Version:  2.0.40 2.2.26 2.4.37 2.6.39 3.0 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 3.10 3.11 3.12 3.13 3.14 3.15

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

This page was automatically generated by LXR 0.3.1 (source).  •  Linux is a registered trademark of Linus Torvalds  •  Contact us