Merge branch 'linus' into cont_syslog
[safe/jmp/linux-2.6] / drivers / net / r8169.c
index 85c635f..03a8318 100644 (file)
@@ -23,6 +23,7 @@
 #include <linux/tcp.h>
 #include <linux/init.h>
 #include <linux/dma-mapping.h>
+#include <linux/pm_runtime.h>
 
 #include <asm/system.h>
 #include <asm/io.h>
@@ -186,7 +187,12 @@ static DEFINE_PCI_DEVICE_TABLE(rtl8169_pci_tbl) = {
 
 MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
 
-static int rx_copybreak = 200;
+/*
+ * we set our copybreak very high so that we don't have
+ * to allocate 16k frames all the time (see note in
+ * rtl8169_open()
+ */
+static int rx_copybreak = 16383;
 static int use_dac;
 static struct {
        u32 msg_enable;
@@ -504,6 +510,7 @@ struct rtl8169_private {
 
        struct mii_if_info mii;
        struct rtl8169_counters counters;
+       u32 saved_wolopts;
 };
 
 MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
@@ -552,6 +559,11 @@ static void mdio_write(void __iomem *ioaddr, int reg_addr, int value)
                        break;
                udelay(25);
        }
+       /*
+        * Some configurations require a small delay even after the write
+        * completed indication or the next write might fail.
+        */
+       udelay(25);
 }
 
 static int mdio_read(void __iomem *ioaddr, int reg_addr)
@@ -743,55 +755,61 @@ static void rtl8169_check_link_status(struct net_device *dev,
 
        spin_lock_irqsave(&tp->lock, flags);
        if (tp->link_ok(ioaddr)) {
+               /* This is to cancel a scheduled suspend if there's one. */
+               pm_request_resume(&tp->pci_dev->dev);
                netif_carrier_on(dev);
-               if (netif_msg_ifup(tp))
-                       printk(KERN_INFO PFX "%s: link up\n", dev->name);
+               netif_info(tp, ifup, dev, "link up\n");
        } else {
-               if (netif_msg_ifdown(tp))
-                       printk(KERN_INFO PFX "%s: link down\n", dev->name);
                netif_carrier_off(dev);
+               netif_info(tp, ifdown, dev, "link down\n");
+               pm_schedule_suspend(&tp->pci_dev->dev, 100);
        }
        spin_unlock_irqrestore(&tp->lock, flags);
 }
 
-static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
+#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
+
+static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
 {
-       struct rtl8169_private *tp = netdev_priv(dev);
        void __iomem *ioaddr = tp->mmio_addr;
        u8 options;
-
-       wol->wolopts = 0;
-
-#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
-       wol->supported = WAKE_ANY;
-
-       spin_lock_irq(&tp->lock);
+       u32 wolopts = 0;
 
        options = RTL_R8(Config1);
        if (!(options & PMEnable))
-               goto out_unlock;
+               return 0;
 
        options = RTL_R8(Config3);
        if (options & LinkUp)
-               wol->wolopts |= WAKE_PHY;
+               wolopts |= WAKE_PHY;
        if (options & MagicPacket)
-               wol->wolopts |= WAKE_MAGIC;
+               wolopts |= WAKE_MAGIC;
 
        options = RTL_R8(Config5);
        if (options & UWF)
-               wol->wolopts |= WAKE_UCAST;
+               wolopts |= WAKE_UCAST;
        if (options & BWF)
-               wol->wolopts |= WAKE_BCAST;
+               wolopts |= WAKE_BCAST;
        if (options & MWF)
-               wol->wolopts |= WAKE_MCAST;
+               wolopts |= WAKE_MCAST;
 
-out_unlock:
-       spin_unlock_irq(&tp->lock);
+       return wolopts;
 }
 
