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