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