Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/linville/wirel...
[linux-2.6.git] / drivers / net / sb1250-mac.c
index e4c8896..564d4d7 100644 (file)
@@ -1,5 +1,6 @@
 /*
  * Copyright (C) 2001,2002,2003,2004 Broadcom Corporation
+ * Copyright (c) 2006, 2007  Maciej W. Rozycki
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
  *
  * This driver is designed for the Broadcom SiByte SOC built-in
  * Ethernet controllers. Written by Mitch Lichtenberg at Broadcom Corp.
+ *
+ * Updated to the driver model and the PHY abstraction layer
+ * by Maciej W. Rozycki.
  */
+
+#include <linux/bug.h>
 #include <linux/module.h>
 #include <linux/kernel.h>
 #include <linux/string.h>
 #include <linux/skbuff.h>
 #include <linux/init.h>
 #include <linux/bitops.h>
-#include <asm/processor.h>             /* Processor type for cache alignment. */
-#include <asm/io.h>
+#include <linux/err.h>
+#include <linux/ethtool.h>
+#include <linux/mii.h>
+#include <linux/phy.h>
+#include <linux/platform_device.h>
+
 #include <asm/cache.h>
+#include <asm/io.h>
+#include <asm/processor.h>     /* Processor type for cache alignment. */
 
 /* This is only here until the firmware is ready.  In that case,
    the firmware leaves the ethernet address in the register for us. */
@@ -48,7 +60,7 @@
 
 /* These identify the driver base version and may not be removed. */
 #if 0
-static char version1[] __devinitdata =
+static char version1[] __initdata =
 "sb1250-mac.c:1.00 1/11/2001 Written by Mitch Lichtenberg\n";
 #endif
 
@@ -57,8 +69,6 @@ static char version1[] __devinitdata =
 
 #define CONFIG_SBMAC_COALESCE
 
-#define MAX_UNITS 4            /* More are supported, limit only on options */
-
 /* Time in jiffies before concluding the transmitter is hung. */
 #define TX_TIMEOUT  (2*HZ)
 
@@ -74,40 +84,30 @@ static int debug = 1;
 module_param(debug, int, S_IRUGO);
 MODULE_PARM_DESC(debug, "Debug messages");
 
-/* mii status msgs */
-static int noisy_mii = 1;
-module_param(noisy_mii, int, S_IRUGO);
-MODULE_PARM_DESC(noisy_mii, "MII status messages");
-
-/* Used to pass the media type, etc.
-   Both 'options[]' and 'full_duplex[]' should exist for driver
-   interoperability.
-   The media type is usually passed in 'options[]'.
-*/
-#ifdef MODULE
-static int options[MAX_UNITS] = {-1, -1, -1, -1};
-module_param_array(options, int, NULL, S_IRUGO);
-MODULE_PARM_DESC(options, "1-" __MODULE_STRING(MAX_UNITS));
-
-static int full_duplex[MAX_UNITS] = {-1, -1, -1, -1};
-module_param_array(full_duplex, int, NULL, S_IRUGO);
-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_tx = 255;
+module_param(int_timeout_tx, int, S_IRUGO);
+MODULE_PARM_DESC(int_timeout_tx, "TX timeout value");
 
-static int int_timeout = 0;
-module_param(int_timeout, int, S_IRUGO);
-MODULE_PARM_DESC(int_timeout, "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/board.h>
 #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>
@@ -126,22 +126,43 @@ MODULE_PARM_DESC(int_timeout, "Timeout value");
 #error invalid SiByte MAC configuation
 #endif
 
+#ifdef K_INT_PHY
+#define SBMAC_PHY_INT                  K_INT_PHY
+#else
+#define SBMAC_PHY_INT                  PHY_POLL
+#endif
+
 /**********************************************************************
  *  Simple types
  ********************************************************************* */
 
+enum sbmac_speed {
+       sbmac_speed_none = 0,
+       sbmac_speed_10 = SPEED_10,
+       sbmac_speed_100 = SPEED_100,
+       sbmac_speed_1000 = SPEED_1000,
+};
 
-typedef enum { sbmac_speed_auto, sbmac_speed_10,
-              sbmac_speed_100, sbmac_speed_1000 } sbmac_speed_t;
-
-typedef enum { sbmac_duplex_auto, sbmac_duplex_half,
-              sbmac_duplex_full } sbmac_duplex_t;
+enum sbmac_duplex {
+       sbmac_duplex_none = -1,
+       sbmac_duplex_half = DUPLEX_HALF,
+       sbmac_duplex_full = 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_none,
+       sbmac_fc_disabled,
+       sbmac_fc_frame,
+       sbmac_fc_collision,
+       sbmac_fc_carrier,
+};
 
-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,11 +176,10 @@ 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
+#define ETHER_ADDR_LEN         6
 #define ENET_PACKET_SIZE       1518
 /*#define ENET_PACKET_SIZE     9216 */
 
@@ -167,52 +187,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 +253,41 @@ struct sbmac_softc {
        /*
         * Linux-specific things
         */
-
-       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;
-
-       unsigned char sbm_phys[2];
+       struct net_device       *sbm_dev;       /* pointer to linux device */
+       struct napi_struct      napi;
+       struct phy_device       *phy_dev;       /* the associated PHY device */
+       struct mii_bus          *mii_bus;       /* the MII bus */
+       int                     phy_irq[PHY_MAX_ADDR];
+       spinlock_t              sbm_lock;       /* spin lock */
+       int                     sbm_devflags;   /* current device flags */
 
        /*
         * Controller-specific things
         */
-
-       volatile void __iomem *sbm_base;          /* MAC's base address */
-       sbmac_state_t    sbm_state;         /* current state */
-
-       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 */
-
-       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];
-
-       sbmacdma_t       sbm_txdma;             /* for now, only use channel 0 */
-       sbmacdma_t       sbm_rxdma;
-       int              rx_hw_checksum;
-       int              sbe_idx;
+       void __iomem            *sbm_base;      /* MAC's base address */
+       enum sbmac_state        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 */
+
+       enum sbmac_speed        sbm_speed;      /* current speed */
+       enum sbmac_duplex       sbm_duplex;     /* current duplex */
+       enum sbmac_fc           sbm_fc;         /* cur. flow control setting */
+       int                     sbm_pause;      /* current pause setting */
+       int                     sbm_link;       /* current link state */
+
+       unsigned char           sbm_hwaddr[ETHER_ADDR_LEN];
+
+       struct sbmacdma         sbm_txdma;      /* only channel 0 for now */
+       struct sbmacdma         sbm_rxdma;
+       int                     rx_hw_checksum;
+       int                     sbe_idx;
 };
 
 
@@ -276,53 +299,59 @@ 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 sbmac_softc *sc, 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 sbmac_softc *sc, 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,struct pt_regs *rgs);
+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_init(struct platform_device *pldev, long long base);
+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 void sbmac_mii_poll(struct net_device *dev);
 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,
-                           unsigned int regval);
+static void sbmac_mii_sync(void __iomem *sbm_mdio);
+static void sbmac_mii_senddata(void __iomem *sbm_mdio, unsigned int data,
+                              int bitcnt);
+static int sbmac_mii_read(struct mii_bus *bus, int phyaddr, int regidx);
+static int sbmac_mii_write(struct mii_bus *bus, int phyaddr, int regidx,
+                          u16 val);
 
 
 /**********************************************************************
  *  Globals
  ********************************************************************* */
 
