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