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