blob: 363fd395c75b0235962a575a8078d87586485e98 [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>
Paul Gortmaker70c71602011-05-22 16:47:17 -040032#include <linux/prefetch.h>
Jiri Pirko5622e402011-07-21 03:26:31 +000033#include <linux/bitops.h>
34#include <linux/if_vlan.h>
Dirk Brandewie5377a412011-01-06 14:29:54 +000035
Linus Torvalds1da177e2005-04-16 15:20:36 -070036char e1000_driver_name[] = "e1000";
Adrian Bunk3ad2cc62005-10-30 16:53:34 +010037static char e1000_driver_string[] = "Intel(R) PRO/1000 Network Driver";
Anupam Chandaab088532010-11-21 09:54:21 -080038#define DRV_VERSION "7.3.21-k8-NAPI"
Stephen Hemmingerabec42a2007-10-29 10:46:19 -070039const char e1000_driver_version[] = DRV_VERSION;
40static const char e1000_copyright[] = "Copyright (c) 1999-2006 Intel Corporation.";
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42/* e1000_pci_tbl - PCI Device ID Table
43 *
44 * Last entry must be all 0s
45 *
46 * Macro expands to...
47 * {PCI_DEVICE(PCI_VENDOR_ID_INTEL, device_id)}
48 */
Alexey Dobriyana3aa1882010-01-07 11:58:11 +000049static DEFINE_PCI_DEVICE_TABLE(e1000_pci_tbl) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 INTEL_E1000_ETHERNET_DEVICE(0x1000),
51 INTEL_E1000_ETHERNET_DEVICE(0x1001),
52 INTEL_E1000_ETHERNET_DEVICE(0x1004),
53 INTEL_E1000_ETHERNET_DEVICE(0x1008),
54 INTEL_E1000_ETHERNET_DEVICE(0x1009),
55 INTEL_E1000_ETHERNET_DEVICE(0x100C),
56 INTEL_E1000_ETHERNET_DEVICE(0x100D),
57 INTEL_E1000_ETHERNET_DEVICE(0x100E),
58 INTEL_E1000_ETHERNET_DEVICE(0x100F),
59 INTEL_E1000_ETHERNET_DEVICE(0x1010),
60 INTEL_E1000_ETHERNET_DEVICE(0x1011),
61 INTEL_E1000_ETHERNET_DEVICE(0x1012),
62 INTEL_E1000_ETHERNET_DEVICE(0x1013),
63 INTEL_E1000_ETHERNET_DEVICE(0x1014),
64 INTEL_E1000_ETHERNET_DEVICE(0x1015),
65 INTEL_E1000_ETHERNET_DEVICE(0x1016),
66 INTEL_E1000_ETHERNET_DEVICE(0x1017),
67 INTEL_E1000_ETHERNET_DEVICE(0x1018),
68 INTEL_E1000_ETHERNET_DEVICE(0x1019),
Malli Chilakala26483452005-04-28 19:44:46 -070069 INTEL_E1000_ETHERNET_DEVICE(0x101A),
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 INTEL_E1000_ETHERNET_DEVICE(0x101D),
71 INTEL_E1000_ETHERNET_DEVICE(0x101E),
72 INTEL_E1000_ETHERNET_DEVICE(0x1026),
73 INTEL_E1000_ETHERNET_DEVICE(0x1027),
74 INTEL_E1000_ETHERNET_DEVICE(0x1028),
75 INTEL_E1000_ETHERNET_DEVICE(0x1075),
76 INTEL_E1000_ETHERNET_DEVICE(0x1076),
77 INTEL_E1000_ETHERNET_DEVICE(0x1077),
78 INTEL_E1000_ETHERNET_DEVICE(0x1078),
79 INTEL_E1000_ETHERNET_DEVICE(0x1079),
80 INTEL_E1000_ETHERNET_DEVICE(0x107A),
81 INTEL_E1000_ETHERNET_DEVICE(0x107B),
82 INTEL_E1000_ETHERNET_DEVICE(0x107C),
83 INTEL_E1000_ETHERNET_DEVICE(0x108A),
Jeff Kirsherb7ee49d2006-01-12 16:51:21 -080084 INTEL_E1000_ETHERNET_DEVICE(0x1099),
Jeff Kirsherb7ee49d2006-01-12 16:51:21 -080085 INTEL_E1000_ETHERNET_DEVICE(0x10B5),
Dirk Brandewie5377a412011-01-06 14:29:54 +000086 INTEL_E1000_ETHERNET_DEVICE(0x2E6E),
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 /* required last entry */
88 {0,}
89};
90
91MODULE_DEVICE_TABLE(pci, e1000_pci_tbl);
92
Nicholas Nunley35574762006-09-27 12:53:34 -070093int e1000_up(struct e1000_adapter *adapter);
94void e1000_down(struct e1000_adapter *adapter);
95void e1000_reinit_locked(struct e1000_adapter *adapter);
96void e1000_reset(struct e1000_adapter *adapter);
Nicholas Nunley35574762006-09-27 12:53:34 -070097int e1000_setup_all_tx_resources(struct e1000_adapter *adapter);
98int e1000_setup_all_rx_resources(struct e1000_adapter *adapter);
99void e1000_free_all_tx_resources(struct e1000_adapter *adapter);
100void e1000_free_all_rx_resources(struct e1000_adapter *adapter);
Adrian Bunk3ad2cc62005-10-30 16:53:34 +0100101static int e1000_setup_tx_resources(struct e1000_adapter *adapter,
Nicholas Nunley35574762006-09-27 12:53:34 -0700102 struct e1000_tx_ring *txdr);
Adrian Bunk3ad2cc62005-10-30 16:53:34 +0100103static int e1000_setup_rx_resources(struct e1000_adapter *adapter,
Nicholas Nunley35574762006-09-27 12:53:34 -0700104 struct e1000_rx_ring *rxdr);
Adrian Bunk3ad2cc62005-10-30 16:53:34 +0100105static void e1000_free_tx_resources(struct e1000_adapter *adapter,
Nicholas Nunley35574762006-09-27 12:53:34 -0700106 struct e1000_tx_ring *tx_ring);
Adrian Bunk3ad2cc62005-10-30 16:53:34 +0100107static void e1000_free_rx_resources(struct e1000_adapter *adapter,
Nicholas Nunley35574762006-09-27 12:53:34 -0700108 struct e1000_rx_ring *rx_ring);
109void e1000_update_stats(struct e1000_adapter *adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111static int e1000_init_module(void);
112static void e1000_exit_module(void);
113static int e1000_probe(struct pci_dev *pdev, const struct pci_device_id *ent);
114static void __devexit e1000_remove(struct pci_dev *pdev);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -0400115static int e1000_alloc_queues(struct e1000_adapter *adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116static int e1000_sw_init(struct e1000_adapter *adapter);
117static int e1000_open(struct net_device *netdev);
118static int e1000_close(struct net_device *netdev);
119static void e1000_configure_tx(struct e1000_adapter *adapter);
120static void e1000_configure_rx(struct e1000_adapter *adapter);
121static void e1000_setup_rctl(struct e1000_adapter *adapter);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -0400122static void e1000_clean_all_tx_rings(struct e1000_adapter *adapter);
123static void e1000_clean_all_rx_rings(struct e1000_adapter *adapter);
124static void e1000_clean_tx_ring(struct e1000_adapter *adapter,
125 struct e1000_tx_ring *tx_ring);
126static void e1000_clean_rx_ring(struct e1000_adapter *adapter,
127 struct e1000_rx_ring *rx_ring);
Patrick McHardydb0ce502007-11-13 20:54:59 -0800128static void e1000_set_rx_mode(struct net_device *netdev);
Jesse Brandeburg5cf42fc2010-09-22 18:22:17 +0000129static void e1000_update_phy_info_task(struct work_struct *work);
Jesse Brandeburga4010af2011-10-05 07:24:41 +0000130static void e1000_watchdog(struct work_struct *work);
Jesse Brandeburg5cf42fc2010-09-22 18:22:17 +0000131static void e1000_82547_tx_fifo_stall_task(struct work_struct *work);
Stephen Hemminger3b29a562009-08-31 19:50:55 +0000132static netdev_tx_t e1000_xmit_frame(struct sk_buff *skb,
133 struct net_device *netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134static struct net_device_stats * e1000_get_stats(struct net_device *netdev);
135static int e1000_change_mtu(struct net_device *netdev, int new_mtu);
136static int e1000_set_mac(struct net_device *netdev, void *p);
David Howells7d12e782006-10-05 14:55:46 +0100137static irqreturn_t e1000_intr(int irq, void *data);
Joe Perchesc3033b02008-03-21 11:06:25 -0700138static bool e1000_clean_tx_irq(struct e1000_adapter *adapter,
139 struct e1000_tx_ring *tx_ring);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700140static int e1000_clean(struct napi_struct *napi, int budget);
Joe Perchesc3033b02008-03-21 11:06:25 -0700141static bool e1000_clean_rx_irq(struct e1000_adapter *adapter,
142 struct e1000_rx_ring *rx_ring,
143 int *work_done, int work_to_do);
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +0000144static bool e1000_clean_jumbo_rx_irq(struct e1000_adapter *adapter,
145 struct e1000_rx_ring *rx_ring,
146 int *work_done, int work_to_do);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -0400147static void e1000_alloc_rx_buffers(struct e1000_adapter *adapter,
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +0000148 struct e1000_rx_ring *rx_ring,
Jeff Kirsher72d64a42006-01-12 16:51:19 -0800149 int cleaned_count);
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +0000150static void e1000_alloc_jumbo_rx_buffers(struct e1000_adapter *adapter,
151 struct e1000_rx_ring *rx_ring,
152 int cleaned_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153static int e1000_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd);
154static int e1000_mii_ioctl(struct net_device *netdev, struct ifreq *ifr,
155 int cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156static void e1000_enter_82542_rst(struct e1000_adapter *adapter);
157static void e1000_leave_82542_rst(struct e1000_adapter *adapter);
158static void e1000_tx_timeout(struct net_device *dev);
David Howells65f27f32006-11-22 14:55:48 +0000159static void e1000_reset_task(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160static void e1000_smartspeed(struct e1000_adapter *adapter);
Auke Koke619d522006-04-14 19:04:52 -0700161static int e1000_82547_fifo_workaround(struct e1000_adapter *adapter,
162 struct sk_buff *skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Jiri Pirko5622e402011-07-21 03:26:31 +0000164static bool e1000_vlan_used(struct e1000_adapter *adapter);
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000165static void e1000_vlan_mode(struct net_device *netdev,
166 netdev_features_t features);
Jiri Pirko8e586132011-12-08 19:52:37 -0500167static int e1000_vlan_rx_add_vid(struct net_device *netdev, u16 vid);
168static int e1000_vlan_rx_kill_vid(struct net_device *netdev, u16 vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169static void e1000_restore_vlan(struct e1000_adapter *adapter);
170
Auke Kok6fdfef12006-06-27 09:06:36 -0700171#ifdef CONFIG_PM
Rafael J. Wysockib43fcd72009-04-15 17:43:24 +0000172static int e1000_suspend(struct pci_dev *pdev, pm_message_t state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173static int e1000_resume(struct pci_dev *pdev);
174#endif
Auke Kokc653e632006-05-23 13:35:57 -0700175static void e1000_shutdown(struct pci_dev *pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177#ifdef CONFIG_NET_POLL_CONTROLLER
178/* for netdump / net console */
179static void e1000_netpoll (struct net_device *netdev);
180#endif
181
Jesse Brandeburg1f753862006-12-15 10:40:39 +0100182#define COPYBREAK_DEFAULT 256
183static unsigned int copybreak __read_mostly = COPYBREAK_DEFAULT;
184module_param(copybreak, uint, 0644);
185MODULE_PARM_DESC(copybreak,
186 "Maximum size of packet that is copied to a new buffer on receive");
187
Auke Kok90267292006-06-08 09:30:24 -0700188static pci_ers_result_t e1000_io_error_detected(struct pci_dev *pdev,
189 pci_channel_state_t state);
190static pci_ers_result_t e1000_io_slot_reset(struct pci_dev *pdev);
191static void e1000_io_resume(struct pci_dev *pdev);
192
193static struct pci_error_handlers e1000_err_handler = {
194 .error_detected = e1000_io_error_detected,
195 .slot_reset = e1000_io_slot_reset,
196 .resume = e1000_io_resume,
197};
Mallikarjuna R Chilakala24025e42005-10-04 07:03:23 -0400198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199static struct pci_driver e1000_driver = {
200 .name = e1000_driver_name,
201 .id_table = e1000_pci_tbl,
202 .probe = e1000_probe,
203 .remove = __devexit_p(e1000_remove),
Auke Kokc4e24f02006-09-27 12:53:19 -0700204#ifdef CONFIG_PM
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300205 /* Power Management Hooks */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 .suspend = e1000_suspend,
Auke Kokc653e632006-05-23 13:35:57 -0700207 .resume = e1000_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208#endif
Auke Kok90267292006-06-08 09:30:24 -0700209 .shutdown = e1000_shutdown,
210 .err_handler = &e1000_err_handler
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211};
212
213MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
214MODULE_DESCRIPTION("Intel(R) PRO/1000 Network Driver");
215MODULE_LICENSE("GPL");
216MODULE_VERSION(DRV_VERSION);
217
218static int debug = NETIF_MSG_DRV | NETIF_MSG_PROBE;
219module_param(debug, int, 0);
220MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
221
222/**
Emil Tantilov675ad472010-04-27 14:02:58 +0000223 * e1000_get_hw_dev - return device
224 * used by hardware layer to print debugging information
225 *
226 **/
227struct net_device *e1000_get_hw_dev(struct e1000_hw *hw)
228{
229 struct e1000_adapter *adapter = hw->back;
230 return adapter->netdev;
231}
232
233/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 * e1000_init_module - Driver Registration Routine
235 *
236 * e1000_init_module is the first routine called when the driver is
237 * loaded. All it does is register with the PCI subsystem.
238 **/
239
Joe Perches64798842008-07-11 15:17:02 -0700240static int __init e1000_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
242 int ret;
Emil Tantilov675ad472010-04-27 14:02:58 +0000243 pr_info("%s - version %s\n", e1000_driver_string, e1000_driver_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Emil Tantilov675ad472010-04-27 14:02:58 +0000245 pr_info("%s\n", e1000_copyright);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Jeff Garzik29917622006-08-19 17:48:59 -0400247 ret = pci_register_driver(&e1000_driver);
Jesse Brandeburg1f753862006-12-15 10:40:39 +0100248 if (copybreak != COPYBREAK_DEFAULT) {
249 if (copybreak == 0)
Emil Tantilov675ad472010-04-27 14:02:58 +0000250 pr_info("copybreak disabled\n");
Jesse Brandeburg1f753862006-12-15 10:40:39 +0100251 else
Emil Tantilov675ad472010-04-27 14:02:58 +0000252 pr_info("copybreak enabled for "
253 "packets <= %u bytes\n", copybreak);
Jesse Brandeburg1f753862006-12-15 10:40:39 +0100254 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 return ret;
256}
257
258module_init(e1000_init_module);
259
260/**
261 * e1000_exit_module - Driver Exit Cleanup Routine
262 *
263 * e1000_exit_module is called just before the driver is removed
264 * from memory.
265 **/
266
Joe Perches64798842008-07-11 15:17:02 -0700267static void __exit e1000_exit_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 pci_unregister_driver(&e1000_driver);
270}
271
272module_exit(e1000_exit_module);
273
Auke Kok2db10a02006-06-27 09:06:28 -0700274static int e1000_request_irq(struct e1000_adapter *adapter)
275{
276 struct net_device *netdev = adapter->netdev;
Al Viro3e188262007-12-11 19:49:39 +0000277 irq_handler_t handler = e1000_intr;
Auke Koke94bd232007-05-16 01:49:46 -0700278 int irq_flags = IRQF_SHARED;
279 int err;
Auke Kok2db10a02006-06-27 09:06:28 -0700280
Auke Koke94bd232007-05-16 01:49:46 -0700281 err = request_irq(adapter->pdev->irq, handler, irq_flags, netdev->name,
282 netdev);
283 if (err) {
Emil Tantilovfeb8f472010-07-26 23:37:21 -0700284 e_err(probe, "Unable to allocate interrupt Error: %d\n", err);
Auke Koke94bd232007-05-16 01:49:46 -0700285 }
Auke Kok2db10a02006-06-27 09:06:28 -0700286
287 return err;
288}
289
290static void e1000_free_irq(struct e1000_adapter *adapter)
291{
292 struct net_device *netdev = adapter->netdev;
293
294 free_irq(adapter->pdev->irq, netdev);
Auke Kok2db10a02006-06-27 09:06:28 -0700295}
296
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297/**
298 * e1000_irq_disable - Mask off interrupt generation on the NIC
299 * @adapter: board private structure
300 **/
301
Joe Perches64798842008-07-11 15:17:02 -0700302static void e1000_irq_disable(struct e1000_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
Joe Perches1dc32912008-07-11 15:17:08 -0700304 struct e1000_hw *hw = &adapter->hw;
305
306 ew32(IMC, ~0);
307 E1000_WRITE_FLUSH();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 synchronize_irq(adapter->pdev->irq);
309}
310
311/**
312 * e1000_irq_enable - Enable default interrupt generation settings
313 * @adapter: board private structure
314 **/
315
Joe Perches64798842008-07-11 15:17:02 -0700316static void e1000_irq_enable(struct e1000_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317{
Joe Perches1dc32912008-07-11 15:17:08 -0700318 struct e1000_hw *hw = &adapter->hw;
319
320 ew32(IMS, IMS_ENABLE_MASK);
321 E1000_WRITE_FLUSH();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322}
Adrian Bunk3ad2cc62005-10-30 16:53:34 +0100323
Joe Perches64798842008-07-11 15:17:02 -0700324static void e1000_update_mng_vlan(struct e1000_adapter *adapter)
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700325{
Joe Perches1dc32912008-07-11 15:17:08 -0700326 struct e1000_hw *hw = &adapter->hw;
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700327 struct net_device *netdev = adapter->netdev;
Joe Perches1dc32912008-07-11 15:17:08 -0700328 u16 vid = hw->mng_cookie.vlan_id;
Joe Perches406874a2008-04-03 10:06:32 -0700329 u16 old_vid = adapter->mng_vlan_id;
Jesse Brandeburg96838a42006-01-18 13:01:39 -0800330
Jiri Pirko5622e402011-07-21 03:26:31 +0000331 if (!e1000_vlan_used(adapter))
332 return;
333
334 if (!test_bit(vid, adapter->active_vlans)) {
335 if (hw->mng_cookie.status &
336 E1000_MNG_DHCP_COOKIE_STATUS_VLAN_SUPPORT) {
337 e1000_vlan_rx_add_vid(netdev, vid);
Jeff Kirsherc5f226f2006-03-02 18:17:55 -0800338 adapter->mng_vlan_id = vid;
Jiri Pirko5622e402011-07-21 03:26:31 +0000339 } else {
340 adapter->mng_vlan_id = E1000_MNG_VLAN_NONE;
341 }
342 if ((old_vid != (u16)E1000_MNG_VLAN_NONE) &&
343 (vid != old_vid) &&
344 !test_bit(old_vid, adapter->active_vlans))
345 e1000_vlan_rx_kill_vid(netdev, old_vid);
346 } else {
347 adapter->mng_vlan_id = vid;
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700348 }
349}
Jeff Kirsherb55ccb32006-01-12 16:50:30 -0800350
Joe Perches64798842008-07-11 15:17:02 -0700351static void e1000_init_manageability(struct e1000_adapter *adapter)
Jeff Garzik0fccd0e2006-12-15 10:56:10 -0500352{
Joe Perches1dc32912008-07-11 15:17:08 -0700353 struct e1000_hw *hw = &adapter->hw;
354
Jeff Garzik0fccd0e2006-12-15 10:56:10 -0500355 if (adapter->en_mng_pt) {
Joe Perches1dc32912008-07-11 15:17:08 -0700356 u32 manc = er32(MANC);
Jeff Garzik0fccd0e2006-12-15 10:56:10 -0500357
358 /* disable hardware interception of ARP */
359 manc &= ~(E1000_MANC_ARP_EN);
360
Joe Perches1dc32912008-07-11 15:17:08 -0700361 ew32(MANC, manc);
Jeff Garzik0fccd0e2006-12-15 10:56:10 -0500362 }
363}
364
Joe Perches64798842008-07-11 15:17:02 -0700365static void e1000_release_manageability(struct e1000_adapter *adapter)
Jeff Garzik0fccd0e2006-12-15 10:56:10 -0500366{
Joe Perches1dc32912008-07-11 15:17:08 -0700367 struct e1000_hw *hw = &adapter->hw;
368
Jeff Garzik0fccd0e2006-12-15 10:56:10 -0500369 if (adapter->en_mng_pt) {
Joe Perches1dc32912008-07-11 15:17:08 -0700370 u32 manc = er32(MANC);
Jeff Garzik0fccd0e2006-12-15 10:56:10 -0500371
372 /* re-enable hardware interception of ARP */
373 manc |= E1000_MANC_ARP_EN;
374
Joe Perches1dc32912008-07-11 15:17:08 -0700375 ew32(MANC, manc);
Jeff Garzik0fccd0e2006-12-15 10:56:10 -0500376 }
377}
378
Auke Koke0aac5a2007-03-06 08:57:21 -0800379/**
380 * e1000_configure - configure the hardware for RX and TX
381 * @adapter = private board structure
382 **/
383static void e1000_configure(struct e1000_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
385 struct net_device *netdev = adapter->netdev;
Auke Kok2db10a02006-06-27 09:06:28 -0700386 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Patrick McHardydb0ce502007-11-13 20:54:59 -0800388 e1000_set_rx_mode(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
390 e1000_restore_vlan(adapter);
Jeff Garzik0fccd0e2006-12-15 10:56:10 -0500391 e1000_init_manageability(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393 e1000_configure_tx(adapter);
394 e1000_setup_rctl(adapter);
395 e1000_configure_rx(adapter);
Jeff Kirsher72d64a42006-01-12 16:51:19 -0800396 /* call E1000_DESC_UNUSED which always leaves
397 * at least 1 descriptor unused to make sure
398 * next_to_use != next_to_clean */
Jeff Kirsherf56799e2006-01-12 16:50:39 -0800399 for (i = 0; i < adapter->num_rx_queues; i++) {
Jeff Kirsher72d64a42006-01-12 16:51:19 -0800400 struct e1000_rx_ring *ring = &adapter->rx_ring[i];
Jeff Kirshera292ca62006-01-12 16:51:30 -0800401 adapter->alloc_rx_buf(adapter, ring,
402 E1000_DESC_UNUSED(ring));
Jeff Kirsherf56799e2006-01-12 16:50:39 -0800403 }
Auke Koke0aac5a2007-03-06 08:57:21 -0800404}
Jeff Kirsher7bfa4812006-01-12 16:50:41 -0800405
Auke Koke0aac5a2007-03-06 08:57:21 -0800406int e1000_up(struct e1000_adapter *adapter)
407{
Joe Perches1dc32912008-07-11 15:17:08 -0700408 struct e1000_hw *hw = &adapter->hw;
409
Auke Koke0aac5a2007-03-06 08:57:21 -0800410 /* hardware has been reset, we need to reload some things */
411 e1000_configure(adapter);
Malli Chilakala5de55622005-04-28 19:39:30 -0700412
Auke Kok1314bbf2006-09-27 12:54:02 -0700413 clear_bit(__E1000_DOWN, &adapter->flags);
414
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700415 napi_enable(&adapter->napi);
Francois Romieuc3570ac2008-07-11 15:17:38 -0700416
Auke Koke0aac5a2007-03-06 08:57:21 -0800417 e1000_irq_enable(adapter);
418
Jesse Brandeburg4cb9be72009-04-21 18:42:05 +0000419 netif_wake_queue(adapter->netdev);
420
Jesse Brandeburg79f3d392006-12-15 10:42:34 +0100421 /* fire a link change interrupt to start the watchdog */
Joe Perches1dc32912008-07-11 15:17:08 -0700422 ew32(ICS, E1000_ICS_LSC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 return 0;
424}
425
Auke Kok79f05bf2006-06-27 09:06:32 -0700426/**
427 * e1000_power_up_phy - restore link in case the phy was powered down
428 * @adapter: address of board private structure
429 *
430 * The phy may be powered down to save power and turn off link when the
431 * driver is unloaded and wake on lan is not enabled (among others)
432 * *** this routine MUST be followed by a call to e1000_reset ***
433 *
434 **/
435
Jesse Brandeburgd6582662006-08-16 13:31:33 -0700436void e1000_power_up_phy(struct e1000_adapter *adapter)
Auke Kok79f05bf2006-06-27 09:06:32 -0700437{
Joe Perches1dc32912008-07-11 15:17:08 -0700438 struct e1000_hw *hw = &adapter->hw;
Joe Perches406874a2008-04-03 10:06:32 -0700439 u16 mii_reg = 0;
Auke Kok79f05bf2006-06-27 09:06:32 -0700440
441 /* Just clear the power down bit to wake the phy back up */
Joe Perches1dc32912008-07-11 15:17:08 -0700442 if (hw->media_type == e1000_media_type_copper) {
Auke Kok79f05bf2006-06-27 09:06:32 -0700443 /* according to the manual, the phy will retain its
444 * settings across a power-down/up cycle */
Joe Perches1dc32912008-07-11 15:17:08 -0700445 e1000_read_phy_reg(hw, PHY_CTRL, &mii_reg);
Auke Kok79f05bf2006-06-27 09:06:32 -0700446 mii_reg &= ~MII_CR_POWER_DOWN;
Joe Perches1dc32912008-07-11 15:17:08 -0700447 e1000_write_phy_reg(hw, PHY_CTRL, mii_reg);
Auke Kok79f05bf2006-06-27 09:06:32 -0700448 }
449}
450
451static void e1000_power_down_phy(struct e1000_adapter *adapter)
452{
Joe Perches1dc32912008-07-11 15:17:08 -0700453 struct e1000_hw *hw = &adapter->hw;
454
Bruce Allan61c25052006-09-27 12:53:54 -0700455 /* Power down the PHY so no link is implied when interface is down *
Joe Perchesc3033b02008-03-21 11:06:25 -0700456 * The PHY cannot be powered down if any of the following is true *
Auke Kok79f05bf2006-06-27 09:06:32 -0700457 * (a) WoL is enabled
458 * (b) AMT is active
459 * (c) SoL/IDER session is active */
Joe Perches1dc32912008-07-11 15:17:08 -0700460 if (!adapter->wol && hw->mac_type >= e1000_82540 &&
461 hw->media_type == e1000_media_type_copper) {
Joe Perches406874a2008-04-03 10:06:32 -0700462 u16 mii_reg = 0;
Bruce Allan61c25052006-09-27 12:53:54 -0700463
Joe Perches1dc32912008-07-11 15:17:08 -0700464 switch (hw->mac_type) {
Bruce Allan61c25052006-09-27 12:53:54 -0700465 case e1000_82540:
466 case e1000_82545:
467 case e1000_82545_rev_3:
468 case e1000_82546:
Dirk Brandewie5377a412011-01-06 14:29:54 +0000469 case e1000_ce4100:
Bruce Allan61c25052006-09-27 12:53:54 -0700470 case e1000_82546_rev_3:
471 case e1000_82541:
472 case e1000_82541_rev_2:
473 case e1000_82547:
474 case e1000_82547_rev_2:
Joe Perches1dc32912008-07-11 15:17:08 -0700475 if (er32(MANC) & E1000_MANC_SMBUS_EN)
Bruce Allan61c25052006-09-27 12:53:54 -0700476 goto out;
477 break;
Bruce Allan61c25052006-09-27 12:53:54 -0700478 default:
479 goto out;
480 }
Joe Perches1dc32912008-07-11 15:17:08 -0700481 e1000_read_phy_reg(hw, PHY_CTRL, &mii_reg);
Auke Kok79f05bf2006-06-27 09:06:32 -0700482 mii_reg |= MII_CR_POWER_DOWN;
Joe Perches1dc32912008-07-11 15:17:08 -0700483 e1000_write_phy_reg(hw, PHY_CTRL, mii_reg);
Jesse Brandeburg4e0d8f72011-10-05 07:24:46 +0000484 msleep(1);
Auke Kok79f05bf2006-06-27 09:06:32 -0700485 }
Bruce Allan61c25052006-09-27 12:53:54 -0700486out:
487 return;
Auke Kok79f05bf2006-06-27 09:06:32 -0700488}
489
Jesse Brandeburga4010af2011-10-05 07:24:41 +0000490static void e1000_down_and_stop(struct e1000_adapter *adapter)
491{
492 set_bit(__E1000_DOWN, &adapter->flags);
493 cancel_work_sync(&adapter->reset_task);
494 cancel_delayed_work_sync(&adapter->watchdog_task);
495 cancel_delayed_work_sync(&adapter->phy_info_task);
496 cancel_delayed_work_sync(&adapter->fifo_stall_task);
497}
498
Joe Perches64798842008-07-11 15:17:02 -0700499void e1000_down(struct e1000_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
Jesse Brandeburga6c42322009-03-25 21:59:22 +0000501 struct e1000_hw *hw = &adapter->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 struct net_device *netdev = adapter->netdev;
Jesse Brandeburga6c42322009-03-25 21:59:22 +0000503 u32 rctl, tctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Auke Kok1314bbf2006-09-27 12:54:02 -0700505
Jesse Brandeburga6c42322009-03-25 21:59:22 +0000506 /* disable receives in the hardware */
507 rctl = er32(RCTL);
508 ew32(RCTL, rctl & ~E1000_RCTL_EN);
509 /* flush and sleep below */
510
Jesse Brandeburg51851072009-09-25 12:17:01 +0000511 netif_tx_disable(netdev);
Jesse Brandeburga6c42322009-03-25 21:59:22 +0000512
513 /* disable transmits in the hardware */
514 tctl = er32(TCTL);
515 tctl &= ~E1000_TCTL_EN;
516 ew32(TCTL, tctl);
517 /* flush both disables and wait for them to finish */
518 E1000_WRITE_FLUSH();
519 msleep(10);
520
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700521 napi_disable(&adapter->napi);
Francois Romieuc3570ac2008-07-11 15:17:38 -0700522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 e1000_irq_disable(adapter);
Jeff Kirsherc1605eb2006-03-02 18:16:38 -0800524
Anupam Chandaab088532010-11-21 09:54:21 -0800525 /*
526 * Setting DOWN must be after irq_disable to prevent
527 * a screaming interrupt. Setting DOWN also prevents
Jesse Brandeburga4010af2011-10-05 07:24:41 +0000528 * tasks from rescheduling.
Anupam Chandaab088532010-11-21 09:54:21 -0800529 */
Jesse Brandeburga4010af2011-10-05 07:24:41 +0000530 e1000_down_and_stop(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 adapter->link_speed = 0;
533 adapter->link_duplex = 0;
534 netif_carrier_off(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
536 e1000_reset(adapter);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -0400537 e1000_clean_all_tx_rings(adapter);
538 e1000_clean_all_rx_rings(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
stephen hemminger38df7a32010-10-21 07:50:57 +0000541static void e1000_reinit_safe(struct e1000_adapter *adapter)
Jesse Brandeburg338c15e2010-09-22 18:22:42 +0000542{
543 while (test_and_set_bit(__E1000_RESETTING, &adapter->flags))
544 msleep(1);
Jesse Brandeburg0ef4eed2011-10-05 07:24:51 +0000545 mutex_lock(&adapter->mutex);
Jesse Brandeburg338c15e2010-09-22 18:22:42 +0000546 e1000_down(adapter);
547 e1000_up(adapter);
Jesse Brandeburg0ef4eed2011-10-05 07:24:51 +0000548 mutex_unlock(&adapter->mutex);
Jesse Brandeburg338c15e2010-09-22 18:22:42 +0000549 clear_bit(__E1000_RESETTING, &adapter->flags);
550}
551
Joe Perches64798842008-07-11 15:17:02 -0700552void e1000_reinit_locked(struct e1000_adapter *adapter)
Auke Kok2db10a02006-06-27 09:06:28 -0700553{
Jesse Brandeburg338c15e2010-09-22 18:22:42 +0000554 /* if rtnl_lock is not held the call path is bogus */
555 ASSERT_RTNL();
Auke Kok2db10a02006-06-27 09:06:28 -0700556 WARN_ON(in_interrupt());
557 while (test_and_set_bit(__E1000_RESETTING, &adapter->flags))
558 msleep(1);
559 e1000_down(adapter);
560 e1000_up(adapter);
561 clear_bit(__E1000_RESETTING, &adapter->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562}
563
Joe Perches64798842008-07-11 15:17:02 -0700564void e1000_reset(struct e1000_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565{
Joe Perches1dc32912008-07-11 15:17:08 -0700566 struct e1000_hw *hw = &adapter->hw;
Joe Perches406874a2008-04-03 10:06:32 -0700567 u32 pba = 0, tx_space, min_tx_space, min_rx_space;
Joe Perchesc3033b02008-03-21 11:06:25 -0700568 bool legacy_pba_adjust = false;
Jesse Brandeburgb7cb8c22009-07-06 10:45:01 +0000569 u16 hwm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
571 /* Repartition Pba for greater than 9k mtu
572 * To take effect CTRL.RST is required.
573 */
574
Joe Perches1dc32912008-07-11 15:17:08 -0700575 switch (hw->mac_type) {
Bruce Allan018ea442006-12-15 10:39:45 +0100576 case e1000_82542_rev2_0:
577 case e1000_82542_rev2_1:
578 case e1000_82543:
579 case e1000_82544:
580 case e1000_82540:
581 case e1000_82541:
582 case e1000_82541_rev_2:
Joe Perchesc3033b02008-03-21 11:06:25 -0700583 legacy_pba_adjust = true;
Bruce Allan018ea442006-12-15 10:39:45 +0100584 pba = E1000_PBA_48K;
585 break;
586 case e1000_82545:
587 case e1000_82545_rev_3:
588 case e1000_82546:
Dirk Brandewie5377a412011-01-06 14:29:54 +0000589 case e1000_ce4100:
Bruce Allan018ea442006-12-15 10:39:45 +0100590 case e1000_82546_rev_3:
591 pba = E1000_PBA_48K;
592 break;
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700593 case e1000_82547:
Malli Chilakala0e6ef3e2005-04-28 19:44:14 -0700594 case e1000_82547_rev_2:
Joe Perchesc3033b02008-03-21 11:06:25 -0700595 legacy_pba_adjust = true;
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700596 pba = E1000_PBA_30K;
597 break;
Bruce Allan018ea442006-12-15 10:39:45 +0100598 case e1000_undefined:
599 case e1000_num_macs:
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700600 break;
601 }
602
Joe Perchesc3033b02008-03-21 11:06:25 -0700603 if (legacy_pba_adjust) {
Jesse Brandeburgb7cb8c22009-07-06 10:45:01 +0000604 if (hw->max_frame_size > E1000_RXBUFFER_8192)
Bruce Allan018ea442006-12-15 10:39:45 +0100605 pba -= 8; /* allocate more FIFO for Tx */
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700606
Joe Perches1dc32912008-07-11 15:17:08 -0700607 if (hw->mac_type == e1000_82547) {
Bruce Allan018ea442006-12-15 10:39:45 +0100608 adapter->tx_fifo_head = 0;
609 adapter->tx_head_addr = pba << E1000_TX_HEAD_ADDR_SHIFT;
610 adapter->tx_fifo_size =
611 (E1000_PBA_40K - pba) << E1000_PBA_BYTES_SHIFT;
612 atomic_set(&adapter->tx_fifo_stall, 0);
613 }
Jesse Brandeburgb7cb8c22009-07-06 10:45:01 +0000614 } else if (hw->max_frame_size > ETH_FRAME_LEN + ETH_FCS_LEN) {
Bruce Allan018ea442006-12-15 10:39:45 +0100615 /* adjust PBA for jumbo frames */
Joe Perches1dc32912008-07-11 15:17:08 -0700616 ew32(PBA, pba);
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700617
Bruce Allan018ea442006-12-15 10:39:45 +0100618 /* To maintain wire speed transmits, the Tx FIFO should be
Jesse Brandeburgb7cb8c22009-07-06 10:45:01 +0000619 * large enough to accommodate two full transmit packets,
Bruce Allan018ea442006-12-15 10:39:45 +0100620 * rounded up to the next 1KB and expressed in KB. Likewise,
Jesse Brandeburgb7cb8c22009-07-06 10:45:01 +0000621 * the Rx FIFO should be large enough to accommodate at least
Bruce Allan018ea442006-12-15 10:39:45 +0100622 * one full receive packet and is similarly rounded up and
623 * expressed in KB. */
Joe Perches1dc32912008-07-11 15:17:08 -0700624 pba = er32(PBA);
Bruce Allan018ea442006-12-15 10:39:45 +0100625 /* upper 16 bits has Tx packet buffer allocation size in KB */
626 tx_space = pba >> 16;
627 /* lower 16 bits has Rx packet buffer allocation size in KB */
628 pba &= 0xffff;
Jesse Brandeburgb7cb8c22009-07-06 10:45:01 +0000629 /*
630 * the tx fifo also stores 16 bytes of information about the tx
631 * but don't include ethernet FCS because hardware appends it
632 */
633 min_tx_space = (hw->max_frame_size +
634 sizeof(struct e1000_tx_desc) -
635 ETH_FCS_LEN) * 2;
Milind Arun Choudhary9099cfb2007-04-27 13:55:29 -0700636 min_tx_space = ALIGN(min_tx_space, 1024);
Bruce Allan018ea442006-12-15 10:39:45 +0100637 min_tx_space >>= 10;
Jesse Brandeburgb7cb8c22009-07-06 10:45:01 +0000638 /* software strips receive CRC, so leave room for it */
639 min_rx_space = hw->max_frame_size;
Milind Arun Choudhary9099cfb2007-04-27 13:55:29 -0700640 min_rx_space = ALIGN(min_rx_space, 1024);
Bruce Allan018ea442006-12-15 10:39:45 +0100641 min_rx_space >>= 10;
642
643 /* If current Tx allocation is less than the min Tx FIFO size,
644 * and the min Tx FIFO size is less than the current Rx FIFO
645 * allocation, take space away from current Rx allocation */
646 if (tx_space < min_tx_space &&
647 ((min_tx_space - tx_space) < pba)) {
648 pba = pba - (min_tx_space - tx_space);
649
650 /* PCI/PCIx hardware has PBA alignment constraints */
Joe Perches1dc32912008-07-11 15:17:08 -0700651 switch (hw->mac_type) {
Bruce Allan018ea442006-12-15 10:39:45 +0100652 case e1000_82545 ... e1000_82546_rev_3:
653 pba &= ~(E1000_PBA_8K - 1);
654 break;
655 default:
656 break;
657 }
658
659 /* if short on rx space, rx wins and must trump tx
660 * adjustment or use Early Receive if available */
Jesse Brandeburg1532ece2009-09-25 12:16:14 +0000661 if (pba < min_rx_space)
662 pba = min_rx_space;
Bruce Allan018ea442006-12-15 10:39:45 +0100663 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 }
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700665
Joe Perches1dc32912008-07-11 15:17:08 -0700666 ew32(PBA, pba);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Jesse Brandeburgb7cb8c22009-07-06 10:45:01 +0000668 /*
669 * flow control settings:
670 * The high water mark must be low enough to fit one full frame
671 * (or the size used for early receive) above it in the Rx FIFO.
672 * Set it to the lower of:
673 * - 90% of the Rx FIFO size, and
674 * - the full Rx FIFO size minus the early receive size (for parts
675 * with ERT support assuming ERT set to E1000_ERT_2048), or
676 * - the full Rx FIFO size minus one full frame
677 */
678 hwm = min(((pba << 10) * 9 / 10),
679 ((pba << 10) - hw->max_frame_size));
Jeff Kirsherf11b7f82006-01-12 16:50:51 -0800680
Jesse Brandeburgb7cb8c22009-07-06 10:45:01 +0000681 hw->fc_high_water = hwm & 0xFFF8; /* 8-byte granularity */
682 hw->fc_low_water = hw->fc_high_water - 8;
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +0000683 hw->fc_pause_time = E1000_FC_PAUSE_TIME;
Joe Perches1dc32912008-07-11 15:17:08 -0700684 hw->fc_send_xon = 1;
685 hw->fc = hw->original_fc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700687 /* Allow time for pending master requests to run */
Joe Perches1dc32912008-07-11 15:17:08 -0700688 e1000_reset_hw(hw);
689 if (hw->mac_type >= e1000_82544)
690 ew32(WUC, 0);
Jeff Kirsher09ae3e82006-09-27 12:53:51 -0700691
Joe Perches1dc32912008-07-11 15:17:08 -0700692 if (e1000_init_hw(hw))
Emil Tantilovfeb8f472010-07-26 23:37:21 -0700693 e_dev_err("Hardware Error\n");
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700694 e1000_update_mng_vlan(adapter);
Jesse Brandeburg3d5460a2006-12-15 10:33:46 +0100695
696 /* if (adapter->hwflags & HWFLAGS_PHY_PWR_BIT) { */
Joe Perches1dc32912008-07-11 15:17:08 -0700697 if (hw->mac_type >= e1000_82544 &&
Joe Perches1dc32912008-07-11 15:17:08 -0700698 hw->autoneg == 1 &&
699 hw->autoneg_advertised == ADVERTISE_1000_FULL) {
700 u32 ctrl = er32(CTRL);
Jesse Brandeburg3d5460a2006-12-15 10:33:46 +0100701 /* clear phy power management bit if we are in gig only mode,
702 * which if enabled will attempt negotiation to 100Mb, which
703 * can cause a loss of link at power off or driver unload */
704 ctrl &= ~E1000_CTRL_SWDPIN3;
Joe Perches1dc32912008-07-11 15:17:08 -0700705 ew32(CTRL, ctrl);
Jesse Brandeburg3d5460a2006-12-15 10:33:46 +0100706 }
707
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 /* Enable h/w to recognize an 802.1Q VLAN Ethernet packet */
Joe Perches1dc32912008-07-11 15:17:08 -0700709 ew32(VET, ETHERNET_IEEE_VLAN_TYPE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
Joe Perches1dc32912008-07-11 15:17:08 -0700711 e1000_reset_adaptive(hw);
712 e1000_phy_get_info(hw, &adapter->phy_info);
Auke Kok9a53a202006-06-27 09:06:45 -0700713
Jeff Garzik0fccd0e2006-12-15 10:56:10 -0500714 e1000_release_manageability(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715}
716
717/**
Auke Kok67b3c272007-12-17 13:50:23 -0800718 * Dump the eeprom for users having checksum issues
719 **/
Adrian Bunkb4ea8952008-02-01 08:21:28 -0800720static void e1000_dump_eeprom(struct e1000_adapter *adapter)
Auke Kok67b3c272007-12-17 13:50:23 -0800721{
722 struct net_device *netdev = adapter->netdev;
723 struct ethtool_eeprom eeprom;
724 const struct ethtool_ops *ops = netdev->ethtool_ops;
725 u8 *data;
726 int i;
727 u16 csum_old, csum_new = 0;
728
729 eeprom.len = ops->get_eeprom_len(netdev);
730 eeprom.offset = 0;
731
732 data = kmalloc(eeprom.len, GFP_KERNEL);
Joe Perchese404dec2012-01-29 12:56:23 +0000733 if (!data)
Auke Kok67b3c272007-12-17 13:50:23 -0800734 return;
Auke Kok67b3c272007-12-17 13:50:23 -0800735
736 ops->get_eeprom(netdev, &eeprom, data);
737
738 csum_old = (data[EEPROM_CHECKSUM_REG * 2]) +
739 (data[EEPROM_CHECKSUM_REG * 2 + 1] << 8);
740 for (i = 0; i < EEPROM_CHECKSUM_REG * 2; i += 2)
741 csum_new += data[i] + (data[i + 1] << 8);
742 csum_new = EEPROM_SUM - csum_new;
743
Emil Tantilov675ad472010-04-27 14:02:58 +0000744 pr_err("/*********************/\n");
745 pr_err("Current EEPROM Checksum : 0x%04x\n", csum_old);
746 pr_err("Calculated : 0x%04x\n", csum_new);
Auke Kok67b3c272007-12-17 13:50:23 -0800747
Emil Tantilov675ad472010-04-27 14:02:58 +0000748 pr_err("Offset Values\n");
749 pr_err("======== ======\n");
Auke Kok67b3c272007-12-17 13:50:23 -0800750 print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 16, 1, data, 128, 0);
751
Emil Tantilov675ad472010-04-27 14:02:58 +0000752 pr_err("Include this output when contacting your support provider.\n");
753 pr_err("This is not a software error! Something bad happened to\n");
754 pr_err("your hardware or EEPROM image. Ignoring this problem could\n");
755 pr_err("result in further problems, possibly loss of data,\n");
756 pr_err("corruption or system hangs!\n");
757 pr_err("The MAC Address will be reset to 00:00:00:00:00:00,\n");
758 pr_err("which is invalid and requires you to set the proper MAC\n");
759 pr_err("address manually before continuing to enable this network\n");
760 pr_err("device. Please inspect the EEPROM dump and report the\n");
761 pr_err("issue to your hardware vendor or Intel Customer Support.\n");
762 pr_err("/*********************/\n");
Auke Kok67b3c272007-12-17 13:50:23 -0800763
764 kfree(data);
765}
766
767/**
Taku Izumi81250292008-07-11 15:17:44 -0700768 * e1000_is_need_ioport - determine if an adapter needs ioport resources or not
769 * @pdev: PCI device information struct
770 *
771 * Return true if an adapter needs ioport resources
772 **/
773static int e1000_is_need_ioport(struct pci_dev *pdev)
774{
775 switch (pdev->device) {
776 case E1000_DEV_ID_82540EM:
777 case E1000_DEV_ID_82540EM_LOM:
778 case E1000_DEV_ID_82540EP:
779 case E1000_DEV_ID_82540EP_LOM:
780 case E1000_DEV_ID_82540EP_LP:
781 case E1000_DEV_ID_82541EI:
782 case E1000_DEV_ID_82541EI_MOBILE:
783 case E1000_DEV_ID_82541ER:
784 case E1000_DEV_ID_82541ER_LOM:
785 case E1000_DEV_ID_82541GI:
786 case E1000_DEV_ID_82541GI_LF:
787 case E1000_DEV_ID_82541GI_MOBILE:
788 case E1000_DEV_ID_82544EI_COPPER:
789 case E1000_DEV_ID_82544EI_FIBER:
790 case E1000_DEV_ID_82544GC_COPPER:
791 case E1000_DEV_ID_82544GC_LOM:
792 case E1000_DEV_ID_82545EM_COPPER:
793 case E1000_DEV_ID_82545EM_FIBER:
794 case E1000_DEV_ID_82546EB_COPPER:
795 case E1000_DEV_ID_82546EB_FIBER:
796 case E1000_DEV_ID_82546EB_QUAD_COPPER:
797 return true;
798 default:
799 return false;
800 }
801}
802
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000803static netdev_features_t e1000_fix_features(struct net_device *netdev,
804 netdev_features_t features)
Jiri Pirko5622e402011-07-21 03:26:31 +0000805{
806 /*
807 * Since there is no support for separate rx/tx vlan accel
808 * enable/disable make sure tx flag is always in same state as rx.
809 */
810 if (features & NETIF_F_HW_VLAN_RX)
811 features |= NETIF_F_HW_VLAN_TX;
812 else
813 features &= ~NETIF_F_HW_VLAN_TX;
814
815 return features;
816}
817
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000818static int e1000_set_features(struct net_device *netdev,
819 netdev_features_t features)
Michał Mirosławe97d3202011-06-08 08:36:42 +0000820{
821 struct e1000_adapter *adapter = netdev_priv(netdev);
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000822 netdev_features_t changed = features ^ netdev->features;
Michał Mirosławe97d3202011-06-08 08:36:42 +0000823
Jiri Pirko5622e402011-07-21 03:26:31 +0000824 if (changed & NETIF_F_HW_VLAN_RX)
825 e1000_vlan_mode(netdev, features);
826
Michał Mirosławe97d3202011-06-08 08:36:42 +0000827 if (!(changed & NETIF_F_RXCSUM))
828 return 0;
829
830 adapter->rx_csum = !!(features & NETIF_F_RXCSUM);
831
832 if (netif_running(netdev))
833 e1000_reinit_locked(adapter);
834 else
835 e1000_reset(adapter);
836
837 return 0;
838}
839
Stephen Hemminger0e7614b2008-11-19 22:18:22 -0800840static const struct net_device_ops e1000_netdev_ops = {
841 .ndo_open = e1000_open,
842 .ndo_stop = e1000_close,
Stephen Hemminger00829822008-11-20 20:14:53 -0800843 .ndo_start_xmit = e1000_xmit_frame,
Stephen Hemminger0e7614b2008-11-19 22:18:22 -0800844 .ndo_get_stats = e1000_get_stats,
845 .ndo_set_rx_mode = e1000_set_rx_mode,
846 .ndo_set_mac_address = e1000_set_mac,
Jiri Pirko5622e402011-07-21 03:26:31 +0000847 .ndo_tx_timeout = e1000_tx_timeout,
Stephen Hemminger0e7614b2008-11-19 22:18:22 -0800848 .ndo_change_mtu = e1000_change_mtu,
849 .ndo_do_ioctl = e1000_ioctl,
850 .ndo_validate_addr = eth_validate_addr,
Stephen Hemminger0e7614b2008-11-19 22:18:22 -0800851 .ndo_vlan_rx_add_vid = e1000_vlan_rx_add_vid,
852 .ndo_vlan_rx_kill_vid = e1000_vlan_rx_kill_vid,
853#ifdef CONFIG_NET_POLL_CONTROLLER
854 .ndo_poll_controller = e1000_netpoll,
855#endif
Jiri Pirko5622e402011-07-21 03:26:31 +0000856 .ndo_fix_features = e1000_fix_features,
857 .ndo_set_features = e1000_set_features,
Stephen Hemminger0e7614b2008-11-19 22:18:22 -0800858};
859
Taku Izumi81250292008-07-11 15:17:44 -0700860/**
Jesse Brandeburge508be12010-09-07 21:01:12 +0000861 * e1000_init_hw_struct - initialize members of hw struct
862 * @adapter: board private struct
863 * @hw: structure used by e1000_hw.c
864 *
865 * Factors out initialization of the e1000_hw struct to its own function
866 * that can be called very early at init (just after struct allocation).
867 * Fields are initialized based on PCI device information and
868 * OS network device settings (MTU size).
869 * Returns negative error codes if MAC type setup fails.
870 */
871static int e1000_init_hw_struct(struct e1000_adapter *adapter,
872 struct e1000_hw *hw)
873{
874 struct pci_dev *pdev = adapter->pdev;
875
876 /* PCI config space info */
877 hw->vendor_id = pdev->vendor;
878 hw->device_id = pdev->device;
879 hw->subsystem_vendor_id = pdev->subsystem_vendor;
880 hw->subsystem_id = pdev->subsystem_device;
881 hw->revision_id = pdev->revision;
882
883 pci_read_config_word(pdev, PCI_COMMAND, &hw->pci_cmd_word);
884
885 hw->max_frame_size = adapter->netdev->mtu +
886 ENET_HEADER_SIZE + ETHERNET_FCS_SIZE;
887 hw->min_frame_size = MINIMUM_ETHERNET_FRAME_SIZE;
888
889 /* identify the MAC */
890 if (e1000_set_mac_type(hw)) {
891 e_err(probe, "Unknown MAC Type\n");
892 return -EIO;
893 }
894
895 switch (hw->mac_type) {
896 default:
897 break;
898 case e1000_82541:
899 case e1000_82547:
900 case e1000_82541_rev_2:
901 case e1000_82547_rev_2:
902 hw->phy_init_script = 1;
903 break;
904 }
905
906 e1000_set_media_type(hw);
907 e1000_get_bus_info(hw);
908
909 hw->wait_autoneg_complete = false;
910 hw->tbi_compatibility_en = true;
911 hw->adaptive_ifs = true;
912
913 /* Copper options */
914
915 if (hw->media_type == e1000_media_type_copper) {
916 hw->mdix = AUTO_ALL_MODES;
917 hw->disable_polarity_correction = false;
918 hw->master_slave = E1000_MASTER_SLAVE;
919 }
920
921 return 0;
922}
923
924/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 * e1000_probe - Device Initialization Routine
926 * @pdev: PCI device information struct
927 * @ent: entry in e1000_pci_tbl
928 *
929 * Returns 0 on success, negative on failure
930 *
931 * e1000_probe initializes an adapter identified by a pci_dev structure.
932 * The OS initialization, configuring of the adapter private structure,
933 * and a hardware reset occur.
934 **/
Joe Perches1dc32912008-07-11 15:17:08 -0700935static int __devinit e1000_probe(struct pci_dev *pdev,
936 const struct pci_device_id *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937{
938 struct net_device *netdev;
939 struct e1000_adapter *adapter;
Joe Perches1dc32912008-07-11 15:17:08 -0700940 struct e1000_hw *hw;
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700941
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 static int cards_found = 0;
Jesse Brandeburg120cd572006-08-31 14:27:46 -0700943 static int global_quad_port_a = 0; /* global ksp3 port a indication */
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700944 int i, err, pci_using_dac;
Joe Perches406874a2008-04-03 10:06:32 -0700945 u16 eeprom_data = 0;
Dirk Brandewie5377a412011-01-06 14:29:54 +0000946 u16 tmp = 0;
Joe Perches406874a2008-04-03 10:06:32 -0700947 u16 eeprom_apme_mask = E1000_EEPROM_APME;
Taku Izumi81250292008-07-11 15:17:44 -0700948 int bars, need_ioport;
Joe Perches0795af52007-10-03 17:59:30 -0700949
Taku Izumi81250292008-07-11 15:17:44 -0700950 /* do not allocate ioport bars when not needed */
951 need_ioport = e1000_is_need_ioport(pdev);
952 if (need_ioport) {
953 bars = pci_select_bars(pdev, IORESOURCE_MEM | IORESOURCE_IO);
954 err = pci_enable_device(pdev);
955 } else {
956 bars = pci_select_bars(pdev, IORESOURCE_MEM);
Karsten Keil4d7155b2009-02-03 15:18:01 -0800957 err = pci_enable_device_mem(pdev);
Taku Izumi81250292008-07-11 15:17:44 -0700958 }
Joe Perchesc7be73b2008-07-11 15:17:28 -0700959 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 return err;
961
Taku Izumi81250292008-07-11 15:17:44 -0700962 err = pci_request_selected_regions(pdev, bars, e1000_driver_name);
Joe Perchesc7be73b2008-07-11 15:17:28 -0700963 if (err)
Vasily Averin6dd62ab2006-08-28 14:56:22 -0700964 goto err_pci_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
966 pci_set_master(pdev);
Nick Nunleydbb5aae2010-02-03 14:49:48 +0000967 err = pci_save_state(pdev);
968 if (err)
969 goto err_alloc_etherdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
Vasily Averin6dd62ab2006-08-28 14:56:22 -0700971 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 netdev = alloc_etherdev(sizeof(struct e1000_adapter));
Vasily Averin6dd62ab2006-08-28 14:56:22 -0700973 if (!netdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 goto err_alloc_etherdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 SET_NETDEV_DEV(netdev, &pdev->dev);
977
978 pci_set_drvdata(pdev, netdev);
Malli Chilakala60490fe2005-06-17 17:41:45 -0700979 adapter = netdev_priv(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 adapter->netdev = netdev;
981 adapter->pdev = pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 adapter->msg_enable = (1 << debug) - 1;
Taku Izumi81250292008-07-11 15:17:44 -0700983 adapter->bars = bars;
984 adapter->need_ioport = need_ioport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
Joe Perches1dc32912008-07-11 15:17:08 -0700986 hw = &adapter->hw;
987 hw->back = adapter;
988
Vasily Averin6dd62ab2006-08-28 14:56:22 -0700989 err = -EIO;
Arjan van de Ven275f1652008-10-20 21:42:39 -0700990 hw->hw_addr = pci_ioremap_bar(pdev, BAR_0);
Joe Perches1dc32912008-07-11 15:17:08 -0700991 if (!hw->hw_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 goto err_ioremap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
Taku Izumi81250292008-07-11 15:17:44 -0700994 if (adapter->need_ioport) {
995 for (i = BAR_1; i <= BAR_5; i++) {
996 if (pci_resource_len(pdev, i) == 0)
997 continue;
998 if (pci_resource_flags(pdev, i) & IORESOURCE_IO) {
999 hw->io_base = pci_resource_start(pdev, i);
1000 break;
1001 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 }
1003 }
1004
Jesse Brandeburge508be12010-09-07 21:01:12 +00001005 /* make ready for any if (hw->...) below */
1006 err = e1000_init_hw_struct(adapter, hw);
1007 if (err)
1008 goto err_sw_init;
1009
1010 /*
1011 * there is a workaround being applied below that limits
1012 * 64-bit DMA addresses to 64-bit hardware. There are some
1013 * 32-bit adapters that Tx hang when given 64-bit DMA addresses
1014 */
1015 pci_using_dac = 0;
1016 if ((hw->bus_type == e1000_bus_type_pcix) &&
1017 !dma_set_mask(&pdev->dev, DMA_BIT_MASK(64))) {
1018 /*
1019 * according to DMA-API-HOWTO, coherent calls will always
1020 * succeed if the set call did
1021 */
1022 dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(64));
1023 pci_using_dac = 1;
Jesse Brandeburge508be12010-09-07 21:01:12 +00001024 } else {
Dean Nelson19a0b672010-11-11 05:50:25 +00001025 err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
1026 if (err) {
1027 pr_err("No usable DMA config, aborting\n");
1028 goto err_dma;
1029 }
1030 dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
Jesse Brandeburge508be12010-09-07 21:01:12 +00001031 }
1032
Stephen Hemminger0e7614b2008-11-19 22:18:22 -08001033 netdev->netdev_ops = &e1000_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 e1000_set_ethtool_ops(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 netdev->watchdog_timeo = 5 * HZ;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001036 netif_napi_add(netdev, &adapter->napi, e1000_clean, 64);
Stephen Hemminger0e7614b2008-11-19 22:18:22 -08001037
Auke Kok0eb5a342006-09-27 12:53:17 -07001038 strncpy(netdev->name, pci_name(pdev), sizeof(netdev->name) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 adapter->bd_number = cards_found;
1041
1042 /* setup the private structure */
1043
Joe Perchesc7be73b2008-07-11 15:17:28 -07001044 err = e1000_sw_init(adapter);
1045 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 goto err_sw_init;
1047
Vasily Averin6dd62ab2006-08-28 14:56:22 -07001048 err = -EIO;
Dirk Brandewie5377a412011-01-06 14:29:54 +00001049 if (hw->mac_type == e1000_ce4100) {
Florian Fainelli13acde82012-01-04 20:23:35 +00001050 hw->ce4100_gbe_mdio_base_virt =
1051 ioremap(pci_resource_start(pdev, BAR_1),
Dirk Brandewie5377a412011-01-06 14:29:54 +00001052 pci_resource_len(pdev, BAR_1));
1053
Florian Fainelli13acde82012-01-04 20:23:35 +00001054 if (!hw->ce4100_gbe_mdio_base_virt)
Dirk Brandewie5377a412011-01-06 14:29:54 +00001055 goto err_mdio_ioremap;
1056 }
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001057
Joe Perches1dc32912008-07-11 15:17:08 -07001058 if (hw->mac_type >= e1000_82543) {
Michał Mirosławe97d3202011-06-08 08:36:42 +00001059 netdev->hw_features = NETIF_F_SG |
Jiri Pirko5622e402011-07-21 03:26:31 +00001060 NETIF_F_HW_CSUM |
1061 NETIF_F_HW_VLAN_RX;
Michał Mirosławe97d3202011-06-08 08:36:42 +00001062 netdev->features = NETIF_F_HW_VLAN_TX |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 NETIF_F_HW_VLAN_FILTER;
1064 }
1065
Joe Perches1dc32912008-07-11 15:17:08 -07001066 if ((hw->mac_type >= e1000_82544) &&
1067 (hw->mac_type != e1000_82547))
Michał Mirosławe97d3202011-06-08 08:36:42 +00001068 netdev->hw_features |= NETIF_F_TSO;
1069
1070 netdev->features |= netdev->hw_features;
1071 netdev->hw_features |= NETIF_F_RXCSUM;
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001072
Yi Zou7b872a52010-09-22 17:57:58 +00001073 if (pci_using_dac) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 netdev->features |= NETIF_F_HIGHDMA;
Yi Zou7b872a52010-09-22 17:57:58 +00001075 netdev->vlan_features |= NETIF_F_HIGHDMA;
1076 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
Patrick McHardy20501a62008-10-11 12:25:59 -07001078 netdev->vlan_features |= NETIF_F_TSO;
Patrick McHardy20501a62008-10-11 12:25:59 -07001079 netdev->vlan_features |= NETIF_F_HW_CSUM;
1080 netdev->vlan_features |= NETIF_F_SG;
1081
Jiri Pirko01789342011-08-16 06:29:00 +00001082 netdev->priv_flags |= IFF_UNICAST_FLT;
1083
Joe Perches1dc32912008-07-11 15:17:08 -07001084 adapter->en_mng_pt = e1000_enable_mng_pass_thru(hw);
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001085
Auke Kokcd94dd02006-06-27 09:08:22 -07001086 /* initialize eeprom parameters */
Joe Perches1dc32912008-07-11 15:17:08 -07001087 if (e1000_init_eeprom_params(hw)) {
Emil Tantilovfeb8f472010-07-26 23:37:21 -07001088 e_err(probe, "EEPROM initialization failed\n");
Vasily Averin6dd62ab2006-08-28 14:56:22 -07001089 goto err_eeprom;
Auke Kokcd94dd02006-06-27 09:08:22 -07001090 }
1091
Jesse Brandeburg96838a42006-01-18 13:01:39 -08001092 /* before reading the EEPROM, reset the controller to
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 * put the device in a known good starting state */
Jesse Brandeburg96838a42006-01-18 13:01:39 -08001094
Joe Perches1dc32912008-07-11 15:17:08 -07001095 e1000_reset_hw(hw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
1097 /* make sure the EEPROM is good */
Joe Perches1dc32912008-07-11 15:17:08 -07001098 if (e1000_validate_eeprom_checksum(hw) < 0) {
Emil Tantilovfeb8f472010-07-26 23:37:21 -07001099 e_err(probe, "The EEPROM Checksum Is Not Valid\n");
Auke Kok67b3c272007-12-17 13:50:23 -08001100 e1000_dump_eeprom(adapter);
1101 /*
1102 * set MAC address to all zeroes to invalidate and temporary
1103 * disable this device for the user. This blocks regular
1104 * traffic while still permitting ethtool ioctls from reaching
1105 * the hardware as well as allowing the user to run the
1106 * interface after manually setting a hw addr using
1107 * `ip set address`
1108 */
Joe Perches1dc32912008-07-11 15:17:08 -07001109 memset(hw->mac_addr, 0, netdev->addr_len);
Auke Kok67b3c272007-12-17 13:50:23 -08001110 } else {
1111 /* copy the MAC address out of the EEPROM */
Joe Perches1dc32912008-07-11 15:17:08 -07001112 if (e1000_read_mac_addr(hw))
Emil Tantilovfeb8f472010-07-26 23:37:21 -07001113 e_err(probe, "EEPROM Read Error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 }
Auke Kok67b3c272007-12-17 13:50:23 -08001115 /* don't block initalization here due to bad MAC address */
Joe Perches1dc32912008-07-11 15:17:08 -07001116 memcpy(netdev->dev_addr, hw->mac_addr, netdev->addr_len);
1117 memcpy(netdev->perm_addr, hw->mac_addr, netdev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
Auke Kok67b3c272007-12-17 13:50:23 -08001119 if (!is_valid_ether_addr(netdev->perm_addr))
Emil Tantilovfeb8f472010-07-26 23:37:21 -07001120 e_err(probe, "Invalid MAC Address\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
Jesse Brandeburga4010af2011-10-05 07:24:41 +00001123 INIT_DELAYED_WORK(&adapter->watchdog_task, e1000_watchdog);
1124 INIT_DELAYED_WORK(&adapter->fifo_stall_task,
1125 e1000_82547_tx_fifo_stall_task);
1126 INIT_DELAYED_WORK(&adapter->phy_info_task, e1000_update_phy_info_task);
David Howells65f27f32006-11-22 14:55:48 +00001127 INIT_WORK(&adapter->reset_task, e1000_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 e1000_check_options(adapter);
1130
1131 /* Initial Wake on LAN setting
1132 * If APM wake is enabled in the EEPROM,
1133 * enable the ACPI Magic Packet filter
1134 */
1135
Joe Perches1dc32912008-07-11 15:17:08 -07001136 switch (hw->mac_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 case e1000_82542_rev2_0:
1138 case e1000_82542_rev2_1:
1139 case e1000_82543:
1140 break;
1141 case e1000_82544:
Joe Perches1dc32912008-07-11 15:17:08 -07001142 e1000_read_eeprom(hw,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 EEPROM_INIT_CONTROL2_REG, 1, &eeprom_data);
1144 eeprom_apme_mask = E1000_EEPROM_82544_APM;
1145 break;
1146 case e1000_82546:
1147 case e1000_82546_rev_3:
Joe Perches1dc32912008-07-11 15:17:08 -07001148 if (er32(STATUS) & E1000_STATUS_FUNC_1){
1149 e1000_read_eeprom(hw,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 EEPROM_INIT_CONTROL3_PORT_B, 1, &eeprom_data);
1151 break;
1152 }
1153 /* Fall Through */
1154 default:
Joe Perches1dc32912008-07-11 15:17:08 -07001155 e1000_read_eeprom(hw,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 EEPROM_INIT_CONTROL3_PORT_A, 1, &eeprom_data);
1157 break;
1158 }
Jesse Brandeburg96838a42006-01-18 13:01:39 -08001159 if (eeprom_data & eeprom_apme_mask)
Jesse Brandeburg120cd572006-08-31 14:27:46 -07001160 adapter->eeprom_wol |= E1000_WUFC_MAG;
1161
1162 /* now that we have the eeprom settings, apply the special cases
1163 * where the eeprom may be wrong or the board simply won't support
1164 * wake on lan on a particular port */
1165 switch (pdev->device) {
1166 case E1000_DEV_ID_82546GB_PCIE:
1167 adapter->eeprom_wol = 0;
1168 break;
1169 case E1000_DEV_ID_82546EB_FIBER:
1170 case E1000_DEV_ID_82546GB_FIBER:
Jesse Brandeburg120cd572006-08-31 14:27:46 -07001171 /* Wake events only supported on port A for dual fiber
1172 * regardless of eeprom setting */
Joe Perches1dc32912008-07-11 15:17:08 -07001173 if (er32(STATUS) & E1000_STATUS_FUNC_1)
Jesse Brandeburg120cd572006-08-31 14:27:46 -07001174 adapter->eeprom_wol = 0;
1175 break;
1176 case E1000_DEV_ID_82546GB_QUAD_COPPER_KSP3:
1177 /* if quad port adapter, disable WoL on all but port A */
1178 if (global_quad_port_a != 0)
1179 adapter->eeprom_wol = 0;
1180 else
Rusty Russell3db1cd52011-12-19 13:56:45 +00001181 adapter->quad_port_a = true;
Jesse Brandeburg120cd572006-08-31 14:27:46 -07001182 /* Reset for multiple quad port adapters */
1183 if (++global_quad_port_a == 4)
1184 global_quad_port_a = 0;
1185 break;
1186 }
1187
1188 /* initialize the wol settings based on the eeprom settings */
1189 adapter->wol = adapter->eeprom_wol;
\"Rafael J. Wysocki\de126482008-11-07 20:30:19 +00001190 device_set_wakeup_enable(&adapter->pdev->dev, adapter->wol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
Dirk Brandewie5377a412011-01-06 14:29:54 +00001192 /* Auto detect PHY address */
1193 if (hw->mac_type == e1000_ce4100) {
1194 for (i = 0; i < 32; i++) {
1195 hw->phy_addr = i;
1196 e1000_read_phy_reg(hw, PHY_ID2, &tmp);
1197 if (tmp == 0 || tmp == 0xFF) {
1198 if (i == 31)
1199 goto err_eeprom;
1200 continue;
1201 } else
1202 break;
1203 }
1204 }
1205
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 /* reset the hardware with the new settings */
1207 e1000_reset(adapter);
1208
Auke Kok416b5d12007-06-01 10:22:39 -07001209 strcpy(netdev->name, "eth%d");
Joe Perchesc7be73b2008-07-11 15:17:28 -07001210 err = register_netdev(netdev);
1211 if (err)
Auke Kok416b5d12007-06-01 10:22:39 -07001212 goto err_register;
Auke Kok1314bbf2006-09-27 12:54:02 -07001213
Jiri Pirko5622e402011-07-21 03:26:31 +00001214 e1000_vlan_mode(netdev, netdev->features);
1215
Emil Tantilov675ad472010-04-27 14:02:58 +00001216 /* print bus type/speed/width info */
Emil Tantilovfeb8f472010-07-26 23:37:21 -07001217 e_info(probe, "(PCI%s:%dMHz:%d-bit) %pM\n",
Joe Perches7837e582010-06-11 12:51:49 +00001218 ((hw->bus_type == e1000_bus_type_pcix) ? "-X" : ""),
1219 ((hw->bus_speed == e1000_bus_speed_133) ? 133 :
1220 (hw->bus_speed == e1000_bus_speed_120) ? 120 :
1221 (hw->bus_speed == e1000_bus_speed_100) ? 100 :
1222 (hw->bus_speed == e1000_bus_speed_66) ? 66 : 33),
1223 ((hw->bus_width == e1000_bus_width_64) ? 64 : 32),
1224 netdev->dev_addr);
Emil Tantilov675ad472010-04-27 14:02:58 +00001225
Jesse Brandeburgeb62efd2009-04-17 20:44:36 +00001226 /* carrier off reporting is important to ethtool even BEFORE open */
1227 netif_carrier_off(netdev);
1228
Emil Tantilovfeb8f472010-07-26 23:37:21 -07001229 e_info(probe, "Intel(R) PRO/1000 Network Connection\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
1231 cards_found++;
1232 return 0;
1233
1234err_register:
Vasily Averin6dd62ab2006-08-28 14:56:22 -07001235err_eeprom:
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00001236 e1000_phy_hw_reset(hw);
Vasily Averin6dd62ab2006-08-28 14:56:22 -07001237
Joe Perches1dc32912008-07-11 15:17:08 -07001238 if (hw->flash_address)
1239 iounmap(hw->flash_address);
Vasily Averin6dd62ab2006-08-28 14:56:22 -07001240 kfree(adapter->tx_ring);
1241 kfree(adapter->rx_ring);
Jesse Brandeburge508be12010-09-07 21:01:12 +00001242err_dma:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243err_sw_init:
Dirk Brandewie5377a412011-01-06 14:29:54 +00001244err_mdio_ioremap:
Florian Fainelli13acde82012-01-04 20:23:35 +00001245 iounmap(hw->ce4100_gbe_mdio_base_virt);
Joe Perches1dc32912008-07-11 15:17:08 -07001246 iounmap(hw->hw_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247err_ioremap:
1248 free_netdev(netdev);
1249err_alloc_etherdev:
Taku Izumi81250292008-07-11 15:17:44 -07001250 pci_release_selected_regions(pdev, bars);
Vasily Averin6dd62ab2006-08-28 14:56:22 -07001251err_pci_reg:
Vasily Averin6dd62ab2006-08-28 14:56:22 -07001252 pci_disable_device(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 return err;
1254}
1255
1256/**
1257 * e1000_remove - Device Removal Routine
1258 * @pdev: PCI device information struct
1259 *
1260 * e1000_remove is called by the PCI subsystem to alert the driver
1261 * that it should release a PCI device. The could be caused by a
1262 * Hot-Plug event, or because the driver is going to be removed from
1263 * memory.
1264 **/
1265
Joe Perches64798842008-07-11 15:17:02 -07001266static void __devexit e1000_remove(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267{
1268 struct net_device *netdev = pci_get_drvdata(pdev);
Malli Chilakala60490fe2005-06-17 17:41:45 -07001269 struct e1000_adapter *adapter = netdev_priv(netdev);
Joe Perches1dc32912008-07-11 15:17:08 -07001270 struct e1000_hw *hw = &adapter->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
Jesse Brandeburga4010af2011-10-05 07:24:41 +00001272 e1000_down_and_stop(adapter);
Jeff Garzik0fccd0e2006-12-15 10:56:10 -05001273 e1000_release_manageability(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001275 unregister_netdev(netdev);
1276
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00001277 e1000_phy_hw_reset(hw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
Mallikarjuna R Chilakala24025e42005-10-04 07:03:23 -04001279 kfree(adapter->tx_ring);
1280 kfree(adapter->rx_ring);
Mallikarjuna R Chilakala24025e42005-10-04 07:03:23 -04001281
Florian Fainelli1c267502012-01-04 20:23:34 +00001282 if (hw->mac_type == e1000_ce4100)
Florian Fainelli13acde82012-01-04 20:23:35 +00001283 iounmap(hw->ce4100_gbe_mdio_base_virt);
Joe Perches1dc32912008-07-11 15:17:08 -07001284 iounmap(hw->hw_addr);
1285 if (hw->flash_address)
1286 iounmap(hw->flash_address);
Taku Izumi81250292008-07-11 15:17:44 -07001287 pci_release_selected_regions(pdev, adapter->bars);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
1289 free_netdev(netdev);
1290
1291 pci_disable_device(pdev);
1292}
1293
1294/**
1295 * e1000_sw_init - Initialize general software structures (struct e1000_adapter)
1296 * @adapter: board private structure to initialize
1297 *
1298 * e1000_sw_init initializes the Adapter private data structure.
Jesse Brandeburge508be12010-09-07 21:01:12 +00001299 * e1000_init_hw_struct MUST be called before this function
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 **/
1301
Joe Perches64798842008-07-11 15:17:02 -07001302static int __devinit e1000_sw_init(struct e1000_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303{
Auke Kokeb0f8052006-07-14 16:14:48 -07001304 adapter->rx_buffer_len = MAXIMUM_ETHERNET_VLAN_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
Jeff Kirsherf56799e2006-01-12 16:50:39 -08001306 adapter->num_tx_queues = 1;
1307 adapter->num_rx_queues = 1;
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001308
1309 if (e1000_alloc_queues(adapter)) {
Emil Tantilovfeb8f472010-07-26 23:37:21 -07001310 e_err(probe, "Unable to allocate memory for queues\n");
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001311 return -ENOMEM;
1312 }
1313
Herbert Xu47313052007-05-29 15:07:31 -07001314 /* Explicitly disable IRQ since the NIC can be in any state. */
Herbert Xu47313052007-05-29 15:07:31 -07001315 e1000_irq_disable(adapter);
1316
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 spin_lock_init(&adapter->stats_lock);
Jesse Brandeburg0ef4eed2011-10-05 07:24:51 +00001318 mutex_init(&adapter->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
Auke Kok1314bbf2006-09-27 12:54:02 -07001320 set_bit(__E1000_DOWN, &adapter->flags);
1321
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 return 0;
1323}
1324
1325/**
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001326 * e1000_alloc_queues - Allocate memory for all rings
1327 * @adapter: board private structure to initialize
1328 *
1329 * We allocate one ring per queue at run-time since we don't know the
Wang Chen3e1d7cd2008-12-03 22:07:10 -08001330 * number of queues at compile-time.
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001331 **/
1332
Joe Perches64798842008-07-11 15:17:02 -07001333static int __devinit e1000_alloc_queues(struct e1000_adapter *adapter)
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001334{
Yan Burman1c7e5b12007-03-06 08:58:04 -08001335 adapter->tx_ring = kcalloc(adapter->num_tx_queues,
1336 sizeof(struct e1000_tx_ring), GFP_KERNEL);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001337 if (!adapter->tx_ring)
1338 return -ENOMEM;
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001339
Yan Burman1c7e5b12007-03-06 08:58:04 -08001340 adapter->rx_ring = kcalloc(adapter->num_rx_queues,
1341 sizeof(struct e1000_rx_ring), GFP_KERNEL);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001342 if (!adapter->rx_ring) {
1343 kfree(adapter->tx_ring);
1344 return -ENOMEM;
1345 }
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001346
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001347 return E1000_SUCCESS;
1348}
1349
1350/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 * e1000_open - Called when a network interface is made active
1352 * @netdev: network interface device structure
1353 *
1354 * Returns 0 on success, negative value on failure
1355 *
1356 * The open entry point is called when a network interface is made
1357 * active by the system (IFF_UP). At this point all resources needed
1358 * for transmit and receive operations are allocated, the interrupt
Jesse Brandeburga4010af2011-10-05 07:24:41 +00001359 * handler is registered with the OS, the watchdog task is started,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 * and the stack is notified that the interface is ready.
1361 **/
1362
Joe Perches64798842008-07-11 15:17:02 -07001363static int e1000_open(struct net_device *netdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364{
Malli Chilakala60490fe2005-06-17 17:41:45 -07001365 struct e1000_adapter *adapter = netdev_priv(netdev);
Joe Perches1dc32912008-07-11 15:17:08 -07001366 struct e1000_hw *hw = &adapter->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 int err;
1368
Auke Kok2db10a02006-06-27 09:06:28 -07001369 /* disallow open during test */
Auke Kok1314bbf2006-09-27 12:54:02 -07001370 if (test_bit(__E1000_TESTING, &adapter->flags))
Auke Kok2db10a02006-06-27 09:06:28 -07001371 return -EBUSY;
1372
Jesse Brandeburgeb62efd2009-04-17 20:44:36 +00001373 netif_carrier_off(netdev);
1374
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 /* allocate transmit descriptors */
Auke Koke0aac5a2007-03-06 08:57:21 -08001376 err = e1000_setup_all_tx_resources(adapter);
1377 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 goto err_setup_tx;
1379
1380 /* allocate receive descriptors */
Auke Koke0aac5a2007-03-06 08:57:21 -08001381 err = e1000_setup_all_rx_resources(adapter);
Linus Torvaldsb5bf28c2007-02-21 11:21:44 -08001382 if (err)
Auke Koke0aac5a2007-03-06 08:57:21 -08001383 goto err_setup_rx;
Linus Torvaldsb5bf28c2007-02-21 11:21:44 -08001384
Auke Kok79f05bf2006-06-27 09:06:32 -07001385 e1000_power_up_phy(adapter);
1386
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001387 adapter->mng_vlan_id = E1000_MNG_VLAN_NONE;
Joe Perches1dc32912008-07-11 15:17:08 -07001388 if ((hw->mng_cookie.status &
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001389 E1000_MNG_DHCP_COOKIE_STATUS_VLAN_SUPPORT)) {
1390 e1000_update_mng_vlan(adapter);
1391 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
Auke Koke0aac5a2007-03-06 08:57:21 -08001393 /* before we allocate an interrupt, we must be ready to handle it.
1394 * Setting DEBUG_SHIRQ in the kernel makes it fire an interrupt
1395 * as soon as we call pci_request_irq, so we have to setup our
1396 * clean_rx handler before we do so. */
1397 e1000_configure(adapter);
1398
1399 err = e1000_request_irq(adapter);
1400 if (err)
1401 goto err_req_irq;
1402
1403 /* From here on the code is the same as e1000_up() */
1404 clear_bit(__E1000_DOWN, &adapter->flags);
1405
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001406 napi_enable(&adapter->napi);
Herbert Xu47313052007-05-29 15:07:31 -07001407
Auke Koke0aac5a2007-03-06 08:57:21 -08001408 e1000_irq_enable(adapter);
1409
Ben Hutchings076152d2008-07-18 17:50:57 -07001410 netif_start_queue(netdev);
1411
Auke Koke0aac5a2007-03-06 08:57:21 -08001412 /* fire a link status change interrupt to start the watchdog */
Joe Perches1dc32912008-07-11 15:17:08 -07001413 ew32(ICS, E1000_ICS_LSC);
Auke Koke0aac5a2007-03-06 08:57:21 -08001414
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 return E1000_SUCCESS;
1416
Linus Torvaldsb5bf28c2007-02-21 11:21:44 -08001417err_req_irq:
Auke Koke0aac5a2007-03-06 08:57:21 -08001418 e1000_power_down_phy(adapter);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001419 e1000_free_all_rx_resources(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420err_setup_rx:
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001421 e1000_free_all_tx_resources(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422err_setup_tx:
1423 e1000_reset(adapter);
1424
1425 return err;
1426}
1427
1428/**
1429 * e1000_close - Disables a network interface
1430 * @netdev: network interface device structure
1431 *
1432 * Returns 0, this is not allowed to fail
1433 *
1434 * The close entry point is called when an interface is de-activated
1435 * by the OS. The hardware is still under the drivers control, but
1436 * needs to be disabled. A global MAC reset is issued to stop the
1437 * hardware, and all transmit and receive resources are freed.
1438 **/
1439
Joe Perches64798842008-07-11 15:17:02 -07001440static int e1000_close(struct net_device *netdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441{
Malli Chilakala60490fe2005-06-17 17:41:45 -07001442 struct e1000_adapter *adapter = netdev_priv(netdev);
Joe Perches1dc32912008-07-11 15:17:08 -07001443 struct e1000_hw *hw = &adapter->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
Auke Kok2db10a02006-06-27 09:06:28 -07001445 WARN_ON(test_bit(__E1000_RESETTING, &adapter->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 e1000_down(adapter);
Auke Kok79f05bf2006-06-27 09:06:32 -07001447 e1000_power_down_phy(adapter);
Auke Kok2db10a02006-06-27 09:06:28 -07001448 e1000_free_irq(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001450 e1000_free_all_tx_resources(adapter);
1451 e1000_free_all_rx_resources(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
Bruce Allan46665602006-09-27 12:54:08 -07001453 /* kill manageability vlan ID if supported, but not if a vlan with
1454 * the same ID is registered on the host OS (let 8021q kill it) */
Joe Perches1dc32912008-07-11 15:17:08 -07001455 if ((hw->mng_cookie.status &
Bruce Allan46665602006-09-27 12:54:08 -07001456 E1000_MNG_DHCP_COOKIE_STATUS_VLAN_SUPPORT) &&
Jiri Pirko5622e402011-07-21 03:26:31 +00001457 !test_bit(adapter->mng_vlan_id, adapter->active_vlans)) {
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001458 e1000_vlan_rx_kill_vid(netdev, adapter->mng_vlan_id);
1459 }
Jeff Kirsherb55ccb32006-01-12 16:50:30 -08001460
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 return 0;
1462}
1463
1464/**
1465 * e1000_check_64k_bound - check that memory doesn't cross 64kB boundary
1466 * @adapter: address of board private structure
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001467 * @start: address of beginning of memory
1468 * @len: length of memory
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 **/
Joe Perches64798842008-07-11 15:17:02 -07001470static bool e1000_check_64k_bound(struct e1000_adapter *adapter, void *start,
1471 unsigned long len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472{
Joe Perches1dc32912008-07-11 15:17:08 -07001473 struct e1000_hw *hw = &adapter->hw;
Joe Perchese982f172008-07-11 15:17:18 -07001474 unsigned long begin = (unsigned long)start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 unsigned long end = begin + len;
1476
Malli Chilakala26483452005-04-28 19:44:46 -07001477 /* First rev 82545 and 82546 need to not allow any memory
1478 * write location to cross 64k boundary due to errata 23 */
Joe Perches1dc32912008-07-11 15:17:08 -07001479 if (hw->mac_type == e1000_82545 ||
Dirk Brandewie5377a412011-01-06 14:29:54 +00001480 hw->mac_type == e1000_ce4100 ||
Joe Perches1dc32912008-07-11 15:17:08 -07001481 hw->mac_type == e1000_82546) {
Joe Perchesc3033b02008-03-21 11:06:25 -07001482 return ((begin ^ (end - 1)) >> 16) != 0 ? false : true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 }
1484
Joe Perchesc3033b02008-03-21 11:06:25 -07001485 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486}
1487
1488/**
1489 * e1000_setup_tx_resources - allocate Tx resources (Descriptors)
1490 * @adapter: board private structure
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001491 * @txdr: tx descriptor ring (for a specific queue) to setup
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 *
1493 * Return 0 on success, negative on failure
1494 **/
1495
Joe Perches64798842008-07-11 15:17:02 -07001496static int e1000_setup_tx_resources(struct e1000_adapter *adapter,
1497 struct e1000_tx_ring *txdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 struct pci_dev *pdev = adapter->pdev;
1500 int size;
1501
1502 size = sizeof(struct e1000_buffer) * txdr->count;
Eric Dumazet89bf67f2010-11-22 00:15:06 +00001503 txdr->buffer_info = vzalloc(size);
Jesse Brandeburg96838a42006-01-18 13:01:39 -08001504 if (!txdr->buffer_info) {
Emil Tantilovfeb8f472010-07-26 23:37:21 -07001505 e_err(probe, "Unable to allocate memory for the Tx descriptor "
1506 "ring\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 return -ENOMEM;
1508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
1510 /* round up to nearest 4K */
1511
1512 txdr->size = txdr->count * sizeof(struct e1000_tx_desc);
Milind Arun Choudhary9099cfb2007-04-27 13:55:29 -07001513 txdr->size = ALIGN(txdr->size, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
Nick Nunleyb16f53b2010-04-27 13:08:45 +00001515 txdr->desc = dma_alloc_coherent(&pdev->dev, txdr->size, &txdr->dma,
1516 GFP_KERNEL);
Jesse Brandeburg96838a42006-01-18 13:01:39 -08001517 if (!txdr->desc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518setup_tx_desc_die:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 vfree(txdr->buffer_info);
Emil Tantilovfeb8f472010-07-26 23:37:21 -07001520 e_err(probe, "Unable to allocate memory for the Tx descriptor "
1521 "ring\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 return -ENOMEM;
1523 }
1524
Malli Chilakala26483452005-04-28 19:44:46 -07001525 /* Fix for errata 23, can't cross 64kB boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 if (!e1000_check_64k_bound(adapter, txdr->desc, txdr->size)) {
1527 void *olddesc = txdr->desc;
1528 dma_addr_t olddma = txdr->dma;
Emil Tantilovfeb8f472010-07-26 23:37:21 -07001529 e_err(tx_err, "txdr align check failed: %u bytes at %p\n",
Emil Tantilov675ad472010-04-27 14:02:58 +00001530 txdr->size, txdr->desc);
Malli Chilakala26483452005-04-28 19:44:46 -07001531 /* Try again, without freeing the previous */
Nick Nunleyb16f53b2010-04-27 13:08:45 +00001532 txdr->desc = dma_alloc_coherent(&pdev->dev, txdr->size,
1533 &txdr->dma, GFP_KERNEL);
Malli Chilakala26483452005-04-28 19:44:46 -07001534 /* Failed allocation, critical failure */
Jesse Brandeburg96838a42006-01-18 13:01:39 -08001535 if (!txdr->desc) {
Nick Nunleyb16f53b2010-04-27 13:08:45 +00001536 dma_free_coherent(&pdev->dev, txdr->size, olddesc,
1537 olddma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 goto setup_tx_desc_die;
1539 }
1540
1541 if (!e1000_check_64k_bound(adapter, txdr->desc, txdr->size)) {
1542 /* give up */
Nick Nunleyb16f53b2010-04-27 13:08:45 +00001543 dma_free_coherent(&pdev->dev, txdr->size, txdr->desc,
1544 txdr->dma);
1545 dma_free_coherent(&pdev->dev, txdr->size, olddesc,
1546 olddma);
Emil Tantilovfeb8f472010-07-26 23:37:21 -07001547 e_err(probe, "Unable to allocate aligned memory "
Emil Tantilov675ad472010-04-27 14:02:58 +00001548 "for the transmit descriptor ring\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 vfree(txdr->buffer_info);
1550 return -ENOMEM;
1551 } else {
Malli Chilakala26483452005-04-28 19:44:46 -07001552 /* Free old allocation, new allocation was successful */
Nick Nunleyb16f53b2010-04-27 13:08:45 +00001553 dma_free_coherent(&pdev->dev, txdr->size, olddesc,
1554 olddma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 }
1556 }
1557 memset(txdr->desc, 0, txdr->size);
1558
1559 txdr->next_to_use = 0;
1560 txdr->next_to_clean = 0;
1561
1562 return 0;
1563}
1564
1565/**
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001566 * e1000_setup_all_tx_resources - wrapper to allocate Tx resources
1567 * (Descriptors) for all queues
1568 * @adapter: board private structure
1569 *
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001570 * Return 0 on success, negative on failure
1571 **/
1572
Joe Perches64798842008-07-11 15:17:02 -07001573int e1000_setup_all_tx_resources(struct e1000_adapter *adapter)
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001574{
1575 int i, err = 0;
1576
Jeff Kirsherf56799e2006-01-12 16:50:39 -08001577 for (i = 0; i < adapter->num_tx_queues; i++) {
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001578 err = e1000_setup_tx_resources(adapter, &adapter->tx_ring[i]);
1579 if (err) {
Emil Tantilovfeb8f472010-07-26 23:37:21 -07001580 e_err(probe, "Allocation for Tx Queue %u failed\n", i);
Vasily Averin3fbbc722006-08-28 14:56:24 -07001581 for (i-- ; i >= 0; i--)
1582 e1000_free_tx_resources(adapter,
1583 &adapter->tx_ring[i]);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001584 break;
1585 }
1586 }
1587
1588 return err;
1589}
1590
1591/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 * e1000_configure_tx - Configure 8254x Transmit Unit after Reset
1593 * @adapter: board private structure
1594 *
1595 * Configure the Tx unit of the MAC after a reset.
1596 **/
1597
Joe Perches64798842008-07-11 15:17:02 -07001598static void e1000_configure_tx(struct e1000_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599{
Joe Perches406874a2008-04-03 10:06:32 -07001600 u64 tdba;
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001601 struct e1000_hw *hw = &adapter->hw;
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00001602 u32 tdlen, tctl, tipg;
Joe Perches406874a2008-04-03 10:06:32 -07001603 u32 ipgr1, ipgr2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604
1605 /* Setup the HW Tx Head and Tail descriptor pointers */
1606
Jeff Kirsherf56799e2006-01-12 16:50:39 -08001607 switch (adapter->num_tx_queues) {
Mallikarjuna R Chilakala24025e42005-10-04 07:03:23 -04001608 case 1:
1609 default:
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001610 tdba = adapter->tx_ring[0].dma;
1611 tdlen = adapter->tx_ring[0].count *
1612 sizeof(struct e1000_tx_desc);
Joe Perches1dc32912008-07-11 15:17:08 -07001613 ew32(TDLEN, tdlen);
1614 ew32(TDBAH, (tdba >> 32));
1615 ew32(TDBAL, (tdba & 0x00000000ffffffffULL));
1616 ew32(TDT, 0);
1617 ew32(TDH, 0);
Auke Kok6a951692006-09-11 14:00:21 -07001618 adapter->tx_ring[0].tdh = ((hw->mac_type >= e1000_82543) ? E1000_TDH : E1000_82542_TDH);
1619 adapter->tx_ring[0].tdt = ((hw->mac_type >= e1000_82543) ? E1000_TDT : E1000_82542_TDT);
Mallikarjuna R Chilakala24025e42005-10-04 07:03:23 -04001620 break;
1621 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
1623 /* Set the default values for the Tx Inter Packet Gap timer */
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00001624 if ((hw->media_type == e1000_media_type_fiber ||
Jesse Brandeburgd89b6c62006-12-15 10:38:32 +01001625 hw->media_type == e1000_media_type_internal_serdes))
Jeff Kirsher0fadb052006-01-12 16:51:05 -08001626 tipg = DEFAULT_82543_TIPG_IPGT_FIBER;
1627 else
1628 tipg = DEFAULT_82543_TIPG_IPGT_COPPER;
1629
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001630 switch (hw->mac_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 case e1000_82542_rev2_0:
1632 case e1000_82542_rev2_1:
1633 tipg = DEFAULT_82542_TIPG_IPGT;
Jeff Kirsher0fadb052006-01-12 16:51:05 -08001634 ipgr1 = DEFAULT_82542_TIPG_IPGR1;
1635 ipgr2 = DEFAULT_82542_TIPG_IPGR2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 break;
1637 default:
Jeff Kirsher0fadb052006-01-12 16:51:05 -08001638 ipgr1 = DEFAULT_82543_TIPG_IPGR1;
1639 ipgr2 = DEFAULT_82543_TIPG_IPGR2;
1640 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 }
Jeff Kirsher0fadb052006-01-12 16:51:05 -08001642 tipg |= ipgr1 << E1000_TIPG_IPGR1_SHIFT;
1643 tipg |= ipgr2 << E1000_TIPG_IPGR2_SHIFT;
Joe Perches1dc32912008-07-11 15:17:08 -07001644 ew32(TIPG, tipg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
1646 /* Set the Tx Interrupt Delay register */
1647
Joe Perches1dc32912008-07-11 15:17:08 -07001648 ew32(TIDV, adapter->tx_int_delay);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001649 if (hw->mac_type >= e1000_82540)
Joe Perches1dc32912008-07-11 15:17:08 -07001650 ew32(TADV, adapter->tx_abs_int_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
1652 /* Program the Transmit Control Register */
1653
Joe Perches1dc32912008-07-11 15:17:08 -07001654 tctl = er32(TCTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 tctl &= ~E1000_TCTL_CT;
Jeff Kirsher7e6c9862006-03-02 18:19:30 -08001656 tctl |= E1000_TCTL_PSP | E1000_TCTL_RTLC |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 (E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT);
1658
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001659 e1000_config_collision_dist(hw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
1661 /* Setup Transmit Descriptor Settings for eop descriptor */
Jesse Brandeburg6a042da2006-11-01 08:48:04 -08001662 adapter->txd_cmd = E1000_TXD_CMD_EOP | E1000_TXD_CMD_IFCS;
1663
1664 /* only set IDE if we are delaying interrupts using the timers */
1665 if (adapter->tx_int_delay)
1666 adapter->txd_cmd |= E1000_TXD_CMD_IDE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001668 if (hw->mac_type < e1000_82543)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 adapter->txd_cmd |= E1000_TXD_CMD_RPS;
1670 else
1671 adapter->txd_cmd |= E1000_TXD_CMD_RS;
1672
1673 /* Cache if we're 82544 running in PCI-X because we'll
1674 * need this to apply a workaround later in the send path. */
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001675 if (hw->mac_type == e1000_82544 &&
1676 hw->bus_type == e1000_bus_type_pcix)
Rusty Russell3db1cd52011-12-19 13:56:45 +00001677 adapter->pcix_82544 = true;
Jeff Kirsher7e6c9862006-03-02 18:19:30 -08001678
Joe Perches1dc32912008-07-11 15:17:08 -07001679 ew32(TCTL, tctl);
Jeff Kirsher7e6c9862006-03-02 18:19:30 -08001680
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681}
1682
1683/**
1684 * e1000_setup_rx_resources - allocate Rx resources (Descriptors)
1685 * @adapter: board private structure
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001686 * @rxdr: rx descriptor ring (for a specific queue) to setup
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 *
1688 * Returns 0 on success, negative on failure
1689 **/
1690
Joe Perches64798842008-07-11 15:17:02 -07001691static int e1000_setup_rx_resources(struct e1000_adapter *adapter,
1692 struct e1000_rx_ring *rxdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 struct pci_dev *pdev = adapter->pdev;
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001695 int size, desc_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
1697 size = sizeof(struct e1000_buffer) * rxdr->count;
Eric Dumazet89bf67f2010-11-22 00:15:06 +00001698 rxdr->buffer_info = vzalloc(size);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001699 if (!rxdr->buffer_info) {
Emil Tantilovfeb8f472010-07-26 23:37:21 -07001700 e_err(probe, "Unable to allocate memory for the Rx descriptor "
1701 "ring\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 return -ENOMEM;
1703 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00001705 desc_len = sizeof(struct e1000_rx_desc);
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001706
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 /* Round up to nearest 4K */
1708
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001709 rxdr->size = rxdr->count * desc_len;
Milind Arun Choudhary9099cfb2007-04-27 13:55:29 -07001710 rxdr->size = ALIGN(rxdr->size, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
Nick Nunleyb16f53b2010-04-27 13:08:45 +00001712 rxdr->desc = dma_alloc_coherent(&pdev->dev, rxdr->size, &rxdr->dma,
1713 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001715 if (!rxdr->desc) {
Emil Tantilovfeb8f472010-07-26 23:37:21 -07001716 e_err(probe, "Unable to allocate memory for the Rx descriptor "
1717 "ring\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718setup_rx_desc_die:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 vfree(rxdr->buffer_info);
1720 return -ENOMEM;
1721 }
1722
Malli Chilakala26483452005-04-28 19:44:46 -07001723 /* Fix for errata 23, can't cross 64kB boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 if (!e1000_check_64k_bound(adapter, rxdr->desc, rxdr->size)) {
1725 void *olddesc = rxdr->desc;
1726 dma_addr_t olddma = rxdr->dma;
Emil Tantilovfeb8f472010-07-26 23:37:21 -07001727 e_err(rx_err, "rxdr align check failed: %u bytes at %p\n",
Emil Tantilov675ad472010-04-27 14:02:58 +00001728 rxdr->size, rxdr->desc);
Malli Chilakala26483452005-04-28 19:44:46 -07001729 /* Try again, without freeing the previous */
Nick Nunleyb16f53b2010-04-27 13:08:45 +00001730 rxdr->desc = dma_alloc_coherent(&pdev->dev, rxdr->size,
1731 &rxdr->dma, GFP_KERNEL);
Malli Chilakala26483452005-04-28 19:44:46 -07001732 /* Failed allocation, critical failure */
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001733 if (!rxdr->desc) {
Nick Nunleyb16f53b2010-04-27 13:08:45 +00001734 dma_free_coherent(&pdev->dev, rxdr->size, olddesc,
1735 olddma);
Emil Tantilovfeb8f472010-07-26 23:37:21 -07001736 e_err(probe, "Unable to allocate memory for the Rx "
1737 "descriptor ring\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 goto setup_rx_desc_die;
1739 }
1740
1741 if (!e1000_check_64k_bound(adapter, rxdr->desc, rxdr->size)) {
1742 /* give up */
Nick Nunleyb16f53b2010-04-27 13:08:45 +00001743 dma_free_coherent(&pdev->dev, rxdr->size, rxdr->desc,
1744 rxdr->dma);
1745 dma_free_coherent(&pdev->dev, rxdr->size, olddesc,
1746 olddma);
Emil Tantilovfeb8f472010-07-26 23:37:21 -07001747 e_err(probe, "Unable to allocate aligned memory for "
1748 "the Rx descriptor ring\n");
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001749 goto setup_rx_desc_die;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 } else {
Malli Chilakala26483452005-04-28 19:44:46 -07001751 /* Free old allocation, new allocation was successful */
Nick Nunleyb16f53b2010-04-27 13:08:45 +00001752 dma_free_coherent(&pdev->dev, rxdr->size, olddesc,
1753 olddma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 }
1755 }
1756 memset(rxdr->desc, 0, rxdr->size);
1757
1758 rxdr->next_to_clean = 0;
1759 rxdr->next_to_use = 0;
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00001760 rxdr->rx_skb_top = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
1762 return 0;
1763}
1764
1765/**
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001766 * e1000_setup_all_rx_resources - wrapper to allocate Rx resources
1767 * (Descriptors) for all queues
1768 * @adapter: board private structure
1769 *
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001770 * Return 0 on success, negative on failure
1771 **/
1772
Joe Perches64798842008-07-11 15:17:02 -07001773int e1000_setup_all_rx_resources(struct e1000_adapter *adapter)
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001774{
1775 int i, err = 0;
1776
Jeff Kirsherf56799e2006-01-12 16:50:39 -08001777 for (i = 0; i < adapter->num_rx_queues; i++) {
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001778 err = e1000_setup_rx_resources(adapter, &adapter->rx_ring[i]);
1779 if (err) {
Emil Tantilovfeb8f472010-07-26 23:37:21 -07001780 e_err(probe, "Allocation for Rx Queue %u failed\n", i);
Vasily Averin3fbbc722006-08-28 14:56:24 -07001781 for (i-- ; i >= 0; i--)
1782 e1000_free_rx_resources(adapter,
1783 &adapter->rx_ring[i]);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001784 break;
1785 }
1786 }
1787
1788 return err;
1789}
1790
1791/**
Malli Chilakala26483452005-04-28 19:44:46 -07001792 * e1000_setup_rctl - configure the receive control registers
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 * @adapter: Board private structure
1794 **/
Joe Perches64798842008-07-11 15:17:02 -07001795static void e1000_setup_rctl(struct e1000_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796{
Joe Perches1dc32912008-07-11 15:17:08 -07001797 struct e1000_hw *hw = &adapter->hw;
Brandeburg, Jesse630b25c2008-09-16 13:01:28 -07001798 u32 rctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799
Joe Perches1dc32912008-07-11 15:17:08 -07001800 rctl = er32(RCTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801
1802 rctl &= ~(3 << E1000_RCTL_MO_SHIFT);
1803
Dean Nelsond5bc77a2011-09-16 16:52:54 +00001804 rctl |= E1000_RCTL_BAM | E1000_RCTL_LBM_NO |
1805 E1000_RCTL_RDMTS_HALF |
Joe Perches1dc32912008-07-11 15:17:08 -07001806 (hw->mc_filter_type << E1000_RCTL_MO_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
Joe Perches1dc32912008-07-11 15:17:08 -07001808 if (hw->tbi_compatibility_on == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 rctl |= E1000_RCTL_SBP;
1810 else
1811 rctl &= ~E1000_RCTL_SBP;
1812
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001813 if (adapter->netdev->mtu <= ETH_DATA_LEN)
1814 rctl &= ~E1000_RCTL_LPE;
1815 else
1816 rctl |= E1000_RCTL_LPE;
1817
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 /* Setup buffer sizes */
Auke Kok9e2feac2006-04-14 19:05:18 -07001819 rctl &= ~E1000_RCTL_SZ_4096;
1820 rctl |= E1000_RCTL_BSEX;
1821 switch (adapter->rx_buffer_len) {
Jeff Kirshera1415ee2006-02-28 20:24:07 -08001822 case E1000_RXBUFFER_2048:
1823 default:
1824 rctl |= E1000_RCTL_SZ_2048;
1825 rctl &= ~E1000_RCTL_BSEX;
1826 break;
1827 case E1000_RXBUFFER_4096:
1828 rctl |= E1000_RCTL_SZ_4096;
1829 break;
1830 case E1000_RXBUFFER_8192:
1831 rctl |= E1000_RCTL_SZ_8192;
1832 break;
1833 case E1000_RXBUFFER_16384:
1834 rctl |= E1000_RCTL_SZ_16384;
1835 break;
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001836 }
1837
Joe Perches1dc32912008-07-11 15:17:08 -07001838 ew32(RCTL, rctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839}
1840
1841/**
1842 * e1000_configure_rx - Configure 8254x Receive Unit after Reset
1843 * @adapter: board private structure
1844 *
1845 * Configure the Rx unit of the MAC after a reset.
1846 **/
1847
Joe Perches64798842008-07-11 15:17:02 -07001848static void e1000_configure_rx(struct e1000_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849{
Joe Perches406874a2008-04-03 10:06:32 -07001850 u64 rdba;
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001851 struct e1000_hw *hw = &adapter->hw;
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00001852 u32 rdlen, rctl, rxcsum;
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001853
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00001854 if (adapter->netdev->mtu > ETH_DATA_LEN) {
1855 rdlen = adapter->rx_ring[0].count *
1856 sizeof(struct e1000_rx_desc);
1857 adapter->clean_rx = e1000_clean_jumbo_rx_irq;
1858 adapter->alloc_rx_buf = e1000_alloc_jumbo_rx_buffers;
1859 } else {
1860 rdlen = adapter->rx_ring[0].count *
1861 sizeof(struct e1000_rx_desc);
1862 adapter->clean_rx = e1000_clean_rx_irq;
1863 adapter->alloc_rx_buf = e1000_alloc_rx_buffers;
1864 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865
1866 /* disable receives while setting up the descriptors */
Joe Perches1dc32912008-07-11 15:17:08 -07001867 rctl = er32(RCTL);
1868 ew32(RCTL, rctl & ~E1000_RCTL_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869
1870 /* set the Receive Delay Timer Register */
Joe Perches1dc32912008-07-11 15:17:08 -07001871 ew32(RDTR, adapter->rx_int_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001873 if (hw->mac_type >= e1000_82540) {
Joe Perches1dc32912008-07-11 15:17:08 -07001874 ew32(RADV, adapter->rx_abs_int_delay);
Jesse Brandeburg835bb122006-11-01 08:48:13 -08001875 if (adapter->itr_setting != 0)
Joe Perches1dc32912008-07-11 15:17:08 -07001876 ew32(ITR, 1000000000 / (adapter->itr * 256));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 }
1878
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001879 /* Setup the HW Rx Head and Tail Descriptor Pointers and
1880 * the Base and Length of the Rx Descriptor Ring */
Jeff Kirsherf56799e2006-01-12 16:50:39 -08001881 switch (adapter->num_rx_queues) {
Mallikarjuna R Chilakala24025e42005-10-04 07:03:23 -04001882 case 1:
1883 default:
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001884 rdba = adapter->rx_ring[0].dma;
Joe Perches1dc32912008-07-11 15:17:08 -07001885 ew32(RDLEN, rdlen);
1886 ew32(RDBAH, (rdba >> 32));
1887 ew32(RDBAL, (rdba & 0x00000000ffffffffULL));
1888 ew32(RDT, 0);
1889 ew32(RDH, 0);
Auke Kok6a951692006-09-11 14:00:21 -07001890 adapter->rx_ring[0].rdh = ((hw->mac_type >= e1000_82543) ? E1000_RDH : E1000_82542_RDH);
1891 adapter->rx_ring[0].rdt = ((hw->mac_type >= e1000_82543) ? E1000_RDT : E1000_82542_RDT);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001892 break;
Mallikarjuna R Chilakala24025e42005-10-04 07:03:23 -04001893 }
1894
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 /* Enable 82543 Receive Checksum Offload for TCP and UDP */
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001896 if (hw->mac_type >= e1000_82543) {
Joe Perches1dc32912008-07-11 15:17:08 -07001897 rxcsum = er32(RXCSUM);
Brandeburg, Jesse630b25c2008-09-16 13:01:28 -07001898 if (adapter->rx_csum)
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001899 rxcsum |= E1000_RXCSUM_TUOFL;
Brandeburg, Jesse630b25c2008-09-16 13:01:28 -07001900 else
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001901 /* don't need to clear IPPCSE as it defaults to 0 */
Brandeburg, Jesse630b25c2008-09-16 13:01:28 -07001902 rxcsum &= ~E1000_RXCSUM_TUOFL;
Joe Perches1dc32912008-07-11 15:17:08 -07001903 ew32(RXCSUM, rxcsum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 }
1905
1906 /* Enable Receives */
Dean Nelsond5bc77a2011-09-16 16:52:54 +00001907 ew32(RCTL, rctl | E1000_RCTL_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908}
1909
1910/**
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001911 * e1000_free_tx_resources - Free Tx Resources per Queue
1912 * @adapter: board private structure
1913 * @tx_ring: Tx descriptor ring for a specific queue
1914 *
1915 * Free all transmit software resources
1916 **/
1917
Joe Perches64798842008-07-11 15:17:02 -07001918static void e1000_free_tx_resources(struct e1000_adapter *adapter,
1919 struct e1000_tx_ring *tx_ring)
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001920{
1921 struct pci_dev *pdev = adapter->pdev;
1922
1923 e1000_clean_tx_ring(adapter, tx_ring);
1924
1925 vfree(tx_ring->buffer_info);
1926 tx_ring->buffer_info = NULL;
1927
Nick Nunleyb16f53b2010-04-27 13:08:45 +00001928 dma_free_coherent(&pdev->dev, tx_ring->size, tx_ring->desc,
1929 tx_ring->dma);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001930
1931 tx_ring->desc = NULL;
1932}
1933
1934/**
1935 * e1000_free_all_tx_resources - Free Tx Resources for All Queues
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 * @adapter: board private structure
1937 *
1938 * Free all transmit software resources
1939 **/
1940
Joe Perches64798842008-07-11 15:17:02 -07001941void e1000_free_all_tx_resources(struct e1000_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942{
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001943 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944
Jeff Kirsherf56799e2006-01-12 16:50:39 -08001945 for (i = 0; i < adapter->num_tx_queues; i++)
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001946 e1000_free_tx_resources(adapter, &adapter->tx_ring[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947}
1948
Joe Perches64798842008-07-11 15:17:02 -07001949static void e1000_unmap_and_free_tx_resource(struct e1000_adapter *adapter,
1950 struct e1000_buffer *buffer_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951{
Alexander Duyck602c0552009-12-02 16:46:00 +00001952 if (buffer_info->dma) {
1953 if (buffer_info->mapped_as_page)
Nick Nunleyb16f53b2010-04-27 13:08:45 +00001954 dma_unmap_page(&adapter->pdev->dev, buffer_info->dma,
1955 buffer_info->length, DMA_TO_DEVICE);
Alexander Duyck602c0552009-12-02 16:46:00 +00001956 else
Nick Nunleyb16f53b2010-04-27 13:08:45 +00001957 dma_unmap_single(&adapter->pdev->dev, buffer_info->dma,
Alexander Duyck602c0552009-12-02 16:46:00 +00001958 buffer_info->length,
Nick Nunleyb16f53b2010-04-27 13:08:45 +00001959 DMA_TO_DEVICE);
Alexander Duyck602c0552009-12-02 16:46:00 +00001960 buffer_info->dma = 0;
1961 }
Jesse Brandeburga9ebadd2006-11-01 08:47:53 -08001962 if (buffer_info->skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 dev_kfree_skb_any(buffer_info->skb);
Jesse Brandeburga9ebadd2006-11-01 08:47:53 -08001964 buffer_info->skb = NULL;
1965 }
Alexander Duyck37e73df2009-03-25 21:58:45 +00001966 buffer_info->time_stamp = 0;
Jesse Brandeburga9ebadd2006-11-01 08:47:53 -08001967 /* buffer_info must be completely set up in the transmit path */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968}
1969
1970/**
1971 * e1000_clean_tx_ring - Free Tx Buffers
1972 * @adapter: board private structure
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001973 * @tx_ring: ring to be cleaned
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 **/
1975
Joe Perches64798842008-07-11 15:17:02 -07001976static void e1000_clean_tx_ring(struct e1000_adapter *adapter,
1977 struct e1000_tx_ring *tx_ring)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978{
Joe Perches1dc32912008-07-11 15:17:08 -07001979 struct e1000_hw *hw = &adapter->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 struct e1000_buffer *buffer_info;
1981 unsigned long size;
1982 unsigned int i;
1983
1984 /* Free all the Tx ring sk_buffs */
1985
Jesse Brandeburg96838a42006-01-18 13:01:39 -08001986 for (i = 0; i < tx_ring->count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 buffer_info = &tx_ring->buffer_info[i];
1988 e1000_unmap_and_free_tx_resource(adapter, buffer_info);
1989 }
1990
1991 size = sizeof(struct e1000_buffer) * tx_ring->count;
1992 memset(tx_ring->buffer_info, 0, size);
1993
1994 /* Zero out the descriptor ring */
1995
1996 memset(tx_ring->desc, 0, tx_ring->size);
1997
1998 tx_ring->next_to_use = 0;
1999 tx_ring->next_to_clean = 0;
Rusty Russell3db1cd52011-12-19 13:56:45 +00002000 tx_ring->last_tx_tso = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001
Joe Perches1dc32912008-07-11 15:17:08 -07002002 writel(0, hw->hw_addr + tx_ring->tdh);
2003 writel(0, hw->hw_addr + tx_ring->tdt);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002004}
2005
2006/**
2007 * e1000_clean_all_tx_rings - Free Tx Buffers for all queues
2008 * @adapter: board private structure
2009 **/
2010
Joe Perches64798842008-07-11 15:17:02 -07002011static void e1000_clean_all_tx_rings(struct e1000_adapter *adapter)
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002012{
2013 int i;
2014
Jeff Kirsherf56799e2006-01-12 16:50:39 -08002015 for (i = 0; i < adapter->num_tx_queues; i++)
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002016 e1000_clean_tx_ring(adapter, &adapter->tx_ring[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017}
2018
2019/**
2020 * e1000_free_rx_resources - Free Rx Resources
2021 * @adapter: board private structure
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002022 * @rx_ring: ring to clean the resources from
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 *
2024 * Free all receive software resources
2025 **/
2026
Joe Perches64798842008-07-11 15:17:02 -07002027static void e1000_free_rx_resources(struct e1000_adapter *adapter,
2028 struct e1000_rx_ring *rx_ring)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 struct pci_dev *pdev = adapter->pdev;
2031
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002032 e1000_clean_rx_ring(adapter, rx_ring);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033
2034 vfree(rx_ring->buffer_info);
2035 rx_ring->buffer_info = NULL;
2036
Nick Nunleyb16f53b2010-04-27 13:08:45 +00002037 dma_free_coherent(&pdev->dev, rx_ring->size, rx_ring->desc,
2038 rx_ring->dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039
2040 rx_ring->desc = NULL;
2041}
2042
2043/**
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002044 * e1000_free_all_rx_resources - Free Rx Resources for All Queues
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 * @adapter: board private structure
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002046 *
2047 * Free all receive software resources
2048 **/
2049
Joe Perches64798842008-07-11 15:17:02 -07002050void e1000_free_all_rx_resources(struct e1000_adapter *adapter)
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002051{
2052 int i;
2053
Jeff Kirsherf56799e2006-01-12 16:50:39 -08002054 for (i = 0; i < adapter->num_rx_queues; i++)
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002055 e1000_free_rx_resources(adapter, &adapter->rx_ring[i]);
2056}
2057
2058/**
2059 * e1000_clean_rx_ring - Free Rx Buffers per Queue
2060 * @adapter: board private structure
2061 * @rx_ring: ring to free buffers from
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 **/
2063
Joe Perches64798842008-07-11 15:17:02 -07002064static void e1000_clean_rx_ring(struct e1000_adapter *adapter,
2065 struct e1000_rx_ring *rx_ring)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066{
Joe Perches1dc32912008-07-11 15:17:08 -07002067 struct e1000_hw *hw = &adapter->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 struct e1000_buffer *buffer_info;
2069 struct pci_dev *pdev = adapter->pdev;
2070 unsigned long size;
Brandeburg, Jesse630b25c2008-09-16 13:01:28 -07002071 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
2073 /* Free all the Rx ring sk_buffs */
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002074 for (i = 0; i < rx_ring->count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 buffer_info = &rx_ring->buffer_info[i];
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00002076 if (buffer_info->dma &&
2077 adapter->clean_rx == e1000_clean_rx_irq) {
Nick Nunleyb16f53b2010-04-27 13:08:45 +00002078 dma_unmap_single(&pdev->dev, buffer_info->dma,
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00002079 buffer_info->length,
Nick Nunleyb16f53b2010-04-27 13:08:45 +00002080 DMA_FROM_DEVICE);
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00002081 } else if (buffer_info->dma &&
2082 adapter->clean_rx == e1000_clean_jumbo_rx_irq) {
Nick Nunleyb16f53b2010-04-27 13:08:45 +00002083 dma_unmap_page(&pdev->dev, buffer_info->dma,
2084 buffer_info->length,
2085 DMA_FROM_DEVICE);
Jesse Brandeburg679be3b2009-06-30 12:45:34 +00002086 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
Jesse Brandeburg679be3b2009-06-30 12:45:34 +00002088 buffer_info->dma = 0;
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00002089 if (buffer_info->page) {
2090 put_page(buffer_info->page);
2091 buffer_info->page = NULL;
2092 }
Jesse Brandeburg679be3b2009-06-30 12:45:34 +00002093 if (buffer_info->skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 dev_kfree_skb(buffer_info->skb);
2095 buffer_info->skb = NULL;
Jeff Kirsher997f5cb2006-01-12 16:50:55 -08002096 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 }
2098
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00002099 /* there also may be some cached data from a chained receive */
2100 if (rx_ring->rx_skb_top) {
2101 dev_kfree_skb(rx_ring->rx_skb_top);
2102 rx_ring->rx_skb_top = NULL;
2103 }
2104
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 size = sizeof(struct e1000_buffer) * rx_ring->count;
2106 memset(rx_ring->buffer_info, 0, size);
2107
2108 /* Zero out the descriptor ring */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 memset(rx_ring->desc, 0, rx_ring->size);
2110
2111 rx_ring->next_to_clean = 0;
2112 rx_ring->next_to_use = 0;
2113
Joe Perches1dc32912008-07-11 15:17:08 -07002114 writel(0, hw->hw_addr + rx_ring->rdh);
2115 writel(0, hw->hw_addr + rx_ring->rdt);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002116}
2117
2118/**
2119 * e1000_clean_all_rx_rings - Free Rx Buffers for all queues
2120 * @adapter: board private structure
2121 **/
2122
Joe Perches64798842008-07-11 15:17:02 -07002123static void e1000_clean_all_rx_rings(struct e1000_adapter *adapter)
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002124{
2125 int i;
2126
Jeff Kirsherf56799e2006-01-12 16:50:39 -08002127 for (i = 0; i < adapter->num_rx_queues; i++)
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002128 e1000_clean_rx_ring(adapter, &adapter->rx_ring[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129}
2130
2131/* The 82542 2.0 (revision 2) needs to have the receive unit in reset
2132 * and memory write and invalidate disabled for certain operations
2133 */
Joe Perches64798842008-07-11 15:17:02 -07002134static void e1000_enter_82542_rst(struct e1000_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135{
Joe Perches1dc32912008-07-11 15:17:08 -07002136 struct e1000_hw *hw = &adapter->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 struct net_device *netdev = adapter->netdev;
Joe Perches406874a2008-04-03 10:06:32 -07002138 u32 rctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139
Joe Perches1dc32912008-07-11 15:17:08 -07002140 e1000_pci_clear_mwi(hw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141
Joe Perches1dc32912008-07-11 15:17:08 -07002142 rctl = er32(RCTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 rctl |= E1000_RCTL_RST;
Joe Perches1dc32912008-07-11 15:17:08 -07002144 ew32(RCTL, rctl);
2145 E1000_WRITE_FLUSH();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 mdelay(5);
2147
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002148 if (netif_running(netdev))
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002149 e1000_clean_all_rx_rings(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150}
2151
Joe Perches64798842008-07-11 15:17:02 -07002152static void e1000_leave_82542_rst(struct e1000_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153{
Joe Perches1dc32912008-07-11 15:17:08 -07002154 struct e1000_hw *hw = &adapter->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 struct net_device *netdev = adapter->netdev;
Joe Perches406874a2008-04-03 10:06:32 -07002156 u32 rctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157
Joe Perches1dc32912008-07-11 15:17:08 -07002158 rctl = er32(RCTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 rctl &= ~E1000_RCTL_RST;
Joe Perches1dc32912008-07-11 15:17:08 -07002160 ew32(RCTL, rctl);
2161 E1000_WRITE_FLUSH();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 mdelay(5);
2163
Joe Perches1dc32912008-07-11 15:17:08 -07002164 if (hw->pci_cmd_word & PCI_COMMAND_INVALIDATE)
2165 e1000_pci_set_mwi(hw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002167 if (netif_running(netdev)) {
Jeff Kirsher72d64a42006-01-12 16:51:19 -08002168 /* No need to loop, because 82542 supports only 1 queue */
2169 struct e1000_rx_ring *ring = &adapter->rx_ring[0];
Jesse Brandeburg7c4d3362006-01-18 13:01:45 -08002170 e1000_configure_rx(adapter);
Jeff Kirsher72d64a42006-01-12 16:51:19 -08002171 adapter->alloc_rx_buf(adapter, ring, E1000_DESC_UNUSED(ring));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 }
2173}
2174
2175/**
2176 * e1000_set_mac - Change the Ethernet Address of the NIC
2177 * @netdev: network interface device structure
2178 * @p: pointer to an address structure
2179 *
2180 * Returns 0 on success, negative on failure
2181 **/
2182
Joe Perches64798842008-07-11 15:17:02 -07002183static int e1000_set_mac(struct net_device *netdev, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184{
Malli Chilakala60490fe2005-06-17 17:41:45 -07002185 struct e1000_adapter *adapter = netdev_priv(netdev);
Joe Perches1dc32912008-07-11 15:17:08 -07002186 struct e1000_hw *hw = &adapter->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 struct sockaddr *addr = p;
2188
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002189 if (!is_valid_ether_addr(addr->sa_data))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 return -EADDRNOTAVAIL;
2191
2192 /* 82542 2.0 needs to be in reset to write receive address registers */
2193
Joe Perches1dc32912008-07-11 15:17:08 -07002194 if (hw->mac_type == e1000_82542_rev2_0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 e1000_enter_82542_rst(adapter);
2196
2197 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
Joe Perches1dc32912008-07-11 15:17:08 -07002198 memcpy(hw->mac_addr, addr->sa_data, netdev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199
Joe Perches1dc32912008-07-11 15:17:08 -07002200 e1000_rar_set(hw, hw->mac_addr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201
Joe Perches1dc32912008-07-11 15:17:08 -07002202 if (hw->mac_type == e1000_82542_rev2_0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 e1000_leave_82542_rst(adapter);
2204
2205 return 0;
2206}
2207
2208/**
Patrick McHardydb0ce502007-11-13 20:54:59 -08002209 * e1000_set_rx_mode - Secondary Unicast, Multicast and Promiscuous mode set
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 * @netdev: network interface device structure
2211 *
Patrick McHardydb0ce502007-11-13 20:54:59 -08002212 * The set_rx_mode entry point is called whenever the unicast or multicast
2213 * address lists or the network interface flags are updated. This routine is
2214 * responsible for configuring the hardware for proper unicast, multicast,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 * promiscuous mode, and all-multi behavior.
2216 **/
2217
Joe Perches64798842008-07-11 15:17:02 -07002218static void e1000_set_rx_mode(struct net_device *netdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219{
Malli Chilakala60490fe2005-06-17 17:41:45 -07002220 struct e1000_adapter *adapter = netdev_priv(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 struct e1000_hw *hw = &adapter->hw;
Jiri Pirkoccffad22009-05-22 23:22:17 +00002222 struct netdev_hw_addr *ha;
2223 bool use_uc = false;
Joe Perches406874a2008-04-03 10:06:32 -07002224 u32 rctl;
2225 u32 hash_value;
Mallikarjuna R Chilakala868d5302005-10-04 06:58:59 -04002226 int i, rar_entries = E1000_RAR_ENTRIES;
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00002227 int mta_reg_count = E1000_NUM_MTA_REGISTERS;
Jesse Brandeburg81c52282009-04-04 16:36:53 -07002228 u32 *mcarray = kcalloc(mta_reg_count, sizeof(u32), GFP_ATOMIC);
2229
2230 if (!mcarray) {
Emil Tantilovfeb8f472010-07-26 23:37:21 -07002231 e_err(probe, "memory allocation failed\n");
Jesse Brandeburg81c52282009-04-04 16:36:53 -07002232 return;
2233 }
Auke Kokcd94dd02006-06-27 09:08:22 -07002234
Malli Chilakala26483452005-04-28 19:44:46 -07002235 /* Check for Promiscuous and All Multicast modes */
2236
Joe Perches1dc32912008-07-11 15:17:08 -07002237 rctl = er32(RCTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002239 if (netdev->flags & IFF_PROMISC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 rctl |= (E1000_RCTL_UPE | E1000_RCTL_MPE);
Patrick McHardy746b9f02008-07-16 20:15:45 -07002241 rctl &= ~E1000_RCTL_VFE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 } else {
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00002243 if (netdev->flags & IFF_ALLMULTI)
Patrick McHardy746b9f02008-07-16 20:15:45 -07002244 rctl |= E1000_RCTL_MPE;
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00002245 else
Patrick McHardy746b9f02008-07-16 20:15:45 -07002246 rctl &= ~E1000_RCTL_MPE;
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00002247 /* Enable VLAN filter if there is a VLAN */
Jiri Pirko5622e402011-07-21 03:26:31 +00002248 if (e1000_vlan_used(adapter))
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00002249 rctl |= E1000_RCTL_VFE;
Patrick McHardydb0ce502007-11-13 20:54:59 -08002250 }
2251
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08002252 if (netdev_uc_count(netdev) > rar_entries - 1) {
Patrick McHardydb0ce502007-11-13 20:54:59 -08002253 rctl |= E1000_RCTL_UPE;
2254 } else if (!(netdev->flags & IFF_PROMISC)) {
2255 rctl &= ~E1000_RCTL_UPE;
Jiri Pirkoccffad22009-05-22 23:22:17 +00002256 use_uc = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 }
2258
Joe Perches1dc32912008-07-11 15:17:08 -07002259 ew32(RCTL, rctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260
2261 /* 82542 2.0 needs to be in reset to write receive address registers */
2262
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002263 if (hw->mac_type == e1000_82542_rev2_0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 e1000_enter_82542_rst(adapter);
2265
Patrick McHardydb0ce502007-11-13 20:54:59 -08002266 /* load the first 14 addresses into the exact filters 1-14. Unicast
2267 * addresses take precedence to avoid disabling unicast filtering
2268 * when possible.
2269 *
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04002270 * RAR 0 is used for the station MAC address
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 * if there are not 14 addresses, go ahead and clear the filters
2272 */
Jiri Pirkoccffad22009-05-22 23:22:17 +00002273 i = 1;
2274 if (use_uc)
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08002275 netdev_for_each_uc_addr(ha, netdev) {
Jiri Pirkoccffad22009-05-22 23:22:17 +00002276 if (i == rar_entries)
2277 break;
2278 e1000_rar_set(hw, ha->addr, i++);
2279 }
2280
Jiri Pirko22bedad2010-04-01 21:22:57 +00002281 netdev_for_each_mc_addr(ha, netdev) {
Jiri Pirko7a81e9f2010-02-22 09:10:44 +00002282 if (i == rar_entries) {
2283 /* load any remaining addresses into the hash table */
2284 u32 hash_reg, hash_bit, mta;
Jiri Pirko22bedad2010-04-01 21:22:57 +00002285 hash_value = e1000_hash_mc_addr(hw, ha->addr);
Jiri Pirko7a81e9f2010-02-22 09:10:44 +00002286 hash_reg = (hash_value >> 5) & 0x7F;
2287 hash_bit = hash_value & 0x1F;
2288 mta = (1 << hash_bit);
2289 mcarray[hash_reg] |= mta;
Jiri Pirko10886af2010-02-23 01:19:22 -08002290 } else {
Jiri Pirko22bedad2010-04-01 21:22:57 +00002291 e1000_rar_set(hw, ha->addr, i++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 }
2293 }
2294
Jiri Pirko7a81e9f2010-02-22 09:10:44 +00002295 for (; i < rar_entries; i++) {
2296 E1000_WRITE_REG_ARRAY(hw, RA, i << 1, 0);
2297 E1000_WRITE_FLUSH();
2298 E1000_WRITE_REG_ARRAY(hw, RA, (i << 1) + 1, 0);
2299 E1000_WRITE_FLUSH();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 }
2301
Jesse Brandeburg81c52282009-04-04 16:36:53 -07002302 /* write the hash table completely, write from bottom to avoid
2303 * both stupid write combining chipsets, and flushing each write */
2304 for (i = mta_reg_count - 1; i >= 0 ; i--) {
2305 /*
2306 * If we are on an 82544 has an errata where writing odd
2307 * offsets overwrites the previous even offset, but writing
2308 * backwards over the range solves the issue by always
2309 * writing the odd offset first
2310 */
2311 E1000_WRITE_REG_ARRAY(hw, MTA, i, mcarray[i]);
2312 }
2313 E1000_WRITE_FLUSH();
2314
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002315 if (hw->mac_type == e1000_82542_rev2_0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 e1000_leave_82542_rst(adapter);
Jesse Brandeburg81c52282009-04-04 16:36:53 -07002317
2318 kfree(mcarray);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319}
2320
Jesse Brandeburga4010af2011-10-05 07:24:41 +00002321/**
2322 * e1000_update_phy_info_task - get phy info
2323 * @work: work struct contained inside adapter struct
2324 *
2325 * Need to wait a few seconds after link up to get diagnostic information from
2326 * the phy
2327 */
Jesse Brandeburg5cf42fc2010-09-22 18:22:17 +00002328static void e1000_update_phy_info_task(struct work_struct *work)
2329{
2330 struct e1000_adapter *adapter = container_of(work,
Jesse Brandeburga4010af2011-10-05 07:24:41 +00002331 struct e1000_adapter,
2332 phy_info_task.work);
Jesse Brandeburg0ef4eed2011-10-05 07:24:51 +00002333 if (test_bit(__E1000_DOWN, &adapter->flags))
2334 return;
2335 mutex_lock(&adapter->mutex);
Jesse Brandeburga4010af2011-10-05 07:24:41 +00002336 e1000_phy_get_info(&adapter->hw, &adapter->phy_info);
Jesse Brandeburg0ef4eed2011-10-05 07:24:51 +00002337 mutex_unlock(&adapter->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338}
2339
2340/**
Jesse Brandeburg5cf42fc2010-09-22 18:22:17 +00002341 * e1000_82547_tx_fifo_stall_task - task to complete work
2342 * @work: work struct contained inside adapter struct
2343 **/
2344static void e1000_82547_tx_fifo_stall_task(struct work_struct *work)
2345{
2346 struct e1000_adapter *adapter = container_of(work,
Jesse Brandeburga4010af2011-10-05 07:24:41 +00002347 struct e1000_adapter,
2348 fifo_stall_task.work);
Joe Perches1dc32912008-07-11 15:17:08 -07002349 struct e1000_hw *hw = &adapter->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 struct net_device *netdev = adapter->netdev;
Joe Perches406874a2008-04-03 10:06:32 -07002351 u32 tctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352
Jesse Brandeburg0ef4eed2011-10-05 07:24:51 +00002353 if (test_bit(__E1000_DOWN, &adapter->flags))
2354 return;
2355 mutex_lock(&adapter->mutex);
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002356 if (atomic_read(&adapter->tx_fifo_stall)) {
Joe Perches1dc32912008-07-11 15:17:08 -07002357 if ((er32(TDT) == er32(TDH)) &&
2358 (er32(TDFT) == er32(TDFH)) &&
2359 (er32(TDFTS) == er32(TDFHS))) {
2360 tctl = er32(TCTL);
2361 ew32(TCTL, tctl & ~E1000_TCTL_EN);
2362 ew32(TDFT, adapter->tx_head_addr);
2363 ew32(TDFH, adapter->tx_head_addr);
2364 ew32(TDFTS, adapter->tx_head_addr);
2365 ew32(TDFHS, adapter->tx_head_addr);
2366 ew32(TCTL, tctl);
2367 E1000_WRITE_FLUSH();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368
2369 adapter->tx_fifo_head = 0;
2370 atomic_set(&adapter->tx_fifo_stall, 0);
2371 netif_wake_queue(netdev);
Jesse Brandeburgbaa34742009-09-25 12:17:23 +00002372 } else if (!test_bit(__E1000_DOWN, &adapter->flags)) {
Jesse Brandeburga4010af2011-10-05 07:24:41 +00002373 schedule_delayed_work(&adapter->fifo_stall_task, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 }
2375 }
Jesse Brandeburg0ef4eed2011-10-05 07:24:51 +00002376 mutex_unlock(&adapter->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377}
2378
Nick Nunleyb5481922010-02-03 14:49:28 +00002379bool e1000_has_link(struct e1000_adapter *adapter)
Jesse Brandeburgbe0f0712009-09-25 12:17:44 +00002380{
2381 struct e1000_hw *hw = &adapter->hw;
2382 bool link_active = false;
Jesse Brandeburgbe0f0712009-09-25 12:17:44 +00002383
Nicolas Schichan6d9e5132011-07-09 00:24:18 +00002384 /* get_link_status is set on LSC (link status) interrupt or rx
2385 * sequence error interrupt (except on intel ce4100).
2386 * get_link_status will stay false until the
2387 * e1000_check_for_link establishes link for copper adapters
2388 * ONLY
Jesse Brandeburgbe0f0712009-09-25 12:17:44 +00002389 */
2390 switch (hw->media_type) {
2391 case e1000_media_type_copper:
Nicolas Schichan6d9e5132011-07-09 00:24:18 +00002392 if (hw->mac_type == e1000_ce4100)
2393 hw->get_link_status = 1;
Jesse Brandeburgbe0f0712009-09-25 12:17:44 +00002394 if (hw->get_link_status) {
Jesse Brandeburg120a5d02009-09-25 15:19:46 -07002395 e1000_check_for_link(hw);
Jesse Brandeburgbe0f0712009-09-25 12:17:44 +00002396 link_active = !hw->get_link_status;
2397 } else {
2398 link_active = true;
2399 }
2400 break;
2401 case e1000_media_type_fiber:
Jesse Brandeburg120a5d02009-09-25 15:19:46 -07002402 e1000_check_for_link(hw);
Jesse Brandeburgbe0f0712009-09-25 12:17:44 +00002403 link_active = !!(er32(STATUS) & E1000_STATUS_LU);
2404 break;
2405 case e1000_media_type_internal_serdes:
Jesse Brandeburg120a5d02009-09-25 15:19:46 -07002406 e1000_check_for_link(hw);
Jesse Brandeburgbe0f0712009-09-25 12:17:44 +00002407 link_active = hw->serdes_has_link;
2408 break;
2409 default:
2410 break;
2411 }
2412
2413 return link_active;
2414}
2415
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416/**
Jesse Brandeburga4010af2011-10-05 07:24:41 +00002417 * e1000_watchdog - work function
2418 * @work: work struct contained inside adapter struct
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 **/
Jesse Brandeburga4010af2011-10-05 07:24:41 +00002420static void e1000_watchdog(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421{
Jesse Brandeburga4010af2011-10-05 07:24:41 +00002422 struct e1000_adapter *adapter = container_of(work,
2423 struct e1000_adapter,
2424 watchdog_task.work);
Joe Perches1dc32912008-07-11 15:17:08 -07002425 struct e1000_hw *hw = &adapter->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 struct net_device *netdev = adapter->netdev;
Jeff Kirsher545c67c2006-01-12 16:50:25 -08002427 struct e1000_tx_ring *txdr = adapter->tx_ring;
Joe Perches406874a2008-04-03 10:06:32 -07002428 u32 link, tctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429
Jesse Brandeburg0ef4eed2011-10-05 07:24:51 +00002430 if (test_bit(__E1000_DOWN, &adapter->flags))
2431 return;
2432
2433 mutex_lock(&adapter->mutex);
Jesse Brandeburgbe0f0712009-09-25 12:17:44 +00002434 link = e1000_has_link(adapter);
2435 if ((netif_carrier_ok(netdev)) && link)
2436 goto link_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002438 if (link) {
2439 if (!netif_carrier_ok(netdev)) {
Joe Perches406874a2008-04-03 10:06:32 -07002440 u32 ctrl;
Joe Perchesc3033b02008-03-21 11:06:25 -07002441 bool txb2b = true;
Jesse Brandeburgbe0f0712009-09-25 12:17:44 +00002442 /* update snapshot of PHY registers on LSC */
Joe Perches1dc32912008-07-11 15:17:08 -07002443 e1000_get_speed_and_duplex(hw,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 &adapter->link_speed,
2445 &adapter->link_duplex);
2446
Joe Perches1dc32912008-07-11 15:17:08 -07002447 ctrl = er32(CTRL);
Emil Tantilov675ad472010-04-27 14:02:58 +00002448 pr_info("%s NIC Link is Up %d Mbps %s, "
2449 "Flow Control: %s\n",
2450 netdev->name,
2451 adapter->link_speed,
2452 adapter->link_duplex == FULL_DUPLEX ?
2453 "Full Duplex" : "Half Duplex",
2454 ((ctrl & E1000_CTRL_TFCE) && (ctrl &
2455 E1000_CTRL_RFCE)) ? "RX/TX" : ((ctrl &
2456 E1000_CTRL_RFCE) ? "RX" : ((ctrl &
2457 E1000_CTRL_TFCE) ? "TX" : "None")));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458
Emil Tantilov39ca5f02010-03-26 11:25:58 +00002459 /* adjust timeout factor according to speed/duplex */
Jeff Kirsher66a2b0a2006-01-12 16:50:53 -08002460 adapter->tx_timeout_factor = 1;
Jeff Kirsher7e6c9862006-03-02 18:19:30 -08002461 switch (adapter->link_speed) {
2462 case SPEED_10:
Joe Perchesc3033b02008-03-21 11:06:25 -07002463 txb2b = false;
Jesse Brandeburgbe0f0712009-09-25 12:17:44 +00002464 adapter->tx_timeout_factor = 16;
Jeff Kirsher7e6c9862006-03-02 18:19:30 -08002465 break;
2466 case SPEED_100:
Joe Perchesc3033b02008-03-21 11:06:25 -07002467 txb2b = false;
Jeff Kirsher7e6c9862006-03-02 18:19:30 -08002468 /* maybe add some timeout factor ? */
2469 break;
2470 }
2471
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00002472 /* enable transmits in the hardware */
Joe Perches1dc32912008-07-11 15:17:08 -07002473 tctl = er32(TCTL);
Jeff Kirsher7e6c9862006-03-02 18:19:30 -08002474 tctl |= E1000_TCTL_EN;
Joe Perches1dc32912008-07-11 15:17:08 -07002475 ew32(TCTL, tctl);
Jeff Kirsher66a2b0a2006-01-12 16:50:53 -08002476
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 netif_carrier_on(netdev);
Jesse Brandeburgbaa34742009-09-25 12:17:23 +00002478 if (!test_bit(__E1000_DOWN, &adapter->flags))
Jesse Brandeburga4010af2011-10-05 07:24:41 +00002479 schedule_delayed_work(&adapter->phy_info_task,
2480 2 * HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 adapter->smartspeed = 0;
2482 }
2483 } else {
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002484 if (netif_carrier_ok(netdev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 adapter->link_speed = 0;
2486 adapter->link_duplex = 0;
Emil Tantilov675ad472010-04-27 14:02:58 +00002487 pr_info("%s NIC Link is Down\n",
2488 netdev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 netif_carrier_off(netdev);
Jesse Brandeburgbaa34742009-09-25 12:17:23 +00002490
2491 if (!test_bit(__E1000_DOWN, &adapter->flags))
Jesse Brandeburga4010af2011-10-05 07:24:41 +00002492 schedule_delayed_work(&adapter->phy_info_task,
2493 2 * HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 }
2495
2496 e1000_smartspeed(adapter);
2497 }
2498
Jesse Brandeburgbe0f0712009-09-25 12:17:44 +00002499link_up:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 e1000_update_stats(adapter);
2501
Joe Perches1dc32912008-07-11 15:17:08 -07002502 hw->tx_packet_delta = adapter->stats.tpt - adapter->tpt_old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 adapter->tpt_old = adapter->stats.tpt;
Joe Perches1dc32912008-07-11 15:17:08 -07002504 hw->collision_delta = adapter->stats.colc - adapter->colc_old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 adapter->colc_old = adapter->stats.colc;
2506
2507 adapter->gorcl = adapter->stats.gorcl - adapter->gorcl_old;
2508 adapter->gorcl_old = adapter->stats.gorcl;
2509 adapter->gotcl = adapter->stats.gotcl - adapter->gotcl_old;
2510 adapter->gotcl_old = adapter->stats.gotcl;
2511
Joe Perches1dc32912008-07-11 15:17:08 -07002512 e1000_update_adaptive(hw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513
Jeff Kirsherf56799e2006-01-12 16:50:39 -08002514 if (!netif_carrier_ok(netdev)) {
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002515 if (E1000_DESC_UNUSED(txdr) + 1 < txdr->count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 /* We've lost link, so the controller stops DMA,
2517 * but we've got queued Tx work that's never going
2518 * to get done, so reset controller to flush Tx.
2519 * (Do the reset outside of interrupt context). */
Jeff Kirsher87041632006-03-02 18:21:24 -08002520 adapter->tx_timeout_count++;
2521 schedule_work(&adapter->reset_task);
Jesse Brandeburg0ef4eed2011-10-05 07:24:51 +00002522 /* exit immediately since reset is imminent */
2523 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 }
2525 }
2526
Jesse Brandeburgeab2abf2010-05-04 22:26:03 +00002527 /* Simple mode for Interrupt Throttle Rate (ITR) */
2528 if (hw->mac_type >= e1000_82540 && adapter->itr_setting == 4) {
2529 /*
2530 * Symmetric Tx/Rx gets a reduced ITR=2000;
2531 * Total asymmetrical Tx or Rx gets ITR=8000;
2532 * everyone else is between 2000-8000.
2533 */
2534 u32 goc = (adapter->gotcl + adapter->gorcl) / 10000;
2535 u32 dif = (adapter->gotcl > adapter->gorcl ?
2536 adapter->gotcl - adapter->gorcl :
2537 adapter->gorcl - adapter->gotcl) / 10000;
2538 u32 itr = goc > 0 ? (dif * 6000 / goc + 2000) : 8000;
2539
2540 ew32(ITR, 1000000000 / (itr * 256));
2541 }
2542
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 /* Cause software interrupt to ensure rx ring is cleaned */
Joe Perches1dc32912008-07-11 15:17:08 -07002544 ew32(ICS, E1000_ICS_RXDMT0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545
Malli Chilakala26483452005-04-28 19:44:46 -07002546 /* Force detection of hung controller every watchdog period */
Joe Perchesc3033b02008-03-21 11:06:25 -07002547 adapter->detect_tx_hung = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548
Jesse Brandeburga4010af2011-10-05 07:24:41 +00002549 /* Reschedule the task */
Jesse Brandeburgbaa34742009-09-25 12:17:23 +00002550 if (!test_bit(__E1000_DOWN, &adapter->flags))
Jesse Brandeburga4010af2011-10-05 07:24:41 +00002551 schedule_delayed_work(&adapter->watchdog_task, 2 * HZ);
Jesse Brandeburg0ef4eed2011-10-05 07:24:51 +00002552
2553unlock:
2554 mutex_unlock(&adapter->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555}
2556
Jesse Brandeburg835bb122006-11-01 08:48:13 -08002557enum latency_range {
2558 lowest_latency = 0,
2559 low_latency = 1,
2560 bulk_latency = 2,
2561 latency_invalid = 255
2562};
2563
2564/**
2565 * e1000_update_itr - update the dynamic ITR value based on statistics
Jesse Brandeburg8fce4732009-09-25 12:18:41 +00002566 * @adapter: pointer to adapter
2567 * @itr_setting: current adapter->itr
2568 * @packets: the number of packets during this measurement interval
2569 * @bytes: the number of bytes during this measurement interval
2570 *
Jesse Brandeburg835bb122006-11-01 08:48:13 -08002571 * Stores a new ITR value based on packets and byte
2572 * counts during the last interrupt. The advantage of per interrupt
2573 * computation is faster updates and more accurate ITR for the current
2574 * traffic pattern. Constants in this function were computed
2575 * based on theoretical maximum wire speed and thresholds were set based
2576 * on testing data as well as attempting to minimize response time
2577 * while increasing bulk throughput.
2578 * this functionality is controlled by the InterruptThrottleRate module
2579 * parameter (see e1000_param.c)
Jesse Brandeburg835bb122006-11-01 08:48:13 -08002580 **/
2581static unsigned int e1000_update_itr(struct e1000_adapter *adapter,
Joe Perches64798842008-07-11 15:17:02 -07002582 u16 itr_setting, int packets, int bytes)
Jesse Brandeburg835bb122006-11-01 08:48:13 -08002583{
2584 unsigned int retval = itr_setting;
2585 struct e1000_hw *hw = &adapter->hw;
2586
2587 if (unlikely(hw->mac_type < e1000_82540))
2588 goto update_itr_done;
2589
2590 if (packets == 0)
2591 goto update_itr_done;
2592
Jesse Brandeburg835bb122006-11-01 08:48:13 -08002593 switch (itr_setting) {
2594 case lowest_latency:
Jesse Brandeburg2b653262006-12-15 10:30:44 +01002595 /* jumbo frames get bulk treatment*/
2596 if (bytes/packets > 8000)
2597 retval = bulk_latency;
2598 else if ((packets < 5) && (bytes > 512))
Jesse Brandeburg835bb122006-11-01 08:48:13 -08002599 retval = low_latency;
2600 break;
2601 case low_latency: /* 50 usec aka 20000 ints/s */
2602 if (bytes > 10000) {
Jesse Brandeburg2b653262006-12-15 10:30:44 +01002603 /* jumbo frames need bulk latency setting */
2604 if (bytes/packets > 8000)
2605 retval = bulk_latency;
2606 else if ((packets < 10) || ((bytes/packets) > 1200))
Jesse Brandeburg835bb122006-11-01 08:48:13 -08002607 retval = bulk_latency;
2608 else if ((packets > 35))
2609 retval = lowest_latency;
Jesse Brandeburg2b653262006-12-15 10:30:44 +01002610 } else if (bytes/packets > 2000)
2611 retval = bulk_latency;
2612 else if (packets <= 2 && bytes < 512)
Jesse Brandeburg835bb122006-11-01 08:48:13 -08002613 retval = lowest_latency;
2614 break;
2615 case bulk_latency: /* 250 usec aka 4000 ints/s */
2616 if (bytes > 25000) {
2617 if (packets > 35)
2618 retval = low_latency;
Jesse Brandeburg2b653262006-12-15 10:30:44 +01002619 } else if (bytes < 6000) {
2620 retval = low_latency;
Jesse Brandeburg835bb122006-11-01 08:48:13 -08002621 }
2622 break;
2623 }
2624
2625update_itr_done:
2626 return retval;
2627}
2628
2629static void e1000_set_itr(struct e1000_adapter *adapter)
2630{
2631 struct e1000_hw *hw = &adapter->hw;
Joe Perches406874a2008-04-03 10:06:32 -07002632 u16 current_itr;
2633 u32 new_itr = adapter->itr;
Jesse Brandeburg835bb122006-11-01 08:48:13 -08002634
2635 if (unlikely(hw->mac_type < e1000_82540))
2636 return;
2637
2638 /* for non-gigabit speeds, just fix the interrupt rate at 4000 */
2639 if (unlikely(adapter->link_speed != SPEED_1000)) {
2640 current_itr = 0;
2641 new_itr = 4000;
2642 goto set_itr_now;
2643 }
2644
2645 adapter->tx_itr = e1000_update_itr(adapter,
2646 adapter->tx_itr,
2647 adapter->total_tx_packets,
2648 adapter->total_tx_bytes);
Jesse Brandeburg2b653262006-12-15 10:30:44 +01002649 /* conservative mode (itr 3) eliminates the lowest_latency setting */
2650 if (adapter->itr_setting == 3 && adapter->tx_itr == lowest_latency)
2651 adapter->tx_itr = low_latency;
2652
Jesse Brandeburg835bb122006-11-01 08:48:13 -08002653 adapter->rx_itr = e1000_update_itr(adapter,
2654 adapter->rx_itr,
2655 adapter->total_rx_packets,
2656 adapter->total_rx_bytes);
Jesse Brandeburg2b653262006-12-15 10:30:44 +01002657 /* conservative mode (itr 3) eliminates the lowest_latency setting */
2658 if (adapter->itr_setting == 3 && adapter->rx_itr == lowest_latency)
2659 adapter->rx_itr = low_latency;
Jesse Brandeburg835bb122006-11-01 08:48:13 -08002660
2661 current_itr = max(adapter->rx_itr, adapter->tx_itr);
2662
Jesse Brandeburg835bb122006-11-01 08:48:13 -08002663 switch (current_itr) {
2664 /* counts and packets in update_itr are dependent on these numbers */
2665 case lowest_latency:
2666 new_itr = 70000;
2667 break;
2668 case low_latency:
2669 new_itr = 20000; /* aka hwitr = ~200 */
2670 break;
2671 case bulk_latency:
2672 new_itr = 4000;
2673 break;
2674 default:
2675 break;
2676 }
2677
2678set_itr_now:
2679 if (new_itr != adapter->itr) {
2680 /* this attempts to bias the interrupt rate towards Bulk
2681 * by adding intermediate steps when interrupt rate is
2682 * increasing */
2683 new_itr = new_itr > adapter->itr ?
2684 min(adapter->itr + (new_itr >> 2), new_itr) :
2685 new_itr;
2686 adapter->itr = new_itr;
Joe Perches1dc32912008-07-11 15:17:08 -07002687 ew32(ITR, 1000000000 / (new_itr * 256));
Jesse Brandeburg835bb122006-11-01 08:48:13 -08002688 }
Jesse Brandeburg835bb122006-11-01 08:48:13 -08002689}
2690
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691#define E1000_TX_FLAGS_CSUM 0x00000001
2692#define E1000_TX_FLAGS_VLAN 0x00000002
2693#define E1000_TX_FLAGS_TSO 0x00000004
Malli Chilakala2d7edb92005-04-28 19:43:52 -07002694#define E1000_TX_FLAGS_IPV4 0x00000008
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695#define E1000_TX_FLAGS_VLAN_MASK 0xffff0000
2696#define E1000_TX_FLAGS_VLAN_SHIFT 16
2697
Joe Perches64798842008-07-11 15:17:02 -07002698static int e1000_tso(struct e1000_adapter *adapter,
2699 struct e1000_tx_ring *tx_ring, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 struct e1000_context_desc *context_desc;
Jeff Kirsher545c67c2006-01-12 16:50:25 -08002702 struct e1000_buffer *buffer_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 unsigned int i;
Joe Perches406874a2008-04-03 10:06:32 -07002704 u32 cmd_length = 0;
2705 u16 ipcse = 0, tucse, mss;
2706 u8 ipcss, ipcso, tucss, tucso, hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 int err;
2708
Herbert Xu89114af2006-07-08 13:34:32 -07002709 if (skb_is_gso(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 if (skb_header_cloned(skb)) {
2711 err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2712 if (err)
2713 return err;
2714 }
2715
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07002716 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
Herbert Xu79671682006-06-22 02:40:14 -07002717 mss = skb_shinfo(skb)->gso_size;
Alexey Dobriyan60828232006-05-23 14:52:21 -07002718 if (skb->protocol == htons(ETH_P_IP)) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002719 struct iphdr *iph = ip_hdr(skb);
2720 iph->tot_len = 0;
2721 iph->check = 0;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07002722 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
2723 iph->daddr, 0,
2724 IPPROTO_TCP,
2725 0);
Malli Chilakala2d7edb92005-04-28 19:43:52 -07002726 cmd_length = E1000_TXD_CMD_IP;
Arnaldo Carvalho de Meloea2ae172007-04-25 17:55:53 -07002727 ipcse = skb_transport_offset(skb) - 1;
Auke Koke15fdd02006-08-16 11:28:45 -07002728 } else if (skb->protocol == htons(ETH_P_IPV6)) {
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07002729 ipv6_hdr(skb)->payload_len = 0;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07002730 tcp_hdr(skb)->check =
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07002731 ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
2732 &ipv6_hdr(skb)->daddr,
2733 0, IPPROTO_TCP, 0);
Malli Chilakala2d7edb92005-04-28 19:43:52 -07002734 ipcse = 0;
Malli Chilakala2d7edb92005-04-28 19:43:52 -07002735 }
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03002736 ipcss = skb_network_offset(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002737 ipcso = (void *)&(ip_hdr(skb)->check) - (void *)skb->data;
Arnaldo Carvalho de Meloea2ae172007-04-25 17:55:53 -07002738 tucss = skb_transport_offset(skb);
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07002739 tucso = (void *)&(tcp_hdr(skb)->check) - (void *)skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 tucse = 0;
2741
2742 cmd_length |= (E1000_TXD_CMD_DEXT | E1000_TXD_CMD_TSE |
Malli Chilakala2d7edb92005-04-28 19:43:52 -07002743 E1000_TXD_CMD_TCP | (skb->len - (hdr_len)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002745 i = tx_ring->next_to_use;
2746 context_desc = E1000_CONTEXT_DESC(*tx_ring, i);
Jeff Kirsher545c67c2006-01-12 16:50:25 -08002747 buffer_info = &tx_ring->buffer_info[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748
2749 context_desc->lower_setup.ip_fields.ipcss = ipcss;
2750 context_desc->lower_setup.ip_fields.ipcso = ipcso;
2751 context_desc->lower_setup.ip_fields.ipcse = cpu_to_le16(ipcse);
2752 context_desc->upper_setup.tcp_fields.tucss = tucss;
2753 context_desc->upper_setup.tcp_fields.tucso = tucso;
2754 context_desc->upper_setup.tcp_fields.tucse = cpu_to_le16(tucse);
2755 context_desc->tcp_seg_setup.fields.mss = cpu_to_le16(mss);
2756 context_desc->tcp_seg_setup.fields.hdr_len = hdr_len;
2757 context_desc->cmd_and_length = cpu_to_le32(cmd_length);
2758
Jeff Kirsher545c67c2006-01-12 16:50:25 -08002759 buffer_info->time_stamp = jiffies;
Jesse Brandeburga9ebadd2006-11-01 08:47:53 -08002760 buffer_info->next_to_watch = i;
Jeff Kirsher545c67c2006-01-12 16:50:25 -08002761
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002762 if (++i == tx_ring->count) i = 0;
2763 tx_ring->next_to_use = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764
Joe Perchesc3033b02008-03-21 11:06:25 -07002765 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 }
Joe Perchesc3033b02008-03-21 11:06:25 -07002767 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768}
2769
Joe Perches64798842008-07-11 15:17:02 -07002770static bool e1000_tx_csum(struct e1000_adapter *adapter,
2771 struct e1000_tx_ring *tx_ring, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772{
2773 struct e1000_context_desc *context_desc;
Jeff Kirsher545c67c2006-01-12 16:50:25 -08002774 struct e1000_buffer *buffer_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 unsigned int i;
Joe Perches406874a2008-04-03 10:06:32 -07002776 u8 css;
Dave Graham3ed30672008-10-09 14:29:26 -07002777 u32 cmd_len = E1000_TXD_CMD_DEXT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778
Dave Graham3ed30672008-10-09 14:29:26 -07002779 if (skb->ip_summed != CHECKSUM_PARTIAL)
2780 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781
Dave Graham3ed30672008-10-09 14:29:26 -07002782 switch (skb->protocol) {
Harvey Harrison09640e62009-02-01 00:45:17 -08002783 case cpu_to_be16(ETH_P_IP):
Dave Graham3ed30672008-10-09 14:29:26 -07002784 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
2785 cmd_len |= E1000_TXD_CMD_TCP;
2786 break;
Harvey Harrison09640e62009-02-01 00:45:17 -08002787 case cpu_to_be16(ETH_P_IPV6):
Dave Graham3ed30672008-10-09 14:29:26 -07002788 /* XXX not handling all IPV6 headers */
2789 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
2790 cmd_len |= E1000_TXD_CMD_TCP;
2791 break;
2792 default:
2793 if (unlikely(net_ratelimit()))
Emil Tantilovfeb8f472010-07-26 23:37:21 -07002794 e_warn(drv, "checksum_partial proto=%x!\n",
2795 skb->protocol);
Dave Graham3ed30672008-10-09 14:29:26 -07002796 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 }
2798
Michał Mirosław0d0b1672010-12-14 15:24:08 +00002799 css = skb_checksum_start_offset(skb);
Dave Graham3ed30672008-10-09 14:29:26 -07002800
2801 i = tx_ring->next_to_use;
2802 buffer_info = &tx_ring->buffer_info[i];
2803 context_desc = E1000_CONTEXT_DESC(*tx_ring, i);
2804
2805 context_desc->lower_setup.ip_config = 0;
2806 context_desc->upper_setup.tcp_fields.tucss = css;
2807 context_desc->upper_setup.tcp_fields.tucso =
2808 css + skb->csum_offset;
2809 context_desc->upper_setup.tcp_fields.tucse = 0;
2810 context_desc->tcp_seg_setup.data = 0;
2811 context_desc->cmd_and_length = cpu_to_le32(cmd_len);
2812
2813 buffer_info->time_stamp = jiffies;
2814 buffer_info->next_to_watch = i;
2815
2816 if (unlikely(++i == tx_ring->count)) i = 0;
2817 tx_ring->next_to_use = i;
2818
2819 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820}
2821
2822#define E1000_MAX_TXD_PWR 12
2823#define E1000_MAX_DATA_PER_TXD (1<<E1000_MAX_TXD_PWR)
2824
Joe Perches64798842008-07-11 15:17:02 -07002825static int e1000_tx_map(struct e1000_adapter *adapter,
2826 struct e1000_tx_ring *tx_ring,
2827 struct sk_buff *skb, unsigned int first,
2828 unsigned int max_per_txd, unsigned int nr_frags,
2829 unsigned int mss)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830{
Joe Perches1dc32912008-07-11 15:17:08 -07002831 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck602c0552009-12-02 16:46:00 +00002832 struct pci_dev *pdev = adapter->pdev;
Alexander Duyck37e73df2009-03-25 21:58:45 +00002833 struct e1000_buffer *buffer_info;
Jesse Brandeburgd20b6062009-03-02 16:03:21 -08002834 unsigned int len = skb_headlen(skb);
Alexander Duyck602c0552009-12-02 16:46:00 +00002835 unsigned int offset = 0, size, count = 0, i;
Dean Nelson31c15a22011-08-25 14:39:24 +00002836 unsigned int f, bytecount, segs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837
2838 i = tx_ring->next_to_use;
2839
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002840 while (len) {
Alexander Duyck37e73df2009-03-25 21:58:45 +00002841 buffer_info = &tx_ring->buffer_info[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 size = min(len, max_per_txd);
Jeff Kirsherfd803242005-12-13 00:06:22 -05002843 /* Workaround for Controller erratum --
2844 * descriptor for non-tso packet in a linear SKB that follows a
2845 * tso gets written back prematurely before the data is fully
Jeff Kirsher0f15a8f2006-03-02 18:46:29 -08002846 * DMA'd to the controller */
Jeff Kirsherfd803242005-12-13 00:06:22 -05002847 if (!skb->data_len && tx_ring->last_tx_tso &&
Herbert Xu89114af2006-07-08 13:34:32 -07002848 !skb_is_gso(skb)) {
Rusty Russell3db1cd52011-12-19 13:56:45 +00002849 tx_ring->last_tx_tso = false;
Jeff Kirsherfd803242005-12-13 00:06:22 -05002850 size -= 4;
2851 }
2852
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 /* Workaround for premature desc write-backs
2854 * in TSO mode. Append 4-byte sentinel desc */
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002855 if (unlikely(mss && !nr_frags && size == len && size > 8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 size -= 4;
Malli Chilakala97338bd2005-04-28 19:41:46 -07002857 /* work-around for errata 10 and it applies
2858 * to all controllers in PCI-X mode
2859 * The fix is to make sure that the first descriptor of a
2860 * packet is smaller than 2048 - 16 - 16 (or 2016) bytes
2861 */
Joe Perches1dc32912008-07-11 15:17:08 -07002862 if (unlikely((hw->bus_type == e1000_bus_type_pcix) &&
Malli Chilakala97338bd2005-04-28 19:41:46 -07002863 (size > 2015) && count == 0))
2864 size = 2015;
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002865
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 /* Workaround for potential 82544 hang in PCI-X. Avoid
2867 * terminating buffers within evenly-aligned dwords. */
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002868 if (unlikely(adapter->pcix_82544 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 !((unsigned long)(skb->data + offset + size - 1) & 4) &&
2870 size > 4))
2871 size -= 4;
2872
2873 buffer_info->length = size;
Jesse Brandeburgcdd75492009-09-25 12:18:07 +00002874 /* set time_stamp *before* dma to help avoid a possible race */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 buffer_info->time_stamp = jiffies;
Alexander Duyck602c0552009-12-02 16:46:00 +00002876 buffer_info->mapped_as_page = false;
Nick Nunleyb16f53b2010-04-27 13:08:45 +00002877 buffer_info->dma = dma_map_single(&pdev->dev,
2878 skb->data + offset,
2879 size, DMA_TO_DEVICE);
2880 if (dma_mapping_error(&pdev->dev, buffer_info->dma))
Alexander Duyck602c0552009-12-02 16:46:00 +00002881 goto dma_error;
Jesse Brandeburga9ebadd2006-11-01 08:47:53 -08002882 buffer_info->next_to_watch = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883
2884 len -= size;
2885 offset += size;
2886 count++;
Alexander Duyck37e73df2009-03-25 21:58:45 +00002887 if (len) {
2888 i++;
2889 if (unlikely(i == tx_ring->count))
2890 i = 0;
2891 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 }
2893
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002894 for (f = 0; f < nr_frags; f++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00002895 const struct skb_frag_struct *frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896
2897 frag = &skb_shinfo(skb)->frags[f];
Eric Dumazet9e903e02011-10-18 21:00:24 +00002898 len = skb_frag_size(frag);
Ian Campbell877749b2011-08-29 23:18:26 +00002899 offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002901 while (len) {
Ian Campbell877749b2011-08-29 23:18:26 +00002902 unsigned long bufend;
Alexander Duyck37e73df2009-03-25 21:58:45 +00002903 i++;
2904 if (unlikely(i == tx_ring->count))
2905 i = 0;
2906
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 buffer_info = &tx_ring->buffer_info[i];
2908 size = min(len, max_per_txd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 /* Workaround for premature desc write-backs
2910 * in TSO mode. Append 4-byte sentinel desc */
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002911 if (unlikely(mss && f == (nr_frags-1) && size == len && size > 8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 size -= 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 /* Workaround for potential 82544 hang in PCI-X.
2914 * Avoid terminating buffers within evenly-aligned
2915 * dwords. */
Ian Campbell877749b2011-08-29 23:18:26 +00002916 bufend = (unsigned long)
2917 page_to_phys(skb_frag_page(frag));
2918 bufend += offset + size - 1;
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002919 if (unlikely(adapter->pcix_82544 &&
Ian Campbell877749b2011-08-29 23:18:26 +00002920 !(bufend & 4) &&
2921 size > 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 size -= 4;
2923
2924 buffer_info->length = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 buffer_info->time_stamp = jiffies;
Alexander Duyck602c0552009-12-02 16:46:00 +00002926 buffer_info->mapped_as_page = true;
Ian Campbell877749b2011-08-29 23:18:26 +00002927 buffer_info->dma = skb_frag_dma_map(&pdev->dev, frag,
2928 offset, size, DMA_TO_DEVICE);
Nick Nunleyb16f53b2010-04-27 13:08:45 +00002929 if (dma_mapping_error(&pdev->dev, buffer_info->dma))
Alexander Duyck602c0552009-12-02 16:46:00 +00002930 goto dma_error;
Jesse Brandeburga9ebadd2006-11-01 08:47:53 -08002931 buffer_info->next_to_watch = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932
2933 len -= size;
2934 offset += size;
2935 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 }
2937 }
2938
Dean Nelson31c15a22011-08-25 14:39:24 +00002939 segs = skb_shinfo(skb)->gso_segs ?: 1;
2940 /* multiply data chunks by size of headers */
2941 bytecount = ((segs - 1) * skb_headlen(skb)) + skb->len;
2942
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 tx_ring->buffer_info[i].skb = skb;
Dean Nelson31c15a22011-08-25 14:39:24 +00002944 tx_ring->buffer_info[i].segs = segs;
2945 tx_ring->buffer_info[i].bytecount = bytecount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 tx_ring->buffer_info[first].next_to_watch = i;
2947
2948 return count;
Alexander Duyck602c0552009-12-02 16:46:00 +00002949
2950dma_error:
2951 dev_err(&pdev->dev, "TX DMA map failed\n");
2952 buffer_info->dma = 0;
Roel Kluinc1fa3472010-01-19 14:21:45 +00002953 if (count)
Alexander Duyck602c0552009-12-02 16:46:00 +00002954 count--;
Roel Kluinc1fa3472010-01-19 14:21:45 +00002955
2956 while (count--) {
2957 if (i==0)
Alexander Duyck602c0552009-12-02 16:46:00 +00002958 i += tx_ring->count;
Roel Kluinc1fa3472010-01-19 14:21:45 +00002959 i--;
Alexander Duyck602c0552009-12-02 16:46:00 +00002960 buffer_info = &tx_ring->buffer_info[i];
2961 e1000_unmap_and_free_tx_resource(adapter, buffer_info);
2962 }
2963
2964 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965}
2966
Joe Perches64798842008-07-11 15:17:02 -07002967static void e1000_tx_queue(struct e1000_adapter *adapter,
2968 struct e1000_tx_ring *tx_ring, int tx_flags,
2969 int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970{
Joe Perches1dc32912008-07-11 15:17:08 -07002971 struct e1000_hw *hw = &adapter->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 struct e1000_tx_desc *tx_desc = NULL;
2973 struct e1000_buffer *buffer_info;
Joe Perches406874a2008-04-03 10:06:32 -07002974 u32 txd_upper = 0, txd_lower = E1000_TXD_CMD_IFCS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 unsigned int i;
2976
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002977 if (likely(tx_flags & E1000_TX_FLAGS_TSO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 txd_lower |= E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D |
2979 E1000_TXD_CMD_TSE;
Malli Chilakala2d7edb92005-04-28 19:43:52 -07002980 txd_upper |= E1000_TXD_POPTS_TXSM << 8;
2981
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002982 if (likely(tx_flags & E1000_TX_FLAGS_IPV4))
Malli Chilakala2d7edb92005-04-28 19:43:52 -07002983 txd_upper |= E1000_TXD_POPTS_IXSM << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 }
2985
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002986 if (likely(tx_flags & E1000_TX_FLAGS_CSUM)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 txd_lower |= E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D;
2988 txd_upper |= E1000_TXD_POPTS_TXSM << 8;
2989 }
2990
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002991 if (unlikely(tx_flags & E1000_TX_FLAGS_VLAN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 txd_lower |= E1000_TXD_CMD_VLE;
2993 txd_upper |= (tx_flags & E1000_TX_FLAGS_VLAN_MASK);
2994 }
2995
2996 i = tx_ring->next_to_use;
2997
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002998 while (count--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 buffer_info = &tx_ring->buffer_info[i];
3000 tx_desc = E1000_TX_DESC(*tx_ring, i);
3001 tx_desc->buffer_addr = cpu_to_le64(buffer_info->dma);
3002 tx_desc->lower.data =
3003 cpu_to_le32(txd_lower | buffer_info->length);
3004 tx_desc->upper.data = cpu_to_le32(txd_upper);
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003005 if (unlikely(++i == tx_ring->count)) i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 }
3007
3008 tx_desc->lower.data |= cpu_to_le32(adapter->txd_cmd);
3009
3010 /* Force memory writes to complete before letting h/w
3011 * know there are new descriptors to fetch. (Only
3012 * applicable for weak-ordered memory model archs,
3013 * such as IA-64). */
3014 wmb();
3015
3016 tx_ring->next_to_use = i;
Joe Perches1dc32912008-07-11 15:17:08 -07003017 writel(i, hw->hw_addr + tx_ring->tdt);
Jesse Brandeburg2ce90472006-11-01 08:47:42 -08003018 /* we need this if more than one processor can write to our tail
3019 * at a time, it syncronizes IO on IA64/Altix systems */
3020 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021}
3022
3023/**
3024 * 82547 workaround to avoid controller hang in half-duplex environment.
3025 * The workaround is to avoid queuing a large packet that would span
3026 * the internal Tx FIFO ring boundary by notifying the stack to resend
3027 * the packet at a later time. This gives the Tx FIFO an opportunity to
3028 * flush all packets. When that occurs, we reset the Tx FIFO pointers
3029 * to the beginning of the Tx FIFO.
3030 **/
3031
3032#define E1000_FIFO_HDR 0x10
3033#define E1000_82547_PAD_LEN 0x3E0
3034
Joe Perches64798842008-07-11 15:17:02 -07003035static int e1000_82547_fifo_workaround(struct e1000_adapter *adapter,
3036 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037{
Joe Perches406874a2008-04-03 10:06:32 -07003038 u32 fifo_space = adapter->tx_fifo_size - adapter->tx_fifo_head;
3039 u32 skb_fifo_len = skb->len + E1000_FIFO_HDR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040
Milind Arun Choudhary9099cfb2007-04-27 13:55:29 -07003041 skb_fifo_len = ALIGN(skb_fifo_len, E1000_FIFO_HDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003043 if (adapter->link_duplex != HALF_DUPLEX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 goto no_fifo_stall_required;
3045
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003046 if (atomic_read(&adapter->tx_fifo_stall))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 return 1;
3048
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003049 if (skb_fifo_len >= (E1000_82547_PAD_LEN + fifo_space)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 atomic_set(&adapter->tx_fifo_stall, 1);
3051 return 1;
3052 }
3053
3054no_fifo_stall_required:
3055 adapter->tx_fifo_head += skb_fifo_len;
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003056 if (adapter->tx_fifo_head >= adapter->tx_fifo_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 adapter->tx_fifo_head -= adapter->tx_fifo_size;
3058 return 0;
3059}
3060
Jesse Brandeburg65c79732006-09-27 12:53:48 -07003061static int __e1000_maybe_stop_tx(struct net_device *netdev, int size)
3062{
3063 struct e1000_adapter *adapter = netdev_priv(netdev);
3064 struct e1000_tx_ring *tx_ring = adapter->tx_ring;
3065
3066 netif_stop_queue(netdev);
3067 /* Herbert's original patch had:
3068 * smp_mb__after_netif_stop_queue();
3069 * but since that doesn't exist yet, just open code it. */
3070 smp_mb();
3071
3072 /* We need to check again in a case another CPU has just
3073 * made room available. */
3074 if (likely(E1000_DESC_UNUSED(tx_ring) < size))
3075 return -EBUSY;
3076
3077 /* A reprieve! */
3078 netif_start_queue(netdev);
Jesse Brandeburgfcfb1222006-11-01 08:47:59 -08003079 ++adapter->restart_queue;
Jesse Brandeburg65c79732006-09-27 12:53:48 -07003080 return 0;
3081}
3082
3083static int e1000_maybe_stop_tx(struct net_device *netdev,
3084 struct e1000_tx_ring *tx_ring, int size)
3085{
3086 if (likely(E1000_DESC_UNUSED(tx_ring) >= size))
3087 return 0;
3088 return __e1000_maybe_stop_tx(netdev, size);
3089}
3090
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091#define TXD_USE_COUNT(S, X) (((S) >> (X)) + 1 )
Stephen Hemminger3b29a562009-08-31 19:50:55 +00003092static netdev_tx_t e1000_xmit_frame(struct sk_buff *skb,
3093 struct net_device *netdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094{
Malli Chilakala60490fe2005-06-17 17:41:45 -07003095 struct e1000_adapter *adapter = netdev_priv(netdev);
Joe Perches1dc32912008-07-11 15:17:08 -07003096 struct e1000_hw *hw = &adapter->hw;
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003097 struct e1000_tx_ring *tx_ring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 unsigned int first, max_per_txd = E1000_MAX_DATA_PER_TXD;
3099 unsigned int max_txd_pwr = E1000_MAX_TXD_PWR;
3100 unsigned int tx_flags = 0;
Eric Dumazete743d312010-04-14 15:59:40 -07003101 unsigned int len = skb_headlen(skb);
Krishna Kumar6d1e3aa2007-10-05 14:15:16 -07003102 unsigned int nr_frags;
3103 unsigned int mss;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 int count = 0;
Auke Kok76c224b2006-05-23 13:36:06 -07003105 int tso;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 unsigned int f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107
Jesse Brandeburg65c79732006-09-27 12:53:48 -07003108 /* This goes back to the question of how to logically map a tx queue
3109 * to a flow. Right now, performance is impacted slightly negatively
3110 * if using multiple tx queues. If the stack breaks away from a
3111 * single qdisc implementation, we can look at this again. */
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003112 tx_ring = adapter->tx_ring;
Mallikarjuna R Chilakala24025e42005-10-04 07:03:23 -04003113
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003114 if (unlikely(skb->len <= 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 dev_kfree_skb_any(skb);
3116 return NETDEV_TX_OK;
3117 }
3118
Herbert Xu79671682006-06-22 02:40:14 -07003119 mss = skb_shinfo(skb)->gso_size;
Auke Kok76c224b2006-05-23 13:36:06 -07003120 /* The controller does a simple calculation to
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 * make sure there is enough room in the FIFO before
3122 * initiating the DMA for each buffer. The calc is:
3123 * 4 = ceil(buffer len/mss). To make sure we don't
3124 * overrun the FIFO, adjust the max buffer len if mss
3125 * drops. */
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003126 if (mss) {
Joe Perches406874a2008-04-03 10:06:32 -07003127 u8 hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 max_per_txd = min(mss << 2, max_per_txd);
3129 max_txd_pwr = fls(max_per_txd) - 1;
Jeff Kirsher9a3056d2006-01-12 16:50:23 -08003130
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07003131 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
Krishna Kumar6d1e3aa2007-10-05 14:15:16 -07003132 if (skb->data_len && hdr_len == len) {
Joe Perches1dc32912008-07-11 15:17:08 -07003133 switch (hw->mac_type) {
Jeff Kirsher9f687882006-03-02 18:20:17 -08003134 unsigned int pull_size;
Herbert Xu683a2aa2006-12-16 12:04:33 +11003135 case e1000_82544:
3136 /* Make sure we have room to chop off 4 bytes,
3137 * and that the end alignment will work out to
3138 * this hardware's requirements
3139 * NOTE: this is a TSO only workaround
3140 * if end byte alignment not correct move us
3141 * into the next dword */
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07003142 if ((unsigned long)(skb_tail_pointer(skb) - 1) & 4)
Herbert Xu683a2aa2006-12-16 12:04:33 +11003143 break;
3144 /* fall through */
Jeff Kirsher9f687882006-03-02 18:20:17 -08003145 pull_size = min((unsigned int)4, skb->data_len);
3146 if (!__pskb_pull_tail(skb, pull_size)) {
Emil Tantilovfeb8f472010-07-26 23:37:21 -07003147 e_err(drv, "__pskb_pull_tail "
3148 "failed.\n");
Jeff Kirsher9f687882006-03-02 18:20:17 -08003149 dev_kfree_skb_any(skb);
Jeff Garzik749dfc72006-03-11 13:35:31 -05003150 return NETDEV_TX_OK;
Jeff Kirsher9f687882006-03-02 18:20:17 -08003151 }
Eric Dumazete743d312010-04-14 15:59:40 -07003152 len = skb_headlen(skb);
Jeff Kirsher9f687882006-03-02 18:20:17 -08003153 break;
3154 default:
3155 /* do nothing */
3156 break;
Jeff Kirsherd74bbd32006-01-12 16:51:07 -08003157 }
Jeff Kirsher9a3056d2006-01-12 16:50:23 -08003158 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 }
3160
Jeff Kirsher9a3056d2006-01-12 16:50:23 -08003161 /* reserve a descriptor for the offload context */
Patrick McHardy84fa7932006-08-29 16:44:56 -07003162 if ((mss) || (skb->ip_summed == CHECKSUM_PARTIAL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 count++;
Malli Chilakala26483452005-04-28 19:44:46 -07003164 count++;
Jeff Kirsherfd803242005-12-13 00:06:22 -05003165
Jeff Kirsherfd803242005-12-13 00:06:22 -05003166 /* Controller Erratum workaround */
Herbert Xu89114af2006-07-08 13:34:32 -07003167 if (!skb->data_len && tx_ring->last_tx_tso && !skb_is_gso(skb))
Jeff Kirsherfd803242005-12-13 00:06:22 -05003168 count++;
Jeff Kirsherfd803242005-12-13 00:06:22 -05003169
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170 count += TXD_USE_COUNT(len, max_txd_pwr);
3171
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003172 if (adapter->pcix_82544)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 count++;
3174
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003175 /* work-around for errata 10 and it applies to all controllers
Malli Chilakala97338bd2005-04-28 19:41:46 -07003176 * in PCI-X mode, so add one more descriptor to the count
3177 */
Joe Perches1dc32912008-07-11 15:17:08 -07003178 if (unlikely((hw->bus_type == e1000_bus_type_pcix) &&
Malli Chilakala97338bd2005-04-28 19:41:46 -07003179 (len > 2015)))
3180 count++;
3181
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 nr_frags = skb_shinfo(skb)->nr_frags;
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003183 for (f = 0; f < nr_frags; f++)
Eric Dumazet9e903e02011-10-18 21:00:24 +00003184 count += TXD_USE_COUNT(skb_frag_size(&skb_shinfo(skb)->frags[f]),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 max_txd_pwr);
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003186 if (adapter->pcix_82544)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 count += nr_frags;
3188
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 /* need: count + 2 desc gap to keep tail from touching
3190 * head, otherwise try next time */
Alexander Duyck80179432009-01-21 14:42:47 -08003191 if (unlikely(e1000_maybe_stop_tx(netdev, tx_ring, count + 2)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193
Jesse Brandeburga4010af2011-10-05 07:24:41 +00003194 if (unlikely((hw->mac_type == e1000_82547) &&
3195 (e1000_82547_fifo_workaround(adapter, skb)))) {
3196 netif_stop_queue(netdev);
3197 if (!test_bit(__E1000_DOWN, &adapter->flags))
3198 schedule_delayed_work(&adapter->fifo_stall_task, 1);
3199 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 }
3201
Jiri Pirko5622e402011-07-21 03:26:31 +00003202 if (vlan_tx_tag_present(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 tx_flags |= E1000_TX_FLAGS_VLAN;
3204 tx_flags |= (vlan_tx_tag_get(skb) << E1000_TX_FLAGS_VLAN_SHIFT);
3205 }
3206
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003207 first = tx_ring->next_to_use;
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003208
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003209 tso = e1000_tso(adapter, tx_ring, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 if (tso < 0) {
3211 dev_kfree_skb_any(skb);
3212 return NETDEV_TX_OK;
3213 }
3214
Jeff Kirsherfd803242005-12-13 00:06:22 -05003215 if (likely(tso)) {
Jesse Brandeburg8fce4732009-09-25 12:18:41 +00003216 if (likely(hw->mac_type != e1000_82544))
Rusty Russell3db1cd52011-12-19 13:56:45 +00003217 tx_ring->last_tx_tso = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 tx_flags |= E1000_TX_FLAGS_TSO;
Jeff Kirsherfd803242005-12-13 00:06:22 -05003219 } else if (likely(e1000_tx_csum(adapter, tx_ring, skb)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 tx_flags |= E1000_TX_FLAGS_CSUM;
3221
Alexey Dobriyan60828232006-05-23 14:52:21 -07003222 if (likely(skb->protocol == htons(ETH_P_IP)))
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003223 tx_flags |= E1000_TX_FLAGS_IPV4;
3224
Alexander Duyck37e73df2009-03-25 21:58:45 +00003225 count = e1000_tx_map(adapter, tx_ring, skb, first, max_per_txd,
3226 nr_frags, mss);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227
Alexander Duyck37e73df2009-03-25 21:58:45 +00003228 if (count) {
3229 e1000_tx_queue(adapter, tx_ring, tx_flags, count);
Alexander Duyck37e73df2009-03-25 21:58:45 +00003230 /* Make sure there is space in the ring for the next send. */
3231 e1000_maybe_stop_tx(netdev, tx_ring, MAX_SKB_FRAGS + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232
Alexander Duyck37e73df2009-03-25 21:58:45 +00003233 } else {
3234 dev_kfree_skb_any(skb);
3235 tx_ring->buffer_info[first].time_stamp = 0;
3236 tx_ring->next_to_use = first;
3237 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 return NETDEV_TX_OK;
3240}
3241
3242/**
3243 * e1000_tx_timeout - Respond to a Tx Hang
3244 * @netdev: network interface device structure
3245 **/
3246
Joe Perches64798842008-07-11 15:17:02 -07003247static void e1000_tx_timeout(struct net_device *netdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248{
Malli Chilakala60490fe2005-06-17 17:41:45 -07003249 struct e1000_adapter *adapter = netdev_priv(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250
3251 /* Do the reset outside of interrupt context */
Jeff Kirsher87041632006-03-02 18:21:24 -08003252 adapter->tx_timeout_count++;
3253 schedule_work(&adapter->reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254}
3255
Joe Perches64798842008-07-11 15:17:02 -07003256static void e1000_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257{
David Howells65f27f32006-11-22 14:55:48 +00003258 struct e1000_adapter *adapter =
3259 container_of(work, struct e1000_adapter, reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260
Jesse Brandeburg0ef4eed2011-10-05 07:24:51 +00003261 if (test_bit(__E1000_DOWN, &adapter->flags))
3262 return;
Jesse Brandeburg338c15e2010-09-22 18:22:42 +00003263 e1000_reinit_safe(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264}
3265
3266/**
3267 * e1000_get_stats - Get System Network Statistics
3268 * @netdev: network interface device structure
3269 *
3270 * Returns the address of the device statistics structure.
Jesse Brandeburga4010af2011-10-05 07:24:41 +00003271 * The statistics are actually updated from the watchdog.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 **/
3273
Joe Perches64798842008-07-11 15:17:02 -07003274static struct net_device_stats *e1000_get_stats(struct net_device *netdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275{
Jeff Kirsher6b7660c2006-01-12 16:50:35 -08003276 /* only return the current stats */
Ajit Khaparde5fe31de2009-10-07 02:42:23 +00003277 return &netdev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278}
3279
3280/**
3281 * e1000_change_mtu - Change the Maximum Transfer Unit
3282 * @netdev: network interface device structure
3283 * @new_mtu: new value for maximum frame size
3284 *
3285 * Returns 0 on success, negative on failure
3286 **/
3287
Joe Perches64798842008-07-11 15:17:02 -07003288static int e1000_change_mtu(struct net_device *netdev, int new_mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289{
Malli Chilakala60490fe2005-06-17 17:41:45 -07003290 struct e1000_adapter *adapter = netdev_priv(netdev);
Joe Perches1dc32912008-07-11 15:17:08 -07003291 struct e1000_hw *hw = &adapter->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292 int max_frame = new_mtu + ENET_HEADER_SIZE + ETHERNET_FCS_SIZE;
3293
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003294 if ((max_frame < MINIMUM_ETHERNET_FRAME_SIZE) ||
3295 (max_frame > MAX_JUMBO_FRAME_SIZE)) {
Emil Tantilovfeb8f472010-07-26 23:37:21 -07003296 e_err(probe, "Invalid MTU setting\n");
Mallikarjuna R Chilakala868d5302005-10-04 06:58:59 -04003297 return -EINVAL;
3298 }
3299
Jeff Kirsher997f5cb2006-01-12 16:50:55 -08003300 /* Adapter-specific max frame size limits. */
Joe Perches1dc32912008-07-11 15:17:08 -07003301 switch (hw->mac_type) {
Auke Kok9e2feac2006-04-14 19:05:18 -07003302 case e1000_undefined ... e1000_82542_rev2_1:
Jesse Brandeburgb7cb8c22009-07-06 10:45:01 +00003303 if (max_frame > (ETH_FRAME_LEN + ETH_FCS_LEN)) {
Emil Tantilovfeb8f472010-07-26 23:37:21 -07003304 e_err(probe, "Jumbo Frames not supported.\n");
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003305 return -EINVAL;
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003306 }
Jeff Kirsher997f5cb2006-01-12 16:50:55 -08003307 break;
Jeff Kirsher997f5cb2006-01-12 16:50:55 -08003308 default:
3309 /* Capable of supporting up to MAX_JUMBO_FRAME_SIZE limit. */
3310 break;
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003311 }
3312
Jesse Brandeburg3d6114e2009-09-25 12:19:02 +00003313 while (test_and_set_bit(__E1000_RESETTING, &adapter->flags))
3314 msleep(1);
3315 /* e1000_down has a dependency on max_frame_size */
3316 hw->max_frame_size = max_frame;
3317 if (netif_running(netdev))
3318 e1000_down(adapter);
3319
David S. Miller87f50322006-07-31 22:39:40 -07003320 /* NOTE: netdev_alloc_skb reserves 16 bytes, and typically NET_IP_ALIGN
Auke Kok9e2feac2006-04-14 19:05:18 -07003321 * means we reserve 2 more, this pushes us to allocate from the next
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00003322 * larger slab size.
3323 * i.e. RXBUFFER_2048 --> size-4096 slab
3324 * however with the new *_jumbo_rx* routines, jumbo receives will use
3325 * fragmented skbs */
Jeff Kirsher997f5cb2006-01-12 16:50:55 -08003326
Jesse Brandeburg99261462010-01-22 22:56:16 +00003327 if (max_frame <= E1000_RXBUFFER_2048)
Auke Kok9e2feac2006-04-14 19:05:18 -07003328 adapter->rx_buffer_len = E1000_RXBUFFER_2048;
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00003329 else
3330#if (PAGE_SIZE >= E1000_RXBUFFER_16384)
Auke Kok9e2feac2006-04-14 19:05:18 -07003331 adapter->rx_buffer_len = E1000_RXBUFFER_16384;
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00003332#elif (PAGE_SIZE >= E1000_RXBUFFER_4096)
3333 adapter->rx_buffer_len = PAGE_SIZE;
3334#endif
Auke Kok9e2feac2006-04-14 19:05:18 -07003335
3336 /* adjust allocation if LPE protects us, and we aren't using SBP */
Joe Perches1dc32912008-07-11 15:17:08 -07003337 if (!hw->tbi_compatibility_on &&
Jesse Brandeburgb7cb8c22009-07-06 10:45:01 +00003338 ((max_frame == (ETH_FRAME_LEN + ETH_FCS_LEN)) ||
Auke Kok9e2feac2006-04-14 19:05:18 -07003339 (max_frame == MAXIMUM_ETHERNET_VLAN_SIZE)))
3340 adapter->rx_buffer_len = MAXIMUM_ETHERNET_VLAN_SIZE;
Jeff Kirsher997f5cb2006-01-12 16:50:55 -08003341
Emil Tantilov675ad472010-04-27 14:02:58 +00003342 pr_info("%s changing MTU from %d to %d\n",
3343 netdev->name, netdev->mtu, new_mtu);
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003344 netdev->mtu = new_mtu;
3345
Auke Kok2db10a02006-06-27 09:06:28 -07003346 if (netif_running(netdev))
Jesse Brandeburg3d6114e2009-09-25 12:19:02 +00003347 e1000_up(adapter);
3348 else
3349 e1000_reset(adapter);
3350
3351 clear_bit(__E1000_RESETTING, &adapter->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353 return 0;
3354}
3355
3356/**
3357 * e1000_update_stats - Update the board statistics counters
3358 * @adapter: board private structure
3359 **/
3360
Joe Perches64798842008-07-11 15:17:02 -07003361void e1000_update_stats(struct e1000_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362{
Ajit Khaparde5fe31de2009-10-07 02:42:23 +00003363 struct net_device *netdev = adapter->netdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364 struct e1000_hw *hw = &adapter->hw;
Linas Vepstas282f33c2006-06-08 22:19:44 -07003365 struct pci_dev *pdev = adapter->pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366 unsigned long flags;
Joe Perches406874a2008-04-03 10:06:32 -07003367 u16 phy_tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368
3369#define PHY_IDLE_ERROR_COUNT_MASK 0x00FF
3370
Linas Vepstas282f33c2006-06-08 22:19:44 -07003371 /*
3372 * Prevent stats update while adapter is being reset, or if the pci
3373 * connection is down.
3374 */
Auke Kok90267292006-06-08 09:30:24 -07003375 if (adapter->link_speed == 0)
3376 return;
Linas Vepstas81b19552006-12-12 18:29:15 -06003377 if (pci_channel_offline(pdev))
Linas Vepstas282f33c2006-06-08 22:19:44 -07003378 return;
Auke Kok90267292006-06-08 09:30:24 -07003379
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380 spin_lock_irqsave(&adapter->stats_lock, flags);
3381
Masatake YAMATO828d0552007-10-20 03:06:37 +02003382 /* these counters are modified from e1000_tbi_adjust_stats,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383 * called from the interrupt context, so they must only
3384 * be written while holding adapter->stats_lock
3385 */
3386
Joe Perches1dc32912008-07-11 15:17:08 -07003387 adapter->stats.crcerrs += er32(CRCERRS);
3388 adapter->stats.gprc += er32(GPRC);
3389 adapter->stats.gorcl += er32(GORCL);
3390 adapter->stats.gorch += er32(GORCH);
3391 adapter->stats.bprc += er32(BPRC);
3392 adapter->stats.mprc += er32(MPRC);
3393 adapter->stats.roc += er32(ROC);
Auke Kokcd94dd02006-06-27 09:08:22 -07003394
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00003395 adapter->stats.prc64 += er32(PRC64);
3396 adapter->stats.prc127 += er32(PRC127);
3397 adapter->stats.prc255 += er32(PRC255);
3398 adapter->stats.prc511 += er32(PRC511);
3399 adapter->stats.prc1023 += er32(PRC1023);
3400 adapter->stats.prc1522 += er32(PRC1522);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401
Joe Perches1dc32912008-07-11 15:17:08 -07003402 adapter->stats.symerrs += er32(SYMERRS);
3403 adapter->stats.mpc += er32(MPC);
3404 adapter->stats.scc += er32(SCC);
3405 adapter->stats.ecol += er32(ECOL);
3406 adapter->stats.mcc += er32(MCC);
3407 adapter->stats.latecol += er32(LATECOL);
3408 adapter->stats.dc += er32(DC);
3409 adapter->stats.sec += er32(SEC);
3410 adapter->stats.rlec += er32(RLEC);
3411 adapter->stats.xonrxc += er32(XONRXC);
3412 adapter->stats.xontxc += er32(XONTXC);
3413 adapter->stats.xoffrxc += er32(XOFFRXC);
3414 adapter->stats.xofftxc += er32(XOFFTXC);
3415 adapter->stats.fcruc += er32(FCRUC);
3416 adapter->stats.gptc += er32(GPTC);
3417 adapter->stats.gotcl += er32(GOTCL);
3418 adapter->stats.gotch += er32(GOTCH);
3419 adapter->stats.rnbc += er32(RNBC);
3420 adapter->stats.ruc += er32(RUC);
3421 adapter->stats.rfc += er32(RFC);
3422 adapter->stats.rjc += er32(RJC);
3423 adapter->stats.torl += er32(TORL);
3424 adapter->stats.torh += er32(TORH);
3425 adapter->stats.totl += er32(TOTL);
3426 adapter->stats.toth += er32(TOTH);
3427 adapter->stats.tpr += er32(TPR);
Auke Kokcd94dd02006-06-27 09:08:22 -07003428
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00003429 adapter->stats.ptc64 += er32(PTC64);
3430 adapter->stats.ptc127 += er32(PTC127);
3431 adapter->stats.ptc255 += er32(PTC255);
3432 adapter->stats.ptc511 += er32(PTC511);
3433 adapter->stats.ptc1023 += er32(PTC1023);
3434 adapter->stats.ptc1522 += er32(PTC1522);
Auke Kokcd94dd02006-06-27 09:08:22 -07003435
Joe Perches1dc32912008-07-11 15:17:08 -07003436 adapter->stats.mptc += er32(MPTC);
3437 adapter->stats.bptc += er32(BPTC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438
3439 /* used for adaptive IFS */
3440
Joe Perches1dc32912008-07-11 15:17:08 -07003441 hw->tx_packet_delta = er32(TPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442 adapter->stats.tpt += hw->tx_packet_delta;
Joe Perches1dc32912008-07-11 15:17:08 -07003443 hw->collision_delta = er32(COLC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 adapter->stats.colc += hw->collision_delta;
3445
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003446 if (hw->mac_type >= e1000_82543) {
Joe Perches1dc32912008-07-11 15:17:08 -07003447 adapter->stats.algnerrc += er32(ALGNERRC);
3448 adapter->stats.rxerrc += er32(RXERRC);
3449 adapter->stats.tncrs += er32(TNCRS);
3450 adapter->stats.cexterr += er32(CEXTERR);
3451 adapter->stats.tsctc += er32(TSCTC);
3452 adapter->stats.tsctfc += er32(TSCTFC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453 }
3454
3455 /* Fill out the OS statistics structure */
Ajit Khaparde5fe31de2009-10-07 02:42:23 +00003456 netdev->stats.multicast = adapter->stats.mprc;
3457 netdev->stats.collisions = adapter->stats.colc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458
3459 /* Rx Errors */
3460
Jeff Kirsher87041632006-03-02 18:21:24 -08003461 /* RLEC on some newer hardware can be incorrect so build
3462 * our own version based on RUC and ROC */
Ajit Khaparde5fe31de2009-10-07 02:42:23 +00003463 netdev->stats.rx_errors = adapter->stats.rxerrc +
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 adapter->stats.crcerrs + adapter->stats.algnerrc +
Jeff Kirsher87041632006-03-02 18:21:24 -08003465 adapter->stats.ruc + adapter->stats.roc +
3466 adapter->stats.cexterr;
Mitch Williams49559852006-09-27 12:53:37 -07003467 adapter->stats.rlerrc = adapter->stats.ruc + adapter->stats.roc;
Ajit Khaparde5fe31de2009-10-07 02:42:23 +00003468 netdev->stats.rx_length_errors = adapter->stats.rlerrc;
3469 netdev->stats.rx_crc_errors = adapter->stats.crcerrs;
3470 netdev->stats.rx_frame_errors = adapter->stats.algnerrc;
3471 netdev->stats.rx_missed_errors = adapter->stats.mpc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472
3473 /* Tx Errors */
Mitch Williams49559852006-09-27 12:53:37 -07003474 adapter->stats.txerrc = adapter->stats.ecol + adapter->stats.latecol;
Ajit Khaparde5fe31de2009-10-07 02:42:23 +00003475 netdev->stats.tx_errors = adapter->stats.txerrc;
3476 netdev->stats.tx_aborted_errors = adapter->stats.ecol;
3477 netdev->stats.tx_window_errors = adapter->stats.latecol;
3478 netdev->stats.tx_carrier_errors = adapter->stats.tncrs;
Joe Perches1dc32912008-07-11 15:17:08 -07003479 if (hw->bad_tx_carr_stats_fd &&
Jeff Garzik167fb282006-12-15 10:41:15 -05003480 adapter->link_duplex == FULL_DUPLEX) {
Ajit Khaparde5fe31de2009-10-07 02:42:23 +00003481 netdev->stats.tx_carrier_errors = 0;
Jeff Garzik167fb282006-12-15 10:41:15 -05003482 adapter->stats.tncrs = 0;
3483 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484
3485 /* Tx Dropped needs to be maintained elsewhere */
3486
3487 /* Phy Stats */
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003488 if (hw->media_type == e1000_media_type_copper) {
3489 if ((adapter->link_speed == SPEED_1000) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490 (!e1000_read_phy_reg(hw, PHY_1000T_STATUS, &phy_tmp))) {
3491 phy_tmp &= PHY_IDLE_ERROR_COUNT_MASK;
3492 adapter->phy_stats.idle_errors += phy_tmp;
3493 }
3494
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003495 if ((hw->mac_type <= e1000_82546) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496 (hw->phy_type == e1000_phy_m88) &&
3497 !e1000_read_phy_reg(hw, M88E1000_RX_ERR_CNTR, &phy_tmp))
3498 adapter->phy_stats.receive_errors += phy_tmp;
3499 }
3500
Jeff Garzik15e376b2006-12-15 11:16:33 -05003501 /* Management Stats */
Joe Perches1dc32912008-07-11 15:17:08 -07003502 if (hw->has_smbus) {
3503 adapter->stats.mgptc += er32(MGTPTC);
3504 adapter->stats.mgprc += er32(MGTPRC);
3505 adapter->stats.mgpdc += er32(MGTPDC);
Jeff Garzik15e376b2006-12-15 11:16:33 -05003506 }
3507
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508 spin_unlock_irqrestore(&adapter->stats_lock, flags);
3509}
Jesse Brandeburg9ac98282006-11-01 08:48:10 -08003510
3511/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512 * e1000_intr - Interrupt Handler
3513 * @irq: interrupt number
3514 * @data: pointer to a network interface device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515 **/
3516
Joe Perches64798842008-07-11 15:17:02 -07003517static irqreturn_t e1000_intr(int irq, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518{
3519 struct net_device *netdev = data;
Malli Chilakala60490fe2005-06-17 17:41:45 -07003520 struct e1000_adapter *adapter = netdev_priv(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521 struct e1000_hw *hw = &adapter->hw;
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00003522 u32 icr = er32(ICR);
Francois Romieuc3570ac2008-07-11 15:17:38 -07003523
Jesse Brandeburg4c11b8a2011-01-13 07:48:13 +00003524 if (unlikely((!icr)))
Jesse Brandeburg835bb122006-11-01 08:48:13 -08003525 return IRQ_NONE; /* Not our interrupt */
3526
Jesse Brandeburg4c11b8a2011-01-13 07:48:13 +00003527 /*
3528 * we might have caused the interrupt, but the above
3529 * read cleared it, and just in case the driver is
3530 * down there is nothing to do so return handled
3531 */
3532 if (unlikely(test_bit(__E1000_DOWN, &adapter->flags)))
3533 return IRQ_HANDLED;
3534
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003535 if (unlikely(icr & (E1000_ICR_RXSEQ | E1000_ICR_LSC))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536 hw->get_link_status = 1;
Auke Kok1314bbf2006-09-27 12:54:02 -07003537 /* guard against interrupt when we're going down */
3538 if (!test_bit(__E1000_DOWN, &adapter->flags))
Jesse Brandeburga4010af2011-10-05 07:24:41 +00003539 schedule_delayed_work(&adapter->watchdog_task, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540 }
3541
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00003542 /* disable interrupts, without the synchronize_irq bit */
3543 ew32(IMC, ~0);
3544 E1000_WRITE_FLUSH();
3545
Ben Hutchings288379f2009-01-19 16:43:59 -08003546 if (likely(napi_schedule_prep(&adapter->napi))) {
Jesse Brandeburg835bb122006-11-01 08:48:13 -08003547 adapter->total_tx_bytes = 0;
3548 adapter->total_tx_packets = 0;
3549 adapter->total_rx_bytes = 0;
3550 adapter->total_rx_packets = 0;
Ben Hutchings288379f2009-01-19 16:43:59 -08003551 __napi_schedule(&adapter->napi);
Jesse Brandeburga6c42322009-03-25 21:59:22 +00003552 } else {
Auke Kok90fb5132006-11-01 08:47:30 -08003553 /* this really should not happen! if it does it is basically a
3554 * bug, but not a hard error, so enable ints and continue */
Jesse Brandeburga6c42322009-03-25 21:59:22 +00003555 if (!test_bit(__E1000_DOWN, &adapter->flags))
3556 e1000_irq_enable(adapter);
3557 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559 return IRQ_HANDLED;
3560}
3561
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562/**
3563 * e1000_clean - NAPI Rx polling callback
3564 * @adapter: board private structure
3565 **/
Joe Perches64798842008-07-11 15:17:02 -07003566static int e1000_clean(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003568 struct e1000_adapter *adapter = container_of(napi, struct e1000_adapter, napi);
Jesse Brandeburg650b5a52009-09-25 12:19:23 +00003569 int tx_clean_complete = 0, work_done = 0;
Malli Chilakala26483452005-04-28 19:44:46 -07003570
Jesse Brandeburg650b5a52009-09-25 12:19:23 +00003571 tx_clean_complete = e1000_clean_tx_irq(adapter, &adapter->tx_ring[0]);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003572
Jesse Brandeburg650b5a52009-09-25 12:19:23 +00003573 adapter->clean_rx(adapter, &adapter->rx_ring[0], &work_done, budget);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003574
Jesse Brandeburg650b5a52009-09-25 12:19:23 +00003575 if (!tx_clean_complete)
David S. Millerd2c7ddd2008-01-15 22:43:24 -08003576 work_done = budget;
3577
David S. Miller53e52c72008-01-07 21:06:12 -08003578 /* If budget not fully consumed, exit the polling mode */
3579 if (work_done < budget) {
Jesse Brandeburg835bb122006-11-01 08:48:13 -08003580 if (likely(adapter->itr_setting & 3))
3581 e1000_set_itr(adapter);
Ben Hutchings288379f2009-01-19 16:43:59 -08003582 napi_complete(napi);
Jesse Brandeburga6c42322009-03-25 21:59:22 +00003583 if (!test_bit(__E1000_DOWN, &adapter->flags))
3584 e1000_irq_enable(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585 }
3586
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003587 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588}
3589
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590/**
3591 * e1000_clean_tx_irq - Reclaim resources after transmit completes
3592 * @adapter: board private structure
3593 **/
Joe Perches64798842008-07-11 15:17:02 -07003594static bool e1000_clean_tx_irq(struct e1000_adapter *adapter,
3595 struct e1000_tx_ring *tx_ring)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596{
Joe Perches1dc32912008-07-11 15:17:08 -07003597 struct e1000_hw *hw = &adapter->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598 struct net_device *netdev = adapter->netdev;
3599 struct e1000_tx_desc *tx_desc, *eop_desc;
3600 struct e1000_buffer *buffer_info;
3601 unsigned int i, eop;
Jeff Kirsher2a1af5d2006-03-02 18:20:43 -08003602 unsigned int count = 0;
Jesse Brandeburg835bb122006-11-01 08:48:13 -08003603 unsigned int total_tx_bytes=0, total_tx_packets=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604
3605 i = tx_ring->next_to_clean;
3606 eop = tx_ring->buffer_info[i].next_to_watch;
3607 eop_desc = E1000_TX_DESC(*tx_ring, eop);
3608
Alexander Duyckccfb3422009-03-25 21:59:04 +00003609 while ((eop_desc->upper.data & cpu_to_le32(E1000_TXD_STAT_DD)) &&
3610 (count < tx_ring->count)) {
Jesse Brandeburg843f4262009-04-16 16:59:47 +00003611 bool cleaned = false;
Jeff Kirsher2d0bb1c2010-08-08 16:02:31 +00003612 rmb(); /* read buffer_info after eop_desc */
Jesse Brandeburg843f4262009-04-16 16:59:47 +00003613 for ( ; !cleaned; count++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614 tx_desc = E1000_TX_DESC(*tx_ring, i);
3615 buffer_info = &tx_ring->buffer_info[i];
3616 cleaned = (i == eop);
3617
Jesse Brandeburg835bb122006-11-01 08:48:13 -08003618 if (cleaned) {
Dean Nelson31c15a22011-08-25 14:39:24 +00003619 total_tx_packets += buffer_info->segs;
3620 total_tx_bytes += buffer_info->bytecount;
Jesse Brandeburg835bb122006-11-01 08:48:13 -08003621 }
Jeff Kirsherfd803242005-12-13 00:06:22 -05003622 e1000_unmap_and_free_tx_resource(adapter, buffer_info);
Jesse Brandeburga9ebadd2006-11-01 08:47:53 -08003623 tx_desc->upper.data = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003625 if (unlikely(++i == tx_ring->count)) i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626 }
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003627
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628 eop = tx_ring->buffer_info[i].next_to_watch;
3629 eop_desc = E1000_TX_DESC(*tx_ring, eop);
3630 }
3631
3632 tx_ring->next_to_clean = i;
3633
Auke Kok77b2aad2006-04-14 19:05:25 -07003634#define TX_WAKE_THRESHOLD 32
Jesse Brandeburg843f4262009-04-16 16:59:47 +00003635 if (unlikely(count && netif_carrier_ok(netdev) &&
Jesse Brandeburg65c79732006-09-27 12:53:48 -07003636 E1000_DESC_UNUSED(tx_ring) >= TX_WAKE_THRESHOLD)) {
3637 /* Make sure that anybody stopping the queue after this
3638 * sees the new next_to_clean.
3639 */
3640 smp_mb();
Jesse Brandeburgcdd75492009-09-25 12:18:07 +00003641
3642 if (netif_queue_stopped(netdev) &&
3643 !(test_bit(__E1000_DOWN, &adapter->flags))) {
Auke Kok77b2aad2006-04-14 19:05:25 -07003644 netif_wake_queue(netdev);
Jesse Brandeburgfcfb1222006-11-01 08:47:59 -08003645 ++adapter->restart_queue;
3646 }
Auke Kok77b2aad2006-04-14 19:05:25 -07003647 }
Malli Chilakala26483452005-04-28 19:44:46 -07003648
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003649 if (adapter->detect_tx_hung) {
Malli Chilakala26483452005-04-28 19:44:46 -07003650 /* Detect a transmit hang in hardware, this serializes the
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651 * check with the clearing of time_stamp and movement of i */
Joe Perchesc3033b02008-03-21 11:06:25 -07003652 adapter->detect_tx_hung = false;
Jesse Brandeburgcdd75492009-09-25 12:18:07 +00003653 if (tx_ring->buffer_info[eop].time_stamp &&
3654 time_after(jiffies, tx_ring->buffer_info[eop].time_stamp +
Joe Perches8e95a202009-12-03 07:58:21 +00003655 (adapter->tx_timeout_factor * HZ)) &&
3656 !(er32(STATUS) & E1000_STATUS_TXOFF)) {
Malli Chilakala70b8f1e2005-04-28 19:40:40 -07003657
3658 /* detected Tx unit hang */
Emil Tantilovfeb8f472010-07-26 23:37:21 -07003659 e_err(drv, "Detected Tx Unit Hang\n"
Emil Tantilov675ad472010-04-27 14:02:58 +00003660 " Tx Queue <%lu>\n"
3661 " TDH <%x>\n"
3662 " TDT <%x>\n"
3663 " next_to_use <%x>\n"
3664 " next_to_clean <%x>\n"
3665 "buffer_info[next_to_clean]\n"
3666 " time_stamp <%lx>\n"
3667 " next_to_watch <%x>\n"
3668 " jiffies <%lx>\n"
3669 " next_to_watch.status <%x>\n",
Jeff Kirsher7bfa4812006-01-12 16:50:41 -08003670 (unsigned long)((tx_ring - adapter->tx_ring) /
3671 sizeof(struct e1000_tx_ring)),
Joe Perches1dc32912008-07-11 15:17:08 -07003672 readl(hw->hw_addr + tx_ring->tdh),
3673 readl(hw->hw_addr + tx_ring->tdt),
Malli Chilakala70b8f1e2005-04-28 19:40:40 -07003674 tx_ring->next_to_use,
Jeff Kirsher392137f2006-01-12 16:50:57 -08003675 tx_ring->next_to_clean,
Jesse Brandeburgcdd75492009-09-25 12:18:07 +00003676 tx_ring->buffer_info[eop].time_stamp,
Malli Chilakala70b8f1e2005-04-28 19:40:40 -07003677 eop,
3678 jiffies,
3679 eop_desc->upper.fields.status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680 netif_stop_queue(netdev);
Malli Chilakala70b8f1e2005-04-28 19:40:40 -07003681 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682 }
Jesse Brandeburg835bb122006-11-01 08:48:13 -08003683 adapter->total_tx_bytes += total_tx_bytes;
3684 adapter->total_tx_packets += total_tx_packets;
Ajit Khaparde5fe31de2009-10-07 02:42:23 +00003685 netdev->stats.tx_bytes += total_tx_bytes;
3686 netdev->stats.tx_packets += total_tx_packets;
Eric Dumazet807540b2010-09-23 05:40:09 +00003687 return count < tx_ring->count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688}
3689
3690/**
3691 * e1000_rx_checksum - Receive Checksum Offload for 82543
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003692 * @adapter: board private structure
3693 * @status_err: receive descriptor status and error fields
3694 * @csum: receive descriptor csum field
3695 * @sk_buff: socket buffer with received data
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 **/
3697
Joe Perches64798842008-07-11 15:17:02 -07003698static void e1000_rx_checksum(struct e1000_adapter *adapter, u32 status_err,
3699 u32 csum, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700{
Joe Perches1dc32912008-07-11 15:17:08 -07003701 struct e1000_hw *hw = &adapter->hw;
Joe Perches406874a2008-04-03 10:06:32 -07003702 u16 status = (u16)status_err;
3703 u8 errors = (u8)(status_err >> 24);
Eric Dumazetbc8acf22010-09-02 13:07:41 -07003704
3705 skb_checksum_none_assert(skb);
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003706
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707 /* 82543 or newer only */
Joe Perches1dc32912008-07-11 15:17:08 -07003708 if (unlikely(hw->mac_type < e1000_82543)) return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709 /* Ignore Checksum bit is set */
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003710 if (unlikely(status & E1000_RXD_STAT_IXSM)) return;
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003711 /* TCP/UDP checksum error bit is set */
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003712 if (unlikely(errors & E1000_RXD_ERR_TCPE)) {
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003713 /* let the stack verify checksum errors */
3714 adapter->hw_csum_err++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715 return;
3716 }
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003717 /* TCP/UDP Checksum has not been calculated */
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00003718 if (!(status & E1000_RXD_STAT_TCPCS))
3719 return;
3720
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003721 /* It must be a TCP or UDP packet with a valid checksum */
3722 if (likely(status & E1000_RXD_STAT_TCPCS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723 /* TCP checksum is good */
3724 skb->ip_summed = CHECKSUM_UNNECESSARY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725 }
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003726 adapter->hw_csum_good++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727}
3728
3729/**
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00003730 * e1000_consume_page - helper function
3731 **/
3732static void e1000_consume_page(struct e1000_buffer *bi, struct sk_buff *skb,
3733 u16 length)
3734{
3735 bi->page = NULL;
3736 skb->len += length;
3737 skb->data_len += length;
Eric Dumazeted64b3c2011-10-13 07:53:42 +00003738 skb->truesize += PAGE_SIZE;
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00003739}
3740
3741/**
3742 * e1000_receive_skb - helper function to handle rx indications
3743 * @adapter: board private structure
3744 * @status: descriptor status field as written by hardware
3745 * @vlan: descriptor vlan field as written by hardware (no le/be conversion)
3746 * @skb: pointer to sk_buff to be indicated to stack
3747 */
3748static void e1000_receive_skb(struct e1000_adapter *adapter, u8 status,
3749 __le16 vlan, struct sk_buff *skb)
3750{
Jesse Brandeburg6a08d192010-09-22 18:23:05 +00003751 skb->protocol = eth_type_trans(skb, adapter->netdev);
3752
Jiri Pirko5622e402011-07-21 03:26:31 +00003753 if (status & E1000_RXD_STAT_VP) {
3754 u16 vid = le16_to_cpu(vlan) & E1000_RXD_SPC_VLAN_MASK;
3755
3756 __vlan_hwaccel_put_tag(skb, vid);
3757 }
3758 napi_gro_receive(&adapter->napi, skb);
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00003759}
3760
3761/**
3762 * e1000_clean_jumbo_rx_irq - Send received data up the network stack; legacy
3763 * @adapter: board private structure
3764 * @rx_ring: ring to clean
3765 * @work_done: amount of napi work completed this call
3766 * @work_to_do: max amount of work allowed for this call to do
3767 *
3768 * the return value indicates whether actual cleaning was done, there
3769 * is no guarantee that everything was cleaned
3770 */
3771static bool e1000_clean_jumbo_rx_irq(struct e1000_adapter *adapter,
3772 struct e1000_rx_ring *rx_ring,
3773 int *work_done, int work_to_do)
3774{
3775 struct e1000_hw *hw = &adapter->hw;
3776 struct net_device *netdev = adapter->netdev;
3777 struct pci_dev *pdev = adapter->pdev;
3778 struct e1000_rx_desc *rx_desc, *next_rxd;
3779 struct e1000_buffer *buffer_info, *next_buffer;
3780 unsigned long irq_flags;
3781 u32 length;
3782 unsigned int i;
3783 int cleaned_count = 0;
3784 bool cleaned = false;
3785 unsigned int total_rx_bytes=0, total_rx_packets=0;
3786
3787 i = rx_ring->next_to_clean;
3788 rx_desc = E1000_RX_DESC(*rx_ring, i);
3789 buffer_info = &rx_ring->buffer_info[i];
3790
3791 while (rx_desc->status & E1000_RXD_STAT_DD) {
3792 struct sk_buff *skb;
3793 u8 status;
3794
3795 if (*work_done >= work_to_do)
3796 break;
3797 (*work_done)++;
Jeff Kirsher2d0bb1c2010-08-08 16:02:31 +00003798 rmb(); /* read descriptor and rx_buffer_info after status DD */
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00003799
3800 status = rx_desc->status;
3801 skb = buffer_info->skb;
3802 buffer_info->skb = NULL;
3803
3804 if (++i == rx_ring->count) i = 0;
3805 next_rxd = E1000_RX_DESC(*rx_ring, i);
3806 prefetch(next_rxd);
3807
3808 next_buffer = &rx_ring->buffer_info[i];
3809
3810 cleaned = true;
3811 cleaned_count++;
Nick Nunleyb16f53b2010-04-27 13:08:45 +00003812 dma_unmap_page(&pdev->dev, buffer_info->dma,
3813 buffer_info->length, DMA_FROM_DEVICE);
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00003814 buffer_info->dma = 0;
3815
3816 length = le16_to_cpu(rx_desc->length);
3817
3818 /* errors is only valid for DD + EOP descriptors */
3819 if (unlikely((status & E1000_RXD_STAT_EOP) &&
3820 (rx_desc->errors & E1000_RXD_ERR_FRAME_ERR_MASK))) {
3821 u8 last_byte = *(skb->data + length - 1);
3822 if (TBI_ACCEPT(hw, status, rx_desc->errors, length,
3823 last_byte)) {
3824 spin_lock_irqsave(&adapter->stats_lock,
3825 irq_flags);
3826 e1000_tbi_adjust_stats(hw, &adapter->stats,
3827 length, skb->data);
3828 spin_unlock_irqrestore(&adapter->stats_lock,
3829 irq_flags);
3830 length--;
3831 } else {
3832 /* recycle both page and skb */
3833 buffer_info->skb = skb;
3834 /* an error means any chain goes out the window
3835 * too */
3836 if (rx_ring->rx_skb_top)
3837 dev_kfree_skb(rx_ring->rx_skb_top);
3838 rx_ring->rx_skb_top = NULL;
3839 goto next_desc;
3840 }
3841 }
3842
3843#define rxtop rx_ring->rx_skb_top
3844 if (!(status & E1000_RXD_STAT_EOP)) {
3845 /* this descriptor is only the beginning (or middle) */
3846 if (!rxtop) {
3847 /* this is the beginning of a chain */
3848 rxtop = skb;
3849 skb_fill_page_desc(rxtop, 0, buffer_info->page,
3850 0, length);
3851 } else {
3852 /* this is the middle of a chain */
3853 skb_fill_page_desc(rxtop,
3854 skb_shinfo(rxtop)->nr_frags,
3855 buffer_info->page, 0, length);
3856 /* re-use the skb, only consumed the page */
3857 buffer_info->skb = skb;
3858 }
3859 e1000_consume_page(buffer_info, rxtop, length);
3860 goto next_desc;
3861 } else {
3862 if (rxtop) {
3863 /* end of the chain */
3864 skb_fill_page_desc(rxtop,
3865 skb_shinfo(rxtop)->nr_frags,
3866 buffer_info->page, 0, length);
3867 /* re-use the current skb, we only consumed the
3868 * page */
3869 buffer_info->skb = skb;
3870 skb = rxtop;
3871 rxtop = NULL;
3872 e1000_consume_page(buffer_info, skb, length);
3873 } else {
3874 /* no chain, got EOP, this buf is the packet
3875 * copybreak to save the put_page/alloc_page */
3876 if (length <= copybreak &&
3877 skb_tailroom(skb) >= length) {
3878 u8 *vaddr;
3879 vaddr = kmap_atomic(buffer_info->page,
3880 KM_SKB_DATA_SOFTIRQ);
3881 memcpy(skb_tail_pointer(skb), vaddr, length);
3882 kunmap_atomic(vaddr,
3883 KM_SKB_DATA_SOFTIRQ);
3884 /* re-use the page, so don't erase
3885 * buffer_info->page */
3886 skb_put(skb, length);
3887 } else {
3888 skb_fill_page_desc(skb, 0,
3889 buffer_info->page, 0,
3890 length);
3891 e1000_consume_page(buffer_info, skb,
3892 length);
3893 }
3894 }
3895 }
3896
3897 /* Receive Checksum Offload XXX recompute due to CRC strip? */
3898 e1000_rx_checksum(adapter,
3899 (u32)(status) |
3900 ((u32)(rx_desc->errors) << 24),
3901 le16_to_cpu(rx_desc->csum), skb);
3902
3903 pskb_trim(skb, skb->len - 4);
3904
3905 /* probably a little skewed due to removing CRC */
3906 total_rx_bytes += skb->len;
3907 total_rx_packets++;
3908
3909 /* eth type trans needs skb->data to point to something */
3910 if (!pskb_may_pull(skb, ETH_HLEN)) {
Emil Tantilovfeb8f472010-07-26 23:37:21 -07003911 e_err(drv, "pskb_may_pull failed.\n");
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00003912 dev_kfree_skb(skb);
3913 goto next_desc;
3914 }
3915
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00003916 e1000_receive_skb(adapter, status, rx_desc->special, skb);
3917
3918next_desc:
3919 rx_desc->status = 0;
3920
3921 /* return some buffers to hardware, one at a time is too slow */
3922 if (unlikely(cleaned_count >= E1000_RX_BUFFER_WRITE)) {
3923 adapter->alloc_rx_buf(adapter, rx_ring, cleaned_count);
3924 cleaned_count = 0;
3925 }
3926
3927 /* use prefetched values */
3928 rx_desc = next_rxd;
3929 buffer_info = next_buffer;
3930 }
3931 rx_ring->next_to_clean = i;
3932
3933 cleaned_count = E1000_DESC_UNUSED(rx_ring);
3934 if (cleaned_count)
3935 adapter->alloc_rx_buf(adapter, rx_ring, cleaned_count);
3936
3937 adapter->total_rx_packets += total_rx_packets;
3938 adapter->total_rx_bytes += total_rx_bytes;
Ajit Khaparde5fe31de2009-10-07 02:42:23 +00003939 netdev->stats.rx_bytes += total_rx_bytes;
3940 netdev->stats.rx_packets += total_rx_packets;
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00003941 return cleaned;
3942}
3943
Joe Perches57bf6ee2010-05-13 15:26:17 +00003944/*
3945 * this should improve performance for small packets with large amounts
3946 * of reassembly being done in the stack
3947 */
3948static void e1000_check_copybreak(struct net_device *netdev,
3949 struct e1000_buffer *buffer_info,
3950 u32 length, struct sk_buff **skb)
3951{
3952 struct sk_buff *new_skb;
3953
3954 if (length > copybreak)
3955 return;
3956
3957 new_skb = netdev_alloc_skb_ip_align(netdev, length);
3958 if (!new_skb)
3959 return;
3960
3961 skb_copy_to_linear_data_offset(new_skb, -NET_IP_ALIGN,
3962 (*skb)->data - NET_IP_ALIGN,
3963 length + NET_IP_ALIGN);
3964 /* save the skb in buffer_info as good */
3965 buffer_info->skb = *skb;
3966 *skb = new_skb;
3967}
3968
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00003969/**
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003970 * e1000_clean_rx_irq - Send received data up the network stack; legacy
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971 * @adapter: board private structure
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00003972 * @rx_ring: ring to clean
3973 * @work_done: amount of napi work completed this call
3974 * @work_to_do: max amount of work allowed for this call to do
3975 */
Joe Perches64798842008-07-11 15:17:02 -07003976static bool e1000_clean_rx_irq(struct e1000_adapter *adapter,
3977 struct e1000_rx_ring *rx_ring,
3978 int *work_done, int work_to_do)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979{
Joe Perches1dc32912008-07-11 15:17:08 -07003980 struct e1000_hw *hw = &adapter->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981 struct net_device *netdev = adapter->netdev;
3982 struct pci_dev *pdev = adapter->pdev;
Jesse Brandeburg86c3d592006-01-18 13:01:43 -08003983 struct e1000_rx_desc *rx_desc, *next_rxd;
3984 struct e1000_buffer *buffer_info, *next_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985 unsigned long flags;
Joe Perches406874a2008-04-03 10:06:32 -07003986 u32 length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987 unsigned int i;
Jeff Kirsher72d64a42006-01-12 16:51:19 -08003988 int cleaned_count = 0;
Joe Perchesc3033b02008-03-21 11:06:25 -07003989 bool cleaned = false;
Jesse Brandeburg835bb122006-11-01 08:48:13 -08003990 unsigned int total_rx_bytes=0, total_rx_packets=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991
3992 i = rx_ring->next_to_clean;
3993 rx_desc = E1000_RX_DESC(*rx_ring, i);
Jesse Brandeburgb92ff8e2006-01-18 13:01:32 -08003994 buffer_info = &rx_ring->buffer_info[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995
Jesse Brandeburgb92ff8e2006-01-18 13:01:32 -08003996 while (rx_desc->status & E1000_RXD_STAT_DD) {
Auke Kok24f476e2006-06-08 09:28:47 -07003997 struct sk_buff *skb;
Jeff Kirshera292ca62006-01-12 16:51:30 -08003998 u8 status;
Auke Kok90fb5132006-11-01 08:47:30 -08003999
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004000 if (*work_done >= work_to_do)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001 break;
4002 (*work_done)++;
Jeff Kirsher2d0bb1c2010-08-08 16:02:31 +00004003 rmb(); /* read descriptor and rx_buffer_info after status DD */
Francois Romieuc3570ac2008-07-11 15:17:38 -07004004
Jeff Kirshera292ca62006-01-12 16:51:30 -08004005 status = rx_desc->status;
Jesse Brandeburgb92ff8e2006-01-18 13:01:32 -08004006 skb = buffer_info->skb;
Jesse Brandeburg86c3d592006-01-18 13:01:43 -08004007 buffer_info->skb = NULL;
4008
Jeff Kirsher30320be2006-03-02 18:21:57 -08004009 prefetch(skb->data - NET_IP_ALIGN);
4010
Jesse Brandeburg86c3d592006-01-18 13:01:43 -08004011 if (++i == rx_ring->count) i = 0;
4012 next_rxd = E1000_RX_DESC(*rx_ring, i);
Jeff Kirsher30320be2006-03-02 18:21:57 -08004013 prefetch(next_rxd);
4014
Jesse Brandeburg86c3d592006-01-18 13:01:43 -08004015 next_buffer = &rx_ring->buffer_info[i];
Jesse Brandeburg86c3d592006-01-18 13:01:43 -08004016
Joe Perchesc3033b02008-03-21 11:06:25 -07004017 cleaned = true;
Jeff Kirsher72d64a42006-01-12 16:51:19 -08004018 cleaned_count++;
Nick Nunleyb16f53b2010-04-27 13:08:45 +00004019 dma_unmap_single(&pdev->dev, buffer_info->dma,
4020 buffer_info->length, DMA_FROM_DEVICE);
Jesse Brandeburg679be3b2009-06-30 12:45:34 +00004021 buffer_info->dma = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023 length = le16_to_cpu(rx_desc->length);
Neil Hormanea30e112009-06-02 01:29:58 -07004024 /* !EOP means multiple descriptors were used to store a single
Jesse Brandeburg40a14de2010-01-19 14:15:38 +00004025 * packet, if thats the case we need to toss it. In fact, we
4026 * to toss every packet with the EOP bit clear and the next
4027 * frame that _does_ have the EOP bit set, as it is by
4028 * definition only a frame fragment
4029 */
4030 if (unlikely(!(status & E1000_RXD_STAT_EOP)))
4031 adapter->discarding = true;
4032
4033 if (adapter->discarding) {
Jeff Kirshera1415ee2006-02-28 20:24:07 -08004034 /* All receives must fit into a single buffer */
Emil Tantilovfeb8f472010-07-26 23:37:21 -07004035 e_dbg("Receive packet consumed multiple buffers\n");
Auke Kok864c4e42006-06-27 09:06:53 -07004036 /* recycle */
Auke Kok8fc897b2006-08-28 14:56:16 -07004037 buffer_info->skb = skb;
Jesse Brandeburg40a14de2010-01-19 14:15:38 +00004038 if (status & E1000_RXD_STAT_EOP)
4039 adapter->discarding = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040 goto next_desc;
4041 }
4042
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004043 if (unlikely(rx_desc->errors & E1000_RXD_ERR_FRAME_ERR_MASK)) {
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004044 u8 last_byte = *(skb->data + length - 1);
Joe Perches1dc32912008-07-11 15:17:08 -07004045 if (TBI_ACCEPT(hw, status, rx_desc->errors, length,
4046 last_byte)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047 spin_lock_irqsave(&adapter->stats_lock, flags);
Joe Perches1dc32912008-07-11 15:17:08 -07004048 e1000_tbi_adjust_stats(hw, &adapter->stats,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049 length, skb->data);
4050 spin_unlock_irqrestore(&adapter->stats_lock,
4051 flags);
4052 length--;
4053 } else {
Auke Kok9e2feac2006-04-14 19:05:18 -07004054 /* recycle */
4055 buffer_info->skb = skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056 goto next_desc;
4057 }
Auke Kok1cb58212006-04-18 12:31:04 -07004058 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059
Jesse Brandeburgd2a1e212006-10-24 14:46:06 -07004060 /* adjust length to remove Ethernet CRC, this must be
4061 * done after the TBI_ACCEPT workaround above */
4062 length -= 4;
4063
Jesse Brandeburg835bb122006-11-01 08:48:13 -08004064 /* probably a little skewed due to removing CRC */
4065 total_rx_bytes += length;
4066 total_rx_packets++;
4067
Joe Perches57bf6ee2010-05-13 15:26:17 +00004068 e1000_check_copybreak(netdev, buffer_info, length, &skb);
4069
Auke Kok996695d2006-11-01 08:47:50 -08004070 skb_put(skb, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071
4072 /* Receive Checksum Offload */
Jeff Kirshera292ca62006-01-12 16:51:30 -08004073 e1000_rx_checksum(adapter,
Joe Perches406874a2008-04-03 10:06:32 -07004074 (u32)(status) |
4075 ((u32)(rx_desc->errors) << 24),
David S. Millerc3d7a3a2006-03-15 14:26:28 -08004076 le16_to_cpu(rx_desc->csum), skb);
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004077
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004078 e1000_receive_skb(adapter, status, rx_desc->special, skb);
Francois Romieuc3570ac2008-07-11 15:17:38 -07004079
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080next_desc:
4081 rx_desc->status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082
Jeff Kirsher72d64a42006-01-12 16:51:19 -08004083 /* return some buffers to hardware, one at a time is too slow */
4084 if (unlikely(cleaned_count >= E1000_RX_BUFFER_WRITE)) {
4085 adapter->alloc_rx_buf(adapter, rx_ring, cleaned_count);
4086 cleaned_count = 0;
4087 }
4088
Jeff Kirsher30320be2006-03-02 18:21:57 -08004089 /* use prefetched values */
Jesse Brandeburg86c3d592006-01-18 13:01:43 -08004090 rx_desc = next_rxd;
4091 buffer_info = next_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093 rx_ring->next_to_clean = i;
Jeff Kirsher72d64a42006-01-12 16:51:19 -08004094
4095 cleaned_count = E1000_DESC_UNUSED(rx_ring);
4096 if (cleaned_count)
4097 adapter->alloc_rx_buf(adapter, rx_ring, cleaned_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098
Jesse Brandeburg835bb122006-11-01 08:48:13 -08004099 adapter->total_rx_packets += total_rx_packets;
4100 adapter->total_rx_bytes += total_rx_bytes;
Ajit Khaparde5fe31de2009-10-07 02:42:23 +00004101 netdev->stats.rx_bytes += total_rx_bytes;
4102 netdev->stats.rx_packets += total_rx_packets;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004103 return cleaned;
4104}
4105
4106/**
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004107 * e1000_alloc_jumbo_rx_buffers - Replace used jumbo receive buffers
4108 * @adapter: address of board private structure
4109 * @rx_ring: pointer to receive ring structure
4110 * @cleaned_count: number of buffers to allocate this pass
4111 **/
4112
4113static void
4114e1000_alloc_jumbo_rx_buffers(struct e1000_adapter *adapter,
4115 struct e1000_rx_ring *rx_ring, int cleaned_count)
4116{
4117 struct net_device *netdev = adapter->netdev;
4118 struct pci_dev *pdev = adapter->pdev;
4119 struct e1000_rx_desc *rx_desc;
4120 struct e1000_buffer *buffer_info;
4121 struct sk_buff *skb;
4122 unsigned int i;
Eric Dumazet89d71a62009-10-13 05:34:20 +00004123 unsigned int bufsz = 256 - 16 /*for skb_reserve */ ;
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004124
4125 i = rx_ring->next_to_use;
4126 buffer_info = &rx_ring->buffer_info[i];
4127
4128 while (cleaned_count--) {
4129 skb = buffer_info->skb;
4130 if (skb) {
4131 skb_trim(skb, 0);
4132 goto check_page;
4133 }
4134
Eric Dumazet89d71a62009-10-13 05:34:20 +00004135 skb = netdev_alloc_skb_ip_align(netdev, bufsz);
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004136 if (unlikely(!skb)) {
4137 /* Better luck next round */
4138 adapter->alloc_rx_buff_failed++;
4139 break;
4140 }
4141
4142 /* Fix for errata 23, can't cross 64kB boundary */
4143 if (!e1000_check_64k_bound(adapter, skb->data, bufsz)) {
4144 struct sk_buff *oldskb = skb;
Emil Tantilovfeb8f472010-07-26 23:37:21 -07004145 e_err(rx_err, "skb align check failed: %u bytes at "
4146 "%p\n", bufsz, skb->data);
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004147 /* Try again, without freeing the previous */
Eric Dumazet89d71a62009-10-13 05:34:20 +00004148 skb = netdev_alloc_skb_ip_align(netdev, bufsz);
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004149 /* Failed allocation, critical failure */
4150 if (!skb) {
4151 dev_kfree_skb(oldskb);
4152 adapter->alloc_rx_buff_failed++;
4153 break;
4154 }
4155
4156 if (!e1000_check_64k_bound(adapter, skb->data, bufsz)) {
4157 /* give up */
4158 dev_kfree_skb(skb);
4159 dev_kfree_skb(oldskb);
4160 break; /* while (cleaned_count--) */
4161 }
4162
4163 /* Use new allocation */
4164 dev_kfree_skb(oldskb);
4165 }
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004166 buffer_info->skb = skb;
4167 buffer_info->length = adapter->rx_buffer_len;
4168check_page:
4169 /* allocate a new page if necessary */
4170 if (!buffer_info->page) {
4171 buffer_info->page = alloc_page(GFP_ATOMIC);
4172 if (unlikely(!buffer_info->page)) {
4173 adapter->alloc_rx_buff_failed++;
4174 break;
4175 }
4176 }
4177
Anton Blanchardb5abb022010-02-19 17:54:53 +00004178 if (!buffer_info->dma) {
Nick Nunleyb16f53b2010-04-27 13:08:45 +00004179 buffer_info->dma = dma_map_page(&pdev->dev,
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004180 buffer_info->page, 0,
Nick Nunleyb16f53b2010-04-27 13:08:45 +00004181 buffer_info->length,
4182 DMA_FROM_DEVICE);
4183 if (dma_mapping_error(&pdev->dev, buffer_info->dma)) {
Anton Blanchardb5abb022010-02-19 17:54:53 +00004184 put_page(buffer_info->page);
4185 dev_kfree_skb(skb);
4186 buffer_info->page = NULL;
4187 buffer_info->skb = NULL;
4188 buffer_info->dma = 0;
4189 adapter->alloc_rx_buff_failed++;
4190 break; /* while !buffer_info->skb */
4191 }
4192 }
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004193
4194 rx_desc = E1000_RX_DESC(*rx_ring, i);
4195 rx_desc->buffer_addr = cpu_to_le64(buffer_info->dma);
4196
4197 if (unlikely(++i == rx_ring->count))
4198 i = 0;
4199 buffer_info = &rx_ring->buffer_info[i];
4200 }
4201
4202 if (likely(rx_ring->next_to_use != i)) {
4203 rx_ring->next_to_use = i;
4204 if (unlikely(i-- == 0))
4205 i = (rx_ring->count - 1);
4206
4207 /* Force memory writes to complete before letting h/w
4208 * know there are new descriptors to fetch. (Only
4209 * applicable for weak-ordered memory model archs,
4210 * such as IA-64). */
4211 wmb();
4212 writel(i, adapter->hw.hw_addr + rx_ring->rdt);
4213 }
4214}
4215
4216/**
Malli Chilakala2d7edb92005-04-28 19:43:52 -07004217 * e1000_alloc_rx_buffers - Replace used receive buffers; legacy & extended
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218 * @adapter: address of board private structure
4219 **/
4220
Joe Perches64798842008-07-11 15:17:02 -07004221static void e1000_alloc_rx_buffers(struct e1000_adapter *adapter,
4222 struct e1000_rx_ring *rx_ring,
4223 int cleaned_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224{
Joe Perches1dc32912008-07-11 15:17:08 -07004225 struct e1000_hw *hw = &adapter->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226 struct net_device *netdev = adapter->netdev;
4227 struct pci_dev *pdev = adapter->pdev;
4228 struct e1000_rx_desc *rx_desc;
4229 struct e1000_buffer *buffer_info;
4230 struct sk_buff *skb;
Malli Chilakala26483452005-04-28 19:44:46 -07004231 unsigned int i;
Eric Dumazet89d71a62009-10-13 05:34:20 +00004232 unsigned int bufsz = adapter->rx_buffer_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233
4234 i = rx_ring->next_to_use;
4235 buffer_info = &rx_ring->buffer_info[i];
4236
Jeff Kirshera292ca62006-01-12 16:51:30 -08004237 while (cleaned_count--) {
Christoph Hellwigca6f7222006-08-31 14:27:47 -07004238 skb = buffer_info->skb;
4239 if (skb) {
Jeff Kirshera292ca62006-01-12 16:51:30 -08004240 skb_trim(skb, 0);
4241 goto map_skb;
4242 }
4243
Eric Dumazet89d71a62009-10-13 05:34:20 +00004244 skb = netdev_alloc_skb_ip_align(netdev, bufsz);
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004245 if (unlikely(!skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246 /* Better luck next round */
Jeff Kirsher72d64a42006-01-12 16:51:19 -08004247 adapter->alloc_rx_buff_failed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248 break;
4249 }
4250
Malli Chilakala26483452005-04-28 19:44:46 -07004251 /* Fix for errata 23, can't cross 64kB boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252 if (!e1000_check_64k_bound(adapter, skb->data, bufsz)) {
4253 struct sk_buff *oldskb = skb;
Emil Tantilovfeb8f472010-07-26 23:37:21 -07004254 e_err(rx_err, "skb align check failed: %u bytes at "
4255 "%p\n", bufsz, skb->data);
Malli Chilakala26483452005-04-28 19:44:46 -07004256 /* Try again, without freeing the previous */
Eric Dumazet89d71a62009-10-13 05:34:20 +00004257 skb = netdev_alloc_skb_ip_align(netdev, bufsz);
Malli Chilakala26483452005-04-28 19:44:46 -07004258 /* Failed allocation, critical failure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259 if (!skb) {
4260 dev_kfree_skb(oldskb);
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004261 adapter->alloc_rx_buff_failed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262 break;
4263 }
Malli Chilakala26483452005-04-28 19:44:46 -07004264
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265 if (!e1000_check_64k_bound(adapter, skb->data, bufsz)) {
4266 /* give up */
4267 dev_kfree_skb(skb);
4268 dev_kfree_skb(oldskb);
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004269 adapter->alloc_rx_buff_failed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270 break; /* while !buffer_info->skb */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271 }
Christoph Hellwigca6f7222006-08-31 14:27:47 -07004272
4273 /* Use new allocation */
4274 dev_kfree_skb(oldskb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276 buffer_info->skb = skb;
4277 buffer_info->length = adapter->rx_buffer_len;
Jeff Kirshera292ca62006-01-12 16:51:30 -08004278map_skb:
Nick Nunleyb16f53b2010-04-27 13:08:45 +00004279 buffer_info->dma = dma_map_single(&pdev->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280 skb->data,
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004281 buffer_info->length,
Nick Nunleyb16f53b2010-04-27 13:08:45 +00004282 DMA_FROM_DEVICE);
4283 if (dma_mapping_error(&pdev->dev, buffer_info->dma)) {
Anton Blanchardb5abb022010-02-19 17:54:53 +00004284 dev_kfree_skb(skb);
4285 buffer_info->skb = NULL;
4286 buffer_info->dma = 0;
4287 adapter->alloc_rx_buff_failed++;
4288 break; /* while !buffer_info->skb */
4289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004291 /*
4292 * XXX if it was allocated cleanly it will never map to a
4293 * boundary crossing
4294 */
4295
Malli Chilakala26483452005-04-28 19:44:46 -07004296 /* Fix for errata 23, can't cross 64kB boundary */
4297 if (!e1000_check_64k_bound(adapter,
4298 (void *)(unsigned long)buffer_info->dma,
4299 adapter->rx_buffer_len)) {
Emil Tantilovfeb8f472010-07-26 23:37:21 -07004300 e_err(rx_err, "dma align check failed: %u bytes at "
4301 "%p\n", adapter->rx_buffer_len,
Emil Tantilov675ad472010-04-27 14:02:58 +00004302 (void *)(unsigned long)buffer_info->dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303 dev_kfree_skb(skb);
4304 buffer_info->skb = NULL;
4305
Nick Nunleyb16f53b2010-04-27 13:08:45 +00004306 dma_unmap_single(&pdev->dev, buffer_info->dma,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307 adapter->rx_buffer_len,
Nick Nunleyb16f53b2010-04-27 13:08:45 +00004308 DMA_FROM_DEVICE);
Jesse Brandeburg679be3b2009-06-30 12:45:34 +00004309 buffer_info->dma = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004311 adapter->alloc_rx_buff_failed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312 break; /* while !buffer_info->skb */
4313 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314 rx_desc = E1000_RX_DESC(*rx_ring, i);
4315 rx_desc->buffer_addr = cpu_to_le64(buffer_info->dma);
4316
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004317 if (unlikely(++i == rx_ring->count))
4318 i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319 buffer_info = &rx_ring->buffer_info[i];
4320 }
4321
Jesse Brandeburgb92ff8e2006-01-18 13:01:32 -08004322 if (likely(rx_ring->next_to_use != i)) {
4323 rx_ring->next_to_use = i;
4324 if (unlikely(i-- == 0))
4325 i = (rx_ring->count - 1);
4326
4327 /* Force memory writes to complete before letting h/w
4328 * know there are new descriptors to fetch. (Only
4329 * applicable for weak-ordered memory model archs,
4330 * such as IA-64). */
4331 wmb();
Joe Perches1dc32912008-07-11 15:17:08 -07004332 writel(i, hw->hw_addr + rx_ring->rdt);
Jesse Brandeburgb92ff8e2006-01-18 13:01:32 -08004333 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334}
4335
4336/**
4337 * e1000_smartspeed - Workaround for SmartSpeed on 82541 and 82547 controllers.
4338 * @adapter:
4339 **/
4340
Joe Perches64798842008-07-11 15:17:02 -07004341static void e1000_smartspeed(struct e1000_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342{
Joe Perches1dc32912008-07-11 15:17:08 -07004343 struct e1000_hw *hw = &adapter->hw;
Joe Perches406874a2008-04-03 10:06:32 -07004344 u16 phy_status;
4345 u16 phy_ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346
Joe Perches1dc32912008-07-11 15:17:08 -07004347 if ((hw->phy_type != e1000_phy_igp) || !hw->autoneg ||
4348 !(hw->autoneg_advertised & ADVERTISE_1000_FULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349 return;
4350
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004351 if (adapter->smartspeed == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352 /* If Master/Slave config fault is asserted twice,
4353 * we assume back-to-back */
Joe Perches1dc32912008-07-11 15:17:08 -07004354 e1000_read_phy_reg(hw, PHY_1000T_STATUS, &phy_status);
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004355 if (!(phy_status & SR_1000T_MS_CONFIG_FAULT)) return;
Joe Perches1dc32912008-07-11 15:17:08 -07004356 e1000_read_phy_reg(hw, PHY_1000T_STATUS, &phy_status);
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004357 if (!(phy_status & SR_1000T_MS_CONFIG_FAULT)) return;
Joe Perches1dc32912008-07-11 15:17:08 -07004358 e1000_read_phy_reg(hw, PHY_1000T_CTRL, &phy_ctrl);
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004359 if (phy_ctrl & CR_1000T_MS_ENABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360 phy_ctrl &= ~CR_1000T_MS_ENABLE;
Joe Perches1dc32912008-07-11 15:17:08 -07004361 e1000_write_phy_reg(hw, PHY_1000T_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362 phy_ctrl);
4363 adapter->smartspeed++;
Joe Perches1dc32912008-07-11 15:17:08 -07004364 if (!e1000_phy_setup_autoneg(hw) &&
4365 !e1000_read_phy_reg(hw, PHY_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366 &phy_ctrl)) {
4367 phy_ctrl |= (MII_CR_AUTO_NEG_EN |
4368 MII_CR_RESTART_AUTO_NEG);
Joe Perches1dc32912008-07-11 15:17:08 -07004369 e1000_write_phy_reg(hw, PHY_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370 phy_ctrl);
4371 }
4372 }
4373 return;
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004374 } else if (adapter->smartspeed == E1000_SMARTSPEED_DOWNSHIFT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375 /* If still no link, perhaps using 2/3 pair cable */
Joe Perches1dc32912008-07-11 15:17:08 -07004376 e1000_read_phy_reg(hw, PHY_1000T_CTRL, &phy_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377 phy_ctrl |= CR_1000T_MS_ENABLE;
Joe Perches1dc32912008-07-11 15:17:08 -07004378 e1000_write_phy_reg(hw, PHY_1000T_CTRL, phy_ctrl);
4379 if (!e1000_phy_setup_autoneg(hw) &&
4380 !e1000_read_phy_reg(hw, PHY_CTRL, &phy_ctrl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381 phy_ctrl |= (MII_CR_AUTO_NEG_EN |
4382 MII_CR_RESTART_AUTO_NEG);
Joe Perches1dc32912008-07-11 15:17:08 -07004383 e1000_write_phy_reg(hw, PHY_CTRL, phy_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384 }
4385 }
4386 /* Restart process after E1000_SMARTSPEED_MAX iterations */
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004387 if (adapter->smartspeed++ == E1000_SMARTSPEED_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388 adapter->smartspeed = 0;
4389}
4390
4391/**
4392 * e1000_ioctl -
4393 * @netdev:
4394 * @ifreq:
4395 * @cmd:
4396 **/
4397
Joe Perches64798842008-07-11 15:17:02 -07004398static int e1000_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399{
4400 switch (cmd) {
4401 case SIOCGMIIPHY:
4402 case SIOCGMIIREG:
4403 case SIOCSMIIREG:
4404 return e1000_mii_ioctl(netdev, ifr, cmd);
4405 default:
4406 return -EOPNOTSUPP;
4407 }
4408}
4409
4410/**
4411 * e1000_mii_ioctl -
4412 * @netdev:
4413 * @ifreq:
4414 * @cmd:
4415 **/
4416
Joe Perches64798842008-07-11 15:17:02 -07004417static int e1000_mii_ioctl(struct net_device *netdev, struct ifreq *ifr,
4418 int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419{
Malli Chilakala60490fe2005-06-17 17:41:45 -07004420 struct e1000_adapter *adapter = netdev_priv(netdev);
Joe Perches1dc32912008-07-11 15:17:08 -07004421 struct e1000_hw *hw = &adapter->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422 struct mii_ioctl_data *data = if_mii(ifr);
4423 int retval;
Joe Perches406874a2008-04-03 10:06:32 -07004424 u16 mii_reg;
Malli Chilakala97876fc2005-06-17 17:40:19 -07004425 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426
Joe Perches1dc32912008-07-11 15:17:08 -07004427 if (hw->media_type != e1000_media_type_copper)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428 return -EOPNOTSUPP;
4429
4430 switch (cmd) {
4431 case SIOCGMIIPHY:
Joe Perches1dc32912008-07-11 15:17:08 -07004432 data->phy_id = hw->phy_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433 break;
4434 case SIOCGMIIREG:
Malli Chilakala97876fc2005-06-17 17:40:19 -07004435 spin_lock_irqsave(&adapter->stats_lock, flags);
Joe Perches1dc32912008-07-11 15:17:08 -07004436 if (e1000_read_phy_reg(hw, data->reg_num & 0x1F,
Malli Chilakala97876fc2005-06-17 17:40:19 -07004437 &data->val_out)) {
4438 spin_unlock_irqrestore(&adapter->stats_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439 return -EIO;
Malli Chilakala97876fc2005-06-17 17:40:19 -07004440 }
4441 spin_unlock_irqrestore(&adapter->stats_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442 break;
4443 case SIOCSMIIREG:
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004444 if (data->reg_num & ~(0x1F))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445 return -EFAULT;
4446 mii_reg = data->val_in;
Malli Chilakala97876fc2005-06-17 17:40:19 -07004447 spin_lock_irqsave(&adapter->stats_lock, flags);
Joe Perches1dc32912008-07-11 15:17:08 -07004448 if (e1000_write_phy_reg(hw, data->reg_num,
Malli Chilakala97876fc2005-06-17 17:40:19 -07004449 mii_reg)) {
4450 spin_unlock_irqrestore(&adapter->stats_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451 return -EIO;
Malli Chilakala97876fc2005-06-17 17:40:19 -07004452 }
Jesse Brandeburgf0163ac2007-11-13 21:00:09 -08004453 spin_unlock_irqrestore(&adapter->stats_lock, flags);
Joe Perches1dc32912008-07-11 15:17:08 -07004454 if (hw->media_type == e1000_media_type_copper) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455 switch (data->reg_num) {
4456 case PHY_CTRL:
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004457 if (mii_reg & MII_CR_POWER_DOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458 break;
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004459 if (mii_reg & MII_CR_AUTO_NEG_EN) {
Joe Perches1dc32912008-07-11 15:17:08 -07004460 hw->autoneg = 1;
4461 hw->autoneg_advertised = 0x2F;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462 } else {
David Decotigny14ad2512011-04-27 18:32:43 +00004463 u32 speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464 if (mii_reg & 0x40)
David Decotigny14ad2512011-04-27 18:32:43 +00004465 speed = SPEED_1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466 else if (mii_reg & 0x2000)
David Decotigny14ad2512011-04-27 18:32:43 +00004467 speed = SPEED_100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468 else
David Decotigny14ad2512011-04-27 18:32:43 +00004469 speed = SPEED_10;
4470 retval = e1000_set_spd_dplx(
4471 adapter, speed,
4472 ((mii_reg & 0x100)
4473 ? DUPLEX_FULL :
4474 DUPLEX_HALF));
Jesse Brandeburgf0163ac2007-11-13 21:00:09 -08004475 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476 return retval;
4477 }
Auke Kok2db10a02006-06-27 09:06:28 -07004478 if (netif_running(adapter->netdev))
4479 e1000_reinit_locked(adapter);
4480 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481 e1000_reset(adapter);
4482 break;
4483 case M88E1000_PHY_SPEC_CTRL:
4484 case M88E1000_EXT_PHY_SPEC_CTRL:
Joe Perches1dc32912008-07-11 15:17:08 -07004485 if (e1000_phy_reset(hw))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486 return -EIO;
4487 break;
4488 }
4489 } else {
4490 switch (data->reg_num) {
4491 case PHY_CTRL:
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004492 if (mii_reg & MII_CR_POWER_DOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493 break;
Auke Kok2db10a02006-06-27 09:06:28 -07004494 if (netif_running(adapter->netdev))
4495 e1000_reinit_locked(adapter);
4496 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497 e1000_reset(adapter);
4498 break;
4499 }
4500 }
4501 break;
4502 default:
4503 return -EOPNOTSUPP;
4504 }
4505 return E1000_SUCCESS;
4506}
4507
Joe Perches64798842008-07-11 15:17:02 -07004508void e1000_pci_set_mwi(struct e1000_hw *hw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509{
4510 struct e1000_adapter *adapter = hw->back;
Malli Chilakala26483452005-04-28 19:44:46 -07004511 int ret_val = pci_set_mwi(adapter->pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004513 if (ret_val)
Emil Tantilovfeb8f472010-07-26 23:37:21 -07004514 e_err(probe, "Error in setting MWI\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515}
4516
Joe Perches64798842008-07-11 15:17:02 -07004517void e1000_pci_clear_mwi(struct e1000_hw *hw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518{
4519 struct e1000_adapter *adapter = hw->back;
4520
4521 pci_clear_mwi(adapter->pdev);
4522}
4523
Joe Perches64798842008-07-11 15:17:02 -07004524int e1000_pcix_get_mmrbc(struct e1000_hw *hw)
Peter Oruba007755e2007-09-28 22:42:06 -07004525{
4526 struct e1000_adapter *adapter = hw->back;
4527 return pcix_get_mmrbc(adapter->pdev);
4528}
4529
Joe Perches64798842008-07-11 15:17:02 -07004530void e1000_pcix_set_mmrbc(struct e1000_hw *hw, int mmrbc)
Peter Oruba007755e2007-09-28 22:42:06 -07004531{
4532 struct e1000_adapter *adapter = hw->back;
4533 pcix_set_mmrbc(adapter->pdev, mmrbc);
4534}
4535
Joe Perches64798842008-07-11 15:17:02 -07004536void e1000_io_write(struct e1000_hw *hw, unsigned long port, u32 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537{
4538 outl(value, port);
4539}
4540
Jiri Pirko5622e402011-07-21 03:26:31 +00004541static bool e1000_vlan_used(struct e1000_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542{
Jiri Pirko5622e402011-07-21 03:26:31 +00004543 u16 vid;
4544
4545 for_each_set_bit(vid, adapter->active_vlans, VLAN_N_VID)
4546 return true;
4547 return false;
4548}
4549
4550static void e1000_vlan_filter_on_off(struct e1000_adapter *adapter,
4551 bool filter_on)
4552{
Joe Perches1dc32912008-07-11 15:17:08 -07004553 struct e1000_hw *hw = &adapter->hw;
Jiri Pirko5622e402011-07-21 03:26:31 +00004554 u32 rctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555
Jesse Brandeburg9150b762008-03-21 11:06:58 -07004556 if (!test_bit(__E1000_DOWN, &adapter->flags))
4557 e1000_irq_disable(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558
Jiri Pirko5622e402011-07-21 03:26:31 +00004559 if (filter_on) {
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00004560 /* enable VLAN receive filtering */
4561 rctl = er32(RCTL);
4562 rctl &= ~E1000_RCTL_CFIEN;
Jiri Pirko5622e402011-07-21 03:26:31 +00004563 if (!(adapter->netdev->flags & IFF_PROMISC))
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00004564 rctl |= E1000_RCTL_VFE;
4565 ew32(RCTL, rctl);
4566 e1000_update_mng_vlan(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567 } else {
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00004568 /* disable VLAN receive filtering */
4569 rctl = er32(RCTL);
4570 rctl &= ~E1000_RCTL_VFE;
4571 ew32(RCTL, rctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572 }
4573
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
Michał Mirosławc8f44af2011-11-15 15:29:55 +00004578static void e1000_vlan_mode(struct net_device *netdev,
4579 netdev_features_t features)
Jiri Pirko5622e402011-07-21 03:26:31 +00004580{
4581 struct e1000_adapter *adapter = netdev_priv(netdev);
4582 struct e1000_hw *hw = &adapter->hw;
4583 u32 ctrl;
4584
4585 if (!test_bit(__E1000_DOWN, &adapter->flags))
4586 e1000_irq_disable(adapter);
4587
4588 ctrl = er32(CTRL);
4589 if (features & NETIF_F_HW_VLAN_RX) {
4590 /* enable VLAN tag insert/strip */
4591 ctrl |= E1000_CTRL_VME;
4592 } else {
4593 /* disable VLAN tag insert/strip */
4594 ctrl &= ~E1000_CTRL_VME;
4595 }
4596 ew32(CTRL, ctrl);
4597
4598 if (!test_bit(__E1000_DOWN, &adapter->flags))
4599 e1000_irq_enable(adapter);
4600}
4601
Jiri Pirko8e586132011-12-08 19:52:37 -05004602static int e1000_vlan_rx_add_vid(struct net_device *netdev, u16 vid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603{
Malli Chilakala60490fe2005-06-17 17:41:45 -07004604 struct e1000_adapter *adapter = netdev_priv(netdev);
Joe Perches1dc32912008-07-11 15:17:08 -07004605 struct e1000_hw *hw = &adapter->hw;
Joe Perches406874a2008-04-03 10:06:32 -07004606 u32 vfta, index;
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004607
Joe Perches1dc32912008-07-11 15:17:08 -07004608 if ((hw->mng_cookie.status &
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004609 E1000_MNG_DHCP_COOKIE_STATUS_VLAN_SUPPORT) &&
4610 (vid == adapter->mng_vlan_id))
Jiri Pirko8e586132011-12-08 19:52:37 -05004611 return 0;
Jiri Pirko5622e402011-07-21 03:26:31 +00004612
4613 if (!e1000_vlan_used(adapter))
4614 e1000_vlan_filter_on_off(adapter, true);
4615
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616 /* add VID to filter table */
4617 index = (vid >> 5) & 0x7F;
Joe Perches1dc32912008-07-11 15:17:08 -07004618 vfta = E1000_READ_REG_ARRAY(hw, VFTA, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619 vfta |= (1 << (vid & 0x1F));
Joe Perches1dc32912008-07-11 15:17:08 -07004620 e1000_write_vfta(hw, index, vfta);
Jiri Pirko5622e402011-07-21 03:26:31 +00004621
4622 set_bit(vid, adapter->active_vlans);
Jiri Pirko8e586132011-12-08 19:52:37 -05004623
4624 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625}
4626
Jiri Pirko8e586132011-12-08 19:52:37 -05004627static int e1000_vlan_rx_kill_vid(struct net_device *netdev, u16 vid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628{
Malli Chilakala60490fe2005-06-17 17:41:45 -07004629 struct e1000_adapter *adapter = netdev_priv(netdev);
Joe Perches1dc32912008-07-11 15:17:08 -07004630 struct e1000_hw *hw = &adapter->hw;
Joe Perches406874a2008-04-03 10:06:32 -07004631 u32 vfta, index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632
Jesse Brandeburg9150b762008-03-21 11:06:58 -07004633 if (!test_bit(__E1000_DOWN, &adapter->flags))
4634 e1000_irq_disable(adapter);
Jesse Brandeburg9150b762008-03-21 11:06:58 -07004635 if (!test_bit(__E1000_DOWN, &adapter->flags))
4636 e1000_irq_enable(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637
4638 /* remove VID from filter table */
4639 index = (vid >> 5) & 0x7F;
Joe Perches1dc32912008-07-11 15:17:08 -07004640 vfta = E1000_READ_REG_ARRAY(hw, VFTA, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641 vfta &= ~(1 << (vid & 0x1F));
Joe Perches1dc32912008-07-11 15:17:08 -07004642 e1000_write_vfta(hw, index, vfta);
Jiri Pirko5622e402011-07-21 03:26:31 +00004643
4644 clear_bit(vid, adapter->active_vlans);
4645
4646 if (!e1000_vlan_used(adapter))
4647 e1000_vlan_filter_on_off(adapter, false);
Jiri Pirko8e586132011-12-08 19:52:37 -05004648
4649 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650}
4651
Joe Perches64798842008-07-11 15:17:02 -07004652static void e1000_restore_vlan(struct e1000_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653{
Jiri Pirko5622e402011-07-21 03:26:31 +00004654 u16 vid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655
Jiri Pirko5622e402011-07-21 03:26:31 +00004656 if (!e1000_vlan_used(adapter))
4657 return;
4658
4659 e1000_vlan_filter_on_off(adapter, true);
4660 for_each_set_bit(vid, adapter->active_vlans, VLAN_N_VID)
4661 e1000_vlan_rx_add_vid(adapter->netdev, vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662}
4663
David Decotigny14ad2512011-04-27 18:32:43 +00004664int e1000_set_spd_dplx(struct e1000_adapter *adapter, u32 spd, u8 dplx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665{
Joe Perches1dc32912008-07-11 15:17:08 -07004666 struct e1000_hw *hw = &adapter->hw;
4667
4668 hw->autoneg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669
David Decotigny14ad2512011-04-27 18:32:43 +00004670 /* Make sure dplx is at most 1 bit and lsb of speed is not set
4671 * for the switch() below to work */
4672 if ((spd & 1) || (dplx & ~1))
4673 goto err_inval;
4674
Malli Chilakala69213682005-06-17 17:44:20 -07004675 /* Fiber NICs only allow 1000 gbps Full duplex */
Joe Perches1dc32912008-07-11 15:17:08 -07004676 if ((hw->media_type == e1000_media_type_fiber) &&
David Decotigny14ad2512011-04-27 18:32:43 +00004677 spd != SPEED_1000 &&
4678 dplx != DUPLEX_FULL)
4679 goto err_inval;
Malli Chilakala69213682005-06-17 17:44:20 -07004680
David Decotigny14ad2512011-04-27 18:32:43 +00004681 switch (spd + dplx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682 case SPEED_10 + DUPLEX_HALF:
Joe Perches1dc32912008-07-11 15:17:08 -07004683 hw->forced_speed_duplex = e1000_10_half;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684 break;
4685 case SPEED_10 + DUPLEX_FULL:
Joe Perches1dc32912008-07-11 15:17:08 -07004686 hw->forced_speed_duplex = e1000_10_full;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687 break;
4688 case SPEED_100 + DUPLEX_HALF:
Joe Perches1dc32912008-07-11 15:17:08 -07004689 hw->forced_speed_duplex = e1000_100_half;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690 break;
4691 case SPEED_100 + DUPLEX_FULL:
Joe Perches1dc32912008-07-11 15:17:08 -07004692 hw->forced_speed_duplex = e1000_100_full;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693 break;
4694 case SPEED_1000 + DUPLEX_FULL:
Joe Perches1dc32912008-07-11 15:17:08 -07004695 hw->autoneg = 1;
4696 hw->autoneg_advertised = ADVERTISE_1000_FULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697 break;
4698 case SPEED_1000 + DUPLEX_HALF: /* not supported */
4699 default:
David Decotigny14ad2512011-04-27 18:32:43 +00004700 goto err_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701 }
4702 return 0;
David Decotigny14ad2512011-04-27 18:32:43 +00004703
4704err_inval:
4705 e_err(probe, "Unsupported Speed/Duplex configuration\n");
4706 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707}
4708
Rafael J. Wysockib43fcd72009-04-15 17:43:24 +00004709static int __e1000_shutdown(struct pci_dev *pdev, bool *enable_wake)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710{
4711 struct net_device *netdev = pci_get_drvdata(pdev);
Malli Chilakala60490fe2005-06-17 17:41:45 -07004712 struct e1000_adapter *adapter = netdev_priv(netdev);
Joe Perches1dc32912008-07-11 15:17:08 -07004713 struct e1000_hw *hw = &adapter->hw;
Joe Perches406874a2008-04-03 10:06:32 -07004714 u32 ctrl, ctrl_ext, rctl, status;
4715 u32 wufc = adapter->wol;
Auke Kok6fdfef12006-06-27 09:06:36 -07004716#ifdef CONFIG_PM
Jeff Kirsher240b1712006-01-12 16:51:28 -08004717 int retval = 0;
Auke Kok6fdfef12006-06-27 09:06:36 -07004718#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719
4720 netif_device_detach(netdev);
4721
Auke Kok2db10a02006-06-27 09:06:28 -07004722 if (netif_running(netdev)) {
4723 WARN_ON(test_bit(__E1000_RESETTING, &adapter->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724 e1000_down(adapter);
Auke Kok2db10a02006-06-27 09:06:28 -07004725 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004726
Jesse Brandeburg2f826652006-01-18 13:01:34 -08004727#ifdef CONFIG_PM
Kok, Auke1d33e9c2007-02-16 14:39:28 -08004728 retval = pci_save_state(pdev);
Jesse Brandeburg3a3847e2012-01-04 20:23:33 +00004729 if (retval)
Jesse Brandeburg2f826652006-01-18 13:01:34 -08004730 return retval;
4731#endif
4732
Joe Perches1dc32912008-07-11 15:17:08 -07004733 status = er32(STATUS);
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004734 if (status & E1000_STATUS_LU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004735 wufc &= ~E1000_WUFC_LNKC;
4736
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004737 if (wufc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738 e1000_setup_rctl(adapter);
Patrick McHardydb0ce502007-11-13 20:54:59 -08004739 e1000_set_rx_mode(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740
4741 /* turn on all-multi mode if wake on multicast is enabled */
Jesse Brandeburg120cd572006-08-31 14:27:46 -07004742 if (wufc & E1000_WUFC_MC) {
Joe Perches1dc32912008-07-11 15:17:08 -07004743 rctl = er32(RCTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744 rctl |= E1000_RCTL_MPE;
Joe Perches1dc32912008-07-11 15:17:08 -07004745 ew32(RCTL, rctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746 }
4747
Joe Perches1dc32912008-07-11 15:17:08 -07004748 if (hw->mac_type >= e1000_82540) {
4749 ctrl = er32(CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750 /* advertise wake from D3Cold */
4751 #define E1000_CTRL_ADVD3WUC 0x00100000
4752 /* phy power management enable */
4753 #define E1000_CTRL_EN_PHY_PWR_MGMT 0x00200000
4754 ctrl |= E1000_CTRL_ADVD3WUC |
4755 E1000_CTRL_EN_PHY_PWR_MGMT;
Joe Perches1dc32912008-07-11 15:17:08 -07004756 ew32(CTRL, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757 }
4758
Joe Perches1dc32912008-07-11 15:17:08 -07004759 if (hw->media_type == e1000_media_type_fiber ||
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00004760 hw->media_type == e1000_media_type_internal_serdes) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761 /* keep the laser running in D3 */
Joe Perches1dc32912008-07-11 15:17:08 -07004762 ctrl_ext = er32(CTRL_EXT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763 ctrl_ext |= E1000_CTRL_EXT_SDP7_DATA;
Joe Perches1dc32912008-07-11 15:17:08 -07004764 ew32(CTRL_EXT, ctrl_ext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765 }
4766
Joe Perches1dc32912008-07-11 15:17:08 -07004767 ew32(WUC, E1000_WUC_PME_EN);
4768 ew32(WUFC, wufc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769 } else {
Joe Perches1dc32912008-07-11 15:17:08 -07004770 ew32(WUC, 0);
4771 ew32(WUFC, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772 }
4773
Jeff Garzik0fccd0e2006-12-15 10:56:10 -05004774 e1000_release_manageability(adapter);
4775
Rafael J. Wysockib43fcd72009-04-15 17:43:24 +00004776 *enable_wake = !!wufc;
4777
Jeff Garzik0fccd0e2006-12-15 10:56:10 -05004778 /* make sure adapter isn't asleep if manageability is enabled */
Rafael J. Wysockib43fcd72009-04-15 17:43:24 +00004779 if (adapter->en_mng_pt)
4780 *enable_wake = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781
Auke Kokedd106f2006-11-06 08:57:12 -08004782 if (netif_running(netdev))
4783 e1000_free_irq(adapter);
4784
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785 pci_disable_device(pdev);
Jeff Kirsher240b1712006-01-12 16:51:28 -08004786
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787 return 0;
4788}
4789
Jesse Brandeburg2f826652006-01-18 13:01:34 -08004790#ifdef CONFIG_PM
Rafael J. Wysockib43fcd72009-04-15 17:43:24 +00004791static int e1000_suspend(struct pci_dev *pdev, pm_message_t state)
4792{
4793 int retval;
4794 bool wake;
4795
4796 retval = __e1000_shutdown(pdev, &wake);
4797 if (retval)
4798 return retval;
4799
4800 if (wake) {
4801 pci_prepare_to_sleep(pdev);
4802 } else {
4803 pci_wake_from_d3(pdev, false);
4804 pci_set_power_state(pdev, PCI_D3hot);
4805 }
4806
4807 return 0;
4808}
4809
Joe Perches64798842008-07-11 15:17:02 -07004810static int e1000_resume(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811{
4812 struct net_device *netdev = pci_get_drvdata(pdev);
Malli Chilakala60490fe2005-06-17 17:41:45 -07004813 struct e1000_adapter *adapter = netdev_priv(netdev);
Joe Perches1dc32912008-07-11 15:17:08 -07004814 struct e1000_hw *hw = &adapter->hw;
Joe Perches406874a2008-04-03 10:06:32 -07004815 u32 err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004816
Auke Kokd0e027d2006-04-14 19:04:40 -07004817 pci_set_power_state(pdev, PCI_D0);
Kok, Auke1d33e9c2007-02-16 14:39:28 -08004818 pci_restore_state(pdev);
Nick Nunleydbb5aae2010-02-03 14:49:48 +00004819 pci_save_state(pdev);
Taku Izumi81250292008-07-11 15:17:44 -07004820
4821 if (adapter->need_ioport)
4822 err = pci_enable_device(pdev);
4823 else
4824 err = pci_enable_device_mem(pdev);
Joe Perchesc7be73b2008-07-11 15:17:28 -07004825 if (err) {
Emil Tantilov675ad472010-04-27 14:02:58 +00004826 pr_err("Cannot enable PCI device from suspend\n");
Auke Kok3d1dd8c2006-08-28 14:56:27 -07004827 return err;
4828 }
Malli Chilakalaa4cb8472005-04-28 19:41:28 -07004829 pci_set_master(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830
Auke Kokd0e027d2006-04-14 19:04:40 -07004831 pci_enable_wake(pdev, PCI_D3hot, 0);
4832 pci_enable_wake(pdev, PCI_D3cold, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833
Joe Perchesc7be73b2008-07-11 15:17:28 -07004834 if (netif_running(netdev)) {
4835 err = e1000_request_irq(adapter);
4836 if (err)
4837 return err;
4838 }
Auke Kokedd106f2006-11-06 08:57:12 -08004839
4840 e1000_power_up_phy(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841 e1000_reset(adapter);
Joe Perches1dc32912008-07-11 15:17:08 -07004842 ew32(WUS, ~0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843
Jeff Garzik0fccd0e2006-12-15 10:56:10 -05004844 e1000_init_manageability(adapter);
4845
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004846 if (netif_running(netdev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847 e1000_up(adapter);
4848
4849 netif_device_attach(netdev);
4850
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851 return 0;
4852}
4853#endif
Auke Kokc653e632006-05-23 13:35:57 -07004854
4855static void e1000_shutdown(struct pci_dev *pdev)
4856{
Rafael J. Wysockib43fcd72009-04-15 17:43:24 +00004857 bool wake;
4858
4859 __e1000_shutdown(pdev, &wake);
4860
4861 if (system_state == SYSTEM_POWER_OFF) {
4862 pci_wake_from_d3(pdev, wake);
4863 pci_set_power_state(pdev, PCI_D3hot);
4864 }
Auke Kokc653e632006-05-23 13:35:57 -07004865}
4866
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867#ifdef CONFIG_NET_POLL_CONTROLLER
4868/*
4869 * Polling 'interrupt' - used by things like netconsole to send skbs
4870 * without having to re-enable interrupts. It's not called while
4871 * the interrupt routine is executing.
4872 */
Joe Perches64798842008-07-11 15:17:02 -07004873static void e1000_netpoll(struct net_device *netdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874{
Malli Chilakala60490fe2005-06-17 17:41:45 -07004875 struct e1000_adapter *adapter = netdev_priv(netdev);
Auke Kokd3d9e482006-07-14 16:14:23 -07004876
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877 disable_irq(adapter->pdev->irq);
David Howells7d12e782006-10-05 14:55:46 +01004878 e1000_intr(adapter->pdev->irq, netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879 enable_irq(adapter->pdev->irq);
4880}
4881#endif
4882
Auke Kok90267292006-06-08 09:30:24 -07004883/**
4884 * e1000_io_error_detected - called when PCI error is detected
4885 * @pdev: Pointer to PCI device
Jesse Brandeburg120a5d02009-09-25 15:19:46 -07004886 * @state: The current pci connection state
Auke Kok90267292006-06-08 09:30:24 -07004887 *
4888 * This function is called after a PCI bus error affecting
4889 * this device has been detected.
4890 */
Joe Perches64798842008-07-11 15:17:02 -07004891static pci_ers_result_t e1000_io_error_detected(struct pci_dev *pdev,
4892 pci_channel_state_t state)
Auke Kok90267292006-06-08 09:30:24 -07004893{
4894 struct net_device *netdev = pci_get_drvdata(pdev);
Wang Chen4cf16532008-11-12 23:38:14 -08004895 struct e1000_adapter *adapter = netdev_priv(netdev);
Auke Kok90267292006-06-08 09:30:24 -07004896
4897 netif_device_detach(netdev);
4898
Andre Detscheab63302009-06-30 12:46:13 +00004899 if (state == pci_channel_io_perm_failure)
4900 return PCI_ERS_RESULT_DISCONNECT;
4901
Auke Kok90267292006-06-08 09:30:24 -07004902 if (netif_running(netdev))
4903 e1000_down(adapter);
Linas Vepstas72e8d6b2006-09-18 20:58:06 -07004904 pci_disable_device(pdev);
Auke Kok90267292006-06-08 09:30:24 -07004905
4906 /* Request a slot slot reset. */
4907 return PCI_ERS_RESULT_NEED_RESET;
4908}
4909
4910/**
4911 * e1000_io_slot_reset - called after the pci bus has been reset.
4912 * @pdev: Pointer to PCI device
4913 *
4914 * Restart the card from scratch, as if from a cold-boot. Implementation
4915 * resembles the first-half of the e1000_resume routine.
4916 */
4917static pci_ers_result_t e1000_io_slot_reset(struct pci_dev *pdev)
4918{
4919 struct net_device *netdev = pci_get_drvdata(pdev);
Wang Chen4cf16532008-11-12 23:38:14 -08004920 struct e1000_adapter *adapter = netdev_priv(netdev);
Joe Perches1dc32912008-07-11 15:17:08 -07004921 struct e1000_hw *hw = &adapter->hw;
Taku Izumi81250292008-07-11 15:17:44 -07004922 int err;
Auke Kok90267292006-06-08 09:30:24 -07004923
Taku Izumi81250292008-07-11 15:17:44 -07004924 if (adapter->need_ioport)
4925 err = pci_enable_device(pdev);
4926 else
4927 err = pci_enable_device_mem(pdev);
4928 if (err) {
Emil Tantilov675ad472010-04-27 14:02:58 +00004929 pr_err("Cannot re-enable PCI device after reset.\n");
Auke Kok90267292006-06-08 09:30:24 -07004930 return PCI_ERS_RESULT_DISCONNECT;
4931 }
4932 pci_set_master(pdev);
4933
Linas Vepstasdbf38c92006-09-27 12:54:11 -07004934 pci_enable_wake(pdev, PCI_D3hot, 0);
4935 pci_enable_wake(pdev, PCI_D3cold, 0);
Auke Kok90267292006-06-08 09:30:24 -07004936
Auke Kok90267292006-06-08 09:30:24 -07004937 e1000_reset(adapter);
Joe Perches1dc32912008-07-11 15:17:08 -07004938 ew32(WUS, ~0);
Auke Kok90267292006-06-08 09:30:24 -07004939
4940 return PCI_ERS_RESULT_RECOVERED;
4941}
4942
4943/**
4944 * e1000_io_resume - called when traffic can start flowing again.
4945 * @pdev: Pointer to PCI device
4946 *
4947 * This callback is called when the error recovery driver tells us that
4948 * its OK to resume normal operation. Implementation resembles the
4949 * second-half of the e1000_resume routine.
4950 */
4951static void e1000_io_resume(struct pci_dev *pdev)
4952{
4953 struct net_device *netdev = pci_get_drvdata(pdev);
Wang Chen4cf16532008-11-12 23:38:14 -08004954 struct e1000_adapter *adapter = netdev_priv(netdev);
Jeff Garzik0fccd0e2006-12-15 10:56:10 -05004955
4956 e1000_init_manageability(adapter);
Auke Kok90267292006-06-08 09:30:24 -07004957
4958 if (netif_running(netdev)) {
4959 if (e1000_up(adapter)) {
Emil Tantilov675ad472010-04-27 14:02:58 +00004960 pr_info("can't bring device back up after reset\n");
Auke Kok90267292006-06-08 09:30:24 -07004961 return;
4962 }
4963 }
4964
4965 netif_device_attach(netdev);
Auke Kok90267292006-06-08 09:30:24 -07004966}
4967
Linus Torvalds1da177e2005-04-16 15:20:36 -07004968/* e1000_main.c */