blob: dc9fba09b17cac22657ed07172908041fe865e5b [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{
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000329 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000330 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 Gopalpet46ceb602009-11-02 07:03:34 +0000349 struct gfar __iomem *regs = priv->gfargrp[0].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 */
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000358 gfar_configure_coalescing(priv, 0xFF, 0xFF);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000359
360 if (priv->rx_filer_enable)
361 rctrl |= RCTRL_FILREN;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000362
363 if (priv->rx_csum_enable)
364 rctrl |= RCTRL_CHECKSUMMING;
365
366 if (priv->extended_hash) {
367 rctrl |= RCTRL_EXTHASH;
368
369 gfar_clear_exact_match(ndev);
370 rctrl |= RCTRL_EMEN;
371 }
372
373 if (priv->padding) {
374 rctrl &= ~RCTRL_PAL_MASK;
375 rctrl |= RCTRL_PADDING(priv->padding);
376 }
377
378 /* keep vlan related bits if it's enabled */
379 if (priv->vlgrp) {
380 rctrl |= RCTRL_VLEX | RCTRL_PRSDEP_INIT;
381 tctrl |= TCTRL_VLINS;
382 }
383
384 /* Init rctrl based on our settings */
385 gfar_write(&regs->rctrl, rctrl);
386
387 if (ndev->features & NETIF_F_IP_CSUM)
388 tctrl |= TCTRL_INIT_CSUM;
389
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000390 tctrl |= TCTRL_TXSCHED_PRIO;
391
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000392 gfar_write(&regs->tctrl, tctrl);
393
394 /* Set the extraction length and index */
395 attrs = ATTRELI_EL(priv->rx_stash_size) |
396 ATTRELI_EI(priv->rx_stash_index);
397
398 gfar_write(&regs->attreli, attrs);
399
400 /* Start with defaults, and add stashing or locking
401 * depending on the approprate variables */
402 attrs = ATTR_INIT_SETTINGS;
403
404 if (priv->bd_stash_en)
405 attrs |= ATTR_BDSTASH;
406
407 if (priv->rx_stash_size != 0)
408 attrs |= ATTR_BUFSTASH;
409
410 gfar_write(&regs->attr, attrs);
411
412 gfar_write(&regs->fifo_tx_thr, priv->fifo_threshold);
413 gfar_write(&regs->fifo_tx_starve, priv->fifo_starve);
414 gfar_write(&regs->fifo_tx_starve_shutoff, priv->fifo_starve_off);
415}
416
Andy Fleming26ccfc32009-03-10 12:58:28 +0000417static const struct net_device_ops gfar_netdev_ops = {
418 .ndo_open = gfar_enet_open,
419 .ndo_start_xmit = gfar_start_xmit,
420 .ndo_stop = gfar_close,
421 .ndo_change_mtu = gfar_change_mtu,
422 .ndo_set_multicast_list = gfar_set_multi,
423 .ndo_tx_timeout = gfar_timeout,
424 .ndo_do_ioctl = gfar_ioctl,
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000425 .ndo_select_queue = gfar_select_queue,
Andy Fleming26ccfc32009-03-10 12:58:28 +0000426 .ndo_vlan_rx_register = gfar_vlan_rx_register,
Ben Hutchings240c1022009-07-09 17:54:35 +0000427 .ndo_set_mac_address = eth_mac_addr,
428 .ndo_validate_addr = eth_validate_addr,
Andy Fleming26ccfc32009-03-10 12:58:28 +0000429#ifdef CONFIG_NET_POLL_CONTROLLER
430 .ndo_poll_controller = gfar_netpoll,
431#endif
432};
433
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000434void lock_rx_qs(struct gfar_private *priv)
435{
436 int i = 0x0;
437
438 for (i = 0; i < priv->num_rx_queues; i++)
439 spin_lock(&priv->rx_queue[i]->rxlock);
440}
441
442void lock_tx_qs(struct gfar_private *priv)
443{
444 int i = 0x0;
445
446 for (i = 0; i < priv->num_tx_queues; i++)
447 spin_lock(&priv->tx_queue[i]->txlock);
448}
449
450void unlock_rx_qs(struct gfar_private *priv)
451{
452 int i = 0x0;
453
454 for (i = 0; i < priv->num_rx_queues; i++)
455 spin_unlock(&priv->rx_queue[i]->rxlock);
456}
457
458void unlock_tx_qs(struct gfar_private *priv)
459{
460 int i = 0x0;
461
462 for (i = 0; i < priv->num_tx_queues; i++)
463 spin_unlock(&priv->tx_queue[i]->txlock);
464}
465
Andy Fleming7f7f5312005-11-11 12:38:59 -0600466/* Returns 1 if incoming frames use an FCB */
467static inline int gfar_uses_fcb(struct gfar_private *priv)
Kumar Gala0bbaf062005-06-20 10:54:21 -0500468{
Dai Haruki77ecaf22008-12-16 15:30:48 -0800469 return priv->vlgrp || priv->rx_csum_enable;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500470}
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400471
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000472u16 gfar_select_queue(struct net_device *dev, struct sk_buff *skb)
473{
474 return skb_get_queue_mapping(skb);
475}
476static void free_tx_pointers(struct gfar_private *priv)
477{
478 int i = 0;
479
480 for (i = 0; i < priv->num_tx_queues; i++)
481 kfree(priv->tx_queue[i]);
482}
483
484static void free_rx_pointers(struct gfar_private *priv)
485{
486 int i = 0;
487
488 for (i = 0; i < priv->num_rx_queues; i++)
489 kfree(priv->rx_queue[i]);
490}
491
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000492static void unmap_group_regs(struct gfar_private *priv)
493{
494 int i = 0;
495
496 for (i = 0; i < MAXGROUPS; i++)
497 if (priv->gfargrp[i].regs)
498 iounmap(priv->gfargrp[i].regs);
499}
500
501static void disable_napi(struct gfar_private *priv)
502{
503 int i = 0;
504
505 for (i = 0; i < priv->num_grps; i++)
506 napi_disable(&priv->gfargrp[i].napi);
507}
508
509static void enable_napi(struct gfar_private *priv)
510{
511 int i = 0;
512
513 for (i = 0; i < priv->num_grps; i++)
514 napi_enable(&priv->gfargrp[i].napi);
515}
516
517static int gfar_parse_group(struct device_node *np,
518 struct gfar_private *priv, const char *model)
519{
520 u32 *queue_mask;
521 u64 addr, size;
522
523 addr = of_translate_address(np,
524 of_get_address(np, 0, &size, NULL));
525 priv->gfargrp[priv->num_grps].regs = ioremap(addr, size);
526
527 if (!priv->gfargrp[priv->num_grps].regs)
528 return -ENOMEM;
529
530 priv->gfargrp[priv->num_grps].interruptTransmit =
531 irq_of_parse_and_map(np, 0);
532
533 /* If we aren't the FEC we have multiple interrupts */
534 if (model && strcasecmp(model, "FEC")) {
535 priv->gfargrp[priv->num_grps].interruptReceive =
536 irq_of_parse_and_map(np, 1);
537 priv->gfargrp[priv->num_grps].interruptError =
538 irq_of_parse_and_map(np,2);
539 if (priv->gfargrp[priv->num_grps].interruptTransmit < 0 ||
540 priv->gfargrp[priv->num_grps].interruptReceive < 0 ||
541 priv->gfargrp[priv->num_grps].interruptError < 0) {
542 return -EINVAL;
543 }
544 }
545
546 priv->gfargrp[priv->num_grps].grp_id = priv->num_grps;
547 priv->gfargrp[priv->num_grps].priv = priv;
548 spin_lock_init(&priv->gfargrp[priv->num_grps].grplock);
549 if(priv->mode == MQ_MG_MODE) {
550 queue_mask = (u32 *)of_get_property(np,
551 "fsl,rx-bit-map", NULL);
552 priv->gfargrp[priv->num_grps].rx_bit_map =
553 queue_mask ? *queue_mask :(DEFAULT_MAPPING >> priv->num_grps);
554 queue_mask = (u32 *)of_get_property(np,
555 "fsl,tx-bit-map", NULL);
556 priv->gfargrp[priv->num_grps].tx_bit_map =
557 queue_mask ? *queue_mask : (DEFAULT_MAPPING >> priv->num_grps);
558 } else {
559 priv->gfargrp[priv->num_grps].rx_bit_map = 0xFF;
560 priv->gfargrp[priv->num_grps].tx_bit_map = 0xFF;
561 }
562 priv->num_grps++;
563
564 return 0;
565}
566
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000567static int gfar_of_init(struct of_device *ofdev, struct net_device **pdev)
Andy Flemingb31a1d82008-12-16 15:29:15 -0800568{
Andy Flemingb31a1d82008-12-16 15:29:15 -0800569 const char *model;
570 const char *ctype;
571 const void *mac_addr;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000572 int err = 0, i;
573 struct net_device *dev = NULL;
574 struct gfar_private *priv = NULL;
575 struct device_node *np = ofdev->node;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000576 struct device_node *child = NULL;
Andy Fleming4d7902f2009-02-04 16:43:44 -0800577 const u32 *stash;
578 const u32 *stash_len;
579 const u32 *stash_idx;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000580 unsigned int num_tx_qs, num_rx_qs;
581 u32 *tx_queues, *rx_queues;
Andy Flemingb31a1d82008-12-16 15:29:15 -0800582
583 if (!np || !of_device_is_available(np))
584 return -ENODEV;
585
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000586 /* parse the num of tx and rx queues */
587 tx_queues = (u32 *)of_get_property(np, "fsl,num_tx_queues", NULL);
588 num_tx_qs = tx_queues ? *tx_queues : 1;
589
590 if (num_tx_qs > MAX_TX_QS) {
591 printk(KERN_ERR "num_tx_qs(=%d) greater than MAX_TX_QS(=%d)\n",
592 num_tx_qs, MAX_TX_QS);
593 printk(KERN_ERR "Cannot do alloc_etherdev, aborting\n");
594 return -EINVAL;
595 }
596
597 rx_queues = (u32 *)of_get_property(np, "fsl,num_rx_queues", NULL);
598 num_rx_qs = rx_queues ? *rx_queues : 1;
599
600 if (num_rx_qs > MAX_RX_QS) {
601 printk(KERN_ERR "num_rx_qs(=%d) greater than MAX_RX_QS(=%d)\n",
602 num_tx_qs, MAX_TX_QS);
603 printk(KERN_ERR "Cannot do alloc_etherdev, aborting\n");
604 return -EINVAL;
605 }
606
607 *pdev = alloc_etherdev_mq(sizeof(*priv), num_tx_qs);
608 dev = *pdev;
609 if (NULL == dev)
610 return -ENOMEM;
611
612 priv = netdev_priv(dev);
613 priv->node = ofdev->node;
614 priv->ndev = dev;
615
616 dev->num_tx_queues = num_tx_qs;
617 dev->real_num_tx_queues = num_tx_qs;
618 priv->num_tx_queues = num_tx_qs;
619 priv->num_rx_queues = num_rx_qs;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000620 priv->num_grps = 0x0;
Andy Flemingb31a1d82008-12-16 15:29:15 -0800621
622 model = of_get_property(np, "model", NULL);
623
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000624 for (i = 0; i < MAXGROUPS; i++)
625 priv->gfargrp[i].regs = NULL;
Andy Flemingb31a1d82008-12-16 15:29:15 -0800626
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000627 /* Parse and initialize group specific information */
628 if (of_device_is_compatible(np, "fsl,etsec2")) {
629 priv->mode = MQ_MG_MODE;
630 for_each_child_of_node(np, child) {
631 err = gfar_parse_group(child, priv, model);
632 if (err)
633 goto err_grp_init;
Andy Flemingb31a1d82008-12-16 15:29:15 -0800634 }
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000635 } else {
636 priv->mode = SQ_SG_MODE;
637 err = gfar_parse_group(np, priv, model);
638 if(err)
639 goto err_grp_init;
Andy Flemingb31a1d82008-12-16 15:29:15 -0800640 }
641
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000642 for (i = 0; i < priv->num_tx_queues; i++)
643 priv->tx_queue[i] = NULL;
644 for (i = 0; i < priv->num_rx_queues; i++)
645 priv->rx_queue[i] = NULL;
646
647 for (i = 0; i < priv->num_tx_queues; i++) {
648 priv->tx_queue[i] = (struct gfar_priv_tx_q *)kmalloc(
649 sizeof (struct gfar_priv_tx_q), GFP_KERNEL);
650 if (!priv->tx_queue[i]) {
651 err = -ENOMEM;
652 goto tx_alloc_failed;
653 }
654 priv->tx_queue[i]->tx_skbuff = NULL;
655 priv->tx_queue[i]->qindex = i;
656 priv->tx_queue[i]->dev = dev;
657 spin_lock_init(&(priv->tx_queue[i]->txlock));
658 }
659
660 for (i = 0; i < priv->num_rx_queues; i++) {
661 priv->rx_queue[i] = (struct gfar_priv_rx_q *)kmalloc(
662 sizeof (struct gfar_priv_rx_q), GFP_KERNEL);
663 if (!priv->rx_queue[i]) {
664 err = -ENOMEM;
665 goto rx_alloc_failed;
666 }
667 priv->rx_queue[i]->rx_skbuff = NULL;
668 priv->rx_queue[i]->qindex = i;
669 priv->rx_queue[i]->dev = dev;
670 spin_lock_init(&(priv->rx_queue[i]->rxlock));
671 }
672
673
Andy Fleming4d7902f2009-02-04 16:43:44 -0800674 stash = of_get_property(np, "bd-stash", NULL);
675
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000676 if (stash) {
Andy Fleming4d7902f2009-02-04 16:43:44 -0800677 priv->device_flags |= FSL_GIANFAR_DEV_HAS_BD_STASHING;
678 priv->bd_stash_en = 1;
679 }
680
681 stash_len = of_get_property(np, "rx-stash-len", NULL);
682
683 if (stash_len)
684 priv->rx_stash_size = *stash_len;
685
686 stash_idx = of_get_property(np, "rx-stash-idx", NULL);
687
688 if (stash_idx)
689 priv->rx_stash_index = *stash_idx;
690
691 if (stash_len || stash_idx)
692 priv->device_flags |= FSL_GIANFAR_DEV_HAS_BUF_STASHING;
693
Andy Flemingb31a1d82008-12-16 15:29:15 -0800694 mac_addr = of_get_mac_address(np);
695 if (mac_addr)
696 memcpy(dev->dev_addr, mac_addr, MAC_ADDR_LEN);
697
698 if (model && !strcasecmp(model, "TSEC"))
699 priv->device_flags =
700 FSL_GIANFAR_DEV_HAS_GIGABIT |
701 FSL_GIANFAR_DEV_HAS_COALESCE |
702 FSL_GIANFAR_DEV_HAS_RMON |
703 FSL_GIANFAR_DEV_HAS_MULTI_INTR;
704 if (model && !strcasecmp(model, "eTSEC"))
705 priv->device_flags =
706 FSL_GIANFAR_DEV_HAS_GIGABIT |
707 FSL_GIANFAR_DEV_HAS_COALESCE |
708 FSL_GIANFAR_DEV_HAS_RMON |
709 FSL_GIANFAR_DEV_HAS_MULTI_INTR |
Dai Haruki2c2db482008-12-16 15:31:15 -0800710 FSL_GIANFAR_DEV_HAS_PADDING |
Andy Flemingb31a1d82008-12-16 15:29:15 -0800711 FSL_GIANFAR_DEV_HAS_CSUM |
712 FSL_GIANFAR_DEV_HAS_VLAN |
713 FSL_GIANFAR_DEV_HAS_MAGIC_PACKET |
714 FSL_GIANFAR_DEV_HAS_EXTENDED_HASH;
715
716 ctype = of_get_property(np, "phy-connection-type", NULL);
717
718 /* We only care about rgmii-id. The rest are autodetected */
719 if (ctype && !strcmp(ctype, "rgmii-id"))
720 priv->interface = PHY_INTERFACE_MODE_RGMII_ID;
721 else
722 priv->interface = PHY_INTERFACE_MODE_MII;
723
724 if (of_get_property(np, "fsl,magic-packet", NULL))
725 priv->device_flags |= FSL_GIANFAR_DEV_HAS_MAGIC_PACKET;
726
Grant Likelyfe192a42009-04-25 12:53:12 +0000727 priv->phy_node = of_parse_phandle(np, "phy-handle", 0);
Andy Flemingb31a1d82008-12-16 15:29:15 -0800728
729 /* Find the TBI PHY. If it's not there, we don't support SGMII */
Grant Likelyfe192a42009-04-25 12:53:12 +0000730 priv->tbi_node = of_parse_phandle(np, "tbi-handle", 0);
Andy Flemingb31a1d82008-12-16 15:29:15 -0800731
732 return 0;
733
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000734rx_alloc_failed:
735 free_rx_pointers(priv);
736tx_alloc_failed:
737 free_tx_pointers(priv);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000738err_grp_init:
739 unmap_group_regs(priv);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000740 free_netdev(dev);
Andy Flemingb31a1d82008-12-16 15:29:15 -0800741 return err;
742}
743
Clifford Wolf0faac9f2009-01-09 10:23:11 +0000744/* Ioctl MII Interface */
745static int gfar_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
746{
747 struct gfar_private *priv = netdev_priv(dev);
748
749 if (!netif_running(dev))
750 return -EINVAL;
751
752 if (!priv->phydev)
753 return -ENODEV;
754
755 return phy_mii_ioctl(priv->phydev, if_mii(rq), cmd);
756}
757
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000758static unsigned int reverse_bitmap(unsigned int bit_map, unsigned int max_qs)
759{
760 unsigned int new_bit_map = 0x0;
761 int mask = 0x1 << (max_qs - 1), i;
762 for (i = 0; i < max_qs; i++) {
763 if (bit_map & mask)
764 new_bit_map = new_bit_map + (1 << i);
765 mask = mask >> 0x1;
766 }
767 return new_bit_map;
768}
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400769/* Set up the ethernet device structure, private data,
770 * and anything else we need before we start */
Andy Flemingb31a1d82008-12-16 15:29:15 -0800771static int gfar_probe(struct of_device *ofdev,
772 const struct of_device_id *match)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773{
774 u32 tempval;
775 struct net_device *dev = NULL;
776 struct gfar_private *priv = NULL;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000777 struct gfar __iomem *regs = NULL;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000778 int err = 0, i, grp_idx = 0;
Dai Harukic50a5d92008-12-17 16:51:32 -0800779 int len_devname;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000780 u32 rstat = 0, tstat = 0, rqueue = 0, tqueue = 0;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000781 u32 isrg = 0;
782 u32 *baddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000784 err = gfar_of_init(ofdev, &dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000786 if (err)
787 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
789 priv = netdev_priv(dev);
Kumar Gala48268572009-03-18 23:28:22 -0700790 priv->ndev = dev;
791 priv->ofdev = ofdev;
Andy Flemingb31a1d82008-12-16 15:29:15 -0800792 priv->node = ofdev->node;
Kumar Gala48268572009-03-18 23:28:22 -0700793 SET_NETDEV_DEV(dev, &ofdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
Scott Woodd87eb122008-07-11 18:04:45 -0500795 spin_lock_init(&priv->bflock);
Sebastian Siewiorab939902008-08-19 21:12:45 +0200796 INIT_WORK(&priv->reset_task, gfar_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
Andy Flemingb31a1d82008-12-16 15:29:15 -0800798 dev_set_drvdata(&ofdev->dev, priv);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000799 regs = priv->gfargrp[0].regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
801 /* Stop the DMA engine now, in case it was running before */
802 /* (The firmware could have used it, and left it running). */
Andy Fleming257d9382008-12-16 15:25:45 -0800803 gfar_halt(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
805 /* Reset MAC layer */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000806 gfar_write(&regs->maccfg1, MACCFG1_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Andy Flemingb98ac702009-02-04 16:38:05 -0800808 /* We need to delay at least 3 TX clocks */
809 udelay(2);
810
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 tempval = (MACCFG1_TX_FLOW | MACCFG1_RX_FLOW);
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000812 gfar_write(&regs->maccfg1, tempval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
814 /* Initialize MACCFG2. */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000815 gfar_write(&regs->maccfg2, MACCFG2_INIT_SETTINGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
817 /* Initialize ECNTRL */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000818 gfar_write(&regs->ecntrl, ECNTRL_INIT_SETTINGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 /* Set the dev->base_addr to the gfar reg region */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000821 dev->base_addr = (unsigned long) regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
Andy Flemingb31a1d82008-12-16 15:29:15 -0800823 SET_NETDEV_DEV(dev, &ofdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
825 /* Fill in the dev structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 dev->watchdog_timeo = TX_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 dev->mtu = 1500;
Andy Fleming26ccfc32009-03-10 12:58:28 +0000828 dev->netdev_ops = &gfar_netdev_ops;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500829 dev->ethtool_ops = &gfar_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000831 /* Register for napi ...We are registering NAPI for each grp */
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000832 for (i = 0; i < priv->num_grps; i++)
833 netif_napi_add(dev, &priv->gfargrp[i].napi, gfar_poll, GFAR_DEV_WEIGHT);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000834
Andy Flemingb31a1d82008-12-16 15:29:15 -0800835 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_CSUM) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500836 priv->rx_csum_enable = 1;
Dai Haruki4669bc92008-12-17 16:51:04 -0800837 dev->features |= NETIF_F_IP_CSUM | NETIF_F_SG | NETIF_F_HIGHDMA;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500838 } else
839 priv->rx_csum_enable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
Kumar Gala0bbaf062005-06-20 10:54:21 -0500841 priv->vlgrp = NULL;
842
Andy Fleming26ccfc32009-03-10 12:58:28 +0000843 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_VLAN)
Kumar Gala0bbaf062005-06-20 10:54:21 -0500844 dev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500845
Andy Flemingb31a1d82008-12-16 15:29:15 -0800846 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_EXTENDED_HASH) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500847 priv->extended_hash = 1;
848 priv->hash_width = 9;
849
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000850 priv->hash_regs[0] = &regs->igaddr0;
851 priv->hash_regs[1] = &regs->igaddr1;
852 priv->hash_regs[2] = &regs->igaddr2;
853 priv->hash_regs[3] = &regs->igaddr3;
854 priv->hash_regs[4] = &regs->igaddr4;
855 priv->hash_regs[5] = &regs->igaddr5;
856 priv->hash_regs[6] = &regs->igaddr6;
857 priv->hash_regs[7] = &regs->igaddr7;
858 priv->hash_regs[8] = &regs->gaddr0;
859 priv->hash_regs[9] = &regs->gaddr1;
860 priv->hash_regs[10] = &regs->gaddr2;
861 priv->hash_regs[11] = &regs->gaddr3;
862 priv->hash_regs[12] = &regs->gaddr4;
863 priv->hash_regs[13] = &regs->gaddr5;
864 priv->hash_regs[14] = &regs->gaddr6;
865 priv->hash_regs[15] = &regs->gaddr7;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500866
867 } else {
868 priv->extended_hash = 0;
869 priv->hash_width = 8;
870
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000871 priv->hash_regs[0] = &regs->gaddr0;
872 priv->hash_regs[1] = &regs->gaddr1;
873 priv->hash_regs[2] = &regs->gaddr2;
874 priv->hash_regs[3] = &regs->gaddr3;
875 priv->hash_regs[4] = &regs->gaddr4;
876 priv->hash_regs[5] = &regs->gaddr5;
877 priv->hash_regs[6] = &regs->gaddr6;
878 priv->hash_regs[7] = &regs->gaddr7;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500879 }
880
Andy Flemingb31a1d82008-12-16 15:29:15 -0800881 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_PADDING)
Kumar Gala0bbaf062005-06-20 10:54:21 -0500882 priv->padding = DEFAULT_PADDING;
883 else
884 priv->padding = 0;
885
Kumar Gala0bbaf062005-06-20 10:54:21 -0500886 if (dev->features & NETIF_F_IP_CSUM)
887 dev->hard_header_len += GMAC_FCB_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000889 /* Program the isrg regs only if number of grps > 1 */
890 if (priv->num_grps > 1) {
891 baddr = &regs->isrg0;
892 for (i = 0; i < priv->num_grps; i++) {
893 isrg |= (priv->gfargrp[i].rx_bit_map << ISRG_SHIFT_RX);
894 isrg |= (priv->gfargrp[i].tx_bit_map << ISRG_SHIFT_TX);
895 gfar_write(baddr, isrg);
896 baddr++;
897 isrg = 0x0;
898 }
899 }
900
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000901 /* Need to reverse the bit maps as bit_map's MSB is q0
902 * but, for_each_bit parses from right to left, which
903 * basically reverses the queue numbers */
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000904 for (i = 0; i< priv->num_grps; i++) {
905 priv->gfargrp[i].tx_bit_map = reverse_bitmap(
906 priv->gfargrp[i].tx_bit_map, MAX_TX_QS);
907 priv->gfargrp[i].rx_bit_map = reverse_bitmap(
908 priv->gfargrp[i].rx_bit_map, MAX_RX_QS);
909 }
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000910
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000911 /* Calculate RSTAT, TSTAT, RQUEUE and TQUEUE values,
912 * also assign queues to groups */
913 for (grp_idx = 0; grp_idx < priv->num_grps; grp_idx++) {
914 priv->gfargrp[grp_idx].num_rx_queues = 0x0;
915 for_each_bit(i, &priv->gfargrp[grp_idx].rx_bit_map,
916 priv->num_rx_queues) {
917 priv->gfargrp[grp_idx].num_rx_queues++;
918 priv->rx_queue[i]->grp = &priv->gfargrp[grp_idx];
919 rstat = rstat | (RSTAT_CLEAR_RHALT >> i);
920 rqueue = rqueue | ((RQUEUE_EN0 | RQUEUE_EX0) >> i);
921 }
922 priv->gfargrp[grp_idx].num_tx_queues = 0x0;
923 for_each_bit (i, &priv->gfargrp[grp_idx].tx_bit_map,
924 priv->num_tx_queues) {
925 priv->gfargrp[grp_idx].num_tx_queues++;
926 priv->tx_queue[i]->grp = &priv->gfargrp[grp_idx];
927 tstat = tstat | (TSTAT_CLEAR_THALT >> i);
928 tqueue = tqueue | (TQUEUE_EN0 >> i);
929 }
930 priv->gfargrp[grp_idx].rstat = rstat;
931 priv->gfargrp[grp_idx].tstat = tstat;
932 rstat = tstat =0;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000933 }
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000934
935 gfar_write(&regs->rqueue, rqueue);
936 gfar_write(&regs->tqueue, tqueue);
937
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 priv->rx_buffer_size = DEFAULT_RX_BUFFER_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000940 /* Initializing some of the rx/tx queue level parameters */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000941 for (i = 0; i < priv->num_tx_queues; i++) {
942 priv->tx_queue[i]->tx_ring_size = DEFAULT_TX_RING_SIZE;
943 priv->tx_queue[i]->num_txbdfree = DEFAULT_TX_RING_SIZE;
944 priv->tx_queue[i]->txcoalescing = DEFAULT_TX_COALESCE;
945 priv->tx_queue[i]->txic = DEFAULT_TXIC;
946 }
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000947
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000948 for (i = 0; i < priv->num_rx_queues; i++) {
949 priv->rx_queue[i]->rx_ring_size = DEFAULT_RX_RING_SIZE;
950 priv->rx_queue[i]->rxcoalescing = DEFAULT_RX_COALESCE;
951 priv->rx_queue[i]->rxic = DEFAULT_RXIC;
952 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
Kumar Gala0bbaf062005-06-20 10:54:21 -0500954 /* Enable most messages by default */
955 priv->msg_enable = (NETIF_MSG_IFUP << 1 ) - 1;
956
Trent Piephod3eab822008-10-02 11:12:24 +0000957 /* Carrier starts down, phylib will bring it up */
958 netif_carrier_off(dev);
959
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 err = register_netdev(dev);
961
962 if (err) {
963 printk(KERN_ERR "%s: Cannot register net device, aborting.\n",
964 dev->name);
965 goto register_fail;
966 }
967
Anton Vorontsov2884e5c2009-02-01 00:52:34 -0800968 device_init_wakeup(&dev->dev,
969 priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
970
Dai Harukic50a5d92008-12-17 16:51:32 -0800971 /* fill out IRQ number and name fields */
972 len_devname = strlen(dev->name);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000973 for (i = 0; i < priv->num_grps; i++) {
974 strncpy(&priv->gfargrp[i].int_name_tx[0], dev->name,
975 len_devname);
976 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
977 strncpy(&priv->gfargrp[i].int_name_tx[len_devname],
978 "_g", sizeof("_g"));
979 priv->gfargrp[i].int_name_tx[
980 strlen(priv->gfargrp[i].int_name_tx)] = i+48;
981 strncpy(&priv->gfargrp[i].int_name_tx[strlen(
982 priv->gfargrp[i].int_name_tx)],
983 "_tx", sizeof("_tx") + 1);
Dai Harukic50a5d92008-12-17 16:51:32 -0800984
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000985 strncpy(&priv->gfargrp[i].int_name_rx[0], dev->name,
986 len_devname);
987 strncpy(&priv->gfargrp[i].int_name_rx[len_devname],
988 "_g", sizeof("_g"));
989 priv->gfargrp[i].int_name_rx[
990 strlen(priv->gfargrp[i].int_name_rx)] = i+48;
991 strncpy(&priv->gfargrp[i].int_name_rx[strlen(
992 priv->gfargrp[i].int_name_rx)],
993 "_rx", sizeof("_rx") + 1);
Dai Harukic50a5d92008-12-17 16:51:32 -0800994
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000995 strncpy(&priv->gfargrp[i].int_name_er[0], dev->name,
996 len_devname);
997 strncpy(&priv->gfargrp[i].int_name_er[len_devname],
998 "_g", sizeof("_g"));
999 priv->gfargrp[i].int_name_er[strlen(
1000 priv->gfargrp[i].int_name_er)] = i+48;
1001 strncpy(&priv->gfargrp[i].int_name_er[strlen(\
1002 priv->gfargrp[i].int_name_er)],
1003 "_er", sizeof("_er") + 1);
1004 } else
1005 priv->gfargrp[i].int_name_tx[len_devname] = '\0';
1006 }
Dai Harukic50a5d92008-12-17 16:51:32 -08001007
Andy Fleming7f7f5312005-11-11 12:38:59 -06001008 /* Create all the sysfs files */
1009 gfar_init_sysfs(dev);
1010
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 /* Print out the device info */
Johannes Berge1749612008-10-27 15:59:26 -07001012 printk(KERN_INFO DEVICE_NAME "%pM\n", dev->name, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
1014 /* Even more device info helps when determining which kernel */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001015 /* provided which set of benchmarks. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 printk(KERN_INFO "%s: Running with NAPI enabled\n", dev->name);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001017 for (i = 0; i < priv->num_rx_queues; i++)
1018 printk(KERN_INFO "%s: :RX BD ring size for Q[%d]: %d\n",
1019 dev->name, i, priv->rx_queue[i]->rx_ring_size);
1020 for(i = 0; i < priv->num_tx_queues; i++)
1021 printk(KERN_INFO "%s:TX BD ring size for Q[%d]: %d\n",
1022 dev->name, i, priv->tx_queue[i]->tx_ring_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
1024 return 0;
1025
1026register_fail:
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001027 unmap_group_regs(priv);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001028 free_tx_pointers(priv);
1029 free_rx_pointers(priv);
Grant Likelyfe192a42009-04-25 12:53:12 +00001030 if (priv->phy_node)
1031 of_node_put(priv->phy_node);
1032 if (priv->tbi_node)
1033 of_node_put(priv->tbi_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 free_netdev(dev);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001035 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036}
1037
Andy Flemingb31a1d82008-12-16 15:29:15 -08001038static int gfar_remove(struct of_device *ofdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039{
Andy Flemingb31a1d82008-12-16 15:29:15 -08001040 struct gfar_private *priv = dev_get_drvdata(&ofdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
Grant Likelyfe192a42009-04-25 12:53:12 +00001042 if (priv->phy_node)
1043 of_node_put(priv->phy_node);
1044 if (priv->tbi_node)
1045 of_node_put(priv->tbi_node);
1046
Andy Flemingb31a1d82008-12-16 15:29:15 -08001047 dev_set_drvdata(&ofdev->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
David S. Millerd9d8e042009-09-06 01:41:02 -07001049 unregister_netdev(priv->ndev);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001050 unmap_group_regs(priv);
Kumar Gala48268572009-03-18 23:28:22 -07001051 free_netdev(priv->ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
1053 return 0;
1054}
1055
Scott Woodd87eb122008-07-11 18:04:45 -05001056#ifdef CONFIG_PM
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001057
1058static int gfar_suspend(struct device *dev)
Scott Woodd87eb122008-07-11 18:04:45 -05001059{
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001060 struct gfar_private *priv = dev_get_drvdata(dev);
1061 struct net_device *ndev = priv->ndev;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001062 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Scott Woodd87eb122008-07-11 18:04:45 -05001063 unsigned long flags;
1064 u32 tempval;
1065
1066 int magic_packet = priv->wol_en &&
Andy Flemingb31a1d82008-12-16 15:29:15 -08001067 (priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
Scott Woodd87eb122008-07-11 18:04:45 -05001068
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001069 netif_device_detach(ndev);
Scott Woodd87eb122008-07-11 18:04:45 -05001070
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001071 if (netif_running(ndev)) {
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001072
1073 local_irq_save(flags);
1074 lock_tx_qs(priv);
1075 lock_rx_qs(priv);
Scott Woodd87eb122008-07-11 18:04:45 -05001076
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001077 gfar_halt_nodisable(ndev);
Scott Woodd87eb122008-07-11 18:04:45 -05001078
1079 /* Disable Tx, and Rx if wake-on-LAN is disabled. */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001080 tempval = gfar_read(&regs->maccfg1);
Scott Woodd87eb122008-07-11 18:04:45 -05001081
1082 tempval &= ~MACCFG1_TX_EN;
1083
1084 if (!magic_packet)
1085 tempval &= ~MACCFG1_RX_EN;
1086
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001087 gfar_write(&regs->maccfg1, tempval);
Scott Woodd87eb122008-07-11 18:04:45 -05001088
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001089 unlock_rx_qs(priv);
1090 unlock_tx_qs(priv);
1091 local_irq_restore(flags);
Scott Woodd87eb122008-07-11 18:04:45 -05001092
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001093 disable_napi(priv);
Scott Woodd87eb122008-07-11 18:04:45 -05001094
1095 if (magic_packet) {
1096 /* Enable interrupt on Magic Packet */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001097 gfar_write(&regs->imask, IMASK_MAG);
Scott Woodd87eb122008-07-11 18:04:45 -05001098
1099 /* Enable Magic Packet mode */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001100 tempval = gfar_read(&regs->maccfg2);
Scott Woodd87eb122008-07-11 18:04:45 -05001101 tempval |= MACCFG2_MPEN;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001102 gfar_write(&regs->maccfg2, tempval);
Scott Woodd87eb122008-07-11 18:04:45 -05001103 } else {
1104 phy_stop(priv->phydev);
1105 }
1106 }
1107
1108 return 0;
1109}
1110
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001111static int gfar_resume(struct device *dev)
Scott Woodd87eb122008-07-11 18:04:45 -05001112{
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001113 struct gfar_private *priv = dev_get_drvdata(dev);
1114 struct net_device *ndev = priv->ndev;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001115 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Scott Woodd87eb122008-07-11 18:04:45 -05001116 unsigned long flags;
1117 u32 tempval;
1118 int magic_packet = priv->wol_en &&
Andy Flemingb31a1d82008-12-16 15:29:15 -08001119 (priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
Scott Woodd87eb122008-07-11 18:04:45 -05001120
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001121 if (!netif_running(ndev)) {
1122 netif_device_attach(ndev);
Scott Woodd87eb122008-07-11 18:04:45 -05001123 return 0;
1124 }
1125
1126 if (!magic_packet && priv->phydev)
1127 phy_start(priv->phydev);
1128
1129 /* Disable Magic Packet mode, in case something
1130 * else woke us up.
1131 */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001132 local_irq_save(flags);
1133 lock_tx_qs(priv);
1134 lock_rx_qs(priv);
Scott Woodd87eb122008-07-11 18:04:45 -05001135
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001136 tempval = gfar_read(&regs->maccfg2);
Scott Woodd87eb122008-07-11 18:04:45 -05001137 tempval &= ~MACCFG2_MPEN;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001138 gfar_write(&regs->maccfg2, tempval);
Scott Woodd87eb122008-07-11 18:04:45 -05001139
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001140 gfar_start(ndev);
Scott Woodd87eb122008-07-11 18:04:45 -05001141
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001142 unlock_rx_qs(priv);
1143 unlock_tx_qs(priv);
1144 local_irq_restore(flags);
Scott Woodd87eb122008-07-11 18:04:45 -05001145
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001146 netif_device_attach(ndev);
Scott Woodd87eb122008-07-11 18:04:45 -05001147
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001148 enable_napi(priv);
Scott Woodd87eb122008-07-11 18:04:45 -05001149
1150 return 0;
1151}
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001152
1153static int gfar_restore(struct device *dev)
1154{
1155 struct gfar_private *priv = dev_get_drvdata(dev);
1156 struct net_device *ndev = priv->ndev;
1157
1158 if (!netif_running(ndev))
1159 return 0;
1160
1161 gfar_init_bds(ndev);
1162 init_registers(ndev);
1163 gfar_set_mac_address(ndev);
1164 gfar_init_mac(ndev);
1165 gfar_start(ndev);
1166
1167 priv->oldlink = 0;
1168 priv->oldspeed = 0;
1169 priv->oldduplex = -1;
1170
1171 if (priv->phydev)
1172 phy_start(priv->phydev);
1173
1174 netif_device_attach(ndev);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001175 napi_enable(&priv->gfargrp.napi);
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001176
1177 return 0;
1178}
1179
1180static struct dev_pm_ops gfar_pm_ops = {
1181 .suspend = gfar_suspend,
1182 .resume = gfar_resume,
1183 .freeze = gfar_suspend,
1184 .thaw = gfar_resume,
1185 .restore = gfar_restore,
1186};
1187
1188#define GFAR_PM_OPS (&gfar_pm_ops)
1189
1190static int gfar_legacy_suspend(struct of_device *ofdev, pm_message_t state)
1191{
1192 return gfar_suspend(&ofdev->dev);
1193}
1194
1195static int gfar_legacy_resume(struct of_device *ofdev)
1196{
1197 return gfar_resume(&ofdev->dev);
1198}
1199
Scott Woodd87eb122008-07-11 18:04:45 -05001200#else
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001201
1202#define GFAR_PM_OPS NULL
1203#define gfar_legacy_suspend NULL
1204#define gfar_legacy_resume NULL
1205
Scott Woodd87eb122008-07-11 18:04:45 -05001206#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001208/* Reads the controller's registers to determine what interface
1209 * connects it to the PHY.
1210 */
1211static phy_interface_t gfar_get_interface(struct net_device *dev)
1212{
1213 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001214 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001215 u32 ecntrl;
1216
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001217 ecntrl = gfar_read(&regs->ecntrl);
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001218
1219 if (ecntrl & ECNTRL_SGMII_MODE)
1220 return PHY_INTERFACE_MODE_SGMII;
1221
1222 if (ecntrl & ECNTRL_TBI_MODE) {
1223 if (ecntrl & ECNTRL_REDUCED_MODE)
1224 return PHY_INTERFACE_MODE_RTBI;
1225 else
1226 return PHY_INTERFACE_MODE_TBI;
1227 }
1228
1229 if (ecntrl & ECNTRL_REDUCED_MODE) {
1230 if (ecntrl & ECNTRL_REDUCED_MII_MODE)
1231 return PHY_INTERFACE_MODE_RMII;
Andy Fleming7132ab72007-07-11 11:43:07 -05001232 else {
Andy Flemingb31a1d82008-12-16 15:29:15 -08001233 phy_interface_t interface = priv->interface;
Andy Fleming7132ab72007-07-11 11:43:07 -05001234
1235 /*
1236 * This isn't autodetected right now, so it must
1237 * be set by the device tree or platform code.
1238 */
1239 if (interface == PHY_INTERFACE_MODE_RGMII_ID)
1240 return PHY_INTERFACE_MODE_RGMII_ID;
1241
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001242 return PHY_INTERFACE_MODE_RGMII;
Andy Fleming7132ab72007-07-11 11:43:07 -05001243 }
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001244 }
1245
Andy Flemingb31a1d82008-12-16 15:29:15 -08001246 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT)
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001247 return PHY_INTERFACE_MODE_GMII;
1248
1249 return PHY_INTERFACE_MODE_MII;
1250}
1251
1252
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001253/* Initializes driver's PHY state, and attaches to the PHY.
1254 * Returns 0 on success.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 */
1256static int init_phy(struct net_device *dev)
1257{
1258 struct gfar_private *priv = netdev_priv(dev);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001259 uint gigabit_support =
Andy Flemingb31a1d82008-12-16 15:29:15 -08001260 priv->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT ?
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001261 SUPPORTED_1000baseT_Full : 0;
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001262 phy_interface_t interface;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
1264 priv->oldlink = 0;
1265 priv->oldspeed = 0;
1266 priv->oldduplex = -1;
1267
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001268 interface = gfar_get_interface(dev);
1269
Anton Vorontsov1db780f2009-07-16 21:31:42 +00001270 priv->phydev = of_phy_connect(dev, priv->phy_node, &adjust_link, 0,
1271 interface);
1272 if (!priv->phydev)
1273 priv->phydev = of_phy_connect_fixed_link(dev, &adjust_link,
1274 interface);
1275 if (!priv->phydev) {
1276 dev_err(&dev->dev, "could not attach to PHY\n");
1277 return -ENODEV;
Grant Likelyfe192a42009-04-25 12:53:12 +00001278 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
Kapil Junejad3c12872007-05-11 18:25:11 -05001280 if (interface == PHY_INTERFACE_MODE_SGMII)
1281 gfar_configure_serdes(dev);
1282
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001283 /* Remove any features not supported by the controller */
Grant Likelyfe192a42009-04-25 12:53:12 +00001284 priv->phydev->supported &= (GFAR_SUPPORTED | gigabit_support);
1285 priv->phydev->advertising = priv->phydev->supported;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
1287 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288}
1289
Paul Gortmakerd0313582008-04-17 00:08:10 -04001290/*
1291 * Initialize TBI PHY interface for communicating with the
1292 * SERDES lynx PHY on the chip. We communicate with this PHY
1293 * through the MDIO bus on each controller, treating it as a
1294 * "normal" PHY at the address found in the TBIPA register. We assume
1295 * that the TBIPA register is valid. Either the MDIO bus code will set
1296 * it to a value that doesn't conflict with other PHYs on the bus, or the
1297 * value doesn't matter, as there are no other PHYs on the bus.
1298 */
Kapil Junejad3c12872007-05-11 18:25:11 -05001299static void gfar_configure_serdes(struct net_device *dev)
1300{
1301 struct gfar_private *priv = netdev_priv(dev);
Grant Likelyfe192a42009-04-25 12:53:12 +00001302 struct phy_device *tbiphy;
Trent Piephoc1324192008-10-30 18:17:06 -07001303
Grant Likelyfe192a42009-04-25 12:53:12 +00001304 if (!priv->tbi_node) {
1305 dev_warn(&dev->dev, "error: SGMII mode requires that the "
1306 "device tree specify a tbi-handle\n");
1307 return;
1308 }
1309
1310 tbiphy = of_phy_find_device(priv->tbi_node);
1311 if (!tbiphy) {
1312 dev_err(&dev->dev, "error: Could not get TBI device\n");
Andy Flemingb31a1d82008-12-16 15:29:15 -08001313 return;
1314 }
Kapil Junejad3c12872007-05-11 18:25:11 -05001315
Andy Flemingb31a1d82008-12-16 15:29:15 -08001316 /*
1317 * If the link is already up, we must already be ok, and don't need to
Trent Piephobdb59f92008-10-30 18:17:07 -07001318 * configure and reset the TBI<->SerDes link. Maybe U-Boot configured
1319 * everything for us? Resetting it takes the link down and requires
1320 * several seconds for it to come back.
1321 */
Grant Likelyfe192a42009-04-25 12:53:12 +00001322 if (phy_read(tbiphy, MII_BMSR) & BMSR_LSTATUS)
Andy Flemingb31a1d82008-12-16 15:29:15 -08001323 return;
Kapil Junejad3c12872007-05-11 18:25:11 -05001324
Paul Gortmakerd0313582008-04-17 00:08:10 -04001325 /* Single clk mode, mii mode off(for serdes communication) */
Grant Likelyfe192a42009-04-25 12:53:12 +00001326 phy_write(tbiphy, MII_TBICON, TBICON_CLK_SELECT);
Kapil Junejad3c12872007-05-11 18:25:11 -05001327
Grant Likelyfe192a42009-04-25 12:53:12 +00001328 phy_write(tbiphy, MII_ADVERTISE,
Kapil Junejad3c12872007-05-11 18:25:11 -05001329 ADVERTISE_1000XFULL | ADVERTISE_1000XPAUSE |
1330 ADVERTISE_1000XPSE_ASYM);
1331
Grant Likelyfe192a42009-04-25 12:53:12 +00001332 phy_write(tbiphy, MII_BMCR, BMCR_ANENABLE |
Kapil Junejad3c12872007-05-11 18:25:11 -05001333 BMCR_ANRESTART | BMCR_FULLDPLX | BMCR_SPEED1000);
1334}
1335
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336static void init_registers(struct net_device *dev)
1337{
1338 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001339 struct gfar __iomem *regs = NULL;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001340 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001342 for (i = 0; i < priv->num_grps; i++) {
1343 regs = priv->gfargrp[i].regs;
1344 /* Clear IEVENT */
1345 gfar_write(&regs->ievent, IEVENT_INIT_CLEAR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001347 /* Initialize IMASK */
1348 gfar_write(&regs->imask, IMASK_INIT_CLEAR);
1349 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001351 regs = priv->gfargrp[0].regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 /* Init hash registers to zero */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001353 gfar_write(&regs->igaddr0, 0);
1354 gfar_write(&regs->igaddr1, 0);
1355 gfar_write(&regs->igaddr2, 0);
1356 gfar_write(&regs->igaddr3, 0);
1357 gfar_write(&regs->igaddr4, 0);
1358 gfar_write(&regs->igaddr5, 0);
1359 gfar_write(&regs->igaddr6, 0);
1360 gfar_write(&regs->igaddr7, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001362 gfar_write(&regs->gaddr0, 0);
1363 gfar_write(&regs->gaddr1, 0);
1364 gfar_write(&regs->gaddr2, 0);
1365 gfar_write(&regs->gaddr3, 0);
1366 gfar_write(&regs->gaddr4, 0);
1367 gfar_write(&regs->gaddr5, 0);
1368 gfar_write(&regs->gaddr6, 0);
1369 gfar_write(&regs->gaddr7, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 /* Zero out the rmon mib registers if it has them */
Andy Flemingb31a1d82008-12-16 15:29:15 -08001372 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_RMON) {
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001373 memset_io(&(regs->rmon), 0, sizeof (struct rmon_mib));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
1375 /* Mask off the CAM interrupts */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001376 gfar_write(&regs->rmon.cam1, 0xffffffff);
1377 gfar_write(&regs->rmon.cam2, 0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 }
1379
1380 /* Initialize the max receive buffer length */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001381 gfar_write(&regs->mrblr, priv->rx_buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 /* Initialize the Minimum Frame Length Register */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001384 gfar_write(&regs->minflr, MINFLR_INIT_SETTINGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385}
1386
Kumar Gala0bbaf062005-06-20 10:54:21 -05001387
1388/* Halt the receive and transmit queues */
Scott Woodd87eb122008-07-11 18:04:45 -05001389static void gfar_halt_nodisable(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390{
1391 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001392 struct gfar __iomem *regs = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 u32 tempval;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001394 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001396 for (i = 0; i < priv->num_grps; i++) {
1397 regs = priv->gfargrp[i].regs;
1398 /* Mask all interrupts */
1399 gfar_write(&regs->imask, IMASK_INIT_CLEAR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001401 /* Clear all interrupts */
1402 gfar_write(&regs->ievent, IEVENT_INIT_CLEAR);
1403 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001405 regs = priv->gfargrp[0].regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 /* Stop the DMA, and wait for it to stop */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001407 tempval = gfar_read(&regs->dmactrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 if ((tempval & (DMACTRL_GRS | DMACTRL_GTS))
1409 != (DMACTRL_GRS | DMACTRL_GTS)) {
1410 tempval |= (DMACTRL_GRS | DMACTRL_GTS);
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001411 gfar_write(&regs->dmactrl, tempval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001413 while (!(gfar_read(&regs->ievent) &
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 (IEVENT_GRSC | IEVENT_GTSC)))
1415 cpu_relax();
1416 }
Scott Woodd87eb122008-07-11 18:04:45 -05001417}
Scott Woodd87eb122008-07-11 18:04:45 -05001418
1419/* Halt the receive and transmit queues */
1420void gfar_halt(struct net_device *dev)
1421{
1422 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001423 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Scott Woodd87eb122008-07-11 18:04:45 -05001424 u32 tempval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
Scott Wood2a54adc2008-08-12 15:10:46 -05001426 gfar_halt_nodisable(dev);
1427
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 /* Disable Rx and Tx */
1429 tempval = gfar_read(&regs->maccfg1);
1430 tempval &= ~(MACCFG1_RX_EN | MACCFG1_TX_EN);
1431 gfar_write(&regs->maccfg1, tempval);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001432}
1433
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001434static void free_grp_irqs(struct gfar_priv_grp *grp)
1435{
1436 free_irq(grp->interruptError, grp);
1437 free_irq(grp->interruptTransmit, grp);
1438 free_irq(grp->interruptReceive, grp);
1439}
1440
Kumar Gala0bbaf062005-06-20 10:54:21 -05001441void stop_gfar(struct net_device *dev)
1442{
1443 struct gfar_private *priv = netdev_priv(dev);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001444 unsigned long flags;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001445 int i;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001446
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001447 phy_stop(priv->phydev);
1448
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001449
Kumar Gala0bbaf062005-06-20 10:54:21 -05001450 /* Lock it down */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001451 local_irq_save(flags);
1452 lock_tx_qs(priv);
1453 lock_rx_qs(priv);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001454
Kumar Gala0bbaf062005-06-20 10:54:21 -05001455 gfar_halt(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001457 unlock_rx_qs(priv);
1458 unlock_tx_qs(priv);
1459 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
1461 /* Free the IRQs */
Andy Flemingb31a1d82008-12-16 15:29:15 -08001462 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001463 for (i = 0; i < priv->num_grps; i++)
1464 free_grp_irqs(&priv->gfargrp[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 } else {
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001466 for (i = 0; i < priv->num_grps; i++)
1467 free_irq(priv->gfargrp[i].interruptTransmit,
1468 &priv->gfargrp[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 }
1470
1471 free_skb_resources(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472}
1473
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001474static void free_skb_tx_queue(struct gfar_priv_tx_q *tx_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 struct txbd8 *txbdp;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001477 struct gfar_private *priv = netdev_priv(tx_queue->dev);
Dai Haruki4669bc92008-12-17 16:51:04 -08001478 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001480 txbdp = tx_queue->tx_bd_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001482 for (i = 0; i < tx_queue->tx_ring_size; i++) {
1483 if (!tx_queue->tx_skbuff[i])
Dai Haruki4669bc92008-12-17 16:51:04 -08001484 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
Kumar Gala48268572009-03-18 23:28:22 -07001486 dma_unmap_single(&priv->ofdev->dev, txbdp->bufPtr,
Dai Haruki4669bc92008-12-17 16:51:04 -08001487 txbdp->length, DMA_TO_DEVICE);
1488 txbdp->lstatus = 0;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001489 for (j = 0; j < skb_shinfo(tx_queue->tx_skbuff[i])->nr_frags;
1490 j++) {
Dai Haruki4669bc92008-12-17 16:51:04 -08001491 txbdp++;
Kumar Gala48268572009-03-18 23:28:22 -07001492 dma_unmap_page(&priv->ofdev->dev, txbdp->bufPtr,
Dai Haruki4669bc92008-12-17 16:51:04 -08001493 txbdp->length, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 }
Andy Flemingad5da7a2008-05-07 13:20:55 -05001495 txbdp++;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001496 dev_kfree_skb_any(tx_queue->tx_skbuff[i]);
1497 tx_queue->tx_skbuff[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 }
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001499 kfree(tx_queue->tx_skbuff);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001500}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001502static void free_skb_rx_queue(struct gfar_priv_rx_q *rx_queue)
1503{
1504 struct rxbd8 *rxbdp;
1505 struct gfar_private *priv = netdev_priv(rx_queue->dev);
1506 int i;
1507
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001508 rxbdp = rx_queue->rx_bd_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001510 for (i = 0; i < rx_queue->rx_ring_size; i++) {
1511 if (rx_queue->rx_skbuff[i]) {
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001512 dma_unmap_single(&priv->ofdev->dev,
1513 rxbdp->bufPtr, priv->rx_buffer_size,
Anton Vorontsove69edd22009-10-12 06:00:30 +00001514 DMA_FROM_DEVICE);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001515 dev_kfree_skb_any(rx_queue->rx_skbuff[i]);
1516 rx_queue->rx_skbuff[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 }
Anton Vorontsove69edd22009-10-12 06:00:30 +00001518 rxbdp->lstatus = 0;
1519 rxbdp->bufPtr = 0;
1520 rxbdp++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 }
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001522 kfree(rx_queue->rx_skbuff);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001523}
Anton Vorontsove69edd22009-10-12 06:00:30 +00001524
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001525/* If there are any tx skbs or rx skbs still around, free them.
1526 * Then free tx_skbuff and rx_skbuff */
1527static void free_skb_resources(struct gfar_private *priv)
1528{
1529 struct gfar_priv_tx_q *tx_queue = NULL;
1530 struct gfar_priv_rx_q *rx_queue = NULL;
1531 int i;
1532
1533 /* Go through all the buffer descriptors and free their data buffers */
1534 for (i = 0; i < priv->num_tx_queues; i++) {
1535 tx_queue = priv->tx_queue[i];
1536 if(!tx_queue->tx_skbuff)
1537 free_skb_tx_queue(tx_queue);
1538 }
1539
1540 for (i = 0; i < priv->num_rx_queues; i++) {
1541 rx_queue = priv->rx_queue[i];
1542 if(!rx_queue->rx_skbuff)
1543 free_skb_rx_queue(rx_queue);
1544 }
1545
1546 dma_free_coherent(&priv->ofdev->dev,
1547 sizeof(struct txbd8) * priv->total_tx_ring_size +
1548 sizeof(struct rxbd8) * priv->total_rx_ring_size,
1549 priv->tx_queue[0]->tx_bd_base,
1550 priv->tx_queue[0]->tx_bd_dma_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551}
1552
Kumar Gala0bbaf062005-06-20 10:54:21 -05001553void gfar_start(struct net_device *dev)
1554{
1555 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001556 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001557 u32 tempval;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001558 int i = 0;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001559
1560 /* Enable Rx and Tx in MACCFG1 */
1561 tempval = gfar_read(&regs->maccfg1);
1562 tempval |= (MACCFG1_RX_EN | MACCFG1_TX_EN);
1563 gfar_write(&regs->maccfg1, tempval);
1564
1565 /* Initialize DMACTRL to have WWR and WOP */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001566 tempval = gfar_read(&regs->dmactrl);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001567 tempval |= DMACTRL_INIT_SETTINGS;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001568 gfar_write(&regs->dmactrl, tempval);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001569
Kumar Gala0bbaf062005-06-20 10:54:21 -05001570 /* Make sure we aren't stopped */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001571 tempval = gfar_read(&regs->dmactrl);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001572 tempval &= ~(DMACTRL_GRS | DMACTRL_GTS);
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001573 gfar_write(&regs->dmactrl, tempval);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001574
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001575 for (i = 0; i < priv->num_grps; i++) {
1576 regs = priv->gfargrp[i].regs;
1577 /* Clear THLT/RHLT, so that the DMA starts polling now */
1578 gfar_write(&regs->tstat, priv->gfargrp[i].tstat);
1579 gfar_write(&regs->rstat, priv->gfargrp[i].rstat);
1580 /* Unmask the interrupts we look for */
1581 gfar_write(&regs->imask, IMASK_DEFAULT);
1582 }
Dai Haruki12dea572008-12-16 15:30:20 -08001583
1584 dev->trans_start = jiffies;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001585}
1586
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001587void gfar_configure_coalescing(struct gfar_private *priv,
1588 unsigned int tx_mask, unsigned int rx_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589{
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001590 struct gfar __iomem *regs = priv->gfargrp[0].regs;
1591 u32 *baddr;
1592 int i = 0;
1593
1594 /* Backward compatible case ---- even if we enable
1595 * multiple queues, there's only single reg to program
1596 */
1597 gfar_write(&regs->txic, 0);
1598 if(likely(priv->tx_queue[0]->txcoalescing))
1599 gfar_write(&regs->txic, priv->tx_queue[0]->txic);
1600
1601 gfar_write(&regs->rxic, 0);
1602 if(unlikely(priv->rx_queue[0]->rxcoalescing))
1603 gfar_write(&regs->rxic, priv->rx_queue[0]->rxic);
1604
1605 if (priv->mode == MQ_MG_MODE) {
1606 baddr = &regs->txic0;
1607 for_each_bit (i, &tx_mask, priv->num_tx_queues) {
1608 if (likely(priv->tx_queue[i]->txcoalescing)) {
1609 gfar_write(baddr + i, 0);
1610 gfar_write(baddr + i, priv->tx_queue[i]->txic);
1611 }
1612 }
1613
1614 baddr = &regs->rxic0;
1615 for_each_bit (i, &rx_mask, priv->num_rx_queues) {
1616 if (likely(priv->rx_queue[i]->rxcoalescing)) {
1617 gfar_write(baddr + i, 0);
1618 gfar_write(baddr + i, priv->rx_queue[i]->rxic);
1619 }
1620 }
1621 }
1622}
1623
1624static int register_grp_irqs(struct gfar_priv_grp *grp)
1625{
1626 struct gfar_private *priv = grp->priv;
1627 struct net_device *dev = priv->ndev;
Anton Vorontsovccc05c62009-10-12 06:00:26 +00001628 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 /* If the device has multiple interrupts, register for
1631 * them. Otherwise, only register for the one */
Andy Flemingb31a1d82008-12-16 15:29:15 -08001632 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001633 /* Install our interrupt handlers for Error,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 * Transmit, and Receive */
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001635 if ((err = request_irq(grp->interruptError, gfar_error, 0,
1636 grp->int_name_er,grp)) < 0) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001637 if (netif_msg_intr(priv))
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001638 printk(KERN_ERR "%s: Can't get IRQ %d\n",
1639 dev->name, grp->interruptError);
1640
1641 goto err_irq_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 }
1643
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001644 if ((err = request_irq(grp->interruptTransmit, gfar_transmit,
1645 0, grp->int_name_tx, grp)) < 0) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001646 if (netif_msg_intr(priv))
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001647 printk(KERN_ERR "%s: Can't get IRQ %d\n",
1648 dev->name, grp->interruptTransmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 goto tx_irq_fail;
1650 }
1651
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001652 if ((err = request_irq(grp->interruptReceive, gfar_receive, 0,
1653 grp->int_name_rx, grp)) < 0) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001654 if (netif_msg_intr(priv))
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001655 printk(KERN_ERR "%s: Can't get IRQ %d\n",
1656 dev->name, grp->interruptReceive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 goto rx_irq_fail;
1658 }
1659 } else {
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001660 if ((err = request_irq(grp->interruptTransmit, gfar_interrupt, 0,
1661 grp->int_name_tx, grp)) < 0) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001662 if (netif_msg_intr(priv))
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001663 printk(KERN_ERR "%s: Can't get IRQ %d\n",
1664 dev->name, grp->interruptTransmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 goto err_irq_fail;
1666 }
1667 }
1668
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001669 return 0;
1670
1671rx_irq_fail:
1672 free_irq(grp->interruptTransmit, grp);
1673tx_irq_fail:
1674 free_irq(grp->interruptError, grp);
1675err_irq_fail:
1676 return err;
1677
1678}
1679
1680/* Bring the controller up and running */
1681int startup_gfar(struct net_device *ndev)
1682{
1683 struct gfar_private *priv = netdev_priv(ndev);
1684 struct gfar __iomem *regs = NULL;
1685 int err, i, j;
1686
1687 for (i = 0; i < priv->num_grps; i++) {
1688 regs= priv->gfargrp[i].regs;
1689 gfar_write(&regs->imask, IMASK_INIT_CLEAR);
1690 }
1691
1692 regs= priv->gfargrp[0].regs;
1693 err = gfar_alloc_skb_resources(ndev);
1694 if (err)
1695 return err;
1696
1697 gfar_init_mac(ndev);
1698
1699 for (i = 0; i < priv->num_grps; i++) {
1700 err = register_grp_irqs(&priv->gfargrp[i]);
1701 if (err) {
1702 for (j = 0; j < i; j++)
1703 free_grp_irqs(&priv->gfargrp[j]);
1704 goto irq_fail;
1705 }
1706 }
1707
Andy Fleming7f7f5312005-11-11 12:38:59 -06001708 /* Start the controller */
Anton Vorontsovccc05c62009-10-12 06:00:26 +00001709 gfar_start(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710
Anton Vorontsov826aa4a2009-10-12 06:00:34 +00001711 phy_start(priv->phydev);
1712
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001713 gfar_configure_coalescing(priv, 0xFF, 0xFF);
1714
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 return 0;
1716
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001717irq_fail:
Anton Vorontsove69edd22009-10-12 06:00:30 +00001718 free_skb_resources(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 return err;
1720}
1721
1722/* Called when something needs to use the ethernet device */
1723/* Returns 0 for success. */
1724static int gfar_enet_open(struct net_device *dev)
1725{
Li Yang94e8cc32007-10-12 21:53:51 +08001726 struct gfar_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 int err;
1728
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001729 enable_napi(priv);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001730
Andy Fleming0fd56bb2009-02-04 16:43:16 -08001731 skb_queue_head_init(&priv->rx_recycle);
1732
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 /* Initialize a bunch of registers */
1734 init_registers(dev);
1735
1736 gfar_set_mac_address(dev);
1737
1738 err = init_phy(dev);
1739
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001740 if (err) {
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001741 disable_napi(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 return err;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001743 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
1745 err = startup_gfar(dev);
Anton Vorontsovdb0e8e32007-10-17 23:57:46 +04001746 if (err) {
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001747 disable_napi(priv);
Anton Vorontsovdb0e8e32007-10-17 23:57:46 +04001748 return err;
1749 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001751 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
Anton Vorontsov2884e5c2009-02-01 00:52:34 -08001753 device_set_wakeup_enable(&dev->dev, priv->wol_en);
1754
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 return err;
1756}
1757
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07001758static inline struct txfcb *gfar_add_fcb(struct sk_buff *skb)
Kumar Gala0bbaf062005-06-20 10:54:21 -05001759{
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07001760 struct txfcb *fcb = (struct txfcb *)skb_push(skb, GMAC_FCB_LEN);
Kumar Gala6c31d552009-04-28 08:04:10 -07001761
1762 memset(fcb, 0, GMAC_FCB_LEN);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001763
Kumar Gala0bbaf062005-06-20 10:54:21 -05001764 return fcb;
1765}
1766
1767static inline void gfar_tx_checksum(struct sk_buff *skb, struct txfcb *fcb)
1768{
Andy Fleming7f7f5312005-11-11 12:38:59 -06001769 u8 flags = 0;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001770
1771 /* If we're here, it's a IP packet with a TCP or UDP
1772 * payload. We set it to checksum, using a pseudo-header
1773 * we provide
1774 */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001775 flags = TXFCB_DEFAULT;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001776
Andy Fleming7f7f5312005-11-11 12:38:59 -06001777 /* Tell the controller what the protocol is */
1778 /* And provide the already calculated phcs */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001779 if (ip_hdr(skb)->protocol == IPPROTO_UDP) {
Andy Fleming7f7f5312005-11-11 12:38:59 -06001780 flags |= TXFCB_UDP;
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -03001781 fcb->phcs = udp_hdr(skb)->check;
Andy Fleming7f7f5312005-11-11 12:38:59 -06001782 } else
Kumar Gala8da32de2007-06-29 00:12:04 -05001783 fcb->phcs = tcp_hdr(skb)->check;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001784
1785 /* l3os is the distance between the start of the
1786 * frame (skb->data) and the start of the IP hdr.
1787 * l4os is the distance between the start of the
1788 * l3 hdr and the l4 hdr */
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03001789 fcb->l3os = (u16)(skb_network_offset(skb) - GMAC_FCB_LEN);
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -03001790 fcb->l4os = skb_network_header_len(skb);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001791
Andy Fleming7f7f5312005-11-11 12:38:59 -06001792 fcb->flags = flags;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001793}
1794
Andy Fleming7f7f5312005-11-11 12:38:59 -06001795void inline gfar_tx_vlan(struct sk_buff *skb, struct txfcb *fcb)
Kumar Gala0bbaf062005-06-20 10:54:21 -05001796{
Andy Fleming7f7f5312005-11-11 12:38:59 -06001797 fcb->flags |= TXFCB_VLN;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001798 fcb->vlctl = vlan_tx_tag_get(skb);
1799}
1800
Dai Haruki4669bc92008-12-17 16:51:04 -08001801static inline struct txbd8 *skip_txbd(struct txbd8 *bdp, int stride,
1802 struct txbd8 *base, int ring_size)
1803{
1804 struct txbd8 *new_bd = bdp + stride;
1805
1806 return (new_bd >= (base + ring_size)) ? (new_bd - ring_size) : new_bd;
1807}
1808
1809static inline struct txbd8 *next_txbd(struct txbd8 *bdp, struct txbd8 *base,
1810 int ring_size)
1811{
1812 return skip_txbd(bdp, 1, base, ring_size);
1813}
1814
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815/* This is called by the kernel when a frame is ready for transmission. */
1816/* It is pointed to by the dev->hard_start_xmit function pointer */
1817static int gfar_start_xmit(struct sk_buff *skb, struct net_device *dev)
1818{
1819 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001820 struct gfar_priv_tx_q *tx_queue = NULL;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001821 struct netdev_queue *txq;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001822 struct gfar __iomem *regs = NULL;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001823 struct txfcb *fcb = NULL;
Dai Haruki4669bc92008-12-17 16:51:04 -08001824 struct txbd8 *txbdp, *txbdp_start, *base;
Dai Haruki5a5efed2008-12-16 15:34:50 -08001825 u32 lstatus;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001826 int i, rq = 0;
Dai Haruki4669bc92008-12-17 16:51:04 -08001827 u32 bufaddr;
Andy Flemingfef61082006-04-20 16:44:29 -05001828 unsigned long flags;
Dai Haruki4669bc92008-12-17 16:51:04 -08001829 unsigned int nr_frags, length;
1830
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001831
1832 rq = skb->queue_mapping;
1833 tx_queue = priv->tx_queue[rq];
1834 txq = netdev_get_tx_queue(dev, rq);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001835 base = tx_queue->tx_bd_base;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001836 regs = tx_queue->grp->regs;
Dai Haruki4669bc92008-12-17 16:51:04 -08001837
Li Yang5b28bea2009-03-27 15:54:30 -07001838 /* make space for additional header when fcb is needed */
1839 if (((skb->ip_summed == CHECKSUM_PARTIAL) ||
1840 (priv->vlgrp && vlan_tx_tag_present(skb))) &&
1841 (skb_headroom(skb) < GMAC_FCB_LEN)) {
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07001842 struct sk_buff *skb_new;
1843
1844 skb_new = skb_realloc_headroom(skb, GMAC_FCB_LEN);
1845 if (!skb_new) {
1846 dev->stats.tx_errors++;
David S. Millerbd14ba82009-03-27 01:10:58 -07001847 kfree_skb(skb);
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07001848 return NETDEV_TX_OK;
1849 }
1850 kfree_skb(skb);
1851 skb = skb_new;
1852 }
1853
Dai Haruki4669bc92008-12-17 16:51:04 -08001854 /* total number of fragments in the SKB */
1855 nr_frags = skb_shinfo(skb)->nr_frags;
1856
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001857 spin_lock_irqsave(&tx_queue->txlock, flags);
Dai Haruki4669bc92008-12-17 16:51:04 -08001858
1859 /* check if there is space to queue this packet */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001860 if ((nr_frags+1) > tx_queue->num_txbdfree) {
Dai Haruki4669bc92008-12-17 16:51:04 -08001861 /* no space, stop the queue */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001862 netif_tx_stop_queue(txq);
Dai Haruki4669bc92008-12-17 16:51:04 -08001863 dev->stats.tx_fifo_errors++;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001864 spin_unlock_irqrestore(&tx_queue->txlock, flags);
Dai Haruki4669bc92008-12-17 16:51:04 -08001865 return NETDEV_TX_BUSY;
1866 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867
1868 /* Update transmit stats */
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001869 dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001871 txbdp = txbdp_start = tx_queue->cur_tx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872
Dai Haruki4669bc92008-12-17 16:51:04 -08001873 if (nr_frags == 0) {
1874 lstatus = txbdp->lstatus | BD_LFLAG(TXBD_LAST | TXBD_INTERRUPT);
1875 } else {
1876 /* Place the fragment addresses and lengths into the TxBDs */
1877 for (i = 0; i < nr_frags; i++) {
1878 /* Point at the next BD, wrapping as needed */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001879 txbdp = next_txbd(txbdp, base, tx_queue->tx_ring_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
Dai Haruki4669bc92008-12-17 16:51:04 -08001881 length = skb_shinfo(skb)->frags[i].size;
1882
1883 lstatus = txbdp->lstatus | length |
1884 BD_LFLAG(TXBD_READY);
1885
1886 /* Handle the last BD specially */
1887 if (i == nr_frags - 1)
1888 lstatus |= BD_LFLAG(TXBD_LAST | TXBD_INTERRUPT);
1889
Kumar Gala48268572009-03-18 23:28:22 -07001890 bufaddr = dma_map_page(&priv->ofdev->dev,
Dai Haruki4669bc92008-12-17 16:51:04 -08001891 skb_shinfo(skb)->frags[i].page,
1892 skb_shinfo(skb)->frags[i].page_offset,
1893 length,
1894 DMA_TO_DEVICE);
1895
1896 /* set the TxBD length and buffer pointer */
1897 txbdp->bufPtr = bufaddr;
1898 txbdp->lstatus = lstatus;
1899 }
1900
1901 lstatus = txbdp_start->lstatus;
1902 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903
Kumar Gala0bbaf062005-06-20 10:54:21 -05001904 /* Set up checksumming */
Dai Haruki12dea572008-12-16 15:30:20 -08001905 if (CHECKSUM_PARTIAL == skb->ip_summed) {
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07001906 fcb = gfar_add_fcb(skb);
1907 lstatus |= BD_LFLAG(TXBD_TOE);
1908 gfar_tx_checksum(skb, fcb);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001909 }
1910
Dai Haruki77ecaf22008-12-16 15:30:48 -08001911 if (priv->vlgrp && vlan_tx_tag_present(skb)) {
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07001912 if (unlikely(NULL == fcb)) {
1913 fcb = gfar_add_fcb(skb);
Dai Haruki5a5efed2008-12-16 15:34:50 -08001914 lstatus |= BD_LFLAG(TXBD_TOE);
Andy Fleming7f7f5312005-11-11 12:38:59 -06001915 }
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07001916
1917 gfar_tx_vlan(skb, fcb);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001918 }
1919
Dai Haruki4669bc92008-12-17 16:51:04 -08001920 /* setup the TxBD length and buffer pointer for the first BD */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001921 tx_queue->tx_skbuff[tx_queue->skb_curtx] = skb;
Kumar Gala48268572009-03-18 23:28:22 -07001922 txbdp_start->bufPtr = dma_map_single(&priv->ofdev->dev, skb->data,
Dai Haruki4669bc92008-12-17 16:51:04 -08001923 skb_headlen(skb), DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924
Dai Haruki4669bc92008-12-17 16:51:04 -08001925 lstatus |= BD_LFLAG(TXBD_CRC | TXBD_READY) | skb_headlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926
Dai Haruki4669bc92008-12-17 16:51:04 -08001927 /*
1928 * The powerpc-specific eieio() is used, as wmb() has too strong
Scott Wood3b6330c2007-05-16 15:06:59 -05001929 * semantics (it requires synchronization between cacheable and
1930 * uncacheable mappings, which eieio doesn't provide and which we
1931 * don't need), thus requiring a more expensive sync instruction. At
1932 * some point, the set of architecture-independent barrier functions
1933 * should be expanded to include weaker barriers.
1934 */
Scott Wood3b6330c2007-05-16 15:06:59 -05001935 eieio();
Andy Fleming7f7f5312005-11-11 12:38:59 -06001936
Dai Haruki4669bc92008-12-17 16:51:04 -08001937 txbdp_start->lstatus = lstatus;
1938
1939 /* Update the current skb pointer to the next entry we will use
1940 * (wrapping if necessary) */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001941 tx_queue->skb_curtx = (tx_queue->skb_curtx + 1) &
1942 TX_RING_MOD_MASK(tx_queue->tx_ring_size);
Dai Haruki4669bc92008-12-17 16:51:04 -08001943
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001944 tx_queue->cur_tx = next_txbd(txbdp, base, tx_queue->tx_ring_size);
Dai Haruki4669bc92008-12-17 16:51:04 -08001945
1946 /* reduce TxBD free count */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001947 tx_queue->num_txbdfree -= (nr_frags + 1);
Dai Haruki4669bc92008-12-17 16:51:04 -08001948
1949 dev->trans_start = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950
1951 /* If the next BD still needs to be cleaned up, then the bds
1952 are full. We need to tell the kernel to stop sending us stuff. */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001953 if (!tx_queue->num_txbdfree) {
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001954 netif_tx_stop_queue(txq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001956 dev->stats.tx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 }
1958
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 /* Tell the DMA to go go go */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001960 gfar_write(&regs->tstat, TSTAT_CLEAR_THALT >> tx_queue->qindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961
1962 /* Unlock priv */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001963 spin_unlock_irqrestore(&tx_queue->txlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07001965 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966}
1967
1968/* Stops the kernel queue, and halts the controller */
1969static int gfar_close(struct net_device *dev)
1970{
1971 struct gfar_private *priv = netdev_priv(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001972
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001973 disable_napi(priv);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001974
Andy Fleming0fd56bb2009-02-04 16:43:16 -08001975 skb_queue_purge(&priv->rx_recycle);
Sebastian Siewiorab939902008-08-19 21:12:45 +02001976 cancel_work_sync(&priv->reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 stop_gfar(dev);
1978
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001979 /* Disconnect from the PHY */
1980 phy_disconnect(priv->phydev);
1981 priv->phydev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001983 netif_tx_stop_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984
1985 return 0;
1986}
1987
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988/* Changes the mac address if the controller is not running. */
Andy Flemingf162b9d2008-05-02 13:00:30 -05001989static int gfar_set_mac_address(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990{
Andy Fleming7f7f5312005-11-11 12:38:59 -06001991 gfar_set_mac_for_addr(dev, 0, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992
1993 return 0;
1994}
1995
1996
Kumar Gala0bbaf062005-06-20 10:54:21 -05001997/* Enables and disables VLAN insertion/extraction */
1998static void gfar_vlan_rx_register(struct net_device *dev,
1999 struct vlan_group *grp)
2000{
2001 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002002 struct gfar __iomem *regs = NULL;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002003 unsigned long flags;
2004 u32 tempval;
2005
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002006 regs = priv->gfargrp[0].regs;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002007 local_irq_save(flags);
2008 lock_rx_qs(priv);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002009
Anton Vorontsovcd1f55a2009-01-26 14:33:23 -08002010 priv->vlgrp = grp;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002011
2012 if (grp) {
2013 /* Enable VLAN tag insertion */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002014 tempval = gfar_read(&regs->tctrl);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002015 tempval |= TCTRL_VLINS;
2016
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002017 gfar_write(&regs->tctrl, tempval);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002018
Kumar Gala0bbaf062005-06-20 10:54:21 -05002019 /* Enable VLAN tag extraction */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002020 tempval = gfar_read(&regs->rctrl);
Dai Haruki77ecaf22008-12-16 15:30:48 -08002021 tempval |= (RCTRL_VLEX | RCTRL_PRSDEP_INIT);
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002022 gfar_write(&regs->rctrl, tempval);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002023 } else {
2024 /* Disable VLAN tag insertion */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002025 tempval = gfar_read(&regs->tctrl);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002026 tempval &= ~TCTRL_VLINS;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002027 gfar_write(&regs->tctrl, tempval);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002028
2029 /* Disable VLAN tag extraction */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002030 tempval = gfar_read(&regs->rctrl);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002031 tempval &= ~RCTRL_VLEX;
Dai Haruki77ecaf22008-12-16 15:30:48 -08002032 /* If parse is no longer required, then disable parser */
2033 if (tempval & RCTRL_REQ_PARSER)
2034 tempval |= RCTRL_PRSDEP_INIT;
2035 else
2036 tempval &= ~RCTRL_PRSDEP_INIT;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002037 gfar_write(&regs->rctrl, tempval);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002038 }
2039
Dai Haruki77ecaf22008-12-16 15:30:48 -08002040 gfar_change_mtu(dev, dev->mtu);
2041
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002042 unlock_rx_qs(priv);
2043 local_irq_restore(flags);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002044}
2045
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046static int gfar_change_mtu(struct net_device *dev, int new_mtu)
2047{
2048 int tempsize, tempval;
2049 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002050 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 int oldsize = priv->rx_buffer_size;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002052 int frame_size = new_mtu + ETH_HLEN;
2053
Dai Haruki77ecaf22008-12-16 15:30:48 -08002054 if (priv->vlgrp)
Dai Harukifaa89572008-03-24 10:53:26 -05002055 frame_size += VLAN_HLEN;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002056
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 if ((frame_size < 64) || (frame_size > JUMBO_FRAME_SIZE)) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05002058 if (netif_msg_drv(priv))
2059 printk(KERN_ERR "%s: Invalid MTU setting\n",
2060 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 return -EINVAL;
2062 }
2063
Dai Haruki77ecaf22008-12-16 15:30:48 -08002064 if (gfar_uses_fcb(priv))
2065 frame_size += GMAC_FCB_LEN;
2066
2067 frame_size += priv->padding;
2068
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 tempsize =
2070 (frame_size & ~(INCREMENTAL_BUFFER_SIZE - 1)) +
2071 INCREMENTAL_BUFFER_SIZE;
2072
2073 /* Only stop and start the controller if it isn't already
Andy Fleming7f7f5312005-11-11 12:38:59 -06002074 * stopped, and we changed something */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 if ((oldsize != tempsize) && (dev->flags & IFF_UP))
2076 stop_gfar(dev);
2077
2078 priv->rx_buffer_size = tempsize;
2079
2080 dev->mtu = new_mtu;
2081
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002082 gfar_write(&regs->mrblr, priv->rx_buffer_size);
2083 gfar_write(&regs->maxfrm, priv->rx_buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084
2085 /* If the mtu is larger than the max size for standard
2086 * ethernet frames (ie, a jumbo frame), then set maccfg2
2087 * to allow huge frames, and to check the length */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002088 tempval = gfar_read(&regs->maccfg2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089
2090 if (priv->rx_buffer_size > DEFAULT_RX_BUFFER_SIZE)
2091 tempval |= (MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK);
2092 else
2093 tempval &= ~(MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK);
2094
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002095 gfar_write(&regs->maccfg2, tempval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096
2097 if ((oldsize != tempsize) && (dev->flags & IFF_UP))
2098 startup_gfar(dev);
2099
2100 return 0;
2101}
2102
Sebastian Siewiorab939902008-08-19 21:12:45 +02002103/* gfar_reset_task gets scheduled when a packet has not been
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 * transmitted after a set amount of time.
2105 * For now, assume that clearing out all the structures, and
Sebastian Siewiorab939902008-08-19 21:12:45 +02002106 * starting over will fix the problem.
2107 */
2108static void gfar_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109{
Sebastian Siewiorab939902008-08-19 21:12:45 +02002110 struct gfar_private *priv = container_of(work, struct gfar_private,
2111 reset_task);
Kumar Gala48268572009-03-18 23:28:22 -07002112 struct net_device *dev = priv->ndev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113
2114 if (dev->flags & IFF_UP) {
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002115 netif_tx_stop_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 stop_gfar(dev);
2117 startup_gfar(dev);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002118 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 }
2120
David S. Miller263ba322008-07-15 03:47:41 -07002121 netif_tx_schedule_all(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122}
2123
Sebastian Siewiorab939902008-08-19 21:12:45 +02002124static void gfar_timeout(struct net_device *dev)
2125{
2126 struct gfar_private *priv = netdev_priv(dev);
2127
2128 dev->stats.tx_errors++;
2129 schedule_work(&priv->reset_task);
2130}
2131
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132/* Interrupt Handler for Transmit complete */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002133static int gfar_clean_tx_ring(struct gfar_priv_tx_q *tx_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134{
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002135 struct net_device *dev = tx_queue->dev;
Dai Harukid080cd62008-04-09 19:37:51 -05002136 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002137 struct gfar_priv_rx_q *rx_queue = NULL;
Dai Haruki4669bc92008-12-17 16:51:04 -08002138 struct txbd8 *bdp;
2139 struct txbd8 *lbdp = NULL;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002140 struct txbd8 *base = tx_queue->tx_bd_base;
Dai Haruki4669bc92008-12-17 16:51:04 -08002141 struct sk_buff *skb;
2142 int skb_dirtytx;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002143 int tx_ring_size = tx_queue->tx_ring_size;
Dai Haruki4669bc92008-12-17 16:51:04 -08002144 int frags = 0;
2145 int i;
Dai Harukid080cd62008-04-09 19:37:51 -05002146 int howmany = 0;
Dai Haruki4669bc92008-12-17 16:51:04 -08002147 u32 lstatus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002149 rx_queue = priv->rx_queue[tx_queue->qindex];
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002150 bdp = tx_queue->dirty_tx;
2151 skb_dirtytx = tx_queue->skb_dirtytx;
Dai Haruki4669bc92008-12-17 16:51:04 -08002152
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002153 while ((skb = tx_queue->tx_skbuff[skb_dirtytx])) {
Dai Haruki4669bc92008-12-17 16:51:04 -08002154 frags = skb_shinfo(skb)->nr_frags;
2155 lbdp = skip_txbd(bdp, frags, base, tx_ring_size);
2156
2157 lstatus = lbdp->lstatus;
2158
2159 /* Only clean completed frames */
2160 if ((lstatus & BD_LFLAG(TXBD_READY)) &&
2161 (lstatus & BD_LENGTH_MASK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 break;
2163
Kumar Gala48268572009-03-18 23:28:22 -07002164 dma_unmap_single(&priv->ofdev->dev,
Dai Haruki4669bc92008-12-17 16:51:04 -08002165 bdp->bufPtr,
2166 bdp->length,
2167 DMA_TO_DEVICE);
2168
2169 bdp->lstatus &= BD_LFLAG(TXBD_WRAP);
2170 bdp = next_txbd(bdp, base, tx_ring_size);
2171
2172 for (i = 0; i < frags; i++) {
Kumar Gala48268572009-03-18 23:28:22 -07002173 dma_unmap_page(&priv->ofdev->dev,
Dai Haruki4669bc92008-12-17 16:51:04 -08002174 bdp->bufPtr,
2175 bdp->length,
2176 DMA_TO_DEVICE);
2177 bdp->lstatus &= BD_LFLAG(TXBD_WRAP);
2178 bdp = next_txbd(bdp, base, tx_ring_size);
2179 }
2180
Andy Fleming0fd56bb2009-02-04 16:43:16 -08002181 /*
2182 * If there's room in the queue (limit it to rx_buffer_size)
2183 * we add this skb back into the pool, if it's the right size
2184 */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002185 if (skb_queue_len(&priv->rx_recycle) < rx_queue->rx_ring_size &&
Andy Fleming0fd56bb2009-02-04 16:43:16 -08002186 skb_recycle_check(skb, priv->rx_buffer_size +
2187 RXBUF_ALIGNMENT))
2188 __skb_queue_head(&priv->rx_recycle, skb);
2189 else
2190 dev_kfree_skb_any(skb);
2191
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002192 tx_queue->tx_skbuff[skb_dirtytx] = NULL;
Dai Haruki4669bc92008-12-17 16:51:04 -08002193
2194 skb_dirtytx = (skb_dirtytx + 1) &
2195 TX_RING_MOD_MASK(tx_ring_size);
2196
Dai Harukid080cd62008-04-09 19:37:51 -05002197 howmany++;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002198 tx_queue->num_txbdfree += frags + 1;
Dai Haruki4669bc92008-12-17 16:51:04 -08002199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200
Dai Haruki4669bc92008-12-17 16:51:04 -08002201 /* If we freed a buffer, we can restart transmission, if necessary */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002202 if (__netif_subqueue_stopped(dev, tx_queue->qindex) && tx_queue->num_txbdfree)
2203 netif_wake_subqueue(dev, tx_queue->qindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204
Dai Haruki4669bc92008-12-17 16:51:04 -08002205 /* Update dirty indicators */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002206 tx_queue->skb_dirtytx = skb_dirtytx;
2207 tx_queue->dirty_tx = bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208
Dai Harukid080cd62008-04-09 19:37:51 -05002209 dev->stats.tx_packets += howmany;
2210
2211 return howmany;
2212}
2213
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002214static void gfar_schedule_cleanup(struct gfar_priv_grp *gfargrp)
Dai Haruki8c7396a2008-12-17 16:52:00 -08002215{
Anton Vorontsova6d0b912009-01-12 21:57:34 -08002216 unsigned long flags;
2217
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002218 spin_lock_irqsave(&gfargrp->grplock, flags);
2219 if (napi_schedule_prep(&gfargrp->napi)) {
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002220 gfar_write(&gfargrp->regs->imask, IMASK_RTX_DISABLED);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002221 __napi_schedule(&gfargrp->napi);
Jarek Poplawski8707bdd2009-02-09 14:59:30 -08002222 } else {
2223 /*
2224 * Clear IEVENT, so interrupts aren't called again
2225 * because of the packets that have already arrived.
2226 */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002227 gfar_write(&gfargrp->regs->ievent, IEVENT_RTX_MASK);
Dai Haruki8c7396a2008-12-17 16:52:00 -08002228 }
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002229 spin_unlock_irqrestore(&gfargrp->grplock, flags);
Anton Vorontsova6d0b912009-01-12 21:57:34 -08002230
Dai Haruki8c7396a2008-12-17 16:52:00 -08002231}
2232
Dai Harukid080cd62008-04-09 19:37:51 -05002233/* Interrupt Handler for Transmit complete */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002234static irqreturn_t gfar_transmit(int irq, void *grp_id)
Dai Harukid080cd62008-04-09 19:37:51 -05002235{
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002236 gfar_schedule_cleanup((struct gfar_priv_grp *)grp_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 return IRQ_HANDLED;
2238}
2239
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002240static void gfar_new_rxbdp(struct gfar_priv_rx_q *rx_queue, struct rxbd8 *bdp,
Andy Fleming815b97c2008-04-22 17:18:29 -05002241 struct sk_buff *skb)
2242{
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002243 struct net_device *dev = rx_queue->dev;
Andy Fleming815b97c2008-04-22 17:18:29 -05002244 struct gfar_private *priv = netdev_priv(dev);
Anton Vorontsov8a102fe2009-10-12 06:00:37 +00002245 dma_addr_t buf;
Andy Fleming815b97c2008-04-22 17:18:29 -05002246
Anton Vorontsov8a102fe2009-10-12 06:00:37 +00002247 buf = dma_map_single(&priv->ofdev->dev, skb->data,
2248 priv->rx_buffer_size, DMA_FROM_DEVICE);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002249 gfar_init_rxbdp(rx_queue, bdp, buf);
Andy Fleming815b97c2008-04-22 17:18:29 -05002250}
2251
2252
2253struct sk_buff * gfar_new_skb(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254{
Andy Fleming7f7f5312005-11-11 12:38:59 -06002255 unsigned int alignamount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 struct gfar_private *priv = netdev_priv(dev);
2257 struct sk_buff *skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258
Andy Fleming0fd56bb2009-02-04 16:43:16 -08002259 skb = __skb_dequeue(&priv->rx_recycle);
2260 if (!skb)
2261 skb = netdev_alloc_skb(dev,
2262 priv->rx_buffer_size + RXBUF_ALIGNMENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263
Andy Fleming815b97c2008-04-22 17:18:29 -05002264 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 return NULL;
2266
Andy Fleming7f7f5312005-11-11 12:38:59 -06002267 alignamount = RXBUF_ALIGNMENT -
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002268 (((unsigned long) skb->data) & (RXBUF_ALIGNMENT - 1));
Andy Fleming7f7f5312005-11-11 12:38:59 -06002269
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 /* We need the data buffer to be aligned properly. We will reserve
2271 * as many bytes as needed to align the data properly
2272 */
Andy Fleming7f7f5312005-11-11 12:38:59 -06002273 skb_reserve(skb, alignamount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 return skb;
2276}
2277
Li Yang298e1a92007-10-16 14:18:13 +08002278static inline void count_errors(unsigned short status, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279{
Li Yang298e1a92007-10-16 14:18:13 +08002280 struct gfar_private *priv = netdev_priv(dev);
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002281 struct net_device_stats *stats = &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 struct gfar_extra_stats *estats = &priv->extra_stats;
2283
2284 /* If the packet was truncated, none of the other errors
2285 * matter */
2286 if (status & RXBD_TRUNCATED) {
2287 stats->rx_length_errors++;
2288
2289 estats->rx_trunc++;
2290
2291 return;
2292 }
2293 /* Count the errors, if there were any */
2294 if (status & (RXBD_LARGE | RXBD_SHORT)) {
2295 stats->rx_length_errors++;
2296
2297 if (status & RXBD_LARGE)
2298 estats->rx_large++;
2299 else
2300 estats->rx_short++;
2301 }
2302 if (status & RXBD_NONOCTET) {
2303 stats->rx_frame_errors++;
2304 estats->rx_nonoctet++;
2305 }
2306 if (status & RXBD_CRCERR) {
2307 estats->rx_crcerr++;
2308 stats->rx_crc_errors++;
2309 }
2310 if (status & RXBD_OVERRUN) {
2311 estats->rx_overrun++;
2312 stats->rx_crc_errors++;
2313 }
2314}
2315
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002316irqreturn_t gfar_receive(int irq, void *grp_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317{
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002318 gfar_schedule_cleanup((struct gfar_priv_grp *)grp_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 return IRQ_HANDLED;
2320}
2321
Kumar Gala0bbaf062005-06-20 10:54:21 -05002322static inline void gfar_rx_checksum(struct sk_buff *skb, struct rxfcb *fcb)
2323{
2324 /* If valid headers were found, and valid sums
2325 * were verified, then we tell the kernel that no
2326 * checksumming is necessary. Otherwise, it is */
Andy Fleming7f7f5312005-11-11 12:38:59 -06002327 if ((fcb->flags & RXFCB_CSUM_MASK) == (RXFCB_CIP | RXFCB_CTU))
Kumar Gala0bbaf062005-06-20 10:54:21 -05002328 skb->ip_summed = CHECKSUM_UNNECESSARY;
2329 else
2330 skb->ip_summed = CHECKSUM_NONE;
2331}
2332
2333
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334/* gfar_process_frame() -- handle one incoming packet if skb
2335 * isn't NULL. */
2336static int gfar_process_frame(struct net_device *dev, struct sk_buff *skb,
Dai Haruki2c2db482008-12-16 15:31:15 -08002337 int amount_pull)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338{
2339 struct gfar_private *priv = netdev_priv(dev);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002340 struct rxfcb *fcb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341
Dai Haruki2c2db482008-12-16 15:31:15 -08002342 int ret;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002343
Dai Haruki2c2db482008-12-16 15:31:15 -08002344 /* fcb is at the beginning if exists */
2345 fcb = (struct rxfcb *)skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346
Dai Haruki2c2db482008-12-16 15:31:15 -08002347 /* Remove the FCB from the skb */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002348 skb_set_queue_mapping(skb, fcb->rq);
Dai Haruki2c2db482008-12-16 15:31:15 -08002349 /* Remove the padded bytes, if there are any */
2350 if (amount_pull)
2351 skb_pull(skb, amount_pull);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002352
Dai Haruki2c2db482008-12-16 15:31:15 -08002353 if (priv->rx_csum_enable)
2354 gfar_rx_checksum(skb, fcb);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002355
Dai Haruki2c2db482008-12-16 15:31:15 -08002356 /* Tell the skb what kind of packet this is */
2357 skb->protocol = eth_type_trans(skb, dev);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002358
Dai Haruki2c2db482008-12-16 15:31:15 -08002359 /* Send the packet up the stack */
2360 if (unlikely(priv->vlgrp && (fcb->flags & RXFCB_VLN)))
2361 ret = vlan_hwaccel_receive_skb(skb, priv->vlgrp, fcb->vlctl);
2362 else
2363 ret = netif_receive_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364
Dai Haruki2c2db482008-12-16 15:31:15 -08002365 if (NET_RX_DROP == ret)
2366 priv->extra_stats.kernel_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367
2368 return 0;
2369}
2370
2371/* gfar_clean_rx_ring() -- Processes each frame in the rx ring
Kumar Gala0bbaf062005-06-20 10:54:21 -05002372 * until the budget/quota has been reached. Returns the number
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 * of frames handled
2374 */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002375int gfar_clean_rx_ring(struct gfar_priv_rx_q *rx_queue, int rx_work_limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376{
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002377 struct net_device *dev = rx_queue->dev;
Andy Fleming31de1982008-12-16 15:33:40 -08002378 struct rxbd8 *bdp, *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 struct sk_buff *skb;
Dai Haruki2c2db482008-12-16 15:31:15 -08002380 int pkt_len;
2381 int amount_pull;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 int howmany = 0;
2383 struct gfar_private *priv = netdev_priv(dev);
2384
2385 /* Get the first full descriptor */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002386 bdp = rx_queue->cur_rx;
2387 base = rx_queue->rx_bd_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388
Dai Haruki2c2db482008-12-16 15:31:15 -08002389 amount_pull = (gfar_uses_fcb(priv) ? GMAC_FCB_LEN : 0) +
2390 priv->padding;
2391
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 while (!((bdp->status & RXBD_EMPTY) || (--rx_work_limit < 0))) {
Andy Fleming815b97c2008-04-22 17:18:29 -05002393 struct sk_buff *newskb;
Scott Wood3b6330c2007-05-16 15:06:59 -05002394 rmb();
Andy Fleming815b97c2008-04-22 17:18:29 -05002395
2396 /* Add another skb for the future */
2397 newskb = gfar_new_skb(dev);
2398
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002399 skb = rx_queue->rx_skbuff[rx_queue->skb_currx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400
Kumar Gala48268572009-03-18 23:28:22 -07002401 dma_unmap_single(&priv->ofdev->dev, bdp->bufPtr,
Andy Fleming81183052008-11-12 10:07:11 -06002402 priv->rx_buffer_size, DMA_FROM_DEVICE);
2403
Andy Fleming815b97c2008-04-22 17:18:29 -05002404 /* We drop the frame if we failed to allocate a new buffer */
2405 if (unlikely(!newskb || !(bdp->status & RXBD_LAST) ||
2406 bdp->status & RXBD_ERR)) {
2407 count_errors(bdp->status, dev);
2408
2409 if (unlikely(!newskb))
2410 newskb = skb;
Lennert Buytenhek4e2fd552009-05-25 00:42:34 -07002411 else if (skb) {
2412 /*
2413 * We need to reset ->data to what it
2414 * was before gfar_new_skb() re-aligned
2415 * it to an RXBUF_ALIGNMENT boundary
2416 * before we put the skb back on the
2417 * recycle list.
2418 */
2419 skb->data = skb->head + NET_SKB_PAD;
Andy Fleming0fd56bb2009-02-04 16:43:16 -08002420 __skb_queue_head(&priv->rx_recycle, skb);
Lennert Buytenhek4e2fd552009-05-25 00:42:34 -07002421 }
Andy Fleming815b97c2008-04-22 17:18:29 -05002422 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 /* Increment the number of packets */
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002424 dev->stats.rx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 howmany++;
2426
Dai Haruki2c2db482008-12-16 15:31:15 -08002427 if (likely(skb)) {
2428 pkt_len = bdp->length - ETH_FCS_LEN;
2429 /* Remove the FCS from the packet length */
2430 skb_put(skb, pkt_len);
2431 dev->stats.rx_bytes += pkt_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432
Andy Fleming1577ece2009-02-04 16:42:12 -08002433 if (in_irq() || irqs_disabled())
2434 printk("Interrupt problem!\n");
Dai Haruki2c2db482008-12-16 15:31:15 -08002435 gfar_process_frame(dev, skb, amount_pull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436
Dai Haruki2c2db482008-12-16 15:31:15 -08002437 } else {
2438 if (netif_msg_rx_err(priv))
2439 printk(KERN_WARNING
2440 "%s: Missing skb!\n", dev->name);
2441 dev->stats.rx_dropped++;
2442 priv->extra_stats.rx_skbmissing++;
2443 }
2444
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 }
2446
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002447 rx_queue->rx_skbuff[rx_queue->skb_currx] = newskb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448
Andy Fleming815b97c2008-04-22 17:18:29 -05002449 /* Setup the new bdp */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002450 gfar_new_rxbdp(rx_queue, bdp, newskb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451
2452 /* Update to the next pointer */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002453 bdp = next_bd(bdp, base, rx_queue->rx_ring_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454
2455 /* update to point at the next skb */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002456 rx_queue->skb_currx =
2457 (rx_queue->skb_currx + 1) &
2458 RX_RING_MOD_MASK(rx_queue->rx_ring_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 }
2460
2461 /* Update the current rxbd pointer to be the next one */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002462 rx_queue->cur_rx = bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 return howmany;
2465}
2466
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002467static int gfar_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468{
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002469 struct gfar_priv_grp *gfargrp = container_of(napi,
2470 struct gfar_priv_grp, napi);
2471 struct gfar_private *priv = gfargrp->priv;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002472 struct gfar __iomem *regs = gfargrp->regs;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002473 struct gfar_priv_tx_q *tx_queue = NULL;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002474 struct gfar_priv_rx_q *rx_queue = NULL;
2475 int rx_cleaned = 0, budget_per_queue = 0, rx_cleaned_per_queue = 0;
2476 int tx_cleaned = 0, i, left_over_budget = budget, serviced_queues = 0;
2477 int num_queues = 0;
Dai Harukid080cd62008-04-09 19:37:51 -05002478 unsigned long flags;
2479
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002480 num_queues = gfargrp->num_rx_queues;
2481 budget_per_queue = budget/num_queues;
2482
Dai Haruki8c7396a2008-12-17 16:52:00 -08002483 /* Clear IEVENT, so interrupts aren't called again
2484 * because of the packets that have already arrived */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002485 gfar_write(&regs->ievent, IEVENT_RTX_MASK);
Dai Haruki8c7396a2008-12-17 16:52:00 -08002486
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002487 while (num_queues && left_over_budget) {
2488
2489 budget_per_queue = left_over_budget/num_queues;
2490 left_over_budget = 0;
2491
2492 for_each_bit(i, &gfargrp->rx_bit_map, priv->num_rx_queues) {
2493 if (test_bit(i, &serviced_queues))
2494 continue;
2495 rx_queue = priv->rx_queue[i];
2496 tx_queue = priv->tx_queue[rx_queue->qindex];
2497
2498 /* If we fail to get the lock,
2499 * don't bother with the TX BDs */
2500 if (spin_trylock_irqsave(&tx_queue->txlock, flags)) {
2501 tx_cleaned += gfar_clean_tx_ring(tx_queue);
2502 spin_unlock_irqrestore(&tx_queue->txlock,
2503 flags);
2504 }
2505
2506 rx_cleaned_per_queue = gfar_clean_rx_ring(rx_queue,
2507 budget_per_queue);
2508 rx_cleaned += rx_cleaned_per_queue;
2509 if(rx_cleaned_per_queue < budget_per_queue) {
2510 left_over_budget = left_over_budget +
2511 (budget_per_queue - rx_cleaned_per_queue);
2512 set_bit(i, &serviced_queues);
2513 num_queues--;
2514 }
2515 }
Dai Harukid080cd62008-04-09 19:37:51 -05002516 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517
Andy Fleming42199882008-12-17 16:52:30 -08002518 if (tx_cleaned)
2519 return budget;
2520
2521 if (rx_cleaned < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -08002522 napi_complete(napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523
2524 /* Clear the halt bit in RSTAT */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002525 gfar_write(&regs->rstat, gfargrp->rstat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002527 gfar_write(&regs->imask, IMASK_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528
2529 /* If we are coalescing interrupts, update the timer */
2530 /* Otherwise, clear it */
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002531 gfar_configure_coalescing(priv,
2532 gfargrp->rx_bit_map, gfargrp->tx_bit_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 }
2534
Andy Fleming42199882008-12-17 16:52:30 -08002535 return rx_cleaned;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537
Vitaly Woolf2d71c22006-11-07 13:27:02 +03002538#ifdef CONFIG_NET_POLL_CONTROLLER
2539/*
2540 * Polling 'interrupt' - used by things like netconsole to send skbs
2541 * without having to re-enable interrupts. It's not called while
2542 * the interrupt routine is executing.
2543 */
2544static void gfar_netpoll(struct net_device *dev)
2545{
2546 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002547 int i = 0;
Vitaly Woolf2d71c22006-11-07 13:27:02 +03002548
2549 /* If the device has multiple interrupts, run tx/rx */
Andy Flemingb31a1d82008-12-16 15:29:15 -08002550 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002551 for (i = 0; i < priv->num_grps; i++) {
2552 disable_irq(priv->gfargrp[i].interruptTransmit);
2553 disable_irq(priv->gfargrp[i].interruptReceive);
2554 disable_irq(priv->gfargrp[i].interruptError);
2555 gfar_interrupt(priv->gfargrp[i].interruptTransmit,
2556 &priv->gfargrp[i]);
2557 enable_irq(priv->gfargrp[i].interruptError);
2558 enable_irq(priv->gfargrp[i].interruptReceive);
2559 enable_irq(priv->gfargrp[i].interruptTransmit);
2560 }
Vitaly Woolf2d71c22006-11-07 13:27:02 +03002561 } else {
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002562 for (i = 0; i < priv->num_grps; i++) {
2563 disable_irq(priv->gfargrp[i].interruptTransmit);
2564 gfar_interrupt(priv->gfargrp[i].interruptTransmit,
2565 &priv->gfargrp[i]);
2566 enable_irq(priv->gfargrp[i].interruptTransmit);
Vitaly Woolf2d71c22006-11-07 13:27:02 +03002567 }
2568}
2569#endif
2570
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571/* The interrupt handler for devices with one interrupt */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002572static irqreturn_t gfar_interrupt(int irq, void *grp_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573{
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002574 struct gfar_priv_grp *gfargrp = grp_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575
2576 /* Save ievent for future reference */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002577 u32 events = gfar_read(&gfargrp->regs->ievent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 /* Check for reception */
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002580 if (events & IEVENT_RX_MASK)
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002581 gfar_receive(irq, grp_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582
2583 /* Check for transmit completion */
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002584 if (events & IEVENT_TX_MASK)
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002585 gfar_transmit(irq, grp_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002587 /* Check for errors */
2588 if (events & IEVENT_ERR_MASK)
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002589 gfar_error(irq, grp_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590
2591 return IRQ_HANDLED;
2592}
2593
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594/* Called every time the controller might need to be made
2595 * aware of new link state. The PHY code conveys this
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002596 * information through variables in the phydev structure, and this
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 * function converts those variables into the appropriate
2598 * register values, and can bring down the device if needed.
2599 */
2600static void adjust_link(struct net_device *dev)
2601{
2602 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002603 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002604 unsigned long flags;
2605 struct phy_device *phydev = priv->phydev;
2606 int new_state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002608 local_irq_save(flags);
2609 lock_tx_qs(priv);
2610
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002611 if (phydev->link) {
2612 u32 tempval = gfar_read(&regs->maccfg2);
Andy Fleming7f7f5312005-11-11 12:38:59 -06002613 u32 ecntrl = gfar_read(&regs->ecntrl);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002614
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 /* Now we make sure that we can be in full duplex mode.
2616 * If not, we operate in half-duplex mode. */
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002617 if (phydev->duplex != priv->oldduplex) {
2618 new_state = 1;
2619 if (!(phydev->duplex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 tempval &= ~(MACCFG2_FULL_DUPLEX);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002621 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 tempval |= MACCFG2_FULL_DUPLEX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002624 priv->oldduplex = phydev->duplex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 }
2626
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002627 if (phydev->speed != priv->oldspeed) {
2628 new_state = 1;
2629 switch (phydev->speed) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 case 1000:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 tempval =
2632 ((tempval & ~(MACCFG2_IF)) | MACCFG2_GMII);
Li Yangf430e492009-01-06 14:08:10 -08002633
2634 ecntrl &= ~(ECNTRL_R100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 break;
2636 case 100:
2637 case 10:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 tempval =
2639 ((tempval & ~(MACCFG2_IF)) | MACCFG2_MII);
Andy Fleming7f7f5312005-11-11 12:38:59 -06002640
2641 /* Reduced mode distinguishes
2642 * between 10 and 100 */
2643 if (phydev->speed == SPEED_100)
2644 ecntrl |= ECNTRL_R100;
2645 else
2646 ecntrl &= ~(ECNTRL_R100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 break;
2648 default:
Kumar Gala0bbaf062005-06-20 10:54:21 -05002649 if (netif_msg_link(priv))
2650 printk(KERN_WARNING
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002651 "%s: Ack! Speed (%d) is not 10/100/1000!\n",
2652 dev->name, phydev->speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 break;
2654 }
2655
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002656 priv->oldspeed = phydev->speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 }
2658
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002659 gfar_write(&regs->maccfg2, tempval);
Andy Fleming7f7f5312005-11-11 12:38:59 -06002660 gfar_write(&regs->ecntrl, ecntrl);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002661
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 if (!priv->oldlink) {
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002663 new_state = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 priv->oldlink = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 }
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002666 } else if (priv->oldlink) {
2667 new_state = 1;
2668 priv->oldlink = 0;
2669 priv->oldspeed = 0;
2670 priv->oldduplex = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002673 if (new_state && netif_msg_link(priv))
2674 phy_print_status(phydev);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002675 unlock_tx_qs(priv);
2676 local_irq_restore(flags);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002677}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678
2679/* Update the hash table based on the current list of multicast
2680 * addresses we subscribe to. Also, change the promiscuity of
2681 * the device based on the flags (this function is called
2682 * whenever dev->flags is changed */
2683static void gfar_set_multi(struct net_device *dev)
2684{
2685 struct dev_mc_list *mc_ptr;
2686 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002687 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 u32 tempval;
2689
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002690 if (dev->flags & IFF_PROMISC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 /* Set RCTRL to PROM */
2692 tempval = gfar_read(&regs->rctrl);
2693 tempval |= RCTRL_PROM;
2694 gfar_write(&regs->rctrl, tempval);
2695 } else {
2696 /* Set RCTRL to not PROM */
2697 tempval = gfar_read(&regs->rctrl);
2698 tempval &= ~(RCTRL_PROM);
2699 gfar_write(&regs->rctrl, tempval);
2700 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002701
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002702 if (dev->flags & IFF_ALLMULTI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 /* Set the hash to rx all multicast frames */
Kumar Gala0bbaf062005-06-20 10:54:21 -05002704 gfar_write(&regs->igaddr0, 0xffffffff);
2705 gfar_write(&regs->igaddr1, 0xffffffff);
2706 gfar_write(&regs->igaddr2, 0xffffffff);
2707 gfar_write(&regs->igaddr3, 0xffffffff);
2708 gfar_write(&regs->igaddr4, 0xffffffff);
2709 gfar_write(&regs->igaddr5, 0xffffffff);
2710 gfar_write(&regs->igaddr6, 0xffffffff);
2711 gfar_write(&regs->igaddr7, 0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 gfar_write(&regs->gaddr0, 0xffffffff);
2713 gfar_write(&regs->gaddr1, 0xffffffff);
2714 gfar_write(&regs->gaddr2, 0xffffffff);
2715 gfar_write(&regs->gaddr3, 0xffffffff);
2716 gfar_write(&regs->gaddr4, 0xffffffff);
2717 gfar_write(&regs->gaddr5, 0xffffffff);
2718 gfar_write(&regs->gaddr6, 0xffffffff);
2719 gfar_write(&regs->gaddr7, 0xffffffff);
2720 } else {
Andy Fleming7f7f5312005-11-11 12:38:59 -06002721 int em_num;
2722 int idx;
2723
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 /* zero out the hash */
Kumar Gala0bbaf062005-06-20 10:54:21 -05002725 gfar_write(&regs->igaddr0, 0x0);
2726 gfar_write(&regs->igaddr1, 0x0);
2727 gfar_write(&regs->igaddr2, 0x0);
2728 gfar_write(&regs->igaddr3, 0x0);
2729 gfar_write(&regs->igaddr4, 0x0);
2730 gfar_write(&regs->igaddr5, 0x0);
2731 gfar_write(&regs->igaddr6, 0x0);
2732 gfar_write(&regs->igaddr7, 0x0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 gfar_write(&regs->gaddr0, 0x0);
2734 gfar_write(&regs->gaddr1, 0x0);
2735 gfar_write(&regs->gaddr2, 0x0);
2736 gfar_write(&regs->gaddr3, 0x0);
2737 gfar_write(&regs->gaddr4, 0x0);
2738 gfar_write(&regs->gaddr5, 0x0);
2739 gfar_write(&regs->gaddr6, 0x0);
2740 gfar_write(&regs->gaddr7, 0x0);
2741
Andy Fleming7f7f5312005-11-11 12:38:59 -06002742 /* If we have extended hash tables, we need to
2743 * clear the exact match registers to prepare for
2744 * setting them */
2745 if (priv->extended_hash) {
2746 em_num = GFAR_EM_NUM + 1;
2747 gfar_clear_exact_match(dev);
2748 idx = 1;
2749 } else {
2750 idx = 0;
2751 em_num = 0;
2752 }
2753
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002754 if (dev->mc_count == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 return;
2756
2757 /* Parse the list, and set the appropriate bits */
2758 for(mc_ptr = dev->mc_list; mc_ptr; mc_ptr = mc_ptr->next) {
Andy Fleming7f7f5312005-11-11 12:38:59 -06002759 if (idx < em_num) {
2760 gfar_set_mac_for_addr(dev, idx,
2761 mc_ptr->dmi_addr);
2762 idx++;
2763 } else
2764 gfar_set_hash_for_addr(dev, mc_ptr->dmi_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 }
2766 }
2767
2768 return;
2769}
2770
Andy Fleming7f7f5312005-11-11 12:38:59 -06002771
2772/* Clears each of the exact match registers to zero, so they
2773 * don't interfere with normal reception */
2774static void gfar_clear_exact_match(struct net_device *dev)
2775{
2776 int idx;
2777 u8 zero_arr[MAC_ADDR_LEN] = {0,0,0,0,0,0};
2778
2779 for(idx = 1;idx < GFAR_EM_NUM + 1;idx++)
2780 gfar_set_mac_for_addr(dev, idx, (u8 *)zero_arr);
2781}
2782
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783/* Set the appropriate hash bit for the given addr */
2784/* The algorithm works like so:
2785 * 1) Take the Destination Address (ie the multicast address), and
2786 * do a CRC on it (little endian), and reverse the bits of the
2787 * result.
2788 * 2) Use the 8 most significant bits as a hash into a 256-entry
2789 * table. The table is controlled through 8 32-bit registers:
2790 * gaddr0-7. gaddr0's MSB is entry 0, and gaddr7's LSB is
2791 * gaddr7. This means that the 3 most significant bits in the
2792 * hash index which gaddr register to use, and the 5 other bits
2793 * indicate which bit (assuming an IBM numbering scheme, which
2794 * for PowerPC (tm) is usually the case) in the register holds
2795 * the entry. */
2796static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr)
2797{
2798 u32 tempval;
2799 struct gfar_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 u32 result = ether_crc(MAC_ADDR_LEN, addr);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002801 int width = priv->hash_width;
2802 u8 whichbit = (result >> (32 - width)) & 0x1f;
2803 u8 whichreg = result >> (32 - width + 5);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 u32 value = (1 << (31-whichbit));
2805
Kumar Gala0bbaf062005-06-20 10:54:21 -05002806 tempval = gfar_read(priv->hash_regs[whichreg]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 tempval |= value;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002808 gfar_write(priv->hash_regs[whichreg], tempval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809
2810 return;
2811}
2812
Andy Fleming7f7f5312005-11-11 12:38:59 -06002813
2814/* There are multiple MAC Address register pairs on some controllers
2815 * This function sets the numth pair to a given address
2816 */
2817static void gfar_set_mac_for_addr(struct net_device *dev, int num, u8 *addr)
2818{
2819 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002820 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Andy Fleming7f7f5312005-11-11 12:38:59 -06002821 int idx;
2822 char tmpbuf[MAC_ADDR_LEN];
2823 u32 tempval;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002824 u32 __iomem *macptr = &regs->macstnaddr1;
Andy Fleming7f7f5312005-11-11 12:38:59 -06002825
2826 macptr += num*2;
2827
2828 /* Now copy it into the mac registers backwards, cuz */
2829 /* little endian is silly */
2830 for (idx = 0; idx < MAC_ADDR_LEN; idx++)
2831 tmpbuf[MAC_ADDR_LEN - 1 - idx] = addr[idx];
2832
2833 gfar_write(macptr, *((u32 *) (tmpbuf)));
2834
2835 tempval = *((u32 *) (tmpbuf + 4));
2836
2837 gfar_write(macptr+1, tempval);
2838}
2839
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840/* GFAR error interrupt handler */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002841static irqreturn_t gfar_error(int irq, void *grp_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842{
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002843 struct gfar_priv_grp *gfargrp = grp_id;
2844 struct gfar __iomem *regs = gfargrp->regs;
2845 struct gfar_private *priv= gfargrp->priv;
2846 struct net_device *dev = priv->ndev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847
2848 /* Save ievent for future reference */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002849 u32 events = gfar_read(&regs->ievent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850
2851 /* Clear IEVENT */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002852 gfar_write(&regs->ievent, events & IEVENT_ERR_MASK);
Scott Woodd87eb122008-07-11 18:04:45 -05002853
2854 /* Magic Packet is not an error. */
Andy Flemingb31a1d82008-12-16 15:29:15 -08002855 if ((priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET) &&
Scott Woodd87eb122008-07-11 18:04:45 -05002856 (events & IEVENT_MAG))
2857 events &= ~IEVENT_MAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858
2859 /* Hmm... */
Kumar Gala0bbaf062005-06-20 10:54:21 -05002860 if (netif_msg_rx_err(priv) || netif_msg_tx_err(priv))
2861 printk(KERN_DEBUG "%s: error interrupt (ievent=0x%08x imask=0x%08x)\n",
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002862 dev->name, events, gfar_read(&regs->imask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863
2864 /* Update the error counters */
2865 if (events & IEVENT_TXE) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002866 dev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867
2868 if (events & IEVENT_LC)
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002869 dev->stats.tx_window_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 if (events & IEVENT_CRL)
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002871 dev->stats.tx_aborted_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 if (events & IEVENT_XFUN) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05002873 if (netif_msg_tx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002874 printk(KERN_DEBUG "%s: TX FIFO underrun, "
2875 "packet dropped.\n", dev->name);
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002876 dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 priv->extra_stats.tx_underrun++;
2878
2879 /* Reactivate the Tx Queues */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002880 gfar_write(&regs->tstat, gfargrp->tstat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 }
Kumar Gala0bbaf062005-06-20 10:54:21 -05002882 if (netif_msg_tx_err(priv))
2883 printk(KERN_DEBUG "%s: Transmit Error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 }
2885 if (events & IEVENT_BSY) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002886 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 priv->extra_stats.rx_bsy++;
2888
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002889 gfar_receive(irq, grp_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890
Kumar Gala0bbaf062005-06-20 10:54:21 -05002891 if (netif_msg_rx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002892 printk(KERN_DEBUG "%s: busy error (rstat: %x)\n",
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002893 dev->name, gfar_read(&regs->rstat));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 }
2895 if (events & IEVENT_BABR) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002896 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 priv->extra_stats.rx_babr++;
2898
Kumar Gala0bbaf062005-06-20 10:54:21 -05002899 if (netif_msg_rx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002900 printk(KERN_DEBUG "%s: babbling RX error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 }
2902 if (events & IEVENT_EBERR) {
2903 priv->extra_stats.eberr++;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002904 if (netif_msg_rx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002905 printk(KERN_DEBUG "%s: bus error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 }
Kumar Gala0bbaf062005-06-20 10:54:21 -05002907 if ((events & IEVENT_RXC) && netif_msg_rx_status(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002908 printk(KERN_DEBUG "%s: control frame\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909
2910 if (events & IEVENT_BABT) {
2911 priv->extra_stats.tx_babt++;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002912 if (netif_msg_tx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002913 printk(KERN_DEBUG "%s: babbling TX error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 }
2915 return IRQ_HANDLED;
2916}
2917
Andy Flemingb31a1d82008-12-16 15:29:15 -08002918static struct of_device_id gfar_match[] =
2919{
2920 {
2921 .type = "network",
2922 .compatible = "gianfar",
2923 },
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002924 {
2925 .compatible = "fsl,etsec2",
2926 },
Andy Flemingb31a1d82008-12-16 15:29:15 -08002927 {},
2928};
Anton Vorontsove72701a2009-10-14 14:54:52 -07002929MODULE_DEVICE_TABLE(of, gfar_match);
Andy Flemingb31a1d82008-12-16 15:29:15 -08002930
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931/* Structure for a device driver */
Andy Flemingb31a1d82008-12-16 15:29:15 -08002932static struct of_platform_driver gfar_driver = {
2933 .name = "fsl-gianfar",
2934 .match_table = gfar_match,
2935
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 .probe = gfar_probe,
2937 .remove = gfar_remove,
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00002938 .suspend = gfar_legacy_suspend,
2939 .resume = gfar_legacy_resume,
2940 .driver.pm = GFAR_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941};
2942
2943static int __init gfar_init(void)
2944{
Andy Fleming1577ece2009-02-04 16:42:12 -08002945 return of_register_platform_driver(&gfar_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946}
2947
2948static void __exit gfar_exit(void)
2949{
Andy Flemingb31a1d82008-12-16 15:29:15 -08002950 of_unregister_platform_driver(&gfar_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951}
2952
2953module_init(gfar_init);
2954module_exit(gfar_exit);
2955