blob: 068f9a2cf42c8c89a7bdcb4c5e9c1efd65a51149 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
Andy Fleminge8a2b6a2006-12-01 12:01:06 -060012 * Copyright (c) 2002-2006 Freescale Semiconductor, Inc.
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +040013 * Copyright (c) 2007 MontaVista Software, Inc.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 *
15 * This program is free software; you can redistribute it and/or modify it
16 * under the terms of the GNU General Public License as published by the
17 * Free Software Foundation; either version 2 of the License, or (at your
18 * option) any later version.
19 *
20 * Gianfar: AKA Lambda Draconis, "Dragon"
21 * RA 11 31 24.2
22 * Dec +69 19 52
23 * V 3.84
24 * B-V +1.62
25 *
26 * Theory of operation
Kumar Gala0bbaf062005-06-20 10:54:21 -050027 *
Andy Flemingb31a1d82008-12-16 15:29:15 -080028 * The driver is initialized through of_device. Configuration information
29 * is therefore conveyed through an OF-style device tree.
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 *
31 * The Gianfar Ethernet Controller uses a ring of buffer
32 * descriptors. The beginning is indicated by a register
Kumar Gala0bbaf062005-06-20 10:54:21 -050033 * pointing to the physical address of the start of the ring.
34 * The end is determined by a "wrap" bit being set in the
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 * last descriptor of the ring.
36 *
37 * When a packet is received, the RXF bit in the
Kumar Gala0bbaf062005-06-20 10:54:21 -050038 * IEVENT register is set, triggering an interrupt when the
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 * corresponding bit in the IMASK register is also set (if
40 * interrupt coalescing is active, then the interrupt may not
41 * happen immediately, but will wait until either a set number
Andy Flemingbb40dcb2005-09-23 22:54:21 -040042 * of frames or amount of time have passed). In NAPI, the
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 * interrupt handler will signal there is work to be done, and
Francois Romieu0aa15382008-07-11 00:33:52 +020044 * exit. This method will start at the last known empty
Kumar Gala0bbaf062005-06-20 10:54:21 -050045 * descriptor, and process every subsequent descriptor until there
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 * are none left with data (NAPI will stop after a set number of
47 * packets to give time to other tasks, but will eventually
48 * process all the packets). The data arrives inside a
49 * pre-allocated skb, and so after the skb is passed up to the
50 * stack, a new skb must be allocated, and the address field in
51 * the buffer descriptor must be updated to indicate this new
52 * skb.
53 *
54 * When the kernel requests that a packet be transmitted, the
55 * driver starts where it left off last time, and points the
56 * descriptor at the buffer which was passed in. The driver
57 * then informs the DMA engine that there are packets ready to
58 * be transmitted. Once the controller is finished transmitting
59 * the packet, an interrupt may be triggered (under the same
60 * conditions as for reception, but depending on the TXF bit).
61 * The driver then cleans up the buffer.
62 */
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#include <linux/string.h>
66#include <linux/errno.h>
Andy Flemingbb40dcb2005-09-23 22:54:21 -040067#include <linux/unistd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include <linux/slab.h>
69#include <linux/interrupt.h>
70#include <linux/init.h>
71#include <linux/delay.h>
72#include <linux/netdevice.h>
73#include <linux/etherdevice.h>
74#include <linux/skbuff.h>
Kumar Gala0bbaf062005-06-20 10:54:21 -050075#include <linux/if_vlan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#include <linux/spinlock.h>
77#include <linux/mm.h>
Grant Likelyfe192a42009-04-25 12:53:12 +000078#include <linux/of_mdio.h>
Andy Flemingb31a1d82008-12-16 15:29:15 -080079#include <linux/of_platform.h>
Kumar Gala0bbaf062005-06-20 10:54:21 -050080#include <linux/ip.h>
81#include <linux/tcp.h>
82#include <linux/udp.h>
Kumar Gala9c07b8842006-01-11 11:26:25 -080083#include <linux/in.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85#include <asm/io.h>
86#include <asm/irq.h>
87#include <asm/uaccess.h>
88#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070089#include <linux/dma-mapping.h>
90#include <linux/crc32.h>
Andy Flemingbb40dcb2005-09-23 22:54:21 -040091#include <linux/mii.h>
92#include <linux/phy.h>
Andy Flemingb31a1d82008-12-16 15:29:15 -080093#include <linux/phy_fixed.h>
94#include <linux/of.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96#include "gianfar.h"
Andy Fleming1577ece2009-02-04 16:42:12 -080097#include "fsl_pq_mdio.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99#define TX_TIMEOUT (1*HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100#undef BRIEF_GFAR_ERRORS
101#undef VERBOSE_GFAR_ERRORS
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103const char gfar_driver_name[] = "Gianfar Ethernet";
Andy Fleming7f7f5312005-11-11 12:38:59 -0600104const char gfar_driver_version[] = "1.3";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106static int gfar_enet_open(struct net_device *dev);
107static int gfar_start_xmit(struct sk_buff *skb, struct net_device *dev);
Sebastian Siewiorab939902008-08-19 21:12:45 +0200108static void gfar_reset_task(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109static void gfar_timeout(struct net_device *dev);
110static int gfar_close(struct net_device *dev);
Andy Fleming815b97c2008-04-22 17:18:29 -0500111struct sk_buff *gfar_new_skb(struct net_device *dev);
112static void gfar_new_rxbdp(struct net_device *dev, struct rxbd8 *bdp,
113 struct sk_buff *skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114static int gfar_set_mac_address(struct net_device *dev);
115static int gfar_change_mtu(struct net_device *dev, int new_mtu);
David Howells7d12e782006-10-05 14:55:46 +0100116static irqreturn_t gfar_error(int irq, void *dev_id);
117static irqreturn_t gfar_transmit(int irq, void *dev_id);
118static irqreturn_t gfar_interrupt(int irq, void *dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119static void adjust_link(struct net_device *dev);
120static void init_registers(struct net_device *dev);
121static int init_phy(struct net_device *dev);
Andy Flemingb31a1d82008-12-16 15:29:15 -0800122static int gfar_probe(struct of_device *ofdev,
123 const struct of_device_id *match);
124static int gfar_remove(struct of_device *ofdev);
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400125static void free_skb_resources(struct gfar_private *priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126static void gfar_set_multi(struct net_device *dev);
127static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr);
Kapil Junejad3c12872007-05-11 18:25:11 -0500128static void gfar_configure_serdes(struct net_device *dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700129static int gfar_poll(struct napi_struct *napi, int budget);
Vitaly Woolf2d71c22006-11-07 13:27:02 +0300130#ifdef CONFIG_NET_POLL_CONTROLLER
131static void gfar_netpoll(struct net_device *dev);
132#endif
Kumar Gala0bbaf062005-06-20 10:54:21 -0500133int gfar_clean_rx_ring(struct net_device *dev, int rx_work_limit);
Andy Flemingf162b9d2008-05-02 13:00:30 -0500134static int gfar_clean_tx_ring(struct net_device *dev);
Dai Haruki2c2db482008-12-16 15:31:15 -0800135static int gfar_process_frame(struct net_device *dev, struct sk_buff *skb,
136 int amount_pull);
Kumar Gala0bbaf062005-06-20 10:54:21 -0500137static void gfar_vlan_rx_register(struct net_device *netdev,
138 struct vlan_group *grp);
Andy Fleming7f7f5312005-11-11 12:38:59 -0600139void gfar_halt(struct net_device *dev);
Scott Woodd87eb122008-07-11 18:04:45 -0500140static void gfar_halt_nodisable(struct net_device *dev);
Andy Fleming7f7f5312005-11-11 12:38:59 -0600141void gfar_start(struct net_device *dev);
142static void gfar_clear_exact_match(struct net_device *dev);
143static void gfar_set_mac_for_addr(struct net_device *dev, int num, u8 *addr);
Andy Fleming26ccfc32009-03-10 12:58:28 +0000144static int gfar_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146MODULE_AUTHOR("Freescale Semiconductor, Inc");
147MODULE_DESCRIPTION("Gianfar Ethernet Driver");
148MODULE_LICENSE("GPL");
149
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000150static int gfar_alloc_skb_resources(struct net_device *ndev)
151{
152 struct txbd8 *txbdp;
153 struct rxbd8 *rxbdp;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000154 void *vaddr;
155 int i;
156 struct gfar_private *priv = netdev_priv(ndev);
157 struct device *dev = &priv->ofdev->dev;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000158
159 /* Allocate memory for the buffer descriptors */
160 vaddr = dma_alloc_coherent(dev, sizeof(*txbdp) * priv->tx_ring_size +
161 sizeof(*rxbdp) * priv->rx_ring_size,
Anton Vorontsov32c513b2009-10-12 06:00:36 +0000162 &priv->tx_bd_dma_base, GFP_KERNEL);
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000163 if (!vaddr) {
164 if (netif_msg_ifup(priv))
165 pr_err("%s: Could not allocate buffer descriptors!\n",
166 ndev->name);
167 return -ENOMEM;
168 }
169
170 priv->tx_bd_base = vaddr;
171
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000172 /* Start the rx descriptor ring where the tx ring leaves off */
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000173 vaddr = vaddr + sizeof(*txbdp) * priv->tx_ring_size;
174 priv->rx_bd_base = vaddr;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000175
176 /* Setup the skbuff rings */
177 priv->tx_skbuff = kmalloc(sizeof(*priv->tx_skbuff) *
178 priv->tx_ring_size, GFP_KERNEL);
179 if (!priv->tx_skbuff) {
180 if (netif_msg_ifup(priv))
181 pr_err("%s: Could not allocate tx_skbuff\n",
182 ndev->name);
183 goto cleanup;
184 }
185
186 for (i = 0; i < priv->tx_ring_size; i++)
187 priv->tx_skbuff[i] = NULL;
188
189 priv->rx_skbuff = kmalloc(sizeof(*priv->rx_skbuff) *
190 priv->rx_ring_size, GFP_KERNEL);
191 if (!priv->rx_skbuff) {
192 if (netif_msg_ifup(priv))
193 pr_err("%s: Could not allocate rx_skbuff\n",
194 ndev->name);
195 goto cleanup;
196 }
197
198 for (i = 0; i < priv->rx_ring_size; i++)
199 priv->rx_skbuff[i] = NULL;
200
201 /* Initialize some variables in our dev structure */
202 priv->num_txbdfree = priv->tx_ring_size;
203 priv->dirty_tx = priv->cur_tx = priv->tx_bd_base;
204 priv->cur_rx = priv->rx_bd_base;
205 priv->skb_curtx = priv->skb_dirtytx = 0;
206 priv->skb_currx = 0;
207
208 /* Initialize Transmit Descriptor Ring */
209 txbdp = priv->tx_bd_base;
210 for (i = 0; i < priv->tx_ring_size; i++) {
211 txbdp->lstatus = 0;
212 txbdp->bufPtr = 0;
213 txbdp++;
214 }
215
216 /* Set the last descriptor in the ring to indicate wrap */
217 txbdp--;
218 txbdp->status |= TXBD_WRAP;
219
220 rxbdp = priv->rx_bd_base;
221 for (i = 0; i < priv->rx_ring_size; i++) {
222 struct sk_buff *skb;
223
224 skb = gfar_new_skb(ndev);
225 if (!skb) {
226 pr_err("%s: Can't allocate RX buffers\n", ndev->name);
227 goto cleanup;
228 }
229
230 priv->rx_skbuff[i] = skb;
231
232 gfar_new_rxbdp(ndev, rxbdp, skb);
233
234 rxbdp++;
235 }
236
237 return 0;
238
239cleanup:
240 free_skb_resources(priv);
241 return -ENOMEM;
242}
243
244static void gfar_init_mac(struct net_device *ndev)
245{
246 struct gfar_private *priv = netdev_priv(ndev);
247 struct gfar __iomem *regs = priv->regs;
248 u32 rctrl = 0;
249 u32 tctrl = 0;
250 u32 attrs = 0;
251
Anton Vorontsov32c513b2009-10-12 06:00:36 +0000252 /* enet DMA only understands physical addresses */
253 gfar_write(&regs->tbase0, priv->tx_bd_dma_base);
254 gfar_write(&regs->rbase0, priv->tx_bd_dma_base +
255 sizeof(*priv->tx_bd_base) *
256 priv->tx_ring_size);
257
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000258 /* Configure the coalescing support */
259 gfar_write(&regs->txic, 0);
260 if (priv->txcoalescing)
261 gfar_write(&regs->txic, priv->txic);
262
263 gfar_write(&regs->rxic, 0);
264 if (priv->rxcoalescing)
265 gfar_write(&regs->rxic, priv->rxic);
266
267 if (priv->rx_csum_enable)
268 rctrl |= RCTRL_CHECKSUMMING;
269
270 if (priv->extended_hash) {
271 rctrl |= RCTRL_EXTHASH;
272
273 gfar_clear_exact_match(ndev);
274 rctrl |= RCTRL_EMEN;
275 }
276
277 if (priv->padding) {
278 rctrl &= ~RCTRL_PAL_MASK;
279 rctrl |= RCTRL_PADDING(priv->padding);
280 }
281
282 /* keep vlan related bits if it's enabled */
283 if (priv->vlgrp) {
284 rctrl |= RCTRL_VLEX | RCTRL_PRSDEP_INIT;
285 tctrl |= TCTRL_VLINS;
286 }
287
288 /* Init rctrl based on our settings */
289 gfar_write(&regs->rctrl, rctrl);
290
291 if (ndev->features & NETIF_F_IP_CSUM)
292 tctrl |= TCTRL_INIT_CSUM;
293
294 gfar_write(&regs->tctrl, tctrl);
295
296 /* Set the extraction length and index */
297 attrs = ATTRELI_EL(priv->rx_stash_size) |
298 ATTRELI_EI(priv->rx_stash_index);
299
300 gfar_write(&regs->attreli, attrs);
301
302 /* Start with defaults, and add stashing or locking
303 * depending on the approprate variables */
304 attrs = ATTR_INIT_SETTINGS;
305
306 if (priv->bd_stash_en)
307 attrs |= ATTR_BDSTASH;
308
309 if (priv->rx_stash_size != 0)
310 attrs |= ATTR_BUFSTASH;
311
312 gfar_write(&regs->attr, attrs);
313
314 gfar_write(&regs->fifo_tx_thr, priv->fifo_threshold);
315 gfar_write(&regs->fifo_tx_starve, priv->fifo_starve);
316 gfar_write(&regs->fifo_tx_starve_shutoff, priv->fifo_starve_off);
317}
318
Andy Fleming26ccfc32009-03-10 12:58:28 +0000319static const struct net_device_ops gfar_netdev_ops = {
320 .ndo_open = gfar_enet_open,
321 .ndo_start_xmit = gfar_start_xmit,
322 .ndo_stop = gfar_close,
323 .ndo_change_mtu = gfar_change_mtu,
324 .ndo_set_multicast_list = gfar_set_multi,
325 .ndo_tx_timeout = gfar_timeout,
326 .ndo_do_ioctl = gfar_ioctl,
327 .ndo_vlan_rx_register = gfar_vlan_rx_register,
Ben Hutchings240c1022009-07-09 17:54:35 +0000328 .ndo_set_mac_address = eth_mac_addr,
329 .ndo_validate_addr = eth_validate_addr,
Andy Fleming26ccfc32009-03-10 12:58:28 +0000330#ifdef CONFIG_NET_POLL_CONTROLLER
331 .ndo_poll_controller = gfar_netpoll,
332#endif
333};
334
Andy Fleming7f7f5312005-11-11 12:38:59 -0600335/* Returns 1 if incoming frames use an FCB */
336static inline int gfar_uses_fcb(struct gfar_private *priv)
Kumar Gala0bbaf062005-06-20 10:54:21 -0500337{
Dai Haruki77ecaf22008-12-16 15:30:48 -0800338 return priv->vlgrp || priv->rx_csum_enable;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500339}
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400340
Andy Flemingb31a1d82008-12-16 15:29:15 -0800341static int gfar_of_init(struct net_device *dev)
342{
Andy Flemingb31a1d82008-12-16 15:29:15 -0800343 const char *model;
344 const char *ctype;
345 const void *mac_addr;
Andy Flemingb31a1d82008-12-16 15:29:15 -0800346 u64 addr, size;
347 int err = 0;
348 struct gfar_private *priv = netdev_priv(dev);
349 struct device_node *np = priv->node;
Andy Fleming4d7902f2009-02-04 16:43:44 -0800350 const u32 *stash;
351 const u32 *stash_len;
352 const u32 *stash_idx;
Andy Flemingb31a1d82008-12-16 15:29:15 -0800353
354 if (!np || !of_device_is_available(np))
355 return -ENODEV;
356
357 /* get a pointer to the register memory */
358 addr = of_translate_address(np, of_get_address(np, 0, &size, NULL));
359 priv->regs = ioremap(addr, size);
360
361 if (priv->regs == NULL)
362 return -ENOMEM;
363
364 priv->interruptTransmit = irq_of_parse_and_map(np, 0);
365
366 model = of_get_property(np, "model", NULL);
367
368 /* If we aren't the FEC we have multiple interrupts */
369 if (model && strcasecmp(model, "FEC")) {
370 priv->interruptReceive = irq_of_parse_and_map(np, 1);
371
372 priv->interruptError = irq_of_parse_and_map(np, 2);
373
374 if (priv->interruptTransmit < 0 ||
375 priv->interruptReceive < 0 ||
376 priv->interruptError < 0) {
377 err = -EINVAL;
378 goto err_out;
379 }
380 }
381
Andy Fleming4d7902f2009-02-04 16:43:44 -0800382 stash = of_get_property(np, "bd-stash", NULL);
383
384 if(stash) {
385 priv->device_flags |= FSL_GIANFAR_DEV_HAS_BD_STASHING;
386 priv->bd_stash_en = 1;
387 }
388
389 stash_len = of_get_property(np, "rx-stash-len", NULL);
390
391 if (stash_len)
392 priv->rx_stash_size = *stash_len;
393
394 stash_idx = of_get_property(np, "rx-stash-idx", NULL);
395
396 if (stash_idx)
397 priv->rx_stash_index = *stash_idx;
398
399 if (stash_len || stash_idx)
400 priv->device_flags |= FSL_GIANFAR_DEV_HAS_BUF_STASHING;
401
Andy Flemingb31a1d82008-12-16 15:29:15 -0800402 mac_addr = of_get_mac_address(np);
403 if (mac_addr)
404 memcpy(dev->dev_addr, mac_addr, MAC_ADDR_LEN);
405
406 if (model && !strcasecmp(model, "TSEC"))
407 priv->device_flags =
408 FSL_GIANFAR_DEV_HAS_GIGABIT |
409 FSL_GIANFAR_DEV_HAS_COALESCE |
410 FSL_GIANFAR_DEV_HAS_RMON |
411 FSL_GIANFAR_DEV_HAS_MULTI_INTR;
412 if (model && !strcasecmp(model, "eTSEC"))
413 priv->device_flags =
414 FSL_GIANFAR_DEV_HAS_GIGABIT |
415 FSL_GIANFAR_DEV_HAS_COALESCE |
416 FSL_GIANFAR_DEV_HAS_RMON |
417 FSL_GIANFAR_DEV_HAS_MULTI_INTR |
Dai Haruki2c2db482008-12-16 15:31:15 -0800418 FSL_GIANFAR_DEV_HAS_PADDING |
Andy Flemingb31a1d82008-12-16 15:29:15 -0800419 FSL_GIANFAR_DEV_HAS_CSUM |
420 FSL_GIANFAR_DEV_HAS_VLAN |
421 FSL_GIANFAR_DEV_HAS_MAGIC_PACKET |
422 FSL_GIANFAR_DEV_HAS_EXTENDED_HASH;
423
424 ctype = of_get_property(np, "phy-connection-type", NULL);
425
426 /* We only care about rgmii-id. The rest are autodetected */
427 if (ctype && !strcmp(ctype, "rgmii-id"))
428 priv->interface = PHY_INTERFACE_MODE_RGMII_ID;
429 else
430 priv->interface = PHY_INTERFACE_MODE_MII;
431
432 if (of_get_property(np, "fsl,magic-packet", NULL))
433 priv->device_flags |= FSL_GIANFAR_DEV_HAS_MAGIC_PACKET;
434
Grant Likelyfe192a42009-04-25 12:53:12 +0000435 priv->phy_node = of_parse_phandle(np, "phy-handle", 0);
Andy Flemingb31a1d82008-12-16 15:29:15 -0800436
437 /* Find the TBI PHY. If it's not there, we don't support SGMII */
Grant Likelyfe192a42009-04-25 12:53:12 +0000438 priv->tbi_node = of_parse_phandle(np, "tbi-handle", 0);
Andy Flemingb31a1d82008-12-16 15:29:15 -0800439
440 return 0;
441
442err_out:
443 iounmap(priv->regs);
444 return err;
445}
446
Clifford Wolf0faac9f2009-01-09 10:23:11 +0000447/* Ioctl MII Interface */
448static int gfar_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
449{
450 struct gfar_private *priv = netdev_priv(dev);
451
452 if (!netif_running(dev))
453 return -EINVAL;
454
455 if (!priv->phydev)
456 return -ENODEV;
457
458 return phy_mii_ioctl(priv->phydev, if_mii(rq), cmd);
459}
460
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400461/* Set up the ethernet device structure, private data,
462 * and anything else we need before we start */
Andy Flemingb31a1d82008-12-16 15:29:15 -0800463static int gfar_probe(struct of_device *ofdev,
464 const struct of_device_id *match)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
466 u32 tempval;
467 struct net_device *dev = NULL;
468 struct gfar_private *priv = NULL;
Dai Harukic50a5d92008-12-17 16:51:32 -0800469 int err = 0;
470 int len_devname;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
472 /* Create an ethernet device instance */
473 dev = alloc_etherdev(sizeof (*priv));
474
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400475 if (NULL == dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 return -ENOMEM;
477
478 priv = netdev_priv(dev);
Kumar Gala48268572009-03-18 23:28:22 -0700479 priv->ndev = dev;
480 priv->ofdev = ofdev;
Andy Flemingb31a1d82008-12-16 15:29:15 -0800481 priv->node = ofdev->node;
Kumar Gala48268572009-03-18 23:28:22 -0700482 SET_NETDEV_DEV(dev, &ofdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
Andy Flemingb31a1d82008-12-16 15:29:15 -0800484 err = gfar_of_init(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Andy Flemingb31a1d82008-12-16 15:29:15 -0800486 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 goto regs_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Andy Flemingfef61082006-04-20 16:44:29 -0500489 spin_lock_init(&priv->txlock);
490 spin_lock_init(&priv->rxlock);
Scott Woodd87eb122008-07-11 18:04:45 -0500491 spin_lock_init(&priv->bflock);
Sebastian Siewiorab939902008-08-19 21:12:45 +0200492 INIT_WORK(&priv->reset_task, gfar_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Andy Flemingb31a1d82008-12-16 15:29:15 -0800494 dev_set_drvdata(&ofdev->dev, priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
496 /* Stop the DMA engine now, in case it was running before */
497 /* (The firmware could have used it, and left it running). */
Andy Fleming257d9382008-12-16 15:25:45 -0800498 gfar_halt(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
500 /* Reset MAC layer */
501 gfar_write(&priv->regs->maccfg1, MACCFG1_SOFT_RESET);
502
Andy Flemingb98ac702009-02-04 16:38:05 -0800503 /* We need to delay at least 3 TX clocks */
504 udelay(2);
505
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 tempval = (MACCFG1_TX_FLOW | MACCFG1_RX_FLOW);
507 gfar_write(&priv->regs->maccfg1, tempval);
508
509 /* Initialize MACCFG2. */
510 gfar_write(&priv->regs->maccfg2, MACCFG2_INIT_SETTINGS);
511
512 /* Initialize ECNTRL */
513 gfar_write(&priv->regs->ecntrl, ECNTRL_INIT_SETTINGS);
514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 /* Set the dev->base_addr to the gfar reg region */
516 dev->base_addr = (unsigned long) (priv->regs);
517
Andy Flemingb31a1d82008-12-16 15:29:15 -0800518 SET_NETDEV_DEV(dev, &ofdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
520 /* Fill in the dev structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 dev->watchdog_timeo = TX_TIMEOUT;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700522 netif_napi_add(dev, &priv->napi, gfar_poll, GFAR_DEV_WEIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 dev->mtu = 1500;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
Andy Fleming26ccfc32009-03-10 12:58:28 +0000525 dev->netdev_ops = &gfar_netdev_ops;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500526 dev->ethtool_ops = &gfar_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Andy Flemingb31a1d82008-12-16 15:29:15 -0800528 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_CSUM) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500529 priv->rx_csum_enable = 1;
Dai Haruki4669bc92008-12-17 16:51:04 -0800530 dev->features |= NETIF_F_IP_CSUM | NETIF_F_SG | NETIF_F_HIGHDMA;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500531 } else
532 priv->rx_csum_enable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Kumar Gala0bbaf062005-06-20 10:54:21 -0500534 priv->vlgrp = NULL;
535
Andy Fleming26ccfc32009-03-10 12:58:28 +0000536 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_VLAN)
Kumar Gala0bbaf062005-06-20 10:54:21 -0500537 dev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500538
Andy Flemingb31a1d82008-12-16 15:29:15 -0800539 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_EXTENDED_HASH) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500540 priv->extended_hash = 1;
541 priv->hash_width = 9;
542
543 priv->hash_regs[0] = &priv->regs->igaddr0;
544 priv->hash_regs[1] = &priv->regs->igaddr1;
545 priv->hash_regs[2] = &priv->regs->igaddr2;
546 priv->hash_regs[3] = &priv->regs->igaddr3;
547 priv->hash_regs[4] = &priv->regs->igaddr4;
548 priv->hash_regs[5] = &priv->regs->igaddr5;
549 priv->hash_regs[6] = &priv->regs->igaddr6;
550 priv->hash_regs[7] = &priv->regs->igaddr7;
551 priv->hash_regs[8] = &priv->regs->gaddr0;
552 priv->hash_regs[9] = &priv->regs->gaddr1;
553 priv->hash_regs[10] = &priv->regs->gaddr2;
554 priv->hash_regs[11] = &priv->regs->gaddr3;
555 priv->hash_regs[12] = &priv->regs->gaddr4;
556 priv->hash_regs[13] = &priv->regs->gaddr5;
557 priv->hash_regs[14] = &priv->regs->gaddr6;
558 priv->hash_regs[15] = &priv->regs->gaddr7;
559
560 } else {
561 priv->extended_hash = 0;
562 priv->hash_width = 8;
563
564 priv->hash_regs[0] = &priv->regs->gaddr0;
Andy Fleming1577ece2009-02-04 16:42:12 -0800565 priv->hash_regs[1] = &priv->regs->gaddr1;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500566 priv->hash_regs[2] = &priv->regs->gaddr2;
567 priv->hash_regs[3] = &priv->regs->gaddr3;
568 priv->hash_regs[4] = &priv->regs->gaddr4;
569 priv->hash_regs[5] = &priv->regs->gaddr5;
570 priv->hash_regs[6] = &priv->regs->gaddr6;
571 priv->hash_regs[7] = &priv->regs->gaddr7;
572 }
573
Andy Flemingb31a1d82008-12-16 15:29:15 -0800574 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_PADDING)
Kumar Gala0bbaf062005-06-20 10:54:21 -0500575 priv->padding = DEFAULT_PADDING;
576 else
577 priv->padding = 0;
578
Kumar Gala0bbaf062005-06-20 10:54:21 -0500579 if (dev->features & NETIF_F_IP_CSUM)
580 dev->hard_header_len += GMAC_FCB_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
582 priv->rx_buffer_size = DEFAULT_RX_BUFFER_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 priv->tx_ring_size = DEFAULT_TX_RING_SIZE;
584 priv->rx_ring_size = DEFAULT_RX_RING_SIZE;
Dai Haruki4669bc92008-12-17 16:51:04 -0800585 priv->num_txbdfree = DEFAULT_TX_RING_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
587 priv->txcoalescing = DEFAULT_TX_COALESCE;
Dai Harukib46a8452008-12-16 15:29:52 -0800588 priv->txic = DEFAULT_TXIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 priv->rxcoalescing = DEFAULT_RX_COALESCE;
Dai Harukib46a8452008-12-16 15:29:52 -0800590 priv->rxic = DEFAULT_RXIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
Kumar Gala0bbaf062005-06-20 10:54:21 -0500592 /* Enable most messages by default */
593 priv->msg_enable = (NETIF_MSG_IFUP << 1 ) - 1;
594
Trent Piephod3eab822008-10-02 11:12:24 +0000595 /* Carrier starts down, phylib will bring it up */
596 netif_carrier_off(dev);
597
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 err = register_netdev(dev);
599
600 if (err) {
601 printk(KERN_ERR "%s: Cannot register net device, aborting.\n",
602 dev->name);
603 goto register_fail;
604 }
605
Anton Vorontsov2884e5c2009-02-01 00:52:34 -0800606 device_init_wakeup(&dev->dev,
607 priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
608
Dai Harukic50a5d92008-12-17 16:51:32 -0800609 /* fill out IRQ number and name fields */
610 len_devname = strlen(dev->name);
611 strncpy(&priv->int_name_tx[0], dev->name, len_devname);
612 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
613 strncpy(&priv->int_name_tx[len_devname],
614 "_tx", sizeof("_tx") + 1);
615
616 strncpy(&priv->int_name_rx[0], dev->name, len_devname);
617 strncpy(&priv->int_name_rx[len_devname],
618 "_rx", sizeof("_rx") + 1);
619
620 strncpy(&priv->int_name_er[0], dev->name, len_devname);
621 strncpy(&priv->int_name_er[len_devname],
622 "_er", sizeof("_er") + 1);
623 } else
624 priv->int_name_tx[len_devname] = '\0';
625
Andy Fleming7f7f5312005-11-11 12:38:59 -0600626 /* Create all the sysfs files */
627 gfar_init_sysfs(dev);
628
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 /* Print out the device info */
Johannes Berge1749612008-10-27 15:59:26 -0700630 printk(KERN_INFO DEVICE_NAME "%pM\n", dev->name, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
632 /* Even more device info helps when determining which kernel */
Andy Fleming7f7f5312005-11-11 12:38:59 -0600633 /* provided which set of benchmarks. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 printk(KERN_INFO "%s: Running with NAPI enabled\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 printk(KERN_INFO "%s: %d/%d RX/TX BD ring size\n",
636 dev->name, priv->rx_ring_size, priv->tx_ring_size);
637
638 return 0;
639
640register_fail:
Kumar Galacc8c6e32006-02-01 15:18:03 -0600641 iounmap(priv->regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642regs_fail:
Grant Likelyfe192a42009-04-25 12:53:12 +0000643 if (priv->phy_node)
644 of_node_put(priv->phy_node);
645 if (priv->tbi_node)
646 of_node_put(priv->tbi_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 free_netdev(dev);
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400648 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649}
650
Andy Flemingb31a1d82008-12-16 15:29:15 -0800651static int gfar_remove(struct of_device *ofdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652{
Andy Flemingb31a1d82008-12-16 15:29:15 -0800653 struct gfar_private *priv = dev_get_drvdata(&ofdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Grant Likelyfe192a42009-04-25 12:53:12 +0000655 if (priv->phy_node)
656 of_node_put(priv->phy_node);
657 if (priv->tbi_node)
658 of_node_put(priv->tbi_node);
659
Andy Flemingb31a1d82008-12-16 15:29:15 -0800660 dev_set_drvdata(&ofdev->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
David S. Millerd9d8e042009-09-06 01:41:02 -0700662 unregister_netdev(priv->ndev);
Kumar Galacc8c6e32006-02-01 15:18:03 -0600663 iounmap(priv->regs);
Kumar Gala48268572009-03-18 23:28:22 -0700664 free_netdev(priv->ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
666 return 0;
667}
668
Scott Woodd87eb122008-07-11 18:04:45 -0500669#ifdef CONFIG_PM
Andy Flemingb31a1d82008-12-16 15:29:15 -0800670static int gfar_suspend(struct of_device *ofdev, pm_message_t state)
Scott Woodd87eb122008-07-11 18:04:45 -0500671{
Andy Flemingb31a1d82008-12-16 15:29:15 -0800672 struct gfar_private *priv = dev_get_drvdata(&ofdev->dev);
Anton Vorontsov29ded5f2009-03-21 13:27:55 -0700673 struct net_device *dev = priv->ndev;
Scott Woodd87eb122008-07-11 18:04:45 -0500674 unsigned long flags;
675 u32 tempval;
676
677 int magic_packet = priv->wol_en &&
Andy Flemingb31a1d82008-12-16 15:29:15 -0800678 (priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
Scott Woodd87eb122008-07-11 18:04:45 -0500679
680 netif_device_detach(dev);
681
682 if (netif_running(dev)) {
683 spin_lock_irqsave(&priv->txlock, flags);
684 spin_lock(&priv->rxlock);
685
686 gfar_halt_nodisable(dev);
687
688 /* Disable Tx, and Rx if wake-on-LAN is disabled. */
689 tempval = gfar_read(&priv->regs->maccfg1);
690
691 tempval &= ~MACCFG1_TX_EN;
692
693 if (!magic_packet)
694 tempval &= ~MACCFG1_RX_EN;
695
696 gfar_write(&priv->regs->maccfg1, tempval);
697
698 spin_unlock(&priv->rxlock);
699 spin_unlock_irqrestore(&priv->txlock, flags);
700
Scott Woodd87eb122008-07-11 18:04:45 -0500701 napi_disable(&priv->napi);
Scott Woodd87eb122008-07-11 18:04:45 -0500702
703 if (magic_packet) {
704 /* Enable interrupt on Magic Packet */
705 gfar_write(&priv->regs->imask, IMASK_MAG);
706
707 /* Enable Magic Packet mode */
708 tempval = gfar_read(&priv->regs->maccfg2);
709 tempval |= MACCFG2_MPEN;
710 gfar_write(&priv->regs->maccfg2, tempval);
711 } else {
712 phy_stop(priv->phydev);
713 }
714 }
715
716 return 0;
717}
718
Andy Flemingb31a1d82008-12-16 15:29:15 -0800719static int gfar_resume(struct of_device *ofdev)
Scott Woodd87eb122008-07-11 18:04:45 -0500720{
Andy Flemingb31a1d82008-12-16 15:29:15 -0800721 struct gfar_private *priv = dev_get_drvdata(&ofdev->dev);
Anton Vorontsov29ded5f2009-03-21 13:27:55 -0700722 struct net_device *dev = priv->ndev;
Scott Woodd87eb122008-07-11 18:04:45 -0500723 unsigned long flags;
724 u32 tempval;
725 int magic_packet = priv->wol_en &&
Andy Flemingb31a1d82008-12-16 15:29:15 -0800726 (priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
Scott Woodd87eb122008-07-11 18:04:45 -0500727
728 if (!netif_running(dev)) {
729 netif_device_attach(dev);
730 return 0;
731 }
732
733 if (!magic_packet && priv->phydev)
734 phy_start(priv->phydev);
735
736 /* Disable Magic Packet mode, in case something
737 * else woke us up.
738 */
739
740 spin_lock_irqsave(&priv->txlock, flags);
741 spin_lock(&priv->rxlock);
742
743 tempval = gfar_read(&priv->regs->maccfg2);
744 tempval &= ~MACCFG2_MPEN;
745 gfar_write(&priv->regs->maccfg2, tempval);
746
747 gfar_start(dev);
748
749 spin_unlock(&priv->rxlock);
750 spin_unlock_irqrestore(&priv->txlock, flags);
751
752 netif_device_attach(dev);
753
Scott Woodd87eb122008-07-11 18:04:45 -0500754 napi_enable(&priv->napi);
Scott Woodd87eb122008-07-11 18:04:45 -0500755
756 return 0;
757}
758#else
759#define gfar_suspend NULL
760#define gfar_resume NULL
761#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600763/* Reads the controller's registers to determine what interface
764 * connects it to the PHY.
765 */
766static phy_interface_t gfar_get_interface(struct net_device *dev)
767{
768 struct gfar_private *priv = netdev_priv(dev);
769 u32 ecntrl = gfar_read(&priv->regs->ecntrl);
770
771 if (ecntrl & ECNTRL_SGMII_MODE)
772 return PHY_INTERFACE_MODE_SGMII;
773
774 if (ecntrl & ECNTRL_TBI_MODE) {
775 if (ecntrl & ECNTRL_REDUCED_MODE)
776 return PHY_INTERFACE_MODE_RTBI;
777 else
778 return PHY_INTERFACE_MODE_TBI;
779 }
780
781 if (ecntrl & ECNTRL_REDUCED_MODE) {
782 if (ecntrl & ECNTRL_REDUCED_MII_MODE)
783 return PHY_INTERFACE_MODE_RMII;
Andy Fleming7132ab72007-07-11 11:43:07 -0500784 else {
Andy Flemingb31a1d82008-12-16 15:29:15 -0800785 phy_interface_t interface = priv->interface;
Andy Fleming7132ab72007-07-11 11:43:07 -0500786
787 /*
788 * This isn't autodetected right now, so it must
789 * be set by the device tree or platform code.
790 */
791 if (interface == PHY_INTERFACE_MODE_RGMII_ID)
792 return PHY_INTERFACE_MODE_RGMII_ID;
793
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600794 return PHY_INTERFACE_MODE_RGMII;
Andy Fleming7132ab72007-07-11 11:43:07 -0500795 }
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600796 }
797
Andy Flemingb31a1d82008-12-16 15:29:15 -0800798 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT)
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600799 return PHY_INTERFACE_MODE_GMII;
800
801 return PHY_INTERFACE_MODE_MII;
802}
803
804
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400805/* Initializes driver's PHY state, and attaches to the PHY.
806 * Returns 0 on success.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 */
808static int init_phy(struct net_device *dev)
809{
810 struct gfar_private *priv = netdev_priv(dev);
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400811 uint gigabit_support =
Andy Flemingb31a1d82008-12-16 15:29:15 -0800812 priv->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT ?
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400813 SUPPORTED_1000baseT_Full : 0;
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600814 phy_interface_t interface;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
816 priv->oldlink = 0;
817 priv->oldspeed = 0;
818 priv->oldduplex = -1;
819
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600820 interface = gfar_get_interface(dev);
821
Anton Vorontsov1db780f2009-07-16 21:31:42 +0000822 priv->phydev = of_phy_connect(dev, priv->phy_node, &adjust_link, 0,
823 interface);
824 if (!priv->phydev)
825 priv->phydev = of_phy_connect_fixed_link(dev, &adjust_link,
826 interface);
827 if (!priv->phydev) {
828 dev_err(&dev->dev, "could not attach to PHY\n");
829 return -ENODEV;
Grant Likelyfe192a42009-04-25 12:53:12 +0000830 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
Kapil Junejad3c12872007-05-11 18:25:11 -0500832 if (interface == PHY_INTERFACE_MODE_SGMII)
833 gfar_configure_serdes(dev);
834
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400835 /* Remove any features not supported by the controller */
Grant Likelyfe192a42009-04-25 12:53:12 +0000836 priv->phydev->supported &= (GFAR_SUPPORTED | gigabit_support);
837 priv->phydev->advertising = priv->phydev->supported;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
839 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840}
841
Paul Gortmakerd0313582008-04-17 00:08:10 -0400842/*
843 * Initialize TBI PHY interface for communicating with the
844 * SERDES lynx PHY on the chip. We communicate with this PHY
845 * through the MDIO bus on each controller, treating it as a
846 * "normal" PHY at the address found in the TBIPA register. We assume
847 * that the TBIPA register is valid. Either the MDIO bus code will set
848 * it to a value that doesn't conflict with other PHYs on the bus, or the
849 * value doesn't matter, as there are no other PHYs on the bus.
850 */
Kapil Junejad3c12872007-05-11 18:25:11 -0500851static void gfar_configure_serdes(struct net_device *dev)
852{
853 struct gfar_private *priv = netdev_priv(dev);
Grant Likelyfe192a42009-04-25 12:53:12 +0000854 struct phy_device *tbiphy;
Trent Piephoc1324192008-10-30 18:17:06 -0700855
Grant Likelyfe192a42009-04-25 12:53:12 +0000856 if (!priv->tbi_node) {
857 dev_warn(&dev->dev, "error: SGMII mode requires that the "
858 "device tree specify a tbi-handle\n");
859 return;
860 }
861
862 tbiphy = of_phy_find_device(priv->tbi_node);
863 if (!tbiphy) {
864 dev_err(&dev->dev, "error: Could not get TBI device\n");
Andy Flemingb31a1d82008-12-16 15:29:15 -0800865 return;
866 }
Kapil Junejad3c12872007-05-11 18:25:11 -0500867
Andy Flemingb31a1d82008-12-16 15:29:15 -0800868 /*
869 * If the link is already up, we must already be ok, and don't need to
Trent Piephobdb59f92008-10-30 18:17:07 -0700870 * configure and reset the TBI<->SerDes link. Maybe U-Boot configured
871 * everything for us? Resetting it takes the link down and requires
872 * several seconds for it to come back.
873 */
Grant Likelyfe192a42009-04-25 12:53:12 +0000874 if (phy_read(tbiphy, MII_BMSR) & BMSR_LSTATUS)
Andy Flemingb31a1d82008-12-16 15:29:15 -0800875 return;
Kapil Junejad3c12872007-05-11 18:25:11 -0500876
Paul Gortmakerd0313582008-04-17 00:08:10 -0400877 /* Single clk mode, mii mode off(for serdes communication) */
Grant Likelyfe192a42009-04-25 12:53:12 +0000878 phy_write(tbiphy, MII_TBICON, TBICON_CLK_SELECT);
Kapil Junejad3c12872007-05-11 18:25:11 -0500879
Grant Likelyfe192a42009-04-25 12:53:12 +0000880 phy_write(tbiphy, MII_ADVERTISE,
Kapil Junejad3c12872007-05-11 18:25:11 -0500881 ADVERTISE_1000XFULL | ADVERTISE_1000XPAUSE |
882 ADVERTISE_1000XPSE_ASYM);
883
Grant Likelyfe192a42009-04-25 12:53:12 +0000884 phy_write(tbiphy, MII_BMCR, BMCR_ANENABLE |
Kapil Junejad3c12872007-05-11 18:25:11 -0500885 BMCR_ANRESTART | BMCR_FULLDPLX | BMCR_SPEED1000);
886}
887
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888static void init_registers(struct net_device *dev)
889{
890 struct gfar_private *priv = netdev_priv(dev);
891
892 /* Clear IEVENT */
893 gfar_write(&priv->regs->ievent, IEVENT_INIT_CLEAR);
894
895 /* Initialize IMASK */
896 gfar_write(&priv->regs->imask, IMASK_INIT_CLEAR);
897
898 /* Init hash registers to zero */
Kumar Gala0bbaf062005-06-20 10:54:21 -0500899 gfar_write(&priv->regs->igaddr0, 0);
900 gfar_write(&priv->regs->igaddr1, 0);
901 gfar_write(&priv->regs->igaddr2, 0);
902 gfar_write(&priv->regs->igaddr3, 0);
903 gfar_write(&priv->regs->igaddr4, 0);
904 gfar_write(&priv->regs->igaddr5, 0);
905 gfar_write(&priv->regs->igaddr6, 0);
906 gfar_write(&priv->regs->igaddr7, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
908 gfar_write(&priv->regs->gaddr0, 0);
909 gfar_write(&priv->regs->gaddr1, 0);
910 gfar_write(&priv->regs->gaddr2, 0);
911 gfar_write(&priv->regs->gaddr3, 0);
912 gfar_write(&priv->regs->gaddr4, 0);
913 gfar_write(&priv->regs->gaddr5, 0);
914 gfar_write(&priv->regs->gaddr6, 0);
915 gfar_write(&priv->regs->gaddr7, 0);
916
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 /* Zero out the rmon mib registers if it has them */
Andy Flemingb31a1d82008-12-16 15:29:15 -0800918 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_RMON) {
Kumar Galacc8c6e32006-02-01 15:18:03 -0600919 memset_io(&(priv->regs->rmon), 0, sizeof (struct rmon_mib));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
921 /* Mask off the CAM interrupts */
922 gfar_write(&priv->regs->rmon.cam1, 0xffffffff);
923 gfar_write(&priv->regs->rmon.cam2, 0xffffffff);
924 }
925
926 /* Initialize the max receive buffer length */
927 gfar_write(&priv->regs->mrblr, priv->rx_buffer_size);
928
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 /* Initialize the Minimum Frame Length Register */
930 gfar_write(&priv->regs->minflr, MINFLR_INIT_SETTINGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931}
932
Kumar Gala0bbaf062005-06-20 10:54:21 -0500933
934/* Halt the receive and transmit queues */
Scott Woodd87eb122008-07-11 18:04:45 -0500935static void gfar_halt_nodisable(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936{
937 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -0600938 struct gfar __iomem *regs = priv->regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 u32 tempval;
940
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 /* Mask all interrupts */
942 gfar_write(&regs->imask, IMASK_INIT_CLEAR);
943
944 /* Clear all interrupts */
945 gfar_write(&regs->ievent, IEVENT_INIT_CLEAR);
946
947 /* Stop the DMA, and wait for it to stop */
948 tempval = gfar_read(&priv->regs->dmactrl);
949 if ((tempval & (DMACTRL_GRS | DMACTRL_GTS))
950 != (DMACTRL_GRS | DMACTRL_GTS)) {
951 tempval |= (DMACTRL_GRS | DMACTRL_GTS);
952 gfar_write(&priv->regs->dmactrl, tempval);
953
954 while (!(gfar_read(&priv->regs->ievent) &
955 (IEVENT_GRSC | IEVENT_GTSC)))
956 cpu_relax();
957 }
Scott Woodd87eb122008-07-11 18:04:45 -0500958}
Scott Woodd87eb122008-07-11 18:04:45 -0500959
960/* Halt the receive and transmit queues */
961void gfar_halt(struct net_device *dev)
962{
963 struct gfar_private *priv = netdev_priv(dev);
964 struct gfar __iomem *regs = priv->regs;
965 u32 tempval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
Scott Wood2a54adc2008-08-12 15:10:46 -0500967 gfar_halt_nodisable(dev);
968
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 /* Disable Rx and Tx */
970 tempval = gfar_read(&regs->maccfg1);
971 tempval &= ~(MACCFG1_RX_EN | MACCFG1_TX_EN);
972 gfar_write(&regs->maccfg1, tempval);
Kumar Gala0bbaf062005-06-20 10:54:21 -0500973}
974
975void stop_gfar(struct net_device *dev)
976{
977 struct gfar_private *priv = netdev_priv(dev);
Kumar Gala0bbaf062005-06-20 10:54:21 -0500978 unsigned long flags;
979
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400980 phy_stop(priv->phydev);
981
Kumar Gala0bbaf062005-06-20 10:54:21 -0500982 /* Lock it down */
Andy Flemingfef61082006-04-20 16:44:29 -0500983 spin_lock_irqsave(&priv->txlock, flags);
984 spin_lock(&priv->rxlock);
Kumar Gala0bbaf062005-06-20 10:54:21 -0500985
Kumar Gala0bbaf062005-06-20 10:54:21 -0500986 gfar_halt(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
Andy Flemingfef61082006-04-20 16:44:29 -0500988 spin_unlock(&priv->rxlock);
989 spin_unlock_irqrestore(&priv->txlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
991 /* Free the IRQs */
Andy Flemingb31a1d82008-12-16 15:29:15 -0800992 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 free_irq(priv->interruptError, dev);
994 free_irq(priv->interruptTransmit, dev);
995 free_irq(priv->interruptReceive, dev);
996 } else {
Andy Fleming1577ece2009-02-04 16:42:12 -0800997 free_irq(priv->interruptTransmit, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 }
999
1000 free_skb_resources(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001}
1002
1003/* If there are any tx skbs or rx skbs still around, free them.
1004 * Then free tx_skbuff and rx_skbuff */
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001005static void free_skb_resources(struct gfar_private *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006{
Anton Vorontsove69edd22009-10-12 06:00:30 +00001007 struct device *dev = &priv->ofdev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 struct rxbd8 *rxbdp;
1009 struct txbd8 *txbdp;
Dai Haruki4669bc92008-12-17 16:51:04 -08001010 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
1012 /* Go through all the buffer descriptors and free their data buffers */
1013 txbdp = priv->tx_bd_base;
1014
Anton Vorontsove69edd22009-10-12 06:00:30 +00001015 if (!priv->tx_skbuff)
1016 goto skip_tx_skbuff;
1017
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 for (i = 0; i < priv->tx_ring_size; i++) {
Dai Haruki4669bc92008-12-17 16:51:04 -08001019 if (!priv->tx_skbuff[i])
1020 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
Kumar Gala48268572009-03-18 23:28:22 -07001022 dma_unmap_single(&priv->ofdev->dev, txbdp->bufPtr,
Dai Haruki4669bc92008-12-17 16:51:04 -08001023 txbdp->length, DMA_TO_DEVICE);
1024 txbdp->lstatus = 0;
1025 for (j = 0; j < skb_shinfo(priv->tx_skbuff[i])->nr_frags; j++) {
1026 txbdp++;
Kumar Gala48268572009-03-18 23:28:22 -07001027 dma_unmap_page(&priv->ofdev->dev, txbdp->bufPtr,
Dai Haruki4669bc92008-12-17 16:51:04 -08001028 txbdp->length, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 }
Andy Flemingad5da7a2008-05-07 13:20:55 -05001030 txbdp++;
Dai Haruki4669bc92008-12-17 16:51:04 -08001031 dev_kfree_skb_any(priv->tx_skbuff[i]);
1032 priv->tx_skbuff[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 }
1034
1035 kfree(priv->tx_skbuff);
Anton Vorontsove69edd22009-10-12 06:00:30 +00001036skip_tx_skbuff:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
1038 rxbdp = priv->rx_bd_base;
1039
Anton Vorontsove69edd22009-10-12 06:00:30 +00001040 if (!priv->rx_skbuff)
1041 goto skip_rx_skbuff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
Anton Vorontsove69edd22009-10-12 06:00:30 +00001043 for (i = 0; i < priv->rx_ring_size; i++) {
1044 if (priv->rx_skbuff[i]) {
1045 dma_unmap_single(&priv->ofdev->dev, rxbdp->bufPtr,
1046 priv->rx_buffer_size,
1047 DMA_FROM_DEVICE);
1048 dev_kfree_skb_any(priv->rx_skbuff[i]);
1049 priv->rx_skbuff[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 }
1051
Anton Vorontsove69edd22009-10-12 06:00:30 +00001052 rxbdp->lstatus = 0;
1053 rxbdp->bufPtr = 0;
1054 rxbdp++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 }
Anton Vorontsove69edd22009-10-12 06:00:30 +00001056
1057 kfree(priv->rx_skbuff);
1058skip_rx_skbuff:
1059
1060 dma_free_coherent(dev, sizeof(*txbdp) * priv->tx_ring_size +
1061 sizeof(*rxbdp) * priv->rx_ring_size,
Anton Vorontsov32c513b2009-10-12 06:00:36 +00001062 priv->tx_bd_base, priv->tx_bd_dma_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063}
1064
Kumar Gala0bbaf062005-06-20 10:54:21 -05001065void gfar_start(struct net_device *dev)
1066{
1067 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -06001068 struct gfar __iomem *regs = priv->regs;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001069 u32 tempval;
1070
1071 /* Enable Rx and Tx in MACCFG1 */
1072 tempval = gfar_read(&regs->maccfg1);
1073 tempval |= (MACCFG1_RX_EN | MACCFG1_TX_EN);
1074 gfar_write(&regs->maccfg1, tempval);
1075
1076 /* Initialize DMACTRL to have WWR and WOP */
1077 tempval = gfar_read(&priv->regs->dmactrl);
1078 tempval |= DMACTRL_INIT_SETTINGS;
1079 gfar_write(&priv->regs->dmactrl, tempval);
1080
Kumar Gala0bbaf062005-06-20 10:54:21 -05001081 /* Make sure we aren't stopped */
1082 tempval = gfar_read(&priv->regs->dmactrl);
1083 tempval &= ~(DMACTRL_GRS | DMACTRL_GTS);
1084 gfar_write(&priv->regs->dmactrl, tempval);
1085
Andy Flemingfef61082006-04-20 16:44:29 -05001086 /* Clear THLT/RHLT, so that the DMA starts polling now */
1087 gfar_write(&regs->tstat, TSTAT_CLEAR_THALT);
1088 gfar_write(&regs->rstat, RSTAT_CLEAR_RHALT);
1089
Kumar Gala0bbaf062005-06-20 10:54:21 -05001090 /* Unmask the interrupts we look for */
1091 gfar_write(&regs->imask, IMASK_DEFAULT);
Dai Haruki12dea572008-12-16 15:30:20 -08001092
1093 dev->trans_start = jiffies;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001094}
1095
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096/* Bring the controller up and running */
Anton Vorontsovccc05c62009-10-12 06:00:26 +00001097int startup_gfar(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098{
Anton Vorontsovccc05c62009-10-12 06:00:26 +00001099 struct gfar_private *priv = netdev_priv(ndev);
Kumar Galacc8c6e32006-02-01 15:18:03 -06001100 struct gfar __iomem *regs = priv->regs;
Anton Vorontsovccc05c62009-10-12 06:00:26 +00001101 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
1103 gfar_write(&regs->imask, IMASK_INIT_CLEAR);
1104
Anton Vorontsov826aa4a2009-10-12 06:00:34 +00001105 err = gfar_alloc_skb_resources(ndev);
1106 if (err)
1107 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
Anton Vorontsov826aa4a2009-10-12 06:00:34 +00001109 gfar_init_mac(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 /* If the device has multiple interrupts, register for
1112 * them. Otherwise, only register for the one */
Andy Flemingb31a1d82008-12-16 15:29:15 -08001113 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001114 /* Install our interrupt handlers for Error,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 * Transmit, and Receive */
Anton Vorontsovccc05c62009-10-12 06:00:26 +00001116 err = request_irq(priv->interruptError, gfar_error, 0,
1117 priv->int_name_er, ndev);
1118 if (err) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001119 if (netif_msg_intr(priv))
Anton Vorontsovccc05c62009-10-12 06:00:26 +00001120 pr_err("%s: Can't get IRQ %d\n", ndev->name,
1121 priv->interruptError);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 goto err_irq_fail;
1123 }
1124
Anton Vorontsovccc05c62009-10-12 06:00:26 +00001125 err = request_irq(priv->interruptTransmit, gfar_transmit, 0,
1126 priv->int_name_tx, ndev);
1127 if (err) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001128 if (netif_msg_intr(priv))
Anton Vorontsovccc05c62009-10-12 06:00:26 +00001129 pr_err("%s: Can't get IRQ %d\n", ndev->name,
1130 priv->interruptTransmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 goto tx_irq_fail;
1132 }
1133
Anton Vorontsovccc05c62009-10-12 06:00:26 +00001134 err = request_irq(priv->interruptReceive, gfar_receive, 0,
1135 priv->int_name_rx, ndev);
1136 if (err) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001137 if (netif_msg_intr(priv))
Anton Vorontsovccc05c62009-10-12 06:00:26 +00001138 pr_err("%s: Can't get IRQ %d (receive0)\n",
1139 ndev->name, priv->interruptReceive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 goto rx_irq_fail;
1141 }
1142 } else {
Anton Vorontsovccc05c62009-10-12 06:00:26 +00001143 err = request_irq(priv->interruptTransmit, gfar_interrupt,
1144 0, priv->int_name_tx, ndev);
1145 if (err) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001146 if (netif_msg_intr(priv))
Anton Vorontsovccc05c62009-10-12 06:00:26 +00001147 pr_err("%s: Can't get IRQ %d\n", ndev->name,
1148 priv->interruptTransmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 goto err_irq_fail;
1150 }
1151 }
1152
Andy Fleming7f7f5312005-11-11 12:38:59 -06001153 /* Start the controller */
Anton Vorontsovccc05c62009-10-12 06:00:26 +00001154 gfar_start(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
Anton Vorontsov826aa4a2009-10-12 06:00:34 +00001156 phy_start(priv->phydev);
1157
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 return 0;
1159
1160rx_irq_fail:
Anton Vorontsovccc05c62009-10-12 06:00:26 +00001161 free_irq(priv->interruptTransmit, ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162tx_irq_fail:
Anton Vorontsovccc05c62009-10-12 06:00:26 +00001163 free_irq(priv->interruptError, ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164err_irq_fail:
Anton Vorontsove69edd22009-10-12 06:00:30 +00001165 free_skb_resources(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 return err;
1167}
1168
1169/* Called when something needs to use the ethernet device */
1170/* Returns 0 for success. */
1171static int gfar_enet_open(struct net_device *dev)
1172{
Li Yang94e8cc32007-10-12 21:53:51 +08001173 struct gfar_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 int err;
1175
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001176 napi_enable(&priv->napi);
1177
Andy Fleming0fd56bb2009-02-04 16:43:16 -08001178 skb_queue_head_init(&priv->rx_recycle);
1179
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 /* Initialize a bunch of registers */
1181 init_registers(dev);
1182
1183 gfar_set_mac_address(dev);
1184
1185 err = init_phy(dev);
1186
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001187 if(err) {
1188 napi_disable(&priv->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 return err;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001190 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
1192 err = startup_gfar(dev);
Anton Vorontsovdb0e8e32007-10-17 23:57:46 +04001193 if (err) {
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001194 napi_disable(&priv->napi);
Anton Vorontsovdb0e8e32007-10-17 23:57:46 +04001195 return err;
1196 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
1198 netif_start_queue(dev);
1199
Anton Vorontsov2884e5c2009-02-01 00:52:34 -08001200 device_set_wakeup_enable(&dev->dev, priv->wol_en);
1201
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 return err;
1203}
1204
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07001205static inline struct txfcb *gfar_add_fcb(struct sk_buff *skb)
Kumar Gala0bbaf062005-06-20 10:54:21 -05001206{
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07001207 struct txfcb *fcb = (struct txfcb *)skb_push(skb, GMAC_FCB_LEN);
Kumar Gala6c31d552009-04-28 08:04:10 -07001208
1209 memset(fcb, 0, GMAC_FCB_LEN);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001210
Kumar Gala0bbaf062005-06-20 10:54:21 -05001211 return fcb;
1212}
1213
1214static inline void gfar_tx_checksum(struct sk_buff *skb, struct txfcb *fcb)
1215{
Andy Fleming7f7f5312005-11-11 12:38:59 -06001216 u8 flags = 0;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001217
1218 /* If we're here, it's a IP packet with a TCP or UDP
1219 * payload. We set it to checksum, using a pseudo-header
1220 * we provide
1221 */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001222 flags = TXFCB_DEFAULT;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001223
Andy Fleming7f7f5312005-11-11 12:38:59 -06001224 /* Tell the controller what the protocol is */
1225 /* And provide the already calculated phcs */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001226 if (ip_hdr(skb)->protocol == IPPROTO_UDP) {
Andy Fleming7f7f5312005-11-11 12:38:59 -06001227 flags |= TXFCB_UDP;
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -03001228 fcb->phcs = udp_hdr(skb)->check;
Andy Fleming7f7f5312005-11-11 12:38:59 -06001229 } else
Kumar Gala8da32de2007-06-29 00:12:04 -05001230 fcb->phcs = tcp_hdr(skb)->check;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001231
1232 /* l3os is the distance between the start of the
1233 * frame (skb->data) and the start of the IP hdr.
1234 * l4os is the distance between the start of the
1235 * l3 hdr and the l4 hdr */
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03001236 fcb->l3os = (u16)(skb_network_offset(skb) - GMAC_FCB_LEN);
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -03001237 fcb->l4os = skb_network_header_len(skb);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001238
Andy Fleming7f7f5312005-11-11 12:38:59 -06001239 fcb->flags = flags;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001240}
1241
Andy Fleming7f7f5312005-11-11 12:38:59 -06001242void inline gfar_tx_vlan(struct sk_buff *skb, struct txfcb *fcb)
Kumar Gala0bbaf062005-06-20 10:54:21 -05001243{
Andy Fleming7f7f5312005-11-11 12:38:59 -06001244 fcb->flags |= TXFCB_VLN;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001245 fcb->vlctl = vlan_tx_tag_get(skb);
1246}
1247
Dai Haruki4669bc92008-12-17 16:51:04 -08001248static inline struct txbd8 *skip_txbd(struct txbd8 *bdp, int stride,
1249 struct txbd8 *base, int ring_size)
1250{
1251 struct txbd8 *new_bd = bdp + stride;
1252
1253 return (new_bd >= (base + ring_size)) ? (new_bd - ring_size) : new_bd;
1254}
1255
1256static inline struct txbd8 *next_txbd(struct txbd8 *bdp, struct txbd8 *base,
1257 int ring_size)
1258{
1259 return skip_txbd(bdp, 1, base, ring_size);
1260}
1261
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262/* This is called by the kernel when a frame is ready for transmission. */
1263/* It is pointed to by the dev->hard_start_xmit function pointer */
1264static int gfar_start_xmit(struct sk_buff *skb, struct net_device *dev)
1265{
1266 struct gfar_private *priv = netdev_priv(dev);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001267 struct txfcb *fcb = NULL;
Dai Haruki4669bc92008-12-17 16:51:04 -08001268 struct txbd8 *txbdp, *txbdp_start, *base;
Dai Haruki5a5efed2008-12-16 15:34:50 -08001269 u32 lstatus;
Dai Haruki4669bc92008-12-17 16:51:04 -08001270 int i;
1271 u32 bufaddr;
Andy Flemingfef61082006-04-20 16:44:29 -05001272 unsigned long flags;
Dai Haruki4669bc92008-12-17 16:51:04 -08001273 unsigned int nr_frags, length;
1274
1275 base = priv->tx_bd_base;
1276
Li Yang5b28bea2009-03-27 15:54:30 -07001277 /* make space for additional header when fcb is needed */
1278 if (((skb->ip_summed == CHECKSUM_PARTIAL) ||
1279 (priv->vlgrp && vlan_tx_tag_present(skb))) &&
1280 (skb_headroom(skb) < GMAC_FCB_LEN)) {
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07001281 struct sk_buff *skb_new;
1282
1283 skb_new = skb_realloc_headroom(skb, GMAC_FCB_LEN);
1284 if (!skb_new) {
1285 dev->stats.tx_errors++;
David S. Millerbd14ba82009-03-27 01:10:58 -07001286 kfree_skb(skb);
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07001287 return NETDEV_TX_OK;
1288 }
1289 kfree_skb(skb);
1290 skb = skb_new;
1291 }
1292
Dai Haruki4669bc92008-12-17 16:51:04 -08001293 /* total number of fragments in the SKB */
1294 nr_frags = skb_shinfo(skb)->nr_frags;
1295
1296 spin_lock_irqsave(&priv->txlock, flags);
1297
1298 /* check if there is space to queue this packet */
Rini van Zetten7958a452009-02-27 03:18:48 -08001299 if ((nr_frags+1) > priv->num_txbdfree) {
Dai Haruki4669bc92008-12-17 16:51:04 -08001300 /* no space, stop the queue */
1301 netif_stop_queue(dev);
1302 dev->stats.tx_fifo_errors++;
1303 spin_unlock_irqrestore(&priv->txlock, flags);
1304 return NETDEV_TX_BUSY;
1305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
1307 /* Update transmit stats */
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001308 dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
Dai Haruki4669bc92008-12-17 16:51:04 -08001310 txbdp = txbdp_start = priv->cur_tx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
Dai Haruki4669bc92008-12-17 16:51:04 -08001312 if (nr_frags == 0) {
1313 lstatus = txbdp->lstatus | BD_LFLAG(TXBD_LAST | TXBD_INTERRUPT);
1314 } else {
1315 /* Place the fragment addresses and lengths into the TxBDs */
1316 for (i = 0; i < nr_frags; i++) {
1317 /* Point at the next BD, wrapping as needed */
1318 txbdp = next_txbd(txbdp, base, priv->tx_ring_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
Dai Haruki4669bc92008-12-17 16:51:04 -08001320 length = skb_shinfo(skb)->frags[i].size;
1321
1322 lstatus = txbdp->lstatus | length |
1323 BD_LFLAG(TXBD_READY);
1324
1325 /* Handle the last BD specially */
1326 if (i == nr_frags - 1)
1327 lstatus |= BD_LFLAG(TXBD_LAST | TXBD_INTERRUPT);
1328
Kumar Gala48268572009-03-18 23:28:22 -07001329 bufaddr = dma_map_page(&priv->ofdev->dev,
Dai Haruki4669bc92008-12-17 16:51:04 -08001330 skb_shinfo(skb)->frags[i].page,
1331 skb_shinfo(skb)->frags[i].page_offset,
1332 length,
1333 DMA_TO_DEVICE);
1334
1335 /* set the TxBD length and buffer pointer */
1336 txbdp->bufPtr = bufaddr;
1337 txbdp->lstatus = lstatus;
1338 }
1339
1340 lstatus = txbdp_start->lstatus;
1341 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
Kumar Gala0bbaf062005-06-20 10:54:21 -05001343 /* Set up checksumming */
Dai Haruki12dea572008-12-16 15:30:20 -08001344 if (CHECKSUM_PARTIAL == skb->ip_summed) {
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07001345 fcb = gfar_add_fcb(skb);
1346 lstatus |= BD_LFLAG(TXBD_TOE);
1347 gfar_tx_checksum(skb, fcb);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001348 }
1349
Dai Haruki77ecaf22008-12-16 15:30:48 -08001350 if (priv->vlgrp && vlan_tx_tag_present(skb)) {
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07001351 if (unlikely(NULL == fcb)) {
1352 fcb = gfar_add_fcb(skb);
Dai Haruki5a5efed2008-12-16 15:34:50 -08001353 lstatus |= BD_LFLAG(TXBD_TOE);
Andy Fleming7f7f5312005-11-11 12:38:59 -06001354 }
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07001355
1356 gfar_tx_vlan(skb, fcb);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001357 }
1358
Dai Haruki4669bc92008-12-17 16:51:04 -08001359 /* setup the TxBD length and buffer pointer for the first BD */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 priv->tx_skbuff[priv->skb_curtx] = skb;
Kumar Gala48268572009-03-18 23:28:22 -07001361 txbdp_start->bufPtr = dma_map_single(&priv->ofdev->dev, skb->data,
Dai Haruki4669bc92008-12-17 16:51:04 -08001362 skb_headlen(skb), DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
Dai Haruki4669bc92008-12-17 16:51:04 -08001364 lstatus |= BD_LFLAG(TXBD_CRC | TXBD_READY) | skb_headlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
Dai Haruki4669bc92008-12-17 16:51:04 -08001366 /*
1367 * The powerpc-specific eieio() is used, as wmb() has too strong
Scott Wood3b6330c2007-05-16 15:06:59 -05001368 * semantics (it requires synchronization between cacheable and
1369 * uncacheable mappings, which eieio doesn't provide and which we
1370 * don't need), thus requiring a more expensive sync instruction. At
1371 * some point, the set of architecture-independent barrier functions
1372 * should be expanded to include weaker barriers.
1373 */
Scott Wood3b6330c2007-05-16 15:06:59 -05001374 eieio();
Andy Fleming7f7f5312005-11-11 12:38:59 -06001375
Dai Haruki4669bc92008-12-17 16:51:04 -08001376 txbdp_start->lstatus = lstatus;
1377
1378 /* Update the current skb pointer to the next entry we will use
1379 * (wrapping if necessary) */
1380 priv->skb_curtx = (priv->skb_curtx + 1) &
1381 TX_RING_MOD_MASK(priv->tx_ring_size);
1382
1383 priv->cur_tx = next_txbd(txbdp, base, priv->tx_ring_size);
1384
1385 /* reduce TxBD free count */
1386 priv->num_txbdfree -= (nr_frags + 1);
1387
1388 dev->trans_start = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
1390 /* If the next BD still needs to be cleaned up, then the bds
1391 are full. We need to tell the kernel to stop sending us stuff. */
Dai Haruki4669bc92008-12-17 16:51:04 -08001392 if (!priv->num_txbdfree) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 netif_stop_queue(dev);
1394
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001395 dev->stats.tx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 }
1397
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 /* Tell the DMA to go go go */
1399 gfar_write(&priv->regs->tstat, TSTAT_CLEAR_THALT);
1400
1401 /* Unlock priv */
Andy Flemingfef61082006-04-20 16:44:29 -05001402 spin_unlock_irqrestore(&priv->txlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07001404 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405}
1406
1407/* Stops the kernel queue, and halts the controller */
1408static int gfar_close(struct net_device *dev)
1409{
1410 struct gfar_private *priv = netdev_priv(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001411
1412 napi_disable(&priv->napi);
1413
Andy Fleming0fd56bb2009-02-04 16:43:16 -08001414 skb_queue_purge(&priv->rx_recycle);
Sebastian Siewiorab939902008-08-19 21:12:45 +02001415 cancel_work_sync(&priv->reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 stop_gfar(dev);
1417
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001418 /* Disconnect from the PHY */
1419 phy_disconnect(priv->phydev);
1420 priv->phydev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
1422 netif_stop_queue(dev);
1423
1424 return 0;
1425}
1426
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427/* Changes the mac address if the controller is not running. */
Andy Flemingf162b9d2008-05-02 13:00:30 -05001428static int gfar_set_mac_address(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429{
Andy Fleming7f7f5312005-11-11 12:38:59 -06001430 gfar_set_mac_for_addr(dev, 0, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
1432 return 0;
1433}
1434
1435
Kumar Gala0bbaf062005-06-20 10:54:21 -05001436/* Enables and disables VLAN insertion/extraction */
1437static void gfar_vlan_rx_register(struct net_device *dev,
1438 struct vlan_group *grp)
1439{
1440 struct gfar_private *priv = netdev_priv(dev);
1441 unsigned long flags;
1442 u32 tempval;
1443
Andy Flemingfef61082006-04-20 16:44:29 -05001444 spin_lock_irqsave(&priv->rxlock, flags);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001445
Anton Vorontsovcd1f55a2009-01-26 14:33:23 -08001446 priv->vlgrp = grp;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001447
1448 if (grp) {
1449 /* Enable VLAN tag insertion */
1450 tempval = gfar_read(&priv->regs->tctrl);
1451 tempval |= TCTRL_VLINS;
1452
1453 gfar_write(&priv->regs->tctrl, tempval);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001454
Kumar Gala0bbaf062005-06-20 10:54:21 -05001455 /* Enable VLAN tag extraction */
1456 tempval = gfar_read(&priv->regs->rctrl);
Dai Haruki77ecaf22008-12-16 15:30:48 -08001457 tempval |= (RCTRL_VLEX | RCTRL_PRSDEP_INIT);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001458 gfar_write(&priv->regs->rctrl, tempval);
1459 } else {
1460 /* Disable VLAN tag insertion */
1461 tempval = gfar_read(&priv->regs->tctrl);
1462 tempval &= ~TCTRL_VLINS;
1463 gfar_write(&priv->regs->tctrl, tempval);
1464
1465 /* Disable VLAN tag extraction */
1466 tempval = gfar_read(&priv->regs->rctrl);
1467 tempval &= ~RCTRL_VLEX;
Dai Haruki77ecaf22008-12-16 15:30:48 -08001468 /* If parse is no longer required, then disable parser */
1469 if (tempval & RCTRL_REQ_PARSER)
1470 tempval |= RCTRL_PRSDEP_INIT;
1471 else
1472 tempval &= ~RCTRL_PRSDEP_INIT;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001473 gfar_write(&priv->regs->rctrl, tempval);
1474 }
1475
Dai Haruki77ecaf22008-12-16 15:30:48 -08001476 gfar_change_mtu(dev, dev->mtu);
1477
Andy Flemingfef61082006-04-20 16:44:29 -05001478 spin_unlock_irqrestore(&priv->rxlock, flags);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001479}
1480
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481static int gfar_change_mtu(struct net_device *dev, int new_mtu)
1482{
1483 int tempsize, tempval;
1484 struct gfar_private *priv = netdev_priv(dev);
1485 int oldsize = priv->rx_buffer_size;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001486 int frame_size = new_mtu + ETH_HLEN;
1487
Dai Haruki77ecaf22008-12-16 15:30:48 -08001488 if (priv->vlgrp)
Dai Harukifaa89572008-03-24 10:53:26 -05001489 frame_size += VLAN_HLEN;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001490
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 if ((frame_size < 64) || (frame_size > JUMBO_FRAME_SIZE)) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001492 if (netif_msg_drv(priv))
1493 printk(KERN_ERR "%s: Invalid MTU setting\n",
1494 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 return -EINVAL;
1496 }
1497
Dai Haruki77ecaf22008-12-16 15:30:48 -08001498 if (gfar_uses_fcb(priv))
1499 frame_size += GMAC_FCB_LEN;
1500
1501 frame_size += priv->padding;
1502
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 tempsize =
1504 (frame_size & ~(INCREMENTAL_BUFFER_SIZE - 1)) +
1505 INCREMENTAL_BUFFER_SIZE;
1506
1507 /* Only stop and start the controller if it isn't already
Andy Fleming7f7f5312005-11-11 12:38:59 -06001508 * stopped, and we changed something */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 if ((oldsize != tempsize) && (dev->flags & IFF_UP))
1510 stop_gfar(dev);
1511
1512 priv->rx_buffer_size = tempsize;
1513
1514 dev->mtu = new_mtu;
1515
1516 gfar_write(&priv->regs->mrblr, priv->rx_buffer_size);
1517 gfar_write(&priv->regs->maxfrm, priv->rx_buffer_size);
1518
1519 /* If the mtu is larger than the max size for standard
1520 * ethernet frames (ie, a jumbo frame), then set maccfg2
1521 * to allow huge frames, and to check the length */
1522 tempval = gfar_read(&priv->regs->maccfg2);
1523
1524 if (priv->rx_buffer_size > DEFAULT_RX_BUFFER_SIZE)
1525 tempval |= (MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK);
1526 else
1527 tempval &= ~(MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK);
1528
1529 gfar_write(&priv->regs->maccfg2, tempval);
1530
1531 if ((oldsize != tempsize) && (dev->flags & IFF_UP))
1532 startup_gfar(dev);
1533
1534 return 0;
1535}
1536
Sebastian Siewiorab939902008-08-19 21:12:45 +02001537/* gfar_reset_task gets scheduled when a packet has not been
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 * transmitted after a set amount of time.
1539 * For now, assume that clearing out all the structures, and
Sebastian Siewiorab939902008-08-19 21:12:45 +02001540 * starting over will fix the problem.
1541 */
1542static void gfar_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543{
Sebastian Siewiorab939902008-08-19 21:12:45 +02001544 struct gfar_private *priv = container_of(work, struct gfar_private,
1545 reset_task);
Kumar Gala48268572009-03-18 23:28:22 -07001546 struct net_device *dev = priv->ndev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547
1548 if (dev->flags & IFF_UP) {
Markus Brunnercbea27072009-04-15 02:35:40 -07001549 netif_stop_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 stop_gfar(dev);
1551 startup_gfar(dev);
Markus Brunnercbea27072009-04-15 02:35:40 -07001552 netif_start_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 }
1554
David S. Miller263ba322008-07-15 03:47:41 -07001555 netif_tx_schedule_all(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556}
1557
Sebastian Siewiorab939902008-08-19 21:12:45 +02001558static void gfar_timeout(struct net_device *dev)
1559{
1560 struct gfar_private *priv = netdev_priv(dev);
1561
1562 dev->stats.tx_errors++;
1563 schedule_work(&priv->reset_task);
1564}
1565
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566/* Interrupt Handler for Transmit complete */
Andy Flemingf162b9d2008-05-02 13:00:30 -05001567static int gfar_clean_tx_ring(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568{
Dai Harukid080cd62008-04-09 19:37:51 -05001569 struct gfar_private *priv = netdev_priv(dev);
Dai Haruki4669bc92008-12-17 16:51:04 -08001570 struct txbd8 *bdp;
1571 struct txbd8 *lbdp = NULL;
1572 struct txbd8 *base = priv->tx_bd_base;
1573 struct sk_buff *skb;
1574 int skb_dirtytx;
1575 int tx_ring_size = priv->tx_ring_size;
1576 int frags = 0;
1577 int i;
Dai Harukid080cd62008-04-09 19:37:51 -05001578 int howmany = 0;
Dai Haruki4669bc92008-12-17 16:51:04 -08001579 u32 lstatus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 bdp = priv->dirty_tx;
Dai Haruki4669bc92008-12-17 16:51:04 -08001582 skb_dirtytx = priv->skb_dirtytx;
1583
1584 while ((skb = priv->tx_skbuff[skb_dirtytx])) {
1585 frags = skb_shinfo(skb)->nr_frags;
1586 lbdp = skip_txbd(bdp, frags, base, tx_ring_size);
1587
1588 lstatus = lbdp->lstatus;
1589
1590 /* Only clean completed frames */
1591 if ((lstatus & BD_LFLAG(TXBD_READY)) &&
1592 (lstatus & BD_LENGTH_MASK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 break;
1594
Kumar Gala48268572009-03-18 23:28:22 -07001595 dma_unmap_single(&priv->ofdev->dev,
Dai Haruki4669bc92008-12-17 16:51:04 -08001596 bdp->bufPtr,
1597 bdp->length,
1598 DMA_TO_DEVICE);
1599
1600 bdp->lstatus &= BD_LFLAG(TXBD_WRAP);
1601 bdp = next_txbd(bdp, base, tx_ring_size);
1602
1603 for (i = 0; i < frags; i++) {
Kumar Gala48268572009-03-18 23:28:22 -07001604 dma_unmap_page(&priv->ofdev->dev,
Dai Haruki4669bc92008-12-17 16:51:04 -08001605 bdp->bufPtr,
1606 bdp->length,
1607 DMA_TO_DEVICE);
1608 bdp->lstatus &= BD_LFLAG(TXBD_WRAP);
1609 bdp = next_txbd(bdp, base, tx_ring_size);
1610 }
1611
Andy Fleming0fd56bb2009-02-04 16:43:16 -08001612 /*
1613 * If there's room in the queue (limit it to rx_buffer_size)
1614 * we add this skb back into the pool, if it's the right size
1615 */
1616 if (skb_queue_len(&priv->rx_recycle) < priv->rx_ring_size &&
1617 skb_recycle_check(skb, priv->rx_buffer_size +
1618 RXBUF_ALIGNMENT))
1619 __skb_queue_head(&priv->rx_recycle, skb);
1620 else
1621 dev_kfree_skb_any(skb);
1622
Dai Haruki4669bc92008-12-17 16:51:04 -08001623 priv->tx_skbuff[skb_dirtytx] = NULL;
1624
1625 skb_dirtytx = (skb_dirtytx + 1) &
1626 TX_RING_MOD_MASK(tx_ring_size);
1627
Dai Harukid080cd62008-04-09 19:37:51 -05001628 howmany++;
Dai Haruki4669bc92008-12-17 16:51:04 -08001629 priv->num_txbdfree += frags + 1;
1630 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631
Dai Haruki4669bc92008-12-17 16:51:04 -08001632 /* If we freed a buffer, we can restart transmission, if necessary */
1633 if (netif_queue_stopped(dev) && priv->num_txbdfree)
1634 netif_wake_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635
Dai Haruki4669bc92008-12-17 16:51:04 -08001636 /* Update dirty indicators */
1637 priv->skb_dirtytx = skb_dirtytx;
1638 priv->dirty_tx = bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639
Dai Harukid080cd62008-04-09 19:37:51 -05001640 dev->stats.tx_packets += howmany;
1641
1642 return howmany;
1643}
1644
Dai Haruki8c7396a2008-12-17 16:52:00 -08001645static void gfar_schedule_cleanup(struct net_device *dev)
1646{
1647 struct gfar_private *priv = netdev_priv(dev);
Anton Vorontsova6d0b912009-01-12 21:57:34 -08001648 unsigned long flags;
1649
1650 spin_lock_irqsave(&priv->txlock, flags);
1651 spin_lock(&priv->rxlock);
1652
Ben Hutchings288379f2009-01-19 16:43:59 -08001653 if (napi_schedule_prep(&priv->napi)) {
Dai Haruki8c7396a2008-12-17 16:52:00 -08001654 gfar_write(&priv->regs->imask, IMASK_RTX_DISABLED);
Ben Hutchings288379f2009-01-19 16:43:59 -08001655 __napi_schedule(&priv->napi);
Jarek Poplawski8707bdd2009-02-09 14:59:30 -08001656 } else {
1657 /*
1658 * Clear IEVENT, so interrupts aren't called again
1659 * because of the packets that have already arrived.
1660 */
1661 gfar_write(&priv->regs->ievent, IEVENT_RTX_MASK);
Dai Haruki8c7396a2008-12-17 16:52:00 -08001662 }
Anton Vorontsova6d0b912009-01-12 21:57:34 -08001663
1664 spin_unlock(&priv->rxlock);
1665 spin_unlock_irqrestore(&priv->txlock, flags);
Dai Haruki8c7396a2008-12-17 16:52:00 -08001666}
1667
Dai Harukid080cd62008-04-09 19:37:51 -05001668/* Interrupt Handler for Transmit complete */
1669static irqreturn_t gfar_transmit(int irq, void *dev_id)
1670{
Dai Haruki8c7396a2008-12-17 16:52:00 -08001671 gfar_schedule_cleanup((struct net_device *)dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 return IRQ_HANDLED;
1673}
1674
Andy Fleming815b97c2008-04-22 17:18:29 -05001675static void gfar_new_rxbdp(struct net_device *dev, struct rxbd8 *bdp,
1676 struct sk_buff *skb)
1677{
1678 struct gfar_private *priv = netdev_priv(dev);
Dai Haruki5a5efed2008-12-16 15:34:50 -08001679 u32 lstatus;
Andy Fleming815b97c2008-04-22 17:18:29 -05001680
Kumar Gala48268572009-03-18 23:28:22 -07001681 bdp->bufPtr = dma_map_single(&priv->ofdev->dev, skb->data,
Andy Fleming815b97c2008-04-22 17:18:29 -05001682 priv->rx_buffer_size, DMA_FROM_DEVICE);
1683
Dai Haruki5a5efed2008-12-16 15:34:50 -08001684 lstatus = BD_LFLAG(RXBD_EMPTY | RXBD_INTERRUPT);
Andy Fleming815b97c2008-04-22 17:18:29 -05001685
1686 if (bdp == priv->rx_bd_base + priv->rx_ring_size - 1)
Dai Haruki5a5efed2008-12-16 15:34:50 -08001687 lstatus |= BD_LFLAG(RXBD_WRAP);
Andy Fleming815b97c2008-04-22 17:18:29 -05001688
1689 eieio();
1690
Dai Haruki5a5efed2008-12-16 15:34:50 -08001691 bdp->lstatus = lstatus;
Andy Fleming815b97c2008-04-22 17:18:29 -05001692}
1693
1694
1695struct sk_buff * gfar_new_skb(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696{
Andy Fleming7f7f5312005-11-11 12:38:59 -06001697 unsigned int alignamount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 struct gfar_private *priv = netdev_priv(dev);
1699 struct sk_buff *skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700
Andy Fleming0fd56bb2009-02-04 16:43:16 -08001701 skb = __skb_dequeue(&priv->rx_recycle);
1702 if (!skb)
1703 skb = netdev_alloc_skb(dev,
1704 priv->rx_buffer_size + RXBUF_ALIGNMENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705
Andy Fleming815b97c2008-04-22 17:18:29 -05001706 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 return NULL;
1708
Andy Fleming7f7f5312005-11-11 12:38:59 -06001709 alignamount = RXBUF_ALIGNMENT -
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001710 (((unsigned long) skb->data) & (RXBUF_ALIGNMENT - 1));
Andy Fleming7f7f5312005-11-11 12:38:59 -06001711
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 /* We need the data buffer to be aligned properly. We will reserve
1713 * as many bytes as needed to align the data properly
1714 */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001715 skb_reserve(skb, alignamount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 return skb;
1718}
1719
Li Yang298e1a92007-10-16 14:18:13 +08001720static inline void count_errors(unsigned short status, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721{
Li Yang298e1a92007-10-16 14:18:13 +08001722 struct gfar_private *priv = netdev_priv(dev);
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001723 struct net_device_stats *stats = &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 struct gfar_extra_stats *estats = &priv->extra_stats;
1725
1726 /* If the packet was truncated, none of the other errors
1727 * matter */
1728 if (status & RXBD_TRUNCATED) {
1729 stats->rx_length_errors++;
1730
1731 estats->rx_trunc++;
1732
1733 return;
1734 }
1735 /* Count the errors, if there were any */
1736 if (status & (RXBD_LARGE | RXBD_SHORT)) {
1737 stats->rx_length_errors++;
1738
1739 if (status & RXBD_LARGE)
1740 estats->rx_large++;
1741 else
1742 estats->rx_short++;
1743 }
1744 if (status & RXBD_NONOCTET) {
1745 stats->rx_frame_errors++;
1746 estats->rx_nonoctet++;
1747 }
1748 if (status & RXBD_CRCERR) {
1749 estats->rx_crcerr++;
1750 stats->rx_crc_errors++;
1751 }
1752 if (status & RXBD_OVERRUN) {
1753 estats->rx_overrun++;
1754 stats->rx_crc_errors++;
1755 }
1756}
1757
David Howells7d12e782006-10-05 14:55:46 +01001758irqreturn_t gfar_receive(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759{
Dai Haruki8c7396a2008-12-17 16:52:00 -08001760 gfar_schedule_cleanup((struct net_device *)dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 return IRQ_HANDLED;
1762}
1763
Kumar Gala0bbaf062005-06-20 10:54:21 -05001764static inline void gfar_rx_checksum(struct sk_buff *skb, struct rxfcb *fcb)
1765{
1766 /* If valid headers were found, and valid sums
1767 * were verified, then we tell the kernel that no
1768 * checksumming is necessary. Otherwise, it is */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001769 if ((fcb->flags & RXFCB_CSUM_MASK) == (RXFCB_CIP | RXFCB_CTU))
Kumar Gala0bbaf062005-06-20 10:54:21 -05001770 skb->ip_summed = CHECKSUM_UNNECESSARY;
1771 else
1772 skb->ip_summed = CHECKSUM_NONE;
1773}
1774
1775
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776/* gfar_process_frame() -- handle one incoming packet if skb
1777 * isn't NULL. */
1778static int gfar_process_frame(struct net_device *dev, struct sk_buff *skb,
Dai Haruki2c2db482008-12-16 15:31:15 -08001779 int amount_pull)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780{
1781 struct gfar_private *priv = netdev_priv(dev);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001782 struct rxfcb *fcb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783
Dai Haruki2c2db482008-12-16 15:31:15 -08001784 int ret;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001785
Dai Haruki2c2db482008-12-16 15:31:15 -08001786 /* fcb is at the beginning if exists */
1787 fcb = (struct rxfcb *)skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
Dai Haruki2c2db482008-12-16 15:31:15 -08001789 /* Remove the FCB from the skb */
1790 /* Remove the padded bytes, if there are any */
1791 if (amount_pull)
1792 skb_pull(skb, amount_pull);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001793
Dai Haruki2c2db482008-12-16 15:31:15 -08001794 if (priv->rx_csum_enable)
1795 gfar_rx_checksum(skb, fcb);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001796
Dai Haruki2c2db482008-12-16 15:31:15 -08001797 /* Tell the skb what kind of packet this is */
1798 skb->protocol = eth_type_trans(skb, dev);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001799
Dai Haruki2c2db482008-12-16 15:31:15 -08001800 /* Send the packet up the stack */
1801 if (unlikely(priv->vlgrp && (fcb->flags & RXFCB_VLN)))
1802 ret = vlan_hwaccel_receive_skb(skb, priv->vlgrp, fcb->vlctl);
1803 else
1804 ret = netif_receive_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
Dai Haruki2c2db482008-12-16 15:31:15 -08001806 if (NET_RX_DROP == ret)
1807 priv->extra_stats.kernel_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808
1809 return 0;
1810}
1811
1812/* gfar_clean_rx_ring() -- Processes each frame in the rx ring
Kumar Gala0bbaf062005-06-20 10:54:21 -05001813 * until the budget/quota has been reached. Returns the number
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 * of frames handled
1815 */
Kumar Gala0bbaf062005-06-20 10:54:21 -05001816int gfar_clean_rx_ring(struct net_device *dev, int rx_work_limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817{
Andy Fleming31de1982008-12-16 15:33:40 -08001818 struct rxbd8 *bdp, *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 struct sk_buff *skb;
Dai Haruki2c2db482008-12-16 15:31:15 -08001820 int pkt_len;
1821 int amount_pull;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 int howmany = 0;
1823 struct gfar_private *priv = netdev_priv(dev);
1824
1825 /* Get the first full descriptor */
1826 bdp = priv->cur_rx;
Andy Fleming31de1982008-12-16 15:33:40 -08001827 base = priv->rx_bd_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828
Dai Haruki2c2db482008-12-16 15:31:15 -08001829 amount_pull = (gfar_uses_fcb(priv) ? GMAC_FCB_LEN : 0) +
1830 priv->padding;
1831
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 while (!((bdp->status & RXBD_EMPTY) || (--rx_work_limit < 0))) {
Andy Fleming815b97c2008-04-22 17:18:29 -05001833 struct sk_buff *newskb;
Scott Wood3b6330c2007-05-16 15:06:59 -05001834 rmb();
Andy Fleming815b97c2008-04-22 17:18:29 -05001835
1836 /* Add another skb for the future */
1837 newskb = gfar_new_skb(dev);
1838
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 skb = priv->rx_skbuff[priv->skb_currx];
1840
Kumar Gala48268572009-03-18 23:28:22 -07001841 dma_unmap_single(&priv->ofdev->dev, bdp->bufPtr,
Andy Fleming81183052008-11-12 10:07:11 -06001842 priv->rx_buffer_size, DMA_FROM_DEVICE);
1843
Andy Fleming815b97c2008-04-22 17:18:29 -05001844 /* We drop the frame if we failed to allocate a new buffer */
1845 if (unlikely(!newskb || !(bdp->status & RXBD_LAST) ||
1846 bdp->status & RXBD_ERR)) {
1847 count_errors(bdp->status, dev);
1848
1849 if (unlikely(!newskb))
1850 newskb = skb;
Lennert Buytenhek4e2fd552009-05-25 00:42:34 -07001851 else if (skb) {
1852 /*
1853 * We need to reset ->data to what it
1854 * was before gfar_new_skb() re-aligned
1855 * it to an RXBUF_ALIGNMENT boundary
1856 * before we put the skb back on the
1857 * recycle list.
1858 */
1859 skb->data = skb->head + NET_SKB_PAD;
Andy Fleming0fd56bb2009-02-04 16:43:16 -08001860 __skb_queue_head(&priv->rx_recycle, skb);
Lennert Buytenhek4e2fd552009-05-25 00:42:34 -07001861 }
Andy Fleming815b97c2008-04-22 17:18:29 -05001862 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 /* Increment the number of packets */
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001864 dev->stats.rx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 howmany++;
1866
Dai Haruki2c2db482008-12-16 15:31:15 -08001867 if (likely(skb)) {
1868 pkt_len = bdp->length - ETH_FCS_LEN;
1869 /* Remove the FCS from the packet length */
1870 skb_put(skb, pkt_len);
1871 dev->stats.rx_bytes += pkt_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872
Andy Fleming1577ece2009-02-04 16:42:12 -08001873 if (in_irq() || irqs_disabled())
1874 printk("Interrupt problem!\n");
Dai Haruki2c2db482008-12-16 15:31:15 -08001875 gfar_process_frame(dev, skb, amount_pull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876
Dai Haruki2c2db482008-12-16 15:31:15 -08001877 } else {
1878 if (netif_msg_rx_err(priv))
1879 printk(KERN_WARNING
1880 "%s: Missing skb!\n", dev->name);
1881 dev->stats.rx_dropped++;
1882 priv->extra_stats.rx_skbmissing++;
1883 }
1884
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 }
1886
Andy Fleming815b97c2008-04-22 17:18:29 -05001887 priv->rx_skbuff[priv->skb_currx] = newskb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888
Andy Fleming815b97c2008-04-22 17:18:29 -05001889 /* Setup the new bdp */
1890 gfar_new_rxbdp(dev, bdp, newskb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
1892 /* Update to the next pointer */
Andy Fleming31de1982008-12-16 15:33:40 -08001893 bdp = next_bd(bdp, base, priv->rx_ring_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894
1895 /* update to point at the next skb */
1896 priv->skb_currx =
Andy Fleming815b97c2008-04-22 17:18:29 -05001897 (priv->skb_currx + 1) &
1898 RX_RING_MOD_MASK(priv->rx_ring_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 }
1900
1901 /* Update the current rxbd pointer to be the next one */
1902 priv->cur_rx = bdp;
1903
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 return howmany;
1905}
1906
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001907static int gfar_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001909 struct gfar_private *priv = container_of(napi, struct gfar_private, napi);
Kumar Gala48268572009-03-18 23:28:22 -07001910 struct net_device *dev = priv->ndev;
Andy Fleming42199882008-12-17 16:52:30 -08001911 int tx_cleaned = 0;
1912 int rx_cleaned = 0;
Dai Harukid080cd62008-04-09 19:37:51 -05001913 unsigned long flags;
1914
Dai Haruki8c7396a2008-12-17 16:52:00 -08001915 /* Clear IEVENT, so interrupts aren't called again
1916 * because of the packets that have already arrived */
1917 gfar_write(&priv->regs->ievent, IEVENT_RTX_MASK);
1918
Dai Harukid080cd62008-04-09 19:37:51 -05001919 /* If we fail to get the lock, don't bother with the TX BDs */
1920 if (spin_trylock_irqsave(&priv->txlock, flags)) {
Andy Fleming42199882008-12-17 16:52:30 -08001921 tx_cleaned = gfar_clean_tx_ring(dev);
Dai Harukid080cd62008-04-09 19:37:51 -05001922 spin_unlock_irqrestore(&priv->txlock, flags);
1923 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924
Andy Fleming42199882008-12-17 16:52:30 -08001925 rx_cleaned = gfar_clean_rx_ring(dev, budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926
Andy Fleming42199882008-12-17 16:52:30 -08001927 if (tx_cleaned)
1928 return budget;
1929
1930 if (rx_cleaned < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -08001931 napi_complete(napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932
1933 /* Clear the halt bit in RSTAT */
1934 gfar_write(&priv->regs->rstat, RSTAT_CLEAR_RHALT);
1935
1936 gfar_write(&priv->regs->imask, IMASK_DEFAULT);
1937
1938 /* If we are coalescing interrupts, update the timer */
1939 /* Otherwise, clear it */
Andy Fleming2f448912008-03-24 10:53:28 -05001940 if (likely(priv->rxcoalescing)) {
1941 gfar_write(&priv->regs->rxic, 0);
Dai Harukib46a8452008-12-16 15:29:52 -08001942 gfar_write(&priv->regs->rxic, priv->rxic);
Andy Fleming2f448912008-03-24 10:53:28 -05001943 }
Dai Haruki8c7396a2008-12-17 16:52:00 -08001944 if (likely(priv->txcoalescing)) {
1945 gfar_write(&priv->regs->txic, 0);
1946 gfar_write(&priv->regs->txic, priv->txic);
1947 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 }
1949
Andy Fleming42199882008-12-17 16:52:30 -08001950 return rx_cleaned;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952
Vitaly Woolf2d71c22006-11-07 13:27:02 +03001953#ifdef CONFIG_NET_POLL_CONTROLLER
1954/*
1955 * Polling 'interrupt' - used by things like netconsole to send skbs
1956 * without having to re-enable interrupts. It's not called while
1957 * the interrupt routine is executing.
1958 */
1959static void gfar_netpoll(struct net_device *dev)
1960{
1961 struct gfar_private *priv = netdev_priv(dev);
1962
1963 /* If the device has multiple interrupts, run tx/rx */
Andy Flemingb31a1d82008-12-16 15:29:15 -08001964 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
Vitaly Woolf2d71c22006-11-07 13:27:02 +03001965 disable_irq(priv->interruptTransmit);
1966 disable_irq(priv->interruptReceive);
1967 disable_irq(priv->interruptError);
1968 gfar_interrupt(priv->interruptTransmit, dev);
1969 enable_irq(priv->interruptError);
1970 enable_irq(priv->interruptReceive);
1971 enable_irq(priv->interruptTransmit);
1972 } else {
1973 disable_irq(priv->interruptTransmit);
1974 gfar_interrupt(priv->interruptTransmit, dev);
1975 enable_irq(priv->interruptTransmit);
1976 }
1977}
1978#endif
1979
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980/* The interrupt handler for devices with one interrupt */
David Howells7d12e782006-10-05 14:55:46 +01001981static irqreturn_t gfar_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982{
1983 struct net_device *dev = dev_id;
1984 struct gfar_private *priv = netdev_priv(dev);
1985
1986 /* Save ievent for future reference */
1987 u32 events = gfar_read(&priv->regs->ievent);
1988
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 /* Check for reception */
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04001990 if (events & IEVENT_RX_MASK)
David Howells7d12e782006-10-05 14:55:46 +01001991 gfar_receive(irq, dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992
1993 /* Check for transmit completion */
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04001994 if (events & IEVENT_TX_MASK)
David Howells7d12e782006-10-05 14:55:46 +01001995 gfar_transmit(irq, dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04001997 /* Check for errors */
1998 if (events & IEVENT_ERR_MASK)
1999 gfar_error(irq, dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000
2001 return IRQ_HANDLED;
2002}
2003
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004/* Called every time the controller might need to be made
2005 * aware of new link state. The PHY code conveys this
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002006 * information through variables in the phydev structure, and this
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 * function converts those variables into the appropriate
2008 * register values, and can bring down the device if needed.
2009 */
2010static void adjust_link(struct net_device *dev)
2011{
2012 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -06002013 struct gfar __iomem *regs = priv->regs;
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002014 unsigned long flags;
2015 struct phy_device *phydev = priv->phydev;
2016 int new_state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017
Andy Flemingfef61082006-04-20 16:44:29 -05002018 spin_lock_irqsave(&priv->txlock, flags);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002019 if (phydev->link) {
2020 u32 tempval = gfar_read(&regs->maccfg2);
Andy Fleming7f7f5312005-11-11 12:38:59 -06002021 u32 ecntrl = gfar_read(&regs->ecntrl);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002022
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 /* Now we make sure that we can be in full duplex mode.
2024 * If not, we operate in half-duplex mode. */
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002025 if (phydev->duplex != priv->oldduplex) {
2026 new_state = 1;
2027 if (!(phydev->duplex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 tempval &= ~(MACCFG2_FULL_DUPLEX);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002029 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 tempval |= MACCFG2_FULL_DUPLEX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002032 priv->oldduplex = phydev->duplex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 }
2034
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002035 if (phydev->speed != priv->oldspeed) {
2036 new_state = 1;
2037 switch (phydev->speed) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 case 1000:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 tempval =
2040 ((tempval & ~(MACCFG2_IF)) | MACCFG2_GMII);
Li Yangf430e492009-01-06 14:08:10 -08002041
2042 ecntrl &= ~(ECNTRL_R100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 break;
2044 case 100:
2045 case 10:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 tempval =
2047 ((tempval & ~(MACCFG2_IF)) | MACCFG2_MII);
Andy Fleming7f7f5312005-11-11 12:38:59 -06002048
2049 /* Reduced mode distinguishes
2050 * between 10 and 100 */
2051 if (phydev->speed == SPEED_100)
2052 ecntrl |= ECNTRL_R100;
2053 else
2054 ecntrl &= ~(ECNTRL_R100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 break;
2056 default:
Kumar Gala0bbaf062005-06-20 10:54:21 -05002057 if (netif_msg_link(priv))
2058 printk(KERN_WARNING
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002059 "%s: Ack! Speed (%d) is not 10/100/1000!\n",
2060 dev->name, phydev->speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 break;
2062 }
2063
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002064 priv->oldspeed = phydev->speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 }
2066
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002067 gfar_write(&regs->maccfg2, tempval);
Andy Fleming7f7f5312005-11-11 12:38:59 -06002068 gfar_write(&regs->ecntrl, ecntrl);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002069
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 if (!priv->oldlink) {
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002071 new_state = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 priv->oldlink = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 }
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002074 } else if (priv->oldlink) {
2075 new_state = 1;
2076 priv->oldlink = 0;
2077 priv->oldspeed = 0;
2078 priv->oldduplex = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002081 if (new_state && netif_msg_link(priv))
2082 phy_print_status(phydev);
2083
Andy Flemingfef61082006-04-20 16:44:29 -05002084 spin_unlock_irqrestore(&priv->txlock, flags);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002085}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086
2087/* Update the hash table based on the current list of multicast
2088 * addresses we subscribe to. Also, change the promiscuity of
2089 * the device based on the flags (this function is called
2090 * whenever dev->flags is changed */
2091static void gfar_set_multi(struct net_device *dev)
2092{
2093 struct dev_mc_list *mc_ptr;
2094 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -06002095 struct gfar __iomem *regs = priv->regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 u32 tempval;
2097
2098 if(dev->flags & IFF_PROMISC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 /* Set RCTRL to PROM */
2100 tempval = gfar_read(&regs->rctrl);
2101 tempval |= RCTRL_PROM;
2102 gfar_write(&regs->rctrl, tempval);
2103 } else {
2104 /* Set RCTRL to not PROM */
2105 tempval = gfar_read(&regs->rctrl);
2106 tempval &= ~(RCTRL_PROM);
2107 gfar_write(&regs->rctrl, tempval);
2108 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002109
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 if(dev->flags & IFF_ALLMULTI) {
2111 /* Set the hash to rx all multicast frames */
Kumar Gala0bbaf062005-06-20 10:54:21 -05002112 gfar_write(&regs->igaddr0, 0xffffffff);
2113 gfar_write(&regs->igaddr1, 0xffffffff);
2114 gfar_write(&regs->igaddr2, 0xffffffff);
2115 gfar_write(&regs->igaddr3, 0xffffffff);
2116 gfar_write(&regs->igaddr4, 0xffffffff);
2117 gfar_write(&regs->igaddr5, 0xffffffff);
2118 gfar_write(&regs->igaddr6, 0xffffffff);
2119 gfar_write(&regs->igaddr7, 0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 gfar_write(&regs->gaddr0, 0xffffffff);
2121 gfar_write(&regs->gaddr1, 0xffffffff);
2122 gfar_write(&regs->gaddr2, 0xffffffff);
2123 gfar_write(&regs->gaddr3, 0xffffffff);
2124 gfar_write(&regs->gaddr4, 0xffffffff);
2125 gfar_write(&regs->gaddr5, 0xffffffff);
2126 gfar_write(&regs->gaddr6, 0xffffffff);
2127 gfar_write(&regs->gaddr7, 0xffffffff);
2128 } else {
Andy Fleming7f7f5312005-11-11 12:38:59 -06002129 int em_num;
2130 int idx;
2131
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 /* zero out the hash */
Kumar Gala0bbaf062005-06-20 10:54:21 -05002133 gfar_write(&regs->igaddr0, 0x0);
2134 gfar_write(&regs->igaddr1, 0x0);
2135 gfar_write(&regs->igaddr2, 0x0);
2136 gfar_write(&regs->igaddr3, 0x0);
2137 gfar_write(&regs->igaddr4, 0x0);
2138 gfar_write(&regs->igaddr5, 0x0);
2139 gfar_write(&regs->igaddr6, 0x0);
2140 gfar_write(&regs->igaddr7, 0x0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 gfar_write(&regs->gaddr0, 0x0);
2142 gfar_write(&regs->gaddr1, 0x0);
2143 gfar_write(&regs->gaddr2, 0x0);
2144 gfar_write(&regs->gaddr3, 0x0);
2145 gfar_write(&regs->gaddr4, 0x0);
2146 gfar_write(&regs->gaddr5, 0x0);
2147 gfar_write(&regs->gaddr6, 0x0);
2148 gfar_write(&regs->gaddr7, 0x0);
2149
Andy Fleming7f7f5312005-11-11 12:38:59 -06002150 /* If we have extended hash tables, we need to
2151 * clear the exact match registers to prepare for
2152 * setting them */
2153 if (priv->extended_hash) {
2154 em_num = GFAR_EM_NUM + 1;
2155 gfar_clear_exact_match(dev);
2156 idx = 1;
2157 } else {
2158 idx = 0;
2159 em_num = 0;
2160 }
2161
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 if(dev->mc_count == 0)
2163 return;
2164
2165 /* Parse the list, and set the appropriate bits */
2166 for(mc_ptr = dev->mc_list; mc_ptr; mc_ptr = mc_ptr->next) {
Andy Fleming7f7f5312005-11-11 12:38:59 -06002167 if (idx < em_num) {
2168 gfar_set_mac_for_addr(dev, idx,
2169 mc_ptr->dmi_addr);
2170 idx++;
2171 } else
2172 gfar_set_hash_for_addr(dev, mc_ptr->dmi_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 }
2174 }
2175
2176 return;
2177}
2178
Andy Fleming7f7f5312005-11-11 12:38:59 -06002179
2180/* Clears each of the exact match registers to zero, so they
2181 * don't interfere with normal reception */
2182static void gfar_clear_exact_match(struct net_device *dev)
2183{
2184 int idx;
2185 u8 zero_arr[MAC_ADDR_LEN] = {0,0,0,0,0,0};
2186
2187 for(idx = 1;idx < GFAR_EM_NUM + 1;idx++)
2188 gfar_set_mac_for_addr(dev, idx, (u8 *)zero_arr);
2189}
2190
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191/* Set the appropriate hash bit for the given addr */
2192/* The algorithm works like so:
2193 * 1) Take the Destination Address (ie the multicast address), and
2194 * do a CRC on it (little endian), and reverse the bits of the
2195 * result.
2196 * 2) Use the 8 most significant bits as a hash into a 256-entry
2197 * table. The table is controlled through 8 32-bit registers:
2198 * gaddr0-7. gaddr0's MSB is entry 0, and gaddr7's LSB is
2199 * gaddr7. This means that the 3 most significant bits in the
2200 * hash index which gaddr register to use, and the 5 other bits
2201 * indicate which bit (assuming an IBM numbering scheme, which
2202 * for PowerPC (tm) is usually the case) in the register holds
2203 * the entry. */
2204static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr)
2205{
2206 u32 tempval;
2207 struct gfar_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 u32 result = ether_crc(MAC_ADDR_LEN, addr);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002209 int width = priv->hash_width;
2210 u8 whichbit = (result >> (32 - width)) & 0x1f;
2211 u8 whichreg = result >> (32 - width + 5);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 u32 value = (1 << (31-whichbit));
2213
Kumar Gala0bbaf062005-06-20 10:54:21 -05002214 tempval = gfar_read(priv->hash_regs[whichreg]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 tempval |= value;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002216 gfar_write(priv->hash_regs[whichreg], tempval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217
2218 return;
2219}
2220
Andy Fleming7f7f5312005-11-11 12:38:59 -06002221
2222/* There are multiple MAC Address register pairs on some controllers
2223 * This function sets the numth pair to a given address
2224 */
2225static void gfar_set_mac_for_addr(struct net_device *dev, int num, u8 *addr)
2226{
2227 struct gfar_private *priv = netdev_priv(dev);
2228 int idx;
2229 char tmpbuf[MAC_ADDR_LEN];
2230 u32 tempval;
Kumar Galacc8c6e32006-02-01 15:18:03 -06002231 u32 __iomem *macptr = &priv->regs->macstnaddr1;
Andy Fleming7f7f5312005-11-11 12:38:59 -06002232
2233 macptr += num*2;
2234
2235 /* Now copy it into the mac registers backwards, cuz */
2236 /* little endian is silly */
2237 for (idx = 0; idx < MAC_ADDR_LEN; idx++)
2238 tmpbuf[MAC_ADDR_LEN - 1 - idx] = addr[idx];
2239
2240 gfar_write(macptr, *((u32 *) (tmpbuf)));
2241
2242 tempval = *((u32 *) (tmpbuf + 4));
2243
2244 gfar_write(macptr+1, tempval);
2245}
2246
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247/* GFAR error interrupt handler */
David Howells7d12e782006-10-05 14:55:46 +01002248static irqreturn_t gfar_error(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249{
2250 struct net_device *dev = dev_id;
2251 struct gfar_private *priv = netdev_priv(dev);
2252
2253 /* Save ievent for future reference */
2254 u32 events = gfar_read(&priv->regs->ievent);
2255
2256 /* Clear IEVENT */
Scott Woodd87eb122008-07-11 18:04:45 -05002257 gfar_write(&priv->regs->ievent, events & IEVENT_ERR_MASK);
2258
2259 /* Magic Packet is not an error. */
Andy Flemingb31a1d82008-12-16 15:29:15 -08002260 if ((priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET) &&
Scott Woodd87eb122008-07-11 18:04:45 -05002261 (events & IEVENT_MAG))
2262 events &= ~IEVENT_MAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263
2264 /* Hmm... */
Kumar Gala0bbaf062005-06-20 10:54:21 -05002265 if (netif_msg_rx_err(priv) || netif_msg_tx_err(priv))
2266 printk(KERN_DEBUG "%s: error interrupt (ievent=0x%08x imask=0x%08x)\n",
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002267 dev->name, events, gfar_read(&priv->regs->imask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268
2269 /* Update the error counters */
2270 if (events & IEVENT_TXE) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002271 dev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272
2273 if (events & IEVENT_LC)
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002274 dev->stats.tx_window_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 if (events & IEVENT_CRL)
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002276 dev->stats.tx_aborted_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 if (events & IEVENT_XFUN) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05002278 if (netif_msg_tx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002279 printk(KERN_DEBUG "%s: TX FIFO underrun, "
2280 "packet dropped.\n", dev->name);
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002281 dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 priv->extra_stats.tx_underrun++;
2283
2284 /* Reactivate the Tx Queues */
2285 gfar_write(&priv->regs->tstat, TSTAT_CLEAR_THALT);
2286 }
Kumar Gala0bbaf062005-06-20 10:54:21 -05002287 if (netif_msg_tx_err(priv))
2288 printk(KERN_DEBUG "%s: Transmit Error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 }
2290 if (events & IEVENT_BSY) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002291 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 priv->extra_stats.rx_bsy++;
2293
David Howells7d12e782006-10-05 14:55:46 +01002294 gfar_receive(irq, dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295
Kumar Gala0bbaf062005-06-20 10:54:21 -05002296 if (netif_msg_rx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002297 printk(KERN_DEBUG "%s: busy error (rstat: %x)\n",
2298 dev->name, gfar_read(&priv->regs->rstat));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 }
2300 if (events & IEVENT_BABR) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002301 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 priv->extra_stats.rx_babr++;
2303
Kumar Gala0bbaf062005-06-20 10:54:21 -05002304 if (netif_msg_rx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002305 printk(KERN_DEBUG "%s: babbling RX error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 }
2307 if (events & IEVENT_EBERR) {
2308 priv->extra_stats.eberr++;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002309 if (netif_msg_rx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002310 printk(KERN_DEBUG "%s: bus error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 }
Kumar Gala0bbaf062005-06-20 10:54:21 -05002312 if ((events & IEVENT_RXC) && netif_msg_rx_status(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002313 printk(KERN_DEBUG "%s: control frame\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314
2315 if (events & IEVENT_BABT) {
2316 priv->extra_stats.tx_babt++;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002317 if (netif_msg_tx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002318 printk(KERN_DEBUG "%s: babbling TX error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 }
2320 return IRQ_HANDLED;
2321}
2322
Kay Sievers72abb462008-04-18 13:50:44 -07002323/* work with hotplug and coldplug */
2324MODULE_ALIAS("platform:fsl-gianfar");
2325
Andy Flemingb31a1d82008-12-16 15:29:15 -08002326static struct of_device_id gfar_match[] =
2327{
2328 {
2329 .type = "network",
2330 .compatible = "gianfar",
2331 },
2332 {},
2333};
2334
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335/* Structure for a device driver */
Andy Flemingb31a1d82008-12-16 15:29:15 -08002336static struct of_platform_driver gfar_driver = {
2337 .name = "fsl-gianfar",
2338 .match_table = gfar_match,
2339
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 .probe = gfar_probe,
2341 .remove = gfar_remove,
Scott Woodd87eb122008-07-11 18:04:45 -05002342 .suspend = gfar_suspend,
2343 .resume = gfar_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344};
2345
2346static int __init gfar_init(void)
2347{
Andy Fleming1577ece2009-02-04 16:42:12 -08002348 return of_register_platform_driver(&gfar_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349}
2350
2351static void __exit gfar_exit(void)
2352{
Andy Flemingb31a1d82008-12-16 15:29:15 -08002353 of_unregister_platform_driver(&gfar_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354}
2355
2356module_init(gfar_init);
2357module_exit(gfar_exit);
2358