-static uint64_t sbmac_orig_hwaddr[MAX_UNITS];
+static char sbmac_string[] = "sb1250-mac";
+static char sbmac_pretty[] = "SB1250 MAC";
+
+static char sbmac_mdio_string[] = "sb1250-mac-mdio";
 
 
 /**********************************************************************
@@ -334,185 +363,66 @@ static uint64_t sbmac_orig_hwaddr[MAX_UNITS];
 #define        MII_COMMAND_WRITE       0x01
 #define        MII_COMMAND_ACK         0x02
 
-#define BMCR_RESET     0x8000
-#define BMCR_LOOPBACK  0x4000
-#define BMCR_SPEED0    0x2000
-#define BMCR_ANENABLE  0x1000
-#define BMCR_POWERDOWN 0x0800
-#define BMCR_ISOLATE   0x0400
-#define BMCR_RESTARTAN 0x0200
-#define BMCR_DUPLEX    0x0100
-#define BMCR_COLTEST   0x0080
-#define BMCR_SPEED1    0x0040
-#define BMCR_SPEED1000 BMCR_SPEED1
-#define BMCR_SPEED100  BMCR_SPEED0
-#define BMCR_SPEED10   0
-
-#define BMSR_100BT4    0x8000
-#define BMSR_100BT_FDX 0x4000
-#define BMSR_100BT_HDX  0x2000
-#define BMSR_10BT_FDX   0x1000
-#define BMSR_10BT_HDX   0x0800
-#define BMSR_100BT2_FDX 0x0400
-#define BMSR_100BT2_HDX 0x0200
-#define BMSR_1000BT_XSR        0x0100
-#define BMSR_PRESUP    0x0040
-#define BMSR_ANCOMPLT  0x0020
-#define BMSR_REMFAULT  0x0010
-#define BMSR_AUTONEG   0x0008
-#define BMSR_LINKSTAT  0x0004
-#define BMSR_JABDETECT 0x0002
-#define BMSR_EXTCAPAB  0x0001
-
-#define PHYIDR1        0x2000
-#define PHYIDR2                0x5C60
-
-#define ANAR_NP                0x8000
-#define ANAR_RF                0x2000
-#define ANAR_ASYPAUSE  0x0800
-#define ANAR_PAUSE     0x0400
-#define ANAR_T4                0x0200
-#define ANAR_TXFD      0x0100
-#define ANAR_TXHD      0x0080
-#define ANAR_10FD      0x0040
-#define ANAR_10HD      0x0020
-#define ANAR_PSB       0x0001
-
-#define ANLPAR_NP      0x8000
-#define ANLPAR_ACK     0x4000
-#define ANLPAR_RF      0x2000
-#define ANLPAR_ASYPAUSE        0x0800
-#define ANLPAR_PAUSE   0x0400
-#define ANLPAR_T4      0x0200
-#define ANLPAR_TXFD    0x0100
-#define ANLPAR_TXHD    0x0080
-#define ANLPAR_10FD    0x0040
-#define ANLPAR_10HD    0x0020
-#define ANLPAR_PSB     0x0001  /* 802.3 */
-
-#define ANER_PDF       0x0010
-#define ANER_LPNPABLE  0x0008
-#define ANER_NPABLE    0x0004
-#define ANER_PAGERX    0x0002
-#define ANER_LPANABLE  0x0001
-
-#define ANNPTR_NP      0x8000
-#define ANNPTR_MP      0x2000
-#define ANNPTR_ACK2    0x1000
-#define ANNPTR_TOGTX   0x0800
-#define ANNPTR_CODE    0x0008
-
-#define ANNPRR_NP      0x8000
-#define ANNPRR_MP      0x2000
-#define ANNPRR_ACK3    0x1000
-#define ANNPRR_TOGTX   0x0800
-#define ANNPRR_CODE    0x0008
-
-#define K1TCR_TESTMODE 0x0000
-#define K1TCR_MSMCE    0x1000
-#define K1TCR_MSCV     0x0800
-#define K1TCR_RPTR     0x0400
-#define K1TCR_1000BT_FDX 0x200
-#define K1TCR_1000BT_HDX 0x100
-
-#define K1STSR_MSMCFLT 0x8000
-#define K1STSR_MSCFGRES        0x4000
-#define K1STSR_LRSTAT  0x2000
-#define K1STSR_RRSTAT  0x1000
-#define K1STSR_LP1KFD  0x0800
-#define K1STSR_LP1KHD   0x0400
-#define K1STSR_LPASMDIR        0x0200
-
-#define K1SCR_1KX_FDX  0x8000
-#define K1SCR_1KX_HDX  0x4000
-#define K1SCR_1KT_FDX  0x2000
-#define K1SCR_1KT_HDX  0x1000
-
-#define STRAP_PHY1     0x0800
-#define STRAP_NCMODE   0x0400
-#define STRAP_MANMSCFG 0x0200
-#define STRAP_ANENABLE 0x0100
-#define STRAP_MSVAL    0x0080
-#define STRAP_1KHDXADV 0x0010
-#define STRAP_1KFDXADV 0x0008
-#define STRAP_100ADV   0x0004
-#define STRAP_SPEEDSEL 0x0000
-#define STRAP_SPEED100 0x0001
-
-#define PHYSUP_SPEED1000 0x10
-#define PHYSUP_SPEED100  0x08
-#define PHYSUP_SPEED10   0x00
-#define PHYSUP_LINKUP   0x04
-#define PHYSUP_FDX       0x02
-
-#define        MII_BMCR        0x00    /* Basic mode control register (rw) */
-#define        MII_BMSR        0x01    /* Basic mode status register (ro) */
-#define        MII_PHYIDR1     0x02
-#define        MII_PHYIDR2     0x03
-
-#define MII_K1STSR     0x0A    /* 1K Status Register (ro) */
-#define        MII_ANLPAR      0x05    /* Autonegotiation lnk partner abilities (rw) */
-
-
 #define M_MAC_MDIO_DIR_OUTPUT  0               /* for clarity */
 
 #define ENABLE                 1
 #define DISABLE                0
 
 /**********************************************************************
- *  SBMAC_MII_SYNC(s)
+ *  SBMAC_MII_SYNC(sbm_mdio)
  *
  *  Synchronize with the MII - send a pattern of bits to the MII
  *  that will guarantee that it is ready to accept a command.
  *
  *  Input parameters:
- *        s - sbmac structure
+ *        sbm_mdio - address of the MAC's MDIO register
  *
  *  Return value:
  *        nothing
  ********************************************************************* */
 
-static void sbmac_mii_sync(struct sbmac_softc *s)
+static void sbmac_mii_sync(void __iomem *sbm_mdio)
 {
        int cnt;
        uint64_t bits;
        int mac_mdio_genc;
 
-       mac_mdio_genc = __raw_readq(s->sbm_mdio) & M_MAC_GENC;
+       mac_mdio_genc = __raw_readq(sbm_mdio) & M_MAC_GENC;
 
        bits = M_MAC_MDIO_DIR_OUTPUT | M_MAC_MDIO_OUT;
 
-       __raw_writeq(bits | mac_mdio_genc, s->sbm_mdio);
+       __raw_writeq(bits | mac_mdio_genc, sbm_mdio);
 
        for (cnt = 0; cnt < 32; cnt++) {
-               __raw_writeq(bits | M_MAC_MDC | mac_mdio_genc, s->sbm_mdio);
-               __raw_writeq(bits | mac_mdio_genc, s->sbm_mdio);
+               __raw_writeq(bits | M_MAC_MDC | mac_mdio_genc, sbm_mdio);
+               __raw_writeq(bits | mac_mdio_genc, sbm_mdio);
        }
 }
 
 /**********************************************************************
- *  SBMAC_MII_SENDDATA(s,data,bitcnt)
+ *  SBMAC_MII_SENDDATA(sbm_mdio, data, bitcnt)
  *
  *  Send some bits to the MII.  The bits to be sent are right-
  *  justified in the 'data' parameter.
  *
  *  Input parameters:
- *        s - sbmac structure
- *        data - data to send
- *        bitcnt - number of bits to send
+ *        sbm_mdio - address of the MAC's MDIO register
+ *        data     - data to send
+ *        bitcnt   - number of bits to send
  ********************************************************************* */
 
-static void sbmac_mii_senddata(struct sbmac_softc *s,unsigned int data, int bitcnt)
+static void sbmac_mii_senddata(void __iomem *sbm_mdio, unsigned int data,
+                              int bitcnt)
 {
        int i;
        uint64_t bits;
        unsigned int curmask;
        int mac_mdio_genc;
 
-       mac_mdio_genc = __raw_readq(s->sbm_mdio) & M_MAC_GENC;
+       mac_mdio_genc = __raw_readq(sbm_mdio) & M_MAC_GENC;
 
        bits = M_MAC_MDIO_DIR_OUTPUT;
-       __raw_writeq(bits | mac_mdio_genc, s->sbm_mdio);
+       __raw_writeq(bits | mac_mdio_genc, sbm_mdio);
 
        curmask = 1 << (bitcnt - 1);
 
@@ -520,9 +430,9 @@ static void sbmac_mii_senddata(struct sbmac_softc *s,unsigned int data, int bitc
                if (data & curmask)
                        bits |= M_MAC_MDIO_OUT;
                else bits &= ~M_MAC_MDIO_OUT;
-               __raw_writeq(bits | mac_mdio_genc, s->sbm_mdio);
-               __raw_writeq(bits | M_MAC_MDC | mac_mdio_genc, s->sbm_mdio);
-               __raw_writeq(bits | mac_mdio_genc, s->sbm_mdio);
+               __raw_writeq(bits | mac_mdio_genc, sbm_mdio);
+               __raw_writeq(bits | M_MAC_MDC | mac_mdio_genc, sbm_mdio);
+               __raw_writeq(bits | mac_mdio_genc, sbm_mdio);
                curmask >>= 1;
        }
 }
