]> git.karo-electronics.de Git - linux-beck.git/blob - drivers/net/e1000/e1000_main.c
d7df00c2dbd6ea0f97f3819878b1d815c02d6a42
[linux-beck.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 #include <net/ip6_checksum.h>
31
32 char e1000_driver_name[] = "e1000";
33 static char e1000_driver_string[] = "Intel(R) PRO/1000 Network Driver";
34 #define DRV_VERSION "7.3.21-k3-NAPI"
35 const char e1000_driver_version[] = DRV_VERSION;
36 static const char e1000_copyright[] = "Copyright (c) 1999-2006 Intel Corporation.";
37
38 /* e1000_pci_tbl - PCI Device ID Table
39  *
40  * Last entry must be all 0s
41  *
42  * Macro expands to...
43  *   {PCI_DEVICE(PCI_VENDOR_ID_INTEL, device_id)}
44  */
45 static struct pci_device_id e1000_pci_tbl[] = {
46         INTEL_E1000_ETHERNET_DEVICE(0x1000),
47         INTEL_E1000_ETHERNET_DEVICE(0x1001),
48         INTEL_E1000_ETHERNET_DEVICE(0x1004),
49         INTEL_E1000_ETHERNET_DEVICE(0x1008),
50         INTEL_E1000_ETHERNET_DEVICE(0x1009),
51         INTEL_E1000_ETHERNET_DEVICE(0x100C),
52         INTEL_E1000_ETHERNET_DEVICE(0x100D),
53         INTEL_E1000_ETHERNET_DEVICE(0x100E),
54         INTEL_E1000_ETHERNET_DEVICE(0x100F),
55         INTEL_E1000_ETHERNET_DEVICE(0x1010),
56         INTEL_E1000_ETHERNET_DEVICE(0x1011),
57         INTEL_E1000_ETHERNET_DEVICE(0x1012),
58         INTEL_E1000_ETHERNET_DEVICE(0x1013),
59         INTEL_E1000_ETHERNET_DEVICE(0x1014),
60         INTEL_E1000_ETHERNET_DEVICE(0x1015),
61         INTEL_E1000_ETHERNET_DEVICE(0x1016),
62         INTEL_E1000_ETHERNET_DEVICE(0x1017),
63         INTEL_E1000_ETHERNET_DEVICE(0x1018),
64         INTEL_E1000_ETHERNET_DEVICE(0x1019),
65         INTEL_E1000_ETHERNET_DEVICE(0x101A),
66         INTEL_E1000_ETHERNET_DEVICE(0x101D),
67         INTEL_E1000_ETHERNET_DEVICE(0x101E),
68         INTEL_E1000_ETHERNET_DEVICE(0x1026),
69         INTEL_E1000_ETHERNET_DEVICE(0x1027),
70         INTEL_E1000_ETHERNET_DEVICE(0x1028),
71         INTEL_E1000_ETHERNET_DEVICE(0x1075),
72         INTEL_E1000_ETHERNET_DEVICE(0x1076),
73         INTEL_E1000_ETHERNET_DEVICE(0x1077),
74         INTEL_E1000_ETHERNET_DEVICE(0x1078),
75         INTEL_E1000_ETHERNET_DEVICE(0x1079),
76         INTEL_E1000_ETHERNET_DEVICE(0x107A),
77         INTEL_E1000_ETHERNET_DEVICE(0x107B),
78         INTEL_E1000_ETHERNET_DEVICE(0x107C),
79         INTEL_E1000_ETHERNET_DEVICE(0x108A),
80         INTEL_E1000_ETHERNET_DEVICE(0x1099),
81         INTEL_E1000_ETHERNET_DEVICE(0x10B5),
82         /* required last entry */
83         {0,}
84 };
85
86 MODULE_DEVICE_TABLE(pci, e1000_pci_tbl);
87
88 int e1000_up(struct e1000_adapter *adapter);
89 void e1000_down(struct e1000_adapter *adapter);
90 void e1000_reinit_locked(struct e1000_adapter *adapter);
91 void e1000_reset(struct e1000_adapter *adapter);
92 int e1000_set_spd_dplx(struct e1000_adapter *adapter, u16 spddplx);
93 int e1000_setup_all_tx_resources(struct e1000_adapter *adapter);
94 int e1000_setup_all_rx_resources(struct e1000_adapter *adapter);
95 void e1000_free_all_tx_resources(struct e1000_adapter *adapter);
96 void e1000_free_all_rx_resources(struct e1000_adapter *adapter);
97 static int e1000_setup_tx_resources(struct e1000_adapter *adapter,
98                              struct e1000_tx_ring *txdr);
99 static int e1000_setup_rx_resources(struct e1000_adapter *adapter,
100                              struct e1000_rx_ring *rxdr);
101 static void e1000_free_tx_resources(struct e1000_adapter *adapter,
102                              struct e1000_tx_ring *tx_ring);
103 static void e1000_free_rx_resources(struct e1000_adapter *adapter,
104                              struct e1000_rx_ring *rx_ring);
105 void e1000_update_stats(struct e1000_adapter *adapter);
106
107 static int e1000_init_module(void);
108 static void e1000_exit_module(void);
109 static int e1000_probe(struct pci_dev *pdev, const struct pci_device_id *ent);
110 static void __devexit e1000_remove(struct pci_dev *pdev);
111 static int e1000_alloc_queues(struct e1000_adapter *adapter);
112 static int e1000_sw_init(struct e1000_adapter *adapter);
113 static int e1000_open(struct net_device *netdev);
114 static int e1000_close(struct net_device *netdev);
115 static void e1000_configure_tx(struct e1000_adapter *adapter);
116 static void e1000_configure_rx(struct e1000_adapter *adapter);
117 static void e1000_setup_rctl(struct e1000_adapter *adapter);
118 static void e1000_clean_all_tx_rings(struct e1000_adapter *adapter);
119 static void e1000_clean_all_rx_rings(struct e1000_adapter *adapter);
120 static void e1000_clean_tx_ring(struct e1000_adapter *adapter,
121                                 struct e1000_tx_ring *tx_ring);
122 static void e1000_clean_rx_ring(struct e1000_adapter *adapter,
123                                 struct e1000_rx_ring *rx_ring);
124 static void e1000_set_rx_mode(struct net_device *netdev);
125 static void e1000_update_phy_info(unsigned long data);
126 static void e1000_watchdog(unsigned long data);
127 static void e1000_82547_tx_fifo_stall(unsigned long data);
128 static int e1000_xmit_frame(struct sk_buff *skb, struct net_device *netdev);
129 static struct net_device_stats * e1000_get_stats(struct net_device *netdev);
130 static int e1000_change_mtu(struct net_device *netdev, int new_mtu);
131 static int e1000_set_mac(struct net_device *netdev, void *p);
132 static irqreturn_t e1000_intr(int irq, void *data);
133 static irqreturn_t e1000_intr_msi(int irq, void *data);
134 static bool e1000_clean_tx_irq(struct e1000_adapter *adapter,
135                                struct e1000_tx_ring *tx_ring);
136 static int e1000_clean(struct napi_struct *napi, int budget);
137 static bool e1000_clean_rx_irq(struct e1000_adapter *adapter,
138                                struct e1000_rx_ring *rx_ring,
139                                int *work_done, int work_to_do);
140 static bool e1000_clean_jumbo_rx_irq(struct e1000_adapter *adapter,
141                                      struct e1000_rx_ring *rx_ring,
142                                      int *work_done, int work_to_do);
143 static void e1000_alloc_rx_buffers(struct e1000_adapter *adapter,
144                                    struct e1000_rx_ring *rx_ring,
145                                    int cleaned_count);
146 static void e1000_alloc_jumbo_rx_buffers(struct e1000_adapter *adapter,
147                                          struct e1000_rx_ring *rx_ring,
148                                          int cleaned_count);
149 static int e1000_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd);
150 static int e1000_mii_ioctl(struct net_device *netdev, struct ifreq *ifr,
151                            int cmd);
152 static void e1000_enter_82542_rst(struct e1000_adapter *adapter);
153 static void e1000_leave_82542_rst(struct e1000_adapter *adapter);
154 static void e1000_tx_timeout(struct net_device *dev);
155 static void e1000_reset_task(struct work_struct *work);
156 static void e1000_smartspeed(struct e1000_adapter *adapter);
157 static int e1000_82547_fifo_workaround(struct e1000_adapter *adapter,
158                                        struct sk_buff *skb);
159
160 static void e1000_vlan_rx_register(struct net_device *netdev, struct vlan_group *grp);
161 static void e1000_vlan_rx_add_vid(struct net_device *netdev, u16 vid);
162 static void e1000_vlan_rx_kill_vid(struct net_device *netdev, u16 vid);
163 static void e1000_restore_vlan(struct e1000_adapter *adapter);
164
165 #ifdef CONFIG_PM
166 static int e1000_suspend(struct pci_dev *pdev, pm_message_t state);
167 static int e1000_resume(struct pci_dev *pdev);
168 #endif
169 static void e1000_shutdown(struct pci_dev *pdev);
170
171 #ifdef CONFIG_NET_POLL_CONTROLLER
172 /* for netdump / net console */
173 static void e1000_netpoll (struct net_device *netdev);
174 #endif
175
176 #define COPYBREAK_DEFAULT 256
177 static unsigned int copybreak __read_mostly = COPYBREAK_DEFAULT;
178 module_param(copybreak, uint, 0644);
179 MODULE_PARM_DESC(copybreak,
180         "Maximum size of packet that is copied to a new buffer on receive");
181
182 static pci_ers_result_t e1000_io_error_detected(struct pci_dev *pdev,
183                      pci_channel_state_t state);
184 static pci_ers_result_t e1000_io_slot_reset(struct pci_dev *pdev);
185 static void e1000_io_resume(struct pci_dev *pdev);
186
187 static struct pci_error_handlers e1000_err_handler = {
188         .error_detected = e1000_io_error_detected,
189         .slot_reset = e1000_io_slot_reset,
190         .resume = e1000_io_resume,
191 };
192
193 static struct pci_driver e1000_driver = {
194         .name     = e1000_driver_name,
195         .id_table = e1000_pci_tbl,
196         .probe    = e1000_probe,
197         .remove   = __devexit_p(e1000_remove),
198 #ifdef CONFIG_PM
199         /* Power Managment Hooks */
200         .suspend  = e1000_suspend,
201         .resume   = e1000_resume,
202 #endif
203         .shutdown = e1000_shutdown,
204         .err_handler = &e1000_err_handler
205 };
206
207 MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
208 MODULE_DESCRIPTION("Intel(R) PRO/1000 Network Driver");
209 MODULE_LICENSE("GPL");
210 MODULE_VERSION(DRV_VERSION);
211
212 static int debug = NETIF_MSG_DRV | NETIF_MSG_PROBE;
213 module_param(debug, int, 0);
214 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
215
216 /**
217  * e1000_init_module - Driver Registration Routine
218  *
219  * e1000_init_module is the first routine called when the driver is
220  * loaded. All it does is register with the PCI subsystem.
221  **/
222
223 static int __init e1000_init_module(void)
224 {
225         int ret;
226         printk(KERN_INFO "%s - version %s\n",
227                e1000_driver_string, e1000_driver_version);
228
229         printk(KERN_INFO "%s\n", e1000_copyright);
230
231         ret = pci_register_driver(&e1000_driver);
232         if (copybreak != COPYBREAK_DEFAULT) {
233                 if (copybreak == 0)
234                         printk(KERN_INFO "e1000: copybreak disabled\n");
235                 else
236                         printk(KERN_INFO "e1000: copybreak enabled for "
237                                "packets <= %u bytes\n", copybreak);
238         }
239         return ret;
240 }
241
242 module_init(e1000_init_module);
243
244 /**
245  * e1000_exit_module - Driver Exit Cleanup Routine
246  *
247  * e1000_exit_module is called just before the driver is removed
248  * from memory.
249  **/
250
251 static void __exit e1000_exit_module(void)
252 {
253         pci_unregister_driver(&e1000_driver);
254 }
255
256 module_exit(e1000_exit_module);
257
258 static int e1000_request_irq(struct e1000_adapter *adapter)
259 {
260         struct e1000_hw *hw = &adapter->hw;
261         struct net_device *netdev = adapter->netdev;
262         irq_handler_t handler = e1000_intr;
263         int irq_flags = IRQF_SHARED;
264         int err;
265
266         if (hw->mac_type >= e1000_82571) {
267                 adapter->have_msi = !pci_enable_msi(adapter->pdev);
268                 if (adapter->have_msi) {
269                         handler = e1000_intr_msi;
270                         irq_flags = 0;
271                 }
272         }
273
274         err = request_irq(adapter->pdev->irq, handler, irq_flags, netdev->name,
275                           netdev);
276         if (err) {
277                 if (adapter->have_msi)
278                         pci_disable_msi(adapter->pdev);
279                 DPRINTK(PROBE, ERR,
280                         "Unable to allocate interrupt Error: %d\n", err);
281         }
282
283         return err;
284 }
285
286 static void e1000_free_irq(struct e1000_adapter *adapter)
287 {
288         struct net_device *netdev = adapter->netdev;
289
290         free_irq(adapter->pdev->irq, netdev);
291
292         if (adapter->have_msi)
293                 pci_disable_msi(adapter->pdev);
294 }
295
296 /**
297  * e1000_irq_disable - Mask off interrupt generation on the NIC
298  * @adapter: board private structure
299  **/
300
301 static void e1000_irq_disable(struct e1000_adapter *adapter)
302 {
303         struct e1000_hw *hw = &adapter->hw;
304
305         ew32(IMC, ~0);
306         E1000_WRITE_FLUSH();
307         synchronize_irq(adapter->pdev->irq);
308 }
309
310 /**
311  * e1000_irq_enable - Enable default interrupt generation settings
312  * @adapter: board private structure
313  **/
314
315 static void e1000_irq_enable(struct e1000_adapter *adapter)
316 {
317         struct e1000_hw *hw = &adapter->hw;
318
319         ew32(IMS, IMS_ENABLE_MASK);
320         E1000_WRITE_FLUSH();
321 }
322
323 static void e1000_update_mng_vlan(struct e1000_adapter *adapter)
324 {
325         struct e1000_hw *hw = &adapter->hw;
326         struct net_device *netdev = adapter->netdev;
327         u16 vid = hw->mng_cookie.vlan_id;
328         u16 old_vid = adapter->mng_vlan_id;
329         if (adapter->vlgrp) {
330                 if (!vlan_group_get_device(adapter->vlgrp, vid)) {
331                         if (hw->mng_cookie.status &
332                                 E1000_MNG_DHCP_COOKIE_STATUS_VLAN_SUPPORT) {
333                                 e1000_vlan_rx_add_vid(netdev, vid);
334                                 adapter->mng_vlan_id = vid;
335                         } else
336                                 adapter->mng_vlan_id = E1000_MNG_VLAN_NONE;
337
338                         if ((old_vid != (u16)E1000_MNG_VLAN_NONE) &&
339                                         (vid != old_vid) &&
340                             !vlan_group_get_device(adapter->vlgrp, old_vid))
341                                 e1000_vlan_rx_kill_vid(netdev, old_vid);
342                 } else
343                         adapter->mng_vlan_id = vid;
344         }
345 }
346
347 /**
348  * e1000_release_hw_control - release control of the h/w to f/w
349  * @adapter: address of board private structure
350  *
351  * e1000_release_hw_control resets {CTRL_EXT|FWSM}:DRV_LOAD bit.
352  * For ASF and Pass Through versions of f/w this means that the
353  * driver is no longer loaded. For AMT version (only with 82573) i
354  * of the f/w this means that the network i/f is closed.
355  *
356  **/
357
358 static void e1000_release_hw_control(struct e1000_adapter *adapter)
359 {
360         u32 ctrl_ext;
361         u32 swsm;
362         struct e1000_hw *hw = &adapter->hw;
363
364         /* Let firmware taken over control of h/w */
365         switch (hw->mac_type) {
366         case e1000_82573:
367                 swsm = er32(SWSM);
368                 ew32(SWSM, swsm & ~E1000_SWSM_DRV_LOAD);
369                 break;
370         case e1000_82571:
371         case e1000_82572:
372         case e1000_80003es2lan:
373         case e1000_ich8lan:
374                 ctrl_ext = er32(CTRL_EXT);
375                 ew32(CTRL_EXT, ctrl_ext & ~E1000_CTRL_EXT_DRV_LOAD);
376                 break;
377         default:
378                 break;
379         }
380 }
381
382 /**
383  * e1000_get_hw_control - get control of the h/w from f/w
384  * @adapter: address of board private structure
385  *
386  * e1000_get_hw_control sets {CTRL_EXT|FWSM}:DRV_LOAD bit.
387  * For ASF and Pass Through versions of f/w this means that
388  * the driver is loaded. For AMT version (only with 82573)
389  * of the f/w this means that the network i/f is open.
390  *
391  **/
392
393 static void e1000_get_hw_control(struct e1000_adapter *adapter)
394 {
395         u32 ctrl_ext;
396         u32 swsm;
397         struct e1000_hw *hw = &adapter->hw;
398
399         /* Let firmware know the driver has taken over */
400         switch (hw->mac_type) {
401         case e1000_82573:
402                 swsm = er32(SWSM);
403                 ew32(SWSM, swsm | E1000_SWSM_DRV_LOAD);
404                 break;
405         case e1000_82571:
406         case e1000_82572:
407         case e1000_80003es2lan:
408         case e1000_ich8lan:
409                 ctrl_ext = er32(CTRL_EXT);
410                 ew32(CTRL_EXT, ctrl_ext | E1000_CTRL_EXT_DRV_LOAD);
411                 break;
412         default:
413                 break;
414         }
415 }
416
417 static void e1000_init_manageability(struct e1000_adapter *adapter)
418 {
419         struct e1000_hw *hw = &adapter->hw;
420
421         if (adapter->en_mng_pt) {
422                 u32 manc = er32(MANC);
423
424                 /* disable hardware interception of ARP */
425                 manc &= ~(E1000_MANC_ARP_EN);
426
427                 /* enable receiving management packets to the host */
428                 /* this will probably generate destination unreachable messages
429                  * from the host OS, but the packets will be handled on SMBUS */
430                 if (hw->has_manc2h) {
431                         u32 manc2h = er32(MANC2H);
432
433                         manc |= E1000_MANC_EN_MNG2HOST;
434 #define E1000_MNG2HOST_PORT_623 (1 << 5)
435 #define E1000_MNG2HOST_PORT_664 (1 << 6)
436                         manc2h |= E1000_MNG2HOST_PORT_623;
437                         manc2h |= E1000_MNG2HOST_PORT_664;
438                         ew32(MANC2H, manc2h);
439                 }
440
441                 ew32(MANC, manc);
442         }
443 }
444
445 static void e1000_release_manageability(struct e1000_adapter *adapter)
446 {
447         struct e1000_hw *hw = &adapter->hw;
448
449         if (adapter->en_mng_pt) {
450                 u32 manc = er32(MANC);
451
452                 /* re-enable hardware interception of ARP */
453                 manc |= E1000_MANC_ARP_EN;
454
455                 if (hw->has_manc2h)
456                         manc &= ~E1000_MANC_EN_MNG2HOST;
457
458                 /* don't explicitly have to mess with MANC2H since
459                  * MANC has an enable disable that gates MANC2H */
460
461                 ew32(MANC, manc);
462         }
463 }
464
465 /**
466  * e1000_configure - configure the hardware for RX and TX
467  * @adapter = private board structure
468  **/
469 static void e1000_configure(struct e1000_adapter *adapter)
470 {
471         struct net_device *netdev = adapter->netdev;
472         int i;
473
474         e1000_set_rx_mode(netdev);
475
476         e1000_restore_vlan(adapter);
477         e1000_init_manageability(adapter);
478
479         e1000_configure_tx(adapter);
480         e1000_setup_rctl(adapter);
481         e1000_configure_rx(adapter);
482         /* call E1000_DESC_UNUSED which always leaves
483          * at least 1 descriptor unused to make sure
484          * next_to_use != next_to_clean */
485         for (i = 0; i < adapter->num_rx_queues; i++) {
486                 struct e1000_rx_ring *ring = &adapter->rx_ring[i];
487                 adapter->alloc_rx_buf(adapter, ring,
488                                       E1000_DESC_UNUSED(ring));
489         }
490
491         adapter->tx_queue_len = netdev->tx_queue_len;
492 }
493
494 int e1000_up(struct e1000_adapter *adapter)
495 {
496         struct e1000_hw *hw = &adapter->hw;
497
498         /* hardware has been reset, we need to reload some things */
499         e1000_configure(adapter);
500
501         clear_bit(__E1000_DOWN, &adapter->flags);
502
503         napi_enable(&adapter->napi);
504
505         e1000_irq_enable(adapter);
506
507         netif_wake_queue(adapter->netdev);
508
509         /* fire a link change interrupt to start the watchdog */
510         ew32(ICS, E1000_ICS_LSC);
511         return 0;
512 }
513
514 /**
515  * e1000_power_up_phy - restore link in case the phy was powered down
516  * @adapter: address of board private structure
517  *
518  * The phy may be powered down to save power and turn off link when the
519  * driver is unloaded and wake on lan is not enabled (among others)
520  * *** this routine MUST be followed by a call to e1000_reset ***
521  *
522  **/
523
524 void e1000_power_up_phy(struct e1000_adapter *adapter)
525 {
526         struct e1000_hw *hw = &adapter->hw;
527         u16 mii_reg = 0;
528
529         /* Just clear the power down bit to wake the phy back up */
530         if (hw->media_type == e1000_media_type_copper) {
531                 /* according to the manual, the phy will retain its
532                  * settings across a power-down/up cycle */
533                 e1000_read_phy_reg(hw, PHY_CTRL, &mii_reg);
534                 mii_reg &= ~MII_CR_POWER_DOWN;
535                 e1000_write_phy_reg(hw, PHY_CTRL, mii_reg);
536         }
537 }
538
539 static void e1000_power_down_phy(struct e1000_adapter *adapter)
540 {
541         struct e1000_hw *hw = &adapter->hw;
542
543         /* Power down the PHY so no link is implied when interface is down *
544          * The PHY cannot be powered down if any of the following is true *
545          * (a) WoL is enabled
546          * (b) AMT is active
547          * (c) SoL/IDER session is active */
548         if (!adapter->wol && hw->mac_type >= e1000_82540 &&
549            hw->media_type == e1000_media_type_copper) {
550                 u16 mii_reg = 0;
551
552                 switch (hw->mac_type) {
553                 case e1000_82540:
554                 case e1000_82545:
555                 case e1000_82545_rev_3:
556                 case e1000_82546:
557                 case e1000_82546_rev_3:
558                 case e1000_82541:
559                 case e1000_82541_rev_2:
560                 case e1000_82547:
561                 case e1000_82547_rev_2:
562                         if (er32(MANC) & E1000_MANC_SMBUS_EN)
563                                 goto out;
564                         break;
565                 case e1000_82571:
566                 case e1000_82572:
567                 case e1000_82573:
568                 case e1000_80003es2lan:
569                 case e1000_ich8lan:
570                         if (e1000_check_mng_mode(hw) ||
571                             e1000_check_phy_reset_block(hw))
572                                 goto out;
573                         break;
574                 default:
575                         goto out;
576                 }
577                 e1000_read_phy_reg(hw, PHY_CTRL, &mii_reg);
578                 mii_reg |= MII_CR_POWER_DOWN;
579                 e1000_write_phy_reg(hw, PHY_CTRL, mii_reg);
580                 mdelay(1);
581         }
582 out:
583         return;
584 }
585
586 void e1000_down(struct e1000_adapter *adapter)
587 {
588         struct e1000_hw *hw = &adapter->hw;
589         struct net_device *netdev = adapter->netdev;
590         u32 rctl, tctl;
591
592         /* signal that we're down so the interrupt handler does not
593          * reschedule our watchdog timer */
594         set_bit(__E1000_DOWN, &adapter->flags);
595
596         /* disable receives in the hardware */
597         rctl = er32(RCTL);
598         ew32(RCTL, rctl & ~E1000_RCTL_EN);
599         /* flush and sleep below */
600
601         /* can be netif_tx_disable when NETIF_F_LLTX is removed */
602         netif_stop_queue(netdev);
603
604         /* disable transmits in the hardware */
605         tctl = er32(TCTL);
606         tctl &= ~E1000_TCTL_EN;
607         ew32(TCTL, tctl);
608         /* flush both disables and wait for them to finish */
609         E1000_WRITE_FLUSH();
610         msleep(10);
611
612         napi_disable(&adapter->napi);
613
614         e1000_irq_disable(adapter);
615
616         del_timer_sync(&adapter->tx_fifo_stall_timer);
617         del_timer_sync(&adapter->watchdog_timer);
618         del_timer_sync(&adapter->phy_info_timer);
619
620         netdev->tx_queue_len = adapter->tx_queue_len;
621         adapter->link_speed = 0;
622         adapter->link_duplex = 0;
623         netif_carrier_off(netdev);
624
625         e1000_reset(adapter);
626         e1000_clean_all_tx_rings(adapter);
627         e1000_clean_all_rx_rings(adapter);
628 }
629
630 void e1000_reinit_locked(struct e1000_adapter *adapter)
631 {
632         WARN_ON(in_interrupt());
633         while (test_and_set_bit(__E1000_RESETTING, &adapter->flags))
634                 msleep(1);
635         e1000_down(adapter);
636         e1000_up(adapter);
637         clear_bit(__E1000_RESETTING, &adapter->flags);
638 }
639
640 void e1000_reset(struct e1000_adapter *adapter)
641 {
642         struct e1000_hw *hw = &adapter->hw;
643         u32 pba = 0, tx_space, min_tx_space, min_rx_space;
644         bool legacy_pba_adjust = false;
645         u16 hwm;
646
647         /* Repartition Pba for greater than 9k mtu
648          * To take effect CTRL.RST is required.
649          */
650
651         switch (hw->mac_type) {
652         case e1000_82542_rev2_0:
653         case e1000_82542_rev2_1:
654         case e1000_82543:
655         case e1000_82544:
656         case e1000_82540:
657         case e1000_82541:
658         case e1000_82541_rev_2:
659                 legacy_pba_adjust = true;
660                 pba = E1000_PBA_48K;
661                 break;
662         case e1000_82545:
663         case e1000_82545_rev_3:
664         case e1000_82546:
665         case e1000_82546_rev_3:
666                 pba = E1000_PBA_48K;
667                 break;
668         case e1000_82547:
669         case e1000_82547_rev_2:
670                 legacy_pba_adjust = true;
671                 pba = E1000_PBA_30K;
672                 break;
673         case e1000_82571:
674         case e1000_82572:
675         case e1000_80003es2lan:
676                 pba = E1000_PBA_38K;
677                 break;
678         case e1000_82573:
679                 pba = E1000_PBA_20K;
680                 break;
681         case e1000_ich8lan:
682                 pba = E1000_PBA_8K;
683         case e1000_undefined:
684         case e1000_num_macs:
685                 break;
686         }
687
688         if (legacy_pba_adjust) {
689                 if (hw->max_frame_size > E1000_RXBUFFER_8192)
690                         pba -= 8; /* allocate more FIFO for Tx */
691
692                 if (hw->mac_type == e1000_82547) {
693                         adapter->tx_fifo_head = 0;
694                         adapter->tx_head_addr = pba << E1000_TX_HEAD_ADDR_SHIFT;
695                         adapter->tx_fifo_size =
696                                 (E1000_PBA_40K - pba) << E1000_PBA_BYTES_SHIFT;
697                         atomic_set(&adapter->tx_fifo_stall, 0);
698                 }
699         } else if (hw->max_frame_size >  ETH_FRAME_LEN + ETH_FCS_LEN) {
700                 /* adjust PBA for jumbo frames */
701                 ew32(PBA, pba);
702
703                 /* To maintain wire speed transmits, the Tx FIFO should be
704                  * large enough to accommodate two full transmit packets,
705                  * rounded up to the next 1KB and expressed in KB.  Likewise,
706                  * the Rx FIFO should be large enough to accommodate at least
707                  * one full receive packet and is similarly rounded up and
708                  * expressed in KB. */
709                 pba = er32(PBA);
710                 /* upper 16 bits has Tx packet buffer allocation size in KB */
711                 tx_space = pba >> 16;
712                 /* lower 16 bits has Rx packet buffer allocation size in KB */
713                 pba &= 0xffff;
714                 /*
715                  * the tx fifo also stores 16 bytes of information about the tx
716                  * but don't include ethernet FCS because hardware appends it
717                  */
718                 min_tx_space = (hw->max_frame_size +
719                                 sizeof(struct e1000_tx_desc) -
720                                 ETH_FCS_LEN) * 2;
721                 min_tx_space = ALIGN(min_tx_space, 1024);
722                 min_tx_space >>= 10;
723                 /* software strips receive CRC, so leave room for it */
724                 min_rx_space = hw->max_frame_size;
725                 min_rx_space = ALIGN(min_rx_space, 1024);
726                 min_rx_space >>= 10;
727
728                 /* If current Tx allocation is less than the min Tx FIFO size,
729                  * and the min Tx FIFO size is less than the current Rx FIFO
730                  * allocation, take space away from current Rx allocation */
731                 if (tx_space < min_tx_space &&
732                     ((min_tx_space - tx_space) < pba)) {
733                         pba = pba - (min_tx_space - tx_space);
734
735                         /* PCI/PCIx hardware has PBA alignment constraints */
736                         switch (hw->mac_type) {
737                         case e1000_82545 ... e1000_82546_rev_3:
738                                 pba &= ~(E1000_PBA_8K - 1);
739                                 break;
740                         default:
741                                 break;
742                         }
743
744                         /* if short on rx space, rx wins and must trump tx
745                          * adjustment or use Early Receive if available */
746                         if (pba < min_rx_space) {
747                                 switch (hw->mac_type) {
748                                 case e1000_82573:
749                                         /* ERT enabled in e1000_configure_rx */
750                                         break;
751                                 default:
752                                         pba = min_rx_space;
753                                         break;
754                                 }
755                         }
756                 }
757         }
758
759         ew32(PBA, pba);
760
761         /*
762          * flow control settings:
763          * The high water mark must be low enough to fit one full frame
764          * (or the size used for early receive) above it in the Rx FIFO.
765          * Set it to the lower of:
766          * - 90% of the Rx FIFO size, and
767          * - the full Rx FIFO size minus the early receive size (for parts
768          *   with ERT support assuming ERT set to E1000_ERT_2048), or
769          * - the full Rx FIFO size minus one full frame
770          */
771         hwm = min(((pba << 10) * 9 / 10),
772                   ((pba << 10) - hw->max_frame_size));
773
774         hw->fc_high_water = hwm & 0xFFF8;       /* 8-byte granularity */
775         hw->fc_low_water = hw->fc_high_water - 8;
776         hw->fc_pause_time = E1000_FC_PAUSE_TIME;
777         hw->fc_send_xon = 1;
778         hw->fc = hw->original_fc;
779
780         /* Allow time for pending master requests to run */
781         e1000_reset_hw(hw);
782         if (hw->mac_type >= e1000_82544)
783                 ew32(WUC, 0);
784
785         if (e1000_init_hw(hw))
786                 DPRINTK(PROBE, ERR, "Hardware Error\n");
787         e1000_update_mng_vlan(adapter);
788
789         /* if (adapter->hwflags & HWFLAGS_PHY_PWR_BIT) { */
790         if (hw->mac_type >= e1000_82544 &&
791             hw->mac_type <= e1000_82547_rev_2 &&
792             hw->autoneg == 1 &&
793             hw->autoneg_advertised == ADVERTISE_1000_FULL) {
794                 u32 ctrl = er32(CTRL);
795                 /* clear phy power management bit if we are in gig only mode,
796                  * which if enabled will attempt negotiation to 100Mb, which
797                  * can cause a loss of link at power off or driver unload */
798                 ctrl &= ~E1000_CTRL_SWDPIN3;
799                 ew32(CTRL, ctrl);
800         }
801
802         /* Enable h/w to recognize an 802.1Q VLAN Ethernet packet */
803         ew32(VET, ETHERNET_IEEE_VLAN_TYPE);
804
805         e1000_reset_adaptive(hw);
806         e1000_phy_get_info(hw, &adapter->phy_info);
807
808         if (!adapter->smart_power_down &&
809             (hw->mac_type == e1000_82571 ||
810              hw->mac_type == e1000_82572)) {
811                 u16 phy_data = 0;
812                 /* speed up time to link by disabling smart power down, ignore
813                  * the return value of this function because there is nothing
814                  * different we would do if it failed */
815                 e1000_read_phy_reg(hw, IGP02E1000_PHY_POWER_MGMT,
816                                    &phy_data);
817                 phy_data &= ~IGP02E1000_PM_SPD;
818                 e1000_write_phy_reg(hw, IGP02E1000_PHY_POWER_MGMT,
819                                     phy_data);
820         }
821
822         e1000_release_manageability(adapter);
823 }
824
825 /**
826  *  Dump the eeprom for users having checksum issues
827  **/
828 static void e1000_dump_eeprom(struct e1000_adapter *adapter)
829 {
830         struct net_device *netdev = adapter->netdev;
831         struct ethtool_eeprom eeprom;
832         const struct ethtool_ops *ops = netdev->ethtool_ops;
833         u8 *data;
834         int i;
835         u16 csum_old, csum_new = 0;
836
837         eeprom.len = ops->get_eeprom_len(netdev);
838         eeprom.offset = 0;
839
840         data = kmalloc(eeprom.len, GFP_KERNEL);
841         if (!data) {
842                 printk(KERN_ERR "Unable to allocate memory to dump EEPROM"
843                        " data\n");
844                 return;
845         }
846
847         ops->get_eeprom(netdev, &eeprom, data);
848
849         csum_old = (data[EEPROM_CHECKSUM_REG * 2]) +
850                    (data[EEPROM_CHECKSUM_REG * 2 + 1] << 8);
851         for (i = 0; i < EEPROM_CHECKSUM_REG * 2; i += 2)
852                 csum_new += data[i] + (data[i + 1] << 8);
853         csum_new = EEPROM_SUM - csum_new;
854
855         printk(KERN_ERR "/*********************/\n");
856         printk(KERN_ERR "Current EEPROM Checksum : 0x%04x\n", csum_old);
857         printk(KERN_ERR "Calculated              : 0x%04x\n", csum_new);
858
859         printk(KERN_ERR "Offset    Values\n");
860         printk(KERN_ERR "========  ======\n");
861         print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 16, 1, data, 128, 0);
862
863         printk(KERN_ERR "Include this output when contacting your support "
864                "provider.\n");
865         printk(KERN_ERR "This is not a software error! Something bad "
866                "happened to your hardware or\n");
867         printk(KERN_ERR "EEPROM image. Ignoring this "
868                "problem could result in further problems,\n");
869         printk(KERN_ERR "possibly loss of data, corruption or system hangs!\n");
870         printk(KERN_ERR "The MAC Address will be reset to 00:00:00:00:00:00, "
871                "which is invalid\n");
872         printk(KERN_ERR "and requires you to set the proper MAC "
873                "address manually before continuing\n");
874         printk(KERN_ERR "to enable this network device.\n");
875         printk(KERN_ERR "Please inspect the EEPROM dump and report the issue "
876                "to your hardware vendor\n");
877         printk(KERN_ERR "or Intel Customer Support.\n");
878         printk(KERN_ERR "/*********************/\n");
879
880         kfree(data);
881 }
882
883 /**
884  * e1000_is_need_ioport - determine if an adapter needs ioport resources or not
885  * @pdev: PCI device information struct
886  *
887  * Return true if an adapter needs ioport resources
888  **/
889 static int e1000_is_need_ioport(struct pci_dev *pdev)
890 {
891         switch (pdev->device) {
892         case E1000_DEV_ID_82540EM:
893         case E1000_DEV_ID_82540EM_LOM:
894         case E1000_DEV_ID_82540EP:
895         case E1000_DEV_ID_82540EP_LOM:
896         case E1000_DEV_ID_82540EP_LP:
897         case E1000_DEV_ID_82541EI:
898         case E1000_DEV_ID_82541EI_MOBILE:
899         case E1000_DEV_ID_82541ER:
900         case E1000_DEV_ID_82541ER_LOM:
901         case E1000_DEV_ID_82541GI:
902         case E1000_DEV_ID_82541GI_LF:
903         case E1000_DEV_ID_82541GI_MOBILE:
904         case E1000_DEV_ID_82544EI_COPPER:
905         case E1000_DEV_ID_82544EI_FIBER:
906         case E1000_DEV_ID_82544GC_COPPER:
907         case E1000_DEV_ID_82544GC_LOM:
908         case E1000_DEV_ID_82545EM_COPPER:
909         case E1000_DEV_ID_82545EM_FIBER:
910         case E1000_DEV_ID_82546EB_COPPER:
911         case E1000_DEV_ID_82546EB_FIBER:
912         case E1000_DEV_ID_82546EB_QUAD_COPPER:
913                 return true;
914         default:
915                 return false;
916         }
917 }
918
919 static const struct net_device_ops e1000_netdev_ops = {
920         .ndo_open               = e1000_open,
921         .ndo_stop               = e1000_close,
922         .ndo_start_xmit         = e1000_xmit_frame,
923         .ndo_get_stats          = e1000_get_stats,
924         .ndo_set_rx_mode        = e1000_set_rx_mode,
925         .ndo_set_mac_address    = e1000_set_mac,
926         .ndo_tx_timeout         = e1000_tx_timeout,
927         .ndo_change_mtu         = e1000_change_mtu,
928         .ndo_do_ioctl           = e1000_ioctl,
929         .ndo_validate_addr      = eth_validate_addr,
930
931         .ndo_vlan_rx_register   = e1000_vlan_rx_register,
932         .ndo_vlan_rx_add_vid    = e1000_vlan_rx_add_vid,
933         .ndo_vlan_rx_kill_vid   = e1000_vlan_rx_kill_vid,
934 #ifdef CONFIG_NET_POLL_CONTROLLER
935         .ndo_poll_controller    = e1000_netpoll,
936 #endif
937 };
938
939 /**
940  * e1000_probe - Device Initialization Routine
941  * @pdev: PCI device information struct
942  * @ent: entry in e1000_pci_tbl
943  *
944  * Returns 0 on success, negative on failure
945  *
946  * e1000_probe initializes an adapter identified by a pci_dev structure.
947  * The OS initialization, configuring of the adapter private structure,
948  * and a hardware reset occur.
949  **/
950 static int __devinit e1000_probe(struct pci_dev *pdev,
951                                  const struct pci_device_id *ent)
952 {
953         struct net_device *netdev;
954         struct e1000_adapter *adapter;
955         struct e1000_hw *hw;
956
957         static int cards_found = 0;
958         static int global_quad_port_a = 0; /* global ksp3 port a indication */
959         int i, err, pci_using_dac;
960         u16 eeprom_data = 0;
961         u16 eeprom_apme_mask = E1000_EEPROM_APME;
962         int bars, need_ioport;
963
964         /* do not allocate ioport bars when not needed */
965         need_ioport = e1000_is_need_ioport(pdev);
966         if (need_ioport) {
967                 bars = pci_select_bars(pdev, IORESOURCE_MEM | IORESOURCE_IO);
968                 err = pci_enable_device(pdev);
969         } else {
970                 bars = pci_select_bars(pdev, IORESOURCE_MEM);
971                 err = pci_enable_device_mem(pdev);
972         }
973         if (err)
974                 return err;
975
976         if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) &&
977             !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64))) {
978                 pci_using_dac = 1;
979         } else {
980                 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
981                 if (err) {
982                         err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
983                         if (err) {
984                                 E1000_ERR("No usable DMA configuration, "
985                                           "aborting\n");
986                                 goto err_dma;
987                         }
988                 }
989                 pci_using_dac = 0;
990         }
991
992         err = pci_request_selected_regions(pdev, bars, e1000_driver_name);
993         if (err)
994                 goto err_pci_reg;
995
996         pci_set_master(pdev);
997
998         err = -ENOMEM;
999         netdev = alloc_etherdev(sizeof(struct e1000_adapter));
1000         if (!netdev)
1001                 goto err_alloc_etherdev;
1002
1003         SET_NETDEV_DEV(netdev, &pdev->dev);
1004
1005         pci_set_drvdata(pdev, netdev);
1006         adapter = netdev_priv(netdev);
1007         adapter->netdev = netdev;
1008         adapter->pdev = pdev;
1009         adapter->msg_enable = (1 << debug) - 1;
1010         adapter->bars = bars;
1011         adapter->need_ioport = need_ioport;
1012
1013         hw = &adapter->hw;
1014         hw->back = adapter;
1015
1016         err = -EIO;
1017         hw->hw_addr = pci_ioremap_bar(pdev, BAR_0);
1018         if (!hw->hw_addr)
1019                 goto err_ioremap;
1020
1021         if (adapter->need_ioport) {
1022                 for (i = BAR_1; i <= BAR_5; i++) {
1023                         if (pci_resource_len(pdev, i) == 0)
1024                                 continue;
1025                         if (pci_resource_flags(pdev, i) & IORESOURCE_IO) {
1026                                 hw->io_base = pci_resource_start(pdev, i);
1027                                 break;
1028                         }
1029                 }
1030         }
1031
1032         netdev->netdev_ops = &e1000_netdev_ops;
1033         e1000_set_ethtool_ops(netdev);
1034         netdev->watchdog_timeo = 5 * HZ;
1035         netif_napi_add(netdev, &adapter->napi, e1000_clean, 64);
1036
1037         strncpy(netdev->name, pci_name(pdev), sizeof(netdev->name) - 1);
1038
1039         adapter->bd_number = cards_found;
1040
1041         /* setup the private structure */
1042
1043         err = e1000_sw_init(adapter);
1044         if (err)
1045                 goto err_sw_init;
1046
1047         err = -EIO;
1048         /* Flash BAR mapping must happen after e1000_sw_init
1049          * because it depends on mac_type */
1050         if ((hw->mac_type == e1000_ich8lan) &&
1051            (pci_resource_flags(pdev, 1) & IORESOURCE_MEM)) {
1052                 hw->flash_address = pci_ioremap_bar(pdev, 1);
1053                 if (!hw->flash_address)
1054                         goto err_flashmap;
1055         }
1056
1057         if (e1000_check_phy_reset_block(hw))
1058                 DPRINTK(PROBE, INFO, "PHY reset is blocked due to SOL/IDER session.\n");
1059
1060         if (hw->mac_type >= e1000_82543) {
1061                 netdev->features = NETIF_F_SG |
1062                                    NETIF_F_HW_CSUM |
1063                                    NETIF_F_HW_VLAN_TX |
1064                                    NETIF_F_HW_VLAN_RX |
1065                                    NETIF_F_HW_VLAN_FILTER;
1066                 if (hw->mac_type == e1000_ich8lan)
1067                         netdev->features &= ~NETIF_F_HW_VLAN_FILTER;
1068         }
1069
1070         if ((hw->mac_type >= e1000_82544) &&
1071            (hw->mac_type != e1000_82547))
1072                 netdev->features |= NETIF_F_TSO;
1073
1074         if (hw->mac_type > e1000_82547_rev_2)
1075                 netdev->features |= NETIF_F_TSO6;
1076         if (pci_using_dac)
1077                 netdev->features |= NETIF_F_HIGHDMA;
1078
1079         netdev->vlan_features |= NETIF_F_TSO;
1080         netdev->vlan_features |= NETIF_F_TSO6;
1081         netdev->vlan_features |= NETIF_F_HW_CSUM;
1082         netdev->vlan_features |= NETIF_F_SG;
1083
1084         adapter->en_mng_pt = e1000_enable_mng_pass_thru(hw);
1085
1086         /* initialize eeprom parameters */
1087         if (e1000_init_eeprom_params(hw)) {
1088                 E1000_ERR("EEPROM initialization failed\n");
1089                 goto err_eeprom;
1090         }
1091
1092         /* before reading the EEPROM, reset the controller to
1093          * put the device in a known good starting state */
1094
1095         e1000_reset_hw(hw);
1096
1097         /* make sure the EEPROM is good */
1098         if (e1000_validate_eeprom_checksum(hw) < 0) {
1099                 DPRINTK(PROBE, ERR, "The EEPROM Checksum Is Not Valid\n");
1100                 e1000_dump_eeprom(adapter);
1101                 /*
1102                  * set MAC address to all zeroes to invalidate and temporary
1103                  * disable this device for the user. This blocks regular
1104                  * traffic while still permitting ethtool ioctls from reaching
1105                  * the hardware as well as allowing the user to run the
1106                  * interface after manually setting a hw addr using
1107                  * `ip set address`
1108                  */
1109                 memset(hw->mac_addr, 0, netdev->addr_len);
1110         } else {
1111                 /* copy the MAC address out of the EEPROM */
1112                 if (e1000_read_mac_addr(hw))
1113                         DPRINTK(PROBE, ERR, "EEPROM Read Error\n");
1114         }
1115         /* don't block initalization here due to bad MAC address */
1116         memcpy(netdev->dev_addr, hw->mac_addr, netdev->addr_len);
1117         memcpy(netdev->perm_addr, hw->mac_addr, netdev->addr_len);
1118
1119         if (!is_valid_ether_addr(netdev->perm_addr))
1120                 DPRINTK(PROBE, ERR, "Invalid MAC Address\n");
1121
1122         e1000_get_bus_info(hw);
1123
1124         init_timer(&adapter->tx_fifo_stall_timer);
1125         adapter->tx_fifo_stall_timer.function = &e1000_82547_tx_fifo_stall;
1126         adapter->tx_fifo_stall_timer.data = (unsigned long)adapter;
1127
1128         init_timer(&adapter->watchdog_timer);
1129         adapter->watchdog_timer.function = &e1000_watchdog;
1130         adapter->watchdog_timer.data = (unsigned long) adapter;
1131
1132         init_timer(&adapter->phy_info_timer);
1133         adapter->phy_info_timer.function = &e1000_update_phy_info;
1134         adapter->phy_info_timer.data = (unsigned long)adapter;
1135
1136         INIT_WORK(&adapter->reset_task, e1000_reset_task);
1137
1138         e1000_check_options(adapter);
1139
1140         /* Initial Wake on LAN setting
1141          * If APM wake is enabled in the EEPROM,
1142          * enable the ACPI Magic Packet filter
1143          */
1144
1145         switch (hw->mac_type) {
1146         case e1000_82542_rev2_0:
1147         case e1000_82542_rev2_1:
1148         case e1000_82543:
1149                 break;
1150         case e1000_82544:
1151                 e1000_read_eeprom(hw,
1152                         EEPROM_INIT_CONTROL2_REG, 1, &eeprom_data);
1153                 eeprom_apme_mask = E1000_EEPROM_82544_APM;
1154                 break;
1155         case e1000_ich8lan:
1156                 e1000_read_eeprom(hw,
1157                         EEPROM_INIT_CONTROL1_REG, 1, &eeprom_data);
1158                 eeprom_apme_mask = E1000_EEPROM_ICH8_APME;
1159                 break;
1160         case e1000_82546:
1161         case e1000_82546_rev_3:
1162         case e1000_82571:
1163         case e1000_80003es2lan:
1164                 if (er32(STATUS) & E1000_STATUS_FUNC_1){
1165                         e1000_read_eeprom(hw,
1166                                 EEPROM_INIT_CONTROL3_PORT_B, 1, &eeprom_data);
1167                         break;
1168                 }
1169                 /* Fall Through */
1170         default:
1171                 e1000_read_eeprom(hw,
1172                         EEPROM_INIT_CONTROL3_PORT_A, 1, &eeprom_data);
1173                 break;
1174         }
1175         if (eeprom_data & eeprom_apme_mask)
1176                 adapter->eeprom_wol |= E1000_WUFC_MAG;
1177
1178         /* now that we have the eeprom settings, apply the special cases
1179          * where the eeprom may be wrong or the board simply won't support
1180          * wake on lan on a particular port */
1181         switch (pdev->device) {
1182         case E1000_DEV_ID_82546GB_PCIE:
1183                 adapter->eeprom_wol = 0;
1184                 break;
1185         case E1000_DEV_ID_82546EB_FIBER:
1186         case E1000_DEV_ID_82546GB_FIBER:
1187         case E1000_DEV_ID_82571EB_FIBER:
1188                 /* Wake events only supported on port A for dual fiber
1189                  * regardless of eeprom setting */
1190                 if (er32(STATUS) & E1000_STATUS_FUNC_1)
1191                         adapter->eeprom_wol = 0;
1192                 break;
1193         case E1000_DEV_ID_82546GB_QUAD_COPPER_KSP3:
1194         case E1000_DEV_ID_82571EB_QUAD_COPPER:
1195         case E1000_DEV_ID_82571EB_QUAD_FIBER:
1196         case E1000_DEV_ID_82571EB_QUAD_COPPER_LOWPROFILE:
1197         case E1000_DEV_ID_82571PT_QUAD_COPPER:
1198                 /* if quad port adapter, disable WoL on all but port A */
1199                 if (global_quad_port_a != 0)
1200                         adapter->eeprom_wol = 0;
1201                 else
1202                         adapter->quad_port_a = 1;
1203                 /* Reset for multiple quad port adapters */
1204                 if (++global_quad_port_a == 4)
1205                         global_quad_port_a = 0;
1206                 break;
1207         }
1208
1209         /* initialize the wol settings based on the eeprom settings */
1210         adapter->wol = adapter->eeprom_wol;
1211         device_set_wakeup_enable(&adapter->pdev->dev, adapter->wol);
1212
1213         /* print bus type/speed/width info */
1214         DPRINTK(PROBE, INFO, "(PCI%s:%s:%s) ",
1215                 ((hw->bus_type == e1000_bus_type_pcix) ? "-X" :
1216                  (hw->bus_type == e1000_bus_type_pci_express ? " Express":"")),
1217                 ((hw->bus_speed == e1000_bus_speed_2500) ? "2.5Gb/s" :
1218                  (hw->bus_speed == e1000_bus_speed_133) ? "133MHz" :
1219                  (hw->bus_speed == e1000_bus_speed_120) ? "120MHz" :
1220                  (hw->bus_speed == e1000_bus_speed_100) ? "100MHz" :
1221                  (hw->bus_speed == e1000_bus_speed_66) ? "66MHz" : "33MHz"),
1222                 ((hw->bus_width == e1000_bus_width_64) ? "64-bit" :
1223                  (hw->bus_width == e1000_bus_width_pciex_4) ? "Width x4" :
1224                  (hw->bus_width == e1000_bus_width_pciex_1) ? "Width x1" :
1225                  "32-bit"));
1226
1227         printk("%pM\n", netdev->dev_addr);
1228
1229         if (hw->bus_type == e1000_bus_type_pci_express) {
1230                 DPRINTK(PROBE, WARNING, "This device (id %04x:%04x) will no "
1231                         "longer be supported by this driver in the future.\n",
1232                         pdev->vendor, pdev->device);
1233                 DPRINTK(PROBE, WARNING, "please use the \"e1000e\" "
1234                         "driver instead.\n");
1235         }
1236
1237         /* reset the hardware with the new settings */
1238         e1000_reset(adapter);
1239
1240         /* If the controller is 82573 and f/w is AMT, do not set
1241          * DRV_LOAD until the interface is up.  For all other cases,
1242          * let the f/w know that the h/w is now under the control
1243          * of the driver. */
1244         if (hw->mac_type != e1000_82573 ||
1245             !e1000_check_mng_mode(hw))
1246                 e1000_get_hw_control(adapter);
1247
1248         strcpy(netdev->name, "eth%d");
1249         err = register_netdev(netdev);
1250         if (err)
1251                 goto err_register;
1252
1253         /* carrier off reporting is important to ethtool even BEFORE open */
1254         netif_carrier_off(netdev);
1255
1256         DPRINTK(PROBE, INFO, "Intel(R) PRO/1000 Network Connection\n");
1257
1258         cards_found++;
1259         return 0;
1260
1261 err_register:
1262         e1000_release_hw_control(adapter);
1263 err_eeprom:
1264         if (!e1000_check_phy_reset_block(hw))
1265                 e1000_phy_hw_reset(hw);
1266
1267         if (hw->flash_address)
1268                 iounmap(hw->flash_address);
1269 err_flashmap:
1270         kfree(adapter->tx_ring);
1271         kfree(adapter->rx_ring);
1272 err_sw_init:
1273         iounmap(hw->hw_addr);
1274 err_ioremap:
1275         free_netdev(netdev);
1276 err_alloc_etherdev:
1277         pci_release_selected_regions(pdev, bars);
1278 err_pci_reg:
1279 err_dma:
1280         pci_disable_device(pdev);
1281         return err;
1282 }
1283
1284 /**
1285  * e1000_remove - Device Removal Routine
1286  * @pdev: PCI device information struct
1287  *
1288  * e1000_remove is called by the PCI subsystem to alert the driver
1289  * that it should release a PCI device.  The could be caused by a
1290  * Hot-Plug event, or because the driver is going to be removed from
1291  * memory.
1292  **/
1293
1294 static void __devexit e1000_remove(struct pci_dev *pdev)
1295 {
1296         struct net_device *netdev = pci_get_drvdata(pdev);
1297         struct e1000_adapter *adapter = netdev_priv(netdev);
1298         struct e1000_hw *hw = &adapter->hw;
1299
1300         cancel_work_sync(&adapter->reset_task);
1301
1302         e1000_release_manageability(adapter);
1303
1304         /* Release control of h/w to f/w.  If f/w is AMT enabled, this
1305          * would have already happened in close and is redundant. */
1306         e1000_release_hw_control(adapter);
1307
1308         unregister_netdev(netdev);
1309
1310         if (!e1000_check_phy_reset_block(hw))
1311                 e1000_phy_hw_reset(hw);
1312
1313         kfree(adapter->tx_ring);
1314         kfree(adapter->rx_ring);
1315
1316         iounmap(hw->hw_addr);
1317         if (hw->flash_address)
1318                 iounmap(hw->flash_address);
1319         pci_release_selected_regions(pdev, adapter->bars);
1320
1321         free_netdev(netdev);
1322
1323         pci_disable_device(pdev);
1324 }
1325
1326 /**
1327  * e1000_sw_init - Initialize general software structures (struct e1000_adapter)
1328  * @adapter: board private structure to initialize
1329  *
1330  * e1000_sw_init initializes the Adapter private data structure.
1331  * Fields are initialized based on PCI device information and
1332  * OS network device settings (MTU size).
1333  **/
1334
1335 static int __devinit e1000_sw_init(struct e1000_adapter *adapter)
1336 {
1337         struct e1000_hw *hw = &adapter->hw;
1338         struct net_device *netdev = adapter->netdev;
1339         struct pci_dev *pdev = adapter->pdev;
1340
1341         /* PCI config space info */
1342
1343         hw->vendor_id = pdev->vendor;
1344         hw->device_id = pdev->device;
1345         hw->subsystem_vendor_id = pdev->subsystem_vendor;
1346         hw->subsystem_id = pdev->subsystem_device;
1347         hw->revision_id = pdev->revision;
1348
1349         pci_read_config_word(pdev, PCI_COMMAND, &hw->pci_cmd_word);
1350
1351         adapter->rx_buffer_len = MAXIMUM_ETHERNET_VLAN_SIZE;
1352         hw->max_frame_size = netdev->mtu +
1353                              ENET_HEADER_SIZE + ETHERNET_FCS_SIZE;
1354         hw->min_frame_size = MINIMUM_ETHERNET_FRAME_SIZE;
1355
1356         /* identify the MAC */
1357
1358         if (e1000_set_mac_type(hw)) {
1359                 DPRINTK(PROBE, ERR, "Unknown MAC Type\n");
1360                 return -EIO;
1361         }
1362
1363         switch (hw->mac_type) {
1364         default:
1365                 break;
1366         case e1000_82541:
1367         case e1000_82547:
1368         case e1000_82541_rev_2:
1369         case e1000_82547_rev_2:
1370                 hw->phy_init_script = 1;
1371                 break;
1372         }
1373
1374         e1000_set_media_type(hw);
1375
1376         hw->wait_autoneg_complete = false;
1377         hw->tbi_compatibility_en = true;
1378         hw->adaptive_ifs = true;
1379
1380         /* Copper options */
1381
1382         if (hw->media_type == e1000_media_type_copper) {
1383                 hw->mdix = AUTO_ALL_MODES;
1384                 hw->disable_polarity_correction = false;
1385                 hw->master_slave = E1000_MASTER_SLAVE;
1386         }
1387
1388         adapter->num_tx_queues = 1;
1389         adapter->num_rx_queues = 1;
1390
1391         if (e1000_alloc_queues(adapter)) {
1392                 DPRINTK(PROBE, ERR, "Unable to allocate memory for queues\n");
1393                 return -ENOMEM;
1394         }
1395
1396         /* Explicitly disable IRQ since the NIC can be in any state. */
1397         e1000_irq_disable(adapter);
1398
1399         spin_lock_init(&adapter->stats_lock);
1400
1401         set_bit(__E1000_DOWN, &adapter->flags);
1402
1403         return 0;
1404 }
1405
1406 /**
1407  * e1000_alloc_queues - Allocate memory for all rings
1408  * @adapter: board private structure to initialize
1409  *
1410  * We allocate one ring per queue at run-time since we don't know the
1411  * number of queues at compile-time.
1412  **/
1413
1414 static int __devinit e1000_alloc_queues(struct e1000_adapter *adapter)
1415 {
1416         adapter->tx_ring = kcalloc(adapter->num_tx_queues,
1417                                    sizeof(struct e1000_tx_ring), GFP_KERNEL);
1418         if (!adapter->tx_ring)
1419                 return -ENOMEM;
1420
1421         adapter->rx_ring = kcalloc(adapter->num_rx_queues,
1422                                    sizeof(struct e1000_rx_ring), GFP_KERNEL);
1423         if (!adapter->rx_ring) {
1424                 kfree(adapter->tx_ring);
1425                 return -ENOMEM;
1426         }
1427
1428         return E1000_SUCCESS;
1429 }
1430
1431 /**
1432  * e1000_open - Called when a network interface is made active
1433  * @netdev: network interface device structure
1434  *
1435  * Returns 0 on success, negative value on failure
1436  *
1437  * The open entry point is called when a network interface is made
1438  * active by the system (IFF_UP).  At this point all resources needed
1439  * for transmit and receive operations are allocated, the interrupt
1440  * handler is registered with the OS, the watchdog timer is started,
1441  * and the stack is notified that the interface is ready.
1442  **/
1443
1444 static int e1000_open(struct net_device *netdev)
1445 {
1446         struct e1000_adapter *adapter = netdev_priv(netdev);
1447         struct e1000_hw *hw = &adapter->hw;
1448         int err;
1449
1450         /* disallow open during test */
1451         if (test_bit(__E1000_TESTING, &adapter->flags))
1452                 return -EBUSY;
1453
1454         netif_carrier_off(netdev);
1455
1456         /* allocate transmit descriptors */
1457         err = e1000_setup_all_tx_resources(adapter);
1458         if (err)
1459                 goto err_setup_tx;
1460
1461         /* allocate receive descriptors */
1462         err = e1000_setup_all_rx_resources(adapter);
1463         if (err)
1464                 goto err_setup_rx;
1465
1466         e1000_power_up_phy(adapter);
1467
1468         adapter->mng_vlan_id = E1000_MNG_VLAN_NONE;
1469         if ((hw->mng_cookie.status &
1470                           E1000_MNG_DHCP_COOKIE_STATUS_VLAN_SUPPORT)) {
1471                 e1000_update_mng_vlan(adapter);
1472         }
1473
1474         /* If AMT is enabled, let the firmware know that the network
1475          * interface is now open */
1476         if (hw->mac_type == e1000_82573 &&
1477             e1000_check_mng_mode(hw))
1478                 e1000_get_hw_control(adapter);
1479
1480         /* before we allocate an interrupt, we must be ready to handle it.
1481          * Setting DEBUG_SHIRQ in the kernel makes it fire an interrupt
1482          * as soon as we call pci_request_irq, so we have to setup our
1483          * clean_rx handler before we do so.  */
1484         e1000_configure(adapter);
1485
1486         err = e1000_request_irq(adapter);
1487         if (err)
1488                 goto err_req_irq;
1489
1490         /* From here on the code is the same as e1000_up() */
1491         clear_bit(__E1000_DOWN, &adapter->flags);
1492
1493         napi_enable(&adapter->napi);
1494
1495         e1000_irq_enable(adapter);
1496
1497         netif_start_queue(netdev);
1498
1499         /* fire a link status change interrupt to start the watchdog */
1500         ew32(ICS, E1000_ICS_LSC);
1501
1502         return E1000_SUCCESS;
1503
1504 err_req_irq:
1505         e1000_release_hw_control(adapter);
1506         e1000_power_down_phy(adapter);
1507         e1000_free_all_rx_resources(adapter);
1508 err_setup_rx:
1509         e1000_free_all_tx_resources(adapter);
1510 err_setup_tx:
1511         e1000_reset(adapter);
1512
1513         return err;
1514 }
1515
1516 /**
1517  * e1000_close - Disables a network interface
1518  * @netdev: network interface device structure
1519  *
1520  * Returns 0, this is not allowed to fail
1521  *
1522  * The close entry point is called when an interface is de-activated
1523  * by the OS.  The hardware is still under the drivers control, but
1524  * needs to be disabled.  A global MAC reset is issued to stop the
1525  * hardware, and all transmit and receive resources are freed.
1526  **/
1527
1528 static int e1000_close(struct net_device *netdev)
1529 {
1530         struct e1000_adapter *adapter = netdev_priv(netdev);
1531         struct e1000_hw *hw = &adapter->hw;
1532
1533         WARN_ON(test_bit(__E1000_RESETTING, &adapter->flags));
1534         e1000_down(adapter);
1535         e1000_power_down_phy(adapter);
1536         e1000_free_irq(adapter);
1537
1538         e1000_free_all_tx_resources(adapter);
1539         e1000_free_all_rx_resources(adapter);
1540
1541         /* kill manageability vlan ID if supported, but not if a vlan with
1542          * the same ID is registered on the host OS (let 8021q kill it) */
1543         if ((hw->mng_cookie.status &
1544                           E1000_MNG_DHCP_COOKIE_STATUS_VLAN_SUPPORT) &&
1545              !(adapter->vlgrp &&
1546                vlan_group_get_device(adapter->vlgrp, adapter->mng_vlan_id))) {
1547                 e1000_vlan_rx_kill_vid(netdev, adapter->mng_vlan_id);
1548         }
1549
1550         /* If AMT is enabled, let the firmware know that the network
1551          * interface is now closed */
1552         if (hw->mac_type == e1000_82573 &&
1553             e1000_check_mng_mode(hw))
1554                 e1000_release_hw_control(adapter);
1555
1556         return 0;
1557 }
1558
1559 /**
1560  * e1000_check_64k_bound - check that memory doesn't cross 64kB boundary
1561  * @adapter: address of board private structure
1562  * @start: address of beginning of memory
1563  * @len: length of memory
1564  **/
1565 static bool e1000_check_64k_bound(struct e1000_adapter *adapter, void *start,
1566                                   unsigned long len)
1567 {
1568         struct e1000_hw *hw = &adapter->hw;
1569         unsigned long begin = (unsigned long)start;
1570         unsigned long end = begin + len;
1571
1572         /* First rev 82545 and 82546 need to not allow any memory
1573          * write location to cross 64k boundary due to errata 23 */
1574         if (hw->mac_type == e1000_82545 ||
1575             hw->mac_type == e1000_82546) {
1576                 return ((begin ^ (end - 1)) >> 16) != 0 ? false : true;
1577         }
1578
1579         return true;
1580 }
1581
1582 /**
1583  * e1000_setup_tx_resources - allocate Tx resources (Descriptors)
1584  * @adapter: board private structure
1585  * @txdr:    tx descriptor ring (for a specific queue) to setup
1586  *
1587  * Return 0 on success, negative on failure
1588  **/
1589
1590 static int e1000_setup_tx_resources(struct e1000_adapter *adapter,
1591                                     struct e1000_tx_ring *txdr)
1592 {
1593         struct pci_dev *pdev = adapter->pdev;
1594         int size;
1595
1596         size = sizeof(struct e1000_buffer) * txdr->count;
1597         txdr->buffer_info = vmalloc(size);
1598         if (!txdr->buffer_info) {
1599                 DPRINTK(PROBE, ERR,
1600                 "Unable to allocate memory for the transmit descriptor ring\n");
1601                 return -ENOMEM;
1602         }
1603         memset(txdr->buffer_info, 0, size);
1604
1605         /* round up to nearest 4K */
1606
1607         txdr->size = txdr->count * sizeof(struct e1000_tx_desc);
1608         txdr->size = ALIGN(txdr->size, 4096);
1609
1610         txdr->desc = pci_alloc_consistent(pdev, txdr->size, &txdr->dma);
1611         if (!txdr->desc) {
1612 setup_tx_desc_die:
1613                 vfree(txdr->buffer_info);
1614                 DPRINTK(PROBE, ERR,
1615                 "Unable to allocate memory for the transmit descriptor ring\n");
1616                 return -ENOMEM;
1617         }
1618
1619         /* Fix for errata 23, can't cross 64kB boundary */
1620         if (!e1000_check_64k_bound(adapter, txdr->desc, txdr->size)) {
1621                 void *olddesc = txdr->desc;
1622                 dma_addr_t olddma = txdr->dma;
1623                 DPRINTK(TX_ERR, ERR, "txdr align check failed: %u bytes "
1624                                      "at %p\n", txdr->size, txdr->desc);
1625                 /* Try again, without freeing the previous */
1626                 txdr->desc = pci_alloc_consistent(pdev, txdr->size, &txdr->dma);
1627                 /* Failed allocation, critical failure */
1628                 if (!txdr->desc) {
1629                         pci_free_consistent(pdev, txdr->size, olddesc, olddma);
1630                         goto setup_tx_desc_die;
1631                 }
1632
1633                 if (!e1000_check_64k_bound(adapter, txdr->desc, txdr->size)) {
1634                         /* give up */
1635                         pci_free_consistent(pdev, txdr->size, txdr->desc,
1636                                             txdr->dma);
1637                         pci_free_consistent(pdev, txdr->size, olddesc, olddma);
1638                         DPRINTK(PROBE, ERR,
1639                                 "Unable to allocate aligned memory "
1640                                 "for the transmit descriptor ring\n");
1641                         vfree(txdr->buffer_info);
1642                         return -ENOMEM;
1643                 } else {
1644                         /* Free old allocation, new allocation was successful */
1645                         pci_free_consistent(pdev, txdr->size, olddesc, olddma);
1646                 }
1647         }
1648         memset(txdr->desc, 0, txdr->size);
1649
1650         txdr->next_to_use = 0;
1651         txdr->next_to_clean = 0;
1652
1653         return 0;
1654 }
1655
1656 /**
1657  * e1000_setup_all_tx_resources - wrapper to allocate Tx resources
1658  *                                (Descriptors) for all queues
1659  * @adapter: board private structure
1660  *
1661  * Return 0 on success, negative on failure
1662  **/
1663
1664 int e1000_setup_all_tx_resources(struct e1000_adapter *adapter)
1665 {
1666         int i, err = 0;
1667
1668         for (i = 0; i < adapter->num_tx_queues; i++) {
1669                 err = e1000_setup_tx_resources(adapter, &adapter->tx_ring[i]);
1670                 if (err) {
1671                         DPRINTK(PROBE, ERR,
1672                                 "Allocation for Tx Queue %u failed\n", i);
1673                         for (i-- ; i >= 0; i--)
1674                                 e1000_free_tx_resources(adapter,
1675                                                         &adapter->tx_ring[i]);
1676                         break;
1677                 }
1678         }
1679
1680         return err;
1681 }
1682
1683 /**
1684  * e1000_configure_tx - Configure 8254x Transmit Unit after Reset
1685  * @adapter: board private structure
1686  *
1687  * Configure the Tx unit of the MAC after a reset.
1688  **/
1689
1690 static void e1000_configure_tx(struct e1000_adapter *adapter)
1691 {
1692         u64 tdba;
1693         struct e1000_hw *hw = &adapter->hw;
1694         u32 tdlen, tctl, tipg, tarc;
1695         u32 ipgr1, ipgr2;
1696
1697         /* Setup the HW Tx Head and Tail descriptor pointers */
1698
1699         switch (adapter->num_tx_queues) {
1700         case 1:
1701         default:
1702                 tdba = adapter->tx_ring[0].dma;
1703                 tdlen = adapter->tx_ring[0].count *
1704                         sizeof(struct e1000_tx_desc);
1705                 ew32(TDLEN, tdlen);
1706                 ew32(TDBAH, (tdba >> 32));
1707                 ew32(TDBAL, (tdba & 0x00000000ffffffffULL));
1708                 ew32(TDT, 0);
1709                 ew32(TDH, 0);
1710                 adapter->tx_ring[0].tdh = ((hw->mac_type >= e1000_82543) ? E1000_TDH : E1000_82542_TDH);
1711                 adapter->tx_ring[0].tdt = ((hw->mac_type >= e1000_82543) ? E1000_TDT : E1000_82542_TDT);
1712                 break;
1713         }
1714
1715         /* Set the default values for the Tx Inter Packet Gap timer */
1716         if (hw->mac_type <= e1000_82547_rev_2 &&
1717             (hw->media_type == e1000_media_type_fiber ||
1718              hw->media_type == e1000_media_type_internal_serdes))
1719                 tipg = DEFAULT_82543_TIPG_IPGT_FIBER;
1720         else
1721                 tipg = DEFAULT_82543_TIPG_IPGT_COPPER;
1722
1723         switch (hw->mac_type) {
1724         case e1000_82542_rev2_0:
1725         case e1000_82542_rev2_1:
1726                 tipg = DEFAULT_82542_TIPG_IPGT;
1727                 ipgr1 = DEFAULT_82542_TIPG_IPGR1;
1728                 ipgr2 = DEFAULT_82542_TIPG_IPGR2;
1729                 break;
1730         case e1000_80003es2lan:
1731                 ipgr1 = DEFAULT_82543_TIPG_IPGR1;
1732                 ipgr2 = DEFAULT_80003ES2LAN_TIPG_IPGR2;
1733                 break;
1734         default:
1735                 ipgr1 = DEFAULT_82543_TIPG_IPGR1;
1736                 ipgr2 = DEFAULT_82543_TIPG_IPGR2;
1737                 break;
1738         }
1739         tipg |= ipgr1 << E1000_TIPG_IPGR1_SHIFT;
1740         tipg |= ipgr2 << E1000_TIPG_IPGR2_SHIFT;
1741         ew32(TIPG, tipg);
1742
1743         /* Set the Tx Interrupt Delay register */
1744
1745         ew32(TIDV, adapter->tx_int_delay);
1746         if (hw->mac_type >= e1000_82540)
1747                 ew32(TADV, adapter->tx_abs_int_delay);
1748
1749         /* Program the Transmit Control Register */
1750
1751         tctl = er32(TCTL);
1752         tctl &= ~E1000_TCTL_CT;
1753         tctl |= E1000_TCTL_PSP | E1000_TCTL_RTLC |
1754                 (E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT);
1755
1756         if (hw->mac_type == e1000_82571 || hw->mac_type == e1000_82572) {
1757                 tarc = er32(TARC0);
1758                 /* set the speed mode bit, we'll clear it if we're not at
1759                  * gigabit link later */
1760                 tarc |= (1 << 21);
1761                 ew32(TARC0, tarc);
1762         } else if (hw->mac_type == e1000_80003es2lan) {
1763                 tarc = er32(TARC0);
1764                 tarc |= 1;
1765                 ew32(TARC0, tarc);
1766                 tarc = er32(TARC1);
1767                 tarc |= 1;
1768                 ew32(TARC1, tarc);
1769         }
1770
1771         e1000_config_collision_dist(hw);
1772
1773         /* Setup Transmit Descriptor Settings for eop descriptor */
1774         adapter->txd_cmd = E1000_TXD_CMD_EOP | E1000_TXD_CMD_IFCS;
1775
1776         /* only set IDE if we are delaying interrupts using the timers */
1777         if (adapter->tx_int_delay)
1778                 adapter->txd_cmd |= E1000_TXD_CMD_IDE;
1779
1780         if (hw->mac_type < e1000_82543)
1781                 adapter->txd_cmd |= E1000_TXD_CMD_RPS;
1782         else
1783                 adapter->txd_cmd |= E1000_TXD_CMD_RS;
1784
1785         /* Cache if we're 82544 running in PCI-X because we'll
1786          * need this to apply a workaround later in the send path. */
1787         if (hw->mac_type == e1000_82544 &&
1788             hw->bus_type == e1000_bus_type_pcix)
1789                 adapter->pcix_82544 = 1;
1790
1791         ew32(TCTL, tctl);
1792
1793 }
1794
1795 /**
1796  * e1000_setup_rx_resources - allocate Rx resources (Descriptors)
1797  * @adapter: board private structure
1798  * @rxdr:    rx descriptor ring (for a specific queue) to setup
1799  *
1800  * Returns 0 on success, negative on failure
1801  **/
1802
1803 static int e1000_setup_rx_resources(struct e1000_adapter *adapter,
1804                                     struct e1000_rx_ring *rxdr)
1805 {
1806         struct e1000_hw *hw = &adapter->hw;
1807         struct pci_dev *pdev = adapter->pdev;
1808         int size, desc_len;
1809
1810         size = sizeof(struct e1000_buffer) * rxdr->count;
1811         rxdr->buffer_info = vmalloc(size);
1812         if (!rxdr->buffer_info) {
1813                 DPRINTK(PROBE, ERR,
1814                 "Unable to allocate memory for the receive descriptor ring\n");
1815                 return -ENOMEM;
1816         }
1817         memset(rxdr->buffer_info, 0, size);
1818
1819         if (hw->mac_type <= e1000_82547_rev_2)
1820                 desc_len = sizeof(struct e1000_rx_desc);
1821         else
1822                 desc_len = sizeof(union e1000_rx_desc_packet_split);
1823
1824         /* Round up to nearest 4K */
1825
1826         rxdr->size = rxdr->count * desc_len;
1827         rxdr->size = ALIGN(rxdr->size, 4096);
1828
1829         rxdr->desc = pci_alloc_consistent(pdev, rxdr->size, &rxdr->dma);
1830
1831         if (!rxdr->desc) {
1832                 DPRINTK(PROBE, ERR,
1833                 "Unable to allocate memory for the receive descriptor ring\n");
1834 setup_rx_desc_die:
1835                 vfree(rxdr->buffer_info);
1836                 return -ENOMEM;
1837         }
1838
1839         /* Fix for errata 23, can't cross 64kB boundary */
1840         if (!e1000_check_64k_bound(adapter, rxdr->desc, rxdr->size)) {
1841                 void *olddesc = rxdr->desc;
1842                 dma_addr_t olddma = rxdr->dma;
1843                 DPRINTK(RX_ERR, ERR, "rxdr align check failed: %u bytes "
1844                                      "at %p\n", rxdr->size, rxdr->desc);
1845                 /* Try again, without freeing the previous */
1846                 rxdr->desc = pci_alloc_consistent(pdev, rxdr->size, &rxdr->dma);
1847                 /* Failed allocation, critical failure */
1848                 if (!rxdr->desc) {
1849                         pci_free_consistent(pdev, rxdr->size, olddesc, olddma);
1850                         DPRINTK(PROBE, ERR,
1851                                 "Unable to allocate memory "
1852                                 "for the receive descriptor ring\n");
1853                         goto setup_rx_desc_die;
1854                 }
1855
1856                 if (!e1000_check_64k_bound(adapter, rxdr->desc, rxdr->size)) {
1857                         /* give up */
1858                         pci_free_consistent(pdev, rxdr->size, rxdr->desc,
1859                                             rxdr->dma);
1860                         pci_free_consistent(pdev, rxdr->size, olddesc, olddma);
1861                         DPRINTK(PROBE, ERR,
1862                                 "Unable to allocate aligned memory "
1863                                 "for the receive descriptor ring\n");
1864                         goto setup_rx_desc_die;
1865                 } else {
1866                         /* Free old allocation, new allocation was successful */
1867                         pci_free_consistent(pdev, rxdr->size, olddesc, olddma);
1868                 }
1869         }
1870         memset(rxdr->desc, 0, rxdr->size);
1871
1872         rxdr->next_to_clean = 0;
1873         rxdr->next_to_use = 0;
1874         rxdr->rx_skb_top = NULL;
1875
1876         return 0;
1877 }
1878
1879 /**
1880  * e1000_setup_all_rx_resources - wrapper to allocate Rx resources
1881  *                                (Descriptors) for all queues
1882  * @adapter: board private structure
1883  *
1884  * Return 0 on success, negative on failure
1885  **/
1886
1887 int e1000_setup_all_rx_resources(struct e1000_adapter *adapter)
1888 {
1889         int i, err = 0;
1890
1891         for (i = 0; i < adapter->num_rx_queues; i++) {
1892                 err = e1000_setup_rx_resources(adapter, &adapter->rx_ring[i]);
1893                 if (err) {
1894                         DPRINTK(PROBE, ERR,
1895                                 "Allocation for Rx Queue %u failed\n", i);
1896                         for (i-- ; i >= 0; i--)
1897                                 e1000_free_rx_resources(adapter,
1898                                                         &adapter->rx_ring[i]);
1899                         break;
1900                 }
1901         }
1902
1903         return err;
1904 }
1905
1906 /**
1907  * e1000_setup_rctl - configure the receive control registers
1908  * @adapter: Board private structure
1909  **/
1910 static void e1000_setup_rctl(struct e1000_adapter *adapter)
1911 {
1912         struct e1000_hw *hw = &adapter->hw;
1913         u32 rctl;
1914
1915         rctl = er32(RCTL);
1916
1917         rctl &= ~(3 << E1000_RCTL_MO_SHIFT);
1918
1919         rctl |= E1000_RCTL_EN | E1000_RCTL_BAM |
1920                 E1000_RCTL_LBM_NO | E1000_RCTL_RDMTS_HALF |
1921                 (hw->mc_filter_type << E1000_RCTL_MO_SHIFT);
1922
1923         if (hw->tbi_compatibility_on == 1)
1924                 rctl |= E1000_RCTL_SBP;
1925         else
1926                 rctl &= ~E1000_RCTL_SBP;
1927
1928         if (adapter->netdev->mtu <= ETH_DATA_LEN)
1929                 rctl &= ~E1000_RCTL_LPE;
1930         else
1931                 rctl |= E1000_RCTL_LPE;
1932
1933         /* Setup buffer sizes */
1934         rctl &= ~E1000_RCTL_SZ_4096;
1935         rctl |= E1000_RCTL_BSEX;
1936         switch (adapter->rx_buffer_len) {
1937                 case E1000_RXBUFFER_256:
1938                         rctl |= E1000_RCTL_SZ_256;
1939                         rctl &= ~E1000_RCTL_BSEX;
1940                         break;
1941                 case E1000_RXBUFFER_512:
1942                         rctl |= E1000_RCTL_SZ_512;
1943                         rctl &= ~E1000_RCTL_BSEX;
1944                         break;
1945                 case E1000_RXBUFFER_1024:
1946                         rctl |= E1000_RCTL_SZ_1024;
1947                         rctl &= ~E1000_RCTL_BSEX;
1948                         break;
1949                 case E1000_RXBUFFER_2048:
1950                 default:
1951                         rctl |= E1000_RCTL_SZ_2048;
1952                         rctl &= ~E1000_RCTL_BSEX;
1953                         break;
1954                 case E1000_RXBUFFER_4096:
1955                         rctl |= E1000_RCTL_SZ_4096;
1956                         break;
1957                 case E1000_RXBUFFER_8192:
1958                         rctl |= E1000_RCTL_SZ_8192;
1959                         break;
1960                 case E1000_RXBUFFER_16384:
1961                         rctl |= E1000_RCTL_SZ_16384;
1962                         break;
1963         }
1964
1965         ew32(RCTL, rctl);
1966 }
1967
1968 /**
1969  * e1000_configure_rx - Configure 8254x Receive Unit after Reset
1970  * @adapter: board private structure
1971  *
1972  * Configure the Rx unit of the MAC after a reset.
1973  **/
1974
1975 static void e1000_configure_rx(struct e1000_adapter *adapter)
1976 {
1977         u64 rdba;
1978         struct e1000_hw *hw = &adapter->hw;
1979         u32 rdlen, rctl, rxcsum, ctrl_ext;
1980
1981         if (adapter->netdev->mtu > ETH_DATA_LEN) {
1982                 rdlen = adapter->rx_ring[0].count *
1983                         sizeof(struct e1000_rx_desc);
1984                 adapter->clean_rx = e1000_clean_jumbo_rx_irq;
1985                 adapter->alloc_rx_buf = e1000_alloc_jumbo_rx_buffers;
1986         } else {
1987                 rdlen = adapter->rx_ring[0].count *
1988                         sizeof(struct e1000_rx_desc);
1989                 adapter->clean_rx = e1000_clean_rx_irq;
1990                 adapter->alloc_rx_buf = e1000_alloc_rx_buffers;
1991         }
1992
1993         /* disable receives while setting up the descriptors */
1994         rctl = er32(RCTL);
1995         ew32(RCTL, rctl & ~E1000_RCTL_EN);
1996
1997         /* set the Receive Delay Timer Register */
1998         ew32(RDTR, adapter->rx_int_delay);
1999
2000         if (hw->mac_type >= e1000_82540) {
2001                 ew32(RADV, adapter->rx_abs_int_delay);
2002                 if (adapter->itr_setting != 0)
2003                         ew32(ITR, 1000000000 / (adapter->itr * 256));
2004         }
2005
2006         if (hw->mac_type >= e1000_82571) {
2007                 ctrl_ext = er32(CTRL_EXT);
2008                 /* Reset delay timers after every interrupt */
2009                 ctrl_ext |= E1000_CTRL_EXT_INT_TIMER_CLR;
2010                 /* Auto-Mask interrupts upon ICR access */
2011                 ctrl_ext |= E1000_CTRL_EXT_IAME;
2012                 ew32(IAM, 0xffffffff);
2013                 ew32(CTRL_EXT, ctrl_ext);
2014                 E1000_WRITE_FLUSH();
2015         }
2016
2017         /* Setup the HW Rx Head and Tail Descriptor Pointers and
2018          * the Base and Length of the Rx Descriptor Ring */
2019         switch (adapter->num_rx_queues) {
2020         case 1:
2021         default:
2022                 rdba = adapter->rx_ring[0].dma;
2023                 ew32(RDLEN, rdlen);
2024                 ew32(RDBAH, (rdba >> 32));
2025                 ew32(RDBAL, (rdba & 0x00000000ffffffffULL));
2026                 ew32(RDT, 0);
2027                 ew32(RDH, 0);
2028                 adapter->rx_ring[0].rdh = ((hw->mac_type >= e1000_82543) ? E1000_RDH : E1000_82542_RDH);
2029                 adapter->rx_ring[0].rdt = ((hw->mac_type >= e1000_82543) ? E1000_RDT : E1000_82542_RDT);
2030                 break;
2031         }
2032
2033         /* Enable 82543 Receive Checksum Offload for TCP and UDP */
2034         if (hw->mac_type >= e1000_82543) {
2035                 rxcsum = er32(RXCSUM);
2036                 if (adapter->rx_csum)
2037                         rxcsum |= E1000_RXCSUM_TUOFL;
2038                 else
2039                         /* don't need to clear IPPCSE as it defaults to 0 */
2040                         rxcsum &= ~E1000_RXCSUM_TUOFL;
2041                 ew32(RXCSUM, rxcsum);
2042         }
2043
2044         /* Enable Receives */
2045         ew32(RCTL, rctl);
2046 }
2047
2048 /**
2049  * e1000_free_tx_resources - Free Tx Resources per Queue
2050  * @adapter: board private structure
2051  * @tx_ring: Tx descriptor ring for a specific queue
2052  *
2053  * Free all transmit software resources
2054  **/
2055
2056 static void e1000_free_tx_resources(struct e1000_adapter *adapter,
2057                                     struct e1000_tx_ring *tx_ring)
2058 {
2059         struct pci_dev *pdev = adapter->pdev;
2060
2061         e1000_clean_tx_ring(adapter, tx_ring);
2062
2063         vfree(tx_ring->buffer_info);
2064         tx_ring->buffer_info = NULL;
2065
2066         pci_free_consistent(pdev, tx_ring->size, tx_ring->desc, tx_ring->dma);
2067
2068         tx_ring->desc = NULL;
2069 }
2070
2071 /**
2072  * e1000_free_all_tx_resources - Free Tx Resources for All Queues
2073  * @adapter: board private structure
2074  *
2075  * Free all transmit software resources
2076  **/
2077
2078 void e1000_free_all_tx_resources(struct e1000_adapter *adapter)
2079 {
2080         int i;
2081
2082         for (i = 0; i < adapter->num_tx_queues; i++)
2083                 e1000_free_tx_resources(adapter, &adapter->tx_ring[i]);
2084 }
2085
2086 static void e1000_unmap_and_free_tx_resource(struct e1000_adapter *adapter,
2087                                              struct e1000_buffer *buffer_info)
2088 {
2089         buffer_info->dma = 0;
2090         if (buffer_info->skb) {
2091                 skb_dma_unmap(&adapter->pdev->dev, buffer_info->skb,
2092                               DMA_TO_DEVICE);
2093                 dev_kfree_skb_any(buffer_info->skb);
2094                 buffer_info->skb = NULL;
2095         }
2096         buffer_info->time_stamp = 0;
2097         /* buffer_info must be completely set up in the transmit path */
2098 }
2099
2100 /**
2101  * e1000_clean_tx_ring - Free Tx Buffers
2102  * @adapter: board private structure
2103  * @tx_ring: ring to be cleaned
2104  **/
2105
2106 static void e1000_clean_tx_ring(struct e1000_adapter *adapter,
2107                                 struct e1000_tx_ring *tx_ring)
2108 {
2109         struct e1000_hw *hw = &adapter->hw;
2110         struct e1000_buffer *buffer_info;
2111         unsigned long size;
2112         unsigned int i;
2113
2114         /* Free all the Tx ring sk_buffs */
2115
2116         for (i = 0; i < tx_ring->count; i++) {
2117                 buffer_info = &tx_ring->buffer_info[i];
2118                 e1000_unmap_and_free_tx_resource(adapter, buffer_info);
2119         }
2120
2121         size = sizeof(struct e1000_buffer) * tx_ring->count;
2122         memset(tx_ring->buffer_info, 0, size);
2123
2124         /* Zero out the descriptor ring */
2125
2126         memset(tx_ring->desc, 0, tx_ring->size);
2127
2128         tx_ring->next_to_use = 0;
2129         tx_ring->next_to_clean = 0;
2130         tx_ring->last_tx_tso = 0;
2131
2132         writel(0, hw->hw_addr + tx_ring->tdh);
2133         writel(0, hw->hw_addr + tx_ring->tdt);
2134 }
2135
2136 /**
2137  * e1000_clean_all_tx_rings - Free Tx Buffers for all queues
2138  * @adapter: board private structure
2139  **/
2140
2141 static void e1000_clean_all_tx_rings(struct e1000_adapter *adapter)
2142 {
2143         int i;
2144
2145         for (i = 0; i < adapter->num_tx_queues; i++)
2146                 e1000_clean_tx_ring(adapter, &adapter->tx_ring[i]);
2147 }
2148
2149 /**
2150  * e1000_free_rx_resources - Free Rx Resources
2151  * @adapter: board private structure
2152  * @rx_ring: ring to clean the resources from
2153  *
2154  * Free all receive software resources
2155  **/
2156
2157 static void e1000_free_rx_resources(struct e1000_adapter *adapter,
2158                                     struct e1000_rx_ring *rx_ring)
2159 {
2160         struct pci_dev *pdev = adapter->pdev;
2161
2162         e1000_clean_rx_ring(adapter, rx_ring);
2163
2164         vfree(rx_ring->buffer_info);
2165         rx_ring->buffer_info = NULL;
2166
2167         pci_free_consistent(pdev, rx_ring->size, rx_ring->desc, rx_ring->dma);
2168
2169         rx_ring->desc = NULL;
2170 }
2171
2172 /**
2173  * e1000_free_all_rx_resources - Free Rx Resources for All Queues
2174  * @adapter: board private structure
2175  *
2176  * Free all receive software resources
2177  **/
2178
2179 void e1000_free_all_rx_resources(struct e1000_adapter *adapter)
2180 {
2181         int i;
2182
2183         for (i = 0; i < adapter->num_rx_queues; i++)
2184                 e1000_free_rx_resources(adapter, &adapter->rx_ring[i]);
2185 }
2186
2187 /**
2188  * e1000_clean_rx_ring - Free Rx Buffers per Queue
2189  * @adapter: board private structure
2190  * @rx_ring: ring to free buffers from
2191  **/
2192
2193 static void e1000_clean_rx_ring(struct e1000_adapter *adapter,
2194                                 struct e1000_rx_ring *rx_ring)
2195 {
2196         struct e1000_hw *hw = &adapter->hw;
2197         struct e1000_buffer *buffer_info;
2198         struct pci_dev *pdev = adapter->pdev;
2199         unsigned long size;
2200         unsigned int i;
2201
2202         /* Free all the Rx ring sk_buffs */
2203         for (i = 0; i < rx_ring->count; i++) {
2204                 buffer_info = &rx_ring->buffer_info[i];
2205                 if (buffer_info->dma &&
2206                     adapter->clean_rx == e1000_clean_rx_irq) {
2207                         pci_unmap_single(pdev, buffer_info->dma,
2208                                          buffer_info->length,
2209                                          PCI_DMA_FROMDEVICE);
2210                 } else if (buffer_info->dma &&
2211                            adapter->clean_rx == e1000_clean_jumbo_rx_irq) {
2212                         pci_unmap_page(pdev, buffer_info->dma,
2213                                        buffer_info->length,
2214                                        PCI_DMA_FROMDEVICE);
2215                 }
2216
2217                 buffer_info->dma = 0;
2218                 if (buffer_info->page) {
2219                         put_page(buffer_info->page);
2220                         buffer_info->page = NULL;
2221                 }
2222                 if (buffer_info->skb) {
2223                         dev_kfree_skb(buffer_info->skb);
2224                         buffer_info->skb = NULL;
2225                 }
2226         }
2227
2228         /* there also may be some cached data from a chained receive */
2229         if (rx_ring->rx_skb_top) {
2230                 dev_kfree_skb(rx_ring->rx_skb_top);
2231                 rx_ring->rx_skb_top = NULL;
2232         }
2233
2234         size = sizeof(struct e1000_buffer) * rx_ring->count;
2235         memset(rx_ring->buffer_info, 0, size);
2236
2237         /* Zero out the descriptor ring */
2238         memset(rx_ring->desc, 0, rx_ring->size);
2239
2240         rx_ring->next_to_clean = 0;
2241         rx_ring->next_to_use = 0;
2242
2243         writel(0, hw->hw_addr + rx_ring->rdh);
2244         writel(0, hw->hw_addr + rx_ring->rdt);
2245 }
2246
2247 /**
2248  * e1000_clean_all_rx_rings - Free Rx Buffers for all queues
2249  * @adapter: board private structure
2250  **/
2251
2252 static void e1000_clean_all_rx_rings(struct e1000_adapter *adapter)
2253 {
2254         int i;
2255
2256         for (i = 0; i < adapter->num_rx_queues; i++)
2257                 e1000_clean_rx_ring(adapter, &adapter->rx_ring[i]);
2258 }
2259
2260 /* The 82542 2.0 (revision 2) needs to have the receive unit in reset
2261  * and memory write and invalidate disabled for certain operations
2262  */
2263 static void e1000_enter_82542_rst(struct e1000_adapter *adapter)
2264 {
2265         struct e1000_hw *hw = &adapter->hw;
2266         struct net_device *netdev = adapter->netdev;
2267         u32 rctl;
2268
2269         e1000_pci_clear_mwi(hw);
2270
2271         rctl = er32(RCTL);
2272         rctl |= E1000_RCTL_RST;
2273         ew32(RCTL, rctl);
2274         E1000_WRITE_FLUSH();
2275         mdelay(5);
2276
2277         if (netif_running(netdev))
2278                 e1000_clean_all_rx_rings(adapter);
2279 }
2280
2281 static void e1000_leave_82542_rst(struct e1000_adapter *adapter)
2282 {
2283         struct e1000_hw *hw = &adapter->hw;
2284         struct net_device *netdev = adapter->netdev;
2285         u32 rctl;
2286
2287         rctl = er32(RCTL);
2288         rctl &= ~E1000_RCTL_RST;
2289         ew32(RCTL, rctl);
2290         E1000_WRITE_FLUSH();
2291         mdelay(5);
2292
2293         if (hw->pci_cmd_word & PCI_COMMAND_INVALIDATE)
2294                 e1000_pci_set_mwi(hw);
2295
2296         if (netif_running(netdev)) {
2297                 /* No need to loop, because 82542 supports only 1 queue */
2298                 struct e1000_rx_ring *ring = &adapter->rx_ring[0];
2299                 e1000_configure_rx(adapter);
2300                 adapter->alloc_rx_buf(adapter, ring, E1000_DESC_UNUSED(ring));
2301         }
2302 }
2303
2304 /**
2305  * e1000_set_mac - Change the Ethernet Address of the NIC
2306  * @netdev: network interface device structure
2307  * @p: pointer to an address structure
2308  *
2309  * Returns 0 on success, negative on failure
2310  **/
2311
2312 static int e1000_set_mac(struct net_device *netdev, void *p)
2313 {
2314         struct e1000_adapter *adapter = netdev_priv(netdev);
2315         struct e1000_hw *hw = &adapter->hw;
2316         struct sockaddr *addr = p;
2317
2318         if (!is_valid_ether_addr(addr->sa_data))
2319                 return -EADDRNOTAVAIL;
2320
2321         /* 82542 2.0 needs to be in reset to write receive address registers */
2322
2323         if (hw->mac_type == e1000_82542_rev2_0)
2324                 e1000_enter_82542_rst(adapter);
2325
2326         memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
2327         memcpy(hw->mac_addr, addr->sa_data, netdev->addr_len);
2328
2329         e1000_rar_set(hw, hw->mac_addr, 0);
2330
2331         /* With 82571 controllers, LAA may be overwritten (with the default)
2332          * due to controller reset from the other port. */
2333         if (hw->mac_type == e1000_82571) {
2334                 /* activate the work around */
2335                 hw->laa_is_present = 1;
2336
2337                 /* Hold a copy of the LAA in RAR[14] This is done so that
2338                  * between the time RAR[0] gets clobbered  and the time it
2339                  * gets fixed (in e1000_watchdog), the actual LAA is in one
2340                  * of the RARs and no incoming packets directed to this port
2341                  * are dropped. Eventaully the LAA will be in RAR[0] and
2342                  * RAR[14] */
2343                 e1000_rar_set(hw, hw->mac_addr,
2344                                         E1000_RAR_ENTRIES - 1);
2345         }
2346
2347         if (hw->mac_type == e1000_82542_rev2_0)
2348                 e1000_leave_82542_rst(adapter);
2349
2350         return 0;
2351 }
2352
2353 /**
2354  * e1000_set_rx_mode - Secondary Unicast, Multicast and Promiscuous mode set
2355  * @netdev: network interface device structure
2356  *
2357  * The set_rx_mode entry point is called whenever the unicast or multicast
2358  * address lists or the network interface flags are updated. This routine is
2359  * responsible for configuring the hardware for proper unicast, multicast,
2360  * promiscuous mode, and all-multi behavior.
2361  **/
2362
2363 static void e1000_set_rx_mode(struct net_device *netdev)
2364 {
2365         struct e1000_adapter *adapter = netdev_priv(netdev);
2366         struct e1000_hw *hw = &adapter->hw;
2367         struct netdev_hw_addr *ha;
2368         bool use_uc = false;
2369         struct dev_addr_list *mc_ptr;
2370         u32 rctl;
2371         u32 hash_value;
2372         int i, rar_entries = E1000_RAR_ENTRIES;
2373         int mta_reg_count = (hw->mac_type == e1000_ich8lan) ?
2374                                 E1000_NUM_MTA_REGISTERS_ICH8LAN :
2375                                 E1000_NUM_MTA_REGISTERS;
2376         u32 *mcarray = kcalloc(mta_reg_count, sizeof(u32), GFP_ATOMIC);
2377
2378         if (!mcarray) {
2379                 DPRINTK(PROBE, ERR, "memory allocation failed\n");
2380                 return;
2381         }
2382
2383         if (hw->mac_type == e1000_ich8lan)
2384                 rar_entries = E1000_RAR_ENTRIES_ICH8LAN;
2385
2386         /* reserve RAR[14] for LAA over-write work-around */
2387         if (hw->mac_type == e1000_82571)
2388                 rar_entries--;
2389
2390         /* Check for Promiscuous and All Multicast modes */
2391
2392         rctl = er32(RCTL);
2393
2394         if (netdev->flags & IFF_PROMISC) {
2395                 rctl |= (E1000_RCTL_UPE | E1000_RCTL_MPE);
2396                 rctl &= ~E1000_RCTL_VFE;
2397         } else {
2398                 if (netdev->flags & IFF_ALLMULTI) {
2399                         rctl |= E1000_RCTL_MPE;
2400                 } else {
2401                         rctl &= ~E1000_RCTL_MPE;
2402                 }
2403                 if (adapter->hw.mac_type != e1000_ich8lan)
2404                         rctl |= E1000_RCTL_VFE;
2405         }
2406
2407         if (netdev->uc.count > rar_entries - 1) {
2408                 rctl |= E1000_RCTL_UPE;
2409         } else if (!(netdev->flags & IFF_PROMISC)) {
2410                 rctl &= ~E1000_RCTL_UPE;
2411                 use_uc = true;
2412         }
2413
2414         ew32(RCTL, rctl);
2415
2416         /* 82542 2.0 needs to be in reset to write receive address registers */
2417
2418         if (hw->mac_type == e1000_82542_rev2_0)
2419                 e1000_enter_82542_rst(adapter);
2420
2421         /* load the first 14 addresses into the exact filters 1-14. Unicast
2422          * addresses take precedence to avoid disabling unicast filtering
2423          * when possible.
2424          *
2425          * RAR 0 is used for the station MAC adddress
2426          * if there are not 14 addresses, go ahead and clear the filters
2427          * -- with 82571 controllers only 0-13 entries are filled here
2428          */
2429         i = 1;
2430         if (use_uc)
2431                 list_for_each_entry(ha, &netdev->uc.list, list) {
2432                         if (i == rar_entries)
2433                                 break;
2434                         e1000_rar_set(hw, ha->addr, i++);
2435                 }
2436
2437         WARN_ON(i == rar_entries);
2438
2439         mc_ptr = netdev->mc_list;
2440
2441         for (; i < rar_entries; i++) {
2442                 if (mc_ptr) {
2443                         e1000_rar_set(hw, mc_ptr->da_addr, i);
2444                         mc_ptr = mc_ptr->next;
2445                 } else {
2446                         E1000_WRITE_REG_ARRAY(hw, RA, i << 1, 0);
2447                         E1000_WRITE_FLUSH();
2448                         E1000_WRITE_REG_ARRAY(hw, RA, (i << 1) + 1, 0);
2449                         E1000_WRITE_FLUSH();
2450                 }
2451         }
2452
2453         /* load any remaining addresses into the hash table */
2454
2455         for (; mc_ptr; mc_ptr = mc_ptr->next) {
2456                 u32 hash_reg, hash_bit, mta;
2457                 hash_value = e1000_hash_mc_addr(hw, mc_ptr->da_addr);
2458                 hash_reg = (hash_value >> 5) & 0x7F;
2459                 hash_bit = hash_value & 0x1F;
2460                 mta = (1 << hash_bit);
2461                 mcarray[hash_reg] |= mta;
2462         }
2463
2464         /* write the hash table completely, write from bottom to avoid
2465          * both stupid write combining chipsets, and flushing each write */
2466         for (i = mta_reg_count - 1; i >= 0 ; i--) {
2467                 /*
2468                  * If we are on an 82544 has an errata where writing odd
2469                  * offsets overwrites the previous even offset, but writing
2470                  * backwards over the range solves the issue by always
2471                  * writing the odd offset first
2472                  */
2473                 E1000_WRITE_REG_ARRAY(hw, MTA, i, mcarray[i]);
2474         }
2475         E1000_WRITE_FLUSH();
2476
2477         if (hw->mac_type == e1000_82542_rev2_0)
2478                 e1000_leave_82542_rst(adapter);
2479
2480         kfree(mcarray);
2481 }
2482
2483 /* Need to wait a few seconds after link up to get diagnostic information from
2484  * the phy */
2485
2486 static void e1000_update_phy_info(unsigned long data)
2487 {
2488         struct e1000_adapter *adapter = (struct e1000_adapter *)data;
2489         struct e1000_hw *hw = &adapter->hw;
2490         e1000_phy_get_info(hw, &adapter->phy_info);
2491 }
2492
2493 /**
2494  * e1000_82547_tx_fifo_stall - Timer Call-back
2495  * @data: pointer to adapter cast into an unsigned long
2496  **/
2497
2498 static void e1000_82547_tx_fifo_stall(unsigned long data)
2499 {
2500         struct e1000_adapter *adapter = (struct e1000_adapter *)data;
2501         struct e1000_hw *hw = &adapter->hw;
2502         struct net_device *netdev = adapter->netdev;
2503         u32 tctl;
2504
2505         if (atomic_read(&adapter->tx_fifo_stall)) {
2506                 if ((er32(TDT) == er32(TDH)) &&
2507                    (er32(TDFT) == er32(TDFH)) &&
2508                    (er32(TDFTS) == er32(TDFHS))) {
2509                         tctl = er32(TCTL);
2510                         ew32(TCTL, tctl & ~E1000_TCTL_EN);
2511                         ew32(TDFT, adapter->tx_head_addr);
2512                         ew32(TDFH, adapter->tx_head_addr);
2513                         ew32(TDFTS, adapter->tx_head_addr);
2514                         ew32(TDFHS, adapter->tx_head_addr);
2515                         ew32(TCTL, tctl);
2516                         E1000_WRITE_FLUSH();
2517
2518                         adapter->tx_fifo_head = 0;
2519                         atomic_set(&adapter->tx_fifo_stall, 0);
2520                         netif_wake_queue(netdev);
2521                 } else {
2522                         mod_timer(&adapter->tx_fifo_stall_timer, jiffies + 1);
2523                 }
2524         }
2525 }
2526
2527 /**
2528  * e1000_watchdog - Timer Call-back
2529  * @data: pointer to adapter cast into an unsigned long
2530  **/
2531 static void e1000_watchdog(unsigned long data)
2532 {
2533         struct e1000_adapter *adapter = (struct e1000_adapter *)data;
2534         struct e1000_hw *hw = &adapter->hw;
2535         struct net_device *netdev = adapter->netdev;
2536         struct e1000_tx_ring *txdr = adapter->tx_ring;
2537         u32 link, tctl;
2538         s32 ret_val;
2539
2540         ret_val = e1000_check_for_link(hw);
2541         if ((ret_val == E1000_ERR_PHY) &&
2542             (hw->phy_type == e1000_phy_igp_3) &&
2543             (er32(CTRL) & E1000_PHY_CTRL_GBE_DISABLE)) {
2544                 /* See e1000_kumeran_lock_loss_workaround() */
2545                 DPRINTK(LINK, INFO,
2546                         "Gigabit has been disabled, downgrading speed\n");
2547         }
2548
2549         if (hw->mac_type == e1000_82573) {
2550                 e1000_enable_tx_pkt_filtering(hw);
2551                 if (adapter->mng_vlan_id != hw->mng_cookie.vlan_id)
2552                         e1000_update_mng_vlan(adapter);
2553         }
2554
2555         if ((hw->media_type == e1000_media_type_internal_serdes) &&
2556            !(er32(TXCW) & E1000_TXCW_ANE))
2557                 link = !hw->serdes_link_down;
2558         else
2559                 link = er32(STATUS) & E1000_STATUS_LU;
2560
2561         if (link) {
2562                 if (!netif_carrier_ok(netdev)) {
2563                         u32 ctrl;
2564                         bool txb2b = true;
2565                         e1000_get_speed_and_duplex(hw,
2566                                                    &adapter->link_speed,
2567                                                    &adapter->link_duplex);
2568
2569                         ctrl = er32(CTRL);
2570                         printk(KERN_INFO "e1000: %s NIC Link is Up %d Mbps %s, "
2571                                "Flow Control: %s\n",
2572                                netdev->name,
2573                                adapter->link_speed,
2574                                adapter->link_duplex == FULL_DUPLEX ?
2575                                 "Full Duplex" : "Half Duplex",
2576                                 ((ctrl & E1000_CTRL_TFCE) && (ctrl &
2577                                 E1000_CTRL_RFCE)) ? "RX/TX" : ((ctrl &
2578                                 E1000_CTRL_RFCE) ? "RX" : ((ctrl &
2579                                 E1000_CTRL_TFCE) ? "TX" : "None" )));
2580
2581                         /* tweak tx_queue_len according to speed/duplex
2582                          * and adjust the timeout factor */
2583                         netdev->tx_queue_len = adapter->tx_queue_len;
2584                         adapter->tx_timeout_factor = 1;
2585                         switch (adapter->link_speed) {
2586                         case SPEED_10:
2587                                 txb2b = false;
2588                                 netdev->tx_queue_len = 10;
2589                                 adapter->tx_timeout_factor = 8;
2590                                 break;
2591                         case SPEED_100:
2592                                 txb2b = false;
2593                                 netdev->tx_queue_len = 100;
2594                                 /* maybe add some timeout factor ? */
2595                                 break;
2596                         }
2597
2598                         if ((hw->mac_type == e1000_82571 ||
2599                              hw->mac_type == e1000_82572) &&
2600                             !txb2b) {
2601                                 u32 tarc0;
2602                                 tarc0 = er32(TARC0);
2603                                 tarc0 &= ~(1 << 21);
2604                                 ew32(TARC0, tarc0);
2605                         }
2606
2607                         /* disable TSO for pcie and 10/100 speeds, to avoid
2608                          * some hardware issues */
2609                         if (!adapter->tso_force &&
2610                             hw->bus_type == e1000_bus_type_pci_express){
2611                                 switch (adapter->link_speed) {
2612                                 case SPEED_10:
2613                                 case SPEED_100:
2614                                         DPRINTK(PROBE,INFO,
2615                                         "10/100 speed: disabling TSO\n");
2616                                         netdev->features &= ~NETIF_F_TSO;
2617                                         netdev->features &= ~NETIF_F_TSO6;
2618                                         break;
2619                                 case SPEED_1000:
2620                                         netdev->features |= NETIF_F_TSO;
2621                                         netdev->features |= NETIF_F_TSO6;
2622                                         break;
2623                                 default:
2624                                         /* oops */
2625                                         break;
2626                                 }
2627                         }
2628
2629                         /* enable transmits in the hardware, need to do this
2630                          * after setting TARC0 */
2631                         tctl = er32(TCTL);
2632                         tctl |= E1000_TCTL_EN;
2633                         ew32(TCTL, tctl);
2634
2635                         netif_carrier_on(netdev);
2636                         mod_timer(&adapter->phy_info_timer, round_jiffies(jiffies + 2 * HZ));
2637                         adapter->smartspeed = 0;
2638                 } else {
2639                         /* make sure the receive unit is started */
2640                         if (hw->rx_needs_kicking) {
2641                                 u32 rctl = er32(RCTL);
2642                                 ew32(RCTL, rctl | E1000_RCTL_EN);
2643                         }
2644                 }
2645         } else {
2646                 if (netif_carrier_ok(netdev)) {
2647                         adapter->link_speed = 0;
2648                         adapter->link_duplex = 0;
2649                         printk(KERN_INFO "e1000: %s NIC Link is Down\n",
2650                                netdev->name);
2651                         netif_carrier_off(netdev);
2652                         mod_timer(&adapter->phy_info_timer, round_jiffies(jiffies + 2 * HZ));
2653
2654                         /* 80003ES2LAN workaround--
2655                          * For packet buffer work-around on link down event;
2656                          * disable receives in the ISR and
2657                          * reset device here in the watchdog
2658                          */
2659                         if (hw->mac_type == e1000_80003es2lan)
2660                                 /* reset device */
2661                                 schedule_work(&adapter->reset_task);
2662                 }
2663
2664                 e1000_smartspeed(adapter);
2665         }
2666
2667         e1000_update_stats(adapter);
2668
2669         hw->tx_packet_delta = adapter->stats.tpt - adapter->tpt_old;
2670         adapter->tpt_old = adapter->stats.tpt;
2671         hw->collision_delta = adapter->stats.colc - adapter->colc_old;
2672         adapter->colc_old = adapter->stats.colc;
2673
2674         adapter->gorcl = adapter->stats.gorcl - adapter->gorcl_old;
2675         adapter->gorcl_old = adapter->stats.gorcl;
2676         adapter->gotcl = adapter->stats.gotcl - adapter->gotcl_old;
2677         adapter->gotcl_old = adapter->stats.gotcl;
2678
2679         e1000_update_adaptive(hw);
2680
2681         if (!netif_carrier_ok(netdev)) {
2682                 if (E1000_DESC_UNUSED(txdr) + 1 < txdr->count) {
2683                         /* We've lost link, so the controller stops DMA,
2684                          * but we've got queued Tx work that's never going
2685                          * to get done, so reset controller to flush Tx.
2686                          * (Do the reset outside of interrupt context). */
2687                         adapter->tx_timeout_count++;
2688                         schedule_work(&adapter->reset_task);
2689                         /* return immediately since reset is imminent */
2690                         return;
2691                 }
2692         }
2693
2694         /* Cause software interrupt to ensure rx ring is cleaned */
2695         ew32(ICS, E1000_ICS_RXDMT0);
2696
2697         /* Force detection of hung controller every watchdog period */
2698         adapter->detect_tx_hung = true;
2699
2700         /* With 82571 controllers, LAA may be overwritten due to controller
2701          * reset from the other port. Set the appropriate LAA in RAR[0] */
2702         if (hw->mac_type == e1000_82571 && hw->laa_is_present)
2703                 e1000_rar_set(hw, hw->mac_addr, 0);
2704
2705         /* Reset the timer */
2706         mod_timer(&adapter->watchdog_timer, round_jiffies(jiffies + 2 * HZ));
2707 }
2708
2709 enum latency_range {
2710         lowest_latency = 0,
2711         low_latency = 1,
2712         bulk_latency = 2,
2713         latency_invalid = 255
2714 };
2715
2716 /**
2717  * e1000_update_itr - update the dynamic ITR value based on statistics
2718  *      Stores a new ITR value based on packets and byte
2719  *      counts during the last interrupt.  The advantage of per interrupt
2720  *      computation is faster updates and more accurate ITR for the current
2721  *      traffic pattern.  Constants in this function were computed
2722  *      based on theoretical maximum wire speed and thresholds were set based
2723  *      on testing data as well as attempting to minimize response time
2724  *      while increasing bulk throughput.
2725  *      this functionality is controlled by the InterruptThrottleRate module
2726  *      parameter (see e1000_param.c)
2727  * @adapter: pointer to adapter
2728  * @itr_setting: current adapter->itr
2729  * @packets: the number of packets during this measurement interval
2730  * @bytes: the number of bytes during this measurement interval
2731  **/
2732 static unsigned int e1000_update_itr(struct e1000_adapter *adapter,
2733                                      u16 itr_setting, int packets, int bytes)
2734 {
2735         unsigned int retval = itr_setting;
2736         struct e1000_hw *hw = &adapter->hw;
2737
2738         if (unlikely(hw->mac_type < e1000_82540))
2739                 goto update_itr_done;
2740
2741         if (packets == 0)
2742                 goto update_itr_done;
2743
2744         switch (itr_setting) {
2745         case lowest_latency:
2746                 /* jumbo frames get bulk treatment*/
2747                 if (bytes/packets > 8000)
2748                         retval = bulk_latency;
2749                 else if ((packets < 5) && (bytes > 512))
2750                         retval = low_latency;
2751                 break;
2752         case low_latency:  /* 50 usec aka 20000 ints/s */
2753                 if (bytes > 10000) {
2754                         /* jumbo frames need bulk latency setting */
2755                         if (bytes/packets > 8000)
2756                                 retval = bulk_latency;
2757                         else if ((packets < 10) || ((bytes/packets) > 1200))
2758                                 retval = bulk_latency;
2759                         else if ((packets > 35))
2760                                 retval = lowest_latency;
2761                 } else if (bytes/packets > 2000)
2762                         retval = bulk_latency;
2763                 else if (packets <= 2 && bytes < 512)
2764                         retval = lowest_latency;
2765                 break;
2766         case bulk_latency: /* 250 usec aka 4000 ints/s */
2767                 if (bytes > 25000) {
2768                         if (packets > 35)
2769                                 retval = low_latency;
2770                 } else if (bytes < 6000) {
2771                         retval = low_latency;
2772                 }
2773                 break;
2774         }
2775
2776 update_itr_done:
2777         return retval;
2778 }
2779
2780 static void e1000_set_itr(struct e1000_adapter *adapter)
2781 {
2782         struct e1000_hw *hw = &adapter->hw;
2783         u16 current_itr;
2784         u32 new_itr = adapter->itr;
2785
2786         if (unlikely(hw->mac_type < e1000_82540))
2787                 return;
2788
2789         /* for non-gigabit speeds, just fix the interrupt rate at 4000 */
2790         if (unlikely(adapter->link_speed != SPEED_1000)) {
2791                 current_itr = 0;
2792                 new_itr = 4000;
2793                 goto set_itr_now;
2794         }
2795
2796         adapter->tx_itr = e1000_update_itr(adapter,
2797                                     adapter->tx_itr,
2798                                     adapter->total_tx_packets,
2799                                     adapter->total_tx_bytes);
2800         /* conservative mode (itr 3) eliminates the lowest_latency setting */
2801         if (adapter->itr_setting == 3 && adapter->tx_itr == lowest_latency)
2802                 adapter->tx_itr = low_latency;
2803
2804         adapter->rx_itr = e1000_update_itr(adapter,
2805                                     adapter->rx_itr,
2806                                     adapter->total_rx_packets,
2807                                     adapter->total_rx_bytes);
2808         /* conservative mode (itr 3) eliminates the lowest_latency setting */
2809         if (adapter->itr_setting == 3 && adapter->rx_itr == lowest_latency)
2810                 adapter->rx_itr = low_latency;
2811
2812         current_itr = max(adapter->rx_itr, adapter->tx_itr);
2813
2814         switch (current_itr) {
2815         /* counts and packets in update_itr are dependent on these numbers */
2816         case lowest_latency:
2817                 new_itr = 70000;
2818                 break;
2819         case low_latency:
2820                 new_itr = 20000; /* aka hwitr = ~200 */
2821                 break;
2822         case bulk_latency:
2823                 new_itr = 4000;
2824                 break;
2825         default:
2826                 break;
2827         }
2828
2829 set_itr_now:
2830         if (new_itr != adapter->itr) {
2831                 /* this attempts to bias the interrupt rate towards Bulk
2832                  * by adding intermediate steps when interrupt rate is
2833                  * increasing */
2834                 new_itr = new_itr > adapter->itr ?
2835                              min(adapter->itr + (new_itr >> 2), new_itr) :
2836                              new_itr;
2837                 adapter->itr = new_itr;
2838                 ew32(ITR, 1000000000 / (new_itr * 256));
2839         }
2840
2841         return;
2842 }
2843
2844 #define E1000_TX_FLAGS_CSUM             0x00000001
2845 #define E1000_TX_FLAGS_VLAN             0x00000002
2846 #define E1000_TX_FLAGS_TSO              0x00000004
2847 #define E1000_TX_FLAGS_IPV4             0x00000008
2848 #define E1000_TX_FLAGS_VLAN_MASK        0xffff0000
2849 #define E1000_TX_FLAGS_VLAN_SHIFT       16
2850
2851 static int e1000_tso(struct e1000_adapter *adapter,
2852                      struct e1000_tx_ring *tx_ring, struct sk_buff *skb)
2853 {
2854         struct e1000_context_desc *context_desc;
2855         struct e1000_buffer *buffer_info;
2856         unsigned int i;
2857         u32 cmd_length = 0;
2858         u16 ipcse = 0, tucse, mss;
2859         u8 ipcss, ipcso, tucss, tucso, hdr_len;
2860         int err;
2861
2862         if (skb_is_gso(skb)) {
2863                 if (skb_header_cloned(skb)) {
2864                         err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2865                         if (err)
2866                                 return err;
2867                 }
2868
2869                 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
2870                 mss = skb_shinfo(skb)->gso_size;
2871                 if (skb->protocol == htons(ETH_P_IP)) {
2872                         struct iphdr *iph = ip_hdr(skb);
2873                         iph->tot_len = 0;
2874                         iph->check = 0;
2875                         tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
2876                                                                  iph->daddr, 0,
2877                                                                  IPPROTO_TCP,
2878                                                                  0);
2879                         cmd_length = E1000_TXD_CMD_IP;
2880                         ipcse = skb_transport_offset(skb) - 1;
2881                 } else if (skb->protocol == htons(ETH_P_IPV6)) {
2882                         ipv6_hdr(skb)->payload_len = 0;
2883                         tcp_hdr(skb)->check =
2884                                 ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
2885                                                  &ipv6_hdr(skb)->daddr,
2886                                                  0, IPPROTO_TCP, 0);
2887                         ipcse = 0;
2888                 }
2889                 ipcss = skb_network_offset(skb);
2890                 ipcso = (void *)&(ip_hdr(skb)->check) - (void *)skb->data;
2891                 tucss = skb_transport_offset(skb);
2892                 tucso = (void *)&(tcp_hdr(skb)->check) - (void *)skb->data;
2893                 tucse = 0;
2894
2895                 cmd_length |= (E1000_TXD_CMD_DEXT | E1000_TXD_CMD_TSE |
2896                                E1000_TXD_CMD_TCP | (skb->len - (hdr_len)));
2897
2898                 i = tx_ring->next_to_use;
2899                 context_desc = E1000_CONTEXT_DESC(*tx_ring, i);
2900                 buffer_info = &tx_ring->buffer_info[i];
2901
2902                 context_desc->lower_setup.ip_fields.ipcss  = ipcss;
2903                 context_desc->lower_setup.ip_fields.ipcso  = ipcso;
2904                 context_desc->lower_setup.ip_fields.ipcse  = cpu_to_le16(ipcse);
2905                 context_desc->upper_setup.tcp_fields.tucss = tucss;
2906                 context_desc->upper_setup.tcp_fields.tucso = tucso;
2907                 context_desc->upper_setup.tcp_fields.tucse = cpu_to_le16(tucse);
2908                 context_desc->tcp_seg_setup.fields.mss     = cpu_to_le16(mss);
2909                 context_desc->tcp_seg_setup.fields.hdr_len = hdr_len;
2910                 context_desc->cmd_and_length = cpu_to_le32(cmd_length);
2911
2912                 buffer_info->time_stamp = jiffies;
2913                 buffer_info->next_to_watch = i;
2914
2915                 if (++i == tx_ring->count) i = 0;
2916                 tx_ring->next_to_use = i;
2917
2918                 return true;
2919         }
2920         return false;
2921 }
2922
2923 static bool e1000_tx_csum(struct e1000_adapter *adapter,
2924                           struct e1000_tx_ring *tx_ring, struct sk_buff *skb)
2925 {
2926         struct e1000_context_desc *context_desc;
2927         struct e1000_buffer *buffer_info;
2928         unsigned int i;
2929         u8 css;
2930         u32 cmd_len = E1000_TXD_CMD_DEXT;
2931
2932         if (skb->ip_summed != CHECKSUM_PARTIAL)
2933                 return false;
2934
2935         switch (skb->protocol) {
2936         case cpu_to_be16(ETH_P_IP):
2937                 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
2938                         cmd_len |= E1000_TXD_CMD_TCP;
2939                 break;
2940         case cpu_to_be16(ETH_P_IPV6):
2941                 /* XXX not handling all IPV6 headers */
2942                 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
2943                         cmd_len |= E1000_TXD_CMD_TCP;
2944                 break;
2945         default:
2946                 if (unlikely(net_ratelimit()))
2947                         DPRINTK(DRV, WARNING,
2948                                 "checksum_partial proto=%x!\n", skb->protocol);
2949                 break;
2950         }
2951
2952         css = skb_transport_offset(skb);
2953
2954         i = tx_ring->next_to_use;
2955         buffer_info = &tx_ring->buffer_info[i];
2956         context_desc = E1000_CONTEXT_DESC(*tx_ring, i);
2957
2958         context_desc->lower_setup.ip_config = 0;
2959         context_desc->upper_setup.tcp_fields.tucss = css;
2960         context_desc->upper_setup.tcp_fields.tucso =
2961                 css + skb->csum_offset;
2962         context_desc->upper_setup.tcp_fields.tucse = 0;
2963         context_desc->tcp_seg_setup.data = 0;
2964         context_desc->cmd_and_length = cpu_to_le32(cmd_len);
2965
2966         buffer_info->time_stamp = jiffies;
2967         buffer_info->next_to_watch = i;
2968
2969         if (unlikely(++i == tx_ring->count)) i = 0;
2970         tx_ring->next_to_use = i;
2971
2972         return true;
2973 }
2974
2975 #define E1000_MAX_TXD_PWR       12
2976 #define E1000_MAX_DATA_PER_TXD  (1<<E1000_MAX_TXD_PWR)
2977
2978 static int e1000_tx_map(struct e1000_adapter *adapter,
2979                         struct e1000_tx_ring *tx_ring,
2980                         struct sk_buff *skb, unsigned int first,
2981                         unsigned int max_per_txd, unsigned int nr_frags,
2982                         unsigned int mss)
2983 {
2984         struct e1000_hw *hw = &adapter->hw;
2985         struct e1000_buffer *buffer_info;
2986         unsigned int len = skb_headlen(skb);
2987         unsigned int offset, size, count = 0, i;
2988         unsigned int f;
2989         dma_addr_t *map;
2990
2991         i = tx_ring->next_to_use;
2992
2993         if (skb_dma_map(&adapter->pdev->dev, skb, DMA_TO_DEVICE)) {
2994                 dev_err(&adapter->pdev->dev, "TX DMA map failed\n");
2995                 return 0;
2996         }
2997
2998         map = skb_shinfo(skb)->dma_maps;
2999         offset = 0;
3000
3001         while (len) {
3002                 buffer_info = &tx_ring->buffer_info[i];
3003                 size = min(len, max_per_txd);
3004                 /* Workaround for Controller erratum --
3005                  * descriptor for non-tso packet in a linear SKB that follows a
3006                  * tso gets written back prematurely before the data is fully
3007                  * DMA'd to the controller */
3008                 if (!skb->data_len && tx_ring->last_tx_tso &&
3009                     !skb_is_gso(skb)) {
3010                         tx_ring->last_tx_tso = 0;
3011                         size -= 4;
3012                 }
3013
3014                 /* Workaround for premature desc write-backs
3015                  * in TSO mode.  Append 4-byte sentinel desc */
3016                 if (unlikely(mss && !nr_frags && size == len && size > 8))
3017                         size -= 4;
3018                 /* work-around for errata 10 and it applies
3019                  * to all controllers in PCI-X mode
3020                  * The fix is to make sure that the first descriptor of a
3021                  * packet is smaller than 2048 - 16 - 16 (or 2016) bytes
3022                  */
3023                 if (unlikely((hw->bus_type == e1000_bus_type_pcix) &&
3024                                 (size > 2015) && count == 0))
3025                         size = 2015;
3026
3027                 /* Workaround for potential 82544 hang in PCI-X.  Avoid
3028                  * terminating buffers within evenly-aligned dwords. */
3029                 if (unlikely(adapter->pcix_82544 &&
3030                    !((unsigned long)(skb->data + offset + size - 1) & 4) &&
3031                    size > 4))
3032                         size -= 4;
3033
3034                 buffer_info->length = size;
3035                 buffer_info->dma = skb_shinfo(skb)->dma_head + offset;
3036                 buffer_info->time_stamp = jiffies;
3037                 buffer_info->next_to_watch = i;
3038
3039                 len -= size;
3040                 offset += size;
3041                 count++;
3042                 if (len) {
3043                         i++;
3044                         if (unlikely(i == tx_ring->count))
3045                                 i = 0;
3046                 }
3047         }
3048
3049         for (f = 0; f < nr_frags; f++) {
3050                 struct skb_frag_struct *frag;
3051
3052                 frag = &skb_shinfo(skb)->frags[f];
3053                 len = frag->size;
3054                 offset = 0;
3055
3056                 while (len) {
3057                         i++;
3058                         if (unlikely(i == tx_ring->count))
3059                                 i = 0;
3060
3061                         buffer_info = &tx_ring->buffer_info[i];
3062                         size = min(len, max_per_txd);
3063                         /* Workaround for premature desc write-backs
3064                          * in TSO mode.  Append 4-byte sentinel desc */
3065                         if (unlikely(mss && f == (nr_frags-1) && size == len && size > 8))
3066                                 size -= 4;
3067                         /* Workaround for potential 82544 hang in PCI-X.
3068                          * Avoid terminating buffers within evenly-aligned
3069                          * dwords. */
3070                         if (unlikely(adapter->pcix_82544 &&
3071                            !((unsigned long)(frag->page+offset+size-1) & 4) &&
3072                            size > 4))
3073                                 size -= 4;
3074
3075                         buffer_info->length = size;
3076                         buffer_info->dma = map[f] + offset;
3077                         buffer_info->time_stamp = jiffies;
3078                         buffer_info->next_to_watch = i;
3079
3080                         len -= size;
3081                         offset += size;
3082                         count++;
3083                 }
3084         }
3085
3086         tx_ring->buffer_info[i].skb = skb;
3087         tx_ring->buffer_info[first].next_to_watch = i;
3088
3089         return count;
3090 }
3091
3092 static void e1000_tx_queue(struct e1000_adapter *adapter,
3093                            struct e1000_tx_ring *tx_ring, int tx_flags,
3094                            int count)
3095 {
3096         struct e1000_hw *hw = &adapter->hw;
3097         struct e1000_tx_desc *tx_desc = NULL;
3098         struct e1000_buffer *buffer_info;
3099         u32 txd_upper = 0, txd_lower = E1000_TXD_CMD_IFCS;
3100         unsigned int i;
3101
3102         if (likely(tx_flags & E1000_TX_FLAGS_TSO)) {
3103                 txd_lower |= E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D |
3104                              E1000_TXD_CMD_TSE;
3105                 txd_upper |= E1000_TXD_POPTS_TXSM << 8;
3106
3107                 if (likely(tx_flags & E1000_TX_FLAGS_IPV4))
3108                         txd_upper |= E1000_TXD_POPTS_IXSM << 8;
3109         }
3110
3111         if (likely(tx_flags & E1000_TX_FLAGS_CSUM)) {
3112                 txd_lower |= E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D;
3113                 txd_upper |= E1000_TXD_POPTS_TXSM << 8;
3114         }
3115
3116         if (unlikely(tx_flags & E1000_TX_FLAGS_VLAN)) {
3117                 txd_lower |= E1000_TXD_CMD_VLE;
3118                 txd_upper |= (tx_flags & E1000_TX_FLAGS_VLAN_MASK);
3119         }
3120
3121         i = tx_ring->next_to_use;
3122
3123         while (count--) {
3124                 buffer_info = &tx_ring->buffer_info[i];
3125                 tx_desc = E1000_TX_DESC(*tx_ring, i);
3126                 tx_desc->buffer_addr = cpu_to_le64(buffer_info->dma);
3127                 tx_desc->lower.data =
3128                         cpu_to_le32(txd_lower | buffer_info->length);
3129                 tx_desc->upper.data = cpu_to_le32(txd_upper);
3130                 if (unlikely(++i == tx_ring->count)) i = 0;
3131         }
3132
3133         tx_desc->lower.data |= cpu_to_le32(adapter->txd_cmd);
3134
3135         /* Force memory writes to complete before letting h/w
3136          * know there are new descriptors to fetch.  (Only
3137          * applicable for weak-ordered memory model archs,
3138          * such as IA-64). */
3139         wmb();
3140
3141         tx_ring->next_to_use = i;
3142         writel(i, hw->hw_addr + tx_ring->tdt);
3143         /* we need this if more than one processor can write to our tail
3144          * at a time, it syncronizes IO on IA64/Altix systems */
3145         mmiowb();
3146 }
3147
3148 /**
3149  * 82547 workaround to avoid controller hang in half-duplex environment.
3150  * The workaround is to avoid queuing a large packet that would span
3151  * the internal Tx FIFO ring boundary by notifying the stack to resend
3152  * the packet at a later time.  This gives the Tx FIFO an opportunity to
3153  * flush all packets.  When that occurs, we reset the Tx FIFO pointers
3154  * to the beginning of the Tx FIFO.
3155  **/
3156
3157 #define E1000_FIFO_HDR                  0x10
3158 #define E1000_82547_PAD_LEN             0x3E0
3159
3160 static int e1000_82547_fifo_workaround(struct e1000_adapter *adapter,
3161                                        struct sk_buff *skb)
3162 {
3163         u32 fifo_space = adapter->tx_fifo_size - adapter->tx_fifo_head;
3164         u32 skb_fifo_len = skb->len + E1000_FIFO_HDR;
3165
3166         skb_fifo_len = ALIGN(skb_fifo_len, E1000_FIFO_HDR);
3167
3168         if (adapter->link_duplex != HALF_DUPLEX)
3169                 goto no_fifo_stall_required;
3170
3171         if (atomic_read(&adapter->tx_fifo_stall))
3172                 return 1;
3173
3174         if (skb_fifo_len >= (E1000_82547_PAD_LEN + fifo_space)) {
3175                 atomic_set(&adapter->tx_fifo_stall, 1);
3176                 return 1;
3177         }
3178
3179 no_fifo_stall_required:
3180         adapter->tx_fifo_head += skb_fifo_len;
3181         if (adapter->tx_fifo_head >= adapter->tx_fifo_size)
3182                 adapter->tx_fifo_head -= adapter->tx_fifo_size;
3183         return 0;
3184 }
3185
3186 #define MINIMUM_DHCP_PACKET_SIZE 282
3187 static int e1000_transfer_dhcp_info(struct e1000_adapter *adapter,
3188                                     struct sk_buff *skb)
3189 {
3190         struct e1000_hw *hw =  &adapter->hw;
3191         u16 length, offset;
3192         if (vlan_tx_tag_present(skb)) {
3193                 if (!((vlan_tx_tag_get(skb) == hw->mng_cookie.vlan_id) &&
3194                         ( hw->mng_cookie.status &
3195                           E1000_MNG_DHCP_COOKIE_STATUS_VLAN_SUPPORT)) )
3196                         return 0;
3197         }
3198         if (skb->len > MINIMUM_DHCP_PACKET_SIZE) {
3199                 struct ethhdr *eth = (struct ethhdr *)skb->data;
3200                 if ((htons(ETH_P_IP) == eth->h_proto)) {
3201                         const struct iphdr *ip =
3202                                 (struct iphdr *)((u8 *)skb->data+14);
3203                         if (IPPROTO_UDP == ip->protocol) {
3204                                 struct udphdr *udp =
3205                                         (struct udphdr *)((u8 *)ip +
3206                                                 (ip->ihl << 2));
3207                                 if (ntohs(udp->dest) == 67) {
3208                                         offset = (u8 *)udp + 8 - skb->data;
3209                                         length = skb->len - offset;
3210
3211                                         return e1000_mng_write_dhcp_info(hw,
3212                                                         (u8 *)udp + 8,
3213                                                         length);
3214                                 }
3215                         }
3216                 }
3217         }
3218         return 0;
3219 }
3220
3221 static int __e1000_maybe_stop_tx(struct net_device *netdev, int size)
3222 {
3223         struct e1000_adapter *adapter = netdev_priv(netdev);
3224         struct e1000_tx_ring *tx_ring = adapter->tx_ring;
3225
3226         netif_stop_queue(netdev);
3227         /* Herbert's original patch had:
3228          *  smp_mb__after_netif_stop_queue();
3229          * but since that doesn't exist yet, just open code it. */
3230         smp_mb();
3231
3232         /* We need to check again in a case another CPU has just
3233          * made room available. */
3234         if (likely(E1000_DESC_UNUSED(tx_ring) < size))
3235                 return -EBUSY;
3236
3237         /* A reprieve! */
3238         netif_start_queue(netdev);
3239         ++adapter->restart_queue;
3240         return 0;
3241 }
3242
3243 static int e1000_maybe_stop_tx(struct net_device *netdev,
3244                                struct e1000_tx_ring *tx_ring, int size)
3245 {
3246         if (likely(E1000_DESC_UNUSED(tx_ring) >= size))
3247                 return 0;
3248         return __e1000_maybe_stop_tx(netdev, size);
3249 }
3250
3251 #define TXD_USE_COUNT(S, X) (((S) >> (X)) + 1 )
3252 static int e1000_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
3253 {
3254         struct e1000_adapter *adapter = netdev_priv(netdev);
3255         struct e1000_hw *hw = &adapter->hw;
3256         struct e1000_tx_ring *tx_ring;
3257         unsigned int first, max_per_txd = E1000_MAX_DATA_PER_TXD;
3258         unsigned int max_txd_pwr = E1000_MAX_TXD_PWR;
3259         unsigned int tx_flags = 0;
3260         unsigned int len = skb->len - skb->data_len;
3261         unsigned int nr_frags;
3262         unsigned int mss;
3263         int count = 0;
3264         int tso;
3265         unsigned int f;
3266
3267         /* This goes back to the question of how to logically map a tx queue
3268          * to a flow.  Right now, performance is impacted slightly negatively
3269          * if using multiple tx queues.  If the stack breaks away from a
3270          * single qdisc implementation, we can look at this again. */
3271         tx_ring = adapter->tx_ring;
3272
3273         if (unlikely(skb->len <= 0)) {
3274                 dev_kfree_skb_any(skb);
3275                 return NETDEV_TX_OK;
3276         }
3277
3278         /* 82571 and newer doesn't need the workaround that limited descriptor
3279          * length to 4kB */
3280         if (hw->mac_type >= e1000_82571)
3281                 max_per_txd = 8192;
3282
3283         mss = skb_shinfo(skb)->gso_size;
3284         /* The controller does a simple calculation to
3285          * make sure there is enough room in the FIFO before
3286          * initiating the DMA for each buffer.  The calc is:
3287          * 4 = ceil(buffer len/mss).  To make sure we don't
3288          * overrun the FIFO, adjust the max buffer len if mss
3289          * drops. */
3290         if (mss) {
3291                 u8 hdr_len;
3292                 max_per_txd = min(mss << 2, max_per_txd);
3293                 max_txd_pwr = fls(max_per_txd) - 1;
3294
3295                 /* TSO Workaround for 82571/2/3 Controllers -- if skb->data
3296                 * points to just header, pull a few bytes of payload from
3297                 * frags into skb->data */
3298                 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
3299                 if (skb->data_len && hdr_len == len) {
3300                         switch (hw->mac_type) {
3301                                 unsigned int pull_size;
3302                         case e1000_82544:
3303                                 /* Make sure we have room to chop off 4 bytes,
3304                                  * and that the end alignment will work out to
3305                                  * this hardware's requirements
3306                                  * NOTE: this is a TSO only workaround
3307                                  * if end byte alignment not correct move us
3308                                  * into the next dword */
3309                                 if ((unsigned long)(skb_tail_pointer(skb) - 1) & 4)
3310                                         break;
3311                                 /* fall through */
3312                         case e1000_82571:
3313                         case e1000_82572:
3314                         case e1000_82573:
3315                         case e1000_ich8lan:
3316                                 pull_size = min((unsigned int)4, skb->data_len);
3317                                 if (!__pskb_pull_tail(skb, pull_size)) {
3318                                         DPRINTK(DRV, ERR,
3319                                                 "__pskb_pull_tail failed.\n");
3320                                         dev_kfree_skb_any(skb);
3321                                         return NETDEV_TX_OK;
3322                                 }
3323                                 len = skb->len - skb->data_len;
3324                                 break;
3325                         default:
3326                                 /* do nothing */
3327                                 break;
3328                         }
3329                 }
3330         }
3331
3332         /* reserve a descriptor for the offload context */
3333         if ((mss) || (skb->ip_summed == CHECKSUM_PARTIAL))
3334                 count++;
3335         count++;
3336
3337         /* Controller Erratum workaround */
3338         if (!skb->data_len && tx_ring->last_tx_tso && !skb_is_gso(skb))
3339                 count++;
3340
3341         count += TXD_USE_COUNT(len, max_txd_pwr);
3342
3343         if (adapter->pcix_82544)
3344                 count++;
3345
3346         /* work-around for errata 10 and it applies to all controllers
3347          * in PCI-X mode, so add one more descriptor to the count
3348          */
3349         if (unlikely((hw->bus_type == e1000_bus_type_pcix) &&
3350                         (len > 2015)))
3351                 count++;
3352
3353         nr_frags = skb_shinfo(skb)->nr_frags;
3354         for (f = 0; f < nr_frags; f++)
3355                 count += TXD_USE_COUNT(skb_shinfo(skb)->frags[f].size,
3356                                        max_txd_pwr);
3357         if (adapter->pcix_82544)
3358                 count += nr_frags;
3359
3360
3361         if (hw->tx_pkt_filtering &&
3362             (hw->mac_type == e1000_82573))
3363                 e1000_transfer_dhcp_info(adapter, skb);
3364
3365         /* need: count + 2 desc gap to keep tail from touching
3366          * head, otherwise try next time */
3367         if (unlikely(e1000_maybe_stop_tx(netdev, tx_ring, count + 2)))
3368                 return NETDEV_TX_BUSY;
3369
3370         if (unlikely(hw->mac_type == e1000_82547)) {
3371                 if (unlikely(e1000_82547_fifo_workaround(adapter, skb))) {
3372                         netif_stop_queue(netdev);
3373                         mod_timer(&adapter->tx_fifo_stall_timer, jiffies + 1);
3374                         return NETDEV_TX_BUSY;
3375                 }
3376         }
3377
3378         if (unlikely(adapter->vlgrp && vlan_tx_tag_present(skb))) {
3379                 tx_flags |= E1000_TX_FLAGS_VLAN;
3380                 tx_flags |= (vlan_tx_tag_get(skb) << E1000_TX_FLAGS_VLAN_SHIFT);
3381         }
3382
3383         first = tx_ring->next_to_use;
3384
3385         tso = e1000_tso(adapter, tx_ring, skb);
3386         if (tso < 0) {
3387                 dev_kfree_skb_any(skb);
3388                 return NETDEV_TX_OK;
3389         }
3390
3391         if (likely(tso)) {
3392                 tx_ring->last_tx_tso = 1;
3393                 tx_flags |= E1000_TX_FLAGS_TSO;
3394         } else if (likely(e1000_tx_csum(adapter, tx_ring, skb)))
3395                 tx_flags |= E1000_TX_FLAGS_CSUM;
3396
3397         /* Old method was to assume IPv4 packet by default if TSO was enabled.
3398          * 82571 hardware supports TSO capabilities for IPv6 as well...
3399          * no longer assume, we must. */
3400         if (likely(skb->protocol == htons(ETH_P_IP)))
3401                 tx_flags |= E1000_TX_FLAGS_IPV4;
3402
3403         count = e1000_tx_map(adapter, tx_ring, skb, first, max_per_txd,
3404                              nr_frags, mss);
3405
3406         if (count) {
3407                 e1000_tx_queue(adapter, tx_ring, tx_flags, count);
3408                 /* Make sure there is space in the ring for the next send. */
3409                 e1000_maybe_stop_tx(netdev, tx_ring, MAX_SKB_FRAGS + 2);
3410
3411         } else {
3412                 dev_kfree_skb_any(skb);
3413                 tx_ring->buffer_info[first].time_stamp = 0;
3414                 tx_ring->next_to_use = first;
3415         }
3416
3417         return NETDEV_TX_OK;
3418 }
3419
3420 /**
3421  * e1000_tx_timeout - Respond to a Tx Hang
3422  * @netdev: network interface device structure
3423  **/
3424
3425 static void e1000_tx_timeout(struct net_device *netdev)
3426 {
3427         struct e1000_adapter *adapter = netdev_priv(netdev);
3428
3429         /* Do the reset outside of interrupt context */
3430         adapter->tx_timeout_count++;
3431         schedule_work(&adapter->reset_task);
3432 }
3433
3434 static void e1000_reset_task(struct work_struct *work)
3435 {
3436         struct e1000_adapter *adapter =
3437                 container_of(work, struct e1000_adapter, reset_task);
3438
3439         e1000_reinit_locked(adapter);
3440 }
3441
3442 /**
3443  * e1000_get_stats - Get System Network Statistics
3444  * @netdev: network interface device structure
3445  *
3446  * Returns the address of the device statistics structure.
3447  * The statistics are actually updated from the timer callback.
3448  **/
3449
3450 static struct net_device_stats *e1000_get_stats(struct net_device *netdev)
3451 {
3452         struct e1000_adapter *adapter = netdev_priv(netdev);
3453
3454         /* only return the current stats */
3455         return &adapter->net_stats;
3456 }
3457
3458 /**
3459  * e1000_change_mtu - Change the Maximum Transfer Unit
3460  * @netdev: network interface device structure
3461  * @new_mtu: new value for maximum frame size
3462  *
3463  * Returns 0 on success, negative on failure
3464  **/
3465
3466 static int e1000_change_mtu(struct net_device *netdev, int new_mtu)
3467 {
3468         struct e1000_adapter *adapter = netdev_priv(netdev);
3469         struct e1000_hw *hw = &adapter->hw;
3470         int max_frame = new_mtu + ENET_HEADER_SIZE + ETHERNET_FCS_SIZE;
3471         u16 eeprom_data = 0;
3472
3473         if ((max_frame < MINIMUM_ETHERNET_FRAME_SIZE) ||
3474             (max_frame > MAX_JUMBO_FRAME_SIZE)) {
3475                 DPRINTK(PROBE, ERR, "Invalid MTU setting\n");
3476                 return -EINVAL;
3477         }
3478
3479         /* Adapter-specific max frame size limits. */
3480         switch (hw->mac_type) {
3481         case e1000_undefined ... e1000_82542_rev2_1:
3482         case e1000_ich8lan:
3483                 if (max_frame > (ETH_FRAME_LEN + ETH_FCS_LEN)) {
3484                         DPRINTK(PROBE, ERR, "Jumbo Frames not supported.\n");
3485                         return -EINVAL;
3486                 }
3487                 break;
3488         case e1000_82573:
3489                 /* Jumbo Frames not supported if:
3490                  * - this is not an 82573L device
3491                  * - ASPM is enabled in any way (0x1A bits 3:2) */
3492                 e1000_read_eeprom(hw, EEPROM_INIT_3GIO_3, 1,
3493                                   &eeprom_data);
3494                 if ((hw->device_id != E1000_DEV_ID_82573L) ||
3495                     (eeprom_data & EEPROM_WORD1A_ASPM_MASK)) {
3496                         if (max_frame > (ETH_FRAME_LEN + ETH_FCS_LEN)) {
3497                                 DPRINTK(PROBE, ERR,
3498                                         "Jumbo Frames not supported.\n");
3499                                 return -EINVAL;
3500                         }
3501                         break;
3502                 }
3503                 /* ERT will be enabled later to enable wire speed receives */
3504
3505                 /* fall through to get support */
3506         case e1000_82571:
3507         case e1000_82572:
3508         case e1000_80003es2lan:
3509 #define MAX_STD_JUMBO_FRAME_SIZE 9234
3510                 if (max_frame > MAX_STD_JUMBO_FRAME_SIZE) {
3511                         DPRINTK(PROBE, ERR, "MTU > 9216 not supported.\n");
3512                         return -EINVAL;
3513                 }
3514                 break;
3515         default:
3516                 /* Capable of supporting up to MAX_JUMBO_FRAME_SIZE limit. */
3517                 break;
3518         }
3519
3520         /* NOTE: netdev_alloc_skb reserves 16 bytes, and typically NET_IP_ALIGN
3521          * means we reserve 2 more, this pushes us to allocate from the next
3522          * larger slab size.
3523          * i.e. RXBUFFER_2048 --> size-4096 slab
3524          *  however with the new *_jumbo_rx* routines, jumbo receives will use
3525          *  fragmented skbs */
3526
3527         if (max_frame <= E1000_RXBUFFER_256)
3528                 adapter->rx_buffer_len = E1000_RXBUFFER_256;
3529         else if (max_frame <= E1000_RXBUFFER_512)
3530                 adapter->rx_buffer_len = E1000_RXBUFFER_512;
3531         else if (max_frame <= E1000_RXBUFFER_1024)
3532                 adapter->rx_buffer_len = E1000_RXBUFFER_1024;
3533         else if (max_frame <= E1000_RXBUFFER_2048)
3534                 adapter->rx_buffer_len = E1000_RXBUFFER_2048;
3535         else
3536 #if (PAGE_SIZE >= E1000_RXBUFFER_16384)
3537                 adapter->rx_buffer_len = E1000_RXBUFFER_16384;
3538 #elif (PAGE_SIZE >= E1000_RXBUFFER_4096)
3539                 adapter->rx_buffer_len = PAGE_SIZE;
3540 #endif
3541
3542         /* adjust allocation if LPE protects us, and we aren't using SBP */
3543         if (!hw->tbi_compatibility_on &&
3544             ((max_frame == (ETH_FRAME_LEN + ETH_FCS_LEN)) ||
3545              (max_frame == MAXIMUM_ETHERNET_VLAN_SIZE)))
3546                 adapter->rx_buffer_len = MAXIMUM_ETHERNET_VLAN_SIZE;
3547
3548         netdev->mtu = new_mtu;
3549         hw->max_frame_size = max_frame;
3550
3551         if (netif_running(netdev))
3552                 e1000_reinit_locked(adapter);
3553
3554         return 0;
3555 }
3556
3557 /**
3558  * e1000_update_stats - Update the board statistics counters
3559  * @adapter: board private structure
3560  **/
3561
3562 void e1000_update_stats(struct e1000_adapter *adapter)
3563 {
3564         struct e1000_hw *hw = &adapter->hw;
3565         struct pci_dev *pdev = adapter->pdev;
3566         unsigned long flags;
3567         u16 phy_tmp;
3568
3569 #define PHY_IDLE_ERROR_COUNT_MASK 0x00FF
3570
3571         /*
3572          * Prevent stats update while adapter is being reset, or if the pci
3573          * connection is down.
3574          */
3575         if (adapter->link_speed == 0)
3576                 return;
3577         if (pci_channel_offline(pdev))
3578                 return;
3579
3580         spin_lock_irqsave(&adapter->stats_lock, flags);
3581
3582         /* these counters are modified from e1000_tbi_adjust_stats,
3583          * called from the interrupt context, so they must only
3584          * be written while holding adapter->stats_lock
3585          */
3586
3587         adapter->stats.crcerrs += er32(CRCERRS);
3588         adapter->stats.gprc += er32(GPRC);
3589         adapter->stats.gorcl += er32(GORCL);
3590         adapter->stats.gorch += er32(GORCH);
3591         adapter->stats.bprc += er32(BPRC);
3592         adapter->stats.mprc += er32(MPRC);
3593         adapter->stats.roc += er32(ROC);
3594
3595         if (hw->mac_type != e1000_ich8lan) {
3596                 adapter->stats.prc64 += er32(PRC64);
3597                 adapter->stats.prc127 += er32(PRC127);
3598                 adapter->stats.prc255 += er32(PRC255);
3599                 adapter->stats.prc511 += er32(PRC511);
3600                 adapter->stats.prc1023 += er32(PRC1023);
3601                 adapter->stats.prc1522 += er32(PRC1522);
3602         }
3603
3604         adapter->stats.symerrs += er32(SYMERRS);
3605         adapter->stats.mpc += er32(MPC);
3606         adapter->stats.scc += er32(SCC);
3607         adapter->stats.ecol += er32(ECOL);
3608         adapter->stats.mcc += er32(MCC);
3609         adapter->stats.latecol += er32(LATECOL);
3610         adapter->stats.dc += er32(DC);
3611         adapter->stats.sec += er32(SEC);
3612         adapter->stats.rlec += er32(RLEC);
3613         adapter->stats.xonrxc += er32(XONRXC);
3614         adapter->stats.xontxc += er32(XONTXC);
3615         adapter->stats.xoffrxc += er32(XOFFRXC);
3616         adapter->stats.xofftxc += er32(XOFFTXC);
3617         adapter->stats.fcruc += er32(FCRUC);
3618         adapter->stats.gptc += er32(GPTC);
3619         adapter->stats.gotcl += er32(GOTCL);
3620         adapter->stats.gotch += er32(GOTCH);
3621         adapter->stats.rnbc += er32(RNBC);
3622         adapter->stats.ruc += er32(RUC);
3623         adapter->stats.rfc += er32(RFC);
3624         adapter->stats.rjc += er32(RJC);
3625         adapter->stats.torl += er32(TORL);
3626         adapter->stats.torh += er32(TORH);
3627         adapter->stats.totl += er32(TOTL);
3628         adapter->stats.toth += er32(TOTH);
3629         adapter->stats.tpr += er32(TPR);
3630
3631         if (hw->mac_type != e1000_ich8lan) {
3632                 adapter->stats.ptc64 += er32(PTC64);
3633                 adapter->stats.ptc127 += er32(PTC127);
3634                 adapter->stats.ptc255 += er32(PTC255);
3635                 adapter->stats.ptc511 += er32(PTC511);
3636                 adapter->stats.ptc1023 += er32(PTC1023);
3637                 adapter->stats.ptc1522 += er32(PTC1522);
3638         }
3639
3640         adapter->stats.mptc += er32(MPTC);
3641         adapter->stats.bptc += er32(BPTC);
3642
3643         /* used for adaptive IFS */
3644
3645         hw->tx_packet_delta = er32(TPT);
3646         adapter->stats.tpt += hw->tx_packet_delta;
3647         hw->collision_delta = er32(COLC);
3648         adapter->stats.colc += hw->collision_delta;
3649
3650         if (hw->mac_type >= e1000_82543) {
3651                 adapter->stats.algnerrc += er32(ALGNERRC);
3652                 adapter->stats.rxerrc += er32(RXERRC);
3653                 adapter->stats.tncrs += er32(TNCRS);
3654                 adapter->stats.cexterr += er32(CEXTERR);
3655                 adapter->stats.tsctc += er32(TSCTC);
3656                 adapter->stats.tsctfc += er32(TSCTFC);
3657         }
3658         if (hw->mac_type > e1000_82547_rev_2) {
3659                 adapter->stats.iac += er32(IAC);
3660                 adapter->stats.icrxoc += er32(ICRXOC);
3661
3662                 if (hw->mac_type != e1000_ich8lan) {
3663                         adapter->stats.icrxptc += er32(ICRXPTC);
3664                         adapter->stats.icrxatc += er32(ICRXATC);
3665                         adapter->stats.ictxptc += er32(ICTXPTC);
3666                         adapter->stats.ictxatc += er32(ICTXATC);
3667                         adapter->stats.ictxqec += er32(ICTXQEC);
3668                         adapter->stats.ictxqmtc += er32(ICTXQMTC);
3669                         adapter->stats.icrxdmtc += er32(ICRXDMTC);
3670                 }
3671         }
3672
3673         /* Fill out the OS statistics structure */
3674         adapter->net_stats.multicast = adapter->stats.mprc;
3675         adapter->net_stats.collisions = adapter->stats.colc;
3676
3677         /* Rx Errors */
3678
3679         /* RLEC on some newer hardware can be incorrect so build
3680         * our own version based on RUC and ROC */
3681         adapter->net_stats.rx_errors = adapter->stats.rxerrc +
3682                 adapter->stats.crcerrs + adapter->stats.algnerrc +
3683                 adapter->stats.ruc + adapter->stats.roc +
3684                 adapter->stats.cexterr;
3685         adapter->stats.rlerrc = adapter->stats.ruc + adapter->stats.roc;
3686         adapter->net_stats.rx_length_errors = adapter->stats.rlerrc;
3687         adapter->net_stats.rx_crc_errors = adapter->stats.crcerrs;
3688         adapter->net_stats.rx_frame_errors = adapter->stats.algnerrc;
3689         adapter->net_stats.rx_missed_errors = adapter->stats.mpc;
3690
3691         /* Tx Errors */
3692         adapter->stats.txerrc = adapter->stats.ecol + adapter->stats.latecol;
3693         adapter->net_stats.tx_errors = adapter->stats.txerrc;
3694         adapter->net_stats.tx_aborted_errors = adapter->stats.ecol;
3695         adapter->net_stats.tx_window_errors = adapter->stats.latecol;
3696         adapter->net_stats.tx_carrier_errors = adapter->stats.tncrs;
3697         if (hw->bad_tx_carr_stats_fd &&
3698             adapter->link_duplex == FULL_DUPLEX) {
3699                 adapter->net_stats.tx_carrier_errors = 0;
3700                 adapter->stats.tncrs = 0;
3701         }
3702
3703         /* Tx Dropped needs to be maintained elsewhere */
3704
3705         /* Phy Stats */
3706         if (hw->media_type == e1000_media_type_copper) {
3707                 if ((adapter->link_speed == SPEED_1000) &&
3708                    (!e1000_read_phy_reg(hw, PHY_1000T_STATUS, &phy_tmp))) {
3709                         phy_tmp &= PHY_IDLE_ERROR_COUNT_MASK;
3710                         adapter->phy_stats.idle_errors += phy_tmp;
3711                 }
3712
3713                 if ((hw->mac_type <= e1000_82546) &&
3714                    (hw->phy_type == e1000_phy_m88) &&
3715                    !e1000_read_phy_reg(hw, M88E1000_RX_ERR_CNTR, &phy_tmp))
3716                         adapter->phy_stats.receive_errors += phy_tmp;
3717         }
3718
3719         /* Management Stats */
3720         if (hw->has_smbus) {
3721                 adapter->stats.mgptc += er32(MGTPTC);
3722                 adapter->stats.mgprc += er32(MGTPRC);
3723                 adapter->stats.mgpdc += er32(MGTPDC);
3724         }
3725
3726         spin_unlock_irqrestore(&adapter->stats_lock, flags);
3727 }
3728
3729 /**
3730  * e1000_intr_msi - Interrupt Handler
3731  * @irq: interrupt number
3732  * @data: pointer to a network interface device structure
3733  **/
3734
3735 static irqreturn_t e1000_intr_msi(int irq, void *data)
3736 {
3737         struct net_device *netdev = data;
3738         struct e1000_adapter *adapter = netdev_priv(netdev);
3739         struct e1000_hw *hw = &adapter->hw;
3740         u32 icr = er32(ICR);
3741
3742         /* in NAPI mode read ICR disables interrupts using IAM */
3743
3744         if (icr & (E1000_ICR_RXSEQ | E1000_ICR_LSC)) {
3745                 hw->get_link_status = 1;
3746                 /* 80003ES2LAN workaround-- For packet buffer work-around on
3747                  * link down event; disable receives here in the ISR and reset
3748                  * adapter in watchdog */
3749                 if (netif_carrier_ok(netdev) &&
3750                     (hw->mac_type == e1000_80003es2lan)) {
3751                         /* disable receives */
3752                         u32 rctl = er32(RCTL);
3753                         ew32(RCTL, rctl & ~E1000_RCTL_EN);
3754                 }
3755                 /* guard against interrupt when we're going down */
3756                 if (!test_bit(__E1000_DOWN, &adapter->flags))
3757                         mod_timer(&adapter->watchdog_timer, jiffies + 1);
3758         }
3759
3760         if (likely(napi_schedule_prep(&adapter->napi))) {
3761                 adapter->total_tx_bytes = 0;
3762                 adapter->total_tx_packets = 0;
3763                 adapter->total_rx_bytes = 0;
3764                 adapter->total_rx_packets = 0;
3765                 __napi_schedule(&adapter->napi);
3766         } else
3767                 e1000_irq_enable(adapter);
3768
3769         return IRQ_HANDLED;
3770 }
3771
3772 /**
3773  * e1000_intr - Interrupt Handler
3774  * @irq: interrupt number
3775  * @data: pointer to a network interface device structure
3776  **/
3777
3778 static irqreturn_t e1000_intr(int irq, void *data)
3779 {
3780         struct net_device *netdev = data;
3781         struct e1000_adapter *adapter = netdev_priv(netdev);
3782         struct e1000_hw *hw = &adapter->hw;
3783         u32 rctl, icr = er32(ICR);
3784
3785         if (unlikely((!icr) || test_bit(__E1000_DOWN, &adapter->flags)))
3786                 return IRQ_NONE;  /* Not our interrupt */
3787
3788         /* IMS will not auto-mask if INT_ASSERTED is not set, and if it is
3789          * not set, then the adapter didn't send an interrupt */
3790         if (unlikely(hw->mac_type >= e1000_82571 &&
3791                      !(icr & E1000_ICR_INT_ASSERTED)))
3792                 return IRQ_NONE;
3793
3794         /* Interrupt Auto-Mask...upon reading ICR, interrupts are masked.  No
3795          * need for the IMC write */
3796
3797         if (unlikely(icr & (E1000_ICR_RXSEQ | E1000_ICR_LSC))) {
3798                 hw->get_link_status = 1;
3799                 /* 80003ES2LAN workaround--
3800                  * For packet buffer work-around on link down event;
3801                  * disable receives here in the ISR and
3802                  * reset adapter in watchdog
3803                  */
3804                 if (netif_carrier_ok(netdev) &&
3805                     (hw->mac_type == e1000_80003es2lan)) {
3806                         /* disable receives */
3807                         rctl = er32(RCTL);
3808                         ew32(RCTL, rctl & ~E1000_RCTL_EN);
3809                 }
3810                 /* guard against interrupt when we're going down */
3811                 if (!test_bit(__E1000_DOWN, &adapter->flags))
3812                         mod_timer(&adapter->watchdog_timer, jiffies + 1);
3813         }
3814
3815         if (unlikely(hw->mac_type < e1000_82571)) {
3816                 /* disable interrupts, without the synchronize_irq bit */
3817                 ew32(IMC, ~0);
3818                 E1000_WRITE_FLUSH();
3819         }
3820         if (likely(napi_schedule_prep(&adapter->napi))) {
3821                 adapter->total_tx_bytes = 0;
3822                 adapter->total_tx_packets = 0;
3823                 adapter->total_rx_bytes = 0;
3824                 adapter->total_rx_packets = 0;
3825                 __napi_schedule(&adapter->napi);
3826         } else {
3827                 /* this really should not happen! if it does it is basically a
3828                  * bug, but not a hard error, so enable ints and continue */
3829                 if (!test_bit(__E1000_DOWN, &adapter->flags))
3830                         e1000_irq_enable(adapter);
3831         }
3832
3833         return IRQ_HANDLED;
3834 }
3835
3836 /**
3837  * e1000_clean - NAPI Rx polling callback
3838  * @adapter: board private structure
3839  **/
3840 static int e1000_clean(struct napi_struct *napi, int budget)
3841 {
3842         struct e1000_adapter *adapter = container_of(napi, struct e1000_adapter, napi);
3843         struct net_device *poll_dev = adapter->netdev;
3844         int tx_cleaned = 0, work_done = 0;
3845
3846         adapter = netdev_priv(poll_dev);
3847
3848         tx_cleaned = e1000_clean_tx_irq(adapter, &adapter->tx_ring[0]);
3849
3850         adapter->clean_rx(adapter, &adapter->rx_ring[0],
3851                           &work_done, budget);
3852
3853         if (!tx_cleaned)
3854                 work_done = budget;
3855
3856         /* If budget not fully consumed, exit the polling mode */
3857         if (work_done < budget) {
3858                 if (likely(adapter->itr_setting & 3))
3859                         e1000_set_itr(adapter);
3860                 napi_complete(napi);
3861                 if (!test_bit(__E1000_DOWN, &adapter->flags))
3862                         e1000_irq_enable(adapter);
3863         }
3864
3865         return work_done;
3866 }
3867
3868 /**
3869  * e1000_clean_tx_irq - Reclaim resources after transmit completes
3870  * @adapter: board private structure
3871  **/
3872 static bool e1000_clean_tx_irq(struct e1000_adapter *adapter,
3873                                struct e1000_tx_ring *tx_ring)
3874 {
3875         struct e1000_hw *hw = &adapter->hw;
3876         struct net_device *netdev = adapter->netdev;
3877         struct e1000_tx_desc *tx_desc, *eop_desc;
3878         struct e1000_buffer *buffer_info;
3879         unsigned int i, eop;
3880         unsigned int count = 0;
3881         unsigned int total_tx_bytes=0, total_tx_packets=0;
3882
3883         i = tx_ring->next_to_clean;
3884         eop = tx_ring->buffer_info[i].next_to_watch;
3885         eop_desc = E1000_TX_DESC(*tx_ring, eop);
3886
3887         while ((eop_desc->upper.data & cpu_to_le32(E1000_TXD_STAT_DD)) &&
3888                (count < tx_ring->count)) {
3889                 bool cleaned = false;
3890                 for ( ; !cleaned; count++) {
3891                         tx_desc = E1000_TX_DESC(*tx_ring, i);
3892                         buffer_info = &tx_ring->buffer_info[i];
3893                         cleaned = (i == eop);
3894
3895                         if (cleaned) {
3896                                 struct sk_buff *skb = buffer_info->skb;
3897                                 unsigned int segs, bytecount;
3898                                 segs = skb_shinfo(skb)->gso_segs ?: 1;
3899                                 /* multiply data chunks by size of headers */
3900                                 bytecount = ((segs - 1) * skb_headlen(skb)) +
3901                                             skb->len;
3902                                 total_tx_packets += segs;
3903                                 total_tx_bytes += bytecount;
3904                         }
3905                         e1000_unmap_and_free_tx_resource(adapter, buffer_info);
3906                         tx_desc->upper.data = 0;
3907
3908                         if (unlikely(++i == tx_ring->count)) i = 0;
3909                 }
3910
3911                 eop = tx_ring->buffer_info[i].next_to_watch;
3912                 eop_desc = E1000_TX_DESC(*tx_ring, eop);
3913         }
3914
3915         tx_ring->next_to_clean = i;
3916
3917 #define TX_WAKE_THRESHOLD 32
3918         if (unlikely(count && netif_carrier_ok(netdev) &&
3919                      E1000_DESC_UNUSED(tx_ring) >= TX_WAKE_THRESHOLD)) {
3920                 /* Make sure that anybody stopping the queue after this
3921                  * sees the new next_to_clean.
3922                  */
3923                 smp_mb();
3924                 if (netif_queue_stopped(netdev)) {
3925                         netif_wake_queue(netdev);
3926                         ++adapter->restart_queue;
3927                 }
3928         }
3929
3930         if (adapter->detect_tx_hung) {
3931                 /* Detect a transmit hang in hardware, this serializes the
3932                  * check with the clearing of time_stamp and movement of i */
3933                 adapter->detect_tx_hung = false;
3934                 if (tx_ring->buffer_info[i].time_stamp &&
3935                     time_after(jiffies, tx_ring->buffer_info[i].time_stamp +
3936                                (adapter->tx_timeout_factor * HZ))
3937                     && !(er32(STATUS) & E1000_STATUS_TXOFF)) {
3938
3939                         /* detected Tx unit hang */
3940                         DPRINTK(DRV, ERR, "Detected Tx Unit Hang\n"
3941                                         "  Tx Queue             <%lu>\n"
3942                                         "  TDH                  <%x>\n"
3943                                         "  TDT                  <%x>\n"
3944                                         "  next_to_use          <%x>\n"
3945                                         "  next_to_clean        <%x>\n"
3946                                         "buffer_info[next_to_clean]\n"
3947                                         "  time_stamp           <%lx>\n"
3948                                         "  next_to_watch        <%x>\n"
3949                                         "  jiffies              <%lx>\n"
3950                                         "  next_to_watch.status <%x>\n",
3951                                 (unsigned long)((tx_ring - adapter->tx_ring) /
3952                                         sizeof(struct e1000_tx_ring)),
3953                                 readl(hw->hw_addr + tx_ring->tdh),
3954                                 readl(hw->hw_addr + tx_ring->tdt),
3955                                 tx_ring->next_to_use,
3956                                 tx_ring->next_to_clean,
3957                                 tx_ring->buffer_info[i].time_stamp,
3958                                 eop,
3959                                 jiffies,
3960                                 eop_desc->upper.fields.status);
3961                         netif_stop_queue(netdev);
3962                 }
3963         }
3964         adapter->total_tx_bytes += total_tx_bytes;
3965         adapter->total_tx_packets += total_tx_packets;
3966         adapter->net_stats.tx_bytes += total_tx_bytes;
3967         adapter->net_stats.tx_packets += total_tx_packets;
3968         return (count < tx_ring->count);
3969 }
3970
3971 /**
3972  * e1000_rx_checksum - Receive Checksum Offload for 82543
3973  * @adapter:     board private structure
3974  * @status_err:  receive descriptor status and error fields
3975  * @csum:        receive descriptor csum field
3976  * @sk_buff:     socket buffer with received data
3977  **/
3978
3979 static void e1000_rx_checksum(struct e1000_adapter *adapter, u32 status_err,
3980                               u32 csum, struct sk_buff *skb)
3981 {
3982         struct e1000_hw *hw = &adapter->hw;
3983         u16 status = (u16)status_err;
3984         u8 errors = (u8)(status_err >> 24);
3985         skb->ip_summed = CHECKSUM_NONE;
3986
3987         /* 82543 or newer only */
3988         if (unlikely(hw->mac_type < e1000_82543)) return;
3989         /* Ignore Checksum bit is set */
3990         if (unlikely(status & E1000_RXD_STAT_IXSM)) return;
3991         /* TCP/UDP checksum error bit is set */
3992         if (unlikely(errors & E1000_RXD_ERR_TCPE)) {
3993                 /* let the stack verify checksum errors */
3994                 adapter->hw_csum_err++;
3995                 return;
3996         }
3997         /* TCP/UDP Checksum has not been calculated */
3998         if (hw->mac_type <= e1000_82547_rev_2) {
3999                 if (!(status & E1000_RXD_STAT_TCPCS))
4000                         return;
4001         } else {
4002                 if (!(status & (E1000_RXD_STAT_TCPCS | E1000_RXD_STAT_UDPCS)))
4003                         return;
4004         }
4005         /* It must be a TCP or UDP packet with a valid checksum */
4006         if (likely(status & E1000_RXD_STAT_TCPCS)) {
4007                 /* TCP checksum is good */
4008                 skb->ip_summed = CHECKSUM_UNNECESSARY;
4009         } else if (hw->mac_type > e1000_82547_rev_2) {
4010                 /* IP fragment with UDP payload */
4011                 /* Hardware complements the payload checksum, so we undo it
4012                  * and then put the value in host order for further stack use.
4013                  */
4014                 __sum16 sum = (__force __sum16)htons(csum);
4015                 skb->csum = csum_unfold(~sum);
4016                 skb->ip_summed = CHECKSUM_COMPLETE;
4017         }
4018         adapter->hw_csum_good++;
4019 }
4020
4021 /**
4022  * e1000_consume_page - helper function
4023  **/
4024 static void e1000_consume_page(struct e1000_buffer *bi, struct sk_buff *skb,
4025                                u16 length)
4026 {
4027         bi->page = NULL;
4028         skb->len += length;
4029         skb->data_len += length;
4030         skb->truesize += length;
4031 }
4032
4033 /**
4034  * e1000_receive_skb - helper function to handle rx indications
4035  * @adapter: board private structure
4036  * @status: descriptor status field as written by hardware
4037  * @vlan: descriptor vlan field as written by hardware (no le/be conversion)
4038  * @skb: pointer to sk_buff to be indicated to stack
4039  */
4040 static void e1000_receive_skb(struct e1000_adapter *adapter, u8 status,
4041                               __le16 vlan, struct sk_buff *skb)
4042 {
4043         if (unlikely(adapter->vlgrp && (status & E1000_RXD_STAT_VP))) {
4044                 vlan_hwaccel_receive_skb(skb, adapter->vlgrp,
4045                                          le16_to_cpu(vlan) &
4046                                          E1000_RXD_SPC_VLAN_MASK);
4047         } else {
4048                 netif_receive_skb(skb);
4049         }
4050 }
4051
4052 /**
4053  * e1000_clean_jumbo_rx_irq - Send received data up the network stack; legacy
4054  * @adapter: board private structure
4055  * @rx_ring: ring to clean
4056  * @work_done: amount of napi work completed this call
4057  * @work_to_do: max amount of work allowed for this call to do
4058  *
4059  * the return value indicates whether actual cleaning was done, there
4060  * is no guarantee that everything was cleaned
4061  */
4062 static bool e1000_clean_jumbo_rx_irq(struct e1000_adapter *adapter,
4063                                      struct e1000_rx_ring *rx_ring,
4064                                      int *work_done, int work_to_do)
4065 {
4066         struct e1000_hw *hw = &adapter->hw;
4067         struct net_device *netdev = adapter->netdev;
4068         struct pci_dev *pdev = adapter->pdev;
4069         struct e1000_rx_desc *rx_desc, *next_rxd;
4070         struct e1000_buffer *buffer_info, *next_buffer;
4071         unsigned long irq_flags;
4072         u32 length;
4073         unsigned int i;
4074         int cleaned_count = 0;
4075         bool cleaned = false;
4076         unsigned int total_rx_bytes=0, total_rx_packets=0;
4077
4078         i = rx_ring->next_to_clean;
4079         rx_desc = E1000_RX_DESC(*rx_ring, i);
4080         buffer_info = &rx_ring->buffer_info[i];
4081
4082         while (rx_desc->status & E1000_RXD_STAT_DD) {
4083                 struct sk_buff *skb;
4084                 u8 status;
4085
4086                 if (*work_done >= work_to_do)
4087                         break;
4088                 (*work_done)++;
4089
4090                 status = rx_desc->status;
4091                 skb = buffer_info->skb;
4092                 buffer_info->skb = NULL;
4093
4094                 if (++i == rx_ring->count) i = 0;
4095                 next_rxd = E1000_RX_DESC(*rx_ring, i);
4096                 prefetch(next_rxd);
4097
4098                 next_buffer = &rx_ring->buffer_info[i];
4099
4100                 cleaned = true;
4101                 cleaned_count++;
4102                 pci_unmap_page(pdev, buffer_info->dma, buffer_info->length,
4103                                PCI_DMA_FROMDEVICE);
4104                 buffer_info->dma = 0;
4105
4106                 length = le16_to_cpu(rx_desc->length);
4107
4108                 /* errors is only valid for DD + EOP descriptors */
4109                 if (unlikely((status & E1000_RXD_STAT_EOP) &&
4110                     (rx_desc->errors & E1000_RXD_ERR_FRAME_ERR_MASK))) {
4111                         u8 last_byte = *(skb->data + length - 1);
4112                         if (TBI_ACCEPT(hw, status, rx_desc->errors, length,
4113                                        last_byte)) {
4114                                 spin_lock_irqsave(&adapter->stats_lock,
4115                                                   irq_flags);
4116                                 e1000_tbi_adjust_stats(hw, &adapter->stats,
4117                                                        length, skb->data);
4118                                 spin_unlock_irqrestore(&adapter->stats_lock,
4119                                                        irq_flags);
4120                                 length--;
4121                         } else {
4122                                 /* recycle both page and skb */
4123                                 buffer_info->skb = skb;
4124                                 /* an error means any chain goes out the window
4125                                  * too */
4126                                 if (rx_ring->rx_skb_top)
4127                                         dev_kfree_skb(rx_ring->rx_skb_top);
4128                                 rx_ring->rx_skb_top = NULL;
4129                                 goto next_desc;
4130                         }
4131                 }
4132
4133 #define rxtop rx_ring->rx_skb_top
4134                 if (!(status & E1000_RXD_STAT_EOP)) {
4135                         /* this descriptor is only the beginning (or middle) */
4136                         if (!rxtop) {
4137                                 /* this is the beginning of a chain */
4138                                 rxtop = skb;
4139                                 skb_fill_page_desc(rxtop, 0, buffer_info->page,
4140                                                    0, length);
4141                         } else {
4142                                 /* this is the middle of a chain */
4143                                 skb_fill_page_desc(rxtop,
4144                                     skb_shinfo(rxtop)->nr_frags,
4145                                     buffer_info->page, 0, length);
4146                                 /* re-use the skb, only consumed the page */
4147                                 buffer_info->skb = skb;
4148                         }
4149                         e1000_consume_page(buffer_info, rxtop, length);
4150                         goto next_desc;
4151                 } else {
4152                         if (rxtop) {
4153                                 /* end of the chain */
4154                                 skb_fill_page_desc(rxtop,
4155                                     skb_shinfo(rxtop)->nr_frags,
4156                                     buffer_info->page, 0, length);
4157                                 /* re-use the current skb, we only consumed the
4158                                  * page */
4159                                 buffer_info->skb = skb;
4160                                 skb = rxtop;
4161                                 rxtop = NULL;
4162                                 e1000_consume_page(buffer_info, skb, length);
4163                         } else {
4164                                 /* no chain, got EOP, this buf is the packet
4165                                  * copybreak to save the put_page/alloc_page */
4166                                 if (length <= copybreak &&
4167                                     skb_tailroom(skb) >= length) {
4168                                         u8 *vaddr;
4169                                         vaddr = kmap_atomic(buffer_info->page,
4170                                                             KM_SKB_DATA_SOFTIRQ);
4171                                         memcpy(skb_tail_pointer(skb), vaddr, length);
4172                                         kunmap_atomic(vaddr,
4173                                                       KM_SKB_DATA_SOFTIRQ);
4174                                         /* re-use the page, so don't erase
4175                                          * buffer_info->page */
4176                                         skb_put(skb, length);
4177                                 } else {
4178                                         skb_fill_page_desc(skb, 0,
4179                                                            buffer_info->page, 0,
4180                                                            length);
4181                                         e1000_consume_page(buffer_info, skb,
4182                                                            length);
4183                                 }
4184                         }
4185                 }
4186
4187                 /* Receive Checksum Offload XXX recompute due to CRC strip? */
4188                 e1000_rx_checksum(adapter,
4189                                   (u32)(status) |
4190                                   ((u32)(rx_desc->errors) << 24),
4191                                   le16_to_cpu(rx_desc->csum), skb);
4192
4193                 pskb_trim(skb, skb->len - 4);
4194
4195                 /* probably a little skewed due to removing CRC */
4196                 total_rx_bytes += skb->len;
4197                 total_rx_packets++;
4198
4199                 /* eth type trans needs skb->data to point to something */
4200                 if (!pskb_may_pull(skb, ETH_HLEN)) {
4201                         DPRINTK(DRV, ERR, "pskb_may_pull failed.\n");
4202                         dev_kfree_skb(skb);
4203                         goto next_desc;
4204                 }
4205
4206                 skb->protocol = eth_type_trans(skb, netdev);
4207
4208                 e1000_receive_skb(adapter, status, rx_desc->special, skb);
4209
4210 next_desc:
4211                 rx_desc->status = 0;
4212
4213                 /* return some buffers to hardware, one at a time is too slow */
4214                 if (unlikely(cleaned_count >= E1000_RX_BUFFER_WRITE)) {
4215                         adapter->alloc_rx_buf(adapter, rx_ring, cleaned_count);
4216                         cleaned_count = 0;
4217                 }
4218
4219                 /* use prefetched values */
4220                 rx_desc = next_rxd;
4221                 buffer_info = next_buffer;
4222         }
4223         rx_ring->next_to_clean = i;
4224
4225         cleaned_count = E1000_DESC_UNUSED(rx_ring);
4226         if (cleaned_count)
4227                 adapter->alloc_rx_buf(adapter, rx_ring, cleaned_count);
4228
4229         adapter->total_rx_packets += total_rx_packets;
4230         adapter->total_rx_bytes += total_rx_bytes;
4231         adapter->net_stats.rx_bytes += total_rx_bytes;
4232         adapter->net_stats.rx_packets += total_rx_packets;
4233         return cleaned;
4234 }
4235
4236 /**
4237  * e1000_clean_rx_irq - Send received data up the network stack; legacy
4238  * @adapter: board private structure
4239  * @rx_ring: ring to clean
4240  * @work_done: amount of napi work completed this call
4241  * @work_to_do: max amount of work allowed for this call to do
4242  */
4243 static bool e1000_clean_rx_irq(struct e1000_adapter *adapter,
4244                                struct e1000_rx_ring *rx_ring,
4245                                int *work_done, int work_to_do)
4246 {
4247         struct e1000_hw *hw = &adapter->hw;
4248         struct net_device *netdev = adapter->netdev;
4249         struct pci_dev *pdev = adapter->pdev;
4250         struct e1000_rx_desc *rx_desc, *next_rxd;
4251         struct e1000_buffer *buffer_info, *next_buffer;
4252         unsigned long flags;
4253         u32 length;
4254         unsigned int i;
4255         int cleaned_count = 0;
4256         bool cleaned = false;
4257         unsigned int total_rx_bytes=0, total_rx_packets=0;
4258
4259         i = rx_ring->next_to_clean;
4260         rx_desc = E1000_RX_DESC(*rx_ring, i);
4261         buffer_info = &rx_ring->buffer_info[i];
4262
4263         while (rx_desc->status & E1000_RXD_STAT_DD) {
4264                 struct sk_buff *skb;
4265                 u8 status;
4266
4267                 if (*work_done >= work_to_do)
4268                         break;
4269                 (*work_done)++;
4270
4271                 status = rx_desc->status;
4272                 skb = buffer_info->skb;
4273                 buffer_info->skb = NULL;
4274
4275                 prefetch(skb->data - NET_IP_ALIGN);
4276
4277                 if (++i == rx_ring->count) i = 0;
4278                 next_rxd = E1000_RX_DESC(*rx_ring, i);
4279                 prefetch(next_rxd);
4280
4281                 next_buffer = &rx_ring->buffer_info[i];
4282
4283                 cleaned = true;
4284                 cleaned_count++;
4285                 pci_unmap_single(pdev, buffer_info->dma, buffer_info->length,
4286                                  PCI_DMA_FROMDEVICE);
4287                 buffer_info->dma = 0;
4288
4289                 length = le16_to_cpu(rx_desc->length);
4290                 /* !EOP means multiple descriptors were used to store a single
4291                  * packet, also make sure the frame isn't just CRC only */
4292                 if (unlikely(!(status & E1000_RXD_STAT_EOP) || (length <= 4))) {
4293                         /* All receives must fit into a single buffer */
4294                         E1000_DBG("%s: Receive packet consumed multiple"
4295                                   " buffers\n", netdev->name);
4296                         /* recycle */
4297                         buffer_info->skb = skb;
4298                         goto next_desc;
4299                 }
4300
4301                 if (unlikely(rx_desc->errors & E1000_RXD_ERR_FRAME_ERR_MASK)) {
4302                         u8 last_byte = *(skb->data + length - 1);
4303                         if (TBI_ACCEPT(hw, status, rx_desc->errors, length,
4304                                        last_byte)) {
4305                                 spin_lock_irqsave(&adapter->stats_lock, flags);
4306                                 e1000_tbi_adjust_stats(hw, &adapter->stats,
4307                                                        length, skb->data);
4308                                 spin_unlock_irqrestore(&adapter->stats_lock,
4309                                                        flags);
4310                                 length--;
4311                         } else {
4312                                 /* recycle */
4313                                 buffer_info->skb = skb;
4314                                 goto next_desc;
4315                         }
4316                 }
4317
4318                 /* adjust length to remove Ethernet CRC, this must be
4319                  * done after the TBI_ACCEPT workaround above */
4320                 length -= 4;
4321
4322                 /* probably a little skewed due to removing CRC */
4323                 total_rx_bytes += length;
4324                 total_rx_packets++;
4325
4326                 /* code added for copybreak, this should improve
4327                  * performance for small packets with large amounts
4328                  * of reassembly being done in the stack */
4329                 if (length < copybreak) {
4330                         struct sk_buff *new_skb =
4331                             netdev_alloc_skb(netdev, length + NET_IP_ALIGN);
4332                         if (new_skb) {
4333                                 skb_reserve(new_skb, NET_IP_ALIGN);
4334                                 skb_copy_to_linear_data_offset(new_skb,
4335                                                                -NET_IP_ALIGN,
4336                                                                (skb->data -
4337                                                                 NET_IP_ALIGN),
4338                                                                (length +
4339                                                                 NET_IP_ALIGN));
4340                                 /* save the skb in buffer_info as good */
4341                                 buffer_info->skb = skb;
4342                                 skb = new_skb;
4343                         }
4344                         /* else just continue with the old one */
4345                 }
4346                 /* end copybreak code */
4347                 skb_put(skb, length);
4348
4349                 /* Receive Checksum Offload */
4350                 e1000_rx_checksum(adapter,
4351                                   (u32)(status) |
4352                                   ((u32)(rx_desc->errors) << 24),
4353                                   le16_to_cpu(rx_desc->csum), skb);
4354
4355                 skb->protocol = eth_type_trans(skb, netdev);
4356
4357                 e1000_receive_skb(adapter, status, rx_desc->special, skb);
4358
4359 next_desc:
4360                 rx_desc->status = 0;
4361
4362                 /* return some buffers to hardware, one at a time is too slow */
4363                 if (unlikely(cleaned_count >= E1000_RX_BUFFER_WRITE)) {
4364                         adapter->alloc_rx_buf(adapter, rx_ring, cleaned_count);
4365                         cleaned_count = 0;
4366                 }
4367
4368                 /* use prefetched values */
4369                 rx_desc = next_rxd;
4370                 buffer_info = next_buffer;
4371         }
4372         rx_ring->next_to_clean = i;
4373
4374         cleaned_count = E1000_DESC_UNUSED(rx_ring);
4375         if (cleaned_count)
4376                 adapter->alloc_rx_buf(adapter, rx_ring, cleaned_count);
4377
4378         adapter->total_rx_packets += total_rx_packets;
4379         adapter->total_rx_bytes += total_rx_bytes;
4380         adapter->net_stats.rx_bytes += total_rx_bytes;
4381         adapter->net_stats.rx_packets += total_rx_packets;
4382         return cleaned;
4383 }
4384
4385 /**
4386  * e1000_alloc_jumbo_rx_buffers - Replace used jumbo receive buffers
4387  * @adapter: address of board private structure
4388  * @rx_ring: pointer to receive ring structure
4389  * @cleaned_count: number of buffers to allocate this pass
4390  **/
4391
4392 static void
4393 e1000_alloc_jumbo_rx_buffers(struct e1000_adapter *adapter,
4394                              struct e1000_rx_ring *rx_ring, int cleaned_count)
4395 {
4396         struct net_device *netdev = adapter->netdev;
4397         struct pci_dev *pdev = adapter->pdev;
4398         struct e1000_rx_desc *rx_desc;
4399         struct e1000_buffer *buffer_info;
4400         struct sk_buff *skb;
4401         unsigned int i;
4402         unsigned int bufsz = 256 -
4403                              16 /*for skb_reserve */ -
4404                              NET_IP_ALIGN;
4405
4406         i = rx_ring->next_to_use;
4407         buffer_info = &rx_ring->buffer_info[i];
4408
4409         while (cleaned_count--) {
4410                 skb = buffer_info->skb;
4411                 if (skb) {
4412                         skb_trim(skb, 0);
4413                         goto check_page;
4414                 }
4415
4416                 skb = netdev_alloc_skb(netdev, bufsz);
4417                 if (unlikely(!skb)) {
4418                         /* Better luck next round */
4419                         adapter->alloc_rx_buff_failed++;
4420                         break;
4421                 }
4422
4423                 /* Fix for errata 23, can't cross 64kB boundary */
4424                 if (!e1000_check_64k_bound(adapter, skb->data, bufsz)) {
4425                         struct sk_buff *oldskb = skb;
4426                         DPRINTK(PROBE, ERR, "skb align check failed: %u bytes "
4427                                              "at %p\n", bufsz, skb->data);
4428                         /* Try again, without freeing the previous */
4429                         skb = netdev_alloc_skb(netdev, bufsz);
4430                         /* Failed allocation, critical failure */
4431                         if (!skb) {
4432                                 dev_kfree_skb(oldskb);
4433                                 adapter->alloc_rx_buff_failed++;
4434                                 break;
4435                         }
4436
4437                         if (!e1000_check_64k_bound(adapter, skb->data, bufsz)) {
4438                                 /* give up */
4439                                 dev_kfree_skb(skb);
4440                                 dev_kfree_skb(oldskb);
4441                                 break; /* while (cleaned_count--) */
4442                         }
4443
4444                         /* Use new allocation */
4445                         dev_kfree_skb(oldskb);
4446                 }
4447                 /* Make buffer alignment 2 beyond a 16 byte boundary
4448                  * this will result in a 16 byte aligned IP header after
4449                  * the 14 byte MAC header is removed
4450                  */
4451                 skb_reserve(skb, NET_IP_ALIGN);
4452
4453                 buffer_info->skb = skb;
4454                 buffer_info->length = adapter->rx_buffer_len;
4455 check_page:
4456                 /* allocate a new page if necessary */
4457                 if (!buffer_info->page) {
4458                         buffer_info->page = alloc_page(GFP_ATOMIC);
4459                         if (unlikely(!buffer_info->page)) {
4460                                 adapter->alloc_rx_buff_failed++;
4461                                 break;
4462                         }
4463                 }
4464
4465                 if (!buffer_info->dma)
4466                         buffer_info->dma = pci_map_page(pdev,
4467                                                         buffer_info->page, 0,
4468                                                         buffer_info->length,
4469                                                         PCI_DMA_FROMDEVICE);
4470
4471                 rx_desc = E1000_RX_DESC(*rx_ring, i);
4472                 rx_desc->buffer_addr = cpu_to_le64(buffer_info->dma);
4473
4474                 if (unlikely(++i == rx_ring->count))
4475                         i = 0;
4476                 buffer_info = &rx_ring->buffer_info[i];
4477         }
4478
4479         if (likely(rx_ring->next_to_use != i)) {
4480                 rx_ring->next_to_use = i;
4481                 if (unlikely(i-- == 0))
4482                         i = (rx_ring->count - 1);
4483
4484                 /* Force memory writes to complete before letting h/w
4485                  * know there are new descriptors to fetch.  (Only
4486                  * applicable for weak-ordered memory model archs,
4487                  * such as IA-64). */
4488                 wmb();
4489                 writel(i, adapter->hw.hw_addr + rx_ring->rdt);
4490         }
4491 }
4492
4493 /**
4494  * e1000_alloc_rx_buffers - Replace used receive buffers; legacy & extended
4495  * @adapter: address of board private structure
4496  **/
4497
4498 static void e1000_alloc_rx_buffers(struct e1000_adapter *adapter,
4499                                    struct e1000_rx_ring *rx_ring,
4500                                    int cleaned_count)
4501 {
4502         struct e1000_hw *hw = &adapter->hw;
4503         struct net_device *netdev = adapter->netdev;
4504         struct pci_dev *pdev = adapter->pdev;
4505         struct e1000_rx_desc *rx_desc;
4506         struct e1000_buffer *buffer_info;
4507         struct sk_buff *skb;
4508         unsigned int i;
4509         unsigned int bufsz = adapter->rx_buffer_len + NET_IP_ALIGN;
4510
4511         i = rx_ring->next_to_use;
4512         buffer_info = &rx_ring->buffer_info[i];
4513
4514         while (cleaned_count--) {
4515                 skb = buffer_info->skb;
4516                 if (skb) {
4517                         skb_trim(skb, 0);
4518                         goto map_skb;
4519                 }
4520
4521                 skb = netdev_alloc_skb(netdev, bufsz);
4522                 if (unlikely(!skb)) {
4523                         /* Better luck next round */
4524                         adapter->alloc_rx_buff_failed++;
4525                         break;
4526                 }
4527
4528                 /* Fix for errata 23, can't cross 64kB boundary */
4529                 if (!e1000_check_64k_bound(adapter, skb->data, bufsz)) {
4530                         struct sk_buff *oldskb = skb;
4531                         DPRINTK(RX_ERR, ERR, "skb align check failed: %u bytes "
4532                                              "at %p\n", bufsz, skb->data);
4533                         /* Try again, without freeing the previous */
4534                         skb = netdev_alloc_skb(netdev, bufsz);
4535                         /* Failed allocation, critical failure */
4536                         if (!skb) {
4537                                 dev_kfree_skb(oldskb);
4538                                 adapter->alloc_rx_buff_failed++;
4539                                 break;
4540                         }
4541
4542                         if (!e1000_check_64k_bound(adapter, skb->data, bufsz)) {
4543                                 /* give up */
4544                                 dev_kfree_skb(skb);
4545                                 dev_kfree_skb(oldskb);
4546                                 adapter->alloc_rx_buff_failed++;
4547                                 break; /* while !buffer_info->skb */
4548                         }
4549
4550                         /* Use new allocation */
4551                         dev_kfree_skb(oldskb);
4552                 }
4553                 /* Make buffer alignment 2 beyond a 16 byte boundary
4554                  * this will result in a 16 byte aligned IP header after
4555                  * the 14 byte MAC header is removed
4556                  */
4557                 skb_reserve(skb, NET_IP_ALIGN);
4558
4559                 buffer_info->skb = skb;
4560                 buffer_info->length = adapter->rx_buffer_len;
4561 map_skb:
4562                 buffer_info->dma = pci_map_single(pdev,
4563                                                   skb->data,
4564                                                   buffer_info->length,
4565                                                   PCI_DMA_FROMDEVICE);
4566
4567                 /*
4568                  * XXX if it was allocated cleanly it will never map to a
4569                  * boundary crossing
4570                  */
4571
4572                 /* Fix for errata 23, can't cross 64kB boundary */
4573                 if (!e1000_check_64k_bound(adapter,
4574                                         (void *)(unsigned long)buffer_info->dma,
4575                                         adapter->rx_buffer_len)) {
4576                         DPRINTK(RX_ERR, ERR,
4577                                 "dma align check failed: %u bytes at %p\n",
4578                                 adapter->rx_buffer_len,
4579                                 (void *)(unsigned long)buffer_info->dma);
4580                         dev_kfree_skb(skb);
4581                         buffer_info->skb = NULL;
4582
4583                         pci_unmap_single(pdev, buffer_info->dma,
4584                                          adapter->rx_buffer_len,
4585                                          PCI_DMA_FROMDEVICE);
4586                         buffer_info->dma = 0;
4587
4588                         adapter->alloc_rx_buff_failed++;
4589                         break; /* while !buffer_info->skb */
4590                 }
4591                 rx_desc = E1000_RX_DESC(*rx_ring, i);
4592                 rx_desc->buffer_addr = cpu_to_le64(buffer_info->dma);
4593
4594                 if (unlikely(++i == rx_ring->count))
4595                         i = 0;
4596                 buffer_info = &rx_ring->buffer_info[i];
4597         }
4598
4599         if (likely(rx_ring->next_to_use != i)) {
4600                 rx_ring->next_to_use = i;
4601                 if (unlikely(i-- == 0))
4602                         i = (rx_ring->count - 1);
4603
4604                 /* Force memory writes to complete before letting h/w
4605                  * know there are new descriptors to fetch.  (Only
4606                  * applicable for weak-ordered memory model archs,
4607                  * such as IA-64). */
4608                 wmb();
4609                 writel(i, hw->hw_addr + rx_ring->rdt);
4610         }
4611 }
4612
4613 /**
4614  * e1000_smartspeed - Workaround for SmartSpeed on 82541 and 82547 controllers.
4615  * @adapter:
4616  **/
4617
4618 static void e1000_smartspeed(struct e1000_adapter *adapter)
4619 {
4620         struct e1000_hw *hw = &adapter->hw;
4621         u16 phy_status;
4622         u16 phy_ctrl;
4623
4624         if ((hw->phy_type != e1000_phy_igp) || !hw->autoneg ||
4625            !(hw->autoneg_advertised & ADVERTISE_1000_FULL))
4626                 return;
4627
4628         if (adapter->smartspeed == 0) {
4629                 /* If Master/Slave config fault is asserted twice,
4630                  * we assume back-to-back */
4631                 e1000_read_phy_reg(hw, PHY_1000T_STATUS, &phy_status);
4632                 if (!(phy_status & SR_1000T_MS_CONFIG_FAULT)) return;
4633                 e1000_read_phy_reg(hw, PHY_1000T_STATUS, &phy_status);
4634                 if (!(phy_status & SR_1000T_MS_CONFIG_FAULT)) return;
4635                 e1000_read_phy_reg(hw, PHY_1000T_CTRL, &phy_ctrl);
4636                 if (phy_ctrl & CR_1000T_MS_ENABLE) {
4637                         phy_ctrl &= ~CR_1000T_MS_ENABLE;
4638                         e1000_write_phy_reg(hw, PHY_1000T_CTRL,
4639                                             phy_ctrl);
4640                         adapter->smartspeed++;
4641                         if (!e1000_phy_setup_autoneg(hw) &&
4642                            !e1000_read_phy_reg(hw, PHY_CTRL,
4643                                                &phy_ctrl)) {
4644                                 phy_ctrl |= (MII_CR_AUTO_NEG_EN |
4645                                              MII_CR_RESTART_AUTO_NEG);
4646                                 e1000_write_phy_reg(hw, PHY_CTRL,
4647                                                     phy_ctrl);
4648                         }
4649                 }
4650                 return;
4651         } else if (adapter->smartspeed == E1000_SMARTSPEED_DOWNSHIFT) {
4652                 /* If still no link, perhaps using 2/3 pair cable */
4653                 e1000_read_phy_reg(hw, PHY_1000T_CTRL, &phy_ctrl);
4654                 phy_ctrl |= CR_1000T_MS_ENABLE;
4655                 e1000_write_phy_reg(hw, PHY_1000T_CTRL, phy_ctrl);
4656                 if (!e1000_phy_setup_autoneg(hw) &&
4657                    !e1000_read_phy_reg(hw, PHY_CTRL, &phy_ctrl)) {
4658                         phy_ctrl |= (MII_CR_AUTO_NEG_EN |
4659                                      MII_CR_RESTART_AUTO_NEG);
4660                         e1000_write_phy_reg(hw, PHY_CTRL, phy_ctrl);
4661                 }
4662         }
4663         /* Restart process after E1000_SMARTSPEED_MAX iterations */
4664         if (adapter->smartspeed++ == E1000_SMARTSPEED_MAX)
4665                 adapter->smartspeed = 0;
4666 }
4667
4668 /**
4669  * e1000_ioctl -
4670  * @netdev:
4671  * @ifreq:
4672  * @cmd:
4673  **/
4674
4675 static int e1000_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
4676 {
4677         switch (cmd) {
4678         case SIOCGMIIPHY:
4679         case SIOCGMIIREG:
4680         case SIOCSMIIREG:
4681                 return e1000_mii_ioctl(netdev, ifr, cmd);
4682         default:
4683                 return -EOPNOTSUPP;
4684         }
4685 }
4686
4687 /**
4688  * e1000_mii_ioctl -
4689  * @netdev:
4690  * @ifreq:
4691  * @cmd:
4692  **/
4693
4694 static int e1000_mii_ioctl(struct net_device *netdev, struct ifreq *ifr,
4695                            int cmd)
4696 {
4697         struct e1000_adapter *adapter = netdev_priv(netdev);
4698         struct e1000_hw *hw = &adapter->hw;
4699         struct mii_ioctl_data *data = if_mii(ifr);
4700         int retval;
4701         u16 mii_reg;
4702         u16 spddplx;
4703         unsigned long flags;
4704
4705         if (hw->media_type != e1000_media_type_copper)
4706                 return -EOPNOTSUPP;
4707
4708         switch (cmd) {
4709         case SIOCGMIIPHY:
4710                 data->phy_id = hw->phy_addr;
4711                 break;
4712         case SIOCGMIIREG:
4713                 if (!capable(CAP_NET_ADMIN))
4714                         return -EPERM;
4715                 spin_lock_irqsave(&adapter->stats_lock, flags);
4716                 if (e1000_read_phy_reg(hw, data->reg_num & 0x1F,
4717                                    &data->val_out)) {
4718                         spin_unlock_irqrestore(&adapter->stats_lock, flags);
4719                         return -EIO;
4720                 }
4721                 spin_unlock_irqrestore(&adapter->stats_lock, flags);
4722                 break;
4723         case SIOCSMIIREG:
4724                 if (!capable(CAP_NET_ADMIN))
4725                         return -EPERM;
4726                 if (data->reg_num & ~(0x1F))
4727                         return -EFAULT;
4728                 mii_reg = data->val_in;
4729                 spin_lock_irqsave(&adapter->stats_lock, flags);
4730                 if (e1000_write_phy_reg(hw, data->reg_num,
4731                                         mii_reg)) {
4732                         spin_unlock_irqrestore(&adapter->stats_lock, flags);
4733                         return -EIO;
4734                 }
4735                 spin_unlock_irqrestore(&adapter->stats_lock, flags);
4736                 if (hw->media_type == e1000_media_type_copper) {
4737                         switch (data->reg_num) {
4738                         case PHY_CTRL:
4739                                 if (mii_reg & MII_CR_POWER_DOWN)
4740                                         break;
4741                                 if (mii_reg & MII_CR_AUTO_NEG_EN) {
4742                                         hw->autoneg = 1;
4743                                         hw->autoneg_advertised = 0x2F;
4744                                 } else {
4745                                         if (mii_reg & 0x40)
4746                                                 spddplx = SPEED_1000;
4747                                         else if (mii_reg & 0x2000)
4748                                                 spddplx = SPEED_100;
4749                                         else
4750                                                 spddplx = SPEED_10;
4751                                         spddplx += (mii_reg & 0x100)
4752                                                    ? DUPLEX_FULL :
4753                                                    DUPLEX_HALF;
4754                                         retval = e1000_set_spd_dplx(adapter,
4755                                                                     spddplx);
4756                                         if (retval)
4757                                                 return retval;
4758                                 }
4759                                 if (netif_running(adapter->netdev))
4760                                         e1000_reinit_locked(adapter);
4761                                 else
4762                                         e1000_reset(adapter);
4763                                 break;
4764                         case M88E1000_PHY_SPEC_CTRL:
4765                         case M88E1000_EXT_PHY_SPEC_CTRL:
4766                                 if (e1000_phy_reset(hw))
4767                                         return -EIO;
4768                                 break;
4769                         }
4770                 } else {
4771                         switch (data->reg_num) {
4772                         case PHY_CTRL:
4773                                 if (mii_reg & MII_CR_POWER_DOWN)
4774                                         break;
4775                                 if (netif_running(adapter->netdev))
4776                                         e1000_reinit_locked(adapter);
4777                                 else
4778                                         e1000_reset(adapter);
4779                                 break;
4780                         }
4781                 }
4782                 break;
4783         default:
4784                 return -EOPNOTSUPP;
4785         }
4786         return E1000_SUCCESS;
4787 }
4788
4789 void e1000_pci_set_mwi(struct e1000_hw *hw)
4790 {
4791         struct e1000_adapter *adapter = hw->back;
4792         int ret_val = pci_set_mwi(adapter->pdev);
4793
4794         if (ret_val)
4795                 DPRINTK(PROBE, ERR, "Error in setting MWI\n");
4796 }
4797
4798 void e1000_pci_clear_mwi(struct e1000_hw *hw)
4799 {
4800         struct e1000_adapter *adapter = hw->back;
4801
4802         pci_clear_mwi(adapter->pdev);
4803 }
4804
4805 int e1000_pcix_get_mmrbc(struct e1000_hw *hw)
4806 {
4807         struct e1000_adapter *adapter = hw->back;
4808         return pcix_get_mmrbc(adapter->pdev);
4809 }
4810
4811 void e1000_pcix_set_mmrbc(struct e1000_hw *hw, int mmrbc)
4812 {
4813         struct e1000_adapter *adapter = hw->back;
4814         pcix_set_mmrbc(adapter->pdev, mmrbc);
4815 }
4816
4817 s32 e1000_read_pcie_cap_reg(struct e1000_hw *hw, u32 reg, u16 *value)
4818 {
4819     struct e1000_adapter *adapter = hw->back;
4820     u16 cap_offset;
4821
4822     cap_offset = pci_find_capability(adapter->pdev, PCI_CAP_ID_EXP);
4823     if (!cap_offset)
4824         return -E1000_ERR_CONFIG;
4825
4826     pci_read_config_word(adapter->pdev, cap_offset + reg, value);
4827
4828     return E1000_SUCCESS;
4829 }
4830
4831 void e1000_io_write(struct e1000_hw *hw, unsigned long port, u32 value)
4832 {
4833         outl(value, port);
4834 }
4835
4836 static void e1000_vlan_rx_register(struct net_device *netdev,
4837                                    struct vlan_group *grp)
4838 {
4839         struct e1000_adapter *adapter = netdev_priv(netdev);
4840         struct e1000_hw *hw = &adapter->hw;
4841         u32 ctrl, rctl;
4842
4843         if (!test_bit(__E1000_DOWN, &adapter->flags))
4844                 e1000_irq_disable(adapter);
4845         adapter->vlgrp = grp;
4846
4847         if (grp) {
4848                 /* enable VLAN tag insert/strip */
4849                 ctrl = er32(CTRL);
4850                 ctrl |= E1000_CTRL_VME;
4851                 ew32(CTRL, ctrl);
4852
4853                 if (adapter->hw.mac_type != e1000_ich8lan) {
4854                         /* enable VLAN receive filtering */
4855                         rctl = er32(RCTL);
4856                         rctl &= ~E1000_RCTL_CFIEN;
4857                         ew32(RCTL, rctl);
4858                         e1000_update_mng_vlan(adapter);
4859                 }
4860         } else {
4861                 /* disable VLAN tag insert/strip */
4862                 ctrl = er32(CTRL);
4863                 ctrl &= ~E1000_CTRL_VME;
4864                 ew32(CTRL, ctrl);
4865
4866                 if (adapter->hw.mac_type != e1000_ich8lan) {
4867                         if (adapter->mng_vlan_id !=
4868                             (u16)E1000_MNG_VLAN_NONE) {
4869                                 e1000_vlan_rx_kill_vid(netdev,
4870                                                        adapter->mng_vlan_id);
4871                                 adapter->mng_vlan_id = E1000_MNG_VLAN_NONE;
4872                         }
4873                 }
4874         }
4875
4876         if (!test_bit(__E1000_DOWN, &adapter->flags))
4877                 e1000_irq_enable(adapter);
4878 }
4879
4880 static void e1000_vlan_rx_add_vid(struct net_device *netdev, u16 vid)
4881 {
4882         struct e1000_adapter *adapter = netdev_priv(netdev);
4883         struct e1000_hw *hw = &adapter->hw;
4884         u32 vfta, index;
4885
4886         if ((hw->mng_cookie.status &
4887              E1000_MNG_DHCP_COOKIE_STATUS_VLAN_SUPPORT) &&
4888             (vid == adapter->mng_vlan_id))
4889                 return;
4890         /* add VID to filter table */
4891         index = (vid >> 5) & 0x7F;
4892         vfta = E1000_READ_REG_ARRAY(hw, VFTA, index);
4893         vfta |= (1 << (vid & 0x1F));
4894         e1000_write_vfta(hw, index, vfta);
4895 }
4896
4897 static void e1000_vlan_rx_kill_vid(struct net_device *netdev, u16 vid)
4898 {
4899         struct e1000_adapter *adapter = netdev_priv(netdev);
4900         struct e1000_hw *hw = &adapter->hw;
4901         u32 vfta, index;
4902
4903         if (!test_bit(__E1000_DOWN, &adapter->flags))
4904                 e1000_irq_disable(adapter);
4905         vlan_group_set_device(adapter->vlgrp, vid, NULL);
4906         if (!test_bit(__E1000_DOWN, &adapter->flags))
4907                 e1000_irq_enable(adapter);
4908
4909         if ((hw->mng_cookie.status &
4910              E1000_MNG_DHCP_COOKIE_STATUS_VLAN_SUPPORT) &&
4911             (vid == adapter->mng_vlan_id)) {
4912                 /* release control to f/w */
4913                 e1000_release_hw_control(adapter);
4914                 return;
4915         }
4916
4917         /* remove VID from filter table */
4918         index = (vid >> 5) & 0x7F;
4919         vfta = E1000_READ_REG_ARRAY(hw, VFTA, index);
4920         vfta &= ~(1 << (vid & 0x1F));
4921         e1000_write_vfta(hw, index, vfta);
4922 }
4923
4924 static void e1000_restore_vlan(struct e1000_adapter *adapter)
4925 {
4926         e1000_vlan_rx_register(adapter->netdev, adapter->vlgrp);
4927
4928         if (adapter->vlgrp) {
4929                 u16 vid;
4930                 for (vid = 0; vid < VLAN_GROUP_ARRAY_LEN; vid++) {
4931                         if (!vlan_group_get_device(adapter->vlgrp, vid))
4932                                 continue;
4933                         e1000_vlan_rx_add_vid(adapter->netdev, vid);
4934                 }
4935         }
4936 }
4937
4938 int e1000_set_spd_dplx(struct e1000_adapter *adapter, u16 spddplx)
4939 {
4940         struct e1000_hw *hw = &adapter->hw;
4941
4942         hw->autoneg = 0;
4943
4944         /* Fiber NICs only allow 1000 gbps Full duplex */
4945         if ((hw->media_type == e1000_media_type_fiber) &&
4946                 spddplx != (SPEED_1000 + DUPLEX_FULL)) {
4947                 DPRINTK(PROBE, ERR, "Unsupported Speed/Duplex configuration\n");
4948                 return -EINVAL;
4949         }
4950
4951         switch (spddplx) {
4952         case SPEED_10 + DUPLEX_HALF:
4953                 hw->forced_speed_duplex = e1000_10_half;
4954                 break;
4955         case SPEED_10 + DUPLEX_FULL:
4956                 hw->forced_speed_duplex = e1000_10_full;
4957                 break;
4958         case SPEED_100 + DUPLEX_HALF:
4959                 hw->forced_speed_duplex = e1000_100_half;
4960                 break;
4961         case SPEED_100 + DUPLEX_FULL:
4962                 hw->forced_speed_duplex = e1000_100_full;
4963                 break;
4964         case SPEED_1000 + DUPLEX_FULL:
4965                 hw->autoneg = 1;
4966                 hw->autoneg_advertised = ADVERTISE_1000_FULL;
4967                 break;
4968         case SPEED_1000 + DUPLEX_HALF: /* not supported */
4969         default:
4970                 DPRINTK(PROBE, ERR, "Unsupported Speed/Duplex configuration\n");
4971                 return -EINVAL;
4972         }
4973         return 0;
4974 }
4975
4976 static int __e1000_shutdown(struct pci_dev *pdev, bool *enable_wake)
4977 {
4978         struct net_device *netdev = pci_get_drvdata(pdev);
4979         struct e1000_adapter *adapter = netdev_priv(netdev);
4980         struct e1000_hw *hw = &adapter->hw;
4981         u32 ctrl, ctrl_ext, rctl, status;
4982         u32 wufc = adapter->wol;
4983 #ifdef CONFIG_PM
4984         int retval = 0;
4985 #endif
4986
4987         netif_device_detach(netdev);
4988
4989         if (netif_running(netdev)) {
4990                 WARN_ON(test_bit(__E1000_RESETTING, &adapter->flags));
4991                 e1000_down(adapter);
4992         }
4993
4994 #ifdef CONFIG_PM
4995         retval = pci_save_state(pdev);
4996         if (retval)
4997                 return retval;
4998 #endif
4999
5000         status = er32(STATUS);
5001         if (status & E1000_STATUS_LU)
5002                 wufc &= ~E1000_WUFC_LNKC;
5003
5004         if (wufc) {
5005                 e1000_setup_rctl(adapter);
5006                 e1000_set_rx_mode(netdev);
5007
5008                 /* turn on all-multi mode if wake on multicast is enabled */
5009                 if (wufc & E1000_WUFC_MC) {
5010                         rctl = er32(RCTL);
5011                         rctl |= E1000_RCTL_MPE;
5012                         ew32(RCTL, rctl);
5013                 }
5014
5015                 if (hw->mac_type >= e1000_82540) {
5016                         ctrl = er32(CTRL);
5017                         /* advertise wake from D3Cold */
5018                         #define E1000_CTRL_ADVD3WUC 0x00100000
5019                         /* phy power management enable */
5020                         #define E1000_CTRL_EN_PHY_PWR_MGMT 0x00200000
5021                         ctrl |= E1000_CTRL_ADVD3WUC |
5022                                 E1000_CTRL_EN_PHY_PWR_MGMT;
5023                         ew32(CTRL, ctrl);
5024                 }
5025
5026                 if (hw->media_type == e1000_media_type_fiber ||
5027                    hw->media_type == e1000_media_type_internal_serdes) {
5028                         /* keep the laser running in D3 */
5029                         ctrl_ext = er32(CTRL_EXT);
5030                         ctrl_ext |= E1000_CTRL_EXT_SDP7_DATA;
5031                         ew32(CTRL_EXT, ctrl_ext);
5032                 }
5033
5034                 /* Allow time for pending master requests to run */
5035                 e1000_disable_pciex_master(hw);
5036
5037                 ew32(WUC, E1000_WUC_PME_EN);
5038                 ew32(WUFC, wufc);
5039         } else {
5040                 ew32(WUC, 0);
5041                 ew32(WUFC, 0);
5042         }
5043
5044         e1000_release_manageability(adapter);
5045
5046         *enable_wake = !!wufc;
5047
5048         /* make sure adapter isn't asleep if manageability is enabled */
5049         if (adapter->en_mng_pt)
5050                 *enable_wake = true;
5051
5052         if (hw->phy_type == e1000_phy_igp_3)
5053                 e1000_phy_powerdown_workaround(hw);
5054
5055         if (netif_running(netdev))
5056                 e1000_free_irq(adapter);
5057
5058         /* Release control of h/w to f/w.  If f/w is AMT enabled, this
5059          * would have already happened in close and is redundant. */
5060         e1000_release_hw_control(adapter);
5061
5062         pci_disable_device(pdev);
5063
5064         return 0;
5065 }
5066
5067 #ifdef CONFIG_PM
5068 static int e1000_suspend(struct pci_dev *pdev, pm_message_t state)
5069 {
5070         int retval;
5071         bool wake;
5072
5073         retval = __e1000_shutdown(pdev, &wake);
5074         if (retval)
5075                 return retval;
5076
5077         if (wake) {
5078                 pci_prepare_to_sleep(pdev);
5079         } else {
5080                 pci_wake_from_d3(pdev, false);
5081                 pci_set_power_state(pdev, PCI_D3hot);
5082         }
5083
5084         return 0;
5085 }
5086
5087 static int e1000_resume(struct pci_dev *pdev)
5088 {
5089         struct net_device *netdev = pci_get_drvdata(pdev);
5090         struct e1000_adapter *adapter = netdev_priv(netdev);
5091         struct e1000_hw *hw = &adapter->hw;
5092         u32 err;
5093
5094         pci_set_power_state(pdev, PCI_D0);
5095         pci_restore_state(pdev);
5096
5097         if (adapter->need_ioport)
5098                 err = pci_enable_device(pdev);
5099         else
5100                 err = pci_enable_device_mem(pdev);
5101         if (err) {
5102                 printk(KERN_ERR "e1000: Cannot enable PCI device from suspend\n");
5103                 return err;
5104         }
5105         pci_set_master(pdev);
5106
5107         pci_enable_wake(pdev, PCI_D3hot, 0);
5108         pci_enable_wake(pdev, PCI_D3cold, 0);
5109
5110         if (netif_running(netdev)) {
5111                 err = e1000_request_irq(adapter);
5112                 if (err)
5113                         return err;
5114         }
5115
5116         e1000_power_up_phy(adapter);
5117         e1000_reset(adapter);
5118         ew32(WUS, ~0);
5119
5120         e1000_init_manageability(adapter);
5121
5122         if (netif_running(netdev))
5123                 e1000_up(adapter);
5124
5125         netif_device_attach(netdev);
5126
5127         /* If the controller is 82573 and f/w is AMT, do not set
5128          * DRV_LOAD until the interface is up.  For all other cases,
5129          * let the f/w know that the h/w is now under the control
5130          * of the driver. */
5131         if (hw->mac_type != e1000_82573 ||
5132             !e1000_check_mng_mode(hw))
5133                 e1000_get_hw_control(adapter);
5134
5135         return 0;
5136 }
5137 #endif
5138
5139 static void e1000_shutdown(struct pci_dev *pdev)
5140 {
5141         bool wake;
5142
5143         __e1000_shutdown(pdev, &wake);
5144
5145         if (system_state == SYSTEM_POWER_OFF) {
5146                 pci_wake_from_d3(pdev, wake);
5147                 pci_set_power_state(pdev, PCI_D3hot);
5148         }
5149 }
5150
5151 #ifdef CONFIG_NET_POLL_CONTROLLER
5152 /*
5153  * Polling 'interrupt' - used by things like netconsole to send skbs
5154  * without having to re-enable interrupts. It's not called while
5155  * the interrupt routine is executing.
5156  */
5157 static void e1000_netpoll(struct net_device *netdev)
5158 {
5159         struct e1000_adapter *adapter = netdev_priv(netdev);
5160
5161         disable_irq(adapter->pdev->irq);
5162         e1000_intr(adapter->pdev->irq, netdev);
5163         enable_irq(adapter->pdev->irq);
5164 }
5165 #endif
5166
5167 /**
5168  * e1000_io_error_detected - called when PCI error is detected
5169  * @pdev: Pointer to PCI device
5170  * @state: The current pci conneection state
5171  *
5172  * This function is called after a PCI bus error affecting
5173  * this device has been detected.
5174  */
5175 static pci_ers_result_t e1000_io_error_detected(struct pci_dev *pdev,
5176                                                 pci_channel_state_t state)
5177 {
5178         struct net_device *netdev = pci_get_drvdata(pdev);
5179         struct e1000_adapter *adapter = netdev_priv(netdev);
5180
5181         netif_device_detach(netdev);
5182
5183         if (state == pci_channel_io_perm_failure)
5184                 return PCI_ERS_RESULT_DISCONNECT;
5185
5186         if (netif_running(netdev))
5187                 e1000_down(adapter);
5188         pci_disable_device(pdev);
5189
5190         /* Request a slot slot reset. */
5191         return PCI_ERS_RESULT_NEED_RESET;
5192 }
5193
5194 /**
5195  * e1000_io_slot_reset - called after the pci bus has been reset.
5196  * @pdev: Pointer to PCI device
5197  *
5198  * Restart the card from scratch, as if from a cold-boot. Implementation
5199  * resembles the first-half of the e1000_resume routine.
5200  */
5201 static pci_ers_result_t e1000_io_slot_reset(struct pci_dev *pdev)
5202 {
5203         struct net_device *netdev = pci_get_drvdata(pdev);
5204         struct e1000_adapter *adapter = netdev_priv(netdev);
5205         struct e1000_hw *hw = &adapter->hw;
5206         int err;
5207
5208         if (adapter->need_ioport)
5209                 err = pci_enable_device(pdev);
5210         else
5211                 err = pci_enable_device_mem(pdev);
5212         if (err) {
5213                 printk(KERN_ERR "e1000: Cannot re-enable PCI device after reset.\n");
5214                 return PCI_ERS_RESULT_DISCONNECT;
5215         }
5216         pci_set_master(pdev);
5217
5218         pci_enable_wake(pdev, PCI_D3hot, 0);
5219         pci_enable_wake(pdev, PCI_D3cold, 0);
5220
5221         e1000_reset(adapter);
5222         ew32(WUS, ~0);
5223
5224         return PCI_ERS_RESULT_RECOVERED;
5225 }
5226
5227 /**
5228  * e1000_io_resume - called when traffic can start flowing again.
5229  * @pdev: Pointer to PCI device
5230  *
5231  * This callback is called when the error recovery driver tells us that
5232  * its OK to resume normal operation. Implementation resembles the
5233  * second-half of the e1000_resume routine.
5234  */
5235 static void e1000_io_resume(struct pci_dev *pdev)
5236 {
5237         struct net_device *netdev = pci_get_drvdata(pdev);
5238         struct e1000_adapter *adapter = netdev_priv(netdev);
5239         struct e1000_hw *hw = &adapter->hw;
5240
5241         e1000_init_manageability(adapter);
5242
5243         if (netif_running(netdev)) {
5244                 if (e1000_up(adapter)) {
5245                         printk("e1000: can't bring device back up after reset\n");
5246                         return;
5247                 }
5248         }
5249
5250         netif_device_attach(netdev);
5251
5252         /* If the controller is 82573 and f/w is AMT, do not set
5253          * DRV_LOAD until the interface is up.  For all other cases,
5254          * let the f/w know that the h/w is now under the control
5255          * of the driver. */
5256         if (hw->mac_type != e1000_82573 ||
5257             !e1000_check_mng_mode(hw))
5258                 e1000_get_hw_control(adapter);
5259
5260 }
5261
5262 /* e1000_main.c */