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