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