greth: convert to netdev_tx_t
[linux-2.6.git] / drivers / net / greth.c
1 /*
2  * Aeroflex Gaisler GRETH 10/100/1G Ethernet MAC.
3  *
4  * 2005-2009 (c) Aeroflex Gaisler AB
5  *
6  * This driver supports GRETH 10/100 and GRETH 10/100/1G Ethernet MACs
7  * available in the GRLIB VHDL IP core library.
8  *
9  * Full documentation of both cores can be found here:
10  * http://www.gaisler.com/products/grlib/grip.pdf
11  *
12  * The Gigabit version supports scatter/gather DMA, any alignment of
13  * buffers and checksum offloading.
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  * Contributors: Kristoffer Glembo
21  *               Daniel Hellstrom
22  *               Marko Isomaki
23  */
24
25 #include <linux/module.h>
26 #include <linux/uaccess.h>
27 #include <linux/init.h>
28 #include <linux/netdevice.h>
29 #include <linux/etherdevice.h>
30 #include <linux/ethtool.h>
31 #include <linux/skbuff.h>
32 #include <linux/io.h>
33 #include <linux/crc32.h>
34 #include <linux/mii.h>
35 #include <linux/of_device.h>
36 #include <linux/of_platform.h>
37 #include <asm/cacheflush.h>
38 #include <asm/byteorder.h>
39
40 #ifdef CONFIG_SPARC
41 #include <asm/idprom.h>
42 #endif
43
44 #include "greth.h"
45
46 #define GRETH_DEF_MSG_ENABLE      \
47         (NETIF_MSG_DRV          | \
48          NETIF_MSG_PROBE        | \
49          NETIF_MSG_LINK         | \
50          NETIF_MSG_IFDOWN       | \
51          NETIF_MSG_IFUP         | \
52          NETIF_MSG_RX_ERR       | \
53          NETIF_MSG_TX_ERR)
54
55 static int greth_debug = -1;    /* -1 == use GRETH_DEF_MSG_ENABLE as value */
56 module_param(greth_debug, int, 0);
57 MODULE_PARM_DESC(greth_debug, "GRETH bitmapped debugging message enable value");
58
59 /* Accept MAC address of the form macaddr=0x08,0x00,0x20,0x30,0x40,0x50 */
60 static int macaddr[6];
61 module_param_array(macaddr, int, NULL, 0);
62 MODULE_PARM_DESC(macaddr, "GRETH Ethernet MAC address");
63
64 static int greth_edcl = 1;
65 module_param(greth_edcl, int, 0);
66 MODULE_PARM_DESC(greth_edcl, "GRETH EDCL usage indicator. Set to 1 if EDCL is used.");
67
68 static int greth_open(struct net_device *dev);
69 static netdev_tx_t greth_start_xmit(struct sk_buff *skb,
70            struct net_device *dev);
71 static netdev_tx_t greth_start_xmit_gbit(struct sk_buff *skb,
72            struct net_device *dev);
73 static int greth_rx(struct net_device *dev, int limit);
74 static int greth_rx_gbit(struct net_device *dev, int limit);
75 static void greth_clean_tx(struct net_device *dev);
76 static void greth_clean_tx_gbit(struct net_device *dev);
77 static irqreturn_t greth_interrupt(int irq, void *dev_id);
78 static int greth_close(struct net_device *dev);
79 static int greth_set_mac_add(struct net_device *dev, void *p);
80 static void greth_set_multicast_list(struct net_device *dev);
81
82 #define GRETH_REGLOAD(a)            (be32_to_cpu(__raw_readl(&(a))))
83 #define GRETH_REGSAVE(a, v)         (__raw_writel(cpu_to_be32(v), &(a)))
84 #define GRETH_REGORIN(a, v)         (GRETH_REGSAVE(a, (GRETH_REGLOAD(a) | (v))))
85 #define GRETH_REGANDIN(a, v)        (GRETH_REGSAVE(a, (GRETH_REGLOAD(a) & (v))))
86
87 #define NEXT_TX(N)      (((N) + 1) & GRETH_TXBD_NUM_MASK)
88 #define SKIP_TX(N, C)   (((N) + C) & GRETH_TXBD_NUM_MASK)
89 #define NEXT_RX(N)      (((N) + 1) & GRETH_RXBD_NUM_MASK)
90
91 static void greth_print_rx_packet(void *addr, int len)
92 {
93         print_hex_dump(KERN_DEBUG, "RX: ", DUMP_PREFIX_OFFSET, 16, 1,
94                         addr, len, true);
95 }
96
97 static void greth_print_tx_packet(struct sk_buff *skb)
98 {
99         int i;
100         int length;
101
102         if (skb_shinfo(skb)->nr_frags == 0)
103                 length = skb->len;
104         else
105                 length = skb_headlen(skb);
106
107         print_hex_dump(KERN_DEBUG, "TX: ", DUMP_PREFIX_OFFSET, 16, 1,
108                         skb->data, length, true);
109
110         for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
111
112                 print_hex_dump(KERN_DEBUG, "TX: ", DUMP_PREFIX_OFFSET, 16, 1,
113                                phys_to_virt(page_to_phys(skb_shinfo(skb)->frags[i].page)) +
114                                skb_shinfo(skb)->frags[i].page_offset,
115                                length, true);
116         }
117 }
118
119 static inline void greth_enable_tx(struct greth_private *greth)
120 {
121         wmb();
122         GRETH_REGORIN(greth->regs->control, GRETH_TXEN);
123 }
124
125 static inline void greth_disable_tx(struct greth_private *greth)
126 {
127         GRETH_REGANDIN(greth->regs->control, ~GRETH_TXEN);
128 }
129
130 static inline void greth_enable_rx(struct greth_private *greth)
131 {
132         wmb();
133         GRETH_REGORIN(greth->regs->control, GRETH_RXEN);
134 }
135
136 static inline void greth_disable_rx(struct greth_private *greth)
137 {
138         GRETH_REGANDIN(greth->regs->control, ~GRETH_RXEN);
139 }
140
141 static inline void greth_enable_irqs(struct greth_private *greth)
142 {
143         GRETH_REGORIN(greth->regs->control, GRETH_RXI | GRETH_TXI);
144 }
145
146 static inline void greth_disable_irqs(struct greth_private *greth)
147 {
148         GRETH_REGANDIN(greth->regs->control, ~(GRETH_RXI|GRETH_TXI));
149 }
150
151 static inline void greth_write_bd(u32 *bd, u32 val)
152 {
153         __raw_writel(cpu_to_be32(val), bd);
154 }
155
156 static inline u32 greth_read_bd(u32 *bd)
157 {
158         return be32_to_cpu(__raw_readl(bd));
159 }
160
161 static void greth_clean_rings(struct greth_private *greth)
162 {
163         int i;
164         struct greth_bd *rx_bdp = greth->rx_bd_base;
165         struct greth_bd *tx_bdp = greth->tx_bd_base;
166
167         if (greth->gbit_mac) {
168
169                 /* Free and unmap RX buffers */
170                 for (i = 0; i < GRETH_RXBD_NUM; i++, rx_bdp++) {
171                         if (greth->rx_skbuff[i] != NULL) {
172                                 dev_kfree_skb(greth->rx_skbuff[i]);
173                                 dma_unmap_single(greth->dev,
174                                                  greth_read_bd(&rx_bdp->addr),
175                                                  MAX_FRAME_SIZE+NET_IP_ALIGN,
176                                                  DMA_FROM_DEVICE);
177                         }
178                 }
179
180                 /* TX buffers */
181                 while (greth->tx_free < GRETH_TXBD_NUM) {
182
183                         struct sk_buff *skb = greth->tx_skbuff[greth->tx_last];
184                         int nr_frags = skb_shinfo(skb)->nr_frags;
185                         tx_bdp = greth->tx_bd_base + greth->tx_last;
186                         greth->tx_last = NEXT_TX(greth->tx_last);
187
188                         dma_unmap_single(greth->dev,
189                                          greth_read_bd(&tx_bdp->addr),
190                                          skb_headlen(skb),
191                                          DMA_TO_DEVICE);
192
193                         for (i = 0; i < nr_frags; i++) {
194                                 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
195                                 tx_bdp = greth->tx_bd_base + greth->tx_last;
196
197                                 dma_unmap_page(greth->dev,
198                                                greth_read_bd(&tx_bdp->addr),
199                                                frag->size,
200                                                DMA_TO_DEVICE);
201
202                                 greth->tx_last = NEXT_TX(greth->tx_last);
203                         }
204                         greth->tx_free += nr_frags+1;
205                         dev_kfree_skb(skb);
206                 }
207
208
209         } else { /* 10/100 Mbps MAC */
210
211                 for (i = 0; i < GRETH_RXBD_NUM; i++, rx_bdp++) {
212                         kfree(greth->rx_bufs[i]);
213                         dma_unmap_single(greth->dev,
214                                          greth_read_bd(&rx_bdp->addr),
215                                          MAX_FRAME_SIZE,
216                                          DMA_FROM_DEVICE);
217                 }
218                 for (i = 0; i < GRETH_TXBD_NUM; i++, tx_bdp++) {
219                         kfree(greth->tx_bufs[i]);
220                         dma_unmap_single(greth->dev,
221                                          greth_read_bd(&tx_bdp->addr),
222                                          MAX_FRAME_SIZE,
223                                          DMA_TO_DEVICE);
224                 }
225         }
226 }
227
228 static int greth_init_rings(struct greth_private *greth)
229 {
230         struct sk_buff *skb;
231         struct greth_bd *rx_bd, *tx_bd;
232         u32 dma_addr;
233         int i;
234
235         rx_bd = greth->rx_bd_base;
236         tx_bd = greth->tx_bd_base;
237
238         /* Initialize descriptor rings and buffers */
239         if (greth->gbit_mac) {
240
241                 for (i = 0; i < GRETH_RXBD_NUM; i++) {
242                         skb = netdev_alloc_skb(greth->netdev, MAX_FRAME_SIZE+NET_IP_ALIGN);
243                         if (skb == NULL) {
244                                 if (netif_msg_ifup(greth))
245                                         dev_err(greth->dev, "Error allocating DMA ring.\n");
246                                 goto cleanup;
247                         }
248                         skb_reserve(skb, NET_IP_ALIGN);
249                         dma_addr = dma_map_single(greth->dev,
250                                                   skb->data,
251                                                   MAX_FRAME_SIZE+NET_IP_ALIGN,
252                                                   DMA_FROM_DEVICE);
253
254                         if (dma_mapping_error(greth->dev, dma_addr)) {
255                                 if (netif_msg_ifup(greth))
256                                         dev_err(greth->dev, "Could not create initial DMA mapping\n");
257                                 goto cleanup;
258                         }
259                         greth->rx_skbuff[i] = skb;
260                         greth_write_bd(&rx_bd[i].addr, dma_addr);
261                         greth_write_bd(&rx_bd[i].stat, GRETH_BD_EN | GRETH_BD_IE);
262                 }
263
264         } else {
265
266                 /* 10/100 MAC uses a fixed set of buffers and copy to/from SKBs */
267                 for (i = 0; i < GRETH_RXBD_NUM; i++) {
268
269                         greth->rx_bufs[i] = kmalloc(MAX_FRAME_SIZE, GFP_KERNEL);
270
271                         if (greth->rx_bufs[i] == NULL) {
272                                 if (netif_msg_ifup(greth))
273                                         dev_err(greth->dev, "Error allocating DMA ring.\n");
274                                 goto cleanup;
275                         }
276
277                         dma_addr = dma_map_single(greth->dev,
278                                                   greth->rx_bufs[i],
279                                                   MAX_FRAME_SIZE,
280                                                   DMA_FROM_DEVICE);
281
282                         if (dma_mapping_error(greth->dev, dma_addr)) {
283                                 if (netif_msg_ifup(greth))
284                                         dev_err(greth->dev, "Could not create initial DMA mapping\n");
285                                 goto cleanup;
286                         }
287                         greth_write_bd(&rx_bd[i].addr, dma_addr);
288                         greth_write_bd(&rx_bd[i].stat, GRETH_BD_EN | GRETH_BD_IE);
289                 }
290                 for (i = 0; i < GRETH_TXBD_NUM; i++) {
291
292                         greth->tx_bufs[i] = kmalloc(MAX_FRAME_SIZE, GFP_KERNEL);
293
294                         if (greth->tx_bufs[i] == NULL) {
295                                 if (netif_msg_ifup(greth))
296                                         dev_err(greth->dev, "Error allocating DMA ring.\n");
297                                 goto cleanup;
298                         }
299
300                         dma_addr = dma_map_single(greth->dev,
301                                                   greth->tx_bufs[i],
302                                                   MAX_FRAME_SIZE,
303                                                   DMA_TO_DEVICE);
304
305                         if (dma_mapping_error(greth->dev, dma_addr)) {
306                                 if (netif_msg_ifup(greth))
307                                         dev_err(greth->dev, "Could not create initial DMA mapping\n");
308                                 goto cleanup;
309                         }
310                         greth_write_bd(&tx_bd[i].addr, dma_addr);
311                         greth_write_bd(&tx_bd[i].stat, 0);
312                 }
313         }
314         greth_write_bd(&rx_bd[GRETH_RXBD_NUM - 1].stat,
315                        greth_read_bd(&rx_bd[GRETH_RXBD_NUM - 1].stat) | GRETH_BD_WR);
316
317         /* Initialize pointers. */
318         greth->rx_cur = 0;
319         greth->tx_next = 0;
320         greth->tx_last = 0;
321         greth->tx_free = GRETH_TXBD_NUM;
322
323         /* Initialize descriptor base address */
324         GRETH_REGSAVE(greth->regs->tx_desc_p, greth->tx_bd_base_phys);
325         GRETH_REGSAVE(greth->regs->rx_desc_p, greth->rx_bd_base_phys);
326
327         return 0;
328
329 cleanup:
330         greth_clean_rings(greth);
331         return -ENOMEM;
332 }
333
334 static int greth_open(struct net_device *dev)
335 {
336         struct greth_private *greth = netdev_priv(dev);
337         int err;
338
339         err = greth_init_rings(greth);
340         if (err) {
341                 if (netif_msg_ifup(greth))
342                         dev_err(&dev->dev, "Could not allocate memory for DMA rings\n");
343                 return err;
344         }
345
346         err = request_irq(greth->irq, greth_interrupt, 0, "eth", (void *) dev);
347         if (err) {
348                 if (netif_msg_ifup(greth))
349                         dev_err(&dev->dev, "Could not allocate interrupt %d\n", dev->irq);
350                 greth_clean_rings(greth);
351                 return err;
352         }
353
354         if (netif_msg_ifup(greth))
355                 dev_dbg(&dev->dev, " starting queue\n");
356         netif_start_queue(dev);
357
358         napi_enable(&greth->napi);
359
360         greth_enable_irqs(greth);
361         greth_enable_tx(greth);
362         greth_enable_rx(greth);
363         return 0;
364
365 }
366
367 static int greth_close(struct net_device *dev)
368 {
369         struct greth_private *greth = netdev_priv(dev);
370
371         napi_disable(&greth->napi);
372
373         greth_disable_tx(greth);
374
375         netif_stop_queue(dev);
376
377         free_irq(greth->irq, (void *) dev);
378
379         greth_clean_rings(greth);
380
381         return 0;
382 }
383
384 static netdev_tx_t
385 greth_start_xmit(struct sk_buff *skb, struct net_device *dev)
386 {
387         struct greth_private *greth = netdev_priv(dev);
388         struct greth_bd *bdp;
389         int err = NETDEV_TX_OK;
390         u32 status, dma_addr;
391
392         bdp = greth->tx_bd_base + greth->tx_next;
393
394         if (unlikely(greth->tx_free <= 0)) {
395                 netif_stop_queue(dev);
396                 return NETDEV_TX_BUSY;
397         }
398
399         if (netif_msg_pktdata(greth))
400                 greth_print_tx_packet(skb);
401
402
403         if (unlikely(skb->len > MAX_FRAME_SIZE)) {
404                 dev->stats.tx_errors++;
405                 goto out;
406         }
407
408         dma_addr = greth_read_bd(&bdp->addr);
409
410         memcpy((unsigned char *) phys_to_virt(dma_addr), skb->data, skb->len);
411
412         dma_sync_single_for_device(greth->dev, dma_addr, skb->len, DMA_TO_DEVICE);
413
414         status = GRETH_BD_EN | (skb->len & GRETH_BD_LEN);
415
416         /* Wrap around descriptor ring */
417         if (greth->tx_next == GRETH_TXBD_NUM_MASK) {
418                 status |= GRETH_BD_WR;
419         }
420
421         greth->tx_next = NEXT_TX(greth->tx_next);
422         greth->tx_free--;
423
424         /* No more descriptors */
425         if (unlikely(greth->tx_free == 0)) {
426
427                 /* Free transmitted descriptors */
428                 greth_clean_tx(dev);
429
430                 /* If nothing was cleaned, stop queue & wait for irq */
431                 if (unlikely(greth->tx_free == 0)) {
432                         status |= GRETH_BD_IE;
433                         netif_stop_queue(dev);
434                 }
435         }
436
437         /* Write descriptor control word and enable transmission */
438         greth_write_bd(&bdp->stat, status);
439         greth_enable_tx(greth);
440
441 out:
442         dev_kfree_skb(skb);
443         return err;
444 }
445
446
447 static netdev_tx_t
448 greth_start_xmit_gbit(struct sk_buff *skb, struct net_device *dev)
449 {
450         struct greth_private *greth = netdev_priv(dev);
451         struct greth_bd *bdp;
452         u32 status = 0, dma_addr;
453         int curr_tx, nr_frags, i, err = NETDEV_TX_OK;
454
455         nr_frags = skb_shinfo(skb)->nr_frags;
456
457         if (greth->tx_free < nr_frags + 1) {
458                 netif_stop_queue(dev);
459                 err = NETDEV_TX_BUSY;
460                 goto out;
461         }
462
463         if (netif_msg_pktdata(greth))
464                 greth_print_tx_packet(skb);
465
466         if (unlikely(skb->len > MAX_FRAME_SIZE)) {
467                 dev->stats.tx_errors++;
468                 goto out;
469         }
470
471         /* Save skb pointer. */
472         greth->tx_skbuff[greth->tx_next] = skb;
473
474         /* Linear buf */
475         if (nr_frags != 0)
476                 status = GRETH_TXBD_MORE;
477
478         status |= GRETH_TXBD_CSALL;
479         status |= skb_headlen(skb) & GRETH_BD_LEN;
480         if (greth->tx_next == GRETH_TXBD_NUM_MASK)
481                 status |= GRETH_BD_WR;
482
483
484         bdp = greth->tx_bd_base + greth->tx_next;
485         greth_write_bd(&bdp->stat, status);
486         dma_addr = dma_map_single(greth->dev, skb->data, skb_headlen(skb), DMA_TO_DEVICE);
487
488         if (unlikely(dma_mapping_error(greth->dev, dma_addr)))
489                 goto map_error;
490
491         greth_write_bd(&bdp->addr, dma_addr);
492
493         curr_tx = NEXT_TX(greth->tx_next);
494
495         /* Frags */
496         for (i = 0; i < nr_frags; i++) {
497                 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
498                 greth->tx_skbuff[curr_tx] = NULL;
499                 bdp = greth->tx_bd_base + curr_tx;
500
501                 status = GRETH_TXBD_CSALL;
502                 status |= frag->size & GRETH_BD_LEN;
503
504                 /* Wrap around descriptor ring */
505                 if (curr_tx == GRETH_TXBD_NUM_MASK)
506                         status |= GRETH_BD_WR;
507
508                 /* More fragments left */
509                 if (i < nr_frags - 1)
510                         status |= GRETH_TXBD_MORE;
511
512                 /* ... last fragment, check if out of descriptors  */
513                 else if (greth->tx_free - nr_frags - 1 < (MAX_SKB_FRAGS + 1)) {
514
515                         /* Enable interrupts and stop queue */
516                         status |= GRETH_BD_IE;
517                         netif_stop_queue(dev);
518                 }
519
520                 greth_write_bd(&bdp->stat, status);
521
522                 dma_addr = dma_map_page(greth->dev,
523                                         frag->page,
524                                         frag->page_offset,
525                                         frag->size,
526                                         DMA_TO_DEVICE);
527
528                 if (unlikely(dma_mapping_error(greth->dev, dma_addr)))
529                         goto frag_map_error;
530
531                 greth_write_bd(&bdp->addr, dma_addr);
532
533                 curr_tx = NEXT_TX(curr_tx);
534         }
535
536         wmb();
537
538         /* Enable the descriptors that we configured ...  */
539         for (i = 0; i < nr_frags + 1; i++) {
540                 bdp = greth->tx_bd_base + greth->tx_next;
541                 greth_write_bd(&bdp->stat, greth_read_bd(&bdp->stat) | GRETH_BD_EN);
542                 greth->tx_next = NEXT_TX(greth->tx_next);
543                 greth->tx_free--;
544         }
545
546         greth_enable_tx(greth);
547
548         return NETDEV_TX_OK;
549
550 frag_map_error:
551         /* Unmap SKB mappings that succeeded */
552         for (i = 0; greth->tx_next + i != curr_tx; i++) {
553                 bdp = greth->tx_bd_base + greth->tx_next + i;
554                 dma_unmap_single(greth->dev,
555                                  greth_read_bd(&bdp->addr),
556                                  greth_read_bd(&bdp->stat) & GRETH_BD_LEN,
557                                  DMA_TO_DEVICE);
558         }
559 map_error:
560         if (net_ratelimit())
561                 dev_warn(greth->dev, "Could not create TX DMA mapping\n");
562         dev_kfree_skb(skb);
563         return NETDEV_TX_OK;
564
565 out:
566         return err;
567 }
568
569
570 static irqreturn_t greth_interrupt(int irq, void *dev_id)
571 {
572         struct net_device *dev = dev_id;
573         struct greth_private *greth;
574         u32 status;
575         irqreturn_t retval = IRQ_NONE;
576
577         greth = netdev_priv(dev);
578
579         spin_lock(&greth->devlock);
580
581         /* Get the interrupt events that caused us to be here. */
582         status = GRETH_REGLOAD(greth->regs->status);
583
584         /* Handle rx and tx interrupts through poll */
585         if (status & (GRETH_INT_RX | GRETH_INT_TX)) {
586
587                 /* Clear interrupt status */
588                 GRETH_REGORIN(greth->regs->status,
589                               status & (GRETH_INT_RX | GRETH_INT_TX));
590
591                 retval = IRQ_HANDLED;
592
593                 /* Disable interrupts and schedule poll() */
594                 greth_disable_irqs(greth);
595                 napi_schedule(&greth->napi);
596         }
597
598         mmiowb();
599         spin_unlock(&greth->devlock);
600
601         return retval;
602 }
603
604 static void greth_clean_tx(struct net_device *dev)
605 {
606         struct greth_private *greth;
607         struct greth_bd *bdp;
608         u32 stat;
609
610         greth = netdev_priv(dev);
611
612         while (1) {
613                 bdp = greth->tx_bd_base + greth->tx_last;
614                 stat = greth_read_bd(&bdp->stat);
615
616                 if (unlikely(stat & GRETH_BD_EN))
617                         break;
618
619                 if (greth->tx_free == GRETH_TXBD_NUM)
620                         break;
621
622                 /* Check status for errors */
623                 if (unlikely(stat & GRETH_TXBD_STATUS)) {
624                         dev->stats.tx_errors++;
625                         if (stat & GRETH_TXBD_ERR_AL)
626                                 dev->stats.tx_aborted_errors++;
627                         if (stat & GRETH_TXBD_ERR_UE)
628                                 dev->stats.tx_fifo_errors++;
629                 }
630                 dev->stats.tx_packets++;
631                 greth->tx_last = NEXT_TX(greth->tx_last);
632                 greth->tx_free++;
633         }
634
635         if (greth->tx_free > 0) {
636                 netif_wake_queue(dev);
637         }
638
639 }
640
641 static inline void greth_update_tx_stats(struct net_device *dev, u32 stat)
642 {
643         /* Check status for errors */
644         if (unlikely(stat & GRETH_TXBD_STATUS)) {
645                 dev->stats.tx_errors++;
646                 if (stat & GRETH_TXBD_ERR_AL)
647                         dev->stats.tx_aborted_errors++;
648                 if (stat & GRETH_TXBD_ERR_UE)
649                         dev->stats.tx_fifo_errors++;
650                 if (stat & GRETH_TXBD_ERR_LC)
651                         dev->stats.tx_aborted_errors++;
652         }
653         dev->stats.tx_packets++;
654 }
655
656 static void greth_clean_tx_gbit(struct net_device *dev)
657 {
658         struct greth_private *greth;
659         struct greth_bd *bdp, *bdp_last_frag;
660         struct sk_buff *skb;
661         u32 stat;
662         int nr_frags, i;
663
664         greth = netdev_priv(dev);
665
666         while (greth->tx_free < GRETH_TXBD_NUM) {
667
668                 skb = greth->tx_skbuff[greth->tx_last];
669
670                 nr_frags = skb_shinfo(skb)->nr_frags;
671
672                 /* We only clean fully completed SKBs */
673                 bdp_last_frag = greth->tx_bd_base + SKIP_TX(greth->tx_last, nr_frags);
674                 stat = bdp_last_frag->stat;
675
676                 if (stat & GRETH_BD_EN)
677                         break;
678
679                 greth->tx_skbuff[greth->tx_last] = NULL;
680
681                 greth_update_tx_stats(dev, stat);
682
683                 bdp = greth->tx_bd_base + greth->tx_last;
684
685                 greth->tx_last = NEXT_TX(greth->tx_last);
686
687                 dma_unmap_single(greth->dev,
688                                  greth_read_bd(&bdp->addr),
689                                  skb_headlen(skb),
690                                  DMA_TO_DEVICE);
691
692                 for (i = 0; i < nr_frags; i++) {
693                         skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
694                         bdp = greth->tx_bd_base + greth->tx_last;
695
696                         dma_unmap_page(greth->dev,
697                                        greth_read_bd(&bdp->addr),
698                                        frag->size,
699                                        DMA_TO_DEVICE);
700
701                         greth->tx_last = NEXT_TX(greth->tx_last);
702                 }
703                 greth->tx_free += nr_frags+1;
704                 dev_kfree_skb(skb);
705         }
706         if (greth->tx_free > (MAX_SKB_FRAGS + 1)) {
707                 netif_wake_queue(dev);
708         }
709 }
710
711 static int greth_pending_packets(struct greth_private *greth)
712 {
713         struct greth_bd *bdp;
714         u32 status;
715         bdp = greth->rx_bd_base + greth->rx_cur;
716         status = greth_read_bd(&bdp->stat);
717         if (status & GRETH_BD_EN)
718                 return 0;
719         else
720                 return 1;
721 }
722
723 static int greth_rx(struct net_device *dev, int limit)
724 {
725         struct greth_private *greth;
726         struct greth_bd *bdp;
727         struct sk_buff *skb;
728         int pkt_len;
729         int bad, count;
730         u32 status, dma_addr;
731
732         greth = netdev_priv(dev);
733
734         for (count = 0; count < limit; ++count) {
735
736                 bdp = greth->rx_bd_base + greth->rx_cur;
737                 status = greth_read_bd(&bdp->stat);
738                 dma_addr = greth_read_bd(&bdp->addr);
739                 bad = 0;
740
741                 if (unlikely(status & GRETH_BD_EN)) {
742                         break;
743                 }
744
745                 /* Check status for errors. */
746                 if (unlikely(status & GRETH_RXBD_STATUS)) {
747                         if (status & GRETH_RXBD_ERR_FT) {
748                                 dev->stats.rx_length_errors++;
749                                 bad = 1;
750                         }
751                         if (status & (GRETH_RXBD_ERR_AE | GRETH_RXBD_ERR_OE)) {
752                                 dev->stats.rx_frame_errors++;
753                                 bad = 1;
754                         }
755                         if (status & GRETH_RXBD_ERR_CRC) {
756                                 dev->stats.rx_crc_errors++;
757                                 bad = 1;
758                         }
759                 }
760                 if (unlikely(bad)) {
761                         dev->stats.rx_errors++;
762
763                 } else {
764
765                         pkt_len = status & GRETH_BD_LEN;
766
767                         skb = netdev_alloc_skb(dev, pkt_len + NET_IP_ALIGN);
768
769                         if (unlikely(skb == NULL)) {
770
771                                 if (net_ratelimit())
772                                         dev_warn(&dev->dev, "low on memory - " "packet dropped\n");
773
774                                 dev->stats.rx_dropped++;
775
776                         } else {
777                                 skb_reserve(skb, NET_IP_ALIGN);
778                                 skb->dev = dev;
779
780                                 dma_sync_single_for_cpu(greth->dev,
781                                                         dma_addr,
782                                                         pkt_len,
783                                                         DMA_FROM_DEVICE);
784
785                                 if (netif_msg_pktdata(greth))
786                                         greth_print_rx_packet(phys_to_virt(dma_addr), pkt_len);
787
788                                 memcpy(skb_put(skb, pkt_len), phys_to_virt(dma_addr), pkt_len);
789
790                                 skb->protocol = eth_type_trans(skb, dev);
791                                 dev->stats.rx_packets++;
792                                 netif_receive_skb(skb);
793                         }
794                 }
795
796                 status = GRETH_BD_EN | GRETH_BD_IE;
797                 if (greth->rx_cur == GRETH_RXBD_NUM_MASK) {
798                         status |= GRETH_BD_WR;
799                 }
800
801                 wmb();
802                 greth_write_bd(&bdp->stat, status);
803
804                 dma_sync_single_for_device(greth->dev, dma_addr, MAX_FRAME_SIZE, DMA_FROM_DEVICE);
805
806                 greth_enable_rx(greth);
807
808                 greth->rx_cur = NEXT_RX(greth->rx_cur);
809         }
810
811         return count;
812 }
813
814 static inline int hw_checksummed(u32 status)
815 {
816
817         if (status & GRETH_RXBD_IP_FRAG)
818                 return 0;
819
820         if (status & GRETH_RXBD_IP && status & GRETH_RXBD_IP_CSERR)
821                 return 0;
822
823         if (status & GRETH_RXBD_UDP && status & GRETH_RXBD_UDP_CSERR)
824                 return 0;
825
826         if (status & GRETH_RXBD_TCP && status & GRETH_RXBD_TCP_CSERR)
827                 return 0;
828
829         return 1;
830 }
831
832 static int greth_rx_gbit(struct net_device *dev, int limit)
833 {
834         struct greth_private *greth;
835         struct greth_bd *bdp;
836         struct sk_buff *skb, *newskb;
837         int pkt_len;
838         int bad, count = 0;
839         u32 status, dma_addr;
840
841         greth = netdev_priv(dev);
842
843         for (count = 0; count < limit; ++count) {
844
845                 bdp = greth->rx_bd_base + greth->rx_cur;
846                 skb = greth->rx_skbuff[greth->rx_cur];
847                 status = greth_read_bd(&bdp->stat);
848                 bad = 0;
849
850                 if (status & GRETH_BD_EN)
851                         break;
852
853                 /* Check status for errors. */
854                 if (unlikely(status & GRETH_RXBD_STATUS)) {
855
856                         if (status & GRETH_RXBD_ERR_FT) {
857                                 dev->stats.rx_length_errors++;
858                                 bad = 1;
859                         } else if (status &
860                                    (GRETH_RXBD_ERR_AE | GRETH_RXBD_ERR_OE | GRETH_RXBD_ERR_LE)) {
861                                 dev->stats.rx_frame_errors++;
862                                 bad = 1;
863                         } else if (status & GRETH_RXBD_ERR_CRC) {
864                                 dev->stats.rx_crc_errors++;
865                                 bad = 1;
866                         }
867                 }
868
869                 /* Allocate new skb to replace current */
870                 newskb = netdev_alloc_skb(dev, MAX_FRAME_SIZE + NET_IP_ALIGN);
871
872                 if (!bad && newskb) {
873                         skb_reserve(newskb, NET_IP_ALIGN);
874
875                         dma_addr = dma_map_single(greth->dev,
876                                                       newskb->data,
877                                                       MAX_FRAME_SIZE + NET_IP_ALIGN,
878                                                       DMA_FROM_DEVICE);
879
880                         if (!dma_mapping_error(greth->dev, dma_addr)) {
881                                 /* Process the incoming frame. */
882                                 pkt_len = status & GRETH_BD_LEN;
883
884                                 dma_unmap_single(greth->dev,
885                                                  greth_read_bd(&bdp->addr),
886                                                  MAX_FRAME_SIZE + NET_IP_ALIGN,
887                                                  DMA_FROM_DEVICE);
888
889                                 if (netif_msg_pktdata(greth))
890                                         greth_print_rx_packet(phys_to_virt(greth_read_bd(&bdp->addr)), pkt_len);
891
892                                 skb_put(skb, pkt_len);
893
894                                 if (greth->flags & GRETH_FLAG_RX_CSUM && hw_checksummed(status))
895                                         skb->ip_summed = CHECKSUM_UNNECESSARY;
896                                 else
897                                         skb->ip_summed = CHECKSUM_NONE;
898
899                                 skb->dev = dev;
900                                 skb->protocol = eth_type_trans(skb, dev);
901                                 dev->stats.rx_packets++;
902                                 netif_receive_skb(skb);
903
904                                 greth->rx_skbuff[greth->rx_cur] = newskb;
905                                 greth_write_bd(&bdp->addr, dma_addr);
906                         } else {
907                                 if (net_ratelimit())
908                                         dev_warn(greth->dev, "Could not create DMA mapping, dropping packet\n");
909                                 dev_kfree_skb(newskb);
910                                 dev->stats.rx_dropped++;
911                         }
912                 } else {
913                         if (net_ratelimit())
914                                 dev_warn(greth->dev, "Could not allocate SKB, dropping packet\n");
915                         dev->stats.rx_dropped++;
916                 }
917
918                 status = GRETH_BD_EN | GRETH_BD_IE;
919                 if (greth->rx_cur == GRETH_RXBD_NUM_MASK) {
920                         status |= GRETH_BD_WR;
921                 }
922
923                 wmb();
924                 greth_write_bd(&bdp->stat, status);
925                 greth_enable_rx(greth);
926                 greth->rx_cur = NEXT_RX(greth->rx_cur);
927         }
928
929         return count;
930
931 }
932
933 static int greth_poll(struct napi_struct *napi, int budget)
934 {
935         struct greth_private *greth;
936         int work_done = 0;
937         greth = container_of(napi, struct greth_private, napi);
938
939         if (greth->gbit_mac) {
940                 greth_clean_tx_gbit(greth->netdev);
941         } else {
942                 greth_clean_tx(greth->netdev);
943         }
944
945 restart_poll:
946         if (greth->gbit_mac) {
947                 work_done += greth_rx_gbit(greth->netdev, budget - work_done);
948         } else {
949                 work_done += greth_rx(greth->netdev, budget - work_done);
950         }
951
952         if (work_done < budget) {
953
954                 napi_complete(napi);
955
956                 if (greth_pending_packets(greth)) {
957                         napi_reschedule(napi);
958                         goto restart_poll;
959                 }
960         }
961
962         greth_enable_irqs(greth);
963         return work_done;
964 }
965
966 static int greth_set_mac_add(struct net_device *dev, void *p)
967 {
968         struct sockaddr *addr = p;
969         struct greth_private *greth;
970         struct greth_regs *regs;
971
972         greth = netdev_priv(dev);
973         regs = (struct greth_regs *) greth->regs;
974
975         if (!is_valid_ether_addr(addr->sa_data))
976                 return -EINVAL;
977
978         memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
979
980         GRETH_REGSAVE(regs->esa_msb, addr->sa_data[0] << 8 | addr->sa_data[1]);
981         GRETH_REGSAVE(regs->esa_lsb,
982                       addr->sa_data[2] << 24 | addr->
983                       sa_data[3] << 16 | addr->sa_data[4] << 8 | addr->sa_data[5]);
984         return 0;
985 }
986
987 static u32 greth_hash_get_index(__u8 *addr)
988 {
989         return (ether_crc(6, addr)) & 0x3F;
990 }
991
992 static void greth_set_hash_filter(struct net_device *dev)
993 {
994         struct dev_mc_list *curr;
995         struct greth_private *greth = netdev_priv(dev);
996         struct greth_regs *regs = (struct greth_regs *) greth->regs;
997         u32 mc_filter[2];
998         unsigned int bitnr;
999
1000         mc_filter[0] = mc_filter[1] = 0;
1001
1002         netdev_for_each_mc_addr(curr, dev) {
1003                 bitnr = greth_hash_get_index(curr->dmi_addr);
1004                 mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
1005         }
1006
1007         GRETH_REGSAVE(regs->hash_msb, mc_filter[1]);
1008         GRETH_REGSAVE(regs->hash_lsb, mc_filter[0]);
1009 }
1010
1011 static void greth_set_multicast_list(struct net_device *dev)
1012 {
1013         int cfg;
1014         struct greth_private *greth = netdev_priv(dev);
1015         struct greth_regs *regs = (struct greth_regs *) greth->regs;
1016
1017         cfg = GRETH_REGLOAD(regs->control);
1018         if (dev->flags & IFF_PROMISC)
1019                 cfg |= GRETH_CTRL_PR;
1020         else
1021                 cfg &= ~GRETH_CTRL_PR;
1022
1023         if (greth->multicast) {
1024                 if (dev->flags & IFF_ALLMULTI) {
1025                         GRETH_REGSAVE(regs->hash_msb, -1);
1026                         GRETH_REGSAVE(regs->hash_lsb, -1);
1027                         cfg |= GRETH_CTRL_MCEN;
1028                         GRETH_REGSAVE(regs->control, cfg);
1029                         return;
1030                 }
1031
1032                 if (netdev_mc_empty(dev)) {
1033                         cfg &= ~GRETH_CTRL_MCEN;
1034                         GRETH_REGSAVE(regs->control, cfg);
1035                         return;
1036                 }
1037
1038                 /* Setup multicast filter */
1039                 greth_set_hash_filter(dev);
1040                 cfg |= GRETH_CTRL_MCEN;
1041         }
1042         GRETH_REGSAVE(regs->control, cfg);
1043 }
1044
1045 static u32 greth_get_msglevel(struct net_device *dev)
1046 {
1047         struct greth_private *greth = netdev_priv(dev);
1048         return greth->msg_enable;
1049 }
1050
1051 static void greth_set_msglevel(struct net_device *dev, u32 value)
1052 {
1053         struct greth_private *greth = netdev_priv(dev);
1054         greth->msg_enable = value;
1055 }
1056 static int greth_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1057 {
1058         struct greth_private *greth = netdev_priv(dev);
1059         struct phy_device *phy = greth->phy;
1060
1061         if (!phy)
1062                 return -ENODEV;
1063
1064         return phy_ethtool_gset(phy, cmd);
1065 }
1066
1067 static int greth_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1068 {
1069         struct greth_private *greth = netdev_priv(dev);
1070         struct phy_device *phy = greth->phy;
1071
1072         if (!phy)
1073                 return -ENODEV;
1074
1075         return phy_ethtool_sset(phy, cmd);
1076 }
1077
1078 static int greth_get_regs_len(struct net_device *dev)
1079 {
1080         return sizeof(struct greth_regs);
1081 }
1082
1083 static void greth_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
1084 {
1085         struct greth_private *greth = netdev_priv(dev);
1086
1087         strncpy(info->driver, dev_driver_string(greth->dev), 32);
1088         strncpy(info->version, "revision: 1.0", 32);
1089         strncpy(info->bus_info, greth->dev->bus->name, 32);
1090         strncpy(info->fw_version, "N/A", 32);
1091         info->eedump_len = 0;
1092         info->regdump_len = sizeof(struct greth_regs);
1093 }
1094
1095 static void greth_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *p)
1096 {
1097         int i;
1098         struct greth_private *greth = netdev_priv(dev);
1099         u32 __iomem *greth_regs = (u32 __iomem *) greth->regs;
1100         u32 *buff = p;
1101
1102         for (i = 0; i < sizeof(struct greth_regs) / sizeof(u32); i++)
1103                 buff[i] = greth_read_bd(&greth_regs[i]);
1104 }
1105
1106 static u32 greth_get_rx_csum(struct net_device *dev)
1107 {
1108         struct greth_private *greth = netdev_priv(dev);
1109         return (greth->flags & GRETH_FLAG_RX_CSUM) != 0;
1110 }
1111
1112 static int greth_set_rx_csum(struct net_device *dev, u32 data)
1113 {
1114         struct greth_private *greth = netdev_priv(dev);
1115
1116         spin_lock_bh(&greth->devlock);
1117
1118         if (data)
1119                 greth->flags |= GRETH_FLAG_RX_CSUM;
1120         else
1121                 greth->flags &= ~GRETH_FLAG_RX_CSUM;
1122
1123         spin_unlock_bh(&greth->devlock);
1124
1125         return 0;
1126 }
1127
1128 static u32 greth_get_tx_csum(struct net_device *dev)
1129 {
1130         return (dev->features & NETIF_F_IP_CSUM) != 0;
1131 }
1132
1133 static int greth_set_tx_csum(struct net_device *dev, u32 data)
1134 {
1135         netif_tx_lock_bh(dev);
1136         ethtool_op_set_tx_csum(dev, data);
1137         netif_tx_unlock_bh(dev);
1138         return 0;
1139 }
1140
1141 static const struct ethtool_ops greth_ethtool_ops = {
1142         .get_msglevel           = greth_get_msglevel,
1143         .set_msglevel           = greth_set_msglevel,
1144         .get_settings           = greth_get_settings,
1145         .set_settings           = greth_set_settings,
1146         .get_drvinfo            = greth_get_drvinfo,
1147         .get_regs_len           = greth_get_regs_len,
1148         .get_regs               = greth_get_regs,
1149         .get_rx_csum            = greth_get_rx_csum,
1150         .set_rx_csum            = greth_set_rx_csum,
1151         .get_tx_csum            = greth_get_tx_csum,
1152         .set_tx_csum            = greth_set_tx_csum,
1153         .get_link               = ethtool_op_get_link,
1154 };
1155
1156 static struct net_device_ops greth_netdev_ops = {
1157         .ndo_open = greth_open,
1158         .ndo_stop = greth_close,
1159         .ndo_start_xmit = greth_start_xmit,
1160         .ndo_set_mac_address = greth_set_mac_add,
1161         .ndo_validate_addr      = eth_validate_addr,
1162 };
1163
1164 static inline int wait_for_mdio(struct greth_private *greth)
1165 {
1166         unsigned long timeout = jiffies + 4*HZ/100;
1167         while (GRETH_REGLOAD(greth->regs->mdio) & GRETH_MII_BUSY) {
1168                 if (time_after(jiffies, timeout))
1169                         return 0;
1170         }
1171         return 1;
1172 }
1173
1174 static int greth_mdio_read(struct mii_bus *bus, int phy, int reg)
1175 {
1176         struct greth_private *greth = bus->priv;
1177         int data;
1178
1179         if (!wait_for_mdio(greth))
1180                 return -EBUSY;
1181
1182         GRETH_REGSAVE(greth->regs->mdio, ((phy & 0x1F) << 11) | ((reg & 0x1F) << 6) | 2);
1183
1184         if (!wait_for_mdio(greth))
1185                 return -EBUSY;
1186
1187         if (!(GRETH_REGLOAD(greth->regs->mdio) & GRETH_MII_NVALID)) {
1188                 data = (GRETH_REGLOAD(greth->regs->mdio) >> 16) & 0xFFFF;
1189                 return data;
1190
1191         } else {
1192                 return -1;
1193         }
1194 }
1195
1196 static int greth_mdio_write(struct mii_bus *bus, int phy, int reg, u16 val)
1197 {
1198         struct greth_private *greth = bus->priv;
1199
1200         if (!wait_for_mdio(greth))
1201                 return -EBUSY;
1202
1203         GRETH_REGSAVE(greth->regs->mdio,
1204                       ((val & 0xFFFF) << 16) | ((phy & 0x1F) << 11) | ((reg & 0x1F) << 6) | 1);
1205
1206         if (!wait_for_mdio(greth))
1207                 return -EBUSY;
1208
1209         return 0;
1210 }
1211
1212 static int greth_mdio_reset(struct mii_bus *bus)
1213 {
1214         return 0;
1215 }
1216
1217 static void greth_link_change(struct net_device *dev)
1218 {
1219         struct greth_private *greth = netdev_priv(dev);
1220         struct phy_device *phydev = greth->phy;
1221         unsigned long flags;
1222
1223         int status_change = 0;
1224
1225         spin_lock_irqsave(&greth->devlock, flags);
1226
1227         if (phydev->link) {
1228
1229                 if ((greth->speed != phydev->speed) || (greth->duplex != phydev->duplex)) {
1230
1231                         GRETH_REGANDIN(greth->regs->control,
1232                                        ~(GRETH_CTRL_FD | GRETH_CTRL_SP | GRETH_CTRL_GB));
1233
1234                         if (phydev->duplex)
1235                                 GRETH_REGORIN(greth->regs->control, GRETH_CTRL_FD);
1236
1237                         if (phydev->speed == SPEED_100) {
1238
1239                                 GRETH_REGORIN(greth->regs->control, GRETH_CTRL_SP);
1240                         }
1241
1242                         else if (phydev->speed == SPEED_1000)
1243                                 GRETH_REGORIN(greth->regs->control, GRETH_CTRL_GB);
1244
1245                         greth->speed = phydev->speed;
1246                         greth->duplex = phydev->duplex;
1247                         status_change = 1;
1248                 }
1249         }
1250
1251         if (phydev->link != greth->link) {
1252                 if (!phydev->link) {
1253                         greth->speed = 0;
1254                         greth->duplex = -1;
1255                 }
1256                 greth->link = phydev->link;
1257
1258                 status_change = 1;
1259         }
1260
1261         spin_unlock_irqrestore(&greth->devlock, flags);
1262
1263         if (status_change) {
1264                 if (phydev->link)
1265                         pr_debug("%s: link up (%d/%s)\n",
1266                                 dev->name, phydev->speed,
1267                                 DUPLEX_FULL == phydev->duplex ? "Full" : "Half");
1268                 else
1269                         pr_debug("%s: link down\n", dev->name);
1270         }
1271 }
1272
1273 static int greth_mdio_probe(struct net_device *dev)
1274 {
1275         struct greth_private *greth = netdev_priv(dev);
1276         struct phy_device *phy = NULL;
1277         int ret;
1278
1279         /* Find the first PHY */
1280         phy = phy_find_first(greth->mdio);
1281
1282         if (!phy) {
1283                 if (netif_msg_probe(greth))
1284                         dev_err(&dev->dev, "no PHY found\n");
1285                 return -ENXIO;
1286         }
1287
1288         ret = phy_connect_direct(dev, phy, &greth_link_change,
1289                         0, greth->gbit_mac ?
1290                         PHY_INTERFACE_MODE_GMII :
1291                         PHY_INTERFACE_MODE_MII);
1292         if (ret) {
1293                 if (netif_msg_ifup(greth))
1294                         dev_err(&dev->dev, "could not attach to PHY\n");
1295                 return ret;
1296         }
1297
1298         if (greth->gbit_mac)
1299                 phy->supported &= PHY_GBIT_FEATURES;
1300         else
1301                 phy->supported &= PHY_BASIC_FEATURES;
1302
1303         phy->advertising = phy->supported;
1304
1305         greth->link = 0;
1306         greth->speed = 0;
1307         greth->duplex = -1;
1308         greth->phy = phy;
1309
1310         return 0;
1311 }
1312
1313 static inline int phy_aneg_done(struct phy_device *phydev)
1314 {
1315         int retval;
1316
1317         retval = phy_read(phydev, MII_BMSR);
1318
1319         return (retval < 0) ? retval : (retval & BMSR_ANEGCOMPLETE);
1320 }
1321
1322 static int greth_mdio_init(struct greth_private *greth)
1323 {
1324         int ret, phy;
1325         unsigned long timeout;
1326
1327         greth->mdio = mdiobus_alloc();
1328         if (!greth->mdio) {
1329                 return -ENOMEM;
1330         }
1331
1332         greth->mdio->name = "greth-mdio";
1333         snprintf(greth->mdio->id, MII_BUS_ID_SIZE, "%s-%d", greth->mdio->name, greth->irq);
1334         greth->mdio->read = greth_mdio_read;
1335         greth->mdio->write = greth_mdio_write;
1336         greth->mdio->reset = greth_mdio_reset;
1337         greth->mdio->priv = greth;
1338
1339         greth->mdio->irq = greth->mdio_irqs;
1340
1341         for (phy = 0; phy < PHY_MAX_ADDR; phy++)
1342                 greth->mdio->irq[phy] = PHY_POLL;
1343
1344         ret = mdiobus_register(greth->mdio);
1345         if (ret) {
1346                 goto error;
1347         }
1348
1349         ret = greth_mdio_probe(greth->netdev);
1350         if (ret) {
1351                 if (netif_msg_probe(greth))
1352                         dev_err(&greth->netdev->dev, "failed to probe MDIO bus\n");
1353                 goto unreg_mdio;
1354         }
1355
1356         phy_start(greth->phy);
1357
1358         /* If Ethernet debug link is used make autoneg happen right away */
1359         if (greth->edcl && greth_edcl == 1) {
1360                 phy_start_aneg(greth->phy);
1361                 timeout = jiffies + 6*HZ;
1362                 while (!phy_aneg_done(greth->phy) && time_before(jiffies, timeout)) {
1363                 }
1364                 genphy_read_status(greth->phy);
1365                 greth_link_change(greth->netdev);
1366         }
1367
1368         return 0;
1369
1370 unreg_mdio:
1371         mdiobus_unregister(greth->mdio);
1372 error:
1373         mdiobus_free(greth->mdio);
1374         return ret;
1375 }
1376
1377 /* Initialize the GRETH MAC */
1378 static int __devinit greth_of_probe(struct of_device *ofdev, const struct of_device_id *match)
1379 {
1380         struct net_device *dev;
1381         struct greth_private *greth;
1382         struct greth_regs *regs;
1383
1384         int i;
1385         int err;
1386         int tmp;
1387         unsigned long timeout;
1388
1389         dev = alloc_etherdev(sizeof(struct greth_private));
1390
1391         if (dev == NULL)
1392                 return -ENOMEM;
1393
1394         greth = netdev_priv(dev);
1395         greth->netdev = dev;
1396         greth->dev = &ofdev->dev;
1397
1398         if (greth_debug > 0)
1399                 greth->msg_enable = greth_debug;
1400         else
1401                 greth->msg_enable = GRETH_DEF_MSG_ENABLE;
1402
1403         spin_lock_init(&greth->devlock);
1404
1405         greth->regs = of_ioremap(&ofdev->resource[0], 0,
1406                                  resource_size(&ofdev->resource[0]),
1407                                  "grlib-greth regs");
1408
1409         if (greth->regs == NULL) {
1410                 if (netif_msg_probe(greth))
1411                         dev_err(greth->dev, "ioremap failure.\n");
1412                 err = -EIO;
1413                 goto error1;
1414         }
1415
1416         regs = (struct greth_regs *) greth->regs;
1417         greth->irq = ofdev->irqs[0];
1418
1419         dev_set_drvdata(greth->dev, dev);
1420         SET_NETDEV_DEV(dev, greth->dev);
1421
1422         if (netif_msg_probe(greth))
1423                 dev_dbg(greth->dev, "reseting controller.\n");
1424
1425         /* Reset the controller. */
1426         GRETH_REGSAVE(regs->control, GRETH_RESET);
1427
1428         /* Wait for MAC to reset itself */
1429         timeout = jiffies + HZ/100;
1430         while (GRETH_REGLOAD(regs->control) & GRETH_RESET) {
1431                 if (time_after(jiffies, timeout)) {
1432                         err = -EIO;
1433                         if (netif_msg_probe(greth))
1434                                 dev_err(greth->dev, "timeout when waiting for reset.\n");
1435                         goto error2;
1436                 }
1437         }
1438
1439         /* Get default PHY address  */
1440         greth->phyaddr = (GRETH_REGLOAD(regs->mdio) >> 11) & 0x1F;
1441
1442         /* Check if we have GBIT capable MAC */
1443         tmp = GRETH_REGLOAD(regs->control);
1444         greth->gbit_mac = (tmp >> 27) & 1;
1445
1446         /* Check for multicast capability */
1447         greth->multicast = (tmp >> 25) & 1;
1448
1449         greth->edcl = (tmp >> 31) & 1;
1450
1451         /* If we have EDCL we disable the EDCL speed-duplex FSM so
1452          * it doesn't interfere with the software */
1453         if (greth->edcl != 0)
1454                 GRETH_REGORIN(regs->control, GRETH_CTRL_DISDUPLEX);
1455
1456         /* Check if MAC can handle MDIO interrupts */
1457         greth->mdio_int_en = (tmp >> 26) & 1;
1458
1459         err = greth_mdio_init(greth);
1460         if (err) {
1461                 if (netif_msg_probe(greth))
1462                         dev_err(greth->dev, "failed to register MDIO bus\n");
1463                 goto error2;
1464         }
1465
1466         /* Allocate TX descriptor ring in coherent memory */
1467         greth->tx_bd_base = (struct greth_bd *) dma_alloc_coherent(greth->dev,
1468                                                                    1024,
1469                                                                    &greth->tx_bd_base_phys,
1470                                                                    GFP_KERNEL);
1471
1472         if (!greth->tx_bd_base) {
1473                 if (netif_msg_probe(greth))
1474                         dev_err(&dev->dev, "could not allocate descriptor memory.\n");
1475                 err = -ENOMEM;
1476                 goto error3;
1477         }
1478
1479         memset(greth->tx_bd_base, 0, 1024);
1480
1481         /* Allocate RX descriptor ring in coherent memory */
1482         greth->rx_bd_base = (struct greth_bd *) dma_alloc_coherent(greth->dev,
1483                                                                    1024,
1484                                                                    &greth->rx_bd_base_phys,
1485                                                                    GFP_KERNEL);
1486
1487         if (!greth->rx_bd_base) {
1488                 if (netif_msg_probe(greth))
1489                         dev_err(greth->dev, "could not allocate descriptor memory.\n");
1490                 err = -ENOMEM;
1491                 goto error4;
1492         }
1493
1494         memset(greth->rx_bd_base, 0, 1024);
1495
1496         /* Get MAC address from: module param, OF property or ID prom */
1497         for (i = 0; i < 6; i++) {
1498                 if (macaddr[i] != 0)
1499                         break;
1500         }
1501         if (i == 6) {
1502                 const unsigned char *addr;
1503                 int len;
1504                 addr = of_get_property(ofdev->node, "local-mac-address", &len);
1505                 if (addr != NULL && len == 6) {
1506                         for (i = 0; i < 6; i++)
1507                                 macaddr[i] = (unsigned int) addr[i];
1508                 } else {
1509 #ifdef CONFIG_SPARC
1510                         for (i = 0; i < 6; i++)
1511                                 macaddr[i] = (unsigned int) idprom->id_ethaddr[i];
1512 #endif
1513                 }
1514         }
1515
1516         for (i = 0; i < 6; i++)
1517                 dev->dev_addr[i] = macaddr[i];
1518
1519         macaddr[5]++;
1520
1521         if (!is_valid_ether_addr(&dev->dev_addr[0])) {
1522                 if (netif_msg_probe(greth))
1523                         dev_err(greth->dev, "no valid ethernet address, aborting.\n");
1524                 err = -EINVAL;
1525                 goto error5;
1526         }
1527
1528         GRETH_REGSAVE(regs->esa_msb, dev->dev_addr[0] << 8 | dev->dev_addr[1]);
1529         GRETH_REGSAVE(regs->esa_lsb, dev->dev_addr[2] << 24 | dev->dev_addr[3] << 16 |
1530                       dev->dev_addr[4] << 8 | dev->dev_addr[5]);
1531
1532         /* Clear all pending interrupts except PHY irq */
1533         GRETH_REGSAVE(regs->status, 0xFF);
1534
1535         if (greth->gbit_mac) {
1536                 dev->features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_HIGHDMA;
1537                 greth_netdev_ops.ndo_start_xmit = greth_start_xmit_gbit;
1538                 greth->flags = GRETH_FLAG_RX_CSUM;
1539         }
1540
1541         if (greth->multicast) {
1542                 greth_netdev_ops.ndo_set_multicast_list = greth_set_multicast_list;
1543                 dev->flags |= IFF_MULTICAST;
1544         } else {
1545                 dev->flags &= ~IFF_MULTICAST;
1546         }
1547
1548         dev->netdev_ops = &greth_netdev_ops;
1549         dev->ethtool_ops = &greth_ethtool_ops;
1550
1551         if (register_netdev(dev)) {
1552                 if (netif_msg_probe(greth))
1553                         dev_err(greth->dev, "netdevice registration failed.\n");
1554                 err = -ENOMEM;
1555                 goto error5;
1556         }
1557
1558         /* setup NAPI */
1559         memset(&greth->napi, 0, sizeof(greth->napi));
1560         netif_napi_add(dev, &greth->napi, greth_poll, 64);
1561
1562         return 0;
1563
1564 error5:
1565         dma_free_coherent(greth->dev, 1024, greth->rx_bd_base, greth->rx_bd_base_phys);
1566 error4:
1567         dma_free_coherent(greth->dev, 1024, greth->tx_bd_base, greth->tx_bd_base_phys);
1568 error3:
1569         mdiobus_unregister(greth->mdio);
1570 error2:
1571         of_iounmap(&ofdev->resource[0], greth->regs, resource_size(&ofdev->resource[0]));
1572 error1:
1573         free_netdev(dev);
1574         return err;
1575 }
1576
1577 static int __devexit greth_of_remove(struct of_device *of_dev)
1578 {
1579         struct net_device *ndev = dev_get_drvdata(&of_dev->dev);
1580         struct greth_private *greth = netdev_priv(ndev);
1581
1582         /* Free descriptor areas */
1583         dma_free_coherent(&of_dev->dev, 1024, greth->rx_bd_base, greth->rx_bd_base_phys);
1584
1585         dma_free_coherent(&of_dev->dev, 1024, greth->tx_bd_base, greth->tx_bd_base_phys);
1586
1587         dev_set_drvdata(&of_dev->dev, NULL);
1588
1589         if (greth->phy)
1590                 phy_stop(greth->phy);
1591         mdiobus_unregister(greth->mdio);
1592
1593         unregister_netdev(ndev);
1594         free_netdev(ndev);
1595
1596         of_iounmap(&of_dev->resource[0], greth->regs, resource_size(&of_dev->resource[0]));
1597
1598         return 0;
1599 }
1600
1601 static struct of_device_id greth_of_match[] = {
1602         {
1603          .name = "GAISLER_ETHMAC",
1604          },
1605         {},
1606 };
1607
1608 MODULE_DEVICE_TABLE(of, greth_of_match);
1609
1610 static struct of_platform_driver greth_of_driver = {
1611         .name = "grlib-greth",
1612         .match_table = greth_of_match,
1613         .probe = greth_of_probe,
1614         .remove = __devexit_p(greth_of_remove),
1615         .driver = {
1616                    .owner = THIS_MODULE,
1617                    .name = "grlib-greth",
1618                    },
1619 };
1620
1621 static int __init greth_init(void)
1622 {
1623         return of_register_platform_driver(&greth_of_driver);
1624 }
1625
1626 static void __exit greth_cleanup(void)
1627 {
1628         of_unregister_platform_driver(&greth_of_driver);
1629 }
1630
1631 module_init(greth_init);
1632 module_exit(greth_cleanup);
1633
1634 MODULE_AUTHOR("Aeroflex Gaisler AB.");
1635 MODULE_DESCRIPTION("Aeroflex Gaisler Ethernet MAC driver");
1636 MODULE_LICENSE("GPL");