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