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