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