WorkStruct: Pass the work_struct pointer instead of context data
[linux-2.6.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 work_struct *work);
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, e1000_reset_task);
912
913         e1000_check_options(adapter);
914
915         /* Initial Wake on LAN setting
916          * If APM wake is enabled in the EEPROM,
917          * enable the ACPI Magic Packet filter
918          */
919
920         switch (adapter->hw.mac_type) {
921         case e1000_82542_rev2_0:
922         case e1000_82542_rev2_1:
923         case e1000_82543:
924                 break;
925         case e1000_82544:
926                 e1000_read_eeprom(&adapter->hw,
927                         EEPROM_INIT_CONTROL2_REG, 1, &eeprom_data);
928                 eeprom_apme_mask = E1000_EEPROM_82544_APM;
929                 break;
930         case e1000_ich8lan:
931                 e1000_read_eeprom(&adapter->hw,
932                         EEPROM_INIT_CONTROL1_REG, 1, &eeprom_data);
933                 eeprom_apme_mask = E1000_EEPROM_ICH8_APME;
934                 break;
935         case e1000_82546:
936         case e1000_82546_rev_3:
937         case e1000_82571:
938         case e1000_80003es2lan:
939                 if (E1000_READ_REG(&adapter->hw, STATUS) & E1000_STATUS_FUNC_1){
940                         e1000_read_eeprom(&adapter->hw,
941                                 EEPROM_INIT_CONTROL3_PORT_B, 1, &eeprom_data);
942                         break;
943                 }
944                 /* Fall Through */
945         default:
946                 e1000_read_eeprom(&adapter->hw,
947                         EEPROM_INIT_CONTROL3_PORT_A, 1, &eeprom_data);
948                 break;
949         }
950         if (eeprom_data & eeprom_apme_mask)
951                 adapter->eeprom_wol |= E1000_WUFC_MAG;
952
953         /* now that we have the eeprom settings, apply the special cases
954          * where the eeprom may be wrong or the board simply won't support
955          * wake on lan on a particular port */
956         switch (pdev->device) {
957         case E1000_DEV_ID_82546GB_PCIE:
958                 adapter->eeprom_wol = 0;
959                 break;
960         case E1000_DEV_ID_82546EB_FIBER:
961         case E1000_DEV_ID_82546GB_FIBER:
962         case E1000_DEV_ID_82571EB_FIBER:
963                 /* Wake events only supported on port A for dual fiber
964                  * regardless of eeprom setting */
965                 if (E1000_READ_REG(&adapter->hw, STATUS) & E1000_STATUS_FUNC_1)
966                         adapter->eeprom_wol = 0;
967                 break;
968         case E1000_DEV_ID_82546GB_QUAD_COPPER_KSP3:
969         case E1000_DEV_ID_82571EB_QUAD_COPPER:
970                 /* if quad port adapter, disable WoL on all but port A */
971                 if (global_quad_port_a != 0)
972                         adapter->eeprom_wol = 0;
973                 else
974                         adapter->quad_port_a = 1;
975                 /* Reset for multiple quad port adapters */
976                 if (++global_quad_port_a == 4)
977                         global_quad_port_a = 0;
978                 break;
979         }
980
981         /* initialize the wol settings based on the eeprom settings */
982         adapter->wol = adapter->eeprom_wol;
983
984         /* print bus type/speed/width info */
985         {
986         struct e1000_hw *hw = &adapter->hw;
987         DPRINTK(PROBE, INFO, "(PCI%s:%s:%s) ",
988                 ((hw->bus_type == e1000_bus_type_pcix) ? "-X" :
989                  (hw->bus_type == e1000_bus_type_pci_express ? " Express":"")),
990                 ((hw->bus_speed == e1000_bus_speed_2500) ? "2.5Gb/s" :
991                  (hw->bus_speed == e1000_bus_speed_133) ? "133MHz" :
992                  (hw->bus_speed == e1000_bus_speed_120) ? "120MHz" :
993                  (hw->bus_speed == e1000_bus_speed_100) ? "100MHz" :
994                  (hw->bus_speed == e1000_bus_speed_66) ? "66MHz" : "33MHz"),
995                 ((hw->bus_width == e1000_bus_width_64) ? "64-bit" :
996                  (hw->bus_width == e1000_bus_width_pciex_4) ? "Width x4" :
997                  (hw->bus_width == e1000_bus_width_pciex_1) ? "Width x1" :
998                  "32-bit"));
999         }
1000
1001         for (i = 0; i < 6; i++)
1002                 printk("%2.2x%c", netdev->dev_addr[i], i == 5 ? '\n' : ':');
1003
1004         /* reset the hardware with the new settings */
1005         e1000_reset(adapter);
1006
1007         /* If the controller is 82573 and f/w is AMT, do not set
1008          * DRV_LOAD until the interface is up.  For all other cases,
1009          * let the f/w know that the h/w is now under the control
1010          * of the driver. */
1011         if (adapter->hw.mac_type != e1000_82573 ||
1012             !e1000_check_mng_mode(&adapter->hw))
1013                 e1000_get_hw_control(adapter);
1014
1015         strcpy(netdev->name, "eth%d");
1016         if ((err = register_netdev(netdev)))
1017                 goto err_register;
1018
1019         /* tell the stack to leave us alone until e1000_open() is called */
1020         netif_carrier_off(netdev);
1021         netif_stop_queue(netdev);
1022
1023         DPRINTK(PROBE, INFO, "Intel(R) PRO/1000 Network Connection\n");
1024
1025         cards_found++;
1026         return 0;
1027
1028 err_register:
1029         e1000_release_hw_control(adapter);
1030 err_eeprom:
1031         if (!e1000_check_phy_reset_block(&adapter->hw))
1032                 e1000_phy_hw_reset(&adapter->hw);
1033
1034         if (adapter->hw.flash_address)
1035                 iounmap(adapter->hw.flash_address);
1036 err_flashmap:
1037 #ifdef CONFIG_E1000_NAPI
1038         for (i = 0; i < adapter->num_rx_queues; i++)
1039                 dev_put(&adapter->polling_netdev[i]);
1040 #endif
1041
1042         kfree(adapter->tx_ring);
1043         kfree(adapter->rx_ring);
1044 #ifdef CONFIG_E1000_NAPI
1045         kfree(adapter->polling_netdev);
1046 #endif
1047 err_sw_init:
1048         iounmap(adapter->hw.hw_addr);
1049 err_ioremap:
1050         free_netdev(netdev);
1051 err_alloc_etherdev:
1052         pci_release_regions(pdev);
1053 err_pci_reg:
1054 err_dma:
1055         pci_disable_device(pdev);
1056         return err;
1057 }
1058
1059 /**
1060  * e1000_remove - Device Removal Routine
1061  * @pdev: PCI device information struct
1062  *
1063  * e1000_remove is called by the PCI subsystem to alert the driver
1064  * that it should release a PCI device.  The could be caused by a
1065  * Hot-Plug event, or because the driver is going to be removed from
1066  * memory.
1067  **/
1068
1069 static void __devexit
1070 e1000_remove(struct pci_dev *pdev)
1071 {
1072         struct net_device *netdev = pci_get_drvdata(pdev);
1073         struct e1000_adapter *adapter = netdev_priv(netdev);
1074         uint32_t manc;
1075 #ifdef CONFIG_E1000_NAPI
1076         int i;
1077 #endif
1078
1079         flush_scheduled_work();
1080
1081         if (adapter->hw.mac_type >= e1000_82540 &&
1082             adapter->hw.mac_type < e1000_82571 &&
1083             adapter->hw.media_type == e1000_media_type_copper) {
1084                 manc = E1000_READ_REG(&adapter->hw, MANC);
1085                 if (manc & E1000_MANC_SMBUS_EN) {
1086                         manc |= E1000_MANC_ARP_EN;
1087                         E1000_WRITE_REG(&adapter->hw, MANC, manc);
1088                 }
1089         }
1090
1091         /* Release control of h/w to f/w.  If f/w is AMT enabled, this
1092          * would have already happened in close and is redundant. */
1093         e1000_release_hw_control(adapter);
1094
1095         unregister_netdev(netdev);
1096 #ifdef CONFIG_E1000_NAPI
1097         for (i = 0; i < adapter->num_rx_queues; i++)
1098                 dev_put(&adapter->polling_netdev[i]);
1099 #endif
1100
1101         if (!e1000_check_phy_reset_block(&adapter->hw))
1102                 e1000_phy_hw_reset(&adapter->hw);
1103
1104         kfree(adapter->tx_ring);
1105         kfree(adapter->rx_ring);
1106 #ifdef CONFIG_E1000_NAPI
1107         kfree(adapter->polling_netdev);
1108 #endif
1109
1110         iounmap(adapter->hw.hw_addr);
1111         if (adapter->hw.flash_address)
1112                 iounmap(adapter->hw.flash_address);
1113         pci_release_regions(pdev);
1114
1115         free_netdev(netdev);
1116
1117         pci_disable_device(pdev);
1118 }
1119
1120 /**
1121  * e1000_sw_init - Initialize general software structures (struct e1000_adapter)
1122  * @adapter: board private structure to initialize
1123  *
1124  * e1000_sw_init initializes the Adapter private data structure.
1125  * Fields are initialized based on PCI device information and
1126  * OS network device settings (MTU size).
1127  **/
1128
1129 static int __devinit
1130 e1000_sw_init(struct e1000_adapter *adapter)
1131 {
1132         struct e1000_hw *hw = &adapter->hw;
1133         struct net_device *netdev = adapter->netdev;
1134         struct pci_dev *pdev = adapter->pdev;
1135 #ifdef CONFIG_E1000_NAPI
1136         int i;
1137 #endif
1138
1139         /* PCI config space info */
1140
1141         hw->vendor_id = pdev->vendor;
1142         hw->device_id = pdev->device;
1143         hw->subsystem_vendor_id = pdev->subsystem_vendor;
1144         hw->subsystem_id = pdev->subsystem_device;
1145
1146         pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
1147
1148         pci_read_config_word(pdev, PCI_COMMAND, &hw->pci_cmd_word);
1149
1150         adapter->rx_buffer_len = MAXIMUM_ETHERNET_VLAN_SIZE;
1151         adapter->rx_ps_bsize0 = E1000_RXBUFFER_128;
1152         hw->max_frame_size = netdev->mtu +
1153                              ENET_HEADER_SIZE + ETHERNET_FCS_SIZE;
1154         hw->min_frame_size = MINIMUM_ETHERNET_FRAME_SIZE;
1155
1156         /* identify the MAC */
1157
1158         if (e1000_set_mac_type(hw)) {
1159                 DPRINTK(PROBE, ERR, "Unknown MAC Type\n");
1160                 return -EIO;
1161         }
1162
1163         switch (hw->mac_type) {
1164         default:
1165                 break;
1166         case e1000_82541:
1167         case e1000_82547:
1168         case e1000_82541_rev_2:
1169         case e1000_82547_rev_2:
1170                 hw->phy_init_script = 1;
1171                 break;
1172         }
1173
1174         e1000_set_media_type(hw);
1175
1176         hw->wait_autoneg_complete = FALSE;
1177         hw->tbi_compatibility_en = TRUE;
1178         hw->adaptive_ifs = TRUE;
1179
1180         /* Copper options */
1181
1182         if (hw->media_type == e1000_media_type_copper) {
1183                 hw->mdix = AUTO_ALL_MODES;
1184                 hw->disable_polarity_correction = FALSE;
1185                 hw->master_slave = E1000_MASTER_SLAVE;
1186         }
1187
1188         adapter->num_tx_queues = 1;
1189         adapter->num_rx_queues = 1;
1190
1191         if (e1000_alloc_queues(adapter)) {
1192                 DPRINTK(PROBE, ERR, "Unable to allocate memory for queues\n");
1193                 return -ENOMEM;
1194         }
1195
1196 #ifdef CONFIG_E1000_NAPI
1197         for (i = 0; i < adapter->num_rx_queues; i++) {
1198                 adapter->polling_netdev[i].priv = adapter;
1199                 adapter->polling_netdev[i].poll = &e1000_clean;
1200                 adapter->polling_netdev[i].weight = 64;
1201                 dev_hold(&adapter->polling_netdev[i]);
1202                 set_bit(__LINK_STATE_START, &adapter->polling_netdev[i].state);
1203         }
1204         spin_lock_init(&adapter->tx_queue_lock);
1205 #endif
1206
1207         atomic_set(&adapter->irq_sem, 1);
1208         spin_lock_init(&adapter->stats_lock);
1209
1210         set_bit(__E1000_DOWN, &adapter->flags);
1211
1212         return 0;
1213 }
1214
1215 /**
1216  * e1000_alloc_queues - Allocate memory for all rings
1217  * @adapter: board private structure to initialize
1218  *
1219  * We allocate one ring per queue at run-time since we don't know the
1220  * number of queues at compile-time.  The polling_netdev array is
1221  * intended for Multiqueue, but should work fine with a single queue.
1222  **/
1223
1224 static int __devinit
1225 e1000_alloc_queues(struct e1000_adapter *adapter)
1226 {
1227         int size;
1228
1229         size = sizeof(struct e1000_tx_ring) * adapter->num_tx_queues;
1230         adapter->tx_ring = kmalloc(size, GFP_KERNEL);
1231         if (!adapter->tx_ring)
1232                 return -ENOMEM;
1233         memset(adapter->tx_ring, 0, size);
1234
1235         size = sizeof(struct e1000_rx_ring) * adapter->num_rx_queues;
1236         adapter->rx_ring = kmalloc(size, GFP_KERNEL);
1237         if (!adapter->rx_ring) {
1238                 kfree(adapter->tx_ring);
1239                 return -ENOMEM;
1240         }
1241         memset(adapter->rx_ring, 0, size);
1242
1243 #ifdef CONFIG_E1000_NAPI
1244         size = sizeof(struct net_device) * adapter->num_rx_queues;
1245         adapter->polling_netdev = kmalloc(size, GFP_KERNEL);
1246         if (!adapter->polling_netdev) {
1247                 kfree(adapter->tx_ring);
1248                 kfree(adapter->rx_ring);
1249                 return -ENOMEM;
1250         }
1251         memset(adapter->polling_netdev, 0, size);
1252 #endif
1253
1254         return E1000_SUCCESS;
1255 }
1256
1257 /**
1258  * e1000_open - Called when a network interface is made active
1259  * @netdev: network interface device structure
1260  *
1261  * Returns 0 on success, negative value on failure
1262  *
1263  * The open entry point is called when a network interface is made
1264  * active by the system (IFF_UP).  At this point all resources needed
1265  * for transmit and receive operations are allocated, the interrupt
1266  * handler is registered with the OS, the watchdog timer is started,
1267  * and the stack is notified that the interface is ready.
1268  **/
1269
1270 static int
1271 e1000_open(struct net_device *netdev)
1272 {
1273         struct e1000_adapter *adapter = netdev_priv(netdev);
1274         int err;
1275
1276         /* disallow open during test */
1277         if (test_bit(__E1000_TESTING, &adapter->flags))
1278                 return -EBUSY;
1279
1280         /* allocate transmit descriptors */
1281
1282         if ((err = e1000_setup_all_tx_resources(adapter)))
1283                 goto err_setup_tx;
1284
1285         /* allocate receive descriptors */
1286
1287         if ((err = e1000_setup_all_rx_resources(adapter)))
1288                 goto err_setup_rx;
1289
1290         err = e1000_request_irq(adapter);
1291         if (err)
1292                 goto err_req_irq;
1293
1294         e1000_power_up_phy(adapter);
1295
1296         if ((err = e1000_up(adapter)))
1297                 goto err_up;
1298         adapter->mng_vlan_id = E1000_MNG_VLAN_NONE;
1299         if ((adapter->hw.mng_cookie.status &
1300                           E1000_MNG_DHCP_COOKIE_STATUS_VLAN_SUPPORT)) {
1301                 e1000_update_mng_vlan(adapter);
1302         }
1303
1304         /* If AMT is enabled, let the firmware know that the network
1305          * interface is now open */
1306         if (adapter->hw.mac_type == e1000_82573 &&
1307             e1000_check_mng_mode(&adapter->hw))
1308                 e1000_get_hw_control(adapter);
1309
1310         return E1000_SUCCESS;
1311
1312 err_up:
1313         e1000_power_down_phy(adapter);
1314         e1000_free_irq(adapter);
1315 err_req_irq:
1316         e1000_free_all_rx_resources(adapter);
1317 err_setup_rx:
1318         e1000_free_all_tx_resources(adapter);
1319 err_setup_tx:
1320         e1000_reset(adapter);
1321
1322         return err;
1323 }
1324
1325 /**
1326  * e1000_close - Disables a network interface
1327  * @netdev: network interface device structure
1328  *
1329  * Returns 0, this is not allowed to fail
1330  *
1331  * The close entry point is called when an interface is de-activated
1332  * by the OS.  The hardware is still under the drivers control, but
1333  * needs to be disabled.  A global MAC reset is issued to stop the
1334  * hardware, and all transmit and receive resources are freed.
1335  **/
1336
1337 static int
1338 e1000_close(struct net_device *netdev)
1339 {
1340         struct e1000_adapter *adapter = netdev_priv(netdev);
1341
1342         WARN_ON(test_bit(__E1000_RESETTING, &adapter->flags));
1343         e1000_down(adapter);
1344         e1000_power_down_phy(adapter);
1345         e1000_free_irq(adapter);
1346
1347         e1000_free_all_tx_resources(adapter);
1348         e1000_free_all_rx_resources(adapter);
1349
1350         /* kill manageability vlan ID if supported, but not if a vlan with
1351          * the same ID is registered on the host OS (let 8021q kill it) */
1352         if ((adapter->hw.mng_cookie.status &
1353                           E1000_MNG_DHCP_COOKIE_STATUS_VLAN_SUPPORT) &&
1354              !(adapter->vlgrp &&
1355                           adapter->vlgrp->vlan_devices[adapter->mng_vlan_id])) {
1356                 e1000_vlan_rx_kill_vid(netdev, adapter->mng_vlan_id);
1357         }
1358
1359         /* If AMT is enabled, let the firmware know that the network
1360          * interface is now closed */
1361         if (adapter->hw.mac_type == e1000_82573 &&
1362             e1000_check_mng_mode(&adapter->hw))
1363                 e1000_release_hw_control(adapter);
1364
1365         return 0;
1366 }
1367
1368 /**
1369  * e1000_check_64k_bound - check that memory doesn't cross 64kB boundary
1370  * @adapter: address of board private structure
1371  * @start: address of beginning of memory
1372  * @len: length of memory
1373  **/
1374 static boolean_t
1375 e1000_check_64k_bound(struct e1000_adapter *adapter,
1376                       void *start, unsigned long len)
1377 {
1378         unsigned long begin = (unsigned long) start;
1379         unsigned long end = begin + len;
1380
1381         /* First rev 82545 and 82546 need to not allow any memory
1382          * write location to cross 64k boundary due to errata 23 */
1383         if (adapter->hw.mac_type == e1000_82545 ||
1384             adapter->hw.mac_type == e1000_82546) {
1385                 return ((begin ^ (end - 1)) >> 16) != 0 ? FALSE : TRUE;
1386         }
1387
1388         return TRUE;
1389 }
1390
1391 /**
1392  * e1000_setup_tx_resources - allocate Tx resources (Descriptors)
1393  * @adapter: board private structure
1394  * @txdr:    tx descriptor ring (for a specific queue) to setup
1395  *
1396  * Return 0 on success, negative on failure
1397  **/
1398
1399 static int
1400 e1000_setup_tx_resources(struct e1000_adapter *adapter,
1401                          struct e1000_tx_ring *txdr)
1402 {
1403         struct pci_dev *pdev = adapter->pdev;
1404         int size;
1405
1406         size = sizeof(struct e1000_buffer) * txdr->count;
1407         txdr->buffer_info = vmalloc(size);
1408         if (!txdr->buffer_info) {
1409                 DPRINTK(PROBE, ERR,
1410                 "Unable to allocate memory for the transmit descriptor ring\n");
1411                 return -ENOMEM;
1412         }
1413         memset(txdr->buffer_info, 0, size);
1414
1415         /* round up to nearest 4K */
1416
1417         txdr->size = txdr->count * sizeof(struct e1000_tx_desc);
1418         E1000_ROUNDUP(txdr->size, 4096);
1419
1420         txdr->desc = pci_alloc_consistent(pdev, txdr->size, &txdr->dma);
1421         if (!txdr->desc) {
1422 setup_tx_desc_die:
1423                 vfree(txdr->buffer_info);
1424                 DPRINTK(PROBE, ERR,
1425                 "Unable to allocate memory for the transmit descriptor ring\n");
1426                 return -ENOMEM;
1427         }
1428
1429         /* Fix for errata 23, can't cross 64kB boundary */
1430         if (!e1000_check_64k_bound(adapter, txdr->desc, txdr->size)) {
1431                 void *olddesc = txdr->desc;
1432                 dma_addr_t olddma = txdr->dma;
1433                 DPRINTK(TX_ERR, ERR, "txdr align check failed: %u bytes "
1434                                      "at %p\n", txdr->size, txdr->desc);
1435                 /* Try again, without freeing the previous */
1436                 txdr->desc = pci_alloc_consistent(pdev, txdr->size, &txdr->dma);
1437                 /* Failed allocation, critical failure */
1438                 if (!txdr->desc) {
1439                         pci_free_consistent(pdev, txdr->size, olddesc, olddma);
1440                         goto setup_tx_desc_die;
1441                 }
1442
1443                 if (!e1000_check_64k_bound(adapter, txdr->desc, txdr->size)) {
1444                         /* give up */
1445                         pci_free_consistent(pdev, txdr->size, txdr->desc,
1446                                             txdr->dma);
1447                         pci_free_consistent(pdev, txdr->size, olddesc, olddma);
1448                         DPRINTK(PROBE, ERR,
1449                                 "Unable to allocate aligned memory "
1450                                 "for the transmit descriptor ring\n");
1451                         vfree(txdr->buffer_info);
1452                         return -ENOMEM;
1453                 } else {
1454                         /* Free old allocation, new allocation was successful */
1455                         pci_free_consistent(pdev, txdr->size, olddesc, olddma);
1456                 }
1457         }
1458         memset(txdr->desc, 0, txdr->size);
1459
1460         txdr->next_to_use = 0;
1461         txdr->next_to_clean = 0;
1462         spin_lock_init(&txdr->tx_lock);
1463
1464         return 0;
1465 }
1466
1467 /**
1468  * e1000_setup_all_tx_resources - wrapper to allocate Tx resources
1469  *                                (Descriptors) for all queues
1470  * @adapter: board private structure
1471  *
1472  * Return 0 on success, negative on failure
1473  **/
1474
1475 int
1476 e1000_setup_all_tx_resources(struct e1000_adapter *adapter)
1477 {
1478         int i, err = 0;
1479
1480         for (i = 0; i < adapter->num_tx_queues; i++) {
1481                 err = e1000_setup_tx_resources(adapter, &adapter->tx_ring[i]);
1482                 if (err) {
1483                         DPRINTK(PROBE, ERR,
1484                                 "Allocation for Tx Queue %u failed\n", i);
1485                         for (i-- ; i >= 0; i--)
1486                                 e1000_free_tx_resources(adapter,
1487                                                         &adapter->tx_ring[i]);
1488                         break;
1489                 }
1490         }
1491
1492         return err;
1493 }
1494
1495 /**
1496  * e1000_configure_tx - Configure 8254x Transmit Unit after Reset
1497  * @adapter: board private structure
1498  *
1499  * Configure the Tx unit of the MAC after a reset.
1500  **/
1501
1502 static void
1503 e1000_configure_tx(struct e1000_adapter *adapter)
1504 {
1505         uint64_t tdba;
1506         struct e1000_hw *hw = &adapter->hw;
1507         uint32_t tdlen, tctl, tipg, tarc;
1508         uint32_t ipgr1, ipgr2;
1509
1510         /* Setup the HW Tx Head and Tail descriptor pointers */
1511
1512         switch (adapter->num_tx_queues) {
1513         case 1:
1514         default:
1515                 tdba = adapter->tx_ring[0].dma;
1516                 tdlen = adapter->tx_ring[0].count *
1517                         sizeof(struct e1000_tx_desc);
1518                 E1000_WRITE_REG(hw, TDLEN, tdlen);
1519                 E1000_WRITE_REG(hw, TDBAH, (tdba >> 32));
1520                 E1000_WRITE_REG(hw, TDBAL, (tdba & 0x00000000ffffffffULL));
1521                 E1000_WRITE_REG(hw, TDT, 0);
1522                 E1000_WRITE_REG(hw, TDH, 0);
1523                 adapter->tx_ring[0].tdh = ((hw->mac_type >= e1000_82543) ? E1000_TDH : E1000_82542_TDH);
1524                 adapter->tx_ring[0].tdt = ((hw->mac_type >= e1000_82543) ? E1000_TDT : E1000_82542_TDT);
1525                 break;
1526         }
1527
1528         /* Set the default values for the Tx Inter Packet Gap timer */
1529
1530         if (hw->media_type == e1000_media_type_fiber ||
1531             hw->media_type == e1000_media_type_internal_serdes)
1532                 tipg = DEFAULT_82543_TIPG_IPGT_FIBER;
1533         else
1534                 tipg = DEFAULT_82543_TIPG_IPGT_COPPER;
1535
1536         switch (hw->mac_type) {
1537         case e1000_82542_rev2_0:
1538         case e1000_82542_rev2_1:
1539                 tipg = DEFAULT_82542_TIPG_IPGT;
1540                 ipgr1 = DEFAULT_82542_TIPG_IPGR1;
1541                 ipgr2 = DEFAULT_82542_TIPG_IPGR2;
1542                 break;
1543         case e1000_80003es2lan:
1544                 ipgr1 = DEFAULT_82543_TIPG_IPGR1;
1545                 ipgr2 = DEFAULT_80003ES2LAN_TIPG_IPGR2;
1546                 break;
1547         default:
1548                 ipgr1 = DEFAULT_82543_TIPG_IPGR1;
1549                 ipgr2 = DEFAULT_82543_TIPG_IPGR2;
1550                 break;
1551         }
1552         tipg |= ipgr1 << E1000_TIPG_IPGR1_SHIFT;
1553         tipg |= ipgr2 << E1000_TIPG_IPGR2_SHIFT;
1554         E1000_WRITE_REG(hw, TIPG, tipg);
1555
1556         /* Set the Tx Interrupt Delay register */
1557
1558         E1000_WRITE_REG(hw, TIDV, adapter->tx_int_delay);
1559         if (hw->mac_type >= e1000_82540)
1560                 E1000_WRITE_REG(hw, TADV, adapter->tx_abs_int_delay);
1561
1562         /* Program the Transmit Control Register */
1563
1564         tctl = E1000_READ_REG(hw, TCTL);
1565         tctl &= ~E1000_TCTL_CT;
1566         tctl |= E1000_TCTL_PSP | E1000_TCTL_RTLC |
1567                 (E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT);
1568
1569         if (hw->mac_type == e1000_82571 || hw->mac_type == e1000_82572) {
1570                 tarc = E1000_READ_REG(hw, TARC0);
1571                 tarc |= (1 << 21);
1572                 E1000_WRITE_REG(hw, TARC0, tarc);
1573         } else if (hw->mac_type == e1000_80003es2lan) {
1574                 tarc = E1000_READ_REG(hw, TARC0);
1575                 tarc |= 1;
1576                 E1000_WRITE_REG(hw, TARC0, tarc);
1577                 tarc = E1000_READ_REG(hw, TARC1);
1578                 tarc |= 1;
1579                 E1000_WRITE_REG(hw, TARC1, tarc);
1580         }
1581
1582         e1000_config_collision_dist(hw);
1583
1584         /* Setup Transmit Descriptor Settings for eop descriptor */
1585         adapter->txd_cmd = E1000_TXD_CMD_IDE | E1000_TXD_CMD_EOP |
1586                 E1000_TXD_CMD_IFCS;
1587
1588         if (hw->mac_type < e1000_82543)
1589                 adapter->txd_cmd |= E1000_TXD_CMD_RPS;
1590         else
1591                 adapter->txd_cmd |= E1000_TXD_CMD_RS;
1592
1593         /* Cache if we're 82544 running in PCI-X because we'll
1594          * need this to apply a workaround later in the send path. */
1595         if (hw->mac_type == e1000_82544 &&
1596             hw->bus_type == e1000_bus_type_pcix)
1597                 adapter->pcix_82544 = 1;
1598
1599         E1000_WRITE_REG(hw, TCTL, tctl);
1600
1601 }
1602
1603 /**
1604  * e1000_setup_rx_resources - allocate Rx resources (Descriptors)
1605  * @adapter: board private structure
1606  * @rxdr:    rx descriptor ring (for a specific queue) to setup
1607  *
1608  * Returns 0 on success, negative on failure
1609  **/
1610
1611 static int
1612 e1000_setup_rx_resources(struct e1000_adapter *adapter,
1613                          struct e1000_rx_ring *rxdr)
1614 {
1615         struct pci_dev *pdev = adapter->pdev;
1616         int size, desc_len;
1617
1618         size = sizeof(struct e1000_buffer) * rxdr->count;
1619         rxdr->buffer_info = vmalloc(size);
1620         if (!rxdr->buffer_info) {
1621                 DPRINTK(PROBE, ERR,
1622                 "Unable to allocate memory for the receive descriptor ring\n");
1623                 return -ENOMEM;
1624         }
1625         memset(rxdr->buffer_info, 0, size);
1626
1627         size = sizeof(struct e1000_ps_page) * rxdr->count;
1628         rxdr->ps_page = kmalloc(size, GFP_KERNEL);
1629         if (!rxdr->ps_page) {
1630                 vfree(rxdr->buffer_info);
1631                 DPRINTK(PROBE, ERR,
1632                 "Unable to allocate memory for the receive descriptor ring\n");
1633                 return -ENOMEM;
1634         }
1635         memset(rxdr->ps_page, 0, size);
1636
1637         size = sizeof(struct e1000_ps_page_dma) * rxdr->count;
1638         rxdr->ps_page_dma = kmalloc(size, GFP_KERNEL);
1639         if (!rxdr->ps_page_dma) {
1640                 vfree(rxdr->buffer_info);
1641                 kfree(rxdr->ps_page);
1642                 DPRINTK(PROBE, ERR,
1643                 "Unable to allocate memory for the receive descriptor ring\n");
1644                 return -ENOMEM;
1645         }
1646         memset(rxdr->ps_page_dma, 0, size);
1647
1648         if (adapter->hw.mac_type <= e1000_82547_rev_2)
1649                 desc_len = sizeof(struct e1000_rx_desc);
1650         else
1651                 desc_len = sizeof(union e1000_rx_desc_packet_split);
1652
1653         /* Round up to nearest 4K */
1654
1655         rxdr->size = rxdr->count * desc_len;
1656         E1000_ROUNDUP(rxdr->size, 4096);
1657
1658         rxdr->desc = pci_alloc_consistent(pdev, rxdr->size, &rxdr->dma);
1659
1660         if (!rxdr->desc) {
1661                 DPRINTK(PROBE, ERR,
1662                 "Unable to allocate memory for the receive descriptor ring\n");
1663 setup_rx_desc_die:
1664                 vfree(rxdr->buffer_info);
1665                 kfree(rxdr->ps_page);
1666                 kfree(rxdr->ps_page_dma);
1667                 return -ENOMEM;
1668         }
1669
1670         /* Fix for errata 23, can't cross 64kB boundary */
1671         if (!e1000_check_64k_bound(adapter, rxdr->desc, rxdr->size)) {
1672                 void *olddesc = rxdr->desc;
1673                 dma_addr_t olddma = rxdr->dma;
1674                 DPRINTK(RX_ERR, ERR, "rxdr align check failed: %u bytes "
1675                                      "at %p\n", rxdr->size, rxdr->desc);
1676                 /* Try again, without freeing the previous */
1677                 rxdr->desc = pci_alloc_consistent(pdev, rxdr->size, &rxdr->dma);
1678                 /* Failed allocation, critical failure */
1679                 if (!rxdr->desc) {
1680                         pci_free_consistent(pdev, rxdr->size, olddesc, olddma);
1681                         DPRINTK(PROBE, ERR,
1682                                 "Unable to allocate memory "
1683                                 "for the receive descriptor ring\n");
1684                         goto setup_rx_desc_die;
1685                 }
1686
1687                 if (!e1000_check_64k_bound(adapter, rxdr->desc, rxdr->size)) {
1688                         /* give up */
1689                         pci_free_consistent(pdev, rxdr->size, rxdr->desc,
1690                                             rxdr->dma);
1691                         pci_free_consistent(pdev, rxdr->size, olddesc, olddma);
1692                         DPRINTK(PROBE, ERR,
1693                                 "Unable to allocate aligned memory "
1694                                 "for the receive descriptor ring\n");
1695                         goto setup_rx_desc_die;
1696                 } else {
1697                         /* Free old allocation, new allocation was successful */
1698                         pci_free_consistent(pdev, rxdr->size, olddesc, olddma);
1699                 }
1700         }
1701         memset(rxdr->desc, 0, rxdr->size);
1702
1703         rxdr->next_to_clean = 0;
1704         rxdr->next_to_use = 0;
1705
1706         return 0;
1707 }
1708
1709 /**
1710  * e1000_setup_all_rx_resources - wrapper to allocate Rx resources
1711  *                                (Descriptors) for all queues
1712  * @adapter: board private structure
1713  *
1714  * Return 0 on success, negative on failure
1715  **/
1716
1717 int
1718 e1000_setup_all_rx_resources(struct e1000_adapter *adapter)
1719 {
1720         int i, err = 0;
1721
1722         for (i = 0; i < adapter->num_rx_queues; i++) {
1723                 err = e1000_setup_rx_resources(adapter, &adapter->rx_ring[i]);
1724                 if (err) {
1725                         DPRINTK(PROBE, ERR,
1726                                 "Allocation for Rx Queue %u failed\n", i);
1727                         for (i-- ; i >= 0; i--)
1728                                 e1000_free_rx_resources(adapter,
1729                                                         &adapter->rx_ring[i]);
1730                         break;
1731                 }
1732         }
1733
1734         return err;
1735 }
1736
1737 /**
1738  * e1000_setup_rctl - configure the receive control registers
1739  * @adapter: Board private structure
1740  **/
1741 #define PAGE_USE_COUNT(S) (((S) >> PAGE_SHIFT) + \
1742                         (((S) & (PAGE_SIZE - 1)) ? 1 : 0))
1743 static void
1744 e1000_setup_rctl(struct e1000_adapter *adapter)
1745 {
1746         uint32_t rctl, rfctl;
1747         uint32_t psrctl = 0;
1748 #ifndef CONFIG_E1000_DISABLE_PACKET_SPLIT
1749         uint32_t pages = 0;
1750 #endif
1751
1752         rctl = E1000_READ_REG(&adapter->hw, RCTL);
1753
1754         rctl &= ~(3 << E1000_RCTL_MO_SHIFT);
1755
1756         rctl |= E1000_RCTL_EN | E1000_RCTL_BAM |
1757                 E1000_RCTL_LBM_NO | E1000_RCTL_RDMTS_HALF |
1758                 (adapter->hw.mc_filter_type << E1000_RCTL_MO_SHIFT);
1759
1760         if (adapter->hw.tbi_compatibility_on == 1)
1761                 rctl |= E1000_RCTL_SBP;
1762         else
1763                 rctl &= ~E1000_RCTL_SBP;
1764
1765         if (adapter->netdev->mtu <= ETH_DATA_LEN)
1766                 rctl &= ~E1000_RCTL_LPE;
1767         else
1768                 rctl |= E1000_RCTL_LPE;
1769
1770         /* Setup buffer sizes */
1771         rctl &= ~E1000_RCTL_SZ_4096;
1772         rctl |= E1000_RCTL_BSEX;
1773         switch (adapter->rx_buffer_len) {
1774                 case E1000_RXBUFFER_256:
1775                         rctl |= E1000_RCTL_SZ_256;
1776                         rctl &= ~E1000_RCTL_BSEX;
1777                         break;
1778                 case E1000_RXBUFFER_512:
1779                         rctl |= E1000_RCTL_SZ_512;
1780                         rctl &= ~E1000_RCTL_BSEX;
1781                         break;
1782                 case E1000_RXBUFFER_1024:
1783                         rctl |= E1000_RCTL_SZ_1024;
1784                         rctl &= ~E1000_RCTL_BSEX;
1785                         break;
1786                 case E1000_RXBUFFER_2048:
1787                 default:
1788                         rctl |= E1000_RCTL_SZ_2048;
1789                         rctl &= ~E1000_RCTL_BSEX;
1790                         break;
1791                 case E1000_RXBUFFER_4096:
1792                         rctl |= E1000_RCTL_SZ_4096;
1793                         break;
1794                 case E1000_RXBUFFER_8192:
1795                         rctl |= E1000_RCTL_SZ_8192;
1796                         break;
1797                 case E1000_RXBUFFER_16384:
1798                         rctl |= E1000_RCTL_SZ_16384;
1799                         break;
1800         }
1801
1802 #ifndef CONFIG_E1000_DISABLE_PACKET_SPLIT
1803         /* 82571 and greater support packet-split where the protocol
1804          * header is placed in skb->data and the packet data is
1805          * placed in pages hanging off of skb_shinfo(skb)->nr_frags.
1806          * In the case of a non-split, skb->data is linearly filled,
1807          * followed by the page buffers.  Therefore, skb->data is
1808          * sized to hold the largest protocol header.
1809          */
1810         /* allocations using alloc_page take too long for regular MTU
1811          * so only enable packet split for jumbo frames */
1812         pages = PAGE_USE_COUNT(adapter->netdev->mtu);
1813         if ((adapter->hw.mac_type >= e1000_82571) && (pages <= 3) &&
1814             PAGE_SIZE <= 16384 && (rctl & E1000_RCTL_LPE))
1815                 adapter->rx_ps_pages = pages;
1816         else
1817                 adapter->rx_ps_pages = 0;
1818 #endif
1819         if (adapter->rx_ps_pages) {
1820                 /* Configure extra packet-split registers */
1821                 rfctl = E1000_READ_REG(&adapter->hw, RFCTL);
1822                 rfctl |= E1000_RFCTL_EXTEN;
1823                 /* disable IPv6 packet split support */
1824                 rfctl |= E1000_RFCTL_IPV6_DIS;
1825                 E1000_WRITE_REG(&adapter->hw, RFCTL, rfctl);
1826
1827                 rctl |= E1000_RCTL_DTYP_PS;
1828
1829                 psrctl |= adapter->rx_ps_bsize0 >>
1830                         E1000_PSRCTL_BSIZE0_SHIFT;
1831
1832                 switch (adapter->rx_ps_pages) {
1833                 case 3:
1834                         psrctl |= PAGE_SIZE <<
1835                                 E1000_PSRCTL_BSIZE3_SHIFT;
1836                 case 2:
1837                         psrctl |= PAGE_SIZE <<
1838                                 E1000_PSRCTL_BSIZE2_SHIFT;
1839                 case 1:
1840                         psrctl |= PAGE_SIZE >>
1841                                 E1000_PSRCTL_BSIZE1_SHIFT;
1842                         break;
1843                 }
1844
1845                 E1000_WRITE_REG(&adapter->hw, PSRCTL, psrctl);
1846         }
1847
1848         E1000_WRITE_REG(&adapter->hw, RCTL, rctl);
1849 }
1850
1851 /**
1852  * e1000_configure_rx - Configure 8254x Receive Unit after Reset
1853  * @adapter: board private structure
1854  *
1855  * Configure the Rx unit of the MAC after a reset.
1856  **/
1857
1858 static void
1859 e1000_configure_rx(struct e1000_adapter *adapter)
1860 {
1861         uint64_t rdba;
1862         struct e1000_hw *hw = &adapter->hw;
1863         uint32_t rdlen, rctl, rxcsum, ctrl_ext;
1864
1865         if (adapter->rx_ps_pages) {
1866                 /* this is a 32 byte descriptor */
1867                 rdlen = adapter->rx_ring[0].count *
1868                         sizeof(union e1000_rx_desc_packet_split);
1869                 adapter->clean_rx = e1000_clean_rx_irq_ps;
1870                 adapter->alloc_rx_buf = e1000_alloc_rx_buffers_ps;
1871         } else {
1872                 rdlen = adapter->rx_ring[0].count *
1873                         sizeof(struct e1000_rx_desc);
1874                 adapter->clean_rx = e1000_clean_rx_irq;
1875                 adapter->alloc_rx_buf = e1000_alloc_rx_buffers;
1876         }
1877
1878         /* disable receives while setting up the descriptors */
1879         rctl = E1000_READ_REG(hw, RCTL);
1880         E1000_WRITE_REG(hw, RCTL, rctl & ~E1000_RCTL_EN);
1881
1882         /* set the Receive Delay Timer Register */
1883         E1000_WRITE_REG(hw, RDTR, adapter->rx_int_delay);
1884
1885         if (hw->mac_type >= e1000_82540) {
1886                 E1000_WRITE_REG(hw, RADV, adapter->rx_abs_int_delay);
1887                 if (adapter->itr > 1)
1888                         E1000_WRITE_REG(hw, ITR,
1889                                 1000000000 / (adapter->itr * 256));
1890         }
1891
1892         if (hw->mac_type >= e1000_82571) {
1893                 ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
1894                 /* Reset delay timers after every interrupt */
1895                 ctrl_ext |= E1000_CTRL_EXT_INT_TIMER_CLR;
1896 #ifdef CONFIG_E1000_NAPI
1897                 /* Auto-Mask interrupts upon ICR read. */
1898                 ctrl_ext |= E1000_CTRL_EXT_IAME;
1899 #endif
1900                 E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
1901                 E1000_WRITE_REG(hw, IAM, ~0);
1902                 E1000_WRITE_FLUSH(hw);
1903         }
1904
1905         /* Setup the HW Rx Head and Tail Descriptor Pointers and
1906          * the Base and Length of the Rx Descriptor Ring */
1907         switch (adapter->num_rx_queues) {
1908         case 1:
1909         default:
1910                 rdba = adapter->rx_ring[0].dma;
1911                 E1000_WRITE_REG(hw, RDLEN, rdlen);
1912                 E1000_WRITE_REG(hw, RDBAH, (rdba >> 32));
1913                 E1000_WRITE_REG(hw, RDBAL, (rdba & 0x00000000ffffffffULL));
1914                 E1000_WRITE_REG(hw, RDT, 0);
1915                 E1000_WRITE_REG(hw, RDH, 0);
1916                 adapter->rx_ring[0].rdh = ((hw->mac_type >= e1000_82543) ? E1000_RDH : E1000_82542_RDH);
1917                 adapter->rx_ring[0].rdt = ((hw->mac_type >= e1000_82543) ? E1000_RDT : E1000_82542_RDT);
1918                 break;
1919         }
1920
1921         /* Enable 82543 Receive Checksum Offload for TCP and UDP */
1922         if (hw->mac_type >= e1000_82543) {
1923                 rxcsum = E1000_READ_REG(hw, RXCSUM);
1924                 if (adapter->rx_csum == TRUE) {
1925                         rxcsum |= E1000_RXCSUM_TUOFL;
1926
1927                         /* Enable 82571 IPv4 payload checksum for UDP fragments
1928                          * Must be used in conjunction with packet-split. */
1929                         if ((hw->mac_type >= e1000_82571) &&
1930                             (adapter->rx_ps_pages)) {
1931                                 rxcsum |= E1000_RXCSUM_IPPCSE;
1932                         }
1933                 } else {
1934                         rxcsum &= ~E1000_RXCSUM_TUOFL;
1935                         /* don't need to clear IPPCSE as it defaults to 0 */
1936                 }
1937                 E1000_WRITE_REG(hw, RXCSUM, rxcsum);
1938         }
1939
1940         /* Enable Receives */
1941         E1000_WRITE_REG(hw, RCTL, rctl);
1942 }
1943
1944 /**
1945  * e1000_free_tx_resources - Free Tx Resources per Queue
1946  * @adapter: board private structure
1947  * @tx_ring: Tx descriptor ring for a specific queue
1948  *
1949  * Free all transmit software resources
1950  **/
1951
1952 static void
1953 e1000_free_tx_resources(struct e1000_adapter *adapter,
1954                         struct e1000_tx_ring *tx_ring)
1955 {
1956         struct pci_dev *pdev = adapter->pdev;
1957
1958         e1000_clean_tx_ring(adapter, tx_ring);
1959
1960         vfree(tx_ring->buffer_info);
1961         tx_ring->buffer_info = NULL;
1962
1963         pci_free_consistent(pdev, tx_ring->size, tx_ring->desc, tx_ring->dma);
1964
1965         tx_ring->desc = NULL;
1966 }
1967
1968 /**
1969  * e1000_free_all_tx_resources - Free Tx Resources for All Queues
1970  * @adapter: board private structure
1971  *
1972  * Free all transmit software resources
1973  **/
1974
1975 void
1976 e1000_free_all_tx_resources(struct e1000_adapter *adapter)
1977 {
1978         int i;
1979
1980         for (i = 0; i < adapter->num_tx_queues; i++)
1981                 e1000_free_tx_resources(adapter, &adapter->tx_ring[i]);
1982 }
1983
1984 static void
1985 e1000_unmap_and_free_tx_resource(struct e1000_adapter *adapter,
1986                         struct e1000_buffer *buffer_info)
1987 {
1988         if (buffer_info->dma) {
1989                 pci_unmap_page(adapter->pdev,
1990                                 buffer_info->dma,
1991                                 buffer_info->length,
1992                                 PCI_DMA_TODEVICE);
1993         }
1994         if (buffer_info->skb)
1995                 dev_kfree_skb_any(buffer_info->skb);
1996         memset(buffer_info, 0, sizeof(struct e1000_buffer));
1997 }
1998
1999 /**
2000  * e1000_clean_tx_ring - Free Tx Buffers
2001  * @adapter: board private structure
2002  * @tx_ring: ring to be cleaned
2003  **/
2004
2005 static void
2006 e1000_clean_tx_ring(struct e1000_adapter *adapter,
2007                     struct e1000_tx_ring *tx_ring)
2008 {
2009         struct e1000_buffer *buffer_info;
2010         unsigned long size;
2011         unsigned int i;
2012
2013         /* Free all the Tx ring sk_buffs */
2014
2015         for (i = 0; i < tx_ring->count; i++) {
2016                 buffer_info = &tx_ring->buffer_info[i];
2017                 e1000_unmap_and_free_tx_resource(adapter, buffer_info);
2018         }
2019
2020         size = sizeof(struct e1000_buffer) * tx_ring->count;
2021         memset(tx_ring->buffer_info, 0, size);
2022
2023         /* Zero out the descriptor ring */
2024
2025         memset(tx_ring->desc, 0, tx_ring->size);
2026
2027         tx_ring->next_to_use = 0;
2028         tx_ring->next_to_clean = 0;
2029         tx_ring->last_tx_tso = 0;
2030
2031         writel(0, adapter->hw.hw_addr + tx_ring->tdh);
2032         writel(0, adapter->hw.hw_addr + tx_ring->tdt);
2033 }
2034
2035 /**
2036  * e1000_clean_all_tx_rings - Free Tx Buffers for all queues
2037  * @adapter: board private structure
2038  **/
2039
2040 static void
2041 e1000_clean_all_tx_rings(struct e1000_adapter *adapter)
2042 {
2043         int i;
2044
2045         for (i = 0; i < adapter->num_tx_queues; i++)
2046                 e1000_clean_tx_ring(adapter, &adapter->tx_ring[i]);
2047 }
2048
2049 /**
2050  * e1000_free_rx_resources - Free Rx Resources
2051  * @adapter: board private structure
2052  * @rx_ring: ring to clean the resources from
2053  *
2054  * Free all receive software resources
2055  **/
2056
2057 static void
2058 e1000_free_rx_resources(struct e1000_adapter *adapter,
2059                         struct e1000_rx_ring *rx_ring)
2060 {
2061         struct pci_dev *pdev = adapter->pdev;
2062
2063         e1000_clean_rx_ring(adapter, rx_ring);
2064
2065         vfree(rx_ring->buffer_info);
2066         rx_ring->buffer_info = NULL;
2067         kfree(rx_ring->ps_page);
2068         rx_ring->ps_page = NULL;
2069         kfree(rx_ring->ps_page_dma);
2070         rx_ring->ps_page_dma = NULL;
2071
2072         pci_free_consistent(pdev, rx_ring->size, rx_ring->desc, rx_ring->dma);
2073
2074         rx_ring->desc = NULL;
2075 }
2076
2077 /**
2078  * e1000_free_all_rx_resources - Free Rx Resources for All Queues
2079  * @adapter: board private structure
2080  *
2081  * Free all receive software resources
2082  **/
2083
2084 void
2085 e1000_free_all_rx_resources(struct e1000_adapter *adapter)
2086 {
2087         int i;
2088
2089         for (i = 0; i < adapter->num_rx_queues; i++)
2090                 e1000_free_rx_resources(adapter, &adapter->rx_ring[i]);
2091 }
2092
2093 /**
2094  * e1000_clean_rx_ring - Free Rx Buffers per Queue
2095  * @adapter: board private structure
2096  * @rx_ring: ring to free buffers from
2097  **/
2098
2099 static void
2100 e1000_clean_rx_ring(struct e1000_adapter *adapter,
2101                     struct e1000_rx_ring *rx_ring)
2102 {
2103         struct e1000_buffer *buffer_info;
2104         struct e1000_ps_page *ps_page;
2105         struct e1000_ps_page_dma *ps_page_dma;
2106         struct pci_dev *pdev = adapter->pdev;
2107         unsigned long size;
2108         unsigned int i, j;
2109
2110         /* Free all the Rx ring sk_buffs */
2111         for (i = 0; i < rx_ring->count; i++) {
2112                 buffer_info = &rx_ring->buffer_info[i];
2113                 if (buffer_info->skb) {
2114                         pci_unmap_single(pdev,
2115                                          buffer_info->dma,
2116                                          buffer_info->length,
2117                                          PCI_DMA_FROMDEVICE);
2118
2119                         dev_kfree_skb(buffer_info->skb);
2120                         buffer_info->skb = NULL;
2121                 }
2122                 ps_page = &rx_ring->ps_page[i];
2123                 ps_page_dma = &rx_ring->ps_page_dma[i];
2124                 for (j = 0; j < adapter->rx_ps_pages; j++) {
2125                         if (!ps_page->ps_page[j]) break;
2126                         pci_unmap_page(pdev,
2127                                        ps_page_dma->ps_page_dma[j],
2128                                        PAGE_SIZE, PCI_DMA_FROMDEVICE);
2129                         ps_page_dma->ps_page_dma[j] = 0;
2130                         put_page(ps_page->ps_page[j]);
2131                         ps_page->ps_page[j] = NULL;
2132                 }
2133         }
2134
2135         size = sizeof(struct e1000_buffer) * rx_ring->count;
2136         memset(rx_ring->buffer_info, 0, size);
2137         size = sizeof(struct e1000_ps_page) * rx_ring->count;
2138         memset(rx_ring->ps_page, 0, size);
2139         size = sizeof(struct e1000_ps_page_dma) * rx_ring->count;
2140         memset(rx_ring->ps_page_dma, 0, size);
2141
2142         /* Zero out the descriptor ring */
2143
2144         memset(rx_ring->desc, 0, rx_ring->size);
2145
2146         rx_ring->next_to_clean = 0;
2147         rx_ring->next_to_use = 0;
2148
2149         writel(0, adapter->hw.hw_addr + rx_ring->rdh);
2150         writel(0, adapter->hw.hw_addr + rx_ring->rdt);
2151 }
2152
2153 /**
2154  * e1000_clean_all_rx_rings - Free Rx Buffers for all queues
2155  * @adapter: board private structure
2156  **/
2157
2158 static void
2159 e1000_clean_all_rx_rings(struct e1000_adapter *adapter)
2160 {
2161         int i;
2162
2163         for (i = 0; i < adapter->num_rx_queues; i++)
2164                 e1000_clean_rx_ring(adapter, &adapter->rx_ring[i]);
2165 }
2166
2167 /* The 82542 2.0 (revision 2) needs to have the receive unit in reset
2168  * and memory write and invalidate disabled for certain operations
2169  */
2170 static void
2171 e1000_enter_82542_rst(struct e1000_adapter *adapter)
2172 {
2173         struct net_device *netdev = adapter->netdev;
2174         uint32_t rctl;
2175
2176         e1000_pci_clear_mwi(&adapter->hw);
2177
2178         rctl = E1000_READ_REG(&adapter->hw, RCTL);
2179         rctl |= E1000_RCTL_RST;
2180         E1000_WRITE_REG(&adapter->hw, RCTL, rctl);
2181         E1000_WRITE_FLUSH(&adapter->hw);
2182         mdelay(5);
2183
2184         if (netif_running(netdev))
2185                 e1000_clean_all_rx_rings(adapter);
2186 }
2187
2188 static void
2189 e1000_leave_82542_rst(struct e1000_adapter *adapter)
2190 {
2191         struct net_device *netdev = adapter->netdev;
2192         uint32_t rctl;
2193
2194         rctl = E1000_READ_REG(&adapter->hw, RCTL);
2195         rctl &= ~E1000_RCTL_RST;
2196         E1000_WRITE_REG(&adapter->hw, RCTL, rctl);
2197         E1000_WRITE_FLUSH(&adapter->hw);
2198         mdelay(5);
2199
2200         if (adapter->hw.pci_cmd_word & PCI_COMMAND_INVALIDATE)
2201                 e1000_pci_set_mwi(&adapter->hw);
2202
2203         if (netif_running(netdev)) {
2204                 /* No need to loop, because 82542 supports only 1 queue */
2205                 struct e1000_rx_ring *ring = &adapter->rx_ring[0];
2206                 e1000_configure_rx(adapter);
2207                 adapter->alloc_rx_buf(adapter, ring, E1000_DESC_UNUSED(ring));
2208         }
2209 }
2210
2211 /**
2212  * e1000_set_mac - Change the Ethernet Address of the NIC
2213  * @netdev: network interface device structure
2214  * @p: pointer to an address structure
2215  *
2216  * Returns 0 on success, negative on failure
2217  **/
2218
2219 static int
2220 e1000_set_mac(struct net_device *netdev, void *p)
2221 {
2222         struct e1000_adapter *adapter = netdev_priv(netdev);
2223         struct sockaddr *addr = p;
2224
2225         if (!is_valid_ether_addr(addr->sa_data))
2226                 return -EADDRNOTAVAIL;
2227
2228         /* 82542 2.0 needs to be in reset to write receive address registers */
2229
2230         if (adapter->hw.mac_type == e1000_82542_rev2_0)
2231                 e1000_enter_82542_rst(adapter);
2232
2233         memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
2234         memcpy(adapter->hw.mac_addr, addr->sa_data, netdev->addr_len);
2235
2236         e1000_rar_set(&adapter->hw, adapter->hw.mac_addr, 0);
2237
2238         /* With 82571 controllers, LAA may be overwritten (with the default)
2239          * due to controller reset from the other port. */
2240         if (adapter->hw.mac_type == e1000_82571) {
2241                 /* activate the work around */
2242                 adapter->hw.laa_is_present = 1;
2243
2244                 /* Hold a copy of the LAA in RAR[14] This is done so that
2245                  * between the time RAR[0] gets clobbered  and the time it
2246                  * gets fixed (in e1000_watchdog), the actual LAA is in one
2247                  * of the RARs and no incoming packets directed to this port
2248                  * are dropped. Eventaully the LAA will be in RAR[0] and
2249                  * RAR[14] */
2250                 e1000_rar_set(&adapter->hw, adapter->hw.mac_addr,
2251                                         E1000_RAR_ENTRIES - 1);
2252         }
2253
2254         if (adapter->hw.mac_type == e1000_82542_rev2_0)
2255                 e1000_leave_82542_rst(adapter);
2256
2257         return 0;
2258 }
2259
2260 /**
2261  * e1000_set_multi - Multicast and Promiscuous mode set
2262  * @netdev: network interface device structure
2263  *
2264  * The set_multi entry point is called whenever the multicast address
2265  * list or the network interface flags are updated.  This routine is
2266  * responsible for configuring the hardware for proper multicast,
2267  * promiscuous mode, and all-multi behavior.
2268  **/
2269
2270 static void
2271 e1000_set_multi(struct net_device *netdev)
2272 {
2273         struct e1000_adapter *adapter = netdev_priv(netdev);
2274         struct e1000_hw *hw = &adapter->hw;
2275         struct dev_mc_list *mc_ptr;
2276         uint32_t rctl;
2277         uint32_t hash_value;
2278         int i, rar_entries = E1000_RAR_ENTRIES;
2279         int mta_reg_count = (hw->mac_type == e1000_ich8lan) ?
2280                                 E1000_NUM_MTA_REGISTERS_ICH8LAN :
2281                                 E1000_NUM_MTA_REGISTERS;
2282
2283         if (adapter->hw.mac_type == e1000_ich8lan)
2284                 rar_entries = E1000_RAR_ENTRIES_ICH8LAN;
2285
2286         /* reserve RAR[14] for LAA over-write work-around */
2287         if (adapter->hw.mac_type == e1000_82571)
2288                 rar_entries--;
2289
2290         /* Check for Promiscuous and All Multicast modes */
2291
2292         rctl = E1000_READ_REG(hw, RCTL);
2293
2294         if (netdev->flags & IFF_PROMISC) {
2295                 rctl |= (E1000_RCTL_UPE | E1000_RCTL_MPE);
2296         } else if (netdev->flags & IFF_ALLMULTI) {
2297                 rctl |= E1000_RCTL_MPE;
2298                 rctl &= ~E1000_RCTL_UPE;
2299         } else {
2300                 rctl &= ~(E1000_RCTL_UPE | E1000_RCTL_MPE);
2301         }
2302
2303         E1000_WRITE_REG(hw, RCTL, rctl);
2304
2305         /* 82542 2.0 needs to be in reset to write receive address registers */
2306
2307         if (hw->mac_type == e1000_82542_rev2_0)
2308                 e1000_enter_82542_rst(adapter);
2309
2310         /* load the first 14 multicast address into the exact filters 1-14
2311          * RAR 0 is used for the station MAC adddress
2312          * if there are not 14 addresses, go ahead and clear the filters
2313          * -- with 82571 controllers only 0-13 entries are filled here
2314          */
2315         mc_ptr = netdev->mc_list;
2316
2317         for (i = 1; i < rar_entries; i++) {
2318                 if (mc_ptr) {
2319                         e1000_rar_set(hw, mc_ptr->dmi_addr, i);
2320                         mc_ptr = mc_ptr->next;
2321                 } else {
2322                         E1000_WRITE_REG_ARRAY(hw, RA, i << 1, 0);
2323                         E1000_WRITE_FLUSH(hw);
2324                         E1000_WRITE_REG_ARRAY(hw, RA, (i << 1) + 1, 0);
2325                         E1000_WRITE_FLUSH(hw);
2326                 }
2327         }
2328
2329         /* clear the old settings from the multicast hash table */
2330
2331         for (i = 0; i < mta_reg_count; i++) {
2332                 E1000_WRITE_REG_ARRAY(hw, MTA, i, 0);
2333                 E1000_WRITE_FLUSH(hw);
2334         }
2335
2336         /* load any remaining addresses into the hash table */
2337
2338         for (; mc_ptr; mc_ptr = mc_ptr->next) {
2339                 hash_value = e1000_hash_mc_addr(hw, mc_ptr->dmi_addr);
2340                 e1000_mta_set(hw, hash_value);
2341         }
2342
2343         if (hw->mac_type == e1000_82542_rev2_0)
2344                 e1000_leave_82542_rst(adapter);
2345 }
2346
2347 /* Need to wait a few seconds after link up to get diagnostic information from
2348  * the phy */
2349
2350 static void
2351 e1000_update_phy_info(unsigned long data)
2352 {
2353         struct e1000_adapter *adapter = (struct e1000_adapter *) data;
2354         e1000_phy_get_info(&adapter->hw, &adapter->phy_info);
2355 }
2356
2357 /**
2358  * e1000_82547_tx_fifo_stall - Timer Call-back
2359  * @data: pointer to adapter cast into an unsigned long
2360  **/
2361
2362 static void
2363 e1000_82547_tx_fifo_stall(unsigned long data)
2364 {
2365         struct e1000_adapter *adapter = (struct e1000_adapter *) data;
2366         struct net_device *netdev = adapter->netdev;
2367         uint32_t tctl;
2368
2369         if (atomic_read(&adapter->tx_fifo_stall)) {
2370                 if ((E1000_READ_REG(&adapter->hw, TDT) ==
2371                     E1000_READ_REG(&adapter->hw, TDH)) &&
2372                    (E1000_READ_REG(&adapter->hw, TDFT) ==
2373                     E1000_READ_REG(&adapter->hw, TDFH)) &&
2374                    (E1000_READ_REG(&adapter->hw, TDFTS) ==
2375                     E1000_READ_REG(&adapter->hw, TDFHS))) {
2376                         tctl = E1000_READ_REG(&adapter->hw, TCTL);
2377                         E1000_WRITE_REG(&adapter->hw, TCTL,
2378                                         tctl & ~E1000_TCTL_EN);
2379                         E1000_WRITE_REG(&adapter->hw, TDFT,
2380                                         adapter->tx_head_addr);
2381                         E1000_WRITE_REG(&adapter->hw, TDFH,
2382                                         adapter->tx_head_addr);
2383                         E1000_WRITE_REG(&adapter->hw, TDFTS,
2384                                         adapter->tx_head_addr);
2385                         E1000_WRITE_REG(&adapter->hw, TDFHS,
2386                                         adapter->tx_head_addr);
2387                         E1000_WRITE_REG(&adapter->hw, TCTL, tctl);
2388                         E1000_WRITE_FLUSH(&adapter->hw);
2389
2390                         adapter->tx_fifo_head = 0;
2391                         atomic_set(&adapter->tx_fifo_stall, 0);
2392                         netif_wake_queue(netdev);
2393                 } else {
2394                         mod_timer(&adapter->tx_fifo_stall_timer, jiffies + 1);
2395                 }
2396         }
2397 }
2398
2399 /**
2400  * e1000_watchdog - Timer Call-back
2401  * @data: pointer to adapter cast into an unsigned long
2402  **/
2403 static void
2404 e1000_watchdog(unsigned long data)
2405 {
2406         struct e1000_adapter *adapter = (struct e1000_adapter *) data;
2407         struct net_device *netdev = adapter->netdev;
2408         struct e1000_tx_ring *txdr = adapter->tx_ring;
2409         uint32_t link, tctl;
2410         int32_t ret_val;
2411
2412         ret_val = e1000_check_for_link(&adapter->hw);
2413         if ((ret_val == E1000_ERR_PHY) &&
2414             (adapter->hw.phy_type == e1000_phy_igp_3) &&
2415             (E1000_READ_REG(&adapter->hw, CTRL) & E1000_PHY_CTRL_GBE_DISABLE)) {
2416                 /* See e1000_kumeran_lock_loss_workaround() */
2417                 DPRINTK(LINK, INFO,
2418                         "Gigabit has been disabled, downgrading speed\n");
2419         }
2420         if (adapter->hw.mac_type == e1000_82573) {
2421                 e1000_enable_tx_pkt_filtering(&adapter->hw);
2422                 if (adapter->mng_vlan_id != adapter->hw.mng_cookie.vlan_id)
2423                         e1000_update_mng_vlan(adapter);
2424         }
2425
2426         if ((adapter->hw.media_type == e1000_media_type_internal_serdes) &&
2427            !(E1000_READ_REG(&adapter->hw, TXCW) & E1000_TXCW_ANE))
2428                 link = !adapter->hw.serdes_link_down;
2429         else
2430                 link = E1000_READ_REG(&adapter->hw, STATUS) & E1000_STATUS_LU;
2431
2432         if (link) {
2433                 if (!netif_carrier_ok(netdev)) {
2434                         boolean_t txb2b = 1;
2435                         e1000_get_speed_and_duplex(&adapter->hw,
2436                                                    &adapter->link_speed,
2437                                                    &adapter->link_duplex);
2438
2439                         DPRINTK(LINK, INFO, "NIC Link is Up %d Mbps %s\n",
2440                                adapter->link_speed,
2441                                adapter->link_duplex == FULL_DUPLEX ?
2442                                "Full Duplex" : "Half Duplex");
2443
2444                         /* tweak tx_queue_len according to speed/duplex
2445                          * and adjust the timeout factor */
2446                         netdev->tx_queue_len = adapter->tx_queue_len;
2447                         adapter->tx_timeout_factor = 1;
2448                         switch (adapter->link_speed) {
2449                         case SPEED_10:
2450                                 txb2b = 0;
2451                                 netdev->tx_queue_len = 10;
2452                                 adapter->tx_timeout_factor = 8;
2453                                 break;
2454                         case SPEED_100:
2455                                 txb2b = 0;
2456                                 netdev->tx_queue_len = 100;
2457                                 /* maybe add some timeout factor ? */
2458                                 break;
2459                         }
2460
2461                         if ((adapter->hw.mac_type == e1000_82571 ||
2462                              adapter->hw.mac_type == e1000_82572) &&
2463                             txb2b == 0) {
2464 #define SPEED_MODE_BIT (1 << 21)
2465                                 uint32_t tarc0;
2466                                 tarc0 = E1000_READ_REG(&adapter->hw, TARC0);
2467                                 tarc0 &= ~SPEED_MODE_BIT;
2468                                 E1000_WRITE_REG(&adapter->hw, TARC0, tarc0);
2469                         }
2470                                 
2471 #ifdef NETIF_F_TSO
2472                         /* disable TSO for pcie and 10/100 speeds, to avoid
2473                          * some hardware issues */
2474                         if (!adapter->tso_force &&
2475                             adapter->hw.bus_type == e1000_bus_type_pci_express){
2476                                 switch (adapter->link_speed) {
2477                                 case SPEED_10:
2478                                 case SPEED_100:
2479                                         DPRINTK(PROBE,INFO,
2480                                         "10/100 speed: disabling TSO\n");
2481                                         netdev->features &= ~NETIF_F_TSO;
2482                                         break;
2483                                 case SPEED_1000:
2484                                         netdev->features |= NETIF_F_TSO;
2485                                         break;
2486                                 default:
2487                                         /* oops */
2488                                         break;
2489                                 }
2490                         }
2491 #endif
2492
2493                         /* enable transmits in the hardware, need to do this
2494                          * after setting TARC0 */
2495                         tctl = E1000_READ_REG(&adapter->hw, TCTL);
2496                         tctl |= E1000_TCTL_EN;
2497                         E1000_WRITE_REG(&adapter->hw, TCTL, tctl);
2498
2499                         netif_carrier_on(netdev);
2500                         netif_wake_queue(netdev);
2501                         mod_timer(&adapter->phy_info_timer, jiffies + 2 * HZ);
2502                         adapter->smartspeed = 0;
2503                 }
2504         } else {
2505                 if (netif_carrier_ok(netdev)) {
2506                         adapter->link_speed = 0;
2507                         adapter->link_duplex = 0;
2508                         DPRINTK(LINK, INFO, "NIC Link is Down\n");
2509                         netif_carrier_off(netdev);
2510                         netif_stop_queue(netdev);
2511                         mod_timer(&adapter->phy_info_timer, jiffies + 2 * HZ);
2512
2513                         /* 80003ES2LAN workaround--
2514                          * For packet buffer work-around on link down event;
2515                          * disable receives in the ISR and
2516                          * reset device here in the watchdog
2517                          */
2518                         if (adapter->hw.mac_type == e1000_80003es2lan)
2519                                 /* reset device */
2520                                 schedule_work(&adapter->reset_task);
2521                 }
2522
2523                 e1000_smartspeed(adapter);
2524         }
2525
2526         e1000_update_stats(adapter);
2527
2528         adapter->hw.tx_packet_delta = adapter->stats.tpt - adapter->tpt_old;
2529         adapter->tpt_old = adapter->stats.tpt;
2530         adapter->hw.collision_delta = adapter->stats.colc - adapter->colc_old;
2531         adapter->colc_old = adapter->stats.colc;
2532
2533         adapter->gorcl = adapter->stats.gorcl - adapter->gorcl_old;
2534         adapter->gorcl_old = adapter->stats.gorcl;
2535         adapter->gotcl = adapter->stats.gotcl - adapter->gotcl_old;
2536         adapter->gotcl_old = adapter->stats.gotcl;
2537
2538         e1000_update_adaptive(&adapter->hw);
2539
2540         if (!netif_carrier_ok(netdev)) {
2541                 if (E1000_DESC_UNUSED(txdr) + 1 < txdr->count) {
2542                         /* We've lost link, so the controller stops DMA,
2543                          * but we've got queued Tx work that's never going
2544                          * to get done, so reset controller to flush Tx.
2545                          * (Do the reset outside of interrupt context). */
2546                         adapter->tx_timeout_count++;
2547                         schedule_work(&adapter->reset_task);
2548                 }
2549         }
2550
2551         /* Dynamic mode for Interrupt Throttle Rate (ITR) */
2552         if (adapter->hw.mac_type >= e1000_82540 && adapter->itr == 1) {
2553                 /* Symmetric Tx/Rx gets a reduced ITR=2000; Total
2554                  * asymmetrical Tx or Rx gets ITR=8000; everyone
2555                  * else is between 2000-8000. */
2556                 uint32_t goc = (adapter->gotcl + adapter->gorcl) / 10000;
2557                 uint32_t dif = (adapter->gotcl > adapter->gorcl ?
2558                         adapter->gotcl - adapter->gorcl :
2559                         adapter->gorcl - adapter->gotcl) / 10000;
2560                 uint32_t itr = goc > 0 ? (dif * 6000 / goc + 2000) : 8000;
2561                 E1000_WRITE_REG(&adapter->hw, ITR, 1000000000 / (itr * 256));
2562         }
2563
2564         /* Cause software interrupt to ensure rx ring is cleaned */
2565         E1000_WRITE_REG(&adapter->hw, ICS, E1000_ICS_RXDMT0);
2566
2567         /* Force detection of hung controller every watchdog period */
2568         adapter->detect_tx_hung = TRUE;
2569
2570         /* With 82571 controllers, LAA may be overwritten due to controller
2571          * reset from the other port. Set the appropriate LAA in RAR[0] */
2572         if (adapter->hw.mac_type == e1000_82571 && adapter->hw.laa_is_present)
2573                 e1000_rar_set(&adapter->hw, adapter->hw.mac_addr, 0);
2574
2575         /* Reset the timer */
2576         mod_timer(&adapter->watchdog_timer, jiffies + 2 * HZ);
2577 }
2578
2579 #define E1000_TX_FLAGS_CSUM             0x00000001
2580 #define E1000_TX_FLAGS_VLAN             0x00000002
2581 #define E1000_TX_FLAGS_TSO              0x00000004
2582 #define E1000_TX_FLAGS_IPV4             0x00000008
2583 #define E1000_TX_FLAGS_VLAN_MASK        0xffff0000
2584 #define E1000_TX_FLAGS_VLAN_SHIFT       16
2585
2586 static int
2587 e1000_tso(struct e1000_adapter *adapter, struct e1000_tx_ring *tx_ring,
2588           struct sk_buff *skb)
2589 {
2590 #ifdef NETIF_F_TSO
2591         struct e1000_context_desc *context_desc;
2592         struct e1000_buffer *buffer_info;
2593         unsigned int i;
2594         uint32_t cmd_length = 0;
2595         uint16_t ipcse = 0, tucse, mss;
2596         uint8_t ipcss, ipcso, tucss, tucso, hdr_len;
2597         int err;
2598
2599         if (skb_is_gso(skb)) {
2600                 if (skb_header_cloned(skb)) {
2601                         err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2602                         if (err)
2603                                 return err;
2604                 }
2605
2606                 hdr_len = ((skb->h.raw - skb->data) + (skb->h.th->doff << 2));
2607                 mss = skb_shinfo(skb)->gso_size;
2608                 if (skb->protocol == htons(ETH_P_IP)) {
2609                         skb->nh.iph->tot_len = 0;
2610                         skb->nh.iph->check = 0;
2611                         skb->h.th->check =
2612                                 ~csum_tcpudp_magic(skb->nh.iph->saddr,
2613                                                    skb->nh.iph->daddr,
2614                                                    0,
2615                                                    IPPROTO_TCP,
2616                                                    0);
2617                         cmd_length = E1000_TXD_CMD_IP;
2618                         ipcse = skb->h.raw - skb->data - 1;
2619 #ifdef NETIF_F_TSO_IPV6
2620                 } else if (skb->protocol == htons(ETH_P_IPV6)) {
2621                         skb->nh.ipv6h->payload_len = 0;
2622                         skb->h.th->check =
2623                                 ~csum_ipv6_magic(&skb->nh.ipv6h->saddr,
2624                                                  &skb->nh.ipv6h->daddr,
2625                                                  0,
2626                                                  IPPROTO_TCP,
2627                                                  0);
2628                         ipcse = 0;
2629 #endif
2630                 }
2631                 ipcss = skb->nh.raw - skb->data;
2632                 ipcso = (void *)&(skb->nh.iph->check) - (void *)skb->data;
2633                 tucss = skb->h.raw - skb->data;
2634                 tucso = (void *)&(skb->h.th->check) - (void *)skb->data;
2635                 tucse = 0;
2636
2637                 cmd_length |= (E1000_TXD_CMD_DEXT | E1000_TXD_CMD_TSE |
2638                                E1000_TXD_CMD_TCP | (skb->len - (hdr_len)));
2639
2640                 i = tx_ring->next_to_use;
2641                 context_desc = E1000_CONTEXT_DESC(*tx_ring, i);
2642                 buffer_info = &tx_ring->buffer_info[i];
2643
2644                 context_desc->lower_setup.ip_fields.ipcss  = ipcss;
2645                 context_desc->lower_setup.ip_fields.ipcso  = ipcso;
2646                 context_desc->lower_setup.ip_fields.ipcse  = cpu_to_le16(ipcse);
2647                 context_desc->upper_setup.tcp_fields.tucss = tucss;
2648                 context_desc->upper_setup.tcp_fields.tucso = tucso;
2649                 context_desc->upper_setup.tcp_fields.tucse = cpu_to_le16(tucse);
2650                 context_desc->tcp_seg_setup.fields.mss     = cpu_to_le16(mss);
2651                 context_desc->tcp_seg_setup.fields.hdr_len = hdr_len;
2652                 context_desc->cmd_and_length = cpu_to_le32(cmd_length);
2653
2654                 buffer_info->time_stamp = jiffies;
2655
2656                 if (++i == tx_ring->count) i = 0;
2657                 tx_ring->next_to_use = i;
2658
2659                 return TRUE;
2660         }
2661 #endif
2662
2663         return FALSE;
2664 }
2665
2666 static boolean_t
2667 e1000_tx_csum(struct e1000_adapter *adapter, struct e1000_tx_ring *tx_ring,
2668               struct sk_buff *skb)
2669 {
2670         struct e1000_context_desc *context_desc;
2671         struct e1000_buffer *buffer_info;
2672         unsigned int i;
2673         uint8_t css;
2674
2675         if (likely(skb->ip_summed == CHECKSUM_PARTIAL)) {
2676                 css = skb->h.raw - skb->data;
2677
2678                 i = tx_ring->next_to_use;
2679                 buffer_info = &tx_ring->buffer_info[i];
2680                 context_desc = E1000_CONTEXT_DESC(*tx_ring, i);
2681
2682                 context_desc->upper_setup.tcp_fields.tucss = css;
2683                 context_desc->upper_setup.tcp_fields.tucso = css + skb->csum;
2684                 context_desc->upper_setup.tcp_fields.tucse = 0;
2685                 context_desc->tcp_seg_setup.data = 0;
2686                 context_desc->cmd_and_length = cpu_to_le32(E1000_TXD_CMD_DEXT);
2687
2688                 buffer_info->time_stamp = jiffies;
2689
2690                 if (unlikely(++i == tx_ring->count)) i = 0;
2691                 tx_ring->next_to_use = i;
2692
2693                 return TRUE;
2694         }
2695
2696         return FALSE;
2697 }
2698
2699 #define E1000_MAX_TXD_PWR       12
2700 #define E1000_MAX_DATA_PER_TXD  (1<<E1000_MAX_TXD_PWR)
2701
2702 static int
2703 e1000_tx_map(struct e1000_adapter *adapter, struct e1000_tx_ring *tx_ring,
2704              struct sk_buff *skb, unsigned int first, unsigned int max_per_txd,
2705              unsigned int nr_frags, unsigned int mss)
2706 {
2707         struct e1000_buffer *buffer_info;
2708         unsigned int len = skb->len;
2709         unsigned int offset = 0, size, count = 0, i;
2710         unsigned int f;
2711         len -= skb->data_len;
2712
2713         i = tx_ring->next_to_use;
2714
2715         while (len) {
2716                 buffer_info = &tx_ring->buffer_info[i];
2717                 size = min(len, max_per_txd);
2718 #ifdef NETIF_F_TSO
2719                 /* Workaround for Controller erratum --
2720                  * descriptor for non-tso packet in a linear SKB that follows a
2721                  * tso gets written back prematurely before the data is fully
2722                  * DMA'd to the controller */
2723                 if (!skb->data_len && tx_ring->last_tx_tso &&
2724                     !skb_is_gso(skb)) {
2725                         tx_ring->last_tx_tso = 0;
2726                         size -= 4;
2727                 }
2728
2729                 /* Workaround for premature desc write-backs
2730                  * in TSO mode.  Append 4-byte sentinel desc */
2731                 if (unlikely(mss && !nr_frags && size == len && size > 8))
2732                         size -= 4;
2733 #endif
2734                 /* work-around for errata 10 and it applies
2735                  * to all controllers in PCI-X mode
2736                  * The fix is to make sure that the first descriptor of a
2737                  * packet is smaller than 2048 - 16 - 16 (or 2016) bytes
2738                  */
2739                 if (unlikely((adapter->hw.bus_type == e1000_bus_type_pcix) &&
2740                                 (size > 2015) && count == 0))
2741                         size = 2015;
2742
2743                 /* Workaround for potential 82544 hang in PCI-X.  Avoid
2744                  * terminating buffers within evenly-aligned dwords. */
2745                 if (unlikely(adapter->pcix_82544 &&
2746                    !((unsigned long)(skb->data + offset + size - 1) & 4) &&
2747                    size > 4))
2748                         size -= 4;
2749
2750                 buffer_info->length = size;
2751                 buffer_info->dma =
2752                         pci_map_single(adapter->pdev,
2753                                 skb->data + offset,
2754                                 size,
2755                                 PCI_DMA_TODEVICE);
2756                 buffer_info->time_stamp = jiffies;
2757
2758                 len -= size;
2759                 offset += size;
2760                 count++;
2761                 if (unlikely(++i == tx_ring->count)) i = 0;
2762         }
2763
2764         for (f = 0; f < nr_frags; f++) {
2765                 struct skb_frag_struct *frag;
2766
2767                 frag = &skb_shinfo(skb)->frags[f];
2768                 len = frag->size;
2769                 offset = frag->page_offset;
2770
2771                 while (len) {
2772                         buffer_info = &tx_ring->buffer_info[i];
2773                         size = min(len, max_per_txd);
2774 #ifdef NETIF_F_TSO
2775                         /* Workaround for premature desc write-backs
2776                          * in TSO mode.  Append 4-byte sentinel desc */
2777                         if (unlikely(mss && f == (nr_frags-1) && size == len && size > 8))
2778                                 size -= 4;
2779 #endif
2780                         /* Workaround for potential 82544 hang in PCI-X.
2781                          * Avoid terminating buffers within evenly-aligned
2782                          * dwords. */
2783                         if (unlikely(adapter->pcix_82544 &&
2784                            !((unsigned long)(frag->page+offset+size-1) & 4) &&
2785                            size > 4))
2786                                 size -= 4;
2787
2788                         buffer_info->length = size;
2789                         buffer_info->dma =
2790                                 pci_map_page(adapter->pdev,
2791                                         frag->page,
2792                                         offset,
2793                                         size,
2794                                         PCI_DMA_TODEVICE);
2795                         buffer_info->time_stamp = jiffies;
2796
2797                         len -= size;
2798                         offset += size;
2799                         count++;
2800                         if (unlikely(++i == tx_ring->count)) i = 0;
2801                 }
2802         }
2803
2804         i = (i == 0) ? tx_ring->count - 1 : i - 1;
2805         tx_ring->buffer_info[i].skb = skb;
2806         tx_ring->buffer_info[first].next_to_watch = i;
2807
2808         return count;
2809 }
2810
2811 static void
2812 e1000_tx_queue(struct e1000_adapter *adapter, struct e1000_tx_ring *tx_ring,
2813                int tx_flags, int count)
2814 {
2815         struct e1000_tx_desc *tx_desc = NULL;
2816         struct e1000_buffer *buffer_info;
2817         uint32_t txd_upper = 0, txd_lower = E1000_TXD_CMD_IFCS;
2818         unsigned int i;
2819
2820         if (likely(tx_flags & E1000_TX_FLAGS_TSO)) {
2821                 txd_lower |= E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D |
2822                              E1000_TXD_CMD_TSE;
2823                 txd_upper |= E1000_TXD_POPTS_TXSM << 8;
2824
2825                 if (likely(tx_flags & E1000_TX_FLAGS_IPV4))
2826                         txd_upper |= E1000_TXD_POPTS_IXSM << 8;
2827         }
2828
2829         if (likely(tx_flags & E1000_TX_FLAGS_CSUM)) {
2830                 txd_lower |= E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D;
2831                 txd_upper |= E1000_TXD_POPTS_TXSM << 8;
2832         }
2833
2834         if (unlikely(tx_flags & E1000_TX_FLAGS_VLAN)) {
2835                 txd_lower |= E1000_TXD_CMD_VLE;
2836                 txd_upper |= (tx_flags & E1000_TX_FLAGS_VLAN_MASK);
2837         }
2838
2839         i = tx_ring->next_to_use;
2840
2841         while (count--) {
2842                 buffer_info = &tx_ring->buffer_info[i];
2843                 tx_desc = E1000_TX_DESC(*tx_ring, i);
2844                 tx_desc->buffer_addr = cpu_to_le64(buffer_info->dma);
2845                 tx_desc->lower.data =
2846                         cpu_to_le32(txd_lower | buffer_info->length);
2847                 tx_desc->upper.data = cpu_to_le32(txd_upper);
2848                 if (unlikely(++i == tx_ring->count)) i = 0;
2849         }
2850
2851         tx_desc->lower.data |= cpu_to_le32(adapter->txd_cmd);
2852
2853         /* Force memory writes to complete before letting h/w
2854          * know there are new descriptors to fetch.  (Only
2855          * applicable for weak-ordered memory model archs,
2856          * such as IA-64). */
2857         wmb();
2858
2859         tx_ring->next_to_use = i;
2860         writel(i, adapter->hw.hw_addr + tx_ring->tdt);
2861 }
2862
2863 /**
2864  * 82547 workaround to avoid controller hang in half-duplex environment.
2865  * The workaround is to avoid queuing a large packet that would span
2866  * the internal Tx FIFO ring boundary by notifying the stack to resend
2867  * the packet at a later time.  This gives the Tx FIFO an opportunity to
2868  * flush all packets.  When that occurs, we reset the Tx FIFO pointers
2869  * to the beginning of the Tx FIFO.
2870  **/
2871
2872 #define E1000_FIFO_HDR                  0x10
2873 #define E1000_82547_PAD_LEN             0x3E0
2874
2875 static int
2876 e1000_82547_fifo_workaround(struct e1000_adapter *adapter, struct sk_buff *skb)
2877 {
2878         uint32_t fifo_space = adapter->tx_fifo_size - adapter->tx_fifo_head;
2879         uint32_t skb_fifo_len = skb->len + E1000_FIFO_HDR;
2880
2881         E1000_ROUNDUP(skb_fifo_len, E1000_FIFO_HDR);
2882
2883         if (adapter->link_duplex != HALF_DUPLEX)
2884                 goto no_fifo_stall_required;
2885
2886         if (atomic_read(&adapter->tx_fifo_stall))
2887                 return 1;
2888
2889         if (skb_fifo_len >= (E1000_82547_PAD_LEN + fifo_space)) {
2890                 atomic_set(&adapter->tx_fifo_stall, 1);
2891                 return 1;
2892         }
2893
2894 no_fifo_stall_required:
2895         adapter->tx_fifo_head += skb_fifo_len;
2896         if (adapter->tx_fifo_head >= adapter->tx_fifo_size)
2897                 adapter->tx_fifo_head -= adapter->tx_fifo_size;
2898         return 0;
2899 }
2900
2901 #define MINIMUM_DHCP_PACKET_SIZE 282
2902 static int
2903 e1000_transfer_dhcp_info(struct e1000_adapter *adapter, struct sk_buff *skb)
2904 {
2905         struct e1000_hw *hw =  &adapter->hw;
2906         uint16_t length, offset;
2907         if (vlan_tx_tag_present(skb)) {
2908                 if (!((vlan_tx_tag_get(skb) == adapter->hw.mng_cookie.vlan_id) &&
2909                         ( adapter->hw.mng_cookie.status &
2910                           E1000_MNG_DHCP_COOKIE_STATUS_VLAN_SUPPORT)) )
2911                         return 0;
2912         }
2913         if (skb->len > MINIMUM_DHCP_PACKET_SIZE) {
2914                 struct ethhdr *eth = (struct ethhdr *) skb->data;
2915                 if ((htons(ETH_P_IP) == eth->h_proto)) {
2916                         const struct iphdr *ip =
2917                                 (struct iphdr *)((uint8_t *)skb->data+14);
2918                         if (IPPROTO_UDP == ip->protocol) {
2919                                 struct udphdr *udp =
2920                                         (struct udphdr *)((uint8_t *)ip +
2921                                                 (ip->ihl << 2));
2922                                 if (ntohs(udp->dest) == 67) {
2923                                         offset = (uint8_t *)udp + 8 - skb->data;
2924                                         length = skb->len - offset;
2925
2926                                         return e1000_mng_write_dhcp_info(hw,
2927                                                         (uint8_t *)udp + 8,
2928                                                         length);
2929                                 }
2930                         }
2931                 }
2932         }
2933         return 0;
2934 }
2935
2936 static int __e1000_maybe_stop_tx(struct net_device *netdev, int size)
2937 {
2938         struct e1000_adapter *adapter = netdev_priv(netdev);
2939         struct e1000_tx_ring *tx_ring = adapter->tx_ring;
2940
2941         netif_stop_queue(netdev);
2942         /* Herbert's original patch had:
2943          *  smp_mb__after_netif_stop_queue();
2944          * but since that doesn't exist yet, just open code it. */
2945         smp_mb();
2946
2947         /* We need to check again in a case another CPU has just
2948          * made room available. */
2949         if (likely(E1000_DESC_UNUSED(tx_ring) < size))
2950                 return -EBUSY;
2951
2952         /* A reprieve! */
2953         netif_start_queue(netdev);
2954         return 0;
2955 }
2956
2957 static int e1000_maybe_stop_tx(struct net_device *netdev,
2958                                struct e1000_tx_ring *tx_ring, int size)
2959 {
2960         if (likely(E1000_DESC_UNUSED(tx_ring) >= size))
2961                 return 0;
2962         return __e1000_maybe_stop_tx(netdev, size);
2963 }
2964
2965 #define TXD_USE_COUNT(S, X) (((S) >> (X)) + 1 )
2966 static int
2967 e1000_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
2968 {
2969         struct e1000_adapter *adapter = netdev_priv(netdev);
2970         struct e1000_tx_ring *tx_ring;
2971         unsigned int first, max_per_txd = E1000_MAX_DATA_PER_TXD;
2972         unsigned int max_txd_pwr = E1000_MAX_TXD_PWR;
2973         unsigned int tx_flags = 0;
2974         unsigned int len = skb->len;
2975         unsigned long flags;
2976         unsigned int nr_frags = 0;
2977         unsigned int mss = 0;
2978         int count = 0;
2979         int tso;
2980         unsigned int f;
2981         len -= skb->data_len;
2982
2983         /* This goes back to the question of how to logically map a tx queue
2984          * to a flow.  Right now, performance is impacted slightly negatively
2985          * if using multiple tx queues.  If the stack breaks away from a
2986          * single qdisc implementation, we can look at this again. */
2987         tx_ring = adapter->tx_ring;
2988
2989         if (unlikely(skb->len <= 0)) {
2990                 dev_kfree_skb_any(skb);
2991                 return NETDEV_TX_OK;
2992         }
2993
2994         /* 82571 and newer doesn't need the workaround that limited descriptor
2995          * length to 4kB */
2996         if (adapter->hw.mac_type >= e1000_82571)
2997                 max_per_txd = 8192;
2998
2999 #ifdef NETIF_F_TSO
3000         mss = skb_shinfo(skb)->gso_size;
3001         /* The controller does a simple calculation to
3002          * make sure there is enough room in the FIFO before
3003          * initiating the DMA for each buffer.  The calc is:
3004          * 4 = ceil(buffer len/mss).  To make sure we don't
3005          * overrun the FIFO, adjust the max buffer len if mss
3006          * drops. */
3007         if (mss) {
3008                 uint8_t hdr_len;
3009                 max_per_txd = min(mss << 2, max_per_txd);
3010                 max_txd_pwr = fls(max_per_txd) - 1;
3011
3012         /* TSO Workaround for 82571/2/3 Controllers -- if skb->data
3013          * points to just header, pull a few bytes of payload from
3014          * frags into skb->data */
3015                 hdr_len = ((skb->h.raw - skb->data) + (skb->h.th->doff << 2));
3016                 if (skb->data_len && (hdr_len == (skb->len - skb->data_len))) {
3017                         switch (adapter->hw.mac_type) {
3018                                 unsigned int pull_size;
3019                         case e1000_82571:
3020                         case e1000_82572:
3021                         case e1000_82573:
3022                         case e1000_ich8lan:
3023                                 pull_size = min((unsigned int)4, skb->data_len);
3024                                 if (!__pskb_pull_tail(skb, pull_size)) {
3025                                         DPRINTK(DRV, ERR,
3026                                                 "__pskb_pull_tail failed.\n");
3027                                         dev_kfree_skb_any(skb);
3028                                         return NETDEV_TX_OK;
3029                                 }
3030                                 len = skb->len - skb->data_len;
3031                                 break;
3032                         default:
3033                                 /* do nothing */
3034                                 break;
3035                         }
3036                 }
3037         }
3038
3039         /* reserve a descriptor for the offload context */
3040         if ((mss) || (skb->ip_summed == CHECKSUM_PARTIAL))
3041                 count++;
3042         count++;
3043 #else
3044         if (skb->ip_summed == CHECKSUM_PARTIAL)
3045                 count++;
3046 #endif
3047
3048 #ifdef NETIF_F_TSO
3049         /* Controller Erratum workaround */
3050         if (!skb->data_len && tx_ring->last_tx_tso && !skb_is_gso(skb))
3051                 count++;
3052 #endif
3053
3054         count += TXD_USE_COUNT(len, max_txd_pwr);
3055
3056         if (adapter->pcix_82544)
3057                 count++;
3058
3059         /* work-around for errata 10 and it applies to all controllers
3060          * in PCI-X mode, so add one more descriptor to the count
3061          */
3062         if (unlikely((adapter->hw.bus_type == e1000_bus_type_pcix) &&
3063                         (len > 2015)))
3064                 count++;
3065
3066         nr_frags = skb_shinfo(skb)->nr_frags;
3067         for (f = 0; f < nr_frags; f++)
3068                 count += TXD_USE_COUNT(skb_shinfo(skb)->frags[f].size,
3069                                        max_txd_pwr);
3070         if (adapter->pcix_82544)
3071                 count += nr_frags;
3072
3073
3074         if (adapter->hw.tx_pkt_filtering &&
3075             (adapter->hw.mac_type == e1000_82573))
3076                 e1000_transfer_dhcp_info(adapter, skb);
3077
3078         local_irq_save(flags);
3079         if (!spin_trylock(&tx_ring->tx_lock)) {
3080                 /* Collision - tell upper layer to requeue */
3081                 local_irq_restore(flags);
3082                 return NETDEV_TX_LOCKED;
3083         }
3084
3085         /* need: count + 2 desc gap to keep tail from touching
3086          * head, otherwise try next time */
3087         if (unlikely(e1000_maybe_stop_tx(netdev, tx_ring, count + 2))) {
3088                 spin_unlock_irqrestore(&tx_ring->tx_lock, flags);
3089                 return NETDEV_TX_BUSY;
3090         }
3091
3092         if (unlikely(adapter->hw.mac_type == e1000_82547)) {
3093                 if (unlikely(e1000_82547_fifo_workaround(adapter, skb))) {
3094                         netif_stop_queue(netdev);
3095                         mod_timer(&adapter->tx_fifo_stall_timer, jiffies + 1);
3096                         spin_unlock_irqrestore(&tx_ring->tx_lock, flags);
3097                         return NETDEV_TX_BUSY;
3098                 }
3099         }
3100
3101         if (unlikely(adapter->vlgrp && vlan_tx_tag_present(skb))) {
3102                 tx_flags |= E1000_TX_FLAGS_VLAN;
3103                 tx_flags |= (vlan_tx_tag_get(skb) << E1000_TX_FLAGS_VLAN_SHIFT);
3104         }
3105
3106         first = tx_ring->next_to_use;
3107
3108         tso = e1000_tso(adapter, tx_ring, skb);
3109         if (tso < 0) {
3110                 dev_kfree_skb_any(skb);
3111                 spin_unlock_irqrestore(&tx_ring->tx_lock, flags);
3112                 return NETDEV_TX_OK;
3113         }
3114
3115         if (likely(tso)) {
3116                 tx_ring->last_tx_tso = 1;
3117                 tx_flags |= E1000_TX_FLAGS_TSO;
3118         } else if (likely(e1000_tx_csum(adapter, tx_ring, skb)))
3119                 tx_flags |= E1000_TX_FLAGS_CSUM;
3120
3121         /* Old method was to assume IPv4 packet by default if TSO was enabled.
3122          * 82571 hardware supports TSO capabilities for IPv6 as well...
3123          * no longer assume, we must. */
3124         if (likely(skb->protocol == htons(ETH_P_IP)))
3125                 tx_flags |= E1000_TX_FLAGS_IPV4;
3126
3127         e1000_tx_queue(adapter, tx_ring, tx_flags,
3128                        e1000_tx_map(adapter, tx_ring, skb, first,
3129                                     max_per_txd, nr_frags, mss));
3130
3131         netdev->trans_start = jiffies;
3132
3133         /* Make sure there is space in the ring for the next send. */
3134         e1000_maybe_stop_tx(netdev, tx_ring, MAX_SKB_FRAGS + 2);
3135
3136         spin_unlock_irqrestore(&tx_ring->tx_lock, flags);
3137         return NETDEV_TX_OK;
3138 }
3139
3140 /**
3141  * e1000_tx_timeout - Respond to a Tx Hang
3142  * @netdev: network interface device structure
3143  **/
3144
3145 static void
3146 e1000_tx_timeout(struct net_device *netdev)
3147 {
3148         struct e1000_adapter *adapter = netdev_priv(netdev);
3149
3150         /* Do the reset outside of interrupt context */
3151         adapter->tx_timeout_count++;
3152         schedule_work(&adapter->reset_task);
3153 }
3154
3155 static void
3156 e1000_reset_task(struct work_struct *work)
3157 {
3158         struct e1000_adapter *adapter =
3159                 container_of(work, struct e1000_adapter, reset_task);
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                                 return -EINVAL;
3223                         }
3224                         break;
3225                 }
3226                 /* ERT will be enabled later to enable wire speed receives */
3227
3228                 /* fall through to get support */
3229         case e1000_82571:
3230         case e1000_82572:
3231         case e1000_80003es2lan:
3232 #define MAX_STD_JUMBO_FRAME_SIZE 9234
3233                 if (max_frame > MAX_STD_JUMBO_FRAME_SIZE) {
3234                         DPRINTK(PROBE, ERR, "MTU > 9216 not supported.\n");
3235                         return -EINVAL;
3236                 }
3237                 break;
3238         default:
3239                 /* Capable of supporting up to MAX_JUMBO_FRAME_SIZE limit. */
3240                 break;
3241         }
3242
3243         /* NOTE: netdev_alloc_skb reserves 16 bytes, and typically NET_IP_ALIGN
3244          * means we reserve 2 more, this pushes us to allocate from the next
3245          * larger slab size
3246          * i.e. RXBUFFER_2048 --> size-4096 slab */
3247
3248         if (max_frame <= E1000_RXBUFFER_256)
3249                 adapter->rx_buffer_len = E1000_RXBUFFER_256;
3250         else if (max_frame <= E1000_RXBUFFER_512)
3251                 adapter->rx_buffer_len = E1000_RXBUFFER_512;
3252         else if (max_frame <= E1000_RXBUFFER_1024)
3253                 adapter->rx_buffer_len = E1000_RXBUFFER_1024;
3254         else if (max_frame <= E1000_RXBUFFER_2048)
3255                 adapter->rx_buffer_len = E1000_RXBUFFER_2048;
3256         else if (max_frame <= E1000_RXBUFFER_4096)
3257                 adapter->rx_buffer_len = E1000_RXBUFFER_4096;
3258         else if (max_frame <= E1000_RXBUFFER_8192)
3259                 adapter->rx_buffer_len = E1000_RXBUFFER_8192;
3260         else if (max_frame <= E1000_RXBUFFER_16384)
3261                 adapter->rx_buffer_len = E1000_RXBUFFER_16384;
3262
3263         /* adjust allocation if LPE protects us, and we aren't using SBP */
3264         if (!adapter->hw.tbi_compatibility_on &&
3265             ((max_frame == MAXIMUM_ETHERNET_FRAME_SIZE) ||
3266              (max_frame == MAXIMUM_ETHERNET_VLAN_SIZE)))
3267                 adapter->rx_buffer_len = MAXIMUM_ETHERNET_VLAN_SIZE;
3268
3269         netdev->mtu = new_mtu;
3270
3271         if (netif_running(netdev))
3272                 e1000_reinit_locked(adapter);
3273
3274         adapter->hw.max_frame_size = max_frame;
3275
3276         return 0;
3277 }
3278
3279 /**
3280  * e1000_update_stats - Update the board statistics counters
3281  * @adapter: board private structure
3282  **/
3283
3284 void
3285 e1000_update_stats(struct e1000_adapter *adapter)
3286 {
3287         struct e1000_hw *hw = &adapter->hw;
3288         struct pci_dev *pdev = adapter->pdev;
3289         unsigned long flags;
3290         uint16_t phy_tmp;
3291
3292 #define PHY_IDLE_ERROR_COUNT_MASK 0x00FF
3293
3294         /*
3295          * Prevent stats update while adapter is being reset, or if the pci
3296          * connection is down.
3297          */
3298         if (adapter->link_speed == 0)
3299                 return;
3300         if (pdev->error_state && pdev->error_state != pci_channel_io_normal)
3301                 return;
3302
3303         spin_lock_irqsave(&adapter->stats_lock, flags);
3304
3305         /* these counters are modified from e1000_adjust_tbi_stats,
3306          * called from the interrupt context, so they must only
3307          * be written while holding adapter->stats_lock
3308          */
3309
3310         adapter->stats.crcerrs += E1000_READ_REG(hw, CRCERRS);
3311         adapter->stats.gprc += E1000_READ_REG(hw, GPRC);
3312         adapter->stats.gorcl += E1000_READ_REG(hw, GORCL);
3313         adapter->stats.gorch += E1000_READ_REG(hw, GORCH);
3314         adapter->stats.bprc += E1000_READ_REG(hw, BPRC);
3315         adapter->stats.mprc += E1000_READ_REG(hw, MPRC);
3316         adapter->stats.roc += E1000_READ_REG(hw, ROC);
3317
3318         if (adapter->hw.mac_type != e1000_ich8lan) {
3319         adapter->stats.prc64 += E1000_READ_REG(hw, PRC64);
3320         adapter->stats.prc127 += E1000_READ_REG(hw, PRC127);
3321         adapter->stats.prc255 += E1000_READ_REG(hw, PRC255);
3322         adapter->stats.prc511 += E1000_READ_REG(hw, PRC511);
3323         adapter->stats.prc1023 += E1000_READ_REG(hw, PRC1023);
3324         adapter->stats.prc1522 += E1000_READ_REG(hw, PRC1522);
3325         }
3326
3327         adapter->stats.symerrs += E1000_READ_REG(hw, SYMERRS);
3328         adapter->stats.mpc += E1000_READ_REG(hw, MPC);
3329         adapter->stats.scc += E1000_READ_REG(hw, SCC);
3330         adapter->stats.ecol += E1000_READ_REG(hw, ECOL);
3331         adapter->stats.mcc += E1000_READ_REG(hw, MCC);
3332         adapter->stats.latecol += E1000_READ_REG(hw, LATECOL);
3333         adapter->stats.dc += E1000_READ_REG(hw, DC);
3334         adapter->stats.sec += E1000_READ_REG(hw, SEC);
3335         adapter->stats.rlec += E1000_READ_REG(hw, RLEC);
3336         adapter->stats.xonrxc += E1000_READ_REG(hw, XONRXC);
3337         adapter->stats.xontxc += E1000_READ_REG(hw, XONTXC);
3338         adapter->stats.xoffrxc += E1000_READ_REG(hw, XOFFRXC);
3339         adapter->stats.xofftxc += E1000_READ_REG(hw, XOFFTXC);
3340         adapter->stats.fcruc += E1000_READ_REG(hw, FCRUC);
3341         adapter->stats.gptc += E1000_READ_REG(hw, GPTC);
3342         adapter->stats.gotcl += E1000_READ_REG(hw, GOTCL);
3343         adapter->stats.gotch += E1000_READ_REG(hw, GOTCH);
3344         adapter->stats.rnbc += E1000_READ_REG(hw, RNBC);
3345         adapter->stats.ruc += E1000_READ_REG(hw, RUC);
3346         adapter->stats.rfc += E1000_READ_REG(hw, RFC);
3347         adapter->stats.rjc += E1000_READ_REG(hw, RJC);
3348         adapter->stats.torl += E1000_READ_REG(hw, TORL);
3349         adapter->stats.torh += E1000_READ_REG(hw, TORH);
3350         adapter->stats.totl += E1000_READ_REG(hw, TOTL);
3351         adapter->stats.toth += E1000_READ_REG(hw, TOTH);
3352         adapter->stats.tpr += E1000_READ_REG(hw, TPR);
3353
3354         if (adapter->hw.mac_type != e1000_ich8lan) {
3355         adapter->stats.ptc64 += E1000_READ_REG(hw, PTC64);
3356         adapter->stats.ptc127 += E1000_READ_REG(hw, PTC127);
3357         adapter->stats.ptc255 += E1000_READ_REG(hw, PTC255);
3358         adapter->stats.ptc511 += E1000_READ_REG(hw, PTC511);
3359         adapter->stats.ptc1023 += E1000_READ_REG(hw, PTC1023);
3360         adapter->stats.ptc1522 += E1000_READ_REG(hw, PTC1522);
3361         }
3362
3363         adapter->stats.mptc += E1000_READ_REG(hw, MPTC);
3364         adapter->stats.bptc += E1000_READ_REG(hw, BPTC);
3365
3366         /* used for adaptive IFS */
3367
3368         hw->tx_packet_delta = E1000_READ_REG(hw, TPT);
3369         adapter->stats.tpt += hw->tx_packet_delta;
3370         hw->collision_delta = E1000_READ_REG(hw, COLC);
3371         adapter->stats.colc += hw->collision_delta;
3372
3373         if (hw->mac_type >= e1000_82543) {
3374                 adapter->stats.algnerrc += E1000_READ_REG(hw, ALGNERRC);
3375                 adapter->stats.rxerrc += E1000_READ_REG(hw, RXERRC);
3376                 adapter->stats.tncrs += E1000_READ_REG(hw, TNCRS);
3377                 adapter->stats.cexterr += E1000_READ_REG(hw, CEXTERR);
3378                 adapter->stats.tsctc += E1000_READ_REG(hw, TSCTC);
3379                 adapter->stats.tsctfc += E1000_READ_REG(hw, TSCTFC);
3380         }
3381         if (hw->mac_type > e1000_82547_rev_2) {
3382                 adapter->stats.iac += E1000_READ_REG(hw, IAC);
3383                 adapter->stats.icrxoc += E1000_READ_REG(hw, ICRXOC);
3384
3385                 if (adapter->hw.mac_type != e1000_ich8lan) {
3386                 adapter->stats.icrxptc += E1000_READ_REG(hw, ICRXPTC);
3387                 adapter->stats.icrxatc += E1000_READ_REG(hw, ICRXATC);
3388                 adapter->stats.ictxptc += E1000_READ_REG(hw, ICTXPTC);
3389                 adapter->stats.ictxatc += E1000_READ_REG(hw, ICTXATC);
3390                 adapter->stats.ictxqec += E1000_READ_REG(hw, ICTXQEC);
3391                 adapter->stats.ictxqmtc += E1000_READ_REG(hw, ICTXQMTC);
3392                 adapter->stats.icrxdmtc += E1000_READ_REG(hw, ICRXDMTC);
3393                 }
3394         }
3395
3396         /* Fill out the OS statistics structure */
3397
3398         adapter->net_stats.rx_packets = adapter->stats.gprc;
3399         adapter->net_stats.tx_packets = adapter->stats.gptc;
3400         adapter->net_stats.rx_bytes = adapter->stats.gorcl;
3401         adapter->net_stats.tx_bytes = adapter->stats.gotcl;
3402         adapter->net_stats.multicast = adapter->stats.mprc;
3403         adapter->net_stats.collisions = adapter->stats.colc;
3404
3405         /* Rx Errors */
3406
3407         /* RLEC on some newer hardware can be incorrect so build
3408         * our own version based on RUC and ROC */
3409         adapter->net_stats.rx_errors = adapter->stats.rxerrc +
3410                 adapter->stats.crcerrs + adapter->stats.algnerrc +
3411                 adapter->stats.ruc + adapter->stats.roc +
3412                 adapter->stats.cexterr;
3413         adapter->stats.rlerrc = adapter->stats.ruc + adapter->stats.roc;
3414         adapter->net_stats.rx_length_errors = adapter->stats.rlerrc;
3415         adapter->net_stats.rx_crc_errors = adapter->stats.crcerrs;
3416         adapter->net_stats.rx_frame_errors = adapter->stats.algnerrc;
3417         adapter->net_stats.rx_missed_errors = adapter->stats.mpc;
3418
3419         /* Tx Errors */
3420         adapter->stats.txerrc = adapter->stats.ecol + adapter->stats.latecol;
3421         adapter->net_stats.tx_errors = adapter->stats.txerrc;
3422         adapter->net_stats.tx_aborted_errors = adapter->stats.ecol;
3423         adapter->net_stats.tx_window_errors = adapter->stats.latecol;
3424         adapter->net_stats.tx_carrier_errors = adapter->stats.tncrs;
3425
3426         /* Tx Dropped needs to be maintained elsewhere */
3427
3428         /* Phy Stats */
3429
3430         if (hw->media_type == e1000_media_type_copper) {
3431                 if ((adapter->link_speed == SPEED_1000) &&
3432                    (!e1000_read_phy_reg(hw, PHY_1000T_STATUS, &phy_tmp))) {
3433                         phy_tmp &= PHY_IDLE_ERROR_COUNT_MASK;
3434                         adapter->phy_stats.idle_errors += phy_tmp;
3435                 }
3436
3437                 if ((hw->mac_type <= e1000_82546) &&
3438                    (hw->phy_type == e1000_phy_m88) &&
3439                    !e1000_read_phy_reg(hw, M88E1000_RX_ERR_CNTR, &phy_tmp))
3440                         adapter->phy_stats.receive_errors += phy_tmp;
3441         }
3442
3443         spin_unlock_irqrestore(&adapter->stats_lock, flags);
3444 }
3445
3446 /**
3447  * e1000_intr - Interrupt Handler
3448  * @irq: interrupt number
3449  * @data: pointer to a network interface device structure
3450  **/
3451
3452 static irqreturn_t
3453 e1000_intr(int irq, void *data)
3454 {
3455         struct net_device *netdev = data;
3456         struct e1000_adapter *adapter = netdev_priv(netdev);
3457         struct e1000_hw *hw = &adapter->hw;
3458         uint32_t rctl, icr = E1000_READ_REG(hw, ICR);
3459 #ifndef CONFIG_E1000_NAPI
3460         int i;
3461 #else
3462         /* Interrupt Auto-Mask...upon reading ICR,
3463          * interrupts are masked.  No need for the
3464          * IMC write, but it does mean we should
3465          * account for it ASAP. */
3466         if (likely(hw->mac_type >= e1000_82571))
3467                 atomic_inc(&adapter->irq_sem);
3468 #endif
3469
3470         if (unlikely(!icr)) {
3471 #ifdef CONFIG_E1000_NAPI
3472                 if (hw->mac_type >= e1000_82571)
3473                         e1000_irq_enable(adapter);
3474 #endif
3475                 return IRQ_NONE;  /* Not our interrupt */
3476         }
3477
3478         if (unlikely(icr & (E1000_ICR_RXSEQ | E1000_ICR_LSC))) {
3479                 hw->get_link_status = 1;
3480                 /* 80003ES2LAN workaround--
3481                  * For packet buffer work-around on link down event;
3482                  * disable receives here in the ISR and
3483                  * reset adapter in watchdog
3484                  */
3485                 if (netif_carrier_ok(netdev) &&
3486                     (adapter->hw.mac_type == e1000_80003es2lan)) {
3487                         /* disable receives */
3488                         rctl = E1000_READ_REG(hw, RCTL);
3489                         E1000_WRITE_REG(hw, RCTL, rctl & ~E1000_RCTL_EN);
3490                 }
3491                 /* guard against interrupt when we're going down */
3492                 if (!test_bit(__E1000_DOWN, &adapter->flags))
3493                         mod_timer(&adapter->watchdog_timer, jiffies + 1);
3494         }
3495
3496 #ifdef CONFIG_E1000_NAPI
3497         if (unlikely(hw->mac_type < e1000_82571)) {
3498                 atomic_inc(&adapter->irq_sem);
3499                 E1000_WRITE_REG(hw, IMC, ~0);
3500                 E1000_WRITE_FLUSH(hw);
3501         }
3502         if (likely(netif_rx_schedule_prep(netdev)))
3503                 __netif_rx_schedule(netdev);
3504         else
3505                 e1000_irq_enable(adapter);
3506 #else
3507         /* Writing IMC and IMS is needed for 82547.
3508          * Due to Hub Link bus being occupied, an interrupt
3509          * de-assertion message is not able to be sent.
3510          * When an interrupt assertion message is generated later,
3511          * two messages are re-ordered and sent out.
3512          * That causes APIC to think 82547 is in de-assertion
3513          * state, while 82547 is in assertion state, resulting
3514          * in dead lock. Writing IMC forces 82547 into
3515          * de-assertion state.
3516          */
3517         if (hw->mac_type == e1000_82547 || hw->mac_type == e1000_82547_rev_2) {
3518                 atomic_inc(&adapter->irq_sem);
3519                 E1000_WRITE_REG(hw, IMC, ~0);
3520         }
3521
3522         for (i = 0; i < E1000_MAX_INTR; i++)
3523                 if (unlikely(!adapter->clean_rx(adapter, adapter->rx_ring) &
3524                    !e1000_clean_tx_irq(adapter, adapter->tx_ring)))
3525                         break;
3526
3527         if (hw->mac_type == e1000_82547 || hw->mac_type == e1000_82547_rev_2)
3528                 e1000_irq_enable(adapter);
3529
3530 #endif
3531
3532         return IRQ_HANDLED;
3533 }
3534
3535 #ifdef CONFIG_E1000_NAPI
3536 /**
3537  * e1000_clean - NAPI Rx polling callback
3538  * @adapter: board private structure
3539  **/
3540
3541 static int
3542 e1000_clean(struct net_device *poll_dev, int *budget)
3543 {
3544         struct e1000_adapter *adapter;
3545         int work_to_do = min(*budget, poll_dev->quota);
3546         int tx_cleaned = 0, work_done = 0;
3547
3548         /* Must NOT use netdev_priv macro here. */
3549         adapter = poll_dev->priv;
3550
3551         /* Keep link state information with original netdev */
3552         if (!netif_carrier_ok(poll_dev))
3553                 goto quit_polling;
3554
3555         /* e1000_clean is called per-cpu.  This lock protects
3556          * tx_ring[0] from being cleaned by multiple cpus
3557          * simultaneously.  A failure obtaining the lock means
3558          * tx_ring[0] is currently being cleaned anyway. */
3559         if (spin_trylock(&adapter->tx_queue_lock)) {
3560                 tx_cleaned = e1000_clean_tx_irq(adapter,
3561                                                 &adapter->tx_ring[0]);
3562                 spin_unlock(&adapter->tx_queue_lock);
3563         }
3564
3565         adapter->clean_rx(adapter, &adapter->rx_ring[0],
3566                           &work_done, work_to_do);
3567
3568         *budget -= work_done;
3569         poll_dev->quota -= work_done;
3570
3571         /* If no Tx and not enough Rx work done, exit the polling mode */
3572         if ((!tx_cleaned && (work_done == 0)) ||
3573            !netif_running(poll_dev)) {
3574 quit_polling:
3575                 netif_rx_complete(poll_dev);
3576                 e1000_irq_enable(adapter);
3577                 return 0;
3578         }
3579
3580         return 1;
3581 }
3582
3583 #endif
3584 /**
3585  * e1000_clean_tx_irq - Reclaim resources after transmit completes
3586  * @adapter: board private structure
3587  **/
3588
3589 static boolean_t
3590 e1000_clean_tx_irq(struct e1000_adapter *adapter,
3591                    struct e1000_tx_ring *tx_ring)
3592 {
3593         struct net_device *netdev = adapter->netdev;
3594         struct e1000_tx_desc *tx_desc, *eop_desc;
3595         struct e1000_buffer *buffer_info;
3596         unsigned int i, eop;
3597 #ifdef CONFIG_E1000_NAPI
3598         unsigned int count = 0;
3599 #endif
3600         boolean_t cleaned = FALSE;
3601
3602         i = tx_ring->next_to_clean;
3603         eop = tx_ring->buffer_info[i].next_to_watch;
3604         eop_desc = E1000_TX_DESC(*tx_ring, eop);
3605
3606         while (eop_desc->upper.data & cpu_to_le32(E1000_TXD_STAT_DD)) {
3607                 for (cleaned = FALSE; !cleaned; ) {
3608                         tx_desc = E1000_TX_DESC(*tx_ring, i);
3609                         buffer_info = &tx_ring->buffer_info[i];
3610                         cleaned = (i == eop);
3611
3612                         e1000_unmap_and_free_tx_resource(adapter, buffer_info);
3613                         memset(tx_desc, 0, sizeof(struct e1000_tx_desc));
3614
3615                         if (unlikely(++i == tx_ring->count)) i = 0;
3616                 }
3617
3618
3619                 eop = tx_ring->buffer_info[i].next_to_watch;
3620                 eop_desc = E1000_TX_DESC(*tx_ring, eop);
3621 #ifdef CONFIG_E1000_NAPI
3622 #define E1000_TX_WEIGHT 64
3623                 /* weight of a sort for tx, to avoid endless transmit cleanup */
3624                 if (count++ == E1000_TX_WEIGHT) break;
3625 #endif
3626         }
3627
3628         tx_ring->next_to_clean = i;
3629
3630 #define TX_WAKE_THRESHOLD 32
3631         if (unlikely(cleaned && netif_carrier_ok(netdev) &&
3632                      E1000_DESC_UNUSED(tx_ring) >= TX_WAKE_THRESHOLD)) {
3633                 /* Make sure that anybody stopping the queue after this
3634                  * sees the new next_to_clean.
3635                  */
3636                 smp_mb();
3637                 if (netif_queue_stopped(netdev))
3638                         netif_wake_queue(netdev);
3639         }
3640
3641         if (adapter->detect_tx_hung) {
3642                 /* Detect a transmit hang in hardware, this serializes the
3643                  * check with the clearing of time_stamp and movement of i */
3644                 adapter->detect_tx_hung = FALSE;
3645                 if (tx_ring->buffer_info[eop].dma &&
3646                     time_after(jiffies, tx_ring->buffer_info[eop].time_stamp +
3647                                (adapter->tx_timeout_factor * HZ))
3648                     && !(E1000_READ_REG(&adapter->hw, STATUS) &
3649                          E1000_STATUS_TXOFF)) {
3650
3651                         /* detected Tx unit hang */
3652                         DPRINTK(DRV, ERR, "Detected Tx Unit Hang\n"
3653                                         "  Tx Queue             <%lu>\n"
3654                                         "  TDH                  <%x>\n"
3655                                         "  TDT                  <%x>\n"
3656                                         "  next_to_use          <%x>\n"
3657                                         "  next_to_clean        <%x>\n"
3658                                         "buffer_info[next_to_clean]\n"
3659                                         "  time_stamp           <%lx>\n"
3660                                         "  next_to_watch        <%x>\n"
3661                                         "  jiffies              <%lx>\n"
3662                                         "  next_to_watch.status <%x>\n",
3663                                 (unsigned long)((tx_ring - adapter->tx_ring) /
3664                                         sizeof(struct e1000_tx_ring)),
3665                                 readl(adapter->hw.hw_addr + tx_ring->tdh),
3666                                 readl(adapter->hw.hw_addr + tx_ring->tdt),
3667                                 tx_ring->next_to_use,
3668                                 tx_ring->next_to_clean,
3669                                 tx_ring->buffer_info[eop].time_stamp,
3670                                 eop,
3671                                 jiffies,
3672                                 eop_desc->upper.fields.status);
3673                         netif_stop_queue(netdev);
3674                 }
3675         }
3676         return cleaned;
3677 }
3678
3679 /**
3680  * e1000_rx_checksum - Receive Checksum Offload for 82543
3681  * @adapter:     board private structure
3682  * @status_err:  receive descriptor status and error fields
3683  * @csum:        receive descriptor csum field
3684  * @sk_buff:     socket buffer with received data
3685  **/
3686
3687 static void
3688 e1000_rx_checksum(struct e1000_adapter *adapter,
3689                   uint32_t status_err, uint32_t csum,
3690                   struct sk_buff *skb)
3691 {
3692         uint16_t status = (uint16_t)status_err;
3693         uint8_t errors = (uint8_t)(status_err >> 24);
3694         skb->ip_summed = CHECKSUM_NONE;
3695
3696         /* 82543 or newer only */
3697         if (unlikely(adapter->hw.mac_type < e1000_82543)) return;
3698         /* Ignore Checksum bit is set */
3699         if (unlikely(status & E1000_RXD_STAT_IXSM)) return;
3700         /* TCP/UDP checksum error bit is set */
3701         if (unlikely(errors & E1000_RXD_ERR_TCPE)) {
3702                 /* let the stack verify checksum errors */
3703                 adapter->hw_csum_err++;
3704                 return;
3705         }
3706         /* TCP/UDP Checksum has not been calculated */
3707         if (adapter->hw.mac_type <= e1000_82547_rev_2) {
3708                 if (!(status & E1000_RXD_STAT_TCPCS))
3709                         return;
3710         } else {
3711                 if (!(status & (E1000_RXD_STAT_TCPCS | E1000_RXD_STAT_UDPCS)))
3712                         return;
3713         }
3714         /* It must be a TCP or UDP packet with a valid checksum */
3715         if (likely(status & E1000_RXD_STAT_TCPCS)) {
3716                 /* TCP checksum is good */
3717                 skb->ip_summed = CHECKSUM_UNNECESSARY;
3718         } else if (adapter->hw.mac_type > e1000_82547_rev_2) {
3719                 /* IP fragment with UDP payload */
3720                 /* Hardware complements the payload checksum, so we undo it
3721                  * and then put the value in host order for further stack use.
3722                  */
3723                 csum = ntohl(csum ^ 0xFFFF);
3724                 skb->csum = csum;
3725                 skb->ip_summed = CHECKSUM_COMPLETE;
3726         }
3727         adapter->hw_csum_good++;
3728 }
3729
3730 /**
3731  * e1000_clean_rx_irq - Send received data up the network stack; legacy
3732  * @adapter: board private structure
3733  **/
3734
3735 static boolean_t
3736 #ifdef CONFIG_E1000_NAPI
3737 e1000_clean_rx_irq(struct e1000_adapter *adapter,
3738                    struct e1000_rx_ring *rx_ring,
3739                    int *work_done, int work_to_do)
3740 #else
3741 e1000_clean_rx_irq(struct e1000_adapter *adapter,
3742                    struct e1000_rx_ring *rx_ring)
3743 #endif
3744 {
3745         struct net_device *netdev = adapter->netdev;
3746         struct pci_dev *pdev = adapter->pdev;
3747         struct e1000_rx_desc *rx_desc, *next_rxd;
3748         struct e1000_buffer *buffer_info, *next_buffer;
3749         unsigned long flags;
3750         uint32_t length;
3751         uint8_t last_byte;
3752         unsigned int i;
3753         int cleaned_count = 0;
3754         boolean_t cleaned = FALSE;
3755
3756         i = rx_ring->next_to_clean;
3757         rx_desc = E1000_RX_DESC(*rx_ring, i);
3758         buffer_info = &rx_ring->buffer_info[i];
3759
3760         while (rx_desc->status & E1000_RXD_STAT_DD) {
3761                 struct sk_buff *skb;
3762                 u8 status;
3763 #ifdef CONFIG_E1000_NAPI
3764                 if (*work_done >= work_to_do)
3765                         break;
3766                 (*work_done)++;
3767 #endif
3768                 status = rx_desc->status;
3769                 skb = buffer_info->skb;
3770                 buffer_info->skb = NULL;
3771
3772                 prefetch(skb->data - NET_IP_ALIGN);
3773
3774                 if (++i == rx_ring->count) i = 0;
3775                 next_rxd = E1000_RX_DESC(*rx_ring, i);
3776                 prefetch(next_rxd);
3777
3778                 next_buffer = &rx_ring->buffer_info[i];
3779
3780                 cleaned = TRUE;
3781                 cleaned_count++;
3782                 pci_unmap_single(pdev,
3783                                  buffer_info->dma,
3784                                  buffer_info->length,
3785                                  PCI_DMA_FROMDEVICE);
3786
3787                 length = le16_to_cpu(rx_desc->length);
3788
3789                 if (unlikely(!(status & E1000_RXD_STAT_EOP))) {
3790                         /* All receives must fit into a single buffer */
3791                         E1000_DBG("%s: Receive packet consumed multiple"
3792                                   " buffers\n", netdev->name);
3793                         /* recycle */
3794                         buffer_info->skb = skb;
3795                         goto next_desc;
3796                 }
3797
3798                 if (unlikely(rx_desc->errors & E1000_RXD_ERR_FRAME_ERR_MASK)) {
3799                         last_byte = *(skb->data + length - 1);
3800                         if (TBI_ACCEPT(&adapter->hw, status,
3801                                       rx_desc->errors, length, last_byte)) {
3802                                 spin_lock_irqsave(&adapter->stats_lock, flags);
3803                                 e1000_tbi_adjust_stats(&adapter->hw,
3804                                                        &adapter->stats,
3805                                                        length, skb->data);
3806                                 spin_unlock_irqrestore(&adapter->stats_lock,
3807                                                        flags);
3808                                 length--;
3809                         } else {
3810                                 /* recycle */
3811                                 buffer_info->skb = skb;
3812                                 goto next_desc;
3813                         }
3814                 }
3815
3816                 /* adjust length to remove Ethernet CRC, this must be
3817                  * done after the TBI_ACCEPT workaround above */
3818                 length -= 4;
3819
3820                 /* code added for copybreak, this should improve
3821                  * performance for small packets with large amounts
3822                  * of reassembly being done in the stack */
3823 #define E1000_CB_LENGTH 256
3824                 if (length < E1000_CB_LENGTH) {
3825                         struct sk_buff *new_skb =
3826                             netdev_alloc_skb(netdev, length + NET_IP_ALIGN);
3827                         if (new_skb) {
3828                                 skb_reserve(new_skb, NET_IP_ALIGN);
3829                                 memcpy(new_skb->data - NET_IP_ALIGN,
3830                                        skb->data - NET_IP_ALIGN,
3831                                        length + NET_IP_ALIGN);
3832                                 /* save the skb in buffer_info as good */
3833                                 buffer_info->skb = skb;
3834                                 skb = new_skb;
3835                                 skb_put(skb, length);
3836                         }
3837                 } else
3838                         skb_put(skb, length);
3839
3840                 /* end copybreak code */
3841
3842                 /* Receive Checksum Offload */
3843                 e1000_rx_checksum(adapter,
3844                                   (uint32_t)(status) |
3845                                   ((uint32_t)(rx_desc->errors) << 24),
3846                                   le16_to_cpu(rx_desc->csum), skb);
3847
3848                 skb->protocol = eth_type_trans(skb, netdev);
3849 #ifdef CONFIG_E1000_NAPI
3850                 if (unlikely(adapter->vlgrp &&
3851                             (status & E1000_RXD_STAT_VP))) {
3852                         vlan_hwaccel_receive_skb(skb, adapter->vlgrp,
3853                                                  le16_to_cpu(rx_desc->special) &
3854                                                  E1000_RXD_SPC_VLAN_MASK);
3855                 } else {
3856                         netif_receive_skb(skb);
3857                 }
3858 #else /* CONFIG_E1000_NAPI */
3859                 if (unlikely(adapter->vlgrp &&
3860                             (status & E1000_RXD_STAT_VP))) {
3861                         vlan_hwaccel_rx(skb, adapter->vlgrp,
3862                                         le16_to_cpu(rx_desc->special) &
3863                                         E1000_RXD_SPC_VLAN_MASK);
3864                 } else {
3865                         netif_rx(skb);
3866                 }
3867 #endif /* CONFIG_E1000_NAPI */
3868                 netdev->last_rx = jiffies;
3869
3870 next_desc:
3871                 rx_desc->status = 0;
3872
3873                 /* return some buffers to hardware, one at a time is too slow */
3874                 if (unlikely(cleaned_count >= E1000_RX_BUFFER_WRITE)) {
3875                         adapter->alloc_rx_buf(adapter, rx_ring, cleaned_count);
3876                         cleaned_count = 0;
3877                 }
3878
3879                 /* use prefetched values */
3880                 rx_desc = next_rxd;
3881                 buffer_info = next_buffer;
3882         }
3883         rx_ring->next_to_clean = i;
3884
3885         cleaned_count = E1000_DESC_UNUSED(rx_ring);
3886         if (cleaned_count)
3887                 adapter->alloc_rx_buf(adapter, rx_ring, cleaned_count);
3888
3889         return cleaned;
3890 }
3891
3892 /**
3893  * e1000_clean_rx_irq_ps - Send received data up the network stack; packet split
3894  * @adapter: board private structure
3895  **/
3896
3897 static boolean_t
3898 #ifdef CONFIG_E1000_NAPI
3899 e1000_clean_rx_irq_ps(struct e1000_adapter *adapter,
3900                       struct e1000_rx_ring *rx_ring,
3901                       int *work_done, int work_to_do)
3902 #else
3903 e1000_clean_rx_irq_ps(struct e1000_adapter *adapter,
3904                       struct e1000_rx_ring *rx_ring)
3905 #endif
3906 {
3907         union e1000_rx_desc_packet_split *rx_desc, *next_rxd;
3908         struct net_device *netdev = adapter->netdev;
3909         struct pci_dev *pdev = adapter->pdev;
3910         struct e1000_buffer *buffer_info, *next_buffer;
3911         struct e1000_ps_page *ps_page;
3912         struct e1000_ps_page_dma *ps_page_dma;
3913         struct sk_buff *skb;
3914         unsigned int i, j;
3915         uint32_t length, staterr;
3916         int cleaned_count = 0;
3917         boolean_t cleaned = FALSE;
3918
3919         i = rx_ring->next_to_clean;
3920         rx_desc = E1000_RX_DESC_PS(*rx_ring, i);
3921         staterr = le32_to_cpu(rx_desc->wb.middle.status_error);
3922         buffer_info = &rx_ring->buffer_info[i];
3923
3924         while (staterr & E1000_RXD_STAT_DD) {
3925                 ps_page = &rx_ring->ps_page[i];
3926                 ps_page_dma = &rx_ring->ps_page_dma[i];
3927 #ifdef CONFIG_E1000_NAPI
3928                 if (unlikely(*work_done >= work_to_do))
3929                         break;
3930                 (*work_done)++;
3931 #endif
3932                 skb = buffer_info->skb;
3933
3934                 /* in the packet split case this is header only */
3935                 prefetch(skb->data - NET_IP_ALIGN);
3936
3937                 if (++i == rx_ring->count) i = 0;
3938                 next_rxd = E1000_RX_DESC_PS(*rx_ring, i);
3939                 prefetch(next_rxd);
3940
3941                 next_buffer = &rx_ring->buffer_info[i];
3942
3943                 cleaned = TRUE;
3944                 cleaned_count++;
3945                 pci_unmap_single(pdev, buffer_info->dma,
3946                                  buffer_info->length,
3947                                  PCI_DMA_FROMDEVICE);
3948
3949                 if (unlikely(!(staterr & E1000_RXD_STAT_EOP))) {
3950                         E1000_DBG("%s: Packet Split buffers didn't pick up"
3951                                   " the full packet\n", netdev->name);
3952                         dev_kfree_skb_irq(skb);
3953                         goto next_desc;
3954                 }
3955
3956                 if (unlikely(staterr & E1000_RXDEXT_ERR_FRAME_ERR_MASK)) {
3957                         dev_kfree_skb_irq(skb);
3958                         goto next_desc;
3959                 }
3960
3961                 length = le16_to_cpu(rx_desc->wb.middle.length0);
3962
3963                 if (unlikely(!length)) {
3964                         E1000_DBG("%s: Last part of the packet spanning"
3965                                   " multiple descriptors\n", netdev->name);
3966                         dev_kfree_skb_irq(skb);
3967                         goto next_desc;
3968                 }
3969
3970                 /* Good Receive */
3971                 skb_put(skb, length);
3972
3973                 {
3974                 /* this looks ugly, but it seems compiler issues make it
3975                    more efficient than reusing j */
3976                 int l1 = le16_to_cpu(rx_desc->wb.upper.length[0]);
3977
3978                 /* page alloc/put takes too long and effects small packet
3979                  * throughput, so unsplit small packets and save the alloc/put*/
3980                 if (l1 && ((length + l1) <= adapter->rx_ps_bsize0)) {
3981                         u8 *vaddr;
3982                         /* there is no documentation about how to call
3983                          * kmap_atomic, so we can't hold the mapping
3984                          * very long */
3985                         pci_dma_sync_single_for_cpu(pdev,
3986                                 ps_page_dma->ps_page_dma[0],
3987                                 PAGE_SIZE,
3988                                 PCI_DMA_FROMDEVICE);
3989                         vaddr = kmap_atomic(ps_page->ps_page[0],
3990                                             KM_SKB_DATA_SOFTIRQ);
3991                         memcpy(skb->tail, vaddr, l1);
3992                         kunmap_atomic(vaddr, KM_SKB_DATA_SOFTIRQ);
3993                         pci_dma_sync_single_for_device(pdev,
3994                                 ps_page_dma->ps_page_dma[0],
3995                                 PAGE_SIZE, PCI_DMA_FROMDEVICE);
3996                         /* remove the CRC */
3997                         l1 -= 4;
3998                         skb_put(skb, l1);
3999                         goto copydone;
4000                 } /* if */
4001                 }
4002                 
4003                 for (j = 0; j < adapter->rx_ps_pages; j++) {
4004                         if (!(length= le16_to_cpu(rx_desc->wb.upper.length[j])))
4005                                 break;
4006                         pci_unmap_page(pdev, ps_page_dma->ps_page_dma[j],
4007                                         PAGE_SIZE, PCI_DMA_FROMDEVICE);
4008                         ps_page_dma->ps_page_dma[j] = 0;
4009                         skb_fill_page_desc(skb, j, ps_page->ps_page[j], 0,
4010                                            length);
4011                         ps_page->ps_page[j] = NULL;
4012                         skb->len += length;
4013                         skb->data_len += length;
4014                         skb->truesize += length;
4015                 }
4016
4017                 /* strip the ethernet crc, problem is we're using pages now so
4018                  * this whole operation can get a little cpu intensive */
4019                 pskb_trim(skb, skb->len - 4);
4020
4021 copydone:
4022                 e1000_rx_checksum(adapter, staterr,
4023                                   le16_to_cpu(rx_desc->wb.lower.hi_dword.csum_ip.csum), skb);
4024                 skb->protocol = eth_type_trans(skb, netdev);
4025
4026                 if (likely(rx_desc->wb.upper.header_status &
4027                            cpu_to_le16(E1000_RXDPS_HDRSTAT_HDRSP)))
4028                         adapter->rx_hdr_split++;
4029 #ifdef CONFIG_E1000_NAPI
4030                 if (unlikely(adapter->vlgrp && (staterr & E1000_RXD_STAT_VP))) {
4031                         vlan_hwaccel_receive_skb(skb, adapter->vlgrp,
4032                                 le16_to_cpu(rx_desc->wb.middle.vlan) &
4033                                 E1000_RXD_SPC_VLAN_MASK);
4034                 } else {
4035                         netif_receive_skb(skb);
4036                 }
4037 #else /* CONFIG_E1000_NAPI */
4038                 if (unlikely(adapter->vlgrp && (staterr & E1000_RXD_STAT_VP))) {
4039                         vlan_hwaccel_rx(skb, adapter->vlgrp,
4040                                 le16_to_cpu(rx_desc->wb.middle.vlan) &
4041                                 E1000_RXD_SPC_VLAN_MASK);
4042                 } else {
4043                         netif_rx(skb);
4044                 }
4045 #endif /* CONFIG_E1000_NAPI */
4046                 netdev->last_rx = jiffies;
4047
4048 next_desc:
4049                 rx_desc->wb.middle.status_error &= cpu_to_le32(~0xFF);
4050                 buffer_info->skb = NULL;
4051
4052                 /* return some buffers to hardware, one at a time is too slow */
4053                 if (unlikely(cleaned_count >= E1000_RX_BUFFER_WRITE)) {
4054                         adapter->alloc_rx_buf(adapter, rx_ring, cleaned_count);
4055                         cleaned_count = 0;
4056                 }
4057
4058                 /* use prefetched values */
4059                 rx_desc = next_rxd;
4060                 buffer_info = next_buffer;
4061
4062                 staterr = le32_to_cpu(rx_desc->wb.middle.status_error);
4063         }
4064         rx_ring->next_to_clean = i;
4065
4066         cleaned_count = E1000_DESC_UNUSED(rx_ring);
4067         if (cleaned_count)
4068                 adapter->alloc_rx_buf(adapter, rx_ring, cleaned_count);
4069
4070         return cleaned;
4071 }
4072
4073 /**
4074  * e1000_alloc_rx_buffers - Replace used receive buffers; legacy & extended
4075  * @adapter: address of board private structure
4076  **/
4077
4078 static void
4079 e1000_alloc_rx_buffers(struct e1000_adapter *adapter,
4080                        struct e1000_rx_ring *rx_ring,
4081                        int cleaned_count)
4082 {
4083         struct net_device *netdev = adapter->netdev;
4084         struct pci_dev *pdev = adapter->pdev;
4085         struct e1000_rx_desc *rx_desc;
4086         struct e1000_buffer *buffer_info;
4087         struct sk_buff *skb;
4088         unsigned int i;
4089         unsigned int bufsz = adapter->rx_buffer_len + NET_IP_ALIGN;
4090
4091         i = rx_ring->next_to_use;
4092         buffer_info = &rx_ring->buffer_info[i];
4093
4094         while (cleaned_count--) {
4095                 skb = buffer_info->skb;
4096                 if (skb) {
4097                         skb_trim(skb, 0);
4098                         goto map_skb;
4099                 }
4100
4101                 skb = netdev_alloc_skb(netdev, bufsz);
4102                 if (unlikely(!skb)) {
4103                         /* Better luck next round */
4104                         adapter->alloc_rx_buff_failed++;
4105                         break;
4106                 }
4107
4108                 /* Fix for errata 23, can't cross 64kB boundary */
4109                 if (!e1000_check_64k_bound(adapter, skb->data, bufsz)) {
4110                         struct sk_buff *oldskb = skb;
4111                         DPRINTK(RX_ERR, ERR, "skb align check failed: %u bytes "
4112                                              "at %p\n", bufsz, skb->data);
4113                         /* Try again, without freeing the previous */
4114                         skb = netdev_alloc_skb(netdev, bufsz);
4115                         /* Failed allocation, critical failure */
4116                         if (!skb) {
4117                                 dev_kfree_skb(oldskb);
4118                                 break;
4119                         }
4120
4121                         if (!e1000_check_64k_bound(adapter, skb->data, bufsz)) {
4122                                 /* give up */
4123                                 dev_kfree_skb(skb);
4124                                 dev_kfree_skb(oldskb);
4125                                 break; /* while !buffer_info->skb */
4126                         }
4127
4128                         /* Use new allocation */
4129                         dev_kfree_skb(oldskb);
4130                 }
4131                 /* Make buffer alignment 2 beyond a 16 byte boundary
4132                  * this will result in a 16 byte aligned IP header after
4133                  * the 14 byte MAC header is removed
4134                  */
4135                 skb_reserve(skb, NET_IP_ALIGN);
4136
4137                 buffer_info->skb = skb;
4138                 buffer_info->length = adapter->rx_buffer_len;
4139 map_skb:
4140                 buffer_info->dma = pci_map_single(pdev,
4141                                                   skb->data,
4142                                                   adapter->rx_buffer_len,
4143                                                   PCI_DMA_FROMDEVICE);
4144
4145                 /* Fix for errata 23, can't cross 64kB boundary */
4146                 if (!e1000_check_64k_bound(adapter,
4147                                         (void *)(unsigned long)buffer_info->dma,
4148                                         adapter->rx_buffer_len)) {
4149                         DPRINTK(RX_ERR, ERR,
4150                                 "dma align check failed: %u bytes at %p\n",
4151                                 adapter->rx_buffer_len,
4152                                 (void *)(unsigned long)buffer_info->dma);
4153                         dev_kfree_skb(skb);
4154                         buffer_info->skb = NULL;
4155
4156                         pci_unmap_single(pdev, buffer_info->dma,
4157                                          adapter->rx_buffer_len,
4158                                          PCI_DMA_FROMDEVICE);
4159
4160                         break; /* while !buffer_info->skb */
4161                 }
4162                 rx_desc = E1000_RX_DESC(*rx_ring, i);
4163                 rx_desc->buffer_addr = cpu_to_le64(buffer_info->dma);
4164
4165                 if (unlikely(++i == rx_ring->count))
4166                         i = 0;
4167                 buffer_info = &rx_ring->buffer_info[i];
4168         }
4169
4170         if (likely(rx_ring->next_to_use != i)) {
4171                 rx_ring->next_to_use = i;
4172                 if (unlikely(i-- == 0))
4173                         i = (rx_ring->count - 1);
4174
4175                 /* Force memory writes to complete before letting h/w
4176                  * know there are new descriptors to fetch.  (Only
4177                  * applicable for weak-ordered memory model archs,
4178                  * such as IA-64). */
4179                 wmb();
4180                 writel(i, adapter->hw.hw_addr + rx_ring->rdt);
4181         }
4182 }
4183
4184 /**
4185  * e1000_alloc_rx_buffers_ps - Replace used receive buffers; packet split
4186  * @adapter: address of board private structure
4187  **/
4188
4189 static void
4190 e1000_alloc_rx_buffers_ps(struct e1000_adapter *adapter,
4191                           struct e1000_rx_ring *rx_ring,
4192                           int cleaned_count)
4193 {
4194         struct net_device *netdev = adapter->netdev;
4195         struct pci_dev *pdev = adapter->pdev;
4196         union e1000_rx_desc_packet_split *rx_desc;
4197         struct e1000_buffer *buffer_info;
4198         struct e1000_ps_page *ps_page;
4199         struct e1000_ps_page_dma *ps_page_dma;
4200         struct sk_buff *skb;
4201         unsigned int i, j;
4202
4203         i = rx_ring->next_to_use;
4204         buffer_info = &rx_ring->buffer_info[i];
4205         ps_page = &rx_ring->ps_page[i];
4206         ps_page_dma = &rx_ring->ps_page_dma[i];
4207
4208         while (cleaned_count--) {
4209                 rx_desc = E1000_RX_DESC_PS(*rx_ring, i);
4210
4211                 for (j = 0; j < PS_PAGE_BUFFERS; j++) {
4212                         if (j < adapter->rx_ps_pages) {
4213                                 if (likely(!ps_page->ps_page[j])) {
4214                                         ps_page->ps_page[j] =
4215                                                 alloc_page(GFP_ATOMIC);
4216                                         if (unlikely(!ps_page->ps_page[j])) {
4217                                                 adapter->alloc_rx_buff_failed++;
4218                                                 goto no_buffers;
4219                                         }
4220                                         ps_page_dma->ps_page_dma[j] =
4221                                                 pci_map_page(pdev,
4222                                                             ps_page->ps_page[j],
4223                                                             0, PAGE_SIZE,
4224                                                             PCI_DMA_FROMDEVICE);
4225                                 }
4226                                 /* Refresh the desc even if buffer_addrs didn't
4227                                  * change because each write-back erases
4228                                  * this info.
4229                                  */
4230                                 rx_desc->read.buffer_addr[j+1] =
4231                                      cpu_to_le64(ps_page_dma->ps_page_dma[j]);
4232                         } else
4233                                 rx_desc->read.buffer_addr[j+1] = ~0;
4234                 }
4235
4236                 skb = netdev_alloc_skb(netdev,
4237                                        adapter->rx_ps_bsize0 + NET_IP_ALIGN);
4238
4239                 if (unlikely(!skb)) {
4240                         adapter->alloc_rx_buff_failed++;
4241                         break;
4242                 }
4243
4244                 /* Make buffer alignment 2 beyond a 16 byte boundary
4245                  * this will result in a 16 byte aligned IP header after
4246                  * the 14 byte MAC header is removed
4247                  */
4248                 skb_reserve(skb, NET_IP_ALIGN);
4249
4250                 buffer_info->skb = skb;
4251                 buffer_info->length = adapter->rx_ps_bsize0;
4252                 buffer_info->dma = pci_map_single(pdev, skb->data,
4253                                                   adapter->rx_ps_bsize0,
4254                                                   PCI_DMA_FROMDEVICE);
4255
4256                 rx_desc->read.buffer_addr[0] = cpu_to_le64(buffer_info->dma);
4257
4258                 if (unlikely(++i == rx_ring->count)) i = 0;
4259                 buffer_info = &rx_ring->buffer_info[i];
4260                 ps_page = &rx_ring->ps_page[i];
4261                 ps_page_dma = &rx_ring->ps_page_dma[i];
4262         }
4263
4264 no_buffers:
4265         if (likely(rx_ring->next_to_use != i)) {
4266                 rx_ring->next_to_use = i;
4267                 if (unlikely(i-- == 0)) i = (rx_ring->count - 1);
4268
4269                 /* Force memory writes to complete before letting h/w
4270                  * know there are new descriptors to fetch.  (Only
4271                  * applicable for weak-ordered memory model archs,
4272                  * such as IA-64). */
4273                 wmb();
4274                 /* Hardware increments by 16 bytes, but packet split
4275                  * descriptors are 32 bytes...so we increment tail
4276                  * twice as much.
4277                  */
4278                 writel(i<<1, adapter->hw.hw_addr + rx_ring->rdt);
4279         }
4280 }
4281
4282 /**
4283  * e1000_smartspeed - Workaround for SmartSpeed on 82541 and 82547 controllers.
4284  * @adapter:
4285  **/
4286
4287 static void
4288 e1000_smartspeed(struct e1000_adapter *adapter)
4289 {
4290         uint16_t phy_status;
4291         uint16_t phy_ctrl;
4292
4293         if ((adapter->hw.phy_type != e1000_phy_igp) || !adapter->hw.autoneg ||
4294            !(adapter->hw.autoneg_advertised & ADVERTISE_1000_FULL))
4295                 return;
4296
4297         if (adapter->smartspeed == 0) {
4298                 /* If Master/Slave config fault is asserted twice,
4299                  * we assume back-to-back */
4300                 e1000_read_phy_reg(&adapter->hw, PHY_1000T_STATUS, &phy_status);
4301                 if (!(phy_status & SR_1000T_MS_CONFIG_FAULT)) return;
4302                 e1000_read_phy_reg(&adapter->hw, PHY_1000T_STATUS, &phy_status);
4303                 if (!(phy_status & SR_1000T_MS_CONFIG_FAULT)) return;
4304                 e1000_read_phy_reg(&adapter->hw, PHY_1000T_CTRL, &phy_ctrl);
4305                 if (phy_ctrl & CR_1000T_MS_ENABLE) {
4306                         phy_ctrl &= ~CR_1000T_MS_ENABLE;
4307                         e1000_write_phy_reg(&adapter->hw, PHY_1000T_CTRL,
4308                                             phy_ctrl);
4309                         adapter->smartspeed++;
4310                         if (!e1000_phy_setup_autoneg(&adapter->hw) &&
4311                            !e1000_read_phy_reg(&adapter->hw, PHY_CTRL,
4312                                                &phy_ctrl)) {
4313                                 phy_ctrl |= (MII_CR_AUTO_NEG_EN |
4314                                              MII_CR_RESTART_AUTO_NEG);
4315                                 e1000_write_phy_reg(&adapter->hw, PHY_CTRL,
4316                                                     phy_ctrl);
4317                         }
4318                 }
4319                 return;
4320         } else if (adapter->smartspeed == E1000_SMARTSPEED_DOWNSHIFT) {
4321                 /* If still no link, perhaps using 2/3 pair cable */
4322                 e1000_read_phy_reg(&adapter->hw, PHY_1000T_CTRL, &phy_ctrl);
4323                 phy_ctrl |= CR_1000T_MS_ENABLE;
4324                 e1000_write_phy_reg(&adapter->hw, PHY_1000T_CTRL, phy_ctrl);
4325                 if (!e1000_phy_setup_autoneg(&adapter->hw) &&
4326                    !e1000_read_phy_reg(&adapter->hw, PHY_CTRL, &phy_ctrl)) {
4327                         phy_ctrl |= (MII_CR_AUTO_NEG_EN |
4328                                      MII_CR_RESTART_AUTO_NEG);
4329                         e1000_write_phy_reg(&adapter->hw, PHY_CTRL, phy_ctrl);
4330                 }
4331         }
4332         /* Restart process after E1000_SMARTSPEED_MAX iterations */
4333         if (adapter->smartspeed++ == E1000_SMARTSPEED_MAX)
4334                 adapter->smartspeed = 0;
4335 }
4336
4337 /**
4338  * e1000_ioctl -
4339  * @netdev:
4340  * @ifreq:
4341  * @cmd:
4342  **/
4343
4344 static int
4345 e1000_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
4346 {
4347         switch (cmd) {
4348         case SIOCGMIIPHY:
4349         case SIOCGMIIREG:
4350         case SIOCSMIIREG:
4351                 return e1000_mii_ioctl(netdev, ifr, cmd);
4352         default:
4353                 return -EOPNOTSUPP;
4354         }
4355 }
4356
4357 /**
4358  * e1000_mii_ioctl -
4359  * @netdev:
4360  * @ifreq:
4361  * @cmd:
4362  **/
4363
4364 static int
4365 e1000_mii_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
4366 {
4367         struct e1000_adapter *adapter = netdev_priv(netdev);
4368         struct mii_ioctl_data *data = if_mii(ifr);
4369         int retval;
4370         uint16_t mii_reg;
4371         uint16_t spddplx;
4372         unsigned long flags;
4373
4374         if (adapter->hw.media_type != e1000_media_type_copper)
4375                 return -EOPNOTSUPP;
4376
4377         switch (cmd) {
4378         case SIOCGMIIPHY:
4379                 data->phy_id = adapter->hw.phy_addr;
4380                 break;
4381         case SIOCGMIIREG:
4382                 if (!capable(CAP_NET_ADMIN))
4383                         return -EPERM;
4384                 spin_lock_irqsave(&adapter->stats_lock, flags);
4385                 if (e1000_read_phy_reg(&adapter->hw, data->reg_num & 0x1F,
4386                                    &data->val_out)) {
4387                         spin_unlock_irqrestore(&adapter->stats_lock, flags);
4388                         return -EIO;
4389                 }
4390                 spin_unlock_irqrestore(&adapter->stats_lock, flags);
4391                 break;
4392         case SIOCSMIIREG:
4393                 if (!capable(CAP_NET_ADMIN))
4394                         return -EPERM;
4395                 if (data->reg_num & ~(0x1F))
4396                         return -EFAULT;
4397                 mii_reg = data->val_in;
4398                 spin_lock_irqsave(&adapter->stats_lock, flags);
4399                 if (e1000_write_phy_reg(&adapter->hw, data->reg_num,
4400                                         mii_reg)) {
4401                         spin_unlock_irqrestore(&adapter->stats_lock, flags);
4402                         return -EIO;
4403                 }
4404                 if (adapter->hw.media_type == e1000_media_type_copper) {
4405                         switch (data->reg_num) {
4406                         case PHY_CTRL:
4407                                 if (mii_reg & MII_CR_POWER_DOWN)
4408                                         break;
4409                                 if (mii_reg & MII_CR_AUTO_NEG_EN) {
4410                                         adapter->hw.autoneg = 1;
4411                                         adapter->hw.autoneg_advertised = 0x2F;
4412                                 } else {
4413                                         if (mii_reg & 0x40)
4414                                                 spddplx = SPEED_1000;
4415                                         else if (mii_reg & 0x2000)
4416                                                 spddplx = SPEED_100;
4417                                         else
4418                                                 spddplx = SPEED_10;
4419                                         spddplx += (mii_reg & 0x100)
4420                                                    ? DUPLEX_FULL :
4421                                                    DUPLEX_HALF;
4422                                         retval = e1000_set_spd_dplx(adapter,
4423                                                                     spddplx);
4424                                         if (retval) {
4425                                                 spin_unlock_irqrestore(
4426                                                         &adapter->stats_lock,
4427                                                         flags);
4428                                                 return retval;
4429                                         }
4430                                 }
4431                                 if (netif_running(adapter->netdev))
4432                                         e1000_reinit_locked(adapter);
4433                                 else
4434                                         e1000_reset(adapter);
4435                                 break;
4436                         case M88E1000_PHY_SPEC_CTRL:
4437                         case M88E1000_EXT_PHY_SPEC_CTRL:
4438                                 if (e1000_phy_reset(&adapter->hw)) {
4439                                         spin_unlock_irqrestore(
4440                                                 &adapter->stats_lock, flags);
4441                                         return -EIO;
4442                                 }
4443                                 break;
4444                         }
4445                 } else {
4446                         switch (data->reg_num) {
4447                         case PHY_CTRL:
4448                                 if (mii_reg & MII_CR_POWER_DOWN)
4449                                         break;
4450                                 if (netif_running(adapter->netdev))
4451                                         e1000_reinit_locked(adapter);
4452                                 else
4453                                         e1000_reset(adapter);
4454                                 break;
4455                         }
4456                 }
4457                 spin_unlock_irqrestore(&adapter->stats_lock, flags);
4458                 break;
4459         default:
4460                 return -EOPNOTSUPP;
4461         }
4462         return E1000_SUCCESS;
4463 }
4464
4465 void
4466 e1000_pci_set_mwi(struct e1000_hw *hw)
4467 {
4468         struct e1000_adapter *adapter = hw->back;
4469         int ret_val = pci_set_mwi(adapter->pdev);
4470
4471         if (ret_val)
4472                 DPRINTK(PROBE, ERR, "Error in setting MWI\n");
4473 }
4474
4475 void
4476 e1000_pci_clear_mwi(struct e1000_hw *hw)
4477 {
4478         struct e1000_adapter *adapter = hw->back;
4479
4480         pci_clear_mwi(adapter->pdev);
4481 }
4482
4483 void
4484 e1000_read_pci_cfg(struct e1000_hw *hw, uint32_t reg, uint16_t *value)
4485 {
4486         struct e1000_adapter *adapter = hw->back;
4487
4488         pci_read_config_word(adapter->pdev, reg, value);
4489 }
4490
4491 void
4492 e1000_write_pci_cfg(struct e1000_hw *hw, uint32_t reg, uint16_t *value)
4493 {
4494         struct e1000_adapter *adapter = hw->back;
4495
4496         pci_write_config_word(adapter->pdev, reg, *value);
4497 }
4498
4499 int32_t
4500 e1000_read_pcie_cap_reg(struct e1000_hw *hw, uint32_t reg, uint16_t *value)
4501 {
4502     struct e1000_adapter *adapter = hw->back;
4503     uint16_t cap_offset;
4504
4505     cap_offset = pci_find_capability(adapter->pdev, PCI_CAP_ID_EXP);
4506     if (!cap_offset)
4507         return -E1000_ERR_CONFIG;
4508
4509     pci_read_config_word(adapter->pdev, cap_offset + reg, value);
4510
4511     return E1000_SUCCESS;
4512 }
4513
4514
4515 void
4516 e1000_io_write(struct e1000_hw *hw, unsigned long port, uint32_t value)
4517 {
4518         outl(value, port);
4519 }
4520
4521 static void
4522 e1000_vlan_rx_register(struct net_device *netdev, struct vlan_group *grp)
4523 {
4524         struct e1000_adapter *adapter = netdev_priv(netdev);
4525         uint32_t ctrl, rctl;
4526
4527         e1000_irq_disable(adapter);
4528         adapter->vlgrp = grp;
4529
4530         if (grp) {
4531                 /* enable VLAN tag insert/strip */
4532                 ctrl = E1000_READ_REG(&adapter->hw, CTRL);
4533                 ctrl |= E1000_CTRL_VME;
4534                 E1000_WRITE_REG(&adapter->hw, CTRL, ctrl);
4535
4536                 if (adapter->hw.mac_type != e1000_ich8lan) {
4537                 /* enable VLAN receive filtering */
4538                 rctl = E1000_READ_REG(&adapter->hw, RCTL);
4539                 rctl |= E1000_RCTL_VFE;
4540                 rctl &= ~E1000_RCTL_CFIEN;
4541                 E1000_WRITE_REG(&adapter->hw, RCTL, rctl);
4542                 e1000_update_mng_vlan(adapter);
4543                 }
4544         } else {
4545                 /* disable VLAN tag insert/strip */
4546                 ctrl = E1000_READ_REG(&adapter->hw, CTRL);
4547                 ctrl &= ~E1000_CTRL_VME;
4548                 E1000_WRITE_REG(&adapter->hw, CTRL, ctrl);
4549
4550                 if (adapter->hw.mac_type != e1000_ich8lan) {
4551                 /* disable VLAN filtering */
4552                 rctl = E1000_READ_REG(&adapter->hw, RCTL);
4553                 rctl &= ~E1000_RCTL_VFE;
4554                 E1000_WRITE_REG(&adapter->hw, RCTL, rctl);
4555                 if (adapter->mng_vlan_id != (uint16_t)E1000_MNG_VLAN_NONE) {
4556                         e1000_vlan_rx_kill_vid(netdev, adapter->mng_vlan_id);
4557                         adapter->mng_vlan_id = E1000_MNG_VLAN_NONE;
4558                 }
4559                 }
4560         }
4561
4562         e1000_irq_enable(adapter);
4563 }
4564
4565 static void
4566 e1000_vlan_rx_add_vid(struct net_device *netdev, uint16_t vid)
4567 {
4568         struct e1000_adapter *adapter = netdev_priv(netdev);
4569         uint32_t vfta, index;
4570
4571         if ((adapter->hw.mng_cookie.status &
4572              E1000_MNG_DHCP_COOKIE_STATUS_VLAN_SUPPORT) &&
4573             (vid == adapter->mng_vlan_id))
4574                 return;
4575         /* add VID to filter table */
4576         index = (vid >> 5) & 0x7F;
4577         vfta = E1000_READ_REG_ARRAY(&adapter->hw, VFTA, index);
4578         vfta |= (1 << (vid & 0x1F));
4579         e1000_write_vfta(&adapter->hw, index, vfta);
4580 }
4581
4582 static void
4583 e1000_vlan_rx_kill_vid(struct net_device *netdev, uint16_t vid)
4584 {
4585         struct e1000_adapter *adapter = netdev_priv(netdev);
4586         uint32_t vfta, index;
4587
4588         e1000_irq_disable(adapter);
4589
4590         if (adapter->vlgrp)
4591                 adapter->vlgrp->vlan_devices[vid] = NULL;
4592
4593         e1000_irq_enable(adapter);
4594
4595         if ((adapter->hw.mng_cookie.status &
4596              E1000_MNG_DHCP_COOKIE_STATUS_VLAN_SUPPORT) &&
4597             (vid == adapter->mng_vlan_id)) {
4598                 /* release control to f/w */
4599                 e1000_release_hw_control(adapter);
4600                 return;
4601         }
4602
4603         /* remove VID from filter table */
4604         index = (vid >> 5) & 0x7F;
4605         vfta = E1000_READ_REG_ARRAY(&adapter->hw, VFTA, index);
4606         vfta &= ~(1 << (vid & 0x1F));
4607         e1000_write_vfta(&adapter->hw, index, vfta);
4608 }
4609
4610 static void
4611 e1000_restore_vlan(struct e1000_adapter *adapter)
4612 {
4613         e1000_vlan_rx_register(adapter->netdev, adapter->vlgrp);
4614
4615         if (adapter->vlgrp) {
4616                 uint16_t vid;
4617                 for (vid = 0; vid < VLAN_GROUP_ARRAY_LEN; vid++) {
4618                         if (!adapter->vlgrp->vlan_devices[vid])
4619                                 continue;
4620                         e1000_vlan_rx_add_vid(adapter->netdev, vid);
4621                 }
4622         }
4623 }
4624
4625 int
4626 e1000_set_spd_dplx(struct e1000_adapter *adapter, uint16_t spddplx)
4627 {
4628         adapter->hw.autoneg = 0;
4629
4630         /* Fiber NICs only allow 1000 gbps Full duplex */
4631         if ((adapter->hw.media_type == e1000_media_type_fiber) &&
4632                 spddplx != (SPEED_1000 + DUPLEX_FULL)) {
4633                 DPRINTK(PROBE, ERR, "Unsupported Speed/Duplex configuration\n");
4634                 return -EINVAL;
4635         }
4636
4637         switch (spddplx) {
4638         case SPEED_10 + DUPLEX_HALF:
4639                 adapter->hw.forced_speed_duplex = e1000_10_half;
4640                 break;
4641         case SPEED_10 + DUPLEX_FULL:
4642                 adapter->hw.forced_speed_duplex = e1000_10_full;
4643                 break;
4644         case SPEED_100 + DUPLEX_HALF:
4645                 adapter->hw.forced_speed_duplex = e1000_100_half;
4646                 break;
4647         case SPEED_100 + DUPLEX_FULL:
4648                 adapter->hw.forced_speed_duplex = e1000_100_full;
4649                 break;
4650         case SPEED_1000 + DUPLEX_FULL:
4651                 adapter->hw.autoneg = 1;
4652                 adapter->hw.autoneg_advertised = ADVERTISE_1000_FULL;
4653                 break;
4654         case SPEED_1000 + DUPLEX_HALF: /* not supported */
4655         default:
4656                 DPRINTK(PROBE, ERR, "Unsupported Speed/Duplex configuration\n");
4657                 return -EINVAL;
4658         }
4659         return 0;
4660 }
4661
4662 #ifdef CONFIG_PM
4663 /* Save/restore 16 or 64 dwords of PCI config space depending on which
4664  * bus we're on (PCI(X) vs. PCI-E)
4665  */
4666 #define PCIE_CONFIG_SPACE_LEN 256
4667 #define PCI_CONFIG_SPACE_LEN 64
4668 static int
4669 e1000_pci_save_state(struct e1000_adapter *adapter)
4670 {
4671         struct pci_dev *dev = adapter->pdev;
4672         int size;
4673         int i;
4674
4675         if (adapter->hw.mac_type >= e1000_82571)
4676                 size = PCIE_CONFIG_SPACE_LEN;
4677         else
4678                 size = PCI_CONFIG_SPACE_LEN;
4679
4680         WARN_ON(adapter->config_space != NULL);
4681
4682         adapter->config_space = kmalloc(size, GFP_KERNEL);
4683         if (!adapter->config_space) {
4684                 DPRINTK(PROBE, ERR, "unable to allocate %d bytes\n", size);
4685                 return -ENOMEM;
4686         }
4687         for (i = 0; i < (size / 4); i++)
4688                 pci_read_config_dword(dev, i * 4, &adapter->config_space[i]);
4689         return 0;
4690 }
4691
4692 static void
4693 e1000_pci_restore_state(struct e1000_adapter *adapter)
4694 {
4695         struct pci_dev *dev = adapter->pdev;
4696         int size;
4697         int i;
4698
4699         if (adapter->config_space == NULL)
4700                 return;
4701
4702         if (adapter->hw.mac_type >= e1000_82571)
4703                 size = PCIE_CONFIG_SPACE_LEN;
4704         else
4705                 size = PCI_CONFIG_SPACE_LEN;
4706         for (i = 0; i < (size / 4); i++)
4707                 pci_write_config_dword(dev, i * 4, adapter->config_space[i]);
4708         kfree(adapter->config_space);
4709         adapter->config_space = NULL;
4710         return;
4711 }
4712 #endif /* CONFIG_PM */
4713
4714 static int
4715 e1000_suspend(struct pci_dev *pdev, pm_message_t state)
4716 {
4717         struct net_device *netdev = pci_get_drvdata(pdev);
4718         struct e1000_adapter *adapter = netdev_priv(netdev);
4719         uint32_t ctrl, ctrl_ext, rctl, manc, status;
4720         uint32_t wufc = adapter->wol;
4721 #ifdef CONFIG_PM
4722         int retval = 0;
4723 #endif
4724
4725         netif_device_detach(netdev);
4726
4727         if (netif_running(netdev)) {
4728                 WARN_ON(test_bit(__E1000_RESETTING, &adapter->flags));
4729                 e1000_down(adapter);
4730         }
4731
4732 #ifdef CONFIG_PM
4733         /* Implement our own version of pci_save_state(pdev) because pci-
4734          * express adapters have 256-byte config spaces. */
4735         retval = e1000_pci_save_state(adapter);
4736         if (retval)
4737                 return retval;
4738 #endif
4739
4740         status = E1000_READ_REG(&adapter->hw, STATUS);
4741         if (status & E1000_STATUS_LU)
4742                 wufc &= ~E1000_WUFC_LNKC;
4743
4744         if (wufc) {
4745                 e1000_setup_rctl(adapter);
4746                 e1000_set_multi(netdev);
4747
4748                 /* turn on all-multi mode if wake on multicast is enabled */
4749                 if (wufc & E1000_WUFC_MC) {
4750                         rctl = E1000_READ_REG(&adapter->hw, RCTL);
4751                         rctl |= E1000_RCTL_MPE;
4752                         E1000_WRITE_REG(&adapter->hw, RCTL, rctl);
4753                 }
4754
4755                 if (adapter->hw.mac_type >= e1000_82540) {
4756                         ctrl = E1000_READ_REG(&adapter->hw, CTRL);
4757                         /* advertise wake from D3Cold */
4758                         #define E1000_CTRL_ADVD3WUC 0x00100000
4759                         /* phy power management enable */
4760                         #define E1000_CTRL_EN_PHY_PWR_MGMT 0x00200000
4761                         ctrl |= E1000_CTRL_ADVD3WUC |
4762                                 E1000_CTRL_EN_PHY_PWR_MGMT;
4763                         E1000_WRITE_REG(&adapter->hw, CTRL, ctrl);
4764                 }
4765
4766                 if (adapter->hw.media_type == e1000_media_type_fiber ||
4767                    adapter->hw.media_type == e1000_media_type_internal_serdes) {
4768                         /* keep the laser running in D3 */
4769                         ctrl_ext = E1000_READ_REG(&adapter->hw, CTRL_EXT);
4770                         ctrl_ext |= E1000_CTRL_EXT_SDP7_DATA;
4771                         E1000_WRITE_REG(&adapter->hw, CTRL_EXT, ctrl_ext);
4772                 }
4773
4774                 /* Allow time for pending master requests to run */
4775                 e1000_disable_pciex_master(&adapter->hw);
4776
4777                 E1000_WRITE_REG(&adapter->hw, WUC, E1000_WUC_PME_EN);
4778                 E1000_WRITE_REG(&adapter->hw, WUFC, wufc);
4779                 pci_enable_wake(pdev, PCI_D3hot, 1);
4780                 pci_enable_wake(pdev, PCI_D3cold, 1);
4781         } else {
4782                 E1000_WRITE_REG(&adapter->hw, WUC, 0);
4783                 E1000_WRITE_REG(&adapter->hw, WUFC, 0);
4784                 pci_enable_wake(pdev, PCI_D3hot, 0);
4785                 pci_enable_wake(pdev, PCI_D3cold, 0);
4786         }
4787
4788         if (adapter->hw.mac_type >= e1000_82540 &&
4789             adapter->hw.mac_type < e1000_82571 &&
4790             adapter->hw.media_type == e1000_media_type_copper) {
4791                 manc = E1000_READ_REG(&adapter->hw, MANC);
4792                 if (manc & E1000_MANC_SMBUS_EN) {
4793                         manc |= E1000_MANC_ARP_EN;
4794                         E1000_WRITE_REG(&adapter->hw, MANC, manc);
4795                         pci_enable_wake(pdev, PCI_D3hot, 1);
4796                         pci_enable_wake(pdev, PCI_D3cold, 1);
4797                 }
4798         }
4799
4800         if (adapter->hw.phy_type == e1000_phy_igp_3)
4801                 e1000_phy_powerdown_workaround(&adapter->hw);
4802
4803         if (netif_running(netdev))
4804                 e1000_free_irq(adapter);
4805
4806         /* Release control of h/w to f/w.  If f/w is AMT enabled, this
4807          * would have already happened in close and is redundant. */
4808         e1000_release_hw_control(adapter);
4809
4810         pci_disable_device(pdev);
4811
4812         pci_set_power_state(pdev, pci_choose_state(pdev, state));
4813
4814         return 0;
4815 }
4816
4817 #ifdef CONFIG_PM
4818 static int
4819 e1000_resume(struct pci_dev *pdev)
4820 {
4821         struct net_device *netdev = pci_get_drvdata(pdev);
4822         struct e1000_adapter *adapter = netdev_priv(netdev);
4823         uint32_t manc, err;
4824
4825         pci_set_power_state(pdev, PCI_D0);
4826         e1000_pci_restore_state(adapter);
4827         if ((err = pci_enable_device(pdev))) {
4828                 printk(KERN_ERR "e1000: Cannot enable PCI device from suspend\n");
4829                 return err;
4830         }
4831         pci_set_master(pdev);
4832
4833         pci_enable_wake(pdev, PCI_D3hot, 0);
4834         pci_enable_wake(pdev, PCI_D3cold, 0);
4835
4836         if (netif_running(netdev) && (err = e1000_request_irq(adapter)))
4837                 return err;
4838
4839         e1000_power_up_phy(adapter);
4840         e1000_reset(adapter);
4841         E1000_WRITE_REG(&adapter->hw, WUS, ~0);
4842
4843         if (netif_running(netdev))
4844                 e1000_up(adapter);
4845
4846         netif_device_attach(netdev);
4847
4848         if (adapter->hw.mac_type >= e1000_82540 &&
4849             adapter->hw.mac_type < e1000_82571 &&
4850             adapter->hw.media_type == e1000_media_type_copper) {
4851                 manc = E1000_READ_REG(&adapter->hw, MANC);
4852                 manc &= ~(E1000_MANC_ARP_EN);
4853                 E1000_WRITE_REG(&adapter->hw, MANC, manc);
4854         }
4855
4856         /* If the controller is 82573 and f/w is AMT, do not set
4857          * DRV_LOAD until the interface is up.  For all other cases,
4858          * let the f/w know that the h/w is now under the control
4859          * of the driver. */
4860         if (adapter->hw.mac_type != e1000_82573 ||
4861             !e1000_check_mng_mode(&adapter->hw))
4862                 e1000_get_hw_control(adapter);
4863
4864         return 0;
4865 }
4866 #endif
4867
4868 static void e1000_shutdown(struct pci_dev *pdev)
4869 {
4870         e1000_suspend(pdev, PMSG_SUSPEND);
4871 }
4872
4873 #ifdef CONFIG_NET_POLL_CONTROLLER
4874 /*
4875  * Polling 'interrupt' - used by things like netconsole to send skbs
4876  * without having to re-enable interrupts. It's not called while
4877  * the interrupt routine is executing.
4878  */
4879 static void
4880 e1000_netpoll(struct net_device *netdev)
4881 {
4882         struct e1000_adapter *adapter = netdev_priv(netdev);
4883
4884         disable_irq(adapter->pdev->irq);
4885         e1000_intr(adapter->pdev->irq, netdev);
4886         e1000_clean_tx_irq(adapter, adapter->tx_ring);
4887 #ifndef CONFIG_E1000_NAPI
4888         adapter->clean_rx(adapter, adapter->rx_ring);
4889 #endif
4890         enable_irq(adapter->pdev->irq);
4891 }
4892 #endif
4893
4894 /**
4895  * e1000_io_error_detected - called when PCI error is detected
4896  * @pdev: Pointer to PCI device
4897  * @state: The current pci conneection state
4898  *
4899  * This function is called after a PCI bus error affecting
4900  * this device has been detected.
4901  */
4902 static pci_ers_result_t e1000_io_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
4903 {
4904         struct net_device *netdev = pci_get_drvdata(pdev);
4905         struct e1000_adapter *adapter = netdev->priv;
4906
4907         netif_device_detach(netdev);
4908
4909         if (netif_running(netdev))
4910                 e1000_down(adapter);
4911         pci_disable_device(pdev);
4912
4913         /* Request a slot slot reset. */
4914         return PCI_ERS_RESULT_NEED_RESET;
4915 }
4916
4917 /**
4918  * e1000_io_slot_reset - called after the pci bus has been reset.
4919  * @pdev: Pointer to PCI device
4920  *
4921  * Restart the card from scratch, as if from a cold-boot. Implementation
4922  * resembles the first-half of the e1000_resume routine.
4923  */
4924 static pci_ers_result_t e1000_io_slot_reset(struct pci_dev *pdev)
4925 {
4926         struct net_device *netdev = pci_get_drvdata(pdev);
4927         struct e1000_adapter *adapter = netdev->priv;
4928
4929         if (pci_enable_device(pdev)) {
4930                 printk(KERN_ERR "e1000: Cannot re-enable PCI device after reset.\n");
4931                 return PCI_ERS_RESULT_DISCONNECT;
4932         }
4933         pci_set_master(pdev);
4934
4935         pci_enable_wake(pdev, PCI_D3hot, 0);
4936         pci_enable_wake(pdev, PCI_D3cold, 0);
4937
4938         e1000_reset(adapter);
4939         E1000_WRITE_REG(&adapter->hw, WUS, ~0);
4940
4941         return PCI_ERS_RESULT_RECOVERED;
4942 }
4943
4944 /**
4945  * e1000_io_resume - called when traffic can start flowing again.
4946  * @pdev: Pointer to PCI device
4947  *
4948  * This callback is called when the error recovery driver tells us that
4949  * its OK to resume normal operation. Implementation resembles the
4950  * second-half of the e1000_resume routine.
4951  */
4952 static void e1000_io_resume(struct pci_dev *pdev)
4953 {
4954         struct net_device *netdev = pci_get_drvdata(pdev);
4955         struct e1000_adapter *adapter = netdev->priv;
4956         uint32_t manc, swsm;
4957
4958         if (netif_running(netdev)) {
4959                 if (e1000_up(adapter)) {
4960                         printk("e1000: can't bring device back up after reset\n");
4961                         return;
4962                 }
4963         }
4964
4965         netif_device_attach(netdev);
4966
4967         if (adapter->hw.mac_type >= e1000_82540 &&
4968             adapter->hw.mac_type < e1000_82571 &&
4969             adapter->hw.media_type == e1000_media_type_copper) {
4970                 manc = E1000_READ_REG(&adapter->hw, MANC);
4971                 manc &= ~(E1000_MANC_ARP_EN);
4972                 E1000_WRITE_REG(&adapter->hw, MANC, manc);
4973         }
4974
4975         switch (adapter->hw.mac_type) {
4976         case e1000_82573:
4977                 swsm = E1000_READ_REG(&adapter->hw, SWSM);
4978                 E1000_WRITE_REG(&adapter->hw, SWSM,
4979                                 swsm | E1000_SWSM_DRV_LOAD);
4980                 break;
4981         default:
4982                 break;
4983         }
4984
4985         if (netif_running(netdev))
4986                 mod_timer(&adapter->watchdog_timer, jiffies);
4987 }
4988
4989 /* e1000_main.c */