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