Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /* $Id: sunlance.c,v 1.112 2002/01/15 06:48:55 davem Exp $
0003  * lance.c: Linux/Sparc/Lance driver
0004  *
0005  *  Written 1995, 1996 by Miguel de Icaza
0006  * Sources:
0007  *  The Linux  depca driver
0008  *  The Linux  lance driver.
0009  *  The Linux  skeleton driver.
0010  *  The NetBSD Sparc/Lance driver.
0011  *  Theo de Raadt (deraadt@openbsd.org)
0012  *  NCR92C990 Lan Controller manual
0013  *
0014  * 1.4:
0015  *  Added support to run with a ledma on the Sun4m
0016  *
0017  * 1.5:
0018  *  Added multiple card detection.
0019  *
0020  *   4/17/96: Burst sizes and tpe selection on sun4m by Eddie C. Dost
0021  *        (ecd@skynet.be)
0022  *
0023  *   5/15/96: auto carrier detection on sun4m by Eddie C. Dost
0024  *        (ecd@skynet.be)
0025  *
0026  *   5/17/96: lebuffer on scsi/ether cards now work David S. Miller
0027  *        (davem@caip.rutgers.edu)
0028  *
0029  *   5/29/96: override option 'tpe-link-test?', if it is 'false', as
0030  *        this disables auto carrier detection on sun4m. Eddie C. Dost
0031  *        (ecd@skynet.be)
0032  *
0033  * 1.7:
0034  *   6/26/96: Bug fix for multiple ledmas, miguel.
0035  *
0036  * 1.8:
0037  *        Stole multicast code from depca.c, fixed lance_tx.
0038  *
0039  * 1.9:
0040  *   8/21/96: Fixed the multicast code (Pedro Roque)
0041  *
0042  *   8/28/96: Send fake packet in lance_open() if auto_select is true,
0043  *        so we can detect the carrier loss condition in time.
0044  *        Eddie C. Dost (ecd@skynet.be)
0045  *
0046  *   9/15/96: Align rx_buf so that eth_copy_and_sum() won't cause an
0047  *        MNA trap during chksum_partial_copy(). (ecd@skynet.be)
0048  *
0049  *  11/17/96: Handle LE_C0_MERR in lance_interrupt(). (ecd@skynet.be)
0050  *
0051  *  12/22/96: Don't loop forever in lance_rx() on incomplete packets.
0052  *        This was the sun4c killer. Shit, stupid bug.
0053  *        (ecd@skynet.be)
0054  *
0055  * 1.10:
0056  *   1/26/97: Modularize driver. (ecd@skynet.be)
0057  *
0058  * 1.11:
0059  *  12/27/97: Added sun4d support. (jj@sunsite.mff.cuni.cz)
0060  *
0061  * 1.12:
0062  *   11/3/99: Fixed SMP race in lance_start_xmit found by davem.
0063  *            Anton Blanchard (anton@progsoc.uts.edu.au)
0064  * 2.00: 11/9/99: Massive overhaul and port to new SBUS driver interfaces.
0065  *        David S. Miller (davem@redhat.com)
0066  * 2.01:
0067  *      11/08/01: Use library crc32 functions (Matt_Domsch@dell.com)
0068  *
0069  */
0070 
0071 #undef DEBUG_DRIVER
0072 
0073 static char lancestr[] = "LANCE";
0074 
0075 #include <linux/module.h>
0076 #include <linux/kernel.h>
0077 #include <linux/types.h>
0078 #include <linux/fcntl.h>
0079 #include <linux/interrupt.h>
0080 #include <linux/ioport.h>
0081 #include <linux/in.h>
0082 #include <linux/string.h>
0083 #include <linux/delay.h>
0084 #include <linux/crc32.h>
0085 #include <linux/errno.h>
0086 #include <linux/socket.h> /* Used for the temporal inet entries and routing */
0087 #include <linux/route.h>
0088 #include <linux/netdevice.h>
0089 #include <linux/etherdevice.h>
0090 #include <linux/skbuff.h>
0091 #include <linux/ethtool.h>
0092 #include <linux/bitops.h>
0093 #include <linux/dma-mapping.h>
0094 #include <linux/of.h>
0095 #include <linux/of_device.h>
0096 #include <linux/gfp.h>
0097 #include <linux/pgtable.h>
0098 
0099 #include <asm/io.h>
0100 #include <asm/dma.h>
0101 #include <asm/byteorder.h>  /* Used by the checksum routines */
0102 #include <asm/idprom.h>
0103 #include <asm/prom.h>
0104 #include <asm/auxio.h>      /* For tpe-link-test? setting */
0105 #include <asm/irq.h>
0106 
0107 #define DRV_NAME    "sunlance"
0108 #define DRV_RELDATE "8/24/03"
0109 #define DRV_AUTHOR  "Miguel de Icaza (miguel@nuclecu.unam.mx)"
0110 
0111 MODULE_AUTHOR(DRV_AUTHOR);
0112 MODULE_DESCRIPTION("Sun Lance ethernet driver");
0113 MODULE_LICENSE("GPL");
0114 
0115 /* Define: 2^4 Tx buffers and 2^4 Rx buffers */
0116 #ifndef LANCE_LOG_TX_BUFFERS
0117 #define LANCE_LOG_TX_BUFFERS 4
0118 #define LANCE_LOG_RX_BUFFERS 4
0119 #endif
0120 
0121 #define LE_CSR0 0
0122 #define LE_CSR1 1
0123 #define LE_CSR2 2
0124 #define LE_CSR3 3
0125 
0126 #define LE_MO_PROM      0x8000  /* Enable promiscuous mode */
0127 
0128 #define LE_C0_ERR   0x8000  /* Error: set if BAB, SQE, MISS or ME is set */
0129 #define LE_C0_BABL  0x4000  /* BAB:  Babble: tx timeout. */
0130 #define LE_C0_CERR  0x2000  /* SQE:  Signal quality error */
0131 #define LE_C0_MISS  0x1000  /* MISS: Missed a packet */
0132 #define LE_C0_MERR  0x0800  /* ME:   Memory error */
0133 #define LE_C0_RINT  0x0400  /* Received interrupt */
0134 #define LE_C0_TINT  0x0200  /* Transmitter Interrupt */
0135 #define LE_C0_IDON  0x0100  /* IFIN: Init finished. */
0136 #define LE_C0_INTR  0x0080  /* Interrupt or error */
0137 #define LE_C0_INEA  0x0040  /* Interrupt enable */
0138 #define LE_C0_RXON  0x0020  /* Receiver on */
0139 #define LE_C0_TXON  0x0010  /* Transmitter on */
0140 #define LE_C0_TDMD  0x0008  /* Transmitter demand */
0141 #define LE_C0_STOP  0x0004  /* Stop the card */
0142 #define LE_C0_STRT  0x0002  /* Start the card */
0143 #define LE_C0_INIT  0x0001  /* Init the card */
0144 
0145 #define LE_C3_BSWP  0x4     /* SWAP */
0146 #define LE_C3_ACON  0x2 /* ALE Control */
0147 #define LE_C3_BCON  0x1 /* Byte control */
0148 
0149 /* Receive message descriptor 1 */
0150 #define LE_R1_OWN       0x80    /* Who owns the entry */
0151 #define LE_R1_ERR       0x40    /* Error: if FRA, OFL, CRC or BUF is set */
0152 #define LE_R1_FRA       0x20    /* FRA: Frame error */
0153 #define LE_R1_OFL       0x10    /* OFL: Frame overflow */
0154 #define LE_R1_CRC       0x08    /* CRC error */
0155 #define LE_R1_BUF       0x04    /* BUF: Buffer error */
0156 #define LE_R1_SOP       0x02    /* Start of packet */
0157 #define LE_R1_EOP       0x01    /* End of packet */
0158 #define LE_R1_POK       0x03    /* Packet is complete: SOP + EOP */
0159 
0160 #define LE_T1_OWN       0x80    /* Lance owns the packet */
0161 #define LE_T1_ERR       0x40    /* Error summary */
0162 #define LE_T1_EMORE     0x10    /* Error: more than one retry needed */
0163 #define LE_T1_EONE      0x08    /* Error: one retry needed */
0164 #define LE_T1_EDEF      0x04    /* Error: deferred */
0165 #define LE_T1_SOP       0x02    /* Start of packet */
0166 #define LE_T1_EOP       0x01    /* End of packet */
0167 #define LE_T1_POK   0x03    /* Packet is complete: SOP + EOP */
0168 
0169 #define LE_T3_BUF       0x8000  /* Buffer error */
0170 #define LE_T3_UFL       0x4000  /* Error underflow */
0171 #define LE_T3_LCOL      0x1000  /* Error late collision */
0172 #define LE_T3_CLOS      0x0800  /* Error carrier loss */
0173 #define LE_T3_RTY       0x0400  /* Error retry */
0174 #define LE_T3_TDR       0x03ff  /* Time Domain Reflectometry counter */
0175 
0176 #define TX_RING_SIZE            (1 << (LANCE_LOG_TX_BUFFERS))
0177 #define TX_RING_MOD_MASK        (TX_RING_SIZE - 1)
0178 #define TX_RING_LEN_BITS        ((LANCE_LOG_TX_BUFFERS) << 29)
0179 #define TX_NEXT(__x)            (((__x)+1) & TX_RING_MOD_MASK)
0180 
0181 #define RX_RING_SIZE            (1 << (LANCE_LOG_RX_BUFFERS))
0182 #define RX_RING_MOD_MASK        (RX_RING_SIZE - 1)
0183 #define RX_RING_LEN_BITS        ((LANCE_LOG_RX_BUFFERS) << 29)
0184 #define RX_NEXT(__x)            (((__x)+1) & RX_RING_MOD_MASK)
0185 
0186 #define PKT_BUF_SZ      1544
0187 #define RX_BUFF_SIZE            PKT_BUF_SZ
0188 #define TX_BUFF_SIZE            PKT_BUF_SZ
0189 
0190 struct lance_rx_desc {
0191     u16 rmd0;       /* low address of packet */
0192     u8  rmd1_bits;  /* descriptor bits */
0193     u8  rmd1_hadr;  /* high address of packet */
0194     s16 length;     /* This length is 2s complement (negative)!
0195                  * Buffer length
0196                  */
0197     u16 mblength;   /* This is the actual number of bytes received */
0198 };
0199 
0200 struct lance_tx_desc {
0201     u16 tmd0;       /* low address of packet */
0202     u8  tmd1_bits;  /* descriptor bits */
0203     u8  tmd1_hadr;  /* high address of packet */
0204     s16     length;     /* Length is 2s complement (negative)! */
0205     u16     misc;
0206 };
0207 
0208 /* The LANCE initialization block, described in databook. */
0209 /* On the Sparc, this block should be on a DMA region     */
0210 struct lance_init_block {
0211     u16 mode;       /* Pre-set mode (reg. 15) */
0212     u8  phys_addr[6];   /* Physical ethernet address */
0213     u32 filter[2];  /* Multicast filter. */
0214 
0215     /* Receive and transmit ring base, along with extra bits. */
0216     u16 rx_ptr;     /* receive descriptor addr */
0217     u16 rx_len;     /* receive len and high addr */
0218     u16 tx_ptr;     /* transmit descriptor addr */
0219     u16 tx_len;     /* transmit len and high addr */
0220 
0221     /* The Tx and Rx ring entries must aligned on 8-byte boundaries. */
0222     struct lance_rx_desc brx_ring[RX_RING_SIZE];
0223     struct lance_tx_desc btx_ring[TX_RING_SIZE];
0224 
0225     u8  tx_buf [TX_RING_SIZE][TX_BUFF_SIZE];
0226     u8  pad[2];     /* align rx_buf for copy_and_sum(). */
0227     u8  rx_buf [RX_RING_SIZE][RX_BUFF_SIZE];
0228 };
0229 
0230 #define libdesc_offset(rt, elem) \
0231 ((__u32)(((unsigned long)(&(((struct lance_init_block *)0)->rt[elem])))))
0232 
0233 #define libbuff_offset(rt, elem) \
0234 ((__u32)(((unsigned long)(&(((struct lance_init_block *)0)->rt[elem][0])))))
0235 
0236 struct lance_private {
0237     void __iomem    *lregs;     /* Lance RAP/RDP regs.      */
0238     void __iomem    *dregs;     /* DMA controller regs.     */
0239     struct lance_init_block __iomem *init_block_iomem;
0240     struct lance_init_block *init_block_mem;
0241 
0242     spinlock_t  lock;
0243 
0244     int     rx_new, tx_new;
0245     int     rx_old, tx_old;
0246 
0247     struct platform_device *ledma;  /* If set this points to ledma  */
0248     char        tpe;        /* cable-selection is TPE   */
0249     char        auto_select;    /* cable-selection by carrier   */
0250     char        burst_sizes;    /* ledma SBus burst sizes   */
0251     char        pio_buffer; /* init block in PIO space? */
0252 
0253     unsigned short  busmaster_regval;
0254 
0255     void (*init_ring)(struct net_device *);
0256     void (*rx)(struct net_device *);
0257     void (*tx)(struct net_device *);
0258 
0259     char                   *name;
0260     dma_addr_t      init_block_dvma;
0261     struct net_device      *dev;          /* Backpointer    */
0262     struct platform_device       *op;
0263     struct platform_device       *lebuffer;
0264     struct timer_list       multicast_timer;
0265 };
0266 
0267 #define TX_BUFFS_AVAIL ((lp->tx_old<=lp->tx_new)?\
0268             lp->tx_old+TX_RING_MOD_MASK-lp->tx_new:\
0269             lp->tx_old - lp->tx_new-1)
0270 
0271 /* Lance registers. */
0272 #define RDP     0x00UL      /* register data port       */
0273 #define RAP     0x02UL      /* register address port    */
0274 #define LANCE_REG_SIZE  0x04UL
0275 
0276 #define STOP_LANCE(__lp) \
0277 do {    void __iomem *__base = (__lp)->lregs; \
0278     sbus_writew(LE_CSR0,    __base + RAP); \
0279     sbus_writew(LE_C0_STOP, __base + RDP); \
0280 } while (0)
0281 
0282 int sparc_lance_debug = 2;
0283 
0284 /* The Lance uses 24 bit addresses */
0285 /* On the Sun4c the DVMA will provide the remaining bytes for us */
0286 /* On the Sun4m we have to instruct the ledma to provide them    */
0287 /* Even worse, on scsi/ether SBUS cards, the init block and the
0288  * transmit/receive buffers are addresses as offsets from absolute
0289  * zero on the lebuffer PIO area. -DaveM
0290  */
0291 
0292 #define LANCE_ADDR(x) ((long)(x) & ~0xff000000)
0293 
0294 /* Load the CSR registers */
0295 static void load_csrs(struct lance_private *lp)
0296 {
0297     u32 leptr;
0298 
0299     if (lp->pio_buffer)
0300         leptr = 0;
0301     else
0302         leptr = LANCE_ADDR(lp->init_block_dvma);
0303 
0304     sbus_writew(LE_CSR1,          lp->lregs + RAP);
0305     sbus_writew(leptr & 0xffff,   lp->lregs + RDP);
0306     sbus_writew(LE_CSR2,          lp->lregs + RAP);
0307     sbus_writew(leptr >> 16,      lp->lregs + RDP);
0308     sbus_writew(LE_CSR3,          lp->lregs + RAP);
0309     sbus_writew(lp->busmaster_regval, lp->lregs + RDP);
0310 
0311     /* Point back to csr0 */
0312     sbus_writew(LE_CSR0, lp->lregs + RAP);
0313 }
0314 
0315 /* Setup the Lance Rx and Tx rings */
0316 static void lance_init_ring_dvma(struct net_device *dev)
0317 {
0318     struct lance_private *lp = netdev_priv(dev);
0319     struct lance_init_block *ib = lp->init_block_mem;
0320     dma_addr_t aib = lp->init_block_dvma;
0321     __u32 leptr;
0322     int i;
0323 
0324     /* Lock out other processes while setting up hardware */
0325     netif_stop_queue(dev);
0326     lp->rx_new = lp->tx_new = 0;
0327     lp->rx_old = lp->tx_old = 0;
0328 
0329     /* Copy the ethernet address to the lance init block
0330      * Note that on the sparc you need to swap the ethernet address.
0331      */
0332     ib->phys_addr [0] = dev->dev_addr [1];
0333     ib->phys_addr [1] = dev->dev_addr [0];
0334     ib->phys_addr [2] = dev->dev_addr [3];
0335     ib->phys_addr [3] = dev->dev_addr [2];
0336     ib->phys_addr [4] = dev->dev_addr [5];
0337     ib->phys_addr [5] = dev->dev_addr [4];
0338 
0339     /* Setup the Tx ring entries */
0340     for (i = 0; i < TX_RING_SIZE; i++) {
0341         leptr = LANCE_ADDR(aib + libbuff_offset(tx_buf, i));
0342         ib->btx_ring [i].tmd0      = leptr;
0343         ib->btx_ring [i].tmd1_hadr = leptr >> 16;
0344         ib->btx_ring [i].tmd1_bits = 0;
0345         ib->btx_ring [i].length    = 0xf000; /* The ones required by tmd2 */
0346         ib->btx_ring [i].misc      = 0;
0347     }
0348 
0349     /* Setup the Rx ring entries */
0350     for (i = 0; i < RX_RING_SIZE; i++) {
0351         leptr = LANCE_ADDR(aib + libbuff_offset(rx_buf, i));
0352 
0353         ib->brx_ring [i].rmd0      = leptr;
0354         ib->brx_ring [i].rmd1_hadr = leptr >> 16;
0355         ib->brx_ring [i].rmd1_bits = LE_R1_OWN;
0356         ib->brx_ring [i].length    = -RX_BUFF_SIZE | 0xf000;
0357         ib->brx_ring [i].mblength  = 0;
0358     }
0359 
0360     /* Setup the initialization block */
0361 
0362     /* Setup rx descriptor pointer */
0363     leptr = LANCE_ADDR(aib + libdesc_offset(brx_ring, 0));
0364     ib->rx_len = (LANCE_LOG_RX_BUFFERS << 13) | (leptr >> 16);
0365     ib->rx_ptr = leptr;
0366 
0367     /* Setup tx descriptor pointer */
0368     leptr = LANCE_ADDR(aib + libdesc_offset(btx_ring, 0));
0369     ib->tx_len = (LANCE_LOG_TX_BUFFERS << 13) | (leptr >> 16);
0370     ib->tx_ptr = leptr;
0371 }
0372 
0373 static void lance_init_ring_pio(struct net_device *dev)
0374 {
0375     struct lance_private *lp = netdev_priv(dev);
0376     struct lance_init_block __iomem *ib = lp->init_block_iomem;
0377     u32 leptr;
0378     int i;
0379 
0380     /* Lock out other processes while setting up hardware */
0381     netif_stop_queue(dev);
0382     lp->rx_new = lp->tx_new = 0;
0383     lp->rx_old = lp->tx_old = 0;
0384 
0385     /* Copy the ethernet address to the lance init block
0386      * Note that on the sparc you need to swap the ethernet address.
0387      */
0388     sbus_writeb(dev->dev_addr[1], &ib->phys_addr[0]);
0389     sbus_writeb(dev->dev_addr[0], &ib->phys_addr[1]);
0390     sbus_writeb(dev->dev_addr[3], &ib->phys_addr[2]);
0391     sbus_writeb(dev->dev_addr[2], &ib->phys_addr[3]);
0392     sbus_writeb(dev->dev_addr[5], &ib->phys_addr[4]);
0393     sbus_writeb(dev->dev_addr[4], &ib->phys_addr[5]);
0394 
0395     /* Setup the Tx ring entries */
0396     for (i = 0; i < TX_RING_SIZE; i++) {
0397         leptr = libbuff_offset(tx_buf, i);
0398         sbus_writew(leptr,  &ib->btx_ring [i].tmd0);
0399         sbus_writeb(leptr >> 16,&ib->btx_ring [i].tmd1_hadr);
0400         sbus_writeb(0,      &ib->btx_ring [i].tmd1_bits);
0401 
0402         /* The ones required by tmd2 */
0403         sbus_writew(0xf000, &ib->btx_ring [i].length);
0404         sbus_writew(0,      &ib->btx_ring [i].misc);
0405     }
0406 
0407     /* Setup the Rx ring entries */
0408     for (i = 0; i < RX_RING_SIZE; i++) {
0409         leptr = libbuff_offset(rx_buf, i);
0410 
0411         sbus_writew(leptr,  &ib->brx_ring [i].rmd0);
0412         sbus_writeb(leptr >> 16,&ib->brx_ring [i].rmd1_hadr);
0413         sbus_writeb(LE_R1_OWN,  &ib->brx_ring [i].rmd1_bits);
0414         sbus_writew(-RX_BUFF_SIZE|0xf000,
0415                 &ib->brx_ring [i].length);
0416         sbus_writew(0,      &ib->brx_ring [i].mblength);
0417     }
0418 
0419     /* Setup the initialization block */
0420 
0421     /* Setup rx descriptor pointer */
0422     leptr = libdesc_offset(brx_ring, 0);
0423     sbus_writew((LANCE_LOG_RX_BUFFERS << 13) | (leptr >> 16),
0424             &ib->rx_len);
0425     sbus_writew(leptr, &ib->rx_ptr);
0426 
0427     /* Setup tx descriptor pointer */
0428     leptr = libdesc_offset(btx_ring, 0);
0429     sbus_writew((LANCE_LOG_TX_BUFFERS << 13) | (leptr >> 16),
0430             &ib->tx_len);
0431     sbus_writew(leptr, &ib->tx_ptr);
0432 }
0433 
0434 static void init_restart_ledma(struct lance_private *lp)
0435 {
0436     u32 csr = sbus_readl(lp->dregs + DMA_CSR);
0437 
0438     if (!(csr & DMA_HNDL_ERROR)) {
0439         /* E-Cache draining */
0440         while (sbus_readl(lp->dregs + DMA_CSR) & DMA_FIFO_ISDRAIN)
0441             barrier();
0442     }
0443 
0444     csr = sbus_readl(lp->dregs + DMA_CSR);
0445     csr &= ~DMA_E_BURSTS;
0446     if (lp->burst_sizes & DMA_BURST32)
0447         csr |= DMA_E_BURST32;
0448     else
0449         csr |= DMA_E_BURST16;
0450 
0451     csr |= (DMA_DSBL_RD_DRN | DMA_DSBL_WR_INV | DMA_FIFO_INV);
0452 
0453     if (lp->tpe)
0454         csr |= DMA_EN_ENETAUI;
0455     else
0456         csr &= ~DMA_EN_ENETAUI;
0457     udelay(20);
0458     sbus_writel(csr, lp->dregs + DMA_CSR);
0459     udelay(200);
0460 }
0461 
0462 static int init_restart_lance(struct lance_private *lp)
0463 {
0464     u16 regval = 0;
0465     int i;
0466 
0467     if (lp->dregs)
0468         init_restart_ledma(lp);
0469 
0470     sbus_writew(LE_CSR0,    lp->lregs + RAP);
0471     sbus_writew(LE_C0_INIT, lp->lregs + RDP);
0472 
0473     /* Wait for the lance to complete initialization */
0474     for (i = 0; i < 100; i++) {
0475         regval = sbus_readw(lp->lregs + RDP);
0476 
0477         if (regval & (LE_C0_ERR | LE_C0_IDON))
0478             break;
0479         barrier();
0480     }
0481     if (i == 100 || (regval & LE_C0_ERR)) {
0482         printk(KERN_ERR "LANCE unopened after %d ticks, csr0=%4.4x.\n",
0483                i, regval);
0484         if (lp->dregs)
0485             printk("dcsr=%8.8x\n", sbus_readl(lp->dregs + DMA_CSR));
0486         return -1;
0487     }
0488 
0489     /* Clear IDON by writing a "1", enable interrupts and start lance */
0490     sbus_writew(LE_C0_IDON,         lp->lregs + RDP);
0491     sbus_writew(LE_C0_INEA | LE_C0_STRT,    lp->lregs + RDP);
0492 
0493     if (lp->dregs) {
0494         u32 csr = sbus_readl(lp->dregs + DMA_CSR);
0495 
0496         csr |= DMA_INT_ENAB;
0497         sbus_writel(csr, lp->dregs + DMA_CSR);
0498     }
0499 
0500     return 0;
0501 }
0502 
0503 static void lance_rx_dvma(struct net_device *dev)
0504 {
0505     struct lance_private *lp = netdev_priv(dev);
0506     struct lance_init_block *ib = lp->init_block_mem;
0507     struct lance_rx_desc *rd;
0508     u8 bits;
0509     int len, entry = lp->rx_new;
0510     struct sk_buff *skb;
0511 
0512     for (rd = &ib->brx_ring [entry];
0513          !((bits = rd->rmd1_bits) & LE_R1_OWN);
0514          rd = &ib->brx_ring [entry]) {
0515 
0516         /* We got an incomplete frame? */
0517         if ((bits & LE_R1_POK) != LE_R1_POK) {
0518             dev->stats.rx_over_errors++;
0519             dev->stats.rx_errors++;
0520         } else if (bits & LE_R1_ERR) {
0521             /* Count only the end frame as a rx error,
0522              * not the beginning
0523              */
0524             if (bits & LE_R1_BUF) dev->stats.rx_fifo_errors++;
0525             if (bits & LE_R1_CRC) dev->stats.rx_crc_errors++;
0526             if (bits & LE_R1_OFL) dev->stats.rx_over_errors++;
0527             if (bits & LE_R1_FRA) dev->stats.rx_frame_errors++;
0528             if (bits & LE_R1_EOP) dev->stats.rx_errors++;
0529         } else {
0530             len = (rd->mblength & 0xfff) - 4;
0531             skb = netdev_alloc_skb(dev, len + 2);
0532 
0533             if (skb == NULL) {
0534                 dev->stats.rx_dropped++;
0535                 rd->mblength = 0;
0536                 rd->rmd1_bits = LE_R1_OWN;
0537                 lp->rx_new = RX_NEXT(entry);
0538                 return;
0539             }
0540 
0541             dev->stats.rx_bytes += len;
0542 
0543             skb_reserve(skb, 2);        /* 16 byte align */
0544             skb_put(skb, len);      /* make room */
0545             skb_copy_to_linear_data(skb,
0546                      (unsigned char *)&(ib->rx_buf [entry][0]),
0547                      len);
0548             skb->protocol = eth_type_trans(skb, dev);
0549             netif_rx(skb);
0550             dev->stats.rx_packets++;
0551         }
0552 
0553         /* Return the packet to the pool */
0554         rd->mblength = 0;
0555         rd->rmd1_bits = LE_R1_OWN;
0556         entry = RX_NEXT(entry);
0557     }
0558 
0559     lp->rx_new = entry;
0560 }
0561 
0562 static void lance_tx_dvma(struct net_device *dev)
0563 {
0564     struct lance_private *lp = netdev_priv(dev);
0565     struct lance_init_block *ib = lp->init_block_mem;
0566     int i, j;
0567 
0568     spin_lock(&lp->lock);
0569 
0570     j = lp->tx_old;
0571     for (i = j; i != lp->tx_new; i = j) {
0572         struct lance_tx_desc *td = &ib->btx_ring [i];
0573         u8 bits = td->tmd1_bits;
0574 
0575         /* If we hit a packet not owned by us, stop */
0576         if (bits & LE_T1_OWN)
0577             break;
0578 
0579         if (bits & LE_T1_ERR) {
0580             u16 status = td->misc;
0581 
0582             dev->stats.tx_errors++;
0583             if (status & LE_T3_RTY)  dev->stats.tx_aborted_errors++;
0584             if (status & LE_T3_LCOL) dev->stats.tx_window_errors++;
0585 
0586             if (status & LE_T3_CLOS) {
0587                 dev->stats.tx_carrier_errors++;
0588                 if (lp->auto_select) {
0589                     lp->tpe = 1 - lp->tpe;
0590                     printk(KERN_NOTICE "%s: Carrier Lost, trying %s\n",
0591                            dev->name, lp->tpe?"TPE":"AUI");
0592                     STOP_LANCE(lp);
0593                     lp->init_ring(dev);
0594                     load_csrs(lp);
0595                     init_restart_lance(lp);
0596                     goto out;
0597                 }
0598             }
0599 
0600             /* Buffer errors and underflows turn off the
0601              * transmitter, restart the adapter.
0602              */
0603             if (status & (LE_T3_BUF|LE_T3_UFL)) {
0604                 dev->stats.tx_fifo_errors++;
0605 
0606                 printk(KERN_ERR "%s: Tx: ERR_BUF|ERR_UFL, restarting\n",
0607                        dev->name);
0608                 STOP_LANCE(lp);
0609                 lp->init_ring(dev);
0610                 load_csrs(lp);
0611                 init_restart_lance(lp);
0612                 goto out;
0613             }
0614         } else if ((bits & LE_T1_POK) == LE_T1_POK) {
0615             /*
0616              * So we don't count the packet more than once.
0617              */
0618             td->tmd1_bits = bits & ~(LE_T1_POK);
0619 
0620             /* One collision before packet was sent. */
0621             if (bits & LE_T1_EONE)
0622                 dev->stats.collisions++;
0623 
0624             /* More than one collision, be optimistic. */
0625             if (bits & LE_T1_EMORE)
0626                 dev->stats.collisions += 2;
0627 
0628             dev->stats.tx_packets++;
0629         }
0630 
0631         j = TX_NEXT(j);
0632     }
0633     lp->tx_old = j;
0634 out:
0635     if (netif_queue_stopped(dev) &&
0636         TX_BUFFS_AVAIL > 0)
0637         netif_wake_queue(dev);
0638 
0639     spin_unlock(&lp->lock);
0640 }
0641 
0642 static void lance_piocopy_to_skb(struct sk_buff *skb, void __iomem *piobuf, int len)
0643 {
0644     u16 *p16 = (u16 *) skb->data;
0645     u32 *p32;
0646     u8 *p8;
0647     void __iomem *pbuf = piobuf;
0648 
0649     /* We know here that both src and dest are on a 16bit boundary. */
0650     *p16++ = sbus_readw(pbuf);
0651     p32 = (u32 *) p16;
0652     pbuf += 2;
0653     len -= 2;
0654 
0655     while (len >= 4) {
0656         *p32++ = sbus_readl(pbuf);
0657         pbuf += 4;
0658         len -= 4;
0659     }
0660     p8 = (u8 *) p32;
0661     if (len >= 2) {
0662         p16 = (u16 *) p32;
0663         *p16++ = sbus_readw(pbuf);
0664         pbuf += 2;
0665         len -= 2;
0666         p8 = (u8 *) p16;
0667     }
0668     if (len >= 1)
0669         *p8 = sbus_readb(pbuf);
0670 }
0671 
0672 static void lance_rx_pio(struct net_device *dev)
0673 {
0674     struct lance_private *lp = netdev_priv(dev);
0675     struct lance_init_block __iomem *ib = lp->init_block_iomem;
0676     struct lance_rx_desc __iomem *rd;
0677     unsigned char bits;
0678     int len, entry;
0679     struct sk_buff *skb;
0680 
0681     entry = lp->rx_new;
0682     for (rd = &ib->brx_ring [entry];
0683          !((bits = sbus_readb(&rd->rmd1_bits)) & LE_R1_OWN);
0684          rd = &ib->brx_ring [entry]) {
0685 
0686         /* We got an incomplete frame? */
0687         if ((bits & LE_R1_POK) != LE_R1_POK) {
0688             dev->stats.rx_over_errors++;
0689             dev->stats.rx_errors++;
0690         } else if (bits & LE_R1_ERR) {
0691             /* Count only the end frame as a rx error,
0692              * not the beginning
0693              */
0694             if (bits & LE_R1_BUF) dev->stats.rx_fifo_errors++;
0695             if (bits & LE_R1_CRC) dev->stats.rx_crc_errors++;
0696             if (bits & LE_R1_OFL) dev->stats.rx_over_errors++;
0697             if (bits & LE_R1_FRA) dev->stats.rx_frame_errors++;
0698             if (bits & LE_R1_EOP) dev->stats.rx_errors++;
0699         } else {
0700             len = (sbus_readw(&rd->mblength) & 0xfff) - 4;
0701             skb = netdev_alloc_skb(dev, len + 2);
0702 
0703             if (skb == NULL) {
0704                 dev->stats.rx_dropped++;
0705                 sbus_writew(0, &rd->mblength);
0706                 sbus_writeb(LE_R1_OWN, &rd->rmd1_bits);
0707                 lp->rx_new = RX_NEXT(entry);
0708                 return;
0709             }
0710 
0711             dev->stats.rx_bytes += len;
0712 
0713             skb_reserve (skb, 2);       /* 16 byte align */
0714             skb_put(skb, len);      /* make room */
0715             lance_piocopy_to_skb(skb, &(ib->rx_buf[entry][0]), len);
0716             skb->protocol = eth_type_trans(skb, dev);
0717             netif_rx(skb);
0718             dev->stats.rx_packets++;
0719         }
0720 
0721         /* Return the packet to the pool */
0722         sbus_writew(0, &rd->mblength);
0723         sbus_writeb(LE_R1_OWN, &rd->rmd1_bits);
0724         entry = RX_NEXT(entry);
0725     }
0726 
0727     lp->rx_new = entry;
0728 }
0729 
0730 static void lance_tx_pio(struct net_device *dev)
0731 {
0732     struct lance_private *lp = netdev_priv(dev);
0733     struct lance_init_block __iomem *ib = lp->init_block_iomem;
0734     int i, j;
0735 
0736     spin_lock(&lp->lock);
0737 
0738     j = lp->tx_old;
0739     for (i = j; i != lp->tx_new; i = j) {
0740         struct lance_tx_desc __iomem *td = &ib->btx_ring [i];
0741         u8 bits = sbus_readb(&td->tmd1_bits);
0742 
0743         /* If we hit a packet not owned by us, stop */
0744         if (bits & LE_T1_OWN)
0745             break;
0746 
0747         if (bits & LE_T1_ERR) {
0748             u16 status = sbus_readw(&td->misc);
0749 
0750             dev->stats.tx_errors++;
0751             if (status & LE_T3_RTY)  dev->stats.tx_aborted_errors++;
0752             if (status & LE_T3_LCOL) dev->stats.tx_window_errors++;
0753 
0754             if (status & LE_T3_CLOS) {
0755                 dev->stats.tx_carrier_errors++;
0756                 if (lp->auto_select) {
0757                     lp->tpe = 1 - lp->tpe;
0758                     printk(KERN_NOTICE "%s: Carrier Lost, trying %s\n",
0759                            dev->name, lp->tpe?"TPE":"AUI");
0760                     STOP_LANCE(lp);
0761                     lp->init_ring(dev);
0762                     load_csrs(lp);
0763                     init_restart_lance(lp);
0764                     goto out;
0765                 }
0766             }
0767 
0768             /* Buffer errors and underflows turn off the
0769              * transmitter, restart the adapter.
0770              */
0771             if (status & (LE_T3_BUF|LE_T3_UFL)) {
0772                 dev->stats.tx_fifo_errors++;
0773 
0774                 printk(KERN_ERR "%s: Tx: ERR_BUF|ERR_UFL, restarting\n",
0775                        dev->name);
0776                 STOP_LANCE(lp);
0777                 lp->init_ring(dev);
0778                 load_csrs(lp);
0779                 init_restart_lance(lp);
0780                 goto out;
0781             }
0782         } else if ((bits & LE_T1_POK) == LE_T1_POK) {
0783             /*
0784              * So we don't count the packet more than once.
0785              */
0786             sbus_writeb(bits & ~(LE_T1_POK), &td->tmd1_bits);
0787 
0788             /* One collision before packet was sent. */
0789             if (bits & LE_T1_EONE)
0790                 dev->stats.collisions++;
0791 
0792             /* More than one collision, be optimistic. */
0793             if (bits & LE_T1_EMORE)
0794                 dev->stats.collisions += 2;
0795 
0796             dev->stats.tx_packets++;
0797         }
0798 
0799         j = TX_NEXT(j);
0800     }
0801     lp->tx_old = j;
0802 
0803     if (netif_queue_stopped(dev) &&
0804         TX_BUFFS_AVAIL > 0)
0805         netif_wake_queue(dev);
0806 out:
0807     spin_unlock(&lp->lock);
0808 }
0809 
0810 static irqreturn_t lance_interrupt(int irq, void *dev_id)
0811 {
0812     struct net_device *dev = dev_id;
0813     struct lance_private *lp = netdev_priv(dev);
0814     int csr0;
0815 
0816     sbus_writew(LE_CSR0, lp->lregs + RAP);
0817     csr0 = sbus_readw(lp->lregs + RDP);
0818 
0819     /* Acknowledge all the interrupt sources ASAP */
0820     sbus_writew(csr0 & (LE_C0_INTR | LE_C0_TINT | LE_C0_RINT),
0821             lp->lregs + RDP);
0822 
0823     if ((csr0 & LE_C0_ERR) != 0) {
0824         /* Clear the error condition */
0825         sbus_writew((LE_C0_BABL | LE_C0_ERR | LE_C0_MISS |
0826                  LE_C0_CERR | LE_C0_MERR),
0827                 lp->lregs + RDP);
0828     }
0829 
0830     if (csr0 & LE_C0_RINT)
0831         lp->rx(dev);
0832 
0833     if (csr0 & LE_C0_TINT)
0834         lp->tx(dev);
0835 
0836     if (csr0 & LE_C0_BABL)
0837         dev->stats.tx_errors++;
0838 
0839     if (csr0 & LE_C0_MISS)
0840         dev->stats.rx_errors++;
0841 
0842     if (csr0 & LE_C0_MERR) {
0843         if (lp->dregs) {
0844             u32 addr = sbus_readl(lp->dregs + DMA_ADDR);
0845 
0846             printk(KERN_ERR "%s: Memory error, status %04x, addr %06x\n",
0847                    dev->name, csr0, addr & 0xffffff);
0848         } else {
0849             printk(KERN_ERR "%s: Memory error, status %04x\n",
0850                    dev->name, csr0);
0851         }
0852 
0853         sbus_writew(LE_C0_STOP, lp->lregs + RDP);
0854 
0855         if (lp->dregs) {
0856             u32 dma_csr = sbus_readl(lp->dregs + DMA_CSR);
0857 
0858             dma_csr |= DMA_FIFO_INV;
0859             sbus_writel(dma_csr, lp->dregs + DMA_CSR);
0860         }
0861 
0862         lp->init_ring(dev);
0863         load_csrs(lp);
0864         init_restart_lance(lp);
0865         netif_wake_queue(dev);
0866     }
0867 
0868     sbus_writew(LE_C0_INEA, lp->lregs + RDP);
0869 
0870     return IRQ_HANDLED;
0871 }
0872 
0873 /* Build a fake network packet and send it to ourselves. */
0874 static void build_fake_packet(struct lance_private *lp)
0875 {
0876     struct net_device *dev = lp->dev;
0877     int i, entry;
0878 
0879     entry = lp->tx_new & TX_RING_MOD_MASK;
0880     if (lp->pio_buffer) {
0881         struct lance_init_block __iomem *ib = lp->init_block_iomem;
0882         u16 __iomem *packet = (u16 __iomem *) &(ib->tx_buf[entry][0]);
0883         struct ethhdr __iomem *eth = (struct ethhdr __iomem *) packet;
0884         for (i = 0; i < (ETH_ZLEN / sizeof(u16)); i++)
0885             sbus_writew(0, &packet[i]);
0886         for (i = 0; i < 6; i++) {
0887             sbus_writeb(dev->dev_addr[i], &eth->h_dest[i]);
0888             sbus_writeb(dev->dev_addr[i], &eth->h_source[i]);
0889         }
0890         sbus_writew((-ETH_ZLEN) | 0xf000, &ib->btx_ring[entry].length);
0891         sbus_writew(0, &ib->btx_ring[entry].misc);
0892         sbus_writeb(LE_T1_POK|LE_T1_OWN, &ib->btx_ring[entry].tmd1_bits);
0893     } else {
0894         struct lance_init_block *ib = lp->init_block_mem;
0895         u16 *packet = (u16 *) &(ib->tx_buf[entry][0]);
0896         struct ethhdr *eth = (struct ethhdr *) packet;
0897         memset(packet, 0, ETH_ZLEN);
0898         for (i = 0; i < 6; i++) {
0899             eth->h_dest[i] = dev->dev_addr[i];
0900             eth->h_source[i] = dev->dev_addr[i];
0901         }
0902         ib->btx_ring[entry].length = (-ETH_ZLEN) | 0xf000;
0903         ib->btx_ring[entry].misc = 0;
0904         ib->btx_ring[entry].tmd1_bits = (LE_T1_POK|LE_T1_OWN);
0905     }
0906     lp->tx_new = TX_NEXT(entry);
0907 }
0908 
0909 static int lance_open(struct net_device *dev)
0910 {
0911     struct lance_private *lp = netdev_priv(dev);
0912     int status = 0;
0913 
0914     STOP_LANCE(lp);
0915 
0916     if (request_irq(dev->irq, lance_interrupt, IRQF_SHARED,
0917             lancestr, (void *) dev)) {
0918         printk(KERN_ERR "Lance: Can't get irq %d\n", dev->irq);
0919         return -EAGAIN;
0920     }
0921 
0922     /* On the 4m, setup the ledma to provide the upper bits for buffers */
0923     if (lp->dregs) {
0924         u32 regval = lp->init_block_dvma & 0xff000000;
0925 
0926         sbus_writel(regval, lp->dregs + DMA_TEST);
0927     }
0928 
0929     /* Set mode and clear multicast filter only at device open,
0930      * so that lance_init_ring() called at any error will not
0931      * forget multicast filters.
0932      *
0933      * BTW it is common bug in all lance drivers! --ANK
0934      */
0935     if (lp->pio_buffer) {
0936         struct lance_init_block __iomem *ib = lp->init_block_iomem;
0937         sbus_writew(0, &ib->mode);
0938         sbus_writel(0, &ib->filter[0]);
0939         sbus_writel(0, &ib->filter[1]);
0940     } else {
0941         struct lance_init_block *ib = lp->init_block_mem;
0942         ib->mode = 0;
0943         ib->filter [0] = 0;
0944         ib->filter [1] = 0;
0945     }
0946 
0947     lp->init_ring(dev);
0948     load_csrs(lp);
0949 
0950     netif_start_queue(dev);
0951 
0952     status = init_restart_lance(lp);
0953     if (!status && lp->auto_select) {
0954         build_fake_packet(lp);
0955         sbus_writew(LE_C0_INEA | LE_C0_TDMD, lp->lregs + RDP);
0956     }
0957 
0958     return status;
0959 }
0960 
0961 static int lance_close(struct net_device *dev)
0962 {
0963     struct lance_private *lp = netdev_priv(dev);
0964 
0965     netif_stop_queue(dev);
0966     del_timer_sync(&lp->multicast_timer);
0967 
0968     STOP_LANCE(lp);
0969 
0970     free_irq(dev->irq, (void *) dev);
0971     return 0;
0972 }
0973 
0974 static int lance_reset(struct net_device *dev)
0975 {
0976     struct lance_private *lp = netdev_priv(dev);
0977     int status;
0978 
0979     STOP_LANCE(lp);
0980 
0981     /* On the 4m, reset the dma too */
0982     if (lp->dregs) {
0983         u32 csr, addr;
0984 
0985         printk(KERN_ERR "resetting ledma\n");
0986         csr = sbus_readl(lp->dregs + DMA_CSR);
0987         sbus_writel(csr | DMA_RST_ENET, lp->dregs + DMA_CSR);
0988         udelay(200);
0989         sbus_writel(csr & ~DMA_RST_ENET, lp->dregs + DMA_CSR);
0990 
0991         addr = lp->init_block_dvma & 0xff000000;
0992         sbus_writel(addr, lp->dregs + DMA_TEST);
0993     }
0994     lp->init_ring(dev);
0995     load_csrs(lp);
0996     netif_trans_update(dev); /* prevent tx timeout */
0997     status = init_restart_lance(lp);
0998     return status;
0999 }
1000 
1001 static void lance_piocopy_from_skb(void __iomem *dest, unsigned char *src, int len)
1002 {
1003     void __iomem *piobuf = dest;
1004     u32 *p32;
1005     u16 *p16;
1006     u8 *p8;
1007 
1008     switch ((unsigned long)src & 0x3) {
1009     case 0:
1010         p32 = (u32 *) src;
1011         while (len >= 4) {
1012             sbus_writel(*p32, piobuf);
1013             p32++;
1014             piobuf += 4;
1015             len -= 4;
1016         }
1017         src = (char *) p32;
1018         break;
1019     case 1:
1020     case 3:
1021         p8 = (u8 *) src;
1022         while (len >= 4) {
1023             u32 val;
1024 
1025             val  = p8[0] << 24;
1026             val |= p8[1] << 16;
1027             val |= p8[2] << 8;
1028             val |= p8[3];
1029             sbus_writel(val, piobuf);
1030             p8 += 4;
1031             piobuf += 4;
1032             len -= 4;
1033         }
1034         src = (char *) p8;
1035         break;
1036     case 2:
1037         p16 = (u16 *) src;
1038         while (len >= 4) {
1039             u32 val = p16[0]<<16 | p16[1];
1040             sbus_writel(val, piobuf);
1041             p16 += 2;
1042             piobuf += 4;
1043             len -= 4;
1044         }
1045         src = (char *) p16;
1046         break;
1047     }
1048     if (len >= 2) {
1049         u16 val = src[0] << 8 | src[1];
1050         sbus_writew(val, piobuf);
1051         src += 2;
1052         piobuf += 2;
1053         len -= 2;
1054     }
1055     if (len >= 1)
1056         sbus_writeb(src[0], piobuf);
1057 }
1058 
1059 static void lance_piozero(void __iomem *dest, int len)
1060 {
1061     void __iomem *piobuf = dest;
1062 
1063     if ((unsigned long)piobuf & 1) {
1064         sbus_writeb(0, piobuf);
1065         piobuf += 1;
1066         len -= 1;
1067         if (len == 0)
1068             return;
1069     }
1070     if (len == 1) {
1071         sbus_writeb(0, piobuf);
1072         return;
1073     }
1074     if ((unsigned long)piobuf & 2) {
1075         sbus_writew(0, piobuf);
1076         piobuf += 2;
1077         len -= 2;
1078         if (len == 0)
1079             return;
1080     }
1081     while (len >= 4) {
1082         sbus_writel(0, piobuf);
1083         piobuf += 4;
1084         len -= 4;
1085     }
1086     if (len >= 2) {
1087         sbus_writew(0, piobuf);
1088         piobuf += 2;
1089         len -= 2;
1090     }
1091     if (len >= 1)
1092         sbus_writeb(0, piobuf);
1093 }
1094 
1095 static void lance_tx_timeout(struct net_device *dev, unsigned int txqueue)
1096 {
1097     struct lance_private *lp = netdev_priv(dev);
1098 
1099     printk(KERN_ERR "%s: transmit timed out, status %04x, reset\n",
1100            dev->name, sbus_readw(lp->lregs + RDP));
1101     lance_reset(dev);
1102     netif_wake_queue(dev);
1103 }
1104 
1105 static netdev_tx_t lance_start_xmit(struct sk_buff *skb, struct net_device *dev)
1106 {
1107     struct lance_private *lp = netdev_priv(dev);
1108     int entry, skblen, len;
1109 
1110     skblen = skb->len;
1111 
1112     len = (skblen <= ETH_ZLEN) ? ETH_ZLEN : skblen;
1113 
1114     spin_lock_irq(&lp->lock);
1115 
1116     dev->stats.tx_bytes += len;
1117 
1118     entry = lp->tx_new & TX_RING_MOD_MASK;
1119     if (lp->pio_buffer) {
1120         struct lance_init_block __iomem *ib = lp->init_block_iomem;
1121         sbus_writew((-len) | 0xf000, &ib->btx_ring[entry].length);
1122         sbus_writew(0, &ib->btx_ring[entry].misc);
1123         lance_piocopy_from_skb(&ib->tx_buf[entry][0], skb->data, skblen);
1124         if (len != skblen)
1125             lance_piozero(&ib->tx_buf[entry][skblen], len - skblen);
1126         sbus_writeb(LE_T1_POK | LE_T1_OWN, &ib->btx_ring[entry].tmd1_bits);
1127     } else {
1128         struct lance_init_block *ib = lp->init_block_mem;
1129         ib->btx_ring [entry].length = (-len) | 0xf000;
1130         ib->btx_ring [entry].misc = 0;
1131         skb_copy_from_linear_data(skb, &ib->tx_buf [entry][0], skblen);
1132         if (len != skblen)
1133             memset((char *) &ib->tx_buf [entry][skblen], 0, len - skblen);
1134         ib->btx_ring [entry].tmd1_bits = (LE_T1_POK | LE_T1_OWN);
1135     }
1136 
1137     lp->tx_new = TX_NEXT(entry);
1138 
1139     if (TX_BUFFS_AVAIL <= 0)
1140         netif_stop_queue(dev);
1141 
1142     /* Kick the lance: transmit now */
1143     sbus_writew(LE_C0_INEA | LE_C0_TDMD, lp->lregs + RDP);
1144 
1145     /* Read back CSR to invalidate the E-Cache.
1146      * This is needed, because DMA_DSBL_WR_INV is set.
1147      */
1148     if (lp->dregs)
1149         sbus_readw(lp->lregs + RDP);
1150 
1151     spin_unlock_irq(&lp->lock);
1152 
1153     dev_kfree_skb(skb);
1154 
1155     return NETDEV_TX_OK;
1156 }
1157 
1158 /* taken from the depca driver */
1159 static void lance_load_multicast(struct net_device *dev)
1160 {
1161     struct lance_private *lp = netdev_priv(dev);
1162     struct netdev_hw_addr *ha;
1163     u32 crc;
1164     u32 val;
1165 
1166     /* set all multicast bits */
1167     if (dev->flags & IFF_ALLMULTI)
1168         val = ~0;
1169     else
1170         val = 0;
1171 
1172     if (lp->pio_buffer) {
1173         struct lance_init_block __iomem *ib = lp->init_block_iomem;
1174         sbus_writel(val, &ib->filter[0]);
1175         sbus_writel(val, &ib->filter[1]);
1176     } else {
1177         struct lance_init_block *ib = lp->init_block_mem;
1178         ib->filter [0] = val;
1179         ib->filter [1] = val;
1180     }
1181 
1182     if (dev->flags & IFF_ALLMULTI)
1183         return;
1184 
1185     /* Add addresses */
1186     netdev_for_each_mc_addr(ha, dev) {
1187         crc = ether_crc_le(6, ha->addr);
1188         crc = crc >> 26;
1189         if (lp->pio_buffer) {
1190             struct lance_init_block __iomem *ib = lp->init_block_iomem;
1191             u16 __iomem *mcast_table = (u16 __iomem *) &ib->filter;
1192             u16 tmp = sbus_readw(&mcast_table[crc>>4]);
1193             tmp |= 1 << (crc & 0xf);
1194             sbus_writew(tmp, &mcast_table[crc>>4]);
1195         } else {
1196             struct lance_init_block *ib = lp->init_block_mem;
1197             u16 *mcast_table = (u16 *) &ib->filter;
1198             mcast_table [crc >> 4] |= 1 << (crc & 0xf);
1199         }
1200     }
1201 }
1202 
1203 static void lance_set_multicast(struct net_device *dev)
1204 {
1205     struct lance_private *lp = netdev_priv(dev);
1206     struct lance_init_block *ib_mem = lp->init_block_mem;
1207     struct lance_init_block __iomem *ib_iomem = lp->init_block_iomem;
1208     u16 mode;
1209 
1210     if (!netif_running(dev))
1211         return;
1212 
1213     if (lp->tx_old != lp->tx_new) {
1214         mod_timer(&lp->multicast_timer, jiffies + 4);
1215         netif_wake_queue(dev);
1216         return;
1217     }
1218 
1219     netif_stop_queue(dev);
1220 
1221     STOP_LANCE(lp);
1222     lp->init_ring(dev);
1223 
1224     if (lp->pio_buffer)
1225         mode = sbus_readw(&ib_iomem->mode);
1226     else
1227         mode = ib_mem->mode;
1228     if (dev->flags & IFF_PROMISC) {
1229         mode |= LE_MO_PROM;
1230         if (lp->pio_buffer)
1231             sbus_writew(mode, &ib_iomem->mode);
1232         else
1233             ib_mem->mode = mode;
1234     } else {
1235         mode &= ~LE_MO_PROM;
1236         if (lp->pio_buffer)
1237             sbus_writew(mode, &ib_iomem->mode);
1238         else
1239             ib_mem->mode = mode;
1240         lance_load_multicast(dev);
1241     }
1242     load_csrs(lp);
1243     init_restart_lance(lp);
1244     netif_wake_queue(dev);
1245 }
1246 
1247 static void lance_set_multicast_retry(struct timer_list *t)
1248 {
1249     struct lance_private *lp = from_timer(lp, t, multicast_timer);
1250     struct net_device *dev = lp->dev;
1251 
1252     lance_set_multicast(dev);
1253 }
1254 
1255 static void lance_free_hwresources(struct lance_private *lp)
1256 {
1257     if (lp->lregs)
1258         of_iounmap(&lp->op->resource[0], lp->lregs, LANCE_REG_SIZE);
1259     if (lp->dregs) {
1260         struct platform_device *ledma = lp->ledma;
1261 
1262         of_iounmap(&ledma->resource[0], lp->dregs,
1263                resource_size(&ledma->resource[0]));
1264     }
1265     if (lp->init_block_iomem) {
1266         of_iounmap(&lp->lebuffer->resource[0], lp->init_block_iomem,
1267                sizeof(struct lance_init_block));
1268     } else if (lp->init_block_mem) {
1269         dma_free_coherent(&lp->op->dev,
1270                   sizeof(struct lance_init_block),
1271                   lp->init_block_mem,
1272                   lp->init_block_dvma);
1273     }
1274 }
1275 
1276 /* Ethtool support... */
1277 static void sparc_lance_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
1278 {
1279     strlcpy(info->driver, "sunlance", sizeof(info->driver));
1280 }
1281 
1282 static const struct ethtool_ops sparc_lance_ethtool_ops = {
1283     .get_drvinfo        = sparc_lance_get_drvinfo,
1284     .get_link       = ethtool_op_get_link,
1285 };
1286 
1287 static const struct net_device_ops sparc_lance_ops = {
1288     .ndo_open       = lance_open,
1289     .ndo_stop       = lance_close,
1290     .ndo_start_xmit     = lance_start_xmit,
1291     .ndo_set_rx_mode    = lance_set_multicast,
1292     .ndo_tx_timeout     = lance_tx_timeout,
1293     .ndo_set_mac_address    = eth_mac_addr,
1294     .ndo_validate_addr  = eth_validate_addr,
1295 };
1296 
1297 static int sparc_lance_probe_one(struct platform_device *op,
1298                  struct platform_device *ledma,
1299                  struct platform_device *lebuffer)
1300 {
1301     struct device_node *dp = op->dev.of_node;
1302     struct lance_private *lp;
1303     struct net_device *dev;
1304 
1305     dev = alloc_etherdev(sizeof(struct lance_private) + 8);
1306     if (!dev)
1307         return -ENOMEM;
1308 
1309     lp = netdev_priv(dev);
1310 
1311     spin_lock_init(&lp->lock);
1312 
1313     /* Copy the IDPROM ethernet address to the device structure, later we
1314      * will copy the address in the device structure to the lance
1315      * initialization block.
1316      */
1317     eth_hw_addr_set(dev, idprom->id_ethaddr);
1318 
1319     /* Get the IO region */
1320     lp->lregs = of_ioremap(&op->resource[0], 0,
1321                    LANCE_REG_SIZE, lancestr);
1322     if (!lp->lregs) {
1323         printk(KERN_ERR "SunLance: Cannot map registers.\n");
1324         goto fail;
1325     }
1326 
1327     lp->ledma = ledma;
1328     if (lp->ledma) {
1329         lp->dregs = of_ioremap(&ledma->resource[0], 0,
1330                        resource_size(&ledma->resource[0]),
1331                        "ledma");
1332         if (!lp->dregs) {
1333             printk(KERN_ERR "SunLance: Cannot map "
1334                    "ledma registers.\n");
1335             goto fail;
1336         }
1337     }
1338 
1339     lp->op = op;
1340     lp->lebuffer = lebuffer;
1341     if (lebuffer) {
1342         /* sanity check */
1343         if (lebuffer->resource[0].start & 7) {
1344             printk(KERN_ERR "SunLance: ERROR: Rx and Tx rings not on even boundary.\n");
1345             goto fail;
1346         }
1347         lp->init_block_iomem =
1348             of_ioremap(&lebuffer->resource[0], 0,
1349                    sizeof(struct lance_init_block), "lebuffer");
1350         if (!lp->init_block_iomem) {
1351             printk(KERN_ERR "SunLance: Cannot map PIO buffer.\n");
1352             goto fail;
1353         }
1354         lp->init_block_dvma = 0;
1355         lp->pio_buffer = 1;
1356         lp->init_ring = lance_init_ring_pio;
1357         lp->rx = lance_rx_pio;
1358         lp->tx = lance_tx_pio;
1359     } else {
1360         lp->init_block_mem =
1361             dma_alloc_coherent(&op->dev,
1362                        sizeof(struct lance_init_block),
1363                        &lp->init_block_dvma, GFP_ATOMIC);
1364         if (!lp->init_block_mem)
1365             goto fail;
1366 
1367         lp->pio_buffer = 0;
1368         lp->init_ring = lance_init_ring_dvma;
1369         lp->rx = lance_rx_dvma;
1370         lp->tx = lance_tx_dvma;
1371     }
1372     lp->busmaster_regval = of_getintprop_default(dp,  "busmaster-regval",
1373                              (LE_C3_BSWP |
1374                               LE_C3_ACON |
1375                               LE_C3_BCON));
1376 
1377     lp->name = lancestr;
1378 
1379     lp->burst_sizes = 0;
1380     if (lp->ledma) {
1381         struct device_node *ledma_dp = ledma->dev.of_node;
1382         struct device_node *sbus_dp;
1383         unsigned int sbmask;
1384         const char *prop;
1385         u32 csr;
1386 
1387         /* Find burst-size property for ledma */
1388         lp->burst_sizes = of_getintprop_default(ledma_dp,
1389                             "burst-sizes", 0);
1390 
1391         /* ledma may be capable of fast bursts, but sbus may not. */
1392         sbus_dp = ledma_dp->parent;
1393         sbmask = of_getintprop_default(sbus_dp, "burst-sizes",
1394                            DMA_BURSTBITS);
1395         lp->burst_sizes &= sbmask;
1396 
1397         /* Get the cable-selection property */
1398         prop = of_get_property(ledma_dp, "cable-selection", NULL);
1399         if (!prop || prop[0] == '\0') {
1400             struct device_node *nd;
1401 
1402             printk(KERN_INFO "SunLance: using "
1403                    "auto-carrier-detection.\n");
1404 
1405             nd = of_find_node_by_path("/options");
1406             if (!nd)
1407                 goto no_link_test;
1408 
1409             prop = of_get_property(nd, "tpe-link-test?", NULL);
1410             if (!prop)
1411                 goto node_put;
1412 
1413             if (strcmp(prop, "true")) {
1414                 printk(KERN_NOTICE "SunLance: warning: overriding option "
1415                        "'tpe-link-test?'\n");
1416                 printk(KERN_NOTICE "SunLance: warning: mail any problems "
1417                        "to ecd@skynet.be\n");
1418                 auxio_set_lte(AUXIO_LTE_ON);
1419             }
1420 node_put:
1421             of_node_put(nd);
1422 no_link_test:
1423             lp->auto_select = 1;
1424             lp->tpe = 0;
1425         } else if (!strcmp(prop, "aui")) {
1426             lp->auto_select = 0;
1427             lp->tpe = 0;
1428         } else {
1429             lp->auto_select = 0;
1430             lp->tpe = 1;
1431         }
1432 
1433         /* Reset ledma */
1434         csr = sbus_readl(lp->dregs + DMA_CSR);
1435         sbus_writel(csr | DMA_RST_ENET, lp->dregs + DMA_CSR);
1436         udelay(200);
1437         sbus_writel(csr & ~DMA_RST_ENET, lp->dregs + DMA_CSR);
1438     } else
1439         lp->dregs = NULL;
1440 
1441     lp->dev = dev;
1442     SET_NETDEV_DEV(dev, &op->dev);
1443     dev->watchdog_timeo = 5*HZ;
1444     dev->ethtool_ops = &sparc_lance_ethtool_ops;
1445     dev->netdev_ops = &sparc_lance_ops;
1446 
1447     dev->irq = op->archdata.irqs[0];
1448 
1449     /* We cannot sleep if the chip is busy during a
1450      * multicast list update event, because such events
1451      * can occur from interrupts (ex. IPv6).  So we
1452      * use a timer to try again later when necessary. -DaveM
1453      */
1454     timer_setup(&lp->multicast_timer, lance_set_multicast_retry, 0);
1455 
1456     if (register_netdev(dev)) {
1457         printk(KERN_ERR "SunLance: Cannot register device.\n");
1458         goto fail;
1459     }
1460 
1461     platform_set_drvdata(op, lp);
1462 
1463     printk(KERN_INFO "%s: LANCE %pM\n",
1464            dev->name, dev->dev_addr);
1465 
1466     return 0;
1467 
1468 fail:
1469     lance_free_hwresources(lp);
1470     free_netdev(dev);
1471     return -ENODEV;
1472 }
1473 
1474 static int sunlance_sbus_probe(struct platform_device *op)
1475 {
1476     struct platform_device *parent = to_platform_device(op->dev.parent);
1477     struct device_node *parent_dp = parent->dev.of_node;
1478     int err;
1479 
1480     if (of_node_name_eq(parent_dp, "ledma")) {
1481         err = sparc_lance_probe_one(op, parent, NULL);
1482     } else if (of_node_name_eq(parent_dp, "lebuffer")) {
1483         err = sparc_lance_probe_one(op, NULL, parent);
1484     } else
1485         err = sparc_lance_probe_one(op, NULL, NULL);
1486 
1487     return err;
1488 }
1489 
1490 static int sunlance_sbus_remove(struct platform_device *op)
1491 {
1492     struct lance_private *lp = platform_get_drvdata(op);
1493     struct net_device *net_dev = lp->dev;
1494 
1495     unregister_netdev(net_dev);
1496 
1497     lance_free_hwresources(lp);
1498 
1499     free_netdev(net_dev);
1500 
1501     return 0;
1502 }
1503 
1504 static const struct of_device_id sunlance_sbus_match[] = {
1505     {
1506         .name = "le",
1507     },
1508     {},
1509 };
1510 
1511 MODULE_DEVICE_TABLE(of, sunlance_sbus_match);
1512 
1513 static struct platform_driver sunlance_sbus_driver = {
1514     .driver = {
1515         .name = "sunlance",
1516         .of_match_table = sunlance_sbus_match,
1517     },
1518     .probe      = sunlance_sbus_probe,
1519     .remove     = sunlance_sbus_remove,
1520 };
1521 
1522 module_platform_driver(sunlance_sbus_driver);