[PATCH] SCSI: fix request flag-related build breakage
[sfrench/cifs-2.6.git] / drivers / net / e1000 / e1000_main.c
1 /*******************************************************************************
2
3   
4   Copyright(c) 1999 - 2006 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   e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
26   Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27
28 *******************************************************************************/
29
30 #include "e1000.h"
31
32 char e1000_driver_name[] = "e1000";
33 static char e1000_driver_string[] = "Intel(R) PRO/1000 Network Driver";
34 #ifndef CONFIG_E1000_NAPI
35 #define DRIVERNAPI
36 #else
37 #define DRIVERNAPI "-NAPI"
38 #endif
39 #define DRV_VERSION "7.2.7-k2"DRIVERNAPI
40 char e1000_driver_version[] = DRV_VERSION;
41 static char e1000_copyright[] = "Copyright (c) 1999-2006 Intel Corporation.";
42
43 /* e1000_pci_tbl - PCI Device ID Table
44  *
45  * Last entry must be all 0s
46  *
47  * Macro expands to...
48  *   {PCI_DEVICE(PCI_VENDOR_ID_INTEL, device_id)}
49  */
50 static struct pci_device_id e1000_pci_tbl[] = {
51         INTEL_E1000_ETHERNET_DEVICE(0x1000),
52         INTEL_E1000_ETHERNET_DEVICE(0x1001),
53         INTEL_E1000_ETHERNET_DEVICE(0x1004),
54         INTEL_E1000_ETHERNET_DEVICE(0x1008),
55         INTEL_E1000_ETHERNET_DEVICE(0x1009),
56         INTEL_E1000_ETHERNET_DEVICE(0x100C),
57         INTEL_E1000_ETHERNET_DEVICE(0x100D),
58         INTEL_E1000_ETHERNET_DEVICE(0x100E),
59         INTEL_E1000_ETHERNET_DEVICE(0x100F),
60         INTEL_E1000_ETHERNET_DEVICE(0x1010),
61         INTEL_E1000_ETHERNET_DEVICE(0x1011),
62         INTEL_E1000_ETHERNET_DEVICE(0x1012),
63         INTEL_E1000_ETHERNET_DEVICE(0x1013),
64         INTEL_E1000_ETHERNET_DEVICE(0x1014),
65         INTEL_E1000_ETHERNET_DEVICE(0x1015),
66         INTEL_E1000_ETHERNET_DEVICE(0x1016),
67         INTEL_E1000_ETHERNET_DEVICE(0x1017),
68         INTEL_E1000_ETHERNET_DEVICE(0x1018),
69         INTEL_E1000_ETHERNET_DEVICE(0x1019),
70         INTEL_E1000_ETHERNET_DEVICE(0x101A),
71         INTEL_E1000_ETHERNET_DEVICE(0x101D),
72         INTEL_E1000_ETHERNET_DEVICE(0x101E),
73         INTEL_E1000_ETHERNET_DEVICE(0x1026),
74         INTEL_E1000_ETHERNET_DEVICE(0x1027),
75         INTEL_E1000_ETHERNET_DEVICE(0x1028),
76         INTEL_E1000_ETHERNET_DEVICE(0x1049),
77         INTEL_E1000_ETHERNET_DEVICE(0x104A),
78         INTEL_E1000_ETHERNET_DEVICE(0x104B),
79         INTEL_E1000_ETHERNET_DEVICE(0x104C),
80         INTEL_E1000_ETHERNET_DEVICE(0x104D),
81         INTEL_E1000_ETHERNET_DEVICE(0x105E),
82         INTEL_E1000_ETHERNET_DEVICE(0x105F),
83         INTEL_E1000_ETHERNET_DEVICE(0x1060),
84         INTEL_E1000_ETHERNET_DEVICE(0x1075),
85         INTEL_E1000_ETHERNET_DEVICE(0x1076),
86         INTEL_E1000_ETHERNET_DEVICE(0x1077),
87         INTEL_E1000_ETHERNET_DEVICE(0x1078),
88         INTEL_E1000_ETHERNET_DEVICE(0x1079),
89         INTEL_E1000_ETHERNET_DEVICE(0x107A),
90         INTEL_E1000_ETHERNET_DEVICE(0x107B),
91         INTEL_E1000_ETHERNET_DEVICE(0x107C),
92         INTEL_E1000_ETHERNET_DEVICE(0x107D),
93         INTEL_E1000_ETHERNET_DEVICE(0x107E),
94         INTEL_E1000_ETHERNET_DEVICE(0x107F),
95         INTEL_E1000_ETHERNET_DEVICE(0x108A),
96         INTEL_E1000_ETHERNET_DEVICE(0x108B),
97         INTEL_E1000_ETHERNET_DEVICE(0x108C),
98         INTEL_E1000_ETHERNET_DEVICE(0x1096),
99         INTEL_E1000_ETHERNET_DEVICE(0x1098),
100         INTEL_E1000_ETHERNET_DEVICE(0x1099),
101         INTEL_E1000_ETHERNET_DEVICE(0x109A),
102         INTEL_E1000_ETHERNET_DEVICE(0x10A4),
103         INTEL_E1000_ETHERNET_DEVICE(0x10B5),
104         INTEL_E1000_ETHERNET_DEVICE(0x10B9),
105         INTEL_E1000_ETHERNET_DEVICE(0x10BA),
106         INTEL_E1000_ETHERNET_DEVICE(0x10BB),
107         /* required last entry */
108         {0,}
109 };
110
111 MODULE_DEVICE_TABLE(pci, e1000_pci_tbl);
112
113 static int e1000_setup_tx_resources(struct e1000_adapter *adapter,
114                                     struct e1000_tx_ring *txdr);
115 static int e1000_setup_rx_resources(struct e1000_adapter *adapter,
116                                     struct e1000_rx_ring *rxdr);
117 static void e1000_free_tx_resources(struct e1000_adapter *adapter,
118                                     struct e1000_tx_ring *tx_ring);
119 static void e1000_free_rx_resources(struct e1000_adapter *adapter,
120                                     struct e1000_rx_ring *rx_ring);
121
122 /* Local Function Prototypes */
123
124 static int e1000_init_module(void);
125 static void e1000_exit_module(void);
126 static int e1000_probe(struct pci_dev *pdev, const struct pci_device_id *ent);
127 static void __devexit e1000_remove(struct pci_dev *pdev);
128 static int e1000_alloc_queues(struct e1000_adapter *adapter);
129 static int e1000_sw_init(struct e1000_adapter *adapter);
130 static int e1000_open(struct net_device *netdev);
131 static int e1000_close(struct net_device *netdev);
132 static void e1000_configure_tx(struct e1000_adapter *adapter);
133 static void e1000_configure_rx(struct e1000_adapter *adapter);
134 static void e1000_setup_rctl(struct e1000_adapter *adapter);
135 static void e1000_clean_all_tx_rings(struct e1000_adapter *adapter);
136 static void e1000_clean_all_rx_rings(struct e1000_adapter *adapter);
137 static void e1000_clean_tx_ring(struct e1000_adapter *adapter,
138                                 struct e1000_tx_ring *tx_ring);
139 static void e1000_clean_rx_ring(struct e1000_adapter *adapter,
140                                 struct e1000_rx_ring *rx_ring);
141 static void e1000_set_multi(struct net_device *netdev);
142 static void e1000_update_phy_info(unsigned long data);
143 static void e1000_watchdog(unsigned long data);
144 static void e1000_82547_tx_fifo_stall(unsigned long data);
145 static int e1000_xmit_frame(struct sk_buff *skb, struct net_device *netdev);
146 static struct net_device_stats * e1000_get_stats(struct net_device *netdev);
147 static int e1000_change_mtu(struct net_device *netdev, int new_mtu);
148 static int e1000_set_mac(struct net_device *netdev, void *p);
149 static irqreturn_t e1000_intr(int irq, void *data, struct pt_regs *regs);
150 static boolean_t e1000_clean_tx_irq(struct e1000_adapter *adapter,
151                                     struct e1000_tx_ring *tx_ring);
152 #ifdef CONFIG_E1000_NAPI
153 static int e1000_clean(struct net_device *poll_dev, int *budget);
154 static boolean_t e1000_clean_rx_irq(struct e1000_adapter *adapter,
155                                     struct e1000_rx_ring *rx_ring,
156                                     int *work_done, int work_to_do);
157 static boolean_t e1000_clean_rx_irq_ps(struct e1000_adapter *adapter,
158                                        struct e1000_rx_ring *rx_ring,
159                                        int *work_done, int work_to_do);
160 #else
161 static boolean_t e1000_clean_rx_irq(struct e1000_adapter *adapter,
162                                     struct e1000_rx_ring *rx_ring);
163 static boolean_t e1000_clean_rx_irq_ps(struct e1000_adapter *adapter,
164                                        struct e1000_rx_ring *rx_ring);
165 #endif
166 static void e1000_alloc_rx_buffers(struct e1000_adapter *adapter,
167                                    struct e1000_rx_ring *rx_ring,
168                                    int cleaned_count);
169 static void e1000_alloc_rx_buffers_ps(struct e1000_adapter *adapter,
170                                       struct e1000_rx_ring *rx_ring,
171                                       int cleaned_count);
172 static int e1000_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd);
173 static int e1000_mii_ioctl(struct net_device *netdev, struct ifreq *ifr,
174                            int cmd);
175 static void e1000_enter_82542_rst(struct e1000_adapter *adapter);
176 static void e1000_leave_82542_rst(struct e1000_adapter *adapter);
177 static void e1000_tx_timeout(struct net_device *dev);
178 static void e1000_reset_task(struct net_device *dev);
179 static void e1000_smartspeed(struct e1000_adapter *adapter);
180 static int e1000_82547_fifo_workaround(struct e1000_adapter *adapter,
181                                        struct sk_buff *skb);
182
183 static void e1000_vlan_rx_register(struct net_device *netdev, struct vlan_group *grp);
184 static void e1000_vlan_rx_add_vid(struct net_device *netdev, uint16_t vid);
185 static void e1000_vlan_rx_kill_vid(struct net_device *netdev, uint16_t vid);
186 static void e1000_restore_vlan(struct e1000_adapter *adapter);
187
188 static int e1000_suspend(struct pci_dev *pdev, pm_message_t state);
189 #ifdef CONFIG_PM
190 static int e1000_resume(struct pci_dev *pdev);
191 #endif
192 static void e1000_shutdown(struct pci_dev *pdev);
193
194 #ifdef CONFIG_NET_POLL_CONTROLLER
195 /* for netdump / net console */
196 static void e1000_netpoll (struct net_device *netdev);
197 #endif
198
199 static pci_ers_result_t e1000_io_error_detected(struct pci_dev *pdev,
200                      pci_channel_state_t state);
201 static pci_ers_result_t e1000_io_slot_reset(struct pci_dev *pdev);
202 static void e1000_io_resume(struct pci_dev *pdev);
203
204 static struct pci_error_handlers e1000_err_handler = {
205         .error_detected = e1000_io_error_detected,
206         .slot_reset = e1000_io_slot_reset,
207         .resume = e1000_io_resume,
208 };
209
210 static struct pci_driver e1000_driver = {
211         .name     = e1000_driver_name,
212         .id_table = e1000_pci_tbl,
213         .probe    = e1000_probe,
214         .remove   = __devexit_p(e1000_remove),
215         /* Power Managment Hooks */
216         .suspend  = e1000_suspend,
217 #ifdef CONFIG_PM
218         .resume   = e1000_resume,
219 #endif
220         .shutdown = e1000_shutdown,
221         .err_handler = &e1000_err_handler
222 };
223
224 MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
225 MODULE_DESCRIPTION("Intel(R) PRO/1000 Network Driver");
226 MODULE_LICENSE("GPL");
227 MODULE_VERSION(DRV_VERSION);
228
229 static int debug = NETIF_MSG_DRV | NETIF_MSG_PROBE;
230 module_param(debug, int, 0);
231 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
232
233 /**
234  * e1000_init_module - Driver Registration Routine
235  *
236  * e1000_init_module is the first routine called when the driver is
237  * loaded. All it does is register with the PCI subsystem.
238  **/
239
240 static int __init
241 e1000_init_module(void)
242 {
243         int ret;
244         printk(KERN_INFO "%s - version %s\n",
245                e1000_driver_string, e1000_driver_version);
246
247         printk(KERN_INFO "%s\n", e1000_copyright);
248
249         ret = pci_register_driver(&e1000_driver);
250
251         return ret;
252 }
253
254 module_init(e1000_init_module);
255
256 /**
257  * e1000_exit_module - Driver Exit Cleanup Routine
258  *
259  * e1000_exit_module is called just before the driver is removed
260  * from memory.
261  **/
262
263 static void __exit
264 e1000_exit_module(void)
265 {
266         pci_unregister_driver(&e1000_driver);
267 }
268
269 module_exit(e1000_exit_module);
270
271 static int e1000_request_irq(struct e1000_adapter *adapter)
272 {
273         struct net_device *netdev = adapter->netdev;
274         int flags, err = 0;
275
276         flags = IRQF_SHARED;
277 #ifdef CONFIG_PCI_MSI
278         if (adapter->hw.mac_type > e1000_82547_rev_2) {
279                 adapter->have_msi = TRUE;
280                 if ((err = pci_enable_msi(adapter->pdev))) {
281                         DPRINTK(PROBE, ERR,
282                          "Unable to allocate MSI interrupt Error: %d\n", err);
283                         adapter->have_msi = FALSE;
284                 }
285         }
286         if (adapter->have_msi)
287                 flags &= ~IRQF_SHARED;
288 #endif
289         if ((err = request_irq(adapter->pdev->irq, &e1000_intr, flags,
290                                netdev->name, netdev)))
291                 DPRINTK(PROBE, ERR,
292                         "Unable to allocate interrupt Error: %d\n", err);
293
294         return err;
295 }
296
297 static void e1000_free_irq(struct e1000_adapter *adapter)
298 {
299         struct net_device *netdev = adapter->netdev;
300
301         free_irq(adapter->pdev->irq, netdev);
302
303 #ifdef CONFIG_PCI_MSI
304         if (adapter->have_msi)
305                 pci_disable_msi(adapter->pdev);
306 #endif
307 }
308
309 /**
310  * e1000_irq_disable - Mask off interrupt generation on the NIC
311  * @adapter: board private structure
312  **/
313
314 static void
315 e1000_irq_disable(struct e1000_adapter *adapter)
316 {
317         atomic_inc(&adapter->irq_sem);
318         E1000_WRITE_REG(&adapter->hw, IMC, ~0);
319         E1000_WRITE_FLUSH(&adapter->hw);
320         synchronize_irq(adapter->pdev->irq);
321 }
322
323 /**
324  * e1000_irq_enable - Enable default interrupt generation settings
325  * @adapter: board private structure
326  **/
327
328 static void
329 e1000_irq_enable(struct e1000_adapter *adapter)
330 {
331         if (likely(atomic_dec_and_test(&adapter->irq_sem))) {
332                 E1000_WRITE_REG(&adapter->hw, IMS, IMS_ENABLE_MASK);
333                 E1000_WRITE_FLUSH(&adapter->hw);
334         }
335 }
336
337 static void
338 e1000_update_mng_vlan(struct e1000_adapter *adapter)
339 {
340         struct net_device *netdev = adapter->netdev;
341         uint16_t vid = adapter->hw.mng_cookie.vlan_id;
342         uint16_t old_vid = adapter->mng_vlan_id;
343         if (adapter->vlgrp) {
344                 if (!adapter->vlgrp->vlan_devices[vid]) {
345                         if (adapter->hw.mng_cookie.status &
346                                 E1000_MNG_DHCP_COOKIE_STATUS_VLAN_SUPPORT) {
347                                 e1000_vlan_rx_add_vid(netdev, vid);
348                                 adapter->mng_vlan_id = vid;
349                         } else
350                                 adapter->mng_vlan_id = E1000_MNG_VLAN_NONE;
351
352                         if ((old_vid != (uint16_t)E1000_MNG_VLAN_NONE) &&
353                                         (vid != old_vid) &&
354                                         !adapter->vlgrp->vlan_devices[old_vid])
355                                 e1000_vlan_rx_kill_vid(netdev, old_vid);
356                 } else
357                         adapter->mng_vlan_id = vid;
358         }
359 }
360
361 /**
362  * e1000_release_hw_control - release control of the h/w to f/w
363  * @adapter: address of board private structure
364  *
365  * e1000_release_hw_control resets {CTRL_EXT|FWSM}:DRV_LOAD bit.
366  * For ASF and Pass Through versions of f/w this means that the
367  * driver is no longer loaded. For AMT version (only with 82573) i
368  * of the f/w this means that the netowrk i/f is closed.
369  *
370  **/
371
372 static void
373 e1000_release_hw_control(struct e1000_adapter *adapter)
374 {
375         uint32_t ctrl_ext;
376         uint32_t swsm;
377         uint32_t extcnf;
378
379         /* Let firmware taken over control of h/w */
380         switch (adapter->hw.mac_type) {
381         case e1000_82571:
382         case e1000_82572:
383         case e1000_80003es2lan:
384                 ctrl_ext = E1000_READ_REG(&adapter->hw, CTRL_EXT);
385                 E1000_WRITE_REG(&adapter->hw, CTRL_EXT,
386                                 ctrl_ext & ~E1000_CTRL_EXT_DRV_LOAD);
387                 break;
388         case e1000_82573:
389                 swsm = E1000_READ_REG(&adapter->hw, SWSM);
390                 E1000_WRITE_REG(&adapter->hw, SWSM,
391                                 swsm & ~E1000_SWSM_DRV_LOAD);
392         case e1000_ich8lan:
393                 extcnf = E1000_READ_REG(&adapter->hw, CTRL_EXT);
394                 E1000_WRITE_REG(&adapter->hw, CTRL_EXT,
395                                 extcnf & ~E1000_CTRL_EXT_DRV_LOAD);
396                 break;
397         default:
398                 break;
399         }
400 }
401
402 /**
403  * e1000_get_hw_control - get control of the h/w from f/w
404  * @adapter: address of board private structure
405  *
406  * e1000_get_hw_control sets {CTRL_EXT|FWSM}:DRV_LOAD bit.
407  * For ASF and Pass Through versions of f/w this means that
408  * the driver is loaded. For AMT version (only with 82573)
409  * of the f/w this means that the netowrk i/f is open.
410  *
411  **/
412
413 static void
414 e1000_get_hw_control(struct e1000_adapter *adapter)
415 {
416         uint32_t ctrl_ext;
417         uint32_t swsm;
418         uint32_t extcnf;
419         /* Let firmware know the driver has taken over */
420         switch (adapter->hw.mac_type) {
421         case e1000_82571:
422         case e1000_82572:
423         case e1000_80003es2lan:
424                 ctrl_ext = E1000_READ_REG(&adapter->hw, CTRL_EXT);
425                 E1000_WRITE_REG(&adapter->hw, CTRL_EXT,
426                                 ctrl_ext | E1000_CTRL_EXT_DRV_LOAD);
427                 break;
428         case e1000_82573:
429                 swsm = E1000_READ_REG(&adapter->hw, SWSM);
430                 E1000_WRITE_REG(&adapter->hw, SWSM,
431                                 swsm | E1000_SWSM_DRV_LOAD);
432                 break;
433         case e1000_ich8lan:
434                 extcnf = E1000_READ_REG(&adapter->hw, EXTCNF_CTRL);
435                 E1000_WRITE_REG(&adapter->hw, EXTCNF_CTRL,
436                                 extcnf | E1000_EXTCNF_CTRL_SWFLAG);
437                 break;
438         default:
439                 break;
440         }
441 }
442
443 int
444 e1000_up(struct e1000_adapter *adapter)
445 {
446         struct net_device *netdev = adapter->netdev;
447         int i;
448
449         /* hardware has been reset, we need to reload some things */
450
451         e1000_set_multi(netdev);
452
453         e1000_restore_vlan(adapter);
454
455         e1000_configure_tx(adapter);
456         e1000_setup_rctl(adapter);
457         e1000_configure_rx(adapter);
458         /* call E1000_DESC_UNUSED which always leaves
459          * at least 1 descriptor unused to make sure
460          * next_to_use != next_to_clean */
461         for (i = 0; i < adapter->num_rx_queues; i++) {
462                 struct e1000_rx_ring *ring = &adapter->rx_ring[i];
463                 adapter->alloc_rx_buf(adapter, ring,
464                                       E1000_DESC_UNUSED(ring));
465         }
466
467         adapter->tx_queue_len = netdev->tx_queue_len;
468
469         mod_timer(&adapter->watchdog_timer, jiffies);
470
471 #ifdef CONFIG_E1000_NAPI
472         netif_poll_enable(netdev);
473 #endif
474         e1000_irq_enable(adapter);
475
476         return 0;
477 }
478
479 /**
480  * e1000_power_up_phy - restore link in case the phy was powered down
481  * @adapter: address of board private structure
482  *
483  * The phy may be powered down to save power and turn off link when the
484  * driver is unloaded and wake on lan is not enabled (among others)
485  * *** this routine MUST be followed by a call to e1000_reset ***
486  *
487  **/
488
489 void e1000_power_up_phy(struct e1000_adapter *adapter)
490 {
491         uint16_t mii_reg = 0;
492
493         /* Just clear the power down bit to wake the phy back up */
494         if (adapter->hw.media_type == e1000_media_type_copper) {
495                 /* according to the manual, the phy will retain its
496                  * settings across a power-down/up cycle */
497                 e1000_read_phy_reg(&adapter->hw, PHY_CTRL, &mii_reg);
498                 mii_reg &= ~MII_CR_POWER_DOWN;
499                 e1000_write_phy_reg(&adapter->hw, PHY_CTRL, mii_reg);
500         }
501 }
502
503 static void e1000_power_down_phy(struct e1000_adapter *adapter)
504 {
505         boolean_t mng_mode_enabled = (adapter->hw.mac_type >= e1000_82571) &&
506                                       e1000_check_mng_mode(&adapter->hw);
507         /* Power down the PHY so no link is implied when interface is down
508          * The PHY cannot be powered down if any of the following is TRUE
509          * (a) WoL is enabled
510          * (b) AMT is active
511          * (c) SoL/IDER session is active */
512         if (!adapter->wol && adapter->hw.mac_type >= e1000_82540 &&
513             adapter->hw.mac_type != e1000_ich8lan &&
514             adapter->hw.media_type == e1000_media_type_copper &&
515             !(E1000_READ_REG(&adapter->hw, MANC) & E1000_MANC_SMBUS_EN) &&
516             !mng_mode_enabled &&
517             !e1000_check_phy_reset_block(&adapter->hw)) {
518                 uint16_t mii_reg = 0;
519                 e1000_read_phy_reg(&adapter->hw, PHY_CTRL, &mii_reg);
520                 mii_reg |= MII_CR_POWER_DOWN;
521                 e1000_write_phy_reg(&adapter->hw, PHY_CTRL, mii_reg);
522                 mdelay(1);
523         }
524 }
525
526 void
527 e1000_down(struct e1000_adapter *adapter)
528 {
529         struct net_device *netdev = adapter->netdev;
530
531         e1000_irq_disable(adapter);
532
533         del_timer_sync(&adapter->tx_fifo_stall_timer);
534         del_timer_sync(&adapter->watchdog_timer);
535         del_timer_sync(&adapter->phy_info_timer);
536
537 #ifdef CONFIG_E1000_NAPI
538         netif_poll_disable(netdev);
539 #endif
540         netdev->tx_queue_len = adapter->tx_queue_len;
541         adapter->link_speed = 0;
542         adapter->link_duplex = 0;
543         netif_carrier_off(netdev);
544         netif_stop_queue(netdev);
545
546         e1000_reset(adapter);
547         e1000_clean_all_tx_rings(adapter);
548         e1000_clean_all_rx_rings(adapter);
549 }
550
551 void
552 e1000_reinit_locked(struct e1000_adapter *adapter)
553 {
554         WARN_ON(in_interrupt());
555         while (test_and_set_bit(__E1000_RESETTING, &adapter->flags))
556                 msleep(1);
557         e1000_down(adapter);
558         e1000_up(adapter);
559         clear_bit(__E1000_RESETTING, &adapter->flags);
560 }
561
562 void
563 e1000_reset(struct e1000_adapter *adapter)
564 {
565         uint32_t pba, manc;
566         uint16_t fc_high_water_mark = E1000_FC_HIGH_DIFF;
567
568         /* Repartition Pba for greater than 9k mtu
569          * To take effect CTRL.RST is required.
570          */
571
572         switch (adapter->hw.mac_type) {
573         case e1000_82547:
574         case e1000_82547_rev_2:
575                 pba = E1000_PBA_30K;
576                 break;
577         case e1000_82571:
578         case e1000_82572:
579         case e1000_80003es2lan:
580                 pba = E1000_PBA_38K;
581                 break;
582         case e1000_82573:
583                 pba = E1000_PBA_12K;
584                 break;
585         case e1000_ich8lan:
586                 pba = E1000_PBA_8K;
587                 break;
588         default:
589                 pba = E1000_PBA_48K;
590                 break;
591         }
592
593         if ((adapter->hw.mac_type != e1000_82573) &&
594            (adapter->netdev->mtu > E1000_RXBUFFER_8192))
595                 pba -= 8; /* allocate more FIFO for Tx */
596
597
598         if (adapter->hw.mac_type == e1000_82547) {
599                 adapter->tx_fifo_head = 0;
600                 adapter->tx_head_addr = pba << E1000_TX_HEAD_ADDR_SHIFT;
601                 adapter->tx_fifo_size =
602                         (E1000_PBA_40K - pba) << E1000_PBA_BYTES_SHIFT;
603                 atomic_set(&adapter->tx_fifo_stall, 0);
604         }
605
606         E1000_WRITE_REG(&adapter->hw, PBA, pba);
607
608         /* flow control settings */
609         /* Set the FC high water mark to 90% of the FIFO size.
610          * Required to clear last 3 LSB */
611         fc_high_water_mark = ((pba * 9216)/10) & 0xFFF8;
612         /* We can't use 90% on small FIFOs because the remainder
613          * would be less than 1 full frame.  In this case, we size
614          * it to allow at least a full frame above the high water
615          *  mark. */
616         if (pba < E1000_PBA_16K)
617                 fc_high_water_mark = (pba * 1024) - 1600;
618
619         adapter->hw.fc_high_water = fc_high_water_mark;
620         adapter->hw.fc_low_water = fc_high_water_mark - 8;
621         if (adapter->hw.mac_type == e1000_80003es2lan)
622                 adapter->hw.fc_pause_time = 0xFFFF;
623         else
624                 adapter->hw.fc_pause_time = E1000_FC_PAUSE_TIME;
625         adapter->hw.fc_send_xon = 1;
626         adapter->hw.fc = adapter->hw.original_fc;
627
628         /* Allow time for pending master requests to run */
629         e1000_reset_hw(&adapter->hw);
630         if (adapter->hw.mac_type >= e1000_82544)
631                 E1000_WRITE_REG(&adapter->hw, WUC, 0);
632         if (e1000_init_hw(&adapter->hw))
633                 DPRINTK(PROBE, ERR, "Hardware Error\n");
634         e1000_update_mng_vlan(adapter);
635         /* Enable h/w to recognize an 802.1Q VLAN Ethernet packet */
636         E1000_WRITE_REG(&adapter->hw, VET, ETHERNET_IEEE_VLAN_TYPE);
637
638         e1000_reset_adaptive(&adapter->hw);
639         e1000_phy_get_info(&adapter->hw, &adapter->phy_info);
640
641         if (!adapter->smart_power_down &&
642             (adapter->hw.mac_type == e1000_82571 ||
643              adapter->hw.mac_type == e1000_82572)) {
644                 uint16_t phy_data = 0;
645                 /* speed up time to link by disabling smart power down, ignore
646                  * the return value of this function because there is nothing
647                  * different we would do if it failed */
648                 e1000_read_phy_reg(&adapter->hw, IGP02E1000_PHY_POWER_MGMT,
649                                    &phy_data);
650                 phy_data &= ~IGP02E1000_PM_SPD;
651                 e1000_write_phy_reg(&adapter->hw, IGP02E1000_PHY_POWER_MGMT,
652                                     phy_data);
653         }
654
655         if (adapter->hw.mac_type < e1000_ich8lan)
656         /* FIXME: this code is duplicate and wrong for PCI Express */
657         if (adapter->en_mng_pt) {
658                 manc = E1000_READ_REG(&adapter->hw, MANC);
659                 manc |= (E1000_MANC_ARP_EN | E1000_MANC_EN_MNG2HOST);
660                 E1000_WRITE_REG(&adapter->hw, MANC, manc);
661         }
662 }
663
664 /**
665  * e1000_probe - Device Initialization Routine
666  * @pdev: PCI device information struct
667  * @ent: entry in e1000_pci_tbl
668  *
669  * Returns 0 on success, negative on failure
670  *
671  * e1000_probe initializes an adapter identified by a pci_dev structure.
672  * The OS initialization, configuring of the adapter private structure,
673  * and a hardware reset occur.
674  **/
675
676 static int __devinit
677 e1000_probe(struct pci_dev *pdev,
678             const struct pci_device_id *ent)
679 {
680         struct net_device *netdev;
681         struct e1000_adapter *adapter;
682         unsigned long mmio_start, mmio_len;
683         unsigned long flash_start, flash_len;
684
685         static int cards_found = 0;
686         static int global_quad_port_a = 0; /* global ksp3 port a indication */
687         int i, err, pci_using_dac;
688         uint16_t eeprom_data = 0;
689         uint16_t eeprom_apme_mask = E1000_EEPROM_APME;
690         if ((err = pci_enable_device(pdev)))
691                 return err;
692
693         if (!(err = pci_set_dma_mask(pdev, DMA_64BIT_MASK)) &&
694             !(err = pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK))) {
695                 pci_using_dac = 1;
696         } else {
697                 if ((err = pci_set_dma_mask(pdev, DMA_32BIT_MASK)) &&
698                     (err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK))) {
699                         E1000_ERR("No usable DMA configuration, aborting\n");
700                         goto err_dma;
701                 }
702                 pci_using_dac = 0;
703         }
704
705         if ((err = pci_request_regions(pdev, e1000_driver_name)))
706                 goto err_pci_reg;
707
708         pci_set_master(pdev);
709
710         err = -ENOMEM;
711         netdev = alloc_etherdev(sizeof(struct e1000_adapter));
712         if (!netdev)
713                 goto err_alloc_etherdev;
714
715         SET_MODULE_OWNER(netdev);
716         SET_NETDEV_DEV(netdev, &pdev->dev);
717
718         pci_set_drvdata(pdev, netdev);
719         adapter = netdev_priv(netdev);
720         adapter->netdev = netdev;
721         adapter->pdev = pdev;
722         adapter->hw.back = adapter;
723         adapter->msg_enable = (1 << debug) - 1;
724
725         mmio_start = pci_resource_start(pdev, BAR_0);
726         mmio_len = pci_resource_len(pdev, BAR_0);
727
728         err = -EIO;
729         adapter->hw.hw_addr = ioremap(mmio_start, mmio_len);
730         if (!adapter->hw.hw_addr)
731                 goto err_ioremap;
732
733         for (i = BAR_1; i <= BAR_5; i++) {
734                 if (pci_resource_len(pdev, i) == 0)
735                         continue;
736                 if (pci_resource_flags(pdev, i) & IORESOURCE_IO) {
737                         adapter->hw.io_base = pci_resource_start(pdev, i);
738                         break;
739                 }
740         }
741
742         netdev->open = &e1000_open;
743         netdev->stop = &e1000_close;
744         netdev->hard_start_xmit = &e1000_xmit_frame;
745         netdev->get_stats = &e1000_get_stats;
746         netdev->set_multicast_list = &e1000_set_multi;
747         netdev->set_mac_address = &e1000_set_mac;
748         netdev->change_mtu = &e1000_change_mtu;
749         netdev->do_ioctl = &e1000_ioctl;
750         e1000_set_ethtool_ops(netdev);
751         netdev->tx_timeout = &e1000_tx_timeout;
752         netdev->watchdog_timeo = 5 * HZ;
753 #ifdef CONFIG_E1000_NAPI
754         netdev->poll = &e1000_clean;
755         netdev->weight = 64;
756 #endif
757         netdev->vlan_rx_register = e1000_vlan_rx_register;
758         netdev->vlan_rx_add_vid = e1000_vlan_rx_add_vid;
759         netdev->vlan_rx_kill_vid = e1000_vlan_rx_kill_vid;
760 #ifdef CONFIG_NET_POLL_CONTROLLER
761         netdev->poll_controller = e1000_netpoll;
762 #endif
763         strcpy(netdev->name, pci_name(pdev));
764
765         netdev->mem_start = mmio_start;
766         netdev->mem_end = mmio_start + mmio_len;
767         netdev->base_addr = adapter->hw.io_base;
768
769         adapter->bd_number = cards_found;
770
771         /* setup the private structure */
772
773         if ((err = e1000_sw_init(adapter)))
774                 goto err_sw_init;
775
776         err = -EIO;
777         /* Flash BAR mapping must happen after e1000_sw_init
778          * because it depends on mac_type */
779         if ((adapter->hw.mac_type == e1000_ich8lan) &&
780            (pci_resource_flags(pdev, 1) & IORESOURCE_MEM)) {
781                 flash_start = pci_resource_start(pdev, 1);
782                 flash_len = pci_resource_len(pdev, 1);
783                 adapter->hw.flash_address = ioremap(flash_start, flash_len);
784                 if (!adapter->hw.flash_address)
785                         goto err_flashmap;
786         }
787
788         if (e1000_check_phy_reset_block(&adapter->hw))
789                 DPRINTK(PROBE, INFO, "PHY reset is blocked due to SOL/IDER session.\n");
790
791         if (adapter->hw.mac_type >= e1000_82543) {
792                 netdev->features = NETIF_F_SG |
793                                    NETIF_F_HW_CSUM |
794                                    NETIF_F_HW_VLAN_TX |
795                                    NETIF_F_HW_VLAN_RX |
796                                    NETIF_F_HW_VLAN_FILTER;
797                 if (adapter->hw.mac_type == e1000_ich8lan)
798                         netdev->features &= ~NETIF_F_HW_VLAN_FILTER;
799         }
800
801 #ifdef NETIF_F_TSO
802         if ((adapter->hw.mac_type >= e1000_82544) &&
803            (adapter->hw.mac_type != e1000_82547))
804                 netdev->features |= NETIF_F_TSO;
805
806 #ifdef NETIF_F_TSO_IPV6
807         if (adapter->hw.mac_type > e1000_82547_rev_2)
808                 netdev->features |= NETIF_F_TSO_IPV6;
809 #endif
810 #endif
811         if (pci_using_dac)
812                 netdev->features |= NETIF_F_HIGHDMA;
813
814         netdev->features |= NETIF_F_LLTX;
815
816         adapter->en_mng_pt = e1000_enable_mng_pass_thru(&adapter->hw);
817
818         /* initialize eeprom parameters */
819
820         if (e1000_init_eeprom_params(&adapter->hw)) {
821                 E1000_ERR("EEPROM initialization failed\n");
822                 goto err_eeprom;
823         }
824
825         /* before reading the EEPROM, reset the controller to
826          * put the device in a known good starting state */
827
828         e1000_reset_hw(&adapter->hw);
829
830         /* make sure the EEPROM is good */
831
832         if (e1000_validate_eeprom_checksum(&adapter->hw) < 0) {
833                 DPRINTK(PROBE, ERR, "The EEPROM Checksum Is Not Valid\n");
834                 goto err_eeprom;
835         }
836
837         /* copy the MAC address out of the EEPROM */
838
839         if (e1000_read_mac_addr(&adapter->hw))
840                 DPRINTK(PROBE, ERR, "EEPROM Read Error\n");
841         memcpy(netdev->dev_addr, adapter->hw.mac_addr, netdev->addr_len);
842         memcpy(netdev->perm_addr, adapter->hw.mac_addr, netdev->addr_len);
843
844         if (!is_valid_ether_addr(netdev->perm_addr)) {
845                 DPRINTK(PROBE, ERR, "Invalid MAC Address\n");
846                 goto err_eeprom;
847         }
848
849         e1000_get_bus_info(&adapter->hw);
850
851         init_timer(&adapter->tx_fifo_stall_timer);
852         adapter->tx_fifo_stall_timer.function = &e1000_82547_tx_fifo_stall;
853         adapter->tx_fifo_stall_timer.data = (unsigned long) adapter;
854
855         init_timer(&adapter->watchdog_timer);
856         adapter->watchdog_timer.function = &e1000_watchdog;
857         adapter->watchdog_timer.data = (unsigned long) adapter;
858
859         init_timer(&adapter->phy_info_timer);
860         adapter->phy_info_timer.function = &e1000_update_phy_info;
861         adapter->phy_info_timer.data = (unsigned long) adapter;
862
863         INIT_WORK(&adapter->reset_task,
864                 (void (*)(void *))e1000_reset_task, netdev);
865
866         /* we're going to reset, so assume we have no link for now */
867
868         netif_carrier_off(netdev);
869         netif_stop_queue(netdev);
870
871         e1000_check_options(adapter);
872
873         /* Initial Wake on LAN setting
874          * If APM wake is enabled in the EEPROM,
875          * enable the ACPI Magic Packet filter
876          */
877
878         switch (adapter->hw.mac_type) {
879         case e1000_82542_rev2_0:
880         case e1000_82542_rev2_1:
881         case e1000_82543:
882                 break;
883         case e1000_82544:
884                 e1000_read_eeprom(&adapter->hw,
885                         EEPROM_INIT_CONTROL2_REG, 1, &eeprom_data);
886                 eeprom_apme_mask = E1000_EEPROM_82544_APM;
887                 break;
888         case e1000_ich8lan:
889                 e1000_read_eeprom(&adapter->hw,
890                         EEPROM_INIT_CONTROL1_REG, 1, &eeprom_data);
891                 eeprom_apme_mask = E1000_EEPROM_ICH8_APME;
892                 break;
893         case e1000_82546:
894         case e1000_82546_rev_3:
895         case e1000_82571:
896         case e1000_80003es2lan:
897                 if (E1000_READ_REG(&adapter->hw, STATUS) & E1000_STATUS_FUNC_1){
898                         e1000_read_eeprom(&adapter->hw,
899                                 EEPROM_INIT_CONTROL3_PORT_B, 1, &eeprom_data);
900                         break;
901                 }
902                 /* Fall Through */
903         default:
904                 e1000_read_eeprom(&adapter->hw,
905                         EEPROM_INIT_CONTROL3_PORT_A, 1, &eeprom_data);
906                 break;
907         }
908         if (eeprom_data & eeprom_apme_mask)
909                 adapter->eeprom_wol |= E1000_WUFC_MAG;
910
911         /* now that we have the eeprom settings, apply the special cases
912          * where the eeprom may be wrong or the board simply won't support
913          * wake on lan on a particular port */
914         switch (pdev->device) {
915         case E1000_DEV_ID_82546GB_PCIE:
916                 adapter->eeprom_wol = 0;
917                 break;
918         case E1000_DEV_ID_82546EB_FIBER:
919         case E1000_DEV_ID_82546GB_FIBER:
920         case E1000_DEV_ID_82571EB_FIBER:
921                 /* Wake events only supported on port A for dual fiber
922                  * regardless of eeprom setting */
923                 if (E1000_READ_REG(&adapter->hw, STATUS) & E1000_STATUS_FUNC_1)
924                         adapter->eeprom_wol = 0;
925                 break;
926         case E1000_DEV_ID_82546GB_QUAD_COPPER_KSP3:
927         case E1000_DEV_ID_82571EB_QUAD_COPPER:
928                 /* if quad port adapter, disable WoL on all but port A */
929                 if (global_quad_port_a != 0)
930                         adapter->eeprom_wol = 0;
931                 else
932                         adapter->quad_port_a = 1;
933                 /* Reset for multiple quad port adapters */
934                 if (++global_quad_port_a == 4)
935                         global_quad_port_a = 0;
936                 break;
937         }
938
939         /* initialize the wol settings based on the eeprom settings */
940         adapter->wol = adapter->eeprom_wol;
941
942         /* print bus type/speed/width info */
943         {
944         struct e1000_hw *hw = &adapter->hw;
945         DPRINTK(PROBE, INFO, "(PCI%s:%s:%s) ",
946                 ((hw->bus_type == e1000_bus_type_pcix) ? "-X" :
947                  (hw->bus_type == e1000_bus_type_pci_express ? " Express":"")),
948                 ((hw->bus_speed == e1000_bus_speed_2500) ? "2.5Gb/s" :
949                  (hw->bus_speed == e1000_bus_speed_133) ? "133MHz" :
950                  (hw->bus_speed == e1000_bus_speed_120) ? "120MHz" :
951                  (hw->bus_speed == e1000_bus_speed_100) ? "100MHz" :
952                  (hw->bus_speed == e1000_bus_speed_66) ? "66MHz" : "33MHz"),
953                 ((hw->bus_width == e1000_bus_width_64) ? "64-bit" :
954                  (hw->bus_width == e1000_bus_width_pciex_4) ? "Width x4" :
955                  (hw->bus_width == e1000_bus_width_pciex_1) ? "Width x1" :
956                  "32-bit"));
957         }
958
959         for (i = 0; i < 6; i++)
960                 printk("%2.2x%c", netdev->dev_addr[i], i == 5 ? '\n' : ':');
961
962         /* reset the hardware with the new settings */
963         e1000_reset(adapter);
964
965         /* If the controller is 82573 and f/w is AMT, do not set
966          * DRV_LOAD until the interface is up.  For all other cases,
967          * let the f/w know that the h/w is now under the control
968          * of the driver. */
969         if (adapter->hw.mac_type != e1000_82573 ||
970             !e1000_check_mng_mode(&adapter->hw))
971                 e1000_get_hw_control(adapter);
972
973         strcpy(netdev->name, "eth%d");
974         if ((err = register_netdev(netdev)))
975                 goto err_register;
976
977         DPRINTK(PROBE, INFO, "Intel(R) PRO/1000 Network Connection\n");
978
979         cards_found++;
980         return 0;
981
982 err_register:
983         e1000_release_hw_control(adapter);
984 err_eeprom:
985         if (!e1000_check_phy_reset_block(&adapter->hw))
986                 e1000_phy_hw_reset(&adapter->hw);
987
988         if (adapter->hw.flash_address)
989                 iounmap(adapter->hw.flash_address);
990 err_flashmap:
991 #ifdef CONFIG_E1000_NAPI
992         for (i = 0; i < adapter->num_rx_queues; i++)
993                 dev_put(&adapter->polling_netdev[i]);
994 #endif
995
996         kfree(adapter->tx_ring);
997         kfree(adapter->rx_ring);
998 #ifdef CONFIG_E1000_NAPI
999         kfree(adapter->polling_netdev);
1000 #endif
1001 err_sw_init:
1002         iounmap(adapter->hw.hw_addr);
1003 err_ioremap:
1004         free_netdev(netdev);
1005 err_alloc_etherdev:
1006         pci_release_regions(pdev);
1007 err_pci_reg:
1008 err_dma:
1009         pci_disable_device(pdev);
1010         return err;
1011 }
1012
1013 /**
1014  * e1000_remove - Device Removal Routine
1015  * @pdev: PCI device information struct
1016  *
1017  * e1000_remove is called by the PCI subsystem to alert the driver
1018  * that it should release a PCI device.  The could be caused by a
1019  * Hot-Plug event, or because the driver is going to be removed from
1020  * memory.
1021  **/
1022
1023 static void __devexit
1024 e1000_remove(struct pci_dev *pdev)
1025 {
1026         struct net_device *netdev = pci_get_drvdata(pdev);
1027         struct e1000_adapter *adapter = netdev_priv(netdev);
1028         uint32_t manc;
1029 #ifdef CONFIG_E1000_NAPI
1030         int i;
1031 #endif
1032
1033         flush_scheduled_work();
1034
1035         if (adapter->hw.mac_type >= e1000_82540 &&
1036            adapter->hw.mac_type != e1000_ich8lan &&
1037            adapter->hw.media_type == e1000_media_type_copper) {
1038                 manc = E1000_READ_REG(&adapter->hw, MANC);
1039                 if (manc & E1000_MANC_SMBUS_EN) {
1040                         manc |= E1000_MANC_ARP_EN;
1041                         E1000_WRITE_REG(&adapter->hw, MANC, manc);
1042                 }
1043         }
1044
1045         /* Release control of h/w to f/w.  If f/w is AMT enabled, this
1046          * would have already happened in close and is redundant. */
1047         e1000_release_hw_control(adapter);
1048
1049         unregister_netdev(netdev);
1050 #ifdef CONFIG_E1000_NAPI
1051         for (i = 0; i < adapter->num_rx_queues; i++)
1052                 dev_put(&adapter->polling_netdev[i]);
1053 #endif
1054
1055         if (!e1000_check_phy_reset_block(&adapter->hw))
1056                 e1000_phy_hw_reset(&adapter->hw);
1057
1058         kfree(adapter->tx_ring);
1059         kfree(adapter->rx_ring);
1060 #ifdef CONFIG_E1000_NAPI
1061         kfree(adapter->polling_netdev);
1062 #endif
1063
1064         iounmap(adapter->hw.hw_addr);
1065         if (adapter->hw.flash_address)
1066                 iounmap(adapter->hw.flash_address);
1067         pci_release_regions(pdev);
1068
1069         free_netdev(netdev);
1070
1071         pci_disable_device(pdev);
1072 }
1073
1074 /**
1075  * e1000_sw_init - Initialize general software structures (struct e1000_adapter)
1076  * @adapter: board private structure to initialize
1077  *
1078  * e1000_sw_init initializes the Adapter private data structure.
1079  * Fields are initialized based on PCI device information and
1080  * OS network device settings (MTU size).
1081  **/
1082
1083 static int __devinit
1084 e1000_sw_init(struct e1000_adapter *adapter)
1085 {
1086         struct e1000_hw *hw = &adapter->hw;
1087         struct net_device *netdev = adapter->netdev;
1088         struct pci_dev *pdev = adapter->pdev;
1089 #ifdef CONFIG_E1000_NAPI
1090         int i;
1091 #endif
1092
1093         /* PCI config space info */
1094
1095         hw->vendor_id = pdev->vendor;
1096         hw->device_id = pdev->device;
1097         hw->subsystem_vendor_id = pdev->subsystem_vendor;
1098         hw->subsystem_id = pdev->subsystem_device;
1099
1100         pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
1101
1102         pci_read_config_word(pdev, PCI_COMMAND, &hw->pci_cmd_word);
1103
1104         adapter->rx_buffer_len = MAXIMUM_ETHERNET_VLAN_SIZE;
1105         adapter->rx_ps_bsize0 = E1000_RXBUFFER_128;
1106         hw->max_frame_size = netdev->mtu +
1107                              ENET_HEADER_SIZE + ETHERNET_FCS_SIZE;
1108         hw->min_frame_size = MINIMUM_ETHERNET_FRAME_SIZE;
1109
1110         /* identify the MAC */
1111
1112         if (e1000_set_mac_type(hw)) {
1113                 DPRINTK(PROBE, ERR, "Unknown MAC Type\n");
1114                 return -EIO;
1115         }
1116
1117         switch (hw->mac_type) {
1118         default:
1119                 break;
1120         case e1000_82541:
1121         case e1000_82547:
1122         case e1000_82541_rev_2:
1123         case e1000_82547_rev_2:
1124                 hw->phy_init_script = 1;
1125                 break;
1126         }
1127
1128         e1000_set_media_type(hw);
1129
1130         hw->wait_autoneg_complete = FALSE;
1131         hw->tbi_compatibility_en = TRUE;
1132         hw->adaptive_ifs = TRUE;
1133
1134         /* Copper options */
1135
1136         if (hw->media_type == e1000_media_type_copper) {
1137                 hw->mdix = AUTO_ALL_MODES;
1138                 hw->disable_polarity_correction = FALSE;
1139                 hw->master_slave = E1000_MASTER_SLAVE;
1140         }
1141
1142         adapter->num_tx_queues = 1;
1143         adapter->num_rx_queues = 1;
1144
1145         if (e1000_alloc_queues(adapter)) {
1146                 DPRINTK(PROBE, ERR, "Unable to allocate memory for queues\n");
1147                 return -ENOMEM;
1148         }
1149
1150 #ifdef CONFIG_E1000_NAPI
1151         for (i = 0; i < adapter->num_rx_queues; i++) {
1152                 adapter->polling_netdev[i].priv = adapter;
1153                 adapter->polling_netdev[i].poll = &e1000_clean;
1154                 adapter->polling_netdev[i].weight = 64;
1155                 dev_hold(&adapter->polling_netdev[i]);
1156                 set_bit(__LINK_STATE_START, &adapter->polling_netdev[i].state);
1157         }
1158         spin_lock_init(&adapter->tx_queue_lock);
1159 #endif
1160
1161         atomic_set(&adapter->irq_sem, 1);
1162         spin_lock_init(&adapter->stats_lock);
1163
1164         return 0;
1165 }
1166
1167 /**
1168  * e1000_alloc_queues - Allocate memory for all rings
1169  * @adapter: board private structure to initialize
1170  *
1171  * We allocate one ring per queue at run-time since we don't know the
1172  * number of queues at compile-time.  The polling_netdev array is
1173  * intended for Multiqueue, but should work fine with a single queue.
1174  **/
1175
1176 static int __devinit
1177 e1000_alloc_queues(struct e1000_adapter *adapter)
1178 {
1179         int size;
1180
1181         size = sizeof(struct e1000_tx_ring) * adapter->num_tx_queues;
1182         adapter->tx_ring = kmalloc(size, GFP_KERNEL);
1183         if (!adapter->tx_ring)
1184                 return -ENOMEM;
1185         memset(adapter->tx_ring, 0, size);
1186
1187         size = sizeof(struct e1000_rx_ring) * adapter->num_rx_queues;
1188         adapter->rx_ring = kmalloc(size, GFP_KERNEL);
1189         if (!adapter->rx_ring) {
1190                 kfree(adapter->tx_ring);
1191                 return -ENOMEM;
1192         }
1193         memset(adapter->rx_ring, 0, size);
1194
1195 #ifdef CONFIG_E1000_NAPI
1196         size = sizeof(struct net_device) * adapter->num_rx_queues;
1197         adapter->polling_netdev = kmalloc(size, GFP_KERNEL);
1198         if (!adapter->polling_netdev) {
1199                 kfree(adapter->tx_ring);
1200                 kfree(adapter->rx_ring);
1201                 return -ENOMEM;
1202         }
1203         memset(adapter->polling_netdev, 0, size);
1204 #endif
1205
1206         return E1000_SUCCESS;
1207 }
1208
1209 /**
1210  * e1000_open - Called when a network interface is made active
1211  * @netdev: network interface device structure
1212  *
1213  * Returns 0 on success, negative value on failure
1214  *
1215  * The open entry point is called when a network interface is made
1216  * active by the system (IFF_UP).  At this point all resources needed
1217  * for transmit and receive operations are allocated, the interrupt
1218  * handler is registered with the OS, the watchdog timer is started,
1219  * and the stack is notified that the interface is ready.
1220  **/
1221
1222 static int
1223 e1000_open(struct net_device *netdev)
1224 {
1225         struct e1000_adapter *adapter = netdev_priv(netdev);
1226         int err;
1227
1228         /* disallow open during test */
1229         if (test_bit(__E1000_DRIVER_TESTING, &adapter->flags))
1230                 return -EBUSY;
1231
1232         /* allocate transmit descriptors */
1233
1234         if ((err = e1000_setup_all_tx_resources(adapter)))
1235                 goto err_setup_tx;
1236
1237         /* allocate receive descriptors */
1238
1239         if ((err = e1000_setup_all_rx_resources(adapter)))
1240                 goto err_setup_rx;
1241
1242         err = e1000_request_irq(adapter);
1243         if (err)
1244                 goto err_req_irq;
1245
1246         e1000_power_up_phy(adapter);
1247
1248         if ((err = e1000_up(adapter)))
1249                 goto err_up;
1250         adapter->mng_vlan_id = E1000_MNG_VLAN_NONE;
1251         if ((adapter->hw.mng_cookie.status &
1252                           E1000_MNG_DHCP_COOKIE_STATUS_VLAN_SUPPORT)) {
1253                 e1000_update_mng_vlan(adapter);
1254         }
1255
1256         /* If AMT is enabled, let the firmware know that the network
1257          * interface is now open */
1258         if (adapter->hw.mac_type == e1000_82573 &&
1259             e1000_check_mng_mode(&adapter->hw))
1260                 e1000_get_hw_control(adapter);
1261
1262         return E1000_SUCCESS;
1263
1264 err_up:
1265         e1000_power_down_phy(adapter);
1266         e1000_free_irq(adapter);
1267 err_req_irq:
1268         e1000_free_all_rx_resources(adapter);
1269 err_setup_rx:
1270         e1000_free_all_tx_resources(adapter);
1271 err_setup_tx:
1272         e1000_reset(adapter);
1273
1274         return err;
1275 }
1276
1277 /**
1278  * e1000_close - Disables a network interface
1279  * @netdev: network interface device structure
1280  *
1281  * Returns 0, this is not allowed to fail
1282  *
1283  * The close entry point is called when an interface is de-activated
1284  * by the OS.  The hardware is still under the drivers control, but
1285  * needs to be disabled.  A global MAC reset is issued to stop the
1286  * hardware, and all transmit and receive resources are freed.
1287  **/
1288
1289 static int
1290 e1000_close(struct net_device *netdev)
1291 {
1292         struct e1000_adapter *adapter = netdev_priv(netdev);
1293
1294         WARN_ON(test_bit(__E1000_RESETTING, &adapter->flags));
1295         e1000_down(adapter);
1296         e1000_power_down_phy(adapter);
1297         e1000_free_irq(adapter);
1298
1299         e1000_free_all_tx_resources(adapter);
1300         e1000_free_all_rx_resources(adapter);
1301
1302         if ((adapter->hw.mng_cookie.status &
1303                           E1000_MNG_DHCP_COOKIE_STATUS_VLAN_SUPPORT)) {
1304                 e1000_vlan_rx_kill_vid(netdev, adapter->mng_vlan_id);
1305         }
1306
1307         /* If AMT is enabled, let the firmware know that the network
1308          * interface is now closed */
1309         if (adapter->hw.mac_type == e1000_82573 &&
1310             e1000_check_mng_mode(&adapter->hw))
1311                 e1000_release_hw_control(adapter);
1312
1313         return 0;
1314 }
1315
1316 /**
1317  * e1000_check_64k_bound - check that memory doesn't cross 64kB boundary
1318  * @adapter: address of board private structure
1319  * @start: address of beginning of memory
1320  * @len: length of memory
1321  **/
1322 static boolean_t
1323 e1000_check_64k_bound(struct e1000_adapter *adapter,
1324                       void *start, unsigned long len)
1325 {
1326         unsigned long begin = (unsigned long) start;
1327         unsigned long end = begin + len;
1328
1329         /* First rev 82545 and 82546 need to not allow any memory
1330          * write location to cross 64k boundary due to errata 23 */
1331         if (adapter->hw.mac_type == e1000_82545 ||
1332             adapter->hw.mac_type == e1000_82546) {
1333                 return ((begin ^ (end - 1)) >> 16) != 0 ? FALSE : TRUE;
1334         }
1335
1336         return TRUE;
1337 }
1338
1339 /**
1340  * e1000_setup_tx_resources - allocate Tx resources (Descriptors)
1341  * @adapter: board private structure
1342  * @txdr:    tx descriptor ring (for a specific queue) to setup
1343  *
1344  * Return 0 on success, negative on failure
1345  **/
1346
1347 static int
1348 e1000_setup_tx_resources(struct e1000_adapter *adapter,
1349                          struct e1000_tx_ring *txdr)
1350 {
1351         struct pci_dev *pdev = adapter->pdev;
1352         int size;
1353
1354         size = sizeof(struct e1000_buffer) * txdr->count;
1355         txdr->buffer_info = vmalloc(size);
1356         if (!txdr->buffer_info) {
1357                 DPRINTK(PROBE, ERR,
1358                 "Unable to allocate memory for the transmit descriptor ring\n");
1359                 return -ENOMEM;
1360         }
1361         memset(txdr->buffer_info, 0, size);
1362
1363         /* round up to nearest 4K */
1364
1365         txdr->size = txdr->count * sizeof(struct e1000_tx_desc);
1366         E1000_ROUNDUP(txdr->size, 4096);
1367
1368         txdr->desc = pci_alloc_consistent(pdev, txdr->size, &txdr->dma);
1369         if (!txdr->desc) {
1370 setup_tx_desc_die:
1371                 vfree(txdr->buffer_info);
1372                 DPRINTK(PROBE, ERR,
1373                 "Unable to allocate memory for the transmit descriptor ring\n");
1374                 return -ENOMEM;
1375         }
1376
1377         /* Fix for errata 23, can't cross 64kB boundary */
1378         if (!e1000_check_64k_bound(adapter, txdr->desc, txdr->size)) {
1379                 void *olddesc = txdr->desc;
1380                 dma_addr_t olddma = txdr->dma;
1381                 DPRINTK(TX_ERR, ERR, "txdr align check failed: %u bytes "
1382                                      "at %p\n", txdr->size, txdr->desc);
1383                 /* Try again, without freeing the previous */
1384                 txdr->desc = pci_alloc_consistent(pdev, txdr->size, &txdr->dma);
1385                 /* Failed allocation, critical failure */
1386                 if (!txdr->desc) {
1387                         pci_free_consistent(pdev, txdr->size, olddesc, olddma);
1388                         goto setup_tx_desc_die;
1389                 }
1390
1391                 if (!e1000_check_64k_bound(adapter, txdr->desc, txdr->size)) {
1392                         /* give up */
1393                         pci_free_consistent(pdev, txdr->size, txdr->desc,
1394                                             txdr->dma);
1395                         pci_free_consistent(pdev, txdr->size, olddesc, olddma);
1396                         DPRINTK(PROBE, ERR,
1397                                 "Unable to allocate aligned memory "
1398                                 "for the transmit descriptor ring\n");
1399                         vfree(txdr->buffer_info);
1400                         return -ENOMEM;
1401                 } else {
1402                         /* Free old allocation, new allocation was successful */
1403                         pci_free_consistent(pdev, txdr->size, olddesc, olddma);
1404                 }
1405         }
1406         memset(txdr->desc, 0, txdr->size);
1407
1408         txdr->next_to_use = 0;
1409         txdr->next_to_clean = 0;
1410         spin_lock_init(&txdr->tx_lock);
1411
1412         return 0;
1413 }
1414
1415 /**
1416  * e1000_setup_all_tx_resources - wrapper to allocate Tx resources
1417  *                                (Descriptors) for all queues
1418  * @adapter: board private structure
1419  *
1420  * Return 0 on success, negative on failure
1421  **/
1422
1423 int
1424 e1000_setup_all_tx_resources(struct e1000_adapter *adapter)
1425 {
1426         int i, err = 0;
1427
1428         for (i = 0; i < adapter->num_tx_queues; i++) {
1429                 err = e1000_setup_tx_resources(adapter, &adapter->tx_ring[i]);
1430                 if (err) {
1431                         DPRINTK(PROBE, ERR,
1432                                 "Allocation for Tx Queue %u failed\n", i);
1433                         for (i-- ; i >= 0; i--)
1434                                 e1000_free_tx_resources(adapter,
1435                                                         &adapter->tx_ring[i]);
1436                         break;
1437                 }
1438         }
1439
1440         return err;
1441 }
1442
1443 /**
1444  * e1000_configure_tx - Configure 8254x Transmit Unit after Reset
1445  * @adapter: board private structure
1446  *
1447  * Configure the Tx unit of the MAC after a reset.
1448  **/
1449
1450 static void
1451 e1000_configure_tx(struct e1000_adapter *adapter)
1452 {
1453         uint64_t tdba;
1454         struct e1000_hw *hw = &adapter->hw;
1455         uint32_t tdlen, tctl, tipg, tarc;
1456         uint32_t ipgr1, ipgr2;
1457
1458         /* Setup the HW Tx Head and Tail descriptor pointers */
1459
1460         switch (adapter->num_tx_queues) {
1461         case 1:
1462         default:
1463                 tdba = adapter->tx_ring[0].dma;
1464                 tdlen = adapter->tx_ring[0].count *
1465                         sizeof(struct e1000_tx_desc);
1466                 E1000_WRITE_REG(hw, TDLEN, tdlen);
1467                 E1000_WRITE_REG(hw, TDBAH, (tdba >> 32));
1468                 E1000_WRITE_REG(hw, TDBAL, (tdba & 0x00000000ffffffffULL));
1469                 E1000_WRITE_REG(hw, TDT, 0);
1470                 E1000_WRITE_REG(hw, TDH, 0);
1471                 adapter->tx_ring[0].tdh = ((hw->mac_type >= e1000_82543) ? E1000_TDH : E1000_82542_TDH);
1472                 adapter->tx_ring[0].tdt = ((hw->mac_type >= e1000_82543) ? E1000_TDT : E1000_82542_TDT);
1473                 break;
1474         }
1475
1476         /* Set the default values for the Tx Inter Packet Gap timer */
1477
1478         if (hw->media_type == e1000_media_type_fiber ||
1479             hw->media_type == e1000_media_type_internal_serdes)
1480                 tipg = DEFAULT_82543_TIPG_IPGT_FIBER;
1481         else
1482                 tipg = DEFAULT_82543_TIPG_IPGT_COPPER;
1483
1484         switch (hw->mac_type) {
1485         case e1000_82542_rev2_0:
1486         case e1000_82542_rev2_1:
1487                 tipg = DEFAULT_82542_TIPG_IPGT;
1488                 ipgr1 = DEFAULT_82542_TIPG_IPGR1;
1489                 ipgr2 = DEFAULT_82542_TIPG_IPGR2;
1490                 break;
1491         case e1000_80003es2lan:
1492                 ipgr1 = DEFAULT_82543_TIPG_IPGR1;
1493                 ipgr2 = DEFAULT_80003ES2LAN_TIPG_IPGR2;
1494                 break;
1495         default:
1496                 ipgr1 = DEFAULT_82543_TIPG_IPGR1;
1497                 ipgr2 = DEFAULT_82543_TIPG_IPGR2;
1498                 break;
1499         }
1500         tipg |= ipgr1 << E1000_TIPG_IPGR1_SHIFT;
1501         tipg |= ipgr2 << E1000_TIPG_IPGR2_SHIFT;
1502         E1000_WRITE_REG(hw, TIPG, tipg);
1503
1504         /* Set the Tx Interrupt Delay register */
1505
1506         E1000_WRITE_REG(hw, TIDV, adapter->tx_int_delay);
1507         if (hw->mac_type >= e1000_82540)
1508                 E1000_WRITE_REG(hw, TADV, adapter->tx_abs_int_delay);
1509
1510         /* Program the Transmit Control Register */
1511
1512         tctl = E1000_READ_REG(hw, TCTL);
1513
1514         tctl &= ~E1000_TCTL_CT;
1515         tctl |= E1000_TCTL_PSP | E1000_TCTL_RTLC |
1516                 (E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT);
1517
1518 #ifdef DISABLE_MULR
1519         /* disable Multiple Reads for debugging */
1520         tctl &= ~E1000_TCTL_MULR;
1521 #endif
1522
1523         if (hw->mac_type == e1000_82571 || hw->mac_type == e1000_82572) {
1524                 tarc = E1000_READ_REG(hw, TARC0);
1525                 tarc |= ((1 << 25) | (1 << 21));
1526                 E1000_WRITE_REG(hw, TARC0, tarc);
1527                 tarc = E1000_READ_REG(hw, TARC1);
1528                 tarc |= (1 << 25);
1529                 if (tctl & E1000_TCTL_MULR)
1530                         tarc &= ~(1 << 28);
1531                 else
1532                         tarc |= (1 << 28);
1533                 E1000_WRITE_REG(hw, TARC1, tarc);
1534         } else if (hw->mac_type == e1000_80003es2lan) {
1535                 tarc = E1000_READ_REG(hw, TARC0);
1536                 tarc |= 1;
1537                 E1000_WRITE_REG(hw, TARC0, tarc);
1538                 tarc = E1000_READ_REG(hw, TARC1);
1539                 tarc |= 1;
1540                 E1000_WRITE_REG(hw, TARC1, tarc);
1541         }
1542
1543         e1000_config_collision_dist(hw);
1544
1545         /* Setup Transmit Descriptor Settings for eop descriptor */
1546         adapter->txd_cmd = E1000_TXD_CMD_IDE | E1000_TXD_CMD_EOP |
1547                 E1000_TXD_CMD_IFCS;
1548
1549         if (hw->mac_type < e1000_82543)
1550                 adapter->txd_cmd |= E1000_TXD_CMD_RPS;
1551         else
1552                 adapter->txd_cmd |= E1000_TXD_CMD_RS;
1553
1554         /* Cache if we're 82544 running in PCI-X because we'll
1555          * need this to apply a workaround later in the send path. */
1556         if (hw->mac_type == e1000_82544 &&
1557             hw->bus_type == e1000_bus_type_pcix)
1558                 adapter->pcix_82544 = 1;
1559
1560         E1000_WRITE_REG(hw, TCTL, tctl);
1561
1562 }
1563
1564 /**
1565  * e1000_setup_rx_resources - allocate Rx resources (Descriptors)
1566  * @adapter: board private structure
1567  * @rxdr:    rx descriptor ring (for a specific queue) to setup
1568  *
1569  * Returns 0 on success, negative on failure
1570  **/
1571
1572 static int
1573 e1000_setup_rx_resources(struct e1000_adapter *adapter,
1574                          struct e1000_rx_ring *rxdr)
1575 {
1576         struct pci_dev *pdev = adapter->pdev;
1577         int size, desc_len;
1578
1579         size = sizeof(struct e1000_buffer) * rxdr->count;
1580         rxdr->buffer_info = vmalloc(size);
1581         if (!rxdr->buffer_info) {
1582                 DPRINTK(PROBE, ERR,
1583                 "Unable to allocate memory for the receive descriptor ring\n");
1584                 return -ENOMEM;
1585         }
1586         memset(rxdr->buffer_info, 0, size);
1587
1588         size = sizeof(struct e1000_ps_page) * rxdr->count;
1589         rxdr->ps_page = kmalloc(size, GFP_KERNEL);
1590         if (!rxdr->ps_page) {
1591                 vfree(rxdr->buffer_info);
1592                 DPRINTK(PROBE, ERR,
1593                 "Unable to allocate memory for the receive descriptor ring\n");
1594                 return -ENOMEM;
1595         }
1596         memset(rxdr->ps_page, 0, size);
1597
1598         size = sizeof(struct e1000_ps_page_dma) * rxdr->count;
1599         rxdr->ps_page_dma = kmalloc(size, GFP_KERNEL);
1600         if (!rxdr->ps_page_dma) {
1601                 vfree(rxdr->buffer_info);
1602                 kfree(rxdr->ps_page);
1603                 DPRINTK(PROBE, ERR,
1604                 "Unable to allocate memory for the receive descriptor ring\n");
1605                 return -ENOMEM;
1606         }
1607         memset(rxdr->ps_page_dma, 0, size);
1608
1609         if (adapter->hw.mac_type <= e1000_82547_rev_2)
1610                 desc_len = sizeof(struct e1000_rx_desc);
1611         else
1612                 desc_len = sizeof(union e1000_rx_desc_packet_split);
1613
1614         /* Round up to nearest 4K */
1615
1616         rxdr->size = rxdr->count * desc_len;
1617         E1000_ROUNDUP(rxdr->size, 4096);
1618
1619         rxdr->desc = pci_alloc_consistent(pdev, rxdr->size, &rxdr->dma);
1620
1621         if (!rxdr->desc) {
1622                 DPRINTK(PROBE, ERR,
1623                 "Unable to allocate memory for the receive descriptor ring\n");
1624 setup_rx_desc_die:
1625                 vfree(rxdr->buffer_info);
1626                 kfree(rxdr->ps_page);
1627                 kfree(rxdr->ps_page_dma);
1628                 return -ENOMEM;
1629         }
1630
1631         /* Fix for errata 23, can't cross 64kB boundary */
1632         if (!e1000_check_64k_bound(adapter, rxdr->desc, rxdr->size)) {
1633                 void *olddesc = rxdr->desc;
1634                 dma_addr_t olddma = rxdr->dma;
1635                 DPRINTK(RX_ERR, ERR, "rxdr align check failed: %u bytes "
1636                                      "at %p\n", rxdr->size, rxdr->desc);
1637                 /* Try again, without freeing the previous */
1638                 rxdr->desc = pci_alloc_consistent(pdev, rxdr->size, &rxdr->dma);
1639                 /* Failed allocation, critical failure */
1640                 if (!rxdr->desc) {
1641                         pci_free_consistent(pdev, rxdr->size, olddesc, olddma);
1642                         DPRINTK(PROBE, ERR,
1643                                 "Unable to allocate memory "
1644                                 "for the receive descriptor ring\n");
1645                         goto setup_rx_desc_die;
1646                 }
1647
1648                 if (!e1000_check_64k_bound(adapter, rxdr->desc, rxdr->size)) {
1649                         /* give up */
1650                         pci_free_consistent(pdev, rxdr->size, rxdr->desc,
1651                                             rxdr->dma);
1652                         pci_free_consistent(pdev, rxdr->size, olddesc, olddma);
1653                         DPRINTK(PROBE, ERR,
1654                                 "Unable to allocate aligned memory "
1655                                 "for the receive descriptor ring\n");
1656                         goto setup_rx_desc_die;
1657                 } else {
1658                         /* Free old allocation, new allocation was successful */
1659                         pci_free_consistent(pdev, rxdr->size, olddesc, olddma);
1660                 }
1661         }
1662         memset(rxdr->desc, 0, rxdr->size);
1663
1664         rxdr->next_to_clean = 0;
1665         rxdr->next_to_use = 0;
1666
1667         return 0;
1668 }
1669
1670 /**
1671  * e1000_setup_all_rx_resources - wrapper to allocate Rx resources
1672  *                                (Descriptors) for all queues
1673  * @adapter: board private structure
1674  *
1675  * Return 0 on success, negative on failure
1676  **/
1677
1678 int
1679 e1000_setup_all_rx_resources(struct e1000_adapter *adapter)
1680 {
1681         int i, err = 0;
1682
1683         for (i = 0; i < adapter->num_rx_queues; i++) {
1684                 err = e1000_setup_rx_resources(adapter, &adapter->rx_ring[i]);
1685                 if (err) {
1686                         DPRINTK(PROBE, ERR,
1687                                 "Allocation for Rx Queue %u failed\n", i);
1688                         for (i-- ; i >= 0; i--)
1689                                 e1000_free_rx_resources(adapter,
1690                                                         &adapter->rx_ring[i]);
1691                         break;
1692                 }
1693         }
1694
1695         return err;
1696 }
1697
1698 /**
1699  * e1000_setup_rctl - configure the receive control registers
1700  * @adapter: Board private structure
1701  **/
1702 #define PAGE_USE_COUNT(S) (((S) >> PAGE_SHIFT) + \
1703                         (((S) & (PAGE_SIZE - 1)) ? 1 : 0))
1704 static void
1705 e1000_setup_rctl(struct e1000_adapter *adapter)
1706 {
1707         uint32_t rctl, rfctl;
1708         uint32_t psrctl = 0;
1709 #ifndef CONFIG_E1000_DISABLE_PACKET_SPLIT
1710         uint32_t pages = 0;
1711 #endif
1712
1713         rctl = E1000_READ_REG(&adapter->hw, RCTL);
1714
1715         rctl &= ~(3 << E1000_RCTL_MO_SHIFT);
1716
1717         rctl |= E1000_RCTL_EN | E1000_RCTL_BAM |
1718                 E1000_RCTL_LBM_NO | E1000_RCTL_RDMTS_HALF |
1719                 (adapter->hw.mc_filter_type << E1000_RCTL_MO_SHIFT);
1720
1721         if (adapter->hw.tbi_compatibility_on == 1)
1722                 rctl |= E1000_RCTL_SBP;
1723         else
1724                 rctl &= ~E1000_RCTL_SBP;
1725
1726         if (adapter->netdev->mtu <= ETH_DATA_LEN)
1727                 rctl &= ~E1000_RCTL_LPE;
1728         else
1729                 rctl |= E1000_RCTL_LPE;
1730
1731         /* Setup buffer sizes */
1732         rctl &= ~E1000_RCTL_SZ_4096;
1733         rctl |= E1000_RCTL_BSEX;
1734         switch (adapter->rx_buffer_len) {
1735                 case E1000_RXBUFFER_256:
1736                         rctl |= E1000_RCTL_SZ_256;
1737                         rctl &= ~E1000_RCTL_BSEX;
1738                         break;
1739                 case E1000_RXBUFFER_512:
1740                         rctl |= E1000_RCTL_SZ_512;
1741                         rctl &= ~E1000_RCTL_BSEX;
1742                         break;
1743                 case E1000_RXBUFFER_1024:
1744                         rctl |= E1000_RCTL_SZ_1024;
1745                         rctl &= ~E1000_RCTL_BSEX;
1746                         break;
1747                 case E1000_RXBUFFER_2048:
1748                 default:
1749                         rctl |= E1000_RCTL_SZ_2048;
1750                         rctl &= ~E1000_RCTL_BSEX;
1751                         break;
1752                 case E1000_RXBUFFER_4096:
1753                         rctl |= E1000_RCTL_SZ_4096;
1754                         break;
1755                 case E1000_RXBUFFER_8192:
1756                         rctl |= E1000_RCTL_SZ_8192;
1757                         break;
1758                 case E1000_RXBUFFER_16384:
1759                         rctl |= E1000_RCTL_SZ_16384;
1760                         break;
1761         }
1762
1763 #ifndef CONFIG_E1000_DISABLE_PACKET_SPLIT
1764         /* 82571 and greater support packet-split where the protocol
1765          * header is placed in skb->data and the packet data is
1766          * placed in pages hanging off of skb_shinfo(skb)->nr_frags.
1767          * In the case of a non-split, skb->data is linearly filled,
1768          * followed by the page buffers.  Therefore, skb->data is
1769          * sized to hold the largest protocol header.
1770          */
1771         pages = PAGE_USE_COUNT(adapter->netdev->mtu);
1772         if ((adapter->hw.mac_type > e1000_82547_rev_2) && (pages <= 3) &&
1773             PAGE_SIZE <= 16384)
1774                 adapter->rx_ps_pages = pages;
1775         else
1776                 adapter->rx_ps_pages = 0;
1777 #endif
1778         if (adapter->rx_ps_pages) {
1779                 /* Configure extra packet-split registers */
1780                 rfctl = E1000_READ_REG(&adapter->hw, RFCTL);
1781                 rfctl |= E1000_RFCTL_EXTEN;
1782                 /* disable IPv6 packet split support */
1783                 rfctl |= E1000_RFCTL_IPV6_DIS;
1784                 E1000_WRITE_REG(&adapter->hw, RFCTL, rfctl);
1785
1786                 rctl |= E1000_RCTL_DTYP_PS;
1787
1788                 psrctl |= adapter->rx_ps_bsize0 >>
1789                         E1000_PSRCTL_BSIZE0_SHIFT;
1790
1791                 switch (adapter->rx_ps_pages) {
1792                 case 3:
1793                         psrctl |= PAGE_SIZE <<
1794                                 E1000_PSRCTL_BSIZE3_SHIFT;
1795                 case 2:
1796                         psrctl |= PAGE_SIZE <<
1797                                 E1000_PSRCTL_BSIZE2_SHIFT;
1798                 case 1:
1799                         psrctl |= PAGE_SIZE >>
1800                                 E1000_PSRCTL_BSIZE1_SHIFT;
1801                         break;
1802                 }
1803
1804                 E1000_WRITE_REG(&adapter->hw, PSRCTL, psrctl);
1805         }
1806
1807         E1000_WRITE_REG(&adapter->hw, RCTL, rctl);
1808 }
1809
1810 /**
1811  * e1000_configure_rx - Configure 8254x Receive Unit after Reset
1812  * @adapter: board private structure
1813  *
1814  * Configure the Rx unit of the MAC after a reset.
1815  **/
1816
1817 static void
1818 e1000_configure_rx(struct e1000_adapter *adapter)
1819 {
1820         uint64_t rdba;
1821         struct e1000_hw *hw = &adapter->hw;
1822         uint32_t rdlen, rctl, rxcsum, ctrl_ext;
1823
1824         if (adapter->rx_ps_pages) {
1825                 /* this is a 32 byte descriptor */
1826                 rdlen = adapter->rx_ring[0].count *
1827                         sizeof(union e1000_rx_desc_packet_split);
1828                 adapter->clean_rx = e1000_clean_rx_irq_ps;
1829                 adapter->alloc_rx_buf = e1000_alloc_rx_buffers_ps;
1830         } else {
1831                 rdlen = adapter->rx_ring[0].count *
1832                         sizeof(struct e1000_rx_desc);
1833                 adapter->clean_rx = e1000_clean_rx_irq;
1834                 adapter->alloc_rx_buf = e1000_alloc_rx_buffers;
1835         }
1836
1837         /* disable receives while setting up the descriptors */
1838         rctl = E1000_READ_REG(hw, RCTL);
1839         E1000_WRITE_REG(hw, RCTL, rctl & ~E1000_RCTL_EN);
1840
1841         /* set the Receive Delay Timer Register */
1842         E1000_WRITE_REG(hw, RDTR, adapter->rx_int_delay);
1843
1844         if (hw->mac_type >= e1000_82540) {
1845                 E1000_WRITE_REG(hw, RADV, adapter->rx_abs_int_delay);
1846                 if (adapter->itr > 1)
1847                         E1000_WRITE_REG(hw, ITR,
1848                                 1000000000 / (adapter->itr * 256));
1849         }
1850
1851         if (hw->mac_type >= e1000_82571) {
1852                 ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
1853                 /* Reset delay timers after every interrupt */
1854                 ctrl_ext |= E1000_CTRL_EXT_INT_TIMER_CLR;
1855 #ifdef CONFIG_E1000_NAPI
1856                 /* Auto-Mask interrupts upon ICR read. */
1857                 ctrl_ext |= E1000_CTRL_EXT_IAME;
1858 #endif
1859                 E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
1860                 E1000_WRITE_REG(hw, IAM, ~0);
1861                 E1000_WRITE_FLUSH(hw);
1862         }
1863
1864         /* Setup the HW Rx Head and Tail Descriptor Pointers and
1865          * the Base and Length of the Rx Descriptor Ring */
1866         switch (adapter->num_rx_queues) {
1867         case 1:
1868         default:
1869                 rdba = adapter->rx_ring[0].dma;
1870                 E1000_WRITE_REG(hw, RDLEN, rdlen);
1871                 E1000_WRITE_REG(hw, RDBAH, (rdba >> 32));
1872                 E1000_WRITE_REG(hw, RDBAL, (rdba & 0x00000000ffffffffULL));
1873                 E1000_WRITE_REG(hw, RDT, 0);
1874                 E1000_WRITE_REG(hw, RDH, 0);
1875                 adapter->rx_ring[0].rdh = ((hw->mac_type >= e1000_82543) ? E1000_RDH : E1000_82542_RDH);
1876                 adapter->rx_ring[0].rdt = ((hw->mac_type >= e1000_82543) ? E1000_RDT : E1000_82542_RDT);
1877                 break;
1878         }
1879
1880         /* Enable 82543 Receive Checksum Offload for TCP and UDP */
1881         if (hw->mac_type >= e1000_82543) {
1882                 rxcsum = E1000_READ_REG(hw, RXCSUM);
1883                 if (adapter->rx_csum == TRUE) {
1884                         rxcsum |= E1000_RXCSUM_TUOFL;
1885
1886                         /* Enable 82571 IPv4 payload checksum for UDP fragments
1887                          * Must be used in conjunction with packet-split. */
1888                         if ((hw->mac_type >= e1000_82571) &&
1889                             (adapter->rx_ps_pages)) {
1890                                 rxcsum |= E1000_RXCSUM_IPPCSE;
1891                         }
1892                 } else {
1893                         rxcsum &= ~E1000_RXCSUM_TUOFL;
1894                         /* don't need to clear IPPCSE as it defaults to 0 */
1895                 }
1896                 E1000_WRITE_REG(hw, RXCSUM, rxcsum);
1897         }
1898
1899         /* Enable Receives */
1900         E1000_WRITE_REG(hw, RCTL, rctl);
1901 }
1902
1903 /**
1904  * e1000_free_tx_resources - Free Tx Resources per Queue
1905  * @adapter: board private structure
1906  * @tx_ring: Tx descriptor ring for a specific queue
1907  *
1908  * Free all transmit software resources
1909  **/
1910
1911 static void
1912 e1000_free_tx_resources(struct e1000_adapter *adapter,
1913                         struct e1000_tx_ring *tx_ring)
1914 {
1915         struct pci_dev *pdev = adapter->pdev;
1916
1917         e1000_clean_tx_ring(adapter, tx_ring);
1918
1919         vfree(tx_ring->buffer_info);
1920         tx_ring->buffer_info = NULL;
1921
1922         pci_free_consistent(pdev, tx_ring->size, tx_ring->desc, tx_ring->dma);
1923
1924         tx_ring->desc = NULL;
1925 }
1926
1927 /**
1928  * e1000_free_all_tx_resources - Free Tx Resources for All Queues
1929  * @adapter: board private structure
1930  *
1931  * Free all transmit software resources
1932  **/
1933
1934 void
1935 e1000_free_all_tx_resources(struct e1000_adapter *adapter)
1936 {
1937         int i;
1938
1939         for (i = 0; i < adapter->num_tx_queues; i++)
1940                 e1000_free_tx_resources(adapter, &adapter->tx_ring[i]);
1941 }
1942
1943 static void
1944 e1000_unmap_and_free_tx_resource(struct e1000_adapter *adapter,
1945                         struct e1000_buffer *buffer_info)
1946 {
1947         if (buffer_info->dma) {
1948                 pci_unmap_page(adapter->pdev,
1949                                 buffer_info->dma,
1950                                 buffer_info->length,
1951                                 PCI_DMA_TODEVICE);
1952         }
1953         if (buffer_info->skb)
1954                 dev_kfree_skb_any(buffer_info->skb);
1955         memset(buffer_info, 0, sizeof(struct e1000_buffer));
1956 }
1957
1958 /**
1959  * e1000_clean_tx_ring - Free Tx Buffers
1960  * @adapter: board private structure
1961  * @tx_ring: ring to be cleaned
1962  **/
1963
1964 static void
1965 e1000_clean_tx_ring(struct e1000_adapter *adapter,
1966                     struct e1000_tx_ring *tx_ring)
1967 {
1968         struct e1000_buffer *buffer_info;
1969         unsigned long size;
1970         unsigned int i;
1971
1972         /* Free all the Tx ring sk_buffs */
1973
1974         for (i = 0; i < tx_ring->count; i++) {
1975                 buffer_info = &tx_ring->buffer_info[i];
1976                 e1000_unmap_and_free_tx_resource(adapter, buffer_info);
1977         }
1978
1979         size = sizeof(struct e1000_buffer) * tx_ring->count;
1980         memset(tx_ring->buffer_info, 0, size);
1981
1982         /* Zero out the descriptor ring */
1983
1984         memset(tx_ring->desc, 0, tx_ring->size);
1985
1986         tx_ring->next_to_use = 0;
1987         tx_ring->next_to_clean = 0;
1988         tx_ring->last_tx_tso = 0;
1989
1990         writel(0, adapter->hw.hw_addr + tx_ring->tdh);
1991         writel(0, adapter->hw.hw_addr + tx_ring->tdt);
1992 }
1993
1994 /**
1995  * e1000_clean_all_tx_rings - Free Tx Buffers for all queues
1996  * @adapter: board private structure
1997  **/
1998
1999 static void
2000 e1000_clean_all_tx_rings(struct e1000_adapter *adapter)
2001 {
2002         int i;
2003
2004         for (i = 0; i < adapter->num_tx_queues; i++)
2005                 e1000_clean_tx_ring(adapter, &adapter->tx_ring[i]);
2006 }
2007
2008 /**
2009  * e1000_free_rx_resources - Free Rx Resources
2010  * @adapter: board private structure
2011  * @rx_ring: ring to clean the resources from
2012  *
2013  * Free all receive software resources
2014  **/
2015
2016 static void
2017 e1000_free_rx_resources(struct e1000_adapter *adapter,
2018                         struct e1000_rx_ring *rx_ring)
2019 {
2020         struct pci_dev *pdev = adapter->pdev;
2021
2022         e1000_clean_rx_ring(adapter, rx_ring);
2023
2024         vfree(rx_ring->buffer_info);
2025         rx_ring->buffer_info = NULL;
2026         kfree(rx_ring->ps_page);
2027         rx_ring->ps_page = NULL;
2028         kfree(rx_ring->ps_page_dma);
2029         rx_ring->ps_page_dma = NULL;
2030
2031         pci_free_consistent(pdev, rx_ring->size, rx_ring->desc, rx_ring->dma);
2032
2033         rx_ring->desc = NULL;
2034 }
2035
2036 /**
2037  * e1000_free_all_rx_resources - Free Rx Resources for All Queues
2038  * @adapter: board private structure
2039  *
2040  * Free all receive software resources
2041  **/
2042
2043 void
2044 e1000_free_all_rx_resources(struct e1000_adapter *adapter)
2045 {
2046         int i;
2047
2048         for (i = 0; i < adapter->num_rx_queues; i++)
2049                 e1000_free_rx_resources(adapter, &adapter->rx_ring[i]);
2050 }
2051
2052 /**
2053  * e1000_clean_rx_ring - Free Rx Buffers per Queue
2054  * @adapter: board private structure
2055  * @rx_ring: ring to free buffers from
2056  **/
2057
2058 static void
2059 e1000_clean_rx_ring(struct e1000_adapter *adapter,
2060                     struct e1000_rx_ring *rx_ring)
2061 {
2062         struct e1000_buffer *buffer_info;
2063         struct e1000_ps_page *ps_page;
2064         struct e1000_ps_page_dma *ps_page_dma;
2065         struct pci_dev *pdev = adapter->pdev;
2066         unsigned long size;
2067         unsigned int i, j;
2068
2069         /* Free all the Rx ring sk_buffs */
2070         for (i = 0; i < rx_ring->count; i++) {
2071                 buffer_info = &rx_ring->buffer_info[i];
2072                 if (buffer_info->skb) {
2073                         pci_unmap_single(pdev,
2074                                          buffer_info->dma,
2075                                          buffer_info->length,
2076                                          PCI_DMA_FROMDEVICE);
2077
2078                         dev_kfree_skb(buffer_info->skb);
2079                         buffer_info->skb = NULL;
2080                 }
2081                 ps_page = &rx_ring->ps_page[i];
2082                 ps_page_dma = &rx_ring->ps_page_dma[i];
2083                 for (j = 0; j < adapter->rx_ps_pages; j++) {
2084                         if (!ps_page->ps_page[j]) break;
2085                         pci_unmap_page(pdev,
2086                                        ps_page_dma->ps_page_dma[j],
2087                                        PAGE_SIZE, PCI_DMA_FROMDEVICE);
2088                         ps_page_dma->ps_page_dma[j] = 0;
2089                         put_page(ps_page->ps_page[j]);
2090                         ps_page->ps_page[j] = NULL;
2091                 }
2092         }
2093
2094         size = sizeof(struct e1000_buffer) * rx_ring->count;
2095         memset(rx_ring->buffer_info, 0, size);
2096         size = sizeof(struct e1000_ps_page) * rx_ring->count;
2097         memset(rx_ring->ps_page, 0, size);
2098         size = sizeof(struct e1000_ps_page_dma) * rx_ring->count;
2099         memset(rx_ring->ps_page_dma, 0, size);
2100
2101         /* Zero out the descriptor ring */
2102
2103         memset(rx_ring->desc, 0, rx_ring->size);
2104
2105         rx_ring->next_to_clean = 0;
2106         rx_ring->next_to_use = 0;
2107
2108         writel(0, adapter->hw.hw_addr + rx_ring->rdh);
2109         writel(0, adapter->hw.hw_addr + rx_ring->rdt);
2110 }
2111
2112 /**
2113  * e1000_clean_all_rx_rings - Free Rx Buffers for all queues
2114  * @adapter: board private structure
2115  **/
2116
2117 static void
2118 e1000_clean_all_rx_rings(struct e1000_adapter *adapter)
2119 {
2120         int i;
2121
2122         for (i = 0; i < adapter->num_rx_queues; i++)
2123                 e1000_clean_rx_ring(adapter, &adapter->rx_ring[i]);
2124 }
2125
2126 /* The 82542 2.0 (revision 2) needs to have the receive unit in reset
2127  * and memory write and invalidate disabled for certain operations
2128  */
2129 static void
2130 e1000_enter_82542_rst(struct e1000_adapter *adapter)
2131 {
2132         struct net_device *netdev = adapter->netdev;
2133         uint32_t rctl;
2134
2135         e1000_pci_clear_mwi(&adapter->hw);
2136
2137         rctl = E1000_READ_REG(&adapter->hw, RCTL);
2138         rctl |= E1000_RCTL_RST;
2139         E1000_WRITE_REG(&adapter->hw, RCTL, rctl);
2140         E1000_WRITE_FLUSH(&adapter->hw);
2141         mdelay(5);
2142
2143         if (netif_running(netdev))
2144                 e1000_clean_all_rx_rings(adapter);
2145 }
2146
2147 static void
2148 e1000_leave_82542_rst(struct e1000_adapter *adapter)
2149 {
2150         struct net_device *netdev = adapter->netdev;
2151         uint32_t rctl;
2152
2153         rctl = E1000_READ_REG(&adapter->hw, RCTL);
2154         rctl &= ~E1000_RCTL_RST;
2155         E1000_WRITE_REG(&adapter->hw, RCTL, rctl);
2156         E1000_WRITE_FLUSH(&adapter->hw);
2157         mdelay(5);
2158
2159         if (adapter->hw.pci_cmd_word & PCI_COMMAND_INVALIDATE)
2160                 e1000_pci_set_mwi(&adapter->hw);
2161
2162         if (netif_running(netdev)) {
2163                 /* No need to loop, because 82542 supports only 1 queue */
2164                 struct e1000_rx_ring *ring = &adapter->rx_ring[0];
2165                 e1000_configure_rx(adapter);
2166                 adapter->alloc_rx_buf(adapter, ring, E1000_DESC_UNUSED(ring));
2167         }
2168 }
2169
2170 /**
2171  * e1000_set_mac - Change the Ethernet Address of the NIC
2172  * @netdev: network interface device structure
2173  * @p: pointer to an address structure
2174  *
2175  * Returns 0 on success, negative on failure
2176  **/
2177
2178 static int
2179 e1000_set_mac(struct net_device *netdev, void *p)
2180 {
2181         struct e1000_adapter *adapter = netdev_priv(netdev);
2182         struct sockaddr *addr = p;
2183
2184         if (!is_valid_ether_addr(addr->sa_data))
2185                 return -EADDRNOTAVAIL;
2186
2187         /* 82542 2.0 needs to be in reset to write receive address registers */
2188
2189         if (adapter->hw.mac_type == e1000_82542_rev2_0)
2190                 e1000_enter_82542_rst(adapter);
2191
2192         memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
2193         memcpy(adapter->hw.mac_addr, addr->sa_data, netdev->addr_len);
2194
2195         e1000_rar_set(&adapter->hw, adapter->hw.mac_addr, 0);
2196
2197         /* With 82571 controllers, LAA may be overwritten (with the default)
2198          * due to controller reset from the other port. */
2199         if (adapter->hw.mac_type == e1000_82571) {
2200                 /* activate the work around */
2201                 adapter->hw.laa_is_present = 1;
2202
2203                 /* Hold a copy of the LAA in RAR[14] This is done so that
2204                  * between the time RAR[0] gets clobbered  and the time it
2205                  * gets fixed (in e1000_watchdog), the actual LAA is in one
2206                  * of the RARs and no incoming packets directed to this port
2207                  * are dropped. Eventaully the LAA will be in RAR[0] and
2208                  * RAR[14] */
2209                 e1000_rar_set(&adapter->hw, adapter->hw.mac_addr,
2210                                         E1000_RAR_ENTRIES - 1);
2211         }
2212
2213         if (adapter->hw.mac_type == e1000_82542_rev2_0)
2214                 e1000_leave_82542_rst(adapter);
2215
2216         return 0;
2217 }
2218
2219 /**
2220  * e1000_set_multi - Multicast and Promiscuous mode set
2221  * @netdev: network interface device structure
2222  *
2223  * The set_multi entry point is called whenever the multicast address
2224  * list or the network interface flags are updated.  This routine is
2225  * responsible for configuring the hardware for proper multicast,
2226  * promiscuous mode, and all-multi behavior.
2227  **/
2228
2229 static void
2230 e1000_set_multi(struct net_device *netdev)
2231 {
2232         struct e1000_adapter *adapter = netdev_priv(netdev);
2233         struct e1000_hw *hw = &adapter->hw;
2234         struct dev_mc_list *mc_ptr;
2235         uint32_t rctl;
2236         uint32_t hash_value;
2237         int i, rar_entries = E1000_RAR_ENTRIES;
2238         int mta_reg_count = (hw->mac_type == e1000_ich8lan) ?
2239                                 E1000_NUM_MTA_REGISTERS_ICH8LAN :
2240                                 E1000_NUM_MTA_REGISTERS;
2241
2242         if (adapter->hw.mac_type == e1000_ich8lan)
2243                 rar_entries = E1000_RAR_ENTRIES_ICH8LAN;
2244
2245         /* reserve RAR[14] for LAA over-write work-around */
2246         if (adapter->hw.mac_type == e1000_82571)
2247                 rar_entries--;
2248
2249         /* Check for Promiscuous and All Multicast modes */
2250
2251         rctl = E1000_READ_REG(hw, RCTL);
2252
2253         if (netdev->flags & IFF_PROMISC) {
2254                 rctl |= (E1000_RCTL_UPE | E1000_RCTL_MPE);
2255         } else if (netdev->flags & IFF_ALLMULTI) {
2256                 rctl |= E1000_RCTL_MPE;
2257                 rctl &= ~E1000_RCTL_UPE;
2258         } else {
2259                 rctl &= ~(E1000_RCTL_UPE | E1000_RCTL_MPE);
2260         }
2261
2262         E1000_WRITE_REG(hw, RCTL, rctl);
2263
2264         /* 82542 2.0 needs to be in reset to write receive address registers */
2265
2266         if (hw->mac_type == e1000_82542_rev2_0)
2267                 e1000_enter_82542_rst(adapter);
2268
2269         /* load the first 14 multicast address into the exact filters 1-14
2270          * RAR 0 is used for the station MAC adddress
2271          * if there are not 14 addresses, go ahead and clear the filters
2272          * -- with 82571 controllers only 0-13 entries are filled here
2273          */
2274         mc_ptr = netdev->mc_list;
2275
2276         for (i = 1; i < rar_entries; i++) {
2277                 if (mc_ptr) {
2278                         e1000_rar_set(hw, mc_ptr->dmi_addr, i);
2279                         mc_ptr = mc_ptr->next;
2280                 } else {
2281                         E1000_WRITE_REG_ARRAY(hw, RA, i << 1, 0);
2282                         E1000_WRITE_FLUSH(hw);
2283                         E1000_WRITE_REG_ARRAY(hw, RA, (i << 1) + 1, 0);
2284                         E1000_WRITE_FLUSH(hw);
2285                 }
2286         }
2287
2288         /* clear the old settings from the multicast hash table */
2289
2290         for (i = 0; i < mta_reg_count; i++) {
2291                 E1000_WRITE_REG_ARRAY(hw, MTA, i, 0);
2292                 E1000_WRITE_FLUSH(hw);
2293         }
2294
2295         /* load any remaining addresses into the hash table */
2296
2297         for (; mc_ptr; mc_ptr = mc_ptr->next) {
2298                 hash_value = e1000_hash_mc_addr(hw, mc_ptr->dmi_addr);
2299                 e1000_mta_set(hw, hash_value);
2300         }
2301
2302         if (hw->mac_type == e1000_82542_rev2_0)
2303                 e1000_leave_82542_rst(adapter);
2304 }
2305
2306 /* Need to wait a few seconds after link up to get diagnostic information from
2307  * the phy */
2308
2309 static void
2310 e1000_update_phy_info(unsigned long data)
2311 {
2312         struct e1000_adapter *adapter = (struct e1000_adapter *) data;
2313         e1000_phy_get_info(&adapter->hw, &adapter->phy_info);
2314 }
2315
2316 /**
2317  * e1000_82547_tx_fifo_stall - Timer Call-back
2318  * @data: pointer to adapter cast into an unsigned long
2319  **/
2320
2321 static void
2322 e1000_82547_tx_fifo_stall(unsigned long data)
2323 {
2324         struct e1000_adapter *adapter = (struct e1000_adapter *) data;
2325         struct net_device *netdev = adapter->netdev;
2326         uint32_t tctl;
2327
2328         if (atomic_read(&adapter->tx_fifo_stall)) {
2329                 if ((E1000_READ_REG(&adapter->hw, TDT) ==
2330                     E1000_READ_REG(&adapter->hw, TDH)) &&
2331                    (E1000_READ_REG(&adapter->hw, TDFT) ==
2332                     E1000_READ_REG(&adapter->hw, TDFH)) &&
2333                    (E1000_READ_REG(&adapter->hw, TDFTS) ==
2334                     E1000_READ_REG(&adapter->hw, TDFHS))) {
2335                         tctl = E1000_READ_REG(&adapter->hw, TCTL);
2336                         E1000_WRITE_REG(&adapter->hw, TCTL,
2337                                         tctl & ~E1000_TCTL_EN);
2338                         E1000_WRITE_REG(&adapter->hw, TDFT,
2339                                         adapter->tx_head_addr);
2340                         E1000_WRITE_REG(&adapter->hw, TDFH,
2341                                         adapter->tx_head_addr);
2342                         E1000_WRITE_REG(&adapter->hw, TDFTS,
2343                                         adapter->tx_head_addr);
2344                         E1000_WRITE_REG(&adapter->hw, TDFHS,
2345                                         adapter->tx_head_addr);
2346                         E1000_WRITE_REG(&adapter->hw, TCTL, tctl);
2347                         E1000_WRITE_FLUSH(&adapter->hw);
2348
2349                         adapter->tx_fifo_head = 0;
2350                         atomic_set(&adapter->tx_fifo_stall, 0);
2351                         netif_wake_queue(netdev);
2352                 } else {
2353                         mod_timer(&adapter->tx_fifo_stall_timer, jiffies + 1);
2354                 }
2355         }
2356 }
2357
2358 /**
2359  * e1000_watchdog - Timer Call-back
2360  * @data: pointer to adapter cast into an unsigned long
2361  **/
2362 static void
2363 e1000_watchdog(unsigned long data)
2364 {
2365         struct e1000_adapter *adapter = (struct e1000_adapter *) data;
2366         struct net_device *netdev = adapter->netdev;
2367         struct e1000_tx_ring *txdr = adapter->tx_ring;
2368         uint32_t link, tctl;
2369         int32_t ret_val;
2370
2371         ret_val = e1000_check_for_link(&adapter->hw);
2372         if ((ret_val == E1000_ERR_PHY) &&
2373             (adapter->hw.phy_type == e1000_phy_igp_3) &&
2374             (E1000_READ_REG(&adapter->hw, CTRL) & E1000_PHY_CTRL_GBE_DISABLE)) {
2375                 /* See e1000_kumeran_lock_loss_workaround() */
2376                 DPRINTK(LINK, INFO,
2377                         "Gigabit has been disabled, downgrading speed\n");
2378         }
2379         if (adapter->hw.mac_type == e1000_82573) {
2380                 e1000_enable_tx_pkt_filtering(&adapter->hw);
2381                 if (adapter->mng_vlan_id != adapter->hw.mng_cookie.vlan_id)
2382                         e1000_update_mng_vlan(adapter);
2383         }
2384
2385         if ((adapter->hw.media_type == e1000_media_type_internal_serdes) &&
2386            !(E1000_READ_REG(&adapter->hw, TXCW) & E1000_TXCW_ANE))
2387                 link = !adapter->hw.serdes_link_down;
2388         else
2389                 link = E1000_READ_REG(&adapter->hw, STATUS) & E1000_STATUS_LU;
2390
2391         if (link) {
2392                 if (!netif_carrier_ok(netdev)) {
2393                         boolean_t txb2b = 1;
2394                         e1000_get_speed_and_duplex(&adapter->hw,
2395                                                    &adapter->link_speed,
2396                                                    &adapter->link_duplex);
2397
2398                         DPRINTK(LINK, INFO, "NIC Link is Up %d Mbps %s\n",
2399                                adapter->link_speed,
2400                                adapter->link_duplex == FULL_DUPLEX ?
2401                                "Full Duplex" : "Half Duplex");
2402
2403                         /* tweak tx_queue_len according to speed/duplex
2404                          * and adjust the timeout factor */
2405                         netdev->tx_queue_len = adapter->tx_queue_len;
2406                         adapter->tx_timeout_factor = 1;
2407                         switch (adapter->link_speed) {
2408                         case SPEED_10:
2409                                 txb2b = 0;
2410                                 netdev->tx_queue_len = 10;
2411                                 adapter->tx_timeout_factor = 8;
2412                                 break;
2413                         case SPEED_100:
2414                                 txb2b = 0;
2415                                 netdev->tx_queue_len = 100;
2416                                 /* maybe add some timeout factor ? */
2417                                 break;
2418                         }
2419
2420                         if ((adapter->hw.mac_type == e1000_82571 ||
2421                              adapter->hw.mac_type == e1000_82572) &&
2422                             txb2b == 0) {
2423 #define SPEED_MODE_BIT (1 << 21)
2424                                 uint32_t tarc0;
2425                                 tarc0 = E1000_READ_REG(&adapter->hw, TARC0);
2426                                 tarc0 &= ~SPEED_MODE_BIT;
2427                                 E1000_WRITE_REG(&adapter->hw, TARC0, tarc0);
2428                         }
2429                                 
2430 #ifdef NETIF_F_TSO
2431                         /* disable TSO for pcie and 10/100 speeds, to avoid
2432                          * some hardware issues */
2433                         if (!adapter->tso_force &&
2434                             adapter->hw.bus_type == e1000_bus_type_pci_express){
2435                                 switch (adapter->link_speed) {
2436                                 case SPEED_10:
2437                                 case SPEED_100:
2438                                         DPRINTK(PROBE,INFO,
2439                                         "10/100 speed: disabling TSO\n");
2440                                         netdev->features &= ~NETIF_F_TSO;
2441                                         break;
2442                                 case SPEED_1000:
2443                                         netdev->features |= NETIF_F_TSO;
2444                                         break;
2445                                 default:
2446                                         /* oops */
2447                                         break;
2448                                 }
2449                         }
2450 #endif
2451
2452                         /* enable transmits in the hardware, need to do this
2453                          * after setting TARC0 */
2454                         tctl = E1000_READ_REG(&adapter->hw, TCTL);
2455                         tctl |= E1000_TCTL_EN;
2456                         E1000_WRITE_REG(&adapter->hw, TCTL, tctl);
2457
2458                         netif_carrier_on(netdev);
2459                         netif_wake_queue(netdev);
2460                         mod_timer(&adapter->phy_info_timer, jiffies + 2 * HZ);
2461                         adapter->smartspeed = 0;
2462                 }
2463         } else {
2464                 if (netif_carrier_ok(netdev)) {
2465                         adapter->link_speed = 0;
2466                         adapter->link_duplex = 0;
2467                         DPRINTK(LINK, INFO, "NIC Link is Down\n");
2468                         netif_carrier_off(netdev);
2469                         netif_stop_queue(netdev);
2470                         mod_timer(&adapter->phy_info_timer, jiffies + 2 * HZ);
2471
2472                         /* 80003ES2LAN workaround--
2473                          * For packet buffer work-around on link down event;
2474                          * disable receives in the ISR and
2475                          * reset device here in the watchdog
2476                          */
2477                         if (adapter->hw.mac_type == e1000_80003es2lan)
2478                                 /* reset device */
2479                                 schedule_work(&adapter->reset_task);
2480                 }
2481
2482                 e1000_smartspeed(adapter);
2483         }
2484
2485         e1000_update_stats(adapter);
2486
2487         adapter->hw.tx_packet_delta = adapter->stats.tpt - adapter->tpt_old;
2488         adapter->tpt_old = adapter->stats.tpt;
2489         adapter->hw.collision_delta = adapter->stats.colc - adapter->colc_old;
2490         adapter->colc_old = adapter->stats.colc;
2491
2492         adapter->gorcl = adapter->stats.gorcl - adapter->gorcl_old;
2493         adapter->gorcl_old = adapter->stats.gorcl;
2494         adapter->gotcl = adapter->stats.gotcl - adapter->gotcl_old;
2495         adapter->gotcl_old = adapter->stats.gotcl;
2496
2497         e1000_update_adaptive(&adapter->hw);
2498
2499         if (!netif_carrier_ok(netdev)) {
2500                 if (E1000_DESC_UNUSED(txdr) + 1 < txdr->count) {
2501                         /* We've lost link, so the controller stops DMA,
2502                          * but we've got queued Tx work that's never going
2503                          * to get done, so reset controller to flush Tx.
2504                          * (Do the reset outside of interrupt context). */
2505                         adapter->tx_timeout_count++;
2506                         schedule_work(&adapter->reset_task);
2507                 }
2508         }
2509
2510         /* Dynamic mode for Interrupt Throttle Rate (ITR) */
2511         if (adapter->hw.mac_type >= e1000_82540 && adapter->itr == 1) {
2512                 /* Symmetric Tx/Rx gets a reduced ITR=2000; Total
2513                  * asymmetrical Tx or Rx gets ITR=8000; everyone
2514                  * else is between 2000-8000. */
2515                 uint32_t goc = (adapter->gotcl + adapter->gorcl) / 10000;
2516                 uint32_t dif = (adapter->gotcl > adapter->gorcl ?
2517                         adapter->gotcl - adapter->gorcl :
2518                         adapter->gorcl - adapter->gotcl) / 10000;
2519                 uint32_t itr = goc > 0 ? (dif * 6000 / goc + 2000) : 8000;
2520                 E1000_WRITE_REG(&adapter->hw, ITR, 1000000000 / (itr * 256));
2521         }
2522
2523         /* Cause software interrupt to ensure rx ring is cleaned */
2524         E1000_WRITE_REG(&adapter->hw, ICS, E1000_ICS_RXDMT0);
2525
2526         /* Force detection of hung controller every watchdog period */
2527         adapter->detect_tx_hung = TRUE;
2528
2529         /* With 82571 controllers, LAA may be overwritten due to controller
2530          * reset from the other port. Set the appropriate LAA in RAR[0] */
2531         if (adapter->hw.mac_type == e1000_82571 && adapter->hw.laa_is_present)
2532                 e1000_rar_set(&adapter->hw, adapter->hw.mac_addr, 0);
2533
2534         /* Reset the timer */
2535         mod_timer(&adapter->watchdog_timer, jiffies + 2 * HZ);
2536 }
2537
2538 #define E1000_TX_FLAGS_CSUM             0x00000001
2539 #define E1000_TX_FLAGS_VLAN             0x00000002
2540 #define E1000_TX_FLAGS_TSO              0x00000004
2541 #define E1000_TX_FLAGS_IPV4             0x00000008
2542 #define E1000_TX_FLAGS_VLAN_MASK        0xffff0000
2543 #define E1000_TX_FLAGS_VLAN_SHIFT       16
2544
2545 static int
2546 e1000_tso(struct e1000_adapter *adapter, struct e1000_tx_ring *tx_ring,
2547           struct sk_buff *skb)
2548 {
2549 #ifdef NETIF_F_TSO
2550         struct e1000_context_desc *context_desc;
2551         struct e1000_buffer *buffer_info;
2552         unsigned int i;
2553         uint32_t cmd_length = 0;
2554         uint16_t ipcse = 0, tucse, mss;
2555         uint8_t ipcss, ipcso, tucss, tucso, hdr_len;
2556         int err;
2557
2558         if (skb_is_gso(skb)) {
2559                 if (skb_header_cloned(skb)) {
2560                         err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2561                         if (err)
2562                                 return err;
2563                 }
2564
2565                 hdr_len = ((skb->h.raw - skb->data) + (skb->h.th->doff << 2));
2566                 mss = skb_shinfo(skb)->gso_size;
2567                 if (skb->protocol == htons(ETH_P_IP)) {
2568                         skb->nh.iph->tot_len = 0;
2569                         skb->nh.iph->check = 0;
2570                         skb->h.th->check =
2571                                 ~csum_tcpudp_magic(skb->nh.iph->saddr,
2572                                                    skb->nh.iph->daddr,
2573                                                    0,
2574                                                    IPPROTO_TCP,
2575                                                    0);
2576                         cmd_length = E1000_TXD_CMD_IP;
2577                         ipcse = skb->h.raw - skb->data - 1;
2578 #ifdef NETIF_F_TSO_IPV6
2579                 } else if (skb->protocol == htons(ETH_P_IPV6)) {
2580                         skb->nh.ipv6h->payload_len = 0;
2581                         skb->h.th->check =
2582                                 ~csum_ipv6_magic(&skb->nh.ipv6h->saddr,
2583                                                  &skb->nh.ipv6h->daddr,
2584                                                  0,
2585                                                  IPPROTO_TCP,
2586                                                  0);
2587                         ipcse = 0;
2588 #endif
2589                 }
2590                 ipcss = skb->nh.raw - skb->data;
2591                 ipcso = (void *)&(skb->nh.iph->check) - (void *)skb->data;
2592                 tucss = skb->h.raw - skb->data;
2593                 tucso = (void *)&(skb->h.th->check) - (void *)skb->data;
2594                 tucse = 0;
2595
2596                 cmd_length |= (E1000_TXD_CMD_DEXT | E1000_TXD_CMD_TSE |
2597                                E1000_TXD_CMD_TCP | (skb->len - (hdr_len)));
2598
2599                 i = tx_ring->next_to_use;
2600                 context_desc = E1000_CONTEXT_DESC(*tx_ring, i);
2601                 buffer_info = &tx_ring->buffer_info[i];
2602
2603                 context_desc->lower_setup.ip_fields.ipcss  = ipcss;
2604                 context_desc->lower_setup.ip_fields.ipcso  = ipcso;
2605                 context_desc->lower_setup.ip_fields.ipcse  = cpu_to_le16(ipcse);
2606                 context_desc->upper_setup.tcp_fields.tucss = tucss;
2607                 context_desc->upper_setup.tcp_fields.tucso = tucso;
2608                 context_desc->upper_setup.tcp_fields.tucse = cpu_to_le16(tucse);
2609                 context_desc->tcp_seg_setup.fields.mss     = cpu_to_le16(mss);
2610                 context_desc->tcp_seg_setup.fields.hdr_len = hdr_len;
2611                 context_desc->cmd_and_length = cpu_to_le32(cmd_length);
2612
2613                 buffer_info->time_stamp = jiffies;
2614
2615                 if (++i == tx_ring->count) i = 0;
2616                 tx_ring->next_to_use = i;
2617
2618                 return TRUE;
2619         }
2620 #endif
2621
2622         return FALSE;
2623 }
2624
2625 static boolean_t
2626 e1000_tx_csum(struct e1000_adapter *adapter, struct e1000_tx_ring *tx_ring,
2627               struct sk_buff *skb)
2628 {
2629         struct e1000_context_desc *context_desc;
2630         struct e1000_buffer *buffer_info;
2631         unsigned int i;
2632         uint8_t css;
2633
2634         if (likely(skb->ip_summed == CHECKSUM_PARTIAL)) {
2635                 css = skb->h.raw - skb->data;
2636
2637                 i = tx_ring->next_to_use;
2638                 buffer_info = &tx_ring->buffer_info[i];
2639                 context_desc = E1000_CONTEXT_DESC(*tx_ring, i);
2640
2641                 context_desc->upper_setup.tcp_fields.tucss = css;
2642                 context_desc->upper_setup.tcp_fields.tucso = css + skb->csum;
2643                 context_desc->upper_setup.tcp_fields.tucse = 0;
2644                 context_desc->tcp_seg_setup.data = 0;
2645                 context_desc->cmd_and_length = cpu_to_le32(E1000_TXD_CMD_DEXT);
2646
2647                 buffer_info->time_stamp = jiffies;
2648
2649                 if (unlikely(++i == tx_ring->count)) i = 0;
2650                 tx_ring->next_to_use = i;
2651
2652                 return TRUE;
2653         }
2654
2655         return FALSE;
2656 }
2657
2658 #define E1000_MAX_TXD_PWR       12
2659 #define E1000_MAX_DATA_PER_TXD  (1<<E1000_MAX_TXD_PWR)
2660
2661 static int
2662 e1000_tx_map(struct e1000_adapter *adapter, struct e1000_tx_ring *tx_ring,
2663              struct sk_buff *skb, unsigned int first, unsigned int max_per_txd,
2664              unsigned int nr_frags, unsigned int mss)
2665 {
2666         struct e1000_buffer *buffer_info;
2667         unsigned int len = skb->len;
2668         unsigned int offset = 0, size, count = 0, i;
2669         unsigned int f;
2670         len -= skb->data_len;
2671
2672         i = tx_ring->next_to_use;
2673
2674         while (len) {
2675                 buffer_info = &tx_ring->buffer_info[i];
2676                 size = min(len, max_per_txd);
2677 #ifdef NETIF_F_TSO
2678                 /* Workaround for Controller erratum --
2679                  * descriptor for non-tso packet in a linear SKB that follows a
2680                  * tso gets written back prematurely before the data is fully
2681                  * DMA'd to the controller */
2682                 if (!skb->data_len && tx_ring->last_tx_tso &&
2683                     !skb_is_gso(skb)) {
2684                         tx_ring->last_tx_tso = 0;
2685                         size -= 4;
2686                 }
2687
2688                 /* Workaround for premature desc write-backs
2689                  * in TSO mode.  Append 4-byte sentinel desc */
2690                 if (unlikely(mss && !nr_frags && size == len && size > 8))
2691                         size -= 4;
2692 #endif
2693                 /* work-around for errata 10 and it applies
2694                  * to all controllers in PCI-X mode
2695                  * The fix is to make sure that the first descriptor of a
2696                  * packet is smaller than 2048 - 16 - 16 (or 2016) bytes
2697                  */
2698                 if (unlikely((adapter->hw.bus_type == e1000_bus_type_pcix) &&
2699                                 (size > 2015) && count == 0))
2700                         size = 2015;
2701
2702                 /* Workaround for potential 82544 hang in PCI-X.  Avoid
2703                  * terminating buffers within evenly-aligned dwords. */
2704                 if (unlikely(adapter->pcix_82544 &&
2705                    !((unsigned long)(skb->data + offset + size - 1) & 4) &&
2706                    size > 4))
2707                         size -= 4;
2708
2709                 buffer_info->length = size;
2710                 buffer_info->dma =
2711                         pci_map_single(adapter->pdev,
2712                                 skb->data + offset,
2713                                 size,
2714                                 PCI_DMA_TODEVICE);
2715                 buffer_info->time_stamp = jiffies;
2716
2717                 len -= size;
2718                 offset += size;
2719                 count++;
2720                 if (unlikely(++i == tx_ring->count)) i = 0;
2721         }
2722
2723         for (f = 0; f < nr_frags; f++) {
2724                 struct skb_frag_struct *frag;
2725
2726                 frag = &skb_shinfo(skb)->frags[f];
2727                 len = frag->size;
2728                 offset = frag->page_offset;
2729
2730                 while (len) {
2731                         buffer_info = &tx_ring->buffer_info[i];
2732                         size = min(len, max_per_txd);
2733 #ifdef NETIF_F_TSO
2734                         /* Workaround for premature desc write-backs
2735                          * in TSO mode.  Append 4-byte sentinel desc */
2736                         if (unlikely(mss && f == (nr_frags-1) && size == len && size > 8))
2737                                 size -= 4;
2738 #endif
2739                         /* Workaround for potential 82544 hang in PCI-X.
2740                          * Avoid terminating buffers within evenly-aligned
2741                          * dwords. */
2742                         if (unlikely(adapter->pcix_82544 &&
2743                            !((unsigned long)(frag->page+offset+size-1) & 4) &&
2744                            size > 4))
2745                                 size -= 4;
2746
2747                         buffer_info->length = size;
2748                         buffer_info->dma =
2749                                 pci_map_page(adapter->pdev,
2750                                         frag->page,
2751                                         offset,
2752                                         size,
2753                                         PCI_DMA_TODEVICE);
2754                         buffer_info->time_stamp = jiffies;
2755
2756                         len -= size;
2757                         offset += size;
2758                         count++;
2759                         if (unlikely(++i == tx_ring->count)) i = 0;
2760                 }
2761         }
2762
2763         i = (i == 0) ? tx_ring->count - 1 : i - 1;
2764         tx_ring->buffer_info[i].skb = skb;
2765         tx_ring->buffer_info[first].next_to_watch = i;
2766
2767         return count;
2768 }
2769
2770 static void
2771 e1000_tx_queue(struct e1000_adapter *adapter, struct e1000_tx_ring *tx_ring,
2772                int tx_flags, int count)
2773 {
2774         struct e1000_tx_desc *tx_desc = NULL;
2775         struct e1000_buffer *buffer_info;
2776         uint32_t txd_upper = 0, txd_lower = E1000_TXD_CMD_IFCS;
2777         unsigned int i;
2778
2779         if (likely(tx_flags & E1000_TX_FLAGS_TSO)) {
2780                 txd_lower |= E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D |
2781                              E1000_TXD_CMD_TSE;
2782                 txd_upper |= E1000_TXD_POPTS_TXSM << 8;
2783
2784                 if (likely(tx_flags & E1000_TX_FLAGS_IPV4))
2785                         txd_upper |= E1000_TXD_POPTS_IXSM << 8;
2786         }
2787
2788         if (likely(tx_flags & E1000_TX_FLAGS_CSUM)) {
2789                 txd_lower |= E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D;
2790                 txd_upper |= E1000_TXD_POPTS_TXSM << 8;
2791         }
2792
2793         if (unlikely(tx_flags & E1000_TX_FLAGS_VLAN)) {
2794                 txd_lower |= E1000_TXD_CMD_VLE;
2795                 txd_upper |= (tx_flags & E1000_TX_FLAGS_VLAN_MASK);
2796         }
2797
2798         i = tx_ring->next_to_use;
2799
2800         while (count--) {
2801                 buffer_info = &tx_ring->buffer_info[i];
2802                 tx_desc = E1000_TX_DESC(*tx_ring, i);
2803                 tx_desc->buffer_addr = cpu_to_le64(buffer_info->dma);
2804                 tx_desc->lower.data =
2805                         cpu_to_le32(txd_lower | buffer_info->length);
2806                 tx_desc->upper.data = cpu_to_le32(txd_upper);
2807                 if (unlikely(++i == tx_ring->count)) i = 0;
2808         }
2809
2810         tx_desc->lower.data |= cpu_to_le32(adapter->txd_cmd);
2811
2812         /* Force memory writes to complete before letting h/w
2813          * know there are new descriptors to fetch.  (Only
2814          * applicable for weak-ordered memory model archs,
2815          * such as IA-64). */
2816         wmb();
2817
2818         tx_ring->next_to_use = i;
2819         writel(i, adapter->hw.hw_addr + tx_ring->tdt);
2820 }
2821
2822 /**
2823  * 82547 workaround to avoid controller hang in half-duplex environment.
2824  * The workaround is to avoid queuing a large packet that would span
2825  * the internal Tx FIFO ring boundary by notifying the stack to resend
2826  * the packet at a later time.  This gives the Tx FIFO an opportunity to
2827  * flush all packets.  When that occurs, we reset the Tx FIFO pointers
2828  * to the beginning of the Tx FIFO.
2829  **/
2830
2831 #define E1000_FIFO_HDR                  0x10
2832 #define E1000_82547_PAD_LEN             0x3E0
2833
2834 static int
2835 e1000_82547_fifo_workaround(struct e1000_adapter *adapter, struct sk_buff *skb)
2836 {
2837         uint32_t fifo_space = adapter->tx_fifo_size - adapter->tx_fifo_head;
2838         uint32_t skb_fifo_len = skb->len + E1000_FIFO_HDR;
2839
2840         E1000_ROUNDUP(skb_fifo_len, E1000_FIFO_HDR);
2841
2842         if (adapter->link_duplex != HALF_DUPLEX)
2843                 goto no_fifo_stall_required;
2844
2845         if (atomic_read(&adapter->tx_fifo_stall))
2846                 return 1;
2847
2848         if (skb_fifo_len >= (E1000_82547_PAD_LEN + fifo_space)) {
2849                 atomic_set(&adapter->tx_fifo_stall, 1);
2850                 return 1;
2851         }
2852
2853 no_fifo_stall_required:
2854         adapter->tx_fifo_head += skb_fifo_len;
2855         if (adapter->tx_fifo_head >= adapter->tx_fifo_size)
2856                 adapter->tx_fifo_head -= adapter->tx_fifo_size;
2857         return 0;
2858 }
2859
2860 #define MINIMUM_DHCP_PACKET_SIZE 282
2861 static int
2862 e1000_transfer_dhcp_info(struct e1000_adapter *adapter, struct sk_buff *skb)
2863 {
2864         struct e1000_hw *hw =  &adapter->hw;
2865         uint16_t length, offset;
2866         if (vlan_tx_tag_present(skb)) {
2867                 if (!((vlan_tx_tag_get(skb) == adapter->hw.mng_cookie.vlan_id) &&
2868                         ( adapter->hw.mng_cookie.status &
2869                           E1000_MNG_DHCP_COOKIE_STATUS_VLAN_SUPPORT)) )
2870                         return 0;
2871         }
2872         if (skb->len > MINIMUM_DHCP_PACKET_SIZE) {
2873                 struct ethhdr *eth = (struct ethhdr *) skb->data;
2874                 if ((htons(ETH_P_IP) == eth->h_proto)) {
2875                         const struct iphdr *ip =
2876                                 (struct iphdr *)((uint8_t *)skb->data+14);
2877                         if (IPPROTO_UDP == ip->protocol) {
2878                                 struct udphdr *udp =
2879                                         (struct udphdr *)((uint8_t *)ip +
2880                                                 (ip->ihl << 2));
2881                                 if (ntohs(udp->dest) == 67) {
2882                                         offset = (uint8_t *)udp + 8 - skb->data;
2883                                         length = skb->len - offset;
2884
2885                                         return e1000_mng_write_dhcp_info(hw,
2886                                                         (uint8_t *)udp + 8,
2887                                                         length);
2888                                 }
2889                         }
2890                 }
2891         }
2892         return 0;
2893 }
2894
2895 #define TXD_USE_COUNT(S, X) (((S) >> (X)) + 1 )
2896 static int
2897 e1000_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
2898 {
2899         struct e1000_adapter *adapter = netdev_priv(netdev);
2900         struct e1000_tx_ring *tx_ring;
2901         unsigned int first, max_per_txd = E1000_MAX_DATA_PER_TXD;
2902         unsigned int max_txd_pwr = E1000_MAX_TXD_PWR;
2903         unsigned int tx_flags = 0;
2904         unsigned int len = skb->len;
2905         unsigned long flags;
2906         unsigned int nr_frags = 0;
2907         unsigned int mss = 0;
2908         int count = 0;
2909         int tso;
2910         unsigned int f;
2911         len -= skb->data_len;
2912
2913         tx_ring = adapter->tx_ring;
2914
2915         if (unlikely(skb->len <= 0)) {
2916                 dev_kfree_skb_any(skb);
2917                 return NETDEV_TX_OK;
2918         }
2919
2920 #ifdef NETIF_F_TSO
2921         mss = skb_shinfo(skb)->gso_size;
2922         /* The controller does a simple calculation to
2923          * make sure there is enough room in the FIFO before
2924          * initiating the DMA for each buffer.  The calc is:
2925          * 4 = ceil(buffer len/mss).  To make sure we don't
2926          * overrun the FIFO, adjust the max buffer len if mss
2927          * drops. */
2928         if (mss) {
2929                 uint8_t hdr_len;
2930                 max_per_txd = min(mss << 2, max_per_txd);
2931                 max_txd_pwr = fls(max_per_txd) - 1;
2932
2933         /* TSO Workaround for 82571/2/3 Controllers -- if skb->data
2934          * points to just header, pull a few bytes of payload from
2935          * frags into skb->data */
2936                 hdr_len = ((skb->h.raw - skb->data) + (skb->h.th->doff << 2));
2937                 if (skb->data_len && (hdr_len == (skb->len - skb->data_len))) {
2938                         switch (adapter->hw.mac_type) {
2939                                 unsigned int pull_size;
2940                         case e1000_82571:
2941                         case e1000_82572:
2942                         case e1000_82573:
2943                         case e1000_ich8lan:
2944                                 pull_size = min((unsigned int)4, skb->data_len);
2945                                 if (!__pskb_pull_tail(skb, pull_size)) {
2946                                         DPRINTK(DRV, ERR,
2947                                                 "__pskb_pull_tail failed.\n");
2948                                         dev_kfree_skb_any(skb);
2949                                         return NETDEV_TX_OK;
2950                                 }
2951                                 len = skb->len - skb->data_len;
2952                                 break;
2953                         default:
2954                                 /* do nothing */
2955                                 break;
2956                         }
2957                 }
2958         }
2959
2960         /* reserve a descriptor for the offload context */
2961         if ((mss) || (skb->ip_summed == CHECKSUM_PARTIAL))
2962                 count++;
2963         count++;
2964 #else
2965         if (skb->ip_summed == CHECKSUM_PARTIAL)
2966                 count++;
2967 #endif
2968
2969 #ifdef NETIF_F_TSO
2970         /* Controller Erratum workaround */
2971         if (!skb->data_len && tx_ring->last_tx_tso && !skb_is_gso(skb))
2972                 count++;
2973 #endif
2974
2975         count += TXD_USE_COUNT(len, max_txd_pwr);
2976
2977         if (adapter->pcix_82544)
2978                 count++;
2979
2980         /* work-around for errata 10 and it applies to all controllers
2981          * in PCI-X mode, so add one more descriptor to the count
2982          */
2983         if (unlikely((adapter->hw.bus_type == e1000_bus_type_pcix) &&
2984                         (len > 2015)))
2985                 count++;
2986
2987         nr_frags = skb_shinfo(skb)->nr_frags;
2988         for (f = 0; f < nr_frags; f++)
2989                 count += TXD_USE_COUNT(skb_shinfo(skb)->frags[f].size,
2990                                        max_txd_pwr);
2991         if (adapter->pcix_82544)
2992                 count += nr_frags;
2993
2994
2995         if (adapter->hw.tx_pkt_filtering &&
2996             (adapter->hw.mac_type == e1000_82573))
2997                 e1000_transfer_dhcp_info(adapter, skb);
2998
2999         local_irq_save(flags);
3000         if (!spin_trylock(&tx_ring->tx_lock)) {
3001                 /* Collision - tell upper layer to requeue */
3002                 local_irq_restore(flags);
3003                 return NETDEV_TX_LOCKED;
3004         }
3005
3006         /* need: count + 2 desc gap to keep tail from touching
3007          * head, otherwise try next time */
3008         if (unlikely(E1000_DESC_UNUSED(tx_ring) < count + 2)) {
3009                 netif_stop_queue(netdev);
3010                 spin_unlock_irqrestore(&tx_ring->tx_lock, flags);
3011                 return NETDEV_TX_BUSY;
3012         }
3013
3014         if (unlikely(adapter->hw.mac_type == e1000_82547)) {
3015                 if (unlikely(e1000_82547_fifo_workaround(adapter, skb))) {
3016                         netif_stop_queue(netdev);
3017                         mod_timer(&adapter->tx_fifo_stall_timer, jiffies);
3018                         spin_unlock_irqrestore(&tx_ring->tx_lock, flags);
3019                         return NETDEV_TX_BUSY;
3020                 }
3021         }
3022
3023         if (unlikely(adapter->vlgrp && vlan_tx_tag_present(skb))) {
3024                 tx_flags |= E1000_TX_FLAGS_VLAN;
3025                 tx_flags |= (vlan_tx_tag_get(skb) << E1000_TX_FLAGS_VLAN_SHIFT);
3026         }
3027
3028         first = tx_ring->next_to_use;
3029
3030         tso = e1000_tso(adapter, tx_ring, skb);
3031         if (tso < 0) {
3032                 dev_kfree_skb_any(skb);
3033                 spin_unlock_irqrestore(&tx_ring->tx_lock, flags);
3034                 return NETDEV_TX_OK;
3035         }
3036
3037         if (likely(tso)) {
3038                 tx_ring->last_tx_tso = 1;
3039                 tx_flags |= E1000_TX_FLAGS_TSO;
3040         } else if (likely(e1000_tx_csum(adapter, tx_ring, skb)))
3041                 tx_flags |= E1000_TX_FLAGS_CSUM;
3042
3043         /* Old method was to assume IPv4 packet by default if TSO was enabled.
3044          * 82571 hardware supports TSO capabilities for IPv6 as well...
3045          * no longer assume, we must. */
3046         if (likely(skb->protocol == htons(ETH_P_IP)))
3047                 tx_flags |= E1000_TX_FLAGS_IPV4;
3048
3049         e1000_tx_queue(adapter, tx_ring, tx_flags,
3050                        e1000_tx_map(adapter, tx_ring, skb, first,
3051                                     max_per_txd, nr_frags, mss));
3052
3053         netdev->trans_start = jiffies;
3054
3055         /* Make sure there is space in the ring for the next send. */
3056         if (unlikely(E1000_DESC_UNUSED(tx_ring) < MAX_SKB_FRAGS + 2))
3057                 netif_stop_queue(netdev);
3058
3059         spin_unlock_irqrestore(&tx_ring->tx_lock, flags);
3060         return NETDEV_TX_OK;
3061 }
3062
3063 /**
3064  * e1000_tx_timeout - Respond to a Tx Hang
3065  * @netdev: network interface device structure
3066  **/
3067
3068 static void
3069 e1000_tx_timeout(struct net_device *netdev)
3070 {
3071         struct e1000_adapter *adapter = netdev_priv(netdev);
3072
3073         /* Do the reset outside of interrupt context */
3074         adapter->tx_timeout_count++;
3075         schedule_work(&adapter->reset_task);
3076 }
3077
3078 static void
3079 e1000_reset_task(struct net_device *netdev)
3080 {
3081         struct e1000_adapter *adapter = netdev_priv(netdev);
3082
3083         e1000_reinit_locked(adapter);
3084 }
3085
3086 /**
3087  * e1000_get_stats - Get System Network Statistics
3088  * @netdev: network interface device structure
3089  *
3090  * Returns the address of the device statistics structure.
3091  * The statistics are actually updated from the timer callback.
3092  **/
3093
3094 static struct net_device_stats *
3095 e1000_get_stats(struct net_device *netdev)
3096 {
3097         struct e1000_adapter *adapter = netdev_priv(netdev);
3098
3099         /* only return the current stats */
3100         return &adapter->net_stats;
3101 }
3102
3103 /**
3104  * e1000_change_mtu - Change the Maximum Transfer Unit
3105  * @netdev: network interface device structure
3106  * @new_mtu: new value for maximum frame size
3107  *
3108  * Returns 0 on success, negative on failure
3109  **/
3110
3111 static int
3112 e1000_change_mtu(struct net_device *netdev, int new_mtu)
3113 {
3114         struct e1000_adapter *adapter = netdev_priv(netdev);
3115         int max_frame = new_mtu + ENET_HEADER_SIZE + ETHERNET_FCS_SIZE;
3116         uint16_t eeprom_data = 0;
3117
3118         if ((max_frame < MINIMUM_ETHERNET_FRAME_SIZE) ||
3119             (max_frame > MAX_JUMBO_FRAME_SIZE)) {
3120                 DPRINTK(PROBE, ERR, "Invalid MTU setting\n");
3121                 return -EINVAL;
3122         }
3123
3124         /* Adapter-specific max frame size limits. */
3125         switch (adapter->hw.mac_type) {
3126         case e1000_undefined ... e1000_82542_rev2_1:
3127         case e1000_ich8lan:
3128                 if (max_frame > MAXIMUM_ETHERNET_FRAME_SIZE) {
3129                         DPRINTK(PROBE, ERR, "Jumbo Frames not supported.\n");
3130                         return -EINVAL;
3131                 }
3132                 break;
3133         case e1000_82573:
3134                 /* only enable jumbo frames if ASPM is disabled completely
3135                  * this means both bits must be zero in 0x1A bits 3:2 */
3136                 e1000_read_eeprom(&adapter->hw, EEPROM_INIT_3GIO_3, 1,
3137                                   &eeprom_data);
3138                 if (eeprom_data & EEPROM_WORD1A_ASPM_MASK) {
3139                         if (max_frame > MAXIMUM_ETHERNET_FRAME_SIZE) {
3140                                 DPRINTK(PROBE, ERR,
3141                                         "Jumbo Frames not supported.\n");
3142                                 return -EINVAL;
3143                         }</