-static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
+static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
 {
        struct rtl8169_private *tp = netdev_priv(dev);
+
+       spin_lock_irq(&tp->lock);
+
+       wol->supported = WAKE_ANY;
+       wol->wolopts = __rtl8169_get_wol(tp);
+
+       spin_unlock_irq(&tp->lock);
+}
+
+static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
+{
        void __iomem *ioaddr = tp->mmio_addr;
        unsigned int i;
        static const struct {
@@ -808,23 +826,29 @@ static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
                { WAKE_ANY,   Config5, LanWake }
        };
 
-       spin_lock_irq(&tp->lock);
-
        RTL_W8(Cfg9346, Cfg9346_Unlock);
 
        for (i = 0; i < ARRAY_SIZE(cfg); i++) {
                u8 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
-               if (wol->wolopts & cfg[i].opt)
+               if (wolopts & cfg[i].opt)
                        options |= cfg[i].mask;
                RTL_W8(cfg[i].reg, options);
        }
 
        RTL_W8(Cfg9346, Cfg9346_Lock);
+}
+
+static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
+{
+       struct rtl8169_private *tp = netdev_priv(dev);
+
+       spin_lock_irq(&tp->lock);
 
        if (wol->wolopts)
                tp->features |= RTL_FEATURE_WOL;
        else
                tp->features &= ~RTL_FEATURE_WOL;
+       __rtl8169_set_wol(tp, wol->wolopts);
        device_set_wakeup_enable(&tp->pci_dev->dev, wol->wolopts);
 
        spin_unlock_irq(&tp->lock);
@@ -862,11 +886,8 @@ static int rtl8169_set_speed_tbi(struct net_device *dev,
        } else if (autoneg == AUTONEG_ENABLE)
                RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
        else {
-               if (netif_msg_link(tp)) {
-                       printk(KERN_WARNING "%s: "
-                              "incorrect speed setting refused in TBI mode\n",
-                              dev->name);
-               }
+               netif_warn(tp, link, dev,
+                          "incorrect speed setting refused in TBI mode\n");
                ret = -EOPNOTSUPP;
        }
 
@@ -901,9 +922,9 @@ static int rtl8169_set_speed_xmii(struct net_device *dev,
                    (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);
+               } else {
+                       netif_info(tp, link, dev,
+                                  "PHY does not support 1000Mbps\n");
                }
 
                bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
@@ -1042,14 +1063,14 @@ static void rtl8169_vlan_rx_register(struct net_device *dev,
 }
 
 static int rtl8169_rx_vlan_skb(struct rtl8169_private *tp, struct RxDesc *desc,
-                              struct sk_buff *skb)
+                              struct sk_buff *skb, int polling)
 {
        u32 opts2 = le32_to_cpu(desc->opts2);
        struct vlan_group *vlgrp = tp->vlgrp;
        int ret;
 
        if (vlgrp && (opts2 & RxVlanTag)) {
-               vlan_hwaccel_receive_skb(skb, vlgrp, swab16(opts2 & 0xffff));
+               __vlan_hwaccel_rx(skb, vlgrp, swab16(opts2 & 0xffff), polling);
                ret = 0;
        } else
                ret = -1;
@@ -1066,7 +1087,7 @@ static inline u32 rtl8169_tx_vlan_tag(struct rtl8169_private *tp,
 }
 
 static int rtl8169_rx_vlan_skb(struct rtl8169_private *tp, struct RxDesc *desc,
-                              struct sk_buff *skb)
+                              struct sk_buff *skb, int polling)
 {
        return -1;
 }
@@ -2705,8 +2726,7 @@ static void rtl8169_phy_timer(unsigned long __opaque)
        if (tp->link_ok(ioaddr))
                goto out_unlock;
 
-       if (netif_msg_link(tp))
-               printk(KERN_WARNING "%s: PHY reset until link up\n", dev->name);
+       netif_warn(tp, link, dev, "PHY reset until link up\n");
 
        tp->phy_reset_enable(ioaddr);
 
