sb1250-mac.c: De-typedef, de-volatile, de-etc...
[firefly-linux-kernel-4.4.55.git] / drivers / net / sb1250-mac.c
index 0a3a379b634c15a39de6e7f25f06d207f5630537..0cffd46724dfc8a5e93a6ee54ec4c75d10fe0427 100644 (file)
@@ -95,19 +95,28 @@ MODULE_PARM_DESC(full_duplex, "1-" __MODULE_STRING(MAX_UNITS));
 #endif
 
 #ifdef CONFIG_SBMAC_COALESCE
-static int int_pktcnt = 0;
-module_param(int_pktcnt, int, S_IRUGO);
-MODULE_PARM_DESC(int_pktcnt, "Packet count");
+static int int_pktcnt_tx = 255;
+module_param(int_pktcnt_tx, int, S_IRUGO);
+MODULE_PARM_DESC(int_pktcnt_tx, "TX packet count");
 
-static int int_timeout = 0;
-module_param(int_timeout, int, S_IRUGO);
-MODULE_PARM_DESC(int_timeout, "Timeout value");
+static int int_timeout_tx = 255;
+module_param(int_timeout_tx, int, S_IRUGO);
+MODULE_PARM_DESC(int_timeout_tx, "TX timeout value");
+
+static int int_pktcnt_rx = 64;
+module_param(int_pktcnt_rx, int, S_IRUGO);
+MODULE_PARM_DESC(int_pktcnt_rx, "RX packet count");
+
+static int int_timeout_rx = 64;
+module_param(int_timeout_rx, int, S_IRUGO);
+MODULE_PARM_DESC(int_timeout_rx, "RX timeout value");
 #endif
 
 #include <asm/sibyte/sb1250.h>
 #if defined(CONFIG_SIBYTE_BCM1x55) || defined(CONFIG_SIBYTE_BCM1x80)
 #include <asm/sibyte/bcm1480_regs.h>
 #include <asm/sibyte/bcm1480_int.h>
+#define R_MAC_DMA_OODPKTLOST_RX        R_MAC_DMA_OODPKTLOST
 #elif defined(CONFIG_SIBYTE_SB1250) || defined(CONFIG_SIBYTE_BCM112X)
 #include <asm/sibyte/sb1250_regs.h>
 #include <asm/sibyte/sb1250_int.h>
@@ -131,17 +140,17 @@ MODULE_PARM_DESC(int_timeout, "Timeout value");
  ********************************************************************* */
 
 
-typedef enum { sbmac_speed_auto, sbmac_speed_10,
-              sbmac_speed_100, sbmac_speed_1000 } sbmac_speed_t;
+enum sbmac_speed { sbmac_speed_auto, sbmac_speed_10,
+                  sbmac_speed_100, sbmac_speed_1000 };
 
-typedef enum { sbmac_duplex_auto, sbmac_duplex_half,
-              sbmac_duplex_full } sbmac_duplex_t;
+enum sbmac_duplex { sbmac_duplex_auto, sbmac_duplex_half,
+                   sbmac_duplex_full };
 
-typedef enum { sbmac_fc_auto, sbmac_fc_disabled, sbmac_fc_frame,
-              sbmac_fc_collision, sbmac_fc_carrier } sbmac_fc_t;
+enum sbmac_fc { sbmac_fc_auto, sbmac_fc_disabled, sbmac_fc_frame,
+               sbmac_fc_collision, sbmac_fc_carrier } sbmac_fc_t;
 
-typedef enum { sbmac_state_uninit, sbmac_state_off, sbmac_state_on,
-              sbmac_state_broken } sbmac_state_t;
+enum sbmac_state { sbmac_state_uninit, sbmac_state_off, sbmac_state_on,
+                  sbmac_state_broken };
 
 
 /**********************************************************************
@@ -155,8 +164,8 @@ typedef enum { sbmac_state_uninit, sbmac_state_off, sbmac_state_on,
 
 #define NUMCACHEBLKS(x) (((x)+SMP_CACHE_BYTES-1)/SMP_CACHE_BYTES)
 
-#define SBMAC_MAX_TXDESCR      32
-#define SBMAC_MAX_RXDESCR      32
+#define SBMAC_MAX_TXDESCR      256
+#define SBMAC_MAX_RXDESCR      256
 
 #define ETHER_ALIGN    2
 #define ETHER_ADDR_LEN 6
@@ -167,52 +176,61 @@ typedef enum { sbmac_state_uninit, sbmac_state_off, sbmac_state_on,
  *  DMA Descriptor structure
  ********************************************************************* */
 
-typedef struct sbdmadscr_s {
+struct sbdmadscr {
        uint64_t  dscr_a;
        uint64_t  dscr_b;
-} sbdmadscr_t;
-
-typedef unsigned long paddr_t;
+};
 
 /**********************************************************************
  *  DMA Controller structure
  ********************************************************************* */
 
