blob: 032073d1e3d23e75c31f7917851a87b2a965a30e [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>
Manfred Rudigiercc772ab2010-04-08 23:10:03 +000085#include <linux/net_tstamp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87#include <asm/io.h>
88#include <asm/irq.h>
89#include <asm/uaccess.h>
90#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070091#include <linux/dma-mapping.h>
92#include <linux/crc32.h>
Andy Flemingbb40dcb2005-09-23 22:54:21 -040093#include <linux/mii.h>
94#include <linux/phy.h>
Andy Flemingb31a1d82008-12-16 15:29:15 -080095#include <linux/phy_fixed.h>
96#include <linux/of.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98#include "gianfar.h"
Andy Fleming1577ece2009-02-04 16:42:12 -080099#include "fsl_pq_mdio.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101#define TX_TIMEOUT (1*HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102#undef BRIEF_GFAR_ERRORS
103#undef VERBOSE_GFAR_ERRORS
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105const char gfar_driver_name[] = "Gianfar Ethernet";
Andy Fleming7f7f5312005-11-11 12:38:59 -0600106const char gfar_driver_version[] = "1.3";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108static int gfar_enet_open(struct net_device *dev);
109static int gfar_start_xmit(struct sk_buff *skb, struct net_device *dev);
Sebastian Siewiorab939902008-08-19 21:12:45 +0200110static void gfar_reset_task(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111static void gfar_timeout(struct net_device *dev);
112static int gfar_close(struct net_device *dev);
Andy Fleming815b97c2008-04-22 17:18:29 -0500113struct sk_buff *gfar_new_skb(struct net_device *dev);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000114static void gfar_new_rxbdp(struct gfar_priv_rx_q *rx_queue, struct rxbd8 *bdp,
Andy Fleming815b97c2008-04-22 17:18:29 -0500115 struct sk_buff *skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116static int gfar_set_mac_address(struct net_device *dev);
117static int gfar_change_mtu(struct net_device *dev, int new_mtu);
David Howells7d12e782006-10-05 14:55:46 +0100118static irqreturn_t gfar_error(int irq, void *dev_id);
119static irqreturn_t gfar_transmit(int irq, void *dev_id);
120static irqreturn_t gfar_interrupt(int irq, void *dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121static void adjust_link(struct net_device *dev);
122static void init_registers(struct net_device *dev);
123static int init_phy(struct net_device *dev);
Andy Flemingb31a1d82008-12-16 15:29:15 -0800124static int gfar_probe(struct of_device *ofdev,
125 const struct of_device_id *match);
126static int gfar_remove(struct of_device *ofdev);
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400127static void free_skb_resources(struct gfar_private *priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128static void gfar_set_multi(struct net_device *dev);
129static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr);
Kapil Junejad3c12872007-05-11 18:25:11 -0500130static void gfar_configure_serdes(struct net_device *dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700131static int gfar_poll(struct napi_struct *napi, int budget);
Vitaly Woolf2d71c22006-11-07 13:27:02 +0300132#ifdef CONFIG_NET_POLL_CONTROLLER
133static void gfar_netpoll(struct net_device *dev);
134#endif
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000135int gfar_clean_rx_ring(struct gfar_priv_rx_q *rx_queue, int rx_work_limit);
136static int gfar_clean_tx_ring(struct gfar_priv_tx_q *tx_queue);
Dai Haruki2c2db482008-12-16 15:31:15 -0800137static int gfar_process_frame(struct net_device *dev, struct sk_buff *skb,
138 int amount_pull);
Kumar Gala0bbaf062005-06-20 10:54:21 -0500139static void gfar_vlan_rx_register(struct net_device *netdev,
140 struct vlan_group *grp);
Andy Fleming7f7f5312005-11-11 12:38:59 -0600141void gfar_halt(struct net_device *dev);
Scott Woodd87eb122008-07-11 18:04:45 -0500142static void gfar_halt_nodisable(struct net_device *dev);
Andy Fleming7f7f5312005-11-11 12:38:59 -0600143void gfar_start(struct net_device *dev);
144static void gfar_clear_exact_match(struct net_device *dev);
145static void gfar_set_mac_for_addr(struct net_device *dev, int num, u8 *addr);
Andy Fleming26ccfc32009-03-10 12:58:28 +0000146static int gfar_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
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;
Anton Vorontsov18294ad2009-11-04 12:53:00 +0000330 u32 __iomem *baddr;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000331 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
Sandeep Gopalpet1ccb8382009-12-16 01:14:58 +0000360 if (priv->rx_filer_enable) {
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000361 rctrl |= RCTRL_FILREN;
Sandeep Gopalpet1ccb8382009-12-16 01:14:58 +0000362 /* Program the RIR0 reg with the required distribution */
363 gfar_write(&regs->rir0, DEFAULT_RIR0);
364 }
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000365
366 if (priv->rx_csum_enable)
367 rctrl |= RCTRL_CHECKSUMMING;
368
369 if (priv->extended_hash) {
370 rctrl |= RCTRL_EXTHASH;
371
372 gfar_clear_exact_match(ndev);
373 rctrl |= RCTRL_EMEN;
374 }
375
376 if (priv->padding) {
377 rctrl &= ~RCTRL_PAL_MASK;
378 rctrl |= RCTRL_PADDING(priv->padding);
379 }
380
Manfred Rudigiercc772ab2010-04-08 23:10:03 +0000381 /* Insert receive time stamps into padding alignment bytes */
382 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER) {
383 rctrl &= ~RCTRL_PAL_MASK;
384 rctrl |= RCTRL_PRSDEP_INIT | RCTRL_TS_ENABLE | RCTRL_PADDING(8);
385 priv->padding = 8;
386 }
387
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000388 /* keep vlan related bits if it's enabled */
389 if (priv->vlgrp) {
390 rctrl |= RCTRL_VLEX | RCTRL_PRSDEP_INIT;
391 tctrl |= TCTRL_VLINS;
392 }
393
394 /* Init rctrl based on our settings */
395 gfar_write(&regs->rctrl, rctrl);
396
397 if (ndev->features & NETIF_F_IP_CSUM)
398 tctrl |= TCTRL_INIT_CSUM;
399
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000400 tctrl |= TCTRL_TXSCHED_PRIO;
401
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000402 gfar_write(&regs->tctrl, tctrl);
403
404 /* Set the extraction length and index */
405 attrs = ATTRELI_EL(priv->rx_stash_size) |
406 ATTRELI_EI(priv->rx_stash_index);
407
408 gfar_write(&regs->attreli, attrs);
409
410 /* Start with defaults, and add stashing or locking
411 * depending on the approprate variables */
412 attrs = ATTR_INIT_SETTINGS;
413
414 if (priv->bd_stash_en)
415 attrs |= ATTR_BDSTASH;
416
417 if (priv->rx_stash_size != 0)
418 attrs |= ATTR_BUFSTASH;
419
420 gfar_write(&regs->attr, attrs);
421
422 gfar_write(&regs->fifo_tx_thr, priv->fifo_threshold);
423 gfar_write(&regs->fifo_tx_starve, priv->fifo_starve);
424 gfar_write(&regs->fifo_tx_starve_shutoff, priv->fifo_starve_off);
425}
426
Sandeep Gopalpeta7f38042009-12-16 01:15:07 +0000427static struct net_device_stats *gfar_get_stats(struct net_device *dev)
428{
429 struct gfar_private *priv = netdev_priv(dev);
430 struct netdev_queue *txq;
431 unsigned long rx_packets = 0, rx_bytes = 0, rx_dropped = 0;
432 unsigned long tx_packets = 0, tx_bytes = 0;
433 int i = 0;
434
435 for (i = 0; i < priv->num_rx_queues; i++) {
436 rx_packets += priv->rx_queue[i]->stats.rx_packets;
437 rx_bytes += priv->rx_queue[i]->stats.rx_bytes;
438 rx_dropped += priv->rx_queue[i]->stats.rx_dropped;
439 }
440
441 dev->stats.rx_packets = rx_packets;
442 dev->stats.rx_bytes = rx_bytes;
443 dev->stats.rx_dropped = rx_dropped;
444
445 for (i = 0; i < priv->num_tx_queues; i++) {
446 txq = netdev_get_tx_queue(dev, i);
447 tx_bytes += txq->tx_bytes;
448 tx_packets += txq->tx_packets;
449 }
450
451 dev->stats.tx_bytes = tx_bytes;
452 dev->stats.tx_packets = tx_packets;
453
454 return &dev->stats;
455}
456
Andy Fleming26ccfc32009-03-10 12:58:28 +0000457static const struct net_device_ops gfar_netdev_ops = {
458 .ndo_open = gfar_enet_open,
459 .ndo_start_xmit = gfar_start_xmit,
460 .ndo_stop = gfar_close,
461 .ndo_change_mtu = gfar_change_mtu,
462 .ndo_set_multicast_list = gfar_set_multi,
463 .ndo_tx_timeout = gfar_timeout,
464 .ndo_do_ioctl = gfar_ioctl,
Sandeep Gopalpeta7f38042009-12-16 01:15:07 +0000465 .ndo_get_stats = gfar_get_stats,
Andy Fleming26ccfc32009-03-10 12:58:28 +0000466 .ndo_vlan_rx_register = gfar_vlan_rx_register,
Ben Hutchings240c1022009-07-09 17:54:35 +0000467 .ndo_set_mac_address = eth_mac_addr,
468 .ndo_validate_addr = eth_validate_addr,
Andy Fleming26ccfc32009-03-10 12:58:28 +0000469#ifdef CONFIG_NET_POLL_CONTROLLER
470 .ndo_poll_controller = gfar_netpoll,
471#endif
472};
473
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000474unsigned int ftp_rqfpr[MAX_FILER_IDX + 1];
475unsigned int ftp_rqfcr[MAX_FILER_IDX + 1];
476
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000477void lock_rx_qs(struct gfar_private *priv)
478{
479 int i = 0x0;
480
481 for (i = 0; i < priv->num_rx_queues; i++)
482 spin_lock(&priv->rx_queue[i]->rxlock);
483}
484
485void lock_tx_qs(struct gfar_private *priv)
486{
487 int i = 0x0;
488
489 for (i = 0; i < priv->num_tx_queues; i++)
490 spin_lock(&priv->tx_queue[i]->txlock);
491}
492
493void unlock_rx_qs(struct gfar_private *priv)
494{
495 int i = 0x0;
496
497 for (i = 0; i < priv->num_rx_queues; i++)
498 spin_unlock(&priv->rx_queue[i]->rxlock);
499}
500
501void unlock_tx_qs(struct gfar_private *priv)
502{
503 int i = 0x0;
504
505 for (i = 0; i < priv->num_tx_queues; i++)
506 spin_unlock(&priv->tx_queue[i]->txlock);
507}
508
Andy Fleming7f7f5312005-11-11 12:38:59 -0600509/* Returns 1 if incoming frames use an FCB */
510static inline int gfar_uses_fcb(struct gfar_private *priv)
Kumar Gala0bbaf062005-06-20 10:54:21 -0500511{
Manfred Rudigiercc772ab2010-04-08 23:10:03 +0000512 return priv->vlgrp || priv->rx_csum_enable ||
513 (priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER);
Kumar Gala0bbaf062005-06-20 10:54:21 -0500514}
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400515
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000516static void free_tx_pointers(struct gfar_private *priv)
517{
518 int i = 0;
519
520 for (i = 0; i < priv->num_tx_queues; i++)
521 kfree(priv->tx_queue[i]);
522}
523
524static void free_rx_pointers(struct gfar_private *priv)
525{
526 int i = 0;
527
528 for (i = 0; i < priv->num_rx_queues; i++)
529 kfree(priv->rx_queue[i]);
530}
531
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000532static void unmap_group_regs(struct gfar_private *priv)
533{
534 int i = 0;
535
536 for (i = 0; i < MAXGROUPS; i++)
537 if (priv->gfargrp[i].regs)
538 iounmap(priv->gfargrp[i].regs);
539}
540
541static void disable_napi(struct gfar_private *priv)
542{
543 int i = 0;
544
545 for (i = 0; i < priv->num_grps; i++)
546 napi_disable(&priv->gfargrp[i].napi);
547}
548
549static void enable_napi(struct gfar_private *priv)
550{
551 int i = 0;
552
553 for (i = 0; i < priv->num_grps; i++)
554 napi_enable(&priv->gfargrp[i].napi);
555}
556
557static int gfar_parse_group(struct device_node *np,
558 struct gfar_private *priv, const char *model)
559{
560 u32 *queue_mask;
561 u64 addr, size;
562
563 addr = of_translate_address(np,
564 of_get_address(np, 0, &size, NULL));
565 priv->gfargrp[priv->num_grps].regs = ioremap(addr, size);
566
567 if (!priv->gfargrp[priv->num_grps].regs)
568 return -ENOMEM;
569
570 priv->gfargrp[priv->num_grps].interruptTransmit =
571 irq_of_parse_and_map(np, 0);
572
573 /* If we aren't the FEC we have multiple interrupts */
574 if (model && strcasecmp(model, "FEC")) {
575 priv->gfargrp[priv->num_grps].interruptReceive =
576 irq_of_parse_and_map(np, 1);
577 priv->gfargrp[priv->num_grps].interruptError =
578 irq_of_parse_and_map(np,2);
579 if (priv->gfargrp[priv->num_grps].interruptTransmit < 0 ||
580 priv->gfargrp[priv->num_grps].interruptReceive < 0 ||
581 priv->gfargrp[priv->num_grps].interruptError < 0) {
582 return -EINVAL;
583 }
584 }
585
586 priv->gfargrp[priv->num_grps].grp_id = priv->num_grps;
587 priv->gfargrp[priv->num_grps].priv = priv;
588 spin_lock_init(&priv->gfargrp[priv->num_grps].grplock);
589 if(priv->mode == MQ_MG_MODE) {
590 queue_mask = (u32 *)of_get_property(np,
591 "fsl,rx-bit-map", NULL);
592 priv->gfargrp[priv->num_grps].rx_bit_map =
593 queue_mask ? *queue_mask :(DEFAULT_MAPPING >> priv->num_grps);
594 queue_mask = (u32 *)of_get_property(np,
595 "fsl,tx-bit-map", NULL);
596 priv->gfargrp[priv->num_grps].tx_bit_map =
597 queue_mask ? *queue_mask : (DEFAULT_MAPPING >> priv->num_grps);
598 } else {
599 priv->gfargrp[priv->num_grps].rx_bit_map = 0xFF;
600 priv->gfargrp[priv->num_grps].tx_bit_map = 0xFF;
601 }
602 priv->num_grps++;
603
604 return 0;
605}
606
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000607static int gfar_of_init(struct of_device *ofdev, struct net_device **pdev)
Andy Flemingb31a1d82008-12-16 15:29:15 -0800608{
Andy Flemingb31a1d82008-12-16 15:29:15 -0800609 const char *model;
610 const char *ctype;
611 const void *mac_addr;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000612 int err = 0, i;
613 struct net_device *dev = NULL;
614 struct gfar_private *priv = NULL;
615 struct device_node *np = ofdev->node;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000616 struct device_node *child = NULL;
Andy Fleming4d7902f2009-02-04 16:43:44 -0800617 const u32 *stash;
618 const u32 *stash_len;
619 const u32 *stash_idx;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000620 unsigned int num_tx_qs, num_rx_qs;
621 u32 *tx_queues, *rx_queues;
Andy Flemingb31a1d82008-12-16 15:29:15 -0800622
623 if (!np || !of_device_is_available(np))
624 return -ENODEV;
625
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000626 /* parse the num of tx and rx queues */
627 tx_queues = (u32 *)of_get_property(np, "fsl,num_tx_queues", NULL);
628 num_tx_qs = tx_queues ? *tx_queues : 1;
629
630 if (num_tx_qs > MAX_TX_QS) {
631 printk(KERN_ERR "num_tx_qs(=%d) greater than MAX_TX_QS(=%d)\n",
632 num_tx_qs, MAX_TX_QS);
633 printk(KERN_ERR "Cannot do alloc_etherdev, aborting\n");
634 return -EINVAL;
635 }
636
637 rx_queues = (u32 *)of_get_property(np, "fsl,num_rx_queues", NULL);
638 num_rx_qs = rx_queues ? *rx_queues : 1;
639
640 if (num_rx_qs > MAX_RX_QS) {
641 printk(KERN_ERR "num_rx_qs(=%d) greater than MAX_RX_QS(=%d)\n",
642 num_tx_qs, MAX_TX_QS);
643 printk(KERN_ERR "Cannot do alloc_etherdev, aborting\n");
644 return -EINVAL;
645 }
646
647 *pdev = alloc_etherdev_mq(sizeof(*priv), num_tx_qs);
648 dev = *pdev;
649 if (NULL == dev)
650 return -ENOMEM;
651
652 priv = netdev_priv(dev);
653 priv->node = ofdev->node;
654 priv->ndev = dev;
655
656 dev->num_tx_queues = num_tx_qs;
657 dev->real_num_tx_queues = num_tx_qs;
658 priv->num_tx_queues = num_tx_qs;
659 priv->num_rx_queues = num_rx_qs;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000660 priv->num_grps = 0x0;
Andy Flemingb31a1d82008-12-16 15:29:15 -0800661
662 model = of_get_property(np, "model", NULL);
663
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000664 for (i = 0; i < MAXGROUPS; i++)
665 priv->gfargrp[i].regs = NULL;
Andy Flemingb31a1d82008-12-16 15:29:15 -0800666
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000667 /* Parse and initialize group specific information */
668 if (of_device_is_compatible(np, "fsl,etsec2")) {
669 priv->mode = MQ_MG_MODE;
670 for_each_child_of_node(np, child) {
671 err = gfar_parse_group(child, priv, model);
672 if (err)
673 goto err_grp_init;
Andy Flemingb31a1d82008-12-16 15:29:15 -0800674 }
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000675 } else {
676 priv->mode = SQ_SG_MODE;
677 err = gfar_parse_group(np, priv, model);
678 if(err)
679 goto err_grp_init;
Andy Flemingb31a1d82008-12-16 15:29:15 -0800680 }
681
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000682 for (i = 0; i < priv->num_tx_queues; i++)
683 priv->tx_queue[i] = NULL;
684 for (i = 0; i < priv->num_rx_queues; i++)
685 priv->rx_queue[i] = NULL;
686
687 for (i = 0; i < priv->num_tx_queues; i++) {
Kim Phillipsed130582010-03-30 11:54:21 +0000688 priv->tx_queue[i] = (struct gfar_priv_tx_q *)kzalloc(
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000689 sizeof (struct gfar_priv_tx_q), GFP_KERNEL);
690 if (!priv->tx_queue[i]) {
691 err = -ENOMEM;
692 goto tx_alloc_failed;
693 }
694 priv->tx_queue[i]->tx_skbuff = NULL;
695 priv->tx_queue[i]->qindex = i;
696 priv->tx_queue[i]->dev = dev;
697 spin_lock_init(&(priv->tx_queue[i]->txlock));
698 }
699
700 for (i = 0; i < priv->num_rx_queues; i++) {
Kim Phillipsed130582010-03-30 11:54:21 +0000701 priv->rx_queue[i] = (struct gfar_priv_rx_q *)kzalloc(
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000702 sizeof (struct gfar_priv_rx_q), GFP_KERNEL);
703 if (!priv->rx_queue[i]) {
704 err = -ENOMEM;
705 goto rx_alloc_failed;
706 }
707 priv->rx_queue[i]->rx_skbuff = NULL;
708 priv->rx_queue[i]->qindex = i;
709 priv->rx_queue[i]->dev = dev;
710 spin_lock_init(&(priv->rx_queue[i]->rxlock));
711 }
712
713
Andy Fleming4d7902f2009-02-04 16:43:44 -0800714 stash = of_get_property(np, "bd-stash", NULL);
715
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000716 if (stash) {
Andy Fleming4d7902f2009-02-04 16:43:44 -0800717 priv->device_flags |= FSL_GIANFAR_DEV_HAS_BD_STASHING;
718 priv->bd_stash_en = 1;
719 }
720
721 stash_len = of_get_property(np, "rx-stash-len", NULL);
722
723 if (stash_len)
724 priv->rx_stash_size = *stash_len;
725
726 stash_idx = of_get_property(np, "rx-stash-idx", NULL);
727
728 if (stash_idx)
729 priv->rx_stash_index = *stash_idx;
730
731 if (stash_len || stash_idx)
732 priv->device_flags |= FSL_GIANFAR_DEV_HAS_BUF_STASHING;
733
Andy Flemingb31a1d82008-12-16 15:29:15 -0800734 mac_addr = of_get_mac_address(np);
735 if (mac_addr)
736 memcpy(dev->dev_addr, mac_addr, MAC_ADDR_LEN);
737
738 if (model && !strcasecmp(model, "TSEC"))
739 priv->device_flags =
740 FSL_GIANFAR_DEV_HAS_GIGABIT |
741 FSL_GIANFAR_DEV_HAS_COALESCE |
742 FSL_GIANFAR_DEV_HAS_RMON |
743 FSL_GIANFAR_DEV_HAS_MULTI_INTR;
744 if (model && !strcasecmp(model, "eTSEC"))
745 priv->device_flags =
746 FSL_GIANFAR_DEV_HAS_GIGABIT |
747 FSL_GIANFAR_DEV_HAS_COALESCE |
748 FSL_GIANFAR_DEV_HAS_RMON |
749 FSL_GIANFAR_DEV_HAS_MULTI_INTR |
Dai Haruki2c2db482008-12-16 15:31:15 -0800750 FSL_GIANFAR_DEV_HAS_PADDING |
Andy Flemingb31a1d82008-12-16 15:29:15 -0800751 FSL_GIANFAR_DEV_HAS_CSUM |
752 FSL_GIANFAR_DEV_HAS_VLAN |
753 FSL_GIANFAR_DEV_HAS_MAGIC_PACKET |
Manfred Rudigiercc772ab2010-04-08 23:10:03 +0000754 FSL_GIANFAR_DEV_HAS_EXTENDED_HASH |
755 FSL_GIANFAR_DEV_HAS_TIMER;
Andy Flemingb31a1d82008-12-16 15:29:15 -0800756
757 ctype = of_get_property(np, "phy-connection-type", NULL);
758
759 /* We only care about rgmii-id. The rest are autodetected */
760 if (ctype && !strcmp(ctype, "rgmii-id"))
761 priv->interface = PHY_INTERFACE_MODE_RGMII_ID;
762 else
763 priv->interface = PHY_INTERFACE_MODE_MII;
764
765 if (of_get_property(np, "fsl,magic-packet", NULL))
766 priv->device_flags |= FSL_GIANFAR_DEV_HAS_MAGIC_PACKET;
767
Grant Likelyfe192a42009-04-25 12:53:12 +0000768 priv->phy_node = of_parse_phandle(np, "phy-handle", 0);
Andy Flemingb31a1d82008-12-16 15:29:15 -0800769
770 /* Find the TBI PHY. If it's not there, we don't support SGMII */
Grant Likelyfe192a42009-04-25 12:53:12 +0000771 priv->tbi_node = of_parse_phandle(np, "tbi-handle", 0);
Andy Flemingb31a1d82008-12-16 15:29:15 -0800772
773 return 0;
774
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000775rx_alloc_failed:
776 free_rx_pointers(priv);
777tx_alloc_failed:
778 free_tx_pointers(priv);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000779err_grp_init:
780 unmap_group_regs(priv);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000781 free_netdev(dev);
Andy Flemingb31a1d82008-12-16 15:29:15 -0800782 return err;
783}
784
Manfred Rudigiercc772ab2010-04-08 23:10:03 +0000785static int gfar_hwtstamp_ioctl(struct net_device *netdev,
786 struct ifreq *ifr, int cmd)
787{
788 struct hwtstamp_config config;
789 struct gfar_private *priv = netdev_priv(netdev);
790
791 if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
792 return -EFAULT;
793
794 /* reserved for future extensions */
795 if (config.flags)
796 return -EINVAL;
797
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +0000798 switch (config.tx_type) {
799 case HWTSTAMP_TX_OFF:
800 priv->hwts_tx_en = 0;
801 break;
802 case HWTSTAMP_TX_ON:
803 if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER))
804 return -ERANGE;
805 priv->hwts_tx_en = 1;
806 break;
807 default:
Manfred Rudigiercc772ab2010-04-08 23:10:03 +0000808 return -ERANGE;
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +0000809 }
Manfred Rudigiercc772ab2010-04-08 23:10:03 +0000810
811 switch (config.rx_filter) {
812 case HWTSTAMP_FILTER_NONE:
813 priv->hwts_rx_en = 0;
814 break;
815 default:
816 if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER))
817 return -ERANGE;
818 priv->hwts_rx_en = 1;
819 config.rx_filter = HWTSTAMP_FILTER_ALL;
820 break;
821 }
822
823 return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
824 -EFAULT : 0;
825}
826
Clifford Wolf0faac9f2009-01-09 10:23:11 +0000827/* Ioctl MII Interface */
828static int gfar_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
829{
830 struct gfar_private *priv = netdev_priv(dev);
831
832 if (!netif_running(dev))
833 return -EINVAL;
834
Manfred Rudigiercc772ab2010-04-08 23:10:03 +0000835 if (cmd == SIOCSHWTSTAMP)
836 return gfar_hwtstamp_ioctl(dev, rq, cmd);
837
Clifford Wolf0faac9f2009-01-09 10:23:11 +0000838 if (!priv->phydev)
839 return -ENODEV;
840
841 return phy_mii_ioctl(priv->phydev, if_mii(rq), cmd);
842}
843
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000844static unsigned int reverse_bitmap(unsigned int bit_map, unsigned int max_qs)
845{
846 unsigned int new_bit_map = 0x0;
847 int mask = 0x1 << (max_qs - 1), i;
848 for (i = 0; i < max_qs; i++) {
849 if (bit_map & mask)
850 new_bit_map = new_bit_map + (1 << i);
851 mask = mask >> 0x1;
852 }
853 return new_bit_map;
854}
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000855
Anton Vorontsov18294ad2009-11-04 12:53:00 +0000856static u32 cluster_entry_per_class(struct gfar_private *priv, u32 rqfar,
857 u32 class)
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000858{
859 u32 rqfpr = FPR_FILER_MASK;
860 u32 rqfcr = 0x0;
861
862 rqfar--;
863 rqfcr = RQFCR_CLE | RQFCR_PID_MASK | RQFCR_CMP_EXACT;
864 ftp_rqfpr[rqfar] = rqfpr;
865 ftp_rqfcr[rqfar] = rqfcr;
866 gfar_write_filer(priv, rqfar, rqfcr, rqfpr);
867
868 rqfar--;
869 rqfcr = RQFCR_CMP_NOMATCH;
870 ftp_rqfpr[rqfar] = rqfpr;
871 ftp_rqfcr[rqfar] = rqfcr;
872 gfar_write_filer(priv, rqfar, rqfcr, rqfpr);
873
874 rqfar--;
875 rqfcr = RQFCR_CMP_EXACT | RQFCR_PID_PARSE | RQFCR_CLE | RQFCR_AND;
876 rqfpr = class;
877 ftp_rqfcr[rqfar] = rqfcr;
878 ftp_rqfpr[rqfar] = rqfpr;
879 gfar_write_filer(priv, rqfar, rqfcr, rqfpr);
880
881 rqfar--;
882 rqfcr = RQFCR_CMP_EXACT | RQFCR_PID_MASK | RQFCR_AND;
883 rqfpr = class;
884 ftp_rqfcr[rqfar] = rqfcr;
885 ftp_rqfpr[rqfar] = rqfpr;
886 gfar_write_filer(priv, rqfar, rqfcr, rqfpr);
887
888 return rqfar;
889}
890
891static void gfar_init_filer_table(struct gfar_private *priv)
892{
893 int i = 0x0;
894 u32 rqfar = MAX_FILER_IDX;
895 u32 rqfcr = 0x0;
896 u32 rqfpr = FPR_FILER_MASK;
897
898 /* Default rule */
899 rqfcr = RQFCR_CMP_MATCH;
900 ftp_rqfcr[rqfar] = rqfcr;
901 ftp_rqfpr[rqfar] = rqfpr;
902 gfar_write_filer(priv, rqfar, rqfcr, rqfpr);
903
904 rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV6);
905 rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV6 | RQFPR_UDP);
906 rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV6 | RQFPR_TCP);
907 rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV4);
908 rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV4 | RQFPR_UDP);
909 rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV4 | RQFPR_TCP);
910
911 /* cur_filer_idx indicated the fisrt non-masked rule */
912 priv->cur_filer_idx = rqfar;
913
914 /* Rest are masked rules */
915 rqfcr = RQFCR_CMP_NOMATCH;
916 for (i = 0; i < rqfar; i++) {
917 ftp_rqfcr[i] = rqfcr;
918 ftp_rqfpr[i] = rqfpr;
919 gfar_write_filer(priv, i, rqfcr, rqfpr);
920 }
921}
922
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400923/* Set up the ethernet device structure, private data,
924 * and anything else we need before we start */
Andy Flemingb31a1d82008-12-16 15:29:15 -0800925static int gfar_probe(struct of_device *ofdev,
926 const struct of_device_id *match)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927{
928 u32 tempval;
929 struct net_device *dev = NULL;
930 struct gfar_private *priv = NULL;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000931 struct gfar __iomem *regs = NULL;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000932 int err = 0, i, grp_idx = 0;
Dai Harukic50a5d92008-12-17 16:51:32 -0800933 int len_devname;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000934 u32 rstat = 0, tstat = 0, rqueue = 0, tqueue = 0;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000935 u32 isrg = 0;
Anton Vorontsov18294ad2009-11-04 12:53:00 +0000936 u32 __iomem *baddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000938 err = gfar_of_init(ofdev, &dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000940 if (err)
941 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
943 priv = netdev_priv(dev);
Kumar Gala48268572009-03-18 23:28:22 -0700944 priv->ndev = dev;
945 priv->ofdev = ofdev;
Andy Flemingb31a1d82008-12-16 15:29:15 -0800946 priv->node = ofdev->node;
Kumar Gala48268572009-03-18 23:28:22 -0700947 SET_NETDEV_DEV(dev, &ofdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
Scott Woodd87eb122008-07-11 18:04:45 -0500949 spin_lock_init(&priv->bflock);
Sebastian Siewiorab939902008-08-19 21:12:45 +0200950 INIT_WORK(&priv->reset_task, gfar_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
Andy Flemingb31a1d82008-12-16 15:29:15 -0800952 dev_set_drvdata(&ofdev->dev, priv);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000953 regs = priv->gfargrp[0].regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
955 /* Stop the DMA engine now, in case it was running before */
956 /* (The firmware could have used it, and left it running). */
Andy Fleming257d9382008-12-16 15:25:45 -0800957 gfar_halt(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
959 /* Reset MAC layer */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000960 gfar_write(&regs->maccfg1, MACCFG1_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
Andy Flemingb98ac702009-02-04 16:38:05 -0800962 /* We need to delay at least 3 TX clocks */
963 udelay(2);
964
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 tempval = (MACCFG1_TX_FLOW | MACCFG1_RX_FLOW);
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000966 gfar_write(&regs->maccfg1, tempval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
968 /* Initialize MACCFG2. */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000969 gfar_write(&regs->maccfg2, MACCFG2_INIT_SETTINGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
971 /* Initialize ECNTRL */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000972 gfar_write(&regs->ecntrl, ECNTRL_INIT_SETTINGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 /* Set the dev->base_addr to the gfar reg region */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000975 dev->base_addr = (unsigned long) regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
Andy Flemingb31a1d82008-12-16 15:29:15 -0800977 SET_NETDEV_DEV(dev, &ofdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
979 /* Fill in the dev structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 dev->watchdog_timeo = TX_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 dev->mtu = 1500;
Andy Fleming26ccfc32009-03-10 12:58:28 +0000982 dev->netdev_ops = &gfar_netdev_ops;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500983 dev->ethtool_ops = &gfar_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000985 /* Register for napi ...We are registering NAPI for each grp */
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000986 for (i = 0; i < priv->num_grps; i++)
987 netif_napi_add(dev, &priv->gfargrp[i].napi, gfar_poll, GFAR_DEV_WEIGHT);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000988
Andy Flemingb31a1d82008-12-16 15:29:15 -0800989 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_CSUM) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500990 priv->rx_csum_enable = 1;
Dai Haruki4669bc92008-12-17 16:51:04 -0800991 dev->features |= NETIF_F_IP_CSUM | NETIF_F_SG | NETIF_F_HIGHDMA;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500992 } else
993 priv->rx_csum_enable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
Kumar Gala0bbaf062005-06-20 10:54:21 -0500995 priv->vlgrp = NULL;
996
Andy Fleming26ccfc32009-03-10 12:58:28 +0000997 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_VLAN)
Kumar Gala0bbaf062005-06-20 10:54:21 -0500998 dev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500999
Andy Flemingb31a1d82008-12-16 15:29:15 -08001000 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_EXTENDED_HASH) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001001 priv->extended_hash = 1;
1002 priv->hash_width = 9;
1003
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001004 priv->hash_regs[0] = &regs->igaddr0;
1005 priv->hash_regs[1] = &regs->igaddr1;
1006 priv->hash_regs[2] = &regs->igaddr2;
1007 priv->hash_regs[3] = &regs->igaddr3;
1008 priv->hash_regs[4] = &regs->igaddr4;
1009 priv->hash_regs[5] = &regs->igaddr5;
1010 priv->hash_regs[6] = &regs->igaddr6;
1011 priv->hash_regs[7] = &regs->igaddr7;
1012 priv->hash_regs[8] = &regs->gaddr0;
1013 priv->hash_regs[9] = &regs->gaddr1;
1014 priv->hash_regs[10] = &regs->gaddr2;
1015 priv->hash_regs[11] = &regs->gaddr3;
1016 priv->hash_regs[12] = &regs->gaddr4;
1017 priv->hash_regs[13] = &regs->gaddr5;
1018 priv->hash_regs[14] = &regs->gaddr6;
1019 priv->hash_regs[15] = &regs->gaddr7;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001020
1021 } else {
1022 priv->extended_hash = 0;
1023 priv->hash_width = 8;
1024
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001025 priv->hash_regs[0] = &regs->gaddr0;
1026 priv->hash_regs[1] = &regs->gaddr1;
1027 priv->hash_regs[2] = &regs->gaddr2;
1028 priv->hash_regs[3] = &regs->gaddr3;
1029 priv->hash_regs[4] = &regs->gaddr4;
1030 priv->hash_regs[5] = &regs->gaddr5;
1031 priv->hash_regs[6] = &regs->gaddr6;
1032 priv->hash_regs[7] = &regs->gaddr7;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001033 }
1034
Andy Flemingb31a1d82008-12-16 15:29:15 -08001035 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_PADDING)
Kumar Gala0bbaf062005-06-20 10:54:21 -05001036 priv->padding = DEFAULT_PADDING;
1037 else
1038 priv->padding = 0;
1039
Manfred Rudigiercc772ab2010-04-08 23:10:03 +00001040 if (dev->features & NETIF_F_IP_CSUM ||
1041 priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER)
Kumar Gala0bbaf062005-06-20 10:54:21 -05001042 dev->hard_header_len += GMAC_FCB_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001044 /* Program the isrg regs only if number of grps > 1 */
1045 if (priv->num_grps > 1) {
1046 baddr = &regs->isrg0;
1047 for (i = 0; i < priv->num_grps; i++) {
1048 isrg |= (priv->gfargrp[i].rx_bit_map << ISRG_SHIFT_RX);
1049 isrg |= (priv->gfargrp[i].tx_bit_map << ISRG_SHIFT_TX);
1050 gfar_write(baddr, isrg);
1051 baddr++;
1052 isrg = 0x0;
1053 }
1054 }
1055
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001056 /* Need to reverse the bit maps as bit_map's MSB is q0
Akinobu Mita984b3f52010-03-05 13:41:37 -08001057 * but, for_each_set_bit parses from right to left, which
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001058 * basically reverses the queue numbers */
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001059 for (i = 0; i< priv->num_grps; i++) {
1060 priv->gfargrp[i].tx_bit_map = reverse_bitmap(
1061 priv->gfargrp[i].tx_bit_map, MAX_TX_QS);
1062 priv->gfargrp[i].rx_bit_map = reverse_bitmap(
1063 priv->gfargrp[i].rx_bit_map, MAX_RX_QS);
1064 }
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001065
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001066 /* Calculate RSTAT, TSTAT, RQUEUE and TQUEUE values,
1067 * also assign queues to groups */
1068 for (grp_idx = 0; grp_idx < priv->num_grps; grp_idx++) {
1069 priv->gfargrp[grp_idx].num_rx_queues = 0x0;
Akinobu Mita984b3f52010-03-05 13:41:37 -08001070 for_each_set_bit(i, &priv->gfargrp[grp_idx].rx_bit_map,
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001071 priv->num_rx_queues) {
1072 priv->gfargrp[grp_idx].num_rx_queues++;
1073 priv->rx_queue[i]->grp = &priv->gfargrp[grp_idx];
1074 rstat = rstat | (RSTAT_CLEAR_RHALT >> i);
1075 rqueue = rqueue | ((RQUEUE_EN0 | RQUEUE_EX0) >> i);
1076 }
1077 priv->gfargrp[grp_idx].num_tx_queues = 0x0;
Akinobu Mita984b3f52010-03-05 13:41:37 -08001078 for_each_set_bit(i, &priv->gfargrp[grp_idx].tx_bit_map,
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001079 priv->num_tx_queues) {
1080 priv->gfargrp[grp_idx].num_tx_queues++;
1081 priv->tx_queue[i]->grp = &priv->gfargrp[grp_idx];
1082 tstat = tstat | (TSTAT_CLEAR_THALT >> i);
1083 tqueue = tqueue | (TQUEUE_EN0 >> i);
1084 }
1085 priv->gfargrp[grp_idx].rstat = rstat;
1086 priv->gfargrp[grp_idx].tstat = tstat;
1087 rstat = tstat =0;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001088 }
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001089
1090 gfar_write(&regs->rqueue, rqueue);
1091 gfar_write(&regs->tqueue, tqueue);
1092
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 priv->rx_buffer_size = DEFAULT_RX_BUFFER_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001095 /* Initializing some of the rx/tx queue level parameters */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001096 for (i = 0; i < priv->num_tx_queues; i++) {
1097 priv->tx_queue[i]->tx_ring_size = DEFAULT_TX_RING_SIZE;
1098 priv->tx_queue[i]->num_txbdfree = DEFAULT_TX_RING_SIZE;
1099 priv->tx_queue[i]->txcoalescing = DEFAULT_TX_COALESCE;
1100 priv->tx_queue[i]->txic = DEFAULT_TXIC;
1101 }
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001102
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001103 for (i = 0; i < priv->num_rx_queues; i++) {
1104 priv->rx_queue[i]->rx_ring_size = DEFAULT_RX_RING_SIZE;
1105 priv->rx_queue[i]->rxcoalescing = DEFAULT_RX_COALESCE;
1106 priv->rx_queue[i]->rxic = DEFAULT_RXIC;
1107 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
Sandeep Gopalpet1ccb8382009-12-16 01:14:58 +00001109 /* enable filer if using multiple RX queues*/
1110 if(priv->num_rx_queues > 1)
1111 priv->rx_filer_enable = 1;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001112 /* Enable most messages by default */
1113 priv->msg_enable = (NETIF_MSG_IFUP << 1 ) - 1;
1114
Trent Piephod3eab822008-10-02 11:12:24 +00001115 /* Carrier starts down, phylib will bring it up */
1116 netif_carrier_off(dev);
1117
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 err = register_netdev(dev);
1119
1120 if (err) {
1121 printk(KERN_ERR "%s: Cannot register net device, aborting.\n",
1122 dev->name);
1123 goto register_fail;
1124 }
1125
Anton Vorontsov2884e5c2009-02-01 00:52:34 -08001126 device_init_wakeup(&dev->dev,
1127 priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
1128
Dai Harukic50a5d92008-12-17 16:51:32 -08001129 /* fill out IRQ number and name fields */
1130 len_devname = strlen(dev->name);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001131 for (i = 0; i < priv->num_grps; i++) {
1132 strncpy(&priv->gfargrp[i].int_name_tx[0], dev->name,
1133 len_devname);
1134 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
1135 strncpy(&priv->gfargrp[i].int_name_tx[len_devname],
1136 "_g", sizeof("_g"));
1137 priv->gfargrp[i].int_name_tx[
1138 strlen(priv->gfargrp[i].int_name_tx)] = i+48;
1139 strncpy(&priv->gfargrp[i].int_name_tx[strlen(
1140 priv->gfargrp[i].int_name_tx)],
1141 "_tx", sizeof("_tx") + 1);
Dai Harukic50a5d92008-12-17 16:51:32 -08001142
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001143 strncpy(&priv->gfargrp[i].int_name_rx[0], dev->name,
1144 len_devname);
1145 strncpy(&priv->gfargrp[i].int_name_rx[len_devname],
1146 "_g", sizeof("_g"));
1147 priv->gfargrp[i].int_name_rx[
1148 strlen(priv->gfargrp[i].int_name_rx)] = i+48;
1149 strncpy(&priv->gfargrp[i].int_name_rx[strlen(
1150 priv->gfargrp[i].int_name_rx)],
1151 "_rx", sizeof("_rx") + 1);
Dai Harukic50a5d92008-12-17 16:51:32 -08001152
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001153 strncpy(&priv->gfargrp[i].int_name_er[0], dev->name,
1154 len_devname);
1155 strncpy(&priv->gfargrp[i].int_name_er[len_devname],
1156 "_g", sizeof("_g"));
1157 priv->gfargrp[i].int_name_er[strlen(
1158 priv->gfargrp[i].int_name_er)] = i+48;
1159 strncpy(&priv->gfargrp[i].int_name_er[strlen(\
1160 priv->gfargrp[i].int_name_er)],
1161 "_er", sizeof("_er") + 1);
1162 } else
1163 priv->gfargrp[i].int_name_tx[len_devname] = '\0';
1164 }
Dai Harukic50a5d92008-12-17 16:51:32 -08001165
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +00001166 /* Initialize the filer table */
1167 gfar_init_filer_table(priv);
1168
Andy Fleming7f7f5312005-11-11 12:38:59 -06001169 /* Create all the sysfs files */
1170 gfar_init_sysfs(dev);
1171
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 /* Print out the device info */
Johannes Berge1749612008-10-27 15:59:26 -07001173 printk(KERN_INFO DEVICE_NAME "%pM\n", dev->name, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
1175 /* Even more device info helps when determining which kernel */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001176 /* provided which set of benchmarks. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 printk(KERN_INFO "%s: Running with NAPI enabled\n", dev->name);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001178 for (i = 0; i < priv->num_rx_queues; i++)
Kim Phillipsddc01b32010-03-30 11:54:22 +00001179 printk(KERN_INFO "%s: RX BD ring size for Q[%d]: %d\n",
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001180 dev->name, i, priv->rx_queue[i]->rx_ring_size);
1181 for(i = 0; i < priv->num_tx_queues; i++)
Kim Phillipsddc01b32010-03-30 11:54:22 +00001182 printk(KERN_INFO "%s: TX BD ring size for Q[%d]: %d\n",
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001183 dev->name, i, priv->tx_queue[i]->tx_ring_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
1185 return 0;
1186
1187register_fail:
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001188 unmap_group_regs(priv);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001189 free_tx_pointers(priv);
1190 free_rx_pointers(priv);
Grant Likelyfe192a42009-04-25 12:53:12 +00001191 if (priv->phy_node)
1192 of_node_put(priv->phy_node);
1193 if (priv->tbi_node)
1194 of_node_put(priv->tbi_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 free_netdev(dev);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001196 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197}
1198
Andy Flemingb31a1d82008-12-16 15:29:15 -08001199static int gfar_remove(struct of_device *ofdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200{
Andy Flemingb31a1d82008-12-16 15:29:15 -08001201 struct gfar_private *priv = dev_get_drvdata(&ofdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
Grant Likelyfe192a42009-04-25 12:53:12 +00001203 if (priv->phy_node)
1204 of_node_put(priv->phy_node);
1205 if (priv->tbi_node)
1206 of_node_put(priv->tbi_node);
1207
Andy Flemingb31a1d82008-12-16 15:29:15 -08001208 dev_set_drvdata(&ofdev->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
David S. Millerd9d8e042009-09-06 01:41:02 -07001210 unregister_netdev(priv->ndev);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001211 unmap_group_regs(priv);
Kumar Gala48268572009-03-18 23:28:22 -07001212 free_netdev(priv->ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
1214 return 0;
1215}
1216
Scott Woodd87eb122008-07-11 18:04:45 -05001217#ifdef CONFIG_PM
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001218
1219static int gfar_suspend(struct device *dev)
Scott Woodd87eb122008-07-11 18:04:45 -05001220{
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001221 struct gfar_private *priv = dev_get_drvdata(dev);
1222 struct net_device *ndev = priv->ndev;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001223 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Scott Woodd87eb122008-07-11 18:04:45 -05001224 unsigned long flags;
1225 u32 tempval;
1226
1227 int magic_packet = priv->wol_en &&
Andy Flemingb31a1d82008-12-16 15:29:15 -08001228 (priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
Scott Woodd87eb122008-07-11 18:04:45 -05001229
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001230 netif_device_detach(ndev);
Scott Woodd87eb122008-07-11 18:04:45 -05001231
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001232 if (netif_running(ndev)) {
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001233
1234 local_irq_save(flags);
1235 lock_tx_qs(priv);
1236 lock_rx_qs(priv);
Scott Woodd87eb122008-07-11 18:04:45 -05001237
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001238 gfar_halt_nodisable(ndev);
Scott Woodd87eb122008-07-11 18:04:45 -05001239
1240 /* Disable Tx, and Rx if wake-on-LAN is disabled. */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001241 tempval = gfar_read(&regs->maccfg1);
Scott Woodd87eb122008-07-11 18:04:45 -05001242
1243 tempval &= ~MACCFG1_TX_EN;
1244
1245 if (!magic_packet)
1246 tempval &= ~MACCFG1_RX_EN;
1247
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001248 gfar_write(&regs->maccfg1, tempval);
Scott Woodd87eb122008-07-11 18:04:45 -05001249
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001250 unlock_rx_qs(priv);
1251 unlock_tx_qs(priv);
1252 local_irq_restore(flags);
Scott Woodd87eb122008-07-11 18:04:45 -05001253
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001254 disable_napi(priv);
Scott Woodd87eb122008-07-11 18:04:45 -05001255
1256 if (magic_packet) {
1257 /* Enable interrupt on Magic Packet */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001258 gfar_write(&regs->imask, IMASK_MAG);
Scott Woodd87eb122008-07-11 18:04:45 -05001259
1260 /* Enable Magic Packet mode */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001261 tempval = gfar_read(&regs->maccfg2);
Scott Woodd87eb122008-07-11 18:04:45 -05001262 tempval |= MACCFG2_MPEN;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001263 gfar_write(&regs->maccfg2, tempval);
Scott Woodd87eb122008-07-11 18:04:45 -05001264 } else {
1265 phy_stop(priv->phydev);
1266 }
1267 }
1268
1269 return 0;
1270}
1271
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001272static int gfar_resume(struct device *dev)
Scott Woodd87eb122008-07-11 18:04:45 -05001273{
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001274 struct gfar_private *priv = dev_get_drvdata(dev);
1275 struct net_device *ndev = priv->ndev;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001276 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Scott Woodd87eb122008-07-11 18:04:45 -05001277 unsigned long flags;
1278 u32 tempval;
1279 int magic_packet = priv->wol_en &&
Andy Flemingb31a1d82008-12-16 15:29:15 -08001280 (priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
Scott Woodd87eb122008-07-11 18:04:45 -05001281
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001282 if (!netif_running(ndev)) {
1283 netif_device_attach(ndev);
Scott Woodd87eb122008-07-11 18:04:45 -05001284 return 0;
1285 }
1286
1287 if (!magic_packet && priv->phydev)
1288 phy_start(priv->phydev);
1289
1290 /* Disable Magic Packet mode, in case something
1291 * else woke us up.
1292 */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001293 local_irq_save(flags);
1294 lock_tx_qs(priv);
1295 lock_rx_qs(priv);
Scott Woodd87eb122008-07-11 18:04:45 -05001296
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001297 tempval = gfar_read(&regs->maccfg2);
Scott Woodd87eb122008-07-11 18:04:45 -05001298 tempval &= ~MACCFG2_MPEN;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001299 gfar_write(&regs->maccfg2, tempval);
Scott Woodd87eb122008-07-11 18:04:45 -05001300
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001301 gfar_start(ndev);
Scott Woodd87eb122008-07-11 18:04:45 -05001302
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001303 unlock_rx_qs(priv);
1304 unlock_tx_qs(priv);
1305 local_irq_restore(flags);
Scott Woodd87eb122008-07-11 18:04:45 -05001306
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001307 netif_device_attach(ndev);
Scott Woodd87eb122008-07-11 18:04:45 -05001308
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001309 enable_napi(priv);
Scott Woodd87eb122008-07-11 18:04:45 -05001310
1311 return 0;
1312}
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001313
1314static int gfar_restore(struct device *dev)
1315{
1316 struct gfar_private *priv = dev_get_drvdata(dev);
1317 struct net_device *ndev = priv->ndev;
1318
1319 if (!netif_running(ndev))
1320 return 0;
1321
1322 gfar_init_bds(ndev);
1323 init_registers(ndev);
1324 gfar_set_mac_address(ndev);
1325 gfar_init_mac(ndev);
1326 gfar_start(ndev);
1327
1328 priv->oldlink = 0;
1329 priv->oldspeed = 0;
1330 priv->oldduplex = -1;
1331
1332 if (priv->phydev)
1333 phy_start(priv->phydev);
1334
1335 netif_device_attach(ndev);
Anton Vorontsov5ea681d2009-11-10 14:11:05 +00001336 enable_napi(priv);
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001337
1338 return 0;
1339}
1340
1341static struct dev_pm_ops gfar_pm_ops = {
1342 .suspend = gfar_suspend,
1343 .resume = gfar_resume,
1344 .freeze = gfar_suspend,
1345 .thaw = gfar_resume,
1346 .restore = gfar_restore,
1347};
1348
1349#define GFAR_PM_OPS (&gfar_pm_ops)
1350
1351static int gfar_legacy_suspend(struct of_device *ofdev, pm_message_t state)
1352{
1353 return gfar_suspend(&ofdev->dev);
1354}
1355
1356static int gfar_legacy_resume(struct of_device *ofdev)
1357{
1358 return gfar_resume(&ofdev->dev);
1359}
1360
Scott Woodd87eb122008-07-11 18:04:45 -05001361#else
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001362
1363#define GFAR_PM_OPS NULL
1364#define gfar_legacy_suspend NULL
1365#define gfar_legacy_resume NULL
1366
Scott Woodd87eb122008-07-11 18:04:45 -05001367#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001369/* Reads the controller's registers to determine what interface
1370 * connects it to the PHY.
1371 */
1372static phy_interface_t gfar_get_interface(struct net_device *dev)
1373{
1374 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001375 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001376 u32 ecntrl;
1377
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001378 ecntrl = gfar_read(&regs->ecntrl);
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001379
1380 if (ecntrl & ECNTRL_SGMII_MODE)
1381 return PHY_INTERFACE_MODE_SGMII;
1382
1383 if (ecntrl & ECNTRL_TBI_MODE) {
1384 if (ecntrl & ECNTRL_REDUCED_MODE)
1385 return PHY_INTERFACE_MODE_RTBI;
1386 else
1387 return PHY_INTERFACE_MODE_TBI;
1388 }
1389
1390 if (ecntrl & ECNTRL_REDUCED_MODE) {
1391 if (ecntrl & ECNTRL_REDUCED_MII_MODE)
1392 return PHY_INTERFACE_MODE_RMII;
Andy Fleming7132ab72007-07-11 11:43:07 -05001393 else {
Andy Flemingb31a1d82008-12-16 15:29:15 -08001394 phy_interface_t interface = priv->interface;
Andy Fleming7132ab72007-07-11 11:43:07 -05001395
1396 /*
1397 * This isn't autodetected right now, so it must
1398 * be set by the device tree or platform code.
1399 */
1400 if (interface == PHY_INTERFACE_MODE_RGMII_ID)
1401 return PHY_INTERFACE_MODE_RGMII_ID;
1402
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001403 return PHY_INTERFACE_MODE_RGMII;
Andy Fleming7132ab72007-07-11 11:43:07 -05001404 }
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001405 }
1406
Andy Flemingb31a1d82008-12-16 15:29:15 -08001407 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT)
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001408 return PHY_INTERFACE_MODE_GMII;
1409
1410 return PHY_INTERFACE_MODE_MII;
1411}
1412
1413
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001414/* Initializes driver's PHY state, and attaches to the PHY.
1415 * Returns 0 on success.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 */
1417static int init_phy(struct net_device *dev)
1418{
1419 struct gfar_private *priv = netdev_priv(dev);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001420 uint gigabit_support =
Andy Flemingb31a1d82008-12-16 15:29:15 -08001421 priv->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT ?
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001422 SUPPORTED_1000baseT_Full : 0;
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001423 phy_interface_t interface;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
1425 priv->oldlink = 0;
1426 priv->oldspeed = 0;
1427 priv->oldduplex = -1;
1428
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001429 interface = gfar_get_interface(dev);
1430
Anton Vorontsov1db780f2009-07-16 21:31:42 +00001431 priv->phydev = of_phy_connect(dev, priv->phy_node, &adjust_link, 0,
1432 interface);
1433 if (!priv->phydev)
1434 priv->phydev = of_phy_connect_fixed_link(dev, &adjust_link,
1435 interface);
1436 if (!priv->phydev) {
1437 dev_err(&dev->dev, "could not attach to PHY\n");
1438 return -ENODEV;
Grant Likelyfe192a42009-04-25 12:53:12 +00001439 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
Kapil Junejad3c12872007-05-11 18:25:11 -05001441 if (interface == PHY_INTERFACE_MODE_SGMII)
1442 gfar_configure_serdes(dev);
1443
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001444 /* Remove any features not supported by the controller */
Grant Likelyfe192a42009-04-25 12:53:12 +00001445 priv->phydev->supported &= (GFAR_SUPPORTED | gigabit_support);
1446 priv->phydev->advertising = priv->phydev->supported;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
1448 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449}
1450
Paul Gortmakerd0313582008-04-17 00:08:10 -04001451/*
1452 * Initialize TBI PHY interface for communicating with the
1453 * SERDES lynx PHY on the chip. We communicate with this PHY
1454 * through the MDIO bus on each controller, treating it as a
1455 * "normal" PHY at the address found in the TBIPA register. We assume
1456 * that the TBIPA register is valid. Either the MDIO bus code will set
1457 * it to a value that doesn't conflict with other PHYs on the bus, or the
1458 * value doesn't matter, as there are no other PHYs on the bus.
1459 */
Kapil Junejad3c12872007-05-11 18:25:11 -05001460static void gfar_configure_serdes(struct net_device *dev)
1461{
1462 struct gfar_private *priv = netdev_priv(dev);
Grant Likelyfe192a42009-04-25 12:53:12 +00001463 struct phy_device *tbiphy;
Trent Piephoc1324192008-10-30 18:17:06 -07001464
Grant Likelyfe192a42009-04-25 12:53:12 +00001465 if (!priv->tbi_node) {
1466 dev_warn(&dev->dev, "error: SGMII mode requires that the "
1467 "device tree specify a tbi-handle\n");
1468 return;
1469 }
1470
1471 tbiphy = of_phy_find_device(priv->tbi_node);
1472 if (!tbiphy) {
1473 dev_err(&dev->dev, "error: Could not get TBI device\n");
Andy Flemingb31a1d82008-12-16 15:29:15 -08001474 return;
1475 }
Kapil Junejad3c12872007-05-11 18:25:11 -05001476
Andy Flemingb31a1d82008-12-16 15:29:15 -08001477 /*
1478 * If the link is already up, we must already be ok, and don't need to
Trent Piephobdb59f92008-10-30 18:17:07 -07001479 * configure and reset the TBI<->SerDes link. Maybe U-Boot configured
1480 * everything for us? Resetting it takes the link down and requires
1481 * several seconds for it to come back.
1482 */
Grant Likelyfe192a42009-04-25 12:53:12 +00001483 if (phy_read(tbiphy, MII_BMSR) & BMSR_LSTATUS)
Andy Flemingb31a1d82008-12-16 15:29:15 -08001484 return;
Kapil Junejad3c12872007-05-11 18:25:11 -05001485
Paul Gortmakerd0313582008-04-17 00:08:10 -04001486 /* Single clk mode, mii mode off(for serdes communication) */
Grant Likelyfe192a42009-04-25 12:53:12 +00001487 phy_write(tbiphy, MII_TBICON, TBICON_CLK_SELECT);
Kapil Junejad3c12872007-05-11 18:25:11 -05001488
Grant Likelyfe192a42009-04-25 12:53:12 +00001489 phy_write(tbiphy, MII_ADVERTISE,
Kapil Junejad3c12872007-05-11 18:25:11 -05001490 ADVERTISE_1000XFULL | ADVERTISE_1000XPAUSE |
1491 ADVERTISE_1000XPSE_ASYM);
1492
Grant Likelyfe192a42009-04-25 12:53:12 +00001493 phy_write(tbiphy, MII_BMCR, BMCR_ANENABLE |
Kapil Junejad3c12872007-05-11 18:25:11 -05001494 BMCR_ANRESTART | BMCR_FULLDPLX | BMCR_SPEED1000);
1495}
1496
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497static void init_registers(struct net_device *dev)
1498{
1499 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001500 struct gfar __iomem *regs = NULL;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001501 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001503 for (i = 0; i < priv->num_grps; i++) {
1504 regs = priv->gfargrp[i].regs;
1505 /* Clear IEVENT */
1506 gfar_write(&regs->ievent, IEVENT_INIT_CLEAR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001508 /* Initialize IMASK */
1509 gfar_write(&regs->imask, IMASK_INIT_CLEAR);
1510 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001512 regs = priv->gfargrp[0].regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 /* Init hash registers to zero */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001514 gfar_write(&regs->igaddr0, 0);
1515 gfar_write(&regs->igaddr1, 0);
1516 gfar_write(&regs->igaddr2, 0);
1517 gfar_write(&regs->igaddr3, 0);
1518 gfar_write(&regs->igaddr4, 0);
1519 gfar_write(&regs->igaddr5, 0);
1520 gfar_write(&regs->igaddr6, 0);
1521 gfar_write(&regs->igaddr7, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001523 gfar_write(&regs->gaddr0, 0);
1524 gfar_write(&regs->gaddr1, 0);
1525 gfar_write(&regs->gaddr2, 0);
1526 gfar_write(&regs->gaddr3, 0);
1527 gfar_write(&regs->gaddr4, 0);
1528 gfar_write(&regs->gaddr5, 0);
1529 gfar_write(&regs->gaddr6, 0);
1530 gfar_write(&regs->gaddr7, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 /* Zero out the rmon mib registers if it has them */
Andy Flemingb31a1d82008-12-16 15:29:15 -08001533 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_RMON) {
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001534 memset_io(&(regs->rmon), 0, sizeof (struct rmon_mib));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
1536 /* Mask off the CAM interrupts */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001537 gfar_write(&regs->rmon.cam1, 0xffffffff);
1538 gfar_write(&regs->rmon.cam2, 0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 }
1540
1541 /* Initialize the max receive buffer length */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001542 gfar_write(&regs->mrblr, priv->rx_buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 /* Initialize the Minimum Frame Length Register */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001545 gfar_write(&regs->minflr, MINFLR_INIT_SETTINGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546}
1547
Kumar Gala0bbaf062005-06-20 10:54:21 -05001548
1549/* Halt the receive and transmit queues */
Scott Woodd87eb122008-07-11 18:04:45 -05001550static void gfar_halt_nodisable(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551{
1552 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001553 struct gfar __iomem *regs = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 u32 tempval;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001555 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001557 for (i = 0; i < priv->num_grps; i++) {
1558 regs = priv->gfargrp[i].regs;
1559 /* Mask all interrupts */
1560 gfar_write(&regs->imask, IMASK_INIT_CLEAR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001562 /* Clear all interrupts */
1563 gfar_write(&regs->ievent, IEVENT_INIT_CLEAR);
1564 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001566 regs = priv->gfargrp[0].regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 /* Stop the DMA, and wait for it to stop */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001568 tempval = gfar_read(&regs->dmactrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 if ((tempval & (DMACTRL_GRS | DMACTRL_GTS))
1570 != (DMACTRL_GRS | DMACTRL_GTS)) {
1571 tempval |= (DMACTRL_GRS | DMACTRL_GTS);
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001572 gfar_write(&regs->dmactrl, tempval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001574 while (!(gfar_read(&regs->ievent) &
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 (IEVENT_GRSC | IEVENT_GTSC)))
1576 cpu_relax();
1577 }
Scott Woodd87eb122008-07-11 18:04:45 -05001578}
Scott Woodd87eb122008-07-11 18:04:45 -05001579
1580/* Halt the receive and transmit queues */
1581void gfar_halt(struct net_device *dev)
1582{
1583 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001584 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Scott Woodd87eb122008-07-11 18:04:45 -05001585 u32 tempval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
Scott Wood2a54adc2008-08-12 15:10:46 -05001587 gfar_halt_nodisable(dev);
1588
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 /* Disable Rx and Tx */
1590 tempval = gfar_read(&regs->maccfg1);
1591 tempval &= ~(MACCFG1_RX_EN | MACCFG1_TX_EN);
1592 gfar_write(&regs->maccfg1, tempval);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001593}
1594
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001595static void free_grp_irqs(struct gfar_priv_grp *grp)
1596{
1597 free_irq(grp->interruptError, grp);
1598 free_irq(grp->interruptTransmit, grp);
1599 free_irq(grp->interruptReceive, grp);
1600}
1601
Kumar Gala0bbaf062005-06-20 10:54:21 -05001602void stop_gfar(struct net_device *dev)
1603{
1604 struct gfar_private *priv = netdev_priv(dev);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001605 unsigned long flags;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001606 int i;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001607
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001608 phy_stop(priv->phydev);
1609
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001610
Kumar Gala0bbaf062005-06-20 10:54:21 -05001611 /* Lock it down */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001612 local_irq_save(flags);
1613 lock_tx_qs(priv);
1614 lock_rx_qs(priv);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001615
Kumar Gala0bbaf062005-06-20 10:54:21 -05001616 gfar_halt(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001618 unlock_rx_qs(priv);
1619 unlock_tx_qs(priv);
1620 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621
1622 /* Free the IRQs */
Andy Flemingb31a1d82008-12-16 15:29:15 -08001623 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001624 for (i = 0; i < priv->num_grps; i++)
1625 free_grp_irqs(&priv->gfargrp[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 } else {
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001627 for (i = 0; i < priv->num_grps; i++)
1628 free_irq(priv->gfargrp[i].interruptTransmit,
1629 &priv->gfargrp[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 }
1631
1632 free_skb_resources(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633}
1634
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001635static void free_skb_tx_queue(struct gfar_priv_tx_q *tx_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 struct txbd8 *txbdp;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001638 struct gfar_private *priv = netdev_priv(tx_queue->dev);
Dai Haruki4669bc92008-12-17 16:51:04 -08001639 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001641 txbdp = tx_queue->tx_bd_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001643 for (i = 0; i < tx_queue->tx_ring_size; i++) {
1644 if (!tx_queue->tx_skbuff[i])
Dai Haruki4669bc92008-12-17 16:51:04 -08001645 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
Kumar Gala48268572009-03-18 23:28:22 -07001647 dma_unmap_single(&priv->ofdev->dev, txbdp->bufPtr,
Dai Haruki4669bc92008-12-17 16:51:04 -08001648 txbdp->length, DMA_TO_DEVICE);
1649 txbdp->lstatus = 0;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001650 for (j = 0; j < skb_shinfo(tx_queue->tx_skbuff[i])->nr_frags;
1651 j++) {
Dai Haruki4669bc92008-12-17 16:51:04 -08001652 txbdp++;
Kumar Gala48268572009-03-18 23:28:22 -07001653 dma_unmap_page(&priv->ofdev->dev, txbdp->bufPtr,
Dai Haruki4669bc92008-12-17 16:51:04 -08001654 txbdp->length, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 }
Andy Flemingad5da7a2008-05-07 13:20:55 -05001656 txbdp++;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001657 dev_kfree_skb_any(tx_queue->tx_skbuff[i]);
1658 tx_queue->tx_skbuff[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 }
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001660 kfree(tx_queue->tx_skbuff);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001661}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001663static void free_skb_rx_queue(struct gfar_priv_rx_q *rx_queue)
1664{
1665 struct rxbd8 *rxbdp;
1666 struct gfar_private *priv = netdev_priv(rx_queue->dev);
1667 int i;
1668
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001669 rxbdp = rx_queue->rx_bd_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001671 for (i = 0; i < rx_queue->rx_ring_size; i++) {
1672 if (rx_queue->rx_skbuff[i]) {
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001673 dma_unmap_single(&priv->ofdev->dev,
1674 rxbdp->bufPtr, priv->rx_buffer_size,
Anton Vorontsove69edd22009-10-12 06:00:30 +00001675 DMA_FROM_DEVICE);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001676 dev_kfree_skb_any(rx_queue->rx_skbuff[i]);
1677 rx_queue->rx_skbuff[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 }
Anton Vorontsove69edd22009-10-12 06:00:30 +00001679 rxbdp->lstatus = 0;
1680 rxbdp->bufPtr = 0;
1681 rxbdp++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 }
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001683 kfree(rx_queue->rx_skbuff);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001684}
Anton Vorontsove69edd22009-10-12 06:00:30 +00001685
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001686/* If there are any tx skbs or rx skbs still around, free them.
1687 * Then free tx_skbuff and rx_skbuff */
1688static void free_skb_resources(struct gfar_private *priv)
1689{
1690 struct gfar_priv_tx_q *tx_queue = NULL;
1691 struct gfar_priv_rx_q *rx_queue = NULL;
1692 int i;
1693
1694 /* Go through all the buffer descriptors and free their data buffers */
1695 for (i = 0; i < priv->num_tx_queues; i++) {
1696 tx_queue = priv->tx_queue[i];
Andy Fleming7c0d10d2010-03-29 15:42:23 +00001697 if(tx_queue->tx_skbuff)
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001698 free_skb_tx_queue(tx_queue);
1699 }
1700
1701 for (i = 0; i < priv->num_rx_queues; i++) {
1702 rx_queue = priv->rx_queue[i];
Andy Fleming7c0d10d2010-03-29 15:42:23 +00001703 if(rx_queue->rx_skbuff)
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001704 free_skb_rx_queue(rx_queue);
1705 }
1706
1707 dma_free_coherent(&priv->ofdev->dev,
1708 sizeof(struct txbd8) * priv->total_tx_ring_size +
1709 sizeof(struct rxbd8) * priv->total_rx_ring_size,
1710 priv->tx_queue[0]->tx_bd_base,
1711 priv->tx_queue[0]->tx_bd_dma_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712}
1713
Kumar Gala0bbaf062005-06-20 10:54:21 -05001714void gfar_start(struct net_device *dev)
1715{
1716 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001717 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001718 u32 tempval;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001719 int i = 0;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001720
1721 /* Enable Rx and Tx in MACCFG1 */
1722 tempval = gfar_read(&regs->maccfg1);
1723 tempval |= (MACCFG1_RX_EN | MACCFG1_TX_EN);
1724 gfar_write(&regs->maccfg1, tempval);
1725
1726 /* Initialize DMACTRL to have WWR and WOP */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001727 tempval = gfar_read(&regs->dmactrl);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001728 tempval |= DMACTRL_INIT_SETTINGS;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001729 gfar_write(&regs->dmactrl, tempval);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001730
Kumar Gala0bbaf062005-06-20 10:54:21 -05001731 /* Make sure we aren't stopped */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001732 tempval = gfar_read(&regs->dmactrl);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001733 tempval &= ~(DMACTRL_GRS | DMACTRL_GTS);
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001734 gfar_write(&regs->dmactrl, tempval);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001735
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001736 for (i = 0; i < priv->num_grps; i++) {
1737 regs = priv->gfargrp[i].regs;
1738 /* Clear THLT/RHLT, so that the DMA starts polling now */
1739 gfar_write(&regs->tstat, priv->gfargrp[i].tstat);
1740 gfar_write(&regs->rstat, priv->gfargrp[i].rstat);
1741 /* Unmask the interrupts we look for */
1742 gfar_write(&regs->imask, IMASK_DEFAULT);
1743 }
Dai Haruki12dea572008-12-16 15:30:20 -08001744
1745 dev->trans_start = jiffies;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001746}
1747
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001748void gfar_configure_coalescing(struct gfar_private *priv,
Anton Vorontsov18294ad2009-11-04 12:53:00 +00001749 unsigned long tx_mask, unsigned long rx_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750{
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001751 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Anton Vorontsov18294ad2009-11-04 12:53:00 +00001752 u32 __iomem *baddr;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001753 int i = 0;
1754
1755 /* Backward compatible case ---- even if we enable
1756 * multiple queues, there's only single reg to program
1757 */
1758 gfar_write(&regs->txic, 0);
1759 if(likely(priv->tx_queue[0]->txcoalescing))
1760 gfar_write(&regs->txic, priv->tx_queue[0]->txic);
1761
1762 gfar_write(&regs->rxic, 0);
1763 if(unlikely(priv->rx_queue[0]->rxcoalescing))
1764 gfar_write(&regs->rxic, priv->rx_queue[0]->rxic);
1765
1766 if (priv->mode == MQ_MG_MODE) {
1767 baddr = &regs->txic0;
Akinobu Mita984b3f52010-03-05 13:41:37 -08001768 for_each_set_bit(i, &tx_mask, priv->num_tx_queues) {
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001769 if (likely(priv->tx_queue[i]->txcoalescing)) {
1770 gfar_write(baddr + i, 0);
1771 gfar_write(baddr + i, priv->tx_queue[i]->txic);
1772 }
1773 }
1774
1775 baddr = &regs->rxic0;
Akinobu Mita984b3f52010-03-05 13:41:37 -08001776 for_each_set_bit(i, &rx_mask, priv->num_rx_queues) {
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001777 if (likely(priv->rx_queue[i]->rxcoalescing)) {
1778 gfar_write(baddr + i, 0);
1779 gfar_write(baddr + i, priv->rx_queue[i]->rxic);
1780 }
1781 }
1782 }
1783}
1784
1785static int register_grp_irqs(struct gfar_priv_grp *grp)
1786{
1787 struct gfar_private *priv = grp->priv;
1788 struct net_device *dev = priv->ndev;
Anton Vorontsovccc05c62009-10-12 06:00:26 +00001789 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 /* If the device has multiple interrupts, register for
1792 * them. Otherwise, only register for the one */
Andy Flemingb31a1d82008-12-16 15:29:15 -08001793 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001794 /* Install our interrupt handlers for Error,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 * Transmit, and Receive */
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001796 if ((err = request_irq(grp->interruptError, gfar_error, 0,
1797 grp->int_name_er,grp)) < 0) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001798 if (netif_msg_intr(priv))
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001799 printk(KERN_ERR "%s: Can't get IRQ %d\n",
1800 dev->name, grp->interruptError);
1801
1802 goto err_irq_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 }
1804
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001805 if ((err = request_irq(grp->interruptTransmit, gfar_transmit,
1806 0, grp->int_name_tx, grp)) < 0) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001807 if (netif_msg_intr(priv))
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001808 printk(KERN_ERR "%s: Can't get IRQ %d\n",
1809 dev->name, grp->interruptTransmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 goto tx_irq_fail;
1811 }
1812
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001813 if ((err = request_irq(grp->interruptReceive, gfar_receive, 0,
1814 grp->int_name_rx, grp)) < 0) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001815 if (netif_msg_intr(priv))
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001816 printk(KERN_ERR "%s: Can't get IRQ %d\n",
1817 dev->name, grp->interruptReceive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 goto rx_irq_fail;
1819 }
1820 } else {
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001821 if ((err = request_irq(grp->interruptTransmit, gfar_interrupt, 0,
1822 grp->int_name_tx, grp)) < 0) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001823 if (netif_msg_intr(priv))
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001824 printk(KERN_ERR "%s: Can't get IRQ %d\n",
1825 dev->name, grp->interruptTransmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 goto err_irq_fail;
1827 }
1828 }
1829
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001830 return 0;
1831
1832rx_irq_fail:
1833 free_irq(grp->interruptTransmit, grp);
1834tx_irq_fail:
1835 free_irq(grp->interruptError, grp);
1836err_irq_fail:
1837 return err;
1838
1839}
1840
1841/* Bring the controller up and running */
1842int startup_gfar(struct net_device *ndev)
1843{
1844 struct gfar_private *priv = netdev_priv(ndev);
1845 struct gfar __iomem *regs = NULL;
1846 int err, i, j;
1847
1848 for (i = 0; i < priv->num_grps; i++) {
1849 regs= priv->gfargrp[i].regs;
1850 gfar_write(&regs->imask, IMASK_INIT_CLEAR);
1851 }
1852
1853 regs= priv->gfargrp[0].regs;
1854 err = gfar_alloc_skb_resources(ndev);
1855 if (err)
1856 return err;
1857
1858 gfar_init_mac(ndev);
1859
1860 for (i = 0; i < priv->num_grps; i++) {
1861 err = register_grp_irqs(&priv->gfargrp[i]);
1862 if (err) {
1863 for (j = 0; j < i; j++)
1864 free_grp_irqs(&priv->gfargrp[j]);
1865 goto irq_fail;
1866 }
1867 }
1868
Andy Fleming7f7f5312005-11-11 12:38:59 -06001869 /* Start the controller */
Anton Vorontsovccc05c62009-10-12 06:00:26 +00001870 gfar_start(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871
Anton Vorontsov826aa4a2009-10-12 06:00:34 +00001872 phy_start(priv->phydev);
1873
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001874 gfar_configure_coalescing(priv, 0xFF, 0xFF);
1875
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 return 0;
1877
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001878irq_fail:
Anton Vorontsove69edd22009-10-12 06:00:30 +00001879 free_skb_resources(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 return err;
1881}
1882
1883/* Called when something needs to use the ethernet device */
1884/* Returns 0 for success. */
1885static int gfar_enet_open(struct net_device *dev)
1886{
Li Yang94e8cc32007-10-12 21:53:51 +08001887 struct gfar_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 int err;
1889
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001890 enable_napi(priv);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001891
Andy Fleming0fd56bb2009-02-04 16:43:16 -08001892 skb_queue_head_init(&priv->rx_recycle);
1893
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 /* Initialize a bunch of registers */
1895 init_registers(dev);
1896
1897 gfar_set_mac_address(dev);
1898
1899 err = init_phy(dev);
1900
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001901 if (err) {
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001902 disable_napi(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 return err;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001904 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905
1906 err = startup_gfar(dev);
Anton Vorontsovdb0e8e32007-10-17 23:57:46 +04001907 if (err) {
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001908 disable_napi(priv);
Anton Vorontsovdb0e8e32007-10-17 23:57:46 +04001909 return err;
1910 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001912 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913
Anton Vorontsov2884e5c2009-02-01 00:52:34 -08001914 device_set_wakeup_enable(&dev->dev, priv->wol_en);
1915
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 return err;
1917}
1918
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07001919static inline struct txfcb *gfar_add_fcb(struct sk_buff *skb)
Kumar Gala0bbaf062005-06-20 10:54:21 -05001920{
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07001921 struct txfcb *fcb = (struct txfcb *)skb_push(skb, GMAC_FCB_LEN);
Kumar Gala6c31d552009-04-28 08:04:10 -07001922
1923 memset(fcb, 0, GMAC_FCB_LEN);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001924
Kumar Gala0bbaf062005-06-20 10:54:21 -05001925 return fcb;
1926}
1927
1928static inline void gfar_tx_checksum(struct sk_buff *skb, struct txfcb *fcb)
1929{
Andy Fleming7f7f5312005-11-11 12:38:59 -06001930 u8 flags = 0;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001931
1932 /* If we're here, it's a IP packet with a TCP or UDP
1933 * payload. We set it to checksum, using a pseudo-header
1934 * we provide
1935 */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001936 flags = TXFCB_DEFAULT;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001937
Andy Fleming7f7f5312005-11-11 12:38:59 -06001938 /* Tell the controller what the protocol is */
1939 /* And provide the already calculated phcs */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001940 if (ip_hdr(skb)->protocol == IPPROTO_UDP) {
Andy Fleming7f7f5312005-11-11 12:38:59 -06001941 flags |= TXFCB_UDP;
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -03001942 fcb->phcs = udp_hdr(skb)->check;
Andy Fleming7f7f5312005-11-11 12:38:59 -06001943 } else
Kumar Gala8da32de2007-06-29 00:12:04 -05001944 fcb->phcs = tcp_hdr(skb)->check;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001945
1946 /* l3os is the distance between the start of the
1947 * frame (skb->data) and the start of the IP hdr.
1948 * l4os is the distance between the start of the
1949 * l3 hdr and the l4 hdr */
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03001950 fcb->l3os = (u16)(skb_network_offset(skb) - GMAC_FCB_LEN);
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -03001951 fcb->l4os = skb_network_header_len(skb);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001952
Andy Fleming7f7f5312005-11-11 12:38:59 -06001953 fcb->flags = flags;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001954}
1955
Andy Fleming7f7f5312005-11-11 12:38:59 -06001956void inline gfar_tx_vlan(struct sk_buff *skb, struct txfcb *fcb)
Kumar Gala0bbaf062005-06-20 10:54:21 -05001957{
Andy Fleming7f7f5312005-11-11 12:38:59 -06001958 fcb->flags |= TXFCB_VLN;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001959 fcb->vlctl = vlan_tx_tag_get(skb);
1960}
1961
Dai Haruki4669bc92008-12-17 16:51:04 -08001962static inline struct txbd8 *skip_txbd(struct txbd8 *bdp, int stride,
1963 struct txbd8 *base, int ring_size)
1964{
1965 struct txbd8 *new_bd = bdp + stride;
1966
1967 return (new_bd >= (base + ring_size)) ? (new_bd - ring_size) : new_bd;
1968}
1969
1970static inline struct txbd8 *next_txbd(struct txbd8 *bdp, struct txbd8 *base,
1971 int ring_size)
1972{
1973 return skip_txbd(bdp, 1, base, ring_size);
1974}
1975
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976/* This is called by the kernel when a frame is ready for transmission. */
1977/* It is pointed to by the dev->hard_start_xmit function pointer */
1978static int gfar_start_xmit(struct sk_buff *skb, struct net_device *dev)
1979{
1980 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001981 struct gfar_priv_tx_q *tx_queue = NULL;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001982 struct netdev_queue *txq;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001983 struct gfar __iomem *regs = NULL;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001984 struct txfcb *fcb = NULL;
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00001985 struct txbd8 *txbdp, *txbdp_start, *base, *txbdp_tstamp = NULL;
Dai Haruki5a5efed2008-12-16 15:34:50 -08001986 u32 lstatus;
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00001987 int i, rq = 0, do_tstamp = 0;
Dai Haruki4669bc92008-12-17 16:51:04 -08001988 u32 bufaddr;
Andy Flemingfef61082006-04-20 16:44:29 -05001989 unsigned long flags;
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00001990 unsigned int nr_frags, nr_txbds, length;
1991 union skb_shared_tx *shtx;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001992
1993 rq = skb->queue_mapping;
1994 tx_queue = priv->tx_queue[rq];
1995 txq = netdev_get_tx_queue(dev, rq);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001996 base = tx_queue->tx_bd_base;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001997 regs = tx_queue->grp->regs;
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00001998 shtx = skb_tx(skb);
1999
2000 /* check if time stamp should be generated */
2001 if (unlikely(shtx->hardware && priv->hwts_tx_en))
2002 do_tstamp = 1;
Dai Haruki4669bc92008-12-17 16:51:04 -08002003
Li Yang5b28bea2009-03-27 15:54:30 -07002004 /* make space for additional header when fcb is needed */
2005 if (((skb->ip_summed == CHECKSUM_PARTIAL) ||
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002006 (priv->vlgrp && vlan_tx_tag_present(skb)) ||
2007 unlikely(do_tstamp)) &&
Li Yang5b28bea2009-03-27 15:54:30 -07002008 (skb_headroom(skb) < GMAC_FCB_LEN)) {
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07002009 struct sk_buff *skb_new;
2010
2011 skb_new = skb_realloc_headroom(skb, GMAC_FCB_LEN);
2012 if (!skb_new) {
2013 dev->stats.tx_errors++;
David S. Millerbd14ba82009-03-27 01:10:58 -07002014 kfree_skb(skb);
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07002015 return NETDEV_TX_OK;
2016 }
2017 kfree_skb(skb);
2018 skb = skb_new;
2019 }
2020
Dai Haruki4669bc92008-12-17 16:51:04 -08002021 /* total number of fragments in the SKB */
2022 nr_frags = skb_shinfo(skb)->nr_frags;
2023
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002024 /* calculate the required number of TxBDs for this skb */
2025 if (unlikely(do_tstamp))
2026 nr_txbds = nr_frags + 2;
2027 else
2028 nr_txbds = nr_frags + 1;
2029
Dai Haruki4669bc92008-12-17 16:51:04 -08002030 /* check if there is space to queue this packet */
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002031 if (nr_txbds > tx_queue->num_txbdfree) {
Dai Haruki4669bc92008-12-17 16:51:04 -08002032 /* no space, stop the queue */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002033 netif_tx_stop_queue(txq);
Dai Haruki4669bc92008-12-17 16:51:04 -08002034 dev->stats.tx_fifo_errors++;
Dai Haruki4669bc92008-12-17 16:51:04 -08002035 return NETDEV_TX_BUSY;
2036 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037
2038 /* Update transmit stats */
Sandeep Gopalpeta7f38042009-12-16 01:15:07 +00002039 txq->tx_bytes += skb->len;
2040 txq->tx_packets ++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002042 txbdp = txbdp_start = tx_queue->cur_tx;
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002043 lstatus = txbdp->lstatus;
2044
2045 /* Time stamp insertion requires one additional TxBD */
2046 if (unlikely(do_tstamp))
2047 txbdp_tstamp = txbdp = next_txbd(txbdp, base,
2048 tx_queue->tx_ring_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049
Dai Haruki4669bc92008-12-17 16:51:04 -08002050 if (nr_frags == 0) {
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002051 if (unlikely(do_tstamp))
2052 txbdp_tstamp->lstatus |= BD_LFLAG(TXBD_LAST |
2053 TXBD_INTERRUPT);
2054 else
2055 lstatus |= BD_LFLAG(TXBD_LAST | TXBD_INTERRUPT);
Dai Haruki4669bc92008-12-17 16:51:04 -08002056 } else {
2057 /* Place the fragment addresses and lengths into the TxBDs */
2058 for (i = 0; i < nr_frags; i++) {
2059 /* Point at the next BD, wrapping as needed */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002060 txbdp = next_txbd(txbdp, base, tx_queue->tx_ring_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061
Dai Haruki4669bc92008-12-17 16:51:04 -08002062 length = skb_shinfo(skb)->frags[i].size;
2063
2064 lstatus = txbdp->lstatus | length |
2065 BD_LFLAG(TXBD_READY);
2066
2067 /* Handle the last BD specially */
2068 if (i == nr_frags - 1)
2069 lstatus |= BD_LFLAG(TXBD_LAST | TXBD_INTERRUPT);
2070
Kumar Gala48268572009-03-18 23:28:22 -07002071 bufaddr = dma_map_page(&priv->ofdev->dev,
Dai Haruki4669bc92008-12-17 16:51:04 -08002072 skb_shinfo(skb)->frags[i].page,
2073 skb_shinfo(skb)->frags[i].page_offset,
2074 length,
2075 DMA_TO_DEVICE);
2076
2077 /* set the TxBD length and buffer pointer */
2078 txbdp->bufPtr = bufaddr;
2079 txbdp->lstatus = lstatus;
2080 }
2081
2082 lstatus = txbdp_start->lstatus;
2083 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084
Kumar Gala0bbaf062005-06-20 10:54:21 -05002085 /* Set up checksumming */
Dai Haruki12dea572008-12-16 15:30:20 -08002086 if (CHECKSUM_PARTIAL == skb->ip_summed) {
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07002087 fcb = gfar_add_fcb(skb);
2088 lstatus |= BD_LFLAG(TXBD_TOE);
2089 gfar_tx_checksum(skb, fcb);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002090 }
2091
Dai Haruki77ecaf22008-12-16 15:30:48 -08002092 if (priv->vlgrp && vlan_tx_tag_present(skb)) {
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07002093 if (unlikely(NULL == fcb)) {
2094 fcb = gfar_add_fcb(skb);
Dai Haruki5a5efed2008-12-16 15:34:50 -08002095 lstatus |= BD_LFLAG(TXBD_TOE);
Andy Fleming7f7f5312005-11-11 12:38:59 -06002096 }
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07002097
2098 gfar_tx_vlan(skb, fcb);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002099 }
2100
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002101 /* Setup tx hardware time stamping if requested */
2102 if (unlikely(do_tstamp)) {
2103 shtx->in_progress = 1;
2104 if (fcb == NULL)
2105 fcb = gfar_add_fcb(skb);
2106 fcb->ptp = 1;
2107 lstatus |= BD_LFLAG(TXBD_TOE);
2108 }
2109
Kumar Gala48268572009-03-18 23:28:22 -07002110 txbdp_start->bufPtr = dma_map_single(&priv->ofdev->dev, skb->data,
Dai Haruki4669bc92008-12-17 16:51:04 -08002111 skb_headlen(skb), DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002113 /*
2114 * If time stamping is requested one additional TxBD must be set up. The
2115 * first TxBD points to the FCB and must have a data length of
2116 * GMAC_FCB_LEN. The second TxBD points to the actual frame data with
2117 * the full frame length.
2118 */
2119 if (unlikely(do_tstamp)) {
2120 txbdp_tstamp->bufPtr = txbdp_start->bufPtr + GMAC_FCB_LEN;
2121 txbdp_tstamp->lstatus |= BD_LFLAG(TXBD_READY) |
2122 (skb_headlen(skb) - GMAC_FCB_LEN);
2123 lstatus |= BD_LFLAG(TXBD_CRC | TXBD_READY) | GMAC_FCB_LEN;
2124 } else {
2125 lstatus |= BD_LFLAG(TXBD_CRC | TXBD_READY) | skb_headlen(skb);
2126 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127
Dai Haruki4669bc92008-12-17 16:51:04 -08002128 /*
Anton Vorontsova3bc1f12009-11-10 14:11:10 +00002129 * We can work in parallel with gfar_clean_tx_ring(), except
2130 * when modifying num_txbdfree. Note that we didn't grab the lock
2131 * when we were reading the num_txbdfree and checking for available
2132 * space, that's because outside of this function it can only grow,
2133 * and once we've got needed space, it cannot suddenly disappear.
2134 *
2135 * The lock also protects us from gfar_error(), which can modify
2136 * regs->tstat and thus retrigger the transfers, which is why we
2137 * also must grab the lock before setting ready bit for the first
2138 * to be transmitted BD.
2139 */
2140 spin_lock_irqsave(&tx_queue->txlock, flags);
2141
2142 /*
Dai Haruki4669bc92008-12-17 16:51:04 -08002143 * The powerpc-specific eieio() is used, as wmb() has too strong
Scott Wood3b6330c2007-05-16 15:06:59 -05002144 * semantics (it requires synchronization between cacheable and
2145 * uncacheable mappings, which eieio doesn't provide and which we
2146 * don't need), thus requiring a more expensive sync instruction. At
2147 * some point, the set of architecture-independent barrier functions
2148 * should be expanded to include weaker barriers.
2149 */
Scott Wood3b6330c2007-05-16 15:06:59 -05002150 eieio();
Andy Fleming7f7f5312005-11-11 12:38:59 -06002151
Dai Haruki4669bc92008-12-17 16:51:04 -08002152 txbdp_start->lstatus = lstatus;
2153
Anton Vorontsov0eddba52010-03-03 08:18:58 +00002154 eieio(); /* force lstatus write before tx_skbuff */
2155
2156 tx_queue->tx_skbuff[tx_queue->skb_curtx] = skb;
2157
Dai Haruki4669bc92008-12-17 16:51:04 -08002158 /* Update the current skb pointer to the next entry we will use
2159 * (wrapping if necessary) */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002160 tx_queue->skb_curtx = (tx_queue->skb_curtx + 1) &
2161 TX_RING_MOD_MASK(tx_queue->tx_ring_size);
Dai Haruki4669bc92008-12-17 16:51:04 -08002162
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002163 tx_queue->cur_tx = next_txbd(txbdp, base, tx_queue->tx_ring_size);
Dai Haruki4669bc92008-12-17 16:51:04 -08002164
2165 /* reduce TxBD free count */
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002166 tx_queue->num_txbdfree -= (nr_txbds);
Dai Haruki4669bc92008-12-17 16:51:04 -08002167
2168 dev->trans_start = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169
2170 /* If the next BD still needs to be cleaned up, then the bds
2171 are full. We need to tell the kernel to stop sending us stuff. */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002172 if (!tx_queue->num_txbdfree) {
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002173 netif_tx_stop_queue(txq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002175 dev->stats.tx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 }
2177
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 /* Tell the DMA to go go go */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002179 gfar_write(&regs->tstat, TSTAT_CLEAR_THALT >> tx_queue->qindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180
2181 /* Unlock priv */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002182 spin_unlock_irqrestore(&tx_queue->txlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07002184 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185}
2186
2187/* Stops the kernel queue, and halts the controller */
2188static int gfar_close(struct net_device *dev)
2189{
2190 struct gfar_private *priv = netdev_priv(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002191
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002192 disable_napi(priv);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002193
Andy Fleming0fd56bb2009-02-04 16:43:16 -08002194 skb_queue_purge(&priv->rx_recycle);
Sebastian Siewiorab939902008-08-19 21:12:45 +02002195 cancel_work_sync(&priv->reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 stop_gfar(dev);
2197
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002198 /* Disconnect from the PHY */
2199 phy_disconnect(priv->phydev);
2200 priv->phydev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002202 netif_tx_stop_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203
2204 return 0;
2205}
2206
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207/* Changes the mac address if the controller is not running. */
Andy Flemingf162b9d2008-05-02 13:00:30 -05002208static int gfar_set_mac_address(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209{
Andy Fleming7f7f5312005-11-11 12:38:59 -06002210 gfar_set_mac_for_addr(dev, 0, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211
2212 return 0;
2213}
2214
2215
Kumar Gala0bbaf062005-06-20 10:54:21 -05002216/* Enables and disables VLAN insertion/extraction */
2217static void gfar_vlan_rx_register(struct net_device *dev,
2218 struct vlan_group *grp)
2219{
2220 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002221 struct gfar __iomem *regs = NULL;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002222 unsigned long flags;
2223 u32 tempval;
2224
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002225 regs = priv->gfargrp[0].regs;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002226 local_irq_save(flags);
2227 lock_rx_qs(priv);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002228
Anton Vorontsovcd1f55a2009-01-26 14:33:23 -08002229 priv->vlgrp = grp;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002230
2231 if (grp) {
2232 /* Enable VLAN tag insertion */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002233 tempval = gfar_read(&regs->tctrl);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002234 tempval |= TCTRL_VLINS;
2235
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002236 gfar_write(&regs->tctrl, tempval);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002237
Kumar Gala0bbaf062005-06-20 10:54:21 -05002238 /* Enable VLAN tag extraction */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002239 tempval = gfar_read(&regs->rctrl);
Dai Haruki77ecaf22008-12-16 15:30:48 -08002240 tempval |= (RCTRL_VLEX | RCTRL_PRSDEP_INIT);
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002241 gfar_write(&regs->rctrl, tempval);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002242 } else {
2243 /* Disable VLAN tag insertion */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002244 tempval = gfar_read(&regs->tctrl);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002245 tempval &= ~TCTRL_VLINS;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002246 gfar_write(&regs->tctrl, tempval);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002247
2248 /* Disable VLAN tag extraction */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002249 tempval = gfar_read(&regs->rctrl);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002250 tempval &= ~RCTRL_VLEX;
Dai Haruki77ecaf22008-12-16 15:30:48 -08002251 /* If parse is no longer required, then disable parser */
2252 if (tempval & RCTRL_REQ_PARSER)
2253 tempval |= RCTRL_PRSDEP_INIT;
2254 else
2255 tempval &= ~RCTRL_PRSDEP_INIT;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002256 gfar_write(&regs->rctrl, tempval);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002257 }
2258
Dai Haruki77ecaf22008-12-16 15:30:48 -08002259 gfar_change_mtu(dev, dev->mtu);
2260
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002261 unlock_rx_qs(priv);
2262 local_irq_restore(flags);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002263}
2264
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265static int gfar_change_mtu(struct net_device *dev, int new_mtu)
2266{
2267 int tempsize, tempval;
2268 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002269 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 int oldsize = priv->rx_buffer_size;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002271 int frame_size = new_mtu + ETH_HLEN;
2272
Dai Haruki77ecaf22008-12-16 15:30:48 -08002273 if (priv->vlgrp)
Dai Harukifaa89572008-03-24 10:53:26 -05002274 frame_size += VLAN_HLEN;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002275
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 if ((frame_size < 64) || (frame_size > JUMBO_FRAME_SIZE)) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05002277 if (netif_msg_drv(priv))
2278 printk(KERN_ERR "%s: Invalid MTU setting\n",
2279 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 return -EINVAL;
2281 }
2282
Dai Haruki77ecaf22008-12-16 15:30:48 -08002283 if (gfar_uses_fcb(priv))
2284 frame_size += GMAC_FCB_LEN;
2285
2286 frame_size += priv->padding;
2287
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 tempsize =
2289 (frame_size & ~(INCREMENTAL_BUFFER_SIZE - 1)) +
2290 INCREMENTAL_BUFFER_SIZE;
2291
2292 /* Only stop and start the controller if it isn't already
Andy Fleming7f7f5312005-11-11 12:38:59 -06002293 * stopped, and we changed something */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 if ((oldsize != tempsize) && (dev->flags & IFF_UP))
2295 stop_gfar(dev);
2296
2297 priv->rx_buffer_size = tempsize;
2298
2299 dev->mtu = new_mtu;
2300
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002301 gfar_write(&regs->mrblr, priv->rx_buffer_size);
2302 gfar_write(&regs->maxfrm, priv->rx_buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303
2304 /* If the mtu is larger than the max size for standard
2305 * ethernet frames (ie, a jumbo frame), then set maccfg2
2306 * to allow huge frames, and to check the length */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002307 tempval = gfar_read(&regs->maccfg2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308
2309 if (priv->rx_buffer_size > DEFAULT_RX_BUFFER_SIZE)
2310 tempval |= (MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK);
2311 else
2312 tempval &= ~(MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK);
2313
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002314 gfar_write(&regs->maccfg2, tempval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315
2316 if ((oldsize != tempsize) && (dev->flags & IFF_UP))
2317 startup_gfar(dev);
2318
2319 return 0;
2320}
2321
Sebastian Siewiorab939902008-08-19 21:12:45 +02002322/* gfar_reset_task gets scheduled when a packet has not been
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 * transmitted after a set amount of time.
2324 * For now, assume that clearing out all the structures, and
Sebastian Siewiorab939902008-08-19 21:12:45 +02002325 * starting over will fix the problem.
2326 */
2327static void gfar_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328{
Sebastian Siewiorab939902008-08-19 21:12:45 +02002329 struct gfar_private *priv = container_of(work, struct gfar_private,
2330 reset_task);
Kumar Gala48268572009-03-18 23:28:22 -07002331 struct net_device *dev = priv->ndev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332
2333 if (dev->flags & IFF_UP) {
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002334 netif_tx_stop_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 stop_gfar(dev);
2336 startup_gfar(dev);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002337 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 }
2339
David S. Miller263ba322008-07-15 03:47:41 -07002340 netif_tx_schedule_all(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341}
2342
Sebastian Siewiorab939902008-08-19 21:12:45 +02002343static void gfar_timeout(struct net_device *dev)
2344{
2345 struct gfar_private *priv = netdev_priv(dev);
2346
2347 dev->stats.tx_errors++;
2348 schedule_work(&priv->reset_task);
2349}
2350
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351/* Interrupt Handler for Transmit complete */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002352static int gfar_clean_tx_ring(struct gfar_priv_tx_q *tx_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353{
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002354 struct net_device *dev = tx_queue->dev;
Dai Harukid080cd62008-04-09 19:37:51 -05002355 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002356 struct gfar_priv_rx_q *rx_queue = NULL;
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002357 struct txbd8 *bdp, *next = NULL;
Dai Haruki4669bc92008-12-17 16:51:04 -08002358 struct txbd8 *lbdp = NULL;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002359 struct txbd8 *base = tx_queue->tx_bd_base;
Dai Haruki4669bc92008-12-17 16:51:04 -08002360 struct sk_buff *skb;
2361 int skb_dirtytx;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002362 int tx_ring_size = tx_queue->tx_ring_size;
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002363 int frags = 0, nr_txbds = 0;
Dai Haruki4669bc92008-12-17 16:51:04 -08002364 int i;
Dai Harukid080cd62008-04-09 19:37:51 -05002365 int howmany = 0;
Dai Haruki4669bc92008-12-17 16:51:04 -08002366 u32 lstatus;
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002367 size_t buflen;
2368 union skb_shared_tx *shtx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002370 rx_queue = priv->rx_queue[tx_queue->qindex];
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002371 bdp = tx_queue->dirty_tx;
2372 skb_dirtytx = tx_queue->skb_dirtytx;
Dai Haruki4669bc92008-12-17 16:51:04 -08002373
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002374 while ((skb = tx_queue->tx_skbuff[skb_dirtytx])) {
Anton Vorontsova3bc1f12009-11-10 14:11:10 +00002375 unsigned long flags;
2376
Dai Haruki4669bc92008-12-17 16:51:04 -08002377 frags = skb_shinfo(skb)->nr_frags;
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002378
2379 /*
2380 * When time stamping, one additional TxBD must be freed.
2381 * Also, we need to dma_unmap_single() the TxPAL.
2382 */
2383 shtx = skb_tx(skb);
2384 if (unlikely(shtx->in_progress))
2385 nr_txbds = frags + 2;
2386 else
2387 nr_txbds = frags + 1;
2388
2389 lbdp = skip_txbd(bdp, nr_txbds - 1, base, tx_ring_size);
Dai Haruki4669bc92008-12-17 16:51:04 -08002390
2391 lstatus = lbdp->lstatus;
2392
2393 /* Only clean completed frames */
2394 if ((lstatus & BD_LFLAG(TXBD_READY)) &&
2395 (lstatus & BD_LENGTH_MASK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 break;
2397
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002398 if (unlikely(shtx->in_progress)) {
2399 next = next_txbd(bdp, base, tx_ring_size);
2400 buflen = next->length + GMAC_FCB_LEN;
2401 } else
2402 buflen = bdp->length;
2403
2404 dma_unmap_single(&priv->ofdev->dev, bdp->bufPtr,
2405 buflen, DMA_TO_DEVICE);
2406
2407 if (unlikely(shtx->in_progress)) {
2408 struct skb_shared_hwtstamps shhwtstamps;
2409 u64 *ns = (u64*) (((u32)skb->data + 0x10) & ~0x7);
2410 memset(&shhwtstamps, 0, sizeof(shhwtstamps));
2411 shhwtstamps.hwtstamp = ns_to_ktime(*ns);
2412 skb_tstamp_tx(skb, &shhwtstamps);
2413 bdp->lstatus &= BD_LFLAG(TXBD_WRAP);
2414 bdp = next;
2415 }
Dai Haruki4669bc92008-12-17 16:51:04 -08002416
2417 bdp->lstatus &= BD_LFLAG(TXBD_WRAP);
2418 bdp = next_txbd(bdp, base, tx_ring_size);
2419
2420 for (i = 0; i < frags; i++) {
Kumar Gala48268572009-03-18 23:28:22 -07002421 dma_unmap_page(&priv->ofdev->dev,
Dai Haruki4669bc92008-12-17 16:51:04 -08002422 bdp->bufPtr,
2423 bdp->length,
2424 DMA_TO_DEVICE);
2425 bdp->lstatus &= BD_LFLAG(TXBD_WRAP);
2426 bdp = next_txbd(bdp, base, tx_ring_size);
2427 }
2428
Andy Fleming0fd56bb2009-02-04 16:43:16 -08002429 /*
2430 * If there's room in the queue (limit it to rx_buffer_size)
2431 * we add this skb back into the pool, if it's the right size
2432 */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002433 if (skb_queue_len(&priv->rx_recycle) < rx_queue->rx_ring_size &&
Andy Fleming0fd56bb2009-02-04 16:43:16 -08002434 skb_recycle_check(skb, priv->rx_buffer_size +
2435 RXBUF_ALIGNMENT))
2436 __skb_queue_head(&priv->rx_recycle, skb);
2437 else
2438 dev_kfree_skb_any(skb);
2439
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002440 tx_queue->tx_skbuff[skb_dirtytx] = NULL;
Dai Haruki4669bc92008-12-17 16:51:04 -08002441
2442 skb_dirtytx = (skb_dirtytx + 1) &
2443 TX_RING_MOD_MASK(tx_ring_size);
2444
Dai Harukid080cd62008-04-09 19:37:51 -05002445 howmany++;
Anton Vorontsova3bc1f12009-11-10 14:11:10 +00002446 spin_lock_irqsave(&tx_queue->txlock, flags);
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002447 tx_queue->num_txbdfree += nr_txbds;
Anton Vorontsova3bc1f12009-11-10 14:11:10 +00002448 spin_unlock_irqrestore(&tx_queue->txlock, flags);
Dai Haruki4669bc92008-12-17 16:51:04 -08002449 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450
Dai Haruki4669bc92008-12-17 16:51:04 -08002451 /* If we freed a buffer, we can restart transmission, if necessary */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002452 if (__netif_subqueue_stopped(dev, tx_queue->qindex) && tx_queue->num_txbdfree)
2453 netif_wake_subqueue(dev, tx_queue->qindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454
Dai Haruki4669bc92008-12-17 16:51:04 -08002455 /* Update dirty indicators */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002456 tx_queue->skb_dirtytx = skb_dirtytx;
2457 tx_queue->dirty_tx = bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458
Dai Harukid080cd62008-04-09 19:37:51 -05002459 return howmany;
2460}
2461
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002462static void gfar_schedule_cleanup(struct gfar_priv_grp *gfargrp)
Dai Haruki8c7396a2008-12-17 16:52:00 -08002463{
Anton Vorontsova6d0b912009-01-12 21:57:34 -08002464 unsigned long flags;
2465
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002466 spin_lock_irqsave(&gfargrp->grplock, flags);
2467 if (napi_schedule_prep(&gfargrp->napi)) {
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002468 gfar_write(&gfargrp->regs->imask, IMASK_RTX_DISABLED);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002469 __napi_schedule(&gfargrp->napi);
Jarek Poplawski8707bdd2009-02-09 14:59:30 -08002470 } else {
2471 /*
2472 * Clear IEVENT, so interrupts aren't called again
2473 * because of the packets that have already arrived.
2474 */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002475 gfar_write(&gfargrp->regs->ievent, IEVENT_RTX_MASK);
Dai Haruki8c7396a2008-12-17 16:52:00 -08002476 }
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002477 spin_unlock_irqrestore(&gfargrp->grplock, flags);
Anton Vorontsova6d0b912009-01-12 21:57:34 -08002478
Dai Haruki8c7396a2008-12-17 16:52:00 -08002479}
2480
Dai Harukid080cd62008-04-09 19:37:51 -05002481/* Interrupt Handler for Transmit complete */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002482static irqreturn_t gfar_transmit(int irq, void *grp_id)
Dai Harukid080cd62008-04-09 19:37:51 -05002483{
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002484 gfar_schedule_cleanup((struct gfar_priv_grp *)grp_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 return IRQ_HANDLED;
2486}
2487
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002488static void gfar_new_rxbdp(struct gfar_priv_rx_q *rx_queue, struct rxbd8 *bdp,
Andy Fleming815b97c2008-04-22 17:18:29 -05002489 struct sk_buff *skb)
2490{
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002491 struct net_device *dev = rx_queue->dev;
Andy Fleming815b97c2008-04-22 17:18:29 -05002492 struct gfar_private *priv = netdev_priv(dev);
Anton Vorontsov8a102fe2009-10-12 06:00:37 +00002493 dma_addr_t buf;
Andy Fleming815b97c2008-04-22 17:18:29 -05002494
Anton Vorontsov8a102fe2009-10-12 06:00:37 +00002495 buf = dma_map_single(&priv->ofdev->dev, skb->data,
2496 priv->rx_buffer_size, DMA_FROM_DEVICE);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002497 gfar_init_rxbdp(rx_queue, bdp, buf);
Andy Fleming815b97c2008-04-22 17:18:29 -05002498}
2499
2500
2501struct sk_buff * gfar_new_skb(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502{
Andy Fleming7f7f5312005-11-11 12:38:59 -06002503 unsigned int alignamount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 struct gfar_private *priv = netdev_priv(dev);
2505 struct sk_buff *skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506
Andy Fleming0fd56bb2009-02-04 16:43:16 -08002507 skb = __skb_dequeue(&priv->rx_recycle);
2508 if (!skb)
2509 skb = netdev_alloc_skb(dev,
2510 priv->rx_buffer_size + RXBUF_ALIGNMENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511
Andy Fleming815b97c2008-04-22 17:18:29 -05002512 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 return NULL;
2514
Andy Fleming7f7f5312005-11-11 12:38:59 -06002515 alignamount = RXBUF_ALIGNMENT -
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002516 (((unsigned long) skb->data) & (RXBUF_ALIGNMENT - 1));
Andy Fleming7f7f5312005-11-11 12:38:59 -06002517
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 /* We need the data buffer to be aligned properly. We will reserve
2519 * as many bytes as needed to align the data properly
2520 */
Andy Fleming7f7f5312005-11-11 12:38:59 -06002521 skb_reserve(skb, alignamount);
Ben Menchacaa6d36d52010-03-24 05:05:02 +00002522 GFAR_CB(skb)->alignamount = alignamount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 return skb;
2525}
2526
Li Yang298e1a92007-10-16 14:18:13 +08002527static inline void count_errors(unsigned short status, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528{
Li Yang298e1a92007-10-16 14:18:13 +08002529 struct gfar_private *priv = netdev_priv(dev);
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002530 struct net_device_stats *stats = &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 struct gfar_extra_stats *estats = &priv->extra_stats;
2532
2533 /* If the packet was truncated, none of the other errors
2534 * matter */
2535 if (status & RXBD_TRUNCATED) {
2536 stats->rx_length_errors++;
2537
2538 estats->rx_trunc++;
2539
2540 return;
2541 }
2542 /* Count the errors, if there were any */
2543 if (status & (RXBD_LARGE | RXBD_SHORT)) {
2544 stats->rx_length_errors++;
2545
2546 if (status & RXBD_LARGE)
2547 estats->rx_large++;
2548 else
2549 estats->rx_short++;
2550 }
2551 if (status & RXBD_NONOCTET) {
2552 stats->rx_frame_errors++;
2553 estats->rx_nonoctet++;
2554 }
2555 if (status & RXBD_CRCERR) {
2556 estats->rx_crcerr++;
2557 stats->rx_crc_errors++;
2558 }
2559 if (status & RXBD_OVERRUN) {
2560 estats->rx_overrun++;
2561 stats->rx_crc_errors++;
2562 }
2563}
2564
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002565irqreturn_t gfar_receive(int irq, void *grp_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566{
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002567 gfar_schedule_cleanup((struct gfar_priv_grp *)grp_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 return IRQ_HANDLED;
2569}
2570
Kumar Gala0bbaf062005-06-20 10:54:21 -05002571static inline void gfar_rx_checksum(struct sk_buff *skb, struct rxfcb *fcb)
2572{
2573 /* If valid headers were found, and valid sums
2574 * were verified, then we tell the kernel that no
2575 * checksumming is necessary. Otherwise, it is */
Andy Fleming7f7f5312005-11-11 12:38:59 -06002576 if ((fcb->flags & RXFCB_CSUM_MASK) == (RXFCB_CIP | RXFCB_CTU))
Kumar Gala0bbaf062005-06-20 10:54:21 -05002577 skb->ip_summed = CHECKSUM_UNNECESSARY;
2578 else
2579 skb->ip_summed = CHECKSUM_NONE;
2580}
2581
2582
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583/* gfar_process_frame() -- handle one incoming packet if skb
2584 * isn't NULL. */
2585static int gfar_process_frame(struct net_device *dev, struct sk_buff *skb,
Dai Haruki2c2db482008-12-16 15:31:15 -08002586 int amount_pull)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587{
2588 struct gfar_private *priv = netdev_priv(dev);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002589 struct rxfcb *fcb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590
Dai Haruki2c2db482008-12-16 15:31:15 -08002591 int ret;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002592
Dai Haruki2c2db482008-12-16 15:31:15 -08002593 /* fcb is at the beginning if exists */
2594 fcb = (struct rxfcb *)skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595
Dai Haruki2c2db482008-12-16 15:31:15 -08002596 /* Remove the FCB from the skb */
2597 /* Remove the padded bytes, if there are any */
Sandeep Gopalpetf74dac02009-12-24 03:13:06 +00002598 if (amount_pull) {
2599 skb_record_rx_queue(skb, fcb->rq);
Dai Haruki2c2db482008-12-16 15:31:15 -08002600 skb_pull(skb, amount_pull);
Sandeep Gopalpetf74dac02009-12-24 03:13:06 +00002601 }
Kumar Gala0bbaf062005-06-20 10:54:21 -05002602
Manfred Rudigiercc772ab2010-04-08 23:10:03 +00002603 /* Get receive timestamp from the skb */
2604 if (priv->hwts_rx_en) {
2605 struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
2606 u64 *ns = (u64 *) skb->data;
2607 memset(shhwtstamps, 0, sizeof(*shhwtstamps));
2608 shhwtstamps->hwtstamp = ns_to_ktime(*ns);
2609 }
2610
2611 if (priv->padding)
2612 skb_pull(skb, priv->padding);
2613
Dai Haruki2c2db482008-12-16 15:31:15 -08002614 if (priv->rx_csum_enable)
2615 gfar_rx_checksum(skb, fcb);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002616
Dai Haruki2c2db482008-12-16 15:31:15 -08002617 /* Tell the skb what kind of packet this is */
2618 skb->protocol = eth_type_trans(skb, dev);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002619
Dai Haruki2c2db482008-12-16 15:31:15 -08002620 /* Send the packet up the stack */
2621 if (unlikely(priv->vlgrp && (fcb->flags & RXFCB_VLN)))
2622 ret = vlan_hwaccel_receive_skb(skb, priv->vlgrp, fcb->vlctl);
2623 else
2624 ret = netif_receive_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625
Dai Haruki2c2db482008-12-16 15:31:15 -08002626 if (NET_RX_DROP == ret)
2627 priv->extra_stats.kernel_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628
2629 return 0;
2630}
2631
2632/* gfar_clean_rx_ring() -- Processes each frame in the rx ring
Kumar Gala0bbaf062005-06-20 10:54:21 -05002633 * until the budget/quota has been reached. Returns the number
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 * of frames handled
2635 */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002636int gfar_clean_rx_ring(struct gfar_priv_rx_q *rx_queue, int rx_work_limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637{
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002638 struct net_device *dev = rx_queue->dev;
Andy Fleming31de1982008-12-16 15:33:40 -08002639 struct rxbd8 *bdp, *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 struct sk_buff *skb;
Dai Haruki2c2db482008-12-16 15:31:15 -08002641 int pkt_len;
2642 int amount_pull;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 int howmany = 0;
2644 struct gfar_private *priv = netdev_priv(dev);
2645
2646 /* Get the first full descriptor */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002647 bdp = rx_queue->cur_rx;
2648 base = rx_queue->rx_bd_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649
Manfred Rudigiercc772ab2010-04-08 23:10:03 +00002650 amount_pull = (gfar_uses_fcb(priv) ? GMAC_FCB_LEN : 0);
Dai Haruki2c2db482008-12-16 15:31:15 -08002651
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 while (!((bdp->status & RXBD_EMPTY) || (--rx_work_limit < 0))) {
Andy Fleming815b97c2008-04-22 17:18:29 -05002653 struct sk_buff *newskb;
Scott Wood3b6330c2007-05-16 15:06:59 -05002654 rmb();
Andy Fleming815b97c2008-04-22 17:18:29 -05002655
2656 /* Add another skb for the future */
2657 newskb = gfar_new_skb(dev);
2658
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002659 skb = rx_queue->rx_skbuff[rx_queue->skb_currx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660
Kumar Gala48268572009-03-18 23:28:22 -07002661 dma_unmap_single(&priv->ofdev->dev, bdp->bufPtr,
Andy Fleming81183052008-11-12 10:07:11 -06002662 priv->rx_buffer_size, DMA_FROM_DEVICE);
2663
Andy Fleming815b97c2008-04-22 17:18:29 -05002664 /* We drop the frame if we failed to allocate a new buffer */
2665 if (unlikely(!newskb || !(bdp->status & RXBD_LAST) ||
2666 bdp->status & RXBD_ERR)) {
2667 count_errors(bdp->status, dev);
2668
2669 if (unlikely(!newskb))
2670 newskb = skb;
Lennert Buytenhek4e2fd552009-05-25 00:42:34 -07002671 else if (skb) {
2672 /*
Ben Menchacaa6d36d52010-03-24 05:05:02 +00002673 * We need to un-reserve() the skb to what it
Lennert Buytenhek4e2fd552009-05-25 00:42:34 -07002674 * was before gfar_new_skb() re-aligned
2675 * it to an RXBUF_ALIGNMENT boundary
2676 * before we put the skb back on the
2677 * recycle list.
2678 */
Ben Menchacaa6d36d52010-03-24 05:05:02 +00002679 skb_reserve(skb, -GFAR_CB(skb)->alignamount);
Andy Fleming0fd56bb2009-02-04 16:43:16 -08002680 __skb_queue_head(&priv->rx_recycle, skb);
Lennert Buytenhek4e2fd552009-05-25 00:42:34 -07002681 }
Andy Fleming815b97c2008-04-22 17:18:29 -05002682 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 /* Increment the number of packets */
Sandeep Gopalpeta7f38042009-12-16 01:15:07 +00002684 rx_queue->stats.rx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 howmany++;
2686
Dai Haruki2c2db482008-12-16 15:31:15 -08002687 if (likely(skb)) {
2688 pkt_len = bdp->length - ETH_FCS_LEN;
2689 /* Remove the FCS from the packet length */
2690 skb_put(skb, pkt_len);
Sandeep Gopalpeta7f38042009-12-16 01:15:07 +00002691 rx_queue->stats.rx_bytes += pkt_len;
Sandeep Gopalpetf74dac02009-12-24 03:13:06 +00002692 skb_record_rx_queue(skb, rx_queue->qindex);
Dai Haruki2c2db482008-12-16 15:31:15 -08002693 gfar_process_frame(dev, skb, amount_pull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694
Dai Haruki2c2db482008-12-16 15:31:15 -08002695 } else {
2696 if (netif_msg_rx_err(priv))
2697 printk(KERN_WARNING
2698 "%s: Missing skb!\n", dev->name);
Sandeep Gopalpeta7f38042009-12-16 01:15:07 +00002699 rx_queue->stats.rx_dropped++;
Dai Haruki2c2db482008-12-16 15:31:15 -08002700 priv->extra_stats.rx_skbmissing++;
2701 }
2702
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 }
2704
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002705 rx_queue->rx_skbuff[rx_queue->skb_currx] = newskb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706
Andy Fleming815b97c2008-04-22 17:18:29 -05002707 /* Setup the new bdp */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002708 gfar_new_rxbdp(rx_queue, bdp, newskb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709
2710 /* Update to the next pointer */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002711 bdp = next_bd(bdp, base, rx_queue->rx_ring_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712
2713 /* update to point at the next skb */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002714 rx_queue->skb_currx =
2715 (rx_queue->skb_currx + 1) &
2716 RX_RING_MOD_MASK(rx_queue->rx_ring_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 }
2718
2719 /* Update the current rxbd pointer to be the next one */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002720 rx_queue->cur_rx = bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 return howmany;
2723}
2724
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002725static int gfar_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726{
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002727 struct gfar_priv_grp *gfargrp = container_of(napi,
2728 struct gfar_priv_grp, napi);
2729 struct gfar_private *priv = gfargrp->priv;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002730 struct gfar __iomem *regs = gfargrp->regs;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002731 struct gfar_priv_tx_q *tx_queue = NULL;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002732 struct gfar_priv_rx_q *rx_queue = NULL;
2733 int rx_cleaned = 0, budget_per_queue = 0, rx_cleaned_per_queue = 0;
Anton Vorontsov18294ad2009-11-04 12:53:00 +00002734 int tx_cleaned = 0, i, left_over_budget = budget;
2735 unsigned long serviced_queues = 0;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002736 int num_queues = 0;
Dai Harukid080cd62008-04-09 19:37:51 -05002737
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002738 num_queues = gfargrp->num_rx_queues;
2739 budget_per_queue = budget/num_queues;
2740
Dai Haruki8c7396a2008-12-17 16:52:00 -08002741 /* Clear IEVENT, so interrupts aren't called again
2742 * because of the packets that have already arrived */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002743 gfar_write(&regs->ievent, IEVENT_RTX_MASK);
Dai Haruki8c7396a2008-12-17 16:52:00 -08002744
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002745 while (num_queues && left_over_budget) {
2746
2747 budget_per_queue = left_over_budget/num_queues;
2748 left_over_budget = 0;
2749
Akinobu Mita984b3f52010-03-05 13:41:37 -08002750 for_each_set_bit(i, &gfargrp->rx_bit_map, priv->num_rx_queues) {
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002751 if (test_bit(i, &serviced_queues))
2752 continue;
2753 rx_queue = priv->rx_queue[i];
2754 tx_queue = priv->tx_queue[rx_queue->qindex];
2755
Anton Vorontsova3bc1f12009-11-10 14:11:10 +00002756 tx_cleaned += gfar_clean_tx_ring(tx_queue);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002757 rx_cleaned_per_queue = gfar_clean_rx_ring(rx_queue,
2758 budget_per_queue);
2759 rx_cleaned += rx_cleaned_per_queue;
2760 if(rx_cleaned_per_queue < budget_per_queue) {
2761 left_over_budget = left_over_budget +
2762 (budget_per_queue - rx_cleaned_per_queue);
2763 set_bit(i, &serviced_queues);
2764 num_queues--;
2765 }
2766 }
Dai Harukid080cd62008-04-09 19:37:51 -05002767 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768
Andy Fleming42199882008-12-17 16:52:30 -08002769 if (tx_cleaned)
2770 return budget;
2771
2772 if (rx_cleaned < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -08002773 napi_complete(napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774
2775 /* Clear the halt bit in RSTAT */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002776 gfar_write(&regs->rstat, gfargrp->rstat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002778 gfar_write(&regs->imask, IMASK_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779
2780 /* If we are coalescing interrupts, update the timer */
2781 /* Otherwise, clear it */
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002782 gfar_configure_coalescing(priv,
2783 gfargrp->rx_bit_map, gfargrp->tx_bit_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 }
2785
Andy Fleming42199882008-12-17 16:52:30 -08002786 return rx_cleaned;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788
Vitaly Woolf2d71c22006-11-07 13:27:02 +03002789#ifdef CONFIG_NET_POLL_CONTROLLER
2790/*
2791 * Polling 'interrupt' - used by things like netconsole to send skbs
2792 * without having to re-enable interrupts. It's not called while
2793 * the interrupt routine is executing.
2794 */
2795static void gfar_netpoll(struct net_device *dev)
2796{
2797 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002798 int i = 0;
Vitaly Woolf2d71c22006-11-07 13:27:02 +03002799
2800 /* If the device has multiple interrupts, run tx/rx */
Andy Flemingb31a1d82008-12-16 15:29:15 -08002801 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002802 for (i = 0; i < priv->num_grps; i++) {
2803 disable_irq(priv->gfargrp[i].interruptTransmit);
2804 disable_irq(priv->gfargrp[i].interruptReceive);
2805 disable_irq(priv->gfargrp[i].interruptError);
2806 gfar_interrupt(priv->gfargrp[i].interruptTransmit,
2807 &priv->gfargrp[i]);
2808 enable_irq(priv->gfargrp[i].interruptError);
2809 enable_irq(priv->gfargrp[i].interruptReceive);
2810 enable_irq(priv->gfargrp[i].interruptTransmit);
2811 }
Vitaly Woolf2d71c22006-11-07 13:27:02 +03002812 } else {
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002813 for (i = 0; i < priv->num_grps; i++) {
2814 disable_irq(priv->gfargrp[i].interruptTransmit);
2815 gfar_interrupt(priv->gfargrp[i].interruptTransmit,
2816 &priv->gfargrp[i]);
2817 enable_irq(priv->gfargrp[i].interruptTransmit);
Anton Vorontsov43de0042009-12-09 02:52:19 -08002818 }
Vitaly Woolf2d71c22006-11-07 13:27:02 +03002819 }
2820}
2821#endif
2822
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823/* The interrupt handler for devices with one interrupt */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002824static irqreturn_t gfar_interrupt(int irq, void *grp_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825{
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002826 struct gfar_priv_grp *gfargrp = grp_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827
2828 /* Save ievent for future reference */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002829 u32 events = gfar_read(&gfargrp->regs->ievent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 /* Check for reception */
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002832 if (events & IEVENT_RX_MASK)
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002833 gfar_receive(irq, grp_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834
2835 /* Check for transmit completion */
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002836 if (events & IEVENT_TX_MASK)
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002837 gfar_transmit(irq, grp_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002839 /* Check for errors */
2840 if (events & IEVENT_ERR_MASK)
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002841 gfar_error(irq, grp_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842
2843 return IRQ_HANDLED;
2844}
2845
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846/* Called every time the controller might need to be made
2847 * aware of new link state. The PHY code conveys this
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002848 * information through variables in the phydev structure, and this
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 * function converts those variables into the appropriate
2850 * register values, and can bring down the device if needed.
2851 */
2852static void adjust_link(struct net_device *dev)
2853{
2854 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002855 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002856 unsigned long flags;
2857 struct phy_device *phydev = priv->phydev;
2858 int new_state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002860 local_irq_save(flags);
2861 lock_tx_qs(priv);
2862
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002863 if (phydev->link) {
2864 u32 tempval = gfar_read(&regs->maccfg2);
Andy Fleming7f7f5312005-11-11 12:38:59 -06002865 u32 ecntrl = gfar_read(&regs->ecntrl);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002866
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 /* Now we make sure that we can be in full duplex mode.
2868 * If not, we operate in half-duplex mode. */
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002869 if (phydev->duplex != priv->oldduplex) {
2870 new_state = 1;
2871 if (!(phydev->duplex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 tempval &= ~(MACCFG2_FULL_DUPLEX);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002873 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 tempval |= MACCFG2_FULL_DUPLEX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002876 priv->oldduplex = phydev->duplex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 }
2878
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002879 if (phydev->speed != priv->oldspeed) {
2880 new_state = 1;
2881 switch (phydev->speed) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 case 1000:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 tempval =
2884 ((tempval & ~(MACCFG2_IF)) | MACCFG2_GMII);
Li Yangf430e492009-01-06 14:08:10 -08002885
2886 ecntrl &= ~(ECNTRL_R100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 break;
2888 case 100:
2889 case 10:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 tempval =
2891 ((tempval & ~(MACCFG2_IF)) | MACCFG2_MII);
Andy Fleming7f7f5312005-11-11 12:38:59 -06002892
2893 /* Reduced mode distinguishes
2894 * between 10 and 100 */
2895 if (phydev->speed == SPEED_100)
2896 ecntrl |= ECNTRL_R100;
2897 else
2898 ecntrl &= ~(ECNTRL_R100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 break;
2900 default:
Kumar Gala0bbaf062005-06-20 10:54:21 -05002901 if (netif_msg_link(priv))
2902 printk(KERN_WARNING
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002903 "%s: Ack! Speed (%d) is not 10/100/1000!\n",
2904 dev->name, phydev->speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 break;
2906 }
2907
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002908 priv->oldspeed = phydev->speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 }
2910
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002911 gfar_write(&regs->maccfg2, tempval);
Andy Fleming7f7f5312005-11-11 12:38:59 -06002912 gfar_write(&regs->ecntrl, ecntrl);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002913
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 if (!priv->oldlink) {
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002915 new_state = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 priv->oldlink = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 }
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002918 } else if (priv->oldlink) {
2919 new_state = 1;
2920 priv->oldlink = 0;
2921 priv->oldspeed = 0;
2922 priv->oldduplex = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002925 if (new_state && netif_msg_link(priv))
2926 phy_print_status(phydev);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002927 unlock_tx_qs(priv);
2928 local_irq_restore(flags);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002929}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930
2931/* Update the hash table based on the current list of multicast
2932 * addresses we subscribe to. Also, change the promiscuity of
2933 * the device based on the flags (this function is called
2934 * whenever dev->flags is changed */
2935static void gfar_set_multi(struct net_device *dev)
2936{
Jiri Pirko22bedad32010-04-01 21:22:57 +00002937 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002939 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 u32 tempval;
2941
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002942 if (dev->flags & IFF_PROMISC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 /* Set RCTRL to PROM */
2944 tempval = gfar_read(&regs->rctrl);
2945 tempval |= RCTRL_PROM;
2946 gfar_write(&regs->rctrl, tempval);
2947 } else {
2948 /* Set RCTRL to not PROM */
2949 tempval = gfar_read(&regs->rctrl);
2950 tempval &= ~(RCTRL_PROM);
2951 gfar_write(&regs->rctrl, tempval);
2952 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002953
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002954 if (dev->flags & IFF_ALLMULTI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 /* Set the hash to rx all multicast frames */
Kumar Gala0bbaf062005-06-20 10:54:21 -05002956 gfar_write(&regs->igaddr0, 0xffffffff);
2957 gfar_write(&regs->igaddr1, 0xffffffff);
2958 gfar_write(&regs->igaddr2, 0xffffffff);
2959 gfar_write(&regs->igaddr3, 0xffffffff);
2960 gfar_write(&regs->igaddr4, 0xffffffff);
2961 gfar_write(&regs->igaddr5, 0xffffffff);
2962 gfar_write(&regs->igaddr6, 0xffffffff);
2963 gfar_write(&regs->igaddr7, 0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 gfar_write(&regs->gaddr0, 0xffffffff);
2965 gfar_write(&regs->gaddr1, 0xffffffff);
2966 gfar_write(&regs->gaddr2, 0xffffffff);
2967 gfar_write(&regs->gaddr3, 0xffffffff);
2968 gfar_write(&regs->gaddr4, 0xffffffff);
2969 gfar_write(&regs->gaddr5, 0xffffffff);
2970 gfar_write(&regs->gaddr6, 0xffffffff);
2971 gfar_write(&regs->gaddr7, 0xffffffff);
2972 } else {
Andy Fleming7f7f5312005-11-11 12:38:59 -06002973 int em_num;
2974 int idx;
2975
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 /* zero out the hash */
Kumar Gala0bbaf062005-06-20 10:54:21 -05002977 gfar_write(&regs->igaddr0, 0x0);
2978 gfar_write(&regs->igaddr1, 0x0);
2979 gfar_write(&regs->igaddr2, 0x0);
2980 gfar_write(&regs->igaddr3, 0x0);
2981 gfar_write(&regs->igaddr4, 0x0);
2982 gfar_write(&regs->igaddr5, 0x0);
2983 gfar_write(&regs->igaddr6, 0x0);
2984 gfar_write(&regs->igaddr7, 0x0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 gfar_write(&regs->gaddr0, 0x0);
2986 gfar_write(&regs->gaddr1, 0x0);
2987 gfar_write(&regs->gaddr2, 0x0);
2988 gfar_write(&regs->gaddr3, 0x0);
2989 gfar_write(&regs->gaddr4, 0x0);
2990 gfar_write(&regs->gaddr5, 0x0);
2991 gfar_write(&regs->gaddr6, 0x0);
2992 gfar_write(&regs->gaddr7, 0x0);
2993
Andy Fleming7f7f5312005-11-11 12:38:59 -06002994 /* If we have extended hash tables, we need to
2995 * clear the exact match registers to prepare for
2996 * setting them */
2997 if (priv->extended_hash) {
2998 em_num = GFAR_EM_NUM + 1;
2999 gfar_clear_exact_match(dev);
3000 idx = 1;
3001 } else {
3002 idx = 0;
3003 em_num = 0;
3004 }
3005
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00003006 if (netdev_mc_empty(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 return;
3008
3009 /* Parse the list, and set the appropriate bits */
Jiri Pirko22bedad32010-04-01 21:22:57 +00003010 netdev_for_each_mc_addr(ha, dev) {
Andy Fleming7f7f5312005-11-11 12:38:59 -06003011 if (idx < em_num) {
Jiri Pirko22bedad32010-04-01 21:22:57 +00003012 gfar_set_mac_for_addr(dev, idx, ha->addr);
Andy Fleming7f7f5312005-11-11 12:38:59 -06003013 idx++;
3014 } else
Jiri Pirko22bedad32010-04-01 21:22:57 +00003015 gfar_set_hash_for_addr(dev, ha->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 }
3017 }
3018
3019 return;
3020}
3021
Andy Fleming7f7f5312005-11-11 12:38:59 -06003022
3023/* Clears each of the exact match registers to zero, so they
3024 * don't interfere with normal reception */
3025static void gfar_clear_exact_match(struct net_device *dev)
3026{
3027 int idx;
3028 u8 zero_arr[MAC_ADDR_LEN] = {0,0,0,0,0,0};
3029
3030 for(idx = 1;idx < GFAR_EM_NUM + 1;idx++)
3031 gfar_set_mac_for_addr(dev, idx, (u8 *)zero_arr);
3032}
3033
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034/* Set the appropriate hash bit for the given addr */
3035/* The algorithm works like so:
3036 * 1) Take the Destination Address (ie the multicast address), and
3037 * do a CRC on it (little endian), and reverse the bits of the
3038 * result.
3039 * 2) Use the 8 most significant bits as a hash into a 256-entry
3040 * table. The table is controlled through 8 32-bit registers:
3041 * gaddr0-7. gaddr0's MSB is entry 0, and gaddr7's LSB is
3042 * gaddr7. This means that the 3 most significant bits in the
3043 * hash index which gaddr register to use, and the 5 other bits
3044 * indicate which bit (assuming an IBM numbering scheme, which
3045 * for PowerPC (tm) is usually the case) in the register holds
3046 * the entry. */
3047static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr)
3048{
3049 u32 tempval;
3050 struct gfar_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 u32 result = ether_crc(MAC_ADDR_LEN, addr);
Kumar Gala0bbaf062005-06-20 10:54:21 -05003052 int width = priv->hash_width;
3053 u8 whichbit = (result >> (32 - width)) & 0x1f;
3054 u8 whichreg = result >> (32 - width + 5);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 u32 value = (1 << (31-whichbit));
3056
Kumar Gala0bbaf062005-06-20 10:54:21 -05003057 tempval = gfar_read(priv->hash_regs[whichreg]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 tempval |= value;
Kumar Gala0bbaf062005-06-20 10:54:21 -05003059 gfar_write(priv->hash_regs[whichreg], tempval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060
3061 return;
3062}
3063
Andy Fleming7f7f5312005-11-11 12:38:59 -06003064
3065/* There are multiple MAC Address register pairs on some controllers
3066 * This function sets the numth pair to a given address
3067 */
3068static void gfar_set_mac_for_addr(struct net_device *dev, int num, u8 *addr)
3069{
3070 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00003071 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Andy Fleming7f7f5312005-11-11 12:38:59 -06003072 int idx;
3073 char tmpbuf[MAC_ADDR_LEN];
3074 u32 tempval;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003075 u32 __iomem *macptr = &regs->macstnaddr1;
Andy Fleming7f7f5312005-11-11 12:38:59 -06003076
3077 macptr += num*2;
3078
3079 /* Now copy it into the mac registers backwards, cuz */
3080 /* little endian is silly */
3081 for (idx = 0; idx < MAC_ADDR_LEN; idx++)
3082 tmpbuf[MAC_ADDR_LEN - 1 - idx] = addr[idx];
3083
3084 gfar_write(macptr, *((u32 *) (tmpbuf)));
3085
3086 tempval = *((u32 *) (tmpbuf + 4));
3087
3088 gfar_write(macptr+1, tempval);
3089}
3090
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091/* GFAR error interrupt handler */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003092static irqreturn_t gfar_error(int irq, void *grp_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093{
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003094 struct gfar_priv_grp *gfargrp = grp_id;
3095 struct gfar __iomem *regs = gfargrp->regs;
3096 struct gfar_private *priv= gfargrp->priv;
3097 struct net_device *dev = priv->ndev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098
3099 /* Save ievent for future reference */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003100 u32 events = gfar_read(&regs->ievent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101
3102 /* Clear IEVENT */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003103 gfar_write(&regs->ievent, events & IEVENT_ERR_MASK);
Scott Woodd87eb122008-07-11 18:04:45 -05003104
3105 /* Magic Packet is not an error. */
Andy Flemingb31a1d82008-12-16 15:29:15 -08003106 if ((priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET) &&
Scott Woodd87eb122008-07-11 18:04:45 -05003107 (events & IEVENT_MAG))
3108 events &= ~IEVENT_MAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109
3110 /* Hmm... */
Kumar Gala0bbaf062005-06-20 10:54:21 -05003111 if (netif_msg_rx_err(priv) || netif_msg_tx_err(priv))
3112 printk(KERN_DEBUG "%s: error interrupt (ievent=0x%08x imask=0x%08x)\n",
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003113 dev->name, events, gfar_read(&regs->imask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114
3115 /* Update the error counters */
3116 if (events & IEVENT_TXE) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07003117 dev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118
3119 if (events & IEVENT_LC)
Jeff Garzik09f75cd2007-10-03 17:41:50 -07003120 dev->stats.tx_window_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 if (events & IEVENT_CRL)
Jeff Garzik09f75cd2007-10-03 17:41:50 -07003122 dev->stats.tx_aborted_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 if (events & IEVENT_XFUN) {
Anton Vorontsov836cf7f2009-11-10 14:11:08 +00003124 unsigned long flags;
3125
Kumar Gala0bbaf062005-06-20 10:54:21 -05003126 if (netif_msg_tx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04003127 printk(KERN_DEBUG "%s: TX FIFO underrun, "
3128 "packet dropped.\n", dev->name);
Jeff Garzik09f75cd2007-10-03 17:41:50 -07003129 dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 priv->extra_stats.tx_underrun++;
3131
Anton Vorontsov836cf7f2009-11-10 14:11:08 +00003132 local_irq_save(flags);
3133 lock_tx_qs(priv);
3134
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 /* Reactivate the Tx Queues */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00003136 gfar_write(&regs->tstat, gfargrp->tstat);
Anton Vorontsov836cf7f2009-11-10 14:11:08 +00003137
3138 unlock_tx_qs(priv);
3139 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 }
Kumar Gala0bbaf062005-06-20 10:54:21 -05003141 if (netif_msg_tx_err(priv))
3142 printk(KERN_DEBUG "%s: Transmit Error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143 }
3144 if (events & IEVENT_BSY) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07003145 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 priv->extra_stats.rx_bsy++;
3147
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003148 gfar_receive(irq, grp_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149
Kumar Gala0bbaf062005-06-20 10:54:21 -05003150 if (netif_msg_rx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04003151 printk(KERN_DEBUG "%s: busy error (rstat: %x)\n",
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003152 dev->name, gfar_read(&regs->rstat));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 }
3154 if (events & IEVENT_BABR) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07003155 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 priv->extra_stats.rx_babr++;
3157
Kumar Gala0bbaf062005-06-20 10:54:21 -05003158 if (netif_msg_rx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04003159 printk(KERN_DEBUG "%s: babbling RX error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 }
3161 if (events & IEVENT_EBERR) {
3162 priv->extra_stats.eberr++;
Kumar Gala0bbaf062005-06-20 10:54:21 -05003163 if (netif_msg_rx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04003164 printk(KERN_DEBUG "%s: bus error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 }
Kumar Gala0bbaf062005-06-20 10:54:21 -05003166 if ((events & IEVENT_RXC) && netif_msg_rx_status(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04003167 printk(KERN_DEBUG "%s: control frame\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168
3169 if (events & IEVENT_BABT) {
3170 priv->extra_stats.tx_babt++;
Kumar Gala0bbaf062005-06-20 10:54:21 -05003171 if (netif_msg_tx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04003172 printk(KERN_DEBUG "%s: babbling TX error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 }
3174 return IRQ_HANDLED;
3175}
3176
Andy Flemingb31a1d82008-12-16 15:29:15 -08003177static struct of_device_id gfar_match[] =
3178{
3179 {
3180 .type = "network",
3181 .compatible = "gianfar",
3182 },
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00003183 {
3184 .compatible = "fsl,etsec2",
3185 },
Andy Flemingb31a1d82008-12-16 15:29:15 -08003186 {},
3187};
Anton Vorontsove72701a2009-10-14 14:54:52 -07003188MODULE_DEVICE_TABLE(of, gfar_match);
Andy Flemingb31a1d82008-12-16 15:29:15 -08003189
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190/* Structure for a device driver */
Andy Flemingb31a1d82008-12-16 15:29:15 -08003191static struct of_platform_driver gfar_driver = {
3192 .name = "fsl-gianfar",
3193 .match_table = gfar_match,
3194
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 .probe = gfar_probe,
3196 .remove = gfar_remove,
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00003197 .suspend = gfar_legacy_suspend,
3198 .resume = gfar_legacy_resume,
3199 .driver.pm = GFAR_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200};
3201
3202static int __init gfar_init(void)
3203{
Andy Fleming1577ece2009-02-04 16:42:12 -08003204 return of_register_platform_driver(&gfar_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205}
3206
3207static void __exit gfar_exit(void)
3208{
Andy Flemingb31a1d82008-12-16 15:29:15 -08003209 of_unregister_platform_driver(&gfar_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210}
3211
3212module_init(gfar_init);
3213module_exit(gfar_exit);
3214