bcmgenet: Deleted unnecessary select_queue method.
[linux-block.git] / drivers / net / ethernet / freescale / gianfar.c
CommitLineData
0977f817 1/* drivers/net/ethernet/freescale/gianfar.c
1da177e4
LT
2 *
3 * Gianfar Ethernet Driver
7f7f5316
AF
4 * This driver is designed for the non-CPM ethernet controllers
5 * on the 85xx and 83xx family of integrated processors
1da177e4
LT
6 * Based on 8260_io/fcc_enet.c
7 *
8 * Author: Andy Fleming
4c8d3d99 9 * Maintainer: Kumar Gala
a12f801d 10 * Modifier: Sandeep Gopalpet <sandeep.kumar@freescale.com>
1da177e4 11 *
20862788 12 * Copyright 2002-2009, 2011-2013 Freescale Semiconductor, Inc.
a12f801d 13 * Copyright 2007 MontaVista Software, Inc.
1da177e4
LT
14 *
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
0bbaf069 27 *
b31a1d8b
AF
28 * The driver is initialized through of_device. Configuration information
29 * is therefore conveyed through an OF-style device tree.
1da177e4
LT
30 *
31 * The Gianfar Ethernet Controller uses a ring of buffer
32 * descriptors. The beginning is indicated by a register
0bbaf069
KG
33 * pointing to the physical address of the start of the ring.
34 * The end is determined by a "wrap" bit being set in the
1da177e4
LT
35 * last descriptor of the ring.
36 *
37 * When a packet is received, the RXF bit in the
0bbaf069 38 * IEVENT register is set, triggering an interrupt when the
1da177e4
LT
39 * 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
bb40dcbb 42 * of frames or amount of time have passed). In NAPI, the
1da177e4 43 * interrupt handler will signal there is work to be done, and
0aa1538f 44 * exit. This method will start at the last known empty
0bbaf069 45 * descriptor, and process every subsequent descriptor until there
1da177e4
LT
46 * 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
59deab26
JP
64#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
65#define DEBUG
66
1da177e4 67#include <linux/kernel.h>
1da177e4
LT
68#include <linux/string.h>
69#include <linux/errno.h>
bb40dcbb 70#include <linux/unistd.h>
1da177e4
LT
71#include <linux/slab.h>
72#include <linux/interrupt.h>
1da177e4
LT
73#include <linux/delay.h>
74#include <linux/netdevice.h>
75#include <linux/etherdevice.h>
76#include <linux/skbuff.h>
0bbaf069 77#include <linux/if_vlan.h>
1da177e4
LT
78#include <linux/spinlock.h>
79#include <linux/mm.h>
5af50730
RH
80#include <linux/of_address.h>
81#include <linux/of_irq.h>
fe192a49 82#include <linux/of_mdio.h>
b31a1d8b 83#include <linux/of_platform.h>
0bbaf069
KG
84#include <linux/ip.h>
85#include <linux/tcp.h>
86#include <linux/udp.h>
9c07b884 87#include <linux/in.h>
cc772ab7 88#include <linux/net_tstamp.h>
1da177e4
LT
89
90#include <asm/io.h>
7d350977 91#include <asm/reg.h>
2969b1f7 92#include <asm/mpc85xx.h>
1da177e4
LT
93#include <asm/irq.h>
94#include <asm/uaccess.h>
95#include <linux/module.h>
1da177e4
LT
96#include <linux/dma-mapping.h>
97#include <linux/crc32.h>
bb40dcbb
AF
98#include <linux/mii.h>
99#include <linux/phy.h>
b31a1d8b
AF
100#include <linux/phy_fixed.h>
101#include <linux/of.h>
4b6ba8aa 102#include <linux/of_net.h>
1da177e4
LT
103
104#include "gianfar.h"
1da177e4
LT
105
106#define TX_TIMEOUT (1*HZ)
1da177e4 107
7f7f5316 108const char gfar_driver_version[] = "1.3";
1da177e4 109
1da177e4
LT
110static int gfar_enet_open(struct net_device *dev);
111static int gfar_start_xmit(struct sk_buff *skb, struct net_device *dev);
ab939905 112static void gfar_reset_task(struct work_struct *work);
1da177e4
LT
113static void gfar_timeout(struct net_device *dev);
114static int gfar_close(struct net_device *dev);
815b97c6 115struct sk_buff *gfar_new_skb(struct net_device *dev);
a12f801d 116static void gfar_new_rxbdp(struct gfar_priv_rx_q *rx_queue, struct rxbd8 *bdp,
bc4598bc 117 struct sk_buff *skb);
1da177e4
LT
118static int gfar_set_mac_address(struct net_device *dev);
119static int gfar_change_mtu(struct net_device *dev, int new_mtu);
7d12e780
DH
120static irqreturn_t gfar_error(int irq, void *dev_id);
121static irqreturn_t gfar_transmit(int irq, void *dev_id);
122static irqreturn_t gfar_interrupt(int irq, void *dev_id);
1da177e4
LT
123static void adjust_link(struct net_device *dev);
124static void init_registers(struct net_device *dev);
125static int init_phy(struct net_device *dev);
74888760 126static int gfar_probe(struct platform_device *ofdev);
2dc11581 127static int gfar_remove(struct platform_device *ofdev);
bb40dcbb 128static void free_skb_resources(struct gfar_private *priv);
1da177e4
LT
129static void gfar_set_multi(struct net_device *dev);
130static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr);
d3c12873 131static void gfar_configure_serdes(struct net_device *dev);
bea3348e 132static int gfar_poll(struct napi_struct *napi, int budget);
5eaedf31 133static int gfar_poll_sq(struct napi_struct *napi, int budget);
f2d71c2d
VW
134#ifdef CONFIG_NET_POLL_CONTROLLER
135static void gfar_netpoll(struct net_device *dev);
136#endif
a12f801d 137int gfar_clean_rx_ring(struct gfar_priv_rx_q *rx_queue, int rx_work_limit);
c233cf40 138static void gfar_clean_tx_ring(struct gfar_priv_tx_q *tx_queue);
61db26c6
CM
139static void gfar_process_frame(struct net_device *dev, struct sk_buff *skb,
140 int amount_pull, struct napi_struct *napi);
c10650b6 141static void gfar_halt_nodisable(struct gfar_private *priv);
7f7f5316 142static void gfar_clear_exact_match(struct net_device *dev);
b6bc7650
JP
143static void gfar_set_mac_for_addr(struct net_device *dev, int num,
144 const u8 *addr);
26ccfc37 145static int gfar_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
1da177e4 146
1da177e4
LT
147MODULE_AUTHOR("Freescale Semiconductor, Inc");
148MODULE_DESCRIPTION("Gianfar Ethernet Driver");
149MODULE_LICENSE("GPL");
150
a12f801d 151static void gfar_init_rxbdp(struct gfar_priv_rx_q *rx_queue, struct rxbd8 *bdp,
8a102fe0
AV
152 dma_addr_t buf)
153{
8a102fe0
AV
154 u32 lstatus;
155
156 bdp->bufPtr = buf;
157
158 lstatus = BD_LFLAG(RXBD_EMPTY | RXBD_INTERRUPT);
a12f801d 159 if (bdp == rx_queue->rx_bd_base + rx_queue->rx_ring_size - 1)
8a102fe0
AV
160 lstatus |= BD_LFLAG(RXBD_WRAP);
161
162 eieio();
163
164 bdp->lstatus = lstatus;
165}
166
8728327e 167static int gfar_init_bds(struct net_device *ndev)
826aa4a0 168{
8728327e 169 struct gfar_private *priv = netdev_priv(ndev);
a12f801d
SG
170 struct gfar_priv_tx_q *tx_queue = NULL;
171 struct gfar_priv_rx_q *rx_queue = NULL;
826aa4a0
AV
172 struct txbd8 *txbdp;
173 struct rxbd8 *rxbdp;
fba4ed03 174 int i, j;
a12f801d 175
fba4ed03
SG
176 for (i = 0; i < priv->num_tx_queues; i++) {
177 tx_queue = priv->tx_queue[i];
178 /* Initialize some variables in our dev structure */
179 tx_queue->num_txbdfree = tx_queue->tx_ring_size;
180 tx_queue->dirty_tx = tx_queue->tx_bd_base;
181 tx_queue->cur_tx = tx_queue->tx_bd_base;
182 tx_queue->skb_curtx = 0;
183 tx_queue->skb_dirtytx = 0;
184
185 /* Initialize Transmit Descriptor Ring */
186 txbdp = tx_queue->tx_bd_base;
187 for (j = 0; j < tx_queue->tx_ring_size; j++) {
188 txbdp->lstatus = 0;
189 txbdp->bufPtr = 0;
190 txbdp++;
191 }
8728327e 192
fba4ed03
SG
193 /* Set the last descriptor in the ring to indicate wrap */
194 txbdp--;
195 txbdp->status |= TXBD_WRAP;
8728327e
AV
196 }
197
fba4ed03
SG
198 for (i = 0; i < priv->num_rx_queues; i++) {
199 rx_queue = priv->rx_queue[i];
200 rx_queue->cur_rx = rx_queue->rx_bd_base;
201 rx_queue->skb_currx = 0;
202 rxbdp = rx_queue->rx_bd_base;
8728327e 203
fba4ed03
SG
204 for (j = 0; j < rx_queue->rx_ring_size; j++) {
205 struct sk_buff *skb = rx_queue->rx_skbuff[j];
8728327e 206
fba4ed03
SG
207 if (skb) {
208 gfar_init_rxbdp(rx_queue, rxbdp,
209 rxbdp->bufPtr);
210 } else {
211 skb = gfar_new_skb(ndev);
212 if (!skb) {
59deab26 213 netdev_err(ndev, "Can't allocate RX buffers\n");
1eb8f7a7 214 return -ENOMEM;
fba4ed03
SG
215 }
216 rx_queue->rx_skbuff[j] = skb;
217
218 gfar_new_rxbdp(rx_queue, rxbdp, skb);
8728327e 219 }
8728327e 220
fba4ed03 221 rxbdp++;
8728327e
AV
222 }
223
8728327e
AV
224 }
225
226 return 0;
227}
228
229static int gfar_alloc_skb_resources(struct net_device *ndev)
230{
826aa4a0 231 void *vaddr;
fba4ed03
SG
232 dma_addr_t addr;
233 int i, j, k;
826aa4a0 234 struct gfar_private *priv = netdev_priv(ndev);
369ec162 235 struct device *dev = priv->dev;
a12f801d
SG
236 struct gfar_priv_tx_q *tx_queue = NULL;
237 struct gfar_priv_rx_q *rx_queue = NULL;
238
fba4ed03
SG
239 priv->total_tx_ring_size = 0;
240 for (i = 0; i < priv->num_tx_queues; i++)
241 priv->total_tx_ring_size += priv->tx_queue[i]->tx_ring_size;
242
243 priv->total_rx_ring_size = 0;
244 for (i = 0; i < priv->num_rx_queues; i++)
245 priv->total_rx_ring_size += priv->rx_queue[i]->rx_ring_size;
826aa4a0
AV
246
247 /* Allocate memory for the buffer descriptors */
8728327e 248 vaddr = dma_alloc_coherent(dev,
d0320f75
JP
249 (priv->total_tx_ring_size *
250 sizeof(struct txbd8)) +
251 (priv->total_rx_ring_size *
252 sizeof(struct rxbd8)),
253 &addr, GFP_KERNEL);
254 if (!vaddr)
826aa4a0 255 return -ENOMEM;
826aa4a0 256
fba4ed03
SG
257 for (i = 0; i < priv->num_tx_queues; i++) {
258 tx_queue = priv->tx_queue[i];
43d620c8 259 tx_queue->tx_bd_base = vaddr;
fba4ed03
SG
260 tx_queue->tx_bd_dma_base = addr;
261 tx_queue->dev = ndev;
262 /* enet DMA only understands physical addresses */
bc4598bc
JC
263 addr += sizeof(struct txbd8) * tx_queue->tx_ring_size;
264 vaddr += sizeof(struct txbd8) * tx_queue->tx_ring_size;
fba4ed03 265 }
826aa4a0 266
826aa4a0 267 /* Start the rx descriptor ring where the tx ring leaves off */
fba4ed03
SG
268 for (i = 0; i < priv->num_rx_queues; i++) {
269 rx_queue = priv->rx_queue[i];
43d620c8 270 rx_queue->rx_bd_base = vaddr;
fba4ed03
SG
271 rx_queue->rx_bd_dma_base = addr;
272 rx_queue->dev = ndev;
bc4598bc
JC
273 addr += sizeof(struct rxbd8) * rx_queue->rx_ring_size;
274 vaddr += sizeof(struct rxbd8) * rx_queue->rx_ring_size;
fba4ed03 275 }
826aa4a0
AV
276
277 /* Setup the skbuff rings */
fba4ed03
SG
278 for (i = 0; i < priv->num_tx_queues; i++) {
279 tx_queue = priv->tx_queue[i];
14f8dc49
JP
280 tx_queue->tx_skbuff =
281 kmalloc_array(tx_queue->tx_ring_size,
282 sizeof(*tx_queue->tx_skbuff),
283 GFP_KERNEL);
284 if (!tx_queue->tx_skbuff)
fba4ed03 285 goto cleanup;
826aa4a0 286
fba4ed03
SG
287 for (k = 0; k < tx_queue->tx_ring_size; k++)
288 tx_queue->tx_skbuff[k] = NULL;
289 }
826aa4a0 290
fba4ed03
SG
291 for (i = 0; i < priv->num_rx_queues; i++) {
292 rx_queue = priv->rx_queue[i];
14f8dc49
JP
293 rx_queue->rx_skbuff =
294 kmalloc_array(rx_queue->rx_ring_size,
295 sizeof(*rx_queue->rx_skbuff),
296 GFP_KERNEL);
297 if (!rx_queue->rx_skbuff)
fba4ed03 298 goto cleanup;
fba4ed03
SG
299
300 for (j = 0; j < rx_queue->rx_ring_size; j++)
301 rx_queue->rx_skbuff[j] = NULL;
302 }
826aa4a0 303
8728327e
AV
304 if (gfar_init_bds(ndev))
305 goto cleanup;
826aa4a0
AV
306
307 return 0;
308
309cleanup:
310 free_skb_resources(priv);
311 return -ENOMEM;
312}
313
fba4ed03
SG
314static void gfar_init_tx_rx_base(struct gfar_private *priv)
315{
46ceb60c 316 struct gfar __iomem *regs = priv->gfargrp[0].regs;
18294ad1 317 u32 __iomem *baddr;
fba4ed03
SG
318 int i;
319
320 baddr = &regs->tbase0;
bc4598bc 321 for (i = 0; i < priv->num_tx_queues; i++) {
fba4ed03 322 gfar_write(baddr, priv->tx_queue[i]->tx_bd_dma_base);
bc4598bc 323 baddr += 2;
fba4ed03
SG
324 }
325
326 baddr = &regs->rbase0;
bc4598bc 327 for (i = 0; i < priv->num_rx_queues; i++) {
fba4ed03 328 gfar_write(baddr, priv->rx_queue[i]->rx_bd_dma_base);
bc4598bc 329 baddr += 2;
fba4ed03
SG
330 }
331}
332
826aa4a0
AV
333static void gfar_init_mac(struct net_device *ndev)
334{
335 struct gfar_private *priv = netdev_priv(ndev);
46ceb60c 336 struct gfar __iomem *regs = priv->gfargrp[0].regs;
826aa4a0
AV
337 u32 rctrl = 0;
338 u32 tctrl = 0;
826aa4a0 339
fba4ed03
SG
340 /* write the tx/rx base registers */
341 gfar_init_tx_rx_base(priv);
32c513bc 342
826aa4a0 343 /* Configure the coalescing support */
800c644b 344 gfar_configure_coalescing_all(priv);
fba4ed03 345
ba779711
CM
346 /* set this when rx hw offload (TOE) functions are being used */
347 priv->uses_rxfcb = 0;
348
1ccb8389 349 if (priv->rx_filer_enable) {
fba4ed03 350 rctrl |= RCTRL_FILREN;
1ccb8389
SG
351 /* Program the RIR0 reg with the required distribution */
352 gfar_write(&regs->rir0, DEFAULT_RIR0);
353 }
826aa4a0 354
f5ae6279
CM
355 /* Restore PROMISC mode */
356 if (ndev->flags & IFF_PROMISC)
357 rctrl |= RCTRL_PROM;
358
ba779711 359 if (ndev->features & NETIF_F_RXCSUM) {
826aa4a0 360 rctrl |= RCTRL_CHECKSUMMING;
ba779711
CM
361 priv->uses_rxfcb = 1;
362 }
826aa4a0
AV
363
364 if (priv->extended_hash) {
365 rctrl |= RCTRL_EXTHASH;
366
367 gfar_clear_exact_match(ndev);
368 rctrl |= RCTRL_EMEN;
369 }
370
371 if (priv->padding) {
372 rctrl &= ~RCTRL_PAL_MASK;
373 rctrl |= RCTRL_PADDING(priv->padding);
374 }
375
97553f7f 376 /* Enable HW time stamping if requested from user space */
ba779711 377 if (priv->hwts_rx_en) {
97553f7f 378 rctrl |= RCTRL_PRSDEP_INIT | RCTRL_TS_ENABLE;
ba779711
CM
379 priv->uses_rxfcb = 1;
380 }
97553f7f 381
f646968f 382 if (ndev->features & NETIF_F_HW_VLAN_CTAG_RX) {
b852b720 383 rctrl |= RCTRL_VLEX | RCTRL_PRSDEP_INIT;
ba779711
CM
384 priv->uses_rxfcb = 1;
385 }
826aa4a0
AV
386
387 /* Init rctrl based on our settings */
388 gfar_write(&regs->rctrl, rctrl);
389
390 if (ndev->features & NETIF_F_IP_CSUM)
391 tctrl |= TCTRL_INIT_CSUM;
392
b98b8bab
CM
393 if (priv->prio_sched_en)
394 tctrl |= TCTRL_TXSCHED_PRIO;
395 else {
396 tctrl |= TCTRL_TXSCHED_WRRS;
397 gfar_write(&regs->tr03wt, DEFAULT_WRRS_WEIGHT);
398 gfar_write(&regs->tr47wt, DEFAULT_WRRS_WEIGHT);
399 }
fba4ed03 400
826aa4a0 401 gfar_write(&regs->tctrl, tctrl);
826aa4a0
AV
402}
403
a7f38041
SG
404static struct net_device_stats *gfar_get_stats(struct net_device *dev)
405{
406 struct gfar_private *priv = netdev_priv(dev);
a7f38041
SG
407 unsigned long rx_packets = 0, rx_bytes = 0, rx_dropped = 0;
408 unsigned long tx_packets = 0, tx_bytes = 0;
3a2e16c8 409 int i;
a7f38041
SG
410
411 for (i = 0; i < priv->num_rx_queues; i++) {
412 rx_packets += priv->rx_queue[i]->stats.rx_packets;
bc4598bc 413 rx_bytes += priv->rx_queue[i]->stats.rx_bytes;
a7f38041
SG
414 rx_dropped += priv->rx_queue[i]->stats.rx_dropped;
415 }
416
417 dev->stats.rx_packets = rx_packets;
bc4598bc 418 dev->stats.rx_bytes = rx_bytes;
a7f38041
SG
419 dev->stats.rx_dropped = rx_dropped;
420
421 for (i = 0; i < priv->num_tx_queues; i++) {
1ac9ad13
ED
422 tx_bytes += priv->tx_queue[i]->stats.tx_bytes;
423 tx_packets += priv->tx_queue[i]->stats.tx_packets;
a7f38041
SG
424 }
425
bc4598bc 426 dev->stats.tx_bytes = tx_bytes;
a7f38041
SG
427 dev->stats.tx_packets = tx_packets;
428
429 return &dev->stats;
430}
431
26ccfc37
AF
432static const struct net_device_ops gfar_netdev_ops = {
433 .ndo_open = gfar_enet_open,
434 .ndo_start_xmit = gfar_start_xmit,
435 .ndo_stop = gfar_close,
436 .ndo_change_mtu = gfar_change_mtu,
8b3afe95 437 .ndo_set_features = gfar_set_features,
afc4b13d 438 .ndo_set_rx_mode = gfar_set_multi,
26ccfc37
AF
439 .ndo_tx_timeout = gfar_timeout,
440 .ndo_do_ioctl = gfar_ioctl,
a7f38041 441 .ndo_get_stats = gfar_get_stats,
240c102d
BH
442 .ndo_set_mac_address = eth_mac_addr,
443 .ndo_validate_addr = eth_validate_addr,
26ccfc37
AF
444#ifdef CONFIG_NET_POLL_CONTROLLER
445 .ndo_poll_controller = gfar_netpoll,
446#endif
447};
448
efeddce7
CM
449static void gfar_ints_disable(struct gfar_private *priv)
450{
451 int i;
452 for (i = 0; i < priv->num_grps; i++) {
453 struct gfar __iomem *regs = priv->gfargrp[i].regs;
454 /* Clear IEVENT */
455 gfar_write(&regs->ievent, IEVENT_INIT_CLEAR);
456
457 /* Initialize IMASK */
458 gfar_write(&regs->imask, IMASK_INIT_CLEAR);
459 }
460}
461
462static void gfar_ints_enable(struct gfar_private *priv)
463{
464 int i;
465 for (i = 0; i < priv->num_grps; i++) {
466 struct gfar __iomem *regs = priv->gfargrp[i].regs;
467 /* Unmask the interrupts we look for */
468 gfar_write(&regs->imask, IMASK_DEFAULT);
469 }
470}
471
fba4ed03
SG
472void lock_rx_qs(struct gfar_private *priv)
473{
3a2e16c8 474 int i;
fba4ed03
SG
475
476 for (i = 0; i < priv->num_rx_queues; i++)
477 spin_lock(&priv->rx_queue[i]->rxlock);
478}
479
480void lock_tx_qs(struct gfar_private *priv)
481{
3a2e16c8 482 int i;
fba4ed03
SG
483
484 for (i = 0; i < priv->num_tx_queues; i++)
485 spin_lock(&priv->tx_queue[i]->txlock);
486}
487
488void unlock_rx_qs(struct gfar_private *priv)
489{
3a2e16c8 490 int i;
fba4ed03
SG
491
492 for (i = 0; i < priv->num_rx_queues; i++)
493 spin_unlock(&priv->rx_queue[i]->rxlock);
494}
495
496void unlock_tx_qs(struct gfar_private *priv)
497{
3a2e16c8 498 int i;
fba4ed03
SG
499
500 for (i = 0; i < priv->num_tx_queues; i++)
501 spin_unlock(&priv->tx_queue[i]->txlock);
502}
503
20862788
CM
504static int gfar_alloc_tx_queues(struct gfar_private *priv)
505{
506 int i;
507
508 for (i = 0; i < priv->num_tx_queues; i++) {
509 priv->tx_queue[i] = kzalloc(sizeof(struct gfar_priv_tx_q),
510 GFP_KERNEL);
511 if (!priv->tx_queue[i])
512 return -ENOMEM;
513
514 priv->tx_queue[i]->tx_skbuff = NULL;
515 priv->tx_queue[i]->qindex = i;
516 priv->tx_queue[i]->dev = priv->ndev;
517 spin_lock_init(&(priv->tx_queue[i]->txlock));
518 }
519 return 0;
520}
521
522static int gfar_alloc_rx_queues(struct gfar_private *priv)
523{
524 int i;
525
526 for (i = 0; i < priv->num_rx_queues; i++) {
527 priv->rx_queue[i] = kzalloc(sizeof(struct gfar_priv_rx_q),
528 GFP_KERNEL);
529 if (!priv->rx_queue[i])
530 return -ENOMEM;
531
532 priv->rx_queue[i]->rx_skbuff = NULL;
533 priv->rx_queue[i]->qindex = i;
534 priv->rx_queue[i]->dev = priv->ndev;
535 spin_lock_init(&(priv->rx_queue[i]->rxlock));
536 }
537 return 0;
538}
539
540static void gfar_free_tx_queues(struct gfar_private *priv)
fba4ed03 541{
3a2e16c8 542 int i;
fba4ed03
SG
543
544 for (i = 0; i < priv->num_tx_queues; i++)
545 kfree(priv->tx_queue[i]);
546}
547
20862788 548static void gfar_free_rx_queues(struct gfar_private *priv)
fba4ed03 549{
3a2e16c8 550 int i;
fba4ed03
SG
551
552 for (i = 0; i < priv->num_rx_queues; i++)
553 kfree(priv->rx_queue[i]);
554}
555
46ceb60c
SG
556static void unmap_group_regs(struct gfar_private *priv)
557{
3a2e16c8 558 int i;
46ceb60c
SG
559
560 for (i = 0; i < MAXGROUPS; i++)
561 if (priv->gfargrp[i].regs)
562 iounmap(priv->gfargrp[i].regs);
563}
564
ee873fda
CM
565static void free_gfar_dev(struct gfar_private *priv)
566{
567 int i, j;
568
569 for (i = 0; i < priv->num_grps; i++)
570 for (j = 0; j < GFAR_NUM_IRQS; j++) {
571 kfree(priv->gfargrp[i].irqinfo[j]);
572 priv->gfargrp[i].irqinfo[j] = NULL;
573 }
574
575 free_netdev(priv->ndev);
576}
577
46ceb60c
SG
578static void disable_napi(struct gfar_private *priv)
579{
3a2e16c8 580 int i;
46ceb60c
SG
581
582 for (i = 0; i < priv->num_grps; i++)
583 napi_disable(&priv->gfargrp[i].napi);
584}
585
586static void enable_napi(struct gfar_private *priv)
587{
3a2e16c8 588 int i;
46ceb60c
SG
589
590 for (i = 0; i < priv->num_grps; i++)
591 napi_enable(&priv->gfargrp[i].napi);
592}
593
594static int gfar_parse_group(struct device_node *np,
bc4598bc 595 struct gfar_private *priv, const char *model)
46ceb60c 596{
5fedcc14 597 struct gfar_priv_grp *grp = &priv->gfargrp[priv->num_grps];
46ceb60c 598 u32 *queue_mask;
ee873fda
CM
599 int i;
600
7c1e7e99
PG
601 for (i = 0; i < GFAR_NUM_IRQS; i++) {
602 grp->irqinfo[i] = kzalloc(sizeof(struct gfar_irqinfo),
603 GFP_KERNEL);
604 if (!grp->irqinfo[i])
ee873fda 605 return -ENOMEM;
ee873fda 606 }
46ceb60c 607
5fedcc14
CM
608 grp->regs = of_iomap(np, 0);
609 if (!grp->regs)
46ceb60c
SG
610 return -ENOMEM;
611
ee873fda 612 gfar_irq(grp, TX)->irq = irq_of_parse_and_map(np, 0);
46ceb60c
SG
613
614 /* If we aren't the FEC we have multiple interrupts */
615 if (model && strcasecmp(model, "FEC")) {
ee873fda
CM
616 gfar_irq(grp, RX)->irq = irq_of_parse_and_map(np, 1);
617 gfar_irq(grp, ER)->irq = irq_of_parse_and_map(np, 2);
618 if (gfar_irq(grp, TX)->irq == NO_IRQ ||
619 gfar_irq(grp, RX)->irq == NO_IRQ ||
620 gfar_irq(grp, ER)->irq == NO_IRQ)
46ceb60c 621 return -EINVAL;
46ceb60c
SG
622 }
623
5fedcc14
CM
624 grp->priv = priv;
625 spin_lock_init(&grp->grplock);
bc4598bc
JC
626 if (priv->mode == MQ_MG_MODE) {
627 queue_mask = (u32 *)of_get_property(np, "fsl,rx-bit-map", NULL);
5fedcc14 628 grp->rx_bit_map = queue_mask ?
bc4598bc
JC
629 *queue_mask : (DEFAULT_MAPPING >> priv->num_grps);
630 queue_mask = (u32 *)of_get_property(np, "fsl,tx-bit-map", NULL);
5fedcc14 631 grp->tx_bit_map = queue_mask ?
bc4598bc 632 *queue_mask : (DEFAULT_MAPPING >> priv->num_grps);
46ceb60c 633 } else {
5fedcc14
CM
634 grp->rx_bit_map = 0xFF;
635 grp->tx_bit_map = 0xFF;
46ceb60c 636 }
20862788
CM
637
638 /* bit_map's MSB is q0 (from q0 to q7) but, for_each_set_bit parses
639 * right to left, so we need to revert the 8 bits to get the q index
640 */
641 grp->rx_bit_map = bitrev8(grp->rx_bit_map);
642 grp->tx_bit_map = bitrev8(grp->tx_bit_map);
643
644 /* Calculate RSTAT, TSTAT, RQUEUE and TQUEUE values,
645 * also assign queues to groups
646 */
647 for_each_set_bit(i, &grp->rx_bit_map, priv->num_rx_queues) {
648 grp->num_rx_queues++;
649 grp->rstat |= (RSTAT_CLEAR_RHALT >> i);
650 priv->rqueue |= ((RQUEUE_EN0 | RQUEUE_EX0) >> i);
651 priv->rx_queue[i]->grp = grp;
652 }
653
654 for_each_set_bit(i, &grp->tx_bit_map, priv->num_tx_queues) {
655 grp->num_tx_queues++;
656 grp->tstat |= (TSTAT_CLEAR_THALT >> i);
657 priv->tqueue |= (TQUEUE_EN0 >> i);
658 priv->tx_queue[i]->grp = grp;
659 }
660
46ceb60c
SG
661 priv->num_grps++;
662
663 return 0;
664}
665
2dc11581 666static int gfar_of_init(struct platform_device *ofdev, struct net_device **pdev)
b31a1d8b 667{
b31a1d8b
AF
668 const char *model;
669 const char *ctype;
670 const void *mac_addr;
fba4ed03
SG
671 int err = 0, i;
672 struct net_device *dev = NULL;
673 struct gfar_private *priv = NULL;
61c7a080 674 struct device_node *np = ofdev->dev.of_node;
46ceb60c 675 struct device_node *child = NULL;
4d7902f2
AF
676 const u32 *stash;
677 const u32 *stash_len;
678 const u32 *stash_idx;
fba4ed03
SG
679 unsigned int num_tx_qs, num_rx_qs;
680 u32 *tx_queues, *rx_queues;
b31a1d8b
AF
681
682 if (!np || !of_device_is_available(np))
683 return -ENODEV;
684
fba4ed03
SG
685 /* parse the num of tx and rx queues */
686 tx_queues = (u32 *)of_get_property(np, "fsl,num_tx_queues", NULL);
687 num_tx_qs = tx_queues ? *tx_queues : 1;
688
689 if (num_tx_qs > MAX_TX_QS) {
59deab26
JP
690 pr_err("num_tx_qs(=%d) greater than MAX_TX_QS(=%d)\n",
691 num_tx_qs, MAX_TX_QS);
692 pr_err("Cannot do alloc_etherdev, aborting\n");
fba4ed03
SG
693 return -EINVAL;
694 }
695
696 rx_queues = (u32 *)of_get_property(np, "fsl,num_rx_queues", NULL);
697 num_rx_qs = rx_queues ? *rx_queues : 1;
698
699 if (num_rx_qs > MAX_RX_QS) {
59deab26
JP
700 pr_err("num_rx_qs(=%d) greater than MAX_RX_QS(=%d)\n",
701 num_rx_qs, MAX_RX_QS);
702 pr_err("Cannot do alloc_etherdev, aborting\n");
fba4ed03
SG
703 return -EINVAL;
704 }
705
706 *pdev = alloc_etherdev_mq(sizeof(*priv), num_tx_qs);
707 dev = *pdev;
708 if (NULL == dev)
709 return -ENOMEM;
710
711 priv = netdev_priv(dev);
fba4ed03
SG
712 priv->ndev = dev;
713
fba4ed03 714 priv->num_tx_queues = num_tx_qs;
fe069123 715 netif_set_real_num_rx_queues(dev, num_rx_qs);
fba4ed03 716 priv->num_rx_queues = num_rx_qs;
20862788
CM
717
718 err = gfar_alloc_tx_queues(priv);
719 if (err)
720 goto tx_alloc_failed;
721
722 err = gfar_alloc_rx_queues(priv);
723 if (err)
724 goto rx_alloc_failed;
b31a1d8b 725
0977f817 726 /* Init Rx queue filer rule set linked list */
4aa3a715
SP
727 INIT_LIST_HEAD(&priv->rx_list.list);
728 priv->rx_list.count = 0;
729 mutex_init(&priv->rx_queue_access);
730
b31a1d8b
AF
731 model = of_get_property(np, "model", NULL);
732
46ceb60c
SG
733 for (i = 0; i < MAXGROUPS; i++)
734 priv->gfargrp[i].regs = NULL;
b31a1d8b 735
46ceb60c
SG
736 /* Parse and initialize group specific information */
737 if (of_device_is_compatible(np, "fsl,etsec2")) {
738 priv->mode = MQ_MG_MODE;
739 for_each_child_of_node(np, child) {
740 err = gfar_parse_group(child, priv, model);
741 if (err)
742 goto err_grp_init;
b31a1d8b 743 }
46ceb60c
SG
744 } else {
745 priv->mode = SQ_SG_MODE;
746 err = gfar_parse_group(np, priv, model);
bc4598bc 747 if (err)
46ceb60c 748 goto err_grp_init;
b31a1d8b
AF
749 }
750
4d7902f2
AF
751 stash = of_get_property(np, "bd-stash", NULL);
752
a12f801d 753 if (stash) {
4d7902f2
AF
754 priv->device_flags |= FSL_GIANFAR_DEV_HAS_BD_STASHING;
755 priv->bd_stash_en = 1;
756 }
757
758 stash_len = of_get_property(np, "rx-stash-len", NULL);
759
760 if (stash_len)
761 priv->rx_stash_size = *stash_len;
762
763 stash_idx = of_get_property(np, "rx-stash-idx", NULL);
764
765 if (stash_idx)
766 priv->rx_stash_index = *stash_idx;
767
768 if (stash_len || stash_idx)
769 priv->device_flags |= FSL_GIANFAR_DEV_HAS_BUF_STASHING;
770
b31a1d8b 771 mac_addr = of_get_mac_address(np);
bc4598bc 772
b31a1d8b 773 if (mac_addr)
6a3c910c 774 memcpy(dev->dev_addr, mac_addr, ETH_ALEN);
b31a1d8b
AF
775
776 if (model && !strcasecmp(model, "TSEC"))
34018fd4 777 priv->device_flags |= FSL_GIANFAR_DEV_HAS_GIGABIT |
bc4598bc
JC
778 FSL_GIANFAR_DEV_HAS_COALESCE |
779 FSL_GIANFAR_DEV_HAS_RMON |
780 FSL_GIANFAR_DEV_HAS_MULTI_INTR;
781
b31a1d8b 782 if (model && !strcasecmp(model, "eTSEC"))
34018fd4 783 priv->device_flags |= FSL_GIANFAR_DEV_HAS_GIGABIT |
bc4598bc
JC
784 FSL_GIANFAR_DEV_HAS_COALESCE |
785 FSL_GIANFAR_DEV_HAS_RMON |
786 FSL_GIANFAR_DEV_HAS_MULTI_INTR |
bc4598bc
JC
787 FSL_GIANFAR_DEV_HAS_CSUM |
788 FSL_GIANFAR_DEV_HAS_VLAN |
789 FSL_GIANFAR_DEV_HAS_MAGIC_PACKET |
790 FSL_GIANFAR_DEV_HAS_EXTENDED_HASH |
791 FSL_GIANFAR_DEV_HAS_TIMER;
b31a1d8b
AF
792
793 ctype = of_get_property(np, "phy-connection-type", NULL);
794
795 /* We only care about rgmii-id. The rest are autodetected */
796 if (ctype && !strcmp(ctype, "rgmii-id"))
797 priv->interface = PHY_INTERFACE_MODE_RGMII_ID;
798 else
799 priv->interface = PHY_INTERFACE_MODE_MII;
800
801 if (of_get_property(np, "fsl,magic-packet", NULL))
802 priv->device_flags |= FSL_GIANFAR_DEV_HAS_MAGIC_PACKET;
803
fe192a49 804 priv->phy_node = of_parse_phandle(np, "phy-handle", 0);
b31a1d8b
AF
805
806 /* Find the TBI PHY. If it's not there, we don't support SGMII */
fe192a49 807 priv->tbi_node = of_parse_phandle(np, "tbi-handle", 0);
b31a1d8b
AF
808
809 return 0;
810
46ceb60c
SG
811err_grp_init:
812 unmap_group_regs(priv);
20862788
CM
813rx_alloc_failed:
814 gfar_free_rx_queues(priv);
815tx_alloc_failed:
816 gfar_free_tx_queues(priv);
ee873fda 817 free_gfar_dev(priv);
b31a1d8b
AF
818 return err;
819}
820
ca0c88c2 821static int gfar_hwtstamp_set(struct net_device *netdev, struct ifreq *ifr)
cc772ab7
MR
822{
823 struct hwtstamp_config config;
824 struct gfar_private *priv = netdev_priv(netdev);
825
826 if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
827 return -EFAULT;
828
829 /* reserved for future extensions */
830 if (config.flags)
831 return -EINVAL;
832
f0ee7acf
MR
833 switch (config.tx_type) {
834 case HWTSTAMP_TX_OFF:
835 priv->hwts_tx_en = 0;
836 break;
837 case HWTSTAMP_TX_ON:
838 if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER))
839 return -ERANGE;
840 priv->hwts_tx_en = 1;
841 break;
842 default:
cc772ab7 843 return -ERANGE;
f0ee7acf 844 }
cc772ab7
MR
845
846 switch (config.rx_filter) {
847 case HWTSTAMP_FILTER_NONE:
97553f7f
MR
848 if (priv->hwts_rx_en) {
849 stop_gfar(netdev);
850 priv->hwts_rx_en = 0;
851 startup_gfar(netdev);
852 }
cc772ab7
MR
853 break;
854 default:
855 if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER))
856 return -ERANGE;
97553f7f
MR
857 if (!priv->hwts_rx_en) {
858 stop_gfar(netdev);
859 priv->hwts_rx_en = 1;
860 startup_gfar(netdev);
861 }
cc772ab7
MR
862 config.rx_filter = HWTSTAMP_FILTER_ALL;
863 break;
864 }
865
866 return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
867 -EFAULT : 0;
868}
869
ca0c88c2
BH
870static int gfar_hwtstamp_get(struct net_device *netdev, struct ifreq *ifr)
871{
872 struct hwtstamp_config config;
873 struct gfar_private *priv = netdev_priv(netdev);
874
875 config.flags = 0;
876 config.tx_type = priv->hwts_tx_en ? HWTSTAMP_TX_ON : HWTSTAMP_TX_OFF;
877 config.rx_filter = (priv->hwts_rx_en ?
878 HWTSTAMP_FILTER_ALL : HWTSTAMP_FILTER_NONE);
879
880 return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
881 -EFAULT : 0;
882}
883
0faac9f7
CW
884static int gfar_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
885{
886 struct gfar_private *priv = netdev_priv(dev);
887
888 if (!netif_running(dev))
889 return -EINVAL;
890
cc772ab7 891 if (cmd == SIOCSHWTSTAMP)
ca0c88c2
BH
892 return gfar_hwtstamp_set(dev, rq);
893 if (cmd == SIOCGHWTSTAMP)
894 return gfar_hwtstamp_get(dev, rq);
cc772ab7 895
0faac9f7
CW
896 if (!priv->phydev)
897 return -ENODEV;
898
28b04113 899 return phy_mii_ioctl(priv->phydev, rq, cmd);
0faac9f7
CW
900}
901
18294ad1
AV
902static u32 cluster_entry_per_class(struct gfar_private *priv, u32 rqfar,
903 u32 class)
7a8b3372
SG
904{
905 u32 rqfpr = FPR_FILER_MASK;
906 u32 rqfcr = 0x0;
907
908 rqfar--;
909 rqfcr = RQFCR_CLE | RQFCR_PID_MASK | RQFCR_CMP_EXACT;
6c43e046
WJB
910 priv->ftp_rqfpr[rqfar] = rqfpr;
911 priv->ftp_rqfcr[rqfar] = rqfcr;
7a8b3372
SG
912 gfar_write_filer(priv, rqfar, rqfcr, rqfpr);
913
914 rqfar--;
915 rqfcr = RQFCR_CMP_NOMATCH;
6c43e046
WJB
916 priv->ftp_rqfpr[rqfar] = rqfpr;
917 priv->ftp_rqfcr[rqfar] = rqfcr;
7a8b3372
SG
918 gfar_write_filer(priv, rqfar, rqfcr, rqfpr);
919
920 rqfar--;
921 rqfcr = RQFCR_CMP_EXACT | RQFCR_PID_PARSE | RQFCR_CLE | RQFCR_AND;
922 rqfpr = class;
6c43e046
WJB
923 priv->ftp_rqfcr[rqfar] = rqfcr;
924 priv->ftp_rqfpr[rqfar] = rqfpr;
7a8b3372
SG
925 gfar_write_filer(priv, rqfar, rqfcr, rqfpr);
926
927 rqfar--;
928 rqfcr = RQFCR_CMP_EXACT | RQFCR_PID_MASK | RQFCR_AND;
929 rqfpr = class;
6c43e046
WJB
930 priv->ftp_rqfcr[rqfar] = rqfcr;
931 priv->ftp_rqfpr[rqfar] = rqfpr;
7a8b3372
SG
932 gfar_write_filer(priv, rqfar, rqfcr, rqfpr);
933
934 return rqfar;
935}
936
937static void gfar_init_filer_table(struct gfar_private *priv)
938{
939 int i = 0x0;
940 u32 rqfar = MAX_FILER_IDX;
941 u32 rqfcr = 0x0;
942 u32 rqfpr = FPR_FILER_MASK;
943
944 /* Default rule */
945 rqfcr = RQFCR_CMP_MATCH;
6c43e046
WJB
946 priv->ftp_rqfcr[rqfar] = rqfcr;
947 priv->ftp_rqfpr[rqfar] = rqfpr;
7a8b3372
SG
948 gfar_write_filer(priv, rqfar, rqfcr, rqfpr);
949
950 rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV6);
951 rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV6 | RQFPR_UDP);
952 rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV6 | RQFPR_TCP);
953 rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV4);
954 rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV4 | RQFPR_UDP);
955 rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV4 | RQFPR_TCP);
956
85dd08eb 957 /* cur_filer_idx indicated the first non-masked rule */
7a8b3372
SG
958 priv->cur_filer_idx = rqfar;
959
960 /* Rest are masked rules */
961 rqfcr = RQFCR_CMP_NOMATCH;
962 for (i = 0; i < rqfar; i++) {
6c43e046
WJB
963 priv->ftp_rqfcr[i] = rqfcr;
964 priv->ftp_rqfpr[i] = rqfpr;
7a8b3372
SG
965 gfar_write_filer(priv, i, rqfcr, rqfpr);
966 }
967}
968
2969b1f7 969static void __gfar_detect_errata_83xx(struct gfar_private *priv)
7d350977 970{
7d350977
AV
971 unsigned int pvr = mfspr(SPRN_PVR);
972 unsigned int svr = mfspr(SPRN_SVR);
973 unsigned int mod = (svr >> 16) & 0xfff6; /* w/o E suffix */
974 unsigned int rev = svr & 0xffff;
975
976 /* MPC8313 Rev 2.0 and higher; All MPC837x */
977 if ((pvr == 0x80850010 && mod == 0x80b0 && rev >= 0x0020) ||
bc4598bc 978 (pvr == 0x80861010 && (mod & 0xfff9) == 0x80c0))
7d350977
AV
979 priv->errata |= GFAR_ERRATA_74;
980
deb90eac
AV
981 /* MPC8313 and MPC837x all rev */
982 if ((pvr == 0x80850010 && mod == 0x80b0) ||
bc4598bc 983 (pvr == 0x80861010 && (mod & 0xfff9) == 0x80c0))
deb90eac
AV
984 priv->errata |= GFAR_ERRATA_76;
985
2969b1f7
CM
986 /* MPC8313 Rev < 2.0 */
987 if (pvr == 0x80850010 && mod == 0x80b0 && rev < 0x0020)
988 priv->errata |= GFAR_ERRATA_12;
989}
990
991static void __gfar_detect_errata_85xx(struct gfar_private *priv)
992{
993 unsigned int svr = mfspr(SPRN_SVR);
994
995 if ((SVR_SOC_VER(svr) == SVR_8548) && (SVR_REV(svr) == 0x20))
4363c2fd 996 priv->errata |= GFAR_ERRATA_12;
53fad773
CM
997 if (((SVR_SOC_VER(svr) == SVR_P2020) && (SVR_REV(svr) < 0x20)) ||
998 ((SVR_SOC_VER(svr) == SVR_P2010) && (SVR_REV(svr) < 0x20)))
999 priv->errata |= GFAR_ERRATA_76; /* aka eTSEC 20 */
2969b1f7
CM
1000}
1001
1002static void gfar_detect_errata(struct gfar_private *priv)
1003{
1004 struct device *dev = &priv->ofdev->dev;
1005
1006 /* no plans to fix */
1007 priv->errata |= GFAR_ERRATA_A002;
1008
1009 if (pvr_version_is(PVR_VER_E500V1) || pvr_version_is(PVR_VER_E500V2))
1010 __gfar_detect_errata_85xx(priv);
1011 else /* non-mpc85xx parts, i.e. e300 core based */
1012 __gfar_detect_errata_83xx(priv);
4363c2fd 1013
7d350977
AV
1014 if (priv->errata)
1015 dev_info(dev, "enabled errata workarounds, flags: 0x%x\n",
1016 priv->errata);
1017}
1018
20862788
CM
1019static void gfar_hw_init(struct gfar_private *priv)
1020{
1021 struct gfar __iomem *regs = priv->gfargrp[0].regs;
34018fd4 1022 u32 tempval, attrs;
20862788
CM
1023
1024 /* Reset MAC layer */
1025 gfar_write(&regs->maccfg1, MACCFG1_SOFT_RESET);
1026
1027 /* We need to delay at least 3 TX clocks */
1028 udelay(2);
1029
1030 /* the soft reset bit is not self-resetting, so we need to
1031 * clear it before resuming normal operation
1032 */
1033 gfar_write(&regs->maccfg1, 0);
1034
1035 /* Initialize MACCFG2. */
1036 tempval = MACCFG2_INIT_SETTINGS;
1037 if (gfar_has_errata(priv, GFAR_ERRATA_74))
1038 tempval |= MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK;
1039 gfar_write(&regs->maccfg2, tempval);
1040
1041 /* Initialize ECNTRL */
1042 gfar_write(&regs->ecntrl, ECNTRL_INIT_SETTINGS);
1043
34018fd4
CM
1044 /* Set the extraction length and index */
1045 attrs = ATTRELI_EL(priv->rx_stash_size) |
1046 ATTRELI_EI(priv->rx_stash_index);
1047
1048 gfar_write(&regs->attreli, attrs);
1049
1050 /* Start with defaults, and add stashing
1051 * depending on driver parameters
1052 */
1053 attrs = ATTR_INIT_SETTINGS;
1054
1055 if (priv->bd_stash_en)
1056 attrs |= ATTR_BDSTASH;
1057
1058 if (priv->rx_stash_size != 0)
1059 attrs |= ATTR_BUFSTASH;
1060
1061 gfar_write(&regs->attr, attrs);
1062
1063 /* FIFO configs */
1064 gfar_write(&regs->fifo_tx_thr, DEFAULT_FIFO_TX_THR);
1065 gfar_write(&regs->fifo_tx_starve, DEFAULT_FIFO_TX_STARVE);
1066 gfar_write(&regs->fifo_tx_starve_shutoff, DEFAULT_FIFO_TX_STARVE_OFF);
1067
20862788
CM
1068 /* Program the interrupt steering regs, only for MG devices */
1069 if (priv->num_grps > 1)
1070 gfar_write_isrg(priv);
20862788
CM
1071}
1072
1073static void __init gfar_init_addr_hash_table(struct gfar_private *priv)
1074{
1075 struct gfar __iomem *regs = priv->gfargrp[0].regs;
1076
1077 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_EXTENDED_HASH) {
1078 priv->extended_hash = 1;
1079 priv->hash_width = 9;
1080
1081 priv->hash_regs[0] = &regs->igaddr0;
1082 priv->hash_regs[1] = &regs->igaddr1;
1083 priv->hash_regs[2] = &regs->igaddr2;
1084 priv->hash_regs[3] = &regs->igaddr3;
1085 priv->hash_regs[4] = &regs->igaddr4;
1086 priv->hash_regs[5] = &regs->igaddr5;
1087 priv->hash_regs[6] = &regs->igaddr6;
1088 priv->hash_regs[7] = &regs->igaddr7;
1089 priv->hash_regs[8] = &regs->gaddr0;
1090 priv->hash_regs[9] = &regs->gaddr1;
1091 priv->hash_regs[10] = &regs->gaddr2;
1092 priv->hash_regs[11] = &regs->gaddr3;
1093 priv->hash_regs[12] = &regs->gaddr4;
1094 priv->hash_regs[13] = &regs->gaddr5;
1095 priv->hash_regs[14] = &regs->gaddr6;
1096 priv->hash_regs[15] = &regs->gaddr7;
1097
1098 } else {
1099 priv->extended_hash = 0;
1100 priv->hash_width = 8;
1101
1102 priv->hash_regs[0] = &regs->gaddr0;
1103 priv->hash_regs[1] = &regs->gaddr1;
1104 priv->hash_regs[2] = &regs->gaddr2;
1105 priv->hash_regs[3] = &regs->gaddr3;
1106 priv->hash_regs[4] = &regs->gaddr4;
1107 priv->hash_regs[5] = &regs->gaddr5;
1108 priv->hash_regs[6] = &regs->gaddr6;
1109 priv->hash_regs[7] = &regs->gaddr7;
1110 }
1111}
1112
bb40dcbb 1113/* Set up the ethernet device structure, private data,
0977f817
JC
1114 * and anything else we need before we start
1115 */
74888760 1116static int gfar_probe(struct platform_device *ofdev)
1da177e4 1117{
1da177e4
LT
1118 struct net_device *dev = NULL;
1119 struct gfar_private *priv = NULL;
20862788 1120 int err = 0, i;
1da177e4 1121
fba4ed03 1122 err = gfar_of_init(ofdev, &dev);
1da177e4 1123
fba4ed03
SG
1124 if (err)
1125 return err;
1da177e4
LT
1126
1127 priv = netdev_priv(dev);
4826857f
KG
1128 priv->ndev = dev;
1129 priv->ofdev = ofdev;
369ec162 1130 priv->dev = &ofdev->dev;
4826857f 1131 SET_NETDEV_DEV(dev, &ofdev->dev);
1da177e4 1132
d87eb127 1133 spin_lock_init(&priv->bflock);
ab939905 1134 INIT_WORK(&priv->reset_task, gfar_reset_task);
1da177e4 1135
8513fbd8 1136 platform_set_drvdata(ofdev, priv);
1da177e4 1137
7d350977
AV
1138 gfar_detect_errata(priv);
1139
0977f817
JC
1140 /* Stop the DMA engine now, in case it was running before
1141 * (The firmware could have used it, and left it running).
1142 */
c10650b6 1143 gfar_halt(priv);
1da177e4 1144
20862788 1145 gfar_hw_init(priv);
1da177e4 1146
1da177e4 1147 /* Set the dev->base_addr to the gfar reg region */
20862788 1148 dev->base_addr = (unsigned long) priv->gfargrp[0].regs;
1da177e4 1149
1da177e4 1150 /* Fill in the dev structure */
1da177e4 1151 dev->watchdog_timeo = TX_TIMEOUT;
1da177e4 1152 dev->mtu = 1500;
26ccfc37 1153 dev->netdev_ops = &gfar_netdev_ops;
0bbaf069
KG
1154 dev->ethtool_ops = &gfar_ethtool_ops;
1155
fba4ed03 1156 /* Register for napi ...We are registering NAPI for each grp */
5eaedf31
CM
1157 if (priv->mode == SQ_SG_MODE)
1158 netif_napi_add(dev, &priv->gfargrp[0].napi, gfar_poll_sq,
bc4598bc 1159 GFAR_DEV_WEIGHT);
5eaedf31
CM
1160 else
1161 for (i = 0; i < priv->num_grps; i++)
1162 netif_napi_add(dev, &priv->gfargrp[i].napi, gfar_poll,
1163 GFAR_DEV_WEIGHT);
a12f801d 1164
b31a1d8b 1165 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_CSUM) {
8b3afe95 1166 dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_SG |
bc4598bc 1167 NETIF_F_RXCSUM;
8b3afe95 1168 dev->features |= NETIF_F_IP_CSUM | NETIF_F_SG |
bc4598bc 1169 NETIF_F_RXCSUM | NETIF_F_HIGHDMA;
8b3afe95 1170 }
0bbaf069 1171
87c288c6 1172 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_VLAN) {
f646968f
PM
1173 dev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX |
1174 NETIF_F_HW_VLAN_CTAG_RX;
1175 dev->features |= NETIF_F_HW_VLAN_CTAG_RX;
87c288c6 1176 }
0bbaf069 1177
20862788 1178 gfar_init_addr_hash_table(priv);
0bbaf069 1179
532c37bc
CM
1180 /* Insert receive time stamps into padding alignment bytes */
1181 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER)
1182 priv->padding = 8;
0bbaf069 1183
cc772ab7 1184 if (dev->features & NETIF_F_IP_CSUM ||
bc4598bc 1185 priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER)
bee9e58c 1186 dev->needed_headroom = GMAC_FCB_LEN;
1da177e4
LT
1187
1188 priv->rx_buffer_size = DEFAULT_RX_BUFFER_SIZE;
1da177e4 1189
a12f801d 1190 /* Initializing some of the rx/tx queue level parameters */
fba4ed03
SG
1191 for (i = 0; i < priv->num_tx_queues; i++) {
1192 priv->tx_queue[i]->tx_ring_size = DEFAULT_TX_RING_SIZE;
1193 priv->tx_queue[i]->num_txbdfree = DEFAULT_TX_RING_SIZE;
1194 priv->tx_queue[i]->txcoalescing = DEFAULT_TX_COALESCE;
1195 priv->tx_queue[i]->txic = DEFAULT_TXIC;
1196 }
a12f801d 1197
fba4ed03
SG
1198 for (i = 0; i < priv->num_rx_queues; i++) {
1199 priv->rx_queue[i]->rx_ring_size = DEFAULT_RX_RING_SIZE;
1200 priv->rx_queue[i]->rxcoalescing = DEFAULT_RX_COALESCE;
1201 priv->rx_queue[i]->rxic = DEFAULT_RXIC;
1202 }
1da177e4 1203
0977f817 1204 /* always enable rx filer */
4aa3a715 1205 priv->rx_filer_enable = 1;
0bbaf069
KG
1206 /* Enable most messages by default */
1207 priv->msg_enable = (NETIF_MSG_IFUP << 1 ) - 1;
b98b8bab
CM
1208 /* use pritority h/w tx queue scheduling for single queue devices */
1209 if (priv->num_tx_queues == 1)
1210 priv->prio_sched_en = 1;
0bbaf069 1211
d3eab82b
TP
1212 /* Carrier starts down, phylib will bring it up */
1213 netif_carrier_off(dev);
1214
1da177e4
LT
1215 err = register_netdev(dev);
1216
1217 if (err) {
59deab26 1218 pr_err("%s: Cannot register net device, aborting\n", dev->name);
1da177e4
LT
1219 goto register_fail;
1220 }
1221
2884e5cc 1222 device_init_wakeup(&dev->dev,
bc4598bc
JC
1223 priv->device_flags &
1224 FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
2884e5cc 1225
c50a5d9a 1226 /* fill out IRQ number and name fields */
46ceb60c 1227 for (i = 0; i < priv->num_grps; i++) {
ee873fda 1228 struct gfar_priv_grp *grp = &priv->gfargrp[i];
46ceb60c 1229 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
ee873fda 1230 sprintf(gfar_irq(grp, TX)->name, "%s%s%c%s",
0015e551 1231 dev->name, "_g", '0' + i, "_tx");
ee873fda 1232 sprintf(gfar_irq(grp, RX)->name, "%s%s%c%s",
0015e551 1233 dev->name, "_g", '0' + i, "_rx");
ee873fda 1234 sprintf(gfar_irq(grp, ER)->name, "%s%s%c%s",
0015e551 1235 dev->name, "_g", '0' + i, "_er");
46ceb60c 1236 } else
ee873fda 1237 strcpy(gfar_irq(grp, TX)->name, dev->name);
46ceb60c 1238 }
c50a5d9a 1239
7a8b3372
SG
1240 /* Initialize the filer table */
1241 gfar_init_filer_table(priv);
1242
1da177e4 1243 /* Print out the device info */
59deab26 1244 netdev_info(dev, "mac: %pM\n", dev->dev_addr);
1da177e4 1245
0977f817
JC
1246 /* Even more device info helps when determining which kernel
1247 * provided which set of benchmarks.
1248 */
59deab26 1249 netdev_info(dev, "Running with NAPI enabled\n");
fba4ed03 1250 for (i = 0; i < priv->num_rx_queues; i++)
59deab26
JP
1251 netdev_info(dev, "RX BD ring size for Q[%d]: %d\n",
1252 i, priv->rx_queue[i]->rx_ring_size);
bc4598bc 1253 for (i = 0; i < priv->num_tx_queues; i++)
59deab26
JP
1254 netdev_info(dev, "TX BD ring size for Q[%d]: %d\n",
1255 i, priv->tx_queue[i]->tx_ring_size);
1da177e4
LT
1256
1257 return 0;
1258
1259register_fail:
46ceb60c 1260 unmap_group_regs(priv);
20862788
CM
1261 gfar_free_rx_queues(priv);
1262 gfar_free_tx_queues(priv);
fe192a49
GL
1263 if (priv->phy_node)
1264 of_node_put(priv->phy_node);
1265 if (priv->tbi_node)
1266 of_node_put(priv->tbi_node);
ee873fda 1267 free_gfar_dev(priv);
bb40dcbb 1268 return err;
1da177e4
LT
1269}
1270
2dc11581 1271static int gfar_remove(struct platform_device *ofdev)
1da177e4 1272{
8513fbd8 1273 struct gfar_private *priv = platform_get_drvdata(ofdev);
1da177e4 1274
fe192a49
GL
1275 if (priv->phy_node)
1276 of_node_put(priv->phy_node);
1277 if (priv->tbi_node)
1278 of_node_put(priv->tbi_node);
1279
d9d8e041 1280 unregister_netdev(priv->ndev);
46ceb60c 1281 unmap_group_regs(priv);
20862788
CM
1282 gfar_free_rx_queues(priv);
1283 gfar_free_tx_queues(priv);
ee873fda 1284 free_gfar_dev(priv);
1da177e4
LT
1285
1286 return 0;
1287}
1288
d87eb127 1289#ifdef CONFIG_PM
be926fc4
AV
1290
1291static int gfar_suspend(struct device *dev)
d87eb127 1292{
be926fc4
AV
1293 struct gfar_private *priv = dev_get_drvdata(dev);
1294 struct net_device *ndev = priv->ndev;
46ceb60c 1295 struct gfar __iomem *regs = priv->gfargrp[0].regs;
d87eb127
SW
1296 unsigned long flags;
1297 u32 tempval;
1298
1299 int magic_packet = priv->wol_en &&
bc4598bc
JC
1300 (priv->device_flags &
1301 FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
d87eb127 1302
be926fc4 1303 netif_device_detach(ndev);
d87eb127 1304
be926fc4 1305 if (netif_running(ndev)) {
fba4ed03
SG
1306
1307 local_irq_save(flags);
1308 lock_tx_qs(priv);
1309 lock_rx_qs(priv);
d87eb127 1310
c10650b6 1311 gfar_halt_nodisable(priv);
d87eb127
SW
1312
1313 /* Disable Tx, and Rx if wake-on-LAN is disabled. */
f4983704 1314 tempval = gfar_read(&regs->maccfg1);
d87eb127
SW
1315
1316 tempval &= ~MACCFG1_TX_EN;
1317
1318 if (!magic_packet)
1319 tempval &= ~MACCFG1_RX_EN;
1320
f4983704 1321 gfar_write(&regs->maccfg1, tempval);
d87eb127 1322
fba4ed03
SG
1323 unlock_rx_qs(priv);
1324 unlock_tx_qs(priv);
1325 local_irq_restore(flags);
d87eb127 1326
46ceb60c 1327 disable_napi(priv);
d87eb127
SW
1328
1329 if (magic_packet) {
1330 /* Enable interrupt on Magic Packet */
f4983704 1331 gfar_write(&regs->imask, IMASK_MAG);
d87eb127
SW
1332
1333 /* Enable Magic Packet mode */
f4983704 1334 tempval = gfar_read(&regs->maccfg2);
d87eb127 1335 tempval |= MACCFG2_MPEN;
f4983704 1336 gfar_write(&regs->maccfg2, tempval);
d87eb127
SW
1337 } else {
1338 phy_stop(priv->phydev);
1339 }
1340 }
1341
1342 return 0;
1343}
1344
be926fc4 1345static int gfar_resume(struct device *dev)
d87eb127 1346{
be926fc4
AV
1347 struct gfar_private *priv = dev_get_drvdata(dev);
1348 struct net_device *ndev = priv->ndev;
46ceb60c 1349 struct gfar __iomem *regs = priv->gfargrp[0].regs;
d87eb127
SW
1350 unsigned long flags;
1351 u32 tempval;
1352 int magic_packet = priv->wol_en &&
bc4598bc
JC
1353 (priv->device_flags &
1354 FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
d87eb127 1355
be926fc4
AV
1356 if (!netif_running(ndev)) {
1357 netif_device_attach(ndev);
d87eb127
SW
1358 return 0;
1359 }
1360
1361 if (!magic_packet && priv->phydev)
1362 phy_start(priv->phydev);
1363
1364 /* Disable Magic Packet mode, in case something
1365 * else woke us up.
1366 */
fba4ed03
SG
1367 local_irq_save(flags);
1368 lock_tx_qs(priv);
1369 lock_rx_qs(priv);
d87eb127 1370
f4983704 1371 tempval = gfar_read(&regs->maccfg2);
d87eb127 1372 tempval &= ~MACCFG2_MPEN;
f4983704 1373 gfar_write(&regs->maccfg2, tempval);
d87eb127 1374
c10650b6 1375 gfar_start(priv);
d87eb127 1376
fba4ed03
SG
1377 unlock_rx_qs(priv);
1378 unlock_tx_qs(priv);
1379 local_irq_restore(flags);
d87eb127 1380
be926fc4
AV
1381 netif_device_attach(ndev);
1382
46ceb60c 1383 enable_napi(priv);
be926fc4
AV
1384
1385 return 0;
1386}
1387
1388static int gfar_restore(struct device *dev)
1389{
1390 struct gfar_private *priv = dev_get_drvdata(dev);
1391 struct net_device *ndev = priv->ndev;
1392
103cdd1d
WD
1393 if (!netif_running(ndev)) {
1394 netif_device_attach(ndev);
1395
be926fc4 1396 return 0;
103cdd1d 1397 }
be926fc4 1398
1eb8f7a7
CM
1399 if (gfar_init_bds(ndev)) {
1400 free_skb_resources(priv);
1401 return -ENOMEM;
1402 }
1403
be926fc4
AV
1404 init_registers(ndev);
1405 gfar_set_mac_address(ndev);
1406 gfar_init_mac(ndev);
c10650b6 1407 gfar_start(priv);
be926fc4
AV
1408
1409 priv->oldlink = 0;
1410 priv->oldspeed = 0;
1411 priv->oldduplex = -1;
1412
1413 if (priv->phydev)
1414 phy_start(priv->phydev);
d87eb127 1415
be926fc4 1416 netif_device_attach(ndev);
5ea681d4 1417 enable_napi(priv);
d87eb127
SW
1418
1419 return 0;
1420}
be926fc4
AV
1421
1422static struct dev_pm_ops gfar_pm_ops = {
1423 .suspend = gfar_suspend,
1424 .resume = gfar_resume,
1425 .freeze = gfar_suspend,
1426 .thaw = gfar_resume,
1427 .restore = gfar_restore,
1428};
1429
1430#define GFAR_PM_OPS (&gfar_pm_ops)
1431
d87eb127 1432#else
be926fc4
AV
1433
1434#define GFAR_PM_OPS NULL
be926fc4 1435
d87eb127 1436#endif
1da177e4 1437
e8a2b6a4
AF
1438/* Reads the controller's registers to determine what interface
1439 * connects it to the PHY.
1440 */
1441static phy_interface_t gfar_get_interface(struct net_device *dev)
1442{
1443 struct gfar_private *priv = netdev_priv(dev);
46ceb60c 1444 struct gfar __iomem *regs = priv->gfargrp[0].regs;
f4983704
SG
1445 u32 ecntrl;
1446
f4983704 1447 ecntrl = gfar_read(&regs->ecntrl);
e8a2b6a4
AF
1448
1449 if (ecntrl & ECNTRL_SGMII_MODE)
1450 return PHY_INTERFACE_MODE_SGMII;
1451
1452 if (ecntrl & ECNTRL_TBI_MODE) {
1453 if (ecntrl & ECNTRL_REDUCED_MODE)
1454 return PHY_INTERFACE_MODE_RTBI;
1455 else
1456 return PHY_INTERFACE_MODE_TBI;
1457 }
1458
1459 if (ecntrl & ECNTRL_REDUCED_MODE) {
bc4598bc 1460 if (ecntrl & ECNTRL_REDUCED_MII_MODE) {
e8a2b6a4 1461 return PHY_INTERFACE_MODE_RMII;
bc4598bc 1462 }
7132ab7f 1463 else {
b31a1d8b 1464 phy_interface_t interface = priv->interface;
7132ab7f 1465
0977f817 1466 /* This isn't autodetected right now, so it must
7132ab7f
AF
1467 * be set by the device tree or platform code.
1468 */
1469 if (interface == PHY_INTERFACE_MODE_RGMII_ID)
1470 return PHY_INTERFACE_MODE_RGMII_ID;
1471
e8a2b6a4 1472 return PHY_INTERFACE_MODE_RGMII;
7132ab7f 1473 }
e8a2b6a4
AF
1474 }
1475
b31a1d8b 1476 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT)
e8a2b6a4
AF
1477 return PHY_INTERFACE_MODE_GMII;
1478
1479 return PHY_INTERFACE_MODE_MII;
1480}
1481
1482
bb40dcbb
AF
1483/* Initializes driver's PHY state, and attaches to the PHY.
1484 * Returns 0 on success.
1da177e4
LT
1485 */
1486static int init_phy(struct net_device *dev)
1487{
1488 struct gfar_private *priv = netdev_priv(dev);
bb40dcbb 1489 uint gigabit_support =
b31a1d8b 1490 priv->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT ?
23402bdd 1491 GFAR_SUPPORTED_GBIT : 0;
e8a2b6a4 1492 phy_interface_t interface;
1da177e4
LT
1493
1494 priv->oldlink = 0;
1495 priv->oldspeed = 0;
1496 priv->oldduplex = -1;
1497
e8a2b6a4
AF
1498 interface = gfar_get_interface(dev);
1499
1db780f8
AV
1500 priv->phydev = of_phy_connect(dev, priv->phy_node, &adjust_link, 0,
1501 interface);
1502 if (!priv->phydev)
1503 priv->phydev = of_phy_connect_fixed_link(dev, &adjust_link,
1504 interface);
1505 if (!priv->phydev) {
1506 dev_err(&dev->dev, "could not attach to PHY\n");
1507 return -ENODEV;
fe192a49 1508 }
1da177e4 1509
d3c12873
KJ
1510 if (interface == PHY_INTERFACE_MODE_SGMII)
1511 gfar_configure_serdes(dev);
1512
bb40dcbb 1513 /* Remove any features not supported by the controller */
fe192a49
GL
1514 priv->phydev->supported &= (GFAR_SUPPORTED | gigabit_support);
1515 priv->phydev->advertising = priv->phydev->supported;
1da177e4
LT
1516
1517 return 0;
1da177e4
LT
1518}
1519
0977f817 1520/* Initialize TBI PHY interface for communicating with the
d0313587
PG
1521 * SERDES lynx PHY on the chip. We communicate with this PHY
1522 * through the MDIO bus on each controller, treating it as a
1523 * "normal" PHY at the address found in the TBIPA register. We assume
1524 * that the TBIPA register is valid. Either the MDIO bus code will set
1525 * it to a value that doesn't conflict with other PHYs on the bus, or the
1526 * value doesn't matter, as there are no other PHYs on the bus.
1527 */
d3c12873
KJ
1528static void gfar_configure_serdes(struct net_device *dev)
1529{
1530 struct gfar_private *priv = netdev_priv(dev);
fe192a49
GL
1531 struct phy_device *tbiphy;
1532
1533 if (!priv->tbi_node) {
1534 dev_warn(&dev->dev, "error: SGMII mode requires that the "
1535 "device tree specify a tbi-handle\n");
1536 return;
1537 }
c132419e 1538
fe192a49
GL
1539 tbiphy = of_phy_find_device(priv->tbi_node);
1540 if (!tbiphy) {
1541 dev_err(&dev->dev, "error: Could not get TBI device\n");
b31a1d8b
AF
1542 return;
1543 }
d3c12873 1544
0977f817 1545 /* If the link is already up, we must already be ok, and don't need to
bdb59f94
TP
1546 * configure and reset the TBI<->SerDes link. Maybe U-Boot configured
1547 * everything for us? Resetting it takes the link down and requires
1548 * several seconds for it to come back.
1549 */
fe192a49 1550 if (phy_read(tbiphy, MII_BMSR) & BMSR_LSTATUS)
b31a1d8b 1551 return;
d3c12873 1552
d0313587 1553 /* Single clk mode, mii mode off(for serdes communication) */
fe192a49 1554 phy_write(tbiphy, MII_TBICON, TBICON_CLK_SELECT);
d3c12873 1555
fe192a49 1556 phy_write(tbiphy, MII_ADVERTISE,
bc4598bc
JC
1557 ADVERTISE_1000XFULL | ADVERTISE_1000XPAUSE |
1558 ADVERTISE_1000XPSE_ASYM);
d3c12873 1559
bc4598bc
JC
1560 phy_write(tbiphy, MII_BMCR,
1561 BMCR_ANENABLE | BMCR_ANRESTART | BMCR_FULLDPLX |
1562 BMCR_SPEED1000);
d3c12873
KJ
1563}
1564
1da177e4
LT
1565static void init_registers(struct net_device *dev)
1566{
1567 struct gfar_private *priv = netdev_priv(dev);
efeddce7 1568 struct gfar __iomem *regs = priv->gfargrp[0].regs;
1da177e4 1569
efeddce7 1570 gfar_ints_disable(priv);
1da177e4
LT
1571
1572 /* Init hash registers to zero */
f4983704
SG
1573 gfar_write(&regs->igaddr0, 0);
1574 gfar_write(&regs->igaddr1, 0);
1575 gfar_write(&regs->igaddr2, 0);
1576 gfar_write(&regs->igaddr3, 0);
1577 gfar_write(&regs->igaddr4, 0);
1578 gfar_write(&regs->igaddr5, 0);
1579 gfar_write(&regs->igaddr6, 0);
1580 gfar_write(&regs->igaddr7, 0);
1581
1582 gfar_write(&regs->gaddr0, 0);
1583 gfar_write(&regs->gaddr1, 0);
1584 gfar_write(&regs->gaddr2, 0);
1585 gfar_write(&regs->gaddr3, 0);
1586 gfar_write(&regs->gaddr4, 0);
1587 gfar_write(&regs->gaddr5, 0);
1588 gfar_write(&regs->gaddr6, 0);
1589 gfar_write(&regs->gaddr7, 0);
1da177e4 1590
1da177e4 1591 /* Zero out the rmon mib registers if it has them */
b31a1d8b 1592 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_RMON) {
f4983704 1593 memset_io(&(regs->rmon), 0, sizeof (struct rmon_mib));
1da177e4
LT
1594
1595 /* Mask off the CAM interrupts */
f4983704
SG
1596 gfar_write(&regs->rmon.cam1, 0xffffffff);
1597 gfar_write(&regs->rmon.cam2, 0xffffffff);
1da177e4
LT
1598 }
1599
1600 /* Initialize the max receive buffer length */
f4983704 1601 gfar_write(&regs->mrblr, priv->rx_buffer_size);
1da177e4 1602
1da177e4 1603 /* Initialize the Minimum Frame Length Register */
f4983704 1604 gfar_write(&regs->minflr, MINFLR_INIT_SETTINGS);
1da177e4
LT
1605}
1606
511d934f
AV
1607static int __gfar_is_rx_idle(struct gfar_private *priv)
1608{
1609 u32 res;
1610
0977f817 1611 /* Normaly TSEC should not hang on GRS commands, so we should
511d934f
AV
1612 * actually wait for IEVENT_GRSC flag.
1613 */
ad3660c2 1614 if (!gfar_has_errata(priv, GFAR_ERRATA_A002))
511d934f
AV
1615 return 0;
1616
0977f817 1617 /* Read the eTSEC register at offset 0xD1C. If bits 7-14 are
511d934f
AV
1618 * the same as bits 23-30, the eTSEC Rx is assumed to be idle
1619 * and the Rx can be safely reset.
1620 */
1621 res = gfar_read((void __iomem *)priv->gfargrp[0].regs + 0xd1c);
1622 res &= 0x7f807f80;
1623 if ((res & 0xffff) == (res >> 16))
1624 return 1;
1625
1626 return 0;
1627}
0bbaf069
KG
1628
1629/* Halt the receive and transmit queues */
c10650b6 1630static void gfar_halt_nodisable(struct gfar_private *priv)
1da177e4 1631{
efeddce7 1632 struct gfar __iomem *regs = priv->gfargrp[0].regs;
1da177e4
LT
1633 u32 tempval;
1634
efeddce7 1635 gfar_ints_disable(priv);
1da177e4 1636
1da177e4 1637 /* Stop the DMA, and wait for it to stop */
f4983704 1638 tempval = gfar_read(&regs->dmactrl);
bc4598bc
JC
1639 if ((tempval & (DMACTRL_GRS | DMACTRL_GTS)) !=
1640 (DMACTRL_GRS | DMACTRL_GTS)) {
511d934f
AV
1641 int ret;
1642
1da177e4 1643 tempval |= (DMACTRL_GRS | DMACTRL_GTS);
f4983704 1644 gfar_write(&regs->dmactrl, tempval);
1da177e4 1645
511d934f
AV
1646 do {
1647 ret = spin_event_timeout(((gfar_read(&regs->ievent) &
1648 (IEVENT_GRSC | IEVENT_GTSC)) ==
1649 (IEVENT_GRSC | IEVENT_GTSC)), 1000000, 0);
1650 if (!ret && !(gfar_read(&regs->ievent) & IEVENT_GRSC))
1651 ret = __gfar_is_rx_idle(priv);
1652 } while (!ret);
1da177e4 1653 }
d87eb127 1654}
d87eb127
SW
1655
1656/* Halt the receive and transmit queues */
c10650b6 1657void gfar_halt(struct gfar_private *priv)
d87eb127 1658{
46ceb60c 1659 struct gfar __iomem *regs = priv->gfargrp[0].regs;
d87eb127 1660 u32 tempval;
1da177e4 1661
c10650b6
CM
1662 /* Dissable the Rx/Tx hw queues */
1663 gfar_write(&regs->rqueue, 0);
1664 gfar_write(&regs->tqueue, 0);
2a54adc3 1665
c10650b6
CM
1666 mdelay(10);
1667
1668 gfar_halt_nodisable(priv);
1669
1670 /* Disable Rx/Tx DMA */
1da177e4
LT
1671 tempval = gfar_read(&regs->maccfg1);
1672 tempval &= ~(MACCFG1_RX_EN | MACCFG1_TX_EN);
1673 gfar_write(&regs->maccfg1, tempval);
0bbaf069
KG
1674}
1675
46ceb60c
SG
1676static void free_grp_irqs(struct gfar_priv_grp *grp)
1677{
ee873fda
CM
1678 free_irq(gfar_irq(grp, TX)->irq, grp);
1679 free_irq(gfar_irq(grp, RX)->irq, grp);
1680 free_irq(gfar_irq(grp, ER)->irq, grp);
46ceb60c
SG
1681}
1682
0bbaf069
KG
1683void stop_gfar(struct net_device *dev)
1684{
1685 struct gfar_private *priv = netdev_priv(dev);
0bbaf069 1686 unsigned long flags;
46ceb60c 1687 int i;
0bbaf069 1688
bb40dcbb
AF
1689 phy_stop(priv->phydev);
1690
a12f801d 1691
0bbaf069 1692 /* Lock it down */
fba4ed03
SG
1693 local_irq_save(flags);
1694 lock_tx_qs(priv);
1695 lock_rx_qs(priv);
0bbaf069 1696
c10650b6 1697 gfar_halt(priv);
1da177e4 1698
fba4ed03
SG
1699 unlock_rx_qs(priv);
1700 unlock_tx_qs(priv);
1701 local_irq_restore(flags);
1da177e4
LT
1702
1703 /* Free the IRQs */
b31a1d8b 1704 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
46ceb60c
SG
1705 for (i = 0; i < priv->num_grps; i++)
1706 free_grp_irqs(&priv->gfargrp[i]);
1da177e4 1707 } else {
46ceb60c 1708 for (i = 0; i < priv->num_grps; i++)
ee873fda 1709 free_irq(gfar_irq(&priv->gfargrp[i], TX)->irq,
bc4598bc 1710 &priv->gfargrp[i]);
1da177e4
LT
1711 }
1712
1713 free_skb_resources(priv);
1da177e4
LT
1714}
1715
fba4ed03 1716static void free_skb_tx_queue(struct gfar_priv_tx_q *tx_queue)
1da177e4 1717{
1da177e4 1718 struct txbd8 *txbdp;
fba4ed03 1719 struct gfar_private *priv = netdev_priv(tx_queue->dev);
4669bc90 1720 int i, j;
1da177e4 1721
a12f801d 1722 txbdp = tx_queue->tx_bd_base;
1da177e4 1723
a12f801d
SG
1724 for (i = 0; i < tx_queue->tx_ring_size; i++) {
1725 if (!tx_queue->tx_skbuff[i])
4669bc90 1726 continue;
1da177e4 1727
369ec162 1728 dma_unmap_single(priv->dev, txbdp->bufPtr,
bc4598bc 1729 txbdp->length, DMA_TO_DEVICE);
4669bc90 1730 txbdp->lstatus = 0;
fba4ed03 1731 for (j = 0; j < skb_shinfo(tx_queue->tx_skbuff[i])->nr_frags;
bc4598bc 1732 j++) {
4669bc90 1733 txbdp++;
369ec162 1734 dma_unmap_page(priv->dev, txbdp->bufPtr,
bc4598bc 1735 txbdp->length, DMA_TO_DEVICE);
1da177e4 1736 }
ad5da7ab 1737 txbdp++;
a12f801d
SG
1738 dev_kfree_skb_any(tx_queue->tx_skbuff[i]);
1739 tx_queue->tx_skbuff[i] = NULL;
1da177e4 1740 }
a12f801d 1741 kfree(tx_queue->tx_skbuff);
1eb8f7a7 1742 tx_queue->tx_skbuff = NULL;
fba4ed03 1743}
1da177e4 1744
fba4ed03
SG
1745static void free_skb_rx_queue(struct gfar_priv_rx_q *rx_queue)
1746{
1747 struct rxbd8 *rxbdp;
1748 struct gfar_private *priv = netdev_priv(rx_queue->dev);
1749 int i;
1da177e4 1750
fba4ed03 1751 rxbdp = rx_queue->rx_bd_base;
1da177e4 1752
a12f801d
SG
1753 for (i = 0; i < rx_queue->rx_ring_size; i++) {
1754 if (rx_queue->rx_skbuff[i]) {
369ec162
CM
1755 dma_unmap_single(priv->dev, rxbdp->bufPtr,
1756 priv->rx_buffer_size,
bc4598bc 1757 DMA_FROM_DEVICE);
a12f801d
SG
1758 dev_kfree_skb_any(rx_queue->rx_skbuff[i]);
1759 rx_queue->rx_skbuff[i] = NULL;
1da177e4 1760 }
e69edd21
AV
1761 rxbdp->lstatus = 0;
1762 rxbdp->bufPtr = 0;
1763 rxbdp++;
1da177e4 1764 }
a12f801d 1765 kfree(rx_queue->rx_skbuff);
1eb8f7a7 1766 rx_queue->rx_skbuff = NULL;
fba4ed03 1767}
e69edd21 1768
fba4ed03 1769/* If there are any tx skbs or rx skbs still around, free them.
0977f817
JC
1770 * Then free tx_skbuff and rx_skbuff
1771 */
fba4ed03
SG
1772static void free_skb_resources(struct gfar_private *priv)
1773{
1774 struct gfar_priv_tx_q *tx_queue = NULL;
1775 struct gfar_priv_rx_q *rx_queue = NULL;
1776 int i;
1777
1778 /* Go through all the buffer descriptors and free their data buffers */
1779 for (i = 0; i < priv->num_tx_queues; i++) {
d8a0f1b0 1780 struct netdev_queue *txq;
bc4598bc 1781
fba4ed03 1782 tx_queue = priv->tx_queue[i];
d8a0f1b0 1783 txq = netdev_get_tx_queue(tx_queue->dev, tx_queue->qindex);
bc4598bc 1784 if (tx_queue->tx_skbuff)
fba4ed03 1785 free_skb_tx_queue(tx_queue);
d8a0f1b0 1786 netdev_tx_reset_queue(txq);
fba4ed03
SG
1787 }
1788
1789 for (i = 0; i < priv->num_rx_queues; i++) {
1790 rx_queue = priv->rx_queue[i];
bc4598bc 1791 if (rx_queue->rx_skbuff)
fba4ed03
SG
1792 free_skb_rx_queue(rx_queue);
1793 }
1794
369ec162 1795 dma_free_coherent(priv->dev,
bc4598bc
JC
1796 sizeof(struct txbd8) * priv->total_tx_ring_size +
1797 sizeof(struct rxbd8) * priv->total_rx_ring_size,
1798 priv->tx_queue[0]->tx_bd_base,
1799 priv->tx_queue[0]->tx_bd_dma_base);
1da177e4
LT
1800}
1801
c10650b6 1802void gfar_start(struct gfar_private *priv)
0bbaf069 1803{
46ceb60c 1804 struct gfar __iomem *regs = priv->gfargrp[0].regs;
0bbaf069 1805 u32 tempval;
46ceb60c 1806 int i = 0;
0bbaf069 1807
c10650b6
CM
1808 /* Enable Rx/Tx hw queues */
1809 gfar_write(&regs->rqueue, priv->rqueue);
1810 gfar_write(&regs->tqueue, priv->tqueue);
0bbaf069
KG
1811
1812 /* Initialize DMACTRL to have WWR and WOP */
f4983704 1813 tempval = gfar_read(&regs->dmactrl);
0bbaf069 1814 tempval |= DMACTRL_INIT_SETTINGS;
f4983704 1815 gfar_write(&regs->dmactrl, tempval);
0bbaf069 1816
0bbaf069 1817 /* Make sure we aren't stopped */
f4983704 1818 tempval = gfar_read(&regs->dmactrl);
0bbaf069 1819 tempval &= ~(DMACTRL_GRS | DMACTRL_GTS);
f4983704 1820 gfar_write(&regs->dmactrl, tempval);
0bbaf069 1821
46ceb60c
SG
1822 for (i = 0; i < priv->num_grps; i++) {
1823 regs = priv->gfargrp[i].regs;
1824 /* Clear THLT/RHLT, so that the DMA starts polling now */
1825 gfar_write(&regs->tstat, priv->gfargrp[i].tstat);
1826 gfar_write(&regs->rstat, priv->gfargrp[i].rstat);
46ceb60c 1827 }
12dea57b 1828
c10650b6
CM
1829 /* Enable Rx/Tx DMA */
1830 tempval = gfar_read(&regs->maccfg1);
1831 tempval |= (MACCFG1_RX_EN | MACCFG1_TX_EN);
1832 gfar_write(&regs->maccfg1, tempval);
1833
efeddce7
CM
1834 gfar_ints_enable(priv);
1835
c10650b6 1836 priv->ndev->trans_start = jiffies; /* prevent tx timeout */
0bbaf069
KG
1837}
1838
800c644b 1839static void gfar_configure_coalescing(struct gfar_private *priv,
bc4598bc 1840 unsigned long tx_mask, unsigned long rx_mask)
1da177e4 1841{
46ceb60c 1842 struct gfar __iomem *regs = priv->gfargrp[0].regs;
18294ad1 1843 u32 __iomem *baddr;
815b97c6 1844
46ceb60c 1845 if (priv->mode == MQ_MG_MODE) {
5d9657d8 1846 int i = 0;
c6e1160e 1847
46ceb60c 1848 baddr = &regs->txic0;
984b3f57 1849 for_each_set_bit(i, &tx_mask, priv->num_tx_queues) {
9740e001
CM
1850 gfar_write(baddr + i, 0);
1851 if (likely(priv->tx_queue[i]->txcoalescing))
46ceb60c 1852 gfar_write(baddr + i, priv->tx_queue[i]->txic);
46ceb60c
SG
1853 }
1854
1855 baddr = &regs->rxic0;
984b3f57 1856 for_each_set_bit(i, &rx_mask, priv->num_rx_queues) {
9740e001
CM
1857 gfar_write(baddr + i, 0);
1858 if (likely(priv->rx_queue[i]->rxcoalescing))
46ceb60c 1859 gfar_write(baddr + i, priv->rx_queue[i]->rxic);
46ceb60c 1860 }
5d9657d8 1861 } else {
c6e1160e 1862 /* Backward compatible case -- even if we enable
5d9657d8
CM
1863 * multiple queues, there's only single reg to program
1864 */
1865 gfar_write(&regs->txic, 0);
1866 if (likely(priv->tx_queue[0]->txcoalescing))
1867 gfar_write(&regs->txic, priv->tx_queue[0]->txic);
1868
1869 gfar_write(&regs->rxic, 0);
1870 if (unlikely(priv->rx_queue[0]->rxcoalescing))
1871 gfar_write(&regs->rxic, priv->rx_queue[0]->rxic);
46ceb60c
SG
1872 }
1873}
1874
800c644b
CM
1875void gfar_configure_coalescing_all(struct gfar_private *priv)
1876{
1877 gfar_configure_coalescing(priv, 0xFF, 0xFF);
1878}
1879
46ceb60c
SG
1880static int register_grp_irqs(struct gfar_priv_grp *grp)
1881{
1882 struct gfar_private *priv = grp->priv;
1883 struct net_device *dev = priv->ndev;
1884 int err;
1da177e4 1885
1da177e4 1886 /* If the device has multiple interrupts, register for
0977f817
JC
1887 * them. Otherwise, only register for the one
1888 */
b31a1d8b 1889 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
0bbaf069 1890 /* Install our interrupt handlers for Error,
0977f817
JC
1891 * Transmit, and Receive
1892 */
ee873fda
CM
1893 err = request_irq(gfar_irq(grp, ER)->irq, gfar_error, 0,
1894 gfar_irq(grp, ER)->name, grp);
1895 if (err < 0) {
59deab26 1896 netif_err(priv, intr, dev, "Can't get IRQ %d\n",
ee873fda 1897 gfar_irq(grp, ER)->irq);
46ceb60c 1898
2145f1af 1899 goto err_irq_fail;
1da177e4 1900 }
ee873fda
CM
1901 err = request_irq(gfar_irq(grp, TX)->irq, gfar_transmit, 0,
1902 gfar_irq(grp, TX)->name, grp);
1903 if (err < 0) {
59deab26 1904 netif_err(priv, intr, dev, "Can't get IRQ %d\n",
ee873fda 1905 gfar_irq(grp, TX)->irq);
1da177e4
LT
1906 goto tx_irq_fail;
1907 }
ee873fda
CM
1908 err = request_irq(gfar_irq(grp, RX)->irq, gfar_receive, 0,
1909 gfar_irq(grp, RX)->name, grp);
1910 if (err < 0) {
59deab26 1911 netif_err(priv, intr, dev, "Can't get IRQ %d\n",
ee873fda 1912 gfar_irq(grp, RX)->irq);
1da177e4
LT
1913 goto rx_irq_fail;
1914 }
1915 } else {
ee873fda
CM
1916 err = request_irq(gfar_irq(grp, TX)->irq, gfar_interrupt, 0,
1917 gfar_irq(grp, TX)->name, grp);
1918 if (err < 0) {
59deab26 1919 netif_err(priv, intr, dev, "Can't get IRQ %d\n",
ee873fda 1920 gfar_irq(grp, TX)->irq);
1da177e4
LT
1921 goto err_irq_fail;
1922 }
1923 }
1924
46ceb60c
SG
1925 return 0;
1926
1927rx_irq_fail:
ee873fda 1928 free_irq(gfar_irq(grp, TX)->irq, grp);
46ceb60c 1929tx_irq_fail:
ee873fda 1930 free_irq(gfar_irq(grp, ER)->irq, grp);
46ceb60c
SG
1931err_irq_fail:
1932 return err;
1933
1934}
1935
1936/* Bring the controller up and running */
1937int startup_gfar(struct net_device *ndev)
1938{
1939 struct gfar_private *priv = netdev_priv(ndev);
46ceb60c
SG
1940 int err, i, j;
1941
efeddce7 1942 gfar_ints_disable(priv);
46ceb60c 1943
46ceb60c
SG
1944 err = gfar_alloc_skb_resources(ndev);
1945 if (err)
1946 return err;
1947
1948 gfar_init_mac(ndev);
1949
1950 for (i = 0; i < priv->num_grps; i++) {
1951 err = register_grp_irqs(&priv->gfargrp[i]);
1952 if (err) {
1953 for (j = 0; j < i; j++)
1954 free_grp_irqs(&priv->gfargrp[j]);
ff76015f 1955 goto irq_fail;
46ceb60c
SG
1956 }
1957 }
1958
7f7f5316 1959 /* Start the controller */
c10650b6 1960 gfar_start(priv);
1da177e4 1961
826aa4a0
AV
1962 phy_start(priv->phydev);
1963
800c644b 1964 gfar_configure_coalescing_all(priv);
46ceb60c 1965
1da177e4
LT
1966 return 0;
1967
46ceb60c 1968irq_fail:
e69edd21 1969 free_skb_resources(priv);
1da177e4
LT
1970 return err;
1971}
1972
0977f817
JC
1973/* Called when something needs to use the ethernet device
1974 * Returns 0 for success.
1975 */
1da177e4
LT
1976static int gfar_enet_open(struct net_device *dev)
1977{
94e8cc35 1978 struct gfar_private *priv = netdev_priv(dev);
1da177e4
LT
1979 int err;
1980
46ceb60c 1981 enable_napi(priv);
bea3348e 1982
1da177e4
LT
1983 /* Initialize a bunch of registers */
1984 init_registers(dev);
1985
1986 gfar_set_mac_address(dev);
1987
1988 err = init_phy(dev);
1989
a12f801d 1990 if (err) {
46ceb60c 1991 disable_napi(priv);
1da177e4 1992 return err;
bea3348e 1993 }
1da177e4
LT
1994
1995 err = startup_gfar(dev);
db0e8e3f 1996 if (err) {
46ceb60c 1997 disable_napi(priv);
db0e8e3f
AV
1998 return err;
1999 }
1da177e4 2000
fba4ed03 2001 netif_tx_start_all_queues(dev);
1da177e4 2002
2884e5cc
AV
2003 device_set_wakeup_enable(&dev->dev, priv->wol_en);
2004
1da177e4
LT
2005 return err;
2006}
2007
54dc79fe 2008static inline struct txfcb *gfar_add_fcb(struct sk_buff *skb)
0bbaf069 2009{
54dc79fe 2010 struct txfcb *fcb = (struct txfcb *)skb_push(skb, GMAC_FCB_LEN);
6c31d55f
KG
2011
2012 memset(fcb, 0, GMAC_FCB_LEN);
0bbaf069 2013
0bbaf069
KG
2014 return fcb;
2015}
2016
9c4886e5 2017static inline void gfar_tx_checksum(struct sk_buff *skb, struct txfcb *fcb,
bc4598bc 2018 int fcb_length)
0bbaf069 2019{
0bbaf069
KG
2020 /* If we're here, it's a IP packet with a TCP or UDP
2021 * payload. We set it to checksum, using a pseudo-header
2022 * we provide
2023 */
3a2e16c8 2024 u8 flags = TXFCB_DEFAULT;
0bbaf069 2025
0977f817
JC
2026 /* Tell the controller what the protocol is
2027 * And provide the already calculated phcs
2028 */
eddc9ec5 2029 if (ip_hdr(skb)->protocol == IPPROTO_UDP) {
7f7f5316 2030 flags |= TXFCB_UDP;
4bedb452 2031 fcb->phcs = udp_hdr(skb)->check;
7f7f5316 2032 } else
8da32de5 2033 fcb->phcs = tcp_hdr(skb)->check;
0bbaf069
KG
2034
2035 /* l3os is the distance between the start of the
2036 * frame (skb->data) and the start of the IP hdr.
2037 * l4os is the distance between the start of the
0977f817
JC
2038 * l3 hdr and the l4 hdr
2039 */
9c4886e5 2040 fcb->l3os = (u16)(skb_network_offset(skb) - fcb_length);
cfe1fc77 2041 fcb->l4os = skb_network_header_len(skb);
0bbaf069 2042
7f7f5316 2043 fcb->flags = flags;
0bbaf069
KG
2044}
2045
7f7f5316 2046void inline gfar_tx_vlan(struct sk_buff *skb, struct txfcb *fcb)
0bbaf069 2047{
7f7f5316 2048 fcb->flags |= TXFCB_VLN;
0bbaf069
KG
2049 fcb->vlctl = vlan_tx_tag_get(skb);
2050}
2051
4669bc90 2052static inline struct txbd8 *skip_txbd(struct txbd8 *bdp, int stride,
bc4598bc 2053 struct txbd8 *base, int ring_size)
4669bc90
DH
2054{
2055 struct txbd8 *new_bd = bdp + stride;
2056
2057 return (new_bd >= (base + ring_size)) ? (new_bd - ring_size) : new_bd;
2058}
2059
2060static inline struct txbd8 *next_txbd(struct txbd8 *bdp, struct txbd8 *base,
bc4598bc 2061 int ring_size)
4669bc90
DH
2062{
2063 return skip_txbd(bdp, 1, base, ring_size);
2064}
2065
02d88fb4
CM
2066/* eTSEC12: csum generation not supported for some fcb offsets */
2067static inline bool gfar_csum_errata_12(struct gfar_private *priv,
2068 unsigned long fcb_addr)
2069{
2070 return (gfar_has_errata(priv, GFAR_ERRATA_12) &&
2071 (fcb_addr % 0x20) > 0x18);
2072}
2073
2074/* eTSEC76: csum generation for frames larger than 2500 may
2075 * cause excess delays before start of transmission
2076 */
2077static inline bool gfar_csum_errata_76(struct gfar_private *priv,
2078 unsigned int len)
2079{
2080 return (gfar_has_errata(priv, GFAR_ERRATA_76) &&
2081 (len > 2500));
2082}
2083
0977f817
JC
2084/* This is called by the kernel when a frame is ready for transmission.
2085 * It is pointed to by the dev->hard_start_xmit function pointer
2086 */
1da177e4
LT
2087static int gfar_start_xmit(struct sk_buff *skb, struct net_device *dev)
2088{
2089 struct gfar_private *priv = netdev_priv(dev);
a12f801d 2090 struct gfar_priv_tx_q *tx_queue = NULL;
fba4ed03 2091 struct netdev_queue *txq;
f4983704 2092 struct gfar __iomem *regs = NULL;
0bbaf069 2093 struct txfcb *fcb = NULL;
f0ee7acf 2094 struct txbd8 *txbdp, *txbdp_start, *base, *txbdp_tstamp = NULL;
5a5efed4 2095 u32 lstatus;
0d0cffdc
CM
2096 int i, rq = 0;
2097 int do_tstamp, do_csum, do_vlan;
4669bc90 2098 u32 bufaddr;
fef6108d 2099 unsigned long flags;
50ad076b 2100 unsigned int nr_frags, nr_txbds, bytes_sent, fcb_len = 0;
fba4ed03
SG
2101
2102 rq = skb->queue_mapping;
2103 tx_queue = priv->tx_queue[rq];
2104 txq = netdev_get_tx_queue(dev, rq);
a12f801d 2105 base = tx_queue->tx_bd_base;
46ceb60c 2106 regs = tx_queue->grp->regs;
f0ee7acf 2107
0d0cffdc
CM
2108 do_csum = (CHECKSUM_PARTIAL == skb->ip_summed);
2109 do_vlan = vlan_tx_tag_present(skb);
2110 do_tstamp = (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
2111 priv->hwts_tx_en;
2112
2113 if (do_csum || do_vlan)
2114 fcb_len = GMAC_FCB_LEN;
2115
f0ee7acf 2116 /* check if time stamp should be generated */
0d0cffdc
CM
2117 if (unlikely(do_tstamp))
2118 fcb_len = GMAC_FCB_LEN + GMAC_TXPAL_LEN;
4669bc90 2119
5b28beaf 2120 /* make space for additional header when fcb is needed */
0d0cffdc 2121 if (fcb_len && unlikely(skb_headroom(skb) < fcb_len)) {
54dc79fe
SH
2122 struct sk_buff *skb_new;
2123
0d0cffdc 2124 skb_new = skb_realloc_headroom(skb, fcb_len);
54dc79fe
SH
2125 if (!skb_new) {
2126 dev->stats.tx_errors++;
bd14ba84 2127 kfree_skb(skb);
54dc79fe
SH
2128 return NETDEV_TX_OK;
2129 }
db83d136 2130
313b037c
ED
2131 if (skb->sk)
2132 skb_set_owner_w(skb_new, skb->sk);
2133 consume_skb(skb);
54dc79fe
SH
2134 skb = skb_new;
2135 }
2136
4669bc90
DH
2137 /* total number of fragments in the SKB */
2138 nr_frags = skb_shinfo(skb)->nr_frags;
2139
f0ee7acf
MR
2140 /* calculate the required number of TxBDs for this skb */
2141 if (unlikely(do_tstamp))
2142 nr_txbds = nr_frags + 2;
2143 else
2144 nr_txbds = nr_frags + 1;
2145
4669bc90 2146 /* check if there is space to queue this packet */
f0ee7acf 2147 if (nr_txbds > tx_queue->num_txbdfree) {
4669bc90 2148 /* no space, stop the queue */
fba4ed03 2149 netif_tx_stop_queue(txq);
4669bc90 2150 dev->stats.tx_fifo_errors++;
4669bc90
DH
2151 return NETDEV_TX_BUSY;
2152 }
1da177e4
LT
2153
2154 /* Update transmit stats */
50ad076b
CM
2155 bytes_sent = skb->len;
2156 tx_queue->stats.tx_bytes += bytes_sent;
2157 /* keep Tx bytes on wire for BQL accounting */
2158 GFAR_CB(skb)->bytes_sent = bytes_sent;
1ac9ad13 2159 tx_queue->stats.tx_packets++;
1da177e4 2160
a12f801d 2161 txbdp = txbdp_start = tx_queue->cur_tx;
f0ee7acf
MR
2162 lstatus = txbdp->lstatus;
2163
2164 /* Time stamp insertion requires one additional TxBD */
2165 if (unlikely(do_tstamp))
2166 txbdp_tstamp = txbdp = next_txbd(txbdp, base,
bc4598bc 2167 tx_queue->tx_ring_size);
1da177e4 2168
4669bc90 2169 if (nr_frags == 0) {
f0ee7acf
MR
2170 if (unlikely(do_tstamp))
2171 txbdp_tstamp->lstatus |= BD_LFLAG(TXBD_LAST |
bc4598bc 2172 TXBD_INTERRUPT);
f0ee7acf
MR
2173 else
2174 lstatus |= BD_LFLAG(TXBD_LAST | TXBD_INTERRUPT);
4669bc90
DH
2175 } else {
2176 /* Place the fragment addresses and lengths into the TxBDs */
2177 for (i = 0; i < nr_frags; i++) {
50ad076b 2178 unsigned int frag_len;
4669bc90 2179 /* Point at the next BD, wrapping as needed */
a12f801d 2180 txbdp = next_txbd(txbdp, base, tx_queue->tx_ring_size);
4669bc90 2181
50ad076b 2182 frag_len = skb_shinfo(skb)->frags[i].size;
4669bc90 2183
50ad076b 2184 lstatus = txbdp->lstatus | frag_len |
bc4598bc 2185 BD_LFLAG(TXBD_READY);
4669bc90
DH
2186
2187 /* Handle the last BD specially */
2188 if (i == nr_frags - 1)
2189 lstatus |= BD_LFLAG(TXBD_LAST | TXBD_INTERRUPT);
1da177e4 2190
369ec162 2191 bufaddr = skb_frag_dma_map(priv->dev,
2234a722
IC
2192 &skb_shinfo(skb)->frags[i],
2193 0,
50ad076b 2194 frag_len,
2234a722 2195 DMA_TO_DEVICE);
4669bc90
DH
2196
2197 /* set the TxBD length and buffer pointer */
2198 txbdp->bufPtr = bufaddr;
2199 txbdp->lstatus = lstatus;
2200 }
2201
2202 lstatus = txbdp_start->lstatus;
2203 }
1da177e4 2204
9c4886e5
MR
2205 /* Add TxPAL between FCB and frame if required */
2206 if (unlikely(do_tstamp)) {
2207 skb_push(skb, GMAC_TXPAL_LEN);
2208 memset(skb->data, 0, GMAC_TXPAL_LEN);
2209 }
2210
0d0cffdc
CM
2211 /* Add TxFCB if required */
2212 if (fcb_len) {
54dc79fe 2213 fcb = gfar_add_fcb(skb);
02d88fb4 2214 lstatus |= BD_LFLAG(TXBD_TOE);
0d0cffdc
CM
2215 }
2216
2217 /* Set up checksumming */
2218 if (do_csum) {
2219 gfar_tx_checksum(skb, fcb, fcb_len);
02d88fb4
CM
2220
2221 if (unlikely(gfar_csum_errata_12(priv, (unsigned long)fcb)) ||
2222 unlikely(gfar_csum_errata_76(priv, skb->len))) {
4363c2fd
AD
2223 __skb_pull(skb, GMAC_FCB_LEN);
2224 skb_checksum_help(skb);
0d0cffdc
CM
2225 if (do_vlan || do_tstamp) {
2226 /* put back a new fcb for vlan/tstamp TOE */
2227 fcb = gfar_add_fcb(skb);
2228 } else {
2229 /* Tx TOE not used */
2230 lstatus &= ~(BD_LFLAG(TXBD_TOE));
2231 fcb = NULL;
2232 }
4363c2fd 2233 }
0bbaf069
KG
2234 }
2235
0d0cffdc 2236 if (do_vlan)
54dc79fe 2237 gfar_tx_vlan(skb, fcb);
0bbaf069 2238
f0ee7acf
MR
2239 /* Setup tx hardware time stamping if requested */
2240 if (unlikely(do_tstamp)) {
2244d07b 2241 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
f0ee7acf 2242 fcb->ptp = 1;
f0ee7acf
MR
2243 }
2244
369ec162 2245 txbdp_start->bufPtr = dma_map_single(priv->dev, skb->data,
bc4598bc 2246 skb_headlen(skb), DMA_TO_DEVICE);
1da177e4 2247
0977f817 2248 /* If time stamping is requested one additional TxBD must be set up. The
f0ee7acf
MR
2249 * first TxBD points to the FCB and must have a data length of
2250 * GMAC_FCB_LEN. The second TxBD points to the actual frame data with
2251 * the full frame length.
2252 */
2253 if (unlikely(do_tstamp)) {
0d0cffdc 2254 txbdp_tstamp->bufPtr = txbdp_start->bufPtr + fcb_len;
f0ee7acf 2255 txbdp_tstamp->lstatus |= BD_LFLAG(TXBD_READY) |
0d0cffdc 2256 (skb_headlen(skb) - fcb_len);
f0ee7acf
MR
2257 lstatus |= BD_LFLAG(TXBD_CRC | TXBD_READY) | GMAC_FCB_LEN;
2258 } else {
2259 lstatus |= BD_LFLAG(TXBD_CRC | TXBD_READY) | skb_headlen(skb);
2260 }
1da177e4 2261
50ad076b 2262 netdev_tx_sent_queue(txq, bytes_sent);
d8a0f1b0 2263
0977f817 2264 /* We can work in parallel with gfar_clean_tx_ring(), except
a3bc1f11
AV
2265 * when modifying num_txbdfree. Note that we didn't grab the lock
2266 * when we were reading the num_txbdfree and checking for available
2267 * space, that's because outside of this function it can only grow,
2268 * and once we've got needed space, it cannot suddenly disappear.
2269 *
2270 * The lock also protects us from gfar_error(), which can modify
2271 * regs->tstat and thus retrigger the transfers, which is why we
2272 * also must grab the lock before setting ready bit for the first
2273 * to be transmitted BD.
2274 */
2275 spin_lock_irqsave(&tx_queue->txlock, flags);
2276
0977f817 2277 /* The powerpc-specific eieio() is used, as wmb() has too strong
3b6330ce
SW
2278 * semantics (it requires synchronization between cacheable and
2279 * uncacheable mappings, which eieio doesn't provide and which we
2280 * don't need), thus requiring a more expensive sync instruction. At
2281 * some point, the set of architecture-independent barrier functions
2282 * should be expanded to include weaker barriers.
2283 */
3b6330ce 2284 eieio();
7f7f5316 2285
4669bc90
DH
2286 txbdp_start->lstatus = lstatus;
2287
0eddba52
AV
2288 eieio(); /* force lstatus write before tx_skbuff */
2289
2290 tx_queue->tx_skbuff[tx_queue->skb_curtx] = skb;
2291
4669bc90 2292 /* Update the current skb pointer to the next entry we will use
0977f817
JC
2293 * (wrapping if necessary)
2294 */
a12f801d 2295 tx_queue->skb_curtx = (tx_queue->skb_curtx + 1) &
bc4598bc 2296 TX_RING_MOD_MASK(tx_queue->tx_ring_size);
4669bc90 2297
a12f801d 2298 tx_queue->cur_tx = next_txbd(txbdp, base, tx_queue->tx_ring_size);
4669bc90
DH
2299
2300 /* reduce TxBD free count */
f0ee7acf 2301 tx_queue->num_txbdfree -= (nr_txbds);
1da177e4
LT
2302
2303 /* If the next BD still needs to be cleaned up, then the bds
0977f817
JC
2304 * are full. We need to tell the kernel to stop sending us stuff.
2305 */
a12f801d 2306 if (!tx_queue->num_txbdfree) {
fba4ed03 2307 netif_tx_stop_queue(txq);
1da177e4 2308
09f75cd7 2309 dev->stats.tx_fifo_errors++;
1da177e4
LT
2310 }
2311
1da177e4 2312 /* Tell the DMA to go go go */
fba4ed03 2313 gfar_write(&regs->tstat, TSTAT_CLEAR_THALT >> tx_queue->qindex);
1da177e4
LT
2314
2315 /* Unlock priv */
a12f801d 2316 spin_unlock_irqrestore(&tx_queue->txlock, flags);
1da177e4 2317
54dc79fe 2318 return NETDEV_TX_OK;
1da177e4
LT
2319}
2320
2321/* Stops the kernel queue, and halts the controller */
2322static int gfar_close(struct net_device *dev)
2323{
2324 struct gfar_private *priv = netdev_priv(dev);
bea3348e 2325
46ceb60c 2326 disable_napi(priv);
bea3348e 2327
ab939905 2328 cancel_work_sync(&priv->reset_task);
1da177e4
LT
2329 stop_gfar(dev);
2330
bb40dcbb
AF
2331 /* Disconnect from the PHY */
2332 phy_disconnect(priv->phydev);
2333 priv->phydev = NULL;
1da177e4 2334
fba4ed03 2335 netif_tx_stop_all_queues(dev);
1da177e4
LT
2336
2337 return 0;
2338}
2339
1da177e4 2340/* Changes the mac address if the controller is not running. */
f162b9d5 2341static int gfar_set_mac_address(struct net_device *dev)
1da177e4 2342{
7f7f5316 2343 gfar_set_mac_for_addr(dev, 0, dev->dev_addr);
1da177e4
LT
2344
2345 return 0;
2346}
2347
f3dc1586
SP
2348/* Check if rx parser should be activated */
2349void gfar_check_rx_parser_mode(struct gfar_private *priv)
2350{
2351 struct gfar __iomem *regs;
2352 u32 tempval;
2353
2354 regs = priv->gfargrp[0].regs;
2355
2356 tempval = gfar_read(&regs->rctrl);
2357 /* If parse is no longer required, then disable parser */
ba779711 2358 if (tempval & RCTRL_REQ_PARSER) {
f3dc1586 2359 tempval |= RCTRL_PRSDEP_INIT;
ba779711
CM
2360 priv->uses_rxfcb = 1;
2361 } else {
f3dc1586 2362 tempval &= ~RCTRL_PRSDEP_INIT;
ba779711
CM
2363 priv->uses_rxfcb = 0;
2364 }
f3dc1586
SP
2365 gfar_write(&regs->rctrl, tempval);
2366}
2367
0bbaf069 2368/* Enables and disables VLAN insertion/extraction */
c8f44aff 2369void gfar_vlan_mode(struct net_device *dev, netdev_features_t features)
0bbaf069
KG
2370{
2371 struct gfar_private *priv = netdev_priv(dev);
f4983704 2372 struct gfar __iomem *regs = NULL;
0bbaf069
KG
2373 unsigned long flags;
2374 u32 tempval;
2375
46ceb60c 2376 regs = priv->gfargrp[0].regs;
fba4ed03
SG
2377 local_irq_save(flags);
2378 lock_rx_qs(priv);
0bbaf069 2379
f646968f 2380 if (features & NETIF_F_HW_VLAN_CTAG_TX) {
0bbaf069 2381 /* Enable VLAN tag insertion */
f4983704 2382 tempval = gfar_read(&regs->tctrl);
0bbaf069 2383 tempval |= TCTRL_VLINS;
f4983704 2384 gfar_write(&regs->tctrl, tempval);
0bbaf069
KG
2385 } else {
2386 /* Disable VLAN tag insertion */
f4983704 2387 tempval = gfar_read(&regs->tctrl);
0bbaf069 2388 tempval &= ~TCTRL_VLINS;
f4983704 2389 gfar_write(&regs->tctrl, tempval);
87c288c6 2390 }
0bbaf069 2391
f646968f 2392 if (features & NETIF_F_HW_VLAN_CTAG_RX) {
87c288c6
JP
2393 /* Enable VLAN tag extraction */
2394 tempval = gfar_read(&regs->rctrl);
2395 tempval |= (RCTRL_VLEX | RCTRL_PRSDEP_INIT);
2396 gfar_write(&regs->rctrl, tempval);
ba779711 2397 priv->uses_rxfcb = 1;
87c288c6 2398 } else {
0bbaf069 2399 /* Disable VLAN tag extraction */
f4983704 2400 tempval = gfar_read(&regs->rctrl);
0bbaf069 2401 tempval &= ~RCTRL_VLEX;
f4983704 2402 gfar_write(&regs->rctrl, tempval);
f3dc1586
SP
2403
2404 gfar_check_rx_parser_mode(priv);
0bbaf069
KG
2405 }
2406
77ecaf2d
DH
2407 gfar_change_mtu(dev, dev->mtu);
2408
fba4ed03
SG
2409 unlock_rx_qs(priv);
2410 local_irq_restore(flags);
0bbaf069
KG
2411}
2412
1da177e4
LT
2413static int gfar_change_mtu(struct net_device *dev, int new_mtu)
2414{
2415 int tempsize, tempval;
2416 struct gfar_private *priv = netdev_priv(dev);
46ceb60c 2417 struct gfar __iomem *regs = priv->gfargrp[0].regs;
1da177e4 2418 int oldsize = priv->rx_buffer_size;
0bbaf069
KG
2419 int frame_size = new_mtu + ETH_HLEN;
2420
1da177e4 2421 if ((frame_size < 64) || (frame_size > JUMBO_FRAME_SIZE)) {
59deab26 2422 netif_err(priv, drv, dev, "Invalid MTU setting\n");
1da177e4
LT
2423 return -EINVAL;
2424 }
2425
ba779711 2426 if (priv->uses_rxfcb)
77ecaf2d
DH
2427 frame_size += GMAC_FCB_LEN;
2428
2429 frame_size += priv->padding;
2430
bc4598bc
JC
2431 tempsize = (frame_size & ~(INCREMENTAL_BUFFER_SIZE - 1)) +
2432 INCREMENTAL_BUFFER_SIZE;
1da177e4
LT
2433
2434 /* Only stop and start the controller if it isn't already
0977f817
JC
2435 * stopped, and we changed something
2436 */
1da177e4
LT
2437 if ((oldsize != tempsize) && (dev->flags & IFF_UP))
2438 stop_gfar(dev);
2439
2440 priv->rx_buffer_size = tempsize;
2441
2442 dev->mtu = new_mtu;
2443
f4983704
SG
2444 gfar_write(&regs->mrblr, priv->rx_buffer_size);
2445 gfar_write(&regs->maxfrm, priv->rx_buffer_size);
1da177e4
LT
2446
2447 /* If the mtu is larger than the max size for standard
2448 * ethernet frames (ie, a jumbo frame), then set maccfg2
0977f817
JC
2449 * to allow huge frames, and to check the length
2450 */
f4983704 2451 tempval = gfar_read(&regs->maccfg2);
1da177e4 2452
7d350977 2453 if (priv->rx_buffer_size > DEFAULT_RX_BUFFER_SIZE ||
bc4598bc 2454 gfar_has_errata(priv, GFAR_ERRATA_74))
1da177e4
LT
2455 tempval |= (MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK);
2456 else
2457 tempval &= ~(MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK);
2458
f4983704 2459 gfar_write(&regs->maccfg2, tempval);
1da177e4
LT
2460
2461 if ((oldsize != tempsize) && (dev->flags & IFF_UP))
2462 startup_gfar(dev);
2463
2464 return 0;
2465}
2466
ab939905 2467/* gfar_reset_task gets scheduled when a packet has not been
1da177e4
LT
2468 * transmitted after a set amount of time.
2469 * For now, assume that clearing out all the structures, and
ab939905
SS
2470 * starting over will fix the problem.
2471 */
2472static void gfar_reset_task(struct work_struct *work)
1da177e4 2473{
ab939905 2474 struct gfar_private *priv = container_of(work, struct gfar_private,
bc4598bc 2475 reset_task);
4826857f 2476 struct net_device *dev = priv->ndev;
1da177e4
LT
2477
2478 if (dev->flags & IFF_UP) {
fba4ed03 2479 netif_tx_stop_all_queues(dev);
1da177e4
LT
2480 stop_gfar(dev);
2481 startup_gfar(dev);
fba4ed03 2482 netif_tx_start_all_queues(dev);
1da177e4
LT
2483 }
2484
263ba320 2485 netif_tx_schedule_all(dev);
1da177e4
LT
2486}
2487
ab939905
SS
2488static void gfar_timeout(struct net_device *dev)
2489{
2490 struct gfar_private *priv = netdev_priv(dev);
2491
2492 dev->stats.tx_errors++;
2493 schedule_work(&priv->reset_task);
2494}
2495
acbc0f03
EL
2496static void gfar_align_skb(struct sk_buff *skb)
2497{
2498 /* We need the data buffer to be aligned properly. We will reserve
2499 * as many bytes as needed to align the data properly
2500 */
2501 skb_reserve(skb, RXBUF_ALIGNMENT -
bc4598bc 2502 (((unsigned long) skb->data) & (RXBUF_ALIGNMENT - 1)));
acbc0f03
EL
2503}
2504
1da177e4 2505/* Interrupt Handler for Transmit complete */
c233cf40 2506static void gfar_clean_tx_ring(struct gfar_priv_tx_q *tx_queue)
1da177e4 2507{
a12f801d 2508 struct net_device *dev = tx_queue->dev;
d8a0f1b0 2509 struct netdev_queue *txq;
d080cd63 2510 struct gfar_private *priv = netdev_priv(dev);
f0ee7acf 2511 struct txbd8 *bdp, *next = NULL;
4669bc90 2512 struct txbd8 *lbdp = NULL;
a12f801d 2513 struct txbd8 *base = tx_queue->tx_bd_base;
4669bc90
DH
2514 struct sk_buff *skb;
2515 int skb_dirtytx;
a12f801d 2516 int tx_ring_size = tx_queue->tx_ring_size;
f0ee7acf 2517 int frags = 0, nr_txbds = 0;
4669bc90 2518 int i;
d080cd63 2519 int howmany = 0;
d8a0f1b0
PG
2520 int tqi = tx_queue->qindex;
2521 unsigned int bytes_sent = 0;
4669bc90 2522 u32 lstatus;
f0ee7acf 2523 size_t buflen;
1da177e4 2524
d8a0f1b0 2525 txq = netdev_get_tx_queue(dev, tqi);
a12f801d
SG
2526 bdp = tx_queue->dirty_tx;
2527 skb_dirtytx = tx_queue->skb_dirtytx;
1da177e4 2528
a12f801d 2529 while ((skb = tx_queue->tx_skbuff[skb_dirtytx])) {
a3bc1f11
AV
2530 unsigned long flags;
2531
4669bc90 2532 frags = skb_shinfo(skb)->nr_frags;
f0ee7acf 2533
0977f817 2534 /* When time stamping, one additional TxBD must be freed.
f0ee7acf
MR
2535 * Also, we need to dma_unmap_single() the TxPAL.
2536 */
2244d07b 2537 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS))
f0ee7acf
MR
2538 nr_txbds = frags + 2;
2539 else
2540 nr_txbds = frags + 1;
2541
2542 lbdp = skip_txbd(bdp, nr_txbds - 1, base, tx_ring_size);
1da177e4 2543
4669bc90 2544 lstatus = lbdp->lstatus;
1da177e4 2545
4669bc90
DH
2546 /* Only clean completed frames */
2547 if ((lstatus & BD_LFLAG(TXBD_READY)) &&
bc4598bc 2548 (lstatus & BD_LENGTH_MASK))
4669bc90
DH
2549 break;
2550
2244d07b 2551 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)) {
f0ee7acf 2552 next = next_txbd(bdp, base, tx_ring_size);
9c4886e5 2553 buflen = next->length + GMAC_FCB_LEN + GMAC_TXPAL_LEN;
f0ee7acf
MR
2554 } else
2555 buflen = bdp->length;
2556
369ec162 2557 dma_unmap_single(priv->dev, bdp->bufPtr,
bc4598bc 2558 buflen, DMA_TO_DEVICE);
f0ee7acf 2559
2244d07b 2560 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)) {
f0ee7acf
MR
2561 struct skb_shared_hwtstamps shhwtstamps;
2562 u64 *ns = (u64*) (((u32)skb->data + 0x10) & ~0x7);
bc4598bc 2563
f0ee7acf
MR
2564 memset(&shhwtstamps, 0, sizeof(shhwtstamps));
2565 shhwtstamps.hwtstamp = ns_to_ktime(*ns);
9c4886e5 2566 skb_pull(skb, GMAC_FCB_LEN + GMAC_TXPAL_LEN);
f0ee7acf
MR
2567 skb_tstamp_tx(skb, &shhwtstamps);
2568 bdp->lstatus &= BD_LFLAG(TXBD_WRAP);
2569 bdp = next;
2570 }
81183059 2571
4669bc90
DH
2572 bdp->lstatus &= BD_LFLAG(TXBD_WRAP);
2573 bdp = next_txbd(bdp, base, tx_ring_size);
d080cd63 2574
4669bc90 2575 for (i = 0; i < frags; i++) {
369ec162 2576 dma_unmap_page(priv->dev, bdp->bufPtr,
bc4598bc 2577 bdp->length, DMA_TO_DEVICE);
4669bc90
DH
2578 bdp->lstatus &= BD_LFLAG(TXBD_WRAP);
2579 bdp = next_txbd(bdp, base, tx_ring_size);
2580 }
1da177e4 2581
50ad076b 2582 bytes_sent += GFAR_CB(skb)->bytes_sent;
d8a0f1b0 2583
acb600de 2584 dev_kfree_skb_any(skb);
0fd56bb5 2585
a12f801d 2586 tx_queue->tx_skbuff[skb_dirtytx] = NULL;
d080cd63 2587
4669bc90 2588 skb_dirtytx = (skb_dirtytx + 1) &
bc4598bc 2589 TX_RING_MOD_MASK(tx_ring_size);
4669bc90
DH
2590
2591 howmany++;
a3bc1f11 2592 spin_lock_irqsave(&tx_queue->txlock, flags);
f0ee7acf 2593 tx_queue->num_txbdfree += nr_txbds;
a3bc1f11 2594 spin_unlock_irqrestore(&tx_queue->txlock, flags);
4669bc90 2595 }
1da177e4 2596
4669bc90 2597 /* If we freed a buffer, we can restart transmission, if necessary */
5407b14c 2598 if (netif_tx_queue_stopped(txq) && tx_queue->num_txbdfree)
d8a0f1b0 2599 netif_wake_subqueue(dev, tqi);
1da177e4 2600
4669bc90 2601 /* Update dirty indicators */
a12f801d
SG
2602 tx_queue->skb_dirtytx = skb_dirtytx;
2603 tx_queue->dirty_tx = bdp;
1da177e4 2604
d8a0f1b0 2605 netdev_tx_completed_queue(txq, howmany, bytes_sent);
d080cd63
DH
2606}
2607
f4983704 2608static void gfar_schedule_cleanup(struct gfar_priv_grp *gfargrp)
d080cd63 2609{
a6d0b91a
AV
2610 unsigned long flags;
2611
fba4ed03
SG
2612 spin_lock_irqsave(&gfargrp->grplock, flags);
2613 if (napi_schedule_prep(&gfargrp->napi)) {
f4983704 2614 gfar_write(&gfargrp->regs->imask, IMASK_RTX_DISABLED);
fba4ed03 2615 __napi_schedule(&gfargrp->napi);
8707bdd4 2616 } else {
0977f817 2617 /* Clear IEVENT, so interrupts aren't called again
8707bdd4
JP
2618 * because of the packets that have already arrived.
2619 */
f4983704 2620 gfar_write(&gfargrp->regs->ievent, IEVENT_RTX_MASK);
2f448911 2621 }
fba4ed03 2622 spin_unlock_irqrestore(&gfargrp->grplock, flags);
a6d0b91a 2623
8c7396ae 2624}
1da177e4 2625
8c7396ae 2626/* Interrupt Handler for Transmit complete */
f4983704 2627static irqreturn_t gfar_transmit(int irq, void *grp_id)
8c7396ae 2628{
f4983704 2629 gfar_schedule_cleanup((struct gfar_priv_grp *)grp_id);
1da177e4
LT
2630 return IRQ_HANDLED;
2631}
2632
a12f801d 2633static void gfar_new_rxbdp(struct gfar_priv_rx_q *rx_queue, struct rxbd8 *bdp,
bc4598bc 2634 struct sk_buff *skb)
815b97c6 2635{
a12f801d 2636 struct net_device *dev = rx_queue->dev;
815b97c6 2637 struct gfar_private *priv = netdev_priv(dev);
8a102fe0 2638 dma_addr_t buf;
815b97c6 2639
369ec162 2640 buf = dma_map_single(priv->dev, skb->data,
8a102fe0 2641 priv->rx_buffer_size, DMA_FROM_DEVICE);
a12f801d 2642 gfar_init_rxbdp(rx_queue, bdp, buf);
815b97c6
AF
2643}
2644
2281a0f3 2645static struct sk_buff *gfar_alloc_skb(struct net_device *dev)
1da177e4
LT
2646{
2647 struct gfar_private *priv = netdev_priv(dev);
acb600de 2648 struct sk_buff *skb;
1da177e4 2649
acbc0f03 2650 skb = netdev_alloc_skb(dev, priv->rx_buffer_size + RXBUF_ALIGNMENT);
815b97c6 2651 if (!skb)
1da177e4
LT
2652 return NULL;
2653
acbc0f03 2654 gfar_align_skb(skb);
7f7f5316 2655
acbc0f03
EL
2656 return skb;
2657}
2658
2281a0f3 2659struct sk_buff *gfar_new_skb(struct net_device *dev)
acbc0f03 2660{
acb600de 2661 return gfar_alloc_skb(dev);
1da177e4
LT
2662}
2663
298e1a9e 2664static inline void count_errors(unsigned short status, struct net_device *dev)
1da177e4 2665{
298e1a9e 2666 struct gfar_private *priv = netdev_priv(dev);
09f75cd7 2667 struct net_device_stats *stats = &dev->stats;
1da177e4
LT
2668 struct gfar_extra_stats *estats = &priv->extra_stats;
2669
0977f817 2670 /* If the packet was truncated, none of the other errors matter */
1da177e4
LT
2671 if (status & RXBD_TRUNCATED) {
2672 stats->rx_length_errors++;
2673
212079df 2674 atomic64_inc(&estats->rx_trunc);
1da177e4
LT
2675
2676 return;
2677 }
2678 /* Count the errors, if there were any */
2679 if (status & (RXBD_LARGE | RXBD_SHORT)) {
2680 stats->rx_length_errors++;
2681
2682 if (status & RXBD_LARGE)
212079df 2683 atomic64_inc(&estats->rx_large);
1da177e4 2684 else
212079df 2685 atomic64_inc(&estats->rx_short);
1da177e4
LT
2686 }
2687 if (status & RXBD_NONOCTET) {
2688 stats->rx_frame_errors++;
212079df 2689 atomic64_inc(&estats->rx_nonoctet);
1da177e4
LT
2690 }
2691 if (status & RXBD_CRCERR) {
212079df 2692 atomic64_inc(&estats->rx_crcerr);
1da177e4
LT
2693 stats->rx_crc_errors++;
2694 }
2695 if (status & RXBD_OVERRUN) {
212079df 2696 atomic64_inc(&estats->rx_overrun);
1da177e4
LT
2697 stats->rx_crc_errors++;
2698 }
2699}
2700
f4983704 2701irqreturn_t gfar_receive(int irq, void *grp_id)
1da177e4 2702{
f4983704 2703 gfar_schedule_cleanup((struct gfar_priv_grp *)grp_id);
1da177e4
LT
2704 return IRQ_HANDLED;
2705}
2706
0bbaf069
KG
2707static inline void gfar_rx_checksum(struct sk_buff *skb, struct rxfcb *fcb)
2708{
2709 /* If valid headers were found, and valid sums
2710 * were verified, then we tell the kernel that no
0977f817
JC
2711 * checksumming is necessary. Otherwise, it is [FIXME]
2712 */
7f7f5316 2713 if ((fcb->flags & RXFCB_CSUM_MASK) == (RXFCB_CIP | RXFCB_CTU))
0bbaf069
KG
2714 skb->ip_summed = CHECKSUM_UNNECESSARY;
2715 else
bc8acf2c 2716 skb_checksum_none_assert(skb);
0bbaf069
KG
2717}
2718
2719
0977f817 2720/* gfar_process_frame() -- handle one incoming packet if skb isn't NULL. */
61db26c6
CM
2721static void gfar_process_frame(struct net_device *dev, struct sk_buff *skb,
2722 int amount_pull, struct napi_struct *napi)
1da177e4
LT
2723{
2724 struct gfar_private *priv = netdev_priv(dev);
0bbaf069 2725 struct rxfcb *fcb = NULL;
1da177e4 2726
2c2db48a
DH
2727 /* fcb is at the beginning if exists */
2728 fcb = (struct rxfcb *)skb->data;
0bbaf069 2729
0977f817
JC
2730 /* Remove the FCB from the skb
2731 * Remove the padded bytes, if there are any
2732 */
f74dac08
SG
2733 if (amount_pull) {
2734 skb_record_rx_queue(skb, fcb->rq);
2c2db48a 2735 skb_pull(skb, amount_pull);
f74dac08 2736 }
0bbaf069 2737
cc772ab7
MR
2738 /* Get receive timestamp from the skb */
2739 if (priv->hwts_rx_en) {
2740 struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
2741 u64 *ns = (u64 *) skb->data;
bc4598bc 2742
cc772ab7
MR
2743 memset(shhwtstamps, 0, sizeof(*shhwtstamps));
2744 shhwtstamps->hwtstamp = ns_to_ktime(*ns);
2745 }
2746
2747 if (priv->padding)
2748 skb_pull(skb, priv->padding);
2749
8b3afe95 2750 if (dev->features & NETIF_F_RXCSUM)
2c2db48a 2751 gfar_rx_checksum(skb, fcb);
0bbaf069 2752
2c2db48a
DH
2753 /* Tell the skb what kind of packet this is */
2754 skb->protocol = eth_type_trans(skb, dev);
1da177e4 2755
f646968f 2756 /* There's need to check for NETIF_F_HW_VLAN_CTAG_RX here.
32f7fd44
JP
2757 * Even if vlan rx accel is disabled, on some chips
2758 * RXFCB_VLN is pseudo randomly set.
2759 */
f646968f 2760 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX &&
32f7fd44 2761 fcb->flags & RXFCB_VLN)
e5905c83 2762 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), fcb->vlctl);
87c288c6 2763
2c2db48a 2764 /* Send the packet up the stack */
953d2768 2765 napi_gro_receive(napi, skb);
0bbaf069 2766
1da177e4
LT
2767}
2768
2769/* gfar_clean_rx_ring() -- Processes each frame in the rx ring
2281a0f3
JC
2770 * until the budget/quota has been reached. Returns the number
2771 * of frames handled
1da177e4 2772 */
a12f801d 2773int gfar_clean_rx_ring(struct gfar_priv_rx_q *rx_queue, int rx_work_limit)
1da177e4 2774{
a12f801d 2775 struct net_device *dev = rx_queue->dev;
31de198b 2776 struct rxbd8 *bdp, *base;
1da177e4 2777 struct sk_buff *skb;
2c2db48a
DH
2778 int pkt_len;
2779 int amount_pull;
1da177e4
LT
2780 int howmany = 0;
2781 struct gfar_private *priv = netdev_priv(dev);
2782
2783 /* Get the first full descriptor */
a12f801d
SG
2784 bdp = rx_queue->cur_rx;
2785 base = rx_queue->rx_bd_base;
1da177e4 2786
ba779711 2787 amount_pull = priv->uses_rxfcb ? GMAC_FCB_LEN : 0;
2c2db48a 2788
1da177e4 2789 while (!((bdp->status & RXBD_EMPTY) || (--rx_work_limit < 0))) {
815b97c6 2790 struct sk_buff *newskb;
bc4598bc 2791
3b6330ce 2792 rmb();
815b97c6
AF
2793
2794 /* Add another skb for the future */
2795 newskb = gfar_new_skb(dev);
2796
a12f801d 2797 skb = rx_queue->rx_skbuff[rx_queue->skb_currx];
1da177e4 2798
369ec162 2799 dma_unmap_single(priv->dev, bdp->bufPtr,
bc4598bc 2800 priv->rx_buffer_size, DMA_FROM_DEVICE);
81183059 2801
63b88b90 2802 if (unlikely(!(bdp->status & RXBD_ERR) &&
bc4598bc 2803 bdp->length > priv->rx_buffer_size))
63b88b90
AV
2804 bdp->status = RXBD_LARGE;
2805
815b97c6
AF
2806 /* We drop the frame if we failed to allocate a new buffer */
2807 if (unlikely(!newskb || !(bdp->status & RXBD_LAST) ||
bc4598bc 2808 bdp->status & RXBD_ERR)) {
815b97c6
AF
2809 count_errors(bdp->status, dev);
2810
2811 if (unlikely(!newskb))
2812 newskb = skb;
acbc0f03 2813 else if (skb)
acb600de 2814 dev_kfree_skb(skb);
815b97c6 2815 } else {
1da177e4 2816 /* Increment the number of packets */
a7f38041 2817 rx_queue->stats.rx_packets++;
1da177e4
LT
2818 howmany++;
2819
2c2db48a
DH
2820 if (likely(skb)) {
2821 pkt_len = bdp->length - ETH_FCS_LEN;
2822 /* Remove the FCS from the packet length */
2823 skb_put(skb, pkt_len);
a7f38041 2824 rx_queue->stats.rx_bytes += pkt_len;
f74dac08 2825 skb_record_rx_queue(skb, rx_queue->qindex);
cd754a57 2826 gfar_process_frame(dev, skb, amount_pull,
bc4598bc 2827 &rx_queue->grp->napi);
2c2db48a
DH
2828
2829 } else {
59deab26 2830 netif_warn(priv, rx_err, dev, "Missing skb!\n");
a7f38041 2831 rx_queue->stats.rx_dropped++;
212079df 2832 atomic64_inc(&priv->extra_stats.rx_skbmissing);
2c2db48a 2833 }
1da177e4 2834
1da177e4
LT
2835 }
2836
a12f801d 2837 rx_queue->rx_skbuff[rx_queue->skb_currx] = newskb;
1da177e4 2838
815b97c6 2839 /* Setup the new bdp */
a12f801d 2840 gfar_new_rxbdp(rx_queue, bdp, newskb);
1da177e4
LT
2841
2842 /* Update to the next pointer */
a12f801d 2843 bdp = next_bd(bdp, base, rx_queue->rx_ring_size);
1da177e4
LT
2844
2845 /* update to point at the next skb */
bc4598bc
JC
2846 rx_queue->skb_currx = (rx_queue->skb_currx + 1) &
2847 RX_RING_MOD_MASK(rx_queue->rx_ring_size);
1da177e4
LT
2848 }
2849
2850 /* Update the current rxbd pointer to be the next one */
a12f801d 2851 rx_queue->cur_rx = bdp;
1da177e4 2852
1da177e4
LT
2853 return howmany;
2854}
2855
5eaedf31
CM
2856static int gfar_poll_sq(struct napi_struct *napi, int budget)
2857{
2858 struct gfar_priv_grp *gfargrp =
2859 container_of(napi, struct gfar_priv_grp, napi);
2860 struct gfar __iomem *regs = gfargrp->regs;
2861 struct gfar_priv_tx_q *tx_queue = gfargrp->priv->tx_queue[0];
2862 struct gfar_priv_rx_q *rx_queue = gfargrp->priv->rx_queue[0];
2863 int work_done = 0;
2864
2865 /* Clear IEVENT, so interrupts aren't called again
2866 * because of the packets that have already arrived
2867 */
2868 gfar_write(&regs->ievent, IEVENT_RTX_MASK);
2869
2870 /* run Tx cleanup to completion */
2871 if (tx_queue->tx_skbuff[tx_queue->skb_dirtytx])
2872 gfar_clean_tx_ring(tx_queue);
2873
2874 work_done = gfar_clean_rx_ring(rx_queue, budget);
2875
2876 if (work_done < budget) {
2877 napi_complete(napi);
2878 /* Clear the halt bit in RSTAT */
2879 gfar_write(&regs->rstat, gfargrp->rstat);
2880
2881 gfar_write(&regs->imask, IMASK_DEFAULT);
2882
2883 /* If we are coalescing interrupts, update the timer
2884 * Otherwise, clear it
2885 */
2886 gfar_write(&regs->txic, 0);
2887 if (likely(tx_queue->txcoalescing))
2888 gfar_write(&regs->txic, tx_queue->txic);
2889
2890 gfar_write(&regs->rxic, 0);
2891 if (unlikely(rx_queue->rxcoalescing))
2892 gfar_write(&regs->rxic, rx_queue->rxic);
2893 }
2894
2895 return work_done;
2896}
2897
bea3348e 2898static int gfar_poll(struct napi_struct *napi, int budget)
1da177e4 2899{
bc4598bc
JC
2900 struct gfar_priv_grp *gfargrp =
2901 container_of(napi, struct gfar_priv_grp, napi);
fba4ed03 2902 struct gfar_private *priv = gfargrp->priv;
46ceb60c 2903 struct gfar __iomem *regs = gfargrp->regs;
a12f801d 2904 struct gfar_priv_tx_q *tx_queue = NULL;
fba4ed03 2905 struct gfar_priv_rx_q *rx_queue = NULL;
c233cf40 2906 int work_done = 0, work_done_per_q = 0;
39c0a0d5 2907 int i, budget_per_q = 0;
3ba405db 2908 int has_tx_work = 0;
6be5ed3f
CM
2909 unsigned long rstat_rxf;
2910 int num_act_queues;
fba4ed03 2911
8c7396ae 2912 /* Clear IEVENT, so interrupts aren't called again
0977f817
JC
2913 * because of the packets that have already arrived
2914 */
f4983704 2915 gfar_write(&regs->ievent, IEVENT_RTX_MASK);
8c7396ae 2916
6be5ed3f
CM
2917 rstat_rxf = gfar_read(&regs->rstat) & RSTAT_RXF_MASK;
2918
2919 num_act_queues = bitmap_weight(&rstat_rxf, MAX_RX_QS);
2920 if (num_act_queues)
2921 budget_per_q = budget/num_act_queues;
2922
3ba405db
CM
2923 for_each_set_bit(i, &gfargrp->tx_bit_map, priv->num_tx_queues) {
2924 tx_queue = priv->tx_queue[i];
2925 /* run Tx cleanup to completion */
2926 if (tx_queue->tx_skbuff[tx_queue->skb_dirtytx]) {
2927 gfar_clean_tx_ring(tx_queue);
2928 has_tx_work = 1;
c233cf40 2929 }
3ba405db 2930 }
fba4ed03 2931
3ba405db
CM
2932 for_each_set_bit(i, &gfargrp->rx_bit_map, priv->num_rx_queues) {
2933 /* skip queue if not active */
2934 if (!(rstat_rxf & (RSTAT_CLEAR_RXF0 >> i)))
2935 continue;
1da177e4 2936
3ba405db
CM
2937 rx_queue = priv->rx_queue[i];
2938 work_done_per_q =
2939 gfar_clean_rx_ring(rx_queue, budget_per_q);
2940 work_done += work_done_per_q;
2941
2942 /* finished processing this queue */
2943 if (work_done_per_q < budget_per_q) {
2944 /* clear active queue hw indication */
2945 gfar_write(&regs->rstat,
2946 RSTAT_CLEAR_RXF0 >> i);
2947 num_act_queues--;
2948
2949 if (!num_act_queues)
2950 break;
2951 }
2952 }
42199884 2953
3ba405db 2954 if (!num_act_queues && !has_tx_work) {
1da177e4 2955
3ba405db 2956 napi_complete(napi);
1da177e4 2957
3ba405db
CM
2958 /* Clear the halt bit in RSTAT */
2959 gfar_write(&regs->rstat, gfargrp->rstat);
1da177e4 2960
3ba405db 2961 gfar_write(&regs->imask, IMASK_DEFAULT);
c233cf40 2962
3ba405db
CM
2963 /* If we are coalescing interrupts, update the timer
2964 * Otherwise, clear it
2965 */
2966 gfar_configure_coalescing(priv, gfargrp->rx_bit_map,
2967 gfargrp->tx_bit_map);
1da177e4
LT
2968 }
2969
c233cf40 2970 return work_done;
1da177e4 2971}
1da177e4 2972
f2d71c2d 2973#ifdef CONFIG_NET_POLL_CONTROLLER
0977f817 2974/* Polling 'interrupt' - used by things like netconsole to send skbs
f2d71c2d
VW
2975 * without having to re-enable interrupts. It's not called while
2976 * the interrupt routine is executing.
2977 */
2978static void gfar_netpoll(struct net_device *dev)
2979{
2980 struct gfar_private *priv = netdev_priv(dev);
3a2e16c8 2981 int i;
f2d71c2d
VW
2982
2983 /* If the device has multiple interrupts, run tx/rx */
b31a1d8b 2984 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
46ceb60c 2985 for (i = 0; i < priv->num_grps; i++) {
62ed839d
PG
2986 struct gfar_priv_grp *grp = &priv->gfargrp[i];
2987
2988 disable_irq(gfar_irq(grp, TX)->irq);
2989 disable_irq(gfar_irq(grp, RX)->irq);
2990 disable_irq(gfar_irq(grp, ER)->irq);
2991 gfar_interrupt(gfar_irq(grp, TX)->irq, grp);
2992 enable_irq(gfar_irq(grp, ER)->irq);
2993 enable_irq(gfar_irq(grp, RX)->irq);
2994 enable_irq(gfar_irq(grp, TX)->irq);
46ceb60c 2995 }
f2d71c2d 2996 } else {
46ceb60c 2997 for (i = 0; i < priv->num_grps; i++) {
62ed839d
PG
2998 struct gfar_priv_grp *grp = &priv->gfargrp[i];
2999
3000 disable_irq(gfar_irq(grp, TX)->irq);
3001 gfar_interrupt(gfar_irq(grp, TX)->irq, grp);
3002 enable_irq(gfar_irq(grp, TX)->irq);
43de004b 3003 }
f2d71c2d
VW
3004 }
3005}
3006#endif
3007
1da177e4 3008/* The interrupt handler for devices with one interrupt */
f4983704 3009static irqreturn_t gfar_interrupt(int irq, void *grp_id)
1da177e4 3010{
f4983704 3011 struct gfar_priv_grp *gfargrp = grp_id;
1da177e4
LT
3012
3013 /* Save ievent for future reference */
f4983704 3014 u32 events = gfar_read(&gfargrp->regs->ievent);
1da177e4 3015
1da177e4 3016 /* Check for reception */
538cc7ee 3017 if (events & IEVENT_RX_MASK)
f4983704 3018 gfar_receive(irq, grp_id);
1da177e4
LT
3019
3020 /* Check for transmit completion */
538cc7ee 3021 if (events & IEVENT_TX_MASK)
f4983704 3022 gfar_transmit(irq, grp_id);
1da177e4 3023
538cc7ee
SS
3024 /* Check for errors */
3025 if (events & IEVENT_ERR_MASK)
f4983704 3026 gfar_error(irq, grp_id);
1da177e4
LT
3027
3028 return IRQ_HANDLED;
3029}
3030
23402bdd
CM
3031static u32 gfar_get_flowctrl_cfg(struct gfar_private *priv)
3032{
3033 struct phy_device *phydev = priv->phydev;
3034 u32 val = 0;
3035
3036 if (!phydev->duplex)
3037 return val;
3038
3039 if (!priv->pause_aneg_en) {
3040 if (priv->tx_pause_en)
3041 val |= MACCFG1_TX_FLOW;
3042 if (priv->rx_pause_en)
3043 val |= MACCFG1_RX_FLOW;
3044 } else {
3045 u16 lcl_adv, rmt_adv;
3046 u8 flowctrl;
3047 /* get link partner capabilities */
3048 rmt_adv = 0;
3049 if (phydev->pause)
3050 rmt_adv = LPA_PAUSE_CAP;
3051 if (phydev->asym_pause)
3052 rmt_adv |= LPA_PAUSE_ASYM;
3053
3054 lcl_adv = mii_advertise_flowctrl(phydev->advertising);
3055
3056 flowctrl = mii_resolve_flowctrl_fdx(lcl_adv, rmt_adv);
3057 if (flowctrl & FLOW_CTRL_TX)
3058 val |= MACCFG1_TX_FLOW;
3059 if (flowctrl & FLOW_CTRL_RX)
3060 val |= MACCFG1_RX_FLOW;
3061 }
3062
3063 return val;
3064}
3065
1da177e4
LT
3066/* Called every time the controller might need to be made
3067 * aware of new link state. The PHY code conveys this
bb40dcbb 3068 * information through variables in the phydev structure, and this
1da177e4
LT
3069 * function converts those variables into the appropriate
3070 * register values, and can bring down the device if needed.
3071 */
3072static void adjust_link(struct net_device *dev)
3073{
3074 struct gfar_private *priv = netdev_priv(dev);
46ceb60c 3075 struct gfar __iomem *regs = priv->gfargrp[0].regs;
bb40dcbb
AF
3076 unsigned long flags;
3077 struct phy_device *phydev = priv->phydev;
3078 int new_state = 0;
3079
fba4ed03
SG
3080 local_irq_save(flags);
3081 lock_tx_qs(priv);
3082
bb40dcbb 3083 if (phydev->link) {
23402bdd 3084 u32 tempval1 = gfar_read(&regs->maccfg1);
bb40dcbb 3085 u32 tempval = gfar_read(&regs->maccfg2);
7f7f5316 3086 u32 ecntrl = gfar_read(&regs->ecntrl);
1da177e4 3087
1da177e4 3088 /* Now we make sure that we can be in full duplex mode.
0977f817
JC
3089 * If not, we operate in half-duplex mode.
3090 */
bb40dcbb
AF
3091 if (phydev->duplex != priv->oldduplex) {
3092 new_state = 1;
3093 if (!(phydev->duplex))
1da177e4 3094 tempval &= ~(MACCFG2_FULL_DUPLEX);
bb40dcbb 3095 else
1da177e4 3096 tempval |= MACCFG2_FULL_DUPLEX;
1da177e4 3097
bb40dcbb 3098 priv->oldduplex = phydev->duplex;
1da177e4
LT
3099 }
3100
bb40dcbb
AF
3101 if (phydev->speed != priv->oldspeed) {
3102 new_state = 1;
3103 switch (phydev->speed) {
1da177e4 3104 case 1000:
1da177e4
LT
3105 tempval =
3106 ((tempval & ~(MACCFG2_IF)) | MACCFG2_GMII);
f430e49e
LY
3107
3108 ecntrl &= ~(ECNTRL_R100);
1da177e4
LT
3109 break;
3110 case 100:
3111 case 10:
1da177e4
LT
3112 tempval =
3113 ((tempval & ~(MACCFG2_IF)) | MACCFG2_MII);
7f7f5316
AF
3114
3115 /* Reduced mode distinguishes
0977f817
JC
3116 * between 10 and 100
3117 */
7f7f5316
AF
3118 if (phydev->speed == SPEED_100)
3119 ecntrl |= ECNTRL_R100;
3120 else
3121 ecntrl &= ~(ECNTRL_R100);
1da177e4
LT
3122 break;
3123 default:
59deab26
JP
3124 netif_warn(priv, link, dev,
3125 "Ack! Speed (%d) is not 10/100/1000!\n",
3126 phydev->speed);
1da177e4
LT
3127 break;
3128 }
3129
bb40dcbb 3130 priv->oldspeed = phydev->speed;
1da177e4
LT
3131 }
3132
23402bdd
CM
3133 tempval1 &= ~(MACCFG1_TX_FLOW | MACCFG1_RX_FLOW);
3134 tempval1 |= gfar_get_flowctrl_cfg(priv);
3135
3136 gfar_write(&regs->maccfg1, tempval1);
bb40dcbb 3137 gfar_write(&regs->maccfg2, tempval);
7f7f5316 3138 gfar_write(&regs->ecntrl, ecntrl);
bb40dcbb 3139
1da177e4 3140 if (!priv->oldlink) {
bb40dcbb 3141 new_state = 1;
1da177e4 3142 priv->oldlink = 1;
1da177e4 3143 }
bb40dcbb
AF
3144 } else if (priv->oldlink) {
3145 new_state = 1;
3146 priv->oldlink = 0;
3147 priv->oldspeed = 0;
3148 priv->oldduplex = -1;
1da177e4 3149 }
1da177e4 3150
bb40dcbb
AF
3151 if (new_state && netif_msg_link(priv))
3152 phy_print_status(phydev);
fba4ed03
SG
3153 unlock_tx_qs(priv);
3154 local_irq_restore(flags);
bb40dcbb 3155}
1da177e4
LT
3156
3157/* Update the hash table based on the current list of multicast
3158 * addresses we subscribe to. Also, change the promiscuity of
3159 * the device based on the flags (this function is called
0977f817
JC
3160 * whenever dev->flags is changed
3161 */
1da177e4
LT
3162static void gfar_set_multi(struct net_device *dev)
3163{
22bedad3 3164 struct netdev_hw_addr *ha;
1da177e4 3165 struct gfar_private *priv = netdev_priv(dev);
46ceb60c 3166 struct gfar __iomem *regs = priv->gfargrp[0].regs;
1da177e4
LT
3167 u32 tempval;
3168
a12f801d 3169 if (dev->flags & IFF_PROMISC) {
1da177e4
LT
3170 /* Set RCTRL to PROM */
3171 tempval = gfar_read(&regs->rctrl);
3172 tempval |= RCTRL_PROM;
3173 gfar_write(&regs->rctrl, tempval);
3174 } else {
3175 /* Set RCTRL to not PROM */
3176 tempval = gfar_read(&regs->rctrl);
3177 tempval &= ~(RCTRL_PROM);
3178 gfar_write(&regs->rctrl, tempval);
3179 }
6aa20a22 3180
a12f801d 3181 if (dev->flags & IFF_ALLMULTI) {
1da177e4 3182 /* Set the hash to rx all multicast frames */
0bbaf069
KG
3183 gfar_write(&regs->igaddr0, 0xffffffff);
3184 gfar_write(&regs->igaddr1, 0xffffffff);
3185 gfar_write(&regs->igaddr2, 0xffffffff);
3186 gfar_write(&regs->igaddr3, 0xffffffff);
3187 gfar_write(&regs->igaddr4, 0xffffffff);
3188 gfar_write(&regs->igaddr5, 0xffffffff);
3189 gfar_write(&regs->igaddr6, 0xffffffff);
3190 gfar_write(&regs->igaddr7, 0xffffffff);
1da177e4
LT
3191 gfar_write(&regs->gaddr0, 0xffffffff);
3192 gfar_write(&regs->gaddr1, 0xffffffff);
3193 gfar_write(&regs->gaddr2, 0xffffffff);
3194 gfar_write(&regs->gaddr3, 0xffffffff);
3195 gfar_write(&regs->gaddr4, 0xffffffff);
3196 gfar_write(&regs->gaddr5, 0xffffffff);
3197 gfar_write(&regs->gaddr6, 0xffffffff);
3198 gfar_write(&regs->gaddr7, 0xffffffff);
3199 } else {
7f7f5316
AF
3200 int em_num;
3201 int idx;
3202
1da177e4 3203 /* zero out the hash */
0bbaf069
KG
3204 gfar_write(&regs->igaddr0, 0x0);
3205 gfar_write(&regs->igaddr1, 0x0);
3206 gfar_write(&regs->igaddr2, 0x0);
3207 gfar_write(&regs->igaddr3, 0x0);
3208 gfar_write(&regs->igaddr4, 0x0);
3209 gfar_write(&regs->igaddr5, 0x0);
3210 gfar_write(&regs->igaddr6, 0x0);
3211 gfar_write(&regs->igaddr7, 0x0);
1da177e4
LT
3212 gfar_write(&regs->gaddr0, 0x0);
3213 gfar_write(&regs->gaddr1, 0x0);
3214 gfar_write(&regs->gaddr2, 0x0);
3215 gfar_write(&regs->gaddr3, 0x0);
3216 gfar_write(&regs->gaddr4, 0x0);
3217 gfar_write(&regs->gaddr5, 0x0);
3218 gfar_write(&regs->gaddr6, 0x0);
3219 gfar_write(&regs->gaddr7, 0x0);
3220
7f7f5316
AF
3221 /* If we have extended hash tables, we need to
3222 * clear the exact match registers to prepare for
0977f817
JC
3223 * setting them
3224 */
7f7f5316
AF
3225 if (priv->extended_hash) {
3226 em_num = GFAR_EM_NUM + 1;
3227 gfar_clear_exact_match(dev);
3228 idx = 1;
3229 } else {
3230 idx = 0;
3231 em_num = 0;
3232 }
3233
4cd24eaf 3234 if (netdev_mc_empty(dev))
1da177e4
LT
3235 return;
3236
3237 /* Parse the list, and set the appropriate bits */
22bedad3 3238 netdev_for_each_mc_addr(ha, dev) {
7f7f5316 3239 if (idx < em_num) {
22bedad3 3240 gfar_set_mac_for_addr(dev, idx, ha->addr);
7f7f5316
AF
3241 idx++;
3242 } else
22bedad3 3243 gfar_set_hash_for_addr(dev, ha->addr);
1da177e4
LT
3244 }
3245 }
1da177e4
LT
3246}
3247
7f7f5316
AF
3248
3249/* Clears each of the exact match registers to zero, so they
0977f817
JC
3250 * don't interfere with normal reception
3251 */
7f7f5316
AF
3252static void gfar_clear_exact_match(struct net_device *dev)
3253{
3254 int idx;
6a3c910c 3255 static const u8 zero_arr[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
7f7f5316 3256
bc4598bc 3257 for (idx = 1; idx < GFAR_EM_NUM + 1; idx++)
b6bc7650 3258 gfar_set_mac_for_addr(dev, idx, zero_arr);
7f7f5316
AF
3259}
3260
1da177e4
LT
3261/* Set the appropriate hash bit for the given addr */
3262/* The algorithm works like so:
3263 * 1) Take the Destination Address (ie the multicast address), and
3264 * do a CRC on it (little endian), and reverse the bits of the
3265 * result.
3266 * 2) Use the 8 most significant bits as a hash into a 256-entry
3267 * table. The table is controlled through 8 32-bit registers:
3268 * gaddr0-7. gaddr0's MSB is entry 0, and gaddr7's LSB is
3269 * gaddr7. This means that the 3 most significant bits in the
3270 * hash index which gaddr register to use, and the 5 other bits
3271 * indicate which bit (assuming an IBM numbering scheme, which
3272 * for PowerPC (tm) is usually the case) in the register holds
0977f817
JC
3273 * the entry.
3274 */
1da177e4
LT
3275static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr)
3276{
3277 u32 tempval;
3278 struct gfar_private *priv = netdev_priv(dev);
6a3c910c 3279 u32 result = ether_crc(ETH_ALEN, addr);
0bbaf069
KG
3280 int width = priv->hash_width;
3281 u8 whichbit = (result >> (32 - width)) & 0x1f;
3282 u8 whichreg = result >> (32 - width + 5);
1da177e4
LT
3283 u32 value = (1 << (31-whichbit));
3284
0bbaf069 3285 tempval = gfar_read(priv->hash_regs[whichreg]);
1da177e4 3286 tempval |= value;
0bbaf069 3287 gfar_write(priv->hash_regs[whichreg], tempval);
1da177e4
LT
3288}
3289
7f7f5316
AF
3290
3291/* There are multiple MAC Address register pairs on some controllers
3292 * This function sets the numth pair to a given address
3293 */
b6bc7650
JP
3294static void gfar_set_mac_for_addr(struct net_device *dev, int num,
3295 const u8 *addr)
7f7f5316
AF
3296{
3297 struct gfar_private *priv = netdev_priv(dev);
46ceb60c 3298 struct gfar __iomem *regs = priv->gfargrp[0].regs;
7f7f5316 3299 int idx;
6a3c910c 3300 char tmpbuf[ETH_ALEN];
7f7f5316 3301 u32 tempval;
f4983704 3302 u32 __iomem *macptr = &regs->macstnaddr1;
7f7f5316
AF
3303
3304 macptr += num*2;
3305
0977f817
JC
3306 /* Now copy it into the mac registers backwards, cuz
3307 * little endian is silly
3308 */
6a3c910c
JP
3309 for (idx = 0; idx < ETH_ALEN; idx++)
3310 tmpbuf[ETH_ALEN - 1 - idx] = addr[idx];
7f7f5316
AF
3311
3312 gfar_write(macptr, *((u32 *) (tmpbuf)));
3313
3314 tempval = *((u32 *) (tmpbuf + 4));
3315
3316 gfar_write(macptr+1, tempval);
3317}
3318
1da177e4 3319/* GFAR error interrupt handler */
f4983704 3320static irqreturn_t gfar_error(int irq, void *grp_id)
1da177e4 3321{
f4983704
SG
3322 struct gfar_priv_grp *gfargrp = grp_id;
3323 struct gfar __iomem *regs = gfargrp->regs;
3324 struct gfar_private *priv= gfargrp->priv;
3325 struct net_device *dev = priv->ndev;
1da177e4
LT
3326
3327 /* Save ievent for future reference */
f4983704 3328 u32 events = gfar_read(&regs->ievent);
1da177e4
LT
3329
3330 /* Clear IEVENT */
f4983704 3331 gfar_write(&regs->ievent, events & IEVENT_ERR_MASK);
d87eb127
SW
3332
3333 /* Magic Packet is not an error. */
b31a1d8b 3334 if ((priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET) &&
d87eb127
SW
3335 (events & IEVENT_MAG))
3336 events &= ~IEVENT_MAG;
1da177e4
LT
3337
3338 /* Hmm... */
0bbaf069 3339 if (netif_msg_rx_err(priv) || netif_msg_tx_err(priv))
bc4598bc
JC
3340 netdev_dbg(dev,
3341 "error interrupt (ievent=0x%08x imask=0x%08x)\n",
59deab26 3342 events, gfar_read(&regs->imask));
1da177e4
LT
3343
3344 /* Update the error counters */
3345 if (events & IEVENT_TXE) {
09f75cd7 3346 dev->stats.tx_errors++;
1da177e4
LT
3347
3348 if (events & IEVENT_LC)
09f75cd7 3349 dev->stats.tx_window_errors++;
1da177e4 3350 if (events & IEVENT_CRL)
09f75cd7 3351 dev->stats.tx_aborted_errors++;
1da177e4 3352 if (events & IEVENT_XFUN) {
836cf7fa
AV
3353 unsigned long flags;
3354
59deab26
JP
3355 netif_dbg(priv, tx_err, dev,
3356 "TX FIFO underrun, packet dropped\n");
09f75cd7 3357 dev->stats.tx_dropped++;
212079df 3358 atomic64_inc(&priv->extra_stats.tx_underrun);
1da177e4 3359
836cf7fa
AV
3360 local_irq_save(flags);
3361 lock_tx_qs(priv);
3362
1da177e4 3363 /* Reactivate the Tx Queues */
fba4ed03 3364 gfar_write(&regs->tstat, gfargrp->tstat);
836cf7fa
AV
3365
3366 unlock_tx_qs(priv);
3367 local_irq_restore(flags);
1da177e4 3368 }
59deab26 3369 netif_dbg(priv, tx_err, dev, "Transmit Error\n");
1da177e4
LT
3370 }
3371 if (events & IEVENT_BSY) {
09f75cd7 3372 dev->stats.rx_errors++;
212079df 3373 atomic64_inc(&priv->extra_stats.rx_bsy);
1da177e4 3374
f4983704 3375 gfar_receive(irq, grp_id);
1da177e4 3376
59deab26
JP
3377 netif_dbg(priv, rx_err, dev, "busy error (rstat: %x)\n",
3378 gfar_read(&regs->rstat));
1da177e4
LT
3379 }
3380 if (events & IEVENT_BABR) {
09f75cd7 3381 dev->stats.rx_errors++;
212079df 3382 atomic64_inc(&priv->extra_stats.rx_babr);
1da177e4 3383
59deab26 3384 netif_dbg(priv, rx_err, dev, "babbling RX error\n");
1da177e4
LT
3385 }
3386 if (events & IEVENT_EBERR) {
212079df 3387 atomic64_inc(&priv->extra_stats.eberr);
59deab26 3388 netif_dbg(priv, rx_err, dev, "bus error\n");
1da177e4 3389 }
59deab26
JP
3390 if (events & IEVENT_RXC)
3391 netif_dbg(priv, rx_status, dev, "control frame\n");
1da177e4
LT
3392
3393 if (events & IEVENT_BABT) {
212079df 3394 atomic64_inc(&priv->extra_stats.tx_babt);
59deab26 3395 netif_dbg(priv, tx_err, dev, "babbling TX error\n");
1da177e4
LT
3396 }
3397 return IRQ_HANDLED;
3398}
3399
b31a1d8b
AF
3400static struct of_device_id gfar_match[] =
3401{
3402 {
3403 .type = "network",
3404 .compatible = "gianfar",
3405 },
46ceb60c
SG
3406 {
3407 .compatible = "fsl,etsec2",
3408 },
b31a1d8b
AF
3409 {},
3410};
e72701ac 3411MODULE_DEVICE_TABLE(of, gfar_match);
b31a1d8b 3412
1da177e4 3413/* Structure for a device driver */
74888760 3414static struct platform_driver gfar_driver = {
4018294b
GL
3415 .driver = {
3416 .name = "fsl-gianfar",
3417 .owner = THIS_MODULE,
3418 .pm = GFAR_PM_OPS,
3419 .of_match_table = gfar_match,
3420 },
1da177e4
LT
3421 .probe = gfar_probe,
3422 .remove = gfar_remove,
3423};
3424
db62f684 3425module_platform_driver(gfar_driver);