blob: aa258e899261de28d3aee6ef543ad2ec04e4ca7f [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);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000146u16 gfar_select_queue(struct net_device *dev, struct sk_buff *skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148MODULE_AUTHOR("Freescale Semiconductor, Inc");
149MODULE_DESCRIPTION("Gianfar Ethernet Driver");
150MODULE_LICENSE("GPL");
151
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000152static void gfar_init_rxbdp(struct gfar_priv_rx_q *rx_queue, struct rxbd8 *bdp,
Anton Vorontsov8a102fe2009-10-12 06:00:37 +0000153 dma_addr_t buf)
154{
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;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000175 int i, j;
Anton Vorontsov87283272009-10-12 06:00:39 +0000176
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000177 for (i = 0; i < priv->num_tx_queues; i++) {
178 tx_queue = priv->tx_queue[i];
179 /* Initialize some variables in our dev structure */
180 tx_queue->num_txbdfree = tx_queue->tx_ring_size;
181 tx_queue->dirty_tx = tx_queue->tx_bd_base;
182 tx_queue->cur_tx = tx_queue->tx_bd_base;
183 tx_queue->skb_curtx = 0;
184 tx_queue->skb_dirtytx = 0;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000185
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000186 /* Initialize Transmit Descriptor Ring */
187 txbdp = tx_queue->tx_bd_base;
188 for (j = 0; j < tx_queue->tx_ring_size; j++) {
189 txbdp->lstatus = 0;
190 txbdp->bufPtr = 0;
191 txbdp++;
Anton Vorontsov87283272009-10-12 06:00:39 +0000192 }
193
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000194 /* Set the last descriptor in the ring to indicate wrap */
195 txbdp--;
196 txbdp->status |= TXBD_WRAP;
197 }
198
199 for (i = 0; i < priv->num_rx_queues; i++) {
200 rx_queue = priv->rx_queue[i];
201 rx_queue->cur_rx = rx_queue->rx_bd_base;
202 rx_queue->skb_currx = 0;
203 rxbdp = rx_queue->rx_bd_base;
204
205 for (j = 0; j < rx_queue->rx_ring_size; j++) {
206 struct sk_buff *skb = rx_queue->rx_skbuff[j];
207
208 if (skb) {
209 gfar_init_rxbdp(rx_queue, rxbdp,
210 rxbdp->bufPtr);
211 } else {
212 skb = gfar_new_skb(ndev);
213 if (!skb) {
214 pr_err("%s: Can't allocate RX buffers\n",
215 ndev->name);
216 goto err_rxalloc_fail;
217 }
218 rx_queue->rx_skbuff[j] = skb;
219
220 gfar_new_rxbdp(rx_queue, rxbdp, skb);
221 }
222
223 rxbdp++;
224 }
225
Anton Vorontsov87283272009-10-12 06:00:39 +0000226 }
227
228 return 0;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000229
230err_rxalloc_fail:
231 free_skb_resources(priv);
232 return -ENOMEM;
Anton Vorontsov87283272009-10-12 06:00:39 +0000233}
234
235static int gfar_alloc_skb_resources(struct net_device *ndev)
236{
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000237 void *vaddr;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000238 dma_addr_t addr;
239 int i, j, k;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000240 struct gfar_private *priv = netdev_priv(ndev);
241 struct device *dev = &priv->ofdev->dev;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000242 struct gfar_priv_tx_q *tx_queue = NULL;
243 struct gfar_priv_rx_q *rx_queue = NULL;
244
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000245 priv->total_tx_ring_size = 0;
246 for (i = 0; i < priv->num_tx_queues; i++)
247 priv->total_tx_ring_size += priv->tx_queue[i]->tx_ring_size;
248
249 priv->total_rx_ring_size = 0;
250 for (i = 0; i < priv->num_rx_queues; i++)
251 priv->total_rx_ring_size += priv->rx_queue[i]->rx_ring_size;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000252
253 /* Allocate memory for the buffer descriptors */
Anton Vorontsov87283272009-10-12 06:00:39 +0000254 vaddr = dma_alloc_coherent(dev,
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000255 sizeof(struct txbd8) * priv->total_tx_ring_size +
256 sizeof(struct rxbd8) * priv->total_rx_ring_size,
257 &addr, GFP_KERNEL);
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000258 if (!vaddr) {
259 if (netif_msg_ifup(priv))
260 pr_err("%s: Could not allocate buffer descriptors!\n",
261 ndev->name);
262 return -ENOMEM;
263 }
264
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000265 for (i = 0; i < priv->num_tx_queues; i++) {
266 tx_queue = priv->tx_queue[i];
267 tx_queue->tx_bd_base = (struct txbd8 *) vaddr;
268 tx_queue->tx_bd_dma_base = addr;
269 tx_queue->dev = ndev;
270 /* enet DMA only understands physical addresses */
271 addr += sizeof(struct txbd8) *tx_queue->tx_ring_size;
272 vaddr += sizeof(struct txbd8) *tx_queue->tx_ring_size;
273 }
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000274
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000275 /* Start the rx descriptor ring where the tx ring leaves off */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000276 for (i = 0; i < priv->num_rx_queues; i++) {
277 rx_queue = priv->rx_queue[i];
278 rx_queue->rx_bd_base = (struct rxbd8 *) vaddr;
279 rx_queue->rx_bd_dma_base = addr;
280 rx_queue->dev = ndev;
281 addr += sizeof (struct rxbd8) * rx_queue->rx_ring_size;
282 vaddr += sizeof (struct rxbd8) * rx_queue->rx_ring_size;
283 }
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000284
285 /* Setup the skbuff rings */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000286 for (i = 0; i < priv->num_tx_queues; i++) {
287 tx_queue = priv->tx_queue[i];
288 tx_queue->tx_skbuff = kmalloc(sizeof(*tx_queue->tx_skbuff) *
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000289 tx_queue->tx_ring_size, GFP_KERNEL);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000290 if (!tx_queue->tx_skbuff) {
291 if (netif_msg_ifup(priv))
292 pr_err("%s: Could not allocate tx_skbuff\n",
293 ndev->name);
294 goto cleanup;
295 }
296
297 for (k = 0; k < tx_queue->tx_ring_size; k++)
298 tx_queue->tx_skbuff[k] = NULL;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000299 }
300
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000301 for (i = 0; i < priv->num_rx_queues; i++) {
302 rx_queue = priv->rx_queue[i];
303 rx_queue->rx_skbuff = kmalloc(sizeof(*rx_queue->rx_skbuff) *
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000304 rx_queue->rx_ring_size, GFP_KERNEL);
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000305
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000306 if (!rx_queue->rx_skbuff) {
307 if (netif_msg_ifup(priv))
308 pr_err("%s: Could not allocate rx_skbuff\n",
309 ndev->name);
310 goto cleanup;
311 }
312
313 for (j = 0; j < rx_queue->rx_ring_size; j++)
314 rx_queue->rx_skbuff[j] = NULL;
315 }
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000316
Anton Vorontsov87283272009-10-12 06:00:39 +0000317 if (gfar_init_bds(ndev))
318 goto cleanup;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000319
320 return 0;
321
322cleanup:
323 free_skb_resources(priv);
324 return -ENOMEM;
325}
326
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000327static void gfar_init_tx_rx_base(struct gfar_private *priv)
328{
329 struct gfar __iomem *regs = priv->gfargrp.regs;
330 u32 *baddr;
331 int i;
332
333 baddr = &regs->tbase0;
334 for(i = 0; i < priv->num_tx_queues; i++) {
335 gfar_write(baddr, priv->tx_queue[i]->tx_bd_dma_base);
336 baddr += 2;
337 }
338
339 baddr = &regs->rbase0;
340 for(i = 0; i < priv->num_rx_queues; i++) {
341 gfar_write(baddr, priv->rx_queue[i]->rx_bd_dma_base);
342 baddr += 2;
343 }
344}
345
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000346static void gfar_init_mac(struct net_device *ndev)
347{
348 struct gfar_private *priv = netdev_priv(ndev);
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000349 struct gfar __iomem *regs = priv->gfargrp.regs;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000350 u32 rctrl = 0;
351 u32 tctrl = 0;
352 u32 attrs = 0;
353
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000354 /* write the tx/rx base registers */
355 gfar_init_tx_rx_base(priv);
Anton Vorontsov32c513b2009-10-12 06:00:36 +0000356
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000357 /* Configure the coalescing support */
358 gfar_write(&regs->txic, 0);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000359 if (priv->tx_queue[0]->txcoalescing)
360 gfar_write(&regs->txic, priv->tx_queue[0]->txic);
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000361
362 gfar_write(&regs->rxic, 0);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000363 if (priv->rx_queue[0]->rxcoalescing)
364 gfar_write(&regs->rxic, priv->rx_queue[0]->rxic);
365
366 if (priv->rx_filer_enable)
367 rctrl |= RCTRL_FILREN;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000368
369 if (priv->rx_csum_enable)
370 rctrl |= RCTRL_CHECKSUMMING;
371
372 if (priv->extended_hash) {
373 rctrl |= RCTRL_EXTHASH;
374
375 gfar_clear_exact_match(ndev);
376 rctrl |= RCTRL_EMEN;
377 }
378
379 if (priv->padding) {
380 rctrl &= ~RCTRL_PAL_MASK;
381 rctrl |= RCTRL_PADDING(priv->padding);
382 }
383
384 /* keep vlan related bits if it's enabled */
385 if (priv->vlgrp) {
386 rctrl |= RCTRL_VLEX | RCTRL_PRSDEP_INIT;
387 tctrl |= TCTRL_VLINS;
388 }
389
390 /* Init rctrl based on our settings */
391 gfar_write(&regs->rctrl, rctrl);
392
393 if (ndev->features & NETIF_F_IP_CSUM)
394 tctrl |= TCTRL_INIT_CSUM;
395
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000396 tctrl |= TCTRL_TXSCHED_PRIO;
397
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000398 gfar_write(&regs->tctrl, tctrl);
399
400 /* Set the extraction length and index */
401 attrs = ATTRELI_EL(priv->rx_stash_size) |
402 ATTRELI_EI(priv->rx_stash_index);
403
404 gfar_write(&regs->attreli, attrs);
405
406 /* Start with defaults, and add stashing or locking
407 * depending on the approprate variables */
408 attrs = ATTR_INIT_SETTINGS;
409
410 if (priv->bd_stash_en)
411 attrs |= ATTR_BDSTASH;
412
413 if (priv->rx_stash_size != 0)
414 attrs |= ATTR_BUFSTASH;
415
416 gfar_write(&regs->attr, attrs);
417
418 gfar_write(&regs->fifo_tx_thr, priv->fifo_threshold);
419 gfar_write(&regs->fifo_tx_starve, priv->fifo_starve);
420 gfar_write(&regs->fifo_tx_starve_shutoff, priv->fifo_starve_off);
421}
422
Andy Fleming26ccfc32009-03-10 12:58:28 +0000423static const struct net_device_ops gfar_netdev_ops = {
424 .ndo_open = gfar_enet_open,
425 .ndo_start_xmit = gfar_start_xmit,
426 .ndo_stop = gfar_close,
427 .ndo_change_mtu = gfar_change_mtu,
428 .ndo_set_multicast_list = gfar_set_multi,
429 .ndo_tx_timeout = gfar_timeout,
430 .ndo_do_ioctl = gfar_ioctl,
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000431 .ndo_select_queue = gfar_select_queue,
Andy Fleming26ccfc32009-03-10 12:58:28 +0000432 .ndo_vlan_rx_register = gfar_vlan_rx_register,
Ben Hutchings240c1022009-07-09 17:54:35 +0000433 .ndo_set_mac_address = eth_mac_addr,
434 .ndo_validate_addr = eth_validate_addr,
Andy Fleming26ccfc32009-03-10 12:58:28 +0000435#ifdef CONFIG_NET_POLL_CONTROLLER
436 .ndo_poll_controller = gfar_netpoll,
437#endif
438};
439
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000440void lock_rx_qs(struct gfar_private *priv)
441{
442 int i = 0x0;
443
444 for (i = 0; i < priv->num_rx_queues; i++)
445 spin_lock(&priv->rx_queue[i]->rxlock);
446}
447
448void lock_tx_qs(struct gfar_private *priv)
449{
450 int i = 0x0;
451
452 for (i = 0; i < priv->num_tx_queues; i++)
453 spin_lock(&priv->tx_queue[i]->txlock);
454}
455
456void unlock_rx_qs(struct gfar_private *priv)
457{
458 int i = 0x0;
459
460 for (i = 0; i < priv->num_rx_queues; i++)
461 spin_unlock(&priv->rx_queue[i]->rxlock);
462}
463
464void unlock_tx_qs(struct gfar_private *priv)
465{
466 int i = 0x0;
467
468 for (i = 0; i < priv->num_tx_queues; i++)
469 spin_unlock(&priv->tx_queue[i]->txlock);
470}
471
Andy Fleming7f7f5312005-11-11 12:38:59 -0600472/* Returns 1 if incoming frames use an FCB */
473static inline int gfar_uses_fcb(struct gfar_private *priv)
Kumar Gala0bbaf062005-06-20 10:54:21 -0500474{
Dai Haruki77ecaf22008-12-16 15:30:48 -0800475 return priv->vlgrp || priv->rx_csum_enable;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500476}
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400477
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000478u16 gfar_select_queue(struct net_device *dev, struct sk_buff *skb)
479{
480 return skb_get_queue_mapping(skb);
481}
482static void free_tx_pointers(struct gfar_private *priv)
483{
484 int i = 0;
485
486 for (i = 0; i < priv->num_tx_queues; i++)
487 kfree(priv->tx_queue[i]);
488}
489
490static void free_rx_pointers(struct gfar_private *priv)
491{
492 int i = 0;
493
494 for (i = 0; i < priv->num_rx_queues; i++)
495 kfree(priv->rx_queue[i]);
496}
497
498static int gfar_of_init(struct of_device *ofdev, struct net_device **pdev)
Andy Flemingb31a1d82008-12-16 15:29:15 -0800499{
Andy Flemingb31a1d82008-12-16 15:29:15 -0800500 const char *model;
501 const char *ctype;
502 const void *mac_addr;
Andy Flemingb31a1d82008-12-16 15:29:15 -0800503 u64 addr, size;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000504 int err = 0, i;
505 struct net_device *dev = NULL;
506 struct gfar_private *priv = NULL;
507 struct device_node *np = ofdev->node;
Andy Fleming4d7902f2009-02-04 16:43:44 -0800508 const u32 *stash;
509 const u32 *stash_len;
510 const u32 *stash_idx;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000511 unsigned int num_tx_qs, num_rx_qs;
512 u32 *tx_queues, *rx_queues;
Andy Flemingb31a1d82008-12-16 15:29:15 -0800513
514 if (!np || !of_device_is_available(np))
515 return -ENODEV;
516
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000517 /* parse the num of tx and rx queues */
518 tx_queues = (u32 *)of_get_property(np, "fsl,num_tx_queues", NULL);
519 num_tx_qs = tx_queues ? *tx_queues : 1;
520
521 if (num_tx_qs > MAX_TX_QS) {
522 printk(KERN_ERR "num_tx_qs(=%d) greater than MAX_TX_QS(=%d)\n",
523 num_tx_qs, MAX_TX_QS);
524 printk(KERN_ERR "Cannot do alloc_etherdev, aborting\n");
525 return -EINVAL;
526 }
527
528 rx_queues = (u32 *)of_get_property(np, "fsl,num_rx_queues", NULL);
529 num_rx_qs = rx_queues ? *rx_queues : 1;
530
531 if (num_rx_qs > MAX_RX_QS) {
532 printk(KERN_ERR "num_rx_qs(=%d) greater than MAX_RX_QS(=%d)\n",
533 num_tx_qs, MAX_TX_QS);
534 printk(KERN_ERR "Cannot do alloc_etherdev, aborting\n");
535 return -EINVAL;
536 }
537
538 *pdev = alloc_etherdev_mq(sizeof(*priv), num_tx_qs);
539 dev = *pdev;
540 if (NULL == dev)
541 return -ENOMEM;
542
543 priv = netdev_priv(dev);
544 priv->node = ofdev->node;
545 priv->ndev = dev;
546
547 dev->num_tx_queues = num_tx_qs;
548 dev->real_num_tx_queues = num_tx_qs;
549 priv->num_tx_queues = num_tx_qs;
550 priv->num_rx_queues = num_rx_qs;
551
Andy Flemingb31a1d82008-12-16 15:29:15 -0800552 /* get a pointer to the register memory */
553 addr = of_translate_address(np, of_get_address(np, 0, &size, NULL));
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000554 priv->gfargrp.regs = ioremap(addr, size);
Andy Flemingb31a1d82008-12-16 15:29:15 -0800555
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000556 if (priv->gfargrp.regs == NULL) {
557 err = -ENOMEM;
558 goto err_out;
559 }
Andy Flemingb31a1d82008-12-16 15:29:15 -0800560
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000561 priv->gfargrp.priv = priv; /* back pointer from group to priv */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000562 priv->gfargrp.rx_bit_map = DEFAULT_MAPPING;
563 priv->gfargrp.tx_bit_map = DEFAULT_MAPPING;
564
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000565 priv->gfargrp.interruptTransmit = irq_of_parse_and_map(np, 0);
Andy Flemingb31a1d82008-12-16 15:29:15 -0800566
567 model = of_get_property(np, "model", NULL);
568
569 /* If we aren't the FEC we have multiple interrupts */
570 if (model && strcasecmp(model, "FEC")) {
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000571 priv->gfargrp.interruptReceive = irq_of_parse_and_map(np, 1);
Andy Flemingb31a1d82008-12-16 15:29:15 -0800572
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000573 priv->gfargrp.interruptError = irq_of_parse_and_map(np, 2);
Andy Flemingb31a1d82008-12-16 15:29:15 -0800574
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000575 if (priv->gfargrp.interruptTransmit < 0 ||
576 priv->gfargrp.interruptReceive < 0 ||
577 priv->gfargrp.interruptError < 0) {
Andy Flemingb31a1d82008-12-16 15:29:15 -0800578 err = -EINVAL;
579 goto err_out;
580 }
581 }
582
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000583 for (i = 0; i < priv->num_tx_queues; i++)
584 priv->tx_queue[i] = NULL;
585 for (i = 0; i < priv->num_rx_queues; i++)
586 priv->rx_queue[i] = NULL;
587
588 for (i = 0; i < priv->num_tx_queues; i++) {
589 priv->tx_queue[i] = (struct gfar_priv_tx_q *)kmalloc(
590 sizeof (struct gfar_priv_tx_q), GFP_KERNEL);
591 if (!priv->tx_queue[i]) {
592 err = -ENOMEM;
593 goto tx_alloc_failed;
594 }
595 priv->tx_queue[i]->tx_skbuff = NULL;
596 priv->tx_queue[i]->qindex = i;
597 priv->tx_queue[i]->dev = dev;
598 spin_lock_init(&(priv->tx_queue[i]->txlock));
599 }
600
601 for (i = 0; i < priv->num_rx_queues; i++) {
602 priv->rx_queue[i] = (struct gfar_priv_rx_q *)kmalloc(
603 sizeof (struct gfar_priv_rx_q), GFP_KERNEL);
604 if (!priv->rx_queue[i]) {
605 err = -ENOMEM;
606 goto rx_alloc_failed;
607 }
608 priv->rx_queue[i]->rx_skbuff = NULL;
609 priv->rx_queue[i]->qindex = i;
610 priv->rx_queue[i]->dev = dev;
611 spin_lock_init(&(priv->rx_queue[i]->rxlock));
612 }
613
614
Andy Fleming4d7902f2009-02-04 16:43:44 -0800615 stash = of_get_property(np, "bd-stash", NULL);
616
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000617 if (stash) {
Andy Fleming4d7902f2009-02-04 16:43:44 -0800618 priv->device_flags |= FSL_GIANFAR_DEV_HAS_BD_STASHING;
619 priv->bd_stash_en = 1;
620 }
621
622 stash_len = of_get_property(np, "rx-stash-len", NULL);
623
624 if (stash_len)
625 priv->rx_stash_size = *stash_len;
626
627 stash_idx = of_get_property(np, "rx-stash-idx", NULL);
628
629 if (stash_idx)
630 priv->rx_stash_index = *stash_idx;
631
632 if (stash_len || stash_idx)
633 priv->device_flags |= FSL_GIANFAR_DEV_HAS_BUF_STASHING;
634
Andy Flemingb31a1d82008-12-16 15:29:15 -0800635 mac_addr = of_get_mac_address(np);
636 if (mac_addr)
637 memcpy(dev->dev_addr, mac_addr, MAC_ADDR_LEN);
638
639 if (model && !strcasecmp(model, "TSEC"))
640 priv->device_flags =
641 FSL_GIANFAR_DEV_HAS_GIGABIT |
642 FSL_GIANFAR_DEV_HAS_COALESCE |
643 FSL_GIANFAR_DEV_HAS_RMON |
644 FSL_GIANFAR_DEV_HAS_MULTI_INTR;
645 if (model && !strcasecmp(model, "eTSEC"))
646 priv->device_flags =
647 FSL_GIANFAR_DEV_HAS_GIGABIT |
648 FSL_GIANFAR_DEV_HAS_COALESCE |
649 FSL_GIANFAR_DEV_HAS_RMON |
650 FSL_GIANFAR_DEV_HAS_MULTI_INTR |
Dai Haruki2c2db482008-12-16 15:31:15 -0800651 FSL_GIANFAR_DEV_HAS_PADDING |
Andy Flemingb31a1d82008-12-16 15:29:15 -0800652 FSL_GIANFAR_DEV_HAS_CSUM |
653 FSL_GIANFAR_DEV_HAS_VLAN |
654 FSL_GIANFAR_DEV_HAS_MAGIC_PACKET |
655 FSL_GIANFAR_DEV_HAS_EXTENDED_HASH;
656
657 ctype = of_get_property(np, "phy-connection-type", NULL);
658
659 /* We only care about rgmii-id. The rest are autodetected */
660 if (ctype && !strcmp(ctype, "rgmii-id"))
661 priv->interface = PHY_INTERFACE_MODE_RGMII_ID;
662 else
663 priv->interface = PHY_INTERFACE_MODE_MII;
664
665 if (of_get_property(np, "fsl,magic-packet", NULL))
666 priv->device_flags |= FSL_GIANFAR_DEV_HAS_MAGIC_PACKET;
667
Grant Likelyfe192a42009-04-25 12:53:12 +0000668 priv->phy_node = of_parse_phandle(np, "phy-handle", 0);
Andy Flemingb31a1d82008-12-16 15:29:15 -0800669
670 /* Find the TBI PHY. If it's not there, we don't support SGMII */
Grant Likelyfe192a42009-04-25 12:53:12 +0000671 priv->tbi_node = of_parse_phandle(np, "tbi-handle", 0);
Andy Flemingb31a1d82008-12-16 15:29:15 -0800672
673 return 0;
674
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000675rx_alloc_failed:
676 free_rx_pointers(priv);
677tx_alloc_failed:
678 free_tx_pointers(priv);
Andy Flemingb31a1d82008-12-16 15:29:15 -0800679err_out:
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000680 iounmap(priv->gfargrp.regs);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000681 free_netdev(dev);
Andy Flemingb31a1d82008-12-16 15:29:15 -0800682 return err;
683}
684
Clifford Wolf0faac9f2009-01-09 10:23:11 +0000685/* Ioctl MII Interface */
686static int gfar_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
687{
688 struct gfar_private *priv = netdev_priv(dev);
689
690 if (!netif_running(dev))
691 return -EINVAL;
692
693 if (!priv->phydev)
694 return -ENODEV;
695
696 return phy_mii_ioctl(priv->phydev, if_mii(rq), cmd);
697}
698
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000699static unsigned int reverse_bitmap(unsigned int bit_map, unsigned int max_qs)
700{
701 unsigned int new_bit_map = 0x0;
702 int mask = 0x1 << (max_qs - 1), i;
703 for (i = 0; i < max_qs; i++) {
704 if (bit_map & mask)
705 new_bit_map = new_bit_map + (1 << i);
706 mask = mask >> 0x1;
707 }
708 return new_bit_map;
709}
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400710/* Set up the ethernet device structure, private data,
711 * and anything else we need before we start */
Andy Flemingb31a1d82008-12-16 15:29:15 -0800712static int gfar_probe(struct of_device *ofdev,
713 const struct of_device_id *match)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714{
715 u32 tempval;
716 struct net_device *dev = NULL;
717 struct gfar_private *priv = NULL;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000718 struct gfar __iomem *regs = NULL;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000719 int err = 0, i;
Dai Harukic50a5d92008-12-17 16:51:32 -0800720 int len_devname;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000721 u32 rstat = 0, tstat = 0, rqueue = 0, tqueue = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000723 err = gfar_of_init(ofdev, &dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000725 if (err)
726 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
728 priv = netdev_priv(dev);
Kumar Gala48268572009-03-18 23:28:22 -0700729 priv->ndev = dev;
730 priv->ofdev = ofdev;
Andy Flemingb31a1d82008-12-16 15:29:15 -0800731 priv->node = ofdev->node;
Kumar Gala48268572009-03-18 23:28:22 -0700732 SET_NETDEV_DEV(dev, &ofdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000734 spin_lock_init(&priv->gfargrp.grplock);
Scott Woodd87eb122008-07-11 18:04:45 -0500735 spin_lock_init(&priv->bflock);
Sebastian Siewiorab939902008-08-19 21:12:45 +0200736 INIT_WORK(&priv->reset_task, gfar_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
Andy Flemingb31a1d82008-12-16 15:29:15 -0800738 dev_set_drvdata(&ofdev->dev, priv);
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000739 regs = priv->gfargrp.regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
741 /* Stop the DMA engine now, in case it was running before */
742 /* (The firmware could have used it, and left it running). */
Andy Fleming257d9382008-12-16 15:25:45 -0800743 gfar_halt(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
745 /* Reset MAC layer */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000746 gfar_write(&regs->maccfg1, MACCFG1_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
Andy Flemingb98ac702009-02-04 16:38:05 -0800748 /* We need to delay at least 3 TX clocks */
749 udelay(2);
750
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 tempval = (MACCFG1_TX_FLOW | MACCFG1_RX_FLOW);
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000752 gfar_write(&regs->maccfg1, tempval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
754 /* Initialize MACCFG2. */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000755 gfar_write(&regs->maccfg2, MACCFG2_INIT_SETTINGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
757 /* Initialize ECNTRL */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000758 gfar_write(&regs->ecntrl, ECNTRL_INIT_SETTINGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 /* Set the dev->base_addr to the gfar reg region */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000761 dev->base_addr = (unsigned long) regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
Andy Flemingb31a1d82008-12-16 15:29:15 -0800763 SET_NETDEV_DEV(dev, &ofdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
765 /* Fill in the dev structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 dev->watchdog_timeo = TX_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 dev->mtu = 1500;
Andy Fleming26ccfc32009-03-10 12:58:28 +0000768 dev->netdev_ops = &gfar_netdev_ops;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500769 dev->ethtool_ops = &gfar_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000771 /* Register for napi ...We are registering NAPI for each grp */
772 netif_napi_add(dev, &priv->gfargrp.napi, gfar_poll, GFAR_DEV_WEIGHT);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000773
Andy Flemingb31a1d82008-12-16 15:29:15 -0800774 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_CSUM) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500775 priv->rx_csum_enable = 1;
Dai Haruki4669bc92008-12-17 16:51:04 -0800776 dev->features |= NETIF_F_IP_CSUM | NETIF_F_SG | NETIF_F_HIGHDMA;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500777 } else
778 priv->rx_csum_enable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
Kumar Gala0bbaf062005-06-20 10:54:21 -0500780 priv->vlgrp = NULL;
781
Andy Fleming26ccfc32009-03-10 12:58:28 +0000782 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_VLAN)
Kumar Gala0bbaf062005-06-20 10:54:21 -0500783 dev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500784
Andy Flemingb31a1d82008-12-16 15:29:15 -0800785 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_EXTENDED_HASH) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500786 priv->extended_hash = 1;
787 priv->hash_width = 9;
788
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000789 priv->hash_regs[0] = &regs->igaddr0;
790 priv->hash_regs[1] = &regs->igaddr1;
791 priv->hash_regs[2] = &regs->igaddr2;
792 priv->hash_regs[3] = &regs->igaddr3;
793 priv->hash_regs[4] = &regs->igaddr4;
794 priv->hash_regs[5] = &regs->igaddr5;
795 priv->hash_regs[6] = &regs->igaddr6;
796 priv->hash_regs[7] = &regs->igaddr7;
797 priv->hash_regs[8] = &regs->gaddr0;
798 priv->hash_regs[9] = &regs->gaddr1;
799 priv->hash_regs[10] = &regs->gaddr2;
800 priv->hash_regs[11] = &regs->gaddr3;
801 priv->hash_regs[12] = &regs->gaddr4;
802 priv->hash_regs[13] = &regs->gaddr5;
803 priv->hash_regs[14] = &regs->gaddr6;
804 priv->hash_regs[15] = &regs->gaddr7;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500805
806 } else {
807 priv->extended_hash = 0;
808 priv->hash_width = 8;
809
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000810 priv->hash_regs[0] = &regs->gaddr0;
811 priv->hash_regs[1] = &regs->gaddr1;
812 priv->hash_regs[2] = &regs->gaddr2;
813 priv->hash_regs[3] = &regs->gaddr3;
814 priv->hash_regs[4] = &regs->gaddr4;
815 priv->hash_regs[5] = &regs->gaddr5;
816 priv->hash_regs[6] = &regs->gaddr6;
817 priv->hash_regs[7] = &regs->gaddr7;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500818 }
819
Andy Flemingb31a1d82008-12-16 15:29:15 -0800820 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_PADDING)
Kumar Gala0bbaf062005-06-20 10:54:21 -0500821 priv->padding = DEFAULT_PADDING;
822 else
823 priv->padding = 0;
824
Kumar Gala0bbaf062005-06-20 10:54:21 -0500825 if (dev->features & NETIF_F_IP_CSUM)
826 dev->hard_header_len += GMAC_FCB_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000828 /* Need to reverse the bit maps as bit_map's MSB is q0
829 * but, for_each_bit parses from right to left, which
830 * basically reverses the queue numbers */
831 priv->gfargrp.tx_bit_map = reverse_bitmap(priv->gfargrp.tx_bit_map, MAX_TX_QS);
832 priv->gfargrp.rx_bit_map = reverse_bitmap(priv->gfargrp.rx_bit_map, MAX_RX_QS);
833
834 /* Calculate RSTAT, TSTAT, RQUEUE and TQUEUE values */
835 for_each_bit(i, &priv->gfargrp.rx_bit_map, priv->num_rx_queues) {
836 priv->gfargrp.num_rx_queues++;
837 rstat = rstat | (RSTAT_CLEAR_RHALT >> i);
838 rqueue = rqueue | ((RQUEUE_EN0 | RQUEUE_EX0) >> i);
839 }
840 for_each_bit (i, &priv->gfargrp.tx_bit_map, priv->num_tx_queues) {
841 priv->gfargrp.num_tx_queues++;
842 tstat = tstat | (TSTAT_CLEAR_THALT >> i);
843 tqueue = tqueue | (TQUEUE_EN0 >> i);
844 }
845 priv->gfargrp.rstat = rstat;
846 priv->gfargrp.tstat = tstat;
847
848 gfar_write(&regs->rqueue, rqueue);
849 gfar_write(&regs->tqueue, tqueue);
850
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 priv->rx_buffer_size = DEFAULT_RX_BUFFER_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000853 /* Initializing some of the rx/tx queue level parameters */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000854 for (i = 0; i < priv->num_tx_queues; i++) {
855 priv->tx_queue[i]->tx_ring_size = DEFAULT_TX_RING_SIZE;
856 priv->tx_queue[i]->num_txbdfree = DEFAULT_TX_RING_SIZE;
857 priv->tx_queue[i]->txcoalescing = DEFAULT_TX_COALESCE;
858 priv->tx_queue[i]->txic = DEFAULT_TXIC;
859 }
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000860
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000861 for (i = 0; i < priv->num_rx_queues; i++) {
862 priv->rx_queue[i]->rx_ring_size = DEFAULT_RX_RING_SIZE;
863 priv->rx_queue[i]->rxcoalescing = DEFAULT_RX_COALESCE;
864 priv->rx_queue[i]->rxic = DEFAULT_RXIC;
865 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
Kumar Gala0bbaf062005-06-20 10:54:21 -0500867 /* Enable most messages by default */
868 priv->msg_enable = (NETIF_MSG_IFUP << 1 ) - 1;
869
Trent Piephod3eab822008-10-02 11:12:24 +0000870 /* Carrier starts down, phylib will bring it up */
871 netif_carrier_off(dev);
872
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 err = register_netdev(dev);
874
875 if (err) {
876 printk(KERN_ERR "%s: Cannot register net device, aborting.\n",
877 dev->name);
878 goto register_fail;
879 }
880
Anton Vorontsov2884e5c2009-02-01 00:52:34 -0800881 device_init_wakeup(&dev->dev,
882 priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
883
Dai Harukic50a5d92008-12-17 16:51:32 -0800884 /* fill out IRQ number and name fields */
885 len_devname = strlen(dev->name);
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000886 strncpy(&priv->gfargrp.int_name_tx[0], dev->name, len_devname);
Dai Harukic50a5d92008-12-17 16:51:32 -0800887 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000888 strncpy(&priv->gfargrp.int_name_tx[len_devname],
Dai Harukic50a5d92008-12-17 16:51:32 -0800889 "_tx", sizeof("_tx") + 1);
890
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000891 strncpy(&priv->gfargrp.int_name_rx[0], dev->name, len_devname);
892 strncpy(&priv->gfargrp.int_name_rx[len_devname],
Dai Harukic50a5d92008-12-17 16:51:32 -0800893 "_rx", sizeof("_rx") + 1);
894
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000895 strncpy(&priv->gfargrp.int_name_er[0], dev->name, len_devname);
896 strncpy(&priv->gfargrp.int_name_er[len_devname],
Dai Harukic50a5d92008-12-17 16:51:32 -0800897 "_er", sizeof("_er") + 1);
898 } else
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000899 priv->gfargrp.int_name_tx[len_devname] = '\0';
Dai Harukic50a5d92008-12-17 16:51:32 -0800900
Andy Fleming7f7f5312005-11-11 12:38:59 -0600901 /* Create all the sysfs files */
902 gfar_init_sysfs(dev);
903
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 /* Print out the device info */
Johannes Berge1749612008-10-27 15:59:26 -0700905 printk(KERN_INFO DEVICE_NAME "%pM\n", dev->name, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
907 /* Even more device info helps when determining which kernel */
Andy Fleming7f7f5312005-11-11 12:38:59 -0600908 /* provided which set of benchmarks. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 printk(KERN_INFO "%s: Running with NAPI enabled\n", dev->name);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000910 for (i = 0; i < priv->num_rx_queues; i++)
911 printk(KERN_INFO "%s: :RX BD ring size for Q[%d]: %d\n",
912 dev->name, i, priv->rx_queue[i]->rx_ring_size);
913 for(i = 0; i < priv->num_tx_queues; i++)
914 printk(KERN_INFO "%s:TX BD ring size for Q[%d]: %d\n",
915 dev->name, i, priv->tx_queue[i]->tx_ring_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
917 return 0;
918
919register_fail:
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000920 iounmap(priv->gfargrp.regs);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000921 free_tx_pointers(priv);
922 free_rx_pointers(priv);
Grant Likelyfe192a42009-04-25 12:53:12 +0000923 if (priv->phy_node)
924 of_node_put(priv->phy_node);
925 if (priv->tbi_node)
926 of_node_put(priv->tbi_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 free_netdev(dev);
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400928 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929}
930
Andy Flemingb31a1d82008-12-16 15:29:15 -0800931static int gfar_remove(struct of_device *ofdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932{
Andy Flemingb31a1d82008-12-16 15:29:15 -0800933 struct gfar_private *priv = dev_get_drvdata(&ofdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
Grant Likelyfe192a42009-04-25 12:53:12 +0000935 if (priv->phy_node)
936 of_node_put(priv->phy_node);
937 if (priv->tbi_node)
938 of_node_put(priv->tbi_node);
939
Andy Flemingb31a1d82008-12-16 15:29:15 -0800940 dev_set_drvdata(&ofdev->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
David S. Millerd9d8e042009-09-06 01:41:02 -0700942 unregister_netdev(priv->ndev);
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000943 iounmap(priv->gfargrp.regs);
Kumar Gala48268572009-03-18 23:28:22 -0700944 free_netdev(priv->ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
946 return 0;
947}
948
Scott Woodd87eb122008-07-11 18:04:45 -0500949#ifdef CONFIG_PM
Anton Vorontsovbe926fc2009-10-12 06:00:42 +0000950
951static int gfar_suspend(struct device *dev)
Scott Woodd87eb122008-07-11 18:04:45 -0500952{
Anton Vorontsovbe926fc2009-10-12 06:00:42 +0000953 struct gfar_private *priv = dev_get_drvdata(dev);
954 struct net_device *ndev = priv->ndev;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000955 struct gfar __iomem *regs = NULL;
Scott Woodd87eb122008-07-11 18:04:45 -0500956 unsigned long flags;
957 u32 tempval;
958
959 int magic_packet = priv->wol_en &&
Andy Flemingb31a1d82008-12-16 15:29:15 -0800960 (priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
Scott Woodd87eb122008-07-11 18:04:45 -0500961
Anton Vorontsovbe926fc2009-10-12 06:00:42 +0000962 netif_device_detach(ndev);
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000963 regs = priv->gfargrp.regs;
Scott Woodd87eb122008-07-11 18:04:45 -0500964
Anton Vorontsovbe926fc2009-10-12 06:00:42 +0000965 if (netif_running(ndev)) {
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000966
967 local_irq_save(flags);
968 lock_tx_qs(priv);
969 lock_rx_qs(priv);
Scott Woodd87eb122008-07-11 18:04:45 -0500970
Anton Vorontsovbe926fc2009-10-12 06:00:42 +0000971 gfar_halt_nodisable(ndev);
Scott Woodd87eb122008-07-11 18:04:45 -0500972
973 /* Disable Tx, and Rx if wake-on-LAN is disabled. */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000974 tempval = gfar_read(&regs->maccfg1);
Scott Woodd87eb122008-07-11 18:04:45 -0500975
976 tempval &= ~MACCFG1_TX_EN;
977
978 if (!magic_packet)
979 tempval &= ~MACCFG1_RX_EN;
980
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000981 gfar_write(&regs->maccfg1, tempval);
Scott Woodd87eb122008-07-11 18:04:45 -0500982
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000983 unlock_rx_qs(priv);
984 unlock_tx_qs(priv);
985 local_irq_restore(flags);
Scott Woodd87eb122008-07-11 18:04:45 -0500986
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000987 napi_disable(&priv->gfargrp.napi);
Scott Woodd87eb122008-07-11 18:04:45 -0500988
989 if (magic_packet) {
990 /* Enable interrupt on Magic Packet */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000991 gfar_write(&regs->imask, IMASK_MAG);
Scott Woodd87eb122008-07-11 18:04:45 -0500992
993 /* Enable Magic Packet mode */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000994 tempval = gfar_read(&regs->maccfg2);
Scott Woodd87eb122008-07-11 18:04:45 -0500995 tempval |= MACCFG2_MPEN;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000996 gfar_write(&regs->maccfg2, tempval);
Scott Woodd87eb122008-07-11 18:04:45 -0500997 } else {
998 phy_stop(priv->phydev);
999 }
1000 }
1001
1002 return 0;
1003}
1004
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001005static int gfar_resume(struct device *dev)
Scott Woodd87eb122008-07-11 18:04:45 -05001006{
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001007 struct gfar_private *priv = dev_get_drvdata(dev);
1008 struct net_device *ndev = priv->ndev;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001009 struct gfar __iomem *regs = NULL;
Scott Woodd87eb122008-07-11 18:04:45 -05001010 unsigned long flags;
1011 u32 tempval;
1012 int magic_packet = priv->wol_en &&
Andy Flemingb31a1d82008-12-16 15:29:15 -08001013 (priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
Scott Woodd87eb122008-07-11 18:04:45 -05001014
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001015 if (!netif_running(ndev)) {
1016 netif_device_attach(ndev);
Scott Woodd87eb122008-07-11 18:04:45 -05001017 return 0;
1018 }
1019
1020 if (!magic_packet && priv->phydev)
1021 phy_start(priv->phydev);
1022
1023 /* Disable Magic Packet mode, in case something
1024 * else woke us up.
1025 */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001026 regs = priv->gfargrp.regs;
Scott Woodd87eb122008-07-11 18:04:45 -05001027
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001028 local_irq_save(flags);
1029 lock_tx_qs(priv);
1030 lock_rx_qs(priv);
Scott Woodd87eb122008-07-11 18:04:45 -05001031
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001032 tempval = gfar_read(&regs->maccfg2);
Scott Woodd87eb122008-07-11 18:04:45 -05001033 tempval &= ~MACCFG2_MPEN;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001034 gfar_write(&regs->maccfg2, tempval);
Scott Woodd87eb122008-07-11 18:04:45 -05001035
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001036 gfar_start(ndev);
Scott Woodd87eb122008-07-11 18:04:45 -05001037
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001038 unlock_rx_qs(priv);
1039 unlock_tx_qs(priv);
1040 local_irq_restore(flags);
Scott Woodd87eb122008-07-11 18:04:45 -05001041
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001042 netif_device_attach(ndev);
Scott Woodd87eb122008-07-11 18:04:45 -05001043
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001044 napi_enable(&priv->gfargrp.napi);
Scott Woodd87eb122008-07-11 18:04:45 -05001045
1046 return 0;
1047}
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001048
1049static int gfar_restore(struct device *dev)
1050{
1051 struct gfar_private *priv = dev_get_drvdata(dev);
1052 struct net_device *ndev = priv->ndev;
1053
1054 if (!netif_running(ndev))
1055 return 0;
1056
1057 gfar_init_bds(ndev);
1058 init_registers(ndev);
1059 gfar_set_mac_address(ndev);
1060 gfar_init_mac(ndev);
1061 gfar_start(ndev);
1062
1063 priv->oldlink = 0;
1064 priv->oldspeed = 0;
1065 priv->oldduplex = -1;
1066
1067 if (priv->phydev)
1068 phy_start(priv->phydev);
1069
1070 netif_device_attach(ndev);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001071 napi_enable(&priv->gfargrp.napi);
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001072
1073 return 0;
1074}
1075
1076static struct dev_pm_ops gfar_pm_ops = {
1077 .suspend = gfar_suspend,
1078 .resume = gfar_resume,
1079 .freeze = gfar_suspend,
1080 .thaw = gfar_resume,
1081 .restore = gfar_restore,
1082};
1083
1084#define GFAR_PM_OPS (&gfar_pm_ops)
1085
1086static int gfar_legacy_suspend(struct of_device *ofdev, pm_message_t state)
1087{
1088 return gfar_suspend(&ofdev->dev);
1089}
1090
1091static int gfar_legacy_resume(struct of_device *ofdev)
1092{
1093 return gfar_resume(&ofdev->dev);
1094}
1095
Scott Woodd87eb122008-07-11 18:04:45 -05001096#else
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001097
1098#define GFAR_PM_OPS NULL
1099#define gfar_legacy_suspend NULL
1100#define gfar_legacy_resume NULL
1101
Scott Woodd87eb122008-07-11 18:04:45 -05001102#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001104/* Reads the controller's registers to determine what interface
1105 * connects it to the PHY.
1106 */
1107static phy_interface_t gfar_get_interface(struct net_device *dev)
1108{
1109 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001110 struct gfar __iomem *regs = NULL;
1111 u32 ecntrl;
1112
1113 regs = priv->gfargrp.regs;
1114 ecntrl = gfar_read(&regs->ecntrl);
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001115
1116 if (ecntrl & ECNTRL_SGMII_MODE)
1117 return PHY_INTERFACE_MODE_SGMII;
1118
1119 if (ecntrl & ECNTRL_TBI_MODE) {
1120 if (ecntrl & ECNTRL_REDUCED_MODE)
1121 return PHY_INTERFACE_MODE_RTBI;
1122 else
1123 return PHY_INTERFACE_MODE_TBI;
1124 }
1125
1126 if (ecntrl & ECNTRL_REDUCED_MODE) {
1127 if (ecntrl & ECNTRL_REDUCED_MII_MODE)
1128 return PHY_INTERFACE_MODE_RMII;
Andy Fleming7132ab72007-07-11 11:43:07 -05001129 else {
Andy Flemingb31a1d82008-12-16 15:29:15 -08001130 phy_interface_t interface = priv->interface;
Andy Fleming7132ab72007-07-11 11:43:07 -05001131
1132 /*
1133 * This isn't autodetected right now, so it must
1134 * be set by the device tree or platform code.
1135 */
1136 if (interface == PHY_INTERFACE_MODE_RGMII_ID)
1137 return PHY_INTERFACE_MODE_RGMII_ID;
1138
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001139 return PHY_INTERFACE_MODE_RGMII;
Andy Fleming7132ab72007-07-11 11:43:07 -05001140 }
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001141 }
1142
Andy Flemingb31a1d82008-12-16 15:29:15 -08001143 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT)
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001144 return PHY_INTERFACE_MODE_GMII;
1145
1146 return PHY_INTERFACE_MODE_MII;
1147}
1148
1149
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001150/* Initializes driver's PHY state, and attaches to the PHY.
1151 * Returns 0 on success.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 */
1153static int init_phy(struct net_device *dev)
1154{
1155 struct gfar_private *priv = netdev_priv(dev);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001156 uint gigabit_support =
Andy Flemingb31a1d82008-12-16 15:29:15 -08001157 priv->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT ?
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001158 SUPPORTED_1000baseT_Full : 0;
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001159 phy_interface_t interface;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
1161 priv->oldlink = 0;
1162 priv->oldspeed = 0;
1163 priv->oldduplex = -1;
1164
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001165 interface = gfar_get_interface(dev);
1166
Anton Vorontsov1db780f2009-07-16 21:31:42 +00001167 priv->phydev = of_phy_connect(dev, priv->phy_node, &adjust_link, 0,
1168 interface);
1169 if (!priv->phydev)
1170 priv->phydev = of_phy_connect_fixed_link(dev, &adjust_link,
1171 interface);
1172 if (!priv->phydev) {
1173 dev_err(&dev->dev, "could not attach to PHY\n");
1174 return -ENODEV;
Grant Likelyfe192a42009-04-25 12:53:12 +00001175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
Kapil Junejad3c12872007-05-11 18:25:11 -05001177 if (interface == PHY_INTERFACE_MODE_SGMII)
1178 gfar_configure_serdes(dev);
1179
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001180 /* Remove any features not supported by the controller */
Grant Likelyfe192a42009-04-25 12:53:12 +00001181 priv->phydev->supported &= (GFAR_SUPPORTED | gigabit_support);
1182 priv->phydev->advertising = priv->phydev->supported;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
1184 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185}
1186
Paul Gortmakerd0313582008-04-17 00:08:10 -04001187/*
1188 * Initialize TBI PHY interface for communicating with the
1189 * SERDES lynx PHY on the chip. We communicate with this PHY
1190 * through the MDIO bus on each controller, treating it as a
1191 * "normal" PHY at the address found in the TBIPA register. We assume
1192 * that the TBIPA register is valid. Either the MDIO bus code will set
1193 * it to a value that doesn't conflict with other PHYs on the bus, or the
1194 * value doesn't matter, as there are no other PHYs on the bus.
1195 */
Kapil Junejad3c12872007-05-11 18:25:11 -05001196static void gfar_configure_serdes(struct net_device *dev)
1197{
1198 struct gfar_private *priv = netdev_priv(dev);
Grant Likelyfe192a42009-04-25 12:53:12 +00001199 struct phy_device *tbiphy;
Trent Piephoc1324192008-10-30 18:17:06 -07001200
Grant Likelyfe192a42009-04-25 12:53:12 +00001201 if (!priv->tbi_node) {
1202 dev_warn(&dev->dev, "error: SGMII mode requires that the "
1203 "device tree specify a tbi-handle\n");
1204 return;
1205 }
1206
1207 tbiphy = of_phy_find_device(priv->tbi_node);
1208 if (!tbiphy) {
1209 dev_err(&dev->dev, "error: Could not get TBI device\n");
Andy Flemingb31a1d82008-12-16 15:29:15 -08001210 return;
1211 }
Kapil Junejad3c12872007-05-11 18:25:11 -05001212
Andy Flemingb31a1d82008-12-16 15:29:15 -08001213 /*
1214 * If the link is already up, we must already be ok, and don't need to
Trent Piephobdb59f92008-10-30 18:17:07 -07001215 * configure and reset the TBI<->SerDes link. Maybe U-Boot configured
1216 * everything for us? Resetting it takes the link down and requires
1217 * several seconds for it to come back.
1218 */
Grant Likelyfe192a42009-04-25 12:53:12 +00001219 if (phy_read(tbiphy, MII_BMSR) & BMSR_LSTATUS)
Andy Flemingb31a1d82008-12-16 15:29:15 -08001220 return;
Kapil Junejad3c12872007-05-11 18:25:11 -05001221
Paul Gortmakerd0313582008-04-17 00:08:10 -04001222 /* Single clk mode, mii mode off(for serdes communication) */
Grant Likelyfe192a42009-04-25 12:53:12 +00001223 phy_write(tbiphy, MII_TBICON, TBICON_CLK_SELECT);
Kapil Junejad3c12872007-05-11 18:25:11 -05001224
Grant Likelyfe192a42009-04-25 12:53:12 +00001225 phy_write(tbiphy, MII_ADVERTISE,
Kapil Junejad3c12872007-05-11 18:25:11 -05001226 ADVERTISE_1000XFULL | ADVERTISE_1000XPAUSE |
1227 ADVERTISE_1000XPSE_ASYM);
1228
Grant Likelyfe192a42009-04-25 12:53:12 +00001229 phy_write(tbiphy, MII_BMCR, BMCR_ANENABLE |
Kapil Junejad3c12872007-05-11 18:25:11 -05001230 BMCR_ANRESTART | BMCR_FULLDPLX | BMCR_SPEED1000);
1231}
1232
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233static void init_registers(struct net_device *dev)
1234{
1235 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001236 struct gfar __iomem *regs = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001238 regs = priv->gfargrp.regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 /* Clear IEVENT */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001240 gfar_write(&regs->ievent, IEVENT_INIT_CLEAR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
1242 /* Initialize IMASK */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001243 gfar_write(&regs->imask, IMASK_INIT_CLEAR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
1245 /* Init hash registers to zero */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001246 gfar_write(&regs->igaddr0, 0);
1247 gfar_write(&regs->igaddr1, 0);
1248 gfar_write(&regs->igaddr2, 0);
1249 gfar_write(&regs->igaddr3, 0);
1250 gfar_write(&regs->igaddr4, 0);
1251 gfar_write(&regs->igaddr5, 0);
1252 gfar_write(&regs->igaddr6, 0);
1253 gfar_write(&regs->igaddr7, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001255 gfar_write(&regs->gaddr0, 0);
1256 gfar_write(&regs->gaddr1, 0);
1257 gfar_write(&regs->gaddr2, 0);
1258 gfar_write(&regs->gaddr3, 0);
1259 gfar_write(&regs->gaddr4, 0);
1260 gfar_write(&regs->gaddr5, 0);
1261 gfar_write(&regs->gaddr6, 0);
1262 gfar_write(&regs->gaddr7, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 /* Zero out the rmon mib registers if it has them */
Andy Flemingb31a1d82008-12-16 15:29:15 -08001265 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_RMON) {
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001266 memset_io(&(regs->rmon), 0, sizeof (struct rmon_mib));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
1268 /* Mask off the CAM interrupts */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001269 gfar_write(&regs->rmon.cam1, 0xffffffff);
1270 gfar_write(&regs->rmon.cam2, 0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 }
1272
1273 /* Initialize the max receive buffer length */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001274 gfar_write(&regs->mrblr, priv->rx_buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 /* Initialize the Minimum Frame Length Register */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001277 gfar_write(&regs->minflr, MINFLR_INIT_SETTINGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278}
1279
Kumar Gala0bbaf062005-06-20 10:54:21 -05001280
1281/* Halt the receive and transmit queues */
Scott Woodd87eb122008-07-11 18:04:45 -05001282static void gfar_halt_nodisable(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283{
1284 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001285 struct gfar __iomem *regs = priv->gfargrp.regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 u32 tempval;
1287
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 /* Mask all interrupts */
1289 gfar_write(&regs->imask, IMASK_INIT_CLEAR);
1290
1291 /* Clear all interrupts */
1292 gfar_write(&regs->ievent, IEVENT_INIT_CLEAR);
1293
1294 /* Stop the DMA, and wait for it to stop */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001295 tempval = gfar_read(&regs->dmactrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 if ((tempval & (DMACTRL_GRS | DMACTRL_GTS))
1297 != (DMACTRL_GRS | DMACTRL_GTS)) {
1298 tempval |= (DMACTRL_GRS | DMACTRL_GTS);
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001299 gfar_write(&regs->dmactrl, tempval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001301 while (!(gfar_read(&regs->ievent) &
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 (IEVENT_GRSC | IEVENT_GTSC)))
1303 cpu_relax();
1304 }
Scott Woodd87eb122008-07-11 18:04:45 -05001305}
Scott Woodd87eb122008-07-11 18:04:45 -05001306
1307/* Halt the receive and transmit queues */
1308void gfar_halt(struct net_device *dev)
1309{
1310 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001311 struct gfar __iomem *regs = priv->gfargrp.regs;
Scott Woodd87eb122008-07-11 18:04:45 -05001312 u32 tempval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
Scott Wood2a54adc2008-08-12 15:10:46 -05001314 gfar_halt_nodisable(dev);
1315
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 /* Disable Rx and Tx */
1317 tempval = gfar_read(&regs->maccfg1);
1318 tempval &= ~(MACCFG1_RX_EN | MACCFG1_TX_EN);
1319 gfar_write(&regs->maccfg1, tempval);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001320}
1321
1322void stop_gfar(struct net_device *dev)
1323{
1324 struct gfar_private *priv = netdev_priv(dev);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001325 unsigned long flags;
1326
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001327 phy_stop(priv->phydev);
1328
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001329
Kumar Gala0bbaf062005-06-20 10:54:21 -05001330 /* Lock it down */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001331 local_irq_save(flags);
1332 lock_tx_qs(priv);
1333 lock_rx_qs(priv);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001334
Kumar Gala0bbaf062005-06-20 10:54:21 -05001335 gfar_halt(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001337 unlock_rx_qs(priv);
1338 unlock_tx_qs(priv);
1339 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
1341 /* Free the IRQs */
Andy Flemingb31a1d82008-12-16 15:29:15 -08001342 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001343 free_irq(priv->gfargrp.interruptError, &priv->gfargrp);
1344 free_irq(priv->gfargrp.interruptTransmit, &priv->gfargrp);
1345 free_irq(priv->gfargrp.interruptReceive, &priv->gfargrp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 } else {
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001347 free_irq(priv->gfargrp.interruptTransmit, &priv->gfargrp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 }
1349
1350 free_skb_resources(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351}
1352
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001353static void free_skb_tx_queue(struct gfar_priv_tx_q *tx_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 struct txbd8 *txbdp;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001356 struct gfar_private *priv = netdev_priv(tx_queue->dev);
Dai Haruki4669bc92008-12-17 16:51:04 -08001357 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001359 txbdp = tx_queue->tx_bd_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001361 for (i = 0; i < tx_queue->tx_ring_size; i++) {
1362 if (!tx_queue->tx_skbuff[i])
Dai Haruki4669bc92008-12-17 16:51:04 -08001363 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
Kumar Gala48268572009-03-18 23:28:22 -07001365 dma_unmap_single(&priv->ofdev->dev, txbdp->bufPtr,
Dai Haruki4669bc92008-12-17 16:51:04 -08001366 txbdp->length, DMA_TO_DEVICE);
1367 txbdp->lstatus = 0;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001368 for (j = 0; j < skb_shinfo(tx_queue->tx_skbuff[i])->nr_frags;
1369 j++) {
Dai Haruki4669bc92008-12-17 16:51:04 -08001370 txbdp++;
Kumar Gala48268572009-03-18 23:28:22 -07001371 dma_unmap_page(&priv->ofdev->dev, txbdp->bufPtr,
Dai Haruki4669bc92008-12-17 16:51:04 -08001372 txbdp->length, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 }
Andy Flemingad5da7a2008-05-07 13:20:55 -05001374 txbdp++;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001375 dev_kfree_skb_any(tx_queue->tx_skbuff[i]);
1376 tx_queue->tx_skbuff[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 }
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001378 kfree(tx_queue->tx_skbuff);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001379}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001381static void free_skb_rx_queue(struct gfar_priv_rx_q *rx_queue)
1382{
1383 struct rxbd8 *rxbdp;
1384 struct gfar_private *priv = netdev_priv(rx_queue->dev);
1385 int i;
1386
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001387 rxbdp = rx_queue->rx_bd_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001389 for (i = 0; i < rx_queue->rx_ring_size; i++) {
1390 if (rx_queue->rx_skbuff[i]) {
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001391 dma_unmap_single(&priv->ofdev->dev,
1392 rxbdp->bufPtr, priv->rx_buffer_size,
Anton Vorontsove69edd22009-10-12 06:00:30 +00001393 DMA_FROM_DEVICE);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001394 dev_kfree_skb_any(rx_queue->rx_skbuff[i]);
1395 rx_queue->rx_skbuff[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 }
Anton Vorontsove69edd22009-10-12 06:00:30 +00001397 rxbdp->lstatus = 0;
1398 rxbdp->bufPtr = 0;
1399 rxbdp++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 }
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001401 kfree(rx_queue->rx_skbuff);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001402}
Anton Vorontsove69edd22009-10-12 06:00:30 +00001403
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001404/* If there are any tx skbs or rx skbs still around, free them.
1405 * Then free tx_skbuff and rx_skbuff */
1406static void free_skb_resources(struct gfar_private *priv)
1407{
1408 struct gfar_priv_tx_q *tx_queue = NULL;
1409 struct gfar_priv_rx_q *rx_queue = NULL;
1410 int i;
1411
1412 /* Go through all the buffer descriptors and free their data buffers */
1413 for (i = 0; i < priv->num_tx_queues; i++) {
1414 tx_queue = priv->tx_queue[i];
1415 if(!tx_queue->tx_skbuff)
1416 free_skb_tx_queue(tx_queue);
1417 }
1418
1419 for (i = 0; i < priv->num_rx_queues; i++) {
1420 rx_queue = priv->rx_queue[i];
1421 if(!rx_queue->rx_skbuff)
1422 free_skb_rx_queue(rx_queue);
1423 }
1424
1425 dma_free_coherent(&priv->ofdev->dev,
1426 sizeof(struct txbd8) * priv->total_tx_ring_size +
1427 sizeof(struct rxbd8) * priv->total_rx_ring_size,
1428 priv->tx_queue[0]->tx_bd_base,
1429 priv->tx_queue[0]->tx_bd_dma_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430}
1431
Kumar Gala0bbaf062005-06-20 10:54:21 -05001432void gfar_start(struct net_device *dev)
1433{
1434 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001435 struct gfar __iomem *regs = priv->gfargrp.regs;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001436 u32 tempval;
1437
1438 /* Enable Rx and Tx in MACCFG1 */
1439 tempval = gfar_read(&regs->maccfg1);
1440 tempval |= (MACCFG1_RX_EN | MACCFG1_TX_EN);
1441 gfar_write(&regs->maccfg1, tempval);
1442
1443 /* Initialize DMACTRL to have WWR and WOP */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001444 tempval = gfar_read(&regs->dmactrl);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001445 tempval |= DMACTRL_INIT_SETTINGS;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001446 gfar_write(&regs->dmactrl, tempval);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001447
Kumar Gala0bbaf062005-06-20 10:54:21 -05001448 /* Make sure we aren't stopped */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001449 tempval = gfar_read(&regs->dmactrl);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001450 tempval &= ~(DMACTRL_GRS | DMACTRL_GTS);
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001451 gfar_write(&regs->dmactrl, tempval);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001452
Andy Flemingfef61082006-04-20 16:44:29 -05001453 /* Clear THLT/RHLT, so that the DMA starts polling now */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001454 gfar_write(&regs->tstat, priv->gfargrp.tstat);
1455 gfar_write(&regs->rstat, priv->gfargrp.rstat);
Andy Flemingfef61082006-04-20 16:44:29 -05001456
Kumar Gala0bbaf062005-06-20 10:54:21 -05001457 /* Unmask the interrupts we look for */
1458 gfar_write(&regs->imask, IMASK_DEFAULT);
Dai Haruki12dea572008-12-16 15:30:20 -08001459
1460 dev->trans_start = jiffies;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001461}
1462
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463/* Bring the controller up and running */
Anton Vorontsovccc05c62009-10-12 06:00:26 +00001464int startup_gfar(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465{
Anton Vorontsovccc05c62009-10-12 06:00:26 +00001466 struct gfar_private *priv = netdev_priv(ndev);
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001467 struct gfar __iomem *regs = priv->gfargrp.regs;
Anton Vorontsovccc05c62009-10-12 06:00:26 +00001468 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469
1470 gfar_write(&regs->imask, IMASK_INIT_CLEAR);
1471
Anton Vorontsov826aa4a2009-10-12 06:00:34 +00001472 err = gfar_alloc_skb_resources(ndev);
1473 if (err)
1474 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
Anton Vorontsov826aa4a2009-10-12 06:00:34 +00001476 gfar_init_mac(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 /* If the device has multiple interrupts, register for
1479 * them. Otherwise, only register for the one */
Andy Flemingb31a1d82008-12-16 15:29:15 -08001480 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001481 /* Install our interrupt handlers for Error,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 * Transmit, and Receive */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001483 err = request_irq(priv->gfargrp.interruptError, gfar_error, 0,
1484 priv->gfargrp.int_name_er, &priv->gfargrp);
Anton Vorontsovccc05c62009-10-12 06:00:26 +00001485 if (err) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001486 if (netif_msg_intr(priv))
Anton Vorontsovccc05c62009-10-12 06:00:26 +00001487 pr_err("%s: Can't get IRQ %d\n", ndev->name,
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001488 priv->gfargrp.interruptError);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 goto err_irq_fail;
1490 }
1491
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001492 err = request_irq(priv->gfargrp.interruptTransmit,
1493 gfar_transmit, 0,
1494 priv->gfargrp.int_name_tx,
1495 &priv->gfargrp);
Anton Vorontsovccc05c62009-10-12 06:00:26 +00001496 if (err) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001497 if (netif_msg_intr(priv))
Anton Vorontsovccc05c62009-10-12 06:00:26 +00001498 pr_err("%s: Can't get IRQ %d\n", ndev->name,
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001499 priv->gfargrp.interruptTransmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 goto tx_irq_fail;
1501 }
1502
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001503 err = request_irq(priv->gfargrp.interruptReceive,
1504 gfar_receive, 0,
1505 priv->gfargrp.int_name_rx,
1506 &priv->gfargrp);
Anton Vorontsovccc05c62009-10-12 06:00:26 +00001507 if (err) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001508 if (netif_msg_intr(priv))
Anton Vorontsovccc05c62009-10-12 06:00:26 +00001509 pr_err("%s: Can't get IRQ %d (receive0)\n",
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001510 ndev->name,
1511 priv->gfargrp.interruptReceive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 goto rx_irq_fail;
1513 }
1514 } else {
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001515 err = request_irq(priv->gfargrp.interruptTransmit,
1516 gfar_interrupt, 0,
1517 priv->gfargrp.int_name_tx,
1518 &priv->gfargrp);
Anton Vorontsovccc05c62009-10-12 06:00:26 +00001519 if (err) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001520 if (netif_msg_intr(priv))
Anton Vorontsovccc05c62009-10-12 06:00:26 +00001521 pr_err("%s: Can't get IRQ %d\n", ndev->name,
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001522 priv->gfargrp.interruptTransmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 goto err_irq_fail;
1524 }
1525 }
1526
Andy Fleming7f7f5312005-11-11 12:38:59 -06001527 /* Start the controller */
Anton Vorontsovccc05c62009-10-12 06:00:26 +00001528 gfar_start(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
Anton Vorontsov826aa4a2009-10-12 06:00:34 +00001530 phy_start(priv->phydev);
1531
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 return 0;
1533
1534rx_irq_fail:
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001535 free_irq(priv->gfargrp.interruptTransmit, &priv->gfargrp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536tx_irq_fail:
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001537 free_irq(priv->gfargrp.interruptError, &priv->gfargrp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538err_irq_fail:
Anton Vorontsove69edd22009-10-12 06:00:30 +00001539 free_skb_resources(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 return err;
1541}
1542
1543/* Called when something needs to use the ethernet device */
1544/* Returns 0 for success. */
1545static int gfar_enet_open(struct net_device *dev)
1546{
Li Yang94e8cc32007-10-12 21:53:51 +08001547 struct gfar_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 int err;
1549
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001550 napi_enable(&priv->gfargrp.napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001551
Andy Fleming0fd56bb2009-02-04 16:43:16 -08001552 skb_queue_head_init(&priv->rx_recycle);
1553
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 /* Initialize a bunch of registers */
1555 init_registers(dev);
1556
1557 gfar_set_mac_address(dev);
1558
1559 err = init_phy(dev);
1560
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001561 if (err) {
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001562 napi_disable(&priv->gfargrp.napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 return err;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001564 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
1566 err = startup_gfar(dev);
Anton Vorontsovdb0e8e32007-10-17 23:57:46 +04001567 if (err) {
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001568 napi_disable(&priv->gfargrp.napi);
Anton Vorontsovdb0e8e32007-10-17 23:57:46 +04001569 return err;
1570 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001572 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573
Anton Vorontsov2884e5c2009-02-01 00:52:34 -08001574 device_set_wakeup_enable(&dev->dev, priv->wol_en);
1575
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 return err;
1577}
1578
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07001579static inline struct txfcb *gfar_add_fcb(struct sk_buff *skb)
Kumar Gala0bbaf062005-06-20 10:54:21 -05001580{
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07001581 struct txfcb *fcb = (struct txfcb *)skb_push(skb, GMAC_FCB_LEN);
Kumar Gala6c31d552009-04-28 08:04:10 -07001582
1583 memset(fcb, 0, GMAC_FCB_LEN);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001584
Kumar Gala0bbaf062005-06-20 10:54:21 -05001585 return fcb;
1586}
1587
1588static inline void gfar_tx_checksum(struct sk_buff *skb, struct txfcb *fcb)
1589{
Andy Fleming7f7f5312005-11-11 12:38:59 -06001590 u8 flags = 0;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001591
1592 /* If we're here, it's a IP packet with a TCP or UDP
1593 * payload. We set it to checksum, using a pseudo-header
1594 * we provide
1595 */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001596 flags = TXFCB_DEFAULT;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001597
Andy Fleming7f7f5312005-11-11 12:38:59 -06001598 /* Tell the controller what the protocol is */
1599 /* And provide the already calculated phcs */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001600 if (ip_hdr(skb)->protocol == IPPROTO_UDP) {
Andy Fleming7f7f5312005-11-11 12:38:59 -06001601 flags |= TXFCB_UDP;
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -03001602 fcb->phcs = udp_hdr(skb)->check;
Andy Fleming7f7f5312005-11-11 12:38:59 -06001603 } else
Kumar Gala8da32de2007-06-29 00:12:04 -05001604 fcb->phcs = tcp_hdr(skb)->check;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001605
1606 /* l3os is the distance between the start of the
1607 * frame (skb->data) and the start of the IP hdr.
1608 * l4os is the distance between the start of the
1609 * l3 hdr and the l4 hdr */
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03001610 fcb->l3os = (u16)(skb_network_offset(skb) - GMAC_FCB_LEN);
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -03001611 fcb->l4os = skb_network_header_len(skb);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001612
Andy Fleming7f7f5312005-11-11 12:38:59 -06001613 fcb->flags = flags;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001614}
1615
Andy Fleming7f7f5312005-11-11 12:38:59 -06001616void inline gfar_tx_vlan(struct sk_buff *skb, struct txfcb *fcb)
Kumar Gala0bbaf062005-06-20 10:54:21 -05001617{
Andy Fleming7f7f5312005-11-11 12:38:59 -06001618 fcb->flags |= TXFCB_VLN;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001619 fcb->vlctl = vlan_tx_tag_get(skb);
1620}
1621
Dai Haruki4669bc92008-12-17 16:51:04 -08001622static inline struct txbd8 *skip_txbd(struct txbd8 *bdp, int stride,
1623 struct txbd8 *base, int ring_size)
1624{
1625 struct txbd8 *new_bd = bdp + stride;
1626
1627 return (new_bd >= (base + ring_size)) ? (new_bd - ring_size) : new_bd;
1628}
1629
1630static inline struct txbd8 *next_txbd(struct txbd8 *bdp, struct txbd8 *base,
1631 int ring_size)
1632{
1633 return skip_txbd(bdp, 1, base, ring_size);
1634}
1635
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636/* This is called by the kernel when a frame is ready for transmission. */
1637/* It is pointed to by the dev->hard_start_xmit function pointer */
1638static int gfar_start_xmit(struct sk_buff *skb, struct net_device *dev)
1639{
1640 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001641 struct gfar_priv_tx_q *tx_queue = NULL;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001642 struct netdev_queue *txq;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001643 struct gfar __iomem *regs = NULL;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001644 struct txfcb *fcb = NULL;
Dai Haruki4669bc92008-12-17 16:51:04 -08001645 struct txbd8 *txbdp, *txbdp_start, *base;
Dai Haruki5a5efed2008-12-16 15:34:50 -08001646 u32 lstatus;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001647 int i, rq = 0;
Dai Haruki4669bc92008-12-17 16:51:04 -08001648 u32 bufaddr;
Andy Flemingfef61082006-04-20 16:44:29 -05001649 unsigned long flags;
Dai Haruki4669bc92008-12-17 16:51:04 -08001650 unsigned int nr_frags, length;
1651
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001652
1653 rq = skb->queue_mapping;
1654 tx_queue = priv->tx_queue[rq];
1655 txq = netdev_get_tx_queue(dev, rq);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001656 base = tx_queue->tx_bd_base;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001657 regs = priv->gfargrp.regs;
Dai Haruki4669bc92008-12-17 16:51:04 -08001658
Li Yang5b28bea2009-03-27 15:54:30 -07001659 /* make space for additional header when fcb is needed */
1660 if (((skb->ip_summed == CHECKSUM_PARTIAL) ||
1661 (priv->vlgrp && vlan_tx_tag_present(skb))) &&
1662 (skb_headroom(skb) < GMAC_FCB_LEN)) {
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07001663 struct sk_buff *skb_new;
1664
1665 skb_new = skb_realloc_headroom(skb, GMAC_FCB_LEN);
1666 if (!skb_new) {
1667 dev->stats.tx_errors++;
David S. Millerbd14ba82009-03-27 01:10:58 -07001668 kfree_skb(skb);
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07001669 return NETDEV_TX_OK;
1670 }
1671 kfree_skb(skb);
1672 skb = skb_new;
1673 }
1674
Dai Haruki4669bc92008-12-17 16:51:04 -08001675 /* total number of fragments in the SKB */
1676 nr_frags = skb_shinfo(skb)->nr_frags;
1677
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001678 spin_lock_irqsave(&tx_queue->txlock, flags);
Dai Haruki4669bc92008-12-17 16:51:04 -08001679
1680 /* check if there is space to queue this packet */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001681 if ((nr_frags+1) > tx_queue->num_txbdfree) {
Dai Haruki4669bc92008-12-17 16:51:04 -08001682 /* no space, stop the queue */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001683 netif_tx_stop_queue(txq);
Dai Haruki4669bc92008-12-17 16:51:04 -08001684 dev->stats.tx_fifo_errors++;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001685 spin_unlock_irqrestore(&tx_queue->txlock, flags);
Dai Haruki4669bc92008-12-17 16:51:04 -08001686 return NETDEV_TX_BUSY;
1687 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
1689 /* Update transmit stats */
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001690 dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001692 txbdp = txbdp_start = tx_queue->cur_tx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
Dai Haruki4669bc92008-12-17 16:51:04 -08001694 if (nr_frags == 0) {
1695 lstatus = txbdp->lstatus | BD_LFLAG(TXBD_LAST | TXBD_INTERRUPT);
1696 } else {
1697 /* Place the fragment addresses and lengths into the TxBDs */
1698 for (i = 0; i < nr_frags; i++) {
1699 /* Point at the next BD, wrapping as needed */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001700 txbdp = next_txbd(txbdp, base, tx_queue->tx_ring_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701
Dai Haruki4669bc92008-12-17 16:51:04 -08001702 length = skb_shinfo(skb)->frags[i].size;
1703
1704 lstatus = txbdp->lstatus | length |
1705 BD_LFLAG(TXBD_READY);
1706
1707 /* Handle the last BD specially */
1708 if (i == nr_frags - 1)
1709 lstatus |= BD_LFLAG(TXBD_LAST | TXBD_INTERRUPT);
1710
Kumar Gala48268572009-03-18 23:28:22 -07001711 bufaddr = dma_map_page(&priv->ofdev->dev,
Dai Haruki4669bc92008-12-17 16:51:04 -08001712 skb_shinfo(skb)->frags[i].page,
1713 skb_shinfo(skb)->frags[i].page_offset,
1714 length,
1715 DMA_TO_DEVICE);
1716
1717 /* set the TxBD length and buffer pointer */
1718 txbdp->bufPtr = bufaddr;
1719 txbdp->lstatus = lstatus;
1720 }
1721
1722 lstatus = txbdp_start->lstatus;
1723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724
Kumar Gala0bbaf062005-06-20 10:54:21 -05001725 /* Set up checksumming */
Dai Haruki12dea572008-12-16 15:30:20 -08001726 if (CHECKSUM_PARTIAL == skb->ip_summed) {
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07001727 fcb = gfar_add_fcb(skb);
1728 lstatus |= BD_LFLAG(TXBD_TOE);
1729 gfar_tx_checksum(skb, fcb);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001730 }
1731
Dai Haruki77ecaf22008-12-16 15:30:48 -08001732 if (priv->vlgrp && vlan_tx_tag_present(skb)) {
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07001733 if (unlikely(NULL == fcb)) {
1734 fcb = gfar_add_fcb(skb);
Dai Haruki5a5efed2008-12-16 15:34:50 -08001735 lstatus |= BD_LFLAG(TXBD_TOE);
Andy Fleming7f7f5312005-11-11 12:38:59 -06001736 }
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07001737
1738 gfar_tx_vlan(skb, fcb);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001739 }
1740
Dai Haruki4669bc92008-12-17 16:51:04 -08001741 /* setup the TxBD length and buffer pointer for the first BD */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001742 tx_queue->tx_skbuff[tx_queue->skb_curtx] = skb;
Kumar Gala48268572009-03-18 23:28:22 -07001743 txbdp_start->bufPtr = dma_map_single(&priv->ofdev->dev, skb->data,
Dai Haruki4669bc92008-12-17 16:51:04 -08001744 skb_headlen(skb), DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745
Dai Haruki4669bc92008-12-17 16:51:04 -08001746 lstatus |= BD_LFLAG(TXBD_CRC | TXBD_READY) | skb_headlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747
Dai Haruki4669bc92008-12-17 16:51:04 -08001748 /*
1749 * The powerpc-specific eieio() is used, as wmb() has too strong
Scott Wood3b6330c2007-05-16 15:06:59 -05001750 * semantics (it requires synchronization between cacheable and
1751 * uncacheable mappings, which eieio doesn't provide and which we
1752 * don't need), thus requiring a more expensive sync instruction. At
1753 * some point, the set of architecture-independent barrier functions
1754 * should be expanded to include weaker barriers.
1755 */
Scott Wood3b6330c2007-05-16 15:06:59 -05001756 eieio();
Andy Fleming7f7f5312005-11-11 12:38:59 -06001757
Dai Haruki4669bc92008-12-17 16:51:04 -08001758 txbdp_start->lstatus = lstatus;
1759
1760 /* Update the current skb pointer to the next entry we will use
1761 * (wrapping if necessary) */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001762 tx_queue->skb_curtx = (tx_queue->skb_curtx + 1) &
1763 TX_RING_MOD_MASK(tx_queue->tx_ring_size);
Dai Haruki4669bc92008-12-17 16:51:04 -08001764
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001765 tx_queue->cur_tx = next_txbd(txbdp, base, tx_queue->tx_ring_size);
Dai Haruki4669bc92008-12-17 16:51:04 -08001766
1767 /* reduce TxBD free count */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001768 tx_queue->num_txbdfree -= (nr_frags + 1);
Dai Haruki4669bc92008-12-17 16:51:04 -08001769
1770 dev->trans_start = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771
1772 /* If the next BD still needs to be cleaned up, then the bds
1773 are full. We need to tell the kernel to stop sending us stuff. */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001774 if (!tx_queue->num_txbdfree) {
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001775 netif_tx_stop_queue(txq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001777 dev->stats.tx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 }
1779
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 /* Tell the DMA to go go go */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001781 gfar_write(&regs->tstat, TSTAT_CLEAR_THALT >> tx_queue->qindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782
1783 /* Unlock priv */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001784 spin_unlock_irqrestore(&tx_queue->txlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07001786 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787}
1788
1789/* Stops the kernel queue, and halts the controller */
1790static int gfar_close(struct net_device *dev)
1791{
1792 struct gfar_private *priv = netdev_priv(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001793
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001794 napi_disable(&priv->gfargrp.napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001795
Andy Fleming0fd56bb2009-02-04 16:43:16 -08001796 skb_queue_purge(&priv->rx_recycle);
Sebastian Siewiorab939902008-08-19 21:12:45 +02001797 cancel_work_sync(&priv->reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 stop_gfar(dev);
1799
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001800 /* Disconnect from the PHY */
1801 phy_disconnect(priv->phydev);
1802 priv->phydev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001804 netif_tx_stop_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
1806 return 0;
1807}
1808
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809/* Changes the mac address if the controller is not running. */
Andy Flemingf162b9d2008-05-02 13:00:30 -05001810static int gfar_set_mac_address(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811{
Andy Fleming7f7f5312005-11-11 12:38:59 -06001812 gfar_set_mac_for_addr(dev, 0, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813
1814 return 0;
1815}
1816
1817
Kumar Gala0bbaf062005-06-20 10:54:21 -05001818/* Enables and disables VLAN insertion/extraction */
1819static void gfar_vlan_rx_register(struct net_device *dev,
1820 struct vlan_group *grp)
1821{
1822 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001823 struct gfar __iomem *regs = NULL;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001824 unsigned long flags;
1825 u32 tempval;
1826
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001827 regs = priv->gfargrp.regs;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001828 local_irq_save(flags);
1829 lock_rx_qs(priv);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001830
Anton Vorontsovcd1f55a2009-01-26 14:33:23 -08001831 priv->vlgrp = grp;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001832
1833 if (grp) {
1834 /* Enable VLAN tag insertion */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001835 tempval = gfar_read(&regs->tctrl);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001836 tempval |= TCTRL_VLINS;
1837
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001838 gfar_write(&regs->tctrl, tempval);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001839
Kumar Gala0bbaf062005-06-20 10:54:21 -05001840 /* Enable VLAN tag extraction */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001841 tempval = gfar_read(&regs->rctrl);
Dai Haruki77ecaf22008-12-16 15:30:48 -08001842 tempval |= (RCTRL_VLEX | RCTRL_PRSDEP_INIT);
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001843 gfar_write(&regs->rctrl, tempval);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001844 } else {
1845 /* Disable VLAN tag insertion */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001846 tempval = gfar_read(&regs->tctrl);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001847 tempval &= ~TCTRL_VLINS;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001848 gfar_write(&regs->tctrl, tempval);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001849
1850 /* Disable VLAN tag extraction */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001851 tempval = gfar_read(&regs->rctrl);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001852 tempval &= ~RCTRL_VLEX;
Dai Haruki77ecaf22008-12-16 15:30:48 -08001853 /* If parse is no longer required, then disable parser */
1854 if (tempval & RCTRL_REQ_PARSER)
1855 tempval |= RCTRL_PRSDEP_INIT;
1856 else
1857 tempval &= ~RCTRL_PRSDEP_INIT;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001858 gfar_write(&regs->rctrl, tempval);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001859 }
1860
Dai Haruki77ecaf22008-12-16 15:30:48 -08001861 gfar_change_mtu(dev, dev->mtu);
1862
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001863 unlock_rx_qs(priv);
1864 local_irq_restore(flags);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001865}
1866
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867static int gfar_change_mtu(struct net_device *dev, int new_mtu)
1868{
1869 int tempsize, tempval;
1870 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001871 struct gfar __iomem *regs = priv->gfargrp.regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 int oldsize = priv->rx_buffer_size;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001873 int frame_size = new_mtu + ETH_HLEN;
1874
Dai Haruki77ecaf22008-12-16 15:30:48 -08001875 if (priv->vlgrp)
Dai Harukifaa89572008-03-24 10:53:26 -05001876 frame_size += VLAN_HLEN;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001877
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 if ((frame_size < 64) || (frame_size > JUMBO_FRAME_SIZE)) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001879 if (netif_msg_drv(priv))
1880 printk(KERN_ERR "%s: Invalid MTU setting\n",
1881 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 return -EINVAL;
1883 }
1884
Dai Haruki77ecaf22008-12-16 15:30:48 -08001885 if (gfar_uses_fcb(priv))
1886 frame_size += GMAC_FCB_LEN;
1887
1888 frame_size += priv->padding;
1889
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 tempsize =
1891 (frame_size & ~(INCREMENTAL_BUFFER_SIZE - 1)) +
1892 INCREMENTAL_BUFFER_SIZE;
1893
1894 /* Only stop and start the controller if it isn't already
Andy Fleming7f7f5312005-11-11 12:38:59 -06001895 * stopped, and we changed something */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 if ((oldsize != tempsize) && (dev->flags & IFF_UP))
1897 stop_gfar(dev);
1898
1899 priv->rx_buffer_size = tempsize;
1900
1901 dev->mtu = new_mtu;
1902
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001903 gfar_write(&regs->mrblr, priv->rx_buffer_size);
1904 gfar_write(&regs->maxfrm, priv->rx_buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905
1906 /* If the mtu is larger than the max size for standard
1907 * ethernet frames (ie, a jumbo frame), then set maccfg2
1908 * to allow huge frames, and to check the length */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001909 tempval = gfar_read(&regs->maccfg2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910
1911 if (priv->rx_buffer_size > DEFAULT_RX_BUFFER_SIZE)
1912 tempval |= (MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK);
1913 else
1914 tempval &= ~(MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK);
1915
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001916 gfar_write(&regs->maccfg2, tempval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917
1918 if ((oldsize != tempsize) && (dev->flags & IFF_UP))
1919 startup_gfar(dev);
1920
1921 return 0;
1922}
1923
Sebastian Siewiorab939902008-08-19 21:12:45 +02001924/* gfar_reset_task gets scheduled when a packet has not been
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 * transmitted after a set amount of time.
1926 * For now, assume that clearing out all the structures, and
Sebastian Siewiorab939902008-08-19 21:12:45 +02001927 * starting over will fix the problem.
1928 */
1929static void gfar_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930{
Sebastian Siewiorab939902008-08-19 21:12:45 +02001931 struct gfar_private *priv = container_of(work, struct gfar_private,
1932 reset_task);
Kumar Gala48268572009-03-18 23:28:22 -07001933 struct net_device *dev = priv->ndev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
1935 if (dev->flags & IFF_UP) {
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001936 netif_tx_stop_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 stop_gfar(dev);
1938 startup_gfar(dev);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001939 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 }
1941
David S. Miller263ba322008-07-15 03:47:41 -07001942 netif_tx_schedule_all(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943}
1944
Sebastian Siewiorab939902008-08-19 21:12:45 +02001945static void gfar_timeout(struct net_device *dev)
1946{
1947 struct gfar_private *priv = netdev_priv(dev);
1948
1949 dev->stats.tx_errors++;
1950 schedule_work(&priv->reset_task);
1951}
1952
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953/* Interrupt Handler for Transmit complete */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001954static int gfar_clean_tx_ring(struct gfar_priv_tx_q *tx_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955{
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001956 struct net_device *dev = tx_queue->dev;
Dai Harukid080cd62008-04-09 19:37:51 -05001957 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001958 struct gfar_priv_rx_q *rx_queue = NULL;
Dai Haruki4669bc92008-12-17 16:51:04 -08001959 struct txbd8 *bdp;
1960 struct txbd8 *lbdp = NULL;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001961 struct txbd8 *base = tx_queue->tx_bd_base;
Dai Haruki4669bc92008-12-17 16:51:04 -08001962 struct sk_buff *skb;
1963 int skb_dirtytx;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001964 int tx_ring_size = tx_queue->tx_ring_size;
Dai Haruki4669bc92008-12-17 16:51:04 -08001965 int frags = 0;
1966 int i;
Dai Harukid080cd62008-04-09 19:37:51 -05001967 int howmany = 0;
Dai Haruki4669bc92008-12-17 16:51:04 -08001968 u32 lstatus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001970 rx_queue = priv->rx_queue[tx_queue->qindex];
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001971 bdp = tx_queue->dirty_tx;
1972 skb_dirtytx = tx_queue->skb_dirtytx;
Dai Haruki4669bc92008-12-17 16:51:04 -08001973
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001974 while ((skb = tx_queue->tx_skbuff[skb_dirtytx])) {
Dai Haruki4669bc92008-12-17 16:51:04 -08001975 frags = skb_shinfo(skb)->nr_frags;
1976 lbdp = skip_txbd(bdp, frags, base, tx_ring_size);
1977
1978 lstatus = lbdp->lstatus;
1979
1980 /* Only clean completed frames */
1981 if ((lstatus & BD_LFLAG(TXBD_READY)) &&
1982 (lstatus & BD_LENGTH_MASK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 break;
1984
Kumar Gala48268572009-03-18 23:28:22 -07001985 dma_unmap_single(&priv->ofdev->dev,
Dai Haruki4669bc92008-12-17 16:51:04 -08001986 bdp->bufPtr,
1987 bdp->length,
1988 DMA_TO_DEVICE);
1989
1990 bdp->lstatus &= BD_LFLAG(TXBD_WRAP);
1991 bdp = next_txbd(bdp, base, tx_ring_size);
1992
1993 for (i = 0; i < frags; i++) {
Kumar Gala48268572009-03-18 23:28:22 -07001994 dma_unmap_page(&priv->ofdev->dev,
Dai Haruki4669bc92008-12-17 16:51:04 -08001995 bdp->bufPtr,
1996 bdp->length,
1997 DMA_TO_DEVICE);
1998 bdp->lstatus &= BD_LFLAG(TXBD_WRAP);
1999 bdp = next_txbd(bdp, base, tx_ring_size);
2000 }
2001
Andy Fleming0fd56bb2009-02-04 16:43:16 -08002002 /*
2003 * If there's room in the queue (limit it to rx_buffer_size)
2004 * we add this skb back into the pool, if it's the right size
2005 */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002006 if (skb_queue_len(&priv->rx_recycle) < rx_queue->rx_ring_size &&
Andy Fleming0fd56bb2009-02-04 16:43:16 -08002007 skb_recycle_check(skb, priv->rx_buffer_size +
2008 RXBUF_ALIGNMENT))
2009 __skb_queue_head(&priv->rx_recycle, skb);
2010 else
2011 dev_kfree_skb_any(skb);
2012
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002013 tx_queue->tx_skbuff[skb_dirtytx] = NULL;
Dai Haruki4669bc92008-12-17 16:51:04 -08002014
2015 skb_dirtytx = (skb_dirtytx + 1) &
2016 TX_RING_MOD_MASK(tx_ring_size);
2017
Dai Harukid080cd62008-04-09 19:37:51 -05002018 howmany++;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002019 tx_queue->num_txbdfree += frags + 1;
Dai Haruki4669bc92008-12-17 16:51:04 -08002020 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021
Dai Haruki4669bc92008-12-17 16:51:04 -08002022 /* If we freed a buffer, we can restart transmission, if necessary */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002023 if (__netif_subqueue_stopped(dev, tx_queue->qindex) && tx_queue->num_txbdfree)
2024 netif_wake_subqueue(dev, tx_queue->qindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025
Dai Haruki4669bc92008-12-17 16:51:04 -08002026 /* Update dirty indicators */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002027 tx_queue->skb_dirtytx = skb_dirtytx;
2028 tx_queue->dirty_tx = bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029
Dai Harukid080cd62008-04-09 19:37:51 -05002030 dev->stats.tx_packets += howmany;
2031
2032 return howmany;
2033}
2034
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002035static void gfar_schedule_cleanup(struct gfar_priv_grp *gfargrp)
Dai Haruki8c7396a2008-12-17 16:52:00 -08002036{
Anton Vorontsova6d0b912009-01-12 21:57:34 -08002037 unsigned long flags;
2038
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002039 spin_lock_irqsave(&gfargrp->grplock, flags);
2040 if (napi_schedule_prep(&gfargrp->napi)) {
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002041 gfar_write(&gfargrp->regs->imask, IMASK_RTX_DISABLED);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002042 __napi_schedule(&gfargrp->napi);
Jarek Poplawski8707bdd2009-02-09 14:59:30 -08002043 } else {
2044 /*
2045 * Clear IEVENT, so interrupts aren't called again
2046 * because of the packets that have already arrived.
2047 */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002048 gfar_write(&gfargrp->regs->ievent, IEVENT_RTX_MASK);
Dai Haruki8c7396a2008-12-17 16:52:00 -08002049 }
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002050 spin_unlock_irqrestore(&gfargrp->grplock, flags);
Anton Vorontsova6d0b912009-01-12 21:57:34 -08002051
Dai Haruki8c7396a2008-12-17 16:52:00 -08002052}
2053
Dai Harukid080cd62008-04-09 19:37:51 -05002054/* Interrupt Handler for Transmit complete */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002055static irqreturn_t gfar_transmit(int irq, void *grp_id)
Dai Harukid080cd62008-04-09 19:37:51 -05002056{
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002057 gfar_schedule_cleanup((struct gfar_priv_grp *)grp_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 return IRQ_HANDLED;
2059}
2060
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002061static void gfar_new_rxbdp(struct gfar_priv_rx_q *rx_queue, struct rxbd8 *bdp,
Andy Fleming815b97c2008-04-22 17:18:29 -05002062 struct sk_buff *skb)
2063{
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002064 struct net_device *dev = rx_queue->dev;
Andy Fleming815b97c2008-04-22 17:18:29 -05002065 struct gfar_private *priv = netdev_priv(dev);
Anton Vorontsov8a102fe2009-10-12 06:00:37 +00002066 dma_addr_t buf;
Andy Fleming815b97c2008-04-22 17:18:29 -05002067
Anton Vorontsov8a102fe2009-10-12 06:00:37 +00002068 buf = dma_map_single(&priv->ofdev->dev, skb->data,
2069 priv->rx_buffer_size, DMA_FROM_DEVICE);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002070 gfar_init_rxbdp(rx_queue, bdp, buf);
Andy Fleming815b97c2008-04-22 17:18:29 -05002071}
2072
2073
2074struct sk_buff * gfar_new_skb(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075{
Andy Fleming7f7f5312005-11-11 12:38:59 -06002076 unsigned int alignamount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 struct gfar_private *priv = netdev_priv(dev);
2078 struct sk_buff *skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079
Andy Fleming0fd56bb2009-02-04 16:43:16 -08002080 skb = __skb_dequeue(&priv->rx_recycle);
2081 if (!skb)
2082 skb = netdev_alloc_skb(dev,
2083 priv->rx_buffer_size + RXBUF_ALIGNMENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084
Andy Fleming815b97c2008-04-22 17:18:29 -05002085 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 return NULL;
2087
Andy Fleming7f7f5312005-11-11 12:38:59 -06002088 alignamount = RXBUF_ALIGNMENT -
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002089 (((unsigned long) skb->data) & (RXBUF_ALIGNMENT - 1));
Andy Fleming7f7f5312005-11-11 12:38:59 -06002090
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 /* We need the data buffer to be aligned properly. We will reserve
2092 * as many bytes as needed to align the data properly
2093 */
Andy Fleming7f7f5312005-11-11 12:38:59 -06002094 skb_reserve(skb, alignamount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 return skb;
2097}
2098
Li Yang298e1a92007-10-16 14:18:13 +08002099static inline void count_errors(unsigned short status, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100{
Li Yang298e1a92007-10-16 14:18:13 +08002101 struct gfar_private *priv = netdev_priv(dev);
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002102 struct net_device_stats *stats = &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 struct gfar_extra_stats *estats = &priv->extra_stats;
2104
2105 /* If the packet was truncated, none of the other errors
2106 * matter */
2107 if (status & RXBD_TRUNCATED) {
2108 stats->rx_length_errors++;
2109
2110 estats->rx_trunc++;
2111
2112 return;
2113 }
2114 /* Count the errors, if there were any */
2115 if (status & (RXBD_LARGE | RXBD_SHORT)) {
2116 stats->rx_length_errors++;
2117
2118 if (status & RXBD_LARGE)
2119 estats->rx_large++;
2120 else
2121 estats->rx_short++;
2122 }
2123 if (status & RXBD_NONOCTET) {
2124 stats->rx_frame_errors++;
2125 estats->rx_nonoctet++;
2126 }
2127 if (status & RXBD_CRCERR) {
2128 estats->rx_crcerr++;
2129 stats->rx_crc_errors++;
2130 }
2131 if (status & RXBD_OVERRUN) {
2132 estats->rx_overrun++;
2133 stats->rx_crc_errors++;
2134 }
2135}
2136
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002137irqreturn_t gfar_receive(int irq, void *grp_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138{
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002139 gfar_schedule_cleanup((struct gfar_priv_grp *)grp_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 return IRQ_HANDLED;
2141}
2142
Kumar Gala0bbaf062005-06-20 10:54:21 -05002143static inline void gfar_rx_checksum(struct sk_buff *skb, struct rxfcb *fcb)
2144{
2145 /* If valid headers were found, and valid sums
2146 * were verified, then we tell the kernel that no
2147 * checksumming is necessary. Otherwise, it is */
Andy Fleming7f7f5312005-11-11 12:38:59 -06002148 if ((fcb->flags & RXFCB_CSUM_MASK) == (RXFCB_CIP | RXFCB_CTU))
Kumar Gala0bbaf062005-06-20 10:54:21 -05002149 skb->ip_summed = CHECKSUM_UNNECESSARY;
2150 else
2151 skb->ip_summed = CHECKSUM_NONE;
2152}
2153
2154
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155/* gfar_process_frame() -- handle one incoming packet if skb
2156 * isn't NULL. */
2157static int gfar_process_frame(struct net_device *dev, struct sk_buff *skb,
Dai Haruki2c2db482008-12-16 15:31:15 -08002158 int amount_pull)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159{
2160 struct gfar_private *priv = netdev_priv(dev);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002161 struct rxfcb *fcb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162
Dai Haruki2c2db482008-12-16 15:31:15 -08002163 int ret;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002164
Dai Haruki2c2db482008-12-16 15:31:15 -08002165 /* fcb is at the beginning if exists */
2166 fcb = (struct rxfcb *)skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167
Dai Haruki2c2db482008-12-16 15:31:15 -08002168 /* Remove the FCB from the skb */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002169 skb_set_queue_mapping(skb, fcb->rq);
Dai Haruki2c2db482008-12-16 15:31:15 -08002170 /* Remove the padded bytes, if there are any */
2171 if (amount_pull)
2172 skb_pull(skb, amount_pull);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002173
Dai Haruki2c2db482008-12-16 15:31:15 -08002174 if (priv->rx_csum_enable)
2175 gfar_rx_checksum(skb, fcb);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002176
Dai Haruki2c2db482008-12-16 15:31:15 -08002177 /* Tell the skb what kind of packet this is */
2178 skb->protocol = eth_type_trans(skb, dev);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002179
Dai Haruki2c2db482008-12-16 15:31:15 -08002180 /* Send the packet up the stack */
2181 if (unlikely(priv->vlgrp && (fcb->flags & RXFCB_VLN)))
2182 ret = vlan_hwaccel_receive_skb(skb, priv->vlgrp, fcb->vlctl);
2183 else
2184 ret = netif_receive_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185
Dai Haruki2c2db482008-12-16 15:31:15 -08002186 if (NET_RX_DROP == ret)
2187 priv->extra_stats.kernel_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188
2189 return 0;
2190}
2191
2192/* gfar_clean_rx_ring() -- Processes each frame in the rx ring
Kumar Gala0bbaf062005-06-20 10:54:21 -05002193 * until the budget/quota has been reached. Returns the number
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 * of frames handled
2195 */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002196int gfar_clean_rx_ring(struct gfar_priv_rx_q *rx_queue, int rx_work_limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197{
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002198 struct net_device *dev = rx_queue->dev;
Andy Fleming31de1982008-12-16 15:33:40 -08002199 struct rxbd8 *bdp, *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 struct sk_buff *skb;
Dai Haruki2c2db482008-12-16 15:31:15 -08002201 int pkt_len;
2202 int amount_pull;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 int howmany = 0;
2204 struct gfar_private *priv = netdev_priv(dev);
2205
2206 /* Get the first full descriptor */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002207 bdp = rx_queue->cur_rx;
2208 base = rx_queue->rx_bd_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
Dai Haruki2c2db482008-12-16 15:31:15 -08002210 amount_pull = (gfar_uses_fcb(priv) ? GMAC_FCB_LEN : 0) +
2211 priv->padding;
2212
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 while (!((bdp->status & RXBD_EMPTY) || (--rx_work_limit < 0))) {
Andy Fleming815b97c2008-04-22 17:18:29 -05002214 struct sk_buff *newskb;
Scott Wood3b6330c2007-05-16 15:06:59 -05002215 rmb();
Andy Fleming815b97c2008-04-22 17:18:29 -05002216
2217 /* Add another skb for the future */
2218 newskb = gfar_new_skb(dev);
2219
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002220 skb = rx_queue->rx_skbuff[rx_queue->skb_currx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221
Kumar Gala48268572009-03-18 23:28:22 -07002222 dma_unmap_single(&priv->ofdev->dev, bdp->bufPtr,
Andy Fleming81183052008-11-12 10:07:11 -06002223 priv->rx_buffer_size, DMA_FROM_DEVICE);
2224
Andy Fleming815b97c2008-04-22 17:18:29 -05002225 /* We drop the frame if we failed to allocate a new buffer */
2226 if (unlikely(!newskb || !(bdp->status & RXBD_LAST) ||
2227 bdp->status & RXBD_ERR)) {
2228 count_errors(bdp->status, dev);
2229
2230 if (unlikely(!newskb))
2231 newskb = skb;
Lennert Buytenhek4e2fd552009-05-25 00:42:34 -07002232 else if (skb) {
2233 /*
2234 * We need to reset ->data to what it
2235 * was before gfar_new_skb() re-aligned
2236 * it to an RXBUF_ALIGNMENT boundary
2237 * before we put the skb back on the
2238 * recycle list.
2239 */
2240 skb->data = skb->head + NET_SKB_PAD;
Andy Fleming0fd56bb2009-02-04 16:43:16 -08002241 __skb_queue_head(&priv->rx_recycle, skb);
Lennert Buytenhek4e2fd552009-05-25 00:42:34 -07002242 }
Andy Fleming815b97c2008-04-22 17:18:29 -05002243 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 /* Increment the number of packets */
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002245 dev->stats.rx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 howmany++;
2247
Dai Haruki2c2db482008-12-16 15:31:15 -08002248 if (likely(skb)) {
2249 pkt_len = bdp->length - ETH_FCS_LEN;
2250 /* Remove the FCS from the packet length */
2251 skb_put(skb, pkt_len);
2252 dev->stats.rx_bytes += pkt_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253
Andy Fleming1577ece2009-02-04 16:42:12 -08002254 if (in_irq() || irqs_disabled())
2255 printk("Interrupt problem!\n");
Dai Haruki2c2db482008-12-16 15:31:15 -08002256 gfar_process_frame(dev, skb, amount_pull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257
Dai Haruki2c2db482008-12-16 15:31:15 -08002258 } else {
2259 if (netif_msg_rx_err(priv))
2260 printk(KERN_WARNING
2261 "%s: Missing skb!\n", dev->name);
2262 dev->stats.rx_dropped++;
2263 priv->extra_stats.rx_skbmissing++;
2264 }
2265
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 }
2267
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002268 rx_queue->rx_skbuff[rx_queue->skb_currx] = newskb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269
Andy Fleming815b97c2008-04-22 17:18:29 -05002270 /* Setup the new bdp */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002271 gfar_new_rxbdp(rx_queue, bdp, newskb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272
2273 /* Update to the next pointer */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002274 bdp = next_bd(bdp, base, rx_queue->rx_ring_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275
2276 /* update to point at the next skb */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002277 rx_queue->skb_currx =
2278 (rx_queue->skb_currx + 1) &
2279 RX_RING_MOD_MASK(rx_queue->rx_ring_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 }
2281
2282 /* Update the current rxbd pointer to be the next one */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002283 rx_queue->cur_rx = bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 return howmany;
2286}
2287
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002288static int gfar_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289{
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002290 struct gfar_priv_grp *gfargrp = container_of(napi,
2291 struct gfar_priv_grp, napi);
2292 struct gfar_private *priv = gfargrp->priv;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002293 struct gfar __iomem *regs = priv->gfargrp.regs;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002294 struct gfar_priv_tx_q *tx_queue = NULL;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002295 struct gfar_priv_rx_q *rx_queue = NULL;
2296 int rx_cleaned = 0, budget_per_queue = 0, rx_cleaned_per_queue = 0;
2297 int tx_cleaned = 0, i, left_over_budget = budget, serviced_queues = 0;
2298 int num_queues = 0;
Dai Harukid080cd62008-04-09 19:37:51 -05002299 unsigned long flags;
2300
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002301 num_queues = gfargrp->num_rx_queues;
2302 budget_per_queue = budget/num_queues;
2303
Dai Haruki8c7396a2008-12-17 16:52:00 -08002304 /* Clear IEVENT, so interrupts aren't called again
2305 * because of the packets that have already arrived */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002306 gfar_write(&regs->ievent, IEVENT_RTX_MASK);
Dai Haruki8c7396a2008-12-17 16:52:00 -08002307
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002308 while (num_queues && left_over_budget) {
2309
2310 budget_per_queue = left_over_budget/num_queues;
2311 left_over_budget = 0;
2312
2313 for_each_bit(i, &gfargrp->rx_bit_map, priv->num_rx_queues) {
2314 if (test_bit(i, &serviced_queues))
2315 continue;
2316 rx_queue = priv->rx_queue[i];
2317 tx_queue = priv->tx_queue[rx_queue->qindex];
2318
2319 /* If we fail to get the lock,
2320 * don't bother with the TX BDs */
2321 if (spin_trylock_irqsave(&tx_queue->txlock, flags)) {
2322 tx_cleaned += gfar_clean_tx_ring(tx_queue);
2323 spin_unlock_irqrestore(&tx_queue->txlock,
2324 flags);
2325 }
2326
2327 rx_cleaned_per_queue = gfar_clean_rx_ring(rx_queue,
2328 budget_per_queue);
2329 rx_cleaned += rx_cleaned_per_queue;
2330 if(rx_cleaned_per_queue < budget_per_queue) {
2331 left_over_budget = left_over_budget +
2332 (budget_per_queue - rx_cleaned_per_queue);
2333 set_bit(i, &serviced_queues);
2334 num_queues--;
2335 }
2336 }
Dai Harukid080cd62008-04-09 19:37:51 -05002337 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338
Andy Fleming42199882008-12-17 16:52:30 -08002339 if (tx_cleaned)
2340 return budget;
2341
2342 if (rx_cleaned < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -08002343 napi_complete(napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344
2345 /* Clear the halt bit in RSTAT */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002346 gfar_write(&regs->rstat, gfargrp->rstat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002348 gfar_write(&regs->imask, IMASK_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349
2350 /* If we are coalescing interrupts, update the timer */
2351 /* Otherwise, clear it */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002352 if (likely(rx_queue->rxcoalescing)) {
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002353 gfar_write(&regs->rxic, 0);
2354 gfar_write(&regs->rxic, rx_queue->rxic);
Andy Fleming2f448912008-03-24 10:53:28 -05002355 }
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002356 if (likely(tx_queue->txcoalescing)) {
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002357 gfar_write(&regs->txic, 0);
2358 gfar_write(&regs->txic, tx_queue->txic);
Dai Haruki8c7396a2008-12-17 16:52:00 -08002359 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 }
2361
Andy Fleming42199882008-12-17 16:52:30 -08002362 return rx_cleaned;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364
Vitaly Woolf2d71c22006-11-07 13:27:02 +03002365#ifdef CONFIG_NET_POLL_CONTROLLER
2366/*
2367 * Polling 'interrupt' - used by things like netconsole to send skbs
2368 * without having to re-enable interrupts. It's not called while
2369 * the interrupt routine is executing.
2370 */
2371static void gfar_netpoll(struct net_device *dev)
2372{
2373 struct gfar_private *priv = netdev_priv(dev);
2374
2375 /* If the device has multiple interrupts, run tx/rx */
Andy Flemingb31a1d82008-12-16 15:29:15 -08002376 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002377 disable_irq(priv->gfargrp.interruptTransmit);
2378 disable_irq(priv->gfargrp.interruptReceive);
2379 disable_irq(priv->gfargrp.interruptError);
2380 gfar_interrupt(priv->gfargrp.interruptTransmit, &priv->gfargrp);
2381 enable_irq(priv->gfargrp.interruptError);
2382 enable_irq(priv->gfargrp.interruptReceive);
2383 enable_irq(priv->gfargrp.interruptTransmit);
Vitaly Woolf2d71c22006-11-07 13:27:02 +03002384 } else {
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002385 disable_irq(priv->gfargrp.interruptTransmit);
2386 gfar_interrupt(priv->gfargrp.interruptTransmit, &priv->gfargrp);
2387 enable_irq(priv->gfargrp.interruptTransmit);
Vitaly Woolf2d71c22006-11-07 13:27:02 +03002388 }
2389}
2390#endif
2391
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392/* The interrupt handler for devices with one interrupt */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002393static irqreturn_t gfar_interrupt(int irq, void *grp_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394{
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002395 struct gfar_priv_grp *gfargrp = grp_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396
2397 /* Save ievent for future reference */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002398 u32 events = gfar_read(&gfargrp->regs->ievent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 /* Check for reception */
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002401 if (events & IEVENT_RX_MASK)
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002402 gfar_receive(irq, grp_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403
2404 /* Check for transmit completion */
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002405 if (events & IEVENT_TX_MASK)
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002406 gfar_transmit(irq, grp_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002408 /* Check for errors */
2409 if (events & IEVENT_ERR_MASK)
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002410 gfar_error(irq, grp_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411
2412 return IRQ_HANDLED;
2413}
2414
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415/* Called every time the controller might need to be made
2416 * aware of new link state. The PHY code conveys this
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002417 * information through variables in the phydev structure, and this
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 * function converts those variables into the appropriate
2419 * register values, and can bring down the device if needed.
2420 */
2421static void adjust_link(struct net_device *dev)
2422{
2423 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002424 struct gfar __iomem *regs = priv->gfargrp.regs;
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002425 unsigned long flags;
2426 struct phy_device *phydev = priv->phydev;
2427 int new_state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002429 local_irq_save(flags);
2430 lock_tx_qs(priv);
2431
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002432 if (phydev->link) {
2433 u32 tempval = gfar_read(&regs->maccfg2);
Andy Fleming7f7f5312005-11-11 12:38:59 -06002434 u32 ecntrl = gfar_read(&regs->ecntrl);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002435
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 /* Now we make sure that we can be in full duplex mode.
2437 * If not, we operate in half-duplex mode. */
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002438 if (phydev->duplex != priv->oldduplex) {
2439 new_state = 1;
2440 if (!(phydev->duplex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 tempval &= ~(MACCFG2_FULL_DUPLEX);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002442 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 tempval |= MACCFG2_FULL_DUPLEX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002445 priv->oldduplex = phydev->duplex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 }
2447
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002448 if (phydev->speed != priv->oldspeed) {
2449 new_state = 1;
2450 switch (phydev->speed) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 case 1000:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 tempval =
2453 ((tempval & ~(MACCFG2_IF)) | MACCFG2_GMII);
Li Yangf430e492009-01-06 14:08:10 -08002454
2455 ecntrl &= ~(ECNTRL_R100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 break;
2457 case 100:
2458 case 10:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 tempval =
2460 ((tempval & ~(MACCFG2_IF)) | MACCFG2_MII);
Andy Fleming7f7f5312005-11-11 12:38:59 -06002461
2462 /* Reduced mode distinguishes
2463 * between 10 and 100 */
2464 if (phydev->speed == SPEED_100)
2465 ecntrl |= ECNTRL_R100;
2466 else
2467 ecntrl &= ~(ECNTRL_R100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 break;
2469 default:
Kumar Gala0bbaf062005-06-20 10:54:21 -05002470 if (netif_msg_link(priv))
2471 printk(KERN_WARNING
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002472 "%s: Ack! Speed (%d) is not 10/100/1000!\n",
2473 dev->name, phydev->speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 break;
2475 }
2476
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002477 priv->oldspeed = phydev->speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 }
2479
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002480 gfar_write(&regs->maccfg2, tempval);
Andy Fleming7f7f5312005-11-11 12:38:59 -06002481 gfar_write(&regs->ecntrl, ecntrl);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002482
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 if (!priv->oldlink) {
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002484 new_state = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 priv->oldlink = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 }
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002487 } else if (priv->oldlink) {
2488 new_state = 1;
2489 priv->oldlink = 0;
2490 priv->oldspeed = 0;
2491 priv->oldduplex = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002494 if (new_state && netif_msg_link(priv))
2495 phy_print_status(phydev);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002496 unlock_tx_qs(priv);
2497 local_irq_restore(flags);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002498}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499
2500/* Update the hash table based on the current list of multicast
2501 * addresses we subscribe to. Also, change the promiscuity of
2502 * the device based on the flags (this function is called
2503 * whenever dev->flags is changed */
2504static void gfar_set_multi(struct net_device *dev)
2505{
2506 struct dev_mc_list *mc_ptr;
2507 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002508 struct gfar __iomem *regs = priv->gfargrp.regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 u32 tempval;
2510
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002511 if (dev->flags & IFF_PROMISC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 /* Set RCTRL to PROM */
2513 tempval = gfar_read(&regs->rctrl);
2514 tempval |= RCTRL_PROM;
2515 gfar_write(&regs->rctrl, tempval);
2516 } else {
2517 /* Set RCTRL to not PROM */
2518 tempval = gfar_read(&regs->rctrl);
2519 tempval &= ~(RCTRL_PROM);
2520 gfar_write(&regs->rctrl, tempval);
2521 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002522
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002523 if (dev->flags & IFF_ALLMULTI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 /* Set the hash to rx all multicast frames */
Kumar Gala0bbaf062005-06-20 10:54:21 -05002525 gfar_write(&regs->igaddr0, 0xffffffff);
2526 gfar_write(&regs->igaddr1, 0xffffffff);
2527 gfar_write(&regs->igaddr2, 0xffffffff);
2528 gfar_write(&regs->igaddr3, 0xffffffff);
2529 gfar_write(&regs->igaddr4, 0xffffffff);
2530 gfar_write(&regs->igaddr5, 0xffffffff);
2531 gfar_write(&regs->igaddr6, 0xffffffff);
2532 gfar_write(&regs->igaddr7, 0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 gfar_write(&regs->gaddr0, 0xffffffff);
2534 gfar_write(&regs->gaddr1, 0xffffffff);
2535 gfar_write(&regs->gaddr2, 0xffffffff);
2536 gfar_write(&regs->gaddr3, 0xffffffff);
2537 gfar_write(&regs->gaddr4, 0xffffffff);
2538 gfar_write(&regs->gaddr5, 0xffffffff);
2539 gfar_write(&regs->gaddr6, 0xffffffff);
2540 gfar_write(&regs->gaddr7, 0xffffffff);
2541 } else {
Andy Fleming7f7f5312005-11-11 12:38:59 -06002542 int em_num;
2543 int idx;
2544
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 /* zero out the hash */
Kumar Gala0bbaf062005-06-20 10:54:21 -05002546 gfar_write(&regs->igaddr0, 0x0);
2547 gfar_write(&regs->igaddr1, 0x0);
2548 gfar_write(&regs->igaddr2, 0x0);
2549 gfar_write(&regs->igaddr3, 0x0);
2550 gfar_write(&regs->igaddr4, 0x0);
2551 gfar_write(&regs->igaddr5, 0x0);
2552 gfar_write(&regs->igaddr6, 0x0);
2553 gfar_write(&regs->igaddr7, 0x0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 gfar_write(&regs->gaddr0, 0x0);
2555 gfar_write(&regs->gaddr1, 0x0);
2556 gfar_write(&regs->gaddr2, 0x0);
2557 gfar_write(&regs->gaddr3, 0x0);
2558 gfar_write(&regs->gaddr4, 0x0);
2559 gfar_write(&regs->gaddr5, 0x0);
2560 gfar_write(&regs->gaddr6, 0x0);
2561 gfar_write(&regs->gaddr7, 0x0);
2562
Andy Fleming7f7f5312005-11-11 12:38:59 -06002563 /* If we have extended hash tables, we need to
2564 * clear the exact match registers to prepare for
2565 * setting them */
2566 if (priv->extended_hash) {
2567 em_num = GFAR_EM_NUM + 1;
2568 gfar_clear_exact_match(dev);
2569 idx = 1;
2570 } else {
2571 idx = 0;
2572 em_num = 0;
2573 }
2574
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002575 if (dev->mc_count == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 return;
2577
2578 /* Parse the list, and set the appropriate bits */
2579 for(mc_ptr = dev->mc_list; mc_ptr; mc_ptr = mc_ptr->next) {
Andy Fleming7f7f5312005-11-11 12:38:59 -06002580 if (idx < em_num) {
2581 gfar_set_mac_for_addr(dev, idx,
2582 mc_ptr->dmi_addr);
2583 idx++;
2584 } else
2585 gfar_set_hash_for_addr(dev, mc_ptr->dmi_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 }
2587 }
2588
2589 return;
2590}
2591
Andy Fleming7f7f5312005-11-11 12:38:59 -06002592
2593/* Clears each of the exact match registers to zero, so they
2594 * don't interfere with normal reception */
2595static void gfar_clear_exact_match(struct net_device *dev)
2596{
2597 int idx;
2598 u8 zero_arr[MAC_ADDR_LEN] = {0,0,0,0,0,0};
2599
2600 for(idx = 1;idx < GFAR_EM_NUM + 1;idx++)
2601 gfar_set_mac_for_addr(dev, idx, (u8 *)zero_arr);
2602}
2603
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604/* Set the appropriate hash bit for the given addr */
2605/* The algorithm works like so:
2606 * 1) Take the Destination Address (ie the multicast address), and
2607 * do a CRC on it (little endian), and reverse the bits of the
2608 * result.
2609 * 2) Use the 8 most significant bits as a hash into a 256-entry
2610 * table. The table is controlled through 8 32-bit registers:
2611 * gaddr0-7. gaddr0's MSB is entry 0, and gaddr7's LSB is
2612 * gaddr7. This means that the 3 most significant bits in the
2613 * hash index which gaddr register to use, and the 5 other bits
2614 * indicate which bit (assuming an IBM numbering scheme, which
2615 * for PowerPC (tm) is usually the case) in the register holds
2616 * the entry. */
2617static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr)
2618{
2619 u32 tempval;
2620 struct gfar_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 u32 result = ether_crc(MAC_ADDR_LEN, addr);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002622 int width = priv->hash_width;
2623 u8 whichbit = (result >> (32 - width)) & 0x1f;
2624 u8 whichreg = result >> (32 - width + 5);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 u32 value = (1 << (31-whichbit));
2626
Kumar Gala0bbaf062005-06-20 10:54:21 -05002627 tempval = gfar_read(priv->hash_regs[whichreg]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 tempval |= value;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002629 gfar_write(priv->hash_regs[whichreg], tempval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630
2631 return;
2632}
2633
Andy Fleming7f7f5312005-11-11 12:38:59 -06002634
2635/* There are multiple MAC Address register pairs on some controllers
2636 * This function sets the numth pair to a given address
2637 */
2638static void gfar_set_mac_for_addr(struct net_device *dev, int num, u8 *addr)
2639{
2640 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002641 struct gfar __iomem *regs = priv->gfargrp.regs;
Andy Fleming7f7f5312005-11-11 12:38:59 -06002642 int idx;
2643 char tmpbuf[MAC_ADDR_LEN];
2644 u32 tempval;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002645 u32 __iomem *macptr = &regs->macstnaddr1;
Andy Fleming7f7f5312005-11-11 12:38:59 -06002646
2647 macptr += num*2;
2648
2649 /* Now copy it into the mac registers backwards, cuz */
2650 /* little endian is silly */
2651 for (idx = 0; idx < MAC_ADDR_LEN; idx++)
2652 tmpbuf[MAC_ADDR_LEN - 1 - idx] = addr[idx];
2653
2654 gfar_write(macptr, *((u32 *) (tmpbuf)));
2655
2656 tempval = *((u32 *) (tmpbuf + 4));
2657
2658 gfar_write(macptr+1, tempval);
2659}
2660
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661/* GFAR error interrupt handler */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002662static irqreturn_t gfar_error(int irq, void *grp_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663{
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002664 struct gfar_priv_grp *gfargrp = grp_id;
2665 struct gfar __iomem *regs = gfargrp->regs;
2666 struct gfar_private *priv= gfargrp->priv;
2667 struct net_device *dev = priv->ndev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668
2669 /* Save ievent for future reference */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002670 u32 events = gfar_read(&regs->ievent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671
2672 /* Clear IEVENT */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002673 gfar_write(&regs->ievent, events & IEVENT_ERR_MASK);
Scott Woodd87eb122008-07-11 18:04:45 -05002674
2675 /* Magic Packet is not an error. */
Andy Flemingb31a1d82008-12-16 15:29:15 -08002676 if ((priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET) &&
Scott Woodd87eb122008-07-11 18:04:45 -05002677 (events & IEVENT_MAG))
2678 events &= ~IEVENT_MAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679
2680 /* Hmm... */
Kumar Gala0bbaf062005-06-20 10:54:21 -05002681 if (netif_msg_rx_err(priv) || netif_msg_tx_err(priv))
2682 printk(KERN_DEBUG "%s: error interrupt (ievent=0x%08x imask=0x%08x)\n",
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002683 dev->name, events, gfar_read(&regs->imask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684
2685 /* Update the error counters */
2686 if (events & IEVENT_TXE) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002687 dev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688
2689 if (events & IEVENT_LC)
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002690 dev->stats.tx_window_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 if (events & IEVENT_CRL)
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002692 dev->stats.tx_aborted_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 if (events & IEVENT_XFUN) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05002694 if (netif_msg_tx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002695 printk(KERN_DEBUG "%s: TX FIFO underrun, "
2696 "packet dropped.\n", dev->name);
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002697 dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 priv->extra_stats.tx_underrun++;
2699
2700 /* Reactivate the Tx Queues */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002701 gfar_write(&regs->tstat, gfargrp->tstat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 }
Kumar Gala0bbaf062005-06-20 10:54:21 -05002703 if (netif_msg_tx_err(priv))
2704 printk(KERN_DEBUG "%s: Transmit Error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 }
2706 if (events & IEVENT_BSY) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002707 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 priv->extra_stats.rx_bsy++;
2709
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002710 gfar_receive(irq, grp_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711
Kumar Gala0bbaf062005-06-20 10:54:21 -05002712 if (netif_msg_rx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002713 printk(KERN_DEBUG "%s: busy error (rstat: %x)\n",
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002714 dev->name, gfar_read(&regs->rstat));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 }
2716 if (events & IEVENT_BABR) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002717 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 priv->extra_stats.rx_babr++;
2719
Kumar Gala0bbaf062005-06-20 10:54:21 -05002720 if (netif_msg_rx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002721 printk(KERN_DEBUG "%s: babbling RX error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 }
2723 if (events & IEVENT_EBERR) {
2724 priv->extra_stats.eberr++;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002725 if (netif_msg_rx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002726 printk(KERN_DEBUG "%s: bus error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 }
Kumar Gala0bbaf062005-06-20 10:54:21 -05002728 if ((events & IEVENT_RXC) && netif_msg_rx_status(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002729 printk(KERN_DEBUG "%s: control frame\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730
2731 if (events & IEVENT_BABT) {
2732 priv->extra_stats.tx_babt++;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002733 if (netif_msg_tx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002734 printk(KERN_DEBUG "%s: babbling TX error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 }
2736 return IRQ_HANDLED;
2737}
2738
Andy Flemingb31a1d82008-12-16 15:29:15 -08002739static struct of_device_id gfar_match[] =
2740{
2741 {
2742 .type = "network",
2743 .compatible = "gianfar",
2744 },
2745 {},
2746};
Anton Vorontsove72701a2009-10-14 14:54:52 -07002747MODULE_DEVICE_TABLE(of, gfar_match);
Andy Flemingb31a1d82008-12-16 15:29:15 -08002748
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749/* Structure for a device driver */
Andy Flemingb31a1d82008-12-16 15:29:15 -08002750static struct of_platform_driver gfar_driver = {
2751 .name = "fsl-gianfar",
2752 .match_table = gfar_match,
2753
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 .probe = gfar_probe,
2755 .remove = gfar_remove,
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00002756 .suspend = gfar_legacy_suspend,
2757 .resume = gfar_legacy_resume,
2758 .driver.pm = GFAR_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759};
2760
2761static int __init gfar_init(void)
2762{
Andy Fleming1577ece2009-02-04 16:42:12 -08002763 return of_register_platform_driver(&gfar_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764}
2765
2766static void __exit gfar_exit(void)
2767{
Andy Flemingb31a1d82008-12-16 15:29:15 -08002768 of_unregister_platform_driver(&gfar_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769}
2770
2771module_init(gfar_init);
2772module_exit(gfar_exit);
2773