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