@@ -2760,6 +2780,7 @@ static void rtl8169_release_board(struct pci_dev *pdev, struct net_device *dev,
 {
        iounmap(ioaddr);
        pci_release_regions(pdev);
+       pci_clear_mwi(pdev);
        pci_disable_device(pdev);
        free_netdev(dev);
 }
@@ -2776,8 +2797,7 @@ static void rtl8169_phy_reset(struct net_device *dev,
                        return;
                msleep(1);
        }
-       if (netif_msg_link(tp))
-               printk(KERN_ERR "%s: PHY reset failed.\n", dev->name);
+       netif_err(tp, link, dev, "PHY reset failed\n");
 }
 
 static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
@@ -2811,8 +2831,8 @@ static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
         */
        rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL);
 
-       if ((RTL_R8(PHYstatus) & TBI_Enable) && netif_msg_link(tp))
-               printk(KERN_INFO PFX "%s: TBI auto-negotiating\n", dev->name);
+       if (RTL_R8(PHYstatus) & TBI_Enable)
+               netif_info(tp, link, dev, "TBI auto-negotiating\n");
 }
 
 static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
@@ -2827,8 +2847,13 @@ static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
        spin_lock_irq(&tp->lock);
 
        RTL_W8(Cfg9346, Cfg9346_Unlock);
-       RTL_W32(MAC0, low);
+
        RTL_W32(MAC4, high);
+       RTL_R32(MAC4);
+
+       RTL_W32(MAC0, low);
+       RTL_R32(MAC0);
+
        RTL_W8(Cfg9346, Cfg9346_Lock);
 
        spin_unlock_irq(&tp->lock);
@@ -3012,41 +3037,34 @@ rtl8169_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
        /* enable device (incl. PCI PM wakeup and hotplug setup) */
        rc = pci_enable_device(pdev);
        if (rc < 0) {
-               if (netif_msg_probe(tp))
-                       dev_err(&pdev->dev, "enable failure\n");
+               netif_err(tp, probe, dev, "enable failure\n");
                goto err_out_free_dev_1;
        }
 
-       rc = pci_set_mwi(pdev);
-       if (rc < 0)
-               goto err_out_disable_2;
+       if (pci_set_mwi(pdev) < 0)
+               netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
 
        /* make sure PCI base addr 1 is MMIO */
        if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
-               if (netif_msg_probe(tp)) {
-                       dev_err(&pdev->dev,
-                               "region #%d not an MMIO resource, aborting\n",
-                               region);
-               }
+               netif_err(tp, probe, dev,
+                         "region #%d not an MMIO resource, aborting\n",
+                         region);
                rc = -ENODEV;
-               goto err_out_mwi_3;
+               goto err_out_mwi_2;
        }
 
        /* check for weird/broken PCI region reporting */
        if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
-               if (netif_msg_probe(tp)) {
-                       dev_err(&pdev->dev,
-                               "Invalid PCI region size(s), aborting\n");
-               }
+               netif_err(tp, probe, dev,
+                         "Invalid PCI region size(s), aborting\n");
                rc = -ENODEV;
-               goto err_out_mwi_3;
+               goto err_out_mwi_2;
        }
 
        rc = pci_request_regions(pdev, MODULENAME);
        if (rc < 0) {
-               if (netif_msg_probe(tp))
-                       dev_err(&pdev->dev, "could not request regions.\n");
-               goto err_out_mwi_3;
+               netif_err(tp, probe, dev, "could not request regions\n");
+               goto err_out_mwi_2;
        }
 
        tp->cp_cmd = PCIMulRW | RxChkSum;
@@ -3058,26 +3076,22 @@ rtl8169_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
        } else {
                rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
                if (rc < 0) {
-                       if (netif_msg_probe(tp)) {
-                               dev_err(&pdev->dev,
-                                       "DMA configuration failed.\n");
-                       }
-                       goto err_out_free_res_4;
+                       netif_err(tp, probe, dev, "DMA configuration failed\n");
+                       goto err_out_free_res_3;
                }
        }
 
        /* ioremap MMIO region */
        ioaddr = ioremap(pci_resource_start(pdev, region), R8169_REGS_SIZE);
        if (!ioaddr) {
-               if (netif_msg_probe(tp))
-                       dev_err(&pdev->dev, "cannot remap MMIO, aborting\n");
+               netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
                rc = -EIO;
-               goto err_out_free_res_4;
+               goto err_out_free_res_3;
        }
 
        tp->pcie_cap = pci_find_capability(pdev, PCI_CAP_ID_EXP);
