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