Merge branch 'bugfix' of git://git.kernel.org/pub/scm/linux/kernel/git/jeremy/xen...
[linux-2.6-block.git] / drivers / net / r8169.c
index 3b19e0ce290fa4b6ba5f53a0b33c23b30ef098f8..b82780d805f544c50b341ed1f34f07f41e400c99 100644 (file)
@@ -51,9 +51,6 @@
 #define TX_BUFFS_AVAIL(tp) \
        (tp->dirty_tx + NUM_TX_DESC - tp->cur_tx - 1)
 
-/* Maximum events (Rx packets, etc.) to handle at each interrupt. */
-static const int max_interrupt_work = 20;
-
 /* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
    The RTL chips use a 64 element hash table based on the Ethernet CRC. */
 static const int multicast_filter_limit = 32;
@@ -93,6 +90,7 @@ static const int multicast_filter_limit = 32;
 #define RTL_R32(reg)           ((unsigned long) readl (ioaddr + (reg)))
 
 enum mac_version {
+       RTL_GIGA_MAC_NONE   = 0x00,
        RTL_GIGA_MAC_VER_01 = 0x01, // 8169
        RTL_GIGA_MAC_VER_02 = 0x02, // 8169S
        RTL_GIGA_MAC_VER_03 = 0x03, // 8110S
@@ -478,7 +476,6 @@ struct rtl8169_private {
        u16 intr_event;
        u16 napi_event;
        u16 intr_mask;
-       int phy_auto_nego_reg;
        int phy_1000_ctrl_reg;
 #ifdef CONFIG_R8169_VLAN
        struct vlan_group *vlgrp;
@@ -843,76 +840,81 @@ static int rtl8169_set_speed_xmii(struct net_device *dev,
 {
        struct rtl8169_private *tp = netdev_priv(dev);
        void __iomem *ioaddr = tp->mmio_addr;
-       int auto_nego, giga_ctrl;
-
-       auto_nego = mdio_read(ioaddr, MII_ADVERTISE);
-       auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
-                      ADVERTISE_100HALF | ADVERTISE_100FULL);
-       giga_ctrl = mdio_read(ioaddr, MII_CTRL1000);
-       giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
+       int giga_ctrl, bmcr;
 
        if (autoneg == AUTONEG_ENABLE) {
+               int auto_nego;
+
+               auto_nego = mdio_read(ioaddr, MII_ADVERTISE);
                auto_nego |= (ADVERTISE_10HALF | ADVERTISE_10FULL |
                              ADVERTISE_100HALF | ADVERTISE_100FULL);
-               giga_ctrl |= ADVERTISE_1000FULL | ADVERTISE_1000HALF;
-       } else {
-               if (speed == SPEED_10)
-                       auto_nego |= ADVERTISE_10HALF | ADVERTISE_10FULL;
-               else if (speed == SPEED_100)
-                       auto_nego |= ADVERTISE_100HALF | ADVERTISE_100FULL;
-               else if (speed == SPEED_1000)
-                       giga_ctrl |= ADVERTISE_1000FULL | ADVERTISE_1000HALF;
-
-               if (duplex == DUPLEX_HALF)
-                       auto_nego &= ~(ADVERTISE_10FULL | ADVERTISE_100FULL);
-
-               if (duplex == DUPLEX_FULL)
-                       auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_100HALF);
+               auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
 
-               /* This tweak comes straight from Realtek's driver. */
-               if ((speed == SPEED_100) && (duplex == DUPLEX_HALF) &&
-                   ((tp->mac_version == RTL_GIGA_MAC_VER_13) ||
-                    (tp->mac_version == RTL_GIGA_MAC_VER_16))) {
-                       auto_nego = ADVERTISE_100HALF | ADVERTISE_CSMA;
-               }
-       }
+               giga_ctrl = mdio_read(ioaddr, MII_CTRL1000);
+               giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
 
-       /* The 8100e/8101e/8102e do Fast Ethernet only. */
-       if ((tp->mac_version == RTL_GIGA_MAC_VER_07) ||
-           (tp->mac_version == RTL_GIGA_MAC_VER_08) ||
-           (tp->mac_version == RTL_GIGA_MAC_VER_09) ||
-           (tp->mac_version == RTL_GIGA_MAC_VER_10) ||
-           (tp->mac_version == RTL_GIGA_MAC_VER_13) ||
-           (tp->mac_version == RTL_GIGA_MAC_VER_14) ||
-           (tp->mac_version == RTL_GIGA_MAC_VER_15) ||
-           (tp->mac_version == RTL_GIGA_MAC_VER_16)) {
-               if ((giga_ctrl & (ADVERTISE_1000FULL | ADVERTISE_1000HALF)) &&
-                   netif_msg_link(tp)) {
+               /* The 8100e/8101e/8102e do Fast Ethernet only. */
+               if ((tp->mac_version != RTL_GIGA_MAC_VER_07) &&
+                   (tp->mac_version != RTL_GIGA_MAC_VER_08) &&
+                   (tp->mac_version != RTL_GIGA_MAC_VER_09) &&
+                   (tp->mac_version != RTL_GIGA_MAC_VER_10) &&
+                   (tp->mac_version != RTL_GIGA_MAC_VER_13) &&
+                   (tp->mac_version != RTL_GIGA_MAC_VER_14) &&
+                   (tp->mac_version != RTL_GIGA_MAC_VER_15) &&
+                   (tp->mac_version != RTL_GIGA_MAC_VER_16)) {
+                       giga_ctrl |= ADVERTISE_1000FULL | ADVERTISE_1000HALF;
+               } else if (netif_msg_link(tp)) {
                        printk(KERN_INFO "%s: PHY does not support 1000Mbps.\n",
                               dev->name);
                }
-               giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
-       }
 
-       auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
+               bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
+
+               if ((tp->mac_version == RTL_GIGA_MAC_VER_11) ||
+                   (tp->mac_version == RTL_GIGA_MAC_VER_12) ||
+                   (tp->mac_version >= RTL_GIGA_MAC_VER_17)) {
+                       /*
+                        * Wake up the PHY.
+                        * Vendor specific (0x1f) and reserved (0x0e) MII
+                        * registers.
+                        */
+                       mdio_write(ioaddr, 0x1f, 0x0000);
+                       mdio_write(ioaddr, 0x0e, 0x0000);
+               }
+
+               mdio_write(ioaddr, MII_ADVERTISE, auto_nego);
+               mdio_write(ioaddr, MII_CTRL1000, giga_ctrl);
+       } else {
+               giga_ctrl = 0;
+
+               if (speed == SPEED_10)
+                       bmcr = 0;
+               else if (speed == SPEED_100)
+                       bmcr = BMCR_SPEED100;
+               else
+                       return -EINVAL;
+
+               if (duplex == DUPLEX_FULL)
+                       bmcr |= BMCR_FULLDPLX;
 
-       if ((tp->mac_version == RTL_GIGA_MAC_VER_11) ||
-           (tp->mac_version == RTL_GIGA_MAC_VER_12) ||
-           (tp->mac_version >= RTL_GIGA_MAC_VER_17)) {
-               /*
-                * Wake up the PHY.
-                * Vendor specific (0x1f) and reserved (0x0e) MII registers.
-                */
                mdio_write(ioaddr, 0x1f, 0x0000);
-               mdio_write(ioaddr, 0x0e, 0x0000);
        }
 
-       tp->phy_auto_nego_reg = auto_nego;
        tp->phy_1000_ctrl_reg = giga_ctrl;
 
-       mdio_write(ioaddr, MII_ADVERTISE, auto_nego);
-       mdio_write(ioaddr, MII_CTRL1000, giga_ctrl);
-       mdio_write(ioaddr, MII_BMCR, BMCR_ANENABLE | BMCR_ANRESTART);
+       mdio_write(ioaddr, MII_BMCR, bmcr);
+
+       if ((tp->mac_version == RTL_GIGA_MAC_VER_02) ||
+           (tp->mac_version == RTL_GIGA_MAC_VER_03)) {
+               if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
+                       mdio_write(ioaddr, 0x17, 0x2138);
+                       mdio_write(ioaddr, 0x0e, 0x0260);
+               } else {
+                       mdio_write(ioaddr, 0x17, 0x2108);
+                       mdio_write(ioaddr, 0x0e, 0x0000);
+               }
+       }
+
        return 0;
 }
 
@@ -1295,7 +1297,8 @@ static void rtl8169_get_mac_version(struct rtl8169_private *tp,
                { 0xfc800000, 0x00800000,       RTL_GIGA_MAC_VER_02 },
                { 0xfc800000, 0x00000000,       RTL_GIGA_MAC_VER_01 },
 
-               { 0x00000000, 0x00000000,       RTL_GIGA_MAC_VER_01 }   /* Catch-all */
+               /* Catch-all */
+               { 0x00000000, 0x00000000,       RTL_GIGA_MAC_NONE   }
        }, *p = mac_info;
        u32 reg;
 
@@ -1303,12 +1306,6 @@ static void rtl8169_get_mac_version(struct rtl8169_private *tp,
        while ((reg & p->mask) != p->val)
                p++;
        tp->mac_version = p->mac_version;
-
-       if (p->mask == 0x00000000) {
-               struct pci_dev *pdev = tp->pci_dev;
-
-               dev_info(&pdev->dev, "unknown MAC (%08x)\n", reg);
-       }
 }
 
 static void rtl8169_print_mac_version(struct rtl8169_private *tp)
@@ -1884,6 +1881,7 @@ static const struct rtl_cfg_info {
        u16 intr_event;
        u16 napi_event;
        unsigned features;
+       u8 default_ver;
 } rtl_cfg_infos [] = {
        [RTL_CFG_0] = {
                .hw_start       = rtl_hw_start_8169,
@@ -1892,7 +1890,8 @@ static const struct rtl_cfg_info {
                .intr_event     = SYSErr | LinkChg | RxOverflow |
                                  RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
                .napi_event     = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
-               .features       = RTL_FEATURE_GMII
+               .features       = RTL_FEATURE_GMII,
+               .default_ver    = RTL_GIGA_MAC_VER_01,
        },
        [RTL_CFG_1] = {
                .hw_start       = rtl_hw_start_8168,
@@ -1901,7 +1900,8 @@ static const struct rtl_cfg_info {
                .intr_event     = SYSErr | LinkChg | RxOverflow |
                                  TxErr | TxOK | RxOK | RxErr,
                .napi_event     = TxErr | TxOK | RxOK | RxOverflow,
-               .features       = RTL_FEATURE_GMII | RTL_FEATURE_MSI
+               .features       = RTL_FEATURE_GMII | RTL_FEATURE_MSI,
+               .default_ver    = RTL_GIGA_MAC_VER_11,
        },
        [RTL_CFG_2] = {
                .hw_start       = rtl_hw_start_8101,
@@ -1910,7 +1910,8 @@ static const struct rtl_cfg_info {
                .intr_event     = SYSErr | LinkChg | RxOverflow | PCSTimeout |
                                  RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
                .napi_event     = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
-               .features       = RTL_FEATURE_MSI
+               .features       = RTL_FEATURE_MSI,
+               .default_ver    = RTL_GIGA_MAC_VER_13,
        }
 };
 
@@ -2059,8 +2060,6 @@ rtl8169_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
                }
        }
 
-       pci_set_master(pdev);
-
        /* ioremap MMIO region */
        ioaddr = ioremap(pci_resource_start(pdev, region), R8169_REGS_SIZE);
        if (!ioaddr) {
@@ -2088,9 +2087,20 @@ rtl8169_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
 
        RTL_W16(IntrStatus, 0xffff);
 
+       pci_set_master(pdev);
+
        /* Identify chip attached to board */
        rtl8169_get_mac_version(tp, ioaddr);
 
+       /* Use appropriate default if unknown */
+       if (tp->mac_version == RTL_GIGA_MAC_NONE) {
+               if (netif_msg_probe(tp)) {
+                       dev_notice(&pdev->dev,
+                                  "unknown MAC, using family default\n");
+               }
+               tp->mac_version = cfg->default_ver;
+       }
+
        rtl8169_print_mac_version(tp);
 
        for (i = 0; i < ARRAY_SIZE(rtl_chip_info); i++) {
@@ -2098,13 +2108,9 @@ rtl8169_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
                        break;
        }
        if (i == ARRAY_SIZE(rtl_chip_info)) {
-               /* Unknown chip: assume array element #0, original RTL-8169 */
-               if (netif_msg_probe(tp)) {
-                       dev_printk(KERN_DEBUG, &pdev->dev,
-                               "unknown chip version, assuming %s\n",
-                               rtl_chip_info[0].name);
-               }
-               i = 0;
+               dev_err(&pdev->dev,
+                       "driver bug, MAC version not found in rtl_chip_info\n");
+               goto err_out_msi_5;
        }
        tp->chipset = i;
 
@@ -3269,8 +3275,6 @@ static int rtl8169_start_xmit(struct sk_buff *skb, struct net_device *dev)
        status = opts1 | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
        txd->opts1 = cpu_to_le32(status);
 
-       dev->trans_start = jiffies;
-
        tp->cur_tx += frags + 1;
 
        smp_wmb();
@@ -3371,7 +3375,7 @@ static void rtl8169_tx_interrupt(struct net_device *dev,
                rtl8169_unmap_tx_skb(tp->pci_dev, tx_skb, tp->TxDescArray + entry);
 
                if (status & LastFrag) {
-                       dev_kfree_skb_irq(tx_skb->skb);
+                       dev_kfree_skb(tx_skb->skb);
                        tx_skb->skb = NULL;
                }
                dirty_tx++;
@@ -3802,45 +3806,32 @@ static struct net_device_stats *rtl8169_get_stats(struct net_device *dev)
        return &dev->stats;
 }
 
-#ifdef CONFIG_PM
-
-static int rtl8169_suspend(struct pci_dev *pdev, pm_message_t state)
+static void rtl8169_net_suspend(struct net_device *dev)
 {
-       struct net_device *dev = pci_get_drvdata(pdev);
-       struct rtl8169_private *tp = netdev_priv(dev);
-       void __iomem *ioaddr = tp->mmio_addr;
-
        if (!netif_running(dev))
-               goto out_pci_suspend;
+               return;
 
        netif_device_detach(dev);
        netif_stop_queue(dev);
+}
 
-       spin_lock_irq(&tp->lock);
-
-       rtl8169_asic_down(ioaddr);
-
-       rtl8169_rx_missed(dev, ioaddr);
+#ifdef CONFIG_PM
 
-       spin_unlock_irq(&tp->lock);
+static int rtl8169_suspend(struct device *device)
+{
+       struct pci_dev *pdev = to_pci_dev(device);
+       struct net_device *dev = pci_get_drvdata(pdev);
 
-out_pci_suspend:
-       pci_save_state(pdev);
-       pci_enable_wake(pdev, pci_choose_state(pdev, state),
-               (tp->features & RTL_FEATURE_WOL) ? 1 : 0);
-       pci_set_power_state(pdev, pci_choose_state(pdev, state));
+       rtl8169_net_suspend(dev);
 
        return 0;
 }
 
-static int rtl8169_resume(struct pci_dev *pdev)
+static int rtl8169_resume(struct device *device)
 {
+       struct pci_dev *pdev = to_pci_dev(device);
        struct net_device *dev = pci_get_drvdata(pdev);
 
-       pci_set_power_state(pdev, PCI_D0);
-       pci_restore_state(pdev);
-       pci_enable_wake(pdev, PCI_D0, 0);
-
        if (!netif_running(dev))
                goto out;
 
@@ -3851,23 +3842,59 @@ out:
        return 0;
 }
 
+static struct dev_pm_ops rtl8169_pm_ops = {
+       .suspend = rtl8169_suspend,
+       .resume = rtl8169_resume,
+       .freeze = rtl8169_suspend,
+       .thaw = rtl8169_resume,
+       .poweroff = rtl8169_suspend,
+       .restore = rtl8169_resume,
+};
+
+#define RTL8169_PM_OPS (&rtl8169_pm_ops)
+
+#else /* !CONFIG_PM */
+
+#define RTL8169_PM_OPS NULL
+
+#endif /* !CONFIG_PM */
+
 static void rtl_shutdown(struct pci_dev *pdev)
 {
-       rtl8169_suspend(pdev, PMSG_SUSPEND);
-}
+       struct net_device *dev = pci_get_drvdata(pdev);
+       struct rtl8169_private *tp = netdev_priv(dev);
+       void __iomem *ioaddr = tp->mmio_addr;
 
-#endif /* CONFIG_PM */
+       rtl8169_net_suspend(dev);
+
+       spin_lock_irq(&tp->lock);
+
+       rtl8169_asic_down(ioaddr);
+
+       spin_unlock_irq(&tp->lock);
+
+       if (system_state == SYSTEM_POWER_OFF) {
+               /* WoL fails with some 8168 when the receiver is disabled. */
+               if (tp->features & RTL_FEATURE_WOL) {
+                       pci_clear_master(pdev);
+
+                       RTL_W8(ChipCmd, CmdRxEnb);
+                       /* PCI commit */
+                       RTL_R8(ChipCmd);
+               }
+
+               pci_wake_from_d3(pdev, true);
+               pci_set_power_state(pdev, PCI_D3hot);
+       }
+}
 
 static struct pci_driver rtl8169_pci_driver = {
        .name           = MODULENAME,
        .id_table       = rtl8169_pci_tbl,
        .probe          = rtl8169_init_one,
        .remove         = __devexit_p(rtl8169_remove_one),
-#ifdef CONFIG_PM
-       .suspend        = rtl8169_suspend,
-       .resume         = rtl8169_resume,
        .shutdown       = rtl_shutdown,
-#endif
+       .driver.pm      = RTL8169_PM_OPS,
 };
 
 static int __init rtl8169_init_module(void)