[PATCH] e1000: Enable polling before enabling interrupts
[linux-3.10.git] / drivers / net / e1000 / e1000_main.c
1 /*******************************************************************************
2
3   
4   Copyright(c) 1999 - 2004 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  * 5.3.12       6/7/04
33  * - kcompat NETIF_MSG for older kernels (2.4.9) <sean.p.mcdermott@intel.com>
34  * - if_mii support and associated kcompat for older kernels
35  * - More errlogging support from Jon Mason <jonmason@us.ibm.com>
36  * - Fix TSO issues on PPC64 machines -- Jon Mason <jonmason@us.ibm.com>
37  *
38  * 5.7.1        12/16/04
39  * - Resurrect 82547EI/GI related fix in e1000_intr to avoid deadlocks. This
40  *   fix was removed as it caused system instability. The suspected cause of 
41  *   this is the called to e1000_irq_disable in e1000_intr. Inlined the 
42  *   required piece of e1000_irq_disable into e1000_intr - Anton Blanchard
43  * 5.7.0        12/10/04
44  * - include fix to the condition that determines when to quit NAPI - Robert Olsson
45  * - use netif_poll_{disable/enable} to synchronize between NAPI and i/f up/down
46  * 5.6.5        11/01/04
47  * - Enabling NETIF_F_SG without checksum offload is illegal - 
48      John Mason <jdmason@us.ibm.com>
49  * 5.6.3        10/26/04
50  * - Remove redundant initialization - Jamal Hadi
51  * - Reset buffer_info->dma in tx resource cleanup logic
52  * 5.6.2        10/12/04
53  * - Avoid filling tx_ring completely - shemminger@osdl.org
54  * - Replace schedule_timeout() with msleep()/msleep_interruptible() -
55  *   nacc@us.ibm.com
56  * - Sparse cleanup - shemminger@osdl.org
57  * - Fix tx resource cleanup logic
58  * - LLTX support - ak@suse.de and hadi@cyberus.ca
59  */
60
61 char e1000_driver_name[] = "e1000";
62 char e1000_driver_string[] = "Intel(R) PRO/1000 Network Driver";
63 #ifndef CONFIG_E1000_NAPI
64 #define DRIVERNAPI
65 #else
66 #define DRIVERNAPI "-NAPI"
67 #endif
68 #define DRV_VERSION "5.7.6-k2"DRIVERNAPI
69 char e1000_driver_version[] = DRV_VERSION;
70 char e1000_copyright[] = "Copyright (c) 1999-2004 Intel Corporation.";
71
72 /* e1000_pci_tbl - PCI Device ID Table
73  *
74  * Last entry must be all 0s
75  *
76  * Macro expands to...
77  *   {PCI_DEVICE(PCI_VENDOR_ID_INTEL, device_id)}
78  */
79 static struct pci_device_id e1000_pci_tbl[] = {
80         INTEL_E1000_ETHERNET_DEVICE(0x1000),
81         INTEL_E1000_ETHERNET_DEVICE(0x1001),
82         INTEL_E1000_ETHERNET_DEVICE(0x1004),
83         INTEL_E1000_ETHERNET_DEVICE(0x1008),
84         INTEL_E1000_ETHERNET_DEVICE(0x1009),
85         INTEL_E1000_ETHERNET_DEVICE(0x100C),
86         INTEL_E1000_ETHERNET_DEVICE(0x100D),
87         INTEL_E1000_ETHERNET_DEVICE(0x100E),
88         INTEL_E1000_ETHERNET_DEVICE(0x100F),
89         INTEL_E1000_ETHERNET_DEVICE(0x1010),
90         INTEL_E1000_ETHERNET_DEVICE(0x1011),
91         INTEL_E1000_ETHERNET_DEVICE(0x1012),
92         INTEL_E1000_ETHERNET_DEVICE(0x1013),
93         INTEL_E1000_ETHERNET_DEVICE(0x1014),
94         INTEL_E1000_ETHERNET_DEVICE(0x1015),
95         INTEL_E1000_ETHERNET_DEVICE(0x1016),
96         INTEL_E1000_ETHERNET_DEVICE(0x1017),
97         INTEL_E1000_ETHERNET_DEVICE(0x1018),
98         INTEL_E1000_ETHERNET_DEVICE(0x1019),
99         INTEL_E1000_ETHERNET_DEVICE(0x101D),
100         INTEL_E1000_ETHERNET_DEVICE(0x101E),
101         INTEL_E1000_ETHERNET_DEVICE(0x1026),
102         INTEL_E1000_ETHERNET_DEVICE(0x1027),
103         INTEL_E1000_ETHERNET_DEVICE(0x1028),
104         INTEL_E1000_ETHERNET_DEVICE(0x1075),
105         INTEL_E1000_ETHERNET_DEVICE(0x1076),
106         INTEL_E1000_ETHERNET_DEVICE(0x1077),
107         INTEL_E1000_ETHERNET_DEVICE(0x1078),
108         INTEL_E1000_ETHERNET_DEVICE(0x1079),
109         INTEL_E1000_ETHERNET_DEVICE(0x107A),
110         INTEL_E1000_ETHERNET_DEVICE(0x107B),
111         INTEL_E1000_ETHERNET_DEVICE(0x107C),
112         INTEL_E1000_ETHERNET_DEVICE(0x108A),
113         /* required last entry */
114         {0,}
115 };
116
117 MODULE_DEVICE_TABLE(pci, e1000_pci_tbl);
118
119 int e1000_up(struct e1000_adapter *adapter);
120 void e1000_down(struct e1000_adapter *adapter);
121 void e1000_reset(struct e1000_adapter *adapter);
122 int e1000_set_spd_dplx(struct e1000_adapter *adapter, uint16_t spddplx);
123 int e1000_setup_tx_resources(struct e1000_adapter *adapter);
124 int e1000_setup_rx_resources(struct e1000_adapter *adapter);
125 void e1000_free_tx_resources(struct e1000_adapter *adapter);
126 void e1000_free_rx_resources(struct e1000_adapter *adapter);
127 void e1000_update_stats(struct e1000_adapter *adapter);
128
129 /* Local Function Prototypes */
130
131 static int e1000_init_module(void);
132 static void e1000_exit_module(void);
133 static int e1000_probe(struct pci_dev *pdev, const struct pci_device_id *ent);
134 static void __devexit e1000_remove(struct pci_dev *pdev);
135 static int e1000_sw_init(struct e1000_adapter *adapter);
136 static int e1000_open(struct net_device *netdev);
137 static int e1000_close(struct net_device *netdev);
138 static void e1000_configure_tx(struct e1000_adapter *adapter);
139 static void e1000_configure_rx(struct e1000_adapter *adapter);
140 static void e1000_setup_rctl(struct e1000_adapter *adapter);
141 static void e1000_clean_tx_ring(struct e1000_adapter *adapter);
142 static void e1000_clean_rx_ring(struct e1000_adapter *adapter);
143 static void e1000_set_multi(struct net_device *netdev);
144 static void e1000_update_phy_info(unsigned long data);
145 static void e1000_watchdog(unsigned long data);
146 static void e1000_watchdog_task(struct e1000_adapter *adapter);
147 static void e1000_82547_tx_fifo_stall(unsigned long data);
148 static int e1000_xmit_frame(struct sk_buff *skb, struct net_device *netdev);
149 static struct net_device_stats * e1000_get_stats(struct net_device *netdev);
150 static int e1000_change_mtu(struct net_device *netdev, int new_mtu);
151 static int e1000_set_mac(struct net_device *netdev, void *p);
152 static irqreturn_t e1000_intr(int irq, void *data, struct pt_regs *regs);
153 static boolean_t e1000_clean_tx_irq(struct e1000_adapter *adapter);
154 #ifdef CONFIG_E1000_NAPI
155 static int e1000_clean(struct net_device *netdev, int *budget);
156 static boolean_t e1000_clean_rx_irq(struct e1000_adapter *adapter,
157                                     int *work_done, int work_to_do);
158 #else
159 static boolean_t e1000_clean_rx_irq(struct e1000_adapter *adapter);
160 #endif
161 static void e1000_alloc_rx_buffers(struct e1000_adapter *adapter);
162 static int e1000_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd);
163 static int e1000_mii_ioctl(struct net_device *netdev, struct ifreq *ifr,
164                            int cmd);
165 void e1000_set_ethtool_ops(struct net_device *netdev);
166 static void e1000_enter_82542_rst(struct e1000_adapter *adapter);
167 static void e1000_leave_82542_rst(struct e1000_adapter *adapter);
168 static void e1000_tx_timeout(struct net_device *dev);
169 static void e1000_tx_timeout_task(struct net_device *dev);
170 static void e1000_smartspeed(struct e1000_adapter *adapter);
171 static inline int e1000_82547_fifo_workaround(struct e1000_adapter *adapter,
172                                               struct sk_buff *skb);
173
174 static void e1000_vlan_rx_register(struct net_device *netdev, struct vlan_group *grp);
175 static void e1000_vlan_rx_add_vid(struct net_device *netdev, uint16_t vid);
176 static void e1000_vlan_rx_kill_vid(struct net_device *netdev, uint16_t vid);
177 static void e1000_restore_vlan(struct e1000_adapter *adapter);
178
179 static int e1000_notify_reboot(struct notifier_block *, unsigned long event, void *ptr);
180 static int e1000_suspend(struct pci_dev *pdev, uint32_t state);
181 #ifdef CONFIG_PM
182 static int e1000_resume(struct pci_dev *pdev);
183 #endif
184
185 #ifdef CONFIG_NET_POLL_CONTROLLER
186 /* for netdump / net console */
187 static void e1000_netpoll (struct net_device *netdev);
188 #endif
189
190 struct notifier_block e1000_notifier_reboot = {
191         .notifier_call  = e1000_notify_reboot,
192         .next           = NULL,
193         .priority       = 0
194 };
195
196 /* Exported from other modules */
197
198 extern void e1000_check_options(struct e1000_adapter *adapter);
199
200 static struct pci_driver e1000_driver = {
201         .name     = e1000_driver_name,
202         .id_table = e1000_pci_tbl,
203         .probe    = e1000_probe,
204         .remove   = __devexit_p(e1000_remove),
205         /* Power Managment Hooks */
206 #ifdef CONFIG_PM
207         .suspend  = e1000_suspend,
208         .resume   = e1000_resume
209 #endif
210 };
211
212 MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
213 MODULE_DESCRIPTION("Intel(R) PRO/1000 Network Driver");
214 MODULE_LICENSE("GPL");
215 MODULE_VERSION(DRV_VERSION);
216
217 static int debug = NETIF_MSG_DRV | NETIF_MSG_PROBE;
218 module_param(debug, int, 0);
219 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
220
221 /**
222  * e1000_init_module - Driver Registration Routine
223  *
224  * e1000_init_module is the first routine called when the driver is
225  * loaded. All it does is register with the PCI subsystem.
226  **/
227
228 static int __init
229 e1000_init_module(void)
230 {
231         int ret;
232         printk(KERN_INFO "%s - version %s\n",
233                e1000_driver_string, e1000_driver_version);
234
235         printk(KERN_INFO "%s\n", e1000_copyright);
236
237         ret = pci_module_init(&e1000_driver);
238         if(ret >= 0) {
239                 register_reboot_notifier(&e1000_notifier_reboot);
240         }
241         return ret;
242 }
243
244 module_init(e1000_init_module);
245
246 /**
247  * e1000_exit_module - Driver Exit Cleanup Routine
248  *
249  * e1000_exit_module is called just before the driver is removed
250  * from memory.
251  **/
252
253 static void __exit
254 e1000_exit_module(void)
255 {
256         unregister_reboot_notifier(&e1000_notifier_reboot);
257         pci_unregister_driver(&e1000_driver);
258 }
259
260 module_exit(e1000_exit_module);
261
262 /**
263  * e1000_irq_disable - Mask off interrupt generation on the NIC
264  * @adapter: board private structure
265  **/
266
267 static inline void
268 e1000_irq_disable(struct e1000_adapter *adapter)
269 {
270         atomic_inc(&adapter->irq_sem);
271         E1000_WRITE_REG(&adapter->hw, IMC, ~0);
272         E1000_WRITE_FLUSH(&adapter->hw);
273         synchronize_irq(adapter->pdev->irq);
274 }
275
276 /**
277  * e1000_irq_enable - Enable default interrupt generation settings
278  * @adapter: board private structure
279  **/
280
281 static inline void
282 e1000_irq_enable(struct e1000_adapter *adapter)
283 {
284         if(likely(atomic_dec_and_test(&adapter->irq_sem))) {
285                 E1000_WRITE_REG(&adapter->hw, IMS, IMS_ENABLE_MASK);
286                 E1000_WRITE_FLUSH(&adapter->hw);
287         }
288 }
289
290 int
291 e1000_up(struct e1000_adapter *adapter)
292 {
293         struct net_device *netdev = adapter->netdev;
294         int err;
295
296         /* hardware has been reset, we need to reload some things */
297
298         /* Reset the PHY if it was previously powered down */
299         if(adapter->hw.media_type == e1000_media_type_copper) {
300                 uint16_t mii_reg;
301                 e1000_read_phy_reg(&adapter->hw, PHY_CTRL, &mii_reg);
302                 if(mii_reg & MII_CR_POWER_DOWN)
303                         e1000_phy_reset(&adapter->hw);
304         }
305
306         e1000_set_multi(netdev);
307
308         e1000_restore_vlan(adapter);
309
310         e1000_configure_tx(adapter);
311         e1000_setup_rctl(adapter);
312         e1000_configure_rx(adapter);
313         e1000_alloc_rx_buffers(adapter);
314
315 #ifdef CONFIG_PCI_MSI
316         if(adapter->hw.mac_type > e1000_82547_rev_2) {
317                 adapter->have_msi = TRUE;
318                 if((err = pci_enable_msi(adapter->pdev))) {
319                         DPRINTK(PROBE, ERR,
320                          "Unable to allocate MSI interrupt Error: %d\n", err);
321                         adapter->have_msi = FALSE;
322                 }
323         }
324 #endif
325         if((err = request_irq(adapter->pdev->irq, &e1000_intr,
326                               SA_SHIRQ | SA_SAMPLE_RANDOM,
327                               netdev->name, netdev)))
328                 return err;
329
330         mod_timer(&adapter->watchdog_timer, jiffies);
331
332 #ifdef CONFIG_E1000_NAPI
333         netif_poll_enable(netdev);
334 #endif
335         e1000_irq_enable(adapter);
336
337         return 0;
338 }
339
340 void
341 e1000_down(struct e1000_adapter *adapter)
342 {
343         struct net_device *netdev = adapter->netdev;
344
345         e1000_irq_disable(adapter);
346         free_irq(adapter->pdev->irq, netdev);
347 #ifdef CONFIG_PCI_MSI
348         if(adapter->hw.mac_type > e1000_82547_rev_2 &&
349            adapter->have_msi == TRUE)
350                 pci_disable_msi(adapter->pdev);
351 #endif
352         del_timer_sync(&adapter->tx_fifo_stall_timer);
353         del_timer_sync(&adapter->watchdog_timer);
354         del_timer_sync(&adapter->phy_info_timer);
355
356 #ifdef CONFIG_E1000_NAPI
357         netif_poll_disable(netdev);
358 #endif
359         adapter->link_speed = 0;
360         adapter->link_duplex = 0;
361         netif_carrier_off(netdev);
362         netif_stop_queue(netdev);
363
364         e1000_reset(adapter);
365         e1000_clean_tx_ring(adapter);
366         e1000_clean_rx_ring(adapter);
367
368         /* If WoL is not enabled
369          * Power down the PHY so no link is implied when interface is down */
370         if(!adapter->wol && adapter->hw.media_type == e1000_media_type_copper) {
371                 uint16_t mii_reg;
372                 e1000_read_phy_reg(&adapter->hw, PHY_CTRL, &mii_reg);
373                 mii_reg |= MII_CR_POWER_DOWN;
374                 e1000_write_phy_reg(&adapter->hw, PHY_CTRL, mii_reg);
375         }
376 }
377
378 void
379 e1000_reset(struct e1000_adapter *adapter)
380 {
381         uint32_t pba;
382
383         /* Repartition Pba for greater than 9k mtu
384          * To take effect CTRL.RST is required.
385          */
386
387         if(adapter->hw.mac_type < e1000_82547) {
388                 if(adapter->rx_buffer_len > E1000_RXBUFFER_8192)
389                         pba = E1000_PBA_40K;
390                 else
391                         pba = E1000_PBA_48K;
392         } else {
393                 if(adapter->rx_buffer_len > E1000_RXBUFFER_8192)
394                         pba = E1000_PBA_22K;
395                 else
396                         pba = E1000_PBA_30K;
397                 adapter->tx_fifo_head = 0;
398                 adapter->tx_head_addr = pba << E1000_TX_HEAD_ADDR_SHIFT;
399                 adapter->tx_fifo_size =
400                         (E1000_PBA_40K - pba) << E1000_PBA_BYTES_SHIFT;
401                 atomic_set(&adapter->tx_fifo_stall, 0);
402         }
403         E1000_WRITE_REG(&adapter->hw, PBA, pba);
404
405         /* flow control settings */
406         adapter->hw.fc_high_water = (pba << E1000_PBA_BYTES_SHIFT) -
407                                     E1000_FC_HIGH_DIFF;
408         adapter->hw.fc_low_water = (pba << E1000_PBA_BYTES_SHIFT) -
409                                    E1000_FC_LOW_DIFF;
410         adapter->hw.fc_pause_time = E1000_FC_PAUSE_TIME;
411         adapter->hw.fc_send_xon = 1;
412         adapter->hw.fc = adapter->hw.original_fc;
413
414         e1000_reset_hw(&adapter->hw);
415         if(adapter->hw.mac_type >= e1000_82544)
416                 E1000_WRITE_REG(&adapter->hw, WUC, 0);
417         if(e1000_init_hw(&adapter->hw))
418                 DPRINTK(PROBE, ERR, "Hardware Error\n");
419
420         /* Enable h/w to recognize an 802.1Q VLAN Ethernet packet */
421         E1000_WRITE_REG(&adapter->hw, VET, ETHERNET_IEEE_VLAN_TYPE);
422
423         e1000_reset_adaptive(&adapter->hw);
424         e1000_phy_get_info(&adapter->hw, &adapter->phy_info);
425 }
426
427 /**
428  * e1000_probe - Device Initialization Routine
429  * @pdev: PCI device information struct
430  * @ent: entry in e1000_pci_tbl
431  *
432  * Returns 0 on success, negative on failure
433  *
434  * e1000_probe initializes an adapter identified by a pci_dev structure.
435  * The OS initialization, configuring of the adapter private structure,
436  * and a hardware reset occur.
437  **/
438
439 static int __devinit
440 e1000_probe(struct pci_dev *pdev,
441             const struct pci_device_id *ent)
442 {
443         struct net_device *netdev;
444         struct e1000_adapter *adapter;
445         static int cards_found = 0;
446         unsigned long mmio_start;
447         int mmio_len;
448         int pci_using_dac;
449         int i;
450         int err;
451         uint16_t eeprom_data;
452         uint16_t eeprom_apme_mask = E1000_EEPROM_APME;
453
454         if((err = pci_enable_device(pdev)))
455                 return err;
456
457         if(!(err = pci_set_dma_mask(pdev, DMA_64BIT_MASK))) {
458                 pci_using_dac = 1;
459         } else {
460                 if((err = pci_set_dma_mask(pdev, DMA_32BIT_MASK))) {
461                         E1000_ERR("No usable DMA configuration, aborting\n");
462                         return err;
463                 }
464                 pci_using_dac = 0;
465         }
466
467         if((err = pci_request_regions(pdev, e1000_driver_name)))
468                 return err;
469
470         pci_set_master(pdev);
471
472         netdev = alloc_etherdev(sizeof(struct e1000_adapter));
473         if(!netdev) {
474                 err = -ENOMEM;
475                 goto err_alloc_etherdev;
476         }
477
478         SET_MODULE_OWNER(netdev);
479         SET_NETDEV_DEV(netdev, &pdev->dev);
480
481         pci_set_drvdata(pdev, netdev);
482         adapter = netdev->priv;
483         adapter->netdev = netdev;
484         adapter->pdev = pdev;
485         adapter->hw.back = adapter;
486         adapter->msg_enable = (1 << debug) - 1;
487
488         mmio_start = pci_resource_start(pdev, BAR_0);
489         mmio_len = pci_resource_len(pdev, BAR_0);
490
491         adapter->hw.hw_addr = ioremap(mmio_start, mmio_len);
492         if(!adapter->hw.hw_addr) {
493                 err = -EIO;
494                 goto err_ioremap;
495         }
496
497         for(i = BAR_1; i <= BAR_5; i++) {
498                 if(pci_resource_len(pdev, i) == 0)
499                         continue;
500                 if(pci_resource_flags(pdev, i) & IORESOURCE_IO) {
501                         adapter->hw.io_base = pci_resource_start(pdev, i);
502                         break;
503                 }
504         }
505
506         netdev->open = &e1000_open;
507         netdev->stop = &e1000_close;
508         netdev->hard_start_xmit = &e1000_xmit_frame;
509         netdev->get_stats = &e1000_get_stats;
510         netdev->set_multicast_list = &e1000_set_multi;
511         netdev->set_mac_address = &e1000_set_mac;
512         netdev->change_mtu = &e1000_change_mtu;
513         netdev->do_ioctl = &e1000_ioctl;
514         e1000_set_ethtool_ops(netdev);
515         netdev->tx_timeout = &e1000_tx_timeout;
516         netdev->watchdog_timeo = 5 * HZ;
517 #ifdef CONFIG_E1000_NAPI
518         netdev->poll = &e1000_clean;
519         netdev->weight = 64;
520 #endif
521         netdev->vlan_rx_register = e1000_vlan_rx_register;
522         netdev->vlan_rx_add_vid = e1000_vlan_rx_add_vid;
523         netdev->vlan_rx_kill_vid = e1000_vlan_rx_kill_vid;
524 #ifdef CONFIG_NET_POLL_CONTROLLER
525         netdev->poll_controller = e1000_netpoll;
526 #endif
527         strcpy(netdev->name, pci_name(pdev));
528
529         netdev->mem_start = mmio_start;
530         netdev->mem_end = mmio_start + mmio_len;
531         netdev->base_addr = adapter->hw.io_base;
532
533         adapter->bd_number = cards_found;
534
535         /* setup the private structure */
536
537         if((err = e1000_sw_init(adapter)))
538                 goto err_sw_init;
539
540         if(adapter->hw.mac_type >= e1000_82543) {
541                 netdev->features = NETIF_F_SG |
542                                    NETIF_F_HW_CSUM |
543                                    NETIF_F_HW_VLAN_TX |
544                                    NETIF_F_HW_VLAN_RX |
545                                    NETIF_F_HW_VLAN_FILTER;
546         }
547
548 #ifdef NETIF_F_TSO
549         if((adapter->hw.mac_type >= e1000_82544) &&
550            (adapter->hw.mac_type != e1000_82547))
551                 netdev->features |= NETIF_F_TSO;
552 #endif
553         if(pci_using_dac)
554                 netdev->features |= NETIF_F_HIGHDMA;
555
556         /* hard_start_xmit is safe against parallel locking */
557         netdev->features |= NETIF_F_LLTX; 
558  
559         /* before reading the EEPROM, reset the controller to 
560          * put the device in a known good starting state */
561         
562         e1000_reset_hw(&adapter->hw);
563
564         /* make sure the EEPROM is good */
565
566         if(e1000_validate_eeprom_checksum(&adapter->hw) < 0) {
567                 DPRINTK(PROBE, ERR, "The EEPROM Checksum Is Not Valid\n");
568                 err = -EIO;
569                 goto err_eeprom;
570         }
571
572         /* copy the MAC address out of the EEPROM */
573
574         if (e1000_read_mac_addr(&adapter->hw))
575                 DPRINTK(PROBE, ERR, "EEPROM Read Error\n");
576         memcpy(netdev->dev_addr, adapter->hw.mac_addr, netdev->addr_len);
577
578         if(!is_valid_ether_addr(netdev->dev_addr)) {
579                 DPRINTK(PROBE, ERR, "Invalid MAC Address\n");
580                 err = -EIO;
581                 goto err_eeprom;
582         }
583
584         e1000_read_part_num(&adapter->hw, &(adapter->part_num));
585
586         e1000_get_bus_info(&adapter->hw);
587
588         init_timer(&adapter->tx_fifo_stall_timer);
589         adapter->tx_fifo_stall_timer.function = &e1000_82547_tx_fifo_stall;
590         adapter->tx_fifo_stall_timer.data = (unsigned long) adapter;
591
592         init_timer(&adapter->watchdog_timer);
593         adapter->watchdog_timer.function = &e1000_watchdog;
594         adapter->watchdog_timer.data = (unsigned long) adapter;
595
596         INIT_WORK(&adapter->watchdog_task,
597                 (void (*)(void *))e1000_watchdog_task, adapter);
598
599         init_timer(&adapter->phy_info_timer);
600         adapter->phy_info_timer.function = &e1000_update_phy_info;
601         adapter->phy_info_timer.data = (unsigned long) adapter;
602
603         INIT_WORK(&adapter->tx_timeout_task,
604                 (void (*)(void *))e1000_tx_timeout_task, netdev);
605
606         /* we're going to reset, so assume we have no link for now */
607
608         netif_carrier_off(netdev);
609         netif_stop_queue(netdev);
610
611         e1000_check_options(adapter);
612
613         /* Initial Wake on LAN setting
614          * If APM wake is enabled in the EEPROM,
615          * enable the ACPI Magic Packet filter
616          */
617
618         switch(adapter->hw.mac_type) {
619         case e1000_82542_rev2_0:
620         case e1000_82542_rev2_1:
621         case e1000_82543:
622                 break;
623         case e1000_82544:
624                 e1000_read_eeprom(&adapter->hw,
625                         EEPROM_INIT_CONTROL2_REG, 1, &eeprom_data);
626                 eeprom_apme_mask = E1000_EEPROM_82544_APM;
627                 break;
628         case e1000_82546:
629         case e1000_82546_rev_3:
630                 if((E1000_READ_REG(&adapter->hw, STATUS) & E1000_STATUS_FUNC_1)
631                    && (adapter->hw.media_type == e1000_media_type_copper)) {
632                         e1000_read_eeprom(&adapter->hw,
633                                 EEPROM_INIT_CONTROL3_PORT_B, 1, &eeprom_data);
634                         break;
635                 }
636                 /* Fall Through */
637         default:
638                 e1000_read_eeprom(&adapter->hw,
639                         EEPROM_INIT_CONTROL3_PORT_A, 1, &eeprom_data);
640                 break;
641         }
642         if(eeprom_data & eeprom_apme_mask)
643                 adapter->wol |= E1000_WUFC_MAG;
644
645         /* reset the hardware with the new settings */
646         e1000_reset(adapter);
647
648         strcpy(netdev->name, "eth%d");
649         if((err = register_netdev(netdev)))
650                 goto err_register;
651
652         DPRINTK(PROBE, INFO, "Intel(R) PRO/1000 Network Connection\n");
653
654         cards_found++;
655         return 0;
656
657 err_register:
658 err_sw_init:
659 err_eeprom:
660         iounmap(adapter->hw.hw_addr);
661 err_ioremap:
662         free_netdev(netdev);
663 err_alloc_etherdev:
664         pci_release_regions(pdev);
665         return err;
666 }
667
668 /**
669  * e1000_remove - Device Removal Routine
670  * @pdev: PCI device information struct
671  *
672  * e1000_remove is called by the PCI subsystem to alert the driver
673  * that it should release a PCI device.  The could be caused by a
674  * Hot-Plug event, or because the driver is going to be removed from
675  * memory.
676  **/
677
678 static void __devexit
679 e1000_remove(struct pci_dev *pdev)
680 {
681         struct net_device *netdev = pci_get_drvdata(pdev);
682         struct e1000_adapter *adapter = netdev->priv;
683         uint32_t manc;
684
685         flush_scheduled_work();
686
687         if(adapter->hw.mac_type >= e1000_82540 &&
688            adapter->hw.media_type == e1000_media_type_copper) {
689                 manc = E1000_READ_REG(&adapter->hw, MANC);
690                 if(manc & E1000_MANC_SMBUS_EN) {
691                         manc |= E1000_MANC_ARP_EN;
692                         E1000_WRITE_REG(&adapter->hw, MANC, manc);
693                 }
694         }
695
696         unregister_netdev(netdev);
697
698         e1000_phy_hw_reset(&adapter->hw);
699
700         iounmap(adapter->hw.hw_addr);
701         pci_release_regions(pdev);
702
703         free_netdev(netdev);
704
705         pci_disable_device(pdev);
706 }
707
708 /**
709  * e1000_sw_init - Initialize general software structures (struct e1000_adapter)
710  * @adapter: board private structure to initialize
711  *
712  * e1000_sw_init initializes the Adapter private data structure.
713  * Fields are initialized based on PCI device information and
714  * OS network device settings (MTU size).
715  **/
716
717 static int __devinit
718 e1000_sw_init(struct e1000_adapter *adapter)
719 {
720         struct e1000_hw *hw = &adapter->hw;
721         struct net_device *netdev = adapter->netdev;
722         struct pci_dev *pdev = adapter->pdev;
723
724         /* PCI config space info */
725
726         hw->vendor_id = pdev->vendor;
727         hw->device_id = pdev->device;
728         hw->subsystem_vendor_id = pdev->subsystem_vendor;
729         hw->subsystem_id = pdev->subsystem_device;
730
731         pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
732
733         pci_read_config_word(pdev, PCI_COMMAND, &hw->pci_cmd_word);
734
735         adapter->rx_buffer_len = E1000_RXBUFFER_2048;
736         hw->max_frame_size = netdev->mtu +
737                              ENET_HEADER_SIZE + ETHERNET_FCS_SIZE;
738         hw->min_frame_size = MINIMUM_ETHERNET_FRAME_SIZE;
739
740         /* identify the MAC */
741
742         if(e1000_set_mac_type(hw)) {
743                 DPRINTK(PROBE, ERR, "Unknown MAC Type\n");
744                 return -EIO;
745         }
746
747         /* initialize eeprom parameters */
748
749         e1000_init_eeprom_params(hw);
750
751         switch(hw->mac_type) {
752         default:
753                 break;
754         case e1000_82541:
755         case e1000_82547:
756         case e1000_82541_rev_2:
757         case e1000_82547_rev_2:
758                 hw->phy_init_script = 1;
759                 break;
760         }
761
762         e1000_set_media_type(hw);
763
764         hw->wait_autoneg_complete = FALSE;
765         hw->tbi_compatibility_en = TRUE;
766         hw->adaptive_ifs = TRUE;
767
768         /* Copper options */
769
770         if(hw->media_type == e1000_media_type_copper) {
771                 hw->mdix = AUTO_ALL_MODES;
772                 hw->disable_polarity_correction = FALSE;
773                 hw->master_slave = E1000_MASTER_SLAVE;
774         }
775
776         atomic_set(&adapter->irq_sem, 1);
777         spin_lock_init(&adapter->stats_lock);
778         spin_lock_init(&adapter->tx_lock);
779
780         return 0;
781 }
782
783 /**
784  * e1000_open - Called when a network interface is made active
785  * @netdev: network interface device structure
786  *
787  * Returns 0 on success, negative value on failure
788  *
789  * The open entry point is called when a network interface is made
790  * active by the system (IFF_UP).  At this point all resources needed
791  * for transmit and receive operations are allocated, the interrupt
792  * handler is registered with the OS, the watchdog timer is started,
793  * and the stack is notified that the interface is ready.
794  **/
795
796 static int
797 e1000_open(struct net_device *netdev)
798 {
799         struct e1000_adapter *adapter = netdev->priv;
800         int err;
801
802         /* allocate transmit descriptors */
803
804         if((err = e1000_setup_tx_resources(adapter)))
805                 goto err_setup_tx;
806
807         /* allocate receive descriptors */
808
809         if((err = e1000_setup_rx_resources(adapter)))
810                 goto err_setup_rx;
811
812         if((err = e1000_up(adapter)))
813                 goto err_up;
814
815         return E1000_SUCCESS;
816
817 err_up:
818         e1000_free_rx_resources(adapter);
819 err_setup_rx:
820         e1000_free_tx_resources(adapter);
821 err_setup_tx:
822         e1000_reset(adapter);
823
824         return err;
825 }
826
827 /**
828  * e1000_close - Disables a network interface
829  * @netdev: network interface device structure
830  *
831  * Returns 0, this is not allowed to fail
832  *
833  * The close entry point is called when an interface is de-activated
834  * by the OS.  The hardware is still under the drivers control, but
835  * needs to be disabled.  A global MAC reset is issued to stop the
836  * hardware, and all transmit and receive resources are freed.
837  **/
838
839 static int
840 e1000_close(struct net_device *netdev)
841 {
842         struct e1000_adapter *adapter = netdev->priv;
843
844         e1000_down(adapter);
845
846         e1000_free_tx_resources(adapter);
847         e1000_free_rx_resources(adapter);
848
849         return 0;
850 }
851
852 /**
853  * e1000_check_64k_bound - check that memory doesn't cross 64kB boundary
854  * @adapter: address of board private structure
855  * @begin: address of beginning of memory
856  * @end: address of end of memory
857  **/
858 static inline boolean_t
859 e1000_check_64k_bound(struct e1000_adapter *adapter,
860                       void *start, unsigned long len)
861 {
862         unsigned long begin = (unsigned long) start;
863         unsigned long end = begin + len;
864
865         /* first rev 82545 and 82546 need to not allow any memory
866          * write location to cross a 64k boundary due to errata 23 */
867         if (adapter->hw.mac_type == e1000_82545 ||
868             adapter->hw.mac_type == e1000_82546 ) {
869
870                 /* check buffer doesn't cross 64kB */
871                 return ((begin ^ (end - 1)) >> 16) != 0 ? FALSE : TRUE;
872         }
873
874         return TRUE;
875 }
876
877 /**
878  * e1000_setup_tx_resources - allocate Tx resources (Descriptors)
879  * @adapter: board private structure
880  *
881  * Return 0 on success, negative on failure
882  **/
883
884 int
885 e1000_setup_tx_resources(struct e1000_adapter *adapter)
886 {
887         struct e1000_desc_ring *txdr = &adapter->tx_ring;
888         struct pci_dev *pdev = adapter->pdev;
889         int size;
890
891         size = sizeof(struct e1000_buffer) * txdr->count;
892         txdr->buffer_info = vmalloc(size);
893         if(!txdr->buffer_info) {
894                 DPRINTK(PROBE, ERR, 
895                 "Unable to Allocate Memory for the Transmit descriptor ring\n");
896                 return -ENOMEM;
897         }
898         memset(txdr->buffer_info, 0, size);
899
900         /* round up to nearest 4K */
901
902         txdr->size = txdr->count * sizeof(struct e1000_tx_desc);
903         E1000_ROUNDUP(txdr->size, 4096);
904
905         txdr->desc = pci_alloc_consistent(pdev, txdr->size, &txdr->dma);
906         if(!txdr->desc) {
907 setup_tx_desc_die:
908                 DPRINTK(PROBE, ERR, 
909                 "Unable to Allocate Memory for the Transmit descriptor ring\n");
910                 vfree(txdr->buffer_info);
911                 return -ENOMEM;
912         }
913
914         /* fix for errata 23, cant cross 64kB boundary */
915         if (!e1000_check_64k_bound(adapter, txdr->desc, txdr->size)) {
916                 void *olddesc = txdr->desc;
917                 dma_addr_t olddma = txdr->dma;
918                 DPRINTK(TX_ERR,ERR,"txdr align check failed: %u bytes at %p\n",
919                         txdr->size, txdr->desc);
920                 /* try again, without freeing the previous */
921                 txdr->desc = pci_alloc_consistent(pdev, txdr->size, &txdr->dma);
922                 /* failed allocation, critial failure */
923                 if(!txdr->desc) {
924                         pci_free_consistent(pdev, txdr->size, olddesc, olddma);
925                         goto setup_tx_desc_die;
926                 }
927
928                 if (!e1000_check_64k_bound(adapter, txdr->desc, txdr->size)) {
929                         /* give up */
930                         pci_free_consistent(pdev, txdr->size,
931                              txdr->desc, txdr->dma);
932                         pci_free_consistent(pdev, txdr->size, olddesc, olddma);
933                         DPRINTK(PROBE, ERR,
934                          "Unable to Allocate aligned Memory for the Transmit"
935                          " descriptor ring\n");
936                         vfree(txdr->buffer_info);
937                         return -ENOMEM;
938                 } else {
939                         /* free old, move on with the new one since its okay */
940                         pci_free_consistent(pdev, txdr->size, olddesc, olddma);
941                 }
942         }
943         memset(txdr->desc, 0, txdr->size);
944
945         txdr->next_to_use = 0;
946         txdr->next_to_clean = 0;
947
948         return 0;
949 }
950
951 /**
952  * e1000_configure_tx - Configure 8254x Transmit Unit after Reset
953  * @adapter: board private structure
954  *
955  * Configure the Tx unit of the MAC after a reset.
956  **/
957
958 static void
959 e1000_configure_tx(struct e1000_adapter *adapter)
960 {
961         uint64_t tdba = adapter->tx_ring.dma;
962         uint32_t tdlen = adapter->tx_ring.count * sizeof(struct e1000_tx_desc);
963         uint32_t tctl, tipg;
964
965         E1000_WRITE_REG(&adapter->hw, TDBAL, (tdba & 0x00000000ffffffffULL));
966         E1000_WRITE_REG(&adapter->hw, TDBAH, (tdba >> 32));
967
968         E1000_WRITE_REG(&adapter->hw, TDLEN, tdlen);
969
970         /* Setup the HW Tx Head and Tail descriptor pointers */
971
972         E1000_WRITE_REG(&adapter->hw, TDH, 0);
973         E1000_WRITE_REG(&adapter->hw, TDT, 0);
974
975         /* Set the default values for the Tx Inter Packet Gap timer */
976
977         switch (adapter->hw.mac_type) {
978         case e1000_82542_rev2_0:
979         case e1000_82542_rev2_1:
980                 tipg = DEFAULT_82542_TIPG_IPGT;
981                 tipg |= DEFAULT_82542_TIPG_IPGR1 << E1000_TIPG_IPGR1_SHIFT;
982                 tipg |= DEFAULT_82542_TIPG_IPGR2 << E1000_TIPG_IPGR2_SHIFT;
983                 break;
984         default:
985                 if(adapter->hw.media_type == e1000_media_type_fiber ||
986                    adapter->hw.media_type == e1000_media_type_internal_serdes)
987                         tipg = DEFAULT_82543_TIPG_IPGT_FIBER;
988                 else
989                         tipg = DEFAULT_82543_TIPG_IPGT_COPPER;
990                 tipg |= DEFAULT_82543_TIPG_IPGR1 << E1000_TIPG_IPGR1_SHIFT;
991                 tipg |= DEFAULT_82543_TIPG_IPGR2 << E1000_TIPG_IPGR2_SHIFT;
992         }
993         E1000_WRITE_REG(&adapter->hw, TIPG, tipg);
994
995         /* Set the Tx Interrupt Delay register */
996
997         E1000_WRITE_REG(&adapter->hw, TIDV, adapter->tx_int_delay);
998         if(adapter->hw.mac_type >= e1000_82540)
999                 E1000_WRITE_REG(&adapter->hw, TADV, adapter->tx_abs_int_delay);
1000
1001         /* Program the Transmit Control Register */
1002
1003         tctl = E1000_READ_REG(&adapter->hw, TCTL);
1004
1005         tctl &= ~E1000_TCTL_CT;
1006         tctl |= E1000_TCTL_EN | E1000_TCTL_PSP |
1007                 (E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT);
1008
1009         E1000_WRITE_REG(&adapter->hw, TCTL, tctl);
1010
1011         e1000_config_collision_dist(&adapter->hw);
1012
1013         /* Setup Transmit Descriptor Settings for eop descriptor */
1014         adapter->txd_cmd = E1000_TXD_CMD_IDE | E1000_TXD_CMD_EOP |
1015                 E1000_TXD_CMD_IFCS;
1016
1017         if(adapter->hw.mac_type < e1000_82543)
1018                 adapter->txd_cmd |= E1000_TXD_CMD_RPS;
1019         else
1020                 adapter->txd_cmd |= E1000_TXD_CMD_RS;
1021
1022         /* Cache if we're 82544 running in PCI-X because we'll
1023          * need this to apply a workaround later in the send path. */
1024         if(adapter->hw.mac_type == e1000_82544 &&
1025            adapter->hw.bus_type == e1000_bus_type_pcix)
1026                 adapter->pcix_82544 = 1;
1027 }
1028
1029 /**
1030  * e1000_setup_rx_resources - allocate Rx resources (Descriptors)
1031  * @adapter: board private structure
1032  *
1033  * Returns 0 on success, negative on failure
1034  **/
1035
1036 int
1037 e1000_setup_rx_resources(struct e1000_adapter *adapter)
1038 {
1039         struct e1000_desc_ring *rxdr = &adapter->rx_ring;
1040         struct pci_dev *pdev = adapter->pdev;
1041         int size;
1042
1043         size = sizeof(struct e1000_buffer) * rxdr->count;
1044         rxdr->buffer_info = vmalloc(size);
1045         if(!rxdr->buffer_info) {
1046                 DPRINTK(PROBE, ERR, 
1047                 "Unable to Allocate Memory for the Recieve descriptor ring\n");
1048                 return -ENOMEM;
1049         }
1050         memset(rxdr->buffer_info, 0, size);
1051
1052         /* Round up to nearest 4K */
1053
1054         rxdr->size = rxdr->count * sizeof(struct e1000_rx_desc);
1055         E1000_ROUNDUP(rxdr->size, 4096);
1056
1057         rxdr->desc = pci_alloc_consistent(pdev, rxdr->size, &rxdr->dma);
1058
1059         if(!rxdr->desc) {
1060 setup_rx_desc_die:
1061                 DPRINTK(PROBE, ERR, 
1062                 "Unble to Allocate Memory for the Recieve descriptor ring\n");
1063                 vfree(rxdr->buffer_info);
1064                 return -ENOMEM;
1065         }
1066
1067         /* fix for errata 23, cant cross 64kB boundary */
1068         if (!e1000_check_64k_bound(adapter, rxdr->desc, rxdr->size)) {
1069                 void *olddesc = rxdr->desc;
1070                 dma_addr_t olddma = rxdr->dma;
1071                 DPRINTK(RX_ERR,ERR,
1072                         "rxdr align check failed: %u bytes at %p\n",
1073                         rxdr->size, rxdr->desc);
1074                 /* try again, without freeing the previous */
1075                 rxdr->desc = pci_alloc_consistent(pdev, rxdr->size, &rxdr->dma);
1076                 /* failed allocation, critial failure */
1077                 if(!rxdr->desc) {
1078                         pci_free_consistent(pdev, rxdr->size, olddesc, olddma);
1079                         goto setup_rx_desc_die;
1080                 }
1081
1082                 if (!e1000_check_64k_bound(adapter, rxdr->desc, rxdr->size)) {
1083                         /* give up */
1084                         pci_free_consistent(pdev, rxdr->size,
1085                              rxdr->desc, rxdr->dma);
1086                         pci_free_consistent(pdev, rxdr->size, olddesc, olddma);
1087                         DPRINTK(PROBE, ERR, 
1088                                 "Unable to Allocate aligned Memory for the"
1089                                 " Receive descriptor ring\n");
1090                         vfree(rxdr->buffer_info);
1091                         return -ENOMEM;
1092                 } else {
1093                         /* free old, move on with the new one since its okay */
1094                         pci_free_consistent(pdev, rxdr->size, olddesc, olddma);
1095                 }
1096         }
1097         memset(rxdr->desc, 0, rxdr->size);
1098
1099         rxdr->next_to_clean = 0;
1100         rxdr->next_to_use = 0;
1101
1102         return 0;
1103 }
1104
1105 /**
1106  * e1000_setup_rctl - configure the receive control register
1107  * @adapter: Board private structure
1108  **/
1109
1110 static void
1111 e1000_setup_rctl(struct e1000_adapter *adapter)
1112 {
1113         uint32_t rctl;
1114
1115         rctl = E1000_READ_REG(&adapter->hw, RCTL);
1116
1117         rctl &= ~(3 << E1000_RCTL_MO_SHIFT);
1118
1119         rctl |= E1000_RCTL_EN | E1000_RCTL_BAM |
1120                 E1000_RCTL_LBM_NO | E1000_RCTL_RDMTS_HALF |
1121                 (adapter->hw.mc_filter_type << E1000_RCTL_MO_SHIFT);
1122
1123         if(adapter->hw.tbi_compatibility_on == 1)
1124                 rctl |= E1000_RCTL_SBP;
1125         else
1126                 rctl &= ~E1000_RCTL_SBP;
1127
1128         /* Setup buffer sizes */
1129         rctl &= ~(E1000_RCTL_SZ_4096);
1130         rctl |= (E1000_RCTL_BSEX | E1000_RCTL_LPE);
1131         switch (adapter->rx_buffer_len) {
1132         case E1000_RXBUFFER_2048:
1133         default:
1134                 rctl |= E1000_RCTL_SZ_2048;
1135                 rctl &= ~(E1000_RCTL_BSEX | E1000_RCTL_LPE);
1136                 break;
1137         case E1000_RXBUFFER_4096:
1138                 rctl |= E1000_RCTL_SZ_4096;
1139                 break;
1140         case E1000_RXBUFFER_8192:
1141                 rctl |= E1000_RCTL_SZ_8192;
1142                 break;
1143         case E1000_RXBUFFER_16384:
1144                 rctl |= E1000_RCTL_SZ_16384;
1145                 break;
1146         }
1147
1148         E1000_WRITE_REG(&adapter->hw, RCTL, rctl);
1149 }
1150
1151 /**
1152  * e1000_configure_rx - Configure 8254x Receive Unit after Reset
1153  * @adapter: board private structure
1154  *
1155  * Configure the Rx unit of the MAC after a reset.
1156  **/
1157
1158 static void
1159 e1000_configure_rx(struct e1000_adapter *adapter)
1160 {
1161         uint64_t rdba = adapter->rx_ring.dma;
1162         uint32_t rdlen = adapter->rx_ring.count * sizeof(struct e1000_rx_desc);
1163         uint32_t rctl;
1164         uint32_t rxcsum;
1165
1166         /* disable receives while setting up the descriptors */
1167         rctl = E1000_READ_REG(&adapter->hw, RCTL);
1168         E1000_WRITE_REG(&adapter->hw, RCTL, rctl & ~E1000_RCTL_EN);
1169
1170         /* set the Receive Delay Timer Register */
1171         E1000_WRITE_REG(&adapter->hw, RDTR, adapter->rx_int_delay);
1172
1173         if(adapter->hw.mac_type >= e1000_82540) {
1174                 E1000_WRITE_REG(&adapter->hw, RADV, adapter->rx_abs_int_delay);
1175                 if(adapter->itr > 1)
1176                         E1000_WRITE_REG(&adapter->hw, ITR,
1177                                 1000000000 / (adapter->itr * 256));
1178         }
1179
1180         /* Setup the Base and Length of the Rx Descriptor Ring */
1181         E1000_WRITE_REG(&adapter->hw, RDBAL, (rdba & 0x00000000ffffffffULL));
1182         E1000_WRITE_REG(&adapter->hw, RDBAH, (rdba >> 32));
1183
1184         E1000_WRITE_REG(&adapter->hw, RDLEN, rdlen);
1185
1186         /* Setup the HW Rx Head and Tail Descriptor Pointers */
1187         E1000_WRITE_REG(&adapter->hw, RDH, 0);
1188         E1000_WRITE_REG(&adapter->hw, RDT, 0);
1189
1190         /* Enable 82543 Receive Checksum Offload for TCP and UDP */
1191         if((adapter->hw.mac_type >= e1000_82543) &&
1192            (adapter->rx_csum == TRUE)) {
1193                 rxcsum = E1000_READ_REG(&adapter->hw, RXCSUM);
1194                 rxcsum |= E1000_RXCSUM_TUOFL;
1195                 E1000_WRITE_REG(&adapter->hw, RXCSUM, rxcsum);
1196         }
1197
1198         /* Enable Receives */
1199         E1000_WRITE_REG(&adapter->hw, RCTL, rctl);
1200 }
1201
1202 /**
1203  * e1000_free_tx_resources - Free Tx Resources
1204  * @adapter: board private structure
1205  *
1206  * Free all transmit software resources
1207  **/
1208
1209 void
1210 e1000_free_tx_resources(struct e1000_adapter *adapter)
1211 {
1212         struct pci_dev *pdev = adapter->pdev;
1213
1214         e1000_clean_tx_ring(adapter);
1215
1216         vfree(adapter->tx_ring.buffer_info);
1217         adapter->tx_ring.buffer_info = NULL;
1218
1219         pci_free_consistent(pdev, adapter->tx_ring.size,
1220                             adapter->tx_ring.desc, adapter->tx_ring.dma);
1221
1222         adapter->tx_ring.desc = NULL;
1223 }
1224
1225 static inline void
1226 e1000_unmap_and_free_tx_resource(struct e1000_adapter *adapter,
1227                         struct e1000_buffer *buffer_info)
1228 {
1229         struct pci_dev *pdev = adapter->pdev;
1230
1231         if(buffer_info->dma) {
1232                 pci_unmap_page(pdev,
1233                                buffer_info->dma,
1234                                buffer_info->length,
1235                                PCI_DMA_TODEVICE);
1236                 buffer_info->dma = 0;
1237         }
1238         if(buffer_info->skb) {
1239                 dev_kfree_skb_any(buffer_info->skb);
1240                 buffer_info->skb = NULL;
1241         }
1242 }
1243
1244 /**
1245  * e1000_clean_tx_ring - Free Tx Buffers
1246  * @adapter: board private structure
1247  **/
1248
1249 static void
1250 e1000_clean_tx_ring(struct e1000_adapter *adapter)
1251 {
1252         struct e1000_desc_ring *tx_ring = &adapter->tx_ring;
1253         struct e1000_buffer *buffer_info;
1254         unsigned long size;
1255         unsigned int i;
1256
1257         /* Free all the Tx ring sk_buffs */
1258
1259         if (likely(adapter->previous_buffer_info.skb != NULL)) {
1260                 e1000_unmap_and_free_tx_resource(adapter, 
1261                                 &adapter->previous_buffer_info);
1262         }
1263
1264         for(i = 0; i < tx_ring->count; i++) {
1265                 buffer_info = &tx_ring->buffer_info[i];
1266                 e1000_unmap_and_free_tx_resource(adapter, buffer_info);
1267         }
1268
1269         size = sizeof(struct e1000_buffer) * tx_ring->count;
1270         memset(tx_ring->buffer_info, 0, size);
1271
1272         /* Zero out the descriptor ring */
1273
1274         memset(tx_ring->desc, 0, tx_ring->size);
1275
1276         tx_ring->next_to_use = 0;
1277         tx_ring->next_to_clean = 0;
1278
1279         E1000_WRITE_REG(&adapter->hw, TDH, 0);
1280         E1000_WRITE_REG(&adapter->hw, TDT, 0);
1281 }
1282
1283 /**
1284  * e1000_free_rx_resources - Free Rx Resources
1285  * @adapter: board private structure
1286  *
1287  * Free all receive software resources
1288  **/
1289
1290 void
1291 e1000_free_rx_resources(struct e1000_adapter *adapter)
1292 {
1293         struct e1000_desc_ring *rx_ring = &adapter->rx_ring;
1294         struct pci_dev *pdev = adapter->pdev;
1295
1296         e1000_clean_rx_ring(adapter);
1297
1298         vfree(rx_ring->buffer_info);
1299         rx_ring->buffer_info = NULL;
1300
1301         pci_free_consistent(pdev, rx_ring->size, rx_ring->desc, rx_ring->dma);
1302
1303         rx_ring->desc = NULL;
1304 }
1305
1306 /**
1307  * e1000_clean_rx_ring - Free Rx Buffers
1308  * @adapter: board private structure
1309  **/
1310
1311 static void
1312 e1000_clean_rx_ring(struct e1000_adapter *adapter)
1313 {
1314         struct e1000_desc_ring *rx_ring = &adapter->rx_ring;
1315         struct e1000_buffer *buffer_info;
1316         struct pci_dev *pdev = adapter->pdev;
1317         unsigned long size;
1318         unsigned int i;
1319
1320         /* Free all the Rx ring sk_buffs */
1321
1322         for(i = 0; i < rx_ring->count; i++) {
1323                 buffer_info = &rx_ring->buffer_info[i];
1324                 if(buffer_info->skb) {
1325
1326                         pci_unmap_single(pdev,
1327                                          buffer_info->dma,
1328                                          buffer_info->length,
1329                                          PCI_DMA_FROMDEVICE);
1330
1331                         dev_kfree_skb(buffer_info->skb);
1332                         buffer_info->skb = NULL;
1333                 }
1334         }
1335
1336         size = sizeof(struct e1000_buffer) * rx_ring->count;
1337         memset(rx_ring->buffer_info, 0, size);
1338
1339         /* Zero out the descriptor ring */
1340
1341         memset(rx_ring->desc, 0, rx_ring->size);
1342
1343         rx_ring->next_to_clean = 0;
1344         rx_ring->next_to_use = 0;
1345
1346         E1000_WRITE_REG(&adapter->hw, RDH, 0);
1347         E1000_WRITE_REG(&adapter->hw, RDT, 0);
1348 }
1349
1350 /* The 82542 2.0 (revision 2) needs to have the receive unit in reset
1351  * and memory write and invalidate disabled for certain operations
1352  */
1353 static void
1354 e1000_enter_82542_rst(struct e1000_adapter *adapter)
1355 {
1356         struct net_device *netdev = adapter->netdev;
1357         uint32_t rctl;
1358
1359         e1000_pci_clear_mwi(&adapter->hw);
1360
1361         rctl = E1000_READ_REG(&adapter->hw, RCTL);
1362         rctl |= E1000_RCTL_RST;
1363         E1000_WRITE_REG(&adapter->hw, RCTL, rctl);
1364         E1000_WRITE_FLUSH(&adapter->hw);
1365         mdelay(5);
1366
1367         if(netif_running(netdev))
1368                 e1000_clean_rx_ring(adapter);
1369 }
1370
1371 static void
1372 e1000_leave_82542_rst(struct e1000_adapter *adapter)
1373 {
1374         struct net_device *netdev = adapter->netdev;
1375         uint32_t rctl;
1376
1377         rctl = E1000_READ_REG(&adapter->hw, RCTL);
1378         rctl &= ~E1000_RCTL_RST;
1379         E1000_WRITE_REG(&adapter->hw, RCTL, rctl);
1380         E1000_WRITE_FLUSH(&adapter->hw);
1381         mdelay(5);
1382
1383         if(adapter->hw.pci_cmd_word & PCI_COMMAND_INVALIDATE)
1384                 e1000_pci_set_mwi(&adapter->hw);
1385
1386         if(netif_running(netdev)) {
1387                 e1000_configure_rx(adapter);
1388                 e1000_alloc_rx_buffers(adapter);
1389         }
1390 }
1391
1392 /**
1393  * e1000_set_mac - Change the Ethernet Address of the NIC
1394  * @netdev: network interface device structure
1395  * @p: pointer to an address structure
1396  *
1397  * Returns 0 on success, negative on failure
1398  **/
1399
1400 static int
1401 e1000_set_mac(struct net_device *netdev, void *p)
1402 {
1403         struct e1000_adapter *adapter = netdev->priv;
1404         struct sockaddr *addr = p;
1405
1406         if(!is_valid_ether_addr(addr->sa_data))
1407                 return -EADDRNOTAVAIL;
1408
1409         /* 82542 2.0 needs to be in reset to write receive address registers */
1410
1411         if(adapter->hw.mac_type == e1000_82542_rev2_0)
1412                 e1000_enter_82542_rst(adapter);
1413
1414         memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
1415         memcpy(adapter->hw.mac_addr, addr->sa_data, netdev->addr_len);
1416
1417         e1000_rar_set(&adapter->hw, adapter->hw.mac_addr, 0);
1418
1419         if(adapter->hw.mac_type == e1000_82542_rev2_0)
1420                 e1000_leave_82542_rst(adapter);
1421
1422         return 0;
1423 }
1424
1425 /**
1426  * e1000_set_multi - Multicast and Promiscuous mode set
1427  * @netdev: network interface device structure
1428  *
1429  * The set_multi entry point is called whenever the multicast address
1430  * list or the network interface flags are updated.  This routine is
1431  * responsible for configuring the hardware for proper multicast,
1432  * promiscuous mode, and all-multi behavior.
1433  **/
1434
1435 static void
1436 e1000_set_multi(struct net_device *netdev)
1437 {
1438         struct e1000_adapter *adapter = netdev->priv;
1439         struct e1000_hw *hw = &adapter->hw;
1440         struct dev_mc_list *mc_ptr;
1441         uint32_t rctl;
1442         uint32_t hash_value;
1443         int i;
1444         unsigned long flags;
1445
1446         /* Check for Promiscuous and All Multicast modes */
1447
1448         spin_lock_irqsave(&adapter->tx_lock, flags);
1449
1450         rctl = E1000_READ_REG(hw, RCTL);
1451
1452         if(netdev->flags & IFF_PROMISC) {
1453                 rctl |= (E1000_RCTL_UPE | E1000_RCTL_MPE);
1454         } else if(netdev->flags & IFF_ALLMULTI) {
1455                 rctl |= E1000_RCTL_MPE;
1456                 rctl &= ~E1000_RCTL_UPE;
1457         } else {
1458                 rctl &= ~(E1000_RCTL_UPE | E1000_RCTL_MPE);
1459         }
1460
1461         E1000_WRITE_REG(hw, RCTL, rctl);
1462
1463         /* 82542 2.0 needs to be in reset to write receive address registers */
1464
1465         if(hw->mac_type == e1000_82542_rev2_0)
1466                 e1000_enter_82542_rst(adapter);
1467
1468         /* load the first 14 multicast address into the exact filters 1-14
1469          * RAR 0 is used for the station MAC adddress
1470          * if there are not 14 addresses, go ahead and clear the filters
1471          */
1472         mc_ptr = netdev->mc_list;
1473
1474         for(i = 1; i < E1000_RAR_ENTRIES; i++) {
1475                 if(mc_ptr) {
1476                         e1000_rar_set(hw, mc_ptr->dmi_addr, i);
1477                         mc_ptr = mc_ptr->next;
1478                 } else {
1479                         E1000_WRITE_REG_ARRAY(hw, RA, i << 1, 0);
1480                         E1000_WRITE_REG_ARRAY(hw, RA, (i << 1) + 1, 0);
1481                 }
1482         }
1483
1484         /* clear the old settings from the multicast hash table */
1485
1486         for(i = 0; i < E1000_NUM_MTA_REGISTERS; i++)
1487                 E1000_WRITE_REG_ARRAY(hw, MTA, i, 0);
1488
1489         /* load any remaining addresses into the hash table */
1490
1491         for(; mc_ptr; mc_ptr = mc_ptr->next) {
1492                 hash_value = e1000_hash_mc_addr(hw, mc_ptr->dmi_addr);
1493                 e1000_mta_set(hw, hash_value);
1494         }
1495
1496         if(hw->mac_type == e1000_82542_rev2_0)
1497                 e1000_leave_82542_rst(adapter);
1498
1499         spin_unlock_irqrestore(&adapter->tx_lock, flags);
1500 }
1501
1502 /* Need to wait a few seconds after link up to get diagnostic information from
1503  * the phy */
1504
1505 static void
1506 e1000_update_phy_info(unsigned long data)
1507 {
1508         struct e1000_adapter *adapter = (struct e1000_adapter *) data;
1509         e1000_phy_get_info(&adapter->hw, &adapter->phy_info);
1510 }
1511
1512 /**
1513  * e1000_82547_tx_fifo_stall - Timer Call-back
1514  * @data: pointer to adapter cast into an unsigned long
1515  **/
1516
1517 static void
1518 e1000_82547_tx_fifo_stall(unsigned long data)
1519 {
1520         struct e1000_adapter *adapter = (struct e1000_adapter *) data;
1521         struct net_device *netdev = adapter->netdev;
1522         uint32_t tctl;
1523
1524         if(atomic_read(&adapter->tx_fifo_stall)) {
1525                 if((E1000_READ_REG(&adapter->hw, TDT) ==
1526                     E1000_READ_REG(&adapter->hw, TDH)) &&
1527                    (E1000_READ_REG(&adapter->hw, TDFT) ==
1528                     E1000_READ_REG(&adapter->hw, TDFH)) &&
1529                    (E1000_READ_REG(&adapter->hw, TDFTS) ==
1530                     E1000_READ_REG(&adapter->hw, TDFHS))) {
1531                         tctl = E1000_READ_REG(&adapter->hw, TCTL);
1532                         E1000_WRITE_REG(&adapter->hw, TCTL,
1533                                         tctl & ~E1000_TCTL_EN);
1534                         E1000_WRITE_REG(&adapter->hw, TDFT,
1535                                         adapter->tx_head_addr);
1536                         E1000_WRITE_REG(&adapter->hw, TDFH,
1537                                         adapter->tx_head_addr);
1538                         E1000_WRITE_REG(&adapter->hw, TDFTS,
1539                                         adapter->tx_head_addr);
1540                         E1000_WRITE_REG(&adapter->hw, TDFHS,
1541                                         adapter->tx_head_addr);
1542                         E1000_WRITE_REG(&adapter->hw, TCTL, tctl);
1543                         E1000_WRITE_FLUSH(&adapter->hw);
1544
1545                         adapter->tx_fifo_head = 0;
1546                         atomic_set(&adapter->tx_fifo_stall, 0);
1547                         netif_wake_queue(netdev);
1548                 } else {
1549                         mod_timer(&adapter->tx_fifo_stall_timer, jiffies + 1);
1550                 }
1551         }
1552 }
1553
1554 /**
1555  * e1000_watchdog - Timer Call-back
1556  * @data: pointer to adapter cast into an unsigned long
1557  **/
1558 static void
1559 e1000_watchdog(unsigned long data)
1560 {
1561         struct e1000_adapter *adapter = (struct e1000_adapter *) data;
1562
1563         /* Do the rest outside of interrupt context */
1564         schedule_work(&adapter->watchdog_task);
1565 }
1566
1567 static void
1568 e1000_watchdog_task(struct e1000_adapter *adapter)
1569 {
1570         struct net_device *netdev = adapter->netdev;
1571         struct e1000_desc_ring *txdr = &adapter->tx_ring;
1572         uint32_t link;
1573
1574         e1000_check_for_link(&adapter->hw);
1575
1576         if((adapter->hw.media_type == e1000_media_type_internal_serdes) &&
1577            !(E1000_READ_REG(&adapter->hw, TXCW) & E1000_TXCW_ANE))
1578                 link = !adapter->hw.serdes_link_down;
1579         else
1580                 link = E1000_READ_REG(&adapter->hw, STATUS) & E1000_STATUS_LU;
1581
1582         if(link) {
1583                 if(!netif_carrier_ok(netdev)) {
1584                         e1000_get_speed_and_duplex(&adapter->hw,
1585                                                    &adapter->link_speed,
1586                                                    &adapter->link_duplex);
1587
1588                         DPRINTK(LINK, INFO, "NIC Link is Up %d Mbps %s\n",
1589                                adapter->link_speed,
1590                                adapter->link_duplex == FULL_DUPLEX ?
1591                                "Full Duplex" : "Half Duplex");
1592
1593                         netif_carrier_on(netdev);
1594                         netif_wake_queue(netdev);
1595                         mod_timer(&adapter->phy_info_timer, jiffies + 2 * HZ);
1596                         adapter->smartspeed = 0;
1597                 }
1598         } else {
1599                 if(netif_carrier_ok(netdev)) {
1600                         adapter->link_speed = 0;
1601                         adapter->link_duplex = 0;
1602                         DPRINTK(LINK, INFO, "NIC Link is Down\n");
1603                         netif_carrier_off(netdev);
1604                         netif_stop_queue(netdev);
1605                         mod_timer(&adapter->phy_info_timer, jiffies + 2 * HZ);
1606                 }
1607
1608                 e1000_smartspeed(adapter);
1609         }
1610
1611         e1000_update_stats(adapter);
1612
1613         adapter->hw.tx_packet_delta = adapter->stats.tpt - adapter->tpt_old;
1614         adapter->tpt_old = adapter->stats.tpt;
1615         adapter->hw.collision_delta = adapter->stats.colc - adapter->colc_old;
1616         adapter->colc_old = adapter->stats.colc;
1617
1618         adapter->gorcl = adapter->stats.gorcl - adapter->gorcl_old;
1619         adapter->gorcl_old = adapter->stats.gorcl;
1620         adapter->gotcl = adapter->stats.gotcl - adapter->gotcl_old;
1621         adapter->gotcl_old = adapter->stats.gotcl;
1622
1623         e1000_update_adaptive(&adapter->hw);
1624
1625         if(!netif_carrier_ok(netdev)) {
1626                 if(E1000_DESC_UNUSED(txdr) + 1 < txdr->count) {
1627                         /* We've lost link, so the controller stops DMA,
1628                          * but we've got queued Tx work that's never going
1629                          * to get done, so reset controller to flush Tx.
1630                          * (Do the reset outside of interrupt context). */
1631                         schedule_work(&adapter->tx_timeout_task);
1632                 }
1633         }
1634
1635         /* Dynamic mode for Interrupt Throttle Rate (ITR) */
1636         if(adapter->hw.mac_type >= e1000_82540 && adapter->itr == 1) {
1637                 /* Symmetric Tx/Rx gets a reduced ITR=2000; Total
1638                  * asymmetrical Tx or Rx gets ITR=8000; everyone
1639                  * else is between 2000-8000. */
1640                 uint32_t goc = (adapter->gotcl + adapter->gorcl) / 10000;
1641                 uint32_t dif = (adapter->gotcl > adapter->gorcl ? 
1642                         adapter->gotcl - adapter->gorcl :
1643                         adapter->gorcl - adapter->gotcl) / 10000;
1644                 uint32_t itr = goc > 0 ? (dif * 6000 / goc + 2000) : 8000;
1645                 E1000_WRITE_REG(&adapter->hw, ITR, 1000000000 / (itr * 256));
1646         }
1647
1648         /* Cause software interrupt to ensure rx ring is cleaned */
1649         E1000_WRITE_REG(&adapter->hw, ICS, E1000_ICS_RXDMT0);
1650
1651         /* Force detection of hung controller every watchdog period*/
1652         adapter->detect_tx_hung = TRUE;
1653
1654         /* Reset the timer */
1655         mod_timer(&adapter->watchdog_timer, jiffies + 2 * HZ);
1656 }
1657
1658 #define E1000_TX_FLAGS_CSUM             0x00000001
1659 #define E1000_TX_FLAGS_VLAN             0x00000002
1660 #define E1000_TX_FLAGS_TSO              0x00000004
1661 #define E1000_TX_FLAGS_VLAN_MASK        0xffff0000
1662 #define E1000_TX_FLAGS_VLAN_SHIFT       16
1663
1664 static inline int
1665 e1000_tso(struct e1000_adapter *adapter, struct sk_buff *skb)
1666 {
1667 #ifdef NETIF_F_TSO
1668         struct e1000_context_desc *context_desc;
1669         unsigned int i;
1670         uint32_t cmd_length = 0;
1671         uint16_t ipcse, tucse, mss;
1672         uint8_t ipcss, ipcso, tucss, tucso, hdr_len;
1673         int err;
1674
1675         if(skb_shinfo(skb)->tso_size) {
1676                 if (skb_header_cloned(skb)) {
1677                         err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
1678                         if (err)
1679                                 return err;
1680                 }
1681
1682                 hdr_len = ((skb->h.raw - skb->data) + (skb->h.th->doff << 2));
1683                 mss = skb_shinfo(skb)->tso_size;
1684                 skb->nh.iph->tot_len = 0;
1685                 skb->nh.iph->check = 0;
1686                 skb->h.th->check = ~csum_tcpudp_magic(skb->nh.iph->saddr,
1687                                                       skb->nh.iph->daddr,
1688                                                       0,
1689                                                       IPPROTO_TCP,
1690                                                       0);
1691                 ipcss = skb->nh.raw - skb->data;
1692                 ipcso = (void *)&(skb->nh.iph->check) - (void *)skb->data;
1693                 ipcse = skb->h.raw - skb->data - 1;
1694                 tucss = skb->h.raw - skb->data;
1695                 tucso = (void *)&(skb->h.th->check) - (void *)skb->data;
1696                 tucse = 0;
1697
1698                 cmd_length |= (E1000_TXD_CMD_DEXT | E1000_TXD_CMD_TSE |
1699                                E1000_TXD_CMD_IP | E1000_TXD_CMD_TCP |
1700                                (skb->len - (hdr_len)));
1701
1702                 i = adapter->tx_ring.next_to_use;
1703                 context_desc = E1000_CONTEXT_DESC(adapter->tx_ring, i);
1704
1705                 context_desc->lower_setup.ip_fields.ipcss  = ipcss;
1706                 context_desc->lower_setup.ip_fields.ipcso  = ipcso;
1707                 context_desc->lower_setup.ip_fields.ipcse  = cpu_to_le16(ipcse);
1708                 context_desc->upper_setup.tcp_fields.tucss = tucss;
1709                 context_desc->upper_setup.tcp_fields.tucso = tucso;
1710                 context_desc->upper_setup.tcp_fields.tucse = cpu_to_le16(tucse);
1711                 context_desc->tcp_seg_setup.fields.mss     = cpu_to_le16(mss);
1712                 context_desc->tcp_seg_setup.fields.hdr_len = hdr_len;
1713                 context_desc->cmd_and_length = cpu_to_le32(cmd_length);
1714
1715                 if(++i == adapter->tx_ring.count) i = 0;
1716                 adapter->tx_ring.next_to_use = i;
1717
1718                 return 1;
1719         }
1720 #endif
1721
1722         return 0;
1723 }
1724
1725 static inline boolean_t
1726 e1000_tx_csum(struct e1000_adapter *adapter, struct sk_buff *skb)
1727 {
1728         struct e1000_context_desc *context_desc;
1729         unsigned int i;
1730         uint8_t css;
1731
1732         if(likely(skb->ip_summed == CHECKSUM_HW)) {
1733                 css = skb->h.raw - skb->data;
1734
1735                 i = adapter->tx_ring.next_to_use;
1736                 context_desc = E1000_CONTEXT_DESC(adapter->tx_ring, i);
1737
1738                 context_desc->upper_setup.tcp_fields.tucss = css;
1739                 context_desc->upper_setup.tcp_fields.tucso = css + skb->csum;
1740                 context_desc->upper_setup.tcp_fields.tucse = 0;
1741                 context_desc->tcp_seg_setup.data = 0;
1742                 context_desc->cmd_and_length = cpu_to_le32(E1000_TXD_CMD_DEXT);
1743
1744                 if(unlikely(++i == adapter->tx_ring.count)) i = 0;
1745                 adapter->tx_ring.next_to_use = i;
1746
1747                 return TRUE;
1748         }
1749
1750         return FALSE;
1751 }
1752
1753 #define E1000_MAX_TXD_PWR       12
1754 #define E1000_MAX_DATA_PER_TXD  (1<<E1000_MAX_TXD_PWR)
1755
1756 static inline int
1757 e1000_tx_map(struct e1000_adapter *adapter, struct sk_buff *skb,
1758         unsigned int first, unsigned int max_per_txd,
1759         unsigned int nr_frags, unsigned int mss)
1760 {
1761         struct e1000_desc_ring *tx_ring = &adapter->tx_ring;
1762         struct e1000_buffer *buffer_info;
1763         unsigned int len = skb->len;
1764         unsigned int offset = 0, size, count = 0, i;
1765         unsigned int f;
1766         len -= skb->data_len;
1767
1768         i = tx_ring->next_to_use;
1769
1770         while(len) {
1771                 buffer_info = &tx_ring->buffer_info[i];
1772                 size = min(len, max_per_txd);
1773 #ifdef NETIF_F_TSO
1774                 /* Workaround for premature desc write-backs
1775                  * in TSO mode.  Append 4-byte sentinel desc */
1776                 if(unlikely(mss && !nr_frags && size == len && size > 8))
1777                         size -= 4;
1778 #endif
1779                 /* Workaround for potential 82544 hang in PCI-X.  Avoid
1780                  * terminating buffers within evenly-aligned dwords. */
1781                 if(unlikely(adapter->pcix_82544 &&
1782                    !((unsigned long)(skb->data + offset + size - 1) & 4) &&
1783                    size > 4))
1784                         size -= 4;
1785
1786                 buffer_info->length = size;
1787                 buffer_info->dma =
1788                         pci_map_single(adapter->pdev,
1789                                 skb->data + offset,
1790                                 size,
1791                                 PCI_DMA_TODEVICE);
1792                 buffer_info->time_stamp = jiffies;
1793
1794                 len -= size;
1795                 offset += size;
1796                 count++;
1797                 if(unlikely(++i == tx_ring->count)) i = 0;
1798         }
1799
1800         for(f = 0; f < nr_frags; f++) {
1801                 struct skb_frag_struct *frag;
1802
1803                 frag = &skb_shinfo(skb)->frags[f];
1804                 len = frag->size;
1805                 offset = frag->page_offset;
1806
1807                 while(len) {
1808                         buffer_info = &tx_ring->buffer_info[i];
1809                         size = min(len, max_per_txd);
1810 #ifdef NETIF_F_TSO
1811                         /* Workaround for premature desc write-backs
1812                          * in TSO mode.  Append 4-byte sentinel desc */
1813                         if(unlikely(mss && f == (nr_frags-1) && size == len && size > 8))
1814                                 size -= 4;
1815 #endif
1816                         /* Workaround for potential 82544 hang in PCI-X.
1817                          * Avoid terminating buffers within evenly-aligned
1818                          * dwords. */
1819                         if(unlikely(adapter->pcix_82544 &&
1820                            !((unsigned long)(frag->page+offset+size-1) & 4) &&
1821                            size > 4))
1822                                 size -= 4;
1823
1824                         buffer_info->length = size;
1825                         buffer_info->dma =
1826                                 pci_map_page(adapter->pdev,
1827                                         frag->page,
1828                                         offset,
1829                                         size,
1830                                         PCI_DMA_TODEVICE);
1831                         buffer_info->time_stamp = jiffies;
1832
1833                         len -= size;
1834                         offset += size;
1835                         count++;
1836                         if(unlikely(++i == tx_ring->count)) i = 0;
1837                 }
1838         }
1839
1840         i = (i == 0) ? tx_ring->count - 1 : i - 1;
1841         tx_ring->buffer_info[i].skb = skb;
1842         tx_ring->buffer_info[first].next_to_watch = i;
1843
1844         return count;
1845 }
1846
1847 static inline void
1848 e1000_tx_queue(struct e1000_adapter *adapter, int count, int tx_flags)
1849 {
1850         struct e1000_desc_ring *tx_ring = &adapter->tx_ring;
1851         struct e1000_tx_desc *tx_desc = NULL;
1852         struct e1000_buffer *buffer_info;
1853         uint32_t txd_upper = 0, txd_lower = E1000_TXD_CMD_IFCS;
1854         unsigned int i;
1855
1856         if(likely(tx_flags & E1000_TX_FLAGS_TSO)) {
1857                 txd_lower |= E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D |
1858                              E1000_TXD_CMD_TSE;
1859                 txd_upper |= (E1000_TXD_POPTS_IXSM | E1000_TXD_POPTS_TXSM) << 8;
1860         }
1861
1862         if(likely(tx_flags & E1000_TX_FLAGS_CSUM)) {
1863                 txd_lower |= E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D;
1864                 txd_upper |= E1000_TXD_POPTS_TXSM << 8;
1865         }
1866
1867         if(unlikely(tx_flags & E1000_TX_FLAGS_VLAN)) {
1868                 txd_lower |= E1000_TXD_CMD_VLE;
1869                 txd_upper |= (tx_flags & E1000_TX_FLAGS_VLAN_MASK);
1870         }
1871
1872         i = tx_ring->next_to_use;
1873
1874         while(count--) {
1875                 buffer_info = &tx_ring->buffer_info[i];
1876                 tx_desc = E1000_TX_DESC(*tx_ring, i);
1877                 tx_desc->buffer_addr = cpu_to_le64(buffer_info->dma);
1878                 tx_desc->lower.data =
1879                         cpu_to_le32(txd_lower | buffer_info->length);
1880                 tx_desc->upper.data = cpu_to_le32(txd_upper);
1881                 if(unlikely(++i == tx_ring->count)) i = 0;
1882         }
1883
1884         tx_desc->lower.data |= cpu_to_le32(adapter->txd_cmd);
1885
1886         /* Force memory writes to complete before letting h/w
1887          * know there are new descriptors to fetch.  (Only
1888          * applicable for weak-ordered memory model archs,
1889          * such as IA-64). */
1890         wmb();
1891
1892         tx_ring->next_to_use = i;
1893         E1000_WRITE_REG(&adapter->hw, TDT, i);
1894 }
1895
1896 /**
1897  * 82547 workaround to avoid controller hang in half-duplex environment.
1898  * The workaround is to avoid queuing a large packet that would span
1899  * the internal Tx FIFO ring boundary by notifying the stack to resend
1900  * the packet at a later time.  This gives the Tx FIFO an opportunity to
1901  * flush all packets.  When that occurs, we reset the Tx FIFO pointers
1902  * to the beginning of the Tx FIFO.
1903  **/
1904
1905 #define E1000_FIFO_HDR                  0x10
1906 #define E1000_82547_PAD_LEN             0x3E0
1907
1908 static inline int
1909 e1000_82547_fifo_workaround(struct e1000_adapter *adapter, struct sk_buff *skb)
1910 {
1911         uint32_t fifo_space = adapter->tx_fifo_size - adapter->tx_fifo_head;
1912         uint32_t skb_fifo_len = skb->len + E1000_FIFO_HDR;
1913
1914         E1000_ROUNDUP(skb_fifo_len, E1000_FIFO_HDR);
1915
1916         if(adapter->link_duplex != HALF_DUPLEX)
1917                 goto no_fifo_stall_required;
1918
1919         if(atomic_read(&adapter->tx_fifo_stall))
1920                 return 1;
1921
1922         if(skb_fifo_len >= (E1000_82547_PAD_LEN + fifo_space)) {
1923                 atomic_set(&adapter->tx_fifo_stall, 1);
1924                 return 1;
1925         }
1926
1927 no_fifo_stall_required:
1928         adapter->tx_fifo_head += skb_fifo_len;
1929         if(adapter->tx_fifo_head >= adapter->tx_fifo_size)
1930                 adapter->tx_fifo_head -= adapter->tx_fifo_size;
1931         return 0;
1932 }
1933
1934 #define TXD_USE_COUNT(S, X) (((S) >> (X)) + 1 )
1935 static int
1936 e1000_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
1937 {
1938         struct e1000_adapter *adapter = netdev->priv;
1939         unsigned int first, max_per_txd = E1000_MAX_DATA_PER_TXD;
1940         unsigned int max_txd_pwr = E1000_MAX_TXD_PWR;
1941         unsigned int tx_flags = 0;
1942         unsigned int len = skb->len;
1943         unsigned long flags;
1944         unsigned int nr_frags = 0;
1945         unsigned int mss = 0;
1946         int count = 0;
1947         int tso;
1948         unsigned int f;
1949         len -= skb->data_len;
1950
1951         if(unlikely(skb->len <= 0)) {
1952                 dev_kfree_skb_any(skb);
1953                 return NETDEV_TX_OK;
1954         }
1955
1956 #ifdef NETIF_F_TSO
1957         mss = skb_shinfo(skb)->tso_size;
1958         /* The controller does a simple calculation to
1959          * make sure there is enough room in the FIFO before
1960          * initiating the DMA for each buffer.  The calc is:
1961          * 4 = ceil(buffer len/mss).  To make sure we don't
1962          * overrun the FIFO, adjust the max buffer len if mss
1963          * drops. */
1964         if(mss) {
1965                 max_per_txd = min(mss << 2, max_per_txd);
1966                 max_txd_pwr = fls(max_per_txd) - 1;
1967         }
1968
1969         if((mss) || (skb->ip_summed == CHECKSUM_HW))
1970                 count++;
1971         count++;        /* for sentinel desc */
1972 #else
1973         if(skb->ip_summed == CHECKSUM_HW)
1974                 count++;
1975 #endif
1976         count += TXD_USE_COUNT(len, max_txd_pwr);
1977
1978         if(adapter->pcix_82544)
1979                 count++;
1980
1981         nr_frags = skb_shinfo(skb)->nr_frags;
1982         for(f = 0; f < nr_frags; f++)
1983                 count += TXD_USE_COUNT(skb_shinfo(skb)->frags[f].size,
1984                                        max_txd_pwr);
1985         if(adapter->pcix_82544)
1986                 count += nr_frags;
1987
1988         local_irq_save(flags); 
1989         if (!spin_trylock(&adapter->tx_lock)) { 
1990                 /* Collision - tell upper layer to requeue */ 
1991                 local_irq_restore(flags); 
1992                 return NETDEV_TX_LOCKED; 
1993         } 
1994
1995         /* need: count + 2 desc gap to keep tail from touching
1996          * head, otherwise try next time */
1997         if(unlikely(E1000_DESC_UNUSED(&adapter->tx_ring) < count + 2)) {
1998                 netif_stop_queue(netdev);
1999                 spin_unlock_irqrestore(&adapter->tx_lock, flags);
2000                 return NETDEV_TX_BUSY;
2001         }
2002
2003         if(unlikely(adapter->hw.mac_type == e1000_82547)) {
2004                 if(unlikely(e1000_82547_fifo_workaround(adapter, skb))) {
2005                         netif_stop_queue(netdev);
2006                         mod_timer(&adapter->tx_fifo_stall_timer, jiffies);
2007                         spin_unlock_irqrestore(&adapter->tx_lock, flags);
2008                         return NETDEV_TX_BUSY;
2009                 }
2010         }
2011
2012         if(unlikely(adapter->vlgrp && vlan_tx_tag_present(skb))) {
2013                 tx_flags |= E1000_TX_FLAGS_VLAN;
2014                 tx_flags |= (vlan_tx_tag_get(skb) << E1000_TX_FLAGS_VLAN_SHIFT);
2015         }
2016
2017         first = adapter->tx_ring.next_to_use;
2018         
2019         tso = e1000_tso(adapter, skb);
2020         if (tso < 0) {
2021                 dev_kfree_skb_any(skb);
2022                 return NETDEV_TX_OK;
2023         }
2024
2025         if (likely(tso))
2026                 tx_flags |= E1000_TX_FLAGS_TSO;
2027         else if(likely(e1000_tx_csum(adapter, skb)))
2028                 tx_flags |= E1000_TX_FLAGS_CSUM;
2029
2030         e1000_tx_queue(adapter,
2031                 e1000_tx_map(adapter, skb, first, max_per_txd, nr_frags, mss),
2032                 tx_flags);
2033
2034         netdev->trans_start = jiffies;
2035
2036         /* Make sure there is space in the ring for the next send. */
2037         if(unlikely(E1000_DESC_UNUSED(&adapter->tx_ring) < MAX_SKB_FRAGS + 2))
2038                 netif_stop_queue(netdev);
2039
2040         spin_unlock_irqrestore(&adapter->tx_lock, flags);
2041         return NETDEV_TX_OK;
2042 }
2043
2044 /**
2045  * e1000_tx_timeout - Respond to a Tx Hang
2046  * @netdev: network interface device structure
2047  **/
2048
2049 static void
2050 e1000_tx_timeout(struct net_device *netdev)
2051 {
2052         struct e1000_adapter *adapter = netdev->priv;
2053
2054         /* Do the reset outside of interrupt context */
2055         schedule_work(&adapter->tx_timeout_task);
2056 }
2057
2058 static void
2059 e1000_tx_timeout_task(struct net_device *netdev)
2060 {
2061         struct e1000_adapter *adapter = netdev->priv;
2062
2063         e1000_down(adapter);
2064         e1000_up(adapter);
2065 }
2066
2067 /**
2068  * e1000_get_stats - Get System Network Statistics
2069  * @netdev: network interface device structure
2070  *
2071  * Returns the address of the device statistics structure.
2072  * The statistics are actually updated from the timer callback.
2073  **/
2074
2075 static struct net_device_stats *
2076 e1000_get_stats(struct net_device *netdev)
2077 {
2078         struct e1000_adapter *adapter = netdev->priv;
2079
2080         e1000_update_stats(adapter);
2081         return &adapter->net_stats;
2082 }
2083
2084 /**
2085  * e1000_change_mtu - Change the Maximum Transfer Unit
2086  * @netdev: network interface device structure
2087  * @new_mtu: new value for maximum frame size
2088  *
2089  * Returns 0 on success, negative on failure
2090  **/
2091
2092 static int
2093 e1000_change_mtu(struct net_device *netdev, int new_mtu)
2094 {
2095         struct e1000_adapter *adapter = netdev->priv;
2096         int old_mtu = adapter->rx_buffer_len;
2097         int max_frame = new_mtu + ENET_HEADER_SIZE + ETHERNET_FCS_SIZE;
2098
2099         if((max_frame < MINIMUM_ETHERNET_FRAME_SIZE) ||
2100                 (max_frame > MAX_JUMBO_FRAME_SIZE)) {
2101                         DPRINTK(PROBE, ERR, "Invalid MTU setting\n");
2102                         return -EINVAL;
2103         }
2104
2105         if(max_frame <= MAXIMUM_ETHERNET_FRAME_SIZE) {
2106                 adapter->rx_buffer_len = E1000_RXBUFFER_2048;
2107
2108         } else if(adapter->hw.mac_type < e1000_82543) {
2109                 DPRINTK(PROBE, ERR, "Jumbo Frames not supported on 82542\n");
2110                 return -EINVAL;
2111
2112         } else if(max_frame <= E1000_RXBUFFER_4096) {
2113                 adapter->rx_buffer_len = E1000_RXBUFFER_4096;
2114
2115         } else if(max_frame <= E1000_RXBUFFER_8192) {
2116                 adapter->rx_buffer_len = E1000_RXBUFFER_8192;
2117
2118         } else {
2119                 adapter->rx_buffer_len = E1000_RXBUFFER_16384;
2120         }
2121
2122         if(old_mtu != adapter->rx_buffer_len && netif_running(netdev)) {
2123                 e1000_down(adapter);
2124                 e1000_up(adapter);
2125         }
2126
2127         netdev->mtu = new_mtu;
2128         adapter->hw.max_frame_size = max_frame;
2129
2130         return 0;
2131 }
2132
2133 /**
2134  * e1000_update_stats - Update the board statistics counters
2135  * @adapter: board private structure
2136  **/
2137
2138 void
2139 e1000_update_stats(struct e1000_adapter *adapter)
2140 {
2141         struct e1000_hw *hw = &adapter->hw;
2142         unsigned long flags;
2143         uint16_t phy_tmp;
2144
2145 #define PHY_IDLE_ERROR_COUNT_MASK 0x00FF
2146
2147         spin_lock_irqsave(&adapter->stats_lock, flags);
2148
2149         /* these counters are modified from e1000_adjust_tbi_stats,
2150          * called from the interrupt context, so they must only
2151          * be written while holding adapter->stats_lock
2152          */
2153
2154         adapter->stats.crcerrs += E1000_READ_REG(hw, CRCERRS);
2155         adapter->stats.gprc += E1000_READ_REG(hw, GPRC);
2156         adapter->stats.gorcl += E1000_READ_REG(hw, GORCL);
2157         adapter->stats.gorch += E1000_READ_REG(hw, GORCH);
2158         adapter->stats.bprc += E1000_READ_REG(hw, BPRC);
2159         adapter->stats.mprc += E1000_READ_REG(hw, MPRC);
2160         adapter->stats.roc += E1000_READ_REG(hw, ROC);
2161         adapter->stats.prc64 += E1000_READ_REG(hw, PRC64);
2162         adapter->stats.prc127 += E1000_READ_REG(hw, PRC127);
2163         adapter->stats.prc255 += E1000_READ_REG(hw, PRC255);
2164         adapter->stats.prc511 += E1000_READ_REG(hw, PRC511);
2165         adapter->stats.prc1023 += E1000_READ_REG(hw, PRC1023);
2166         adapter->stats.prc1522 += E1000_READ_REG(hw, PRC1522);
2167
2168         adapter->stats.symerrs += E1000_READ_REG(hw, SYMERRS);
2169         adapter->stats.mpc += E1000_READ_REG(hw, MPC);
2170         adapter->stats.scc += E1000_READ_REG(hw, SCC);
2171         adapter->stats.ecol += E1000_READ_REG(hw, ECOL);
2172         adapter->stats.mcc += E1000_READ_REG(hw, MCC);
2173         adapter->stats.latecol += E1000_READ_REG(hw, LATECOL);
2174         adapter->stats.dc += E1000_READ_REG(hw, DC);
2175         adapter->stats.sec += E1000_READ_REG(hw, SEC);
2176         adapter->stats.rlec += E1000_READ_REG(hw, RLEC);
2177         adapter->stats.xonrxc += E1000_READ_REG(hw, XONRXC);
2178         adapter->stats.xontxc += E1000_READ_REG(hw, XONTXC);
2179         adapter->stats.xoffrxc += E1000_READ_REG(hw, XOFFRXC);
2180         adapter->stats.xofftxc += E1000_READ_REG(hw, XOFFTXC);
2181         adapter->stats.fcruc += E1000_READ_REG(hw, FCRUC);
2182         adapter->stats.gptc += E1000_READ_REG(hw, GPTC);
2183         adapter->stats.gotcl += E1000_READ_REG(hw, GOTCL);
2184         adapter->stats.gotch += E1000_READ_REG(hw, GOTCH);
2185         adapter->stats.rnbc += E1000_READ_REG(hw, RNBC);
2186         adapter->stats.ruc += E1000_READ_REG(hw, RUC);
2187         adapter->stats.rfc += E1000_READ_REG(hw, RFC);
2188         adapter->stats.rjc += E1000_READ_REG(hw, RJC);
2189         adapter->stats.torl += E1000_READ_REG(hw, TORL);
2190         adapter->stats.torh += E1000_READ_REG(hw, TORH);
2191         adapter->stats.totl += E1000_READ_REG(hw, TOTL);
2192         adapter->stats.toth += E1000_READ_REG(hw, TOTH);
2193         adapter->stats.tpr += E1000_READ_REG(hw, TPR);
2194         adapter->stats.ptc64 += E1000_READ_REG(hw, PTC64);
2195         adapter->stats.ptc127 += E1000_READ_REG(hw, PTC127);
2196         adapter->stats.ptc255 += E1000_READ_REG(hw, PTC255);
2197         adapter->stats.ptc511 += E1000_READ_REG(hw, PTC511);
2198         adapter->stats.ptc1023 += E1000_READ_REG(hw, PTC1023);
2199         adapter->stats.ptc1522 += E1000_READ_REG(hw, PTC1522);
2200         adapter->stats.mptc += E1000_READ_REG(hw, MPTC);
2201         adapter->stats.bptc += E1000_READ_REG(hw, BPTC);
2202
2203         /* used for adaptive IFS */
2204
2205         hw->tx_packet_delta = E1000_READ_REG(hw, TPT);
2206         adapter->stats.tpt += hw->tx_packet_delta;
2207         hw->collision_delta = E1000_READ_REG(hw, COLC);
2208         adapter->stats.colc += hw->collision_delta;
2209
2210         if(hw->mac_type >= e1000_82543) {
2211                 adapter->stats.algnerrc += E1000_READ_REG(hw, ALGNERRC);
2212                 adapter->stats.rxerrc += E1000_READ_REG(hw, RXERRC);
2213                 adapter->stats.tncrs += E1000_READ_REG(hw, TNCRS);
2214                 adapter->stats.cexterr += E1000_READ_REG(hw, CEXTERR);
2215                 adapter->stats.tsctc += E1000_READ_REG(hw, TSCTC);
2216                 adapter->stats.tsctfc += E1000_READ_REG(hw, TSCTFC);
2217         }
2218
2219         /* Fill out the OS statistics structure */
2220
2221         adapter->net_stats.rx_packets = adapter->stats.gprc;
2222         adapter->net_stats.tx_packets = adapter->stats.gptc;
2223         adapter->net_stats.rx_bytes = adapter->stats.gorcl;
2224         adapter->net_stats.tx_bytes = adapter->stats.gotcl;
2225         adapter->net_stats.multicast = adapter->stats.mprc;
2226         adapter->net_stats.collisions = adapter->stats.colc;
2227
2228         /* Rx Errors */
2229
2230         adapter->net_stats.rx_errors = adapter->stats.rxerrc +
2231                 adapter->stats.crcerrs + adapter->stats.algnerrc +
2232                 adapter->stats.rlec + adapter->stats.rnbc +
2233                 adapter->stats.mpc + adapter->stats.cexterr;
2234         adapter->net_stats.rx_dropped = adapter->stats.rnbc;
2235         adapter->net_stats.rx_length_errors = adapter->stats.rlec;
2236         adapter->net_stats.rx_crc_errors = adapter->stats.crcerrs;
2237         adapter->net_stats.rx_frame_errors = adapter->stats.algnerrc;
2238         adapter->net_stats.rx_fifo_errors = adapter->stats.mpc;
2239         adapter->net_stats.rx_missed_errors = adapter->stats.mpc;
2240
2241         /* Tx Errors */
2242
2243         adapter->net_stats.tx_errors = adapter->stats.ecol +
2244                                        adapter->stats.latecol;
2245         adapter->net_stats.tx_aborted_errors = adapter->stats.ecol;
2246         adapter->net_stats.tx_window_errors = adapter->stats.latecol;
2247         adapter->net_stats.tx_carrier_errors = adapter->stats.tncrs;
2248
2249         /* Tx Dropped needs to be maintained elsewhere */
2250
2251         /* Phy Stats */
2252
2253         if(hw->media_type == e1000_media_type_copper) {
2254                 if((adapter->link_speed == SPEED_1000) &&
2255                    (!e1000_read_phy_reg(hw, PHY_1000T_STATUS, &phy_tmp))) {
2256                         phy_tmp &= PHY_IDLE_ERROR_COUNT_MASK;
2257                         adapter->phy_stats.idle_errors += phy_tmp;
2258                 }
2259
2260                 if((hw->mac_type <= e1000_82546) &&
2261                    (hw->phy_type == e1000_phy_m88) &&
2262                    !e1000_read_phy_reg(hw, M88E1000_RX_ERR_CNTR, &phy_tmp))
2263                         adapter->phy_stats.receive_errors += phy_tmp;
2264         }
2265
2266         spin_unlock_irqrestore(&adapter->stats_lock, flags);
2267 }
2268
2269 /**
2270  * e1000_intr - Interrupt Handler
2271  * @irq: interrupt number
2272  * @data: pointer to a network interface device structure
2273  * @pt_regs: CPU registers structure
2274  **/
2275
2276 static irqreturn_t
2277 e1000_intr(int irq, void *data, struct pt_regs *regs)
2278 {
2279         struct net_device *netdev = data;
2280         struct e1000_adapter *adapter = netdev->priv;
2281         struct e1000_hw *hw = &adapter->hw;
2282         uint32_t icr = E1000_READ_REG(hw, ICR);
2283 #ifndef CONFIG_E1000_NAPI
2284         unsigned int i;
2285 #endif
2286
2287         if(unlikely(!icr))
2288                 return IRQ_NONE;  /* Not our interrupt */
2289
2290         if(unlikely(icr & (E1000_ICR_RXSEQ | E1000_ICR_LSC))) {
2291                 hw->get_link_status = 1;
2292                 mod_timer(&adapter->watchdog_timer, jiffies);
2293         }
2294
2295 #ifdef CONFIG_E1000_NAPI
2296         if(likely(netif_rx_schedule_prep(netdev))) {
2297
2298                 /* Disable interrupts and register for poll. The flush 
2299                   of the posted write is intentionally left out.
2300                 */
2301
2302                 atomic_inc(&adapter->irq_sem);
2303                 E1000_WRITE_REG(hw, IMC, ~0);
2304                 __netif_rx_schedule(netdev);
2305         }
2306 #else
2307         /* Writing IMC and IMS is needed for 82547.
2308            Due to Hub Link bus being occupied, an interrupt
2309            de-assertion message is not able to be sent.
2310            When an interrupt assertion message is generated later,
2311            two messages are re-ordered and sent out.
2312            That causes APIC to think 82547 is in de-assertion
2313            state, while 82547 is in assertion state, resulting
2314            in dead lock. Writing IMC forces 82547 into
2315            de-assertion state.
2316         */
2317         if(hw->mac_type == e1000_82547 || hw->mac_type == e1000_82547_rev_2){
2318                 atomic_inc(&adapter->irq_sem);
2319                 E1000_WRITE_REG(&adapter->hw, IMC, ~0);
2320         }
2321
2322         for(i = 0; i < E1000_MAX_INTR; i++)
2323                 if(unlikely(!e1000_clean_rx_irq(adapter) &
2324                    !e1000_clean_tx_irq(adapter)))
2325                         break;
2326
2327         if(hw->mac_type == e1000_82547 || hw->mac_type == e1000_82547_rev_2)
2328                 e1000_irq_enable(adapter);
2329 #endif
2330
2331         return IRQ_HANDLED;
2332 }
2333
2334 #ifdef CONFIG_E1000_NAPI
2335 /**
2336  * e1000_clean - NAPI Rx polling callback
2337  * @adapter: board private structure
2338  **/
2339
2340 static int
2341 e1000_clean(struct net_device *netdev, int *budget)
2342 {
2343         struct e1000_adapter *adapter = netdev->priv;
2344         int work_to_do = min(*budget, netdev->quota);
2345         int tx_cleaned;
2346         int work_done = 0;
2347         
2348         tx_cleaned = e1000_clean_tx_irq(adapter);
2349         e1000_clean_rx_irq(adapter, &work_done, work_to_do);
2350
2351         *budget -= work_done;
2352         netdev->quota -= work_done;
2353         
2354         /* if no Tx and not enough Rx work done, exit the polling mode */
2355         if((!tx_cleaned && (work_done < work_to_do)) || 
2356                                 !netif_running(netdev)) {
2357                 netif_rx_complete(netdev);
2358                 e1000_irq_enable(adapter);
2359                 return 0;
2360         }
2361
2362         return 1;
2363 }
2364
2365 #endif
2366 /**
2367  * e1000_clean_tx_irq - Reclaim resources after transmit completes
2368  * @adapter: board private structure
2369  **/
2370
2371 static boolean_t
2372 e1000_clean_tx_irq(struct e1000_adapter *adapter)
2373 {
2374         struct e1000_desc_ring *tx_ring = &adapter->tx_ring;
2375         struct net_device *netdev = adapter->netdev;
2376         struct e1000_tx_desc *tx_desc, *eop_desc;
2377         struct e1000_buffer *buffer_info;
2378         unsigned int i, eop;
2379         boolean_t cleaned = FALSE;
2380
2381         i = tx_ring->next_to_clean;
2382         eop = tx_ring->buffer_info[i].next_to_watch;
2383         eop_desc = E1000_TX_DESC(*tx_ring, eop);
2384
2385         while(eop_desc->upper.data & cpu_to_le32(E1000_TXD_STAT_DD)) {
2386                 /* pre-mature writeback of Tx descriptors     */
2387                 /* clear (free buffers and unmap pci_mapping) */
2388                 /* previous_buffer_info                       */
2389                 if (likely(adapter->previous_buffer_info.skb != NULL)) {
2390                         e1000_unmap_and_free_tx_resource(adapter, 
2391                                         &adapter->previous_buffer_info);
2392                 }
2393
2394                 for(cleaned = FALSE; !cleaned; ) {
2395                         tx_desc = E1000_TX_DESC(*tx_ring, i);
2396                         buffer_info = &tx_ring->buffer_info[i];
2397                         cleaned = (i == eop);
2398
2399                         /* pre-mature writeback of Tx descriptors */
2400                         /* save the cleaning of the this for the  */
2401                         /* next iteration                         */
2402                         if (cleaned) {
2403                                 memcpy(&adapter->previous_buffer_info,
2404                                         buffer_info,
2405                                         sizeof(struct e1000_buffer));
2406                                 memset(buffer_info,
2407                                         0,
2408                                         sizeof(struct e1000_buffer));
2409                         } else {
2410                                 e1000_unmap_and_free_tx_resource(adapter, 
2411                                                         buffer_info);
2412                         }
2413
2414                         tx_desc->buffer_addr = 0;
2415                         tx_desc->lower.data = 0;
2416                         tx_desc->upper.data = 0;
2417
2418                         cleaned = (i == eop);
2419                         if(unlikely(++i == tx_ring->count)) i = 0;
2420                 }
2421                 
2422                 eop = tx_ring->buffer_info[i].next_to_watch;
2423                 eop_desc = E1000_TX_DESC(*tx_ring, eop);
2424         }
2425
2426         tx_ring->next_to_clean = i;
2427
2428         spin_lock(&adapter->tx_lock);
2429
2430         if(unlikely(cleaned && netif_queue_stopped(netdev) &&
2431                     netif_carrier_ok(netdev)))
2432                 netif_wake_queue(netdev);
2433
2434         spin_unlock(&adapter->tx_lock);
2435  
2436         if(adapter->detect_tx_hung) {
2437                 /* detect a transmit hang in hardware, this serializes the
2438                  * check with the clearing of time_stamp and movement of i */
2439                 adapter->detect_tx_hung = FALSE;
2440                 if(tx_ring->buffer_info[i].dma &&
2441                    time_after(jiffies, tx_ring->buffer_info[i].time_stamp + HZ) &&
2442                    !(E1000_READ_REG(&adapter->hw, STATUS) & E1000_STATUS_TXOFF))
2443                         netif_stop_queue(netdev);
2444         }
2445
2446         return cleaned;
2447 }
2448
2449 /**
2450  * e1000_rx_checksum - Receive Checksum Offload for 82543
2451  * @adapter: board private structure
2452  * @rx_desc: receive descriptor
2453  * @sk_buff: socket buffer with received data
2454  **/
2455
2456 static inline void
2457 e1000_rx_checksum(struct e1000_adapter *adapter,
2458                   struct e1000_rx_desc *rx_desc,
2459                   struct sk_buff *skb)
2460 {
2461         /* 82543 or newer only */
2462         if(unlikely((adapter->hw.mac_type < e1000_82543) ||
2463         /* Ignore Checksum bit is set */
2464         (rx_desc->status & E1000_RXD_STAT_IXSM) ||
2465         /* TCP Checksum has not been calculated */
2466         (!(rx_desc->status & E1000_RXD_STAT_TCPCS)))) {
2467                 skb->ip_summed = CHECKSUM_NONE;
2468                 return;
2469         }
2470
2471         /* At this point we know the hardware did the TCP checksum */
2472         /* now look at the TCP checksum error bit */
2473         if(rx_desc->errors & E1000_RXD_ERR_TCPE) {
2474                 /* let the stack verify checksum errors */
2475                 skb->ip_summed = CHECKSUM_NONE;
2476                 adapter->hw_csum_err++;
2477         } else {
2478                 /* TCP checksum is good */
2479                 skb->ip_summed = CHECKSUM_UNNECESSARY;
2480                 adapter->hw_csum_good++;
2481         }
2482 }
2483
2484 /**
2485  * e1000_clean_rx_irq - Send received data up the network stack
2486  * @adapter: board private structure
2487  **/
2488
2489 static boolean_t
2490 #ifdef CONFIG_E1000_NAPI
2491 e1000_clean_rx_irq(struct e1000_adapter *adapter, int *work_done,
2492                    int work_to_do)
2493 #else
2494 e1000_clean_rx_irq(struct e1000_adapter *adapter)
2495 #endif
2496 {
2497         struct e1000_desc_ring *rx_ring = &adapter->rx_ring;
2498         struct net_device *netdev = adapter->netdev;
2499         struct pci_dev *pdev = adapter->pdev;
2500         struct e1000_rx_desc *rx_desc;
2501         struct e1000_buffer *buffer_info;
2502         struct sk_buff *skb;
2503         unsigned long flags;
2504         uint32_t length;
2505         uint8_t last_byte;
2506         unsigned int i;
2507         boolean_t cleaned = FALSE;
2508
2509         i = rx_ring->next_to_clean;
2510         rx_desc = E1000_RX_DESC(*rx_ring, i);
2511
2512         while(rx_desc->status & E1000_RXD_STAT_DD) {
2513                 buffer_info = &rx_ring->buffer_info[i];
2514 #ifdef CONFIG_E1000_NAPI
2515                 if(*work_done >= work_to_do)
2516                         break;
2517                 (*work_done)++;
2518 #endif
2519                 cleaned = TRUE;
2520
2521                 pci_unmap_single(pdev,
2522                                  buffer_info->dma,
2523                                  buffer_info->length,
2524                                  PCI_DMA_FROMDEVICE);
2525
2526                 skb = buffer_info->skb;
2527                 length = le16_to_cpu(rx_desc->length);
2528
2529                 if(unlikely(!(rx_desc->status & E1000_RXD_STAT_EOP))) {
2530                         /* All receives must fit into a single buffer */
2531                         E1000_DBG("%s: Receive packet consumed multiple"
2532                                         " buffers\n", netdev->name);
2533                         dev_kfree_skb_irq(skb);
2534                         goto next_desc;
2535                 }
2536
2537                 if(unlikely(rx_desc->errors & E1000_RXD_ERR_FRAME_ERR_MASK)) {
2538                         last_byte = *(skb->data + length - 1);
2539                         if(TBI_ACCEPT(&adapter->hw, rx_desc->status,
2540                                       rx_desc->errors, length, last_byte)) {
2541                                 spin_lock_irqsave(&adapter->stats_lock, flags);
2542                                 e1000_tbi_adjust_stats(&adapter->hw,
2543                                                        &adapter->stats,
2544                                                        length, skb->data);
2545                                 spin_unlock_irqrestore(&adapter->stats_lock,
2546                                                        flags);
2547                                 length--;
2548                         } else {
2549                                 dev_kfree_skb_irq(skb);
2550                                 goto next_desc;
2551                         }
2552                 }
2553
2554                 /* Good Receive */
2555                 skb_put(skb, length - ETHERNET_FCS_SIZE);
2556
2557                 /* Receive Checksum Offload */
2558                 e1000_rx_checksum(adapter, rx_desc, skb);
2559
2560                 skb->protocol = eth_type_trans(skb, netdev);
2561 #ifdef CONFIG_E1000_NAPI
2562                 if(unlikely(adapter->vlgrp &&
2563                             (rx_desc->status & E1000_RXD_STAT_VP))) {
2564                         vlan_hwaccel_receive_skb(skb, adapter->vlgrp,
2565                                         le16_to_cpu(rx_desc->special) &
2566                                         E1000_RXD_SPC_VLAN_MASK);
2567                 } else {
2568                         netif_receive_skb(skb);
2569                 }
2570 #else /* CONFIG_E1000_NAPI */
2571                 if(unlikely(adapter->vlgrp &&
2572                             (rx_desc->status & E1000_RXD_STAT_VP))) {
2573                         vlan_hwaccel_rx(skb, adapter->vlgrp,
2574                                         le16_to_cpu(rx_desc->special) &
2575                                         E1000_RXD_SPC_VLAN_MASK);
2576                 } else {
2577                         netif_rx(skb);
2578                 }
2579 #endif /* CONFIG_E1000_NAPI */
2580                 netdev->last_rx = jiffies;
2581
2582 next_desc:
2583                 rx_desc->status = 0;
2584                 buffer_info->skb = NULL;
2585                 if(unlikely(++i == rx_ring->count)) i = 0;
2586
2587                 rx_desc = E1000_RX_DESC(*rx_ring, i);
2588         }
2589
2590         rx_ring->next_to_clean = i;
2591
2592         e1000_alloc_rx_buffers(adapter);
2593
2594         return cleaned;
2595 }
2596
2597 /**
2598  * e1000_alloc_rx_buffers - Replace used receive buffers
2599  * @adapter: address of board private structure
2600  **/
2601
2602 static void
2603 e1000_alloc_rx_buffers(struct e1000_adapter *adapter)
2604 {
2605         struct e1000_desc_ring *rx_ring = &adapter->rx_ring;
2606         struct net_device *netdev = adapter->netdev;
2607         struct pci_dev *pdev = adapter->pdev;
2608         struct e1000_rx_desc *rx_desc;
2609         struct e1000_buffer *buffer_info;
2610         struct sk_buff *skb;
2611         unsigned int i, bufsz;
2612
2613         i = rx_ring->next_to_use;
2614         buffer_info = &rx_ring->buffer_info[i];
2615
2616         while(!buffer_info->skb) {
2617                 bufsz = adapter->rx_buffer_len + NET_IP_ALIGN;
2618
2619                 skb = dev_alloc_skb(bufsz);
2620                 if(unlikely(!skb)) {
2621                         /* Better luck next round */
2622                         break;
2623                 }
2624
2625                 /* fix for errata 23, cant cross 64kB boundary */
2626                 if (!e1000_check_64k_bound(adapter, skb->data, bufsz)) {
2627                         struct sk_buff *oldskb = skb;
2628                         DPRINTK(RX_ERR,ERR,
2629                                 "skb align check failed: %u bytes at %p\n",
2630                                 bufsz, skb->data);
2631                         /* try again, without freeing the previous */
2632                         skb = dev_alloc_skb(bufsz);
2633                         if (!skb) {
2634                                 dev_kfree_skb(oldskb);
2635                                 break;
2636                         }
2637                         if (!e1000_check_64k_bound(adapter, skb->data, bufsz)) {
2638                                 /* give up */
2639                                 dev_kfree_skb(skb);
2640                                 dev_kfree_skb(oldskb);
2641                                 break; /* while !buffer_info->skb */
2642                         } else {
2643                                 /* move on with the new one */
2644                                 dev_kfree_skb(oldskb);
2645                         }
2646                 }
2647
2648                 /* Make buffer alignment 2 beyond a 16 byte boundary
2649                  * this will result in a 16 byte aligned IP header after
2650                  * the 14 byte MAC header is removed
2651                  */
2652                 skb_reserve(skb, NET_IP_ALIGN);
2653
2654                 skb->dev = netdev;
2655
2656                 buffer_info->skb = skb;
2657                 buffer_info->length = adapter->rx_buffer_len;
2658                 buffer_info->dma = pci_map_single(pdev,
2659                                                   skb->data,
2660                                                   adapter->rx_buffer_len,
2661                                                   PCI_DMA_FROMDEVICE);
2662
2663                 /* fix for errata 23, cant cross 64kB boundary */
2664                 if(!e1000_check_64k_bound(adapter,
2665                                                (void *)(unsigned long)buffer_info->dma,
2666                                                adapter->rx_buffer_len)) {
2667                         DPRINTK(RX_ERR,ERR,
2668                                 "dma align check failed: %u bytes at %ld\n",
2669                                 adapter->rx_buffer_len, (unsigned long)buffer_info->dma);
2670
2671                         dev_kfree_skb(skb);
2672                         buffer_info->skb = NULL;
2673
2674                         pci_unmap_single(pdev,
2675                                          buffer_info->dma,
2676                                          adapter->rx_buffer_len,
2677                                          PCI_DMA_FROMDEVICE);
2678
2679                         break; /* while !buffer_info->skb */
2680                 }
2681
2682                 rx_desc = E1000_RX_DESC(*rx_ring, i);
2683                 rx_desc->buffer_addr = cpu_to_le64(buffer_info->dma);
2684
2685                 if(unlikely((i & ~(E1000_RX_BUFFER_WRITE - 1)) == i)) {
2686                         /* Force memory writes to complete before letting h/w
2687                          * know there are new descriptors to fetch.  (Only
2688                          * applicable for weak-ordered memory model archs,
2689                          * such as IA-64). */
2690                         wmb();
2691
2692                         E1000_WRITE_REG(&adapter->hw, RDT, i);
2693                 }
2694
2695                 if(unlikely(++i == rx_ring->count)) i = 0;
2696                 buffer_info = &rx_ring->buffer_info[i];
2697         }
2698
2699         rx_ring->next_to_use = i;
2700 }
2701
2702 /**
2703  * e1000_smartspeed - Workaround for SmartSpeed on 82541 and 82547 controllers.
2704  * @adapter:
2705  **/
2706
2707 static void
2708 e1000_smartspeed(struct e1000_adapter *adapter)
2709 {
2710         uint16_t phy_status;
2711         uint16_t phy_ctrl;
2712
2713         if((adapter->hw.phy_type != e1000_phy_igp) || !adapter->hw.autoneg ||
2714            !(adapter->hw.autoneg_advertised & ADVERTISE_1000_FULL))
2715                 return;
2716
2717         if(adapter->smartspeed == 0) {
2718                 /* If Master/Slave config fault is asserted twice,
2719                  * we assume back-to-back */
2720                 e1000_read_phy_reg(&adapter->hw, PHY_1000T_STATUS, &phy_status);
2721                 if(!(phy_status & SR_1000T_MS_CONFIG_FAULT)) return;
2722                 e1000_read_phy_reg(&adapter->hw, PHY_1000T_STATUS, &phy_status);
2723                 if(!(phy_status & SR_1000T_MS_CONFIG_FAULT)) return;
2724                 e1000_read_phy_reg(&adapter->hw, PHY_1000T_CTRL, &phy_ctrl);
2725                 if(phy_ctrl & CR_1000T_MS_ENABLE) {
2726                         phy_ctrl &= ~CR_1000T_MS_ENABLE;
2727                         e1000_write_phy_reg(&adapter->hw, PHY_1000T_CTRL,
2728                                             phy_ctrl);
2729                         adapter->smartspeed++;
2730                         if(!e1000_phy_setup_autoneg(&adapter->hw) &&
2731                            !e1000_read_phy_reg(&adapter->hw, PHY_CTRL,
2732                                                &phy_ctrl)) {
2733                                 phy_ctrl |= (MII_CR_AUTO_NEG_EN |
2734                                              MII_CR_RESTART_AUTO_NEG);
2735                                 e1000_write_phy_reg(&adapter->hw, PHY_CTRL,
2736                                                     phy_ctrl);
2737                         }
2738                 }
2739                 return;
2740         } else if(adapter->smartspeed == E1000_SMARTSPEED_DOWNSHIFT) {
2741                 /* If still no link, perhaps using 2/3 pair cable */
2742                 e1000_read_phy_reg(&adapter->hw, PHY_1000T_CTRL, &phy_ctrl);
2743                 phy_ctrl |= CR_1000T_MS_ENABLE;
2744                 e1000_write_phy_reg(&adapter->hw, PHY_1000T_CTRL, phy_ctrl);
2745                 if(!e1000_phy_setup_autoneg(&adapter->hw) &&
2746                    !e1000_read_phy_reg(&adapter->hw, PHY_CTRL, &phy_ctrl)) {
2747                         phy_ctrl |= (MII_CR_AUTO_NEG_EN |
2748                                      MII_CR_RESTART_AUTO_NEG);
2749                         e1000_write_phy_reg(&adapter->hw, PHY_CTRL, phy_ctrl);
2750                 }
2751         }
2752         /* Restart process after E1000_SMARTSPEED_MAX iterations */
2753         if(adapter->smartspeed++ == E1000_SMARTSPEED_MAX)
2754                 adapter->smartspeed = 0;
2755 }
2756
2757 /**
2758  * e1000_ioctl -
2759  * @netdev:
2760  * @ifreq:
2761  * @cmd:
2762  **/
2763
2764 static int
2765 e1000_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2766 {
2767         switch (cmd) {
2768         case SIOCGMIIPHY:
2769         case SIOCGMIIREG:
2770         case SIOCSMIIREG:
2771                 return e1000_mii_ioctl(netdev, ifr, cmd);
2772         default:
2773                 return -EOPNOTSUPP;
2774         }
2775 }
2776
2777 /**
2778  * e1000_mii_ioctl -
2779  * @netdev:
2780  * @ifreq:
2781  * @cmd:
2782  **/
2783
2784 static int
2785 e1000_mii_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2786 {
2787         struct e1000_adapter *adapter = netdev->priv;
2788         struct mii_ioctl_data *data = if_mii(ifr);
2789         int retval;
2790         uint16_t mii_reg;
2791         uint16_t spddplx;
2792
2793         if(adapter->hw.media_type != e1000_media_type_copper)
2794                 return -EOPNOTSUPP;
2795
2796         switch (cmd) {
2797         case SIOCGMIIPHY:
2798                 data->phy_id = adapter->hw.phy_addr;
2799                 break;
2800         case SIOCGMIIREG:
2801                 if (!capable(CAP_NET_ADMIN))
2802                         return -EPERM;
2803                 if (e1000_read_phy_reg(&adapter->hw, data->reg_num & 0x1F,
2804                                    &data->val_out))
2805                         return -EIO;
2806                 break;
2807         case SIOCSMIIREG:
2808                 if (!capable(CAP_NET_ADMIN))
2809                         return -EPERM;
2810                 if (data->reg_num & ~(0x1F))
2811                         return -EFAULT;
2812                 mii_reg = data->val_in;
2813                 if (e1000_write_phy_reg(&adapter->hw, data->reg_num,
2814                                         mii_reg))
2815                         return -EIO;
2816                 if (adapter->hw.phy_type == e1000_phy_m88) {
2817                         switch (data->reg_num) {
2818                         case PHY_CTRL:
2819                                 if(mii_reg & MII_CR_POWER_DOWN)
2820                                         break;
2821                                 if(mii_reg & MII_CR_AUTO_NEG_EN) {
2822                                         adapter->hw.autoneg = 1;
2823                                         adapter->hw.autoneg_advertised = 0x2F;
2824                                 } else {
2825                                         if (mii_reg & 0x40)
2826                                                 spddplx = SPEED_1000;
2827                                         else if (mii_reg & 0x2000)
2828                                                 spddplx = SPEED_100;
2829                                         else
2830                                                 spddplx = SPEED_10;
2831                                         spddplx += (mii_reg & 0x100)
2832                                                    ? FULL_DUPLEX :
2833                                                    HALF_DUPLEX;
2834                                         retval = e1000_set_spd_dplx(adapter,
2835                                                                     spddplx);
2836                                         if(retval)
2837                                                 return retval;
2838                                 }
2839                                 if(netif_running(adapter->netdev)) {
2840                                         e1000_down(adapter);
2841                                         e1000_up(adapter);
2842                                 } else
2843                                         e1000_reset(adapter);
2844                                 break;
2845                         case M88E1000_PHY_SPEC_CTRL:
2846                         case M88E1000_EXT_PHY_SPEC_CTRL:
2847                                 if (e1000_phy_reset(&adapter->hw))
2848                                         return -EIO;
2849                                 break;
2850                         }
2851                 } else {
2852                         switch (data->reg_num) {
2853                         case PHY_CTRL:
2854                                 if(mii_reg & MII_CR_POWER_DOWN)
2855                                         break;
2856                                 if(netif_running(adapter->netdev)) {
2857                                         e1000_down(adapter);
2858                                         e1000_up(adapter);
2859                                 } else
2860                                         e1000_reset(adapter);
2861                                 break;
2862                         }
2863                 }
2864                 break;
2865         default:
2866                 return -EOPNOTSUPP;
2867         }
2868         return E1000_SUCCESS;
2869 }
2870
2871 void
2872 e1000_pci_set_mwi(struct e1000_hw *hw)
2873 {
2874         struct e1000_adapter *adapter = hw->back;
2875
2876         int ret;
2877         ret = pci_set_mwi(adapter->pdev);
2878 }
2879
2880 void
2881 e1000_pci_clear_mwi(struct e1000_hw *hw)
2882 {
2883         struct e1000_adapter *adapter = hw->back;
2884
2885         pci_clear_mwi(adapter->pdev);
2886 }
2887
2888 void
2889 e1000_read_pci_cfg(struct e1000_hw *hw, uint32_t reg, uint16_t *value)
2890 {
2891         struct e1000_adapter *adapter = hw->back;
2892
2893         pci_read_config_word(adapter->pdev, reg, value);
2894 }
2895
2896 void
2897 e1000_write_pci_cfg(struct e1000_hw *hw, uint32_t reg, uint16_t *value)
2898 {
2899         struct e1000_adapter *adapter = hw->back;
2900
2901         pci_write_config_word(adapter->pdev, reg, *value);
2902 }
2903
2904 uint32_t
2905 e1000_io_read(struct e1000_hw *hw, unsigned long port)
2906 {
2907         return inl(port);
2908 }
2909
2910 void
2911 e1000_io_write(struct e1000_hw *hw, unsigned long port, uint32_t value)
2912 {
2913         outl(value, port);
2914 }
2915
2916 static void
2917 e1000_vlan_rx_register(struct net_device *netdev, struct vlan_group *grp)
2918 {
2919         struct e1000_adapter *adapter = netdev->priv;
2920         uint32_t ctrl, rctl;
2921
2922         e1000_irq_disable(adapter);
2923         adapter->vlgrp = grp;
2924
2925         if(grp) {
2926                 /* enable VLAN tag insert/strip */
2927                 ctrl = E1000_READ_REG(&adapter->hw, CTRL);
2928                 ctrl |= E1000_CTRL_VME;
2929                 E1000_WRITE_REG(&adapter->hw, CTRL, ctrl);
2930
2931                 /* enable VLAN receive filtering */
2932                 rctl = E1000_READ_REG(&adapter->hw, RCTL);
2933                 rctl |= E1000_RCTL_VFE;
2934                 rctl &= ~E1000_RCTL_CFIEN;
2935                 E1000_WRITE_REG(&adapter->hw, RCTL, rctl);
2936         } else {
2937                 /* disable VLAN tag insert/strip */
2938                 ctrl = E1000_READ_REG(&adapter->hw, CTRL);
2939                 ctrl &= ~E1000_CTRL_VME;
2940                 E1000_WRITE_REG(&adapter->hw, CTRL, ctrl);
2941
2942                 /* disable VLAN filtering */
2943                 rctl = E1000_READ_REG(&adapter->hw, RCTL);
2944                 rctl &= ~E1000_RCTL_VFE;
2945                 E1000_WRITE_REG(&adapter->hw, RCTL, rctl);
2946         }
2947
2948         e1000_irq_enable(adapter);
2949 }
2950
2951 static void
2952 e1000_vlan_rx_add_vid(struct net_device *netdev, uint16_t vid)
2953 {
2954         struct e1000_adapter *adapter = netdev->priv;
2955         uint32_t vfta, index;
2956
2957         /* add VID to filter table */
2958         index = (vid >> 5) & 0x7F;
2959         vfta = E1000_READ_REG_ARRAY(&adapter->hw, VFTA, index);
2960         vfta |= (1 << (vid & 0x1F));
2961         e1000_write_vfta(&adapter->hw, index, vfta);
2962 }
2963
2964 static void
2965 e1000_vlan_rx_kill_vid(struct net_device *netdev, uint16_t vid)
2966 {
2967         struct e1000_adapter *adapter = netdev->priv;
2968         uint32_t vfta, index;
2969
2970         e1000_irq_disable(adapter);
2971
2972         if(adapter->vlgrp)
2973                 adapter->vlgrp->vlan_devices[vid] = NULL;
2974
2975         e1000_irq_enable(adapter);
2976
2977         /* remove VID from filter table */
2978         index = (vid >> 5) & 0x7F;
2979         vfta = E1000_READ_REG_ARRAY(&adapter->hw, VFTA, index);
2980         vfta &= ~(1 << (vid & 0x1F));
2981         e1000_write_vfta(&adapter->hw, index, vfta);
2982 }
2983
2984 static void
2985 e1000_restore_vlan(struct e1000_adapter *adapter)
2986 {
2987         e1000_vlan_rx_register(adapter->netdev, adapter->vlgrp);
2988
2989         if(adapter->vlgrp) {
2990                 uint16_t vid;
2991                 for(vid = 0; vid < VLAN_GROUP_ARRAY_LEN; vid++) {
2992                         if(!adapter->vlgrp->vlan_devices[vid])
2993                                 continue;
2994                         e1000_vlan_rx_add_vid(adapter->netdev, vid);
2995                 }
2996         }
2997 }
2998
2999 int
3000 e1000_set_spd_dplx(struct e1000_adapter *adapter, uint16_t spddplx)
3001 {
3002         adapter->hw.autoneg = 0;
3003
3004         switch(spddplx) {
3005         case SPEED_10 + DUPLEX_HALF:
3006                 adapter->hw.forced_speed_duplex = e1000_10_half;
3007                 break;
3008         case SPEED_10 + DUPLEX_FULL:
3009                 adapter->hw.forced_speed_duplex = e1000_10_full;
3010                 break;
3011         case SPEED_100 + DUPLEX_HALF:
3012                 adapter->hw.forced_speed_duplex = e1000_100_half;
3013                 break;
3014         case SPEED_100 + DUPLEX_FULL:
3015                 adapter->hw.forced_speed_duplex = e1000_100_full;
3016                 break;
3017         case SPEED_1000 + DUPLEX_FULL:
3018                 adapter->hw.autoneg = 1;
3019                 adapter->hw.autoneg_advertised = ADVERTISE_1000_FULL;
3020                 break;
3021         case SPEED_1000 + DUPLEX_HALF: /* not supported */
3022         default:
3023                 DPRINTK(PROBE, ERR, 
3024                         "Unsupported Speed/Duplexity configuration\n");
3025                 return -EINVAL;
3026         }
3027         return 0;
3028 }
3029
3030 static int
3031 e1000_notify_reboot(struct notifier_block *nb, unsigned long event, void *p)
3032 {
3033         struct pci_dev *pdev = NULL;
3034
3035         switch(event) {
3036         case SYS_DOWN:
3037         case SYS_HALT:
3038         case SYS_POWER_OFF:
3039                 while((pdev = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, pdev))) {
3040                         if(pci_dev_driver(pdev) == &e1000_driver)
3041                                 e1000_suspend(pdev, 3);
3042                 }
3043         }
3044         return NOTIFY_DONE;
3045 }
3046
3047 static int
3048 e1000_suspend(struct pci_dev *pdev, uint32_t state)
3049 {
3050         struct net_device *netdev = pci_get_drvdata(pdev);
3051         struct e1000_adapter *adapter = netdev->priv;
3052         uint32_t ctrl, ctrl_ext, rctl, manc, status;
3053         uint32_t wufc = adapter->wol;
3054
3055         netif_device_detach(netdev);
3056
3057         if(netif_running(netdev))
3058                 e1000_down(adapter);
3059
3060         status = E1000_READ_REG(&adapter->hw, STATUS);
3061         if(status & E1000_STATUS_LU)
3062                 wufc &= ~E1000_WUFC_LNKC;
3063
3064         if(wufc) {
3065                 e1000_setup_rctl(adapter);
3066                 e1000_set_multi(netdev);
3067
3068                 /* turn on all-multi mode if wake on multicast is enabled */
3069                 if(adapter->wol & E1000_WUFC_MC) {
3070                         rctl = E1000_READ_REG(&adapter->hw, RCTL);
3071                         rctl |= E1000_RCTL_MPE;
3072                         E1000_WRITE_REG(&adapter->hw, RCTL, rctl);
3073                 }
3074
3075                 if(adapter->hw.mac_type >= e1000_82540) {
3076                         ctrl = E1000_READ_REG(&adapter->hw, CTRL);
3077                         /* advertise wake from D3Cold */
3078                         #define E1000_CTRL_ADVD3WUC 0x00100000
3079                         /* phy power management enable */
3080                         #define E1000_CTRL_EN_PHY_PWR_MGMT 0x00200000
3081                         ctrl |= E1000_CTRL_ADVD3WUC |
3082                                 E1000_CTRL_EN_PHY_PWR_MGMT;
3083                         E1000_WRITE_REG(&adapter->hw, CTRL, ctrl);
3084                 }
3085
3086                 if(adapter->hw.media_type == e1000_media_type_fiber ||
3087                    adapter->hw.media_type == e1000_media_type_internal_serdes) {
3088                         /* keep the laser running in D3 */
3089                         ctrl_ext = E1000_READ_REG(&adapter->hw, CTRL_EXT);
3090                         ctrl_ext |= E1000_CTRL_EXT_SDP7_DATA;
3091                         E1000_WRITE_REG(&adapter->hw, CTRL_EXT, ctrl_ext);
3092                 }
3093
3094                 E1000_WRITE_REG(&adapter->hw, WUC, E1000_WUC_PME_EN);
3095                 E1000_WRITE_REG(&adapter->hw, WUFC, wufc);
3096                 pci_enable_wake(pdev, 3, 1);
3097                 pci_enable_wake(pdev, 4, 1); /* 4 == D3 cold */
3098         } else {
3099                 E1000_WRITE_REG(&adapter->hw, WUC, 0);
3100                 E1000_WRITE_REG(&adapter->hw, WUFC, 0);
3101                 pci_enable_wake(pdev, 3, 0);
3102                 pci_enable_wake(pdev, 4, 0); /* 4 == D3 cold */
3103         }
3104
3105         pci_save_state(pdev);
3106
3107         if(adapter->hw.mac_type >= e1000_82540 &&
3108            adapter->hw.media_type == e1000_media_type_copper) {
3109                 manc = E1000_READ_REG(&adapter->hw, MANC);
3110                 if(manc & E1000_MANC_SMBUS_EN) {
3111                         manc |= E1000_MANC_ARP_EN;
3112                         E1000_WRITE_REG(&adapter->hw, MANC, manc);
3113                         pci_enable_wake(pdev, 3, 1);
3114                         pci_enable_wake(pdev, 4, 1); /* 4 == D3 cold */
3115                 }
3116         }
3117
3118         pci_disable_device(pdev);
3119
3120         state = (state > 0) ? 3 : 0;
3121         pci_set_power_state(pdev, state);
3122
3123         return 0;
3124 }
3125
3126 #ifdef CONFIG_PM
3127 static int
3128 e1000_resume(struct pci_dev *pdev)
3129 {
3130         struct net_device *netdev = pci_get_drvdata(pdev);
3131         struct e1000_adapter *adapter = netdev->priv;
3132         uint32_t manc, ret;
3133
3134         pci_set_power_state(pdev, 0);
3135         pci_restore_state(pdev);
3136         ret = pci_enable_device(pdev);
3137         if (pdev->is_busmaster)
3138                 pci_set_master(pdev);
3139
3140         pci_enable_wake(pdev, 3, 0);
3141         pci_enable_wake(pdev, 4, 0); /* 4 == D3 cold */
3142
3143         e1000_reset(adapter);
3144         E1000_WRITE_REG(&adapter->hw, WUS, ~0);
3145
3146         if(netif_running(netdev))
3147                 e1000_up(adapter);
3148
3149         netif_device_attach(netdev);
3150
3151         if(adapter->hw.mac_type >= e1000_82540 &&
3152            adapter->hw.media_type == e1000_media_type_copper) {
3153                 manc = E1000_READ_REG(&adapter->hw, MANC);
3154                 manc &= ~(E1000_MANC_ARP_EN);
3155                 E1000_WRITE_REG(&adapter->hw, MANC, manc);
3156         }
3157
3158         return 0;
3159 }
3160 #endif
3161
3162 #ifdef CONFIG_NET_POLL_CONTROLLER
3163 /*
3164  * Polling 'interrupt' - used by things like netconsole to send skbs
3165  * without having to re-enable interrupts. It's not called while
3166  * the interrupt routine is executing.
3167  */
3168 static void
3169 e1000_netpoll (struct net_device *netdev)
3170 {
3171         struct e1000_adapter *adapter = netdev->priv;
3172         disable_irq(adapter->pdev->irq);
3173         e1000_intr(adapter->pdev->irq, netdev, NULL);
3174         enable_irq(adapter->pdev->irq);
3175 }
3176 #endif
3177
3178 /* e1000_main.c */