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