blob: 9c6c1fbbc4122ddc9eb9f04348be94fd026bbf0e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*******************************************************************************
2
3
4 Copyright(c) 1999 - 2004 Intel Corporation. All rights reserved.
5
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2 of the License, or (at your option)
9 any later version.
10
11 This program is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 more details.
15
16 You should have received a copy of the GNU General Public License along with
17 this program; if not, write to the Free Software Foundation, Inc., 59
18 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
20 The full GNU General Public License is included in this distribution in the
21 file called LICENSE.
22
23 Contact Information:
24 Linux NICS <linux.nics@intel.com>
25 Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
26
27*******************************************************************************/
28
29#include "e1000.h"
30
31/* Change Log
32 * 5.3.12 6/7/04
33 * - kcompat NETIF_MSG for older kernels (2.4.9) <sean.p.mcdermott@intel.com>
34 * - if_mii support and associated kcompat for older kernels
35 * - More errlogging support from Jon Mason <jonmason@us.ibm.com>
36 * - Fix TSO issues on PPC64 machines -- Jon Mason <jonmason@us.ibm.com>
37 *
38 * 5.7.1 12/16/04
39 * - Resurrect 82547EI/GI related fix in e1000_intr to avoid deadlocks. This
40 * fix was removed as it caused system instability. The suspected cause of
41 * this is the called to e1000_irq_disable in e1000_intr. Inlined the
42 * required piece of e1000_irq_disable into e1000_intr - Anton Blanchard
43 * 5.7.0 12/10/04
44 * - include fix to the condition that determines when to quit NAPI - Robert Olsson
45 * - use netif_poll_{disable/enable} to synchronize between NAPI and i/f up/down
46 * 5.6.5 11/01/04
47 * - Enabling NETIF_F_SG without checksum offload is illegal -
48 John Mason <jdmason@us.ibm.com>
49 * 5.6.3 10/26/04
50 * - Remove redundant initialization - Jamal Hadi
51 * - Reset buffer_info->dma in tx resource cleanup logic
52 * 5.6.2 10/12/04
53 * - Avoid filling tx_ring completely - shemminger@osdl.org
54 * - Replace schedule_timeout() with msleep()/msleep_interruptible() -
55 * nacc@us.ibm.com
56 * - Sparse cleanup - shemminger@osdl.org
57 * - Fix tx resource cleanup logic
58 * - LLTX support - ak@suse.de and hadi@cyberus.ca
59 */
60
61char e1000_driver_name[] = "e1000";
62char e1000_driver_string[] = "Intel(R) PRO/1000 Network Driver";
63#ifndef CONFIG_E1000_NAPI
64#define DRIVERNAPI
65#else
66#define DRIVERNAPI "-NAPI"
67#endif
68#define DRV_VERSION "5.7.6-k2"DRIVERNAPI
69char e1000_driver_version[] = DRV_VERSION;
70char e1000_copyright[] = "Copyright (c) 1999-2004 Intel Corporation.";
71
72/* e1000_pci_tbl - PCI Device ID Table
73 *
74 * Last entry must be all 0s
75 *
76 * Macro expands to...
77 * {PCI_DEVICE(PCI_VENDOR_ID_INTEL, device_id)}
78 */
79static struct pci_device_id e1000_pci_tbl[] = {
80 INTEL_E1000_ETHERNET_DEVICE(0x1000),
81 INTEL_E1000_ETHERNET_DEVICE(0x1001),
82 INTEL_E1000_ETHERNET_DEVICE(0x1004),
83 INTEL_E1000_ETHERNET_DEVICE(0x1008),
84 INTEL_E1000_ETHERNET_DEVICE(0x1009),
85 INTEL_E1000_ETHERNET_DEVICE(0x100C),
86 INTEL_E1000_ETHERNET_DEVICE(0x100D),
87 INTEL_E1000_ETHERNET_DEVICE(0x100E),
88 INTEL_E1000_ETHERNET_DEVICE(0x100F),
89 INTEL_E1000_ETHERNET_DEVICE(0x1010),
90 INTEL_E1000_ETHERNET_DEVICE(0x1011),
91 INTEL_E1000_ETHERNET_DEVICE(0x1012),
92 INTEL_E1000_ETHERNET_DEVICE(0x1013),
93 INTEL_E1000_ETHERNET_DEVICE(0x1014),
94 INTEL_E1000_ETHERNET_DEVICE(0x1015),
95 INTEL_E1000_ETHERNET_DEVICE(0x1016),
96 INTEL_E1000_ETHERNET_DEVICE(0x1017),
97 INTEL_E1000_ETHERNET_DEVICE(0x1018),
98 INTEL_E1000_ETHERNET_DEVICE(0x1019),
99 INTEL_E1000_ETHERNET_DEVICE(0x101D),
100 INTEL_E1000_ETHERNET_DEVICE(0x101E),
101 INTEL_E1000_ETHERNET_DEVICE(0x1026),
102 INTEL_E1000_ETHERNET_DEVICE(0x1027),
103 INTEL_E1000_ETHERNET_DEVICE(0x1028),
104 INTEL_E1000_ETHERNET_DEVICE(0x1075),
105 INTEL_E1000_ETHERNET_DEVICE(0x1076),
106 INTEL_E1000_ETHERNET_DEVICE(0x1077),
107 INTEL_E1000_ETHERNET_DEVICE(0x1078),
108 INTEL_E1000_ETHERNET_DEVICE(0x1079),
109 INTEL_E1000_ETHERNET_DEVICE(0x107A),
110 INTEL_E1000_ETHERNET_DEVICE(0x107B),
111 INTEL_E1000_ETHERNET_DEVICE(0x107C),
112 INTEL_E1000_ETHERNET_DEVICE(0x108A),
113 /* required last entry */
114 {0,}
115};
116
117MODULE_DEVICE_TABLE(pci, e1000_pci_tbl);
118
119int e1000_up(struct e1000_adapter *adapter);
120void e1000_down(struct e1000_adapter *adapter);
121void e1000_reset(struct e1000_adapter *adapter);
122int e1000_set_spd_dplx(struct e1000_adapter *adapter, uint16_t spddplx);
123int e1000_setup_tx_resources(struct e1000_adapter *adapter);
124int e1000_setup_rx_resources(struct e1000_adapter *adapter);
125void e1000_free_tx_resources(struct e1000_adapter *adapter);
126void e1000_free_rx_resources(struct e1000_adapter *adapter);
127void e1000_update_stats(struct e1000_adapter *adapter);
128
129/* Local Function Prototypes */
130
131static int e1000_init_module(void);
132static void e1000_exit_module(void);
133static int e1000_probe(struct pci_dev *pdev, const struct pci_device_id *ent);
134static void __devexit e1000_remove(struct pci_dev *pdev);
135static int e1000_sw_init(struct e1000_adapter *adapter);
136static int e1000_open(struct net_device *netdev);
137static int e1000_close(struct net_device *netdev);
138static void e1000_configure_tx(struct e1000_adapter *adapter);
139static void e1000_configure_rx(struct e1000_adapter *adapter);
140static void e1000_setup_rctl(struct e1000_adapter *adapter);
141static void e1000_clean_tx_ring(struct e1000_adapter *adapter);
142static void e1000_clean_rx_ring(struct e1000_adapter *adapter);
143static void e1000_set_multi(struct net_device *netdev);
144static void e1000_update_phy_info(unsigned long data);
145static void e1000_watchdog(unsigned long data);
146static void e1000_watchdog_task(struct e1000_adapter *adapter);
147static void e1000_82547_tx_fifo_stall(unsigned long data);
148static int e1000_xmit_frame(struct sk_buff *skb, struct net_device *netdev);
149static struct net_device_stats * e1000_get_stats(struct net_device *netdev);
150static int e1000_change_mtu(struct net_device *netdev, int new_mtu);
151static int e1000_set_mac(struct net_device *netdev, void *p);
152static irqreturn_t e1000_intr(int irq, void *data, struct pt_regs *regs);
153static boolean_t e1000_clean_tx_irq(struct e1000_adapter *adapter);
154#ifdef CONFIG_E1000_NAPI
155static int e1000_clean(struct net_device *netdev, int *budget);
156static boolean_t e1000_clean_rx_irq(struct e1000_adapter *adapter,
157 int *work_done, int work_to_do);
158#else
159static boolean_t e1000_clean_rx_irq(struct e1000_adapter *adapter);
160#endif
161static void e1000_alloc_rx_buffers(struct e1000_adapter *adapter);
162static int e1000_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd);
163static int e1000_mii_ioctl(struct net_device *netdev, struct ifreq *ifr,
164 int cmd);
165void e1000_set_ethtool_ops(struct net_device *netdev);
166static void e1000_enter_82542_rst(struct e1000_adapter *adapter);
167static void e1000_leave_82542_rst(struct e1000_adapter *adapter);
168static void e1000_tx_timeout(struct net_device *dev);
169static void e1000_tx_timeout_task(struct net_device *dev);
170static void e1000_smartspeed(struct e1000_adapter *adapter);
171static inline int e1000_82547_fifo_workaround(struct e1000_adapter *adapter,
172 struct sk_buff *skb);
173
174static void e1000_vlan_rx_register(struct net_device *netdev, struct vlan_group *grp);
175static void e1000_vlan_rx_add_vid(struct net_device *netdev, uint16_t vid);
176static void e1000_vlan_rx_kill_vid(struct net_device *netdev, uint16_t vid);
177static void e1000_restore_vlan(struct e1000_adapter *adapter);
178
179static int e1000_notify_reboot(struct notifier_block *, unsigned long event, void *ptr);
180static int e1000_suspend(struct pci_dev *pdev, uint32_t state);
181#ifdef CONFIG_PM
182static int e1000_resume(struct pci_dev *pdev);
183#endif
184
185#ifdef CONFIG_NET_POLL_CONTROLLER
186/* for netdump / net console */
187static void e1000_netpoll (struct net_device *netdev);
188#endif
189
190struct notifier_block e1000_notifier_reboot = {
191 .notifier_call = e1000_notify_reboot,
192 .next = NULL,
193 .priority = 0
194};
195
196/* Exported from other modules */
197
198extern void e1000_check_options(struct e1000_adapter *adapter);
199
200static struct pci_driver e1000_driver = {
201 .name = e1000_driver_name,
202 .id_table = e1000_pci_tbl,
203 .probe = e1000_probe,
204 .remove = __devexit_p(e1000_remove),
205 /* Power Managment Hooks */
206#ifdef CONFIG_PM
207 .suspend = e1000_suspend,
208 .resume = e1000_resume
209#endif
210};
211
212MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
213MODULE_DESCRIPTION("Intel(R) PRO/1000 Network Driver");
214MODULE_LICENSE("GPL");
215MODULE_VERSION(DRV_VERSION);
216
217static int debug = NETIF_MSG_DRV | NETIF_MSG_PROBE;
218module_param(debug, int, 0);
219MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
220
221/**
222 * e1000_init_module - Driver Registration Routine
223 *
224 * e1000_init_module is the first routine called when the driver is
225 * loaded. All it does is register with the PCI subsystem.
226 **/
227
228static int __init
229e1000_init_module(void)
230{
231 int ret;
232 printk(KERN_INFO "%s - version %s\n",
233 e1000_driver_string, e1000_driver_version);
234
235 printk(KERN_INFO "%s\n", e1000_copyright);
236
237 ret = pci_module_init(&e1000_driver);
238 if(ret >= 0) {
239 register_reboot_notifier(&e1000_notifier_reboot);
240 }
241 return ret;
242}
243
244module_init(e1000_init_module);
245
246/**
247 * e1000_exit_module - Driver Exit Cleanup Routine
248 *
249 * e1000_exit_module is called just before the driver is removed
250 * from memory.
251 **/
252
253static void __exit
254e1000_exit_module(void)
255{
256 unregister_reboot_notifier(&e1000_notifier_reboot);
257 pci_unregister_driver(&e1000_driver);
258}
259
260module_exit(e1000_exit_module);
261
262/**
263 * e1000_irq_disable - Mask off interrupt generation on the NIC
264 * @adapter: board private structure
265 **/
266
267static inline void
268e1000_irq_disable(struct e1000_adapter *adapter)
269{
270 atomic_inc(&adapter->irq_sem);
271 E1000_WRITE_REG(&adapter->hw, IMC, ~0);
272 E1000_WRITE_FLUSH(&adapter->hw);
273 synchronize_irq(adapter->pdev->irq);
274}
275
276/**
277 * e1000_irq_enable - Enable default interrupt generation settings
278 * @adapter: board private structure
279 **/
280
281static inline void
282e1000_irq_enable(struct e1000_adapter *adapter)
283{
284 if(likely(atomic_dec_and_test(&adapter->irq_sem))) {
285 E1000_WRITE_REG(&adapter->hw, IMS, IMS_ENABLE_MASK);
286 E1000_WRITE_FLUSH(&adapter->hw);
287 }
288}
289
290int
291e1000_up(struct e1000_adapter *adapter)
292{
293 struct net_device *netdev = adapter->netdev;
294 int err;
295
296 /* hardware has been reset, we need to reload some things */
297
298 /* Reset the PHY if it was previously powered down */
299 if(adapter->hw.media_type == e1000_media_type_copper) {
300 uint16_t mii_reg;
301 e1000_read_phy_reg(&adapter->hw, PHY_CTRL, &mii_reg);
302 if(mii_reg & MII_CR_POWER_DOWN)
303 e1000_phy_reset(&adapter->hw);
304 }
305
306 e1000_set_multi(netdev);
307
308 e1000_restore_vlan(adapter);
309
310 e1000_configure_tx(adapter);
311 e1000_setup_rctl(adapter);
312 e1000_configure_rx(adapter);
313 e1000_alloc_rx_buffers(adapter);
314
Malli Chilakalafa4f7ef2005-04-28 19:39:13 -0700315#ifdef CONFIG_PCI_MSI
316 if(adapter->hw.mac_type > e1000_82547_rev_2) {
317 adapter->have_msi = TRUE;
318 if((err = pci_enable_msi(adapter->pdev))) {
319 DPRINTK(PROBE, ERR,
320 "Unable to allocate MSI interrupt Error: %d\n", err);
321 adapter->have_msi = FALSE;
322 }
323 }
324#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 if((err = request_irq(adapter->pdev->irq, &e1000_intr,
326 SA_SHIRQ | SA_SAMPLE_RANDOM,
327 netdev->name, netdev)))
328 return err;
329
330 mod_timer(&adapter->watchdog_timer, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
332#ifdef CONFIG_E1000_NAPI
333 netif_poll_enable(netdev);
334#endif
Malli Chilakala5de55622005-04-28 19:39:30 -0700335 e1000_irq_enable(adapter);
336
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 return 0;
338}
339
340void
341e1000_down(struct e1000_adapter *adapter)
342{
343 struct net_device *netdev = adapter->netdev;
344
345 e1000_irq_disable(adapter);
346 free_irq(adapter->pdev->irq, netdev);
Malli Chilakalafa4f7ef2005-04-28 19:39:13 -0700347#ifdef CONFIG_PCI_MSI
348 if(adapter->hw.mac_type > e1000_82547_rev_2 &&
349 adapter->have_msi == TRUE)
350 pci_disable_msi(adapter->pdev);
351#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 del_timer_sync(&adapter->tx_fifo_stall_timer);
353 del_timer_sync(&adapter->watchdog_timer);
354 del_timer_sync(&adapter->phy_info_timer);
355
356#ifdef CONFIG_E1000_NAPI
357 netif_poll_disable(netdev);
358#endif
359 adapter->link_speed = 0;
360 adapter->link_duplex = 0;
361 netif_carrier_off(netdev);
362 netif_stop_queue(netdev);
363
364 e1000_reset(adapter);
365 e1000_clean_tx_ring(adapter);
366 e1000_clean_rx_ring(adapter);
367
368 /* If WoL is not enabled
369 * Power down the PHY so no link is implied when interface is down */
370 if(!adapter->wol && adapter->hw.media_type == e1000_media_type_copper) {
371 uint16_t mii_reg;
372 e1000_read_phy_reg(&adapter->hw, PHY_CTRL, &mii_reg);
373 mii_reg |= MII_CR_POWER_DOWN;
374 e1000_write_phy_reg(&adapter->hw, PHY_CTRL, mii_reg);
Malli Chilakala4e48a2b2005-04-28 19:39:53 -0700375 mdelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 }
377}
378
379void
380e1000_reset(struct e1000_adapter *adapter)
381{
382 uint32_t pba;
383
384 /* Repartition Pba for greater than 9k mtu
385 * To take effect CTRL.RST is required.
386 */
387
388 if(adapter->hw.mac_type < e1000_82547) {
389 if(adapter->rx_buffer_len > E1000_RXBUFFER_8192)
390 pba = E1000_PBA_40K;
391 else
392 pba = E1000_PBA_48K;
393 } else {
394 if(adapter->rx_buffer_len > E1000_RXBUFFER_8192)
395 pba = E1000_PBA_22K;
396 else
397 pba = E1000_PBA_30K;
398 adapter->tx_fifo_head = 0;
399 adapter->tx_head_addr = pba << E1000_TX_HEAD_ADDR_SHIFT;
400 adapter->tx_fifo_size =
401 (E1000_PBA_40K - pba) << E1000_PBA_BYTES_SHIFT;
402 atomic_set(&adapter->tx_fifo_stall, 0);
403 }
404 E1000_WRITE_REG(&adapter->hw, PBA, pba);
405
406 /* flow control settings */
407 adapter->hw.fc_high_water = (pba << E1000_PBA_BYTES_SHIFT) -
408 E1000_FC_HIGH_DIFF;
409 adapter->hw.fc_low_water = (pba << E1000_PBA_BYTES_SHIFT) -
410 E1000_FC_LOW_DIFF;
411 adapter->hw.fc_pause_time = E1000_FC_PAUSE_TIME;
412 adapter->hw.fc_send_xon = 1;
413 adapter->hw.fc = adapter->hw.original_fc;
414
415 e1000_reset_hw(&adapter->hw);
416 if(adapter->hw.mac_type >= e1000_82544)
417 E1000_WRITE_REG(&adapter->hw, WUC, 0);
418 if(e1000_init_hw(&adapter->hw))
419 DPRINTK(PROBE, ERR, "Hardware Error\n");
420
421 /* Enable h/w to recognize an 802.1Q VLAN Ethernet packet */
422 E1000_WRITE_REG(&adapter->hw, VET, ETHERNET_IEEE_VLAN_TYPE);
423
424 e1000_reset_adaptive(&adapter->hw);
425 e1000_phy_get_info(&adapter->hw, &adapter->phy_info);
426}
427
428/**
429 * e1000_probe - Device Initialization Routine
430 * @pdev: PCI device information struct
431 * @ent: entry in e1000_pci_tbl
432 *
433 * Returns 0 on success, negative on failure
434 *
435 * e1000_probe initializes an adapter identified by a pci_dev structure.
436 * The OS initialization, configuring of the adapter private structure,
437 * and a hardware reset occur.
438 **/
439
440static int __devinit
441e1000_probe(struct pci_dev *pdev,
442 const struct pci_device_id *ent)
443{
444 struct net_device *netdev;
445 struct e1000_adapter *adapter;
446 static int cards_found = 0;
447 unsigned long mmio_start;
448 int mmio_len;
449 int pci_using_dac;
450 int i;
451 int err;
452 uint16_t eeprom_data;
453 uint16_t eeprom_apme_mask = E1000_EEPROM_APME;
454
455 if((err = pci_enable_device(pdev)))
456 return err;
457
458 if(!(err = pci_set_dma_mask(pdev, DMA_64BIT_MASK))) {
459 pci_using_dac = 1;
460 } else {
461 if((err = pci_set_dma_mask(pdev, DMA_32BIT_MASK))) {
462 E1000_ERR("No usable DMA configuration, aborting\n");
463 return err;
464 }
465 pci_using_dac = 0;
466 }
467
468 if((err = pci_request_regions(pdev, e1000_driver_name)))
469 return err;
470
471 pci_set_master(pdev);
472
473 netdev = alloc_etherdev(sizeof(struct e1000_adapter));
474 if(!netdev) {
475 err = -ENOMEM;
476 goto err_alloc_etherdev;
477 }
478
479 SET_MODULE_OWNER(netdev);
480 SET_NETDEV_DEV(netdev, &pdev->dev);
481
482 pci_set_drvdata(pdev, netdev);
483 adapter = netdev->priv;
484 adapter->netdev = netdev;
485 adapter->pdev = pdev;
486 adapter->hw.back = adapter;
487 adapter->msg_enable = (1 << debug) - 1;
488
489 mmio_start = pci_resource_start(pdev, BAR_0);
490 mmio_len = pci_resource_len(pdev, BAR_0);
491
492 adapter->hw.hw_addr = ioremap(mmio_start, mmio_len);
493 if(!adapter->hw.hw_addr) {
494 err = -EIO;
495 goto err_ioremap;
496 }
497
498 for(i = BAR_1; i <= BAR_5; i++) {
499 if(pci_resource_len(pdev, i) == 0)
500 continue;
501 if(pci_resource_flags(pdev, i) & IORESOURCE_IO) {
502 adapter->hw.io_base = pci_resource_start(pdev, i);
503 break;
504 }
505 }
506
507 netdev->open = &e1000_open;
508 netdev->stop = &e1000_close;
509 netdev->hard_start_xmit = &e1000_xmit_frame;
510 netdev->get_stats = &e1000_get_stats;
511 netdev->set_multicast_list = &e1000_set_multi;
512 netdev->set_mac_address = &e1000_set_mac;
513 netdev->change_mtu = &e1000_change_mtu;
514 netdev->do_ioctl = &e1000_ioctl;
515 e1000_set_ethtool_ops(netdev);
516 netdev->tx_timeout = &e1000_tx_timeout;
517 netdev->watchdog_timeo = 5 * HZ;
518#ifdef CONFIG_E1000_NAPI
519 netdev->poll = &e1000_clean;
520 netdev->weight = 64;
521#endif
522 netdev->vlan_rx_register = e1000_vlan_rx_register;
523 netdev->vlan_rx_add_vid = e1000_vlan_rx_add_vid;
524 netdev->vlan_rx_kill_vid = e1000_vlan_rx_kill_vid;
525#ifdef CONFIG_NET_POLL_CONTROLLER
526 netdev->poll_controller = e1000_netpoll;
527#endif
528 strcpy(netdev->name, pci_name(pdev));
529
530 netdev->mem_start = mmio_start;
531 netdev->mem_end = mmio_start + mmio_len;
532 netdev->base_addr = adapter->hw.io_base;
533
534 adapter->bd_number = cards_found;
535
536 /* setup the private structure */
537
538 if((err = e1000_sw_init(adapter)))
539 goto err_sw_init;
540
541 if(adapter->hw.mac_type >= e1000_82543) {
542 netdev->features = NETIF_F_SG |
543 NETIF_F_HW_CSUM |
544 NETIF_F_HW_VLAN_TX |
545 NETIF_F_HW_VLAN_RX |
546 NETIF_F_HW_VLAN_FILTER;
547 }
548
549#ifdef NETIF_F_TSO
550 if((adapter->hw.mac_type >= e1000_82544) &&
551 (adapter->hw.mac_type != e1000_82547))
552 netdev->features |= NETIF_F_TSO;
553#endif
554 if(pci_using_dac)
555 netdev->features |= NETIF_F_HIGHDMA;
556
557 /* hard_start_xmit is safe against parallel locking */
558 netdev->features |= NETIF_F_LLTX;
559
560 /* before reading the EEPROM, reset the controller to
561 * put the device in a known good starting state */
562
563 e1000_reset_hw(&adapter->hw);
564
565 /* make sure the EEPROM is good */
566
567 if(e1000_validate_eeprom_checksum(&adapter->hw) < 0) {
568 DPRINTK(PROBE, ERR, "The EEPROM Checksum Is Not Valid\n");
569 err = -EIO;
570 goto err_eeprom;
571 }
572
573 /* copy the MAC address out of the EEPROM */
574
575 if (e1000_read_mac_addr(&adapter->hw))
576 DPRINTK(PROBE, ERR, "EEPROM Read Error\n");
577 memcpy(netdev->dev_addr, adapter->hw.mac_addr, netdev->addr_len);
578
579 if(!is_valid_ether_addr(netdev->dev_addr)) {
580 DPRINTK(PROBE, ERR, "Invalid MAC Address\n");
581 err = -EIO;
582 goto err_eeprom;
583 }
584
585 e1000_read_part_num(&adapter->hw, &(adapter->part_num));
586
587 e1000_get_bus_info(&adapter->hw);
588
589 init_timer(&adapter->tx_fifo_stall_timer);
590 adapter->tx_fifo_stall_timer.function = &e1000_82547_tx_fifo_stall;
591 adapter->tx_fifo_stall_timer.data = (unsigned long) adapter;
592
593 init_timer(&adapter->watchdog_timer);
594 adapter->watchdog_timer.function = &e1000_watchdog;
595 adapter->watchdog_timer.data = (unsigned long) adapter;
596
597 INIT_WORK(&adapter->watchdog_task,
598 (void (*)(void *))e1000_watchdog_task, adapter);
599
600 init_timer(&adapter->phy_info_timer);
601 adapter->phy_info_timer.function = &e1000_update_phy_info;
602 adapter->phy_info_timer.data = (unsigned long) adapter;
603
604 INIT_WORK(&adapter->tx_timeout_task,
605 (void (*)(void *))e1000_tx_timeout_task, netdev);
606
607 /* we're going to reset, so assume we have no link for now */
608
609 netif_carrier_off(netdev);
610 netif_stop_queue(netdev);
611
612 e1000_check_options(adapter);
613
614 /* Initial Wake on LAN setting
615 * If APM wake is enabled in the EEPROM,
616 * enable the ACPI Magic Packet filter
617 */
618
619 switch(adapter->hw.mac_type) {
620 case e1000_82542_rev2_0:
621 case e1000_82542_rev2_1:
622 case e1000_82543:
623 break;
624 case e1000_82544:
625 e1000_read_eeprom(&adapter->hw,
626 EEPROM_INIT_CONTROL2_REG, 1, &eeprom_data);
627 eeprom_apme_mask = E1000_EEPROM_82544_APM;
628 break;
629 case e1000_82546:
630 case e1000_82546_rev_3:
631 if((E1000_READ_REG(&adapter->hw, STATUS) & E1000_STATUS_FUNC_1)
632 && (adapter->hw.media_type == e1000_media_type_copper)) {
633 e1000_read_eeprom(&adapter->hw,
634 EEPROM_INIT_CONTROL3_PORT_B, 1, &eeprom_data);
635 break;
636 }
637 /* Fall Through */
638 default:
639 e1000_read_eeprom(&adapter->hw,
640 EEPROM_INIT_CONTROL3_PORT_A, 1, &eeprom_data);
641 break;
642 }
643 if(eeprom_data & eeprom_apme_mask)
644 adapter->wol |= E1000_WUFC_MAG;
645
646 /* reset the hardware with the new settings */
647 e1000_reset(adapter);
648
649 strcpy(netdev->name, "eth%d");
650 if((err = register_netdev(netdev)))
651 goto err_register;
652
653 DPRINTK(PROBE, INFO, "Intel(R) PRO/1000 Network Connection\n");
654
655 cards_found++;
656 return 0;
657
658err_register:
659err_sw_init:
660err_eeprom:
661 iounmap(adapter->hw.hw_addr);
662err_ioremap:
663 free_netdev(netdev);
664err_alloc_etherdev:
665 pci_release_regions(pdev);
666 return err;
667}
668
669/**
670 * e1000_remove - Device Removal Routine
671 * @pdev: PCI device information struct
672 *
673 * e1000_remove is called by the PCI subsystem to alert the driver
674 * that it should release a PCI device. The could be caused by a
675 * Hot-Plug event, or because the driver is going to be removed from
676 * memory.
677 **/
678
679static void __devexit
680e1000_remove(struct pci_dev *pdev)
681{
682 struct net_device *netdev = pci_get_drvdata(pdev);
683 struct e1000_adapter *adapter = netdev->priv;
684 uint32_t manc;
685
686 flush_scheduled_work();
687
688 if(adapter->hw.mac_type >= e1000_82540 &&
689 adapter->hw.media_type == e1000_media_type_copper) {
690 manc = E1000_READ_REG(&adapter->hw, MANC);
691 if(manc & E1000_MANC_SMBUS_EN) {
692 manc |= E1000_MANC_ARP_EN;
693 E1000_WRITE_REG(&adapter->hw, MANC, manc);
694 }
695 }
696
697 unregister_netdev(netdev);
698
699 e1000_phy_hw_reset(&adapter->hw);
700
701 iounmap(adapter->hw.hw_addr);
702 pci_release_regions(pdev);
703
704 free_netdev(netdev);
705
706 pci_disable_device(pdev);
707}
708
709/**
710 * e1000_sw_init - Initialize general software structures (struct e1000_adapter)
711 * @adapter: board private structure to initialize
712 *
713 * e1000_sw_init initializes the Adapter private data structure.
714 * Fields are initialized based on PCI device information and
715 * OS network device settings (MTU size).
716 **/
717
718static int __devinit
719e1000_sw_init(struct e1000_adapter *adapter)
720{
721 struct e1000_hw *hw = &adapter->hw;
722 struct net_device *netdev = adapter->netdev;
723 struct pci_dev *pdev = adapter->pdev;
724
725 /* PCI config space info */
726
727 hw->vendor_id = pdev->vendor;
728 hw->device_id = pdev->device;
729 hw->subsystem_vendor_id = pdev->subsystem_vendor;
730 hw->subsystem_id = pdev->subsystem_device;
731
732 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
733
734 pci_read_config_word(pdev, PCI_COMMAND, &hw->pci_cmd_word);
735
736 adapter->rx_buffer_len = E1000_RXBUFFER_2048;
737 hw->max_frame_size = netdev->mtu +
738 ENET_HEADER_SIZE + ETHERNET_FCS_SIZE;
739 hw->min_frame_size = MINIMUM_ETHERNET_FRAME_SIZE;
740
741 /* identify the MAC */
742
743 if(e1000_set_mac_type(hw)) {
744 DPRINTK(PROBE, ERR, "Unknown MAC Type\n");
745 return -EIO;
746 }
747
748 /* initialize eeprom parameters */
749
750 e1000_init_eeprom_params(hw);
751
752 switch(hw->mac_type) {
753 default:
754 break;
755 case e1000_82541:
756 case e1000_82547:
757 case e1000_82541_rev_2:
758 case e1000_82547_rev_2:
759 hw->phy_init_script = 1;
760 break;
761 }
762
763 e1000_set_media_type(hw);
764
765 hw->wait_autoneg_complete = FALSE;
766 hw->tbi_compatibility_en = TRUE;
767 hw->adaptive_ifs = TRUE;
768
769 /* Copper options */
770
771 if(hw->media_type == e1000_media_type_copper) {
772 hw->mdix = AUTO_ALL_MODES;
773 hw->disable_polarity_correction = FALSE;
774 hw->master_slave = E1000_MASTER_SLAVE;
775 }
776
777 atomic_set(&adapter->irq_sem, 1);
778 spin_lock_init(&adapter->stats_lock);
779 spin_lock_init(&adapter->tx_lock);
780
781 return 0;
782}
783
784/**
785 * e1000_open - Called when a network interface is made active
786 * @netdev: network interface device structure
787 *
788 * Returns 0 on success, negative value on failure
789 *
790 * The open entry point is called when a network interface is made
791 * active by the system (IFF_UP). At this point all resources needed
792 * for transmit and receive operations are allocated, the interrupt
793 * handler is registered with the OS, the watchdog timer is started,
794 * and the stack is notified that the interface is ready.
795 **/
796
797static int
798e1000_open(struct net_device *netdev)
799{
800 struct e1000_adapter *adapter = netdev->priv;
801 int err;
802
803 /* allocate transmit descriptors */
804
805 if((err = e1000_setup_tx_resources(adapter)))
806 goto err_setup_tx;
807
808 /* allocate receive descriptors */
809
810 if((err = e1000_setup_rx_resources(adapter)))
811 goto err_setup_rx;
812
813 if((err = e1000_up(adapter)))
814 goto err_up;
815
816 return E1000_SUCCESS;
817
818err_up:
819 e1000_free_rx_resources(adapter);
820err_setup_rx:
821 e1000_free_tx_resources(adapter);
822err_setup_tx:
823 e1000_reset(adapter);
824
825 return err;
826}
827
828/**
829 * e1000_close - Disables a network interface
830 * @netdev: network interface device structure
831 *
832 * Returns 0, this is not allowed to fail
833 *
834 * The close entry point is called when an interface is de-activated
835 * by the OS. The hardware is still under the drivers control, but
836 * needs to be disabled. A global MAC reset is issued to stop the
837 * hardware, and all transmit and receive resources are freed.
838 **/
839
840static int
841e1000_close(struct net_device *netdev)
842{
843 struct e1000_adapter *adapter = netdev->priv;
844
845 e1000_down(adapter);
846
847 e1000_free_tx_resources(adapter);
848 e1000_free_rx_resources(adapter);
849
850 return 0;
851}
852
853/**
854 * e1000_check_64k_bound - check that memory doesn't cross 64kB boundary
855 * @adapter: address of board private structure
856 * @begin: address of beginning of memory
857 * @end: address of end of memory
858 **/
859static inline boolean_t
860e1000_check_64k_bound(struct e1000_adapter *adapter,
861 void *start, unsigned long len)
862{
863 unsigned long begin = (unsigned long) start;
864 unsigned long end = begin + len;
865
866 /* first rev 82545 and 82546 need to not allow any memory
867 * write location to cross a 64k boundary due to errata 23 */
868 if (adapter->hw.mac_type == e1000_82545 ||
869 adapter->hw.mac_type == e1000_82546 ) {
870
871 /* check buffer doesn't cross 64kB */
872 return ((begin ^ (end - 1)) >> 16) != 0 ? FALSE : TRUE;
873 }
874
875 return TRUE;
876}
877
878/**
879 * e1000_setup_tx_resources - allocate Tx resources (Descriptors)
880 * @adapter: board private structure
881 *
882 * Return 0 on success, negative on failure
883 **/
884
885int
886e1000_setup_tx_resources(struct e1000_adapter *adapter)
887{
888 struct e1000_desc_ring *txdr = &adapter->tx_ring;
889 struct pci_dev *pdev = adapter->pdev;
890 int size;
891
892 size = sizeof(struct e1000_buffer) * txdr->count;
893 txdr->buffer_info = vmalloc(size);
894 if(!txdr->buffer_info) {
895 DPRINTK(PROBE, ERR,
896 "Unable to Allocate Memory for the Transmit descriptor ring\n");
897 return -ENOMEM;
898 }
899 memset(txdr->buffer_info, 0, size);
900
901 /* round up to nearest 4K */
902
903 txdr->size = txdr->count * sizeof(struct e1000_tx_desc);
904 E1000_ROUNDUP(txdr->size, 4096);
905
906 txdr->desc = pci_alloc_consistent(pdev, txdr->size, &txdr->dma);
907 if(!txdr->desc) {
908setup_tx_desc_die:
909 DPRINTK(PROBE, ERR,
910 "Unable to Allocate Memory for the Transmit descriptor ring\n");
911 vfree(txdr->buffer_info);
912 return -ENOMEM;
913 }
914
915 /* fix for errata 23, cant cross 64kB boundary */
916 if (!e1000_check_64k_bound(adapter, txdr->desc, txdr->size)) {
917 void *olddesc = txdr->desc;
918 dma_addr_t olddma = txdr->dma;
919 DPRINTK(TX_ERR,ERR,"txdr align check failed: %u bytes at %p\n",
920 txdr->size, txdr->desc);
921 /* try again, without freeing the previous */
922 txdr->desc = pci_alloc_consistent(pdev, txdr->size, &txdr->dma);
923 /* failed allocation, critial failure */
924 if(!txdr->desc) {
925 pci_free_consistent(pdev, txdr->size, olddesc, olddma);
926 goto setup_tx_desc_die;
927 }
928
929 if (!e1000_check_64k_bound(adapter, txdr->desc, txdr->size)) {
930 /* give up */
931 pci_free_consistent(pdev, txdr->size,
932 txdr->desc, txdr->dma);
933 pci_free_consistent(pdev, txdr->size, olddesc, olddma);
934 DPRINTK(PROBE, ERR,
935 "Unable to Allocate aligned Memory for the Transmit"
936 " descriptor ring\n");
937 vfree(txdr->buffer_info);
938 return -ENOMEM;
939 } else {
940 /* free old, move on with the new one since its okay */
941 pci_free_consistent(pdev, txdr->size, olddesc, olddma);
942 }
943 }
944 memset(txdr->desc, 0, txdr->size);
945
946 txdr->next_to_use = 0;
947 txdr->next_to_clean = 0;
948
949 return 0;
950}
951
952/**
953 * e1000_configure_tx - Configure 8254x Transmit Unit after Reset
954 * @adapter: board private structure
955 *
956 * Configure the Tx unit of the MAC after a reset.
957 **/
958
959static void
960e1000_configure_tx(struct e1000_adapter *adapter)
961{
962 uint64_t tdba = adapter->tx_ring.dma;
963 uint32_t tdlen = adapter->tx_ring.count * sizeof(struct e1000_tx_desc);
964 uint32_t tctl, tipg;
965
966 E1000_WRITE_REG(&adapter->hw, TDBAL, (tdba & 0x00000000ffffffffULL));
967 E1000_WRITE_REG(&adapter->hw, TDBAH, (tdba >> 32));
968
969 E1000_WRITE_REG(&adapter->hw, TDLEN, tdlen);
970
971 /* Setup the HW Tx Head and Tail descriptor pointers */
972
973 E1000_WRITE_REG(&adapter->hw, TDH, 0);
974 E1000_WRITE_REG(&adapter->hw, TDT, 0);
975
976 /* Set the default values for the Tx Inter Packet Gap timer */
977
978 switch (adapter->hw.mac_type) {
979 case e1000_82542_rev2_0:
980 case e1000_82542_rev2_1:
981 tipg = DEFAULT_82542_TIPG_IPGT;
982 tipg |= DEFAULT_82542_TIPG_IPGR1 << E1000_TIPG_IPGR1_SHIFT;
983 tipg |= DEFAULT_82542_TIPG_IPGR2 << E1000_TIPG_IPGR2_SHIFT;
984 break;
985 default:
986 if(adapter->hw.media_type == e1000_media_type_fiber ||
987 adapter->hw.media_type == e1000_media_type_internal_serdes)
988 tipg = DEFAULT_82543_TIPG_IPGT_FIBER;
989 else
990 tipg = DEFAULT_82543_TIPG_IPGT_COPPER;
991 tipg |= DEFAULT_82543_TIPG_IPGR1 << E1000_TIPG_IPGR1_SHIFT;
992 tipg |= DEFAULT_82543_TIPG_IPGR2 << E1000_TIPG_IPGR2_SHIFT;
993 }
994 E1000_WRITE_REG(&adapter->hw, TIPG, tipg);
995
996 /* Set the Tx Interrupt Delay register */
997
998 E1000_WRITE_REG(&adapter->hw, TIDV, adapter->tx_int_delay);
999 if(adapter->hw.mac_type >= e1000_82540)
1000 E1000_WRITE_REG(&adapter->hw, TADV, adapter->tx_abs_int_delay);
1001
1002 /* Program the Transmit Control Register */
1003
1004 tctl = E1000_READ_REG(&adapter->hw, TCTL);
1005
1006 tctl &= ~E1000_TCTL_CT;
1007 tctl |= E1000_TCTL_EN | E1000_TCTL_PSP |
1008 (E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT);
1009
1010 E1000_WRITE_REG(&adapter->hw, TCTL, tctl);
1011
1012 e1000_config_collision_dist(&adapter->hw);
1013
1014 /* Setup Transmit Descriptor Settings for eop descriptor */
1015 adapter->txd_cmd = E1000_TXD_CMD_IDE | E1000_TXD_CMD_EOP |
1016 E1000_TXD_CMD_IFCS;
1017
1018 if(adapter->hw.mac_type < e1000_82543)
1019 adapter->txd_cmd |= E1000_TXD_CMD_RPS;
1020 else
1021 adapter->txd_cmd |= E1000_TXD_CMD_RS;
1022
1023 /* Cache if we're 82544 running in PCI-X because we'll
1024 * need this to apply a workaround later in the send path. */
1025 if(adapter->hw.mac_type == e1000_82544 &&
1026 adapter->hw.bus_type == e1000_bus_type_pcix)
1027 adapter->pcix_82544 = 1;
1028}
1029
1030/**
1031 * e1000_setup_rx_resources - allocate Rx resources (Descriptors)
1032 * @adapter: board private structure
1033 *
1034 * Returns 0 on success, negative on failure
1035 **/
1036
1037int
1038e1000_setup_rx_resources(struct e1000_adapter *adapter)
1039{
1040 struct e1000_desc_ring *rxdr = &adapter->rx_ring;
1041 struct pci_dev *pdev = adapter->pdev;
1042 int size;
1043
1044 size = sizeof(struct e1000_buffer) * rxdr->count;
1045 rxdr->buffer_info = vmalloc(size);
1046 if(!rxdr->buffer_info) {
1047 DPRINTK(PROBE, ERR,
1048 "Unable to Allocate Memory for the Recieve descriptor ring\n");
1049 return -ENOMEM;
1050 }
1051 memset(rxdr->buffer_info, 0, size);
1052
1053 /* Round up to nearest 4K */
1054
1055 rxdr->size = rxdr->count * sizeof(struct e1000_rx_desc);
1056 E1000_ROUNDUP(rxdr->size, 4096);
1057
1058 rxdr->desc = pci_alloc_consistent(pdev, rxdr->size, &rxdr->dma);
1059
1060 if(!rxdr->desc) {
1061setup_rx_desc_die:
1062 DPRINTK(PROBE, ERR,
1063 "Unble to Allocate Memory for the Recieve descriptor ring\n");
1064 vfree(rxdr->buffer_info);
1065 return -ENOMEM;
1066 }
1067
1068 /* fix for errata 23, cant cross 64kB boundary */
1069 if (!e1000_check_64k_bound(adapter, rxdr->desc, rxdr->size)) {
1070 void *olddesc = rxdr->desc;
1071 dma_addr_t olddma = rxdr->dma;
1072 DPRINTK(RX_ERR,ERR,
1073 "rxdr align check failed: %u bytes at %p\n",
1074 rxdr->size, rxdr->desc);
1075 /* try again, without freeing the previous */
1076 rxdr->desc = pci_alloc_consistent(pdev, rxdr->size, &rxdr->dma);
1077 /* failed allocation, critial failure */
1078 if(!rxdr->desc) {
1079 pci_free_consistent(pdev, rxdr->size, olddesc, olddma);
1080 goto setup_rx_desc_die;
1081 }
1082
1083 if (!e1000_check_64k_bound(adapter, rxdr->desc, rxdr->size)) {
1084 /* give up */
1085 pci_free_consistent(pdev, rxdr->size,
1086 rxdr->desc, rxdr->dma);
1087 pci_free_consistent(pdev, rxdr->size, olddesc, olddma);
1088 DPRINTK(PROBE, ERR,
1089 "Unable to Allocate aligned Memory for the"
1090 " Receive descriptor ring\n");
1091 vfree(rxdr->buffer_info);
1092 return -ENOMEM;
1093 } else {
1094 /* free old, move on with the new one since its okay */
1095 pci_free_consistent(pdev, rxdr->size, olddesc, olddma);
1096 }
1097 }
1098 memset(rxdr->desc, 0, rxdr->size);
1099
1100 rxdr->next_to_clean = 0;
1101 rxdr->next_to_use = 0;
1102
1103 return 0;
1104}
1105
1106/**
1107 * e1000_setup_rctl - configure the receive control register
1108 * @adapter: Board private structure
1109 **/
1110
1111static void
1112e1000_setup_rctl(struct e1000_adapter *adapter)
1113{
1114 uint32_t rctl;
1115
1116 rctl = E1000_READ_REG(&adapter->hw, RCTL);
1117
1118 rctl &= ~(3 << E1000_RCTL_MO_SHIFT);
1119
1120 rctl |= E1000_RCTL_EN | E1000_RCTL_BAM |
1121 E1000_RCTL_LBM_NO | E1000_RCTL_RDMTS_HALF |
1122 (adapter->hw.mc_filter_type << E1000_RCTL_MO_SHIFT);
1123
1124 if(adapter->hw.tbi_compatibility_on == 1)
1125 rctl |= E1000_RCTL_SBP;
1126 else
1127 rctl &= ~E1000_RCTL_SBP;
1128
1129 /* Setup buffer sizes */
1130 rctl &= ~(E1000_RCTL_SZ_4096);
1131 rctl |= (E1000_RCTL_BSEX | E1000_RCTL_LPE);
1132 switch (adapter->rx_buffer_len) {
1133 case E1000_RXBUFFER_2048:
1134 default:
1135 rctl |= E1000_RCTL_SZ_2048;
1136 rctl &= ~(E1000_RCTL_BSEX | E1000_RCTL_LPE);
1137 break;
1138 case E1000_RXBUFFER_4096:
1139 rctl |= E1000_RCTL_SZ_4096;
1140 break;
1141 case E1000_RXBUFFER_8192:
1142 rctl |= E1000_RCTL_SZ_8192;
1143 break;
1144 case E1000_RXBUFFER_16384:
1145 rctl |= E1000_RCTL_SZ_16384;
1146 break;
1147 }
1148
1149 E1000_WRITE_REG(&adapter->hw, RCTL, rctl);
1150}
1151
1152/**
1153 * e1000_configure_rx - Configure 8254x Receive Unit after Reset
1154 * @adapter: board private structure
1155 *
1156 * Configure the Rx unit of the MAC after a reset.
1157 **/
1158
1159static void
1160e1000_configure_rx(struct e1000_adapter *adapter)
1161{
1162 uint64_t rdba = adapter->rx_ring.dma;
1163 uint32_t rdlen = adapter->rx_ring.count * sizeof(struct e1000_rx_desc);
1164 uint32_t rctl;
1165 uint32_t rxcsum;
1166
1167 /* disable receives while setting up the descriptors */
1168 rctl = E1000_READ_REG(&adapter->hw, RCTL);
1169 E1000_WRITE_REG(&adapter->hw, RCTL, rctl & ~E1000_RCTL_EN);
1170
1171 /* set the Receive Delay Timer Register */
1172 E1000_WRITE_REG(&adapter->hw, RDTR, adapter->rx_int_delay);
1173
1174 if(adapter->hw.mac_type >= e1000_82540) {
1175 E1000_WRITE_REG(&adapter->hw, RADV, adapter->rx_abs_int_delay);
1176 if(adapter->itr > 1)
1177 E1000_WRITE_REG(&adapter->hw, ITR,
1178 1000000000 / (adapter->itr * 256));
1179 }
1180
1181 /* Setup the Base and Length of the Rx Descriptor Ring */
1182 E1000_WRITE_REG(&adapter->hw, RDBAL, (rdba & 0x00000000ffffffffULL));
1183 E1000_WRITE_REG(&adapter->hw, RDBAH, (rdba >> 32));
1184
1185 E1000_WRITE_REG(&adapter->hw, RDLEN, rdlen);
1186
1187 /* Setup the HW Rx Head and Tail Descriptor Pointers */
1188 E1000_WRITE_REG(&adapter->hw, RDH, 0);
1189 E1000_WRITE_REG(&adapter->hw, RDT, 0);
1190
1191 /* Enable 82543 Receive Checksum Offload for TCP and UDP */
1192 if((adapter->hw.mac_type >= e1000_82543) &&
1193 (adapter->rx_csum == TRUE)) {
1194 rxcsum = E1000_READ_REG(&adapter->hw, RXCSUM);
1195 rxcsum |= E1000_RXCSUM_TUOFL;
1196 E1000_WRITE_REG(&adapter->hw, RXCSUM, rxcsum);
1197 }
1198
1199 /* Enable Receives */
1200 E1000_WRITE_REG(&adapter->hw, RCTL, rctl);
1201}
1202
1203/**
1204 * e1000_free_tx_resources - Free Tx Resources
1205 * @adapter: board private structure
1206 *
1207 * Free all transmit software resources
1208 **/
1209
1210void
1211e1000_free_tx_resources(struct e1000_adapter *adapter)
1212{
1213 struct pci_dev *pdev = adapter->pdev;
1214
1215 e1000_clean_tx_ring(adapter);
1216
1217 vfree(adapter->tx_ring.buffer_info);
1218 adapter->tx_ring.buffer_info = NULL;
1219
1220 pci_free_consistent(pdev, adapter->tx_ring.size,
1221 adapter->tx_ring.desc, adapter->tx_ring.dma);
1222
1223 adapter->tx_ring.desc = NULL;
1224}
1225
1226static inline void
1227e1000_unmap_and_free_tx_resource(struct e1000_adapter *adapter,
1228 struct e1000_buffer *buffer_info)
1229{
1230 struct pci_dev *pdev = adapter->pdev;
1231
1232 if(buffer_info->dma) {
1233 pci_unmap_page(pdev,
1234 buffer_info->dma,
1235 buffer_info->length,
1236 PCI_DMA_TODEVICE);
1237 buffer_info->dma = 0;
1238 }
1239 if(buffer_info->skb) {
1240 dev_kfree_skb_any(buffer_info->skb);
1241 buffer_info->skb = NULL;
1242 }
1243}
1244
1245/**
1246 * e1000_clean_tx_ring - Free Tx Buffers
1247 * @adapter: board private structure
1248 **/
1249
1250static void
1251e1000_clean_tx_ring(struct e1000_adapter *adapter)
1252{
1253 struct e1000_desc_ring *tx_ring = &adapter->tx_ring;
1254 struct e1000_buffer *buffer_info;
1255 unsigned long size;
1256 unsigned int i;
1257
1258 /* Free all the Tx ring sk_buffs */
1259
1260 if (likely(adapter->previous_buffer_info.skb != NULL)) {
1261 e1000_unmap_and_free_tx_resource(adapter,
1262 &adapter->previous_buffer_info);
1263 }
1264
1265 for(i = 0; i < tx_ring->count; i++) {
1266 buffer_info = &tx_ring->buffer_info[i];
1267 e1000_unmap_and_free_tx_resource(adapter, buffer_info);
1268 }
1269
1270 size = sizeof(struct e1000_buffer) * tx_ring->count;
1271 memset(tx_ring->buffer_info, 0, size);
1272
1273 /* Zero out the descriptor ring */
1274
1275 memset(tx_ring->desc, 0, tx_ring->size);
1276
1277 tx_ring->next_to_use = 0;
1278 tx_ring->next_to_clean = 0;
1279
1280 E1000_WRITE_REG(&adapter->hw, TDH, 0);
1281 E1000_WRITE_REG(&adapter->hw, TDT, 0);
1282}
1283
1284/**
1285 * e1000_free_rx_resources - Free Rx Resources
1286 * @adapter: board private structure
1287 *
1288 * Free all receive software resources
1289 **/
1290
1291void
1292e1000_free_rx_resources(struct e1000_adapter *adapter)
1293{
1294 struct e1000_desc_ring *rx_ring = &adapter->rx_ring;
1295 struct pci_dev *pdev = adapter->pdev;
1296
1297 e1000_clean_rx_ring(adapter);
1298
1299 vfree(rx_ring->buffer_info);
1300 rx_ring->buffer_info = NULL;
1301
1302 pci_free_consistent(pdev, rx_ring->size, rx_ring->desc, rx_ring->dma);
1303
1304 rx_ring->desc = NULL;
1305}
1306
1307/**
1308 * e1000_clean_rx_ring - Free Rx Buffers
1309 * @adapter: board private structure
1310 **/
1311
1312static void
1313e1000_clean_rx_ring(struct e1000_adapter *adapter)
1314{
1315 struct e1000_desc_ring *rx_ring = &adapter->rx_ring;
1316 struct e1000_buffer *buffer_info;
1317 struct pci_dev *pdev = adapter->pdev;
1318 unsigned long size;
1319 unsigned int i;
1320
1321 /* Free all the Rx ring sk_buffs */
1322
1323 for(i = 0; i < rx_ring->count; i++) {
1324 buffer_info = &rx_ring->buffer_info[i];
1325 if(buffer_info->skb) {
1326
1327 pci_unmap_single(pdev,
1328 buffer_info->dma,
1329 buffer_info->length,
1330 PCI_DMA_FROMDEVICE);
1331
1332 dev_kfree_skb(buffer_info->skb);
1333 buffer_info->skb = NULL;
1334 }
1335 }
1336
1337 size = sizeof(struct e1000_buffer) * rx_ring->count;
1338 memset(rx_ring->buffer_info, 0, size);
1339
1340 /* Zero out the descriptor ring */
1341
1342 memset(rx_ring->desc, 0, rx_ring->size);
1343
1344 rx_ring->next_to_clean = 0;
1345 rx_ring->next_to_use = 0;
1346
1347 E1000_WRITE_REG(&adapter->hw, RDH, 0);
1348 E1000_WRITE_REG(&adapter->hw, RDT, 0);
1349}
1350
1351/* The 82542 2.0 (revision 2) needs to have the receive unit in reset
1352 * and memory write and invalidate disabled for certain operations
1353 */
1354static void
1355e1000_enter_82542_rst(struct e1000_adapter *adapter)
1356{
1357 struct net_device *netdev = adapter->netdev;
1358 uint32_t rctl;
1359
1360 e1000_pci_clear_mwi(&adapter->hw);
1361
1362 rctl = E1000_READ_REG(&adapter->hw, RCTL);
1363 rctl |= E1000_RCTL_RST;
1364 E1000_WRITE_REG(&adapter->hw, RCTL, rctl);
1365 E1000_WRITE_FLUSH(&adapter->hw);
1366 mdelay(5);
1367
1368 if(netif_running(netdev))
1369 e1000_clean_rx_ring(adapter);
1370}
1371
1372static void
1373e1000_leave_82542_rst(struct e1000_adapter *adapter)
1374{
1375 struct net_device *netdev = adapter->netdev;
1376 uint32_t rctl;
1377
1378 rctl = E1000_READ_REG(&adapter->hw, RCTL);
1379 rctl &= ~E1000_RCTL_RST;
1380 E1000_WRITE_REG(&adapter->hw, RCTL, rctl);
1381 E1000_WRITE_FLUSH(&adapter->hw);
1382 mdelay(5);
1383
1384 if(adapter->hw.pci_cmd_word & PCI_COMMAND_INVALIDATE)
1385 e1000_pci_set_mwi(&adapter->hw);
1386
1387 if(netif_running(netdev)) {
1388 e1000_configure_rx(adapter);
1389 e1000_alloc_rx_buffers(adapter);
1390 }
1391}
1392
1393/**
1394 * e1000_set_mac - Change the Ethernet Address of the NIC
1395 * @netdev: network interface device structure
1396 * @p: pointer to an address structure
1397 *
1398 * Returns 0 on success, negative on failure
1399 **/
1400
1401static int
1402e1000_set_mac(struct net_device *netdev, void *p)
1403{
1404 struct e1000_adapter *adapter = netdev->priv;
1405 struct sockaddr *addr = p;
1406
1407 if(!is_valid_ether_addr(addr->sa_data))
1408 return -EADDRNOTAVAIL;
1409
1410 /* 82542 2.0 needs to be in reset to write receive address registers */
1411
1412 if(adapter->hw.mac_type == e1000_82542_rev2_0)
1413 e1000_enter_82542_rst(adapter);
1414
1415 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
1416 memcpy(adapter->hw.mac_addr, addr->sa_data, netdev->addr_len);
1417
1418 e1000_rar_set(&adapter->hw, adapter->hw.mac_addr, 0);
1419
1420 if(adapter->hw.mac_type == e1000_82542_rev2_0)
1421 e1000_leave_82542_rst(adapter);
1422
1423 return 0;
1424}
1425
1426/**
1427 * e1000_set_multi - Multicast and Promiscuous mode set
1428 * @netdev: network interface device structure
1429 *
1430 * The set_multi entry point is called whenever the multicast address
1431 * list or the network interface flags are updated. This routine is
1432 * responsible for configuring the hardware for proper multicast,
1433 * promiscuous mode, and all-multi behavior.
1434 **/
1435
1436static void
1437e1000_set_multi(struct net_device *netdev)
1438{
1439 struct e1000_adapter *adapter = netdev->priv;
1440 struct e1000_hw *hw = &adapter->hw;
1441 struct dev_mc_list *mc_ptr;
1442 uint32_t rctl;
1443 uint32_t hash_value;
1444 int i;
1445 unsigned long flags;
1446
1447 /* Check for Promiscuous and All Multicast modes */
1448
1449 spin_lock_irqsave(&adapter->tx_lock, flags);
1450
1451 rctl = E1000_READ_REG(hw, RCTL);
1452
1453 if(netdev->flags & IFF_PROMISC) {
1454 rctl |= (E1000_RCTL_UPE | E1000_RCTL_MPE);
1455 } else if(netdev->flags & IFF_ALLMULTI) {
1456 rctl |= E1000_RCTL_MPE;
1457 rctl &= ~E1000_RCTL_UPE;
1458 } else {
1459 rctl &= ~(E1000_RCTL_UPE | E1000_RCTL_MPE);
1460 }
1461
1462 E1000_WRITE_REG(hw, RCTL, rctl);
1463
1464 /* 82542 2.0 needs to be in reset to write receive address registers */
1465
1466 if(hw->mac_type == e1000_82542_rev2_0)
1467 e1000_enter_82542_rst(adapter);
1468
1469 /* load the first 14 multicast address into the exact filters 1-14
1470 * RAR 0 is used for the station MAC adddress
1471 * if there are not 14 addresses, go ahead and clear the filters
1472 */
1473 mc_ptr = netdev->mc_list;
1474
1475 for(i = 1; i < E1000_RAR_ENTRIES; i++) {
1476 if(mc_ptr) {
1477 e1000_rar_set(hw, mc_ptr->dmi_addr, i);
1478 mc_ptr = mc_ptr->next;
1479 } else {
1480 E1000_WRITE_REG_ARRAY(hw, RA, i << 1, 0);
1481 E1000_WRITE_REG_ARRAY(hw, RA, (i << 1) + 1, 0);
1482 }
1483 }
1484
1485 /* clear the old settings from the multicast hash table */
1486
1487 for(i = 0; i < E1000_NUM_MTA_REGISTERS; i++)
1488 E1000_WRITE_REG_ARRAY(hw, MTA, i, 0);
1489
1490 /* load any remaining addresses into the hash table */
1491
1492 for(; mc_ptr; mc_ptr = mc_ptr->next) {
1493 hash_value = e1000_hash_mc_addr(hw, mc_ptr->dmi_addr);
1494 e1000_mta_set(hw, hash_value);
1495 }
1496
1497 if(hw->mac_type == e1000_82542_rev2_0)
1498 e1000_leave_82542_rst(adapter);
1499
1500 spin_unlock_irqrestore(&adapter->tx_lock, flags);
1501}
1502
1503/* Need to wait a few seconds after link up to get diagnostic information from
1504 * the phy */
1505
1506static void
1507e1000_update_phy_info(unsigned long data)
1508{
1509 struct e1000_adapter *adapter = (struct e1000_adapter *) data;
1510 e1000_phy_get_info(&adapter->hw, &adapter->phy_info);
1511}
1512
1513/**
1514 * e1000_82547_tx_fifo_stall - Timer Call-back
1515 * @data: pointer to adapter cast into an unsigned long
1516 **/
1517
1518static void
1519e1000_82547_tx_fifo_stall(unsigned long data)
1520{
1521 struct e1000_adapter *adapter = (struct e1000_adapter *) data;
1522 struct net_device *netdev = adapter->netdev;
1523 uint32_t tctl;
1524
1525 if(atomic_read(&adapter->tx_fifo_stall)) {
1526 if((E1000_READ_REG(&adapter->hw, TDT) ==
1527 E1000_READ_REG(&adapter->hw, TDH)) &&
1528 (E1000_READ_REG(&adapter->hw, TDFT) ==
1529 E1000_READ_REG(&adapter->hw, TDFH)) &&
1530 (E1000_READ_REG(&adapter->hw, TDFTS) ==
1531 E1000_READ_REG(&adapter->hw, TDFHS))) {
1532 tctl = E1000_READ_REG(&adapter->hw, TCTL);
1533 E1000_WRITE_REG(&adapter->hw, TCTL,
1534 tctl & ~E1000_TCTL_EN);
1535 E1000_WRITE_REG(&adapter->hw, TDFT,
1536 adapter->tx_head_addr);
1537 E1000_WRITE_REG(&adapter->hw, TDFH,
1538 adapter->tx_head_addr);
1539 E1000_WRITE_REG(&adapter->hw, TDFTS,
1540 adapter->tx_head_addr);
1541 E1000_WRITE_REG(&adapter->hw, TDFHS,
1542 adapter->tx_head_addr);
1543 E1000_WRITE_REG(&adapter->hw, TCTL, tctl);
1544 E1000_WRITE_FLUSH(&adapter->hw);
1545
1546 adapter->tx_fifo_head = 0;
1547 atomic_set(&adapter->tx_fifo_stall, 0);
1548 netif_wake_queue(netdev);
1549 } else {
1550 mod_timer(&adapter->tx_fifo_stall_timer, jiffies + 1);
1551 }
1552 }
1553}
1554
1555/**
1556 * e1000_watchdog - Timer Call-back
1557 * @data: pointer to adapter cast into an unsigned long
1558 **/
1559static void
1560e1000_watchdog(unsigned long data)
1561{
1562 struct e1000_adapter *adapter = (struct e1000_adapter *) data;
1563
1564 /* Do the rest outside of interrupt context */
1565 schedule_work(&adapter->watchdog_task);
1566}
1567
1568static void
1569e1000_watchdog_task(struct e1000_adapter *adapter)
1570{
1571 struct net_device *netdev = adapter->netdev;
1572 struct e1000_desc_ring *txdr = &adapter->tx_ring;
1573 uint32_t link;
1574
1575 e1000_check_for_link(&adapter->hw);
1576
1577 if((adapter->hw.media_type == e1000_media_type_internal_serdes) &&
1578 !(E1000_READ_REG(&adapter->hw, TXCW) & E1000_TXCW_ANE))
1579 link = !adapter->hw.serdes_link_down;
1580 else
1581 link = E1000_READ_REG(&adapter->hw, STATUS) & E1000_STATUS_LU;
1582
1583 if(link) {
1584 if(!netif_carrier_ok(netdev)) {
1585 e1000_get_speed_and_duplex(&adapter->hw,
1586 &adapter->link_speed,
1587 &adapter->link_duplex);
1588
1589 DPRINTK(LINK, INFO, "NIC Link is Up %d Mbps %s\n",
1590 adapter->link_speed,
1591 adapter->link_duplex == FULL_DUPLEX ?
1592 "Full Duplex" : "Half Duplex");
1593
1594 netif_carrier_on(netdev);
1595 netif_wake_queue(netdev);
1596 mod_timer(&adapter->phy_info_timer, jiffies + 2 * HZ);
1597 adapter->smartspeed = 0;
1598 }
1599 } else {
1600 if(netif_carrier_ok(netdev)) {
1601 adapter->link_speed = 0;
1602 adapter->link_duplex = 0;
1603 DPRINTK(LINK, INFO, "NIC Link is Down\n");
1604 netif_carrier_off(netdev);
1605 netif_stop_queue(netdev);
1606 mod_timer(&adapter->phy_info_timer, jiffies + 2 * HZ);
1607 }
1608
1609 e1000_smartspeed(adapter);
1610 }
1611
1612 e1000_update_stats(adapter);
1613
1614 adapter->hw.tx_packet_delta = adapter->stats.tpt - adapter->tpt_old;
1615 adapter->tpt_old = adapter->stats.tpt;
1616 adapter->hw.collision_delta = adapter->stats.colc - adapter->colc_old;
1617 adapter->colc_old = adapter->stats.colc;
1618
1619 adapter->gorcl = adapter->stats.gorcl - adapter->gorcl_old;
1620 adapter->gorcl_old = adapter->stats.gorcl;
1621 adapter->gotcl = adapter->stats.gotcl - adapter->gotcl_old;
1622 adapter->gotcl_old = adapter->stats.gotcl;
1623
1624 e1000_update_adaptive(&adapter->hw);
1625
1626 if(!netif_carrier_ok(netdev)) {
1627 if(E1000_DESC_UNUSED(txdr) + 1 < txdr->count) {
1628 /* We've lost link, so the controller stops DMA,
1629 * but we've got queued Tx work that's never going
1630 * to get done, so reset controller to flush Tx.
1631 * (Do the reset outside of interrupt context). */
1632 schedule_work(&adapter->tx_timeout_task);
1633 }
1634 }
1635
1636 /* Dynamic mode for Interrupt Throttle Rate (ITR) */
1637 if(adapter->hw.mac_type >= e1000_82540 && adapter->itr == 1) {
1638 /* Symmetric Tx/Rx gets a reduced ITR=2000; Total
1639 * asymmetrical Tx or Rx gets ITR=8000; everyone
1640 * else is between 2000-8000. */
1641 uint32_t goc = (adapter->gotcl + adapter->gorcl) / 10000;
1642 uint32_t dif = (adapter->gotcl > adapter->gorcl ?
1643 adapter->gotcl - adapter->gorcl :
1644 adapter->gorcl - adapter->gotcl) / 10000;
1645 uint32_t itr = goc > 0 ? (dif * 6000 / goc + 2000) : 8000;
1646 E1000_WRITE_REG(&adapter->hw, ITR, 1000000000 / (itr * 256));
1647 }
1648
1649 /* Cause software interrupt to ensure rx ring is cleaned */
1650 E1000_WRITE_REG(&adapter->hw, ICS, E1000_ICS_RXDMT0);
1651
1652 /* Force detection of hung controller every watchdog period*/
1653 adapter->detect_tx_hung = TRUE;
1654
1655 /* Reset the timer */
1656 mod_timer(&adapter->watchdog_timer, jiffies + 2 * HZ);
1657}
1658
1659#define E1000_TX_FLAGS_CSUM 0x00000001
1660#define E1000_TX_FLAGS_VLAN 0x00000002
1661#define E1000_TX_FLAGS_TSO 0x00000004
1662#define E1000_TX_FLAGS_VLAN_MASK 0xffff0000
1663#define E1000_TX_FLAGS_VLAN_SHIFT 16
1664
1665static inline int
1666e1000_tso(struct e1000_adapter *adapter, struct sk_buff *skb)
1667{
1668#ifdef NETIF_F_TSO
1669 struct e1000_context_desc *context_desc;
1670 unsigned int i;
1671 uint32_t cmd_length = 0;
1672 uint16_t ipcse, tucse, mss;
1673 uint8_t ipcss, ipcso, tucss, tucso, hdr_len;
1674 int err;
1675
1676 if(skb_shinfo(skb)->tso_size) {
1677 if (skb_header_cloned(skb)) {
1678 err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
1679 if (err)
1680 return err;
1681 }
1682
1683 hdr_len = ((skb->h.raw - skb->data) + (skb->h.th->doff << 2));
1684 mss = skb_shinfo(skb)->tso_size;
1685 skb->nh.iph->tot_len = 0;
1686 skb->nh.iph->check = 0;
1687 skb->h.th->check = ~csum_tcpudp_magic(skb->nh.iph->saddr,
1688 skb->nh.iph->daddr,
1689 0,
1690 IPPROTO_TCP,
1691 0);
1692 ipcss = skb->nh.raw - skb->data;
1693 ipcso = (void *)&(skb->nh.iph->check) - (void *)skb->data;
1694 ipcse = skb->h.raw - skb->data - 1;
1695 tucss = skb->h.raw - skb->data;
1696 tucso = (void *)&(skb->h.th->check) - (void *)skb->data;
1697 tucse = 0;
1698
1699 cmd_length |= (E1000_TXD_CMD_DEXT | E1000_TXD_CMD_TSE |
1700 E1000_TXD_CMD_IP | E1000_TXD_CMD_TCP |
1701 (skb->len - (hdr_len)));
1702
1703 i = adapter->tx_ring.next_to_use;
1704 context_desc = E1000_CONTEXT_DESC(adapter->tx_ring, i);
1705
1706 context_desc->lower_setup.ip_fields.ipcss = ipcss;
1707 context_desc->lower_setup.ip_fields.ipcso = ipcso;
1708 context_desc->lower_setup.ip_fields.ipcse = cpu_to_le16(ipcse);
1709 context_desc->upper_setup.tcp_fields.tucss = tucss;
1710 context_desc->upper_setup.tcp_fields.tucso = tucso;
1711 context_desc->upper_setup.tcp_fields.tucse = cpu_to_le16(tucse);
1712 context_desc->tcp_seg_setup.fields.mss = cpu_to_le16(mss);
1713 context_desc->tcp_seg_setup.fields.hdr_len = hdr_len;
1714 context_desc->cmd_and_length = cpu_to_le32(cmd_length);
1715
1716 if(++i == adapter->tx_ring.count) i = 0;
1717 adapter->tx_ring.next_to_use = i;
1718
1719 return 1;
1720 }
1721#endif
1722
1723 return 0;
1724}
1725
1726static inline boolean_t
1727e1000_tx_csum(struct e1000_adapter *adapter, struct sk_buff *skb)
1728{
1729 struct e1000_context_desc *context_desc;
1730 unsigned int i;
1731 uint8_t css;
1732
1733 if(likely(skb->ip_summed == CHECKSUM_HW)) {
1734 css = skb->h.raw - skb->data;
1735
1736 i = adapter->tx_ring.next_to_use;
1737 context_desc = E1000_CONTEXT_DESC(adapter->tx_ring, i);
1738
1739 context_desc->upper_setup.tcp_fields.tucss = css;
1740 context_desc->upper_setup.tcp_fields.tucso = css + skb->csum;
1741 context_desc->upper_setup.tcp_fields.tucse = 0;
1742 context_desc->tcp_seg_setup.data = 0;
1743 context_desc->cmd_and_length = cpu_to_le32(E1000_TXD_CMD_DEXT);
1744
1745 if(unlikely(++i == adapter->tx_ring.count)) i = 0;
1746 adapter->tx_ring.next_to_use = i;
1747
1748 return TRUE;
1749 }
1750
1751 return FALSE;
1752}
1753
1754#define E1000_MAX_TXD_PWR 12
1755#define E1000_MAX_DATA_PER_TXD (1<<E1000_MAX_TXD_PWR)
1756
1757static inline int
1758e1000_tx_map(struct e1000_adapter *adapter, struct sk_buff *skb,
1759 unsigned int first, unsigned int max_per_txd,
1760 unsigned int nr_frags, unsigned int mss)
1761{
1762 struct e1000_desc_ring *tx_ring = &adapter->tx_ring;
1763 struct e1000_buffer *buffer_info;
1764 unsigned int len = skb->len;
1765 unsigned int offset = 0, size, count = 0, i;
1766 unsigned int f;
1767 len -= skb->data_len;
1768
1769 i = tx_ring->next_to_use;
1770
1771 while(len) {
1772 buffer_info = &tx_ring->buffer_info[i];
1773 size = min(len, max_per_txd);
1774#ifdef NETIF_F_TSO
1775 /* Workaround for premature desc write-backs
1776 * in TSO mode. Append 4-byte sentinel desc */
1777 if(unlikely(mss && !nr_frags && size == len && size > 8))
1778 size -= 4;
1779#endif
1780 /* Workaround for potential 82544 hang in PCI-X. Avoid
1781 * terminating buffers within evenly-aligned dwords. */
1782 if(unlikely(adapter->pcix_82544 &&
1783 !((unsigned long)(skb->data + offset + size - 1) & 4) &&
1784 size > 4))
1785 size -= 4;
1786
1787 buffer_info->length = size;
1788 buffer_info->dma =
1789 pci_map_single(adapter->pdev,
1790 skb->data + offset,
1791 size,
1792 PCI_DMA_TODEVICE);
1793 buffer_info->time_stamp = jiffies;
1794
1795 len -= size;
1796 offset += size;
1797 count++;
1798 if(unlikely(++i == tx_ring->count)) i = 0;
1799 }
1800
1801 for(f = 0; f < nr_frags; f++) {
1802 struct skb_frag_struct *frag;
1803
1804 frag = &skb_shinfo(skb)->frags[f];
1805 len = frag->size;
1806 offset = frag->page_offset;
1807
1808 while(len) {
1809 buffer_info = &tx_ring->buffer_info[i];
1810 size = min(len, max_per_txd);
1811#ifdef NETIF_F_TSO
1812 /* Workaround for premature desc write-backs
1813 * in TSO mode. Append 4-byte sentinel desc */
1814 if(unlikely(mss && f == (nr_frags-1) && size == len && size > 8))
1815 size -= 4;
1816#endif
1817 /* Workaround for potential 82544 hang in PCI-X.
1818 * Avoid terminating buffers within evenly-aligned
1819 * dwords. */
1820 if(unlikely(adapter->pcix_82544 &&
1821 !((unsigned long)(frag->page+offset+size-1) & 4) &&
1822 size > 4))
1823 size -= 4;
1824
1825 buffer_info->length = size;
1826 buffer_info->dma =
1827 pci_map_page(adapter->pdev,
1828 frag->page,
1829 offset,
1830 size,
1831 PCI_DMA_TODEVICE);
1832 buffer_info->time_stamp = jiffies;
1833
1834 len -= size;
1835 offset += size;
1836 count++;
1837 if(unlikely(++i == tx_ring->count)) i = 0;
1838 }
1839 }
1840
1841 i = (i == 0) ? tx_ring->count - 1 : i - 1;
1842 tx_ring->buffer_info[i].skb = skb;
1843 tx_ring->buffer_info[first].next_to_watch = i;
1844
1845 return count;
1846}
1847
1848static inline void
1849e1000_tx_queue(struct e1000_adapter *adapter, int count, int tx_flags)
1850{
1851 struct e1000_desc_ring *tx_ring = &adapter->tx_ring;
1852 struct e1000_tx_desc *tx_desc = NULL;
1853 struct e1000_buffer *buffer_info;
1854 uint32_t txd_upper = 0, txd_lower = E1000_TXD_CMD_IFCS;
1855 unsigned int i;
1856
1857 if(likely(tx_flags & E1000_TX_FLAGS_TSO)) {
1858 txd_lower |= E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D |
1859 E1000_TXD_CMD_TSE;
1860 txd_upper |= (E1000_TXD_POPTS_IXSM | E1000_TXD_POPTS_TXSM) << 8;
1861 }
1862
1863 if(likely(tx_flags & E1000_TX_FLAGS_CSUM)) {
1864 txd_lower |= E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D;
1865 txd_upper |= E1000_TXD_POPTS_TXSM << 8;
1866 }
1867
1868 if(unlikely(tx_flags & E1000_TX_FLAGS_VLAN)) {
1869 txd_lower |= E1000_TXD_CMD_VLE;
1870 txd_upper |= (tx_flags & E1000_TX_FLAGS_VLAN_MASK);
1871 }
1872
1873 i = tx_ring->next_to_use;
1874
1875 while(count--) {
1876 buffer_info = &tx_ring->buffer_info[i];
1877 tx_desc = E1000_TX_DESC(*tx_ring, i);
1878 tx_desc->buffer_addr = cpu_to_le64(buffer_info->dma);
1879 tx_desc->lower.data =
1880 cpu_to_le32(txd_lower | buffer_info->length);
1881 tx_desc->upper.data = cpu_to_le32(txd_upper);
1882 if(unlikely(++i == tx_ring->count)) i = 0;
1883 }
1884
1885 tx_desc->lower.data |= cpu_to_le32(adapter->txd_cmd);
1886
1887 /* Force memory writes to complete before letting h/w
1888 * know there are new descriptors to fetch. (Only
1889 * applicable for weak-ordered memory model archs,
1890 * such as IA-64). */
1891 wmb();
1892
1893 tx_ring->next_to_use = i;
1894 E1000_WRITE_REG(&adapter->hw, TDT, i);
1895}
1896
1897/**
1898 * 82547 workaround to avoid controller hang in half-duplex environment.
1899 * The workaround is to avoid queuing a large packet that would span
1900 * the internal Tx FIFO ring boundary by notifying the stack to resend
1901 * the packet at a later time. This gives the Tx FIFO an opportunity to
1902 * flush all packets. When that occurs, we reset the Tx FIFO pointers
1903 * to the beginning of the Tx FIFO.
1904 **/
1905
1906#define E1000_FIFO_HDR 0x10
1907#define E1000_82547_PAD_LEN 0x3E0
1908
1909static inline int
1910e1000_82547_fifo_workaround(struct e1000_adapter *adapter, struct sk_buff *skb)
1911{
1912 uint32_t fifo_space = adapter->tx_fifo_size - adapter->tx_fifo_head;
1913 uint32_t skb_fifo_len = skb->len + E1000_FIFO_HDR;
1914
1915 E1000_ROUNDUP(skb_fifo_len, E1000_FIFO_HDR);
1916
1917 if(adapter->link_duplex != HALF_DUPLEX)
1918 goto no_fifo_stall_required;
1919
1920 if(atomic_read(&adapter->tx_fifo_stall))
1921 return 1;
1922
1923 if(skb_fifo_len >= (E1000_82547_PAD_LEN + fifo_space)) {
1924 atomic_set(&adapter->tx_fifo_stall, 1);
1925 return 1;
1926 }
1927
1928no_fifo_stall_required:
1929 adapter->tx_fifo_head += skb_fifo_len;
1930 if(adapter->tx_fifo_head >= adapter->tx_fifo_size)
1931 adapter->tx_fifo_head -= adapter->tx_fifo_size;
1932 return 0;
1933}
1934
1935#define TXD_USE_COUNT(S, X) (((S) >> (X)) + 1 )
1936static int
1937e1000_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
1938{
1939 struct e1000_adapter *adapter = netdev->priv;
1940 unsigned int first, max_per_txd = E1000_MAX_DATA_PER_TXD;
1941 unsigned int max_txd_pwr = E1000_MAX_TXD_PWR;
1942 unsigned int tx_flags = 0;
1943 unsigned int len = skb->len;
1944 unsigned long flags;
1945 unsigned int nr_frags = 0;
1946 unsigned int mss = 0;
1947 int count = 0;
1948 int tso;
1949 unsigned int f;
1950 len -= skb->data_len;
1951
1952 if(unlikely(skb->len <= 0)) {
1953 dev_kfree_skb_any(skb);
1954 return NETDEV_TX_OK;
1955 }
1956
1957#ifdef NETIF_F_TSO
1958 mss = skb_shinfo(skb)->tso_size;
1959 /* The controller does a simple calculation to
1960 * make sure there is enough room in the FIFO before
1961 * initiating the DMA for each buffer. The calc is:
1962 * 4 = ceil(buffer len/mss). To make sure we don't
1963 * overrun the FIFO, adjust the max buffer len if mss
1964 * drops. */
1965 if(mss) {
1966 max_per_txd = min(mss << 2, max_per_txd);
1967 max_txd_pwr = fls(max_per_txd) - 1;
1968 }
1969
1970 if((mss) || (skb->ip_summed == CHECKSUM_HW))
1971 count++;
1972 count++; /* for sentinel desc */
1973#else
1974 if(skb->ip_summed == CHECKSUM_HW)
1975 count++;
1976#endif
1977 count += TXD_USE_COUNT(len, max_txd_pwr);
1978
1979 if(adapter->pcix_82544)
1980 count++;
1981
1982 nr_frags = skb_shinfo(skb)->nr_frags;
1983 for(f = 0; f < nr_frags; f++)
1984 count += TXD_USE_COUNT(skb_shinfo(skb)->frags[f].size,
1985 max_txd_pwr);
1986 if(adapter->pcix_82544)
1987 count += nr_frags;
1988
1989 local_irq_save(flags);
1990 if (!spin_trylock(&adapter->tx_lock)) {
1991 /* Collision - tell upper layer to requeue */
1992 local_irq_restore(flags);
1993 return NETDEV_TX_LOCKED;
1994 }
1995
1996 /* need: count + 2 desc gap to keep tail from touching
1997 * head, otherwise try next time */
1998 if(unlikely(E1000_DESC_UNUSED(&adapter->tx_ring) < count + 2)) {
1999 netif_stop_queue(netdev);
2000 spin_unlock_irqrestore(&adapter->tx_lock, flags);
2001 return NETDEV_TX_BUSY;
2002 }
2003
2004 if(unlikely(adapter->hw.mac_type == e1000_82547)) {
2005 if(unlikely(e1000_82547_fifo_workaround(adapter, skb))) {
2006 netif_stop_queue(netdev);
2007 mod_timer(&adapter->tx_fifo_stall_timer, jiffies);
2008 spin_unlock_irqrestore(&adapter->tx_lock, flags);
2009 return NETDEV_TX_BUSY;
2010 }
2011 }
2012
2013 if(unlikely(adapter->vlgrp && vlan_tx_tag_present(skb))) {
2014 tx_flags |= E1000_TX_FLAGS_VLAN;
2015 tx_flags |= (vlan_tx_tag_get(skb) << E1000_TX_FLAGS_VLAN_SHIFT);
2016 }
2017
2018 first = adapter->tx_ring.next_to_use;
2019
2020 tso = e1000_tso(adapter, skb);
2021 if (tso < 0) {
2022 dev_kfree_skb_any(skb);
2023 return NETDEV_TX_OK;
2024 }
2025
2026 if (likely(tso))
2027 tx_flags |= E1000_TX_FLAGS_TSO;
2028 else if(likely(e1000_tx_csum(adapter, skb)))
2029 tx_flags |= E1000_TX_FLAGS_CSUM;
2030
2031 e1000_tx_queue(adapter,
2032 e1000_tx_map(adapter, skb, first, max_per_txd, nr_frags, mss),
2033 tx_flags);
2034
2035 netdev->trans_start = jiffies;
2036
2037 /* Make sure there is space in the ring for the next send. */
2038 if(unlikely(E1000_DESC_UNUSED(&adapter->tx_ring) < MAX_SKB_FRAGS + 2))
2039 netif_stop_queue(netdev);
2040
2041 spin_unlock_irqrestore(&adapter->tx_lock, flags);
2042 return NETDEV_TX_OK;
2043}
2044
2045/**
2046 * e1000_tx_timeout - Respond to a Tx Hang
2047 * @netdev: network interface device structure
2048 **/
2049
2050static void
2051e1000_tx_timeout(struct net_device *netdev)
2052{
2053 struct e1000_adapter *adapter = netdev->priv;
2054
2055 /* Do the reset outside of interrupt context */
2056 schedule_work(&adapter->tx_timeout_task);
2057}
2058
2059static void
2060e1000_tx_timeout_task(struct net_device *netdev)
2061{
2062 struct e1000_adapter *adapter = netdev->priv;
2063
2064 e1000_down(adapter);
2065 e1000_up(adapter);
2066}
2067
2068/**
2069 * e1000_get_stats - Get System Network Statistics
2070 * @netdev: network interface device structure
2071 *
2072 * Returns the address of the device statistics structure.
2073 * The statistics are actually updated from the timer callback.
2074 **/
2075
2076static struct net_device_stats *
2077e1000_get_stats(struct net_device *netdev)
2078{
2079 struct e1000_adapter *adapter = netdev->priv;
2080
2081 e1000_update_stats(adapter);
2082 return &adapter->net_stats;
2083}
2084
2085/**
2086 * e1000_change_mtu - Change the Maximum Transfer Unit
2087 * @netdev: network interface device structure
2088 * @new_mtu: new value for maximum frame size
2089 *
2090 * Returns 0 on success, negative on failure
2091 **/
2092
2093static int
2094e1000_change_mtu(struct net_device *netdev, int new_mtu)
2095{
2096 struct e1000_adapter *adapter = netdev->priv;
2097 int old_mtu = adapter->rx_buffer_len;
2098 int max_frame = new_mtu + ENET_HEADER_SIZE + ETHERNET_FCS_SIZE;
2099
2100 if((max_frame < MINIMUM_ETHERNET_FRAME_SIZE) ||
2101 (max_frame > MAX_JUMBO_FRAME_SIZE)) {
2102 DPRINTK(PROBE, ERR, "Invalid MTU setting\n");
2103 return -EINVAL;
2104 }
2105
2106 if(max_frame <= MAXIMUM_ETHERNET_FRAME_SIZE) {
2107 adapter->rx_buffer_len = E1000_RXBUFFER_2048;
2108
2109 } else if(adapter->hw.mac_type < e1000_82543) {
2110 DPRINTK(PROBE, ERR, "Jumbo Frames not supported on 82542\n");
2111 return -EINVAL;
2112
2113 } else if(max_frame <= E1000_RXBUFFER_4096) {
2114 adapter->rx_buffer_len = E1000_RXBUFFER_4096;
2115
2116 } else if(max_frame <= E1000_RXBUFFER_8192) {
2117 adapter->rx_buffer_len = E1000_RXBUFFER_8192;
2118
2119 } else {
2120 adapter->rx_buffer_len = E1000_RXBUFFER_16384;
2121 }
2122
2123 if(old_mtu != adapter->rx_buffer_len && netif_running(netdev)) {
2124 e1000_down(adapter);
2125 e1000_up(adapter);
2126 }
2127
2128 netdev->mtu = new_mtu;
2129 adapter->hw.max_frame_size = max_frame;
2130
2131 return 0;
2132}
2133
2134/**
2135 * e1000_update_stats - Update the board statistics counters
2136 * @adapter: board private structure
2137 **/
2138
2139void
2140e1000_update_stats(struct e1000_adapter *adapter)
2141{
2142 struct e1000_hw *hw = &adapter->hw;
2143 unsigned long flags;
2144 uint16_t phy_tmp;
2145
2146#define PHY_IDLE_ERROR_COUNT_MASK 0x00FF
2147
2148 spin_lock_irqsave(&adapter->stats_lock, flags);
2149
2150 /* these counters are modified from e1000_adjust_tbi_stats,
2151 * called from the interrupt context, so they must only
2152 * be written while holding adapter->stats_lock
2153 */
2154
2155 adapter->stats.crcerrs += E1000_READ_REG(hw, CRCERRS);
2156 adapter->stats.gprc += E1000_READ_REG(hw, GPRC);
2157 adapter->stats.gorcl += E1000_READ_REG(hw, GORCL);
2158 adapter->stats.gorch += E1000_READ_REG(hw, GORCH);
2159 adapter->stats.bprc += E1000_READ_REG(hw, BPRC);
2160 adapter->stats.mprc += E1000_READ_REG(hw, MPRC);
2161 adapter->stats.roc += E1000_READ_REG(hw, ROC);
2162 adapter->stats.prc64 += E1000_READ_REG(hw, PRC64);
2163 adapter->stats.prc127 += E1000_READ_REG(hw, PRC127);
2164 adapter->stats.prc255 += E1000_READ_REG(hw, PRC255);
2165 adapter->stats.prc511 += E1000_READ_REG(hw, PRC511);
2166 adapter->stats.prc1023 += E1000_READ_REG(hw, PRC1023);
2167 adapter->stats.prc1522 += E1000_READ_REG(hw, PRC1522);
2168
2169 adapter->stats.symerrs += E1000_READ_REG(hw, SYMERRS);
2170 adapter->stats.mpc += E1000_READ_REG(hw, MPC);
2171 adapter->stats.scc += E1000_READ_REG(hw, SCC);
2172 adapter->stats.ecol += E1000_READ_REG(hw, ECOL);
2173 adapter->stats.mcc += E1000_READ_REG(hw, MCC);
2174 adapter->stats.latecol += E1000_READ_REG(hw, LATECOL);
2175 adapter->stats.dc += E1000_READ_REG(hw, DC);
2176 adapter->stats.sec += E1000_READ_REG(hw, SEC);
2177 adapter->stats.rlec += E1000_READ_REG(hw, RLEC);
2178 adapter->stats.xonrxc += E1000_READ_REG(hw, XONRXC);
2179 adapter->stats.xontxc += E1000_READ_REG(hw, XONTXC);
2180 adapter->stats.xoffrxc += E1000_READ_REG(hw, XOFFRXC);
2181 adapter->stats.xofftxc += E1000_READ_REG(hw, XOFFTXC);
2182 adapter->stats.fcruc += E1000_READ_REG(hw, FCRUC);
2183 adapter->stats.gptc += E1000_READ_REG(hw, GPTC);
2184 adapter->stats.gotcl += E1000_READ_REG(hw, GOTCL);
2185 adapter->stats.gotch += E1000_READ_REG(hw, GOTCH);
2186 adapter->stats.rnbc += E1000_READ_REG(hw, RNBC);
2187 adapter->stats.ruc += E1000_READ_REG(hw, RUC);
2188 adapter->stats.rfc += E1000_READ_REG(hw, RFC);
2189 adapter->stats.rjc += E1000_READ_REG(hw, RJC);
2190 adapter->stats.torl += E1000_READ_REG(hw, TORL);
2191 adapter->stats.torh += E1000_READ_REG(hw, TORH);
2192 adapter->stats.totl += E1000_READ_REG(hw, TOTL);
2193 adapter->stats.toth += E1000_READ_REG(hw, TOTH);
2194 adapter->stats.tpr += E1000_READ_REG(hw, TPR);
2195 adapter->stats.ptc64 += E1000_READ_REG(hw, PTC64);
2196 adapter->stats.ptc127 += E1000_READ_REG(hw, PTC127);
2197 adapter->stats.ptc255 += E1000_READ_REG(hw, PTC255);
2198 adapter->stats.ptc511 += E1000_READ_REG(hw, PTC511);
2199 adapter->stats.ptc1023 += E1000_READ_REG(hw, PTC1023);
2200 adapter->stats.ptc1522 += E1000_READ_REG(hw, PTC1522);
2201 adapter->stats.mptc += E1000_READ_REG(hw, MPTC);
2202 adapter->stats.bptc += E1000_READ_REG(hw, BPTC);
2203
2204 /* used for adaptive IFS */
2205
2206 hw->tx_packet_delta = E1000_READ_REG(hw, TPT);
2207 adapter->stats.tpt += hw->tx_packet_delta;
2208 hw->collision_delta = E1000_READ_REG(hw, COLC);
2209 adapter->stats.colc += hw->collision_delta;
2210
2211 if(hw->mac_type >= e1000_82543) {
2212 adapter->stats.algnerrc += E1000_READ_REG(hw, ALGNERRC);
2213 adapter->stats.rxerrc += E1000_READ_REG(hw, RXERRC);
2214 adapter->stats.tncrs += E1000_READ_REG(hw, TNCRS);
2215 adapter->stats.cexterr += E1000_READ_REG(hw, CEXTERR);
2216 adapter->stats.tsctc += E1000_READ_REG(hw, TSCTC);
2217 adapter->stats.tsctfc += E1000_READ_REG(hw, TSCTFC);
2218 }
2219
2220 /* Fill out the OS statistics structure */
2221
2222 adapter->net_stats.rx_packets = adapter->stats.gprc;
2223 adapter->net_stats.tx_packets = adapter->stats.gptc;
2224 adapter->net_stats.rx_bytes = adapter->stats.gorcl;
2225 adapter->net_stats.tx_bytes = adapter->stats.gotcl;
2226 adapter->net_stats.multicast = adapter->stats.mprc;
2227 adapter->net_stats.collisions = adapter->stats.colc;
2228
2229 /* Rx Errors */
2230
2231 adapter->net_stats.rx_errors = adapter->stats.rxerrc +
2232 adapter->stats.crcerrs + adapter->stats.algnerrc +
2233 adapter->stats.rlec + adapter->stats.rnbc +
2234 adapter->stats.mpc + adapter->stats.cexterr;
2235 adapter->net_stats.rx_dropped = adapter->stats.rnbc;
2236 adapter->net_stats.rx_length_errors = adapter->stats.rlec;
2237 adapter->net_stats.rx_crc_errors = adapter->stats.crcerrs;
2238 adapter->net_stats.rx_frame_errors = adapter->stats.algnerrc;
2239 adapter->net_stats.rx_fifo_errors = adapter->stats.mpc;
2240 adapter->net_stats.rx_missed_errors = adapter->stats.mpc;
2241
2242 /* Tx Errors */
2243
2244 adapter->net_stats.tx_errors = adapter->stats.ecol +
2245 adapter->stats.latecol;
2246 adapter->net_stats.tx_aborted_errors = adapter->stats.ecol;
2247 adapter->net_stats.tx_window_errors = adapter->stats.latecol;
2248 adapter->net_stats.tx_carrier_errors = adapter->stats.tncrs;
2249
2250 /* Tx Dropped needs to be maintained elsewhere */
2251
2252 /* Phy Stats */
2253
2254 if(hw->media_type == e1000_media_type_copper) {
2255 if((adapter->link_speed == SPEED_1000) &&
2256 (!e1000_read_phy_reg(hw, PHY_1000T_STATUS, &phy_tmp))) {
2257 phy_tmp &= PHY_IDLE_ERROR_COUNT_MASK;
2258 adapter->phy_stats.idle_errors += phy_tmp;
2259 }
2260
2261 if((hw->mac_type <= e1000_82546) &&
2262 (hw->phy_type == e1000_phy_m88) &&
2263 !e1000_read_phy_reg(hw, M88E1000_RX_ERR_CNTR, &phy_tmp))
2264 adapter->phy_stats.receive_errors += phy_tmp;
2265 }
2266
2267 spin_unlock_irqrestore(&adapter->stats_lock, flags);
2268}
2269
2270/**
2271 * e1000_intr - Interrupt Handler
2272 * @irq: interrupt number
2273 * @data: pointer to a network interface device structure
2274 * @pt_regs: CPU registers structure
2275 **/
2276
2277static irqreturn_t
2278e1000_intr(int irq, void *data, struct pt_regs *regs)
2279{
2280 struct net_device *netdev = data;
2281 struct e1000_adapter *adapter = netdev->priv;
2282 struct e1000_hw *hw = &adapter->hw;
2283 uint32_t icr = E1000_READ_REG(hw, ICR);
2284#ifndef CONFIG_E1000_NAPI
2285 unsigned int i;
2286#endif
2287
2288 if(unlikely(!icr))
2289 return IRQ_NONE; /* Not our interrupt */
2290
2291 if(unlikely(icr & (E1000_ICR_RXSEQ | E1000_ICR_LSC))) {
2292 hw->get_link_status = 1;
2293 mod_timer(&adapter->watchdog_timer, jiffies);
2294 }
2295
2296#ifdef CONFIG_E1000_NAPI
2297 if(likely(netif_rx_schedule_prep(netdev))) {
2298
2299 /* Disable interrupts and register for poll. The flush
2300 of the posted write is intentionally left out.
2301 */
2302
2303 atomic_inc(&adapter->irq_sem);
2304 E1000_WRITE_REG(hw, IMC, ~0);
2305 __netif_rx_schedule(netdev);
2306 }
2307#else
2308 /* Writing IMC and IMS is needed for 82547.
2309 Due to Hub Link bus being occupied, an interrupt
2310 de-assertion message is not able to be sent.
2311 When an interrupt assertion message is generated later,
2312 two messages are re-ordered and sent out.
2313 That causes APIC to think 82547 is in de-assertion
2314 state, while 82547 is in assertion state, resulting
2315 in dead lock. Writing IMC forces 82547 into
2316 de-assertion state.
2317 */
2318 if(hw->mac_type == e1000_82547 || hw->mac_type == e1000_82547_rev_2){
2319 atomic_inc(&adapter->irq_sem);
2320 E1000_WRITE_REG(&adapter->hw, IMC, ~0);
2321 }
2322
2323 for(i = 0; i < E1000_MAX_INTR; i++)
2324 if(unlikely(!e1000_clean_rx_irq(adapter) &
2325 !e1000_clean_tx_irq(adapter)))
2326 break;
2327
2328 if(hw->mac_type == e1000_82547 || hw->mac_type == e1000_82547_rev_2)
2329 e1000_irq_enable(adapter);
2330#endif
2331
2332 return IRQ_HANDLED;
2333}
2334
2335#ifdef CONFIG_E1000_NAPI
2336/**
2337 * e1000_clean - NAPI Rx polling callback
2338 * @adapter: board private structure
2339 **/
2340
2341static int
2342e1000_clean(struct net_device *netdev, int *budget)
2343{
2344 struct e1000_adapter *adapter = netdev->priv;
2345 int work_to_do = min(*budget, netdev->quota);
2346 int tx_cleaned;
2347 int work_done = 0;
2348
2349 tx_cleaned = e1000_clean_tx_irq(adapter);
2350 e1000_clean_rx_irq(adapter, &work_done, work_to_do);
2351
2352 *budget -= work_done;
2353 netdev->quota -= work_done;
2354
2355 /* if no Tx and not enough Rx work done, exit the polling mode */
2356 if((!tx_cleaned && (work_done < work_to_do)) ||
2357 !netif_running(netdev)) {
2358 netif_rx_complete(netdev);
2359 e1000_irq_enable(adapter);
2360 return 0;
2361 }
2362
2363 return 1;
2364}
2365
2366#endif
2367/**
2368 * e1000_clean_tx_irq - Reclaim resources after transmit completes
2369 * @adapter: board private structure
2370 **/
2371
2372static boolean_t
2373e1000_clean_tx_irq(struct e1000_adapter *adapter)
2374{
2375 struct e1000_desc_ring *tx_ring = &adapter->tx_ring;
2376 struct net_device *netdev = adapter->netdev;
2377 struct e1000_tx_desc *tx_desc, *eop_desc;
2378 struct e1000_buffer *buffer_info;
2379 unsigned int i, eop;
2380 boolean_t cleaned = FALSE;
2381
2382 i = tx_ring->next_to_clean;
2383 eop = tx_ring->buffer_info[i].next_to_watch;
2384 eop_desc = E1000_TX_DESC(*tx_ring, eop);
2385
2386 while(eop_desc->upper.data & cpu_to_le32(E1000_TXD_STAT_DD)) {
2387 /* pre-mature writeback of Tx descriptors */
2388 /* clear (free buffers and unmap pci_mapping) */
2389 /* previous_buffer_info */
2390 if (likely(adapter->previous_buffer_info.skb != NULL)) {
2391 e1000_unmap_and_free_tx_resource(adapter,
2392 &adapter->previous_buffer_info);
2393 }
2394
2395 for(cleaned = FALSE; !cleaned; ) {
2396 tx_desc = E1000_TX_DESC(*tx_ring, i);
2397 buffer_info = &tx_ring->buffer_info[i];
2398 cleaned = (i == eop);
2399
2400 /* pre-mature writeback of Tx descriptors */
2401 /* save the cleaning of the this for the */
2402 /* next iteration */
2403 if (cleaned) {
2404 memcpy(&adapter->previous_buffer_info,
2405 buffer_info,
2406 sizeof(struct e1000_buffer));
2407 memset(buffer_info,
2408 0,
2409 sizeof(struct e1000_buffer));
2410 } else {
2411 e1000_unmap_and_free_tx_resource(adapter,
2412 buffer_info);
2413 }
2414
2415 tx_desc->buffer_addr = 0;
2416 tx_desc->lower.data = 0;
2417 tx_desc->upper.data = 0;
2418
2419 cleaned = (i == eop);
2420 if(unlikely(++i == tx_ring->count)) i = 0;
2421 }
2422
2423 eop = tx_ring->buffer_info[i].next_to_watch;
2424 eop_desc = E1000_TX_DESC(*tx_ring, eop);
2425 }
2426
2427 tx_ring->next_to_clean = i;
2428
2429 spin_lock(&adapter->tx_lock);
2430
2431 if(unlikely(cleaned && netif_queue_stopped(netdev) &&
2432 netif_carrier_ok(netdev)))
2433 netif_wake_queue(netdev);
2434
2435 spin_unlock(&adapter->tx_lock);
2436
2437 if(adapter->detect_tx_hung) {
2438 /* detect a transmit hang in hardware, this serializes the
2439 * check with the clearing of time_stamp and movement of i */
2440 adapter->detect_tx_hung = FALSE;
2441 if(tx_ring->buffer_info[i].dma &&
2442 time_after(jiffies, tx_ring->buffer_info[i].time_stamp + HZ) &&
2443 !(E1000_READ_REG(&adapter->hw, STATUS) & E1000_STATUS_TXOFF))
2444 netif_stop_queue(netdev);
2445 }
2446
2447 return cleaned;
2448}
2449
2450/**
2451 * e1000_rx_checksum - Receive Checksum Offload for 82543
2452 * @adapter: board private structure
2453 * @rx_desc: receive descriptor
2454 * @sk_buff: socket buffer with received data
2455 **/
2456
2457static inline void
2458e1000_rx_checksum(struct e1000_adapter *adapter,
2459 struct e1000_rx_desc *rx_desc,
2460 struct sk_buff *skb)
2461{
2462 /* 82543 or newer only */
2463 if(unlikely((adapter->hw.mac_type < e1000_82543) ||
2464 /* Ignore Checksum bit is set */
2465 (rx_desc->status & E1000_RXD_STAT_IXSM) ||
2466 /* TCP Checksum has not been calculated */
2467 (!(rx_desc->status & E1000_RXD_STAT_TCPCS)))) {
2468 skb->ip_summed = CHECKSUM_NONE;
2469 return;
2470 }
2471
2472 /* At this point we know the hardware did the TCP checksum */
2473 /* now look at the TCP checksum error bit */
2474 if(rx_desc->errors & E1000_RXD_ERR_TCPE) {
2475 /* let the stack verify checksum errors */
2476 skb->ip_summed = CHECKSUM_NONE;
2477 adapter->hw_csum_err++;
2478 } else {
2479 /* TCP checksum is good */
2480 skb->ip_summed = CHECKSUM_UNNECESSARY;
2481 adapter->hw_csum_good++;
2482 }
2483}
2484
2485/**
2486 * e1000_clean_rx_irq - Send received data up the network stack
2487 * @adapter: board private structure
2488 **/
2489
2490static boolean_t
2491#ifdef CONFIG_E1000_NAPI
2492e1000_clean_rx_irq(struct e1000_adapter *adapter, int *work_done,
2493 int work_to_do)
2494#else
2495e1000_clean_rx_irq(struct e1000_adapter *adapter)
2496#endif
2497{
2498 struct e1000_desc_ring *rx_ring = &adapter->rx_ring;
2499 struct net_device *netdev = adapter->netdev;
2500 struct pci_dev *pdev = adapter->pdev;
2501 struct e1000_rx_desc *rx_desc;
2502 struct e1000_buffer *buffer_info;
2503 struct sk_buff *skb;
2504 unsigned long flags;
2505 uint32_t length;
2506 uint8_t last_byte;
2507 unsigned int i;
2508 boolean_t cleaned = FALSE;
2509
2510 i = rx_ring->next_to_clean;
2511 rx_desc = E1000_RX_DESC(*rx_ring, i);
2512
2513 while(rx_desc->status & E1000_RXD_STAT_DD) {
2514 buffer_info = &rx_ring->buffer_info[i];
2515#ifdef CONFIG_E1000_NAPI
2516 if(*work_done >= work_to_do)
2517 break;
2518 (*work_done)++;
2519#endif
2520 cleaned = TRUE;
2521
2522 pci_unmap_single(pdev,
2523 buffer_info->dma,
2524 buffer_info->length,
2525 PCI_DMA_FROMDEVICE);
2526
2527 skb = buffer_info->skb;
2528 length = le16_to_cpu(rx_desc->length);
2529
2530 if(unlikely(!(rx_desc->status & E1000_RXD_STAT_EOP))) {
2531 /* All receives must fit into a single buffer */
2532 E1000_DBG("%s: Receive packet consumed multiple"
2533 " buffers\n", netdev->name);
2534 dev_kfree_skb_irq(skb);
2535 goto next_desc;
2536 }
2537
2538 if(unlikely(rx_desc->errors & E1000_RXD_ERR_FRAME_ERR_MASK)) {
2539 last_byte = *(skb->data + length - 1);
2540 if(TBI_ACCEPT(&adapter->hw, rx_desc->status,
2541 rx_desc->errors, length, last_byte)) {
2542 spin_lock_irqsave(&adapter->stats_lock, flags);
2543 e1000_tbi_adjust_stats(&adapter->hw,
2544 &adapter->stats,
2545 length, skb->data);
2546 spin_unlock_irqrestore(&adapter->stats_lock,
2547 flags);
2548 length--;
2549 } else {
2550 dev_kfree_skb_irq(skb);
2551 goto next_desc;
2552 }
2553 }
2554
2555 /* Good Receive */
2556 skb_put(skb, length - ETHERNET_FCS_SIZE);
2557
2558 /* Receive Checksum Offload */
2559 e1000_rx_checksum(adapter, rx_desc, skb);
2560
2561 skb->protocol = eth_type_trans(skb, netdev);
2562#ifdef CONFIG_E1000_NAPI
2563 if(unlikely(adapter->vlgrp &&
2564 (rx_desc->status & E1000_RXD_STAT_VP))) {
2565 vlan_hwaccel_receive_skb(skb, adapter->vlgrp,
2566 le16_to_cpu(rx_desc->special) &
2567 E1000_RXD_SPC_VLAN_MASK);
2568 } else {
2569 netif_receive_skb(skb);
2570 }
2571#else /* CONFIG_E1000_NAPI */
2572 if(unlikely(adapter->vlgrp &&
2573 (rx_desc->status & E1000_RXD_STAT_VP))) {
2574 vlan_hwaccel_rx(skb, adapter->vlgrp,
2575 le16_to_cpu(rx_desc->special) &
2576 E1000_RXD_SPC_VLAN_MASK);
2577 } else {
2578 netif_rx(skb);
2579 }
2580#endif /* CONFIG_E1000_NAPI */
2581 netdev->last_rx = jiffies;
2582
2583next_desc:
2584 rx_desc->status = 0;
2585 buffer_info->skb = NULL;
2586 if(unlikely(++i == rx_ring->count)) i = 0;
2587
2588 rx_desc = E1000_RX_DESC(*rx_ring, i);
2589 }
2590
2591 rx_ring->next_to_clean = i;
2592
2593 e1000_alloc_rx_buffers(adapter);
2594
2595 return cleaned;
2596}
2597
2598/**
2599 * e1000_alloc_rx_buffers - Replace used receive buffers
2600 * @adapter: address of board private structure
2601 **/
2602
2603static void
2604e1000_alloc_rx_buffers(struct e1000_adapter *adapter)
2605{
2606 struct e1000_desc_ring *rx_ring = &adapter->rx_ring;
2607 struct net_device *netdev = adapter->netdev;
2608 struct pci_dev *pdev = adapter->pdev;
2609 struct e1000_rx_desc *rx_desc;
2610 struct e1000_buffer *buffer_info;
2611 struct sk_buff *skb;
2612 unsigned int i, bufsz;
2613
2614 i = rx_ring->next_to_use;
2615 buffer_info = &rx_ring->buffer_info[i];
2616
2617 while(!buffer_info->skb) {
2618 bufsz = adapter->rx_buffer_len + NET_IP_ALIGN;
2619
2620 skb = dev_alloc_skb(bufsz);
2621 if(unlikely(!skb)) {
2622 /* Better luck next round */
2623 break;
2624 }
2625
2626 /* fix for errata 23, cant cross 64kB boundary */
2627 if (!e1000_check_64k_bound(adapter, skb->data, bufsz)) {
2628 struct sk_buff *oldskb = skb;
2629 DPRINTK(RX_ERR,ERR,
2630 "skb align check failed: %u bytes at %p\n",
2631 bufsz, skb->data);
2632 /* try again, without freeing the previous */
2633 skb = dev_alloc_skb(bufsz);
2634 if (!skb) {
2635 dev_kfree_skb(oldskb);
2636 break;
2637 }
2638 if (!e1000_check_64k_bound(adapter, skb->data, bufsz)) {
2639 /* give up */
2640 dev_kfree_skb(skb);
2641 dev_kfree_skb(oldskb);
2642 break; /* while !buffer_info->skb */
2643 } else {
2644 /* move on with the new one */
2645 dev_kfree_skb(oldskb);
2646 }
2647 }
2648
2649 /* Make buffer alignment 2 beyond a 16 byte boundary
2650 * this will result in a 16 byte aligned IP header after
2651 * the 14 byte MAC header is removed
2652 */
2653 skb_reserve(skb, NET_IP_ALIGN);
2654
2655 skb->dev = netdev;
2656
2657 buffer_info->skb = skb;
2658 buffer_info->length = adapter->rx_buffer_len;
2659 buffer_info->dma = pci_map_single(pdev,
2660 skb->data,
2661 adapter->rx_buffer_len,
2662 PCI_DMA_FROMDEVICE);
2663
2664 /* fix for errata 23, cant cross 64kB boundary */
2665 if(!e1000_check_64k_bound(adapter,
2666 (void *)(unsigned long)buffer_info->dma,
2667 adapter->rx_buffer_len)) {
2668 DPRINTK(RX_ERR,ERR,
2669 "dma align check failed: %u bytes at %ld\n",
2670 adapter->rx_buffer_len, (unsigned long)buffer_info->dma);
2671
2672 dev_kfree_skb(skb);
2673 buffer_info->skb = NULL;
2674
2675 pci_unmap_single(pdev,
2676 buffer_info->dma,
2677 adapter->rx_buffer_len,
2678 PCI_DMA_FROMDEVICE);
2679
2680 break; /* while !buffer_info->skb */
2681 }
2682
2683 rx_desc = E1000_RX_DESC(*rx_ring, i);
2684 rx_desc->buffer_addr = cpu_to_le64(buffer_info->dma);
2685
2686 if(unlikely((i & ~(E1000_RX_BUFFER_WRITE - 1)) == i)) {
2687 /* Force memory writes to complete before letting h/w
2688 * know there are new descriptors to fetch. (Only
2689 * applicable for weak-ordered memory model archs,
2690 * such as IA-64). */
2691 wmb();
2692
2693 E1000_WRITE_REG(&adapter->hw, RDT, i);
2694 }
2695
2696 if(unlikely(++i == rx_ring->count)) i = 0;
2697 buffer_info = &rx_ring->buffer_info[i];
2698 }
2699
2700 rx_ring->next_to_use = i;
2701}
2702
2703/**
2704 * e1000_smartspeed - Workaround for SmartSpeed on 82541 and 82547 controllers.
2705 * @adapter:
2706 **/
2707
2708static void
2709e1000_smartspeed(struct e1000_adapter *adapter)
2710{
2711 uint16_t phy_status;
2712 uint16_t phy_ctrl;
2713
2714 if((adapter->hw.phy_type != e1000_phy_igp) || !adapter->hw.autoneg ||
2715 !(adapter->hw.autoneg_advertised & ADVERTISE_1000_FULL))
2716 return;
2717
2718 if(adapter->smartspeed == 0) {
2719 /* If Master/Slave config fault is asserted twice,
2720 * we assume back-to-back */
2721 e1000_read_phy_reg(&adapter->hw, PHY_1000T_STATUS, &phy_status);
2722 if(!(phy_status & SR_1000T_MS_CONFIG_FAULT)) return;
2723 e1000_read_phy_reg(&adapter->hw, PHY_1000T_STATUS, &phy_status);
2724 if(!(phy_status & SR_1000T_MS_CONFIG_FAULT)) return;
2725 e1000_read_phy_reg(&adapter->hw, PHY_1000T_CTRL, &phy_ctrl);
2726 if(phy_ctrl & CR_1000T_MS_ENABLE) {
2727 phy_ctrl &= ~CR_1000T_MS_ENABLE;
2728 e1000_write_phy_reg(&adapter->hw, PHY_1000T_CTRL,
2729 phy_ctrl);
2730 adapter->smartspeed++;
2731 if(!e1000_phy_setup_autoneg(&adapter->hw) &&
2732 !e1000_read_phy_reg(&adapter->hw, PHY_CTRL,
2733 &phy_ctrl)) {
2734 phy_ctrl |= (MII_CR_AUTO_NEG_EN |
2735 MII_CR_RESTART_AUTO_NEG);
2736 e1000_write_phy_reg(&adapter->hw, PHY_CTRL,
2737 phy_ctrl);
2738 }
2739 }
2740 return;
2741 } else if(adapter->smartspeed == E1000_SMARTSPEED_DOWNSHIFT) {
2742 /* If still no link, perhaps using 2/3 pair cable */
2743 e1000_read_phy_reg(&adapter->hw, PHY_1000T_CTRL, &phy_ctrl);
2744 phy_ctrl |= CR_1000T_MS_ENABLE;
2745 e1000_write_phy_reg(&adapter->hw, PHY_1000T_CTRL, phy_ctrl);
2746 if(!e1000_phy_setup_autoneg(&adapter->hw) &&
2747 !e1000_read_phy_reg(&adapter->hw, PHY_CTRL, &phy_ctrl)) {
2748 phy_ctrl |= (MII_CR_AUTO_NEG_EN |
2749 MII_CR_RESTART_AUTO_NEG);
2750 e1000_write_phy_reg(&adapter->hw, PHY_CTRL, phy_ctrl);
2751 }
2752 }
2753 /* Restart process after E1000_SMARTSPEED_MAX iterations */
2754 if(adapter->smartspeed++ == E1000_SMARTSPEED_MAX)
2755 adapter->smartspeed = 0;
2756}
2757
2758/**
2759 * e1000_ioctl -
2760 * @netdev:
2761 * @ifreq:
2762 * @cmd:
2763 **/
2764
2765static int
2766e1000_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2767{
2768 switch (cmd) {
2769 case SIOCGMIIPHY:
2770 case SIOCGMIIREG:
2771 case SIOCSMIIREG:
2772 return e1000_mii_ioctl(netdev, ifr, cmd);
2773 default:
2774 return -EOPNOTSUPP;
2775 }
2776}
2777
2778/**
2779 * e1000_mii_ioctl -
2780 * @netdev:
2781 * @ifreq:
2782 * @cmd:
2783 **/
2784
2785static int
2786e1000_mii_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2787{
2788 struct e1000_adapter *adapter = netdev->priv;
2789 struct mii_ioctl_data *data = if_mii(ifr);
2790 int retval;
2791 uint16_t mii_reg;
2792 uint16_t spddplx;
2793
2794 if(adapter->hw.media_type != e1000_media_type_copper)
2795 return -EOPNOTSUPP;
2796
2797 switch (cmd) {
2798 case SIOCGMIIPHY:
2799 data->phy_id = adapter->hw.phy_addr;
2800 break;
2801 case SIOCGMIIREG:
2802 if (!capable(CAP_NET_ADMIN))
2803 return -EPERM;
2804 if (e1000_read_phy_reg(&adapter->hw, data->reg_num & 0x1F,
2805 &data->val_out))
2806 return -EIO;
2807 break;
2808 case SIOCSMIIREG:
2809 if (!capable(CAP_NET_ADMIN))
2810 return -EPERM;
2811 if (data->reg_num & ~(0x1F))
2812 return -EFAULT;
2813 mii_reg = data->val_in;
2814 if (e1000_write_phy_reg(&adapter->hw, data->reg_num,
2815 mii_reg))
2816 return -EIO;
2817 if (adapter->hw.phy_type == e1000_phy_m88) {
2818 switch (data->reg_num) {
2819 case PHY_CTRL:
2820 if(mii_reg & MII_CR_POWER_DOWN)
2821 break;
2822 if(mii_reg & MII_CR_AUTO_NEG_EN) {
2823 adapter->hw.autoneg = 1;
2824 adapter->hw.autoneg_advertised = 0x2F;
2825 } else {
2826 if (mii_reg & 0x40)
2827 spddplx = SPEED_1000;
2828 else if (mii_reg & 0x2000)
2829 spddplx = SPEED_100;
2830 else
2831 spddplx = SPEED_10;
2832 spddplx += (mii_reg & 0x100)
2833 ? FULL_DUPLEX :
2834 HALF_DUPLEX;
2835 retval = e1000_set_spd_dplx(adapter,
2836 spddplx);
2837 if(retval)
2838 return retval;
2839 }
2840 if(netif_running(adapter->netdev)) {
2841 e1000_down(adapter);
2842 e1000_up(adapter);
2843 } else
2844 e1000_reset(adapter);
2845 break;
2846 case M88E1000_PHY_SPEC_CTRL:
2847 case M88E1000_EXT_PHY_SPEC_CTRL:
2848 if (e1000_phy_reset(&adapter->hw))
2849 return -EIO;
2850 break;
2851 }
2852 } else {
2853 switch (data->reg_num) {
2854 case PHY_CTRL:
2855 if(mii_reg & MII_CR_POWER_DOWN)
2856 break;
2857 if(netif_running(adapter->netdev)) {
2858 e1000_down(adapter);
2859 e1000_up(adapter);
2860 } else
2861 e1000_reset(adapter);
2862 break;
2863 }
2864 }
2865 break;
2866 default:
2867 return -EOPNOTSUPP;
2868 }
2869 return E1000_SUCCESS;
2870}
2871
2872void
2873e1000_pci_set_mwi(struct e1000_hw *hw)
2874{
2875 struct e1000_adapter *adapter = hw->back;
2876
2877 int ret;
2878 ret = pci_set_mwi(adapter->pdev);
2879}
2880
2881void
2882e1000_pci_clear_mwi(struct e1000_hw *hw)
2883{
2884 struct e1000_adapter *adapter = hw->back;
2885
2886 pci_clear_mwi(adapter->pdev);
2887}
2888
2889void
2890e1000_read_pci_cfg(struct e1000_hw *hw, uint32_t reg, uint16_t *value)
2891{
2892 struct e1000_adapter *adapter = hw->back;
2893
2894 pci_read_config_word(adapter->pdev, reg, value);
2895}
2896
2897void
2898e1000_write_pci_cfg(struct e1000_hw *hw, uint32_t reg, uint16_t *value)
2899{
2900 struct e1000_adapter *adapter = hw->back;
2901
2902 pci_write_config_word(adapter->pdev, reg, *value);
2903}
2904
2905uint32_t
2906e1000_io_read(struct e1000_hw *hw, unsigned long port)
2907{
2908 return inl(port);
2909}
2910
2911void
2912e1000_io_write(struct e1000_hw *hw, unsigned long port, uint32_t value)
2913{
2914 outl(value, port);
2915}
2916
2917static void
2918e1000_vlan_rx_register(struct net_device *netdev, struct vlan_group *grp)
2919{
2920 struct e1000_adapter *adapter = netdev->priv;
2921 uint32_t ctrl, rctl;
2922
2923 e1000_irq_disable(adapter);
2924 adapter->vlgrp = grp;
2925
2926 if(grp) {
2927 /* enable VLAN tag insert/strip */
2928 ctrl = E1000_READ_REG(&adapter->hw, CTRL);
2929 ctrl |= E1000_CTRL_VME;
2930 E1000_WRITE_REG(&adapter->hw, CTRL, ctrl);
2931
2932 /* enable VLAN receive filtering */
2933 rctl = E1000_READ_REG(&adapter->hw, RCTL);
2934 rctl |= E1000_RCTL_VFE;
2935 rctl &= ~E1000_RCTL_CFIEN;
2936 E1000_WRITE_REG(&adapter->hw, RCTL, rctl);
2937 } else {
2938 /* disable VLAN tag insert/strip */
2939 ctrl = E1000_READ_REG(&adapter->hw, CTRL);
2940 ctrl &= ~E1000_CTRL_VME;
2941 E1000_WRITE_REG(&adapter->hw, CTRL, ctrl);
2942
2943 /* disable VLAN filtering */
2944 rctl = E1000_READ_REG(&adapter->hw, RCTL);
2945 rctl &= ~E1000_RCTL_VFE;
2946 E1000_WRITE_REG(&adapter->hw, RCTL, rctl);
2947 }
2948
2949 e1000_irq_enable(adapter);
2950}
2951
2952static void
2953e1000_vlan_rx_add_vid(struct net_device *netdev, uint16_t vid)
2954{
2955 struct e1000_adapter *adapter = netdev->priv;
2956 uint32_t vfta, index;
2957
2958 /* add VID to filter table */
2959 index = (vid >> 5) & 0x7F;
2960 vfta = E1000_READ_REG_ARRAY(&adapter->hw, VFTA, index);
2961 vfta |= (1 << (vid & 0x1F));
2962 e1000_write_vfta(&adapter->hw, index, vfta);
2963}
2964
2965static void
2966e1000_vlan_rx_kill_vid(struct net_device *netdev, uint16_t vid)
2967{
2968 struct e1000_adapter *adapter = netdev->priv;
2969 uint32_t vfta, index;
2970
2971 e1000_irq_disable(adapter);
2972
2973 if(adapter->vlgrp)
2974 adapter->vlgrp->vlan_devices[vid] = NULL;
2975
2976 e1000_irq_enable(adapter);
2977
2978 /* remove VID from filter table */
2979 index = (vid >> 5) & 0x7F;
2980 vfta = E1000_READ_REG_ARRAY(&adapter->hw, VFTA, index);
2981 vfta &= ~(1 << (vid & 0x1F));
2982 e1000_write_vfta(&adapter->hw, index, vfta);
2983}
2984
2985static void
2986e1000_restore_vlan(struct e1000_adapter *adapter)
2987{
2988 e1000_vlan_rx_register(adapter->netdev, adapter->vlgrp);
2989
2990 if(adapter->vlgrp) {
2991 uint16_t vid;
2992 for(vid = 0; vid < VLAN_GROUP_ARRAY_LEN; vid++) {
2993 if(!adapter->vlgrp->vlan_devices[vid])
2994 continue;
2995 e1000_vlan_rx_add_vid(adapter->netdev, vid);
2996 }
2997 }
2998}
2999
3000int
3001e1000_set_spd_dplx(struct e1000_adapter *adapter, uint16_t spddplx)
3002{
3003 adapter->hw.autoneg = 0;
3004
3005 switch(spddplx) {
3006 case SPEED_10 + DUPLEX_HALF:
3007 adapter->hw.forced_speed_duplex = e1000_10_half;
3008 break;
3009 case SPEED_10 + DUPLEX_FULL:
3010 adapter->hw.forced_speed_duplex = e1000_10_full;
3011 break;
3012 case SPEED_100 + DUPLEX_HALF:
3013 adapter->hw.forced_speed_duplex = e1000_100_half;
3014 break;
3015 case SPEED_100 + DUPLEX_FULL:
3016 adapter->hw.forced_speed_duplex = e1000_100_full;
3017 break;
3018 case SPEED_1000 + DUPLEX_FULL:
3019 adapter->hw.autoneg = 1;
3020 adapter->hw.autoneg_advertised = ADVERTISE_1000_FULL;
3021 break;
3022 case SPEED_1000 + DUPLEX_HALF: /* not supported */
3023 default:
3024 DPRINTK(PROBE, ERR,
3025 "Unsupported Speed/Duplexity configuration\n");
3026 return -EINVAL;
3027 }
3028 return 0;
3029}
3030
3031static int
3032e1000_notify_reboot(struct notifier_block *nb, unsigned long event, void *p)
3033{
3034 struct pci_dev *pdev = NULL;
3035
3036 switch(event) {
3037 case SYS_DOWN:
3038 case SYS_HALT:
3039 case SYS_POWER_OFF:
3040 while((pdev = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, pdev))) {
3041 if(pci_dev_driver(pdev) == &e1000_driver)
3042 e1000_suspend(pdev, 3);
3043 }
3044 }
3045 return NOTIFY_DONE;
3046}
3047
3048static int
3049e1000_suspend(struct pci_dev *pdev, uint32_t state)
3050{
3051 struct net_device *netdev = pci_get_drvdata(pdev);
3052 struct e1000_adapter *adapter = netdev->priv;
3053 uint32_t ctrl, ctrl_ext, rctl, manc, status;
3054 uint32_t wufc = adapter->wol;
3055
3056 netif_device_detach(netdev);
3057
3058 if(netif_running(netdev))
3059 e1000_down(adapter);
3060
3061 status = E1000_READ_REG(&adapter->hw, STATUS);
3062 if(status & E1000_STATUS_LU)
3063 wufc &= ~E1000_WUFC_LNKC;
3064
3065 if(wufc) {
3066 e1000_setup_rctl(adapter);
3067 e1000_set_multi(netdev);
3068
3069 /* turn on all-multi mode if wake on multicast is enabled */
3070 if(adapter->wol & E1000_WUFC_MC) {
3071 rctl = E1000_READ_REG(&adapter->hw, RCTL);
3072 rctl |= E1000_RCTL_MPE;
3073 E1000_WRITE_REG(&adapter->hw, RCTL, rctl);
3074 }
3075
3076 if(adapter->hw.mac_type >= e1000_82540) {
3077 ctrl = E1000_READ_REG(&adapter->hw, CTRL);
3078 /* advertise wake from D3Cold */
3079 #define E1000_CTRL_ADVD3WUC 0x00100000
3080 /* phy power management enable */
3081 #define E1000_CTRL_EN_PHY_PWR_MGMT 0x00200000
3082 ctrl |= E1000_CTRL_ADVD3WUC |
3083 E1000_CTRL_EN_PHY_PWR_MGMT;
3084 E1000_WRITE_REG(&adapter->hw, CTRL, ctrl);
3085 }
3086
3087 if(adapter->hw.media_type == e1000_media_type_fiber ||
3088 adapter->hw.media_type == e1000_media_type_internal_serdes) {
3089 /* keep the laser running in D3 */
3090 ctrl_ext = E1000_READ_REG(&adapter->hw, CTRL_EXT);
3091 ctrl_ext |= E1000_CTRL_EXT_SDP7_DATA;
3092 E1000_WRITE_REG(&adapter->hw, CTRL_EXT, ctrl_ext);
3093 }
3094
3095 E1000_WRITE_REG(&adapter->hw, WUC, E1000_WUC_PME_EN);
3096 E1000_WRITE_REG(&adapter->hw, WUFC, wufc);
3097 pci_enable_wake(pdev, 3, 1);
3098 pci_enable_wake(pdev, 4, 1); /* 4 == D3 cold */
3099 } else {
3100 E1000_WRITE_REG(&adapter->hw, WUC, 0);
3101 E1000_WRITE_REG(&adapter->hw, WUFC, 0);
3102 pci_enable_wake(pdev, 3, 0);
3103 pci_enable_wake(pdev, 4, 0); /* 4 == D3 cold */
3104 }
3105
3106 pci_save_state(pdev);
3107
3108 if(adapter->hw.mac_type >= e1000_82540 &&
3109 adapter->hw.media_type == e1000_media_type_copper) {
3110 manc = E1000_READ_REG(&adapter->hw, MANC);
3111 if(manc & E1000_MANC_SMBUS_EN) {
3112 manc |= E1000_MANC_ARP_EN;
3113 E1000_WRITE_REG(&adapter->hw, MANC, manc);
3114 pci_enable_wake(pdev, 3, 1);
3115 pci_enable_wake(pdev, 4, 1); /* 4 == D3 cold */
3116 }
3117 }
3118
3119 pci_disable_device(pdev);
3120
3121 state = (state > 0) ? 3 : 0;
3122 pci_set_power_state(pdev, state);
3123
3124 return 0;
3125}
3126
3127#ifdef CONFIG_PM
3128static int
3129e1000_resume(struct pci_dev *pdev)
3130{
3131 struct net_device *netdev = pci_get_drvdata(pdev);
3132 struct e1000_adapter *adapter = netdev->priv;
3133 uint32_t manc, ret;
3134
3135 pci_set_power_state(pdev, 0);
3136 pci_restore_state(pdev);
3137 ret = pci_enable_device(pdev);
3138 if (pdev->is_busmaster)
3139 pci_set_master(pdev);
3140
3141 pci_enable_wake(pdev, 3, 0);
3142 pci_enable_wake(pdev, 4, 0); /* 4 == D3 cold */
3143
3144 e1000_reset(adapter);
3145 E1000_WRITE_REG(&adapter->hw, WUS, ~0);
3146
3147 if(netif_running(netdev))
3148 e1000_up(adapter);
3149
3150 netif_device_attach(netdev);
3151
3152 if(adapter->hw.mac_type >= e1000_82540 &&
3153 adapter->hw.media_type == e1000_media_type_copper) {
3154 manc = E1000_READ_REG(&adapter->hw, MANC);
3155 manc &= ~(E1000_MANC_ARP_EN);
3156 E1000_WRITE_REG(&adapter->hw, MANC, manc);
3157 }
3158
3159 return 0;
3160}
3161#endif
3162
3163#ifdef CONFIG_NET_POLL_CONTROLLER
3164/*
3165 * Polling 'interrupt' - used by things like netconsole to send skbs
3166 * without having to re-enable interrupts. It's not called while
3167 * the interrupt routine is executing.
3168 */
3169static void
3170e1000_netpoll (struct net_device *netdev)
3171{
3172 struct e1000_adapter *adapter = netdev->priv;
3173 disable_irq(adapter->pdev->irq);
3174 e1000_intr(adapter->pdev->irq, netdev, NULL);
3175 enable_irq(adapter->pdev->irq);
3176}
3177#endif
3178
3179/* e1000_main.c */