blob: 8d0d0d420c21886ad665715e9b9512d54a6c71c2 [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);
Bill Pemberton9f9a12f2012-12-03 09:24:25 -0500114static void 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 Pirko52f55092012-03-20 18:10:01 +0000167static void e1000_vlan_filter_on_off(struct e1000_adapter *adapter,
168 bool filter_on);
Jiri Pirko8e586132011-12-08 19:52:37 -0500169static int e1000_vlan_rx_add_vid(struct net_device *netdev, u16 vid);
170static int e1000_vlan_rx_kill_vid(struct net_device *netdev, u16 vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171static void e1000_restore_vlan(struct e1000_adapter *adapter);
172
Auke Kok6fdfef12006-06-27 09:06:36 -0700173#ifdef CONFIG_PM
Rafael J. Wysockib43fcd72009-04-15 17:43:24 +0000174static int e1000_suspend(struct pci_dev *pdev, pm_message_t state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175static int e1000_resume(struct pci_dev *pdev);
176#endif
Auke Kokc653e632006-05-23 13:35:57 -0700177static void e1000_shutdown(struct pci_dev *pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179#ifdef CONFIG_NET_POLL_CONTROLLER
180/* for netdump / net console */
181static void e1000_netpoll (struct net_device *netdev);
182#endif
183
Jesse Brandeburg1f753862006-12-15 10:40:39 +0100184#define COPYBREAK_DEFAULT 256
185static unsigned int copybreak __read_mostly = COPYBREAK_DEFAULT;
186module_param(copybreak, uint, 0644);
187MODULE_PARM_DESC(copybreak,
188 "Maximum size of packet that is copied to a new buffer on receive");
189
Auke Kok90267292006-06-08 09:30:24 -0700190static pci_ers_result_t e1000_io_error_detected(struct pci_dev *pdev,
191 pci_channel_state_t state);
192static pci_ers_result_t e1000_io_slot_reset(struct pci_dev *pdev);
193static void e1000_io_resume(struct pci_dev *pdev);
194
Stephen Hemminger3646f0e2012-09-07 09:33:15 -0700195static const struct pci_error_handlers e1000_err_handler = {
Auke Kok90267292006-06-08 09:30:24 -0700196 .error_detected = e1000_io_error_detected,
197 .slot_reset = e1000_io_slot_reset,
198 .resume = e1000_io_resume,
199};
Mallikarjuna R Chilakala24025e4ec2005-10-04 07:03:23 -0400200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201static struct pci_driver e1000_driver = {
202 .name = e1000_driver_name,
203 .id_table = e1000_pci_tbl,
204 .probe = e1000_probe,
Bill Pemberton9f9a12f2012-12-03 09:24:25 -0500205 .remove = e1000_remove,
Auke Kokc4e24f02006-09-27 12:53:19 -0700206#ifdef CONFIG_PM
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300207 /* Power Management Hooks */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 .suspend = e1000_suspend,
Auke Kokc653e632006-05-23 13:35:57 -0700209 .resume = e1000_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210#endif
Auke Kok90267292006-06-08 09:30:24 -0700211 .shutdown = e1000_shutdown,
212 .err_handler = &e1000_err_handler
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213};
214
215MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
216MODULE_DESCRIPTION("Intel(R) PRO/1000 Network Driver");
217MODULE_LICENSE("GPL");
218MODULE_VERSION(DRV_VERSION);
219
stephen hemmingerb3f4d592012-03-13 06:04:20 +0000220#define DEFAULT_MSG_ENABLE (NETIF_MSG_DRV|NETIF_MSG_PROBE|NETIF_MSG_LINK)
221static int debug = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222module_param(debug, int, 0);
223MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
224
225/**
Emil Tantilov675ad472010-04-27 14:02:58 +0000226 * e1000_get_hw_dev - return device
227 * used by hardware layer to print debugging information
228 *
229 **/
230struct net_device *e1000_get_hw_dev(struct e1000_hw *hw)
231{
232 struct e1000_adapter *adapter = hw->back;
233 return adapter->netdev;
234}
235
236/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 * e1000_init_module - Driver Registration Routine
238 *
239 * e1000_init_module is the first routine called when the driver is
240 * loaded. All it does is register with the PCI subsystem.
241 **/
Joe Perches64798842008-07-11 15:17:02 -0700242static int __init e1000_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
244 int ret;
Emil Tantilov675ad472010-04-27 14:02:58 +0000245 pr_info("%s - version %s\n", e1000_driver_string, e1000_driver_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Emil Tantilov675ad472010-04-27 14:02:58 +0000247 pr_info("%s\n", e1000_copyright);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Jeff Garzik29917622006-08-19 17:48:59 -0400249 ret = pci_register_driver(&e1000_driver);
Jesse Brandeburg1f753862006-12-15 10:40:39 +0100250 if (copybreak != COPYBREAK_DEFAULT) {
251 if (copybreak == 0)
Emil Tantilov675ad472010-04-27 14:02:58 +0000252 pr_info("copybreak disabled\n");
Jesse Brandeburg1f753862006-12-15 10:40:39 +0100253 else
Emil Tantilov675ad472010-04-27 14:02:58 +0000254 pr_info("copybreak enabled for "
255 "packets <= %u bytes\n", copybreak);
Jesse Brandeburg1f753862006-12-15 10:40:39 +0100256 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 return ret;
258}
259
260module_init(e1000_init_module);
261
262/**
263 * e1000_exit_module - Driver Exit Cleanup Routine
264 *
265 * e1000_exit_module is called just before the driver is removed
266 * from memory.
267 **/
Joe Perches64798842008-07-11 15:17:02 -0700268static void __exit e1000_exit_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 pci_unregister_driver(&e1000_driver);
271}
272
273module_exit(e1000_exit_module);
274
Auke Kok2db10a02006-06-27 09:06:28 -0700275static int e1000_request_irq(struct e1000_adapter *adapter)
276{
277 struct net_device *netdev = adapter->netdev;
Al Viro3e188262007-12-11 19:49:39 +0000278 irq_handler_t handler = e1000_intr;
Auke Koke94bd232007-05-16 01:49:46 -0700279 int irq_flags = IRQF_SHARED;
280 int err;
Auke Kok2db10a02006-06-27 09:06:28 -0700281
Auke Koke94bd232007-05-16 01:49:46 -0700282 err = request_irq(adapter->pdev->irq, handler, irq_flags, netdev->name,
283 netdev);
284 if (err) {
Emil Tantilovfeb8f472010-07-26 23:37:21 -0700285 e_err(probe, "Unable to allocate interrupt Error: %d\n", err);
Auke Koke94bd232007-05-16 01:49:46 -0700286 }
Auke Kok2db10a02006-06-27 09:06:28 -0700287
288 return err;
289}
290
291static void e1000_free_irq(struct e1000_adapter *adapter)
292{
293 struct net_device *netdev = adapter->netdev;
294
295 free_irq(adapter->pdev->irq, netdev);
Auke Kok2db10a02006-06-27 09:06:28 -0700296}
297
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298/**
299 * e1000_irq_disable - Mask off interrupt generation on the NIC
300 * @adapter: board private structure
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 **/
Joe Perches64798842008-07-11 15:17:02 -0700315static void e1000_irq_enable(struct e1000_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316{
Joe Perches1dc32912008-07-11 15:17:08 -0700317 struct e1000_hw *hw = &adapter->hw;
318
319 ew32(IMS, IMS_ENABLE_MASK);
320 E1000_WRITE_FLUSH();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321}
Adrian Bunk3ad2cc62005-10-30 16:53:34 +0100322
Joe Perches64798842008-07-11 15:17:02 -0700323static void e1000_update_mng_vlan(struct e1000_adapter *adapter)
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700324{
Joe Perches1dc32912008-07-11 15:17:08 -0700325 struct e1000_hw *hw = &adapter->hw;
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700326 struct net_device *netdev = adapter->netdev;
Joe Perches1dc32912008-07-11 15:17:08 -0700327 u16 vid = hw->mng_cookie.vlan_id;
Joe Perches406874a2008-04-03 10:06:32 -0700328 u16 old_vid = adapter->mng_vlan_id;
Jesse Brandeburg96838a42006-01-18 13:01:39 -0800329
Jiri Pirko5622e402011-07-21 03:26:31 +0000330 if (!e1000_vlan_used(adapter))
331 return;
332
333 if (!test_bit(vid, adapter->active_vlans)) {
334 if (hw->mng_cookie.status &
335 E1000_MNG_DHCP_COOKIE_STATUS_VLAN_SUPPORT) {
336 e1000_vlan_rx_add_vid(netdev, vid);
Jeff Kirsherc5f226f2006-03-02 18:17:55 -0800337 adapter->mng_vlan_id = vid;
Jiri Pirko5622e402011-07-21 03:26:31 +0000338 } else {
339 adapter->mng_vlan_id = E1000_MNG_VLAN_NONE;
340 }
341 if ((old_vid != (u16)E1000_MNG_VLAN_NONE) &&
342 (vid != old_vid) &&
343 !test_bit(old_vid, adapter->active_vlans))
344 e1000_vlan_rx_kill_vid(netdev, old_vid);
345 } else {
346 adapter->mng_vlan_id = vid;
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700347 }
348}
Jeff Kirsherb55ccb32006-01-12 16:50:30 -0800349
Joe Perches64798842008-07-11 15:17:02 -0700350static void e1000_init_manageability(struct e1000_adapter *adapter)
Jeff Garzik0fccd0e2006-12-15 10:56:10 -0500351{
Joe Perches1dc32912008-07-11 15:17:08 -0700352 struct e1000_hw *hw = &adapter->hw;
353
Jeff Garzik0fccd0e2006-12-15 10:56:10 -0500354 if (adapter->en_mng_pt) {
Joe Perches1dc32912008-07-11 15:17:08 -0700355 u32 manc = er32(MANC);
Jeff Garzik0fccd0e2006-12-15 10:56:10 -0500356
357 /* disable hardware interception of ARP */
358 manc &= ~(E1000_MANC_ARP_EN);
359
Joe Perches1dc32912008-07-11 15:17:08 -0700360 ew32(MANC, manc);
Jeff Garzik0fccd0e2006-12-15 10:56:10 -0500361 }
362}
363
Joe Perches64798842008-07-11 15:17:02 -0700364static void e1000_release_manageability(struct e1000_adapter *adapter)
Jeff Garzik0fccd0e2006-12-15 10:56:10 -0500365{
Joe Perches1dc32912008-07-11 15:17:08 -0700366 struct e1000_hw *hw = &adapter->hw;
367
Jeff Garzik0fccd0e2006-12-15 10:56:10 -0500368 if (adapter->en_mng_pt) {
Joe Perches1dc32912008-07-11 15:17:08 -0700369 u32 manc = er32(MANC);
Jeff Garzik0fccd0e2006-12-15 10:56:10 -0500370
371 /* re-enable hardware interception of ARP */
372 manc |= E1000_MANC_ARP_EN;
373
Joe Perches1dc32912008-07-11 15:17:08 -0700374 ew32(MANC, manc);
Jeff Garzik0fccd0e2006-12-15 10:56:10 -0500375 }
376}
377
Auke Koke0aac5a2007-03-06 08:57:21 -0800378/**
379 * e1000_configure - configure the hardware for RX and TX
380 * @adapter = private board structure
381 **/
382static void e1000_configure(struct e1000_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383{
384 struct net_device *netdev = adapter->netdev;
Auke Kok2db10a02006-06-27 09:06:28 -0700385 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Patrick McHardydb0ce502007-11-13 20:54:59 -0800387 e1000_set_rx_mode(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
389 e1000_restore_vlan(adapter);
Jeff Garzik0fccd0e2006-12-15 10:56:10 -0500390 e1000_init_manageability(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
392 e1000_configure_tx(adapter);
393 e1000_setup_rctl(adapter);
394 e1000_configure_rx(adapter);
Jeff Kirsher72d64a42006-01-12 16:51:19 -0800395 /* call E1000_DESC_UNUSED which always leaves
396 * at least 1 descriptor unused to make sure
Jeff Kirsher6cfbd972013-02-09 12:49:21 +0000397 * next_to_use != next_to_clean
398 */
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,
Jeff Kirsher6cfbd972013-02-09 12:49:21 +0000402 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 ***
Auke Kok79f05bf2006-06-27 09:06:32 -0700433 **/
Jesse Brandeburgd6582662006-08-16 13:31:33 -0700434void e1000_power_up_phy(struct e1000_adapter *adapter)
Auke Kok79f05bf2006-06-27 09:06:32 -0700435{
Joe Perches1dc32912008-07-11 15:17:08 -0700436 struct e1000_hw *hw = &adapter->hw;
Joe Perches406874a2008-04-03 10:06:32 -0700437 u16 mii_reg = 0;
Auke Kok79f05bf2006-06-27 09:06:32 -0700438
439 /* Just clear the power down bit to wake the phy back up */
Joe Perches1dc32912008-07-11 15:17:08 -0700440 if (hw->media_type == e1000_media_type_copper) {
Auke Kok79f05bf2006-06-27 09:06:32 -0700441 /* according to the manual, the phy will retain its
Jeff Kirsher6cfbd972013-02-09 12:49:21 +0000442 * settings across a power-down/up cycle
443 */
Joe Perches1dc32912008-07-11 15:17:08 -0700444 e1000_read_phy_reg(hw, PHY_CTRL, &mii_reg);
Auke Kok79f05bf2006-06-27 09:06:32 -0700445 mii_reg &= ~MII_CR_POWER_DOWN;
Joe Perches1dc32912008-07-11 15:17:08 -0700446 e1000_write_phy_reg(hw, PHY_CTRL, mii_reg);
Auke Kok79f05bf2006-06-27 09:06:32 -0700447 }
448}
449
450static void e1000_power_down_phy(struct e1000_adapter *adapter)
451{
Joe Perches1dc32912008-07-11 15:17:08 -0700452 struct e1000_hw *hw = &adapter->hw;
453
Bruce Allan61c25052006-09-27 12:53:54 -0700454 /* Power down the PHY so no link is implied when interface is down *
Joe Perchesc3033b02008-03-21 11:06:25 -0700455 * The PHY cannot be powered down if any of the following is true *
Auke Kok79f05bf2006-06-27 09:06:32 -0700456 * (a) WoL is enabled
457 * (b) AMT is active
Jeff Kirsher6cfbd972013-02-09 12:49:21 +0000458 * (c) SoL/IDER session is active
459 */
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 Brandeburg4e0d8f7d2011-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);
Tushar Dave8ce69092012-05-17 01:04:50 +0000493
494 /* Only kill reset task if adapter is not resetting */
495 if (!test_bit(__E1000_RESETTING, &adapter->flags))
496 cancel_work_sync(&adapter->reset_task);
497
Jesse Brandeburga4010af2011-10-05 07:24:41 +0000498 cancel_delayed_work_sync(&adapter->watchdog_task);
499 cancel_delayed_work_sync(&adapter->phy_info_task);
500 cancel_delayed_work_sync(&adapter->fifo_stall_task);
501}
502
Joe Perches64798842008-07-11 15:17:02 -0700503void e1000_down(struct e1000_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504{
Jesse Brandeburga6c42322009-03-25 21:59:22 +0000505 struct e1000_hw *hw = &adapter->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 struct net_device *netdev = adapter->netdev;
Jesse Brandeburga6c42322009-03-25 21:59:22 +0000507 u32 rctl, tctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Auke Kok1314bbf2006-09-27 12:54:02 -0700509
Jesse Brandeburga6c42322009-03-25 21:59:22 +0000510 /* disable receives in the hardware */
511 rctl = er32(RCTL);
512 ew32(RCTL, rctl & ~E1000_RCTL_EN);
513 /* flush and sleep below */
514
Jesse Brandeburg51851072009-09-25 12:17:01 +0000515 netif_tx_disable(netdev);
Jesse Brandeburga6c42322009-03-25 21:59:22 +0000516
517 /* disable transmits in the hardware */
518 tctl = er32(TCTL);
519 tctl &= ~E1000_TCTL_EN;
520 ew32(TCTL, tctl);
521 /* flush both disables and wait for them to finish */
522 E1000_WRITE_FLUSH();
523 msleep(10);
524
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700525 napi_disable(&adapter->napi);
Francois Romieuc3570ac2008-07-11 15:17:38 -0700526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 e1000_irq_disable(adapter);
Jeff Kirsherc1605eb2006-03-02 18:16:38 -0800528
Jeff Kirsher6cfbd972013-02-09 12:49:21 +0000529 /* Setting DOWN must be after irq_disable to prevent
Anupam Chandaab088532010-11-21 09:54:21 -0800530 * a screaming interrupt. Setting DOWN also prevents
Jesse Brandeburga4010af2011-10-05 07:24:41 +0000531 * tasks from rescheduling.
Anupam Chandaab088532010-11-21 09:54:21 -0800532 */
Jesse Brandeburga4010af2011-10-05 07:24:41 +0000533 e1000_down_and_stop(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 adapter->link_speed = 0;
536 adapter->link_duplex = 0;
537 netif_carrier_off(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
539 e1000_reset(adapter);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -0400540 e1000_clean_all_tx_rings(adapter);
541 e1000_clean_all_rx_rings(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
stephen hemminger38df7a32010-10-21 07:50:57 +0000544static void e1000_reinit_safe(struct e1000_adapter *adapter)
Jesse Brandeburg338c15e2010-09-22 18:22:42 +0000545{
546 while (test_and_set_bit(__E1000_RESETTING, &adapter->flags))
547 msleep(1);
Jesse Brandeburg0ef4eed2011-10-05 07:24:51 +0000548 mutex_lock(&adapter->mutex);
Jesse Brandeburg338c15e2010-09-22 18:22:42 +0000549 e1000_down(adapter);
550 e1000_up(adapter);
Jesse Brandeburg0ef4eed2011-10-05 07:24:51 +0000551 mutex_unlock(&adapter->mutex);
Jesse Brandeburg338c15e2010-09-22 18:22:42 +0000552 clear_bit(__E1000_RESETTING, &adapter->flags);
553}
554
Joe Perches64798842008-07-11 15:17:02 -0700555void e1000_reinit_locked(struct e1000_adapter *adapter)
Auke Kok2db10a02006-06-27 09:06:28 -0700556{
Jesse Brandeburg338c15e2010-09-22 18:22:42 +0000557 /* if rtnl_lock is not held the call path is bogus */
558 ASSERT_RTNL();
Auke Kok2db10a02006-06-27 09:06:28 -0700559 WARN_ON(in_interrupt());
560 while (test_and_set_bit(__E1000_RESETTING, &adapter->flags))
561 msleep(1);
562 e1000_down(adapter);
563 e1000_up(adapter);
564 clear_bit(__E1000_RESETTING, &adapter->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565}
566
Joe Perches64798842008-07-11 15:17:02 -0700567void e1000_reset(struct e1000_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
Joe Perches1dc32912008-07-11 15:17:08 -0700569 struct e1000_hw *hw = &adapter->hw;
Joe Perches406874a2008-04-03 10:06:32 -0700570 u32 pba = 0, tx_space, min_tx_space, min_rx_space;
Joe Perchesc3033b02008-03-21 11:06:25 -0700571 bool legacy_pba_adjust = false;
Jesse Brandeburgb7cb8c22009-07-06 10:45:01 +0000572 u16 hwm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
574 /* Repartition Pba for greater than 9k mtu
575 * To take effect CTRL.RST is required.
576 */
577
Joe Perches1dc32912008-07-11 15:17:08 -0700578 switch (hw->mac_type) {
Bruce Allan018ea442006-12-15 10:39:45 +0100579 case e1000_82542_rev2_0:
580 case e1000_82542_rev2_1:
581 case e1000_82543:
582 case e1000_82544:
583 case e1000_82540:
584 case e1000_82541:
585 case e1000_82541_rev_2:
Joe Perchesc3033b02008-03-21 11:06:25 -0700586 legacy_pba_adjust = true;
Bruce Allan018ea442006-12-15 10:39:45 +0100587 pba = E1000_PBA_48K;
588 break;
589 case e1000_82545:
590 case e1000_82545_rev_3:
591 case e1000_82546:
Dirk Brandewie5377a412011-01-06 14:29:54 +0000592 case e1000_ce4100:
Bruce Allan018ea442006-12-15 10:39:45 +0100593 case e1000_82546_rev_3:
594 pba = E1000_PBA_48K;
595 break;
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700596 case e1000_82547:
Malli Chilakala0e6ef3e2005-04-28 19:44:14 -0700597 case e1000_82547_rev_2:
Joe Perchesc3033b02008-03-21 11:06:25 -0700598 legacy_pba_adjust = true;
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700599 pba = E1000_PBA_30K;
600 break;
Bruce Allan018ea442006-12-15 10:39:45 +0100601 case e1000_undefined:
602 case e1000_num_macs:
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700603 break;
604 }
605
Joe Perchesc3033b02008-03-21 11:06:25 -0700606 if (legacy_pba_adjust) {
Jesse Brandeburgb7cb8c22009-07-06 10:45:01 +0000607 if (hw->max_frame_size > E1000_RXBUFFER_8192)
Bruce Allan018ea442006-12-15 10:39:45 +0100608 pba -= 8; /* allocate more FIFO for Tx */
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700609
Joe Perches1dc32912008-07-11 15:17:08 -0700610 if (hw->mac_type == e1000_82547) {
Bruce Allan018ea442006-12-15 10:39:45 +0100611 adapter->tx_fifo_head = 0;
612 adapter->tx_head_addr = pba << E1000_TX_HEAD_ADDR_SHIFT;
613 adapter->tx_fifo_size =
614 (E1000_PBA_40K - pba) << E1000_PBA_BYTES_SHIFT;
615 atomic_set(&adapter->tx_fifo_stall, 0);
616 }
Jesse Brandeburgb7cb8c22009-07-06 10:45:01 +0000617 } else if (hw->max_frame_size > ETH_FRAME_LEN + ETH_FCS_LEN) {
Bruce Allan018ea442006-12-15 10:39:45 +0100618 /* adjust PBA for jumbo frames */
Joe Perches1dc32912008-07-11 15:17:08 -0700619 ew32(PBA, pba);
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700620
Bruce Allan018ea442006-12-15 10:39:45 +0100621 /* To maintain wire speed transmits, the Tx FIFO should be
Jesse Brandeburgb7cb8c22009-07-06 10:45:01 +0000622 * large enough to accommodate two full transmit packets,
Bruce Allan018ea442006-12-15 10:39:45 +0100623 * rounded up to the next 1KB and expressed in KB. Likewise,
Jesse Brandeburgb7cb8c22009-07-06 10:45:01 +0000624 * the Rx FIFO should be large enough to accommodate at least
Bruce Allan018ea442006-12-15 10:39:45 +0100625 * one full receive packet and is similarly rounded up and
Jeff Kirsher6cfbd972013-02-09 12:49:21 +0000626 * expressed in KB.
627 */
Joe Perches1dc32912008-07-11 15:17:08 -0700628 pba = er32(PBA);
Bruce Allan018ea442006-12-15 10:39:45 +0100629 /* upper 16 bits has Tx packet buffer allocation size in KB */
630 tx_space = pba >> 16;
631 /* lower 16 bits has Rx packet buffer allocation size in KB */
632 pba &= 0xffff;
Jeff Kirsher6cfbd972013-02-09 12:49:21 +0000633 /* the Tx fifo also stores 16 bytes of information about the Tx
Jesse Brandeburgb7cb8c22009-07-06 10:45:01 +0000634 * but don't include ethernet FCS because hardware appends it
635 */
636 min_tx_space = (hw->max_frame_size +
637 sizeof(struct e1000_tx_desc) -
638 ETH_FCS_LEN) * 2;
Milind Arun Choudhary9099cfb2007-04-27 13:55:29 -0700639 min_tx_space = ALIGN(min_tx_space, 1024);
Bruce Allan018ea442006-12-15 10:39:45 +0100640 min_tx_space >>= 10;
Jesse Brandeburgb7cb8c22009-07-06 10:45:01 +0000641 /* software strips receive CRC, so leave room for it */
642 min_rx_space = hw->max_frame_size;
Milind Arun Choudhary9099cfb2007-04-27 13:55:29 -0700643 min_rx_space = ALIGN(min_rx_space, 1024);
Bruce Allan018ea442006-12-15 10:39:45 +0100644 min_rx_space >>= 10;
645
646 /* If current Tx allocation is less than the min Tx FIFO size,
647 * and the min Tx FIFO size is less than the current Rx FIFO
Jeff Kirsher6cfbd972013-02-09 12:49:21 +0000648 * allocation, take space away from current Rx allocation
649 */
Bruce Allan018ea442006-12-15 10:39:45 +0100650 if (tx_space < min_tx_space &&
651 ((min_tx_space - tx_space) < pba)) {
652 pba = pba - (min_tx_space - tx_space);
653
654 /* PCI/PCIx hardware has PBA alignment constraints */
Joe Perches1dc32912008-07-11 15:17:08 -0700655 switch (hw->mac_type) {
Bruce Allan018ea442006-12-15 10:39:45 +0100656 case e1000_82545 ... e1000_82546_rev_3:
657 pba &= ~(E1000_PBA_8K - 1);
658 break;
659 default:
660 break;
661 }
662
Jeff Kirsher6cfbd972013-02-09 12:49:21 +0000663 /* if short on Rx space, Rx wins and must trump Tx
664 * adjustment or use Early Receive if available
665 */
Jesse Brandeburg1532ece2009-09-25 12:16:14 +0000666 if (pba < min_rx_space)
667 pba = min_rx_space;
Bruce Allan018ea442006-12-15 10:39:45 +0100668 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 }
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700670
Joe Perches1dc32912008-07-11 15:17:08 -0700671 ew32(PBA, pba);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
Jeff Kirsher6cfbd972013-02-09 12:49:21 +0000673 /* flow control settings:
Jesse Brandeburgb7cb8c22009-07-06 10:45:01 +0000674 * The high water mark must be low enough to fit one full frame
675 * (or the size used for early receive) above it in the Rx FIFO.
676 * Set it to the lower of:
677 * - 90% of the Rx FIFO size, and
678 * - the full Rx FIFO size minus the early receive size (for parts
679 * with ERT support assuming ERT set to E1000_ERT_2048), or
680 * - the full Rx FIFO size minus one full frame
681 */
682 hwm = min(((pba << 10) * 9 / 10),
683 ((pba << 10) - hw->max_frame_size));
Jeff Kirsherf11b7f82006-01-12 16:50:51 -0800684
Jesse Brandeburgb7cb8c22009-07-06 10:45:01 +0000685 hw->fc_high_water = hwm & 0xFFF8; /* 8-byte granularity */
686 hw->fc_low_water = hw->fc_high_water - 8;
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +0000687 hw->fc_pause_time = E1000_FC_PAUSE_TIME;
Joe Perches1dc32912008-07-11 15:17:08 -0700688 hw->fc_send_xon = 1;
689 hw->fc = hw->original_fc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700691 /* Allow time for pending master requests to run */
Joe Perches1dc32912008-07-11 15:17:08 -0700692 e1000_reset_hw(hw);
693 if (hw->mac_type >= e1000_82544)
694 ew32(WUC, 0);
Jeff Kirsher09ae3e82006-09-27 12:53:51 -0700695
Joe Perches1dc32912008-07-11 15:17:08 -0700696 if (e1000_init_hw(hw))
Emil Tantilovfeb8f472010-07-26 23:37:21 -0700697 e_dev_err("Hardware Error\n");
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700698 e1000_update_mng_vlan(adapter);
Jesse Brandeburg3d5460a2006-12-15 10:33:46 +0100699
700 /* if (adapter->hwflags & HWFLAGS_PHY_PWR_BIT) { */
Joe Perches1dc32912008-07-11 15:17:08 -0700701 if (hw->mac_type >= e1000_82544 &&
Joe Perches1dc32912008-07-11 15:17:08 -0700702 hw->autoneg == 1 &&
703 hw->autoneg_advertised == ADVERTISE_1000_FULL) {
704 u32 ctrl = er32(CTRL);
Jesse Brandeburg3d5460a2006-12-15 10:33:46 +0100705 /* clear phy power management bit if we are in gig only mode,
706 * which if enabled will attempt negotiation to 100Mb, which
Jeff Kirsher6cfbd972013-02-09 12:49:21 +0000707 * can cause a loss of link at power off or driver unload
708 */
Jesse Brandeburg3d5460a2006-12-15 10:33:46 +0100709 ctrl &= ~E1000_CTRL_SWDPIN3;
Joe Perches1dc32912008-07-11 15:17:08 -0700710 ew32(CTRL, ctrl);
Jesse Brandeburg3d5460a2006-12-15 10:33:46 +0100711 }
712
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 /* Enable h/w to recognize an 802.1Q VLAN Ethernet packet */
Joe Perches1dc32912008-07-11 15:17:08 -0700714 ew32(VET, ETHERNET_IEEE_VLAN_TYPE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
Joe Perches1dc32912008-07-11 15:17:08 -0700716 e1000_reset_adaptive(hw);
717 e1000_phy_get_info(hw, &adapter->phy_info);
Auke Kok9a53a202006-06-27 09:06:45 -0700718
Jeff Garzik0fccd0e2006-12-15 10:56:10 -0500719 e1000_release_manageability(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720}
721
Ben Hutchings1aa8b472012-07-10 10:56:59 +0000722/* Dump the eeprom for users having checksum issues */
Adrian Bunkb4ea8952008-02-01 08:21:28 -0800723static void e1000_dump_eeprom(struct e1000_adapter *adapter)
Auke Kok67b3c272007-12-17 13:50:23 -0800724{
725 struct net_device *netdev = adapter->netdev;
726 struct ethtool_eeprom eeprom;
727 const struct ethtool_ops *ops = netdev->ethtool_ops;
728 u8 *data;
729 int i;
730 u16 csum_old, csum_new = 0;
731
732 eeprom.len = ops->get_eeprom_len(netdev);
733 eeprom.offset = 0;
734
735 data = kmalloc(eeprom.len, GFP_KERNEL);
Joe Perchese404dec2012-01-29 12:56:23 +0000736 if (!data)
Auke Kok67b3c272007-12-17 13:50:23 -0800737 return;
Auke Kok67b3c272007-12-17 13:50:23 -0800738
739 ops->get_eeprom(netdev, &eeprom, data);
740
741 csum_old = (data[EEPROM_CHECKSUM_REG * 2]) +
742 (data[EEPROM_CHECKSUM_REG * 2 + 1] << 8);
743 for (i = 0; i < EEPROM_CHECKSUM_REG * 2; i += 2)
744 csum_new += data[i] + (data[i + 1] << 8);
745 csum_new = EEPROM_SUM - csum_new;
746
Emil Tantilov675ad472010-04-27 14:02:58 +0000747 pr_err("/*********************/\n");
748 pr_err("Current EEPROM Checksum : 0x%04x\n", csum_old);
749 pr_err("Calculated : 0x%04x\n", csum_new);
Auke Kok67b3c272007-12-17 13:50:23 -0800750
Emil Tantilov675ad472010-04-27 14:02:58 +0000751 pr_err("Offset Values\n");
752 pr_err("======== ======\n");
Auke Kok67b3c272007-12-17 13:50:23 -0800753 print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 16, 1, data, 128, 0);
754
Emil Tantilov675ad472010-04-27 14:02:58 +0000755 pr_err("Include this output when contacting your support provider.\n");
756 pr_err("This is not a software error! Something bad happened to\n");
757 pr_err("your hardware or EEPROM image. Ignoring this problem could\n");
758 pr_err("result in further problems, possibly loss of data,\n");
759 pr_err("corruption or system hangs!\n");
760 pr_err("The MAC Address will be reset to 00:00:00:00:00:00,\n");
761 pr_err("which is invalid and requires you to set the proper MAC\n");
762 pr_err("address manually before continuing to enable this network\n");
763 pr_err("device. Please inspect the EEPROM dump and report the\n");
764 pr_err("issue to your hardware vendor or Intel Customer Support.\n");
765 pr_err("/*********************/\n");
Auke Kok67b3c272007-12-17 13:50:23 -0800766
767 kfree(data);
768}
769
770/**
Taku Izumi81250292008-07-11 15:17:44 -0700771 * e1000_is_need_ioport - determine if an adapter needs ioport resources or not
772 * @pdev: PCI device information struct
773 *
774 * Return true if an adapter needs ioport resources
775 **/
776static int e1000_is_need_ioport(struct pci_dev *pdev)
777{
778 switch (pdev->device) {
779 case E1000_DEV_ID_82540EM:
780 case E1000_DEV_ID_82540EM_LOM:
781 case E1000_DEV_ID_82540EP:
782 case E1000_DEV_ID_82540EP_LOM:
783 case E1000_DEV_ID_82540EP_LP:
784 case E1000_DEV_ID_82541EI:
785 case E1000_DEV_ID_82541EI_MOBILE:
786 case E1000_DEV_ID_82541ER:
787 case E1000_DEV_ID_82541ER_LOM:
788 case E1000_DEV_ID_82541GI:
789 case E1000_DEV_ID_82541GI_LF:
790 case E1000_DEV_ID_82541GI_MOBILE:
791 case E1000_DEV_ID_82544EI_COPPER:
792 case E1000_DEV_ID_82544EI_FIBER:
793 case E1000_DEV_ID_82544GC_COPPER:
794 case E1000_DEV_ID_82544GC_LOM:
795 case E1000_DEV_ID_82545EM_COPPER:
796 case E1000_DEV_ID_82545EM_FIBER:
797 case E1000_DEV_ID_82546EB_COPPER:
798 case E1000_DEV_ID_82546EB_FIBER:
799 case E1000_DEV_ID_82546EB_QUAD_COPPER:
800 return true;
801 default:
802 return false;
803 }
804}
805
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000806static netdev_features_t e1000_fix_features(struct net_device *netdev,
807 netdev_features_t features)
Jiri Pirko5622e402011-07-21 03:26:31 +0000808{
Jeff Kirsher6cfbd972013-02-09 12:49:21 +0000809 /* Since there is no support for separate Rx/Tx vlan accel
810 * enable/disable make sure Tx flag is always in same state as Rx.
Jiri Pirko5622e402011-07-21 03:26:31 +0000811 */
Patrick McHardyf6469682013-04-19 02:04:27 +0000812 if (features & NETIF_F_HW_VLAN_CTAG_RX)
813 features |= NETIF_F_HW_VLAN_CTAG_TX;
Jiri Pirko5622e402011-07-21 03:26:31 +0000814 else
Patrick McHardyf6469682013-04-19 02:04:27 +0000815 features &= ~NETIF_F_HW_VLAN_CTAG_TX;
Jiri Pirko5622e402011-07-21 03:26:31 +0000816
817 return features;
818}
819
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000820static int e1000_set_features(struct net_device *netdev,
821 netdev_features_t features)
Michał Mirosławe97d3202011-06-08 08:36:42 +0000822{
823 struct e1000_adapter *adapter = netdev_priv(netdev);
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000824 netdev_features_t changed = features ^ netdev->features;
Michał Mirosławe97d3202011-06-08 08:36:42 +0000825
Patrick McHardyf6469682013-04-19 02:04:27 +0000826 if (changed & NETIF_F_HW_VLAN_CTAG_RX)
Jiri Pirko5622e402011-07-21 03:26:31 +0000827 e1000_vlan_mode(netdev, features);
828
Ben Greeare825b732012-04-04 06:01:29 +0000829 if (!(changed & (NETIF_F_RXCSUM | NETIF_F_RXALL)))
Michał Mirosławe97d3202011-06-08 08:36:42 +0000830 return 0;
831
Ben Greeare825b732012-04-04 06:01:29 +0000832 netdev->features = features;
Michał Mirosławe97d3202011-06-08 08:36:42 +0000833 adapter->rx_csum = !!(features & NETIF_F_RXCSUM);
834
835 if (netif_running(netdev))
836 e1000_reinit_locked(adapter);
837 else
838 e1000_reset(adapter);
839
840 return 0;
841}
842
Stephen Hemminger0e7614b2008-11-19 22:18:22 -0800843static const struct net_device_ops e1000_netdev_ops = {
844 .ndo_open = e1000_open,
845 .ndo_stop = e1000_close,
Stephen Hemminger00829822008-11-20 20:14:53 -0800846 .ndo_start_xmit = e1000_xmit_frame,
Stephen Hemminger0e7614b2008-11-19 22:18:22 -0800847 .ndo_get_stats = e1000_get_stats,
848 .ndo_set_rx_mode = e1000_set_rx_mode,
849 .ndo_set_mac_address = e1000_set_mac,
Jiri Pirko5622e402011-07-21 03:26:31 +0000850 .ndo_tx_timeout = e1000_tx_timeout,
Stephen Hemminger0e7614b2008-11-19 22:18:22 -0800851 .ndo_change_mtu = e1000_change_mtu,
852 .ndo_do_ioctl = e1000_ioctl,
853 .ndo_validate_addr = eth_validate_addr,
Stephen Hemminger0e7614b2008-11-19 22:18:22 -0800854 .ndo_vlan_rx_add_vid = e1000_vlan_rx_add_vid,
855 .ndo_vlan_rx_kill_vid = e1000_vlan_rx_kill_vid,
856#ifdef CONFIG_NET_POLL_CONTROLLER
857 .ndo_poll_controller = e1000_netpoll,
858#endif
Jiri Pirko5622e402011-07-21 03:26:31 +0000859 .ndo_fix_features = e1000_fix_features,
860 .ndo_set_features = e1000_set_features,
Stephen Hemminger0e7614b2008-11-19 22:18:22 -0800861};
862
Taku Izumi81250292008-07-11 15:17:44 -0700863/**
Jesse Brandeburge508be12010-09-07 21:01:12 +0000864 * e1000_init_hw_struct - initialize members of hw struct
865 * @adapter: board private struct
866 * @hw: structure used by e1000_hw.c
867 *
868 * Factors out initialization of the e1000_hw struct to its own function
869 * that can be called very early at init (just after struct allocation).
870 * Fields are initialized based on PCI device information and
871 * OS network device settings (MTU size).
872 * Returns negative error codes if MAC type setup fails.
873 */
874static int e1000_init_hw_struct(struct e1000_adapter *adapter,
875 struct e1000_hw *hw)
876{
877 struct pci_dev *pdev = adapter->pdev;
878
879 /* PCI config space info */
880 hw->vendor_id = pdev->vendor;
881 hw->device_id = pdev->device;
882 hw->subsystem_vendor_id = pdev->subsystem_vendor;
883 hw->subsystem_id = pdev->subsystem_device;
884 hw->revision_id = pdev->revision;
885
886 pci_read_config_word(pdev, PCI_COMMAND, &hw->pci_cmd_word);
887
888 hw->max_frame_size = adapter->netdev->mtu +
889 ENET_HEADER_SIZE + ETHERNET_FCS_SIZE;
890 hw->min_frame_size = MINIMUM_ETHERNET_FRAME_SIZE;
891
892 /* identify the MAC */
893 if (e1000_set_mac_type(hw)) {
894 e_err(probe, "Unknown MAC Type\n");
895 return -EIO;
896 }
897
898 switch (hw->mac_type) {
899 default:
900 break;
901 case e1000_82541:
902 case e1000_82547:
903 case e1000_82541_rev_2:
904 case e1000_82547_rev_2:
905 hw->phy_init_script = 1;
906 break;
907 }
908
909 e1000_set_media_type(hw);
910 e1000_get_bus_info(hw);
911
912 hw->wait_autoneg_complete = false;
913 hw->tbi_compatibility_en = true;
914 hw->adaptive_ifs = true;
915
916 /* Copper options */
917
918 if (hw->media_type == e1000_media_type_copper) {
919 hw->mdix = AUTO_ALL_MODES;
920 hw->disable_polarity_correction = false;
921 hw->master_slave = E1000_MASTER_SLAVE;
922 }
923
924 return 0;
925}
926
927/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 * e1000_probe - Device Initialization Routine
929 * @pdev: PCI device information struct
930 * @ent: entry in e1000_pci_tbl
931 *
932 * Returns 0 on success, negative on failure
933 *
934 * e1000_probe initializes an adapter identified by a pci_dev structure.
935 * The OS initialization, configuring of the adapter private structure,
936 * and a hardware reset occur.
937 **/
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +0000938static int e1000_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939{
940 struct net_device *netdev;
941 struct e1000_adapter *adapter;
Joe Perches1dc32912008-07-11 15:17:08 -0700942 struct e1000_hw *hw;
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700943
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 static int cards_found = 0;
Jesse Brandeburg120cd572006-08-31 14:27:46 -0700945 static int global_quad_port_a = 0; /* global ksp3 port a indication */
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700946 int i, err, pci_using_dac;
Joe Perches406874a2008-04-03 10:06:32 -0700947 u16 eeprom_data = 0;
Dirk Brandewie5377a412011-01-06 14:29:54 +0000948 u16 tmp = 0;
Joe Perches406874a2008-04-03 10:06:32 -0700949 u16 eeprom_apme_mask = E1000_EEPROM_APME;
Taku Izumi81250292008-07-11 15:17:44 -0700950 int bars, need_ioport;
Joe Perches0795af52007-10-03 17:59:30 -0700951
Taku Izumi81250292008-07-11 15:17:44 -0700952 /* do not allocate ioport bars when not needed */
953 need_ioport = e1000_is_need_ioport(pdev);
954 if (need_ioport) {
955 bars = pci_select_bars(pdev, IORESOURCE_MEM | IORESOURCE_IO);
956 err = pci_enable_device(pdev);
957 } else {
958 bars = pci_select_bars(pdev, IORESOURCE_MEM);
Karsten Keil4d7155b2009-02-03 15:18:01 -0800959 err = pci_enable_device_mem(pdev);
Taku Izumi81250292008-07-11 15:17:44 -0700960 }
Joe Perchesc7be73b2008-07-11 15:17:28 -0700961 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 return err;
963
Taku Izumi81250292008-07-11 15:17:44 -0700964 err = pci_request_selected_regions(pdev, bars, e1000_driver_name);
Joe Perchesc7be73b2008-07-11 15:17:28 -0700965 if (err)
Vasily Averin6dd62ab2006-08-28 14:56:22 -0700966 goto err_pci_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
968 pci_set_master(pdev);
Nick Nunleydbb5aae2010-02-03 14:49:48 +0000969 err = pci_save_state(pdev);
970 if (err)
971 goto err_alloc_etherdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
Vasily Averin6dd62ab2006-08-28 14:56:22 -0700973 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 netdev = alloc_etherdev(sizeof(struct e1000_adapter));
Vasily Averin6dd62ab2006-08-28 14:56:22 -0700975 if (!netdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 goto err_alloc_etherdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 SET_NETDEV_DEV(netdev, &pdev->dev);
979
980 pci_set_drvdata(pdev, netdev);
Malli Chilakala60490fe2005-06-17 17:41:45 -0700981 adapter = netdev_priv(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 adapter->netdev = netdev;
983 adapter->pdev = pdev;
stephen hemmingerb3f4d592012-03-13 06:04:20 +0000984 adapter->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
Taku Izumi81250292008-07-11 15:17:44 -0700985 adapter->bars = bars;
986 adapter->need_ioport = need_ioport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
Joe Perches1dc32912008-07-11 15:17:08 -0700988 hw = &adapter->hw;
989 hw->back = adapter;
990
Vasily Averin6dd62ab2006-08-28 14:56:22 -0700991 err = -EIO;
Arjan van de Ven275f1652008-10-20 21:42:39 -0700992 hw->hw_addr = pci_ioremap_bar(pdev, BAR_0);
Joe Perches1dc32912008-07-11 15:17:08 -0700993 if (!hw->hw_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 goto err_ioremap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
Taku Izumi81250292008-07-11 15:17:44 -0700996 if (adapter->need_ioport) {
997 for (i = BAR_1; i <= BAR_5; i++) {
998 if (pci_resource_len(pdev, i) == 0)
999 continue;
1000 if (pci_resource_flags(pdev, i) & IORESOURCE_IO) {
1001 hw->io_base = pci_resource_start(pdev, i);
1002 break;
1003 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 }
1005 }
1006
Jesse Brandeburge508be12010-09-07 21:01:12 +00001007 /* make ready for any if (hw->...) below */
1008 err = e1000_init_hw_struct(adapter, hw);
1009 if (err)
1010 goto err_sw_init;
1011
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00001012 /* there is a workaround being applied below that limits
Jesse Brandeburge508be12010-09-07 21:01:12 +00001013 * 64-bit DMA addresses to 64-bit hardware. There are some
1014 * 32-bit adapters that Tx hang when given 64-bit DMA addresses
1015 */
1016 pci_using_dac = 0;
1017 if ((hw->bus_type == e1000_bus_type_pcix) &&
1018 !dma_set_mask(&pdev->dev, DMA_BIT_MASK(64))) {
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00001019 /* according to DMA-API-HOWTO, coherent calls will always
Jesse Brandeburge508be12010-09-07 21:01:12 +00001020 * 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 |
Patrick McHardyf6469682013-04-19 02:04:27 +00001061 NETIF_F_HW_VLAN_CTAG_RX;
1062 netdev->features = NETIF_F_HW_VLAN_CTAG_TX |
1063 NETIF_F_HW_VLAN_CTAG_FILTER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 }
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
Ben Greear11a78dc2012-02-11 15:40:01 +00001070 netdev->priv_flags |= IFF_SUPP_NOFCS;
1071
Michał Mirosławe97d3202011-06-08 08:36:42 +00001072 netdev->features |= netdev->hw_features;
Tushar Dave75006732012-06-12 13:03:29 +00001073 netdev->hw_features |= (NETIF_F_RXCSUM |
1074 NETIF_F_RXALL |
1075 NETIF_F_RXFCS);
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001076
Yi Zou7b872a52010-09-22 17:57:58 +00001077 if (pci_using_dac) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 netdev->features |= NETIF_F_HIGHDMA;
Yi Zou7b872a52010-09-22 17:57:58 +00001079 netdev->vlan_features |= NETIF_F_HIGHDMA;
1080 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
Tushar Dave75006732012-06-12 13:03:29 +00001082 netdev->vlan_features |= (NETIF_F_TSO |
1083 NETIF_F_HW_CSUM |
1084 NETIF_F_SG);
Patrick McHardy20501a62008-10-11 12:25:59 -07001085
Jiri Pirko01789342011-08-16 06:29:00 +00001086 netdev->priv_flags |= IFF_UNICAST_FLT;
1087
Joe Perches1dc32912008-07-11 15:17:08 -07001088 adapter->en_mng_pt = e1000_enable_mng_pass_thru(hw);
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001089
Auke Kokcd94dd02006-06-27 09:08:22 -07001090 /* initialize eeprom parameters */
Joe Perches1dc32912008-07-11 15:17:08 -07001091 if (e1000_init_eeprom_params(hw)) {
Emil Tantilovfeb8f472010-07-26 23:37:21 -07001092 e_err(probe, "EEPROM initialization failed\n");
Vasily Averin6dd62ab2006-08-28 14:56:22 -07001093 goto err_eeprom;
Auke Kokcd94dd02006-06-27 09:08:22 -07001094 }
1095
Jesse Brandeburg96838a42006-01-18 13:01:39 -08001096 /* before reading the EEPROM, reset the controller to
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00001097 * put the device in a known good starting state
1098 */
Jesse Brandeburg96838a42006-01-18 13:01:39 -08001099
Joe Perches1dc32912008-07-11 15:17:08 -07001100 e1000_reset_hw(hw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
1102 /* make sure the EEPROM is good */
Joe Perches1dc32912008-07-11 15:17:08 -07001103 if (e1000_validate_eeprom_checksum(hw) < 0) {
Emil Tantilovfeb8f472010-07-26 23:37:21 -07001104 e_err(probe, "The EEPROM Checksum Is Not Valid\n");
Auke Kok67b3c272007-12-17 13:50:23 -08001105 e1000_dump_eeprom(adapter);
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00001106 /* set MAC address to all zeroes to invalidate and temporary
Auke Kok67b3c272007-12-17 13:50:23 -08001107 * disable this device for the user. This blocks regular
1108 * traffic while still permitting ethtool ioctls from reaching
1109 * the hardware as well as allowing the user to run the
1110 * interface after manually setting a hw addr using
1111 * `ip set address`
1112 */
Joe Perches1dc32912008-07-11 15:17:08 -07001113 memset(hw->mac_addr, 0, netdev->addr_len);
Auke Kok67b3c272007-12-17 13:50:23 -08001114 } else {
1115 /* copy the MAC address out of the EEPROM */
Joe Perches1dc32912008-07-11 15:17:08 -07001116 if (e1000_read_mac_addr(hw))
Emil Tantilovfeb8f472010-07-26 23:37:21 -07001117 e_err(probe, "EEPROM Read Error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 }
Auke Kok67b3c272007-12-17 13:50:23 -08001119 /* don't block initalization here due to bad MAC address */
Joe Perches1dc32912008-07-11 15:17:08 -07001120 memcpy(netdev->dev_addr, hw->mac_addr, netdev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
Jiri Pirkoaaeb6cd2013-01-08 01:38:26 +00001122 if (!is_valid_ether_addr(netdev->dev_addr))
Emil Tantilovfeb8f472010-07-26 23:37:21 -07001123 e_err(probe, "Invalid MAC Address\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
Jesse Brandeburga4010af2011-10-05 07:24:41 +00001126 INIT_DELAYED_WORK(&adapter->watchdog_task, e1000_watchdog);
1127 INIT_DELAYED_WORK(&adapter->fifo_stall_task,
1128 e1000_82547_tx_fifo_stall_task);
1129 INIT_DELAYED_WORK(&adapter->phy_info_task, e1000_update_phy_info_task);
David Howells65f27f32006-11-22 14:55:48 +00001130 INIT_WORK(&adapter->reset_task, e1000_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 e1000_check_options(adapter);
1133
1134 /* Initial Wake on LAN setting
1135 * If APM wake is enabled in the EEPROM,
1136 * enable the ACPI Magic Packet filter
1137 */
1138
Joe Perches1dc32912008-07-11 15:17:08 -07001139 switch (hw->mac_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 case e1000_82542_rev2_0:
1141 case e1000_82542_rev2_1:
1142 case e1000_82543:
1143 break;
1144 case e1000_82544:
Joe Perches1dc32912008-07-11 15:17:08 -07001145 e1000_read_eeprom(hw,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 EEPROM_INIT_CONTROL2_REG, 1, &eeprom_data);
1147 eeprom_apme_mask = E1000_EEPROM_82544_APM;
1148 break;
1149 case e1000_82546:
1150 case e1000_82546_rev_3:
Joe Perches1dc32912008-07-11 15:17:08 -07001151 if (er32(STATUS) & E1000_STATUS_FUNC_1){
1152 e1000_read_eeprom(hw,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 EEPROM_INIT_CONTROL3_PORT_B, 1, &eeprom_data);
1154 break;
1155 }
1156 /* Fall Through */
1157 default:
Joe Perches1dc32912008-07-11 15:17:08 -07001158 e1000_read_eeprom(hw,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 EEPROM_INIT_CONTROL3_PORT_A, 1, &eeprom_data);
1160 break;
1161 }
Jesse Brandeburg96838a42006-01-18 13:01:39 -08001162 if (eeprom_data & eeprom_apme_mask)
Jesse Brandeburg120cd572006-08-31 14:27:46 -07001163 adapter->eeprom_wol |= E1000_WUFC_MAG;
1164
1165 /* now that we have the eeprom settings, apply the special cases
1166 * where the eeprom may be wrong or the board simply won't support
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00001167 * wake on lan on a particular port
1168 */
Jesse Brandeburg120cd572006-08-31 14:27:46 -07001169 switch (pdev->device) {
1170 case E1000_DEV_ID_82546GB_PCIE:
1171 adapter->eeprom_wol = 0;
1172 break;
1173 case E1000_DEV_ID_82546EB_FIBER:
1174 case E1000_DEV_ID_82546GB_FIBER:
Jesse Brandeburg120cd572006-08-31 14:27:46 -07001175 /* Wake events only supported on port A for dual fiber
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00001176 * regardless of eeprom setting
1177 */
Joe Perches1dc32912008-07-11 15:17:08 -07001178 if (er32(STATUS) & E1000_STATUS_FUNC_1)
Jesse Brandeburg120cd572006-08-31 14:27:46 -07001179 adapter->eeprom_wol = 0;
1180 break;
1181 case E1000_DEV_ID_82546GB_QUAD_COPPER_KSP3:
1182 /* if quad port adapter, disable WoL on all but port A */
1183 if (global_quad_port_a != 0)
1184 adapter->eeprom_wol = 0;
1185 else
Rusty Russell3db1cd52011-12-19 13:56:45 +00001186 adapter->quad_port_a = true;
Jesse Brandeburg120cd572006-08-31 14:27:46 -07001187 /* Reset for multiple quad port adapters */
1188 if (++global_quad_port_a == 4)
1189 global_quad_port_a = 0;
1190 break;
1191 }
1192
1193 /* initialize the wol settings based on the eeprom settings */
1194 adapter->wol = adapter->eeprom_wol;
\"Rafael J. Wysocki\de126482008-11-07 20:30:19 +00001195 device_set_wakeup_enable(&adapter->pdev->dev, adapter->wol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
Dirk Brandewie5377a412011-01-06 14:29:54 +00001197 /* Auto detect PHY address */
1198 if (hw->mac_type == e1000_ce4100) {
1199 for (i = 0; i < 32; i++) {
1200 hw->phy_addr = i;
1201 e1000_read_phy_reg(hw, PHY_ID2, &tmp);
1202 if (tmp == 0 || tmp == 0xFF) {
1203 if (i == 31)
1204 goto err_eeprom;
1205 continue;
1206 } else
1207 break;
1208 }
1209 }
1210
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 /* reset the hardware with the new settings */
1212 e1000_reset(adapter);
1213
Auke Kok416b5d12007-06-01 10:22:39 -07001214 strcpy(netdev->name, "eth%d");
Joe Perchesc7be73b2008-07-11 15:17:28 -07001215 err = register_netdev(netdev);
1216 if (err)
Auke Kok416b5d12007-06-01 10:22:39 -07001217 goto err_register;
Auke Kok1314bbf2006-09-27 12:54:02 -07001218
Jiri Pirko52f55092012-03-20 18:10:01 +00001219 e1000_vlan_filter_on_off(adapter, false);
Jiri Pirko5622e402011-07-21 03:26:31 +00001220
Emil Tantilov675ad472010-04-27 14:02:58 +00001221 /* print bus type/speed/width info */
Emil Tantilovfeb8f472010-07-26 23:37:21 -07001222 e_info(probe, "(PCI%s:%dMHz:%d-bit) %pM\n",
Joe Perches7837e582010-06-11 12:51:49 +00001223 ((hw->bus_type == e1000_bus_type_pcix) ? "-X" : ""),
1224 ((hw->bus_speed == e1000_bus_speed_133) ? 133 :
1225 (hw->bus_speed == e1000_bus_speed_120) ? 120 :
1226 (hw->bus_speed == e1000_bus_speed_100) ? 100 :
1227 (hw->bus_speed == e1000_bus_speed_66) ? 66 : 33),
1228 ((hw->bus_width == e1000_bus_width_64) ? 64 : 32),
1229 netdev->dev_addr);
Emil Tantilov675ad472010-04-27 14:02:58 +00001230
Jesse Brandeburgeb62efd2009-04-17 20:44:36 +00001231 /* carrier off reporting is important to ethtool even BEFORE open */
1232 netif_carrier_off(netdev);
1233
Emil Tantilovfeb8f472010-07-26 23:37:21 -07001234 e_info(probe, "Intel(R) PRO/1000 Network Connection\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
1236 cards_found++;
1237 return 0;
1238
1239err_register:
Vasily Averin6dd62ab2006-08-28 14:56:22 -07001240err_eeprom:
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00001241 e1000_phy_hw_reset(hw);
Vasily Averin6dd62ab2006-08-28 14:56:22 -07001242
Joe Perches1dc32912008-07-11 15:17:08 -07001243 if (hw->flash_address)
1244 iounmap(hw->flash_address);
Vasily Averin6dd62ab2006-08-28 14:56:22 -07001245 kfree(adapter->tx_ring);
1246 kfree(adapter->rx_ring);
Jesse Brandeburge508be12010-09-07 21:01:12 +00001247err_dma:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248err_sw_init:
Dirk Brandewie5377a412011-01-06 14:29:54 +00001249err_mdio_ioremap:
Florian Fainelli13acde82012-01-04 20:23:35 +00001250 iounmap(hw->ce4100_gbe_mdio_base_virt);
Joe Perches1dc32912008-07-11 15:17:08 -07001251 iounmap(hw->hw_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252err_ioremap:
1253 free_netdev(netdev);
1254err_alloc_etherdev:
Taku Izumi81250292008-07-11 15:17:44 -07001255 pci_release_selected_regions(pdev, bars);
Vasily Averin6dd62ab2006-08-28 14:56:22 -07001256err_pci_reg:
Vasily Averin6dd62ab2006-08-28 14:56:22 -07001257 pci_disable_device(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 return err;
1259}
1260
1261/**
1262 * e1000_remove - Device Removal Routine
1263 * @pdev: PCI device information struct
1264 *
1265 * e1000_remove is called by the PCI subsystem to alert the driver
1266 * that it should release a PCI device. The could be caused by a
1267 * Hot-Plug event, or because the driver is going to be removed from
1268 * memory.
1269 **/
Bill Pemberton9f9a12f2012-12-03 09:24:25 -05001270static void e1000_remove(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271{
1272 struct net_device *netdev = pci_get_drvdata(pdev);
Malli Chilakala60490fe2005-06-17 17:41:45 -07001273 struct e1000_adapter *adapter = netdev_priv(netdev);
Joe Perches1dc32912008-07-11 15:17:08 -07001274 struct e1000_hw *hw = &adapter->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
Jesse Brandeburga4010af2011-10-05 07:24:41 +00001276 e1000_down_and_stop(adapter);
Jeff Garzik0fccd0e2006-12-15 10:56:10 -05001277 e1000_release_manageability(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001279 unregister_netdev(netdev);
1280
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00001281 e1000_phy_hw_reset(hw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
Mallikarjuna R Chilakala24025e4ec2005-10-04 07:03:23 -04001283 kfree(adapter->tx_ring);
1284 kfree(adapter->rx_ring);
Mallikarjuna R Chilakala24025e4ec2005-10-04 07:03:23 -04001285
Florian Fainelli1c267502012-01-04 20:23:34 +00001286 if (hw->mac_type == e1000_ce4100)
Florian Fainelli13acde82012-01-04 20:23:35 +00001287 iounmap(hw->ce4100_gbe_mdio_base_virt);
Joe Perches1dc32912008-07-11 15:17:08 -07001288 iounmap(hw->hw_addr);
1289 if (hw->flash_address)
1290 iounmap(hw->flash_address);
Taku Izumi81250292008-07-11 15:17:44 -07001291 pci_release_selected_regions(pdev, adapter->bars);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
1293 free_netdev(netdev);
1294
1295 pci_disable_device(pdev);
1296}
1297
1298/**
1299 * e1000_sw_init - Initialize general software structures (struct e1000_adapter)
1300 * @adapter: board private structure to initialize
1301 *
1302 * e1000_sw_init initializes the Adapter private data structure.
Jesse Brandeburge508be12010-09-07 21:01:12 +00001303 * e1000_init_hw_struct MUST be called before this function
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 **/
Bill Pemberton9f9a12f2012-12-03 09:24:25 -05001305static int e1000_sw_init(struct e1000_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306{
Auke Kokeb0f8052006-07-14 16:14:48 -07001307 adapter->rx_buffer_len = MAXIMUM_ETHERNET_VLAN_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
Jeff Kirsherf56799e2006-01-12 16:50:39 -08001309 adapter->num_tx_queues = 1;
1310 adapter->num_rx_queues = 1;
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001311
1312 if (e1000_alloc_queues(adapter)) {
Emil Tantilovfeb8f472010-07-26 23:37:21 -07001313 e_err(probe, "Unable to allocate memory for queues\n");
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001314 return -ENOMEM;
1315 }
1316
Herbert Xu47313052007-05-29 15:07:31 -07001317 /* Explicitly disable IRQ since the NIC can be in any state. */
Herbert Xu47313052007-05-29 15:07:31 -07001318 e1000_irq_disable(adapter);
1319
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 spin_lock_init(&adapter->stats_lock);
Jesse Brandeburg0ef4eed2011-10-05 07:24:51 +00001321 mutex_init(&adapter->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
Auke Kok1314bbf2006-09-27 12:54:02 -07001323 set_bit(__E1000_DOWN, &adapter->flags);
1324
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 return 0;
1326}
1327
1328/**
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001329 * e1000_alloc_queues - Allocate memory for all rings
1330 * @adapter: board private structure to initialize
1331 *
1332 * We allocate one ring per queue at run-time since we don't know the
Wang Chen3e1d7cd2008-12-03 22:07:10 -08001333 * number of queues at compile-time.
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001334 **/
Bill Pemberton9f9a12f2012-12-03 09:24:25 -05001335static int e1000_alloc_queues(struct e1000_adapter *adapter)
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001336{
Yan Burman1c7e5b12007-03-06 08:58:04 -08001337 adapter->tx_ring = kcalloc(adapter->num_tx_queues,
1338 sizeof(struct e1000_tx_ring), GFP_KERNEL);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001339 if (!adapter->tx_ring)
1340 return -ENOMEM;
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001341
Yan Burman1c7e5b12007-03-06 08:58:04 -08001342 adapter->rx_ring = kcalloc(adapter->num_rx_queues,
1343 sizeof(struct e1000_rx_ring), GFP_KERNEL);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001344 if (!adapter->rx_ring) {
1345 kfree(adapter->tx_ring);
1346 return -ENOMEM;
1347 }
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001348
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001349 return E1000_SUCCESS;
1350}
1351
1352/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 * e1000_open - Called when a network interface is made active
1354 * @netdev: network interface device structure
1355 *
1356 * Returns 0 on success, negative value on failure
1357 *
1358 * The open entry point is called when a network interface is made
1359 * active by the system (IFF_UP). At this point all resources needed
1360 * for transmit and receive operations are allocated, the interrupt
Jesse Brandeburga4010af2011-10-05 07:24:41 +00001361 * handler is registered with the OS, the watchdog task is started,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 * and the stack is notified that the interface is ready.
1363 **/
Joe Perches64798842008-07-11 15:17:02 -07001364static int e1000_open(struct net_device *netdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365{
Malli Chilakala60490fe2005-06-17 17:41:45 -07001366 struct e1000_adapter *adapter = netdev_priv(netdev);
Joe Perches1dc32912008-07-11 15:17:08 -07001367 struct e1000_hw *hw = &adapter->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 int err;
1369
Auke Kok2db10a02006-06-27 09:06:28 -07001370 /* disallow open during test */
Auke Kok1314bbf2006-09-27 12:54:02 -07001371 if (test_bit(__E1000_TESTING, &adapter->flags))
Auke Kok2db10a02006-06-27 09:06:28 -07001372 return -EBUSY;
1373
Jesse Brandeburgeb62efd2009-04-17 20:44:36 +00001374 netif_carrier_off(netdev);
1375
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 /* allocate transmit descriptors */
Auke Koke0aac5a2007-03-06 08:57:21 -08001377 err = e1000_setup_all_tx_resources(adapter);
1378 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 goto err_setup_tx;
1380
1381 /* allocate receive descriptors */
Auke Koke0aac5a2007-03-06 08:57:21 -08001382 err = e1000_setup_all_rx_resources(adapter);
Linus Torvaldsb5bf28c2007-02-21 11:21:44 -08001383 if (err)
Auke Koke0aac5a2007-03-06 08:57:21 -08001384 goto err_setup_rx;
Linus Torvaldsb5bf28c2007-02-21 11:21:44 -08001385
Auke Kok79f05bf2006-06-27 09:06:32 -07001386 e1000_power_up_phy(adapter);
1387
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001388 adapter->mng_vlan_id = E1000_MNG_VLAN_NONE;
Joe Perches1dc32912008-07-11 15:17:08 -07001389 if ((hw->mng_cookie.status &
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001390 E1000_MNG_DHCP_COOKIE_STATUS_VLAN_SUPPORT)) {
1391 e1000_update_mng_vlan(adapter);
1392 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
Auke Koke0aac5a2007-03-06 08:57:21 -08001394 /* before we allocate an interrupt, we must be ready to handle it.
1395 * Setting DEBUG_SHIRQ in the kernel makes it fire an interrupt
1396 * as soon as we call pci_request_irq, so we have to setup our
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00001397 * clean_rx handler before we do so.
1398 */
Auke Koke0aac5a2007-03-06 08:57:21 -08001399 e1000_configure(adapter);
1400
1401 err = e1000_request_irq(adapter);
1402 if (err)
1403 goto err_req_irq;
1404
1405 /* From here on the code is the same as e1000_up() */
1406 clear_bit(__E1000_DOWN, &adapter->flags);
1407
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001408 napi_enable(&adapter->napi);
Herbert Xu47313052007-05-29 15:07:31 -07001409
Auke Koke0aac5a2007-03-06 08:57:21 -08001410 e1000_irq_enable(adapter);
1411
Ben Hutchings076152d2008-07-18 17:50:57 -07001412 netif_start_queue(netdev);
1413
Auke Koke0aac5a2007-03-06 08:57:21 -08001414 /* fire a link status change interrupt to start the watchdog */
Joe Perches1dc32912008-07-11 15:17:08 -07001415 ew32(ICS, E1000_ICS_LSC);
Auke Koke0aac5a2007-03-06 08:57:21 -08001416
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 return E1000_SUCCESS;
1418
Linus Torvaldsb5bf28c2007-02-21 11:21:44 -08001419err_req_irq:
Auke Koke0aac5a2007-03-06 08:57:21 -08001420 e1000_power_down_phy(adapter);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001421 e1000_free_all_rx_resources(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422err_setup_rx:
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001423 e1000_free_all_tx_resources(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424err_setup_tx:
1425 e1000_reset(adapter);
1426
1427 return err;
1428}
1429
1430/**
1431 * e1000_close - Disables a network interface
1432 * @netdev: network interface device structure
1433 *
1434 * Returns 0, this is not allowed to fail
1435 *
1436 * The close entry point is called when an interface is de-activated
1437 * by the OS. The hardware is still under the drivers control, but
1438 * needs to be disabled. A global MAC reset is issued to stop the
1439 * hardware, and all transmit and receive resources are freed.
1440 **/
Joe Perches64798842008-07-11 15:17:02 -07001441static int e1000_close(struct net_device *netdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442{
Malli Chilakala60490fe2005-06-17 17:41:45 -07001443 struct e1000_adapter *adapter = netdev_priv(netdev);
Joe Perches1dc32912008-07-11 15:17:08 -07001444 struct e1000_hw *hw = &adapter->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
Auke Kok2db10a02006-06-27 09:06:28 -07001446 WARN_ON(test_bit(__E1000_RESETTING, &adapter->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 e1000_down(adapter);
Auke Kok79f05bf2006-06-27 09:06:32 -07001448 e1000_power_down_phy(adapter);
Auke Kok2db10a02006-06-27 09:06:28 -07001449 e1000_free_irq(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001451 e1000_free_all_tx_resources(adapter);
1452 e1000_free_all_rx_resources(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
Bruce Allan46665602006-09-27 12:54:08 -07001454 /* kill manageability vlan ID if supported, but not if a vlan with
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00001455 * the same ID is registered on the host OS (let 8021q kill it)
1456 */
Joe Perches1dc32912008-07-11 15:17:08 -07001457 if ((hw->mng_cookie.status &
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00001458 E1000_MNG_DHCP_COOKIE_STATUS_VLAN_SUPPORT) &&
1459 !test_bit(adapter->mng_vlan_id, adapter->active_vlans)) {
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001460 e1000_vlan_rx_kill_vid(netdev, adapter->mng_vlan_id);
1461 }
Jeff Kirsherb55ccb32006-01-12 16:50:30 -08001462
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 return 0;
1464}
1465
1466/**
1467 * e1000_check_64k_bound - check that memory doesn't cross 64kB boundary
1468 * @adapter: address of board private structure
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001469 * @start: address of beginning of memory
1470 * @len: length of memory
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 **/
Joe Perches64798842008-07-11 15:17:02 -07001472static bool e1000_check_64k_bound(struct e1000_adapter *adapter, void *start,
1473 unsigned long len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474{
Joe Perches1dc32912008-07-11 15:17:08 -07001475 struct e1000_hw *hw = &adapter->hw;
Joe Perchese982f172008-07-11 15:17:18 -07001476 unsigned long begin = (unsigned long)start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 unsigned long end = begin + len;
1478
Malli Chilakala26483452005-04-28 19:44:46 -07001479 /* First rev 82545 and 82546 need to not allow any memory
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00001480 * write location to cross 64k boundary due to errata 23
1481 */
Joe Perches1dc32912008-07-11 15:17:08 -07001482 if (hw->mac_type == e1000_82545 ||
Dirk Brandewie5377a412011-01-06 14:29:54 +00001483 hw->mac_type == e1000_ce4100 ||
Joe Perches1dc32912008-07-11 15:17:08 -07001484 hw->mac_type == e1000_82546) {
Joe Perchesc3033b02008-03-21 11:06:25 -07001485 return ((begin ^ (end - 1)) >> 16) != 0 ? false : true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 }
1487
Joe Perchesc3033b02008-03-21 11:06:25 -07001488 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489}
1490
1491/**
1492 * e1000_setup_tx_resources - allocate Tx resources (Descriptors)
1493 * @adapter: board private structure
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001494 * @txdr: tx descriptor ring (for a specific queue) to setup
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 *
1496 * Return 0 on success, negative on failure
1497 **/
Joe Perches64798842008-07-11 15:17:02 -07001498static int e1000_setup_tx_resources(struct e1000_adapter *adapter,
1499 struct e1000_tx_ring *txdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 struct pci_dev *pdev = adapter->pdev;
1502 int size;
1503
1504 size = sizeof(struct e1000_buffer) * txdr->count;
Eric Dumazet89bf67f2010-11-22 00:15:06 +00001505 txdr->buffer_info = vzalloc(size);
Joe Perches14f8dc42013-02-07 11:46:27 +00001506 if (!txdr->buffer_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
1509 /* round up to nearest 4K */
1510
1511 txdr->size = txdr->count * sizeof(struct e1000_tx_desc);
Milind Arun Choudhary9099cfb2007-04-27 13:55:29 -07001512 txdr->size = ALIGN(txdr->size, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
Nick Nunleyb16f53b2010-04-27 13:08:45 +00001514 txdr->desc = dma_alloc_coherent(&pdev->dev, txdr->size, &txdr->dma,
1515 GFP_KERNEL);
Jesse Brandeburg96838a42006-01-18 13:01:39 -08001516 if (!txdr->desc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517setup_tx_desc_die:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 vfree(txdr->buffer_info);
1519 return -ENOMEM;
1520 }
1521
Malli Chilakala26483452005-04-28 19:44:46 -07001522 /* Fix for errata 23, can't cross 64kB boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 if (!e1000_check_64k_bound(adapter, txdr->desc, txdr->size)) {
1524 void *olddesc = txdr->desc;
1525 dma_addr_t olddma = txdr->dma;
Emil Tantilovfeb8f472010-07-26 23:37:21 -07001526 e_err(tx_err, "txdr align check failed: %u bytes at %p\n",
Emil Tantilov675ad472010-04-27 14:02:58 +00001527 txdr->size, txdr->desc);
Malli Chilakala26483452005-04-28 19:44:46 -07001528 /* Try again, without freeing the previous */
Nick Nunleyb16f53b2010-04-27 13:08:45 +00001529 txdr->desc = dma_alloc_coherent(&pdev->dev, txdr->size,
1530 &txdr->dma, GFP_KERNEL);
Malli Chilakala26483452005-04-28 19:44:46 -07001531 /* Failed allocation, critical failure */
Jesse Brandeburg96838a42006-01-18 13:01:39 -08001532 if (!txdr->desc) {
Nick Nunleyb16f53b2010-04-27 13:08:45 +00001533 dma_free_coherent(&pdev->dev, txdr->size, olddesc,
1534 olddma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 goto setup_tx_desc_die;
1536 }
1537
1538 if (!e1000_check_64k_bound(adapter, txdr->desc, txdr->size)) {
1539 /* give up */
Nick Nunleyb16f53b2010-04-27 13:08:45 +00001540 dma_free_coherent(&pdev->dev, txdr->size, txdr->desc,
1541 txdr->dma);
1542 dma_free_coherent(&pdev->dev, txdr->size, olddesc,
1543 olddma);
Emil Tantilovfeb8f472010-07-26 23:37:21 -07001544 e_err(probe, "Unable to allocate aligned memory "
Emil Tantilov675ad472010-04-27 14:02:58 +00001545 "for the transmit descriptor ring\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 vfree(txdr->buffer_info);
1547 return -ENOMEM;
1548 } else {
Malli Chilakala26483452005-04-28 19:44:46 -07001549 /* Free old allocation, new allocation was successful */
Nick Nunleyb16f53b2010-04-27 13:08:45 +00001550 dma_free_coherent(&pdev->dev, txdr->size, olddesc,
1551 olddma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 }
1553 }
1554 memset(txdr->desc, 0, txdr->size);
1555
1556 txdr->next_to_use = 0;
1557 txdr->next_to_clean = 0;
1558
1559 return 0;
1560}
1561
1562/**
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001563 * e1000_setup_all_tx_resources - wrapper to allocate Tx resources
1564 * (Descriptors) for all queues
1565 * @adapter: board private structure
1566 *
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001567 * Return 0 on success, negative on failure
1568 **/
Joe Perches64798842008-07-11 15:17:02 -07001569int e1000_setup_all_tx_resources(struct e1000_adapter *adapter)
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001570{
1571 int i, err = 0;
1572
Jeff Kirsherf56799e2006-01-12 16:50:39 -08001573 for (i = 0; i < adapter->num_tx_queues; i++) {
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001574 err = e1000_setup_tx_resources(adapter, &adapter->tx_ring[i]);
1575 if (err) {
Emil Tantilovfeb8f472010-07-26 23:37:21 -07001576 e_err(probe, "Allocation for Tx Queue %u failed\n", i);
Vasily Averin3fbbc722006-08-28 14:56:24 -07001577 for (i-- ; i >= 0; i--)
1578 e1000_free_tx_resources(adapter,
1579 &adapter->tx_ring[i]);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001580 break;
1581 }
1582 }
1583
1584 return err;
1585}
1586
1587/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 * e1000_configure_tx - Configure 8254x Transmit Unit after Reset
1589 * @adapter: board private structure
1590 *
1591 * Configure the Tx unit of the MAC after a reset.
1592 **/
Joe Perches64798842008-07-11 15:17:02 -07001593static void e1000_configure_tx(struct e1000_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594{
Joe Perches406874a2008-04-03 10:06:32 -07001595 u64 tdba;
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001596 struct e1000_hw *hw = &adapter->hw;
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00001597 u32 tdlen, tctl, tipg;
Joe Perches406874a2008-04-03 10:06:32 -07001598 u32 ipgr1, ipgr2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
1600 /* Setup the HW Tx Head and Tail descriptor pointers */
1601
Jeff Kirsherf56799e2006-01-12 16:50:39 -08001602 switch (adapter->num_tx_queues) {
Mallikarjuna R Chilakala24025e4ec2005-10-04 07:03:23 -04001603 case 1:
1604 default:
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001605 tdba = adapter->tx_ring[0].dma;
1606 tdlen = adapter->tx_ring[0].count *
1607 sizeof(struct e1000_tx_desc);
Joe Perches1dc32912008-07-11 15:17:08 -07001608 ew32(TDLEN, tdlen);
1609 ew32(TDBAH, (tdba >> 32));
1610 ew32(TDBAL, (tdba & 0x00000000ffffffffULL));
1611 ew32(TDT, 0);
1612 ew32(TDH, 0);
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00001613 adapter->tx_ring[0].tdh = ((hw->mac_type >= e1000_82543) ?
1614 E1000_TDH : E1000_82542_TDH);
1615 adapter->tx_ring[0].tdt = ((hw->mac_type >= e1000_82543) ?
1616 E1000_TDT : E1000_82542_TDT);
Mallikarjuna R Chilakala24025e4ec2005-10-04 07:03:23 -04001617 break;
1618 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619
1620 /* Set the default values for the Tx Inter Packet Gap timer */
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00001621 if ((hw->media_type == e1000_media_type_fiber ||
Jesse Brandeburgd89b6c62006-12-15 10:38:32 +01001622 hw->media_type == e1000_media_type_internal_serdes))
Jeff Kirsher0fadb052006-01-12 16:51:05 -08001623 tipg = DEFAULT_82543_TIPG_IPGT_FIBER;
1624 else
1625 tipg = DEFAULT_82543_TIPG_IPGT_COPPER;
1626
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001627 switch (hw->mac_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 case e1000_82542_rev2_0:
1629 case e1000_82542_rev2_1:
1630 tipg = DEFAULT_82542_TIPG_IPGT;
Jeff Kirsher0fadb052006-01-12 16:51:05 -08001631 ipgr1 = DEFAULT_82542_TIPG_IPGR1;
1632 ipgr2 = DEFAULT_82542_TIPG_IPGR2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 break;
1634 default:
Jeff Kirsher0fadb052006-01-12 16:51:05 -08001635 ipgr1 = DEFAULT_82543_TIPG_IPGR1;
1636 ipgr2 = DEFAULT_82543_TIPG_IPGR2;
1637 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 }
Jeff Kirsher0fadb052006-01-12 16:51:05 -08001639 tipg |= ipgr1 << E1000_TIPG_IPGR1_SHIFT;
1640 tipg |= ipgr2 << E1000_TIPG_IPGR2_SHIFT;
Joe Perches1dc32912008-07-11 15:17:08 -07001641 ew32(TIPG, tipg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
1643 /* Set the Tx Interrupt Delay register */
1644
Joe Perches1dc32912008-07-11 15:17:08 -07001645 ew32(TIDV, adapter->tx_int_delay);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001646 if (hw->mac_type >= e1000_82540)
Joe Perches1dc32912008-07-11 15:17:08 -07001647 ew32(TADV, adapter->tx_abs_int_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648
1649 /* Program the Transmit Control Register */
1650
Joe Perches1dc32912008-07-11 15:17:08 -07001651 tctl = er32(TCTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 tctl &= ~E1000_TCTL_CT;
Jeff Kirsher7e6c9862006-03-02 18:19:30 -08001653 tctl |= E1000_TCTL_PSP | E1000_TCTL_RTLC |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 (E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT);
1655
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001656 e1000_config_collision_dist(hw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657
1658 /* Setup Transmit Descriptor Settings for eop descriptor */
Jesse Brandeburg6a042da2006-11-01 08:48:04 -08001659 adapter->txd_cmd = E1000_TXD_CMD_EOP | E1000_TXD_CMD_IFCS;
1660
1661 /* only set IDE if we are delaying interrupts using the timers */
1662 if (adapter->tx_int_delay)
1663 adapter->txd_cmd |= E1000_TXD_CMD_IDE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001665 if (hw->mac_type < e1000_82543)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 adapter->txd_cmd |= E1000_TXD_CMD_RPS;
1667 else
1668 adapter->txd_cmd |= E1000_TXD_CMD_RS;
1669
1670 /* Cache if we're 82544 running in PCI-X because we'll
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00001671 * need this to apply a workaround later in the send path.
1672 */
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001673 if (hw->mac_type == e1000_82544 &&
1674 hw->bus_type == e1000_bus_type_pcix)
Rusty Russell3db1cd52011-12-19 13:56:45 +00001675 adapter->pcix_82544 = true;
Jeff Kirsher7e6c9862006-03-02 18:19:30 -08001676
Joe Perches1dc32912008-07-11 15:17:08 -07001677 ew32(TCTL, tctl);
Jeff Kirsher7e6c9862006-03-02 18:19:30 -08001678
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679}
1680
1681/**
1682 * e1000_setup_rx_resources - allocate Rx resources (Descriptors)
1683 * @adapter: board private structure
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001684 * @rxdr: rx descriptor ring (for a specific queue) to setup
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 *
1686 * Returns 0 on success, negative on failure
1687 **/
Joe Perches64798842008-07-11 15:17:02 -07001688static int e1000_setup_rx_resources(struct e1000_adapter *adapter,
1689 struct e1000_rx_ring *rxdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 struct pci_dev *pdev = adapter->pdev;
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001692 int size, desc_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
1694 size = sizeof(struct e1000_buffer) * rxdr->count;
Eric Dumazet89bf67f2010-11-22 00:15:06 +00001695 rxdr->buffer_info = vzalloc(size);
Joe Perches14f8dc42013-02-07 11:46:27 +00001696 if (!rxdr->buffer_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00001699 desc_len = sizeof(struct e1000_rx_desc);
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001700
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 /* Round up to nearest 4K */
1702
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001703 rxdr->size = rxdr->count * desc_len;
Milind Arun Choudhary9099cfb2007-04-27 13:55:29 -07001704 rxdr->size = ALIGN(rxdr->size, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705
Nick Nunleyb16f53b2010-04-27 13:08:45 +00001706 rxdr->desc = dma_alloc_coherent(&pdev->dev, rxdr->size, &rxdr->dma,
1707 GFP_KERNEL);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001708 if (!rxdr->desc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709setup_rx_desc_die:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 vfree(rxdr->buffer_info);
1711 return -ENOMEM;
1712 }
1713
Malli Chilakala26483452005-04-28 19:44:46 -07001714 /* Fix for errata 23, can't cross 64kB boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 if (!e1000_check_64k_bound(adapter, rxdr->desc, rxdr->size)) {
1716 void *olddesc = rxdr->desc;
1717 dma_addr_t olddma = rxdr->dma;
Emil Tantilovfeb8f472010-07-26 23:37:21 -07001718 e_err(rx_err, "rxdr align check failed: %u bytes at %p\n",
Emil Tantilov675ad472010-04-27 14:02:58 +00001719 rxdr->size, rxdr->desc);
Malli Chilakala26483452005-04-28 19:44:46 -07001720 /* Try again, without freeing the previous */
Nick Nunleyb16f53b2010-04-27 13:08:45 +00001721 rxdr->desc = dma_alloc_coherent(&pdev->dev, rxdr->size,
1722 &rxdr->dma, GFP_KERNEL);
Malli Chilakala26483452005-04-28 19:44:46 -07001723 /* Failed allocation, critical failure */
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001724 if (!rxdr->desc) {
Nick Nunleyb16f53b2010-04-27 13:08:45 +00001725 dma_free_coherent(&pdev->dev, rxdr->size, olddesc,
1726 olddma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 goto setup_rx_desc_die;
1728 }
1729
1730 if (!e1000_check_64k_bound(adapter, rxdr->desc, rxdr->size)) {
1731 /* give up */
Nick Nunleyb16f53b2010-04-27 13:08:45 +00001732 dma_free_coherent(&pdev->dev, rxdr->size, rxdr->desc,
1733 rxdr->dma);
1734 dma_free_coherent(&pdev->dev, rxdr->size, olddesc,
1735 olddma);
Emil Tantilovfeb8f472010-07-26 23:37:21 -07001736 e_err(probe, "Unable to allocate aligned memory for "
1737 "the Rx descriptor ring\n");
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001738 goto setup_rx_desc_die;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 } else {
Malli Chilakala26483452005-04-28 19:44:46 -07001740 /* Free old allocation, new allocation was successful */
Nick Nunleyb16f53b2010-04-27 13:08:45 +00001741 dma_free_coherent(&pdev->dev, rxdr->size, olddesc,
1742 olddma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 }
1744 }
1745 memset(rxdr->desc, 0, rxdr->size);
1746
1747 rxdr->next_to_clean = 0;
1748 rxdr->next_to_use = 0;
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00001749 rxdr->rx_skb_top = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
1751 return 0;
1752}
1753
1754/**
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001755 * e1000_setup_all_rx_resources - wrapper to allocate Rx resources
1756 * (Descriptors) for all queues
1757 * @adapter: board private structure
1758 *
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001759 * Return 0 on success, negative on failure
1760 **/
Joe Perches64798842008-07-11 15:17:02 -07001761int e1000_setup_all_rx_resources(struct e1000_adapter *adapter)
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001762{
1763 int i, err = 0;
1764
Jeff Kirsherf56799e2006-01-12 16:50:39 -08001765 for (i = 0; i < adapter->num_rx_queues; i++) {
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001766 err = e1000_setup_rx_resources(adapter, &adapter->rx_ring[i]);
1767 if (err) {
Emil Tantilovfeb8f472010-07-26 23:37:21 -07001768 e_err(probe, "Allocation for Rx Queue %u failed\n", i);
Vasily Averin3fbbc722006-08-28 14:56:24 -07001769 for (i-- ; i >= 0; i--)
1770 e1000_free_rx_resources(adapter,
1771 &adapter->rx_ring[i]);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001772 break;
1773 }
1774 }
1775
1776 return err;
1777}
1778
1779/**
Malli Chilakala26483452005-04-28 19:44:46 -07001780 * e1000_setup_rctl - configure the receive control registers
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 * @adapter: Board private structure
1782 **/
Joe Perches64798842008-07-11 15:17:02 -07001783static void e1000_setup_rctl(struct e1000_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784{
Joe Perches1dc32912008-07-11 15:17:08 -07001785 struct e1000_hw *hw = &adapter->hw;
Brandeburg, Jesse630b25c2008-09-16 13:01:28 -07001786 u32 rctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
Joe Perches1dc32912008-07-11 15:17:08 -07001788 rctl = er32(RCTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
1790 rctl &= ~(3 << E1000_RCTL_MO_SHIFT);
1791
Dean Nelsond5bc77a2011-09-16 16:52:54 +00001792 rctl |= E1000_RCTL_BAM | E1000_RCTL_LBM_NO |
1793 E1000_RCTL_RDMTS_HALF |
Joe Perches1dc32912008-07-11 15:17:08 -07001794 (hw->mc_filter_type << E1000_RCTL_MO_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795
Joe Perches1dc32912008-07-11 15:17:08 -07001796 if (hw->tbi_compatibility_on == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 rctl |= E1000_RCTL_SBP;
1798 else
1799 rctl &= ~E1000_RCTL_SBP;
1800
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001801 if (adapter->netdev->mtu <= ETH_DATA_LEN)
1802 rctl &= ~E1000_RCTL_LPE;
1803 else
1804 rctl |= E1000_RCTL_LPE;
1805
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 /* Setup buffer sizes */
Auke Kok9e2feac2006-04-14 19:05:18 -07001807 rctl &= ~E1000_RCTL_SZ_4096;
1808 rctl |= E1000_RCTL_BSEX;
1809 switch (adapter->rx_buffer_len) {
Jeff Kirshera1415ee2006-02-28 20:24:07 -08001810 case E1000_RXBUFFER_2048:
1811 default:
1812 rctl |= E1000_RCTL_SZ_2048;
1813 rctl &= ~E1000_RCTL_BSEX;
1814 break;
1815 case E1000_RXBUFFER_4096:
1816 rctl |= E1000_RCTL_SZ_4096;
1817 break;
1818 case E1000_RXBUFFER_8192:
1819 rctl |= E1000_RCTL_SZ_8192;
1820 break;
1821 case E1000_RXBUFFER_16384:
1822 rctl |= E1000_RCTL_SZ_16384;
1823 break;
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001824 }
1825
Ben Greeare825b732012-04-04 06:01:29 +00001826 /* This is useful for sniffing bad packets. */
1827 if (adapter->netdev->features & NETIF_F_RXALL) {
1828 /* UPE and MPE will be handled by normal PROMISC logic
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00001829 * in e1000e_set_rx_mode
1830 */
Ben Greeare825b732012-04-04 06:01:29 +00001831 rctl |= (E1000_RCTL_SBP | /* Receive bad packets */
1832 E1000_RCTL_BAM | /* RX All Bcast Pkts */
1833 E1000_RCTL_PMCF); /* RX All MAC Ctrl Pkts */
1834
1835 rctl &= ~(E1000_RCTL_VFE | /* Disable VLAN filter */
1836 E1000_RCTL_DPF | /* Allow filtered pause */
1837 E1000_RCTL_CFIEN); /* Dis VLAN CFIEN Filter */
1838 /* Do not mess with E1000_CTRL_VME, it affects transmit as well,
1839 * and that breaks VLANs.
1840 */
1841 }
1842
Joe Perches1dc32912008-07-11 15:17:08 -07001843 ew32(RCTL, rctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844}
1845
1846/**
1847 * e1000_configure_rx - Configure 8254x Receive Unit after Reset
1848 * @adapter: board private structure
1849 *
1850 * Configure the Rx unit of the MAC after a reset.
1851 **/
Joe Perches64798842008-07-11 15:17:02 -07001852static void e1000_configure_rx(struct e1000_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853{
Joe Perches406874a2008-04-03 10:06:32 -07001854 u64 rdba;
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001855 struct e1000_hw *hw = &adapter->hw;
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00001856 u32 rdlen, rctl, rxcsum;
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001857
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00001858 if (adapter->netdev->mtu > ETH_DATA_LEN) {
1859 rdlen = adapter->rx_ring[0].count *
1860 sizeof(struct e1000_rx_desc);
1861 adapter->clean_rx = e1000_clean_jumbo_rx_irq;
1862 adapter->alloc_rx_buf = e1000_alloc_jumbo_rx_buffers;
1863 } else {
1864 rdlen = adapter->rx_ring[0].count *
1865 sizeof(struct e1000_rx_desc);
1866 adapter->clean_rx = e1000_clean_rx_irq;
1867 adapter->alloc_rx_buf = e1000_alloc_rx_buffers;
1868 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869
1870 /* disable receives while setting up the descriptors */
Joe Perches1dc32912008-07-11 15:17:08 -07001871 rctl = er32(RCTL);
1872 ew32(RCTL, rctl & ~E1000_RCTL_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873
1874 /* set the Receive Delay Timer Register */
Joe Perches1dc32912008-07-11 15:17:08 -07001875 ew32(RDTR, adapter->rx_int_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001877 if (hw->mac_type >= e1000_82540) {
Joe Perches1dc32912008-07-11 15:17:08 -07001878 ew32(RADV, adapter->rx_abs_int_delay);
Jesse Brandeburg835bb122006-11-01 08:48:13 -08001879 if (adapter->itr_setting != 0)
Joe Perches1dc32912008-07-11 15:17:08 -07001880 ew32(ITR, 1000000000 / (adapter->itr * 256));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 }
1882
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001883 /* Setup the HW Rx Head and Tail Descriptor Pointers and
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00001884 * the Base and Length of the Rx Descriptor Ring
1885 */
Jeff Kirsherf56799e2006-01-12 16:50:39 -08001886 switch (adapter->num_rx_queues) {
Mallikarjuna R Chilakala24025e4ec2005-10-04 07:03:23 -04001887 case 1:
1888 default:
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001889 rdba = adapter->rx_ring[0].dma;
Joe Perches1dc32912008-07-11 15:17:08 -07001890 ew32(RDLEN, rdlen);
1891 ew32(RDBAH, (rdba >> 32));
1892 ew32(RDBAL, (rdba & 0x00000000ffffffffULL));
1893 ew32(RDT, 0);
1894 ew32(RDH, 0);
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00001895 adapter->rx_ring[0].rdh = ((hw->mac_type >= e1000_82543) ?
1896 E1000_RDH : E1000_82542_RDH);
1897 adapter->rx_ring[0].rdt = ((hw->mac_type >= e1000_82543) ?
1898 E1000_RDT : E1000_82542_RDT);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001899 break;
Mallikarjuna R Chilakala24025e4ec2005-10-04 07:03:23 -04001900 }
1901
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 /* Enable 82543 Receive Checksum Offload for TCP and UDP */
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001903 if (hw->mac_type >= e1000_82543) {
Joe Perches1dc32912008-07-11 15:17:08 -07001904 rxcsum = er32(RXCSUM);
Brandeburg, Jesse630b25c2008-09-16 13:01:28 -07001905 if (adapter->rx_csum)
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001906 rxcsum |= E1000_RXCSUM_TUOFL;
Brandeburg, Jesse630b25c2008-09-16 13:01:28 -07001907 else
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001908 /* don't need to clear IPPCSE as it defaults to 0 */
Brandeburg, Jesse630b25c2008-09-16 13:01:28 -07001909 rxcsum &= ~E1000_RXCSUM_TUOFL;
Joe Perches1dc32912008-07-11 15:17:08 -07001910 ew32(RXCSUM, rxcsum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 }
1912
1913 /* Enable Receives */
Dean Nelsond5bc77a2011-09-16 16:52:54 +00001914 ew32(RCTL, rctl | E1000_RCTL_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915}
1916
1917/**
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001918 * e1000_free_tx_resources - Free Tx Resources per Queue
1919 * @adapter: board private structure
1920 * @tx_ring: Tx descriptor ring for a specific queue
1921 *
1922 * Free all transmit software resources
1923 **/
Joe Perches64798842008-07-11 15:17:02 -07001924static void e1000_free_tx_resources(struct e1000_adapter *adapter,
1925 struct e1000_tx_ring *tx_ring)
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001926{
1927 struct pci_dev *pdev = adapter->pdev;
1928
1929 e1000_clean_tx_ring(adapter, tx_ring);
1930
1931 vfree(tx_ring->buffer_info);
1932 tx_ring->buffer_info = NULL;
1933
Nick Nunleyb16f53b2010-04-27 13:08:45 +00001934 dma_free_coherent(&pdev->dev, tx_ring->size, tx_ring->desc,
1935 tx_ring->dma);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001936
1937 tx_ring->desc = NULL;
1938}
1939
1940/**
1941 * e1000_free_all_tx_resources - Free Tx Resources for All Queues
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 * @adapter: board private structure
1943 *
1944 * Free all transmit software resources
1945 **/
Joe Perches64798842008-07-11 15:17:02 -07001946void e1000_free_all_tx_resources(struct e1000_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947{
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001948 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949
Jeff Kirsherf56799e2006-01-12 16:50:39 -08001950 for (i = 0; i < adapter->num_tx_queues; i++)
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001951 e1000_free_tx_resources(adapter, &adapter->tx_ring[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952}
1953
Joe Perches64798842008-07-11 15:17:02 -07001954static void e1000_unmap_and_free_tx_resource(struct e1000_adapter *adapter,
1955 struct e1000_buffer *buffer_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956{
Alexander Duyck602c0552009-12-02 16:46:00 +00001957 if (buffer_info->dma) {
1958 if (buffer_info->mapped_as_page)
Nick Nunleyb16f53b2010-04-27 13:08:45 +00001959 dma_unmap_page(&adapter->pdev->dev, buffer_info->dma,
1960 buffer_info->length, DMA_TO_DEVICE);
Alexander Duyck602c0552009-12-02 16:46:00 +00001961 else
Nick Nunleyb16f53b2010-04-27 13:08:45 +00001962 dma_unmap_single(&adapter->pdev->dev, buffer_info->dma,
Alexander Duyck602c0552009-12-02 16:46:00 +00001963 buffer_info->length,
Nick Nunleyb16f53b2010-04-27 13:08:45 +00001964 DMA_TO_DEVICE);
Alexander Duyck602c0552009-12-02 16:46:00 +00001965 buffer_info->dma = 0;
1966 }
Jesse Brandeburga9ebadd2006-11-01 08:47:53 -08001967 if (buffer_info->skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 dev_kfree_skb_any(buffer_info->skb);
Jesse Brandeburga9ebadd2006-11-01 08:47:53 -08001969 buffer_info->skb = NULL;
1970 }
Alexander Duyck37e73df2009-03-25 21:58:45 +00001971 buffer_info->time_stamp = 0;
Jesse Brandeburga9ebadd2006-11-01 08:47:53 -08001972 /* buffer_info must be completely set up in the transmit path */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973}
1974
1975/**
1976 * e1000_clean_tx_ring - Free Tx Buffers
1977 * @adapter: board private structure
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001978 * @tx_ring: ring to be cleaned
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 **/
Joe Perches64798842008-07-11 15:17:02 -07001980static void e1000_clean_tx_ring(struct e1000_adapter *adapter,
1981 struct e1000_tx_ring *tx_ring)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982{
Joe Perches1dc32912008-07-11 15:17:08 -07001983 struct e1000_hw *hw = &adapter->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 struct e1000_buffer *buffer_info;
1985 unsigned long size;
1986 unsigned int i;
1987
1988 /* Free all the Tx ring sk_buffs */
1989
Jesse Brandeburg96838a42006-01-18 13:01:39 -08001990 for (i = 0; i < tx_ring->count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 buffer_info = &tx_ring->buffer_info[i];
1992 e1000_unmap_and_free_tx_resource(adapter, buffer_info);
1993 }
1994
Otto Estuardo Solares Cabrera2f66fd32012-08-24 08:04:58 +00001995 netdev_reset_queue(adapter->netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 size = sizeof(struct e1000_buffer) * tx_ring->count;
1997 memset(tx_ring->buffer_info, 0, size);
1998
1999 /* Zero out the descriptor ring */
2000
2001 memset(tx_ring->desc, 0, tx_ring->size);
2002
2003 tx_ring->next_to_use = 0;
2004 tx_ring->next_to_clean = 0;
Rusty Russell3db1cd52011-12-19 13:56:45 +00002005 tx_ring->last_tx_tso = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006
Joe Perches1dc32912008-07-11 15:17:08 -07002007 writel(0, hw->hw_addr + tx_ring->tdh);
2008 writel(0, hw->hw_addr + tx_ring->tdt);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002009}
2010
2011/**
2012 * e1000_clean_all_tx_rings - Free Tx Buffers for all queues
2013 * @adapter: board private structure
2014 **/
Joe Perches64798842008-07-11 15:17:02 -07002015static void e1000_clean_all_tx_rings(struct e1000_adapter *adapter)
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002016{
2017 int i;
2018
Jeff Kirsherf56799e2006-01-12 16:50:39 -08002019 for (i = 0; i < adapter->num_tx_queues; i++)
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002020 e1000_clean_tx_ring(adapter, &adapter->tx_ring[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021}
2022
2023/**
2024 * e1000_free_rx_resources - Free Rx Resources
2025 * @adapter: board private structure
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002026 * @rx_ring: ring to clean the resources from
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 *
2028 * Free all receive software resources
2029 **/
Joe Perches64798842008-07-11 15:17:02 -07002030static void e1000_free_rx_resources(struct e1000_adapter *adapter,
2031 struct e1000_rx_ring *rx_ring)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 struct pci_dev *pdev = adapter->pdev;
2034
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002035 e1000_clean_rx_ring(adapter, rx_ring);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036
2037 vfree(rx_ring->buffer_info);
2038 rx_ring->buffer_info = NULL;
2039
Nick Nunleyb16f53b2010-04-27 13:08:45 +00002040 dma_free_coherent(&pdev->dev, rx_ring->size, rx_ring->desc,
2041 rx_ring->dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042
2043 rx_ring->desc = NULL;
2044}
2045
2046/**
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002047 * e1000_free_all_rx_resources - Free Rx Resources for All Queues
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 * @adapter: board private structure
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002049 *
2050 * Free all receive software resources
2051 **/
Joe Perches64798842008-07-11 15:17:02 -07002052void e1000_free_all_rx_resources(struct e1000_adapter *adapter)
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002053{
2054 int i;
2055
Jeff Kirsherf56799e2006-01-12 16:50:39 -08002056 for (i = 0; i < adapter->num_rx_queues; i++)
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002057 e1000_free_rx_resources(adapter, &adapter->rx_ring[i]);
2058}
2059
2060/**
2061 * e1000_clean_rx_ring - Free Rx Buffers per Queue
2062 * @adapter: board private structure
2063 * @rx_ring: ring to free buffers from
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 **/
Joe Perches64798842008-07-11 15:17:02 -07002065static void e1000_clean_rx_ring(struct e1000_adapter *adapter,
2066 struct e1000_rx_ring *rx_ring)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067{
Joe Perches1dc32912008-07-11 15:17:08 -07002068 struct e1000_hw *hw = &adapter->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 struct e1000_buffer *buffer_info;
2070 struct pci_dev *pdev = adapter->pdev;
2071 unsigned long size;
Brandeburg, Jesse630b25c2008-09-16 13:01:28 -07002072 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073
2074 /* Free all the Rx ring sk_buffs */
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002075 for (i = 0; i < rx_ring->count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 buffer_info = &rx_ring->buffer_info[i];
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00002077 if (buffer_info->dma &&
2078 adapter->clean_rx == e1000_clean_rx_irq) {
Nick Nunleyb16f53b2010-04-27 13:08:45 +00002079 dma_unmap_single(&pdev->dev, buffer_info->dma,
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00002080 buffer_info->length,
Nick Nunleyb16f53b2010-04-27 13:08:45 +00002081 DMA_FROM_DEVICE);
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00002082 } else if (buffer_info->dma &&
2083 adapter->clean_rx == e1000_clean_jumbo_rx_irq) {
Nick Nunleyb16f53b2010-04-27 13:08:45 +00002084 dma_unmap_page(&pdev->dev, buffer_info->dma,
2085 buffer_info->length,
2086 DMA_FROM_DEVICE);
Jesse Brandeburg679be3b2009-06-30 12:45:34 +00002087 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088
Jesse Brandeburg679be3b2009-06-30 12:45:34 +00002089 buffer_info->dma = 0;
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00002090 if (buffer_info->page) {
2091 put_page(buffer_info->page);
2092 buffer_info->page = NULL;
2093 }
Jesse Brandeburg679be3b2009-06-30 12:45:34 +00002094 if (buffer_info->skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 dev_kfree_skb(buffer_info->skb);
2096 buffer_info->skb = NULL;
Jeff Kirsher997f5cb2006-01-12 16:50:55 -08002097 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 }
2099
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00002100 /* there also may be some cached data from a chained receive */
2101 if (rx_ring->rx_skb_top) {
2102 dev_kfree_skb(rx_ring->rx_skb_top);
2103 rx_ring->rx_skb_top = NULL;
2104 }
2105
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 size = sizeof(struct e1000_buffer) * rx_ring->count;
2107 memset(rx_ring->buffer_info, 0, size);
2108
2109 /* Zero out the descriptor ring */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 memset(rx_ring->desc, 0, rx_ring->size);
2111
2112 rx_ring->next_to_clean = 0;
2113 rx_ring->next_to_use = 0;
2114
Joe Perches1dc32912008-07-11 15:17:08 -07002115 writel(0, hw->hw_addr + rx_ring->rdh);
2116 writel(0, hw->hw_addr + rx_ring->rdt);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002117}
2118
2119/**
2120 * e1000_clean_all_rx_rings - Free Rx Buffers for all queues
2121 * @adapter: board private structure
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 **/
Joe Perches64798842008-07-11 15:17:02 -07002182static int e1000_set_mac(struct net_device *netdev, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183{
Malli Chilakala60490fe2005-06-17 17:41:45 -07002184 struct e1000_adapter *adapter = netdev_priv(netdev);
Joe Perches1dc32912008-07-11 15:17:08 -07002185 struct e1000_hw *hw = &adapter->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 struct sockaddr *addr = p;
2187
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002188 if (!is_valid_ether_addr(addr->sa_data))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 return -EADDRNOTAVAIL;
2190
2191 /* 82542 2.0 needs to be in reset to write receive address registers */
2192
Joe Perches1dc32912008-07-11 15:17:08 -07002193 if (hw->mac_type == e1000_82542_rev2_0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 e1000_enter_82542_rst(adapter);
2195
2196 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
Joe Perches1dc32912008-07-11 15:17:08 -07002197 memcpy(hw->mac_addr, addr->sa_data, netdev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198
Joe Perches1dc32912008-07-11 15:17:08 -07002199 e1000_rar_set(hw, hw->mac_addr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200
Joe Perches1dc32912008-07-11 15:17:08 -07002201 if (hw->mac_type == e1000_82542_rev2_0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 e1000_leave_82542_rst(adapter);
2203
2204 return 0;
2205}
2206
2207/**
Patrick McHardydb0ce502007-11-13 20:54:59 -08002208 * e1000_set_rx_mode - Secondary Unicast, Multicast and Promiscuous mode set
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 * @netdev: network interface device structure
2210 *
Patrick McHardydb0ce502007-11-13 20:54:59 -08002211 * The set_rx_mode entry point is called whenever the unicast or multicast
2212 * address lists or the network interface flags are updated. This routine is
2213 * responsible for configuring the hardware for proper unicast, multicast,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 * promiscuous mode, and all-multi behavior.
2215 **/
Joe Perches64798842008-07-11 15:17:02 -07002216static void e1000_set_rx_mode(struct net_device *netdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217{
Malli Chilakala60490fe2005-06-17 17:41:45 -07002218 struct e1000_adapter *adapter = netdev_priv(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 struct e1000_hw *hw = &adapter->hw;
Jiri Pirkoccffad252009-05-22 23:22:17 +00002220 struct netdev_hw_addr *ha;
2221 bool use_uc = false;
Joe Perches406874a2008-04-03 10:06:32 -07002222 u32 rctl;
2223 u32 hash_value;
Mallikarjuna R Chilakala868d5302005-10-04 06:58:59 -04002224 int i, rar_entries = E1000_RAR_ENTRIES;
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00002225 int mta_reg_count = E1000_NUM_MTA_REGISTERS;
Jesse Brandeburg81c52282009-04-04 16:36:53 -07002226 u32 *mcarray = kcalloc(mta_reg_count, sizeof(u32), GFP_ATOMIC);
2227
Joe Perches14f8dc42013-02-07 11:46:27 +00002228 if (!mcarray)
Jesse Brandeburg81c52282009-04-04 16:36:53 -07002229 return;
Auke Kokcd94dd02006-06-27 09:08:22 -07002230
Malli Chilakala26483452005-04-28 19:44:46 -07002231 /* Check for Promiscuous and All Multicast modes */
2232
Joe Perches1dc32912008-07-11 15:17:08 -07002233 rctl = er32(RCTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002235 if (netdev->flags & IFF_PROMISC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 rctl |= (E1000_RCTL_UPE | E1000_RCTL_MPE);
Patrick McHardy746b9f02008-07-16 20:15:45 -07002237 rctl &= ~E1000_RCTL_VFE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 } else {
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00002239 if (netdev->flags & IFF_ALLMULTI)
Patrick McHardy746b9f02008-07-16 20:15:45 -07002240 rctl |= E1000_RCTL_MPE;
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00002241 else
Patrick McHardy746b9f02008-07-16 20:15:45 -07002242 rctl &= ~E1000_RCTL_MPE;
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00002243 /* Enable VLAN filter if there is a VLAN */
Jiri Pirko5622e402011-07-21 03:26:31 +00002244 if (e1000_vlan_used(adapter))
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00002245 rctl |= E1000_RCTL_VFE;
Patrick McHardydb0ce502007-11-13 20:54:59 -08002246 }
2247
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08002248 if (netdev_uc_count(netdev) > rar_entries - 1) {
Patrick McHardydb0ce502007-11-13 20:54:59 -08002249 rctl |= E1000_RCTL_UPE;
2250 } else if (!(netdev->flags & IFF_PROMISC)) {
2251 rctl &= ~E1000_RCTL_UPE;
Jiri Pirkoccffad252009-05-22 23:22:17 +00002252 use_uc = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 }
2254
Joe Perches1dc32912008-07-11 15:17:08 -07002255 ew32(RCTL, rctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256
2257 /* 82542 2.0 needs to be in reset to write receive address registers */
2258
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002259 if (hw->mac_type == e1000_82542_rev2_0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 e1000_enter_82542_rst(adapter);
2261
Patrick McHardydb0ce502007-11-13 20:54:59 -08002262 /* load the first 14 addresses into the exact filters 1-14. Unicast
2263 * addresses take precedence to avoid disabling unicast filtering
2264 * when possible.
2265 *
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04002266 * RAR 0 is used for the station MAC address
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 * if there are not 14 addresses, go ahead and clear the filters
2268 */
Jiri Pirkoccffad252009-05-22 23:22:17 +00002269 i = 1;
2270 if (use_uc)
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08002271 netdev_for_each_uc_addr(ha, netdev) {
Jiri Pirkoccffad252009-05-22 23:22:17 +00002272 if (i == rar_entries)
2273 break;
2274 e1000_rar_set(hw, ha->addr, i++);
2275 }
2276
Jiri Pirko22bedad32010-04-01 21:22:57 +00002277 netdev_for_each_mc_addr(ha, netdev) {
Jiri Pirko7a81e9f2010-02-22 09:10:44 +00002278 if (i == rar_entries) {
2279 /* load any remaining addresses into the hash table */
2280 u32 hash_reg, hash_bit, mta;
Jiri Pirko22bedad32010-04-01 21:22:57 +00002281 hash_value = e1000_hash_mc_addr(hw, ha->addr);
Jiri Pirko7a81e9f2010-02-22 09:10:44 +00002282 hash_reg = (hash_value >> 5) & 0x7F;
2283 hash_bit = hash_value & 0x1F;
2284 mta = (1 << hash_bit);
2285 mcarray[hash_reg] |= mta;
Jiri Pirko10886af2010-02-23 01:19:22 -08002286 } else {
Jiri Pirko22bedad32010-04-01 21:22:57 +00002287 e1000_rar_set(hw, ha->addr, i++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 }
2289 }
2290
Jiri Pirko7a81e9f2010-02-22 09:10:44 +00002291 for (; i < rar_entries; i++) {
2292 E1000_WRITE_REG_ARRAY(hw, RA, i << 1, 0);
2293 E1000_WRITE_FLUSH();
2294 E1000_WRITE_REG_ARRAY(hw, RA, (i << 1) + 1, 0);
2295 E1000_WRITE_FLUSH();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 }
2297
Jesse Brandeburg81c52282009-04-04 16:36:53 -07002298 /* write the hash table completely, write from bottom to avoid
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00002299 * both stupid write combining chipsets, and flushing each write
2300 */
Jesse Brandeburg81c52282009-04-04 16:36:53 -07002301 for (i = mta_reg_count - 1; i >= 0 ; i--) {
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00002302 /* If we are on an 82544 has an errata where writing odd
Jesse Brandeburg81c52282009-04-04 16:36:53 -07002303 * offsets overwrites the previous even offset, but writing
2304 * backwards over the range solves the issue by always
2305 * writing the odd offset first
2306 */
2307 E1000_WRITE_REG_ARRAY(hw, MTA, i, mcarray[i]);
2308 }
2309 E1000_WRITE_FLUSH();
2310
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002311 if (hw->mac_type == e1000_82542_rev2_0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 e1000_leave_82542_rst(adapter);
Jesse Brandeburg81c52282009-04-04 16:36:53 -07002313
2314 kfree(mcarray);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315}
2316
Jesse Brandeburga4010af2011-10-05 07:24:41 +00002317/**
2318 * e1000_update_phy_info_task - get phy info
2319 * @work: work struct contained inside adapter struct
2320 *
2321 * Need to wait a few seconds after link up to get diagnostic information from
2322 * the phy
2323 */
Jesse Brandeburg5cf42fc2010-09-22 18:22:17 +00002324static void e1000_update_phy_info_task(struct work_struct *work)
2325{
2326 struct e1000_adapter *adapter = container_of(work,
Jesse Brandeburga4010af2011-10-05 07:24:41 +00002327 struct e1000_adapter,
2328 phy_info_task.work);
Jesse Brandeburg0ef4eed2011-10-05 07:24:51 +00002329 if (test_bit(__E1000_DOWN, &adapter->flags))
2330 return;
2331 mutex_lock(&adapter->mutex);
Jesse Brandeburga4010af2011-10-05 07:24:41 +00002332 e1000_phy_get_info(&adapter->hw, &adapter->phy_info);
Jesse Brandeburg0ef4eed2011-10-05 07:24:51 +00002333 mutex_unlock(&adapter->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334}
2335
2336/**
Jesse Brandeburg5cf42fc2010-09-22 18:22:17 +00002337 * e1000_82547_tx_fifo_stall_task - task to complete work
2338 * @work: work struct contained inside adapter struct
2339 **/
2340static void e1000_82547_tx_fifo_stall_task(struct work_struct *work)
2341{
2342 struct e1000_adapter *adapter = container_of(work,
Jesse Brandeburga4010af2011-10-05 07:24:41 +00002343 struct e1000_adapter,
2344 fifo_stall_task.work);
Joe Perches1dc32912008-07-11 15:17:08 -07002345 struct e1000_hw *hw = &adapter->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 struct net_device *netdev = adapter->netdev;
Joe Perches406874a2008-04-03 10:06:32 -07002347 u32 tctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348
Jesse Brandeburg0ef4eed2011-10-05 07:24:51 +00002349 if (test_bit(__E1000_DOWN, &adapter->flags))
2350 return;
2351 mutex_lock(&adapter->mutex);
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002352 if (atomic_read(&adapter->tx_fifo_stall)) {
Joe Perches1dc32912008-07-11 15:17:08 -07002353 if ((er32(TDT) == er32(TDH)) &&
2354 (er32(TDFT) == er32(TDFH)) &&
2355 (er32(TDFTS) == er32(TDFHS))) {
2356 tctl = er32(TCTL);
2357 ew32(TCTL, tctl & ~E1000_TCTL_EN);
2358 ew32(TDFT, adapter->tx_head_addr);
2359 ew32(TDFH, adapter->tx_head_addr);
2360 ew32(TDFTS, adapter->tx_head_addr);
2361 ew32(TDFHS, adapter->tx_head_addr);
2362 ew32(TCTL, tctl);
2363 E1000_WRITE_FLUSH();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364
2365 adapter->tx_fifo_head = 0;
2366 atomic_set(&adapter->tx_fifo_stall, 0);
2367 netif_wake_queue(netdev);
Jesse Brandeburgbaa34742009-09-25 12:17:23 +00002368 } else if (!test_bit(__E1000_DOWN, &adapter->flags)) {
Jesse Brandeburga4010af2011-10-05 07:24:41 +00002369 schedule_delayed_work(&adapter->fifo_stall_task, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 }
2371 }
Jesse Brandeburg0ef4eed2011-10-05 07:24:51 +00002372 mutex_unlock(&adapter->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373}
2374
Nick Nunleyb5481922010-02-03 14:49:28 +00002375bool e1000_has_link(struct e1000_adapter *adapter)
Jesse Brandeburgbe0f0712009-09-25 12:17:44 +00002376{
2377 struct e1000_hw *hw = &adapter->hw;
2378 bool link_active = false;
Jesse Brandeburgbe0f0712009-09-25 12:17:44 +00002379
Nicolas Schichan6d9e5132011-07-09 00:24:18 +00002380 /* get_link_status is set on LSC (link status) interrupt or rx
2381 * sequence error interrupt (except on intel ce4100).
2382 * get_link_status will stay false until the
2383 * e1000_check_for_link establishes link for copper adapters
2384 * ONLY
Jesse Brandeburgbe0f0712009-09-25 12:17:44 +00002385 */
2386 switch (hw->media_type) {
2387 case e1000_media_type_copper:
Nicolas Schichan6d9e5132011-07-09 00:24:18 +00002388 if (hw->mac_type == e1000_ce4100)
2389 hw->get_link_status = 1;
Jesse Brandeburgbe0f0712009-09-25 12:17:44 +00002390 if (hw->get_link_status) {
Jesse Brandeburg120a5d02009-09-25 15:19:46 -07002391 e1000_check_for_link(hw);
Jesse Brandeburgbe0f0712009-09-25 12:17:44 +00002392 link_active = !hw->get_link_status;
2393 } else {
2394 link_active = true;
2395 }
2396 break;
2397 case e1000_media_type_fiber:
Jesse Brandeburg120a5d02009-09-25 15:19:46 -07002398 e1000_check_for_link(hw);
Jesse Brandeburgbe0f0712009-09-25 12:17:44 +00002399 link_active = !!(er32(STATUS) & E1000_STATUS_LU);
2400 break;
2401 case e1000_media_type_internal_serdes:
Jesse Brandeburg120a5d02009-09-25 15:19:46 -07002402 e1000_check_for_link(hw);
Jesse Brandeburgbe0f0712009-09-25 12:17:44 +00002403 link_active = hw->serdes_has_link;
2404 break;
2405 default:
2406 break;
2407 }
2408
2409 return link_active;
2410}
2411
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412/**
Jesse Brandeburga4010af2011-10-05 07:24:41 +00002413 * e1000_watchdog - work function
2414 * @work: work struct contained inside adapter struct
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 **/
Jesse Brandeburga4010af2011-10-05 07:24:41 +00002416static void e1000_watchdog(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417{
Jesse Brandeburga4010af2011-10-05 07:24:41 +00002418 struct e1000_adapter *adapter = container_of(work,
2419 struct e1000_adapter,
2420 watchdog_task.work);
Joe Perches1dc32912008-07-11 15:17:08 -07002421 struct e1000_hw *hw = &adapter->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 struct net_device *netdev = adapter->netdev;
Jeff Kirsher545c67c2006-01-12 16:50:25 -08002423 struct e1000_tx_ring *txdr = adapter->tx_ring;
Joe Perches406874a2008-04-03 10:06:32 -07002424 u32 link, tctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425
Jesse Brandeburg0ef4eed2011-10-05 07:24:51 +00002426 if (test_bit(__E1000_DOWN, &adapter->flags))
2427 return;
2428
2429 mutex_lock(&adapter->mutex);
Jesse Brandeburgbe0f0712009-09-25 12:17:44 +00002430 link = e1000_has_link(adapter);
2431 if ((netif_carrier_ok(netdev)) && link)
2432 goto link_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002434 if (link) {
2435 if (!netif_carrier_ok(netdev)) {
Joe Perches406874a2008-04-03 10:06:32 -07002436 u32 ctrl;
Joe Perchesc3033b02008-03-21 11:06:25 -07002437 bool txb2b = true;
Jesse Brandeburgbe0f0712009-09-25 12:17:44 +00002438 /* update snapshot of PHY registers on LSC */
Joe Perches1dc32912008-07-11 15:17:08 -07002439 e1000_get_speed_and_duplex(hw,
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00002440 &adapter->link_speed,
2441 &adapter->link_duplex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442
Joe Perches1dc32912008-07-11 15:17:08 -07002443 ctrl = er32(CTRL);
Emil Tantilov675ad472010-04-27 14:02:58 +00002444 pr_info("%s NIC Link is Up %d Mbps %s, "
2445 "Flow Control: %s\n",
2446 netdev->name,
2447 adapter->link_speed,
2448 adapter->link_duplex == FULL_DUPLEX ?
2449 "Full Duplex" : "Half Duplex",
2450 ((ctrl & E1000_CTRL_TFCE) && (ctrl &
2451 E1000_CTRL_RFCE)) ? "RX/TX" : ((ctrl &
2452 E1000_CTRL_RFCE) ? "RX" : ((ctrl &
2453 E1000_CTRL_TFCE) ? "TX" : "None")));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454
Emil Tantilov39ca5f02010-03-26 11:25:58 +00002455 /* adjust timeout factor according to speed/duplex */
Jeff Kirsher66a2b0a2006-01-12 16:50:53 -08002456 adapter->tx_timeout_factor = 1;
Jeff Kirsher7e6c9862006-03-02 18:19:30 -08002457 switch (adapter->link_speed) {
2458 case SPEED_10:
Joe Perchesc3033b02008-03-21 11:06:25 -07002459 txb2b = false;
Jesse Brandeburgbe0f0712009-09-25 12:17:44 +00002460 adapter->tx_timeout_factor = 16;
Jeff Kirsher7e6c9862006-03-02 18:19:30 -08002461 break;
2462 case SPEED_100:
Joe Perchesc3033b02008-03-21 11:06:25 -07002463 txb2b = false;
Jeff Kirsher7e6c9862006-03-02 18:19:30 -08002464 /* maybe add some timeout factor ? */
2465 break;
2466 }
2467
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00002468 /* enable transmits in the hardware */
Joe Perches1dc32912008-07-11 15:17:08 -07002469 tctl = er32(TCTL);
Jeff Kirsher7e6c9862006-03-02 18:19:30 -08002470 tctl |= E1000_TCTL_EN;
Joe Perches1dc32912008-07-11 15:17:08 -07002471 ew32(TCTL, tctl);
Jeff Kirsher66a2b0a2006-01-12 16:50:53 -08002472
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 netif_carrier_on(netdev);
Jesse Brandeburgbaa34742009-09-25 12:17:23 +00002474 if (!test_bit(__E1000_DOWN, &adapter->flags))
Jesse Brandeburga4010af2011-10-05 07:24:41 +00002475 schedule_delayed_work(&adapter->phy_info_task,
2476 2 * HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 adapter->smartspeed = 0;
2478 }
2479 } else {
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002480 if (netif_carrier_ok(netdev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 adapter->link_speed = 0;
2482 adapter->link_duplex = 0;
Emil Tantilov675ad472010-04-27 14:02:58 +00002483 pr_info("%s NIC Link is Down\n",
2484 netdev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 netif_carrier_off(netdev);
Jesse Brandeburgbaa34742009-09-25 12:17:23 +00002486
2487 if (!test_bit(__E1000_DOWN, &adapter->flags))
Jesse Brandeburga4010af2011-10-05 07:24:41 +00002488 schedule_delayed_work(&adapter->phy_info_task,
2489 2 * HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 }
2491
2492 e1000_smartspeed(adapter);
2493 }
2494
Jesse Brandeburgbe0f0712009-09-25 12:17:44 +00002495link_up:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 e1000_update_stats(adapter);
2497
Joe Perches1dc32912008-07-11 15:17:08 -07002498 hw->tx_packet_delta = adapter->stats.tpt - adapter->tpt_old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 adapter->tpt_old = adapter->stats.tpt;
Joe Perches1dc32912008-07-11 15:17:08 -07002500 hw->collision_delta = adapter->stats.colc - adapter->colc_old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 adapter->colc_old = adapter->stats.colc;
2502
2503 adapter->gorcl = adapter->stats.gorcl - adapter->gorcl_old;
2504 adapter->gorcl_old = adapter->stats.gorcl;
2505 adapter->gotcl = adapter->stats.gotcl - adapter->gotcl_old;
2506 adapter->gotcl_old = adapter->stats.gotcl;
2507
Joe Perches1dc32912008-07-11 15:17:08 -07002508 e1000_update_adaptive(hw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509
Jeff Kirsherf56799e2006-01-12 16:50:39 -08002510 if (!netif_carrier_ok(netdev)) {
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002511 if (E1000_DESC_UNUSED(txdr) + 1 < txdr->count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 /* We've lost link, so the controller stops DMA,
2513 * but we've got queued Tx work that's never going
2514 * to get done, so reset controller to flush Tx.
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00002515 * (Do the reset outside of interrupt context).
2516 */
Jeff Kirsher87041632006-03-02 18:21:24 -08002517 adapter->tx_timeout_count++;
2518 schedule_work(&adapter->reset_task);
Jesse Brandeburg0ef4eed2011-10-05 07:24:51 +00002519 /* exit immediately since reset is imminent */
2520 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 }
2522 }
2523
Jesse Brandeburgeab2abf2010-05-04 22:26:03 +00002524 /* Simple mode for Interrupt Throttle Rate (ITR) */
2525 if (hw->mac_type >= e1000_82540 && adapter->itr_setting == 4) {
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00002526 /* Symmetric Tx/Rx gets a reduced ITR=2000;
Jesse Brandeburgeab2abf2010-05-04 22:26:03 +00002527 * Total asymmetrical Tx or Rx gets ITR=8000;
2528 * everyone else is between 2000-8000.
2529 */
2530 u32 goc = (adapter->gotcl + adapter->gorcl) / 10000;
2531 u32 dif = (adapter->gotcl > adapter->gorcl ?
2532 adapter->gotcl - adapter->gorcl :
2533 adapter->gorcl - adapter->gotcl) / 10000;
2534 u32 itr = goc > 0 ? (dif * 6000 / goc + 2000) : 8000;
2535
2536 ew32(ITR, 1000000000 / (itr * 256));
2537 }
2538
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 /* Cause software interrupt to ensure rx ring is cleaned */
Joe Perches1dc32912008-07-11 15:17:08 -07002540 ew32(ICS, E1000_ICS_RXDMT0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541
Malli Chilakala26483452005-04-28 19:44:46 -07002542 /* Force detection of hung controller every watchdog period */
Joe Perchesc3033b02008-03-21 11:06:25 -07002543 adapter->detect_tx_hung = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544
Jesse Brandeburga4010af2011-10-05 07:24:41 +00002545 /* Reschedule the task */
Jesse Brandeburgbaa34742009-09-25 12:17:23 +00002546 if (!test_bit(__E1000_DOWN, &adapter->flags))
Jesse Brandeburga4010af2011-10-05 07:24:41 +00002547 schedule_delayed_work(&adapter->watchdog_task, 2 * HZ);
Jesse Brandeburg0ef4eed2011-10-05 07:24:51 +00002548
2549unlock:
2550 mutex_unlock(&adapter->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551}
2552
Jesse Brandeburg835bb122006-11-01 08:48:13 -08002553enum latency_range {
2554 lowest_latency = 0,
2555 low_latency = 1,
2556 bulk_latency = 2,
2557 latency_invalid = 255
2558};
2559
2560/**
2561 * e1000_update_itr - update the dynamic ITR value based on statistics
Jesse Brandeburg8fce4732009-09-25 12:18:41 +00002562 * @adapter: pointer to adapter
2563 * @itr_setting: current adapter->itr
2564 * @packets: the number of packets during this measurement interval
2565 * @bytes: the number of bytes during this measurement interval
2566 *
Jesse Brandeburg835bb122006-11-01 08:48:13 -08002567 * Stores a new ITR value based on packets and byte
2568 * counts during the last interrupt. The advantage of per interrupt
2569 * computation is faster updates and more accurate ITR for the current
2570 * traffic pattern. Constants in this function were computed
2571 * based on theoretical maximum wire speed and thresholds were set based
2572 * on testing data as well as attempting to minimize response time
2573 * while increasing bulk throughput.
2574 * this functionality is controlled by the InterruptThrottleRate module
2575 * parameter (see e1000_param.c)
Jesse Brandeburg835bb122006-11-01 08:48:13 -08002576 **/
2577static unsigned int e1000_update_itr(struct e1000_adapter *adapter,
Joe Perches64798842008-07-11 15:17:02 -07002578 u16 itr_setting, int packets, int bytes)
Jesse Brandeburg835bb122006-11-01 08:48:13 -08002579{
2580 unsigned int retval = itr_setting;
2581 struct e1000_hw *hw = &adapter->hw;
2582
2583 if (unlikely(hw->mac_type < e1000_82540))
2584 goto update_itr_done;
2585
2586 if (packets == 0)
2587 goto update_itr_done;
2588
Jesse Brandeburg835bb122006-11-01 08:48:13 -08002589 switch (itr_setting) {
2590 case lowest_latency:
Jesse Brandeburg2b653262006-12-15 10:30:44 +01002591 /* jumbo frames get bulk treatment*/
2592 if (bytes/packets > 8000)
2593 retval = bulk_latency;
2594 else if ((packets < 5) && (bytes > 512))
Jesse Brandeburg835bb122006-11-01 08:48:13 -08002595 retval = low_latency;
2596 break;
2597 case low_latency: /* 50 usec aka 20000 ints/s */
2598 if (bytes > 10000) {
Jesse Brandeburg2b653262006-12-15 10:30:44 +01002599 /* jumbo frames need bulk latency setting */
2600 if (bytes/packets > 8000)
2601 retval = bulk_latency;
2602 else if ((packets < 10) || ((bytes/packets) > 1200))
Jesse Brandeburg835bb122006-11-01 08:48:13 -08002603 retval = bulk_latency;
2604 else if ((packets > 35))
2605 retval = lowest_latency;
Jesse Brandeburg2b653262006-12-15 10:30:44 +01002606 } else if (bytes/packets > 2000)
2607 retval = bulk_latency;
2608 else if (packets <= 2 && bytes < 512)
Jesse Brandeburg835bb122006-11-01 08:48:13 -08002609 retval = lowest_latency;
2610 break;
2611 case bulk_latency: /* 250 usec aka 4000 ints/s */
2612 if (bytes > 25000) {
2613 if (packets > 35)
2614 retval = low_latency;
Jesse Brandeburg2b653262006-12-15 10:30:44 +01002615 } else if (bytes < 6000) {
2616 retval = low_latency;
Jesse Brandeburg835bb122006-11-01 08:48:13 -08002617 }
2618 break;
2619 }
2620
2621update_itr_done:
2622 return retval;
2623}
2624
2625static void e1000_set_itr(struct e1000_adapter *adapter)
2626{
2627 struct e1000_hw *hw = &adapter->hw;
Joe Perches406874a2008-04-03 10:06:32 -07002628 u16 current_itr;
2629 u32 new_itr = adapter->itr;
Jesse Brandeburg835bb122006-11-01 08:48:13 -08002630
2631 if (unlikely(hw->mac_type < e1000_82540))
2632 return;
2633
2634 /* for non-gigabit speeds, just fix the interrupt rate at 4000 */
2635 if (unlikely(adapter->link_speed != SPEED_1000)) {
2636 current_itr = 0;
2637 new_itr = 4000;
2638 goto set_itr_now;
2639 }
2640
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00002641 adapter->tx_itr = e1000_update_itr(adapter, adapter->tx_itr,
2642 adapter->total_tx_packets,
2643 adapter->total_tx_bytes);
Jesse Brandeburg2b653262006-12-15 10:30:44 +01002644 /* conservative mode (itr 3) eliminates the lowest_latency setting */
2645 if (adapter->itr_setting == 3 && adapter->tx_itr == lowest_latency)
2646 adapter->tx_itr = low_latency;
2647
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00002648 adapter->rx_itr = e1000_update_itr(adapter, adapter->rx_itr,
2649 adapter->total_rx_packets,
2650 adapter->total_rx_bytes);
Jesse Brandeburg2b653262006-12-15 10:30:44 +01002651 /* conservative mode (itr 3) eliminates the lowest_latency setting */
2652 if (adapter->itr_setting == 3 && adapter->rx_itr == lowest_latency)
2653 adapter->rx_itr = low_latency;
Jesse Brandeburg835bb122006-11-01 08:48:13 -08002654
2655 current_itr = max(adapter->rx_itr, adapter->tx_itr);
2656
Jesse Brandeburg835bb122006-11-01 08:48:13 -08002657 switch (current_itr) {
2658 /* counts and packets in update_itr are dependent on these numbers */
2659 case lowest_latency:
2660 new_itr = 70000;
2661 break;
2662 case low_latency:
2663 new_itr = 20000; /* aka hwitr = ~200 */
2664 break;
2665 case bulk_latency:
2666 new_itr = 4000;
2667 break;
2668 default:
2669 break;
2670 }
2671
2672set_itr_now:
2673 if (new_itr != adapter->itr) {
2674 /* this attempts to bias the interrupt rate towards Bulk
2675 * by adding intermediate steps when interrupt rate is
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00002676 * increasing
2677 */
Jesse Brandeburg835bb122006-11-01 08:48:13 -08002678 new_itr = new_itr > adapter->itr ?
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00002679 min(adapter->itr + (new_itr >> 2), new_itr) :
2680 new_itr;
Jesse Brandeburg835bb122006-11-01 08:48:13 -08002681 adapter->itr = new_itr;
Joe Perches1dc32912008-07-11 15:17:08 -07002682 ew32(ITR, 1000000000 / (new_itr * 256));
Jesse Brandeburg835bb122006-11-01 08:48:13 -08002683 }
Jesse Brandeburg835bb122006-11-01 08:48:13 -08002684}
2685
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686#define E1000_TX_FLAGS_CSUM 0x00000001
2687#define E1000_TX_FLAGS_VLAN 0x00000002
2688#define E1000_TX_FLAGS_TSO 0x00000004
Malli Chilakala2d7edb92005-04-28 19:43:52 -07002689#define E1000_TX_FLAGS_IPV4 0x00000008
Ben Greear11a78dc2012-02-11 15:40:01 +00002690#define E1000_TX_FLAGS_NO_FCS 0x00000010
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691#define E1000_TX_FLAGS_VLAN_MASK 0xffff0000
2692#define E1000_TX_FLAGS_VLAN_SHIFT 16
2693
Joe Perches64798842008-07-11 15:17:02 -07002694static int e1000_tso(struct e1000_adapter *adapter,
2695 struct e1000_tx_ring *tx_ring, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 struct e1000_context_desc *context_desc;
Jeff Kirsher545c67c2006-01-12 16:50:25 -08002698 struct e1000_buffer *buffer_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 unsigned int i;
Joe Perches406874a2008-04-03 10:06:32 -07002700 u32 cmd_length = 0;
2701 u16 ipcse = 0, tucse, mss;
2702 u8 ipcss, ipcso, tucss, tucso, hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 int err;
2704
Herbert Xu89114af2006-07-08 13:34:32 -07002705 if (skb_is_gso(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 if (skb_header_cloned(skb)) {
2707 err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2708 if (err)
2709 return err;
2710 }
2711
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07002712 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
Herbert Xu79671682006-06-22 02:40:14 -07002713 mss = skb_shinfo(skb)->gso_size;
Alexey Dobriyan60828232006-05-23 14:52:21 -07002714 if (skb->protocol == htons(ETH_P_IP)) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002715 struct iphdr *iph = ip_hdr(skb);
2716 iph->tot_len = 0;
2717 iph->check = 0;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07002718 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
2719 iph->daddr, 0,
2720 IPPROTO_TCP,
2721 0);
Malli Chilakala2d7edb92005-04-28 19:43:52 -07002722 cmd_length = E1000_TXD_CMD_IP;
Arnaldo Carvalho de Meloea2ae172007-04-25 17:55:53 -07002723 ipcse = skb_transport_offset(skb) - 1;
Auke Koke15fdd02006-08-16 11:28:45 -07002724 } else if (skb->protocol == htons(ETH_P_IPV6)) {
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07002725 ipv6_hdr(skb)->payload_len = 0;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07002726 tcp_hdr(skb)->check =
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07002727 ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
2728 &ipv6_hdr(skb)->daddr,
2729 0, IPPROTO_TCP, 0);
Malli Chilakala2d7edb92005-04-28 19:43:52 -07002730 ipcse = 0;
Malli Chilakala2d7edb92005-04-28 19:43:52 -07002731 }
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03002732 ipcss = skb_network_offset(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002733 ipcso = (void *)&(ip_hdr(skb)->check) - (void *)skb->data;
Arnaldo Carvalho de Meloea2ae172007-04-25 17:55:53 -07002734 tucss = skb_transport_offset(skb);
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07002735 tucso = (void *)&(tcp_hdr(skb)->check) - (void *)skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 tucse = 0;
2737
2738 cmd_length |= (E1000_TXD_CMD_DEXT | E1000_TXD_CMD_TSE |
Malli Chilakala2d7edb92005-04-28 19:43:52 -07002739 E1000_TXD_CMD_TCP | (skb->len - (hdr_len)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002741 i = tx_ring->next_to_use;
2742 context_desc = E1000_CONTEXT_DESC(*tx_ring, i);
Jeff Kirsher545c67c2006-01-12 16:50:25 -08002743 buffer_info = &tx_ring->buffer_info[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744
2745 context_desc->lower_setup.ip_fields.ipcss = ipcss;
2746 context_desc->lower_setup.ip_fields.ipcso = ipcso;
2747 context_desc->lower_setup.ip_fields.ipcse = cpu_to_le16(ipcse);
2748 context_desc->upper_setup.tcp_fields.tucss = tucss;
2749 context_desc->upper_setup.tcp_fields.tucso = tucso;
2750 context_desc->upper_setup.tcp_fields.tucse = cpu_to_le16(tucse);
2751 context_desc->tcp_seg_setup.fields.mss = cpu_to_le16(mss);
2752 context_desc->tcp_seg_setup.fields.hdr_len = hdr_len;
2753 context_desc->cmd_and_length = cpu_to_le32(cmd_length);
2754
Jeff Kirsher545c67c2006-01-12 16:50:25 -08002755 buffer_info->time_stamp = jiffies;
Jesse Brandeburga9ebadd2006-11-01 08:47:53 -08002756 buffer_info->next_to_watch = i;
Jeff Kirsher545c67c2006-01-12 16:50:25 -08002757
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002758 if (++i == tx_ring->count) i = 0;
2759 tx_ring->next_to_use = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760
Joe Perchesc3033b02008-03-21 11:06:25 -07002761 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 }
Joe Perchesc3033b02008-03-21 11:06:25 -07002763 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764}
2765
Joe Perches64798842008-07-11 15:17:02 -07002766static bool e1000_tx_csum(struct e1000_adapter *adapter,
2767 struct e1000_tx_ring *tx_ring, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768{
2769 struct e1000_context_desc *context_desc;
Jeff Kirsher545c67c2006-01-12 16:50:25 -08002770 struct e1000_buffer *buffer_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 unsigned int i;
Joe Perches406874a2008-04-03 10:06:32 -07002772 u8 css;
Dave Graham3ed30672008-10-09 14:29:26 -07002773 u32 cmd_len = E1000_TXD_CMD_DEXT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774
Dave Graham3ed30672008-10-09 14:29:26 -07002775 if (skb->ip_summed != CHECKSUM_PARTIAL)
2776 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777
Dave Graham3ed30672008-10-09 14:29:26 -07002778 switch (skb->protocol) {
Harvey Harrison09640e62009-02-01 00:45:17 -08002779 case cpu_to_be16(ETH_P_IP):
Dave Graham3ed30672008-10-09 14:29:26 -07002780 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
2781 cmd_len |= E1000_TXD_CMD_TCP;
2782 break;
Harvey Harrison09640e62009-02-01 00:45:17 -08002783 case cpu_to_be16(ETH_P_IPV6):
Dave Graham3ed30672008-10-09 14:29:26 -07002784 /* XXX not handling all IPV6 headers */
2785 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
2786 cmd_len |= E1000_TXD_CMD_TCP;
2787 break;
2788 default:
2789 if (unlikely(net_ratelimit()))
Emil Tantilovfeb8f472010-07-26 23:37:21 -07002790 e_warn(drv, "checksum_partial proto=%x!\n",
2791 skb->protocol);
Dave Graham3ed30672008-10-09 14:29:26 -07002792 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 }
2794
Michał Mirosław0d0b1672010-12-14 15:24:08 +00002795 css = skb_checksum_start_offset(skb);
Dave Graham3ed30672008-10-09 14:29:26 -07002796
2797 i = tx_ring->next_to_use;
2798 buffer_info = &tx_ring->buffer_info[i];
2799 context_desc = E1000_CONTEXT_DESC(*tx_ring, i);
2800
2801 context_desc->lower_setup.ip_config = 0;
2802 context_desc->upper_setup.tcp_fields.tucss = css;
2803 context_desc->upper_setup.tcp_fields.tucso =
2804 css + skb->csum_offset;
2805 context_desc->upper_setup.tcp_fields.tucse = 0;
2806 context_desc->tcp_seg_setup.data = 0;
2807 context_desc->cmd_and_length = cpu_to_le32(cmd_len);
2808
2809 buffer_info->time_stamp = jiffies;
2810 buffer_info->next_to_watch = i;
2811
2812 if (unlikely(++i == tx_ring->count)) i = 0;
2813 tx_ring->next_to_use = i;
2814
2815 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816}
2817
2818#define E1000_MAX_TXD_PWR 12
2819#define E1000_MAX_DATA_PER_TXD (1<<E1000_MAX_TXD_PWR)
2820
Joe Perches64798842008-07-11 15:17:02 -07002821static int e1000_tx_map(struct e1000_adapter *adapter,
2822 struct e1000_tx_ring *tx_ring,
2823 struct sk_buff *skb, unsigned int first,
2824 unsigned int max_per_txd, unsigned int nr_frags,
2825 unsigned int mss)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826{
Joe Perches1dc32912008-07-11 15:17:08 -07002827 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck602c0552009-12-02 16:46:00 +00002828 struct pci_dev *pdev = adapter->pdev;
Alexander Duyck37e73df2009-03-25 21:58:45 +00002829 struct e1000_buffer *buffer_info;
Jesse Brandeburgd20b6062009-03-02 16:03:21 -08002830 unsigned int len = skb_headlen(skb);
Alexander Duyck602c0552009-12-02 16:46:00 +00002831 unsigned int offset = 0, size, count = 0, i;
Dean Nelson31c15a22011-08-25 14:39:24 +00002832 unsigned int f, bytecount, segs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833
2834 i = tx_ring->next_to_use;
2835
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002836 while (len) {
Alexander Duyck37e73df2009-03-25 21:58:45 +00002837 buffer_info = &tx_ring->buffer_info[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 size = min(len, max_per_txd);
Jeff Kirsherfd803242005-12-13 00:06:22 -05002839 /* Workaround for Controller erratum --
2840 * descriptor for non-tso packet in a linear SKB that follows a
2841 * tso gets written back prematurely before the data is fully
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00002842 * DMA'd to the controller
2843 */
Jeff Kirsherfd803242005-12-13 00:06:22 -05002844 if (!skb->data_len && tx_ring->last_tx_tso &&
Herbert Xu89114af2006-07-08 13:34:32 -07002845 !skb_is_gso(skb)) {
Rusty Russell3db1cd52011-12-19 13:56:45 +00002846 tx_ring->last_tx_tso = false;
Jeff Kirsherfd803242005-12-13 00:06:22 -05002847 size -= 4;
2848 }
2849
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 /* Workaround for premature desc write-backs
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00002851 * in TSO mode. Append 4-byte sentinel desc
2852 */
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002853 if (unlikely(mss && !nr_frags && size == len && size > 8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 size -= 4;
Malli Chilakala97338bd2005-04-28 19:41:46 -07002855 /* work-around for errata 10 and it applies
2856 * to all controllers in PCI-X mode
2857 * The fix is to make sure that the first descriptor of a
2858 * packet is smaller than 2048 - 16 - 16 (or 2016) bytes
2859 */
Joe Perches1dc32912008-07-11 15:17:08 -07002860 if (unlikely((hw->bus_type == e1000_bus_type_pcix) &&
Malli Chilakala97338bd2005-04-28 19:41:46 -07002861 (size > 2015) && count == 0))
2862 size = 2015;
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002863
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 /* Workaround for potential 82544 hang in PCI-X. Avoid
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00002865 * terminating buffers within evenly-aligned dwords.
2866 */
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002867 if (unlikely(adapter->pcix_82544 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 !((unsigned long)(skb->data + offset + size - 1) & 4) &&
2869 size > 4))
2870 size -= 4;
2871
2872 buffer_info->length = size;
Jesse Brandeburgcdd75492009-09-25 12:18:07 +00002873 /* set time_stamp *before* dma to help avoid a possible race */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 buffer_info->time_stamp = jiffies;
Alexander Duyck602c0552009-12-02 16:46:00 +00002875 buffer_info->mapped_as_page = false;
Nick Nunleyb16f53b2010-04-27 13:08:45 +00002876 buffer_info->dma = dma_map_single(&pdev->dev,
2877 skb->data + offset,
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00002878 size, DMA_TO_DEVICE);
Nick Nunleyb16f53b2010-04-27 13:08:45 +00002879 if (dma_mapping_error(&pdev->dev, buffer_info->dma))
Alexander Duyck602c0552009-12-02 16:46:00 +00002880 goto dma_error;
Jesse Brandeburga9ebadd2006-11-01 08:47:53 -08002881 buffer_info->next_to_watch = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882
2883 len -= size;
2884 offset += size;
2885 count++;
Alexander Duyck37e73df2009-03-25 21:58:45 +00002886 if (len) {
2887 i++;
2888 if (unlikely(i == tx_ring->count))
2889 i = 0;
2890 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 }
2892
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002893 for (f = 0; f < nr_frags; f++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00002894 const struct skb_frag_struct *frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895
2896 frag = &skb_shinfo(skb)->frags[f];
Eric Dumazet9e903e02011-10-18 21:00:24 +00002897 len = skb_frag_size(frag);
Ian Campbell877749b2011-08-29 23:18:26 +00002898 offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002900 while (len) {
Ian Campbell877749b2011-08-29 23:18:26 +00002901 unsigned long bufend;
Alexander Duyck37e73df2009-03-25 21:58:45 +00002902 i++;
2903 if (unlikely(i == tx_ring->count))
2904 i = 0;
2905
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 buffer_info = &tx_ring->buffer_info[i];
2907 size = min(len, max_per_txd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 /* Workaround for premature desc write-backs
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00002909 * in TSO mode. Append 4-byte sentinel desc
2910 */
2911 if (unlikely(mss && f == (nr_frags-1) &&
2912 size == len && size > 8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 size -= 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 /* Workaround for potential 82544 hang in PCI-X.
2915 * Avoid terminating buffers within evenly-aligned
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00002916 * dwords.
2917 */
Ian Campbell877749b2011-08-29 23:18:26 +00002918 bufend = (unsigned long)
2919 page_to_phys(skb_frag_page(frag));
2920 bufend += offset + size - 1;
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002921 if (unlikely(adapter->pcix_82544 &&
Ian Campbell877749b2011-08-29 23:18:26 +00002922 !(bufend & 4) &&
2923 size > 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 size -= 4;
2925
2926 buffer_info->length = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 buffer_info->time_stamp = jiffies;
Alexander Duyck602c0552009-12-02 16:46:00 +00002928 buffer_info->mapped_as_page = true;
Ian Campbell877749b2011-08-29 23:18:26 +00002929 buffer_info->dma = skb_frag_dma_map(&pdev->dev, frag,
2930 offset, size, DMA_TO_DEVICE);
Nick Nunleyb16f53b2010-04-27 13:08:45 +00002931 if (dma_mapping_error(&pdev->dev, buffer_info->dma))
Alexander Duyck602c0552009-12-02 16:46:00 +00002932 goto dma_error;
Jesse Brandeburga9ebadd2006-11-01 08:47:53 -08002933 buffer_info->next_to_watch = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934
2935 len -= size;
2936 offset += size;
2937 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 }
2939 }
2940
Dean Nelson31c15a22011-08-25 14:39:24 +00002941 segs = skb_shinfo(skb)->gso_segs ?: 1;
2942 /* multiply data chunks by size of headers */
2943 bytecount = ((segs - 1) * skb_headlen(skb)) + skb->len;
2944
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 tx_ring->buffer_info[i].skb = skb;
Dean Nelson31c15a22011-08-25 14:39:24 +00002946 tx_ring->buffer_info[i].segs = segs;
2947 tx_ring->buffer_info[i].bytecount = bytecount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 tx_ring->buffer_info[first].next_to_watch = i;
2949
2950 return count;
Alexander Duyck602c0552009-12-02 16:46:00 +00002951
2952dma_error:
2953 dev_err(&pdev->dev, "TX DMA map failed\n");
2954 buffer_info->dma = 0;
Roel Kluinc1fa3472010-01-19 14:21:45 +00002955 if (count)
Alexander Duyck602c0552009-12-02 16:46:00 +00002956 count--;
Roel Kluinc1fa3472010-01-19 14:21:45 +00002957
2958 while (count--) {
2959 if (i==0)
Alexander Duyck602c0552009-12-02 16:46:00 +00002960 i += tx_ring->count;
Roel Kluinc1fa3472010-01-19 14:21:45 +00002961 i--;
Alexander Duyck602c0552009-12-02 16:46:00 +00002962 buffer_info = &tx_ring->buffer_info[i];
2963 e1000_unmap_and_free_tx_resource(adapter, buffer_info);
2964 }
2965
2966 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967}
2968
Joe Perches64798842008-07-11 15:17:02 -07002969static void e1000_tx_queue(struct e1000_adapter *adapter,
2970 struct e1000_tx_ring *tx_ring, int tx_flags,
2971 int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972{
Joe Perches1dc32912008-07-11 15:17:08 -07002973 struct e1000_hw *hw = &adapter->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 struct e1000_tx_desc *tx_desc = NULL;
2975 struct e1000_buffer *buffer_info;
Joe Perches406874a2008-04-03 10:06:32 -07002976 u32 txd_upper = 0, txd_lower = E1000_TXD_CMD_IFCS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 unsigned int i;
2978
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002979 if (likely(tx_flags & E1000_TX_FLAGS_TSO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 txd_lower |= E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D |
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00002981 E1000_TXD_CMD_TSE;
Malli Chilakala2d7edb92005-04-28 19:43:52 -07002982 txd_upper |= E1000_TXD_POPTS_TXSM << 8;
2983
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002984 if (likely(tx_flags & E1000_TX_FLAGS_IPV4))
Malli Chilakala2d7edb92005-04-28 19:43:52 -07002985 txd_upper |= E1000_TXD_POPTS_IXSM << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 }
2987
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002988 if (likely(tx_flags & E1000_TX_FLAGS_CSUM)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 txd_lower |= E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D;
2990 txd_upper |= E1000_TXD_POPTS_TXSM << 8;
2991 }
2992
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002993 if (unlikely(tx_flags & E1000_TX_FLAGS_VLAN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 txd_lower |= E1000_TXD_CMD_VLE;
2995 txd_upper |= (tx_flags & E1000_TX_FLAGS_VLAN_MASK);
2996 }
2997
Ben Greear11a78dc2012-02-11 15:40:01 +00002998 if (unlikely(tx_flags & E1000_TX_FLAGS_NO_FCS))
2999 txd_lower &= ~(E1000_TXD_CMD_IFCS);
3000
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 i = tx_ring->next_to_use;
3002
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003003 while (count--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 buffer_info = &tx_ring->buffer_info[i];
3005 tx_desc = E1000_TX_DESC(*tx_ring, i);
3006 tx_desc->buffer_addr = cpu_to_le64(buffer_info->dma);
3007 tx_desc->lower.data =
3008 cpu_to_le32(txd_lower | buffer_info->length);
3009 tx_desc->upper.data = cpu_to_le32(txd_upper);
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003010 if (unlikely(++i == tx_ring->count)) i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 }
3012
3013 tx_desc->lower.data |= cpu_to_le32(adapter->txd_cmd);
3014
Ben Greear11a78dc2012-02-11 15:40:01 +00003015 /* txd_cmd re-enables FCS, so we'll re-disable it here as desired. */
3016 if (unlikely(tx_flags & E1000_TX_FLAGS_NO_FCS))
3017 tx_desc->lower.data &= ~(cpu_to_le32(E1000_TXD_CMD_IFCS));
3018
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 /* Force memory writes to complete before letting h/w
3020 * know there are new descriptors to fetch. (Only
3021 * applicable for weak-ordered memory model archs,
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00003022 * such as IA-64).
3023 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 wmb();
3025
3026 tx_ring->next_to_use = i;
Joe Perches1dc32912008-07-11 15:17:08 -07003027 writel(i, hw->hw_addr + tx_ring->tdt);
Jesse Brandeburg2ce90472006-11-01 08:47:42 -08003028 /* we need this if more than one processor can write to our tail
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00003029 * at a time, it synchronizes IO on IA64/Altix systems
3030 */
Jesse Brandeburg2ce90472006-11-01 08:47:42 -08003031 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032}
3033
Ben Hutchings1aa8b472012-07-10 10:56:59 +00003034/* 82547 workaround to avoid controller hang in half-duplex environment.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 * The workaround is to avoid queuing a large packet that would span
3036 * the internal Tx FIFO ring boundary by notifying the stack to resend
3037 * the packet at a later time. This gives the Tx FIFO an opportunity to
3038 * flush all packets. When that occurs, we reset the Tx FIFO pointers
3039 * to the beginning of the Tx FIFO.
Ben Hutchings1aa8b472012-07-10 10:56:59 +00003040 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041
3042#define E1000_FIFO_HDR 0x10
3043#define E1000_82547_PAD_LEN 0x3E0
3044
Joe Perches64798842008-07-11 15:17:02 -07003045static int e1000_82547_fifo_workaround(struct e1000_adapter *adapter,
3046 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047{
Joe Perches406874a2008-04-03 10:06:32 -07003048 u32 fifo_space = adapter->tx_fifo_size - adapter->tx_fifo_head;
3049 u32 skb_fifo_len = skb->len + E1000_FIFO_HDR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050
Milind Arun Choudhary9099cfb2007-04-27 13:55:29 -07003051 skb_fifo_len = ALIGN(skb_fifo_len, E1000_FIFO_HDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003053 if (adapter->link_duplex != HALF_DUPLEX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 goto no_fifo_stall_required;
3055
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003056 if (atomic_read(&adapter->tx_fifo_stall))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 return 1;
3058
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003059 if (skb_fifo_len >= (E1000_82547_PAD_LEN + fifo_space)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 atomic_set(&adapter->tx_fifo_stall, 1);
3061 return 1;
3062 }
3063
3064no_fifo_stall_required:
3065 adapter->tx_fifo_head += skb_fifo_len;
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003066 if (adapter->tx_fifo_head >= adapter->tx_fifo_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 adapter->tx_fifo_head -= adapter->tx_fifo_size;
3068 return 0;
3069}
3070
Jesse Brandeburg65c79732006-09-27 12:53:48 -07003071static int __e1000_maybe_stop_tx(struct net_device *netdev, int size)
3072{
3073 struct e1000_adapter *adapter = netdev_priv(netdev);
3074 struct e1000_tx_ring *tx_ring = adapter->tx_ring;
3075
3076 netif_stop_queue(netdev);
3077 /* Herbert's original patch had:
3078 * smp_mb__after_netif_stop_queue();
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00003079 * but since that doesn't exist yet, just open code it.
3080 */
Jesse Brandeburg65c79732006-09-27 12:53:48 -07003081 smp_mb();
3082
3083 /* We need to check again in a case another CPU has just
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00003084 * made room available.
3085 */
Jesse Brandeburg65c79732006-09-27 12:53:48 -07003086 if (likely(E1000_DESC_UNUSED(tx_ring) < size))
3087 return -EBUSY;
3088
3089 /* A reprieve! */
3090 netif_start_queue(netdev);
Jesse Brandeburgfcfb1222006-11-01 08:47:59 -08003091 ++adapter->restart_queue;
Jesse Brandeburg65c79732006-09-27 12:53:48 -07003092 return 0;
3093}
3094
3095static int e1000_maybe_stop_tx(struct net_device *netdev,
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00003096 struct e1000_tx_ring *tx_ring, int size)
Jesse Brandeburg65c79732006-09-27 12:53:48 -07003097{
3098 if (likely(E1000_DESC_UNUSED(tx_ring) >= size))
3099 return 0;
3100 return __e1000_maybe_stop_tx(netdev, size);
3101}
3102
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103#define TXD_USE_COUNT(S, X) (((S) >> (X)) + 1 )
Stephen Hemminger3b29a562009-08-31 19:50:55 +00003104static netdev_tx_t e1000_xmit_frame(struct sk_buff *skb,
3105 struct net_device *netdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106{
Malli Chilakala60490fe2005-06-17 17:41:45 -07003107 struct e1000_adapter *adapter = netdev_priv(netdev);
Joe Perches1dc32912008-07-11 15:17:08 -07003108 struct e1000_hw *hw = &adapter->hw;
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003109 struct e1000_tx_ring *tx_ring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 unsigned int first, max_per_txd = E1000_MAX_DATA_PER_TXD;
3111 unsigned int max_txd_pwr = E1000_MAX_TXD_PWR;
3112 unsigned int tx_flags = 0;
Eric Dumazete743d312010-04-14 15:59:40 -07003113 unsigned int len = skb_headlen(skb);
Krishna Kumar6d1e3aa2007-10-05 14:15:16 -07003114 unsigned int nr_frags;
3115 unsigned int mss;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 int count = 0;
Auke Kok76c224b2006-05-23 13:36:06 -07003117 int tso;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 unsigned int f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00003120 /* This goes back to the question of how to logically map a Tx queue
Jesse Brandeburg65c79732006-09-27 12:53:48 -07003121 * to a flow. Right now, performance is impacted slightly negatively
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00003122 * if using multiple Tx queues. If the stack breaks away from a
3123 * single qdisc implementation, we can look at this again.
3124 */
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003125 tx_ring = adapter->tx_ring;
Mallikarjuna R Chilakala24025e4ec2005-10-04 07:03:23 -04003126
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003127 if (unlikely(skb->len <= 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 dev_kfree_skb_any(skb);
3129 return NETDEV_TX_OK;
3130 }
3131
Tushar Dave59d86c72012-09-15 10:16:57 +00003132 /* On PCI/PCI-X HW, if packet size is less than ETH_ZLEN,
3133 * packets may get corrupted during padding by HW.
3134 * To WA this issue, pad all small packets manually.
3135 */
3136 if (skb->len < ETH_ZLEN) {
3137 if (skb_pad(skb, ETH_ZLEN - skb->len))
3138 return NETDEV_TX_OK;
3139 skb->len = ETH_ZLEN;
3140 skb_set_tail_pointer(skb, ETH_ZLEN);
3141 }
3142
Herbert Xu79671682006-06-22 02:40:14 -07003143 mss = skb_shinfo(skb)->gso_size;
Auke Kok76c224b2006-05-23 13:36:06 -07003144 /* The controller does a simple calculation to
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 * make sure there is enough room in the FIFO before
3146 * initiating the DMA for each buffer. The calc is:
3147 * 4 = ceil(buffer len/mss). To make sure we don't
3148 * overrun the FIFO, adjust the max buffer len if mss
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00003149 * drops.
3150 */
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003151 if (mss) {
Joe Perches406874a2008-04-03 10:06:32 -07003152 u8 hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 max_per_txd = min(mss << 2, max_per_txd);
3154 max_txd_pwr = fls(max_per_txd) - 1;
Jeff Kirsher9a3056d2006-01-12 16:50:23 -08003155
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07003156 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
Krishna Kumar6d1e3aa2007-10-05 14:15:16 -07003157 if (skb->data_len && hdr_len == len) {
Joe Perches1dc32912008-07-11 15:17:08 -07003158 switch (hw->mac_type) {
Jeff Kirsher9f687882006-03-02 18:20:17 -08003159 unsigned int pull_size;
Herbert Xu683a2aa2006-12-16 12:04:33 +11003160 case e1000_82544:
3161 /* Make sure we have room to chop off 4 bytes,
3162 * and that the end alignment will work out to
3163 * this hardware's requirements
3164 * NOTE: this is a TSO only workaround
3165 * if end byte alignment not correct move us
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00003166 * into the next dword
3167 */
3168 if ((unsigned long)(skb_tail_pointer(skb) - 1)
3169 & 4)
Herbert Xu683a2aa2006-12-16 12:04:33 +11003170 break;
3171 /* fall through */
Jeff Kirsher9f687882006-03-02 18:20:17 -08003172 pull_size = min((unsigned int)4, skb->data_len);
3173 if (!__pskb_pull_tail(skb, pull_size)) {
Emil Tantilovfeb8f472010-07-26 23:37:21 -07003174 e_err(drv, "__pskb_pull_tail "
3175 "failed.\n");
Jeff Kirsher9f687882006-03-02 18:20:17 -08003176 dev_kfree_skb_any(skb);
Jeff Garzik749dfc702006-03-11 13:35:31 -05003177 return NETDEV_TX_OK;
Jeff Kirsher9f687882006-03-02 18:20:17 -08003178 }
Eric Dumazete743d312010-04-14 15:59:40 -07003179 len = skb_headlen(skb);
Jeff Kirsher9f687882006-03-02 18:20:17 -08003180 break;
3181 default:
3182 /* do nothing */
3183 break;
Jeff Kirsherd74bbd32006-01-12 16:51:07 -08003184 }
Jeff Kirsher9a3056d2006-01-12 16:50:23 -08003185 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 }
3187
Jeff Kirsher9a3056d2006-01-12 16:50:23 -08003188 /* reserve a descriptor for the offload context */
Patrick McHardy84fa7932006-08-29 16:44:56 -07003189 if ((mss) || (skb->ip_summed == CHECKSUM_PARTIAL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190 count++;
Malli Chilakala26483452005-04-28 19:44:46 -07003191 count++;
Jeff Kirsherfd803242005-12-13 00:06:22 -05003192
Jeff Kirsherfd803242005-12-13 00:06:22 -05003193 /* Controller Erratum workaround */
Herbert Xu89114af2006-07-08 13:34:32 -07003194 if (!skb->data_len && tx_ring->last_tx_tso && !skb_is_gso(skb))
Jeff Kirsherfd803242005-12-13 00:06:22 -05003195 count++;
Jeff Kirsherfd803242005-12-13 00:06:22 -05003196
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 count += TXD_USE_COUNT(len, max_txd_pwr);
3198
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003199 if (adapter->pcix_82544)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 count++;
3201
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003202 /* work-around for errata 10 and it applies to all controllers
Malli Chilakala97338bd2005-04-28 19:41:46 -07003203 * in PCI-X mode, so add one more descriptor to the count
3204 */
Joe Perches1dc32912008-07-11 15:17:08 -07003205 if (unlikely((hw->bus_type == e1000_bus_type_pcix) &&
Malli Chilakala97338bd2005-04-28 19:41:46 -07003206 (len > 2015)))
3207 count++;
3208
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 nr_frags = skb_shinfo(skb)->nr_frags;
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003210 for (f = 0; f < nr_frags; f++)
Eric Dumazet9e903e02011-10-18 21:00:24 +00003211 count += TXD_USE_COUNT(skb_frag_size(&skb_shinfo(skb)->frags[f]),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 max_txd_pwr);
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003213 if (adapter->pcix_82544)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 count += nr_frags;
3215
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 /* need: count + 2 desc gap to keep tail from touching
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00003217 * head, otherwise try next time
3218 */
Alexander Duyck80179432009-01-21 14:42:47 -08003219 if (unlikely(e1000_maybe_stop_tx(netdev, tx_ring, count + 2)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221
Jesse Brandeburga4010af2011-10-05 07:24:41 +00003222 if (unlikely((hw->mac_type == e1000_82547) &&
3223 (e1000_82547_fifo_workaround(adapter, skb)))) {
3224 netif_stop_queue(netdev);
3225 if (!test_bit(__E1000_DOWN, &adapter->flags))
3226 schedule_delayed_work(&adapter->fifo_stall_task, 1);
3227 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228 }
3229
Jiri Pirko5622e402011-07-21 03:26:31 +00003230 if (vlan_tx_tag_present(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 tx_flags |= E1000_TX_FLAGS_VLAN;
3232 tx_flags |= (vlan_tx_tag_get(skb) << E1000_TX_FLAGS_VLAN_SHIFT);
3233 }
3234
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003235 first = tx_ring->next_to_use;
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003236
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003237 tso = e1000_tso(adapter, tx_ring, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238 if (tso < 0) {
3239 dev_kfree_skb_any(skb);
3240 return NETDEV_TX_OK;
3241 }
3242
Jeff Kirsherfd803242005-12-13 00:06:22 -05003243 if (likely(tso)) {
Jesse Brandeburg8fce4732009-09-25 12:18:41 +00003244 if (likely(hw->mac_type != e1000_82544))
Rusty Russell3db1cd52011-12-19 13:56:45 +00003245 tx_ring->last_tx_tso = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 tx_flags |= E1000_TX_FLAGS_TSO;
Jeff Kirsherfd803242005-12-13 00:06:22 -05003247 } else if (likely(e1000_tx_csum(adapter, tx_ring, skb)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 tx_flags |= E1000_TX_FLAGS_CSUM;
3249
Alexey Dobriyan60828232006-05-23 14:52:21 -07003250 if (likely(skb->protocol == htons(ETH_P_IP)))
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003251 tx_flags |= E1000_TX_FLAGS_IPV4;
3252
Ben Greear11a78dc2012-02-11 15:40:01 +00003253 if (unlikely(skb->no_fcs))
3254 tx_flags |= E1000_TX_FLAGS_NO_FCS;
3255
Alexander Duyck37e73df2009-03-25 21:58:45 +00003256 count = e1000_tx_map(adapter, tx_ring, skb, first, max_per_txd,
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00003257 nr_frags, mss);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258
Alexander Duyck37e73df2009-03-25 21:58:45 +00003259 if (count) {
Otto Estuardo Solares Cabrera2f66fd32012-08-24 08:04:58 +00003260 netdev_sent_queue(netdev, skb->len);
Willem de Bruijneab467f2012-04-27 09:04:04 +00003261 skb_tx_timestamp(skb);
3262
Alexander Duyck37e73df2009-03-25 21:58:45 +00003263 e1000_tx_queue(adapter, tx_ring, tx_flags, count);
Alexander Duyck37e73df2009-03-25 21:58:45 +00003264 /* Make sure there is space in the ring for the next send. */
3265 e1000_maybe_stop_tx(netdev, tx_ring, MAX_SKB_FRAGS + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266
Alexander Duyck37e73df2009-03-25 21:58:45 +00003267 } else {
3268 dev_kfree_skb_any(skb);
3269 tx_ring->buffer_info[first].time_stamp = 0;
3270 tx_ring->next_to_use = first;
3271 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 return NETDEV_TX_OK;
3274}
3275
Tushar Daveb04e36b2012-01-27 09:00:46 +00003276#define NUM_REGS 38 /* 1 based count */
3277static void e1000_regdump(struct e1000_adapter *adapter)
3278{
3279 struct e1000_hw *hw = &adapter->hw;
3280 u32 regs[NUM_REGS];
3281 u32 *regs_buff = regs;
3282 int i = 0;
3283
Tushar Davee29b5d82012-02-10 08:06:36 +00003284 static const char * const reg_name[] = {
3285 "CTRL", "STATUS",
3286 "RCTL", "RDLEN", "RDH", "RDT", "RDTR",
3287 "TCTL", "TDBAL", "TDBAH", "TDLEN", "TDH", "TDT",
3288 "TIDV", "TXDCTL", "TADV", "TARC0",
3289 "TDBAL1", "TDBAH1", "TDLEN1", "TDH1", "TDT1",
3290 "TXDCTL1", "TARC1",
3291 "CTRL_EXT", "ERT", "RDBAL", "RDBAH",
3292 "TDFH", "TDFT", "TDFHS", "TDFTS", "TDFPC",
3293 "RDFH", "RDFT", "RDFHS", "RDFTS", "RDFPC"
Tushar Daveb04e36b2012-01-27 09:00:46 +00003294 };
3295
3296 regs_buff[0] = er32(CTRL);
3297 regs_buff[1] = er32(STATUS);
3298
3299 regs_buff[2] = er32(RCTL);
3300 regs_buff[3] = er32(RDLEN);
3301 regs_buff[4] = er32(RDH);
3302 regs_buff[5] = er32(RDT);
3303 regs_buff[6] = er32(RDTR);
3304
3305 regs_buff[7] = er32(TCTL);
3306 regs_buff[8] = er32(TDBAL);
3307 regs_buff[9] = er32(TDBAH);
3308 regs_buff[10] = er32(TDLEN);
3309 regs_buff[11] = er32(TDH);
3310 regs_buff[12] = er32(TDT);
3311 regs_buff[13] = er32(TIDV);
3312 regs_buff[14] = er32(TXDCTL);
3313 regs_buff[15] = er32(TADV);
3314 regs_buff[16] = er32(TARC0);
3315
3316 regs_buff[17] = er32(TDBAL1);
3317 regs_buff[18] = er32(TDBAH1);
3318 regs_buff[19] = er32(TDLEN1);
3319 regs_buff[20] = er32(TDH1);
3320 regs_buff[21] = er32(TDT1);
3321 regs_buff[22] = er32(TXDCTL1);
3322 regs_buff[23] = er32(TARC1);
3323 regs_buff[24] = er32(CTRL_EXT);
3324 regs_buff[25] = er32(ERT);
3325 regs_buff[26] = er32(RDBAL0);
3326 regs_buff[27] = er32(RDBAH0);
3327 regs_buff[28] = er32(TDFH);
3328 regs_buff[29] = er32(TDFT);
3329 regs_buff[30] = er32(TDFHS);
3330 regs_buff[31] = er32(TDFTS);
3331 regs_buff[32] = er32(TDFPC);
3332 regs_buff[33] = er32(RDFH);
3333 regs_buff[34] = er32(RDFT);
3334 regs_buff[35] = er32(RDFHS);
3335 regs_buff[36] = er32(RDFTS);
3336 regs_buff[37] = er32(RDFPC);
3337
3338 pr_info("Register dump\n");
Tushar Davee29b5d82012-02-10 08:06:36 +00003339 for (i = 0; i < NUM_REGS; i++)
3340 pr_info("%-15s %08x\n", reg_name[i], regs_buff[i]);
Tushar Daveb04e36b2012-01-27 09:00:46 +00003341}
3342
3343/*
3344 * e1000_dump: Print registers, tx ring and rx ring
3345 */
3346static void e1000_dump(struct e1000_adapter *adapter)
3347{
3348 /* this code doesn't handle multiple rings */
3349 struct e1000_tx_ring *tx_ring = adapter->tx_ring;
3350 struct e1000_rx_ring *rx_ring = adapter->rx_ring;
3351 int i;
3352
3353 if (!netif_msg_hw(adapter))
3354 return;
3355
3356 /* Print Registers */
3357 e1000_regdump(adapter);
3358
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00003359 /* transmit dump */
Tushar Daveb04e36b2012-01-27 09:00:46 +00003360 pr_info("TX Desc ring0 dump\n");
3361
3362 /* Transmit Descriptor Formats - DEXT[29] is 0 (Legacy) or 1 (Extended)
3363 *
3364 * Legacy Transmit Descriptor
3365 * +--------------------------------------------------------------+
3366 * 0 | Buffer Address [63:0] (Reserved on Write Back) |
3367 * +--------------------------------------------------------------+
3368 * 8 | Special | CSS | Status | CMD | CSO | Length |
3369 * +--------------------------------------------------------------+
3370 * 63 48 47 36 35 32 31 24 23 16 15 0
3371 *
3372 * Extended Context Descriptor (DTYP=0x0) for TSO or checksum offload
3373 * 63 48 47 40 39 32 31 16 15 8 7 0
3374 * +----------------------------------------------------------------+
3375 * 0 | TUCSE | TUCS0 | TUCSS | IPCSE | IPCS0 | IPCSS |
3376 * +----------------------------------------------------------------+
3377 * 8 | MSS | HDRLEN | RSV | STA | TUCMD | DTYP | PAYLEN |
3378 * +----------------------------------------------------------------+
3379 * 63 48 47 40 39 36 35 32 31 24 23 20 19 0
3380 *
3381 * Extended Data Descriptor (DTYP=0x1)
3382 * +----------------------------------------------------------------+
3383 * 0 | Buffer Address [63:0] |
3384 * +----------------------------------------------------------------+
3385 * 8 | VLAN tag | POPTS | Rsvd | Status | Command | DTYP | DTALEN |
3386 * +----------------------------------------------------------------+
3387 * 63 48 47 40 39 36 35 32 31 24 23 20 19 0
3388 */
Tushar Davee29b5d82012-02-10 08:06:36 +00003389 pr_info("Tc[desc] [Ce CoCsIpceCoS] [MssHlRSCm0Plen] [bi->dma ] leng ntw timestmp bi->skb\n");
3390 pr_info("Td[desc] [address 63:0 ] [VlaPoRSCm1Dlen] [bi->dma ] leng ntw timestmp bi->skb\n");
Tushar Daveb04e36b2012-01-27 09:00:46 +00003391
3392 if (!netif_msg_tx_done(adapter))
3393 goto rx_ring_summary;
3394
3395 for (i = 0; tx_ring->desc && (i < tx_ring->count); i++) {
3396 struct e1000_tx_desc *tx_desc = E1000_TX_DESC(*tx_ring, i);
3397 struct e1000_buffer *buffer_info = &tx_ring->buffer_info[i];
Andrei Emeltchenkodd7f5c92012-03-25 17:49:25 +00003398 struct my_u { __le64 a; __le64 b; };
Tushar Daveb04e36b2012-01-27 09:00:46 +00003399 struct my_u *u = (struct my_u *)tx_desc;
Tushar Davee29b5d82012-02-10 08:06:36 +00003400 const char *type;
3401
Tushar Daveb04e36b2012-01-27 09:00:46 +00003402 if (i == tx_ring->next_to_use && i == tx_ring->next_to_clean)
Tushar Davee29b5d82012-02-10 08:06:36 +00003403 type = "NTC/U";
Tushar Daveb04e36b2012-01-27 09:00:46 +00003404 else if (i == tx_ring->next_to_use)
Tushar Davee29b5d82012-02-10 08:06:36 +00003405 type = "NTU";
Tushar Daveb04e36b2012-01-27 09:00:46 +00003406 else if (i == tx_ring->next_to_clean)
Tushar Davee29b5d82012-02-10 08:06:36 +00003407 type = "NTC";
Tushar Daveb04e36b2012-01-27 09:00:46 +00003408 else
Tushar Davee29b5d82012-02-10 08:06:36 +00003409 type = "";
Tushar Daveb04e36b2012-01-27 09:00:46 +00003410
Tushar Davee29b5d82012-02-10 08:06:36 +00003411 pr_info("T%c[0x%03X] %016llX %016llX %016llX %04X %3X %016llX %p %s\n",
3412 ((le64_to_cpu(u->b) & (1<<20)) ? 'd' : 'c'), i,
3413 le64_to_cpu(u->a), le64_to_cpu(u->b),
3414 (u64)buffer_info->dma, buffer_info->length,
3415 buffer_info->next_to_watch,
3416 (u64)buffer_info->time_stamp, buffer_info->skb, type);
Tushar Daveb04e36b2012-01-27 09:00:46 +00003417 }
3418
3419rx_ring_summary:
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00003420 /* receive dump */
Tushar Daveb04e36b2012-01-27 09:00:46 +00003421 pr_info("\nRX Desc ring dump\n");
3422
3423 /* Legacy Receive Descriptor Format
3424 *
3425 * +-----------------------------------------------------+
3426 * | Buffer Address [63:0] |
3427 * +-----------------------------------------------------+
3428 * | VLAN Tag | Errors | Status 0 | Packet csum | Length |
3429 * +-----------------------------------------------------+
3430 * 63 48 47 40 39 32 31 16 15 0
3431 */
Tushar Davee29b5d82012-02-10 08:06:36 +00003432 pr_info("R[desc] [address 63:0 ] [vl er S cks ln] [bi->dma ] [bi->skb]\n");
Tushar Daveb04e36b2012-01-27 09:00:46 +00003433
3434 if (!netif_msg_rx_status(adapter))
3435 goto exit;
3436
3437 for (i = 0; rx_ring->desc && (i < rx_ring->count); i++) {
3438 struct e1000_rx_desc *rx_desc = E1000_RX_DESC(*rx_ring, i);
3439 struct e1000_buffer *buffer_info = &rx_ring->buffer_info[i];
Andrei Emeltchenkodd7f5c92012-03-25 17:49:25 +00003440 struct my_u { __le64 a; __le64 b; };
Tushar Daveb04e36b2012-01-27 09:00:46 +00003441 struct my_u *u = (struct my_u *)rx_desc;
Tushar Davee29b5d82012-02-10 08:06:36 +00003442 const char *type;
3443
Tushar Daveb04e36b2012-01-27 09:00:46 +00003444 if (i == rx_ring->next_to_use)
Tushar Davee29b5d82012-02-10 08:06:36 +00003445 type = "NTU";
Tushar Daveb04e36b2012-01-27 09:00:46 +00003446 else if (i == rx_ring->next_to_clean)
Tushar Davee29b5d82012-02-10 08:06:36 +00003447 type = "NTC";
Tushar Daveb04e36b2012-01-27 09:00:46 +00003448 else
Tushar Davee29b5d82012-02-10 08:06:36 +00003449 type = "";
Tushar Daveb04e36b2012-01-27 09:00:46 +00003450
Tushar Davee29b5d82012-02-10 08:06:36 +00003451 pr_info("R[0x%03X] %016llX %016llX %016llX %p %s\n",
3452 i, le64_to_cpu(u->a), le64_to_cpu(u->b),
3453 (u64)buffer_info->dma, buffer_info->skb, type);
Tushar Daveb04e36b2012-01-27 09:00:46 +00003454 } /* for */
3455
3456 /* dump the descriptor caches */
3457 /* rx */
Tushar Davee29b5d82012-02-10 08:06:36 +00003458 pr_info("Rx descriptor cache in 64bit format\n");
Tushar Daveb04e36b2012-01-27 09:00:46 +00003459 for (i = 0x6000; i <= 0x63FF ; i += 0x10) {
Tushar Davee29b5d82012-02-10 08:06:36 +00003460 pr_info("R%04X: %08X|%08X %08X|%08X\n",
3461 i,
3462 readl(adapter->hw.hw_addr + i+4),
3463 readl(adapter->hw.hw_addr + i),
3464 readl(adapter->hw.hw_addr + i+12),
3465 readl(adapter->hw.hw_addr + i+8));
Tushar Daveb04e36b2012-01-27 09:00:46 +00003466 }
3467 /* tx */
Tushar Davee29b5d82012-02-10 08:06:36 +00003468 pr_info("Tx descriptor cache in 64bit format\n");
Tushar Daveb04e36b2012-01-27 09:00:46 +00003469 for (i = 0x7000; i <= 0x73FF ; i += 0x10) {
Tushar Davee29b5d82012-02-10 08:06:36 +00003470 pr_info("T%04X: %08X|%08X %08X|%08X\n",
3471 i,
3472 readl(adapter->hw.hw_addr + i+4),
3473 readl(adapter->hw.hw_addr + i),
3474 readl(adapter->hw.hw_addr + i+12),
3475 readl(adapter->hw.hw_addr + i+8));
Tushar Daveb04e36b2012-01-27 09:00:46 +00003476 }
3477exit:
3478 return;
3479}
3480
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481/**
3482 * e1000_tx_timeout - Respond to a Tx Hang
3483 * @netdev: network interface device structure
3484 **/
Joe Perches64798842008-07-11 15:17:02 -07003485static void e1000_tx_timeout(struct net_device *netdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486{
Malli Chilakala60490fe2005-06-17 17:41:45 -07003487 struct e1000_adapter *adapter = netdev_priv(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488
3489 /* Do the reset outside of interrupt context */
Jeff Kirsher87041632006-03-02 18:21:24 -08003490 adapter->tx_timeout_count++;
3491 schedule_work(&adapter->reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492}
3493
Joe Perches64798842008-07-11 15:17:02 -07003494static void e1000_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495{
David Howells65f27f32006-11-22 14:55:48 +00003496 struct e1000_adapter *adapter =
3497 container_of(work, struct e1000_adapter, reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498
Jesse Brandeburg0ef4eed2011-10-05 07:24:51 +00003499 if (test_bit(__E1000_DOWN, &adapter->flags))
3500 return;
Tushar Daveb04e36b2012-01-27 09:00:46 +00003501 e_err(drv, "Reset adapter\n");
Jesse Brandeburg338c15e2010-09-22 18:22:42 +00003502 e1000_reinit_safe(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503}
3504
3505/**
3506 * e1000_get_stats - Get System Network Statistics
3507 * @netdev: network interface device structure
3508 *
3509 * Returns the address of the device statistics structure.
Jesse Brandeburga4010af2011-10-05 07:24:41 +00003510 * The statistics are actually updated from the watchdog.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511 **/
Joe Perches64798842008-07-11 15:17:02 -07003512static struct net_device_stats *e1000_get_stats(struct net_device *netdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513{
Jeff Kirsher6b7660c2006-01-12 16:50:35 -08003514 /* only return the current stats */
Ajit Khaparde5fe31de2009-10-07 02:42:23 +00003515 return &netdev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516}
3517
3518/**
3519 * e1000_change_mtu - Change the Maximum Transfer Unit
3520 * @netdev: network interface device structure
3521 * @new_mtu: new value for maximum frame size
3522 *
3523 * Returns 0 on success, negative on failure
3524 **/
Joe Perches64798842008-07-11 15:17:02 -07003525static int e1000_change_mtu(struct net_device *netdev, int new_mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526{
Malli Chilakala60490fe2005-06-17 17:41:45 -07003527 struct e1000_adapter *adapter = netdev_priv(netdev);
Joe Perches1dc32912008-07-11 15:17:08 -07003528 struct e1000_hw *hw = &adapter->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 int max_frame = new_mtu + ENET_HEADER_SIZE + ETHERNET_FCS_SIZE;
3530
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003531 if ((max_frame < MINIMUM_ETHERNET_FRAME_SIZE) ||
3532 (max_frame > MAX_JUMBO_FRAME_SIZE)) {
Emil Tantilovfeb8f472010-07-26 23:37:21 -07003533 e_err(probe, "Invalid MTU setting\n");
Mallikarjuna R Chilakala868d5302005-10-04 06:58:59 -04003534 return -EINVAL;
3535 }
3536
Jeff Kirsher997f5cb2006-01-12 16:50:55 -08003537 /* Adapter-specific max frame size limits. */
Joe Perches1dc32912008-07-11 15:17:08 -07003538 switch (hw->mac_type) {
Auke Kok9e2feac2006-04-14 19:05:18 -07003539 case e1000_undefined ... e1000_82542_rev2_1:
Jesse Brandeburgb7cb8c22009-07-06 10:45:01 +00003540 if (max_frame > (ETH_FRAME_LEN + ETH_FCS_LEN)) {
Emil Tantilovfeb8f472010-07-26 23:37:21 -07003541 e_err(probe, "Jumbo Frames not supported.\n");
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003542 return -EINVAL;
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003543 }
Jeff Kirsher997f5cb2006-01-12 16:50:55 -08003544 break;
Jeff Kirsher997f5cb2006-01-12 16:50:55 -08003545 default:
3546 /* Capable of supporting up to MAX_JUMBO_FRAME_SIZE limit. */
3547 break;
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003548 }
3549
Jesse Brandeburg3d6114e2009-09-25 12:19:02 +00003550 while (test_and_set_bit(__E1000_RESETTING, &adapter->flags))
3551 msleep(1);
3552 /* e1000_down has a dependency on max_frame_size */
3553 hw->max_frame_size = max_frame;
3554 if (netif_running(netdev))
3555 e1000_down(adapter);
3556
David S. Miller87f50322006-07-31 22:39:40 -07003557 /* NOTE: netdev_alloc_skb reserves 16 bytes, and typically NET_IP_ALIGN
Auke Kok9e2feac2006-04-14 19:05:18 -07003558 * means we reserve 2 more, this pushes us to allocate from the next
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00003559 * larger slab size.
3560 * i.e. RXBUFFER_2048 --> size-4096 slab
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00003561 * however with the new *_jumbo_rx* routines, jumbo receives will use
3562 * fragmented skbs
3563 */
Jeff Kirsher997f5cb2006-01-12 16:50:55 -08003564
Jesse Brandeburg99261462010-01-22 22:56:16 +00003565 if (max_frame <= E1000_RXBUFFER_2048)
Auke Kok9e2feac2006-04-14 19:05:18 -07003566 adapter->rx_buffer_len = E1000_RXBUFFER_2048;
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00003567 else
3568#if (PAGE_SIZE >= E1000_RXBUFFER_16384)
Auke Kok9e2feac2006-04-14 19:05:18 -07003569 adapter->rx_buffer_len = E1000_RXBUFFER_16384;
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00003570#elif (PAGE_SIZE >= E1000_RXBUFFER_4096)
3571 adapter->rx_buffer_len = PAGE_SIZE;
3572#endif
Auke Kok9e2feac2006-04-14 19:05:18 -07003573
3574 /* adjust allocation if LPE protects us, and we aren't using SBP */
Joe Perches1dc32912008-07-11 15:17:08 -07003575 if (!hw->tbi_compatibility_on &&
Jesse Brandeburgb7cb8c22009-07-06 10:45:01 +00003576 ((max_frame == (ETH_FRAME_LEN + ETH_FCS_LEN)) ||
Auke Kok9e2feac2006-04-14 19:05:18 -07003577 (max_frame == MAXIMUM_ETHERNET_VLAN_SIZE)))
3578 adapter->rx_buffer_len = MAXIMUM_ETHERNET_VLAN_SIZE;
Jeff Kirsher997f5cb2006-01-12 16:50:55 -08003579
Emil Tantilov675ad472010-04-27 14:02:58 +00003580 pr_info("%s changing MTU from %d to %d\n",
3581 netdev->name, netdev->mtu, new_mtu);
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003582 netdev->mtu = new_mtu;
3583
Auke Kok2db10a02006-06-27 09:06:28 -07003584 if (netif_running(netdev))
Jesse Brandeburg3d6114e2009-09-25 12:19:02 +00003585 e1000_up(adapter);
3586 else
3587 e1000_reset(adapter);
3588
3589 clear_bit(__E1000_RESETTING, &adapter->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591 return 0;
3592}
3593
3594/**
3595 * e1000_update_stats - Update the board statistics counters
3596 * @adapter: board private structure
3597 **/
Joe Perches64798842008-07-11 15:17:02 -07003598void e1000_update_stats(struct e1000_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599{
Ajit Khaparde5fe31de2009-10-07 02:42:23 +00003600 struct net_device *netdev = adapter->netdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601 struct e1000_hw *hw = &adapter->hw;
Linas Vepstas282f33c2006-06-08 22:19:44 -07003602 struct pci_dev *pdev = adapter->pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603 unsigned long flags;
Joe Perches406874a2008-04-03 10:06:32 -07003604 u16 phy_tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605
3606#define PHY_IDLE_ERROR_COUNT_MASK 0x00FF
3607
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00003608 /* Prevent stats update while adapter is being reset, or if the pci
Linas Vepstas282f33c2006-06-08 22:19:44 -07003609 * connection is down.
3610 */
Auke Kok90267292006-06-08 09:30:24 -07003611 if (adapter->link_speed == 0)
3612 return;
Linas Vepstas81b19552006-12-12 18:29:15 -06003613 if (pci_channel_offline(pdev))
Linas Vepstas282f33c2006-06-08 22:19:44 -07003614 return;
Auke Kok90267292006-06-08 09:30:24 -07003615
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616 spin_lock_irqsave(&adapter->stats_lock, flags);
3617
Masatake YAMATO828d0552007-10-20 03:06:37 +02003618 /* these counters are modified from e1000_tbi_adjust_stats,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619 * called from the interrupt context, so they must only
3620 * be written while holding adapter->stats_lock
3621 */
3622
Joe Perches1dc32912008-07-11 15:17:08 -07003623 adapter->stats.crcerrs += er32(CRCERRS);
3624 adapter->stats.gprc += er32(GPRC);
3625 adapter->stats.gorcl += er32(GORCL);
3626 adapter->stats.gorch += er32(GORCH);
3627 adapter->stats.bprc += er32(BPRC);
3628 adapter->stats.mprc += er32(MPRC);
3629 adapter->stats.roc += er32(ROC);
Auke Kokcd94dd02006-06-27 09:08:22 -07003630
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00003631 adapter->stats.prc64 += er32(PRC64);
3632 adapter->stats.prc127 += er32(PRC127);
3633 adapter->stats.prc255 += er32(PRC255);
3634 adapter->stats.prc511 += er32(PRC511);
3635 adapter->stats.prc1023 += er32(PRC1023);
3636 adapter->stats.prc1522 += er32(PRC1522);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637
Joe Perches1dc32912008-07-11 15:17:08 -07003638 adapter->stats.symerrs += er32(SYMERRS);
3639 adapter->stats.mpc += er32(MPC);
3640 adapter->stats.scc += er32(SCC);
3641 adapter->stats.ecol += er32(ECOL);
3642 adapter->stats.mcc += er32(MCC);
3643 adapter->stats.latecol += er32(LATECOL);
3644 adapter->stats.dc += er32(DC);
3645 adapter->stats.sec += er32(SEC);
3646 adapter->stats.rlec += er32(RLEC);
3647 adapter->stats.xonrxc += er32(XONRXC);
3648 adapter->stats.xontxc += er32(XONTXC);
3649 adapter->stats.xoffrxc += er32(XOFFRXC);
3650 adapter->stats.xofftxc += er32(XOFFTXC);
3651 adapter->stats.fcruc += er32(FCRUC);
3652 adapter->stats.gptc += er32(GPTC);
3653 adapter->stats.gotcl += er32(GOTCL);
3654 adapter->stats.gotch += er32(GOTCH);
3655 adapter->stats.rnbc += er32(RNBC);
3656 adapter->stats.ruc += er32(RUC);
3657 adapter->stats.rfc += er32(RFC);
3658 adapter->stats.rjc += er32(RJC);
3659 adapter->stats.torl += er32(TORL);
3660 adapter->stats.torh += er32(TORH);
3661 adapter->stats.totl += er32(TOTL);
3662 adapter->stats.toth += er32(TOTH);
3663 adapter->stats.tpr += er32(TPR);
Auke Kokcd94dd02006-06-27 09:08:22 -07003664
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00003665 adapter->stats.ptc64 += er32(PTC64);
3666 adapter->stats.ptc127 += er32(PTC127);
3667 adapter->stats.ptc255 += er32(PTC255);
3668 adapter->stats.ptc511 += er32(PTC511);
3669 adapter->stats.ptc1023 += er32(PTC1023);
3670 adapter->stats.ptc1522 += er32(PTC1522);
Auke Kokcd94dd02006-06-27 09:08:22 -07003671
Joe Perches1dc32912008-07-11 15:17:08 -07003672 adapter->stats.mptc += er32(MPTC);
3673 adapter->stats.bptc += er32(BPTC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674
3675 /* used for adaptive IFS */
3676
Joe Perches1dc32912008-07-11 15:17:08 -07003677 hw->tx_packet_delta = er32(TPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678 adapter->stats.tpt += hw->tx_packet_delta;
Joe Perches1dc32912008-07-11 15:17:08 -07003679 hw->collision_delta = er32(COLC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680 adapter->stats.colc += hw->collision_delta;
3681
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003682 if (hw->mac_type >= e1000_82543) {
Joe Perches1dc32912008-07-11 15:17:08 -07003683 adapter->stats.algnerrc += er32(ALGNERRC);
3684 adapter->stats.rxerrc += er32(RXERRC);
3685 adapter->stats.tncrs += er32(TNCRS);
3686 adapter->stats.cexterr += er32(CEXTERR);
3687 adapter->stats.tsctc += er32(TSCTC);
3688 adapter->stats.tsctfc += er32(TSCTFC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689 }
3690
3691 /* Fill out the OS statistics structure */
Ajit Khaparde5fe31de2009-10-07 02:42:23 +00003692 netdev->stats.multicast = adapter->stats.mprc;
3693 netdev->stats.collisions = adapter->stats.colc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694
3695 /* Rx Errors */
3696
Jeff Kirsher87041632006-03-02 18:21:24 -08003697 /* RLEC on some newer hardware can be incorrect so build
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00003698 * our own version based on RUC and ROC
3699 */
Ajit Khaparde5fe31de2009-10-07 02:42:23 +00003700 netdev->stats.rx_errors = adapter->stats.rxerrc +
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701 adapter->stats.crcerrs + adapter->stats.algnerrc +
Jeff Kirsher87041632006-03-02 18:21:24 -08003702 adapter->stats.ruc + adapter->stats.roc +
3703 adapter->stats.cexterr;
Mitch Williams49559852006-09-27 12:53:37 -07003704 adapter->stats.rlerrc = adapter->stats.ruc + adapter->stats.roc;
Ajit Khaparde5fe31de2009-10-07 02:42:23 +00003705 netdev->stats.rx_length_errors = adapter->stats.rlerrc;
3706 netdev->stats.rx_crc_errors = adapter->stats.crcerrs;
3707 netdev->stats.rx_frame_errors = adapter->stats.algnerrc;
3708 netdev->stats.rx_missed_errors = adapter->stats.mpc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709
3710 /* Tx Errors */
Mitch Williams49559852006-09-27 12:53:37 -07003711 adapter->stats.txerrc = adapter->stats.ecol + adapter->stats.latecol;
Ajit Khaparde5fe31de2009-10-07 02:42:23 +00003712 netdev->stats.tx_errors = adapter->stats.txerrc;
3713 netdev->stats.tx_aborted_errors = adapter->stats.ecol;
3714 netdev->stats.tx_window_errors = adapter->stats.latecol;
3715 netdev->stats.tx_carrier_errors = adapter->stats.tncrs;
Joe Perches1dc32912008-07-11 15:17:08 -07003716 if (hw->bad_tx_carr_stats_fd &&
Jeff Garzik167fb282006-12-15 10:41:15 -05003717 adapter->link_duplex == FULL_DUPLEX) {
Ajit Khaparde5fe31de2009-10-07 02:42:23 +00003718 netdev->stats.tx_carrier_errors = 0;
Jeff Garzik167fb282006-12-15 10:41:15 -05003719 adapter->stats.tncrs = 0;
3720 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721
3722 /* Tx Dropped needs to be maintained elsewhere */
3723
3724 /* Phy Stats */
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003725 if (hw->media_type == e1000_media_type_copper) {
3726 if ((adapter->link_speed == SPEED_1000) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727 (!e1000_read_phy_reg(hw, PHY_1000T_STATUS, &phy_tmp))) {
3728 phy_tmp &= PHY_IDLE_ERROR_COUNT_MASK;
3729 adapter->phy_stats.idle_errors += phy_tmp;
3730 }
3731
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003732 if ((hw->mac_type <= e1000_82546) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733 (hw->phy_type == e1000_phy_m88) &&
3734 !e1000_read_phy_reg(hw, M88E1000_RX_ERR_CNTR, &phy_tmp))
3735 adapter->phy_stats.receive_errors += phy_tmp;
3736 }
3737
Jeff Garzik15e376b2006-12-15 11:16:33 -05003738 /* Management Stats */
Joe Perches1dc32912008-07-11 15:17:08 -07003739 if (hw->has_smbus) {
3740 adapter->stats.mgptc += er32(MGTPTC);
3741 adapter->stats.mgprc += er32(MGTPRC);
3742 adapter->stats.mgpdc += er32(MGTPDC);
Jeff Garzik15e376b2006-12-15 11:16:33 -05003743 }
3744
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745 spin_unlock_irqrestore(&adapter->stats_lock, flags);
3746}
Jesse Brandeburg9ac98282006-11-01 08:48:10 -08003747
3748/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749 * e1000_intr - Interrupt Handler
3750 * @irq: interrupt number
3751 * @data: pointer to a network interface device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752 **/
Joe Perches64798842008-07-11 15:17:02 -07003753static irqreturn_t e1000_intr(int irq, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754{
3755 struct net_device *netdev = data;
Malli Chilakala60490fe2005-06-17 17:41:45 -07003756 struct e1000_adapter *adapter = netdev_priv(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757 struct e1000_hw *hw = &adapter->hw;
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00003758 u32 icr = er32(ICR);
Francois Romieuc3570ac2008-07-11 15:17:38 -07003759
Jesse Brandeburg4c11b8a2011-01-13 07:48:13 +00003760 if (unlikely((!icr)))
Jesse Brandeburg835bb122006-11-01 08:48:13 -08003761 return IRQ_NONE; /* Not our interrupt */
3762
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00003763 /* we might have caused the interrupt, but the above
Jesse Brandeburg4c11b8a2011-01-13 07:48:13 +00003764 * read cleared it, and just in case the driver is
3765 * down there is nothing to do so return handled
3766 */
3767 if (unlikely(test_bit(__E1000_DOWN, &adapter->flags)))
3768 return IRQ_HANDLED;
3769
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003770 if (unlikely(icr & (E1000_ICR_RXSEQ | E1000_ICR_LSC))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771 hw->get_link_status = 1;
Auke Kok1314bbf2006-09-27 12:54:02 -07003772 /* guard against interrupt when we're going down */
3773 if (!test_bit(__E1000_DOWN, &adapter->flags))
Jesse Brandeburga4010af2011-10-05 07:24:41 +00003774 schedule_delayed_work(&adapter->watchdog_task, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775 }
3776
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00003777 /* disable interrupts, without the synchronize_irq bit */
3778 ew32(IMC, ~0);
3779 E1000_WRITE_FLUSH();
3780
Ben Hutchings288379f2009-01-19 16:43:59 -08003781 if (likely(napi_schedule_prep(&adapter->napi))) {
Jesse Brandeburg835bb122006-11-01 08:48:13 -08003782 adapter->total_tx_bytes = 0;
3783 adapter->total_tx_packets = 0;
3784 adapter->total_rx_bytes = 0;
3785 adapter->total_rx_packets = 0;
Ben Hutchings288379f2009-01-19 16:43:59 -08003786 __napi_schedule(&adapter->napi);
Jesse Brandeburga6c42322009-03-25 21:59:22 +00003787 } else {
Auke Kok90fb5132006-11-01 08:47:30 -08003788 /* this really should not happen! if it does it is basically a
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00003789 * bug, but not a hard error, so enable ints and continue
3790 */
Jesse Brandeburga6c42322009-03-25 21:59:22 +00003791 if (!test_bit(__E1000_DOWN, &adapter->flags))
3792 e1000_irq_enable(adapter);
3793 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795 return IRQ_HANDLED;
3796}
3797
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798/**
3799 * e1000_clean - NAPI Rx polling callback
3800 * @adapter: board private structure
3801 **/
Joe Perches64798842008-07-11 15:17:02 -07003802static int e1000_clean(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803{
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00003804 struct e1000_adapter *adapter = container_of(napi, struct e1000_adapter,
3805 napi);
Jesse Brandeburg650b5a52009-09-25 12:19:23 +00003806 int tx_clean_complete = 0, work_done = 0;
Malli Chilakala26483452005-04-28 19:44:46 -07003807
Jesse Brandeburg650b5a52009-09-25 12:19:23 +00003808 tx_clean_complete = e1000_clean_tx_irq(adapter, &adapter->tx_ring[0]);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003809
Jesse Brandeburg650b5a52009-09-25 12:19:23 +00003810 adapter->clean_rx(adapter, &adapter->rx_ring[0], &work_done, budget);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003811
Jesse Brandeburg650b5a52009-09-25 12:19:23 +00003812 if (!tx_clean_complete)
David S. Millerd2c7ddd2008-01-15 22:43:24 -08003813 work_done = budget;
3814
David S. Miller53e52c72008-01-07 21:06:12 -08003815 /* If budget not fully consumed, exit the polling mode */
3816 if (work_done < budget) {
Jesse Brandeburg835bb122006-11-01 08:48:13 -08003817 if (likely(adapter->itr_setting & 3))
3818 e1000_set_itr(adapter);
Ben Hutchings288379f2009-01-19 16:43:59 -08003819 napi_complete(napi);
Jesse Brandeburga6c42322009-03-25 21:59:22 +00003820 if (!test_bit(__E1000_DOWN, &adapter->flags))
3821 e1000_irq_enable(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822 }
3823
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003824 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825}
3826
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827/**
3828 * e1000_clean_tx_irq - Reclaim resources after transmit completes
3829 * @adapter: board private structure
3830 **/
Joe Perches64798842008-07-11 15:17:02 -07003831static bool e1000_clean_tx_irq(struct e1000_adapter *adapter,
3832 struct e1000_tx_ring *tx_ring)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833{
Joe Perches1dc32912008-07-11 15:17:08 -07003834 struct e1000_hw *hw = &adapter->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835 struct net_device *netdev = adapter->netdev;
3836 struct e1000_tx_desc *tx_desc, *eop_desc;
3837 struct e1000_buffer *buffer_info;
3838 unsigned int i, eop;
Jeff Kirsher2a1af5d2006-03-02 18:20:43 -08003839 unsigned int count = 0;
Jesse Brandeburg835bb122006-11-01 08:48:13 -08003840 unsigned int total_tx_bytes=0, total_tx_packets=0;
Otto Estuardo Solares Cabrera2f66fd32012-08-24 08:04:58 +00003841 unsigned int bytes_compl = 0, pkts_compl = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842
3843 i = tx_ring->next_to_clean;
3844 eop = tx_ring->buffer_info[i].next_to_watch;
3845 eop_desc = E1000_TX_DESC(*tx_ring, eop);
3846
Alexander Duyckccfb3422009-03-25 21:59:04 +00003847 while ((eop_desc->upper.data & cpu_to_le32(E1000_TXD_STAT_DD)) &&
3848 (count < tx_ring->count)) {
Jesse Brandeburg843f4262009-04-16 16:59:47 +00003849 bool cleaned = false;
Jeff Kirsher2d0bb1c2010-08-08 16:02:31 +00003850 rmb(); /* read buffer_info after eop_desc */
Jesse Brandeburg843f4262009-04-16 16:59:47 +00003851 for ( ; !cleaned; count++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852 tx_desc = E1000_TX_DESC(*tx_ring, i);
3853 buffer_info = &tx_ring->buffer_info[i];
3854 cleaned = (i == eop);
3855
Jesse Brandeburg835bb122006-11-01 08:48:13 -08003856 if (cleaned) {
Dean Nelson31c15a22011-08-25 14:39:24 +00003857 total_tx_packets += buffer_info->segs;
3858 total_tx_bytes += buffer_info->bytecount;
Otto Estuardo Solares Cabrera2f66fd32012-08-24 08:04:58 +00003859 if (buffer_info->skb) {
3860 bytes_compl += buffer_info->skb->len;
3861 pkts_compl++;
3862 }
3863
Jesse Brandeburg835bb122006-11-01 08:48:13 -08003864 }
Jeff Kirsherfd803242005-12-13 00:06:22 -05003865 e1000_unmap_and_free_tx_resource(adapter, buffer_info);
Jesse Brandeburga9ebadd2006-11-01 08:47:53 -08003866 tx_desc->upper.data = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003868 if (unlikely(++i == tx_ring->count)) i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869 }
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003870
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871 eop = tx_ring->buffer_info[i].next_to_watch;
3872 eop_desc = E1000_TX_DESC(*tx_ring, eop);
3873 }
3874
3875 tx_ring->next_to_clean = i;
3876
Otto Estuardo Solares Cabrera2f66fd32012-08-24 08:04:58 +00003877 netdev_completed_queue(netdev, pkts_compl, bytes_compl);
3878
Auke Kok77b2aad2006-04-14 19:05:25 -07003879#define TX_WAKE_THRESHOLD 32
Jesse Brandeburg843f4262009-04-16 16:59:47 +00003880 if (unlikely(count && netif_carrier_ok(netdev) &&
Jesse Brandeburg65c79732006-09-27 12:53:48 -07003881 E1000_DESC_UNUSED(tx_ring) >= TX_WAKE_THRESHOLD)) {
3882 /* Make sure that anybody stopping the queue after this
3883 * sees the new next_to_clean.
3884 */
3885 smp_mb();
Jesse Brandeburgcdd75492009-09-25 12:18:07 +00003886
3887 if (netif_queue_stopped(netdev) &&
3888 !(test_bit(__E1000_DOWN, &adapter->flags))) {
Auke Kok77b2aad2006-04-14 19:05:25 -07003889 netif_wake_queue(netdev);
Jesse Brandeburgfcfb1222006-11-01 08:47:59 -08003890 ++adapter->restart_queue;
3891 }
Auke Kok77b2aad2006-04-14 19:05:25 -07003892 }
Malli Chilakala26483452005-04-28 19:44:46 -07003893
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003894 if (adapter->detect_tx_hung) {
Malli Chilakala26483452005-04-28 19:44:46 -07003895 /* Detect a transmit hang in hardware, this serializes the
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00003896 * check with the clearing of time_stamp and movement of i
3897 */
Joe Perchesc3033b02008-03-21 11:06:25 -07003898 adapter->detect_tx_hung = false;
Jesse Brandeburgcdd75492009-09-25 12:18:07 +00003899 if (tx_ring->buffer_info[eop].time_stamp &&
3900 time_after(jiffies, tx_ring->buffer_info[eop].time_stamp +
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00003901 (adapter->tx_timeout_factor * HZ)) &&
Joe Perches8e95a202009-12-03 07:58:21 +00003902 !(er32(STATUS) & E1000_STATUS_TXOFF)) {
Malli Chilakala70b8f1e2005-04-28 19:40:40 -07003903
3904 /* detected Tx unit hang */
Emil Tantilovfeb8f472010-07-26 23:37:21 -07003905 e_err(drv, "Detected Tx Unit Hang\n"
Emil Tantilov675ad472010-04-27 14:02:58 +00003906 " Tx Queue <%lu>\n"
3907 " TDH <%x>\n"
3908 " TDT <%x>\n"
3909 " next_to_use <%x>\n"
3910 " next_to_clean <%x>\n"
3911 "buffer_info[next_to_clean]\n"
3912 " time_stamp <%lx>\n"
3913 " next_to_watch <%x>\n"
3914 " jiffies <%lx>\n"
3915 " next_to_watch.status <%x>\n",
Jeff Kirsher7bfa4812006-01-12 16:50:41 -08003916 (unsigned long)((tx_ring - adapter->tx_ring) /
3917 sizeof(struct e1000_tx_ring)),
Joe Perches1dc32912008-07-11 15:17:08 -07003918 readl(hw->hw_addr + tx_ring->tdh),
3919 readl(hw->hw_addr + tx_ring->tdt),
Malli Chilakala70b8f1e2005-04-28 19:40:40 -07003920 tx_ring->next_to_use,
Jeff Kirsher392137f2006-01-12 16:50:57 -08003921 tx_ring->next_to_clean,
Jesse Brandeburgcdd75492009-09-25 12:18:07 +00003922 tx_ring->buffer_info[eop].time_stamp,
Malli Chilakala70b8f1e2005-04-28 19:40:40 -07003923 eop,
3924 jiffies,
3925 eop_desc->upper.fields.status);
Tushar Daveb04e36b2012-01-27 09:00:46 +00003926 e1000_dump(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927 netif_stop_queue(netdev);
Malli Chilakala70b8f1e2005-04-28 19:40:40 -07003928 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929 }
Jesse Brandeburg835bb122006-11-01 08:48:13 -08003930 adapter->total_tx_bytes += total_tx_bytes;
3931 adapter->total_tx_packets += total_tx_packets;
Ajit Khaparde5fe31de2009-10-07 02:42:23 +00003932 netdev->stats.tx_bytes += total_tx_bytes;
3933 netdev->stats.tx_packets += total_tx_packets;
Eric Dumazet807540b2010-09-23 05:40:09 +00003934 return count < tx_ring->count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935}
3936
3937/**
3938 * e1000_rx_checksum - Receive Checksum Offload for 82543
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003939 * @adapter: board private structure
3940 * @status_err: receive descriptor status and error fields
3941 * @csum: receive descriptor csum field
3942 * @sk_buff: socket buffer with received data
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943 **/
Joe Perches64798842008-07-11 15:17:02 -07003944static void e1000_rx_checksum(struct e1000_adapter *adapter, u32 status_err,
3945 u32 csum, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946{
Joe Perches1dc32912008-07-11 15:17:08 -07003947 struct e1000_hw *hw = &adapter->hw;
Joe Perches406874a2008-04-03 10:06:32 -07003948 u16 status = (u16)status_err;
3949 u8 errors = (u8)(status_err >> 24);
Eric Dumazetbc8acf22010-09-02 13:07:41 -07003950
3951 skb_checksum_none_assert(skb);
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003952
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953 /* 82543 or newer only */
Joe Perches1dc32912008-07-11 15:17:08 -07003954 if (unlikely(hw->mac_type < e1000_82543)) return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955 /* Ignore Checksum bit is set */
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003956 if (unlikely(status & E1000_RXD_STAT_IXSM)) return;
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003957 /* TCP/UDP checksum error bit is set */
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003958 if (unlikely(errors & E1000_RXD_ERR_TCPE)) {
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003959 /* let the stack verify checksum errors */
3960 adapter->hw_csum_err++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961 return;
3962 }
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003963 /* TCP/UDP Checksum has not been calculated */
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00003964 if (!(status & E1000_RXD_STAT_TCPCS))
3965 return;
3966
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003967 /* It must be a TCP or UDP packet with a valid checksum */
3968 if (likely(status & E1000_RXD_STAT_TCPCS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 /* TCP checksum is good */
3970 skb->ip_summed = CHECKSUM_UNNECESSARY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971 }
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003972 adapter->hw_csum_good++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973}
3974
3975/**
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00003976 * e1000_consume_page - helper function
3977 **/
3978static void e1000_consume_page(struct e1000_buffer *bi, struct sk_buff *skb,
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00003979 u16 length)
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00003980{
3981 bi->page = NULL;
3982 skb->len += length;
3983 skb->data_len += length;
Eric Dumazeted64b3c2011-10-13 07:53:42 +00003984 skb->truesize += PAGE_SIZE;
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00003985}
3986
3987/**
3988 * e1000_receive_skb - helper function to handle rx indications
3989 * @adapter: board private structure
3990 * @status: descriptor status field as written by hardware
3991 * @vlan: descriptor vlan field as written by hardware (no le/be conversion)
3992 * @skb: pointer to sk_buff to be indicated to stack
3993 */
3994static void e1000_receive_skb(struct e1000_adapter *adapter, u8 status,
3995 __le16 vlan, struct sk_buff *skb)
3996{
Jesse Brandeburg6a08d192010-09-22 18:23:05 +00003997 skb->protocol = eth_type_trans(skb, adapter->netdev);
3998
Jiri Pirko5622e402011-07-21 03:26:31 +00003999 if (status & E1000_RXD_STAT_VP) {
4000 u16 vid = le16_to_cpu(vlan) & E1000_RXD_SPC_VLAN_MASK;
4001
4002 __vlan_hwaccel_put_tag(skb, vid);
4003 }
4004 napi_gro_receive(&adapter->napi, skb);
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004005}
4006
4007/**
4008 * e1000_clean_jumbo_rx_irq - Send received data up the network stack; legacy
4009 * @adapter: board private structure
4010 * @rx_ring: ring to clean
4011 * @work_done: amount of napi work completed this call
4012 * @work_to_do: max amount of work allowed for this call to do
4013 *
4014 * the return value indicates whether actual cleaning was done, there
4015 * is no guarantee that everything was cleaned
4016 */
4017static bool e1000_clean_jumbo_rx_irq(struct e1000_adapter *adapter,
4018 struct e1000_rx_ring *rx_ring,
4019 int *work_done, int work_to_do)
4020{
4021 struct e1000_hw *hw = &adapter->hw;
4022 struct net_device *netdev = adapter->netdev;
4023 struct pci_dev *pdev = adapter->pdev;
4024 struct e1000_rx_desc *rx_desc, *next_rxd;
4025 struct e1000_buffer *buffer_info, *next_buffer;
4026 unsigned long irq_flags;
4027 u32 length;
4028 unsigned int i;
4029 int cleaned_count = 0;
4030 bool cleaned = false;
4031 unsigned int total_rx_bytes=0, total_rx_packets=0;
4032
4033 i = rx_ring->next_to_clean;
4034 rx_desc = E1000_RX_DESC(*rx_ring, i);
4035 buffer_info = &rx_ring->buffer_info[i];
4036
4037 while (rx_desc->status & E1000_RXD_STAT_DD) {
4038 struct sk_buff *skb;
4039 u8 status;
4040
4041 if (*work_done >= work_to_do)
4042 break;
4043 (*work_done)++;
Jeff Kirsher2d0bb1c2010-08-08 16:02:31 +00004044 rmb(); /* read descriptor and rx_buffer_info after status DD */
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004045
4046 status = rx_desc->status;
4047 skb = buffer_info->skb;
4048 buffer_info->skb = NULL;
4049
4050 if (++i == rx_ring->count) i = 0;
4051 next_rxd = E1000_RX_DESC(*rx_ring, i);
4052 prefetch(next_rxd);
4053
4054 next_buffer = &rx_ring->buffer_info[i];
4055
4056 cleaned = true;
4057 cleaned_count++;
Nick Nunleyb16f53b2010-04-27 13:08:45 +00004058 dma_unmap_page(&pdev->dev, buffer_info->dma,
4059 buffer_info->length, DMA_FROM_DEVICE);
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004060 buffer_info->dma = 0;
4061
4062 length = le16_to_cpu(rx_desc->length);
4063
4064 /* errors is only valid for DD + EOP descriptors */
4065 if (unlikely((status & E1000_RXD_STAT_EOP) &&
4066 (rx_desc->errors & E1000_RXD_ERR_FRAME_ERR_MASK))) {
Sebastian Andrzej Siewiora3060852012-05-11 16:30:46 +00004067 u8 *mapped;
4068 u8 last_byte;
4069
4070 mapped = page_address(buffer_info->page);
4071 last_byte = *(mapped + length - 1);
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004072 if (TBI_ACCEPT(hw, status, rx_desc->errors, length,
4073 last_byte)) {
4074 spin_lock_irqsave(&adapter->stats_lock,
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00004075 irq_flags);
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004076 e1000_tbi_adjust_stats(hw, &adapter->stats,
Sebastian Andrzej Siewior281a8f22012-05-15 09:18:55 +00004077 length, mapped);
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004078 spin_unlock_irqrestore(&adapter->stats_lock,
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00004079 irq_flags);
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004080 length--;
4081 } else {
Ben Greeare825b732012-04-04 06:01:29 +00004082 if (netdev->features & NETIF_F_RXALL)
4083 goto process_skb;
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004084 /* recycle both page and skb */
4085 buffer_info->skb = skb;
4086 /* an error means any chain goes out the window
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00004087 * too
4088 */
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004089 if (rx_ring->rx_skb_top)
4090 dev_kfree_skb(rx_ring->rx_skb_top);
4091 rx_ring->rx_skb_top = NULL;
4092 goto next_desc;
4093 }
4094 }
4095
4096#define rxtop rx_ring->rx_skb_top
Ben Greeare825b732012-04-04 06:01:29 +00004097process_skb:
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004098 if (!(status & E1000_RXD_STAT_EOP)) {
4099 /* this descriptor is only the beginning (or middle) */
4100 if (!rxtop) {
4101 /* this is the beginning of a chain */
4102 rxtop = skb;
4103 skb_fill_page_desc(rxtop, 0, buffer_info->page,
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00004104 0, length);
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004105 } else {
4106 /* this is the middle of a chain */
4107 skb_fill_page_desc(rxtop,
4108 skb_shinfo(rxtop)->nr_frags,
4109 buffer_info->page, 0, length);
4110 /* re-use the skb, only consumed the page */
4111 buffer_info->skb = skb;
4112 }
4113 e1000_consume_page(buffer_info, rxtop, length);
4114 goto next_desc;
4115 } else {
4116 if (rxtop) {
4117 /* end of the chain */
4118 skb_fill_page_desc(rxtop,
4119 skb_shinfo(rxtop)->nr_frags,
4120 buffer_info->page, 0, length);
4121 /* re-use the current skb, we only consumed the
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00004122 * page
4123 */
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004124 buffer_info->skb = skb;
4125 skb = rxtop;
4126 rxtop = NULL;
4127 e1000_consume_page(buffer_info, skb, length);
4128 } else {
4129 /* no chain, got EOP, this buf is the packet
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00004130 * copybreak to save the put_page/alloc_page
4131 */
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004132 if (length <= copybreak &&
4133 skb_tailroom(skb) >= length) {
4134 u8 *vaddr;
Cong Wang46790262011-11-25 23:14:23 +08004135 vaddr = kmap_atomic(buffer_info->page);
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00004136 memcpy(skb_tail_pointer(skb), vaddr,
4137 length);
Cong Wang46790262011-11-25 23:14:23 +08004138 kunmap_atomic(vaddr);
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004139 /* re-use the page, so don't erase
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00004140 * buffer_info->page
4141 */
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004142 skb_put(skb, length);
4143 } else {
4144 skb_fill_page_desc(skb, 0,
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00004145 buffer_info->page, 0,
4146 length);
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004147 e1000_consume_page(buffer_info, skb,
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00004148 length);
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004149 }
4150 }
4151 }
4152
4153 /* Receive Checksum Offload XXX recompute due to CRC strip? */
4154 e1000_rx_checksum(adapter,
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00004155 (u32)(status) |
4156 ((u32)(rx_desc->errors) << 24),
4157 le16_to_cpu(rx_desc->csum), skb);
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004158
Ben Greearb0d15622012-02-11 15:40:11 +00004159 total_rx_bytes += (skb->len - 4); /* don't count FCS */
4160 if (likely(!(netdev->features & NETIF_F_RXFCS)))
4161 pskb_trim(skb, skb->len - 4);
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004162 total_rx_packets++;
4163
4164 /* eth type trans needs skb->data to point to something */
4165 if (!pskb_may_pull(skb, ETH_HLEN)) {
Emil Tantilovfeb8f472010-07-26 23:37:21 -07004166 e_err(drv, "pskb_may_pull failed.\n");
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004167 dev_kfree_skb(skb);
4168 goto next_desc;
4169 }
4170
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004171 e1000_receive_skb(adapter, status, rx_desc->special, skb);
4172
4173next_desc:
4174 rx_desc->status = 0;
4175
4176 /* return some buffers to hardware, one at a time is too slow */
4177 if (unlikely(cleaned_count >= E1000_RX_BUFFER_WRITE)) {
4178 adapter->alloc_rx_buf(adapter, rx_ring, cleaned_count);
4179 cleaned_count = 0;
4180 }
4181
4182 /* use prefetched values */
4183 rx_desc = next_rxd;
4184 buffer_info = next_buffer;
4185 }
4186 rx_ring->next_to_clean = i;
4187
4188 cleaned_count = E1000_DESC_UNUSED(rx_ring);
4189 if (cleaned_count)
4190 adapter->alloc_rx_buf(adapter, rx_ring, cleaned_count);
4191
4192 adapter->total_rx_packets += total_rx_packets;
4193 adapter->total_rx_bytes += total_rx_bytes;
Ajit Khaparde5fe31de2009-10-07 02:42:23 +00004194 netdev->stats.rx_bytes += total_rx_bytes;
4195 netdev->stats.rx_packets += total_rx_packets;
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004196 return cleaned;
4197}
4198
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00004199/* this should improve performance for small packets with large amounts
Joe Perches57bf6ee2010-05-13 15:26:17 +00004200 * of reassembly being done in the stack
4201 */
4202static void e1000_check_copybreak(struct net_device *netdev,
4203 struct e1000_buffer *buffer_info,
4204 u32 length, struct sk_buff **skb)
4205{
4206 struct sk_buff *new_skb;
4207
4208 if (length > copybreak)
4209 return;
4210
4211 new_skb = netdev_alloc_skb_ip_align(netdev, length);
4212 if (!new_skb)
4213 return;
4214
4215 skb_copy_to_linear_data_offset(new_skb, -NET_IP_ALIGN,
4216 (*skb)->data - NET_IP_ALIGN,
4217 length + NET_IP_ALIGN);
4218 /* save the skb in buffer_info as good */
4219 buffer_info->skb = *skb;
4220 *skb = new_skb;
4221}
4222
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004223/**
Malli Chilakala2d7edb92005-04-28 19:43:52 -07004224 * e1000_clean_rx_irq - Send received data up the network stack; legacy
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225 * @adapter: board private structure
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004226 * @rx_ring: ring to clean
4227 * @work_done: amount of napi work completed this call
4228 * @work_to_do: max amount of work allowed for this call to do
4229 */
Joe Perches64798842008-07-11 15:17:02 -07004230static bool e1000_clean_rx_irq(struct e1000_adapter *adapter,
4231 struct e1000_rx_ring *rx_ring,
4232 int *work_done, int work_to_do)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233{
Joe Perches1dc32912008-07-11 15:17:08 -07004234 struct e1000_hw *hw = &adapter->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235 struct net_device *netdev = adapter->netdev;
4236 struct pci_dev *pdev = adapter->pdev;
Jesse Brandeburg86c3d592006-01-18 13:01:43 -08004237 struct e1000_rx_desc *rx_desc, *next_rxd;
4238 struct e1000_buffer *buffer_info, *next_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239 unsigned long flags;
Joe Perches406874a2008-04-03 10:06:32 -07004240 u32 length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241 unsigned int i;
Jeff Kirsher72d64a42006-01-12 16:51:19 -08004242 int cleaned_count = 0;
Joe Perchesc3033b02008-03-21 11:06:25 -07004243 bool cleaned = false;
Jesse Brandeburg835bb122006-11-01 08:48:13 -08004244 unsigned int total_rx_bytes=0, total_rx_packets=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245
4246 i = rx_ring->next_to_clean;
4247 rx_desc = E1000_RX_DESC(*rx_ring, i);
Jesse Brandeburgb92ff8e2006-01-18 13:01:32 -08004248 buffer_info = &rx_ring->buffer_info[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249
Jesse Brandeburgb92ff8e2006-01-18 13:01:32 -08004250 while (rx_desc->status & E1000_RXD_STAT_DD) {
Auke Kok24f476e2006-06-08 09:28:47 -07004251 struct sk_buff *skb;
Jeff Kirshera292ca62006-01-12 16:51:30 -08004252 u8 status;
Auke Kok90fb5132006-11-01 08:47:30 -08004253
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004254 if (*work_done >= work_to_do)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255 break;
4256 (*work_done)++;
Jeff Kirsher2d0bb1c2010-08-08 16:02:31 +00004257 rmb(); /* read descriptor and rx_buffer_info after status DD */
Francois Romieuc3570ac2008-07-11 15:17:38 -07004258
Jeff Kirshera292ca62006-01-12 16:51:30 -08004259 status = rx_desc->status;
Jesse Brandeburgb92ff8e2006-01-18 13:01:32 -08004260 skb = buffer_info->skb;
Jesse Brandeburg86c3d592006-01-18 13:01:43 -08004261 buffer_info->skb = NULL;
4262
Jeff Kirsher30320be2006-03-02 18:21:57 -08004263 prefetch(skb->data - NET_IP_ALIGN);
4264
Jesse Brandeburg86c3d592006-01-18 13:01:43 -08004265 if (++i == rx_ring->count) i = 0;
4266 next_rxd = E1000_RX_DESC(*rx_ring, i);
Jeff Kirsher30320be2006-03-02 18:21:57 -08004267 prefetch(next_rxd);
4268
Jesse Brandeburg86c3d592006-01-18 13:01:43 -08004269 next_buffer = &rx_ring->buffer_info[i];
Jesse Brandeburg86c3d592006-01-18 13:01:43 -08004270
Joe Perchesc3033b02008-03-21 11:06:25 -07004271 cleaned = true;
Jeff Kirsher72d64a42006-01-12 16:51:19 -08004272 cleaned_count++;
Nick Nunleyb16f53b2010-04-27 13:08:45 +00004273 dma_unmap_single(&pdev->dev, buffer_info->dma,
4274 buffer_info->length, DMA_FROM_DEVICE);
Jesse Brandeburg679be3b2009-06-30 12:45:34 +00004275 buffer_info->dma = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277 length = le16_to_cpu(rx_desc->length);
Neil Hormanea30e112009-06-02 01:29:58 -07004278 /* !EOP means multiple descriptors were used to store a single
Jesse Brandeburg40a14de2010-01-19 14:15:38 +00004279 * packet, if thats the case we need to toss it. In fact, we
4280 * to toss every packet with the EOP bit clear and the next
4281 * frame that _does_ have the EOP bit set, as it is by
4282 * definition only a frame fragment
4283 */
4284 if (unlikely(!(status & E1000_RXD_STAT_EOP)))
4285 adapter->discarding = true;
4286
4287 if (adapter->discarding) {
Jeff Kirshera1415ee2006-02-28 20:24:07 -08004288 /* All receives must fit into a single buffer */
Emil Tantilovfeb8f472010-07-26 23:37:21 -07004289 e_dbg("Receive packet consumed multiple buffers\n");
Auke Kok864c4e42006-06-27 09:06:53 -07004290 /* recycle */
Auke Kok8fc897b2006-08-28 14:56:16 -07004291 buffer_info->skb = skb;
Jesse Brandeburg40a14de2010-01-19 14:15:38 +00004292 if (status & E1000_RXD_STAT_EOP)
4293 adapter->discarding = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294 goto next_desc;
4295 }
4296
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004297 if (unlikely(rx_desc->errors & E1000_RXD_ERR_FRAME_ERR_MASK)) {
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004298 u8 last_byte = *(skb->data + length - 1);
Joe Perches1dc32912008-07-11 15:17:08 -07004299 if (TBI_ACCEPT(hw, status, rx_desc->errors, length,
4300 last_byte)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301 spin_lock_irqsave(&adapter->stats_lock, flags);
Joe Perches1dc32912008-07-11 15:17:08 -07004302 e1000_tbi_adjust_stats(hw, &adapter->stats,
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00004303 length, skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304 spin_unlock_irqrestore(&adapter->stats_lock,
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00004305 flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306 length--;
4307 } else {
Ben Greeare825b732012-04-04 06:01:29 +00004308 if (netdev->features & NETIF_F_RXALL)
4309 goto process_skb;
Auke Kok9e2feac2006-04-14 19:05:18 -07004310 /* recycle */
4311 buffer_info->skb = skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312 goto next_desc;
4313 }
Auke Kok1cb58212006-04-18 12:31:04 -07004314 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315
Ben Greeare825b732012-04-04 06:01:29 +00004316process_skb:
Ben Greearb0d15622012-02-11 15:40:11 +00004317 total_rx_bytes += (length - 4); /* don't count FCS */
Jesse Brandeburg835bb122006-11-01 08:48:13 -08004318 total_rx_packets++;
4319
Ben Greearb0d15622012-02-11 15:40:11 +00004320 if (likely(!(netdev->features & NETIF_F_RXFCS)))
4321 /* adjust length to remove Ethernet CRC, this must be
4322 * done after the TBI_ACCEPT workaround above
4323 */
4324 length -= 4;
4325
Joe Perches57bf6ee2010-05-13 15:26:17 +00004326 e1000_check_copybreak(netdev, buffer_info, length, &skb);
4327
Auke Kok996695d2006-11-01 08:47:50 -08004328 skb_put(skb, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329
4330 /* Receive Checksum Offload */
Jeff Kirshera292ca62006-01-12 16:51:30 -08004331 e1000_rx_checksum(adapter,
Joe Perches406874a2008-04-03 10:06:32 -07004332 (u32)(status) |
4333 ((u32)(rx_desc->errors) << 24),
David S. Millerc3d7a3a2006-03-15 14:26:28 -08004334 le16_to_cpu(rx_desc->csum), skb);
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004335
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004336 e1000_receive_skb(adapter, status, rx_desc->special, skb);
Francois Romieuc3570ac2008-07-11 15:17:38 -07004337
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338next_desc:
4339 rx_desc->status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340
Jeff Kirsher72d64a42006-01-12 16:51:19 -08004341 /* return some buffers to hardware, one at a time is too slow */
4342 if (unlikely(cleaned_count >= E1000_RX_BUFFER_WRITE)) {
4343 adapter->alloc_rx_buf(adapter, rx_ring, cleaned_count);
4344 cleaned_count = 0;
4345 }
4346
Jeff Kirsher30320be2006-03-02 18:21:57 -08004347 /* use prefetched values */
Jesse Brandeburg86c3d592006-01-18 13:01:43 -08004348 rx_desc = next_rxd;
4349 buffer_info = next_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351 rx_ring->next_to_clean = i;
Jeff Kirsher72d64a42006-01-12 16:51:19 -08004352
4353 cleaned_count = E1000_DESC_UNUSED(rx_ring);
4354 if (cleaned_count)
4355 adapter->alloc_rx_buf(adapter, rx_ring, cleaned_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356
Jesse Brandeburg835bb122006-11-01 08:48:13 -08004357 adapter->total_rx_packets += total_rx_packets;
4358 adapter->total_rx_bytes += total_rx_bytes;
Ajit Khaparde5fe31de2009-10-07 02:42:23 +00004359 netdev->stats.rx_bytes += total_rx_bytes;
4360 netdev->stats.rx_packets += total_rx_packets;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361 return cleaned;
4362}
4363
4364/**
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004365 * e1000_alloc_jumbo_rx_buffers - Replace used jumbo receive buffers
4366 * @adapter: address of board private structure
4367 * @rx_ring: pointer to receive ring structure
4368 * @cleaned_count: number of buffers to allocate this pass
4369 **/
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004370static void
4371e1000_alloc_jumbo_rx_buffers(struct e1000_adapter *adapter,
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00004372 struct e1000_rx_ring *rx_ring, int cleaned_count)
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004373{
4374 struct net_device *netdev = adapter->netdev;
4375 struct pci_dev *pdev = adapter->pdev;
4376 struct e1000_rx_desc *rx_desc;
4377 struct e1000_buffer *buffer_info;
4378 struct sk_buff *skb;
4379 unsigned int i;
Eric Dumazet89d71a62009-10-13 05:34:20 +00004380 unsigned int bufsz = 256 - 16 /*for skb_reserve */ ;
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004381
4382 i = rx_ring->next_to_use;
4383 buffer_info = &rx_ring->buffer_info[i];
4384
4385 while (cleaned_count--) {
4386 skb = buffer_info->skb;
4387 if (skb) {
4388 skb_trim(skb, 0);
4389 goto check_page;
4390 }
4391
Eric Dumazet89d71a62009-10-13 05:34:20 +00004392 skb = netdev_alloc_skb_ip_align(netdev, bufsz);
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004393 if (unlikely(!skb)) {
4394 /* Better luck next round */
4395 adapter->alloc_rx_buff_failed++;
4396 break;
4397 }
4398
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004399 buffer_info->skb = skb;
4400 buffer_info->length = adapter->rx_buffer_len;
4401check_page:
4402 /* allocate a new page if necessary */
4403 if (!buffer_info->page) {
4404 buffer_info->page = alloc_page(GFP_ATOMIC);
4405 if (unlikely(!buffer_info->page)) {
4406 adapter->alloc_rx_buff_failed++;
4407 break;
4408 }
4409 }
4410
Anton Blanchardb5abb022010-02-19 17:54:53 +00004411 if (!buffer_info->dma) {
Nick Nunleyb16f53b2010-04-27 13:08:45 +00004412 buffer_info->dma = dma_map_page(&pdev->dev,
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00004413 buffer_info->page, 0,
Nick Nunleyb16f53b2010-04-27 13:08:45 +00004414 buffer_info->length,
4415 DMA_FROM_DEVICE);
4416 if (dma_mapping_error(&pdev->dev, buffer_info->dma)) {
Anton Blanchardb5abb022010-02-19 17:54:53 +00004417 put_page(buffer_info->page);
4418 dev_kfree_skb(skb);
4419 buffer_info->page = NULL;
4420 buffer_info->skb = NULL;
4421 buffer_info->dma = 0;
4422 adapter->alloc_rx_buff_failed++;
4423 break; /* while !buffer_info->skb */
4424 }
4425 }
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004426
4427 rx_desc = E1000_RX_DESC(*rx_ring, i);
4428 rx_desc->buffer_addr = cpu_to_le64(buffer_info->dma);
4429
4430 if (unlikely(++i == rx_ring->count))
4431 i = 0;
4432 buffer_info = &rx_ring->buffer_info[i];
4433 }
4434
4435 if (likely(rx_ring->next_to_use != i)) {
4436 rx_ring->next_to_use = i;
4437 if (unlikely(i-- == 0))
4438 i = (rx_ring->count - 1);
4439
4440 /* Force memory writes to complete before letting h/w
4441 * know there are new descriptors to fetch. (Only
4442 * applicable for weak-ordered memory model archs,
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00004443 * such as IA-64).
4444 */
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004445 wmb();
4446 writel(i, adapter->hw.hw_addr + rx_ring->rdt);
4447 }
4448}
4449
4450/**
Malli Chilakala2d7edb92005-04-28 19:43:52 -07004451 * e1000_alloc_rx_buffers - Replace used receive buffers; legacy & extended
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452 * @adapter: address of board private structure
4453 **/
Joe Perches64798842008-07-11 15:17:02 -07004454static void e1000_alloc_rx_buffers(struct e1000_adapter *adapter,
4455 struct e1000_rx_ring *rx_ring,
4456 int cleaned_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457{
Joe Perches1dc32912008-07-11 15:17:08 -07004458 struct e1000_hw *hw = &adapter->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459 struct net_device *netdev = adapter->netdev;
4460 struct pci_dev *pdev = adapter->pdev;
4461 struct e1000_rx_desc *rx_desc;
4462 struct e1000_buffer *buffer_info;
4463 struct sk_buff *skb;
Malli Chilakala26483452005-04-28 19:44:46 -07004464 unsigned int i;
Eric Dumazet89d71a62009-10-13 05:34:20 +00004465 unsigned int bufsz = adapter->rx_buffer_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466
4467 i = rx_ring->next_to_use;
4468 buffer_info = &rx_ring->buffer_info[i];
4469
Jeff Kirshera292ca62006-01-12 16:51:30 -08004470 while (cleaned_count--) {
Christoph Hellwigca6f7222006-08-31 14:27:47 -07004471 skb = buffer_info->skb;
4472 if (skb) {
Jeff Kirshera292ca62006-01-12 16:51:30 -08004473 skb_trim(skb, 0);
4474 goto map_skb;
4475 }
4476
Eric Dumazet89d71a62009-10-13 05:34:20 +00004477 skb = netdev_alloc_skb_ip_align(netdev, bufsz);
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004478 if (unlikely(!skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479 /* Better luck next round */
Jeff Kirsher72d64a42006-01-12 16:51:19 -08004480 adapter->alloc_rx_buff_failed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481 break;
4482 }
4483
Malli Chilakala26483452005-04-28 19:44:46 -07004484 /* Fix for errata 23, can't cross 64kB boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485 if (!e1000_check_64k_bound(adapter, skb->data, bufsz)) {
4486 struct sk_buff *oldskb = skb;
Emil Tantilovfeb8f472010-07-26 23:37:21 -07004487 e_err(rx_err, "skb align check failed: %u bytes at "
4488 "%p\n", bufsz, skb->data);
Malli Chilakala26483452005-04-28 19:44:46 -07004489 /* Try again, without freeing the previous */
Eric Dumazet89d71a62009-10-13 05:34:20 +00004490 skb = netdev_alloc_skb_ip_align(netdev, bufsz);
Malli Chilakala26483452005-04-28 19:44:46 -07004491 /* Failed allocation, critical failure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492 if (!skb) {
4493 dev_kfree_skb(oldskb);
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004494 adapter->alloc_rx_buff_failed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004495 break;
4496 }
Malli Chilakala26483452005-04-28 19:44:46 -07004497
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498 if (!e1000_check_64k_bound(adapter, skb->data, bufsz)) {
4499 /* give up */
4500 dev_kfree_skb(skb);
4501 dev_kfree_skb(oldskb);
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004502 adapter->alloc_rx_buff_failed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503 break; /* while !buffer_info->skb */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504 }
Christoph Hellwigca6f7222006-08-31 14:27:47 -07004505
4506 /* Use new allocation */
4507 dev_kfree_skb(oldskb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509 buffer_info->skb = skb;
4510 buffer_info->length = adapter->rx_buffer_len;
Jeff Kirshera292ca62006-01-12 16:51:30 -08004511map_skb:
Nick Nunleyb16f53b2010-04-27 13:08:45 +00004512 buffer_info->dma = dma_map_single(&pdev->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513 skb->data,
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004514 buffer_info->length,
Nick Nunleyb16f53b2010-04-27 13:08:45 +00004515 DMA_FROM_DEVICE);
4516 if (dma_mapping_error(&pdev->dev, buffer_info->dma)) {
Anton Blanchardb5abb022010-02-19 17:54:53 +00004517 dev_kfree_skb(skb);
4518 buffer_info->skb = NULL;
4519 buffer_info->dma = 0;
4520 adapter->alloc_rx_buff_failed++;
4521 break; /* while !buffer_info->skb */
4522 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004523
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00004524 /* XXX if it was allocated cleanly it will never map to a
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004525 * boundary crossing
4526 */
4527
Malli Chilakala26483452005-04-28 19:44:46 -07004528 /* Fix for errata 23, can't cross 64kB boundary */
4529 if (!e1000_check_64k_bound(adapter,
4530 (void *)(unsigned long)buffer_info->dma,
4531 adapter->rx_buffer_len)) {
Emil Tantilovfeb8f472010-07-26 23:37:21 -07004532 e_err(rx_err, "dma align check failed: %u bytes at "
4533 "%p\n", adapter->rx_buffer_len,
Emil Tantilov675ad472010-04-27 14:02:58 +00004534 (void *)(unsigned long)buffer_info->dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535 dev_kfree_skb(skb);
4536 buffer_info->skb = NULL;
4537
Nick Nunleyb16f53b2010-04-27 13:08:45 +00004538 dma_unmap_single(&pdev->dev, buffer_info->dma,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539 adapter->rx_buffer_len,
Nick Nunleyb16f53b2010-04-27 13:08:45 +00004540 DMA_FROM_DEVICE);
Jesse Brandeburg679be3b2009-06-30 12:45:34 +00004541 buffer_info->dma = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004543 adapter->alloc_rx_buff_failed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544 break; /* while !buffer_info->skb */
4545 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546 rx_desc = E1000_RX_DESC(*rx_ring, i);
4547 rx_desc->buffer_addr = cpu_to_le64(buffer_info->dma);
4548
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004549 if (unlikely(++i == rx_ring->count))
4550 i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551 buffer_info = &rx_ring->buffer_info[i];
4552 }
4553
Jesse Brandeburgb92ff8e2006-01-18 13:01:32 -08004554 if (likely(rx_ring->next_to_use != i)) {
4555 rx_ring->next_to_use = i;
4556 if (unlikely(i-- == 0))
4557 i = (rx_ring->count - 1);
4558
4559 /* Force memory writes to complete before letting h/w
4560 * know there are new descriptors to fetch. (Only
4561 * applicable for weak-ordered memory model archs,
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00004562 * such as IA-64).
4563 */
Jesse Brandeburgb92ff8e2006-01-18 13:01:32 -08004564 wmb();
Joe Perches1dc32912008-07-11 15:17:08 -07004565 writel(i, hw->hw_addr + rx_ring->rdt);
Jesse Brandeburgb92ff8e2006-01-18 13:01:32 -08004566 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567}
4568
4569/**
4570 * e1000_smartspeed - Workaround for SmartSpeed on 82541 and 82547 controllers.
4571 * @adapter:
4572 **/
Joe Perches64798842008-07-11 15:17:02 -07004573static void e1000_smartspeed(struct e1000_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574{
Joe Perches1dc32912008-07-11 15:17:08 -07004575 struct e1000_hw *hw = &adapter->hw;
Joe Perches406874a2008-04-03 10:06:32 -07004576 u16 phy_status;
4577 u16 phy_ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578
Joe Perches1dc32912008-07-11 15:17:08 -07004579 if ((hw->phy_type != e1000_phy_igp) || !hw->autoneg ||
4580 !(hw->autoneg_advertised & ADVERTISE_1000_FULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581 return;
4582
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004583 if (adapter->smartspeed == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584 /* If Master/Slave config fault is asserted twice,
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00004585 * we assume back-to-back
4586 */
Joe Perches1dc32912008-07-11 15:17:08 -07004587 e1000_read_phy_reg(hw, PHY_1000T_STATUS, &phy_status);
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004588 if (!(phy_status & SR_1000T_MS_CONFIG_FAULT)) return;
Joe Perches1dc32912008-07-11 15:17:08 -07004589 e1000_read_phy_reg(hw, PHY_1000T_STATUS, &phy_status);
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004590 if (!(phy_status & SR_1000T_MS_CONFIG_FAULT)) return;
Joe Perches1dc32912008-07-11 15:17:08 -07004591 e1000_read_phy_reg(hw, PHY_1000T_CTRL, &phy_ctrl);
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004592 if (phy_ctrl & CR_1000T_MS_ENABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593 phy_ctrl &= ~CR_1000T_MS_ENABLE;
Joe Perches1dc32912008-07-11 15:17:08 -07004594 e1000_write_phy_reg(hw, PHY_1000T_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595 phy_ctrl);
4596 adapter->smartspeed++;
Joe Perches1dc32912008-07-11 15:17:08 -07004597 if (!e1000_phy_setup_autoneg(hw) &&
4598 !e1000_read_phy_reg(hw, PHY_CTRL,
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00004599 &phy_ctrl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600 phy_ctrl |= (MII_CR_AUTO_NEG_EN |
4601 MII_CR_RESTART_AUTO_NEG);
Joe Perches1dc32912008-07-11 15:17:08 -07004602 e1000_write_phy_reg(hw, PHY_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603 phy_ctrl);
4604 }
4605 }
4606 return;
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004607 } else if (adapter->smartspeed == E1000_SMARTSPEED_DOWNSHIFT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608 /* If still no link, perhaps using 2/3 pair cable */
Joe Perches1dc32912008-07-11 15:17:08 -07004609 e1000_read_phy_reg(hw, PHY_1000T_CTRL, &phy_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610 phy_ctrl |= CR_1000T_MS_ENABLE;
Joe Perches1dc32912008-07-11 15:17:08 -07004611 e1000_write_phy_reg(hw, PHY_1000T_CTRL, phy_ctrl);
4612 if (!e1000_phy_setup_autoneg(hw) &&
4613 !e1000_read_phy_reg(hw, PHY_CTRL, &phy_ctrl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614 phy_ctrl |= (MII_CR_AUTO_NEG_EN |
4615 MII_CR_RESTART_AUTO_NEG);
Joe Perches1dc32912008-07-11 15:17:08 -07004616 e1000_write_phy_reg(hw, PHY_CTRL, phy_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617 }
4618 }
4619 /* Restart process after E1000_SMARTSPEED_MAX iterations */
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004620 if (adapter->smartspeed++ == E1000_SMARTSPEED_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621 adapter->smartspeed = 0;
4622}
4623
4624/**
4625 * e1000_ioctl -
4626 * @netdev:
4627 * @ifreq:
4628 * @cmd:
4629 **/
Joe Perches64798842008-07-11 15:17:02 -07004630static int e1000_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631{
4632 switch (cmd) {
4633 case SIOCGMIIPHY:
4634 case SIOCGMIIREG:
4635 case SIOCSMIIREG:
4636 return e1000_mii_ioctl(netdev, ifr, cmd);
4637 default:
4638 return -EOPNOTSUPP;
4639 }
4640}
4641
4642/**
4643 * e1000_mii_ioctl -
4644 * @netdev:
4645 * @ifreq:
4646 * @cmd:
4647 **/
Joe Perches64798842008-07-11 15:17:02 -07004648static int e1000_mii_ioctl(struct net_device *netdev, struct ifreq *ifr,
4649 int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650{
Malli Chilakala60490fe2005-06-17 17:41:45 -07004651 struct e1000_adapter *adapter = netdev_priv(netdev);
Joe Perches1dc32912008-07-11 15:17:08 -07004652 struct e1000_hw *hw = &adapter->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653 struct mii_ioctl_data *data = if_mii(ifr);
4654 int retval;
Joe Perches406874a2008-04-03 10:06:32 -07004655 u16 mii_reg;
Malli Chilakala97876fc2005-06-17 17:40:19 -07004656 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657
Joe Perches1dc32912008-07-11 15:17:08 -07004658 if (hw->media_type != e1000_media_type_copper)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659 return -EOPNOTSUPP;
4660
4661 switch (cmd) {
4662 case SIOCGMIIPHY:
Joe Perches1dc32912008-07-11 15:17:08 -07004663 data->phy_id = hw->phy_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664 break;
4665 case SIOCGMIIREG:
Malli Chilakala97876fc2005-06-17 17:40:19 -07004666 spin_lock_irqsave(&adapter->stats_lock, flags);
Joe Perches1dc32912008-07-11 15:17:08 -07004667 if (e1000_read_phy_reg(hw, data->reg_num & 0x1F,
Malli Chilakala97876fc2005-06-17 17:40:19 -07004668 &data->val_out)) {
4669 spin_unlock_irqrestore(&adapter->stats_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670 return -EIO;
Malli Chilakala97876fc2005-06-17 17:40:19 -07004671 }
4672 spin_unlock_irqrestore(&adapter->stats_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673 break;
4674 case SIOCSMIIREG:
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004675 if (data->reg_num & ~(0x1F))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676 return -EFAULT;
4677 mii_reg = data->val_in;
Malli Chilakala97876fc2005-06-17 17:40:19 -07004678 spin_lock_irqsave(&adapter->stats_lock, flags);
Joe Perches1dc32912008-07-11 15:17:08 -07004679 if (e1000_write_phy_reg(hw, data->reg_num,
Malli Chilakala97876fc2005-06-17 17:40:19 -07004680 mii_reg)) {
4681 spin_unlock_irqrestore(&adapter->stats_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682 return -EIO;
Malli Chilakala97876fc2005-06-17 17:40:19 -07004683 }
Jesse Brandeburgf0163ac2007-11-13 21:00:09 -08004684 spin_unlock_irqrestore(&adapter->stats_lock, flags);
Joe Perches1dc32912008-07-11 15:17:08 -07004685 if (hw->media_type == e1000_media_type_copper) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686 switch (data->reg_num) {
4687 case PHY_CTRL:
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004688 if (mii_reg & MII_CR_POWER_DOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689 break;
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004690 if (mii_reg & MII_CR_AUTO_NEG_EN) {
Joe Perches1dc32912008-07-11 15:17:08 -07004691 hw->autoneg = 1;
4692 hw->autoneg_advertised = 0x2F;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693 } else {
David Decotigny14ad2512011-04-27 18:32:43 +00004694 u32 speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695 if (mii_reg & 0x40)
David Decotigny14ad2512011-04-27 18:32:43 +00004696 speed = SPEED_1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697 else if (mii_reg & 0x2000)
David Decotigny14ad2512011-04-27 18:32:43 +00004698 speed = SPEED_100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699 else
David Decotigny14ad2512011-04-27 18:32:43 +00004700 speed = SPEED_10;
4701 retval = e1000_set_spd_dplx(
4702 adapter, speed,
4703 ((mii_reg & 0x100)
4704 ? DUPLEX_FULL :
4705 DUPLEX_HALF));
Jesse Brandeburgf0163ac2007-11-13 21:00:09 -08004706 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707 return retval;
4708 }
Auke Kok2db10a02006-06-27 09:06:28 -07004709 if (netif_running(adapter->netdev))
4710 e1000_reinit_locked(adapter);
4711 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712 e1000_reset(adapter);
4713 break;
4714 case M88E1000_PHY_SPEC_CTRL:
4715 case M88E1000_EXT_PHY_SPEC_CTRL:
Joe Perches1dc32912008-07-11 15:17:08 -07004716 if (e1000_phy_reset(hw))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717 return -EIO;
4718 break;
4719 }
4720 } else {
4721 switch (data->reg_num) {
4722 case PHY_CTRL:
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004723 if (mii_reg & MII_CR_POWER_DOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724 break;
Auke Kok2db10a02006-06-27 09:06:28 -07004725 if (netif_running(adapter->netdev))
4726 e1000_reinit_locked(adapter);
4727 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728 e1000_reset(adapter);
4729 break;
4730 }
4731 }
4732 break;
4733 default:
4734 return -EOPNOTSUPP;
4735 }
4736 return E1000_SUCCESS;
4737}
4738
Joe Perches64798842008-07-11 15:17:02 -07004739void e1000_pci_set_mwi(struct e1000_hw *hw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740{
4741 struct e1000_adapter *adapter = hw->back;
Malli Chilakala26483452005-04-28 19:44:46 -07004742 int ret_val = pci_set_mwi(adapter->pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004744 if (ret_val)
Emil Tantilovfeb8f472010-07-26 23:37:21 -07004745 e_err(probe, "Error in setting MWI\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746}
4747
Joe Perches64798842008-07-11 15:17:02 -07004748void e1000_pci_clear_mwi(struct e1000_hw *hw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749{
4750 struct e1000_adapter *adapter = hw->back;
4751
4752 pci_clear_mwi(adapter->pdev);
4753}
4754
Joe Perches64798842008-07-11 15:17:02 -07004755int e1000_pcix_get_mmrbc(struct e1000_hw *hw)
Peter Oruba007755e2007-09-28 22:42:06 -07004756{
4757 struct e1000_adapter *adapter = hw->back;
4758 return pcix_get_mmrbc(adapter->pdev);
4759}
4760
Joe Perches64798842008-07-11 15:17:02 -07004761void e1000_pcix_set_mmrbc(struct e1000_hw *hw, int mmrbc)
Peter Oruba007755e2007-09-28 22:42:06 -07004762{
4763 struct e1000_adapter *adapter = hw->back;
4764 pcix_set_mmrbc(adapter->pdev, mmrbc);
4765}
4766
Joe Perches64798842008-07-11 15:17:02 -07004767void e1000_io_write(struct e1000_hw *hw, unsigned long port, u32 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768{
4769 outl(value, port);
4770}
4771
Jiri Pirko5622e402011-07-21 03:26:31 +00004772static bool e1000_vlan_used(struct e1000_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773{
Jiri Pirko5622e402011-07-21 03:26:31 +00004774 u16 vid;
4775
4776 for_each_set_bit(vid, adapter->active_vlans, VLAN_N_VID)
4777 return true;
4778 return false;
4779}
4780
Jiri Pirko52f55092012-03-20 18:10:01 +00004781static void __e1000_vlan_mode(struct e1000_adapter *adapter,
4782 netdev_features_t features)
4783{
4784 struct e1000_hw *hw = &adapter->hw;
4785 u32 ctrl;
4786
4787 ctrl = er32(CTRL);
Patrick McHardyf6469682013-04-19 02:04:27 +00004788 if (features & NETIF_F_HW_VLAN_CTAG_RX) {
Jiri Pirko52f55092012-03-20 18:10:01 +00004789 /* enable VLAN tag insert/strip */
4790 ctrl |= E1000_CTRL_VME;
4791 } else {
4792 /* disable VLAN tag insert/strip */
4793 ctrl &= ~E1000_CTRL_VME;
4794 }
4795 ew32(CTRL, ctrl);
4796}
Jiri Pirko5622e402011-07-21 03:26:31 +00004797static void e1000_vlan_filter_on_off(struct e1000_adapter *adapter,
4798 bool filter_on)
4799{
Joe Perches1dc32912008-07-11 15:17:08 -07004800 struct e1000_hw *hw = &adapter->hw;
Jiri Pirko5622e402011-07-21 03:26:31 +00004801 u32 rctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802
Jesse Brandeburg9150b762008-03-21 11:06:58 -07004803 if (!test_bit(__E1000_DOWN, &adapter->flags))
4804 e1000_irq_disable(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004805
Jiri Pirko52f55092012-03-20 18:10:01 +00004806 __e1000_vlan_mode(adapter, adapter->netdev->features);
Jiri Pirko5622e402011-07-21 03:26:31 +00004807 if (filter_on) {
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00004808 /* enable VLAN receive filtering */
4809 rctl = er32(RCTL);
4810 rctl &= ~E1000_RCTL_CFIEN;
Jiri Pirko5622e402011-07-21 03:26:31 +00004811 if (!(adapter->netdev->flags & IFF_PROMISC))
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00004812 rctl |= E1000_RCTL_VFE;
4813 ew32(RCTL, rctl);
4814 e1000_update_mng_vlan(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815 } else {
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00004816 /* disable VLAN receive filtering */
4817 rctl = er32(RCTL);
4818 rctl &= ~E1000_RCTL_VFE;
4819 ew32(RCTL, rctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004820 }
4821
Jesse Brandeburg9150b762008-03-21 11:06:58 -07004822 if (!test_bit(__E1000_DOWN, &adapter->flags))
4823 e1000_irq_enable(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004824}
4825
Michał Mirosławc8f44af2011-11-15 15:29:55 +00004826static void e1000_vlan_mode(struct net_device *netdev,
Jiri Pirko52f55092012-03-20 18:10:01 +00004827 netdev_features_t features)
Jiri Pirko5622e402011-07-21 03:26:31 +00004828{
4829 struct e1000_adapter *adapter = netdev_priv(netdev);
Jiri Pirko5622e402011-07-21 03:26:31 +00004830
4831 if (!test_bit(__E1000_DOWN, &adapter->flags))
4832 e1000_irq_disable(adapter);
4833
Jiri Pirko52f55092012-03-20 18:10:01 +00004834 __e1000_vlan_mode(adapter, features);
Jiri Pirko5622e402011-07-21 03:26:31 +00004835
4836 if (!test_bit(__E1000_DOWN, &adapter->flags))
4837 e1000_irq_enable(adapter);
4838}
4839
Jiri Pirko8e586132011-12-08 19:52:37 -05004840static int e1000_vlan_rx_add_vid(struct net_device *netdev, u16 vid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841{
Malli Chilakala60490fe2005-06-17 17:41:45 -07004842 struct e1000_adapter *adapter = netdev_priv(netdev);
Joe Perches1dc32912008-07-11 15:17:08 -07004843 struct e1000_hw *hw = &adapter->hw;
Joe Perches406874a2008-04-03 10:06:32 -07004844 u32 vfta, index;
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004845
Joe Perches1dc32912008-07-11 15:17:08 -07004846 if ((hw->mng_cookie.status &
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004847 E1000_MNG_DHCP_COOKIE_STATUS_VLAN_SUPPORT) &&
4848 (vid == adapter->mng_vlan_id))
Jiri Pirko8e586132011-12-08 19:52:37 -05004849 return 0;
Jiri Pirko5622e402011-07-21 03:26:31 +00004850
4851 if (!e1000_vlan_used(adapter))
4852 e1000_vlan_filter_on_off(adapter, true);
4853
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854 /* add VID to filter table */
4855 index = (vid >> 5) & 0x7F;
Joe Perches1dc32912008-07-11 15:17:08 -07004856 vfta = E1000_READ_REG_ARRAY(hw, VFTA, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004857 vfta |= (1 << (vid & 0x1F));
Joe Perches1dc32912008-07-11 15:17:08 -07004858 e1000_write_vfta(hw, index, vfta);
Jiri Pirko5622e402011-07-21 03:26:31 +00004859
4860 set_bit(vid, adapter->active_vlans);
Jiri Pirko8e586132011-12-08 19:52:37 -05004861
4862 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863}
4864
Jiri Pirko8e586132011-12-08 19:52:37 -05004865static int e1000_vlan_rx_kill_vid(struct net_device *netdev, u16 vid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004866{
Malli Chilakala60490fe2005-06-17 17:41:45 -07004867 struct e1000_adapter *adapter = netdev_priv(netdev);
Joe Perches1dc32912008-07-11 15:17:08 -07004868 struct e1000_hw *hw = &adapter->hw;
Joe Perches406874a2008-04-03 10:06:32 -07004869 u32 vfta, index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870
Jesse Brandeburg9150b762008-03-21 11:06:58 -07004871 if (!test_bit(__E1000_DOWN, &adapter->flags))
4872 e1000_irq_disable(adapter);
Jesse Brandeburg9150b762008-03-21 11:06:58 -07004873 if (!test_bit(__E1000_DOWN, &adapter->flags))
4874 e1000_irq_enable(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004875
4876 /* remove VID from filter table */
4877 index = (vid >> 5) & 0x7F;
Joe Perches1dc32912008-07-11 15:17:08 -07004878 vfta = E1000_READ_REG_ARRAY(hw, VFTA, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879 vfta &= ~(1 << (vid & 0x1F));
Joe Perches1dc32912008-07-11 15:17:08 -07004880 e1000_write_vfta(hw, index, vfta);
Jiri Pirko5622e402011-07-21 03:26:31 +00004881
4882 clear_bit(vid, adapter->active_vlans);
4883
4884 if (!e1000_vlan_used(adapter))
4885 e1000_vlan_filter_on_off(adapter, false);
Jiri Pirko8e586132011-12-08 19:52:37 -05004886
4887 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888}
4889
Joe Perches64798842008-07-11 15:17:02 -07004890static void e1000_restore_vlan(struct e1000_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891{
Jiri Pirko5622e402011-07-21 03:26:31 +00004892 u16 vid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893
Jiri Pirko5622e402011-07-21 03:26:31 +00004894 if (!e1000_vlan_used(adapter))
4895 return;
4896
4897 e1000_vlan_filter_on_off(adapter, true);
4898 for_each_set_bit(vid, adapter->active_vlans, VLAN_N_VID)
4899 e1000_vlan_rx_add_vid(adapter->netdev, vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900}
4901
David Decotigny14ad2512011-04-27 18:32:43 +00004902int e1000_set_spd_dplx(struct e1000_adapter *adapter, u32 spd, u8 dplx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004903{
Joe Perches1dc32912008-07-11 15:17:08 -07004904 struct e1000_hw *hw = &adapter->hw;
4905
4906 hw->autoneg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907
David Decotigny14ad2512011-04-27 18:32:43 +00004908 /* Make sure dplx is at most 1 bit and lsb of speed is not set
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00004909 * for the switch() below to work
4910 */
David Decotigny14ad2512011-04-27 18:32:43 +00004911 if ((spd & 1) || (dplx & ~1))
4912 goto err_inval;
4913
Malli Chilakala69213682005-06-17 17:44:20 -07004914 /* Fiber NICs only allow 1000 gbps Full duplex */
Joe Perches1dc32912008-07-11 15:17:08 -07004915 if ((hw->media_type == e1000_media_type_fiber) &&
David Decotigny14ad2512011-04-27 18:32:43 +00004916 spd != SPEED_1000 &&
4917 dplx != DUPLEX_FULL)
4918 goto err_inval;
Malli Chilakala69213682005-06-17 17:44:20 -07004919
David Decotigny14ad2512011-04-27 18:32:43 +00004920 switch (spd + dplx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921 case SPEED_10 + DUPLEX_HALF:
Joe Perches1dc32912008-07-11 15:17:08 -07004922 hw->forced_speed_duplex = e1000_10_half;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923 break;
4924 case SPEED_10 + DUPLEX_FULL:
Joe Perches1dc32912008-07-11 15:17:08 -07004925 hw->forced_speed_duplex = e1000_10_full;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926 break;
4927 case SPEED_100 + DUPLEX_HALF:
Joe Perches1dc32912008-07-11 15:17:08 -07004928 hw->forced_speed_duplex = e1000_100_half;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929 break;
4930 case SPEED_100 + DUPLEX_FULL:
Joe Perches1dc32912008-07-11 15:17:08 -07004931 hw->forced_speed_duplex = e1000_100_full;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932 break;
4933 case SPEED_1000 + DUPLEX_FULL:
Joe Perches1dc32912008-07-11 15:17:08 -07004934 hw->autoneg = 1;
4935 hw->autoneg_advertised = ADVERTISE_1000_FULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936 break;
4937 case SPEED_1000 + DUPLEX_HALF: /* not supported */
4938 default:
David Decotigny14ad2512011-04-27 18:32:43 +00004939 goto err_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940 }
Jesse Brandeburgc819bbd52012-07-26 02:31:09 +00004941
4942 /* clear MDI, MDI(-X) override is only allowed when autoneg enabled */
4943 hw->mdix = AUTO_ALL_MODES;
4944
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945 return 0;
David Decotigny14ad2512011-04-27 18:32:43 +00004946
4947err_inval:
4948 e_err(probe, "Unsupported Speed/Duplex configuration\n");
4949 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004950}
4951
Rafael J. Wysockib43fcd72009-04-15 17:43:24 +00004952static int __e1000_shutdown(struct pci_dev *pdev, bool *enable_wake)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004953{
4954 struct net_device *netdev = pci_get_drvdata(pdev);
Malli Chilakala60490fe2005-06-17 17:41:45 -07004955 struct e1000_adapter *adapter = netdev_priv(netdev);
Joe Perches1dc32912008-07-11 15:17:08 -07004956 struct e1000_hw *hw = &adapter->hw;
Joe Perches406874a2008-04-03 10:06:32 -07004957 u32 ctrl, ctrl_ext, rctl, status;
4958 u32 wufc = adapter->wol;
Auke Kok6fdfef12006-06-27 09:06:36 -07004959#ifdef CONFIG_PM
Jeff Kirsher240b1712006-01-12 16:51:28 -08004960 int retval = 0;
Auke Kok6fdfef12006-06-27 09:06:36 -07004961#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962
4963 netif_device_detach(netdev);
4964
Auke Kok2db10a02006-06-27 09:06:28 -07004965 if (netif_running(netdev)) {
4966 WARN_ON(test_bit(__E1000_RESETTING, &adapter->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967 e1000_down(adapter);
Auke Kok2db10a02006-06-27 09:06:28 -07004968 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969
Jesse Brandeburg2f826652006-01-18 13:01:34 -08004970#ifdef CONFIG_PM
Kok, Auke1d33e9c2007-02-16 14:39:28 -08004971 retval = pci_save_state(pdev);
Jesse Brandeburg3a3847e2012-01-04 20:23:33 +00004972 if (retval)
Jesse Brandeburg2f826652006-01-18 13:01:34 -08004973 return retval;
4974#endif
4975
Joe Perches1dc32912008-07-11 15:17:08 -07004976 status = er32(STATUS);
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004977 if (status & E1000_STATUS_LU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978 wufc &= ~E1000_WUFC_LNKC;
4979
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004980 if (wufc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981 e1000_setup_rctl(adapter);
Patrick McHardydb0ce502007-11-13 20:54:59 -08004982 e1000_set_rx_mode(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983
Dean Nelsonb8681792012-01-19 17:47:24 +00004984 rctl = er32(RCTL);
4985
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986 /* turn on all-multi mode if wake on multicast is enabled */
Dean Nelsonb8681792012-01-19 17:47:24 +00004987 if (wufc & E1000_WUFC_MC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004988 rctl |= E1000_RCTL_MPE;
Dean Nelsonb8681792012-01-19 17:47:24 +00004989
4990 /* enable receives in the hardware */
4991 ew32(RCTL, rctl | E1000_RCTL_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004992
Joe Perches1dc32912008-07-11 15:17:08 -07004993 if (hw->mac_type >= e1000_82540) {
4994 ctrl = er32(CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995 /* advertise wake from D3Cold */
4996 #define E1000_CTRL_ADVD3WUC 0x00100000
4997 /* phy power management enable */
4998 #define E1000_CTRL_EN_PHY_PWR_MGMT 0x00200000
4999 ctrl |= E1000_CTRL_ADVD3WUC |
5000 E1000_CTRL_EN_PHY_PWR_MGMT;
Joe Perches1dc32912008-07-11 15:17:08 -07005001 ew32(CTRL, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005002 }
5003
Joe Perches1dc32912008-07-11 15:17:08 -07005004 if (hw->media_type == e1000_media_type_fiber ||
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00005005 hw->media_type == e1000_media_type_internal_serdes) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006 /* keep the laser running in D3 */
Joe Perches1dc32912008-07-11 15:17:08 -07005007 ctrl_ext = er32(CTRL_EXT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005008 ctrl_ext |= E1000_CTRL_EXT_SDP7_DATA;
Joe Perches1dc32912008-07-11 15:17:08 -07005009 ew32(CTRL_EXT, ctrl_ext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005010 }
5011
Joe Perches1dc32912008-07-11 15:17:08 -07005012 ew32(WUC, E1000_WUC_PME_EN);
5013 ew32(WUFC, wufc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014 } else {
Joe Perches1dc32912008-07-11 15:17:08 -07005015 ew32(WUC, 0);
5016 ew32(WUFC, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005017 }
5018
Jeff Garzik0fccd0e2006-12-15 10:56:10 -05005019 e1000_release_manageability(adapter);
5020
Rafael J. Wysockib43fcd72009-04-15 17:43:24 +00005021 *enable_wake = !!wufc;
5022
Jeff Garzik0fccd0e2006-12-15 10:56:10 -05005023 /* make sure adapter isn't asleep if manageability is enabled */
Rafael J. Wysockib43fcd72009-04-15 17:43:24 +00005024 if (adapter->en_mng_pt)
5025 *enable_wake = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005026
Auke Kokedd106f2006-11-06 08:57:12 -08005027 if (netif_running(netdev))
5028 e1000_free_irq(adapter);
5029
Linus Torvalds1da177e2005-04-16 15:20:36 -07005030 pci_disable_device(pdev);
Jeff Kirsher240b1712006-01-12 16:51:28 -08005031
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032 return 0;
5033}
5034
Jesse Brandeburg2f826652006-01-18 13:01:34 -08005035#ifdef CONFIG_PM
Rafael J. Wysockib43fcd72009-04-15 17:43:24 +00005036static int e1000_suspend(struct pci_dev *pdev, pm_message_t state)
5037{
5038 int retval;
5039 bool wake;
5040
5041 retval = __e1000_shutdown(pdev, &wake);
5042 if (retval)
5043 return retval;
5044
5045 if (wake) {
5046 pci_prepare_to_sleep(pdev);
5047 } else {
5048 pci_wake_from_d3(pdev, false);
5049 pci_set_power_state(pdev, PCI_D3hot);
5050 }
5051
5052 return 0;
5053}
5054
Joe Perches64798842008-07-11 15:17:02 -07005055static int e1000_resume(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005056{
5057 struct net_device *netdev = pci_get_drvdata(pdev);
Malli Chilakala60490fe2005-06-17 17:41:45 -07005058 struct e1000_adapter *adapter = netdev_priv(netdev);
Joe Perches1dc32912008-07-11 15:17:08 -07005059 struct e1000_hw *hw = &adapter->hw;
Joe Perches406874a2008-04-03 10:06:32 -07005060 u32 err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005061
Auke Kokd0e027d2006-04-14 19:04:40 -07005062 pci_set_power_state(pdev, PCI_D0);
Kok, Auke1d33e9c2007-02-16 14:39:28 -08005063 pci_restore_state(pdev);
Nick Nunleydbb5aae2010-02-03 14:49:48 +00005064 pci_save_state(pdev);
Taku Izumi81250292008-07-11 15:17:44 -07005065
5066 if (adapter->need_ioport)
5067 err = pci_enable_device(pdev);
5068 else
5069 err = pci_enable_device_mem(pdev);
Joe Perchesc7be73b2008-07-11 15:17:28 -07005070 if (err) {
Emil Tantilov675ad472010-04-27 14:02:58 +00005071 pr_err("Cannot enable PCI device from suspend\n");
Auke Kok3d1dd8c2006-08-28 14:56:27 -07005072 return err;
5073 }
Malli Chilakalaa4cb8472005-04-28 19:41:28 -07005074 pci_set_master(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005075
Auke Kokd0e027d2006-04-14 19:04:40 -07005076 pci_enable_wake(pdev, PCI_D3hot, 0);
5077 pci_enable_wake(pdev, PCI_D3cold, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078
Joe Perchesc7be73b2008-07-11 15:17:28 -07005079 if (netif_running(netdev)) {
5080 err = e1000_request_irq(adapter);
5081 if (err)
5082 return err;
5083 }
Auke Kokedd106f2006-11-06 08:57:12 -08005084
5085 e1000_power_up_phy(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005086 e1000_reset(adapter);
Joe Perches1dc32912008-07-11 15:17:08 -07005087 ew32(WUS, ~0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005088
Jeff Garzik0fccd0e2006-12-15 10:56:10 -05005089 e1000_init_manageability(adapter);
5090
Jesse Brandeburg96838a42006-01-18 13:01:39 -08005091 if (netif_running(netdev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005092 e1000_up(adapter);
5093
5094 netif_device_attach(netdev);
5095
Linus Torvalds1da177e2005-04-16 15:20:36 -07005096 return 0;
5097}
5098#endif
Auke Kokc653e632006-05-23 13:35:57 -07005099
5100static void e1000_shutdown(struct pci_dev *pdev)
5101{
Rafael J. Wysockib43fcd72009-04-15 17:43:24 +00005102 bool wake;
5103
5104 __e1000_shutdown(pdev, &wake);
5105
5106 if (system_state == SYSTEM_POWER_OFF) {
5107 pci_wake_from_d3(pdev, wake);
5108 pci_set_power_state(pdev, PCI_D3hot);
5109 }
Auke Kokc653e632006-05-23 13:35:57 -07005110}
5111
Linus Torvalds1da177e2005-04-16 15:20:36 -07005112#ifdef CONFIG_NET_POLL_CONTROLLER
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00005113/* Polling 'interrupt' - used by things like netconsole to send skbs
Linus Torvalds1da177e2005-04-16 15:20:36 -07005114 * without having to re-enable interrupts. It's not called while
5115 * the interrupt routine is executing.
5116 */
Joe Perches64798842008-07-11 15:17:02 -07005117static void e1000_netpoll(struct net_device *netdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005118{
Malli Chilakala60490fe2005-06-17 17:41:45 -07005119 struct e1000_adapter *adapter = netdev_priv(netdev);
Auke Kokd3d9e482006-07-14 16:14:23 -07005120
Linus Torvalds1da177e2005-04-16 15:20:36 -07005121 disable_irq(adapter->pdev->irq);
David Howells7d12e782006-10-05 14:55:46 +01005122 e1000_intr(adapter->pdev->irq, netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123 enable_irq(adapter->pdev->irq);
5124}
5125#endif
5126
Auke Kok90267292006-06-08 09:30:24 -07005127/**
5128 * e1000_io_error_detected - called when PCI error is detected
5129 * @pdev: Pointer to PCI device
Jesse Brandeburg120a5d02009-09-25 15:19:46 -07005130 * @state: The current pci connection state
Auke Kok90267292006-06-08 09:30:24 -07005131 *
5132 * This function is called after a PCI bus error affecting
5133 * this device has been detected.
5134 */
Joe Perches64798842008-07-11 15:17:02 -07005135static pci_ers_result_t e1000_io_error_detected(struct pci_dev *pdev,
5136 pci_channel_state_t state)
Auke Kok90267292006-06-08 09:30:24 -07005137{
5138 struct net_device *netdev = pci_get_drvdata(pdev);
Wang Chen4cf16532008-11-12 23:38:14 -08005139 struct e1000_adapter *adapter = netdev_priv(netdev);
Auke Kok90267292006-06-08 09:30:24 -07005140
5141 netif_device_detach(netdev);
5142
Andre Detscheab63302009-06-30 12:46:13 +00005143 if (state == pci_channel_io_perm_failure)
5144 return PCI_ERS_RESULT_DISCONNECT;
5145
Auke Kok90267292006-06-08 09:30:24 -07005146 if (netif_running(netdev))
5147 e1000_down(adapter);
Linas Vepstas72e8d6b2006-09-18 20:58:06 -07005148 pci_disable_device(pdev);
Auke Kok90267292006-06-08 09:30:24 -07005149
5150 /* Request a slot slot reset. */
5151 return PCI_ERS_RESULT_NEED_RESET;
5152}
5153
5154/**
5155 * e1000_io_slot_reset - called after the pci bus has been reset.
5156 * @pdev: Pointer to PCI device
5157 *
5158 * Restart the card from scratch, as if from a cold-boot. Implementation
5159 * resembles the first-half of the e1000_resume routine.
5160 */
5161static pci_ers_result_t e1000_io_slot_reset(struct pci_dev *pdev)
5162{
5163 struct net_device *netdev = pci_get_drvdata(pdev);
Wang Chen4cf16532008-11-12 23:38:14 -08005164 struct e1000_adapter *adapter = netdev_priv(netdev);
Joe Perches1dc32912008-07-11 15:17:08 -07005165 struct e1000_hw *hw = &adapter->hw;
Taku Izumi81250292008-07-11 15:17:44 -07005166 int err;
Auke Kok90267292006-06-08 09:30:24 -07005167
Taku Izumi81250292008-07-11 15:17:44 -07005168 if (adapter->need_ioport)
5169 err = pci_enable_device(pdev);
5170 else
5171 err = pci_enable_device_mem(pdev);
5172 if (err) {
Emil Tantilov675ad472010-04-27 14:02:58 +00005173 pr_err("Cannot re-enable PCI device after reset.\n");
Auke Kok90267292006-06-08 09:30:24 -07005174 return PCI_ERS_RESULT_DISCONNECT;
5175 }
5176 pci_set_master(pdev);
5177
Linas Vepstasdbf38c92006-09-27 12:54:11 -07005178 pci_enable_wake(pdev, PCI_D3hot, 0);
5179 pci_enable_wake(pdev, PCI_D3cold, 0);
Auke Kok90267292006-06-08 09:30:24 -07005180
Auke Kok90267292006-06-08 09:30:24 -07005181 e1000_reset(adapter);
Joe Perches1dc32912008-07-11 15:17:08 -07005182 ew32(WUS, ~0);
Auke Kok90267292006-06-08 09:30:24 -07005183
5184 return PCI_ERS_RESULT_RECOVERED;
5185}
5186
5187/**
5188 * e1000_io_resume - called when traffic can start flowing again.
5189 * @pdev: Pointer to PCI device
5190 *
5191 * This callback is called when the error recovery driver tells us that
5192 * its OK to resume normal operation. Implementation resembles the
5193 * second-half of the e1000_resume routine.
5194 */
5195static void e1000_io_resume(struct pci_dev *pdev)
5196{
5197 struct net_device *netdev = pci_get_drvdata(pdev);
Wang Chen4cf16532008-11-12 23:38:14 -08005198 struct e1000_adapter *adapter = netdev_priv(netdev);
Jeff Garzik0fccd0e2006-12-15 10:56:10 -05005199
5200 e1000_init_manageability(adapter);
Auke Kok90267292006-06-08 09:30:24 -07005201
5202 if (netif_running(netdev)) {
5203 if (e1000_up(adapter)) {
Emil Tantilov675ad472010-04-27 14:02:58 +00005204 pr_info("can't bring device back up after reset\n");
Auke Kok90267292006-06-08 09:30:24 -07005205 return;
5206 }
5207 }
5208
5209 netif_device_attach(netdev);
Auke Kok90267292006-06-08 09:30:24 -07005210}
5211
Linus Torvalds1da177e2005-04-16 15:20:36 -07005212/* e1000_main.c */