blob: ccb231c4d9334bbe49336eeee5f8491e0c8e321b [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>
Anton Vorontsov7d350972010-06-30 06:39:12 +000088#include <asm/reg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070089#include <asm/irq.h>
90#include <asm/uaccess.h>
91#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070092#include <linux/dma-mapping.h>
93#include <linux/crc32.h>
Andy Flemingbb40dcb2005-09-23 22:54:21 -040094#include <linux/mii.h>
95#include <linux/phy.h>
Andy Flemingb31a1d82008-12-16 15:29:15 -080096#include <linux/phy_fixed.h>
97#include <linux/of.h>
David Daney4b6ba8a2010-10-26 15:07:13 -070098#include <linux/of_net.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100#include "gianfar.h"
Andy Fleming1577ece2009-02-04 16:42:12 -0800101#include "fsl_pq_mdio.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103#define TX_TIMEOUT (1*HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104#undef BRIEF_GFAR_ERRORS
105#undef VERBOSE_GFAR_ERRORS
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107const char gfar_driver_name[] = "Gianfar Ethernet";
Andy Fleming7f7f5312005-11-11 12:38:59 -0600108const char gfar_driver_version[] = "1.3";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110static int gfar_enet_open(struct net_device *dev);
111static int gfar_start_xmit(struct sk_buff *skb, struct net_device *dev);
Sebastian Siewiorab939902008-08-19 21:12:45 +0200112static void gfar_reset_task(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113static void gfar_timeout(struct net_device *dev);
114static int gfar_close(struct net_device *dev);
Andy Fleming815b97c2008-04-22 17:18:29 -0500115struct sk_buff *gfar_new_skb(struct net_device *dev);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000116static void gfar_new_rxbdp(struct gfar_priv_rx_q *rx_queue, struct rxbd8 *bdp,
Andy Fleming815b97c2008-04-22 17:18:29 -0500117 struct sk_buff *skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118static int gfar_set_mac_address(struct net_device *dev);
119static int gfar_change_mtu(struct net_device *dev, int new_mtu);
David Howells7d12e782006-10-05 14:55:46 +0100120static irqreturn_t gfar_error(int irq, void *dev_id);
121static irqreturn_t gfar_transmit(int irq, void *dev_id);
122static irqreturn_t gfar_interrupt(int irq, void *dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123static void adjust_link(struct net_device *dev);
124static void init_registers(struct net_device *dev);
125static int init_phy(struct net_device *dev);
Grant Likely74888762011-02-22 21:05:51 -0700126static int gfar_probe(struct platform_device *ofdev);
Grant Likely2dc11582010-08-06 09:25:50 -0600127static int gfar_remove(struct platform_device *ofdev);
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400128static void free_skb_resources(struct gfar_private *priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129static void gfar_set_multi(struct net_device *dev);
130static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr);
Kapil Junejad3c12872007-05-11 18:25:11 -0500131static void gfar_configure_serdes(struct net_device *dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700132static int gfar_poll(struct napi_struct *napi, int budget);
Vitaly Woolf2d71c22006-11-07 13:27:02 +0300133#ifdef CONFIG_NET_POLL_CONTROLLER
134static void gfar_netpoll(struct net_device *dev);
135#endif
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000136int gfar_clean_rx_ring(struct gfar_priv_rx_q *rx_queue, int rx_work_limit);
137static int gfar_clean_tx_ring(struct gfar_priv_tx_q *tx_queue);
Dai Haruki2c2db482008-12-16 15:31:15 -0800138static int gfar_process_frame(struct net_device *dev, struct sk_buff *skb,
139 int amount_pull);
Kumar Gala0bbaf062005-06-20 10:54:21 -0500140static void gfar_vlan_rx_register(struct net_device *netdev,
141 struct vlan_group *grp);
Andy Fleming7f7f5312005-11-11 12:38:59 -0600142void gfar_halt(struct net_device *dev);
Scott Woodd87eb122008-07-11 18:04:45 -0500143static void gfar_halt_nodisable(struct net_device *dev);
Andy Fleming7f7f5312005-11-11 12:38:59 -0600144void gfar_start(struct net_device *dev);
145static void gfar_clear_exact_match(struct net_device *dev);
Joe Perchesb6bc7652010-12-21 02:16:08 -0800146static void gfar_set_mac_for_addr(struct net_device *dev, int num,
147 const u8 *addr);
Andy Fleming26ccfc32009-03-10 12:58:28 +0000148static int gfar_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150MODULE_AUTHOR("Freescale Semiconductor, Inc");
151MODULE_DESCRIPTION("Gianfar Ethernet Driver");
152MODULE_LICENSE("GPL");
153
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000154static void gfar_init_rxbdp(struct gfar_priv_rx_q *rx_queue, struct rxbd8 *bdp,
Anton Vorontsov8a102fe2009-10-12 06:00:37 +0000155 dma_addr_t buf)
156{
Anton Vorontsov8a102fe2009-10-12 06:00:37 +0000157 u32 lstatus;
158
159 bdp->bufPtr = buf;
160
161 lstatus = BD_LFLAG(RXBD_EMPTY | RXBD_INTERRUPT);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000162 if (bdp == rx_queue->rx_bd_base + rx_queue->rx_ring_size - 1)
Anton Vorontsov8a102fe2009-10-12 06:00:37 +0000163 lstatus |= BD_LFLAG(RXBD_WRAP);
164
165 eieio();
166
167 bdp->lstatus = lstatus;
168}
169
Anton Vorontsov87283272009-10-12 06:00:39 +0000170static int gfar_init_bds(struct net_device *ndev)
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000171{
Anton Vorontsov87283272009-10-12 06:00:39 +0000172 struct gfar_private *priv = netdev_priv(ndev);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000173 struct gfar_priv_tx_q *tx_queue = NULL;
174 struct gfar_priv_rx_q *rx_queue = NULL;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000175 struct txbd8 *txbdp;
176 struct rxbd8 *rxbdp;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000177 int i, j;
Anton Vorontsov87283272009-10-12 06:00:39 +0000178
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000179 for (i = 0; i < priv->num_tx_queues; i++) {
180 tx_queue = priv->tx_queue[i];
181 /* Initialize some variables in our dev structure */
182 tx_queue->num_txbdfree = tx_queue->tx_ring_size;
183 tx_queue->dirty_tx = tx_queue->tx_bd_base;
184 tx_queue->cur_tx = tx_queue->tx_bd_base;
185 tx_queue->skb_curtx = 0;
186 tx_queue->skb_dirtytx = 0;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000187
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000188 /* Initialize Transmit Descriptor Ring */
189 txbdp = tx_queue->tx_bd_base;
190 for (j = 0; j < tx_queue->tx_ring_size; j++) {
191 txbdp->lstatus = 0;
192 txbdp->bufPtr = 0;
193 txbdp++;
Anton Vorontsov87283272009-10-12 06:00:39 +0000194 }
195
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000196 /* Set the last descriptor in the ring to indicate wrap */
197 txbdp--;
198 txbdp->status |= TXBD_WRAP;
199 }
200
201 for (i = 0; i < priv->num_rx_queues; i++) {
202 rx_queue = priv->rx_queue[i];
203 rx_queue->cur_rx = rx_queue->rx_bd_base;
204 rx_queue->skb_currx = 0;
205 rxbdp = rx_queue->rx_bd_base;
206
207 for (j = 0; j < rx_queue->rx_ring_size; j++) {
208 struct sk_buff *skb = rx_queue->rx_skbuff[j];
209
210 if (skb) {
211 gfar_init_rxbdp(rx_queue, rxbdp,
212 rxbdp->bufPtr);
213 } else {
214 skb = gfar_new_skb(ndev);
215 if (!skb) {
216 pr_err("%s: Can't allocate RX buffers\n",
217 ndev->name);
218 goto err_rxalloc_fail;
219 }
220 rx_queue->rx_skbuff[j] = skb;
221
222 gfar_new_rxbdp(rx_queue, rxbdp, skb);
223 }
224
225 rxbdp++;
226 }
227
Anton Vorontsov87283272009-10-12 06:00:39 +0000228 }
229
230 return 0;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000231
232err_rxalloc_fail:
233 free_skb_resources(priv);
234 return -ENOMEM;
Anton Vorontsov87283272009-10-12 06:00:39 +0000235}
236
237static int gfar_alloc_skb_resources(struct net_device *ndev)
238{
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000239 void *vaddr;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000240 dma_addr_t addr;
241 int i, j, k;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000242 struct gfar_private *priv = netdev_priv(ndev);
243 struct device *dev = &priv->ofdev->dev;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000244 struct gfar_priv_tx_q *tx_queue = NULL;
245 struct gfar_priv_rx_q *rx_queue = NULL;
246
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000247 priv->total_tx_ring_size = 0;
248 for (i = 0; i < priv->num_tx_queues; i++)
249 priv->total_tx_ring_size += priv->tx_queue[i]->tx_ring_size;
250
251 priv->total_rx_ring_size = 0;
252 for (i = 0; i < priv->num_rx_queues; i++)
253 priv->total_rx_ring_size += priv->rx_queue[i]->rx_ring_size;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000254
255 /* Allocate memory for the buffer descriptors */
Anton Vorontsov87283272009-10-12 06:00:39 +0000256 vaddr = dma_alloc_coherent(dev,
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000257 sizeof(struct txbd8) * priv->total_tx_ring_size +
258 sizeof(struct rxbd8) * priv->total_rx_ring_size,
259 &addr, GFP_KERNEL);
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000260 if (!vaddr) {
261 if (netif_msg_ifup(priv))
262 pr_err("%s: Could not allocate buffer descriptors!\n",
263 ndev->name);
264 return -ENOMEM;
265 }
266
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000267 for (i = 0; i < priv->num_tx_queues; i++) {
268 tx_queue = priv->tx_queue[i];
269 tx_queue->tx_bd_base = (struct txbd8 *) vaddr;
270 tx_queue->tx_bd_dma_base = addr;
271 tx_queue->dev = ndev;
272 /* enet DMA only understands physical addresses */
273 addr += sizeof(struct txbd8) *tx_queue->tx_ring_size;
274 vaddr += sizeof(struct txbd8) *tx_queue->tx_ring_size;
275 }
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000276
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000277 /* Start the rx descriptor ring where the tx ring leaves off */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000278 for (i = 0; i < priv->num_rx_queues; i++) {
279 rx_queue = priv->rx_queue[i];
280 rx_queue->rx_bd_base = (struct rxbd8 *) vaddr;
281 rx_queue->rx_bd_dma_base = addr;
282 rx_queue->dev = ndev;
283 addr += sizeof (struct rxbd8) * rx_queue->rx_ring_size;
284 vaddr += sizeof (struct rxbd8) * rx_queue->rx_ring_size;
285 }
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000286
287 /* Setup the skbuff rings */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000288 for (i = 0; i < priv->num_tx_queues; i++) {
289 tx_queue = priv->tx_queue[i];
290 tx_queue->tx_skbuff = kmalloc(sizeof(*tx_queue->tx_skbuff) *
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000291 tx_queue->tx_ring_size, GFP_KERNEL);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000292 if (!tx_queue->tx_skbuff) {
293 if (netif_msg_ifup(priv))
294 pr_err("%s: Could not allocate tx_skbuff\n",
295 ndev->name);
296 goto cleanup;
297 }
298
299 for (k = 0; k < tx_queue->tx_ring_size; k++)
300 tx_queue->tx_skbuff[k] = NULL;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000301 }
302
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000303 for (i = 0; i < priv->num_rx_queues; i++) {
304 rx_queue = priv->rx_queue[i];
305 rx_queue->rx_skbuff = kmalloc(sizeof(*rx_queue->rx_skbuff) *
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000306 rx_queue->rx_ring_size, GFP_KERNEL);
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000307
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000308 if (!rx_queue->rx_skbuff) {
309 if (netif_msg_ifup(priv))
310 pr_err("%s: Could not allocate rx_skbuff\n",
311 ndev->name);
312 goto cleanup;
313 }
314
315 for (j = 0; j < rx_queue->rx_ring_size; j++)
316 rx_queue->rx_skbuff[j] = NULL;
317 }
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000318
Anton Vorontsov87283272009-10-12 06:00:39 +0000319 if (gfar_init_bds(ndev))
320 goto cleanup;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000321
322 return 0;
323
324cleanup:
325 free_skb_resources(priv);
326 return -ENOMEM;
327}
328
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000329static void gfar_init_tx_rx_base(struct gfar_private *priv)
330{
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000331 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Anton Vorontsov18294ad2009-11-04 12:53:00 +0000332 u32 __iomem *baddr;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000333 int i;
334
335 baddr = &regs->tbase0;
336 for(i = 0; i < priv->num_tx_queues; i++) {
337 gfar_write(baddr, priv->tx_queue[i]->tx_bd_dma_base);
338 baddr += 2;
339 }
340
341 baddr = &regs->rbase0;
342 for(i = 0; i < priv->num_rx_queues; i++) {
343 gfar_write(baddr, priv->rx_queue[i]->rx_bd_dma_base);
344 baddr += 2;
345 }
346}
347
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000348static void gfar_init_mac(struct net_device *ndev)
349{
350 struct gfar_private *priv = netdev_priv(ndev);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000351 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000352 u32 rctrl = 0;
353 u32 tctrl = 0;
354 u32 attrs = 0;
355
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000356 /* write the tx/rx base registers */
357 gfar_init_tx_rx_base(priv);
Anton Vorontsov32c513b2009-10-12 06:00:36 +0000358
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000359 /* Configure the coalescing support */
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000360 gfar_configure_coalescing(priv, 0xFF, 0xFF);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000361
Sandeep Gopalpet1ccb8382009-12-16 01:14:58 +0000362 if (priv->rx_filer_enable) {
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000363 rctrl |= RCTRL_FILREN;
Sandeep Gopalpet1ccb8382009-12-16 01:14:58 +0000364 /* Program the RIR0 reg with the required distribution */
365 gfar_write(&regs->rir0, DEFAULT_RIR0);
366 }
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000367
368 if (priv->rx_csum_enable)
369 rctrl |= RCTRL_CHECKSUMMING;
370
371 if (priv->extended_hash) {
372 rctrl |= RCTRL_EXTHASH;
373
374 gfar_clear_exact_match(ndev);
375 rctrl |= RCTRL_EMEN;
376 }
377
378 if (priv->padding) {
379 rctrl &= ~RCTRL_PAL_MASK;
380 rctrl |= RCTRL_PADDING(priv->padding);
381 }
382
Manfred Rudigiercc772ab2010-04-08 23:10:03 +0000383 /* Insert receive time stamps into padding alignment bytes */
384 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER) {
385 rctrl &= ~RCTRL_PAL_MASK;
Manfred Rudigier97553f72010-06-11 01:49:05 +0000386 rctrl |= RCTRL_PADDING(8);
Manfred Rudigiercc772ab2010-04-08 23:10:03 +0000387 priv->padding = 8;
388 }
389
Manfred Rudigier97553f72010-06-11 01:49:05 +0000390 /* Enable HW time stamping if requested from user space */
391 if (priv->hwts_rx_en)
392 rctrl |= RCTRL_PRSDEP_INIT | RCTRL_TS_ENABLE;
393
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000394 /* keep vlan related bits if it's enabled */
395 if (priv->vlgrp) {
396 rctrl |= RCTRL_VLEX | RCTRL_PRSDEP_INIT;
397 tctrl |= TCTRL_VLINS;
398 }
399
400 /* Init rctrl based on our settings */
401 gfar_write(&regs->rctrl, rctrl);
402
403 if (ndev->features & NETIF_F_IP_CSUM)
404 tctrl |= TCTRL_INIT_CSUM;
405
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000406 tctrl |= TCTRL_TXSCHED_PRIO;
407
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000408 gfar_write(&regs->tctrl, tctrl);
409
410 /* Set the extraction length and index */
411 attrs = ATTRELI_EL(priv->rx_stash_size) |
412 ATTRELI_EI(priv->rx_stash_index);
413
414 gfar_write(&regs->attreli, attrs);
415
416 /* Start with defaults, and add stashing or locking
417 * depending on the approprate variables */
418 attrs = ATTR_INIT_SETTINGS;
419
420 if (priv->bd_stash_en)
421 attrs |= ATTR_BDSTASH;
422
423 if (priv->rx_stash_size != 0)
424 attrs |= ATTR_BUFSTASH;
425
426 gfar_write(&regs->attr, attrs);
427
428 gfar_write(&regs->fifo_tx_thr, priv->fifo_threshold);
429 gfar_write(&regs->fifo_tx_starve, priv->fifo_starve);
430 gfar_write(&regs->fifo_tx_starve_shutoff, priv->fifo_starve_off);
431}
432
Sandeep Gopalpeta7f38042009-12-16 01:15:07 +0000433static struct net_device_stats *gfar_get_stats(struct net_device *dev)
434{
435 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpeta7f38042009-12-16 01:15:07 +0000436 unsigned long rx_packets = 0, rx_bytes = 0, rx_dropped = 0;
437 unsigned long tx_packets = 0, tx_bytes = 0;
438 int i = 0;
439
440 for (i = 0; i < priv->num_rx_queues; i++) {
441 rx_packets += priv->rx_queue[i]->stats.rx_packets;
442 rx_bytes += priv->rx_queue[i]->stats.rx_bytes;
443 rx_dropped += priv->rx_queue[i]->stats.rx_dropped;
444 }
445
446 dev->stats.rx_packets = rx_packets;
447 dev->stats.rx_bytes = rx_bytes;
448 dev->stats.rx_dropped = rx_dropped;
449
450 for (i = 0; i < priv->num_tx_queues; i++) {
Eric Dumazet1ac9ad12011-01-12 12:13:14 +0000451 tx_bytes += priv->tx_queue[i]->stats.tx_bytes;
452 tx_packets += priv->tx_queue[i]->stats.tx_packets;
Sandeep Gopalpeta7f38042009-12-16 01:15:07 +0000453 }
454
455 dev->stats.tx_bytes = tx_bytes;
456 dev->stats.tx_packets = tx_packets;
457
458 return &dev->stats;
459}
460
Andy Fleming26ccfc32009-03-10 12:58:28 +0000461static const struct net_device_ops gfar_netdev_ops = {
462 .ndo_open = gfar_enet_open,
463 .ndo_start_xmit = gfar_start_xmit,
464 .ndo_stop = gfar_close,
465 .ndo_change_mtu = gfar_change_mtu,
466 .ndo_set_multicast_list = gfar_set_multi,
467 .ndo_tx_timeout = gfar_timeout,
468 .ndo_do_ioctl = gfar_ioctl,
Sandeep Gopalpeta7f38042009-12-16 01:15:07 +0000469 .ndo_get_stats = gfar_get_stats,
Andy Fleming26ccfc32009-03-10 12:58:28 +0000470 .ndo_vlan_rx_register = gfar_vlan_rx_register,
Ben Hutchings240c1022009-07-09 17:54:35 +0000471 .ndo_set_mac_address = eth_mac_addr,
472 .ndo_validate_addr = eth_validate_addr,
Andy Fleming26ccfc32009-03-10 12:58:28 +0000473#ifdef CONFIG_NET_POLL_CONTROLLER
474 .ndo_poll_controller = gfar_netpoll,
475#endif
476};
477
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000478unsigned int ftp_rqfpr[MAX_FILER_IDX + 1];
479unsigned int ftp_rqfcr[MAX_FILER_IDX + 1];
480
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000481void lock_rx_qs(struct gfar_private *priv)
482{
483 int i = 0x0;
484
485 for (i = 0; i < priv->num_rx_queues; i++)
486 spin_lock(&priv->rx_queue[i]->rxlock);
487}
488
489void lock_tx_qs(struct gfar_private *priv)
490{
491 int i = 0x0;
492
493 for (i = 0; i < priv->num_tx_queues; i++)
494 spin_lock(&priv->tx_queue[i]->txlock);
495}
496
497void unlock_rx_qs(struct gfar_private *priv)
498{
499 int i = 0x0;
500
501 for (i = 0; i < priv->num_rx_queues; i++)
502 spin_unlock(&priv->rx_queue[i]->rxlock);
503}
504
505void unlock_tx_qs(struct gfar_private *priv)
506{
507 int i = 0x0;
508
509 for (i = 0; i < priv->num_tx_queues; i++)
510 spin_unlock(&priv->tx_queue[i]->txlock);
511}
512
Andy Fleming7f7f5312005-11-11 12:38:59 -0600513/* Returns 1 if incoming frames use an FCB */
514static inline int gfar_uses_fcb(struct gfar_private *priv)
Kumar Gala0bbaf062005-06-20 10:54:21 -0500515{
Manfred Rudigiercc772ab2010-04-08 23:10:03 +0000516 return priv->vlgrp || priv->rx_csum_enable ||
517 (priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER);
Kumar Gala0bbaf062005-06-20 10:54:21 -0500518}
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400519
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000520static void free_tx_pointers(struct gfar_private *priv)
521{
522 int i = 0;
523
524 for (i = 0; i < priv->num_tx_queues; i++)
525 kfree(priv->tx_queue[i]);
526}
527
528static void free_rx_pointers(struct gfar_private *priv)
529{
530 int i = 0;
531
532 for (i = 0; i < priv->num_rx_queues; i++)
533 kfree(priv->rx_queue[i]);
534}
535
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000536static void unmap_group_regs(struct gfar_private *priv)
537{
538 int i = 0;
539
540 for (i = 0; i < MAXGROUPS; i++)
541 if (priv->gfargrp[i].regs)
542 iounmap(priv->gfargrp[i].regs);
543}
544
545static void disable_napi(struct gfar_private *priv)
546{
547 int i = 0;
548
549 for (i = 0; i < priv->num_grps; i++)
550 napi_disable(&priv->gfargrp[i].napi);
551}
552
553static void enable_napi(struct gfar_private *priv)
554{
555 int i = 0;
556
557 for (i = 0; i < priv->num_grps; i++)
558 napi_enable(&priv->gfargrp[i].napi);
559}
560
561static int gfar_parse_group(struct device_node *np,
562 struct gfar_private *priv, const char *model)
563{
564 u32 *queue_mask;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000565
Anton Vorontsov7ce97d42010-04-23 07:12:44 +0000566 priv->gfargrp[priv->num_grps].regs = of_iomap(np, 0);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000567 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);
Nicolas Kaiser28cb6cc2010-11-15 10:59:42 +0000579 if (priv->gfargrp[priv->num_grps].interruptTransmit == NO_IRQ ||
580 priv->gfargrp[priv->num_grps].interruptReceive == NO_IRQ ||
581 priv->gfargrp[priv->num_grps].interruptError == NO_IRQ)
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000582 return -EINVAL;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000583 }
584
585 priv->gfargrp[priv->num_grps].grp_id = priv->num_grps;
586 priv->gfargrp[priv->num_grps].priv = priv;
587 spin_lock_init(&priv->gfargrp[priv->num_grps].grplock);
588 if(priv->mode == MQ_MG_MODE) {
589 queue_mask = (u32 *)of_get_property(np,
590 "fsl,rx-bit-map", NULL);
591 priv->gfargrp[priv->num_grps].rx_bit_map =
592 queue_mask ? *queue_mask :(DEFAULT_MAPPING >> priv->num_grps);
593 queue_mask = (u32 *)of_get_property(np,
594 "fsl,tx-bit-map", NULL);
595 priv->gfargrp[priv->num_grps].tx_bit_map =
596 queue_mask ? *queue_mask : (DEFAULT_MAPPING >> priv->num_grps);
597 } else {
598 priv->gfargrp[priv->num_grps].rx_bit_map = 0xFF;
599 priv->gfargrp[priv->num_grps].tx_bit_map = 0xFF;
600 }
601 priv->num_grps++;
602
603 return 0;
604}
605
Grant Likely2dc11582010-08-06 09:25:50 -0600606static int gfar_of_init(struct platform_device *ofdev, struct net_device **pdev)
Andy Flemingb31a1d82008-12-16 15:29:15 -0800607{
Andy Flemingb31a1d82008-12-16 15:29:15 -0800608 const char *model;
609 const char *ctype;
610 const void *mac_addr;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000611 int err = 0, i;
612 struct net_device *dev = NULL;
613 struct gfar_private *priv = NULL;
Grant Likely61c7a082010-04-13 16:12:29 -0700614 struct device_node *np = ofdev->dev.of_node;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000615 struct device_node *child = NULL;
Andy Fleming4d7902f2009-02-04 16:43:44 -0800616 const u32 *stash;
617 const u32 *stash_len;
618 const u32 *stash_idx;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000619 unsigned int num_tx_qs, num_rx_qs;
620 u32 *tx_queues, *rx_queues;
Andy Flemingb31a1d82008-12-16 15:29:15 -0800621
622 if (!np || !of_device_is_available(np))
623 return -ENODEV;
624
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000625 /* parse the num of tx and rx queues */
626 tx_queues = (u32 *)of_get_property(np, "fsl,num_tx_queues", NULL);
627 num_tx_qs = tx_queues ? *tx_queues : 1;
628
629 if (num_tx_qs > MAX_TX_QS) {
630 printk(KERN_ERR "num_tx_qs(=%d) greater than MAX_TX_QS(=%d)\n",
631 num_tx_qs, MAX_TX_QS);
632 printk(KERN_ERR "Cannot do alloc_etherdev, aborting\n");
633 return -EINVAL;
634 }
635
636 rx_queues = (u32 *)of_get_property(np, "fsl,num_rx_queues", NULL);
637 num_rx_qs = rx_queues ? *rx_queues : 1;
638
639 if (num_rx_qs > MAX_RX_QS) {
640 printk(KERN_ERR "num_rx_qs(=%d) greater than MAX_RX_QS(=%d)\n",
641 num_tx_qs, MAX_TX_QS);
642 printk(KERN_ERR "Cannot do alloc_etherdev, aborting\n");
643 return -EINVAL;
644 }
645
646 *pdev = alloc_etherdev_mq(sizeof(*priv), num_tx_qs);
647 dev = *pdev;
648 if (NULL == dev)
649 return -ENOMEM;
650
651 priv = netdev_priv(dev);
Grant Likely61c7a082010-04-13 16:12:29 -0700652 priv->node = ofdev->dev.of_node;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000653 priv->ndev = dev;
654
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000655 priv->num_tx_queues = num_tx_qs;
Ben Hutchingsfe069122010-09-27 08:27:37 +0000656 netif_set_real_num_rx_queues(dev, num_rx_qs);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000657 priv->num_rx_queues = num_rx_qs;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000658 priv->num_grps = 0x0;
Andy Flemingb31a1d82008-12-16 15:29:15 -0800659
660 model = of_get_property(np, "model", NULL);
661
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000662 for (i = 0; i < MAXGROUPS; i++)
663 priv->gfargrp[i].regs = NULL;
Andy Flemingb31a1d82008-12-16 15:29:15 -0800664
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000665 /* Parse and initialize group specific information */
666 if (of_device_is_compatible(np, "fsl,etsec2")) {
667 priv->mode = MQ_MG_MODE;
668 for_each_child_of_node(np, child) {
669 err = gfar_parse_group(child, priv, model);
670 if (err)
671 goto err_grp_init;
Andy Flemingb31a1d82008-12-16 15:29:15 -0800672 }
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000673 } else {
674 priv->mode = SQ_SG_MODE;
675 err = gfar_parse_group(np, priv, model);
676 if(err)
677 goto err_grp_init;
Andy Flemingb31a1d82008-12-16 15:29:15 -0800678 }
679
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000680 for (i = 0; i < priv->num_tx_queues; i++)
681 priv->tx_queue[i] = NULL;
682 for (i = 0; i < priv->num_rx_queues; i++)
683 priv->rx_queue[i] = NULL;
684
685 for (i = 0; i < priv->num_tx_queues; i++) {
Joe Perchesde47f072010-05-31 17:23:12 +0000686 priv->tx_queue[i] = kzalloc(sizeof(struct gfar_priv_tx_q),
687 GFP_KERNEL);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000688 if (!priv->tx_queue[i]) {
689 err = -ENOMEM;
690 goto tx_alloc_failed;
691 }
692 priv->tx_queue[i]->tx_skbuff = NULL;
693 priv->tx_queue[i]->qindex = i;
694 priv->tx_queue[i]->dev = dev;
695 spin_lock_init(&(priv->tx_queue[i]->txlock));
696 }
697
698 for (i = 0; i < priv->num_rx_queues; i++) {
Joe Perchesde47f072010-05-31 17:23:12 +0000699 priv->rx_queue[i] = kzalloc(sizeof(struct gfar_priv_rx_q),
700 GFP_KERNEL);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000701 if (!priv->rx_queue[i]) {
702 err = -ENOMEM;
703 goto rx_alloc_failed;
704 }
705 priv->rx_queue[i]->rx_skbuff = NULL;
706 priv->rx_queue[i]->qindex = i;
707 priv->rx_queue[i]->dev = dev;
708 spin_lock_init(&(priv->rx_queue[i]->rxlock));
709 }
710
711
Andy Fleming4d7902f2009-02-04 16:43:44 -0800712 stash = of_get_property(np, "bd-stash", NULL);
713
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000714 if (stash) {
Andy Fleming4d7902f2009-02-04 16:43:44 -0800715 priv->device_flags |= FSL_GIANFAR_DEV_HAS_BD_STASHING;
716 priv->bd_stash_en = 1;
717 }
718
719 stash_len = of_get_property(np, "rx-stash-len", NULL);
720
721 if (stash_len)
722 priv->rx_stash_size = *stash_len;
723
724 stash_idx = of_get_property(np, "rx-stash-idx", NULL);
725
726 if (stash_idx)
727 priv->rx_stash_index = *stash_idx;
728
729 if (stash_len || stash_idx)
730 priv->device_flags |= FSL_GIANFAR_DEV_HAS_BUF_STASHING;
731
Andy Flemingb31a1d82008-12-16 15:29:15 -0800732 mac_addr = of_get_mac_address(np);
733 if (mac_addr)
734 memcpy(dev->dev_addr, mac_addr, MAC_ADDR_LEN);
735
736 if (model && !strcasecmp(model, "TSEC"))
737 priv->device_flags =
738 FSL_GIANFAR_DEV_HAS_GIGABIT |
739 FSL_GIANFAR_DEV_HAS_COALESCE |
740 FSL_GIANFAR_DEV_HAS_RMON |
741 FSL_GIANFAR_DEV_HAS_MULTI_INTR;
742 if (model && !strcasecmp(model, "eTSEC"))
743 priv->device_flags =
744 FSL_GIANFAR_DEV_HAS_GIGABIT |
745 FSL_GIANFAR_DEV_HAS_COALESCE |
746 FSL_GIANFAR_DEV_HAS_RMON |
747 FSL_GIANFAR_DEV_HAS_MULTI_INTR |
Dai Haruki2c2db482008-12-16 15:31:15 -0800748 FSL_GIANFAR_DEV_HAS_PADDING |
Andy Flemingb31a1d82008-12-16 15:29:15 -0800749 FSL_GIANFAR_DEV_HAS_CSUM |
750 FSL_GIANFAR_DEV_HAS_VLAN |
751 FSL_GIANFAR_DEV_HAS_MAGIC_PACKET |
Manfred Rudigier97553f72010-06-11 01:49:05 +0000752 FSL_GIANFAR_DEV_HAS_EXTENDED_HASH |
753 FSL_GIANFAR_DEV_HAS_TIMER;
Andy Flemingb31a1d82008-12-16 15:29:15 -0800754
755 ctype = of_get_property(np, "phy-connection-type", NULL);
756
757 /* We only care about rgmii-id. The rest are autodetected */
758 if (ctype && !strcmp(ctype, "rgmii-id"))
759 priv->interface = PHY_INTERFACE_MODE_RGMII_ID;
760 else
761 priv->interface = PHY_INTERFACE_MODE_MII;
762
763 if (of_get_property(np, "fsl,magic-packet", NULL))
764 priv->device_flags |= FSL_GIANFAR_DEV_HAS_MAGIC_PACKET;
765
Grant Likelyfe192a42009-04-25 12:53:12 +0000766 priv->phy_node = of_parse_phandle(np, "phy-handle", 0);
Andy Flemingb31a1d82008-12-16 15:29:15 -0800767
768 /* Find the TBI PHY. If it's not there, we don't support SGMII */
Grant Likelyfe192a42009-04-25 12:53:12 +0000769 priv->tbi_node = of_parse_phandle(np, "tbi-handle", 0);
Andy Flemingb31a1d82008-12-16 15:29:15 -0800770
771 return 0;
772
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000773rx_alloc_failed:
774 free_rx_pointers(priv);
775tx_alloc_failed:
776 free_tx_pointers(priv);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000777err_grp_init:
778 unmap_group_regs(priv);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000779 free_netdev(dev);
Andy Flemingb31a1d82008-12-16 15:29:15 -0800780 return err;
781}
782
Manfred Rudigiercc772ab2010-04-08 23:10:03 +0000783static int gfar_hwtstamp_ioctl(struct net_device *netdev,
784 struct ifreq *ifr, int cmd)
785{
786 struct hwtstamp_config config;
787 struct gfar_private *priv = netdev_priv(netdev);
788
789 if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
790 return -EFAULT;
791
792 /* reserved for future extensions */
793 if (config.flags)
794 return -EINVAL;
795
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +0000796 switch (config.tx_type) {
797 case HWTSTAMP_TX_OFF:
798 priv->hwts_tx_en = 0;
799 break;
800 case HWTSTAMP_TX_ON:
801 if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER))
802 return -ERANGE;
803 priv->hwts_tx_en = 1;
804 break;
805 default:
Manfred Rudigiercc772ab2010-04-08 23:10:03 +0000806 return -ERANGE;
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +0000807 }
Manfred Rudigiercc772ab2010-04-08 23:10:03 +0000808
809 switch (config.rx_filter) {
810 case HWTSTAMP_FILTER_NONE:
Manfred Rudigier97553f72010-06-11 01:49:05 +0000811 if (priv->hwts_rx_en) {
812 stop_gfar(netdev);
813 priv->hwts_rx_en = 0;
814 startup_gfar(netdev);
815 }
Manfred Rudigiercc772ab2010-04-08 23:10:03 +0000816 break;
817 default:
818 if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER))
819 return -ERANGE;
Manfred Rudigier97553f72010-06-11 01:49:05 +0000820 if (!priv->hwts_rx_en) {
821 stop_gfar(netdev);
822 priv->hwts_rx_en = 1;
823 startup_gfar(netdev);
824 }
Manfred Rudigiercc772ab2010-04-08 23:10:03 +0000825 config.rx_filter = HWTSTAMP_FILTER_ALL;
826 break;
827 }
828
829 return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
830 -EFAULT : 0;
831}
832
Clifford Wolf0faac9f2009-01-09 10:23:11 +0000833/* Ioctl MII Interface */
834static int gfar_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
835{
836 struct gfar_private *priv = netdev_priv(dev);
837
838 if (!netif_running(dev))
839 return -EINVAL;
840
Manfred Rudigiercc772ab2010-04-08 23:10:03 +0000841 if (cmd == SIOCSHWTSTAMP)
842 return gfar_hwtstamp_ioctl(dev, rq, cmd);
843
Clifford Wolf0faac9f2009-01-09 10:23:11 +0000844 if (!priv->phydev)
845 return -ENODEV;
846
Richard Cochran28b04112010-07-17 08:48:55 +0000847 return phy_mii_ioctl(priv->phydev, rq, cmd);
Clifford Wolf0faac9f2009-01-09 10:23:11 +0000848}
849
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000850static unsigned int reverse_bitmap(unsigned int bit_map, unsigned int max_qs)
851{
852 unsigned int new_bit_map = 0x0;
853 int mask = 0x1 << (max_qs - 1), i;
854 for (i = 0; i < max_qs; i++) {
855 if (bit_map & mask)
856 new_bit_map = new_bit_map + (1 << i);
857 mask = mask >> 0x1;
858 }
859 return new_bit_map;
860}
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000861
Anton Vorontsov18294ad2009-11-04 12:53:00 +0000862static u32 cluster_entry_per_class(struct gfar_private *priv, u32 rqfar,
863 u32 class)
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000864{
865 u32 rqfpr = FPR_FILER_MASK;
866 u32 rqfcr = 0x0;
867
868 rqfar--;
869 rqfcr = RQFCR_CLE | RQFCR_PID_MASK | RQFCR_CMP_EXACT;
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_NOMATCH;
876 ftp_rqfpr[rqfar] = rqfpr;
877 ftp_rqfcr[rqfar] = rqfcr;
878 gfar_write_filer(priv, rqfar, rqfcr, rqfpr);
879
880 rqfar--;
881 rqfcr = RQFCR_CMP_EXACT | RQFCR_PID_PARSE | RQFCR_CLE | RQFCR_AND;
882 rqfpr = class;
883 ftp_rqfcr[rqfar] = rqfcr;
884 ftp_rqfpr[rqfar] = rqfpr;
885 gfar_write_filer(priv, rqfar, rqfcr, rqfpr);
886
887 rqfar--;
888 rqfcr = RQFCR_CMP_EXACT | RQFCR_PID_MASK | RQFCR_AND;
889 rqfpr = class;
890 ftp_rqfcr[rqfar] = rqfcr;
891 ftp_rqfpr[rqfar] = rqfpr;
892 gfar_write_filer(priv, rqfar, rqfcr, rqfpr);
893
894 return rqfar;
895}
896
897static void gfar_init_filer_table(struct gfar_private *priv)
898{
899 int i = 0x0;
900 u32 rqfar = MAX_FILER_IDX;
901 u32 rqfcr = 0x0;
902 u32 rqfpr = FPR_FILER_MASK;
903
904 /* Default rule */
905 rqfcr = RQFCR_CMP_MATCH;
906 ftp_rqfcr[rqfar] = rqfcr;
907 ftp_rqfpr[rqfar] = rqfpr;
908 gfar_write_filer(priv, rqfar, rqfcr, rqfpr);
909
910 rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV6);
911 rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV6 | RQFPR_UDP);
912 rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV6 | RQFPR_TCP);
913 rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV4);
914 rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV4 | RQFPR_UDP);
915 rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV4 | RQFPR_TCP);
916
Uwe Kleine-König85dd08e2010-06-11 12:16:55 +0200917 /* cur_filer_idx indicated the first non-masked rule */
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000918 priv->cur_filer_idx = rqfar;
919
920 /* Rest are masked rules */
921 rqfcr = RQFCR_CMP_NOMATCH;
922 for (i = 0; i < rqfar; i++) {
923 ftp_rqfcr[i] = rqfcr;
924 ftp_rqfpr[i] = rqfpr;
925 gfar_write_filer(priv, i, rqfcr, rqfpr);
926 }
927}
928
Anton Vorontsov7d350972010-06-30 06:39:12 +0000929static void gfar_detect_errata(struct gfar_private *priv)
930{
931 struct device *dev = &priv->ofdev->dev;
932 unsigned int pvr = mfspr(SPRN_PVR);
933 unsigned int svr = mfspr(SPRN_SVR);
934 unsigned int mod = (svr >> 16) & 0xfff6; /* w/o E suffix */
935 unsigned int rev = svr & 0xffff;
936
937 /* MPC8313 Rev 2.0 and higher; All MPC837x */
938 if ((pvr == 0x80850010 && mod == 0x80b0 && rev >= 0x0020) ||
939 (pvr == 0x80861010 && (mod & 0xfff9) == 0x80c0))
940 priv->errata |= GFAR_ERRATA_74;
941
Anton Vorontsovdeb90ea2010-06-30 06:39:13 +0000942 /* MPC8313 and MPC837x all rev */
943 if ((pvr == 0x80850010 && mod == 0x80b0) ||
944 (pvr == 0x80861010 && (mod & 0xfff9) == 0x80c0))
945 priv->errata |= GFAR_ERRATA_76;
946
Anton Vorontsov511d9342010-06-30 06:39:15 +0000947 /* MPC8313 and MPC837x all rev */
948 if ((pvr == 0x80850010 && mod == 0x80b0) ||
949 (pvr == 0x80861010 && (mod & 0xfff9) == 0x80c0))
950 priv->errata |= GFAR_ERRATA_A002;
951
Anton Vorontsov7d350972010-06-30 06:39:12 +0000952 if (priv->errata)
953 dev_info(dev, "enabled errata workarounds, flags: 0x%x\n",
954 priv->errata);
955}
956
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400957/* Set up the ethernet device structure, private data,
958 * and anything else we need before we start */
Grant Likely74888762011-02-22 21:05:51 -0700959static int gfar_probe(struct platform_device *ofdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960{
961 u32 tempval;
962 struct net_device *dev = NULL;
963 struct gfar_private *priv = NULL;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000964 struct gfar __iomem *regs = NULL;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000965 int err = 0, i, grp_idx = 0;
Dai Harukic50a5d92008-12-17 16:51:32 -0800966 int len_devname;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000967 u32 rstat = 0, tstat = 0, rqueue = 0, tqueue = 0;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000968 u32 isrg = 0;
Anton Vorontsov18294ad2009-11-04 12:53:00 +0000969 u32 __iomem *baddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000971 err = gfar_of_init(ofdev, &dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000973 if (err)
974 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
976 priv = netdev_priv(dev);
Kumar Gala48268572009-03-18 23:28:22 -0700977 priv->ndev = dev;
978 priv->ofdev = ofdev;
Grant Likely61c7a082010-04-13 16:12:29 -0700979 priv->node = ofdev->dev.of_node;
Kumar Gala48268572009-03-18 23:28:22 -0700980 SET_NETDEV_DEV(dev, &ofdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
Scott Woodd87eb122008-07-11 18:04:45 -0500982 spin_lock_init(&priv->bflock);
Sebastian Siewiorab939902008-08-19 21:12:45 +0200983 INIT_WORK(&priv->reset_task, gfar_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
Andy Flemingb31a1d82008-12-16 15:29:15 -0800985 dev_set_drvdata(&ofdev->dev, priv);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000986 regs = priv->gfargrp[0].regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
Anton Vorontsov7d350972010-06-30 06:39:12 +0000988 gfar_detect_errata(priv);
989
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 /* Stop the DMA engine now, in case it was running before */
991 /* (The firmware could have used it, and left it running). */
Andy Fleming257d9382008-12-16 15:25:45 -0800992 gfar_halt(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
994 /* Reset MAC layer */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000995 gfar_write(&regs->maccfg1, MACCFG1_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
Andy Flemingb98ac702009-02-04 16:38:05 -0800997 /* We need to delay at least 3 TX clocks */
998 udelay(2);
999
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 tempval = (MACCFG1_TX_FLOW | MACCFG1_RX_FLOW);
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001001 gfar_write(&regs->maccfg1, tempval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
1003 /* Initialize MACCFG2. */
Anton Vorontsov7d350972010-06-30 06:39:12 +00001004 tempval = MACCFG2_INIT_SETTINGS;
1005 if (gfar_has_errata(priv, GFAR_ERRATA_74))
1006 tempval |= MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK;
1007 gfar_write(&regs->maccfg2, tempval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
1009 /* Initialize ECNTRL */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001010 gfar_write(&regs->ecntrl, ECNTRL_INIT_SETTINGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 /* Set the dev->base_addr to the gfar reg region */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001013 dev->base_addr = (unsigned long) regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
Andy Flemingb31a1d82008-12-16 15:29:15 -08001015 SET_NETDEV_DEV(dev, &ofdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
1017 /* Fill in the dev structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 dev->watchdog_timeo = TX_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 dev->mtu = 1500;
Andy Fleming26ccfc32009-03-10 12:58:28 +00001020 dev->netdev_ops = &gfar_netdev_ops;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001021 dev->ethtool_ops = &gfar_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001023 /* Register for napi ...We are registering NAPI for each grp */
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001024 for (i = 0; i < priv->num_grps; i++)
1025 netif_napi_add(dev, &priv->gfargrp[i].napi, gfar_poll, GFAR_DEV_WEIGHT);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001026
Andy Flemingb31a1d82008-12-16 15:29:15 -08001027 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_CSUM) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001028 priv->rx_csum_enable = 1;
Dai Haruki4669bc92008-12-17 16:51:04 -08001029 dev->features |= NETIF_F_IP_CSUM | NETIF_F_SG | NETIF_F_HIGHDMA;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001030 } else
1031 priv->rx_csum_enable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
Kumar Gala0bbaf062005-06-20 10:54:21 -05001033 priv->vlgrp = NULL;
1034
Andy Fleming26ccfc32009-03-10 12:58:28 +00001035 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_VLAN)
Kumar Gala0bbaf062005-06-20 10:54:21 -05001036 dev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001037
Andy Flemingb31a1d82008-12-16 15:29:15 -08001038 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_EXTENDED_HASH) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001039 priv->extended_hash = 1;
1040 priv->hash_width = 9;
1041
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001042 priv->hash_regs[0] = &regs->igaddr0;
1043 priv->hash_regs[1] = &regs->igaddr1;
1044 priv->hash_regs[2] = &regs->igaddr2;
1045 priv->hash_regs[3] = &regs->igaddr3;
1046 priv->hash_regs[4] = &regs->igaddr4;
1047 priv->hash_regs[5] = &regs->igaddr5;
1048 priv->hash_regs[6] = &regs->igaddr6;
1049 priv->hash_regs[7] = &regs->igaddr7;
1050 priv->hash_regs[8] = &regs->gaddr0;
1051 priv->hash_regs[9] = &regs->gaddr1;
1052 priv->hash_regs[10] = &regs->gaddr2;
1053 priv->hash_regs[11] = &regs->gaddr3;
1054 priv->hash_regs[12] = &regs->gaddr4;
1055 priv->hash_regs[13] = &regs->gaddr5;
1056 priv->hash_regs[14] = &regs->gaddr6;
1057 priv->hash_regs[15] = &regs->gaddr7;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001058
1059 } else {
1060 priv->extended_hash = 0;
1061 priv->hash_width = 8;
1062
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001063 priv->hash_regs[0] = &regs->gaddr0;
1064 priv->hash_regs[1] = &regs->gaddr1;
1065 priv->hash_regs[2] = &regs->gaddr2;
1066 priv->hash_regs[3] = &regs->gaddr3;
1067 priv->hash_regs[4] = &regs->gaddr4;
1068 priv->hash_regs[5] = &regs->gaddr5;
1069 priv->hash_regs[6] = &regs->gaddr6;
1070 priv->hash_regs[7] = &regs->gaddr7;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001071 }
1072
Andy Flemingb31a1d82008-12-16 15:29:15 -08001073 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_PADDING)
Kumar Gala0bbaf062005-06-20 10:54:21 -05001074 priv->padding = DEFAULT_PADDING;
1075 else
1076 priv->padding = 0;
1077
Manfred Rudigiercc772ab2010-04-08 23:10:03 +00001078 if (dev->features & NETIF_F_IP_CSUM ||
1079 priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER)
Kumar Gala0bbaf062005-06-20 10:54:21 -05001080 dev->hard_header_len += GMAC_FCB_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001082 /* Program the isrg regs only if number of grps > 1 */
1083 if (priv->num_grps > 1) {
1084 baddr = &regs->isrg0;
1085 for (i = 0; i < priv->num_grps; i++) {
1086 isrg |= (priv->gfargrp[i].rx_bit_map << ISRG_SHIFT_RX);
1087 isrg |= (priv->gfargrp[i].tx_bit_map << ISRG_SHIFT_TX);
1088 gfar_write(baddr, isrg);
1089 baddr++;
1090 isrg = 0x0;
1091 }
1092 }
1093
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001094 /* Need to reverse the bit maps as bit_map's MSB is q0
Akinobu Mita984b3f52010-03-05 13:41:37 -08001095 * but, for_each_set_bit parses from right to left, which
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001096 * basically reverses the queue numbers */
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001097 for (i = 0; i< priv->num_grps; i++) {
1098 priv->gfargrp[i].tx_bit_map = reverse_bitmap(
1099 priv->gfargrp[i].tx_bit_map, MAX_TX_QS);
1100 priv->gfargrp[i].rx_bit_map = reverse_bitmap(
1101 priv->gfargrp[i].rx_bit_map, MAX_RX_QS);
1102 }
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001103
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001104 /* Calculate RSTAT, TSTAT, RQUEUE and TQUEUE values,
1105 * also assign queues to groups */
1106 for (grp_idx = 0; grp_idx < priv->num_grps; grp_idx++) {
1107 priv->gfargrp[grp_idx].num_rx_queues = 0x0;
Akinobu Mita984b3f52010-03-05 13:41:37 -08001108 for_each_set_bit(i, &priv->gfargrp[grp_idx].rx_bit_map,
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001109 priv->num_rx_queues) {
1110 priv->gfargrp[grp_idx].num_rx_queues++;
1111 priv->rx_queue[i]->grp = &priv->gfargrp[grp_idx];
1112 rstat = rstat | (RSTAT_CLEAR_RHALT >> i);
1113 rqueue = rqueue | ((RQUEUE_EN0 | RQUEUE_EX0) >> i);
1114 }
1115 priv->gfargrp[grp_idx].num_tx_queues = 0x0;
Akinobu Mita984b3f52010-03-05 13:41:37 -08001116 for_each_set_bit(i, &priv->gfargrp[grp_idx].tx_bit_map,
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001117 priv->num_tx_queues) {
1118 priv->gfargrp[grp_idx].num_tx_queues++;
1119 priv->tx_queue[i]->grp = &priv->gfargrp[grp_idx];
1120 tstat = tstat | (TSTAT_CLEAR_THALT >> i);
1121 tqueue = tqueue | (TQUEUE_EN0 >> i);
1122 }
1123 priv->gfargrp[grp_idx].rstat = rstat;
1124 priv->gfargrp[grp_idx].tstat = tstat;
1125 rstat = tstat =0;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001126 }
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001127
1128 gfar_write(&regs->rqueue, rqueue);
1129 gfar_write(&regs->tqueue, tqueue);
1130
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 priv->rx_buffer_size = DEFAULT_RX_BUFFER_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001133 /* Initializing some of the rx/tx queue level parameters */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001134 for (i = 0; i < priv->num_tx_queues; i++) {
1135 priv->tx_queue[i]->tx_ring_size = DEFAULT_TX_RING_SIZE;
1136 priv->tx_queue[i]->num_txbdfree = DEFAULT_TX_RING_SIZE;
1137 priv->tx_queue[i]->txcoalescing = DEFAULT_TX_COALESCE;
1138 priv->tx_queue[i]->txic = DEFAULT_TXIC;
1139 }
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001140
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001141 for (i = 0; i < priv->num_rx_queues; i++) {
1142 priv->rx_queue[i]->rx_ring_size = DEFAULT_RX_RING_SIZE;
1143 priv->rx_queue[i]->rxcoalescing = DEFAULT_RX_COALESCE;
1144 priv->rx_queue[i]->rxic = DEFAULT_RXIC;
1145 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
Sandeep Gopalpet1ccb8382009-12-16 01:14:58 +00001147 /* enable filer if using multiple RX queues*/
1148 if(priv->num_rx_queues > 1)
1149 priv->rx_filer_enable = 1;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001150 /* Enable most messages by default */
1151 priv->msg_enable = (NETIF_MSG_IFUP << 1 ) - 1;
1152
Trent Piephod3eab822008-10-02 11:12:24 +00001153 /* Carrier starts down, phylib will bring it up */
1154 netif_carrier_off(dev);
1155
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 err = register_netdev(dev);
1157
1158 if (err) {
1159 printk(KERN_ERR "%s: Cannot register net device, aborting.\n",
1160 dev->name);
1161 goto register_fail;
1162 }
1163
Anton Vorontsov2884e5c2009-02-01 00:52:34 -08001164 device_init_wakeup(&dev->dev,
1165 priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
1166
Dai Harukic50a5d92008-12-17 16:51:32 -08001167 /* fill out IRQ number and name fields */
1168 len_devname = strlen(dev->name);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001169 for (i = 0; i < priv->num_grps; i++) {
1170 strncpy(&priv->gfargrp[i].int_name_tx[0], dev->name,
1171 len_devname);
1172 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
1173 strncpy(&priv->gfargrp[i].int_name_tx[len_devname],
1174 "_g", sizeof("_g"));
1175 priv->gfargrp[i].int_name_tx[
1176 strlen(priv->gfargrp[i].int_name_tx)] = i+48;
1177 strncpy(&priv->gfargrp[i].int_name_tx[strlen(
1178 priv->gfargrp[i].int_name_tx)],
1179 "_tx", sizeof("_tx") + 1);
Dai Harukic50a5d92008-12-17 16:51:32 -08001180
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001181 strncpy(&priv->gfargrp[i].int_name_rx[0], dev->name,
1182 len_devname);
1183 strncpy(&priv->gfargrp[i].int_name_rx[len_devname],
1184 "_g", sizeof("_g"));
1185 priv->gfargrp[i].int_name_rx[
1186 strlen(priv->gfargrp[i].int_name_rx)] = i+48;
1187 strncpy(&priv->gfargrp[i].int_name_rx[strlen(
1188 priv->gfargrp[i].int_name_rx)],
1189 "_rx", sizeof("_rx") + 1);
Dai Harukic50a5d92008-12-17 16:51:32 -08001190
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001191 strncpy(&priv->gfargrp[i].int_name_er[0], dev->name,
1192 len_devname);
1193 strncpy(&priv->gfargrp[i].int_name_er[len_devname],
1194 "_g", sizeof("_g"));
1195 priv->gfargrp[i].int_name_er[strlen(
1196 priv->gfargrp[i].int_name_er)] = i+48;
1197 strncpy(&priv->gfargrp[i].int_name_er[strlen(\
1198 priv->gfargrp[i].int_name_er)],
1199 "_er", sizeof("_er") + 1);
1200 } else
1201 priv->gfargrp[i].int_name_tx[len_devname] = '\0';
1202 }
Dai Harukic50a5d92008-12-17 16:51:32 -08001203
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +00001204 /* Initialize the filer table */
1205 gfar_init_filer_table(priv);
1206
Andy Fleming7f7f5312005-11-11 12:38:59 -06001207 /* Create all the sysfs files */
1208 gfar_init_sysfs(dev);
1209
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 /* Print out the device info */
Johannes Berge1749612008-10-27 15:59:26 -07001211 printk(KERN_INFO DEVICE_NAME "%pM\n", dev->name, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
1213 /* Even more device info helps when determining which kernel */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001214 /* provided which set of benchmarks. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 printk(KERN_INFO "%s: Running with NAPI enabled\n", dev->name);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001216 for (i = 0; i < priv->num_rx_queues; i++)
Kim Phillipsddc01b32010-03-30 11:54:22 +00001217 printk(KERN_INFO "%s: RX BD ring size for Q[%d]: %d\n",
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001218 dev->name, i, priv->rx_queue[i]->rx_ring_size);
1219 for(i = 0; i < priv->num_tx_queues; i++)
Kim Phillipsddc01b32010-03-30 11:54:22 +00001220 printk(KERN_INFO "%s: TX BD ring size for Q[%d]: %d\n",
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001221 dev->name, i, priv->tx_queue[i]->tx_ring_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
1223 return 0;
1224
1225register_fail:
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001226 unmap_group_regs(priv);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001227 free_tx_pointers(priv);
1228 free_rx_pointers(priv);
Grant Likelyfe192a42009-04-25 12:53:12 +00001229 if (priv->phy_node)
1230 of_node_put(priv->phy_node);
1231 if (priv->tbi_node)
1232 of_node_put(priv->tbi_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 free_netdev(dev);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001234 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235}
1236
Grant Likely2dc11582010-08-06 09:25:50 -06001237static int gfar_remove(struct platform_device *ofdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238{
Andy Flemingb31a1d82008-12-16 15:29:15 -08001239 struct gfar_private *priv = dev_get_drvdata(&ofdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
Grant Likelyfe192a42009-04-25 12:53:12 +00001241 if (priv->phy_node)
1242 of_node_put(priv->phy_node);
1243 if (priv->tbi_node)
1244 of_node_put(priv->tbi_node);
1245
Andy Flemingb31a1d82008-12-16 15:29:15 -08001246 dev_set_drvdata(&ofdev->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
David S. Millerd9d8e042009-09-06 01:41:02 -07001248 unregister_netdev(priv->ndev);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001249 unmap_group_regs(priv);
Kumar Gala48268572009-03-18 23:28:22 -07001250 free_netdev(priv->ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
1252 return 0;
1253}
1254
Scott Woodd87eb122008-07-11 18:04:45 -05001255#ifdef CONFIG_PM
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001256
1257static int gfar_suspend(struct device *dev)
Scott Woodd87eb122008-07-11 18:04:45 -05001258{
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001259 struct gfar_private *priv = dev_get_drvdata(dev);
1260 struct net_device *ndev = priv->ndev;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001261 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Scott Woodd87eb122008-07-11 18:04:45 -05001262 unsigned long flags;
1263 u32 tempval;
1264
1265 int magic_packet = priv->wol_en &&
Andy Flemingb31a1d82008-12-16 15:29:15 -08001266 (priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
Scott Woodd87eb122008-07-11 18:04:45 -05001267
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001268 netif_device_detach(ndev);
Scott Woodd87eb122008-07-11 18:04:45 -05001269
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001270 if (netif_running(ndev)) {
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001271
1272 local_irq_save(flags);
1273 lock_tx_qs(priv);
1274 lock_rx_qs(priv);
Scott Woodd87eb122008-07-11 18:04:45 -05001275
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001276 gfar_halt_nodisable(ndev);
Scott Woodd87eb122008-07-11 18:04:45 -05001277
1278 /* Disable Tx, and Rx if wake-on-LAN is disabled. */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001279 tempval = gfar_read(&regs->maccfg1);
Scott Woodd87eb122008-07-11 18:04:45 -05001280
1281 tempval &= ~MACCFG1_TX_EN;
1282
1283 if (!magic_packet)
1284 tempval &= ~MACCFG1_RX_EN;
1285
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001286 gfar_write(&regs->maccfg1, tempval);
Scott Woodd87eb122008-07-11 18:04:45 -05001287
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001288 unlock_rx_qs(priv);
1289 unlock_tx_qs(priv);
1290 local_irq_restore(flags);
Scott Woodd87eb122008-07-11 18:04:45 -05001291
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001292 disable_napi(priv);
Scott Woodd87eb122008-07-11 18:04:45 -05001293
1294 if (magic_packet) {
1295 /* Enable interrupt on Magic Packet */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001296 gfar_write(&regs->imask, IMASK_MAG);
Scott Woodd87eb122008-07-11 18:04:45 -05001297
1298 /* Enable Magic Packet mode */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001299 tempval = gfar_read(&regs->maccfg2);
Scott Woodd87eb122008-07-11 18:04:45 -05001300 tempval |= MACCFG2_MPEN;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001301 gfar_write(&regs->maccfg2, tempval);
Scott Woodd87eb122008-07-11 18:04:45 -05001302 } else {
1303 phy_stop(priv->phydev);
1304 }
1305 }
1306
1307 return 0;
1308}
1309
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001310static int gfar_resume(struct device *dev)
Scott Woodd87eb122008-07-11 18:04:45 -05001311{
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001312 struct gfar_private *priv = dev_get_drvdata(dev);
1313 struct net_device *ndev = priv->ndev;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001314 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Scott Woodd87eb122008-07-11 18:04:45 -05001315 unsigned long flags;
1316 u32 tempval;
1317 int magic_packet = priv->wol_en &&
Andy Flemingb31a1d82008-12-16 15:29:15 -08001318 (priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
Scott Woodd87eb122008-07-11 18:04:45 -05001319
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001320 if (!netif_running(ndev)) {
1321 netif_device_attach(ndev);
Scott Woodd87eb122008-07-11 18:04:45 -05001322 return 0;
1323 }
1324
1325 if (!magic_packet && priv->phydev)
1326 phy_start(priv->phydev);
1327
1328 /* Disable Magic Packet mode, in case something
1329 * else woke us up.
1330 */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001331 local_irq_save(flags);
1332 lock_tx_qs(priv);
1333 lock_rx_qs(priv);
Scott Woodd87eb122008-07-11 18:04:45 -05001334
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001335 tempval = gfar_read(&regs->maccfg2);
Scott Woodd87eb122008-07-11 18:04:45 -05001336 tempval &= ~MACCFG2_MPEN;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001337 gfar_write(&regs->maccfg2, tempval);
Scott Woodd87eb122008-07-11 18:04:45 -05001338
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001339 gfar_start(ndev);
Scott Woodd87eb122008-07-11 18:04:45 -05001340
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001341 unlock_rx_qs(priv);
1342 unlock_tx_qs(priv);
1343 local_irq_restore(flags);
Scott Woodd87eb122008-07-11 18:04:45 -05001344
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001345 netif_device_attach(ndev);
Scott Woodd87eb122008-07-11 18:04:45 -05001346
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001347 enable_napi(priv);
Scott Woodd87eb122008-07-11 18:04:45 -05001348
1349 return 0;
1350}
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001351
1352static int gfar_restore(struct device *dev)
1353{
1354 struct gfar_private *priv = dev_get_drvdata(dev);
1355 struct net_device *ndev = priv->ndev;
1356
1357 if (!netif_running(ndev))
1358 return 0;
1359
1360 gfar_init_bds(ndev);
1361 init_registers(ndev);
1362 gfar_set_mac_address(ndev);
1363 gfar_init_mac(ndev);
1364 gfar_start(ndev);
1365
1366 priv->oldlink = 0;
1367 priv->oldspeed = 0;
1368 priv->oldduplex = -1;
1369
1370 if (priv->phydev)
1371 phy_start(priv->phydev);
1372
1373 netif_device_attach(ndev);
Anton Vorontsov5ea681d2009-11-10 14:11:05 +00001374 enable_napi(priv);
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001375
1376 return 0;
1377}
1378
1379static struct dev_pm_ops gfar_pm_ops = {
1380 .suspend = gfar_suspend,
1381 .resume = gfar_resume,
1382 .freeze = gfar_suspend,
1383 .thaw = gfar_resume,
1384 .restore = gfar_restore,
1385};
1386
1387#define GFAR_PM_OPS (&gfar_pm_ops)
1388
Scott Woodd87eb122008-07-11 18:04:45 -05001389#else
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001390
1391#define GFAR_PM_OPS NULL
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001392
Scott Woodd87eb122008-07-11 18:04:45 -05001393#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001395/* Reads the controller's registers to determine what interface
1396 * connects it to the PHY.
1397 */
1398static phy_interface_t gfar_get_interface(struct net_device *dev)
1399{
1400 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001401 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001402 u32 ecntrl;
1403
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001404 ecntrl = gfar_read(&regs->ecntrl);
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001405
1406 if (ecntrl & ECNTRL_SGMII_MODE)
1407 return PHY_INTERFACE_MODE_SGMII;
1408
1409 if (ecntrl & ECNTRL_TBI_MODE) {
1410 if (ecntrl & ECNTRL_REDUCED_MODE)
1411 return PHY_INTERFACE_MODE_RTBI;
1412 else
1413 return PHY_INTERFACE_MODE_TBI;
1414 }
1415
1416 if (ecntrl & ECNTRL_REDUCED_MODE) {
1417 if (ecntrl & ECNTRL_REDUCED_MII_MODE)
1418 return PHY_INTERFACE_MODE_RMII;
Andy Fleming7132ab72007-07-11 11:43:07 -05001419 else {
Andy Flemingb31a1d82008-12-16 15:29:15 -08001420 phy_interface_t interface = priv->interface;
Andy Fleming7132ab72007-07-11 11:43:07 -05001421
1422 /*
1423 * This isn't autodetected right now, so it must
1424 * be set by the device tree or platform code.
1425 */
1426 if (interface == PHY_INTERFACE_MODE_RGMII_ID)
1427 return PHY_INTERFACE_MODE_RGMII_ID;
1428
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001429 return PHY_INTERFACE_MODE_RGMII;
Andy Fleming7132ab72007-07-11 11:43:07 -05001430 }
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001431 }
1432
Andy Flemingb31a1d82008-12-16 15:29:15 -08001433 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT)
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001434 return PHY_INTERFACE_MODE_GMII;
1435
1436 return PHY_INTERFACE_MODE_MII;
1437}
1438
1439
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001440/* Initializes driver's PHY state, and attaches to the PHY.
1441 * Returns 0 on success.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 */
1443static int init_phy(struct net_device *dev)
1444{
1445 struct gfar_private *priv = netdev_priv(dev);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001446 uint gigabit_support =
Andy Flemingb31a1d82008-12-16 15:29:15 -08001447 priv->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT ?
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001448 SUPPORTED_1000baseT_Full : 0;
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001449 phy_interface_t interface;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
1451 priv->oldlink = 0;
1452 priv->oldspeed = 0;
1453 priv->oldduplex = -1;
1454
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001455 interface = gfar_get_interface(dev);
1456
Anton Vorontsov1db780f2009-07-16 21:31:42 +00001457 priv->phydev = of_phy_connect(dev, priv->phy_node, &adjust_link, 0,
1458 interface);
1459 if (!priv->phydev)
1460 priv->phydev = of_phy_connect_fixed_link(dev, &adjust_link,
1461 interface);
1462 if (!priv->phydev) {
1463 dev_err(&dev->dev, "could not attach to PHY\n");
1464 return -ENODEV;
Grant Likelyfe192a42009-04-25 12:53:12 +00001465 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466
Kapil Junejad3c12872007-05-11 18:25:11 -05001467 if (interface == PHY_INTERFACE_MODE_SGMII)
1468 gfar_configure_serdes(dev);
1469
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001470 /* Remove any features not supported by the controller */
Grant Likelyfe192a42009-04-25 12:53:12 +00001471 priv->phydev->supported &= (GFAR_SUPPORTED | gigabit_support);
1472 priv->phydev->advertising = priv->phydev->supported;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
1474 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475}
1476
Paul Gortmakerd0313582008-04-17 00:08:10 -04001477/*
1478 * Initialize TBI PHY interface for communicating with the
1479 * SERDES lynx PHY on the chip. We communicate with this PHY
1480 * through the MDIO bus on each controller, treating it as a
1481 * "normal" PHY at the address found in the TBIPA register. We assume
1482 * that the TBIPA register is valid. Either the MDIO bus code will set
1483 * it to a value that doesn't conflict with other PHYs on the bus, or the
1484 * value doesn't matter, as there are no other PHYs on the bus.
1485 */
Kapil Junejad3c12872007-05-11 18:25:11 -05001486static void gfar_configure_serdes(struct net_device *dev)
1487{
1488 struct gfar_private *priv = netdev_priv(dev);
Grant Likelyfe192a42009-04-25 12:53:12 +00001489 struct phy_device *tbiphy;
Trent Piephoc1324192008-10-30 18:17:06 -07001490
Grant Likelyfe192a42009-04-25 12:53:12 +00001491 if (!priv->tbi_node) {
1492 dev_warn(&dev->dev, "error: SGMII mode requires that the "
1493 "device tree specify a tbi-handle\n");
1494 return;
1495 }
1496
1497 tbiphy = of_phy_find_device(priv->tbi_node);
1498 if (!tbiphy) {
1499 dev_err(&dev->dev, "error: Could not get TBI device\n");
Andy Flemingb31a1d82008-12-16 15:29:15 -08001500 return;
1501 }
Kapil Junejad3c12872007-05-11 18:25:11 -05001502
Andy Flemingb31a1d82008-12-16 15:29:15 -08001503 /*
1504 * If the link is already up, we must already be ok, and don't need to
Trent Piephobdb59f92008-10-30 18:17:07 -07001505 * configure and reset the TBI<->SerDes link. Maybe U-Boot configured
1506 * everything for us? Resetting it takes the link down and requires
1507 * several seconds for it to come back.
1508 */
Grant Likelyfe192a42009-04-25 12:53:12 +00001509 if (phy_read(tbiphy, MII_BMSR) & BMSR_LSTATUS)
Andy Flemingb31a1d82008-12-16 15:29:15 -08001510 return;
Kapil Junejad3c12872007-05-11 18:25:11 -05001511
Paul Gortmakerd0313582008-04-17 00:08:10 -04001512 /* Single clk mode, mii mode off(for serdes communication) */
Grant Likelyfe192a42009-04-25 12:53:12 +00001513 phy_write(tbiphy, MII_TBICON, TBICON_CLK_SELECT);
Kapil Junejad3c12872007-05-11 18:25:11 -05001514
Grant Likelyfe192a42009-04-25 12:53:12 +00001515 phy_write(tbiphy, MII_ADVERTISE,
Kapil Junejad3c12872007-05-11 18:25:11 -05001516 ADVERTISE_1000XFULL | ADVERTISE_1000XPAUSE |
1517 ADVERTISE_1000XPSE_ASYM);
1518
Grant Likelyfe192a42009-04-25 12:53:12 +00001519 phy_write(tbiphy, MII_BMCR, BMCR_ANENABLE |
Kapil Junejad3c12872007-05-11 18:25:11 -05001520 BMCR_ANRESTART | BMCR_FULLDPLX | BMCR_SPEED1000);
1521}
1522
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523static void init_registers(struct net_device *dev)
1524{
1525 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001526 struct gfar __iomem *regs = NULL;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001527 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001529 for (i = 0; i < priv->num_grps; i++) {
1530 regs = priv->gfargrp[i].regs;
1531 /* Clear IEVENT */
1532 gfar_write(&regs->ievent, IEVENT_INIT_CLEAR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001534 /* Initialize IMASK */
1535 gfar_write(&regs->imask, IMASK_INIT_CLEAR);
1536 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001538 regs = priv->gfargrp[0].regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 /* Init hash registers to zero */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001540 gfar_write(&regs->igaddr0, 0);
1541 gfar_write(&regs->igaddr1, 0);
1542 gfar_write(&regs->igaddr2, 0);
1543 gfar_write(&regs->igaddr3, 0);
1544 gfar_write(&regs->igaddr4, 0);
1545 gfar_write(&regs->igaddr5, 0);
1546 gfar_write(&regs->igaddr6, 0);
1547 gfar_write(&regs->igaddr7, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001549 gfar_write(&regs->gaddr0, 0);
1550 gfar_write(&regs->gaddr1, 0);
1551 gfar_write(&regs->gaddr2, 0);
1552 gfar_write(&regs->gaddr3, 0);
1553 gfar_write(&regs->gaddr4, 0);
1554 gfar_write(&regs->gaddr5, 0);
1555 gfar_write(&regs->gaddr6, 0);
1556 gfar_write(&regs->gaddr7, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 /* Zero out the rmon mib registers if it has them */
Andy Flemingb31a1d82008-12-16 15:29:15 -08001559 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_RMON) {
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001560 memset_io(&(regs->rmon), 0, sizeof (struct rmon_mib));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
1562 /* Mask off the CAM interrupts */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001563 gfar_write(&regs->rmon.cam1, 0xffffffff);
1564 gfar_write(&regs->rmon.cam2, 0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 }
1566
1567 /* Initialize the max receive buffer length */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001568 gfar_write(&regs->mrblr, priv->rx_buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 /* Initialize the Minimum Frame Length Register */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001571 gfar_write(&regs->minflr, MINFLR_INIT_SETTINGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572}
1573
Anton Vorontsov511d9342010-06-30 06:39:15 +00001574static int __gfar_is_rx_idle(struct gfar_private *priv)
1575{
1576 u32 res;
1577
1578 /*
1579 * Normaly TSEC should not hang on GRS commands, so we should
1580 * actually wait for IEVENT_GRSC flag.
1581 */
1582 if (likely(!gfar_has_errata(priv, GFAR_ERRATA_A002)))
1583 return 0;
1584
1585 /*
1586 * Read the eTSEC register at offset 0xD1C. If bits 7-14 are
1587 * the same as bits 23-30, the eTSEC Rx is assumed to be idle
1588 * and the Rx can be safely reset.
1589 */
1590 res = gfar_read((void __iomem *)priv->gfargrp[0].regs + 0xd1c);
1591 res &= 0x7f807f80;
1592 if ((res & 0xffff) == (res >> 16))
1593 return 1;
1594
1595 return 0;
1596}
Kumar Gala0bbaf062005-06-20 10:54:21 -05001597
1598/* Halt the receive and transmit queues */
Scott Woodd87eb122008-07-11 18:04:45 -05001599static void gfar_halt_nodisable(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600{
1601 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001602 struct gfar __iomem *regs = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 u32 tempval;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001604 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001606 for (i = 0; i < priv->num_grps; i++) {
1607 regs = priv->gfargrp[i].regs;
1608 /* Mask all interrupts */
1609 gfar_write(&regs->imask, IMASK_INIT_CLEAR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001611 /* Clear all interrupts */
1612 gfar_write(&regs->ievent, IEVENT_INIT_CLEAR);
1613 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001615 regs = priv->gfargrp[0].regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 /* Stop the DMA, and wait for it to stop */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001617 tempval = gfar_read(&regs->dmactrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 if ((tempval & (DMACTRL_GRS | DMACTRL_GTS))
1619 != (DMACTRL_GRS | DMACTRL_GTS)) {
Anton Vorontsov511d9342010-06-30 06:39:15 +00001620 int ret;
1621
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 tempval |= (DMACTRL_GRS | DMACTRL_GTS);
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001623 gfar_write(&regs->dmactrl, tempval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
Anton Vorontsov511d9342010-06-30 06:39:15 +00001625 do {
1626 ret = spin_event_timeout(((gfar_read(&regs->ievent) &
1627 (IEVENT_GRSC | IEVENT_GTSC)) ==
1628 (IEVENT_GRSC | IEVENT_GTSC)), 1000000, 0);
1629 if (!ret && !(gfar_read(&regs->ievent) & IEVENT_GRSC))
1630 ret = __gfar_is_rx_idle(priv);
1631 } while (!ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 }
Scott Woodd87eb122008-07-11 18:04:45 -05001633}
Scott Woodd87eb122008-07-11 18:04:45 -05001634
1635/* Halt the receive and transmit queues */
1636void gfar_halt(struct net_device *dev)
1637{
1638 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001639 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Scott Woodd87eb122008-07-11 18:04:45 -05001640 u32 tempval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
Scott Wood2a54adc2008-08-12 15:10:46 -05001642 gfar_halt_nodisable(dev);
1643
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 /* Disable Rx and Tx */
1645 tempval = gfar_read(&regs->maccfg1);
1646 tempval &= ~(MACCFG1_RX_EN | MACCFG1_TX_EN);
1647 gfar_write(&regs->maccfg1, tempval);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001648}
1649
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001650static void free_grp_irqs(struct gfar_priv_grp *grp)
1651{
1652 free_irq(grp->interruptError, grp);
1653 free_irq(grp->interruptTransmit, grp);
1654 free_irq(grp->interruptReceive, grp);
1655}
1656
Kumar Gala0bbaf062005-06-20 10:54:21 -05001657void stop_gfar(struct net_device *dev)
1658{
1659 struct gfar_private *priv = netdev_priv(dev);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001660 unsigned long flags;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001661 int i;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001662
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001663 phy_stop(priv->phydev);
1664
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001665
Kumar Gala0bbaf062005-06-20 10:54:21 -05001666 /* Lock it down */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001667 local_irq_save(flags);
1668 lock_tx_qs(priv);
1669 lock_rx_qs(priv);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001670
Kumar Gala0bbaf062005-06-20 10:54:21 -05001671 gfar_halt(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001673 unlock_rx_qs(priv);
1674 unlock_tx_qs(priv);
1675 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
1677 /* Free the IRQs */
Andy Flemingb31a1d82008-12-16 15:29:15 -08001678 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001679 for (i = 0; i < priv->num_grps; i++)
1680 free_grp_irqs(&priv->gfargrp[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 } else {
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001682 for (i = 0; i < priv->num_grps; i++)
1683 free_irq(priv->gfargrp[i].interruptTransmit,
1684 &priv->gfargrp[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 }
1686
1687 free_skb_resources(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688}
1689
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001690static void free_skb_tx_queue(struct gfar_priv_tx_q *tx_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 struct txbd8 *txbdp;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001693 struct gfar_private *priv = netdev_priv(tx_queue->dev);
Dai Haruki4669bc92008-12-17 16:51:04 -08001694 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001696 txbdp = tx_queue->tx_bd_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001698 for (i = 0; i < tx_queue->tx_ring_size; i++) {
1699 if (!tx_queue->tx_skbuff[i])
Dai Haruki4669bc92008-12-17 16:51:04 -08001700 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701
Kumar Gala48268572009-03-18 23:28:22 -07001702 dma_unmap_single(&priv->ofdev->dev, txbdp->bufPtr,
Dai Haruki4669bc92008-12-17 16:51:04 -08001703 txbdp->length, DMA_TO_DEVICE);
1704 txbdp->lstatus = 0;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001705 for (j = 0; j < skb_shinfo(tx_queue->tx_skbuff[i])->nr_frags;
1706 j++) {
Dai Haruki4669bc92008-12-17 16:51:04 -08001707 txbdp++;
Kumar Gala48268572009-03-18 23:28:22 -07001708 dma_unmap_page(&priv->ofdev->dev, txbdp->bufPtr,
Dai Haruki4669bc92008-12-17 16:51:04 -08001709 txbdp->length, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 }
Andy Flemingad5da7a2008-05-07 13:20:55 -05001711 txbdp++;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001712 dev_kfree_skb_any(tx_queue->tx_skbuff[i]);
1713 tx_queue->tx_skbuff[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 }
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001715 kfree(tx_queue->tx_skbuff);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001716}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001718static void free_skb_rx_queue(struct gfar_priv_rx_q *rx_queue)
1719{
1720 struct rxbd8 *rxbdp;
1721 struct gfar_private *priv = netdev_priv(rx_queue->dev);
1722 int i;
1723
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001724 rxbdp = rx_queue->rx_bd_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001726 for (i = 0; i < rx_queue->rx_ring_size; i++) {
1727 if (rx_queue->rx_skbuff[i]) {
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001728 dma_unmap_single(&priv->ofdev->dev,
1729 rxbdp->bufPtr, priv->rx_buffer_size,
Anton Vorontsove69edd22009-10-12 06:00:30 +00001730 DMA_FROM_DEVICE);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001731 dev_kfree_skb_any(rx_queue->rx_skbuff[i]);
1732 rx_queue->rx_skbuff[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 }
Anton Vorontsove69edd22009-10-12 06:00:30 +00001734 rxbdp->lstatus = 0;
1735 rxbdp->bufPtr = 0;
1736 rxbdp++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 }
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001738 kfree(rx_queue->rx_skbuff);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001739}
Anton Vorontsove69edd22009-10-12 06:00:30 +00001740
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001741/* If there are any tx skbs or rx skbs still around, free them.
1742 * Then free tx_skbuff and rx_skbuff */
1743static void free_skb_resources(struct gfar_private *priv)
1744{
1745 struct gfar_priv_tx_q *tx_queue = NULL;
1746 struct gfar_priv_rx_q *rx_queue = NULL;
1747 int i;
1748
1749 /* Go through all the buffer descriptors and free their data buffers */
1750 for (i = 0; i < priv->num_tx_queues; i++) {
1751 tx_queue = priv->tx_queue[i];
Andy Fleming7c0d10d2010-03-29 15:42:23 +00001752 if(tx_queue->tx_skbuff)
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001753 free_skb_tx_queue(tx_queue);
1754 }
1755
1756 for (i = 0; i < priv->num_rx_queues; i++) {
1757 rx_queue = priv->rx_queue[i];
Andy Fleming7c0d10d2010-03-29 15:42:23 +00001758 if(rx_queue->rx_skbuff)
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001759 free_skb_rx_queue(rx_queue);
1760 }
1761
1762 dma_free_coherent(&priv->ofdev->dev,
1763 sizeof(struct txbd8) * priv->total_tx_ring_size +
1764 sizeof(struct rxbd8) * priv->total_rx_ring_size,
1765 priv->tx_queue[0]->tx_bd_base,
1766 priv->tx_queue[0]->tx_bd_dma_base);
Sebastian Andrzej Siewior7df9c432010-05-04 22:30:47 +00001767 skb_queue_purge(&priv->rx_recycle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768}
1769
Kumar Gala0bbaf062005-06-20 10:54:21 -05001770void gfar_start(struct net_device *dev)
1771{
1772 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001773 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001774 u32 tempval;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001775 int i = 0;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001776
1777 /* Enable Rx and Tx in MACCFG1 */
1778 tempval = gfar_read(&regs->maccfg1);
1779 tempval |= (MACCFG1_RX_EN | MACCFG1_TX_EN);
1780 gfar_write(&regs->maccfg1, tempval);
1781
1782 /* Initialize DMACTRL to have WWR and WOP */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001783 tempval = gfar_read(&regs->dmactrl);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001784 tempval |= DMACTRL_INIT_SETTINGS;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001785 gfar_write(&regs->dmactrl, tempval);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001786
Kumar Gala0bbaf062005-06-20 10:54:21 -05001787 /* Make sure we aren't stopped */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001788 tempval = gfar_read(&regs->dmactrl);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001789 tempval &= ~(DMACTRL_GRS | DMACTRL_GTS);
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001790 gfar_write(&regs->dmactrl, tempval);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001791
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001792 for (i = 0; i < priv->num_grps; i++) {
1793 regs = priv->gfargrp[i].regs;
1794 /* Clear THLT/RHLT, so that the DMA starts polling now */
1795 gfar_write(&regs->tstat, priv->gfargrp[i].tstat);
1796 gfar_write(&regs->rstat, priv->gfargrp[i].rstat);
1797 /* Unmask the interrupts we look for */
1798 gfar_write(&regs->imask, IMASK_DEFAULT);
1799 }
Dai Haruki12dea572008-12-16 15:30:20 -08001800
Eric Dumazet1ae5dc32010-05-10 05:01:31 -07001801 dev->trans_start = jiffies; /* prevent tx timeout */
Kumar Gala0bbaf062005-06-20 10:54:21 -05001802}
1803
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001804void gfar_configure_coalescing(struct gfar_private *priv,
Anton Vorontsov18294ad2009-11-04 12:53:00 +00001805 unsigned long tx_mask, unsigned long rx_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806{
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001807 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Anton Vorontsov18294ad2009-11-04 12:53:00 +00001808 u32 __iomem *baddr;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001809 int i = 0;
1810
1811 /* Backward compatible case ---- even if we enable
1812 * multiple queues, there's only single reg to program
1813 */
1814 gfar_write(&regs->txic, 0);
1815 if(likely(priv->tx_queue[0]->txcoalescing))
1816 gfar_write(&regs->txic, priv->tx_queue[0]->txic);
1817
1818 gfar_write(&regs->rxic, 0);
1819 if(unlikely(priv->rx_queue[0]->rxcoalescing))
1820 gfar_write(&regs->rxic, priv->rx_queue[0]->rxic);
1821
1822 if (priv->mode == MQ_MG_MODE) {
1823 baddr = &regs->txic0;
Akinobu Mita984b3f52010-03-05 13:41:37 -08001824 for_each_set_bit(i, &tx_mask, priv->num_tx_queues) {
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001825 if (likely(priv->tx_queue[i]->txcoalescing)) {
1826 gfar_write(baddr + i, 0);
1827 gfar_write(baddr + i, priv->tx_queue[i]->txic);
1828 }
1829 }
1830
1831 baddr = &regs->rxic0;
Akinobu Mita984b3f52010-03-05 13:41:37 -08001832 for_each_set_bit(i, &rx_mask, priv->num_rx_queues) {
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001833 if (likely(priv->rx_queue[i]->rxcoalescing)) {
1834 gfar_write(baddr + i, 0);
1835 gfar_write(baddr + i, priv->rx_queue[i]->rxic);
1836 }
1837 }
1838 }
1839}
1840
1841static int register_grp_irqs(struct gfar_priv_grp *grp)
1842{
1843 struct gfar_private *priv = grp->priv;
1844 struct net_device *dev = priv->ndev;
Anton Vorontsovccc05c62009-10-12 06:00:26 +00001845 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 /* If the device has multiple interrupts, register for
1848 * them. Otherwise, only register for the one */
Andy Flemingb31a1d82008-12-16 15:29:15 -08001849 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001850 /* Install our interrupt handlers for Error,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 * Transmit, and Receive */
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001852 if ((err = request_irq(grp->interruptError, gfar_error, 0,
1853 grp->int_name_er,grp)) < 0) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001854 if (netif_msg_intr(priv))
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001855 printk(KERN_ERR "%s: Can't get IRQ %d\n",
1856 dev->name, grp->interruptError);
1857
Julia Lawall2145f1a2010-08-05 10:26:20 +00001858 goto err_irq_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 }
1860
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001861 if ((err = request_irq(grp->interruptTransmit, gfar_transmit,
1862 0, grp->int_name_tx, grp)) < 0) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001863 if (netif_msg_intr(priv))
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001864 printk(KERN_ERR "%s: Can't get IRQ %d\n",
1865 dev->name, grp->interruptTransmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 goto tx_irq_fail;
1867 }
1868
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001869 if ((err = request_irq(grp->interruptReceive, gfar_receive, 0,
1870 grp->int_name_rx, grp)) < 0) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001871 if (netif_msg_intr(priv))
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001872 printk(KERN_ERR "%s: Can't get IRQ %d\n",
1873 dev->name, grp->interruptReceive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 goto rx_irq_fail;
1875 }
1876 } else {
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001877 if ((err = request_irq(grp->interruptTransmit, gfar_interrupt, 0,
1878 grp->int_name_tx, grp)) < 0) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001879 if (netif_msg_intr(priv))
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001880 printk(KERN_ERR "%s: Can't get IRQ %d\n",
1881 dev->name, grp->interruptTransmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 goto err_irq_fail;
1883 }
1884 }
1885
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001886 return 0;
1887
1888rx_irq_fail:
1889 free_irq(grp->interruptTransmit, grp);
1890tx_irq_fail:
1891 free_irq(grp->interruptError, grp);
1892err_irq_fail:
1893 return err;
1894
1895}
1896
1897/* Bring the controller up and running */
1898int startup_gfar(struct net_device *ndev)
1899{
1900 struct gfar_private *priv = netdev_priv(ndev);
1901 struct gfar __iomem *regs = NULL;
1902 int err, i, j;
1903
1904 for (i = 0; i < priv->num_grps; i++) {
1905 regs= priv->gfargrp[i].regs;
1906 gfar_write(&regs->imask, IMASK_INIT_CLEAR);
1907 }
1908
1909 regs= priv->gfargrp[0].regs;
1910 err = gfar_alloc_skb_resources(ndev);
1911 if (err)
1912 return err;
1913
1914 gfar_init_mac(ndev);
1915
1916 for (i = 0; i < priv->num_grps; i++) {
1917 err = register_grp_irqs(&priv->gfargrp[i]);
1918 if (err) {
1919 for (j = 0; j < i; j++)
1920 free_grp_irqs(&priv->gfargrp[j]);
Anton Vorontsovff760152011-01-18 02:36:02 +00001921 goto irq_fail;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001922 }
1923 }
1924
Andy Fleming7f7f5312005-11-11 12:38:59 -06001925 /* Start the controller */
Anton Vorontsovccc05c62009-10-12 06:00:26 +00001926 gfar_start(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927
Anton Vorontsov826aa4a2009-10-12 06:00:34 +00001928 phy_start(priv->phydev);
1929
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001930 gfar_configure_coalescing(priv, 0xFF, 0xFF);
1931
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 return 0;
1933
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001934irq_fail:
Anton Vorontsove69edd22009-10-12 06:00:30 +00001935 free_skb_resources(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 return err;
1937}
1938
1939/* Called when something needs to use the ethernet device */
1940/* Returns 0 for success. */
1941static int gfar_enet_open(struct net_device *dev)
1942{
Li Yang94e8cc32007-10-12 21:53:51 +08001943 struct gfar_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 int err;
1945
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001946 enable_napi(priv);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001947
Andy Fleming0fd56bb2009-02-04 16:43:16 -08001948 skb_queue_head_init(&priv->rx_recycle);
1949
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 /* Initialize a bunch of registers */
1951 init_registers(dev);
1952
1953 gfar_set_mac_address(dev);
1954
1955 err = init_phy(dev);
1956
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001957 if (err) {
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001958 disable_napi(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 return err;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001960 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961
1962 err = startup_gfar(dev);
Anton Vorontsovdb0e8e32007-10-17 23:57:46 +04001963 if (err) {
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001964 disable_napi(priv);
Anton Vorontsovdb0e8e32007-10-17 23:57:46 +04001965 return err;
1966 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001968 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969
Anton Vorontsov2884e5c2009-02-01 00:52:34 -08001970 device_set_wakeup_enable(&dev->dev, priv->wol_en);
1971
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 return err;
1973}
1974
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07001975static inline struct txfcb *gfar_add_fcb(struct sk_buff *skb)
Kumar Gala0bbaf062005-06-20 10:54:21 -05001976{
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07001977 struct txfcb *fcb = (struct txfcb *)skb_push(skb, GMAC_FCB_LEN);
Kumar Gala6c31d552009-04-28 08:04:10 -07001978
1979 memset(fcb, 0, GMAC_FCB_LEN);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001980
Kumar Gala0bbaf062005-06-20 10:54:21 -05001981 return fcb;
1982}
1983
1984static inline void gfar_tx_checksum(struct sk_buff *skb, struct txfcb *fcb)
1985{
Andy Fleming7f7f5312005-11-11 12:38:59 -06001986 u8 flags = 0;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001987
1988 /* If we're here, it's a IP packet with a TCP or UDP
1989 * payload. We set it to checksum, using a pseudo-header
1990 * we provide
1991 */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001992 flags = TXFCB_DEFAULT;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001993
Andy Fleming7f7f5312005-11-11 12:38:59 -06001994 /* Tell the controller what the protocol is */
1995 /* And provide the already calculated phcs */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001996 if (ip_hdr(skb)->protocol == IPPROTO_UDP) {
Andy Fleming7f7f5312005-11-11 12:38:59 -06001997 flags |= TXFCB_UDP;
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -03001998 fcb->phcs = udp_hdr(skb)->check;
Andy Fleming7f7f5312005-11-11 12:38:59 -06001999 } else
Kumar Gala8da32de2007-06-29 00:12:04 -05002000 fcb->phcs = tcp_hdr(skb)->check;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002001
2002 /* l3os is the distance between the start of the
2003 * frame (skb->data) and the start of the IP hdr.
2004 * l4os is the distance between the start of the
2005 * l3 hdr and the l4 hdr */
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03002006 fcb->l3os = (u16)(skb_network_offset(skb) - GMAC_FCB_LEN);
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -03002007 fcb->l4os = skb_network_header_len(skb);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002008
Andy Fleming7f7f5312005-11-11 12:38:59 -06002009 fcb->flags = flags;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002010}
2011
Andy Fleming7f7f5312005-11-11 12:38:59 -06002012void inline gfar_tx_vlan(struct sk_buff *skb, struct txfcb *fcb)
Kumar Gala0bbaf062005-06-20 10:54:21 -05002013{
Andy Fleming7f7f5312005-11-11 12:38:59 -06002014 fcb->flags |= TXFCB_VLN;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002015 fcb->vlctl = vlan_tx_tag_get(skb);
2016}
2017
Dai Haruki4669bc92008-12-17 16:51:04 -08002018static inline struct txbd8 *skip_txbd(struct txbd8 *bdp, int stride,
2019 struct txbd8 *base, int ring_size)
2020{
2021 struct txbd8 *new_bd = bdp + stride;
2022
2023 return (new_bd >= (base + ring_size)) ? (new_bd - ring_size) : new_bd;
2024}
2025
2026static inline struct txbd8 *next_txbd(struct txbd8 *bdp, struct txbd8 *base,
2027 int ring_size)
2028{
2029 return skip_txbd(bdp, 1, base, ring_size);
2030}
2031
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032/* This is called by the kernel when a frame is ready for transmission. */
2033/* It is pointed to by the dev->hard_start_xmit function pointer */
2034static int gfar_start_xmit(struct sk_buff *skb, struct net_device *dev)
2035{
2036 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002037 struct gfar_priv_tx_q *tx_queue = NULL;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002038 struct netdev_queue *txq;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002039 struct gfar __iomem *regs = NULL;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002040 struct txfcb *fcb = NULL;
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002041 struct txbd8 *txbdp, *txbdp_start, *base, *txbdp_tstamp = NULL;
Dai Haruki5a5efed2008-12-16 15:34:50 -08002042 u32 lstatus;
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002043 int i, rq = 0, do_tstamp = 0;
Dai Haruki4669bc92008-12-17 16:51:04 -08002044 u32 bufaddr;
Andy Flemingfef61082006-04-20 16:44:29 -05002045 unsigned long flags;
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002046 unsigned int nr_frags, nr_txbds, length;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002047
Anton Vorontsovdeb90ea2010-06-30 06:39:13 +00002048 /*
2049 * TOE=1 frames larger than 2500 bytes may see excess delays
2050 * before start of transmission.
2051 */
2052 if (unlikely(gfar_has_errata(priv, GFAR_ERRATA_76) &&
2053 skb->ip_summed == CHECKSUM_PARTIAL &&
2054 skb->len > 2500)) {
2055 int ret;
2056
2057 ret = skb_checksum_help(skb);
2058 if (ret)
2059 return ret;
2060 }
2061
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002062 rq = skb->queue_mapping;
2063 tx_queue = priv->tx_queue[rq];
2064 txq = netdev_get_tx_queue(dev, rq);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002065 base = tx_queue->tx_bd_base;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002066 regs = tx_queue->grp->regs;
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002067
2068 /* check if time stamp should be generated */
Oliver Hartkopp2244d072010-08-17 08:59:14 +00002069 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
2070 priv->hwts_tx_en))
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002071 do_tstamp = 1;
Dai Haruki4669bc92008-12-17 16:51:04 -08002072
Li Yang5b28bea2009-03-27 15:54:30 -07002073 /* make space for additional header when fcb is needed */
2074 if (((skb->ip_summed == CHECKSUM_PARTIAL) ||
Jesse Grosseab6d182010-10-20 13:56:03 +00002075 vlan_tx_tag_present(skb) ||
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002076 unlikely(do_tstamp)) &&
Li Yang5b28bea2009-03-27 15:54:30 -07002077 (skb_headroom(skb) < GMAC_FCB_LEN)) {
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07002078 struct sk_buff *skb_new;
2079
2080 skb_new = skb_realloc_headroom(skb, GMAC_FCB_LEN);
2081 if (!skb_new) {
2082 dev->stats.tx_errors++;
David S. Millerbd14ba82009-03-27 01:10:58 -07002083 kfree_skb(skb);
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07002084 return NETDEV_TX_OK;
2085 }
2086 kfree_skb(skb);
2087 skb = skb_new;
2088 }
2089
Dai Haruki4669bc92008-12-17 16:51:04 -08002090 /* total number of fragments in the SKB */
2091 nr_frags = skb_shinfo(skb)->nr_frags;
2092
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002093 /* calculate the required number of TxBDs for this skb */
2094 if (unlikely(do_tstamp))
2095 nr_txbds = nr_frags + 2;
2096 else
2097 nr_txbds = nr_frags + 1;
2098
Dai Haruki4669bc92008-12-17 16:51:04 -08002099 /* check if there is space to queue this packet */
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002100 if (nr_txbds > tx_queue->num_txbdfree) {
Dai Haruki4669bc92008-12-17 16:51:04 -08002101 /* no space, stop the queue */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002102 netif_tx_stop_queue(txq);
Dai Haruki4669bc92008-12-17 16:51:04 -08002103 dev->stats.tx_fifo_errors++;
Dai Haruki4669bc92008-12-17 16:51:04 -08002104 return NETDEV_TX_BUSY;
2105 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106
2107 /* Update transmit stats */
Eric Dumazet1ac9ad12011-01-12 12:13:14 +00002108 tx_queue->stats.tx_bytes += skb->len;
2109 tx_queue->stats.tx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002111 txbdp = txbdp_start = tx_queue->cur_tx;
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002112 lstatus = txbdp->lstatus;
2113
2114 /* Time stamp insertion requires one additional TxBD */
2115 if (unlikely(do_tstamp))
2116 txbdp_tstamp = txbdp = next_txbd(txbdp, base,
2117 tx_queue->tx_ring_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118
Dai Haruki4669bc92008-12-17 16:51:04 -08002119 if (nr_frags == 0) {
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002120 if (unlikely(do_tstamp))
2121 txbdp_tstamp->lstatus |= BD_LFLAG(TXBD_LAST |
2122 TXBD_INTERRUPT);
2123 else
2124 lstatus |= BD_LFLAG(TXBD_LAST | TXBD_INTERRUPT);
Dai Haruki4669bc92008-12-17 16:51:04 -08002125 } else {
2126 /* Place the fragment addresses and lengths into the TxBDs */
2127 for (i = 0; i < nr_frags; i++) {
2128 /* Point at the next BD, wrapping as needed */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002129 txbdp = next_txbd(txbdp, base, tx_queue->tx_ring_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130
Dai Haruki4669bc92008-12-17 16:51:04 -08002131 length = skb_shinfo(skb)->frags[i].size;
2132
2133 lstatus = txbdp->lstatus | length |
2134 BD_LFLAG(TXBD_READY);
2135
2136 /* Handle the last BD specially */
2137 if (i == nr_frags - 1)
2138 lstatus |= BD_LFLAG(TXBD_LAST | TXBD_INTERRUPT);
2139
Kumar Gala48268572009-03-18 23:28:22 -07002140 bufaddr = dma_map_page(&priv->ofdev->dev,
Dai Haruki4669bc92008-12-17 16:51:04 -08002141 skb_shinfo(skb)->frags[i].page,
2142 skb_shinfo(skb)->frags[i].page_offset,
2143 length,
2144 DMA_TO_DEVICE);
2145
2146 /* set the TxBD length and buffer pointer */
2147 txbdp->bufPtr = bufaddr;
2148 txbdp->lstatus = lstatus;
2149 }
2150
2151 lstatus = txbdp_start->lstatus;
2152 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153
Kumar Gala0bbaf062005-06-20 10:54:21 -05002154 /* Set up checksumming */
Dai Haruki12dea572008-12-16 15:30:20 -08002155 if (CHECKSUM_PARTIAL == skb->ip_summed) {
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07002156 fcb = gfar_add_fcb(skb);
2157 lstatus |= BD_LFLAG(TXBD_TOE);
2158 gfar_tx_checksum(skb, fcb);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002159 }
2160
Jesse Grosseab6d182010-10-20 13:56:03 +00002161 if (vlan_tx_tag_present(skb)) {
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07002162 if (unlikely(NULL == fcb)) {
2163 fcb = gfar_add_fcb(skb);
Dai Haruki5a5efed2008-12-16 15:34:50 -08002164 lstatus |= BD_LFLAG(TXBD_TOE);
Andy Fleming7f7f5312005-11-11 12:38:59 -06002165 }
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07002166
2167 gfar_tx_vlan(skb, fcb);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002168 }
2169
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002170 /* Setup tx hardware time stamping if requested */
2171 if (unlikely(do_tstamp)) {
Oliver Hartkopp2244d072010-08-17 08:59:14 +00002172 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002173 if (fcb == NULL)
2174 fcb = gfar_add_fcb(skb);
2175 fcb->ptp = 1;
2176 lstatus |= BD_LFLAG(TXBD_TOE);
2177 }
2178
Kumar Gala48268572009-03-18 23:28:22 -07002179 txbdp_start->bufPtr = dma_map_single(&priv->ofdev->dev, skb->data,
Dai Haruki4669bc92008-12-17 16:51:04 -08002180 skb_headlen(skb), DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002182 /*
2183 * If time stamping is requested one additional TxBD must be set up. The
2184 * first TxBD points to the FCB and must have a data length of
2185 * GMAC_FCB_LEN. The second TxBD points to the actual frame data with
2186 * the full frame length.
2187 */
2188 if (unlikely(do_tstamp)) {
2189 txbdp_tstamp->bufPtr = txbdp_start->bufPtr + GMAC_FCB_LEN;
2190 txbdp_tstamp->lstatus |= BD_LFLAG(TXBD_READY) |
2191 (skb_headlen(skb) - GMAC_FCB_LEN);
2192 lstatus |= BD_LFLAG(TXBD_CRC | TXBD_READY) | GMAC_FCB_LEN;
2193 } else {
2194 lstatus |= BD_LFLAG(TXBD_CRC | TXBD_READY) | skb_headlen(skb);
2195 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196
Dai Haruki4669bc92008-12-17 16:51:04 -08002197 /*
Anton Vorontsova3bc1f12009-11-10 14:11:10 +00002198 * We can work in parallel with gfar_clean_tx_ring(), except
2199 * when modifying num_txbdfree. Note that we didn't grab the lock
2200 * when we were reading the num_txbdfree and checking for available
2201 * space, that's because outside of this function it can only grow,
2202 * and once we've got needed space, it cannot suddenly disappear.
2203 *
2204 * The lock also protects us from gfar_error(), which can modify
2205 * regs->tstat and thus retrigger the transfers, which is why we
2206 * also must grab the lock before setting ready bit for the first
2207 * to be transmitted BD.
2208 */
2209 spin_lock_irqsave(&tx_queue->txlock, flags);
2210
2211 /*
Dai Haruki4669bc92008-12-17 16:51:04 -08002212 * The powerpc-specific eieio() is used, as wmb() has too strong
Scott Wood3b6330c2007-05-16 15:06:59 -05002213 * semantics (it requires synchronization between cacheable and
2214 * uncacheable mappings, which eieio doesn't provide and which we
2215 * don't need), thus requiring a more expensive sync instruction. At
2216 * some point, the set of architecture-independent barrier functions
2217 * should be expanded to include weaker barriers.
2218 */
Scott Wood3b6330c2007-05-16 15:06:59 -05002219 eieio();
Andy Fleming7f7f5312005-11-11 12:38:59 -06002220
Dai Haruki4669bc92008-12-17 16:51:04 -08002221 txbdp_start->lstatus = lstatus;
2222
Anton Vorontsov0eddba52010-03-03 08:18:58 +00002223 eieio(); /* force lstatus write before tx_skbuff */
2224
2225 tx_queue->tx_skbuff[tx_queue->skb_curtx] = skb;
2226
Dai Haruki4669bc92008-12-17 16:51:04 -08002227 /* Update the current skb pointer to the next entry we will use
2228 * (wrapping if necessary) */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002229 tx_queue->skb_curtx = (tx_queue->skb_curtx + 1) &
2230 TX_RING_MOD_MASK(tx_queue->tx_ring_size);
Dai Haruki4669bc92008-12-17 16:51:04 -08002231
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002232 tx_queue->cur_tx = next_txbd(txbdp, base, tx_queue->tx_ring_size);
Dai Haruki4669bc92008-12-17 16:51:04 -08002233
2234 /* reduce TxBD free count */
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002235 tx_queue->num_txbdfree -= (nr_txbds);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236
2237 /* If the next BD still needs to be cleaned up, then the bds
2238 are full. We need to tell the kernel to stop sending us stuff. */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002239 if (!tx_queue->num_txbdfree) {
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002240 netif_tx_stop_queue(txq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002242 dev->stats.tx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 }
2244
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 /* Tell the DMA to go go go */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002246 gfar_write(&regs->tstat, TSTAT_CLEAR_THALT >> tx_queue->qindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247
2248 /* Unlock priv */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002249 spin_unlock_irqrestore(&tx_queue->txlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07002251 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252}
2253
2254/* Stops the kernel queue, and halts the controller */
2255static int gfar_close(struct net_device *dev)
2256{
2257 struct gfar_private *priv = netdev_priv(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002258
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002259 disable_napi(priv);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002260
Sebastian Siewiorab939902008-08-19 21:12:45 +02002261 cancel_work_sync(&priv->reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 stop_gfar(dev);
2263
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002264 /* Disconnect from the PHY */
2265 phy_disconnect(priv->phydev);
2266 priv->phydev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002268 netif_tx_stop_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269
2270 return 0;
2271}
2272
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273/* Changes the mac address if the controller is not running. */
Andy Flemingf162b9d2008-05-02 13:00:30 -05002274static int gfar_set_mac_address(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275{
Andy Fleming7f7f5312005-11-11 12:38:59 -06002276 gfar_set_mac_for_addr(dev, 0, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277
2278 return 0;
2279}
2280
2281
Kumar Gala0bbaf062005-06-20 10:54:21 -05002282/* Enables and disables VLAN insertion/extraction */
2283static void gfar_vlan_rx_register(struct net_device *dev,
2284 struct vlan_group *grp)
2285{
2286 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002287 struct gfar __iomem *regs = NULL;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002288 unsigned long flags;
2289 u32 tempval;
2290
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002291 regs = priv->gfargrp[0].regs;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002292 local_irq_save(flags);
2293 lock_rx_qs(priv);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002294
Anton Vorontsovcd1f55a2009-01-26 14:33:23 -08002295 priv->vlgrp = grp;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002296
2297 if (grp) {
2298 /* Enable VLAN tag insertion */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002299 tempval = gfar_read(&regs->tctrl);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002300 tempval |= TCTRL_VLINS;
2301
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002302 gfar_write(&regs->tctrl, tempval);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002303
Kumar Gala0bbaf062005-06-20 10:54:21 -05002304 /* Enable VLAN tag extraction */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002305 tempval = gfar_read(&regs->rctrl);
Dai Haruki77ecaf22008-12-16 15:30:48 -08002306 tempval |= (RCTRL_VLEX | RCTRL_PRSDEP_INIT);
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002307 gfar_write(&regs->rctrl, tempval);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002308 } else {
2309 /* Disable VLAN tag insertion */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002310 tempval = gfar_read(&regs->tctrl);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002311 tempval &= ~TCTRL_VLINS;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002312 gfar_write(&regs->tctrl, tempval);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002313
2314 /* Disable VLAN tag extraction */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002315 tempval = gfar_read(&regs->rctrl);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002316 tempval &= ~RCTRL_VLEX;
Dai Haruki77ecaf22008-12-16 15:30:48 -08002317 /* If parse is no longer required, then disable parser */
2318 if (tempval & RCTRL_REQ_PARSER)
2319 tempval |= RCTRL_PRSDEP_INIT;
2320 else
2321 tempval &= ~RCTRL_PRSDEP_INIT;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002322 gfar_write(&regs->rctrl, tempval);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002323 }
2324
Dai Haruki77ecaf22008-12-16 15:30:48 -08002325 gfar_change_mtu(dev, dev->mtu);
2326
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002327 unlock_rx_qs(priv);
2328 local_irq_restore(flags);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002329}
2330
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331static int gfar_change_mtu(struct net_device *dev, int new_mtu)
2332{
2333 int tempsize, tempval;
2334 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002335 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 int oldsize = priv->rx_buffer_size;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002337 int frame_size = new_mtu + ETH_HLEN;
2338
Dai Haruki77ecaf22008-12-16 15:30:48 -08002339 if (priv->vlgrp)
Dai Harukifaa89572008-03-24 10:53:26 -05002340 frame_size += VLAN_HLEN;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002341
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 if ((frame_size < 64) || (frame_size > JUMBO_FRAME_SIZE)) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05002343 if (netif_msg_drv(priv))
2344 printk(KERN_ERR "%s: Invalid MTU setting\n",
2345 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 return -EINVAL;
2347 }
2348
Dai Haruki77ecaf22008-12-16 15:30:48 -08002349 if (gfar_uses_fcb(priv))
2350 frame_size += GMAC_FCB_LEN;
2351
2352 frame_size += priv->padding;
2353
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 tempsize =
2355 (frame_size & ~(INCREMENTAL_BUFFER_SIZE - 1)) +
2356 INCREMENTAL_BUFFER_SIZE;
2357
2358 /* Only stop and start the controller if it isn't already
Andy Fleming7f7f5312005-11-11 12:38:59 -06002359 * stopped, and we changed something */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 if ((oldsize != tempsize) && (dev->flags & IFF_UP))
2361 stop_gfar(dev);
2362
2363 priv->rx_buffer_size = tempsize;
2364
2365 dev->mtu = new_mtu;
2366
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002367 gfar_write(&regs->mrblr, priv->rx_buffer_size);
2368 gfar_write(&regs->maxfrm, priv->rx_buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369
2370 /* If the mtu is larger than the max size for standard
2371 * ethernet frames (ie, a jumbo frame), then set maccfg2
2372 * to allow huge frames, and to check the length */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002373 tempval = gfar_read(&regs->maccfg2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374
Anton Vorontsov7d350972010-06-30 06:39:12 +00002375 if (priv->rx_buffer_size > DEFAULT_RX_BUFFER_SIZE ||
2376 gfar_has_errata(priv, GFAR_ERRATA_74))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 tempval |= (MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK);
2378 else
2379 tempval &= ~(MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK);
2380
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002381 gfar_write(&regs->maccfg2, tempval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382
2383 if ((oldsize != tempsize) && (dev->flags & IFF_UP))
2384 startup_gfar(dev);
2385
2386 return 0;
2387}
2388
Sebastian Siewiorab939902008-08-19 21:12:45 +02002389/* gfar_reset_task gets scheduled when a packet has not been
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 * transmitted after a set amount of time.
2391 * For now, assume that clearing out all the structures, and
Sebastian Siewiorab939902008-08-19 21:12:45 +02002392 * starting over will fix the problem.
2393 */
2394static void gfar_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395{
Sebastian Siewiorab939902008-08-19 21:12:45 +02002396 struct gfar_private *priv = container_of(work, struct gfar_private,
2397 reset_task);
Kumar Gala48268572009-03-18 23:28:22 -07002398 struct net_device *dev = priv->ndev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399
2400 if (dev->flags & IFF_UP) {
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002401 netif_tx_stop_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 stop_gfar(dev);
2403 startup_gfar(dev);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002404 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 }
2406
David S. Miller263ba322008-07-15 03:47:41 -07002407 netif_tx_schedule_all(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408}
2409
Sebastian Siewiorab939902008-08-19 21:12:45 +02002410static void gfar_timeout(struct net_device *dev)
2411{
2412 struct gfar_private *priv = netdev_priv(dev);
2413
2414 dev->stats.tx_errors++;
2415 schedule_work(&priv->reset_task);
2416}
2417
Eran Libertyacbc0f02010-07-07 15:54:54 -07002418static void gfar_align_skb(struct sk_buff *skb)
2419{
2420 /* We need the data buffer to be aligned properly. We will reserve
2421 * as many bytes as needed to align the data properly
2422 */
2423 skb_reserve(skb, RXBUF_ALIGNMENT -
2424 (((unsigned long) skb->data) & (RXBUF_ALIGNMENT - 1)));
2425}
2426
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427/* Interrupt Handler for Transmit complete */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002428static int gfar_clean_tx_ring(struct gfar_priv_tx_q *tx_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429{
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002430 struct net_device *dev = tx_queue->dev;
Dai Harukid080cd62008-04-09 19:37:51 -05002431 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002432 struct gfar_priv_rx_q *rx_queue = NULL;
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002433 struct txbd8 *bdp, *next = NULL;
Dai Haruki4669bc92008-12-17 16:51:04 -08002434 struct txbd8 *lbdp = NULL;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002435 struct txbd8 *base = tx_queue->tx_bd_base;
Dai Haruki4669bc92008-12-17 16:51:04 -08002436 struct sk_buff *skb;
2437 int skb_dirtytx;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002438 int tx_ring_size = tx_queue->tx_ring_size;
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002439 int frags = 0, nr_txbds = 0;
Dai Haruki4669bc92008-12-17 16:51:04 -08002440 int i;
Dai Harukid080cd62008-04-09 19:37:51 -05002441 int howmany = 0;
Dai Haruki4669bc92008-12-17 16:51:04 -08002442 u32 lstatus;
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002443 size_t buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002445 rx_queue = priv->rx_queue[tx_queue->qindex];
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002446 bdp = tx_queue->dirty_tx;
2447 skb_dirtytx = tx_queue->skb_dirtytx;
Dai Haruki4669bc92008-12-17 16:51:04 -08002448
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002449 while ((skb = tx_queue->tx_skbuff[skb_dirtytx])) {
Anton Vorontsova3bc1f12009-11-10 14:11:10 +00002450 unsigned long flags;
2451
Dai Haruki4669bc92008-12-17 16:51:04 -08002452 frags = skb_shinfo(skb)->nr_frags;
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002453
2454 /*
2455 * When time stamping, one additional TxBD must be freed.
2456 * Also, we need to dma_unmap_single() the TxPAL.
2457 */
Oliver Hartkopp2244d072010-08-17 08:59:14 +00002458 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS))
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002459 nr_txbds = frags + 2;
2460 else
2461 nr_txbds = frags + 1;
2462
2463 lbdp = skip_txbd(bdp, nr_txbds - 1, base, tx_ring_size);
Dai Haruki4669bc92008-12-17 16:51:04 -08002464
2465 lstatus = lbdp->lstatus;
2466
2467 /* Only clean completed frames */
2468 if ((lstatus & BD_LFLAG(TXBD_READY)) &&
2469 (lstatus & BD_LENGTH_MASK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 break;
2471
Oliver Hartkopp2244d072010-08-17 08:59:14 +00002472 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)) {
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002473 next = next_txbd(bdp, base, tx_ring_size);
2474 buflen = next->length + GMAC_FCB_LEN;
2475 } else
2476 buflen = bdp->length;
2477
2478 dma_unmap_single(&priv->ofdev->dev, bdp->bufPtr,
2479 buflen, DMA_TO_DEVICE);
2480
Oliver Hartkopp2244d072010-08-17 08:59:14 +00002481 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)) {
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002482 struct skb_shared_hwtstamps shhwtstamps;
2483 u64 *ns = (u64*) (((u32)skb->data + 0x10) & ~0x7);
2484 memset(&shhwtstamps, 0, sizeof(shhwtstamps));
2485 shhwtstamps.hwtstamp = ns_to_ktime(*ns);
2486 skb_tstamp_tx(skb, &shhwtstamps);
2487 bdp->lstatus &= BD_LFLAG(TXBD_WRAP);
2488 bdp = next;
2489 }
Dai Haruki4669bc92008-12-17 16:51:04 -08002490
2491 bdp->lstatus &= BD_LFLAG(TXBD_WRAP);
2492 bdp = next_txbd(bdp, base, tx_ring_size);
2493
2494 for (i = 0; i < frags; i++) {
Kumar Gala48268572009-03-18 23:28:22 -07002495 dma_unmap_page(&priv->ofdev->dev,
Dai Haruki4669bc92008-12-17 16:51:04 -08002496 bdp->bufPtr,
2497 bdp->length,
2498 DMA_TO_DEVICE);
2499 bdp->lstatus &= BD_LFLAG(TXBD_WRAP);
2500 bdp = next_txbd(bdp, base, tx_ring_size);
2501 }
2502
Andy Fleming0fd56bb2009-02-04 16:43:16 -08002503 /*
2504 * If there's room in the queue (limit it to rx_buffer_size)
2505 * we add this skb back into the pool, if it's the right size
2506 */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002507 if (skb_queue_len(&priv->rx_recycle) < rx_queue->rx_ring_size &&
Andy Fleming0fd56bb2009-02-04 16:43:16 -08002508 skb_recycle_check(skb, priv->rx_buffer_size +
Eran Libertyacbc0f02010-07-07 15:54:54 -07002509 RXBUF_ALIGNMENT)) {
2510 gfar_align_skb(skb);
Jarek Poplawskicd0ea242010-10-19 00:06:36 +00002511 skb_queue_head(&priv->rx_recycle, skb);
Eran Libertyacbc0f02010-07-07 15:54:54 -07002512 } else
Andy Fleming0fd56bb2009-02-04 16:43:16 -08002513 dev_kfree_skb_any(skb);
2514
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002515 tx_queue->tx_skbuff[skb_dirtytx] = NULL;
Dai Haruki4669bc92008-12-17 16:51:04 -08002516
2517 skb_dirtytx = (skb_dirtytx + 1) &
2518 TX_RING_MOD_MASK(tx_ring_size);
2519
Dai Harukid080cd62008-04-09 19:37:51 -05002520 howmany++;
Anton Vorontsova3bc1f12009-11-10 14:11:10 +00002521 spin_lock_irqsave(&tx_queue->txlock, flags);
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002522 tx_queue->num_txbdfree += nr_txbds;
Anton Vorontsova3bc1f12009-11-10 14:11:10 +00002523 spin_unlock_irqrestore(&tx_queue->txlock, flags);
Dai Haruki4669bc92008-12-17 16:51:04 -08002524 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525
Dai Haruki4669bc92008-12-17 16:51:04 -08002526 /* If we freed a buffer, we can restart transmission, if necessary */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002527 if (__netif_subqueue_stopped(dev, tx_queue->qindex) && tx_queue->num_txbdfree)
2528 netif_wake_subqueue(dev, tx_queue->qindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529
Dai Haruki4669bc92008-12-17 16:51:04 -08002530 /* Update dirty indicators */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002531 tx_queue->skb_dirtytx = skb_dirtytx;
2532 tx_queue->dirty_tx = bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533
Dai Harukid080cd62008-04-09 19:37:51 -05002534 return howmany;
2535}
2536
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002537static void gfar_schedule_cleanup(struct gfar_priv_grp *gfargrp)
Dai Haruki8c7396a2008-12-17 16:52:00 -08002538{
Anton Vorontsova6d0b912009-01-12 21:57:34 -08002539 unsigned long flags;
2540
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002541 spin_lock_irqsave(&gfargrp->grplock, flags);
2542 if (napi_schedule_prep(&gfargrp->napi)) {
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002543 gfar_write(&gfargrp->regs->imask, IMASK_RTX_DISABLED);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002544 __napi_schedule(&gfargrp->napi);
Jarek Poplawski8707bdd2009-02-09 14:59:30 -08002545 } else {
2546 /*
2547 * Clear IEVENT, so interrupts aren't called again
2548 * because of the packets that have already arrived.
2549 */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002550 gfar_write(&gfargrp->regs->ievent, IEVENT_RTX_MASK);
Dai Haruki8c7396a2008-12-17 16:52:00 -08002551 }
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002552 spin_unlock_irqrestore(&gfargrp->grplock, flags);
Anton Vorontsova6d0b912009-01-12 21:57:34 -08002553
Dai Haruki8c7396a2008-12-17 16:52:00 -08002554}
2555
Dai Harukid080cd62008-04-09 19:37:51 -05002556/* Interrupt Handler for Transmit complete */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002557static irqreturn_t gfar_transmit(int irq, void *grp_id)
Dai Harukid080cd62008-04-09 19:37:51 -05002558{
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002559 gfar_schedule_cleanup((struct gfar_priv_grp *)grp_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 return IRQ_HANDLED;
2561}
2562
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002563static void gfar_new_rxbdp(struct gfar_priv_rx_q *rx_queue, struct rxbd8 *bdp,
Andy Fleming815b97c2008-04-22 17:18:29 -05002564 struct sk_buff *skb)
2565{
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002566 struct net_device *dev = rx_queue->dev;
Andy Fleming815b97c2008-04-22 17:18:29 -05002567 struct gfar_private *priv = netdev_priv(dev);
Anton Vorontsov8a102fe2009-10-12 06:00:37 +00002568 dma_addr_t buf;
Andy Fleming815b97c2008-04-22 17:18:29 -05002569
Anton Vorontsov8a102fe2009-10-12 06:00:37 +00002570 buf = dma_map_single(&priv->ofdev->dev, skb->data,
2571 priv->rx_buffer_size, DMA_FROM_DEVICE);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002572 gfar_init_rxbdp(rx_queue, bdp, buf);
Andy Fleming815b97c2008-04-22 17:18:29 -05002573}
2574
Eran Libertyacbc0f02010-07-07 15:54:54 -07002575static struct sk_buff * gfar_alloc_skb(struct net_device *dev)
2576{
2577 struct gfar_private *priv = netdev_priv(dev);
2578 struct sk_buff *skb = NULL;
2579
2580 skb = netdev_alloc_skb(dev, priv->rx_buffer_size + RXBUF_ALIGNMENT);
2581 if (!skb)
2582 return NULL;
2583
2584 gfar_align_skb(skb);
2585
2586 return skb;
2587}
Andy Fleming815b97c2008-04-22 17:18:29 -05002588
2589struct sk_buff * gfar_new_skb(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590{
2591 struct gfar_private *priv = netdev_priv(dev);
2592 struct sk_buff *skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593
Jarek Poplawskicd0ea242010-10-19 00:06:36 +00002594 skb = skb_dequeue(&priv->rx_recycle);
Andy Fleming0fd56bb2009-02-04 16:43:16 -08002595 if (!skb)
Eran Libertyacbc0f02010-07-07 15:54:54 -07002596 skb = gfar_alloc_skb(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 return skb;
2599}
2600
Li Yang298e1a92007-10-16 14:18:13 +08002601static inline void count_errors(unsigned short status, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602{
Li Yang298e1a92007-10-16 14:18:13 +08002603 struct gfar_private *priv = netdev_priv(dev);
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002604 struct net_device_stats *stats = &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 struct gfar_extra_stats *estats = &priv->extra_stats;
2606
2607 /* If the packet was truncated, none of the other errors
2608 * matter */
2609 if (status & RXBD_TRUNCATED) {
2610 stats->rx_length_errors++;
2611
2612 estats->rx_trunc++;
2613
2614 return;
2615 }
2616 /* Count the errors, if there were any */
2617 if (status & (RXBD_LARGE | RXBD_SHORT)) {
2618 stats->rx_length_errors++;
2619
2620 if (status & RXBD_LARGE)
2621 estats->rx_large++;
2622 else
2623 estats->rx_short++;
2624 }
2625 if (status & RXBD_NONOCTET) {
2626 stats->rx_frame_errors++;
2627 estats->rx_nonoctet++;
2628 }
2629 if (status & RXBD_CRCERR) {
2630 estats->rx_crcerr++;
2631 stats->rx_crc_errors++;
2632 }
2633 if (status & RXBD_OVERRUN) {
2634 estats->rx_overrun++;
2635 stats->rx_crc_errors++;
2636 }
2637}
2638
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002639irqreturn_t gfar_receive(int irq, void *grp_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640{
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002641 gfar_schedule_cleanup((struct gfar_priv_grp *)grp_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 return IRQ_HANDLED;
2643}
2644
Kumar Gala0bbaf062005-06-20 10:54:21 -05002645static inline void gfar_rx_checksum(struct sk_buff *skb, struct rxfcb *fcb)
2646{
2647 /* If valid headers were found, and valid sums
2648 * were verified, then we tell the kernel that no
2649 * checksumming is necessary. Otherwise, it is */
Andy Fleming7f7f5312005-11-11 12:38:59 -06002650 if ((fcb->flags & RXFCB_CSUM_MASK) == (RXFCB_CIP | RXFCB_CTU))
Kumar Gala0bbaf062005-06-20 10:54:21 -05002651 skb->ip_summed = CHECKSUM_UNNECESSARY;
2652 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07002653 skb_checksum_none_assert(skb);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002654}
2655
2656
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657/* gfar_process_frame() -- handle one incoming packet if skb
2658 * isn't NULL. */
2659static int gfar_process_frame(struct net_device *dev, struct sk_buff *skb,
Dai Haruki2c2db482008-12-16 15:31:15 -08002660 int amount_pull)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661{
2662 struct gfar_private *priv = netdev_priv(dev);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002663 struct rxfcb *fcb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664
Dai Haruki2c2db482008-12-16 15:31:15 -08002665 int ret;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002666
Dai Haruki2c2db482008-12-16 15:31:15 -08002667 /* fcb is at the beginning if exists */
2668 fcb = (struct rxfcb *)skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669
Dai Haruki2c2db482008-12-16 15:31:15 -08002670 /* Remove the FCB from the skb */
2671 /* Remove the padded bytes, if there are any */
Sandeep Gopalpetf74dac02009-12-24 03:13:06 +00002672 if (amount_pull) {
2673 skb_record_rx_queue(skb, fcb->rq);
Dai Haruki2c2db482008-12-16 15:31:15 -08002674 skb_pull(skb, amount_pull);
Sandeep Gopalpetf74dac02009-12-24 03:13:06 +00002675 }
Kumar Gala0bbaf062005-06-20 10:54:21 -05002676
Manfred Rudigiercc772ab2010-04-08 23:10:03 +00002677 /* Get receive timestamp from the skb */
2678 if (priv->hwts_rx_en) {
2679 struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
2680 u64 *ns = (u64 *) skb->data;
2681 memset(shhwtstamps, 0, sizeof(*shhwtstamps));
2682 shhwtstamps->hwtstamp = ns_to_ktime(*ns);
2683 }
2684
2685 if (priv->padding)
2686 skb_pull(skb, priv->padding);
2687
Dai Haruki2c2db482008-12-16 15:31:15 -08002688 if (priv->rx_csum_enable)
2689 gfar_rx_checksum(skb, fcb);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002690
Dai Haruki2c2db482008-12-16 15:31:15 -08002691 /* Tell the skb what kind of packet this is */
2692 skb->protocol = eth_type_trans(skb, dev);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002693
Dai Haruki2c2db482008-12-16 15:31:15 -08002694 /* Send the packet up the stack */
2695 if (unlikely(priv->vlgrp && (fcb->flags & RXFCB_VLN)))
2696 ret = vlan_hwaccel_receive_skb(skb, priv->vlgrp, fcb->vlctl);
2697 else
2698 ret = netif_receive_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699
Dai Haruki2c2db482008-12-16 15:31:15 -08002700 if (NET_RX_DROP == ret)
2701 priv->extra_stats.kernel_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702
2703 return 0;
2704}
2705
2706/* gfar_clean_rx_ring() -- Processes each frame in the rx ring
Kumar Gala0bbaf062005-06-20 10:54:21 -05002707 * until the budget/quota has been reached. Returns the number
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 * of frames handled
2709 */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002710int gfar_clean_rx_ring(struct gfar_priv_rx_q *rx_queue, int rx_work_limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711{
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002712 struct net_device *dev = rx_queue->dev;
Andy Fleming31de1982008-12-16 15:33:40 -08002713 struct rxbd8 *bdp, *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 struct sk_buff *skb;
Dai Haruki2c2db482008-12-16 15:31:15 -08002715 int pkt_len;
2716 int amount_pull;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 int howmany = 0;
2718 struct gfar_private *priv = netdev_priv(dev);
2719
2720 /* Get the first full descriptor */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002721 bdp = rx_queue->cur_rx;
2722 base = rx_queue->rx_bd_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723
Manfred Rudigiercc772ab2010-04-08 23:10:03 +00002724 amount_pull = (gfar_uses_fcb(priv) ? GMAC_FCB_LEN : 0);
Dai Haruki2c2db482008-12-16 15:31:15 -08002725
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 while (!((bdp->status & RXBD_EMPTY) || (--rx_work_limit < 0))) {
Andy Fleming815b97c2008-04-22 17:18:29 -05002727 struct sk_buff *newskb;
Scott Wood3b6330c2007-05-16 15:06:59 -05002728 rmb();
Andy Fleming815b97c2008-04-22 17:18:29 -05002729
2730 /* Add another skb for the future */
2731 newskb = gfar_new_skb(dev);
2732
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002733 skb = rx_queue->rx_skbuff[rx_queue->skb_currx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734
Kumar Gala48268572009-03-18 23:28:22 -07002735 dma_unmap_single(&priv->ofdev->dev, bdp->bufPtr,
Andy Fleming81183052008-11-12 10:07:11 -06002736 priv->rx_buffer_size, DMA_FROM_DEVICE);
2737
Anton Vorontsov63b88b92010-06-11 10:51:03 +00002738 if (unlikely(!(bdp->status & RXBD_ERR) &&
2739 bdp->length > priv->rx_buffer_size))
2740 bdp->status = RXBD_LARGE;
2741
Andy Fleming815b97c2008-04-22 17:18:29 -05002742 /* We drop the frame if we failed to allocate a new buffer */
2743 if (unlikely(!newskb || !(bdp->status & RXBD_LAST) ||
2744 bdp->status & RXBD_ERR)) {
2745 count_errors(bdp->status, dev);
2746
2747 if (unlikely(!newskb))
2748 newskb = skb;
Eran Libertyacbc0f02010-07-07 15:54:54 -07002749 else if (skb)
Jarek Poplawskicd0ea242010-10-19 00:06:36 +00002750 skb_queue_head(&priv->rx_recycle, skb);
Andy Fleming815b97c2008-04-22 17:18:29 -05002751 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 /* Increment the number of packets */
Sandeep Gopalpeta7f38042009-12-16 01:15:07 +00002753 rx_queue->stats.rx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 howmany++;
2755
Dai Haruki2c2db482008-12-16 15:31:15 -08002756 if (likely(skb)) {
2757 pkt_len = bdp->length - ETH_FCS_LEN;
2758 /* Remove the FCS from the packet length */
2759 skb_put(skb, pkt_len);
Sandeep Gopalpeta7f38042009-12-16 01:15:07 +00002760 rx_queue->stats.rx_bytes += pkt_len;
Sandeep Gopalpetf74dac02009-12-24 03:13:06 +00002761 skb_record_rx_queue(skb, rx_queue->qindex);
Dai Haruki2c2db482008-12-16 15:31:15 -08002762 gfar_process_frame(dev, skb, amount_pull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763
Dai Haruki2c2db482008-12-16 15:31:15 -08002764 } else {
2765 if (netif_msg_rx_err(priv))
2766 printk(KERN_WARNING
2767 "%s: Missing skb!\n", dev->name);
Sandeep Gopalpeta7f38042009-12-16 01:15:07 +00002768 rx_queue->stats.rx_dropped++;
Dai Haruki2c2db482008-12-16 15:31:15 -08002769 priv->extra_stats.rx_skbmissing++;
2770 }
2771
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 }
2773
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002774 rx_queue->rx_skbuff[rx_queue->skb_currx] = newskb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775
Andy Fleming815b97c2008-04-22 17:18:29 -05002776 /* Setup the new bdp */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002777 gfar_new_rxbdp(rx_queue, bdp, newskb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778
2779 /* Update to the next pointer */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002780 bdp = next_bd(bdp, base, rx_queue->rx_ring_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781
2782 /* update to point at the next skb */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002783 rx_queue->skb_currx =
2784 (rx_queue->skb_currx + 1) &
2785 RX_RING_MOD_MASK(rx_queue->rx_ring_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 }
2787
2788 /* Update the current rxbd pointer to be the next one */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002789 rx_queue->cur_rx = bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 return howmany;
2792}
2793
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002794static int gfar_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795{
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002796 struct gfar_priv_grp *gfargrp = container_of(napi,
2797 struct gfar_priv_grp, napi);
2798 struct gfar_private *priv = gfargrp->priv;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002799 struct gfar __iomem *regs = gfargrp->regs;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002800 struct gfar_priv_tx_q *tx_queue = NULL;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002801 struct gfar_priv_rx_q *rx_queue = NULL;
2802 int rx_cleaned = 0, budget_per_queue = 0, rx_cleaned_per_queue = 0;
Anton Vorontsov18294ad2009-11-04 12:53:00 +00002803 int tx_cleaned = 0, i, left_over_budget = budget;
2804 unsigned long serviced_queues = 0;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002805 int num_queues = 0;
Dai Harukid080cd62008-04-09 19:37:51 -05002806
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002807 num_queues = gfargrp->num_rx_queues;
2808 budget_per_queue = budget/num_queues;
2809
Dai Haruki8c7396a2008-12-17 16:52:00 -08002810 /* Clear IEVENT, so interrupts aren't called again
2811 * because of the packets that have already arrived */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002812 gfar_write(&regs->ievent, IEVENT_RTX_MASK);
Dai Haruki8c7396a2008-12-17 16:52:00 -08002813
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002814 while (num_queues && left_over_budget) {
2815
2816 budget_per_queue = left_over_budget/num_queues;
2817 left_over_budget = 0;
2818
Akinobu Mita984b3f52010-03-05 13:41:37 -08002819 for_each_set_bit(i, &gfargrp->rx_bit_map, priv->num_rx_queues) {
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002820 if (test_bit(i, &serviced_queues))
2821 continue;
2822 rx_queue = priv->rx_queue[i];
2823 tx_queue = priv->tx_queue[rx_queue->qindex];
2824
Anton Vorontsova3bc1f12009-11-10 14:11:10 +00002825 tx_cleaned += gfar_clean_tx_ring(tx_queue);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002826 rx_cleaned_per_queue = gfar_clean_rx_ring(rx_queue,
2827 budget_per_queue);
2828 rx_cleaned += rx_cleaned_per_queue;
2829 if(rx_cleaned_per_queue < budget_per_queue) {
2830 left_over_budget = left_over_budget +
2831 (budget_per_queue - rx_cleaned_per_queue);
2832 set_bit(i, &serviced_queues);
2833 num_queues--;
2834 }
2835 }
Dai Harukid080cd62008-04-09 19:37:51 -05002836 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837
Andy Fleming42199882008-12-17 16:52:30 -08002838 if (tx_cleaned)
2839 return budget;
2840
2841 if (rx_cleaned < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -08002842 napi_complete(napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843
2844 /* Clear the halt bit in RSTAT */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002845 gfar_write(&regs->rstat, gfargrp->rstat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002847 gfar_write(&regs->imask, IMASK_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848
2849 /* If we are coalescing interrupts, update the timer */
2850 /* Otherwise, clear it */
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002851 gfar_configure_coalescing(priv,
2852 gfargrp->rx_bit_map, gfargrp->tx_bit_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 }
2854
Andy Fleming42199882008-12-17 16:52:30 -08002855 return rx_cleaned;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857
Vitaly Woolf2d71c22006-11-07 13:27:02 +03002858#ifdef CONFIG_NET_POLL_CONTROLLER
2859/*
2860 * Polling 'interrupt' - used by things like netconsole to send skbs
2861 * without having to re-enable interrupts. It's not called while
2862 * the interrupt routine is executing.
2863 */
2864static void gfar_netpoll(struct net_device *dev)
2865{
2866 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002867 int i = 0;
Vitaly Woolf2d71c22006-11-07 13:27:02 +03002868
2869 /* If the device has multiple interrupts, run tx/rx */
Andy Flemingb31a1d82008-12-16 15:29:15 -08002870 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002871 for (i = 0; i < priv->num_grps; i++) {
2872 disable_irq(priv->gfargrp[i].interruptTransmit);
2873 disable_irq(priv->gfargrp[i].interruptReceive);
2874 disable_irq(priv->gfargrp[i].interruptError);
2875 gfar_interrupt(priv->gfargrp[i].interruptTransmit,
2876 &priv->gfargrp[i]);
2877 enable_irq(priv->gfargrp[i].interruptError);
2878 enable_irq(priv->gfargrp[i].interruptReceive);
2879 enable_irq(priv->gfargrp[i].interruptTransmit);
2880 }
Vitaly Woolf2d71c22006-11-07 13:27:02 +03002881 } else {
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002882 for (i = 0; i < priv->num_grps; i++) {
2883 disable_irq(priv->gfargrp[i].interruptTransmit);
2884 gfar_interrupt(priv->gfargrp[i].interruptTransmit,
2885 &priv->gfargrp[i]);
2886 enable_irq(priv->gfargrp[i].interruptTransmit);
Anton Vorontsov43de0042009-12-09 02:52:19 -08002887 }
Vitaly Woolf2d71c22006-11-07 13:27:02 +03002888 }
2889}
2890#endif
2891
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892/* The interrupt handler for devices with one interrupt */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002893static irqreturn_t gfar_interrupt(int irq, void *grp_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894{
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002895 struct gfar_priv_grp *gfargrp = grp_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896
2897 /* Save ievent for future reference */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002898 u32 events = gfar_read(&gfargrp->regs->ievent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 /* Check for reception */
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002901 if (events & IEVENT_RX_MASK)
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002902 gfar_receive(irq, grp_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903
2904 /* Check for transmit completion */
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002905 if (events & IEVENT_TX_MASK)
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002906 gfar_transmit(irq, grp_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002908 /* Check for errors */
2909 if (events & IEVENT_ERR_MASK)
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002910 gfar_error(irq, grp_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911
2912 return IRQ_HANDLED;
2913}
2914
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915/* Called every time the controller might need to be made
2916 * aware of new link state. The PHY code conveys this
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002917 * information through variables in the phydev structure, and this
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 * function converts those variables into the appropriate
2919 * register values, and can bring down the device if needed.
2920 */
2921static void adjust_link(struct net_device *dev)
2922{
2923 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002924 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002925 unsigned long flags;
2926 struct phy_device *phydev = priv->phydev;
2927 int new_state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002929 local_irq_save(flags);
2930 lock_tx_qs(priv);
2931
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002932 if (phydev->link) {
2933 u32 tempval = gfar_read(&regs->maccfg2);
Andy Fleming7f7f5312005-11-11 12:38:59 -06002934 u32 ecntrl = gfar_read(&regs->ecntrl);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002935
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 /* Now we make sure that we can be in full duplex mode.
2937 * If not, we operate in half-duplex mode. */
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002938 if (phydev->duplex != priv->oldduplex) {
2939 new_state = 1;
2940 if (!(phydev->duplex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 tempval &= ~(MACCFG2_FULL_DUPLEX);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002942 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 tempval |= MACCFG2_FULL_DUPLEX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002945 priv->oldduplex = phydev->duplex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 }
2947
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002948 if (phydev->speed != priv->oldspeed) {
2949 new_state = 1;
2950 switch (phydev->speed) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 case 1000:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 tempval =
2953 ((tempval & ~(MACCFG2_IF)) | MACCFG2_GMII);
Li Yangf430e492009-01-06 14:08:10 -08002954
2955 ecntrl &= ~(ECNTRL_R100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 break;
2957 case 100:
2958 case 10:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 tempval =
2960 ((tempval & ~(MACCFG2_IF)) | MACCFG2_MII);
Andy Fleming7f7f5312005-11-11 12:38:59 -06002961
2962 /* Reduced mode distinguishes
2963 * between 10 and 100 */
2964 if (phydev->speed == SPEED_100)
2965 ecntrl |= ECNTRL_R100;
2966 else
2967 ecntrl &= ~(ECNTRL_R100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 break;
2969 default:
Kumar Gala0bbaf062005-06-20 10:54:21 -05002970 if (netif_msg_link(priv))
2971 printk(KERN_WARNING
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002972 "%s: Ack! Speed (%d) is not 10/100/1000!\n",
2973 dev->name, phydev->speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 break;
2975 }
2976
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002977 priv->oldspeed = phydev->speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 }
2979
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002980 gfar_write(&regs->maccfg2, tempval);
Andy Fleming7f7f5312005-11-11 12:38:59 -06002981 gfar_write(&regs->ecntrl, ecntrl);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002982
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 if (!priv->oldlink) {
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002984 new_state = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 priv->oldlink = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 }
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002987 } else if (priv->oldlink) {
2988 new_state = 1;
2989 priv->oldlink = 0;
2990 priv->oldspeed = 0;
2991 priv->oldduplex = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002994 if (new_state && netif_msg_link(priv))
2995 phy_print_status(phydev);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002996 unlock_tx_qs(priv);
2997 local_irq_restore(flags);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002998}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999
3000/* Update the hash table based on the current list of multicast
3001 * addresses we subscribe to. Also, change the promiscuity of
3002 * the device based on the flags (this function is called
3003 * whenever dev->flags is changed */
3004static void gfar_set_multi(struct net_device *dev)
3005{
Jiri Pirko22bedad2010-04-01 21:22:57 +00003006 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00003008 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 u32 tempval;
3010
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00003011 if (dev->flags & IFF_PROMISC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 /* Set RCTRL to PROM */
3013 tempval = gfar_read(&regs->rctrl);
3014 tempval |= RCTRL_PROM;
3015 gfar_write(&regs->rctrl, tempval);
3016 } else {
3017 /* Set RCTRL to not PROM */
3018 tempval = gfar_read(&regs->rctrl);
3019 tempval &= ~(RCTRL_PROM);
3020 gfar_write(&regs->rctrl, tempval);
3021 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003022
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00003023 if (dev->flags & IFF_ALLMULTI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 /* Set the hash to rx all multicast frames */
Kumar Gala0bbaf062005-06-20 10:54:21 -05003025 gfar_write(&regs->igaddr0, 0xffffffff);
3026 gfar_write(&regs->igaddr1, 0xffffffff);
3027 gfar_write(&regs->igaddr2, 0xffffffff);
3028 gfar_write(&regs->igaddr3, 0xffffffff);
3029 gfar_write(&regs->igaddr4, 0xffffffff);
3030 gfar_write(&regs->igaddr5, 0xffffffff);
3031 gfar_write(&regs->igaddr6, 0xffffffff);
3032 gfar_write(&regs->igaddr7, 0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 gfar_write(&regs->gaddr0, 0xffffffff);
3034 gfar_write(&regs->gaddr1, 0xffffffff);
3035 gfar_write(&regs->gaddr2, 0xffffffff);
3036 gfar_write(&regs->gaddr3, 0xffffffff);
3037 gfar_write(&regs->gaddr4, 0xffffffff);
3038 gfar_write(&regs->gaddr5, 0xffffffff);
3039 gfar_write(&regs->gaddr6, 0xffffffff);
3040 gfar_write(&regs->gaddr7, 0xffffffff);
3041 } else {
Andy Fleming7f7f5312005-11-11 12:38:59 -06003042 int em_num;
3043 int idx;
3044
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 /* zero out the hash */
Kumar Gala0bbaf062005-06-20 10:54:21 -05003046 gfar_write(&regs->igaddr0, 0x0);
3047 gfar_write(&regs->igaddr1, 0x0);
3048 gfar_write(&regs->igaddr2, 0x0);
3049 gfar_write(&regs->igaddr3, 0x0);
3050 gfar_write(&regs->igaddr4, 0x0);
3051 gfar_write(&regs->igaddr5, 0x0);
3052 gfar_write(&regs->igaddr6, 0x0);
3053 gfar_write(&regs->igaddr7, 0x0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 gfar_write(&regs->gaddr0, 0x0);
3055 gfar_write(&regs->gaddr1, 0x0);
3056 gfar_write(&regs->gaddr2, 0x0);
3057 gfar_write(&regs->gaddr3, 0x0);
3058 gfar_write(&regs->gaddr4, 0x0);
3059 gfar_write(&regs->gaddr5, 0x0);
3060 gfar_write(&regs->gaddr6, 0x0);
3061 gfar_write(&regs->gaddr7, 0x0);
3062
Andy Fleming7f7f5312005-11-11 12:38:59 -06003063 /* If we have extended hash tables, we need to
3064 * clear the exact match registers to prepare for
3065 * setting them */
3066 if (priv->extended_hash) {
3067 em_num = GFAR_EM_NUM + 1;
3068 gfar_clear_exact_match(dev);
3069 idx = 1;
3070 } else {
3071 idx = 0;
3072 em_num = 0;
3073 }
3074
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00003075 if (netdev_mc_empty(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 return;
3077
3078 /* Parse the list, and set the appropriate bits */
Jiri Pirko22bedad2010-04-01 21:22:57 +00003079 netdev_for_each_mc_addr(ha, dev) {
Andy Fleming7f7f5312005-11-11 12:38:59 -06003080 if (idx < em_num) {
Jiri Pirko22bedad2010-04-01 21:22:57 +00003081 gfar_set_mac_for_addr(dev, idx, ha->addr);
Andy Fleming7f7f5312005-11-11 12:38:59 -06003082 idx++;
3083 } else
Jiri Pirko22bedad2010-04-01 21:22:57 +00003084 gfar_set_hash_for_addr(dev, ha->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 }
3086 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087}
3088
Andy Fleming7f7f5312005-11-11 12:38:59 -06003089
3090/* Clears each of the exact match registers to zero, so they
3091 * don't interfere with normal reception */
3092static void gfar_clear_exact_match(struct net_device *dev)
3093{
3094 int idx;
Joe Perchesb6bc7652010-12-21 02:16:08 -08003095 static const u8 zero_arr[MAC_ADDR_LEN] = {0, 0, 0, 0, 0, 0};
Andy Fleming7f7f5312005-11-11 12:38:59 -06003096
3097 for(idx = 1;idx < GFAR_EM_NUM + 1;idx++)
Joe Perchesb6bc7652010-12-21 02:16:08 -08003098 gfar_set_mac_for_addr(dev, idx, zero_arr);
Andy Fleming7f7f5312005-11-11 12:38:59 -06003099}
3100
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101/* Set the appropriate hash bit for the given addr */
3102/* The algorithm works like so:
3103 * 1) Take the Destination Address (ie the multicast address), and
3104 * do a CRC on it (little endian), and reverse the bits of the
3105 * result.
3106 * 2) Use the 8 most significant bits as a hash into a 256-entry
3107 * table. The table is controlled through 8 32-bit registers:
3108 * gaddr0-7. gaddr0's MSB is entry 0, and gaddr7's LSB is
3109 * gaddr7. This means that the 3 most significant bits in the
3110 * hash index which gaddr register to use, and the 5 other bits
3111 * indicate which bit (assuming an IBM numbering scheme, which
3112 * for PowerPC (tm) is usually the case) in the register holds
3113 * the entry. */
3114static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr)
3115{
3116 u32 tempval;
3117 struct gfar_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 u32 result = ether_crc(MAC_ADDR_LEN, addr);
Kumar Gala0bbaf062005-06-20 10:54:21 -05003119 int width = priv->hash_width;
3120 u8 whichbit = (result >> (32 - width)) & 0x1f;
3121 u8 whichreg = result >> (32 - width + 5);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 u32 value = (1 << (31-whichbit));
3123
Kumar Gala0bbaf062005-06-20 10:54:21 -05003124 tempval = gfar_read(priv->hash_regs[whichreg]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 tempval |= value;
Kumar Gala0bbaf062005-06-20 10:54:21 -05003126 gfar_write(priv->hash_regs[whichreg], tempval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127}
3128
Andy Fleming7f7f5312005-11-11 12:38:59 -06003129
3130/* There are multiple MAC Address register pairs on some controllers
3131 * This function sets the numth pair to a given address
3132 */
Joe Perchesb6bc7652010-12-21 02:16:08 -08003133static void gfar_set_mac_for_addr(struct net_device *dev, int num,
3134 const u8 *addr)
Andy Fleming7f7f5312005-11-11 12:38:59 -06003135{
3136 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00003137 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Andy Fleming7f7f5312005-11-11 12:38:59 -06003138 int idx;
3139 char tmpbuf[MAC_ADDR_LEN];
3140 u32 tempval;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003141 u32 __iomem *macptr = &regs->macstnaddr1;
Andy Fleming7f7f5312005-11-11 12:38:59 -06003142
3143 macptr += num*2;
3144
3145 /* Now copy it into the mac registers backwards, cuz */
3146 /* little endian is silly */
3147 for (idx = 0; idx < MAC_ADDR_LEN; idx++)
3148 tmpbuf[MAC_ADDR_LEN - 1 - idx] = addr[idx];
3149
3150 gfar_write(macptr, *((u32 *) (tmpbuf)));
3151
3152 tempval = *((u32 *) (tmpbuf + 4));
3153
3154 gfar_write(macptr+1, tempval);
3155}
3156
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157/* GFAR error interrupt handler */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003158static irqreturn_t gfar_error(int irq, void *grp_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159{
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003160 struct gfar_priv_grp *gfargrp = grp_id;
3161 struct gfar __iomem *regs = gfargrp->regs;
3162 struct gfar_private *priv= gfargrp->priv;
3163 struct net_device *dev = priv->ndev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164
3165 /* Save ievent for future reference */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003166 u32 events = gfar_read(&regs->ievent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167
3168 /* Clear IEVENT */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003169 gfar_write(&regs->ievent, events & IEVENT_ERR_MASK);
Scott Woodd87eb122008-07-11 18:04:45 -05003170
3171 /* Magic Packet is not an error. */
Andy Flemingb31a1d82008-12-16 15:29:15 -08003172 if ((priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET) &&
Scott Woodd87eb122008-07-11 18:04:45 -05003173 (events & IEVENT_MAG))
3174 events &= ~IEVENT_MAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175
3176 /* Hmm... */
Kumar Gala0bbaf062005-06-20 10:54:21 -05003177 if (netif_msg_rx_err(priv) || netif_msg_tx_err(priv))
3178 printk(KERN_DEBUG "%s: error interrupt (ievent=0x%08x imask=0x%08x)\n",
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003179 dev->name, events, gfar_read(&regs->imask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180
3181 /* Update the error counters */
3182 if (events & IEVENT_TXE) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07003183 dev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184
3185 if (events & IEVENT_LC)
Jeff Garzik09f75cd2007-10-03 17:41:50 -07003186 dev->stats.tx_window_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 if (events & IEVENT_CRL)
Jeff Garzik09f75cd2007-10-03 17:41:50 -07003188 dev->stats.tx_aborted_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 if (events & IEVENT_XFUN) {
Anton Vorontsov836cf7f2009-11-10 14:11:08 +00003190 unsigned long flags;
3191
Kumar Gala0bbaf062005-06-20 10:54:21 -05003192 if (netif_msg_tx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04003193 printk(KERN_DEBUG "%s: TX FIFO underrun, "
3194 "packet dropped.\n", dev->name);
Jeff Garzik09f75cd2007-10-03 17:41:50 -07003195 dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 priv->extra_stats.tx_underrun++;
3197
Anton Vorontsov836cf7f2009-11-10 14:11:08 +00003198 local_irq_save(flags);
3199 lock_tx_qs(priv);
3200
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201 /* Reactivate the Tx Queues */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00003202 gfar_write(&regs->tstat, gfargrp->tstat);
Anton Vorontsov836cf7f2009-11-10 14:11:08 +00003203
3204 unlock_tx_qs(priv);
3205 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 }
Kumar Gala0bbaf062005-06-20 10:54:21 -05003207 if (netif_msg_tx_err(priv))
3208 printk(KERN_DEBUG "%s: Transmit Error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 }
3210 if (events & IEVENT_BSY) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07003211 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 priv->extra_stats.rx_bsy++;
3213
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003214 gfar_receive(irq, grp_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215
Kumar Gala0bbaf062005-06-20 10:54:21 -05003216 if (netif_msg_rx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04003217 printk(KERN_DEBUG "%s: busy error (rstat: %x)\n",
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003218 dev->name, gfar_read(&regs->rstat));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 }
3220 if (events & IEVENT_BABR) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07003221 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222 priv->extra_stats.rx_babr++;
3223
Kumar Gala0bbaf062005-06-20 10:54:21 -05003224 if (netif_msg_rx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04003225 printk(KERN_DEBUG "%s: babbling RX error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 }
3227 if (events & IEVENT_EBERR) {
3228 priv->extra_stats.eberr++;
Kumar Gala0bbaf062005-06-20 10:54:21 -05003229 if (netif_msg_rx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04003230 printk(KERN_DEBUG "%s: bus error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 }
Kumar Gala0bbaf062005-06-20 10:54:21 -05003232 if ((events & IEVENT_RXC) && netif_msg_rx_status(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04003233 printk(KERN_DEBUG "%s: control frame\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234
3235 if (events & IEVENT_BABT) {
3236 priv->extra_stats.tx_babt++;
Kumar Gala0bbaf062005-06-20 10:54:21 -05003237 if (netif_msg_tx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04003238 printk(KERN_DEBUG "%s: babbling TX error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 }
3240 return IRQ_HANDLED;
3241}
3242
Andy Flemingb31a1d82008-12-16 15:29:15 -08003243static struct of_device_id gfar_match[] =
3244{
3245 {
3246 .type = "network",
3247 .compatible = "gianfar",
3248 },
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00003249 {
3250 .compatible = "fsl,etsec2",
3251 },
Andy Flemingb31a1d82008-12-16 15:29:15 -08003252 {},
3253};
Anton Vorontsove72701a2009-10-14 14:54:52 -07003254MODULE_DEVICE_TABLE(of, gfar_match);
Andy Flemingb31a1d82008-12-16 15:29:15 -08003255
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256/* Structure for a device driver */
Grant Likely74888762011-02-22 21:05:51 -07003257static struct platform_driver gfar_driver = {
Grant Likely40182942010-04-13 16:13:02 -07003258 .driver = {
3259 .name = "fsl-gianfar",
3260 .owner = THIS_MODULE,
3261 .pm = GFAR_PM_OPS,
3262 .of_match_table = gfar_match,
3263 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 .probe = gfar_probe,
3265 .remove = gfar_remove,
3266};
3267
3268static int __init gfar_init(void)
3269{
Grant Likely74888762011-02-22 21:05:51 -07003270 return platform_driver_register(&gfar_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271}
3272
3273static void __exit gfar_exit(void)
3274{
Grant Likely74888762011-02-22 21:05:51 -07003275 platform_driver_unregister(&gfar_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276}
3277
3278module_init(gfar_init);
3279module_exit(gfar_exit);
3280