Merge branch 'batman-adv/next' of git://git.open-mesh.org/ecsv/linux-merge
[cascardo/linux.git] / drivers / net / e1000 / e1000_main.c
1 /*******************************************************************************
2
3   Intel PRO/1000 Linux driver
4   Copyright(c) 1999 - 2006 Intel Corporation.
5
6   This program is free software; you can redistribute it and/or modify it
7   under the terms and conditions of the GNU General Public License,
8   version 2, as published by the Free Software Foundation.
9
10   This program is distributed in the hope it will be useful, but WITHOUT
11   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13   more details.
14
15   You should have received a copy of the GNU General Public License along with
16   this program; if not, write to the Free Software Foundation, Inc.,
17   51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
18
19   The full GNU General Public License is included in this distribution in
20   the file called "COPYING".
21
22   Contact Information:
23   Linux NICS <linux.nics@intel.com>
24   e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
25   Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
26
27 *******************************************************************************/
28
29 #include "e1000.h"
30 #include <net/ip6_checksum.h>
31 #include <linux/io.h>
32
33 /* Intel Media SOC GbE MDIO physical base address */
34 static unsigned long ce4100_gbe_mdio_base_phy;
35 /* Intel Media SOC GbE MDIO virtual base address */
36 void __iomem *ce4100_gbe_mdio_base_virt;
37
38 char e1000_driver_name[] = "e1000";
39 static char e1000_driver_string[] = "Intel(R) PRO/1000 Network Driver";
40 #define DRV_VERSION "7.3.21-k8-NAPI"
41 const char e1000_driver_version[] = DRV_VERSION;
42 static const char e1000_copyright[] = "Copyright (c) 1999-2006 Intel Corporation.";
43
44 /* e1000_pci_tbl - PCI Device ID Table
45  *
46  * Last entry must be all 0s
47  *
48  * Macro expands to...
49  *   {PCI_DEVICE(PCI_VENDOR_ID_INTEL, device_id)}
50  */
51 static DEFINE_PCI_DEVICE_TABLE(e1000_pci_tbl) = {
52         INTEL_E1000_ETHERNET_DEVICE(0x1000),
53         INTEL_E1000_ETHERNET_DEVICE(0x1001),
54         INTEL_E1000_ETHERNET_DEVICE(0x1004),
55         INTEL_E1000_ETHERNET_DEVICE(0x1008),
56         INTEL_E1000_ETHERNET_DEVICE(0x1009),
57         INTEL_E1000_ETHERNET_DEVICE(0x100C),
58         INTEL_E1000_ETHERNET_DEVICE(0x100D),
59         INTEL_E1000_ETHERNET_DEVICE(0x100E),
60         INTEL_E1000_ETHERNET_DEVICE(0x100F),
61         INTEL_E1000_ETHERNET_DEVICE(0x1010),
62         INTEL_E1000_ETHERNET_DEVICE(0x1011),
63         INTEL_E1000_ETHERNET_DEVICE(0x1012),
64         INTEL_E1000_ETHERNET_DEVICE(0x1013),
65         INTEL_E1000_ETHERNET_DEVICE(0x1014),
66         INTEL_E1000_ETHERNET_DEVICE(0x1015),
67         INTEL_E1000_ETHERNET_DEVICE(0x1016),
68         INTEL_E1000_ETHERNET_DEVICE(0x1017),
69         INTEL_E1000_ETHERNET_DEVICE(0x1018),
70         INTEL_E1000_ETHERNET_DEVICE(0x1019),
71         INTEL_E1000_ETHERNET_DEVICE(0x101A),
72         INTEL_E1000_ETHERNET_DEVICE(0x101D),
73         INTEL_E1000_ETHERNET_DEVICE(0x101E),
74         INTEL_E1000_ETHERNET_DEVICE(0x1026),
75         INTEL_E1000_ETHERNET_DEVICE(0x1027),
76         INTEL_E1000_ETHERNET_DEVICE(0x1028),
77         INTEL_E1000_ETHERNET_DEVICE(0x1075),
78         INTEL_E1000_ETHERNET_DEVICE(0x1076),
79         INTEL_E1000_ETHERNET_DEVICE(0x1077),
80         INTEL_E1000_ETHERNET_DEVICE(0x1078),
81         INTEL_E1000_ETHERNET_DEVICE(0x1079),
82         INTEL_E1000_ETHERNET_DEVICE(0x107A),
83         INTEL_E1000_ETHERNET_DEVICE(0x107B),
84         INTEL_E1000_ETHERNET_DEVICE(0x107C),
85         INTEL_E1000_ETHERNET_DEVICE(0x108A),
86         INTEL_E1000_ETHERNET_DEVICE(0x1099),
87         INTEL_E1000_ETHERNET_DEVICE(0x10B5),
88         INTEL_E1000_ETHERNET_DEVICE(0x2E6E),
89         /* required last entry */
90         {0,}
91 };
92
93 MODULE_DEVICE_TABLE(pci, e1000_pci_tbl);
94
95 int e1000_up(struct e1000_adapter *adapter);
96 void e1000_down(struct e1000_adapter *adapter);
97 void e1000_reinit_locked(struct e1000_adapter *adapter);
98 void e1000_reset(struct e1000_adapter *adapter);
99 int e1000_setup_all_tx_resources(struct e1000_adapter *adapter);
100 int e1000_setup_all_rx_resources(struct e1000_adapter *adapter);
101 void e1000_free_all_tx_resources(struct e1000_adapter *adapter);
102 void e1000_free_all_rx_resources(struct e1000_adapter *adapter);
103 static int e1000_setup_tx_resources(struct e1000_adapter *adapter,
104                              struct e1000_tx_ring *txdr);
105 static int e1000_setup_rx_resources(struct e1000_adapter *adapter,
106                              struct e1000_rx_ring *rxdr);
107 static void e1000_free_tx_resources(struct e1000_adapter *adapter,
108                              struct e1000_tx_ring *tx_ring);
109 static void e1000_free_rx_resources(struct e1000_adapter *adapter,
110                              struct e1000_rx_ring *rx_ring);
111 void e1000_update_stats(struct e1000_adapter *adapter);
112
113 static int e1000_init_module(void);
114 static void e1000_exit_module(void);
115 static int e1000_probe(struct pci_dev *pdev, const struct pci_device_id *ent);
116 static void __devexit e1000_remove(struct pci_dev *pdev);
117 static int e1000_alloc_queues(struct e1000_adapter *adapter);
118 static int e1000_sw_init(struct e1000_adapter *adapter);
119 static int e1000_open(struct net_device *netdev);
120 static int e1000_close(struct net_device *netdev);
121 static void e1000_configure_tx(struct e1000_adapter *adapter);
122 static void e1000_configure_rx(struct e1000_adapter *adapter);
123 static void e1000_setup_rctl(struct e1000_adapter *adapter);
124 static void e1000_clean_all_tx_rings(struct e1000_adapter *adapter);
125 static void e1000_clean_all_rx_rings(struct e1000_adapter *adapter);
126 static void e1000_clean_tx_ring(struct e1000_adapter *adapter,
127                                 struct e1000_tx_ring *tx_ring);
128 static void e1000_clean_rx_ring(struct e1000_adapter *adapter,
129                                 struct e1000_rx_ring *rx_ring);
130 static void e1000_set_rx_mode(struct net_device *netdev);
131 static void e1000_update_phy_info(unsigned long data);
132 static void e1000_update_phy_info_task(struct work_struct *work);
133 static void e1000_watchdog(unsigned long data);
134 static void e1000_82547_tx_fifo_stall(unsigned long data);
135 static void e1000_82547_tx_fifo_stall_task(struct work_struct *work);
136 static netdev_tx_t e1000_xmit_frame(struct sk_buff *skb,
137                                     struct net_device *netdev);
138 static struct net_device_stats * e1000_get_stats(struct net_device *netdev);
139 static int e1000_change_mtu(struct net_device *netdev, int new_mtu);
140 static int e1000_set_mac(struct net_device *netdev, void *p);
141 static irqreturn_t e1000_intr(int irq, void *data);
142 static bool e1000_clean_tx_irq(struct e1000_adapter *adapter,
143                                struct e1000_tx_ring *tx_ring);
144 static int e1000_clean(struct napi_struct *napi, int budget);
145 static bool e1000_clean_rx_irq(struct e1000_adapter *adapter,
146                                struct e1000_rx_ring *rx_ring,
147                                int *work_done, int work_to_do);
148 static bool e1000_clean_jumbo_rx_irq(struct e1000_adapter *adapter,
149                                      struct e1000_rx_ring *rx_ring,
150                                      int *work_done, int work_to_do);
151 static void e1000_alloc_rx_buffers(struct e1000_adapter *adapter,
152                                    struct e1000_rx_ring *rx_ring,
153                                    int cleaned_count);
154 static void e1000_alloc_jumbo_rx_buffers(struct e1000_adapter *adapter,
155                                          struct e1000_rx_ring *rx_ring,
156                                          int cleaned_count);
157 static int e1000_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd);
158 static int e1000_mii_ioctl(struct net_device *netdev, struct ifreq *ifr,
159                            int cmd);
160 static void e1000_enter_82542_rst(struct e1000_adapter *adapter);
161 static void e1000_leave_82542_rst(struct e1000_adapter *adapter);
162 static void e1000_tx_timeout(struct net_device *dev);
163 static void e1000_reset_task(struct work_struct *work);
164 static void e1000_smartspeed(struct e1000_adapter *adapter);
165 static int e1000_82547_fifo_workaround(struct e1000_adapter *adapter,
166                                        struct sk_buff *skb);
167
168 static void e1000_vlan_rx_register(struct net_device *netdev, struct vlan_group *grp);
169 static void e1000_vlan_rx_add_vid(struct net_device *netdev, u16 vid);
170 static void e1000_vlan_rx_kill_vid(struct net_device *netdev, u16 vid);
171 static void e1000_restore_vlan(struct e1000_adapter *adapter);
172
173 #ifdef CONFIG_PM
174 static int e1000_suspend(struct pci_dev *pdev, pm_message_t state);
175 static int e1000_resume(struct pci_dev *pdev);
176 #endif
177 static void e1000_shutdown(struct pci_dev *pdev);
178
179 #ifdef CONFIG_NET_POLL_CONTROLLER
180 /* for netdump / net console */
181 static void e1000_netpoll (struct net_device *netdev);
182 #endif
183
184 #define COPYBREAK_DEFAULT 256
185 static unsigned int copybreak __read_mostly = COPYBREAK_DEFAULT;
186 module_param(copybreak, uint, 0644);
187 MODULE_PARM_DESC(copybreak,
188         "Maximum size of packet that is copied to a new buffer on receive");
189
190 static pci_ers_result_t e1000_io_error_detected(struct pci_dev *pdev,
191                      pci_channel_state_t state);
192 static pci_ers_result_t e1000_io_slot_reset(struct pci_dev *pdev);
193 static void e1000_io_resume(struct pci_dev *pdev);
194
195 static struct pci_error_handlers e1000_err_handler = {
196         .error_detected = e1000_io_error_detected,
197         .slot_reset = e1000_io_slot_reset,
198         .resume = e1000_io_resume,
199 };
200
201 static struct pci_driver e1000_driver = {
202         .name     = e1000_driver_name,
203         .id_table = e1000_pci_tbl,
204         .probe    = e1000_probe,
205         .remove   = __devexit_p(e1000_remove),
206 #ifdef CONFIG_PM
207         /* Power Management Hooks */
208         .suspend  = e1000_suspend,
209         .resume   = e1000_resume,
210 #endif
211         .shutdown = e1000_shutdown,
212         .err_handler = &e1000_err_handler
213 };
214
215 MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
216 MODULE_DESCRIPTION("Intel(R) PRO/1000 Network Driver");
217 MODULE_LICENSE("GPL");
218 MODULE_VERSION(DRV_VERSION);
219
220 static int debug = NETIF_MSG_DRV | NETIF_MSG_PROBE;
221 module_param(debug, int, 0);
222 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
223
224 /**
225  * e1000_get_hw_dev - return device
226  * used by hardware layer to print debugging information
227  *
228  **/
229 struct net_device *e1000_get_hw_dev(struct e1000_hw *hw)
230 {
231         struct e1000_adapter *adapter = hw->back;
232         return adapter->netdev;
233 }
234
235 /**
236  * e1000_init_module - Driver Registration Routine
237  *
238  * e1000_init_module is the first routine called when the driver is
239  * loaded. All it does is register with the PCI subsystem.
240  **/
241
242 static int __init e1000_init_module(void)
243 {
244         int ret;
245         pr_info("%s - version %s\n", e1000_driver_string, e1000_driver_version);
246
247         pr_info("%s\n", e1000_copyright);
248
249         ret = pci_register_driver(&e1000_driver);
250         if (copybreak != COPYBREAK_DEFAULT) {
251                 if (copybreak == 0)
252                         pr_info("copybreak disabled\n");
253                 else
254                         pr_info("copybreak enabled for "
255                                    "packets <= %u bytes\n", copybreak);
256         }
257         return ret;
258 }
259
260 module_init(e1000_init_module);
261
262 /**
263  * e1000_exit_module - Driver Exit Cleanup Routine
264  *
265  * e1000_exit_module is called just before the driver is removed
266  * from memory.
267  **/
268
269 static void __exit e1000_exit_module(void)
270 {
271         pci_unregister_driver(&e1000_driver);
272 }
273
274 module_exit(e1000_exit_module);
275
276 static int e1000_request_irq(struct e1000_adapter *adapter)
277 {
278         struct net_device *netdev = adapter->netdev;
279         irq_handler_t handler = e1000_intr;
280         int irq_flags = IRQF_SHARED;
281         int err;
282
283         err = request_irq(adapter->pdev->irq, handler, irq_flags, netdev->name,
284                           netdev);
285         if (err) {
286                 e_err(probe, "Unable to allocate interrupt Error: %d\n", err);
287         }
288
289         return err;
290 }
291
292 static void e1000_free_irq(struct e1000_adapter *adapter)
293 {
294         struct net_device *netdev = adapter->netdev;
295
296         free_irq(adapter->pdev->irq, netdev);
297 }
298
299 /**
300  * e1000_irq_disable - Mask off interrupt generation on the NIC
301  * @adapter: board private structure
302  **/
303
304 static void e1000_irq_disable(struct e1000_adapter *adapter)
305 {
306         struct e1000_hw *hw = &adapter->hw;
307
308         ew32(IMC, ~0);
309         E1000_WRITE_FLUSH();
310         synchronize_irq(adapter->pdev->irq);
311 }
312
313 /**
314  * e1000_irq_enable - Enable default interrupt generation settings
315  * @adapter: board private structure
316  **/
317
318 static void e1000_irq_enable(struct e1000_adapter *adapter)
319 {
320         struct e1000_hw *hw = &adapter->hw;
321
322         ew32(IMS, IMS_ENABLE_MASK);
323         E1000_WRITE_FLUSH();
324 }
325
326 static void e1000_update_mng_vlan(struct e1000_adapter *adapter)
327 {
328         struct e1000_hw *hw = &adapter->hw;
329         struct net_device *netdev = adapter->netdev;
330         u16 vid = hw->mng_cookie.vlan_id;
331         u16 old_vid = adapter->mng_vlan_id;
332         if (adapter->vlgrp) {
333                 if (!vlan_group_get_device(adapter->vlgrp, vid)) {
334                         if (hw->mng_cookie.status &
335                                 E1000_MNG_DHCP_COOKIE_STATUS_VLAN_SUPPORT) {
336                                 e1000_vlan_rx_add_vid(netdev, vid);
337                                 adapter->mng_vlan_id = vid;
338                         } else
339                                 adapter->mng_vlan_id = E1000_MNG_VLAN_NONE;
340
341                         if ((old_vid != (u16)E1000_MNG_VLAN_NONE) &&
342                                         (vid != old_vid) &&
343                             !vlan_group_get_device(adapter->vlgrp, old_vid))
344                                 e1000_vlan_rx_kill_vid(netdev, old_vid);
345                 } else
346                         adapter->mng_vlan_id = vid;
347         }
348 }
349
350 static void e1000_init_manageability(struct e1000_adapter *adapter)
351 {
352         struct e1000_hw *hw = &adapter->hw;
353
354         if (adapter->en_mng_pt) {
355                 u32 manc = er32(MANC);
356
357                 /* disable hardware interception of ARP */
358                 manc &= ~(E1000_MANC_ARP_EN);
359
360                 ew32(MANC, manc);
361         }
362 }
363
364 static void e1000_release_manageability(struct e1000_adapter *adapter)
365 {
366         struct e1000_hw *hw = &adapter->hw;
367
368         if (adapter->en_mng_pt) {
369                 u32 manc = er32(MANC);
370
371                 /* re-enable hardware interception of ARP */
372                 manc |= E1000_MANC_ARP_EN;
373
374                 ew32(MANC, manc);
375         }
376 }
377
378 /**
379  * e1000_configure - configure the hardware for RX and TX
380  * @adapter = private board structure
381  **/
382 static void e1000_configure(struct e1000_adapter *adapter)
383 {
384         struct net_device *netdev = adapter->netdev;
385         int i;
386
387         e1000_set_rx_mode(netdev);
388
389         e1000_restore_vlan(adapter);
390         e1000_init_manageability(adapter);
391
392         e1000_configure_tx(adapter);
393         e1000_setup_rctl(adapter);
394         e1000_configure_rx(adapter);
395         /* call E1000_DESC_UNUSED which always leaves
396          * at least 1 descriptor unused to make sure
397          * next_to_use != next_to_clean */
398         for (i = 0; i < adapter->num_rx_queues; i++) {
399                 struct e1000_rx_ring *ring = &adapter->rx_ring[i];
400                 adapter->alloc_rx_buf(adapter, ring,
401                                       E1000_DESC_UNUSED(ring));
402         }
403 }
404
405 int e1000_up(struct e1000_adapter *adapter)
406 {
407         struct e1000_hw *hw = &adapter->hw;
408
409         /* hardware has been reset, we need to reload some things */
410         e1000_configure(adapter);
411
412         clear_bit(__E1000_DOWN, &adapter->flags);
413
414         napi_enable(&adapter->napi);
415
416         e1000_irq_enable(adapter);
417
418         netif_wake_queue(adapter->netdev);
419
420         /* fire a link change interrupt to start the watchdog */
421         ew32(ICS, E1000_ICS_LSC);
422         return 0;
423 }
424
425 /**
426  * e1000_power_up_phy - restore link in case the phy was powered down
427  * @adapter: address of board private structure
428  *
429  * The phy may be powered down to save power and turn off link when the
430  * driver is unloaded and wake on lan is not enabled (among others)
431  * *** this routine MUST be followed by a call to e1000_reset ***
432  *
433  **/
434
435 void e1000_power_up_phy(struct e1000_adapter *adapter)
436 {
437         struct e1000_hw *hw = &adapter->hw;
438         u16 mii_reg = 0;
439
440         /* Just clear the power down bit to wake the phy back up */
441         if (hw->media_type == e1000_media_type_copper) {
442                 /* according to the manual, the phy will retain its
443                  * settings across a power-down/up cycle */
444                 e1000_read_phy_reg(hw, PHY_CTRL, &mii_reg);
445                 mii_reg &= ~MII_CR_POWER_DOWN;
446                 e1000_write_phy_reg(hw, PHY_CTRL, mii_reg);
447         }
448 }
449
450 static void e1000_power_down_phy(struct e1000_adapter *adapter)
451 {
452         struct e1000_hw *hw = &adapter->hw;
453
454         /* Power down the PHY so no link is implied when interface is down *
455          * The PHY cannot be powered down if any of the following is true *
456          * (a) WoL is enabled
457          * (b) AMT is active
458          * (c) SoL/IDER session is active */
459         if (!adapter->wol && hw->mac_type >= e1000_82540 &&
460            hw->media_type == e1000_media_type_copper) {
461                 u16 mii_reg = 0;
462
463                 switch (hw->mac_type) {
464                 case e1000_82540:
465                 case e1000_82545:
466                 case e1000_82545_rev_3:
467                 case e1000_82546:
468                 case e1000_ce4100:
469                 case e1000_82546_rev_3:
470                 case e1000_82541:
471                 case e1000_82541_rev_2:
472                 case e1000_82547:
473                 case e1000_82547_rev_2:
474                         if (er32(MANC) & E1000_MANC_SMBUS_EN)
475                                 goto out;
476                         break;
477                 default:
478                         goto out;
479                 }
480                 e1000_read_phy_reg(hw, PHY_CTRL, &mii_reg);
481                 mii_reg |= MII_CR_POWER_DOWN;
482                 e1000_write_phy_reg(hw, PHY_CTRL, mii_reg);
483                 mdelay(1);
484         }
485 out:
486         return;
487 }
488
489 void e1000_down(struct e1000_adapter *adapter)
490 {
491         struct e1000_hw *hw = &adapter->hw;
492         struct net_device *netdev = adapter->netdev;
493         u32 rctl, tctl;
494
495
496         /* disable receives in the hardware */
497         rctl = er32(RCTL);
498         ew32(RCTL, rctl & ~E1000_RCTL_EN);
499         /* flush and sleep below */
500
501         netif_tx_disable(netdev);
502
503         /* disable transmits in the hardware */
504         tctl = er32(TCTL);
505         tctl &= ~E1000_TCTL_EN;
506         ew32(TCTL, tctl);
507         /* flush both disables and wait for them to finish */
508         E1000_WRITE_FLUSH();
509         msleep(10);
510
511         napi_disable(&adapter->napi);
512
513         e1000_irq_disable(adapter);
514
515         /*
516          * Setting DOWN must be after irq_disable to prevent
517          * a screaming interrupt.  Setting DOWN also prevents
518          * timers and tasks from rescheduling.
519          */
520         set_bit(__E1000_DOWN, &adapter->flags);
521
522         del_timer_sync(&adapter->tx_fifo_stall_timer);
523         del_timer_sync(&adapter->watchdog_timer);
524         del_timer_sync(&adapter->phy_info_timer);
525
526         adapter->link_speed = 0;
527         adapter->link_duplex = 0;
528         netif_carrier_off(netdev);
529
530         e1000_reset(adapter);
531         e1000_clean_all_tx_rings(adapter);
532         e1000_clean_all_rx_rings(adapter);
533 }
534
535 static void e1000_reinit_safe(struct e1000_adapter *adapter)
536 {
537         while (test_and_set_bit(__E1000_RESETTING, &adapter->flags))
538                 msleep(1);
539         rtnl_lock();
540         e1000_down(adapter);
541         e1000_up(adapter);
542         rtnl_unlock();
543         clear_bit(__E1000_RESETTING, &adapter->flags);
544 }
545
546 void e1000_reinit_locked(struct e1000_adapter *adapter)
547 {
548         /* if rtnl_lock is not held the call path is bogus */
549         ASSERT_RTNL();
550         WARN_ON(in_interrupt());
551         while (test_and_set_bit(__E1000_RESETTING, &adapter->flags))
552                 msleep(1);
553         e1000_down(adapter);
554         e1000_up(adapter);
555         clear_bit(__E1000_RESETTING, &adapter->flags);
556 }
557
558 void e1000_reset(struct e1000_adapter *adapter)
559 {
560         struct e1000_hw *hw = &adapter->hw;
561         u32 pba = 0, tx_space, min_tx_space, min_rx_space;
562         bool legacy_pba_adjust = false;
563         u16 hwm;
564
565         /* Repartition Pba for greater than 9k mtu
566          * To take effect CTRL.RST is required.
567          */
568
569         switch (hw->mac_type) {
570         case e1000_82542_rev2_0:
571         case e1000_82542_rev2_1:
572         case e1000_82543:
573         case e1000_82544:
574         case e1000_82540:
575         case e1000_82541:
576         case e1000_82541_rev_2:
577                 legacy_pba_adjust = true;
578                 pba = E1000_PBA_48K;
579                 break;
580         case e1000_82545:
581         case e1000_82545_rev_3:
582         case e1000_82546:
583         case e1000_ce4100:
584         case e1000_82546_rev_3:
585                 pba = E1000_PBA_48K;
586                 break;
587         case e1000_82547:
588         case e1000_82547_rev_2:
589                 legacy_pba_adjust = true;
590                 pba = E1000_PBA_30K;
591                 break;
592         case e1000_undefined:
593         case e1000_num_macs:
594                 break;
595         }
596
597         if (legacy_pba_adjust) {
598                 if (hw->max_frame_size > E1000_RXBUFFER_8192)
599                         pba -= 8; /* allocate more FIFO for Tx */
600
601                 if (hw->mac_type == e1000_82547) {
602                         adapter->tx_fifo_head = 0;
603                         adapter->tx_head_addr = pba << E1000_TX_HEAD_ADDR_SHIFT;
604                         adapter->tx_fifo_size =
605                                 (E1000_PBA_40K - pba) << E1000_PBA_BYTES_SHIFT;
606                         atomic_set(&adapter->tx_fifo_stall, 0);
607                 }
608         } else if (hw->max_frame_size >  ETH_FRAME_LEN + ETH_FCS_LEN) {
609                 /* adjust PBA for jumbo frames */
610                 ew32(PBA, pba);
611
612                 /* To maintain wire speed transmits, the Tx FIFO should be
613                  * large enough to accommodate two full transmit packets,
614                  * rounded up to the next 1KB and expressed in KB.  Likewise,
615                  * the Rx FIFO should be large enough to accommodate at least
616                  * one full receive packet and is similarly rounded up and
617                  * expressed in KB. */
618                 pba = er32(PBA);
619                 /* upper 16 bits has Tx packet buffer allocation size in KB */
620                 tx_space = pba >> 16;
621                 /* lower 16 bits has Rx packet buffer allocation size in KB */
622                 pba &= 0xffff;
623                 /*
624                  * the tx fifo also stores 16 bytes of information about the tx
625                  * but don't include ethernet FCS because hardware appends it
626                  */
627                 min_tx_space = (hw->max_frame_size +
628                                 sizeof(struct e1000_tx_desc) -
629                                 ETH_FCS_LEN) * 2;
630                 min_tx_space = ALIGN(min_tx_space, 1024);
631                 min_tx_space >>= 10;
632                 /* software strips receive CRC, so leave room for it */
633                 min_rx_space = hw->max_frame_size;
634                 min_rx_space = ALIGN(min_rx_space, 1024);
635                 min_rx_space >>= 10;
636
637                 /* If current Tx allocation is less than the min Tx FIFO size,
638                  * and the min Tx FIFO size is less than the current Rx FIFO
639                  * allocation, take space away from current Rx allocation */
640                 if (tx_space < min_tx_space &&
641                     ((min_tx_space - tx_space) < pba)) {
642                         pba = pba - (min_tx_space - tx_space);
643
644                         /* PCI/PCIx hardware has PBA alignment constraints */
645                         switch (hw->mac_type) {
646                         case e1000_82545 ... e1000_82546_rev_3:
647                                 pba &= ~(E1000_PBA_8K - 1);
648                                 break;
649                         default:
650                                 break;
651                         }
652
653                         /* if short on rx space, rx wins and must trump tx
654                          * adjustment or use Early Receive if available */
655                         if (pba < min_rx_space)
656                                 pba = min_rx_space;
657                 }
658         }
659
660         ew32(PBA, pba);
661
662         /*
663          * flow control settings:
664          * The high water mark must be low enough to fit one full frame
665          * (or the size used for early receive) above it in the Rx FIFO.
666          * Set it to the lower of:
667          * - 90% of the Rx FIFO size, and
668          * - the full Rx FIFO size minus the early receive size (for parts
669          *   with ERT support assuming ERT set to E1000_ERT_2048), or
670          * - the full Rx FIFO size minus one full frame
671          */
672         hwm = min(((pba << 10) * 9 / 10),
673                   ((pba << 10) - hw->max_frame_size));
674
675         hw->fc_high_water = hwm & 0xFFF8;       /* 8-byte granularity */
676         hw->fc_low_water = hw->fc_high_water - 8;
677         hw->fc_pause_time = E1000_FC_PAUSE_TIME;
678         hw->fc_send_xon = 1;
679         hw->fc = hw->original_fc;
680
681         /* Allow time for pending master requests to run */
682         e1000_reset_hw(hw);
683         if (hw->mac_type >= e1000_82544)
684                 ew32(WUC, 0);
685
686         if (e1000_init_hw(hw))
687                 e_dev_err("Hardware Error\n");
688         e1000_update_mng_vlan(adapter);
689
690         /* if (adapter->hwflags & HWFLAGS_PHY_PWR_BIT) { */
691         if (hw->mac_type >= e1000_82544 &&
692             hw->autoneg == 1 &&
693             hw->autoneg_advertised == ADVERTISE_1000_FULL) {
694                 u32 ctrl = er32(CTRL);
695                 /* clear phy power management bit if we are in gig only mode,
696                  * which if enabled will attempt negotiation to 100Mb, which
697                  * can cause a loss of link at power off or driver unload */
698                 ctrl &= ~E1000_CTRL_SWDPIN3;
699                 ew32(CTRL, ctrl);
700         }
701
702         /* Enable h/w to recognize an 802.1Q VLAN Ethernet packet */
703         ew32(VET, ETHERNET_IEEE_VLAN_TYPE);
704
705         e1000_reset_adaptive(hw);
706         e1000_phy_get_info(hw, &adapter->phy_info);
707
708         e1000_release_manageability(adapter);
709 }
710
711 /**
712  *  Dump the eeprom for users having checksum issues
713  **/
714 static void e1000_dump_eeprom(struct e1000_adapter *adapter)
715 {
716         struct net_device *netdev = adapter->netdev;
717         struct ethtool_eeprom eeprom;
718         const struct ethtool_ops *ops = netdev->ethtool_ops;
719         u8 *data;
720         int i;
721         u16 csum_old, csum_new = 0;
722
723         eeprom.len = ops->get_eeprom_len(netdev);
724         eeprom.offset = 0;
725
726         data = kmalloc(eeprom.len, GFP_KERNEL);
727         if (!data) {
728                 pr_err("Unable to allocate memory to dump EEPROM data\n");
729                 return;
730         }
731
732         ops->get_eeprom(netdev, &eeprom, data);
733
734         csum_old = (data[EEPROM_CHECKSUM_REG * 2]) +
735                    (data[EEPROM_CHECKSUM_REG * 2 + 1] << 8);
736         for (i = 0; i < EEPROM_CHECKSUM_REG * 2; i += 2)
737                 csum_new += data[i] + (data[i + 1] << 8);
738         csum_new = EEPROM_SUM - csum_new;
739
740         pr_err("/*********************/\n");
741         pr_err("Current EEPROM Checksum : 0x%04x\n", csum_old);
742         pr_err("Calculated              : 0x%04x\n", csum_new);
743
744         pr_err("Offset    Values\n");
745         pr_err("========  ======\n");
746         print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 16, 1, data, 128, 0);
747
748         pr_err("Include this output when contacting your support provider.\n");
749         pr_err("This is not a software error! Something bad happened to\n");
750         pr_err("your hardware or EEPROM image. Ignoring this problem could\n");
751         pr_err("result in further problems, possibly loss of data,\n");
752         pr_err("corruption or system hangs!\n");
753         pr_err("The MAC Address will be reset to 00:00:00:00:00:00,\n");
754         pr_err("which is invalid and requires you to set the proper MAC\n");
755         pr_err("address manually before continuing to enable this network\n");
756         pr_err("device. Please inspect the EEPROM dump and report the\n");
757         pr_err("issue to your hardware vendor or Intel Customer Support.\n");
758         pr_err("/*********************/\n");
759
760         kfree(data);
761 }
762
763 /**
764  * e1000_is_need_ioport - determine if an adapter needs ioport resources or not
765  * @pdev: PCI device information struct
766  *
767  * Return true if an adapter needs ioport resources
768  **/
769 static int e1000_is_need_ioport(struct pci_dev *pdev)
770 {
771         switch (pdev->device) {
772         case E1000_DEV_ID_82540EM:
773         case E1000_DEV_ID_82540EM_LOM:
774         case E1000_DEV_ID_82540EP:
775         case E1000_DEV_ID_82540EP_LOM:
776         case E1000_DEV_ID_82540EP_LP:
777         case E1000_DEV_ID_82541EI:
778         case E1000_DEV_ID_82541EI_MOBILE:
779         case E1000_DEV_ID_82541ER:
780         case E1000_DEV_ID_82541ER_LOM:
781         case E1000_DEV_ID_82541GI:
782         case E1000_DEV_ID_82541GI_LF:
783         case E1000_DEV_ID_82541GI_MOBILE:
784         case E1000_DEV_ID_82544EI_COPPER:
785         case E1000_DEV_ID_82544EI_FIBER:
786         case E1000_DEV_ID_82544GC_COPPER:
787         case E1000_DEV_ID_82544GC_LOM:
788         case E1000_DEV_ID_82545EM_COPPER:
789         case E1000_DEV_ID_82545EM_FIBER:
790         case E1000_DEV_ID_82546EB_COPPER:
791         case E1000_DEV_ID_82546EB_FIBER:
792         case E1000_DEV_ID_82546EB_QUAD_COPPER:
793                 return true;
794         default:
795                 return false;
796         }
797 }
798
799 static const struct net_device_ops e1000_netdev_ops = {
800         .ndo_open               = e1000_open,
801         .ndo_stop               = e1000_close,
802         .ndo_start_xmit         = e1000_xmit_frame,
803         .ndo_get_stats          = e1000_get_stats,
804         .ndo_set_rx_mode        = e1000_set_rx_mode,
805         .ndo_set_mac_address    = e1000_set_mac,
806         .ndo_tx_timeout         = e1000_tx_timeout,
807         .ndo_change_mtu         = e1000_change_mtu,
808         .ndo_do_ioctl           = e1000_ioctl,
809         .ndo_validate_addr      = eth_validate_addr,
810
811         .ndo_vlan_rx_register   = e1000_vlan_rx_register,
812         .ndo_vlan_rx_add_vid    = e1000_vlan_rx_add_vid,
813         .ndo_vlan_rx_kill_vid   = e1000_vlan_rx_kill_vid,
814 #ifdef CONFIG_NET_POLL_CONTROLLER
815         .ndo_poll_controller    = e1000_netpoll,
816 #endif
817 };
818
819 /**
820  * e1000_init_hw_struct - initialize members of hw struct
821  * @adapter: board private struct
822  * @hw: structure used by e1000_hw.c
823  *
824  * Factors out initialization of the e1000_hw struct to its own function
825  * that can be called very early at init (just after struct allocation).
826  * Fields are initialized based on PCI device information and
827  * OS network device settings (MTU size).
828  * Returns negative error codes if MAC type setup fails.
829  */
830 static int e1000_init_hw_struct(struct e1000_adapter *adapter,
831                                 struct e1000_hw *hw)
832 {
833         struct pci_dev *pdev = adapter->pdev;
834
835         /* PCI config space info */
836         hw->vendor_id = pdev->vendor;
837         hw->device_id = pdev->device;
838         hw->subsystem_vendor_id = pdev->subsystem_vendor;
839         hw->subsystem_id = pdev->subsystem_device;
840         hw->revision_id = pdev->revision;
841
842         pci_read_config_word(pdev, PCI_COMMAND, &hw->pci_cmd_word);
843
844         hw->max_frame_size = adapter->netdev->mtu +
845                              ENET_HEADER_SIZE + ETHERNET_FCS_SIZE;
846         hw->min_frame_size = MINIMUM_ETHERNET_FRAME_SIZE;
847
848         /* identify the MAC */
849         if (e1000_set_mac_type(hw)) {
850                 e_err(probe, "Unknown MAC Type\n");
851                 return -EIO;
852         }
853
854         switch (hw->mac_type) {
855         default:
856                 break;
857         case e1000_82541:
858         case e1000_82547:
859         case e1000_82541_rev_2:
860         case e1000_82547_rev_2:
861                 hw->phy_init_script = 1;
862                 break;
863         }
864
865         e1000_set_media_type(hw);
866         e1000_get_bus_info(hw);
867
868         hw->wait_autoneg_complete = false;
869         hw->tbi_compatibility_en = true;
870         hw->adaptive_ifs = true;
871
872         /* Copper options */
873
874         if (hw->media_type == e1000_media_type_copper) {
875                 hw->mdix = AUTO_ALL_MODES;
876                 hw->disable_polarity_correction = false;
877                 hw->master_slave = E1000_MASTER_SLAVE;
878         }
879
880         return 0;
881 }
882
883 /**
884  * e1000_probe - Device Initialization Routine
885  * @pdev: PCI device information struct
886  * @ent: entry in e1000_pci_tbl
887  *
888  * Returns 0 on success, negative on failure
889  *
890  * e1000_probe initializes an adapter identified by a pci_dev structure.
891  * The OS initialization, configuring of the adapter private structure,
892  * and a hardware reset occur.
893  **/
894 static int __devinit e1000_probe(struct pci_dev *pdev,
895                                  const struct pci_device_id *ent)
896 {
897         struct net_device *netdev;
898         struct e1000_adapter *adapter;
899         struct e1000_hw *hw;
900
901         static int cards_found = 0;
902         static int global_quad_port_a = 0; /* global ksp3 port a indication */
903         int i, err, pci_using_dac;
904         u16 eeprom_data = 0;
905         u16 tmp = 0;
906         u16 eeprom_apme_mask = E1000_EEPROM_APME;
907         int bars, need_ioport;
908
909         /* do not allocate ioport bars when not needed */
910         need_ioport = e1000_is_need_ioport(pdev);
911         if (need_ioport) {
912                 bars = pci_select_bars(pdev, IORESOURCE_MEM | IORESOURCE_IO);
913                 err = pci_enable_device(pdev);
914         } else {
915                 bars = pci_select_bars(pdev, IORESOURCE_MEM);
916                 err = pci_enable_device_mem(pdev);
917         }
918         if (err)
919                 return err;
920
921         err = pci_request_selected_regions(pdev, bars, e1000_driver_name);
922         if (err)
923                 goto err_pci_reg;
924
925         pci_set_master(pdev);
926         err = pci_save_state(pdev);
927         if (err)
928                 goto err_alloc_etherdev;
929
930         err = -ENOMEM;
931         netdev = alloc_etherdev(sizeof(struct e1000_adapter));
932         if (!netdev)
933                 goto err_alloc_etherdev;
934
935         SET_NETDEV_DEV(netdev, &pdev->dev);
936
937         pci_set_drvdata(pdev, netdev);
938         adapter = netdev_priv(netdev);
939         adapter->netdev = netdev;
940         adapter->pdev = pdev;
941         adapter->msg_enable = (1 << debug) - 1;
942         adapter->bars = bars;
943         adapter->need_ioport = need_ioport;
944
945         hw = &adapter->hw;
946         hw->back = adapter;
947
948         err = -EIO;
949         hw->hw_addr = pci_ioremap_bar(pdev, BAR_0);
950         if (!hw->hw_addr)
951                 goto err_ioremap;
952
953         if (adapter->need_ioport) {
954                 for (i = BAR_1; i <= BAR_5; i++) {
955                         if (pci_resource_len(pdev, i) == 0)
956                                 continue;
957                         if (pci_resource_flags(pdev, i) & IORESOURCE_IO) {
958                                 hw->io_base = pci_resource_start(pdev, i);
959                                 break;
960                         }
961                 }
962         }
963
964         /* make ready for any if (hw->...) below */
965         err = e1000_init_hw_struct(adapter, hw);
966         if (err)
967                 goto err_sw_init;
968
969         /*
970          * there is a workaround being applied below that limits
971          * 64-bit DMA addresses to 64-bit hardware.  There are some
972          * 32-bit adapters that Tx hang when given 64-bit DMA addresses
973          */
974         pci_using_dac = 0;
975         if ((hw->bus_type == e1000_bus_type_pcix) &&
976             !dma_set_mask(&pdev->dev, DMA_BIT_MASK(64))) {
977                 /*
978                  * according to DMA-API-HOWTO, coherent calls will always
979                  * succeed if the set call did
980                  */
981                 dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(64));
982                 pci_using_dac = 1;
983         } else {
984                 err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
985                 if (err) {
986                         pr_err("No usable DMA config, aborting\n");
987                         goto err_dma;
988                 }
989                 dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
990         }
991
992         netdev->netdev_ops = &e1000_netdev_ops;
993         e1000_set_ethtool_ops(netdev);
994         netdev->watchdog_timeo = 5 * HZ;
995         netif_napi_add(netdev, &adapter->napi, e1000_clean, 64);
996
997         strncpy(netdev->name, pci_name(pdev), sizeof(netdev->name) - 1);
998
999         adapter->bd_number = cards_found;
1000
1001         /* setup the private structure */
1002
1003         err = e1000_sw_init(adapter);
1004         if (err)
1005                 goto err_sw_init;
1006
1007         err = -EIO;
1008         if (hw->mac_type == e1000_ce4100) {
1009                 ce4100_gbe_mdio_base_phy = pci_resource_start(pdev, BAR_1);
1010                 ce4100_gbe_mdio_base_virt = ioremap(ce4100_gbe_mdio_base_phy,
1011                                                 pci_resource_len(pdev, BAR_1));
1012
1013                 if (!ce4100_gbe_mdio_base_virt)
1014                         goto err_mdio_ioremap;
1015         }
1016
1017         if (hw->mac_type >= e1000_82543) {
1018                 netdev->features = NETIF_F_SG |
1019                                    NETIF_F_HW_CSUM |
1020                                    NETIF_F_HW_VLAN_TX |
1021                                    NETIF_F_HW_VLAN_RX |
1022                                    NETIF_F_HW_VLAN_FILTER;
1023         }
1024
1025         if ((hw->mac_type >= e1000_82544) &&
1026            (hw->mac_type != e1000_82547))
1027                 netdev->features |= NETIF_F_TSO;
1028
1029         if (pci_using_dac) {
1030                 netdev->features |= NETIF_F_HIGHDMA;
1031                 netdev->vlan_features |= NETIF_F_HIGHDMA;
1032         }
1033
1034         netdev->vlan_features |= NETIF_F_TSO;
1035         netdev->vlan_features |= NETIF_F_HW_CSUM;
1036         netdev->vlan_features |= NETIF_F_SG;
1037
1038         adapter->en_mng_pt = e1000_enable_mng_pass_thru(hw);
1039
1040         /* initialize eeprom parameters */
1041         if (e1000_init_eeprom_params(hw)) {
1042                 e_err(probe, "EEPROM initialization failed\n");
1043                 goto err_eeprom;
1044         }
1045
1046         /* before reading the EEPROM, reset the controller to
1047          * put the device in a known good starting state */
1048
1049         e1000_reset_hw(hw);
1050
1051         /* make sure the EEPROM is good */
1052         if (e1000_validate_eeprom_checksum(hw) < 0) {
1053                 e_err(probe, "The EEPROM Checksum Is Not Valid\n");
1054                 e1000_dump_eeprom(adapter);
1055                 /*
1056                  * set MAC address to all zeroes to invalidate and temporary
1057                  * disable this device for the user. This blocks regular
1058                  * traffic while still permitting ethtool ioctls from reaching
1059                  * the hardware as well as allowing the user to run the
1060                  * interface after manually setting a hw addr using
1061                  * `ip set address`
1062                  */
1063                 memset(hw->mac_addr, 0, netdev->addr_len);
1064         } else {
1065                 /* copy the MAC address out of the EEPROM */
1066                 if (e1000_read_mac_addr(hw))
1067                         e_err(probe, "EEPROM Read Error\n");
1068         }
1069         /* don't block initalization here due to bad MAC address */
1070         memcpy(netdev->dev_addr, hw->mac_addr, netdev->addr_len);
1071         memcpy(netdev->perm_addr, hw->mac_addr, netdev->addr_len);
1072
1073         if (!is_valid_ether_addr(netdev->perm_addr))
1074                 e_err(probe, "Invalid MAC Address\n");
1075
1076         init_timer(&adapter->tx_fifo_stall_timer);
1077         adapter->tx_fifo_stall_timer.function = e1000_82547_tx_fifo_stall;
1078         adapter->tx_fifo_stall_timer.data = (unsigned long)adapter;
1079
1080         init_timer(&adapter->watchdog_timer);
1081         adapter->watchdog_timer.function = e1000_watchdog;
1082         adapter->watchdog_timer.data = (unsigned long) adapter;
1083
1084         init_timer(&adapter->phy_info_timer);
1085         adapter->phy_info_timer.function = e1000_update_phy_info;
1086         adapter->phy_info_timer.data = (unsigned long)adapter;
1087
1088         INIT_WORK(&adapter->fifo_stall_task, e1000_82547_tx_fifo_stall_task);
1089         INIT_WORK(&adapter->reset_task, e1000_reset_task);
1090         INIT_WORK(&adapter->phy_info_task, e1000_update_phy_info_task);
1091
1092         e1000_check_options(adapter);
1093
1094         /* Initial Wake on LAN setting
1095          * If APM wake is enabled in the EEPROM,
1096          * enable the ACPI Magic Packet filter
1097          */
1098
1099         switch (hw->mac_type) {
1100         case e1000_82542_rev2_0:
1101         case e1000_82542_rev2_1:
1102         case e1000_82543:
1103                 break;
1104         case e1000_82544:
1105                 e1000_read_eeprom(hw,
1106                         EEPROM_INIT_CONTROL2_REG, 1, &eeprom_data);
1107                 eeprom_apme_mask = E1000_EEPROM_82544_APM;
1108                 break;
1109         case e1000_82546:
1110         case e1000_82546_rev_3:
1111                 if (er32(STATUS) & E1000_STATUS_FUNC_1){
1112                         e1000_read_eeprom(hw,
1113                                 EEPROM_INIT_CONTROL3_PORT_B, 1, &eeprom_data);
1114                         break;
1115                 }
1116                 /* Fall Through */
1117         default:
1118                 e1000_read_eeprom(hw,
1119                         EEPROM_INIT_CONTROL3_PORT_A, 1, &eeprom_data);
1120                 break;
1121         }
1122         if (eeprom_data & eeprom_apme_mask)
1123                 adapter->eeprom_wol |= E1000_WUFC_MAG;
1124
1125         /* now that we have the eeprom settings, apply the special cases
1126          * where the eeprom may be wrong or the board simply won't support
1127          * wake on lan on a particular port */
1128         switch (pdev->device) {
1129         case E1000_DEV_ID_82546GB_PCIE:
1130                 adapter->eeprom_wol = 0;
1131                 break;
1132         case E1000_DEV_ID_82546EB_FIBER:
1133         case E1000_DEV_ID_82546GB_FIBER:
1134                 /* Wake events only supported on port A for dual fiber
1135                  * regardless of eeprom setting */
1136                 if (er32(STATUS) & E1000_STATUS_FUNC_1)
1137                         adapter->eeprom_wol = 0;
1138                 break;
1139         case E1000_DEV_ID_82546GB_QUAD_COPPER_KSP3:
1140                 /* if quad port adapter, disable WoL on all but port A */
1141                 if (global_quad_port_a != 0)
1142                         adapter->eeprom_wol = 0;
1143                 else
1144                         adapter->quad_port_a = 1;
1145                 /* Reset for multiple quad port adapters */
1146                 if (++global_quad_port_a == 4)
1147                         global_quad_port_a = 0;
1148                 break;
1149         }
1150
1151         /* initialize the wol settings based on the eeprom settings */
1152         adapter->wol = adapter->eeprom_wol;
1153         device_set_wakeup_enable(&adapter->pdev->dev, adapter->wol);
1154
1155         /* Auto detect PHY address */
1156         if (hw->mac_type == e1000_ce4100) {
1157                 for (i = 0; i < 32; i++) {
1158                         hw->phy_addr = i;
1159                         e1000_read_phy_reg(hw, PHY_ID2, &tmp);
1160                         if (tmp == 0 || tmp == 0xFF) {
1161                                 if (i == 31)
1162                                         goto err_eeprom;
1163                                 continue;
1164                         } else
1165                                 break;
1166                 }
1167         }
1168
1169         /* reset the hardware with the new settings */
1170         e1000_reset(adapter);
1171
1172         strcpy(netdev->name, "eth%d");
1173         err = register_netdev(netdev);
1174         if (err)
1175                 goto err_register;
1176
1177         /* print bus type/speed/width info */
1178         e_info(probe, "(PCI%s:%dMHz:%d-bit) %pM\n",
1179                ((hw->bus_type == e1000_bus_type_pcix) ? "-X" : ""),
1180                ((hw->bus_speed == e1000_bus_speed_133) ? 133 :
1181                 (hw->bus_speed == e1000_bus_speed_120) ? 120 :
1182                 (hw->bus_speed == e1000_bus_speed_100) ? 100 :
1183                 (hw->bus_speed == e1000_bus_speed_66) ? 66 : 33),
1184                ((hw->bus_width == e1000_bus_width_64) ? 64 : 32),
1185                netdev->dev_addr);
1186
1187         /* carrier off reporting is important to ethtool even BEFORE open */
1188         netif_carrier_off(netdev);
1189
1190         e_info(probe, "Intel(R) PRO/1000 Network Connection\n");
1191
1192         cards_found++;
1193         return 0;
1194
1195 err_register:
1196 err_eeprom:
1197         e1000_phy_hw_reset(hw);
1198
1199         if (hw->flash_address)
1200                 iounmap(hw->flash_address);
1201         kfree(adapter->tx_ring);
1202         kfree(adapter->rx_ring);
1203 err_dma:
1204 err_sw_init:
1205 err_mdio_ioremap:
1206         iounmap(ce4100_gbe_mdio_base_virt);
1207         iounmap(hw->hw_addr);
1208 err_ioremap:
1209         free_netdev(netdev);
1210 err_alloc_etherdev:
1211         pci_release_selected_regions(pdev, bars);
1212 err_pci_reg:
1213         pci_disable_device(pdev);
1214         return err;
1215 }
1216
1217 /**
1218  * e1000_remove - Device Removal Routine
1219  * @pdev: PCI device information struct
1220  *
1221  * e1000_remove is called by the PCI subsystem to alert the driver
1222  * that it should release a PCI device.  The could be caused by a
1223  * Hot-Plug event, or because the driver is going to be removed from
1224  * memory.
1225  **/
1226
1227 static void __devexit e1000_remove(struct pci_dev *pdev)
1228 {
1229         struct net_device *netdev = pci_get_drvdata(pdev);
1230         struct e1000_adapter *adapter = netdev_priv(netdev);
1231         struct e1000_hw *hw = &adapter->hw;
1232
1233         set_bit(__E1000_DOWN, &adapter->flags);
1234         del_timer_sync(&adapter->tx_fifo_stall_timer);
1235         del_timer_sync(&adapter->watchdog_timer);
1236         del_timer_sync(&adapter->phy_info_timer);
1237
1238         cancel_work_sync(&adapter->reset_task);
1239
1240         e1000_release_manageability(adapter);
1241
1242         unregister_netdev(netdev);
1243
1244         e1000_phy_hw_reset(hw);
1245
1246         kfree(adapter->tx_ring);
1247         kfree(adapter->rx_ring);
1248
1249         iounmap(hw->hw_addr);
1250         if (hw->flash_address)
1251                 iounmap(hw->flash_address);
1252         pci_release_selected_regions(pdev, adapter->bars);
1253
1254         free_netdev(netdev);
1255
1256         pci_disable_device(pdev);
1257 }
1258
1259 /**
1260  * e1000_sw_init - Initialize general software structures (struct e1000_adapter)
1261  * @adapter: board private structure to initialize
1262  *
1263  * e1000_sw_init initializes the Adapter private data structure.
1264  * e1000_init_hw_struct MUST be called before this function
1265  **/
1266
1267 static int __devinit e1000_sw_init(struct e1000_adapter *adapter)
1268 {
1269         adapter->rx_buffer_len = MAXIMUM_ETHERNET_VLAN_SIZE;
1270
1271         adapter->num_tx_queues = 1;
1272         adapter->num_rx_queues = 1;
1273
1274         if (e1000_alloc_queues(adapter)) {
1275                 e_err(probe, "Unable to allocate memory for queues\n");
1276                 return -ENOMEM;
1277         }
1278
1279         /* Explicitly disable IRQ since the NIC can be in any state. */
1280         e1000_irq_disable(adapter);
1281
1282         spin_lock_init(&adapter->stats_lock);
1283
1284         set_bit(__E1000_DOWN, &adapter->flags);
1285
1286         return 0;
1287 }
1288
1289 /**
1290  * e1000_alloc_queues - Allocate memory for all rings
1291  * @adapter: board private structure to initialize
1292  *
1293  * We allocate one ring per queue at run-time since we don't know the
1294  * number of queues at compile-time.
1295  **/
1296
1297 static int __devinit e1000_alloc_queues(struct e1000_adapter *adapter)
1298 {
1299         adapter->tx_ring = kcalloc(adapter->num_tx_queues,
1300                                    sizeof(struct e1000_tx_ring), GFP_KERNEL);
1301         if (!adapter->tx_ring)
1302                 return -ENOMEM;
1303
1304         adapter->rx_ring = kcalloc(adapter->num_rx_queues,
1305                                    sizeof(struct e1000_rx_ring), GFP_KERNEL);
1306         if (!adapter->rx_ring) {
1307                 kfree(adapter->tx_ring);
1308                 return -ENOMEM;
1309         }
1310
1311         return E1000_SUCCESS;
1312 }
1313
1314 /**
1315  * e1000_open - Called when a network interface is made active
1316  * @netdev: network interface device structure
1317  *
1318  * Returns 0 on success, negative value on failure
1319  *
1320  * The open entry point is called when a network interface is made
1321  * active by the system (IFF_UP).  At this point all resources needed
1322  * for transmit and receive operations are allocated, the interrupt
1323  * handler is registered with the OS, the watchdog timer is started,
1324  * and the stack is notified that the interface is ready.
1325  **/
1326
1327 static int e1000_open(struct net_device *netdev)
1328 {
1329         struct e1000_adapter *adapter = netdev_priv(netdev);
1330         struct e1000_hw *hw = &adapter->hw;
1331         int err;
1332
1333         /* disallow open during test */
1334         if (test_bit(__E1000_TESTING, &adapter->flags))
1335                 return -EBUSY;
1336
1337         netif_carrier_off(netdev);
1338
1339         /* allocate transmit descriptors */
1340         err = e1000_setup_all_tx_resources(adapter);
1341         if (err)
1342                 goto err_setup_tx;
1343
1344         /* allocate receive descriptors */
1345         err = e1000_setup_all_rx_resources(adapter);
1346         if (err)
1347                 goto err_setup_rx;
1348
1349         e1000_power_up_phy(adapter);
1350
1351         adapter->mng_vlan_id = E1000_MNG_VLAN_NONE;
1352         if ((hw->mng_cookie.status &
1353                           E1000_MNG_DHCP_COOKIE_STATUS_VLAN_SUPPORT)) {
1354                 e1000_update_mng_vlan(adapter);
1355         }
1356
1357         /* before we allocate an interrupt, we must be ready to handle it.
1358          * Setting DEBUG_SHIRQ in the kernel makes it fire an interrupt
1359          * as soon as we call pci_request_irq, so we have to setup our
1360          * clean_rx handler before we do so.  */
1361         e1000_configure(adapter);
1362
1363         err = e1000_request_irq(adapter);
1364         if (err)
1365                 goto err_req_irq;
1366
1367         /* From here on the code is the same as e1000_up() */
1368         clear_bit(__E1000_DOWN, &adapter->flags);
1369
1370         napi_enable(&adapter->napi);
1371
1372         e1000_irq_enable(adapter);
1373
1374         netif_start_queue(netdev);
1375
1376         /* fire a link status change interrupt to start the watchdog */
1377         ew32(ICS, E1000_ICS_LSC);
1378
1379         return E1000_SUCCESS;
1380
1381 err_req_irq:
1382         e1000_power_down_phy(adapter);
1383         e1000_free_all_rx_resources(adapter);
1384 err_setup_rx:
1385         e1000_free_all_tx_resources(adapter);
1386 err_setup_tx:
1387         e1000_reset(adapter);
1388
1389         return err;
1390 }
1391
1392 /**
1393  * e1000_close - Disables a network interface
1394  * @netdev: network interface device structure
1395  *
1396  * Returns 0, this is not allowed to fail
1397  *
1398  * The close entry point is called when an interface is de-activated
1399  * by the OS.  The hardware is still under the drivers control, but
1400  * needs to be disabled.  A global MAC reset is issued to stop the
1401  * hardware, and all transmit and receive resources are freed.
1402  **/
1403
1404 static int e1000_close(struct net_device *netdev)
1405 {
1406         struct e1000_adapter *adapter = netdev_priv(netdev);
1407         struct e1000_hw *hw = &adapter->hw;
1408
1409         WARN_ON(test_bit(__E1000_RESETTING, &adapter->flags));
1410         e1000_down(adapter);
1411         e1000_power_down_phy(adapter);
1412         e1000_free_irq(adapter);
1413
1414         e1000_free_all_tx_resources(adapter);
1415         e1000_free_all_rx_resources(adapter);
1416
1417         /* kill manageability vlan ID if supported, but not if a vlan with
1418          * the same ID is registered on the host OS (let 8021q kill it) */
1419         if ((hw->mng_cookie.status &
1420                           E1000_MNG_DHCP_COOKIE_STATUS_VLAN_SUPPORT) &&
1421              !(adapter->vlgrp &&
1422                vlan_group_get_device(adapter->vlgrp, adapter->mng_vlan_id))) {
1423                 e1000_vlan_rx_kill_vid(netdev, adapter->mng_vlan_id);
1424         }
1425
1426         return 0;
1427 }
1428
1429 /**
1430  * e1000_check_64k_bound - check that memory doesn't cross 64kB boundary
1431  * @adapter: address of board private structure
1432  * @start: address of beginning of memory
1433  * @len: length of memory
1434  **/
1435 static bool e1000_check_64k_bound(struct e1000_adapter *adapter, void *start,
1436                                   unsigned long len)
1437 {
1438         struct e1000_hw *hw = &adapter->hw;
1439         unsigned long begin = (unsigned long)start;
1440         unsigned long end = begin + len;
1441
1442         /* First rev 82545 and 82546 need to not allow any memory
1443          * write location to cross 64k boundary due to errata 23 */
1444         if (hw->mac_type == e1000_82545 ||
1445             hw->mac_type == e1000_ce4100 ||
1446             hw->mac_type == e1000_82546) {
1447                 return ((begin ^ (end - 1)) >> 16) != 0 ? false : true;
1448         }
1449
1450         return true;
1451 }
1452
1453 /**
1454  * e1000_setup_tx_resources - allocate Tx resources (Descriptors)
1455  * @adapter: board private structure
1456  * @txdr:    tx descriptor ring (for a specific queue) to setup
1457  *
1458  * Return 0 on success, negative on failure
1459  **/
1460
1461 static int e1000_setup_tx_resources(struct e1000_adapter *adapter,
1462                                     struct e1000_tx_ring *txdr)
1463 {
1464         struct pci_dev *pdev = adapter->pdev;
1465         int size;
1466
1467         size = sizeof(struct e1000_buffer) * txdr->count;
1468         txdr->buffer_info = vzalloc(size);
1469         if (!txdr->buffer_info) {
1470                 e_err(probe, "Unable to allocate memory for the Tx descriptor "
1471                       "ring\n");
1472                 return -ENOMEM;
1473         }
1474
1475         /* round up to nearest 4K */
1476
1477         txdr->size = txdr->count * sizeof(struct e1000_tx_desc);
1478         txdr->size = ALIGN(txdr->size, 4096);
1479
1480         txdr->desc = dma_alloc_coherent(&pdev->dev, txdr->size, &txdr->dma,
1481                                         GFP_KERNEL);
1482         if (!txdr->desc) {
1483 setup_tx_desc_die:
1484                 vfree(txdr->buffer_info);
1485                 e_err(probe, "Unable to allocate memory for the Tx descriptor "
1486                       "ring\n");
1487                 return -ENOMEM;
1488         }
1489
1490         /* Fix for errata 23, can't cross 64kB boundary */
1491         if (!e1000_check_64k_bound(adapter, txdr->desc, txdr->size)) {
1492                 void *olddesc = txdr->desc;
1493                 dma_addr_t olddma = txdr->dma;
1494                 e_err(tx_err, "txdr align check failed: %u bytes at %p\n",
1495                       txdr->size, txdr->desc);
1496                 /* Try again, without freeing the previous */
1497                 txdr->desc = dma_alloc_coherent(&pdev->dev, txdr->size,
1498                                                 &txdr->dma, GFP_KERNEL);
1499                 /* Failed allocation, critical failure */
1500                 if (!txdr->desc) {
1501                         dma_free_coherent(&pdev->dev, txdr->size, olddesc,
1502                                           olddma);
1503                         goto setup_tx_desc_die;
1504                 }
1505
1506                 if (!e1000_check_64k_bound(adapter, txdr->desc, txdr->size)) {
1507                         /* give up */
1508                         dma_free_coherent(&pdev->dev, txdr->size, txdr->desc,
1509                                           txdr->dma);
1510                         dma_free_coherent(&pdev->dev, txdr->size, olddesc,
1511                                           olddma);
1512                         e_err(probe, "Unable to allocate aligned memory "
1513                               "for the transmit descriptor ring\n");
1514                         vfree(txdr->buffer_info);
1515                         return -ENOMEM;
1516                 } else {
1517                         /* Free old allocation, new allocation was successful */
1518                         dma_free_coherent(&pdev->dev, txdr->size, olddesc,
1519                                           olddma);
1520                 }
1521         }
1522         memset(txdr->desc, 0, txdr->size);
1523
1524         txdr->next_to_use = 0;
1525         txdr->next_to_clean = 0;
1526
1527         return 0;
1528 }
1529
1530 /**
1531  * e1000_setup_all_tx_resources - wrapper to allocate Tx resources
1532  *                                (Descriptors) for all queues
1533  * @adapter: board private structure
1534  *
1535  * Return 0 on success, negative on failure
1536  **/
1537
1538 int e1000_setup_all_tx_resources(struct e1000_adapter *adapter)
1539 {
1540         int i, err = 0;
1541
1542         for (i = 0; i < adapter->num_tx_queues; i++) {
1543                 err = e1000_setup_tx_resources(adapter, &adapter->tx_ring[i]);
1544                 if (err) {
1545                         e_err(probe, "Allocation for Tx Queue %u failed\n", i);
1546                         for (i-- ; i >= 0; i--)
1547                                 e1000_free_tx_resources(adapter,
1548                                                         &adapter->tx_ring[i]);
1549                         break;
1550                 }
1551         }
1552
1553         return err;
1554 }
1555
1556 /**
1557  * e1000_configure_tx - Configure 8254x Transmit Unit after Reset
1558  * @adapter: board private structure
1559  *
1560  * Configure the Tx unit of the MAC after a reset.
1561  **/
1562
1563 static void e1000_configure_tx(struct e1000_adapter *adapter)
1564 {
1565         u64 tdba;
1566         struct e1000_hw *hw = &adapter->hw;
1567         u32 tdlen, tctl, tipg;
1568         u32 ipgr1, ipgr2;
1569
1570         /* Setup the HW Tx Head and Tail descriptor pointers */
1571
1572         switch (adapter->num_tx_queues) {
1573         case 1:
1574         default:
1575                 tdba = adapter->tx_ring[0].dma;
1576                 tdlen = adapter->tx_ring[0].count *
1577                         sizeof(struct e1000_tx_desc);
1578                 ew32(TDLEN, tdlen);
1579                 ew32(TDBAH, (tdba >> 32));
1580                 ew32(TDBAL, (tdba & 0x00000000ffffffffULL));
1581                 ew32(TDT, 0);
1582                 ew32(TDH, 0);
1583                 adapter->tx_ring[0].tdh = ((hw->mac_type >= e1000_82543) ? E1000_TDH : E1000_82542_TDH);
1584                 adapter->tx_ring[0].tdt = ((hw->mac_type >= e1000_82543) ? E1000_TDT : E1000_82542_TDT);
1585                 break;
1586         }
1587
1588         /* Set the default values for the Tx Inter Packet Gap timer */
1589         if ((hw->media_type == e1000_media_type_fiber ||
1590              hw->media_type == e1000_media_type_internal_serdes))
1591                 tipg = DEFAULT_82543_TIPG_IPGT_FIBER;
1592         else
1593                 tipg = DEFAULT_82543_TIPG_IPGT_COPPER;
1594
1595         switch (hw->mac_type) {
1596         case e1000_82542_rev2_0:
1597         case e1000_82542_rev2_1:
1598                 tipg = DEFAULT_82542_TIPG_IPGT;
1599                 ipgr1 = DEFAULT_82542_TIPG_IPGR1;
1600                 ipgr2 = DEFAULT_82542_TIPG_IPGR2;
1601                 break;
1602         default:
1603                 ipgr1 = DEFAULT_82543_TIPG_IPGR1;
1604                 ipgr2 = DEFAULT_82543_TIPG_IPGR2;
1605                 break;
1606         }
1607         tipg |= ipgr1 << E1000_TIPG_IPGR1_SHIFT;
1608         tipg |= ipgr2 << E1000_TIPG_IPGR2_SHIFT;
1609         ew32(TIPG, tipg);
1610
1611         /* Set the Tx Interrupt Delay register */
1612
1613         ew32(TIDV, adapter->tx_int_delay);
1614         if (hw->mac_type >= e1000_82540)
1615                 ew32(TADV, adapter->tx_abs_int_delay);
1616
1617         /* Program the Transmit Control Register */
1618
1619         tctl = er32(TCTL);
1620         tctl &= ~E1000_TCTL_CT;
1621         tctl |= E1000_TCTL_PSP | E1000_TCTL_RTLC |
1622                 (E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT);
1623
1624         e1000_config_collision_dist(hw);
1625
1626         /* Setup Transmit Descriptor Settings for eop descriptor */
1627         adapter->txd_cmd = E1000_TXD_CMD_EOP | E1000_TXD_CMD_IFCS;
1628
1629         /* only set IDE if we are delaying interrupts using the timers */
1630         if (adapter->tx_int_delay)
1631                 adapter->txd_cmd |= E1000_TXD_CMD_IDE;
1632
1633         if (hw->mac_type < e1000_82543)
1634                 adapter->txd_cmd |= E1000_TXD_CMD_RPS;
1635         else
1636                 adapter->txd_cmd |= E1000_TXD_CMD_RS;
1637
1638         /* Cache if we're 82544 running in PCI-X because we'll
1639          * need this to apply a workaround later in the send path. */
1640         if (hw->mac_type == e1000_82544 &&
1641             hw->bus_type == e1000_bus_type_pcix)
1642                 adapter->pcix_82544 = 1;
1643
1644         ew32(TCTL, tctl);
1645
1646 }
1647
1648 /**
1649  * e1000_setup_rx_resources - allocate Rx resources (Descriptors)
1650  * @adapter: board private structure
1651  * @rxdr:    rx descriptor ring (for a specific queue) to setup
1652  *
1653  * Returns 0 on success, negative on failure
1654  **/
1655
1656 static int e1000_setup_rx_resources(struct e1000_adapter *adapter,
1657                                     struct e1000_rx_ring *rxdr)
1658 {
1659         struct pci_dev *pdev = adapter->pdev;
1660         int size, desc_len;
1661
1662         size = sizeof(struct e1000_buffer) * rxdr->count;
1663         rxdr->buffer_info = vzalloc(size);
1664         if (!rxdr->buffer_info) {
1665                 e_err(probe, "Unable to allocate memory for the Rx descriptor "
1666                       "ring\n");
1667                 return -ENOMEM;
1668         }
1669
1670         desc_len = sizeof(struct e1000_rx_desc);
1671
1672         /* Round up to nearest 4K */
1673
1674         rxdr->size = rxdr->count * desc_len;
1675         rxdr->size = ALIGN(rxdr->size, 4096);
1676
1677         rxdr->desc = dma_alloc_coherent(&pdev->dev, rxdr->size, &rxdr->dma,
1678                                         GFP_KERNEL);
1679
1680         if (!rxdr->desc) {
1681                 e_err(probe, "Unable to allocate memory for the Rx descriptor "
1682                       "ring\n");
1683 setup_rx_desc_die:
1684                 vfree(rxdr->buffer_info);
1685                 return -ENOMEM;
1686         }
1687
1688         /* Fix for errata 23, can't cross 64kB boundary */
1689         if (!e1000_check_64k_bound(adapter, rxdr->desc, rxdr->size)) {
1690                 void *olddesc = rxdr->desc;
1691                 dma_addr_t olddma = rxdr->dma;
1692                 e_err(rx_err, "rxdr align check failed: %u bytes at %p\n",
1693                       rxdr->size, rxdr->desc);
1694                 /* Try again, without freeing the previous */
1695                 rxdr->desc = dma_alloc_coherent(&pdev->dev, rxdr->size,
1696                                                 &rxdr->dma, GFP_KERNEL);
1697                 /* Failed allocation, critical failure */
1698                 if (!rxdr->desc) {
1699                         dma_free_coherent(&pdev->dev, rxdr->size, olddesc,
1700                                           olddma);
1701                         e_err(probe, "Unable to allocate memory for the Rx "
1702                               "descriptor ring\n");
1703                         goto setup_rx_desc_die;
1704                 }
1705
1706                 if (!e1000_check_64k_bound(adapter, rxdr->desc, rxdr->size)) {
1707                         /* give up */
1708                         dma_free_coherent(&pdev->dev, rxdr->size, rxdr->desc,
1709                                           rxdr->dma);
1710                         dma_free_coherent(&pdev->dev, rxdr->size, olddesc,
1711                                           olddma);
1712                         e_err(probe, "Unable to allocate aligned memory for "
1713                               "the Rx descriptor ring\n");
1714                         goto setup_rx_desc_die;
1715                 } else {
1716                         /* Free old allocation, new allocation was successful */
1717                         dma_free_coherent(&pdev->dev, rxdr->size, olddesc,
1718                                           olddma);
1719                 }
1720         }
1721         memset(rxdr->desc, 0, rxdr->size);
1722
1723         rxdr->next_to_clean = 0;
1724         rxdr->next_to_use = 0;
1725         rxdr->rx_skb_top = NULL;
1726
1727         return 0;
1728 }
1729
1730 /**
1731  * e1000_setup_all_rx_resources - wrapper to allocate Rx resources
1732  *                                (Descriptors) for all queues
1733  * @adapter: board private structure
1734  *
1735  * Return 0 on success, negative on failure
1736  **/
1737
1738 int e1000_setup_all_rx_resources(struct e1000_adapter *adapter)
1739 {
1740         int i, err = 0;
1741
1742         for (i = 0; i < adapter->num_rx_queues; i++) {
1743                 err = e1000_setup_rx_resources(adapter, &adapter->rx_ring[i]);
1744                 if (err) {
1745                         e_err(probe, "Allocation for Rx Queue %u failed\n", i);
1746                         for (i-- ; i >= 0; i--)
1747                                 e1000_free_rx_resources(adapter,
1748                                                         &adapter->rx_ring[i]);
1749                         break;
1750                 }
1751         }
1752
1753         return err;
1754 }
1755
1756 /**
1757  * e1000_setup_rctl - configure the receive control registers
1758  * @adapter: Board private structure
1759  **/
1760 static void e1000_setup_rctl(struct e1000_adapter *adapter)
1761 {
1762         struct e1000_hw *hw = &adapter->hw;
1763         u32 rctl;
1764
1765         rctl = er32(RCTL);
1766
1767         rctl &= ~(3 << E1000_RCTL_MO_SHIFT);
1768
1769         rctl |= E1000_RCTL_EN | E1000_RCTL_BAM |
1770                 E1000_RCTL_LBM_NO | E1000_RCTL_RDMTS_HALF |
1771                 (hw->mc_filter_type << E1000_RCTL_MO_SHIFT);
1772
1773         if (hw->tbi_compatibility_on == 1)
1774                 rctl |= E1000_RCTL_SBP;
1775         else
1776                 rctl &= ~E1000_RCTL_SBP;
1777
1778         if (adapter->netdev->mtu <= ETH_DATA_LEN)
1779                 rctl &= ~E1000_RCTL_LPE;
1780         else
1781                 rctl |= E1000_RCTL_LPE;
1782
1783         /* Setup buffer sizes */
1784         rctl &= ~E1000_RCTL_SZ_4096;
1785         rctl |= E1000_RCTL_BSEX;
1786         switch (adapter->rx_buffer_len) {
1787                 case E1000_RXBUFFER_2048:
1788                 default:
1789                         rctl |= E1000_RCTL_SZ_2048;
1790                         rctl &= ~E1000_RCTL_BSEX;
1791                         break;
1792                 case E1000_RXBUFFER_4096:
1793                         rctl |= E1000_RCTL_SZ_4096;
1794                         break;
1795                 case E1000_RXBUFFER_8192:
1796                         rctl |= E1000_RCTL_SZ_8192;
1797                         break;
1798                 case E1000_RXBUFFER_16384:
1799                         rctl |= E1000_RCTL_SZ_16384;
1800                         break;
1801         }
1802
1803         ew32(RCTL, rctl);
1804 }
1805
1806 /**
1807  * e1000_configure_rx - Configure 8254x Receive Unit after Reset
1808  * @adapter: board private structure
1809  *
1810  * Configure the Rx unit of the MAC after a reset.
1811  **/
1812
1813 static void e1000_configure_rx(struct e1000_adapter *adapter)
1814 {
1815         u64 rdba;
1816         struct e1000_hw *hw = &adapter->hw;
1817         u32 rdlen, rctl, rxcsum;
1818
1819         if (adapter->netdev->mtu > ETH_DATA_LEN) {
1820                 rdlen = adapter->rx_ring[0].count *
1821                         sizeof(struct e1000_rx_desc);
1822                 adapter->clean_rx = e1000_clean_jumbo_rx_irq;
1823                 adapter->alloc_rx_buf = e1000_alloc_jumbo_rx_buffers;
1824         } else {
1825                 rdlen = adapter->rx_ring[0].count *
1826                         sizeof(struct e1000_rx_desc);
1827                 adapter->clean_rx = e1000_clean_rx_irq;
1828                 adapter->alloc_rx_buf = e1000_alloc_rx_buffers;
1829         }
1830
1831         /* disable receives while setting up the descriptors */
1832         rctl = er32(RCTL);
1833         ew32(RCTL, rctl & ~E1000_RCTL_EN);
1834
1835         /* set the Receive Delay Timer Register */
1836         ew32(RDTR, adapter->rx_int_delay);
1837
1838         if (hw->mac_type >= e1000_82540) {
1839                 ew32(RADV, adapter->rx_abs_int_delay);
1840                 if (adapter->itr_setting != 0)
1841                         ew32(ITR, 1000000000 / (adapter->itr * 256));
1842         }
1843
1844         /* Setup the HW Rx Head and Tail Descriptor Pointers and
1845          * the Base and Length of the Rx Descriptor Ring */
1846         switch (adapter->num_rx_queues) {
1847         case 1:
1848         default:
1849                 rdba = adapter->rx_ring[0].dma;
1850                 ew32(RDLEN, rdlen);
1851                 ew32(RDBAH, (rdba >> 32));
1852                 ew32(RDBAL, (rdba & 0x00000000ffffffffULL));
1853                 ew32(RDT, 0);
1854                 ew32(RDH, 0);
1855                 adapter->rx_ring[0].rdh = ((hw->mac_type >= e1000_82543) ? E1000_RDH : E1000_82542_RDH);
1856                 adapter->rx_ring[0].rdt = ((hw->mac_type >= e1000_82543) ? E1000_RDT : E1000_82542_RDT);
1857                 break;
1858         }
1859
1860         /* Enable 82543 Receive Checksum Offload for TCP and UDP */
1861         if (hw->mac_type >= e1000_82543) {
1862                 rxcsum = er32(RXCSUM);
1863                 if (adapter->rx_csum)
1864                         rxcsum |= E1000_RXCSUM_TUOFL;
1865                 else
1866                         /* don't need to clear IPPCSE as it defaults to 0 */
1867                         rxcsum &= ~E1000_RXCSUM_TUOFL;
1868                 ew32(RXCSUM, rxcsum);
1869         }
1870
1871         /* Enable Receives */
1872         ew32(RCTL, rctl);
1873 }
1874
1875 /**
1876  * e1000_free_tx_resources - Free Tx Resources per Queue
1877  * @adapter: board private structure
1878  * @tx_ring: Tx descriptor ring for a specific queue
1879  *
1880  * Free all transmit software resources
1881  **/
1882
1883 static void e1000_free_tx_resources(struct e1000_adapter *adapter,
1884                                     struct e1000_tx_ring *tx_ring)
1885 {
1886         struct pci_dev *pdev = adapter->pdev;
1887
1888         e1000_clean_tx_ring(adapter, tx_ring);
1889
1890         vfree(tx_ring->buffer_info);
1891         tx_ring->buffer_info = NULL;
1892
1893         dma_free_coherent(&pdev->dev, tx_ring->size, tx_ring->desc,
1894                           tx_ring->dma);
1895
1896         tx_ring->desc = NULL;
1897 }
1898
1899 /**
1900  * e1000_free_all_tx_resources - Free Tx Resources for All Queues
1901  * @adapter: board private structure
1902  *
1903  * Free all transmit software resources
1904  **/
1905
1906 void e1000_free_all_tx_resources(struct e1000_adapter *adapter)
1907 {
1908         int i;
1909
1910         for (i = 0; i < adapter->num_tx_queues; i++)
1911                 e1000_free_tx_resources(adapter, &adapter->tx_ring[i]);
1912 }
1913
1914 static void e1000_unmap_and_free_tx_resource(struct e1000_adapter *adapter,
1915                                              struct e1000_buffer *buffer_info)
1916 {
1917         if (buffer_info->dma) {
1918                 if (buffer_info->mapped_as_page)
1919                         dma_unmap_page(&adapter->pdev->dev, buffer_info->dma,
1920                                        buffer_info->length, DMA_TO_DEVICE);
1921                 else
1922                         dma_unmap_single(&adapter->pdev->dev, buffer_info->dma,
1923                                          buffer_info->length,
1924                                          DMA_TO_DEVICE);
1925                 buffer_info->dma = 0;
1926         }
1927         if (buffer_info->skb) {
1928                 dev_kfree_skb_any(buffer_info->skb);
1929                 buffer_info->skb = NULL;
1930         }
1931         buffer_info->time_stamp = 0;
1932         /* buffer_info must be completely set up in the transmit path */
1933 }
1934
1935 /**
1936  * e1000_clean_tx_ring - Free Tx Buffers
1937  * @adapter: board private structure
1938  * @tx_ring: ring to be cleaned
1939  **/
1940
1941 static void e1000_clean_tx_ring(struct e1000_adapter *adapter,
1942                                 struct e1000_tx_ring *tx_ring)
1943 {
1944         struct e1000_hw *hw = &adapter->hw;
1945         struct e1000_buffer *buffer_info;
1946         unsigned long size;
1947         unsigned int i;
1948
1949         /* Free all the Tx ring sk_buffs */
1950
1951         for (i = 0; i < tx_ring->count; i++) {
1952                 buffer_info = &tx_ring->buffer_info[i];
1953                 e1000_unmap_and_free_tx_resource(adapter, buffer_info);
1954         }
1955
1956         size = sizeof(struct e1000_buffer) * tx_ring->count;
1957         memset(tx_ring->buffer_info, 0, size);
1958
1959         /* Zero out the descriptor ring */
1960
1961         memset(tx_ring->desc, 0, tx_ring->size);
1962
1963         tx_ring->next_to_use = 0;
1964         tx_ring->next_to_clean = 0;
1965         tx_ring->last_tx_tso = 0;
1966
1967         writel(0, hw->hw_addr + tx_ring->tdh);
1968         writel(0, hw->hw_addr + tx_ring->tdt);
1969 }
1970
1971 /**
1972  * e1000_clean_all_tx_rings - Free Tx Buffers for all queues
1973  * @adapter: board private structure
1974  **/
1975
1976 static void e1000_clean_all_tx_rings(struct e1000_adapter *adapter)
1977 {
1978         int i;
1979
1980         for (i = 0; i < adapter->num_tx_queues; i++)
1981                 e1000_clean_tx_ring(adapter, &adapter->tx_ring[i]);
1982 }
1983
1984 /**
1985  * e1000_free_rx_resources - Free Rx Resources
1986  * @adapter: board private structure
1987  * @rx_ring: ring to clean the resources from
1988  *
1989  * Free all receive software resources
1990  **/
1991
1992 static void e1000_free_rx_resources(struct e1000_adapter *adapter,
1993                                     struct e1000_rx_ring *rx_ring)
1994 {
1995         struct pci_dev *pdev = adapter->pdev;
1996
1997         e1000_clean_rx_ring(adapter, rx_ring);
1998
1999         vfree(rx_ring->buffer_info);
2000         rx_ring->buffer_info = NULL;
2001
2002         dma_free_coherent(&pdev->dev, rx_ring->size, rx_ring->desc,
2003                           rx_ring->dma);
2004
2005         rx_ring->desc = NULL;
2006 }
2007
2008 /**
2009  * e1000_free_all_rx_resources - Free Rx Resources for All Queues
2010  * @adapter: board private structure
2011  *
2012  * Free all receive software resources
2013  **/
2014
2015 void e1000_free_all_rx_resources(struct e1000_adapter *adapter)
2016 {
2017         int i;
2018
2019         for (i = 0; i < adapter->num_rx_queues; i++)
2020                 e1000_free_rx_resources(adapter, &adapter->rx_ring[i]);
2021 }
2022
2023 /**
2024  * e1000_clean_rx_ring - Free Rx Buffers per Queue
2025  * @adapter: board private structure
2026  * @rx_ring: ring to free buffers from
2027  **/
2028
2029 static void e1000_clean_rx_ring(struct e1000_adapter *adapter,
2030                                 struct e1000_rx_ring *rx_ring)
2031 {
2032         struct e1000_hw *hw = &adapter->hw;
2033         struct e1000_buffer *buffer_info;
2034         struct pci_dev *pdev = adapter->pdev;
2035         unsigned long size;
2036         unsigned int i;
2037
2038         /* Free all the Rx ring sk_buffs */
2039         for (i = 0; i < rx_ring->count; i++) {
2040                 buffer_info = &rx_ring->buffer_info[i];
2041                 if (buffer_info->dma &&
2042                     adapter->clean_rx == e1000_clean_rx_irq) {
2043                         dma_unmap_single(&pdev->dev, buffer_info->dma,
2044                                          buffer_info->length,
2045                                          DMA_FROM_DEVICE);
2046                 } else if (buffer_info->dma &&
2047                            adapter->clean_rx == e1000_clean_jumbo_rx_irq) {
2048                         dma_unmap_page(&pdev->dev, buffer_info->dma,
2049                                        buffer_info->length,
2050                                        DMA_FROM_DEVICE);
2051                 }
2052
2053                 buffer_info->dma = 0;
2054                 if (buffer_info->page) {
2055                         put_page(buffer_info->page);
2056                         buffer_info->page = NULL;
2057                 }
2058                 if (buffer_info->skb) {
2059                         dev_kfree_skb(buffer_info->skb);
2060                         buffer_info->skb = NULL;
2061                 }
2062         }
2063
2064         /* there also may be some cached data from a chained receive */
2065         if (rx_ring->rx_skb_top) {
2066                 dev_kfree_skb(rx_ring->rx_skb_top);
2067                 rx_ring->rx_skb_top = NULL;
2068         }
2069
2070         size = sizeof(struct e1000_buffer) * rx_ring->count;
2071         memset(rx_ring->buffer_info, 0, size);
2072
2073         /* Zero out the descriptor ring */
2074         memset(rx_ring->desc, 0, rx_ring->size);
2075
2076         rx_ring->next_to_clean = 0;
2077         rx_ring->next_to_use = 0;
2078
2079         writel(0, hw->hw_addr + rx_ring->rdh);
2080         writel(0, hw->hw_addr + rx_ring->rdt);
2081 }
2082
2083 /**
2084  * e1000_clean_all_rx_rings - Free Rx Buffers for all queues
2085  * @adapter: board private structure
2086  **/
2087
2088 static void e1000_clean_all_rx_rings(struct e1000_adapter *adapter)
2089 {
2090         int i;
2091
2092         for (i = 0; i < adapter->num_rx_queues; i++)
2093                 e1000_clean_rx_ring(adapter, &adapter->rx_ring[i]);
2094 }
2095
2096 /* The 82542 2.0 (revision 2) needs to have the receive unit in reset
2097  * and memory write and invalidate disabled for certain operations
2098  */
2099 static void e1000_enter_82542_rst(struct e1000_adapter *adapter)
2100 {
2101         struct e1000_hw *hw = &adapter->hw;
2102         struct net_device *netdev = adapter->netdev;
2103         u32 rctl;
2104
2105         e1000_pci_clear_mwi(hw);
2106
2107         rctl = er32(RCTL);
2108         rctl |= E1000_RCTL_RST;
2109         ew32(RCTL, rctl);
2110         E1000_WRITE_FLUSH();
2111         mdelay(5);
2112
2113         if (netif_running(netdev))
2114                 e1000_clean_all_rx_rings(adapter);
2115 }
2116
2117 static void e1000_leave_82542_rst(struct e1000_adapter *adapter)
2118 {
2119         struct e1000_hw *hw = &adapter->hw;
2120         struct net_device *netdev = adapter->netdev;
2121         u32 rctl;
2122
2123         rctl = er32(RCTL);
2124         rctl &= ~E1000_RCTL_RST;
2125         ew32(RCTL, rctl);
2126         E1000_WRITE_FLUSH();
2127         mdelay(5);
2128
2129         if (hw->pci_cmd_word & PCI_COMMAND_INVALIDATE)
2130                 e1000_pci_set_mwi(hw);
2131
2132         if (netif_running(netdev)) {
2133                 /* No need to loop, because 82542 supports only 1 queue */
2134                 struct e1000_rx_ring *ring = &adapter->rx_ring[0];
2135                 e1000_configure_rx(adapter);
2136                 adapter->alloc_rx_buf(adapter, ring, E1000_DESC_UNUSED(ring));
2137         }
2138 }
2139
2140 /**
2141  * e1000_set_mac - Change the Ethernet Address of the NIC
2142  * @netdev: network interface device structure
2143  * @p: pointer to an address structure
2144  *
2145  * Returns 0 on success, negative on failure
2146  **/
2147
2148 static int e1000_set_mac(struct net_device *netdev, void *p)
2149 {
2150         struct e1000_adapter *adapter = netdev_priv(netdev);
2151         struct e1000_hw *hw = &adapter->hw;
2152         struct sockaddr *addr = p;
2153
2154         if (!is_valid_ether_addr(addr->sa_data))
2155                 return -EADDRNOTAVAIL;
2156
2157         /* 82542 2.0 needs to be in reset to write receive address registers */
2158
2159         if (hw->mac_type == e1000_82542_rev2_0)
2160                 e1000_enter_82542_rst(adapter);
2161
2162         memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
2163         memcpy(hw->mac_addr, addr->sa_data, netdev->addr_len);
2164
2165         e1000_rar_set(hw, hw->mac_addr, 0);
2166
2167         if (hw->mac_type == e1000_82542_rev2_0)
2168                 e1000_leave_82542_rst(adapter);
2169
2170         return 0;
2171 }
2172
2173 /**
2174  * e1000_set_rx_mode - Secondary Unicast, Multicast and Promiscuous mode set
2175  * @netdev: network interface device structure
2176  *
2177  * The set_rx_mode entry point is called whenever the unicast or multicast
2178  * address lists or the network interface flags are updated. This routine is
2179  * responsible for configuring the hardware for proper unicast, multicast,
2180  * promiscuous mode, and all-multi behavior.
2181  **/
2182
2183 static void e1000_set_rx_mode(struct net_device *netdev)
2184 {
2185         struct e1000_adapter *adapter = netdev_priv(netdev);
2186         struct e1000_hw *hw = &adapter->hw;
2187         struct netdev_hw_addr *ha;
2188         bool use_uc = false;
2189         u32 rctl;
2190         u32 hash_value;
2191         int i, rar_entries = E1000_RAR_ENTRIES;
2192         int mta_reg_count = E1000_NUM_MTA_REGISTERS;
2193         u32 *mcarray = kcalloc(mta_reg_count, sizeof(u32), GFP_ATOMIC);
2194
2195         if (!mcarray) {
2196                 e_err(probe, "memory allocation failed\n");
2197                 return;
2198         }
2199
2200         /* Check for Promiscuous and All Multicast modes */
2201
2202         rctl = er32(RCTL);
2203
2204         if (netdev->flags & IFF_PROMISC) {
2205                 rctl |= (E1000_RCTL_UPE | E1000_RCTL_MPE);
2206                 rctl &= ~E1000_RCTL_VFE;
2207         } else {
2208                 if (netdev->flags & IFF_ALLMULTI)
2209                         rctl |= E1000_RCTL_MPE;
2210                 else
2211                         rctl &= ~E1000_RCTL_MPE;
2212                 /* Enable VLAN filter if there is a VLAN */
2213                 if (adapter->vlgrp)
2214                         rctl |= E1000_RCTL_VFE;
2215         }
2216
2217         if (netdev_uc_count(netdev) > rar_entries - 1) {
2218                 rctl |= E1000_RCTL_UPE;
2219         } else if (!(netdev->flags & IFF_PROMISC)) {
2220                 rctl &= ~E1000_RCTL_UPE;
2221                 use_uc = true;
2222         }
2223
2224         ew32(RCTL, rctl);
2225
2226         /* 82542 2.0 needs to be in reset to write receive address registers */
2227
2228         if (hw->mac_type == e1000_82542_rev2_0)
2229                 e1000_enter_82542_rst(adapter);
2230
2231         /* load the first 14 addresses into the exact filters 1-14. Unicast
2232          * addresses take precedence to avoid disabling unicast filtering
2233          * when possible.
2234          *
2235          * RAR 0 is used for the station MAC address
2236          * if there are not 14 addresses, go ahead and clear the filters
2237          */
2238         i = 1;
2239         if (use_uc)
2240                 netdev_for_each_uc_addr(ha, netdev) {
2241                         if (i == rar_entries)
2242                                 break;
2243                         e1000_rar_set(hw, ha->addr, i++);
2244                 }
2245
2246         netdev_for_each_mc_addr(ha, netdev) {
2247                 if (i == rar_entries) {
2248                         /* load any remaining addresses into the hash table */
2249                         u32 hash_reg, hash_bit, mta;
2250                         hash_value = e1000_hash_mc_addr(hw, ha->addr);
2251                         hash_reg = (hash_value >> 5) & 0x7F;
2252                         hash_bit = hash_value & 0x1F;
2253                         mta = (1 << hash_bit);
2254                         mcarray[hash_reg] |= mta;
2255                 } else {
2256                         e1000_rar_set(hw, ha->addr, i++);
2257                 }
2258         }
2259
2260         for (; i < rar_entries; i++) {
2261                 E1000_WRITE_REG_ARRAY(hw, RA, i << 1, 0);
2262                 E1000_WRITE_FLUSH();
2263                 E1000_WRITE_REG_ARRAY(hw, RA, (i << 1) + 1, 0);
2264                 E1000_WRITE_FLUSH();
2265         }
2266
2267         /* write the hash table completely, write from bottom to avoid
2268          * both stupid write combining chipsets, and flushing each write */
2269         for (i = mta_reg_count - 1; i >= 0 ; i--) {
2270                 /*
2271                  * If we are on an 82544 has an errata where writing odd
2272                  * offsets overwrites the previous even offset, but writing
2273                  * backwards over the range solves the issue by always
2274                  * writing the odd offset first
2275                  */
2276                 E1000_WRITE_REG_ARRAY(hw, MTA, i, mcarray[i]);
2277         }
2278         E1000_WRITE_FLUSH();
2279
2280         if (hw->mac_type == e1000_82542_rev2_0)
2281                 e1000_leave_82542_rst(adapter);
2282
2283         kfree(mcarray);
2284 }
2285
2286 /* Need to wait a few seconds after link up to get diagnostic information from
2287  * the phy */
2288
2289 static void e1000_update_phy_info(unsigned long data)
2290 {
2291         struct e1000_adapter *adapter = (struct e1000_adapter *)data;
2292         schedule_work(&adapter->phy_info_task);
2293 }
2294
2295 static void e1000_update_phy_info_task(struct work_struct *work)
2296 {
2297         struct e1000_adapter *adapter = container_of(work,
2298                                                      struct e1000_adapter,
2299                                                      phy_info_task);
2300         struct e1000_hw *hw = &adapter->hw;
2301
2302         rtnl_lock();
2303         e1000_phy_get_info(hw, &adapter->phy_info);
2304         rtnl_unlock();
2305 }
2306
2307 /**
2308  * e1000_82547_tx_fifo_stall - Timer Call-back
2309  * @data: pointer to adapter cast into an unsigned long
2310  **/
2311 static void e1000_82547_tx_fifo_stall(unsigned long data)
2312 {
2313         struct e1000_adapter *adapter = (struct e1000_adapter *)data;
2314         schedule_work(&adapter->fifo_stall_task);
2315 }
2316
2317 /**
2318  * e1000_82547_tx_fifo_stall_task - task to complete work
2319  * @work: work struct contained inside adapter struct
2320  **/
2321 static void e1000_82547_tx_fifo_stall_task(struct work_struct *work)
2322 {
2323         struct e1000_adapter *adapter = container_of(work,
2324                                                      struct e1000_adapter,
2325                                                      fifo_stall_task);
2326         struct e1000_hw *hw = &adapter->hw;
2327         struct net_device *netdev = adapter->netdev;
2328         u32 tctl;
2329
2330         rtnl_lock();
2331         if (atomic_read(&adapter->tx_fifo_stall)) {
2332                 if ((er32(TDT) == er32(TDH)) &&
2333                    (er32(TDFT) == er32(TDFH)) &&
2334                    (er32(TDFTS) == er32(TDFHS))) {
2335                         tctl = er32(TCTL);
2336                         ew32(TCTL, tctl & ~E1000_TCTL_EN);
2337                         ew32(TDFT, adapter->tx_head_addr);
2338                         ew32(TDFH, adapter->tx_head_addr);
2339                         ew32(TDFTS, adapter->tx_head_addr);
2340                         ew32(TDFHS, adapter->tx_head_addr);
2341                         ew32(TCTL, tctl);
2342                         E1000_WRITE_FLUSH();
2343
2344                         adapter->tx_fifo_head = 0;
2345                         atomic_set(&adapter->tx_fifo_stall, 0);
2346                         netif_wake_queue(netdev);
2347                 } else if (!test_bit(__E1000_DOWN, &adapter->flags)) {
2348                         mod_timer(&adapter->tx_fifo_stall_timer, jiffies + 1);
2349                 }
2350         }
2351         rtnl_unlock();
2352 }
2353
2354 bool e1000_has_link(struct e1000_adapter *adapter)
2355 {
2356         struct e1000_hw *hw = &adapter->hw;
2357         bool link_active = false;
2358
2359         /* get_link_status is set on LSC (link status) interrupt or
2360          * rx sequence error interrupt.  get_link_status will stay
2361          * false until the e1000_check_for_link establishes link
2362          * for copper adapters ONLY
2363          */
2364         switch (hw->media_type) {
2365         case e1000_media_type_copper:
2366                 if (hw->get_link_status) {
2367                         e1000_check_for_link(hw);
2368                         link_active = !hw->get_link_status;
2369                 } else {
2370                         link_active = true;
2371                 }
2372                 break;
2373         case e1000_media_type_fiber:
2374                 e1000_check_for_link(hw);
2375                 link_active = !!(er32(STATUS) & E1000_STATUS_LU);
2376                 break;
2377         case e1000_media_type_internal_serdes:
2378                 e1000_check_for_link(hw);
2379                 link_active = hw->serdes_has_link;
2380                 break;
2381         default:
2382                 break;
2383         }
2384
2385         return link_active;
2386 }
2387
2388 /**
2389  * e1000_watchdog - Timer Call-back
2390  * @data: pointer to adapter cast into an unsigned long
2391  **/
2392 static void e1000_watchdog(unsigned long data)
2393 {
2394         struct e1000_adapter *adapter = (struct e1000_adapter *)data;
2395         struct e1000_hw *hw = &adapter->hw;
2396         struct net_device *netdev = adapter->netdev;
2397         struct e1000_tx_ring *txdr = adapter->tx_ring;
2398         u32 link, tctl;
2399
2400         link = e1000_has_link(adapter);
2401         if ((netif_carrier_ok(netdev)) && link)
2402                 goto link_up;
2403
2404         if (link) {
2405                 if (!netif_carrier_ok(netdev)) {
2406                         u32 ctrl;
2407                         bool txb2b = true;
2408                         /* update snapshot of PHY registers on LSC */
2409                         e1000_get_speed_and_duplex(hw,
2410                                                    &adapter->link_speed,
2411                                                    &adapter->link_duplex);
2412
2413                         ctrl = er32(CTRL);
2414                         pr_info("%s NIC Link is Up %d Mbps %s, "
2415                                 "Flow Control: %s\n",
2416                                 netdev->name,
2417                                 adapter->link_speed,
2418                                 adapter->link_duplex == FULL_DUPLEX ?
2419                                 "Full Duplex" : "Half Duplex",
2420                                 ((ctrl & E1000_CTRL_TFCE) && (ctrl &
2421                                 E1000_CTRL_RFCE)) ? "RX/TX" : ((ctrl &
2422                                 E1000_CTRL_RFCE) ? "RX" : ((ctrl &
2423                                 E1000_CTRL_TFCE) ? "TX" : "None")));
2424
2425                         /* adjust timeout factor according to speed/duplex */
2426                         adapter->tx_timeout_factor = 1;
2427                         switch (adapter->link_speed) {
2428                         case SPEED_10:
2429                                 txb2b = false;
2430                                 adapter->tx_timeout_factor = 16;
2431                                 break;
2432                         case SPEED_100:
2433                                 txb2b = false;
2434                                 /* maybe add some timeout factor ? */
2435                                 break;
2436                         }
2437
2438                         /* enable transmits in the hardware */
2439                         tctl = er32(TCTL);
2440                         tctl |= E1000_TCTL_EN;
2441                         ew32(TCTL, tctl);
2442
2443                         netif_carrier_on(netdev);
2444                         if (!test_bit(__E1000_DOWN, &adapter->flags))
2445                                 mod_timer(&adapter->phy_info_timer,
2446                                           round_jiffies(jiffies + 2 * HZ));
2447                         adapter->smartspeed = 0;
2448                 }
2449         } else {
2450                 if (netif_carrier_ok(netdev)) {
2451                         adapter->link_speed = 0;
2452                         adapter->link_duplex = 0;
2453                         pr_info("%s NIC Link is Down\n",
2454                                 netdev->name);
2455                         netif_carrier_off(netdev);
2456
2457                         if (!test_bit(__E1000_DOWN, &adapter->flags))
2458                                 mod_timer(&adapter->phy_info_timer,
2459                                           round_jiffies(jiffies + 2 * HZ));
2460                 }
2461
2462                 e1000_smartspeed(adapter);
2463         }
2464
2465 link_up:
2466         e1000_update_stats(adapter);
2467
2468         hw->tx_packet_delta = adapter->stats.tpt - adapter->tpt_old;
2469         adapter->tpt_old = adapter->stats.tpt;
2470         hw->collision_delta = adapter->stats.colc - adapter->colc_old;
2471         adapter->colc_old = adapter->stats.colc;
2472
2473         adapter->gorcl = adapter->stats.gorcl - adapter->gorcl_old;
2474         adapter->gorcl_old = adapter->stats.gorcl;
2475         adapter->gotcl = adapter->stats.gotcl - adapter->gotcl_old;
2476         adapter->gotcl_old = adapter->stats.gotcl;
2477
2478         e1000_update_adaptive(hw);
2479
2480         if (!netif_carrier_ok(netdev)) {
2481                 if (E1000_DESC_UNUSED(txdr) + 1 < txdr->count) {
2482                         /* We've lost link, so the controller stops DMA,
2483                          * but we've got queued Tx work that's never going
2484                          * to get done, so reset controller to flush Tx.
2485                          * (Do the reset outside of interrupt context). */
2486                         adapter->tx_timeout_count++;
2487                         schedule_work(&adapter->reset_task);
2488                         /* return immediately since reset is imminent */
2489                         return;
2490                 }
2491         }
2492
2493         /* Simple mode for Interrupt Throttle Rate (ITR) */
2494         if (hw->mac_type >= e1000_82540 && adapter->itr_setting == 4) {
2495                 /*
2496                  * Symmetric Tx/Rx gets a reduced ITR=2000;
2497                  * Total asymmetrical Tx or Rx gets ITR=8000;
2498                  * everyone else is between 2000-8000.
2499                  */
2500                 u32 goc = (adapter->gotcl + adapter->gorcl) / 10000;
2501                 u32 dif = (adapter->gotcl > adapter->gorcl ?
2502                             adapter->gotcl - adapter->gorcl :
2503                             adapter->gorcl - adapter->gotcl) / 10000;
2504                 u32 itr = goc > 0 ? (dif * 6000 / goc + 2000) : 8000;
2505
2506                 ew32(ITR, 1000000000 / (itr * 256));
2507         }
2508
2509         /* Cause software interrupt to ensure rx ring is cleaned */
2510         ew32(ICS, E1000_ICS_RXDMT0);
2511
2512         /* Force detection of hung controller every watchdog period */
2513         adapter->detect_tx_hung = true;
2514
2515         /* Reset the timer */
2516         if (!test_bit(__E1000_DOWN, &adapter->flags))
2517                 mod_timer(&adapter->watchdog_timer,
2518                           round_jiffies(jiffies + 2 * HZ));
2519 }
2520
2521 enum latency_range {
2522         lowest_latency = 0,
2523         low_latency = 1,
2524         bulk_latency = 2,
2525         latency_invalid = 255
2526 };
2527
2528 /**
2529  * e1000_update_itr - update the dynamic ITR value based on statistics
2530  * @adapter: pointer to adapter
2531  * @itr_setting: current adapter->itr
2532  * @packets: the number of packets during this measurement interval
2533  * @bytes: the number of bytes during this measurement interval
2534  *
2535  *      Stores a new ITR value based on packets and byte
2536  *      counts during the last interrupt.  The advantage of per interrupt
2537  *      computation is faster updates and more accurate ITR for the current
2538  *      traffic pattern.  Constants in this function were computed
2539  *      based on theoretical maximum wire speed and thresholds were set based
2540  *      on testing data as well as attempting to minimize response time
2541  *      while increasing bulk throughput.
2542  *      this functionality is controlled by the InterruptThrottleRate module
2543  *      parameter (see e1000_param.c)
2544  **/
2545 static unsigned int e1000_update_itr(struct e1000_adapter *adapter,
2546                                      u16 itr_setting, int packets, int bytes)
2547 {
2548         unsigned int retval = itr_setting;
2549         struct e1000_hw *hw = &adapter->hw;
2550
2551         if (unlikely(hw->mac_type < e1000_82540))
2552                 goto update_itr_done;
2553
2554         if (packets == 0)
2555                 goto update_itr_done;
2556
2557         switch (itr_setting) {
2558         case lowest_latency:
2559                 /* jumbo frames get bulk treatment*/
2560                 if (bytes/packets > 8000)
2561                         retval = bulk_latency;
2562                 else if ((packets < 5) && (bytes > 512))
2563                         retval = low_latency;
2564                 break;
2565         case low_latency:  /* 50 usec aka 20000 ints/s */
2566                 if (bytes > 10000) {
2567                         /* jumbo frames need bulk latency setting */
2568                         if (bytes/packets > 8000)
2569                                 retval = bulk_latency;
2570                         else if ((packets < 10) || ((bytes/packets) > 1200))
2571                                 retval = bulk_latency;
2572                         else if ((packets > 35))
2573                                 retval = lowest_latency;
2574                 } else if (bytes/packets > 2000)
2575                         retval = bulk_latency;
2576                 else if (packets <= 2 && bytes < 512)
2577                         retval = lowest_latency;
2578                 break;
2579         case bulk_latency: /* 250 usec aka 4000 ints/s */
2580                 if (bytes > 25000) {
2581                         if (packets > 35)
2582                                 retval = low_latency;
2583                 } else if (bytes < 6000) {
2584                         retval = low_latency;
2585                 }
2586                 break;
2587         }
2588
2589 update_itr_done:
2590         return retval;
2591 }
2592
2593 static void e1000_set_itr(struct e1000_adapter *adapter)
2594 {
2595         struct e1000_hw *hw = &adapter->hw;
2596         u16 current_itr;
2597         u32 new_itr = adapter->itr;
2598
2599         if (unlikely(hw->mac_type < e1000_82540))
2600                 return;
2601
2602         /* for non-gigabit speeds, just fix the interrupt rate at 4000 */
2603         if (unlikely(adapter->link_speed != SPEED_1000)) {
2604                 current_itr = 0;
2605                 new_itr = 4000;
2606                 goto set_itr_now;
2607         }
2608
2609         adapter->tx_itr = e1000_update_itr(adapter,
2610                                     adapter->tx_itr,
2611                                     adapter->total_tx_packets,
2612                                     adapter->total_tx_bytes);
2613         /* conservative mode (itr 3) eliminates the lowest_latency setting */
2614         if (adapter->itr_setting == 3 && adapter->tx_itr == lowest_latency)
2615                 adapter->tx_itr = low_latency;
2616
2617         adapter->rx_itr = e1000_update_itr(adapter,
2618                                     adapter->rx_itr,
2619                                     adapter->total_rx_packets,
2620                                     adapter->total_rx_bytes);
2621         /* conservative mode (itr 3) eliminates the lowest_latency setting */
2622         if (adapter->itr_setting == 3 && adapter->rx_itr == lowest_latency)
2623                 adapter->rx_itr = low_latency;
2624
2625         current_itr = max(adapter->rx_itr, adapter->tx_itr);
2626
2627         switch (current_itr) {
2628         /* counts and packets in update_itr are dependent on these numbers */
2629         case lowest_latency:
2630                 new_itr = 70000;
2631                 break;
2632         case low_latency:
2633                 new_itr = 20000; /* aka hwitr = ~200 */
2634                 break;
2635         case bulk_latency:
2636                 new_itr = 4000;
2637                 break;
2638         default:
2639                 break;
2640         }
2641
2642 set_itr_now:
2643         if (new_itr != adapter->itr) {
2644                 /* this attempts to bias the interrupt rate towards Bulk
2645                  * by adding intermediate steps when interrupt rate is
2646                  * increasing */
2647                 new_itr = new_itr > adapter->itr ?
2648                              min(adapter->itr + (new_itr >> 2), new_itr) :
2649                              new_itr;
2650                 adapter->itr = new_itr;
2651                 ew32(ITR, 1000000000 / (new_itr * 256));
2652         }
2653 }
2654
2655 #define E1000_TX_FLAGS_CSUM             0x00000001
2656 #define E1000_TX_FLAGS_VLAN             0x00000002
2657 #define E1000_TX_FLAGS_TSO              0x00000004
2658 #define E1000_TX_FLAGS_IPV4             0x00000008
2659 #define E1000_TX_FLAGS_VLAN_MASK        0xffff0000
2660 #define E1000_TX_FLAGS_VLAN_SHIFT       16
2661
2662 static int e1000_tso(struct e1000_adapter *adapter,
2663                      struct e1000_tx_ring *tx_ring, struct sk_buff *skb)
2664 {
2665         struct e1000_context_desc *context_desc;
2666         struct e1000_buffer *buffer_info;
2667         unsigned int i;
2668         u32 cmd_length = 0;
2669         u16 ipcse = 0, tucse, mss;
2670         u8 ipcss, ipcso, tucss, tucso, hdr_len;
2671         int err;
2672
2673         if (skb_is_gso(skb)) {
2674                 if (skb_header_cloned(skb)) {
2675                         err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2676                         if (err)
2677                                 return err;
2678                 }
2679
2680                 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
2681                 mss = skb_shinfo(skb)->gso_size;
2682                 if (skb->protocol == htons(ETH_P_IP)) {
2683                         struct iphdr *iph = ip_hdr(skb);
2684                         iph->tot_len = 0;
2685                         iph->check = 0;
2686                         tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
2687                                                                  iph->daddr, 0,
2688                                                                  IPPROTO_TCP,
2689                                                                  0);
2690                         cmd_length = E1000_TXD_CMD_IP;
2691                         ipcse = skb_transport_offset(skb) - 1;
2692                 } else if (skb->protocol == htons(ETH_P_IPV6)) {
2693                         ipv6_hdr(skb)->payload_len = 0;
2694                         tcp_hdr(skb)->check =
2695                                 ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
2696                                                  &ipv6_hdr(skb)->daddr,
2697                                                  0, IPPROTO_TCP, 0);
2698                         ipcse = 0;
2699                 }
2700                 ipcss = skb_network_offset(skb);
2701                 ipcso = (void *)&(ip_hdr(skb)->check) - (void *)skb->data;
2702                 tucss = skb_transport_offset(skb);
2703                 tucso = (void *)&(tcp_hdr(skb)->check) - (void *)skb->data;
2704                 tucse = 0;
2705
2706                 cmd_length |= (E1000_TXD_CMD_DEXT | E1000_TXD_CMD_TSE |
2707                                E1000_TXD_CMD_TCP | (skb->len - (hdr_len)));
2708
2709                 i = tx_ring->next_to_use;
2710                 context_desc = E1000_CONTEXT_DESC(*tx_ring, i);
2711                 buffer_info = &tx_ring->buffer_info[i];
2712
2713                 context_desc->lower_setup.ip_fields.ipcss  = ipcss;
2714                 context_desc->lower_setup.ip_fields.ipcso  = ipcso;
2715                 context_desc->lower_setup.ip_fields.ipcse  = cpu_to_le16(ipcse);
2716                 context_desc->upper_setup.tcp_fields.tucss = tucss;
2717                 context_desc->upper_setup.tcp_fields.tucso = tucso;
2718                 context_desc->upper_setup.tcp_fields.tucse = cpu_to_le16(tucse);
2719                 context_desc->tcp_seg_setup.fields.mss     = cpu_to_le16(mss);
2720                 context_desc->tcp_seg_setup.fields.hdr_len = hdr_len;
2721                 context_desc->cmd_and_length = cpu_to_le32(cmd_length);
2722
2723                 buffer_info->time_stamp = jiffies;
2724                 buffer_info->next_to_watch = i;
2725
2726                 if (++i == tx_ring->count) i = 0;
2727                 tx_ring->next_to_use = i;
2728
2729                 return true;
2730         }
2731         return false;
2732 }
2733
2734 static bool e1000_tx_csum(struct e1000_adapter *adapter,
2735                           struct e1000_tx_ring *tx_ring, struct sk_buff *skb)
2736 {
2737         struct e1000_context_desc *context_desc;
2738         struct e1000_buffer *buffer_info;
2739         unsigned int i;
2740         u8 css;
2741         u32 cmd_len = E1000_TXD_CMD_DEXT;
2742
2743         if (skb->ip_summed != CHECKSUM_PARTIAL)
2744                 return false;
2745
2746         switch (skb->protocol) {
2747         case cpu_to_be16(ETH_P_IP):
2748                 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
2749                         cmd_len |= E1000_TXD_CMD_TCP;
2750                 break;
2751         case cpu_to_be16(ETH_P_IPV6):
2752                 /* XXX not handling all IPV6 headers */
2753                 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
2754                         cmd_len |= E1000_TXD_CMD_TCP;
2755                 break;
2756         default:
2757                 if (unlikely(net_ratelimit()))
2758                         e_warn(drv, "checksum_partial proto=%x!\n",
2759                                skb->protocol);
2760                 break;
2761         }
2762
2763         css = skb_checksum_start_offset(skb);
2764
2765         i = tx_ring->next_to_use;
2766         buffer_info = &tx_ring->buffer_info[i];
2767         context_desc = E1000_CONTEXT_DESC(*tx_ring, i);
2768
2769         context_desc->lower_setup.ip_config = 0;
2770         context_desc->upper_setup.tcp_fields.tucss = css;
2771         context_desc->upper_setup.tcp_fields.tucso =
2772                 css + skb->csum_offset;
2773         context_desc->upper_setup.tcp_fields.tucse = 0;
2774         context_desc->tcp_seg_setup.data = 0;
2775         context_desc->cmd_and_length = cpu_to_le32(cmd_len);
2776
2777         buffer_info->time_stamp = jiffies;
2778         buffer_info->next_to_watch = i;
2779
2780         if (unlikely(++i == tx_ring->count)) i = 0;
2781         tx_ring->next_to_use = i;
2782
2783         return true;
2784 }
2785
2786 #define E1000_MAX_TXD_PWR       12
2787 #define E1000_MAX_DATA_PER_TXD  (1<<E1000_MAX_TXD_PWR)
2788
2789 static int e1000_tx_map(struct e1000_adapter *adapter,
2790                         struct e1000_tx_ring *tx_ring,
2791                         struct sk_buff *skb, unsigned int first,
2792                         unsigned int max_per_txd, unsigned int nr_frags,
2793                         unsigned int mss)
2794 {
2795         struct e1000_hw *hw = &adapter->hw;
2796         struct pci_dev *pdev = adapter->pdev;
2797         struct e1000_buffer *buffer_info;
2798         unsigned int len = skb_headlen(skb);
2799         unsigned int offset = 0, size, count = 0, i;
2800         unsigned int f;
2801
2802         i = tx_ring->next_to_use;
2803
2804         while (len) {
2805                 buffer_info = &tx_ring->buffer_info[i];
2806                 size = min(len, max_per_txd);
2807                 /* Workaround for Controller erratum --
2808                  * descriptor for non-tso packet in a linear SKB that follows a
2809                  * tso gets written back prematurely before the data is fully
2810                  * DMA'd to the controller */
2811                 if (!skb->data_len && tx_ring->last_tx_tso &&
2812                     !skb_is_gso(skb)) {
2813                         tx_ring->last_tx_tso = 0;
2814                         size -= 4;
2815                 }
2816
2817                 /* Workaround for premature desc write-backs
2818                  * in TSO mode.  Append 4-byte sentinel desc */
2819                 if (unlikely(mss && !nr_frags && size == len && size > 8))
2820                         size -= 4;
2821                 /* work-around for errata 10 and it applies
2822                  * to all controllers in PCI-X mode
2823                  * The fix is to make sure that the first descriptor of a
2824                  * packet is smaller than 2048 - 16 - 16 (or 2016) bytes
2825                  */
2826                 if (unlikely((hw->bus_type == e1000_bus_type_pcix) &&
2827                                 (size > 2015) && count == 0))
2828                         size = 2015;
2829
2830                 /* Workaround for potential 82544 hang in PCI-X.  Avoid
2831                  * terminating buffers within evenly-aligned dwords. */
2832                 if (unlikely(adapter->pcix_82544 &&
2833                    !((unsigned long)(skb->data + offset + size - 1) & 4) &&
2834                    size > 4))
2835                         size -= 4;
2836
2837                 buffer_info->length = size;
2838                 /* set time_stamp *before* dma to help avoid a possible race */
2839                 buffer_info->time_stamp = jiffies;
2840                 buffer_info->mapped_as_page = false;
2841                 buffer_info->dma = dma_map_single(&pdev->dev,
2842                                                   skb->data + offset,
2843                                                   size, DMA_TO_DEVICE);
2844                 if (dma_mapping_error(&pdev->dev, buffer_info->dma))
2845                         goto dma_error;
2846                 buffer_info->next_to_watch = i;
2847
2848                 len -= size;
2849                 offset += size;
2850                 count++;
2851                 if (len) {
2852                         i++;
2853                         if (unlikely(i == tx_ring->count))
2854                                 i = 0;
2855                 }
2856         }
2857
2858         for (f = 0; f < nr_frags; f++) {
2859                 struct skb_frag_struct *frag;
2860
2861                 frag = &skb_shinfo(skb)->frags[f];
2862                 len = frag->size;
2863                 offset = frag->page_offset;
2864
2865                 while (len) {
2866                         i++;
2867                         if (unlikely(i == tx_ring->count))
2868                                 i = 0;
2869
2870                         buffer_info = &tx_ring->buffer_info[i];
2871                         size = min(len, max_per_txd);
2872                         /* Workaround for premature desc write-backs
2873                          * in TSO mode.  Append 4-byte sentinel desc */
2874                         if (unlikely(mss && f == (nr_frags-1) && size == len && size > 8))
2875                                 size -= 4;
2876                         /* Workaround for potential 82544 hang in PCI-X.
2877                          * Avoid terminating buffers within evenly-aligned
2878                          * dwords. */
2879                         if (unlikely(adapter->pcix_82544 &&
2880                             !((unsigned long)(page_to_phys(frag->page) + offset
2881                                               + size - 1) & 4) &&
2882                             size > 4))
2883                                 size -= 4;
2884
2885                         buffer_info->length = size;
2886                         buffer_info->time_stamp = jiffies;
2887                         buffer_info->mapped_as_page = true;
2888                         buffer_info->dma = dma_map_page(&pdev->dev, frag->page,
2889                                                         offset, size,
2890                                                         DMA_TO_DEVICE);
2891                         if (dma_mapping_error(&pdev->dev, buffer_info->dma))
2892                                 goto dma_error;
2893                         buffer_info->next_to_watch = i;
2894
2895                         len -= size;
2896                         offset += size;
2897                         count++;
2898                 }
2899         }
2900
2901         tx_ring->buffer_info[i].skb = skb;
2902         tx_ring->buffer_info[first].next_to_watch = i;
2903
2904         return count;
2905
2906 dma_error:
2907         dev_err(&pdev->dev, "TX DMA map failed\n");
2908         buffer_info->dma = 0;
2909         if (count)
2910                 count--;
2911
2912         while (count--) {
2913                 if (i==0)
2914                         i += tx_ring->count;
2915                 i--;
2916                 buffer_info = &tx_ring->buffer_info[i];
2917                 e1000_unmap_and_free_tx_resource(adapter, buffer_info);
2918         }
2919
2920         return 0;
2921 }
2922
2923 static void e1000_tx_queue(struct e1000_adapter *adapter,
2924                            struct e1000_tx_ring *tx_ring, int tx_flags,
2925                            int count)
2926 {
2927         struct e1000_hw *hw = &adapter->hw;
2928         struct e1000_tx_desc *tx_desc = NULL;
2929         struct e1000_buffer *buffer_info;
2930         u32 txd_upper = 0, txd_lower = E1000_TXD_CMD_IFCS;
2931         unsigned int i;
2932
2933         if (likely(tx_flags & E1000_TX_FLAGS_TSO)) {
2934                 txd_lower |= E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D |
2935                              E1000_TXD_CMD_TSE;
2936                 txd_upper |= E1000_TXD_POPTS_TXSM << 8;
2937
2938                 if (likely(tx_flags & E1000_TX_FLAGS_IPV4))
2939                         txd_upper |= E1000_TXD_POPTS_IXSM << 8;
2940         }
2941
2942         if (likely(tx_flags & E1000_TX_FLAGS_CSUM)) {
2943                 txd_lower |= E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D;
2944                 txd_upper |= E1000_TXD_POPTS_TXSM << 8;
2945         }
2946
2947         if (unlikely(tx_flags & E1000_TX_FLAGS_VLAN)) {
2948                 txd_lower |= E1000_TXD_CMD_VLE;
2949                 txd_upper |= (tx_flags & E1000_TX_FLAGS_VLAN_MASK);
2950         }
2951
2952         i = tx_ring->next_to_use;
2953
2954         while (count--) {
2955                 buffer_info = &tx_ring->buffer_info[i];
2956                 tx_desc = E1000_TX_DESC(*tx_ring, i);
2957                 tx_desc->buffer_addr = cpu_to_le64(buffer_info->dma);
2958                 tx_desc->lower.data =
2959                         cpu_to_le32(txd_lower | buffer_info->length);
2960                 tx_desc->upper.data = cpu_to_le32(txd_upper);
2961                 if (unlikely(++i == tx_ring->count)) i = 0;
2962         }
2963
2964         tx_desc->lower.data |= cpu_to_le32(adapter->txd_cmd);
2965
2966         /* Force memory writes to complete before letting h/w
2967          * know there are new descriptors to fetch.  (Only
2968          * applicable for weak-ordered memory model archs,
2969          * such as IA-64). */
2970         wmb();
2971
2972         tx_ring->next_to_use = i;
2973         writel(i, hw->hw_addr + tx_ring->tdt);
2974         /* we need this if more than one processor can write to our tail
2975          * at a time, it syncronizes IO on IA64/Altix systems */
2976         mmiowb();
2977 }
2978
2979 /**
2980  * 82547 workaround to avoid controller hang in half-duplex environment.
2981  * The workaround is to avoid queuing a large packet that would span
2982  * the internal Tx FIFO ring boundary by notifying the stack to resend
2983  * the packet at a later time.  This gives the Tx FIFO an opportunity to
2984  * flush all packets.  When that occurs, we reset the Tx FIFO pointers
2985  * to the beginning of the Tx FIFO.
2986  **/
2987
2988 #define E1000_FIFO_HDR                  0x10
2989 #define E1000_82547_PAD_LEN             0x3E0
2990
2991 static int e1000_82547_fifo_workaround(struct e1000_adapter *adapter,
2992                                        struct sk_buff *skb)
2993 {
2994         u32 fifo_space = adapter->tx_fifo_size - adapter->tx_fifo_head;
2995         u32 skb_fifo_len = skb->len + E1000_FIFO_HDR;
2996
2997         skb_fifo_len = ALIGN(skb_fifo_len, E1000_FIFO_HDR);
2998
2999         if (adapter->link_duplex != HALF_DUPLEX)
3000                 goto no_fifo_stall_required;
3001
3002         if (atomic_read(&adapter->tx_fifo_stall))
3003                 return 1;
3004
3005         if (skb_fifo_len >= (E1000_82547_PAD_LEN + fifo_space)) {
3006                 atomic_set(&adapter->tx_fifo_stall, 1);
3007                 return 1;
3008         }
3009
3010 no_fifo_stall_required:
3011         adapter->tx_fifo_head += skb_fifo_len;
3012         if (adapter->tx_fifo_head >= adapter->tx_fifo_size)
3013                 adapter->tx_fifo_head -= adapter->tx_fifo_size;
3014         return 0;
3015 }
3016
3017 static int __e1000_maybe_stop_tx(struct net_device *netdev, int size)
3018 {
3019         struct e1000_adapter *adapter = netdev_priv(netdev);
3020         struct e1000_tx_ring *tx_ring = adapter->tx_ring;
3021
3022         netif_stop_queue(netdev);
3023         /* Herbert's original patch had:
3024          *  smp_mb__after_netif_stop_queue();
3025          * but since that doesn't exist yet, just open code it. */
3026         smp_mb();
3027
3028         /* We need to check again in a case another CPU has just
3029          * made room available. */
3030         if (likely(E1000_DESC_UNUSED(tx_ring) < size))
3031                 return -EBUSY;
3032
3033         /* A reprieve! */
3034         netif_start_queue(netdev);
3035         ++adapter->restart_queue;
3036         return 0;
3037 }
3038
3039 static int e1000_maybe_stop_tx(struct net_device *netdev,
3040                                struct e1000_tx_ring *tx_ring, int size)
3041 {
3042         if (likely(E1000_DESC_UNUSED(tx_ring) >= size))
3043                 return 0;
3044         return __e1000_maybe_stop_tx(netdev, size);
3045 }
3046
3047 #define TXD_USE_COUNT(S, X) (((S) >> (X)) + 1 )
3048 static netdev_tx_t e1000_xmit_frame(struct sk_buff *skb,
3049                                     struct net_device *netdev)
3050 {
3051         struct e1000_adapter *adapter = netdev_priv(netdev);
3052         struct e1000_hw *hw = &adapter->hw;
3053         struct e1000_tx_ring *tx_ring;
3054         unsigned int first, max_per_txd = E1000_MAX_DATA_PER_TXD;
3055         unsigned int max_txd_pwr = E1000_MAX_TXD_PWR;
3056         unsigned int tx_flags = 0;
3057         unsigned int len = skb_headlen(skb);
3058         unsigned int nr_frags;
3059         unsigned int mss;
3060         int count = 0;
3061         int tso;
3062         unsigned int f;
3063
3064         /* This goes back to the question of how to logically map a tx queue
3065          * to a flow.  Right now, performance is impacted slightly negatively
3066          * if using multiple tx queues.  If the stack breaks away from a
3067          * single qdisc implementation, we can look at this again. */
3068         tx_ring = adapter->tx_ring;
3069
3070         if (unlikely(skb->len <= 0)) {
3071                 dev_kfree_skb_any(skb);
3072                 return NETDEV_TX_OK;
3073         }
3074
3075         mss = skb_shinfo(skb)->gso_size;
3076         /* The controller does a simple calculation to
3077          * make sure there is enough room in the FIFO before
3078          * initiating the DMA for each buffer.  The calc is:
3079          * 4 = ceil(buffer len/mss).  To make sure we don't
3080          * overrun the FIFO, adjust the max buffer len if mss
3081          * drops. */
3082         if (mss) {
3083                 u8 hdr_len;
3084                 max_per_txd = min(mss << 2, max_per_txd);
3085                 max_txd_pwr = fls(max_per_txd) - 1;
3086
3087                 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
3088                 if (skb->data_len && hdr_len == len) {
3089                         switch (hw->mac_type) {
3090                                 unsigned int pull_size;
3091                         case e1000_82544:
3092                                 /* Make sure we have room to chop off 4 bytes,
3093                                  * and that the end alignment will work out to
3094                                  * this hardware's requirements
3095                                  * NOTE: this is a TSO only workaround
3096                                  * if end byte alignment not correct move us
3097                                  * into the next dword */
3098                                 if ((unsigned long)(skb_tail_pointer(skb) - 1) & 4)
3099                                         break;
3100                                 /* fall through */
3101                                 pull_size = min((unsigned int)4, skb->data_len);
3102                                 if (!__pskb_pull_tail(skb, pull_size)) {
3103                                         e_err(drv, "__pskb_pull_tail "
3104                                               "failed.\n");
3105                                         dev_kfree_skb_any(skb);
3106                                         return NETDEV_TX_OK;
3107                                 }
3108                                 len = skb_headlen(skb);
3109                                 break;
3110                         default:
3111                                 /* do nothing */
3112                                 break;
3113                         }
3114                 }
3115         }
3116
3117         /* reserve a descriptor for the offload context */
3118         if ((mss) || (skb->ip_summed == CHECKSUM_PARTIAL))
3119                 count++;
3120         count++;
3121
3122         /* Controller Erratum workaround */
3123         if (!skb->data_len && tx_ring->last_tx_tso && !skb_is_gso(skb))
3124                 count++;
3125
3126         count += TXD_USE_COUNT(len, max_txd_pwr);
3127
3128         if (adapter->pcix_82544)
3129                 count++;
3130
3131         /* work-around for errata 10 and it applies to all controllers
3132          * in PCI-X mode, so add one more descriptor to the count
3133          */
3134         if (unlikely((hw->bus_type == e1000_bus_type_pcix) &&
3135                         (len > 2015)))
3136                 count++;
3137
3138         nr_frags = skb_shinfo(skb)->nr_frags;
3139         for (f = 0; f < nr_frags; f++)
3140                 count += TXD_USE_COUNT(skb_shinfo(skb)->frags[f].size,
3141                                        max_txd_pwr);
3142         if (adapter->pcix_82544)
3143                 count += nr_frags;
3144
3145         /* need: count + 2 desc gap to keep tail from touching
3146          * head, otherwise try next time */
3147         if (unlikely(e1000_maybe_stop_tx(netdev, tx_ring, count + 2)))
3148                 return NETDEV_TX_BUSY;
3149
3150         if (unlikely(hw->mac_type == e1000_82547)) {
3151                 if (unlikely(e1000_82547_fifo_workaround(adapter, skb))) {
3152                         netif_stop_queue(netdev);
3153                         if (!test_bit(__E1000_DOWN, &adapter->flags))
3154                                 mod_timer(&adapter->tx_fifo_stall_timer,
3155                                           jiffies + 1);
3156                         return NETDEV_TX_BUSY;
3157                 }
3158         }
3159
3160         if (unlikely(vlan_tx_tag_present(skb))) {
3161                 tx_flags |= E1000_TX_FLAGS_VLAN;
3162                 tx_flags |= (vlan_tx_tag_get(skb) << E1000_TX_FLAGS_VLAN_SHIFT);
3163         }
3164
3165         first = tx_ring->next_to_use;
3166
3167         tso = e1000_tso(adapter, tx_ring, skb);
3168         if (tso < 0) {
3169                 dev_kfree_skb_any(skb);
3170                 return NETDEV_TX_OK;
3171         }
3172
3173         if (likely(tso)) {
3174                 if (likely(hw->mac_type != e1000_82544))
3175                         tx_ring->last_tx_tso = 1;
3176                 tx_flags |= E1000_TX_FLAGS_TSO;
3177         } else if (likely(e1000_tx_csum(adapter, tx_ring, skb)))
3178                 tx_flags |= E1000_TX_FLAGS_CSUM;
3179
3180         if (likely(skb->protocol == htons(ETH_P_IP)))
3181                 tx_flags |= E1000_TX_FLAGS_IPV4;
3182
3183         count = e1000_tx_map(adapter, tx_ring, skb, first, max_per_txd,
3184                              nr_frags, mss);
3185
3186         if (count) {
3187                 e1000_tx_queue(adapter, tx_ring, tx_flags, count);
3188                 /* Make sure there is space in the ring for the next send. */
3189                 e1000_maybe_stop_tx(netdev, tx_ring, MAX_SKB_FRAGS + 2);
3190
3191         } else {
3192                 dev_kfree_skb_any(skb);
3193                 tx_ring->buffer_info[first].time_stamp = 0;
3194                 tx_ring->next_to_use = first;
3195         }
3196
3197         return NETDEV_TX_OK;
3198 }
3199
3200 /**
3201  * e1000_tx_timeout - Respond to a Tx Hang
3202  * @netdev: network interface device structure
3203  **/
3204
3205 static void e1000_tx_timeout(struct net_device *netdev)
3206 {
3207         struct e1000_adapter *adapter = netdev_priv(netdev);
3208
3209         /* Do the reset outside of interrupt context */
3210         adapter->tx_timeout_count++;
3211         schedule_work(&adapter->reset_task);
3212 }
3213
3214 static void e1000_reset_task(struct work_struct *work)
3215 {
3216         struct e1000_adapter *adapter =
3217                 container_of(work, struct e1000_adapter, reset_task);
3218
3219         e1000_reinit_safe(adapter);
3220 }
3221
3222 /**
3223  * e1000_get_stats - Get System Network Statistics
3224  * @netdev: network interface device structure
3225  *
3226  * Returns the address of the device statistics structure.
3227  * The statistics are actually updated from the timer callback.
3228  **/
3229
3230 static struct net_device_stats *e1000_get_stats(struct net_device *netdev)
3231 {
3232         /* only return the current stats */
3233         return &netdev->stats;
3234 }
3235
3236 /**
3237  * e1000_change_mtu - Change the Maximum Transfer Unit
3238  * @netdev: network interface device structure
3239  * @new_mtu: new value for maximum frame size
3240  *
3241  * Returns 0 on success, negative on failure
3242  **/
3243
3244 static int e1000_change_mtu(struct net_device *netdev, int new_mtu)
3245 {
3246         struct e1000_adapter *adapter = netdev_priv(netdev);
3247         struct e1000_hw *hw = &adapter->hw;
3248         int max_frame = new_mtu + ENET_HEADER_SIZE + ETHERNET_FCS_SIZE;
3249
3250         if ((max_frame < MINIMUM_ETHERNET_FRAME_SIZE) ||
3251             (max_frame > MAX_JUMBO_FRAME_SIZE)) {
3252                 e_err(probe, "Invalid MTU setting\n");
3253                 return -EINVAL;
3254         }
3255
3256         /* Adapter-specific max frame size limits. */
3257         switch (hw->mac_type) {
3258         case e1000_undefined ... e1000_82542_rev2_1:
3259                 if (max_frame > (ETH_FRAME_LEN + ETH_FCS_LEN)) {
3260                         e_err(probe, "Jumbo Frames not supported.\n");
3261                         return -EINVAL;
3262                 }
3263                 break;
3264         default:
3265                 /* Capable of supporting up to MAX_JUMBO_FRAME_SIZE limit. */
3266                 break;
3267         }
3268
3269         while (test_and_set_bit(__E1000_RESETTING, &adapter->flags))
3270                 msleep(1);
3271         /* e1000_down has a dependency on max_frame_size */
3272         hw->max_frame_size = max_frame;
3273         if (netif_running(netdev))
3274                 e1000_down(adapter);
3275
3276         /* NOTE: netdev_alloc_skb reserves 16 bytes, and typically NET_IP_ALIGN
3277          * means we reserve 2 more, this pushes us to allocate from the next
3278          * larger slab size.
3279          * i.e. RXBUFFER_2048 --> size-4096 slab
3280          *  however with the new *_jumbo_rx* routines, jumbo receives will use
3281          *  fragmented skbs */
3282
3283         if (max_frame <= E1000_RXBUFFER_2048)
3284                 adapter->rx_buffer_len = E1000_RXBUFFER_2048;
3285         else
3286 #if (PAGE_SIZE >= E1000_RXBUFFER_16384)
3287                 adapter->rx_buffer_len = E1000_RXBUFFER_16384;
3288 #elif (PAGE_SIZE >= E1000_RXBUFFER_4096)
3289                 adapter->rx_buffer_len = PAGE_SIZE;
3290 #endif
3291
3292         /* adjust allocation if LPE protects us, and we aren't using SBP */
3293         if (!hw->tbi_compatibility_on &&
3294             ((max_frame == (ETH_FRAME_LEN + ETH_FCS_LEN)) ||
3295              (max_frame == MAXIMUM_ETHERNET_VLAN_SIZE)))
3296                 adapter->rx_buffer_len = MAXIMUM_ETHERNET_VLAN_SIZE;
3297
3298         pr_info("%s changing MTU from %d to %d\n",
3299                 netdev->name, netdev->mtu, new_mtu);
3300         netdev->mtu = new_mtu;
3301
3302         if (netif_running(netdev))
3303                 e1000_up(adapter);
3304         else
3305                 e1000_reset(adapter);
3306
3307         clear_bit(__E1000_RESETTING, &adapter->flags);
3308
3309         return 0;
3310 }
3311
3312 /**
3313  * e1000_update_stats - Update the board statistics counters
3314  * @adapter: board private structure
3315  **/
3316
3317 void e1000_update_stats(struct e1000_adapter *adapter)
3318 {
3319         struct net_device *netdev = adapter->netdev;
3320         struct e1000_hw *hw = &adapter->hw;
3321         struct pci_dev *pdev = adapter->pdev;
3322         unsigned long flags;
3323         u16 phy_tmp;
3324
3325 #define PHY_IDLE_ERROR_COUNT_MASK 0x00FF
3326
3327         /*
3328          * Prevent stats update while adapter is being reset, or if the pci
3329          * connection is down.
3330          */
3331         if (adapter->link_speed == 0)
3332                 return;
3333         if (pci_channel_offline(pdev))
3334                 return;
3335
3336         spin_lock_irqsave(&adapter->stats_lock, flags);
3337
3338         /* these counters are modified from e1000_tbi_adjust_stats,
3339          * called from the interrupt context, so they must only
3340          * be written while holding adapter->stats_lock
3341          */
3342
3343         adapter->stats.crcerrs += er32(CRCERRS);
3344         adapter->stats.gprc += er32(GPRC);
3345         adapter->stats.gorcl += er32(GORCL);
3346         adapter->stats.gorch += er32(GORCH);
3347         adapter->stats.bprc += er32(BPRC);
3348         adapter->stats.mprc += er32(MPRC);
3349         adapter->stats.roc += er32(ROC);
3350
3351         adapter->stats.prc64 += er32(PRC64);
3352         adapter->stats.prc127 += er32(PRC127);
3353         adapter->stats.prc255 += er32(PRC255);
3354         adapter->stats.prc511 += er32(PRC511);
3355         adapter->stats.prc1023 += er32(PRC1023);
3356         adapter->stats.prc1522 += er32(PRC1522);
3357
3358         adapter->stats.symerrs += er32(SYMERRS);
3359         adapter->stats.mpc += er32(MPC);
3360         adapter->stats.scc += er32(SCC);
3361         adapter->stats.ecol += er32(ECOL);
3362         adapter->stats.mcc += er32(MCC);
3363         adapter->stats.latecol += er32(LATECOL);
3364         adapter->stats.dc += er32(DC);
3365         adapter->stats.sec += er32(SEC);
3366         adapter->stats.rlec += er32(RLEC);
3367         adapter->stats.xonrxc += er32(XONRXC);
3368         adapter->stats.xontxc += er32(XONTXC);
3369         adapter->stats.xoffrxc += er32(XOFFRXC);
3370         adapter->stats.xofftxc += er32(XOFFTXC);
3371         adapter->stats.fcruc += er32(FCRUC);
3372         adapter->stats.gptc += er32(GPTC);
3373         adapter->stats.gotcl += er32(GOTCL);
3374         adapter->stats.gotch += er32(GOTCH);
3375         adapter->stats.rnbc += er32(RNBC);
3376         adapter->stats.ruc += er32(RUC);
3377         adapter->stats.rfc += er32(RFC);
3378         adapter->stats.rjc += er32(RJC);
3379         adapter->stats.torl += er32(TORL);
3380         adapter->stats.torh += er32(TORH);
3381         adapter->stats.totl += er32(TOTL);
3382         adapter->stats.toth += er32(TOTH);
3383         adapter->stats.tpr += er32(TPR);
3384
3385         adapter->stats.ptc64 += er32(PTC64);
3386         adapter->stats.ptc127 += er32(PTC127);
3387         adapter->stats.ptc255 += er32(PTC255);
3388         adapter->stats.ptc511 += er32(PTC511);
3389         adapter->stats.ptc1023 += er32(PTC1023);
3390         adapter->stats.ptc1522 += er32(PTC1522);
3391
3392         adapter->stats.mptc += er32(MPTC);
3393         adapter->stats.bptc += er32(BPTC);
3394
3395         /* used for adaptive IFS */
3396
3397         hw->tx_packet_delta = er32(TPT);
3398         adapter->stats.tpt += hw->tx_packet_delta;
3399         hw->collision_delta = er32(COLC);
3400         adapter->stats.colc += hw->collision_delta;
3401
3402         if (hw->mac_type >= e1000_82543) {
3403                 adapter->stats.algnerrc += er32(ALGNERRC);
3404                 adapter->stats.rxerrc += er32(RXERRC);
3405                 adapter->stats.tncrs += er32(TNCRS);
3406                 adapter->stats.cexterr += er32(CEXTERR);
3407                 adapter->stats.tsctc += er32(TSCTC);
3408                 adapter->stats.tsctfc += er32(TSCTFC);
3409         }
3410
3411         /* Fill out the OS statistics structure */
3412         netdev->stats.multicast = adapter->stats.mprc;
3413         netdev->stats.collisions = adapter->stats.colc;
3414
3415         /* Rx Errors */
3416
3417         /* RLEC on some newer hardware can be incorrect so build
3418         * our own version based on RUC and ROC */
3419         netdev->stats.rx_errors = adapter->stats.rxerrc +
3420                 adapter->stats.crcerrs + adapter->stats.algnerrc +
3421                 adapter->stats.ruc + adapter->stats.roc +
3422                 adapter->stats.cexterr;
3423         adapter->stats.rlerrc = adapter->stats.ruc + adapter->stats.roc;
3424         netdev->stats.rx_length_errors = adapter->stats.rlerrc;
3425         netdev->stats.rx_crc_errors = adapter->stats.crcerrs;
3426         netdev->stats.rx_frame_errors = adapter->stats.algnerrc;
3427         netdev->stats.rx_missed_errors = adapter->stats.mpc;
3428
3429         /* Tx Errors */
3430         adapter->stats.txerrc = adapter->stats.ecol + adapter->stats.latecol;
3431         netdev->stats.tx_errors = adapter->stats.txerrc;
3432         netdev->stats.tx_aborted_errors = adapter->stats.ecol;
3433         netdev->stats.tx_window_errors = adapter->stats.latecol;
3434         netdev->stats.tx_carrier_errors = adapter->stats.tncrs;
3435         if (hw->bad_tx_carr_stats_fd &&
3436             adapter->link_duplex == FULL_DUPLEX) {
3437                 netdev->stats.tx_carrier_errors = 0;
3438                 adapter->stats.tncrs = 0;
3439         }
3440
3441         /* Tx Dropped needs to be maintained elsewhere */
3442
3443         /* Phy Stats */
3444         if (hw->media_type == e1000_media_type_copper) {
3445                 if ((adapter->link_speed == SPEED_1000) &&
3446                    (!e1000_read_phy_reg(hw, PHY_1000T_STATUS, &phy_tmp))) {
3447                         phy_tmp &= PHY_IDLE_ERROR_COUNT_MASK;
3448                         adapter->phy_stats.idle_errors += phy_tmp;
3449                 }
3450
3451                 if ((hw->mac_type <= e1000_82546) &&
3452                    (hw->phy_type == e1000_phy_m88) &&
3453                    !e1000_read_phy_reg(hw, M88E1000_RX_ERR_CNTR, &phy_tmp))
3454                         adapter->phy_stats.receive_errors += phy_tmp;
3455         }
3456
3457         /* Management Stats */
3458         if (hw->has_smbus) {
3459                 adapter->stats.mgptc += er32(MGTPTC);
3460                 adapter->stats.mgprc += er32(MGTPRC);
3461                 adapter->stats.mgpdc += er32(MGTPDC);
3462         }
3463
3464         spin_unlock_irqrestore(&adapter->stats_lock, flags);
3465 }
3466
3467 /**
3468  * e1000_intr - Interrupt Handler
3469  * @irq: interrupt number
3470  * @data: pointer to a network interface device structure
3471  **/
3472
3473 static irqreturn_t e1000_intr(int irq, void *data)
3474 {
3475         struct net_device *netdev = data;
3476         struct e1000_adapter *adapter = netdev_priv(netdev);
3477         struct e1000_hw *hw = &adapter->hw;
3478         u32 icr = er32(ICR);
3479
3480         if (unlikely((!icr)))
3481                 return IRQ_NONE;  /* Not our interrupt */
3482
3483         /*
3484          * we might have caused the interrupt, but the above
3485          * read cleared it, and just in case the driver is
3486          * down there is nothing to do so return handled
3487          */
3488         if (unlikely(test_bit(__E1000_DOWN, &adapter->flags)))
3489                 return IRQ_HANDLED;
3490
3491         if (unlikely(icr & (E1000_ICR_RXSEQ | E1000_ICR_LSC))) {
3492                 hw->get_link_status = 1;
3493                 /* guard against interrupt when we're going down */
3494                 if (!test_bit(__E1000_DOWN, &adapter->flags))
3495                         mod_timer(&adapter->watchdog_timer, jiffies + 1);
3496         }
3497
3498         /* disable interrupts, without the synchronize_irq bit */
3499         ew32(IMC, ~0);
3500         E1000_WRITE_FLUSH();
3501
3502         if (likely(napi_schedule_prep(&adapter->napi))) {
3503                 adapter->total_tx_bytes = 0;
3504                 adapter->total_tx_packets = 0;
3505                 adapter->total_rx_bytes = 0;
3506                 adapter->total_rx_packets = 0;
3507                 __napi_schedule(&adapter->napi);
3508         } else {
3509                 /* this really should not happen! if it does it is basically a
3510                  * bug, but not a hard error, so enable ints and continue */
3511                 if (!test_bit(__E1000_DOWN, &adapter->flags))
3512                         e1000_irq_enable(adapter);
3513         }
3514
3515         return IRQ_HANDLED;
3516 }
3517
3518 /**
3519  * e1000_clean - NAPI Rx polling callback
3520  * @adapter: board private structure
3521  **/
3522 static int e1000_clean(struct napi_struct *napi, int budget)
3523 {
3524         struct e1000_adapter *adapter = container_of(napi, struct e1000_adapter, napi);
3525         int tx_clean_complete = 0, work_done = 0;
3526
3527         tx_clean_complete = e1000_clean_tx_irq(adapter, &adapter->tx_ring[0]);
3528
3529         adapter->clean_rx(adapter, &adapter->rx_ring[0], &work_done, budget);
3530
3531         if (!tx_clean_complete)
3532                 work_done = budget;
3533
3534         /* If budget not fully consumed, exit the polling mode */
3535         if (work_done < budget) {
3536                 if (likely(adapter->itr_setting & 3))
3537                         e1000_set_itr(adapter);
3538                 napi_complete(napi);
3539                 if (!test_bit(__E1000_DOWN, &adapter->flags))
3540                         e1000_irq_enable(adapter);
3541         }
3542
3543         return work_done;
3544 }
3545
3546 /**
3547  * e1000_clean_tx_irq - Reclaim resources after transmit completes
3548  * @adapter: board private structure
3549  **/
3550 static bool e1000_clean_tx_irq(struct e1000_adapter *adapter,
3551                                struct e1000_tx_ring *tx_ring)
3552 {
3553         struct e1000_hw *hw = &adapter->hw;
3554         struct net_device *netdev = adapter->netdev;
3555         struct e1000_tx_desc *tx_desc, *eop_desc;
3556         struct e1000_buffer *buffer_info;
3557         unsigned int i, eop;
3558         unsigned int count = 0;
3559         unsigned int total_tx_bytes=0, total_tx_packets=0;
3560
3561         i = tx_ring->next_to_clean;
3562         eop = tx_ring->buffer_info[i].next_to_watch;
3563         eop_desc = E1000_TX_DESC(*tx_ring, eop);
3564
3565         while ((eop_desc->upper.data & cpu_to_le32(E1000_TXD_STAT_DD)) &&
3566                (count < tx_ring->count)) {
3567                 bool cleaned = false;
3568                 rmb();  /* read buffer_info after eop_desc */
3569                 for ( ; !cleaned; count++) {
3570                         tx_desc = E1000_TX_DESC(*tx_ring, i);
3571                         buffer_info = &tx_ring->buffer_info[i];
3572                         cleaned = (i == eop);
3573
3574                         if (cleaned) {
3575                                 struct sk_buff *skb = buffer_info->skb;
3576                                 unsigned int segs, bytecount;
3577                                 segs = skb_shinfo(skb)->gso_segs ?: 1;
3578                                 /* multiply data chunks by size of headers */
3579                                 bytecount = ((segs - 1) * skb_headlen(skb)) +
3580                                             skb->len;
3581                                 total_tx_packets += segs;
3582                                 total_tx_bytes += bytecount;
3583                         }
3584                         e1000_unmap_and_free_tx_resource(adapter, buffer_info);
3585                         tx_desc->upper.data = 0;
3586
3587                         if (unlikely(++i == tx_ring->count)) i = 0;
3588                 }
3589
3590                 eop = tx_ring->buffer_info[i].next_to_watch;
3591                 eop_desc = E1000_TX_DESC(*tx_ring, eop);
3592         }
3593
3594         tx_ring->next_to_clean = i;
3595
3596 #define TX_WAKE_THRESHOLD 32
3597         if (unlikely(count && netif_carrier_ok(netdev) &&
3598                      E1000_DESC_UNUSED(tx_ring) >= TX_WAKE_THRESHOLD)) {
3599                 /* Make sure that anybody stopping the queue after this
3600                  * sees the new next_to_clean.
3601                  */
3602                 smp_mb();
3603
3604                 if (netif_queue_stopped(netdev) &&
3605                     !(test_bit(__E1000_DOWN, &adapter->flags))) {
3606                         netif_wake_queue(netdev);
3607                         ++adapter->restart_queue;
3608                 }
3609         }
3610
3611         if (adapter->detect_tx_hung) {
3612                 /* Detect a transmit hang in hardware, this serializes the
3613                  * check with the clearing of time_stamp and movement of i */
3614                 adapter->detect_tx_hung = false;
3615                 if (tx_ring->buffer_info[eop].time_stamp &&
3616                     time_after(jiffies, tx_ring->buffer_info[eop].time_stamp +
3617                                (adapter->tx_timeout_factor * HZ)) &&
3618                     !(er32(STATUS) & E1000_STATUS_TXOFF)) {
3619
3620                         /* detected Tx unit hang */
3621                         e_err(drv, "Detected Tx Unit Hang\n"
3622                               "  Tx Queue             <%lu>\n"
3623                               "  TDH                  <%x>\n"
3624                               "  TDT                  <%x>\n"
3625                               "  next_to_use          <%x>\n"
3626                               "  next_to_clean        <%x>\n"
3627                               "buffer_info[next_to_clean]\n"
3628                               "  time_stamp           <%lx>\n"
3629                               "  next_to_watch        <%x>\n"
3630                               "  jiffies              <%lx>\n"
3631                               "  next_to_watch.status <%x>\n",
3632                                 (unsigned long)((tx_ring - adapter->tx_ring) /
3633                                         sizeof(struct e1000_tx_ring)),
3634                                 readl(hw->hw_addr + tx_ring->tdh),
3635                                 readl(hw->hw_addr + tx_ring->tdt),
3636                                 tx_ring->next_to_use,
3637                                 tx_ring->next_to_clean,
3638                                 tx_ring->buffer_info[eop].time_stamp,
3639                                 eop,
3640                                 jiffies,
3641                                 eop_desc->upper.fields.status);
3642                         netif_stop_queue(netdev);
3643                 }
3644         }
3645         adapter->total_tx_bytes += total_tx_bytes;
3646         adapter->total_tx_packets += total_tx_packets;
3647         netdev->stats.tx_bytes += total_tx_bytes;
3648         netdev->stats.tx_packets += total_tx_packets;
3649         return count < tx_ring->count;
3650 }
3651
3652 /**
3653  * e1000_rx_checksum - Receive Checksum Offload for 82543
3654  * @adapter:     board private structure
3655  * @status_err:  receive descriptor status and error fields
3656  * @csum:        receive descriptor csum field
3657  * @sk_buff:     socket buffer with received data
3658  **/
3659
3660 static void e1000_rx_checksum(struct e1000_adapter *adapter, u32 status_err,
3661                               u32 csum, struct sk_buff *skb)
3662 {
3663         struct e1000_hw *hw = &adapter->hw;
3664         u16 status = (u16)status_err;
3665         u8 errors = (u8)(status_err >> 24);
3666
3667         skb_checksum_none_assert(skb);
3668
3669         /* 82543 or newer only */
3670         if (unlikely(hw->mac_type < e1000_82543)) return;
3671         /* Ignore Checksum bit is set */
3672         if (unlikely(status & E1000_RXD_STAT_IXSM)) return;
3673         /* TCP/UDP checksum error bit is set */
3674         if (unlikely(errors & E1000_RXD_ERR_TCPE)) {
3675                 /* let the stack verify checksum errors */
3676                 adapter->hw_csum_err++;
3677                 return;
3678         }
3679         /* TCP/UDP Checksum has not been calculated */
3680         if (!(status & E1000_RXD_STAT_TCPCS))
3681                 return;
3682
3683         /* It must be a TCP or UDP packet with a valid checksum */
3684         if (likely(status & E1000_RXD_STAT_TCPCS)) {
3685                 /* TCP checksum is good */
3686                 skb->ip_summed = CHECKSUM_UNNECESSARY;
3687         }
3688         adapter->hw_csum_good++;
3689 }
3690
3691 /**
3692  * e1000_consume_page - helper function
3693  **/
3694 static void e1000_consume_page(struct e1000_buffer *bi, struct sk_buff *skb,
3695                                u16 length)
3696 {
3697         bi->page = NULL;
3698         skb->len += length;
3699         skb->data_len += length;
3700         skb->truesize += length;
3701 }
3702
3703 /**
3704  * e1000_receive_skb - helper function to handle rx indications
3705  * @adapter: board private structure
3706  * @status: descriptor status field as written by hardware
3707  * @vlan: descriptor vlan field as written by hardware (no le/be conversion)
3708  * @skb: pointer to sk_buff to be indicated to stack
3709  */
3710 static void e1000_receive_skb(struct e1000_adapter *adapter, u8 status,
3711                               __le16 vlan, struct sk_buff *skb)
3712 {
3713         skb->protocol = eth_type_trans(skb, adapter->netdev);
3714
3715         if ((unlikely(adapter->vlgrp && (status & E1000_RXD_STAT_VP))))
3716                 vlan_gro_receive(&adapter->napi, adapter->vlgrp,
3717                                  le16_to_cpu(vlan) & E1000_RXD_SPC_VLAN_MASK,
3718                                  skb);
3719         else
3720                 napi_gro_receive(&adapter->napi, skb);
3721 }
3722
3723 /**
3724  * e1000_clean_jumbo_rx_irq - Send received data up the network stack; legacy
3725  * @adapter: board private structure
3726  * @rx_ring: ring to clean
3727  * @work_done: amount of napi work completed this call
3728  * @work_to_do: max amount of work allowed for this call to do
3729  *
3730  * the return value indicates whether actual cleaning was done, there
3731  * is no guarantee that everything was cleaned
3732  */
3733 static bool e1000_clean_jumbo_rx_irq(struct e1000_adapter *adapter,
3734                                      struct e1000_rx_ring *rx_ring,
3735                                      int *work_done, int work_to_do)
3736 {
3737         struct e1000_hw *hw = &adapter->hw;
3738         struct net_device *netdev = adapter->netdev;
3739         struct pci_dev *pdev = adapter->pdev;
3740         struct e1000_rx_desc *rx_desc, *next_rxd;
3741         struct e1000_buffer *buffer_info, *next_buffer;
3742         unsigned long irq_flags;
3743         u32 length;
3744         unsigned int i;
3745         int cleaned_count = 0;
3746         bool cleaned = false;
3747         unsigned int total_rx_bytes=0, total_rx_packets=0;
3748
3749         i = rx_ring->next_to_clean;
3750         rx_desc = E1000_RX_DESC(*rx_ring, i);
3751         buffer_info = &rx_ring->buffer_info[i];
3752
3753         while (rx_desc->status & E1000_RXD_STAT_DD) {
3754                 struct sk_buff *skb;
3755                 u8 status;
3756
3757                 if (*work_done >= work_to_do)
3758                         break;
3759                 (*work_done)++;
3760                 rmb(); /* read descriptor and rx_buffer_info after status DD */
3761
3762                 status = rx_desc->status;
3763                 skb = buffer_info->skb;
3764                 buffer_info->skb = NULL;
3765
3766                 if (++i == rx_ring->count) i = 0;
3767                 next_rxd = E1000_RX_DESC(*rx_ring, i);
3768                 prefetch(next_rxd);
3769
3770                 next_buffer = &rx_ring->buffer_info[i];
3771
3772                 cleaned = true;
3773                 cleaned_count++;
3774                 dma_unmap_page(&pdev->dev, buffer_info->dma,
3775                                buffer_info->length, DMA_FROM_DEVICE);
3776                 buffer_info->dma = 0;
3777
3778                 length = le16_to_cpu(rx_desc->length);
3779
3780                 /* errors is only valid for DD + EOP descriptors */
3781                 if (unlikely((status & E1000_RXD_STAT_EOP) &&
3782                     (rx_desc->errors & E1000_RXD_ERR_FRAME_ERR_MASK))) {
3783                         u8 last_byte = *(skb->data + length - 1);
3784                         if (TBI_ACCEPT(hw, status, rx_desc->errors, length,
3785                                        last_byte)) {
3786                                 spin_lock_irqsave(&adapter->stats_lock,
3787                                                   irq_flags);
3788                                 e1000_tbi_adjust_stats(hw, &adapter->stats,
3789                                                        length, skb->data);
3790                                 spin_unlock_irqrestore(&adapter->stats_lock,
3791                                                        irq_flags);
3792                                 length--;
3793                         } else {
3794                                 /* recycle both page and skb */
3795                                 buffer_info->skb = skb;
3796                                 /* an error means any chain goes out the window
3797                                  * too */
3798                                 if (rx_ring->rx_skb_top)
3799                                         dev_kfree_skb(rx_ring->rx_skb_top);
3800                                 rx_ring->rx_skb_top = NULL;
3801                                 goto next_desc;
3802                         }
3803                 }
3804
3805 #define rxtop rx_ring->rx_skb_top
3806                 if (!(status & E1000_RXD_STAT_EOP)) {
3807                         /* this descriptor is only the beginning (or middle) */
3808                         if (!rxtop) {
3809                                 /* this is the beginning of a chain */
3810                                 rxtop = skb;
3811                                 skb_fill_page_desc(rxtop, 0, buffer_info->page,
3812                                                    0, length);
3813                         } else {
3814                                 /* this is the middle of a chain */
3815                                 skb_fill_page_desc(rxtop,
3816                                     skb_shinfo(rxtop)->nr_frags,
3817                                     buffer_info->page, 0, length);
3818                                 /* re-use the skb, only consumed the page */
3819                                 buffer_info->skb = skb;
3820                         }
3821                         e1000_consume_page(buffer_info, rxtop, length);
3822                         goto next_desc;
3823                 } else {
3824                         if (rxtop) {
3825                                 /* end of the chain */
3826                                 skb_fill_page_desc(rxtop,
3827                                     skb_shinfo(rxtop)->nr_frags,
3828                                     buffer_info->page, 0, length);
3829                                 /* re-use the current skb, we only consumed the
3830                                  * page */
3831                                 buffer_info->skb = skb;
3832                                 skb = rxtop;
3833                                 rxtop = NULL;
3834                                 e1000_consume_page(buffer_info, skb, length);
3835                         } else {
3836                                 /* no chain, got EOP, this buf is the packet
3837                                  * copybreak to save the put_page/alloc_page */
3838                                 if (length <= copybreak &&
3839                                     skb_tailroom(skb) >= length) {
3840                                         u8 *vaddr;
3841                                         vaddr = kmap_atomic(buffer_info->page,
3842                                                             KM_SKB_DATA_SOFTIRQ);
3843                                         memcpy(skb_tail_pointer(skb), vaddr, length);
3844                                         kunmap_atomic(vaddr,
3845                                                       KM_SKB_DATA_SOFTIRQ);
3846                                         /* re-use the page, so don't erase
3847                                          * buffer_info->page */
3848                                         skb_put(skb, length);
3849                                 } else {
3850                                         skb_fill_page_desc(skb, 0,
3851                                                            buffer_info->page, 0,
3852                                                            length);
3853                                         e1000_consume_page(buffer_info, skb,
3854                                                            length);
3855                                 }
3856                         }
3857                 }
3858
3859                 /* Receive Checksum Offload XXX recompute due to CRC strip? */
3860                 e1000_rx_checksum(adapter,
3861                                   (u32)(status) |
3862                                   ((u32)(rx_desc->errors) << 24),
3863                                   le16_to_cpu(rx_desc->csum), skb);
3864
3865                 pskb_trim(skb, skb->len - 4);
3866
3867                 /* probably a little skewed due to removing CRC */
3868                 total_rx_bytes += skb->len;
3869                 total_rx_packets++;
3870
3871                 /* eth type trans needs skb->data to point to something */
3872                 if (!pskb_may_pull(skb, ETH_HLEN)) {
3873                         e_err(drv, "pskb_may_pull failed.\n");
3874                         dev_kfree_skb(skb);
3875                         goto next_desc;
3876                 }
3877
3878                 e1000_receive_skb(adapter, status, rx_desc->special, skb);
3879
3880 next_desc:
3881                 rx_desc->status = 0;
3882
3883                 /* return some buffers to hardware, one at a time is too slow */
3884                 if (unlikely(cleaned_count >= E1000_RX_BUFFER_WRITE)) {
3885                         adapter->alloc_rx_buf(adapter, rx_ring, cleaned_count);
3886                         cleaned_count = 0;
3887                 }
3888
3889                 /* use prefetched values */
3890                 rx_desc = next_rxd;
3891                 buffer_info = next_buffer;
3892         }
3893         rx_ring->next_to_clean = i;
3894
3895         cleaned_count = E1000_DESC_UNUSED(rx_ring);
3896         if (cleaned_count)
3897                 adapter->alloc_rx_buf(adapter, rx_ring, cleaned_count);
3898
3899         adapter->total_rx_packets += total_rx_packets;
3900         adapter->total_rx_bytes += total_rx_bytes;
3901         netdev->stats.rx_bytes += total_rx_bytes;
3902         netdev->stats.rx_packets += total_rx_packets;
3903         return cleaned;
3904 }
3905
3906 /*
3907  * this should improve performance for small packets with large amounts
3908  * of reassembly being done in the stack
3909  */
3910 static void e1000_check_copybreak(struct net_device *netdev,
3911                                  struct e1000_buffer *buffer_info,
3912                                  u32 length, struct sk_buff **skb)
3913 {
3914         struct sk_buff *new_skb;
3915
3916         if (length > copybreak)
3917                 return;
3918
3919         new_skb = netdev_alloc_skb_ip_align(netdev, length);
3920         if (!new_skb)
3921                 return;
3922
3923         skb_copy_to_linear_data_offset(new_skb, -NET_IP_ALIGN,
3924                                        (*skb)->data - NET_IP_ALIGN,
3925                                        length + NET_IP_ALIGN);
3926         /* save the skb in buffer_info as good */
3927         buffer_info->skb = *skb;
3928         *skb = new_skb;
3929 }
3930
3931 /**
3932  * e1000_clean_rx_irq - Send received data up the network stack; legacy
3933  * @adapter: board private structure
3934  * @rx_ring: ring to clean
3935  * @work_done: amount of napi work completed this call
3936  * @work_to_do: max amount of work allowed for this call to do
3937  */
3938 static bool e1000_clean_rx_irq(struct e1000_adapter *adapter,
3939                                struct e1000_rx_ring *rx_ring,
3940                                int *work_done, int work_to_do)
3941 {
3942         struct e1000_hw *hw = &adapter->hw;
3943         struct net_device *netdev = adapter->netdev;
3944         struct pci_dev *pdev = adapter->pdev;
3945         struct e1000_rx_desc *rx_desc, *next_rxd;
3946         struct e1000_buffer *buffer_info, *next_buffer;
3947         unsigned long flags;
3948         u32 length;
3949         unsigned int i;
3950         int cleaned_count = 0;
3951         bool cleaned = false;
3952         unsigned int total_rx_bytes=0, total_rx_packets=0;
3953
3954         i = rx_ring->next_to_clean;
3955         rx_desc = E1000_RX_DESC(*rx_ring, i);
3956         buffer_info = &rx_ring->buffer_info[i];
3957
3958         while (rx_desc->status & E1000_RXD_STAT_DD) {
3959                 struct sk_buff *skb;
3960                 u8 status;
3961
3962                 if (*work_done >= work_to_do)
3963                         break;
3964                 (*work_done)++;
3965                 rmb(); /* read descriptor and rx_buffer_info after status DD */
3966
3967                 status = rx_desc->status;
3968                 skb = buffer_info->skb;
3969                 buffer_info->skb = NULL;
3970
3971                 prefetch(skb->data - NET_IP_ALIGN);
3972
3973                 if (++i == rx_ring->count) i = 0;
3974                 next_rxd = E1000_RX_DESC(*rx_ring, i);
3975                 prefetch(next_rxd);
3976
3977                 next_buffer = &rx_ring->buffer_info[i];
3978
3979                 cleaned = true;
3980                 cleaned_count++;
3981                 dma_unmap_single(&pdev->dev, buffer_info->dma,
3982                                  buffer_info->length, DMA_FROM_DEVICE);
3983                 buffer_info->dma = 0;
3984
3985                 length = le16_to_cpu(rx_desc->length);
3986                 /* !EOP means multiple descriptors were used to store a single
3987                  * packet, if thats the case we need to toss it.  In fact, we
3988                  * to toss every packet with the EOP bit clear and the next
3989                  * frame that _does_ have the EOP bit set, as it is by
3990                  * definition only a frame fragment
3991                  */
3992                 if (unlikely(!(status & E1000_RXD_STAT_EOP)))
3993                         adapter->discarding = true;
3994
3995                 if (adapter->discarding) {
3996                         /* All receives must fit into a single buffer */
3997                         e_dbg("Receive packet consumed multiple buffers\n");
3998                         /* recycle */
3999                         buffer_info->skb = skb;
4000                         if (status & E1000_RXD_STAT_EOP)
4001                                 adapter->discarding = false;
4002                         goto next_desc;
4003                 }
4004
4005                 if (unlikely(rx_desc->errors & E1000_RXD_ERR_FRAME_ERR_MASK)) {
4006                         u8 last_byte = *(skb->data + length - 1);
4007                         if (TBI_ACCEPT(hw, status, rx_desc->errors, length,
4008                                        last_byte)) {
4009                                 spin_lock_irqsave(&adapter->stats_lock, flags);
4010                                 e1000_tbi_adjust_stats(hw, &adapter->stats,
4011                                                        length, skb->data);
4012                                 spin_unlock_irqrestore(&adapter->stats_lock,
4013                                                        flags);
4014                                 length--;
4015                         } else {
4016                                 /* recycle */
4017                                 buffer_info->skb = skb;
4018                                 goto next_desc;
4019                         }
4020                 }
4021
4022                 /* adjust length to remove Ethernet CRC, this must be
4023                  * done after the TBI_ACCEPT workaround above */
4024                 length -= 4;
4025
4026                 /* probably a little skewed due to removing CRC */
4027                 total_rx_bytes += length;
4028                 total_rx_packets++;
4029
4030                 e1000_check_copybreak(netdev, buffer_info, length, &skb);
4031
4032                 skb_put(skb, length);
4033
4034                 /* Receive Checksum Offload */
4035                 e1000_rx_checksum(adapter,
4036                                   (u32)(status) |
4037                                   ((u32)(rx_desc->errors) << 24),
4038                                   le16_to_cpu(rx_desc->csum), skb);
4039
4040                 e1000_receive_skb(adapter, status, rx_desc->special, skb);
4041
4042 next_desc:
4043                 rx_desc->status = 0;
4044
4045                 /* return some buffers to hardware, one at a time is too slow */
4046                 if (unlikely(cleaned_count >= E1000_RX_BUFFER_WRITE)) {
4047                         adapter->alloc_rx_buf(adapter, rx_ring, cleaned_count);
4048                         cleaned_count = 0;
4049                 }
4050
4051                 /* use prefetched values */
4052                 rx_desc = next_rxd;
4053                 buffer_info = next_buffer;
4054         }
4055         rx_ring->next_to_clean = i;
4056
4057         cleaned_count = E1000_DESC_UNUSED(rx_ring);
4058         if (cleaned_count)
4059                 adapter->alloc_rx_buf(adapter, rx_ring, cleaned_count);
4060
4061         adapter->total_rx_packets += total_rx_packets;
4062         adapter->total_rx_bytes += total_rx_bytes;
4063         netdev->stats.rx_bytes += total_rx_bytes;
4064         netdev->stats.rx_packets += total_rx_packets;
4065         return cleaned;
4066 }
4067
4068 /**
4069  * e1000_alloc_jumbo_rx_buffers - Replace used jumbo receive buffers
4070  * @adapter: address of board private structure
4071  * @rx_ring: pointer to receive ring structure
4072  * @cleaned_count: number of buffers to allocate this pass
4073  **/
4074
4075 static void
4076 e1000_alloc_jumbo_rx_buffers(struct e1000_adapter *adapter,
4077                              struct e1000_rx_ring *rx_ring, int cleaned_count)
4078 {
4079         struct net_device *netdev = adapter->netdev;
4080         struct pci_dev *pdev = adapter->pdev;
4081         struct e1000_rx_desc *rx_desc;
4082         struct e1000_buffer *buffer_info;
4083         struct sk_buff *skb;
4084         unsigned int i;
4085         unsigned int bufsz = 256 - 16 /*for skb_reserve */ ;
4086
4087         i = rx_ring->next_to_use;
4088         buffer_info = &rx_ring->buffer_info[i];
4089
4090         while (cleaned_count--) {
4091                 skb = buffer_info->skb;
4092                 if (skb) {
4093                         skb_trim(skb, 0);
4094                         goto check_page;
4095                 }
4096
4097                 skb = netdev_alloc_skb_ip_align(netdev, bufsz);
4098                 if (unlikely(!skb)) {
4099                         /* Better luck next round */
4100                         adapter->alloc_rx_buff_failed++;
4101                         break;
4102                 }
4103
4104                 /* Fix for errata 23, can't cross 64kB boundary */
4105                 if (!e1000_check_64k_bound(adapter, skb->data, bufsz)) {
4106                         struct sk_buff *oldskb = skb;
4107                         e_err(rx_err, "skb align check failed: %u bytes at "
4108                               "%p\n", bufsz, skb->data);
4109                         /* Try again, without freeing the previous */
4110                         skb = netdev_alloc_skb_ip_align(netdev, bufsz);
4111                         /* Failed allocation, critical failure */
4112                         if (!skb) {
4113                                 dev_kfree_skb(oldskb);
4114                                 adapter->alloc_rx_buff_failed++;
4115                                 break;
4116                         }
4117
4118                         if (!e1000_check_64k_bound(adapter, skb->data, bufsz)) {
4119                                 /* give up */
4120                                 dev_kfree_skb(skb);
4121                                 dev_kfree_skb(oldskb);
4122                                 break; /* while (cleaned_count--) */
4123                         }
4124
4125                         /* Use new allocation */
4126                         dev_kfree_skb(oldskb);
4127                 }
4128                 buffer_info->skb = skb;
4129                 buffer_info->length = adapter->rx_buffer_len;
4130 check_page:
4131                 /* allocate a new page if necessary */
4132                 if (!buffer_info->page) {
4133                         buffer_info->page = alloc_page(GFP_ATOMIC);
4134                         if (unlikely(!buffer_info->page)) {
4135                                 adapter->alloc_rx_buff_failed++;
4136                                 break;
4137                         }
4138                 }
4139
4140                 if (!buffer_info->dma) {
4141                         buffer_info->dma = dma_map_page(&pdev->dev,
4142                                                         buffer_info->page, 0,
4143                                                         buffer_info->length,
4144                                                         DMA_FROM_DEVICE);
4145                         if (dma_mapping_error(&pdev->dev, buffer_info->dma)) {
4146                                 put_page(buffer_info->page);
4147                                 dev_kfree_skb(skb);
4148                                 buffer_info->page = NULL;
4149                                 buffer_info->skb = NULL;
4150                                 buffer_info->dma = 0;
4151                                 adapter->alloc_rx_buff_failed++;
4152                                 break; /* while !buffer_info->skb */
4153                         }
4154                 }
4155
4156                 rx_desc = E1000_RX_DESC(*rx_ring, i);
4157                 rx_desc->buffer_addr = cpu_to_le64(buffer_info->dma);
4158
4159                 if (unlikely(++i == rx_ring->count))
4160                         i = 0;
4161                 buffer_info = &rx_ring->buffer_info[i];
4162         }
4163
4164         if (likely(rx_ring->next_to_use != i)) {
4165                 rx_ring->next_to_use = i;
4166                 if (unlikely(i-- == 0))
4167                         i = (rx_ring->count - 1);
4168
4169                 /* Force memory writes to complete before letting h/w
4170                  * know there are new descriptors to fetch.  (Only
4171                  * applicable for weak-ordered memory model archs,
4172                  * such as IA-64). */
4173                 wmb();
4174                 writel(i, adapter->hw.hw_addr + rx_ring->rdt);
4175         }
4176 }
4177
4178 /**
4179  * e1000_alloc_rx_buffers - Replace used receive buffers; legacy & extended
4180  * @adapter: address of board private structure
4181  **/
4182
4183 static void e1000_alloc_rx_buffers(struct e1000_adapter *adapter,
4184                                    struct e1000_rx_ring *rx_ring,
4185                                    int cleaned_count)
4186 {
4187         struct e1000_hw *hw = &adapter->hw;
4188         struct net_device *netdev = adapter->netdev;
4189         struct pci_dev *pdev = adapter->pdev;
4190         struct e1000_rx_desc *rx_desc;
4191         struct e1000_buffer *buffer_info;
4192         struct sk_buff *skb;
4193         unsigned int i;
4194         unsigned int bufsz = adapter->rx_buffer_len;
4195
4196         i = rx_ring->next_to_use;
4197         buffer_info = &rx_ring->buffer_info[i];
4198
4199         while (cleaned_count--) {
4200                 skb = buffer_info->skb;
4201                 if (skb) {
4202                         skb_trim(skb, 0);
4203                         goto map_skb;
4204                 }
4205
4206                 skb = netdev_alloc_skb_ip_align(netdev, bufsz);
4207                 if (unlikely(!skb)) {
4208                         /* Better luck next round */
4209                         adapter->alloc_rx_buff_failed++;
4210                         break;
4211                 }
4212
4213                 /* Fix for errata 23, can't cross 64kB boundary */
4214                 if (!e1000_check_64k_bound(adapter, skb->data, bufsz)) {
4215                         struct sk_buff *oldskb = skb;
4216                         e_err(rx_err, "skb align check failed: %u bytes at "
4217                               "%p\n", bufsz, skb->data);
4218                         /* Try again, without freeing the previous */
4219                         skb = netdev_alloc_skb_ip_align(netdev, bufsz);
4220                         /* Failed allocation, critical failure */
4221                         if (!skb) {
4222                                 dev_kfree_skb(oldskb);
4223                                 adapter->alloc_rx_buff_failed++;
4224                                 break;
4225                         }
4226
4227                         if (!e1000_check_64k_bound(adapter, skb->data, bufsz)) {
4228                                 /* give up */
4229                                 dev_kfree_skb(skb);
4230                                 dev_kfree_skb(oldskb);
4231                                 adapter->alloc_rx_buff_failed++;
4232                                 break; /* while !buffer_info->skb */
4233                         }
4234
4235                         /* Use new allocation */
4236                         dev_kfree_skb(oldskb);
4237                 }
4238                 buffer_info->skb = skb;
4239                 buffer_info->length = adapter->rx_buffer_len;
4240 map_skb:
4241                 buffer_info->dma = dma_map_single(&pdev->dev,
4242                                                   skb->data,
4243                                                   buffer_info->length,
4244                                                   DMA_FROM_DEVICE);
4245                 if (dma_mapping_error(&pdev->dev, buffer_info->dma)) {
4246                         dev_kfree_skb(skb);
4247                         buffer_info->skb = NULL;
4248                         buffer_info->dma = 0;
4249                         adapter->alloc_rx_buff_failed++;
4250                         break; /* while !buffer_info->skb */
4251                 }
4252
4253                 /*
4254                  * XXX if it was allocated cleanly it will never map to a
4255                  * boundary crossing
4256                  */
4257
4258                 /* Fix for errata 23, can't cross 64kB boundary */
4259                 if (!e1000_check_64k_bound(adapter,
4260                                         (void *)(unsigned long)buffer_info->dma,
4261                                         adapter->rx_buffer_len)) {
4262                         e_err(rx_err, "dma align check failed: %u bytes at "
4263                               "%p\n", adapter->rx_buffer_len,
4264                               (void *)(unsigned long)buffer_info->dma);
4265                         dev_kfree_skb(skb);
4266                         buffer_info->skb = NULL;
4267
4268                         dma_unmap_single(&pdev->dev, buffer_info->dma,
4269                                          adapter->rx_buffer_len,
4270                                          DMA_FROM_DEVICE);
4271                         buffer_info->dma = 0;
4272
4273                         adapter->alloc_rx_buff_failed++;
4274                         break; /* while !buffer_info->skb */
4275                 }
4276                 rx_desc = E1000_RX_DESC(*rx_ring, i);
4277                 rx_desc->buffer_addr = cpu_to_le64(buffer_info->dma);
4278
4279                 if (unlikely(++i == rx_ring->count))
4280                         i = 0;
4281                 buffer_info = &rx_ring->buffer_info[i];
4282         }
4283
4284         if (likely(rx_ring->next_to_use != i)) {
4285                 rx_ring->next_to_use = i;
4286                 if (unlikely(i-- == 0))
4287                         i = (rx_ring->count - 1);
4288
4289                 /* Force memory writes to complete before letting h/w
4290                  * know there are new descriptors to fetch.  (Only
4291                  * applicable for weak-ordered memory model archs,
4292                  * such as IA-64). */
4293                 wmb();
4294                 writel(i, hw->hw_addr + rx_ring->rdt);
4295         }
4296 }
4297
4298 /**
4299  * e1000_smartspeed - Workaround for SmartSpeed on 82541 and 82547 controllers.
4300  * @adapter:
4301  **/
4302
4303 static void e1000_smartspeed(struct e1000_adapter *adapter)
4304 {
4305         struct e1000_hw *hw = &adapter->hw;
4306         u16 phy_status;
4307         u16 phy_ctrl;
4308
4309         if ((hw->phy_type != e1000_phy_igp) || !hw->autoneg ||
4310            !(hw->autoneg_advertised & ADVERTISE_1000_FULL))
4311                 return;
4312
4313         if (adapter->smartspeed == 0) {
4314                 /* If Master/Slave config fault is asserted twice,
4315                  * we assume back-to-back */
4316                 e1000_read_phy_reg(hw, PHY_1000T_STATUS, &phy_status);
4317                 if (!(phy_status & SR_1000T_MS_CONFIG_FAULT)) return;
4318                 e1000_read_phy_reg(hw, PHY_1000T_STATUS, &phy_status);
4319                 if (!(phy_status & SR_1000T_MS_CONFIG_FAULT)) return;
4320                 e1000_read_phy_reg(hw, PHY_1000T_CTRL, &phy_ctrl);
4321                 if (phy_ctrl & CR_1000T_MS_ENABLE) {
4322                         phy_ctrl &= ~CR_1000T_MS_ENABLE;
4323                         e1000_write_phy_reg(hw, PHY_1000T_CTRL,
4324                                             phy_ctrl);
4325                         adapter->smartspeed++;
4326                         if (!e1000_phy_setup_autoneg(hw) &&
4327                            !e1000_read_phy_reg(hw, PHY_CTRL,
4328                                                &phy_ctrl)) {
4329                                 phy_ctrl |= (MII_CR_AUTO_NEG_EN |
4330                                              MII_CR_RESTART_AUTO_NEG);
4331                                 e1000_write_phy_reg(hw, PHY_CTRL,
4332                                                     phy_ctrl);
4333                         }
4334                 }
4335                 return;
4336         } else if (adapter->smartspeed == E1000_SMARTSPEED_DOWNSHIFT) {
4337                 /* If still no link, perhaps using 2/3 pair cable */
4338                 e1000_read_phy_reg(hw, PHY_1000T_CTRL, &phy_ctrl);
4339                 phy_ctrl |= CR_1000T_MS_ENABLE;
4340                 e1000_write_phy_reg(hw, PHY_1000T_CTRL, phy_ctrl);
4341                 if (!e1000_phy_setup_autoneg(hw) &&
4342                    !e1000_read_phy_reg(hw, PHY_CTRL, &phy_ctrl)) {
4343                         phy_ctrl |= (MII_CR_AUTO_NEG_EN |
4344                                      MII_CR_RESTART_AUTO_NEG);
4345                         e1000_write_phy_reg(hw, PHY_CTRL, phy_ctrl);
4346                 }
4347         }
4348         /* Restart process after E1000_SMARTSPEED_MAX iterations */
4349         if (adapter->smartspeed++ == E1000_SMARTSPEED_MAX)
4350                 adapter->smartspeed = 0;
4351 }
4352
4353 /**
4354  * e1000_ioctl -
4355  * @netdev:
4356  * @ifreq:
4357  * @cmd:
4358  **/
4359
4360 static int e1000_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
4361 {
4362         switch (cmd) {
4363         case SIOCGMIIPHY:
4364         case SIOCGMIIREG:
4365         case SIOCSMIIREG:
4366                 return e1000_mii_ioctl(netdev, ifr, cmd);
4367         default:
4368                 return -EOPNOTSUPP;
4369         }
4370 }
4371
4372 /**
4373  * e1000_mii_ioctl -
4374  * @netdev:
4375  * @ifreq:
4376  * @cmd:
4377  **/
4378
4379 static int e1000_mii_ioctl(struct net_device *netdev, struct ifreq *ifr,
4380                            int cmd)
4381 {
4382         struct e1000_adapter *adapter = netdev_priv(netdev);
4383         struct e1000_hw *hw = &adapter->hw;
4384         struct mii_ioctl_data *data = if_mii(ifr);
4385         int retval;
4386         u16 mii_reg;
4387         unsigned long flags;
4388
4389         if (hw->media_type != e1000_media_type_copper)
4390                 return -EOPNOTSUPP;
4391
4392         switch (cmd) {
4393         case SIOCGMIIPHY:
4394                 data->phy_id = hw->phy_addr;
4395                 break;
4396         case SIOCGMIIREG:
4397                 spin_lock_irqsave(&adapter->stats_lock, flags);
4398                 if (e1000_read_phy_reg(hw, data->reg_num & 0x1F,
4399                                    &data->val_out)) {
4400                         spin_unlock_irqrestore(&adapter->stats_lock, flags);
4401                         return -EIO;
4402                 }
4403                 spin_unlock_irqrestore(&adapter->stats_lock, flags);
4404                 break;
4405         case SIOCSMIIREG:
4406                 if (data->reg_num & ~(0x1F))
4407                         return -EFAULT;
4408                 mii_reg = data->val_in;
4409                 spin_lock_irqsave(&adapter->stats_lock, flags);
4410                 if (e1000_write_phy_reg(hw, data->reg_num,
4411                                         mii_reg)) {
4412                         spin_unlock_irqrestore(&adapter->stats_lock, flags);
4413                         return -EIO;
4414                 }
4415                 spin_unlock_irqrestore(&adapter->stats_lock, flags);
4416                 if (hw->media_type == e1000_media_type_copper) {
4417                         switch (data->reg_num) {
4418                         case PHY_CTRL:
4419                                 if (mii_reg & MII_CR_POWER_DOWN)
4420                                         break;
4421                                 if (mii_reg & MII_CR_AUTO_NEG_EN) {
4422                                         hw->autoneg = 1;
4423                                         hw->autoneg_advertised = 0x2F;
4424                                 } else {
4425                                         u32 speed;
4426                                         if (mii_reg & 0x40)
4427                                                 speed = SPEED_1000;
4428                                         else if (mii_reg & 0x2000)
4429                                                 speed = SPEED_100;
4430                                         else
4431                                                 speed = SPEED_10;
4432                                         retval = e1000_set_spd_dplx(
4433                                                 adapter, speed,
4434                                                 ((mii_reg & 0x100)
4435                                                  ? DUPLEX_FULL :
4436                                                  DUPLEX_HALF));
4437                                         if (retval)
4438                                                 return retval;
4439                                 }
4440                                 if (netif_running(adapter->netdev))
4441                                         e1000_reinit_locked(adapter);
4442                                 else
4443                                         e1000_reset(adapter);
4444                                 break;
4445                         case M88E1000_PHY_SPEC_CTRL:
4446                         case M88E1000_EXT_PHY_SPEC_CTRL:
4447                                 if (e1000_phy_reset(hw))
4448                                         return -EIO;
4449                                 break;
4450                         }
4451                 } else {
4452                         switch (data->reg_num) {
4453                         case PHY_CTRL:
4454                                 if (mii_reg & MII_CR_POWER_DOWN)
4455                                         break;
4456                                 if (netif_running(adapter->netdev))
4457                                         e1000_reinit_locked(adapter);
4458                                 else
4459                                         e1000_reset(adapter);
4460                                 break;
4461                         }
4462                 }
4463                 break;
4464         default:
4465                 return -EOPNOTSUPP;
4466         }
4467         return E1000_SUCCESS;
4468 }
4469
4470 void e1000_pci_set_mwi(struct e1000_hw *hw)
4471 {
4472         struct e1000_adapter *adapter = hw->back;
4473         int ret_val = pci_set_mwi(adapter->pdev);
4474
4475         if (ret_val)
4476                 e_err(probe, "Error in setting MWI\n");
4477 }
4478
4479 void e1000_pci_clear_mwi(struct e1000_hw *hw)
4480 {
4481         struct e1000_adapter *adapter = hw->back;
4482
4483         pci_clear_mwi(adapter->pdev);
4484 }
4485
4486 int e1000_pcix_get_mmrbc(struct e1000_hw *hw)
4487 {
4488         struct e1000_adapter *adapter = hw->back;
4489         return pcix_get_mmrbc(adapter->pdev);
4490 }
4491
4492 void e1000_pcix_set_mmrbc(struct e1000_hw *hw, int mmrbc)
4493 {
4494         struct e1000_adapter *adapter = hw->back;
4495         pcix_set_mmrbc(adapter->pdev, mmrbc);
4496 }
4497
4498 void e1000_io_write(struct e1000_hw *hw, unsigned long port, u32 value)
4499 {
4500         outl(value, port);
4501 }
4502
4503 static void e1000_vlan_rx_register(struct net_device *netdev,
4504                                    struct vlan_group *grp)
4505 {
4506         struct e1000_adapter *adapter = netdev_priv(netdev);
4507         struct e1000_hw *hw = &adapter->hw;
4508         u32 ctrl, rctl;
4509
4510         if (!test_bit(__E1000_DOWN, &adapter->flags))
4511                 e1000_irq_disable(adapter);
4512         adapter->vlgrp = grp;
4513
4514         if (grp) {
4515                 /* enable VLAN tag insert/strip */
4516                 ctrl = er32(CTRL);
4517                 ctrl |= E1000_CTRL_VME;
4518                 ew32(CTRL, ctrl);
4519
4520                 /* enable VLAN receive filtering */
4521                 rctl = er32(RCTL);
4522                 rctl &= ~E1000_RCTL_CFIEN;
4523                 if (!(netdev->flags & IFF_PROMISC))
4524                         rctl |= E1000_RCTL_VFE;
4525                 ew32(RCTL, rctl);
4526                 e1000_update_mng_vlan(adapter);
4527         } else {
4528                 /* disable VLAN tag insert/strip */
4529                 ctrl = er32(CTRL);
4530                 ctrl &= ~E1000_CTRL_VME;
4531                 ew32(CTRL, ctrl);
4532
4533                 /* disable VLAN receive filtering */
4534                 rctl = er32(RCTL);
4535                 rctl &= ~E1000_RCTL_VFE;
4536                 ew32(RCTL, rctl);
4537
4538                 if (adapter->mng_vlan_id != (u16)E1000_MNG_VLAN_NONE) {
4539                         e1000_vlan_rx_kill_vid(netdev, adapter->mng_vlan_id);
4540                         adapter->mng_vlan_id = E1000_MNG_VLAN_NONE;
4541                 }
4542         }
4543
4544         if (!test_bit(__E1000_DOWN, &adapter->flags))
4545                 e1000_irq_enable(adapter);
4546 }
4547
4548 static void e1000_vlan_rx_add_vid(struct net_device *netdev, u16 vid)
4549 {
4550         struct e1000_adapter *adapter = netdev_priv(netdev);
4551         struct e1000_hw *hw = &adapter->hw;
4552         u32 vfta, index;
4553
4554         if ((hw->mng_cookie.status &
4555              E1000_MNG_DHCP_COOKIE_STATUS_VLAN_SUPPORT) &&
4556             (vid == adapter->mng_vlan_id))
4557                 return;
4558         /* add VID to filter table */
4559         index = (vid >> 5) & 0x7F;
4560         vfta = E1000_READ_REG_ARRAY(hw, VFTA, index);
4561         vfta |= (1 << (vid & 0x1F));
4562         e1000_write_vfta(hw, index, vfta);
4563 }
4564
4565 static void e1000_vlan_rx_kill_vid(struct net_device *netdev, u16 vid)
4566 {
4567         struct e1000_adapter *adapter = netdev_priv(netdev);
4568         struct e1000_hw *hw = &adapter->hw;
4569         u32 vfta, index;
4570
4571         if (!test_bit(__E1000_DOWN, &adapter->flags))
4572                 e1000_irq_disable(adapter);
4573         vlan_group_set_device(adapter->vlgrp, vid, NULL);
4574         if (!test_bit(__E1000_DOWN, &adapter->flags))
4575                 e1000_irq_enable(adapter);
4576
4577         /* remove VID from filter table */
4578         index = (vid >> 5) & 0x7F;
4579         vfta = E1000_READ_REG_ARRAY(hw, VFTA, index);
4580         vfta &= ~(1 << (vid & 0x1F));
4581         e1000_write_vfta(hw, index, vfta);
4582 }
4583
4584 static void e1000_restore_vlan(struct e1000_adapter *adapter)
4585 {
4586         e1000_vlan_rx_register(adapter->netdev, adapter->vlgrp);
4587
4588         if (adapter->vlgrp) {
4589                 u16 vid;
4590                 for (vid = 0; vid < VLAN_N_VID; vid++) {
4591                         if (!vlan_group_get_device(adapter->vlgrp, vid))
4592                                 continue;
4593                         e1000_vlan_rx_add_vid(adapter->netdev, vid);
4594                 }
4595         }
4596 }
4597
4598 int e1000_set_spd_dplx(struct e1000_adapter *adapter, u32 spd, u8 dplx)
4599 {
4600         struct e1000_hw *hw = &adapter->hw;
4601
4602         hw->autoneg = 0;
4603
4604         /* Make sure dplx is at most 1 bit and lsb of speed is not set
4605          * for the switch() below to work */
4606         if ((spd & 1) || (dplx & ~1))
4607                 goto err_inval;
4608
4609         /* Fiber NICs only allow 1000 gbps Full duplex */
4610         if ((hw->media_type == e1000_media_type_fiber) &&
4611             spd != SPEED_1000 &&
4612             dplx != DUPLEX_FULL)
4613                 goto err_inval;
4614
4615         switch (spd + dplx) {
4616         case SPEED_10 + DUPLEX_HALF:
4617                 hw->forced_speed_duplex = e1000_10_half;
4618                 break;
4619         case SPEED_10 + DUPLEX_FULL:
4620                 hw->forced_speed_duplex = e1000_10_full;
4621                 break;
4622         case SPEED_100 + DUPLEX_HALF:
4623                 hw->forced_speed_duplex = e1000_100_half;
4624                 break;
4625         case SPEED_100 + DUPLEX_FULL:
4626                 hw->forced_speed_duplex = e1000_100_full;
4627                 break;
4628         case SPEED_1000 + DUPLEX_FULL:
4629                 hw->autoneg = 1;
4630                 hw->autoneg_advertised = ADVERTISE_1000_FULL;
4631                 break;
4632         case SPEED_1000 + DUPLEX_HALF: /* not supported */
4633         default:
4634                 goto err_inval;
4635         }
4636         return 0;
4637
4638 err_inval:
4639         e_err(probe, "Unsupported Speed/Duplex configuration\n");
4640         return -EINVAL;
4641 }
4642
4643 static int __e1000_shutdown(struct pci_dev *pdev, bool *enable_wake)
4644 {
4645         struct net_device *netdev = pci_get_drvdata(pdev);
4646         struct e1000_adapter *adapter = netdev_priv(netdev);
4647         struct e1000_hw *hw = &adapter->hw;
4648         u32 ctrl, ctrl_ext, rctl, status;
4649         u32 wufc = adapter->wol;
4650 #ifdef CONFIG_PM
4651         int retval = 0;
4652 #endif
4653
4654         netif_device_detach(netdev);
4655
4656         if (netif_running(netdev)) {
4657                 WARN_ON(test_bit(__E1000_RESETTING, &adapter->flags));
4658                 e1000_down(adapter);
4659         }
4660
4661 #ifdef CONFIG_PM
4662         retval = pci_save_state(pdev);
4663         if (retval)
4664                 return retval;
4665 #endif
4666
4667         status = er32(STATUS);
4668         if (status & E1000_STATUS_LU)
4669                 wufc &= ~E1000_WUFC_LNKC;
4670
4671         if (wufc) {
4672                 e1000_setup_rctl(adapter);
4673                 e1000_set_rx_mode(netdev);
4674
4675                 /* turn on all-multi mode if wake on multicast is enabled */
4676                 if (wufc & E1000_WUFC_MC) {
4677                         rctl = er32(RCTL);
4678                         rctl |= E1000_RCTL_MPE;
4679                         ew32(RCTL, rctl);
4680                 }
4681
4682                 if (hw->mac_type >= e1000_82540) {
4683                         ctrl = er32(CTRL);
4684                         /* advertise wake from D3Cold */
4685                         #define E1000_CTRL_ADVD3WUC 0x00100000
4686                         /* phy power management enable */
4687                         #define E1000_CTRL_EN_PHY_PWR_MGMT 0x00200000
4688                         ctrl |= E1000_CTRL_ADVD3WUC |
4689                                 E1000_CTRL_EN_PHY_PWR_MGMT;
4690                         ew32(CTRL, ctrl);
4691                 }
4692
4693                 if (hw->media_type == e1000_media_type_fiber ||
4694                     hw->media_type == e1000_media_type_internal_serdes) {
4695                         /* keep the laser running in D3 */
4696                         ctrl_ext = er32(CTRL_EXT);
4697                         ctrl_ext |= E1000_CTRL_EXT_SDP7_DATA;
4698                         ew32(CTRL_EXT, ctrl_ext);
4699                 }
4700
4701                 ew32(WUC, E1000_WUC_PME_EN);
4702                 ew32(WUFC, wufc);
4703         } else {
4704                 ew32(WUC, 0);
4705                 ew32(WUFC, 0);
4706         }
4707
4708         e1000_release_manageability(adapter);
4709
4710         *enable_wake = !!wufc;
4711
4712         /* make sure adapter isn't asleep if manageability is enabled */
4713         if (adapter->en_mng_pt)
4714                 *enable_wake = true;
4715
4716         if (netif_running(netdev))
4717                 e1000_free_irq(adapter);
4718
4719         pci_disable_device(pdev);
4720
4721         return 0;
4722 }
4723
4724 #ifdef CONFIG_PM
4725 static int e1000_suspend(struct pci_dev *pdev, pm_message_t state)
4726 {
4727         int retval;
4728         bool wake;
4729
4730         retval = __e1000_shutdown(pdev, &wake);
4731         if (retval)
4732                 return retval;
4733
4734         if (wake) {
4735                 pci_prepare_to_sleep(pdev);
4736         } else {
4737                 pci_wake_from_d3(pdev, false);
4738                 pci_set_power_state(pdev, PCI_D3hot);
4739         }
4740
4741         return 0;
4742 }
4743
4744 static int e1000_resume(struct pci_dev *pdev)
4745 {
4746         struct net_device *netdev = pci_get_drvdata(pdev);
4747         struct e1000_adapter *adapter = netdev_priv(netdev);
4748         struct e1000_hw *hw = &adapter->hw;
4749         u32 err;
4750
4751         pci_set_power_state(pdev, PCI_D0);
4752         pci_restore_state(pdev);
4753         pci_save_state(pdev);
4754
4755         if (adapter->need_ioport)
4756                 err = pci_enable_device(pdev);
4757         else
4758                 err = pci_enable_device_mem(pdev);
4759         if (err) {
4760                 pr_err("Cannot enable PCI device from suspend\n");
4761                 return err;
4762         }
4763         pci_set_master(pdev);
4764
4765         pci_enable_wake(pdev, PCI_D3hot, 0);
4766         pci_enable_wake(pdev, PCI_D3cold, 0);
4767
4768         if (netif_running(netdev)) {
4769                 err = e1000_request_irq(adapter);
4770                 if (err)
4771                         return err;
4772         }
4773
4774         e1000_power_up_phy(adapter);
4775         e1000_reset(adapter);
4776         ew32(WUS, ~0);
4777
4778         e1000_init_manageability(adapter);
4779
4780         if (netif_running(netdev))
4781                 e1000_up(adapter);
4782
4783         netif_device_attach(netdev);
4784
4785         return 0;
4786 }
4787 #endif
4788
4789 static void e1000_shutdown(struct pci_dev *pdev)
4790 {
4791         bool wake;
4792
4793         __e1000_shutdown(pdev, &wake);
4794
4795         if (system_state == SYSTEM_POWER_OFF) {
4796                 pci_wake_from_d3(pdev, wake);
4797                 pci_set_power_state(pdev, PCI_D3hot);
4798         }
4799 }
4800
4801 #ifdef CONFIG_NET_POLL_CONTROLLER
4802 /*
4803  * Polling 'interrupt' - used by things like netconsole to send skbs
4804  * without having to re-enable interrupts. It's not called while
4805  * the interrupt routine is executing.
4806  */
4807 static void e1000_netpoll(struct net_device *netdev)
4808 {
4809         struct e1000_adapter *adapter = netdev_priv(netdev);
4810
4811         disable_irq(adapter->pdev->irq);
4812         e1000_intr(adapter->pdev->irq, netdev);
4813         enable_irq(adapter->pdev->irq);
4814 }
4815 #endif
4816
4817 /**
4818  * e1000_io_error_detected - called when PCI error is detected
4819  * @pdev: Pointer to PCI device
4820  * @state: The current pci connection state
4821  *
4822  * This function is called after a PCI bus error affecting
4823  * this device has been detected.
4824  */
4825 static pci_ers_result_t e1000_io_error_detected(struct pci_dev *pdev,
4826                                                 pci_channel_state_t state)
4827 {
4828         struct net_device *netdev = pci_get_drvdata(pdev);
4829         struct e1000_adapter *adapter = netdev_priv(netdev);
4830
4831         netif_device_detach(netdev);
4832
4833         if (state == pci_channel_io_perm_failure)
4834                 return PCI_ERS_RESULT_DISCONNECT;
4835
4836         if (netif_running(netdev))
4837                 e1000_down(adapter);
4838         pci_disable_device(pdev);
4839
4840         /* Request a slot slot reset. */
4841         return PCI_ERS_RESULT_NEED_RESET;
4842 }
4843
4844 /**
4845  * e1000_io_slot_reset - called after the pci bus has been reset.
4846  * @pdev: Pointer to PCI device
4847  *
4848  * Restart the card from scratch, as if from a cold-boot. Implementation
4849  * resembles the first-half of the e1000_resume routine.
4850  */
4851 static pci_ers_result_t e1000_io_slot_reset(struct pci_dev *pdev)
4852 {
4853         struct net_device *netdev = pci_get_drvdata(pdev);
4854         struct e1000_adapter *adapter = netdev_priv(netdev);
4855         struct e1000_hw *hw = &adapter->hw;
4856         int err;
4857
4858         if (adapter->need_ioport)
4859                 err = pci_enable_device(pdev);
4860         else
4861                 err = pci_enable_device_mem(pdev);
4862         if (err) {
4863                 pr_err("Cannot re-enable PCI device after reset.\n");
4864                 return PCI_ERS_RESULT_DISCONNECT;
4865         }
4866         pci_set_master(pdev);
4867
4868         pci_enable_wake(pdev, PCI_D3hot, 0);
4869         pci_enable_wake(pdev, PCI_D3cold, 0);
4870
4871         e1000_reset(adapter);
4872         ew32(WUS, ~0);
4873
4874         return PCI_ERS_RESULT_RECOVERED;
4875 }
4876
4877 /**
4878  * e1000_io_resume - called when traffic can start flowing again.
4879  * @pdev: Pointer to PCI device
4880  *
4881  * This callback is called when the error recovery driver tells us that
4882  * its OK to resume normal operation. Implementation resembles the
4883  * second-half of the e1000_resume routine.
4884  */
4885 static void e1000_io_resume(struct pci_dev *pdev)
4886 {
4887         struct net_device *netdev = pci_get_drvdata(pdev);
4888         struct e1000_adapter *adapter = netdev_priv(netdev);
4889
4890         e1000_init_manageability(adapter);
4891
4892         if (netif_running(netdev)) {
4893                 if (e1000_up(adapter)) {
4894                         pr_info("can't bring device back up after reset\n");
4895                         return;
4896                 }
4897         }
4898
4899         netif_device_attach(netdev);
4900 }
4901
4902 /* e1000_main.c */