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