blob: 354b2b5936ea537873d6c1a1cfae5f98aa7e0551 [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{
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000154 struct net_device *dev = rx_queue->dev;
Anton Vorontsov8a102fe2009-10-12 06:00:37 +0000155 u32 lstatus;
156
157 bdp->bufPtr = buf;
158
159 lstatus = BD_LFLAG(RXBD_EMPTY | RXBD_INTERRUPT);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000160 if (bdp == rx_queue->rx_bd_base + rx_queue->rx_ring_size - 1)
Anton Vorontsov8a102fe2009-10-12 06:00:37 +0000161 lstatus |= BD_LFLAG(RXBD_WRAP);
162
163 eieio();
164
165 bdp->lstatus = lstatus;
166}
167
Anton Vorontsov87283272009-10-12 06:00:39 +0000168static int gfar_init_bds(struct net_device *ndev)
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000169{
Anton Vorontsov87283272009-10-12 06:00:39 +0000170 struct gfar_private *priv = netdev_priv(ndev);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000171 struct gfar_priv_tx_q *tx_queue = NULL;
172 struct gfar_priv_rx_q *rx_queue = NULL;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000173 struct txbd8 *txbdp;
174 struct rxbd8 *rxbdp;
Anton Vorontsov87283272009-10-12 06:00:39 +0000175 int i;
176
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000177 tx_queue = priv->tx_queue;
178 rx_queue = priv->rx_queue;
179
Anton Vorontsov87283272009-10-12 06:00:39 +0000180 /* Initialize some variables in our dev structure */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000181 tx_queue->num_txbdfree = tx_queue->tx_ring_size;
182 tx_queue->dirty_tx = tx_queue->cur_tx = tx_queue->tx_bd_base;
183 rx_queue->cur_rx = rx_queue->rx_bd_base;
184 tx_queue->skb_curtx = tx_queue->skb_dirtytx = 0;
185 rx_queue->skb_currx = 0;
Anton Vorontsov87283272009-10-12 06:00:39 +0000186
187 /* Initialize Transmit Descriptor Ring */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000188 txbdp = tx_queue->tx_bd_base;
189 for (i = 0; i < tx_queue->tx_ring_size; i++) {
Anton Vorontsov87283272009-10-12 06:00:39 +0000190 txbdp->lstatus = 0;
191 txbdp->bufPtr = 0;
192 txbdp++;
193 }
194
195 /* Set the last descriptor in the ring to indicate wrap */
196 txbdp--;
197 txbdp->status |= TXBD_WRAP;
198
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000199 rxbdp = rx_queue->rx_bd_base;
200 for (i = 0; i < rx_queue->rx_ring_size; i++) {
201 struct sk_buff *skb = rx_queue->rx_skbuff[i];
Anton Vorontsov87283272009-10-12 06:00:39 +0000202
203 if (skb) {
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000204 gfar_init_rxbdp(rx_queue, rxbdp, rxbdp->bufPtr);
Anton Vorontsov87283272009-10-12 06:00:39 +0000205 } else {
206 skb = gfar_new_skb(ndev);
207 if (!skb) {
208 pr_err("%s: Can't allocate RX buffers\n",
209 ndev->name);
210 return -ENOMEM;
211 }
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000212 rx_queue->rx_skbuff[i] = skb;
Anton Vorontsov87283272009-10-12 06:00:39 +0000213
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000214 gfar_new_rxbdp(rx_queue, rxbdp, skb);
Anton Vorontsov87283272009-10-12 06:00:39 +0000215 }
216
217 rxbdp++;
218 }
219
220 return 0;
221}
222
223static int gfar_alloc_skb_resources(struct net_device *ndev)
224{
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000225 void *vaddr;
226 int i;
227 struct gfar_private *priv = netdev_priv(ndev);
228 struct device *dev = &priv->ofdev->dev;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000229 struct gfar_priv_tx_q *tx_queue = NULL;
230 struct gfar_priv_rx_q *rx_queue = NULL;
231
232 tx_queue = priv->tx_queue;
233 rx_queue = priv->rx_queue;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000234
235 /* Allocate memory for the buffer descriptors */
Anton Vorontsov87283272009-10-12 06:00:39 +0000236 vaddr = dma_alloc_coherent(dev,
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000237 sizeof(*tx_queue->tx_bd_base) * tx_queue->tx_ring_size +
238 sizeof(*rx_queue->rx_bd_base) * rx_queue->rx_ring_size,
239 &tx_queue->tx_bd_dma_base, GFP_KERNEL);
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000240 if (!vaddr) {
241 if (netif_msg_ifup(priv))
242 pr_err("%s: Could not allocate buffer descriptors!\n",
243 ndev->name);
244 return -ENOMEM;
245 }
246
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000247 tx_queue->tx_bd_base = vaddr;
248 tx_queue->dev = ndev;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000249
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000250 /* Start the rx descriptor ring where the tx ring leaves off */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000251 vaddr = vaddr + sizeof(*tx_queue->tx_bd_base) * tx_queue->tx_ring_size;
252 rx_queue->rx_bd_base = vaddr;
253 rx_queue->dev = ndev;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000254
255 /* Setup the skbuff rings */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000256 tx_queue->tx_skbuff = kmalloc(sizeof(*tx_queue->tx_skbuff) *
257 tx_queue->tx_ring_size, GFP_KERNEL);
258 if (!tx_queue->tx_skbuff) {
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000259 if (netif_msg_ifup(priv))
260 pr_err("%s: Could not allocate tx_skbuff\n",
261 ndev->name);
262 goto cleanup;
263 }
264
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000265 for (i = 0; i < tx_queue->tx_ring_size; i++)
266 tx_queue->tx_skbuff[i] = NULL;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000267
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000268 rx_queue->rx_skbuff = kmalloc(sizeof(*rx_queue->rx_skbuff) *
269 rx_queue->rx_ring_size, GFP_KERNEL);
270 if (!rx_queue->rx_skbuff) {
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000271 if (netif_msg_ifup(priv))
272 pr_err("%s: Could not allocate rx_skbuff\n",
273 ndev->name);
274 goto cleanup;
275 }
276
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000277 for (i = 0; i < rx_queue->rx_ring_size; i++)
278 rx_queue->rx_skbuff[i] = NULL;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000279
Anton Vorontsov87283272009-10-12 06:00:39 +0000280 if (gfar_init_bds(ndev))
281 goto cleanup;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000282
283 return 0;
284
285cleanup:
286 free_skb_resources(priv);
287 return -ENOMEM;
288}
289
290static void gfar_init_mac(struct net_device *ndev)
291{
292 struct gfar_private *priv = netdev_priv(ndev);
293 struct gfar __iomem *regs = priv->regs;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000294 struct gfar_priv_tx_q *tx_queue = NULL;
295 struct gfar_priv_rx_q *rx_queue = NULL;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000296 u32 rctrl = 0;
297 u32 tctrl = 0;
298 u32 attrs = 0;
299
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000300 tx_queue = priv->tx_queue;
301 rx_queue = priv->rx_queue;
302
Anton Vorontsov32c513b2009-10-12 06:00:36 +0000303 /* enet DMA only understands physical addresses */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000304 gfar_write(&regs->tbase0, tx_queue->tx_bd_dma_base);
305 gfar_write(&regs->rbase0, tx_queue->tx_bd_dma_base +
306 sizeof(*tx_queue->tx_bd_base) *
307 tx_queue->tx_ring_size);
Anton Vorontsov32c513b2009-10-12 06:00:36 +0000308
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000309 /* Configure the coalescing support */
310 gfar_write(&regs->txic, 0);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000311 if (tx_queue->txcoalescing)
312 gfar_write(&regs->txic, tx_queue->txic);
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000313
314 gfar_write(&regs->rxic, 0);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000315 if (rx_queue->rxcoalescing)
316 gfar_write(&regs->rxic, rx_queue->rxic);
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000317
318 if (priv->rx_csum_enable)
319 rctrl |= RCTRL_CHECKSUMMING;
320
321 if (priv->extended_hash) {
322 rctrl |= RCTRL_EXTHASH;
323
324 gfar_clear_exact_match(ndev);
325 rctrl |= RCTRL_EMEN;
326 }
327
328 if (priv->padding) {
329 rctrl &= ~RCTRL_PAL_MASK;
330 rctrl |= RCTRL_PADDING(priv->padding);
331 }
332
333 /* keep vlan related bits if it's enabled */
334 if (priv->vlgrp) {
335 rctrl |= RCTRL_VLEX | RCTRL_PRSDEP_INIT;
336 tctrl |= TCTRL_VLINS;
337 }
338
339 /* Init rctrl based on our settings */
340 gfar_write(&regs->rctrl, rctrl);
341
342 if (ndev->features & NETIF_F_IP_CSUM)
343 tctrl |= TCTRL_INIT_CSUM;
344
345 gfar_write(&regs->tctrl, tctrl);
346
347 /* Set the extraction length and index */
348 attrs = ATTRELI_EL(priv->rx_stash_size) |
349 ATTRELI_EI(priv->rx_stash_index);
350
351 gfar_write(&regs->attreli, attrs);
352
353 /* Start with defaults, and add stashing or locking
354 * depending on the approprate variables */
355 attrs = ATTR_INIT_SETTINGS;
356
357 if (priv->bd_stash_en)
358 attrs |= ATTR_BDSTASH;
359
360 if (priv->rx_stash_size != 0)
361 attrs |= ATTR_BUFSTASH;
362
363 gfar_write(&regs->attr, attrs);
364
365 gfar_write(&regs->fifo_tx_thr, priv->fifo_threshold);
366 gfar_write(&regs->fifo_tx_starve, priv->fifo_starve);
367 gfar_write(&regs->fifo_tx_starve_shutoff, priv->fifo_starve_off);
368}
369
Andy Fleming26ccfc32009-03-10 12:58:28 +0000370static const struct net_device_ops gfar_netdev_ops = {
371 .ndo_open = gfar_enet_open,
372 .ndo_start_xmit = gfar_start_xmit,
373 .ndo_stop = gfar_close,
374 .ndo_change_mtu = gfar_change_mtu,
375 .ndo_set_multicast_list = gfar_set_multi,
376 .ndo_tx_timeout = gfar_timeout,
377 .ndo_do_ioctl = gfar_ioctl,
378 .ndo_vlan_rx_register = gfar_vlan_rx_register,
Ben Hutchings240c1022009-07-09 17:54:35 +0000379 .ndo_set_mac_address = eth_mac_addr,
380 .ndo_validate_addr = eth_validate_addr,
Andy Fleming26ccfc32009-03-10 12:58:28 +0000381#ifdef CONFIG_NET_POLL_CONTROLLER
382 .ndo_poll_controller = gfar_netpoll,
383#endif
384};
385
Andy Fleming7f7f5312005-11-11 12:38:59 -0600386/* Returns 1 if incoming frames use an FCB */
387static inline int gfar_uses_fcb(struct gfar_private *priv)
Kumar Gala0bbaf062005-06-20 10:54:21 -0500388{
Dai Haruki77ecaf22008-12-16 15:30:48 -0800389 return priv->vlgrp || priv->rx_csum_enable;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500390}
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400391
Andy Flemingb31a1d82008-12-16 15:29:15 -0800392static int gfar_of_init(struct net_device *dev)
393{
Andy Flemingb31a1d82008-12-16 15:29:15 -0800394 const char *model;
395 const char *ctype;
396 const void *mac_addr;
Andy Flemingb31a1d82008-12-16 15:29:15 -0800397 u64 addr, size;
398 int err = 0;
399 struct gfar_private *priv = netdev_priv(dev);
400 struct device_node *np = priv->node;
Andy Fleming4d7902f2009-02-04 16:43:44 -0800401 const u32 *stash;
402 const u32 *stash_len;
403 const u32 *stash_idx;
Andy Flemingb31a1d82008-12-16 15:29:15 -0800404
405 if (!np || !of_device_is_available(np))
406 return -ENODEV;
407
408 /* get a pointer to the register memory */
409 addr = of_translate_address(np, of_get_address(np, 0, &size, NULL));
410 priv->regs = ioremap(addr, size);
411
412 if (priv->regs == NULL)
413 return -ENOMEM;
414
415 priv->interruptTransmit = irq_of_parse_and_map(np, 0);
416
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")) {
421 priv->interruptReceive = irq_of_parse_and_map(np, 1);
422
423 priv->interruptError = irq_of_parse_and_map(np, 2);
424
425 if (priv->interruptTransmit < 0 ||
426 priv->interruptReceive < 0 ||
427 priv->interruptError < 0) {
428 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:
494 iounmap(priv->regs);
495 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;
Dai Harukic50a5d92008-12-17 16:51:32 -0800520 int err = 0;
521 int len_devname;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
523 /* Create an ethernet device instance */
524 dev = alloc_etherdev(sizeof (*priv));
525
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400526 if (NULL == dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 return -ENOMEM;
528
529 priv = netdev_priv(dev);
Kumar Gala48268572009-03-18 23:28:22 -0700530 priv->ndev = dev;
531 priv->ofdev = ofdev;
Andy Flemingb31a1d82008-12-16 15:29:15 -0800532 priv->node = ofdev->node;
Kumar Gala48268572009-03-18 23:28:22 -0700533 SET_NETDEV_DEV(dev, &ofdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
Andy Flemingb31a1d82008-12-16 15:29:15 -0800535 err = gfar_of_init(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Andy Flemingb31a1d82008-12-16 15:29:15 -0800537 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 goto regs_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000540 priv->tx_queue = (struct gfar_priv_tx_q *)kmalloc(
541 sizeof (struct gfar_priv_tx_q), GFP_KERNEL);
542 if (!priv->tx_queue)
543 goto regs_fail;
544
545 priv->rx_queue = (struct gfar_priv_rx_q *)kmalloc(
546 sizeof (struct gfar_priv_rx_q), GFP_KERNEL);
547 if (!priv->rx_queue)
548 goto rx_queue_fail;
549
550 spin_lock_init(&priv->tx_queue->txlock);
551 spin_lock_init(&priv->rx_queue->rxlock);
Scott Woodd87eb122008-07-11 18:04:45 -0500552 spin_lock_init(&priv->bflock);
Sebastian Siewiorab939902008-08-19 21:12:45 +0200553 INIT_WORK(&priv->reset_task, gfar_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Andy Flemingb31a1d82008-12-16 15:29:15 -0800555 dev_set_drvdata(&ofdev->dev, priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
557 /* Stop the DMA engine now, in case it was running before */
558 /* (The firmware could have used it, and left it running). */
Andy Fleming257d9382008-12-16 15:25:45 -0800559 gfar_halt(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
561 /* Reset MAC layer */
562 gfar_write(&priv->regs->maccfg1, MACCFG1_SOFT_RESET);
563
Andy Flemingb98ac702009-02-04 16:38:05 -0800564 /* We need to delay at least 3 TX clocks */
565 udelay(2);
566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 tempval = (MACCFG1_TX_FLOW | MACCFG1_RX_FLOW);
568 gfar_write(&priv->regs->maccfg1, tempval);
569
570 /* Initialize MACCFG2. */
571 gfar_write(&priv->regs->maccfg2, MACCFG2_INIT_SETTINGS);
572
573 /* Initialize ECNTRL */
574 gfar_write(&priv->regs->ecntrl, ECNTRL_INIT_SETTINGS);
575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 /* Set the dev->base_addr to the gfar reg region */
577 dev->base_addr = (unsigned long) (priv->regs);
578
Andy Flemingb31a1d82008-12-16 15:29:15 -0800579 SET_NETDEV_DEV(dev, &ofdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
581 /* Fill in the dev structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 dev->watchdog_timeo = TX_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 dev->mtu = 1500;
Andy Fleming26ccfc32009-03-10 12:58:28 +0000584 dev->netdev_ops = &gfar_netdev_ops;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500585 dev->ethtool_ops = &gfar_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000587 /* Register for napi ...NAPI is for each rx_queue */
588 netif_napi_add(dev, &priv->rx_queue->napi, gfar_poll, GFAR_DEV_WEIGHT);
589
Andy Flemingb31a1d82008-12-16 15:29:15 -0800590 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_CSUM) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500591 priv->rx_csum_enable = 1;
Dai Haruki4669bc92008-12-17 16:51:04 -0800592 dev->features |= NETIF_F_IP_CSUM | NETIF_F_SG | NETIF_F_HIGHDMA;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500593 } else
594 priv->rx_csum_enable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Kumar Gala0bbaf062005-06-20 10:54:21 -0500596 priv->vlgrp = NULL;
597
Andy Fleming26ccfc32009-03-10 12:58:28 +0000598 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_VLAN)
Kumar Gala0bbaf062005-06-20 10:54:21 -0500599 dev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500600
Andy Flemingb31a1d82008-12-16 15:29:15 -0800601 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_EXTENDED_HASH) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500602 priv->extended_hash = 1;
603 priv->hash_width = 9;
604
605 priv->hash_regs[0] = &priv->regs->igaddr0;
606 priv->hash_regs[1] = &priv->regs->igaddr1;
607 priv->hash_regs[2] = &priv->regs->igaddr2;
608 priv->hash_regs[3] = &priv->regs->igaddr3;
609 priv->hash_regs[4] = &priv->regs->igaddr4;
610 priv->hash_regs[5] = &priv->regs->igaddr5;
611 priv->hash_regs[6] = &priv->regs->igaddr6;
612 priv->hash_regs[7] = &priv->regs->igaddr7;
613 priv->hash_regs[8] = &priv->regs->gaddr0;
614 priv->hash_regs[9] = &priv->regs->gaddr1;
615 priv->hash_regs[10] = &priv->regs->gaddr2;
616 priv->hash_regs[11] = &priv->regs->gaddr3;
617 priv->hash_regs[12] = &priv->regs->gaddr4;
618 priv->hash_regs[13] = &priv->regs->gaddr5;
619 priv->hash_regs[14] = &priv->regs->gaddr6;
620 priv->hash_regs[15] = &priv->regs->gaddr7;
621
622 } else {
623 priv->extended_hash = 0;
624 priv->hash_width = 8;
625
626 priv->hash_regs[0] = &priv->regs->gaddr0;
Andy Fleming1577ece2009-02-04 16:42:12 -0800627 priv->hash_regs[1] = &priv->regs->gaddr1;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500628 priv->hash_regs[2] = &priv->regs->gaddr2;
629 priv->hash_regs[3] = &priv->regs->gaddr3;
630 priv->hash_regs[4] = &priv->regs->gaddr4;
631 priv->hash_regs[5] = &priv->regs->gaddr5;
632 priv->hash_regs[6] = &priv->regs->gaddr6;
633 priv->hash_regs[7] = &priv->regs->gaddr7;
634 }
635
Andy Flemingb31a1d82008-12-16 15:29:15 -0800636 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_PADDING)
Kumar Gala0bbaf062005-06-20 10:54:21 -0500637 priv->padding = DEFAULT_PADDING;
638 else
639 priv->padding = 0;
640
Kumar Gala0bbaf062005-06-20 10:54:21 -0500641 if (dev->features & NETIF_F_IP_CSUM)
642 dev->hard_header_len += GMAC_FCB_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
644 priv->rx_buffer_size = DEFAULT_RX_BUFFER_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000646 /* Initializing some of the rx/tx queue level parameters */
647 priv->tx_queue->tx_ring_size = DEFAULT_TX_RING_SIZE;
648 priv->tx_queue->num_txbdfree = DEFAULT_TX_RING_SIZE;
649 priv->tx_queue->txcoalescing = DEFAULT_TX_COALESCE;
650 priv->tx_queue->txic = DEFAULT_TXIC;
651
652 priv->rx_queue->rx_ring_size = DEFAULT_RX_RING_SIZE;
653 priv->rx_queue->rxcoalescing = DEFAULT_RX_COALESCE;
654 priv->rx_queue->rxic = DEFAULT_RXIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
Kumar Gala0bbaf062005-06-20 10:54:21 -0500656 /* Enable most messages by default */
657 priv->msg_enable = (NETIF_MSG_IFUP << 1 ) - 1;
658
Trent Piephod3eab822008-10-02 11:12:24 +0000659 /* Carrier starts down, phylib will bring it up */
660 netif_carrier_off(dev);
661
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 err = register_netdev(dev);
663
664 if (err) {
665 printk(KERN_ERR "%s: Cannot register net device, aborting.\n",
666 dev->name);
667 goto register_fail;
668 }
669
Anton Vorontsov2884e5c2009-02-01 00:52:34 -0800670 device_init_wakeup(&dev->dev,
671 priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
672
Dai Harukic50a5d92008-12-17 16:51:32 -0800673 /* fill out IRQ number and name fields */
674 len_devname = strlen(dev->name);
675 strncpy(&priv->int_name_tx[0], dev->name, len_devname);
676 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
677 strncpy(&priv->int_name_tx[len_devname],
678 "_tx", sizeof("_tx") + 1);
679
680 strncpy(&priv->int_name_rx[0], dev->name, len_devname);
681 strncpy(&priv->int_name_rx[len_devname],
682 "_rx", sizeof("_rx") + 1);
683
684 strncpy(&priv->int_name_er[0], dev->name, len_devname);
685 strncpy(&priv->int_name_er[len_devname],
686 "_er", sizeof("_er") + 1);
687 } else
688 priv->int_name_tx[len_devname] = '\0';
689
Andy Fleming7f7f5312005-11-11 12:38:59 -0600690 /* Create all the sysfs files */
691 gfar_init_sysfs(dev);
692
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 /* Print out the device info */
Johannes Berge1749612008-10-27 15:59:26 -0700694 printk(KERN_INFO DEVICE_NAME "%pM\n", dev->name, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
696 /* Even more device info helps when determining which kernel */
Andy Fleming7f7f5312005-11-11 12:38:59 -0600697 /* provided which set of benchmarks. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 printk(KERN_INFO "%s: Running with NAPI enabled\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 printk(KERN_INFO "%s: %d/%d RX/TX BD ring size\n",
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000700 dev->name, priv->rx_queue->rx_ring_size, priv->tx_queue->tx_ring_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
702 return 0;
703
704register_fail:
Kumar Galacc8c6e32006-02-01 15:18:03 -0600705 iounmap(priv->regs);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000706 kfree(priv->rx_queue);
707rx_queue_fail:
708 kfree(priv->tx_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709regs_fail:
Grant Likelyfe192a42009-04-25 12:53:12 +0000710 if (priv->phy_node)
711 of_node_put(priv->phy_node);
712 if (priv->tbi_node)
713 of_node_put(priv->tbi_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 free_netdev(dev);
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400715 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716}
717
Andy Flemingb31a1d82008-12-16 15:29:15 -0800718static int gfar_remove(struct of_device *ofdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719{
Andy Flemingb31a1d82008-12-16 15:29:15 -0800720 struct gfar_private *priv = dev_get_drvdata(&ofdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
Grant Likelyfe192a42009-04-25 12:53:12 +0000722 if (priv->phy_node)
723 of_node_put(priv->phy_node);
724 if (priv->tbi_node)
725 of_node_put(priv->tbi_node);
726
Andy Flemingb31a1d82008-12-16 15:29:15 -0800727 dev_set_drvdata(&ofdev->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
David S. Millerd9d8e042009-09-06 01:41:02 -0700729 unregister_netdev(priv->ndev);
Kumar Galacc8c6e32006-02-01 15:18:03 -0600730 iounmap(priv->regs);
Kumar Gala48268572009-03-18 23:28:22 -0700731 free_netdev(priv->ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
733 return 0;
734}
735
Scott Woodd87eb122008-07-11 18:04:45 -0500736#ifdef CONFIG_PM
Anton Vorontsovbe926fc2009-10-12 06:00:42 +0000737
738static int gfar_suspend(struct device *dev)
Scott Woodd87eb122008-07-11 18:04:45 -0500739{
Anton Vorontsovbe926fc2009-10-12 06:00:42 +0000740 struct gfar_private *priv = dev_get_drvdata(dev);
741 struct net_device *ndev = priv->ndev;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000742 struct gfar_priv_tx_q *tx_queue = NULL;
743 struct gfar_priv_rx_q *rx_queue = NULL;
Scott Woodd87eb122008-07-11 18:04:45 -0500744 unsigned long flags;
745 u32 tempval;
746
747 int magic_packet = priv->wol_en &&
Andy Flemingb31a1d82008-12-16 15:29:15 -0800748 (priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
Scott Woodd87eb122008-07-11 18:04:45 -0500749
Anton Vorontsovbe926fc2009-10-12 06:00:42 +0000750 netif_device_detach(ndev);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000751 tx_queue = priv->tx_queue;
752 rx_queue = priv->rx_queue;
Scott Woodd87eb122008-07-11 18:04:45 -0500753
Anton Vorontsovbe926fc2009-10-12 06:00:42 +0000754 if (netif_running(ndev)) {
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000755 spin_lock_irqsave(&tx_queue->txlock, flags);
756 spin_lock(&rx_queue->rxlock);
Scott Woodd87eb122008-07-11 18:04:45 -0500757
Anton Vorontsovbe926fc2009-10-12 06:00:42 +0000758 gfar_halt_nodisable(ndev);
Scott Woodd87eb122008-07-11 18:04:45 -0500759
760 /* Disable Tx, and Rx if wake-on-LAN is disabled. */
761 tempval = gfar_read(&priv->regs->maccfg1);
762
763 tempval &= ~MACCFG1_TX_EN;
764
765 if (!magic_packet)
766 tempval &= ~MACCFG1_RX_EN;
767
768 gfar_write(&priv->regs->maccfg1, tempval);
769
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000770 spin_unlock(&rx_queue->rxlock);
771 spin_unlock_irqrestore(&tx_queue->txlock, flags);
Scott Woodd87eb122008-07-11 18:04:45 -0500772
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000773 napi_disable(&rx_queue->napi);
Scott Woodd87eb122008-07-11 18:04:45 -0500774
775 if (magic_packet) {
776 /* Enable interrupt on Magic Packet */
777 gfar_write(&priv->regs->imask, IMASK_MAG);
778
779 /* Enable Magic Packet mode */
780 tempval = gfar_read(&priv->regs->maccfg2);
781 tempval |= MACCFG2_MPEN;
782 gfar_write(&priv->regs->maccfg2, tempval);
783 } else {
784 phy_stop(priv->phydev);
785 }
786 }
787
788 return 0;
789}
790
Anton Vorontsovbe926fc2009-10-12 06:00:42 +0000791static int gfar_resume(struct device *dev)
Scott Woodd87eb122008-07-11 18:04:45 -0500792{
Anton Vorontsovbe926fc2009-10-12 06:00:42 +0000793 struct gfar_private *priv = dev_get_drvdata(dev);
794 struct net_device *ndev = priv->ndev;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000795 struct gfar_priv_tx_q *tx_queue = NULL;
796 struct gfar_priv_rx_q *rx_queue = NULL;
Scott Woodd87eb122008-07-11 18:04:45 -0500797 unsigned long flags;
798 u32 tempval;
799 int magic_packet = priv->wol_en &&
Andy Flemingb31a1d82008-12-16 15:29:15 -0800800 (priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
Scott Woodd87eb122008-07-11 18:04:45 -0500801
Anton Vorontsovbe926fc2009-10-12 06:00:42 +0000802 if (!netif_running(ndev)) {
803 netif_device_attach(ndev);
Scott Woodd87eb122008-07-11 18:04:45 -0500804 return 0;
805 }
806
807 if (!magic_packet && priv->phydev)
808 phy_start(priv->phydev);
809
810 /* Disable Magic Packet mode, in case something
811 * else woke us up.
812 */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000813 rx_queue = priv->rx_queue;
814 tx_queue = priv->tx_queue;
Scott Woodd87eb122008-07-11 18:04:45 -0500815
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000816 spin_lock_irqsave(&tx_queue->txlock, flags);
817 spin_lock(&rx_queue->rxlock);
Scott Woodd87eb122008-07-11 18:04:45 -0500818
819 tempval = gfar_read(&priv->regs->maccfg2);
820 tempval &= ~MACCFG2_MPEN;
821 gfar_write(&priv->regs->maccfg2, tempval);
822
Anton Vorontsovbe926fc2009-10-12 06:00:42 +0000823 gfar_start(ndev);
Scott Woodd87eb122008-07-11 18:04:45 -0500824
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000825 spin_unlock(&rx_queue->rxlock);
826 spin_unlock_irqrestore(&tx_queue->txlock, flags);
Scott Woodd87eb122008-07-11 18:04:45 -0500827
Anton Vorontsovbe926fc2009-10-12 06:00:42 +0000828 netif_device_attach(ndev);
Scott Woodd87eb122008-07-11 18:04:45 -0500829
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000830 napi_enable(&rx_queue->napi);
Scott Woodd87eb122008-07-11 18:04:45 -0500831
832 return 0;
833}
Anton Vorontsovbe926fc2009-10-12 06:00:42 +0000834
835static int gfar_restore(struct device *dev)
836{
837 struct gfar_private *priv = dev_get_drvdata(dev);
838 struct net_device *ndev = priv->ndev;
839
840 if (!netif_running(ndev))
841 return 0;
842
843 gfar_init_bds(ndev);
844 init_registers(ndev);
845 gfar_set_mac_address(ndev);
846 gfar_init_mac(ndev);
847 gfar_start(ndev);
848
849 priv->oldlink = 0;
850 priv->oldspeed = 0;
851 priv->oldduplex = -1;
852
853 if (priv->phydev)
854 phy_start(priv->phydev);
855
856 netif_device_attach(ndev);
857 napi_enable(&priv->napi);
858
859 return 0;
860}
861
862static struct dev_pm_ops gfar_pm_ops = {
863 .suspend = gfar_suspend,
864 .resume = gfar_resume,
865 .freeze = gfar_suspend,
866 .thaw = gfar_resume,
867 .restore = gfar_restore,
868};
869
870#define GFAR_PM_OPS (&gfar_pm_ops)
871
872static int gfar_legacy_suspend(struct of_device *ofdev, pm_message_t state)
873{
874 return gfar_suspend(&ofdev->dev);
875}
876
877static int gfar_legacy_resume(struct of_device *ofdev)
878{
879 return gfar_resume(&ofdev->dev);
880}
881
Scott Woodd87eb122008-07-11 18:04:45 -0500882#else
Anton Vorontsovbe926fc2009-10-12 06:00:42 +0000883
884#define GFAR_PM_OPS NULL
885#define gfar_legacy_suspend NULL
886#define gfar_legacy_resume NULL
887
Scott Woodd87eb122008-07-11 18:04:45 -0500888#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600890/* Reads the controller's registers to determine what interface
891 * connects it to the PHY.
892 */
893static phy_interface_t gfar_get_interface(struct net_device *dev)
894{
895 struct gfar_private *priv = netdev_priv(dev);
896 u32 ecntrl = gfar_read(&priv->regs->ecntrl);
897
898 if (ecntrl & ECNTRL_SGMII_MODE)
899 return PHY_INTERFACE_MODE_SGMII;
900
901 if (ecntrl & ECNTRL_TBI_MODE) {
902 if (ecntrl & ECNTRL_REDUCED_MODE)
903 return PHY_INTERFACE_MODE_RTBI;
904 else
905 return PHY_INTERFACE_MODE_TBI;
906 }
907
908 if (ecntrl & ECNTRL_REDUCED_MODE) {
909 if (ecntrl & ECNTRL_REDUCED_MII_MODE)
910 return PHY_INTERFACE_MODE_RMII;
Andy Fleming7132ab72007-07-11 11:43:07 -0500911 else {
Andy Flemingb31a1d82008-12-16 15:29:15 -0800912 phy_interface_t interface = priv->interface;
Andy Fleming7132ab72007-07-11 11:43:07 -0500913
914 /*
915 * This isn't autodetected right now, so it must
916 * be set by the device tree or platform code.
917 */
918 if (interface == PHY_INTERFACE_MODE_RGMII_ID)
919 return PHY_INTERFACE_MODE_RGMII_ID;
920
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600921 return PHY_INTERFACE_MODE_RGMII;
Andy Fleming7132ab72007-07-11 11:43:07 -0500922 }
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600923 }
924
Andy Flemingb31a1d82008-12-16 15:29:15 -0800925 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT)
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600926 return PHY_INTERFACE_MODE_GMII;
927
928 return PHY_INTERFACE_MODE_MII;
929}
930
931
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400932/* Initializes driver's PHY state, and attaches to the PHY.
933 * Returns 0 on success.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 */
935static int init_phy(struct net_device *dev)
936{
937 struct gfar_private *priv = netdev_priv(dev);
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400938 uint gigabit_support =
Andy Flemingb31a1d82008-12-16 15:29:15 -0800939 priv->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT ?
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400940 SUPPORTED_1000baseT_Full : 0;
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600941 phy_interface_t interface;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
943 priv->oldlink = 0;
944 priv->oldspeed = 0;
945 priv->oldduplex = -1;
946
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600947 interface = gfar_get_interface(dev);
948
Anton Vorontsov1db780f2009-07-16 21:31:42 +0000949 priv->phydev = of_phy_connect(dev, priv->phy_node, &adjust_link, 0,
950 interface);
951 if (!priv->phydev)
952 priv->phydev = of_phy_connect_fixed_link(dev, &adjust_link,
953 interface);
954 if (!priv->phydev) {
955 dev_err(&dev->dev, "could not attach to PHY\n");
956 return -ENODEV;
Grant Likelyfe192a42009-04-25 12:53:12 +0000957 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
Kapil Junejad3c12872007-05-11 18:25:11 -0500959 if (interface == PHY_INTERFACE_MODE_SGMII)
960 gfar_configure_serdes(dev);
961
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400962 /* Remove any features not supported by the controller */
Grant Likelyfe192a42009-04-25 12:53:12 +0000963 priv->phydev->supported &= (GFAR_SUPPORTED | gigabit_support);
964 priv->phydev->advertising = priv->phydev->supported;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
966 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967}
968
Paul Gortmakerd0313582008-04-17 00:08:10 -0400969/*
970 * Initialize TBI PHY interface for communicating with the
971 * SERDES lynx PHY on the chip. We communicate with this PHY
972 * through the MDIO bus on each controller, treating it as a
973 * "normal" PHY at the address found in the TBIPA register. We assume
974 * that the TBIPA register is valid. Either the MDIO bus code will set
975 * it to a value that doesn't conflict with other PHYs on the bus, or the
976 * value doesn't matter, as there are no other PHYs on the bus.
977 */
Kapil Junejad3c12872007-05-11 18:25:11 -0500978static void gfar_configure_serdes(struct net_device *dev)
979{
980 struct gfar_private *priv = netdev_priv(dev);
Grant Likelyfe192a42009-04-25 12:53:12 +0000981 struct phy_device *tbiphy;
Trent Piephoc1324192008-10-30 18:17:06 -0700982
Grant Likelyfe192a42009-04-25 12:53:12 +0000983 if (!priv->tbi_node) {
984 dev_warn(&dev->dev, "error: SGMII mode requires that the "
985 "device tree specify a tbi-handle\n");
986 return;
987 }
988
989 tbiphy = of_phy_find_device(priv->tbi_node);
990 if (!tbiphy) {
991 dev_err(&dev->dev, "error: Could not get TBI device\n");
Andy Flemingb31a1d82008-12-16 15:29:15 -0800992 return;
993 }
Kapil Junejad3c12872007-05-11 18:25:11 -0500994
Andy Flemingb31a1d82008-12-16 15:29:15 -0800995 /*
996 * If the link is already up, we must already be ok, and don't need to
Trent Piephobdb59f92008-10-30 18:17:07 -0700997 * configure and reset the TBI<->SerDes link. Maybe U-Boot configured
998 * everything for us? Resetting it takes the link down and requires
999 * several seconds for it to come back.
1000 */
Grant Likelyfe192a42009-04-25 12:53:12 +00001001 if (phy_read(tbiphy, MII_BMSR) & BMSR_LSTATUS)
Andy Flemingb31a1d82008-12-16 15:29:15 -08001002 return;
Kapil Junejad3c12872007-05-11 18:25:11 -05001003
Paul Gortmakerd0313582008-04-17 00:08:10 -04001004 /* Single clk mode, mii mode off(for serdes communication) */
Grant Likelyfe192a42009-04-25 12:53:12 +00001005 phy_write(tbiphy, MII_TBICON, TBICON_CLK_SELECT);
Kapil Junejad3c12872007-05-11 18:25:11 -05001006
Grant Likelyfe192a42009-04-25 12:53:12 +00001007 phy_write(tbiphy, MII_ADVERTISE,
Kapil Junejad3c12872007-05-11 18:25:11 -05001008 ADVERTISE_1000XFULL | ADVERTISE_1000XPAUSE |
1009 ADVERTISE_1000XPSE_ASYM);
1010
Grant Likelyfe192a42009-04-25 12:53:12 +00001011 phy_write(tbiphy, MII_BMCR, BMCR_ANENABLE |
Kapil Junejad3c12872007-05-11 18:25:11 -05001012 BMCR_ANRESTART | BMCR_FULLDPLX | BMCR_SPEED1000);
1013}
1014
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015static void init_registers(struct net_device *dev)
1016{
1017 struct gfar_private *priv = netdev_priv(dev);
1018
1019 /* Clear IEVENT */
1020 gfar_write(&priv->regs->ievent, IEVENT_INIT_CLEAR);
1021
1022 /* Initialize IMASK */
1023 gfar_write(&priv->regs->imask, IMASK_INIT_CLEAR);
1024
1025 /* Init hash registers to zero */
Kumar Gala0bbaf062005-06-20 10:54:21 -05001026 gfar_write(&priv->regs->igaddr0, 0);
1027 gfar_write(&priv->regs->igaddr1, 0);
1028 gfar_write(&priv->regs->igaddr2, 0);
1029 gfar_write(&priv->regs->igaddr3, 0);
1030 gfar_write(&priv->regs->igaddr4, 0);
1031 gfar_write(&priv->regs->igaddr5, 0);
1032 gfar_write(&priv->regs->igaddr6, 0);
1033 gfar_write(&priv->regs->igaddr7, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
1035 gfar_write(&priv->regs->gaddr0, 0);
1036 gfar_write(&priv->regs->gaddr1, 0);
1037 gfar_write(&priv->regs->gaddr2, 0);
1038 gfar_write(&priv->regs->gaddr3, 0);
1039 gfar_write(&priv->regs->gaddr4, 0);
1040 gfar_write(&priv->regs->gaddr5, 0);
1041 gfar_write(&priv->regs->gaddr6, 0);
1042 gfar_write(&priv->regs->gaddr7, 0);
1043
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 /* Zero out the rmon mib registers if it has them */
Andy Flemingb31a1d82008-12-16 15:29:15 -08001045 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_RMON) {
Kumar Galacc8c6e32006-02-01 15:18:03 -06001046 memset_io(&(priv->regs->rmon), 0, sizeof (struct rmon_mib));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
1048 /* Mask off the CAM interrupts */
1049 gfar_write(&priv->regs->rmon.cam1, 0xffffffff);
1050 gfar_write(&priv->regs->rmon.cam2, 0xffffffff);
1051 }
1052
1053 /* Initialize the max receive buffer length */
1054 gfar_write(&priv->regs->mrblr, priv->rx_buffer_size);
1055
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 /* Initialize the Minimum Frame Length Register */
1057 gfar_write(&priv->regs->minflr, MINFLR_INIT_SETTINGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058}
1059
Kumar Gala0bbaf062005-06-20 10:54:21 -05001060
1061/* Halt the receive and transmit queues */
Scott Woodd87eb122008-07-11 18:04:45 -05001062static void gfar_halt_nodisable(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063{
1064 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -06001065 struct gfar __iomem *regs = priv->regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 u32 tempval;
1067
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 /* Mask all interrupts */
1069 gfar_write(&regs->imask, IMASK_INIT_CLEAR);
1070
1071 /* Clear all interrupts */
1072 gfar_write(&regs->ievent, IEVENT_INIT_CLEAR);
1073
1074 /* Stop the DMA, and wait for it to stop */
1075 tempval = gfar_read(&priv->regs->dmactrl);
1076 if ((tempval & (DMACTRL_GRS | DMACTRL_GTS))
1077 != (DMACTRL_GRS | DMACTRL_GTS)) {
1078 tempval |= (DMACTRL_GRS | DMACTRL_GTS);
1079 gfar_write(&priv->regs->dmactrl, tempval);
1080
1081 while (!(gfar_read(&priv->regs->ievent) &
1082 (IEVENT_GRSC | IEVENT_GTSC)))
1083 cpu_relax();
1084 }
Scott Woodd87eb122008-07-11 18:04:45 -05001085}
Scott Woodd87eb122008-07-11 18:04:45 -05001086
1087/* Halt the receive and transmit queues */
1088void gfar_halt(struct net_device *dev)
1089{
1090 struct gfar_private *priv = netdev_priv(dev);
1091 struct gfar __iomem *regs = priv->regs;
1092 u32 tempval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
Scott Wood2a54adc2008-08-12 15:10:46 -05001094 gfar_halt_nodisable(dev);
1095
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 /* Disable Rx and Tx */
1097 tempval = gfar_read(&regs->maccfg1);
1098 tempval &= ~(MACCFG1_RX_EN | MACCFG1_TX_EN);
1099 gfar_write(&regs->maccfg1, tempval);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001100}
1101
1102void stop_gfar(struct net_device *dev)
1103{
1104 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001105 struct gfar_priv_tx_q *tx_queue = NULL;
1106 struct gfar_priv_rx_q *rx_queue = NULL;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001107 unsigned long flags;
1108
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001109 phy_stop(priv->phydev);
1110
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001111 tx_queue = priv->tx_queue;
1112 rx_queue = priv->rx_queue;
1113
Kumar Gala0bbaf062005-06-20 10:54:21 -05001114 /* Lock it down */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001115 spin_lock_irqsave(&tx_queue->txlock, flags);
1116 spin_lock(&rx_queue->rxlock);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001117
Kumar Gala0bbaf062005-06-20 10:54:21 -05001118 gfar_halt(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001120 spin_unlock(&rx_queue->rxlock);
1121 spin_unlock_irqrestore(&tx_queue->txlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
1123 /* Free the IRQs */
Andy Flemingb31a1d82008-12-16 15:29:15 -08001124 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 free_irq(priv->interruptError, dev);
1126 free_irq(priv->interruptTransmit, dev);
1127 free_irq(priv->interruptReceive, dev);
1128 } else {
Andy Fleming1577ece2009-02-04 16:42:12 -08001129 free_irq(priv->interruptTransmit, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 }
1131
1132 free_skb_resources(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133}
1134
1135/* If there are any tx skbs or rx skbs still around, free them.
1136 * Then free tx_skbuff and rx_skbuff */
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001137static void free_skb_resources(struct gfar_private *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138{
Anton Vorontsove69edd22009-10-12 06:00:30 +00001139 struct device *dev = &priv->ofdev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 struct rxbd8 *rxbdp;
1141 struct txbd8 *txbdp;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001142 struct gfar_priv_tx_q *tx_queue = NULL;
1143 struct gfar_priv_rx_q *rx_queue = NULL;
Dai Haruki4669bc92008-12-17 16:51:04 -08001144 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
1146 /* Go through all the buffer descriptors and free their data buffers */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001147 tx_queue = priv->tx_queue;
1148 txbdp = tx_queue->tx_bd_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001150 if (!tx_queue->tx_skbuff)
Anton Vorontsove69edd22009-10-12 06:00:30 +00001151 goto skip_tx_skbuff;
1152
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001153 for (i = 0; i < tx_queue->tx_ring_size; i++) {
1154 if (!tx_queue->tx_skbuff[i])
Dai Haruki4669bc92008-12-17 16:51:04 -08001155 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
Kumar Gala48268572009-03-18 23:28:22 -07001157 dma_unmap_single(&priv->ofdev->dev, txbdp->bufPtr,
Dai Haruki4669bc92008-12-17 16:51:04 -08001158 txbdp->length, DMA_TO_DEVICE);
1159 txbdp->lstatus = 0;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001160 for (j = 0; j < skb_shinfo(tx_queue->tx_skbuff[i])->nr_frags; j++) {
Dai Haruki4669bc92008-12-17 16:51:04 -08001161 txbdp++;
Kumar Gala48268572009-03-18 23:28:22 -07001162 dma_unmap_page(&priv->ofdev->dev, txbdp->bufPtr,
Dai Haruki4669bc92008-12-17 16:51:04 -08001163 txbdp->length, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 }
Andy Flemingad5da7a2008-05-07 13:20:55 -05001165 txbdp++;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001166 dev_kfree_skb_any(tx_queue->tx_skbuff[i]);
1167 tx_queue->tx_skbuff[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 }
1169
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001170 kfree(tx_queue->tx_skbuff);
Anton Vorontsove69edd22009-10-12 06:00:30 +00001171skip_tx_skbuff:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001173 rx_queue = priv->rx_queue;
1174 rxbdp = rx_queue->rx_bd_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001176 if (!rx_queue->rx_skbuff)
Anton Vorontsove69edd22009-10-12 06:00:30 +00001177 goto skip_rx_skbuff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001179 for (i = 0; i < rx_queue->rx_ring_size; i++) {
1180 if (rx_queue->rx_skbuff[i]) {
Anton Vorontsove69edd22009-10-12 06:00:30 +00001181 dma_unmap_single(&priv->ofdev->dev, rxbdp->bufPtr,
1182 priv->rx_buffer_size,
1183 DMA_FROM_DEVICE);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001184 dev_kfree_skb_any(rx_queue->rx_skbuff[i]);
1185 rx_queue->rx_skbuff[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 }
1187
Anton Vorontsove69edd22009-10-12 06:00:30 +00001188 rxbdp->lstatus = 0;
1189 rxbdp->bufPtr = 0;
1190 rxbdp++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 }
Anton Vorontsove69edd22009-10-12 06:00:30 +00001192
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001193 kfree(rx_queue->rx_skbuff);
Anton Vorontsove69edd22009-10-12 06:00:30 +00001194skip_rx_skbuff:
1195
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001196 dma_free_coherent(dev, sizeof(*txbdp) * tx_queue->tx_ring_size +
1197 sizeof(*rxbdp) * rx_queue->rx_ring_size,
1198 tx_queue->tx_bd_base, tx_queue->tx_bd_dma_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199}
1200
Kumar Gala0bbaf062005-06-20 10:54:21 -05001201void gfar_start(struct net_device *dev)
1202{
1203 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001204 struct gfar_priv_tx_q *tx_queue;
1205 struct gfar_priv_rx_q *rx_queue;
Kumar Galacc8c6e32006-02-01 15:18:03 -06001206 struct gfar __iomem *regs = priv->regs;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001207 u32 tempval;
1208
1209 /* Enable Rx and Tx in MACCFG1 */
1210 tempval = gfar_read(&regs->maccfg1);
1211 tempval |= (MACCFG1_RX_EN | MACCFG1_TX_EN);
1212 gfar_write(&regs->maccfg1, tempval);
1213
1214 /* Initialize DMACTRL to have WWR and WOP */
1215 tempval = gfar_read(&priv->regs->dmactrl);
1216 tempval |= DMACTRL_INIT_SETTINGS;
1217 gfar_write(&priv->regs->dmactrl, tempval);
1218
Kumar Gala0bbaf062005-06-20 10:54:21 -05001219 /* Make sure we aren't stopped */
1220 tempval = gfar_read(&priv->regs->dmactrl);
1221 tempval &= ~(DMACTRL_GRS | DMACTRL_GTS);
1222 gfar_write(&priv->regs->dmactrl, tempval);
1223
Andy Flemingfef61082006-04-20 16:44:29 -05001224 /* Clear THLT/RHLT, so that the DMA starts polling now */
1225 gfar_write(&regs->tstat, TSTAT_CLEAR_THALT);
1226 gfar_write(&regs->rstat, RSTAT_CLEAR_RHALT);
1227
Kumar Gala0bbaf062005-06-20 10:54:21 -05001228 /* Unmask the interrupts we look for */
1229 gfar_write(&regs->imask, IMASK_DEFAULT);
Dai Haruki12dea572008-12-16 15:30:20 -08001230
1231 dev->trans_start = jiffies;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001232}
1233
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234/* Bring the controller up and running */
Anton Vorontsovccc05c62009-10-12 06:00:26 +00001235int startup_gfar(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236{
Anton Vorontsovccc05c62009-10-12 06:00:26 +00001237 struct gfar_private *priv = netdev_priv(ndev);
Kumar Galacc8c6e32006-02-01 15:18:03 -06001238 struct gfar __iomem *regs = priv->regs;
Anton Vorontsovccc05c62009-10-12 06:00:26 +00001239 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
1241 gfar_write(&regs->imask, IMASK_INIT_CLEAR);
1242
Anton Vorontsov826aa4a2009-10-12 06:00:34 +00001243 err = gfar_alloc_skb_resources(ndev);
1244 if (err)
1245 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
Anton Vorontsov826aa4a2009-10-12 06:00:34 +00001247 gfar_init_mac(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 /* If the device has multiple interrupts, register for
1250 * them. Otherwise, only register for the one */
Andy Flemingb31a1d82008-12-16 15:29:15 -08001251 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001252 /* Install our interrupt handlers for Error,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 * Transmit, and Receive */
Anton Vorontsovccc05c62009-10-12 06:00:26 +00001254 err = request_irq(priv->interruptError, gfar_error, 0,
1255 priv->int_name_er, ndev);
1256 if (err) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001257 if (netif_msg_intr(priv))
Anton Vorontsovccc05c62009-10-12 06:00:26 +00001258 pr_err("%s: Can't get IRQ %d\n", ndev->name,
1259 priv->interruptError);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 goto err_irq_fail;
1261 }
1262
Anton Vorontsovccc05c62009-10-12 06:00:26 +00001263 err = request_irq(priv->interruptTransmit, gfar_transmit, 0,
1264 priv->int_name_tx, ndev);
1265 if (err) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001266 if (netif_msg_intr(priv))
Anton Vorontsovccc05c62009-10-12 06:00:26 +00001267 pr_err("%s: Can't get IRQ %d\n", ndev->name,
1268 priv->interruptTransmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 goto tx_irq_fail;
1270 }
1271
Anton Vorontsovccc05c62009-10-12 06:00:26 +00001272 err = request_irq(priv->interruptReceive, gfar_receive, 0,
1273 priv->int_name_rx, ndev);
1274 if (err) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001275 if (netif_msg_intr(priv))
Anton Vorontsovccc05c62009-10-12 06:00:26 +00001276 pr_err("%s: Can't get IRQ %d (receive0)\n",
1277 ndev->name, priv->interruptReceive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 goto rx_irq_fail;
1279 }
1280 } else {
Anton Vorontsovccc05c62009-10-12 06:00:26 +00001281 err = request_irq(priv->interruptTransmit, gfar_interrupt,
1282 0, priv->int_name_tx, ndev);
1283 if (err) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001284 if (netif_msg_intr(priv))
Anton Vorontsovccc05c62009-10-12 06:00:26 +00001285 pr_err("%s: Can't get IRQ %d\n", ndev->name,
1286 priv->interruptTransmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 goto err_irq_fail;
1288 }
1289 }
1290
Andy Fleming7f7f5312005-11-11 12:38:59 -06001291 /* Start the controller */
Anton Vorontsovccc05c62009-10-12 06:00:26 +00001292 gfar_start(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
Anton Vorontsov826aa4a2009-10-12 06:00:34 +00001294 phy_start(priv->phydev);
1295
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 return 0;
1297
1298rx_irq_fail:
Anton Vorontsovccc05c62009-10-12 06:00:26 +00001299 free_irq(priv->interruptTransmit, ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300tx_irq_fail:
Anton Vorontsovccc05c62009-10-12 06:00:26 +00001301 free_irq(priv->interruptError, ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302err_irq_fail:
Anton Vorontsove69edd22009-10-12 06:00:30 +00001303 free_skb_resources(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 return err;
1305}
1306
1307/* Called when something needs to use the ethernet device */
1308/* Returns 0 for success. */
1309static int gfar_enet_open(struct net_device *dev)
1310{
Li Yang94e8cc32007-10-12 21:53:51 +08001311 struct gfar_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 int err;
1313
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001314 napi_enable(&priv->rx_queue->napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001315
Andy Fleming0fd56bb2009-02-04 16:43:16 -08001316 skb_queue_head_init(&priv->rx_recycle);
1317
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 /* Initialize a bunch of registers */
1319 init_registers(dev);
1320
1321 gfar_set_mac_address(dev);
1322
1323 err = init_phy(dev);
1324
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001325 if (err) {
1326 napi_disable(&priv->rx_queue->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 return err;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001328 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
1330 err = startup_gfar(dev);
Anton Vorontsovdb0e8e32007-10-17 23:57:46 +04001331 if (err) {
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001332 napi_disable(&priv->rx_queue->napi);
Anton Vorontsovdb0e8e32007-10-17 23:57:46 +04001333 return err;
1334 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
1336 netif_start_queue(dev);
1337
Anton Vorontsov2884e5c2009-02-01 00:52:34 -08001338 device_set_wakeup_enable(&dev->dev, priv->wol_en);
1339
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 return err;
1341}
1342
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07001343static inline struct txfcb *gfar_add_fcb(struct sk_buff *skb)
Kumar Gala0bbaf062005-06-20 10:54:21 -05001344{
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07001345 struct txfcb *fcb = (struct txfcb *)skb_push(skb, GMAC_FCB_LEN);
Kumar Gala6c31d552009-04-28 08:04:10 -07001346
1347 memset(fcb, 0, GMAC_FCB_LEN);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001348
Kumar Gala0bbaf062005-06-20 10:54:21 -05001349 return fcb;
1350}
1351
1352static inline void gfar_tx_checksum(struct sk_buff *skb, struct txfcb *fcb)
1353{
Andy Fleming7f7f5312005-11-11 12:38:59 -06001354 u8 flags = 0;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001355
1356 /* If we're here, it's a IP packet with a TCP or UDP
1357 * payload. We set it to checksum, using a pseudo-header
1358 * we provide
1359 */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001360 flags = TXFCB_DEFAULT;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001361
Andy Fleming7f7f5312005-11-11 12:38:59 -06001362 /* Tell the controller what the protocol is */
1363 /* And provide the already calculated phcs */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001364 if (ip_hdr(skb)->protocol == IPPROTO_UDP) {
Andy Fleming7f7f5312005-11-11 12:38:59 -06001365 flags |= TXFCB_UDP;
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -03001366 fcb->phcs = udp_hdr(skb)->check;
Andy Fleming7f7f5312005-11-11 12:38:59 -06001367 } else
Kumar Gala8da32de2007-06-29 00:12:04 -05001368 fcb->phcs = tcp_hdr(skb)->check;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001369
1370 /* l3os is the distance between the start of the
1371 * frame (skb->data) and the start of the IP hdr.
1372 * l4os is the distance between the start of the
1373 * l3 hdr and the l4 hdr */
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03001374 fcb->l3os = (u16)(skb_network_offset(skb) - GMAC_FCB_LEN);
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -03001375 fcb->l4os = skb_network_header_len(skb);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001376
Andy Fleming7f7f5312005-11-11 12:38:59 -06001377 fcb->flags = flags;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001378}
1379
Andy Fleming7f7f5312005-11-11 12:38:59 -06001380void inline gfar_tx_vlan(struct sk_buff *skb, struct txfcb *fcb)
Kumar Gala0bbaf062005-06-20 10:54:21 -05001381{
Andy Fleming7f7f5312005-11-11 12:38:59 -06001382 fcb->flags |= TXFCB_VLN;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001383 fcb->vlctl = vlan_tx_tag_get(skb);
1384}
1385
Dai Haruki4669bc92008-12-17 16:51:04 -08001386static inline struct txbd8 *skip_txbd(struct txbd8 *bdp, int stride,
1387 struct txbd8 *base, int ring_size)
1388{
1389 struct txbd8 *new_bd = bdp + stride;
1390
1391 return (new_bd >= (base + ring_size)) ? (new_bd - ring_size) : new_bd;
1392}
1393
1394static inline struct txbd8 *next_txbd(struct txbd8 *bdp, struct txbd8 *base,
1395 int ring_size)
1396{
1397 return skip_txbd(bdp, 1, base, ring_size);
1398}
1399
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400/* This is called by the kernel when a frame is ready for transmission. */
1401/* It is pointed to by the dev->hard_start_xmit function pointer */
1402static int gfar_start_xmit(struct sk_buff *skb, struct net_device *dev)
1403{
1404 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001405 struct gfar_priv_tx_q *tx_queue = NULL;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001406 struct txfcb *fcb = NULL;
Dai Haruki4669bc92008-12-17 16:51:04 -08001407 struct txbd8 *txbdp, *txbdp_start, *base;
Dai Haruki5a5efed2008-12-16 15:34:50 -08001408 u32 lstatus;
Dai Haruki4669bc92008-12-17 16:51:04 -08001409 int i;
1410 u32 bufaddr;
Andy Flemingfef61082006-04-20 16:44:29 -05001411 unsigned long flags;
Dai Haruki4669bc92008-12-17 16:51:04 -08001412 unsigned int nr_frags, length;
1413
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001414 tx_queue = priv->tx_queue;
1415 base = tx_queue->tx_bd_base;
Dai Haruki4669bc92008-12-17 16:51:04 -08001416
Li Yang5b28bea2009-03-27 15:54:30 -07001417 /* make space for additional header when fcb is needed */
1418 if (((skb->ip_summed == CHECKSUM_PARTIAL) ||
1419 (priv->vlgrp && vlan_tx_tag_present(skb))) &&
1420 (skb_headroom(skb) < GMAC_FCB_LEN)) {
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07001421 struct sk_buff *skb_new;
1422
1423 skb_new = skb_realloc_headroom(skb, GMAC_FCB_LEN);
1424 if (!skb_new) {
1425 dev->stats.tx_errors++;
David S. Millerbd14ba82009-03-27 01:10:58 -07001426 kfree_skb(skb);
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07001427 return NETDEV_TX_OK;
1428 }
1429 kfree_skb(skb);
1430 skb = skb_new;
1431 }
1432
Dai Haruki4669bc92008-12-17 16:51:04 -08001433 /* total number of fragments in the SKB */
1434 nr_frags = skb_shinfo(skb)->nr_frags;
1435
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001436 spin_lock_irqsave(&tx_queue->txlock, flags);
Dai Haruki4669bc92008-12-17 16:51:04 -08001437
1438 /* check if there is space to queue this packet */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001439 if ((nr_frags+1) > tx_queue->num_txbdfree) {
Dai Haruki4669bc92008-12-17 16:51:04 -08001440 /* no space, stop the queue */
1441 netif_stop_queue(dev);
1442 dev->stats.tx_fifo_errors++;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001443 spin_unlock_irqrestore(&tx_queue->txlock, flags);
Dai Haruki4669bc92008-12-17 16:51:04 -08001444 return NETDEV_TX_BUSY;
1445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
1447 /* Update transmit stats */
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001448 dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001450 txbdp = txbdp_start = tx_queue->cur_tx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
Dai Haruki4669bc92008-12-17 16:51:04 -08001452 if (nr_frags == 0) {
1453 lstatus = txbdp->lstatus | BD_LFLAG(TXBD_LAST | TXBD_INTERRUPT);
1454 } else {
1455 /* Place the fragment addresses and lengths into the TxBDs */
1456 for (i = 0; i < nr_frags; i++) {
1457 /* Point at the next BD, wrapping as needed */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001458 txbdp = next_txbd(txbdp, base, tx_queue->tx_ring_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
Dai Haruki4669bc92008-12-17 16:51:04 -08001460 length = skb_shinfo(skb)->frags[i].size;
1461
1462 lstatus = txbdp->lstatus | length |
1463 BD_LFLAG(TXBD_READY);
1464
1465 /* Handle the last BD specially */
1466 if (i == nr_frags - 1)
1467 lstatus |= BD_LFLAG(TXBD_LAST | TXBD_INTERRUPT);
1468
Kumar Gala48268572009-03-18 23:28:22 -07001469 bufaddr = dma_map_page(&priv->ofdev->dev,
Dai Haruki4669bc92008-12-17 16:51:04 -08001470 skb_shinfo(skb)->frags[i].page,
1471 skb_shinfo(skb)->frags[i].page_offset,
1472 length,
1473 DMA_TO_DEVICE);
1474
1475 /* set the TxBD length and buffer pointer */
1476 txbdp->bufPtr = bufaddr;
1477 txbdp->lstatus = lstatus;
1478 }
1479
1480 lstatus = txbdp_start->lstatus;
1481 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
Kumar Gala0bbaf062005-06-20 10:54:21 -05001483 /* Set up checksumming */
Dai Haruki12dea572008-12-16 15:30:20 -08001484 if (CHECKSUM_PARTIAL == skb->ip_summed) {
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07001485 fcb = gfar_add_fcb(skb);
1486 lstatus |= BD_LFLAG(TXBD_TOE);
1487 gfar_tx_checksum(skb, fcb);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001488 }
1489
Dai Haruki77ecaf22008-12-16 15:30:48 -08001490 if (priv->vlgrp && vlan_tx_tag_present(skb)) {
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07001491 if (unlikely(NULL == fcb)) {
1492 fcb = gfar_add_fcb(skb);
Dai Haruki5a5efed2008-12-16 15:34:50 -08001493 lstatus |= BD_LFLAG(TXBD_TOE);
Andy Fleming7f7f5312005-11-11 12:38:59 -06001494 }
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07001495
1496 gfar_tx_vlan(skb, fcb);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001497 }
1498
Dai Haruki4669bc92008-12-17 16:51:04 -08001499 /* setup the TxBD length and buffer pointer for the first BD */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001500 tx_queue->tx_skbuff[tx_queue->skb_curtx] = skb;
Kumar Gala48268572009-03-18 23:28:22 -07001501 txbdp_start->bufPtr = dma_map_single(&priv->ofdev->dev, skb->data,
Dai Haruki4669bc92008-12-17 16:51:04 -08001502 skb_headlen(skb), DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
Dai Haruki4669bc92008-12-17 16:51:04 -08001504 lstatus |= BD_LFLAG(TXBD_CRC | TXBD_READY) | skb_headlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
Dai Haruki4669bc92008-12-17 16:51:04 -08001506 /*
1507 * The powerpc-specific eieio() is used, as wmb() has too strong
Scott Wood3b6330c2007-05-16 15:06:59 -05001508 * semantics (it requires synchronization between cacheable and
1509 * uncacheable mappings, which eieio doesn't provide and which we
1510 * don't need), thus requiring a more expensive sync instruction. At
1511 * some point, the set of architecture-independent barrier functions
1512 * should be expanded to include weaker barriers.
1513 */
Scott Wood3b6330c2007-05-16 15:06:59 -05001514 eieio();
Andy Fleming7f7f5312005-11-11 12:38:59 -06001515
Dai Haruki4669bc92008-12-17 16:51:04 -08001516 txbdp_start->lstatus = lstatus;
1517
1518 /* Update the current skb pointer to the next entry we will use
1519 * (wrapping if necessary) */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001520 tx_queue->skb_curtx = (tx_queue->skb_curtx + 1) &
1521 TX_RING_MOD_MASK(tx_queue->tx_ring_size);
Dai Haruki4669bc92008-12-17 16:51:04 -08001522
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001523 tx_queue->cur_tx = next_txbd(txbdp, base, tx_queue->tx_ring_size);
Dai Haruki4669bc92008-12-17 16:51:04 -08001524
1525 /* reduce TxBD free count */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001526 tx_queue->num_txbdfree -= (nr_frags + 1);
Dai Haruki4669bc92008-12-17 16:51:04 -08001527
1528 dev->trans_start = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
1530 /* If the next BD still needs to be cleaned up, then the bds
1531 are full. We need to tell the kernel to stop sending us stuff. */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001532 if (!tx_queue->num_txbdfree) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 netif_stop_queue(dev);
1534
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001535 dev->stats.tx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 }
1537
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 /* Tell the DMA to go go go */
1539 gfar_write(&priv->regs->tstat, TSTAT_CLEAR_THALT);
1540
1541 /* Unlock priv */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001542 spin_unlock_irqrestore(&tx_queue->txlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07001544 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545}
1546
1547/* Stops the kernel queue, and halts the controller */
1548static int gfar_close(struct net_device *dev)
1549{
1550 struct gfar_private *priv = netdev_priv(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001551
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001552 napi_disable(&priv->rx_queue->napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001553
Andy Fleming0fd56bb2009-02-04 16:43:16 -08001554 skb_queue_purge(&priv->rx_recycle);
Sebastian Siewiorab939902008-08-19 21:12:45 +02001555 cancel_work_sync(&priv->reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 stop_gfar(dev);
1557
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001558 /* Disconnect from the PHY */
1559 phy_disconnect(priv->phydev);
1560 priv->phydev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
1562 netif_stop_queue(dev);
1563
1564 return 0;
1565}
1566
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567/* Changes the mac address if the controller is not running. */
Andy Flemingf162b9d2008-05-02 13:00:30 -05001568static int gfar_set_mac_address(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569{
Andy Fleming7f7f5312005-11-11 12:38:59 -06001570 gfar_set_mac_for_addr(dev, 0, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
1572 return 0;
1573}
1574
1575
Kumar Gala0bbaf062005-06-20 10:54:21 -05001576/* Enables and disables VLAN insertion/extraction */
1577static void gfar_vlan_rx_register(struct net_device *dev,
1578 struct vlan_group *grp)
1579{
1580 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001581 struct gfar_priv_rx_q *rx_queue = NULL;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001582 unsigned long flags;
1583 u32 tempval;
1584
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001585 rx_queue = priv->rx_queue;
1586 spin_lock_irqsave(&rx_queue->rxlock, flags);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001587
Anton Vorontsovcd1f55a2009-01-26 14:33:23 -08001588 priv->vlgrp = grp;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001589
1590 if (grp) {
1591 /* Enable VLAN tag insertion */
1592 tempval = gfar_read(&priv->regs->tctrl);
1593 tempval |= TCTRL_VLINS;
1594
1595 gfar_write(&priv->regs->tctrl, tempval);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001596
Kumar Gala0bbaf062005-06-20 10:54:21 -05001597 /* Enable VLAN tag extraction */
1598 tempval = gfar_read(&priv->regs->rctrl);
Dai Haruki77ecaf22008-12-16 15:30:48 -08001599 tempval |= (RCTRL_VLEX | RCTRL_PRSDEP_INIT);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001600 gfar_write(&priv->regs->rctrl, tempval);
1601 } else {
1602 /* Disable VLAN tag insertion */
1603 tempval = gfar_read(&priv->regs->tctrl);
1604 tempval &= ~TCTRL_VLINS;
1605 gfar_write(&priv->regs->tctrl, tempval);
1606
1607 /* Disable VLAN tag extraction */
1608 tempval = gfar_read(&priv->regs->rctrl);
1609 tempval &= ~RCTRL_VLEX;
Dai Haruki77ecaf22008-12-16 15:30:48 -08001610 /* If parse is no longer required, then disable parser */
1611 if (tempval & RCTRL_REQ_PARSER)
1612 tempval |= RCTRL_PRSDEP_INIT;
1613 else
1614 tempval &= ~RCTRL_PRSDEP_INIT;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001615 gfar_write(&priv->regs->rctrl, tempval);
1616 }
1617
Dai Haruki77ecaf22008-12-16 15:30:48 -08001618 gfar_change_mtu(dev, dev->mtu);
1619
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001620 spin_unlock_irqrestore(&rx_queue->rxlock, flags);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001621}
1622
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623static int gfar_change_mtu(struct net_device *dev, int new_mtu)
1624{
1625 int tempsize, tempval;
1626 struct gfar_private *priv = netdev_priv(dev);
1627 int oldsize = priv->rx_buffer_size;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001628 int frame_size = new_mtu + ETH_HLEN;
1629
Dai Haruki77ecaf22008-12-16 15:30:48 -08001630 if (priv->vlgrp)
Dai Harukifaa89572008-03-24 10:53:26 -05001631 frame_size += VLAN_HLEN;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001632
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 if ((frame_size < 64) || (frame_size > JUMBO_FRAME_SIZE)) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001634 if (netif_msg_drv(priv))
1635 printk(KERN_ERR "%s: Invalid MTU setting\n",
1636 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 return -EINVAL;
1638 }
1639
Dai Haruki77ecaf22008-12-16 15:30:48 -08001640 if (gfar_uses_fcb(priv))
1641 frame_size += GMAC_FCB_LEN;
1642
1643 frame_size += priv->padding;
1644
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 tempsize =
1646 (frame_size & ~(INCREMENTAL_BUFFER_SIZE - 1)) +
1647 INCREMENTAL_BUFFER_SIZE;
1648
1649 /* Only stop and start the controller if it isn't already
Andy Fleming7f7f5312005-11-11 12:38:59 -06001650 * stopped, and we changed something */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 if ((oldsize != tempsize) && (dev->flags & IFF_UP))
1652 stop_gfar(dev);
1653
1654 priv->rx_buffer_size = tempsize;
1655
1656 dev->mtu = new_mtu;
1657
1658 gfar_write(&priv->regs->mrblr, priv->rx_buffer_size);
1659 gfar_write(&priv->regs->maxfrm, priv->rx_buffer_size);
1660
1661 /* If the mtu is larger than the max size for standard
1662 * ethernet frames (ie, a jumbo frame), then set maccfg2
1663 * to allow huge frames, and to check the length */
1664 tempval = gfar_read(&priv->regs->maccfg2);
1665
1666 if (priv->rx_buffer_size > DEFAULT_RX_BUFFER_SIZE)
1667 tempval |= (MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK);
1668 else
1669 tempval &= ~(MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK);
1670
1671 gfar_write(&priv->regs->maccfg2, tempval);
1672
1673 if ((oldsize != tempsize) && (dev->flags & IFF_UP))
1674 startup_gfar(dev);
1675
1676 return 0;
1677}
1678
Sebastian Siewiorab939902008-08-19 21:12:45 +02001679/* gfar_reset_task gets scheduled when a packet has not been
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 * transmitted after a set amount of time.
1681 * For now, assume that clearing out all the structures, and
Sebastian Siewiorab939902008-08-19 21:12:45 +02001682 * starting over will fix the problem.
1683 */
1684static void gfar_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685{
Sebastian Siewiorab939902008-08-19 21:12:45 +02001686 struct gfar_private *priv = container_of(work, struct gfar_private,
1687 reset_task);
Kumar Gala48268572009-03-18 23:28:22 -07001688 struct net_device *dev = priv->ndev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689
1690 if (dev->flags & IFF_UP) {
Markus Brunnercbea27072009-04-15 02:35:40 -07001691 netif_stop_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 stop_gfar(dev);
1693 startup_gfar(dev);
Markus Brunnercbea27072009-04-15 02:35:40 -07001694 netif_start_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 }
1696
David S. Miller263ba322008-07-15 03:47:41 -07001697 netif_tx_schedule_all(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698}
1699
Sebastian Siewiorab939902008-08-19 21:12:45 +02001700static void gfar_timeout(struct net_device *dev)
1701{
1702 struct gfar_private *priv = netdev_priv(dev);
1703
1704 dev->stats.tx_errors++;
1705 schedule_work(&priv->reset_task);
1706}
1707
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708/* Interrupt Handler for Transmit complete */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001709static int gfar_clean_tx_ring(struct gfar_priv_tx_q *tx_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710{
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001711 struct net_device *dev = tx_queue->dev;
Dai Harukid080cd62008-04-09 19:37:51 -05001712 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001713 struct gfar_priv_rx_q *rx_queue = NULL;
Dai Haruki4669bc92008-12-17 16:51:04 -08001714 struct txbd8 *bdp;
1715 struct txbd8 *lbdp = NULL;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001716 struct txbd8 *base = tx_queue->tx_bd_base;
Dai Haruki4669bc92008-12-17 16:51:04 -08001717 struct sk_buff *skb;
1718 int skb_dirtytx;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001719 int tx_ring_size = tx_queue->tx_ring_size;
Dai Haruki4669bc92008-12-17 16:51:04 -08001720 int frags = 0;
1721 int i;
Dai Harukid080cd62008-04-09 19:37:51 -05001722 int howmany = 0;
Dai Haruki4669bc92008-12-17 16:51:04 -08001723 u32 lstatus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001725 rx_queue = priv->rx_queue;
1726 bdp = tx_queue->dirty_tx;
1727 skb_dirtytx = tx_queue->skb_dirtytx;
Dai Haruki4669bc92008-12-17 16:51:04 -08001728
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001729 while ((skb = tx_queue->tx_skbuff[skb_dirtytx])) {
Dai Haruki4669bc92008-12-17 16:51:04 -08001730 frags = skb_shinfo(skb)->nr_frags;
1731 lbdp = skip_txbd(bdp, frags, base, tx_ring_size);
1732
1733 lstatus = lbdp->lstatus;
1734
1735 /* Only clean completed frames */
1736 if ((lstatus & BD_LFLAG(TXBD_READY)) &&
1737 (lstatus & BD_LENGTH_MASK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 break;
1739
Kumar Gala48268572009-03-18 23:28:22 -07001740 dma_unmap_single(&priv->ofdev->dev,
Dai Haruki4669bc92008-12-17 16:51:04 -08001741 bdp->bufPtr,
1742 bdp->length,
1743 DMA_TO_DEVICE);
1744
1745 bdp->lstatus &= BD_LFLAG(TXBD_WRAP);
1746 bdp = next_txbd(bdp, base, tx_ring_size);
1747
1748 for (i = 0; i < frags; i++) {
Kumar Gala48268572009-03-18 23:28:22 -07001749 dma_unmap_page(&priv->ofdev->dev,
Dai Haruki4669bc92008-12-17 16:51:04 -08001750 bdp->bufPtr,
1751 bdp->length,
1752 DMA_TO_DEVICE);
1753 bdp->lstatus &= BD_LFLAG(TXBD_WRAP);
1754 bdp = next_txbd(bdp, base, tx_ring_size);
1755 }
1756
Andy Fleming0fd56bb2009-02-04 16:43:16 -08001757 /*
1758 * If there's room in the queue (limit it to rx_buffer_size)
1759 * we add this skb back into the pool, if it's the right size
1760 */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001761 if (skb_queue_len(&priv->rx_recycle) < rx_queue->rx_ring_size &&
Andy Fleming0fd56bb2009-02-04 16:43:16 -08001762 skb_recycle_check(skb, priv->rx_buffer_size +
1763 RXBUF_ALIGNMENT))
1764 __skb_queue_head(&priv->rx_recycle, skb);
1765 else
1766 dev_kfree_skb_any(skb);
1767
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001768 tx_queue->tx_skbuff[skb_dirtytx] = NULL;
Dai Haruki4669bc92008-12-17 16:51:04 -08001769
1770 skb_dirtytx = (skb_dirtytx + 1) &
1771 TX_RING_MOD_MASK(tx_ring_size);
1772
Dai Harukid080cd62008-04-09 19:37:51 -05001773 howmany++;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001774 tx_queue->num_txbdfree += frags + 1;
Dai Haruki4669bc92008-12-17 16:51:04 -08001775 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776
Dai Haruki4669bc92008-12-17 16:51:04 -08001777 /* If we freed a buffer, we can restart transmission, if necessary */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001778 if (netif_queue_stopped(dev) && tx_queue->num_txbdfree)
Dai Haruki4669bc92008-12-17 16:51:04 -08001779 netif_wake_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
Dai Haruki4669bc92008-12-17 16:51:04 -08001781 /* Update dirty indicators */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001782 tx_queue->skb_dirtytx = skb_dirtytx;
1783 tx_queue->dirty_tx = bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784
Dai Harukid080cd62008-04-09 19:37:51 -05001785 dev->stats.tx_packets += howmany;
1786
1787 return howmany;
1788}
1789
Dai Haruki8c7396a2008-12-17 16:52:00 -08001790static void gfar_schedule_cleanup(struct net_device *dev)
1791{
1792 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001793 struct gfar_priv_tx_q *tx_queue = NULL;
1794 struct gfar_priv_rx_q *rx_queue = NULL;
Anton Vorontsova6d0b912009-01-12 21:57:34 -08001795 unsigned long flags;
1796
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001797 rx_queue = priv->rx_queue;
1798 tx_queue = priv->tx_queue;
1799 spin_lock_irqsave(&tx_queue->txlock, flags);
1800 spin_lock(&rx_queue->rxlock);
Anton Vorontsova6d0b912009-01-12 21:57:34 -08001801
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001802 if (napi_schedule_prep(&rx_queue->napi)) {
Dai Haruki8c7396a2008-12-17 16:52:00 -08001803 gfar_write(&priv->regs->imask, IMASK_RTX_DISABLED);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001804 __napi_schedule(&rx_queue->napi);
Jarek Poplawski8707bdd2009-02-09 14:59:30 -08001805 } else {
1806 /*
1807 * Clear IEVENT, so interrupts aren't called again
1808 * because of the packets that have already arrived.
1809 */
1810 gfar_write(&priv->regs->ievent, IEVENT_RTX_MASK);
Dai Haruki8c7396a2008-12-17 16:52:00 -08001811 }
Anton Vorontsova6d0b912009-01-12 21:57:34 -08001812
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001813 spin_unlock(&rx_queue->rxlock);
1814 spin_unlock_irqrestore(&tx_queue->txlock, flags);
Dai Haruki8c7396a2008-12-17 16:52:00 -08001815}
1816
Dai Harukid080cd62008-04-09 19:37:51 -05001817/* Interrupt Handler for Transmit complete */
1818static irqreturn_t gfar_transmit(int irq, void *dev_id)
1819{
Dai Haruki8c7396a2008-12-17 16:52:00 -08001820 gfar_schedule_cleanup((struct net_device *)dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 return IRQ_HANDLED;
1822}
1823
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001824static void gfar_new_rxbdp(struct gfar_priv_rx_q *rx_queue, struct rxbd8 *bdp,
Andy Fleming815b97c2008-04-22 17:18:29 -05001825 struct sk_buff *skb)
1826{
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001827 struct net_device *dev = rx_queue->dev;
Andy Fleming815b97c2008-04-22 17:18:29 -05001828 struct gfar_private *priv = netdev_priv(dev);
Anton Vorontsov8a102fe2009-10-12 06:00:37 +00001829 dma_addr_t buf;
Andy Fleming815b97c2008-04-22 17:18:29 -05001830
Anton Vorontsov8a102fe2009-10-12 06:00:37 +00001831 buf = dma_map_single(&priv->ofdev->dev, skb->data,
1832 priv->rx_buffer_size, DMA_FROM_DEVICE);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001833 gfar_init_rxbdp(rx_queue, bdp, buf);
Andy Fleming815b97c2008-04-22 17:18:29 -05001834}
1835
1836
1837struct sk_buff * gfar_new_skb(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838{
Andy Fleming7f7f5312005-11-11 12:38:59 -06001839 unsigned int alignamount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 struct gfar_private *priv = netdev_priv(dev);
1841 struct sk_buff *skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842
Andy Fleming0fd56bb2009-02-04 16:43:16 -08001843 skb = __skb_dequeue(&priv->rx_recycle);
1844 if (!skb)
1845 skb = netdev_alloc_skb(dev,
1846 priv->rx_buffer_size + RXBUF_ALIGNMENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847
Andy Fleming815b97c2008-04-22 17:18:29 -05001848 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 return NULL;
1850
Andy Fleming7f7f5312005-11-11 12:38:59 -06001851 alignamount = RXBUF_ALIGNMENT -
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001852 (((unsigned long) skb->data) & (RXBUF_ALIGNMENT - 1));
Andy Fleming7f7f5312005-11-11 12:38:59 -06001853
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 /* We need the data buffer to be aligned properly. We will reserve
1855 * as many bytes as needed to align the data properly
1856 */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001857 skb_reserve(skb, alignamount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 return skb;
1860}
1861
Li Yang298e1a92007-10-16 14:18:13 +08001862static inline void count_errors(unsigned short status, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863{
Li Yang298e1a92007-10-16 14:18:13 +08001864 struct gfar_private *priv = netdev_priv(dev);
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001865 struct net_device_stats *stats = &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 struct gfar_extra_stats *estats = &priv->extra_stats;
1867
1868 /* If the packet was truncated, none of the other errors
1869 * matter */
1870 if (status & RXBD_TRUNCATED) {
1871 stats->rx_length_errors++;
1872
1873 estats->rx_trunc++;
1874
1875 return;
1876 }
1877 /* Count the errors, if there were any */
1878 if (status & (RXBD_LARGE | RXBD_SHORT)) {
1879 stats->rx_length_errors++;
1880
1881 if (status & RXBD_LARGE)
1882 estats->rx_large++;
1883 else
1884 estats->rx_short++;
1885 }
1886 if (status & RXBD_NONOCTET) {
1887 stats->rx_frame_errors++;
1888 estats->rx_nonoctet++;
1889 }
1890 if (status & RXBD_CRCERR) {
1891 estats->rx_crcerr++;
1892 stats->rx_crc_errors++;
1893 }
1894 if (status & RXBD_OVERRUN) {
1895 estats->rx_overrun++;
1896 stats->rx_crc_errors++;
1897 }
1898}
1899
David Howells7d12e782006-10-05 14:55:46 +01001900irqreturn_t gfar_receive(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901{
Dai Haruki8c7396a2008-12-17 16:52:00 -08001902 gfar_schedule_cleanup((struct net_device *)dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 return IRQ_HANDLED;
1904}
1905
Kumar Gala0bbaf062005-06-20 10:54:21 -05001906static inline void gfar_rx_checksum(struct sk_buff *skb, struct rxfcb *fcb)
1907{
1908 /* If valid headers were found, and valid sums
1909 * were verified, then we tell the kernel that no
1910 * checksumming is necessary. Otherwise, it is */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001911 if ((fcb->flags & RXFCB_CSUM_MASK) == (RXFCB_CIP | RXFCB_CTU))
Kumar Gala0bbaf062005-06-20 10:54:21 -05001912 skb->ip_summed = CHECKSUM_UNNECESSARY;
1913 else
1914 skb->ip_summed = CHECKSUM_NONE;
1915}
1916
1917
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918/* gfar_process_frame() -- handle one incoming packet if skb
1919 * isn't NULL. */
1920static int gfar_process_frame(struct net_device *dev, struct sk_buff *skb,
Dai Haruki2c2db482008-12-16 15:31:15 -08001921 int amount_pull)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922{
1923 struct gfar_private *priv = netdev_priv(dev);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001924 struct rxfcb *fcb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925
Dai Haruki2c2db482008-12-16 15:31:15 -08001926 int ret;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001927
Dai Haruki2c2db482008-12-16 15:31:15 -08001928 /* fcb is at the beginning if exists */
1929 fcb = (struct rxfcb *)skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930
Dai Haruki2c2db482008-12-16 15:31:15 -08001931 /* Remove the FCB from the skb */
1932 /* Remove the padded bytes, if there are any */
1933 if (amount_pull)
1934 skb_pull(skb, amount_pull);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001935
Dai Haruki2c2db482008-12-16 15:31:15 -08001936 if (priv->rx_csum_enable)
1937 gfar_rx_checksum(skb, fcb);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001938
Dai Haruki2c2db482008-12-16 15:31:15 -08001939 /* Tell the skb what kind of packet this is */
1940 skb->protocol = eth_type_trans(skb, dev);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001941
Dai Haruki2c2db482008-12-16 15:31:15 -08001942 /* Send the packet up the stack */
1943 if (unlikely(priv->vlgrp && (fcb->flags & RXFCB_VLN)))
1944 ret = vlan_hwaccel_receive_skb(skb, priv->vlgrp, fcb->vlctl);
1945 else
1946 ret = netif_receive_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947
Dai Haruki2c2db482008-12-16 15:31:15 -08001948 if (NET_RX_DROP == ret)
1949 priv->extra_stats.kernel_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950
1951 return 0;
1952}
1953
1954/* gfar_clean_rx_ring() -- Processes each frame in the rx ring
Kumar Gala0bbaf062005-06-20 10:54:21 -05001955 * until the budget/quota has been reached. Returns the number
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 * of frames handled
1957 */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001958int gfar_clean_rx_ring(struct gfar_priv_rx_q *rx_queue, int rx_work_limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959{
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001960 struct net_device *dev = rx_queue->dev;
Andy Fleming31de1982008-12-16 15:33:40 -08001961 struct rxbd8 *bdp, *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 struct sk_buff *skb;
Dai Haruki2c2db482008-12-16 15:31:15 -08001963 int pkt_len;
1964 int amount_pull;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 int howmany = 0;
1966 struct gfar_private *priv = netdev_priv(dev);
1967
1968 /* Get the first full descriptor */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001969 bdp = rx_queue->cur_rx;
1970 base = rx_queue->rx_bd_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971
Dai Haruki2c2db482008-12-16 15:31:15 -08001972 amount_pull = (gfar_uses_fcb(priv) ? GMAC_FCB_LEN : 0) +
1973 priv->padding;
1974
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 while (!((bdp->status & RXBD_EMPTY) || (--rx_work_limit < 0))) {
Andy Fleming815b97c2008-04-22 17:18:29 -05001976 struct sk_buff *newskb;
Scott Wood3b6330c2007-05-16 15:06:59 -05001977 rmb();
Andy Fleming815b97c2008-04-22 17:18:29 -05001978
1979 /* Add another skb for the future */
1980 newskb = gfar_new_skb(dev);
1981
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001982 skb = rx_queue->rx_skbuff[rx_queue->skb_currx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983
Kumar Gala48268572009-03-18 23:28:22 -07001984 dma_unmap_single(&priv->ofdev->dev, bdp->bufPtr,
Andy Fleming81183052008-11-12 10:07:11 -06001985 priv->rx_buffer_size, DMA_FROM_DEVICE);
1986
Andy Fleming815b97c2008-04-22 17:18:29 -05001987 /* We drop the frame if we failed to allocate a new buffer */
1988 if (unlikely(!newskb || !(bdp->status & RXBD_LAST) ||
1989 bdp->status & RXBD_ERR)) {
1990 count_errors(bdp->status, dev);
1991
1992 if (unlikely(!newskb))
1993 newskb = skb;
Lennert Buytenhek4e2fd552009-05-25 00:42:34 -07001994 else if (skb) {
1995 /*
1996 * We need to reset ->data to what it
1997 * was before gfar_new_skb() re-aligned
1998 * it to an RXBUF_ALIGNMENT boundary
1999 * before we put the skb back on the
2000 * recycle list.
2001 */
2002 skb->data = skb->head + NET_SKB_PAD;
Andy Fleming0fd56bb2009-02-04 16:43:16 -08002003 __skb_queue_head(&priv->rx_recycle, skb);
Lennert Buytenhek4e2fd552009-05-25 00:42:34 -07002004 }
Andy Fleming815b97c2008-04-22 17:18:29 -05002005 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 /* Increment the number of packets */
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002007 dev->stats.rx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 howmany++;
2009
Dai Haruki2c2db482008-12-16 15:31:15 -08002010 if (likely(skb)) {
2011 pkt_len = bdp->length - ETH_FCS_LEN;
2012 /* Remove the FCS from the packet length */
2013 skb_put(skb, pkt_len);
2014 dev->stats.rx_bytes += pkt_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
Andy Fleming1577ece2009-02-04 16:42:12 -08002016 if (in_irq() || irqs_disabled())
2017 printk("Interrupt problem!\n");
Dai Haruki2c2db482008-12-16 15:31:15 -08002018 gfar_process_frame(dev, skb, amount_pull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019
Dai Haruki2c2db482008-12-16 15:31:15 -08002020 } else {
2021 if (netif_msg_rx_err(priv))
2022 printk(KERN_WARNING
2023 "%s: Missing skb!\n", dev->name);
2024 dev->stats.rx_dropped++;
2025 priv->extra_stats.rx_skbmissing++;
2026 }
2027
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 }
2029
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002030 rx_queue->rx_skbuff[rx_queue->skb_currx] = newskb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031
Andy Fleming815b97c2008-04-22 17:18:29 -05002032 /* Setup the new bdp */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002033 gfar_new_rxbdp(rx_queue, bdp, newskb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034
2035 /* Update to the next pointer */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002036 bdp = next_bd(bdp, base, rx_queue->rx_ring_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037
2038 /* update to point at the next skb */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002039 rx_queue->skb_currx =
2040 (rx_queue->skb_currx + 1) &
2041 RX_RING_MOD_MASK(rx_queue->rx_ring_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 }
2043
2044 /* Update the current rxbd pointer to be the next one */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002045 rx_queue->cur_rx = bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 return howmany;
2048}
2049
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002050static int gfar_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051{
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002052 struct gfar_priv_rx_q *rx_queue = container_of(napi,
2053 struct gfar_priv_rx_q, napi);
2054 struct net_device *dev = rx_queue->dev;
2055 struct gfar_private *priv = netdev_priv(dev);
2056 struct gfar_priv_tx_q *tx_queue = NULL;
Andy Fleming42199882008-12-17 16:52:30 -08002057 int tx_cleaned = 0;
2058 int rx_cleaned = 0;
Dai Harukid080cd62008-04-09 19:37:51 -05002059 unsigned long flags;
2060
Dai Haruki8c7396a2008-12-17 16:52:00 -08002061 /* Clear IEVENT, so interrupts aren't called again
2062 * because of the packets that have already arrived */
2063 gfar_write(&priv->regs->ievent, IEVENT_RTX_MASK);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002064 tx_queue = priv->tx_queue;
Dai Haruki8c7396a2008-12-17 16:52:00 -08002065
Dai Harukid080cd62008-04-09 19:37:51 -05002066 /* If we fail to get the lock, don't bother with the TX BDs */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002067 if (spin_trylock_irqsave(&tx_queue->txlock, flags)) {
2068 tx_cleaned = gfar_clean_tx_ring(tx_queue);
2069 spin_unlock_irqrestore(&tx_queue->txlock, flags);
Dai Harukid080cd62008-04-09 19:37:51 -05002070 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002072 rx_cleaned = gfar_clean_rx_ring(rx_queue, budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073
Andy Fleming42199882008-12-17 16:52:30 -08002074 if (tx_cleaned)
2075 return budget;
2076
2077 if (rx_cleaned < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -08002078 napi_complete(napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079
2080 /* Clear the halt bit in RSTAT */
2081 gfar_write(&priv->regs->rstat, RSTAT_CLEAR_RHALT);
2082
2083 gfar_write(&priv->regs->imask, IMASK_DEFAULT);
2084
2085 /* If we are coalescing interrupts, update the timer */
2086 /* Otherwise, clear it */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002087 if (likely(rx_queue->rxcoalescing)) {
Andy Fleming2f448912008-03-24 10:53:28 -05002088 gfar_write(&priv->regs->rxic, 0);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002089 gfar_write(&priv->regs->rxic, rx_queue->rxic);
Andy Fleming2f448912008-03-24 10:53:28 -05002090 }
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002091 if (likely(tx_queue->txcoalescing)) {
Dai Haruki8c7396a2008-12-17 16:52:00 -08002092 gfar_write(&priv->regs->txic, 0);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002093 gfar_write(&priv->regs->txic, tx_queue->txic);
Dai Haruki8c7396a2008-12-17 16:52:00 -08002094 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 }
2096
Andy Fleming42199882008-12-17 16:52:30 -08002097 return rx_cleaned;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099
Vitaly Woolf2d71c22006-11-07 13:27:02 +03002100#ifdef CONFIG_NET_POLL_CONTROLLER
2101/*
2102 * Polling 'interrupt' - used by things like netconsole to send skbs
2103 * without having to re-enable interrupts. It's not called while
2104 * the interrupt routine is executing.
2105 */
2106static void gfar_netpoll(struct net_device *dev)
2107{
2108 struct gfar_private *priv = netdev_priv(dev);
2109
2110 /* If the device has multiple interrupts, run tx/rx */
Andy Flemingb31a1d82008-12-16 15:29:15 -08002111 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
Vitaly Woolf2d71c22006-11-07 13:27:02 +03002112 disable_irq(priv->interruptTransmit);
2113 disable_irq(priv->interruptReceive);
2114 disable_irq(priv->interruptError);
2115 gfar_interrupt(priv->interruptTransmit, dev);
2116 enable_irq(priv->interruptError);
2117 enable_irq(priv->interruptReceive);
2118 enable_irq(priv->interruptTransmit);
2119 } else {
2120 disable_irq(priv->interruptTransmit);
2121 gfar_interrupt(priv->interruptTransmit, dev);
2122 enable_irq(priv->interruptTransmit);
2123 }
2124}
2125#endif
2126
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127/* The interrupt handler for devices with one interrupt */
David Howells7d12e782006-10-05 14:55:46 +01002128static irqreturn_t gfar_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129{
2130 struct net_device *dev = dev_id;
2131 struct gfar_private *priv = netdev_priv(dev);
2132
2133 /* Save ievent for future reference */
2134 u32 events = gfar_read(&priv->regs->ievent);
2135
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 /* Check for reception */
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002137 if (events & IEVENT_RX_MASK)
David Howells7d12e782006-10-05 14:55:46 +01002138 gfar_receive(irq, dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139
2140 /* Check for transmit completion */
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002141 if (events & IEVENT_TX_MASK)
David Howells7d12e782006-10-05 14:55:46 +01002142 gfar_transmit(irq, dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002144 /* Check for errors */
2145 if (events & IEVENT_ERR_MASK)
2146 gfar_error(irq, dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147
2148 return IRQ_HANDLED;
2149}
2150
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151/* Called every time the controller might need to be made
2152 * aware of new link state. The PHY code conveys this
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002153 * information through variables in the phydev structure, and this
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 * function converts those variables into the appropriate
2155 * register values, and can bring down the device if needed.
2156 */
2157static void adjust_link(struct net_device *dev)
2158{
2159 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002160 struct gfar_priv_tx_q *tx_queue = NULL;
Kumar Galacc8c6e32006-02-01 15:18:03 -06002161 struct gfar __iomem *regs = priv->regs;
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002162 unsigned long flags;
2163 struct phy_device *phydev = priv->phydev;
2164 int new_state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002166 tx_queue = priv->tx_queue;
2167 spin_lock_irqsave(&tx_queue->txlock, flags);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002168 if (phydev->link) {
2169 u32 tempval = gfar_read(&regs->maccfg2);
Andy Fleming7f7f5312005-11-11 12:38:59 -06002170 u32 ecntrl = gfar_read(&regs->ecntrl);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002171
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 /* Now we make sure that we can be in full duplex mode.
2173 * If not, we operate in half-duplex mode. */
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002174 if (phydev->duplex != priv->oldduplex) {
2175 new_state = 1;
2176 if (!(phydev->duplex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 tempval &= ~(MACCFG2_FULL_DUPLEX);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002178 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 tempval |= MACCFG2_FULL_DUPLEX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002181 priv->oldduplex = phydev->duplex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 }
2183
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002184 if (phydev->speed != priv->oldspeed) {
2185 new_state = 1;
2186 switch (phydev->speed) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 case 1000:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 tempval =
2189 ((tempval & ~(MACCFG2_IF)) | MACCFG2_GMII);
Li Yangf430e492009-01-06 14:08:10 -08002190
2191 ecntrl &= ~(ECNTRL_R100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 break;
2193 case 100:
2194 case 10:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 tempval =
2196 ((tempval & ~(MACCFG2_IF)) | MACCFG2_MII);
Andy Fleming7f7f5312005-11-11 12:38:59 -06002197
2198 /* Reduced mode distinguishes
2199 * between 10 and 100 */
2200 if (phydev->speed == SPEED_100)
2201 ecntrl |= ECNTRL_R100;
2202 else
2203 ecntrl &= ~(ECNTRL_R100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 break;
2205 default:
Kumar Gala0bbaf062005-06-20 10:54:21 -05002206 if (netif_msg_link(priv))
2207 printk(KERN_WARNING
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002208 "%s: Ack! Speed (%d) is not 10/100/1000!\n",
2209 dev->name, phydev->speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 break;
2211 }
2212
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002213 priv->oldspeed = phydev->speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 }
2215
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002216 gfar_write(&regs->maccfg2, tempval);
Andy Fleming7f7f5312005-11-11 12:38:59 -06002217 gfar_write(&regs->ecntrl, ecntrl);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002218
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 if (!priv->oldlink) {
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002220 new_state = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 priv->oldlink = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 }
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002223 } else if (priv->oldlink) {
2224 new_state = 1;
2225 priv->oldlink = 0;
2226 priv->oldspeed = 0;
2227 priv->oldduplex = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002230 if (new_state && netif_msg_link(priv))
2231 phy_print_status(phydev);
2232
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002233 spin_unlock_irqrestore(&tx_queue->txlock, flags);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002234}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235
2236/* Update the hash table based on the current list of multicast
2237 * addresses we subscribe to. Also, change the promiscuity of
2238 * the device based on the flags (this function is called
2239 * whenever dev->flags is changed */
2240static void gfar_set_multi(struct net_device *dev)
2241{
2242 struct dev_mc_list *mc_ptr;
2243 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -06002244 struct gfar __iomem *regs = priv->regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 u32 tempval;
2246
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002247 if (dev->flags & IFF_PROMISC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 /* Set RCTRL to PROM */
2249 tempval = gfar_read(&regs->rctrl);
2250 tempval |= RCTRL_PROM;
2251 gfar_write(&regs->rctrl, tempval);
2252 } else {
2253 /* Set RCTRL to not PROM */
2254 tempval = gfar_read(&regs->rctrl);
2255 tempval &= ~(RCTRL_PROM);
2256 gfar_write(&regs->rctrl, tempval);
2257 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002258
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002259 if (dev->flags & IFF_ALLMULTI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 /* Set the hash to rx all multicast frames */
Kumar Gala0bbaf062005-06-20 10:54:21 -05002261 gfar_write(&regs->igaddr0, 0xffffffff);
2262 gfar_write(&regs->igaddr1, 0xffffffff);
2263 gfar_write(&regs->igaddr2, 0xffffffff);
2264 gfar_write(&regs->igaddr3, 0xffffffff);
2265 gfar_write(&regs->igaddr4, 0xffffffff);
2266 gfar_write(&regs->igaddr5, 0xffffffff);
2267 gfar_write(&regs->igaddr6, 0xffffffff);
2268 gfar_write(&regs->igaddr7, 0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 gfar_write(&regs->gaddr0, 0xffffffff);
2270 gfar_write(&regs->gaddr1, 0xffffffff);
2271 gfar_write(&regs->gaddr2, 0xffffffff);
2272 gfar_write(&regs->gaddr3, 0xffffffff);
2273 gfar_write(&regs->gaddr4, 0xffffffff);
2274 gfar_write(&regs->gaddr5, 0xffffffff);
2275 gfar_write(&regs->gaddr6, 0xffffffff);
2276 gfar_write(&regs->gaddr7, 0xffffffff);
2277 } else {
Andy Fleming7f7f5312005-11-11 12:38:59 -06002278 int em_num;
2279 int idx;
2280
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 /* zero out the hash */
Kumar Gala0bbaf062005-06-20 10:54:21 -05002282 gfar_write(&regs->igaddr0, 0x0);
2283 gfar_write(&regs->igaddr1, 0x0);
2284 gfar_write(&regs->igaddr2, 0x0);
2285 gfar_write(&regs->igaddr3, 0x0);
2286 gfar_write(&regs->igaddr4, 0x0);
2287 gfar_write(&regs->igaddr5, 0x0);
2288 gfar_write(&regs->igaddr6, 0x0);
2289 gfar_write(&regs->igaddr7, 0x0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 gfar_write(&regs->gaddr0, 0x0);
2291 gfar_write(&regs->gaddr1, 0x0);
2292 gfar_write(&regs->gaddr2, 0x0);
2293 gfar_write(&regs->gaddr3, 0x0);
2294 gfar_write(&regs->gaddr4, 0x0);
2295 gfar_write(&regs->gaddr5, 0x0);
2296 gfar_write(&regs->gaddr6, 0x0);
2297 gfar_write(&regs->gaddr7, 0x0);
2298
Andy Fleming7f7f5312005-11-11 12:38:59 -06002299 /* If we have extended hash tables, we need to
2300 * clear the exact match registers to prepare for
2301 * setting them */
2302 if (priv->extended_hash) {
2303 em_num = GFAR_EM_NUM + 1;
2304 gfar_clear_exact_match(dev);
2305 idx = 1;
2306 } else {
2307 idx = 0;
2308 em_num = 0;
2309 }
2310
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002311 if (dev->mc_count == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 return;
2313
2314 /* Parse the list, and set the appropriate bits */
2315 for(mc_ptr = dev->mc_list; mc_ptr; mc_ptr = mc_ptr->next) {
Andy Fleming7f7f5312005-11-11 12:38:59 -06002316 if (idx < em_num) {
2317 gfar_set_mac_for_addr(dev, idx,
2318 mc_ptr->dmi_addr);
2319 idx++;
2320 } else
2321 gfar_set_hash_for_addr(dev, mc_ptr->dmi_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 }
2323 }
2324
2325 return;
2326}
2327
Andy Fleming7f7f5312005-11-11 12:38:59 -06002328
2329/* Clears each of the exact match registers to zero, so they
2330 * don't interfere with normal reception */
2331static void gfar_clear_exact_match(struct net_device *dev)
2332{
2333 int idx;
2334 u8 zero_arr[MAC_ADDR_LEN] = {0,0,0,0,0,0};
2335
2336 for(idx = 1;idx < GFAR_EM_NUM + 1;idx++)
2337 gfar_set_mac_for_addr(dev, idx, (u8 *)zero_arr);
2338}
2339
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340/* Set the appropriate hash bit for the given addr */
2341/* The algorithm works like so:
2342 * 1) Take the Destination Address (ie the multicast address), and
2343 * do a CRC on it (little endian), and reverse the bits of the
2344 * result.
2345 * 2) Use the 8 most significant bits as a hash into a 256-entry
2346 * table. The table is controlled through 8 32-bit registers:
2347 * gaddr0-7. gaddr0's MSB is entry 0, and gaddr7's LSB is
2348 * gaddr7. This means that the 3 most significant bits in the
2349 * hash index which gaddr register to use, and the 5 other bits
2350 * indicate which bit (assuming an IBM numbering scheme, which
2351 * for PowerPC (tm) is usually the case) in the register holds
2352 * the entry. */
2353static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr)
2354{
2355 u32 tempval;
2356 struct gfar_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 u32 result = ether_crc(MAC_ADDR_LEN, addr);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002358 int width = priv->hash_width;
2359 u8 whichbit = (result >> (32 - width)) & 0x1f;
2360 u8 whichreg = result >> (32 - width + 5);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 u32 value = (1 << (31-whichbit));
2362
Kumar Gala0bbaf062005-06-20 10:54:21 -05002363 tempval = gfar_read(priv->hash_regs[whichreg]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 tempval |= value;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002365 gfar_write(priv->hash_regs[whichreg], tempval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366
2367 return;
2368}
2369
Andy Fleming7f7f5312005-11-11 12:38:59 -06002370
2371/* There are multiple MAC Address register pairs on some controllers
2372 * This function sets the numth pair to a given address
2373 */
2374static void gfar_set_mac_for_addr(struct net_device *dev, int num, u8 *addr)
2375{
2376 struct gfar_private *priv = netdev_priv(dev);
2377 int idx;
2378 char tmpbuf[MAC_ADDR_LEN];
2379 u32 tempval;
Kumar Galacc8c6e32006-02-01 15:18:03 -06002380 u32 __iomem *macptr = &priv->regs->macstnaddr1;
Andy Fleming7f7f5312005-11-11 12:38:59 -06002381
2382 macptr += num*2;
2383
2384 /* Now copy it into the mac registers backwards, cuz */
2385 /* little endian is silly */
2386 for (idx = 0; idx < MAC_ADDR_LEN; idx++)
2387 tmpbuf[MAC_ADDR_LEN - 1 - idx] = addr[idx];
2388
2389 gfar_write(macptr, *((u32 *) (tmpbuf)));
2390
2391 tempval = *((u32 *) (tmpbuf + 4));
2392
2393 gfar_write(macptr+1, tempval);
2394}
2395
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396/* GFAR error interrupt handler */
David Howells7d12e782006-10-05 14:55:46 +01002397static irqreturn_t gfar_error(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398{
2399 struct net_device *dev = dev_id;
2400 struct gfar_private *priv = netdev_priv(dev);
2401
2402 /* Save ievent for future reference */
2403 u32 events = gfar_read(&priv->regs->ievent);
2404
2405 /* Clear IEVENT */
Scott Woodd87eb122008-07-11 18:04:45 -05002406 gfar_write(&priv->regs->ievent, events & IEVENT_ERR_MASK);
2407
2408 /* Magic Packet is not an error. */
Andy Flemingb31a1d82008-12-16 15:29:15 -08002409 if ((priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET) &&
Scott Woodd87eb122008-07-11 18:04:45 -05002410 (events & IEVENT_MAG))
2411 events &= ~IEVENT_MAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412
2413 /* Hmm... */
Kumar Gala0bbaf062005-06-20 10:54:21 -05002414 if (netif_msg_rx_err(priv) || netif_msg_tx_err(priv))
2415 printk(KERN_DEBUG "%s: error interrupt (ievent=0x%08x imask=0x%08x)\n",
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002416 dev->name, events, gfar_read(&priv->regs->imask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417
2418 /* Update the error counters */
2419 if (events & IEVENT_TXE) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002420 dev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421
2422 if (events & IEVENT_LC)
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002423 dev->stats.tx_window_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 if (events & IEVENT_CRL)
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002425 dev->stats.tx_aborted_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 if (events & IEVENT_XFUN) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05002427 if (netif_msg_tx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002428 printk(KERN_DEBUG "%s: TX FIFO underrun, "
2429 "packet dropped.\n", dev->name);
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002430 dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 priv->extra_stats.tx_underrun++;
2432
2433 /* Reactivate the Tx Queues */
2434 gfar_write(&priv->regs->tstat, TSTAT_CLEAR_THALT);
2435 }
Kumar Gala0bbaf062005-06-20 10:54:21 -05002436 if (netif_msg_tx_err(priv))
2437 printk(KERN_DEBUG "%s: Transmit Error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 }
2439 if (events & IEVENT_BSY) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002440 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 priv->extra_stats.rx_bsy++;
2442
David Howells7d12e782006-10-05 14:55:46 +01002443 gfar_receive(irq, dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444
Kumar Gala0bbaf062005-06-20 10:54:21 -05002445 if (netif_msg_rx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002446 printk(KERN_DEBUG "%s: busy error (rstat: %x)\n",
2447 dev->name, gfar_read(&priv->regs->rstat));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 }
2449 if (events & IEVENT_BABR) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002450 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 priv->extra_stats.rx_babr++;
2452
Kumar Gala0bbaf062005-06-20 10:54:21 -05002453 if (netif_msg_rx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002454 printk(KERN_DEBUG "%s: babbling RX error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 }
2456 if (events & IEVENT_EBERR) {
2457 priv->extra_stats.eberr++;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002458 if (netif_msg_rx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002459 printk(KERN_DEBUG "%s: bus error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 }
Kumar Gala0bbaf062005-06-20 10:54:21 -05002461 if ((events & IEVENT_RXC) && netif_msg_rx_status(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002462 printk(KERN_DEBUG "%s: control frame\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463
2464 if (events & IEVENT_BABT) {
2465 priv->extra_stats.tx_babt++;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002466 if (netif_msg_tx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002467 printk(KERN_DEBUG "%s: babbling TX error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 }
2469 return IRQ_HANDLED;
2470}
2471
Andy Flemingb31a1d82008-12-16 15:29:15 -08002472static struct of_device_id gfar_match[] =
2473{
2474 {
2475 .type = "network",
2476 .compatible = "gianfar",
2477 },
2478 {},
2479};
Anton Vorontsove72701a2009-10-14 14:54:52 -07002480MODULE_DEVICE_TABLE(of, gfar_match);
Andy Flemingb31a1d82008-12-16 15:29:15 -08002481
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482/* Structure for a device driver */
Andy Flemingb31a1d82008-12-16 15:29:15 -08002483static struct of_platform_driver gfar_driver = {
2484 .name = "fsl-gianfar",
2485 .match_table = gfar_match,
2486
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 .probe = gfar_probe,
2488 .remove = gfar_remove,
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00002489 .suspend = gfar_legacy_suspend,
2490 .resume = gfar_legacy_resume,
2491 .driver.pm = GFAR_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492};
2493
2494static int __init gfar_init(void)
2495{
Andy Fleming1577ece2009-02-04 16:42:12 -08002496 return of_register_platform_driver(&gfar_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497}
2498
2499static void __exit gfar_exit(void)
2500{
Andy Flemingb31a1d82008-12-16 15:29:15 -08002501 of_unregister_platform_driver(&gfar_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502}
2503
2504module_init(gfar_init);
2505module_exit(gfar_exit);
2506