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