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