e1000: gather hardware bit tweaks.
[linux-2.6/mini2440.git] / drivers / net / e1000 / e1000_main.c
blobaaadb2bb07639cf74d594047416ceaa4ff9be439
1 /*******************************************************************************
3 Intel PRO/1000 Linux driver
4 Copyright(c) 1999 - 2006 Intel Corporation.
6 This program is free software; you can redistribute it and/or modify it
7 under the terms and conditions of the GNU General Public License,
8 version 2, as published by the Free Software Foundation.
10 This program is distributed in the hope it will be useful, but WITHOUT
11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 more details.
15 You should have received a copy of the GNU General Public License along with
16 this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
19 The full GNU General Public License is included in this distribution in
20 the file called "COPYING".
22 Contact Information:
23 Linux NICS <linux.nics@intel.com>
24 e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
25 Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27 *******************************************************************************/
29 #include "e1000.h"
31 char e1000_driver_name[] = "e1000";
32 static char e1000_driver_string[] = "Intel(R) PRO/1000 Network Driver";
33 #ifndef CONFIG_E1000_NAPI
34 #define DRIVERNAPI
35 #else
36 #define DRIVERNAPI "-NAPI"
37 #endif
38 #define DRV_VERSION "7.2.7-k2"DRIVERNAPI
39 char e1000_driver_version[] = DRV_VERSION;
40 static char e1000_copyright[] = "Copyright (c) 1999-2006 Intel Corporation.";
42 /* e1000_pci_tbl - PCI Device ID Table
44 * Last entry must be all 0s
46 * Macro expands to...
47 * {PCI_DEVICE(PCI_VENDOR_ID_INTEL, device_id)}
49 static struct pci_device_id e1000_pci_tbl[] = {
50 INTEL_E1000_ETHERNET_DEVICE(0x1000),
51 INTEL_E1000_ETHERNET_DEVICE(0x1001),
52 INTEL_E1000_ETHERNET_DEVICE(0x1004),
53 INTEL_E1000_ETHERNET_DEVICE(0x1008),
54 INTEL_E1000_ETHERNET_DEVICE(0x1009),
55 INTEL_E1000_ETHERNET_DEVICE(0x100C),
56 INTEL_E1000_ETHERNET_DEVICE(0x100D),
57 INTEL_E1000_ETHERNET_DEVICE(0x100E),
58 INTEL_E1000_ETHERNET_DEVICE(0x100F),
59 INTEL_E1000_ETHERNET_DEVICE(0x1010),
60 INTEL_E1000_ETHERNET_DEVICE(0x1011),
61 INTEL_E1000_ETHERNET_DEVICE(0x1012),
62 INTEL_E1000_ETHERNET_DEVICE(0x1013),
63 INTEL_E1000_ETHERNET_DEVICE(0x1014),
64 INTEL_E1000_ETHERNET_DEVICE(0x1015),
65 INTEL_E1000_ETHERNET_DEVICE(0x1016),
66 INTEL_E1000_ETHERNET_DEVICE(0x1017),
67 INTEL_E1000_ETHERNET_DEVICE(0x1018),
68 INTEL_E1000_ETHERNET_DEVICE(0x1019),
69 INTEL_E1000_ETHERNET_DEVICE(0x101A),
70 INTEL_E1000_ETHERNET_DEVICE(0x101D),
71 INTEL_E1000_ETHERNET_DEVICE(0x101E),
72 INTEL_E1000_ETHERNET_DEVICE(0x1026),
73 INTEL_E1000_ETHERNET_DEVICE(0x1027),
74 INTEL_E1000_ETHERNET_DEVICE(0x1028),
75 INTEL_E1000_ETHERNET_DEVICE(0x1049),
76 INTEL_E1000_ETHERNET_DEVICE(0x104A),
77 INTEL_E1000_ETHERNET_DEVICE(0x104B),
78 INTEL_E1000_ETHERNET_DEVICE(0x104C),
79 INTEL_E1000_ETHERNET_DEVICE(0x104D),
80 INTEL_E1000_ETHERNET_DEVICE(0x105E),
81 INTEL_E1000_ETHERNET_DEVICE(0x105F),
82 INTEL_E1000_ETHERNET_DEVICE(0x1060),
83 INTEL_E1000_ETHERNET_DEVICE(0x1075),
84 INTEL_E1000_ETHERNET_DEVICE(0x1076),
85 INTEL_E1000_ETHERNET_DEVICE(0x1077),
86 INTEL_E1000_ETHERNET_DEVICE(0x1078),
87 INTEL_E1000_ETHERNET_DEVICE(0x1079),
88 INTEL_E1000_ETHERNET_DEVICE(0x107A),
89 INTEL_E1000_ETHERNET_DEVICE(0x107B),
90 INTEL_E1000_ETHERNET_DEVICE(0x107C),
91 INTEL_E1000_ETHERNET_DEVICE(0x107D),
92 INTEL_E1000_ETHERNET_DEVICE(0x107E),
93 INTEL_E1000_ETHERNET_DEVICE(0x107F),
94 INTEL_E1000_ETHERNET_DEVICE(0x108A),
95 INTEL_E1000_ETHERNET_DEVICE(0x108B),
96 INTEL_E1000_ETHERNET_DEVICE(0x108C),
97 INTEL_E1000_ETHERNET_DEVICE(0x1096),
98 INTEL_E1000_ETHERNET_DEVICE(0x1098),
99 INTEL_E1000_ETHERNET_DEVICE(0x1099),
100 INTEL_E1000_ETHERNET_DEVICE(0x109A),
101 INTEL_E1000_ETHERNET_DEVICE(0x10A4),
102 INTEL_E1000_ETHERNET_DEVICE(0x10B5),
103 INTEL_E1000_ETHERNET_DEVICE(0x10B9),
104 INTEL_E1000_ETHERNET_DEVICE(0x10BA),
105 INTEL_E1000_ETHERNET_DEVICE(0x10BB),
106 /* required last entry */
107 {0,}
110 MODULE_DEVICE_TABLE(pci, e1000_pci_tbl);
112 int e1000_up(struct e1000_adapter *adapter);
113 void e1000_down(struct e1000_adapter *adapter);
114 void e1000_reinit_locked(struct e1000_adapter *adapter);
115 void e1000_reset(struct e1000_adapter *adapter);
116 int e1000_set_spd_dplx(struct e1000_adapter *adapter, uint16_t spddplx);
117 int e1000_setup_all_tx_resources(struct e1000_adapter *adapter);
118 int e1000_setup_all_rx_resources(struct e1000_adapter *adapter);
119 void e1000_free_all_tx_resources(struct e1000_adapter *adapter);
120 void e1000_free_all_rx_resources(struct e1000_adapter *adapter);
121 static int e1000_setup_tx_resources(struct e1000_adapter *adapter,
122 struct e1000_tx_ring *txdr);
123 static int e1000_setup_rx_resources(struct e1000_adapter *adapter,
124 struct e1000_rx_ring *rxdr);
125 static void e1000_free_tx_resources(struct e1000_adapter *adapter,
126 struct e1000_tx_ring *tx_ring);
127 static void e1000_free_rx_resources(struct e1000_adapter *adapter,
128 struct e1000_rx_ring *rx_ring);
129 void e1000_update_stats(struct e1000_adapter *adapter);
131 static int e1000_init_module(void);
132 static void e1000_exit_module(void);
133 static int e1000_probe(struct pci_dev *pdev, const struct pci_device_id *ent);
134 static void __devexit e1000_remove(struct pci_dev *pdev);
135 static int e1000_alloc_queues(struct e1000_adapter *adapter);
136 static int e1000_sw_init(struct e1000_adapter *adapter);
137 static int e1000_open(struct net_device *netdev);
138 static int e1000_close(struct net_device *netdev);
139 static void e1000_configure_tx(struct e1000_adapter *adapter);
140 static void e1000_configure_rx(struct e1000_adapter *adapter);
141 static void e1000_setup_rctl(struct e1000_adapter *adapter);
142 static void e1000_clean_all_tx_rings(struct e1000_adapter *adapter);
143 static void e1000_clean_all_rx_rings(struct e1000_adapter *adapter);
144 static void e1000_clean_tx_ring(struct e1000_adapter *adapter,
145 struct e1000_tx_ring *tx_ring);
146 static void e1000_clean_rx_ring(struct e1000_adapter *adapter,
147 struct e1000_rx_ring *rx_ring);
148 static void e1000_set_multi(struct net_device *netdev);
149 static void e1000_update_phy_info(unsigned long data);
150 static void e1000_watchdog(unsigned long data);
151 static void e1000_82547_tx_fifo_stall(unsigned long data);
152 static int e1000_xmit_frame(struct sk_buff *skb, struct net_device *netdev);
153 static struct net_device_stats * e1000_get_stats(struct net_device *netdev);
154 static int e1000_change_mtu(struct net_device *netdev, int new_mtu);
155 static int e1000_set_mac(struct net_device *netdev, void *p);
156 static irqreturn_t e1000_intr(int irq, void *data, struct pt_regs *regs);
157 static boolean_t e1000_clean_tx_irq(struct e1000_adapter *adapter,
158 struct e1000_tx_ring *tx_ring);
159 #ifdef CONFIG_E1000_NAPI
160 static int e1000_clean(struct net_device *poll_dev, int *budget);
161 static boolean_t e1000_clean_rx_irq(struct e1000_adapter *adapter,
162 struct e1000_rx_ring *rx_ring,
163 int *work_done, int work_to_do);
164 static boolean_t e1000_clean_rx_irq_ps(struct e1000_adapter *adapter,
165 struct e1000_rx_ring *rx_ring,
166 int *work_done, int work_to_do);
167 #else
168 static boolean_t e1000_clean_rx_irq(struct e1000_adapter *adapter,
169 struct e1000_rx_ring *rx_ring);
170 static boolean_t e1000_clean_rx_irq_ps(struct e1000_adapter *adapter,
171 struct e1000_rx_ring *rx_ring);
172 #endif
173 static void e1000_alloc_rx_buffers(struct e1000_adapter *adapter,
174 struct e1000_rx_ring *rx_ring,
175 int cleaned_count);
176 static void e1000_alloc_rx_buffers_ps(struct e1000_adapter *adapter,
177 struct e1000_rx_ring *rx_ring,
178 int cleaned_count);
179 static int e1000_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd);
180 static int e1000_mii_ioctl(struct net_device *netdev, struct ifreq *ifr,
181 int cmd);
182 void e1000_set_ethtool_ops(struct net_device *netdev);
183 static void e1000_enter_82542_rst(struct e1000_adapter *adapter);
184 static void e1000_leave_82542_rst(struct e1000_adapter *adapter);
185 static void e1000_tx_timeout(struct net_device *dev);
186 static void e1000_reset_task(struct net_device *dev);
187 static void e1000_smartspeed(struct e1000_adapter *adapter);
188 static int e1000_82547_fifo_workaround(struct e1000_adapter *adapter,
189 struct sk_buff *skb);
191 static void e1000_vlan_rx_register(struct net_device *netdev, struct vlan_group *grp);
192 static void e1000_vlan_rx_add_vid(struct net_device *netdev, uint16_t vid);
193 static void e1000_vlan_rx_kill_vid(struct net_device *netdev, uint16_t vid);
194 static void e1000_restore_vlan(struct e1000_adapter *adapter);
196 static int e1000_suspend(struct pci_dev *pdev, pm_message_t state);
197 #ifdef CONFIG_PM
198 static int e1000_resume(struct pci_dev *pdev);
199 #endif
200 static void e1000_shutdown(struct pci_dev *pdev);
202 #ifdef CONFIG_NET_POLL_CONTROLLER
203 /* for netdump / net console */
204 static void e1000_netpoll (struct net_device *netdev);
205 #endif
207 extern void e1000_check_options(struct e1000_adapter *adapter);
209 static pci_ers_result_t e1000_io_error_detected(struct pci_dev *pdev,
210 pci_channel_state_t state);
211 static pci_ers_result_t e1000_io_slot_reset(struct pci_dev *pdev);
212 static void e1000_io_resume(struct pci_dev *pdev);
214 static struct pci_error_handlers e1000_err_handler = {
215 .error_detected = e1000_io_error_detected,
216 .slot_reset = e1000_io_slot_reset,
217 .resume = e1000_io_resume,
220 static struct pci_driver e1000_driver = {
221 .name = e1000_driver_name,
222 .id_table = e1000_pci_tbl,
223 .probe = e1000_probe,
224 .remove = __devexit_p(e1000_remove),
225 #ifdef CONFIG_PM
226 /* Power Managment Hooks */
227 .suspend = e1000_suspend,
228 .resume = e1000_resume,
229 #endif
230 .shutdown = e1000_shutdown,
231 .err_handler = &e1000_err_handler
234 MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
235 MODULE_DESCRIPTION("Intel(R) PRO/1000 Network Driver");
236 MODULE_LICENSE("GPL");
237 MODULE_VERSION(DRV_VERSION);
239 static int debug = NETIF_MSG_DRV | NETIF_MSG_PROBE;
240 module_param(debug, int, 0);
241 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
244 * e1000_init_module - Driver Registration Routine
246 * e1000_init_module is the first routine called when the driver is
247 * loaded. All it does is register with the PCI subsystem.
250 static int __init
251 e1000_init_module(void)
253 int ret;
254 printk(KERN_INFO "%s - version %s\n",
255 e1000_driver_string, e1000_driver_version);
257 printk(KERN_INFO "%s\n", e1000_copyright);
259 ret = pci_register_driver(&e1000_driver);
261 return ret;
264 module_init(e1000_init_module);
267 * e1000_exit_module - Driver Exit Cleanup Routine
269 * e1000_exit_module is called just before the driver is removed
270 * from memory.
273 static void __exit
274 e1000_exit_module(void)
276 pci_unregister_driver(&e1000_driver);
279 module_exit(e1000_exit_module);
281 static int e1000_request_irq(struct e1000_adapter *adapter)
283 struct net_device *netdev = adapter->netdev;
284 int flags, err = 0;
286 flags = IRQF_SHARED;
287 #ifdef CONFIG_PCI_MSI
288 if (adapter->hw.mac_type > e1000_82547_rev_2) {
289 adapter->have_msi = TRUE;
290 if ((err = pci_enable_msi(adapter->pdev))) {
291 DPRINTK(PROBE, ERR,
292 "Unable to allocate MSI interrupt Error: %d\n", err);
293 adapter->have_msi = FALSE;
296 if (adapter->have_msi)
297 flags &= ~IRQF_SHARED;
298 #endif
299 if ((err = request_irq(adapter->pdev->irq, &e1000_intr, flags,
300 netdev->name, netdev)))
301 DPRINTK(PROBE, ERR,
302 "Unable to allocate interrupt Error: %d\n", err);
304 return err;
307 static void e1000_free_irq(struct e1000_adapter *adapter)
309 struct net_device *netdev = adapter->netdev;
311 free_irq(adapter->pdev->irq, netdev);
313 #ifdef CONFIG_PCI_MSI
314 if (adapter->have_msi)
315 pci_disable_msi(adapter->pdev);
316 #endif
320 * e1000_irq_disable - Mask off interrupt generation on the NIC
321 * @adapter: board private structure
324 static void
325 e1000_irq_disable(struct e1000_adapter *adapter)
327 atomic_inc(&adapter->irq_sem);
328 E1000_WRITE_REG(&adapter->hw, IMC, ~0);
329 E1000_WRITE_FLUSH(&adapter->hw);
330 synchronize_irq(adapter->pdev->irq);
334 * e1000_irq_enable - Enable default interrupt generation settings
335 * @adapter: board private structure
338 static void
339 e1000_irq_enable(struct e1000_adapter *adapter)
341 if (likely(atomic_dec_and_test(&adapter->irq_sem))) {
342 E1000_WRITE_REG(&adapter->hw, IMS, IMS_ENABLE_MASK);
343 E1000_WRITE_FLUSH(&adapter->hw);
347 static void
348 e1000_update_mng_vlan(struct e1000_adapter *adapter)
350 struct net_device *netdev = adapter->netdev;
351 uint16_t vid = adapter->hw.mng_cookie.vlan_id;
352 uint16_t old_vid = adapter->mng_vlan_id;
353 if (adapter->vlgrp) {
354 if (!adapter->vlgrp->vlan_devices[vid]) {
355 if (adapter->hw.mng_cookie.status &
356 E1000_MNG_DHCP_COOKIE_STATUS_VLAN_SUPPORT) {
357 e1000_vlan_rx_add_vid(netdev, vid);
358 adapter->mng_vlan_id = vid;
359 } else
360 adapter->mng_vlan_id = E1000_MNG_VLAN_NONE;
362 if ((old_vid != (uint16_t)E1000_MNG_VLAN_NONE) &&
363 (vid != old_vid) &&
364 !adapter->vlgrp->vlan_devices[old_vid])
365 e1000_vlan_rx_kill_vid(netdev, old_vid);
366 } else
367 adapter->mng_vlan_id = vid;
372 * e1000_release_hw_control - release control of the h/w to f/w
373 * @adapter: address of board private structure
375 * e1000_release_hw_control resets {CTRL_EXT|FWSM}:DRV_LOAD bit.
376 * For ASF and Pass Through versions of f/w this means that the
377 * driver is no longer loaded. For AMT version (only with 82573) i
378 * of the f/w this means that the netowrk i/f is closed.
382 static void
383 e1000_release_hw_control(struct e1000_adapter *adapter)
385 uint32_t ctrl_ext;
386 uint32_t swsm;
387 uint32_t extcnf;
389 /* Let firmware taken over control of h/w */
390 switch (adapter->hw.mac_type) {
391 case e1000_82571:
392 case e1000_82572:
393 case e1000_80003es2lan:
394 ctrl_ext = E1000_READ_REG(&adapter->hw, CTRL_EXT);
395 E1000_WRITE_REG(&adapter->hw, CTRL_EXT,
396 ctrl_ext & ~E1000_CTRL_EXT_DRV_LOAD);
397 break;
398 case e1000_82573:
399 swsm = E1000_READ_REG(&adapter->hw, SWSM);
400 E1000_WRITE_REG(&adapter->hw, SWSM,
401 swsm & ~E1000_SWSM_DRV_LOAD);
402 case e1000_ich8lan:
403 extcnf = E1000_READ_REG(&adapter->hw, CTRL_EXT);
404 E1000_WRITE_REG(&adapter->hw, CTRL_EXT,
405 extcnf & ~E1000_CTRL_EXT_DRV_LOAD);
406 break;
407 default:
408 break;
413 * e1000_get_hw_control - get control of the h/w from f/w
414 * @adapter: address of board private structure
416 * e1000_get_hw_control sets {CTRL_EXT|FWSM}:DRV_LOAD bit.
417 * For ASF and Pass Through versions of f/w this means that
418 * the driver is loaded. For AMT version (only with 82573)
419 * of the f/w this means that the netowrk i/f is open.
423 static void
424 e1000_get_hw_control(struct e1000_adapter *adapter)
426 uint32_t ctrl_ext;
427 uint32_t swsm;
428 uint32_t extcnf;
429 /* Let firmware know the driver has taken over */
430 switch (adapter->hw.mac_type) {
431 case e1000_82571:
432 case e1000_82572:
433 case e1000_80003es2lan:
434 ctrl_ext = E1000_READ_REG(&adapter->hw, CTRL_EXT);
435 E1000_WRITE_REG(&adapter->hw, CTRL_EXT,
436 ctrl_ext | E1000_CTRL_EXT_DRV_LOAD);
437 break;
438 case e1000_82573:
439 swsm = E1000_READ_REG(&adapter->hw, SWSM);
440 E1000_WRITE_REG(&adapter->hw, SWSM,
441 swsm | E1000_SWSM_DRV_LOAD);
442 break;
443 case e1000_ich8lan:
444 extcnf = E1000_READ_REG(&adapter->hw, EXTCNF_CTRL);
445 E1000_WRITE_REG(&adapter->hw, EXTCNF_CTRL,
446 extcnf | E1000_EXTCNF_CTRL_SWFLAG);
447 break;
448 default:
449 break;
454 e1000_up(struct e1000_adapter *adapter)
456 struct net_device *netdev = adapter->netdev;
457 int i;
459 /* hardware has been reset, we need to reload some things */
461 e1000_set_multi(netdev);
463 e1000_restore_vlan(adapter);
465 e1000_configure_tx(adapter);
466 e1000_setup_rctl(adapter);
467 e1000_configure_rx(adapter);
468 /* call E1000_DESC_UNUSED which always leaves
469 * at least 1 descriptor unused to make sure
470 * next_to_use != next_to_clean */
471 for (i = 0; i < adapter->num_rx_queues; i++) {
472 struct e1000_rx_ring *ring = &adapter->rx_ring[i];
473 adapter->alloc_rx_buf(adapter, ring,
474 E1000_DESC_UNUSED(ring));
477 adapter->tx_queue_len = netdev->tx_queue_len;
479 mod_timer(&adapter->watchdog_timer, jiffies);
481 #ifdef CONFIG_E1000_NAPI
482 netif_poll_enable(netdev);
483 #endif
484 e1000_irq_enable(adapter);
486 return 0;
490 * e1000_power_up_phy - restore link in case the phy was powered down
491 * @adapter: address of board private structure
493 * The phy may be powered down to save power and turn off link when the
494 * driver is unloaded and wake on lan is not enabled (among others)
495 * *** this routine MUST be followed by a call to e1000_reset ***
499 void e1000_power_up_phy(struct e1000_adapter *adapter)
501 uint16_t mii_reg = 0;
503 /* Just clear the power down bit to wake the phy back up */
504 if (adapter->hw.media_type == e1000_media_type_copper) {
505 /* according to the manual, the phy will retain its
506 * settings across a power-down/up cycle */
507 e1000_read_phy_reg(&adapter->hw, PHY_CTRL, &mii_reg);
508 mii_reg &= ~MII_CR_POWER_DOWN;
509 e1000_write_phy_reg(&adapter->hw, PHY_CTRL, mii_reg);
513 static void e1000_power_down_phy(struct e1000_adapter *adapter)
515 boolean_t mng_mode_enabled = (adapter->hw.mac_type >= e1000_82571) &&
516 e1000_check_mng_mode(&adapter->hw);
517 /* Power down the PHY so no link is implied when interface is down
518 * The PHY cannot be powered down if any of the following is TRUE
519 * (a) WoL is enabled
520 * (b) AMT is active
521 * (c) SoL/IDER session is active */
522 if (!adapter->wol && adapter->hw.mac_type >= e1000_82540 &&
523 adapter->hw.mac_type != e1000_ich8lan &&
524 adapter->hw.media_type == e1000_media_type_copper &&
525 !(E1000_READ_REG(&adapter->hw, MANC) & E1000_MANC_SMBUS_EN) &&
526 !mng_mode_enabled &&
527 !e1000_check_phy_reset_block(&adapter->hw)) {
528 uint16_t mii_reg = 0;
529 e1000_read_phy_reg(&adapter->hw, PHY_CTRL, &mii_reg);
530 mii_reg |= MII_CR_POWER_DOWN;
531 e1000_write_phy_reg(&adapter->hw, PHY_CTRL, mii_reg);
532 mdelay(1);
536 void
537 e1000_down(struct e1000_adapter *adapter)
539 struct net_device *netdev = adapter->netdev;
541 e1000_irq_disable(adapter);
543 del_timer_sync(&adapter->tx_fifo_stall_timer);
544 del_timer_sync(&adapter->watchdog_timer);
545 del_timer_sync(&adapter->phy_info_timer);
547 #ifdef CONFIG_E1000_NAPI
548 netif_poll_disable(netdev);
549 #endif
550 netdev->tx_queue_len = adapter->tx_queue_len;
551 adapter->link_speed = 0;
552 adapter->link_duplex = 0;
553 netif_carrier_off(netdev);
554 netif_stop_queue(netdev);
556 e1000_reset(adapter);
557 e1000_clean_all_tx_rings(adapter);
558 e1000_clean_all_rx_rings(adapter);
561 void
562 e1000_reinit_locked(struct e1000_adapter *adapter)
564 WARN_ON(in_interrupt());
565 while (test_and_set_bit(__E1000_RESETTING, &adapter->flags))
566 msleep(1);
567 e1000_down(adapter);
568 e1000_up(adapter);
569 clear_bit(__E1000_RESETTING, &adapter->flags);
572 void
573 e1000_reset(struct e1000_adapter *adapter)
575 uint32_t pba, manc;
576 #ifdef DISABLE_MULR
577 uint32_t tctl;
578 #endif
579 uint16_t fc_high_water_mark = E1000_FC_HIGH_DIFF;
581 /* Repartition Pba for greater than 9k mtu
582 * To take effect CTRL.RST is required.
585 switch (adapter->hw.mac_type) {
586 case e1000_82547:
587 case e1000_82547_rev_2:
588 pba = E1000_PBA_30K;
589 break;
590 case e1000_82571:
591 case e1000_82572:
592 case e1000_80003es2lan:
593 pba = E1000_PBA_38K;
594 break;
595 case e1000_82573:
596 pba = E1000_PBA_12K;
597 break;
598 case e1000_ich8lan:
599 pba = E1000_PBA_8K;
600 break;
601 default:
602 pba = E1000_PBA_48K;
603 break;
606 if ((adapter->hw.mac_type != e1000_82573) &&
607 (adapter->netdev->mtu > E1000_RXBUFFER_8192))
608 pba -= 8; /* allocate more FIFO for Tx */
611 if (adapter->hw.mac_type == e1000_82547) {
612 adapter->tx_fifo_head = 0;
613 adapter->tx_head_addr = pba << E1000_TX_HEAD_ADDR_SHIFT;
614 adapter->tx_fifo_size =
615 (E1000_PBA_40K - pba) << E1000_PBA_BYTES_SHIFT;
616 atomic_set(&adapter->tx_fifo_stall, 0);
619 E1000_WRITE_REG(&adapter->hw, PBA, pba);
621 /* flow control settings */
622 /* Set the FC high water mark to 90% of the FIFO size.
623 * Required to clear last 3 LSB */
624 fc_high_water_mark = ((pba * 9216)/10) & 0xFFF8;
625 /* We can't use 90% on small FIFOs because the remainder
626 * would be less than 1 full frame. In this case, we size
627 * it to allow at least a full frame above the high water
628 * mark. */
629 if (pba < E1000_PBA_16K)
630 fc_high_water_mark = (pba * 1024) - 1600;
632 adapter->hw.fc_high_water = fc_high_water_mark;
633 adapter->hw.fc_low_water = fc_high_water_mark - 8;
634 if (adapter->hw.mac_type == e1000_80003es2lan)
635 adapter->hw.fc_pause_time = 0xFFFF;
636 else
637 adapter->hw.fc_pause_time = E1000_FC_PAUSE_TIME;
638 adapter->hw.fc_send_xon = 1;
639 adapter->hw.fc = adapter->hw.original_fc;
641 /* Allow time for pending master requests to run */
642 e1000_reset_hw(&adapter->hw);
643 if (adapter->hw.mac_type >= e1000_82544)
644 E1000_WRITE_REG(&adapter->hw, WUC, 0);
645 #ifdef DISABLE_MULR
646 /* disable Multiple Reads in Transmit Control Register for debugging */
647 tctl = E1000_READ_REG(hw, TCTL);
648 E1000_WRITE_REG(hw, TCTL, tctl & ~E1000_TCTL_MULR);
650 #endif
651 if (e1000_init_hw(&adapter->hw))
652 DPRINTK(PROBE, ERR, "Hardware Error\n");
653 e1000_update_mng_vlan(adapter);
654 /* Enable h/w to recognize an 802.1Q VLAN Ethernet packet */
655 E1000_WRITE_REG(&adapter->hw, VET, ETHERNET_IEEE_VLAN_TYPE);
657 e1000_reset_adaptive(&adapter->hw);
658 e1000_phy_get_info(&adapter->hw, &adapter->phy_info);
660 if (!adapter->smart_power_down &&
661 (adapter->hw.mac_type == e1000_82571 ||
662 adapter->hw.mac_type == e1000_82572)) {
663 uint16_t phy_data = 0;
664 /* speed up time to link by disabling smart power down, ignore
665 * the return value of this function because there is nothing
666 * different we would do if it failed */
667 e1000_read_phy_reg(&adapter->hw, IGP02E1000_PHY_POWER_MGMT,
668 &phy_data);
669 phy_data &= ~IGP02E1000_PM_SPD;
670 e1000_write_phy_reg(&adapter->hw, IGP02E1000_PHY_POWER_MGMT,
671 phy_data);
674 if ((adapter->en_mng_pt) && (adapter->hw.mac_type < e1000_82571)) {
675 manc = E1000_READ_REG(&adapter->hw, MANC);
676 manc |= (E1000_MANC_ARP_EN | E1000_MANC_EN_MNG2HOST);
677 E1000_WRITE_REG(&adapter->hw, MANC, manc);
682 * e1000_probe - Device Initialization Routine
683 * @pdev: PCI device information struct
684 * @ent: entry in e1000_pci_tbl
686 * Returns 0 on success, negative on failure
688 * e1000_probe initializes an adapter identified by a pci_dev structure.
689 * The OS initialization, configuring of the adapter private structure,
690 * and a hardware reset occur.
693 static int __devinit
694 e1000_probe(struct pci_dev *pdev,
695 const struct pci_device_id *ent)
697 struct net_device *netdev;
698 struct e1000_adapter *adapter;
699 unsigned long mmio_start, mmio_len;
700 unsigned long flash_start, flash_len;
702 static int cards_found = 0;
703 static int global_quad_port_a = 0; /* global ksp3 port a indication */
704 int i, err, pci_using_dac;
705 uint16_t eeprom_data = 0;
706 uint16_t eeprom_apme_mask = E1000_EEPROM_APME;
707 if ((err = pci_enable_device(pdev)))
708 return err;
710 if (!(err = pci_set_dma_mask(pdev, DMA_64BIT_MASK)) &&
711 !(err = pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK))) {
712 pci_using_dac = 1;
713 } else {
714 if ((err = pci_set_dma_mask(pdev, DMA_32BIT_MASK)) &&
715 (err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK))) {
716 E1000_ERR("No usable DMA configuration, aborting\n");
717 goto err_dma;
719 pci_using_dac = 0;
722 if ((err = pci_request_regions(pdev, e1000_driver_name)))
723 goto err_pci_reg;
725 pci_set_master(pdev);
727 err = -ENOMEM;
728 netdev = alloc_etherdev(sizeof(struct e1000_adapter));
729 if (!netdev)
730 goto err_alloc_etherdev;
732 SET_MODULE_OWNER(netdev);
733 SET_NETDEV_DEV(netdev, &pdev->dev);
735 pci_set_drvdata(pdev, netdev);
736 adapter = netdev_priv(netdev);
737 adapter->netdev = netdev;
738 adapter->pdev = pdev;
739 adapter->hw.back = adapter;
740 adapter->msg_enable = (1 << debug) - 1;
742 mmio_start = pci_resource_start(pdev, BAR_0);
743 mmio_len = pci_resource_len(pdev, BAR_0);
745 err = -EIO;
746 adapter->hw.hw_addr = ioremap(mmio_start, mmio_len);
747 if (!adapter->hw.hw_addr)
748 goto err_ioremap;
750 for (i = BAR_1; i <= BAR_5; i++) {
751 if (pci_resource_len(pdev, i) == 0)
752 continue;
753 if (pci_resource_flags(pdev, i) & IORESOURCE_IO) {
754 adapter->hw.io_base = pci_resource_start(pdev, i);
755 break;
759 netdev->open = &e1000_open;
760 netdev->stop = &e1000_close;
761 netdev->hard_start_xmit = &e1000_xmit_frame;
762 netdev->get_stats = &e1000_get_stats;
763 netdev->set_multicast_list = &e1000_set_multi;
764 netdev->set_mac_address = &e1000_set_mac;
765 netdev->change_mtu = &e1000_change_mtu;
766 netdev->do_ioctl = &e1000_ioctl;
767 e1000_set_ethtool_ops(netdev);
768 netdev->tx_timeout = &e1000_tx_timeout;
769 netdev->watchdog_timeo = 5 * HZ;
770 #ifdef CONFIG_E1000_NAPI
771 netdev->poll = &e1000_clean;
772 netdev->weight = 64;
773 #endif
774 netdev->vlan_rx_register = e1000_vlan_rx_register;
775 netdev->vlan_rx_add_vid = e1000_vlan_rx_add_vid;
776 netdev->vlan_rx_kill_vid = e1000_vlan_rx_kill_vid;
777 #ifdef CONFIG_NET_POLL_CONTROLLER
778 netdev->poll_controller = e1000_netpoll;
779 #endif
780 strncpy(netdev->name, pci_name(pdev), sizeof(netdev->name) - 1);
782 netdev->mem_start = mmio_start;
783 netdev->mem_end = mmio_start + mmio_len;
784 netdev->base_addr = adapter->hw.io_base;
786 adapter->bd_number = cards_found;
788 /* setup the private structure */
790 if ((err = e1000_sw_init(adapter)))
791 goto err_sw_init;
793 err = -EIO;
794 /* Flash BAR mapping must happen after e1000_sw_init
795 * because it depends on mac_type */
796 if ((adapter->hw.mac_type == e1000_ich8lan) &&
797 (pci_resource_flags(pdev, 1) & IORESOURCE_MEM)) {
798 flash_start = pci_resource_start(pdev, 1);
799 flash_len = pci_resource_len(pdev, 1);
800 adapter->hw.flash_address = ioremap(flash_start, flash_len);
801 if (!adapter->hw.flash_address)
802 goto err_flashmap;
805 if (e1000_check_phy_reset_block(&adapter->hw))
806 DPRINTK(PROBE, INFO, "PHY reset is blocked due to SOL/IDER session.\n");
808 if (adapter->hw.mac_type >= e1000_82543) {
809 netdev->features = NETIF_F_SG |
810 NETIF_F_HW_CSUM |
811 NETIF_F_HW_VLAN_TX |
812 NETIF_F_HW_VLAN_RX |
813 NETIF_F_HW_VLAN_FILTER;
814 if (adapter->hw.mac_type == e1000_ich8lan)
815 netdev->features &= ~NETIF_F_HW_VLAN_FILTER;
818 #ifdef NETIF_F_TSO
819 if ((adapter->hw.mac_type >= e1000_82544) &&
820 (adapter->hw.mac_type != e1000_82547))
821 netdev->features |= NETIF_F_TSO;
823 #ifdef NETIF_F_TSO_IPV6
824 if (adapter->hw.mac_type > e1000_82547_rev_2)
825 netdev->features |= NETIF_F_TSO_IPV6;
826 #endif
827 #endif
828 if (pci_using_dac)
829 netdev->features |= NETIF_F_HIGHDMA;
831 netdev->features |= NETIF_F_LLTX;
833 adapter->en_mng_pt = e1000_enable_mng_pass_thru(&adapter->hw);
835 /* initialize eeprom parameters */
837 if (e1000_init_eeprom_params(&adapter->hw)) {
838 E1000_ERR("EEPROM initialization failed\n");
839 goto err_eeprom;
842 /* before reading the EEPROM, reset the controller to
843 * put the device in a known good starting state */
845 e1000_reset_hw(&adapter->hw);
847 /* make sure the EEPROM is good */
849 if (e1000_validate_eeprom_checksum(&adapter->hw) < 0) {
850 DPRINTK(PROBE, ERR, "The EEPROM Checksum Is Not Valid\n");
851 goto err_eeprom;
854 /* copy the MAC address out of the EEPROM */
856 if (e1000_read_mac_addr(&adapter->hw))
857 DPRINTK(PROBE, ERR, "EEPROM Read Error\n");
858 memcpy(netdev->dev_addr, adapter->hw.mac_addr, netdev->addr_len);
859 memcpy(netdev->perm_addr, adapter->hw.mac_addr, netdev->addr_len);
861 if (!is_valid_ether_addr(netdev->perm_addr)) {
862 DPRINTK(PROBE, ERR, "Invalid MAC Address\n");
863 goto err_eeprom;
866 e1000_get_bus_info(&adapter->hw);
868 init_timer(&adapter->tx_fifo_stall_timer);
869 adapter->tx_fifo_stall_timer.function = &e1000_82547_tx_fifo_stall;
870 adapter->tx_fifo_stall_timer.data = (unsigned long) adapter;
872 init_timer(&adapter->watchdog_timer);
873 adapter->watchdog_timer.function = &e1000_watchdog;
874 adapter->watchdog_timer.data = (unsigned long) adapter;
876 init_timer(&adapter->phy_info_timer);
877 adapter->phy_info_timer.function = &e1000_update_phy_info;
878 adapter->phy_info_timer.data = (unsigned long) adapter;
880 INIT_WORK(&adapter->reset_task,
881 (void (*)(void *))e1000_reset_task, netdev);
883 /* we're going to reset, so assume we have no link for now */
885 netif_carrier_off(netdev);
886 netif_stop_queue(netdev);
888 e1000_check_options(adapter);
890 /* Initial Wake on LAN setting
891 * If APM wake is enabled in the EEPROM,
892 * enable the ACPI Magic Packet filter
895 switch (adapter->hw.mac_type) {
896 case e1000_82542_rev2_0:
897 case e1000_82542_rev2_1:
898 case e1000_82543:
899 break;
900 case e1000_82544:
901 e1000_read_eeprom(&adapter->hw,
902 EEPROM_INIT_CONTROL2_REG, 1, &eeprom_data);
903 eeprom_apme_mask = E1000_EEPROM_82544_APM;
904 break;
905 case e1000_ich8lan:
906 e1000_read_eeprom(&adapter->hw,
907 EEPROM_INIT_CONTROL1_REG, 1, &eeprom_data);
908 eeprom_apme_mask = E1000_EEPROM_ICH8_APME;
909 break;
910 case e1000_82546:
911 case e1000_82546_rev_3:
912 case e1000_82571:
913 case e1000_80003es2lan:
914 if (E1000_READ_REG(&adapter->hw, STATUS) & E1000_STATUS_FUNC_1){
915 e1000_read_eeprom(&adapter->hw,
916 EEPROM_INIT_CONTROL3_PORT_B, 1, &eeprom_data);
917 break;
919 /* Fall Through */
920 default:
921 e1000_read_eeprom(&adapter->hw,
922 EEPROM_INIT_CONTROL3_PORT_A, 1, &eeprom_data);
923 break;
925 if (eeprom_data & eeprom_apme_mask)
926 adapter->eeprom_wol |= E1000_WUFC_MAG;
928 /* now that we have the eeprom settings, apply the special cases
929 * where the eeprom may be wrong or the board simply won't support
930 * wake on lan on a particular port */
931 switch (pdev->device) {
932 case E1000_DEV_ID_82546GB_PCIE:
933 adapter->eeprom_wol = 0;
934 break;
935 case E1000_DEV_ID_82546EB_FIBER:
936 case E1000_DEV_ID_82546GB_FIBER:
937 case E1000_DEV_ID_82571EB_FIBER:
938 /* Wake events only supported on port A for dual fiber
939 * regardless of eeprom setting */
940 if (E1000_READ_REG(&adapter->hw, STATUS) & E1000_STATUS_FUNC_1)
941 adapter->eeprom_wol = 0;
942 break;
943 case E1000_DEV_ID_82546GB_QUAD_COPPER_KSP3:
944 case E1000_DEV_ID_82571EB_QUAD_COPPER:
945 /* if quad port adapter, disable WoL on all but port A */
946 if (global_quad_port_a != 0)
947 adapter->eeprom_wol = 0;
948 else
949 adapter->quad_port_a = 1;
950 /* Reset for multiple quad port adapters */
951 if (++global_quad_port_a == 4)
952 global_quad_port_a = 0;
953 break;
956 /* initialize the wol settings based on the eeprom settings */
957 adapter->wol = adapter->eeprom_wol;
959 /* print bus type/speed/width info */
961 struct e1000_hw *hw = &adapter->hw;
962 DPRINTK(PROBE, INFO, "(PCI%s:%s:%s) ",
963 ((hw->bus_type == e1000_bus_type_pcix) ? "-X" :
964 (hw->bus_type == e1000_bus_type_pci_express ? " Express":"")),
965 ((hw->bus_speed == e1000_bus_speed_2500) ? "2.5Gb/s" :
966 (hw->bus_speed == e1000_bus_speed_133) ? "133MHz" :
967 (hw->bus_speed == e1000_bus_speed_120) ? "120MHz" :
968 (hw->bus_speed == e1000_bus_speed_100) ? "100MHz" :
969 (hw->bus_speed == e1000_bus_speed_66) ? "66MHz" : "33MHz"),
970 ((hw->bus_width == e1000_bus_width_64) ? "64-bit" :
971 (hw->bus_width == e1000_bus_width_pciex_4) ? "Width x4" :
972 (hw->bus_width == e1000_bus_width_pciex_1) ? "Width x1" :
973 "32-bit"));
976 for (i = 0; i < 6; i++)
977 printk("%2.2x%c", netdev->dev_addr[i], i == 5 ? '\n' : ':');
979 /* reset the hardware with the new settings */
980 e1000_reset(adapter);
982 /* If the controller is 82573 and f/w is AMT, do not set
983 * DRV_LOAD until the interface is up. For all other cases,
984 * let the f/w know that the h/w is now under the control
985 * of the driver. */
986 if (adapter->hw.mac_type != e1000_82573 ||
987 !e1000_check_mng_mode(&adapter->hw))
988 e1000_get_hw_control(adapter);
990 strcpy(netdev->name, "eth%d");
991 if ((err = register_netdev(netdev)))
992 goto err_register;
994 DPRINTK(PROBE, INFO, "Intel(R) PRO/1000 Network Connection\n");
996 cards_found++;
997 return 0;
999 err_register:
1000 e1000_release_hw_control(adapter);
1001 err_eeprom:
1002 if (!e1000_check_phy_reset_block(&adapter->hw))
1003 e1000_phy_hw_reset(&adapter->hw);
1005 if (adapter->hw.flash_address)
1006 iounmap(adapter->hw.flash_address);
1007 err_flashmap:
1008 #ifdef CONFIG_E1000_NAPI
1009 for (i = 0; i < adapter->num_rx_queues; i++)
1010 dev_put(&adapter->polling_netdev[i]);
1011 #endif
1013 kfree(adapter->tx_ring);
1014 kfree(adapter->rx_ring);
1015 #ifdef CONFIG_E1000_NAPI
1016 kfree(adapter->polling_netdev);
1017 #endif
1018 err_sw_init:
1019 iounmap(adapter->hw.hw_addr);
1020 err_ioremap:
1021 free_netdev(netdev);
1022 err_alloc_etherdev:
1023 pci_release_regions(pdev);
1024 err_pci_reg:
1025 err_dma:
1026 pci_disable_device(pdev);
1027 return err;
1031 * e1000_remove - Device Removal Routine
1032 * @pdev: PCI device information struct
1034 * e1000_remove is called by the PCI subsystem to alert the driver
1035 * that it should release a PCI device. The could be caused by a
1036 * Hot-Plug event, or because the driver is going to be removed from
1037 * memory.
1040 static void __devexit
1041 e1000_remove(struct pci_dev *pdev)
1043 struct net_device *netdev = pci_get_drvdata(pdev);
1044 struct e1000_adapter *adapter = netdev_priv(netdev);
1045 uint32_t manc;
1046 #ifdef CONFIG_E1000_NAPI
1047 int i;
1048 #endif
1050 flush_scheduled_work();
1052 if (adapter->hw.mac_type < e1000_82571 &&
1053 adapter->hw.media_type == e1000_media_type_copper) {
1054 manc = E1000_READ_REG(&adapter->hw, MANC);
1055 if (manc & E1000_MANC_SMBUS_EN) {
1056 manc |= E1000_MANC_ARP_EN;
1057 E1000_WRITE_REG(&adapter->hw, MANC, manc);
1061 /* Release control of h/w to f/w. If f/w is AMT enabled, this
1062 * would have already happened in close and is redundant. */
1063 e1000_release_hw_control(adapter);
1065 unregister_netdev(netdev);
1066 #ifdef CONFIG_E1000_NAPI
1067 for (i = 0; i < adapter->num_rx_queues; i++)
1068 dev_put(&adapter->polling_netdev[i]);
1069 #endif
1071 if (!e1000_check_phy_reset_block(&adapter->hw))
1072 e1000_phy_hw_reset(&adapter->hw);
1074 kfree(adapter->tx_ring);
1075 kfree(adapter->rx_ring);
1076 #ifdef CONFIG_E1000_NAPI
1077 kfree(adapter->polling_netdev);
1078 #endif
1080 iounmap(adapter->hw.hw_addr);
1081 if (adapter->hw.flash_address)
1082 iounmap(adapter->hw.flash_address);
1083 pci_release_regions(pdev);
1085 free_netdev(netdev);
1087 pci_disable_device(pdev);
1091 * e1000_sw_init - Initialize general software structures (struct e1000_adapter)
1092 * @adapter: board private structure to initialize
1094 * e1000_sw_init initializes the Adapter private data structure.
1095 * Fields are initialized based on PCI device information and
1096 * OS network device settings (MTU size).
1099 static int __devinit
1100 e1000_sw_init(struct e1000_adapter *adapter)
1102 struct e1000_hw *hw = &adapter->hw;
1103 struct net_device *netdev = adapter->netdev;
1104 struct pci_dev *pdev = adapter->pdev;
1105 #ifdef CONFIG_E1000_NAPI
1106 int i;
1107 #endif
1109 /* PCI config space info */
1111 hw->vendor_id = pdev->vendor;
1112 hw->device_id = pdev->device;
1113 hw->subsystem_vendor_id = pdev->subsystem_vendor;
1114 hw->subsystem_id = pdev->subsystem_device;
1116 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
1118 pci_read_config_word(pdev, PCI_COMMAND, &hw->pci_cmd_word);
1120 adapter->rx_buffer_len = MAXIMUM_ETHERNET_VLAN_SIZE;
1121 adapter->rx_ps_bsize0 = E1000_RXBUFFER_128;
1122 hw->max_frame_size = netdev->mtu +
1123 ENET_HEADER_SIZE + ETHERNET_FCS_SIZE;
1124 hw->min_frame_size = MINIMUM_ETHERNET_FRAME_SIZE;
1126 /* identify the MAC */
1128 if (e1000_set_mac_type(hw)) {
1129 DPRINTK(PROBE, ERR, "Unknown MAC Type\n");
1130 return -EIO;
1133 switch (hw->mac_type) {
1134 default:
1135 break;
1136 case e1000_82541:
1137 case e1000_82547:
1138 case e1000_82541_rev_2:
1139 case e1000_82547_rev_2:
1140 hw->phy_init_script = 1;
1141 break;
1144 e1000_set_media_type(hw);
1146 hw->wait_autoneg_complete = FALSE;
1147 hw->tbi_compatibility_en = TRUE;
1148 hw->adaptive_ifs = TRUE;
1150 /* Copper options */
1152 if (hw->media_type == e1000_media_type_copper) {
1153 hw->mdix = AUTO_ALL_MODES;
1154 hw->disable_polarity_correction = FALSE;
1155 hw->master_slave = E1000_MASTER_SLAVE;
1158 adapter->num_tx_queues = 1;
1159 adapter->num_rx_queues = 1;
1161 if (e1000_alloc_queues(adapter)) {
1162 DPRINTK(PROBE, ERR, "Unable to allocate memory for queues\n");
1163 return -ENOMEM;
1166 #ifdef CONFIG_E1000_NAPI
1167 for (i = 0; i < adapter->num_rx_queues; i++) {
1168 adapter->polling_netdev[i].priv = adapter;
1169 adapter->polling_netdev[i].poll = &e1000_clean;
1170 adapter->polling_netdev[i].weight = 64;
1171 dev_hold(&adapter->polling_netdev[i]);
1172 set_bit(__LINK_STATE_START, &adapter->polling_netdev[i].state);
1174 spin_lock_init(&adapter->tx_queue_lock);
1175 #endif
1177 atomic_set(&adapter->irq_sem, 1);
1178 spin_lock_init(&adapter->stats_lock);
1180 return 0;
1184 * e1000_alloc_queues - Allocate memory for all rings
1185 * @adapter: board private structure to initialize
1187 * We allocate one ring per queue at run-time since we don't know the
1188 * number of queues at compile-time. The polling_netdev array is
1189 * intended for Multiqueue, but should work fine with a single queue.
1192 static int __devinit
1193 e1000_alloc_queues(struct e1000_adapter *adapter)
1195 int size;
1197 size = sizeof(struct e1000_tx_ring) * adapter->num_tx_queues;
1198 adapter->tx_ring = kmalloc(size, GFP_KERNEL);
1199 if (!adapter->tx_ring)
1200 return -ENOMEM;
1201 memset(adapter->tx_ring, 0, size);
1203 size = sizeof(struct e1000_rx_ring) * adapter->num_rx_queues;
1204 adapter->rx_ring = kmalloc(size, GFP_KERNEL);
1205 if (!adapter->rx_ring) {
1206 kfree(adapter->tx_ring);
1207 return -ENOMEM;
1209 memset(adapter->rx_ring, 0, size);
1211 #ifdef CONFIG_E1000_NAPI
1212 size = sizeof(struct net_device) * adapter->num_rx_queues;
1213 adapter->polling_netdev = kmalloc(size, GFP_KERNEL);
1214 if (!adapter->polling_netdev) {
1215 kfree(adapter->tx_ring);
1216 kfree(adapter->rx_ring);
1217 return -ENOMEM;
1219 memset(adapter->polling_netdev, 0, size);
1220 #endif
1222 return E1000_SUCCESS;
1226 * e1000_open - Called when a network interface is made active
1227 * @netdev: network interface device structure
1229 * Returns 0 on success, negative value on failure
1231 * The open entry point is called when a network interface is made
1232 * active by the system (IFF_UP). At this point all resources needed
1233 * for transmit and receive operations are allocated, the interrupt
1234 * handler is registered with the OS, the watchdog timer is started,
1235 * and the stack is notified that the interface is ready.
1238 static int
1239 e1000_open(struct net_device *netdev)
1241 struct e1000_adapter *adapter = netdev_priv(netdev);
1242 int err;
1244 /* disallow open during test */
1245 if (test_bit(__E1000_DRIVER_TESTING, &adapter->flags))
1246 return -EBUSY;
1248 /* allocate transmit descriptors */
1250 if ((err = e1000_setup_all_tx_resources(adapter)))
1251 goto err_setup_tx;
1253 /* allocate receive descriptors */
1255 if ((err = e1000_setup_all_rx_resources(adapter)))
1256 goto err_setup_rx;
1258 err = e1000_request_irq(adapter);
1259 if (err)
1260 goto err_req_irq;
1262 e1000_power_up_phy(adapter);
1264 if ((err = e1000_up(adapter)))
1265 goto err_up;
1266 adapter->mng_vlan_id = E1000_MNG_VLAN_NONE;
1267 if ((adapter->hw.mng_cookie.status &
1268 E1000_MNG_DHCP_COOKIE_STATUS_VLAN_SUPPORT)) {
1269 e1000_update_mng_vlan(adapter);
1272 /* If AMT is enabled, let the firmware know that the network
1273 * interface is now open */
1274 if (adapter->hw.mac_type == e1000_82573 &&
1275 e1000_check_mng_mode(&adapter->hw))
1276 e1000_get_hw_control(adapter);
1278 return E1000_SUCCESS;
1280 err_up:
1281 e1000_power_down_phy(adapter);
1282 e1000_free_irq(adapter);
1283 err_req_irq:
1284 e1000_free_all_rx_resources(adapter);
1285 err_setup_rx:
1286 e1000_free_all_tx_resources(adapter);
1287 err_setup_tx:
1288 e1000_reset(adapter);
1290 return err;
1294 * e1000_close - Disables a network interface
1295 * @netdev: network interface device structure
1297 * Returns 0, this is not allowed to fail
1299 * The close entry point is called when an interface is de-activated
1300 * by the OS. The hardware is still under the drivers control, but
1301 * needs to be disabled. A global MAC reset is issued to stop the
1302 * hardware, and all transmit and receive resources are freed.
1305 static int
1306 e1000_close(struct net_device *netdev)
1308 struct e1000_adapter *adapter = netdev_priv(netdev);
1310 WARN_ON(test_bit(__E1000_RESETTING, &adapter->flags));
1311 e1000_down(adapter);
1312 e1000_power_down_phy(adapter);
1313 e1000_free_irq(adapter);
1315 e1000_free_all_tx_resources(adapter);
1316 e1000_free_all_rx_resources(adapter);
1318 if ((adapter->hw.mng_cookie.status &
1319 E1000_MNG_DHCP_COOKIE_STATUS_VLAN_SUPPORT)) {
1320 e1000_vlan_rx_kill_vid(netdev, adapter->mng_vlan_id);
1323 /* If AMT is enabled, let the firmware know that the network
1324 * interface is now closed */
1325 if (adapter->hw.mac_type == e1000_82573 &&
1326 e1000_check_mng_mode(&adapter->hw))
1327 e1000_release_hw_control(adapter);
1329 return 0;
1333 * e1000_check_64k_bound - check that memory doesn't cross 64kB boundary
1334 * @adapter: address of board private structure
1335 * @start: address of beginning of memory
1336 * @len: length of memory
1338 static boolean_t
1339 e1000_check_64k_bound(struct e1000_adapter *adapter,
1340 void *start, unsigned long len)
1342 unsigned long begin = (unsigned long) start;
1343 unsigned long end = begin + len;
1345 /* First rev 82545 and 82546 need to not allow any memory
1346 * write location to cross 64k boundary due to errata 23 */
1347 if (adapter->hw.mac_type == e1000_82545 ||
1348 adapter->hw.mac_type == e1000_82546) {
1349 return ((begin ^ (end - 1)) >> 16) != 0 ? FALSE : TRUE;
1352 return TRUE;
1356 * e1000_setup_tx_resources - allocate Tx resources (Descriptors)
1357 * @adapter: board private structure
1358 * @txdr: tx descriptor ring (for a specific queue) to setup
1360 * Return 0 on success, negative on failure
1363 static int
1364 e1000_setup_tx_resources(struct e1000_adapter *adapter,
1365 struct e1000_tx_ring *txdr)
1367 struct pci_dev *pdev = adapter->pdev;
1368 int size;
1370 size = sizeof(struct e1000_buffer) * txdr->count;
1371 txdr->buffer_info = vmalloc(size);
1372 if (!txdr->buffer_info) {
1373 DPRINTK(PROBE, ERR,
1374 "Unable to allocate memory for the transmit descriptor ring\n");
1375 return -ENOMEM;
1377 memset(txdr->buffer_info, 0, size);
1379 /* round up to nearest 4K */
1381 txdr->size = txdr->count * sizeof(struct e1000_tx_desc);
1382 E1000_ROUNDUP(txdr->size, 4096);
1384 txdr->desc = pci_alloc_consistent(pdev, txdr->size, &txdr->dma);
1385 if (!txdr->desc) {
1386 setup_tx_desc_die:
1387 vfree(txdr->buffer_info);
1388 DPRINTK(PROBE, ERR,
1389 "Unable to allocate memory for the transmit descriptor ring\n");
1390 return -ENOMEM;
1393 /* Fix for errata 23, can't cross 64kB boundary */
1394 if (!e1000_check_64k_bound(adapter, txdr->desc, txdr->size)) {
1395 void *olddesc = txdr->desc;
1396 dma_addr_t olddma = txdr->dma;
1397 DPRINTK(TX_ERR, ERR, "txdr align check failed: %u bytes "
1398 "at %p\n", txdr->size, txdr->desc);
1399 /* Try again, without freeing the previous */
1400 txdr->desc = pci_alloc_consistent(pdev, txdr->size, &txdr->dma);
1401 /* Failed allocation, critical failure */
1402 if (!txdr->desc) {
1403 pci_free_consistent(pdev, txdr->size, olddesc, olddma);
1404 goto setup_tx_desc_die;
1407 if (!e1000_check_64k_bound(adapter, txdr->desc, txdr->size)) {
1408 /* give up */
1409 pci_free_consistent(pdev, txdr->size, txdr->desc,
1410 txdr->dma);
1411 pci_free_consistent(pdev, txdr->size, olddesc, olddma);
1412 DPRINTK(PROBE, ERR,
1413 "Unable to allocate aligned memory "
1414 "for the transmit descriptor ring\n");
1415 vfree(txdr->buffer_info);
1416 return -ENOMEM;
1417 } else {
1418 /* Free old allocation, new allocation was successful */
1419 pci_free_consistent(pdev, txdr->size, olddesc, olddma);
1422 memset(txdr->desc, 0, txdr->size);
1424 txdr->next_to_use = 0;
1425 txdr->next_to_clean = 0;
1426 spin_lock_init(&txdr->tx_lock);
1428 return 0;
1432 * e1000_setup_all_tx_resources - wrapper to allocate Tx resources
1433 * (Descriptors) for all queues
1434 * @adapter: board private structure
1436 * Return 0 on success, negative on failure
1440 e1000_setup_all_tx_resources(struct e1000_adapter *adapter)
1442 int i, err = 0;
1444 for (i = 0; i < adapter->num_tx_queues; i++) {
1445 err = e1000_setup_tx_resources(adapter, &adapter->tx_ring[i]);
1446 if (err) {
1447 DPRINTK(PROBE, ERR,
1448 "Allocation for Tx Queue %u failed\n", i);
1449 for (i-- ; i >= 0; i--)
1450 e1000_free_tx_resources(adapter,
1451 &adapter->tx_ring[i]);
1452 break;
1456 return err;
1460 * e1000_configure_tx - Configure 8254x Transmit Unit after Reset
1461 * @adapter: board private structure
1463 * Configure the Tx unit of the MAC after a reset.
1466 static void
1467 e1000_configure_tx(struct e1000_adapter *adapter)
1469 uint64_t tdba;
1470 struct e1000_hw *hw = &adapter->hw;
1471 uint32_t tdlen, tctl, tipg, tarc;
1472 uint32_t ipgr1, ipgr2;
1474 /* Setup the HW Tx Head and Tail descriptor pointers */
1476 switch (adapter->num_tx_queues) {
1477 case 1:
1478 default:
1479 tdba = adapter->tx_ring[0].dma;
1480 tdlen = adapter->tx_ring[0].count *
1481 sizeof(struct e1000_tx_desc);
1482 E1000_WRITE_REG(hw, TDLEN, tdlen);
1483 E1000_WRITE_REG(hw, TDBAH, (tdba >> 32));
1484 E1000_WRITE_REG(hw, TDBAL, (tdba & 0x00000000ffffffffULL));
1485 E1000_WRITE_REG(hw, TDT, 0);
1486 E1000_WRITE_REG(hw, TDH, 0);
1487 adapter->tx_ring[0].tdh = ((hw->mac_type >= e1000_82543) ? E1000_TDH : E1000_82542_TDH);
1488 adapter->tx_ring[0].tdt = ((hw->mac_type >= e1000_82543) ? E1000_TDT : E1000_82542_TDT);
1489 break;
1492 /* Set the default values for the Tx Inter Packet Gap timer */
1494 if (hw->media_type == e1000_media_type_fiber ||
1495 hw->media_type == e1000_media_type_internal_serdes)
1496 tipg = DEFAULT_82543_TIPG_IPGT_FIBER;
1497 else
1498 tipg = DEFAULT_82543_TIPG_IPGT_COPPER;
1500 switch (hw->mac_type) {
1501 case e1000_82542_rev2_0:
1502 case e1000_82542_rev2_1:
1503 tipg = DEFAULT_82542_TIPG_IPGT;
1504 ipgr1 = DEFAULT_82542_TIPG_IPGR1;
1505 ipgr2 = DEFAULT_82542_TIPG_IPGR2;
1506 break;
1507 case e1000_80003es2lan:
1508 ipgr1 = DEFAULT_82543_TIPG_IPGR1;
1509 ipgr2 = DEFAULT_80003ES2LAN_TIPG_IPGR2;
1510 break;
1511 default:
1512 ipgr1 = DEFAULT_82543_TIPG_IPGR1;
1513 ipgr2 = DEFAULT_82543_TIPG_IPGR2;
1514 break;
1516 tipg |= ipgr1 << E1000_TIPG_IPGR1_SHIFT;
1517 tipg |= ipgr2 << E1000_TIPG_IPGR2_SHIFT;
1518 E1000_WRITE_REG(hw, TIPG, tipg);
1520 /* Set the Tx Interrupt Delay register */
1522 E1000_WRITE_REG(hw, TIDV, adapter->tx_int_delay);
1523 if (hw->mac_type >= e1000_82540)
1524 E1000_WRITE_REG(hw, TADV, adapter->tx_abs_int_delay);
1526 /* Program the Transmit Control Register */
1528 tctl = E1000_READ_REG(hw, TCTL);
1529 tctl &= ~E1000_TCTL_CT;
1530 tctl |= E1000_TCTL_PSP | E1000_TCTL_RTLC |
1531 (E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT);
1533 if (hw->mac_type == e1000_82571 || hw->mac_type == e1000_82572) {
1534 tarc = E1000_READ_REG(hw, TARC0);
1535 tarc |= (1 << 21);
1536 E1000_WRITE_REG(hw, TARC0, tarc);
1537 } else if (hw->mac_type == e1000_80003es2lan) {
1538 tarc = E1000_READ_REG(hw, TARC0);
1539 tarc |= 1;
1540 E1000_WRITE_REG(hw, TARC0, tarc);
1541 tarc = E1000_READ_REG(hw, TARC1);
1542 tarc |= 1;
1543 E1000_WRITE_REG(hw, TARC1, tarc);
1546 e1000_config_collision_dist(hw);
1548 /* Setup Transmit Descriptor Settings for eop descriptor */
1549 adapter->txd_cmd = E1000_TXD_CMD_IDE | E1000_TXD_CMD_EOP |
1550 E1000_TXD_CMD_IFCS;
1552 if (hw->mac_type < e1000_82543)
1553 adapter->txd_cmd |= E1000_TXD_CMD_RPS;
1554 else
1555 adapter->txd_cmd |= E1000_TXD_CMD_RS;
1557 /* Cache if we're 82544 running in PCI-X because we'll
1558 * need this to apply a workaround later in the send path. */
1559 if (hw->mac_type == e1000_82544 &&
1560 hw->bus_type == e1000_bus_type_pcix)
1561 adapter->pcix_82544 = 1;
1563 E1000_WRITE_REG(hw, TCTL, tctl);
1568 * e1000_setup_rx_resources - allocate Rx resources (Descriptors)
1569 * @adapter: board private structure
1570 * @rxdr: rx descriptor ring (for a specific queue) to setup
1572 * Returns 0 on success, negative on failure
1575 static int
1576 e1000_setup_rx_resources(struct e1000_adapter *adapter,
1577 struct e1000_rx_ring *rxdr)
1579 struct pci_dev *pdev = adapter->pdev;
1580 int size, desc_len;
1582 size = sizeof(struct e1000_buffer) * rxdr->count;
1583 rxdr->buffer_info = vmalloc(size);
1584 if (!rxdr->buffer_info) {
1585 DPRINTK(PROBE, ERR,
1586 "Unable to allocate memory for the receive descriptor ring\n");
1587 return -ENOMEM;
1589 memset(rxdr->buffer_info, 0, size);
1591 size = sizeof(struct e1000_ps_page) * rxdr->count;
1592 rxdr->ps_page = kmalloc(size, GFP_KERNEL);
1593 if (!rxdr->ps_page) {
1594 vfree(rxdr->buffer_info);
1595 DPRINTK(PROBE, ERR,
1596 "Unable to allocate memory for the receive descriptor ring\n");
1597 return -ENOMEM;
1599 memset(rxdr->ps_page, 0, size);
1601 size = sizeof(struct e1000_ps_page_dma) * rxdr->count;
1602 rxdr->ps_page_dma = kmalloc(size, GFP_KERNEL);
1603 if (!rxdr->ps_page_dma) {
1604 vfree(rxdr->buffer_info);
1605 kfree(rxdr->ps_page);
1606 DPRINTK(PROBE, ERR,
1607 "Unable to allocate memory for the receive descriptor ring\n");
1608 return -ENOMEM;
1610 memset(rxdr->ps_page_dma, 0, size);
1612 if (adapter->hw.mac_type <= e1000_82547_rev_2)
1613 desc_len = sizeof(struct e1000_rx_desc);
1614 else
1615 desc_len = sizeof(union e1000_rx_desc_packet_split);
1617 /* Round up to nearest 4K */
1619 rxdr->size = rxdr->count * desc_len;
1620 E1000_ROUNDUP(rxdr->size, 4096);
1622 rxdr->desc = pci_alloc_consistent(pdev, rxdr->size, &rxdr->dma);
1624 if (!rxdr->desc) {
1625 DPRINTK(PROBE, ERR,
1626 "Unable to allocate memory for the receive descriptor ring\n");
1627 setup_rx_desc_die:
1628 vfree(rxdr->buffer_info);
1629 kfree(rxdr->ps_page);
1630 kfree(rxdr->ps_page_dma);
1631 return -ENOMEM;
1634 /* Fix for errata 23, can't cross 64kB boundary */
1635 if (!e1000_check_64k_bound(adapter, rxdr->desc, rxdr->size)) {
1636 void *olddesc = rxdr->desc;
1637 dma_addr_t olddma = rxdr->dma;
1638 DPRINTK(RX_ERR, ERR, "rxdr align check failed: %u bytes "
1639 "at %p\n", rxdr->size, rxdr->desc);
1640 /* Try again, without freeing the previous */
1641 rxdr->desc = pci_alloc_consistent(pdev, rxdr->size, &rxdr->dma);
1642 /* Failed allocation, critical failure */
1643 if (!rxdr->desc) {
1644 pci_free_consistent(pdev, rxdr->size, olddesc, olddma);
1645 DPRINTK(PROBE, ERR,
1646 "Unable to allocate memory "
1647 "for the receive descriptor ring\n");
1648 goto setup_rx_desc_die;
1651 if (!e1000_check_64k_bound(adapter, rxdr->desc, rxdr->size)) {
1652 /* give up */
1653 pci_free_consistent(pdev, rxdr->size, rxdr->desc,
1654 rxdr->dma);
1655 pci_free_consistent(pdev, rxdr->size, olddesc, olddma);
1656 DPRINTK(PROBE, ERR,
1657 "Unable to allocate aligned memory "
1658 "for the receive descriptor ring\n");
1659 goto setup_rx_desc_die;
1660 } else {
1661 /* Free old allocation, new allocation was successful */
1662 pci_free_consistent(pdev, rxdr->size, olddesc, olddma);
1665 memset(rxdr->desc, 0, rxdr->size);
1667 rxdr->next_to_clean = 0;
1668 rxdr->next_to_use = 0;
1670 return 0;
1674 * e1000_setup_all_rx_resources - wrapper to allocate Rx resources
1675 * (Descriptors) for all queues
1676 * @adapter: board private structure
1678 * Return 0 on success, negative on failure
1682 e1000_setup_all_rx_resources(struct e1000_adapter *adapter)
1684 int i, err = 0;
1686 for (i = 0; i < adapter->num_rx_queues; i++) {
1687 err = e1000_setup_rx_resources(adapter, &adapter->rx_ring[i]);
1688 if (err) {
1689 DPRINTK(PROBE, ERR,
1690 "Allocation for Rx Queue %u failed\n", i);
1691 for (i-- ; i >= 0; i--)
1692 e1000_free_rx_resources(adapter,
1693 &adapter->rx_ring[i]);
1694 break;
1698 return err;
1702 * e1000_setup_rctl - configure the receive control registers
1703 * @adapter: Board private structure
1705 #define PAGE_USE_COUNT(S) (((S) >> PAGE_SHIFT) + \
1706 (((S) & (PAGE_SIZE - 1)) ? 1 : 0))
1707 static void
1708 e1000_setup_rctl(struct e1000_adapter *adapter)
1710 uint32_t rctl, rfctl;
1711 uint32_t psrctl = 0;
1712 #ifndef CONFIG_E1000_DISABLE_PACKET_SPLIT
1713 uint32_t pages = 0;
1714 #endif
1716 rctl = E1000_READ_REG(&adapter->hw, RCTL);
1718 rctl &= ~(3 << E1000_RCTL_MO_SHIFT);
1720 rctl |= E1000_RCTL_EN | E1000_RCTL_BAM |
1721 E1000_RCTL_LBM_NO | E1000_RCTL_RDMTS_HALF |
1722 (adapter->hw.mc_filter_type << E1000_RCTL_MO_SHIFT);
1724 if (adapter->hw.tbi_compatibility_on == 1)
1725 rctl |= E1000_RCTL_SBP;
1726 else
1727 rctl &= ~E1000_RCTL_SBP;
1729 if (adapter->netdev->mtu <= ETH_DATA_LEN)
1730 rctl &= ~E1000_RCTL_LPE;
1731 else
1732 rctl |= E1000_RCTL_LPE;
1734 /* Setup buffer sizes */
1735 rctl &= ~E1000_RCTL_SZ_4096;
1736 rctl |= E1000_RCTL_BSEX;
1737 switch (adapter->rx_buffer_len) {
1738 case E1000_RXBUFFER_256:
1739 rctl |= E1000_RCTL_SZ_256;
1740 rctl &= ~E1000_RCTL_BSEX;
1741 break;
1742 case E1000_RXBUFFER_512:
1743 rctl |= E1000_RCTL_SZ_512;
1744 rctl &= ~E1000_RCTL_BSEX;
1745 break;
1746 case E1000_RXBUFFER_1024:
1747 rctl |= E1000_RCTL_SZ_1024;
1748 rctl &= ~E1000_RCTL_BSEX;
1749 break;
1750 case E1000_RXBUFFER_2048:
1751 default:
1752 rctl |= E1000_RCTL_SZ_2048;
1753 rctl &= ~E1000_RCTL_BSEX;
1754 break;
1755 case E1000_RXBUFFER_4096:
1756 rctl |= E1000_RCTL_SZ_4096;
1757 break;
1758 case E1000_RXBUFFER_8192:
1759 rctl |= E1000_RCTL_SZ_8192;
1760 break;
1761 case E1000_RXBUFFER_16384:
1762 rctl |= E1000_RCTL_SZ_16384;
1763 break;
1766 #ifndef CONFIG_E1000_DISABLE_PACKET_SPLIT
1767 /* 82571 and greater support packet-split where the protocol
1768 * header is placed in skb->data and the packet data is
1769 * placed in pages hanging off of skb_shinfo(skb)->nr_frags.
1770 * In the case of a non-split, skb->data is linearly filled,
1771 * followed by the page buffers. Therefore, skb->data is
1772 * sized to hold the largest protocol header.
1774 pages = PAGE_USE_COUNT(adapter->netdev->mtu);
1775 if ((adapter->hw.mac_type > e1000_82547_rev_2) && (pages <= 3) &&
1776 PAGE_SIZE <= 16384)
1777 adapter->rx_ps_pages = pages;
1778 else
1779 adapter->rx_ps_pages = 0;
1780 #endif
1781 if (adapter->rx_ps_pages) {
1782 /* Configure extra packet-split registers */
1783 rfctl = E1000_READ_REG(&adapter->hw, RFCTL);
1784 rfctl |= E1000_RFCTL_EXTEN;
1785 /* disable IPv6 packet split support */
1786 rfctl |= E1000_RFCTL_IPV6_DIS;
1787 E1000_WRITE_REG(&adapter->hw, RFCTL, rfctl);
1789 rctl |= E1000_RCTL_DTYP_PS;
1791 psrctl |= adapter->rx_ps_bsize0 >>
1792 E1000_PSRCTL_BSIZE0_SHIFT;
1794 switch (adapter->rx_ps_pages) {
1795 case 3:
1796 psrctl |= PAGE_SIZE <<
1797 E1000_PSRCTL_BSIZE3_SHIFT;
1798 case 2:
1799 psrctl |= PAGE_SIZE <<
1800 E1000_PSRCTL_BSIZE2_SHIFT;
1801 case 1:
1802 psrctl |= PAGE_SIZE >>
1803 E1000_PSRCTL_BSIZE1_SHIFT;
1804 break;
1807 E1000_WRITE_REG(&adapter->hw, PSRCTL, psrctl);
1810 E1000_WRITE_REG(&adapter->hw, RCTL, rctl);
1814 * e1000_configure_rx - Configure 8254x Receive Unit after Reset
1815 * @adapter: board private structure
1817 * Configure the Rx unit of the MAC after a reset.
1820 static void
1821 e1000_configure_rx(struct e1000_adapter *adapter)
1823 uint64_t rdba;
1824 struct e1000_hw *hw = &adapter->hw;
1825 uint32_t rdlen, rctl, rxcsum, ctrl_ext;
1827 if (adapter->rx_ps_pages) {
1828 /* this is a 32 byte descriptor */
1829 rdlen = adapter->rx_ring[0].count *
1830 sizeof(union e1000_rx_desc_packet_split);
1831 adapter->clean_rx = e1000_clean_rx_irq_ps;
1832 adapter->alloc_rx_buf = e1000_alloc_rx_buffers_ps;
1833 } else {
1834 rdlen = adapter->rx_ring[0].count *
1835 sizeof(struct e1000_rx_desc);
1836 adapter->clean_rx = e1000_clean_rx_irq;
1837 adapter->alloc_rx_buf = e1000_alloc_rx_buffers;
1840 /* disable receives while setting up the descriptors */
1841 rctl = E1000_READ_REG(hw, RCTL);
1842 E1000_WRITE_REG(hw, RCTL, rctl & ~E1000_RCTL_EN);
1844 /* set the Receive Delay Timer Register */
1845 E1000_WRITE_REG(hw, RDTR, adapter->rx_int_delay);
1847 if (hw->mac_type >= e1000_82540) {
1848 E1000_WRITE_REG(hw, RADV, adapter->rx_abs_int_delay);
1849 if (adapter->itr > 1)
1850 E1000_WRITE_REG(hw, ITR,
1851 1000000000 / (adapter->itr * 256));
1854 if (hw->mac_type >= e1000_82571) {
1855 ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
1856 /* Reset delay timers after every interrupt */
1857 ctrl_ext |= E1000_CTRL_EXT_INT_TIMER_CLR;
1858 #ifdef CONFIG_E1000_NAPI
1859 /* Auto-Mask interrupts upon ICR read. */
1860 ctrl_ext |= E1000_CTRL_EXT_IAME;
1861 #endif
1862 E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
1863 E1000_WRITE_REG(hw, IAM, ~0);
1864 E1000_WRITE_FLUSH(hw);
1867 /* Setup the HW Rx Head and Tail Descriptor Pointers and
1868 * the Base and Length of the Rx Descriptor Ring */
1869 switch (adapter->num_rx_queues) {
1870 case 1:
1871 default:
1872 rdba = adapter->rx_ring[0].dma;
1873 E1000_WRITE_REG(hw, RDLEN, rdlen);
1874 E1000_WRITE_REG(hw, RDBAH, (rdba >> 32));
1875 E1000_WRITE_REG(hw, RDBAL, (rdba & 0x00000000ffffffffULL));
1876 E1000_WRITE_REG(hw, RDT, 0);
1877 E1000_WRITE_REG(hw, RDH, 0);
1878 adapter->rx_ring[0].rdh = ((hw->mac_type >= e1000_82543) ? E1000_RDH : E1000_82542_RDH);
1879 adapter->rx_ring[0].rdt = ((hw->mac_type >= e1000_82543) ? E1000_RDT : E1000_82542_RDT);
1880 break;
1883 /* Enable 82543 Receive Checksum Offload for TCP and UDP */
1884 if (hw->mac_type >= e1000_82543) {
1885 rxcsum = E1000_READ_REG(hw, RXCSUM);
1886 if (adapter->rx_csum == TRUE) {
1887 rxcsum |= E1000_RXCSUM_TUOFL;
1889 /* Enable 82571 IPv4 payload checksum for UDP fragments
1890 * Must be used in conjunction with packet-split. */
1891 if ((hw->mac_type >= e1000_82571) &&
1892 (adapter->rx_ps_pages)) {
1893 rxcsum |= E1000_RXCSUM_IPPCSE;
1895 } else {
1896 rxcsum &= ~E1000_RXCSUM_TUOFL;
1897 /* don't need to clear IPPCSE as it defaults to 0 */
1899 E1000_WRITE_REG(hw, RXCSUM, rxcsum);
1902 /* Enable Receives */
1903 E1000_WRITE_REG(hw, RCTL, rctl);
1907 * e1000_free_tx_resources - Free Tx Resources per Queue
1908 * @adapter: board private structure
1909 * @tx_ring: Tx descriptor ring for a specific queue
1911 * Free all transmit software resources
1914 static void
1915 e1000_free_tx_resources(struct e1000_adapter *adapter,
1916 struct e1000_tx_ring *tx_ring)
1918 struct pci_dev *pdev = adapter->pdev;
1920 e1000_clean_tx_ring(adapter, tx_ring);
1922 vfree(tx_ring->buffer_info);
1923 tx_ring->buffer_info = NULL;
1925 pci_free_consistent(pdev, tx_ring->size, tx_ring->desc, tx_ring->dma);
1927 tx_ring->desc = NULL;
1931 * e1000_free_all_tx_resources - Free Tx Resources for All Queues
1932 * @adapter: board private structure
1934 * Free all transmit software resources
1937 void
1938 e1000_free_all_tx_resources(struct e1000_adapter *adapter)
1940 int i;
1942 for (i = 0; i < adapter->num_tx_queues; i++)
1943 e1000_free_tx_resources(adapter, &adapter->tx_ring[i]);
1946 static void
1947 e1000_unmap_and_free_tx_resource(struct e1000_adapter *adapter,
1948 struct e1000_buffer *buffer_info)
1950 if (buffer_info->dma) {
1951 pci_unmap_page(adapter->pdev,
1952 buffer_info->dma,
1953 buffer_info->length,
1954 PCI_DMA_TODEVICE);
1956 if (buffer_info->skb)
1957 dev_kfree_skb_any(buffer_info->skb);
1958 memset(buffer_info, 0, sizeof(struct e1000_buffer));
1962 * e1000_clean_tx_ring - Free Tx Buffers
1963 * @adapter: board private structure
1964 * @tx_ring: ring to be cleaned
1967 static void
1968 e1000_clean_tx_ring(struct e1000_adapter *adapter,
1969 struct e1000_tx_ring *tx_ring)
1971 struct e1000_buffer *buffer_info;
1972 unsigned long size;
1973 unsigned int i;
1975 /* Free all the Tx ring sk_buffs */
1977 for (i = 0; i < tx_ring->count; i++) {
1978 buffer_info = &tx_ring->buffer_info[i];
1979 e1000_unmap_and_free_tx_resource(adapter, buffer_info);
1982 size = sizeof(struct e1000_buffer) * tx_ring->count;
1983 memset(tx_ring->buffer_info, 0, size);
1985 /* Zero out the descriptor ring */
1987 memset(tx_ring->desc, 0, tx_ring->size);
1989 tx_ring->next_to_use = 0;
1990 tx_ring->next_to_clean = 0;
1991 tx_ring->last_tx_tso = 0;
1993 writel(0, adapter->hw.hw_addr + tx_ring->tdh);
1994 writel(0, adapter->hw.hw_addr + tx_ring->tdt);
1998 * e1000_clean_all_tx_rings - Free Tx Buffers for all queues
1999 * @adapter: board private structure
2002 static void
2003 e1000_clean_all_tx_rings(struct e1000_adapter *adapter)
2005 int i;
2007 for (i = 0; i < adapter->num_tx_queues; i++)
2008 e1000_clean_tx_ring(adapter, &adapter->tx_ring[i]);
2012 * e1000_free_rx_resources - Free Rx Resources
2013 * @adapter: board private structure
2014 * @rx_ring: ring to clean the resources from
2016 * Free all receive software resources
2019 static void
2020 e1000_free_rx_resources(struct e1000_adapter *adapter,
2021 struct e1000_rx_ring *rx_ring)
2023 struct pci_dev *pdev = adapter->pdev;
2025 e1000_clean_rx_ring(adapter, rx_ring);
2027 vfree(rx_ring->buffer_info);
2028 rx_ring->buffer_info = NULL;
2029 kfree(rx_ring->ps_page);
2030 rx_ring->ps_page = NULL;
2031 kfree(rx_ring->ps_page_dma);
2032 rx_ring->ps_page_dma = NULL;
2034 pci_free_consistent(pdev, rx_ring->size, rx_ring->desc, rx_ring->dma);
2036 rx_ring->desc = NULL;
2040 * e1000_free_all_rx_resources - Free Rx Resources for All Queues
2041 * @adapter: board private structure
2043 * Free all receive software resources
2046 void
2047 e1000_free_all_rx_resources(struct e1000_adapter *adapter)
2049 int i;
2051 for (i = 0; i < adapter->num_rx_queues; i++)
2052 e1000_free_rx_resources(adapter, &adapter->rx_ring[i]);
2056 * e1000_clean_rx_ring - Free Rx Buffers per Queue
2057 * @adapter: board private structure
2058 * @rx_ring: ring to free buffers from
2061 static void
2062 e1000_clean_rx_ring(struct e1000_adapter *adapter,
2063 struct e1000_rx_ring *rx_ring)
2065 struct e1000_buffer *buffer_info;
2066 struct e1000_ps_page *ps_page;
2067 struct e1000_ps_page_dma *ps_page_dma;
2068 struct pci_dev *pdev = adapter->pdev;
2069 unsigned long size;
2070 unsigned int i, j;
2072 /* Free all the Rx ring sk_buffs */
2073 for (i = 0; i < rx_ring->count; i++) {
2074 buffer_info = &rx_ring->buffer_info[i];
2075 if (buffer_info->skb) {
2076 pci_unmap_single(pdev,
2077 buffer_info->dma,
2078 buffer_info->length,
2079 PCI_DMA_FROMDEVICE);
2081 dev_kfree_skb(buffer_info->skb);
2082 buffer_info->skb = NULL;
2084 ps_page = &rx_ring->ps_page[i];
2085 ps_page_dma = &rx_ring->ps_page_dma[i];
2086 for (j = 0; j < adapter->rx_ps_pages; j++) {
2087 if (!ps_page->ps_page[j]) break;
2088 pci_unmap_page(pdev,
2089 ps_page_dma->ps_page_dma[j],
2090 PAGE_SIZE, PCI_DMA_FROMDEVICE);
2091 ps_page_dma->ps_page_dma[j] = 0;
2092 put_page(ps_page->ps_page[j]);
2093 ps_page->ps_page[j] = NULL;
2097 size = sizeof(struct e1000_buffer) * rx_ring->count;
2098 memset(rx_ring->buffer_info, 0, size);
2099 size = sizeof(struct e1000_ps_page) * rx_ring->count;
2100 memset(rx_ring->ps_page, 0, size);
2101 size = sizeof(struct e1000_ps_page_dma) * rx_ring->count;
2102 memset(rx_ring->ps_page_dma, 0, size);
2104 /* Zero out the descriptor ring */
2106 memset(rx_ring->desc, 0, rx_ring->size);
2108 rx_ring->next_to_clean = 0;
2109 rx_ring->next_to_use = 0;
2111 writel(0, adapter->hw.hw_addr + rx_ring->rdh);
2112 writel(0, adapter->hw.hw_addr + rx_ring->rdt);
2116 * e1000_clean_all_rx_rings - Free Rx Buffers for all queues
2117 * @adapter: board private structure
2120 static void
2121 e1000_clean_all_rx_rings(struct e1000_adapter *adapter)
2123 int i;
2125 for (i = 0; i < adapter->num_rx_queues; i++)
2126 e1000_clean_rx_ring(adapter, &adapter->rx_ring[i]);
2129 /* The 82542 2.0 (revision 2) needs to have the receive unit in reset
2130 * and memory write and invalidate disabled for certain operations
2132 static void
2133 e1000_enter_82542_rst(struct e1000_adapter *adapter)
2135 struct net_device *netdev = adapter->netdev;
2136 uint32_t rctl;
2138 e1000_pci_clear_mwi(&adapter->hw);
2140 rctl = E1000_READ_REG(&adapter->hw, RCTL);
2141 rctl |= E1000_RCTL_RST;
2142 E1000_WRITE_REG(&adapter->hw, RCTL, rctl);
2143 E1000_WRITE_FLUSH(&adapter->hw);
2144 mdelay(5);
2146 if (netif_running(netdev))
2147 e1000_clean_all_rx_rings(adapter);
2150 static void
2151 e1000_leave_82542_rst(struct e1000_adapter *adapter)
2153 struct net_device *netdev = adapter->netdev;
2154 uint32_t rctl;
2156 rctl = E1000_READ_REG(&adapter->hw, RCTL);
2157 rctl &= ~E1000_RCTL_RST;
2158 E1000_WRITE_REG(&adapter->hw, RCTL, rctl);
2159 E1000_WRITE_FLUSH(&adapter->hw);
2160 mdelay(5);
2162 if (adapter->hw.pci_cmd_word & PCI_COMMAND_INVALIDATE)
2163 e1000_pci_set_mwi(&adapter->hw);
2165 if (netif_running(netdev)) {
2166 /* No need to loop, because 82542 supports only 1 queue */
2167 struct e1000_rx_ring *ring = &adapter->rx_ring[0];
2168 e1000_configure_rx(adapter);
2169 adapter->alloc_rx_buf(adapter, ring, E1000_DESC_UNUSED(ring));
2174 * e1000_set_mac - Change the Ethernet Address of the NIC
2175 * @netdev: network interface device structure
2176 * @p: pointer to an address structure
2178 * Returns 0 on success, negative on failure
2181 static int
2182 e1000_set_mac(struct net_device *netdev, void *p)
2184 struct e1000_adapter *adapter = netdev_priv(netdev);
2185 struct sockaddr *addr = p;
2187 if (!is_valid_ether_addr(addr->sa_data))
2188 return -EADDRNOTAVAIL;
2190 /* 82542 2.0 needs to be in reset to write receive address registers */
2192 if (adapter->hw.mac_type == e1000_82542_rev2_0)
2193 e1000_enter_82542_rst(adapter);
2195 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
2196 memcpy(adapter->hw.mac_addr, addr->sa_data, netdev->addr_len);
2198 e1000_rar_set(&adapter->hw, adapter->hw.mac_addr, 0);
2200 /* With 82571 controllers, LAA may be overwritten (with the default)
2201 * due to controller reset from the other port. */
2202 if (adapter->hw.mac_type == e1000_82571) {
2203 /* activate the work around */
2204 adapter->hw.laa_is_present = 1;
2206 /* Hold a copy of the LAA in RAR[14] This is done so that
2207 * between the time RAR[0] gets clobbered and the time it
2208 * gets fixed (in e1000_watchdog), the actual LAA is in one
2209 * of the RARs and no incoming packets directed to this port
2210 * are dropped. Eventaully the LAA will be in RAR[0] and
2211 * RAR[14] */
2212 e1000_rar_set(&adapter->hw, adapter->hw.mac_addr,
2213 E1000_RAR_ENTRIES - 1);
2216 if (adapter->hw.mac_type == e1000_82542_rev2_0)
2217 e1000_leave_82542_rst(adapter);
2219 return 0;
2223 * e1000_set_multi - Multicast and Promiscuous mode set
2224 * @netdev: network interface device structure
2226 * The set_multi entry point is called whenever the multicast address
2227 * list or the network interface flags are updated. This routine is
2228 * responsible for configuring the hardware for proper multicast,
2229 * promiscuous mode, and all-multi behavior.
2232 static void
2233 e1000_set_multi(struct net_device *netdev)
2235 struct e1000_adapter *adapter = netdev_priv(netdev);
2236 struct e1000_hw *hw = &adapter->hw;
2237 struct dev_mc_list *mc_ptr;
2238 uint32_t rctl;
2239 uint32_t hash_value;
2240 int i, rar_entries = E1000_RAR_ENTRIES;
2241 int mta_reg_count = (hw->mac_type == e1000_ich8lan) ?
2242 E1000_NUM_MTA_REGISTERS_ICH8LAN :
2243 E1000_NUM_MTA_REGISTERS;
2245 if (adapter->hw.mac_type == e1000_ich8lan)
2246 rar_entries = E1000_RAR_ENTRIES_ICH8LAN;
2248 /* reserve RAR[14] for LAA over-write work-around */
2249 if (adapter->hw.mac_type == e1000_82571)
2250 rar_entries--;
2252 /* Check for Promiscuous and All Multicast modes */
2254 rctl = E1000_READ_REG(hw, RCTL);
2256 if (netdev->flags & IFF_PROMISC) {
2257 rctl |= (E1000_RCTL_UPE | E1000_RCTL_MPE);
2258 } else if (netdev->flags & IFF_ALLMULTI) {
2259 rctl |= E1000_RCTL_MPE;
2260 rctl &= ~E1000_RCTL_UPE;
2261 } else {
2262 rctl &= ~(E1000_RCTL_UPE | E1000_RCTL_MPE);
2265 E1000_WRITE_REG(hw, RCTL, rctl);
2267 /* 82542 2.0 needs to be in reset to write receive address registers */
2269 if (hw->mac_type == e1000_82542_rev2_0)
2270 e1000_enter_82542_rst(adapter);
2272 /* load the first 14 multicast address into the exact filters 1-14
2273 * RAR 0 is used for the station MAC adddress
2274 * if there are not 14 addresses, go ahead and clear the filters
2275 * -- with 82571 controllers only 0-13 entries are filled here
2277 mc_ptr = netdev->mc_list;
2279 for (i = 1; i < rar_entries; i++) {
2280 if (mc_ptr) {
2281 e1000_rar_set(hw, mc_ptr->dmi_addr, i);
2282 mc_ptr = mc_ptr->next;
2283 } else {
2284 E1000_WRITE_REG_ARRAY(hw, RA, i << 1, 0);
2285 E1000_WRITE_FLUSH(hw);
2286 E1000_WRITE_REG_ARRAY(hw, RA, (i << 1) + 1, 0);
2287 E1000_WRITE_FLUSH(hw);
2291 /* clear the old settings from the multicast hash table */
2293 for (i = 0; i < mta_reg_count; i++) {
2294 E1000_WRITE_REG_ARRAY(hw, MTA, i, 0);
2295 E1000_WRITE_FLUSH(hw);
2298 /* load any remaining addresses into the hash table */
2300 for (; mc_ptr; mc_ptr = mc_ptr->next) {
2301 hash_value = e1000_hash_mc_addr(hw, mc_ptr->dmi_addr);
2302 e1000_mta_set(hw, hash_value);
2305 if (hw->mac_type == e1000_82542_rev2_0)
2306 e1000_leave_82542_rst(adapter);
2309 /* Need to wait a few seconds after link up to get diagnostic information from
2310 * the phy */
2312 static void
2313 e1000_update_phy_info(unsigned long data)
2315 struct e1000_adapter *adapter = (struct e1000_adapter *) data;
2316 e1000_phy_get_info(&adapter->hw, &adapter->phy_info);
2320 * e1000_82547_tx_fifo_stall - Timer Call-back
2321 * @data: pointer to adapter cast into an unsigned long
2324 static void
2325 e1000_82547_tx_fifo_stall(unsigned long data)
2327 struct e1000_adapter *adapter = (struct e1000_adapter *) data;
2328 struct net_device *netdev = adapter->netdev;
2329 uint32_t tctl;
2331 if (atomic_read(&adapter->tx_fifo_stall)) {
2332 if ((E1000_READ_REG(&adapter->hw, TDT) ==
2333 E1000_READ_REG(&adapter->hw, TDH)) &&
2334 (E1000_READ_REG(&adapter->hw, TDFT) ==
2335 E1000_READ_REG(&adapter->hw, TDFH)) &&
2336 (E1000_READ_REG(&adapter->hw, TDFTS) ==
2337 E1000_READ_REG(&adapter->hw, TDFHS))) {
2338 tctl = E1000_READ_REG(&adapter->hw, TCTL);
2339 E1000_WRITE_REG(&adapter->hw, TCTL,
2340 tctl & ~E1000_TCTL_EN);
2341 E1000_WRITE_REG(&adapter->hw, TDFT,
2342 adapter->tx_head_addr);
2343 E1000_WRITE_REG(&adapter->hw, TDFH,
2344 adapter->tx_head_addr);
2345 E1000_WRITE_REG(&adapter->hw, TDFTS,
2346 adapter->tx_head_addr);
2347 E1000_WRITE_REG(&adapter->hw, TDFHS,
2348 adapter->tx_head_addr);
2349 E1000_WRITE_REG(&adapter->hw, TCTL, tctl);
2350 E1000_WRITE_FLUSH(&adapter->hw);
2352 adapter->tx_fifo_head = 0;
2353 atomic_set(&adapter->tx_fifo_stall, 0);
2354 netif_wake_queue(netdev);
2355 } else {
2356 mod_timer(&adapter->tx_fifo_stall_timer, jiffies + 1);
2362 * e1000_watchdog - Timer Call-back
2363 * @data: pointer to adapter cast into an unsigned long
2365 static void
2366 e1000_watchdog(unsigned long data)
2368 struct e1000_adapter *adapter = (struct e1000_adapter *) data;
2369 struct net_device *netdev = adapter->netdev;
2370 struct e1000_tx_ring *txdr = adapter->tx_ring;
2371 uint32_t link, tctl;
2372 int32_t ret_val;
2374 ret_val = e1000_check_for_link(&adapter->hw);
2375 if ((ret_val == E1000_ERR_PHY) &&
2376 (adapter->hw.phy_type == e1000_phy_igp_3) &&
2377 (E1000_READ_REG(&adapter->hw, CTRL) & E1000_PHY_CTRL_GBE_DISABLE)) {
2378 /* See e1000_kumeran_lock_loss_workaround() */
2379 DPRINTK(LINK, INFO,
2380 "Gigabit has been disabled, downgrading speed\n");
2382 if (adapter->hw.mac_type == e1000_82573) {
2383 e1000_enable_tx_pkt_filtering(&adapter->hw);
2384 if (adapter->mng_vlan_id != adapter->hw.mng_cookie.vlan_id)
2385 e1000_update_mng_vlan(adapter);
2388 if ((adapter->hw.media_type == e1000_media_type_internal_serdes) &&
2389 !(E1000_READ_REG(&adapter->hw, TXCW) & E1000_TXCW_ANE))
2390 link = !adapter->hw.serdes_link_down;
2391 else
2392 link = E1000_READ_REG(&adapter->hw, STATUS) & E1000_STATUS_LU;
2394 if (link) {
2395 if (!netif_carrier_ok(netdev)) {
2396 boolean_t txb2b = 1;
2397 e1000_get_speed_and_duplex(&adapter->hw,
2398 &adapter->link_speed,
2399 &adapter->link_duplex);
2401 DPRINTK(LINK, INFO, "NIC Link is Up %d Mbps %s\n",
2402 adapter->link_speed,
2403 adapter->link_duplex == FULL_DUPLEX ?
2404 "Full Duplex" : "Half Duplex");
2406 /* tweak tx_queue_len according to speed/duplex
2407 * and adjust the timeout factor */
2408 netdev->tx_queue_len = adapter->tx_queue_len;
2409 adapter->tx_timeout_factor = 1;
2410 switch (adapter->link_speed) {
2411 case SPEED_10:
2412 txb2b = 0;
2413 netdev->tx_queue_len = 10;
2414 adapter->tx_timeout_factor = 8;
2415 break;
2416 case SPEED_100:
2417 txb2b = 0;
2418 netdev->tx_queue_len = 100;
2419 /* maybe add some timeout factor ? */
2420 break;
2423 if ((adapter->hw.mac_type == e1000_82571 ||
2424 adapter->hw.mac_type == e1000_82572) &&
2425 txb2b == 0) {
2426 #define SPEED_MODE_BIT (1 << 21)
2427 uint32_t tarc0;
2428 tarc0 = E1000_READ_REG(&adapter->hw, TARC0);
2429 tarc0 &= ~SPEED_MODE_BIT;
2430 E1000_WRITE_REG(&adapter->hw, TARC0, tarc0);
2433 #ifdef NETIF_F_TSO
2434 /* disable TSO for pcie and 10/100 speeds, to avoid
2435 * some hardware issues */
2436 if (!adapter->tso_force &&
2437 adapter->hw.bus_type == e1000_bus_type_pci_express){
2438 switch (adapter->link_speed) {
2439 case SPEED_10:
2440 case SPEED_100:
2441 DPRINTK(PROBE,INFO,
2442 "10/100 speed: disabling TSO\n");
2443 netdev->features &= ~NETIF_F_TSO;
2444 break;
2445 case SPEED_1000:
2446 netdev->features |= NETIF_F_TSO;
2447 break;
2448 default:
2449 /* oops */
2450 break;
2453 #endif
2455 /* enable transmits in the hardware, need to do this
2456 * after setting TARC0 */
2457 tctl = E1000_READ_REG(&adapter->hw, TCTL);
2458 tctl |= E1000_TCTL_EN;
2459 E1000_WRITE_REG(&adapter->hw, TCTL, tctl);
2461 netif_carrier_on(netdev);
2462 netif_wake_queue(netdev);
2463 mod_timer(&adapter->phy_info_timer, jiffies + 2 * HZ);
2464 adapter->smartspeed = 0;
2466 } else {
2467 if (netif_carrier_ok(netdev)) {
2468 adapter->link_speed = 0;
2469 adapter->link_duplex = 0;
2470 DPRINTK(LINK, INFO, "NIC Link is Down\n");
2471 netif_carrier_off(netdev);
2472 netif_stop_queue(netdev);
2473 mod_timer(&adapter->phy_info_timer, jiffies + 2 * HZ);
2475 /* 80003ES2LAN workaround--
2476 * For packet buffer work-around on link down event;
2477 * disable receives in the ISR and
2478 * reset device here in the watchdog
2480 if (adapter->hw.mac_type == e1000_80003es2lan)
2481 /* reset device */
2482 schedule_work(&adapter->reset_task);
2485 e1000_smartspeed(adapter);
2488 e1000_update_stats(adapter);
2490 adapter->hw.tx_packet_delta = adapter->stats.tpt - adapter->tpt_old;
2491 adapter->tpt_old = adapter->stats.tpt;
2492 adapter->hw.collision_delta = adapter->stats.colc - adapter->colc_old;
2493 adapter->colc_old = adapter->stats.colc;
2495 adapter->gorcl = adapter->stats.gorcl - adapter->gorcl_old;
2496 adapter->gorcl_old = adapter->stats.gorcl;
2497 adapter->gotcl = adapter->stats.gotcl - adapter->gotcl_old;
2498 adapter->gotcl_old = adapter->stats.gotcl;
2500 e1000_update_adaptive(&adapter->hw);
2502 if (!netif_carrier_ok(netdev)) {
2503 if (E1000_DESC_UNUSED(txdr) + 1 < txdr->count) {
2504 /* We've lost link, so the controller stops DMA,
2505 * but we've got queued Tx work that's never going
2506 * to get done, so reset controller to flush Tx.
2507 * (Do the reset outside of interrupt context). */
2508 adapter->tx_timeout_count++;
2509 schedule_work(&adapter->reset_task);
2513 /* Dynamic mode for Interrupt Throttle Rate (ITR) */
2514 if (adapter->hw.mac_type >= e1000_82540 && adapter->itr == 1) {
2515 /* Symmetric Tx/Rx gets a reduced ITR=2000; Total
2516 * asymmetrical Tx or Rx gets ITR=8000; everyone
2517 * else is between 2000-8000. */
2518 uint32_t goc = (adapter->gotcl + adapter->gorcl) / 10000;
2519 uint32_t dif = (adapter->gotcl > adapter->gorcl ?
2520 adapter->gotcl - adapter->gorcl :
2521 adapter->gorcl - adapter->gotcl) / 10000;
2522 uint32_t itr = goc > 0 ? (dif * 6000 / goc + 2000) : 8000;
2523 E1000_WRITE_REG(&adapter->hw, ITR, 1000000000 / (itr * 256));
2526 /* Cause software interrupt to ensure rx ring is cleaned */
2527 E1000_WRITE_REG(&adapter->hw, ICS, E1000_ICS_RXDMT0);
2529 /* Force detection of hung controller every watchdog period */
2530 adapter->detect_tx_hung = TRUE;
2532 /* With 82571 controllers, LAA may be overwritten due to controller
2533 * reset from the other port. Set the appropriate LAA in RAR[0] */
2534 if (adapter->hw.mac_type == e1000_82571 && adapter->hw.laa_is_present)
2535 e1000_rar_set(&adapter->hw, adapter->hw.mac_addr, 0);
2537 /* Reset the timer */
2538 mod_timer(&adapter->watchdog_timer, jiffies + 2 * HZ);
2541 #define E1000_TX_FLAGS_CSUM 0x00000001
2542 #define E1000_TX_FLAGS_VLAN 0x00000002
2543 #define E1000_TX_FLAGS_TSO 0x00000004
2544 #define E1000_TX_FLAGS_IPV4 0x00000008
2545 #define E1000_TX_FLAGS_VLAN_MASK 0xffff0000
2546 #define E1000_TX_FLAGS_VLAN_SHIFT 16
2548 static int
2549 e1000_tso(struct e1000_adapter *adapter, struct e1000_tx_ring *tx_ring,
2550 struct sk_buff *skb)
2552 #ifdef NETIF_F_TSO
2553 struct e1000_context_desc *context_desc;
2554 struct e1000_buffer *buffer_info;
2555 unsigned int i;
2556 uint32_t cmd_length = 0;
2557 uint16_t ipcse = 0, tucse, mss;
2558 uint8_t ipcss, ipcso, tucss, tucso, hdr_len;
2559 int err;
2561 if (skb_is_gso(skb)) {
2562 if (skb_header_cloned(skb)) {
2563 err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2564 if (err)
2565 return err;
2568 hdr_len = ((skb->h.raw - skb->data) + (skb->h.th->doff << 2));
2569 mss = skb_shinfo(skb)->gso_size;
2570 if (skb->protocol == htons(ETH_P_IP)) {
2571 skb->nh.iph->tot_len = 0;
2572 skb->nh.iph->check = 0;
2573 skb->h.th->check =
2574 ~csum_tcpudp_magic(skb->nh.iph->saddr,
2575 skb->nh.iph->daddr,
2577 IPPROTO_TCP,
2579 cmd_length = E1000_TXD_CMD_IP;
2580 ipcse = skb->h.raw - skb->data - 1;
2581 #ifdef NETIF_F_TSO_IPV6
2582 } else if (skb->protocol == htons(ETH_P_IPV6)) {
2583 skb->nh.ipv6h->payload_len = 0;
2584 skb->h.th->check =
2585 ~csum_ipv6_magic(&skb->nh.ipv6h->saddr,
2586 &skb->nh.ipv6h->daddr,
2588 IPPROTO_TCP,
2590 ipcse = 0;
2591 #endif
2593 ipcss = skb->nh.raw - skb->data;
2594 ipcso = (void *)&(skb->nh.iph->check) - (void *)skb->data;
2595 tucss = skb->h.raw - skb->data;
2596 tucso = (void *)&(skb->h.th->check) - (void *)skb->data;
2597 tucse = 0;
2599 cmd_length |= (E1000_TXD_CMD_DEXT | E1000_TXD_CMD_TSE |
2600 E1000_TXD_CMD_TCP | (skb->len - (hdr_len)));
2602 i = tx_ring->next_to_use;
2603 context_desc = E1000_CONTEXT_DESC(*tx_ring, i);
2604 buffer_info = &tx_ring->buffer_info[i];
2606 context_desc->lower_setup.ip_fields.ipcss = ipcss;
2607 context_desc->lower_setup.ip_fields.ipcso = ipcso;
2608 context_desc->lower_setup.ip_fields.ipcse = cpu_to_le16(ipcse);
2609 context_desc->upper_setup.tcp_fields.tucss = tucss;
2610 context_desc->upper_setup.tcp_fields.tucso = tucso;
2611 context_desc->upper_setup.tcp_fields.tucse = cpu_to_le16(tucse);
2612 context_desc->tcp_seg_setup.fields.mss = cpu_to_le16(mss);
2613 context_desc->tcp_seg_setup.fields.hdr_len = hdr_len;
2614 context_desc->cmd_and_length = cpu_to_le32(cmd_length);
2616 buffer_info->time_stamp = jiffies;
2618 if (++i == tx_ring->count) i = 0;
2619 tx_ring->next_to_use = i;
2621 return TRUE;
2623 #endif
2625 return FALSE;
2628 static boolean_t
2629 e1000_tx_csum(struct e1000_adapter *adapter, struct e1000_tx_ring *tx_ring,
2630 struct sk_buff *skb)
2632 struct e1000_context_desc *context_desc;
2633 struct e1000_buffer *buffer_info;
2634 unsigned int i;
2635 uint8_t css;
2637 if (likely(skb->ip_summed == CHECKSUM_PARTIAL)) {
2638 css = skb->h.raw - skb->data;
2640 i = tx_ring->next_to_use;
2641 buffer_info = &tx_ring->buffer_info[i];
2642 context_desc = E1000_CONTEXT_DESC(*tx_ring, i);
2644 context_desc->upper_setup.tcp_fields.tucss = css;
2645 context_desc->upper_setup.tcp_fields.tucso = css + skb->csum;
2646 context_desc->upper_setup.tcp_fields.tucse = 0;
2647 context_desc->tcp_seg_setup.data = 0;
2648 context_desc->cmd_and_length = cpu_to_le32(E1000_TXD_CMD_DEXT);
2650 buffer_info->time_stamp = jiffies;
2652 if (unlikely(++i == tx_ring->count)) i = 0;
2653 tx_ring->next_to_use = i;
2655 return TRUE;
2658 return FALSE;
2661 #define E1000_MAX_TXD_PWR 12
2662 #define E1000_MAX_DATA_PER_TXD (1<<E1000_MAX_TXD_PWR)
2664 static int
2665 e1000_tx_map(struct e1000_adapter *adapter, struct e1000_tx_ring *tx_ring,
2666 struct sk_buff *skb, unsigned int first, unsigned int max_per_txd,
2667 unsigned int nr_frags, unsigned int mss)
2669 struct e1000_buffer *buffer_info;
2670 unsigned int len = skb->len;
2671 unsigned int offset = 0, size, count = 0, i;
2672 unsigned int f;
2673 len -= skb->data_len;
2675 i = tx_ring->next_to_use;
2677 while (len) {
2678 buffer_info = &tx_ring->buffer_info[i];
2679 size = min(len, max_per_txd);
2680 #ifdef NETIF_F_TSO
2681 /* Workaround for Controller erratum --
2682 * descriptor for non-tso packet in a linear SKB that follows a
2683 * tso gets written back prematurely before the data is fully
2684 * DMA'd to the controller */
2685 if (!skb->data_len && tx_ring->last_tx_tso &&
2686 !skb_is_gso(skb)) {
2687 tx_ring->last_tx_tso = 0;
2688 size -= 4;
2691 /* Workaround for premature desc write-backs
2692 * in TSO mode. Append 4-byte sentinel desc */
2693 if (unlikely(mss && !nr_frags && size == len && size > 8))
2694 size -= 4;
2695 #endif
2696 /* work-around for errata 10 and it applies
2697 * to all controllers in PCI-X mode
2698 * The fix is to make sure that the first descriptor of a
2699 * packet is smaller than 2048 - 16 - 16 (or 2016) bytes
2701 if (unlikely((adapter->hw.bus_type == e1000_bus_type_pcix) &&
2702 (size > 2015) && count == 0))
2703 size = 2015;
2705 /* Workaround for potential 82544 hang in PCI-X. Avoid
2706 * terminating buffers within evenly-aligned dwords. */
2707 if (unlikely(adapter->pcix_82544 &&
2708 !((unsigned long)(skb->data + offset + size - 1) & 4) &&
2709 size > 4))
2710 size -= 4;
2712 buffer_info->length = size;
2713 buffer_info->dma =
2714 pci_map_single(adapter->pdev,
2715 skb->data + offset,
2716 size,
2717 PCI_DMA_TODEVICE);
2718 buffer_info->time_stamp = jiffies;
2720 len -= size;
2721 offset += size;
2722 count++;
2723 if (unlikely(++i == tx_ring->count)) i = 0;
2726 for (f = 0; f < nr_frags; f++) {
2727 struct skb_frag_struct *frag;
2729 frag = &skb_shinfo(skb)->frags[f];
2730 len = frag->size;
2731 offset = frag->page_offset;
2733 while (len) {
2734 buffer_info = &tx_ring->buffer_info[i];
2735 size = min(len, max_per_txd);
2736 #ifdef NETIF_F_TSO
2737 /* Workaround for premature desc write-backs
2738 * in TSO mode. Append 4-byte sentinel desc */
2739 if (unlikely(mss && f == (nr_frags-1) && size == len && size > 8))
2740 size -= 4;
2741 #endif
2742 /* Workaround for potential 82544 hang in PCI-X.
2743 * Avoid terminating buffers within evenly-aligned
2744 * dwords. */
2745 if (unlikely(adapter->pcix_82544 &&
2746 !((unsigned long)(frag->page+offset+size-1) & 4) &&
2747 size > 4))
2748 size -= 4;
2750 buffer_info->length = size;
2751 buffer_info->dma =
2752 pci_map_page(adapter->pdev,
2753 frag->page,
2754 offset,
2755 size,
2756 PCI_DMA_TODEVICE);
2757 buffer_info->time_stamp = jiffies;
2759 len -= size;
2760 offset += size;
2761 count++;
2762 if (unlikely(++i == tx_ring->count)) i = 0;
2766 i = (i == 0) ? tx_ring->count - 1 : i - 1;
2767 tx_ring->buffer_info[i].skb = skb;
2768 tx_ring->buffer_info[first].next_to_watch = i;
2770 return count;
2773 static void
2774 e1000_tx_queue(struct e1000_adapter *adapter, struct e1000_tx_ring *tx_ring,
2775 int tx_flags, int count)
2777 struct e1000_tx_desc *tx_desc = NULL;
2778 struct e1000_buffer *buffer_info;
2779 uint32_t txd_upper = 0, txd_lower = E1000_TXD_CMD_IFCS;
2780 unsigned int i;
2782 if (likely(tx_flags & E1000_TX_FLAGS_TSO)) {
2783 txd_lower |= E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D |
2784 E1000_TXD_CMD_TSE;
2785 txd_upper |= E1000_TXD_POPTS_TXSM << 8;
2787 if (likely(tx_flags & E1000_TX_FLAGS_IPV4))
2788 txd_upper |= E1000_TXD_POPTS_IXSM << 8;
2791 if (likely(tx_flags & E1000_TX_FLAGS_CSUM)) {
2792 txd_lower |= E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D;
2793 txd_upper |= E1000_TXD_POPTS_TXSM << 8;
2796 if (unlikely(tx_flags & E1000_TX_FLAGS_VLAN)) {
2797 txd_lower |= E1000_TXD_CMD_VLE;
2798 txd_upper |= (tx_flags & E1000_TX_FLAGS_VLAN_MASK);
2801 i = tx_ring->next_to_use;
2803 while (count--) {
2804 buffer_info = &tx_ring->buffer_info[i];
2805 tx_desc = E1000_TX_DESC(*tx_ring, i);
2806 tx_desc->buffer_addr = cpu_to_le64(buffer_info->dma);
2807 tx_desc->lower.data =
2808 cpu_to_le32(txd_lower | buffer_info->length);
2809 tx_desc->upper.data = cpu_to_le32(txd_upper);
2810 if (unlikely(++i == tx_ring->count)) i = 0;
2813 tx_desc->lower.data |= cpu_to_le32(adapter->txd_cmd);
2815 /* Force memory writes to complete before letting h/w
2816 * know there are new descriptors to fetch. (Only
2817 * applicable for weak-ordered memory model archs,
2818 * such as IA-64). */
2819 wmb();
2821 tx_ring->next_to_use = i;
2822 writel(i, adapter->hw.hw_addr + tx_ring->tdt);
2826 * 82547 workaround to avoid controller hang in half-duplex environment.
2827 * The workaround is to avoid queuing a large packet that would span
2828 * the internal Tx FIFO ring boundary by notifying the stack to resend
2829 * the packet at a later time. This gives the Tx FIFO an opportunity to
2830 * flush all packets. When that occurs, we reset the Tx FIFO pointers
2831 * to the beginning of the Tx FIFO.
2834 #define E1000_FIFO_HDR 0x10
2835 #define E1000_82547_PAD_LEN 0x3E0
2837 static int
2838 e1000_82547_fifo_workaround(struct e1000_adapter *adapter, struct sk_buff *skb)
2840 uint32_t fifo_space = adapter->tx_fifo_size - adapter->tx_fifo_head;
2841 uint32_t skb_fifo_len = skb->len + E1000_FIFO_HDR;
2843 E1000_ROUNDUP(skb_fifo_len, E1000_FIFO_HDR);
2845 if (adapter->link_duplex != HALF_DUPLEX)
2846 goto no_fifo_stall_required;
2848 if (atomic_read(&adapter->tx_fifo_stall))
2849 return 1;
2851 if (skb_fifo_len >= (E1000_82547_PAD_LEN + fifo_space)) {
2852 atomic_set(&adapter->tx_fifo_stall, 1);
2853 return 1;
2856 no_fifo_stall_required:
2857 adapter->tx_fifo_head += skb_fifo_len;
2858 if (adapter->tx_fifo_head >= adapter->tx_fifo_size)
2859 adapter->tx_fifo_head -= adapter->tx_fifo_size;
2860 return 0;
2863 #define MINIMUM_DHCP_PACKET_SIZE 282
2864 static int
2865 e1000_transfer_dhcp_info(struct e1000_adapter *adapter, struct sk_buff *skb)
2867 struct e1000_hw *hw = &adapter->hw;
2868 uint16_t length, offset;
2869 if (vlan_tx_tag_present(skb)) {
2870 if (!((vlan_tx_tag_get(skb) == adapter->hw.mng_cookie.vlan_id) &&
2871 ( adapter->hw.mng_cookie.status &
2872 E1000_MNG_DHCP_COOKIE_STATUS_VLAN_SUPPORT)) )
2873 return 0;
2875 if (skb->len > MINIMUM_DHCP_PACKET_SIZE) {
2876 struct ethhdr *eth = (struct ethhdr *) skb->data;
2877 if ((htons(ETH_P_IP) == eth->h_proto)) {
2878 const struct iphdr *ip =
2879 (struct iphdr *)((uint8_t *)skb->data+14);
2880 if (IPPROTO_UDP == ip->protocol) {
2881 struct udphdr *udp =
2882 (struct udphdr *)((uint8_t *)ip +
2883 (ip->ihl << 2));
2884 if (ntohs(udp->dest) == 67) {
2885 offset = (uint8_t *)udp + 8 - skb->data;
2886 length = skb->len - offset;
2888 return e1000_mng_write_dhcp_info(hw,
2889 (uint8_t *)udp + 8,
2890 length);
2895 return 0;
2898 static int __e1000_maybe_stop_tx(struct net_device *netdev, int size)
2900 struct e1000_adapter *adapter = netdev_priv(netdev);
2901 struct e1000_tx_ring *tx_ring = adapter->tx_ring;
2903 netif_stop_queue(netdev);
2904 /* Herbert's original patch had:
2905 * smp_mb__after_netif_stop_queue();
2906 * but since that doesn't exist yet, just open code it. */
2907 smp_mb();
2909 /* We need to check again in a case another CPU has just
2910 * made room available. */
2911 if (likely(E1000_DESC_UNUSED(tx_ring) < size))
2912 return -EBUSY;
2914 /* A reprieve! */
2915 netif_start_queue(netdev);
2916 return 0;
2919 static int e1000_maybe_stop_tx(struct net_device *netdev,
2920 struct e1000_tx_ring *tx_ring, int size)
2922 if (likely(E1000_DESC_UNUSED(tx_ring) >= size))
2923 return 0;
2924 return __e1000_maybe_stop_tx(netdev, size);
2927 #define TXD_USE_COUNT(S, X) (((S) >> (X)) + 1 )
2928 static int
2929 e1000_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
2931 struct e1000_adapter *adapter = netdev_priv(netdev);
2932 struct e1000_tx_ring *tx_ring;
2933 unsigned int first, max_per_txd = E1000_MAX_DATA_PER_TXD;
2934 unsigned int max_txd_pwr = E1000_MAX_TXD_PWR;
2935 unsigned int tx_flags = 0;
2936 unsigned int len = skb->len;
2937 unsigned long flags;
2938 unsigned int nr_frags = 0;
2939 unsigned int mss = 0;
2940 int count = 0;
2941 int tso;
2942 unsigned int f;
2943 len -= skb->data_len;
2945 /* This goes back to the question of how to logically map a tx queue
2946 * to a flow. Right now, performance is impacted slightly negatively
2947 * if using multiple tx queues. If the stack breaks away from a
2948 * single qdisc implementation, we can look at this again. */
2949 tx_ring = adapter->tx_ring;
2951 if (unlikely(skb->len <= 0)) {
2952 dev_kfree_skb_any(skb);
2953 return NETDEV_TX_OK;
2956 #ifdef NETIF_F_TSO
2957 mss = skb_shinfo(skb)->gso_size;
2958 /* The controller does a simple calculation to
2959 * make sure there is enough room in the FIFO before
2960 * initiating the DMA for each buffer. The calc is:
2961 * 4 = ceil(buffer len/mss). To make sure we don't
2962 * overrun the FIFO, adjust the max buffer len if mss
2963 * drops. */
2964 if (mss) {
2965 uint8_t hdr_len;
2966 max_per_txd = min(mss << 2, max_per_txd);
2967 max_txd_pwr = fls(max_per_txd) - 1;
2969 /* TSO Workaround for 82571/2/3 Controllers -- if skb->data
2970 * points to just header, pull a few bytes of payload from
2971 * frags into skb->data */
2972 hdr_len = ((skb->h.raw - skb->data) + (skb->h.th->doff << 2));
2973 if (skb->data_len && (hdr_len == (skb->len - skb->data_len))) {
2974 switch (adapter->hw.mac_type) {
2975 unsigned int pull_size;
2976 case e1000_82571:
2977 case e1000_82572:
2978 case e1000_82573:
2979 case e1000_ich8lan:
2980 pull_size = min((unsigned int)4, skb->data_len);
2981 if (!__pskb_pull_tail(skb, pull_size)) {
2982 DPRINTK(DRV, ERR,
2983 "__pskb_pull_tail failed.\n");
2984 dev_kfree_skb_any(skb);
2985 return NETDEV_TX_OK;
2987 len = skb->len - skb->data_len;
2988 break;
2989 default:
2990 /* do nothing */
2991 break;
2996 /* reserve a descriptor for the offload context */
2997 if ((mss) || (skb->ip_summed == CHECKSUM_PARTIAL))
2998 count++;
2999 count++;
3000 #else
3001 if (skb->ip_summed == CHECKSUM_PARTIAL)
3002 count++;
3003 #endif
3005 #ifdef NETIF_F_TSO
3006 /* Controller Erratum workaround */
3007 if (!skb->data_len && tx_ring->last_tx_tso && !skb_is_gso(skb))
3008 count++;
3009 #endif
3011 count += TXD_USE_COUNT(len, max_txd_pwr);
3013 if (adapter->pcix_82544)
3014 count++;
3016 /* work-around for errata 10 and it applies to all controllers
3017 * in PCI-X mode, so add one more descriptor to the count
3019 if (unlikely((adapter->hw.bus_type == e1000_bus_type_pcix) &&
3020 (len > 2015)))
3021 count++;
3023 nr_frags = skb_shinfo(skb)->nr_frags;
3024 for (f = 0; f < nr_frags; f++)
3025 count += TXD_USE_COUNT(skb_shinfo(skb)->frags[f].size,
3026 max_txd_pwr);
3027 if (adapter->pcix_82544)
3028 count += nr_frags;
3031 if (adapter->hw.tx_pkt_filtering &&
3032 (adapter->hw.mac_type == e1000_82573))
3033 e1000_transfer_dhcp_info(adapter, skb);
3035 local_irq_save(flags);
3036 if (!spin_trylock(&tx_ring->tx_lock)) {
3037 /* Collision - tell upper layer to requeue */
3038 local_irq_restore(flags);
3039 return NETDEV_TX_LOCKED;
3042 /* need: count + 2 desc gap to keep tail from touching
3043 * head, otherwise try next time */
3044 if (unlikely(e1000_maybe_stop_tx(netdev, tx_ring, count + 2))) {
3045 spin_unlock_irqrestore(&tx_ring->tx_lock, flags);
3046 return NETDEV_TX_BUSY;
3049 if (unlikely(adapter->hw.mac_type == e1000_82547)) {
3050 if (unlikely(e1000_82547_fifo_workaround(adapter, skb))) {
3051 netif_stop_queue(netdev);
3052 mod_timer(&adapter->tx_fifo_stall_timer, jiffies);
3053 spin_unlock_irqrestore(&tx_ring->tx_lock, flags);
3054 return NETDEV_TX_BUSY;
3058 if (unlikely(adapter->vlgrp && vlan_tx_tag_present(skb))) {
3059 tx_flags |= E1000_TX_FLAGS_VLAN;
3060 tx_flags |= (vlan_tx_tag_get(skb) << E1000_TX_FLAGS_VLAN_SHIFT);
3063 first = tx_ring->next_to_use;
3065 tso = e1000_tso(adapter, tx_ring, skb);
3066 if (tso < 0) {
3067 dev_kfree_skb_any(skb);
3068 spin_unlock_irqrestore(&tx_ring->tx_lock, flags);
3069 return NETDEV_TX_OK;
3072 if (likely(tso)) {
3073 tx_ring->last_tx_tso = 1;
3074 tx_flags |= E1000_TX_FLAGS_TSO;
3075 } else if (likely(e1000_tx_csum(adapter, tx_ring, skb)))
3076 tx_flags |= E1000_TX_FLAGS_CSUM;
3078 /* Old method was to assume IPv4 packet by default if TSO was enabled.
3079 * 82571 hardware supports TSO capabilities for IPv6 as well...
3080 * no longer assume, we must. */
3081 if (likely(skb->protocol == htons(ETH_P_IP)))
3082 tx_flags |= E1000_TX_FLAGS_IPV4;
3084 e1000_tx_queue(adapter, tx_ring, tx_flags,
3085 e1000_tx_map(adapter, tx_ring, skb, first,
3086 max_per_txd, nr_frags, mss));
3088 netdev->trans_start = jiffies;
3090 /* Make sure there is space in the ring for the next send. */
3091 e1000_maybe_stop_tx(netdev, tx_ring, MAX_SKB_FRAGS + 2);
3093 spin_unlock_irqrestore(&tx_ring->tx_lock, flags);
3094 return NETDEV_TX_OK;
3098 * e1000_tx_timeout - Respond to a Tx Hang
3099 * @netdev: network interface device structure
3102 static void
3103 e1000_tx_timeout(struct net_device *netdev)
3105 struct e1000_adapter *adapter = netdev_priv(netdev);
3107 /* Do the reset outside of interrupt context */
3108 adapter->tx_timeout_count++;
3109 schedule_work(&adapter->reset_task);
3112 static void
3113 e1000_reset_task(struct net_device *netdev)
3115 struct e1000_adapter *adapter = netdev_priv(netdev);
3117 e1000_reinit_locked(adapter);
3121 * e1000_get_stats - Get System Network Statistics
3122 * @netdev: network interface device structure
3124 * Returns the address of the device statistics structure.
3125 * The statistics are actually updated from the timer callback.
3128 static struct net_device_stats *
3129 e1000_get_stats(struct net_device *netdev)
3131 struct e1000_adapter *adapter = netdev_priv(netdev);
3133 /* only return the current stats */
3134 return &adapter->net_stats;
3138 * e1000_change_mtu - Change the Maximum Transfer Unit
3139 * @netdev: network interface device structure
3140 * @new_mtu: new value for maximum frame size
3142 * Returns 0 on success, negative on failure
3145 static int
3146 e1000_change_mtu(struct net_device *netdev, int new_mtu)
3148 struct e1000_adapter *adapter = netdev_priv(netdev);
3149 int max_frame = new_mtu + ENET_HEADER_SIZE + ETHERNET_FCS_SIZE;
3150 uint16_t eeprom_data = 0;
3152 if ((max_frame < MINIMUM_ETHERNET_FRAME_SIZE) ||
3153 (max_frame > MAX_JUMBO_FRAME_SIZE)) {
3154 DPRINTK(PROBE, ERR, "Invalid MTU setting\n");
3155 return -EINVAL;
3158 /* Adapter-specific max frame size limits. */
3159 switch (adapter->hw.mac_type) {
3160 case e1000_undefined ... e1000_82542_rev2_1:
3161 case e1000_ich8lan:
3162 if (max_frame > MAXIMUM_ETHERNET_FRAME_SIZE) {
3163 DPRINTK(PROBE, ERR, "Jumbo Frames not supported.\n");
3164 return -EINVAL;
3166 break;
3167 case e1000_82573:
3168 /* Jumbo Frames not supported if:
3169 * - this is not an 82573L device
3170 * - ASPM is enabled in any way (0x1A bits 3:2) */
3171 e1000_read_eeprom(&adapter->hw, EEPROM_INIT_3GIO_3, 1,
3172 &eeprom_data);
3173 if ((adapter->hw.device_id != E1000_DEV_ID_82573L) ||
3174 (eeprom_data & EEPROM_WORD1A_ASPM_MASK)) {
3175 if (max_frame > MAXIMUM_ETHERNET_FRAME_SIZE) {
3176 DPRINTK(PROBE, ERR,
3177 "Jumbo Frames not supported.\n");
3178 return -EINVAL;
3180 break;
3182 /* ERT will be enabled later to enable wire speed receives */
3184 /* fall through to get support */
3185 case e1000_82571:
3186 case e1000_82572:
3187 case e1000_80003es2lan:
3188 #define MAX_STD_JUMBO_FRAME_SIZE 9234
3189 if (max_frame > MAX_STD_JUMBO_FRAME_SIZE) {
3190 DPRINTK(PROBE, ERR, "MTU > 9216 not supported.\n");
3191 return -EINVAL;
3193 break;
3194 default:
3195 /* Capable of supporting up to MAX_JUMBO_FRAME_SIZE limit. */
3196 break;
3199 /* NOTE: netdev_alloc_skb reserves 16 bytes, and typically NET_IP_ALIGN
3200 * means we reserve 2 more, this pushes us to allocate from the next
3201 * larger slab size
3202 * i.e. RXBUFFER_2048 --> size-4096 slab */
3204 if (max_frame <= E1000_RXBUFFER_256)
3205 adapter->rx_buffer_len = E1000_RXBUFFER_256;
3206 else if (max_frame <= E1000_RXBUFFER_512)
3207 adapter->rx_buffer_len = E1000_RXBUFFER_512;
3208 else if (max_frame <= E1000_RXBUFFER_1024)
3209 adapter->rx_buffer_len = E1000_RXBUFFER_1024;
3210 else if (max_frame <= E1000_RXBUFFER_2048)
3211 adapter->rx_buffer_len = E1000_RXBUFFER_2048;
3212 else if (max_frame <= E1000_RXBUFFER_4096)
3213 adapter->rx_buffer_len = E1000_RXBUFFER_4096;
3214 else if (max_frame <= E1000_RXBUFFER_8192)
3215 adapter->rx_buffer_len = E1000_RXBUFFER_8192;
3216 else if (max_frame <= E1000_RXBUFFER_16384)
3217 adapter->rx_buffer_len = E1000_RXBUFFER_16384;
3219 /* adjust allocation if LPE protects us, and we aren't using SBP */
3220 if (!adapter->hw.tbi_compatibility_on &&
3221 ((max_frame == MAXIMUM_ETHERNET_FRAME_SIZE) ||
3222 (max_frame == MAXIMUM_ETHERNET_VLAN_SIZE)))
3223 adapter->rx_buffer_len = MAXIMUM_ETHERNET_VLAN_SIZE;
3225 netdev->mtu = new_mtu;
3227 if (netif_running(netdev))
3228 e1000_reinit_locked(adapter);
3230 adapter->hw.max_frame_size = max_frame;
3232 return 0;
3236 * e1000_update_stats - Update the board statistics counters
3237 * @adapter: board private structure
3240 void
3241 e1000_update_stats(struct e1000_adapter *adapter)
3243 struct e1000_hw *hw = &adapter->hw;
3244 struct pci_dev *pdev = adapter->pdev;
3245 unsigned long flags;
3246 uint16_t phy_tmp;
3248 #define PHY_IDLE_ERROR_COUNT_MASK 0x00FF
3251 * Prevent stats update while adapter is being reset, or if the pci
3252 * connection is down.
3254 if (adapter->link_speed == 0)
3255 return;
3256 if (pdev->error_state && pdev->error_state != pci_channel_io_normal)
3257 return;
3259 spin_lock_irqsave(&adapter->stats_lock, flags);
3261 /* these counters are modified from e1000_adjust_tbi_stats,
3262 * called from the interrupt context, so they must only
3263 * be written while holding adapter->stats_lock
3266 adapter->stats.crcerrs += E1000_READ_REG(hw, CRCERRS);
3267 adapter->stats.gprc += E1000_READ_REG(hw, GPRC);
3268 adapter->stats.gorcl += E1000_READ_REG(hw, GORCL);
3269 adapter->stats.gorch += E1000_READ_REG(hw, GORCH);
3270 adapter->stats.bprc += E1000_READ_REG(hw, BPRC);
3271 adapter->stats.mprc += E1000_READ_REG(hw, MPRC);
3272 adapter->stats.roc += E1000_READ_REG(hw, ROC);
3274 if (adapter->hw.mac_type != e1000_ich8lan) {
3275 adapter->stats.prc64 += E1000_READ_REG(hw, PRC64);
3276 adapter->stats.prc127 += E1000_READ_REG(hw, PRC127);
3277 adapter->stats.prc255 += E1000_READ_REG(hw, PRC255);
3278 adapter->stats.prc511 += E1000_READ_REG(hw, PRC511);
3279 adapter->stats.prc1023 += E1000_READ_REG(hw, PRC1023);
3280 adapter->stats.prc1522 += E1000_READ_REG(hw, PRC1522);
3283 adapter->stats.symerrs += E1000_READ_REG(hw, SYMERRS);
3284 adapter->stats.mpc += E1000_READ_REG(hw, MPC);
3285 adapter->stats.scc += E1000_READ_REG(hw, SCC);
3286 adapter->stats.ecol += E1000_READ_REG(hw, ECOL);
3287 adapter->stats.mcc += E1000_READ_REG(hw, MCC);
3288 adapter->stats.latecol += E1000_READ_REG(hw, LATECOL);
3289 adapter->stats.dc += E1000_READ_REG(hw, DC);
3290 adapter->stats.sec += E1000_READ_REG(hw, SEC);
3291 adapter->stats.rlec += E1000_READ_REG(hw, RLEC);
3292 adapter->stats.xonrxc += E1000_READ_REG(hw, XONRXC);
3293 adapter->stats.xontxc += E1000_READ_REG(hw, XONTXC);
3294 adapter->stats.xoffrxc += E1000_READ_REG(hw, XOFFRXC);
3295 adapter->stats.xofftxc += E1000_READ_REG(hw, XOFFTXC);
3296 adapter->stats.fcruc += E1000_READ_REG(hw, FCRUC);
3297 adapter->stats.gptc += E1000_READ_REG(hw, GPTC);
3298 adapter->stats.gotcl += E1000_READ_REG(hw, GOTCL);
3299 adapter->stats.gotch += E1000_READ_REG(hw, GOTCH);
3300 adapter->stats.rnbc += E1000_READ_REG(hw, RNBC);
3301 adapter->stats.ruc += E1000_READ_REG(hw, RUC);
3302 adapter->stats.rfc += E1000_READ_REG(hw, RFC);
3303 adapter->stats.rjc += E1000_READ_REG(hw, RJC);
3304 adapter->stats.torl += E1000_READ_REG(hw, TORL);
3305 adapter->stats.torh += E1000_READ_REG(hw, TORH);
3306 adapter->stats.totl += E1000_READ_REG(hw, TOTL);
3307 adapter->stats.toth += E1000_READ_REG(hw, TOTH);
3308 adapter->stats.tpr += E1000_READ_REG(hw, TPR);
3310 if (adapter->hw.mac_type != e1000_ich8lan) {
3311 adapter->stats.ptc64 += E1000_READ_REG(hw, PTC64);
3312 adapter->stats.ptc127 += E1000_READ_REG(hw, PTC127);
3313 adapter->stats.ptc255 += E1000_READ_REG(hw, PTC255);
3314 adapter->stats.ptc511 += E1000_READ_REG(hw, PTC511);
3315 adapter->stats.ptc1023 += E1000_READ_REG(hw, PTC1023);
3316 adapter->stats.ptc1522 += E1000_READ_REG(hw, PTC1522);
3319 adapter->stats.mptc += E1000_READ_REG(hw, MPTC);
3320 adapter->stats.bptc += E1000_READ_REG(hw, BPTC);
3322 /* used for adaptive IFS */
3324 hw->tx_packet_delta = E1000_READ_REG(hw, TPT);
3325 adapter->stats.tpt += hw->tx_packet_delta;
3326 hw->collision_delta = E1000_READ_REG(hw, COLC);
3327 adapter->stats.colc += hw->collision_delta;
3329 if (hw->mac_type >= e1000_82543) {
3330 adapter->stats.algnerrc += E1000_READ_REG(hw, ALGNERRC);
3331 adapter->stats.rxerrc += E1000_READ_REG(hw, RXERRC);
3332 adapter->stats.tncrs += E1000_READ_REG(hw, TNCRS);
3333 adapter->stats.cexterr += E1000_READ_REG(hw, CEXTERR);
3334 adapter->stats.tsctc += E1000_READ_REG(hw, TSCTC);
3335 adapter->stats.tsctfc += E1000_READ_REG(hw, TSCTFC);
3337 if (hw->mac_type > e1000_82547_rev_2) {
3338 adapter->stats.iac += E1000_READ_REG(hw, IAC);
3339 adapter->stats.icrxoc += E1000_READ_REG(hw, ICRXOC);
3341 if (adapter->hw.mac_type != e1000_ich8lan) {
3342 adapter->stats.icrxptc += E1000_READ_REG(hw, ICRXPTC);
3343 adapter->stats.icrxatc += E1000_READ_REG(hw, ICRXATC);
3344 adapter->stats.ictxptc += E1000_READ_REG(hw, ICTXPTC);
3345 adapter->stats.ictxatc += E1000_READ_REG(hw, ICTXATC);
3346 adapter->stats.ictxqec += E1000_READ_REG(hw, ICTXQEC);
3347 adapter->stats.ictxqmtc += E1000_READ_REG(hw, ICTXQMTC);
3348 adapter->stats.icrxdmtc += E1000_READ_REG(hw, ICRXDMTC);
3352 /* Fill out the OS statistics structure */
3354 adapter->net_stats.rx_packets = adapter->stats.gprc;
3355 adapter->net_stats.tx_packets = adapter->stats.gptc;
3356 adapter->net_stats.rx_bytes = adapter->stats.gorcl;
3357 adapter->net_stats.tx_bytes = adapter->stats.gotcl;
3358 adapter->net_stats.multicast = adapter->stats.mprc;
3359 adapter->net_stats.collisions = adapter->stats.colc;
3361 /* Rx Errors */
3363 /* RLEC on some newer hardware can be incorrect so build
3364 * our own version based on RUC and ROC */
3365 adapter->net_stats.rx_errors = adapter->stats.rxerrc +
3366 adapter->stats.crcerrs + adapter->stats.algnerrc +
3367 adapter->stats.ruc + adapter->stats.roc +
3368 adapter->stats.cexterr;
3369 adapter->stats.rlerrc = adapter->stats.ruc + adapter->stats.roc;
3370 adapter->net_stats.rx_length_errors = adapter->stats.rlerrc;
3371 adapter->net_stats.rx_crc_errors = adapter->stats.crcerrs;
3372 adapter->net_stats.rx_frame_errors = adapter->stats.algnerrc;
3373 adapter->net_stats.rx_missed_errors = adapter->stats.mpc;
3375 /* Tx Errors */
3376 adapter->stats.txerrc = adapter->stats.ecol + adapter->stats.latecol;
3377 adapter->net_stats.tx_errors = adapter->stats.txerrc;
3378 adapter->net_stats.tx_aborted_errors = adapter->stats.ecol;
3379 adapter->net_stats.tx_window_errors = adapter->stats.latecol;
3380 adapter->net_stats.tx_carrier_errors = adapter->stats.tncrs;
3382 /* Tx Dropped needs to be maintained elsewhere */
3384 /* Phy Stats */
3386 if (hw->media_type == e1000_media_type_copper) {
3387 if ((adapter->link_speed == SPEED_1000) &&
3388 (!e1000_read_phy_reg(hw, PHY_1000T_STATUS, &phy_tmp))) {
3389 phy_tmp &= PHY_IDLE_ERROR_COUNT_MASK;
3390 adapter->phy_stats.idle_errors += phy_tmp;
3393 if ((hw->mac_type <= e1000_82546) &&
3394 (hw->phy_type == e1000_phy_m88) &&
3395 !e1000_read_phy_reg(hw, M88E1000_RX_ERR_CNTR, &phy_tmp))
3396 adapter->phy_stats.receive_errors += phy_tmp;
3399 spin_unlock_irqrestore(&adapter->stats_lock, flags);
3403 * e1000_intr - Interrupt Handler
3404 * @irq: interrupt number
3405 * @data: pointer to a network interface device structure
3406 * @pt_regs: CPU registers structure
3409 static irqreturn_t
3410 e1000_intr(int irq, void *data, struct pt_regs *regs)
3412 struct net_device *netdev = data;
3413 struct e1000_adapter *adapter = netdev_priv(netdev);
3414 struct e1000_hw *hw = &adapter->hw;
3415 uint32_t rctl, icr = E1000_READ_REG(hw, ICR);
3416 #ifndef CONFIG_E1000_NAPI
3417 int i;
3418 #else
3419 /* Interrupt Auto-Mask...upon reading ICR,
3420 * interrupts are masked. No need for the
3421 * IMC write, but it does mean we should
3422 * account for it ASAP. */
3423 if (likely(hw->mac_type >= e1000_82571))
3424 atomic_inc(&adapter->irq_sem);
3425 #endif
3427 if (unlikely(!icr)) {
3428 #ifdef CONFIG_E1000_NAPI
3429 if (hw->mac_type >= e1000_82571)
3430 e1000_irq_enable(adapter);
3431 #endif
3432 return IRQ_NONE; /* Not our interrupt */
3435 if (unlikely(icr & (E1000_ICR_RXSEQ | E1000_ICR_LSC))) {
3436 hw->get_link_status = 1;
3437 /* 80003ES2LAN workaround--
3438 * For packet buffer work-around on link down event;
3439 * disable receives here in the ISR and
3440 * reset adapter in watchdog
3442 if (netif_carrier_ok(netdev) &&
3443 (adapter->hw.mac_type == e1000_80003es2lan)) {
3444 /* disable receives */
3445 rctl = E1000_READ_REG(hw, RCTL);
3446 E1000_WRITE_REG(hw, RCTL, rctl & ~E1000_RCTL_EN);
3448 mod_timer(&adapter->watchdog_timer, jiffies);
3451 #ifdef CONFIG_E1000_NAPI
3452 if (unlikely(hw->mac_type < e1000_82571)) {
3453 atomic_inc(&adapter->irq_sem);
3454 E1000_WRITE_REG(hw, IMC, ~0);
3455 E1000_WRITE_FLUSH(hw);
3457 if (likely(netif_rx_schedule_prep(netdev)))
3458 __netif_rx_schedule(netdev);
3459 else
3460 e1000_irq_enable(adapter);
3461 #else
3462 /* Writing IMC and IMS is needed for 82547.
3463 * Due to Hub Link bus being occupied, an interrupt
3464 * de-assertion message is not able to be sent.
3465 * When an interrupt assertion message is generated later,
3466 * two messages are re-ordered and sent out.
3467 * That causes APIC to think 82547 is in de-assertion
3468 * state, while 82547 is in assertion state, resulting
3469 * in dead lock. Writing IMC forces 82547 into
3470 * de-assertion state.
3472 if (hw->mac_type == e1000_82547 || hw->mac_type == e1000_82547_rev_2) {
3473 atomic_inc(&adapter->irq_sem);
3474 E1000_WRITE_REG(hw, IMC, ~0);
3477 for (i = 0; i < E1000_MAX_INTR; i++)
3478 if (unlikely(!adapter->clean_rx(adapter, adapter->rx_ring) &
3479 !e1000_clean_tx_irq(adapter, adapter->tx_ring)))
3480 break;
3482 if (hw->mac_type == e1000_82547 || hw->mac_type == e1000_82547_rev_2)
3483 e1000_irq_enable(adapter);
3485 #endif
3487 return IRQ_HANDLED;
3490 #ifdef CONFIG_E1000_NAPI
3492 * e1000_clean - NAPI Rx polling callback
3493 * @adapter: board private structure
3496 static int
3497 e1000_clean(struct net_device *poll_dev, int *budget)
3499 struct e1000_adapter *adapter;
3500 int work_to_do = min(*budget, poll_dev->quota);
3501 int tx_cleaned = 0, work_done = 0;
3503 /* Must NOT use netdev_priv macro here. */
3504 adapter = poll_dev->priv;
3506 /* Keep link state information with original netdev */
3507 if (!netif_carrier_ok(poll_dev))
3508 goto quit_polling;
3510 /* e1000_clean is called per-cpu. This lock protects
3511 * tx_ring[0] from being cleaned by multiple cpus
3512 * simultaneously. A failure obtaining the lock means
3513 * tx_ring[0] is currently being cleaned anyway. */
3514 if (spin_trylock(&adapter->tx_queue_lock)) {
3515 tx_cleaned = e1000_clean_tx_irq(adapter,
3516 &adapter->tx_ring[0]);
3517 spin_unlock(&adapter->tx_queue_lock);
3520 adapter->clean_rx(adapter, &adapter->rx_ring[0],
3521 &work_done, work_to_do);
3523 *budget -= work_done;
3524 poll_dev->quota -= work_done;
3526 /* If no Tx and not enough Rx work done, exit the polling mode */
3527 if ((!tx_cleaned && (work_done == 0)) ||
3528 !netif_running(poll_dev)) {
3529 quit_polling:
3530 netif_rx_complete(poll_dev);
3531 e1000_irq_enable(adapter);
3532 return 0;
3535 return 1;
3538 #endif
3540 * e1000_clean_tx_irq - Reclaim resources after transmit completes
3541 * @adapter: board private structure
3544 static boolean_t
3545 e1000_clean_tx_irq(struct e1000_adapter *adapter,
3546 struct e1000_tx_ring *tx_ring)
3548 struct net_device *netdev = adapter->netdev;
3549 struct e1000_tx_desc *tx_desc, *eop_desc;
3550 struct e1000_buffer *buffer_info;
3551 unsigned int i, eop;
3552 #ifdef CONFIG_E1000_NAPI
3553 unsigned int count = 0;
3554 #endif
3555 boolean_t cleaned = FALSE;
3557 i = tx_ring->next_to_clean;
3558 eop = tx_ring->buffer_info[i].next_to_watch;
3559 eop_desc = E1000_TX_DESC(*tx_ring, eop);
3561 while (eop_desc->upper.data & cpu_to_le32(E1000_TXD_STAT_DD)) {
3562 for (cleaned = FALSE; !cleaned; ) {
3563 tx_desc = E1000_TX_DESC(*tx_ring, i);
3564 buffer_info = &tx_ring->buffer_info[i];
3565 cleaned = (i == eop);
3567 e1000_unmap_and_free_tx_resource(adapter, buffer_info);
3568 memset(tx_desc, 0, sizeof(struct e1000_tx_desc));
3570 if (unlikely(++i == tx_ring->count)) i = 0;
3574 eop = tx_ring->buffer_info[i].next_to_watch;
3575 eop_desc = E1000_TX_DESC(*tx_ring, eop);
3576 #ifdef CONFIG_E1000_NAPI
3577 #define E1000_TX_WEIGHT 64
3578 /* weight of a sort for tx, to avoid endless transmit cleanup */
3579 if (count++ == E1000_TX_WEIGHT) break;
3580 #endif
3583 tx_ring->next_to_clean = i;
3585 #define TX_WAKE_THRESHOLD 32
3586 if (unlikely(cleaned && netif_carrier_ok(netdev) &&
3587 E1000_DESC_UNUSED(tx_ring) >= TX_WAKE_THRESHOLD)) {
3588 /* Make sure that anybody stopping the queue after this
3589 * sees the new next_to_clean.
3591 smp_mb();
3592 if (netif_queue_stopped(netdev))
3593 netif_wake_queue(netdev);
3596 if (adapter->detect_tx_hung) {
3597 /* Detect a transmit hang in hardware, this serializes the
3598 * check with the clearing of time_stamp and movement of i */
3599 adapter->detect_tx_hung = FALSE;
3600 if (tx_ring->buffer_info[eop].dma &&
3601 time_after(jiffies, tx_ring->buffer_info[eop].time_stamp +
3602 (adapter->tx_timeout_factor * HZ))
3603 && !(E1000_READ_REG(&adapter->hw, STATUS) &
3604 E1000_STATUS_TXOFF)) {
3606 /* detected Tx unit hang */
3607 DPRINTK(DRV, ERR, "Detected Tx Unit Hang\n"
3608 " Tx Queue <%lu>\n"
3609 " TDH <%x>\n"
3610 " TDT <%x>\n"
3611 " next_to_use <%x>\n"
3612 " next_to_clean <%x>\n"
3613 "buffer_info[next_to_clean]\n"
3614 " time_stamp <%lx>\n"
3615 " next_to_watch <%x>\n"
3616 " jiffies <%lx>\n"
3617 " next_to_watch.status <%x>\n",
3618 (unsigned long)((tx_ring - adapter->tx_ring) /
3619 sizeof(struct e1000_tx_ring)),
3620 readl(adapter->hw.hw_addr + tx_ring->tdh),
3621 readl(adapter->hw.hw_addr + tx_ring->tdt),
3622 tx_ring->next_to_use,
3623 tx_ring->next_to_clean,
3624 tx_ring->buffer_info[eop].time_stamp,
3625 eop,
3626 jiffies,
3627 eop_desc->upper.fields.status);
3628 netif_stop_queue(netdev);
3631 return cleaned;
3635 * e1000_rx_checksum - Receive Checksum Offload for 82543
3636 * @adapter: board private structure
3637 * @status_err: receive descriptor status and error fields
3638 * @csum: receive descriptor csum field
3639 * @sk_buff: socket buffer with received data
3642 static void
3643 e1000_rx_checksum(struct e1000_adapter *adapter,
3644 uint32_t status_err, uint32_t csum,
3645 struct sk_buff *skb)
3647 uint16_t status = (uint16_t)status_err;
3648 uint8_t errors = (uint8_t)(status_err >> 24);
3649 skb->ip_summed = CHECKSUM_NONE;
3651 /* 82543 or newer only */
3652 if (unlikely(adapter->hw.mac_type < e1000_82543)) return;
3653 /* Ignore Checksum bit is set */
3654 if (unlikely(status & E1000_RXD_STAT_IXSM)) return;
3655 /* TCP/UDP checksum error bit is set */
3656 if (unlikely(errors & E1000_RXD_ERR_TCPE)) {
3657 /* let the stack verify checksum errors */
3658 adapter->hw_csum_err++;
3659 return;
3661 /* TCP/UDP Checksum has not been calculated */
3662 if (adapter->hw.mac_type <= e1000_82547_rev_2) {
3663 if (!(status & E1000_RXD_STAT_TCPCS))
3664 return;
3665 } else {
3666 if (!(status & (E1000_RXD_STAT_TCPCS | E1000_RXD_STAT_UDPCS)))
3667 return;
3669 /* It must be a TCP or UDP packet with a valid checksum */
3670 if (likely(status & E1000_RXD_STAT_TCPCS)) {
3671 /* TCP checksum is good */
3672 skb->ip_summed = CHECKSUM_UNNECESSARY;
3673 } else if (adapter->hw.mac_type > e1000_82547_rev_2) {
3674 /* IP fragment with UDP payload */
3675 /* Hardware complements the payload checksum, so we undo it
3676 * and then put the value in host order for further stack use.
3678 csum = ntohl(csum ^ 0xFFFF);
3679 skb->csum = csum;
3680 skb->ip_summed = CHECKSUM_COMPLETE;
3682 adapter->hw_csum_good++;
3686 * e1000_clean_rx_irq - Send received data up the network stack; legacy
3687 * @adapter: board private structure
3690 static boolean_t
3691 #ifdef CONFIG_E1000_NAPI
3692 e1000_clean_rx_irq(struct e1000_adapter *adapter,
3693 struct e1000_rx_ring *rx_ring,
3694 int *work_done, int work_to_do)
3695 #else
3696 e1000_clean_rx_irq(struct e1000_adapter *adapter,
3697 struct e1000_rx_ring *rx_ring)
3698 #endif
3700 struct net_device *netdev = adapter->netdev;
3701 struct pci_dev *pdev = adapter->pdev;
3702 struct e1000_rx_desc *rx_desc, *next_rxd;
3703 struct e1000_buffer *buffer_info, *next_buffer;
3704 unsigned long flags;
3705 uint32_t length;
3706 uint8_t last_byte;
3707 unsigned int i;
3708 int cleaned_count = 0;
3709 boolean_t cleaned = FALSE;
3711 i = rx_ring->next_to_clean;
3712 rx_desc = E1000_RX_DESC(*rx_ring, i);
3713 buffer_info = &rx_ring->buffer_info[i];
3715 while (rx_desc->status & E1000_RXD_STAT_DD) {
3716 struct sk_buff *skb;
3717 u8 status;
3718 #ifdef CONFIG_E1000_NAPI
3719 if (*work_done >= work_to_do)
3720 break;
3721 (*work_done)++;
3722 #endif
3723 status = rx_desc->status;
3724 skb = buffer_info->skb;
3725 buffer_info->skb = NULL;
3727 prefetch(skb->data - NET_IP_ALIGN);
3729 if (++i == rx_ring->count) i = 0;
3730 next_rxd = E1000_RX_DESC(*rx_ring, i);
3731 prefetch(next_rxd);
3733 next_buffer = &rx_ring->buffer_info[i];
3735 cleaned = TRUE;
3736 cleaned_count++;
3737 pci_unmap_single(pdev,
3738 buffer_info->dma,
3739 buffer_info->length,
3740 PCI_DMA_FROMDEVICE);
3742 length = le16_to_cpu(rx_desc->length);
3744 /* adjust length to remove Ethernet CRC */
3745 length -= 4;
3747 if (unlikely(!(status & E1000_RXD_STAT_EOP))) {
3748 /* All receives must fit into a single buffer */
3749 E1000_DBG("%s: Receive packet consumed multiple"
3750 " buffers\n", netdev->name);
3751 /* recycle */
3752 buffer_info->skb = skb;
3753 goto next_desc;
3756 if (unlikely(rx_desc->errors & E1000_RXD_ERR_FRAME_ERR_MASK)) {
3757 last_byte = *(skb->data + length - 1);
3758 if (TBI_ACCEPT(&adapter->hw, status,
3759 rx_desc->errors, length, last_byte)) {
3760 spin_lock_irqsave(&adapter->stats_lock, flags);
3761 e1000_tbi_adjust_stats(&adapter->hw,
3762 &adapter->stats,
3763 length, skb->data);
3764 spin_unlock_irqrestore(&adapter->stats_lock,
3765 flags);
3766 length--;
3767 } else {
3768 /* recycle */
3769 buffer_info->skb = skb;
3770 goto next_desc;
3774 /* code added for copybreak, this should improve
3775 * performance for small packets with large amounts
3776 * of reassembly being done in the stack */
3777 #define E1000_CB_LENGTH 256
3778 if (length < E1000_CB_LENGTH) {
3779 struct sk_buff *new_skb =
3780 netdev_alloc_skb(netdev, length + NET_IP_ALIGN);
3781 if (new_skb) {
3782 skb_reserve(new_skb, NET_IP_ALIGN);
3783 memcpy(new_skb->data - NET_IP_ALIGN,
3784 skb->data - NET_IP_ALIGN,
3785 length + NET_IP_ALIGN);
3786 /* save the skb in buffer_info as good */
3787 buffer_info->skb = skb;
3788 skb = new_skb;
3789 skb_put(skb, length);
3791 } else
3792 skb_put(skb, length);
3794 /* end copybreak code */
3796 /* Receive Checksum Offload */
3797 e1000_rx_checksum(adapter,
3798 (uint32_t)(status) |
3799 ((uint32_t)(rx_desc->errors) << 24),
3800 le16_to_cpu(rx_desc->csum), skb);
3802 skb->protocol = eth_type_trans(skb, netdev);
3803 #ifdef CONFIG_E1000_NAPI
3804 if (unlikely(adapter->vlgrp &&
3805 (status & E1000_RXD_STAT_VP))) {
3806 vlan_hwaccel_receive_skb(skb, adapter->vlgrp,
3807 le16_to_cpu(rx_desc->special) &
3808 E1000_RXD_SPC_VLAN_MASK);
3809 } else {
3810 netif_receive_skb(skb);
3812 #else /* CONFIG_E1000_NAPI */
3813 if (unlikely(adapter->vlgrp &&
3814 (status & E1000_RXD_STAT_VP))) {
3815 vlan_hwaccel_rx(skb, adapter->vlgrp,
3816 le16_to_cpu(rx_desc->special) &
3817 E1000_RXD_SPC_VLAN_MASK);
3818 } else {
3819 netif_rx(skb);
3821 #endif /* CONFIG_E1000_NAPI */
3822 netdev->last_rx = jiffies;
3824 next_desc:
3825 rx_desc->status = 0;
3827 /* return some buffers to hardware, one at a time is too slow */
3828 if (unlikely(cleaned_count >= E1000_RX_BUFFER_WRITE)) {
3829 adapter->alloc_rx_buf(adapter, rx_ring, cleaned_count);
3830 cleaned_count = 0;
3833 /* use prefetched values */
3834 rx_desc = next_rxd;
3835 buffer_info = next_buffer;
3837 rx_ring->next_to_clean = i;
3839 cleaned_count = E1000_DESC_UNUSED(rx_ring);
3840 if (cleaned_count)
3841 adapter->alloc_rx_buf(adapter, rx_ring, cleaned_count);
3843 return cleaned;
3847 * e1000_clean_rx_irq_ps - Send received data up the network stack; packet split
3848 * @adapter: board private structure
3851 static boolean_t
3852 #ifdef CONFIG_E1000_NAPI
3853 e1000_clean_rx_irq_ps(struct e1000_adapter *adapter,
3854 struct e1000_rx_ring *rx_ring,
3855 int *work_done, int work_to_do)
3856 #else
3857 e1000_clean_rx_irq_ps(struct e1000_adapter *adapter,
3858 struct e1000_rx_ring *rx_ring)
3859 #endif
3861 union e1000_rx_desc_packet_split *rx_desc, *next_rxd;
3862 struct net_device *netdev = adapter->netdev;
3863 struct pci_dev *pdev = adapter->pdev;
3864 struct e1000_buffer *buffer_info, *next_buffer;
3865 struct e1000_ps_page *ps_page;
3866 struct e1000_ps_page_dma *ps_page_dma;
3867 struct sk_buff *skb;
3868 unsigned int i, j;
3869 uint32_t length, staterr;
3870 int cleaned_count = 0;
3871 boolean_t cleaned = FALSE;
3873 i = rx_ring->next_to_clean;
3874 rx_desc = E1000_RX_DESC_PS(*rx_ring, i);
3875 staterr = le32_to_cpu(rx_desc->wb.middle.status_error);
3876 buffer_info = &rx_ring->buffer_info[i];
3878 while (staterr & E1000_RXD_STAT_DD) {
3879 ps_page = &rx_ring->ps_page[i];
3880 ps_page_dma = &rx_ring->ps_page_dma[i];
3881 #ifdef CONFIG_E1000_NAPI
3882 if (unlikely(*work_done >= work_to_do))
3883 break;
3884 (*work_done)++;
3885 #endif
3886 skb = buffer_info->skb;
3888 /* in the packet split case this is header only */
3889 prefetch(skb->data - NET_IP_ALIGN);
3891 if (++i == rx_ring->count) i = 0;
3892 next_rxd = E1000_RX_DESC_PS(*rx_ring, i);
3893 prefetch(next_rxd);
3895 next_buffer = &rx_ring->buffer_info[i];
3897 cleaned = TRUE;
3898 cleaned_count++;
3899 pci_unmap_single(pdev, buffer_info->dma,
3900 buffer_info->length,
3901 PCI_DMA_FROMDEVICE);
3903 if (unlikely(!(staterr & E1000_RXD_STAT_EOP))) {
3904 E1000_DBG("%s: Packet Split buffers didn't pick up"
3905 " the full packet\n", netdev->name);
3906 dev_kfree_skb_irq(skb);
3907 goto next_desc;
3910 if (unlikely(staterr & E1000_RXDEXT_ERR_FRAME_ERR_MASK)) {
3911 dev_kfree_skb_irq(skb);
3912 goto next_desc;
3915 length = le16_to_cpu(rx_desc->wb.middle.length0);
3917 if (unlikely(!length)) {
3918 E1000_DBG("%s: Last part of the packet spanning"
3919 " multiple descriptors\n", netdev->name);
3920 dev_kfree_skb_irq(skb);
3921 goto next_desc;
3924 /* Good Receive */
3925 skb_put(skb, length);
3928 /* this looks ugly, but it seems compiler issues make it
3929 more efficient than reusing j */
3930 int l1 = le16_to_cpu(rx_desc->wb.upper.length[0]);
3932 /* page alloc/put takes too long and effects small packet
3933 * throughput, so unsplit small packets and save the alloc/put*/
3934 if (l1 && ((length + l1) <= adapter->rx_ps_bsize0)) {
3935 u8 *vaddr;
3936 /* there is no documentation about how to call
3937 * kmap_atomic, so we can't hold the mapping
3938 * very long */
3939 pci_dma_sync_single_for_cpu(pdev,
3940 ps_page_dma->ps_page_dma[0],
3941 PAGE_SIZE,
3942 PCI_DMA_FROMDEVICE);
3943 vaddr = kmap_atomic(ps_page->ps_page[0],
3944 KM_SKB_DATA_SOFTIRQ);
3945 memcpy(skb->tail, vaddr, l1);
3946 kunmap_atomic(vaddr, KM_SKB_DATA_SOFTIRQ);
3947 pci_dma_sync_single_for_device(pdev,
3948 ps_page_dma->ps_page_dma[0],
3949 PAGE_SIZE, PCI_DMA_FROMDEVICE);
3950 /* remove the CRC */
3951 l1 -= 4;
3952 skb_put(skb, l1);
3953 goto copydone;
3954 } /* if */
3957 for (j = 0; j < adapter->rx_ps_pages; j++) {
3958 if (!(length= le16_to_cpu(rx_desc->wb.upper.length[j])))
3959 break;
3960 pci_unmap_page(pdev, ps_page_dma->ps_page_dma[j],
3961 PAGE_SIZE, PCI_DMA_FROMDEVICE);
3962 ps_page_dma->ps_page_dma[j] = 0;
3963 skb_fill_page_desc(skb, j, ps_page->ps_page[j], 0,
3964 length);
3965 ps_page->ps_page[j] = NULL;
3966 skb->len += length;
3967 skb->data_len += length;
3968 skb->truesize += length;
3971 /* strip the ethernet crc, problem is we're using pages now so
3972 * this whole operation can get a little cpu intensive */
3973 pskb_trim(skb, skb->len - 4);
3975 copydone:
3976 e1000_rx_checksum(adapter, staterr,
3977 le16_to_cpu(rx_desc->wb.lower.hi_dword.csum_ip.csum), skb);
3978 skb->protocol = eth_type_trans(skb, netdev);
3980 if (likely(rx_desc->wb.upper.header_status &
3981 cpu_to_le16(E1000_RXDPS_HDRSTAT_HDRSP)))
3982 adapter->rx_hdr_split++;
3983 #ifdef CONFIG_E1000_NAPI
3984 if (unlikely(adapter->vlgrp && (staterr & E1000_RXD_STAT_VP))) {
3985 vlan_hwaccel_receive_skb(skb, adapter->vlgrp,
3986 le16_to_cpu(rx_desc->wb.middle.vlan) &
3987 E1000_RXD_SPC_VLAN_MASK);
3988 } else {
3989 netif_receive_skb(skb);
3991 #else /* CONFIG_E1000_NAPI */
3992 if (unlikely(adapter->vlgrp && (staterr & E1000_RXD_STAT_VP))) {
3993 vlan_hwaccel_rx(skb, adapter->vlgrp,
3994 le16_to_cpu(rx_desc->wb.middle.vlan) &
3995 E1000_RXD_SPC_VLAN_MASK);
3996 } else {
3997 netif_rx(skb);
3999 #endif /* CONFIG_E1000_NAPI */
4000 netdev->last_rx = jiffies;
4002 next_desc:
4003 rx_desc->wb.middle.status_error &= cpu_to_le32(~0xFF);
4004 buffer_info->skb = NULL;
4006 /* return some buffers to hardware, one at a time is too slow */
4007 if (unlikely(cleaned_count >= E1000_RX_BUFFER_WRITE)) {
4008 adapter->alloc_rx_buf(adapter, rx_ring, cleaned_count);
4009 cleaned_count = 0;
4012 /* use prefetched values */
4013 rx_desc = next_rxd;
4014 buffer_info = next_buffer;
4016 staterr = le32_to_cpu(rx_desc->wb.middle.status_error);
4018 rx_ring->next_to_clean = i;
4020 cleaned_count = E1000_DESC_UNUSED(rx_ring);
4021 if (cleaned_count)
4022 adapter->alloc_rx_buf(adapter, rx_ring, cleaned_count);
4024 return cleaned;
4028 * e1000_alloc_rx_buffers - Replace used receive buffers; legacy & extended
4029 * @adapter: address of board private structure
4032 static void
4033 e1000_alloc_rx_buffers(struct e1000_adapter *adapter,
4034 struct e1000_rx_ring *rx_ring,
4035 int cleaned_count)
4037 struct net_device *netdev = adapter->netdev;
4038 struct pci_dev *pdev = adapter->pdev;
4039 struct e1000_rx_desc *rx_desc;
4040 struct e1000_buffer *buffer_info;
4041 struct sk_buff *skb;
4042 unsigned int i;
4043 unsigned int bufsz = adapter->rx_buffer_len + NET_IP_ALIGN;
4045 i = rx_ring->next_to_use;
4046 buffer_info = &rx_ring->buffer_info[i];
4048 while (cleaned_count--) {
4049 skb = buffer_info->skb;
4050 if (skb) {
4051 skb_trim(skb, 0);
4052 goto map_skb;
4055 skb = netdev_alloc_skb(netdev, bufsz);
4056 if (unlikely(!skb)) {
4057 /* Better luck next round */
4058 adapter->alloc_rx_buff_failed++;
4059 break;
4062 /* Fix for errata 23, can't cross 64kB boundary */
4063 if (!e1000_check_64k_bound(adapter, skb->data, bufsz)) {
4064 struct sk_buff *oldskb = skb;
4065 DPRINTK(RX_ERR, ERR, "skb align check failed: %u bytes "
4066 "at %p\n", bufsz, skb->data);
4067 /* Try again, without freeing the previous */
4068 skb = netdev_alloc_skb(netdev, bufsz);
4069 /* Failed allocation, critical failure */
4070 if (!skb) {
4071 dev_kfree_skb(oldskb);
4072 break;
4075 if (!e1000_check_64k_bound(adapter, skb->data, bufsz)) {
4076 /* give up */
4077 dev_kfree_skb(skb);
4078 dev_kfree_skb(oldskb);
4079 break; /* while !buffer_info->skb */
4082 /* Use new allocation */
4083 dev_kfree_skb(oldskb);
4085 /* Make buffer alignment 2 beyond a 16 byte boundary
4086 * this will result in a 16 byte aligned IP header after
4087 * the 14 byte MAC header is removed
4089 skb_reserve(skb, NET_IP_ALIGN);
4091 buffer_info->skb = skb;
4092 buffer_info->length = adapter->rx_buffer_len;
4093 map_skb:
4094 buffer_info->dma = pci_map_single(pdev,
4095 skb->data,
4096 adapter->rx_buffer_len,
4097 PCI_DMA_FROMDEVICE);
4099 /* Fix for errata 23, can't cross 64kB boundary */
4100 if (!e1000_check_64k_bound(adapter,
4101 (void *)(unsigned long)buffer_info->dma,
4102 adapter->rx_buffer_len)) {
4103 DPRINTK(RX_ERR, ERR,
4104 "dma align check failed: %u bytes at %p\n",
4105 adapter->rx_buffer_len,
4106 (void *)(unsigned long)buffer_info->dma);
4107 dev_kfree_skb(skb);
4108 buffer_info->skb = NULL;
4110 pci_unmap_single(pdev, buffer_info->dma,
4111 adapter->rx_buffer_len,
4112 PCI_DMA_FROMDEVICE);
4114 break; /* while !buffer_info->skb */
4116 rx_desc = E1000_RX_DESC(*rx_ring, i);
4117 rx_desc->buffer_addr = cpu_to_le64(buffer_info->dma);
4119 if (unlikely(++i == rx_ring->count))
4120 i = 0;
4121 buffer_info = &rx_ring->buffer_info[i];
4124 if (likely(rx_ring->next_to_use != i)) {
4125 rx_ring->next_to_use = i;
4126 if (unlikely(i-- == 0))
4127 i = (rx_ring->count - 1);
4129 /* Force memory writes to complete before letting h/w
4130 * know there are new descriptors to fetch. (Only
4131 * applicable for weak-ordered memory model archs,
4132 * such as IA-64). */
4133 wmb();
4134 writel(i, adapter->hw.hw_addr + rx_ring->rdt);
4139 * e1000_alloc_rx_buffers_ps - Replace used receive buffers; packet split
4140 * @adapter: address of board private structure
4143 static void
4144 e1000_alloc_rx_buffers_ps(struct e1000_adapter *adapter,
4145 struct e1000_rx_ring *rx_ring,
4146 int cleaned_count)
4148 struct net_device *netdev = adapter->netdev;
4149 struct pci_dev *pdev = adapter->pdev;
4150 union e1000_rx_desc_packet_split *rx_desc;
4151 struct e1000_buffer *buffer_info;
4152 struct e1000_ps_page *ps_page;
4153 struct e1000_ps_page_dma *ps_page_dma;
4154 struct sk_buff *skb;
4155 unsigned int i, j;
4157 i = rx_ring->next_to_use;
4158 buffer_info = &rx_ring->buffer_info[i];
4159 ps_page = &rx_ring->ps_page[i];
4160 ps_page_dma = &rx_ring->ps_page_dma[i];
4162 while (cleaned_count--) {
4163 rx_desc = E1000_RX_DESC_PS(*rx_ring, i);
4165 for (j = 0; j < PS_PAGE_BUFFERS; j++) {
4166 if (j < adapter->rx_ps_pages) {
4167 if (likely(!ps_page->ps_page[j])) {
4168 ps_page->ps_page[j] =
4169 alloc_page(GFP_ATOMIC);
4170 if (unlikely(!ps_page->ps_page[j])) {
4171 adapter->alloc_rx_buff_failed++;
4172 goto no_buffers;
4174 ps_page_dma->ps_page_dma[j] =
4175 pci_map_page(pdev,
4176 ps_page->ps_page[j],
4177 0, PAGE_SIZE,
4178 PCI_DMA_FROMDEVICE);
4180 /* Refresh the desc even if buffer_addrs didn't
4181 * change because each write-back erases
4182 * this info.
4184 rx_desc->read.buffer_addr[j+1] =
4185 cpu_to_le64(ps_page_dma->ps_page_dma[j]);
4186 } else
4187 rx_desc->read.buffer_addr[j+1] = ~0;
4190 skb = netdev_alloc_skb(netdev,
4191 adapter->rx_ps_bsize0 + NET_IP_ALIGN);
4193 if (unlikely(!skb)) {
4194 adapter->alloc_rx_buff_failed++;
4195 break;
4198 /* Make buffer alignment 2 beyond a 16 byte boundary
4199 * this will result in a 16 byte aligned IP header after
4200 * the 14 byte MAC header is removed
4202 skb_reserve(skb, NET_IP_ALIGN);
4204 buffer_info->skb = skb;
4205 buffer_info->length = adapter->rx_ps_bsize0;
4206 buffer_info->dma = pci_map_single(pdev, skb->data,
4207 adapter->rx_ps_bsize0,
4208 PCI_DMA_FROMDEVICE);
4210 rx_desc->read.buffer_addr[0] = cpu_to_le64(buffer_info->dma);
4212 if (unlikely(++i == rx_ring->count)) i = 0;
4213 buffer_info = &rx_ring->buffer_info[i];
4214 ps_page = &rx_ring->ps_page[i];
4215 ps_page_dma = &rx_ring->ps_page_dma[i];
4218 no_buffers:
4219 if (likely(rx_ring->next_to_use != i)) {
4220 rx_ring->next_to_use = i;
4221 if (unlikely(i-- == 0)) i = (rx_ring->count - 1);
4223 /* Force memory writes to complete before letting h/w
4224 * know there are new descriptors to fetch. (Only
4225 * applicable for weak-ordered memory model archs,
4226 * such as IA-64). */
4227 wmb();
4228 /* Hardware increments by 16 bytes, but packet split
4229 * descriptors are 32 bytes...so we increment tail
4230 * twice as much.
4232 writel(i<<1, adapter->hw.hw_addr + rx_ring->rdt);
4237 * e1000_smartspeed - Workaround for SmartSpeed on 82541 and 82547 controllers.
4238 * @adapter:
4241 static void
4242 e1000_smartspeed(struct e1000_adapter *adapter)
4244 uint16_t phy_status;
4245 uint16_t phy_ctrl;
4247 if ((adapter->hw.phy_type != e1000_phy_igp) || !adapter->hw.autoneg ||
4248 !(adapter->hw.autoneg_advertised & ADVERTISE_1000_FULL))
4249 return;
4251 if (adapter->smartspeed == 0) {
4252 /* If Master/Slave config fault is asserted twice,
4253 * we assume back-to-back */
4254 e1000_read_phy_reg(&adapter->hw, PHY_1000T_STATUS, &phy_status);
4255 if (!(phy_status & SR_1000T_MS_CONFIG_FAULT)) return;
4256 e1000_read_phy_reg(&adapter->hw, PHY_1000T_STATUS, &phy_status);
4257 if (!(phy_status & SR_1000T_MS_CONFIG_FAULT)) return;
4258 e1000_read_phy_reg(&adapter->hw, PHY_1000T_CTRL, &phy_ctrl);
4259 if (phy_ctrl & CR_1000T_MS_ENABLE) {
4260 phy_ctrl &= ~CR_1000T_MS_ENABLE;
4261 e1000_write_phy_reg(&adapter->hw, PHY_1000T_CTRL,
4262 phy_ctrl);
4263 adapter->smartspeed++;
4264 if (!e1000_phy_setup_autoneg(&adapter->hw) &&
4265 !e1000_read_phy_reg(&adapter->hw, PHY_CTRL,
4266 &phy_ctrl)) {
4267 phy_ctrl |= (MII_CR_AUTO_NEG_EN |
4268 MII_CR_RESTART_AUTO_NEG);
4269 e1000_write_phy_reg(&adapter->hw, PHY_CTRL,
4270 phy_ctrl);
4273 return;
4274 } else if (adapter->smartspeed == E1000_SMARTSPEED_DOWNSHIFT) {
4275 /* If still no link, perhaps using 2/3 pair cable */
4276 e1000_read_phy_reg(&adapter->hw, PHY_1000T_CTRL, &phy_ctrl);
4277 phy_ctrl |= CR_1000T_MS_ENABLE;
4278 e1000_write_phy_reg(&adapter->hw, PHY_1000T_CTRL, phy_ctrl);
4279 if (!e1000_phy_setup_autoneg(&adapter->hw) &&
4280 !e1000_read_phy_reg(&adapter->hw, PHY_CTRL, &phy_ctrl)) {
4281 phy_ctrl |= (MII_CR_AUTO_NEG_EN |
4282 MII_CR_RESTART_AUTO_NEG);
4283 e1000_write_phy_reg(&adapter->hw, PHY_CTRL, phy_ctrl);
4286 /* Restart process after E1000_SMARTSPEED_MAX iterations */
4287 if (adapter->smartspeed++ == E1000_SMARTSPEED_MAX)
4288 adapter->smartspeed = 0;
4292 * e1000_ioctl -
4293 * @netdev:
4294 * @ifreq:
4295 * @cmd:
4298 static int
4299 e1000_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
4301 switch (cmd) {
4302 case SIOCGMIIPHY:
4303 case SIOCGMIIREG:
4304 case SIOCSMIIREG:
4305 return e1000_mii_ioctl(netdev, ifr, cmd);
4306 default:
4307 return -EOPNOTSUPP;
4312 * e1000_mii_ioctl -
4313 * @netdev:
4314 * @ifreq:
4315 * @cmd:
4318 static int
4319 e1000_mii_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
4321 struct e1000_adapter *adapter = netdev_priv(netdev);
4322 struct mii_ioctl_data *data = if_mii(ifr);
4323 int retval;
4324 uint16_t mii_reg;
4325 uint16_t spddplx;
4326 unsigned long flags;
4328 if (adapter->hw.media_type != e1000_media_type_copper)
4329 return -EOPNOTSUPP;
4331 switch (cmd) {
4332 case SIOCGMIIPHY:
4333 data->phy_id = adapter->hw.phy_addr;
4334 break;
4335 case SIOCGMIIREG:
4336 if (!capable(CAP_NET_ADMIN))
4337 return -EPERM;
4338 spin_lock_irqsave(&adapter->stats_lock, flags);
4339 if (e1000_read_phy_reg(&adapter->hw, data->reg_num & 0x1F,
4340 &data->val_out)) {
4341 spin_unlock_irqrestore(&adapter->stats_lock, flags);
4342 return -EIO;
4344 spin_unlock_irqrestore(&adapter->stats_lock, flags);
4345 break;
4346 case SIOCSMIIREG:
4347 if (!capable(CAP_NET_ADMIN))
4348 return -EPERM;
4349 if (data->reg_num & ~(0x1F))
4350 return -EFAULT;
4351 mii_reg = data->val_in;
4352 spin_lock_irqsave(&adapter->stats_lock, flags);
4353 if (e1000_write_phy_reg(&adapter->hw, data->reg_num,
4354 mii_reg)) {
4355 spin_unlock_irqrestore(&adapter->stats_lock, flags);
4356 return -EIO;
4358 if (adapter->hw.media_type == e1000_media_type_copper) {
4359 switch (data->reg_num) {
4360 case PHY_CTRL:
4361 if (mii_reg & MII_CR_POWER_DOWN)
4362 break;
4363 if (mii_reg & MII_CR_AUTO_NEG_EN) {
4364 adapter->hw.autoneg = 1;
4365 adapter->hw.autoneg_advertised = 0x2F;
4366 } else {
4367 if (mii_reg & 0x40)
4368 spddplx = SPEED_1000;
4369 else if (mii_reg & 0x2000)
4370 spddplx = SPEED_100;
4371 else
4372 spddplx = SPEED_10;
4373 spddplx += (mii_reg & 0x100)
4374 ? DUPLEX_FULL :
4375 DUPLEX_HALF;
4376 retval = e1000_set_spd_dplx(adapter,
4377 spddplx);
4378 if (retval) {
4379 spin_unlock_irqrestore(
4380 &adapter->stats_lock,
4381 flags);
4382 return retval;
4385 if (netif_running(adapter->netdev))
4386 e1000_reinit_locked(adapter);
4387 else
4388 e1000_reset(adapter);
4389 break;
4390 case M88E1000_PHY_SPEC_CTRL:
4391 case M88E1000_EXT_PHY_SPEC_CTRL:
4392 if (e1000_phy_reset(&adapter->hw)) {
4393 spin_unlock_irqrestore(
4394 &adapter->stats_lock, flags);
4395 return -EIO;
4397 break;
4399 } else {
4400 switch (data->reg_num) {
4401 case PHY_CTRL:
4402 if (mii_reg & MII_CR_POWER_DOWN)
4403 break;
4404 if (netif_running(adapter->netdev))
4405 e1000_reinit_locked(adapter);
4406 else
4407 e1000_reset(adapter);
4408 break;
4411 spin_unlock_irqrestore(&adapter->stats_lock, flags);
4412 break;
4413 default:
4414 return -EOPNOTSUPP;
4416 return E1000_SUCCESS;
4419 void
4420 e1000_pci_set_mwi(struct e1000_hw *hw)
4422 struct e1000_adapter *adapter = hw->back;
4423 int ret_val = pci_set_mwi(adapter->pdev);
4425 if (ret_val)
4426 DPRINTK(PROBE, ERR, "Error in setting MWI\n");
4429 void
4430 e1000_pci_clear_mwi(struct e1000_hw *hw)
4432 struct e1000_adapter *adapter = hw->back;
4434 pci_clear_mwi(adapter->pdev);
4437 void
4438 e1000_read_pci_cfg(struct e1000_hw *hw, uint32_t reg, uint16_t *value)
4440 struct e1000_adapter *adapter = hw->back;
4442 pci_read_config_word(adapter->pdev, reg, value);
4445 void
4446 e1000_write_pci_cfg(struct e1000_hw *hw, uint32_t reg, uint16_t *value)
4448 struct e1000_adapter *adapter = hw->back;
4450 pci_write_config_word(adapter->pdev, reg, *value);
4453 void
4454 e1000_io_write(struct e1000_hw *hw, unsigned long port, uint32_t value)
4456 outl(value, port);
4459 static void
4460 e1000_vlan_rx_register(struct net_device *netdev, struct vlan_group *grp)
4462 struct e1000_adapter *adapter = netdev_priv(netdev);
4463 uint32_t ctrl, rctl;
4465 e1000_irq_disable(adapter);
4466 adapter->vlgrp = grp;
4468 if (grp) {
4469 /* enable VLAN tag insert/strip */
4470 ctrl = E1000_READ_REG(&adapter->hw, CTRL);
4471 ctrl |= E1000_CTRL_VME;
4472 E1000_WRITE_REG(&adapter->hw, CTRL, ctrl);
4474 if (adapter->hw.mac_type != e1000_ich8lan) {
4475 /* enable VLAN receive filtering */
4476 rctl = E1000_READ_REG(&adapter->hw, RCTL);
4477 rctl |= E1000_RCTL_VFE;
4478 rctl &= ~E1000_RCTL_CFIEN;
4479 E1000_WRITE_REG(&adapter->hw, RCTL, rctl);
4480 e1000_update_mng_vlan(adapter);
4482 } else {
4483 /* disable VLAN tag insert/strip */
4484 ctrl = E1000_READ_REG(&adapter->hw, CTRL);
4485 ctrl &= ~E1000_CTRL_VME;
4486 E1000_WRITE_REG(&adapter->hw, CTRL, ctrl);
4488 if (adapter->hw.mac_type != e1000_ich8lan) {
4489 /* disable VLAN filtering */
4490 rctl = E1000_READ_REG(&adapter->hw, RCTL);
4491 rctl &= ~E1000_RCTL_VFE;
4492 E1000_WRITE_REG(&adapter->hw, RCTL, rctl);
4493 if (adapter->mng_vlan_id != (uint16_t)E1000_MNG_VLAN_NONE) {
4494 e1000_vlan_rx_kill_vid(netdev, adapter->mng_vlan_id);
4495 adapter->mng_vlan_id = E1000_MNG_VLAN_NONE;
4500 e1000_irq_enable(adapter);
4503 static void
4504 e1000_vlan_rx_add_vid(struct net_device *netdev, uint16_t vid)
4506 struct e1000_adapter *adapter = netdev_priv(netdev);
4507 uint32_t vfta, index;
4509 if ((adapter->hw.mng_cookie.status &
4510 E1000_MNG_DHCP_COOKIE_STATUS_VLAN_SUPPORT) &&
4511 (vid == adapter->mng_vlan_id))
4512 return;
4513 /* add VID to filter table */
4514 index = (vid >> 5) & 0x7F;
4515 vfta = E1000_READ_REG_ARRAY(&adapter->hw, VFTA, index);
4516 vfta |= (1 << (vid & 0x1F));
4517 e1000_write_vfta(&adapter->hw, index, vfta);
4520 static void
4521 e1000_vlan_rx_kill_vid(struct net_device *netdev, uint16_t vid)
4523 struct e1000_adapter *adapter = netdev_priv(netdev);
4524 uint32_t vfta, index;
4526 e1000_irq_disable(adapter);
4528 if (adapter->vlgrp)
4529 adapter->vlgrp->vlan_devices[vid] = NULL;
4531 e1000_irq_enable(adapter);
4533 if ((adapter->hw.mng_cookie.status &
4534 E1000_MNG_DHCP_COOKIE_STATUS_VLAN_SUPPORT) &&
4535 (vid == adapter->mng_vlan_id)) {
4536 /* release control to f/w */
4537 e1000_release_hw_control(adapter);
4538 return;
4541 /* remove VID from filter table */
4542 index = (vid >> 5) & 0x7F;
4543 vfta = E1000_READ_REG_ARRAY(&adapter->hw, VFTA, index);
4544 vfta &= ~(1 << (vid & 0x1F));
4545 e1000_write_vfta(&adapter->hw, index, vfta);
4548 static void
4549 e1000_restore_vlan(struct e1000_adapter *adapter)
4551 e1000_vlan_rx_register(adapter->netdev, adapter->vlgrp);
4553 if (adapter->vlgrp) {
4554 uint16_t vid;
4555 for (vid = 0; vid < VLAN_GROUP_ARRAY_LEN; vid++) {
4556 if (!adapter->vlgrp->vlan_devices[vid])
4557 continue;
4558 e1000_vlan_rx_add_vid(adapter->netdev, vid);
4564 e1000_set_spd_dplx(struct e1000_adapter *adapter, uint16_t spddplx)
4566 adapter->hw.autoneg = 0;
4568 /* Fiber NICs only allow 1000 gbps Full duplex */
4569 if ((adapter->hw.media_type == e1000_media_type_fiber) &&
4570 spddplx != (SPEED_1000 + DUPLEX_FULL)) {
4571 DPRINTK(PROBE, ERR, "Unsupported Speed/Duplex configuration\n");
4572 return -EINVAL;
4575 switch (spddplx) {
4576 case SPEED_10 + DUPLEX_HALF:
4577 adapter->hw.forced_speed_duplex = e1000_10_half;
4578 break;
4579 case SPEED_10 + DUPLEX_FULL:
4580 adapter->hw.forced_speed_duplex = e1000_10_full;
4581 break;
4582 case SPEED_100 + DUPLEX_HALF:
4583 adapter->hw.forced_speed_duplex = e1000_100_half;
4584 break;
4585 case SPEED_100 + DUPLEX_FULL:
4586 adapter->hw.forced_speed_duplex = e1000_100_full;
4587 break;
4588 case SPEED_1000 + DUPLEX_FULL:
4589 adapter->hw.autoneg = 1;
4590 adapter->hw.autoneg_advertised = ADVERTISE_1000_FULL;
4591 break;
4592 case SPEED_1000 + DUPLEX_HALF: /* not supported */
4593 default:
4594 DPRINTK(PROBE, ERR, "Unsupported Speed/Duplex configuration\n");
4595 return -EINVAL;
4597 return 0;
4600 #ifdef CONFIG_PM
4601 /* Save/restore 16 or 64 dwords of PCI config space depending on which
4602 * bus we're on (PCI(X) vs. PCI-E)
4604 #define PCIE_CONFIG_SPACE_LEN 256
4605 #define PCI_CONFIG_SPACE_LEN 64
4606 static int
4607 e1000_pci_save_state(struct e1000_adapter *adapter)
4609 struct pci_dev *dev = adapter->pdev;
4610 int size;
4611 int i;
4613 if (adapter->hw.mac_type >= e1000_82571)
4614 size = PCIE_CONFIG_SPACE_LEN;
4615 else
4616 size = PCI_CONFIG_SPACE_LEN;
4618 WARN_ON(adapter->config_space != NULL);
4620 adapter->config_space = kmalloc(size, GFP_KERNEL);
4621 if (!adapter->config_space) {
4622 DPRINTK(PROBE, ERR, "unable to allocate %d bytes\n", size);
4623 return -ENOMEM;
4625 for (i = 0; i < (size / 4); i++)
4626 pci_read_config_dword(dev, i * 4, &adapter->config_space[i]);
4627 return 0;
4630 static void
4631 e1000_pci_restore_state(struct e1000_adapter *adapter)
4633 struct pci_dev *dev = adapter->pdev;
4634 int size;
4635 int i;
4637 if (adapter->config_space == NULL)
4638 return;
4640 if (adapter->hw.mac_type >= e1000_82571)
4641 size = PCIE_CONFIG_SPACE_LEN;
4642 else
4643 size = PCI_CONFIG_SPACE_LEN;
4644 for (i = 0; i < (size / 4); i++)
4645 pci_write_config_dword(dev, i * 4, adapter->config_space[i]);
4646 kfree(adapter->config_space);
4647 adapter->config_space = NULL;
4648 return;
4650 #endif /* CONFIG_PM */
4652 static int
4653 e1000_suspend(struct pci_dev *pdev, pm_message_t state)
4655 struct net_device *netdev = pci_get_drvdata(pdev);
4656 struct e1000_adapter *adapter = netdev_priv(netdev);
4657 uint32_t ctrl, ctrl_ext, rctl, manc, status;
4658 uint32_t wufc = adapter->wol;
4659 #ifdef CONFIG_PM
4660 int retval = 0;
4661 #endif
4663 netif_device_detach(netdev);
4665 if (netif_running(netdev)) {
4666 WARN_ON(test_bit(__E1000_RESETTING, &adapter->flags));
4667 e1000_down(adapter);
4670 #ifdef CONFIG_PM
4671 /* Implement our own version of pci_save_state(pdev) because pci-
4672 * express adapters have 256-byte config spaces. */
4673 retval = e1000_pci_save_state(adapter);
4674 if (retval)
4675 return retval;
4676 #endif
4678 status = E1000_READ_REG(&adapter->hw, STATUS);
4679 if (status & E1000_STATUS_LU)
4680 wufc &= ~E1000_WUFC_LNKC;
4682 if (wufc) {
4683 e1000_setup_rctl(adapter);
4684 e1000_set_multi(netdev);
4686 /* turn on all-multi mode if wake on multicast is enabled */
4687 if (wufc & E1000_WUFC_MC) {
4688 rctl = E1000_READ_REG(&adapter->hw, RCTL);
4689 rctl |= E1000_RCTL_MPE;
4690 E1000_WRITE_REG(&adapter->hw, RCTL, rctl);
4693 if (adapter->hw.mac_type >= e1000_82540) {
4694 ctrl = E1000_READ_REG(&adapter->hw, CTRL);
4695 /* advertise wake from D3Cold */
4696 #define E1000_CTRL_ADVD3WUC 0x00100000
4697 /* phy power management enable */
4698 #define E1000_CTRL_EN_PHY_PWR_MGMT 0x00200000
4699 ctrl |= E1000_CTRL_ADVD3WUC |
4700 E1000_CTRL_EN_PHY_PWR_MGMT;
4701 E1000_WRITE_REG(&adapter->hw, CTRL, ctrl);
4704 if (adapter->hw.media_type == e1000_media_type_fiber ||
4705 adapter->hw.media_type == e1000_media_type_internal_serdes) {
4706 /* keep the laser running in D3 */
4707 ctrl_ext = E1000_READ_REG(&adapter->hw, CTRL_EXT);
4708 ctrl_ext |= E1000_CTRL_EXT_SDP7_DATA;
4709 E1000_WRITE_REG(&adapter->hw, CTRL_EXT, ctrl_ext);
4712 /* Allow time for pending master requests to run */
4713 e1000_disable_pciex_master(&adapter->hw);
4715 E1000_WRITE_REG(&adapter->hw, WUC, E1000_WUC_PME_EN);
4716 E1000_WRITE_REG(&adapter->hw, WUFC, wufc);
4717 pci_enable_wake(pdev, PCI_D3hot, 1);
4718 pci_enable_wake(pdev, PCI_D3cold, 1);
4719 } else {
4720 E1000_WRITE_REG(&adapter->hw, WUC, 0);
4721 E1000_WRITE_REG(&adapter->hw, WUFC, 0);
4722 pci_enable_wake(pdev, PCI_D3hot, 0);
4723 pci_enable_wake(pdev, PCI_D3cold, 0);
4726 if (adapter->hw.mac_type < e1000_82571 &&
4727 adapter->hw.media_type == e1000_media_type_copper) {
4728 manc = E1000_READ_REG(&adapter->hw, MANC);
4729 if (manc & E1000_MANC_SMBUS_EN) {
4730 manc |= E1000_MANC_ARP_EN;
4731 E1000_WRITE_REG(&adapter->hw, MANC, manc);
4732 pci_enable_wake(pdev, PCI_D3hot, 1);
4733 pci_enable_wake(pdev, PCI_D3cold, 1);
4737 if (adapter->hw.phy_type == e1000_phy_igp_3)
4738 e1000_phy_powerdown_workaround(&adapter->hw);
4740 /* Release control of h/w to f/w. If f/w is AMT enabled, this
4741 * would have already happened in close and is redundant. */
4742 e1000_release_hw_control(adapter);
4744 pci_disable_device(pdev);
4746 pci_set_power_state(pdev, pci_choose_state(pdev, state));
4748 return 0;
4751 #ifdef CONFIG_PM
4752 static int
4753 e1000_resume(struct pci_dev *pdev)
4755 struct net_device *netdev = pci_get_drvdata(pdev);
4756 struct e1000_adapter *adapter = netdev_priv(netdev);
4757 uint32_t manc, err;
4759 pci_set_power_state(pdev, PCI_D0);
4760 e1000_pci_restore_state(adapter);
4761 if ((err = pci_enable_device(pdev))) {
4762 printk(KERN_ERR "e1000: Cannot enable PCI device from suspend\n");
4763 return err;
4765 pci_set_master(pdev);
4767 pci_enable_wake(pdev, PCI_D3hot, 0);
4768 pci_enable_wake(pdev, PCI_D3cold, 0);
4770 e1000_reset(adapter);
4771 E1000_WRITE_REG(&adapter->hw, WUS, ~0);
4773 if (netif_running(netdev))
4774 e1000_up(adapter);
4776 netif_device_attach(netdev);
4778 if (adapter->hw.mac_type < e1000_82571 &&
4779 adapter->hw.media_type == e1000_media_type_copper) {
4780 manc = E1000_READ_REG(&adapter->hw, MANC);
4781 manc &= ~(E1000_MANC_ARP_EN);
4782 E1000_WRITE_REG(&adapter->hw, MANC, manc);
4785 /* If the controller is 82573 and f/w is AMT, do not set
4786 * DRV_LOAD until the interface is up. For all other cases,
4787 * let the f/w know that the h/w is now under the control
4788 * of the driver. */
4789 if (adapter->hw.mac_type != e1000_82573 ||
4790 !e1000_check_mng_mode(&adapter->hw))
4791 e1000_get_hw_control(adapter);
4793 return 0;
4795 #endif
4797 static void e1000_shutdown(struct pci_dev *pdev)
4799 e1000_suspend(pdev, PMSG_SUSPEND);
4802 #ifdef CONFIG_NET_POLL_CONTROLLER
4804 * Polling 'interrupt' - used by things like netconsole to send skbs
4805 * without having to re-enable interrupts. It's not called while
4806 * the interrupt routine is executing.
4808 static void
4809 e1000_netpoll(struct net_device *netdev)
4811 struct e1000_adapter *adapter = netdev_priv(netdev);
4813 disable_irq(adapter->pdev->irq);
4814 e1000_intr(adapter->pdev->irq, netdev, NULL);
4815 e1000_clean_tx_irq(adapter, adapter->tx_ring);
4816 #ifndef CONFIG_E1000_NAPI
4817 adapter->clean_rx(adapter, adapter->rx_ring);
4818 #endif
4819 enable_irq(adapter->pdev->irq);
4821 #endif
4824 * e1000_io_error_detected - called when PCI error is detected
4825 * @pdev: Pointer to PCI device
4826 * @state: The current pci conneection state
4828 * This function is called after a PCI bus error affecting
4829 * this device has been detected.
4831 static pci_ers_result_t e1000_io_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
4833 struct net_device *netdev = pci_get_drvdata(pdev);
4834 struct e1000_adapter *adapter = netdev->priv;
4836 netif_device_detach(netdev);
4838 if (netif_running(netdev))
4839 e1000_down(adapter);
4840 pci_disable_device(pdev);
4842 /* Request a slot slot reset. */
4843 return PCI_ERS_RESULT_NEED_RESET;
4847 * e1000_io_slot_reset - called after the pci bus has been reset.
4848 * @pdev: Pointer to PCI device
4850 * Restart the card from scratch, as if from a cold-boot. Implementation
4851 * resembles the first-half of the e1000_resume routine.
4853 static pci_ers_result_t e1000_io_slot_reset(struct pci_dev *pdev)
4855 struct net_device *netdev = pci_get_drvdata(pdev);
4856 struct e1000_adapter *adapter = netdev->priv;
4858 if (pci_enable_device(pdev)) {
4859 printk(KERN_ERR "e1000: Cannot re-enable PCI device after reset.\n");
4860 return PCI_ERS_RESULT_DISCONNECT;
4862 pci_set_master(pdev);
4864 pci_enable_wake(pdev, 3, 0);
4865 pci_enable_wake(pdev, 4, 0); /* 4 == D3 cold */
4867 /* Perform card reset only on one instance of the card */
4868 if (PCI_FUNC (pdev->devfn) != 0)
4869 return PCI_ERS_RESULT_RECOVERED;
4871 e1000_reset(adapter);
4872 E1000_WRITE_REG(&adapter->hw, WUS, ~0);
4874 return PCI_ERS_RESULT_RECOVERED;
4878 * e1000_io_resume - called when traffic can start flowing again.
4879 * @pdev: Pointer to PCI device
4881 * This callback is called when the error recovery driver tells us that
4882 * its OK to resume normal operation. Implementation resembles the
4883 * second-half of the e1000_resume routine.
4885 static void e1000_io_resume(struct pci_dev *pdev)
4887 struct net_device *netdev = pci_get_drvdata(pdev);
4888 struct e1000_adapter *adapter = netdev->priv;
4889 uint32_t manc, swsm;
4891 if (netif_running(netdev)) {
4892 if (e1000_up(adapter)) {
4893 printk("e1000: can't bring device back up after reset\n");
4894 return;
4898 netif_device_attach(netdev);
4900 if (adapter->hw.mac_type >= e1000_82540 &&
4901 adapter->hw.media_type == e1000_media_type_copper) {
4902 manc = E1000_READ_REG(&adapter->hw, MANC);
4903 manc &= ~(E1000_MANC_ARP_EN);
4904 E1000_WRITE_REG(&adapter->hw, MANC, manc);
4907 switch (adapter->hw.mac_type) {
4908 case e1000_82573:
4909 swsm = E1000_READ_REG(&adapter->hw, SWSM);
4910 E1000_WRITE_REG(&adapter->hw, SWSM,
4911 swsm | E1000_SWSM_DRV_LOAD);
4912 break;
4913 default:
4914 break;
4917 if (netif_running(netdev))
4918 mod_timer(&adapter->watchdog_timer, jiffies);
4921 /* e1000_main.c */