-       if (!tp->pcie_cap && netif_msg_probe(tp))
-               dev_info(&pdev->dev, "no PCI Express capability\n");
+       if (!tp->pcie_cap)
+               netif_info(tp, probe, dev, "no PCI Express capability\n");
 
        RTL_W16(IntrMask, 0x0000);
 
@@ -3100,10 +3114,8 @@ rtl8169_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
 
        /* 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");
-               }
+               netif_notice(tp, probe, dev,
+                            "unknown MAC, using family default\n");
                tp->mac_version = cfg->default_ver;
        }
 
@@ -3116,7 +3128,7 @@ rtl8169_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
        if (i == ARRAY_SIZE(rtl_chip_info)) {
                dev_err(&pdev->dev,
                        "driver bug, MAC version not found in rtl_chip_info\n");
-               goto err_out_msi_5;
+               goto err_out_msi_4;
        }
        tp->chipset = i;
 
@@ -3181,18 +3193,14 @@ rtl8169_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
 
        rc = register_netdev(dev);
        if (rc < 0)
-               goto err_out_msi_5;
+               goto err_out_msi_4;
 
        pci_set_drvdata(pdev, dev);
 
-       if (netif_msg_probe(tp)) {
-               u32 xid = RTL_R32(TxConfig) & 0x9cf0f8ff;
-
-               printk(KERN_INFO "%s: %s at 0x%lx, %pM, XID %08x IRQ %d\n",
-                      dev->name,
-                      rtl_chip_info[tp->chipset].name,
-                      dev->base_addr, dev->dev_addr, xid, dev->irq);
-       }
+       netif_info(tp, probe, dev, "%s at 0x%lx, %pM, XID %08x IRQ %d\n",
+                  rtl_chip_info[tp->chipset].name,
+                  dev->base_addr, dev->dev_addr,
+                  (u32)(RTL_R32(TxConfig) & 0x9cf0f8ff), dev->irq);
 
        rtl8169_init_phy(dev, tp);
 
@@ -3205,17 +3213,22 @@ rtl8169_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
 
        device_set_wakeup_enable(&pdev->dev, tp->features & RTL_FEATURE_WOL);
 
+       if (pci_dev_run_wake(pdev)) {
+               pm_runtime_set_active(&pdev->dev);
+               pm_runtime_enable(&pdev->dev);
+       }
+       pm_runtime_idle(&pdev->dev);
+
 out:
        return rc;
 
-err_out_msi_5:
+err_out_msi_4:
        rtl_disable_msi(pdev, tp);
        iounmap(ioaddr);
-err_out_free_res_4:
+err_out_free_res_3:
        pci_release_regions(pdev);
-err_out_mwi_3:
+err_out_mwi_2:
        pci_clear_mwi(pdev);
-err_out_disable_2:
        pci_disable_device(pdev);
 err_out_free_dev_1:
        free_netdev(dev);
@@ -3227,10 +3240,18 @@ static void __devexit rtl8169_remove_one(struct pci_dev *pdev)
        struct net_device *dev = pci_get_drvdata(pdev);
        struct rtl8169_private *tp = netdev_priv(dev);
 
+       pm_runtime_get_sync(&pdev->dev);
+
        flush_scheduled_work();
 
        unregister_netdev(dev);
 
+       if (pci_dev_run_wake(pdev)) {
+               pm_runtime_disable(&pdev->dev);
+               pm_runtime_set_suspended(&pdev->dev);
+       }
+       pm_runtime_put_noidle(&pdev->dev);
+
        /* restore original MAC address */
        rtl_rar_set(tp, dev->perm_addr);
 