-typedef struct sbmacdma_s {
+struct sbmacdma {
 
        /*
         * This stuff is used to identify the channel and the registers
         * associated with it.
         */
-
-       struct sbmac_softc *sbdma_eth;          /* back pointer to associated MAC */
-       int              sbdma_channel; /* channel number */
-       int              sbdma_txdir;       /* direction (1=transmit) */
-       int              sbdma_maxdescr;        /* total # of descriptors in ring */
+       struct sbmac_softc      *sbdma_eth;     /* back pointer to associated
+                                                  MAC */
+       int                     sbdma_channel;  /* channel number */
+       int                     sbdma_txdir;    /* direction (1=transmit) */
+       int                     sbdma_maxdescr; /* total # of descriptors
+                                                  in ring */
 #ifdef CONFIG_SBMAC_COALESCE
-       int              sbdma_int_pktcnt;  /* # descriptors rx/tx before interrupt*/
-       int              sbdma_int_timeout; /* # usec rx/tx interrupt */
+       int                     sbdma_int_pktcnt;
+                                               /* # descriptors rx/tx
+                                                  before interrupt */
+       int                     sbdma_int_timeout;
+                                               /* # usec rx/tx interrupt */
 #endif
-
-       volatile void __iomem *sbdma_config0;   /* DMA config register 0 */
-       volatile void __iomem *sbdma_config1;   /* DMA config register 1 */
-       volatile void __iomem *sbdma_dscrbase;  /* Descriptor base address */
-       volatile void __iomem *sbdma_dscrcnt;     /* Descriptor count register */
-       volatile void __iomem *sbdma_curdscr;   /* current descriptor address */
+       void __iomem            *sbdma_config0; /* DMA config register 0 */
+       void __iomem            *sbdma_config1; /* DMA config register 1 */
+       void __iomem            *sbdma_dscrbase;
+                                               /* descriptor base address */
+       void __iomem            *sbdma_dscrcnt; /* descriptor count register */
+       void __iomem            *sbdma_curdscr; /* current descriptor
+                                                  address */
+       void __iomem            *sbdma_oodpktlost;
+                                               /* pkt drop (rx only) */
 
        /*
         * This stuff is for maintenance of the ring
         */
-
-       sbdmadscr_t     *sbdma_dscrtable;       /* base of descriptor table */
-       sbdmadscr_t     *sbdma_dscrtable_end; /* end of descriptor table */
-
-       struct sk_buff **sbdma_ctxtable;    /* context table, one per descr */
-
-       paddr_t          sbdma_dscrtable_phys; /* and also the phys addr */
-       sbdmadscr_t     *sbdma_addptr;  /* next dscr for sw to add */
-       sbdmadscr_t     *sbdma_remptr;  /* next dscr for sw to remove */
-} sbmacdma_t;
+       void                    *sbdma_dscrtable_unaligned;
+       struct sbdmadscr        *sbdma_dscrtable;
+                                               /* base of descriptor table */
+       struct sbdmadscr        *sbdma_dscrtable_end;
+                                               /* end of descriptor table */
+       struct sk_buff          **sbdma_ctxtable;
+                                               /* context table, one
+                                                  per descr */
+       dma_addr_t              sbdma_dscrtable_phys;
+                                               /* and also the phys addr */
+       struct sbdmadscr        *sbdma_addptr;  /* next dscr for sw to add */
+       struct sbdmadscr        *sbdma_remptr;  /* next dscr for sw
+                                                  to remove */
+};
 
 
 /**********************************************************************
@@ -224,47 +242,45 @@ struct sbmac_softc {
        /*
         * Linux-specific things
         */
+       struct net_device       *sbm_dev;       /* pointer to linux device */
+       struct napi_struct      napi;
+       spinlock_t              sbm_lock;       /* spin lock */
+       struct timer_list       sbm_timer;      /* for monitoring MII */
+       int                     sbm_devflags;   /* current device flags */
 
-       struct net_device *sbm_dev;             /* pointer to linux device */
-       spinlock_t sbm_lock;            /* spin lock */
-       struct timer_list sbm_timer;            /* for monitoring MII */
-       struct net_device_stats sbm_stats;
-       int sbm_devflags;                       /* current device flags */
-
-       int          sbm_phy_oldbmsr;
-       int          sbm_phy_oldanlpar;
-       int          sbm_phy_oldk1stsr;
-       int          sbm_phy_oldlinkstat;
-       int sbm_buffersize;
+       int                     sbm_phy_oldbmsr;
+       int                     sbm_phy_oldanlpar;
+       int                     sbm_phy_oldk1stsr;
+       int                     sbm_phy_oldlinkstat;
+       int                     sbm_buffersize;
 
-       unsigned char sbm_phys[2];
+       unsigned char           sbm_phys[2];
 
        /*
         * Controller-specific things
         */
+       void __iomem            *sbm_base;      /* MAC's base address */
+       enum sbmac_state        sbm_state;      /* current state */
 
-       void __iomem            *sbm_base;          /* MAC's base address */
-       sbmac_state_t    sbm_state;         /* current state */
+       void __iomem            *sbm_macenable; /* MAC Enable Register */
+       void __iomem            *sbm_maccfg;    /* MAC Config Register */
+       void __iomem            *sbm_fifocfg;   /* FIFO Config Register */
+       void __iomem            *sbm_framecfg;  /* Frame Config Register */
+       void __iomem            *sbm_rxfilter;  /* Receive Filter Register */
+       void __iomem            *sbm_isr;       /* Interrupt Status Register */
+       void __iomem            *sbm_imr;       /* Interrupt Mask Register */
+       void __iomem            *sbm_mdio;      /* MDIO Register */
 
-       volatile void __iomem   *sbm_macenable; /* MAC Enable Register */
-       volatile void __iomem   *sbm_maccfg;    /* MAC Configuration Register */
-       volatile void __iomem   *sbm_fifocfg;   /* FIFO configuration register */
-       volatile void __iomem   *sbm_framecfg;  /* Frame configuration register */
-       volatile void __iomem   *sbm_rxfilter;  /* receive filter register */
-       volatile void __iomem   *sbm_isr;       /* Interrupt status register */
-       volatile void __iomem   *sbm_imr;       /* Interrupt mask register */
-       volatile void __iomem   *sbm_mdio;      /* MDIO register */
+       enum sbmac_speed        sbm_speed;      /* current speed */
+       enum sbmac_duplex       sbm_duplex;     /* current duplex */
+       enum sbmac_fc           sbm_fc;         /* cur. flow control setting */
 
-       sbmac_speed_t    sbm_speed;             /* current speed */
-       sbmac_duplex_t   sbm_duplex;    /* current duplex */
-       sbmac_fc_t       sbm_fc;                /* current flow control setting */
+       unsigned char           sbm_hwaddr[ETHER_ADDR_LEN];
 
