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