@@ -3240,9 +3261,13 @@ static void __devexit rtl8169_remove_one(struct pci_dev *pdev)
 }
 
 static void rtl8169_set_rxbufsize(struct rtl8169_private *tp,
-                                 struct net_device *dev)
+                                 unsigned int mtu)
 {
-       unsigned int max_frame = dev->mtu + VLAN_ETH_HLEN + ETH_FCS_LEN;
+       unsigned int max_frame = mtu + VLAN_ETH_HLEN + ETH_FCS_LEN;
+
+       if (max_frame != 16383)
+               printk(KERN_WARNING PFX "WARNING! Changing of MTU on this "
+                       "NIC may lead to frame reception errors!\n");
 
        tp->rx_buf_sz = (max_frame > RX_BUF_SIZE) ? max_frame : RX_BUF_SIZE;
 }
@@ -3253,8 +3278,19 @@ static int rtl8169_open(struct net_device *dev)
        struct pci_dev *pdev = tp->pci_dev;
        int retval = -ENOMEM;
 
+       pm_runtime_get_sync(&pdev->dev);
 
-       rtl8169_set_rxbufsize(tp, dev);
+       /*
+        * Note that we use a magic value here, its wierd I know
+        * its done because, some subset of rtl8169 hardware suffers from
+        * a problem in which frames received that are longer than
+        * the size set in RxMaxSize register return garbage sizes
+        * when received.  To avoid this we need to turn off filtering,
+        * which is done by setting a value of 16383 in the RxMaxSize register
+        * and allocating 16k frames to handle the largest possible rx value
+        * thats what the magic math below does.
+        */
+       rtl8169_set_rxbufsize(tp, 16383 - VLAN_ETH_HLEN - ETH_FCS_LEN);
 
        /*
         * Rx and Tx desscriptors needs 256 bytes alignment.
@@ -3263,7 +3299,7 @@ static int rtl8169_open(struct net_device *dev)
        tp->TxDescArray = pci_alloc_consistent(pdev, R8169_TX_RING_BYTES,
                                               &tp->TxPhyAddr);
        if (!tp->TxDescArray)
-               goto out;
+               goto err_pm_runtime_put;
 
        tp->RxDescArray = pci_alloc_consistent(pdev, R8169_RX_RING_BYTES,
                                               &tp->RxPhyAddr);
@@ -3290,6 +3326,9 @@ static int rtl8169_open(struct net_device *dev)
 
        rtl8169_request_timer(dev);
 
+       tp->saved_wolopts = 0;
+       pm_runtime_put_noidle(&pdev->dev);
+
        rtl8169_check_link_status(dev, tp, tp->mmio_addr);
 out:
        return retval;
@@ -3299,9 +3338,13 @@ err_release_ring_2:
 err_free_rx_1:
        pci_free_consistent(pdev, R8169_RX_RING_BYTES, tp->RxDescArray,
                            tp->RxPhyAddr);
+       tp->RxDescArray = NULL;
 err_free_tx_0:
        pci_free_consistent(pdev, R8169_TX_RING_BYTES, tp->TxDescArray,
                            tp->TxPhyAddr);
+       tp->TxDescArray = NULL;
+err_pm_runtime_put:
+       pm_runtime_put_noidle(&pdev->dev);
        goto out;
 }
 
@@ -3907,7 +3950,7 @@ static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
 
        rtl8169_down(dev);
 
-       rtl8169_set_rxbufsize(tp, dev);
+       rtl8169_set_rxbufsize(tp, dev->mtu);
 
        ret = rtl8169_init_ring(dev);
        if (ret < 0)
@@ -4131,10 +4174,10 @@ static void rtl8169_reinit_task(struct work_struct *work)
 
        ret = rtl8169_open(dev);
        if (unlikely(ret < 0)) {
-               if (net_ratelimit() && netif_msg_drv(tp)) {
-                       printk(KERN_ERR PFX "%s: reinit failure (status = %d)."
-                              " Rescheduling.\n", dev->name, ret);
-               }
+               if (net_ratelimit())
+                       netif_err(tp, drv, dev,
+                                 "reinit failure (status = %d). Rescheduling\n",
+                                 ret);
                rtl8169_schedule_work(dev, rtl8169_reinit_task);
        }
 
@@ -4164,10 +4207,8 @@ static void rtl8169_reset_task(struct work_struct *work)
                netif_wake_queue(dev);
                rtl8169_check_link_status(dev, tp, tp->mmio_addr);
        } else {
-               if (net_ratelimit() && netif_msg_intr(tp)) {
-                       printk(KERN_EMERG PFX "%s: Rx buffers shortage\n",
-                              dev->name);
-               }
+               if (net_ratelimit())
+                       netif_emerg(tp, intr, dev, "Rx buffers shortage\n");
                rtl8169_schedule_work(dev, rtl8169_reset_task);
        }
 
@@ -4255,11 +4296,7 @@ static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
        u32 opts1;
 
        if (unlikely(TX_BUFFS_AVAIL(tp) < skb_shinfo(skb)->nr_frags)) {
-               if (netif_msg_drv(tp)) {
-                       printk(KERN_ERR
-                              "%s: BUG! Tx Ring full when queue awake!\n",
-                              dev->name);
-               }
+               netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
                goto err_stop;
        }
 
@@ -4292,7 +4329,7 @@ static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
 
        tp->cur_tx += frags + 1;
 
-       smp_wmb();
+       wmb();
 
        RTL_W8(TxPoll, NPQ);    /* set polling bit */
 
