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