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