@@ -4321,11 +4358,8 @@ static void rtl8169_pcierr_interrupt(struct net_device *dev)
        pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
        pci_read_config_word(pdev, PCI_STATUS, &pci_status);
 
-       if (netif_msg_intr(tp)) {
-               printk(KERN_ERR
-                      "%s: PCI error (cmd = 0x%04x, status = 0x%04x).\n",
-                      dev->name, pci_cmd, pci_status);
-       }
+       netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
+                 pci_cmd, pci_status);
 
        /*
         * The recovery sequence below admits a very elaborated explanation:
@@ -4349,8 +4383,7 @@ static void rtl8169_pcierr_interrupt(struct net_device *dev)
 
        /* The infamous DAC f*ckup only happens at boot time */
        if ((tp->cp_cmd & PCIDAC) && !tp->dirty_rx && !tp->cur_rx) {
-               if (netif_msg_intr(tp))
-                       printk(KERN_INFO "%s: disabling PCI DAC.\n", dev->name);
+               netif_info(tp, intr, dev, "disabling PCI DAC\n");
                tp->cp_cmd &= ~PCIDAC;
                RTL_W16(CPlusCmd, tp->cp_cmd);
                dev->features &= ~NETIF_F_HIGHDMA;
@@ -4455,12 +4488,20 @@ out:
        return done;
 }
 