@@ -530,21 +440,22 @@ static void sbmac_mii_senddata(struct sbmac_softc *s,unsigned int data, int bitc
 
 
 /**********************************************************************
- *  SBMAC_MII_READ(s,phyaddr,regidx)
- *
+ *  SBMAC_MII_READ(bus, phyaddr, regidx)
  *  Read a PHY register.
  *
  *  Input parameters:
- *        s - sbmac structure
+ *        bus     - MDIO bus handle
  *        phyaddr - PHY's address
- *        regidx = index of register to read
+ *        regnum  - index of register to read
  *
  *  Return value:
- *        value read, or 0 if an error occurred.
+ *        value read, or 0xffff if an error occurred.
  ********************************************************************* */
 
-static unsigned int sbmac_mii_read(struct sbmac_softc *s,int phyaddr,int regidx)
+static int sbmac_mii_read(struct mii_bus *bus, int phyaddr, int regidx)
 {
+       struct sbmac_softc *sc = (struct sbmac_softc *)bus->priv;
+       void __iomem *sbm_mdio = sc->sbm_mdio;
        int idx;
        int error;
        int regval;
@@ -554,8 +465,7 @@ static unsigned int sbmac_mii_read(struct sbmac_softc *s,int phyaddr,int regidx)
         * Synchronize ourselves so that the PHY knows the next
         * thing coming down is a command
         */
-
-       sbmac_mii_sync(s);
+       sbmac_mii_sync(sbm_mdio);
 
        /*
         * Send the data to the PHY.  The sequence is
@@ -564,37 +474,37 @@ static unsigned int sbmac_mii_read(struct sbmac_softc *s,int phyaddr,int regidx)
         * the PHY addr (5 bits)
         * the register index (5 bits)
         */
+       sbmac_mii_senddata(sbm_mdio, MII_COMMAND_START, 2);
+       sbmac_mii_senddata(sbm_mdio, MII_COMMAND_READ, 2);
+       sbmac_mii_senddata(sbm_mdio, phyaddr, 5);
+       sbmac_mii_senddata(sbm_mdio, regidx, 5);
 
-       sbmac_mii_senddata(s,MII_COMMAND_START, 2);
-       sbmac_mii_senddata(s,MII_COMMAND_READ, 2);
-       sbmac_mii_senddata(s,phyaddr, 5);
-       sbmac_mii_senddata(s,regidx, 5);
-
-       mac_mdio_genc = __raw_readq(s->sbm_mdio) & M_MAC_GENC;
+       mac_mdio_genc = __raw_readq(sbm_mdio) & M_MAC_GENC;
 
        /*
         * Switch the port around without a clock transition.
         */
-       __raw_writeq(M_MAC_MDIO_DIR_INPUT | mac_mdio_genc, s->sbm_mdio);
+       __raw_writeq(M_MAC_MDIO_DIR_INPUT | mac_mdio_genc, sbm_mdio);
 
        /*
         * Send out a clock pulse to signal we want the status
         */
-
-       __raw_writeq(M_MAC_MDIO_DIR_INPUT | M_MAC_MDC | mac_mdio_genc, s->sbm_mdio);
-       __raw_writeq(M_MAC_MDIO_DIR_INPUT | mac_mdio_genc, s->sbm_mdio);
+       __raw_writeq(M_MAC_MDIO_DIR_INPUT | M_MAC_MDC | mac_mdio_genc,
+                    sbm_mdio);
+       __raw_writeq(M_MAC_MDIO_DIR_INPUT | mac_mdio_genc, sbm_mdio);
 
        /*
         * If an error occurred, the PHY will signal '1' back
         */
-       error = __raw_readq(s->sbm_mdio) & M_MAC_MDIO_IN;
+       error = __raw_readq(sbm_mdio) & M_MAC_MDIO_IN;
 
        /*
         * Issue an 'idle' clock pulse, but keep the direction
         * the same.
         */
-       __raw_writeq(M_MAC_MDIO_DIR_INPUT | M_MAC_MDC | mac_mdio_genc, s->sbm_mdio);
-       __raw_writeq(M_MAC_MDIO_DIR_INPUT | mac_mdio_genc, s->sbm_mdio);
+       __raw_writeq(M_MAC_MDIO_DIR_INPUT | M_MAC_MDC | mac_mdio_genc,
+                    sbm_mdio);
+       __raw_writeq(M_MAC_MDIO_DIR_INPUT | mac_mdio_genc, sbm_mdio);
 
        regval = 0;
 
@@ -602,55 +512,60 @@ static unsigned int sbmac_mii_read(struct sbmac_softc *s,int phyaddr,int regidx)
                regval <<= 1;
 
                if (error == 0) {
-                       if (__raw_readq(s->sbm_mdio) & M_MAC_MDIO_IN)
+                       if (__raw_readq(sbm_mdio) & M_MAC_MDIO_IN)
                                regval |= 1;
                }
 
-               __raw_writeq(M_MAC_MDIO_DIR_INPUT|M_MAC_MDC | mac_mdio_genc, s->sbm_mdio);
-               __raw_writeq(M_MAC_MDIO_DIR_INPUT | mac_mdio_genc, s->sbm_mdio);
+               __raw_writeq(M_MAC_MDIO_DIR_INPUT | M_MAC_MDC | mac_mdio_genc,
+                            sbm_mdio);
+               __raw_writeq(M_MAC_MDIO_DIR_INPUT | mac_mdio_genc, sbm_mdio);
        }
 
        /* Switch back to output */
-       __raw_writeq(M_MAC_MDIO_DIR_OUTPUT | mac_mdio_genc, s->sbm_mdio);
+       __raw_writeq(M_MAC_MDIO_DIR_OUTPUT | mac_mdio_genc, sbm_mdio);
 
        if (error == 0)
                return regval;
-       return 0;
+       return 0xffff;
 }
 
 
 /**********************************************************************
- *  SBMAC_MII_WRITE(s,phyaddr,regidx,regval)
+ *  SBMAC_MII_WRITE(bus, phyaddr, regidx, regval)
  *
  *  Write a value to a PHY register.
  *
  *  Input parameters:
- *        s - sbmac structure
+ *        bus     - MDIO bus handle
  *        phyaddr - PHY to use
- *        regidx - register within the PHY
- *        regval - data to write to register
+ *        regidx  - register within the PHY
+ *        regval  - data to write to register
  *
  *  Return value:
- *        nothing
+ *        0 for success
  ********************************************************************* */
 
-static void sbmac_mii_write(struct sbmac_softc *s,int phyaddr,int regidx,
-                           unsigned int regval)
+static int sbmac_mii_write(struct mii_bus *bus, int phyaddr, int regidx,
+                          u16 regval)
 {
+       struct sbmac_softc *sc = (struct sbmac_softc *)bus->priv;
+       void __iomem *sbm_mdio = sc->sbm_mdio;
        int mac_mdio_genc;
 
-       sbmac_mii_sync(s);
+       sbmac_mii_sync(sbm_mdio);
+
+       sbmac_mii_senddata(sbm_mdio, MII_COMMAND_START, 2);
+       sbmac_mii_senddata(sbm_mdio, MII_COMMAND_WRITE, 2);
+       sbmac_mii_senddata(sbm_mdio, phyaddr, 5);
+       sbmac_mii_senddata(sbm_mdio, regidx, 5);
+       sbmac_mii_senddata(sbm_mdio, MII_COMMAND_ACK, 2);
+       sbmac_mii_senddata(sbm_mdio, regval, 16);
 
-       sbmac_mii_senddata(s,MII_COMMAND_START,2);
-       sbmac_mii_senddata(s,MII_COMMAND_WRITE,2);
-       sbmac_mii_senddata(s,phyaddr, 5);
-       sbmac_mii_senddata(s,regidx, 5);
-       sbmac_mii_senddata(s,MII_COMMAND_ACK,2);
-       sbmac_mii_senddata(s,regval,16);
+       mac_mdio_genc = __raw_readq(sbm_mdio) & M_MAC_GENC;
 
-       mac_mdio_genc = __raw_readq(s->sbm_mdio) & M_MAC_GENC;
+       __raw_writeq(M_MAC_MDIO_DIR_OUTPUT | mac_mdio_genc, sbm_mdio);
 
-       __raw_writeq(M_MAC_MDIO_DIR_OUTPUT | mac_mdio_genc, s->sbm_mdio);
+       return 0;
 }
 
 
@@ -663,8 +578,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 +588,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
         */
