blob: e55fb9cc41c37bb7600d4c473622afc606847c8c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*******************************************************************************
2
Auke Kok0abb6eb2006-09-27 12:53:14 -07003 Intel PRO/1000 Linux driver
4 Copyright(c) 1999 - 2006 Intel Corporation.
5
6 This program is free software; you can redistribute it and/or modify it
7 under the terms and conditions of the GNU General Public License,
8 version 2, as published by the Free Software Foundation.
9
10 This program is distributed in the hope it will be useful, but WITHOUT
11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 more details.
Auke Kok0abb6eb2006-09-27 12:53:14 -070014
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 You should have received a copy of the GNU General Public License along with
Auke Kok0abb6eb2006-09-27 12:53:14 -070016 this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
18
19 The full GNU General Public License is included in this distribution in
20 the file called "COPYING".
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 Contact Information:
23 Linux NICS <linux.nics@intel.com>
Auke Kok3d41e302006-04-14 19:05:31 -070024 e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
26
27*******************************************************************************/
28
29#include "e1000.h"
30
Linus Torvalds1da177e2005-04-16 15:20:36 -070031char e1000_driver_name[] = "e1000";
Adrian Bunk3ad2cc62005-10-30 16:53:34 +010032static char e1000_driver_string[] = "Intel(R) PRO/1000 Network Driver";
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#ifndef CONFIG_E1000_NAPI
34#define DRIVERNAPI
35#else
36#define DRIVERNAPI "-NAPI"
37#endif
Auke Kok7cc33232006-08-31 14:27:47 -070038#define DRV_VERSION "7.2.7-k2"DRIVERNAPI
Linus Torvalds1da177e2005-04-16 15:20:36 -070039char e1000_driver_version[] = DRV_VERSION;
Auke Kok3d41e302006-04-14 19:05:31 -070040static char e1000_copyright[] = "Copyright (c) 1999-2006 Intel Corporation.";
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42/* e1000_pci_tbl - PCI Device ID Table
43 *
44 * Last entry must be all 0s
45 *
46 * Macro expands to...
47 * {PCI_DEVICE(PCI_VENDOR_ID_INTEL, device_id)}
48 */
49static struct pci_device_id e1000_pci_tbl[] = {
50 INTEL_E1000_ETHERNET_DEVICE(0x1000),
51 INTEL_E1000_ETHERNET_DEVICE(0x1001),
52 INTEL_E1000_ETHERNET_DEVICE(0x1004),
53 INTEL_E1000_ETHERNET_DEVICE(0x1008),
54 INTEL_E1000_ETHERNET_DEVICE(0x1009),
55 INTEL_E1000_ETHERNET_DEVICE(0x100C),
56 INTEL_E1000_ETHERNET_DEVICE(0x100D),
57 INTEL_E1000_ETHERNET_DEVICE(0x100E),
58 INTEL_E1000_ETHERNET_DEVICE(0x100F),
59 INTEL_E1000_ETHERNET_DEVICE(0x1010),
60 INTEL_E1000_ETHERNET_DEVICE(0x1011),
61 INTEL_E1000_ETHERNET_DEVICE(0x1012),
62 INTEL_E1000_ETHERNET_DEVICE(0x1013),
63 INTEL_E1000_ETHERNET_DEVICE(0x1014),
64 INTEL_E1000_ETHERNET_DEVICE(0x1015),
65 INTEL_E1000_ETHERNET_DEVICE(0x1016),
66 INTEL_E1000_ETHERNET_DEVICE(0x1017),
67 INTEL_E1000_ETHERNET_DEVICE(0x1018),
68 INTEL_E1000_ETHERNET_DEVICE(0x1019),
Malli Chilakala26483452005-04-28 19:44:46 -070069 INTEL_E1000_ETHERNET_DEVICE(0x101A),
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 INTEL_E1000_ETHERNET_DEVICE(0x101D),
71 INTEL_E1000_ETHERNET_DEVICE(0x101E),
72 INTEL_E1000_ETHERNET_DEVICE(0x1026),
73 INTEL_E1000_ETHERNET_DEVICE(0x1027),
74 INTEL_E1000_ETHERNET_DEVICE(0x1028),
Auke Kokae2c3862006-06-27 09:08:30 -070075 INTEL_E1000_ETHERNET_DEVICE(0x1049),
76 INTEL_E1000_ETHERNET_DEVICE(0x104A),
77 INTEL_E1000_ETHERNET_DEVICE(0x104B),
78 INTEL_E1000_ETHERNET_DEVICE(0x104C),
79 INTEL_E1000_ETHERNET_DEVICE(0x104D),
Mallikarjuna R Chilakala07b8fed2005-10-19 10:40:08 -040080 INTEL_E1000_ETHERNET_DEVICE(0x105E),
81 INTEL_E1000_ETHERNET_DEVICE(0x105F),
82 INTEL_E1000_ETHERNET_DEVICE(0x1060),
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 INTEL_E1000_ETHERNET_DEVICE(0x1075),
84 INTEL_E1000_ETHERNET_DEVICE(0x1076),
85 INTEL_E1000_ETHERNET_DEVICE(0x1077),
86 INTEL_E1000_ETHERNET_DEVICE(0x1078),
87 INTEL_E1000_ETHERNET_DEVICE(0x1079),
88 INTEL_E1000_ETHERNET_DEVICE(0x107A),
89 INTEL_E1000_ETHERNET_DEVICE(0x107B),
90 INTEL_E1000_ETHERNET_DEVICE(0x107C),
Mallikarjuna R Chilakala07b8fed2005-10-19 10:40:08 -040091 INTEL_E1000_ETHERNET_DEVICE(0x107D),
92 INTEL_E1000_ETHERNET_DEVICE(0x107E),
93 INTEL_E1000_ETHERNET_DEVICE(0x107F),
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 INTEL_E1000_ETHERNET_DEVICE(0x108A),
Malli Chilakala26483452005-04-28 19:44:46 -070095 INTEL_E1000_ETHERNET_DEVICE(0x108B),
96 INTEL_E1000_ETHERNET_DEVICE(0x108C),
Jeff Kirsher6418ecc2006-03-02 18:21:10 -080097 INTEL_E1000_ETHERNET_DEVICE(0x1096),
98 INTEL_E1000_ETHERNET_DEVICE(0x1098),
Jeff Kirsherb7ee49d2006-01-12 16:51:21 -080099 INTEL_E1000_ETHERNET_DEVICE(0x1099),
Mallikarjuna R Chilakala07b8fed2005-10-19 10:40:08 -0400100 INTEL_E1000_ETHERNET_DEVICE(0x109A),
Jesse Brandeburg5881cde2006-08-31 14:27:47 -0700101 INTEL_E1000_ETHERNET_DEVICE(0x10A4),
Jeff Kirsherb7ee49d2006-01-12 16:51:21 -0800102 INTEL_E1000_ETHERNET_DEVICE(0x10B5),
Jeff Kirsher6418ecc2006-03-02 18:21:10 -0800103 INTEL_E1000_ETHERNET_DEVICE(0x10B9),
Auke Kokae2c3862006-06-27 09:08:30 -0700104 INTEL_E1000_ETHERNET_DEVICE(0x10BA),
105 INTEL_E1000_ETHERNET_DEVICE(0x10BB),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 /* required last entry */
107 {0,}
108};
109
110MODULE_DEVICE_TABLE(pci, e1000_pci_tbl);
111
Nicholas Nunley35574762006-09-27 12:53:34 -0700112int e1000_up(struct e1000_adapter *adapter);
113void e1000_down(struct e1000_adapter *adapter);
114void e1000_reinit_locked(struct e1000_adapter *adapter);
115void e1000_reset(struct e1000_adapter *adapter);
116int e1000_set_spd_dplx(struct e1000_adapter *adapter, uint16_t spddplx);
117int e1000_setup_all_tx_resources(struct e1000_adapter *adapter);
118int e1000_setup_all_rx_resources(struct e1000_adapter *adapter);
119void e1000_free_all_tx_resources(struct e1000_adapter *adapter);
120void e1000_free_all_rx_resources(struct e1000_adapter *adapter);
Adrian Bunk3ad2cc62005-10-30 16:53:34 +0100121static int e1000_setup_tx_resources(struct e1000_adapter *adapter,
Nicholas Nunley35574762006-09-27 12:53:34 -0700122 struct e1000_tx_ring *txdr);
Adrian Bunk3ad2cc62005-10-30 16:53:34 +0100123static int e1000_setup_rx_resources(struct e1000_adapter *adapter,
Nicholas Nunley35574762006-09-27 12:53:34 -0700124 struct e1000_rx_ring *rxdr);
Adrian Bunk3ad2cc62005-10-30 16:53:34 +0100125static void e1000_free_tx_resources(struct e1000_adapter *adapter,
Nicholas Nunley35574762006-09-27 12:53:34 -0700126 struct e1000_tx_ring *tx_ring);
Adrian Bunk3ad2cc62005-10-30 16:53:34 +0100127static void e1000_free_rx_resources(struct e1000_adapter *adapter,
Nicholas Nunley35574762006-09-27 12:53:34 -0700128 struct e1000_rx_ring *rx_ring);
129void e1000_update_stats(struct e1000_adapter *adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
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);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -0400135static int e1000_alloc_queues(struct e1000_adapter *adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136static int e1000_sw_init(struct e1000_adapter *adapter);
137static int e1000_open(struct net_device *netdev);
138static int e1000_close(struct net_device *netdev);
139static void e1000_configure_tx(struct e1000_adapter *adapter);
140static void e1000_configure_rx(struct e1000_adapter *adapter);
141static void e1000_setup_rctl(struct e1000_adapter *adapter);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -0400142static void e1000_clean_all_tx_rings(struct e1000_adapter *adapter);
143static void e1000_clean_all_rx_rings(struct e1000_adapter *adapter);
144static void e1000_clean_tx_ring(struct e1000_adapter *adapter,
145 struct e1000_tx_ring *tx_ring);
146static void e1000_clean_rx_ring(struct e1000_adapter *adapter,
147 struct e1000_rx_ring *rx_ring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148static void e1000_set_multi(struct net_device *netdev);
149static void e1000_update_phy_info(unsigned long data);
150static void e1000_watchdog(unsigned long data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151static void e1000_82547_tx_fifo_stall(unsigned long data);
152static int e1000_xmit_frame(struct sk_buff *skb, struct net_device *netdev);
153static struct net_device_stats * e1000_get_stats(struct net_device *netdev);
154static int e1000_change_mtu(struct net_device *netdev, int new_mtu);
155static int e1000_set_mac(struct net_device *netdev, void *p);
156static irqreturn_t e1000_intr(int irq, void *data, struct pt_regs *regs);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -0400157static boolean_t e1000_clean_tx_irq(struct e1000_adapter *adapter,
158 struct e1000_tx_ring *tx_ring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159#ifdef CONFIG_E1000_NAPI
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -0400160static int e1000_clean(struct net_device *poll_dev, int *budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161static boolean_t e1000_clean_rx_irq(struct e1000_adapter *adapter,
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -0400162 struct e1000_rx_ring *rx_ring,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 int *work_done, int work_to_do);
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700164static boolean_t e1000_clean_rx_irq_ps(struct e1000_adapter *adapter,
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -0400165 struct e1000_rx_ring *rx_ring,
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700166 int *work_done, int work_to_do);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167#else
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -0400168static boolean_t e1000_clean_rx_irq(struct e1000_adapter *adapter,
169 struct e1000_rx_ring *rx_ring);
170static boolean_t e1000_clean_rx_irq_ps(struct e1000_adapter *adapter,
171 struct e1000_rx_ring *rx_ring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172#endif
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -0400173static void e1000_alloc_rx_buffers(struct e1000_adapter *adapter,
Jeff Kirsher72d64a42006-01-12 16:51:19 -0800174 struct e1000_rx_ring *rx_ring,
175 int cleaned_count);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -0400176static void e1000_alloc_rx_buffers_ps(struct e1000_adapter *adapter,
Jeff Kirsher72d64a42006-01-12 16:51:19 -0800177 struct e1000_rx_ring *rx_ring,
178 int cleaned_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179static int e1000_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd);
180static int e1000_mii_ioctl(struct net_device *netdev, struct ifreq *ifr,
181 int cmd);
Nicholas Nunley35574762006-09-27 12:53:34 -0700182void e1000_set_ethtool_ops(struct net_device *netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183static void e1000_enter_82542_rst(struct e1000_adapter *adapter);
184static void e1000_leave_82542_rst(struct e1000_adapter *adapter);
185static void e1000_tx_timeout(struct net_device *dev);
Jeff Kirsher87041632006-03-02 18:21:24 -0800186static void e1000_reset_task(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187static void e1000_smartspeed(struct e1000_adapter *adapter);
Auke Koke619d522006-04-14 19:04:52 -0700188static int e1000_82547_fifo_workaround(struct e1000_adapter *adapter,
189 struct sk_buff *skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191static void e1000_vlan_rx_register(struct net_device *netdev, struct vlan_group *grp);
192static void e1000_vlan_rx_add_vid(struct net_device *netdev, uint16_t vid);
193static void e1000_vlan_rx_kill_vid(struct net_device *netdev, uint16_t vid);
194static void e1000_restore_vlan(struct e1000_adapter *adapter);
195
Ashutosh Naik977e74b2005-10-28 15:14:53 -0700196static int e1000_suspend(struct pci_dev *pdev, pm_message_t state);
Auke Kok6fdfef12006-06-27 09:06:36 -0700197#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198static int e1000_resume(struct pci_dev *pdev);
199#endif
Auke Kokc653e632006-05-23 13:35:57 -0700200static void e1000_shutdown(struct pci_dev *pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202#ifdef CONFIG_NET_POLL_CONTROLLER
203/* for netdump / net console */
204static void e1000_netpoll (struct net_device *netdev);
205#endif
206
Nicholas Nunley35574762006-09-27 12:53:34 -0700207extern void e1000_check_options(struct e1000_adapter *adapter);
208
Auke Kok90267292006-06-08 09:30:24 -0700209static pci_ers_result_t e1000_io_error_detected(struct pci_dev *pdev,
210 pci_channel_state_t state);
211static pci_ers_result_t e1000_io_slot_reset(struct pci_dev *pdev);
212static void e1000_io_resume(struct pci_dev *pdev);
213
214static struct pci_error_handlers e1000_err_handler = {
215 .error_detected = e1000_io_error_detected,
216 .slot_reset = e1000_io_slot_reset,
217 .resume = e1000_io_resume,
218};
Mallikarjuna R Chilakala24025e4ec2005-10-04 07:03:23 -0400219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220static struct pci_driver e1000_driver = {
221 .name = e1000_driver_name,
222 .id_table = e1000_pci_tbl,
223 .probe = e1000_probe,
224 .remove = __devexit_p(e1000_remove),
Auke Kokc4e24f02006-09-27 12:53:19 -0700225#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 /* Power Managment Hooks */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 .suspend = e1000_suspend,
Auke Kokc653e632006-05-23 13:35:57 -0700228 .resume = e1000_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229#endif
Auke Kok90267292006-06-08 09:30:24 -0700230 .shutdown = e1000_shutdown,
231 .err_handler = &e1000_err_handler
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232};
233
234MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
235MODULE_DESCRIPTION("Intel(R) PRO/1000 Network Driver");
236MODULE_LICENSE("GPL");
237MODULE_VERSION(DRV_VERSION);
238
239static int debug = NETIF_MSG_DRV | NETIF_MSG_PROBE;
240module_param(debug, int, 0);
241MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
242
243/**
244 * e1000_init_module - Driver Registration Routine
245 *
246 * e1000_init_module is the first routine called when the driver is
247 * loaded. All it does is register with the PCI subsystem.
248 **/
249
250static int __init
251e1000_init_module(void)
252{
253 int ret;
254 printk(KERN_INFO "%s - version %s\n",
255 e1000_driver_string, e1000_driver_version);
256
257 printk(KERN_INFO "%s\n", e1000_copyright);
258
Jeff Garzik29917622006-08-19 17:48:59 -0400259 ret = pci_register_driver(&e1000_driver);
Tony Luck8b378def2005-07-28 01:07:38 -0700260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 return ret;
262}
263
264module_init(e1000_init_module);
265
266/**
267 * e1000_exit_module - Driver Exit Cleanup Routine
268 *
269 * e1000_exit_module is called just before the driver is removed
270 * from memory.
271 **/
272
273static void __exit
274e1000_exit_module(void)
275{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 pci_unregister_driver(&e1000_driver);
277}
278
279module_exit(e1000_exit_module);
280
Auke Kok2db10a02006-06-27 09:06:28 -0700281static int e1000_request_irq(struct e1000_adapter *adapter)
282{
283 struct net_device *netdev = adapter->netdev;
284 int flags, err = 0;
285
Jeff Garzikc0bc8722006-07-05 14:32:39 -0400286 flags = IRQF_SHARED;
Auke Kok2db10a02006-06-27 09:06:28 -0700287#ifdef CONFIG_PCI_MSI
288 if (adapter->hw.mac_type > e1000_82547_rev_2) {
289 adapter->have_msi = TRUE;
290 if ((err = pci_enable_msi(adapter->pdev))) {
291 DPRINTK(PROBE, ERR,
292 "Unable to allocate MSI interrupt Error: %d\n", err);
293 adapter->have_msi = FALSE;
294 }
295 }
296 if (adapter->have_msi)
Andrew Morton61ef5c02006-07-06 23:58:19 -0700297 flags &= ~IRQF_SHARED;
Auke Kok2db10a02006-06-27 09:06:28 -0700298#endif
299 if ((err = request_irq(adapter->pdev->irq, &e1000_intr, flags,
300 netdev->name, netdev)))
301 DPRINTK(PROBE, ERR,
302 "Unable to allocate interrupt Error: %d\n", err);
303
304 return err;
305}
306
307static void e1000_free_irq(struct e1000_adapter *adapter)
308{
309 struct net_device *netdev = adapter->netdev;
310
311 free_irq(adapter->pdev->irq, netdev);
312
313#ifdef CONFIG_PCI_MSI
314 if (adapter->have_msi)
315 pci_disable_msi(adapter->pdev);
316#endif
317}
318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319/**
320 * e1000_irq_disable - Mask off interrupt generation on the NIC
321 * @adapter: board private structure
322 **/
323
Auke Koke619d522006-04-14 19:04:52 -0700324static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325e1000_irq_disable(struct e1000_adapter *adapter)
326{
327 atomic_inc(&adapter->irq_sem);
328 E1000_WRITE_REG(&adapter->hw, IMC, ~0);
329 E1000_WRITE_FLUSH(&adapter->hw);
330 synchronize_irq(adapter->pdev->irq);
331}
332
333/**
334 * e1000_irq_enable - Enable default interrupt generation settings
335 * @adapter: board private structure
336 **/
337
Auke Koke619d522006-04-14 19:04:52 -0700338static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339e1000_irq_enable(struct e1000_adapter *adapter)
340{
Jesse Brandeburg96838a42006-01-18 13:01:39 -0800341 if (likely(atomic_dec_and_test(&adapter->irq_sem))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 E1000_WRITE_REG(&adapter->hw, IMS, IMS_ENABLE_MASK);
343 E1000_WRITE_FLUSH(&adapter->hw);
344 }
345}
Adrian Bunk3ad2cc62005-10-30 16:53:34 +0100346
347static void
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700348e1000_update_mng_vlan(struct e1000_adapter *adapter)
349{
350 struct net_device *netdev = adapter->netdev;
351 uint16_t vid = adapter->hw.mng_cookie.vlan_id;
352 uint16_t old_vid = adapter->mng_vlan_id;
Jesse Brandeburg96838a42006-01-18 13:01:39 -0800353 if (adapter->vlgrp) {
354 if (!adapter->vlgrp->vlan_devices[vid]) {
355 if (adapter->hw.mng_cookie.status &
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700356 E1000_MNG_DHCP_COOKIE_STATUS_VLAN_SUPPORT) {
357 e1000_vlan_rx_add_vid(netdev, vid);
358 adapter->mng_vlan_id = vid;
359 } else
360 adapter->mng_vlan_id = E1000_MNG_VLAN_NONE;
Jesse Brandeburg96838a42006-01-18 13:01:39 -0800361
362 if ((old_vid != (uint16_t)E1000_MNG_VLAN_NONE) &&
363 (vid != old_vid) &&
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700364 !adapter->vlgrp->vlan_devices[old_vid])
365 e1000_vlan_rx_kill_vid(netdev, old_vid);
Jeff Kirsherc5f226f2006-03-02 18:17:55 -0800366 } else
367 adapter->mng_vlan_id = vid;
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700368 }
369}
Jeff Kirsherb55ccb32006-01-12 16:50:30 -0800370
371/**
372 * e1000_release_hw_control - release control of the h/w to f/w
373 * @adapter: address of board private structure
374 *
375 * e1000_release_hw_control resets {CTRL_EXT|FWSM}:DRV_LOAD bit.
376 * For ASF and Pass Through versions of f/w this means that the
377 * driver is no longer loaded. For AMT version (only with 82573) i
378 * of the f/w this means that the netowrk i/f is closed.
Auke Kok76c224b2006-05-23 13:36:06 -0700379 *
Jeff Kirsherb55ccb32006-01-12 16:50:30 -0800380 **/
381
Auke Koke619d522006-04-14 19:04:52 -0700382static void
Jeff Kirsherb55ccb32006-01-12 16:50:30 -0800383e1000_release_hw_control(struct e1000_adapter *adapter)
384{
385 uint32_t ctrl_ext;
386 uint32_t swsm;
Auke Kokcd94dd02006-06-27 09:08:22 -0700387 uint32_t extcnf;
Jeff Kirsherb55ccb32006-01-12 16:50:30 -0800388
389 /* Let firmware taken over control of h/w */
390 switch (adapter->hw.mac_type) {
391 case e1000_82571:
392 case e1000_82572:
Auke Kok4cc15f52006-04-14 19:04:46 -0700393 case e1000_80003es2lan:
Jeff Kirsherb55ccb32006-01-12 16:50:30 -0800394 ctrl_ext = E1000_READ_REG(&adapter->hw, CTRL_EXT);
395 E1000_WRITE_REG(&adapter->hw, CTRL_EXT,
396 ctrl_ext & ~E1000_CTRL_EXT_DRV_LOAD);
397 break;
398 case e1000_82573:
399 swsm = E1000_READ_REG(&adapter->hw, SWSM);
400 E1000_WRITE_REG(&adapter->hw, SWSM,
401 swsm & ~E1000_SWSM_DRV_LOAD);
Auke Kokcd94dd02006-06-27 09:08:22 -0700402 case e1000_ich8lan:
403 extcnf = E1000_READ_REG(&adapter->hw, CTRL_EXT);
404 E1000_WRITE_REG(&adapter->hw, CTRL_EXT,
405 extcnf & ~E1000_CTRL_EXT_DRV_LOAD);
406 break;
Jeff Kirsherb55ccb32006-01-12 16:50:30 -0800407 default:
408 break;
409 }
410}
411
412/**
413 * e1000_get_hw_control - get control of the h/w from f/w
414 * @adapter: address of board private structure
415 *
416 * e1000_get_hw_control sets {CTRL_EXT|FWSM}:DRV_LOAD bit.
Auke Kok76c224b2006-05-23 13:36:06 -0700417 * For ASF and Pass Through versions of f/w this means that
418 * the driver is loaded. For AMT version (only with 82573)
Jeff Kirsherb55ccb32006-01-12 16:50:30 -0800419 * of the f/w this means that the netowrk i/f is open.
Auke Kok76c224b2006-05-23 13:36:06 -0700420 *
Jeff Kirsherb55ccb32006-01-12 16:50:30 -0800421 **/
422
Auke Koke619d522006-04-14 19:04:52 -0700423static void
Jeff Kirsherb55ccb32006-01-12 16:50:30 -0800424e1000_get_hw_control(struct e1000_adapter *adapter)
425{
426 uint32_t ctrl_ext;
427 uint32_t swsm;
Auke Kokcd94dd02006-06-27 09:08:22 -0700428 uint32_t extcnf;
Jeff Kirsherb55ccb32006-01-12 16:50:30 -0800429 /* Let firmware know the driver has taken over */
430 switch (adapter->hw.mac_type) {
431 case e1000_82571:
432 case e1000_82572:
Auke Kok4cc15f52006-04-14 19:04:46 -0700433 case e1000_80003es2lan:
Jeff Kirsherb55ccb32006-01-12 16:50:30 -0800434 ctrl_ext = E1000_READ_REG(&adapter->hw, CTRL_EXT);
435 E1000_WRITE_REG(&adapter->hw, CTRL_EXT,
436 ctrl_ext | E1000_CTRL_EXT_DRV_LOAD);
437 break;
438 case e1000_82573:
439 swsm = E1000_READ_REG(&adapter->hw, SWSM);
440 E1000_WRITE_REG(&adapter->hw, SWSM,
441 swsm | E1000_SWSM_DRV_LOAD);
442 break;
Auke Kokcd94dd02006-06-27 09:08:22 -0700443 case e1000_ich8lan:
444 extcnf = E1000_READ_REG(&adapter->hw, EXTCNF_CTRL);
445 E1000_WRITE_REG(&adapter->hw, EXTCNF_CTRL,
446 extcnf | E1000_EXTCNF_CTRL_SWFLAG);
447 break;
Jeff Kirsherb55ccb32006-01-12 16:50:30 -0800448 default:
449 break;
450 }
451}
452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453int
454e1000_up(struct e1000_adapter *adapter)
455{
456 struct net_device *netdev = adapter->netdev;
Auke Kok2db10a02006-06-27 09:06:28 -0700457 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
459 /* hardware has been reset, we need to reload some things */
460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 e1000_set_multi(netdev);
462
463 e1000_restore_vlan(adapter);
464
465 e1000_configure_tx(adapter);
466 e1000_setup_rctl(adapter);
467 e1000_configure_rx(adapter);
Jeff Kirsher72d64a42006-01-12 16:51:19 -0800468 /* call E1000_DESC_UNUSED which always leaves
469 * at least 1 descriptor unused to make sure
470 * next_to_use != next_to_clean */
Jeff Kirsherf56799e2006-01-12 16:50:39 -0800471 for (i = 0; i < adapter->num_rx_queues; i++) {
Jeff Kirsher72d64a42006-01-12 16:51:19 -0800472 struct e1000_rx_ring *ring = &adapter->rx_ring[i];
Jeff Kirshera292ca62006-01-12 16:51:30 -0800473 adapter->alloc_rx_buf(adapter, ring,
474 E1000_DESC_UNUSED(ring));
Jeff Kirsherf56799e2006-01-12 16:50:39 -0800475 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Jeff Kirsher7bfa4812006-01-12 16:50:41 -0800477 adapter->tx_queue_len = netdev->tx_queue_len;
478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 mod_timer(&adapter->watchdog_timer, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
481#ifdef CONFIG_E1000_NAPI
482 netif_poll_enable(netdev);
483#endif
Malli Chilakala5de55622005-04-28 19:39:30 -0700484 e1000_irq_enable(adapter);
485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 return 0;
487}
488
Auke Kok79f05bf2006-06-27 09:06:32 -0700489/**
490 * e1000_power_up_phy - restore link in case the phy was powered down
491 * @adapter: address of board private structure
492 *
493 * The phy may be powered down to save power and turn off link when the
494 * driver is unloaded and wake on lan is not enabled (among others)
495 * *** this routine MUST be followed by a call to e1000_reset ***
496 *
497 **/
498
Jesse Brandeburgd6582662006-08-16 13:31:33 -0700499void e1000_power_up_phy(struct e1000_adapter *adapter)
Auke Kok79f05bf2006-06-27 09:06:32 -0700500{
501 uint16_t mii_reg = 0;
502
503 /* Just clear the power down bit to wake the phy back up */
504 if (adapter->hw.media_type == e1000_media_type_copper) {
505 /* according to the manual, the phy will retain its
506 * settings across a power-down/up cycle */
507 e1000_read_phy_reg(&adapter->hw, PHY_CTRL, &mii_reg);
508 mii_reg &= ~MII_CR_POWER_DOWN;
509 e1000_write_phy_reg(&adapter->hw, PHY_CTRL, mii_reg);
510 }
511}
512
513static void e1000_power_down_phy(struct e1000_adapter *adapter)
514{
515 boolean_t mng_mode_enabled = (adapter->hw.mac_type >= e1000_82571) &&
516 e1000_check_mng_mode(&adapter->hw);
517 /* Power down the PHY so no link is implied when interface is down
518 * The PHY cannot be powered down if any of the following is TRUE
519 * (a) WoL is enabled
520 * (b) AMT is active
521 * (c) SoL/IDER session is active */
522 if (!adapter->wol && adapter->hw.mac_type >= e1000_82540 &&
Auke Kokcd94dd02006-06-27 09:08:22 -0700523 adapter->hw.mac_type != e1000_ich8lan &&
Auke Kok79f05bf2006-06-27 09:06:32 -0700524 adapter->hw.media_type == e1000_media_type_copper &&
525 !(E1000_READ_REG(&adapter->hw, MANC) & E1000_MANC_SMBUS_EN) &&
526 !mng_mode_enabled &&
527 !e1000_check_phy_reset_block(&adapter->hw)) {
528 uint16_t mii_reg = 0;
529 e1000_read_phy_reg(&adapter->hw, PHY_CTRL, &mii_reg);
530 mii_reg |= MII_CR_POWER_DOWN;
531 e1000_write_phy_reg(&adapter->hw, PHY_CTRL, mii_reg);
532 mdelay(1);
533 }
534}
535
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536void
537e1000_down(struct e1000_adapter *adapter)
538{
539 struct net_device *netdev = adapter->netdev;
540
541 e1000_irq_disable(adapter);
Jeff Kirsherc1605eb2006-03-02 18:16:38 -0800542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 del_timer_sync(&adapter->tx_fifo_stall_timer);
544 del_timer_sync(&adapter->watchdog_timer);
545 del_timer_sync(&adapter->phy_info_timer);
546
547#ifdef CONFIG_E1000_NAPI
548 netif_poll_disable(netdev);
549#endif
Jeff Kirsher7bfa4812006-01-12 16:50:41 -0800550 netdev->tx_queue_len = adapter->tx_queue_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 adapter->link_speed = 0;
552 adapter->link_duplex = 0;
553 netif_carrier_off(netdev);
554 netif_stop_queue(netdev);
555
556 e1000_reset(adapter);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -0400557 e1000_clean_all_tx_rings(adapter);
558 e1000_clean_all_rx_rings(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561void
Auke Kok2db10a02006-06-27 09:06:28 -0700562e1000_reinit_locked(struct e1000_adapter *adapter)
563{
564 WARN_ON(in_interrupt());
565 while (test_and_set_bit(__E1000_RESETTING, &adapter->flags))
566 msleep(1);
567 e1000_down(adapter);
568 e1000_up(adapter);
569 clear_bit(__E1000_RESETTING, &adapter->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570}
571
572void
573e1000_reset(struct e1000_adapter *adapter)
574{
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700575 uint32_t pba, manc;
Malli Chilakala1125ecb2005-04-28 19:44:25 -0700576 uint16_t fc_high_water_mark = E1000_FC_HIGH_DIFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
578 /* Repartition Pba for greater than 9k mtu
579 * To take effect CTRL.RST is required.
580 */
581
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700582 switch (adapter->hw.mac_type) {
583 case e1000_82547:
Malli Chilakala0e6ef3e2005-04-28 19:44:14 -0700584 case e1000_82547_rev_2:
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700585 pba = E1000_PBA_30K;
586 break;
Mallikarjuna R Chilakala868d5302005-10-04 06:58:59 -0400587 case e1000_82571:
588 case e1000_82572:
Jeff Kirsher6418ecc2006-03-02 18:21:10 -0800589 case e1000_80003es2lan:
Mallikarjuna R Chilakala868d5302005-10-04 06:58:59 -0400590 pba = E1000_PBA_38K;
591 break;
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700592 case e1000_82573:
593 pba = E1000_PBA_12K;
594 break;
Auke Kokcd94dd02006-06-27 09:08:22 -0700595 case e1000_ich8lan:
596 pba = E1000_PBA_8K;
597 break;
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700598 default:
599 pba = E1000_PBA_48K;
600 break;
601 }
602
Jesse Brandeburg96838a42006-01-18 13:01:39 -0800603 if ((adapter->hw.mac_type != e1000_82573) &&
Jeff Kirsherf11b7f82006-01-12 16:50:51 -0800604 (adapter->netdev->mtu > E1000_RXBUFFER_8192))
Malli Chilakala1125ecb2005-04-28 19:44:25 -0700605 pba -= 8; /* allocate more FIFO for Tx */
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700606
607
Jesse Brandeburg96838a42006-01-18 13:01:39 -0800608 if (adapter->hw.mac_type == e1000_82547) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 adapter->tx_fifo_head = 0;
610 adapter->tx_head_addr = pba << E1000_TX_HEAD_ADDR_SHIFT;
611 adapter->tx_fifo_size =
612 (E1000_PBA_40K - pba) << E1000_PBA_BYTES_SHIFT;
613 atomic_set(&adapter->tx_fifo_stall, 0);
614 }
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700615
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 E1000_WRITE_REG(&adapter->hw, PBA, pba);
617
618 /* flow control settings */
Jeff Kirsherf11b7f82006-01-12 16:50:51 -0800619 /* Set the FC high water mark to 90% of the FIFO size.
620 * Required to clear last 3 LSB */
621 fc_high_water_mark = ((pba * 9216)/10) & 0xFFF8;
Auke Kokcd94dd02006-06-27 09:08:22 -0700622 /* We can't use 90% on small FIFOs because the remainder
623 * would be less than 1 full frame. In this case, we size
624 * it to allow at least a full frame above the high water
625 * mark. */
626 if (pba < E1000_PBA_16K)
627 fc_high_water_mark = (pba * 1024) - 1600;
Jeff Kirsherf11b7f82006-01-12 16:50:51 -0800628
629 adapter->hw.fc_high_water = fc_high_water_mark;
630 adapter->hw.fc_low_water = fc_high_water_mark - 8;
Jeff Kirsher87041632006-03-02 18:21:24 -0800631 if (adapter->hw.mac_type == e1000_80003es2lan)
632 adapter->hw.fc_pause_time = 0xFFFF;
633 else
634 adapter->hw.fc_pause_time = E1000_FC_PAUSE_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 adapter->hw.fc_send_xon = 1;
636 adapter->hw.fc = adapter->hw.original_fc;
637
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700638 /* Allow time for pending master requests to run */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 e1000_reset_hw(&adapter->hw);
Jesse Brandeburg96838a42006-01-18 13:01:39 -0800640 if (adapter->hw.mac_type >= e1000_82544)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 E1000_WRITE_REG(&adapter->hw, WUC, 0);
Jesse Brandeburg96838a42006-01-18 13:01:39 -0800642 if (e1000_init_hw(&adapter->hw))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 DPRINTK(PROBE, ERR, "Hardware Error\n");
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700644 e1000_update_mng_vlan(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 /* Enable h/w to recognize an 802.1Q VLAN Ethernet packet */
646 E1000_WRITE_REG(&adapter->hw, VET, ETHERNET_IEEE_VLAN_TYPE);
647
648 e1000_reset_adaptive(&adapter->hw);
649 e1000_phy_get_info(&adapter->hw, &adapter->phy_info);
Auke Kok9a53a202006-06-27 09:06:45 -0700650
651 if (!adapter->smart_power_down &&
652 (adapter->hw.mac_type == e1000_82571 ||
653 adapter->hw.mac_type == e1000_82572)) {
654 uint16_t phy_data = 0;
655 /* speed up time to link by disabling smart power down, ignore
656 * the return value of this function because there is nothing
657 * different we would do if it failed */
658 e1000_read_phy_reg(&adapter->hw, IGP02E1000_PHY_POWER_MGMT,
659 &phy_data);
660 phy_data &= ~IGP02E1000_PM_SPD;
661 e1000_write_phy_reg(&adapter->hw, IGP02E1000_PHY_POWER_MGMT,
662 phy_data);
663 }
664
Jeff Kirsher5f016072006-09-27 12:53:42 -0700665 if ((adapter->en_mng_pt) && (adapter->hw.mac_type < e1000_82571)) {
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700666 manc = E1000_READ_REG(&adapter->hw, MANC);
667 manc |= (E1000_MANC_ARP_EN | E1000_MANC_EN_MNG2HOST);
668 E1000_WRITE_REG(&adapter->hw, MANC, manc);
669 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670}
671
672/**
673 * e1000_probe - Device Initialization Routine
674 * @pdev: PCI device information struct
675 * @ent: entry in e1000_pci_tbl
676 *
677 * Returns 0 on success, negative on failure
678 *
679 * e1000_probe initializes an adapter identified by a pci_dev structure.
680 * The OS initialization, configuring of the adapter private structure,
681 * and a hardware reset occur.
682 **/
683
684static int __devinit
685e1000_probe(struct pci_dev *pdev,
686 const struct pci_device_id *ent)
687{
688 struct net_device *netdev;
689 struct e1000_adapter *adapter;
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700690 unsigned long mmio_start, mmio_len;
Auke Kokcd94dd02006-06-27 09:08:22 -0700691 unsigned long flash_start, flash_len;
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700692
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 static int cards_found = 0;
Jesse Brandeburg120cd572006-08-31 14:27:46 -0700694 static int global_quad_port_a = 0; /* global ksp3 port a indication */
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700695 int i, err, pci_using_dac;
Jesse Brandeburg120cd572006-08-31 14:27:46 -0700696 uint16_t eeprom_data = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 uint16_t eeprom_apme_mask = E1000_EEPROM_APME;
Jesse Brandeburg96838a42006-01-18 13:01:39 -0800698 if ((err = pci_enable_device(pdev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 return err;
700
Auke Kokcd94dd02006-06-27 09:08:22 -0700701 if (!(err = pci_set_dma_mask(pdev, DMA_64BIT_MASK)) &&
702 !(err = pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 pci_using_dac = 1;
704 } else {
Auke Kokcd94dd02006-06-27 09:08:22 -0700705 if ((err = pci_set_dma_mask(pdev, DMA_32BIT_MASK)) &&
706 (err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 E1000_ERR("No usable DMA configuration, aborting\n");
Vasily Averin6dd62ab2006-08-28 14:56:22 -0700708 goto err_dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 }
710 pci_using_dac = 0;
711 }
712
Jesse Brandeburg96838a42006-01-18 13:01:39 -0800713 if ((err = pci_request_regions(pdev, e1000_driver_name)))
Vasily Averin6dd62ab2006-08-28 14:56:22 -0700714 goto err_pci_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
716 pci_set_master(pdev);
717
Vasily Averin6dd62ab2006-08-28 14:56:22 -0700718 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 netdev = alloc_etherdev(sizeof(struct e1000_adapter));
Vasily Averin6dd62ab2006-08-28 14:56:22 -0700720 if (!netdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 goto err_alloc_etherdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
723 SET_MODULE_OWNER(netdev);
724 SET_NETDEV_DEV(netdev, &pdev->dev);
725
726 pci_set_drvdata(pdev, netdev);
Malli Chilakala60490fe2005-06-17 17:41:45 -0700727 adapter = netdev_priv(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 adapter->netdev = netdev;
729 adapter->pdev = pdev;
730 adapter->hw.back = adapter;
731 adapter->msg_enable = (1 << debug) - 1;
732
733 mmio_start = pci_resource_start(pdev, BAR_0);
734 mmio_len = pci_resource_len(pdev, BAR_0);
735
Vasily Averin6dd62ab2006-08-28 14:56:22 -0700736 err = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 adapter->hw.hw_addr = ioremap(mmio_start, mmio_len);
Vasily Averin6dd62ab2006-08-28 14:56:22 -0700738 if (!adapter->hw.hw_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 goto err_ioremap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
Jesse Brandeburg96838a42006-01-18 13:01:39 -0800741 for (i = BAR_1; i <= BAR_5; i++) {
742 if (pci_resource_len(pdev, i) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 continue;
Jesse Brandeburg96838a42006-01-18 13:01:39 -0800744 if (pci_resource_flags(pdev, i) & IORESOURCE_IO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 adapter->hw.io_base = pci_resource_start(pdev, i);
746 break;
747 }
748 }
749
750 netdev->open = &e1000_open;
751 netdev->stop = &e1000_close;
752 netdev->hard_start_xmit = &e1000_xmit_frame;
753 netdev->get_stats = &e1000_get_stats;
754 netdev->set_multicast_list = &e1000_set_multi;
755 netdev->set_mac_address = &e1000_set_mac;
756 netdev->change_mtu = &e1000_change_mtu;
757 netdev->do_ioctl = &e1000_ioctl;
758 e1000_set_ethtool_ops(netdev);
759 netdev->tx_timeout = &e1000_tx_timeout;
760 netdev->watchdog_timeo = 5 * HZ;
761#ifdef CONFIG_E1000_NAPI
762 netdev->poll = &e1000_clean;
763 netdev->weight = 64;
764#endif
765 netdev->vlan_rx_register = e1000_vlan_rx_register;
766 netdev->vlan_rx_add_vid = e1000_vlan_rx_add_vid;
767 netdev->vlan_rx_kill_vid = e1000_vlan_rx_kill_vid;
768#ifdef CONFIG_NET_POLL_CONTROLLER
769 netdev->poll_controller = e1000_netpoll;
770#endif
Auke Kok0eb5a342006-09-27 12:53:17 -0700771 strncpy(netdev->name, pci_name(pdev), sizeof(netdev->name) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
773 netdev->mem_start = mmio_start;
774 netdev->mem_end = mmio_start + mmio_len;
775 netdev->base_addr = adapter->hw.io_base;
776
777 adapter->bd_number = cards_found;
778
779 /* setup the private structure */
780
Jesse Brandeburg96838a42006-01-18 13:01:39 -0800781 if ((err = e1000_sw_init(adapter)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 goto err_sw_init;
783
Vasily Averin6dd62ab2006-08-28 14:56:22 -0700784 err = -EIO;
Auke Kokcd94dd02006-06-27 09:08:22 -0700785 /* Flash BAR mapping must happen after e1000_sw_init
786 * because it depends on mac_type */
787 if ((adapter->hw.mac_type == e1000_ich8lan) &&
788 (pci_resource_flags(pdev, 1) & IORESOURCE_MEM)) {
789 flash_start = pci_resource_start(pdev, 1);
790 flash_len = pci_resource_len(pdev, 1);
791 adapter->hw.flash_address = ioremap(flash_start, flash_len);
Vasily Averin6dd62ab2006-08-28 14:56:22 -0700792 if (!adapter->hw.flash_address)
Auke Kokcd94dd02006-06-27 09:08:22 -0700793 goto err_flashmap;
Auke Kokcd94dd02006-06-27 09:08:22 -0700794 }
795
Vasily Averin6dd62ab2006-08-28 14:56:22 -0700796 if (e1000_check_phy_reset_block(&adapter->hw))
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700797 DPRINTK(PROBE, INFO, "PHY reset is blocked due to SOL/IDER session.\n");
798
Jesse Brandeburg96838a42006-01-18 13:01:39 -0800799 if (adapter->hw.mac_type >= e1000_82543) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 netdev->features = NETIF_F_SG |
801 NETIF_F_HW_CSUM |
802 NETIF_F_HW_VLAN_TX |
803 NETIF_F_HW_VLAN_RX |
804 NETIF_F_HW_VLAN_FILTER;
Auke Kokcd94dd02006-06-27 09:08:22 -0700805 if (adapter->hw.mac_type == e1000_ich8lan)
806 netdev->features &= ~NETIF_F_HW_VLAN_FILTER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 }
808
809#ifdef NETIF_F_TSO
Jesse Brandeburg96838a42006-01-18 13:01:39 -0800810 if ((adapter->hw.mac_type >= e1000_82544) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 (adapter->hw.mac_type != e1000_82547))
812 netdev->features |= NETIF_F_TSO;
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700813
814#ifdef NETIF_F_TSO_IPV6
Jesse Brandeburg96838a42006-01-18 13:01:39 -0800815 if (adapter->hw.mac_type > e1000_82547_rev_2)
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700816 netdev->features |= NETIF_F_TSO_IPV6;
817#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818#endif
Jesse Brandeburg96838a42006-01-18 13:01:39 -0800819 if (pci_using_dac)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 netdev->features |= NETIF_F_HIGHDMA;
821
Auke Kok76c224b2006-05-23 13:36:06 -0700822 netdev->features |= NETIF_F_LLTX;
823
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700824 adapter->en_mng_pt = e1000_enable_mng_pass_thru(&adapter->hw);
825
Auke Kokcd94dd02006-06-27 09:08:22 -0700826 /* initialize eeprom parameters */
827
828 if (e1000_init_eeprom_params(&adapter->hw)) {
829 E1000_ERR("EEPROM initialization failed\n");
Vasily Averin6dd62ab2006-08-28 14:56:22 -0700830 goto err_eeprom;
Auke Kokcd94dd02006-06-27 09:08:22 -0700831 }
832
Jesse Brandeburg96838a42006-01-18 13:01:39 -0800833 /* before reading the EEPROM, reset the controller to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 * put the device in a known good starting state */
Jesse Brandeburg96838a42006-01-18 13:01:39 -0800835
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 e1000_reset_hw(&adapter->hw);
837
838 /* make sure the EEPROM is good */
839
Jesse Brandeburg96838a42006-01-18 13:01:39 -0800840 if (e1000_validate_eeprom_checksum(&adapter->hw) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 DPRINTK(PROBE, ERR, "The EEPROM Checksum Is Not Valid\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 goto err_eeprom;
843 }
844
845 /* copy the MAC address out of the EEPROM */
846
Jesse Brandeburg96838a42006-01-18 13:01:39 -0800847 if (e1000_read_mac_addr(&adapter->hw))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 DPRINTK(PROBE, ERR, "EEPROM Read Error\n");
849 memcpy(netdev->dev_addr, adapter->hw.mac_addr, netdev->addr_len);
John W. Linville9beb0ac2005-09-12 10:48:55 -0400850 memcpy(netdev->perm_addr, adapter->hw.mac_addr, netdev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
Jesse Brandeburg96838a42006-01-18 13:01:39 -0800852 if (!is_valid_ether_addr(netdev->perm_addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 DPRINTK(PROBE, ERR, "Invalid MAC Address\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 goto err_eeprom;
855 }
856
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 e1000_get_bus_info(&adapter->hw);
858
859 init_timer(&adapter->tx_fifo_stall_timer);
860 adapter->tx_fifo_stall_timer.function = &e1000_82547_tx_fifo_stall;
861 adapter->tx_fifo_stall_timer.data = (unsigned long) adapter;
862
863 init_timer(&adapter->watchdog_timer);
864 adapter->watchdog_timer.function = &e1000_watchdog;
865 adapter->watchdog_timer.data = (unsigned long) adapter;
866
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 init_timer(&adapter->phy_info_timer);
868 adapter->phy_info_timer.function = &e1000_update_phy_info;
869 adapter->phy_info_timer.data = (unsigned long) adapter;
870
Jeff Kirsher87041632006-03-02 18:21:24 -0800871 INIT_WORK(&adapter->reset_task,
872 (void (*)(void *))e1000_reset_task, netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
874 /* we're going to reset, so assume we have no link for now */
875
876 netif_carrier_off(netdev);
877 netif_stop_queue(netdev);
878
879 e1000_check_options(adapter);
880
881 /* Initial Wake on LAN setting
882 * If APM wake is enabled in the EEPROM,
883 * enable the ACPI Magic Packet filter
884 */
885
Jesse Brandeburg96838a42006-01-18 13:01:39 -0800886 switch (adapter->hw.mac_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 case e1000_82542_rev2_0:
888 case e1000_82542_rev2_1:
889 case e1000_82543:
890 break;
891 case e1000_82544:
892 e1000_read_eeprom(&adapter->hw,
893 EEPROM_INIT_CONTROL2_REG, 1, &eeprom_data);
894 eeprom_apme_mask = E1000_EEPROM_82544_APM;
895 break;
Auke Kokcd94dd02006-06-27 09:08:22 -0700896 case e1000_ich8lan:
897 e1000_read_eeprom(&adapter->hw,
898 EEPROM_INIT_CONTROL1_REG, 1, &eeprom_data);
899 eeprom_apme_mask = E1000_EEPROM_ICH8_APME;
900 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 case e1000_82546:
902 case e1000_82546_rev_3:
Jeff Kirsherfd803242005-12-13 00:06:22 -0500903 case e1000_82571:
Jeff Kirsher6418ecc2006-03-02 18:21:10 -0800904 case e1000_80003es2lan:
Jesse Brandeburg96838a42006-01-18 13:01:39 -0800905 if (E1000_READ_REG(&adapter->hw, STATUS) & E1000_STATUS_FUNC_1){
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 e1000_read_eeprom(&adapter->hw,
907 EEPROM_INIT_CONTROL3_PORT_B, 1, &eeprom_data);
908 break;
909 }
910 /* Fall Through */
911 default:
912 e1000_read_eeprom(&adapter->hw,
913 EEPROM_INIT_CONTROL3_PORT_A, 1, &eeprom_data);
914 break;
915 }
Jesse Brandeburg96838a42006-01-18 13:01:39 -0800916 if (eeprom_data & eeprom_apme_mask)
Jesse Brandeburg120cd572006-08-31 14:27:46 -0700917 adapter->eeprom_wol |= E1000_WUFC_MAG;
918
919 /* now that we have the eeprom settings, apply the special cases
920 * where the eeprom may be wrong or the board simply won't support
921 * wake on lan on a particular port */
922 switch (pdev->device) {
923 case E1000_DEV_ID_82546GB_PCIE:
924 adapter->eeprom_wol = 0;
925 break;
926 case E1000_DEV_ID_82546EB_FIBER:
927 case E1000_DEV_ID_82546GB_FIBER:
928 case E1000_DEV_ID_82571EB_FIBER:
929 /* Wake events only supported on port A for dual fiber
930 * regardless of eeprom setting */
931 if (E1000_READ_REG(&adapter->hw, STATUS) & E1000_STATUS_FUNC_1)
932 adapter->eeprom_wol = 0;
933 break;
934 case E1000_DEV_ID_82546GB_QUAD_COPPER_KSP3:
Jesse Brandeburg5881cde2006-08-31 14:27:47 -0700935 case E1000_DEV_ID_82571EB_QUAD_COPPER:
Jesse Brandeburg120cd572006-08-31 14:27:46 -0700936 /* if quad port adapter, disable WoL on all but port A */
937 if (global_quad_port_a != 0)
938 adapter->eeprom_wol = 0;
939 else
940 adapter->quad_port_a = 1;
941 /* Reset for multiple quad port adapters */
942 if (++global_quad_port_a == 4)
943 global_quad_port_a = 0;
944 break;
945 }
946
947 /* initialize the wol settings based on the eeprom settings */
948 adapter->wol = adapter->eeprom_wol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
Jeff Kirsherfb3d47d2006-01-12 16:51:25 -0800950 /* print bus type/speed/width info */
951 {
952 struct e1000_hw *hw = &adapter->hw;
953 DPRINTK(PROBE, INFO, "(PCI%s:%s:%s) ",
954 ((hw->bus_type == e1000_bus_type_pcix) ? "-X" :
955 (hw->bus_type == e1000_bus_type_pci_express ? " Express":"")),
956 ((hw->bus_speed == e1000_bus_speed_2500) ? "2.5Gb/s" :
957 (hw->bus_speed == e1000_bus_speed_133) ? "133MHz" :
958 (hw->bus_speed == e1000_bus_speed_120) ? "120MHz" :
959 (hw->bus_speed == e1000_bus_speed_100) ? "100MHz" :
960 (hw->bus_speed == e1000_bus_speed_66) ? "66MHz" : "33MHz"),
961 ((hw->bus_width == e1000_bus_width_64) ? "64-bit" :
962 (hw->bus_width == e1000_bus_width_pciex_4) ? "Width x4" :
963 (hw->bus_width == e1000_bus_width_pciex_1) ? "Width x1" :
964 "32-bit"));
965 }
966
967 for (i = 0; i < 6; i++)
968 printk("%2.2x%c", netdev->dev_addr[i], i == 5 ? '\n' : ':');
969
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 /* reset the hardware with the new settings */
971 e1000_reset(adapter);
972
Jeff Kirsherb55ccb32006-01-12 16:50:30 -0800973 /* If the controller is 82573 and f/w is AMT, do not set
974 * DRV_LOAD until the interface is up. For all other cases,
975 * let the f/w know that the h/w is now under the control
976 * of the driver. */
977 if (adapter->hw.mac_type != e1000_82573 ||
978 !e1000_check_mng_mode(&adapter->hw))
979 e1000_get_hw_control(adapter);
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700980
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 strcpy(netdev->name, "eth%d");
Jesse Brandeburg96838a42006-01-18 13:01:39 -0800982 if ((err = register_netdev(netdev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 goto err_register;
984
985 DPRINTK(PROBE, INFO, "Intel(R) PRO/1000 Network Connection\n");
986
987 cards_found++;
988 return 0;
989
990err_register:
Vasily Averin6dd62ab2006-08-28 14:56:22 -0700991 e1000_release_hw_control(adapter);
992err_eeprom:
993 if (!e1000_check_phy_reset_block(&adapter->hw))
994 e1000_phy_hw_reset(&adapter->hw);
995
Auke Kokcd94dd02006-06-27 09:08:22 -0700996 if (adapter->hw.flash_address)
997 iounmap(adapter->hw.flash_address);
998err_flashmap:
Vasily Averin6dd62ab2006-08-28 14:56:22 -0700999#ifdef CONFIG_E1000_NAPI
1000 for (i = 0; i < adapter->num_rx_queues; i++)
1001 dev_put(&adapter->polling_netdev[i]);
1002#endif
1003
1004 kfree(adapter->tx_ring);
1005 kfree(adapter->rx_ring);
1006#ifdef CONFIG_E1000_NAPI
1007 kfree(adapter->polling_netdev);
1008#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009err_sw_init:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 iounmap(adapter->hw.hw_addr);
1011err_ioremap:
1012 free_netdev(netdev);
1013err_alloc_etherdev:
1014 pci_release_regions(pdev);
Vasily Averin6dd62ab2006-08-28 14:56:22 -07001015err_pci_reg:
1016err_dma:
1017 pci_disable_device(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 return err;
1019}
1020
1021/**
1022 * e1000_remove - Device Removal Routine
1023 * @pdev: PCI device information struct
1024 *
1025 * e1000_remove is called by the PCI subsystem to alert the driver
1026 * that it should release a PCI device. The could be caused by a
1027 * Hot-Plug event, or because the driver is going to be removed from
1028 * memory.
1029 **/
1030
1031static void __devexit
1032e1000_remove(struct pci_dev *pdev)
1033{
1034 struct net_device *netdev = pci_get_drvdata(pdev);
Malli Chilakala60490fe2005-06-17 17:41:45 -07001035 struct e1000_adapter *adapter = netdev_priv(netdev);
Jeff Kirsherb55ccb32006-01-12 16:50:30 -08001036 uint32_t manc;
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001037#ifdef CONFIG_E1000_NAPI
1038 int i;
1039#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
Jeff Garzikbe2b28e2005-10-04 07:13:43 -04001041 flush_scheduled_work();
1042
Jeff Kirsher5f016072006-09-27 12:53:42 -07001043 if (adapter->hw.mac_type < e1000_82571 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 adapter->hw.media_type == e1000_media_type_copper) {
1045 manc = E1000_READ_REG(&adapter->hw, MANC);
Jesse Brandeburg96838a42006-01-18 13:01:39 -08001046 if (manc & E1000_MANC_SMBUS_EN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 manc |= E1000_MANC_ARP_EN;
1048 E1000_WRITE_REG(&adapter->hw, MANC, manc);
1049 }
1050 }
1051
Jeff Kirsherb55ccb32006-01-12 16:50:30 -08001052 /* Release control of h/w to f/w. If f/w is AMT enabled, this
1053 * would have already happened in close and is redundant. */
1054 e1000_release_hw_control(adapter);
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001055
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 unregister_netdev(netdev);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001057#ifdef CONFIG_E1000_NAPI
Jeff Kirsherf56799e2006-01-12 16:50:39 -08001058 for (i = 0; i < adapter->num_rx_queues; i++)
Stephen Hemminger15333062006-03-20 22:32:28 -08001059 dev_put(&adapter->polling_netdev[i]);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001060#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
Jesse Brandeburg96838a42006-01-18 13:01:39 -08001062 if (!e1000_check_phy_reset_block(&adapter->hw))
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001063 e1000_phy_hw_reset(&adapter->hw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
Mallikarjuna R Chilakala24025e4ec2005-10-04 07:03:23 -04001065 kfree(adapter->tx_ring);
1066 kfree(adapter->rx_ring);
1067#ifdef CONFIG_E1000_NAPI
1068 kfree(adapter->polling_netdev);
1069#endif
1070
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 iounmap(adapter->hw.hw_addr);
Auke Kokcd94dd02006-06-27 09:08:22 -07001072 if (adapter->hw.flash_address)
1073 iounmap(adapter->hw.flash_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 pci_release_regions(pdev);
1075
1076 free_netdev(netdev);
1077
1078 pci_disable_device(pdev);
1079}
1080
1081/**
1082 * e1000_sw_init - Initialize general software structures (struct e1000_adapter)
1083 * @adapter: board private structure to initialize
1084 *
1085 * e1000_sw_init initializes the Adapter private data structure.
1086 * Fields are initialized based on PCI device information and
1087 * OS network device settings (MTU size).
1088 **/
1089
1090static int __devinit
1091e1000_sw_init(struct e1000_adapter *adapter)
1092{
1093 struct e1000_hw *hw = &adapter->hw;
1094 struct net_device *netdev = adapter->netdev;
1095 struct pci_dev *pdev = adapter->pdev;
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001096#ifdef CONFIG_E1000_NAPI
1097 int i;
1098#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
1100 /* PCI config space info */
1101
1102 hw->vendor_id = pdev->vendor;
1103 hw->device_id = pdev->device;
1104 hw->subsystem_vendor_id = pdev->subsystem_vendor;
1105 hw->subsystem_id = pdev->subsystem_device;
1106
1107 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
1108
1109 pci_read_config_word(pdev, PCI_COMMAND, &hw->pci_cmd_word);
1110
Auke Kokeb0f8052006-07-14 16:14:48 -07001111 adapter->rx_buffer_len = MAXIMUM_ETHERNET_VLAN_SIZE;
Auke Kok9e2feac2006-04-14 19:05:18 -07001112 adapter->rx_ps_bsize0 = E1000_RXBUFFER_128;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 hw->max_frame_size = netdev->mtu +
1114 ENET_HEADER_SIZE + ETHERNET_FCS_SIZE;
1115 hw->min_frame_size = MINIMUM_ETHERNET_FRAME_SIZE;
1116
1117 /* identify the MAC */
1118
Jesse Brandeburg96838a42006-01-18 13:01:39 -08001119 if (e1000_set_mac_type(hw)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 DPRINTK(PROBE, ERR, "Unknown MAC Type\n");
1121 return -EIO;
1122 }
1123
Jesse Brandeburg96838a42006-01-18 13:01:39 -08001124 switch (hw->mac_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 default:
1126 break;
1127 case e1000_82541:
1128 case e1000_82547:
1129 case e1000_82541_rev_2:
1130 case e1000_82547_rev_2:
1131 hw->phy_init_script = 1;
1132 break;
1133 }
1134
1135 e1000_set_media_type(hw);
1136
1137 hw->wait_autoneg_complete = FALSE;
1138 hw->tbi_compatibility_en = TRUE;
1139 hw->adaptive_ifs = TRUE;
1140
1141 /* Copper options */
1142
Jesse Brandeburg96838a42006-01-18 13:01:39 -08001143 if (hw->media_type == e1000_media_type_copper) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 hw->mdix = AUTO_ALL_MODES;
1145 hw->disable_polarity_correction = FALSE;
1146 hw->master_slave = E1000_MASTER_SLAVE;
1147 }
1148
Jeff Kirsherf56799e2006-01-12 16:50:39 -08001149 adapter->num_tx_queues = 1;
1150 adapter->num_rx_queues = 1;
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001151
1152 if (e1000_alloc_queues(adapter)) {
1153 DPRINTK(PROBE, ERR, "Unable to allocate memory for queues\n");
1154 return -ENOMEM;
1155 }
1156
1157#ifdef CONFIG_E1000_NAPI
Jeff Kirsherf56799e2006-01-12 16:50:39 -08001158 for (i = 0; i < adapter->num_rx_queues; i++) {
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001159 adapter->polling_netdev[i].priv = adapter;
1160 adapter->polling_netdev[i].poll = &e1000_clean;
1161 adapter->polling_netdev[i].weight = 64;
1162 dev_hold(&adapter->polling_netdev[i]);
1163 set_bit(__LINK_STATE_START, &adapter->polling_netdev[i].state);
1164 }
Jeff Kirsher7bfa4812006-01-12 16:50:41 -08001165 spin_lock_init(&adapter->tx_queue_lock);
Mallikarjuna R Chilakala24025e4ec2005-10-04 07:03:23 -04001166#endif
1167
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 atomic_set(&adapter->irq_sem, 1);
1169 spin_lock_init(&adapter->stats_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
1171 return 0;
1172}
1173
1174/**
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001175 * e1000_alloc_queues - Allocate memory for all rings
1176 * @adapter: board private structure to initialize
1177 *
1178 * We allocate one ring per queue at run-time since we don't know the
1179 * number of queues at compile-time. The polling_netdev array is
1180 * intended for Multiqueue, but should work fine with a single queue.
1181 **/
1182
1183static int __devinit
1184e1000_alloc_queues(struct e1000_adapter *adapter)
1185{
1186 int size;
1187
Jeff Kirsherf56799e2006-01-12 16:50:39 -08001188 size = sizeof(struct e1000_tx_ring) * adapter->num_tx_queues;
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001189 adapter->tx_ring = kmalloc(size, GFP_KERNEL);
1190 if (!adapter->tx_ring)
1191 return -ENOMEM;
1192 memset(adapter->tx_ring, 0, size);
1193
Jeff Kirsherf56799e2006-01-12 16:50:39 -08001194 size = sizeof(struct e1000_rx_ring) * adapter->num_rx_queues;
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001195 adapter->rx_ring = kmalloc(size, GFP_KERNEL);
1196 if (!adapter->rx_ring) {
1197 kfree(adapter->tx_ring);
1198 return -ENOMEM;
1199 }
1200 memset(adapter->rx_ring, 0, size);
1201
1202#ifdef CONFIG_E1000_NAPI
Jeff Kirsherf56799e2006-01-12 16:50:39 -08001203 size = sizeof(struct net_device) * adapter->num_rx_queues;
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001204 adapter->polling_netdev = kmalloc(size, GFP_KERNEL);
1205 if (!adapter->polling_netdev) {
1206 kfree(adapter->tx_ring);
1207 kfree(adapter->rx_ring);
1208 return -ENOMEM;
1209 }
1210 memset(adapter->polling_netdev, 0, size);
1211#endif
1212
1213 return E1000_SUCCESS;
1214}
1215
1216/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 * e1000_open - Called when a network interface is made active
1218 * @netdev: network interface device structure
1219 *
1220 * Returns 0 on success, negative value on failure
1221 *
1222 * The open entry point is called when a network interface is made
1223 * active by the system (IFF_UP). At this point all resources needed
1224 * for transmit and receive operations are allocated, the interrupt
1225 * handler is registered with the OS, the watchdog timer is started,
1226 * and the stack is notified that the interface is ready.
1227 **/
1228
1229static int
1230e1000_open(struct net_device *netdev)
1231{
Malli Chilakala60490fe2005-06-17 17:41:45 -07001232 struct e1000_adapter *adapter = netdev_priv(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 int err;
1234
Auke Kok2db10a02006-06-27 09:06:28 -07001235 /* disallow open during test */
1236 if (test_bit(__E1000_DRIVER_TESTING, &adapter->flags))
1237 return -EBUSY;
1238
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 /* allocate transmit descriptors */
1240
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001241 if ((err = e1000_setup_all_tx_resources(adapter)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 goto err_setup_tx;
1243
1244 /* allocate receive descriptors */
1245
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001246 if ((err = e1000_setup_all_rx_resources(adapter)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 goto err_setup_rx;
1248
Auke Kok2db10a02006-06-27 09:06:28 -07001249 err = e1000_request_irq(adapter);
1250 if (err)
Vasily Averin401a5522006-08-28 14:56:19 -07001251 goto err_req_irq;
Auke Kok2db10a02006-06-27 09:06:28 -07001252
Auke Kok79f05bf2006-06-27 09:06:32 -07001253 e1000_power_up_phy(adapter);
1254
Jesse Brandeburg96838a42006-01-18 13:01:39 -08001255 if ((err = e1000_up(adapter)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 goto err_up;
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001257 adapter->mng_vlan_id = E1000_MNG_VLAN_NONE;
Jesse Brandeburg96838a42006-01-18 13:01:39 -08001258 if ((adapter->hw.mng_cookie.status &
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001259 E1000_MNG_DHCP_COOKIE_STATUS_VLAN_SUPPORT)) {
1260 e1000_update_mng_vlan(adapter);
1261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
Jeff Kirsherb55ccb32006-01-12 16:50:30 -08001263 /* If AMT is enabled, let the firmware know that the network
1264 * interface is now open */
1265 if (adapter->hw.mac_type == e1000_82573 &&
1266 e1000_check_mng_mode(&adapter->hw))
1267 e1000_get_hw_control(adapter);
1268
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 return E1000_SUCCESS;
1270
1271err_up:
Vasily Averin401a5522006-08-28 14:56:19 -07001272 e1000_power_down_phy(adapter);
1273 e1000_free_irq(adapter);
1274err_req_irq:
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001275 e1000_free_all_rx_resources(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276err_setup_rx:
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001277 e1000_free_all_tx_resources(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278err_setup_tx:
1279 e1000_reset(adapter);
1280
1281 return err;
1282}
1283
1284/**
1285 * e1000_close - Disables a network interface
1286 * @netdev: network interface device structure
1287 *
1288 * Returns 0, this is not allowed to fail
1289 *
1290 * The close entry point is called when an interface is de-activated
1291 * by the OS. The hardware is still under the drivers control, but
1292 * needs to be disabled. A global MAC reset is issued to stop the
1293 * hardware, and all transmit and receive resources are freed.
1294 **/
1295
1296static int
1297e1000_close(struct net_device *netdev)
1298{
Malli Chilakala60490fe2005-06-17 17:41:45 -07001299 struct e1000_adapter *adapter = netdev_priv(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
Auke Kok2db10a02006-06-27 09:06:28 -07001301 WARN_ON(test_bit(__E1000_RESETTING, &adapter->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 e1000_down(adapter);
Auke Kok79f05bf2006-06-27 09:06:32 -07001303 e1000_power_down_phy(adapter);
Auke Kok2db10a02006-06-27 09:06:28 -07001304 e1000_free_irq(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001306 e1000_free_all_tx_resources(adapter);
1307 e1000_free_all_rx_resources(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
Jesse Brandeburg96838a42006-01-18 13:01:39 -08001309 if ((adapter->hw.mng_cookie.status &
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001310 E1000_MNG_DHCP_COOKIE_STATUS_VLAN_SUPPORT)) {
1311 e1000_vlan_rx_kill_vid(netdev, adapter->mng_vlan_id);
1312 }
Jeff Kirsherb55ccb32006-01-12 16:50:30 -08001313
1314 /* If AMT is enabled, let the firmware know that the network
1315 * interface is now closed */
1316 if (adapter->hw.mac_type == e1000_82573 &&
1317 e1000_check_mng_mode(&adapter->hw))
1318 e1000_release_hw_control(adapter);
1319
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 return 0;
1321}
1322
1323/**
1324 * e1000_check_64k_bound - check that memory doesn't cross 64kB boundary
1325 * @adapter: address of board private structure
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001326 * @start: address of beginning of memory
1327 * @len: length of memory
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 **/
Auke Koke619d522006-04-14 19:04:52 -07001329static boolean_t
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330e1000_check_64k_bound(struct e1000_adapter *adapter,
1331 void *start, unsigned long len)
1332{
1333 unsigned long begin = (unsigned long) start;
1334 unsigned long end = begin + len;
1335
Malli Chilakala26483452005-04-28 19:44:46 -07001336 /* First rev 82545 and 82546 need to not allow any memory
1337 * write location to cross 64k boundary due to errata 23 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 if (adapter->hw.mac_type == e1000_82545 ||
Malli Chilakala26483452005-04-28 19:44:46 -07001339 adapter->hw.mac_type == e1000_82546) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 return ((begin ^ (end - 1)) >> 16) != 0 ? FALSE : TRUE;
1341 }
1342
1343 return TRUE;
1344}
1345
1346/**
1347 * e1000_setup_tx_resources - allocate Tx resources (Descriptors)
1348 * @adapter: board private structure
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001349 * @txdr: tx descriptor ring (for a specific queue) to setup
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 *
1351 * Return 0 on success, negative on failure
1352 **/
1353
Adrian Bunk3ad2cc62005-10-30 16:53:34 +01001354static int
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001355e1000_setup_tx_resources(struct e1000_adapter *adapter,
1356 struct e1000_tx_ring *txdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 struct pci_dev *pdev = adapter->pdev;
1359 int size;
1360
1361 size = sizeof(struct e1000_buffer) * txdr->count;
Auke Kokcd94dd02006-06-27 09:08:22 -07001362 txdr->buffer_info = vmalloc(size);
Jesse Brandeburg96838a42006-01-18 13:01:39 -08001363 if (!txdr->buffer_info) {
Malli Chilakala26483452005-04-28 19:44:46 -07001364 DPRINTK(PROBE, ERR,
1365 "Unable to allocate memory for the transmit descriptor ring\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 return -ENOMEM;
1367 }
1368 memset(txdr->buffer_info, 0, size);
1369
1370 /* round up to nearest 4K */
1371
1372 txdr->size = txdr->count * sizeof(struct e1000_tx_desc);
1373 E1000_ROUNDUP(txdr->size, 4096);
1374
1375 txdr->desc = pci_alloc_consistent(pdev, txdr->size, &txdr->dma);
Jesse Brandeburg96838a42006-01-18 13:01:39 -08001376 if (!txdr->desc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377setup_tx_desc_die:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 vfree(txdr->buffer_info);
Malli Chilakala26483452005-04-28 19:44:46 -07001379 DPRINTK(PROBE, ERR,
1380 "Unable to allocate memory for the transmit descriptor ring\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 return -ENOMEM;
1382 }
1383
Malli Chilakala26483452005-04-28 19:44:46 -07001384 /* Fix for errata 23, can't cross 64kB boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 if (!e1000_check_64k_bound(adapter, txdr->desc, txdr->size)) {
1386 void *olddesc = txdr->desc;
1387 dma_addr_t olddma = txdr->dma;
Malli Chilakala26483452005-04-28 19:44:46 -07001388 DPRINTK(TX_ERR, ERR, "txdr align check failed: %u bytes "
1389 "at %p\n", txdr->size, txdr->desc);
1390 /* Try again, without freeing the previous */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 txdr->desc = pci_alloc_consistent(pdev, txdr->size, &txdr->dma);
Malli Chilakala26483452005-04-28 19:44:46 -07001392 /* Failed allocation, critical failure */
Jesse Brandeburg96838a42006-01-18 13:01:39 -08001393 if (!txdr->desc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 pci_free_consistent(pdev, txdr->size, olddesc, olddma);
1395 goto setup_tx_desc_die;
1396 }
1397
1398 if (!e1000_check_64k_bound(adapter, txdr->desc, txdr->size)) {
1399 /* give up */
Malli Chilakala26483452005-04-28 19:44:46 -07001400 pci_free_consistent(pdev, txdr->size, txdr->desc,
1401 txdr->dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 pci_free_consistent(pdev, txdr->size, olddesc, olddma);
1403 DPRINTK(PROBE, ERR,
Malli Chilakala26483452005-04-28 19:44:46 -07001404 "Unable to allocate aligned memory "
1405 "for the transmit descriptor ring\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 vfree(txdr->buffer_info);
1407 return -ENOMEM;
1408 } else {
Malli Chilakala26483452005-04-28 19:44:46 -07001409 /* Free old allocation, new allocation was successful */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 pci_free_consistent(pdev, txdr->size, olddesc, olddma);
1411 }
1412 }
1413 memset(txdr->desc, 0, txdr->size);
1414
1415 txdr->next_to_use = 0;
1416 txdr->next_to_clean = 0;
Mallikarjuna R Chilakala2ae76d92005-10-04 07:04:22 -04001417 spin_lock_init(&txdr->tx_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
1419 return 0;
1420}
1421
1422/**
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001423 * e1000_setup_all_tx_resources - wrapper to allocate Tx resources
1424 * (Descriptors) for all queues
1425 * @adapter: board private structure
1426 *
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001427 * Return 0 on success, negative on failure
1428 **/
1429
1430int
1431e1000_setup_all_tx_resources(struct e1000_adapter *adapter)
1432{
1433 int i, err = 0;
1434
Jeff Kirsherf56799e2006-01-12 16:50:39 -08001435 for (i = 0; i < adapter->num_tx_queues; i++) {
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001436 err = e1000_setup_tx_resources(adapter, &adapter->tx_ring[i]);
1437 if (err) {
1438 DPRINTK(PROBE, ERR,
1439 "Allocation for Tx Queue %u failed\n", i);
Vasily Averin3fbbc722006-08-28 14:56:24 -07001440 for (i-- ; i >= 0; i--)
1441 e1000_free_tx_resources(adapter,
1442 &adapter->tx_ring[i]);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001443 break;
1444 }
1445 }
1446
1447 return err;
1448}
1449
1450/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 * e1000_configure_tx - Configure 8254x Transmit Unit after Reset
1452 * @adapter: board private structure
1453 *
1454 * Configure the Tx unit of the MAC after a reset.
1455 **/
1456
1457static void
1458e1000_configure_tx(struct e1000_adapter *adapter)
1459{
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001460 uint64_t tdba;
1461 struct e1000_hw *hw = &adapter->hw;
1462 uint32_t tdlen, tctl, tipg, tarc;
Jeff Kirsher0fadb052006-01-12 16:51:05 -08001463 uint32_t ipgr1, ipgr2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
1465 /* Setup the HW Tx Head and Tail descriptor pointers */
1466
Jeff Kirsherf56799e2006-01-12 16:50:39 -08001467 switch (adapter->num_tx_queues) {
Mallikarjuna R Chilakala24025e4ec2005-10-04 07:03:23 -04001468 case 1:
1469 default:
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001470 tdba = adapter->tx_ring[0].dma;
1471 tdlen = adapter->tx_ring[0].count *
1472 sizeof(struct e1000_tx_desc);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001473 E1000_WRITE_REG(hw, TDLEN, tdlen);
Auke Kok4ca213a2006-06-27 09:07:08 -07001474 E1000_WRITE_REG(hw, TDBAH, (tdba >> 32));
1475 E1000_WRITE_REG(hw, TDBAL, (tdba & 0x00000000ffffffffULL));
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001476 E1000_WRITE_REG(hw, TDT, 0);
Auke Kok4ca213a2006-06-27 09:07:08 -07001477 E1000_WRITE_REG(hw, TDH, 0);
Auke Kok6a951692006-09-11 14:00:21 -07001478 adapter->tx_ring[0].tdh = ((hw->mac_type >= e1000_82543) ? E1000_TDH : E1000_82542_TDH);
1479 adapter->tx_ring[0].tdt = ((hw->mac_type >= e1000_82543) ? E1000_TDT : E1000_82542_TDT);
Mallikarjuna R Chilakala24025e4ec2005-10-04 07:03:23 -04001480 break;
1481 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
1483 /* Set the default values for the Tx Inter Packet Gap timer */
1484
Jeff Kirsher0fadb052006-01-12 16:51:05 -08001485 if (hw->media_type == e1000_media_type_fiber ||
1486 hw->media_type == e1000_media_type_internal_serdes)
1487 tipg = DEFAULT_82543_TIPG_IPGT_FIBER;
1488 else
1489 tipg = DEFAULT_82543_TIPG_IPGT_COPPER;
1490
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001491 switch (hw->mac_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 case e1000_82542_rev2_0:
1493 case e1000_82542_rev2_1:
1494 tipg = DEFAULT_82542_TIPG_IPGT;
Jeff Kirsher0fadb052006-01-12 16:51:05 -08001495 ipgr1 = DEFAULT_82542_TIPG_IPGR1;
1496 ipgr2 = DEFAULT_82542_TIPG_IPGR2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 break;
Jeff Kirsher87041632006-03-02 18:21:24 -08001498 case e1000_80003es2lan:
1499 ipgr1 = DEFAULT_82543_TIPG_IPGR1;
1500 ipgr2 = DEFAULT_80003ES2LAN_TIPG_IPGR2;
1501 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 default:
Jeff Kirsher0fadb052006-01-12 16:51:05 -08001503 ipgr1 = DEFAULT_82543_TIPG_IPGR1;
1504 ipgr2 = DEFAULT_82543_TIPG_IPGR2;
1505 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 }
Jeff Kirsher0fadb052006-01-12 16:51:05 -08001507 tipg |= ipgr1 << E1000_TIPG_IPGR1_SHIFT;
1508 tipg |= ipgr2 << E1000_TIPG_IPGR2_SHIFT;
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001509 E1000_WRITE_REG(hw, TIPG, tipg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
1511 /* Set the Tx Interrupt Delay register */
1512
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001513 E1000_WRITE_REG(hw, TIDV, adapter->tx_int_delay);
1514 if (hw->mac_type >= e1000_82540)
1515 E1000_WRITE_REG(hw, TADV, adapter->tx_abs_int_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516
1517 /* Program the Transmit Control Register */
1518
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001519 tctl = E1000_READ_REG(hw, TCTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
1521 tctl &= ~E1000_TCTL_CT;
Jeff Kirsher7e6c9862006-03-02 18:19:30 -08001522 tctl |= E1000_TCTL_PSP | E1000_TCTL_RTLC |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 (E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT);
1524
Jeff Kirsher7e6c9862006-03-02 18:19:30 -08001525#ifdef DISABLE_MULR
1526 /* disable Multiple Reads for debugging */
1527 tctl &= ~E1000_TCTL_MULR;
1528#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
Mallikarjuna R Chilakala2ae76d92005-10-04 07:04:22 -04001530 if (hw->mac_type == e1000_82571 || hw->mac_type == e1000_82572) {
1531 tarc = E1000_READ_REG(hw, TARC0);
1532 tarc |= ((1 << 25) | (1 << 21));
1533 E1000_WRITE_REG(hw, TARC0, tarc);
1534 tarc = E1000_READ_REG(hw, TARC1);
1535 tarc |= (1 << 25);
1536 if (tctl & E1000_TCTL_MULR)
1537 tarc &= ~(1 << 28);
1538 else
1539 tarc |= (1 << 28);
1540 E1000_WRITE_REG(hw, TARC1, tarc);
Jeff Kirsher87041632006-03-02 18:21:24 -08001541 } else if (hw->mac_type == e1000_80003es2lan) {
1542 tarc = E1000_READ_REG(hw, TARC0);
1543 tarc |= 1;
Jeff Kirsher87041632006-03-02 18:21:24 -08001544 E1000_WRITE_REG(hw, TARC0, tarc);
1545 tarc = E1000_READ_REG(hw, TARC1);
1546 tarc |= 1;
1547 E1000_WRITE_REG(hw, TARC1, tarc);
Mallikarjuna R Chilakala2ae76d92005-10-04 07:04:22 -04001548 }
1549
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001550 e1000_config_collision_dist(hw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551
1552 /* Setup Transmit Descriptor Settings for eop descriptor */
1553 adapter->txd_cmd = E1000_TXD_CMD_IDE | E1000_TXD_CMD_EOP |
1554 E1000_TXD_CMD_IFCS;
1555
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001556 if (hw->mac_type < e1000_82543)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 adapter->txd_cmd |= E1000_TXD_CMD_RPS;
1558 else
1559 adapter->txd_cmd |= E1000_TXD_CMD_RS;
1560
1561 /* Cache if we're 82544 running in PCI-X because we'll
1562 * need this to apply a workaround later in the send path. */
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001563 if (hw->mac_type == e1000_82544 &&
1564 hw->bus_type == e1000_bus_type_pcix)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 adapter->pcix_82544 = 1;
Jeff Kirsher7e6c9862006-03-02 18:19:30 -08001566
1567 E1000_WRITE_REG(hw, TCTL, tctl);
1568
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569}
1570
1571/**
1572 * e1000_setup_rx_resources - allocate Rx resources (Descriptors)
1573 * @adapter: board private structure
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001574 * @rxdr: rx descriptor ring (for a specific queue) to setup
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 *
1576 * Returns 0 on success, negative on failure
1577 **/
1578
Adrian Bunk3ad2cc62005-10-30 16:53:34 +01001579static int
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001580e1000_setup_rx_resources(struct e1000_adapter *adapter,
1581 struct e1000_rx_ring *rxdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 struct pci_dev *pdev = adapter->pdev;
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001584 int size, desc_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
1586 size = sizeof(struct e1000_buffer) * rxdr->count;
Auke Kokcd94dd02006-06-27 09:08:22 -07001587 rxdr->buffer_info = vmalloc(size);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001588 if (!rxdr->buffer_info) {
Malli Chilakala26483452005-04-28 19:44:46 -07001589 DPRINTK(PROBE, ERR,
1590 "Unable to allocate memory for the receive descriptor ring\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 return -ENOMEM;
1592 }
1593 memset(rxdr->buffer_info, 0, size);
1594
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001595 size = sizeof(struct e1000_ps_page) * rxdr->count;
1596 rxdr->ps_page = kmalloc(size, GFP_KERNEL);
Jesse Brandeburg96838a42006-01-18 13:01:39 -08001597 if (!rxdr->ps_page) {
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001598 vfree(rxdr->buffer_info);
1599 DPRINTK(PROBE, ERR,
1600 "Unable to allocate memory for the receive descriptor ring\n");
1601 return -ENOMEM;
1602 }
1603 memset(rxdr->ps_page, 0, size);
1604
1605 size = sizeof(struct e1000_ps_page_dma) * rxdr->count;
1606 rxdr->ps_page_dma = kmalloc(size, GFP_KERNEL);
Jesse Brandeburg96838a42006-01-18 13:01:39 -08001607 if (!rxdr->ps_page_dma) {
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001608 vfree(rxdr->buffer_info);
1609 kfree(rxdr->ps_page);
1610 DPRINTK(PROBE, ERR,
1611 "Unable to allocate memory for the receive descriptor ring\n");
1612 return -ENOMEM;
1613 }
1614 memset(rxdr->ps_page_dma, 0, size);
1615
Jesse Brandeburg96838a42006-01-18 13:01:39 -08001616 if (adapter->hw.mac_type <= e1000_82547_rev_2)
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001617 desc_len = sizeof(struct e1000_rx_desc);
1618 else
1619 desc_len = sizeof(union e1000_rx_desc_packet_split);
1620
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 /* Round up to nearest 4K */
1622
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001623 rxdr->size = rxdr->count * desc_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 E1000_ROUNDUP(rxdr->size, 4096);
1625
1626 rxdr->desc = pci_alloc_consistent(pdev, rxdr->size, &rxdr->dma);
1627
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001628 if (!rxdr->desc) {
1629 DPRINTK(PROBE, ERR,
1630 "Unable to allocate memory for the receive descriptor ring\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631setup_rx_desc_die:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 vfree(rxdr->buffer_info);
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001633 kfree(rxdr->ps_page);
1634 kfree(rxdr->ps_page_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 return -ENOMEM;
1636 }
1637
Malli Chilakala26483452005-04-28 19:44:46 -07001638 /* Fix for errata 23, can't cross 64kB boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 if (!e1000_check_64k_bound(adapter, rxdr->desc, rxdr->size)) {
1640 void *olddesc = rxdr->desc;
1641 dma_addr_t olddma = rxdr->dma;
Malli Chilakala26483452005-04-28 19:44:46 -07001642 DPRINTK(RX_ERR, ERR, "rxdr align check failed: %u bytes "
1643 "at %p\n", rxdr->size, rxdr->desc);
1644 /* Try again, without freeing the previous */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 rxdr->desc = pci_alloc_consistent(pdev, rxdr->size, &rxdr->dma);
Malli Chilakala26483452005-04-28 19:44:46 -07001646 /* Failed allocation, critical failure */
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001647 if (!rxdr->desc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 pci_free_consistent(pdev, rxdr->size, olddesc, olddma);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001649 DPRINTK(PROBE, ERR,
1650 "Unable to allocate memory "
1651 "for the receive descriptor ring\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 goto setup_rx_desc_die;
1653 }
1654
1655 if (!e1000_check_64k_bound(adapter, rxdr->desc, rxdr->size)) {
1656 /* give up */
Malli Chilakala26483452005-04-28 19:44:46 -07001657 pci_free_consistent(pdev, rxdr->size, rxdr->desc,
1658 rxdr->dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 pci_free_consistent(pdev, rxdr->size, olddesc, olddma);
Malli Chilakala26483452005-04-28 19:44:46 -07001660 DPRINTK(PROBE, ERR,
1661 "Unable to allocate aligned memory "
1662 "for the receive descriptor ring\n");
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001663 goto setup_rx_desc_die;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 } else {
Malli Chilakala26483452005-04-28 19:44:46 -07001665 /* Free old allocation, new allocation was successful */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 pci_free_consistent(pdev, rxdr->size, olddesc, olddma);
1667 }
1668 }
1669 memset(rxdr->desc, 0, rxdr->size);
1670
1671 rxdr->next_to_clean = 0;
1672 rxdr->next_to_use = 0;
1673
1674 return 0;
1675}
1676
1677/**
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001678 * e1000_setup_all_rx_resources - wrapper to allocate Rx resources
1679 * (Descriptors) for all queues
1680 * @adapter: board private structure
1681 *
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001682 * Return 0 on success, negative on failure
1683 **/
1684
1685int
1686e1000_setup_all_rx_resources(struct e1000_adapter *adapter)
1687{
1688 int i, err = 0;
1689
Jeff Kirsherf56799e2006-01-12 16:50:39 -08001690 for (i = 0; i < adapter->num_rx_queues; i++) {
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001691 err = e1000_setup_rx_resources(adapter, &adapter->rx_ring[i]);
1692 if (err) {
1693 DPRINTK(PROBE, ERR,
1694 "Allocation for Rx Queue %u failed\n", i);
Vasily Averin3fbbc722006-08-28 14:56:24 -07001695 for (i-- ; i >= 0; i--)
1696 e1000_free_rx_resources(adapter,
1697 &adapter->rx_ring[i]);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001698 break;
1699 }
1700 }
1701
1702 return err;
1703}
1704
1705/**
Malli Chilakala26483452005-04-28 19:44:46 -07001706 * e1000_setup_rctl - configure the receive control registers
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 * @adapter: Board private structure
1708 **/
Mallikarjuna R Chilakalae4c811c2005-10-04 07:05:44 -04001709#define PAGE_USE_COUNT(S) (((S) >> PAGE_SHIFT) + \
1710 (((S) & (PAGE_SIZE - 1)) ? 1 : 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711static void
1712e1000_setup_rctl(struct e1000_adapter *adapter)
1713{
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001714 uint32_t rctl, rfctl;
1715 uint32_t psrctl = 0;
Jesse Brandeburg35ec56b2006-01-18 13:01:30 -08001716#ifndef CONFIG_E1000_DISABLE_PACKET_SPLIT
Mallikarjuna R Chilakalae4c811c2005-10-04 07:05:44 -04001717 uint32_t pages = 0;
1718#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
1720 rctl = E1000_READ_REG(&adapter->hw, RCTL);
1721
1722 rctl &= ~(3 << E1000_RCTL_MO_SHIFT);
1723
1724 rctl |= E1000_RCTL_EN | E1000_RCTL_BAM |
1725 E1000_RCTL_LBM_NO | E1000_RCTL_RDMTS_HALF |
1726 (adapter->hw.mc_filter_type << E1000_RCTL_MO_SHIFT);
1727
Jeff Kirsher0fadb052006-01-12 16:51:05 -08001728 if (adapter->hw.tbi_compatibility_on == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 rctl |= E1000_RCTL_SBP;
1730 else
1731 rctl &= ~E1000_RCTL_SBP;
1732
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001733 if (adapter->netdev->mtu <= ETH_DATA_LEN)
1734 rctl &= ~E1000_RCTL_LPE;
1735 else
1736 rctl |= E1000_RCTL_LPE;
1737
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 /* Setup buffer sizes */
Auke Kok9e2feac2006-04-14 19:05:18 -07001739 rctl &= ~E1000_RCTL_SZ_4096;
1740 rctl |= E1000_RCTL_BSEX;
1741 switch (adapter->rx_buffer_len) {
1742 case E1000_RXBUFFER_256:
1743 rctl |= E1000_RCTL_SZ_256;
1744 rctl &= ~E1000_RCTL_BSEX;
1745 break;
1746 case E1000_RXBUFFER_512:
1747 rctl |= E1000_RCTL_SZ_512;
1748 rctl &= ~E1000_RCTL_BSEX;
1749 break;
1750 case E1000_RXBUFFER_1024:
1751 rctl |= E1000_RCTL_SZ_1024;
1752 rctl &= ~E1000_RCTL_BSEX;
1753 break;
Jeff Kirshera1415ee2006-02-28 20:24:07 -08001754 case E1000_RXBUFFER_2048:
1755 default:
1756 rctl |= E1000_RCTL_SZ_2048;
1757 rctl &= ~E1000_RCTL_BSEX;
1758 break;
1759 case E1000_RXBUFFER_4096:
1760 rctl |= E1000_RCTL_SZ_4096;
1761 break;
1762 case E1000_RXBUFFER_8192:
1763 rctl |= E1000_RCTL_SZ_8192;
1764 break;
1765 case E1000_RXBUFFER_16384:
1766 rctl |= E1000_RCTL_SZ_16384;
1767 break;
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001768 }
1769
Jesse Brandeburg35ec56b2006-01-18 13:01:30 -08001770#ifndef CONFIG_E1000_DISABLE_PACKET_SPLIT
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001771 /* 82571 and greater support packet-split where the protocol
1772 * header is placed in skb->data and the packet data is
1773 * placed in pages hanging off of skb_shinfo(skb)->nr_frags.
1774 * In the case of a non-split, skb->data is linearly filled,
1775 * followed by the page buffers. Therefore, skb->data is
1776 * sized to hold the largest protocol header.
1777 */
Mallikarjuna R Chilakalae4c811c2005-10-04 07:05:44 -04001778 pages = PAGE_USE_COUNT(adapter->netdev->mtu);
1779 if ((adapter->hw.mac_type > e1000_82547_rev_2) && (pages <= 3) &&
1780 PAGE_SIZE <= 16384)
1781 adapter->rx_ps_pages = pages;
1782 else
1783 adapter->rx_ps_pages = 0;
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001784#endif
Mallikarjuna R Chilakalae4c811c2005-10-04 07:05:44 -04001785 if (adapter->rx_ps_pages) {
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001786 /* Configure extra packet-split registers */
1787 rfctl = E1000_READ_REG(&adapter->hw, RFCTL);
1788 rfctl |= E1000_RFCTL_EXTEN;
1789 /* disable IPv6 packet split support */
1790 rfctl |= E1000_RFCTL_IPV6_DIS;
1791 E1000_WRITE_REG(&adapter->hw, RFCTL, rfctl);
1792
Auke Kok7dfee0c2006-06-27 09:07:50 -07001793 rctl |= E1000_RCTL_DTYP_PS;
Jesse Brandeburg96838a42006-01-18 13:01:39 -08001794
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001795 psrctl |= adapter->rx_ps_bsize0 >>
1796 E1000_PSRCTL_BSIZE0_SHIFT;
Mallikarjuna R Chilakalae4c811c2005-10-04 07:05:44 -04001797
1798 switch (adapter->rx_ps_pages) {
1799 case 3:
1800 psrctl |= PAGE_SIZE <<
1801 E1000_PSRCTL_BSIZE3_SHIFT;
1802 case 2:
1803 psrctl |= PAGE_SIZE <<
1804 E1000_PSRCTL_BSIZE2_SHIFT;
1805 case 1:
1806 psrctl |= PAGE_SIZE >>
1807 E1000_PSRCTL_BSIZE1_SHIFT;
1808 break;
1809 }
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001810
1811 E1000_WRITE_REG(&adapter->hw, PSRCTL, psrctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 }
1813
1814 E1000_WRITE_REG(&adapter->hw, RCTL, rctl);
1815}
1816
1817/**
1818 * e1000_configure_rx - Configure 8254x Receive Unit after Reset
1819 * @adapter: board private structure
1820 *
1821 * Configure the Rx unit of the MAC after a reset.
1822 **/
1823
1824static void
1825e1000_configure_rx(struct e1000_adapter *adapter)
1826{
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001827 uint64_t rdba;
1828 struct e1000_hw *hw = &adapter->hw;
1829 uint32_t rdlen, rctl, rxcsum, ctrl_ext;
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001830
Mallikarjuna R Chilakalae4c811c2005-10-04 07:05:44 -04001831 if (adapter->rx_ps_pages) {
Jeff Kirsher0f15a8f2006-03-02 18:46:29 -08001832 /* this is a 32 byte descriptor */
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001833 rdlen = adapter->rx_ring[0].count *
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001834 sizeof(union e1000_rx_desc_packet_split);
1835 adapter->clean_rx = e1000_clean_rx_irq_ps;
1836 adapter->alloc_rx_buf = e1000_alloc_rx_buffers_ps;
1837 } else {
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001838 rdlen = adapter->rx_ring[0].count *
1839 sizeof(struct e1000_rx_desc);
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001840 adapter->clean_rx = e1000_clean_rx_irq;
1841 adapter->alloc_rx_buf = e1000_alloc_rx_buffers;
1842 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843
1844 /* disable receives while setting up the descriptors */
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001845 rctl = E1000_READ_REG(hw, RCTL);
1846 E1000_WRITE_REG(hw, RCTL, rctl & ~E1000_RCTL_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847
1848 /* set the Receive Delay Timer Register */
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001849 E1000_WRITE_REG(hw, RDTR, adapter->rx_int_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001851 if (hw->mac_type >= e1000_82540) {
1852 E1000_WRITE_REG(hw, RADV, adapter->rx_abs_int_delay);
Jesse Brandeburg96838a42006-01-18 13:01:39 -08001853 if (adapter->itr > 1)
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001854 E1000_WRITE_REG(hw, ITR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 1000000000 / (adapter->itr * 256));
1856 }
1857
Mallikarjuna R Chilakala2ae76d92005-10-04 07:04:22 -04001858 if (hw->mac_type >= e1000_82571) {
Mallikarjuna R Chilakala2ae76d92005-10-04 07:04:22 -04001859 ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
Jeff Kirsher1e613fd2006-01-12 16:51:16 -08001860 /* Reset delay timers after every interrupt */
Auke Kok6fc7a7e2006-04-14 19:05:12 -07001861 ctrl_ext |= E1000_CTRL_EXT_INT_TIMER_CLR;
Jeff Kirsher1e613fd2006-01-12 16:51:16 -08001862#ifdef CONFIG_E1000_NAPI
1863 /* Auto-Mask interrupts upon ICR read. */
1864 ctrl_ext |= E1000_CTRL_EXT_IAME;
1865#endif
Mallikarjuna R Chilakala2ae76d92005-10-04 07:04:22 -04001866 E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
Jeff Kirsher1e613fd2006-01-12 16:51:16 -08001867 E1000_WRITE_REG(hw, IAM, ~0);
Mallikarjuna R Chilakala2ae76d92005-10-04 07:04:22 -04001868 E1000_WRITE_FLUSH(hw);
1869 }
1870
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001871 /* Setup the HW Rx Head and Tail Descriptor Pointers and
1872 * the Base and Length of the Rx Descriptor Ring */
Jeff Kirsherf56799e2006-01-12 16:50:39 -08001873 switch (adapter->num_rx_queues) {
Mallikarjuna R Chilakala24025e4ec2005-10-04 07:03:23 -04001874 case 1:
1875 default:
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001876 rdba = adapter->rx_ring[0].dma;
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001877 E1000_WRITE_REG(hw, RDLEN, rdlen);
Auke Kok4ca213a2006-06-27 09:07:08 -07001878 E1000_WRITE_REG(hw, RDBAH, (rdba >> 32));
1879 E1000_WRITE_REG(hw, RDBAL, (rdba & 0x00000000ffffffffULL));
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001880 E1000_WRITE_REG(hw, RDT, 0);
Auke Kok4ca213a2006-06-27 09:07:08 -07001881 E1000_WRITE_REG(hw, RDH, 0);
Auke Kok6a951692006-09-11 14:00:21 -07001882 adapter->rx_ring[0].rdh = ((hw->mac_type >= e1000_82543) ? E1000_RDH : E1000_82542_RDH);
1883 adapter->rx_ring[0].rdt = ((hw->mac_type >= e1000_82543) ? E1000_RDT : E1000_82542_RDT);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001884 break;
Mallikarjuna R Chilakala24025e4ec2005-10-04 07:03:23 -04001885 }
1886
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 /* Enable 82543 Receive Checksum Offload for TCP and UDP */
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001888 if (hw->mac_type >= e1000_82543) {
1889 rxcsum = E1000_READ_REG(hw, RXCSUM);
Jesse Brandeburg96838a42006-01-18 13:01:39 -08001890 if (adapter->rx_csum == TRUE) {
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001891 rxcsum |= E1000_RXCSUM_TUOFL;
1892
Mallikarjuna R Chilakala868d5302005-10-04 06:58:59 -04001893 /* Enable 82571 IPv4 payload checksum for UDP fragments
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001894 * Must be used in conjunction with packet-split. */
Jesse Brandeburg96838a42006-01-18 13:01:39 -08001895 if ((hw->mac_type >= e1000_82571) &&
1896 (adapter->rx_ps_pages)) {
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001897 rxcsum |= E1000_RXCSUM_IPPCSE;
1898 }
1899 } else {
1900 rxcsum &= ~E1000_RXCSUM_TUOFL;
1901 /* don't need to clear IPPCSE as it defaults to 0 */
1902 }
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001903 E1000_WRITE_REG(hw, RXCSUM, rxcsum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 }
1905
1906 /* Enable Receives */
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001907 E1000_WRITE_REG(hw, RCTL, rctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908}
1909
1910/**
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001911 * e1000_free_tx_resources - Free Tx Resources per Queue
1912 * @adapter: board private structure
1913 * @tx_ring: Tx descriptor ring for a specific queue
1914 *
1915 * Free all transmit software resources
1916 **/
1917
Adrian Bunk3ad2cc62005-10-30 16:53:34 +01001918static void
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001919e1000_free_tx_resources(struct e1000_adapter *adapter,
1920 struct e1000_tx_ring *tx_ring)
1921{
1922 struct pci_dev *pdev = adapter->pdev;
1923
1924 e1000_clean_tx_ring(adapter, tx_ring);
1925
1926 vfree(tx_ring->buffer_info);
1927 tx_ring->buffer_info = NULL;
1928
1929 pci_free_consistent(pdev, tx_ring->size, tx_ring->desc, tx_ring->dma);
1930
1931 tx_ring->desc = NULL;
1932}
1933
1934/**
1935 * e1000_free_all_tx_resources - Free Tx Resources for All Queues
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 * @adapter: board private structure
1937 *
1938 * Free all transmit software resources
1939 **/
1940
1941void
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001942e1000_free_all_tx_resources(struct e1000_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943{
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001944 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
Jeff Kirsherf56799e2006-01-12 16:50:39 -08001946 for (i = 0; i < adapter->num_tx_queues; i++)
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001947 e1000_free_tx_resources(adapter, &adapter->tx_ring[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948}
1949
Auke Koke619d522006-04-14 19:04:52 -07001950static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951e1000_unmap_and_free_tx_resource(struct e1000_adapter *adapter,
1952 struct e1000_buffer *buffer_info)
1953{
Jesse Brandeburg96838a42006-01-18 13:01:39 -08001954 if (buffer_info->dma) {
Malli Chilakala26483452005-04-28 19:44:46 -07001955 pci_unmap_page(adapter->pdev,
1956 buffer_info->dma,
1957 buffer_info->length,
1958 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 }
Jeff Kirsher8241e352006-01-12 16:51:34 -08001960 if (buffer_info->skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 dev_kfree_skb_any(buffer_info->skb);
Jeff Kirsher8241e352006-01-12 16:51:34 -08001962 memset(buffer_info, 0, sizeof(struct e1000_buffer));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963}
1964
1965/**
1966 * e1000_clean_tx_ring - Free Tx Buffers
1967 * @adapter: board private structure
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001968 * @tx_ring: ring to be cleaned
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 **/
1970
1971static void
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001972e1000_clean_tx_ring(struct e1000_adapter *adapter,
1973 struct e1000_tx_ring *tx_ring)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 struct e1000_buffer *buffer_info;
1976 unsigned long size;
1977 unsigned int i;
1978
1979 /* Free all the Tx ring sk_buffs */
1980
Jesse Brandeburg96838a42006-01-18 13:01:39 -08001981 for (i = 0; i < tx_ring->count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 buffer_info = &tx_ring->buffer_info[i];
1983 e1000_unmap_and_free_tx_resource(adapter, buffer_info);
1984 }
1985
1986 size = sizeof(struct e1000_buffer) * tx_ring->count;
1987 memset(tx_ring->buffer_info, 0, size);
1988
1989 /* Zero out the descriptor ring */
1990
1991 memset(tx_ring->desc, 0, tx_ring->size);
1992
1993 tx_ring->next_to_use = 0;
1994 tx_ring->next_to_clean = 0;
Jeff Kirsherfd803242005-12-13 00:06:22 -05001995 tx_ring->last_tx_tso = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001997 writel(0, adapter->hw.hw_addr + tx_ring->tdh);
1998 writel(0, adapter->hw.hw_addr + tx_ring->tdt);
1999}
2000
2001/**
2002 * e1000_clean_all_tx_rings - Free Tx Buffers for all queues
2003 * @adapter: board private structure
2004 **/
2005
2006static void
2007e1000_clean_all_tx_rings(struct e1000_adapter *adapter)
2008{
2009 int i;
2010
Jeff Kirsherf56799e2006-01-12 16:50:39 -08002011 for (i = 0; i < adapter->num_tx_queues; i++)
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002012 e1000_clean_tx_ring(adapter, &adapter->tx_ring[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013}
2014
2015/**
2016 * e1000_free_rx_resources - Free Rx Resources
2017 * @adapter: board private structure
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002018 * @rx_ring: ring to clean the resources from
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 *
2020 * Free all receive software resources
2021 **/
2022
Adrian Bunk3ad2cc62005-10-30 16:53:34 +01002023static void
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002024e1000_free_rx_resources(struct e1000_adapter *adapter,
2025 struct e1000_rx_ring *rx_ring)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 struct pci_dev *pdev = adapter->pdev;
2028
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002029 e1000_clean_rx_ring(adapter, rx_ring);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030
2031 vfree(rx_ring->buffer_info);
2032 rx_ring->buffer_info = NULL;
Malli Chilakala2d7edb92005-04-28 19:43:52 -07002033 kfree(rx_ring->ps_page);
2034 rx_ring->ps_page = NULL;
2035 kfree(rx_ring->ps_page_dma);
2036 rx_ring->ps_page_dma = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037
2038 pci_free_consistent(pdev, rx_ring->size, rx_ring->desc, rx_ring->dma);
2039
2040 rx_ring->desc = NULL;
2041}
2042
2043/**
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002044 * e1000_free_all_rx_resources - Free Rx Resources for All Queues
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 * @adapter: board private structure
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002046 *
2047 * Free all receive software resources
2048 **/
2049
2050void
2051e1000_free_all_rx_resources(struct e1000_adapter *adapter)
2052{
2053 int i;
2054
Jeff Kirsherf56799e2006-01-12 16:50:39 -08002055 for (i = 0; i < adapter->num_rx_queues; i++)
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002056 e1000_free_rx_resources(adapter, &adapter->rx_ring[i]);
2057}
2058
2059/**
2060 * e1000_clean_rx_ring - Free Rx Buffers per Queue
2061 * @adapter: board private structure
2062 * @rx_ring: ring to free buffers from
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 **/
2064
2065static void
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002066e1000_clean_rx_ring(struct e1000_adapter *adapter,
2067 struct e1000_rx_ring *rx_ring)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 struct e1000_buffer *buffer_info;
Malli Chilakala2d7edb92005-04-28 19:43:52 -07002070 struct e1000_ps_page *ps_page;
2071 struct e1000_ps_page_dma *ps_page_dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 struct pci_dev *pdev = adapter->pdev;
2073 unsigned long size;
Malli Chilakala2d7edb92005-04-28 19:43:52 -07002074 unsigned int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075
2076 /* Free all the Rx ring sk_buffs */
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002077 for (i = 0; i < rx_ring->count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 buffer_info = &rx_ring->buffer_info[i];
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002079 if (buffer_info->skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 pci_unmap_single(pdev,
2081 buffer_info->dma,
2082 buffer_info->length,
2083 PCI_DMA_FROMDEVICE);
2084
2085 dev_kfree_skb(buffer_info->skb);
2086 buffer_info->skb = NULL;
Jeff Kirsher997f5cb2006-01-12 16:50:55 -08002087 }
2088 ps_page = &rx_ring->ps_page[i];
2089 ps_page_dma = &rx_ring->ps_page_dma[i];
2090 for (j = 0; j < adapter->rx_ps_pages; j++) {
2091 if (!ps_page->ps_page[j]) break;
2092 pci_unmap_page(pdev,
2093 ps_page_dma->ps_page_dma[j],
2094 PAGE_SIZE, PCI_DMA_FROMDEVICE);
2095 ps_page_dma->ps_page_dma[j] = 0;
2096 put_page(ps_page->ps_page[j]);
2097 ps_page->ps_page[j] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 }
2099 }
2100
2101 size = sizeof(struct e1000_buffer) * rx_ring->count;
2102 memset(rx_ring->buffer_info, 0, size);
Malli Chilakala2d7edb92005-04-28 19:43:52 -07002103 size = sizeof(struct e1000_ps_page) * rx_ring->count;
2104 memset(rx_ring->ps_page, 0, size);
2105 size = sizeof(struct e1000_ps_page_dma) * rx_ring->count;
2106 memset(rx_ring->ps_page_dma, 0, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107
2108 /* Zero out the descriptor ring */
2109
2110 memset(rx_ring->desc, 0, rx_ring->size);
2111
2112 rx_ring->next_to_clean = 0;
2113 rx_ring->next_to_use = 0;
2114
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002115 writel(0, adapter->hw.hw_addr + rx_ring->rdh);
2116 writel(0, adapter->hw.hw_addr + rx_ring->rdt);
2117}
2118
2119/**
2120 * e1000_clean_all_rx_rings - Free Rx Buffers for all queues
2121 * @adapter: board private structure
2122 **/
2123
2124static void
2125e1000_clean_all_rx_rings(struct e1000_adapter *adapter)
2126{
2127 int i;
2128
Jeff Kirsherf56799e2006-01-12 16:50:39 -08002129 for (i = 0; i < adapter->num_rx_queues; i++)
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002130 e1000_clean_rx_ring(adapter, &adapter->rx_ring[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131}
2132
2133/* The 82542 2.0 (revision 2) needs to have the receive unit in reset
2134 * and memory write and invalidate disabled for certain operations
2135 */
2136static void
2137e1000_enter_82542_rst(struct e1000_adapter *adapter)
2138{
2139 struct net_device *netdev = adapter->netdev;
2140 uint32_t rctl;
2141
2142 e1000_pci_clear_mwi(&adapter->hw);
2143
2144 rctl = E1000_READ_REG(&adapter->hw, RCTL);
2145 rctl |= E1000_RCTL_RST;
2146 E1000_WRITE_REG(&adapter->hw, RCTL, rctl);
2147 E1000_WRITE_FLUSH(&adapter->hw);
2148 mdelay(5);
2149
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002150 if (netif_running(netdev))
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002151 e1000_clean_all_rx_rings(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152}
2153
2154static void
2155e1000_leave_82542_rst(struct e1000_adapter *adapter)
2156{
2157 struct net_device *netdev = adapter->netdev;
2158 uint32_t rctl;
2159
2160 rctl = E1000_READ_REG(&adapter->hw, RCTL);
2161 rctl &= ~E1000_RCTL_RST;
2162 E1000_WRITE_REG(&adapter->hw, RCTL, rctl);
2163 E1000_WRITE_FLUSH(&adapter->hw);
2164 mdelay(5);
2165
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002166 if (adapter->hw.pci_cmd_word & PCI_COMMAND_INVALIDATE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 e1000_pci_set_mwi(&adapter->hw);
2168
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002169 if (netif_running(netdev)) {
Jeff Kirsher72d64a42006-01-12 16:51:19 -08002170 /* No need to loop, because 82542 supports only 1 queue */
2171 struct e1000_rx_ring *ring = &adapter->rx_ring[0];
Jesse Brandeburg7c4d3362006-01-18 13:01:45 -08002172 e1000_configure_rx(adapter);
Jeff Kirsher72d64a42006-01-12 16:51:19 -08002173 adapter->alloc_rx_buf(adapter, ring, E1000_DESC_UNUSED(ring));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 }
2175}
2176
2177/**
2178 * e1000_set_mac - Change the Ethernet Address of the NIC
2179 * @netdev: network interface device structure
2180 * @p: pointer to an address structure
2181 *
2182 * Returns 0 on success, negative on failure
2183 **/
2184
2185static int
2186e1000_set_mac(struct net_device *netdev, void *p)
2187{
Malli Chilakala60490fe2005-06-17 17:41:45 -07002188 struct e1000_adapter *adapter = netdev_priv(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 struct sockaddr *addr = p;
2190
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002191 if (!is_valid_ether_addr(addr->sa_data))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 return -EADDRNOTAVAIL;
2193
2194 /* 82542 2.0 needs to be in reset to write receive address registers */
2195
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002196 if (adapter->hw.mac_type == e1000_82542_rev2_0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 e1000_enter_82542_rst(adapter);
2198
2199 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
2200 memcpy(adapter->hw.mac_addr, addr->sa_data, netdev->addr_len);
2201
2202 e1000_rar_set(&adapter->hw, adapter->hw.mac_addr, 0);
2203
Mallikarjuna R Chilakala868d5302005-10-04 06:58:59 -04002204 /* With 82571 controllers, LAA may be overwritten (with the default)
2205 * due to controller reset from the other port. */
2206 if (adapter->hw.mac_type == e1000_82571) {
2207 /* activate the work around */
2208 adapter->hw.laa_is_present = 1;
2209
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002210 /* Hold a copy of the LAA in RAR[14] This is done so that
2211 * between the time RAR[0] gets clobbered and the time it
2212 * gets fixed (in e1000_watchdog), the actual LAA is in one
Mallikarjuna R Chilakala868d5302005-10-04 06:58:59 -04002213 * of the RARs and no incoming packets directed to this port
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002214 * are dropped. Eventaully the LAA will be in RAR[0] and
Mallikarjuna R Chilakala868d5302005-10-04 06:58:59 -04002215 * RAR[14] */
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002216 e1000_rar_set(&adapter->hw, adapter->hw.mac_addr,
Mallikarjuna R Chilakala868d5302005-10-04 06:58:59 -04002217 E1000_RAR_ENTRIES - 1);
2218 }
2219
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002220 if (adapter->hw.mac_type == e1000_82542_rev2_0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 e1000_leave_82542_rst(adapter);
2222
2223 return 0;
2224}
2225
2226/**
2227 * e1000_set_multi - Multicast and Promiscuous mode set
2228 * @netdev: network interface device structure
2229 *
2230 * The set_multi entry point is called whenever the multicast address
2231 * list or the network interface flags are updated. This routine is
2232 * responsible for configuring the hardware for proper multicast,
2233 * promiscuous mode, and all-multi behavior.
2234 **/
2235
2236static void
2237e1000_set_multi(struct net_device *netdev)
2238{
Malli Chilakala60490fe2005-06-17 17:41:45 -07002239 struct e1000_adapter *adapter = netdev_priv(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 struct e1000_hw *hw = &adapter->hw;
2241 struct dev_mc_list *mc_ptr;
2242 uint32_t rctl;
2243 uint32_t hash_value;
Mallikarjuna R Chilakala868d5302005-10-04 06:58:59 -04002244 int i, rar_entries = E1000_RAR_ENTRIES;
Auke Kokcd94dd02006-06-27 09:08:22 -07002245 int mta_reg_count = (hw->mac_type == e1000_ich8lan) ?
2246 E1000_NUM_MTA_REGISTERS_ICH8LAN :
2247 E1000_NUM_MTA_REGISTERS;
2248
2249 if (adapter->hw.mac_type == e1000_ich8lan)
2250 rar_entries = E1000_RAR_ENTRIES_ICH8LAN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251
Mallikarjuna R Chilakala868d5302005-10-04 06:58:59 -04002252 /* reserve RAR[14] for LAA over-write work-around */
2253 if (adapter->hw.mac_type == e1000_82571)
2254 rar_entries--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255
Malli Chilakala26483452005-04-28 19:44:46 -07002256 /* Check for Promiscuous and All Multicast modes */
2257
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 rctl = E1000_READ_REG(hw, RCTL);
2259
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002260 if (netdev->flags & IFF_PROMISC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 rctl |= (E1000_RCTL_UPE | E1000_RCTL_MPE);
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002262 } else if (netdev->flags & IFF_ALLMULTI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 rctl |= E1000_RCTL_MPE;
2264 rctl &= ~E1000_RCTL_UPE;
2265 } else {
2266 rctl &= ~(E1000_RCTL_UPE | E1000_RCTL_MPE);
2267 }
2268
2269 E1000_WRITE_REG(hw, RCTL, rctl);
2270
2271 /* 82542 2.0 needs to be in reset to write receive address registers */
2272
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002273 if (hw->mac_type == e1000_82542_rev2_0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 e1000_enter_82542_rst(adapter);
2275
2276 /* load the first 14 multicast address into the exact filters 1-14
2277 * RAR 0 is used for the station MAC adddress
2278 * if there are not 14 addresses, go ahead and clear the filters
Mallikarjuna R Chilakala868d5302005-10-04 06:58:59 -04002279 * -- with 82571 controllers only 0-13 entries are filled here
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 */
2281 mc_ptr = netdev->mc_list;
2282
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002283 for (i = 1; i < rar_entries; i++) {
Mallikarjuna R Chilakala868d5302005-10-04 06:58:59 -04002284 if (mc_ptr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 e1000_rar_set(hw, mc_ptr->dmi_addr, i);
2286 mc_ptr = mc_ptr->next;
2287 } else {
2288 E1000_WRITE_REG_ARRAY(hw, RA, i << 1, 0);
Auke Kok4ca213a2006-06-27 09:07:08 -07002289 E1000_WRITE_FLUSH(hw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 E1000_WRITE_REG_ARRAY(hw, RA, (i << 1) + 1, 0);
Auke Kok4ca213a2006-06-27 09:07:08 -07002291 E1000_WRITE_FLUSH(hw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 }
2293 }
2294
2295 /* clear the old settings from the multicast hash table */
2296
Auke Kokcd94dd02006-06-27 09:08:22 -07002297 for (i = 0; i < mta_reg_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 E1000_WRITE_REG_ARRAY(hw, MTA, i, 0);
Auke Kok4ca213a2006-06-27 09:07:08 -07002299 E1000_WRITE_FLUSH(hw);
2300 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301
2302 /* load any remaining addresses into the hash table */
2303
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002304 for (; mc_ptr; mc_ptr = mc_ptr->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 hash_value = e1000_hash_mc_addr(hw, mc_ptr->dmi_addr);
2306 e1000_mta_set(hw, hash_value);
2307 }
2308
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002309 if (hw->mac_type == e1000_82542_rev2_0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 e1000_leave_82542_rst(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311}
2312
2313/* Need to wait a few seconds after link up to get diagnostic information from
2314 * the phy */
2315
2316static void
2317e1000_update_phy_info(unsigned long data)
2318{
2319 struct e1000_adapter *adapter = (struct e1000_adapter *) data;
2320 e1000_phy_get_info(&adapter->hw, &adapter->phy_info);
2321}
2322
2323/**
2324 * e1000_82547_tx_fifo_stall - Timer Call-back
2325 * @data: pointer to adapter cast into an unsigned long
2326 **/
2327
2328static void
2329e1000_82547_tx_fifo_stall(unsigned long data)
2330{
2331 struct e1000_adapter *adapter = (struct e1000_adapter *) data;
2332 struct net_device *netdev = adapter->netdev;
2333 uint32_t tctl;
2334
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002335 if (atomic_read(&adapter->tx_fifo_stall)) {
2336 if ((E1000_READ_REG(&adapter->hw, TDT) ==
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 E1000_READ_REG(&adapter->hw, TDH)) &&
2338 (E1000_READ_REG(&adapter->hw, TDFT) ==
2339 E1000_READ_REG(&adapter->hw, TDFH)) &&
2340 (E1000_READ_REG(&adapter->hw, TDFTS) ==
2341 E1000_READ_REG(&adapter->hw, TDFHS))) {
2342 tctl = E1000_READ_REG(&adapter->hw, TCTL);
2343 E1000_WRITE_REG(&adapter->hw, TCTL,
2344 tctl & ~E1000_TCTL_EN);
2345 E1000_WRITE_REG(&adapter->hw, TDFT,
2346 adapter->tx_head_addr);
2347 E1000_WRITE_REG(&adapter->hw, TDFH,
2348 adapter->tx_head_addr);
2349 E1000_WRITE_REG(&adapter->hw, TDFTS,
2350 adapter->tx_head_addr);
2351 E1000_WRITE_REG(&adapter->hw, TDFHS,
2352 adapter->tx_head_addr);
2353 E1000_WRITE_REG(&adapter->hw, TCTL, tctl);
2354 E1000_WRITE_FLUSH(&adapter->hw);
2355
2356 adapter->tx_fifo_head = 0;
2357 atomic_set(&adapter->tx_fifo_stall, 0);
2358 netif_wake_queue(netdev);
2359 } else {
2360 mod_timer(&adapter->tx_fifo_stall_timer, jiffies + 1);
2361 }
2362 }
2363}
2364
2365/**
2366 * e1000_watchdog - Timer Call-back
2367 * @data: pointer to adapter cast into an unsigned long
2368 **/
2369static void
2370e1000_watchdog(unsigned long data)
2371{
2372 struct e1000_adapter *adapter = (struct e1000_adapter *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 struct net_device *netdev = adapter->netdev;
Jeff Kirsher545c67c2006-01-12 16:50:25 -08002374 struct e1000_tx_ring *txdr = adapter->tx_ring;
Jeff Kirsher7e6c9862006-03-02 18:19:30 -08002375 uint32_t link, tctl;
Auke Kokcd94dd02006-06-27 09:08:22 -07002376 int32_t ret_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377
Auke Kokcd94dd02006-06-27 09:08:22 -07002378 ret_val = e1000_check_for_link(&adapter->hw);
2379 if ((ret_val == E1000_ERR_PHY) &&
2380 (adapter->hw.phy_type == e1000_phy_igp_3) &&
2381 (E1000_READ_REG(&adapter->hw, CTRL) & E1000_PHY_CTRL_GBE_DISABLE)) {
2382 /* See e1000_kumeran_lock_loss_workaround() */
2383 DPRINTK(LINK, INFO,
2384 "Gigabit has been disabled, downgrading speed\n");
2385 }
Malli Chilakala2d7edb92005-04-28 19:43:52 -07002386 if (adapter->hw.mac_type == e1000_82573) {
2387 e1000_enable_tx_pkt_filtering(&adapter->hw);
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002388 if (adapter->mng_vlan_id != adapter->hw.mng_cookie.vlan_id)
Malli Chilakala2d7edb92005-04-28 19:43:52 -07002389 e1000_update_mng_vlan(adapter);
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002392 if ((adapter->hw.media_type == e1000_media_type_internal_serdes) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 !(E1000_READ_REG(&adapter->hw, TXCW) & E1000_TXCW_ANE))
2394 link = !adapter->hw.serdes_link_down;
2395 else
2396 link = E1000_READ_REG(&adapter->hw, STATUS) & E1000_STATUS_LU;
2397
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002398 if (link) {
2399 if (!netif_carrier_ok(netdev)) {
Auke Kokfe7fe282006-04-14 19:04:59 -07002400 boolean_t txb2b = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 e1000_get_speed_and_duplex(&adapter->hw,
2402 &adapter->link_speed,
2403 &adapter->link_duplex);
2404
2405 DPRINTK(LINK, INFO, "NIC Link is Up %d Mbps %s\n",
2406 adapter->link_speed,
2407 adapter->link_duplex == FULL_DUPLEX ?
2408 "Full Duplex" : "Half Duplex");
2409
Jeff Kirsher7e6c9862006-03-02 18:19:30 -08002410 /* tweak tx_queue_len according to speed/duplex
2411 * and adjust the timeout factor */
Jeff Kirsher66a2b0a2006-01-12 16:50:53 -08002412 netdev->tx_queue_len = adapter->tx_queue_len;
2413 adapter->tx_timeout_factor = 1;
Jeff Kirsher7e6c9862006-03-02 18:19:30 -08002414 switch (adapter->link_speed) {
2415 case SPEED_10:
Auke Kokfe7fe282006-04-14 19:04:59 -07002416 txb2b = 0;
Jeff Kirsher7e6c9862006-03-02 18:19:30 -08002417 netdev->tx_queue_len = 10;
2418 adapter->tx_timeout_factor = 8;
2419 break;
2420 case SPEED_100:
Auke Kokfe7fe282006-04-14 19:04:59 -07002421 txb2b = 0;
Jeff Kirsher7e6c9862006-03-02 18:19:30 -08002422 netdev->tx_queue_len = 100;
2423 /* maybe add some timeout factor ? */
2424 break;
2425 }
2426
Auke Kokfe7fe282006-04-14 19:04:59 -07002427 if ((adapter->hw.mac_type == e1000_82571 ||
Jeff Kirsher7e6c9862006-03-02 18:19:30 -08002428 adapter->hw.mac_type == e1000_82572) &&
Auke Kokfe7fe282006-04-14 19:04:59 -07002429 txb2b == 0) {
Jeff Kirsher7e6c9862006-03-02 18:19:30 -08002430#define SPEED_MODE_BIT (1 << 21)
2431 uint32_t tarc0;
2432 tarc0 = E1000_READ_REG(&adapter->hw, TARC0);
2433 tarc0 &= ~SPEED_MODE_BIT;
2434 E1000_WRITE_REG(&adapter->hw, TARC0, tarc0);
2435 }
2436
2437#ifdef NETIF_F_TSO
2438 /* disable TSO for pcie and 10/100 speeds, to avoid
2439 * some hardware issues */
2440 if (!adapter->tso_force &&
2441 adapter->hw.bus_type == e1000_bus_type_pci_express){
Jeff Kirsher66a2b0a2006-01-12 16:50:53 -08002442 switch (adapter->link_speed) {
2443 case SPEED_10:
Jeff Kirsher66a2b0a2006-01-12 16:50:53 -08002444 case SPEED_100:
Jeff Kirsher7e6c9862006-03-02 18:19:30 -08002445 DPRINTK(PROBE,INFO,
2446 "10/100 speed: disabling TSO\n");
2447 netdev->features &= ~NETIF_F_TSO;
2448 break;
2449 case SPEED_1000:
2450 netdev->features |= NETIF_F_TSO;
2451 break;
2452 default:
2453 /* oops */
Jeff Kirsher66a2b0a2006-01-12 16:50:53 -08002454 break;
2455 }
2456 }
Jeff Kirsher7e6c9862006-03-02 18:19:30 -08002457#endif
2458
2459 /* enable transmits in the hardware, need to do this
2460 * after setting TARC0 */
2461 tctl = E1000_READ_REG(&adapter->hw, TCTL);
2462 tctl |= E1000_TCTL_EN;
2463 E1000_WRITE_REG(&adapter->hw, TCTL, tctl);
Jeff Kirsher66a2b0a2006-01-12 16:50:53 -08002464
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 netif_carrier_on(netdev);
2466 netif_wake_queue(netdev);
2467 mod_timer(&adapter->phy_info_timer, jiffies + 2 * HZ);
2468 adapter->smartspeed = 0;
2469 }
2470 } else {
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002471 if (netif_carrier_ok(netdev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 adapter->link_speed = 0;
2473 adapter->link_duplex = 0;
2474 DPRINTK(LINK, INFO, "NIC Link is Down\n");
2475 netif_carrier_off(netdev);
2476 netif_stop_queue(netdev);
2477 mod_timer(&adapter->phy_info_timer, jiffies + 2 * HZ);
Jeff Kirsher87041632006-03-02 18:21:24 -08002478
2479 /* 80003ES2LAN workaround--
2480 * For packet buffer work-around on link down event;
2481 * disable receives in the ISR and
2482 * reset device here in the watchdog
2483 */
Auke Kok8fc897b2006-08-28 14:56:16 -07002484 if (adapter->hw.mac_type == e1000_80003es2lan)
Jeff Kirsher87041632006-03-02 18:21:24 -08002485 /* reset device */
2486 schedule_work(&adapter->reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 }
2488
2489 e1000_smartspeed(adapter);
2490 }
2491
2492 e1000_update_stats(adapter);
2493
2494 adapter->hw.tx_packet_delta = adapter->stats.tpt - adapter->tpt_old;
2495 adapter->tpt_old = adapter->stats.tpt;
2496 adapter->hw.collision_delta = adapter->stats.colc - adapter->colc_old;
2497 adapter->colc_old = adapter->stats.colc;
2498
2499 adapter->gorcl = adapter->stats.gorcl - adapter->gorcl_old;
2500 adapter->gorcl_old = adapter->stats.gorcl;
2501 adapter->gotcl = adapter->stats.gotcl - adapter->gotcl_old;
2502 adapter->gotcl_old = adapter->stats.gotcl;
2503
2504 e1000_update_adaptive(&adapter->hw);
2505
Jeff Kirsherf56799e2006-01-12 16:50:39 -08002506 if (!netif_carrier_ok(netdev)) {
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002507 if (E1000_DESC_UNUSED(txdr) + 1 < txdr->count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 /* We've lost link, so the controller stops DMA,
2509 * but we've got queued Tx work that's never going
2510 * to get done, so reset controller to flush Tx.
2511 * (Do the reset outside of interrupt context). */
Jeff Kirsher87041632006-03-02 18:21:24 -08002512 adapter->tx_timeout_count++;
2513 schedule_work(&adapter->reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 }
2515 }
2516
2517 /* Dynamic mode for Interrupt Throttle Rate (ITR) */
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002518 if (adapter->hw.mac_type >= e1000_82540 && adapter->itr == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 /* Symmetric Tx/Rx gets a reduced ITR=2000; Total
2520 * asymmetrical Tx or Rx gets ITR=8000; everyone
2521 * else is between 2000-8000. */
2522 uint32_t goc = (adapter->gotcl + adapter->gorcl) / 10000;
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002523 uint32_t dif = (adapter->gotcl > adapter->gorcl ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 adapter->gotcl - adapter->gorcl :
2525 adapter->gorcl - adapter->gotcl) / 10000;
2526 uint32_t itr = goc > 0 ? (dif * 6000 / goc + 2000) : 8000;
2527 E1000_WRITE_REG(&adapter->hw, ITR, 1000000000 / (itr * 256));
2528 }
2529
2530 /* Cause software interrupt to ensure rx ring is cleaned */
2531 E1000_WRITE_REG(&adapter->hw, ICS, E1000_ICS_RXDMT0);
2532
Malli Chilakala26483452005-04-28 19:44:46 -07002533 /* Force detection of hung controller every watchdog period */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 adapter->detect_tx_hung = TRUE;
2535
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002536 /* With 82571 controllers, LAA may be overwritten due to controller
Mallikarjuna R Chilakala868d5302005-10-04 06:58:59 -04002537 * reset from the other port. Set the appropriate LAA in RAR[0] */
2538 if (adapter->hw.mac_type == e1000_82571 && adapter->hw.laa_is_present)
2539 e1000_rar_set(&adapter->hw, adapter->hw.mac_addr, 0);
2540
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 /* Reset the timer */
2542 mod_timer(&adapter->watchdog_timer, jiffies + 2 * HZ);
2543}
2544
2545#define E1000_TX_FLAGS_CSUM 0x00000001
2546#define E1000_TX_FLAGS_VLAN 0x00000002
2547#define E1000_TX_FLAGS_TSO 0x00000004
Malli Chilakala2d7edb92005-04-28 19:43:52 -07002548#define E1000_TX_FLAGS_IPV4 0x00000008
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549#define E1000_TX_FLAGS_VLAN_MASK 0xffff0000
2550#define E1000_TX_FLAGS_VLAN_SHIFT 16
2551
Auke Koke619d522006-04-14 19:04:52 -07002552static int
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002553e1000_tso(struct e1000_adapter *adapter, struct e1000_tx_ring *tx_ring,
2554 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555{
2556#ifdef NETIF_F_TSO
2557 struct e1000_context_desc *context_desc;
Jeff Kirsher545c67c2006-01-12 16:50:25 -08002558 struct e1000_buffer *buffer_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 unsigned int i;
2560 uint32_t cmd_length = 0;
Malli Chilakala2d7edb92005-04-28 19:43:52 -07002561 uint16_t ipcse = 0, tucse, mss;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 uint8_t ipcss, ipcso, tucss, tucso, hdr_len;
2563 int err;
2564
Herbert Xu89114af2006-07-08 13:34:32 -07002565 if (skb_is_gso(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 if (skb_header_cloned(skb)) {
2567 err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2568 if (err)
2569 return err;
2570 }
2571
2572 hdr_len = ((skb->h.raw - skb->data) + (skb->h.th->doff << 2));
Herbert Xu79671682006-06-22 02:40:14 -07002573 mss = skb_shinfo(skb)->gso_size;
Alexey Dobriyan60828232006-05-23 14:52:21 -07002574 if (skb->protocol == htons(ETH_P_IP)) {
Malli Chilakala2d7edb92005-04-28 19:43:52 -07002575 skb->nh.iph->tot_len = 0;
2576 skb->nh.iph->check = 0;
2577 skb->h.th->check =
2578 ~csum_tcpudp_magic(skb->nh.iph->saddr,
2579 skb->nh.iph->daddr,
2580 0,
2581 IPPROTO_TCP,
2582 0);
2583 cmd_length = E1000_TXD_CMD_IP;
2584 ipcse = skb->h.raw - skb->data - 1;
2585#ifdef NETIF_F_TSO_IPV6
Auke Koke15fdd02006-08-16 11:28:45 -07002586 } else if (skb->protocol == htons(ETH_P_IPV6)) {
Malli Chilakala2d7edb92005-04-28 19:43:52 -07002587 skb->nh.ipv6h->payload_len = 0;
2588 skb->h.th->check =
2589 ~csum_ipv6_magic(&skb->nh.ipv6h->saddr,
2590 &skb->nh.ipv6h->daddr,
2591 0,
2592 IPPROTO_TCP,
2593 0);
2594 ipcse = 0;
2595#endif
2596 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 ipcss = skb->nh.raw - skb->data;
2598 ipcso = (void *)&(skb->nh.iph->check) - (void *)skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 tucss = skb->h.raw - skb->data;
2600 tucso = (void *)&(skb->h.th->check) - (void *)skb->data;
2601 tucse = 0;
2602
2603 cmd_length |= (E1000_TXD_CMD_DEXT | E1000_TXD_CMD_TSE |
Malli Chilakala2d7edb92005-04-28 19:43:52 -07002604 E1000_TXD_CMD_TCP | (skb->len - (hdr_len)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002606 i = tx_ring->next_to_use;
2607 context_desc = E1000_CONTEXT_DESC(*tx_ring, i);
Jeff Kirsher545c67c2006-01-12 16:50:25 -08002608 buffer_info = &tx_ring->buffer_info[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609
2610 context_desc->lower_setup.ip_fields.ipcss = ipcss;
2611 context_desc->lower_setup.ip_fields.ipcso = ipcso;
2612 context_desc->lower_setup.ip_fields.ipcse = cpu_to_le16(ipcse);
2613 context_desc->upper_setup.tcp_fields.tucss = tucss;
2614 context_desc->upper_setup.tcp_fields.tucso = tucso;
2615 context_desc->upper_setup.tcp_fields.tucse = cpu_to_le16(tucse);
2616 context_desc->tcp_seg_setup.fields.mss = cpu_to_le16(mss);
2617 context_desc->tcp_seg_setup.fields.hdr_len = hdr_len;
2618 context_desc->cmd_and_length = cpu_to_le32(cmd_length);
2619
Jeff Kirsher545c67c2006-01-12 16:50:25 -08002620 buffer_info->time_stamp = jiffies;
2621
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002622 if (++i == tx_ring->count) i = 0;
2623 tx_ring->next_to_use = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624
Jeff Kirsher8241e352006-01-12 16:51:34 -08002625 return TRUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 }
2627#endif
2628
Jeff Kirsher8241e352006-01-12 16:51:34 -08002629 return FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630}
2631
Auke Koke619d522006-04-14 19:04:52 -07002632static boolean_t
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002633e1000_tx_csum(struct e1000_adapter *adapter, struct e1000_tx_ring *tx_ring,
2634 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635{
2636 struct e1000_context_desc *context_desc;
Jeff Kirsher545c67c2006-01-12 16:50:25 -08002637 struct e1000_buffer *buffer_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 unsigned int i;
2639 uint8_t css;
2640
Patrick McHardy84fa7932006-08-29 16:44:56 -07002641 if (likely(skb->ip_summed == CHECKSUM_PARTIAL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 css = skb->h.raw - skb->data;
2643
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002644 i = tx_ring->next_to_use;
Jeff Kirsher545c67c2006-01-12 16:50:25 -08002645 buffer_info = &tx_ring->buffer_info[i];
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002646 context_desc = E1000_CONTEXT_DESC(*tx_ring, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647
2648 context_desc->upper_setup.tcp_fields.tucss = css;
2649 context_desc->upper_setup.tcp_fields.tucso = css + skb->csum;
2650 context_desc->upper_setup.tcp_fields.tucse = 0;
2651 context_desc->tcp_seg_setup.data = 0;
2652 context_desc->cmd_and_length = cpu_to_le32(E1000_TXD_CMD_DEXT);
2653
Jeff Kirsher545c67c2006-01-12 16:50:25 -08002654 buffer_info->time_stamp = jiffies;
2655
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002656 if (unlikely(++i == tx_ring->count)) i = 0;
2657 tx_ring->next_to_use = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658
2659 return TRUE;
2660 }
2661
2662 return FALSE;
2663}
2664
2665#define E1000_MAX_TXD_PWR 12
2666#define E1000_MAX_DATA_PER_TXD (1<<E1000_MAX_TXD_PWR)
2667
Auke Koke619d522006-04-14 19:04:52 -07002668static int
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002669e1000_tx_map(struct e1000_adapter *adapter, struct e1000_tx_ring *tx_ring,
2670 struct sk_buff *skb, unsigned int first, unsigned int max_per_txd,
2671 unsigned int nr_frags, unsigned int mss)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 struct e1000_buffer *buffer_info;
2674 unsigned int len = skb->len;
2675 unsigned int offset = 0, size, count = 0, i;
2676 unsigned int f;
2677 len -= skb->data_len;
2678
2679 i = tx_ring->next_to_use;
2680
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002681 while (len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 buffer_info = &tx_ring->buffer_info[i];
2683 size = min(len, max_per_txd);
2684#ifdef NETIF_F_TSO
Jeff Kirsherfd803242005-12-13 00:06:22 -05002685 /* Workaround for Controller erratum --
2686 * descriptor for non-tso packet in a linear SKB that follows a
2687 * tso gets written back prematurely before the data is fully
Jeff Kirsher0f15a8f2006-03-02 18:46:29 -08002688 * DMA'd to the controller */
Jeff Kirsherfd803242005-12-13 00:06:22 -05002689 if (!skb->data_len && tx_ring->last_tx_tso &&
Herbert Xu89114af2006-07-08 13:34:32 -07002690 !skb_is_gso(skb)) {
Jeff Kirsherfd803242005-12-13 00:06:22 -05002691 tx_ring->last_tx_tso = 0;
2692 size -= 4;
2693 }
2694
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 /* Workaround for premature desc write-backs
2696 * in TSO mode. Append 4-byte sentinel desc */
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002697 if (unlikely(mss && !nr_frags && size == len && size > 8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 size -= 4;
2699#endif
Malli Chilakala97338bd2005-04-28 19:41:46 -07002700 /* work-around for errata 10 and it applies
2701 * to all controllers in PCI-X mode
2702 * The fix is to make sure that the first descriptor of a
2703 * packet is smaller than 2048 - 16 - 16 (or 2016) bytes
2704 */
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002705 if (unlikely((adapter->hw.bus_type == e1000_bus_type_pcix) &&
Malli Chilakala97338bd2005-04-28 19:41:46 -07002706 (size > 2015) && count == 0))
2707 size = 2015;
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002708
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 /* Workaround for potential 82544 hang in PCI-X. Avoid
2710 * terminating buffers within evenly-aligned dwords. */
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002711 if (unlikely(adapter->pcix_82544 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 !((unsigned long)(skb->data + offset + size - 1) & 4) &&
2713 size > 4))
2714 size -= 4;
2715
2716 buffer_info->length = size;
2717 buffer_info->dma =
2718 pci_map_single(adapter->pdev,
2719 skb->data + offset,
2720 size,
2721 PCI_DMA_TODEVICE);
2722 buffer_info->time_stamp = jiffies;
2723
2724 len -= size;
2725 offset += size;
2726 count++;
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002727 if (unlikely(++i == tx_ring->count)) i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 }
2729
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002730 for (f = 0; f < nr_frags; f++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 struct skb_frag_struct *frag;
2732
2733 frag = &skb_shinfo(skb)->frags[f];
2734 len = frag->size;
2735 offset = frag->page_offset;
2736
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002737 while (len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 buffer_info = &tx_ring->buffer_info[i];
2739 size = min(len, max_per_txd);
2740#ifdef NETIF_F_TSO
2741 /* Workaround for premature desc write-backs
2742 * in TSO mode. Append 4-byte sentinel desc */
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002743 if (unlikely(mss && f == (nr_frags-1) && size == len && size > 8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 size -= 4;
2745#endif
2746 /* Workaround for potential 82544 hang in PCI-X.
2747 * Avoid terminating buffers within evenly-aligned
2748 * dwords. */
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002749 if (unlikely(adapter->pcix_82544 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 !((unsigned long)(frag->page+offset+size-1) & 4) &&
2751 size > 4))
2752 size -= 4;
2753
2754 buffer_info->length = size;
2755 buffer_info->dma =
2756 pci_map_page(adapter->pdev,
2757 frag->page,
2758 offset,
2759 size,
2760 PCI_DMA_TODEVICE);
2761 buffer_info->time_stamp = jiffies;
2762
2763 len -= size;
2764 offset += size;
2765 count++;
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002766 if (unlikely(++i == tx_ring->count)) i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 }
2768 }
2769
2770 i = (i == 0) ? tx_ring->count - 1 : i - 1;
2771 tx_ring->buffer_info[i].skb = skb;
2772 tx_ring->buffer_info[first].next_to_watch = i;
2773
2774 return count;
2775}
2776
Auke Koke619d522006-04-14 19:04:52 -07002777static void
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002778e1000_tx_queue(struct e1000_adapter *adapter, struct e1000_tx_ring *tx_ring,
2779 int tx_flags, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 struct e1000_tx_desc *tx_desc = NULL;
2782 struct e1000_buffer *buffer_info;
2783 uint32_t txd_upper = 0, txd_lower = E1000_TXD_CMD_IFCS;
2784 unsigned int i;
2785
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002786 if (likely(tx_flags & E1000_TX_FLAGS_TSO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 txd_lower |= E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D |
2788 E1000_TXD_CMD_TSE;
Malli Chilakala2d7edb92005-04-28 19:43:52 -07002789 txd_upper |= E1000_TXD_POPTS_TXSM << 8;
2790
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002791 if (likely(tx_flags & E1000_TX_FLAGS_IPV4))
Malli Chilakala2d7edb92005-04-28 19:43:52 -07002792 txd_upper |= E1000_TXD_POPTS_IXSM << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 }
2794
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002795 if (likely(tx_flags & E1000_TX_FLAGS_CSUM)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 txd_lower |= E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D;
2797 txd_upper |= E1000_TXD_POPTS_TXSM << 8;
2798 }
2799
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002800 if (unlikely(tx_flags & E1000_TX_FLAGS_VLAN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 txd_lower |= E1000_TXD_CMD_VLE;
2802 txd_upper |= (tx_flags & E1000_TX_FLAGS_VLAN_MASK);
2803 }
2804
2805 i = tx_ring->next_to_use;
2806
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002807 while (count--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 buffer_info = &tx_ring->buffer_info[i];
2809 tx_desc = E1000_TX_DESC(*tx_ring, i);
2810 tx_desc->buffer_addr = cpu_to_le64(buffer_info->dma);
2811 tx_desc->lower.data =
2812 cpu_to_le32(txd_lower | buffer_info->length);
2813 tx_desc->upper.data = cpu_to_le32(txd_upper);
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002814 if (unlikely(++i == tx_ring->count)) i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 }
2816
2817 tx_desc->lower.data |= cpu_to_le32(adapter->txd_cmd);
2818
2819 /* Force memory writes to complete before letting h/w
2820 * know there are new descriptors to fetch. (Only
2821 * applicable for weak-ordered memory model archs,
2822 * such as IA-64). */
2823 wmb();
2824
2825 tx_ring->next_to_use = i;
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002826 writel(i, adapter->hw.hw_addr + tx_ring->tdt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827}
2828
2829/**
2830 * 82547 workaround to avoid controller hang in half-duplex environment.
2831 * The workaround is to avoid queuing a large packet that would span
2832 * the internal Tx FIFO ring boundary by notifying the stack to resend
2833 * the packet at a later time. This gives the Tx FIFO an opportunity to
2834 * flush all packets. When that occurs, we reset the Tx FIFO pointers
2835 * to the beginning of the Tx FIFO.
2836 **/
2837
2838#define E1000_FIFO_HDR 0x10
2839#define E1000_82547_PAD_LEN 0x3E0
2840
Auke Koke619d522006-04-14 19:04:52 -07002841static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842e1000_82547_fifo_workaround(struct e1000_adapter *adapter, struct sk_buff *skb)
2843{
2844 uint32_t fifo_space = adapter->tx_fifo_size - adapter->tx_fifo_head;
2845 uint32_t skb_fifo_len = skb->len + E1000_FIFO_HDR;
2846
2847 E1000_ROUNDUP(skb_fifo_len, E1000_FIFO_HDR);
2848
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002849 if (adapter->link_duplex != HALF_DUPLEX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 goto no_fifo_stall_required;
2851
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002852 if (atomic_read(&adapter->tx_fifo_stall))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 return 1;
2854
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002855 if (skb_fifo_len >= (E1000_82547_PAD_LEN + fifo_space)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 atomic_set(&adapter->tx_fifo_stall, 1);
2857 return 1;
2858 }
2859
2860no_fifo_stall_required:
2861 adapter->tx_fifo_head += skb_fifo_len;
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002862 if (adapter->tx_fifo_head >= adapter->tx_fifo_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 adapter->tx_fifo_head -= adapter->tx_fifo_size;
2864 return 0;
2865}
2866
Malli Chilakala2d7edb92005-04-28 19:43:52 -07002867#define MINIMUM_DHCP_PACKET_SIZE 282
Auke Koke619d522006-04-14 19:04:52 -07002868static int
Malli Chilakala2d7edb92005-04-28 19:43:52 -07002869e1000_transfer_dhcp_info(struct e1000_adapter *adapter, struct sk_buff *skb)
2870{
2871 struct e1000_hw *hw = &adapter->hw;
2872 uint16_t length, offset;
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002873 if (vlan_tx_tag_present(skb)) {
2874 if (!((vlan_tx_tag_get(skb) == adapter->hw.mng_cookie.vlan_id) &&
Malli Chilakala2d7edb92005-04-28 19:43:52 -07002875 ( adapter->hw.mng_cookie.status &
2876 E1000_MNG_DHCP_COOKIE_STATUS_VLAN_SUPPORT)) )
2877 return 0;
2878 }
Jeff Kirsher20a44022006-03-02 18:18:08 -08002879 if (skb->len > MINIMUM_DHCP_PACKET_SIZE) {
Malli Chilakala2d7edb92005-04-28 19:43:52 -07002880 struct ethhdr *eth = (struct ethhdr *) skb->data;
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002881 if ((htons(ETH_P_IP) == eth->h_proto)) {
2882 const struct iphdr *ip =
Malli Chilakala2d7edb92005-04-28 19:43:52 -07002883 (struct iphdr *)((uint8_t *)skb->data+14);
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002884 if (IPPROTO_UDP == ip->protocol) {
2885 struct udphdr *udp =
2886 (struct udphdr *)((uint8_t *)ip +
Malli Chilakala2d7edb92005-04-28 19:43:52 -07002887 (ip->ihl << 2));
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002888 if (ntohs(udp->dest) == 67) {
Malli Chilakala2d7edb92005-04-28 19:43:52 -07002889 offset = (uint8_t *)udp + 8 - skb->data;
2890 length = skb->len - offset;
2891
2892 return e1000_mng_write_dhcp_info(hw,
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002893 (uint8_t *)udp + 8,
Malli Chilakala2d7edb92005-04-28 19:43:52 -07002894 length);
2895 }
2896 }
2897 }
2898 }
2899 return 0;
2900}
2901
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902#define TXD_USE_COUNT(S, X) (((S) >> (X)) + 1 )
2903static int
2904e1000_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
2905{
Malli Chilakala60490fe2005-06-17 17:41:45 -07002906 struct e1000_adapter *adapter = netdev_priv(netdev);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002907 struct e1000_tx_ring *tx_ring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 unsigned int first, max_per_txd = E1000_MAX_DATA_PER_TXD;
2909 unsigned int max_txd_pwr = E1000_MAX_TXD_PWR;
2910 unsigned int tx_flags = 0;
2911 unsigned int len = skb->len;
2912 unsigned long flags;
2913 unsigned int nr_frags = 0;
2914 unsigned int mss = 0;
2915 int count = 0;
Auke Kok76c224b2006-05-23 13:36:06 -07002916 int tso;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 unsigned int f;
2918 len -= skb->data_len;
2919
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002920 tx_ring = adapter->tx_ring;
Mallikarjuna R Chilakala24025e4ec2005-10-04 07:03:23 -04002921
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002922 if (unlikely(skb->len <= 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 dev_kfree_skb_any(skb);
2924 return NETDEV_TX_OK;
2925 }
2926
2927#ifdef NETIF_F_TSO
Herbert Xu79671682006-06-22 02:40:14 -07002928 mss = skb_shinfo(skb)->gso_size;
Auke Kok76c224b2006-05-23 13:36:06 -07002929 /* The controller does a simple calculation to
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 * make sure there is enough room in the FIFO before
2931 * initiating the DMA for each buffer. The calc is:
2932 * 4 = ceil(buffer len/mss). To make sure we don't
2933 * overrun the FIFO, adjust the max buffer len if mss
2934 * drops. */
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002935 if (mss) {
Jeff Kirsher9a3056d2006-01-12 16:50:23 -08002936 uint8_t hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 max_per_txd = min(mss << 2, max_per_txd);
2938 max_txd_pwr = fls(max_per_txd) - 1;
Jeff Kirsher9a3056d2006-01-12 16:50:23 -08002939
Jeff Kirsher9f687882006-03-02 18:20:17 -08002940 /* TSO Workaround for 82571/2/3 Controllers -- if skb->data
Jeff Kirsher9a3056d2006-01-12 16:50:23 -08002941 * points to just header, pull a few bytes of payload from
2942 * frags into skb->data */
2943 hdr_len = ((skb->h.raw - skb->data) + (skb->h.th->doff << 2));
Jeff Kirsher9f687882006-03-02 18:20:17 -08002944 if (skb->data_len && (hdr_len == (skb->len - skb->data_len))) {
2945 switch (adapter->hw.mac_type) {
2946 unsigned int pull_size;
2947 case e1000_82571:
2948 case e1000_82572:
2949 case e1000_82573:
Auke Kokcd94dd02006-06-27 09:08:22 -07002950 case e1000_ich8lan:
Jeff Kirsher9f687882006-03-02 18:20:17 -08002951 pull_size = min((unsigned int)4, skb->data_len);
2952 if (!__pskb_pull_tail(skb, pull_size)) {
Auke Koka5eafce2006-06-27 09:06:49 -07002953 DPRINTK(DRV, ERR,
Jeff Kirsher9f687882006-03-02 18:20:17 -08002954 "__pskb_pull_tail failed.\n");
2955 dev_kfree_skb_any(skb);
Jeff Garzik749dfc702006-03-11 13:35:31 -05002956 return NETDEV_TX_OK;
Jeff Kirsher9f687882006-03-02 18:20:17 -08002957 }
2958 len = skb->len - skb->data_len;
2959 break;
2960 default:
2961 /* do nothing */
2962 break;
Jeff Kirsherd74bbd32006-01-12 16:51:07 -08002963 }
Jeff Kirsher9a3056d2006-01-12 16:50:23 -08002964 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 }
2966
Jeff Kirsher9a3056d2006-01-12 16:50:23 -08002967 /* reserve a descriptor for the offload context */
Patrick McHardy84fa7932006-08-29 16:44:56 -07002968 if ((mss) || (skb->ip_summed == CHECKSUM_PARTIAL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 count++;
Malli Chilakala26483452005-04-28 19:44:46 -07002970 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971#else
Patrick McHardy84fa7932006-08-29 16:44:56 -07002972 if (skb->ip_summed == CHECKSUM_PARTIAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 count++;
2974#endif
Jeff Kirsherfd803242005-12-13 00:06:22 -05002975
2976#ifdef NETIF_F_TSO
2977 /* Controller Erratum workaround */
Herbert Xu89114af2006-07-08 13:34:32 -07002978 if (!skb->data_len && tx_ring->last_tx_tso && !skb_is_gso(skb))
Jeff Kirsherfd803242005-12-13 00:06:22 -05002979 count++;
2980#endif
2981
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 count += TXD_USE_COUNT(len, max_txd_pwr);
2983
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002984 if (adapter->pcix_82544)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 count++;
2986
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002987 /* work-around for errata 10 and it applies to all controllers
Malli Chilakala97338bd2005-04-28 19:41:46 -07002988 * in PCI-X mode, so add one more descriptor to the count
2989 */
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002990 if (unlikely((adapter->hw.bus_type == e1000_bus_type_pcix) &&
Malli Chilakala97338bd2005-04-28 19:41:46 -07002991 (len > 2015)))
2992 count++;
2993
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 nr_frags = skb_shinfo(skb)->nr_frags;
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002995 for (f = 0; f < nr_frags; f++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 count += TXD_USE_COUNT(skb_shinfo(skb)->frags[f].size,
2997 max_txd_pwr);
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002998 if (adapter->pcix_82544)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 count += nr_frags;
3000
Jeff Kirsher0f15a8f2006-03-02 18:46:29 -08003001
3002 if (adapter->hw.tx_pkt_filtering &&
3003 (adapter->hw.mac_type == e1000_82573))
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003004 e1000_transfer_dhcp_info(adapter, skb);
3005
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003006 local_irq_save(flags);
3007 if (!spin_trylock(&tx_ring->tx_lock)) {
3008 /* Collision - tell upper layer to requeue */
3009 local_irq_restore(flags);
3010 return NETDEV_TX_LOCKED;
3011 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012
3013 /* need: count + 2 desc gap to keep tail from touching
3014 * head, otherwise try next time */
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003015 if (unlikely(E1000_DESC_UNUSED(tx_ring) < count + 2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 netif_stop_queue(netdev);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003017 spin_unlock_irqrestore(&tx_ring->tx_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 return NETDEV_TX_BUSY;
3019 }
3020
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003021 if (unlikely(adapter->hw.mac_type == e1000_82547)) {
3022 if (unlikely(e1000_82547_fifo_workaround(adapter, skb))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 netif_stop_queue(netdev);
3024 mod_timer(&adapter->tx_fifo_stall_timer, jiffies);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003025 spin_unlock_irqrestore(&tx_ring->tx_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 return NETDEV_TX_BUSY;
3027 }
3028 }
3029
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003030 if (unlikely(adapter->vlgrp && vlan_tx_tag_present(skb))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 tx_flags |= E1000_TX_FLAGS_VLAN;
3032 tx_flags |= (vlan_tx_tag_get(skb) << E1000_TX_FLAGS_VLAN_SHIFT);
3033 }
3034
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003035 first = tx_ring->next_to_use;
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003036
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003037 tso = e1000_tso(adapter, tx_ring, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 if (tso < 0) {
3039 dev_kfree_skb_any(skb);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003040 spin_unlock_irqrestore(&tx_ring->tx_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 return NETDEV_TX_OK;
3042 }
3043
Jeff Kirsherfd803242005-12-13 00:06:22 -05003044 if (likely(tso)) {
3045 tx_ring->last_tx_tso = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 tx_flags |= E1000_TX_FLAGS_TSO;
Jeff Kirsherfd803242005-12-13 00:06:22 -05003047 } else if (likely(e1000_tx_csum(adapter, tx_ring, skb)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 tx_flags |= E1000_TX_FLAGS_CSUM;
3049
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003050 /* Old method was to assume IPv4 packet by default if TSO was enabled.
Mallikarjuna R Chilakala868d5302005-10-04 06:58:59 -04003051 * 82571 hardware supports TSO capabilities for IPv6 as well...
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003052 * no longer assume, we must. */
Alexey Dobriyan60828232006-05-23 14:52:21 -07003053 if (likely(skb->protocol == htons(ETH_P_IP)))
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003054 tx_flags |= E1000_TX_FLAGS_IPV4;
3055
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003056 e1000_tx_queue(adapter, tx_ring, tx_flags,
3057 e1000_tx_map(adapter, tx_ring, skb, first,
3058 max_per_txd, nr_frags, mss));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059
3060 netdev->trans_start = jiffies;
3061
3062 /* Make sure there is space in the ring for the next send. */
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003063 if (unlikely(E1000_DESC_UNUSED(tx_ring) < MAX_SKB_FRAGS + 2))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 netif_stop_queue(netdev);
3065
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003066 spin_unlock_irqrestore(&tx_ring->tx_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 return NETDEV_TX_OK;
3068}
3069
3070/**
3071 * e1000_tx_timeout - Respond to a Tx Hang
3072 * @netdev: network interface device structure
3073 **/
3074
3075static void
3076e1000_tx_timeout(struct net_device *netdev)
3077{
Malli Chilakala60490fe2005-06-17 17:41:45 -07003078 struct e1000_adapter *adapter = netdev_priv(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079
3080 /* Do the reset outside of interrupt context */
Jeff Kirsher87041632006-03-02 18:21:24 -08003081 adapter->tx_timeout_count++;
3082 schedule_work(&adapter->reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083}
3084
3085static void
Jeff Kirsher87041632006-03-02 18:21:24 -08003086e1000_reset_task(struct net_device *netdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087{
Malli Chilakala60490fe2005-06-17 17:41:45 -07003088 struct e1000_adapter *adapter = netdev_priv(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089
Auke Kok2db10a02006-06-27 09:06:28 -07003090 e1000_reinit_locked(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091}
3092
3093/**
3094 * e1000_get_stats - Get System Network Statistics
3095 * @netdev: network interface device structure
3096 *
3097 * Returns the address of the device statistics structure.
3098 * The statistics are actually updated from the timer callback.
3099 **/
3100
3101static struct net_device_stats *
3102e1000_get_stats(struct net_device *netdev)
3103{
Malli Chilakala60490fe2005-06-17 17:41:45 -07003104 struct e1000_adapter *adapter = netdev_priv(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105
Jeff Kirsher6b7660c2006-01-12 16:50:35 -08003106 /* only return the current stats */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 return &adapter->net_stats;
3108}
3109
3110/**
3111 * e1000_change_mtu - Change the Maximum Transfer Unit
3112 * @netdev: network interface device structure
3113 * @new_mtu: new value for maximum frame size
3114 *
3115 * Returns 0 on success, negative on failure
3116 **/
3117
3118static int
3119e1000_change_mtu(struct net_device *netdev, int new_mtu)
3120{
Malli Chilakala60490fe2005-06-17 17:41:45 -07003121 struct e1000_adapter *adapter = netdev_priv(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 int max_frame = new_mtu + ENET_HEADER_SIZE + ETHERNET_FCS_SIZE;
Jeff Kirsher85b22eb2006-03-02 18:20:29 -08003123 uint16_t eeprom_data = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003125 if ((max_frame < MINIMUM_ETHERNET_FRAME_SIZE) ||
3126 (max_frame > MAX_JUMBO_FRAME_SIZE)) {
3127 DPRINTK(PROBE, ERR, "Invalid MTU setting\n");
Mallikarjuna R Chilakala868d5302005-10-04 06:58:59 -04003128 return -EINVAL;
3129 }
3130
Jeff Kirsher997f5cb2006-01-12 16:50:55 -08003131 /* Adapter-specific max frame size limits. */
3132 switch (adapter->hw.mac_type) {
Auke Kok9e2feac2006-04-14 19:05:18 -07003133 case e1000_undefined ... e1000_82542_rev2_1:
Auke Kokcd94dd02006-06-27 09:08:22 -07003134 case e1000_ich8lan:
Jeff Kirsher997f5cb2006-01-12 16:50:55 -08003135 if (max_frame > MAXIMUM_ETHERNET_FRAME_SIZE) {
3136 DPRINTK(PROBE, ERR, "Jumbo Frames not supported.\n");
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003137 return -EINVAL;
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003138 }
Jeff Kirsher997f5cb2006-01-12 16:50:55 -08003139 break;
Jeff Kirsher85b22eb2006-03-02 18:20:29 -08003140 case e1000_82573:
3141 /* only enable jumbo frames if ASPM is disabled completely
3142 * this means both bits must be zero in 0x1A bits 3:2 */
3143 e1000_read_eeprom(&adapter->hw, EEPROM_INIT_3GIO_3, 1,
3144 &eeprom_data);
3145 if (eeprom_data & EEPROM_WORD1A_ASPM_MASK) {
3146 if (max_frame > MAXIMUM_ETHERNET_FRAME_SIZE) {
3147 DPRINTK(PROBE, ERR,
3148 "Jumbo Frames not supported.\n");
3149 return -EINVAL;
3150 }
3151 break;
3152 }
3153 /* fall through to get support */
Jeff Kirsher997f5cb2006-01-12 16:50:55 -08003154 case e1000_82571:
3155 case e1000_82572:
Jeff Kirsher87041632006-03-02 18:21:24 -08003156 case e1000_80003es2lan:
Jeff Kirsher997f5cb2006-01-12 16:50:55 -08003157#define MAX_STD_JUMBO_FRAME_SIZE 9234
3158 if (max_frame > MAX_STD_JUMBO_FRAME_SIZE) {
3159 DPRINTK(PROBE, ERR, "MTU > 9216 not supported.\n");
3160 return -EINVAL;
3161 }
3162 break;
3163 default:
3164 /* Capable of supporting up to MAX_JUMBO_FRAME_SIZE limit. */
3165 break;
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003166 }
3167
David S. Miller87f50322006-07-31 22:39:40 -07003168 /* NOTE: netdev_alloc_skb reserves 16 bytes, and typically NET_IP_ALIGN
Auke Kok9e2feac2006-04-14 19:05:18 -07003169 * means we reserve 2 more, this pushes us to allocate from the next
3170 * larger slab size
3171 * i.e. RXBUFFER_2048 --> size-4096 slab */
Jeff Kirsher997f5cb2006-01-12 16:50:55 -08003172
Auke Kok9e2feac2006-04-14 19:05:18 -07003173 if (max_frame <= E1000_RXBUFFER_256)
3174 adapter->rx_buffer_len = E1000_RXBUFFER_256;
3175 else if (max_frame <= E1000_RXBUFFER_512)
3176 adapter->rx_buffer_len = E1000_RXBUFFER_512;
3177 else if (max_frame <= E1000_RXBUFFER_1024)
3178 adapter->rx_buffer_len = E1000_RXBUFFER_1024;
3179 else if (max_frame <= E1000_RXBUFFER_2048)
3180 adapter->rx_buffer_len = E1000_RXBUFFER_2048;
3181 else if (max_frame <= E1000_RXBUFFER_4096)
3182 adapter->rx_buffer_len = E1000_RXBUFFER_4096;
3183 else if (max_frame <= E1000_RXBUFFER_8192)
3184 adapter->rx_buffer_len = E1000_RXBUFFER_8192;
3185 else if (max_frame <= E1000_RXBUFFER_16384)
3186 adapter->rx_buffer_len = E1000_RXBUFFER_16384;
3187
3188 /* adjust allocation if LPE protects us, and we aren't using SBP */
Auke Kok9e2feac2006-04-14 19:05:18 -07003189 if (!adapter->hw.tbi_compatibility_on &&
3190 ((max_frame == MAXIMUM_ETHERNET_FRAME_SIZE) ||
3191 (max_frame == MAXIMUM_ETHERNET_VLAN_SIZE)))
3192 adapter->rx_buffer_len = MAXIMUM_ETHERNET_VLAN_SIZE;
Jeff Kirsher997f5cb2006-01-12 16:50:55 -08003193
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003194 netdev->mtu = new_mtu;
3195
Auke Kok2db10a02006-06-27 09:06:28 -07003196 if (netif_running(netdev))
3197 e1000_reinit_locked(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 adapter->hw.max_frame_size = max_frame;
3200
3201 return 0;
3202}
3203
3204/**
3205 * e1000_update_stats - Update the board statistics counters
3206 * @adapter: board private structure
3207 **/
3208
3209void
3210e1000_update_stats(struct e1000_adapter *adapter)
3211{
3212 struct e1000_hw *hw = &adapter->hw;
Linas Vepstas282f33c2006-06-08 22:19:44 -07003213 struct pci_dev *pdev = adapter->pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 unsigned long flags;
3215 uint16_t phy_tmp;
3216
3217#define PHY_IDLE_ERROR_COUNT_MASK 0x00FF
3218
Linas Vepstas282f33c2006-06-08 22:19:44 -07003219 /*
3220 * Prevent stats update while adapter is being reset, or if the pci
3221 * connection is down.
3222 */
Auke Kok90267292006-06-08 09:30:24 -07003223 if (adapter->link_speed == 0)
3224 return;
Linas Vepstas282f33c2006-06-08 22:19:44 -07003225 if (pdev->error_state && pdev->error_state != pci_channel_io_normal)
3226 return;
Auke Kok90267292006-06-08 09:30:24 -07003227
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228 spin_lock_irqsave(&adapter->stats_lock, flags);
3229
3230 /* these counters are modified from e1000_adjust_tbi_stats,
3231 * called from the interrupt context, so they must only
3232 * be written while holding adapter->stats_lock
3233 */
3234
3235 adapter->stats.crcerrs += E1000_READ_REG(hw, CRCERRS);
3236 adapter->stats.gprc += E1000_READ_REG(hw, GPRC);
3237 adapter->stats.gorcl += E1000_READ_REG(hw, GORCL);
3238 adapter->stats.gorch += E1000_READ_REG(hw, GORCH);
3239 adapter->stats.bprc += E1000_READ_REG(hw, BPRC);
3240 adapter->stats.mprc += E1000_READ_REG(hw, MPRC);
3241 adapter->stats.roc += E1000_READ_REG(hw, ROC);
Auke Kokcd94dd02006-06-27 09:08:22 -07003242
3243 if (adapter->hw.mac_type != e1000_ich8lan) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 adapter->stats.prc64 += E1000_READ_REG(hw, PRC64);
3245 adapter->stats.prc127 += E1000_READ_REG(hw, PRC127);
3246 adapter->stats.prc255 += E1000_READ_REG(hw, PRC255);
3247 adapter->stats.prc511 += E1000_READ_REG(hw, PRC511);
3248 adapter->stats.prc1023 += E1000_READ_REG(hw, PRC1023);
3249 adapter->stats.prc1522 += E1000_READ_REG(hw, PRC1522);
Auke Kokcd94dd02006-06-27 09:08:22 -07003250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251
3252 adapter->stats.symerrs += E1000_READ_REG(hw, SYMERRS);
3253 adapter->stats.mpc += E1000_READ_REG(hw, MPC);
3254 adapter->stats.scc += E1000_READ_REG(hw, SCC);
3255 adapter->stats.ecol += E1000_READ_REG(hw, ECOL);
3256 adapter->stats.mcc += E1000_READ_REG(hw, MCC);
3257 adapter->stats.latecol += E1000_READ_REG(hw, LATECOL);
3258 adapter->stats.dc += E1000_READ_REG(hw, DC);
3259 adapter->stats.sec += E1000_READ_REG(hw, SEC);
3260 adapter->stats.rlec += E1000_READ_REG(hw, RLEC);
3261 adapter->stats.xonrxc += E1000_READ_REG(hw, XONRXC);
3262 adapter->stats.xontxc += E1000_READ_REG(hw, XONTXC);
3263 adapter->stats.xoffrxc += E1000_READ_REG(hw, XOFFRXC);
3264 adapter->stats.xofftxc += E1000_READ_REG(hw, XOFFTXC);
3265 adapter->stats.fcruc += E1000_READ_REG(hw, FCRUC);
3266 adapter->stats.gptc += E1000_READ_REG(hw, GPTC);
3267 adapter->stats.gotcl += E1000_READ_REG(hw, GOTCL);
3268 adapter->stats.gotch += E1000_READ_REG(hw, GOTCH);
3269 adapter->stats.rnbc += E1000_READ_REG(hw, RNBC);
3270 adapter->stats.ruc += E1000_READ_REG(hw, RUC);
3271 adapter->stats.rfc += E1000_READ_REG(hw, RFC);
3272 adapter->stats.rjc += E1000_READ_REG(hw, RJC);
3273 adapter->stats.torl += E1000_READ_REG(hw, TORL);
3274 adapter->stats.torh += E1000_READ_REG(hw, TORH);
3275 adapter->stats.totl += E1000_READ_REG(hw, TOTL);
3276 adapter->stats.toth += E1000_READ_REG(hw, TOTH);
3277 adapter->stats.tpr += E1000_READ_REG(hw, TPR);
Auke Kokcd94dd02006-06-27 09:08:22 -07003278
3279 if (adapter->hw.mac_type != e1000_ich8lan) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 adapter->stats.ptc64 += E1000_READ_REG(hw, PTC64);
3281 adapter->stats.ptc127 += E1000_READ_REG(hw, PTC127);
3282 adapter->stats.ptc255 += E1000_READ_REG(hw, PTC255);
3283 adapter->stats.ptc511 += E1000_READ_REG(hw, PTC511);
3284 adapter->stats.ptc1023 += E1000_READ_REG(hw, PTC1023);
3285 adapter->stats.ptc1522 += E1000_READ_REG(hw, PTC1522);
Auke Kokcd94dd02006-06-27 09:08:22 -07003286 }
3287
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 adapter->stats.mptc += E1000_READ_REG(hw, MPTC);
3289 adapter->stats.bptc += E1000_READ_REG(hw, BPTC);
3290
3291 /* used for adaptive IFS */
3292
3293 hw->tx_packet_delta = E1000_READ_REG(hw, TPT);
3294 adapter->stats.tpt += hw->tx_packet_delta;
3295 hw->collision_delta = E1000_READ_REG(hw, COLC);
3296 adapter->stats.colc += hw->collision_delta;
3297
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003298 if (hw->mac_type >= e1000_82543) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 adapter->stats.algnerrc += E1000_READ_REG(hw, ALGNERRC);
3300 adapter->stats.rxerrc += E1000_READ_REG(hw, RXERRC);
3301 adapter->stats.tncrs += E1000_READ_REG(hw, TNCRS);
3302 adapter->stats.cexterr += E1000_READ_REG(hw, CEXTERR);
3303 adapter->stats.tsctc += E1000_READ_REG(hw, TSCTC);
3304 adapter->stats.tsctfc += E1000_READ_REG(hw, TSCTFC);
3305 }
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003306 if (hw->mac_type > e1000_82547_rev_2) {
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003307 adapter->stats.iac += E1000_READ_REG(hw, IAC);
3308 adapter->stats.icrxoc += E1000_READ_REG(hw, ICRXOC);
Auke Kokcd94dd02006-06-27 09:08:22 -07003309
3310 if (adapter->hw.mac_type != e1000_ich8lan) {
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003311 adapter->stats.icrxptc += E1000_READ_REG(hw, ICRXPTC);
3312 adapter->stats.icrxatc += E1000_READ_REG(hw, ICRXATC);
3313 adapter->stats.ictxptc += E1000_READ_REG(hw, ICTXPTC);
3314 adapter->stats.ictxatc += E1000_READ_REG(hw, ICTXATC);
3315 adapter->stats.ictxqec += E1000_READ_REG(hw, ICTXQEC);
3316 adapter->stats.ictxqmtc += E1000_READ_REG(hw, ICTXQMTC);
3317 adapter->stats.icrxdmtc += E1000_READ_REG(hw, ICRXDMTC);
Auke Kokcd94dd02006-06-27 09:08:22 -07003318 }
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320
3321 /* Fill out the OS statistics structure */
3322
3323 adapter->net_stats.rx_packets = adapter->stats.gprc;
3324 adapter->net_stats.tx_packets = adapter->stats.gptc;
3325 adapter->net_stats.rx_bytes = adapter->stats.gorcl;
3326 adapter->net_stats.tx_bytes = adapter->stats.gotcl;
3327 adapter->net_stats.multicast = adapter->stats.mprc;
3328 adapter->net_stats.collisions = adapter->stats.colc;
3329
3330 /* Rx Errors */
3331
Jeff Kirsher87041632006-03-02 18:21:24 -08003332 /* RLEC on some newer hardware can be incorrect so build
3333 * our own version based on RUC and ROC */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334 adapter->net_stats.rx_errors = adapter->stats.rxerrc +
3335 adapter->stats.crcerrs + adapter->stats.algnerrc +
Jeff Kirsher87041632006-03-02 18:21:24 -08003336 adapter->stats.ruc + adapter->stats.roc +
3337 adapter->stats.cexterr;
Mitch Williams49559852006-09-27 12:53:37 -07003338 adapter->stats.rlerrc = adapter->stats.ruc + adapter->stats.roc;
3339 adapter->net_stats.rx_length_errors = adapter->stats.rlerrc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 adapter->net_stats.rx_crc_errors = adapter->stats.crcerrs;
3341 adapter->net_stats.rx_frame_errors = adapter->stats.algnerrc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 adapter->net_stats.rx_missed_errors = adapter->stats.mpc;
3343
3344 /* Tx Errors */
Mitch Williams49559852006-09-27 12:53:37 -07003345 adapter->stats.txerrc = adapter->stats.ecol + adapter->stats.latecol;
3346 adapter->net_stats.tx_errors = adapter->stats.txerrc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347 adapter->net_stats.tx_aborted_errors = adapter->stats.ecol;
3348 adapter->net_stats.tx_window_errors = adapter->stats.latecol;
3349 adapter->net_stats.tx_carrier_errors = adapter->stats.tncrs;
3350
3351 /* Tx Dropped needs to be maintained elsewhere */
3352
3353 /* Phy Stats */
3354
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003355 if (hw->media_type == e1000_media_type_copper) {
3356 if ((adapter->link_speed == SPEED_1000) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 (!e1000_read_phy_reg(hw, PHY_1000T_STATUS, &phy_tmp))) {
3358 phy_tmp &= PHY_IDLE_ERROR_COUNT_MASK;
3359 adapter->phy_stats.idle_errors += phy_tmp;
3360 }
3361
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003362 if ((hw->mac_type <= e1000_82546) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 (hw->phy_type == e1000_phy_m88) &&
3364 !e1000_read_phy_reg(hw, M88E1000_RX_ERR_CNTR, &phy_tmp))
3365 adapter->phy_stats.receive_errors += phy_tmp;
3366 }
3367
3368 spin_unlock_irqrestore(&adapter->stats_lock, flags);
3369}
3370
3371/**
3372 * e1000_intr - Interrupt Handler
3373 * @irq: interrupt number
3374 * @data: pointer to a network interface device structure
3375 * @pt_regs: CPU registers structure
3376 **/
3377
3378static irqreturn_t
3379e1000_intr(int irq, void *data, struct pt_regs *regs)
3380{
3381 struct net_device *netdev = data;
Malli Chilakala60490fe2005-06-17 17:41:45 -07003382 struct e1000_adapter *adapter = netdev_priv(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383 struct e1000_hw *hw = &adapter->hw;
Jeff Kirsher87041632006-03-02 18:21:24 -08003384 uint32_t rctl, icr = E1000_READ_REG(hw, ICR);
Jeff Kirsher1e613fd2006-01-12 16:51:16 -08003385#ifndef CONFIG_E1000_NAPI
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003386 int i;
Jeff Kirsher1e613fd2006-01-12 16:51:16 -08003387#else
3388 /* Interrupt Auto-Mask...upon reading ICR,
3389 * interrupts are masked. No need for the
3390 * IMC write, but it does mean we should
3391 * account for it ASAP. */
3392 if (likely(hw->mac_type >= e1000_82571))
3393 atomic_inc(&adapter->irq_sem);
Jeff Garzikbe2b28e2005-10-04 07:13:43 -04003394#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395
Jeff Kirsher1e613fd2006-01-12 16:51:16 -08003396 if (unlikely(!icr)) {
3397#ifdef CONFIG_E1000_NAPI
3398 if (hw->mac_type >= e1000_82571)
3399 e1000_irq_enable(adapter);
3400#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401 return IRQ_NONE; /* Not our interrupt */
Jeff Kirsher1e613fd2006-01-12 16:51:16 -08003402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003404 if (unlikely(icr & (E1000_ICR_RXSEQ | E1000_ICR_LSC))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 hw->get_link_status = 1;
Jeff Kirsher87041632006-03-02 18:21:24 -08003406 /* 80003ES2LAN workaround--
3407 * For packet buffer work-around on link down event;
3408 * disable receives here in the ISR and
3409 * reset adapter in watchdog
3410 */
3411 if (netif_carrier_ok(netdev) &&
3412 (adapter->hw.mac_type == e1000_80003es2lan)) {
3413 /* disable receives */
3414 rctl = E1000_READ_REG(hw, RCTL);
3415 E1000_WRITE_REG(hw, RCTL, rctl & ~E1000_RCTL_EN);
3416 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417 mod_timer(&adapter->watchdog_timer, jiffies);
3418 }
3419
3420#ifdef CONFIG_E1000_NAPI
Jeff Kirsher1e613fd2006-01-12 16:51:16 -08003421 if (unlikely(hw->mac_type < e1000_82571)) {
3422 atomic_inc(&adapter->irq_sem);
3423 E1000_WRITE_REG(hw, IMC, ~0);
3424 E1000_WRITE_FLUSH(hw);
3425 }
Auke Kokd3d9e482006-07-14 16:14:23 -07003426 if (likely(netif_rx_schedule_prep(netdev)))
3427 __netif_rx_schedule(netdev);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003428 else
3429 e1000_irq_enable(adapter);
Jeff Kirsherc1605eb2006-03-02 18:16:38 -08003430#else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431 /* Writing IMC and IMS is needed for 82547.
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003432 * Due to Hub Link bus being occupied, an interrupt
3433 * de-assertion message is not able to be sent.
3434 * When an interrupt assertion message is generated later,
3435 * two messages are re-ordered and sent out.
3436 * That causes APIC to think 82547 is in de-assertion
3437 * state, while 82547 is in assertion state, resulting
3438 * in dead lock. Writing IMC forces 82547 into
3439 * de-assertion state.
3440 */
3441 if (hw->mac_type == e1000_82547 || hw->mac_type == e1000_82547_rev_2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442 atomic_inc(&adapter->irq_sem);
Malli Chilakala26483452005-04-28 19:44:46 -07003443 E1000_WRITE_REG(hw, IMC, ~0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 }
3445
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003446 for (i = 0; i < E1000_MAX_INTR; i++)
3447 if (unlikely(!adapter->clean_rx(adapter, adapter->rx_ring) &
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003448 !e1000_clean_tx_irq(adapter, adapter->tx_ring)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449 break;
3450
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003451 if (hw->mac_type == e1000_82547 || hw->mac_type == e1000_82547_rev_2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 e1000_irq_enable(adapter);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003453
Jeff Kirsherc1605eb2006-03-02 18:16:38 -08003454#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455
3456 return IRQ_HANDLED;
3457}
3458
3459#ifdef CONFIG_E1000_NAPI
3460/**
3461 * e1000_clean - NAPI Rx polling callback
3462 * @adapter: board private structure
3463 **/
3464
3465static int
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003466e1000_clean(struct net_device *poll_dev, int *budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467{
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003468 struct e1000_adapter *adapter;
3469 int work_to_do = min(*budget, poll_dev->quota);
Auke Kokd3d9e482006-07-14 16:14:23 -07003470 int tx_cleaned = 0, work_done = 0;
Malli Chilakala26483452005-04-28 19:44:46 -07003471
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003472 /* Must NOT use netdev_priv macro here. */
3473 adapter = poll_dev->priv;
3474
3475 /* Keep link state information with original netdev */
Auke Kokd3d9e482006-07-14 16:14:23 -07003476 if (!netif_carrier_ok(poll_dev))
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003477 goto quit_polling;
3478
Auke Kokd3d9e482006-07-14 16:14:23 -07003479 /* e1000_clean is called per-cpu. This lock protects
3480 * tx_ring[0] from being cleaned by multiple cpus
3481 * simultaneously. A failure obtaining the lock means
3482 * tx_ring[0] is currently being cleaned anyway. */
3483 if (spin_trylock(&adapter->tx_queue_lock)) {
3484 tx_cleaned = e1000_clean_tx_irq(adapter,
3485 &adapter->tx_ring[0]);
3486 spin_unlock(&adapter->tx_queue_lock);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003487 }
3488
Auke Kokd3d9e482006-07-14 16:14:23 -07003489 adapter->clean_rx(adapter, &adapter->rx_ring[0],
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003490 &work_done, work_to_do);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491
3492 *budget -= work_done;
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003493 poll_dev->quota -= work_done;
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003494
Malli Chilakala2b028932005-06-17 17:46:06 -07003495 /* If no Tx and not enough Rx work done, exit the polling mode */
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003496 if ((!tx_cleaned && (work_done == 0)) ||
Auke Kokd3d9e482006-07-14 16:14:23 -07003497 !netif_running(poll_dev)) {
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003498quit_polling:
3499 netif_rx_complete(poll_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500 e1000_irq_enable(adapter);
3501 return 0;
3502 }
3503
3504 return 1;
3505}
3506
3507#endif
3508/**
3509 * e1000_clean_tx_irq - Reclaim resources after transmit completes
3510 * @adapter: board private structure
3511 **/
3512
3513static boolean_t
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003514e1000_clean_tx_irq(struct e1000_adapter *adapter,
3515 struct e1000_tx_ring *tx_ring)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517 struct net_device *netdev = adapter->netdev;
3518 struct e1000_tx_desc *tx_desc, *eop_desc;
3519 struct e1000_buffer *buffer_info;
3520 unsigned int i, eop;
Jeff Kirsher2a1af5d2006-03-02 18:20:43 -08003521#ifdef CONFIG_E1000_NAPI
3522 unsigned int count = 0;
3523#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524 boolean_t cleaned = FALSE;
3525
3526 i = tx_ring->next_to_clean;
3527 eop = tx_ring->buffer_info[i].next_to_watch;
3528 eop_desc = E1000_TX_DESC(*tx_ring, eop);
3529
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003530 while (eop_desc->upper.data & cpu_to_le32(E1000_TXD_STAT_DD)) {
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003531 for (cleaned = FALSE; !cleaned; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532 tx_desc = E1000_TX_DESC(*tx_ring, i);
3533 buffer_info = &tx_ring->buffer_info[i];
3534 cleaned = (i == eop);
3535
Jeff Kirsherfd803242005-12-13 00:06:22 -05003536 e1000_unmap_and_free_tx_resource(adapter, buffer_info);
Jeff Kirsher8241e352006-01-12 16:51:34 -08003537 memset(tx_desc, 0, sizeof(struct e1000_tx_desc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003539 if (unlikely(++i == tx_ring->count)) i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540 }
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003541
Jeff Kirsher7bfa4812006-01-12 16:50:41 -08003542
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543 eop = tx_ring->buffer_info[i].next_to_watch;
3544 eop_desc = E1000_TX_DESC(*tx_ring, eop);
Jeff Kirsher2a1af5d2006-03-02 18:20:43 -08003545#ifdef CONFIG_E1000_NAPI
3546#define E1000_TX_WEIGHT 64
3547 /* weight of a sort for tx, to avoid endless transmit cleanup */
3548 if (count++ == E1000_TX_WEIGHT) break;
3549#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550 }
3551
3552 tx_ring->next_to_clean = i;
3553
Auke Kok77b2aad2006-04-14 19:05:25 -07003554#define TX_WAKE_THRESHOLD 32
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003555 if (unlikely(cleaned && netif_queue_stopped(netdev) &&
Auke Kok77b2aad2006-04-14 19:05:25 -07003556 netif_carrier_ok(netdev))) {
3557 spin_lock(&tx_ring->tx_lock);
3558 if (netif_queue_stopped(netdev) &&
3559 (E1000_DESC_UNUSED(tx_ring) >= TX_WAKE_THRESHOLD))
3560 netif_wake_queue(netdev);
3561 spin_unlock(&tx_ring->tx_lock);
3562 }
Malli Chilakala26483452005-04-28 19:44:46 -07003563
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003564 if (adapter->detect_tx_hung) {
Malli Chilakala26483452005-04-28 19:44:46 -07003565 /* Detect a transmit hang in hardware, this serializes the
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566 * check with the clearing of time_stamp and movement of i */
3567 adapter->detect_tx_hung = FALSE;
Jeff Kirsher392137f2006-01-12 16:50:57 -08003568 if (tx_ring->buffer_info[eop].dma &&
3569 time_after(jiffies, tx_ring->buffer_info[eop].time_stamp +
Jeff Kirsher7e6c9862006-03-02 18:19:30 -08003570 (adapter->tx_timeout_factor * HZ))
Malli Chilakala70b8f1e2005-04-28 19:40:40 -07003571 && !(E1000_READ_REG(&adapter->hw, STATUS) &
Jeff Kirsher392137f2006-01-12 16:50:57 -08003572 E1000_STATUS_TXOFF)) {
Malli Chilakala70b8f1e2005-04-28 19:40:40 -07003573
3574 /* detected Tx unit hang */
Malli Chilakalac6963ef2005-06-17 17:42:07 -07003575 DPRINTK(DRV, ERR, "Detected Tx Unit Hang\n"
Jeff Kirsher7bfa4812006-01-12 16:50:41 -08003576 " Tx Queue <%lu>\n"
Malli Chilakala70b8f1e2005-04-28 19:40:40 -07003577 " TDH <%x>\n"
3578 " TDT <%x>\n"
3579 " next_to_use <%x>\n"
3580 " next_to_clean <%x>\n"
3581 "buffer_info[next_to_clean]\n"
Malli Chilakala70b8f1e2005-04-28 19:40:40 -07003582 " time_stamp <%lx>\n"
3583 " next_to_watch <%x>\n"
3584 " jiffies <%lx>\n"
3585 " next_to_watch.status <%x>\n",
Jeff Kirsher7bfa4812006-01-12 16:50:41 -08003586 (unsigned long)((tx_ring - adapter->tx_ring) /
3587 sizeof(struct e1000_tx_ring)),
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003588 readl(adapter->hw.hw_addr + tx_ring->tdh),
3589 readl(adapter->hw.hw_addr + tx_ring->tdt),
Malli Chilakala70b8f1e2005-04-28 19:40:40 -07003590 tx_ring->next_to_use,
Jeff Kirsher392137f2006-01-12 16:50:57 -08003591 tx_ring->next_to_clean,
3592 tx_ring->buffer_info[eop].time_stamp,
Malli Chilakala70b8f1e2005-04-28 19:40:40 -07003593 eop,
3594 jiffies,
3595 eop_desc->upper.fields.status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596 netif_stop_queue(netdev);
Malli Chilakala70b8f1e2005-04-28 19:40:40 -07003597 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599 return cleaned;
3600}
3601
3602/**
3603 * e1000_rx_checksum - Receive Checksum Offload for 82543
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003604 * @adapter: board private structure
3605 * @status_err: receive descriptor status and error fields
3606 * @csum: receive descriptor csum field
3607 * @sk_buff: socket buffer with received data
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608 **/
3609
Auke Koke619d522006-04-14 19:04:52 -07003610static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611e1000_rx_checksum(struct e1000_adapter *adapter,
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003612 uint32_t status_err, uint32_t csum,
3613 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614{
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003615 uint16_t status = (uint16_t)status_err;
3616 uint8_t errors = (uint8_t)(status_err >> 24);
3617 skb->ip_summed = CHECKSUM_NONE;
3618
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619 /* 82543 or newer only */
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003620 if (unlikely(adapter->hw.mac_type < e1000_82543)) return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621 /* Ignore Checksum bit is set */
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003622 if (unlikely(status & E1000_RXD_STAT_IXSM)) return;
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003623 /* TCP/UDP checksum error bit is set */
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003624 if (unlikely(errors & E1000_RXD_ERR_TCPE)) {
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003625 /* let the stack verify checksum errors */
3626 adapter->hw_csum_err++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627 return;
3628 }
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003629 /* TCP/UDP Checksum has not been calculated */
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003630 if (adapter->hw.mac_type <= e1000_82547_rev_2) {
3631 if (!(status & E1000_RXD_STAT_TCPCS))
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003632 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633 } else {
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003634 if (!(status & (E1000_RXD_STAT_TCPCS | E1000_RXD_STAT_UDPCS)))
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003635 return;
3636 }
3637 /* It must be a TCP or UDP packet with a valid checksum */
3638 if (likely(status & E1000_RXD_STAT_TCPCS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639 /* TCP checksum is good */
3640 skb->ip_summed = CHECKSUM_UNNECESSARY;
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003641 } else if (adapter->hw.mac_type > e1000_82547_rev_2) {
3642 /* IP fragment with UDP payload */
3643 /* Hardware complements the payload checksum, so we undo it
3644 * and then put the value in host order for further stack use.
3645 */
3646 csum = ntohl(csum ^ 0xFFFF);
3647 skb->csum = csum;
Patrick McHardy84fa7932006-08-29 16:44:56 -07003648 skb->ip_summed = CHECKSUM_COMPLETE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649 }
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003650 adapter->hw_csum_good++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651}
3652
3653/**
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003654 * e1000_clean_rx_irq - Send received data up the network stack; legacy
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655 * @adapter: board private structure
3656 **/
3657
3658static boolean_t
3659#ifdef CONFIG_E1000_NAPI
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003660e1000_clean_rx_irq(struct e1000_adapter *adapter,
3661 struct e1000_rx_ring *rx_ring,
3662 int *work_done, int work_to_do)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663#else
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003664e1000_clean_rx_irq(struct e1000_adapter *adapter,
3665 struct e1000_rx_ring *rx_ring)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666#endif
3667{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668 struct net_device *netdev = adapter->netdev;
3669 struct pci_dev *pdev = adapter->pdev;
Jesse Brandeburg86c3d592006-01-18 13:01:43 -08003670 struct e1000_rx_desc *rx_desc, *next_rxd;
3671 struct e1000_buffer *buffer_info, *next_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672 unsigned long flags;
3673 uint32_t length;
3674 uint8_t last_byte;
3675 unsigned int i;
Jeff Kirsher72d64a42006-01-12 16:51:19 -08003676 int cleaned_count = 0;
Jeff Kirshera1415ee2006-02-28 20:24:07 -08003677 boolean_t cleaned = FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678
3679 i = rx_ring->next_to_clean;
3680 rx_desc = E1000_RX_DESC(*rx_ring, i);
Jesse Brandeburgb92ff8e2006-01-18 13:01:32 -08003681 buffer_info = &rx_ring->buffer_info[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682
Jesse Brandeburgb92ff8e2006-01-18 13:01:32 -08003683 while (rx_desc->status & E1000_RXD_STAT_DD) {
Auke Kok24f476e2006-06-08 09:28:47 -07003684 struct sk_buff *skb;
Jeff Kirshera292ca62006-01-12 16:51:30 -08003685 u8 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686#ifdef CONFIG_E1000_NAPI
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003687 if (*work_done >= work_to_do)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688 break;
3689 (*work_done)++;
3690#endif
Jeff Kirshera292ca62006-01-12 16:51:30 -08003691 status = rx_desc->status;
Jesse Brandeburgb92ff8e2006-01-18 13:01:32 -08003692 skb = buffer_info->skb;
Jesse Brandeburg86c3d592006-01-18 13:01:43 -08003693 buffer_info->skb = NULL;
3694
Jeff Kirsher30320be2006-03-02 18:21:57 -08003695 prefetch(skb->data - NET_IP_ALIGN);
3696
Jesse Brandeburg86c3d592006-01-18 13:01:43 -08003697 if (++i == rx_ring->count) i = 0;
3698 next_rxd = E1000_RX_DESC(*rx_ring, i);
Jeff Kirsher30320be2006-03-02 18:21:57 -08003699 prefetch(next_rxd);
3700
Jesse Brandeburg86c3d592006-01-18 13:01:43 -08003701 next_buffer = &rx_ring->buffer_info[i];
Jesse Brandeburg86c3d592006-01-18 13:01:43 -08003702
Jeff Kirsher72d64a42006-01-12 16:51:19 -08003703 cleaned = TRUE;
3704 cleaned_count++;
Jeff Kirshera292ca62006-01-12 16:51:30 -08003705 pci_unmap_single(pdev,
3706 buffer_info->dma,
3707 buffer_info->length,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708 PCI_DMA_FROMDEVICE);
3709
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710 length = le16_to_cpu(rx_desc->length);
3711
Auke Kokf235a2a2006-07-14 16:14:34 -07003712 /* adjust length to remove Ethernet CRC */
3713 length -= 4;
3714
Jeff Kirshera1415ee2006-02-28 20:24:07 -08003715 if (unlikely(!(status & E1000_RXD_STAT_EOP))) {
3716 /* All receives must fit into a single buffer */
3717 E1000_DBG("%s: Receive packet consumed multiple"
3718 " buffers\n", netdev->name);
Auke Kok864c4e42006-06-27 09:06:53 -07003719 /* recycle */
Auke Kok8fc897b2006-08-28 14:56:16 -07003720 buffer_info->skb = skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721 goto next_desc;
3722 }
3723
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003724 if (unlikely(rx_desc->errors & E1000_RXD_ERR_FRAME_ERR_MASK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725 last_byte = *(skb->data + length - 1);
Jesse Brandeburgb92ff8e2006-01-18 13:01:32 -08003726 if (TBI_ACCEPT(&adapter->hw, status,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727 rx_desc->errors, length, last_byte)) {
3728 spin_lock_irqsave(&adapter->stats_lock, flags);
Jeff Kirshera292ca62006-01-12 16:51:30 -08003729 e1000_tbi_adjust_stats(&adapter->hw,
3730 &adapter->stats,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731 length, skb->data);
3732 spin_unlock_irqrestore(&adapter->stats_lock,
3733 flags);
3734 length--;
3735 } else {
Auke Kok9e2feac2006-04-14 19:05:18 -07003736 /* recycle */
3737 buffer_info->skb = skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738 goto next_desc;
3739 }
Auke Kok1cb58212006-04-18 12:31:04 -07003740 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741
Jeff Kirshera292ca62006-01-12 16:51:30 -08003742 /* code added for copybreak, this should improve
3743 * performance for small packets with large amounts
3744 * of reassembly being done in the stack */
3745#define E1000_CB_LENGTH 256
Jeff Kirshera1415ee2006-02-28 20:24:07 -08003746 if (length < E1000_CB_LENGTH) {
Jeff Kirshera292ca62006-01-12 16:51:30 -08003747 struct sk_buff *new_skb =
David S. Miller87f50322006-07-31 22:39:40 -07003748 netdev_alloc_skb(netdev, length + NET_IP_ALIGN);
Jeff Kirshera292ca62006-01-12 16:51:30 -08003749 if (new_skb) {
3750 skb_reserve(new_skb, NET_IP_ALIGN);
Jeff Kirshera292ca62006-01-12 16:51:30 -08003751 memcpy(new_skb->data - NET_IP_ALIGN,
3752 skb->data - NET_IP_ALIGN,
3753 length + NET_IP_ALIGN);
3754 /* save the skb in buffer_info as good */
3755 buffer_info->skb = skb;
3756 skb = new_skb;
3757 skb_put(skb, length);
3758 }
Jeff Kirshera1415ee2006-02-28 20:24:07 -08003759 } else
3760 skb_put(skb, length);
Jeff Kirshera292ca62006-01-12 16:51:30 -08003761
3762 /* end copybreak code */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763
3764 /* Receive Checksum Offload */
Jeff Kirshera292ca62006-01-12 16:51:30 -08003765 e1000_rx_checksum(adapter,
3766 (uint32_t)(status) |
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003767 ((uint32_t)(rx_desc->errors) << 24),
David S. Millerc3d7a3a2006-03-15 14:26:28 -08003768 le16_to_cpu(rx_desc->csum), skb);
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003769
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770 skb->protocol = eth_type_trans(skb, netdev);
3771#ifdef CONFIG_E1000_NAPI
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003772 if (unlikely(adapter->vlgrp &&
Jeff Kirshera292ca62006-01-12 16:51:30 -08003773 (status & E1000_RXD_STAT_VP))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774 vlan_hwaccel_receive_skb(skb, adapter->vlgrp,
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003775 le16_to_cpu(rx_desc->special) &
3776 E1000_RXD_SPC_VLAN_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777 } else {
3778 netif_receive_skb(skb);
3779 }
3780#else /* CONFIG_E1000_NAPI */
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003781 if (unlikely(adapter->vlgrp &&
Jesse Brandeburgb92ff8e2006-01-18 13:01:32 -08003782 (status & E1000_RXD_STAT_VP))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783 vlan_hwaccel_rx(skb, adapter->vlgrp,
3784 le16_to_cpu(rx_desc->special) &
3785 E1000_RXD_SPC_VLAN_MASK);
3786 } else {
3787 netif_rx(skb);
3788 }
3789#endif /* CONFIG_E1000_NAPI */
3790 netdev->last_rx = jiffies;
3791
3792next_desc:
3793 rx_desc->status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794
Jeff Kirsher72d64a42006-01-12 16:51:19 -08003795 /* return some buffers to hardware, one at a time is too slow */
3796 if (unlikely(cleaned_count >= E1000_RX_BUFFER_WRITE)) {
3797 adapter->alloc_rx_buf(adapter, rx_ring, cleaned_count);
3798 cleaned_count = 0;
3799 }
3800
Jeff Kirsher30320be2006-03-02 18:21:57 -08003801 /* use prefetched values */
Jesse Brandeburg86c3d592006-01-18 13:01:43 -08003802 rx_desc = next_rxd;
3803 buffer_info = next_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805 rx_ring->next_to_clean = i;
Jeff Kirsher72d64a42006-01-12 16:51:19 -08003806
3807 cleaned_count = E1000_DESC_UNUSED(rx_ring);
3808 if (cleaned_count)
3809 adapter->alloc_rx_buf(adapter, rx_ring, cleaned_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810
3811 return cleaned;
3812}
3813
3814/**
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003815 * e1000_clean_rx_irq_ps - Send received data up the network stack; packet split
3816 * @adapter: board private structure
3817 **/
3818
3819static boolean_t
3820#ifdef CONFIG_E1000_NAPI
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003821e1000_clean_rx_irq_ps(struct e1000_adapter *adapter,
3822 struct e1000_rx_ring *rx_ring,
3823 int *work_done, int work_to_do)
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003824#else
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003825e1000_clean_rx_irq_ps(struct e1000_adapter *adapter,
3826 struct e1000_rx_ring *rx_ring)
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003827#endif
3828{
Jesse Brandeburg86c3d592006-01-18 13:01:43 -08003829 union e1000_rx_desc_packet_split *rx_desc, *next_rxd;
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003830 struct net_device *netdev = adapter->netdev;
3831 struct pci_dev *pdev = adapter->pdev;
Jesse Brandeburg86c3d592006-01-18 13:01:43 -08003832 struct e1000_buffer *buffer_info, *next_buffer;
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003833 struct e1000_ps_page *ps_page;
3834 struct e1000_ps_page_dma *ps_page_dma;
Auke Kok24f476e2006-06-08 09:28:47 -07003835 struct sk_buff *skb;
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003836 unsigned int i, j;
3837 uint32_t length, staterr;
Jeff Kirsher72d64a42006-01-12 16:51:19 -08003838 int cleaned_count = 0;
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003839 boolean_t cleaned = FALSE;
3840
3841 i = rx_ring->next_to_clean;
3842 rx_desc = E1000_RX_DESC_PS(*rx_ring, i);
Malli Chilakala683a38f2005-06-17 17:43:25 -07003843 staterr = le32_to_cpu(rx_desc->wb.middle.status_error);
Auke Kok9e2feac2006-04-14 19:05:18 -07003844 buffer_info = &rx_ring->buffer_info[i];
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003845
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003846 while (staterr & E1000_RXD_STAT_DD) {
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003847 ps_page = &rx_ring->ps_page[i];
3848 ps_page_dma = &rx_ring->ps_page_dma[i];
3849#ifdef CONFIG_E1000_NAPI
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003850 if (unlikely(*work_done >= work_to_do))
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003851 break;
3852 (*work_done)++;
3853#endif
Jesse Brandeburg86c3d592006-01-18 13:01:43 -08003854 skb = buffer_info->skb;
3855
Jeff Kirsher30320be2006-03-02 18:21:57 -08003856 /* in the packet split case this is header only */
3857 prefetch(skb->data - NET_IP_ALIGN);
3858
Jesse Brandeburg86c3d592006-01-18 13:01:43 -08003859 if (++i == rx_ring->count) i = 0;
3860 next_rxd = E1000_RX_DESC_PS(*rx_ring, i);
Jeff Kirsher30320be2006-03-02 18:21:57 -08003861 prefetch(next_rxd);
3862
Jesse Brandeburg86c3d592006-01-18 13:01:43 -08003863 next_buffer = &rx_ring->buffer_info[i];
Jesse Brandeburg86c3d592006-01-18 13:01:43 -08003864
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003865 cleaned = TRUE;
Jeff Kirsher72d64a42006-01-12 16:51:19 -08003866 cleaned_count++;
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003867 pci_unmap_single(pdev, buffer_info->dma,
3868 buffer_info->length,
3869 PCI_DMA_FROMDEVICE);
3870
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003871 if (unlikely(!(staterr & E1000_RXD_STAT_EOP))) {
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003872 E1000_DBG("%s: Packet Split buffers didn't pick up"
3873 " the full packet\n", netdev->name);
3874 dev_kfree_skb_irq(skb);
3875 goto next_desc;
3876 }
3877
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003878 if (unlikely(staterr & E1000_RXDEXT_ERR_FRAME_ERR_MASK)) {
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003879 dev_kfree_skb_irq(skb);
3880 goto next_desc;
3881 }
3882
3883 length = le16_to_cpu(rx_desc->wb.middle.length0);
3884
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003885 if (unlikely(!length)) {
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003886 E1000_DBG("%s: Last part of the packet spanning"
3887 " multiple descriptors\n", netdev->name);
3888 dev_kfree_skb_irq(skb);
3889 goto next_desc;
3890 }
3891
3892 /* Good Receive */
3893 skb_put(skb, length);
3894
Jeff Kirsherdc7c6ad2006-03-02 18:21:40 -08003895 {
3896 /* this looks ugly, but it seems compiler issues make it
3897 more efficient than reusing j */
3898 int l1 = le16_to_cpu(rx_desc->wb.upper.length[0]);
3899
3900 /* page alloc/put takes too long and effects small packet
3901 * throughput, so unsplit small packets and save the alloc/put*/
Auke Kok9e2feac2006-04-14 19:05:18 -07003902 if (l1 && ((length + l1) <= adapter->rx_ps_bsize0)) {
Jeff Kirsherdc7c6ad2006-03-02 18:21:40 -08003903 u8 *vaddr;
Auke Kok76c224b2006-05-23 13:36:06 -07003904 /* there is no documentation about how to call
Jeff Kirsherdc7c6ad2006-03-02 18:21:40 -08003905 * kmap_atomic, so we can't hold the mapping
3906 * very long */
3907 pci_dma_sync_single_for_cpu(pdev,
3908 ps_page_dma->ps_page_dma[0],
3909 PAGE_SIZE,
3910 PCI_DMA_FROMDEVICE);
3911 vaddr = kmap_atomic(ps_page->ps_page[0],
3912 KM_SKB_DATA_SOFTIRQ);
3913 memcpy(skb->tail, vaddr, l1);
3914 kunmap_atomic(vaddr, KM_SKB_DATA_SOFTIRQ);
3915 pci_dma_sync_single_for_device(pdev,
3916 ps_page_dma->ps_page_dma[0],
3917 PAGE_SIZE, PCI_DMA_FROMDEVICE);
Auke Kokf235a2a2006-07-14 16:14:34 -07003918 /* remove the CRC */
3919 l1 -= 4;
Jeff Kirsherdc7c6ad2006-03-02 18:21:40 -08003920 skb_put(skb, l1);
Jeff Kirsherdc7c6ad2006-03-02 18:21:40 -08003921 goto copydone;
3922 } /* if */
3923 }
3924
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003925 for (j = 0; j < adapter->rx_ps_pages; j++) {
Jeff Kirsher30320be2006-03-02 18:21:57 -08003926 if (!(length= le16_to_cpu(rx_desc->wb.upper.length[j])))
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003927 break;
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003928 pci_unmap_page(pdev, ps_page_dma->ps_page_dma[j],
3929 PAGE_SIZE, PCI_DMA_FROMDEVICE);
3930 ps_page_dma->ps_page_dma[j] = 0;
Jeff Kirsher329bfd02006-03-02 18:20:02 -08003931 skb_fill_page_desc(skb, j, ps_page->ps_page[j], 0,
3932 length);
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003933 ps_page->ps_page[j] = NULL;
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003934 skb->len += length;
3935 skb->data_len += length;
Auke Kok5d51b802006-04-14 19:05:06 -07003936 skb->truesize += length;
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003937 }
3938
Auke Kokf235a2a2006-07-14 16:14:34 -07003939 /* strip the ethernet crc, problem is we're using pages now so
3940 * this whole operation can get a little cpu intensive */
3941 pskb_trim(skb, skb->len - 4);
3942
Jeff Kirsherdc7c6ad2006-03-02 18:21:40 -08003943copydone:
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003944 e1000_rx_checksum(adapter, staterr,
David S. Millerc3d7a3a2006-03-15 14:26:28 -08003945 le16_to_cpu(rx_desc->wb.lower.hi_dword.csum_ip.csum), skb);
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003946 skb->protocol = eth_type_trans(skb, netdev);
3947
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003948 if (likely(rx_desc->wb.upper.header_status &
David S. Millerc3d7a3a2006-03-15 14:26:28 -08003949 cpu_to_le16(E1000_RXDPS_HDRSTAT_HDRSP)))
Mallikarjuna R Chilakalae4c811c2005-10-04 07:05:44 -04003950 adapter->rx_hdr_split++;
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003951#ifdef CONFIG_E1000_NAPI
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003952 if (unlikely(adapter->vlgrp && (staterr & E1000_RXD_STAT_VP))) {
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003953 vlan_hwaccel_receive_skb(skb, adapter->vlgrp,
Malli Chilakala683a38f2005-06-17 17:43:25 -07003954 le16_to_cpu(rx_desc->wb.middle.vlan) &
3955 E1000_RXD_SPC_VLAN_MASK);
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003956 } else {
3957 netif_receive_skb(skb);
3958 }
3959#else /* CONFIG_E1000_NAPI */
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003960 if (unlikely(adapter->vlgrp && (staterr & E1000_RXD_STAT_VP))) {
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003961 vlan_hwaccel_rx(skb, adapter->vlgrp,
Malli Chilakala683a38f2005-06-17 17:43:25 -07003962 le16_to_cpu(rx_desc->wb.middle.vlan) &
3963 E1000_RXD_SPC_VLAN_MASK);
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003964 } else {
3965 netif_rx(skb);
3966 }
3967#endif /* CONFIG_E1000_NAPI */
3968 netdev->last_rx = jiffies;
3969
3970next_desc:
David S. Millerc3d7a3a2006-03-15 14:26:28 -08003971 rx_desc->wb.middle.status_error &= cpu_to_le32(~0xFF);
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003972 buffer_info->skb = NULL;
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003973
Jeff Kirsher72d64a42006-01-12 16:51:19 -08003974 /* return some buffers to hardware, one at a time is too slow */
3975 if (unlikely(cleaned_count >= E1000_RX_BUFFER_WRITE)) {
3976 adapter->alloc_rx_buf(adapter, rx_ring, cleaned_count);
3977 cleaned_count = 0;
3978 }
3979
Jeff Kirsher30320be2006-03-02 18:21:57 -08003980 /* use prefetched values */
Jesse Brandeburg86c3d592006-01-18 13:01:43 -08003981 rx_desc = next_rxd;
3982 buffer_info = next_buffer;
3983
Malli Chilakala683a38f2005-06-17 17:43:25 -07003984 staterr = le32_to_cpu(rx_desc->wb.middle.status_error);
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003985 }
3986 rx_ring->next_to_clean = i;
Jeff Kirsher72d64a42006-01-12 16:51:19 -08003987
3988 cleaned_count = E1000_DESC_UNUSED(rx_ring);
3989 if (cleaned_count)
3990 adapter->alloc_rx_buf(adapter, rx_ring, cleaned_count);
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003991
3992 return cleaned;
3993}
3994
3995/**
3996 * e1000_alloc_rx_buffers - Replace used receive buffers; legacy & extended
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997 * @adapter: address of board private structure
3998 **/
3999
4000static void
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04004001e1000_alloc_rx_buffers(struct e1000_adapter *adapter,
Jeff Kirsher72d64a42006-01-12 16:51:19 -08004002 struct e1000_rx_ring *rx_ring,
Jeff Kirshera292ca62006-01-12 16:51:30 -08004003 int cleaned_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005 struct net_device *netdev = adapter->netdev;
4006 struct pci_dev *pdev = adapter->pdev;
4007 struct e1000_rx_desc *rx_desc;
4008 struct e1000_buffer *buffer_info;
4009 struct sk_buff *skb;
Malli Chilakala26483452005-04-28 19:44:46 -07004010 unsigned int i;
4011 unsigned int bufsz = adapter->rx_buffer_len + NET_IP_ALIGN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012
4013 i = rx_ring->next_to_use;
4014 buffer_info = &rx_ring->buffer_info[i];
4015
Jeff Kirshera292ca62006-01-12 16:51:30 -08004016 while (cleaned_count--) {
Christoph Hellwigca6f7222006-08-31 14:27:47 -07004017 skb = buffer_info->skb;
4018 if (skb) {
Jeff Kirshera292ca62006-01-12 16:51:30 -08004019 skb_trim(skb, 0);
4020 goto map_skb;
4021 }
4022
Christoph Hellwigca6f7222006-08-31 14:27:47 -07004023 skb = netdev_alloc_skb(netdev, bufsz);
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004024 if (unlikely(!skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025 /* Better luck next round */
Jeff Kirsher72d64a42006-01-12 16:51:19 -08004026 adapter->alloc_rx_buff_failed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027 break;
4028 }
4029
Malli Chilakala26483452005-04-28 19:44:46 -07004030 /* Fix for errata 23, can't cross 64kB boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031 if (!e1000_check_64k_bound(adapter, skb->data, bufsz)) {
4032 struct sk_buff *oldskb = skb;
Malli Chilakala26483452005-04-28 19:44:46 -07004033 DPRINTK(RX_ERR, ERR, "skb align check failed: %u bytes "
4034 "at %p\n", bufsz, skb->data);
4035 /* Try again, without freeing the previous */
David S. Miller87f50322006-07-31 22:39:40 -07004036 skb = netdev_alloc_skb(netdev, bufsz);
Malli Chilakala26483452005-04-28 19:44:46 -07004037 /* Failed allocation, critical failure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038 if (!skb) {
4039 dev_kfree_skb(oldskb);
4040 break;
4041 }
Malli Chilakala26483452005-04-28 19:44:46 -07004042
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043 if (!e1000_check_64k_bound(adapter, skb->data, bufsz)) {
4044 /* give up */
4045 dev_kfree_skb(skb);
4046 dev_kfree_skb(oldskb);
4047 break; /* while !buffer_info->skb */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048 }
Christoph Hellwigca6f7222006-08-31 14:27:47 -07004049
4050 /* Use new allocation */
4051 dev_kfree_skb(oldskb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053 /* Make buffer alignment 2 beyond a 16 byte boundary
4054 * this will result in a 16 byte aligned IP header after
4055 * the 14 byte MAC header is removed
4056 */
4057 skb_reserve(skb, NET_IP_ALIGN);
4058
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059 buffer_info->skb = skb;
4060 buffer_info->length = adapter->rx_buffer_len;
Jeff Kirshera292ca62006-01-12 16:51:30 -08004061map_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062 buffer_info->dma = pci_map_single(pdev,
4063 skb->data,
4064 adapter->rx_buffer_len,
4065 PCI_DMA_FROMDEVICE);
4066
Malli Chilakala26483452005-04-28 19:44:46 -07004067 /* Fix for errata 23, can't cross 64kB boundary */
4068 if (!e1000_check_64k_bound(adapter,
4069 (void *)(unsigned long)buffer_info->dma,
4070 adapter->rx_buffer_len)) {
4071 DPRINTK(RX_ERR, ERR,
4072 "dma align check failed: %u bytes at %p\n",
4073 adapter->rx_buffer_len,
4074 (void *)(unsigned long)buffer_info->dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075 dev_kfree_skb(skb);
4076 buffer_info->skb = NULL;
4077
Malli Chilakala26483452005-04-28 19:44:46 -07004078 pci_unmap_single(pdev, buffer_info->dma,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079 adapter->rx_buffer_len,
4080 PCI_DMA_FROMDEVICE);
4081
4082 break; /* while !buffer_info->skb */
4083 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084 rx_desc = E1000_RX_DESC(*rx_ring, i);
4085 rx_desc->buffer_addr = cpu_to_le64(buffer_info->dma);
4086
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004087 if (unlikely(++i == rx_ring->count))
4088 i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089 buffer_info = &rx_ring->buffer_info[i];
4090 }
4091
Jesse Brandeburgb92ff8e2006-01-18 13:01:32 -08004092 if (likely(rx_ring->next_to_use != i)) {
4093 rx_ring->next_to_use = i;
4094 if (unlikely(i-- == 0))
4095 i = (rx_ring->count - 1);
4096
4097 /* Force memory writes to complete before letting h/w
4098 * know there are new descriptors to fetch. (Only
4099 * applicable for weak-ordered memory model archs,
4100 * such as IA-64). */
4101 wmb();
4102 writel(i, adapter->hw.hw_addr + rx_ring->rdt);
4103 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104}
4105
4106/**
Malli Chilakala2d7edb92005-04-28 19:43:52 -07004107 * e1000_alloc_rx_buffers_ps - Replace used receive buffers; packet split
4108 * @adapter: address of board private structure
4109 **/
4110
4111static void
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04004112e1000_alloc_rx_buffers_ps(struct e1000_adapter *adapter,
Jeff Kirsher72d64a42006-01-12 16:51:19 -08004113 struct e1000_rx_ring *rx_ring,
4114 int cleaned_count)
Malli Chilakala2d7edb92005-04-28 19:43:52 -07004115{
Malli Chilakala2d7edb92005-04-28 19:43:52 -07004116 struct net_device *netdev = adapter->netdev;
4117 struct pci_dev *pdev = adapter->pdev;
4118 union e1000_rx_desc_packet_split *rx_desc;
4119 struct e1000_buffer *buffer_info;
4120 struct e1000_ps_page *ps_page;
4121 struct e1000_ps_page_dma *ps_page_dma;
4122 struct sk_buff *skb;
4123 unsigned int i, j;
4124
4125 i = rx_ring->next_to_use;
4126 buffer_info = &rx_ring->buffer_info[i];
4127 ps_page = &rx_ring->ps_page[i];
4128 ps_page_dma = &rx_ring->ps_page_dma[i];
4129
Jeff Kirsher72d64a42006-01-12 16:51:19 -08004130 while (cleaned_count--) {
Malli Chilakala2d7edb92005-04-28 19:43:52 -07004131 rx_desc = E1000_RX_DESC_PS(*rx_ring, i);
4132
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004133 for (j = 0; j < PS_PAGE_BUFFERS; j++) {
Mallikarjuna R Chilakalae4c811c2005-10-04 07:05:44 -04004134 if (j < adapter->rx_ps_pages) {
4135 if (likely(!ps_page->ps_page[j])) {
4136 ps_page->ps_page[j] =
4137 alloc_page(GFP_ATOMIC);
Jesse Brandeburgb92ff8e2006-01-18 13:01:32 -08004138 if (unlikely(!ps_page->ps_page[j])) {
4139 adapter->alloc_rx_buff_failed++;
Mallikarjuna R Chilakalae4c811c2005-10-04 07:05:44 -04004140 goto no_buffers;
Jesse Brandeburgb92ff8e2006-01-18 13:01:32 -08004141 }
Mallikarjuna R Chilakalae4c811c2005-10-04 07:05:44 -04004142 ps_page_dma->ps_page_dma[j] =
4143 pci_map_page(pdev,
4144 ps_page->ps_page[j],
4145 0, PAGE_SIZE,
4146 PCI_DMA_FROMDEVICE);
4147 }
4148 /* Refresh the desc even if buffer_addrs didn't
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004149 * change because each write-back erases
Mallikarjuna R Chilakalae4c811c2005-10-04 07:05:44 -04004150 * this info.
4151 */
4152 rx_desc->read.buffer_addr[j+1] =
4153 cpu_to_le64(ps_page_dma->ps_page_dma[j]);
4154 } else
4155 rx_desc->read.buffer_addr[j+1] = ~0;
Malli Chilakala2d7edb92005-04-28 19:43:52 -07004156 }
4157
David S. Miller87f50322006-07-31 22:39:40 -07004158 skb = netdev_alloc_skb(netdev,
4159 adapter->rx_ps_bsize0 + NET_IP_ALIGN);
Malli Chilakala2d7edb92005-04-28 19:43:52 -07004160
Jesse Brandeburgb92ff8e2006-01-18 13:01:32 -08004161 if (unlikely(!skb)) {
4162 adapter->alloc_rx_buff_failed++;
Malli Chilakala2d7edb92005-04-28 19:43:52 -07004163 break;
Jesse Brandeburgb92ff8e2006-01-18 13:01:32 -08004164 }
Malli Chilakala2d7edb92005-04-28 19:43:52 -07004165
4166 /* Make buffer alignment 2 beyond a 16 byte boundary
4167 * this will result in a 16 byte aligned IP header after
4168 * the 14 byte MAC header is removed
4169 */
4170 skb_reserve(skb, NET_IP_ALIGN);
4171
Malli Chilakala2d7edb92005-04-28 19:43:52 -07004172 buffer_info->skb = skb;
4173 buffer_info->length = adapter->rx_ps_bsize0;
4174 buffer_info->dma = pci_map_single(pdev, skb->data,
4175 adapter->rx_ps_bsize0,
4176 PCI_DMA_FROMDEVICE);
4177
4178 rx_desc->read.buffer_addr[0] = cpu_to_le64(buffer_info->dma);
4179
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004180 if (unlikely(++i == rx_ring->count)) i = 0;
Malli Chilakala2d7edb92005-04-28 19:43:52 -07004181 buffer_info = &rx_ring->buffer_info[i];
4182 ps_page = &rx_ring->ps_page[i];
4183 ps_page_dma = &rx_ring->ps_page_dma[i];
4184 }
4185
4186no_buffers:
Jesse Brandeburgb92ff8e2006-01-18 13:01:32 -08004187 if (likely(rx_ring->next_to_use != i)) {
4188 rx_ring->next_to_use = i;
4189 if (unlikely(i-- == 0)) i = (rx_ring->count - 1);
4190
4191 /* Force memory writes to complete before letting h/w
4192 * know there are new descriptors to fetch. (Only
4193 * applicable for weak-ordered memory model archs,
4194 * such as IA-64). */
4195 wmb();
4196 /* Hardware increments by 16 bytes, but packet split
4197 * descriptors are 32 bytes...so we increment tail
4198 * twice as much.
4199 */
4200 writel(i<<1, adapter->hw.hw_addr + rx_ring->rdt);
4201 }
Malli Chilakala2d7edb92005-04-28 19:43:52 -07004202}
4203
4204/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205 * e1000_smartspeed - Workaround for SmartSpeed on 82541 and 82547 controllers.
4206 * @adapter:
4207 **/
4208
4209static void
4210e1000_smartspeed(struct e1000_adapter *adapter)
4211{
4212 uint16_t phy_status;
4213 uint16_t phy_ctrl;
4214
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004215 if ((adapter->hw.phy_type != e1000_phy_igp) || !adapter->hw.autoneg ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216 !(adapter->hw.autoneg_advertised & ADVERTISE_1000_FULL))
4217 return;
4218
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004219 if (adapter->smartspeed == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220 /* If Master/Slave config fault is asserted twice,
4221 * we assume back-to-back */
4222 e1000_read_phy_reg(&adapter->hw, PHY_1000T_STATUS, &phy_status);
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004223 if (!(phy_status & SR_1000T_MS_CONFIG_FAULT)) return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224 e1000_read_phy_reg(&adapter->hw, PHY_1000T_STATUS, &phy_status);
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004225 if (!(phy_status & SR_1000T_MS_CONFIG_FAULT)) return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226 e1000_read_phy_reg(&adapter->hw, PHY_1000T_CTRL, &phy_ctrl);
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004227 if (phy_ctrl & CR_1000T_MS_ENABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228 phy_ctrl &= ~CR_1000T_MS_ENABLE;
4229 e1000_write_phy_reg(&adapter->hw, PHY_1000T_CTRL,
4230 phy_ctrl);
4231 adapter->smartspeed++;
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004232 if (!e1000_phy_setup_autoneg(&adapter->hw) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233 !e1000_read_phy_reg(&adapter->hw, PHY_CTRL,
4234 &phy_ctrl)) {
4235 phy_ctrl |= (MII_CR_AUTO_NEG_EN |
4236 MII_CR_RESTART_AUTO_NEG);
4237 e1000_write_phy_reg(&adapter->hw, PHY_CTRL,
4238 phy_ctrl);
4239 }
4240 }
4241 return;
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004242 } else if (adapter->smartspeed == E1000_SMARTSPEED_DOWNSHIFT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243 /* If still no link, perhaps using 2/3 pair cable */
4244 e1000_read_phy_reg(&adapter->hw, PHY_1000T_CTRL, &phy_ctrl);
4245 phy_ctrl |= CR_1000T_MS_ENABLE;
4246 e1000_write_phy_reg(&adapter->hw, PHY_1000T_CTRL, phy_ctrl);
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004247 if (!e1000_phy_setup_autoneg(&adapter->hw) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248 !e1000_read_phy_reg(&adapter->hw, PHY_CTRL, &phy_ctrl)) {
4249 phy_ctrl |= (MII_CR_AUTO_NEG_EN |
4250 MII_CR_RESTART_AUTO_NEG);
4251 e1000_write_phy_reg(&adapter->hw, PHY_CTRL, phy_ctrl);
4252 }
4253 }
4254 /* Restart process after E1000_SMARTSPEED_MAX iterations */
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004255 if (adapter->smartspeed++ == E1000_SMARTSPEED_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256 adapter->smartspeed = 0;
4257}
4258
4259/**
4260 * e1000_ioctl -
4261 * @netdev:
4262 * @ifreq:
4263 * @cmd:
4264 **/
4265
4266static int
4267e1000_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
4268{
4269 switch (cmd) {
4270 case SIOCGMIIPHY:
4271 case SIOCGMIIREG:
4272 case SIOCSMIIREG:
4273 return e1000_mii_ioctl(netdev, ifr, cmd);
4274 default:
4275 return -EOPNOTSUPP;
4276 }
4277}
4278
4279/**
4280 * e1000_mii_ioctl -
4281 * @netdev:
4282 * @ifreq:
4283 * @cmd:
4284 **/
4285
4286static int
4287e1000_mii_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
4288{
Malli Chilakala60490fe2005-06-17 17:41:45 -07004289 struct e1000_adapter *adapter = netdev_priv(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290 struct mii_ioctl_data *data = if_mii(ifr);
4291 int retval;
4292 uint16_t mii_reg;
4293 uint16_t spddplx;
Malli Chilakala97876fc2005-06-17 17:40:19 -07004294 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004296 if (adapter->hw.media_type != e1000_media_type_copper)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297 return -EOPNOTSUPP;
4298
4299 switch (cmd) {
4300 case SIOCGMIIPHY:
4301 data->phy_id = adapter->hw.phy_addr;
4302 break;
4303 case SIOCGMIIREG:
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004304 if (!capable(CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305 return -EPERM;
Malli Chilakala97876fc2005-06-17 17:40:19 -07004306 spin_lock_irqsave(&adapter->stats_lock, flags);
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004307 if (e1000_read_phy_reg(&adapter->hw, data->reg_num & 0x1F,
Malli Chilakala97876fc2005-06-17 17:40:19 -07004308 &data->val_out)) {
4309 spin_unlock_irqrestore(&adapter->stats_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310 return -EIO;
Malli Chilakala97876fc2005-06-17 17:40:19 -07004311 }
4312 spin_unlock_irqrestore(&adapter->stats_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313 break;
4314 case SIOCSMIIREG:
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004315 if (!capable(CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316 return -EPERM;
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004317 if (data->reg_num & ~(0x1F))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318 return -EFAULT;
4319 mii_reg = data->val_in;
Malli Chilakala97876fc2005-06-17 17:40:19 -07004320 spin_lock_irqsave(&adapter->stats_lock, flags);
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004321 if (e1000_write_phy_reg(&adapter->hw, data->reg_num,
Malli Chilakala97876fc2005-06-17 17:40:19 -07004322 mii_reg)) {
4323 spin_unlock_irqrestore(&adapter->stats_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324 return -EIO;
Malli Chilakala97876fc2005-06-17 17:40:19 -07004325 }
Auke Kokdc86d322006-04-18 12:30:51 -07004326 if (adapter->hw.media_type == e1000_media_type_copper) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327 switch (data->reg_num) {
4328 case PHY_CTRL:
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004329 if (mii_reg & MII_CR_POWER_DOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330 break;
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004331 if (mii_reg & MII_CR_AUTO_NEG_EN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332 adapter->hw.autoneg = 1;
4333 adapter->hw.autoneg_advertised = 0x2F;
4334 } else {
4335 if (mii_reg & 0x40)
4336 spddplx = SPEED_1000;
4337 else if (mii_reg & 0x2000)
4338 spddplx = SPEED_100;
4339 else
4340 spddplx = SPEED_10;
4341 spddplx += (mii_reg & 0x100)
Jeff Kirshercb764322006-03-08 17:24:12 -08004342 ? DUPLEX_FULL :
4343 DUPLEX_HALF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344 retval = e1000_set_spd_dplx(adapter,
4345 spddplx);
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004346 if (retval) {
Malli Chilakala97876fc2005-06-17 17:40:19 -07004347 spin_unlock_irqrestore(
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004348 &adapter->stats_lock,
Malli Chilakala97876fc2005-06-17 17:40:19 -07004349 flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350 return retval;
Malli Chilakala97876fc2005-06-17 17:40:19 -07004351 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352 }
Auke Kok2db10a02006-06-27 09:06:28 -07004353 if (netif_running(adapter->netdev))
4354 e1000_reinit_locked(adapter);
4355 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356 e1000_reset(adapter);
4357 break;
4358 case M88E1000_PHY_SPEC_CTRL:
4359 case M88E1000_EXT_PHY_SPEC_CTRL:
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004360 if (e1000_phy_reset(&adapter->hw)) {
Malli Chilakala97876fc2005-06-17 17:40:19 -07004361 spin_unlock_irqrestore(
4362 &adapter->stats_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363 return -EIO;
Malli Chilakala97876fc2005-06-17 17:40:19 -07004364 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365 break;
4366 }
4367 } else {
4368 switch (data->reg_num) {
4369 case PHY_CTRL:
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004370 if (mii_reg & MII_CR_POWER_DOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371 break;
Auke Kok2db10a02006-06-27 09:06:28 -07004372 if (netif_running(adapter->netdev))
4373 e1000_reinit_locked(adapter);
4374 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375 e1000_reset(adapter);
4376 break;
4377 }
4378 }
Malli Chilakala97876fc2005-06-17 17:40:19 -07004379 spin_unlock_irqrestore(&adapter->stats_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380 break;
4381 default:
4382 return -EOPNOTSUPP;
4383 }
4384 return E1000_SUCCESS;
4385}
4386
4387void
4388e1000_pci_set_mwi(struct e1000_hw *hw)
4389{
4390 struct e1000_adapter *adapter = hw->back;
Malli Chilakala26483452005-04-28 19:44:46 -07004391 int ret_val = pci_set_mwi(adapter->pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004393 if (ret_val)
Malli Chilakala26483452005-04-28 19:44:46 -07004394 DPRINTK(PROBE, ERR, "Error in setting MWI\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395}
4396
4397void
4398e1000_pci_clear_mwi(struct e1000_hw *hw)
4399{
4400 struct e1000_adapter *adapter = hw->back;
4401
4402 pci_clear_mwi(adapter->pdev);
4403}
4404
4405void
4406e1000_read_pci_cfg(struct e1000_hw *hw, uint32_t reg, uint16_t *value)
4407{
4408 struct e1000_adapter *adapter = hw->back;
4409
4410 pci_read_config_word(adapter->pdev, reg, value);
4411}
4412
4413void
4414e1000_write_pci_cfg(struct e1000_hw *hw, uint32_t reg, uint16_t *value)
4415{
4416 struct e1000_adapter *adapter = hw->back;
4417
4418 pci_write_config_word(adapter->pdev, reg, *value);
4419}
4420
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421void
4422e1000_io_write(struct e1000_hw *hw, unsigned long port, uint32_t value)
4423{
4424 outl(value, port);
4425}
4426
4427static void
4428e1000_vlan_rx_register(struct net_device *netdev, struct vlan_group *grp)
4429{
Malli Chilakala60490fe2005-06-17 17:41:45 -07004430 struct e1000_adapter *adapter = netdev_priv(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431 uint32_t ctrl, rctl;
4432
4433 e1000_irq_disable(adapter);
4434 adapter->vlgrp = grp;
4435
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004436 if (grp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437 /* enable VLAN tag insert/strip */
4438 ctrl = E1000_READ_REG(&adapter->hw, CTRL);
4439 ctrl |= E1000_CTRL_VME;
4440 E1000_WRITE_REG(&adapter->hw, CTRL, ctrl);
4441
Auke Kokcd94dd02006-06-27 09:08:22 -07004442 if (adapter->hw.mac_type != e1000_ich8lan) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443 /* enable VLAN receive filtering */
4444 rctl = E1000_READ_REG(&adapter->hw, RCTL);
4445 rctl |= E1000_RCTL_VFE;
4446 rctl &= ~E1000_RCTL_CFIEN;
4447 E1000_WRITE_REG(&adapter->hw, RCTL, rctl);
Malli Chilakala2d7edb92005-04-28 19:43:52 -07004448 e1000_update_mng_vlan(adapter);
Auke Kokcd94dd02006-06-27 09:08:22 -07004449 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450 } else {
4451 /* disable VLAN tag insert/strip */
4452 ctrl = E1000_READ_REG(&adapter->hw, CTRL);
4453 ctrl &= ~E1000_CTRL_VME;
4454 E1000_WRITE_REG(&adapter->hw, CTRL, ctrl);
4455
Auke Kokcd94dd02006-06-27 09:08:22 -07004456 if (adapter->hw.mac_type != e1000_ich8lan) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457 /* disable VLAN filtering */
4458 rctl = E1000_READ_REG(&adapter->hw, RCTL);
4459 rctl &= ~E1000_RCTL_VFE;
4460 E1000_WRITE_REG(&adapter->hw, RCTL, rctl);
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004461 if (adapter->mng_vlan_id != (uint16_t)E1000_MNG_VLAN_NONE) {
Malli Chilakala2d7edb92005-04-28 19:43:52 -07004462 e1000_vlan_rx_kill_vid(netdev, adapter->mng_vlan_id);
4463 adapter->mng_vlan_id = E1000_MNG_VLAN_NONE;
4464 }
Auke Kokcd94dd02006-06-27 09:08:22 -07004465 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466 }
4467
4468 e1000_irq_enable(adapter);
4469}
4470
4471static void
4472e1000_vlan_rx_add_vid(struct net_device *netdev, uint16_t vid)
4473{
Malli Chilakala60490fe2005-06-17 17:41:45 -07004474 struct e1000_adapter *adapter = netdev_priv(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475 uint32_t vfta, index;
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004476
4477 if ((adapter->hw.mng_cookie.status &
4478 E1000_MNG_DHCP_COOKIE_STATUS_VLAN_SUPPORT) &&
4479 (vid == adapter->mng_vlan_id))
Malli Chilakala2d7edb92005-04-28 19:43:52 -07004480 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481 /* add VID to filter table */
4482 index = (vid >> 5) & 0x7F;
4483 vfta = E1000_READ_REG_ARRAY(&adapter->hw, VFTA, index);
4484 vfta |= (1 << (vid & 0x1F));
4485 e1000_write_vfta(&adapter->hw, index, vfta);
4486}
4487
4488static void
4489e1000_vlan_rx_kill_vid(struct net_device *netdev, uint16_t vid)
4490{
Malli Chilakala60490fe2005-06-17 17:41:45 -07004491 struct e1000_adapter *adapter = netdev_priv(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492 uint32_t vfta, index;
4493
4494 e1000_irq_disable(adapter);
4495
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004496 if (adapter->vlgrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497 adapter->vlgrp->vlan_devices[vid] = NULL;
4498
4499 e1000_irq_enable(adapter);
4500
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004501 if ((adapter->hw.mng_cookie.status &
4502 E1000_MNG_DHCP_COOKIE_STATUS_VLAN_SUPPORT) &&
Jeff Kirsherff147012006-01-12 16:51:10 -08004503 (vid == adapter->mng_vlan_id)) {
4504 /* release control to f/w */
4505 e1000_release_hw_control(adapter);
Malli Chilakala2d7edb92005-04-28 19:43:52 -07004506 return;
Jeff Kirsherff147012006-01-12 16:51:10 -08004507 }
4508
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509 /* remove VID from filter table */
4510 index = (vid >> 5) & 0x7F;
4511 vfta = E1000_READ_REG_ARRAY(&adapter->hw, VFTA, index);
4512 vfta &= ~(1 << (vid & 0x1F));
4513 e1000_write_vfta(&adapter->hw, index, vfta);
4514}
4515
4516static void
4517e1000_restore_vlan(struct e1000_adapter *adapter)
4518{
4519 e1000_vlan_rx_register(adapter->netdev, adapter->vlgrp);
4520
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004521 if (adapter->vlgrp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522 uint16_t vid;
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004523 for (vid = 0; vid < VLAN_GROUP_ARRAY_LEN; vid++) {
4524 if (!adapter->vlgrp->vlan_devices[vid])
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525 continue;
4526 e1000_vlan_rx_add_vid(adapter->netdev, vid);
4527 }
4528 }
4529}
4530
4531int
4532e1000_set_spd_dplx(struct e1000_adapter *adapter, uint16_t spddplx)
4533{
4534 adapter->hw.autoneg = 0;
4535
Malli Chilakala69213682005-06-17 17:44:20 -07004536 /* Fiber NICs only allow 1000 gbps Full duplex */
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004537 if ((adapter->hw.media_type == e1000_media_type_fiber) &&
Malli Chilakala69213682005-06-17 17:44:20 -07004538 spddplx != (SPEED_1000 + DUPLEX_FULL)) {
4539 DPRINTK(PROBE, ERR, "Unsupported Speed/Duplex configuration\n");
4540 return -EINVAL;
4541 }
4542
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004543 switch (spddplx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544 case SPEED_10 + DUPLEX_HALF:
4545 adapter->hw.forced_speed_duplex = e1000_10_half;
4546 break;
4547 case SPEED_10 + DUPLEX_FULL:
4548 adapter->hw.forced_speed_duplex = e1000_10_full;
4549 break;
4550 case SPEED_100 + DUPLEX_HALF:
4551 adapter->hw.forced_speed_duplex = e1000_100_half;
4552 break;
4553 case SPEED_100 + DUPLEX_FULL:
4554 adapter->hw.forced_speed_duplex = e1000_100_full;
4555 break;
4556 case SPEED_1000 + DUPLEX_FULL:
4557 adapter->hw.autoneg = 1;
4558 adapter->hw.autoneg_advertised = ADVERTISE_1000_FULL;
4559 break;
4560 case SPEED_1000 + DUPLEX_HALF: /* not supported */
4561 default:
Malli Chilakala26483452005-04-28 19:44:46 -07004562 DPRINTK(PROBE, ERR, "Unsupported Speed/Duplex configuration\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563 return -EINVAL;
4564 }
4565 return 0;
4566}
4567
Martin J. Blighb6a1d5f2005-10-28 15:14:44 -07004568#ifdef CONFIG_PM
Jeff Kirsher0f15a8f2006-03-02 18:46:29 -08004569/* Save/restore 16 or 64 dwords of PCI config space depending on which
4570 * bus we're on (PCI(X) vs. PCI-E)
Jesse Brandeburg2f826652006-01-18 13:01:34 -08004571 */
4572#define PCIE_CONFIG_SPACE_LEN 256
4573#define PCI_CONFIG_SPACE_LEN 64
4574static int
4575e1000_pci_save_state(struct e1000_adapter *adapter)
4576{
4577 struct pci_dev *dev = adapter->pdev;
4578 int size;
4579 int i;
Jeff Kirsher0f15a8f2006-03-02 18:46:29 -08004580
Jesse Brandeburg2f826652006-01-18 13:01:34 -08004581 if (adapter->hw.mac_type >= e1000_82571)
4582 size = PCIE_CONFIG_SPACE_LEN;
4583 else
4584 size = PCI_CONFIG_SPACE_LEN;
4585
4586 WARN_ON(adapter->config_space != NULL);
4587
4588 adapter->config_space = kmalloc(size, GFP_KERNEL);
4589 if (!adapter->config_space) {
4590 DPRINTK(PROBE, ERR, "unable to allocate %d bytes\n", size);
4591 return -ENOMEM;
4592 }
4593 for (i = 0; i < (size / 4); i++)
4594 pci_read_config_dword(dev, i * 4, &adapter->config_space[i]);
4595 return 0;
4596}
4597
4598static void
4599e1000_pci_restore_state(struct e1000_adapter *adapter)
4600{
4601 struct pci_dev *dev = adapter->pdev;
4602 int size;
4603 int i;
Jeff Kirsher0f15a8f2006-03-02 18:46:29 -08004604
Jesse Brandeburg2f826652006-01-18 13:01:34 -08004605 if (adapter->config_space == NULL)
4606 return;
Jeff Kirsher0f15a8f2006-03-02 18:46:29 -08004607
Jesse Brandeburg2f826652006-01-18 13:01:34 -08004608 if (adapter->hw.mac_type >= e1000_82571)
4609 size = PCIE_CONFIG_SPACE_LEN;
4610 else
4611 size = PCI_CONFIG_SPACE_LEN;
4612 for (i = 0; i < (size / 4); i++)
4613 pci_write_config_dword(dev, i * 4, adapter->config_space[i]);
4614 kfree(adapter->config_space);
4615 adapter->config_space = NULL;
4616 return;
4617}
4618#endif /* CONFIG_PM */
4619
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620static int
Pavel Machek829ca9a2005-09-03 15:56:56 -07004621e1000_suspend(struct pci_dev *pdev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622{
4623 struct net_device *netdev = pci_get_drvdata(pdev);
Malli Chilakala60490fe2005-06-17 17:41:45 -07004624 struct e1000_adapter *adapter = netdev_priv(netdev);
Jeff Kirsherb55ccb32006-01-12 16:50:30 -08004625 uint32_t ctrl, ctrl_ext, rctl, manc, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626 uint32_t wufc = adapter->wol;
Auke Kok6fdfef12006-06-27 09:06:36 -07004627#ifdef CONFIG_PM
Jeff Kirsher240b1712006-01-12 16:51:28 -08004628 int retval = 0;
Auke Kok6fdfef12006-06-27 09:06:36 -07004629#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630
4631 netif_device_detach(netdev);
4632
Auke Kok2db10a02006-06-27 09:06:28 -07004633 if (netif_running(netdev)) {
4634 WARN_ON(test_bit(__E1000_RESETTING, &adapter->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635 e1000_down(adapter);
Auke Kok2db10a02006-06-27 09:06:28 -07004636 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637
Jesse Brandeburg2f826652006-01-18 13:01:34 -08004638#ifdef CONFIG_PM
Jeff Kirsher0f15a8f2006-03-02 18:46:29 -08004639 /* Implement our own version of pci_save_state(pdev) because pci-
4640 * express adapters have 256-byte config spaces. */
Jesse Brandeburg2f826652006-01-18 13:01:34 -08004641 retval = e1000_pci_save_state(adapter);
4642 if (retval)
4643 return retval;
4644#endif
4645
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646 status = E1000_READ_REG(&adapter->hw, STATUS);
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004647 if (status & E1000_STATUS_LU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648 wufc &= ~E1000_WUFC_LNKC;
4649
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004650 if (wufc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651 e1000_setup_rctl(adapter);
4652 e1000_set_multi(netdev);
4653
4654 /* turn on all-multi mode if wake on multicast is enabled */
Jesse Brandeburg120cd572006-08-31 14:27:46 -07004655 if (wufc & E1000_WUFC_MC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656 rctl = E1000_READ_REG(&adapter->hw, RCTL);
4657 rctl |= E1000_RCTL_MPE;
4658 E1000_WRITE_REG(&adapter->hw, RCTL, rctl);
4659 }
4660
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004661 if (adapter->hw.mac_type >= e1000_82540) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662 ctrl = E1000_READ_REG(&adapter->hw, CTRL);
4663 /* advertise wake from D3Cold */
4664 #define E1000_CTRL_ADVD3WUC 0x00100000
4665 /* phy power management enable */
4666 #define E1000_CTRL_EN_PHY_PWR_MGMT 0x00200000
4667 ctrl |= E1000_CTRL_ADVD3WUC |
4668 E1000_CTRL_EN_PHY_PWR_MGMT;
4669 E1000_WRITE_REG(&adapter->hw, CTRL, ctrl);
4670 }
4671
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004672 if (adapter->hw.media_type == e1000_media_type_fiber ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673 adapter->hw.media_type == e1000_media_type_internal_serdes) {
4674 /* keep the laser running in D3 */
4675 ctrl_ext = E1000_READ_REG(&adapter->hw, CTRL_EXT);
4676 ctrl_ext |= E1000_CTRL_EXT_SDP7_DATA;
4677 E1000_WRITE_REG(&adapter->hw, CTRL_EXT, ctrl_ext);
4678 }
4679
Malli Chilakala2d7edb92005-04-28 19:43:52 -07004680 /* Allow time for pending master requests to run */
4681 e1000_disable_pciex_master(&adapter->hw);
4682
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683 E1000_WRITE_REG(&adapter->hw, WUC, E1000_WUC_PME_EN);
4684 E1000_WRITE_REG(&adapter->hw, WUFC, wufc);
Auke Kokd0e027d2006-04-14 19:04:40 -07004685 pci_enable_wake(pdev, PCI_D3hot, 1);
4686 pci_enable_wake(pdev, PCI_D3cold, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687 } else {
4688 E1000_WRITE_REG(&adapter->hw, WUC, 0);
4689 E1000_WRITE_REG(&adapter->hw, WUFC, 0);
Auke Kokd0e027d2006-04-14 19:04:40 -07004690 pci_enable_wake(pdev, PCI_D3hot, 0);
4691 pci_enable_wake(pdev, PCI_D3cold, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692 }
4693
Jeff Kirsher5f016072006-09-27 12:53:42 -07004694 if (adapter->hw.mac_type < e1000_82571 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695 adapter->hw.media_type == e1000_media_type_copper) {
4696 manc = E1000_READ_REG(&adapter->hw, MANC);
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004697 if (manc & E1000_MANC_SMBUS_EN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004698 manc |= E1000_MANC_ARP_EN;
4699 E1000_WRITE_REG(&adapter->hw, MANC, manc);
Auke Kokd0e027d2006-04-14 19:04:40 -07004700 pci_enable_wake(pdev, PCI_D3hot, 1);
4701 pci_enable_wake(pdev, PCI_D3cold, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702 }
4703 }
4704
Auke Kokcd94dd02006-06-27 09:08:22 -07004705 if (adapter->hw.phy_type == e1000_phy_igp_3)
4706 e1000_phy_powerdown_workaround(&adapter->hw);
4707
Jeff Kirsherb55ccb32006-01-12 16:50:30 -08004708 /* Release control of h/w to f/w. If f/w is AMT enabled, this
4709 * would have already happened in close and is redundant. */
4710 e1000_release_hw_control(adapter);
Malli Chilakala2d7edb92005-04-28 19:43:52 -07004711
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712 pci_disable_device(pdev);
Jeff Kirsher240b1712006-01-12 16:51:28 -08004713
Auke Kokd0e027d2006-04-14 19:04:40 -07004714 pci_set_power_state(pdev, pci_choose_state(pdev, state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715
4716 return 0;
4717}
4718
Jesse Brandeburg2f826652006-01-18 13:01:34 -08004719#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720static int
4721e1000_resume(struct pci_dev *pdev)
4722{
4723 struct net_device *netdev = pci_get_drvdata(pdev);
Malli Chilakala60490fe2005-06-17 17:41:45 -07004724 struct e1000_adapter *adapter = netdev_priv(netdev);
Auke Kok3d1dd8c2006-08-28 14:56:27 -07004725 uint32_t manc, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004726
Auke Kokd0e027d2006-04-14 19:04:40 -07004727 pci_set_power_state(pdev, PCI_D0);
Jesse Brandeburg2f826652006-01-18 13:01:34 -08004728 e1000_pci_restore_state(adapter);
Auke Kok3d1dd8c2006-08-28 14:56:27 -07004729 if ((err = pci_enable_device(pdev))) {
4730 printk(KERN_ERR "e1000: Cannot enable PCI device from suspend\n");
4731 return err;
4732 }
Malli Chilakalaa4cb8472005-04-28 19:41:28 -07004733 pci_set_master(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734
Auke Kokd0e027d2006-04-14 19:04:40 -07004735 pci_enable_wake(pdev, PCI_D3hot, 0);
4736 pci_enable_wake(pdev, PCI_D3cold, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737
4738 e1000_reset(adapter);
4739 E1000_WRITE_REG(&adapter->hw, WUS, ~0);
4740
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004741 if (netif_running(netdev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742 e1000_up(adapter);
4743
4744 netif_device_attach(netdev);
4745
Jeff Kirsher5f016072006-09-27 12:53:42 -07004746 if (adapter->hw.mac_type < e1000_82571 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004747 adapter->hw.media_type == e1000_media_type_copper) {
4748 manc = E1000_READ_REG(&adapter->hw, MANC);
4749 manc &= ~(E1000_MANC_ARP_EN);
4750 E1000_WRITE_REG(&adapter->hw, MANC, manc);
4751 }
4752
Jeff Kirsherb55ccb32006-01-12 16:50:30 -08004753 /* If the controller is 82573 and f/w is AMT, do not set
4754 * DRV_LOAD until the interface is up. For all other cases,
4755 * let the f/w know that the h/w is now under the control
4756 * of the driver. */
4757 if (adapter->hw.mac_type != e1000_82573 ||
4758 !e1000_check_mng_mode(&adapter->hw))
4759 e1000_get_hw_control(adapter);
Malli Chilakala2d7edb92005-04-28 19:43:52 -07004760
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761 return 0;
4762}
4763#endif
Auke Kokc653e632006-05-23 13:35:57 -07004764
4765static void e1000_shutdown(struct pci_dev *pdev)
4766{
4767 e1000_suspend(pdev, PMSG_SUSPEND);
4768}
4769
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770#ifdef CONFIG_NET_POLL_CONTROLLER
4771/*
4772 * Polling 'interrupt' - used by things like netconsole to send skbs
4773 * without having to re-enable interrupts. It's not called while
4774 * the interrupt routine is executing.
4775 */
4776static void
Malli Chilakala26483452005-04-28 19:44:46 -07004777e1000_netpoll(struct net_device *netdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778{
Malli Chilakala60490fe2005-06-17 17:41:45 -07004779 struct e1000_adapter *adapter = netdev_priv(netdev);
Auke Kokd3d9e482006-07-14 16:14:23 -07004780
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781 disable_irq(adapter->pdev->irq);
4782 e1000_intr(adapter->pdev->irq, netdev, NULL);
Andrew Mortonc4cfe562005-10-15 00:32:29 -07004783 e1000_clean_tx_irq(adapter, adapter->tx_ring);
Jeff Kirshere8da8be2006-01-12 16:51:14 -08004784#ifndef CONFIG_E1000_NAPI
4785 adapter->clean_rx(adapter, adapter->rx_ring);
4786#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787 enable_irq(adapter->pdev->irq);
4788}
4789#endif
4790
Auke Kok90267292006-06-08 09:30:24 -07004791/**
4792 * e1000_io_error_detected - called when PCI error is detected
4793 * @pdev: Pointer to PCI device
4794 * @state: The current pci conneection state
4795 *
4796 * This function is called after a PCI bus error affecting
4797 * this device has been detected.
4798 */
4799static pci_ers_result_t e1000_io_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
4800{
4801 struct net_device *netdev = pci_get_drvdata(pdev);
4802 struct e1000_adapter *adapter = netdev->priv;
4803
4804 netif_device_detach(netdev);
4805
4806 if (netif_running(netdev))
4807 e1000_down(adapter);
Linas Vepstas72e8d6b2006-09-18 20:58:06 -07004808 pci_disable_device(pdev);
Auke Kok90267292006-06-08 09:30:24 -07004809
4810 /* Request a slot slot reset. */
4811 return PCI_ERS_RESULT_NEED_RESET;
4812}
4813
4814/**
4815 * e1000_io_slot_reset - called after the pci bus has been reset.
4816 * @pdev: Pointer to PCI device
4817 *
4818 * Restart the card from scratch, as if from a cold-boot. Implementation
4819 * resembles the first-half of the e1000_resume routine.
4820 */
4821static pci_ers_result_t e1000_io_slot_reset(struct pci_dev *pdev)
4822{
4823 struct net_device *netdev = pci_get_drvdata(pdev);
4824 struct e1000_adapter *adapter = netdev->priv;
4825
4826 if (pci_enable_device(pdev)) {
4827 printk(KERN_ERR "e1000: Cannot re-enable PCI device after reset.\n");
4828 return PCI_ERS_RESULT_DISCONNECT;
4829 }
4830 pci_set_master(pdev);
4831
4832 pci_enable_wake(pdev, 3, 0);
4833 pci_enable_wake(pdev, 4, 0); /* 4 == D3 cold */
4834
4835 /* Perform card reset only on one instance of the card */
4836 if (PCI_FUNC (pdev->devfn) != 0)
4837 return PCI_ERS_RESULT_RECOVERED;
4838
4839 e1000_reset(adapter);
4840 E1000_WRITE_REG(&adapter->hw, WUS, ~0);
4841
4842 return PCI_ERS_RESULT_RECOVERED;
4843}
4844
4845/**
4846 * e1000_io_resume - called when traffic can start flowing again.
4847 * @pdev: Pointer to PCI device
4848 *
4849 * This callback is called when the error recovery driver tells us that
4850 * its OK to resume normal operation. Implementation resembles the
4851 * second-half of the e1000_resume routine.
4852 */
4853static void e1000_io_resume(struct pci_dev *pdev)
4854{
4855 struct net_device *netdev = pci_get_drvdata(pdev);
4856 struct e1000_adapter *adapter = netdev->priv;
4857 uint32_t manc, swsm;
4858
4859 if (netif_running(netdev)) {
4860 if (e1000_up(adapter)) {
4861 printk("e1000: can't bring device back up after reset\n");
4862 return;
4863 }
4864 }
4865
4866 netif_device_attach(netdev);
4867
4868 if (adapter->hw.mac_type >= e1000_82540 &&
4869 adapter->hw.media_type == e1000_media_type_copper) {
4870 manc = E1000_READ_REG(&adapter->hw, MANC);
4871 manc &= ~(E1000_MANC_ARP_EN);
4872 E1000_WRITE_REG(&adapter->hw, MANC, manc);
4873 }
4874
4875 switch (adapter->hw.mac_type) {
4876 case e1000_82573:
4877 swsm = E1000_READ_REG(&adapter->hw, SWSM);
4878 E1000_WRITE_REG(&adapter->hw, SWSM,
4879 swsm | E1000_SWSM_DRV_LOAD);
4880 break;
4881 default:
4882 break;
4883 }
4884
4885 if (netif_running(netdev))
4886 mod_timer(&adapter->watchdog_timer, jiffies);
4887}
4888
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889/* e1000_main.c */