+/*
+ * Warning : rtl8169_rx_interrupt() might be called :
+ * 1) from NAPI (softirq) context
+ *     (polling = 1 : we should call netif_receive_skb())
+ * 2) from process context (rtl8169_reset_task())
+ *     (polling = 0 : we must call netif_rx() instead)
+ */
 static int rtl8169_rx_interrupt(struct net_device *dev,
                                struct rtl8169_private *tp,
                                void __iomem *ioaddr, u32 budget)
 {
        unsigned int cur_rx, rx_left;
        unsigned int delta, count;
+       int polling = (budget != ~(u32)0) ? 1 : 0;
 
        cur_rx = tp->cur_rx;
        rx_left = NUM_RX_DESC + tp->dirty_rx - cur_rx;
@@ -4477,11 +4518,8 @@ static int rtl8169_rx_interrupt(struct net_device *dev,
                if (status & DescOwn)
                        break;
                if (unlikely(status & RxRES)) {
-                       if (netif_msg_rx_err(tp)) {
-                               printk(KERN_INFO
-                                      "%s: Rx ERROR. status = %08x\n",
-                                      dev->name, status);
-                       }
+                       netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
+                                  status);
                        dev->stats.rx_errors++;
                        if (status & (RxRWT | RxRUNT))
                                dev->stats.rx_length_errors++;
@@ -4525,8 +4563,12 @@ static int rtl8169_rx_interrupt(struct net_device *dev,
                        skb_put(skb, pkt_size);
                        skb->protocol = eth_type_trans(skb, dev);
 
-                       if (rtl8169_rx_vlan_skb(tp, desc, skb) < 0)
-                               netif_receive_skb(skb);
+                       if (rtl8169_rx_vlan_skb(tp, desc, skb, polling) < 0) {
+                               if (likely(polling))
+                                       netif_receive_skb(skb);
+                               else
+                                       netif_rx(skb);
+                       }
 
                        dev->stats.rx_bytes += pkt_size;
                        dev->stats.rx_packets++;
@@ -4544,8 +4586,8 @@ static int rtl8169_rx_interrupt(struct net_device *dev,
        tp->cur_rx = cur_rx;
 
        delta = rtl8169_rx_fill(tp, dev, tp->dirty_rx, tp->cur_rx);
-       if (!delta && count && netif_msg_intr(tp))
-               printk(KERN_INFO "%s: no Rx buffer allocated\n", dev->name);
+       if (!delta && count)
+               netif_info(tp, intr, dev, "no Rx buffer allocated\n");
        tp->dirty_rx += delta;
 
        /*
@@ -4555,8 +4597,8 @@ static int rtl8169_rx_interrupt(struct net_device *dev,
         *   after refill ?
         * - how do others driver handle this condition (Uh oh...).
         */
-       if ((tp->dirty_rx + NUM_RX_DESC == tp->cur_rx) && netif_msg_intr(tp))
-               printk(KERN_EMERG "%s: Rx buffers exhausted\n", dev->name);
+       if (tp->dirty_rx + NUM_RX_DESC == tp->cur_rx)
+               netif_emerg(tp, intr, dev, "Rx buffers exhausted\n");
 
        return count;
 }
@@ -4611,10 +4653,9 @@ static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
 
                        if (likely(napi_schedule_prep(&tp->napi)))
                                __napi_schedule(&tp->napi);
-                       else if (netif_msg_intr(tp)) {
-                               printk(KERN_INFO "%s: interrupt %04x in poll\n",
-                               dev->name, status);
-                       }
+                       else
+                               netif_info(tp, intr, dev,
+                                          "interrupt %04x in poll\n", status);
                }
 
                /* We only get a new MSI interrupt when all active irq
@@ -4651,7 +4692,7 @@ static int rtl8169_poll(struct napi_struct *napi, int budget)
                 * until it does.
                 */
                tp->intr_mask = 0xffff;
-               smp_wmb();
+               wmb();
                RTL_W16(IntrMask, tp->intr_event);
        }
 
@@ -4722,6 +4763,8 @@ static int rtl8169_close(struct net_device *dev)
        struct rtl8169_private *tp = netdev_priv(dev);
        struct pci_dev *pdev = tp->pci_dev;
 
+       pm_runtime_get_sync(&pdev->dev);
+
        /* update counters before going down */
        rtl8169_update_counters(dev);
 
@@ -4736,6 +4779,8 @@ static int rtl8169_close(struct net_device *dev)
        tp->TxDescArray = NULL;
        tp->RxDescArray = NULL;
 
+       pm_runtime_put_sync(&pdev->dev);
+
        return 0;
 }
 
