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