blob: c18cb8e883dd3f1d1e4f7eff8fa298c77c879a5b [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"
Andrew Mortond0bb53e2006-11-14 10:35:03 -050030#include <net/ip6_checksum.h>
Dirk Brandewie5377a412011-01-06 14:29:54 +000031#include <linux/io.h>
32
33/* Intel Media SOC GbE MDIO physical base address */
34static unsigned long ce4100_gbe_mdio_base_phy;
35/* Intel Media SOC GbE MDIO virtual base address */
36void __iomem *ce4100_gbe_mdio_base_virt;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Linus Torvalds1da177e2005-04-16 15:20:36 -070038char e1000_driver_name[] = "e1000";
Adrian Bunk3ad2cc62005-10-30 16:53:34 +010039static char e1000_driver_string[] = "Intel(R) PRO/1000 Network Driver";
Anupam Chandaab088532010-11-21 09:54:21 -080040#define DRV_VERSION "7.3.21-k8-NAPI"
Stephen Hemmingerabec42a2007-10-29 10:46:19 -070041const char e1000_driver_version[] = DRV_VERSION;
42static const char e1000_copyright[] = "Copyright (c) 1999-2006 Intel Corporation.";
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44/* e1000_pci_tbl - PCI Device ID Table
45 *
46 * Last entry must be all 0s
47 *
48 * Macro expands to...
49 * {PCI_DEVICE(PCI_VENDOR_ID_INTEL, device_id)}
50 */
Alexey Dobriyana3aa1882010-01-07 11:58:11 +000051static DEFINE_PCI_DEVICE_TABLE(e1000_pci_tbl) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 INTEL_E1000_ETHERNET_DEVICE(0x1000),
53 INTEL_E1000_ETHERNET_DEVICE(0x1001),
54 INTEL_E1000_ETHERNET_DEVICE(0x1004),
55 INTEL_E1000_ETHERNET_DEVICE(0x1008),
56 INTEL_E1000_ETHERNET_DEVICE(0x1009),
57 INTEL_E1000_ETHERNET_DEVICE(0x100C),
58 INTEL_E1000_ETHERNET_DEVICE(0x100D),
59 INTEL_E1000_ETHERNET_DEVICE(0x100E),
60 INTEL_E1000_ETHERNET_DEVICE(0x100F),
61 INTEL_E1000_ETHERNET_DEVICE(0x1010),
62 INTEL_E1000_ETHERNET_DEVICE(0x1011),
63 INTEL_E1000_ETHERNET_DEVICE(0x1012),
64 INTEL_E1000_ETHERNET_DEVICE(0x1013),
65 INTEL_E1000_ETHERNET_DEVICE(0x1014),
66 INTEL_E1000_ETHERNET_DEVICE(0x1015),
67 INTEL_E1000_ETHERNET_DEVICE(0x1016),
68 INTEL_E1000_ETHERNET_DEVICE(0x1017),
69 INTEL_E1000_ETHERNET_DEVICE(0x1018),
70 INTEL_E1000_ETHERNET_DEVICE(0x1019),
Malli Chilakala26483452005-04-28 19:44:46 -070071 INTEL_E1000_ETHERNET_DEVICE(0x101A),
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 INTEL_E1000_ETHERNET_DEVICE(0x101D),
73 INTEL_E1000_ETHERNET_DEVICE(0x101E),
74 INTEL_E1000_ETHERNET_DEVICE(0x1026),
75 INTEL_E1000_ETHERNET_DEVICE(0x1027),
76 INTEL_E1000_ETHERNET_DEVICE(0x1028),
77 INTEL_E1000_ETHERNET_DEVICE(0x1075),
78 INTEL_E1000_ETHERNET_DEVICE(0x1076),
79 INTEL_E1000_ETHERNET_DEVICE(0x1077),
80 INTEL_E1000_ETHERNET_DEVICE(0x1078),
81 INTEL_E1000_ETHERNET_DEVICE(0x1079),
82 INTEL_E1000_ETHERNET_DEVICE(0x107A),
83 INTEL_E1000_ETHERNET_DEVICE(0x107B),
84 INTEL_E1000_ETHERNET_DEVICE(0x107C),
85 INTEL_E1000_ETHERNET_DEVICE(0x108A),
Jeff Kirsherb7ee49d2006-01-12 16:51:21 -080086 INTEL_E1000_ETHERNET_DEVICE(0x1099),
Jeff Kirsherb7ee49d2006-01-12 16:51:21 -080087 INTEL_E1000_ETHERNET_DEVICE(0x10B5),
Dirk Brandewie5377a412011-01-06 14:29:54 +000088 INTEL_E1000_ETHERNET_DEVICE(0x2E6E),
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 /* required last entry */
90 {0,}
91};
92
93MODULE_DEVICE_TABLE(pci, e1000_pci_tbl);
94
Nicholas Nunley35574762006-09-27 12:53:34 -070095int e1000_up(struct e1000_adapter *adapter);
96void e1000_down(struct e1000_adapter *adapter);
97void e1000_reinit_locked(struct e1000_adapter *adapter);
98void e1000_reset(struct e1000_adapter *adapter);
Nicholas Nunley35574762006-09-27 12:53:34 -070099int e1000_setup_all_tx_resources(struct e1000_adapter *adapter);
100int e1000_setup_all_rx_resources(struct e1000_adapter *adapter);
101void e1000_free_all_tx_resources(struct e1000_adapter *adapter);
102void e1000_free_all_rx_resources(struct e1000_adapter *adapter);
Adrian Bunk3ad2cc62005-10-30 16:53:34 +0100103static int e1000_setup_tx_resources(struct e1000_adapter *adapter,
Nicholas Nunley35574762006-09-27 12:53:34 -0700104 struct e1000_tx_ring *txdr);
Adrian Bunk3ad2cc62005-10-30 16:53:34 +0100105static int e1000_setup_rx_resources(struct e1000_adapter *adapter,
Nicholas Nunley35574762006-09-27 12:53:34 -0700106 struct e1000_rx_ring *rxdr);
Adrian Bunk3ad2cc62005-10-30 16:53:34 +0100107static void e1000_free_tx_resources(struct e1000_adapter *adapter,
Nicholas Nunley35574762006-09-27 12:53:34 -0700108 struct e1000_tx_ring *tx_ring);
Adrian Bunk3ad2cc62005-10-30 16:53:34 +0100109static void e1000_free_rx_resources(struct e1000_adapter *adapter,
Nicholas Nunley35574762006-09-27 12:53:34 -0700110 struct e1000_rx_ring *rx_ring);
111void e1000_update_stats(struct e1000_adapter *adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113static int e1000_init_module(void);
114static void e1000_exit_module(void);
115static int e1000_probe(struct pci_dev *pdev, const struct pci_device_id *ent);
116static void __devexit e1000_remove(struct pci_dev *pdev);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -0400117static int e1000_alloc_queues(struct e1000_adapter *adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118static int e1000_sw_init(struct e1000_adapter *adapter);
119static int e1000_open(struct net_device *netdev);
120static int e1000_close(struct net_device *netdev);
121static void e1000_configure_tx(struct e1000_adapter *adapter);
122static void e1000_configure_rx(struct e1000_adapter *adapter);
123static void e1000_setup_rctl(struct e1000_adapter *adapter);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -0400124static void e1000_clean_all_tx_rings(struct e1000_adapter *adapter);
125static void e1000_clean_all_rx_rings(struct e1000_adapter *adapter);
126static void e1000_clean_tx_ring(struct e1000_adapter *adapter,
127 struct e1000_tx_ring *tx_ring);
128static void e1000_clean_rx_ring(struct e1000_adapter *adapter,
129 struct e1000_rx_ring *rx_ring);
Patrick McHardydb0ce502007-11-13 20:54:59 -0800130static void e1000_set_rx_mode(struct net_device *netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131static void e1000_update_phy_info(unsigned long data);
Jesse Brandeburg5cf42fc2010-09-22 18:22:17 +0000132static void e1000_update_phy_info_task(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133static void e1000_watchdog(unsigned long data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134static void e1000_82547_tx_fifo_stall(unsigned long data);
Jesse Brandeburg5cf42fc2010-09-22 18:22:17 +0000135static void e1000_82547_tx_fifo_stall_task(struct work_struct *work);
Stephen Hemminger3b29a562009-08-31 19:50:55 +0000136static netdev_tx_t e1000_xmit_frame(struct sk_buff *skb,
137 struct net_device *netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138static struct net_device_stats * e1000_get_stats(struct net_device *netdev);
139static int e1000_change_mtu(struct net_device *netdev, int new_mtu);
140static int e1000_set_mac(struct net_device *netdev, void *p);
David Howells7d12e782006-10-05 14:55:46 +0100141static irqreturn_t e1000_intr(int irq, void *data);
Joe Perchesc3033b02008-03-21 11:06:25 -0700142static bool e1000_clean_tx_irq(struct e1000_adapter *adapter,
143 struct e1000_tx_ring *tx_ring);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700144static int e1000_clean(struct napi_struct *napi, int budget);
Joe Perchesc3033b02008-03-21 11:06:25 -0700145static bool e1000_clean_rx_irq(struct e1000_adapter *adapter,
146 struct e1000_rx_ring *rx_ring,
147 int *work_done, int work_to_do);
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +0000148static bool e1000_clean_jumbo_rx_irq(struct e1000_adapter *adapter,
149 struct e1000_rx_ring *rx_ring,
150 int *work_done, int work_to_do);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -0400151static void e1000_alloc_rx_buffers(struct e1000_adapter *adapter,
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +0000152 struct e1000_rx_ring *rx_ring,
Jeff Kirsher72d64a42006-01-12 16:51:19 -0800153 int cleaned_count);
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +0000154static void e1000_alloc_jumbo_rx_buffers(struct e1000_adapter *adapter,
155 struct e1000_rx_ring *rx_ring,
156 int cleaned_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157static int e1000_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd);
158static int e1000_mii_ioctl(struct net_device *netdev, struct ifreq *ifr,
159 int cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160static void e1000_enter_82542_rst(struct e1000_adapter *adapter);
161static void e1000_leave_82542_rst(struct e1000_adapter *adapter);
162static void e1000_tx_timeout(struct net_device *dev);
David Howells65f27f32006-11-22 14:55:48 +0000163static void e1000_reset_task(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164static void e1000_smartspeed(struct e1000_adapter *adapter);
Auke Koke619d522006-04-14 19:04:52 -0700165static int e1000_82547_fifo_workaround(struct e1000_adapter *adapter,
166 struct sk_buff *skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
168static void e1000_vlan_rx_register(struct net_device *netdev, struct vlan_group *grp);
Joe Perches406874a2008-04-03 10:06:32 -0700169static void e1000_vlan_rx_add_vid(struct net_device *netdev, u16 vid);
170static void e1000_vlan_rx_kill_vid(struct net_device *netdev, u16 vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171static void e1000_restore_vlan(struct e1000_adapter *adapter);
172
Auke Kok6fdfef12006-06-27 09:06:36 -0700173#ifdef CONFIG_PM
Rafael J. Wysockib43fcd72009-04-15 17:43:24 +0000174static int e1000_suspend(struct pci_dev *pdev, pm_message_t state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175static int e1000_resume(struct pci_dev *pdev);
176#endif
Auke Kokc653e632006-05-23 13:35:57 -0700177static void e1000_shutdown(struct pci_dev *pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179#ifdef CONFIG_NET_POLL_CONTROLLER
180/* for netdump / net console */
181static void e1000_netpoll (struct net_device *netdev);
182#endif
183
Jesse Brandeburg1f753862006-12-15 10:40:39 +0100184#define COPYBREAK_DEFAULT 256
185static unsigned int copybreak __read_mostly = COPYBREAK_DEFAULT;
186module_param(copybreak, uint, 0644);
187MODULE_PARM_DESC(copybreak,
188 "Maximum size of packet that is copied to a new buffer on receive");
189
Auke Kok90267292006-06-08 09:30:24 -0700190static pci_ers_result_t e1000_io_error_detected(struct pci_dev *pdev,
191 pci_channel_state_t state);
192static pci_ers_result_t e1000_io_slot_reset(struct pci_dev *pdev);
193static void e1000_io_resume(struct pci_dev *pdev);
194
195static struct pci_error_handlers e1000_err_handler = {
196 .error_detected = e1000_io_error_detected,
197 .slot_reset = e1000_io_slot_reset,
198 .resume = e1000_io_resume,
199};
Mallikarjuna R Chilakala24025e42005-10-04 07:03:23 -0400200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201static struct pci_driver e1000_driver = {
202 .name = e1000_driver_name,
203 .id_table = e1000_pci_tbl,
204 .probe = e1000_probe,
205 .remove = __devexit_p(e1000_remove),
Auke Kokc4e24f02006-09-27 12:53:19 -0700206#ifdef CONFIG_PM
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300207 /* Power Management Hooks */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 .suspend = e1000_suspend,
Auke Kokc653e632006-05-23 13:35:57 -0700209 .resume = e1000_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210#endif
Auke Kok90267292006-06-08 09:30:24 -0700211 .shutdown = e1000_shutdown,
212 .err_handler = &e1000_err_handler
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213};
214
215MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
216MODULE_DESCRIPTION("Intel(R) PRO/1000 Network Driver");
217MODULE_LICENSE("GPL");
218MODULE_VERSION(DRV_VERSION);
219
220static int debug = NETIF_MSG_DRV | NETIF_MSG_PROBE;
221module_param(debug, int, 0);
222MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
223
224/**
Emil Tantilov675ad472010-04-27 14:02:58 +0000225 * e1000_get_hw_dev - return device
226 * used by hardware layer to print debugging information
227 *
228 **/
229struct net_device *e1000_get_hw_dev(struct e1000_hw *hw)
230{
231 struct e1000_adapter *adapter = hw->back;
232 return adapter->netdev;
233}
234
235/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 * e1000_init_module - Driver Registration Routine
237 *
238 * e1000_init_module is the first routine called when the driver is
239 * loaded. All it does is register with the PCI subsystem.
240 **/
241
Joe Perches64798842008-07-11 15:17:02 -0700242static int __init e1000_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
244 int ret;
Emil Tantilov675ad472010-04-27 14:02:58 +0000245 pr_info("%s - version %s\n", e1000_driver_string, e1000_driver_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Emil Tantilov675ad472010-04-27 14:02:58 +0000247 pr_info("%s\n", e1000_copyright);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Jeff Garzik29917622006-08-19 17:48:59 -0400249 ret = pci_register_driver(&e1000_driver);
Jesse Brandeburg1f753862006-12-15 10:40:39 +0100250 if (copybreak != COPYBREAK_DEFAULT) {
251 if (copybreak == 0)
Emil Tantilov675ad472010-04-27 14:02:58 +0000252 pr_info("copybreak disabled\n");
Jesse Brandeburg1f753862006-12-15 10:40:39 +0100253 else
Emil Tantilov675ad472010-04-27 14:02:58 +0000254 pr_info("copybreak enabled for "
255 "packets <= %u bytes\n", copybreak);
Jesse Brandeburg1f753862006-12-15 10:40:39 +0100256 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 return ret;
258}
259
260module_init(e1000_init_module);
261
262/**
263 * e1000_exit_module - Driver Exit Cleanup Routine
264 *
265 * e1000_exit_module is called just before the driver is removed
266 * from memory.
267 **/
268
Joe Perches64798842008-07-11 15:17:02 -0700269static void __exit e1000_exit_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 pci_unregister_driver(&e1000_driver);
272}
273
274module_exit(e1000_exit_module);
275
Auke Kok2db10a02006-06-27 09:06:28 -0700276static int e1000_request_irq(struct e1000_adapter *adapter)
277{
278 struct net_device *netdev = adapter->netdev;
Al Viro3e188262007-12-11 19:49:39 +0000279 irq_handler_t handler = e1000_intr;
Auke Koke94bd232007-05-16 01:49:46 -0700280 int irq_flags = IRQF_SHARED;
281 int err;
Auke Kok2db10a02006-06-27 09:06:28 -0700282
Auke Koke94bd232007-05-16 01:49:46 -0700283 err = request_irq(adapter->pdev->irq, handler, irq_flags, netdev->name,
284 netdev);
285 if (err) {
Emil Tantilovfeb8f472010-07-26 23:37:21 -0700286 e_err(probe, "Unable to allocate interrupt Error: %d\n", err);
Auke Koke94bd232007-05-16 01:49:46 -0700287 }
Auke Kok2db10a02006-06-27 09:06:28 -0700288
289 return err;
290}
291
292static void e1000_free_irq(struct e1000_adapter *adapter)
293{
294 struct net_device *netdev = adapter->netdev;
295
296 free_irq(adapter->pdev->irq, netdev);
Auke Kok2db10a02006-06-27 09:06:28 -0700297}
298
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299/**
300 * e1000_irq_disable - Mask off interrupt generation on the NIC
301 * @adapter: board private structure
302 **/
303
Joe Perches64798842008-07-11 15:17:02 -0700304static void e1000_irq_disable(struct e1000_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305{
Joe Perches1dc32912008-07-11 15:17:08 -0700306 struct e1000_hw *hw = &adapter->hw;
307
308 ew32(IMC, ~0);
309 E1000_WRITE_FLUSH();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 synchronize_irq(adapter->pdev->irq);
311}
312
313/**
314 * e1000_irq_enable - Enable default interrupt generation settings
315 * @adapter: board private structure
316 **/
317
Joe Perches64798842008-07-11 15:17:02 -0700318static void e1000_irq_enable(struct e1000_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319{
Joe Perches1dc32912008-07-11 15:17:08 -0700320 struct e1000_hw *hw = &adapter->hw;
321
322 ew32(IMS, IMS_ENABLE_MASK);
323 E1000_WRITE_FLUSH();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324}
Adrian Bunk3ad2cc62005-10-30 16:53:34 +0100325
Joe Perches64798842008-07-11 15:17:02 -0700326static void e1000_update_mng_vlan(struct e1000_adapter *adapter)
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700327{
Joe Perches1dc32912008-07-11 15:17:08 -0700328 struct e1000_hw *hw = &adapter->hw;
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700329 struct net_device *netdev = adapter->netdev;
Joe Perches1dc32912008-07-11 15:17:08 -0700330 u16 vid = hw->mng_cookie.vlan_id;
Joe Perches406874a2008-04-03 10:06:32 -0700331 u16 old_vid = adapter->mng_vlan_id;
Jesse Brandeburg96838a42006-01-18 13:01:39 -0800332 if (adapter->vlgrp) {
Dan Aloni5c15bde2007-03-02 20:44:51 -0800333 if (!vlan_group_get_device(adapter->vlgrp, vid)) {
Joe Perches1dc32912008-07-11 15:17:08 -0700334 if (hw->mng_cookie.status &
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700335 E1000_MNG_DHCP_COOKIE_STATUS_VLAN_SUPPORT) {
336 e1000_vlan_rx_add_vid(netdev, vid);
337 adapter->mng_vlan_id = vid;
338 } else
339 adapter->mng_vlan_id = E1000_MNG_VLAN_NONE;
Jesse Brandeburg96838a42006-01-18 13:01:39 -0800340
Joe Perches406874a2008-04-03 10:06:32 -0700341 if ((old_vid != (u16)E1000_MNG_VLAN_NONE) &&
Jesse Brandeburg96838a42006-01-18 13:01:39 -0800342 (vid != old_vid) &&
Dan Aloni5c15bde2007-03-02 20:44:51 -0800343 !vlan_group_get_device(adapter->vlgrp, old_vid))
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700344 e1000_vlan_rx_kill_vid(netdev, old_vid);
Jeff Kirsherc5f226f2006-03-02 18:17:55 -0800345 } else
346 adapter->mng_vlan_id = vid;
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700347 }
348}
Jeff Kirsherb55ccb32006-01-12 16:50:30 -0800349
Joe Perches64798842008-07-11 15:17:02 -0700350static void e1000_init_manageability(struct e1000_adapter *adapter)
Jeff Garzik0fccd0e2006-12-15 10:56:10 -0500351{
Joe Perches1dc32912008-07-11 15:17:08 -0700352 struct e1000_hw *hw = &adapter->hw;
353
Jeff Garzik0fccd0e2006-12-15 10:56:10 -0500354 if (adapter->en_mng_pt) {
Joe Perches1dc32912008-07-11 15:17:08 -0700355 u32 manc = er32(MANC);
Jeff Garzik0fccd0e2006-12-15 10:56:10 -0500356
357 /* disable hardware interception of ARP */
358 manc &= ~(E1000_MANC_ARP_EN);
359
Joe Perches1dc32912008-07-11 15:17:08 -0700360 ew32(MANC, manc);
Jeff Garzik0fccd0e2006-12-15 10:56:10 -0500361 }
362}
363
Joe Perches64798842008-07-11 15:17:02 -0700364static void e1000_release_manageability(struct e1000_adapter *adapter)
Jeff Garzik0fccd0e2006-12-15 10:56:10 -0500365{
Joe Perches1dc32912008-07-11 15:17:08 -0700366 struct e1000_hw *hw = &adapter->hw;
367
Jeff Garzik0fccd0e2006-12-15 10:56:10 -0500368 if (adapter->en_mng_pt) {
Joe Perches1dc32912008-07-11 15:17:08 -0700369 u32 manc = er32(MANC);
Jeff Garzik0fccd0e2006-12-15 10:56:10 -0500370
371 /* re-enable hardware interception of ARP */
372 manc |= E1000_MANC_ARP_EN;
373
Joe Perches1dc32912008-07-11 15:17:08 -0700374 ew32(MANC, manc);
Jeff Garzik0fccd0e2006-12-15 10:56:10 -0500375 }
376}
377
Auke Koke0aac5a2007-03-06 08:57:21 -0800378/**
379 * e1000_configure - configure the hardware for RX and TX
380 * @adapter = private board structure
381 **/
382static void e1000_configure(struct e1000_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383{
384 struct net_device *netdev = adapter->netdev;
Auke Kok2db10a02006-06-27 09:06:28 -0700385 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Patrick McHardydb0ce502007-11-13 20:54:59 -0800387 e1000_set_rx_mode(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
389 e1000_restore_vlan(adapter);
Jeff Garzik0fccd0e2006-12-15 10:56:10 -0500390 e1000_init_manageability(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
392 e1000_configure_tx(adapter);
393 e1000_setup_rctl(adapter);
394 e1000_configure_rx(adapter);
Jeff Kirsher72d64a42006-01-12 16:51:19 -0800395 /* call E1000_DESC_UNUSED which always leaves
396 * at least 1 descriptor unused to make sure
397 * next_to_use != next_to_clean */
Jeff Kirsherf56799e2006-01-12 16:50:39 -0800398 for (i = 0; i < adapter->num_rx_queues; i++) {
Jeff Kirsher72d64a42006-01-12 16:51:19 -0800399 struct e1000_rx_ring *ring = &adapter->rx_ring[i];
Jeff Kirshera292ca62006-01-12 16:51:30 -0800400 adapter->alloc_rx_buf(adapter, ring,
401 E1000_DESC_UNUSED(ring));
Jeff Kirsherf56799e2006-01-12 16:50:39 -0800402 }
Auke Koke0aac5a2007-03-06 08:57:21 -0800403}
Jeff Kirsher7bfa4812006-01-12 16:50:41 -0800404
Auke Koke0aac5a2007-03-06 08:57:21 -0800405int e1000_up(struct e1000_adapter *adapter)
406{
Joe Perches1dc32912008-07-11 15:17:08 -0700407 struct e1000_hw *hw = &adapter->hw;
408
Auke Koke0aac5a2007-03-06 08:57:21 -0800409 /* hardware has been reset, we need to reload some things */
410 e1000_configure(adapter);
Malli Chilakala5de55622005-04-28 19:39:30 -0700411
Auke Kok1314bbf2006-09-27 12:54:02 -0700412 clear_bit(__E1000_DOWN, &adapter->flags);
413
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700414 napi_enable(&adapter->napi);
Francois Romieuc3570ac2008-07-11 15:17:38 -0700415
Auke Koke0aac5a2007-03-06 08:57:21 -0800416 e1000_irq_enable(adapter);
417
Jesse Brandeburg4cb9be72009-04-21 18:42:05 +0000418 netif_wake_queue(adapter->netdev);
419
Jesse Brandeburg79f3d392006-12-15 10:42:34 +0100420 /* fire a link change interrupt to start the watchdog */
Joe Perches1dc32912008-07-11 15:17:08 -0700421 ew32(ICS, E1000_ICS_LSC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 return 0;
423}
424
Auke Kok79f05bf2006-06-27 09:06:32 -0700425/**
426 * e1000_power_up_phy - restore link in case the phy was powered down
427 * @adapter: address of board private structure
428 *
429 * The phy may be powered down to save power and turn off link when the
430 * driver is unloaded and wake on lan is not enabled (among others)
431 * *** this routine MUST be followed by a call to e1000_reset ***
432 *
433 **/
434
Jesse Brandeburgd6582662006-08-16 13:31:33 -0700435void e1000_power_up_phy(struct e1000_adapter *adapter)
Auke Kok79f05bf2006-06-27 09:06:32 -0700436{
Joe Perches1dc32912008-07-11 15:17:08 -0700437 struct e1000_hw *hw = &adapter->hw;
Joe Perches406874a2008-04-03 10:06:32 -0700438 u16 mii_reg = 0;
Auke Kok79f05bf2006-06-27 09:06:32 -0700439
440 /* Just clear the power down bit to wake the phy back up */
Joe Perches1dc32912008-07-11 15:17:08 -0700441 if (hw->media_type == e1000_media_type_copper) {
Auke Kok79f05bf2006-06-27 09:06:32 -0700442 /* according to the manual, the phy will retain its
443 * settings across a power-down/up cycle */
Joe Perches1dc32912008-07-11 15:17:08 -0700444 e1000_read_phy_reg(hw, PHY_CTRL, &mii_reg);
Auke Kok79f05bf2006-06-27 09:06:32 -0700445 mii_reg &= ~MII_CR_POWER_DOWN;
Joe Perches1dc32912008-07-11 15:17:08 -0700446 e1000_write_phy_reg(hw, PHY_CTRL, mii_reg);
Auke Kok79f05bf2006-06-27 09:06:32 -0700447 }
448}
449
450static void e1000_power_down_phy(struct e1000_adapter *adapter)
451{
Joe Perches1dc32912008-07-11 15:17:08 -0700452 struct e1000_hw *hw = &adapter->hw;
453
Bruce Allan61c25052006-09-27 12:53:54 -0700454 /* Power down the PHY so no link is implied when interface is down *
Joe Perchesc3033b02008-03-21 11:06:25 -0700455 * The PHY cannot be powered down if any of the following is true *
Auke Kok79f05bf2006-06-27 09:06:32 -0700456 * (a) WoL is enabled
457 * (b) AMT is active
458 * (c) SoL/IDER session is active */
Joe Perches1dc32912008-07-11 15:17:08 -0700459 if (!adapter->wol && hw->mac_type >= e1000_82540 &&
460 hw->media_type == e1000_media_type_copper) {
Joe Perches406874a2008-04-03 10:06:32 -0700461 u16 mii_reg = 0;
Bruce Allan61c25052006-09-27 12:53:54 -0700462
Joe Perches1dc32912008-07-11 15:17:08 -0700463 switch (hw->mac_type) {
Bruce Allan61c25052006-09-27 12:53:54 -0700464 case e1000_82540:
465 case e1000_82545:
466 case e1000_82545_rev_3:
467 case e1000_82546:
Dirk Brandewie5377a412011-01-06 14:29:54 +0000468 case e1000_ce4100:
Bruce Allan61c25052006-09-27 12:53:54 -0700469 case e1000_82546_rev_3:
470 case e1000_82541:
471 case e1000_82541_rev_2:
472 case e1000_82547:
473 case e1000_82547_rev_2:
Joe Perches1dc32912008-07-11 15:17:08 -0700474 if (er32(MANC) & E1000_MANC_SMBUS_EN)
Bruce Allan61c25052006-09-27 12:53:54 -0700475 goto out;
476 break;
Bruce Allan61c25052006-09-27 12:53:54 -0700477 default:
478 goto out;
479 }
Joe Perches1dc32912008-07-11 15:17:08 -0700480 e1000_read_phy_reg(hw, PHY_CTRL, &mii_reg);
Auke Kok79f05bf2006-06-27 09:06:32 -0700481 mii_reg |= MII_CR_POWER_DOWN;
Joe Perches1dc32912008-07-11 15:17:08 -0700482 e1000_write_phy_reg(hw, PHY_CTRL, mii_reg);
Auke Kok79f05bf2006-06-27 09:06:32 -0700483 mdelay(1);
484 }
Bruce Allan61c25052006-09-27 12:53:54 -0700485out:
486 return;
Auke Kok79f05bf2006-06-27 09:06:32 -0700487}
488
Joe Perches64798842008-07-11 15:17:02 -0700489void e1000_down(struct e1000_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
Jesse Brandeburga6c42322009-03-25 21:59:22 +0000491 struct e1000_hw *hw = &adapter->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 struct net_device *netdev = adapter->netdev;
Jesse Brandeburga6c42322009-03-25 21:59:22 +0000493 u32 rctl, tctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Auke Kok1314bbf2006-09-27 12:54:02 -0700495
Jesse Brandeburga6c42322009-03-25 21:59:22 +0000496 /* disable receives in the hardware */
497 rctl = er32(RCTL);
498 ew32(RCTL, rctl & ~E1000_RCTL_EN);
499 /* flush and sleep below */
500
Jesse Brandeburg51851072009-09-25 12:17:01 +0000501 netif_tx_disable(netdev);
Jesse Brandeburga6c42322009-03-25 21:59:22 +0000502
503 /* disable transmits in the hardware */
504 tctl = er32(TCTL);
505 tctl &= ~E1000_TCTL_EN;
506 ew32(TCTL, tctl);
507 /* flush both disables and wait for them to finish */
508 E1000_WRITE_FLUSH();
509 msleep(10);
510
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700511 napi_disable(&adapter->napi);
Francois Romieuc3570ac2008-07-11 15:17:38 -0700512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 e1000_irq_disable(adapter);
Jeff Kirsherc1605eb2006-03-02 18:16:38 -0800514
Anupam Chandaab088532010-11-21 09:54:21 -0800515 /*
516 * Setting DOWN must be after irq_disable to prevent
517 * a screaming interrupt. Setting DOWN also prevents
518 * timers and tasks from rescheduling.
519 */
520 set_bit(__E1000_DOWN, &adapter->flags);
521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 del_timer_sync(&adapter->tx_fifo_stall_timer);
523 del_timer_sync(&adapter->watchdog_timer);
524 del_timer_sync(&adapter->phy_info_timer);
525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 adapter->link_speed = 0;
527 adapter->link_duplex = 0;
528 netif_carrier_off(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
530 e1000_reset(adapter);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -0400531 e1000_clean_all_tx_rings(adapter);
532 e1000_clean_all_rx_rings(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
stephen hemminger38df7a32010-10-21 07:50:57 +0000535static void e1000_reinit_safe(struct e1000_adapter *adapter)
Jesse Brandeburg338c15e2010-09-22 18:22:42 +0000536{
537 while (test_and_set_bit(__E1000_RESETTING, &adapter->flags))
538 msleep(1);
539 rtnl_lock();
540 e1000_down(adapter);
541 e1000_up(adapter);
542 rtnl_unlock();
543 clear_bit(__E1000_RESETTING, &adapter->flags);
544}
545
Joe Perches64798842008-07-11 15:17:02 -0700546void e1000_reinit_locked(struct e1000_adapter *adapter)
Auke Kok2db10a02006-06-27 09:06:28 -0700547{
Jesse Brandeburg338c15e2010-09-22 18:22:42 +0000548 /* if rtnl_lock is not held the call path is bogus */
549 ASSERT_RTNL();
Auke Kok2db10a02006-06-27 09:06:28 -0700550 WARN_ON(in_interrupt());
551 while (test_and_set_bit(__E1000_RESETTING, &adapter->flags))
552 msleep(1);
553 e1000_down(adapter);
554 e1000_up(adapter);
555 clear_bit(__E1000_RESETTING, &adapter->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556}
557
Joe Perches64798842008-07-11 15:17:02 -0700558void e1000_reset(struct e1000_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
Joe Perches1dc32912008-07-11 15:17:08 -0700560 struct e1000_hw *hw = &adapter->hw;
Joe Perches406874a2008-04-03 10:06:32 -0700561 u32 pba = 0, tx_space, min_tx_space, min_rx_space;
Joe Perchesc3033b02008-03-21 11:06:25 -0700562 bool legacy_pba_adjust = false;
Jesse Brandeburgb7cb8c22009-07-06 10:45:01 +0000563 u16 hwm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
565 /* Repartition Pba for greater than 9k mtu
566 * To take effect CTRL.RST is required.
567 */
568
Joe Perches1dc32912008-07-11 15:17:08 -0700569 switch (hw->mac_type) {
Bruce Allan018ea442006-12-15 10:39:45 +0100570 case e1000_82542_rev2_0:
571 case e1000_82542_rev2_1:
572 case e1000_82543:
573 case e1000_82544:
574 case e1000_82540:
575 case e1000_82541:
576 case e1000_82541_rev_2:
Joe Perchesc3033b02008-03-21 11:06:25 -0700577 legacy_pba_adjust = true;
Bruce Allan018ea442006-12-15 10:39:45 +0100578 pba = E1000_PBA_48K;
579 break;
580 case e1000_82545:
581 case e1000_82545_rev_3:
582 case e1000_82546:
Dirk Brandewie5377a412011-01-06 14:29:54 +0000583 case e1000_ce4100:
Bruce Allan018ea442006-12-15 10:39:45 +0100584 case e1000_82546_rev_3:
585 pba = E1000_PBA_48K;
586 break;
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700587 case e1000_82547:
Malli Chilakala0e6ef3e2005-04-28 19:44:14 -0700588 case e1000_82547_rev_2:
Joe Perchesc3033b02008-03-21 11:06:25 -0700589 legacy_pba_adjust = true;
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700590 pba = E1000_PBA_30K;
591 break;
Bruce Allan018ea442006-12-15 10:39:45 +0100592 case e1000_undefined:
593 case e1000_num_macs:
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700594 break;
595 }
596
Joe Perchesc3033b02008-03-21 11:06:25 -0700597 if (legacy_pba_adjust) {
Jesse Brandeburgb7cb8c22009-07-06 10:45:01 +0000598 if (hw->max_frame_size > E1000_RXBUFFER_8192)
Bruce Allan018ea442006-12-15 10:39:45 +0100599 pba -= 8; /* allocate more FIFO for Tx */
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700600
Joe Perches1dc32912008-07-11 15:17:08 -0700601 if (hw->mac_type == e1000_82547) {
Bruce Allan018ea442006-12-15 10:39:45 +0100602 adapter->tx_fifo_head = 0;
603 adapter->tx_head_addr = pba << E1000_TX_HEAD_ADDR_SHIFT;
604 adapter->tx_fifo_size =
605 (E1000_PBA_40K - pba) << E1000_PBA_BYTES_SHIFT;
606 atomic_set(&adapter->tx_fifo_stall, 0);
607 }
Jesse Brandeburgb7cb8c22009-07-06 10:45:01 +0000608 } else if (hw->max_frame_size > ETH_FRAME_LEN + ETH_FCS_LEN) {
Bruce Allan018ea442006-12-15 10:39:45 +0100609 /* adjust PBA for jumbo frames */
Joe Perches1dc32912008-07-11 15:17:08 -0700610 ew32(PBA, pba);
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700611
Bruce Allan018ea442006-12-15 10:39:45 +0100612 /* To maintain wire speed transmits, the Tx FIFO should be
Jesse Brandeburgb7cb8c22009-07-06 10:45:01 +0000613 * large enough to accommodate two full transmit packets,
Bruce Allan018ea442006-12-15 10:39:45 +0100614 * rounded up to the next 1KB and expressed in KB. Likewise,
Jesse Brandeburgb7cb8c22009-07-06 10:45:01 +0000615 * the Rx FIFO should be large enough to accommodate at least
Bruce Allan018ea442006-12-15 10:39:45 +0100616 * one full receive packet and is similarly rounded up and
617 * expressed in KB. */
Joe Perches1dc32912008-07-11 15:17:08 -0700618 pba = er32(PBA);
Bruce Allan018ea442006-12-15 10:39:45 +0100619 /* upper 16 bits has Tx packet buffer allocation size in KB */
620 tx_space = pba >> 16;
621 /* lower 16 bits has Rx packet buffer allocation size in KB */
622 pba &= 0xffff;
Jesse Brandeburgb7cb8c22009-07-06 10:45:01 +0000623 /*
624 * the tx fifo also stores 16 bytes of information about the tx
625 * but don't include ethernet FCS because hardware appends it
626 */
627 min_tx_space = (hw->max_frame_size +
628 sizeof(struct e1000_tx_desc) -
629 ETH_FCS_LEN) * 2;
Milind Arun Choudhary9099cfb2007-04-27 13:55:29 -0700630 min_tx_space = ALIGN(min_tx_space, 1024);
Bruce Allan018ea442006-12-15 10:39:45 +0100631 min_tx_space >>= 10;
Jesse Brandeburgb7cb8c22009-07-06 10:45:01 +0000632 /* software strips receive CRC, so leave room for it */
633 min_rx_space = hw->max_frame_size;
Milind Arun Choudhary9099cfb2007-04-27 13:55:29 -0700634 min_rx_space = ALIGN(min_rx_space, 1024);
Bruce Allan018ea442006-12-15 10:39:45 +0100635 min_rx_space >>= 10;
636
637 /* If current Tx allocation is less than the min Tx FIFO size,
638 * and the min Tx FIFO size is less than the current Rx FIFO
639 * allocation, take space away from current Rx allocation */
640 if (tx_space < min_tx_space &&
641 ((min_tx_space - tx_space) < pba)) {
642 pba = pba - (min_tx_space - tx_space);
643
644 /* PCI/PCIx hardware has PBA alignment constraints */
Joe Perches1dc32912008-07-11 15:17:08 -0700645 switch (hw->mac_type) {
Bruce Allan018ea442006-12-15 10:39:45 +0100646 case e1000_82545 ... e1000_82546_rev_3:
647 pba &= ~(E1000_PBA_8K - 1);
648 break;
649 default:
650 break;
651 }
652
653 /* if short on rx space, rx wins and must trump tx
654 * adjustment or use Early Receive if available */
Jesse Brandeburg1532ece2009-09-25 12:16:14 +0000655 if (pba < min_rx_space)
656 pba = min_rx_space;
Bruce Allan018ea442006-12-15 10:39:45 +0100657 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 }
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700659
Joe Perches1dc32912008-07-11 15:17:08 -0700660 ew32(PBA, pba);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
Jesse Brandeburgb7cb8c22009-07-06 10:45:01 +0000662 /*
663 * flow control settings:
664 * The high water mark must be low enough to fit one full frame
665 * (or the size used for early receive) above it in the Rx FIFO.
666 * Set it to the lower of:
667 * - 90% of the Rx FIFO size, and
668 * - the full Rx FIFO size minus the early receive size (for parts
669 * with ERT support assuming ERT set to E1000_ERT_2048), or
670 * - the full Rx FIFO size minus one full frame
671 */
672 hwm = min(((pba << 10) * 9 / 10),
673 ((pba << 10) - hw->max_frame_size));
Jeff Kirsherf11b7f82006-01-12 16:50:51 -0800674
Jesse Brandeburgb7cb8c22009-07-06 10:45:01 +0000675 hw->fc_high_water = hwm & 0xFFF8; /* 8-byte granularity */
676 hw->fc_low_water = hw->fc_high_water - 8;
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +0000677 hw->fc_pause_time = E1000_FC_PAUSE_TIME;
Joe Perches1dc32912008-07-11 15:17:08 -0700678 hw->fc_send_xon = 1;
679 hw->fc = hw->original_fc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700681 /* Allow time for pending master requests to run */
Joe Perches1dc32912008-07-11 15:17:08 -0700682 e1000_reset_hw(hw);
683 if (hw->mac_type >= e1000_82544)
684 ew32(WUC, 0);
Jeff Kirsher09ae3e82006-09-27 12:53:51 -0700685
Joe Perches1dc32912008-07-11 15:17:08 -0700686 if (e1000_init_hw(hw))
Emil Tantilovfeb8f472010-07-26 23:37:21 -0700687 e_dev_err("Hardware Error\n");
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700688 e1000_update_mng_vlan(adapter);
Jesse Brandeburg3d5460a2006-12-15 10:33:46 +0100689
690 /* if (adapter->hwflags & HWFLAGS_PHY_PWR_BIT) { */
Joe Perches1dc32912008-07-11 15:17:08 -0700691 if (hw->mac_type >= e1000_82544 &&
Joe Perches1dc32912008-07-11 15:17:08 -0700692 hw->autoneg == 1 &&
693 hw->autoneg_advertised == ADVERTISE_1000_FULL) {
694 u32 ctrl = er32(CTRL);
Jesse Brandeburg3d5460a2006-12-15 10:33:46 +0100695 /* clear phy power management bit if we are in gig only mode,
696 * which if enabled will attempt negotiation to 100Mb, which
697 * can cause a loss of link at power off or driver unload */
698 ctrl &= ~E1000_CTRL_SWDPIN3;
Joe Perches1dc32912008-07-11 15:17:08 -0700699 ew32(CTRL, ctrl);
Jesse Brandeburg3d5460a2006-12-15 10:33:46 +0100700 }
701
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 /* Enable h/w to recognize an 802.1Q VLAN Ethernet packet */
Joe Perches1dc32912008-07-11 15:17:08 -0700703 ew32(VET, ETHERNET_IEEE_VLAN_TYPE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
Joe Perches1dc32912008-07-11 15:17:08 -0700705 e1000_reset_adaptive(hw);
706 e1000_phy_get_info(hw, &adapter->phy_info);
Auke Kok9a53a202006-06-27 09:06:45 -0700707
Jeff Garzik0fccd0e2006-12-15 10:56:10 -0500708 e1000_release_manageability(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709}
710
711/**
Auke Kok67b3c272007-12-17 13:50:23 -0800712 * Dump the eeprom for users having checksum issues
713 **/
Adrian Bunkb4ea8952008-02-01 08:21:28 -0800714static void e1000_dump_eeprom(struct e1000_adapter *adapter)
Auke Kok67b3c272007-12-17 13:50:23 -0800715{
716 struct net_device *netdev = adapter->netdev;
717 struct ethtool_eeprom eeprom;
718 const struct ethtool_ops *ops = netdev->ethtool_ops;
719 u8 *data;
720 int i;
721 u16 csum_old, csum_new = 0;
722
723 eeprom.len = ops->get_eeprom_len(netdev);
724 eeprom.offset = 0;
725
726 data = kmalloc(eeprom.len, GFP_KERNEL);
727 if (!data) {
Emil Tantilov675ad472010-04-27 14:02:58 +0000728 pr_err("Unable to allocate memory to dump EEPROM data\n");
Auke Kok67b3c272007-12-17 13:50:23 -0800729 return;
730 }
731
732 ops->get_eeprom(netdev, &eeprom, data);
733
734 csum_old = (data[EEPROM_CHECKSUM_REG * 2]) +
735 (data[EEPROM_CHECKSUM_REG * 2 + 1] << 8);
736 for (i = 0; i < EEPROM_CHECKSUM_REG * 2; i += 2)
737 csum_new += data[i] + (data[i + 1] << 8);
738 csum_new = EEPROM_SUM - csum_new;
739
Emil Tantilov675ad472010-04-27 14:02:58 +0000740 pr_err("/*********************/\n");
741 pr_err("Current EEPROM Checksum : 0x%04x\n", csum_old);
742 pr_err("Calculated : 0x%04x\n", csum_new);
Auke Kok67b3c272007-12-17 13:50:23 -0800743
Emil Tantilov675ad472010-04-27 14:02:58 +0000744 pr_err("Offset Values\n");
745 pr_err("======== ======\n");
Auke Kok67b3c272007-12-17 13:50:23 -0800746 print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 16, 1, data, 128, 0);
747
Emil Tantilov675ad472010-04-27 14:02:58 +0000748 pr_err("Include this output when contacting your support provider.\n");
749 pr_err("This is not a software error! Something bad happened to\n");
750 pr_err("your hardware or EEPROM image. Ignoring this problem could\n");
751 pr_err("result in further problems, possibly loss of data,\n");
752 pr_err("corruption or system hangs!\n");
753 pr_err("The MAC Address will be reset to 00:00:00:00:00:00,\n");
754 pr_err("which is invalid and requires you to set the proper MAC\n");
755 pr_err("address manually before continuing to enable this network\n");
756 pr_err("device. Please inspect the EEPROM dump and report the\n");
757 pr_err("issue to your hardware vendor or Intel Customer Support.\n");
758 pr_err("/*********************/\n");
Auke Kok67b3c272007-12-17 13:50:23 -0800759
760 kfree(data);
761}
762
763/**
Taku Izumi81250292008-07-11 15:17:44 -0700764 * e1000_is_need_ioport - determine if an adapter needs ioport resources or not
765 * @pdev: PCI device information struct
766 *
767 * Return true if an adapter needs ioport resources
768 **/
769static int e1000_is_need_ioport(struct pci_dev *pdev)
770{
771 switch (pdev->device) {
772 case E1000_DEV_ID_82540EM:
773 case E1000_DEV_ID_82540EM_LOM:
774 case E1000_DEV_ID_82540EP:
775 case E1000_DEV_ID_82540EP_LOM:
776 case E1000_DEV_ID_82540EP_LP:
777 case E1000_DEV_ID_82541EI:
778 case E1000_DEV_ID_82541EI_MOBILE:
779 case E1000_DEV_ID_82541ER:
780 case E1000_DEV_ID_82541ER_LOM:
781 case E1000_DEV_ID_82541GI:
782 case E1000_DEV_ID_82541GI_LF:
783 case E1000_DEV_ID_82541GI_MOBILE:
784 case E1000_DEV_ID_82544EI_COPPER:
785 case E1000_DEV_ID_82544EI_FIBER:
786 case E1000_DEV_ID_82544GC_COPPER:
787 case E1000_DEV_ID_82544GC_LOM:
788 case E1000_DEV_ID_82545EM_COPPER:
789 case E1000_DEV_ID_82545EM_FIBER:
790 case E1000_DEV_ID_82546EB_COPPER:
791 case E1000_DEV_ID_82546EB_FIBER:
792 case E1000_DEV_ID_82546EB_QUAD_COPPER:
793 return true;
794 default:
795 return false;
796 }
797}
798
Stephen Hemminger0e7614b2008-11-19 22:18:22 -0800799static const struct net_device_ops e1000_netdev_ops = {
800 .ndo_open = e1000_open,
801 .ndo_stop = e1000_close,
Stephen Hemminger00829822008-11-20 20:14:53 -0800802 .ndo_start_xmit = e1000_xmit_frame,
Stephen Hemminger0e7614b2008-11-19 22:18:22 -0800803 .ndo_get_stats = e1000_get_stats,
804 .ndo_set_rx_mode = e1000_set_rx_mode,
805 .ndo_set_mac_address = e1000_set_mac,
806 .ndo_tx_timeout = e1000_tx_timeout,
807 .ndo_change_mtu = e1000_change_mtu,
808 .ndo_do_ioctl = e1000_ioctl,
809 .ndo_validate_addr = eth_validate_addr,
810
811 .ndo_vlan_rx_register = e1000_vlan_rx_register,
812 .ndo_vlan_rx_add_vid = e1000_vlan_rx_add_vid,
813 .ndo_vlan_rx_kill_vid = e1000_vlan_rx_kill_vid,
814#ifdef CONFIG_NET_POLL_CONTROLLER
815 .ndo_poll_controller = e1000_netpoll,
816#endif
817};
818
Taku Izumi81250292008-07-11 15:17:44 -0700819/**
Jesse Brandeburge508be12010-09-07 21:01:12 +0000820 * e1000_init_hw_struct - initialize members of hw struct
821 * @adapter: board private struct
822 * @hw: structure used by e1000_hw.c
823 *
824 * Factors out initialization of the e1000_hw struct to its own function
825 * that can be called very early at init (just after struct allocation).
826 * Fields are initialized based on PCI device information and
827 * OS network device settings (MTU size).
828 * Returns negative error codes if MAC type setup fails.
829 */
830static int e1000_init_hw_struct(struct e1000_adapter *adapter,
831 struct e1000_hw *hw)
832{
833 struct pci_dev *pdev = adapter->pdev;
834
835 /* PCI config space info */
836 hw->vendor_id = pdev->vendor;
837 hw->device_id = pdev->device;
838 hw->subsystem_vendor_id = pdev->subsystem_vendor;
839 hw->subsystem_id = pdev->subsystem_device;
840 hw->revision_id = pdev->revision;
841
842 pci_read_config_word(pdev, PCI_COMMAND, &hw->pci_cmd_word);
843
844 hw->max_frame_size = adapter->netdev->mtu +
845 ENET_HEADER_SIZE + ETHERNET_FCS_SIZE;
846 hw->min_frame_size = MINIMUM_ETHERNET_FRAME_SIZE;
847
848 /* identify the MAC */
849 if (e1000_set_mac_type(hw)) {
850 e_err(probe, "Unknown MAC Type\n");
851 return -EIO;
852 }
853
854 switch (hw->mac_type) {
855 default:
856 break;
857 case e1000_82541:
858 case e1000_82547:
859 case e1000_82541_rev_2:
860 case e1000_82547_rev_2:
861 hw->phy_init_script = 1;
862 break;
863 }
864
865 e1000_set_media_type(hw);
866 e1000_get_bus_info(hw);
867
868 hw->wait_autoneg_complete = false;
869 hw->tbi_compatibility_en = true;
870 hw->adaptive_ifs = true;
871
872 /* Copper options */
873
874 if (hw->media_type == e1000_media_type_copper) {
875 hw->mdix = AUTO_ALL_MODES;
876 hw->disable_polarity_correction = false;
877 hw->master_slave = E1000_MASTER_SLAVE;
878 }
879
880 return 0;
881}
882
883/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 * e1000_probe - Device Initialization Routine
885 * @pdev: PCI device information struct
886 * @ent: entry in e1000_pci_tbl
887 *
888 * Returns 0 on success, negative on failure
889 *
890 * e1000_probe initializes an adapter identified by a pci_dev structure.
891 * The OS initialization, configuring of the adapter private structure,
892 * and a hardware reset occur.
893 **/
Joe Perches1dc32912008-07-11 15:17:08 -0700894static int __devinit e1000_probe(struct pci_dev *pdev,
895 const struct pci_device_id *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896{
897 struct net_device *netdev;
898 struct e1000_adapter *adapter;
Joe Perches1dc32912008-07-11 15:17:08 -0700899 struct e1000_hw *hw;
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700900
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 static int cards_found = 0;
Jesse Brandeburg120cd572006-08-31 14:27:46 -0700902 static int global_quad_port_a = 0; /* global ksp3 port a indication */
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700903 int i, err, pci_using_dac;
Joe Perches406874a2008-04-03 10:06:32 -0700904 u16 eeprom_data = 0;
Dirk Brandewie5377a412011-01-06 14:29:54 +0000905 u16 tmp = 0;
Joe Perches406874a2008-04-03 10:06:32 -0700906 u16 eeprom_apme_mask = E1000_EEPROM_APME;
Taku Izumi81250292008-07-11 15:17:44 -0700907 int bars, need_ioport;
Joe Perches0795af52007-10-03 17:59:30 -0700908
Taku Izumi81250292008-07-11 15:17:44 -0700909 /* do not allocate ioport bars when not needed */
910 need_ioport = e1000_is_need_ioport(pdev);
911 if (need_ioport) {
912 bars = pci_select_bars(pdev, IORESOURCE_MEM | IORESOURCE_IO);
913 err = pci_enable_device(pdev);
914 } else {
915 bars = pci_select_bars(pdev, IORESOURCE_MEM);
Karsten Keil4d7155b2009-02-03 15:18:01 -0800916 err = pci_enable_device_mem(pdev);
Taku Izumi81250292008-07-11 15:17:44 -0700917 }
Joe Perchesc7be73b2008-07-11 15:17:28 -0700918 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 return err;
920
Taku Izumi81250292008-07-11 15:17:44 -0700921 err = pci_request_selected_regions(pdev, bars, e1000_driver_name);
Joe Perchesc7be73b2008-07-11 15:17:28 -0700922 if (err)
Vasily Averin6dd62ab2006-08-28 14:56:22 -0700923 goto err_pci_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
925 pci_set_master(pdev);
Nick Nunleydbb5aae2010-02-03 14:49:48 +0000926 err = pci_save_state(pdev);
927 if (err)
928 goto err_alloc_etherdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
Vasily Averin6dd62ab2006-08-28 14:56:22 -0700930 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 netdev = alloc_etherdev(sizeof(struct e1000_adapter));
Vasily Averin6dd62ab2006-08-28 14:56:22 -0700932 if (!netdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 goto err_alloc_etherdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 SET_NETDEV_DEV(netdev, &pdev->dev);
936
937 pci_set_drvdata(pdev, netdev);
Malli Chilakala60490fe2005-06-17 17:41:45 -0700938 adapter = netdev_priv(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 adapter->netdev = netdev;
940 adapter->pdev = pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 adapter->msg_enable = (1 << debug) - 1;
Taku Izumi81250292008-07-11 15:17:44 -0700942 adapter->bars = bars;
943 adapter->need_ioport = need_ioport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
Joe Perches1dc32912008-07-11 15:17:08 -0700945 hw = &adapter->hw;
946 hw->back = adapter;
947
Vasily Averin6dd62ab2006-08-28 14:56:22 -0700948 err = -EIO;
Arjan van de Ven275f1652008-10-20 21:42:39 -0700949 hw->hw_addr = pci_ioremap_bar(pdev, BAR_0);
Joe Perches1dc32912008-07-11 15:17:08 -0700950 if (!hw->hw_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 goto err_ioremap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
Taku Izumi81250292008-07-11 15:17:44 -0700953 if (adapter->need_ioport) {
954 for (i = BAR_1; i <= BAR_5; i++) {
955 if (pci_resource_len(pdev, i) == 0)
956 continue;
957 if (pci_resource_flags(pdev, i) & IORESOURCE_IO) {
958 hw->io_base = pci_resource_start(pdev, i);
959 break;
960 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 }
962 }
963
Jesse Brandeburge508be12010-09-07 21:01:12 +0000964 /* make ready for any if (hw->...) below */
965 err = e1000_init_hw_struct(adapter, hw);
966 if (err)
967 goto err_sw_init;
968
969 /*
970 * there is a workaround being applied below that limits
971 * 64-bit DMA addresses to 64-bit hardware. There are some
972 * 32-bit adapters that Tx hang when given 64-bit DMA addresses
973 */
974 pci_using_dac = 0;
975 if ((hw->bus_type == e1000_bus_type_pcix) &&
976 !dma_set_mask(&pdev->dev, DMA_BIT_MASK(64))) {
977 /*
978 * according to DMA-API-HOWTO, coherent calls will always
979 * succeed if the set call did
980 */
981 dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(64));
982 pci_using_dac = 1;
Jesse Brandeburge508be12010-09-07 21:01:12 +0000983 } else {
Dean Nelson19a0b672010-11-11 05:50:25 +0000984 err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
985 if (err) {
986 pr_err("No usable DMA config, aborting\n");
987 goto err_dma;
988 }
989 dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
Jesse Brandeburge508be12010-09-07 21:01:12 +0000990 }
991
Stephen Hemminger0e7614b2008-11-19 22:18:22 -0800992 netdev->netdev_ops = &e1000_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 e1000_set_ethtool_ops(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 netdev->watchdog_timeo = 5 * HZ;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700995 netif_napi_add(netdev, &adapter->napi, e1000_clean, 64);
Stephen Hemminger0e7614b2008-11-19 22:18:22 -0800996
Auke Kok0eb5a342006-09-27 12:53:17 -0700997 strncpy(netdev->name, pci_name(pdev), sizeof(netdev->name) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 adapter->bd_number = cards_found;
1000
1001 /* setup the private structure */
1002
Joe Perchesc7be73b2008-07-11 15:17:28 -07001003 err = e1000_sw_init(adapter);
1004 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 goto err_sw_init;
1006
Vasily Averin6dd62ab2006-08-28 14:56:22 -07001007 err = -EIO;
Dirk Brandewie5377a412011-01-06 14:29:54 +00001008 if (hw->mac_type == e1000_ce4100) {
1009 ce4100_gbe_mdio_base_phy = pci_resource_start(pdev, BAR_1);
1010 ce4100_gbe_mdio_base_virt = ioremap(ce4100_gbe_mdio_base_phy,
1011 pci_resource_len(pdev, BAR_1));
1012
1013 if (!ce4100_gbe_mdio_base_virt)
1014 goto err_mdio_ioremap;
1015 }
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001016
Joe Perches1dc32912008-07-11 15:17:08 -07001017 if (hw->mac_type >= e1000_82543) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 netdev->features = NETIF_F_SG |
1019 NETIF_F_HW_CSUM |
1020 NETIF_F_HW_VLAN_TX |
1021 NETIF_F_HW_VLAN_RX |
1022 NETIF_F_HW_VLAN_FILTER;
1023 }
1024
Joe Perches1dc32912008-07-11 15:17:08 -07001025 if ((hw->mac_type >= e1000_82544) &&
1026 (hw->mac_type != e1000_82547))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 netdev->features |= NETIF_F_TSO;
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001028
Yi Zou7b872a52010-09-22 17:57:58 +00001029 if (pci_using_dac) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 netdev->features |= NETIF_F_HIGHDMA;
Yi Zou7b872a52010-09-22 17:57:58 +00001031 netdev->vlan_features |= NETIF_F_HIGHDMA;
1032 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
Patrick McHardy20501a62008-10-11 12:25:59 -07001034 netdev->vlan_features |= NETIF_F_TSO;
Patrick McHardy20501a62008-10-11 12:25:59 -07001035 netdev->vlan_features |= NETIF_F_HW_CSUM;
1036 netdev->vlan_features |= NETIF_F_SG;
1037
Joe Perches1dc32912008-07-11 15:17:08 -07001038 adapter->en_mng_pt = e1000_enable_mng_pass_thru(hw);
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001039
Auke Kokcd94dd02006-06-27 09:08:22 -07001040 /* initialize eeprom parameters */
Joe Perches1dc32912008-07-11 15:17:08 -07001041 if (e1000_init_eeprom_params(hw)) {
Emil Tantilovfeb8f472010-07-26 23:37:21 -07001042 e_err(probe, "EEPROM initialization failed\n");
Vasily Averin6dd62ab2006-08-28 14:56:22 -07001043 goto err_eeprom;
Auke Kokcd94dd02006-06-27 09:08:22 -07001044 }
1045
Jesse Brandeburg96838a42006-01-18 13:01:39 -08001046 /* before reading the EEPROM, reset the controller to
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 * put the device in a known good starting state */
Jesse Brandeburg96838a42006-01-18 13:01:39 -08001048
Joe Perches1dc32912008-07-11 15:17:08 -07001049 e1000_reset_hw(hw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
1051 /* make sure the EEPROM is good */
Joe Perches1dc32912008-07-11 15:17:08 -07001052 if (e1000_validate_eeprom_checksum(hw) < 0) {
Emil Tantilovfeb8f472010-07-26 23:37:21 -07001053 e_err(probe, "The EEPROM Checksum Is Not Valid\n");
Auke Kok67b3c272007-12-17 13:50:23 -08001054 e1000_dump_eeprom(adapter);
1055 /*
1056 * set MAC address to all zeroes to invalidate and temporary
1057 * disable this device for the user. This blocks regular
1058 * traffic while still permitting ethtool ioctls from reaching
1059 * the hardware as well as allowing the user to run the
1060 * interface after manually setting a hw addr using
1061 * `ip set address`
1062 */
Joe Perches1dc32912008-07-11 15:17:08 -07001063 memset(hw->mac_addr, 0, netdev->addr_len);
Auke Kok67b3c272007-12-17 13:50:23 -08001064 } else {
1065 /* copy the MAC address out of the EEPROM */
Joe Perches1dc32912008-07-11 15:17:08 -07001066 if (e1000_read_mac_addr(hw))
Emil Tantilovfeb8f472010-07-26 23:37:21 -07001067 e_err(probe, "EEPROM Read Error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 }
Auke Kok67b3c272007-12-17 13:50:23 -08001069 /* don't block initalization here due to bad MAC address */
Joe Perches1dc32912008-07-11 15:17:08 -07001070 memcpy(netdev->dev_addr, hw->mac_addr, netdev->addr_len);
1071 memcpy(netdev->perm_addr, hw->mac_addr, netdev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
Auke Kok67b3c272007-12-17 13:50:23 -08001073 if (!is_valid_ether_addr(netdev->perm_addr))
Emil Tantilovfeb8f472010-07-26 23:37:21 -07001074 e_err(probe, "Invalid MAC Address\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 init_timer(&adapter->tx_fifo_stall_timer);
Joe Perchesc061b182010-08-23 18:20:03 +00001077 adapter->tx_fifo_stall_timer.function = e1000_82547_tx_fifo_stall;
Joe Perchese982f172008-07-11 15:17:18 -07001078 adapter->tx_fifo_stall_timer.data = (unsigned long)adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
1080 init_timer(&adapter->watchdog_timer);
Joe Perchesc061b182010-08-23 18:20:03 +00001081 adapter->watchdog_timer.function = e1000_watchdog;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 adapter->watchdog_timer.data = (unsigned long) adapter;
1083
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 init_timer(&adapter->phy_info_timer);
Joe Perchesc061b182010-08-23 18:20:03 +00001085 adapter->phy_info_timer.function = e1000_update_phy_info;
Joe Perchese982f172008-07-11 15:17:18 -07001086 adapter->phy_info_timer.data = (unsigned long)adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
Jesse Brandeburg5cf42fc2010-09-22 18:22:17 +00001088 INIT_WORK(&adapter->fifo_stall_task, e1000_82547_tx_fifo_stall_task);
David Howells65f27f32006-11-22 14:55:48 +00001089 INIT_WORK(&adapter->reset_task, e1000_reset_task);
Jesse Brandeburg5cf42fc2010-09-22 18:22:17 +00001090 INIT_WORK(&adapter->phy_info_task, e1000_update_phy_info_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 e1000_check_options(adapter);
1093
1094 /* Initial Wake on LAN setting
1095 * If APM wake is enabled in the EEPROM,
1096 * enable the ACPI Magic Packet filter
1097 */
1098
Joe Perches1dc32912008-07-11 15:17:08 -07001099 switch (hw->mac_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 case e1000_82542_rev2_0:
1101 case e1000_82542_rev2_1:
1102 case e1000_82543:
1103 break;
1104 case e1000_82544:
Joe Perches1dc32912008-07-11 15:17:08 -07001105 e1000_read_eeprom(hw,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 EEPROM_INIT_CONTROL2_REG, 1, &eeprom_data);
1107 eeprom_apme_mask = E1000_EEPROM_82544_APM;
1108 break;
1109 case e1000_82546:
1110 case e1000_82546_rev_3:
Joe Perches1dc32912008-07-11 15:17:08 -07001111 if (er32(STATUS) & E1000_STATUS_FUNC_1){
1112 e1000_read_eeprom(hw,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 EEPROM_INIT_CONTROL3_PORT_B, 1, &eeprom_data);
1114 break;
1115 }
1116 /* Fall Through */
1117 default:
Joe Perches1dc32912008-07-11 15:17:08 -07001118 e1000_read_eeprom(hw,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 EEPROM_INIT_CONTROL3_PORT_A, 1, &eeprom_data);
1120 break;
1121 }
Jesse Brandeburg96838a42006-01-18 13:01:39 -08001122 if (eeprom_data & eeprom_apme_mask)
Jesse Brandeburg120cd572006-08-31 14:27:46 -07001123 adapter->eeprom_wol |= E1000_WUFC_MAG;
1124
1125 /* now that we have the eeprom settings, apply the special cases
1126 * where the eeprom may be wrong or the board simply won't support
1127 * wake on lan on a particular port */
1128 switch (pdev->device) {
1129 case E1000_DEV_ID_82546GB_PCIE:
1130 adapter->eeprom_wol = 0;
1131 break;
1132 case E1000_DEV_ID_82546EB_FIBER:
1133 case E1000_DEV_ID_82546GB_FIBER:
Jesse Brandeburg120cd572006-08-31 14:27:46 -07001134 /* Wake events only supported on port A for dual fiber
1135 * regardless of eeprom setting */
Joe Perches1dc32912008-07-11 15:17:08 -07001136 if (er32(STATUS) & E1000_STATUS_FUNC_1)
Jesse Brandeburg120cd572006-08-31 14:27:46 -07001137 adapter->eeprom_wol = 0;
1138 break;
1139 case E1000_DEV_ID_82546GB_QUAD_COPPER_KSP3:
1140 /* if quad port adapter, disable WoL on all but port A */
1141 if (global_quad_port_a != 0)
1142 adapter->eeprom_wol = 0;
1143 else
1144 adapter->quad_port_a = 1;
1145 /* Reset for multiple quad port adapters */
1146 if (++global_quad_port_a == 4)
1147 global_quad_port_a = 0;
1148 break;
1149 }
1150
1151 /* initialize the wol settings based on the eeprom settings */
1152 adapter->wol = adapter->eeprom_wol;
\"Rafael J. Wysocki\de126482008-11-07 20:30:19 +00001153 device_set_wakeup_enable(&adapter->pdev->dev, adapter->wol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
Dirk Brandewie5377a412011-01-06 14:29:54 +00001155 /* Auto detect PHY address */
1156 if (hw->mac_type == e1000_ce4100) {
1157 for (i = 0; i < 32; i++) {
1158 hw->phy_addr = i;
1159 e1000_read_phy_reg(hw, PHY_ID2, &tmp);
1160 if (tmp == 0 || tmp == 0xFF) {
1161 if (i == 31)
1162 goto err_eeprom;
1163 continue;
1164 } else
1165 break;
1166 }
1167 }
1168
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 /* reset the hardware with the new settings */
1170 e1000_reset(adapter);
1171
Auke Kok416b5d12007-06-01 10:22:39 -07001172 strcpy(netdev->name, "eth%d");
Joe Perchesc7be73b2008-07-11 15:17:28 -07001173 err = register_netdev(netdev);
1174 if (err)
Auke Kok416b5d12007-06-01 10:22:39 -07001175 goto err_register;
Auke Kok1314bbf2006-09-27 12:54:02 -07001176
Emil Tantilov675ad472010-04-27 14:02:58 +00001177 /* print bus type/speed/width info */
Emil Tantilovfeb8f472010-07-26 23:37:21 -07001178 e_info(probe, "(PCI%s:%dMHz:%d-bit) %pM\n",
Joe Perches7837e582010-06-11 12:51:49 +00001179 ((hw->bus_type == e1000_bus_type_pcix) ? "-X" : ""),
1180 ((hw->bus_speed == e1000_bus_speed_133) ? 133 :
1181 (hw->bus_speed == e1000_bus_speed_120) ? 120 :
1182 (hw->bus_speed == e1000_bus_speed_100) ? 100 :
1183 (hw->bus_speed == e1000_bus_speed_66) ? 66 : 33),
1184 ((hw->bus_width == e1000_bus_width_64) ? 64 : 32),
1185 netdev->dev_addr);
Emil Tantilov675ad472010-04-27 14:02:58 +00001186
Jesse Brandeburgeb62efd2009-04-17 20:44:36 +00001187 /* carrier off reporting is important to ethtool even BEFORE open */
1188 netif_carrier_off(netdev);
1189
Emil Tantilovfeb8f472010-07-26 23:37:21 -07001190 e_info(probe, "Intel(R) PRO/1000 Network Connection\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
1192 cards_found++;
1193 return 0;
1194
1195err_register:
Vasily Averin6dd62ab2006-08-28 14:56:22 -07001196err_eeprom:
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00001197 e1000_phy_hw_reset(hw);
Vasily Averin6dd62ab2006-08-28 14:56:22 -07001198
Joe Perches1dc32912008-07-11 15:17:08 -07001199 if (hw->flash_address)
1200 iounmap(hw->flash_address);
Vasily Averin6dd62ab2006-08-28 14:56:22 -07001201 kfree(adapter->tx_ring);
1202 kfree(adapter->rx_ring);
Jesse Brandeburge508be12010-09-07 21:01:12 +00001203err_dma:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204err_sw_init:
Dirk Brandewie5377a412011-01-06 14:29:54 +00001205err_mdio_ioremap:
1206 iounmap(ce4100_gbe_mdio_base_virt);
Joe Perches1dc32912008-07-11 15:17:08 -07001207 iounmap(hw->hw_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208err_ioremap:
1209 free_netdev(netdev);
1210err_alloc_etherdev:
Taku Izumi81250292008-07-11 15:17:44 -07001211 pci_release_selected_regions(pdev, bars);
Vasily Averin6dd62ab2006-08-28 14:56:22 -07001212err_pci_reg:
Vasily Averin6dd62ab2006-08-28 14:56:22 -07001213 pci_disable_device(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 return err;
1215}
1216
1217/**
1218 * e1000_remove - Device Removal Routine
1219 * @pdev: PCI device information struct
1220 *
1221 * e1000_remove is called by the PCI subsystem to alert the driver
1222 * that it should release a PCI device. The could be caused by a
1223 * Hot-Plug event, or because the driver is going to be removed from
1224 * memory.
1225 **/
1226
Joe Perches64798842008-07-11 15:17:02 -07001227static void __devexit e1000_remove(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228{
1229 struct net_device *netdev = pci_get_drvdata(pdev);
Malli Chilakala60490fe2005-06-17 17:41:45 -07001230 struct e1000_adapter *adapter = netdev_priv(netdev);
Joe Perches1dc32912008-07-11 15:17:08 -07001231 struct e1000_hw *hw = &adapter->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
Jesse Brandeburgbaa34742009-09-25 12:17:23 +00001233 set_bit(__E1000_DOWN, &adapter->flags);
1234 del_timer_sync(&adapter->tx_fifo_stall_timer);
1235 del_timer_sync(&adapter->watchdog_timer);
1236 del_timer_sync(&adapter->phy_info_timer);
1237
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07001238 cancel_work_sync(&adapter->reset_task);
Jeff Garzikbe2b28e2005-10-04 07:13:43 -04001239
Jeff Garzik0fccd0e2006-12-15 10:56:10 -05001240 e1000_release_manageability(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001242 unregister_netdev(netdev);
1243
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00001244 e1000_phy_hw_reset(hw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
Mallikarjuna R Chilakala24025e42005-10-04 07:03:23 -04001246 kfree(adapter->tx_ring);
1247 kfree(adapter->rx_ring);
Mallikarjuna R Chilakala24025e42005-10-04 07:03:23 -04001248
Joe Perches1dc32912008-07-11 15:17:08 -07001249 iounmap(hw->hw_addr);
1250 if (hw->flash_address)
1251 iounmap(hw->flash_address);
Taku Izumi81250292008-07-11 15:17:44 -07001252 pci_release_selected_regions(pdev, adapter->bars);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
1254 free_netdev(netdev);
1255
1256 pci_disable_device(pdev);
1257}
1258
1259/**
1260 * e1000_sw_init - Initialize general software structures (struct e1000_adapter)
1261 * @adapter: board private structure to initialize
1262 *
1263 * e1000_sw_init initializes the Adapter private data structure.
Jesse Brandeburge508be12010-09-07 21:01:12 +00001264 * e1000_init_hw_struct MUST be called before this function
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 **/
1266
Joe Perches64798842008-07-11 15:17:02 -07001267static int __devinit e1000_sw_init(struct e1000_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268{
Auke Kokeb0f8052006-07-14 16:14:48 -07001269 adapter->rx_buffer_len = MAXIMUM_ETHERNET_VLAN_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
Jeff Kirsherf56799e2006-01-12 16:50:39 -08001271 adapter->num_tx_queues = 1;
1272 adapter->num_rx_queues = 1;
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001273
1274 if (e1000_alloc_queues(adapter)) {
Emil Tantilovfeb8f472010-07-26 23:37:21 -07001275 e_err(probe, "Unable to allocate memory for queues\n");
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001276 return -ENOMEM;
1277 }
1278
Herbert Xu47313052007-05-29 15:07:31 -07001279 /* Explicitly disable IRQ since the NIC can be in any state. */
Herbert Xu47313052007-05-29 15:07:31 -07001280 e1000_irq_disable(adapter);
1281
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 spin_lock_init(&adapter->stats_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
Auke Kok1314bbf2006-09-27 12:54:02 -07001284 set_bit(__E1000_DOWN, &adapter->flags);
1285
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 return 0;
1287}
1288
1289/**
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001290 * e1000_alloc_queues - Allocate memory for all rings
1291 * @adapter: board private structure to initialize
1292 *
1293 * We allocate one ring per queue at run-time since we don't know the
Wang Chen3e1d7cd2008-12-03 22:07:10 -08001294 * number of queues at compile-time.
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001295 **/
1296
Joe Perches64798842008-07-11 15:17:02 -07001297static int __devinit e1000_alloc_queues(struct e1000_adapter *adapter)
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001298{
Yan Burman1c7e5b12007-03-06 08:58:04 -08001299 adapter->tx_ring = kcalloc(adapter->num_tx_queues,
1300 sizeof(struct e1000_tx_ring), GFP_KERNEL);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001301 if (!adapter->tx_ring)
1302 return -ENOMEM;
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001303
Yan Burman1c7e5b12007-03-06 08:58:04 -08001304 adapter->rx_ring = kcalloc(adapter->num_rx_queues,
1305 sizeof(struct e1000_rx_ring), GFP_KERNEL);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001306 if (!adapter->rx_ring) {
1307 kfree(adapter->tx_ring);
1308 return -ENOMEM;
1309 }
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001310
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001311 return E1000_SUCCESS;
1312}
1313
1314/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 * e1000_open - Called when a network interface is made active
1316 * @netdev: network interface device structure
1317 *
1318 * Returns 0 on success, negative value on failure
1319 *
1320 * The open entry point is called when a network interface is made
1321 * active by the system (IFF_UP). At this point all resources needed
1322 * for transmit and receive operations are allocated, the interrupt
1323 * handler is registered with the OS, the watchdog timer is started,
1324 * and the stack is notified that the interface is ready.
1325 **/
1326
Joe Perches64798842008-07-11 15:17:02 -07001327static int e1000_open(struct net_device *netdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328{
Malli Chilakala60490fe2005-06-17 17:41:45 -07001329 struct e1000_adapter *adapter = netdev_priv(netdev);
Joe Perches1dc32912008-07-11 15:17:08 -07001330 struct e1000_hw *hw = &adapter->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 int err;
1332
Auke Kok2db10a02006-06-27 09:06:28 -07001333 /* disallow open during test */
Auke Kok1314bbf2006-09-27 12:54:02 -07001334 if (test_bit(__E1000_TESTING, &adapter->flags))
Auke Kok2db10a02006-06-27 09:06:28 -07001335 return -EBUSY;
1336
Jesse Brandeburgeb62efd2009-04-17 20:44:36 +00001337 netif_carrier_off(netdev);
1338
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 /* allocate transmit descriptors */
Auke Koke0aac5a2007-03-06 08:57:21 -08001340 err = e1000_setup_all_tx_resources(adapter);
1341 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 goto err_setup_tx;
1343
1344 /* allocate receive descriptors */
Auke Koke0aac5a2007-03-06 08:57:21 -08001345 err = e1000_setup_all_rx_resources(adapter);
Linus Torvaldsb5bf28c2007-02-21 11:21:44 -08001346 if (err)
Auke Koke0aac5a2007-03-06 08:57:21 -08001347 goto err_setup_rx;
Linus Torvaldsb5bf28c2007-02-21 11:21:44 -08001348
Auke Kok79f05bf2006-06-27 09:06:32 -07001349 e1000_power_up_phy(adapter);
1350
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001351 adapter->mng_vlan_id = E1000_MNG_VLAN_NONE;
Joe Perches1dc32912008-07-11 15:17:08 -07001352 if ((hw->mng_cookie.status &
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001353 E1000_MNG_DHCP_COOKIE_STATUS_VLAN_SUPPORT)) {
1354 e1000_update_mng_vlan(adapter);
1355 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
Auke Koke0aac5a2007-03-06 08:57:21 -08001357 /* before we allocate an interrupt, we must be ready to handle it.
1358 * Setting DEBUG_SHIRQ in the kernel makes it fire an interrupt
1359 * as soon as we call pci_request_irq, so we have to setup our
1360 * clean_rx handler before we do so. */
1361 e1000_configure(adapter);
1362
1363 err = e1000_request_irq(adapter);
1364 if (err)
1365 goto err_req_irq;
1366
1367 /* From here on the code is the same as e1000_up() */
1368 clear_bit(__E1000_DOWN, &adapter->flags);
1369
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001370 napi_enable(&adapter->napi);
Herbert Xu47313052007-05-29 15:07:31 -07001371
Auke Koke0aac5a2007-03-06 08:57:21 -08001372 e1000_irq_enable(adapter);
1373
Ben Hutchings076152d2008-07-18 17:50:57 -07001374 netif_start_queue(netdev);
1375
Auke Koke0aac5a2007-03-06 08:57:21 -08001376 /* fire a link status change interrupt to start the watchdog */
Joe Perches1dc32912008-07-11 15:17:08 -07001377 ew32(ICS, E1000_ICS_LSC);
Auke Koke0aac5a2007-03-06 08:57:21 -08001378
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 return E1000_SUCCESS;
1380
Linus Torvaldsb5bf28c2007-02-21 11:21:44 -08001381err_req_irq:
Auke Koke0aac5a2007-03-06 08:57:21 -08001382 e1000_power_down_phy(adapter);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001383 e1000_free_all_rx_resources(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384err_setup_rx:
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001385 e1000_free_all_tx_resources(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386err_setup_tx:
1387 e1000_reset(adapter);
1388
1389 return err;
1390}
1391
1392/**
1393 * e1000_close - Disables a network interface
1394 * @netdev: network interface device structure
1395 *
1396 * Returns 0, this is not allowed to fail
1397 *
1398 * The close entry point is called when an interface is de-activated
1399 * by the OS. The hardware is still under the drivers control, but
1400 * needs to be disabled. A global MAC reset is issued to stop the
1401 * hardware, and all transmit and receive resources are freed.
1402 **/
1403
Joe Perches64798842008-07-11 15:17:02 -07001404static int e1000_close(struct net_device *netdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405{
Malli Chilakala60490fe2005-06-17 17:41:45 -07001406 struct e1000_adapter *adapter = netdev_priv(netdev);
Joe Perches1dc32912008-07-11 15:17:08 -07001407 struct e1000_hw *hw = &adapter->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
Auke Kok2db10a02006-06-27 09:06:28 -07001409 WARN_ON(test_bit(__E1000_RESETTING, &adapter->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 e1000_down(adapter);
Auke Kok79f05bf2006-06-27 09:06:32 -07001411 e1000_power_down_phy(adapter);
Auke Kok2db10a02006-06-27 09:06:28 -07001412 e1000_free_irq(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001414 e1000_free_all_tx_resources(adapter);
1415 e1000_free_all_rx_resources(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
Bruce Allan46665602006-09-27 12:54:08 -07001417 /* kill manageability vlan ID if supported, but not if a vlan with
1418 * the same ID is registered on the host OS (let 8021q kill it) */
Joe Perches1dc32912008-07-11 15:17:08 -07001419 if ((hw->mng_cookie.status &
Bruce Allan46665602006-09-27 12:54:08 -07001420 E1000_MNG_DHCP_COOKIE_STATUS_VLAN_SUPPORT) &&
1421 !(adapter->vlgrp &&
Dan Aloni5c15bde2007-03-02 20:44:51 -08001422 vlan_group_get_device(adapter->vlgrp, adapter->mng_vlan_id))) {
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001423 e1000_vlan_rx_kill_vid(netdev, adapter->mng_vlan_id);
1424 }
Jeff Kirsherb55ccb32006-01-12 16:50:30 -08001425
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 return 0;
1427}
1428
1429/**
1430 * e1000_check_64k_bound - check that memory doesn't cross 64kB boundary
1431 * @adapter: address of board private structure
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001432 * @start: address of beginning of memory
1433 * @len: length of memory
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 **/
Joe Perches64798842008-07-11 15:17:02 -07001435static bool e1000_check_64k_bound(struct e1000_adapter *adapter, void *start,
1436 unsigned long len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437{
Joe Perches1dc32912008-07-11 15:17:08 -07001438 struct e1000_hw *hw = &adapter->hw;
Joe Perchese982f172008-07-11 15:17:18 -07001439 unsigned long begin = (unsigned long)start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 unsigned long end = begin + len;
1441
Malli Chilakala26483452005-04-28 19:44:46 -07001442 /* First rev 82545 and 82546 need to not allow any memory
1443 * write location to cross 64k boundary due to errata 23 */
Joe Perches1dc32912008-07-11 15:17:08 -07001444 if (hw->mac_type == e1000_82545 ||
Dirk Brandewie5377a412011-01-06 14:29:54 +00001445 hw->mac_type == e1000_ce4100 ||
Joe Perches1dc32912008-07-11 15:17:08 -07001446 hw->mac_type == e1000_82546) {
Joe Perchesc3033b02008-03-21 11:06:25 -07001447 return ((begin ^ (end - 1)) >> 16) != 0 ? false : true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 }
1449
Joe Perchesc3033b02008-03-21 11:06:25 -07001450 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451}
1452
1453/**
1454 * e1000_setup_tx_resources - allocate Tx resources (Descriptors)
1455 * @adapter: board private structure
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001456 * @txdr: tx descriptor ring (for a specific queue) to setup
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 *
1458 * Return 0 on success, negative on failure
1459 **/
1460
Joe Perches64798842008-07-11 15:17:02 -07001461static int e1000_setup_tx_resources(struct e1000_adapter *adapter,
1462 struct e1000_tx_ring *txdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 struct pci_dev *pdev = adapter->pdev;
1465 int size;
1466
1467 size = sizeof(struct e1000_buffer) * txdr->count;
Eric Dumazet89bf67f2010-11-22 00:15:06 +00001468 txdr->buffer_info = vzalloc(size);
Jesse Brandeburg96838a42006-01-18 13:01:39 -08001469 if (!txdr->buffer_info) {
Emil Tantilovfeb8f472010-07-26 23:37:21 -07001470 e_err(probe, "Unable to allocate memory for the Tx descriptor "
1471 "ring\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 return -ENOMEM;
1473 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
1475 /* round up to nearest 4K */
1476
1477 txdr->size = txdr->count * sizeof(struct e1000_tx_desc);
Milind Arun Choudhary9099cfb2007-04-27 13:55:29 -07001478 txdr->size = ALIGN(txdr->size, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
Nick Nunleyb16f53b2010-04-27 13:08:45 +00001480 txdr->desc = dma_alloc_coherent(&pdev->dev, txdr->size, &txdr->dma,
1481 GFP_KERNEL);
Jesse Brandeburg96838a42006-01-18 13:01:39 -08001482 if (!txdr->desc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483setup_tx_desc_die:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 vfree(txdr->buffer_info);
Emil Tantilovfeb8f472010-07-26 23:37:21 -07001485 e_err(probe, "Unable to allocate memory for the Tx descriptor "
1486 "ring\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 return -ENOMEM;
1488 }
1489
Malli Chilakala26483452005-04-28 19:44:46 -07001490 /* Fix for errata 23, can't cross 64kB boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 if (!e1000_check_64k_bound(adapter, txdr->desc, txdr->size)) {
1492 void *olddesc = txdr->desc;
1493 dma_addr_t olddma = txdr->dma;
Emil Tantilovfeb8f472010-07-26 23:37:21 -07001494 e_err(tx_err, "txdr align check failed: %u bytes at %p\n",
Emil Tantilov675ad472010-04-27 14:02:58 +00001495 txdr->size, txdr->desc);
Malli Chilakala26483452005-04-28 19:44:46 -07001496 /* Try again, without freeing the previous */
Nick Nunleyb16f53b2010-04-27 13:08:45 +00001497 txdr->desc = dma_alloc_coherent(&pdev->dev, txdr->size,
1498 &txdr->dma, GFP_KERNEL);
Malli Chilakala26483452005-04-28 19:44:46 -07001499 /* Failed allocation, critical failure */
Jesse Brandeburg96838a42006-01-18 13:01:39 -08001500 if (!txdr->desc) {
Nick Nunleyb16f53b2010-04-27 13:08:45 +00001501 dma_free_coherent(&pdev->dev, txdr->size, olddesc,
1502 olddma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 goto setup_tx_desc_die;
1504 }
1505
1506 if (!e1000_check_64k_bound(adapter, txdr->desc, txdr->size)) {
1507 /* give up */
Nick Nunleyb16f53b2010-04-27 13:08:45 +00001508 dma_free_coherent(&pdev->dev, txdr->size, txdr->desc,
1509 txdr->dma);
1510 dma_free_coherent(&pdev->dev, txdr->size, olddesc,
1511 olddma);
Emil Tantilovfeb8f472010-07-26 23:37:21 -07001512 e_err(probe, "Unable to allocate aligned memory "
Emil Tantilov675ad472010-04-27 14:02:58 +00001513 "for the transmit descriptor ring\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 vfree(txdr->buffer_info);
1515 return -ENOMEM;
1516 } else {
Malli Chilakala26483452005-04-28 19:44:46 -07001517 /* Free old allocation, new allocation was successful */
Nick Nunleyb16f53b2010-04-27 13:08:45 +00001518 dma_free_coherent(&pdev->dev, txdr->size, olddesc,
1519 olddma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 }
1521 }
1522 memset(txdr->desc, 0, txdr->size);
1523
1524 txdr->next_to_use = 0;
1525 txdr->next_to_clean = 0;
1526
1527 return 0;
1528}
1529
1530/**
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001531 * e1000_setup_all_tx_resources - wrapper to allocate Tx resources
1532 * (Descriptors) for all queues
1533 * @adapter: board private structure
1534 *
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001535 * Return 0 on success, negative on failure
1536 **/
1537
Joe Perches64798842008-07-11 15:17:02 -07001538int e1000_setup_all_tx_resources(struct e1000_adapter *adapter)
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001539{
1540 int i, err = 0;
1541
Jeff Kirsherf56799e2006-01-12 16:50:39 -08001542 for (i = 0; i < adapter->num_tx_queues; i++) {
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001543 err = e1000_setup_tx_resources(adapter, &adapter->tx_ring[i]);
1544 if (err) {
Emil Tantilovfeb8f472010-07-26 23:37:21 -07001545 e_err(probe, "Allocation for Tx Queue %u failed\n", i);
Vasily Averin3fbbc722006-08-28 14:56:24 -07001546 for (i-- ; i >= 0; i--)
1547 e1000_free_tx_resources(adapter,
1548 &adapter->tx_ring[i]);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001549 break;
1550 }
1551 }
1552
1553 return err;
1554}
1555
1556/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 * e1000_configure_tx - Configure 8254x Transmit Unit after Reset
1558 * @adapter: board private structure
1559 *
1560 * Configure the Tx unit of the MAC after a reset.
1561 **/
1562
Joe Perches64798842008-07-11 15:17:02 -07001563static void e1000_configure_tx(struct e1000_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564{
Joe Perches406874a2008-04-03 10:06:32 -07001565 u64 tdba;
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001566 struct e1000_hw *hw = &adapter->hw;
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00001567 u32 tdlen, tctl, tipg;
Joe Perches406874a2008-04-03 10:06:32 -07001568 u32 ipgr1, ipgr2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
1570 /* Setup the HW Tx Head and Tail descriptor pointers */
1571
Jeff Kirsherf56799e2006-01-12 16:50:39 -08001572 switch (adapter->num_tx_queues) {
Mallikarjuna R Chilakala24025e42005-10-04 07:03:23 -04001573 case 1:
1574 default:
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001575 tdba = adapter->tx_ring[0].dma;
1576 tdlen = adapter->tx_ring[0].count *
1577 sizeof(struct e1000_tx_desc);
Joe Perches1dc32912008-07-11 15:17:08 -07001578 ew32(TDLEN, tdlen);
1579 ew32(TDBAH, (tdba >> 32));
1580 ew32(TDBAL, (tdba & 0x00000000ffffffffULL));
1581 ew32(TDT, 0);
1582 ew32(TDH, 0);
Auke Kok6a951692006-09-11 14:00:21 -07001583 adapter->tx_ring[0].tdh = ((hw->mac_type >= e1000_82543) ? E1000_TDH : E1000_82542_TDH);
1584 adapter->tx_ring[0].tdt = ((hw->mac_type >= e1000_82543) ? E1000_TDT : E1000_82542_TDT);
Mallikarjuna R Chilakala24025e42005-10-04 07:03:23 -04001585 break;
1586 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
1588 /* Set the default values for the Tx Inter Packet Gap timer */
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00001589 if ((hw->media_type == e1000_media_type_fiber ||
Jesse Brandeburgd89b6c62006-12-15 10:38:32 +01001590 hw->media_type == e1000_media_type_internal_serdes))
Jeff Kirsher0fadb052006-01-12 16:51:05 -08001591 tipg = DEFAULT_82543_TIPG_IPGT_FIBER;
1592 else
1593 tipg = DEFAULT_82543_TIPG_IPGT_COPPER;
1594
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001595 switch (hw->mac_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 case e1000_82542_rev2_0:
1597 case e1000_82542_rev2_1:
1598 tipg = DEFAULT_82542_TIPG_IPGT;
Jeff Kirsher0fadb052006-01-12 16:51:05 -08001599 ipgr1 = DEFAULT_82542_TIPG_IPGR1;
1600 ipgr2 = DEFAULT_82542_TIPG_IPGR2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 break;
1602 default:
Jeff Kirsher0fadb052006-01-12 16:51:05 -08001603 ipgr1 = DEFAULT_82543_TIPG_IPGR1;
1604 ipgr2 = DEFAULT_82543_TIPG_IPGR2;
1605 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 }
Jeff Kirsher0fadb052006-01-12 16:51:05 -08001607 tipg |= ipgr1 << E1000_TIPG_IPGR1_SHIFT;
1608 tipg |= ipgr2 << E1000_TIPG_IPGR2_SHIFT;
Joe Perches1dc32912008-07-11 15:17:08 -07001609 ew32(TIPG, tipg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610
1611 /* Set the Tx Interrupt Delay register */
1612
Joe Perches1dc32912008-07-11 15:17:08 -07001613 ew32(TIDV, adapter->tx_int_delay);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001614 if (hw->mac_type >= e1000_82540)
Joe Perches1dc32912008-07-11 15:17:08 -07001615 ew32(TADV, adapter->tx_abs_int_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616
1617 /* Program the Transmit Control Register */
1618
Joe Perches1dc32912008-07-11 15:17:08 -07001619 tctl = er32(TCTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 tctl &= ~E1000_TCTL_CT;
Jeff Kirsher7e6c9862006-03-02 18:19:30 -08001621 tctl |= E1000_TCTL_PSP | E1000_TCTL_RTLC |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 (E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT);
1623
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001624 e1000_config_collision_dist(hw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
1626 /* Setup Transmit Descriptor Settings for eop descriptor */
Jesse Brandeburg6a042da2006-11-01 08:48:04 -08001627 adapter->txd_cmd = E1000_TXD_CMD_EOP | E1000_TXD_CMD_IFCS;
1628
1629 /* only set IDE if we are delaying interrupts using the timers */
1630 if (adapter->tx_int_delay)
1631 adapter->txd_cmd |= E1000_TXD_CMD_IDE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001633 if (hw->mac_type < e1000_82543)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 adapter->txd_cmd |= E1000_TXD_CMD_RPS;
1635 else
1636 adapter->txd_cmd |= E1000_TXD_CMD_RS;
1637
1638 /* Cache if we're 82544 running in PCI-X because we'll
1639 * need this to apply a workaround later in the send path. */
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001640 if (hw->mac_type == e1000_82544 &&
1641 hw->bus_type == e1000_bus_type_pcix)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 adapter->pcix_82544 = 1;
Jeff Kirsher7e6c9862006-03-02 18:19:30 -08001643
Joe Perches1dc32912008-07-11 15:17:08 -07001644 ew32(TCTL, tctl);
Jeff Kirsher7e6c9862006-03-02 18:19:30 -08001645
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646}
1647
1648/**
1649 * e1000_setup_rx_resources - allocate Rx resources (Descriptors)
1650 * @adapter: board private structure
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001651 * @rxdr: rx descriptor ring (for a specific queue) to setup
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 *
1653 * Returns 0 on success, negative on failure
1654 **/
1655
Joe Perches64798842008-07-11 15:17:02 -07001656static int e1000_setup_rx_resources(struct e1000_adapter *adapter,
1657 struct e1000_rx_ring *rxdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 struct pci_dev *pdev = adapter->pdev;
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001660 int size, desc_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
1662 size = sizeof(struct e1000_buffer) * rxdr->count;
Eric Dumazet89bf67f2010-11-22 00:15:06 +00001663 rxdr->buffer_info = vzalloc(size);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001664 if (!rxdr->buffer_info) {
Emil Tantilovfeb8f472010-07-26 23:37:21 -07001665 e_err(probe, "Unable to allocate memory for the Rx descriptor "
1666 "ring\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 return -ENOMEM;
1668 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00001670 desc_len = sizeof(struct e1000_rx_desc);
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001671
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 /* Round up to nearest 4K */
1673
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001674 rxdr->size = rxdr->count * desc_len;
Milind Arun Choudhary9099cfb2007-04-27 13:55:29 -07001675 rxdr->size = ALIGN(rxdr->size, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
Nick Nunleyb16f53b2010-04-27 13:08:45 +00001677 rxdr->desc = dma_alloc_coherent(&pdev->dev, rxdr->size, &rxdr->dma,
1678 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001680 if (!rxdr->desc) {
Emil Tantilovfeb8f472010-07-26 23:37:21 -07001681 e_err(probe, "Unable to allocate memory for the Rx descriptor "
1682 "ring\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683setup_rx_desc_die:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 vfree(rxdr->buffer_info);
1685 return -ENOMEM;
1686 }
1687
Malli Chilakala26483452005-04-28 19:44:46 -07001688 /* Fix for errata 23, can't cross 64kB boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 if (!e1000_check_64k_bound(adapter, rxdr->desc, rxdr->size)) {
1690 void *olddesc = rxdr->desc;
1691 dma_addr_t olddma = rxdr->dma;
Emil Tantilovfeb8f472010-07-26 23:37:21 -07001692 e_err(rx_err, "rxdr align check failed: %u bytes at %p\n",
Emil Tantilov675ad472010-04-27 14:02:58 +00001693 rxdr->size, rxdr->desc);
Malli Chilakala26483452005-04-28 19:44:46 -07001694 /* Try again, without freeing the previous */
Nick Nunleyb16f53b2010-04-27 13:08:45 +00001695 rxdr->desc = dma_alloc_coherent(&pdev->dev, rxdr->size,
1696 &rxdr->dma, GFP_KERNEL);
Malli Chilakala26483452005-04-28 19:44:46 -07001697 /* Failed allocation, critical failure */
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001698 if (!rxdr->desc) {
Nick Nunleyb16f53b2010-04-27 13:08:45 +00001699 dma_free_coherent(&pdev->dev, rxdr->size, olddesc,
1700 olddma);
Emil Tantilovfeb8f472010-07-26 23:37:21 -07001701 e_err(probe, "Unable to allocate memory for the Rx "
1702 "descriptor ring\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 goto setup_rx_desc_die;
1704 }
1705
1706 if (!e1000_check_64k_bound(adapter, rxdr->desc, rxdr->size)) {
1707 /* give up */
Nick Nunleyb16f53b2010-04-27 13:08:45 +00001708 dma_free_coherent(&pdev->dev, rxdr->size, rxdr->desc,
1709 rxdr->dma);
1710 dma_free_coherent(&pdev->dev, rxdr->size, olddesc,
1711 olddma);
Emil Tantilovfeb8f472010-07-26 23:37:21 -07001712 e_err(probe, "Unable to allocate aligned memory for "
1713 "the Rx descriptor ring\n");
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001714 goto setup_rx_desc_die;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 } else {
Malli Chilakala26483452005-04-28 19:44:46 -07001716 /* Free old allocation, new allocation was successful */
Nick Nunleyb16f53b2010-04-27 13:08:45 +00001717 dma_free_coherent(&pdev->dev, rxdr->size, olddesc,
1718 olddma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 }
1720 }
1721 memset(rxdr->desc, 0, rxdr->size);
1722
1723 rxdr->next_to_clean = 0;
1724 rxdr->next_to_use = 0;
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00001725 rxdr->rx_skb_top = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
1727 return 0;
1728}
1729
1730/**
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001731 * e1000_setup_all_rx_resources - wrapper to allocate Rx resources
1732 * (Descriptors) for all queues
1733 * @adapter: board private structure
1734 *
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001735 * Return 0 on success, negative on failure
1736 **/
1737
Joe Perches64798842008-07-11 15:17:02 -07001738int e1000_setup_all_rx_resources(struct e1000_adapter *adapter)
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001739{
1740 int i, err = 0;
1741
Jeff Kirsherf56799e2006-01-12 16:50:39 -08001742 for (i = 0; i < adapter->num_rx_queues; i++) {
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001743 err = e1000_setup_rx_resources(adapter, &adapter->rx_ring[i]);
1744 if (err) {
Emil Tantilovfeb8f472010-07-26 23:37:21 -07001745 e_err(probe, "Allocation for Rx Queue %u failed\n", i);
Vasily Averin3fbbc722006-08-28 14:56:24 -07001746 for (i-- ; i >= 0; i--)
1747 e1000_free_rx_resources(adapter,
1748 &adapter->rx_ring[i]);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001749 break;
1750 }
1751 }
1752
1753 return err;
1754}
1755
1756/**
Malli Chilakala26483452005-04-28 19:44:46 -07001757 * e1000_setup_rctl - configure the receive control registers
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 * @adapter: Board private structure
1759 **/
Joe Perches64798842008-07-11 15:17:02 -07001760static void e1000_setup_rctl(struct e1000_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761{
Joe Perches1dc32912008-07-11 15:17:08 -07001762 struct e1000_hw *hw = &adapter->hw;
Brandeburg, Jesse630b25c2008-09-16 13:01:28 -07001763 u32 rctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764
Joe Perches1dc32912008-07-11 15:17:08 -07001765 rctl = er32(RCTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766
1767 rctl &= ~(3 << E1000_RCTL_MO_SHIFT);
1768
1769 rctl |= E1000_RCTL_EN | E1000_RCTL_BAM |
1770 E1000_RCTL_LBM_NO | E1000_RCTL_RDMTS_HALF |
Joe Perches1dc32912008-07-11 15:17:08 -07001771 (hw->mc_filter_type << E1000_RCTL_MO_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772
Joe Perches1dc32912008-07-11 15:17:08 -07001773 if (hw->tbi_compatibility_on == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 rctl |= E1000_RCTL_SBP;
1775 else
1776 rctl &= ~E1000_RCTL_SBP;
1777
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001778 if (adapter->netdev->mtu <= ETH_DATA_LEN)
1779 rctl &= ~E1000_RCTL_LPE;
1780 else
1781 rctl |= E1000_RCTL_LPE;
1782
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 /* Setup buffer sizes */
Auke Kok9e2feac2006-04-14 19:05:18 -07001784 rctl &= ~E1000_RCTL_SZ_4096;
1785 rctl |= E1000_RCTL_BSEX;
1786 switch (adapter->rx_buffer_len) {
Jeff Kirshera1415ee2006-02-28 20:24:07 -08001787 case E1000_RXBUFFER_2048:
1788 default:
1789 rctl |= E1000_RCTL_SZ_2048;
1790 rctl &= ~E1000_RCTL_BSEX;
1791 break;
1792 case E1000_RXBUFFER_4096:
1793 rctl |= E1000_RCTL_SZ_4096;
1794 break;
1795 case E1000_RXBUFFER_8192:
1796 rctl |= E1000_RCTL_SZ_8192;
1797 break;
1798 case E1000_RXBUFFER_16384:
1799 rctl |= E1000_RCTL_SZ_16384;
1800 break;
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001801 }
1802
Joe Perches1dc32912008-07-11 15:17:08 -07001803 ew32(RCTL, rctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804}
1805
1806/**
1807 * e1000_configure_rx - Configure 8254x Receive Unit after Reset
1808 * @adapter: board private structure
1809 *
1810 * Configure the Rx unit of the MAC after a reset.
1811 **/
1812
Joe Perches64798842008-07-11 15:17:02 -07001813static void e1000_configure_rx(struct e1000_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814{
Joe Perches406874a2008-04-03 10:06:32 -07001815 u64 rdba;
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001816 struct e1000_hw *hw = &adapter->hw;
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00001817 u32 rdlen, rctl, rxcsum;
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001818
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00001819 if (adapter->netdev->mtu > ETH_DATA_LEN) {
1820 rdlen = adapter->rx_ring[0].count *
1821 sizeof(struct e1000_rx_desc);
1822 adapter->clean_rx = e1000_clean_jumbo_rx_irq;
1823 adapter->alloc_rx_buf = e1000_alloc_jumbo_rx_buffers;
1824 } else {
1825 rdlen = adapter->rx_ring[0].count *
1826 sizeof(struct e1000_rx_desc);
1827 adapter->clean_rx = e1000_clean_rx_irq;
1828 adapter->alloc_rx_buf = e1000_alloc_rx_buffers;
1829 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830
1831 /* disable receives while setting up the descriptors */
Joe Perches1dc32912008-07-11 15:17:08 -07001832 rctl = er32(RCTL);
1833 ew32(RCTL, rctl & ~E1000_RCTL_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834
1835 /* set the Receive Delay Timer Register */
Joe Perches1dc32912008-07-11 15:17:08 -07001836 ew32(RDTR, adapter->rx_int_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001838 if (hw->mac_type >= e1000_82540) {
Joe Perches1dc32912008-07-11 15:17:08 -07001839 ew32(RADV, adapter->rx_abs_int_delay);
Jesse Brandeburg835bb122006-11-01 08:48:13 -08001840 if (adapter->itr_setting != 0)
Joe Perches1dc32912008-07-11 15:17:08 -07001841 ew32(ITR, 1000000000 / (adapter->itr * 256));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 }
1843
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001844 /* Setup the HW Rx Head and Tail Descriptor Pointers and
1845 * the Base and Length of the Rx Descriptor Ring */
Jeff Kirsherf56799e2006-01-12 16:50:39 -08001846 switch (adapter->num_rx_queues) {
Mallikarjuna R Chilakala24025e42005-10-04 07:03:23 -04001847 case 1:
1848 default:
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001849 rdba = adapter->rx_ring[0].dma;
Joe Perches1dc32912008-07-11 15:17:08 -07001850 ew32(RDLEN, rdlen);
1851 ew32(RDBAH, (rdba >> 32));
1852 ew32(RDBAL, (rdba & 0x00000000ffffffffULL));
1853 ew32(RDT, 0);
1854 ew32(RDH, 0);
Auke Kok6a951692006-09-11 14:00:21 -07001855 adapter->rx_ring[0].rdh = ((hw->mac_type >= e1000_82543) ? E1000_RDH : E1000_82542_RDH);
1856 adapter->rx_ring[0].rdt = ((hw->mac_type >= e1000_82543) ? E1000_RDT : E1000_82542_RDT);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001857 break;
Mallikarjuna R Chilakala24025e42005-10-04 07:03:23 -04001858 }
1859
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 /* Enable 82543 Receive Checksum Offload for TCP and UDP */
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001861 if (hw->mac_type >= e1000_82543) {
Joe Perches1dc32912008-07-11 15:17:08 -07001862 rxcsum = er32(RXCSUM);
Brandeburg, Jesse630b25c2008-09-16 13:01:28 -07001863 if (adapter->rx_csum)
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001864 rxcsum |= E1000_RXCSUM_TUOFL;
Brandeburg, Jesse630b25c2008-09-16 13:01:28 -07001865 else
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001866 /* don't need to clear IPPCSE as it defaults to 0 */
Brandeburg, Jesse630b25c2008-09-16 13:01:28 -07001867 rxcsum &= ~E1000_RXCSUM_TUOFL;
Joe Perches1dc32912008-07-11 15:17:08 -07001868 ew32(RXCSUM, rxcsum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 }
1870
1871 /* Enable Receives */
Joe Perches1dc32912008-07-11 15:17:08 -07001872 ew32(RCTL, rctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873}
1874
1875/**
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001876 * e1000_free_tx_resources - Free Tx Resources per Queue
1877 * @adapter: board private structure
1878 * @tx_ring: Tx descriptor ring for a specific queue
1879 *
1880 * Free all transmit software resources
1881 **/
1882
Joe Perches64798842008-07-11 15:17:02 -07001883static void e1000_free_tx_resources(struct e1000_adapter *adapter,
1884 struct e1000_tx_ring *tx_ring)
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001885{
1886 struct pci_dev *pdev = adapter->pdev;
1887
1888 e1000_clean_tx_ring(adapter, tx_ring);
1889
1890 vfree(tx_ring->buffer_info);
1891 tx_ring->buffer_info = NULL;
1892
Nick Nunleyb16f53b2010-04-27 13:08:45 +00001893 dma_free_coherent(&pdev->dev, tx_ring->size, tx_ring->desc,
1894 tx_ring->dma);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001895
1896 tx_ring->desc = NULL;
1897}
1898
1899/**
1900 * e1000_free_all_tx_resources - Free Tx Resources for All Queues
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 * @adapter: board private structure
1902 *
1903 * Free all transmit software resources
1904 **/
1905
Joe Perches64798842008-07-11 15:17:02 -07001906void e1000_free_all_tx_resources(struct e1000_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907{
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001908 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909
Jeff Kirsherf56799e2006-01-12 16:50:39 -08001910 for (i = 0; i < adapter->num_tx_queues; i++)
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001911 e1000_free_tx_resources(adapter, &adapter->tx_ring[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912}
1913
Joe Perches64798842008-07-11 15:17:02 -07001914static void e1000_unmap_and_free_tx_resource(struct e1000_adapter *adapter,
1915 struct e1000_buffer *buffer_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916{
Alexander Duyck602c0552009-12-02 16:46:00 +00001917 if (buffer_info->dma) {
1918 if (buffer_info->mapped_as_page)
Nick Nunleyb16f53b2010-04-27 13:08:45 +00001919 dma_unmap_page(&adapter->pdev->dev, buffer_info->dma,
1920 buffer_info->length, DMA_TO_DEVICE);
Alexander Duyck602c0552009-12-02 16:46:00 +00001921 else
Nick Nunleyb16f53b2010-04-27 13:08:45 +00001922 dma_unmap_single(&adapter->pdev->dev, buffer_info->dma,
Alexander Duyck602c0552009-12-02 16:46:00 +00001923 buffer_info->length,
Nick Nunleyb16f53b2010-04-27 13:08:45 +00001924 DMA_TO_DEVICE);
Alexander Duyck602c0552009-12-02 16:46:00 +00001925 buffer_info->dma = 0;
1926 }
Jesse Brandeburga9ebadd2006-11-01 08:47:53 -08001927 if (buffer_info->skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 dev_kfree_skb_any(buffer_info->skb);
Jesse Brandeburga9ebadd2006-11-01 08:47:53 -08001929 buffer_info->skb = NULL;
1930 }
Alexander Duyck37e73df2009-03-25 21:58:45 +00001931 buffer_info->time_stamp = 0;
Jesse Brandeburga9ebadd2006-11-01 08:47:53 -08001932 /* buffer_info must be completely set up in the transmit path */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933}
1934
1935/**
1936 * e1000_clean_tx_ring - Free Tx Buffers
1937 * @adapter: board private structure
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001938 * @tx_ring: ring to be cleaned
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 **/
1940
Joe Perches64798842008-07-11 15:17:02 -07001941static void e1000_clean_tx_ring(struct e1000_adapter *adapter,
1942 struct e1000_tx_ring *tx_ring)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943{
Joe Perches1dc32912008-07-11 15:17:08 -07001944 struct e1000_hw *hw = &adapter->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 struct e1000_buffer *buffer_info;
1946 unsigned long size;
1947 unsigned int i;
1948
1949 /* Free all the Tx ring sk_buffs */
1950
Jesse Brandeburg96838a42006-01-18 13:01:39 -08001951 for (i = 0; i < tx_ring->count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 buffer_info = &tx_ring->buffer_info[i];
1953 e1000_unmap_and_free_tx_resource(adapter, buffer_info);
1954 }
1955
1956 size = sizeof(struct e1000_buffer) * tx_ring->count;
1957 memset(tx_ring->buffer_info, 0, size);
1958
1959 /* Zero out the descriptor ring */
1960
1961 memset(tx_ring->desc, 0, tx_ring->size);
1962
1963 tx_ring->next_to_use = 0;
1964 tx_ring->next_to_clean = 0;
Jeff Kirsherfd803242005-12-13 00:06:22 -05001965 tx_ring->last_tx_tso = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
Joe Perches1dc32912008-07-11 15:17:08 -07001967 writel(0, hw->hw_addr + tx_ring->tdh);
1968 writel(0, hw->hw_addr + tx_ring->tdt);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001969}
1970
1971/**
1972 * e1000_clean_all_tx_rings - Free Tx Buffers for all queues
1973 * @adapter: board private structure
1974 **/
1975
Joe Perches64798842008-07-11 15:17:02 -07001976static void e1000_clean_all_tx_rings(struct e1000_adapter *adapter)
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001977{
1978 int i;
1979
Jeff Kirsherf56799e2006-01-12 16:50:39 -08001980 for (i = 0; i < adapter->num_tx_queues; i++)
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001981 e1000_clean_tx_ring(adapter, &adapter->tx_ring[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982}
1983
1984/**
1985 * e1000_free_rx_resources - Free Rx Resources
1986 * @adapter: board private structure
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001987 * @rx_ring: ring to clean the resources from
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 *
1989 * Free all receive software resources
1990 **/
1991
Joe Perches64798842008-07-11 15:17:02 -07001992static void e1000_free_rx_resources(struct e1000_adapter *adapter,
1993 struct e1000_rx_ring *rx_ring)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 struct pci_dev *pdev = adapter->pdev;
1996
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001997 e1000_clean_rx_ring(adapter, rx_ring);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998
1999 vfree(rx_ring->buffer_info);
2000 rx_ring->buffer_info = NULL;
2001
Nick Nunleyb16f53b2010-04-27 13:08:45 +00002002 dma_free_coherent(&pdev->dev, rx_ring->size, rx_ring->desc,
2003 rx_ring->dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004
2005 rx_ring->desc = NULL;
2006}
2007
2008/**
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002009 * e1000_free_all_rx_resources - Free Rx Resources for All Queues
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 * @adapter: board private structure
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002011 *
2012 * Free all receive software resources
2013 **/
2014
Joe Perches64798842008-07-11 15:17:02 -07002015void e1000_free_all_rx_resources(struct e1000_adapter *adapter)
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002016{
2017 int i;
2018
Jeff Kirsherf56799e2006-01-12 16:50:39 -08002019 for (i = 0; i < adapter->num_rx_queues; i++)
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002020 e1000_free_rx_resources(adapter, &adapter->rx_ring[i]);
2021}
2022
2023/**
2024 * e1000_clean_rx_ring - Free Rx Buffers per Queue
2025 * @adapter: board private structure
2026 * @rx_ring: ring to free buffers from
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 **/
2028
Joe Perches64798842008-07-11 15:17:02 -07002029static void e1000_clean_rx_ring(struct e1000_adapter *adapter,
2030 struct e1000_rx_ring *rx_ring)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031{
Joe Perches1dc32912008-07-11 15:17:08 -07002032 struct e1000_hw *hw = &adapter->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 struct e1000_buffer *buffer_info;
2034 struct pci_dev *pdev = adapter->pdev;
2035 unsigned long size;
Brandeburg, Jesse630b25c2008-09-16 13:01:28 -07002036 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037
2038 /* Free all the Rx ring sk_buffs */
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002039 for (i = 0; i < rx_ring->count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 buffer_info = &rx_ring->buffer_info[i];
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00002041 if (buffer_info->dma &&
2042 adapter->clean_rx == e1000_clean_rx_irq) {
Nick Nunleyb16f53b2010-04-27 13:08:45 +00002043 dma_unmap_single(&pdev->dev, buffer_info->dma,
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00002044 buffer_info->length,
Nick Nunleyb16f53b2010-04-27 13:08:45 +00002045 DMA_FROM_DEVICE);
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00002046 } else if (buffer_info->dma &&
2047 adapter->clean_rx == e1000_clean_jumbo_rx_irq) {
Nick Nunleyb16f53b2010-04-27 13:08:45 +00002048 dma_unmap_page(&pdev->dev, buffer_info->dma,
2049 buffer_info->length,
2050 DMA_FROM_DEVICE);
Jesse Brandeburg679be3b2009-06-30 12:45:34 +00002051 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052
Jesse Brandeburg679be3b2009-06-30 12:45:34 +00002053 buffer_info->dma = 0;
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00002054 if (buffer_info->page) {
2055 put_page(buffer_info->page);
2056 buffer_info->page = NULL;
2057 }
Jesse Brandeburg679be3b2009-06-30 12:45:34 +00002058 if (buffer_info->skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 dev_kfree_skb(buffer_info->skb);
2060 buffer_info->skb = NULL;
Jeff Kirsher997f5cb2006-01-12 16:50:55 -08002061 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 }
2063
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00002064 /* there also may be some cached data from a chained receive */
2065 if (rx_ring->rx_skb_top) {
2066 dev_kfree_skb(rx_ring->rx_skb_top);
2067 rx_ring->rx_skb_top = NULL;
2068 }
2069
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 size = sizeof(struct e1000_buffer) * rx_ring->count;
2071 memset(rx_ring->buffer_info, 0, size);
2072
2073 /* Zero out the descriptor ring */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 memset(rx_ring->desc, 0, rx_ring->size);
2075
2076 rx_ring->next_to_clean = 0;
2077 rx_ring->next_to_use = 0;
2078
Joe Perches1dc32912008-07-11 15:17:08 -07002079 writel(0, hw->hw_addr + rx_ring->rdh);
2080 writel(0, hw->hw_addr + rx_ring->rdt);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002081}
2082
2083/**
2084 * e1000_clean_all_rx_rings - Free Rx Buffers for all queues
2085 * @adapter: board private structure
2086 **/
2087
Joe Perches64798842008-07-11 15:17:02 -07002088static void e1000_clean_all_rx_rings(struct e1000_adapter *adapter)
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002089{
2090 int i;
2091
Jeff Kirsherf56799e2006-01-12 16:50:39 -08002092 for (i = 0; i < adapter->num_rx_queues; i++)
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002093 e1000_clean_rx_ring(adapter, &adapter->rx_ring[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094}
2095
2096/* The 82542 2.0 (revision 2) needs to have the receive unit in reset
2097 * and memory write and invalidate disabled for certain operations
2098 */
Joe Perches64798842008-07-11 15:17:02 -07002099static void e1000_enter_82542_rst(struct e1000_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100{
Joe Perches1dc32912008-07-11 15:17:08 -07002101 struct e1000_hw *hw = &adapter->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 struct net_device *netdev = adapter->netdev;
Joe Perches406874a2008-04-03 10:06:32 -07002103 u32 rctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104
Joe Perches1dc32912008-07-11 15:17:08 -07002105 e1000_pci_clear_mwi(hw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106
Joe Perches1dc32912008-07-11 15:17:08 -07002107 rctl = er32(RCTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 rctl |= E1000_RCTL_RST;
Joe Perches1dc32912008-07-11 15:17:08 -07002109 ew32(RCTL, rctl);
2110 E1000_WRITE_FLUSH();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 mdelay(5);
2112
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002113 if (netif_running(netdev))
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002114 e1000_clean_all_rx_rings(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115}
2116
Joe Perches64798842008-07-11 15:17:02 -07002117static void e1000_leave_82542_rst(struct e1000_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118{
Joe Perches1dc32912008-07-11 15:17:08 -07002119 struct e1000_hw *hw = &adapter->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 struct net_device *netdev = adapter->netdev;
Joe Perches406874a2008-04-03 10:06:32 -07002121 u32 rctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122
Joe Perches1dc32912008-07-11 15:17:08 -07002123 rctl = er32(RCTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 rctl &= ~E1000_RCTL_RST;
Joe Perches1dc32912008-07-11 15:17:08 -07002125 ew32(RCTL, rctl);
2126 E1000_WRITE_FLUSH();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 mdelay(5);
2128
Joe Perches1dc32912008-07-11 15:17:08 -07002129 if (hw->pci_cmd_word & PCI_COMMAND_INVALIDATE)
2130 e1000_pci_set_mwi(hw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002132 if (netif_running(netdev)) {
Jeff Kirsher72d64a42006-01-12 16:51:19 -08002133 /* No need to loop, because 82542 supports only 1 queue */
2134 struct e1000_rx_ring *ring = &adapter->rx_ring[0];
Jesse Brandeburg7c4d3362006-01-18 13:01:45 -08002135 e1000_configure_rx(adapter);
Jeff Kirsher72d64a42006-01-12 16:51:19 -08002136 adapter->alloc_rx_buf(adapter, ring, E1000_DESC_UNUSED(ring));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 }
2138}
2139
2140/**
2141 * e1000_set_mac - Change the Ethernet Address of the NIC
2142 * @netdev: network interface device structure
2143 * @p: pointer to an address structure
2144 *
2145 * Returns 0 on success, negative on failure
2146 **/
2147
Joe Perches64798842008-07-11 15:17:02 -07002148static int e1000_set_mac(struct net_device *netdev, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149{
Malli Chilakala60490fe2005-06-17 17:41:45 -07002150 struct e1000_adapter *adapter = netdev_priv(netdev);
Joe Perches1dc32912008-07-11 15:17:08 -07002151 struct e1000_hw *hw = &adapter->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 struct sockaddr *addr = p;
2153
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002154 if (!is_valid_ether_addr(addr->sa_data))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 return -EADDRNOTAVAIL;
2156
2157 /* 82542 2.0 needs to be in reset to write receive address registers */
2158
Joe Perches1dc32912008-07-11 15:17:08 -07002159 if (hw->mac_type == e1000_82542_rev2_0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 e1000_enter_82542_rst(adapter);
2161
2162 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
Joe Perches1dc32912008-07-11 15:17:08 -07002163 memcpy(hw->mac_addr, addr->sa_data, netdev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164
Joe Perches1dc32912008-07-11 15:17:08 -07002165 e1000_rar_set(hw, hw->mac_addr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166
Joe Perches1dc32912008-07-11 15:17:08 -07002167 if (hw->mac_type == e1000_82542_rev2_0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 e1000_leave_82542_rst(adapter);
2169
2170 return 0;
2171}
2172
2173/**
Patrick McHardydb0ce502007-11-13 20:54:59 -08002174 * e1000_set_rx_mode - Secondary Unicast, Multicast and Promiscuous mode set
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 * @netdev: network interface device structure
2176 *
Patrick McHardydb0ce502007-11-13 20:54:59 -08002177 * The set_rx_mode entry point is called whenever the unicast or multicast
2178 * address lists or the network interface flags are updated. This routine is
2179 * responsible for configuring the hardware for proper unicast, multicast,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 * promiscuous mode, and all-multi behavior.
2181 **/
2182
Joe Perches64798842008-07-11 15:17:02 -07002183static void e1000_set_rx_mode(struct net_device *netdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184{
Malli Chilakala60490fe2005-06-17 17:41:45 -07002185 struct e1000_adapter *adapter = netdev_priv(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 struct e1000_hw *hw = &adapter->hw;
Jiri Pirkoccffad22009-05-22 23:22:17 +00002187 struct netdev_hw_addr *ha;
2188 bool use_uc = false;
Joe Perches406874a2008-04-03 10:06:32 -07002189 u32 rctl;
2190 u32 hash_value;
Mallikarjuna R Chilakala868d5302005-10-04 06:58:59 -04002191 int i, rar_entries = E1000_RAR_ENTRIES;
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00002192 int mta_reg_count = E1000_NUM_MTA_REGISTERS;
Jesse Brandeburg81c52282009-04-04 16:36:53 -07002193 u32 *mcarray = kcalloc(mta_reg_count, sizeof(u32), GFP_ATOMIC);
2194
2195 if (!mcarray) {
Emil Tantilovfeb8f472010-07-26 23:37:21 -07002196 e_err(probe, "memory allocation failed\n");
Jesse Brandeburg81c52282009-04-04 16:36:53 -07002197 return;
2198 }
Auke Kokcd94dd02006-06-27 09:08:22 -07002199
Malli Chilakala26483452005-04-28 19:44:46 -07002200 /* Check for Promiscuous and All Multicast modes */
2201
Joe Perches1dc32912008-07-11 15:17:08 -07002202 rctl = er32(RCTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002204 if (netdev->flags & IFF_PROMISC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 rctl |= (E1000_RCTL_UPE | E1000_RCTL_MPE);
Patrick McHardy746b9f02008-07-16 20:15:45 -07002206 rctl &= ~E1000_RCTL_VFE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 } else {
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00002208 if (netdev->flags & IFF_ALLMULTI)
Patrick McHardy746b9f02008-07-16 20:15:45 -07002209 rctl |= E1000_RCTL_MPE;
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00002210 else
Patrick McHardy746b9f02008-07-16 20:15:45 -07002211 rctl &= ~E1000_RCTL_MPE;
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00002212 /* Enable VLAN filter if there is a VLAN */
2213 if (adapter->vlgrp)
2214 rctl |= E1000_RCTL_VFE;
Patrick McHardydb0ce502007-11-13 20:54:59 -08002215 }
2216
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08002217 if (netdev_uc_count(netdev) > rar_entries - 1) {
Patrick McHardydb0ce502007-11-13 20:54:59 -08002218 rctl |= E1000_RCTL_UPE;
2219 } else if (!(netdev->flags & IFF_PROMISC)) {
2220 rctl &= ~E1000_RCTL_UPE;
Jiri Pirkoccffad22009-05-22 23:22:17 +00002221 use_uc = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 }
2223
Joe Perches1dc32912008-07-11 15:17:08 -07002224 ew32(RCTL, rctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225
2226 /* 82542 2.0 needs to be in reset to write receive address registers */
2227
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002228 if (hw->mac_type == e1000_82542_rev2_0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 e1000_enter_82542_rst(adapter);
2230
Patrick McHardydb0ce502007-11-13 20:54:59 -08002231 /* load the first 14 addresses into the exact filters 1-14. Unicast
2232 * addresses take precedence to avoid disabling unicast filtering
2233 * when possible.
2234 *
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04002235 * RAR 0 is used for the station MAC address
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 * if there are not 14 addresses, go ahead and clear the filters
2237 */
Jiri Pirkoccffad22009-05-22 23:22:17 +00002238 i = 1;
2239 if (use_uc)
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08002240 netdev_for_each_uc_addr(ha, netdev) {
Jiri Pirkoccffad22009-05-22 23:22:17 +00002241 if (i == rar_entries)
2242 break;
2243 e1000_rar_set(hw, ha->addr, i++);
2244 }
2245
Jiri Pirko22bedad2010-04-01 21:22:57 +00002246 netdev_for_each_mc_addr(ha, netdev) {
Jiri Pirko7a81e9f2010-02-22 09:10:44 +00002247 if (i == rar_entries) {
2248 /* load any remaining addresses into the hash table */
2249 u32 hash_reg, hash_bit, mta;
Jiri Pirko22bedad2010-04-01 21:22:57 +00002250 hash_value = e1000_hash_mc_addr(hw, ha->addr);
Jiri Pirko7a81e9f2010-02-22 09:10:44 +00002251 hash_reg = (hash_value >> 5) & 0x7F;
2252 hash_bit = hash_value & 0x1F;
2253 mta = (1 << hash_bit);
2254 mcarray[hash_reg] |= mta;
Jiri Pirko10886af2010-02-23 01:19:22 -08002255 } else {
Jiri Pirko22bedad2010-04-01 21:22:57 +00002256 e1000_rar_set(hw, ha->addr, i++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 }
2258 }
2259
Jiri Pirko7a81e9f2010-02-22 09:10:44 +00002260 for (; i < rar_entries; i++) {
2261 E1000_WRITE_REG_ARRAY(hw, RA, i << 1, 0);
2262 E1000_WRITE_FLUSH();
2263 E1000_WRITE_REG_ARRAY(hw, RA, (i << 1) + 1, 0);
2264 E1000_WRITE_FLUSH();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 }
2266
Jesse Brandeburg81c52282009-04-04 16:36:53 -07002267 /* write the hash table completely, write from bottom to avoid
2268 * both stupid write combining chipsets, and flushing each write */
2269 for (i = mta_reg_count - 1; i >= 0 ; i--) {
2270 /*
2271 * If we are on an 82544 has an errata where writing odd
2272 * offsets overwrites the previous even offset, but writing
2273 * backwards over the range solves the issue by always
2274 * writing the odd offset first
2275 */
2276 E1000_WRITE_REG_ARRAY(hw, MTA, i, mcarray[i]);
2277 }
2278 E1000_WRITE_FLUSH();
2279
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002280 if (hw->mac_type == e1000_82542_rev2_0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 e1000_leave_82542_rst(adapter);
Jesse Brandeburg81c52282009-04-04 16:36:53 -07002282
2283 kfree(mcarray);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284}
2285
2286/* Need to wait a few seconds after link up to get diagnostic information from
2287 * the phy */
2288
Joe Perches64798842008-07-11 15:17:02 -07002289static void e1000_update_phy_info(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290{
Joe Perchese982f172008-07-11 15:17:18 -07002291 struct e1000_adapter *adapter = (struct e1000_adapter *)data;
Jesse Brandeburg5cf42fc2010-09-22 18:22:17 +00002292 schedule_work(&adapter->phy_info_task);
2293}
2294
2295static void e1000_update_phy_info_task(struct work_struct *work)
2296{
2297 struct e1000_adapter *adapter = container_of(work,
2298 struct e1000_adapter,
2299 phy_info_task);
Joe Perches1dc32912008-07-11 15:17:08 -07002300 struct e1000_hw *hw = &adapter->hw;
Jesse Brandeburg338c15e2010-09-22 18:22:42 +00002301
2302 rtnl_lock();
Joe Perches1dc32912008-07-11 15:17:08 -07002303 e1000_phy_get_info(hw, &adapter->phy_info);
Jesse Brandeburg338c15e2010-09-22 18:22:42 +00002304 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305}
2306
2307/**
2308 * e1000_82547_tx_fifo_stall - Timer Call-back
2309 * @data: pointer to adapter cast into an unsigned long
2310 **/
Joe Perches64798842008-07-11 15:17:02 -07002311static void e1000_82547_tx_fifo_stall(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312{
Joe Perchese982f172008-07-11 15:17:18 -07002313 struct e1000_adapter *adapter = (struct e1000_adapter *)data;
Jesse Brandeburg5cf42fc2010-09-22 18:22:17 +00002314 schedule_work(&adapter->fifo_stall_task);
2315}
2316
2317/**
2318 * e1000_82547_tx_fifo_stall_task - task to complete work
2319 * @work: work struct contained inside adapter struct
2320 **/
2321static void e1000_82547_tx_fifo_stall_task(struct work_struct *work)
2322{
2323 struct e1000_adapter *adapter = container_of(work,
2324 struct e1000_adapter,
2325 fifo_stall_task);
Joe Perches1dc32912008-07-11 15:17:08 -07002326 struct e1000_hw *hw = &adapter->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 struct net_device *netdev = adapter->netdev;
Joe Perches406874a2008-04-03 10:06:32 -07002328 u32 tctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329
Jesse Brandeburg338c15e2010-09-22 18:22:42 +00002330 rtnl_lock();
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002331 if (atomic_read(&adapter->tx_fifo_stall)) {
Joe Perches1dc32912008-07-11 15:17:08 -07002332 if ((er32(TDT) == er32(TDH)) &&
2333 (er32(TDFT) == er32(TDFH)) &&
2334 (er32(TDFTS) == er32(TDFHS))) {
2335 tctl = er32(TCTL);
2336 ew32(TCTL, tctl & ~E1000_TCTL_EN);
2337 ew32(TDFT, adapter->tx_head_addr);
2338 ew32(TDFH, adapter->tx_head_addr);
2339 ew32(TDFTS, adapter->tx_head_addr);
2340 ew32(TDFHS, adapter->tx_head_addr);
2341 ew32(TCTL, tctl);
2342 E1000_WRITE_FLUSH();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343
2344 adapter->tx_fifo_head = 0;
2345 atomic_set(&adapter->tx_fifo_stall, 0);
2346 netif_wake_queue(netdev);
Jesse Brandeburgbaa34742009-09-25 12:17:23 +00002347 } else if (!test_bit(__E1000_DOWN, &adapter->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 mod_timer(&adapter->tx_fifo_stall_timer, jiffies + 1);
2349 }
2350 }
Jesse Brandeburg338c15e2010-09-22 18:22:42 +00002351 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352}
2353
Nick Nunleyb5481922010-02-03 14:49:28 +00002354bool e1000_has_link(struct e1000_adapter *adapter)
Jesse Brandeburgbe0f0712009-09-25 12:17:44 +00002355{
2356 struct e1000_hw *hw = &adapter->hw;
2357 bool link_active = false;
Jesse Brandeburgbe0f0712009-09-25 12:17:44 +00002358
2359 /* get_link_status is set on LSC (link status) interrupt or
2360 * rx sequence error interrupt. get_link_status will stay
2361 * false until the e1000_check_for_link establishes link
2362 * for copper adapters ONLY
2363 */
2364 switch (hw->media_type) {
2365 case e1000_media_type_copper:
2366 if (hw->get_link_status) {
Jesse Brandeburg120a5d02009-09-25 15:19:46 -07002367 e1000_check_for_link(hw);
Jesse Brandeburgbe0f0712009-09-25 12:17:44 +00002368 link_active = !hw->get_link_status;
2369 } else {
2370 link_active = true;
2371 }
2372 break;
2373 case e1000_media_type_fiber:
Jesse Brandeburg120a5d02009-09-25 15:19:46 -07002374 e1000_check_for_link(hw);
Jesse Brandeburgbe0f0712009-09-25 12:17:44 +00002375 link_active = !!(er32(STATUS) & E1000_STATUS_LU);
2376 break;
2377 case e1000_media_type_internal_serdes:
Jesse Brandeburg120a5d02009-09-25 15:19:46 -07002378 e1000_check_for_link(hw);
Jesse Brandeburgbe0f0712009-09-25 12:17:44 +00002379 link_active = hw->serdes_has_link;
2380 break;
2381 default:
2382 break;
2383 }
2384
2385 return link_active;
2386}
2387
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388/**
2389 * e1000_watchdog - Timer Call-back
2390 * @data: pointer to adapter cast into an unsigned long
2391 **/
Joe Perches64798842008-07-11 15:17:02 -07002392static void e1000_watchdog(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393{
Joe Perchese982f172008-07-11 15:17:18 -07002394 struct e1000_adapter *adapter = (struct e1000_adapter *)data;
Joe Perches1dc32912008-07-11 15:17:08 -07002395 struct e1000_hw *hw = &adapter->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 struct net_device *netdev = adapter->netdev;
Jeff Kirsher545c67c2006-01-12 16:50:25 -08002397 struct e1000_tx_ring *txdr = adapter->tx_ring;
Joe Perches406874a2008-04-03 10:06:32 -07002398 u32 link, tctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399
Jesse Brandeburgbe0f0712009-09-25 12:17:44 +00002400 link = e1000_has_link(adapter);
2401 if ((netif_carrier_ok(netdev)) && link)
2402 goto link_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002404 if (link) {
2405 if (!netif_carrier_ok(netdev)) {
Joe Perches406874a2008-04-03 10:06:32 -07002406 u32 ctrl;
Joe Perchesc3033b02008-03-21 11:06:25 -07002407 bool txb2b = true;
Jesse Brandeburgbe0f0712009-09-25 12:17:44 +00002408 /* update snapshot of PHY registers on LSC */
Joe Perches1dc32912008-07-11 15:17:08 -07002409 e1000_get_speed_and_duplex(hw,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 &adapter->link_speed,
2411 &adapter->link_duplex);
2412
Joe Perches1dc32912008-07-11 15:17:08 -07002413 ctrl = er32(CTRL);
Emil Tantilov675ad472010-04-27 14:02:58 +00002414 pr_info("%s NIC Link is Up %d Mbps %s, "
2415 "Flow Control: %s\n",
2416 netdev->name,
2417 adapter->link_speed,
2418 adapter->link_duplex == FULL_DUPLEX ?
2419 "Full Duplex" : "Half Duplex",
2420 ((ctrl & E1000_CTRL_TFCE) && (ctrl &
2421 E1000_CTRL_RFCE)) ? "RX/TX" : ((ctrl &
2422 E1000_CTRL_RFCE) ? "RX" : ((ctrl &
2423 E1000_CTRL_TFCE) ? "TX" : "None")));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424
Emil Tantilov39ca5f02010-03-26 11:25:58 +00002425 /* adjust timeout factor according to speed/duplex */
Jeff Kirsher66a2b0a2006-01-12 16:50:53 -08002426 adapter->tx_timeout_factor = 1;
Jeff Kirsher7e6c9862006-03-02 18:19:30 -08002427 switch (adapter->link_speed) {
2428 case SPEED_10:
Joe Perchesc3033b02008-03-21 11:06:25 -07002429 txb2b = false;
Jesse Brandeburgbe0f0712009-09-25 12:17:44 +00002430 adapter->tx_timeout_factor = 16;
Jeff Kirsher7e6c9862006-03-02 18:19:30 -08002431 break;
2432 case SPEED_100:
Joe Perchesc3033b02008-03-21 11:06:25 -07002433 txb2b = false;
Jeff Kirsher7e6c9862006-03-02 18:19:30 -08002434 /* maybe add some timeout factor ? */
2435 break;
2436 }
2437
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00002438 /* enable transmits in the hardware */
Joe Perches1dc32912008-07-11 15:17:08 -07002439 tctl = er32(TCTL);
Jeff Kirsher7e6c9862006-03-02 18:19:30 -08002440 tctl |= E1000_TCTL_EN;
Joe Perches1dc32912008-07-11 15:17:08 -07002441 ew32(TCTL, tctl);
Jeff Kirsher66a2b0a2006-01-12 16:50:53 -08002442
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 netif_carrier_on(netdev);
Jesse Brandeburgbaa34742009-09-25 12:17:23 +00002444 if (!test_bit(__E1000_DOWN, &adapter->flags))
2445 mod_timer(&adapter->phy_info_timer,
2446 round_jiffies(jiffies + 2 * HZ));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 adapter->smartspeed = 0;
2448 }
2449 } else {
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002450 if (netif_carrier_ok(netdev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 adapter->link_speed = 0;
2452 adapter->link_duplex = 0;
Emil Tantilov675ad472010-04-27 14:02:58 +00002453 pr_info("%s NIC Link is Down\n",
2454 netdev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 netif_carrier_off(netdev);
Jesse Brandeburgbaa34742009-09-25 12:17:23 +00002456
2457 if (!test_bit(__E1000_DOWN, &adapter->flags))
2458 mod_timer(&adapter->phy_info_timer,
2459 round_jiffies(jiffies + 2 * HZ));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 }
2461
2462 e1000_smartspeed(adapter);
2463 }
2464
Jesse Brandeburgbe0f0712009-09-25 12:17:44 +00002465link_up:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 e1000_update_stats(adapter);
2467
Joe Perches1dc32912008-07-11 15:17:08 -07002468 hw->tx_packet_delta = adapter->stats.tpt - adapter->tpt_old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 adapter->tpt_old = adapter->stats.tpt;
Joe Perches1dc32912008-07-11 15:17:08 -07002470 hw->collision_delta = adapter->stats.colc - adapter->colc_old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 adapter->colc_old = adapter->stats.colc;
2472
2473 adapter->gorcl = adapter->stats.gorcl - adapter->gorcl_old;
2474 adapter->gorcl_old = adapter->stats.gorcl;
2475 adapter->gotcl = adapter->stats.gotcl - adapter->gotcl_old;
2476 adapter->gotcl_old = adapter->stats.gotcl;
2477
Joe Perches1dc32912008-07-11 15:17:08 -07002478 e1000_update_adaptive(hw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479
Jeff Kirsherf56799e2006-01-12 16:50:39 -08002480 if (!netif_carrier_ok(netdev)) {
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002481 if (E1000_DESC_UNUSED(txdr) + 1 < txdr->count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 /* We've lost link, so the controller stops DMA,
2483 * but we've got queued Tx work that's never going
2484 * to get done, so reset controller to flush Tx.
2485 * (Do the reset outside of interrupt context). */
Jeff Kirsher87041632006-03-02 18:21:24 -08002486 adapter->tx_timeout_count++;
2487 schedule_work(&adapter->reset_task);
Jesse Brandeburgc2d5ab42009-05-07 11:07:35 +00002488 /* return immediately since reset is imminent */
2489 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 }
2491 }
2492
Jesse Brandeburgeab2abf2010-05-04 22:26:03 +00002493 /* Simple mode for Interrupt Throttle Rate (ITR) */
2494 if (hw->mac_type >= e1000_82540 && adapter->itr_setting == 4) {
2495 /*
2496 * Symmetric Tx/Rx gets a reduced ITR=2000;
2497 * Total asymmetrical Tx or Rx gets ITR=8000;
2498 * everyone else is between 2000-8000.
2499 */
2500 u32 goc = (adapter->gotcl + adapter->gorcl) / 10000;
2501 u32 dif = (adapter->gotcl > adapter->gorcl ?
2502 adapter->gotcl - adapter->gorcl :
2503 adapter->gorcl - adapter->gotcl) / 10000;
2504 u32 itr = goc > 0 ? (dif * 6000 / goc + 2000) : 8000;
2505
2506 ew32(ITR, 1000000000 / (itr * 256));
2507 }
2508
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 /* Cause software interrupt to ensure rx ring is cleaned */
Joe Perches1dc32912008-07-11 15:17:08 -07002510 ew32(ICS, E1000_ICS_RXDMT0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511
Malli Chilakala26483452005-04-28 19:44:46 -07002512 /* Force detection of hung controller every watchdog period */
Joe Perchesc3033b02008-03-21 11:06:25 -07002513 adapter->detect_tx_hung = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514
2515 /* Reset the timer */
Jesse Brandeburgbaa34742009-09-25 12:17:23 +00002516 if (!test_bit(__E1000_DOWN, &adapter->flags))
2517 mod_timer(&adapter->watchdog_timer,
2518 round_jiffies(jiffies + 2 * HZ));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519}
2520
Jesse Brandeburg835bb122006-11-01 08:48:13 -08002521enum latency_range {
2522 lowest_latency = 0,
2523 low_latency = 1,
2524 bulk_latency = 2,
2525 latency_invalid = 255
2526};
2527
2528/**
2529 * e1000_update_itr - update the dynamic ITR value based on statistics
Jesse Brandeburg8fce4732009-09-25 12:18:41 +00002530 * @adapter: pointer to adapter
2531 * @itr_setting: current adapter->itr
2532 * @packets: the number of packets during this measurement interval
2533 * @bytes: the number of bytes during this measurement interval
2534 *
Jesse Brandeburg835bb122006-11-01 08:48:13 -08002535 * Stores a new ITR value based on packets and byte
2536 * counts during the last interrupt. The advantage of per interrupt
2537 * computation is faster updates and more accurate ITR for the current
2538 * traffic pattern. Constants in this function were computed
2539 * based on theoretical maximum wire speed and thresholds were set based
2540 * on testing data as well as attempting to minimize response time
2541 * while increasing bulk throughput.
2542 * this functionality is controlled by the InterruptThrottleRate module
2543 * parameter (see e1000_param.c)
Jesse Brandeburg835bb122006-11-01 08:48:13 -08002544 **/
2545static unsigned int e1000_update_itr(struct e1000_adapter *adapter,
Joe Perches64798842008-07-11 15:17:02 -07002546 u16 itr_setting, int packets, int bytes)
Jesse Brandeburg835bb122006-11-01 08:48:13 -08002547{
2548 unsigned int retval = itr_setting;
2549 struct e1000_hw *hw = &adapter->hw;
2550
2551 if (unlikely(hw->mac_type < e1000_82540))
2552 goto update_itr_done;
2553
2554 if (packets == 0)
2555 goto update_itr_done;
2556
Jesse Brandeburg835bb122006-11-01 08:48:13 -08002557 switch (itr_setting) {
2558 case lowest_latency:
Jesse Brandeburg2b653262006-12-15 10:30:44 +01002559 /* jumbo frames get bulk treatment*/
2560 if (bytes/packets > 8000)
2561 retval = bulk_latency;
2562 else if ((packets < 5) && (bytes > 512))
Jesse Brandeburg835bb122006-11-01 08:48:13 -08002563 retval = low_latency;
2564 break;
2565 case low_latency: /* 50 usec aka 20000 ints/s */
2566 if (bytes > 10000) {
Jesse Brandeburg2b653262006-12-15 10:30:44 +01002567 /* jumbo frames need bulk latency setting */
2568 if (bytes/packets > 8000)
2569 retval = bulk_latency;
2570 else if ((packets < 10) || ((bytes/packets) > 1200))
Jesse Brandeburg835bb122006-11-01 08:48:13 -08002571 retval = bulk_latency;
2572 else if ((packets > 35))
2573 retval = lowest_latency;
Jesse Brandeburg2b653262006-12-15 10:30:44 +01002574 } else if (bytes/packets > 2000)
2575 retval = bulk_latency;
2576 else if (packets <= 2 && bytes < 512)
Jesse Brandeburg835bb122006-11-01 08:48:13 -08002577 retval = lowest_latency;
2578 break;
2579 case bulk_latency: /* 250 usec aka 4000 ints/s */
2580 if (bytes > 25000) {
2581 if (packets > 35)
2582 retval = low_latency;
Jesse Brandeburg2b653262006-12-15 10:30:44 +01002583 } else if (bytes < 6000) {
2584 retval = low_latency;
Jesse Brandeburg835bb122006-11-01 08:48:13 -08002585 }
2586 break;
2587 }
2588
2589update_itr_done:
2590 return retval;
2591}
2592
2593static void e1000_set_itr(struct e1000_adapter *adapter)
2594{
2595 struct e1000_hw *hw = &adapter->hw;
Joe Perches406874a2008-04-03 10:06:32 -07002596 u16 current_itr;
2597 u32 new_itr = adapter->itr;
Jesse Brandeburg835bb122006-11-01 08:48:13 -08002598
2599 if (unlikely(hw->mac_type < e1000_82540))
2600 return;
2601
2602 /* for non-gigabit speeds, just fix the interrupt rate at 4000 */
2603 if (unlikely(adapter->link_speed != SPEED_1000)) {
2604 current_itr = 0;
2605 new_itr = 4000;
2606 goto set_itr_now;
2607 }
2608
2609 adapter->tx_itr = e1000_update_itr(adapter,
2610 adapter->tx_itr,
2611 adapter->total_tx_packets,
2612 adapter->total_tx_bytes);
Jesse Brandeburg2b653262006-12-15 10:30:44 +01002613 /* conservative mode (itr 3) eliminates the lowest_latency setting */
2614 if (adapter->itr_setting == 3 && adapter->tx_itr == lowest_latency)
2615 adapter->tx_itr = low_latency;
2616
Jesse Brandeburg835bb122006-11-01 08:48:13 -08002617 adapter->rx_itr = e1000_update_itr(adapter,
2618 adapter->rx_itr,
2619 adapter->total_rx_packets,
2620 adapter->total_rx_bytes);
Jesse Brandeburg2b653262006-12-15 10:30:44 +01002621 /* conservative mode (itr 3) eliminates the lowest_latency setting */
2622 if (adapter->itr_setting == 3 && adapter->rx_itr == lowest_latency)
2623 adapter->rx_itr = low_latency;
Jesse Brandeburg835bb122006-11-01 08:48:13 -08002624
2625 current_itr = max(adapter->rx_itr, adapter->tx_itr);
2626
Jesse Brandeburg835bb122006-11-01 08:48:13 -08002627 switch (current_itr) {
2628 /* counts and packets in update_itr are dependent on these numbers */
2629 case lowest_latency:
2630 new_itr = 70000;
2631 break;
2632 case low_latency:
2633 new_itr = 20000; /* aka hwitr = ~200 */
2634 break;
2635 case bulk_latency:
2636 new_itr = 4000;
2637 break;
2638 default:
2639 break;
2640 }
2641
2642set_itr_now:
2643 if (new_itr != adapter->itr) {
2644 /* this attempts to bias the interrupt rate towards Bulk
2645 * by adding intermediate steps when interrupt rate is
2646 * increasing */
2647 new_itr = new_itr > adapter->itr ?
2648 min(adapter->itr + (new_itr >> 2), new_itr) :
2649 new_itr;
2650 adapter->itr = new_itr;
Joe Perches1dc32912008-07-11 15:17:08 -07002651 ew32(ITR, 1000000000 / (new_itr * 256));
Jesse Brandeburg835bb122006-11-01 08:48:13 -08002652 }
Jesse Brandeburg835bb122006-11-01 08:48:13 -08002653}
2654
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655#define E1000_TX_FLAGS_CSUM 0x00000001
2656#define E1000_TX_FLAGS_VLAN 0x00000002
2657#define E1000_TX_FLAGS_TSO 0x00000004
Malli Chilakala2d7edb92005-04-28 19:43:52 -07002658#define E1000_TX_FLAGS_IPV4 0x00000008
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659#define E1000_TX_FLAGS_VLAN_MASK 0xffff0000
2660#define E1000_TX_FLAGS_VLAN_SHIFT 16
2661
Joe Perches64798842008-07-11 15:17:02 -07002662static int e1000_tso(struct e1000_adapter *adapter,
2663 struct e1000_tx_ring *tx_ring, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 struct e1000_context_desc *context_desc;
Jeff Kirsher545c67c2006-01-12 16:50:25 -08002666 struct e1000_buffer *buffer_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 unsigned int i;
Joe Perches406874a2008-04-03 10:06:32 -07002668 u32 cmd_length = 0;
2669 u16 ipcse = 0, tucse, mss;
2670 u8 ipcss, ipcso, tucss, tucso, hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 int err;
2672
Herbert Xu89114af2006-07-08 13:34:32 -07002673 if (skb_is_gso(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 if (skb_header_cloned(skb)) {
2675 err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2676 if (err)
2677 return err;
2678 }
2679
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07002680 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
Herbert Xu79671682006-06-22 02:40:14 -07002681 mss = skb_shinfo(skb)->gso_size;
Alexey Dobriyan60828232006-05-23 14:52:21 -07002682 if (skb->protocol == htons(ETH_P_IP)) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002683 struct iphdr *iph = ip_hdr(skb);
2684 iph->tot_len = 0;
2685 iph->check = 0;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07002686 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
2687 iph->daddr, 0,
2688 IPPROTO_TCP,
2689 0);
Malli Chilakala2d7edb92005-04-28 19:43:52 -07002690 cmd_length = E1000_TXD_CMD_IP;
Arnaldo Carvalho de Meloea2ae172007-04-25 17:55:53 -07002691 ipcse = skb_transport_offset(skb) - 1;
Auke Koke15fdd02006-08-16 11:28:45 -07002692 } else if (skb->protocol == htons(ETH_P_IPV6)) {
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07002693 ipv6_hdr(skb)->payload_len = 0;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07002694 tcp_hdr(skb)->check =
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07002695 ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
2696 &ipv6_hdr(skb)->daddr,
2697 0, IPPROTO_TCP, 0);
Malli Chilakala2d7edb92005-04-28 19:43:52 -07002698 ipcse = 0;
Malli Chilakala2d7edb92005-04-28 19:43:52 -07002699 }
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03002700 ipcss = skb_network_offset(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002701 ipcso = (void *)&(ip_hdr(skb)->check) - (void *)skb->data;
Arnaldo Carvalho de Meloea2ae172007-04-25 17:55:53 -07002702 tucss = skb_transport_offset(skb);
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07002703 tucso = (void *)&(tcp_hdr(skb)->check) - (void *)skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 tucse = 0;
2705
2706 cmd_length |= (E1000_TXD_CMD_DEXT | E1000_TXD_CMD_TSE |
Malli Chilakala2d7edb92005-04-28 19:43:52 -07002707 E1000_TXD_CMD_TCP | (skb->len - (hdr_len)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002709 i = tx_ring->next_to_use;
2710 context_desc = E1000_CONTEXT_DESC(*tx_ring, i);
Jeff Kirsher545c67c2006-01-12 16:50:25 -08002711 buffer_info = &tx_ring->buffer_info[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712
2713 context_desc->lower_setup.ip_fields.ipcss = ipcss;
2714 context_desc->lower_setup.ip_fields.ipcso = ipcso;
2715 context_desc->lower_setup.ip_fields.ipcse = cpu_to_le16(ipcse);
2716 context_desc->upper_setup.tcp_fields.tucss = tucss;
2717 context_desc->upper_setup.tcp_fields.tucso = tucso;
2718 context_desc->upper_setup.tcp_fields.tucse = cpu_to_le16(tucse);
2719 context_desc->tcp_seg_setup.fields.mss = cpu_to_le16(mss);
2720 context_desc->tcp_seg_setup.fields.hdr_len = hdr_len;
2721 context_desc->cmd_and_length = cpu_to_le32(cmd_length);
2722
Jeff Kirsher545c67c2006-01-12 16:50:25 -08002723 buffer_info->time_stamp = jiffies;
Jesse Brandeburga9ebadd2006-11-01 08:47:53 -08002724 buffer_info->next_to_watch = i;
Jeff Kirsher545c67c2006-01-12 16:50:25 -08002725
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002726 if (++i == tx_ring->count) i = 0;
2727 tx_ring->next_to_use = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728
Joe Perchesc3033b02008-03-21 11:06:25 -07002729 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 }
Joe Perchesc3033b02008-03-21 11:06:25 -07002731 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732}
2733
Joe Perches64798842008-07-11 15:17:02 -07002734static bool e1000_tx_csum(struct e1000_adapter *adapter,
2735 struct e1000_tx_ring *tx_ring, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736{
2737 struct e1000_context_desc *context_desc;
Jeff Kirsher545c67c2006-01-12 16:50:25 -08002738 struct e1000_buffer *buffer_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 unsigned int i;
Joe Perches406874a2008-04-03 10:06:32 -07002740 u8 css;
Dave Graham3ed30672008-10-09 14:29:26 -07002741 u32 cmd_len = E1000_TXD_CMD_DEXT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742
Dave Graham3ed30672008-10-09 14:29:26 -07002743 if (skb->ip_summed != CHECKSUM_PARTIAL)
2744 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745
Dave Graham3ed30672008-10-09 14:29:26 -07002746 switch (skb->protocol) {
Harvey Harrison09640e62009-02-01 00:45:17 -08002747 case cpu_to_be16(ETH_P_IP):
Dave Graham3ed30672008-10-09 14:29:26 -07002748 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
2749 cmd_len |= E1000_TXD_CMD_TCP;
2750 break;
Harvey Harrison09640e62009-02-01 00:45:17 -08002751 case cpu_to_be16(ETH_P_IPV6):
Dave Graham3ed30672008-10-09 14:29:26 -07002752 /* XXX not handling all IPV6 headers */
2753 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
2754 cmd_len |= E1000_TXD_CMD_TCP;
2755 break;
2756 default:
2757 if (unlikely(net_ratelimit()))
Emil Tantilovfeb8f472010-07-26 23:37:21 -07002758 e_warn(drv, "checksum_partial proto=%x!\n",
2759 skb->protocol);
Dave Graham3ed30672008-10-09 14:29:26 -07002760 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 }
2762
Michał Mirosław0d0b1672010-12-14 15:24:08 +00002763 css = skb_checksum_start_offset(skb);
Dave Graham3ed30672008-10-09 14:29:26 -07002764
2765 i = tx_ring->next_to_use;
2766 buffer_info = &tx_ring->buffer_info[i];
2767 context_desc = E1000_CONTEXT_DESC(*tx_ring, i);
2768
2769 context_desc->lower_setup.ip_config = 0;
2770 context_desc->upper_setup.tcp_fields.tucss = css;
2771 context_desc->upper_setup.tcp_fields.tucso =
2772 css + skb->csum_offset;
2773 context_desc->upper_setup.tcp_fields.tucse = 0;
2774 context_desc->tcp_seg_setup.data = 0;
2775 context_desc->cmd_and_length = cpu_to_le32(cmd_len);
2776
2777 buffer_info->time_stamp = jiffies;
2778 buffer_info->next_to_watch = i;
2779
2780 if (unlikely(++i == tx_ring->count)) i = 0;
2781 tx_ring->next_to_use = i;
2782
2783 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784}
2785
2786#define E1000_MAX_TXD_PWR 12
2787#define E1000_MAX_DATA_PER_TXD (1<<E1000_MAX_TXD_PWR)
2788
Joe Perches64798842008-07-11 15:17:02 -07002789static int e1000_tx_map(struct e1000_adapter *adapter,
2790 struct e1000_tx_ring *tx_ring,
2791 struct sk_buff *skb, unsigned int first,
2792 unsigned int max_per_txd, unsigned int nr_frags,
2793 unsigned int mss)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794{
Joe Perches1dc32912008-07-11 15:17:08 -07002795 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck602c0552009-12-02 16:46:00 +00002796 struct pci_dev *pdev = adapter->pdev;
Alexander Duyck37e73df2009-03-25 21:58:45 +00002797 struct e1000_buffer *buffer_info;
Jesse Brandeburgd20b6062009-03-02 16:03:21 -08002798 unsigned int len = skb_headlen(skb);
Alexander Duyck602c0552009-12-02 16:46:00 +00002799 unsigned int offset = 0, size, count = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 unsigned int f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801
2802 i = tx_ring->next_to_use;
2803
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002804 while (len) {
Alexander Duyck37e73df2009-03-25 21:58:45 +00002805 buffer_info = &tx_ring->buffer_info[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 size = min(len, max_per_txd);
Jeff Kirsherfd803242005-12-13 00:06:22 -05002807 /* Workaround for Controller erratum --
2808 * descriptor for non-tso packet in a linear SKB that follows a
2809 * tso gets written back prematurely before the data is fully
Jeff Kirsher0f15a8f2006-03-02 18:46:29 -08002810 * DMA'd to the controller */
Jeff Kirsherfd803242005-12-13 00:06:22 -05002811 if (!skb->data_len && tx_ring->last_tx_tso &&
Herbert Xu89114af2006-07-08 13:34:32 -07002812 !skb_is_gso(skb)) {
Jeff Kirsherfd803242005-12-13 00:06:22 -05002813 tx_ring->last_tx_tso = 0;
2814 size -= 4;
2815 }
2816
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 /* Workaround for premature desc write-backs
2818 * in TSO mode. Append 4-byte sentinel desc */
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002819 if (unlikely(mss && !nr_frags && size == len && size > 8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 size -= 4;
Malli Chilakala97338bd2005-04-28 19:41:46 -07002821 /* work-around for errata 10 and it applies
2822 * to all controllers in PCI-X mode
2823 * The fix is to make sure that the first descriptor of a
2824 * packet is smaller than 2048 - 16 - 16 (or 2016) bytes
2825 */
Joe Perches1dc32912008-07-11 15:17:08 -07002826 if (unlikely((hw->bus_type == e1000_bus_type_pcix) &&
Malli Chilakala97338bd2005-04-28 19:41:46 -07002827 (size > 2015) && count == 0))
2828 size = 2015;
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002829
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 /* Workaround for potential 82544 hang in PCI-X. Avoid
2831 * terminating buffers within evenly-aligned dwords. */
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002832 if (unlikely(adapter->pcix_82544 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 !((unsigned long)(skb->data + offset + size - 1) & 4) &&
2834 size > 4))
2835 size -= 4;
2836
2837 buffer_info->length = size;
Jesse Brandeburgcdd75492009-09-25 12:18:07 +00002838 /* set time_stamp *before* dma to help avoid a possible race */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 buffer_info->time_stamp = jiffies;
Alexander Duyck602c0552009-12-02 16:46:00 +00002840 buffer_info->mapped_as_page = false;
Nick Nunleyb16f53b2010-04-27 13:08:45 +00002841 buffer_info->dma = dma_map_single(&pdev->dev,
2842 skb->data + offset,
2843 size, DMA_TO_DEVICE);
2844 if (dma_mapping_error(&pdev->dev, buffer_info->dma))
Alexander Duyck602c0552009-12-02 16:46:00 +00002845 goto dma_error;
Jesse Brandeburga9ebadd2006-11-01 08:47:53 -08002846 buffer_info->next_to_watch = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847
2848 len -= size;
2849 offset += size;
2850 count++;
Alexander Duyck37e73df2009-03-25 21:58:45 +00002851 if (len) {
2852 i++;
2853 if (unlikely(i == tx_ring->count))
2854 i = 0;
2855 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 }
2857
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002858 for (f = 0; f < nr_frags; f++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 struct skb_frag_struct *frag;
2860
2861 frag = &skb_shinfo(skb)->frags[f];
2862 len = frag->size;
Alexander Duyck602c0552009-12-02 16:46:00 +00002863 offset = frag->page_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002865 while (len) {
Alexander Duyck37e73df2009-03-25 21:58:45 +00002866 i++;
2867 if (unlikely(i == tx_ring->count))
2868 i = 0;
2869
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 buffer_info = &tx_ring->buffer_info[i];
2871 size = min(len, max_per_txd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 /* Workaround for premature desc write-backs
2873 * in TSO mode. Append 4-byte sentinel desc */
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002874 if (unlikely(mss && f == (nr_frags-1) && size == len && size > 8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 size -= 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 /* Workaround for potential 82544 hang in PCI-X.
2877 * Avoid terminating buffers within evenly-aligned
2878 * dwords. */
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002879 if (unlikely(adapter->pcix_82544 &&
Jesse Brandeburg8fce4732009-09-25 12:18:41 +00002880 !((unsigned long)(page_to_phys(frag->page) + offset
2881 + size - 1) & 4) &&
2882 size > 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 size -= 4;
2884
2885 buffer_info->length = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 buffer_info->time_stamp = jiffies;
Alexander Duyck602c0552009-12-02 16:46:00 +00002887 buffer_info->mapped_as_page = true;
Nick Nunleyb16f53b2010-04-27 13:08:45 +00002888 buffer_info->dma = dma_map_page(&pdev->dev, frag->page,
Alexander Duyck602c0552009-12-02 16:46:00 +00002889 offset, size,
Nick Nunleyb16f53b2010-04-27 13:08:45 +00002890 DMA_TO_DEVICE);
2891 if (dma_mapping_error(&pdev->dev, buffer_info->dma))
Alexander Duyck602c0552009-12-02 16:46:00 +00002892 goto dma_error;
Jesse Brandeburga9ebadd2006-11-01 08:47:53 -08002893 buffer_info->next_to_watch = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894
2895 len -= size;
2896 offset += size;
2897 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 }
2899 }
2900
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 tx_ring->buffer_info[i].skb = skb;
2902 tx_ring->buffer_info[first].next_to_watch = i;
2903
2904 return count;
Alexander Duyck602c0552009-12-02 16:46:00 +00002905
2906dma_error:
2907 dev_err(&pdev->dev, "TX DMA map failed\n");
2908 buffer_info->dma = 0;
Roel Kluinc1fa3472010-01-19 14:21:45 +00002909 if (count)
Alexander Duyck602c0552009-12-02 16:46:00 +00002910 count--;
Roel Kluinc1fa3472010-01-19 14:21:45 +00002911
2912 while (count--) {
2913 if (i==0)
Alexander Duyck602c0552009-12-02 16:46:00 +00002914 i += tx_ring->count;
Roel Kluinc1fa3472010-01-19 14:21:45 +00002915 i--;
Alexander Duyck602c0552009-12-02 16:46:00 +00002916 buffer_info = &tx_ring->buffer_info[i];
2917 e1000_unmap_and_free_tx_resource(adapter, buffer_info);
2918 }
2919
2920 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921}
2922
Joe Perches64798842008-07-11 15:17:02 -07002923static void e1000_tx_queue(struct e1000_adapter *adapter,
2924 struct e1000_tx_ring *tx_ring, int tx_flags,
2925 int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926{
Joe Perches1dc32912008-07-11 15:17:08 -07002927 struct e1000_hw *hw = &adapter->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 struct e1000_tx_desc *tx_desc = NULL;
2929 struct e1000_buffer *buffer_info;
Joe Perches406874a2008-04-03 10:06:32 -07002930 u32 txd_upper = 0, txd_lower = E1000_TXD_CMD_IFCS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 unsigned int i;
2932
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002933 if (likely(tx_flags & E1000_TX_FLAGS_TSO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 txd_lower |= E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D |
2935 E1000_TXD_CMD_TSE;
Malli Chilakala2d7edb92005-04-28 19:43:52 -07002936 txd_upper |= E1000_TXD_POPTS_TXSM << 8;
2937
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002938 if (likely(tx_flags & E1000_TX_FLAGS_IPV4))
Malli Chilakala2d7edb92005-04-28 19:43:52 -07002939 txd_upper |= E1000_TXD_POPTS_IXSM << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 }
2941
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002942 if (likely(tx_flags & E1000_TX_FLAGS_CSUM)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 txd_lower |= E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D;
2944 txd_upper |= E1000_TXD_POPTS_TXSM << 8;
2945 }
2946
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002947 if (unlikely(tx_flags & E1000_TX_FLAGS_VLAN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 txd_lower |= E1000_TXD_CMD_VLE;
2949 txd_upper |= (tx_flags & E1000_TX_FLAGS_VLAN_MASK);
2950 }
2951
2952 i = tx_ring->next_to_use;
2953
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002954 while (count--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 buffer_info = &tx_ring->buffer_info[i];
2956 tx_desc = E1000_TX_DESC(*tx_ring, i);
2957 tx_desc->buffer_addr = cpu_to_le64(buffer_info->dma);
2958 tx_desc->lower.data =
2959 cpu_to_le32(txd_lower | buffer_info->length);
2960 tx_desc->upper.data = cpu_to_le32(txd_upper);
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002961 if (unlikely(++i == tx_ring->count)) i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 }
2963
2964 tx_desc->lower.data |= cpu_to_le32(adapter->txd_cmd);
2965
2966 /* Force memory writes to complete before letting h/w
2967 * know there are new descriptors to fetch. (Only
2968 * applicable for weak-ordered memory model archs,
2969 * such as IA-64). */
2970 wmb();
2971
2972 tx_ring->next_to_use = i;
Joe Perches1dc32912008-07-11 15:17:08 -07002973 writel(i, hw->hw_addr + tx_ring->tdt);
Jesse Brandeburg2ce90472006-11-01 08:47:42 -08002974 /* we need this if more than one processor can write to our tail
2975 * at a time, it syncronizes IO on IA64/Altix systems */
2976 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977}
2978
2979/**
2980 * 82547 workaround to avoid controller hang in half-duplex environment.
2981 * The workaround is to avoid queuing a large packet that would span
2982 * the internal Tx FIFO ring boundary by notifying the stack to resend
2983 * the packet at a later time. This gives the Tx FIFO an opportunity to
2984 * flush all packets. When that occurs, we reset the Tx FIFO pointers
2985 * to the beginning of the Tx FIFO.
2986 **/
2987
2988#define E1000_FIFO_HDR 0x10
2989#define E1000_82547_PAD_LEN 0x3E0
2990
Joe Perches64798842008-07-11 15:17:02 -07002991static int e1000_82547_fifo_workaround(struct e1000_adapter *adapter,
2992 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993{
Joe Perches406874a2008-04-03 10:06:32 -07002994 u32 fifo_space = adapter->tx_fifo_size - adapter->tx_fifo_head;
2995 u32 skb_fifo_len = skb->len + E1000_FIFO_HDR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996
Milind Arun Choudhary9099cfb2007-04-27 13:55:29 -07002997 skb_fifo_len = ALIGN(skb_fifo_len, E1000_FIFO_HDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002999 if (adapter->link_duplex != HALF_DUPLEX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 goto no_fifo_stall_required;
3001
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003002 if (atomic_read(&adapter->tx_fifo_stall))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 return 1;
3004
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003005 if (skb_fifo_len >= (E1000_82547_PAD_LEN + fifo_space)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 atomic_set(&adapter->tx_fifo_stall, 1);
3007 return 1;
3008 }
3009
3010no_fifo_stall_required:
3011 adapter->tx_fifo_head += skb_fifo_len;
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003012 if (adapter->tx_fifo_head >= adapter->tx_fifo_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 adapter->tx_fifo_head -= adapter->tx_fifo_size;
3014 return 0;
3015}
3016
Jesse Brandeburg65c79732006-09-27 12:53:48 -07003017static int __e1000_maybe_stop_tx(struct net_device *netdev, int size)
3018{
3019 struct e1000_adapter *adapter = netdev_priv(netdev);
3020 struct e1000_tx_ring *tx_ring = adapter->tx_ring;
3021
3022 netif_stop_queue(netdev);
3023 /* Herbert's original patch had:
3024 * smp_mb__after_netif_stop_queue();
3025 * but since that doesn't exist yet, just open code it. */
3026 smp_mb();
3027
3028 /* We need to check again in a case another CPU has just
3029 * made room available. */
3030 if (likely(E1000_DESC_UNUSED(tx_ring) < size))
3031 return -EBUSY;
3032
3033 /* A reprieve! */
3034 netif_start_queue(netdev);
Jesse Brandeburgfcfb1222006-11-01 08:47:59 -08003035 ++adapter->restart_queue;
Jesse Brandeburg65c79732006-09-27 12:53:48 -07003036 return 0;
3037}
3038
3039static int e1000_maybe_stop_tx(struct net_device *netdev,
3040 struct e1000_tx_ring *tx_ring, int size)
3041{
3042 if (likely(E1000_DESC_UNUSED(tx_ring) >= size))
3043 return 0;
3044 return __e1000_maybe_stop_tx(netdev, size);
3045}
3046
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047#define TXD_USE_COUNT(S, X) (((S) >> (X)) + 1 )
Stephen Hemminger3b29a562009-08-31 19:50:55 +00003048static netdev_tx_t e1000_xmit_frame(struct sk_buff *skb,
3049 struct net_device *netdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050{
Malli Chilakala60490fe2005-06-17 17:41:45 -07003051 struct e1000_adapter *adapter = netdev_priv(netdev);
Joe Perches1dc32912008-07-11 15:17:08 -07003052 struct e1000_hw *hw = &adapter->hw;
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003053 struct e1000_tx_ring *tx_ring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 unsigned int first, max_per_txd = E1000_MAX_DATA_PER_TXD;
3055 unsigned int max_txd_pwr = E1000_MAX_TXD_PWR;
3056 unsigned int tx_flags = 0;
Eric Dumazete743d312010-04-14 15:59:40 -07003057 unsigned int len = skb_headlen(skb);
Krishna Kumar6d1e3aa2007-10-05 14:15:16 -07003058 unsigned int nr_frags;
3059 unsigned int mss;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 int count = 0;
Auke Kok76c224b2006-05-23 13:36:06 -07003061 int tso;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 unsigned int f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063
Jesse Brandeburg65c79732006-09-27 12:53:48 -07003064 /* This goes back to the question of how to logically map a tx queue
3065 * to a flow. Right now, performance is impacted slightly negatively
3066 * if using multiple tx queues. If the stack breaks away from a
3067 * single qdisc implementation, we can look at this again. */
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003068 tx_ring = adapter->tx_ring;
Mallikarjuna R Chilakala24025e42005-10-04 07:03:23 -04003069
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003070 if (unlikely(skb->len <= 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 dev_kfree_skb_any(skb);
3072 return NETDEV_TX_OK;
3073 }
3074
Herbert Xu79671682006-06-22 02:40:14 -07003075 mss = skb_shinfo(skb)->gso_size;
Auke Kok76c224b2006-05-23 13:36:06 -07003076 /* The controller does a simple calculation to
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 * make sure there is enough room in the FIFO before
3078 * initiating the DMA for each buffer. The calc is:
3079 * 4 = ceil(buffer len/mss). To make sure we don't
3080 * overrun the FIFO, adjust the max buffer len if mss
3081 * drops. */
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003082 if (mss) {
Joe Perches406874a2008-04-03 10:06:32 -07003083 u8 hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 max_per_txd = min(mss << 2, max_per_txd);
3085 max_txd_pwr = fls(max_per_txd) - 1;
Jeff Kirsher9a3056d2006-01-12 16:50:23 -08003086
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07003087 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
Krishna Kumar6d1e3aa2007-10-05 14:15:16 -07003088 if (skb->data_len && hdr_len == len) {
Joe Perches1dc32912008-07-11 15:17:08 -07003089 switch (hw->mac_type) {
Jeff Kirsher9f687882006-03-02 18:20:17 -08003090 unsigned int pull_size;
Herbert Xu683a2aa2006-12-16 12:04:33 +11003091 case e1000_82544:
3092 /* Make sure we have room to chop off 4 bytes,
3093 * and that the end alignment will work out to
3094 * this hardware's requirements
3095 * NOTE: this is a TSO only workaround
3096 * if end byte alignment not correct move us
3097 * into the next dword */
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07003098 if ((unsigned long)(skb_tail_pointer(skb) - 1) & 4)
Herbert Xu683a2aa2006-12-16 12:04:33 +11003099 break;
3100 /* fall through */
Jeff Kirsher9f687882006-03-02 18:20:17 -08003101 pull_size = min((unsigned int)4, skb->data_len);
3102 if (!__pskb_pull_tail(skb, pull_size)) {
Emil Tantilovfeb8f472010-07-26 23:37:21 -07003103 e_err(drv, "__pskb_pull_tail "
3104 "failed.\n");
Jeff Kirsher9f687882006-03-02 18:20:17 -08003105 dev_kfree_skb_any(skb);
Jeff Garzik749dfc72006-03-11 13:35:31 -05003106 return NETDEV_TX_OK;
Jeff Kirsher9f687882006-03-02 18:20:17 -08003107 }
Eric Dumazete743d312010-04-14 15:59:40 -07003108 len = skb_headlen(skb);
Jeff Kirsher9f687882006-03-02 18:20:17 -08003109 break;
3110 default:
3111 /* do nothing */
3112 break;
Jeff Kirsherd74bbd32006-01-12 16:51:07 -08003113 }
Jeff Kirsher9a3056d2006-01-12 16:50:23 -08003114 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 }
3116
Jeff Kirsher9a3056d2006-01-12 16:50:23 -08003117 /* reserve a descriptor for the offload context */
Patrick McHardy84fa7932006-08-29 16:44:56 -07003118 if ((mss) || (skb->ip_summed == CHECKSUM_PARTIAL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 count++;
Malli Chilakala26483452005-04-28 19:44:46 -07003120 count++;
Jeff Kirsherfd803242005-12-13 00:06:22 -05003121
Jeff Kirsherfd803242005-12-13 00:06:22 -05003122 /* Controller Erratum workaround */
Herbert Xu89114af2006-07-08 13:34:32 -07003123 if (!skb->data_len && tx_ring->last_tx_tso && !skb_is_gso(skb))
Jeff Kirsherfd803242005-12-13 00:06:22 -05003124 count++;
Jeff Kirsherfd803242005-12-13 00:06:22 -05003125
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 count += TXD_USE_COUNT(len, max_txd_pwr);
3127
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003128 if (adapter->pcix_82544)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 count++;
3130
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003131 /* work-around for errata 10 and it applies to all controllers
Malli Chilakala97338bd2005-04-28 19:41:46 -07003132 * in PCI-X mode, so add one more descriptor to the count
3133 */
Joe Perches1dc32912008-07-11 15:17:08 -07003134 if (unlikely((hw->bus_type == e1000_bus_type_pcix) &&
Malli Chilakala97338bd2005-04-28 19:41:46 -07003135 (len > 2015)))
3136 count++;
3137
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 nr_frags = skb_shinfo(skb)->nr_frags;
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003139 for (f = 0; f < nr_frags; f++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 count += TXD_USE_COUNT(skb_shinfo(skb)->frags[f].size,
3141 max_txd_pwr);
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003142 if (adapter->pcix_82544)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143 count += nr_frags;
3144
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 /* need: count + 2 desc gap to keep tail from touching
3146 * head, otherwise try next time */
Alexander Duyck80179432009-01-21 14:42:47 -08003147 if (unlikely(e1000_maybe_stop_tx(netdev, tx_ring, count + 2)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149
Joe Perches1dc32912008-07-11 15:17:08 -07003150 if (unlikely(hw->mac_type == e1000_82547)) {
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003151 if (unlikely(e1000_82547_fifo_workaround(adapter, skb))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 netif_stop_queue(netdev);
Jesse Brandeburgbaa34742009-09-25 12:17:23 +00003153 if (!test_bit(__E1000_DOWN, &adapter->flags))
3154 mod_timer(&adapter->tx_fifo_stall_timer,
3155 jiffies + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 return NETDEV_TX_BUSY;
3157 }
3158 }
3159
Jesse Grosseab6d182010-10-20 13:56:03 +00003160 if (unlikely(vlan_tx_tag_present(skb))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 tx_flags |= E1000_TX_FLAGS_VLAN;
3162 tx_flags |= (vlan_tx_tag_get(skb) << E1000_TX_FLAGS_VLAN_SHIFT);
3163 }
3164
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003165 first = tx_ring->next_to_use;
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003166
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003167 tso = e1000_tso(adapter, tx_ring, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 if (tso < 0) {
3169 dev_kfree_skb_any(skb);
3170 return NETDEV_TX_OK;
3171 }
3172
Jeff Kirsherfd803242005-12-13 00:06:22 -05003173 if (likely(tso)) {
Jesse Brandeburg8fce4732009-09-25 12:18:41 +00003174 if (likely(hw->mac_type != e1000_82544))
3175 tx_ring->last_tx_tso = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176 tx_flags |= E1000_TX_FLAGS_TSO;
Jeff Kirsherfd803242005-12-13 00:06:22 -05003177 } else if (likely(e1000_tx_csum(adapter, tx_ring, skb)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 tx_flags |= E1000_TX_FLAGS_CSUM;
3179
Alexey Dobriyan60828232006-05-23 14:52:21 -07003180 if (likely(skb->protocol == htons(ETH_P_IP)))
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003181 tx_flags |= E1000_TX_FLAGS_IPV4;
3182
Alexander Duyck37e73df2009-03-25 21:58:45 +00003183 count = e1000_tx_map(adapter, tx_ring, skb, first, max_per_txd,
3184 nr_frags, mss);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185
Alexander Duyck37e73df2009-03-25 21:58:45 +00003186 if (count) {
3187 e1000_tx_queue(adapter, tx_ring, tx_flags, count);
Alexander Duyck37e73df2009-03-25 21:58:45 +00003188 /* Make sure there is space in the ring for the next send. */
3189 e1000_maybe_stop_tx(netdev, tx_ring, MAX_SKB_FRAGS + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190
Alexander Duyck37e73df2009-03-25 21:58:45 +00003191 } else {
3192 dev_kfree_skb_any(skb);
3193 tx_ring->buffer_info[first].time_stamp = 0;
3194 tx_ring->next_to_use = first;
3195 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 return NETDEV_TX_OK;
3198}
3199
3200/**
3201 * e1000_tx_timeout - Respond to a Tx Hang
3202 * @netdev: network interface device structure
3203 **/
3204
Joe Perches64798842008-07-11 15:17:02 -07003205static void e1000_tx_timeout(struct net_device *netdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206{
Malli Chilakala60490fe2005-06-17 17:41:45 -07003207 struct e1000_adapter *adapter = netdev_priv(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208
3209 /* Do the reset outside of interrupt context */
Jeff Kirsher87041632006-03-02 18:21:24 -08003210 adapter->tx_timeout_count++;
3211 schedule_work(&adapter->reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212}
3213
Joe Perches64798842008-07-11 15:17:02 -07003214static void e1000_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215{
David Howells65f27f32006-11-22 14:55:48 +00003216 struct e1000_adapter *adapter =
3217 container_of(work, struct e1000_adapter, reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218
Jesse Brandeburg338c15e2010-09-22 18:22:42 +00003219 e1000_reinit_safe(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220}
3221
3222/**
3223 * e1000_get_stats - Get System Network Statistics
3224 * @netdev: network interface device structure
3225 *
3226 * Returns the address of the device statistics structure.
3227 * The statistics are actually updated from the timer callback.
3228 **/
3229
Joe Perches64798842008-07-11 15:17:02 -07003230static struct net_device_stats *e1000_get_stats(struct net_device *netdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231{
Jeff Kirsher6b7660c2006-01-12 16:50:35 -08003232 /* only return the current stats */
Ajit Khaparde5fe31de2009-10-07 02:42:23 +00003233 return &netdev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234}
3235
3236/**
3237 * e1000_change_mtu - Change the Maximum Transfer Unit
3238 * @netdev: network interface device structure
3239 * @new_mtu: new value for maximum frame size
3240 *
3241 * Returns 0 on success, negative on failure
3242 **/
3243
Joe Perches64798842008-07-11 15:17:02 -07003244static int e1000_change_mtu(struct net_device *netdev, int new_mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245{
Malli Chilakala60490fe2005-06-17 17:41:45 -07003246 struct e1000_adapter *adapter = netdev_priv(netdev);
Joe Perches1dc32912008-07-11 15:17:08 -07003247 struct e1000_hw *hw = &adapter->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 int max_frame = new_mtu + ENET_HEADER_SIZE + ETHERNET_FCS_SIZE;
3249
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003250 if ((max_frame < MINIMUM_ETHERNET_FRAME_SIZE) ||
3251 (max_frame > MAX_JUMBO_FRAME_SIZE)) {
Emil Tantilovfeb8f472010-07-26 23:37:21 -07003252 e_err(probe, "Invalid MTU setting\n");
Mallikarjuna R Chilakala868d5302005-10-04 06:58:59 -04003253 return -EINVAL;
3254 }
3255
Jeff Kirsher997f5cb2006-01-12 16:50:55 -08003256 /* Adapter-specific max frame size limits. */
Joe Perches1dc32912008-07-11 15:17:08 -07003257 switch (hw->mac_type) {
Auke Kok9e2feac2006-04-14 19:05:18 -07003258 case e1000_undefined ... e1000_82542_rev2_1:
Jesse Brandeburgb7cb8c22009-07-06 10:45:01 +00003259 if (max_frame > (ETH_FRAME_LEN + ETH_FCS_LEN)) {
Emil Tantilovfeb8f472010-07-26 23:37:21 -07003260 e_err(probe, "Jumbo Frames not supported.\n");
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003261 return -EINVAL;
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003262 }
Jeff Kirsher997f5cb2006-01-12 16:50:55 -08003263 break;
Jeff Kirsher997f5cb2006-01-12 16:50:55 -08003264 default:
3265 /* Capable of supporting up to MAX_JUMBO_FRAME_SIZE limit. */
3266 break;
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003267 }
3268
Jesse Brandeburg3d6114e2009-09-25 12:19:02 +00003269 while (test_and_set_bit(__E1000_RESETTING, &adapter->flags))
3270 msleep(1);
3271 /* e1000_down has a dependency on max_frame_size */
3272 hw->max_frame_size = max_frame;
3273 if (netif_running(netdev))
3274 e1000_down(adapter);
3275
David S. Miller87f50322006-07-31 22:39:40 -07003276 /* NOTE: netdev_alloc_skb reserves 16 bytes, and typically NET_IP_ALIGN
Auke Kok9e2feac2006-04-14 19:05:18 -07003277 * means we reserve 2 more, this pushes us to allocate from the next
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00003278 * larger slab size.
3279 * i.e. RXBUFFER_2048 --> size-4096 slab
3280 * however with the new *_jumbo_rx* routines, jumbo receives will use
3281 * fragmented skbs */
Jeff Kirsher997f5cb2006-01-12 16:50:55 -08003282
Jesse Brandeburg99261462010-01-22 22:56:16 +00003283 if (max_frame <= E1000_RXBUFFER_2048)
Auke Kok9e2feac2006-04-14 19:05:18 -07003284 adapter->rx_buffer_len = E1000_RXBUFFER_2048;
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00003285 else
3286#if (PAGE_SIZE >= E1000_RXBUFFER_16384)
Auke Kok9e2feac2006-04-14 19:05:18 -07003287 adapter->rx_buffer_len = E1000_RXBUFFER_16384;
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00003288#elif (PAGE_SIZE >= E1000_RXBUFFER_4096)
3289 adapter->rx_buffer_len = PAGE_SIZE;
3290#endif
Auke Kok9e2feac2006-04-14 19:05:18 -07003291
3292 /* adjust allocation if LPE protects us, and we aren't using SBP */
Joe Perches1dc32912008-07-11 15:17:08 -07003293 if (!hw->tbi_compatibility_on &&
Jesse Brandeburgb7cb8c22009-07-06 10:45:01 +00003294 ((max_frame == (ETH_FRAME_LEN + ETH_FCS_LEN)) ||
Auke Kok9e2feac2006-04-14 19:05:18 -07003295 (max_frame == MAXIMUM_ETHERNET_VLAN_SIZE)))
3296 adapter->rx_buffer_len = MAXIMUM_ETHERNET_VLAN_SIZE;
Jeff Kirsher997f5cb2006-01-12 16:50:55 -08003297
Emil Tantilov675ad472010-04-27 14:02:58 +00003298 pr_info("%s changing MTU from %d to %d\n",
3299 netdev->name, netdev->mtu, new_mtu);
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003300 netdev->mtu = new_mtu;
3301
Auke Kok2db10a02006-06-27 09:06:28 -07003302 if (netif_running(netdev))
Jesse Brandeburg3d6114e2009-09-25 12:19:02 +00003303 e1000_up(adapter);
3304 else
3305 e1000_reset(adapter);
3306
3307 clear_bit(__E1000_RESETTING, &adapter->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 return 0;
3310}
3311
3312/**
3313 * e1000_update_stats - Update the board statistics counters
3314 * @adapter: board private structure
3315 **/
3316
Joe Perches64798842008-07-11 15:17:02 -07003317void e1000_update_stats(struct e1000_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318{
Ajit Khaparde5fe31de2009-10-07 02:42:23 +00003319 struct net_device *netdev = adapter->netdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320 struct e1000_hw *hw = &adapter->hw;
Linas Vepstas282f33c2006-06-08 22:19:44 -07003321 struct pci_dev *pdev = adapter->pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 unsigned long flags;
Joe Perches406874a2008-04-03 10:06:32 -07003323 u16 phy_tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324
3325#define PHY_IDLE_ERROR_COUNT_MASK 0x00FF
3326
Linas Vepstas282f33c2006-06-08 22:19:44 -07003327 /*
3328 * Prevent stats update while adapter is being reset, or if the pci
3329 * connection is down.
3330 */
Auke Kok90267292006-06-08 09:30:24 -07003331 if (adapter->link_speed == 0)
3332 return;
Linas Vepstas81b19552006-12-12 18:29:15 -06003333 if (pci_channel_offline(pdev))
Linas Vepstas282f33c2006-06-08 22:19:44 -07003334 return;
Auke Kok90267292006-06-08 09:30:24 -07003335
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 spin_lock_irqsave(&adapter->stats_lock, flags);
3337
Masatake YAMATO828d0552007-10-20 03:06:37 +02003338 /* these counters are modified from e1000_tbi_adjust_stats,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 * called from the interrupt context, so they must only
3340 * be written while holding adapter->stats_lock
3341 */
3342
Joe Perches1dc32912008-07-11 15:17:08 -07003343 adapter->stats.crcerrs += er32(CRCERRS);
3344 adapter->stats.gprc += er32(GPRC);
3345 adapter->stats.gorcl += er32(GORCL);
3346 adapter->stats.gorch += er32(GORCH);
3347 adapter->stats.bprc += er32(BPRC);
3348 adapter->stats.mprc += er32(MPRC);
3349 adapter->stats.roc += er32(ROC);
Auke Kokcd94dd02006-06-27 09:08:22 -07003350
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00003351 adapter->stats.prc64 += er32(PRC64);
3352 adapter->stats.prc127 += er32(PRC127);
3353 adapter->stats.prc255 += er32(PRC255);
3354 adapter->stats.prc511 += er32(PRC511);
3355 adapter->stats.prc1023 += er32(PRC1023);
3356 adapter->stats.prc1522 += er32(PRC1522);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357
Joe Perches1dc32912008-07-11 15:17:08 -07003358 adapter->stats.symerrs += er32(SYMERRS);
3359 adapter->stats.mpc += er32(MPC);
3360 adapter->stats.scc += er32(SCC);
3361 adapter->stats.ecol += er32(ECOL);
3362 adapter->stats.mcc += er32(MCC);
3363 adapter->stats.latecol += er32(LATECOL);
3364 adapter->stats.dc += er32(DC);
3365 adapter->stats.sec += er32(SEC);
3366 adapter->stats.rlec += er32(RLEC);
3367 adapter->stats.xonrxc += er32(XONRXC);
3368 adapter->stats.xontxc += er32(XONTXC);
3369 adapter->stats.xoffrxc += er32(XOFFRXC);
3370 adapter->stats.xofftxc += er32(XOFFTXC);
3371 adapter->stats.fcruc += er32(FCRUC);
3372 adapter->stats.gptc += er32(GPTC);
3373 adapter->stats.gotcl += er32(GOTCL);
3374 adapter->stats.gotch += er32(GOTCH);
3375 adapter->stats.rnbc += er32(RNBC);
3376 adapter->stats.ruc += er32(RUC);
3377 adapter->stats.rfc += er32(RFC);
3378 adapter->stats.rjc += er32(RJC);
3379 adapter->stats.torl += er32(TORL);
3380 adapter->stats.torh += er32(TORH);
3381 adapter->stats.totl += er32(TOTL);
3382 adapter->stats.toth += er32(TOTH);
3383 adapter->stats.tpr += er32(TPR);
Auke Kokcd94dd02006-06-27 09:08:22 -07003384
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00003385 adapter->stats.ptc64 += er32(PTC64);
3386 adapter->stats.ptc127 += er32(PTC127);
3387 adapter->stats.ptc255 += er32(PTC255);
3388 adapter->stats.ptc511 += er32(PTC511);
3389 adapter->stats.ptc1023 += er32(PTC1023);
3390 adapter->stats.ptc1522 += er32(PTC1522);
Auke Kokcd94dd02006-06-27 09:08:22 -07003391
Joe Perches1dc32912008-07-11 15:17:08 -07003392 adapter->stats.mptc += er32(MPTC);
3393 adapter->stats.bptc += er32(BPTC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394
3395 /* used for adaptive IFS */
3396
Joe Perches1dc32912008-07-11 15:17:08 -07003397 hw->tx_packet_delta = er32(TPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398 adapter->stats.tpt += hw->tx_packet_delta;
Joe Perches1dc32912008-07-11 15:17:08 -07003399 hw->collision_delta = er32(COLC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400 adapter->stats.colc += hw->collision_delta;
3401
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003402 if (hw->mac_type >= e1000_82543) {
Joe Perches1dc32912008-07-11 15:17:08 -07003403 adapter->stats.algnerrc += er32(ALGNERRC);
3404 adapter->stats.rxerrc += er32(RXERRC);
3405 adapter->stats.tncrs += er32(TNCRS);
3406 adapter->stats.cexterr += er32(CEXTERR);
3407 adapter->stats.tsctc += er32(TSCTC);
3408 adapter->stats.tsctfc += er32(TSCTFC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409 }
3410
3411 /* Fill out the OS statistics structure */
Ajit Khaparde5fe31de2009-10-07 02:42:23 +00003412 netdev->stats.multicast = adapter->stats.mprc;
3413 netdev->stats.collisions = adapter->stats.colc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414
3415 /* Rx Errors */
3416
Jeff Kirsher87041632006-03-02 18:21:24 -08003417 /* RLEC on some newer hardware can be incorrect so build
3418 * our own version based on RUC and ROC */
Ajit Khaparde5fe31de2009-10-07 02:42:23 +00003419 netdev->stats.rx_errors = adapter->stats.rxerrc +
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420 adapter->stats.crcerrs + adapter->stats.algnerrc +
Jeff Kirsher87041632006-03-02 18:21:24 -08003421 adapter->stats.ruc + adapter->stats.roc +
3422 adapter->stats.cexterr;
Mitch Williams49559852006-09-27 12:53:37 -07003423 adapter->stats.rlerrc = adapter->stats.ruc + adapter->stats.roc;
Ajit Khaparde5fe31de2009-10-07 02:42:23 +00003424 netdev->stats.rx_length_errors = adapter->stats.rlerrc;
3425 netdev->stats.rx_crc_errors = adapter->stats.crcerrs;
3426 netdev->stats.rx_frame_errors = adapter->stats.algnerrc;
3427 netdev->stats.rx_missed_errors = adapter->stats.mpc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428
3429 /* Tx Errors */
Mitch Williams49559852006-09-27 12:53:37 -07003430 adapter->stats.txerrc = adapter->stats.ecol + adapter->stats.latecol;
Ajit Khaparde5fe31de2009-10-07 02:42:23 +00003431 netdev->stats.tx_errors = adapter->stats.txerrc;
3432 netdev->stats.tx_aborted_errors = adapter->stats.ecol;
3433 netdev->stats.tx_window_errors = adapter->stats.latecol;
3434 netdev->stats.tx_carrier_errors = adapter->stats.tncrs;
Joe Perches1dc32912008-07-11 15:17:08 -07003435 if (hw->bad_tx_carr_stats_fd &&
Jeff Garzik167fb282006-12-15 10:41:15 -05003436 adapter->link_duplex == FULL_DUPLEX) {
Ajit Khaparde5fe31de2009-10-07 02:42:23 +00003437 netdev->stats.tx_carrier_errors = 0;
Jeff Garzik167fb282006-12-15 10:41:15 -05003438 adapter->stats.tncrs = 0;
3439 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440
3441 /* Tx Dropped needs to be maintained elsewhere */
3442
3443 /* Phy Stats */
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003444 if (hw->media_type == e1000_media_type_copper) {
3445 if ((adapter->link_speed == SPEED_1000) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446 (!e1000_read_phy_reg(hw, PHY_1000T_STATUS, &phy_tmp))) {
3447 phy_tmp &= PHY_IDLE_ERROR_COUNT_MASK;
3448 adapter->phy_stats.idle_errors += phy_tmp;
3449 }
3450
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003451 if ((hw->mac_type <= e1000_82546) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 (hw->phy_type == e1000_phy_m88) &&
3453 !e1000_read_phy_reg(hw, M88E1000_RX_ERR_CNTR, &phy_tmp))
3454 adapter->phy_stats.receive_errors += phy_tmp;
3455 }
3456
Jeff Garzik15e376b2006-12-15 11:16:33 -05003457 /* Management Stats */
Joe Perches1dc32912008-07-11 15:17:08 -07003458 if (hw->has_smbus) {
3459 adapter->stats.mgptc += er32(MGTPTC);
3460 adapter->stats.mgprc += er32(MGTPRC);
3461 adapter->stats.mgpdc += er32(MGTPDC);
Jeff Garzik15e376b2006-12-15 11:16:33 -05003462 }
3463
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 spin_unlock_irqrestore(&adapter->stats_lock, flags);
3465}
Jesse Brandeburg9ac98282006-11-01 08:48:10 -08003466
3467/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 * e1000_intr - Interrupt Handler
3469 * @irq: interrupt number
3470 * @data: pointer to a network interface device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471 **/
3472
Joe Perches64798842008-07-11 15:17:02 -07003473static irqreturn_t e1000_intr(int irq, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474{
3475 struct net_device *netdev = data;
Malli Chilakala60490fe2005-06-17 17:41:45 -07003476 struct e1000_adapter *adapter = netdev_priv(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477 struct e1000_hw *hw = &adapter->hw;
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00003478 u32 icr = er32(ICR);
Francois Romieuc3570ac2008-07-11 15:17:38 -07003479
Jesse Brandeburg4c11b8a2011-01-13 07:48:13 +00003480 if (unlikely((!icr)))
Jesse Brandeburg835bb122006-11-01 08:48:13 -08003481 return IRQ_NONE; /* Not our interrupt */
3482
Jesse Brandeburg4c11b8a2011-01-13 07:48:13 +00003483 /*
3484 * we might have caused the interrupt, but the above
3485 * read cleared it, and just in case the driver is
3486 * down there is nothing to do so return handled
3487 */
3488 if (unlikely(test_bit(__E1000_DOWN, &adapter->flags)))
3489 return IRQ_HANDLED;
3490
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003491 if (unlikely(icr & (E1000_ICR_RXSEQ | E1000_ICR_LSC))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492 hw->get_link_status = 1;
Auke Kok1314bbf2006-09-27 12:54:02 -07003493 /* guard against interrupt when we're going down */
3494 if (!test_bit(__E1000_DOWN, &adapter->flags))
3495 mod_timer(&adapter->watchdog_timer, jiffies + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496 }
3497
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00003498 /* disable interrupts, without the synchronize_irq bit */
3499 ew32(IMC, ~0);
3500 E1000_WRITE_FLUSH();
3501
Ben Hutchings288379f2009-01-19 16:43:59 -08003502 if (likely(napi_schedule_prep(&adapter->napi))) {
Jesse Brandeburg835bb122006-11-01 08:48:13 -08003503 adapter->total_tx_bytes = 0;
3504 adapter->total_tx_packets = 0;
3505 adapter->total_rx_bytes = 0;
3506 adapter->total_rx_packets = 0;
Ben Hutchings288379f2009-01-19 16:43:59 -08003507 __napi_schedule(&adapter->napi);
Jesse Brandeburga6c42322009-03-25 21:59:22 +00003508 } else {
Auke Kok90fb5132006-11-01 08:47:30 -08003509 /* this really should not happen! if it does it is basically a
3510 * bug, but not a hard error, so enable ints and continue */
Jesse Brandeburga6c42322009-03-25 21:59:22 +00003511 if (!test_bit(__E1000_DOWN, &adapter->flags))
3512 e1000_irq_enable(adapter);
3513 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515 return IRQ_HANDLED;
3516}
3517
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518/**
3519 * e1000_clean - NAPI Rx polling callback
3520 * @adapter: board private structure
3521 **/
Joe Perches64798842008-07-11 15:17:02 -07003522static int e1000_clean(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003524 struct e1000_adapter *adapter = container_of(napi, struct e1000_adapter, napi);
Jesse Brandeburg650b5a52009-09-25 12:19:23 +00003525 int tx_clean_complete = 0, work_done = 0;
Malli Chilakala26483452005-04-28 19:44:46 -07003526
Jesse Brandeburg650b5a52009-09-25 12:19:23 +00003527 tx_clean_complete = e1000_clean_tx_irq(adapter, &adapter->tx_ring[0]);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003528
Jesse Brandeburg650b5a52009-09-25 12:19:23 +00003529 adapter->clean_rx(adapter, &adapter->rx_ring[0], &work_done, budget);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003530
Jesse Brandeburg650b5a52009-09-25 12:19:23 +00003531 if (!tx_clean_complete)
David S. Millerd2c7ddd2008-01-15 22:43:24 -08003532 work_done = budget;
3533
David S. Miller53e52c72008-01-07 21:06:12 -08003534 /* If budget not fully consumed, exit the polling mode */
3535 if (work_done < budget) {
Jesse Brandeburg835bb122006-11-01 08:48:13 -08003536 if (likely(adapter->itr_setting & 3))
3537 e1000_set_itr(adapter);
Ben Hutchings288379f2009-01-19 16:43:59 -08003538 napi_complete(napi);
Jesse Brandeburga6c42322009-03-25 21:59:22 +00003539 if (!test_bit(__E1000_DOWN, &adapter->flags))
3540 e1000_irq_enable(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541 }
3542
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003543 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544}
3545
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546/**
3547 * e1000_clean_tx_irq - Reclaim resources after transmit completes
3548 * @adapter: board private structure
3549 **/
Joe Perches64798842008-07-11 15:17:02 -07003550static bool e1000_clean_tx_irq(struct e1000_adapter *adapter,
3551 struct e1000_tx_ring *tx_ring)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552{
Joe Perches1dc32912008-07-11 15:17:08 -07003553 struct e1000_hw *hw = &adapter->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554 struct net_device *netdev = adapter->netdev;
3555 struct e1000_tx_desc *tx_desc, *eop_desc;
3556 struct e1000_buffer *buffer_info;
3557 unsigned int i, eop;
Jeff Kirsher2a1af5d2006-03-02 18:20:43 -08003558 unsigned int count = 0;
Jesse Brandeburg835bb122006-11-01 08:48:13 -08003559 unsigned int total_tx_bytes=0, total_tx_packets=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560
3561 i = tx_ring->next_to_clean;
3562 eop = tx_ring->buffer_info[i].next_to_watch;
3563 eop_desc = E1000_TX_DESC(*tx_ring, eop);
3564
Alexander Duyckccfb3422009-03-25 21:59:04 +00003565 while ((eop_desc->upper.data & cpu_to_le32(E1000_TXD_STAT_DD)) &&
3566 (count < tx_ring->count)) {
Jesse Brandeburg843f4262009-04-16 16:59:47 +00003567 bool cleaned = false;
Jeff Kirsher2d0bb1c2010-08-08 16:02:31 +00003568 rmb(); /* read buffer_info after eop_desc */
Jesse Brandeburg843f4262009-04-16 16:59:47 +00003569 for ( ; !cleaned; count++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 tx_desc = E1000_TX_DESC(*tx_ring, i);
3571 buffer_info = &tx_ring->buffer_info[i];
3572 cleaned = (i == eop);
3573
Jesse Brandeburg835bb122006-11-01 08:48:13 -08003574 if (cleaned) {
Jesse Brandeburg2b653262006-12-15 10:30:44 +01003575 struct sk_buff *skb = buffer_info->skb;
Jesse Brandeburg7753b172007-01-18 09:25:31 -08003576 unsigned int segs, bytecount;
3577 segs = skb_shinfo(skb)->gso_segs ?: 1;
3578 /* multiply data chunks by size of headers */
3579 bytecount = ((segs - 1) * skb_headlen(skb)) +
3580 skb->len;
Jesse Brandeburg2b653262006-12-15 10:30:44 +01003581 total_tx_packets += segs;
Jesse Brandeburg7753b172007-01-18 09:25:31 -08003582 total_tx_bytes += bytecount;
Jesse Brandeburg835bb122006-11-01 08:48:13 -08003583 }
Jeff Kirsherfd803242005-12-13 00:06:22 -05003584 e1000_unmap_and_free_tx_resource(adapter, buffer_info);
Jesse Brandeburga9ebadd2006-11-01 08:47:53 -08003585 tx_desc->upper.data = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003587 if (unlikely(++i == tx_ring->count)) i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588 }
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003589
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590 eop = tx_ring->buffer_info[i].next_to_watch;
3591 eop_desc = E1000_TX_DESC(*tx_ring, eop);
3592 }
3593
3594 tx_ring->next_to_clean = i;
3595
Auke Kok77b2aad2006-04-14 19:05:25 -07003596#define TX_WAKE_THRESHOLD 32
Jesse Brandeburg843f4262009-04-16 16:59:47 +00003597 if (unlikely(count && netif_carrier_ok(netdev) &&
Jesse Brandeburg65c79732006-09-27 12:53:48 -07003598 E1000_DESC_UNUSED(tx_ring) >= TX_WAKE_THRESHOLD)) {
3599 /* Make sure that anybody stopping the queue after this
3600 * sees the new next_to_clean.
3601 */
3602 smp_mb();
Jesse Brandeburgcdd75492009-09-25 12:18:07 +00003603
3604 if (netif_queue_stopped(netdev) &&
3605 !(test_bit(__E1000_DOWN, &adapter->flags))) {
Auke Kok77b2aad2006-04-14 19:05:25 -07003606 netif_wake_queue(netdev);
Jesse Brandeburgfcfb1222006-11-01 08:47:59 -08003607 ++adapter->restart_queue;
3608 }
Auke Kok77b2aad2006-04-14 19:05:25 -07003609 }
Malli Chilakala26483452005-04-28 19:44:46 -07003610
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003611 if (adapter->detect_tx_hung) {
Malli Chilakala26483452005-04-28 19:44:46 -07003612 /* Detect a transmit hang in hardware, this serializes the
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613 * check with the clearing of time_stamp and movement of i */
Joe Perchesc3033b02008-03-21 11:06:25 -07003614 adapter->detect_tx_hung = false;
Jesse Brandeburgcdd75492009-09-25 12:18:07 +00003615 if (tx_ring->buffer_info[eop].time_stamp &&
3616 time_after(jiffies, tx_ring->buffer_info[eop].time_stamp +
Joe Perches8e95a202009-12-03 07:58:21 +00003617 (adapter->tx_timeout_factor * HZ)) &&
3618 !(er32(STATUS) & E1000_STATUS_TXOFF)) {
Malli Chilakala70b8f1e2005-04-28 19:40:40 -07003619
3620 /* detected Tx unit hang */
Emil Tantilovfeb8f472010-07-26 23:37:21 -07003621 e_err(drv, "Detected Tx Unit Hang\n"
Emil Tantilov675ad472010-04-27 14:02:58 +00003622 " Tx Queue <%lu>\n"
3623 " TDH <%x>\n"
3624 " TDT <%x>\n"
3625 " next_to_use <%x>\n"
3626 " next_to_clean <%x>\n"
3627 "buffer_info[next_to_clean]\n"
3628 " time_stamp <%lx>\n"
3629 " next_to_watch <%x>\n"
3630 " jiffies <%lx>\n"
3631 " next_to_watch.status <%x>\n",
Jeff Kirsher7bfa4812006-01-12 16:50:41 -08003632 (unsigned long)((tx_ring - adapter->tx_ring) /
3633 sizeof(struct e1000_tx_ring)),
Joe Perches1dc32912008-07-11 15:17:08 -07003634 readl(hw->hw_addr + tx_ring->tdh),
3635 readl(hw->hw_addr + tx_ring->tdt),
Malli Chilakala70b8f1e2005-04-28 19:40:40 -07003636 tx_ring->next_to_use,
Jeff Kirsher392137f2006-01-12 16:50:57 -08003637 tx_ring->next_to_clean,
Jesse Brandeburgcdd75492009-09-25 12:18:07 +00003638 tx_ring->buffer_info[eop].time_stamp,
Malli Chilakala70b8f1e2005-04-28 19:40:40 -07003639 eop,
3640 jiffies,
3641 eop_desc->upper.fields.status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642 netif_stop_queue(netdev);
Malli Chilakala70b8f1e2005-04-28 19:40:40 -07003643 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644 }
Jesse Brandeburg835bb122006-11-01 08:48:13 -08003645 adapter->total_tx_bytes += total_tx_bytes;
3646 adapter->total_tx_packets += total_tx_packets;
Ajit Khaparde5fe31de2009-10-07 02:42:23 +00003647 netdev->stats.tx_bytes += total_tx_bytes;
3648 netdev->stats.tx_packets += total_tx_packets;
Eric Dumazet807540b2010-09-23 05:40:09 +00003649 return count < tx_ring->count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650}
3651
3652/**
3653 * e1000_rx_checksum - Receive Checksum Offload for 82543
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003654 * @adapter: board private structure
3655 * @status_err: receive descriptor status and error fields
3656 * @csum: receive descriptor csum field
3657 * @sk_buff: socket buffer with received data
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658 **/
3659
Joe Perches64798842008-07-11 15:17:02 -07003660static void e1000_rx_checksum(struct e1000_adapter *adapter, u32 status_err,
3661 u32 csum, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662{
Joe Perches1dc32912008-07-11 15:17:08 -07003663 struct e1000_hw *hw = &adapter->hw;
Joe Perches406874a2008-04-03 10:06:32 -07003664 u16 status = (u16)status_err;
3665 u8 errors = (u8)(status_err >> 24);
Eric Dumazetbc8acf22010-09-02 13:07:41 -07003666
3667 skb_checksum_none_assert(skb);
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003668
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669 /* 82543 or newer only */
Joe Perches1dc32912008-07-11 15:17:08 -07003670 if (unlikely(hw->mac_type < e1000_82543)) return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671 /* Ignore Checksum bit is set */
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003672 if (unlikely(status & E1000_RXD_STAT_IXSM)) return;
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003673 /* TCP/UDP checksum error bit is set */
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003674 if (unlikely(errors & E1000_RXD_ERR_TCPE)) {
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003675 /* let the stack verify checksum errors */
3676 adapter->hw_csum_err++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677 return;
3678 }
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003679 /* TCP/UDP Checksum has not been calculated */
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00003680 if (!(status & E1000_RXD_STAT_TCPCS))
3681 return;
3682
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003683 /* It must be a TCP or UDP packet with a valid checksum */
3684 if (likely(status & E1000_RXD_STAT_TCPCS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685 /* TCP checksum is good */
3686 skb->ip_summed = CHECKSUM_UNNECESSARY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687 }
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003688 adapter->hw_csum_good++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689}
3690
3691/**
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00003692 * e1000_consume_page - helper function
3693 **/
3694static void e1000_consume_page(struct e1000_buffer *bi, struct sk_buff *skb,
3695 u16 length)
3696{
3697 bi->page = NULL;
3698 skb->len += length;
3699 skb->data_len += length;
3700 skb->truesize += length;
3701}
3702
3703/**
3704 * e1000_receive_skb - helper function to handle rx indications
3705 * @adapter: board private structure
3706 * @status: descriptor status field as written by hardware
3707 * @vlan: descriptor vlan field as written by hardware (no le/be conversion)
3708 * @skb: pointer to sk_buff to be indicated to stack
3709 */
3710static void e1000_receive_skb(struct e1000_adapter *adapter, u8 status,
3711 __le16 vlan, struct sk_buff *skb)
3712{
Jesse Brandeburg6a08d192010-09-22 18:23:05 +00003713 skb->protocol = eth_type_trans(skb, adapter->netdev);
3714
3715 if ((unlikely(adapter->vlgrp && (status & E1000_RXD_STAT_VP))))
3716 vlan_gro_receive(&adapter->napi, adapter->vlgrp,
3717 le16_to_cpu(vlan) & E1000_RXD_SPC_VLAN_MASK,
3718 skb);
3719 else
3720 napi_gro_receive(&adapter->napi, skb);
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00003721}
3722
3723/**
3724 * e1000_clean_jumbo_rx_irq - Send received data up the network stack; legacy
3725 * @adapter: board private structure
3726 * @rx_ring: ring to clean
3727 * @work_done: amount of napi work completed this call
3728 * @work_to_do: max amount of work allowed for this call to do
3729 *
3730 * the return value indicates whether actual cleaning was done, there
3731 * is no guarantee that everything was cleaned
3732 */
3733static bool e1000_clean_jumbo_rx_irq(struct e1000_adapter *adapter,
3734 struct e1000_rx_ring *rx_ring,
3735 int *work_done, int work_to_do)
3736{
3737 struct e1000_hw *hw = &adapter->hw;
3738 struct net_device *netdev = adapter->netdev;
3739 struct pci_dev *pdev = adapter->pdev;
3740 struct e1000_rx_desc *rx_desc, *next_rxd;
3741 struct e1000_buffer *buffer_info, *next_buffer;
3742 unsigned long irq_flags;
3743 u32 length;
3744 unsigned int i;
3745 int cleaned_count = 0;
3746 bool cleaned = false;
3747 unsigned int total_rx_bytes=0, total_rx_packets=0;
3748
3749 i = rx_ring->next_to_clean;
3750 rx_desc = E1000_RX_DESC(*rx_ring, i);
3751 buffer_info = &rx_ring->buffer_info[i];
3752
3753 while (rx_desc->status & E1000_RXD_STAT_DD) {
3754 struct sk_buff *skb;
3755 u8 status;
3756
3757 if (*work_done >= work_to_do)
3758 break;
3759 (*work_done)++;
Jeff Kirsher2d0bb1c2010-08-08 16:02:31 +00003760 rmb(); /* read descriptor and rx_buffer_info after status DD */
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00003761
3762 status = rx_desc->status;
3763 skb = buffer_info->skb;
3764 buffer_info->skb = NULL;
3765
3766 if (++i == rx_ring->count) i = 0;
3767 next_rxd = E1000_RX_DESC(*rx_ring, i);
3768 prefetch(next_rxd);
3769
3770 next_buffer = &rx_ring->buffer_info[i];
3771
3772 cleaned = true;
3773 cleaned_count++;
Nick Nunleyb16f53b2010-04-27 13:08:45 +00003774 dma_unmap_page(&pdev->dev, buffer_info->dma,
3775 buffer_info->length, DMA_FROM_DEVICE);
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00003776 buffer_info->dma = 0;
3777
3778 length = le16_to_cpu(rx_desc->length);
3779
3780 /* errors is only valid for DD + EOP descriptors */
3781 if (unlikely((status & E1000_RXD_STAT_EOP) &&
3782 (rx_desc->errors & E1000_RXD_ERR_FRAME_ERR_MASK))) {
3783 u8 last_byte = *(skb->data + length - 1);
3784 if (TBI_ACCEPT(hw, status, rx_desc->errors, length,
3785 last_byte)) {
3786 spin_lock_irqsave(&adapter->stats_lock,
3787 irq_flags);
3788 e1000_tbi_adjust_stats(hw, &adapter->stats,
3789 length, skb->data);
3790 spin_unlock_irqrestore(&adapter->stats_lock,
3791 irq_flags);
3792 length--;
3793 } else {
3794 /* recycle both page and skb */
3795 buffer_info->skb = skb;
3796 /* an error means any chain goes out the window
3797 * too */
3798 if (rx_ring->rx_skb_top)
3799 dev_kfree_skb(rx_ring->rx_skb_top);
3800 rx_ring->rx_skb_top = NULL;
3801 goto next_desc;
3802 }
3803 }
3804
3805#define rxtop rx_ring->rx_skb_top
3806 if (!(status & E1000_RXD_STAT_EOP)) {
3807 /* this descriptor is only the beginning (or middle) */
3808 if (!rxtop) {
3809 /* this is the beginning of a chain */
3810 rxtop = skb;
3811 skb_fill_page_desc(rxtop, 0, buffer_info->page,
3812 0, length);
3813 } else {
3814 /* this is the middle of a chain */
3815 skb_fill_page_desc(rxtop,
3816 skb_shinfo(rxtop)->nr_frags,
3817 buffer_info->page, 0, length);
3818 /* re-use the skb, only consumed the page */
3819 buffer_info->skb = skb;
3820 }
3821 e1000_consume_page(buffer_info, rxtop, length);
3822 goto next_desc;
3823 } else {
3824 if (rxtop) {
3825 /* end of the chain */
3826 skb_fill_page_desc(rxtop,
3827 skb_shinfo(rxtop)->nr_frags,
3828 buffer_info->page, 0, length);
3829 /* re-use the current skb, we only consumed the
3830 * page */
3831 buffer_info->skb = skb;
3832 skb = rxtop;
3833 rxtop = NULL;
3834 e1000_consume_page(buffer_info, skb, length);
3835 } else {
3836 /* no chain, got EOP, this buf is the packet
3837 * copybreak to save the put_page/alloc_page */
3838 if (length <= copybreak &&
3839 skb_tailroom(skb) >= length) {
3840 u8 *vaddr;
3841 vaddr = kmap_atomic(buffer_info->page,
3842 KM_SKB_DATA_SOFTIRQ);
3843 memcpy(skb_tail_pointer(skb), vaddr, length);
3844 kunmap_atomic(vaddr,
3845 KM_SKB_DATA_SOFTIRQ);
3846 /* re-use the page, so don't erase
3847 * buffer_info->page */
3848 skb_put(skb, length);
3849 } else {
3850 skb_fill_page_desc(skb, 0,
3851 buffer_info->page, 0,
3852 length);
3853 e1000_consume_page(buffer_info, skb,
3854 length);
3855 }
3856 }
3857 }
3858
3859 /* Receive Checksum Offload XXX recompute due to CRC strip? */
3860 e1000_rx_checksum(adapter,
3861 (u32)(status) |
3862 ((u32)(rx_desc->errors) << 24),
3863 le16_to_cpu(rx_desc->csum), skb);
3864
3865 pskb_trim(skb, skb->len - 4);
3866
3867 /* probably a little skewed due to removing CRC */
3868 total_rx_bytes += skb->len;
3869 total_rx_packets++;
3870
3871 /* eth type trans needs skb->data to point to something */
3872 if (!pskb_may_pull(skb, ETH_HLEN)) {
Emil Tantilovfeb8f472010-07-26 23:37:21 -07003873 e_err(drv, "pskb_may_pull failed.\n");
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00003874 dev_kfree_skb(skb);
3875 goto next_desc;
3876 }
3877
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00003878 e1000_receive_skb(adapter, status, rx_desc->special, skb);
3879
3880next_desc:
3881 rx_desc->status = 0;
3882
3883 /* return some buffers to hardware, one at a time is too slow */
3884 if (unlikely(cleaned_count >= E1000_RX_BUFFER_WRITE)) {
3885 adapter->alloc_rx_buf(adapter, rx_ring, cleaned_count);
3886 cleaned_count = 0;
3887 }
3888
3889 /* use prefetched values */
3890 rx_desc = next_rxd;
3891 buffer_info = next_buffer;
3892 }
3893 rx_ring->next_to_clean = i;
3894
3895 cleaned_count = E1000_DESC_UNUSED(rx_ring);
3896 if (cleaned_count)
3897 adapter->alloc_rx_buf(adapter, rx_ring, cleaned_count);
3898
3899 adapter->total_rx_packets += total_rx_packets;
3900 adapter->total_rx_bytes += total_rx_bytes;
Ajit Khaparde5fe31de2009-10-07 02:42:23 +00003901 netdev->stats.rx_bytes += total_rx_bytes;
3902 netdev->stats.rx_packets += total_rx_packets;
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00003903 return cleaned;
3904}
3905
Joe Perches57bf6ee2010-05-13 15:26:17 +00003906/*
3907 * this should improve performance for small packets with large amounts
3908 * of reassembly being done in the stack
3909 */
3910static void e1000_check_copybreak(struct net_device *netdev,
3911 struct e1000_buffer *buffer_info,
3912 u32 length, struct sk_buff **skb)
3913{
3914 struct sk_buff *new_skb;
3915
3916 if (length > copybreak)
3917 return;
3918
3919 new_skb = netdev_alloc_skb_ip_align(netdev, length);
3920 if (!new_skb)
3921 return;
3922
3923 skb_copy_to_linear_data_offset(new_skb, -NET_IP_ALIGN,
3924 (*skb)->data - NET_IP_ALIGN,
3925 length + NET_IP_ALIGN);
3926 /* save the skb in buffer_info as good */
3927 buffer_info->skb = *skb;
3928 *skb = new_skb;
3929}
3930
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00003931/**
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003932 * e1000_clean_rx_irq - Send received data up the network stack; legacy
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933 * @adapter: board private structure
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00003934 * @rx_ring: ring to clean
3935 * @work_done: amount of napi work completed this call
3936 * @work_to_do: max amount of work allowed for this call to do
3937 */
Joe Perches64798842008-07-11 15:17:02 -07003938static bool e1000_clean_rx_irq(struct e1000_adapter *adapter,
3939 struct e1000_rx_ring *rx_ring,
3940 int *work_done, int work_to_do)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941{
Joe Perches1dc32912008-07-11 15:17:08 -07003942 struct e1000_hw *hw = &adapter->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943 struct net_device *netdev = adapter->netdev;
3944 struct pci_dev *pdev = adapter->pdev;
Jesse Brandeburg86c3d592006-01-18 13:01:43 -08003945 struct e1000_rx_desc *rx_desc, *next_rxd;
3946 struct e1000_buffer *buffer_info, *next_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947 unsigned long flags;
Joe Perches406874a2008-04-03 10:06:32 -07003948 u32 length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949 unsigned int i;
Jeff Kirsher72d64a42006-01-12 16:51:19 -08003950 int cleaned_count = 0;
Joe Perchesc3033b02008-03-21 11:06:25 -07003951 bool cleaned = false;
Jesse Brandeburg835bb122006-11-01 08:48:13 -08003952 unsigned int total_rx_bytes=0, total_rx_packets=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953
3954 i = rx_ring->next_to_clean;
3955 rx_desc = E1000_RX_DESC(*rx_ring, i);
Jesse Brandeburgb92ff8e2006-01-18 13:01:32 -08003956 buffer_info = &rx_ring->buffer_info[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957
Jesse Brandeburgb92ff8e2006-01-18 13:01:32 -08003958 while (rx_desc->status & E1000_RXD_STAT_DD) {
Auke Kok24f476e2006-06-08 09:28:47 -07003959 struct sk_buff *skb;
Jeff Kirshera292ca62006-01-12 16:51:30 -08003960 u8 status;
Auke Kok90fb5132006-11-01 08:47:30 -08003961
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003962 if (*work_done >= work_to_do)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963 break;
3964 (*work_done)++;
Jeff Kirsher2d0bb1c2010-08-08 16:02:31 +00003965 rmb(); /* read descriptor and rx_buffer_info after status DD */
Francois Romieuc3570ac2008-07-11 15:17:38 -07003966
Jeff Kirshera292ca62006-01-12 16:51:30 -08003967 status = rx_desc->status;
Jesse Brandeburgb92ff8e2006-01-18 13:01:32 -08003968 skb = buffer_info->skb;
Jesse Brandeburg86c3d592006-01-18 13:01:43 -08003969 buffer_info->skb = NULL;
3970
Jeff Kirsher30320be2006-03-02 18:21:57 -08003971 prefetch(skb->data - NET_IP_ALIGN);
3972
Jesse Brandeburg86c3d592006-01-18 13:01:43 -08003973 if (++i == rx_ring->count) i = 0;
3974 next_rxd = E1000_RX_DESC(*rx_ring, i);
Jeff Kirsher30320be2006-03-02 18:21:57 -08003975 prefetch(next_rxd);
3976
Jesse Brandeburg86c3d592006-01-18 13:01:43 -08003977 next_buffer = &rx_ring->buffer_info[i];
Jesse Brandeburg86c3d592006-01-18 13:01:43 -08003978
Joe Perchesc3033b02008-03-21 11:06:25 -07003979 cleaned = true;
Jeff Kirsher72d64a42006-01-12 16:51:19 -08003980 cleaned_count++;
Nick Nunleyb16f53b2010-04-27 13:08:45 +00003981 dma_unmap_single(&pdev->dev, buffer_info->dma,
3982 buffer_info->length, DMA_FROM_DEVICE);
Jesse Brandeburg679be3b2009-06-30 12:45:34 +00003983 buffer_info->dma = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985 length = le16_to_cpu(rx_desc->length);
Neil Hormanea30e112009-06-02 01:29:58 -07003986 /* !EOP means multiple descriptors were used to store a single
Jesse Brandeburg40a14de2010-01-19 14:15:38 +00003987 * packet, if thats the case we need to toss it. In fact, we
3988 * to toss every packet with the EOP bit clear and the next
3989 * frame that _does_ have the EOP bit set, as it is by
3990 * definition only a frame fragment
3991 */
3992 if (unlikely(!(status & E1000_RXD_STAT_EOP)))
3993 adapter->discarding = true;
3994
3995 if (adapter->discarding) {
Jeff Kirshera1415ee2006-02-28 20:24:07 -08003996 /* All receives must fit into a single buffer */
Emil Tantilovfeb8f472010-07-26 23:37:21 -07003997 e_dbg("Receive packet consumed multiple buffers\n");
Auke Kok864c4e42006-06-27 09:06:53 -07003998 /* recycle */
Auke Kok8fc897b2006-08-28 14:56:16 -07003999 buffer_info->skb = skb;
Jesse Brandeburg40a14de2010-01-19 14:15:38 +00004000 if (status & E1000_RXD_STAT_EOP)
4001 adapter->discarding = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002 goto next_desc;
4003 }
4004
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004005 if (unlikely(rx_desc->errors & E1000_RXD_ERR_FRAME_ERR_MASK)) {
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004006 u8 last_byte = *(skb->data + length - 1);
Joe Perches1dc32912008-07-11 15:17:08 -07004007 if (TBI_ACCEPT(hw, status, rx_desc->errors, length,
4008 last_byte)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009 spin_lock_irqsave(&adapter->stats_lock, flags);
Joe Perches1dc32912008-07-11 15:17:08 -07004010 e1000_tbi_adjust_stats(hw, &adapter->stats,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011 length, skb->data);
4012 spin_unlock_irqrestore(&adapter->stats_lock,
4013 flags);
4014 length--;
4015 } else {
Auke Kok9e2feac2006-04-14 19:05:18 -07004016 /* recycle */
4017 buffer_info->skb = skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018 goto next_desc;
4019 }
Auke Kok1cb58212006-04-18 12:31:04 -07004020 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021
Jesse Brandeburgd2a1e212006-10-24 14:46:06 -07004022 /* adjust length to remove Ethernet CRC, this must be
4023 * done after the TBI_ACCEPT workaround above */
4024 length -= 4;
4025
Jesse Brandeburg835bb122006-11-01 08:48:13 -08004026 /* probably a little skewed due to removing CRC */
4027 total_rx_bytes += length;
4028 total_rx_packets++;
4029
Joe Perches57bf6ee2010-05-13 15:26:17 +00004030 e1000_check_copybreak(netdev, buffer_info, length, &skb);
4031
Auke Kok996695d2006-11-01 08:47:50 -08004032 skb_put(skb, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033
4034 /* Receive Checksum Offload */
Jeff Kirshera292ca62006-01-12 16:51:30 -08004035 e1000_rx_checksum(adapter,
Joe Perches406874a2008-04-03 10:06:32 -07004036 (u32)(status) |
4037 ((u32)(rx_desc->errors) << 24),
David S. Millerc3d7a3a2006-03-15 14:26:28 -08004038 le16_to_cpu(rx_desc->csum), skb);
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004039
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004040 e1000_receive_skb(adapter, status, rx_desc->special, skb);
Francois Romieuc3570ac2008-07-11 15:17:38 -07004041
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042next_desc:
4043 rx_desc->status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044
Jeff Kirsher72d64a42006-01-12 16:51:19 -08004045 /* return some buffers to hardware, one at a time is too slow */
4046 if (unlikely(cleaned_count >= E1000_RX_BUFFER_WRITE)) {
4047 adapter->alloc_rx_buf(adapter, rx_ring, cleaned_count);
4048 cleaned_count = 0;
4049 }
4050
Jeff Kirsher30320be2006-03-02 18:21:57 -08004051 /* use prefetched values */
Jesse Brandeburg86c3d592006-01-18 13:01:43 -08004052 rx_desc = next_rxd;
4053 buffer_info = next_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055 rx_ring->next_to_clean = i;
Jeff Kirsher72d64a42006-01-12 16:51:19 -08004056
4057 cleaned_count = E1000_DESC_UNUSED(rx_ring);
4058 if (cleaned_count)
4059 adapter->alloc_rx_buf(adapter, rx_ring, cleaned_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060
Jesse Brandeburg835bb122006-11-01 08:48:13 -08004061 adapter->total_rx_packets += total_rx_packets;
4062 adapter->total_rx_bytes += total_rx_bytes;
Ajit Khaparde5fe31de2009-10-07 02:42:23 +00004063 netdev->stats.rx_bytes += total_rx_bytes;
4064 netdev->stats.rx_packets += total_rx_packets;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065 return cleaned;
4066}
4067
4068/**
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004069 * e1000_alloc_jumbo_rx_buffers - Replace used jumbo receive buffers
4070 * @adapter: address of board private structure
4071 * @rx_ring: pointer to receive ring structure
4072 * @cleaned_count: number of buffers to allocate this pass
4073 **/
4074
4075static void
4076e1000_alloc_jumbo_rx_buffers(struct e1000_adapter *adapter,
4077 struct e1000_rx_ring *rx_ring, int cleaned_count)
4078{
4079 struct net_device *netdev = adapter->netdev;
4080 struct pci_dev *pdev = adapter->pdev;
4081 struct e1000_rx_desc *rx_desc;
4082 struct e1000_buffer *buffer_info;
4083 struct sk_buff *skb;
4084 unsigned int i;
Eric Dumazet89d71a62009-10-13 05:34:20 +00004085 unsigned int bufsz = 256 - 16 /*for skb_reserve */ ;
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004086
4087 i = rx_ring->next_to_use;
4088 buffer_info = &rx_ring->buffer_info[i];
4089
4090 while (cleaned_count--) {
4091 skb = buffer_info->skb;
4092 if (skb) {
4093 skb_trim(skb, 0);
4094 goto check_page;
4095 }
4096
Eric Dumazet89d71a62009-10-13 05:34:20 +00004097 skb = netdev_alloc_skb_ip_align(netdev, bufsz);
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004098 if (unlikely(!skb)) {
4099 /* Better luck next round */
4100 adapter->alloc_rx_buff_failed++;
4101 break;
4102 }
4103
4104 /* Fix for errata 23, can't cross 64kB boundary */
4105 if (!e1000_check_64k_bound(adapter, skb->data, bufsz)) {
4106 struct sk_buff *oldskb = skb;
Emil Tantilovfeb8f472010-07-26 23:37:21 -07004107 e_err(rx_err, "skb align check failed: %u bytes at "
4108 "%p\n", bufsz, skb->data);
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004109 /* Try again, without freeing the previous */
Eric Dumazet89d71a62009-10-13 05:34:20 +00004110 skb = netdev_alloc_skb_ip_align(netdev, bufsz);
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004111 /* Failed allocation, critical failure */
4112 if (!skb) {
4113 dev_kfree_skb(oldskb);
4114 adapter->alloc_rx_buff_failed++;
4115 break;
4116 }
4117
4118 if (!e1000_check_64k_bound(adapter, skb->data, bufsz)) {
4119 /* give up */
4120 dev_kfree_skb(skb);
4121 dev_kfree_skb(oldskb);
4122 break; /* while (cleaned_count--) */
4123 }
4124
4125 /* Use new allocation */
4126 dev_kfree_skb(oldskb);
4127 }
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004128 buffer_info->skb = skb;
4129 buffer_info->length = adapter->rx_buffer_len;
4130check_page:
4131 /* allocate a new page if necessary */
4132 if (!buffer_info->page) {
4133 buffer_info->page = alloc_page(GFP_ATOMIC);
4134 if (unlikely(!buffer_info->page)) {
4135 adapter->alloc_rx_buff_failed++;
4136 break;
4137 }
4138 }
4139
Anton Blanchardb5abb022010-02-19 17:54:53 +00004140 if (!buffer_info->dma) {
Nick Nunleyb16f53b2010-04-27 13:08:45 +00004141 buffer_info->dma = dma_map_page(&pdev->dev,
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004142 buffer_info->page, 0,
Nick Nunleyb16f53b2010-04-27 13:08:45 +00004143 buffer_info->length,
4144 DMA_FROM_DEVICE);
4145 if (dma_mapping_error(&pdev->dev, buffer_info->dma)) {
Anton Blanchardb5abb022010-02-19 17:54:53 +00004146 put_page(buffer_info->page);
4147 dev_kfree_skb(skb);
4148 buffer_info->page = NULL;
4149 buffer_info->skb = NULL;
4150 buffer_info->dma = 0;
4151 adapter->alloc_rx_buff_failed++;
4152 break; /* while !buffer_info->skb */
4153 }
4154 }
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004155
4156 rx_desc = E1000_RX_DESC(*rx_ring, i);
4157 rx_desc->buffer_addr = cpu_to_le64(buffer_info->dma);
4158
4159 if (unlikely(++i == rx_ring->count))
4160 i = 0;
4161 buffer_info = &rx_ring->buffer_info[i];
4162 }
4163
4164 if (likely(rx_ring->next_to_use != i)) {
4165 rx_ring->next_to_use = i;
4166 if (unlikely(i-- == 0))
4167 i = (rx_ring->count - 1);
4168
4169 /* Force memory writes to complete before letting h/w
4170 * know there are new descriptors to fetch. (Only
4171 * applicable for weak-ordered memory model archs,
4172 * such as IA-64). */
4173 wmb();
4174 writel(i, adapter->hw.hw_addr + rx_ring->rdt);
4175 }
4176}
4177
4178/**
Malli Chilakala2d7edb92005-04-28 19:43:52 -07004179 * e1000_alloc_rx_buffers - Replace used receive buffers; legacy & extended
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180 * @adapter: address of board private structure
4181 **/
4182
Joe Perches64798842008-07-11 15:17:02 -07004183static void e1000_alloc_rx_buffers(struct e1000_adapter *adapter,
4184 struct e1000_rx_ring *rx_ring,
4185 int cleaned_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186{
Joe Perches1dc32912008-07-11 15:17:08 -07004187 struct e1000_hw *hw = &adapter->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188 struct net_device *netdev = adapter->netdev;
4189 struct pci_dev *pdev = adapter->pdev;
4190 struct e1000_rx_desc *rx_desc;
4191 struct e1000_buffer *buffer_info;
4192 struct sk_buff *skb;
Malli Chilakala26483452005-04-28 19:44:46 -07004193 unsigned int i;
Eric Dumazet89d71a62009-10-13 05:34:20 +00004194 unsigned int bufsz = adapter->rx_buffer_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195
4196 i = rx_ring->next_to_use;
4197 buffer_info = &rx_ring->buffer_info[i];
4198
Jeff Kirshera292ca62006-01-12 16:51:30 -08004199 while (cleaned_count--) {
Christoph Hellwigca6f7222006-08-31 14:27:47 -07004200 skb = buffer_info->skb;
4201 if (skb) {
Jeff Kirshera292ca62006-01-12 16:51:30 -08004202 skb_trim(skb, 0);
4203 goto map_skb;
4204 }
4205
Eric Dumazet89d71a62009-10-13 05:34:20 +00004206 skb = netdev_alloc_skb_ip_align(netdev, bufsz);
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004207 if (unlikely(!skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208 /* Better luck next round */
Jeff Kirsher72d64a42006-01-12 16:51:19 -08004209 adapter->alloc_rx_buff_failed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210 break;
4211 }
4212
Malli Chilakala26483452005-04-28 19:44:46 -07004213 /* Fix for errata 23, can't cross 64kB boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214 if (!e1000_check_64k_bound(adapter, skb->data, bufsz)) {
4215 struct sk_buff *oldskb = skb;
Emil Tantilovfeb8f472010-07-26 23:37:21 -07004216 e_err(rx_err, "skb align check failed: %u bytes at "
4217 "%p\n", bufsz, skb->data);
Malli Chilakala26483452005-04-28 19:44:46 -07004218 /* Try again, without freeing the previous */
Eric Dumazet89d71a62009-10-13 05:34:20 +00004219 skb = netdev_alloc_skb_ip_align(netdev, bufsz);
Malli Chilakala26483452005-04-28 19:44:46 -07004220 /* Failed allocation, critical failure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221 if (!skb) {
4222 dev_kfree_skb(oldskb);
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004223 adapter->alloc_rx_buff_failed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224 break;
4225 }
Malli Chilakala26483452005-04-28 19:44:46 -07004226
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227 if (!e1000_check_64k_bound(adapter, skb->data, bufsz)) {
4228 /* give up */
4229 dev_kfree_skb(skb);
4230 dev_kfree_skb(oldskb);
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004231 adapter->alloc_rx_buff_failed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232 break; /* while !buffer_info->skb */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233 }
Christoph Hellwigca6f7222006-08-31 14:27:47 -07004234
4235 /* Use new allocation */
4236 dev_kfree_skb(oldskb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238 buffer_info->skb = skb;
4239 buffer_info->length = adapter->rx_buffer_len;
Jeff Kirshera292ca62006-01-12 16:51:30 -08004240map_skb:
Nick Nunleyb16f53b2010-04-27 13:08:45 +00004241 buffer_info->dma = dma_map_single(&pdev->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242 skb->data,
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004243 buffer_info->length,
Nick Nunleyb16f53b2010-04-27 13:08:45 +00004244 DMA_FROM_DEVICE);
4245 if (dma_mapping_error(&pdev->dev, buffer_info->dma)) {
Anton Blanchardb5abb022010-02-19 17:54:53 +00004246 dev_kfree_skb(skb);
4247 buffer_info->skb = NULL;
4248 buffer_info->dma = 0;
4249 adapter->alloc_rx_buff_failed++;
4250 break; /* while !buffer_info->skb */
4251 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004253 /*
4254 * XXX if it was allocated cleanly it will never map to a
4255 * boundary crossing
4256 */
4257
Malli Chilakala26483452005-04-28 19:44:46 -07004258 /* Fix for errata 23, can't cross 64kB boundary */
4259 if (!e1000_check_64k_bound(adapter,
4260 (void *)(unsigned long)buffer_info->dma,
4261 adapter->rx_buffer_len)) {
Emil Tantilovfeb8f472010-07-26 23:37:21 -07004262 e_err(rx_err, "dma align check failed: %u bytes at "
4263 "%p\n", adapter->rx_buffer_len,
Emil Tantilov675ad472010-04-27 14:02:58 +00004264 (void *)(unsigned long)buffer_info->dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265 dev_kfree_skb(skb);
4266 buffer_info->skb = NULL;
4267
Nick Nunleyb16f53b2010-04-27 13:08:45 +00004268 dma_unmap_single(&pdev->dev, buffer_info->dma,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269 adapter->rx_buffer_len,
Nick Nunleyb16f53b2010-04-27 13:08:45 +00004270 DMA_FROM_DEVICE);
Jesse Brandeburg679be3b2009-06-30 12:45:34 +00004271 buffer_info->dma = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004273 adapter->alloc_rx_buff_failed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274 break; /* while !buffer_info->skb */
4275 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276 rx_desc = E1000_RX_DESC(*rx_ring, i);
4277 rx_desc->buffer_addr = cpu_to_le64(buffer_info->dma);
4278
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004279 if (unlikely(++i == rx_ring->count))
4280 i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281 buffer_info = &rx_ring->buffer_info[i];
4282 }
4283
Jesse Brandeburgb92ff8e2006-01-18 13:01:32 -08004284 if (likely(rx_ring->next_to_use != i)) {
4285 rx_ring->next_to_use = i;
4286 if (unlikely(i-- == 0))
4287 i = (rx_ring->count - 1);
4288
4289 /* Force memory writes to complete before letting h/w
4290 * know there are new descriptors to fetch. (Only
4291 * applicable for weak-ordered memory model archs,
4292 * such as IA-64). */
4293 wmb();
Joe Perches1dc32912008-07-11 15:17:08 -07004294 writel(i, hw->hw_addr + rx_ring->rdt);
Jesse Brandeburgb92ff8e2006-01-18 13:01:32 -08004295 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296}
4297
4298/**
4299 * e1000_smartspeed - Workaround for SmartSpeed on 82541 and 82547 controllers.
4300 * @adapter:
4301 **/
4302
Joe Perches64798842008-07-11 15:17:02 -07004303static void e1000_smartspeed(struct e1000_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304{
Joe Perches1dc32912008-07-11 15:17:08 -07004305 struct e1000_hw *hw = &adapter->hw;
Joe Perches406874a2008-04-03 10:06:32 -07004306 u16 phy_status;
4307 u16 phy_ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308
Joe Perches1dc32912008-07-11 15:17:08 -07004309 if ((hw->phy_type != e1000_phy_igp) || !hw->autoneg ||
4310 !(hw->autoneg_advertised & ADVERTISE_1000_FULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311 return;
4312
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004313 if (adapter->smartspeed == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314 /* If Master/Slave config fault is asserted twice,
4315 * we assume back-to-back */
Joe Perches1dc32912008-07-11 15:17:08 -07004316 e1000_read_phy_reg(hw, PHY_1000T_STATUS, &phy_status);
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004317 if (!(phy_status & SR_1000T_MS_CONFIG_FAULT)) return;
Joe Perches1dc32912008-07-11 15:17:08 -07004318 e1000_read_phy_reg(hw, PHY_1000T_STATUS, &phy_status);
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004319 if (!(phy_status & SR_1000T_MS_CONFIG_FAULT)) return;
Joe Perches1dc32912008-07-11 15:17:08 -07004320 e1000_read_phy_reg(hw, PHY_1000T_CTRL, &phy_ctrl);
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004321 if (phy_ctrl & CR_1000T_MS_ENABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322 phy_ctrl &= ~CR_1000T_MS_ENABLE;
Joe Perches1dc32912008-07-11 15:17:08 -07004323 e1000_write_phy_reg(hw, PHY_1000T_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324 phy_ctrl);
4325 adapter->smartspeed++;
Joe Perches1dc32912008-07-11 15:17:08 -07004326 if (!e1000_phy_setup_autoneg(hw) &&
4327 !e1000_read_phy_reg(hw, PHY_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328 &phy_ctrl)) {
4329 phy_ctrl |= (MII_CR_AUTO_NEG_EN |
4330 MII_CR_RESTART_AUTO_NEG);
Joe Perches1dc32912008-07-11 15:17:08 -07004331 e1000_write_phy_reg(hw, PHY_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332 phy_ctrl);
4333 }
4334 }
4335 return;
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004336 } else if (adapter->smartspeed == E1000_SMARTSPEED_DOWNSHIFT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337 /* If still no link, perhaps using 2/3 pair cable */
Joe Perches1dc32912008-07-11 15:17:08 -07004338 e1000_read_phy_reg(hw, PHY_1000T_CTRL, &phy_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339 phy_ctrl |= CR_1000T_MS_ENABLE;
Joe Perches1dc32912008-07-11 15:17:08 -07004340 e1000_write_phy_reg(hw, PHY_1000T_CTRL, phy_ctrl);
4341 if (!e1000_phy_setup_autoneg(hw) &&
4342 !e1000_read_phy_reg(hw, PHY_CTRL, &phy_ctrl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343 phy_ctrl |= (MII_CR_AUTO_NEG_EN |
4344 MII_CR_RESTART_AUTO_NEG);
Joe Perches1dc32912008-07-11 15:17:08 -07004345 e1000_write_phy_reg(hw, PHY_CTRL, phy_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346 }
4347 }
4348 /* Restart process after E1000_SMARTSPEED_MAX iterations */
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004349 if (adapter->smartspeed++ == E1000_SMARTSPEED_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350 adapter->smartspeed = 0;
4351}
4352
4353/**
4354 * e1000_ioctl -
4355 * @netdev:
4356 * @ifreq:
4357 * @cmd:
4358 **/
4359
Joe Perches64798842008-07-11 15:17:02 -07004360static int e1000_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361{
4362 switch (cmd) {
4363 case SIOCGMIIPHY:
4364 case SIOCGMIIREG:
4365 case SIOCSMIIREG:
4366 return e1000_mii_ioctl(netdev, ifr, cmd);
4367 default:
4368 return -EOPNOTSUPP;
4369 }
4370}
4371
4372/**
4373 * e1000_mii_ioctl -
4374 * @netdev:
4375 * @ifreq:
4376 * @cmd:
4377 **/
4378
Joe Perches64798842008-07-11 15:17:02 -07004379static int e1000_mii_ioctl(struct net_device *netdev, struct ifreq *ifr,
4380 int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381{
Malli Chilakala60490fe2005-06-17 17:41:45 -07004382 struct e1000_adapter *adapter = netdev_priv(netdev);
Joe Perches1dc32912008-07-11 15:17:08 -07004383 struct e1000_hw *hw = &adapter->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384 struct mii_ioctl_data *data = if_mii(ifr);
4385 int retval;
Joe Perches406874a2008-04-03 10:06:32 -07004386 u16 mii_reg;
Malli Chilakala97876fc2005-06-17 17:40:19 -07004387 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388
Joe Perches1dc32912008-07-11 15:17:08 -07004389 if (hw->media_type != e1000_media_type_copper)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390 return -EOPNOTSUPP;
4391
4392 switch (cmd) {
4393 case SIOCGMIIPHY:
Joe Perches1dc32912008-07-11 15:17:08 -07004394 data->phy_id = hw->phy_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395 break;
4396 case SIOCGMIIREG:
Malli Chilakala97876fc2005-06-17 17:40:19 -07004397 spin_lock_irqsave(&adapter->stats_lock, flags);
Joe Perches1dc32912008-07-11 15:17:08 -07004398 if (e1000_read_phy_reg(hw, data->reg_num & 0x1F,
Malli Chilakala97876fc2005-06-17 17:40:19 -07004399 &data->val_out)) {
4400 spin_unlock_irqrestore(&adapter->stats_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401 return -EIO;
Malli Chilakala97876fc2005-06-17 17:40:19 -07004402 }
4403 spin_unlock_irqrestore(&adapter->stats_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404 break;
4405 case SIOCSMIIREG:
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004406 if (data->reg_num & ~(0x1F))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407 return -EFAULT;
4408 mii_reg = data->val_in;
Malli Chilakala97876fc2005-06-17 17:40:19 -07004409 spin_lock_irqsave(&adapter->stats_lock, flags);
Joe Perches1dc32912008-07-11 15:17:08 -07004410 if (e1000_write_phy_reg(hw, data->reg_num,
Malli Chilakala97876fc2005-06-17 17:40:19 -07004411 mii_reg)) {
4412 spin_unlock_irqrestore(&adapter->stats_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413 return -EIO;
Malli Chilakala97876fc2005-06-17 17:40:19 -07004414 }
Jesse Brandeburgf0163ac2007-11-13 21:00:09 -08004415 spin_unlock_irqrestore(&adapter->stats_lock, flags);
Joe Perches1dc32912008-07-11 15:17:08 -07004416 if (hw->media_type == e1000_media_type_copper) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417 switch (data->reg_num) {
4418 case PHY_CTRL:
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004419 if (mii_reg & MII_CR_POWER_DOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420 break;
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004421 if (mii_reg & MII_CR_AUTO_NEG_EN) {
Joe Perches1dc32912008-07-11 15:17:08 -07004422 hw->autoneg = 1;
4423 hw->autoneg_advertised = 0x2F;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424 } else {
David Decotigny14ad2512011-04-27 18:32:43 +00004425 u32 speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426 if (mii_reg & 0x40)
David Decotigny14ad2512011-04-27 18:32:43 +00004427 speed = SPEED_1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428 else if (mii_reg & 0x2000)
David Decotigny14ad2512011-04-27 18:32:43 +00004429 speed = SPEED_100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430 else
David Decotigny14ad2512011-04-27 18:32:43 +00004431 speed = SPEED_10;
4432 retval = e1000_set_spd_dplx(
4433 adapter, speed,
4434 ((mii_reg & 0x100)
4435 ? DUPLEX_FULL :
4436 DUPLEX_HALF));
Jesse Brandeburgf0163ac2007-11-13 21:00:09 -08004437 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438 return retval;
4439 }
Auke Kok2db10a02006-06-27 09:06:28 -07004440 if (netif_running(adapter->netdev))
4441 e1000_reinit_locked(adapter);
4442 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443 e1000_reset(adapter);
4444 break;
4445 case M88E1000_PHY_SPEC_CTRL:
4446 case M88E1000_EXT_PHY_SPEC_CTRL:
Joe Perches1dc32912008-07-11 15:17:08 -07004447 if (e1000_phy_reset(hw))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448 return -EIO;
4449 break;
4450 }
4451 } else {
4452 switch (data->reg_num) {
4453 case PHY_CTRL:
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004454 if (mii_reg & MII_CR_POWER_DOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455 break;
Auke Kok2db10a02006-06-27 09:06:28 -07004456 if (netif_running(adapter->netdev))
4457 e1000_reinit_locked(adapter);
4458 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459 e1000_reset(adapter);
4460 break;
4461 }
4462 }
4463 break;
4464 default:
4465 return -EOPNOTSUPP;
4466 }
4467 return E1000_SUCCESS;
4468}
4469
Joe Perches64798842008-07-11 15:17:02 -07004470void e1000_pci_set_mwi(struct e1000_hw *hw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471{
4472 struct e1000_adapter *adapter = hw->back;
Malli Chilakala26483452005-04-28 19:44:46 -07004473 int ret_val = pci_set_mwi(adapter->pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004475 if (ret_val)
Emil Tantilovfeb8f472010-07-26 23:37:21 -07004476 e_err(probe, "Error in setting MWI\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477}
4478
Joe Perches64798842008-07-11 15:17:02 -07004479void e1000_pci_clear_mwi(struct e1000_hw *hw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480{
4481 struct e1000_adapter *adapter = hw->back;
4482
4483 pci_clear_mwi(adapter->pdev);
4484}
4485
Joe Perches64798842008-07-11 15:17:02 -07004486int e1000_pcix_get_mmrbc(struct e1000_hw *hw)
Peter Oruba007755e2007-09-28 22:42:06 -07004487{
4488 struct e1000_adapter *adapter = hw->back;
4489 return pcix_get_mmrbc(adapter->pdev);
4490}
4491
Joe Perches64798842008-07-11 15:17:02 -07004492void e1000_pcix_set_mmrbc(struct e1000_hw *hw, int mmrbc)
Peter Oruba007755e2007-09-28 22:42:06 -07004493{
4494 struct e1000_adapter *adapter = hw->back;
4495 pcix_set_mmrbc(adapter->pdev, mmrbc);
4496}
4497
Joe Perches64798842008-07-11 15:17:02 -07004498void e1000_io_write(struct e1000_hw *hw, unsigned long port, u32 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499{
4500 outl(value, port);
4501}
4502
Joe Perches64798842008-07-11 15:17:02 -07004503static void e1000_vlan_rx_register(struct net_device *netdev,
4504 struct vlan_group *grp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505{
Malli Chilakala60490fe2005-06-17 17:41:45 -07004506 struct e1000_adapter *adapter = netdev_priv(netdev);
Joe Perches1dc32912008-07-11 15:17:08 -07004507 struct e1000_hw *hw = &adapter->hw;
Joe Perches406874a2008-04-03 10:06:32 -07004508 u32 ctrl, rctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509
Jesse Brandeburg9150b762008-03-21 11:06:58 -07004510 if (!test_bit(__E1000_DOWN, &adapter->flags))
4511 e1000_irq_disable(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512 adapter->vlgrp = grp;
4513
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004514 if (grp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515 /* enable VLAN tag insert/strip */
Joe Perches1dc32912008-07-11 15:17:08 -07004516 ctrl = er32(CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004517 ctrl |= E1000_CTRL_VME;
Joe Perches1dc32912008-07-11 15:17:08 -07004518 ew32(CTRL, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00004520 /* enable VLAN receive filtering */
4521 rctl = er32(RCTL);
4522 rctl &= ~E1000_RCTL_CFIEN;
4523 if (!(netdev->flags & IFF_PROMISC))
4524 rctl |= E1000_RCTL_VFE;
4525 ew32(RCTL, rctl);
4526 e1000_update_mng_vlan(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527 } else {
4528 /* disable VLAN tag insert/strip */
Joe Perches1dc32912008-07-11 15:17:08 -07004529 ctrl = er32(CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530 ctrl &= ~E1000_CTRL_VME;
Joe Perches1dc32912008-07-11 15:17:08 -07004531 ew32(CTRL, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00004533 /* disable VLAN receive filtering */
4534 rctl = er32(RCTL);
4535 rctl &= ~E1000_RCTL_VFE;
4536 ew32(RCTL, rctl);
Graham, Davidfd38d7a2009-08-31 14:12:51 +00004537
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00004538 if (adapter->mng_vlan_id != (u16)E1000_MNG_VLAN_NONE) {
Jesse Brandeburg120a5d02009-09-25 15:19:46 -07004539 e1000_vlan_rx_kill_vid(netdev, adapter->mng_vlan_id);
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00004540 adapter->mng_vlan_id = E1000_MNG_VLAN_NONE;
Auke Kokcd94dd02006-06-27 09:08:22 -07004541 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542 }
4543
Jesse Brandeburg9150b762008-03-21 11:06:58 -07004544 if (!test_bit(__E1000_DOWN, &adapter->flags))
4545 e1000_irq_enable(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546}
4547
Joe Perches64798842008-07-11 15:17:02 -07004548static void e1000_vlan_rx_add_vid(struct net_device *netdev, u16 vid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549{
Malli Chilakala60490fe2005-06-17 17:41:45 -07004550 struct e1000_adapter *adapter = netdev_priv(netdev);
Joe Perches1dc32912008-07-11 15:17:08 -07004551 struct e1000_hw *hw = &adapter->hw;
Joe Perches406874a2008-04-03 10:06:32 -07004552 u32 vfta, index;
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004553
Joe Perches1dc32912008-07-11 15:17:08 -07004554 if ((hw->mng_cookie.status &
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004555 E1000_MNG_DHCP_COOKIE_STATUS_VLAN_SUPPORT) &&
4556 (vid == adapter->mng_vlan_id))
Malli Chilakala2d7edb92005-04-28 19:43:52 -07004557 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558 /* add VID to filter table */
4559 index = (vid >> 5) & 0x7F;
Joe Perches1dc32912008-07-11 15:17:08 -07004560 vfta = E1000_READ_REG_ARRAY(hw, VFTA, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561 vfta |= (1 << (vid & 0x1F));
Joe Perches1dc32912008-07-11 15:17:08 -07004562 e1000_write_vfta(hw, index, vfta);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563}
4564
Joe Perches64798842008-07-11 15:17:02 -07004565static void e1000_vlan_rx_kill_vid(struct net_device *netdev, u16 vid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566{
Malli Chilakala60490fe2005-06-17 17:41:45 -07004567 struct e1000_adapter *adapter = netdev_priv(netdev);
Joe Perches1dc32912008-07-11 15:17:08 -07004568 struct e1000_hw *hw = &adapter->hw;
Joe Perches406874a2008-04-03 10:06:32 -07004569 u32 vfta, index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570
Jesse Brandeburg9150b762008-03-21 11:06:58 -07004571 if (!test_bit(__E1000_DOWN, &adapter->flags))
4572 e1000_irq_disable(adapter);
Dan Aloni5c15bde2007-03-02 20:44:51 -08004573 vlan_group_set_device(adapter->vlgrp, vid, NULL);
Jesse Brandeburg9150b762008-03-21 11:06:58 -07004574 if (!test_bit(__E1000_DOWN, &adapter->flags))
4575 e1000_irq_enable(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576
4577 /* remove VID from filter table */
4578 index = (vid >> 5) & 0x7F;
Joe Perches1dc32912008-07-11 15:17:08 -07004579 vfta = E1000_READ_REG_ARRAY(hw, VFTA, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580 vfta &= ~(1 << (vid & 0x1F));
Joe Perches1dc32912008-07-11 15:17:08 -07004581 e1000_write_vfta(hw, index, vfta);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582}
4583
Joe Perches64798842008-07-11 15:17:02 -07004584static void e1000_restore_vlan(struct e1000_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585{
4586 e1000_vlan_rx_register(adapter->netdev, adapter->vlgrp);
4587
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004588 if (adapter->vlgrp) {
Joe Perches406874a2008-04-03 10:06:32 -07004589 u16 vid;
Jesse Grossb7381272010-10-20 13:56:02 +00004590 for (vid = 0; vid < VLAN_N_VID; vid++) {
Dan Aloni5c15bde2007-03-02 20:44:51 -08004591 if (!vlan_group_get_device(adapter->vlgrp, vid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592 continue;
4593 e1000_vlan_rx_add_vid(adapter->netdev, vid);
4594 }
4595 }
4596}
4597
David Decotigny14ad2512011-04-27 18:32:43 +00004598int e1000_set_spd_dplx(struct e1000_adapter *adapter, u32 spd, u8 dplx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599{
Joe Perches1dc32912008-07-11 15:17:08 -07004600 struct e1000_hw *hw = &adapter->hw;
4601
4602 hw->autoneg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603
David Decotigny14ad2512011-04-27 18:32:43 +00004604 /* Make sure dplx is at most 1 bit and lsb of speed is not set
4605 * for the switch() below to work */
4606 if ((spd & 1) || (dplx & ~1))
4607 goto err_inval;
4608
Malli Chilakala69213682005-06-17 17:44:20 -07004609 /* Fiber NICs only allow 1000 gbps Full duplex */
Joe Perches1dc32912008-07-11 15:17:08 -07004610 if ((hw->media_type == e1000_media_type_fiber) &&
David Decotigny14ad2512011-04-27 18:32:43 +00004611 spd != SPEED_1000 &&
4612 dplx != DUPLEX_FULL)
4613 goto err_inval;
Malli Chilakala69213682005-06-17 17:44:20 -07004614
David Decotigny14ad2512011-04-27 18:32:43 +00004615 switch (spd + dplx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616 case SPEED_10 + DUPLEX_HALF:
Joe Perches1dc32912008-07-11 15:17:08 -07004617 hw->forced_speed_duplex = e1000_10_half;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618 break;
4619 case SPEED_10 + DUPLEX_FULL:
Joe Perches1dc32912008-07-11 15:17:08 -07004620 hw->forced_speed_duplex = e1000_10_full;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621 break;
4622 case SPEED_100 + DUPLEX_HALF:
Joe Perches1dc32912008-07-11 15:17:08 -07004623 hw->forced_speed_duplex = e1000_100_half;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624 break;
4625 case SPEED_100 + DUPLEX_FULL:
Joe Perches1dc32912008-07-11 15:17:08 -07004626 hw->forced_speed_duplex = e1000_100_full;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627 break;
4628 case SPEED_1000 + DUPLEX_FULL:
Joe Perches1dc32912008-07-11 15:17:08 -07004629 hw->autoneg = 1;
4630 hw->autoneg_advertised = ADVERTISE_1000_FULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631 break;
4632 case SPEED_1000 + DUPLEX_HALF: /* not supported */
4633 default:
David Decotigny14ad2512011-04-27 18:32:43 +00004634 goto err_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635 }
4636 return 0;
David Decotigny14ad2512011-04-27 18:32:43 +00004637
4638err_inval:
4639 e_err(probe, "Unsupported Speed/Duplex configuration\n");
4640 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641}
4642
Rafael J. Wysockib43fcd72009-04-15 17:43:24 +00004643static int __e1000_shutdown(struct pci_dev *pdev, bool *enable_wake)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644{
4645 struct net_device *netdev = pci_get_drvdata(pdev);
Malli Chilakala60490fe2005-06-17 17:41:45 -07004646 struct e1000_adapter *adapter = netdev_priv(netdev);
Joe Perches1dc32912008-07-11 15:17:08 -07004647 struct e1000_hw *hw = &adapter->hw;
Joe Perches406874a2008-04-03 10:06:32 -07004648 u32 ctrl, ctrl_ext, rctl, status;
4649 u32 wufc = adapter->wol;
Auke Kok6fdfef12006-06-27 09:06:36 -07004650#ifdef CONFIG_PM
Jeff Kirsher240b1712006-01-12 16:51:28 -08004651 int retval = 0;
Auke Kok6fdfef12006-06-27 09:06:36 -07004652#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653
4654 netif_device_detach(netdev);
4655
Auke Kok2db10a02006-06-27 09:06:28 -07004656 if (netif_running(netdev)) {
4657 WARN_ON(test_bit(__E1000_RESETTING, &adapter->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658 e1000_down(adapter);
Auke Kok2db10a02006-06-27 09:06:28 -07004659 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660
Jesse Brandeburg2f826652006-01-18 13:01:34 -08004661#ifdef CONFIG_PM
Kok, Auke1d33e9c2007-02-16 14:39:28 -08004662 retval = pci_save_state(pdev);
Jesse Brandeburg2f826652006-01-18 13:01:34 -08004663 if (retval)
4664 return retval;
4665#endif
4666
Joe Perches1dc32912008-07-11 15:17:08 -07004667 status = er32(STATUS);
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004668 if (status & E1000_STATUS_LU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669 wufc &= ~E1000_WUFC_LNKC;
4670
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004671 if (wufc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672 e1000_setup_rctl(adapter);
Patrick McHardydb0ce502007-11-13 20:54:59 -08004673 e1000_set_rx_mode(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674
4675 /* turn on all-multi mode if wake on multicast is enabled */
Jesse Brandeburg120cd572006-08-31 14:27:46 -07004676 if (wufc & E1000_WUFC_MC) {
Joe Perches1dc32912008-07-11 15:17:08 -07004677 rctl = er32(RCTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678 rctl |= E1000_RCTL_MPE;
Joe Perches1dc32912008-07-11 15:17:08 -07004679 ew32(RCTL, rctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680 }
4681
Joe Perches1dc32912008-07-11 15:17:08 -07004682 if (hw->mac_type >= e1000_82540) {
4683 ctrl = er32(CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684 /* advertise wake from D3Cold */
4685 #define E1000_CTRL_ADVD3WUC 0x00100000
4686 /* phy power management enable */
4687 #define E1000_CTRL_EN_PHY_PWR_MGMT 0x00200000
4688 ctrl |= E1000_CTRL_ADVD3WUC |
4689 E1000_CTRL_EN_PHY_PWR_MGMT;
Joe Perches1dc32912008-07-11 15:17:08 -07004690 ew32(CTRL, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691 }
4692
Joe Perches1dc32912008-07-11 15:17:08 -07004693 if (hw->media_type == e1000_media_type_fiber ||
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00004694 hw->media_type == e1000_media_type_internal_serdes) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695 /* keep the laser running in D3 */
Joe Perches1dc32912008-07-11 15:17:08 -07004696 ctrl_ext = er32(CTRL_EXT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697 ctrl_ext |= E1000_CTRL_EXT_SDP7_DATA;
Joe Perches1dc32912008-07-11 15:17:08 -07004698 ew32(CTRL_EXT, ctrl_ext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699 }
4700
Joe Perches1dc32912008-07-11 15:17:08 -07004701 ew32(WUC, E1000_WUC_PME_EN);
4702 ew32(WUFC, wufc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703 } else {
Joe Perches1dc32912008-07-11 15:17:08 -07004704 ew32(WUC, 0);
4705 ew32(WUFC, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706 }
4707
Jeff Garzik0fccd0e2006-12-15 10:56:10 -05004708 e1000_release_manageability(adapter);
4709
Rafael J. Wysockib43fcd72009-04-15 17:43:24 +00004710 *enable_wake = !!wufc;
4711
Jeff Garzik0fccd0e2006-12-15 10:56:10 -05004712 /* make sure adapter isn't asleep if manageability is enabled */
Rafael J. Wysockib43fcd72009-04-15 17:43:24 +00004713 if (adapter->en_mng_pt)
4714 *enable_wake = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715
Auke Kokedd106f2006-11-06 08:57:12 -08004716 if (netif_running(netdev))
4717 e1000_free_irq(adapter);
4718
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719 pci_disable_device(pdev);
Jeff Kirsher240b1712006-01-12 16:51:28 -08004720
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721 return 0;
4722}
4723
Jesse Brandeburg2f826652006-01-18 13:01:34 -08004724#ifdef CONFIG_PM
Rafael J. Wysockib43fcd72009-04-15 17:43:24 +00004725static int e1000_suspend(struct pci_dev *pdev, pm_message_t state)
4726{
4727 int retval;
4728 bool wake;
4729
4730 retval = __e1000_shutdown(pdev, &wake);
4731 if (retval)
4732 return retval;
4733
4734 if (wake) {
4735 pci_prepare_to_sleep(pdev);
4736 } else {
4737 pci_wake_from_d3(pdev, false);
4738 pci_set_power_state(pdev, PCI_D3hot);
4739 }
4740
4741 return 0;
4742}
4743
Joe Perches64798842008-07-11 15:17:02 -07004744static int e1000_resume(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004745{
4746 struct net_device *netdev = pci_get_drvdata(pdev);
Malli Chilakala60490fe2005-06-17 17:41:45 -07004747 struct e1000_adapter *adapter = netdev_priv(netdev);
Joe Perches1dc32912008-07-11 15:17:08 -07004748 struct e1000_hw *hw = &adapter->hw;
Joe Perches406874a2008-04-03 10:06:32 -07004749 u32 err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750
Auke Kokd0e027d2006-04-14 19:04:40 -07004751 pci_set_power_state(pdev, PCI_D0);
Kok, Auke1d33e9c2007-02-16 14:39:28 -08004752 pci_restore_state(pdev);
Nick Nunleydbb5aae2010-02-03 14:49:48 +00004753 pci_save_state(pdev);
Taku Izumi81250292008-07-11 15:17:44 -07004754
4755 if (adapter->need_ioport)
4756 err = pci_enable_device(pdev);
4757 else
4758 err = pci_enable_device_mem(pdev);
Joe Perchesc7be73b2008-07-11 15:17:28 -07004759 if (err) {
Emil Tantilov675ad472010-04-27 14:02:58 +00004760 pr_err("Cannot enable PCI device from suspend\n");
Auke Kok3d1dd8c2006-08-28 14:56:27 -07004761 return err;
4762 }
Malli Chilakalaa4cb8472005-04-28 19:41:28 -07004763 pci_set_master(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764
Auke Kokd0e027d2006-04-14 19:04:40 -07004765 pci_enable_wake(pdev, PCI_D3hot, 0);
4766 pci_enable_wake(pdev, PCI_D3cold, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767
Joe Perchesc7be73b2008-07-11 15:17:28 -07004768 if (netif_running(netdev)) {
4769 err = e1000_request_irq(adapter);
4770 if (err)
4771 return err;
4772 }
Auke Kokedd106f2006-11-06 08:57:12 -08004773
4774 e1000_power_up_phy(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775 e1000_reset(adapter);
Joe Perches1dc32912008-07-11 15:17:08 -07004776 ew32(WUS, ~0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777
Jeff Garzik0fccd0e2006-12-15 10:56:10 -05004778 e1000_init_manageability(adapter);
4779
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004780 if (netif_running(netdev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781 e1000_up(adapter);
4782
4783 netif_device_attach(netdev);
4784
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785 return 0;
4786}
4787#endif
Auke Kokc653e632006-05-23 13:35:57 -07004788
4789static void e1000_shutdown(struct pci_dev *pdev)
4790{
Rafael J. Wysockib43fcd72009-04-15 17:43:24 +00004791 bool wake;
4792
4793 __e1000_shutdown(pdev, &wake);
4794
4795 if (system_state == SYSTEM_POWER_OFF) {
4796 pci_wake_from_d3(pdev, wake);
4797 pci_set_power_state(pdev, PCI_D3hot);
4798 }
Auke Kokc653e632006-05-23 13:35:57 -07004799}
4800
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801#ifdef CONFIG_NET_POLL_CONTROLLER
4802/*
4803 * Polling 'interrupt' - used by things like netconsole to send skbs
4804 * without having to re-enable interrupts. It's not called while
4805 * the interrupt routine is executing.
4806 */
Joe Perches64798842008-07-11 15:17:02 -07004807static void e1000_netpoll(struct net_device *netdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004808{
Malli Chilakala60490fe2005-06-17 17:41:45 -07004809 struct e1000_adapter *adapter = netdev_priv(netdev);
Auke Kokd3d9e482006-07-14 16:14:23 -07004810
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811 disable_irq(adapter->pdev->irq);
David Howells7d12e782006-10-05 14:55:46 +01004812 e1000_intr(adapter->pdev->irq, netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813 enable_irq(adapter->pdev->irq);
4814}
4815#endif
4816
Auke Kok90267292006-06-08 09:30:24 -07004817/**
4818 * e1000_io_error_detected - called when PCI error is detected
4819 * @pdev: Pointer to PCI device
Jesse Brandeburg120a5d02009-09-25 15:19:46 -07004820 * @state: The current pci connection state
Auke Kok90267292006-06-08 09:30:24 -07004821 *
4822 * This function is called after a PCI bus error affecting
4823 * this device has been detected.
4824 */
Joe Perches64798842008-07-11 15:17:02 -07004825static pci_ers_result_t e1000_io_error_detected(struct pci_dev *pdev,
4826 pci_channel_state_t state)
Auke Kok90267292006-06-08 09:30:24 -07004827{
4828 struct net_device *netdev = pci_get_drvdata(pdev);
Wang Chen4cf16532008-11-12 23:38:14 -08004829 struct e1000_adapter *adapter = netdev_priv(netdev);
Auke Kok90267292006-06-08 09:30:24 -07004830
4831 netif_device_detach(netdev);
4832
Andre Detscheab63302009-06-30 12:46:13 +00004833 if (state == pci_channel_io_perm_failure)
4834 return PCI_ERS_RESULT_DISCONNECT;
4835
Auke Kok90267292006-06-08 09:30:24 -07004836 if (netif_running(netdev))
4837 e1000_down(adapter);
Linas Vepstas72e8d6b2006-09-18 20:58:06 -07004838 pci_disable_device(pdev);
Auke Kok90267292006-06-08 09:30:24 -07004839
4840 /* Request a slot slot reset. */
4841 return PCI_ERS_RESULT_NEED_RESET;
4842}
4843
4844/**
4845 * e1000_io_slot_reset - called after the pci bus has been reset.
4846 * @pdev: Pointer to PCI device
4847 *
4848 * Restart the card from scratch, as if from a cold-boot. Implementation
4849 * resembles the first-half of the e1000_resume routine.
4850 */
4851static pci_ers_result_t e1000_io_slot_reset(struct pci_dev *pdev)
4852{
4853 struct net_device *netdev = pci_get_drvdata(pdev);
Wang Chen4cf16532008-11-12 23:38:14 -08004854 struct e1000_adapter *adapter = netdev_priv(netdev);
Joe Perches1dc32912008-07-11 15:17:08 -07004855 struct e1000_hw *hw = &adapter->hw;
Taku Izumi81250292008-07-11 15:17:44 -07004856 int err;
Auke Kok90267292006-06-08 09:30:24 -07004857
Taku Izumi81250292008-07-11 15:17:44 -07004858 if (adapter->need_ioport)
4859 err = pci_enable_device(pdev);
4860 else
4861 err = pci_enable_device_mem(pdev);
4862 if (err) {
Emil Tantilov675ad472010-04-27 14:02:58 +00004863 pr_err("Cannot re-enable PCI device after reset.\n");
Auke Kok90267292006-06-08 09:30:24 -07004864 return PCI_ERS_RESULT_DISCONNECT;
4865 }
4866 pci_set_master(pdev);
4867
Linas Vepstasdbf38c92006-09-27 12:54:11 -07004868 pci_enable_wake(pdev, PCI_D3hot, 0);
4869 pci_enable_wake(pdev, PCI_D3cold, 0);
Auke Kok90267292006-06-08 09:30:24 -07004870
Auke Kok90267292006-06-08 09:30:24 -07004871 e1000_reset(adapter);
Joe Perches1dc32912008-07-11 15:17:08 -07004872 ew32(WUS, ~0);
Auke Kok90267292006-06-08 09:30:24 -07004873
4874 return PCI_ERS_RESULT_RECOVERED;
4875}
4876
4877/**
4878 * e1000_io_resume - called when traffic can start flowing again.
4879 * @pdev: Pointer to PCI device
4880 *
4881 * This callback is called when the error recovery driver tells us that
4882 * its OK to resume normal operation. Implementation resembles the
4883 * second-half of the e1000_resume routine.
4884 */
4885static void e1000_io_resume(struct pci_dev *pdev)
4886{
4887 struct net_device *netdev = pci_get_drvdata(pdev);
Wang Chen4cf16532008-11-12 23:38:14 -08004888 struct e1000_adapter *adapter = netdev_priv(netdev);
Jeff Garzik0fccd0e2006-12-15 10:56:10 -05004889
4890 e1000_init_manageability(adapter);
Auke Kok90267292006-06-08 09:30:24 -07004891
4892 if (netif_running(netdev)) {
4893 if (e1000_up(adapter)) {
Emil Tantilov675ad472010-04-27 14:02:58 +00004894 pr_info("can't bring device back up after reset\n");
Auke Kok90267292006-06-08 09:30:24 -07004895 return;
4896 }
4897 }
4898
4899 netif_device_attach(netdev);
Auke Kok90267292006-06-08 09:30:24 -07004900}
4901
Linus Torvalds1da177e2005-04-16 15:20:36 -07004902/* e1000_main.c */