@@ -692,27 +608,27 @@ static void sbdma_initctx(sbmacdma_t *d,
        s->sbe_idx =(s->sbm_base - A_MAC_BASE_0)/MAC_SPACING;
 #endif
 
-       __raw_writeq(0, IOADDR(A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_TX_BYTES)));
-       __raw_writeq(0, IOADDR(A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_COLLISIONS)));
-       __raw_writeq(0, IOADDR(A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_LATE_COL)));
-       __raw_writeq(0, IOADDR(A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_EX_COL)));
-       __raw_writeq(0, IOADDR(A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_FCS_ERROR)));
-       __raw_writeq(0, IOADDR(A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_TX_ABORT)));
-       __raw_writeq(0, IOADDR(A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_TX_BAD)));
-       __raw_writeq(0, IOADDR(A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_TX_GOOD)));
-       __raw_writeq(0, IOADDR(A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_TX_RUNT)));
-       __raw_writeq(0, IOADDR(A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_TX_OVERSIZE)));
-       __raw_writeq(0, IOADDR(A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_RX_BYTES)));
-       __raw_writeq(0, IOADDR(A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_RX_MCAST)));
-       __raw_writeq(0, IOADDR(A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_RX_BCAST)));
-       __raw_writeq(0, IOADDR(A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_RX_BAD)));
-       __raw_writeq(0, IOADDR(A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_RX_GOOD)));
-       __raw_writeq(0, IOADDR(A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_RX_RUNT)));
-       __raw_writeq(0, IOADDR(A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_RX_OVERSIZE)));
-       __raw_writeq(0, IOADDR(A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_RX_FCS_ERROR)));
-       __raw_writeq(0, IOADDR(A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_RX_LENGTH_ERROR)));
-       __raw_writeq(0, IOADDR(A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_RX_CODE_ERROR)));
-       __raw_writeq(0, IOADDR(A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_RX_ALIGN_ERROR)));
+       __raw_writeq(0, s->sbm_base + R_MAC_RMON_TX_BYTES);
+       __raw_writeq(0, s->sbm_base + R_MAC_RMON_COLLISIONS);
+       __raw_writeq(0, s->sbm_base + R_MAC_RMON_LATE_COL);
+       __raw_writeq(0, s->sbm_base + R_MAC_RMON_EX_COL);
+       __raw_writeq(0, s->sbm_base + R_MAC_RMON_FCS_ERROR);
+       __raw_writeq(0, s->sbm_base + R_MAC_RMON_TX_ABORT);
+       __raw_writeq(0, s->sbm_base + R_MAC_RMON_TX_BAD);
+       __raw_writeq(0, s->sbm_base + R_MAC_RMON_TX_GOOD);
+       __raw_writeq(0, s->sbm_base + R_MAC_RMON_TX_RUNT);
+       __raw_writeq(0, s->sbm_base + R_MAC_RMON_TX_OVERSIZE);
+       __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_BYTES);
+       __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_MCAST);
+       __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_BCAST);
+       __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_BAD);
+       __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_GOOD);
+       __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_RUNT);
+       __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_OVERSIZE);
+       __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_FCS_ERROR);
+       __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_LENGTH_ERROR);
+       __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_CODE_ERROR);
+       __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_ALIGN_ERROR);
 
        /*
         * initialize register pointers
@@ -728,6 +644,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 +656,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 +676,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 +714,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 +755,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
@@ -854,16 +775,13 @@ static void sbdma_channel_stop(sbmacdma_t *d)
        d->sbdma_remptr = NULL;
 }
 
-static void sbdma_align_skb(struct sk_buff *skb,int power2,int offset)
+static inline void sbdma_align_skb(struct sk_buff *skb,
+                                  unsigned int power2, unsigned int offset)
 {
-       unsigned long addr;
-       unsigned long newaddr;
+       unsigned char *addr = skb->data;
+       unsigned char *newaddr = PTR_ALIGN(addr, power2);
 
-       addr = (unsigned long) skb->data;
-
-       newaddr = (addr + power2 - 1) & ~(power2 - 1);
-
-       skb_reserve(skb,newaddr-addr+offset);
+       skb_reserve(skb, newaddr - addr + offset);
 }
 
 
@@ -874,7 +792,8 @@ static void sbdma_align_skb(struct sk_buff *skb,int power2,int offset)
  *  this queues a buffer for inbound packets.
  *
  *  Input parameters:
- *        d - DMA channel descriptor
+ *        sc - softc structure
+ *         d - DMA channel descriptor
  *        sb - sk_buff to add, or NULL if we should allocate one
  *
  *  Return value:
@@ -883,10 +802,12 @@ 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 sbmac_softc *sc, struct sbmacdma *d,
+                              struct sk_buff *sb)
 {
-       sbdmadscr_t *dsc;
-       sbdmadscr_t *nextdsc;
+       struct net_device *dev = sc->sbm_dev;
+       struct sbdmadscr *dsc;
+       struct sbdmadscr *nextdsc;
        struct sk_buff *sb_new = NULL;
        int pktsize = ENET_PACKET_SIZE;
 
@@ -925,17 +846,16 @@ static int sbdma_add_rcvbuffer(sbmacdma_t *d,struct sk_buff *sb)
         */
 
        if (sb == NULL) {
-               sb_new = dev_alloc_skb(ENET_PACKET_SIZE + SMP_CACHE_BYTES * 2 + ETHER_ALIGN);
+               sb_new = netdev_alloc_skb(dev, ENET_PACKET_SIZE +
+                                              SMP_CACHE_BYTES * 2 +
+                                              NET_IP_ALIGN);
                if (sb_new == NULL) {
-                       printk(KERN_INFO "%s: sk_buff allocation failed\n",
+                       pr_info("%s: sk_buff allocation failed\n",
                               d->sbdma_eth->sbm_dev->name);
                        return -ENOBUFS;
                }
 
-               sbdma_align_skb(sb_new, SMP_CACHE_BYTES, ETHER_ALIGN);
-
-               /* mark skbuff owned by our device */
-               sb_new->dev = d->sbdma_eth->sbm_dev;
+               sbdma_align_skb(sb_new, SMP_CACHE_BYTES, NET_IP_ALIGN);
        }
        else {
                sb_new = sb;
@@ -954,10 +874,10 @@ static int sbdma_add_rcvbuffer(sbmacdma_t *d,struct sk_buff *sb)
         * Do not interrupt per DMA transfer.
         */
        dsc->dscr_a = virt_to_phys(sb_new->data) |
-               V_DMA_DSCRA_A_SIZE(NUMCACHEBLKS(pktsize+ETHER_ALIGN)) | 0;
+               V_DMA_DSCRA_A_SIZE(NUMCACHEBLKS(pktsize + NET_IP_ALIGN)) | 0;
 #else
        dsc->dscr_a = virt_to_phys(sb_new->data) |
-               V_DMA_DSCRA_A_SIZE(NUMCACHEBLKS(pktsize+ETHER_ALIGN)) |
+               V_DMA_DSCRA_A_SIZE(NUMCACHEBLKS(pktsize + NET_IP_ALIGN)) |
                M_DMA_DSCRA_INTERRUPT;
 #endif
 
@@ -1001,10 +921,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;
@@ -1090,7 +1010,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;
@@ -1112,48 +1032,81 @@ static void sbdma_emptyring(sbmacdma_t *d)
  *  with sk_buffs
  *
  *  Input parameters:
- *        d - DMA channel
+ *        sc - softc structure
+ *         d - DMA channel
  *
  *  Return value:
  *        nothing
  ********************************************************************* */
 
-static void sbdma_fillring(sbmacdma_t *d)
+static void sbdma_fillring(struct sbmac_softc *sc, struct sbmacdma *d)
 {
        int idx;
 
-       for (idx = 0; idx < SBMAC_MAX_RXDESCR-1; idx++) {
-               if (sbdma_add_rcvbuffer(d,NULL) != 0)
+       for (idx = 0; idx < SBMAC_MAX_RXDESCR - 1; idx++) {
+               if (sbdma_add_rcvbuffer(sc, d, NULL) != 0)
                        break;
        }
 }
 
+#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)
@@ -1165,9 +1118,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
@@ -1176,13 +1135,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;
 
@@ -1194,7 +1152,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
@@ -1202,9 +1160,15 @@ 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++;
-                               sbdma_add_rcvbuffer(d,sb); /* re-add old buffer */
+                       if (unlikely(sbdma_add_rcvbuffer(sc, d, NULL) ==
+                                    -ENOBUFS)) {
+                               dev->stats.rx_dropped++;
+                               /* Re-add old buffer */
+                               sbdma_add_rcvbuffer(sc, d, sb);
+                               /* 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
@@ -1216,8 +1180,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) {
@@ -1229,16 +1191,30 @@ 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++;
-                       sbdma_add_rcvbuffer(d,sb);
+                       dev->stats.rx_errors++;
+                       sbdma_add_rcvbuffer(sc, d, sb);
                }
 
 
@@ -1247,12 +1223,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)
  *
@@ -1264,22 +1244,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
@@ -1293,8 +1283,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
@@ -1317,8 +1305,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.
@@ -1332,6 +1320,8 @@ static void sbdma_tx_process(struct sbmac_softc *sc,sbmacdma_t *d)
 
                d->sbdma_remptr = SBDMA_NEXTBUF(d,sbdma_remptr);
 
+               packets_handled++;
+
        }
 
        /*
@@ -1340,8 +1330,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);
 
 }
@@ -1379,14 +1371,6 @@ static int sbmac_initctx(struct sbmac_softc *s)
        s->sbm_imr       = s->sbm_base + R_MAC_INT_MASK;
        s->sbm_mdio      = s->sbm_base + R_MAC_MDIO;
 
-       s->sbm_phys[0]   = 1;
-       s->sbm_phys[1]   = 0;
-
-       s->sbm_phy_oldbmsr = 0;
-       s->sbm_phy_oldanlpar = 0;
-       s->sbm_phy_oldk1stsr = 0;
-       s->sbm_phy_oldlinkstat = 0;
-
        /*
         * Initialize the DMA channels.  Right now, only one per MAC is used
         * Note: Only do this _once_, as it allocates memory from the kernel!
@@ -1401,23 +1385,15 @@ static int sbmac_initctx(struct sbmac_softc *s)
 
        s->sbm_state = sbmac_state_off;
 
-       /*
-        * Initial speed is (XXX TEMP) 10MBit/s HDX no FC
-        */
-
-       s->sbm_speed = sbmac_speed_10;
-       s->sbm_duplex = sbmac_duplex_half;
-       s->sbm_fc = sbmac_fc_disabled;
-
        return 0;
 }
 
 
-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) {
@@ -1449,7 +1425,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;
 
@@ -1500,7 +1476,6 @@ static void sbmac_channel_start(struct sbmac_softc *s)
                V_MAC_TX_RL_THRSH(4) |
                V_MAC_RX_PL_THRSH(4) |
                V_MAC_RX_RD_THRSH(4) |  /* Must be '4' */
-               V_MAC_RX_PL_THRSH(4) |
                V_MAC_RX_RL_THRSH(8) |
                0;
 
@@ -1596,7 +1571,7 @@ static void sbmac_channel_start(struct sbmac_softc *s)
         * Fill the receive ring
         */
 
-       sbdma_fillring(&(s->sbm_rxdma));
+       sbdma_fillring(s, &(s->sbm_rxdma));
 
        /*
         * Turn on the rest of the bits in the enable register
@@ -1615,15 +1590,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
@@ -1718,10 +1687,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
@@ -1856,14 +1825,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;
@@ -1921,8 +1890,6 @@ static int sbmac_set_speed(struct sbmac_softc *s,sbmac_speed_t speed)
                cfg |= V_MAC_SPEED_SEL_1000MBPS | M_MAC_BURST_EN;
                break;
 
-       case sbmac_speed_auto:          /* XXX not implemented */
-               /* fall through */
        default:
                return 0;
        }
@@ -1945,15 +1912,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;
 
@@ -1995,8 +1963,6 @@ static int sbmac_set_duplex(struct sbmac_softc *s,sbmac_duplex_t duplex,sbmac_fc
                        cfg |= M_MAC_HDX_EN | V_MAC_FC_CMD_ENAB_FALSECARR;
                        break;
 
-               case sbmac_fc_auto:             /* XXX not implemented */
-                       /* fall through */
                case sbmac_fc_frame:            /* not valid in half duplex */
                default:                        /* invalid selection */
                        return 0;
@@ -2015,15 +1981,12 @@ static int sbmac_set_duplex(struct sbmac_softc *s,sbmac_duplex_t duplex,sbmac_fc
 
                case sbmac_fc_collision:        /* not valid in full duplex */
                case sbmac_fc_carrier:          /* not valid in full duplex */
-               case sbmac_fc_auto:             /* XXX not implemented */
-                       /* fall through */
                default:
                        return 0;
                }
                break;
-       case sbmac_duplex_auto:
-               /* XXX not implemented */
-               break;
+       default:
+               return 0;
        }
 
        /*
@@ -2049,64 +2012,46 @@ static int sbmac_set_duplex(struct sbmac_softc *s,sbmac_duplex_t duplex,sbmac_fc
  *  Return value:
  *        nothing
  ********************************************************************* */
-static irqreturn_t sbmac_intr(int irq,void *dev_instance,struct pt_regs *rgs)
+static irqreturn_t sbmac_intr(int irq,void *dev_instance)
 {
        struct net_device *dev = (struct net_device *) dev_instance;
        struct sbmac_softc *sc = netdev_priv(dev);
        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 (napi_schedule_prep(&sc->napi)) {
+                       __raw_writeq(0, sc->sbm_imr);
+                       __napi_schedule(&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)
  *
@@ -2123,9 +2068,10 @@ static irqreturn_t sbmac_intr(int irq,void *dev_instance,struct pt_regs *rgs)
 static int sbmac_start_tx(struct sk_buff *skb, struct net_device *dev)
 {
        struct sbmac_softc *sc = netdev_priv(dev);
+       unsigned long flags;
 
        /* lock eth irq */
-       spin_lock_irq (&sc->sbm_lock);
+       spin_lock_irqsave(&sc->sbm_lock, flags);
 
        /*
         * Put the buffer on the transmit ring.  If we
@@ -2135,16 +2081,16 @@ static int sbmac_start_tx(struct sk_buff *skb, struct net_device *dev)
        if (sbdma_add_txbuffer(&(sc->sbm_txdma),skb)) {
                /* XXX save skb that we could not send */
                netif_stop_queue(dev);
-               spin_unlock_irq(&sc->sbm_lock);
+               spin_unlock_irqrestore(&sc->sbm_lock, flags);
 
-               return 1;
+               return NETDEV_TX_BUSY;
        }
 
        dev->trans_start = jiffies;
 
-       spin_unlock_irq (&sc->sbm_lock);
+       spin_unlock_irqrestore(&sc->sbm_lock, flags);
 
-       return 0;
+       return NETDEV_TX_OK;
 }
 
 /**********************************************************************
@@ -2164,7 +2110,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;
@@ -2236,8 +2182,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)
@@ -2322,10 +2266,25 @@ static int sb1250_change_mtu(struct net_device *_dev, int new_mtu)
        if (new_mtu >  ENET_PACKET_SIZE)
                return -EINVAL;
        _dev->mtu = new_mtu;
-       printk(KERN_INFO "changing the mtu to %d\n", new_mtu);
+       pr_info("changing the mtu to %d\n", new_mtu);
        return 0;
 }
 
+static const struct net_device_ops sbmac_netdev_ops = {
+       .ndo_open               = sbmac_open,
+       .ndo_stop               = sbmac_close,
+       .ndo_start_xmit         = sbmac_start_tx,
+       .ndo_set_multicast_list = sbmac_set_rx_mode,
+       .ndo_tx_timeout         = sbmac_tx_timeout,
+       .ndo_do_ioctl           = sbmac_mii_ioctl,
+       .ndo_change_mtu         = sb1250_change_mtu,
+       .ndo_validate_addr      = eth_validate_addr,
+       .ndo_set_mac_address    = eth_mac_addr,
+#ifdef CONFIG_NET_POLL_CONTROLLER
+       .ndo_poll_controller    = sbmac_netpoll,
+#endif
+};
+
 /**********************************************************************
  *  SBMAC_INIT(dev)
  *
@@ -2338,26 +2297,23 @@ static int sb1250_change_mtu(struct net_device *_dev, int new_mtu)
  *        status
  ********************************************************************* */
 
-static int sbmac_init(struct net_device *dev, int idx)
+static int sbmac_init(struct platform_device *pldev, long long base)
 {
-       struct sbmac_softc *sc;
+       struct net_device *dev = dev_get_drvdata(&pldev->dev);
+       int idx = pldev->id;
+       struct sbmac_softc *sc = netdev_priv(dev);
        unsigned char *eaddr;
        uint64_t ea_reg;
        int i;
        int err;
 
-       sc = netdev_priv(dev);
-
-       /* Determine controller base address */
-
-       sc->sbm_base = IOADDR(dev->base_addr);
        sc->sbm_dev = dev;
        sc->sbe_idx = idx;
 
        eaddr = sc->sbm_hwaddr;
 
        /*
-        * Read the ethernet address.  The firwmare left this programmed
+        * Read the ethernet address.  The firmware left this programmed
         * for us in the ethernet address register for each mac.
         */
 
@@ -2372,13 +2328,6 @@ static int sbmac_init(struct net_device *dev, int idx)
                dev->dev_addr[i] = eaddr[i];
        }
 
-
-       /*
-        * Init packet size
-        */
-
-       sc->sbm_buffersize = ENET_PACKET_SIZE + SMP_CACHE_BYTES * 2 + ETHER_ALIGN;
-
        /*
         * Initialize context (get pointers to registers and stuff), then
         * allocate the memory for the descriptor tables.
@@ -2392,56 +2341,67 @@ static int sbmac_init(struct net_device *dev, int idx)
 
        spin_lock_init(&(sc->sbm_lock));
 
-       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;
+       dev->netdev_ops = &sbmac_netdev_ops;
+       dev->watchdog_timeo = TX_TIMEOUT;
+
+       netif_napi_add(dev, &sc->napi, sbmac_poll, 16);
 
-       dev->change_mtu         = sb1250_change_mtu;
+       dev->irq                = UNIT_INT(idx);
 
        /* This is needed for PASS2 for Rx H/W checksum feature */
        sbmac_set_iphdr_offset(sc);
 
-       err = register_netdev(dev);
-       if (err)
-               goto out_uninit;
+       sc->mii_bus = mdiobus_alloc();
+       if (sc->mii_bus == NULL) {
+               sbmac_uninitctx(sc);
+               return -ENOMEM;
+       }
 
-       if (sc->rx_hw_checksum == ENABLE) {
-               printk(KERN_INFO "%s: enabling TCP rcv checksum\n",
-                       sc->sbm_dev->name);
+       err = register_netdev(dev);
+       if (err) {
+               printk(KERN_ERR "%s.%d: unable to register netdev\n",
+                      sbmac_string, idx);
+               mdiobus_free(sc->mii_bus);
+               sbmac_uninitctx(sc);
+               return err;
        }
 
+       pr_info("%s.%d: registered as %s\n", sbmac_string, idx, dev->name);
+
+       if (sc->rx_hw_checksum == ENABLE)
+               pr_info("%s: enabling TCP rcv checksum\n", dev->name);
+
        /*
         * Display Ethernet address (this is called during the config
         * process so we need to finish off the config message that
         * 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]);
+       pr_info("%s: SiByte Ethernet at 0x%08Lx, address: %pM\n",
+              dev->name, base, eaddr);
 
+       sc->mii_bus->name = sbmac_mdio_string;
+       snprintf(sc->mii_bus->id, MII_BUS_ID_SIZE, "%x", idx);
+       sc->mii_bus->priv = sc;
+       sc->mii_bus->read = sbmac_mii_read;
+       sc->mii_bus->write = sbmac_mii_write;
+       sc->mii_bus->irq = sc->phy_irq;
+       for (i = 0; i < PHY_MAX_ADDR; ++i)
+               sc->mii_bus->irq[i] = SBMAC_PHY_INT;
 
-       return 0;
-
-out_uninit:
-       sbmac_uninitctx(sc);
+       sc->mii_bus->parent = &pldev->dev;
+       dev_set_drvdata(&pldev->dev, sc->mii_bus);
 
-       return err;
+       return 0;
 }
 
 
 static int sbmac_open(struct net_device *dev)
 {
        struct sbmac_softc *sc = netdev_priv(dev);
+       int err;
 
-       if (debug > 1) {
-               printk(KERN_DEBUG "%s: sbmac_open() irq %d.\n", dev->name, dev->irq);
-       }
+       if (debug > 1)
+               pr_debug("%s: sbmac_open() irq %d.\n", dev->name, dev->irq);
 
        /*
         * map/route interrupt (clear status first, in case something
@@ -2450,23 +2410,35 @@ static int sbmac_open(struct net_device *dev)
         */
 
        __raw_readq(sc->sbm_isr);
-       if (request_irq(dev->irq, &sbmac_intr, IRQF_SHARED, dev->name, dev))
-               return -EBUSY;
+       err = request_irq(dev->irq, sbmac_intr, IRQF_SHARED, dev->name, dev);
+       if (err) {
+               printk(KERN_ERR "%s: unable to get IRQ %d\n", dev->name,
+                      dev->irq);
+               goto out_err;
+       }
 
        /*
-        * Probe phy address
+        * Probe PHY address
         */
-
-       if(sbmac_mii_probe(dev) == -1) {
-               printk("%s: failed to probe PHY.\n", dev->name);
-               return -EINVAL;
+       err = mdiobus_register(sc->mii_bus);
+       if (err) {
+               printk(KERN_ERR "%s: unable to register MDIO bus\n",
+                      dev->name);
+               goto out_unirq;
        }
 
+       sc->sbm_speed = sbmac_speed_none;
+       sc->sbm_duplex = sbmac_duplex_none;
+       sc->sbm_fc = sbmac_fc_none;
+       sc->sbm_pause = -1;
+       sc->sbm_link = 0;
+
        /*
-        * Configure default speed
+        * Attach to the PHY
         */
-
-       sbmac_mii_poll(sc,noisy_mii);
+       err = sbmac_mii_probe(dev);
+       if (err)
+               goto out_unregister;
 
        /*
         * Turn on the channel
@@ -2474,214 +2446,148 @@ static int sbmac_open(struct net_device *dev)
 
        sbmac_set_channel_state(sc,sbmac_state_on);
 
-       /*
-        * XXX Station address is in dev->dev_addr
-        */
-
-       if (dev->if_port == 0)
-               dev->if_port = 0;
-
        netif_start_queue(dev);
 
        sbmac_set_rx_mode(dev);
 
-       /* Set the timer to check for link beat. */
-       init_timer(&sc->sbm_timer);
-       sc->sbm_timer.expires = jiffies + 2 * HZ/100;
-       sc->sbm_timer.data = (unsigned long)dev;
-       sc->sbm_timer.function = &sbmac_timer;
-       add_timer(&sc->sbm_timer);
+       phy_start(sc->phy_dev);
+
+       napi_enable(&sc->napi);
 
        return 0;
+
+out_unregister:
+       mdiobus_unregister(sc->mii_bus);
+
+out_unirq:
+       free_irq(dev->irq, dev);
+
+out_err:
+       return err;
 }
 
 static int sbmac_mii_probe(struct net_device *dev)
 {
+       struct sbmac_softc *sc = netdev_priv(dev);
+       struct phy_device *phy_dev;
        int i;
-       struct sbmac_softc *s = netdev_priv(dev);
-       u16 bmsr, id1, id2;
-       u32 vendor, device;
-
-       for (i=1; i<31; i++) {
-       bmsr = sbmac_mii_read(s, i, MII_BMSR);
-               if (bmsr != 0) {
-                       s->sbm_phys[0] = i;
-                       id1 = sbmac_mii_read(s, i, MII_PHYIDR1);
-                       id2 = sbmac_mii_read(s, i, MII_PHYIDR2);
-                       vendor = ((u32)id1 << 6) | ((id2 >> 10) & 0x3f);
-                       device = (id2 >> 4) & 0x3f;
-
-                       printk(KERN_INFO "%s: found phy %d, vendor %06x part %02x\n",
-                               dev->name, i, vendor, device);
-                       return i;
-               }
-       }
-       return -1;
-}
-
-
-static int sbmac_mii_poll(struct sbmac_softc *s,int noisy)
-{
-    int bmsr,bmcr,k1stsr,anlpar;
-    int chg;
-    char buffer[100];
-    char *p = buffer;
 
-    /* Read the mode status and mode control registers. */
-    bmsr = sbmac_mii_read(s,s->sbm_phys[0],MII_BMSR);
-    bmcr = sbmac_mii_read(s,s->sbm_phys[0],MII_BMCR);
-
-    /* get the link partner status */
-    anlpar = sbmac_mii_read(s,s->sbm_phys[0],MII_ANLPAR);
-
-    /* if supported, read the 1000baseT register */
-    if (bmsr & BMSR_1000BT_XSR) {
-       k1stsr = sbmac_mii_read(s,s->sbm_phys[0],MII_K1STSR);
-       }
-    else {
-       k1stsr = 0;
+       for (i = 0; i < PHY_MAX_ADDR; i++) {
+               phy_dev = sc->mii_bus->phy_map[i];
+               if (phy_dev)
+                       break;
        }
-
-    chg = 0;
-
-    if ((bmsr & BMSR_LINKSTAT) == 0) {
-       /*
-        * If link status is down, clear out old info so that when
-        * it comes back up it will force us to reconfigure speed
-        */
-       s->sbm_phy_oldbmsr = 0;
-       s->sbm_phy_oldanlpar = 0;
-       s->sbm_phy_oldk1stsr = 0;
-       return 0;
+       if (!phy_dev) {
+               printk(KERN_ERR "%s: no PHY found\n", dev->name);
+               return -ENXIO;
        }
 
-    if ((s->sbm_phy_oldbmsr != bmsr) ||
-       (s->sbm_phy_oldanlpar != anlpar) ||
-       (s->sbm_phy_oldk1stsr != k1stsr)) {
-       if (debug > 1) {
-           printk(KERN_DEBUG "%s: bmsr:%x/%x anlpar:%x/%x  k1stsr:%x/%x\n",
-              s->sbm_dev->name,
-              s->sbm_phy_oldbmsr,bmsr,
-              s->sbm_phy_oldanlpar,anlpar,
-              s->sbm_phy_oldk1stsr,k1stsr);
-           }
-       s->sbm_phy_oldbmsr = bmsr;
-       s->sbm_phy_oldanlpar = anlpar;
-       s->sbm_phy_oldk1stsr = k1stsr;
-       chg = 1;
+       phy_dev = phy_connect(dev, dev_name(&phy_dev->dev), &sbmac_mii_poll, 0,
+                             PHY_INTERFACE_MODE_GMII);
+       if (IS_ERR(phy_dev)) {
+               printk(KERN_ERR "%s: could not attach to PHY\n", dev->name);
+               return PTR_ERR(phy_dev);
        }
 
-    if (chg == 0)
-           return 0;
-
-    p += sprintf(p,"Link speed: ");
-
-    if (k1stsr & K1STSR_LP1KFD) {
-       s->sbm_speed = sbmac_speed_1000;
-       s->sbm_duplex = sbmac_duplex_full;
-       s->sbm_fc = sbmac_fc_frame;
-       p += sprintf(p,"1000BaseT FDX");
-       }
-    else if (k1stsr & K1STSR_LP1KHD) {
-       s->sbm_speed = sbmac_speed_1000;
-       s->sbm_duplex = sbmac_duplex_half;
-       s->sbm_fc = sbmac_fc_disabled;
-       p += sprintf(p,"1000BaseT HDX");
-       }
-    else if (anlpar & ANLPAR_TXFD) {
-       s->sbm_speed = sbmac_speed_100;
-       s->sbm_duplex = sbmac_duplex_full;
-       s->sbm_fc = (anlpar & ANLPAR_PAUSE) ? sbmac_fc_frame : sbmac_fc_disabled;
-       p += sprintf(p,"100BaseT FDX");
-       }
-    else if (anlpar & ANLPAR_TXHD) {
-       s->sbm_speed = sbmac_speed_100;
-       s->sbm_duplex = sbmac_duplex_half;
-       s->sbm_fc = sbmac_fc_disabled;
-       p += sprintf(p,"100BaseT HDX");
-       }
-    else if (anlpar & ANLPAR_10FD) {
-       s->sbm_speed = sbmac_speed_10;
-       s->sbm_duplex = sbmac_duplex_full;
-       s->sbm_fc = sbmac_fc_frame;
-       p += sprintf(p,"10BaseT FDX");
-       }
-    else if (anlpar & ANLPAR_10HD) {
-       s->sbm_speed = sbmac_speed_10;
-       s->sbm_duplex = sbmac_duplex_half;
-       s->sbm_fc = sbmac_fc_collision;
-       p += sprintf(p,"10BaseT HDX");
-       }
-    else {
-       p += sprintf(p,"Unknown");
-       }
+       /* Remove any features not supported by the controller */
+       phy_dev->supported &= SUPPORTED_10baseT_Half |
+                             SUPPORTED_10baseT_Full |
+                             SUPPORTED_100baseT_Half |
+                             SUPPORTED_100baseT_Full |
+                             SUPPORTED_1000baseT_Half |
+                             SUPPORTED_1000baseT_Full |
+                             SUPPORTED_Autoneg |
+                             SUPPORTED_MII |
+                             SUPPORTED_Pause |
+                             SUPPORTED_Asym_Pause;
+       phy_dev->advertising = phy_dev->supported;
+
+       pr_info("%s: attached PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
+               dev->name, phy_dev->drv->name,
+               dev_name(&phy_dev->dev), phy_dev->irq);
+
+       sc->phy_dev = phy_dev;
 
-    if (noisy) {
-           printk(KERN_INFO "%s: %s\n",s->sbm_dev->name,buffer);
-           }
-
-    return 1;
+       return 0;
 }
 
 
-static void sbmac_timer(unsigned long data)
+static void sbmac_mii_poll(struct net_device *dev)
 {
-       struct net_device *dev = (struct net_device *)data;
        struct sbmac_softc *sc = netdev_priv(dev);
-       int next_tick = HZ;
-       int mii_status;
+       struct phy_device *phy_dev = sc->phy_dev;
+       unsigned long flags;
+       enum sbmac_fc fc;
+       int link_chg, speed_chg, duplex_chg, pause_chg, fc_chg;
+
+       link_chg = (sc->sbm_link != phy_dev->link);
+       speed_chg = (sc->sbm_speed != phy_dev->speed);
+       duplex_chg = (sc->sbm_duplex != phy_dev->duplex);
+       pause_chg = (sc->sbm_pause != phy_dev->pause);
+
+       if (!link_chg && !speed_chg && !duplex_chg && !pause_chg)
+               return;                                 /* Hmmm... */
+
+       if (!phy_dev->link) {
+               if (link_chg) {
+                       sc->sbm_link = phy_dev->link;
+                       sc->sbm_speed = sbmac_speed_none;
+                       sc->sbm_duplex = sbmac_duplex_none;
+                       sc->sbm_fc = sbmac_fc_disabled;
+                       sc->sbm_pause = -1;
+                       pr_info("%s: link unavailable\n", dev->name);
+               }
+               return;
+       }
 
-       spin_lock_irq (&sc->sbm_lock);
+       if (phy_dev->duplex == DUPLEX_FULL) {
+               if (phy_dev->pause)
+                       fc = sbmac_fc_frame;
+               else
+                       fc = sbmac_fc_disabled;
+       } else
+               fc = sbmac_fc_collision;
+       fc_chg = (sc->sbm_fc != fc);
 
-       /* make IFF_RUNNING follow the MII status bit "Link established" */
-       mii_status = sbmac_mii_read(sc, sc->sbm_phys[0], MII_BMSR);
+       pr_info("%s: link available: %dbase-%cD\n", dev->name, phy_dev->speed,
+               phy_dev->duplex == DUPLEX_FULL ? 'F' : 'H');
 
-       if ( (mii_status & BMSR_LINKSTAT) != (sc->sbm_phy_oldlinkstat) ) {
-               sc->sbm_phy_oldlinkstat = mii_status & BMSR_LINKSTAT;
-               if (mii_status & BMSR_LINKSTAT) {
-                       netif_carrier_on(dev);
-               }
-               else {
-                       netif_carrier_off(dev);
-               }
-       }
+       spin_lock_irqsave(&sc->sbm_lock, flags);
 
-       /*
-        * Poll the PHY to see what speed we should be running at
-        */
+       sc->sbm_speed = phy_dev->speed;
+       sc->sbm_duplex = phy_dev->duplex;
+       sc->sbm_fc = fc;
+       sc->sbm_pause = phy_dev->pause;
+       sc->sbm_link = phy_dev->link;
 
-       if (sbmac_mii_poll(sc,noisy_mii)) {
-               if (sc->sbm_state != sbmac_state_off) {
-                       /*
-                        * something changed, restart the channel
-                        */
-                       if (debug > 1) {
-                               printk("%s: restarting channel because speed changed\n",
-                                      sc->sbm_dev->name);
-                       }
-                       sbmac_channel_stop(sc);
-                       sbmac_channel_start(sc);
-               }
+       if ((speed_chg || duplex_chg || fc_chg) &&
+           sc->sbm_state != sbmac_state_off) {
+               /*
+                * something changed, restart the channel
+                */
+               if (debug > 1)
+                       pr_debug("%s: restarting channel "
+                                "because PHY state changed\n", dev->name);
+               sbmac_channel_stop(sc);
+               sbmac_channel_start(sc);
        }
 
-       spin_unlock_irq (&sc->sbm_lock);
-
-       sc->sbm_timer.expires = jiffies + next_tick;
-       add_timer(&sc->sbm_timer);
+       spin_unlock_irqrestore(&sc->sbm_lock, flags);
 }
 
 
 static void sbmac_tx_timeout (struct net_device *dev)
 {
        struct sbmac_softc *sc = netdev_priv(dev);
+       unsigned long flags;
 
-       spin_lock_irq (&sc->sbm_lock);
+       spin_lock_irqsave(&sc->sbm_lock, flags);
 
 
        dev->trans_start = jiffies;
-       sc->sbm_stats.tx_errors++;
+       dev->stats.tx_errors++;
 
-       spin_unlock_irq (&sc->sbm_lock);
+       spin_unlock_irqrestore(&sc->sbm_lock, flags);
 
        printk (KERN_WARNING "%s: Transmit timed out\n",dev->name);
 }
@@ -2689,22 +2595,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;
@@ -2736,62 +2626,34 @@ static void sbmac_set_rx_mode(struct net_device *dev)
 static int sbmac_mii_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
 {
        struct sbmac_softc *sc = netdev_priv(dev);
-       u16 *data = (u16 *)&rq->ifr_ifru;
-       unsigned long flags;
-       int retval;
 
-       spin_lock_irqsave(&sc->sbm_lock, flags);
-       retval = 0;
-
-       switch(cmd) {
-       case SIOCDEVPRIVATE:            /* Get the address of the PHY in use. */
-               data[0] = sc->sbm_phys[0] & 0x1f;
-               /* Fall Through */
-       case SIOCDEVPRIVATE+1:          /* Read the specified MII register. */
-               data[3] = sbmac_mii_read(sc, data[0] & 0x1f, data[1] & 0x1f);
-               break;
-       case SIOCDEVPRIVATE+2:          /* Write the specified MII register */
-               if (!capable(CAP_NET_ADMIN)) {
-                       retval = -EPERM;
-                       break;
-               }
-               if (debug > 1) {
-                   printk(KERN_DEBUG "%s: sbmac_mii_ioctl: write %02X %02X %02X\n",dev->name,
-                      data[0],data[1],data[2]);
-                   }
-               sbmac_mii_write(sc, data[0] & 0x1f, data[1] & 0x1f, data[2]);
-               break;
-       default:
-               retval = -EOPNOTSUPP;
-       }
+       if (!netif_running(dev) || !sc->phy_dev)
+               return -EINVAL;
 
-       spin_unlock_irqrestore(&sc->sbm_lock, flags);
-       return retval;
+       return phy_mii_ioctl(sc->phy_dev, if_mii(rq), cmd);
 }
 
 static int sbmac_close(struct net_device *dev)
 {
        struct sbmac_softc *sc = netdev_priv(dev);
-       unsigned long flags;
-       int irq;
 
-       sbmac_set_channel_state(sc,sbmac_state_off);
+       napi_disable(&sc->napi);
 
-       del_timer_sync(&sc->sbm_timer);
+       phy_stop(sc->phy_dev);
 
-       spin_lock_irqsave(&sc->sbm_lock, flags);
+       sbmac_set_channel_state(sc, sbmac_state_off);
 
        netif_stop_queue(dev);
 
-       if (debug > 1) {
-               printk(KERN_DEBUG "%s: Shutting down ethercard\n",dev->name);
-       }
+       if (debug > 1)
+               pr_debug("%s: Shutting down ethercard\n", dev->name);
 
-       spin_unlock_irqrestore(&sc->sbm_lock, flags);
+       phy_disconnect(sc->phy_dev);
+       sc->phy_dev = NULL;
+
+       mdiobus_unregister(sc->mii_bus);
 
-       irq = dev->irq;
-       synchronize_irq(irq);
-       free_irq(irq, dev);
+       free_irq(dev->irq, dev);
 
        sbdma_emptyring(&(sc->sbm_txdma));
        sbdma_emptyring(&(sc->sbm_rxdma));
@@ -2799,56 +2661,221 @@ 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) {
+               napi_complete(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;
+}
+
+
+static int __devinit sbmac_probe(struct platform_device *pldev)
+{
+       struct net_device *dev;
+       struct sbmac_softc *sc;
+       void __iomem *sbm_base;
+       struct resource *res;
+       u64 sbmac_orig_hwaddr;
+       int err;
+
+       res = platform_get_resource(pldev, IORESOURCE_MEM, 0);
+       BUG_ON(!res);
+       sbm_base = ioremap_nocache(res->start, res->end - res->start + 1);
+       if (!sbm_base) {
+               printk(KERN_ERR "%s: unable to map device registers\n",
+                      dev_name(&pldev->dev));
+               err = -ENOMEM;
+               goto out_out;
+       }
+
+       /*
+        * 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.
+        * If we find a zero, skip this MAC.
+        */
+       sbmac_orig_hwaddr = __raw_readq(sbm_base + R_MAC_ETHERNET_ADDR);
+       pr_debug("%s: %sconfiguring MAC at 0x%08Lx\n", dev_name(&pldev->dev),
+                sbmac_orig_hwaddr ? "" : "not ", (long long)res->start);
+       if (sbmac_orig_hwaddr == 0) {
+               err = 0;
+               goto out_unmap;
+       }
+
+       /*
+        * Okay, cool.  Initialize this MAC.
+        */
+       dev = alloc_etherdev(sizeof(struct sbmac_softc));
+       if (!dev) {
+               printk(KERN_ERR "%s: unable to allocate etherdev\n",
+                      dev_name(&pldev->dev));
+               err = -ENOMEM;
+               goto out_unmap;
+       }
+
+       dev_set_drvdata(&pldev->dev, dev);
+       SET_NETDEV_DEV(dev, &pldev->dev);
+
+       sc = netdev_priv(dev);
+       sc->sbm_base = sbm_base;
+
+       err = sbmac_init(pldev, res->start);
+       if (err)
+               goto out_kfree;
+
+       return 0;
+
+out_kfree:
+       free_netdev(dev);
+       __raw_writeq(sbmac_orig_hwaddr, sbm_base + R_MAC_ETHERNET_ADDR);
+
+out_unmap:
+       iounmap(sbm_base);
+
+out_out:
+       return err;
+}
+
+static int __exit sbmac_remove(struct platform_device *pldev)
+{
+       struct net_device *dev = dev_get_drvdata(&pldev->dev);
+       struct sbmac_softc *sc = netdev_priv(dev);
+
+       unregister_netdev(dev);
+       sbmac_uninitctx(sc);
+       mdiobus_free(sc->mii_bus);
+       iounmap(sc->sbm_base);
+       free_netdev(dev);
+
+       return 0;
+}
+
+
+static struct platform_device **sbmac_pldev;
+static int sbmac_max_units;
 
 #if defined(SBMAC_ETH0_HWADDR) || defined(SBMAC_ETH1_HWADDR) || defined(SBMAC_ETH2_HWADDR) || defined(SBMAC_ETH3_HWADDR)
-static void
-sbmac_setup_hwaddr(int chan,char *addr)
+static void __init sbmac_setup_hwaddr(int idx, char *addr)
 {
+       void __iomem *sbm_base;
+       unsigned long start, end;
        uint8_t eaddr[6];
        uint64_t val;
-       unsigned long port;
 
-       port = A_MAC_CHANNEL_BASE(chan);
-       sbmac_parse_hwaddr(addr,eaddr);
+       if (idx >= sbmac_max_units)
+               return;
+
+       start = A_MAC_CHANNEL_BASE(idx);
+       end = A_MAC_CHANNEL_BASE(idx + 1) - 1;
+
+       sbm_base = ioremap_nocache(start, end - start + 1);
+       if (!sbm_base) {
+               printk(KERN_ERR "%s: unable to map device registers\n",
+                      sbmac_string);
+               return;
+       }
+
+       sbmac_parse_hwaddr(addr, eaddr);
        val = sbmac_addr2reg(eaddr);
-       __raw_writeq(val, IOADDR(port+R_MAC_ETHERNET_ADDR));
-       val = __raw_readq(IOADDR(port+R_MAC_ETHERNET_ADDR));
+       __raw_writeq(val, sbm_base + R_MAC_ETHERNET_ADDR);
+       val = __raw_readq(sbm_base + R_MAC_ETHERNET_ADDR);
+
+       iounmap(sbm_base);
 }
 #endif
 
-static struct net_device *dev_sbmac[MAX_UNITS];
+static int __init sbmac_platform_probe_one(int idx)
+{
+       struct platform_device *pldev;
+       struct {
+               struct resource r;
+               char name[strlen(sbmac_pretty) + 4];
+       } *res;
+       int err;
+
+       res = kzalloc(sizeof(*res), GFP_KERNEL);
+       if (!res) {
+               printk(KERN_ERR "%s.%d: unable to allocate memory\n",
+                      sbmac_string, idx);
+               err = -ENOMEM;
+               goto out_err;
+       }
+
+       /*
+        * This is the base address of the MAC.
+        */
+       snprintf(res->name, sizeof(res->name), "%s %d", sbmac_pretty, idx);
+       res->r.name = res->name;
+       res->r.flags = IORESOURCE_MEM;
+       res->r.start = A_MAC_CHANNEL_BASE(idx);
+       res->r.end = A_MAC_CHANNEL_BASE(idx + 1) - 1;
+
+       pldev = platform_device_register_simple(sbmac_string, idx, &res->r, 1);
+       if (IS_ERR(pldev)) {
+               printk(KERN_ERR "%s.%d: unable to register platform device\n",
+                      sbmac_string, idx);
+               err = PTR_ERR(pldev);
+               goto out_kfree;
+       }
+
+       if (!pldev->dev.driver) {
+               err = 0;                /* No hardware at this address. */
+               goto out_unregister;
+       }
+
+       sbmac_pldev[idx] = pldev;
+       return 0;
+
+out_unregister:
+       platform_device_unregister(pldev);
+
+out_kfree:
+       kfree(res);
+
+out_err:
+       return err;
+}
 
-static int __init
-sbmac_init_module(void)
+static void __init sbmac_platform_probe(void)
 {
-       int idx;
-       struct net_device *dev;
-       unsigned long port;
-       int chip_max_units;
+       int i;
 
        /* Set the number of available units based on the SOC type.  */
        switch (soc_type) {
        case K_SYS_SOC_TYPE_BCM1250:
        case K_SYS_SOC_TYPE_BCM1250_ALT:
-               chip_max_units = 3;
+               sbmac_max_units = 3;
                break;
        case K_SYS_SOC_TYPE_BCM1120:
        case K_SYS_SOC_TYPE_BCM1125:
        case K_SYS_SOC_TYPE_BCM1125H:
-       case K_SYS_SOC_TYPE_BCM1250_ALT2: /* Hybrid */
-               chip_max_units = 2;
+       case K_SYS_SOC_TYPE_BCM1250_ALT2:       /* Hybrid */
+               sbmac_max_units = 2;
                break;
        case K_SYS_SOC_TYPE_BCM1x55:
        case K_SYS_SOC_TYPE_BCM1x80:
-               chip_max_units = 4;
+               sbmac_max_units = 4;
                break;
        default:
-               chip_max_units = 0;
-               break;
+               return;                         /* none */
        }
-       if (chip_max_units > MAX_UNITS)
-               chip_max_units = MAX_UNITS;
 
        /*
         * For bringup when not using the firmware, we can pre-fill
@@ -2856,89 +2883,71 @@ sbmac_init_module(void)
         * specified in this file (or maybe from the config file?)
         */
 #ifdef SBMAC_ETH0_HWADDR
-       if (chip_max_units > 0)
-         sbmac_setup_hwaddr(0,SBMAC_ETH0_HWADDR);
+       sbmac_setup_hwaddr(0, SBMAC_ETH0_HWADDR);
 #endif
 #ifdef SBMAC_ETH1_HWADDR
-       if (chip_max_units > 1)
-         sbmac_setup_hwaddr(1,SBMAC_ETH1_HWADDR);
+       sbmac_setup_hwaddr(1, SBMAC_ETH1_HWADDR);
 #endif
 #ifdef SBMAC_ETH2_HWADDR
-       if (chip_max_units > 2)
-         sbmac_setup_hwaddr(2,SBMAC_ETH2_HWADDR);
+       sbmac_setup_hwaddr(2, SBMAC_ETH2_HWADDR);
 #endif
 #ifdef SBMAC_ETH3_HWADDR
-       if (chip_max_units > 3)
-         sbmac_setup_hwaddr(3,SBMAC_ETH3_HWADDR);
+       sbmac_setup_hwaddr(3, SBMAC_ETH3_HWADDR);
 #endif
 
+       sbmac_pldev = kcalloc(sbmac_max_units, sizeof(*sbmac_pldev),
+                             GFP_KERNEL);
+       if (!sbmac_pldev) {
+               printk(KERN_ERR "%s: unable to allocate memory\n",
+                      sbmac_string);
+               return;
+       }
+
        /*
         * Walk through the Ethernet controllers and find
         * those who have their MAC addresses set.
         */
-       for (idx = 0; idx < chip_max_units; idx++) {
+       for (i = 0; i < sbmac_max_units; i++)
+               if (sbmac_platform_probe_one(i))
+                       break;
+}
 
-               /*
-                * This is the base address of the MAC.
-                */
 
-               port = A_MAC_CHANNEL_BASE(idx);
+static void __exit sbmac_platform_cleanup(void)
+{
+       int i;
+
+       for (i = 0; i < sbmac_max_units; i++)
+               platform_device_unregister(sbmac_pldev[i]);
+       kfree(sbmac_pldev);
+}
 
-               /*
-                * 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.
-                * If we find a zero, skip this MAC.
-                */
 
-               sbmac_orig_hwaddr[idx] = __raw_readq(IOADDR(port+R_MAC_ETHERNET_ADDR));
-               if (sbmac_orig_hwaddr[idx] == 0) {
-                       printk(KERN_DEBUG "sbmac: not configuring MAC at "
-                              "%lx\n", port);
-                   continue;
-               }
+static struct platform_driver sbmac_driver = {
+       .probe = sbmac_probe,
+       .remove = __exit_p(sbmac_remove),
+       .driver = {
+               .name = sbmac_string,
+       },
+};
 
-               /*
-                * Okay, cool.  Initialize this MAC.
-                */
+static int __init sbmac_init_module(void)
+{
+       int err;
 
-               dev = alloc_etherdev(sizeof(struct sbmac_softc));
-               if (!dev)
-                       return -ENOMEM; /* return ENOMEM */
+       err = platform_driver_register(&sbmac_driver);
+       if (err)
+               return err;
 
-               printk(KERN_DEBUG "sbmac: configuring MAC at %lx\n", port);
+       sbmac_platform_probe();
 
-               dev->irq = UNIT_INT(idx);
-               dev->base_addr = port;
-               dev->mem_end = 0;
-               if (sbmac_init(dev, idx)) {
-                       port = A_MAC_CHANNEL_BASE(idx);
-                       __raw_writeq(sbmac_orig_hwaddr[idx], IOADDR(port+R_MAC_ETHERNET_ADDR));
-                       free_netdev(dev);
-                       continue;
-               }
-               dev_sbmac[idx] = dev;
-       }
-       return 0;
+       return err;
 }
 
-
-static void __exit
-sbmac_cleanup_module(void)
+static void __exit sbmac_cleanup_module(void)
 {
-       struct net_device *dev;
-       int idx;
-
-       for (idx = 0; idx < MAX_UNITS; idx++) {
-               struct sbmac_softc *sc;
-               dev = dev_sbmac[idx];
-               if (!dev)
-                       continue;
-
-               sc = netdev_priv(dev);
-               unregister_netdev(dev);
-               sbmac_uninitctx(sc);
-               free_netdev(dev);
-       }
+       sbmac_platform_cleanup();
+       platform_driver_unregister(&sbmac_driver);
 }
 
 module_init(sbmac_init_module);