@@ -4750,10 +4795,7 @@ static void rtl_set_rx_mode(struct net_device *dev)
 
        if (dev->flags & IFF_PROMISC) {
                /* Unconditionally log net taps. */
-               if (netif_msg_link(tp)) {
-                       printk(KERN_NOTICE "%s: Promiscuous mode enabled.\n",
-                              dev->name);
-               }
+               netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
                rx_mode =
                    AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
                    AcceptAllPhys;
@@ -4764,14 +4806,12 @@ static void rtl_set_rx_mode(struct net_device *dev)
                rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
                mc_filter[1] = mc_filter[0] = 0xffffffff;
        } else {
-               struct dev_mc_list *mclist;
-               unsigned int i;
+               struct netdev_hw_addr *ha;
 
                rx_mode = AcceptBroadcast | AcceptMyPhys;
                mc_filter[1] = mc_filter[0] = 0;
-               for (i = 0, mclist = dev->mc_list; mclist && i < netdev_mc_count(dev);
-                    i++, mclist = mclist->next) {
-                       int bit_nr = ether_crc(ETH_ALEN, mclist->dmi_addr) >> 26;
+               netdev_for_each_mc_addr(ha, dev) {
+                       int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
                        mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
                        rx_mode |= AcceptMulticast;
                }
@@ -4789,8 +4829,8 @@ static void rtl_set_rx_mode(struct net_device *dev)
                mc_filter[1] = swab32(data);
        }
 
-       RTL_W32(MAR0 + 0, mc_filter[0]);
        RTL_W32(MAR0 + 4, mc_filter[1]);
+       RTL_W32(MAR0 + 0, mc_filter[0]);
 
        RTL_W32(RxConfig, tmp);
 
@@ -4839,21 +4879,74 @@ static int rtl8169_suspend(struct device *device)
        return 0;
 }
 
+static void __rtl8169_resume(struct net_device *dev)
+{
+       netif_device_attach(dev);
+       rtl8169_schedule_work(dev, rtl8169_reset_task);
+}
+
 static int rtl8169_resume(struct device *device)
 {
        struct pci_dev *pdev = to_pci_dev(device);
        struct net_device *dev = pci_get_drvdata(pdev);
 
-       if (!netif_running(dev))
-               goto out;
+       if (netif_running(dev))
+               __rtl8169_resume(dev);
 
-       netif_device_attach(dev);
+       return 0;
+}
+
+static int rtl8169_runtime_suspend(struct device *device)
+{
+       struct pci_dev *pdev = to_pci_dev(device);
+       struct net_device *dev = pci_get_drvdata(pdev);
+       struct rtl8169_private *tp = netdev_priv(dev);
+
+       if (!tp->TxDescArray)
+               return 0;
+
+       spin_lock_irq(&tp->lock);
+       tp->saved_wolopts = __rtl8169_get_wol(tp);
+       __rtl8169_set_wol(tp, WAKE_ANY);
+       spin_unlock_irq(&tp->lock);
+
+       rtl8169_net_suspend(dev);
 
-       rtl8169_schedule_work(dev, rtl8169_reset_task);
-out:
        return 0;
 }
 
+static int rtl8169_runtime_resume(struct device *device)
+{
+       struct pci_dev *pdev = to_pci_dev(device);
+       struct net_device *dev = pci_get_drvdata(pdev);
+       struct rtl8169_private *tp = netdev_priv(dev);
+
+       if (!tp->TxDescArray)
+               return 0;
+
+       spin_lock_irq(&tp->lock);
+       __rtl8169_set_wol(tp, tp->saved_wolopts);
+       tp->saved_wolopts = 0;
+       spin_unlock_irq(&tp->lock);
+
+       __rtl8169_resume(dev);
+
+       return 0;
+}
+
+static int rtl8169_runtime_idle(struct device *device)
+{
+       struct pci_dev *pdev = to_pci_dev(device);
+       struct net_device *dev = pci_get_drvdata(pdev);
+       struct rtl8169_private *tp = netdev_priv(dev);
+
+       if (!tp->TxDescArray)
+               return 0;
+
+       rtl8169_check_link_status(dev, tp, tp->mmio_addr);
+       return -EBUSY;
+}
+
 static const struct dev_pm_ops rtl8169_pm_ops = {
        .suspend = rtl8169_suspend,
        .resume = rtl8169_resume,
@@ -4861,6 +4954,9 @@ static const struct dev_pm_ops rtl8169_pm_ops = {
        .thaw = rtl8169_resume,
        .poweroff = rtl8169_suspend,
        .restore = rtl8169_resume,
+       .runtime_suspend = rtl8169_runtime_suspend,
+       .runtime_resume = rtl8169_runtime_resume,
+       .runtime_idle = rtl8169_runtime_idle,
 };
 
 #define RTL8169_PM_OPS (&rtl8169_pm_ops)