-       unsigned char    sbm_hwaddr[ETHER_ADDR_LEN];
-
-       sbmacdma_t       sbm_txdma;             /* for now, only use channel 0 */
-       sbmacdma_t       sbm_rxdma;
-       int              rx_hw_checksum;
-       int              sbe_idx;
+       struct sbmacdma         sbm_txdma;      /* only channel 0 for now */
+       struct sbmacdma         sbm_rxdma;
+       int                     rx_hw_checksum;
+       int                     sbe_idx;
 };
 
 
@@ -276,45 +292,49 @@ struct sbmac_softc {
  *  Prototypes
  ********************************************************************* */
 
-static void sbdma_initctx(sbmacdma_t *d,
-                         struct sbmac_softc *s,
-                         int chan,
-                         int txrx,
-                         int maxdescr);
-static void sbdma_channel_start(sbmacdma_t *d, int rxtx);
-static int sbdma_add_rcvbuffer(sbmacdma_t *d,struct sk_buff *m);
-static int sbdma_add_txbuffer(sbmacdma_t *d,struct sk_buff *m);
-static void sbdma_emptyring(sbmacdma_t *d);
-static void sbdma_fillring(sbmacdma_t *d);
-static void sbdma_rx_process(struct sbmac_softc *sc,sbmacdma_t *d);
-static void sbdma_tx_process(struct sbmac_softc *sc,sbmacdma_t *d);
+static void sbdma_initctx(struct sbmacdma *d, struct sbmac_softc *s, int chan,
+                         int txrx, int maxdescr);
+static void sbdma_channel_start(struct sbmacdma *d, int rxtx);
+static int sbdma_add_rcvbuffer(struct sbmacdma *d, struct sk_buff *m);
+static int sbdma_add_txbuffer(struct sbmacdma *d, struct sk_buff *m);
+static void sbdma_emptyring(struct sbmacdma *d);
+static void sbdma_fillring(struct sbmacdma *d);
+static int sbdma_rx_process(struct sbmac_softc *sc, struct sbmacdma *d,
+                           int work_to_do, int poll);
+static void sbdma_tx_process(struct sbmac_softc *sc, struct sbmacdma *d,
+                            int poll);
 static int sbmac_initctx(struct sbmac_softc *s);
 static void sbmac_channel_start(struct sbmac_softc *s);
 static void sbmac_channel_stop(struct sbmac_softc *s);
-static sbmac_state_t sbmac_set_channel_state(struct sbmac_softc *,sbmac_state_t);
-static void sbmac_promiscuous_mode(struct sbmac_softc *sc,int onoff);
+static enum sbmac_state sbmac_set_channel_state(struct sbmac_softc *,
+                                               enum sbmac_state);
+static void sbmac_promiscuous_mode(struct sbmac_softc *sc, int onoff);
 static uint64_t sbmac_addr2reg(unsigned char *ptr);
-static irqreturn_t sbmac_intr(int irq,void *dev_instance);
+static irqreturn_t sbmac_intr(int irq, void *dev_instance);
 static int sbmac_start_tx(struct sk_buff *skb, struct net_device *dev);
 static void sbmac_setmulti(struct sbmac_softc *sc);
 static int sbmac_init(struct net_device *dev, int idx);
-static int sbmac_set_speed(struct sbmac_softc *s,sbmac_speed_t speed);
-static int sbmac_set_duplex(struct sbmac_softc *s,sbmac_duplex_t duplex,sbmac_fc_t fc);
+static int sbmac_set_speed(struct sbmac_softc *s, enum sbmac_speed speed);
+static int sbmac_set_duplex(struct sbmac_softc *s, enum sbmac_duplex duplex,
+                           enum sbmac_fc fc);
 
 static int sbmac_open(struct net_device *dev);
 static void sbmac_timer(unsigned long data);
 static void sbmac_tx_timeout (struct net_device *dev);
-static struct net_device_stats *sbmac_get_stats(struct net_device *dev);
 static void sbmac_set_rx_mode(struct net_device *dev);
 static int sbmac_mii_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
 static int sbmac_close(struct net_device *dev);
-static int sbmac_mii_poll(struct sbmac_softc *s,int noisy);
+static int sbmac_poll(struct napi_struct *napi, int budget);
+
+static int sbmac_mii_poll(struct sbmac_softc *s, int noisy);
 static int sbmac_mii_probe(struct net_device *dev);
 
 static void sbmac_mii_sync(struct sbmac_softc *s);
-static void sbmac_mii_senddata(struct sbmac_softc *s,unsigned int data, int bitcnt);
-static unsigned int sbmac_mii_read(struct sbmac_softc *s,int phyaddr,int regidx);
-static void sbmac_mii_write(struct sbmac_softc *s,int phyaddr,int regidx,
+static void sbmac_mii_senddata(struct sbmac_softc *s, unsigned int data,
+                              int bitcnt);
+static unsigned int sbmac_mii_read(struct sbmac_softc *s, int phyaddr,
+                                  int regidx);
+static void sbmac_mii_write(struct sbmac_softc *s, int phyaddr, int regidx,
                            unsigned int regval);
 
 
@@ -663,8 +683,8 @@ static void sbmac_mii_write(struct sbmac_softc *s,int phyaddr,int regidx,
  *  way.
  *
  *  Input parameters:
- *        d - sbmacdma_t structure (DMA channel context)
- *        s - sbmac_softc structure (pointer to a MAC)
+ *        d - struct sbmacdma (DMA channel context)
+ *        s - struct sbmac_softc (pointer to a MAC)
  *        chan - channel number (0..1 right now)
  *        txrx - Identifies DMA_TX or DMA_RX for channel direction
  *      maxdescr - number of descriptors
@@ -673,12 +693,13 @@ static void sbmac_mii_write(struct sbmac_softc *s,int phyaddr,int regidx,
  *        nothing
  ********************************************************************* */
 
-static void sbdma_initctx(sbmacdma_t *d,
-                         struct sbmac_softc *s,
-                         int chan,
-                         int txrx,
-                         int maxdescr)
+static void sbdma_initctx(struct sbmacdma *d, struct sbmac_softc *s, int chan,
+                         int txrx, int maxdescr)
 {
+#ifdef CONFIG_SBMAC_COALESCE
+       int int_pktcnt, int_timeout;
+#endif
+
        /*
         * Save away interesting stuff in the structure
         */
@@ -728,6 +749,11 @@ static void sbdma_initctx(sbmacdma_t *d,
                s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_DSCR_CNT);
        d->sbdma_curdscr =
                s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_CUR_DSCRADDR);
+       if (d->sbdma_txdir)
+               d->sbdma_oodpktlost = NULL;
+       else
+               d->sbdma_oodpktlost =
+                       s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_OODPKTLOST_RX);
 
        /*
         * Allocate memory for the ring
@@ -735,17 +761,17 @@ static void sbdma_initctx(sbmacdma_t *d,
 
        d->sbdma_maxdescr = maxdescr;
 
-       d->sbdma_dscrtable = (sbdmadscr_t *)
-               kmalloc((d->sbdma_maxdescr+1)*sizeof(sbdmadscr_t), GFP_KERNEL);
+       d->sbdma_dscrtable_unaligned = kcalloc(d->sbdma_maxdescr + 1,
+                                              sizeof(*d->sbdma_dscrtable),
+                                              GFP_KERNEL);
 
        /*
         * The descriptor table must be aligned to at least 16 bytes or the
         * MAC will corrupt it.
         */
-       d->sbdma_dscrtable = (sbdmadscr_t *)
-               ALIGN((unsigned long)d->sbdma_dscrtable, sizeof(sbdmadscr_t));
-
-       memset(d->sbdma_dscrtable,0,d->sbdma_maxdescr*sizeof(sbdmadscr_t));
+       d->sbdma_dscrtable = (struct sbdmadscr *)
+                            ALIGN((unsigned long)d->sbdma_dscrtable_unaligned,
+                                  sizeof(*d->sbdma_dscrtable));
 
        d->sbdma_dscrtable_end = d->sbdma_dscrtable + d->sbdma_maxdescr;
 
@@ -755,22 +781,22 @@ static void sbdma_initctx(sbmacdma_t *d,
         * And context table
         */
 
-       d->sbdma_ctxtable = (struct sk_buff **)
-               kmalloc(d->sbdma_maxdescr*sizeof(struct sk_buff *), GFP_KERNEL);
-
-       memset(d->sbdma_ctxtable,0,d->sbdma_maxdescr*sizeof(struct sk_buff *));
+       d->sbdma_ctxtable = kcalloc(d->sbdma_maxdescr,
+                                   sizeof(*d->sbdma_ctxtable), GFP_KERNEL);
 
 #ifdef CONFIG_SBMAC_COALESCE
        /*
         * Setup Rx/Tx DMA coalescing defaults
         */
 
+       int_pktcnt = (txrx == DMA_TX) ? int_pktcnt_tx : int_pktcnt_rx;
        if ( int_pktcnt ) {
                d->sbdma_int_pktcnt = int_pktcnt;
        } else {
                d->sbdma_int_pktcnt = 1;
        }
 
+       int_timeout = (txrx == DMA_TX) ? int_timeout_tx : int_timeout_rx;
        if ( int_timeout ) {
                d->sbdma_int_timeout = int_timeout;
        } else {
@@ -793,7 +819,7 @@ static void sbdma_initctx(sbmacdma_t *d,
  *        nothing
  ********************************************************************* */
 
-static void sbdma_channel_start(sbmacdma_t *d, int rxtx )
+static void sbdma_channel_start(struct sbmacdma *d, int rxtx)
 {
        /*
         * Turn on the DMA channel
@@ -834,7 +860,7 @@ static void sbdma_channel_start(sbmacdma_t *d, int rxtx )
  *        nothing
  ********************************************************************* */
 
-static void sbdma_channel_stop(sbmacdma_t *d)
+static void sbdma_channel_stop(struct sbmacdma *d)
 {
        /*
         * Turn off the DMA channel
@@ -883,10 +909,10 @@ static void sbdma_align_skb(struct sk_buff *skb,int power2,int offset)
  ********************************************************************* */
 
 
-static int sbdma_add_rcvbuffer(sbmacdma_t *d,struct sk_buff *sb)
+static int sbdma_add_rcvbuffer(struct sbmacdma *d, struct sk_buff *sb)
 {
-       sbdmadscr_t *dsc;
-       sbdmadscr_t *nextdsc;
+       struct sbdmadscr *dsc;
+       struct sbdmadscr *nextdsc;
        struct sk_buff *sb_new = NULL;
        int pktsize = ENET_PACKET_SIZE;
 
@@ -998,10 +1024,10 @@ static int sbdma_add_rcvbuffer(sbmacdma_t *d,struct sk_buff *sb)
  ********************************************************************* */
 
 
-static int sbdma_add_txbuffer(sbmacdma_t *d,struct sk_buff *sb)
+static int sbdma_add_txbuffer(struct sbmacdma *d, struct sk_buff *sb)
 {
-       sbdmadscr_t *dsc;
-       sbdmadscr_t *nextdsc;
+       struct sbdmadscr *dsc;
+       struct sbdmadscr *nextdsc;
        uint64_t phys;
        uint64_t ncb;
        int length;
@@ -1087,7 +1113,7 @@ static int sbdma_add_txbuffer(sbmacdma_t *d,struct sk_buff *sb)
  *        nothing
  ********************************************************************* */
 
-static void sbdma_emptyring(sbmacdma_t *d)
+static void sbdma_emptyring(struct sbmacdma *d)
 {
        int idx;
        struct sk_buff *sb;
@@ -1115,7 +1141,7 @@ static void sbdma_emptyring(sbmacdma_t *d)
  *        nothing
  ********************************************************************* */
 
-static void sbdma_fillring(sbmacdma_t *d)
+static void sbdma_fillring(struct sbmacdma *d)
 {
        int idx;
 
@@ -1125,32 +1151,64 @@ static void sbdma_fillring(sbmacdma_t *d)
        }
 }
 
+#ifdef CONFIG_NET_POLL_CONTROLLER
+static void sbmac_netpoll(struct net_device *netdev)
+{
+       struct sbmac_softc *sc = netdev_priv(netdev);
+       int irq = sc->sbm_dev->irq;
+
+       __raw_writeq(0, sc->sbm_imr);
+
+       sbmac_intr(irq, netdev);
+
+#ifdef CONFIG_SBMAC_COALESCE
+       __raw_writeq(((M_MAC_INT_EOP_COUNT | M_MAC_INT_EOP_TIMER) << S_MAC_TX_CH0) |
+       ((M_MAC_INT_EOP_COUNT | M_MAC_INT_EOP_TIMER) << S_MAC_RX_CH0),
+       sc->sbm_imr);
+#else
+       __raw_writeq((M_MAC_INT_CHANNEL << S_MAC_TX_CH0) | 
+       (M_MAC_INT_CHANNEL << S_MAC_RX_CH0), sc->sbm_imr);
+#endif
+}
+#endif
 
 /**********************************************************************
- *  SBDMA_RX_PROCESS(sc,d)
+ *  SBDMA_RX_PROCESS(sc,d,work_to_do,poll)
  *
  *  Process "completed" receive buffers on the specified DMA channel.
- *  Note that this isn't really ideal for priority channels, since
- *  it processes all of the packets on a given channel before
- *  returning.
  *
  *  Input parameters:
- *        sc - softc structure
- *        d - DMA channel context
+ *            sc - softc structure
+ *            d - DMA channel context
+ *    work_to_do - no. of packets to process before enabling interrupt
+ *                 again (for NAPI)
+ *          poll - 1: using polling (for NAPI)
  *
  *  Return value:
  *        nothing
  ********************************************************************* */
 
-static void sbdma_rx_process(struct sbmac_softc *sc,sbmacdma_t *d)
+static int sbdma_rx_process(struct sbmac_softc *sc, struct sbmacdma *d,
+                           int work_to_do, int poll)
 {
+       struct net_device *dev = sc->sbm_dev;
        int curidx;
        int hwidx;
-       sbdmadscr_t *dsc;
+       struct sbdmadscr *dsc;
        struct sk_buff *sb;
        int len;
+       int work_done = 0;
+       int dropped = 0;
 
-       for (;;) {
+       prefetch(d);
+
+again:
+       /* Check if the HW dropped any frames */
+       dev->stats.rx_fifo_errors
+           += __raw_readq(sc->sbm_rxdma.sbdma_oodpktlost) & 0xffff;
+       __raw_writeq(0, sc->sbm_rxdma.sbdma_oodpktlost);
+
+       while (work_to_do-- > 0) {
                /*
                 * figure out where we are (as an index) and where
                 * the hardware is (also as an index)
@@ -1162,9 +1220,15 @@ static void sbdma_rx_process(struct sbmac_softc *sc,sbmacdma_t *d)
                 * (sbdma_remptr) and the physical address (sbdma_curdscr CSR)
                 */
 
-               curidx = d->sbdma_remptr - d->sbdma_dscrtable;
-               hwidx = (int) (((__raw_readq(d->sbdma_curdscr) & M_DMA_CURDSCR_ADDR) -
-                               d->sbdma_dscrtable_phys) / sizeof(sbdmadscr_t));
+               dsc = d->sbdma_remptr;
+               curidx = dsc - d->sbdma_dscrtable;
+
+               prefetch(dsc);
+               prefetch(&d->sbdma_ctxtable[curidx]);
+
+               hwidx = ((__raw_readq(d->sbdma_curdscr) & M_DMA_CURDSCR_ADDR) -
+                        d->sbdma_dscrtable_phys) /
+                       sizeof(*d->sbdma_dscrtable);
 
                /*
                 * If they're the same, that means we've processed all
@@ -1173,13 +1237,12 @@ static void sbdma_rx_process(struct sbmac_softc *sc,sbmacdma_t *d)
                 */
 
                if (curidx == hwidx)
-                       break;
+                       goto done;
 
                /*
                 * Otherwise, get the packet's sk_buff ptr back
                 */
 
-               dsc = &(d->sbdma_dscrtable[curidx]);
                sb = d->sbdma_ctxtable[curidx];
                d->sbdma_ctxtable[curidx] = NULL;
 
@@ -1191,7 +1254,7 @@ static void sbdma_rx_process(struct sbmac_softc *sc,sbmacdma_t *d)
                 * receive ring.
                 */
 
-               if (!(dsc->dscr_a & M_DMA_ETHRX_BAD)) {
+               if (likely (!(dsc->dscr_a & M_DMA_ETHRX_BAD))) {
 
                        /*
                         * Add a new buffer to replace the old one.  If we fail
@@ -1199,9 +1262,14 @@ static void sbdma_rx_process(struct sbmac_softc *sc,sbmacdma_t *d)
                         * packet and put it right back on the receive ring.
                         */
 
-                       if (sbdma_add_rcvbuffer(d,NULL) == -ENOBUFS) {
-                               sc->sbm_stats.rx_dropped++;
+                       if (unlikely (sbdma_add_rcvbuffer(d,NULL) ==
+                                     -ENOBUFS)) {
+                               dev->stats.rx_dropped++;
                                sbdma_add_rcvbuffer(d,sb); /* re-add old buffer */
+                               /* No point in continuing at the moment */
+                               printk(KERN_ERR "dropped packet (1)\n");
+                               d->sbdma_remptr = SBDMA_NEXTBUF(d,sbdma_remptr);
+                               goto done;
                        } else {
                                /*
                                 * Set length into the packet
@@ -1213,8 +1281,6 @@ static void sbdma_rx_process(struct sbmac_softc *sc,sbmacdma_t *d)
                                 * receive ring.  Pass the buffer to
                                 * the kernel
                                 */
-                               sc->sbm_stats.rx_bytes += len;
-                               sc->sbm_stats.rx_packets++;
                                sb->protocol = eth_type_trans(sb,d->sbdma_eth->sbm_dev);
                                /* Check hw IPv4/TCP checksum if supported */
                                if (sc->rx_hw_checksum == ENABLE) {
@@ -1226,15 +1292,29 @@ static void sbdma_rx_process(struct sbmac_softc *sc,sbmacdma_t *d)
                                                sb->ip_summed = CHECKSUM_NONE;
                                        }
                                }
-
-                               netif_rx(sb);
+                               prefetch(sb->data);
+                               prefetch((const void *)(((char *)sb->data)+32));
+                               if (poll)
+                                       dropped = netif_receive_skb(sb);
+                               else
+                                       dropped = netif_rx(sb);
+
+                               if (dropped == NET_RX_DROP) {
+                                       dev->stats.rx_dropped++;
+                                       d->sbdma_remptr = SBDMA_NEXTBUF(d,sbdma_remptr);
+                                       goto done;
+                               }
+                               else {
+                                       dev->stats.rx_bytes += len;
+                                       dev->stats.rx_packets++;
+                               }
                        }
                } else {
                        /*
                         * Packet was mangled somehow.  Just drop it and
                         * put it back on the receive ring.
                         */
-                       sc->sbm_stats.rx_errors++;
+                       dev->stats.rx_errors++;
                        sbdma_add_rcvbuffer(d,sb);
                }
 
@@ -1244,12 +1324,16 @@ static void sbdma_rx_process(struct sbmac_softc *sc,sbmacdma_t *d)
                 */
 
                d->sbdma_remptr = SBDMA_NEXTBUF(d,sbdma_remptr);
-
+               work_done++;
        }
+       if (!poll) {
+               work_to_do = 32;
+               goto again; /* collect fifo drop statistics again */
+       }
+done:
+       return work_done;
 }
 
-
-
 /**********************************************************************
  *  SBDMA_TX_PROCESS(sc,d)
  *
@@ -1261,22 +1345,32 @@ static void sbdma_rx_process(struct sbmac_softc *sc,sbmacdma_t *d)
  *
  *  Input parameters:
  *      sc - softc structure
- *        d - DMA channel context
+ *      d - DMA channel context
+ *    poll - 1: using polling (for NAPI)
  *
  *  Return value:
  *        nothing
  ********************************************************************* */
 
-static void sbdma_tx_process(struct sbmac_softc *sc,sbmacdma_t *d)
+static void sbdma_tx_process(struct sbmac_softc *sc, struct sbmacdma *d,
+                            int poll)
 {
+       struct net_device *dev = sc->sbm_dev;
        int curidx;
        int hwidx;
-       sbdmadscr_t *dsc;
+       struct sbdmadscr *dsc;
        struct sk_buff *sb;
        unsigned long flags;
+       int packets_handled = 0;
 
        spin_lock_irqsave(&(sc->sbm_lock), flags);
 
+       if (d->sbdma_remptr == d->sbdma_addptr)
+         goto end_unlock;
+
+       hwidx = ((__raw_readq(d->sbdma_curdscr) & M_DMA_CURDSCR_ADDR) -
+                d->sbdma_dscrtable_phys) / sizeof(*d->sbdma_dscrtable);
+
        for (;;) {
                /*
                 * figure out where we are (as an index) and where
@@ -1290,8 +1384,6 @@ static void sbdma_tx_process(struct sbmac_softc *sc,sbmacdma_t *d)
                 */
 
                curidx = d->sbdma_remptr - d->sbdma_dscrtable;
-               hwidx = (int) (((__raw_readq(d->sbdma_curdscr) & M_DMA_CURDSCR_ADDR) -
-                               d->sbdma_dscrtable_phys) / sizeof(sbdmadscr_t));
 
                /*
                 * If they're the same, that means we've processed all
@@ -1314,8 +1406,8 @@ static void sbdma_tx_process(struct sbmac_softc *sc,sbmacdma_t *d)
                 * Stats
                 */
 
-               sc->sbm_stats.tx_bytes += sb->len;
-               sc->sbm_stats.tx_packets++;
+               dev->stats.tx_bytes += sb->len;
+               dev->stats.tx_packets++;
 
                /*
                 * for transmits, we just free buffers.
@@ -1329,6 +1421,8 @@ static void sbdma_tx_process(struct sbmac_softc *sc,sbmacdma_t *d)
 
                d->sbdma_remptr = SBDMA_NEXTBUF(d,sbdma_remptr);
 
+               packets_handled++;
+
        }
 
        /*
@@ -1337,8 +1431,10 @@ static void sbdma_tx_process(struct sbmac_softc *sc,sbmacdma_t *d)
         * watermark on the transmit queue.
         */
 
-       netif_wake_queue(d->sbdma_eth->sbm_dev);
+       if (packets_handled)
+               netif_wake_queue(d->sbdma_eth->sbm_dev);
 
+end_unlock:
        spin_unlock_irqrestore(&(sc->sbm_lock), flags);
 
 }
@@ -1410,11 +1506,11 @@ static int sbmac_initctx(struct sbmac_softc *s)
 }
 
 
-static void sbdma_uninitctx(struct sbmacdma_s *d)
+static void sbdma_uninitctx(struct sbmacdma *d)
 {
-       if (d->sbdma_dscrtable) {
-               kfree(d->sbdma_dscrtable);
-               d->sbdma_dscrtable = NULL;
+       if (d->sbdma_dscrtable_unaligned) {
+               kfree(d->sbdma_dscrtable_unaligned);
+               d->sbdma_dscrtable_unaligned = d->sbdma_dscrtable = NULL;
        }
 
        if (d->sbdma_ctxtable) {
@@ -1446,7 +1542,7 @@ static void sbmac_uninitctx(struct sbmac_softc *sc)
 static void sbmac_channel_start(struct sbmac_softc *s)
 {
        uint64_t reg;
-       volatile void __iomem *port;
+       void __iomem *port;
        uint64_t cfg,fifo,framecfg;
        int idx, th_value;
 
@@ -1612,15 +1708,9 @@ static void sbmac_channel_start(struct sbmac_softc *s)
 #endif
 
 #ifdef CONFIG_SBMAC_COALESCE
-       /*
-        * Accept any TX interrupt and EOP count/timer RX interrupts on ch 0
-        */
        __raw_writeq(((M_MAC_INT_EOP_COUNT | M_MAC_INT_EOP_TIMER) << S_MAC_TX_CH0) |
                       ((M_MAC_INT_EOP_COUNT | M_MAC_INT_EOP_TIMER) << S_MAC_RX_CH0), s->sbm_imr);
 #else
-       /*
-        * Accept any kind of interrupt on TX and RX DMA channel 0
-        */
        __raw_writeq((M_MAC_INT_CHANNEL << S_MAC_TX_CH0) |
                       (M_MAC_INT_CHANNEL << S_MAC_RX_CH0), s->sbm_imr);
 #endif
@@ -1715,10 +1805,10 @@ static void sbmac_channel_stop(struct sbmac_softc *s)
  *  Return value:
  *        old state
  ********************************************************************* */
-static sbmac_state_t sbmac_set_channel_state(struct sbmac_softc *sc,
-                                            sbmac_state_t state)
+static enum sbmac_state sbmac_set_channel_state(struct sbmac_softc *sc,
+                                               enum sbmac_state state)
 {
-       sbmac_state_t oldstate = sc->sbm_state;
+       enum sbmac_state oldstate = sc->sbm_state;
 
        /*
         * If same as previous state, return
@@ -1853,14 +1943,14 @@ static uint64_t sbmac_addr2reg(unsigned char *ptr)
  *
  *  Input parameters:
  *        s - sbmac structure
- *        speed - speed to set MAC to (see sbmac_speed_t enum)
+ *        speed - speed to set MAC to (see enum sbmac_speed)
  *
  *  Return value:
  *        1 if successful
  *      0 indicates invalid parameters
  ********************************************************************* */
 
-static int sbmac_set_speed(struct sbmac_softc *s,sbmac_speed_t speed)
+static int sbmac_set_speed(struct sbmac_softc *s, enum sbmac_speed speed)
 {
        uint64_t cfg;
        uint64_t framecfg;
@@ -1942,15 +2032,16 @@ static int sbmac_set_speed(struct sbmac_softc *s,sbmac_speed_t speed)
  *
  *  Input parameters:
  *        s - sbmac structure
- *        duplex - duplex setting (see sbmac_duplex_t)
- *        fc - flow control setting (see sbmac_fc_t)
+ *        duplex - duplex setting (see enum sbmac_duplex)
+ *        fc - flow control setting (see enum sbmac_fc)
  *
  *  Return value:
  *        1 if ok
  *        0 if an invalid parameter combination was specified
  ********************************************************************* */
 
-static int sbmac_set_duplex(struct sbmac_softc *s,sbmac_duplex_t duplex,sbmac_fc_t fc)
+static int sbmac_set_duplex(struct sbmac_softc *s, enum sbmac_duplex duplex,
+                           enum sbmac_fc fc)
 {
        uint64_t cfg;
 
@@ -2053,57 +2144,39 @@ static irqreturn_t sbmac_intr(int irq,void *dev_instance)
        uint64_t isr;
        int handled = 0;
 
-       for (;;) {
-
-               /*
-                * Read the ISR (this clears the bits in the real
-                * register, except for counter addr)
-                */
+       /*
+        * Read the ISR (this clears the bits in the real
+        * register, except for counter addr)
+        */
 
-               isr = __raw_readq(sc->sbm_isr) & ~M_MAC_COUNTER_ADDR;
+       isr = __raw_readq(sc->sbm_isr) & ~M_MAC_COUNTER_ADDR;
 
-               if (isr == 0)
-                       break;
+       if (isr == 0)
+               return IRQ_RETVAL(0);
+       handled = 1;
 
-               handled = 1;
+       /*
+        * Transmits on channel 0
+        */
 
-               /*
-                * Transmits on channel 0
-                */
+       if (isr & (M_MAC_INT_CHANNEL << S_MAC_TX_CH0))
+               sbdma_tx_process(sc,&(sc->sbm_txdma), 0);
 
-               if (isr & (M_MAC_INT_CHANNEL << S_MAC_TX_CH0)) {
-                       sbdma_tx_process(sc,&(sc->sbm_txdma));
+       if (isr & (M_MAC_INT_CHANNEL << S_MAC_RX_CH0)) {
+               if (netif_rx_schedule_prep(dev, &sc->napi)) {
+                       __raw_writeq(0, sc->sbm_imr);
+                       __netif_rx_schedule(dev, &sc->napi);
+                       /* Depend on the exit from poll to reenable intr */
                }
-
-               /*
-                * Receives on channel 0
-                */
-
-               /*
-                * It's important to test all the bits (or at least the
-                * EOP_SEEN bit) when deciding to do the RX process
-                * particularly when coalescing, to make sure we
-                * take care of the following:
-                *
-                * If you have some packets waiting (have been received
-                * but no interrupt) and get a TX interrupt before
-                * the RX timer or counter expires, reading the ISR
-                * above will clear the timer and counter, and you
-                * won't get another interrupt until a packet shows
-                * up to start the timer again.  Testing
-                * EOP_SEEN here takes care of this case.
-                * (EOP_SEEN is part of M_MAC_INT_CHANNEL << S_MAC_RX_CH0)
-                */
-
-
-               if (isr & (M_MAC_INT_CHANNEL << S_MAC_RX_CH0)) {
-                       sbdma_rx_process(sc,&(sc->sbm_rxdma));
+               else {
+                       /* may leave some packets behind */
+                       sbdma_rx_process(sc,&(sc->sbm_rxdma),
+                                        SBMAC_MAX_RXDESCR * 2, 0);
                }
        }
        return IRQ_RETVAL(handled);
 }
 
-
 /**********************************************************************
  *  SBMAC_START_TX(skb,dev)
  *
@@ -2161,7 +2234,7 @@ static int sbmac_start_tx(struct sk_buff *skb, struct net_device *dev)
 static void sbmac_setmulti(struct sbmac_softc *sc)
 {
        uint64_t reg;
-       volatile void __iomem *port;
+       void __iomem *port;
        int idx;
        struct dev_mc_list *mclist;
        struct net_device *dev = sc->sbm_dev;
@@ -2233,8 +2306,6 @@ static void sbmac_setmulti(struct sbmac_softc *sc)
        }
 }
 
-
-
 #if defined(SBMAC_ETH0_HWADDR) || defined(SBMAC_ETH1_HWADDR) || defined(SBMAC_ETH2_HWADDR) || defined(SBMAC_ETH3_HWADDR)
 /**********************************************************************
  *  SBMAC_PARSE_XDIGIT(str)
@@ -2342,6 +2413,7 @@ static int sbmac_init(struct net_device *dev, int idx)
        uint64_t ea_reg;
        int i;
        int err;
+       DECLARE_MAC_BUF(mac);
 
        sc = netdev_priv(dev);
 
@@ -2392,13 +2464,17 @@ static int sbmac_init(struct net_device *dev, int idx)
        dev->open               = sbmac_open;
        dev->hard_start_xmit    = sbmac_start_tx;
        dev->stop               = sbmac_close;
-       dev->get_stats          = sbmac_get_stats;
        dev->set_multicast_list = sbmac_set_rx_mode;
        dev->do_ioctl           = sbmac_mii_ioctl;
        dev->tx_timeout         = sbmac_tx_timeout;
        dev->watchdog_timeo     = TX_TIMEOUT;
 
+       netif_napi_add(dev, &sc->napi, sbmac_poll, 16);
+
        dev->change_mtu         = sb1250_change_mtu;
+#ifdef CONFIG_NET_POLL_CONTROLLER
+       dev->poll_controller = sbmac_netpoll;
+#endif
 
        /* This is needed for PASS2 for Rx H/W checksum feature */
        sbmac_set_iphdr_offset(sc);
@@ -2418,10 +2494,8 @@ static int sbmac_init(struct net_device *dev, int idx)
         * was being displayed)
         */
        printk(KERN_INFO
-              "%s: SiByte Ethernet at 0x%08lX, address: %02X:%02X:%02X:%02X:%02X:%02X\n",
-              dev->name, dev->base_addr,
-              eaddr[0],eaddr[1],eaddr[2],eaddr[3],eaddr[4],eaddr[5]);
-
+              "%s: SiByte Ethernet at 0x%08lX, address: %s\n",
+              dev->name, dev->base_addr, print_mac(mac, eaddr));
 
        return 0;
 
@@ -2459,6 +2533,8 @@ static int sbmac_open(struct net_device *dev)
                return -EINVAL;
        }
 
+       napi_enable(&sc->napi);
+
        /*
         * Configure default speed
         */
@@ -2676,7 +2752,7 @@ static void sbmac_tx_timeout (struct net_device *dev)
 
 
        dev->trans_start = jiffies;
-       sc->sbm_stats.tx_errors++;
+       dev->stats.tx_errors++;
 
        spin_unlock_irq (&sc->sbm_lock);
 
@@ -2686,22 +2762,6 @@ static void sbmac_tx_timeout (struct net_device *dev)
 
 
 
-static struct net_device_stats *sbmac_get_stats(struct net_device *dev)
-{
-       struct sbmac_softc *sc = netdev_priv(dev);
-       unsigned long flags;
-
-       spin_lock_irqsave(&sc->sbm_lock, flags);
-
-       /* XXX update other stats here */
-
-       spin_unlock_irqrestore(&sc->sbm_lock, flags);
-
-       return &sc->sbm_stats;
-}
-
-
-
 static void sbmac_set_rx_mode(struct net_device *dev)
 {
        unsigned long flags;
@@ -2772,6 +2832,8 @@ static int sbmac_close(struct net_device *dev)
        unsigned long flags;
        int irq;
 
+       napi_disable(&sc->napi);
+
        sbmac_set_channel_state(sc,sbmac_state_off);
 
        del_timer_sync(&sc->sbm_timer);
@@ -2796,7 +2858,30 @@ static int sbmac_close(struct net_device *dev)
        return 0;
 }
 
+static int sbmac_poll(struct napi_struct *napi, int budget)
+{
+       struct sbmac_softc *sc = container_of(napi, struct sbmac_softc, napi);
+       struct net_device *dev = sc->sbm_dev;
+       int work_done;
+
+       work_done = sbdma_rx_process(sc, &(sc->sbm_rxdma), budget, 1);
+       sbdma_tx_process(sc, &(sc->sbm_txdma), 1);
+
+       if (work_done < budget) {
+               netif_rx_complete(dev, napi);
 
+#ifdef CONFIG_SBMAC_COALESCE
+               __raw_writeq(((M_MAC_INT_EOP_COUNT | M_MAC_INT_EOP_TIMER) << S_MAC_TX_CH0) |
+                            ((M_MAC_INT_EOP_COUNT | M_MAC_INT_EOP_TIMER) << S_MAC_RX_CH0),
+                            sc->sbm_imr);
+#else
+               __raw_writeq((M_MAC_INT_CHANNEL << S_MAC_TX_CH0) |
+                            (M_MAC_INT_CHANNEL << S_MAC_RX_CH0), sc->sbm_imr);
+#endif
+       }
+
+       return work_done;
+}
 
 #if defined(SBMAC_ETH0_HWADDR) || defined(SBMAC_ETH1_HWADDR) || defined(SBMAC_ETH2_HWADDR) || defined(SBMAC_ETH3_HWADDR)
 static void
@@ -2883,7 +2968,7 @@ sbmac_init_module(void)
 
                /*
                 * The R_MAC_ETHERNET_ADDR register will be set to some nonzero
-                * value for us by the firmware if we're going to use this MAC.
+                * value for us by the firmware if we are going to use this MAC.
                 * If we find a zero, skip this MAC.
                 */