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