blob: e38622825fa7336ae74f64229f3e5619cb668335 [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);
Patrick McHardy80d5c362013-04-19 02:04:28 +0000169static int e1000_vlan_rx_add_vid(struct net_device *netdev,
170 __be16 proto, u16 vid);
171static int e1000_vlan_rx_kill_vid(struct net_device *netdev,
172 __be16 proto, u16 vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173static void e1000_restore_vlan(struct e1000_adapter *adapter);
174
Auke Kok6fdfef12006-06-27 09:06:36 -0700175#ifdef CONFIG_PM
Rafael J. Wysockib43fcd72009-04-15 17:43:24 +0000176static int e1000_suspend(struct pci_dev *pdev, pm_message_t state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177static int e1000_resume(struct pci_dev *pdev);
178#endif
Auke Kokc653e632006-05-23 13:35:57 -0700179static void e1000_shutdown(struct pci_dev *pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181#ifdef CONFIG_NET_POLL_CONTROLLER
182/* for netdump / net console */
183static void e1000_netpoll (struct net_device *netdev);
184#endif
185
Jesse Brandeburg1f753862006-12-15 10:40:39 +0100186#define COPYBREAK_DEFAULT 256
187static unsigned int copybreak __read_mostly = COPYBREAK_DEFAULT;
188module_param(copybreak, uint, 0644);
189MODULE_PARM_DESC(copybreak,
190 "Maximum size of packet that is copied to a new buffer on receive");
191
Auke Kok90267292006-06-08 09:30:24 -0700192static pci_ers_result_t e1000_io_error_detected(struct pci_dev *pdev,
193 pci_channel_state_t state);
194static pci_ers_result_t e1000_io_slot_reset(struct pci_dev *pdev);
195static void e1000_io_resume(struct pci_dev *pdev);
196
Stephen Hemminger3646f0e2012-09-07 09:33:15 -0700197static const struct pci_error_handlers e1000_err_handler = {
Auke Kok90267292006-06-08 09:30:24 -0700198 .error_detected = e1000_io_error_detected,
199 .slot_reset = e1000_io_slot_reset,
200 .resume = e1000_io_resume,
201};
Mallikarjuna R Chilakala24025e4ec2005-10-04 07:03:23 -0400202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203static struct pci_driver e1000_driver = {
204 .name = e1000_driver_name,
205 .id_table = e1000_pci_tbl,
206 .probe = e1000_probe,
Bill Pemberton9f9a12f2012-12-03 09:24:25 -0500207 .remove = e1000_remove,
Auke Kokc4e24f02006-09-27 12:53:19 -0700208#ifdef CONFIG_PM
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300209 /* Power Management Hooks */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 .suspend = e1000_suspend,
Auke Kokc653e632006-05-23 13:35:57 -0700211 .resume = e1000_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212#endif
Auke Kok90267292006-06-08 09:30:24 -0700213 .shutdown = e1000_shutdown,
214 .err_handler = &e1000_err_handler
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215};
216
217MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
218MODULE_DESCRIPTION("Intel(R) PRO/1000 Network Driver");
219MODULE_LICENSE("GPL");
220MODULE_VERSION(DRV_VERSION);
221
stephen hemmingerb3f4d592012-03-13 06:04:20 +0000222#define DEFAULT_MSG_ENABLE (NETIF_MSG_DRV|NETIF_MSG_PROBE|NETIF_MSG_LINK)
223static int debug = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224module_param(debug, int, 0);
225MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
226
227/**
Emil Tantilov675ad472010-04-27 14:02:58 +0000228 * e1000_get_hw_dev - return device
229 * used by hardware layer to print debugging information
230 *
231 **/
232struct net_device *e1000_get_hw_dev(struct e1000_hw *hw)
233{
234 struct e1000_adapter *adapter = hw->back;
235 return adapter->netdev;
236}
237
238/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 * e1000_init_module - Driver Registration Routine
240 *
241 * e1000_init_module is the first routine called when the driver is
242 * loaded. All it does is register with the PCI subsystem.
243 **/
Joe Perches64798842008-07-11 15:17:02 -0700244static int __init e1000_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245{
246 int ret;
Emil Tantilov675ad472010-04-27 14:02:58 +0000247 pr_info("%s - version %s\n", e1000_driver_string, e1000_driver_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Emil Tantilov675ad472010-04-27 14:02:58 +0000249 pr_info("%s\n", e1000_copyright);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Jeff Garzik29917622006-08-19 17:48:59 -0400251 ret = pci_register_driver(&e1000_driver);
Jesse Brandeburg1f753862006-12-15 10:40:39 +0100252 if (copybreak != COPYBREAK_DEFAULT) {
253 if (copybreak == 0)
Emil Tantilov675ad472010-04-27 14:02:58 +0000254 pr_info("copybreak disabled\n");
Jesse Brandeburg1f753862006-12-15 10:40:39 +0100255 else
Emil Tantilov675ad472010-04-27 14:02:58 +0000256 pr_info("copybreak enabled for "
257 "packets <= %u bytes\n", copybreak);
Jesse Brandeburg1f753862006-12-15 10:40:39 +0100258 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 return ret;
260}
261
262module_init(e1000_init_module);
263
264/**
265 * e1000_exit_module - Driver Exit Cleanup Routine
266 *
267 * e1000_exit_module is called just before the driver is removed
268 * from memory.
269 **/
Joe Perches64798842008-07-11 15:17:02 -0700270static void __exit e1000_exit_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 pci_unregister_driver(&e1000_driver);
273}
274
275module_exit(e1000_exit_module);
276
Auke Kok2db10a02006-06-27 09:06:28 -0700277static int e1000_request_irq(struct e1000_adapter *adapter)
278{
279 struct net_device *netdev = adapter->netdev;
Al Viro3e188262007-12-11 19:49:39 +0000280 irq_handler_t handler = e1000_intr;
Auke Koke94bd232007-05-16 01:49:46 -0700281 int irq_flags = IRQF_SHARED;
282 int err;
Auke Kok2db10a02006-06-27 09:06:28 -0700283
Auke Koke94bd232007-05-16 01:49:46 -0700284 err = request_irq(adapter->pdev->irq, handler, irq_flags, netdev->name,
285 netdev);
286 if (err) {
Emil Tantilovfeb8f472010-07-26 23:37:21 -0700287 e_err(probe, "Unable to allocate interrupt Error: %d\n", err);
Auke Koke94bd232007-05-16 01:49:46 -0700288 }
Auke Kok2db10a02006-06-27 09:06:28 -0700289
290 return err;
291}
292
293static void e1000_free_irq(struct e1000_adapter *adapter)
294{
295 struct net_device *netdev = adapter->netdev;
296
297 free_irq(adapter->pdev->irq, netdev);
Auke Kok2db10a02006-06-27 09:06:28 -0700298}
299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300/**
301 * e1000_irq_disable - Mask off interrupt generation on the NIC
302 * @adapter: board private structure
303 **/
Joe Perches64798842008-07-11 15:17:02 -0700304static void e1000_irq_disable(struct e1000_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305{
Joe Perches1dc32912008-07-11 15:17:08 -0700306 struct e1000_hw *hw = &adapter->hw;
307
308 ew32(IMC, ~0);
309 E1000_WRITE_FLUSH();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 synchronize_irq(adapter->pdev->irq);
311}
312
313/**
314 * e1000_irq_enable - Enable default interrupt generation settings
315 * @adapter: board private structure
316 **/
Joe Perches64798842008-07-11 15:17:02 -0700317static void e1000_irq_enable(struct e1000_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
Joe Perches1dc32912008-07-11 15:17:08 -0700319 struct e1000_hw *hw = &adapter->hw;
320
321 ew32(IMS, IMS_ENABLE_MASK);
322 E1000_WRITE_FLUSH();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323}
Adrian Bunk3ad2cc62005-10-30 16:53:34 +0100324
Joe Perches64798842008-07-11 15:17:02 -0700325static void e1000_update_mng_vlan(struct e1000_adapter *adapter)
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700326{
Joe Perches1dc32912008-07-11 15:17:08 -0700327 struct e1000_hw *hw = &adapter->hw;
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700328 struct net_device *netdev = adapter->netdev;
Joe Perches1dc32912008-07-11 15:17:08 -0700329 u16 vid = hw->mng_cookie.vlan_id;
Joe Perches406874a2008-04-03 10:06:32 -0700330 u16 old_vid = adapter->mng_vlan_id;
Jesse Brandeburg96838a42006-01-18 13:01:39 -0800331
Jiri Pirko5622e402011-07-21 03:26:31 +0000332 if (!e1000_vlan_used(adapter))
333 return;
334
335 if (!test_bit(vid, adapter->active_vlans)) {
336 if (hw->mng_cookie.status &
337 E1000_MNG_DHCP_COOKIE_STATUS_VLAN_SUPPORT) {
Patrick McHardy80d5c362013-04-19 02:04:28 +0000338 e1000_vlan_rx_add_vid(netdev, htons(ETH_P_8021Q), vid);
Jeff Kirsherc5f226f2006-03-02 18:17:55 -0800339 adapter->mng_vlan_id = vid;
Jiri Pirko5622e402011-07-21 03:26:31 +0000340 } else {
341 adapter->mng_vlan_id = E1000_MNG_VLAN_NONE;
342 }
343 if ((old_vid != (u16)E1000_MNG_VLAN_NONE) &&
344 (vid != old_vid) &&
345 !test_bit(old_vid, adapter->active_vlans))
Patrick McHardy80d5c362013-04-19 02:04:28 +0000346 e1000_vlan_rx_kill_vid(netdev, htons(ETH_P_8021Q),
347 old_vid);
Jiri Pirko5622e402011-07-21 03:26:31 +0000348 } else {
349 adapter->mng_vlan_id = vid;
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700350 }
351}
Jeff Kirsherb55ccb32006-01-12 16:50:30 -0800352
Joe Perches64798842008-07-11 15:17:02 -0700353static void e1000_init_manageability(struct e1000_adapter *adapter)
Jeff Garzik0fccd0e2006-12-15 10:56:10 -0500354{
Joe Perches1dc32912008-07-11 15:17:08 -0700355 struct e1000_hw *hw = &adapter->hw;
356
Jeff Garzik0fccd0e2006-12-15 10:56:10 -0500357 if (adapter->en_mng_pt) {
Joe Perches1dc32912008-07-11 15:17:08 -0700358 u32 manc = er32(MANC);
Jeff Garzik0fccd0e2006-12-15 10:56:10 -0500359
360 /* disable hardware interception of ARP */
361 manc &= ~(E1000_MANC_ARP_EN);
362
Joe Perches1dc32912008-07-11 15:17:08 -0700363 ew32(MANC, manc);
Jeff Garzik0fccd0e2006-12-15 10:56:10 -0500364 }
365}
366
Joe Perches64798842008-07-11 15:17:02 -0700367static void e1000_release_manageability(struct e1000_adapter *adapter)
Jeff Garzik0fccd0e2006-12-15 10:56:10 -0500368{
Joe Perches1dc32912008-07-11 15:17:08 -0700369 struct e1000_hw *hw = &adapter->hw;
370
Jeff Garzik0fccd0e2006-12-15 10:56:10 -0500371 if (adapter->en_mng_pt) {
Joe Perches1dc32912008-07-11 15:17:08 -0700372 u32 manc = er32(MANC);
Jeff Garzik0fccd0e2006-12-15 10:56:10 -0500373
374 /* re-enable hardware interception of ARP */
375 manc |= E1000_MANC_ARP_EN;
376
Joe Perches1dc32912008-07-11 15:17:08 -0700377 ew32(MANC, manc);
Jeff Garzik0fccd0e2006-12-15 10:56:10 -0500378 }
379}
380
Auke Koke0aac5a2007-03-06 08:57:21 -0800381/**
382 * e1000_configure - configure the hardware for RX and TX
383 * @adapter = private board structure
384 **/
385static void e1000_configure(struct e1000_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386{
387 struct net_device *netdev = adapter->netdev;
Auke Kok2db10a02006-06-27 09:06:28 -0700388 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Patrick McHardydb0ce502007-11-13 20:54:59 -0800390 e1000_set_rx_mode(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
392 e1000_restore_vlan(adapter);
Jeff Garzik0fccd0e2006-12-15 10:56:10 -0500393 e1000_init_manageability(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395 e1000_configure_tx(adapter);
396 e1000_setup_rctl(adapter);
397 e1000_configure_rx(adapter);
Jeff Kirsher72d64a42006-01-12 16:51:19 -0800398 /* call E1000_DESC_UNUSED which always leaves
399 * at least 1 descriptor unused to make sure
Jeff Kirsher6cfbd972013-02-09 12:49:21 +0000400 * next_to_use != next_to_clean
401 */
Jeff Kirsherf56799e2006-01-12 16:50:39 -0800402 for (i = 0; i < adapter->num_rx_queues; i++) {
Jeff Kirsher72d64a42006-01-12 16:51:19 -0800403 struct e1000_rx_ring *ring = &adapter->rx_ring[i];
Jeff Kirshera292ca62006-01-12 16:51:30 -0800404 adapter->alloc_rx_buf(adapter, ring,
Jeff Kirsher6cfbd972013-02-09 12:49:21 +0000405 E1000_DESC_UNUSED(ring));
Jeff Kirsherf56799e2006-01-12 16:50:39 -0800406 }
Auke Koke0aac5a2007-03-06 08:57:21 -0800407}
Jeff Kirsher7bfa4812006-01-12 16:50:41 -0800408
Auke Koke0aac5a2007-03-06 08:57:21 -0800409int e1000_up(struct e1000_adapter *adapter)
410{
Joe Perches1dc32912008-07-11 15:17:08 -0700411 struct e1000_hw *hw = &adapter->hw;
412
Auke Koke0aac5a2007-03-06 08:57:21 -0800413 /* hardware has been reset, we need to reload some things */
414 e1000_configure(adapter);
Malli Chilakala5de55622005-04-28 19:39:30 -0700415
Auke Kok1314bbf2006-09-27 12:54:02 -0700416 clear_bit(__E1000_DOWN, &adapter->flags);
417
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700418 napi_enable(&adapter->napi);
Francois Romieuc3570ac2008-07-11 15:17:38 -0700419
Auke Koke0aac5a2007-03-06 08:57:21 -0800420 e1000_irq_enable(adapter);
421
Jesse Brandeburg4cb9be72009-04-21 18:42:05 +0000422 netif_wake_queue(adapter->netdev);
423
Jesse Brandeburg79f3d392006-12-15 10:42:34 +0100424 /* fire a link change interrupt to start the watchdog */
Joe Perches1dc32912008-07-11 15:17:08 -0700425 ew32(ICS, E1000_ICS_LSC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 return 0;
427}
428
Auke Kok79f05bf2006-06-27 09:06:32 -0700429/**
430 * e1000_power_up_phy - restore link in case the phy was powered down
431 * @adapter: address of board private structure
432 *
433 * The phy may be powered down to save power and turn off link when the
434 * driver is unloaded and wake on lan is not enabled (among others)
435 * *** this routine MUST be followed by a call to e1000_reset ***
Auke Kok79f05bf2006-06-27 09:06:32 -0700436 **/
Jesse Brandeburgd6582662006-08-16 13:31:33 -0700437void e1000_power_up_phy(struct e1000_adapter *adapter)
Auke Kok79f05bf2006-06-27 09:06:32 -0700438{
Joe Perches1dc32912008-07-11 15:17:08 -0700439 struct e1000_hw *hw = &adapter->hw;
Joe Perches406874a2008-04-03 10:06:32 -0700440 u16 mii_reg = 0;
Auke Kok79f05bf2006-06-27 09:06:32 -0700441
442 /* Just clear the power down bit to wake the phy back up */
Joe Perches1dc32912008-07-11 15:17:08 -0700443 if (hw->media_type == e1000_media_type_copper) {
Auke Kok79f05bf2006-06-27 09:06:32 -0700444 /* according to the manual, the phy will retain its
Jeff Kirsher6cfbd972013-02-09 12:49:21 +0000445 * settings across a power-down/up cycle
446 */
Joe Perches1dc32912008-07-11 15:17:08 -0700447 e1000_read_phy_reg(hw, PHY_CTRL, &mii_reg);
Auke Kok79f05bf2006-06-27 09:06:32 -0700448 mii_reg &= ~MII_CR_POWER_DOWN;
Joe Perches1dc32912008-07-11 15:17:08 -0700449 e1000_write_phy_reg(hw, PHY_CTRL, mii_reg);
Auke Kok79f05bf2006-06-27 09:06:32 -0700450 }
451}
452
453static void e1000_power_down_phy(struct e1000_adapter *adapter)
454{
Joe Perches1dc32912008-07-11 15:17:08 -0700455 struct e1000_hw *hw = &adapter->hw;
456
Bruce Allan61c25052006-09-27 12:53:54 -0700457 /* Power down the PHY so no link is implied when interface is down *
Joe Perchesc3033b02008-03-21 11:06:25 -0700458 * The PHY cannot be powered down if any of the following is true *
Auke Kok79f05bf2006-06-27 09:06:32 -0700459 * (a) WoL is enabled
460 * (b) AMT is active
Jeff Kirsher6cfbd972013-02-09 12:49:21 +0000461 * (c) SoL/IDER session is active
462 */
Joe Perches1dc32912008-07-11 15:17:08 -0700463 if (!adapter->wol && hw->mac_type >= e1000_82540 &&
464 hw->media_type == e1000_media_type_copper) {
Joe Perches406874a2008-04-03 10:06:32 -0700465 u16 mii_reg = 0;
Bruce Allan61c25052006-09-27 12:53:54 -0700466
Joe Perches1dc32912008-07-11 15:17:08 -0700467 switch (hw->mac_type) {
Bruce Allan61c25052006-09-27 12:53:54 -0700468 case e1000_82540:
469 case e1000_82545:
470 case e1000_82545_rev_3:
471 case e1000_82546:
Dirk Brandewie5377a412011-01-06 14:29:54 +0000472 case e1000_ce4100:
Bruce Allan61c25052006-09-27 12:53:54 -0700473 case e1000_82546_rev_3:
474 case e1000_82541:
475 case e1000_82541_rev_2:
476 case e1000_82547:
477 case e1000_82547_rev_2:
Joe Perches1dc32912008-07-11 15:17:08 -0700478 if (er32(MANC) & E1000_MANC_SMBUS_EN)
Bruce Allan61c25052006-09-27 12:53:54 -0700479 goto out;
480 break;
Bruce Allan61c25052006-09-27 12:53:54 -0700481 default:
482 goto out;
483 }
Joe Perches1dc32912008-07-11 15:17:08 -0700484 e1000_read_phy_reg(hw, PHY_CTRL, &mii_reg);
Auke Kok79f05bf2006-06-27 09:06:32 -0700485 mii_reg |= MII_CR_POWER_DOWN;
Joe Perches1dc32912008-07-11 15:17:08 -0700486 e1000_write_phy_reg(hw, PHY_CTRL, mii_reg);
Jesse Brandeburg4e0d8f7d2011-10-05 07:24:46 +0000487 msleep(1);
Auke Kok79f05bf2006-06-27 09:06:32 -0700488 }
Bruce Allan61c25052006-09-27 12:53:54 -0700489out:
490 return;
Auke Kok79f05bf2006-06-27 09:06:32 -0700491}
492
Jesse Brandeburga4010af2011-10-05 07:24:41 +0000493static void e1000_down_and_stop(struct e1000_adapter *adapter)
494{
495 set_bit(__E1000_DOWN, &adapter->flags);
Tushar Dave8ce69092012-05-17 01:04:50 +0000496
497 /* Only kill reset task if adapter is not resetting */
498 if (!test_bit(__E1000_RESETTING, &adapter->flags))
499 cancel_work_sync(&adapter->reset_task);
500
Jesse Brandeburga4010af2011-10-05 07:24:41 +0000501 cancel_delayed_work_sync(&adapter->watchdog_task);
502 cancel_delayed_work_sync(&adapter->phy_info_task);
503 cancel_delayed_work_sync(&adapter->fifo_stall_task);
504}
505
Joe Perches64798842008-07-11 15:17:02 -0700506void e1000_down(struct e1000_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507{
Jesse Brandeburga6c42322009-03-25 21:59:22 +0000508 struct e1000_hw *hw = &adapter->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 struct net_device *netdev = adapter->netdev;
Jesse Brandeburga6c42322009-03-25 21:59:22 +0000510 u32 rctl, tctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Auke Kok1314bbf2006-09-27 12:54:02 -0700512
Jesse Brandeburga6c42322009-03-25 21:59:22 +0000513 /* disable receives in the hardware */
514 rctl = er32(RCTL);
515 ew32(RCTL, rctl & ~E1000_RCTL_EN);
516 /* flush and sleep below */
517
Jesse Brandeburg51851072009-09-25 12:17:01 +0000518 netif_tx_disable(netdev);
Jesse Brandeburga6c42322009-03-25 21:59:22 +0000519
520 /* disable transmits in the hardware */
521 tctl = er32(TCTL);
522 tctl &= ~E1000_TCTL_EN;
523 ew32(TCTL, tctl);
524 /* flush both disables and wait for them to finish */
525 E1000_WRITE_FLUSH();
526 msleep(10);
527
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700528 napi_disable(&adapter->napi);
Francois Romieuc3570ac2008-07-11 15:17:38 -0700529
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 e1000_irq_disable(adapter);
Jeff Kirsherc1605eb2006-03-02 18:16:38 -0800531
Jeff Kirsher6cfbd972013-02-09 12:49:21 +0000532 /* Setting DOWN must be after irq_disable to prevent
Anupam Chandaab088532010-11-21 09:54:21 -0800533 * a screaming interrupt. Setting DOWN also prevents
Jesse Brandeburga4010af2011-10-05 07:24:41 +0000534 * tasks from rescheduling.
Anupam Chandaab088532010-11-21 09:54:21 -0800535 */
Jesse Brandeburga4010af2011-10-05 07:24:41 +0000536 e1000_down_and_stop(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 adapter->link_speed = 0;
539 adapter->link_duplex = 0;
540 netif_carrier_off(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
542 e1000_reset(adapter);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -0400543 e1000_clean_all_tx_rings(adapter);
544 e1000_clean_all_rx_rings(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
stephen hemminger38df7a32010-10-21 07:50:57 +0000547static void e1000_reinit_safe(struct e1000_adapter *adapter)
Jesse Brandeburg338c15e2010-09-22 18:22:42 +0000548{
549 while (test_and_set_bit(__E1000_RESETTING, &adapter->flags))
550 msleep(1);
Jesse Brandeburg0ef4eed2011-10-05 07:24:51 +0000551 mutex_lock(&adapter->mutex);
Jesse Brandeburg338c15e2010-09-22 18:22:42 +0000552 e1000_down(adapter);
553 e1000_up(adapter);
Jesse Brandeburg0ef4eed2011-10-05 07:24:51 +0000554 mutex_unlock(&adapter->mutex);
Jesse Brandeburg338c15e2010-09-22 18:22:42 +0000555 clear_bit(__E1000_RESETTING, &adapter->flags);
556}
557
Joe Perches64798842008-07-11 15:17:02 -0700558void e1000_reinit_locked(struct e1000_adapter *adapter)
Auke Kok2db10a02006-06-27 09:06:28 -0700559{
Jesse Brandeburg338c15e2010-09-22 18:22:42 +0000560 /* if rtnl_lock is not held the call path is bogus */
561 ASSERT_RTNL();
Auke Kok2db10a02006-06-27 09:06:28 -0700562 WARN_ON(in_interrupt());
563 while (test_and_set_bit(__E1000_RESETTING, &adapter->flags))
564 msleep(1);
565 e1000_down(adapter);
566 e1000_up(adapter);
567 clear_bit(__E1000_RESETTING, &adapter->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568}
569
Joe Perches64798842008-07-11 15:17:02 -0700570void e1000_reset(struct e1000_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571{
Joe Perches1dc32912008-07-11 15:17:08 -0700572 struct e1000_hw *hw = &adapter->hw;
Joe Perches406874a2008-04-03 10:06:32 -0700573 u32 pba = 0, tx_space, min_tx_space, min_rx_space;
Joe Perchesc3033b02008-03-21 11:06:25 -0700574 bool legacy_pba_adjust = false;
Jesse Brandeburgb7cb8c22009-07-06 10:45:01 +0000575 u16 hwm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
577 /* Repartition Pba for greater than 9k mtu
578 * To take effect CTRL.RST is required.
579 */
580
Joe Perches1dc32912008-07-11 15:17:08 -0700581 switch (hw->mac_type) {
Bruce Allan018ea442006-12-15 10:39:45 +0100582 case e1000_82542_rev2_0:
583 case e1000_82542_rev2_1:
584 case e1000_82543:
585 case e1000_82544:
586 case e1000_82540:
587 case e1000_82541:
588 case e1000_82541_rev_2:
Joe Perchesc3033b02008-03-21 11:06:25 -0700589 legacy_pba_adjust = true;
Bruce Allan018ea442006-12-15 10:39:45 +0100590 pba = E1000_PBA_48K;
591 break;
592 case e1000_82545:
593 case e1000_82545_rev_3:
594 case e1000_82546:
Dirk Brandewie5377a412011-01-06 14:29:54 +0000595 case e1000_ce4100:
Bruce Allan018ea442006-12-15 10:39:45 +0100596 case e1000_82546_rev_3:
597 pba = E1000_PBA_48K;
598 break;
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700599 case e1000_82547:
Malli Chilakala0e6ef3e2005-04-28 19:44:14 -0700600 case e1000_82547_rev_2:
Joe Perchesc3033b02008-03-21 11:06:25 -0700601 legacy_pba_adjust = true;
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700602 pba = E1000_PBA_30K;
603 break;
Bruce Allan018ea442006-12-15 10:39:45 +0100604 case e1000_undefined:
605 case e1000_num_macs:
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700606 break;
607 }
608
Joe Perchesc3033b02008-03-21 11:06:25 -0700609 if (legacy_pba_adjust) {
Jesse Brandeburgb7cb8c22009-07-06 10:45:01 +0000610 if (hw->max_frame_size > E1000_RXBUFFER_8192)
Bruce Allan018ea442006-12-15 10:39:45 +0100611 pba -= 8; /* allocate more FIFO for Tx */
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700612
Joe Perches1dc32912008-07-11 15:17:08 -0700613 if (hw->mac_type == e1000_82547) {
Bruce Allan018ea442006-12-15 10:39:45 +0100614 adapter->tx_fifo_head = 0;
615 adapter->tx_head_addr = pba << E1000_TX_HEAD_ADDR_SHIFT;
616 adapter->tx_fifo_size =
617 (E1000_PBA_40K - pba) << E1000_PBA_BYTES_SHIFT;
618 atomic_set(&adapter->tx_fifo_stall, 0);
619 }
Jesse Brandeburgb7cb8c22009-07-06 10:45:01 +0000620 } else if (hw->max_frame_size > ETH_FRAME_LEN + ETH_FCS_LEN) {
Bruce Allan018ea442006-12-15 10:39:45 +0100621 /* adjust PBA for jumbo frames */
Joe Perches1dc32912008-07-11 15:17:08 -0700622 ew32(PBA, pba);
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700623
Bruce Allan018ea442006-12-15 10:39:45 +0100624 /* To maintain wire speed transmits, the Tx FIFO should be
Jesse Brandeburgb7cb8c22009-07-06 10:45:01 +0000625 * large enough to accommodate two full transmit packets,
Bruce Allan018ea442006-12-15 10:39:45 +0100626 * rounded up to the next 1KB and expressed in KB. Likewise,
Jesse Brandeburgb7cb8c22009-07-06 10:45:01 +0000627 * the Rx FIFO should be large enough to accommodate at least
Bruce Allan018ea442006-12-15 10:39:45 +0100628 * one full receive packet and is similarly rounded up and
Jeff Kirsher6cfbd972013-02-09 12:49:21 +0000629 * expressed in KB.
630 */
Joe Perches1dc32912008-07-11 15:17:08 -0700631 pba = er32(PBA);
Bruce Allan018ea442006-12-15 10:39:45 +0100632 /* upper 16 bits has Tx packet buffer allocation size in KB */
633 tx_space = pba >> 16;
634 /* lower 16 bits has Rx packet buffer allocation size in KB */
635 pba &= 0xffff;
Jeff Kirsher6cfbd972013-02-09 12:49:21 +0000636 /* the Tx fifo also stores 16 bytes of information about the Tx
Jesse Brandeburgb7cb8c22009-07-06 10:45:01 +0000637 * but don't include ethernet FCS because hardware appends it
638 */
639 min_tx_space = (hw->max_frame_size +
640 sizeof(struct e1000_tx_desc) -
641 ETH_FCS_LEN) * 2;
Milind Arun Choudhary9099cfb2007-04-27 13:55:29 -0700642 min_tx_space = ALIGN(min_tx_space, 1024);
Bruce Allan018ea442006-12-15 10:39:45 +0100643 min_tx_space >>= 10;
Jesse Brandeburgb7cb8c22009-07-06 10:45:01 +0000644 /* software strips receive CRC, so leave room for it */
645 min_rx_space = hw->max_frame_size;
Milind Arun Choudhary9099cfb2007-04-27 13:55:29 -0700646 min_rx_space = ALIGN(min_rx_space, 1024);
Bruce Allan018ea442006-12-15 10:39:45 +0100647 min_rx_space >>= 10;
648
649 /* If current Tx allocation is less than the min Tx FIFO size,
650 * and the min Tx FIFO size is less than the current Rx FIFO
Jeff Kirsher6cfbd972013-02-09 12:49:21 +0000651 * allocation, take space away from current Rx allocation
652 */
Bruce Allan018ea442006-12-15 10:39:45 +0100653 if (tx_space < min_tx_space &&
654 ((min_tx_space - tx_space) < pba)) {
655 pba = pba - (min_tx_space - tx_space);
656
657 /* PCI/PCIx hardware has PBA alignment constraints */
Joe Perches1dc32912008-07-11 15:17:08 -0700658 switch (hw->mac_type) {
Bruce Allan018ea442006-12-15 10:39:45 +0100659 case e1000_82545 ... e1000_82546_rev_3:
660 pba &= ~(E1000_PBA_8K - 1);
661 break;
662 default:
663 break;
664 }
665
Jeff Kirsher6cfbd972013-02-09 12:49:21 +0000666 /* if short on Rx space, Rx wins and must trump Tx
667 * adjustment or use Early Receive if available
668 */
Jesse Brandeburg1532ece2009-09-25 12:16:14 +0000669 if (pba < min_rx_space)
670 pba = min_rx_space;
Bruce Allan018ea442006-12-15 10:39:45 +0100671 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 }
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700673
Joe Perches1dc32912008-07-11 15:17:08 -0700674 ew32(PBA, pba);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
Jeff Kirsher6cfbd972013-02-09 12:49:21 +0000676 /* flow control settings:
Jesse Brandeburgb7cb8c22009-07-06 10:45:01 +0000677 * The high water mark must be low enough to fit one full frame
678 * (or the size used for early receive) above it in the Rx FIFO.
679 * Set it to the lower of:
680 * - 90% of the Rx FIFO size, and
681 * - the full Rx FIFO size minus the early receive size (for parts
682 * with ERT support assuming ERT set to E1000_ERT_2048), or
683 * - the full Rx FIFO size minus one full frame
684 */
685 hwm = min(((pba << 10) * 9 / 10),
686 ((pba << 10) - hw->max_frame_size));
Jeff Kirsherf11b7f82006-01-12 16:50:51 -0800687
Jesse Brandeburgb7cb8c22009-07-06 10:45:01 +0000688 hw->fc_high_water = hwm & 0xFFF8; /* 8-byte granularity */
689 hw->fc_low_water = hw->fc_high_water - 8;
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +0000690 hw->fc_pause_time = E1000_FC_PAUSE_TIME;
Joe Perches1dc32912008-07-11 15:17:08 -0700691 hw->fc_send_xon = 1;
692 hw->fc = hw->original_fc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700694 /* Allow time for pending master requests to run */
Joe Perches1dc32912008-07-11 15:17:08 -0700695 e1000_reset_hw(hw);
696 if (hw->mac_type >= e1000_82544)
697 ew32(WUC, 0);
Jeff Kirsher09ae3e82006-09-27 12:53:51 -0700698
Joe Perches1dc32912008-07-11 15:17:08 -0700699 if (e1000_init_hw(hw))
Emil Tantilovfeb8f472010-07-26 23:37:21 -0700700 e_dev_err("Hardware Error\n");
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700701 e1000_update_mng_vlan(adapter);
Jesse Brandeburg3d5460a2006-12-15 10:33:46 +0100702
703 /* if (adapter->hwflags & HWFLAGS_PHY_PWR_BIT) { */
Joe Perches1dc32912008-07-11 15:17:08 -0700704 if (hw->mac_type >= e1000_82544 &&
Joe Perches1dc32912008-07-11 15:17:08 -0700705 hw->autoneg == 1 &&
706 hw->autoneg_advertised == ADVERTISE_1000_FULL) {
707 u32 ctrl = er32(CTRL);
Jesse Brandeburg3d5460a2006-12-15 10:33:46 +0100708 /* clear phy power management bit if we are in gig only mode,
709 * which if enabled will attempt negotiation to 100Mb, which
Jeff Kirsher6cfbd972013-02-09 12:49:21 +0000710 * can cause a loss of link at power off or driver unload
711 */
Jesse Brandeburg3d5460a2006-12-15 10:33:46 +0100712 ctrl &= ~E1000_CTRL_SWDPIN3;
Joe Perches1dc32912008-07-11 15:17:08 -0700713 ew32(CTRL, ctrl);
Jesse Brandeburg3d5460a2006-12-15 10:33:46 +0100714 }
715
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 /* Enable h/w to recognize an 802.1Q VLAN Ethernet packet */
Joe Perches1dc32912008-07-11 15:17:08 -0700717 ew32(VET, ETHERNET_IEEE_VLAN_TYPE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
Joe Perches1dc32912008-07-11 15:17:08 -0700719 e1000_reset_adaptive(hw);
720 e1000_phy_get_info(hw, &adapter->phy_info);
Auke Kok9a53a202006-06-27 09:06:45 -0700721
Jeff Garzik0fccd0e2006-12-15 10:56:10 -0500722 e1000_release_manageability(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723}
724
Ben Hutchings1aa8b472012-07-10 10:56:59 +0000725/* Dump the eeprom for users having checksum issues */
Adrian Bunkb4ea8952008-02-01 08:21:28 -0800726static void e1000_dump_eeprom(struct e1000_adapter *adapter)
Auke Kok67b3c272007-12-17 13:50:23 -0800727{
728 struct net_device *netdev = adapter->netdev;
729 struct ethtool_eeprom eeprom;
730 const struct ethtool_ops *ops = netdev->ethtool_ops;
731 u8 *data;
732 int i;
733 u16 csum_old, csum_new = 0;
734
735 eeprom.len = ops->get_eeprom_len(netdev);
736 eeprom.offset = 0;
737
738 data = kmalloc(eeprom.len, GFP_KERNEL);
Joe Perchese404dec2012-01-29 12:56:23 +0000739 if (!data)
Auke Kok67b3c272007-12-17 13:50:23 -0800740 return;
Auke Kok67b3c272007-12-17 13:50:23 -0800741
742 ops->get_eeprom(netdev, &eeprom, data);
743
744 csum_old = (data[EEPROM_CHECKSUM_REG * 2]) +
745 (data[EEPROM_CHECKSUM_REG * 2 + 1] << 8);
746 for (i = 0; i < EEPROM_CHECKSUM_REG * 2; i += 2)
747 csum_new += data[i] + (data[i + 1] << 8);
748 csum_new = EEPROM_SUM - csum_new;
749
Emil Tantilov675ad472010-04-27 14:02:58 +0000750 pr_err("/*********************/\n");
751 pr_err("Current EEPROM Checksum : 0x%04x\n", csum_old);
752 pr_err("Calculated : 0x%04x\n", csum_new);
Auke Kok67b3c272007-12-17 13:50:23 -0800753
Emil Tantilov675ad472010-04-27 14:02:58 +0000754 pr_err("Offset Values\n");
755 pr_err("======== ======\n");
Auke Kok67b3c272007-12-17 13:50:23 -0800756 print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 16, 1, data, 128, 0);
757
Emil Tantilov675ad472010-04-27 14:02:58 +0000758 pr_err("Include this output when contacting your support provider.\n");
759 pr_err("This is not a software error! Something bad happened to\n");
760 pr_err("your hardware or EEPROM image. Ignoring this problem could\n");
761 pr_err("result in further problems, possibly loss of data,\n");
762 pr_err("corruption or system hangs!\n");
763 pr_err("The MAC Address will be reset to 00:00:00:00:00:00,\n");
764 pr_err("which is invalid and requires you to set the proper MAC\n");
765 pr_err("address manually before continuing to enable this network\n");
766 pr_err("device. Please inspect the EEPROM dump and report the\n");
767 pr_err("issue to your hardware vendor or Intel Customer Support.\n");
768 pr_err("/*********************/\n");
Auke Kok67b3c272007-12-17 13:50:23 -0800769
770 kfree(data);
771}
772
773/**
Taku Izumi81250292008-07-11 15:17:44 -0700774 * e1000_is_need_ioport - determine if an adapter needs ioport resources or not
775 * @pdev: PCI device information struct
776 *
777 * Return true if an adapter needs ioport resources
778 **/
779static int e1000_is_need_ioport(struct pci_dev *pdev)
780{
781 switch (pdev->device) {
782 case E1000_DEV_ID_82540EM:
783 case E1000_DEV_ID_82540EM_LOM:
784 case E1000_DEV_ID_82540EP:
785 case E1000_DEV_ID_82540EP_LOM:
786 case E1000_DEV_ID_82540EP_LP:
787 case E1000_DEV_ID_82541EI:
788 case E1000_DEV_ID_82541EI_MOBILE:
789 case E1000_DEV_ID_82541ER:
790 case E1000_DEV_ID_82541ER_LOM:
791 case E1000_DEV_ID_82541GI:
792 case E1000_DEV_ID_82541GI_LF:
793 case E1000_DEV_ID_82541GI_MOBILE:
794 case E1000_DEV_ID_82544EI_COPPER:
795 case E1000_DEV_ID_82544EI_FIBER:
796 case E1000_DEV_ID_82544GC_COPPER:
797 case E1000_DEV_ID_82544GC_LOM:
798 case E1000_DEV_ID_82545EM_COPPER:
799 case E1000_DEV_ID_82545EM_FIBER:
800 case E1000_DEV_ID_82546EB_COPPER:
801 case E1000_DEV_ID_82546EB_FIBER:
802 case E1000_DEV_ID_82546EB_QUAD_COPPER:
803 return true;
804 default:
805 return false;
806 }
807}
808
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000809static netdev_features_t e1000_fix_features(struct net_device *netdev,
810 netdev_features_t features)
Jiri Pirko5622e402011-07-21 03:26:31 +0000811{
Jeff Kirsher6cfbd972013-02-09 12:49:21 +0000812 /* Since there is no support for separate Rx/Tx vlan accel
813 * enable/disable make sure Tx flag is always in same state as Rx.
Jiri Pirko5622e402011-07-21 03:26:31 +0000814 */
Patrick McHardyf6469682013-04-19 02:04:27 +0000815 if (features & NETIF_F_HW_VLAN_CTAG_RX)
816 features |= NETIF_F_HW_VLAN_CTAG_TX;
Jiri Pirko5622e402011-07-21 03:26:31 +0000817 else
Patrick McHardyf6469682013-04-19 02:04:27 +0000818 features &= ~NETIF_F_HW_VLAN_CTAG_TX;
Jiri Pirko5622e402011-07-21 03:26:31 +0000819
820 return features;
821}
822
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000823static int e1000_set_features(struct net_device *netdev,
824 netdev_features_t features)
Michał Mirosławe97d3202011-06-08 08:36:42 +0000825{
826 struct e1000_adapter *adapter = netdev_priv(netdev);
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000827 netdev_features_t changed = features ^ netdev->features;
Michał Mirosławe97d3202011-06-08 08:36:42 +0000828
Patrick McHardyf6469682013-04-19 02:04:27 +0000829 if (changed & NETIF_F_HW_VLAN_CTAG_RX)
Jiri Pirko5622e402011-07-21 03:26:31 +0000830 e1000_vlan_mode(netdev, features);
831
Ben Greeare825b732012-04-04 06:01:29 +0000832 if (!(changed & (NETIF_F_RXCSUM | NETIF_F_RXALL)))
Michał Mirosławe97d3202011-06-08 08:36:42 +0000833 return 0;
834
Ben Greeare825b732012-04-04 06:01:29 +0000835 netdev->features = features;
Michał Mirosławe97d3202011-06-08 08:36:42 +0000836 adapter->rx_csum = !!(features & NETIF_F_RXCSUM);
837
838 if (netif_running(netdev))
839 e1000_reinit_locked(adapter);
840 else
841 e1000_reset(adapter);
842
843 return 0;
844}
845
Stephen Hemminger0e7614b2008-11-19 22:18:22 -0800846static const struct net_device_ops e1000_netdev_ops = {
847 .ndo_open = e1000_open,
848 .ndo_stop = e1000_close,
Stephen Hemminger00829822008-11-20 20:14:53 -0800849 .ndo_start_xmit = e1000_xmit_frame,
Stephen Hemminger0e7614b2008-11-19 22:18:22 -0800850 .ndo_get_stats = e1000_get_stats,
851 .ndo_set_rx_mode = e1000_set_rx_mode,
852 .ndo_set_mac_address = e1000_set_mac,
Jiri Pirko5622e402011-07-21 03:26:31 +0000853 .ndo_tx_timeout = e1000_tx_timeout,
Stephen Hemminger0e7614b2008-11-19 22:18:22 -0800854 .ndo_change_mtu = e1000_change_mtu,
855 .ndo_do_ioctl = e1000_ioctl,
856 .ndo_validate_addr = eth_validate_addr,
Stephen Hemminger0e7614b2008-11-19 22:18:22 -0800857 .ndo_vlan_rx_add_vid = e1000_vlan_rx_add_vid,
858 .ndo_vlan_rx_kill_vid = e1000_vlan_rx_kill_vid,
859#ifdef CONFIG_NET_POLL_CONTROLLER
860 .ndo_poll_controller = e1000_netpoll,
861#endif
Jiri Pirko5622e402011-07-21 03:26:31 +0000862 .ndo_fix_features = e1000_fix_features,
863 .ndo_set_features = e1000_set_features,
Stephen Hemminger0e7614b2008-11-19 22:18:22 -0800864};
865
Taku Izumi81250292008-07-11 15:17:44 -0700866/**
Jesse Brandeburge508be12010-09-07 21:01:12 +0000867 * e1000_init_hw_struct - initialize members of hw struct
868 * @adapter: board private struct
869 * @hw: structure used by e1000_hw.c
870 *
871 * Factors out initialization of the e1000_hw struct to its own function
872 * that can be called very early at init (just after struct allocation).
873 * Fields are initialized based on PCI device information and
874 * OS network device settings (MTU size).
875 * Returns negative error codes if MAC type setup fails.
876 */
877static int e1000_init_hw_struct(struct e1000_adapter *adapter,
878 struct e1000_hw *hw)
879{
880 struct pci_dev *pdev = adapter->pdev;
881
882 /* PCI config space info */
883 hw->vendor_id = pdev->vendor;
884 hw->device_id = pdev->device;
885 hw->subsystem_vendor_id = pdev->subsystem_vendor;
886 hw->subsystem_id = pdev->subsystem_device;
887 hw->revision_id = pdev->revision;
888
889 pci_read_config_word(pdev, PCI_COMMAND, &hw->pci_cmd_word);
890
891 hw->max_frame_size = adapter->netdev->mtu +
892 ENET_HEADER_SIZE + ETHERNET_FCS_SIZE;
893 hw->min_frame_size = MINIMUM_ETHERNET_FRAME_SIZE;
894
895 /* identify the MAC */
896 if (e1000_set_mac_type(hw)) {
897 e_err(probe, "Unknown MAC Type\n");
898 return -EIO;
899 }
900
901 switch (hw->mac_type) {
902 default:
903 break;
904 case e1000_82541:
905 case e1000_82547:
906 case e1000_82541_rev_2:
907 case e1000_82547_rev_2:
908 hw->phy_init_script = 1;
909 break;
910 }
911
912 e1000_set_media_type(hw);
913 e1000_get_bus_info(hw);
914
915 hw->wait_autoneg_complete = false;
916 hw->tbi_compatibility_en = true;
917 hw->adaptive_ifs = true;
918
919 /* Copper options */
920
921 if (hw->media_type == e1000_media_type_copper) {
922 hw->mdix = AUTO_ALL_MODES;
923 hw->disable_polarity_correction = false;
924 hw->master_slave = E1000_MASTER_SLAVE;
925 }
926
927 return 0;
928}
929
930/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 * e1000_probe - Device Initialization Routine
932 * @pdev: PCI device information struct
933 * @ent: entry in e1000_pci_tbl
934 *
935 * Returns 0 on success, negative on failure
936 *
937 * e1000_probe initializes an adapter identified by a pci_dev structure.
938 * The OS initialization, configuring of the adapter private structure,
939 * and a hardware reset occur.
940 **/
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +0000941static int e1000_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942{
943 struct net_device *netdev;
944 struct e1000_adapter *adapter;
Joe Perches1dc32912008-07-11 15:17:08 -0700945 struct e1000_hw *hw;
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700946
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 static int cards_found = 0;
Jesse Brandeburg120cd572006-08-31 14:27:46 -0700948 static int global_quad_port_a = 0; /* global ksp3 port a indication */
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700949 int i, err, pci_using_dac;
Joe Perches406874a2008-04-03 10:06:32 -0700950 u16 eeprom_data = 0;
Dirk Brandewie5377a412011-01-06 14:29:54 +0000951 u16 tmp = 0;
Joe Perches406874a2008-04-03 10:06:32 -0700952 u16 eeprom_apme_mask = E1000_EEPROM_APME;
Taku Izumi81250292008-07-11 15:17:44 -0700953 int bars, need_ioport;
Joe Perches0795af52007-10-03 17:59:30 -0700954
Taku Izumi81250292008-07-11 15:17:44 -0700955 /* do not allocate ioport bars when not needed */
956 need_ioport = e1000_is_need_ioport(pdev);
957 if (need_ioport) {
958 bars = pci_select_bars(pdev, IORESOURCE_MEM | IORESOURCE_IO);
959 err = pci_enable_device(pdev);
960 } else {
961 bars = pci_select_bars(pdev, IORESOURCE_MEM);
Karsten Keil4d7155b2009-02-03 15:18:01 -0800962 err = pci_enable_device_mem(pdev);
Taku Izumi81250292008-07-11 15:17:44 -0700963 }
Joe Perchesc7be73b2008-07-11 15:17:28 -0700964 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 return err;
966
Taku Izumi81250292008-07-11 15:17:44 -0700967 err = pci_request_selected_regions(pdev, bars, e1000_driver_name);
Joe Perchesc7be73b2008-07-11 15:17:28 -0700968 if (err)
Vasily Averin6dd62ab2006-08-28 14:56:22 -0700969 goto err_pci_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
971 pci_set_master(pdev);
Nick Nunleydbb5aae2010-02-03 14:49:48 +0000972 err = pci_save_state(pdev);
973 if (err)
974 goto err_alloc_etherdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
Vasily Averin6dd62ab2006-08-28 14:56:22 -0700976 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 netdev = alloc_etherdev(sizeof(struct e1000_adapter));
Vasily Averin6dd62ab2006-08-28 14:56:22 -0700978 if (!netdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 goto err_alloc_etherdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 SET_NETDEV_DEV(netdev, &pdev->dev);
982
983 pci_set_drvdata(pdev, netdev);
Malli Chilakala60490fe2005-06-17 17:41:45 -0700984 adapter = netdev_priv(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 adapter->netdev = netdev;
986 adapter->pdev = pdev;
stephen hemmingerb3f4d592012-03-13 06:04:20 +0000987 adapter->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
Taku Izumi81250292008-07-11 15:17:44 -0700988 adapter->bars = bars;
989 adapter->need_ioport = need_ioport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
Joe Perches1dc32912008-07-11 15:17:08 -0700991 hw = &adapter->hw;
992 hw->back = adapter;
993
Vasily Averin6dd62ab2006-08-28 14:56:22 -0700994 err = -EIO;
Arjan van de Ven275f1652008-10-20 21:42:39 -0700995 hw->hw_addr = pci_ioremap_bar(pdev, BAR_0);
Joe Perches1dc32912008-07-11 15:17:08 -0700996 if (!hw->hw_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 goto err_ioremap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
Taku Izumi81250292008-07-11 15:17:44 -0700999 if (adapter->need_ioport) {
1000 for (i = BAR_1; i <= BAR_5; i++) {
1001 if (pci_resource_len(pdev, i) == 0)
1002 continue;
1003 if (pci_resource_flags(pdev, i) & IORESOURCE_IO) {
1004 hw->io_base = pci_resource_start(pdev, i);
1005 break;
1006 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 }
1008 }
1009
Jesse Brandeburge508be12010-09-07 21:01:12 +00001010 /* make ready for any if (hw->...) below */
1011 err = e1000_init_hw_struct(adapter, hw);
1012 if (err)
1013 goto err_sw_init;
1014
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00001015 /* there is a workaround being applied below that limits
Jesse Brandeburge508be12010-09-07 21:01:12 +00001016 * 64-bit DMA addresses to 64-bit hardware. There are some
1017 * 32-bit adapters that Tx hang when given 64-bit DMA addresses
1018 */
1019 pci_using_dac = 0;
1020 if ((hw->bus_type == e1000_bus_type_pcix) &&
Russell King9931a262013-06-26 23:49:11 +01001021 !dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64))) {
Jesse Brandeburge508be12010-09-07 21:01:12 +00001022 pci_using_dac = 1;
Jesse Brandeburge508be12010-09-07 21:01:12 +00001023 } else {
Russell King9931a262013-06-26 23:49:11 +01001024 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
Dean Nelson19a0b672010-11-11 05:50:25 +00001025 if (err) {
1026 pr_err("No usable DMA config, aborting\n");
1027 goto err_dma;
1028 }
Jesse Brandeburge508be12010-09-07 21:01:12 +00001029 }
1030
Stephen Hemminger0e7614b2008-11-19 22:18:22 -08001031 netdev->netdev_ops = &e1000_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 e1000_set_ethtool_ops(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 netdev->watchdog_timeo = 5 * HZ;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001034 netif_napi_add(netdev, &adapter->napi, e1000_clean, 64);
Stephen Hemminger0e7614b2008-11-19 22:18:22 -08001035
Auke Kok0eb5a342006-09-27 12:53:17 -07001036 strncpy(netdev->name, pci_name(pdev), sizeof(netdev->name) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 adapter->bd_number = cards_found;
1039
1040 /* setup the private structure */
1041
Joe Perchesc7be73b2008-07-11 15:17:28 -07001042 err = e1000_sw_init(adapter);
1043 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 goto err_sw_init;
1045
Vasily Averin6dd62ab2006-08-28 14:56:22 -07001046 err = -EIO;
Dirk Brandewie5377a412011-01-06 14:29:54 +00001047 if (hw->mac_type == e1000_ce4100) {
Florian Fainelli13acde82012-01-04 20:23:35 +00001048 hw->ce4100_gbe_mdio_base_virt =
1049 ioremap(pci_resource_start(pdev, BAR_1),
Dirk Brandewie5377a412011-01-06 14:29:54 +00001050 pci_resource_len(pdev, BAR_1));
1051
Florian Fainelli13acde82012-01-04 20:23:35 +00001052 if (!hw->ce4100_gbe_mdio_base_virt)
Dirk Brandewie5377a412011-01-06 14:29:54 +00001053 goto err_mdio_ioremap;
1054 }
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001055
Joe Perches1dc32912008-07-11 15:17:08 -07001056 if (hw->mac_type >= e1000_82543) {
Michał Mirosławe97d3202011-06-08 08:36:42 +00001057 netdev->hw_features = NETIF_F_SG |
Jiri Pirko5622e402011-07-21 03:26:31 +00001058 NETIF_F_HW_CSUM |
Patrick McHardyf6469682013-04-19 02:04:27 +00001059 NETIF_F_HW_VLAN_CTAG_RX;
1060 netdev->features = NETIF_F_HW_VLAN_CTAG_TX |
1061 NETIF_F_HW_VLAN_CTAG_FILTER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 }
1063
Joe Perches1dc32912008-07-11 15:17:08 -07001064 if ((hw->mac_type >= e1000_82544) &&
1065 (hw->mac_type != e1000_82547))
Michał Mirosławe97d3202011-06-08 08:36:42 +00001066 netdev->hw_features |= NETIF_F_TSO;
1067
Ben Greear11a78dc2012-02-11 15:40:01 +00001068 netdev->priv_flags |= IFF_SUPP_NOFCS;
1069
Michał Mirosławe97d3202011-06-08 08:36:42 +00001070 netdev->features |= netdev->hw_features;
Tushar Dave75006732012-06-12 13:03:29 +00001071 netdev->hw_features |= (NETIF_F_RXCSUM |
1072 NETIF_F_RXALL |
1073 NETIF_F_RXFCS);
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001074
Yi Zou7b872a52010-09-22 17:57:58 +00001075 if (pci_using_dac) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 netdev->features |= NETIF_F_HIGHDMA;
Yi Zou7b872a52010-09-22 17:57:58 +00001077 netdev->vlan_features |= NETIF_F_HIGHDMA;
1078 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
Tushar Dave75006732012-06-12 13:03:29 +00001080 netdev->vlan_features |= (NETIF_F_TSO |
1081 NETIF_F_HW_CSUM |
1082 NETIF_F_SG);
Patrick McHardy20501a62008-10-11 12:25:59 -07001083
Jiri Pirko01789342011-08-16 06:29:00 +00001084 netdev->priv_flags |= IFF_UNICAST_FLT;
1085
Joe Perches1dc32912008-07-11 15:17:08 -07001086 adapter->en_mng_pt = e1000_enable_mng_pass_thru(hw);
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001087
Auke Kokcd94dd02006-06-27 09:08:22 -07001088 /* initialize eeprom parameters */
Joe Perches1dc32912008-07-11 15:17:08 -07001089 if (e1000_init_eeprom_params(hw)) {
Emil Tantilovfeb8f472010-07-26 23:37:21 -07001090 e_err(probe, "EEPROM initialization failed\n");
Vasily Averin6dd62ab2006-08-28 14:56:22 -07001091 goto err_eeprom;
Auke Kokcd94dd02006-06-27 09:08:22 -07001092 }
1093
Jesse Brandeburg96838a42006-01-18 13:01:39 -08001094 /* before reading the EEPROM, reset the controller to
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00001095 * put the device in a known good starting state
1096 */
Jesse Brandeburg96838a42006-01-18 13:01:39 -08001097
Joe Perches1dc32912008-07-11 15:17:08 -07001098 e1000_reset_hw(hw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
1100 /* make sure the EEPROM is good */
Joe Perches1dc32912008-07-11 15:17:08 -07001101 if (e1000_validate_eeprom_checksum(hw) < 0) {
Emil Tantilovfeb8f472010-07-26 23:37:21 -07001102 e_err(probe, "The EEPROM Checksum Is Not Valid\n");
Auke Kok67b3c272007-12-17 13:50:23 -08001103 e1000_dump_eeprom(adapter);
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00001104 /* set MAC address to all zeroes to invalidate and temporary
Auke Kok67b3c272007-12-17 13:50:23 -08001105 * disable this device for the user. This blocks regular
1106 * traffic while still permitting ethtool ioctls from reaching
1107 * the hardware as well as allowing the user to run the
1108 * interface after manually setting a hw addr using
1109 * `ip set address`
1110 */
Joe Perches1dc32912008-07-11 15:17:08 -07001111 memset(hw->mac_addr, 0, netdev->addr_len);
Auke Kok67b3c272007-12-17 13:50:23 -08001112 } else {
1113 /* copy the MAC address out of the EEPROM */
Joe Perches1dc32912008-07-11 15:17:08 -07001114 if (e1000_read_mac_addr(hw))
Emil Tantilovfeb8f472010-07-26 23:37:21 -07001115 e_err(probe, "EEPROM Read Error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 }
Auke Kok67b3c272007-12-17 13:50:23 -08001117 /* don't block initalization here due to bad MAC address */
Joe Perches1dc32912008-07-11 15:17:08 -07001118 memcpy(netdev->dev_addr, hw->mac_addr, netdev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
Jiri Pirkoaaeb6cd2013-01-08 01:38:26 +00001120 if (!is_valid_ether_addr(netdev->dev_addr))
Emil Tantilovfeb8f472010-07-26 23:37:21 -07001121 e_err(probe, "Invalid MAC Address\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
Jesse Brandeburga4010af2011-10-05 07:24:41 +00001124 INIT_DELAYED_WORK(&adapter->watchdog_task, e1000_watchdog);
1125 INIT_DELAYED_WORK(&adapter->fifo_stall_task,
1126 e1000_82547_tx_fifo_stall_task);
1127 INIT_DELAYED_WORK(&adapter->phy_info_task, e1000_update_phy_info_task);
David Howells65f27f32006-11-22 14:55:48 +00001128 INIT_WORK(&adapter->reset_task, e1000_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 e1000_check_options(adapter);
1131
1132 /* Initial Wake on LAN setting
1133 * If APM wake is enabled in the EEPROM,
1134 * enable the ACPI Magic Packet filter
1135 */
1136
Joe Perches1dc32912008-07-11 15:17:08 -07001137 switch (hw->mac_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 case e1000_82542_rev2_0:
1139 case e1000_82542_rev2_1:
1140 case e1000_82543:
1141 break;
1142 case e1000_82544:
Joe Perches1dc32912008-07-11 15:17:08 -07001143 e1000_read_eeprom(hw,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 EEPROM_INIT_CONTROL2_REG, 1, &eeprom_data);
1145 eeprom_apme_mask = E1000_EEPROM_82544_APM;
1146 break;
1147 case e1000_82546:
1148 case e1000_82546_rev_3:
Joe Perches1dc32912008-07-11 15:17:08 -07001149 if (er32(STATUS) & E1000_STATUS_FUNC_1){
1150 e1000_read_eeprom(hw,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 EEPROM_INIT_CONTROL3_PORT_B, 1, &eeprom_data);
1152 break;
1153 }
1154 /* Fall Through */
1155 default:
Joe Perches1dc32912008-07-11 15:17:08 -07001156 e1000_read_eeprom(hw,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 EEPROM_INIT_CONTROL3_PORT_A, 1, &eeprom_data);
1158 break;
1159 }
Jesse Brandeburg96838a42006-01-18 13:01:39 -08001160 if (eeprom_data & eeprom_apme_mask)
Jesse Brandeburg120cd572006-08-31 14:27:46 -07001161 adapter->eeprom_wol |= E1000_WUFC_MAG;
1162
1163 /* now that we have the eeprom settings, apply the special cases
1164 * where the eeprom may be wrong or the board simply won't support
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00001165 * wake on lan on a particular port
1166 */
Jesse Brandeburg120cd572006-08-31 14:27:46 -07001167 switch (pdev->device) {
1168 case E1000_DEV_ID_82546GB_PCIE:
1169 adapter->eeprom_wol = 0;
1170 break;
1171 case E1000_DEV_ID_82546EB_FIBER:
1172 case E1000_DEV_ID_82546GB_FIBER:
Jesse Brandeburg120cd572006-08-31 14:27:46 -07001173 /* Wake events only supported on port A for dual fiber
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00001174 * regardless of eeprom setting
1175 */
Joe Perches1dc32912008-07-11 15:17:08 -07001176 if (er32(STATUS) & E1000_STATUS_FUNC_1)
Jesse Brandeburg120cd572006-08-31 14:27:46 -07001177 adapter->eeprom_wol = 0;
1178 break;
1179 case E1000_DEV_ID_82546GB_QUAD_COPPER_KSP3:
1180 /* if quad port adapter, disable WoL on all but port A */
1181 if (global_quad_port_a != 0)
1182 adapter->eeprom_wol = 0;
1183 else
Rusty Russell3db1cd52011-12-19 13:56:45 +00001184 adapter->quad_port_a = true;
Jesse Brandeburg120cd572006-08-31 14:27:46 -07001185 /* Reset for multiple quad port adapters */
1186 if (++global_quad_port_a == 4)
1187 global_quad_port_a = 0;
1188 break;
1189 }
1190
1191 /* initialize the wol settings based on the eeprom settings */
1192 adapter->wol = adapter->eeprom_wol;
\"Rafael J. Wysocki\de126482008-11-07 20:30:19 +00001193 device_set_wakeup_enable(&adapter->pdev->dev, adapter->wol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
Dirk Brandewie5377a412011-01-06 14:29:54 +00001195 /* Auto detect PHY address */
1196 if (hw->mac_type == e1000_ce4100) {
1197 for (i = 0; i < 32; i++) {
1198 hw->phy_addr = i;
1199 e1000_read_phy_reg(hw, PHY_ID2, &tmp);
1200 if (tmp == 0 || tmp == 0xFF) {
1201 if (i == 31)
1202 goto err_eeprom;
1203 continue;
1204 } else
1205 break;
1206 }
1207 }
1208
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 /* reset the hardware with the new settings */
1210 e1000_reset(adapter);
1211
Auke Kok416b5d12007-06-01 10:22:39 -07001212 strcpy(netdev->name, "eth%d");
Joe Perchesc7be73b2008-07-11 15:17:28 -07001213 err = register_netdev(netdev);
1214 if (err)
Auke Kok416b5d12007-06-01 10:22:39 -07001215 goto err_register;
Auke Kok1314bbf2006-09-27 12:54:02 -07001216
Jiri Pirko52f55092012-03-20 18:10:01 +00001217 e1000_vlan_filter_on_off(adapter, false);
Jiri Pirko5622e402011-07-21 03:26:31 +00001218
Emil Tantilov675ad472010-04-27 14:02:58 +00001219 /* print bus type/speed/width info */
Emil Tantilovfeb8f472010-07-26 23:37:21 -07001220 e_info(probe, "(PCI%s:%dMHz:%d-bit) %pM\n",
Joe Perches7837e582010-06-11 12:51:49 +00001221 ((hw->bus_type == e1000_bus_type_pcix) ? "-X" : ""),
1222 ((hw->bus_speed == e1000_bus_speed_133) ? 133 :
1223 (hw->bus_speed == e1000_bus_speed_120) ? 120 :
1224 (hw->bus_speed == e1000_bus_speed_100) ? 100 :
1225 (hw->bus_speed == e1000_bus_speed_66) ? 66 : 33),
1226 ((hw->bus_width == e1000_bus_width_64) ? 64 : 32),
1227 netdev->dev_addr);
Emil Tantilov675ad472010-04-27 14:02:58 +00001228
Jesse Brandeburgeb62efd2009-04-17 20:44:36 +00001229 /* carrier off reporting is important to ethtool even BEFORE open */
1230 netif_carrier_off(netdev);
1231
Emil Tantilovfeb8f472010-07-26 23:37:21 -07001232 e_info(probe, "Intel(R) PRO/1000 Network Connection\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
1234 cards_found++;
1235 return 0;
1236
1237err_register:
Vasily Averin6dd62ab2006-08-28 14:56:22 -07001238err_eeprom:
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00001239 e1000_phy_hw_reset(hw);
Vasily Averin6dd62ab2006-08-28 14:56:22 -07001240
Joe Perches1dc32912008-07-11 15:17:08 -07001241 if (hw->flash_address)
1242 iounmap(hw->flash_address);
Vasily Averin6dd62ab2006-08-28 14:56:22 -07001243 kfree(adapter->tx_ring);
1244 kfree(adapter->rx_ring);
Jesse Brandeburge508be12010-09-07 21:01:12 +00001245err_dma:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246err_sw_init:
Dirk Brandewie5377a412011-01-06 14:29:54 +00001247err_mdio_ioremap:
Florian Fainelli13acde82012-01-04 20:23:35 +00001248 iounmap(hw->ce4100_gbe_mdio_base_virt);
Joe Perches1dc32912008-07-11 15:17:08 -07001249 iounmap(hw->hw_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250err_ioremap:
1251 free_netdev(netdev);
1252err_alloc_etherdev:
Taku Izumi81250292008-07-11 15:17:44 -07001253 pci_release_selected_regions(pdev, bars);
Vasily Averin6dd62ab2006-08-28 14:56:22 -07001254err_pci_reg:
Vasily Averin6dd62ab2006-08-28 14:56:22 -07001255 pci_disable_device(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 return err;
1257}
1258
1259/**
1260 * e1000_remove - Device Removal Routine
1261 * @pdev: PCI device information struct
1262 *
1263 * e1000_remove is called by the PCI subsystem to alert the driver
1264 * that it should release a PCI device. The could be caused by a
1265 * Hot-Plug event, or because the driver is going to be removed from
1266 * memory.
1267 **/
Bill Pemberton9f9a12f2012-12-03 09:24:25 -05001268static void e1000_remove(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269{
1270 struct net_device *netdev = pci_get_drvdata(pdev);
Malli Chilakala60490fe2005-06-17 17:41:45 -07001271 struct e1000_adapter *adapter = netdev_priv(netdev);
Joe Perches1dc32912008-07-11 15:17:08 -07001272 struct e1000_hw *hw = &adapter->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
Jesse Brandeburga4010af2011-10-05 07:24:41 +00001274 e1000_down_and_stop(adapter);
Jeff Garzik0fccd0e2006-12-15 10:56:10 -05001275 e1000_release_manageability(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001277 unregister_netdev(netdev);
1278
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00001279 e1000_phy_hw_reset(hw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
Mallikarjuna R Chilakala24025e4ec2005-10-04 07:03:23 -04001281 kfree(adapter->tx_ring);
1282 kfree(adapter->rx_ring);
Mallikarjuna R Chilakala24025e4ec2005-10-04 07:03:23 -04001283
Florian Fainelli1c267502012-01-04 20:23:34 +00001284 if (hw->mac_type == e1000_ce4100)
Florian Fainelli13acde82012-01-04 20:23:35 +00001285 iounmap(hw->ce4100_gbe_mdio_base_virt);
Joe Perches1dc32912008-07-11 15:17:08 -07001286 iounmap(hw->hw_addr);
1287 if (hw->flash_address)
1288 iounmap(hw->flash_address);
Taku Izumi81250292008-07-11 15:17:44 -07001289 pci_release_selected_regions(pdev, adapter->bars);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
1291 free_netdev(netdev);
1292
1293 pci_disable_device(pdev);
1294}
1295
1296/**
1297 * e1000_sw_init - Initialize general software structures (struct e1000_adapter)
1298 * @adapter: board private structure to initialize
1299 *
1300 * e1000_sw_init initializes the Adapter private data structure.
Jesse Brandeburge508be12010-09-07 21:01:12 +00001301 * e1000_init_hw_struct MUST be called before this function
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 **/
Bill Pemberton9f9a12f2012-12-03 09:24:25 -05001303static int e1000_sw_init(struct e1000_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304{
Auke Kokeb0f8052006-07-14 16:14:48 -07001305 adapter->rx_buffer_len = MAXIMUM_ETHERNET_VLAN_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
Jeff Kirsherf56799e2006-01-12 16:50:39 -08001307 adapter->num_tx_queues = 1;
1308 adapter->num_rx_queues = 1;
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001309
1310 if (e1000_alloc_queues(adapter)) {
Emil Tantilovfeb8f472010-07-26 23:37:21 -07001311 e_err(probe, "Unable to allocate memory for queues\n");
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001312 return -ENOMEM;
1313 }
1314
Herbert Xu47313052007-05-29 15:07:31 -07001315 /* Explicitly disable IRQ since the NIC can be in any state. */
Herbert Xu47313052007-05-29 15:07:31 -07001316 e1000_irq_disable(adapter);
1317
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 spin_lock_init(&adapter->stats_lock);
Jesse Brandeburg0ef4eed2011-10-05 07:24:51 +00001319 mutex_init(&adapter->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
Auke Kok1314bbf2006-09-27 12:54:02 -07001321 set_bit(__E1000_DOWN, &adapter->flags);
1322
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 return 0;
1324}
1325
1326/**
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001327 * e1000_alloc_queues - Allocate memory for all rings
1328 * @adapter: board private structure to initialize
1329 *
1330 * We allocate one ring per queue at run-time since we don't know the
Wang Chen3e1d7cd2008-12-03 22:07:10 -08001331 * number of queues at compile-time.
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001332 **/
Bill Pemberton9f9a12f2012-12-03 09:24:25 -05001333static int e1000_alloc_queues(struct e1000_adapter *adapter)
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001334{
Yan Burman1c7e5b12007-03-06 08:58:04 -08001335 adapter->tx_ring = kcalloc(adapter->num_tx_queues,
1336 sizeof(struct e1000_tx_ring), GFP_KERNEL);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001337 if (!adapter->tx_ring)
1338 return -ENOMEM;
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001339
Yan Burman1c7e5b12007-03-06 08:58:04 -08001340 adapter->rx_ring = kcalloc(adapter->num_rx_queues,
1341 sizeof(struct e1000_rx_ring), GFP_KERNEL);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001342 if (!adapter->rx_ring) {
1343 kfree(adapter->tx_ring);
1344 return -ENOMEM;
1345 }
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001346
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001347 return E1000_SUCCESS;
1348}
1349
1350/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 * e1000_open - Called when a network interface is made active
1352 * @netdev: network interface device structure
1353 *
1354 * Returns 0 on success, negative value on failure
1355 *
1356 * The open entry point is called when a network interface is made
1357 * active by the system (IFF_UP). At this point all resources needed
1358 * for transmit and receive operations are allocated, the interrupt
Jesse Brandeburga4010af2011-10-05 07:24:41 +00001359 * handler is registered with the OS, the watchdog task is started,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 * and the stack is notified that the interface is ready.
1361 **/
Joe Perches64798842008-07-11 15:17:02 -07001362static int e1000_open(struct net_device *netdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363{
Malli Chilakala60490fe2005-06-17 17:41:45 -07001364 struct e1000_adapter *adapter = netdev_priv(netdev);
Joe Perches1dc32912008-07-11 15:17:08 -07001365 struct e1000_hw *hw = &adapter->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 int err;
1367
Auke Kok2db10a02006-06-27 09:06:28 -07001368 /* disallow open during test */
Auke Kok1314bbf2006-09-27 12:54:02 -07001369 if (test_bit(__E1000_TESTING, &adapter->flags))
Auke Kok2db10a02006-06-27 09:06:28 -07001370 return -EBUSY;
1371
Jesse Brandeburgeb62efd2009-04-17 20:44:36 +00001372 netif_carrier_off(netdev);
1373
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 /* allocate transmit descriptors */
Auke Koke0aac5a2007-03-06 08:57:21 -08001375 err = e1000_setup_all_tx_resources(adapter);
1376 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 goto err_setup_tx;
1378
1379 /* allocate receive descriptors */
Auke Koke0aac5a2007-03-06 08:57:21 -08001380 err = e1000_setup_all_rx_resources(adapter);
Linus Torvaldsb5bf28c2007-02-21 11:21:44 -08001381 if (err)
Auke Koke0aac5a2007-03-06 08:57:21 -08001382 goto err_setup_rx;
Linus Torvaldsb5bf28c2007-02-21 11:21:44 -08001383
Auke Kok79f05bf2006-06-27 09:06:32 -07001384 e1000_power_up_phy(adapter);
1385
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001386 adapter->mng_vlan_id = E1000_MNG_VLAN_NONE;
Joe Perches1dc32912008-07-11 15:17:08 -07001387 if ((hw->mng_cookie.status &
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001388 E1000_MNG_DHCP_COOKIE_STATUS_VLAN_SUPPORT)) {
1389 e1000_update_mng_vlan(adapter);
1390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
Auke Koke0aac5a2007-03-06 08:57:21 -08001392 /* before we allocate an interrupt, we must be ready to handle it.
1393 * Setting DEBUG_SHIRQ in the kernel makes it fire an interrupt
1394 * as soon as we call pci_request_irq, so we have to setup our
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00001395 * clean_rx handler before we do so.
1396 */
Auke Koke0aac5a2007-03-06 08:57:21 -08001397 e1000_configure(adapter);
1398
1399 err = e1000_request_irq(adapter);
1400 if (err)
1401 goto err_req_irq;
1402
1403 /* From here on the code is the same as e1000_up() */
1404 clear_bit(__E1000_DOWN, &adapter->flags);
1405
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001406 napi_enable(&adapter->napi);
Herbert Xu47313052007-05-29 15:07:31 -07001407
Auke Koke0aac5a2007-03-06 08:57:21 -08001408 e1000_irq_enable(adapter);
1409
Ben Hutchings076152d2008-07-18 17:50:57 -07001410 netif_start_queue(netdev);
1411
Auke Koke0aac5a2007-03-06 08:57:21 -08001412 /* fire a link status change interrupt to start the watchdog */
Joe Perches1dc32912008-07-11 15:17:08 -07001413 ew32(ICS, E1000_ICS_LSC);
Auke Koke0aac5a2007-03-06 08:57:21 -08001414
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 return E1000_SUCCESS;
1416
Linus Torvaldsb5bf28c2007-02-21 11:21:44 -08001417err_req_irq:
Auke Koke0aac5a2007-03-06 08:57:21 -08001418 e1000_power_down_phy(adapter);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001419 e1000_free_all_rx_resources(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420err_setup_rx:
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001421 e1000_free_all_tx_resources(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422err_setup_tx:
1423 e1000_reset(adapter);
1424
1425 return err;
1426}
1427
1428/**
1429 * e1000_close - Disables a network interface
1430 * @netdev: network interface device structure
1431 *
1432 * Returns 0, this is not allowed to fail
1433 *
1434 * The close entry point is called when an interface is de-activated
1435 * by the OS. The hardware is still under the drivers control, but
1436 * needs to be disabled. A global MAC reset is issued to stop the
1437 * hardware, and all transmit and receive resources are freed.
1438 **/
Joe Perches64798842008-07-11 15:17:02 -07001439static int e1000_close(struct net_device *netdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440{
Malli Chilakala60490fe2005-06-17 17:41:45 -07001441 struct e1000_adapter *adapter = netdev_priv(netdev);
Joe Perches1dc32912008-07-11 15:17:08 -07001442 struct e1000_hw *hw = &adapter->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
Auke Kok2db10a02006-06-27 09:06:28 -07001444 WARN_ON(test_bit(__E1000_RESETTING, &adapter->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 e1000_down(adapter);
Auke Kok79f05bf2006-06-27 09:06:32 -07001446 e1000_power_down_phy(adapter);
Auke Kok2db10a02006-06-27 09:06:28 -07001447 e1000_free_irq(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001449 e1000_free_all_tx_resources(adapter);
1450 e1000_free_all_rx_resources(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
Bruce Allan46665602006-09-27 12:54:08 -07001452 /* kill manageability vlan ID if supported, but not if a vlan with
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00001453 * the same ID is registered on the host OS (let 8021q kill it)
1454 */
Joe Perches1dc32912008-07-11 15:17:08 -07001455 if ((hw->mng_cookie.status &
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00001456 E1000_MNG_DHCP_COOKIE_STATUS_VLAN_SUPPORT) &&
1457 !test_bit(adapter->mng_vlan_id, adapter->active_vlans)) {
Patrick McHardy80d5c362013-04-19 02:04:28 +00001458 e1000_vlan_rx_kill_vid(netdev, htons(ETH_P_8021Q),
1459 adapter->mng_vlan_id);
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001460 }
Jeff Kirsherb55ccb32006-01-12 16:50:30 -08001461
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 return 0;
1463}
1464
1465/**
1466 * e1000_check_64k_bound - check that memory doesn't cross 64kB boundary
1467 * @adapter: address of board private structure
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001468 * @start: address of beginning of memory
1469 * @len: length of memory
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 **/
Joe Perches64798842008-07-11 15:17:02 -07001471static bool e1000_check_64k_bound(struct e1000_adapter *adapter, void *start,
1472 unsigned long len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473{
Joe Perches1dc32912008-07-11 15:17:08 -07001474 struct e1000_hw *hw = &adapter->hw;
Joe Perchese982f172008-07-11 15:17:18 -07001475 unsigned long begin = (unsigned long)start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 unsigned long end = begin + len;
1477
Malli Chilakala26483452005-04-28 19:44:46 -07001478 /* First rev 82545 and 82546 need to not allow any memory
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00001479 * write location to cross 64k boundary due to errata 23
1480 */
Joe Perches1dc32912008-07-11 15:17:08 -07001481 if (hw->mac_type == e1000_82545 ||
Dirk Brandewie5377a412011-01-06 14:29:54 +00001482 hw->mac_type == e1000_ce4100 ||
Joe Perches1dc32912008-07-11 15:17:08 -07001483 hw->mac_type == e1000_82546) {
Joe Perchesc3033b02008-03-21 11:06:25 -07001484 return ((begin ^ (end - 1)) >> 16) != 0 ? false : true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 }
1486
Joe Perchesc3033b02008-03-21 11:06:25 -07001487 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488}
1489
1490/**
1491 * e1000_setup_tx_resources - allocate Tx resources (Descriptors)
1492 * @adapter: board private structure
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001493 * @txdr: tx descriptor ring (for a specific queue) to setup
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 *
1495 * Return 0 on success, negative on failure
1496 **/
Joe Perches64798842008-07-11 15:17:02 -07001497static int e1000_setup_tx_resources(struct e1000_adapter *adapter,
1498 struct e1000_tx_ring *txdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 struct pci_dev *pdev = adapter->pdev;
1501 int size;
1502
1503 size = sizeof(struct e1000_buffer) * txdr->count;
Eric Dumazet89bf67f2010-11-22 00:15:06 +00001504 txdr->buffer_info = vzalloc(size);
Joe Perches14f8dc42013-02-07 11:46:27 +00001505 if (!txdr->buffer_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
1508 /* round up to nearest 4K */
1509
1510 txdr->size = txdr->count * sizeof(struct e1000_tx_desc);
Milind Arun Choudhary9099cfb2007-04-27 13:55:29 -07001511 txdr->size = ALIGN(txdr->size, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
Nick Nunleyb16f53b2010-04-27 13:08:45 +00001513 txdr->desc = dma_alloc_coherent(&pdev->dev, txdr->size, &txdr->dma,
1514 GFP_KERNEL);
Jesse Brandeburg96838a42006-01-18 13:01:39 -08001515 if (!txdr->desc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516setup_tx_desc_die:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 vfree(txdr->buffer_info);
1518 return -ENOMEM;
1519 }
1520
Malli Chilakala26483452005-04-28 19:44:46 -07001521 /* Fix for errata 23, can't cross 64kB boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 if (!e1000_check_64k_bound(adapter, txdr->desc, txdr->size)) {
1523 void *olddesc = txdr->desc;
1524 dma_addr_t olddma = txdr->dma;
Emil Tantilovfeb8f472010-07-26 23:37:21 -07001525 e_err(tx_err, "txdr align check failed: %u bytes at %p\n",
Emil Tantilov675ad472010-04-27 14:02:58 +00001526 txdr->size, txdr->desc);
Malli Chilakala26483452005-04-28 19:44:46 -07001527 /* Try again, without freeing the previous */
Nick Nunleyb16f53b2010-04-27 13:08:45 +00001528 txdr->desc = dma_alloc_coherent(&pdev->dev, txdr->size,
1529 &txdr->dma, GFP_KERNEL);
Malli Chilakala26483452005-04-28 19:44:46 -07001530 /* Failed allocation, critical failure */
Jesse Brandeburg96838a42006-01-18 13:01:39 -08001531 if (!txdr->desc) {
Nick Nunleyb16f53b2010-04-27 13:08:45 +00001532 dma_free_coherent(&pdev->dev, txdr->size, olddesc,
1533 olddma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 goto setup_tx_desc_die;
1535 }
1536
1537 if (!e1000_check_64k_bound(adapter, txdr->desc, txdr->size)) {
1538 /* give up */
Nick Nunleyb16f53b2010-04-27 13:08:45 +00001539 dma_free_coherent(&pdev->dev, txdr->size, txdr->desc,
1540 txdr->dma);
1541 dma_free_coherent(&pdev->dev, txdr->size, olddesc,
1542 olddma);
Emil Tantilovfeb8f472010-07-26 23:37:21 -07001543 e_err(probe, "Unable to allocate aligned memory "
Emil Tantilov675ad472010-04-27 14:02:58 +00001544 "for the transmit descriptor ring\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 vfree(txdr->buffer_info);
1546 return -ENOMEM;
1547 } else {
Malli Chilakala26483452005-04-28 19:44:46 -07001548 /* Free old allocation, new allocation was successful */
Nick Nunleyb16f53b2010-04-27 13:08:45 +00001549 dma_free_coherent(&pdev->dev, txdr->size, olddesc,
1550 olddma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 }
1552 }
1553 memset(txdr->desc, 0, txdr->size);
1554
1555 txdr->next_to_use = 0;
1556 txdr->next_to_clean = 0;
1557
1558 return 0;
1559}
1560
1561/**
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001562 * e1000_setup_all_tx_resources - wrapper to allocate Tx resources
1563 * (Descriptors) for all queues
1564 * @adapter: board private structure
1565 *
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001566 * Return 0 on success, negative on failure
1567 **/
Joe Perches64798842008-07-11 15:17:02 -07001568int e1000_setup_all_tx_resources(struct e1000_adapter *adapter)
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001569{
1570 int i, err = 0;
1571
Jeff Kirsherf56799e2006-01-12 16:50:39 -08001572 for (i = 0; i < adapter->num_tx_queues; i++) {
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001573 err = e1000_setup_tx_resources(adapter, &adapter->tx_ring[i]);
1574 if (err) {
Emil Tantilovfeb8f472010-07-26 23:37:21 -07001575 e_err(probe, "Allocation for Tx Queue %u failed\n", i);
Vasily Averin3fbbc722006-08-28 14:56:24 -07001576 for (i-- ; i >= 0; i--)
1577 e1000_free_tx_resources(adapter,
1578 &adapter->tx_ring[i]);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001579 break;
1580 }
1581 }
1582
1583 return err;
1584}
1585
1586/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 * e1000_configure_tx - Configure 8254x Transmit Unit after Reset
1588 * @adapter: board private structure
1589 *
1590 * Configure the Tx unit of the MAC after a reset.
1591 **/
Joe Perches64798842008-07-11 15:17:02 -07001592static void e1000_configure_tx(struct e1000_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593{
Joe Perches406874a2008-04-03 10:06:32 -07001594 u64 tdba;
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001595 struct e1000_hw *hw = &adapter->hw;
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00001596 u32 tdlen, tctl, tipg;
Joe Perches406874a2008-04-03 10:06:32 -07001597 u32 ipgr1, ipgr2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598
1599 /* Setup the HW Tx Head and Tail descriptor pointers */
1600
Jeff Kirsherf56799e2006-01-12 16:50:39 -08001601 switch (adapter->num_tx_queues) {
Mallikarjuna R Chilakala24025e4ec2005-10-04 07:03:23 -04001602 case 1:
1603 default:
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001604 tdba = adapter->tx_ring[0].dma;
1605 tdlen = adapter->tx_ring[0].count *
1606 sizeof(struct e1000_tx_desc);
Joe Perches1dc32912008-07-11 15:17:08 -07001607 ew32(TDLEN, tdlen);
1608 ew32(TDBAH, (tdba >> 32));
1609 ew32(TDBAL, (tdba & 0x00000000ffffffffULL));
1610 ew32(TDT, 0);
1611 ew32(TDH, 0);
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00001612 adapter->tx_ring[0].tdh = ((hw->mac_type >= e1000_82543) ?
1613 E1000_TDH : E1000_82542_TDH);
1614 adapter->tx_ring[0].tdt = ((hw->mac_type >= e1000_82543) ?
1615 E1000_TDT : E1000_82542_TDT);
Mallikarjuna R Chilakala24025e4ec2005-10-04 07:03:23 -04001616 break;
1617 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618
1619 /* Set the default values for the Tx Inter Packet Gap timer */
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00001620 if ((hw->media_type == e1000_media_type_fiber ||
Jesse Brandeburgd89b6c62006-12-15 10:38:32 +01001621 hw->media_type == e1000_media_type_internal_serdes))
Jeff Kirsher0fadb052006-01-12 16:51:05 -08001622 tipg = DEFAULT_82543_TIPG_IPGT_FIBER;
1623 else
1624 tipg = DEFAULT_82543_TIPG_IPGT_COPPER;
1625
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001626 switch (hw->mac_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 case e1000_82542_rev2_0:
1628 case e1000_82542_rev2_1:
1629 tipg = DEFAULT_82542_TIPG_IPGT;
Jeff Kirsher0fadb052006-01-12 16:51:05 -08001630 ipgr1 = DEFAULT_82542_TIPG_IPGR1;
1631 ipgr2 = DEFAULT_82542_TIPG_IPGR2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 break;
1633 default:
Jeff Kirsher0fadb052006-01-12 16:51:05 -08001634 ipgr1 = DEFAULT_82543_TIPG_IPGR1;
1635 ipgr2 = DEFAULT_82543_TIPG_IPGR2;
1636 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 }
Jeff Kirsher0fadb052006-01-12 16:51:05 -08001638 tipg |= ipgr1 << E1000_TIPG_IPGR1_SHIFT;
1639 tipg |= ipgr2 << E1000_TIPG_IPGR2_SHIFT;
Joe Perches1dc32912008-07-11 15:17:08 -07001640 ew32(TIPG, tipg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
1642 /* Set the Tx Interrupt Delay register */
1643
Joe Perches1dc32912008-07-11 15:17:08 -07001644 ew32(TIDV, adapter->tx_int_delay);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001645 if (hw->mac_type >= e1000_82540)
Joe Perches1dc32912008-07-11 15:17:08 -07001646 ew32(TADV, adapter->tx_abs_int_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647
1648 /* Program the Transmit Control Register */
1649
Joe Perches1dc32912008-07-11 15:17:08 -07001650 tctl = er32(TCTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 tctl &= ~E1000_TCTL_CT;
Jeff Kirsher7e6c9862006-03-02 18:19:30 -08001652 tctl |= E1000_TCTL_PSP | E1000_TCTL_RTLC |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 (E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT);
1654
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001655 e1000_config_collision_dist(hw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656
1657 /* Setup Transmit Descriptor Settings for eop descriptor */
Jesse Brandeburg6a042da2006-11-01 08:48:04 -08001658 adapter->txd_cmd = E1000_TXD_CMD_EOP | E1000_TXD_CMD_IFCS;
1659
1660 /* only set IDE if we are delaying interrupts using the timers */
1661 if (adapter->tx_int_delay)
1662 adapter->txd_cmd |= E1000_TXD_CMD_IDE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001664 if (hw->mac_type < e1000_82543)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 adapter->txd_cmd |= E1000_TXD_CMD_RPS;
1666 else
1667 adapter->txd_cmd |= E1000_TXD_CMD_RS;
1668
1669 /* Cache if we're 82544 running in PCI-X because we'll
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00001670 * need this to apply a workaround later in the send path.
1671 */
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001672 if (hw->mac_type == e1000_82544 &&
1673 hw->bus_type == e1000_bus_type_pcix)
Rusty Russell3db1cd52011-12-19 13:56:45 +00001674 adapter->pcix_82544 = true;
Jeff Kirsher7e6c9862006-03-02 18:19:30 -08001675
Joe Perches1dc32912008-07-11 15:17:08 -07001676 ew32(TCTL, tctl);
Jeff Kirsher7e6c9862006-03-02 18:19:30 -08001677
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678}
1679
1680/**
1681 * e1000_setup_rx_resources - allocate Rx resources (Descriptors)
1682 * @adapter: board private structure
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001683 * @rxdr: rx descriptor ring (for a specific queue) to setup
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 *
1685 * Returns 0 on success, negative on failure
1686 **/
Joe Perches64798842008-07-11 15:17:02 -07001687static int e1000_setup_rx_resources(struct e1000_adapter *adapter,
1688 struct e1000_rx_ring *rxdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 struct pci_dev *pdev = adapter->pdev;
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001691 int size, desc_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692
1693 size = sizeof(struct e1000_buffer) * rxdr->count;
Eric Dumazet89bf67f2010-11-22 00:15:06 +00001694 rxdr->buffer_info = vzalloc(size);
Joe Perches14f8dc42013-02-07 11:46:27 +00001695 if (!rxdr->buffer_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00001698 desc_len = sizeof(struct e1000_rx_desc);
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001699
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 /* Round up to nearest 4K */
1701
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001702 rxdr->size = rxdr->count * desc_len;
Milind Arun Choudhary9099cfb2007-04-27 13:55:29 -07001703 rxdr->size = ALIGN(rxdr->size, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704
Nick Nunleyb16f53b2010-04-27 13:08:45 +00001705 rxdr->desc = dma_alloc_coherent(&pdev->dev, rxdr->size, &rxdr->dma,
1706 GFP_KERNEL);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001707 if (!rxdr->desc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708setup_rx_desc_die:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 vfree(rxdr->buffer_info);
1710 return -ENOMEM;
1711 }
1712
Malli Chilakala26483452005-04-28 19:44:46 -07001713 /* Fix for errata 23, can't cross 64kB boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 if (!e1000_check_64k_bound(adapter, rxdr->desc, rxdr->size)) {
1715 void *olddesc = rxdr->desc;
1716 dma_addr_t olddma = rxdr->dma;
Emil Tantilovfeb8f472010-07-26 23:37:21 -07001717 e_err(rx_err, "rxdr align check failed: %u bytes at %p\n",
Emil Tantilov675ad472010-04-27 14:02:58 +00001718 rxdr->size, rxdr->desc);
Malli Chilakala26483452005-04-28 19:44:46 -07001719 /* Try again, without freeing the previous */
Nick Nunleyb16f53b2010-04-27 13:08:45 +00001720 rxdr->desc = dma_alloc_coherent(&pdev->dev, rxdr->size,
1721 &rxdr->dma, GFP_KERNEL);
Malli Chilakala26483452005-04-28 19:44:46 -07001722 /* Failed allocation, critical failure */
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001723 if (!rxdr->desc) {
Nick Nunleyb16f53b2010-04-27 13:08:45 +00001724 dma_free_coherent(&pdev->dev, rxdr->size, olddesc,
1725 olddma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 goto setup_rx_desc_die;
1727 }
1728
1729 if (!e1000_check_64k_bound(adapter, rxdr->desc, rxdr->size)) {
1730 /* give up */
Nick Nunleyb16f53b2010-04-27 13:08:45 +00001731 dma_free_coherent(&pdev->dev, rxdr->size, rxdr->desc,
1732 rxdr->dma);
1733 dma_free_coherent(&pdev->dev, rxdr->size, olddesc,
1734 olddma);
Emil Tantilovfeb8f472010-07-26 23:37:21 -07001735 e_err(probe, "Unable to allocate aligned memory for "
1736 "the Rx descriptor ring\n");
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001737 goto setup_rx_desc_die;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 } else {
Malli Chilakala26483452005-04-28 19:44:46 -07001739 /* Free old allocation, new allocation was successful */
Nick Nunleyb16f53b2010-04-27 13:08:45 +00001740 dma_free_coherent(&pdev->dev, rxdr->size, olddesc,
1741 olddma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 }
1743 }
1744 memset(rxdr->desc, 0, rxdr->size);
1745
1746 rxdr->next_to_clean = 0;
1747 rxdr->next_to_use = 0;
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00001748 rxdr->rx_skb_top = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749
1750 return 0;
1751}
1752
1753/**
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001754 * e1000_setup_all_rx_resources - wrapper to allocate Rx resources
1755 * (Descriptors) for all queues
1756 * @adapter: board private structure
1757 *
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001758 * Return 0 on success, negative on failure
1759 **/
Joe Perches64798842008-07-11 15:17:02 -07001760int e1000_setup_all_rx_resources(struct e1000_adapter *adapter)
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001761{
1762 int i, err = 0;
1763
Jeff Kirsherf56799e2006-01-12 16:50:39 -08001764 for (i = 0; i < adapter->num_rx_queues; i++) {
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001765 err = e1000_setup_rx_resources(adapter, &adapter->rx_ring[i]);
1766 if (err) {
Emil Tantilovfeb8f472010-07-26 23:37:21 -07001767 e_err(probe, "Allocation for Rx Queue %u failed\n", i);
Vasily Averin3fbbc722006-08-28 14:56:24 -07001768 for (i-- ; i >= 0; i--)
1769 e1000_free_rx_resources(adapter,
1770 &adapter->rx_ring[i]);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001771 break;
1772 }
1773 }
1774
1775 return err;
1776}
1777
1778/**
Malli Chilakala26483452005-04-28 19:44:46 -07001779 * e1000_setup_rctl - configure the receive control registers
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 * @adapter: Board private structure
1781 **/
Joe Perches64798842008-07-11 15:17:02 -07001782static void e1000_setup_rctl(struct e1000_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783{
Joe Perches1dc32912008-07-11 15:17:08 -07001784 struct e1000_hw *hw = &adapter->hw;
Brandeburg, Jesse630b25c2008-09-16 13:01:28 -07001785 u32 rctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
Joe Perches1dc32912008-07-11 15:17:08 -07001787 rctl = er32(RCTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
1789 rctl &= ~(3 << E1000_RCTL_MO_SHIFT);
1790
Dean Nelsond5bc77a2011-09-16 16:52:54 +00001791 rctl |= E1000_RCTL_BAM | E1000_RCTL_LBM_NO |
1792 E1000_RCTL_RDMTS_HALF |
Joe Perches1dc32912008-07-11 15:17:08 -07001793 (hw->mc_filter_type << E1000_RCTL_MO_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794
Joe Perches1dc32912008-07-11 15:17:08 -07001795 if (hw->tbi_compatibility_on == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 rctl |= E1000_RCTL_SBP;
1797 else
1798 rctl &= ~E1000_RCTL_SBP;
1799
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001800 if (adapter->netdev->mtu <= ETH_DATA_LEN)
1801 rctl &= ~E1000_RCTL_LPE;
1802 else
1803 rctl |= E1000_RCTL_LPE;
1804
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 /* Setup buffer sizes */
Auke Kok9e2feac2006-04-14 19:05:18 -07001806 rctl &= ~E1000_RCTL_SZ_4096;
1807 rctl |= E1000_RCTL_BSEX;
1808 switch (adapter->rx_buffer_len) {
Jeff Kirshera1415ee2006-02-28 20:24:07 -08001809 case E1000_RXBUFFER_2048:
1810 default:
1811 rctl |= E1000_RCTL_SZ_2048;
1812 rctl &= ~E1000_RCTL_BSEX;
1813 break;
1814 case E1000_RXBUFFER_4096:
1815 rctl |= E1000_RCTL_SZ_4096;
1816 break;
1817 case E1000_RXBUFFER_8192:
1818 rctl |= E1000_RCTL_SZ_8192;
1819 break;
1820 case E1000_RXBUFFER_16384:
1821 rctl |= E1000_RCTL_SZ_16384;
1822 break;
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001823 }
1824
Ben Greeare825b732012-04-04 06:01:29 +00001825 /* This is useful for sniffing bad packets. */
1826 if (adapter->netdev->features & NETIF_F_RXALL) {
1827 /* UPE and MPE will be handled by normal PROMISC logic
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00001828 * in e1000e_set_rx_mode
1829 */
Ben Greeare825b732012-04-04 06:01:29 +00001830 rctl |= (E1000_RCTL_SBP | /* Receive bad packets */
1831 E1000_RCTL_BAM | /* RX All Bcast Pkts */
1832 E1000_RCTL_PMCF); /* RX All MAC Ctrl Pkts */
1833
1834 rctl &= ~(E1000_RCTL_VFE | /* Disable VLAN filter */
1835 E1000_RCTL_DPF | /* Allow filtered pause */
1836 E1000_RCTL_CFIEN); /* Dis VLAN CFIEN Filter */
1837 /* Do not mess with E1000_CTRL_VME, it affects transmit as well,
1838 * and that breaks VLANs.
1839 */
1840 }
1841
Joe Perches1dc32912008-07-11 15:17:08 -07001842 ew32(RCTL, rctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843}
1844
1845/**
1846 * e1000_configure_rx - Configure 8254x Receive Unit after Reset
1847 * @adapter: board private structure
1848 *
1849 * Configure the Rx unit of the MAC after a reset.
1850 **/
Joe Perches64798842008-07-11 15:17:02 -07001851static void e1000_configure_rx(struct e1000_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852{
Joe Perches406874a2008-04-03 10:06:32 -07001853 u64 rdba;
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001854 struct e1000_hw *hw = &adapter->hw;
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00001855 u32 rdlen, rctl, rxcsum;
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001856
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00001857 if (adapter->netdev->mtu > ETH_DATA_LEN) {
1858 rdlen = adapter->rx_ring[0].count *
1859 sizeof(struct e1000_rx_desc);
1860 adapter->clean_rx = e1000_clean_jumbo_rx_irq;
1861 adapter->alloc_rx_buf = e1000_alloc_jumbo_rx_buffers;
1862 } else {
1863 rdlen = adapter->rx_ring[0].count *
1864 sizeof(struct e1000_rx_desc);
1865 adapter->clean_rx = e1000_clean_rx_irq;
1866 adapter->alloc_rx_buf = e1000_alloc_rx_buffers;
1867 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
1869 /* disable receives while setting up the descriptors */
Joe Perches1dc32912008-07-11 15:17:08 -07001870 rctl = er32(RCTL);
1871 ew32(RCTL, rctl & ~E1000_RCTL_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872
1873 /* set the Receive Delay Timer Register */
Joe Perches1dc32912008-07-11 15:17:08 -07001874 ew32(RDTR, adapter->rx_int_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001876 if (hw->mac_type >= e1000_82540) {
Joe Perches1dc32912008-07-11 15:17:08 -07001877 ew32(RADV, adapter->rx_abs_int_delay);
Jesse Brandeburg835bb122006-11-01 08:48:13 -08001878 if (adapter->itr_setting != 0)
Joe Perches1dc32912008-07-11 15:17:08 -07001879 ew32(ITR, 1000000000 / (adapter->itr * 256));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 }
1881
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001882 /* Setup the HW Rx Head and Tail Descriptor Pointers and
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00001883 * the Base and Length of the Rx Descriptor Ring
1884 */
Jeff Kirsherf56799e2006-01-12 16:50:39 -08001885 switch (adapter->num_rx_queues) {
Mallikarjuna R Chilakala24025e4ec2005-10-04 07:03:23 -04001886 case 1:
1887 default:
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001888 rdba = adapter->rx_ring[0].dma;
Joe Perches1dc32912008-07-11 15:17:08 -07001889 ew32(RDLEN, rdlen);
1890 ew32(RDBAH, (rdba >> 32));
1891 ew32(RDBAL, (rdba & 0x00000000ffffffffULL));
1892 ew32(RDT, 0);
1893 ew32(RDH, 0);
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00001894 adapter->rx_ring[0].rdh = ((hw->mac_type >= e1000_82543) ?
1895 E1000_RDH : E1000_82542_RDH);
1896 adapter->rx_ring[0].rdt = ((hw->mac_type >= e1000_82543) ?
1897 E1000_RDT : E1000_82542_RDT);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001898 break;
Mallikarjuna R Chilakala24025e4ec2005-10-04 07:03:23 -04001899 }
1900
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 /* Enable 82543 Receive Checksum Offload for TCP and UDP */
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001902 if (hw->mac_type >= e1000_82543) {
Joe Perches1dc32912008-07-11 15:17:08 -07001903 rxcsum = er32(RXCSUM);
Brandeburg, Jesse630b25c2008-09-16 13:01:28 -07001904 if (adapter->rx_csum)
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001905 rxcsum |= E1000_RXCSUM_TUOFL;
Brandeburg, Jesse630b25c2008-09-16 13:01:28 -07001906 else
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001907 /* don't need to clear IPPCSE as it defaults to 0 */
Brandeburg, Jesse630b25c2008-09-16 13:01:28 -07001908 rxcsum &= ~E1000_RXCSUM_TUOFL;
Joe Perches1dc32912008-07-11 15:17:08 -07001909 ew32(RXCSUM, rxcsum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 }
1911
1912 /* Enable Receives */
Dean Nelsond5bc77a2011-09-16 16:52:54 +00001913 ew32(RCTL, rctl | E1000_RCTL_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914}
1915
1916/**
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001917 * e1000_free_tx_resources - Free Tx Resources per Queue
1918 * @adapter: board private structure
1919 * @tx_ring: Tx descriptor ring for a specific queue
1920 *
1921 * Free all transmit software resources
1922 **/
Joe Perches64798842008-07-11 15:17:02 -07001923static void e1000_free_tx_resources(struct e1000_adapter *adapter,
1924 struct e1000_tx_ring *tx_ring)
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001925{
1926 struct pci_dev *pdev = adapter->pdev;
1927
1928 e1000_clean_tx_ring(adapter, tx_ring);
1929
1930 vfree(tx_ring->buffer_info);
1931 tx_ring->buffer_info = NULL;
1932
Nick Nunleyb16f53b2010-04-27 13:08:45 +00001933 dma_free_coherent(&pdev->dev, tx_ring->size, tx_ring->desc,
1934 tx_ring->dma);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001935
1936 tx_ring->desc = NULL;
1937}
1938
1939/**
1940 * e1000_free_all_tx_resources - Free Tx Resources for All Queues
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 * @adapter: board private structure
1942 *
1943 * Free all transmit software resources
1944 **/
Joe Perches64798842008-07-11 15:17:02 -07001945void e1000_free_all_tx_resources(struct e1000_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946{
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001947 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948
Jeff Kirsherf56799e2006-01-12 16:50:39 -08001949 for (i = 0; i < adapter->num_tx_queues; i++)
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001950 e1000_free_tx_resources(adapter, &adapter->tx_ring[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951}
1952
Joe Perches64798842008-07-11 15:17:02 -07001953static void e1000_unmap_and_free_tx_resource(struct e1000_adapter *adapter,
1954 struct e1000_buffer *buffer_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955{
Alexander Duyck602c0552009-12-02 16:46:00 +00001956 if (buffer_info->dma) {
1957 if (buffer_info->mapped_as_page)
Nick Nunleyb16f53b2010-04-27 13:08:45 +00001958 dma_unmap_page(&adapter->pdev->dev, buffer_info->dma,
1959 buffer_info->length, DMA_TO_DEVICE);
Alexander Duyck602c0552009-12-02 16:46:00 +00001960 else
Nick Nunleyb16f53b2010-04-27 13:08:45 +00001961 dma_unmap_single(&adapter->pdev->dev, buffer_info->dma,
Alexander Duyck602c0552009-12-02 16:46:00 +00001962 buffer_info->length,
Nick Nunleyb16f53b2010-04-27 13:08:45 +00001963 DMA_TO_DEVICE);
Alexander Duyck602c0552009-12-02 16:46:00 +00001964 buffer_info->dma = 0;
1965 }
Jesse Brandeburga9ebadd2006-11-01 08:47:53 -08001966 if (buffer_info->skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 dev_kfree_skb_any(buffer_info->skb);
Jesse Brandeburga9ebadd2006-11-01 08:47:53 -08001968 buffer_info->skb = NULL;
1969 }
Alexander Duyck37e73df2009-03-25 21:58:45 +00001970 buffer_info->time_stamp = 0;
Jesse Brandeburga9ebadd2006-11-01 08:47:53 -08001971 /* buffer_info must be completely set up in the transmit path */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972}
1973
1974/**
1975 * e1000_clean_tx_ring - Free Tx Buffers
1976 * @adapter: board private structure
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001977 * @tx_ring: ring to be cleaned
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 **/
Joe Perches64798842008-07-11 15:17:02 -07001979static void e1000_clean_tx_ring(struct e1000_adapter *adapter,
1980 struct e1000_tx_ring *tx_ring)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981{
Joe Perches1dc32912008-07-11 15:17:08 -07001982 struct e1000_hw *hw = &adapter->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 struct e1000_buffer *buffer_info;
1984 unsigned long size;
1985 unsigned int i;
1986
1987 /* Free all the Tx ring sk_buffs */
1988
Jesse Brandeburg96838a42006-01-18 13:01:39 -08001989 for (i = 0; i < tx_ring->count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 buffer_info = &tx_ring->buffer_info[i];
1991 e1000_unmap_and_free_tx_resource(adapter, buffer_info);
1992 }
1993
Otto Estuardo Solares Cabrera2f66fd32012-08-24 08:04:58 +00001994 netdev_reset_queue(adapter->netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 size = sizeof(struct e1000_buffer) * tx_ring->count;
1996 memset(tx_ring->buffer_info, 0, size);
1997
1998 /* Zero out the descriptor ring */
1999
2000 memset(tx_ring->desc, 0, tx_ring->size);
2001
2002 tx_ring->next_to_use = 0;
2003 tx_ring->next_to_clean = 0;
Rusty Russell3db1cd52011-12-19 13:56:45 +00002004 tx_ring->last_tx_tso = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005
Joe Perches1dc32912008-07-11 15:17:08 -07002006 writel(0, hw->hw_addr + tx_ring->tdh);
2007 writel(0, hw->hw_addr + tx_ring->tdt);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002008}
2009
2010/**
2011 * e1000_clean_all_tx_rings - Free Tx Buffers for all queues
2012 * @adapter: board private structure
2013 **/
Joe Perches64798842008-07-11 15:17:02 -07002014static void e1000_clean_all_tx_rings(struct e1000_adapter *adapter)
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002015{
2016 int i;
2017
Jeff Kirsherf56799e2006-01-12 16:50:39 -08002018 for (i = 0; i < adapter->num_tx_queues; i++)
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002019 e1000_clean_tx_ring(adapter, &adapter->tx_ring[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020}
2021
2022/**
2023 * e1000_free_rx_resources - Free Rx Resources
2024 * @adapter: board private structure
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002025 * @rx_ring: ring to clean the resources from
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 *
2027 * Free all receive software resources
2028 **/
Joe Perches64798842008-07-11 15:17:02 -07002029static void e1000_free_rx_resources(struct e1000_adapter *adapter,
2030 struct e1000_rx_ring *rx_ring)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 struct pci_dev *pdev = adapter->pdev;
2033
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002034 e1000_clean_rx_ring(adapter, rx_ring);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035
2036 vfree(rx_ring->buffer_info);
2037 rx_ring->buffer_info = NULL;
2038
Nick Nunleyb16f53b2010-04-27 13:08:45 +00002039 dma_free_coherent(&pdev->dev, rx_ring->size, rx_ring->desc,
2040 rx_ring->dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041
2042 rx_ring->desc = NULL;
2043}
2044
2045/**
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002046 * e1000_free_all_rx_resources - Free Rx Resources for All Queues
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 * @adapter: board private structure
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002048 *
2049 * Free all receive software resources
2050 **/
Joe Perches64798842008-07-11 15:17:02 -07002051void e1000_free_all_rx_resources(struct e1000_adapter *adapter)
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002052{
2053 int i;
2054
Jeff Kirsherf56799e2006-01-12 16:50:39 -08002055 for (i = 0; i < adapter->num_rx_queues; i++)
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002056 e1000_free_rx_resources(adapter, &adapter->rx_ring[i]);
2057}
2058
2059/**
2060 * e1000_clean_rx_ring - Free Rx Buffers per Queue
2061 * @adapter: board private structure
2062 * @rx_ring: ring to free buffers from
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 **/
Joe Perches64798842008-07-11 15:17:02 -07002064static void e1000_clean_rx_ring(struct e1000_adapter *adapter,
2065 struct e1000_rx_ring *rx_ring)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066{
Joe Perches1dc32912008-07-11 15:17:08 -07002067 struct e1000_hw *hw = &adapter->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 struct e1000_buffer *buffer_info;
2069 struct pci_dev *pdev = adapter->pdev;
2070 unsigned long size;
Brandeburg, Jesse630b25c2008-09-16 13:01:28 -07002071 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
2073 /* Free all the Rx ring sk_buffs */
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002074 for (i = 0; i < rx_ring->count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 buffer_info = &rx_ring->buffer_info[i];
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00002076 if (buffer_info->dma &&
2077 adapter->clean_rx == e1000_clean_rx_irq) {
Nick Nunleyb16f53b2010-04-27 13:08:45 +00002078 dma_unmap_single(&pdev->dev, buffer_info->dma,
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00002079 buffer_info->length,
Nick Nunleyb16f53b2010-04-27 13:08:45 +00002080 DMA_FROM_DEVICE);
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00002081 } else if (buffer_info->dma &&
2082 adapter->clean_rx == e1000_clean_jumbo_rx_irq) {
Nick Nunleyb16f53b2010-04-27 13:08:45 +00002083 dma_unmap_page(&pdev->dev, buffer_info->dma,
2084 buffer_info->length,
2085 DMA_FROM_DEVICE);
Jesse Brandeburg679be3b2009-06-30 12:45:34 +00002086 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
Jesse Brandeburg679be3b2009-06-30 12:45:34 +00002088 buffer_info->dma = 0;
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00002089 if (buffer_info->page) {
2090 put_page(buffer_info->page);
2091 buffer_info->page = NULL;
2092 }
Jesse Brandeburg679be3b2009-06-30 12:45:34 +00002093 if (buffer_info->skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 dev_kfree_skb(buffer_info->skb);
2095 buffer_info->skb = NULL;
Jeff Kirsher997f5cb2006-01-12 16:50:55 -08002096 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 }
2098
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00002099 /* there also may be some cached data from a chained receive */
2100 if (rx_ring->rx_skb_top) {
2101 dev_kfree_skb(rx_ring->rx_skb_top);
2102 rx_ring->rx_skb_top = NULL;
2103 }
2104
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 size = sizeof(struct e1000_buffer) * rx_ring->count;
2106 memset(rx_ring->buffer_info, 0, size);
2107
2108 /* Zero out the descriptor ring */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 memset(rx_ring->desc, 0, rx_ring->size);
2110
2111 rx_ring->next_to_clean = 0;
2112 rx_ring->next_to_use = 0;
2113
Joe Perches1dc32912008-07-11 15:17:08 -07002114 writel(0, hw->hw_addr + rx_ring->rdh);
2115 writel(0, hw->hw_addr + rx_ring->rdt);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002116}
2117
2118/**
2119 * e1000_clean_all_rx_rings - Free Rx Buffers for all queues
2120 * @adapter: board private structure
2121 **/
Joe Perches64798842008-07-11 15:17:02 -07002122static void e1000_clean_all_rx_rings(struct e1000_adapter *adapter)
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002123{
2124 int i;
2125
Jeff Kirsherf56799e2006-01-12 16:50:39 -08002126 for (i = 0; i < adapter->num_rx_queues; i++)
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002127 e1000_clean_rx_ring(adapter, &adapter->rx_ring[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128}
2129
2130/* The 82542 2.0 (revision 2) needs to have the receive unit in reset
2131 * and memory write and invalidate disabled for certain operations
2132 */
Joe Perches64798842008-07-11 15:17:02 -07002133static void e1000_enter_82542_rst(struct e1000_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134{
Joe Perches1dc32912008-07-11 15:17:08 -07002135 struct e1000_hw *hw = &adapter->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 struct net_device *netdev = adapter->netdev;
Joe Perches406874a2008-04-03 10:06:32 -07002137 u32 rctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138
Joe Perches1dc32912008-07-11 15:17:08 -07002139 e1000_pci_clear_mwi(hw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140
Joe Perches1dc32912008-07-11 15:17:08 -07002141 rctl = er32(RCTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 rctl |= E1000_RCTL_RST;
Joe Perches1dc32912008-07-11 15:17:08 -07002143 ew32(RCTL, rctl);
2144 E1000_WRITE_FLUSH();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 mdelay(5);
2146
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002147 if (netif_running(netdev))
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002148 e1000_clean_all_rx_rings(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149}
2150
Joe Perches64798842008-07-11 15:17:02 -07002151static void e1000_leave_82542_rst(struct e1000_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152{
Joe Perches1dc32912008-07-11 15:17:08 -07002153 struct e1000_hw *hw = &adapter->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 struct net_device *netdev = adapter->netdev;
Joe Perches406874a2008-04-03 10:06:32 -07002155 u32 rctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156
Joe Perches1dc32912008-07-11 15:17:08 -07002157 rctl = er32(RCTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 rctl &= ~E1000_RCTL_RST;
Joe Perches1dc32912008-07-11 15:17:08 -07002159 ew32(RCTL, rctl);
2160 E1000_WRITE_FLUSH();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 mdelay(5);
2162
Joe Perches1dc32912008-07-11 15:17:08 -07002163 if (hw->pci_cmd_word & PCI_COMMAND_INVALIDATE)
2164 e1000_pci_set_mwi(hw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002166 if (netif_running(netdev)) {
Jeff Kirsher72d64a42006-01-12 16:51:19 -08002167 /* No need to loop, because 82542 supports only 1 queue */
2168 struct e1000_rx_ring *ring = &adapter->rx_ring[0];
Jesse Brandeburg7c4d3362006-01-18 13:01:45 -08002169 e1000_configure_rx(adapter);
Jeff Kirsher72d64a42006-01-12 16:51:19 -08002170 adapter->alloc_rx_buf(adapter, ring, E1000_DESC_UNUSED(ring));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 }
2172}
2173
2174/**
2175 * e1000_set_mac - Change the Ethernet Address of the NIC
2176 * @netdev: network interface device structure
2177 * @p: pointer to an address structure
2178 *
2179 * Returns 0 on success, negative on failure
2180 **/
Joe Perches64798842008-07-11 15:17:02 -07002181static int e1000_set_mac(struct net_device *netdev, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182{
Malli Chilakala60490fe2005-06-17 17:41:45 -07002183 struct e1000_adapter *adapter = netdev_priv(netdev);
Joe Perches1dc32912008-07-11 15:17:08 -07002184 struct e1000_hw *hw = &adapter->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 struct sockaddr *addr = p;
2186
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002187 if (!is_valid_ether_addr(addr->sa_data))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 return -EADDRNOTAVAIL;
2189
2190 /* 82542 2.0 needs to be in reset to write receive address registers */
2191
Joe Perches1dc32912008-07-11 15:17:08 -07002192 if (hw->mac_type == e1000_82542_rev2_0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 e1000_enter_82542_rst(adapter);
2194
2195 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
Joe Perches1dc32912008-07-11 15:17:08 -07002196 memcpy(hw->mac_addr, addr->sa_data, netdev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197
Joe Perches1dc32912008-07-11 15:17:08 -07002198 e1000_rar_set(hw, hw->mac_addr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199
Joe Perches1dc32912008-07-11 15:17:08 -07002200 if (hw->mac_type == e1000_82542_rev2_0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 e1000_leave_82542_rst(adapter);
2202
2203 return 0;
2204}
2205
2206/**
Patrick McHardydb0ce502007-11-13 20:54:59 -08002207 * e1000_set_rx_mode - Secondary Unicast, Multicast and Promiscuous mode set
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 * @netdev: network interface device structure
2209 *
Patrick McHardydb0ce502007-11-13 20:54:59 -08002210 * The set_rx_mode entry point is called whenever the unicast or multicast
2211 * address lists or the network interface flags are updated. This routine is
2212 * responsible for configuring the hardware for proper unicast, multicast,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 * promiscuous mode, and all-multi behavior.
2214 **/
Joe Perches64798842008-07-11 15:17:02 -07002215static void e1000_set_rx_mode(struct net_device *netdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216{
Malli Chilakala60490fe2005-06-17 17:41:45 -07002217 struct e1000_adapter *adapter = netdev_priv(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 struct e1000_hw *hw = &adapter->hw;
Jiri Pirkoccffad252009-05-22 23:22:17 +00002219 struct netdev_hw_addr *ha;
2220 bool use_uc = false;
Joe Perches406874a2008-04-03 10:06:32 -07002221 u32 rctl;
2222 u32 hash_value;
Mallikarjuna R Chilakala868d5302005-10-04 06:58:59 -04002223 int i, rar_entries = E1000_RAR_ENTRIES;
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00002224 int mta_reg_count = E1000_NUM_MTA_REGISTERS;
Jesse Brandeburg81c52282009-04-04 16:36:53 -07002225 u32 *mcarray = kcalloc(mta_reg_count, sizeof(u32), GFP_ATOMIC);
2226
Joe Perches14f8dc42013-02-07 11:46:27 +00002227 if (!mcarray)
Jesse Brandeburg81c52282009-04-04 16:36:53 -07002228 return;
Auke Kokcd94dd02006-06-27 09:08:22 -07002229
Malli Chilakala26483452005-04-28 19:44:46 -07002230 /* Check for Promiscuous and All Multicast modes */
2231
Joe Perches1dc32912008-07-11 15:17:08 -07002232 rctl = er32(RCTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002234 if (netdev->flags & IFF_PROMISC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 rctl |= (E1000_RCTL_UPE | E1000_RCTL_MPE);
Patrick McHardy746b9f02008-07-16 20:15:45 -07002236 rctl &= ~E1000_RCTL_VFE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 } else {
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00002238 if (netdev->flags & IFF_ALLMULTI)
Patrick McHardy746b9f02008-07-16 20:15:45 -07002239 rctl |= E1000_RCTL_MPE;
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00002240 else
Patrick McHardy746b9f02008-07-16 20:15:45 -07002241 rctl &= ~E1000_RCTL_MPE;
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00002242 /* Enable VLAN filter if there is a VLAN */
Jiri Pirko5622e402011-07-21 03:26:31 +00002243 if (e1000_vlan_used(adapter))
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00002244 rctl |= E1000_RCTL_VFE;
Patrick McHardydb0ce502007-11-13 20:54:59 -08002245 }
2246
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08002247 if (netdev_uc_count(netdev) > rar_entries - 1) {
Patrick McHardydb0ce502007-11-13 20:54:59 -08002248 rctl |= E1000_RCTL_UPE;
2249 } else if (!(netdev->flags & IFF_PROMISC)) {
2250 rctl &= ~E1000_RCTL_UPE;
Jiri Pirkoccffad252009-05-22 23:22:17 +00002251 use_uc = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 }
2253
Joe Perches1dc32912008-07-11 15:17:08 -07002254 ew32(RCTL, rctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255
2256 /* 82542 2.0 needs to be in reset to write receive address registers */
2257
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002258 if (hw->mac_type == e1000_82542_rev2_0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 e1000_enter_82542_rst(adapter);
2260
Patrick McHardydb0ce502007-11-13 20:54:59 -08002261 /* load the first 14 addresses into the exact filters 1-14. Unicast
2262 * addresses take precedence to avoid disabling unicast filtering
2263 * when possible.
2264 *
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04002265 * RAR 0 is used for the station MAC address
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 * if there are not 14 addresses, go ahead and clear the filters
2267 */
Jiri Pirkoccffad252009-05-22 23:22:17 +00002268 i = 1;
2269 if (use_uc)
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08002270 netdev_for_each_uc_addr(ha, netdev) {
Jiri Pirkoccffad252009-05-22 23:22:17 +00002271 if (i == rar_entries)
2272 break;
2273 e1000_rar_set(hw, ha->addr, i++);
2274 }
2275
Jiri Pirko22bedad32010-04-01 21:22:57 +00002276 netdev_for_each_mc_addr(ha, netdev) {
Jiri Pirko7a81e9f2010-02-22 09:10:44 +00002277 if (i == rar_entries) {
2278 /* load any remaining addresses into the hash table */
2279 u32 hash_reg, hash_bit, mta;
Jiri Pirko22bedad32010-04-01 21:22:57 +00002280 hash_value = e1000_hash_mc_addr(hw, ha->addr);
Jiri Pirko7a81e9f2010-02-22 09:10:44 +00002281 hash_reg = (hash_value >> 5) & 0x7F;
2282 hash_bit = hash_value & 0x1F;
2283 mta = (1 << hash_bit);
2284 mcarray[hash_reg] |= mta;
Jiri Pirko10886af2010-02-23 01:19:22 -08002285 } else {
Jiri Pirko22bedad32010-04-01 21:22:57 +00002286 e1000_rar_set(hw, ha->addr, i++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 }
2288 }
2289
Jiri Pirko7a81e9f2010-02-22 09:10:44 +00002290 for (; i < rar_entries; i++) {
2291 E1000_WRITE_REG_ARRAY(hw, RA, i << 1, 0);
2292 E1000_WRITE_FLUSH();
2293 E1000_WRITE_REG_ARRAY(hw, RA, (i << 1) + 1, 0);
2294 E1000_WRITE_FLUSH();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 }
2296
Jesse Brandeburg81c52282009-04-04 16:36:53 -07002297 /* write the hash table completely, write from bottom to avoid
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00002298 * both stupid write combining chipsets, and flushing each write
2299 */
Jesse Brandeburg81c52282009-04-04 16:36:53 -07002300 for (i = mta_reg_count - 1; i >= 0 ; i--) {
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00002301 /* If we are on an 82544 has an errata where writing odd
Jesse Brandeburg81c52282009-04-04 16:36:53 -07002302 * offsets overwrites the previous even offset, but writing
2303 * backwards over the range solves the issue by always
2304 * writing the odd offset first
2305 */
2306 E1000_WRITE_REG_ARRAY(hw, MTA, i, mcarray[i]);
2307 }
2308 E1000_WRITE_FLUSH();
2309
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002310 if (hw->mac_type == e1000_82542_rev2_0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 e1000_leave_82542_rst(adapter);
Jesse Brandeburg81c52282009-04-04 16:36:53 -07002312
2313 kfree(mcarray);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314}
2315
Jesse Brandeburga4010af2011-10-05 07:24:41 +00002316/**
2317 * e1000_update_phy_info_task - get phy info
2318 * @work: work struct contained inside adapter struct
2319 *
2320 * Need to wait a few seconds after link up to get diagnostic information from
2321 * the phy
2322 */
Jesse Brandeburg5cf42fc2010-09-22 18:22:17 +00002323static void e1000_update_phy_info_task(struct work_struct *work)
2324{
2325 struct e1000_adapter *adapter = container_of(work,
Jesse Brandeburga4010af2011-10-05 07:24:41 +00002326 struct e1000_adapter,
2327 phy_info_task.work);
Jesse Brandeburg0ef4eed2011-10-05 07:24:51 +00002328 if (test_bit(__E1000_DOWN, &adapter->flags))
2329 return;
2330 mutex_lock(&adapter->mutex);
Jesse Brandeburga4010af2011-10-05 07:24:41 +00002331 e1000_phy_get_info(&adapter->hw, &adapter->phy_info);
Jesse Brandeburg0ef4eed2011-10-05 07:24:51 +00002332 mutex_unlock(&adapter->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333}
2334
2335/**
Jesse Brandeburg5cf42fc2010-09-22 18:22:17 +00002336 * e1000_82547_tx_fifo_stall_task - task to complete work
2337 * @work: work struct contained inside adapter struct
2338 **/
2339static void e1000_82547_tx_fifo_stall_task(struct work_struct *work)
2340{
2341 struct e1000_adapter *adapter = container_of(work,
Jesse Brandeburga4010af2011-10-05 07:24:41 +00002342 struct e1000_adapter,
2343 fifo_stall_task.work);
Joe Perches1dc32912008-07-11 15:17:08 -07002344 struct e1000_hw *hw = &adapter->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 struct net_device *netdev = adapter->netdev;
Joe Perches406874a2008-04-03 10:06:32 -07002346 u32 tctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347
Jesse Brandeburg0ef4eed2011-10-05 07:24:51 +00002348 if (test_bit(__E1000_DOWN, &adapter->flags))
2349 return;
2350 mutex_lock(&adapter->mutex);
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002351 if (atomic_read(&adapter->tx_fifo_stall)) {
Joe Perches1dc32912008-07-11 15:17:08 -07002352 if ((er32(TDT) == er32(TDH)) &&
2353 (er32(TDFT) == er32(TDFH)) &&
2354 (er32(TDFTS) == er32(TDFHS))) {
2355 tctl = er32(TCTL);
2356 ew32(TCTL, tctl & ~E1000_TCTL_EN);
2357 ew32(TDFT, adapter->tx_head_addr);
2358 ew32(TDFH, adapter->tx_head_addr);
2359 ew32(TDFTS, adapter->tx_head_addr);
2360 ew32(TDFHS, adapter->tx_head_addr);
2361 ew32(TCTL, tctl);
2362 E1000_WRITE_FLUSH();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363
2364 adapter->tx_fifo_head = 0;
2365 atomic_set(&adapter->tx_fifo_stall, 0);
2366 netif_wake_queue(netdev);
Jesse Brandeburgbaa34742009-09-25 12:17:23 +00002367 } else if (!test_bit(__E1000_DOWN, &adapter->flags)) {
Jesse Brandeburga4010af2011-10-05 07:24:41 +00002368 schedule_delayed_work(&adapter->fifo_stall_task, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 }
2370 }
Jesse Brandeburg0ef4eed2011-10-05 07:24:51 +00002371 mutex_unlock(&adapter->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372}
2373
Nick Nunleyb5481922010-02-03 14:49:28 +00002374bool e1000_has_link(struct e1000_adapter *adapter)
Jesse Brandeburgbe0f0712009-09-25 12:17:44 +00002375{
2376 struct e1000_hw *hw = &adapter->hw;
2377 bool link_active = false;
Jesse Brandeburgbe0f0712009-09-25 12:17:44 +00002378
Nicolas Schichan6d9e5132011-07-09 00:24:18 +00002379 /* get_link_status is set on LSC (link status) interrupt or rx
2380 * sequence error interrupt (except on intel ce4100).
2381 * get_link_status will stay false until the
2382 * e1000_check_for_link establishes link for copper adapters
2383 * ONLY
Jesse Brandeburgbe0f0712009-09-25 12:17:44 +00002384 */
2385 switch (hw->media_type) {
2386 case e1000_media_type_copper:
Nicolas Schichan6d9e5132011-07-09 00:24:18 +00002387 if (hw->mac_type == e1000_ce4100)
2388 hw->get_link_status = 1;
Jesse Brandeburgbe0f0712009-09-25 12:17:44 +00002389 if (hw->get_link_status) {
Jesse Brandeburg120a5d02009-09-25 15:19:46 -07002390 e1000_check_for_link(hw);
Jesse Brandeburgbe0f0712009-09-25 12:17:44 +00002391 link_active = !hw->get_link_status;
2392 } else {
2393 link_active = true;
2394 }
2395 break;
2396 case e1000_media_type_fiber:
Jesse Brandeburg120a5d02009-09-25 15:19:46 -07002397 e1000_check_for_link(hw);
Jesse Brandeburgbe0f0712009-09-25 12:17:44 +00002398 link_active = !!(er32(STATUS) & E1000_STATUS_LU);
2399 break;
2400 case e1000_media_type_internal_serdes:
Jesse Brandeburg120a5d02009-09-25 15:19:46 -07002401 e1000_check_for_link(hw);
Jesse Brandeburgbe0f0712009-09-25 12:17:44 +00002402 link_active = hw->serdes_has_link;
2403 break;
2404 default:
2405 break;
2406 }
2407
2408 return link_active;
2409}
2410
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411/**
Jesse Brandeburga4010af2011-10-05 07:24:41 +00002412 * e1000_watchdog - work function
2413 * @work: work struct contained inside adapter struct
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 **/
Jesse Brandeburga4010af2011-10-05 07:24:41 +00002415static void e1000_watchdog(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416{
Jesse Brandeburga4010af2011-10-05 07:24:41 +00002417 struct e1000_adapter *adapter = container_of(work,
2418 struct e1000_adapter,
2419 watchdog_task.work);
Joe Perches1dc32912008-07-11 15:17:08 -07002420 struct e1000_hw *hw = &adapter->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 struct net_device *netdev = adapter->netdev;
Jeff Kirsher545c67c2006-01-12 16:50:25 -08002422 struct e1000_tx_ring *txdr = adapter->tx_ring;
Joe Perches406874a2008-04-03 10:06:32 -07002423 u32 link, tctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424
Jesse Brandeburg0ef4eed2011-10-05 07:24:51 +00002425 if (test_bit(__E1000_DOWN, &adapter->flags))
2426 return;
2427
2428 mutex_lock(&adapter->mutex);
Jesse Brandeburgbe0f0712009-09-25 12:17:44 +00002429 link = e1000_has_link(adapter);
2430 if ((netif_carrier_ok(netdev)) && link)
2431 goto link_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002433 if (link) {
2434 if (!netif_carrier_ok(netdev)) {
Joe Perches406874a2008-04-03 10:06:32 -07002435 u32 ctrl;
Joe Perchesc3033b02008-03-21 11:06:25 -07002436 bool txb2b = true;
Jesse Brandeburgbe0f0712009-09-25 12:17:44 +00002437 /* update snapshot of PHY registers on LSC */
Joe Perches1dc32912008-07-11 15:17:08 -07002438 e1000_get_speed_and_duplex(hw,
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00002439 &adapter->link_speed,
2440 &adapter->link_duplex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441
Joe Perches1dc32912008-07-11 15:17:08 -07002442 ctrl = er32(CTRL);
Emil Tantilov675ad472010-04-27 14:02:58 +00002443 pr_info("%s NIC Link is Up %d Mbps %s, "
2444 "Flow Control: %s\n",
2445 netdev->name,
2446 adapter->link_speed,
2447 adapter->link_duplex == FULL_DUPLEX ?
2448 "Full Duplex" : "Half Duplex",
2449 ((ctrl & E1000_CTRL_TFCE) && (ctrl &
2450 E1000_CTRL_RFCE)) ? "RX/TX" : ((ctrl &
2451 E1000_CTRL_RFCE) ? "RX" : ((ctrl &
2452 E1000_CTRL_TFCE) ? "TX" : "None")));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453
Emil Tantilov39ca5f02010-03-26 11:25:58 +00002454 /* adjust timeout factor according to speed/duplex */
Jeff Kirsher66a2b0a2006-01-12 16:50:53 -08002455 adapter->tx_timeout_factor = 1;
Jeff Kirsher7e6c9862006-03-02 18:19:30 -08002456 switch (adapter->link_speed) {
2457 case SPEED_10:
Joe Perchesc3033b02008-03-21 11:06:25 -07002458 txb2b = false;
Jesse Brandeburgbe0f0712009-09-25 12:17:44 +00002459 adapter->tx_timeout_factor = 16;
Jeff Kirsher7e6c9862006-03-02 18:19:30 -08002460 break;
2461 case SPEED_100:
Joe Perchesc3033b02008-03-21 11:06:25 -07002462 txb2b = false;
Jeff Kirsher7e6c9862006-03-02 18:19:30 -08002463 /* maybe add some timeout factor ? */
2464 break;
2465 }
2466
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00002467 /* enable transmits in the hardware */
Joe Perches1dc32912008-07-11 15:17:08 -07002468 tctl = er32(TCTL);
Jeff Kirsher7e6c9862006-03-02 18:19:30 -08002469 tctl |= E1000_TCTL_EN;
Joe Perches1dc32912008-07-11 15:17:08 -07002470 ew32(TCTL, tctl);
Jeff Kirsher66a2b0a2006-01-12 16:50:53 -08002471
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 netif_carrier_on(netdev);
Jesse Brandeburgbaa34742009-09-25 12:17:23 +00002473 if (!test_bit(__E1000_DOWN, &adapter->flags))
Jesse Brandeburga4010af2011-10-05 07:24:41 +00002474 schedule_delayed_work(&adapter->phy_info_task,
2475 2 * HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 adapter->smartspeed = 0;
2477 }
2478 } else {
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002479 if (netif_carrier_ok(netdev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 adapter->link_speed = 0;
2481 adapter->link_duplex = 0;
Emil Tantilov675ad472010-04-27 14:02:58 +00002482 pr_info("%s NIC Link is Down\n",
2483 netdev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 netif_carrier_off(netdev);
Jesse Brandeburgbaa34742009-09-25 12:17:23 +00002485
2486 if (!test_bit(__E1000_DOWN, &adapter->flags))
Jesse Brandeburga4010af2011-10-05 07:24:41 +00002487 schedule_delayed_work(&adapter->phy_info_task,
2488 2 * HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 }
2490
2491 e1000_smartspeed(adapter);
2492 }
2493
Jesse Brandeburgbe0f0712009-09-25 12:17:44 +00002494link_up:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 e1000_update_stats(adapter);
2496
Joe Perches1dc32912008-07-11 15:17:08 -07002497 hw->tx_packet_delta = adapter->stats.tpt - adapter->tpt_old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 adapter->tpt_old = adapter->stats.tpt;
Joe Perches1dc32912008-07-11 15:17:08 -07002499 hw->collision_delta = adapter->stats.colc - adapter->colc_old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 adapter->colc_old = adapter->stats.colc;
2501
2502 adapter->gorcl = adapter->stats.gorcl - adapter->gorcl_old;
2503 adapter->gorcl_old = adapter->stats.gorcl;
2504 adapter->gotcl = adapter->stats.gotcl - adapter->gotcl_old;
2505 adapter->gotcl_old = adapter->stats.gotcl;
2506
Joe Perches1dc32912008-07-11 15:17:08 -07002507 e1000_update_adaptive(hw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508
Jeff Kirsherf56799e2006-01-12 16:50:39 -08002509 if (!netif_carrier_ok(netdev)) {
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002510 if (E1000_DESC_UNUSED(txdr) + 1 < txdr->count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 /* We've lost link, so the controller stops DMA,
2512 * but we've got queued Tx work that's never going
2513 * to get done, so reset controller to flush Tx.
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00002514 * (Do the reset outside of interrupt context).
2515 */
Jeff Kirsher87041632006-03-02 18:21:24 -08002516 adapter->tx_timeout_count++;
2517 schedule_work(&adapter->reset_task);
Jesse Brandeburg0ef4eed2011-10-05 07:24:51 +00002518 /* exit immediately since reset is imminent */
2519 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 }
2521 }
2522
Jesse Brandeburgeab2abf2010-05-04 22:26:03 +00002523 /* Simple mode for Interrupt Throttle Rate (ITR) */
2524 if (hw->mac_type >= e1000_82540 && adapter->itr_setting == 4) {
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00002525 /* Symmetric Tx/Rx gets a reduced ITR=2000;
Jesse Brandeburgeab2abf2010-05-04 22:26:03 +00002526 * Total asymmetrical Tx or Rx gets ITR=8000;
2527 * everyone else is between 2000-8000.
2528 */
2529 u32 goc = (adapter->gotcl + adapter->gorcl) / 10000;
2530 u32 dif = (adapter->gotcl > adapter->gorcl ?
2531 adapter->gotcl - adapter->gorcl :
2532 adapter->gorcl - adapter->gotcl) / 10000;
2533 u32 itr = goc > 0 ? (dif * 6000 / goc + 2000) : 8000;
2534
2535 ew32(ITR, 1000000000 / (itr * 256));
2536 }
2537
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 /* Cause software interrupt to ensure rx ring is cleaned */
Joe Perches1dc32912008-07-11 15:17:08 -07002539 ew32(ICS, E1000_ICS_RXDMT0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540
Malli Chilakala26483452005-04-28 19:44:46 -07002541 /* Force detection of hung controller every watchdog period */
Joe Perchesc3033b02008-03-21 11:06:25 -07002542 adapter->detect_tx_hung = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543
Jesse Brandeburga4010af2011-10-05 07:24:41 +00002544 /* Reschedule the task */
Jesse Brandeburgbaa34742009-09-25 12:17:23 +00002545 if (!test_bit(__E1000_DOWN, &adapter->flags))
Jesse Brandeburga4010af2011-10-05 07:24:41 +00002546 schedule_delayed_work(&adapter->watchdog_task, 2 * HZ);
Jesse Brandeburg0ef4eed2011-10-05 07:24:51 +00002547
2548unlock:
2549 mutex_unlock(&adapter->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550}
2551
Jesse Brandeburg835bb122006-11-01 08:48:13 -08002552enum latency_range {
2553 lowest_latency = 0,
2554 low_latency = 1,
2555 bulk_latency = 2,
2556 latency_invalid = 255
2557};
2558
2559/**
2560 * e1000_update_itr - update the dynamic ITR value based on statistics
Jesse Brandeburg8fce4732009-09-25 12:18:41 +00002561 * @adapter: pointer to adapter
2562 * @itr_setting: current adapter->itr
2563 * @packets: the number of packets during this measurement interval
2564 * @bytes: the number of bytes during this measurement interval
2565 *
Jesse Brandeburg835bb122006-11-01 08:48:13 -08002566 * Stores a new ITR value based on packets and byte
2567 * counts during the last interrupt. The advantage of per interrupt
2568 * computation is faster updates and more accurate ITR for the current
2569 * traffic pattern. Constants in this function were computed
2570 * based on theoretical maximum wire speed and thresholds were set based
2571 * on testing data as well as attempting to minimize response time
2572 * while increasing bulk throughput.
2573 * this functionality is controlled by the InterruptThrottleRate module
2574 * parameter (see e1000_param.c)
Jesse Brandeburg835bb122006-11-01 08:48:13 -08002575 **/
2576static unsigned int e1000_update_itr(struct e1000_adapter *adapter,
Joe Perches64798842008-07-11 15:17:02 -07002577 u16 itr_setting, int packets, int bytes)
Jesse Brandeburg835bb122006-11-01 08:48:13 -08002578{
2579 unsigned int retval = itr_setting;
2580 struct e1000_hw *hw = &adapter->hw;
2581
2582 if (unlikely(hw->mac_type < e1000_82540))
2583 goto update_itr_done;
2584
2585 if (packets == 0)
2586 goto update_itr_done;
2587
Jesse Brandeburg835bb122006-11-01 08:48:13 -08002588 switch (itr_setting) {
2589 case lowest_latency:
Jesse Brandeburg2b653262006-12-15 10:30:44 +01002590 /* jumbo frames get bulk treatment*/
2591 if (bytes/packets > 8000)
2592 retval = bulk_latency;
2593 else if ((packets < 5) && (bytes > 512))
Jesse Brandeburg835bb122006-11-01 08:48:13 -08002594 retval = low_latency;
2595 break;
2596 case low_latency: /* 50 usec aka 20000 ints/s */
2597 if (bytes > 10000) {
Jesse Brandeburg2b653262006-12-15 10:30:44 +01002598 /* jumbo frames need bulk latency setting */
2599 if (bytes/packets > 8000)
2600 retval = bulk_latency;
2601 else if ((packets < 10) || ((bytes/packets) > 1200))
Jesse Brandeburg835bb122006-11-01 08:48:13 -08002602 retval = bulk_latency;
2603 else if ((packets > 35))
2604 retval = lowest_latency;
Jesse Brandeburg2b653262006-12-15 10:30:44 +01002605 } else if (bytes/packets > 2000)
2606 retval = bulk_latency;
2607 else if (packets <= 2 && bytes < 512)
Jesse Brandeburg835bb122006-11-01 08:48:13 -08002608 retval = lowest_latency;
2609 break;
2610 case bulk_latency: /* 250 usec aka 4000 ints/s */
2611 if (bytes > 25000) {
2612 if (packets > 35)
2613 retval = low_latency;
Jesse Brandeburg2b653262006-12-15 10:30:44 +01002614 } else if (bytes < 6000) {
2615 retval = low_latency;
Jesse Brandeburg835bb122006-11-01 08:48:13 -08002616 }
2617 break;
2618 }
2619
2620update_itr_done:
2621 return retval;
2622}
2623
2624static void e1000_set_itr(struct e1000_adapter *adapter)
2625{
2626 struct e1000_hw *hw = &adapter->hw;
Joe Perches406874a2008-04-03 10:06:32 -07002627 u16 current_itr;
2628 u32 new_itr = adapter->itr;
Jesse Brandeburg835bb122006-11-01 08:48:13 -08002629
2630 if (unlikely(hw->mac_type < e1000_82540))
2631 return;
2632
2633 /* for non-gigabit speeds, just fix the interrupt rate at 4000 */
2634 if (unlikely(adapter->link_speed != SPEED_1000)) {
2635 current_itr = 0;
2636 new_itr = 4000;
2637 goto set_itr_now;
2638 }
2639
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00002640 adapter->tx_itr = e1000_update_itr(adapter, adapter->tx_itr,
2641 adapter->total_tx_packets,
2642 adapter->total_tx_bytes);
Jesse Brandeburg2b653262006-12-15 10:30:44 +01002643 /* conservative mode (itr 3) eliminates the lowest_latency setting */
2644 if (adapter->itr_setting == 3 && adapter->tx_itr == lowest_latency)
2645 adapter->tx_itr = low_latency;
2646
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00002647 adapter->rx_itr = e1000_update_itr(adapter, adapter->rx_itr,
2648 adapter->total_rx_packets,
2649 adapter->total_rx_bytes);
Jesse Brandeburg2b653262006-12-15 10:30:44 +01002650 /* conservative mode (itr 3) eliminates the lowest_latency setting */
2651 if (adapter->itr_setting == 3 && adapter->rx_itr == lowest_latency)
2652 adapter->rx_itr = low_latency;
Jesse Brandeburg835bb122006-11-01 08:48:13 -08002653
2654 current_itr = max(adapter->rx_itr, adapter->tx_itr);
2655
Jesse Brandeburg835bb122006-11-01 08:48:13 -08002656 switch (current_itr) {
2657 /* counts and packets in update_itr are dependent on these numbers */
2658 case lowest_latency:
2659 new_itr = 70000;
2660 break;
2661 case low_latency:
2662 new_itr = 20000; /* aka hwitr = ~200 */
2663 break;
2664 case bulk_latency:
2665 new_itr = 4000;
2666 break;
2667 default:
2668 break;
2669 }
2670
2671set_itr_now:
2672 if (new_itr != adapter->itr) {
2673 /* this attempts to bias the interrupt rate towards Bulk
2674 * by adding intermediate steps when interrupt rate is
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00002675 * increasing
2676 */
Jesse Brandeburg835bb122006-11-01 08:48:13 -08002677 new_itr = new_itr > adapter->itr ?
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00002678 min(adapter->itr + (new_itr >> 2), new_itr) :
2679 new_itr;
Jesse Brandeburg835bb122006-11-01 08:48:13 -08002680 adapter->itr = new_itr;
Joe Perches1dc32912008-07-11 15:17:08 -07002681 ew32(ITR, 1000000000 / (new_itr * 256));
Jesse Brandeburg835bb122006-11-01 08:48:13 -08002682 }
Jesse Brandeburg835bb122006-11-01 08:48:13 -08002683}
2684
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685#define E1000_TX_FLAGS_CSUM 0x00000001
2686#define E1000_TX_FLAGS_VLAN 0x00000002
2687#define E1000_TX_FLAGS_TSO 0x00000004
Malli Chilakala2d7edb92005-04-28 19:43:52 -07002688#define E1000_TX_FLAGS_IPV4 0x00000008
Ben Greear11a78dc2012-02-11 15:40:01 +00002689#define E1000_TX_FLAGS_NO_FCS 0x00000010
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690#define E1000_TX_FLAGS_VLAN_MASK 0xffff0000
2691#define E1000_TX_FLAGS_VLAN_SHIFT 16
2692
Joe Perches64798842008-07-11 15:17:02 -07002693static int e1000_tso(struct e1000_adapter *adapter,
2694 struct e1000_tx_ring *tx_ring, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 struct e1000_context_desc *context_desc;
Jeff Kirsher545c67c2006-01-12 16:50:25 -08002697 struct e1000_buffer *buffer_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 unsigned int i;
Joe Perches406874a2008-04-03 10:06:32 -07002699 u32 cmd_length = 0;
2700 u16 ipcse = 0, tucse, mss;
2701 u8 ipcss, ipcso, tucss, tucso, hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 int err;
2703
Herbert Xu89114af2006-07-08 13:34:32 -07002704 if (skb_is_gso(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 if (skb_header_cloned(skb)) {
2706 err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2707 if (err)
2708 return err;
2709 }
2710
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07002711 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
Herbert Xu79671682006-06-22 02:40:14 -07002712 mss = skb_shinfo(skb)->gso_size;
Alexey Dobriyan60828232006-05-23 14:52:21 -07002713 if (skb->protocol == htons(ETH_P_IP)) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002714 struct iphdr *iph = ip_hdr(skb);
2715 iph->tot_len = 0;
2716 iph->check = 0;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07002717 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
2718 iph->daddr, 0,
2719 IPPROTO_TCP,
2720 0);
Malli Chilakala2d7edb92005-04-28 19:43:52 -07002721 cmd_length = E1000_TXD_CMD_IP;
Arnaldo Carvalho de Meloea2ae172007-04-25 17:55:53 -07002722 ipcse = skb_transport_offset(skb) - 1;
Auke Koke15fdd02006-08-16 11:28:45 -07002723 } else if (skb->protocol == htons(ETH_P_IPV6)) {
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07002724 ipv6_hdr(skb)->payload_len = 0;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07002725 tcp_hdr(skb)->check =
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07002726 ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
2727 &ipv6_hdr(skb)->daddr,
2728 0, IPPROTO_TCP, 0);
Malli Chilakala2d7edb92005-04-28 19:43:52 -07002729 ipcse = 0;
Malli Chilakala2d7edb92005-04-28 19:43:52 -07002730 }
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03002731 ipcss = skb_network_offset(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002732 ipcso = (void *)&(ip_hdr(skb)->check) - (void *)skb->data;
Arnaldo Carvalho de Meloea2ae172007-04-25 17:55:53 -07002733 tucss = skb_transport_offset(skb);
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07002734 tucso = (void *)&(tcp_hdr(skb)->check) - (void *)skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 tucse = 0;
2736
2737 cmd_length |= (E1000_TXD_CMD_DEXT | E1000_TXD_CMD_TSE |
Malli Chilakala2d7edb92005-04-28 19:43:52 -07002738 E1000_TXD_CMD_TCP | (skb->len - (hdr_len)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002740 i = tx_ring->next_to_use;
2741 context_desc = E1000_CONTEXT_DESC(*tx_ring, i);
Jeff Kirsher545c67c2006-01-12 16:50:25 -08002742 buffer_info = &tx_ring->buffer_info[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743
2744 context_desc->lower_setup.ip_fields.ipcss = ipcss;
2745 context_desc->lower_setup.ip_fields.ipcso = ipcso;
2746 context_desc->lower_setup.ip_fields.ipcse = cpu_to_le16(ipcse);
2747 context_desc->upper_setup.tcp_fields.tucss = tucss;
2748 context_desc->upper_setup.tcp_fields.tucso = tucso;
2749 context_desc->upper_setup.tcp_fields.tucse = cpu_to_le16(tucse);
2750 context_desc->tcp_seg_setup.fields.mss = cpu_to_le16(mss);
2751 context_desc->tcp_seg_setup.fields.hdr_len = hdr_len;
2752 context_desc->cmd_and_length = cpu_to_le32(cmd_length);
2753
Jeff Kirsher545c67c2006-01-12 16:50:25 -08002754 buffer_info->time_stamp = jiffies;
Jesse Brandeburga9ebadd2006-11-01 08:47:53 -08002755 buffer_info->next_to_watch = i;
Jeff Kirsher545c67c2006-01-12 16:50:25 -08002756
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002757 if (++i == tx_ring->count) i = 0;
2758 tx_ring->next_to_use = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759
Joe Perchesc3033b02008-03-21 11:06:25 -07002760 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 }
Joe Perchesc3033b02008-03-21 11:06:25 -07002762 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763}
2764
Joe Perches64798842008-07-11 15:17:02 -07002765static bool e1000_tx_csum(struct e1000_adapter *adapter,
2766 struct e1000_tx_ring *tx_ring, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767{
2768 struct e1000_context_desc *context_desc;
Jeff Kirsher545c67c2006-01-12 16:50:25 -08002769 struct e1000_buffer *buffer_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 unsigned int i;
Joe Perches406874a2008-04-03 10:06:32 -07002771 u8 css;
Dave Graham3ed30672008-10-09 14:29:26 -07002772 u32 cmd_len = E1000_TXD_CMD_DEXT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773
Dave Graham3ed30672008-10-09 14:29:26 -07002774 if (skb->ip_summed != CHECKSUM_PARTIAL)
2775 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776
Dave Graham3ed30672008-10-09 14:29:26 -07002777 switch (skb->protocol) {
Harvey Harrison09640e62009-02-01 00:45:17 -08002778 case cpu_to_be16(ETH_P_IP):
Dave Graham3ed30672008-10-09 14:29:26 -07002779 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
2780 cmd_len |= E1000_TXD_CMD_TCP;
2781 break;
Harvey Harrison09640e62009-02-01 00:45:17 -08002782 case cpu_to_be16(ETH_P_IPV6):
Dave Graham3ed30672008-10-09 14:29:26 -07002783 /* XXX not handling all IPV6 headers */
2784 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
2785 cmd_len |= E1000_TXD_CMD_TCP;
2786 break;
2787 default:
2788 if (unlikely(net_ratelimit()))
Emil Tantilovfeb8f472010-07-26 23:37:21 -07002789 e_warn(drv, "checksum_partial proto=%x!\n",
2790 skb->protocol);
Dave Graham3ed30672008-10-09 14:29:26 -07002791 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 }
2793
Michał Mirosław0d0b1672010-12-14 15:24:08 +00002794 css = skb_checksum_start_offset(skb);
Dave Graham3ed30672008-10-09 14:29:26 -07002795
2796 i = tx_ring->next_to_use;
2797 buffer_info = &tx_ring->buffer_info[i];
2798 context_desc = E1000_CONTEXT_DESC(*tx_ring, i);
2799
2800 context_desc->lower_setup.ip_config = 0;
2801 context_desc->upper_setup.tcp_fields.tucss = css;
2802 context_desc->upper_setup.tcp_fields.tucso =
2803 css + skb->csum_offset;
2804 context_desc->upper_setup.tcp_fields.tucse = 0;
2805 context_desc->tcp_seg_setup.data = 0;
2806 context_desc->cmd_and_length = cpu_to_le32(cmd_len);
2807
2808 buffer_info->time_stamp = jiffies;
2809 buffer_info->next_to_watch = i;
2810
2811 if (unlikely(++i == tx_ring->count)) i = 0;
2812 tx_ring->next_to_use = i;
2813
2814 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815}
2816
2817#define E1000_MAX_TXD_PWR 12
2818#define E1000_MAX_DATA_PER_TXD (1<<E1000_MAX_TXD_PWR)
2819
Joe Perches64798842008-07-11 15:17:02 -07002820static int e1000_tx_map(struct e1000_adapter *adapter,
2821 struct e1000_tx_ring *tx_ring,
2822 struct sk_buff *skb, unsigned int first,
2823 unsigned int max_per_txd, unsigned int nr_frags,
2824 unsigned int mss)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825{
Joe Perches1dc32912008-07-11 15:17:08 -07002826 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck602c0552009-12-02 16:46:00 +00002827 struct pci_dev *pdev = adapter->pdev;
Alexander Duyck37e73df2009-03-25 21:58:45 +00002828 struct e1000_buffer *buffer_info;
Jesse Brandeburgd20b6062009-03-02 16:03:21 -08002829 unsigned int len = skb_headlen(skb);
Alexander Duyck602c0552009-12-02 16:46:00 +00002830 unsigned int offset = 0, size, count = 0, i;
Dean Nelson31c15a22011-08-25 14:39:24 +00002831 unsigned int f, bytecount, segs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832
2833 i = tx_ring->next_to_use;
2834
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002835 while (len) {
Alexander Duyck37e73df2009-03-25 21:58:45 +00002836 buffer_info = &tx_ring->buffer_info[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 size = min(len, max_per_txd);
Jeff Kirsherfd803242005-12-13 00:06:22 -05002838 /* Workaround for Controller erratum --
2839 * descriptor for non-tso packet in a linear SKB that follows a
2840 * tso gets written back prematurely before the data is fully
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00002841 * DMA'd to the controller
2842 */
Jeff Kirsherfd803242005-12-13 00:06:22 -05002843 if (!skb->data_len && tx_ring->last_tx_tso &&
Herbert Xu89114af2006-07-08 13:34:32 -07002844 !skb_is_gso(skb)) {
Rusty Russell3db1cd52011-12-19 13:56:45 +00002845 tx_ring->last_tx_tso = false;
Jeff Kirsherfd803242005-12-13 00:06:22 -05002846 size -= 4;
2847 }
2848
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 /* Workaround for premature desc write-backs
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00002850 * in TSO mode. Append 4-byte sentinel desc
2851 */
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002852 if (unlikely(mss && !nr_frags && size == len && size > 8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 size -= 4;
Malli Chilakala97338bd2005-04-28 19:41:46 -07002854 /* work-around for errata 10 and it applies
2855 * to all controllers in PCI-X mode
2856 * The fix is to make sure that the first descriptor of a
2857 * packet is smaller than 2048 - 16 - 16 (or 2016) bytes
2858 */
Joe Perches1dc32912008-07-11 15:17:08 -07002859 if (unlikely((hw->bus_type == e1000_bus_type_pcix) &&
Malli Chilakala97338bd2005-04-28 19:41:46 -07002860 (size > 2015) && count == 0))
2861 size = 2015;
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002862
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 /* Workaround for potential 82544 hang in PCI-X. Avoid
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00002864 * terminating buffers within evenly-aligned dwords.
2865 */
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002866 if (unlikely(adapter->pcix_82544 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 !((unsigned long)(skb->data + offset + size - 1) & 4) &&
2868 size > 4))
2869 size -= 4;
2870
2871 buffer_info->length = size;
Jesse Brandeburgcdd75492009-09-25 12:18:07 +00002872 /* set time_stamp *before* dma to help avoid a possible race */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 buffer_info->time_stamp = jiffies;
Alexander Duyck602c0552009-12-02 16:46:00 +00002874 buffer_info->mapped_as_page = false;
Nick Nunleyb16f53b2010-04-27 13:08:45 +00002875 buffer_info->dma = dma_map_single(&pdev->dev,
2876 skb->data + offset,
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00002877 size, DMA_TO_DEVICE);
Nick Nunleyb16f53b2010-04-27 13:08:45 +00002878 if (dma_mapping_error(&pdev->dev, buffer_info->dma))
Alexander Duyck602c0552009-12-02 16:46:00 +00002879 goto dma_error;
Jesse Brandeburga9ebadd2006-11-01 08:47:53 -08002880 buffer_info->next_to_watch = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881
2882 len -= size;
2883 offset += size;
2884 count++;
Alexander Duyck37e73df2009-03-25 21:58:45 +00002885 if (len) {
2886 i++;
2887 if (unlikely(i == tx_ring->count))
2888 i = 0;
2889 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 }
2891
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002892 for (f = 0; f < nr_frags; f++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00002893 const struct skb_frag_struct *frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894
2895 frag = &skb_shinfo(skb)->frags[f];
Eric Dumazet9e903e02011-10-18 21:00:24 +00002896 len = skb_frag_size(frag);
Ian Campbell877749b2011-08-29 23:18:26 +00002897 offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002899 while (len) {
Ian Campbell877749b2011-08-29 23:18:26 +00002900 unsigned long bufend;
Alexander Duyck37e73df2009-03-25 21:58:45 +00002901 i++;
2902 if (unlikely(i == tx_ring->count))
2903 i = 0;
2904
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 buffer_info = &tx_ring->buffer_info[i];
2906 size = min(len, max_per_txd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 /* Workaround for premature desc write-backs
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00002908 * in TSO mode. Append 4-byte sentinel desc
2909 */
2910 if (unlikely(mss && f == (nr_frags-1) &&
2911 size == len && size > 8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 size -= 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 /* Workaround for potential 82544 hang in PCI-X.
2914 * Avoid terminating buffers within evenly-aligned
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00002915 * dwords.
2916 */
Ian Campbell877749b2011-08-29 23:18:26 +00002917 bufend = (unsigned long)
2918 page_to_phys(skb_frag_page(frag));
2919 bufend += offset + size - 1;
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002920 if (unlikely(adapter->pcix_82544 &&
Ian Campbell877749b2011-08-29 23:18:26 +00002921 !(bufend & 4) &&
2922 size > 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 size -= 4;
2924
2925 buffer_info->length = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 buffer_info->time_stamp = jiffies;
Alexander Duyck602c0552009-12-02 16:46:00 +00002927 buffer_info->mapped_as_page = true;
Ian Campbell877749b2011-08-29 23:18:26 +00002928 buffer_info->dma = skb_frag_dma_map(&pdev->dev, frag,
2929 offset, size, DMA_TO_DEVICE);
Nick Nunleyb16f53b2010-04-27 13:08:45 +00002930 if (dma_mapping_error(&pdev->dev, buffer_info->dma))
Alexander Duyck602c0552009-12-02 16:46:00 +00002931 goto dma_error;
Jesse Brandeburga9ebadd2006-11-01 08:47:53 -08002932 buffer_info->next_to_watch = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933
2934 len -= size;
2935 offset += size;
2936 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 }
2938 }
2939
Dean Nelson31c15a22011-08-25 14:39:24 +00002940 segs = skb_shinfo(skb)->gso_segs ?: 1;
2941 /* multiply data chunks by size of headers */
2942 bytecount = ((segs - 1) * skb_headlen(skb)) + skb->len;
2943
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 tx_ring->buffer_info[i].skb = skb;
Dean Nelson31c15a22011-08-25 14:39:24 +00002945 tx_ring->buffer_info[i].segs = segs;
2946 tx_ring->buffer_info[i].bytecount = bytecount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 tx_ring->buffer_info[first].next_to_watch = i;
2948
2949 return count;
Alexander Duyck602c0552009-12-02 16:46:00 +00002950
2951dma_error:
2952 dev_err(&pdev->dev, "TX DMA map failed\n");
2953 buffer_info->dma = 0;
Roel Kluinc1fa3472010-01-19 14:21:45 +00002954 if (count)
Alexander Duyck602c0552009-12-02 16:46:00 +00002955 count--;
Roel Kluinc1fa3472010-01-19 14:21:45 +00002956
2957 while (count--) {
2958 if (i==0)
Alexander Duyck602c0552009-12-02 16:46:00 +00002959 i += tx_ring->count;
Roel Kluinc1fa3472010-01-19 14:21:45 +00002960 i--;
Alexander Duyck602c0552009-12-02 16:46:00 +00002961 buffer_info = &tx_ring->buffer_info[i];
2962 e1000_unmap_and_free_tx_resource(adapter, buffer_info);
2963 }
2964
2965 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966}
2967
Joe Perches64798842008-07-11 15:17:02 -07002968static void e1000_tx_queue(struct e1000_adapter *adapter,
2969 struct e1000_tx_ring *tx_ring, int tx_flags,
2970 int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971{
Joe Perches1dc32912008-07-11 15:17:08 -07002972 struct e1000_hw *hw = &adapter->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 struct e1000_tx_desc *tx_desc = NULL;
2974 struct e1000_buffer *buffer_info;
Joe Perches406874a2008-04-03 10:06:32 -07002975 u32 txd_upper = 0, txd_lower = E1000_TXD_CMD_IFCS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 unsigned int i;
2977
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002978 if (likely(tx_flags & E1000_TX_FLAGS_TSO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 txd_lower |= E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D |
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00002980 E1000_TXD_CMD_TSE;
Malli Chilakala2d7edb92005-04-28 19:43:52 -07002981 txd_upper |= E1000_TXD_POPTS_TXSM << 8;
2982
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002983 if (likely(tx_flags & E1000_TX_FLAGS_IPV4))
Malli Chilakala2d7edb92005-04-28 19:43:52 -07002984 txd_upper |= E1000_TXD_POPTS_IXSM << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 }
2986
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002987 if (likely(tx_flags & E1000_TX_FLAGS_CSUM)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 txd_lower |= E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D;
2989 txd_upper |= E1000_TXD_POPTS_TXSM << 8;
2990 }
2991
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002992 if (unlikely(tx_flags & E1000_TX_FLAGS_VLAN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 txd_lower |= E1000_TXD_CMD_VLE;
2994 txd_upper |= (tx_flags & E1000_TX_FLAGS_VLAN_MASK);
2995 }
2996
Ben Greear11a78dc2012-02-11 15:40:01 +00002997 if (unlikely(tx_flags & E1000_TX_FLAGS_NO_FCS))
2998 txd_lower &= ~(E1000_TXD_CMD_IFCS);
2999
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 i = tx_ring->next_to_use;
3001
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003002 while (count--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 buffer_info = &tx_ring->buffer_info[i];
3004 tx_desc = E1000_TX_DESC(*tx_ring, i);
3005 tx_desc->buffer_addr = cpu_to_le64(buffer_info->dma);
3006 tx_desc->lower.data =
3007 cpu_to_le32(txd_lower | buffer_info->length);
3008 tx_desc->upper.data = cpu_to_le32(txd_upper);
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003009 if (unlikely(++i == tx_ring->count)) i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 }
3011
3012 tx_desc->lower.data |= cpu_to_le32(adapter->txd_cmd);
3013
Ben Greear11a78dc2012-02-11 15:40:01 +00003014 /* txd_cmd re-enables FCS, so we'll re-disable it here as desired. */
3015 if (unlikely(tx_flags & E1000_TX_FLAGS_NO_FCS))
3016 tx_desc->lower.data &= ~(cpu_to_le32(E1000_TXD_CMD_IFCS));
3017
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 /* Force memory writes to complete before letting h/w
3019 * know there are new descriptors to fetch. (Only
3020 * applicable for weak-ordered memory model archs,
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00003021 * such as IA-64).
3022 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 wmb();
3024
3025 tx_ring->next_to_use = i;
Joe Perches1dc32912008-07-11 15:17:08 -07003026 writel(i, hw->hw_addr + tx_ring->tdt);
Jesse Brandeburg2ce90472006-11-01 08:47:42 -08003027 /* we need this if more than one processor can write to our tail
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00003028 * at a time, it synchronizes IO on IA64/Altix systems
3029 */
Jesse Brandeburg2ce90472006-11-01 08:47:42 -08003030 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031}
3032
Ben Hutchings1aa8b472012-07-10 10:56:59 +00003033/* 82547 workaround to avoid controller hang in half-duplex environment.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 * The workaround is to avoid queuing a large packet that would span
3035 * the internal Tx FIFO ring boundary by notifying the stack to resend
3036 * the packet at a later time. This gives the Tx FIFO an opportunity to
3037 * flush all packets. When that occurs, we reset the Tx FIFO pointers
3038 * to the beginning of the Tx FIFO.
Ben Hutchings1aa8b472012-07-10 10:56:59 +00003039 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040
3041#define E1000_FIFO_HDR 0x10
3042#define E1000_82547_PAD_LEN 0x3E0
3043
Joe Perches64798842008-07-11 15:17:02 -07003044static int e1000_82547_fifo_workaround(struct e1000_adapter *adapter,
3045 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046{
Joe Perches406874a2008-04-03 10:06:32 -07003047 u32 fifo_space = adapter->tx_fifo_size - adapter->tx_fifo_head;
3048 u32 skb_fifo_len = skb->len + E1000_FIFO_HDR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049
Milind Arun Choudhary9099cfb2007-04-27 13:55:29 -07003050 skb_fifo_len = ALIGN(skb_fifo_len, E1000_FIFO_HDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003052 if (adapter->link_duplex != HALF_DUPLEX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 goto no_fifo_stall_required;
3054
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003055 if (atomic_read(&adapter->tx_fifo_stall))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 return 1;
3057
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003058 if (skb_fifo_len >= (E1000_82547_PAD_LEN + fifo_space)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 atomic_set(&adapter->tx_fifo_stall, 1);
3060 return 1;
3061 }
3062
3063no_fifo_stall_required:
3064 adapter->tx_fifo_head += skb_fifo_len;
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003065 if (adapter->tx_fifo_head >= adapter->tx_fifo_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 adapter->tx_fifo_head -= adapter->tx_fifo_size;
3067 return 0;
3068}
3069
Jesse Brandeburg65c79732006-09-27 12:53:48 -07003070static int __e1000_maybe_stop_tx(struct net_device *netdev, int size)
3071{
3072 struct e1000_adapter *adapter = netdev_priv(netdev);
3073 struct e1000_tx_ring *tx_ring = adapter->tx_ring;
3074
3075 netif_stop_queue(netdev);
3076 /* Herbert's original patch had:
3077 * smp_mb__after_netif_stop_queue();
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00003078 * but since that doesn't exist yet, just open code it.
3079 */
Jesse Brandeburg65c79732006-09-27 12:53:48 -07003080 smp_mb();
3081
3082 /* We need to check again in a case another CPU has just
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00003083 * made room available.
3084 */
Jesse Brandeburg65c79732006-09-27 12:53:48 -07003085 if (likely(E1000_DESC_UNUSED(tx_ring) < size))
3086 return -EBUSY;
3087
3088 /* A reprieve! */
3089 netif_start_queue(netdev);
Jesse Brandeburgfcfb1222006-11-01 08:47:59 -08003090 ++adapter->restart_queue;
Jesse Brandeburg65c79732006-09-27 12:53:48 -07003091 return 0;
3092}
3093
3094static int e1000_maybe_stop_tx(struct net_device *netdev,
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00003095 struct e1000_tx_ring *tx_ring, int size)
Jesse Brandeburg65c79732006-09-27 12:53:48 -07003096{
3097 if (likely(E1000_DESC_UNUSED(tx_ring) >= size))
3098 return 0;
3099 return __e1000_maybe_stop_tx(netdev, size);
3100}
3101
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102#define TXD_USE_COUNT(S, X) (((S) >> (X)) + 1 )
Stephen Hemminger3b29a562009-08-31 19:50:55 +00003103static netdev_tx_t e1000_xmit_frame(struct sk_buff *skb,
3104 struct net_device *netdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105{
Malli Chilakala60490fe2005-06-17 17:41:45 -07003106 struct e1000_adapter *adapter = netdev_priv(netdev);
Joe Perches1dc32912008-07-11 15:17:08 -07003107 struct e1000_hw *hw = &adapter->hw;
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003108 struct e1000_tx_ring *tx_ring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 unsigned int first, max_per_txd = E1000_MAX_DATA_PER_TXD;
3110 unsigned int max_txd_pwr = E1000_MAX_TXD_PWR;
3111 unsigned int tx_flags = 0;
Eric Dumazete743d312010-04-14 15:59:40 -07003112 unsigned int len = skb_headlen(skb);
Krishna Kumar6d1e3aa2007-10-05 14:15:16 -07003113 unsigned int nr_frags;
3114 unsigned int mss;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 int count = 0;
Auke Kok76c224b2006-05-23 13:36:06 -07003116 int tso;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 unsigned int f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00003119 /* This goes back to the question of how to logically map a Tx queue
Jesse Brandeburg65c79732006-09-27 12:53:48 -07003120 * to a flow. Right now, performance is impacted slightly negatively
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00003121 * if using multiple Tx queues. If the stack breaks away from a
3122 * single qdisc implementation, we can look at this again.
3123 */
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003124 tx_ring = adapter->tx_ring;
Mallikarjuna R Chilakala24025e4ec2005-10-04 07:03:23 -04003125
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003126 if (unlikely(skb->len <= 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 dev_kfree_skb_any(skb);
3128 return NETDEV_TX_OK;
3129 }
3130
Tushar Dave59d86c72012-09-15 10:16:57 +00003131 /* On PCI/PCI-X HW, if packet size is less than ETH_ZLEN,
3132 * packets may get corrupted during padding by HW.
3133 * To WA this issue, pad all small packets manually.
3134 */
3135 if (skb->len < ETH_ZLEN) {
3136 if (skb_pad(skb, ETH_ZLEN - skb->len))
3137 return NETDEV_TX_OK;
3138 skb->len = ETH_ZLEN;
3139 skb_set_tail_pointer(skb, ETH_ZLEN);
3140 }
3141
Herbert Xu79671682006-06-22 02:40:14 -07003142 mss = skb_shinfo(skb)->gso_size;
Auke Kok76c224b2006-05-23 13:36:06 -07003143 /* The controller does a simple calculation to
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 * make sure there is enough room in the FIFO before
3145 * initiating the DMA for each buffer. The calc is:
3146 * 4 = ceil(buffer len/mss). To make sure we don't
3147 * overrun the FIFO, adjust the max buffer len if mss
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00003148 * drops.
3149 */
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003150 if (mss) {
Joe Perches406874a2008-04-03 10:06:32 -07003151 u8 hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 max_per_txd = min(mss << 2, max_per_txd);
3153 max_txd_pwr = fls(max_per_txd) - 1;
Jeff Kirsher9a3056d2006-01-12 16:50:23 -08003154
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07003155 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
Krishna Kumar6d1e3aa2007-10-05 14:15:16 -07003156 if (skb->data_len && hdr_len == len) {
Joe Perches1dc32912008-07-11 15:17:08 -07003157 switch (hw->mac_type) {
Jeff Kirsher9f687882006-03-02 18:20:17 -08003158 unsigned int pull_size;
Herbert Xu683a2aa2006-12-16 12:04:33 +11003159 case e1000_82544:
3160 /* Make sure we have room to chop off 4 bytes,
3161 * and that the end alignment will work out to
3162 * this hardware's requirements
3163 * NOTE: this is a TSO only workaround
3164 * if end byte alignment not correct move us
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00003165 * into the next dword
3166 */
3167 if ((unsigned long)(skb_tail_pointer(skb) - 1)
3168 & 4)
Herbert Xu683a2aa2006-12-16 12:04:33 +11003169 break;
3170 /* fall through */
Jeff Kirsher9f687882006-03-02 18:20:17 -08003171 pull_size = min((unsigned int)4, skb->data_len);
3172 if (!__pskb_pull_tail(skb, pull_size)) {
Emil Tantilovfeb8f472010-07-26 23:37:21 -07003173 e_err(drv, "__pskb_pull_tail "
3174 "failed.\n");
Jeff Kirsher9f687882006-03-02 18:20:17 -08003175 dev_kfree_skb_any(skb);
Jeff Garzik749dfc702006-03-11 13:35:31 -05003176 return NETDEV_TX_OK;
Jeff Kirsher9f687882006-03-02 18:20:17 -08003177 }
Eric Dumazete743d312010-04-14 15:59:40 -07003178 len = skb_headlen(skb);
Jeff Kirsher9f687882006-03-02 18:20:17 -08003179 break;
3180 default:
3181 /* do nothing */
3182 break;
Jeff Kirsherd74bbd32006-01-12 16:51:07 -08003183 }
Jeff Kirsher9a3056d2006-01-12 16:50:23 -08003184 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 }
3186
Jeff Kirsher9a3056d2006-01-12 16:50:23 -08003187 /* reserve a descriptor for the offload context */
Patrick McHardy84fa7932006-08-29 16:44:56 -07003188 if ((mss) || (skb->ip_summed == CHECKSUM_PARTIAL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 count++;
Malli Chilakala26483452005-04-28 19:44:46 -07003190 count++;
Jeff Kirsherfd803242005-12-13 00:06:22 -05003191
Jeff Kirsherfd803242005-12-13 00:06:22 -05003192 /* Controller Erratum workaround */
Herbert Xu89114af2006-07-08 13:34:32 -07003193 if (!skb->data_len && tx_ring->last_tx_tso && !skb_is_gso(skb))
Jeff Kirsherfd803242005-12-13 00:06:22 -05003194 count++;
Jeff Kirsherfd803242005-12-13 00:06:22 -05003195
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 count += TXD_USE_COUNT(len, max_txd_pwr);
3197
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003198 if (adapter->pcix_82544)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 count++;
3200
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003201 /* work-around for errata 10 and it applies to all controllers
Malli Chilakala97338bd2005-04-28 19:41:46 -07003202 * in PCI-X mode, so add one more descriptor to the count
3203 */
Joe Perches1dc32912008-07-11 15:17:08 -07003204 if (unlikely((hw->bus_type == e1000_bus_type_pcix) &&
Malli Chilakala97338bd2005-04-28 19:41:46 -07003205 (len > 2015)))
3206 count++;
3207
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 nr_frags = skb_shinfo(skb)->nr_frags;
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003209 for (f = 0; f < nr_frags; f++)
Eric Dumazet9e903e02011-10-18 21:00:24 +00003210 count += TXD_USE_COUNT(skb_frag_size(&skb_shinfo(skb)->frags[f]),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 max_txd_pwr);
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003212 if (adapter->pcix_82544)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 count += nr_frags;
3214
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 /* need: count + 2 desc gap to keep tail from touching
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00003216 * head, otherwise try next time
3217 */
Alexander Duyck80179432009-01-21 14:42:47 -08003218 if (unlikely(e1000_maybe_stop_tx(netdev, tx_ring, count + 2)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220
Jesse Brandeburga4010af2011-10-05 07:24:41 +00003221 if (unlikely((hw->mac_type == e1000_82547) &&
3222 (e1000_82547_fifo_workaround(adapter, skb)))) {
3223 netif_stop_queue(netdev);
3224 if (!test_bit(__E1000_DOWN, &adapter->flags))
3225 schedule_delayed_work(&adapter->fifo_stall_task, 1);
3226 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 }
3228
Jiri Pirko5622e402011-07-21 03:26:31 +00003229 if (vlan_tx_tag_present(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 tx_flags |= E1000_TX_FLAGS_VLAN;
3231 tx_flags |= (vlan_tx_tag_get(skb) << E1000_TX_FLAGS_VLAN_SHIFT);
3232 }
3233
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003234 first = tx_ring->next_to_use;
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003235
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003236 tso = e1000_tso(adapter, tx_ring, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 if (tso < 0) {
3238 dev_kfree_skb_any(skb);
3239 return NETDEV_TX_OK;
3240 }
3241
Jeff Kirsherfd803242005-12-13 00:06:22 -05003242 if (likely(tso)) {
Jesse Brandeburg8fce4732009-09-25 12:18:41 +00003243 if (likely(hw->mac_type != e1000_82544))
Rusty Russell3db1cd52011-12-19 13:56:45 +00003244 tx_ring->last_tx_tso = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 tx_flags |= E1000_TX_FLAGS_TSO;
Jeff Kirsherfd803242005-12-13 00:06:22 -05003246 } else if (likely(e1000_tx_csum(adapter, tx_ring, skb)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 tx_flags |= E1000_TX_FLAGS_CSUM;
3248
Alexey Dobriyan60828232006-05-23 14:52:21 -07003249 if (likely(skb->protocol == htons(ETH_P_IP)))
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003250 tx_flags |= E1000_TX_FLAGS_IPV4;
3251
Ben Greear11a78dc2012-02-11 15:40:01 +00003252 if (unlikely(skb->no_fcs))
3253 tx_flags |= E1000_TX_FLAGS_NO_FCS;
3254
Alexander Duyck37e73df2009-03-25 21:58:45 +00003255 count = e1000_tx_map(adapter, tx_ring, skb, first, max_per_txd,
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00003256 nr_frags, mss);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257
Alexander Duyck37e73df2009-03-25 21:58:45 +00003258 if (count) {
Otto Estuardo Solares Cabrera2f66fd32012-08-24 08:04:58 +00003259 netdev_sent_queue(netdev, skb->len);
Willem de Bruijneab467f2012-04-27 09:04:04 +00003260 skb_tx_timestamp(skb);
3261
Alexander Duyck37e73df2009-03-25 21:58:45 +00003262 e1000_tx_queue(adapter, tx_ring, tx_flags, count);
Alexander Duyck37e73df2009-03-25 21:58:45 +00003263 /* Make sure there is space in the ring for the next send. */
3264 e1000_maybe_stop_tx(netdev, tx_ring, MAX_SKB_FRAGS + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265
Alexander Duyck37e73df2009-03-25 21:58:45 +00003266 } else {
3267 dev_kfree_skb_any(skb);
3268 tx_ring->buffer_info[first].time_stamp = 0;
3269 tx_ring->next_to_use = first;
3270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 return NETDEV_TX_OK;
3273}
3274
Tushar Daveb04e36b2012-01-27 09:00:46 +00003275#define NUM_REGS 38 /* 1 based count */
3276static void e1000_regdump(struct e1000_adapter *adapter)
3277{
3278 struct e1000_hw *hw = &adapter->hw;
3279 u32 regs[NUM_REGS];
3280 u32 *regs_buff = regs;
3281 int i = 0;
3282
Tushar Davee29b5d82012-02-10 08:06:36 +00003283 static const char * const reg_name[] = {
3284 "CTRL", "STATUS",
3285 "RCTL", "RDLEN", "RDH", "RDT", "RDTR",
3286 "TCTL", "TDBAL", "TDBAH", "TDLEN", "TDH", "TDT",
3287 "TIDV", "TXDCTL", "TADV", "TARC0",
3288 "TDBAL1", "TDBAH1", "TDLEN1", "TDH1", "TDT1",
3289 "TXDCTL1", "TARC1",
3290 "CTRL_EXT", "ERT", "RDBAL", "RDBAH",
3291 "TDFH", "TDFT", "TDFHS", "TDFTS", "TDFPC",
3292 "RDFH", "RDFT", "RDFHS", "RDFTS", "RDFPC"
Tushar Daveb04e36b2012-01-27 09:00:46 +00003293 };
3294
3295 regs_buff[0] = er32(CTRL);
3296 regs_buff[1] = er32(STATUS);
3297
3298 regs_buff[2] = er32(RCTL);
3299 regs_buff[3] = er32(RDLEN);
3300 regs_buff[4] = er32(RDH);
3301 regs_buff[5] = er32(RDT);
3302 regs_buff[6] = er32(RDTR);
3303
3304 regs_buff[7] = er32(TCTL);
3305 regs_buff[8] = er32(TDBAL);
3306 regs_buff[9] = er32(TDBAH);
3307 regs_buff[10] = er32(TDLEN);
3308 regs_buff[11] = er32(TDH);
3309 regs_buff[12] = er32(TDT);
3310 regs_buff[13] = er32(TIDV);
3311 regs_buff[14] = er32(TXDCTL);
3312 regs_buff[15] = er32(TADV);
3313 regs_buff[16] = er32(TARC0);
3314
3315 regs_buff[17] = er32(TDBAL1);
3316 regs_buff[18] = er32(TDBAH1);
3317 regs_buff[19] = er32(TDLEN1);
3318 regs_buff[20] = er32(TDH1);
3319 regs_buff[21] = er32(TDT1);
3320 regs_buff[22] = er32(TXDCTL1);
3321 regs_buff[23] = er32(TARC1);
3322 regs_buff[24] = er32(CTRL_EXT);
3323 regs_buff[25] = er32(ERT);
3324 regs_buff[26] = er32(RDBAL0);
3325 regs_buff[27] = er32(RDBAH0);
3326 regs_buff[28] = er32(TDFH);
3327 regs_buff[29] = er32(TDFT);
3328 regs_buff[30] = er32(TDFHS);
3329 regs_buff[31] = er32(TDFTS);
3330 regs_buff[32] = er32(TDFPC);
3331 regs_buff[33] = er32(RDFH);
3332 regs_buff[34] = er32(RDFT);
3333 regs_buff[35] = er32(RDFHS);
3334 regs_buff[36] = er32(RDFTS);
3335 regs_buff[37] = er32(RDFPC);
3336
3337 pr_info("Register dump\n");
Tushar Davee29b5d82012-02-10 08:06:36 +00003338 for (i = 0; i < NUM_REGS; i++)
3339 pr_info("%-15s %08x\n", reg_name[i], regs_buff[i]);
Tushar Daveb04e36b2012-01-27 09:00:46 +00003340}
3341
3342/*
3343 * e1000_dump: Print registers, tx ring and rx ring
3344 */
3345static void e1000_dump(struct e1000_adapter *adapter)
3346{
3347 /* this code doesn't handle multiple rings */
3348 struct e1000_tx_ring *tx_ring = adapter->tx_ring;
3349 struct e1000_rx_ring *rx_ring = adapter->rx_ring;
3350 int i;
3351
3352 if (!netif_msg_hw(adapter))
3353 return;
3354
3355 /* Print Registers */
3356 e1000_regdump(adapter);
3357
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00003358 /* transmit dump */
Tushar Daveb04e36b2012-01-27 09:00:46 +00003359 pr_info("TX Desc ring0 dump\n");
3360
3361 /* Transmit Descriptor Formats - DEXT[29] is 0 (Legacy) or 1 (Extended)
3362 *
3363 * Legacy Transmit Descriptor
3364 * +--------------------------------------------------------------+
3365 * 0 | Buffer Address [63:0] (Reserved on Write Back) |
3366 * +--------------------------------------------------------------+
3367 * 8 | Special | CSS | Status | CMD | CSO | Length |
3368 * +--------------------------------------------------------------+
3369 * 63 48 47 36 35 32 31 24 23 16 15 0
3370 *
3371 * Extended Context Descriptor (DTYP=0x0) for TSO or checksum offload
3372 * 63 48 47 40 39 32 31 16 15 8 7 0
3373 * +----------------------------------------------------------------+
3374 * 0 | TUCSE | TUCS0 | TUCSS | IPCSE | IPCS0 | IPCSS |
3375 * +----------------------------------------------------------------+
3376 * 8 | MSS | HDRLEN | RSV | STA | TUCMD | DTYP | PAYLEN |
3377 * +----------------------------------------------------------------+
3378 * 63 48 47 40 39 36 35 32 31 24 23 20 19 0
3379 *
3380 * Extended Data Descriptor (DTYP=0x1)
3381 * +----------------------------------------------------------------+
3382 * 0 | Buffer Address [63:0] |
3383 * +----------------------------------------------------------------+
3384 * 8 | VLAN tag | POPTS | Rsvd | Status | Command | DTYP | DTALEN |
3385 * +----------------------------------------------------------------+
3386 * 63 48 47 40 39 36 35 32 31 24 23 20 19 0
3387 */
Tushar Davee29b5d82012-02-10 08:06:36 +00003388 pr_info("Tc[desc] [Ce CoCsIpceCoS] [MssHlRSCm0Plen] [bi->dma ] leng ntw timestmp bi->skb\n");
3389 pr_info("Td[desc] [address 63:0 ] [VlaPoRSCm1Dlen] [bi->dma ] leng ntw timestmp bi->skb\n");
Tushar Daveb04e36b2012-01-27 09:00:46 +00003390
3391 if (!netif_msg_tx_done(adapter))
3392 goto rx_ring_summary;
3393
3394 for (i = 0; tx_ring->desc && (i < tx_ring->count); i++) {
3395 struct e1000_tx_desc *tx_desc = E1000_TX_DESC(*tx_ring, i);
3396 struct e1000_buffer *buffer_info = &tx_ring->buffer_info[i];
Andrei Emeltchenkodd7f5c92012-03-25 17:49:25 +00003397 struct my_u { __le64 a; __le64 b; };
Tushar Daveb04e36b2012-01-27 09:00:46 +00003398 struct my_u *u = (struct my_u *)tx_desc;
Tushar Davee29b5d82012-02-10 08:06:36 +00003399 const char *type;
3400
Tushar Daveb04e36b2012-01-27 09:00:46 +00003401 if (i == tx_ring->next_to_use && i == tx_ring->next_to_clean)
Tushar Davee29b5d82012-02-10 08:06:36 +00003402 type = "NTC/U";
Tushar Daveb04e36b2012-01-27 09:00:46 +00003403 else if (i == tx_ring->next_to_use)
Tushar Davee29b5d82012-02-10 08:06:36 +00003404 type = "NTU";
Tushar Daveb04e36b2012-01-27 09:00:46 +00003405 else if (i == tx_ring->next_to_clean)
Tushar Davee29b5d82012-02-10 08:06:36 +00003406 type = "NTC";
Tushar Daveb04e36b2012-01-27 09:00:46 +00003407 else
Tushar Davee29b5d82012-02-10 08:06:36 +00003408 type = "";
Tushar Daveb04e36b2012-01-27 09:00:46 +00003409
Tushar Davee29b5d82012-02-10 08:06:36 +00003410 pr_info("T%c[0x%03X] %016llX %016llX %016llX %04X %3X %016llX %p %s\n",
3411 ((le64_to_cpu(u->b) & (1<<20)) ? 'd' : 'c'), i,
3412 le64_to_cpu(u->a), le64_to_cpu(u->b),
3413 (u64)buffer_info->dma, buffer_info->length,
3414 buffer_info->next_to_watch,
3415 (u64)buffer_info->time_stamp, buffer_info->skb, type);
Tushar Daveb04e36b2012-01-27 09:00:46 +00003416 }
3417
3418rx_ring_summary:
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00003419 /* receive dump */
Tushar Daveb04e36b2012-01-27 09:00:46 +00003420 pr_info("\nRX Desc ring dump\n");
3421
3422 /* Legacy Receive Descriptor Format
3423 *
3424 * +-----------------------------------------------------+
3425 * | Buffer Address [63:0] |
3426 * +-----------------------------------------------------+
3427 * | VLAN Tag | Errors | Status 0 | Packet csum | Length |
3428 * +-----------------------------------------------------+
3429 * 63 48 47 40 39 32 31 16 15 0
3430 */
Tushar Davee29b5d82012-02-10 08:06:36 +00003431 pr_info("R[desc] [address 63:0 ] [vl er S cks ln] [bi->dma ] [bi->skb]\n");
Tushar Daveb04e36b2012-01-27 09:00:46 +00003432
3433 if (!netif_msg_rx_status(adapter))
3434 goto exit;
3435
3436 for (i = 0; rx_ring->desc && (i < rx_ring->count); i++) {
3437 struct e1000_rx_desc *rx_desc = E1000_RX_DESC(*rx_ring, i);
3438 struct e1000_buffer *buffer_info = &rx_ring->buffer_info[i];
Andrei Emeltchenkodd7f5c92012-03-25 17:49:25 +00003439 struct my_u { __le64 a; __le64 b; };
Tushar Daveb04e36b2012-01-27 09:00:46 +00003440 struct my_u *u = (struct my_u *)rx_desc;
Tushar Davee29b5d82012-02-10 08:06:36 +00003441 const char *type;
3442
Tushar Daveb04e36b2012-01-27 09:00:46 +00003443 if (i == rx_ring->next_to_use)
Tushar Davee29b5d82012-02-10 08:06:36 +00003444 type = "NTU";
Tushar Daveb04e36b2012-01-27 09:00:46 +00003445 else if (i == rx_ring->next_to_clean)
Tushar Davee29b5d82012-02-10 08:06:36 +00003446 type = "NTC";
Tushar Daveb04e36b2012-01-27 09:00:46 +00003447 else
Tushar Davee29b5d82012-02-10 08:06:36 +00003448 type = "";
Tushar Daveb04e36b2012-01-27 09:00:46 +00003449
Tushar Davee29b5d82012-02-10 08:06:36 +00003450 pr_info("R[0x%03X] %016llX %016llX %016llX %p %s\n",
3451 i, le64_to_cpu(u->a), le64_to_cpu(u->b),
3452 (u64)buffer_info->dma, buffer_info->skb, type);
Tushar Daveb04e36b2012-01-27 09:00:46 +00003453 } /* for */
3454
3455 /* dump the descriptor caches */
3456 /* rx */
Tushar Davee29b5d82012-02-10 08:06:36 +00003457 pr_info("Rx descriptor cache in 64bit format\n");
Tushar Daveb04e36b2012-01-27 09:00:46 +00003458 for (i = 0x6000; i <= 0x63FF ; i += 0x10) {
Tushar Davee29b5d82012-02-10 08:06:36 +00003459 pr_info("R%04X: %08X|%08X %08X|%08X\n",
3460 i,
3461 readl(adapter->hw.hw_addr + i+4),
3462 readl(adapter->hw.hw_addr + i),
3463 readl(adapter->hw.hw_addr + i+12),
3464 readl(adapter->hw.hw_addr + i+8));
Tushar Daveb04e36b2012-01-27 09:00:46 +00003465 }
3466 /* tx */
Tushar Davee29b5d82012-02-10 08:06:36 +00003467 pr_info("Tx descriptor cache in 64bit format\n");
Tushar Daveb04e36b2012-01-27 09:00:46 +00003468 for (i = 0x7000; i <= 0x73FF ; i += 0x10) {
Tushar Davee29b5d82012-02-10 08:06:36 +00003469 pr_info("T%04X: %08X|%08X %08X|%08X\n",
3470 i,
3471 readl(adapter->hw.hw_addr + i+4),
3472 readl(adapter->hw.hw_addr + i),
3473 readl(adapter->hw.hw_addr + i+12),
3474 readl(adapter->hw.hw_addr + i+8));
Tushar Daveb04e36b2012-01-27 09:00:46 +00003475 }
3476exit:
3477 return;
3478}
3479
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480/**
3481 * e1000_tx_timeout - Respond to a Tx Hang
3482 * @netdev: network interface device structure
3483 **/
Joe Perches64798842008-07-11 15:17:02 -07003484static void e1000_tx_timeout(struct net_device *netdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485{
Malli Chilakala60490fe2005-06-17 17:41:45 -07003486 struct e1000_adapter *adapter = netdev_priv(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487
3488 /* Do the reset outside of interrupt context */
Jeff Kirsher87041632006-03-02 18:21:24 -08003489 adapter->tx_timeout_count++;
3490 schedule_work(&adapter->reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491}
3492
Joe Perches64798842008-07-11 15:17:02 -07003493static void e1000_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494{
David Howells65f27f32006-11-22 14:55:48 +00003495 struct e1000_adapter *adapter =
3496 container_of(work, struct e1000_adapter, reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497
Jesse Brandeburg0ef4eed2011-10-05 07:24:51 +00003498 if (test_bit(__E1000_DOWN, &adapter->flags))
3499 return;
Tushar Daveb04e36b2012-01-27 09:00:46 +00003500 e_err(drv, "Reset adapter\n");
Jesse Brandeburg338c15e2010-09-22 18:22:42 +00003501 e1000_reinit_safe(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502}
3503
3504/**
3505 * e1000_get_stats - Get System Network Statistics
3506 * @netdev: network interface device structure
3507 *
3508 * Returns the address of the device statistics structure.
Jesse Brandeburga4010af2011-10-05 07:24:41 +00003509 * The statistics are actually updated from the watchdog.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510 **/
Joe Perches64798842008-07-11 15:17:02 -07003511static struct net_device_stats *e1000_get_stats(struct net_device *netdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512{
Jeff Kirsher6b7660c2006-01-12 16:50:35 -08003513 /* only return the current stats */
Ajit Khaparde5fe31de2009-10-07 02:42:23 +00003514 return &netdev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515}
3516
3517/**
3518 * e1000_change_mtu - Change the Maximum Transfer Unit
3519 * @netdev: network interface device structure
3520 * @new_mtu: new value for maximum frame size
3521 *
3522 * Returns 0 on success, negative on failure
3523 **/
Joe Perches64798842008-07-11 15:17:02 -07003524static int e1000_change_mtu(struct net_device *netdev, int new_mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525{
Malli Chilakala60490fe2005-06-17 17:41:45 -07003526 struct e1000_adapter *adapter = netdev_priv(netdev);
Joe Perches1dc32912008-07-11 15:17:08 -07003527 struct e1000_hw *hw = &adapter->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528 int max_frame = new_mtu + ENET_HEADER_SIZE + ETHERNET_FCS_SIZE;
3529
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003530 if ((max_frame < MINIMUM_ETHERNET_FRAME_SIZE) ||
3531 (max_frame > MAX_JUMBO_FRAME_SIZE)) {
Emil Tantilovfeb8f472010-07-26 23:37:21 -07003532 e_err(probe, "Invalid MTU setting\n");
Mallikarjuna R Chilakala868d5302005-10-04 06:58:59 -04003533 return -EINVAL;
3534 }
3535
Jeff Kirsher997f5cb2006-01-12 16:50:55 -08003536 /* Adapter-specific max frame size limits. */
Joe Perches1dc32912008-07-11 15:17:08 -07003537 switch (hw->mac_type) {
Auke Kok9e2feac2006-04-14 19:05:18 -07003538 case e1000_undefined ... e1000_82542_rev2_1:
Jesse Brandeburgb7cb8c22009-07-06 10:45:01 +00003539 if (max_frame > (ETH_FRAME_LEN + ETH_FCS_LEN)) {
Emil Tantilovfeb8f472010-07-26 23:37:21 -07003540 e_err(probe, "Jumbo Frames not supported.\n");
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003541 return -EINVAL;
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003542 }
Jeff Kirsher997f5cb2006-01-12 16:50:55 -08003543 break;
Jeff Kirsher997f5cb2006-01-12 16:50:55 -08003544 default:
3545 /* Capable of supporting up to MAX_JUMBO_FRAME_SIZE limit. */
3546 break;
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003547 }
3548
Jesse Brandeburg3d6114e2009-09-25 12:19:02 +00003549 while (test_and_set_bit(__E1000_RESETTING, &adapter->flags))
3550 msleep(1);
3551 /* e1000_down has a dependency on max_frame_size */
3552 hw->max_frame_size = max_frame;
3553 if (netif_running(netdev))
3554 e1000_down(adapter);
3555
David S. Miller87f50322006-07-31 22:39:40 -07003556 /* NOTE: netdev_alloc_skb reserves 16 bytes, and typically NET_IP_ALIGN
Auke Kok9e2feac2006-04-14 19:05:18 -07003557 * means we reserve 2 more, this pushes us to allocate from the next
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00003558 * larger slab size.
3559 * i.e. RXBUFFER_2048 --> size-4096 slab
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00003560 * however with the new *_jumbo_rx* routines, jumbo receives will use
3561 * fragmented skbs
3562 */
Jeff Kirsher997f5cb2006-01-12 16:50:55 -08003563
Jesse Brandeburg99261462010-01-22 22:56:16 +00003564 if (max_frame <= E1000_RXBUFFER_2048)
Auke Kok9e2feac2006-04-14 19:05:18 -07003565 adapter->rx_buffer_len = E1000_RXBUFFER_2048;
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00003566 else
3567#if (PAGE_SIZE >= E1000_RXBUFFER_16384)
Auke Kok9e2feac2006-04-14 19:05:18 -07003568 adapter->rx_buffer_len = E1000_RXBUFFER_16384;
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00003569#elif (PAGE_SIZE >= E1000_RXBUFFER_4096)
3570 adapter->rx_buffer_len = PAGE_SIZE;
3571#endif
Auke Kok9e2feac2006-04-14 19:05:18 -07003572
3573 /* adjust allocation if LPE protects us, and we aren't using SBP */
Joe Perches1dc32912008-07-11 15:17:08 -07003574 if (!hw->tbi_compatibility_on &&
Jesse Brandeburgb7cb8c22009-07-06 10:45:01 +00003575 ((max_frame == (ETH_FRAME_LEN + ETH_FCS_LEN)) ||
Auke Kok9e2feac2006-04-14 19:05:18 -07003576 (max_frame == MAXIMUM_ETHERNET_VLAN_SIZE)))
3577 adapter->rx_buffer_len = MAXIMUM_ETHERNET_VLAN_SIZE;
Jeff Kirsher997f5cb2006-01-12 16:50:55 -08003578
Emil Tantilov675ad472010-04-27 14:02:58 +00003579 pr_info("%s changing MTU from %d to %d\n",
3580 netdev->name, netdev->mtu, new_mtu);
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003581 netdev->mtu = new_mtu;
3582
Auke Kok2db10a02006-06-27 09:06:28 -07003583 if (netif_running(netdev))
Jesse Brandeburg3d6114e2009-09-25 12:19:02 +00003584 e1000_up(adapter);
3585 else
3586 e1000_reset(adapter);
3587
3588 clear_bit(__E1000_RESETTING, &adapter->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590 return 0;
3591}
3592
3593/**
3594 * e1000_update_stats - Update the board statistics counters
3595 * @adapter: board private structure
3596 **/
Joe Perches64798842008-07-11 15:17:02 -07003597void e1000_update_stats(struct e1000_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598{
Ajit Khaparde5fe31de2009-10-07 02:42:23 +00003599 struct net_device *netdev = adapter->netdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600 struct e1000_hw *hw = &adapter->hw;
Linas Vepstas282f33c2006-06-08 22:19:44 -07003601 struct pci_dev *pdev = adapter->pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602 unsigned long flags;
Joe Perches406874a2008-04-03 10:06:32 -07003603 u16 phy_tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604
3605#define PHY_IDLE_ERROR_COUNT_MASK 0x00FF
3606
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00003607 /* Prevent stats update while adapter is being reset, or if the pci
Linas Vepstas282f33c2006-06-08 22:19:44 -07003608 * connection is down.
3609 */
Auke Kok90267292006-06-08 09:30:24 -07003610 if (adapter->link_speed == 0)
3611 return;
Linas Vepstas81b19552006-12-12 18:29:15 -06003612 if (pci_channel_offline(pdev))
Linas Vepstas282f33c2006-06-08 22:19:44 -07003613 return;
Auke Kok90267292006-06-08 09:30:24 -07003614
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615 spin_lock_irqsave(&adapter->stats_lock, flags);
3616
Masatake YAMATO828d0552007-10-20 03:06:37 +02003617 /* these counters are modified from e1000_tbi_adjust_stats,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618 * called from the interrupt context, so they must only
3619 * be written while holding adapter->stats_lock
3620 */
3621
Joe Perches1dc32912008-07-11 15:17:08 -07003622 adapter->stats.crcerrs += er32(CRCERRS);
3623 adapter->stats.gprc += er32(GPRC);
3624 adapter->stats.gorcl += er32(GORCL);
3625 adapter->stats.gorch += er32(GORCH);
3626 adapter->stats.bprc += er32(BPRC);
3627 adapter->stats.mprc += er32(MPRC);
3628 adapter->stats.roc += er32(ROC);
Auke Kokcd94dd02006-06-27 09:08:22 -07003629
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00003630 adapter->stats.prc64 += er32(PRC64);
3631 adapter->stats.prc127 += er32(PRC127);
3632 adapter->stats.prc255 += er32(PRC255);
3633 adapter->stats.prc511 += er32(PRC511);
3634 adapter->stats.prc1023 += er32(PRC1023);
3635 adapter->stats.prc1522 += er32(PRC1522);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636
Joe Perches1dc32912008-07-11 15:17:08 -07003637 adapter->stats.symerrs += er32(SYMERRS);
3638 adapter->stats.mpc += er32(MPC);
3639 adapter->stats.scc += er32(SCC);
3640 adapter->stats.ecol += er32(ECOL);
3641 adapter->stats.mcc += er32(MCC);
3642 adapter->stats.latecol += er32(LATECOL);
3643 adapter->stats.dc += er32(DC);
3644 adapter->stats.sec += er32(SEC);
3645 adapter->stats.rlec += er32(RLEC);
3646 adapter->stats.xonrxc += er32(XONRXC);
3647 adapter->stats.xontxc += er32(XONTXC);
3648 adapter->stats.xoffrxc += er32(XOFFRXC);
3649 adapter->stats.xofftxc += er32(XOFFTXC);
3650 adapter->stats.fcruc += er32(FCRUC);
3651 adapter->stats.gptc += er32(GPTC);
3652 adapter->stats.gotcl += er32(GOTCL);
3653 adapter->stats.gotch += er32(GOTCH);
3654 adapter->stats.rnbc += er32(RNBC);
3655 adapter->stats.ruc += er32(RUC);
3656 adapter->stats.rfc += er32(RFC);
3657 adapter->stats.rjc += er32(RJC);
3658 adapter->stats.torl += er32(TORL);
3659 adapter->stats.torh += er32(TORH);
3660 adapter->stats.totl += er32(TOTL);
3661 adapter->stats.toth += er32(TOTH);
3662 adapter->stats.tpr += er32(TPR);
Auke Kokcd94dd02006-06-27 09:08:22 -07003663
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00003664 adapter->stats.ptc64 += er32(PTC64);
3665 adapter->stats.ptc127 += er32(PTC127);
3666 adapter->stats.ptc255 += er32(PTC255);
3667 adapter->stats.ptc511 += er32(PTC511);
3668 adapter->stats.ptc1023 += er32(PTC1023);
3669 adapter->stats.ptc1522 += er32(PTC1522);
Auke Kokcd94dd02006-06-27 09:08:22 -07003670
Joe Perches1dc32912008-07-11 15:17:08 -07003671 adapter->stats.mptc += er32(MPTC);
3672 adapter->stats.bptc += er32(BPTC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673
3674 /* used for adaptive IFS */
3675
Joe Perches1dc32912008-07-11 15:17:08 -07003676 hw->tx_packet_delta = er32(TPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677 adapter->stats.tpt += hw->tx_packet_delta;
Joe Perches1dc32912008-07-11 15:17:08 -07003678 hw->collision_delta = er32(COLC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679 adapter->stats.colc += hw->collision_delta;
3680
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003681 if (hw->mac_type >= e1000_82543) {
Joe Perches1dc32912008-07-11 15:17:08 -07003682 adapter->stats.algnerrc += er32(ALGNERRC);
3683 adapter->stats.rxerrc += er32(RXERRC);
3684 adapter->stats.tncrs += er32(TNCRS);
3685 adapter->stats.cexterr += er32(CEXTERR);
3686 adapter->stats.tsctc += er32(TSCTC);
3687 adapter->stats.tsctfc += er32(TSCTFC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688 }
3689
3690 /* Fill out the OS statistics structure */
Ajit Khaparde5fe31de2009-10-07 02:42:23 +00003691 netdev->stats.multicast = adapter->stats.mprc;
3692 netdev->stats.collisions = adapter->stats.colc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693
3694 /* Rx Errors */
3695
Jeff Kirsher87041632006-03-02 18:21:24 -08003696 /* RLEC on some newer hardware can be incorrect so build
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00003697 * our own version based on RUC and ROC
3698 */
Ajit Khaparde5fe31de2009-10-07 02:42:23 +00003699 netdev->stats.rx_errors = adapter->stats.rxerrc +
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700 adapter->stats.crcerrs + adapter->stats.algnerrc +
Jeff Kirsher87041632006-03-02 18:21:24 -08003701 adapter->stats.ruc + adapter->stats.roc +
3702 adapter->stats.cexterr;
Mitch Williams49559852006-09-27 12:53:37 -07003703 adapter->stats.rlerrc = adapter->stats.ruc + adapter->stats.roc;
Ajit Khaparde5fe31de2009-10-07 02:42:23 +00003704 netdev->stats.rx_length_errors = adapter->stats.rlerrc;
3705 netdev->stats.rx_crc_errors = adapter->stats.crcerrs;
3706 netdev->stats.rx_frame_errors = adapter->stats.algnerrc;
3707 netdev->stats.rx_missed_errors = adapter->stats.mpc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708
3709 /* Tx Errors */
Mitch Williams49559852006-09-27 12:53:37 -07003710 adapter->stats.txerrc = adapter->stats.ecol + adapter->stats.latecol;
Ajit Khaparde5fe31de2009-10-07 02:42:23 +00003711 netdev->stats.tx_errors = adapter->stats.txerrc;
3712 netdev->stats.tx_aborted_errors = adapter->stats.ecol;
3713 netdev->stats.tx_window_errors = adapter->stats.latecol;
3714 netdev->stats.tx_carrier_errors = adapter->stats.tncrs;
Joe Perches1dc32912008-07-11 15:17:08 -07003715 if (hw->bad_tx_carr_stats_fd &&
Jeff Garzik167fb282006-12-15 10:41:15 -05003716 adapter->link_duplex == FULL_DUPLEX) {
Ajit Khaparde5fe31de2009-10-07 02:42:23 +00003717 netdev->stats.tx_carrier_errors = 0;
Jeff Garzik167fb282006-12-15 10:41:15 -05003718 adapter->stats.tncrs = 0;
3719 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720
3721 /* Tx Dropped needs to be maintained elsewhere */
3722
3723 /* Phy Stats */
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003724 if (hw->media_type == e1000_media_type_copper) {
3725 if ((adapter->link_speed == SPEED_1000) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726 (!e1000_read_phy_reg(hw, PHY_1000T_STATUS, &phy_tmp))) {
3727 phy_tmp &= PHY_IDLE_ERROR_COUNT_MASK;
3728 adapter->phy_stats.idle_errors += phy_tmp;
3729 }
3730
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003731 if ((hw->mac_type <= e1000_82546) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732 (hw->phy_type == e1000_phy_m88) &&
3733 !e1000_read_phy_reg(hw, M88E1000_RX_ERR_CNTR, &phy_tmp))
3734 adapter->phy_stats.receive_errors += phy_tmp;
3735 }
3736
Jeff Garzik15e376b2006-12-15 11:16:33 -05003737 /* Management Stats */
Joe Perches1dc32912008-07-11 15:17:08 -07003738 if (hw->has_smbus) {
3739 adapter->stats.mgptc += er32(MGTPTC);
3740 adapter->stats.mgprc += er32(MGTPRC);
3741 adapter->stats.mgpdc += er32(MGTPDC);
Jeff Garzik15e376b2006-12-15 11:16:33 -05003742 }
3743
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744 spin_unlock_irqrestore(&adapter->stats_lock, flags);
3745}
Jesse Brandeburg9ac98282006-11-01 08:48:10 -08003746
3747/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748 * e1000_intr - Interrupt Handler
3749 * @irq: interrupt number
3750 * @data: pointer to a network interface device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751 **/
Joe Perches64798842008-07-11 15:17:02 -07003752static irqreturn_t e1000_intr(int irq, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753{
3754 struct net_device *netdev = data;
Malli Chilakala60490fe2005-06-17 17:41:45 -07003755 struct e1000_adapter *adapter = netdev_priv(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756 struct e1000_hw *hw = &adapter->hw;
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00003757 u32 icr = er32(ICR);
Francois Romieuc3570ac2008-07-11 15:17:38 -07003758
Jesse Brandeburg4c11b8a2011-01-13 07:48:13 +00003759 if (unlikely((!icr)))
Jesse Brandeburg835bb122006-11-01 08:48:13 -08003760 return IRQ_NONE; /* Not our interrupt */
3761
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00003762 /* we might have caused the interrupt, but the above
Jesse Brandeburg4c11b8a2011-01-13 07:48:13 +00003763 * read cleared it, and just in case the driver is
3764 * down there is nothing to do so return handled
3765 */
3766 if (unlikely(test_bit(__E1000_DOWN, &adapter->flags)))
3767 return IRQ_HANDLED;
3768
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003769 if (unlikely(icr & (E1000_ICR_RXSEQ | E1000_ICR_LSC))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770 hw->get_link_status = 1;
Auke Kok1314bbf2006-09-27 12:54:02 -07003771 /* guard against interrupt when we're going down */
3772 if (!test_bit(__E1000_DOWN, &adapter->flags))
Jesse Brandeburga4010af2011-10-05 07:24:41 +00003773 schedule_delayed_work(&adapter->watchdog_task, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774 }
3775
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00003776 /* disable interrupts, without the synchronize_irq bit */
3777 ew32(IMC, ~0);
3778 E1000_WRITE_FLUSH();
3779
Ben Hutchings288379f2009-01-19 16:43:59 -08003780 if (likely(napi_schedule_prep(&adapter->napi))) {
Jesse Brandeburg835bb122006-11-01 08:48:13 -08003781 adapter->total_tx_bytes = 0;
3782 adapter->total_tx_packets = 0;
3783 adapter->total_rx_bytes = 0;
3784 adapter->total_rx_packets = 0;
Ben Hutchings288379f2009-01-19 16:43:59 -08003785 __napi_schedule(&adapter->napi);
Jesse Brandeburga6c42322009-03-25 21:59:22 +00003786 } else {
Auke Kok90fb5132006-11-01 08:47:30 -08003787 /* this really should not happen! if it does it is basically a
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00003788 * bug, but not a hard error, so enable ints and continue
3789 */
Jesse Brandeburga6c42322009-03-25 21:59:22 +00003790 if (!test_bit(__E1000_DOWN, &adapter->flags))
3791 e1000_irq_enable(adapter);
3792 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794 return IRQ_HANDLED;
3795}
3796
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797/**
3798 * e1000_clean - NAPI Rx polling callback
3799 * @adapter: board private structure
3800 **/
Joe Perches64798842008-07-11 15:17:02 -07003801static int e1000_clean(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802{
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00003803 struct e1000_adapter *adapter = container_of(napi, struct e1000_adapter,
3804 napi);
Jesse Brandeburg650b5a52009-09-25 12:19:23 +00003805 int tx_clean_complete = 0, work_done = 0;
Malli Chilakala26483452005-04-28 19:44:46 -07003806
Jesse Brandeburg650b5a52009-09-25 12:19:23 +00003807 tx_clean_complete = e1000_clean_tx_irq(adapter, &adapter->tx_ring[0]);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003808
Jesse Brandeburg650b5a52009-09-25 12:19:23 +00003809 adapter->clean_rx(adapter, &adapter->rx_ring[0], &work_done, budget);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003810
Jesse Brandeburg650b5a52009-09-25 12:19:23 +00003811 if (!tx_clean_complete)
David S. Millerd2c7ddd2008-01-15 22:43:24 -08003812 work_done = budget;
3813
David S. Miller53e52c72008-01-07 21:06:12 -08003814 /* If budget not fully consumed, exit the polling mode */
3815 if (work_done < budget) {
Jesse Brandeburg835bb122006-11-01 08:48:13 -08003816 if (likely(adapter->itr_setting & 3))
3817 e1000_set_itr(adapter);
Ben Hutchings288379f2009-01-19 16:43:59 -08003818 napi_complete(napi);
Jesse Brandeburga6c42322009-03-25 21:59:22 +00003819 if (!test_bit(__E1000_DOWN, &adapter->flags))
3820 e1000_irq_enable(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821 }
3822
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003823 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824}
3825
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826/**
3827 * e1000_clean_tx_irq - Reclaim resources after transmit completes
3828 * @adapter: board private structure
3829 **/
Joe Perches64798842008-07-11 15:17:02 -07003830static bool e1000_clean_tx_irq(struct e1000_adapter *adapter,
3831 struct e1000_tx_ring *tx_ring)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832{
Joe Perches1dc32912008-07-11 15:17:08 -07003833 struct e1000_hw *hw = &adapter->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834 struct net_device *netdev = adapter->netdev;
3835 struct e1000_tx_desc *tx_desc, *eop_desc;
3836 struct e1000_buffer *buffer_info;
3837 unsigned int i, eop;
Jeff Kirsher2a1af5d2006-03-02 18:20:43 -08003838 unsigned int count = 0;
Jesse Brandeburg835bb122006-11-01 08:48:13 -08003839 unsigned int total_tx_bytes=0, total_tx_packets=0;
Otto Estuardo Solares Cabrera2f66fd32012-08-24 08:04:58 +00003840 unsigned int bytes_compl = 0, pkts_compl = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841
3842 i = tx_ring->next_to_clean;
3843 eop = tx_ring->buffer_info[i].next_to_watch;
3844 eop_desc = E1000_TX_DESC(*tx_ring, eop);
3845
Alexander Duyckccfb3422009-03-25 21:59:04 +00003846 while ((eop_desc->upper.data & cpu_to_le32(E1000_TXD_STAT_DD)) &&
3847 (count < tx_ring->count)) {
Jesse Brandeburg843f4262009-04-16 16:59:47 +00003848 bool cleaned = false;
Jeff Kirsher2d0bb1c2010-08-08 16:02:31 +00003849 rmb(); /* read buffer_info after eop_desc */
Jesse Brandeburg843f4262009-04-16 16:59:47 +00003850 for ( ; !cleaned; count++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851 tx_desc = E1000_TX_DESC(*tx_ring, i);
3852 buffer_info = &tx_ring->buffer_info[i];
3853 cleaned = (i == eop);
3854
Jesse Brandeburg835bb122006-11-01 08:48:13 -08003855 if (cleaned) {
Dean Nelson31c15a22011-08-25 14:39:24 +00003856 total_tx_packets += buffer_info->segs;
3857 total_tx_bytes += buffer_info->bytecount;
Otto Estuardo Solares Cabrera2f66fd32012-08-24 08:04:58 +00003858 if (buffer_info->skb) {
3859 bytes_compl += buffer_info->skb->len;
3860 pkts_compl++;
3861 }
3862
Jesse Brandeburg835bb122006-11-01 08:48:13 -08003863 }
Jeff Kirsherfd803242005-12-13 00:06:22 -05003864 e1000_unmap_and_free_tx_resource(adapter, buffer_info);
Jesse Brandeburga9ebadd2006-11-01 08:47:53 -08003865 tx_desc->upper.data = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003867 if (unlikely(++i == tx_ring->count)) i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868 }
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003869
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870 eop = tx_ring->buffer_info[i].next_to_watch;
3871 eop_desc = E1000_TX_DESC(*tx_ring, eop);
3872 }
3873
3874 tx_ring->next_to_clean = i;
3875
Otto Estuardo Solares Cabrera2f66fd32012-08-24 08:04:58 +00003876 netdev_completed_queue(netdev, pkts_compl, bytes_compl);
3877
Auke Kok77b2aad2006-04-14 19:05:25 -07003878#define TX_WAKE_THRESHOLD 32
Jesse Brandeburg843f4262009-04-16 16:59:47 +00003879 if (unlikely(count && netif_carrier_ok(netdev) &&
Jesse Brandeburg65c79732006-09-27 12:53:48 -07003880 E1000_DESC_UNUSED(tx_ring) >= TX_WAKE_THRESHOLD)) {
3881 /* Make sure that anybody stopping the queue after this
3882 * sees the new next_to_clean.
3883 */
3884 smp_mb();
Jesse Brandeburgcdd75492009-09-25 12:18:07 +00003885
3886 if (netif_queue_stopped(netdev) &&
3887 !(test_bit(__E1000_DOWN, &adapter->flags))) {
Auke Kok77b2aad2006-04-14 19:05:25 -07003888 netif_wake_queue(netdev);
Jesse Brandeburgfcfb1222006-11-01 08:47:59 -08003889 ++adapter->restart_queue;
3890 }
Auke Kok77b2aad2006-04-14 19:05:25 -07003891 }
Malli Chilakala26483452005-04-28 19:44:46 -07003892
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003893 if (adapter->detect_tx_hung) {
Malli Chilakala26483452005-04-28 19:44:46 -07003894 /* Detect a transmit hang in hardware, this serializes the
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00003895 * check with the clearing of time_stamp and movement of i
3896 */
Joe Perchesc3033b02008-03-21 11:06:25 -07003897 adapter->detect_tx_hung = false;
Jesse Brandeburgcdd75492009-09-25 12:18:07 +00003898 if (tx_ring->buffer_info[eop].time_stamp &&
3899 time_after(jiffies, tx_ring->buffer_info[eop].time_stamp +
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00003900 (adapter->tx_timeout_factor * HZ)) &&
Joe Perches8e95a202009-12-03 07:58:21 +00003901 !(er32(STATUS) & E1000_STATUS_TXOFF)) {
Malli Chilakala70b8f1e2005-04-28 19:40:40 -07003902
3903 /* detected Tx unit hang */
Emil Tantilovfeb8f472010-07-26 23:37:21 -07003904 e_err(drv, "Detected Tx Unit Hang\n"
Emil Tantilov675ad472010-04-27 14:02:58 +00003905 " Tx Queue <%lu>\n"
3906 " TDH <%x>\n"
3907 " TDT <%x>\n"
3908 " next_to_use <%x>\n"
3909 " next_to_clean <%x>\n"
3910 "buffer_info[next_to_clean]\n"
3911 " time_stamp <%lx>\n"
3912 " next_to_watch <%x>\n"
3913 " jiffies <%lx>\n"
3914 " next_to_watch.status <%x>\n",
Hong Zhiguo49a45a02013-10-22 18:32:56 +00003915 (unsigned long)(tx_ring - adapter->tx_ring),
Joe Perches1dc32912008-07-11 15:17:08 -07003916 readl(hw->hw_addr + tx_ring->tdh),
3917 readl(hw->hw_addr + tx_ring->tdt),
Malli Chilakala70b8f1e2005-04-28 19:40:40 -07003918 tx_ring->next_to_use,
Jeff Kirsher392137f2006-01-12 16:50:57 -08003919 tx_ring->next_to_clean,
Jesse Brandeburgcdd75492009-09-25 12:18:07 +00003920 tx_ring->buffer_info[eop].time_stamp,
Malli Chilakala70b8f1e2005-04-28 19:40:40 -07003921 eop,
3922 jiffies,
3923 eop_desc->upper.fields.status);
Tushar Daveb04e36b2012-01-27 09:00:46 +00003924 e1000_dump(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925 netif_stop_queue(netdev);
Malli Chilakala70b8f1e2005-04-28 19:40:40 -07003926 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927 }
Jesse Brandeburg835bb122006-11-01 08:48:13 -08003928 adapter->total_tx_bytes += total_tx_bytes;
3929 adapter->total_tx_packets += total_tx_packets;
Ajit Khaparde5fe31de2009-10-07 02:42:23 +00003930 netdev->stats.tx_bytes += total_tx_bytes;
3931 netdev->stats.tx_packets += total_tx_packets;
Eric Dumazet807540b2010-09-23 05:40:09 +00003932 return count < tx_ring->count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933}
3934
3935/**
3936 * e1000_rx_checksum - Receive Checksum Offload for 82543
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003937 * @adapter: board private structure
3938 * @status_err: receive descriptor status and error fields
3939 * @csum: receive descriptor csum field
3940 * @sk_buff: socket buffer with received data
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941 **/
Joe Perches64798842008-07-11 15:17:02 -07003942static void e1000_rx_checksum(struct e1000_adapter *adapter, u32 status_err,
3943 u32 csum, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944{
Joe Perches1dc32912008-07-11 15:17:08 -07003945 struct e1000_hw *hw = &adapter->hw;
Joe Perches406874a2008-04-03 10:06:32 -07003946 u16 status = (u16)status_err;
3947 u8 errors = (u8)(status_err >> 24);
Eric Dumazetbc8acf22010-09-02 13:07:41 -07003948
3949 skb_checksum_none_assert(skb);
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003950
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 /* 82543 or newer only */
Joe Perches1dc32912008-07-11 15:17:08 -07003952 if (unlikely(hw->mac_type < e1000_82543)) return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953 /* Ignore Checksum bit is set */
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003954 if (unlikely(status & E1000_RXD_STAT_IXSM)) return;
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003955 /* TCP/UDP checksum error bit is set */
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003956 if (unlikely(errors & E1000_RXD_ERR_TCPE)) {
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003957 /* let the stack verify checksum errors */
3958 adapter->hw_csum_err++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959 return;
3960 }
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003961 /* TCP/UDP Checksum has not been calculated */
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00003962 if (!(status & E1000_RXD_STAT_TCPCS))
3963 return;
3964
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003965 /* It must be a TCP or UDP packet with a valid checksum */
3966 if (likely(status & E1000_RXD_STAT_TCPCS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967 /* TCP checksum is good */
3968 skb->ip_summed = CHECKSUM_UNNECESSARY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 }
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003970 adapter->hw_csum_good++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971}
3972
3973/**
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00003974 * e1000_consume_page - helper function
3975 **/
3976static void e1000_consume_page(struct e1000_buffer *bi, struct sk_buff *skb,
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00003977 u16 length)
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00003978{
3979 bi->page = NULL;
3980 skb->len += length;
3981 skb->data_len += length;
Eric Dumazeted64b3c2011-10-13 07:53:42 +00003982 skb->truesize += PAGE_SIZE;
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00003983}
3984
3985/**
3986 * e1000_receive_skb - helper function to handle rx indications
3987 * @adapter: board private structure
3988 * @status: descriptor status field as written by hardware
3989 * @vlan: descriptor vlan field as written by hardware (no le/be conversion)
3990 * @skb: pointer to sk_buff to be indicated to stack
3991 */
3992static void e1000_receive_skb(struct e1000_adapter *adapter, u8 status,
3993 __le16 vlan, struct sk_buff *skb)
3994{
Jesse Brandeburg6a08d192010-09-22 18:23:05 +00003995 skb->protocol = eth_type_trans(skb, adapter->netdev);
3996
Jiri Pirko5622e402011-07-21 03:26:31 +00003997 if (status & E1000_RXD_STAT_VP) {
3998 u16 vid = le16_to_cpu(vlan) & E1000_RXD_SPC_VLAN_MASK;
3999
Patrick McHardy86a9bad2013-04-19 02:04:30 +00004000 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vid);
Jiri Pirko5622e402011-07-21 03:26:31 +00004001 }
4002 napi_gro_receive(&adapter->napi, skb);
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004003}
4004
4005/**
4006 * e1000_clean_jumbo_rx_irq - Send received data up the network stack; legacy
4007 * @adapter: board private structure
4008 * @rx_ring: ring to clean
4009 * @work_done: amount of napi work completed this call
4010 * @work_to_do: max amount of work allowed for this call to do
4011 *
4012 * the return value indicates whether actual cleaning was done, there
4013 * is no guarantee that everything was cleaned
4014 */
4015static bool e1000_clean_jumbo_rx_irq(struct e1000_adapter *adapter,
4016 struct e1000_rx_ring *rx_ring,
4017 int *work_done, int work_to_do)
4018{
4019 struct e1000_hw *hw = &adapter->hw;
4020 struct net_device *netdev = adapter->netdev;
4021 struct pci_dev *pdev = adapter->pdev;
4022 struct e1000_rx_desc *rx_desc, *next_rxd;
4023 struct e1000_buffer *buffer_info, *next_buffer;
4024 unsigned long irq_flags;
4025 u32 length;
4026 unsigned int i;
4027 int cleaned_count = 0;
4028 bool cleaned = false;
4029 unsigned int total_rx_bytes=0, total_rx_packets=0;
4030
4031 i = rx_ring->next_to_clean;
4032 rx_desc = E1000_RX_DESC(*rx_ring, i);
4033 buffer_info = &rx_ring->buffer_info[i];
4034
4035 while (rx_desc->status & E1000_RXD_STAT_DD) {
4036 struct sk_buff *skb;
4037 u8 status;
4038
4039 if (*work_done >= work_to_do)
4040 break;
4041 (*work_done)++;
Jeff Kirsher2d0bb1c2010-08-08 16:02:31 +00004042 rmb(); /* read descriptor and rx_buffer_info after status DD */
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004043
4044 status = rx_desc->status;
4045 skb = buffer_info->skb;
4046 buffer_info->skb = NULL;
4047
4048 if (++i == rx_ring->count) i = 0;
4049 next_rxd = E1000_RX_DESC(*rx_ring, i);
4050 prefetch(next_rxd);
4051
4052 next_buffer = &rx_ring->buffer_info[i];
4053
4054 cleaned = true;
4055 cleaned_count++;
Nick Nunleyb16f53b2010-04-27 13:08:45 +00004056 dma_unmap_page(&pdev->dev, buffer_info->dma,
4057 buffer_info->length, DMA_FROM_DEVICE);
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004058 buffer_info->dma = 0;
4059
4060 length = le16_to_cpu(rx_desc->length);
4061
4062 /* errors is only valid for DD + EOP descriptors */
4063 if (unlikely((status & E1000_RXD_STAT_EOP) &&
4064 (rx_desc->errors & E1000_RXD_ERR_FRAME_ERR_MASK))) {
Sebastian Andrzej Siewiora3060852012-05-11 16:30:46 +00004065 u8 *mapped;
4066 u8 last_byte;
4067
4068 mapped = page_address(buffer_info->page);
4069 last_byte = *(mapped + length - 1);
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004070 if (TBI_ACCEPT(hw, status, rx_desc->errors, length,
4071 last_byte)) {
4072 spin_lock_irqsave(&adapter->stats_lock,
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00004073 irq_flags);
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004074 e1000_tbi_adjust_stats(hw, &adapter->stats,
Sebastian Andrzej Siewior281a8f22012-05-15 09:18:55 +00004075 length, mapped);
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004076 spin_unlock_irqrestore(&adapter->stats_lock,
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00004077 irq_flags);
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004078 length--;
4079 } else {
Ben Greeare825b732012-04-04 06:01:29 +00004080 if (netdev->features & NETIF_F_RXALL)
4081 goto process_skb;
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004082 /* recycle both page and skb */
4083 buffer_info->skb = skb;
4084 /* an error means any chain goes out the window
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00004085 * too
4086 */
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004087 if (rx_ring->rx_skb_top)
4088 dev_kfree_skb(rx_ring->rx_skb_top);
4089 rx_ring->rx_skb_top = NULL;
4090 goto next_desc;
4091 }
4092 }
4093
4094#define rxtop rx_ring->rx_skb_top
Ben Greeare825b732012-04-04 06:01:29 +00004095process_skb:
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004096 if (!(status & E1000_RXD_STAT_EOP)) {
4097 /* this descriptor is only the beginning (or middle) */
4098 if (!rxtop) {
4099 /* this is the beginning of a chain */
4100 rxtop = skb;
4101 skb_fill_page_desc(rxtop, 0, buffer_info->page,
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00004102 0, length);
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004103 } else {
4104 /* this is the middle of a chain */
4105 skb_fill_page_desc(rxtop,
4106 skb_shinfo(rxtop)->nr_frags,
4107 buffer_info->page, 0, length);
4108 /* re-use the skb, only consumed the page */
4109 buffer_info->skb = skb;
4110 }
4111 e1000_consume_page(buffer_info, rxtop, length);
4112 goto next_desc;
4113 } else {
4114 if (rxtop) {
4115 /* end of the chain */
4116 skb_fill_page_desc(rxtop,
4117 skb_shinfo(rxtop)->nr_frags,
4118 buffer_info->page, 0, length);
4119 /* re-use the current skb, we only consumed the
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00004120 * page
4121 */
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004122 buffer_info->skb = skb;
4123 skb = rxtop;
4124 rxtop = NULL;
4125 e1000_consume_page(buffer_info, skb, length);
4126 } else {
4127 /* no chain, got EOP, this buf is the packet
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00004128 * copybreak to save the put_page/alloc_page
4129 */
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004130 if (length <= copybreak &&
4131 skb_tailroom(skb) >= length) {
4132 u8 *vaddr;
Cong Wang46790262011-11-25 23:14:23 +08004133 vaddr = kmap_atomic(buffer_info->page);
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00004134 memcpy(skb_tail_pointer(skb), vaddr,
4135 length);
Cong Wang46790262011-11-25 23:14:23 +08004136 kunmap_atomic(vaddr);
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004137 /* re-use the page, so don't erase
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00004138 * buffer_info->page
4139 */
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004140 skb_put(skb, length);
4141 } else {
4142 skb_fill_page_desc(skb, 0,
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00004143 buffer_info->page, 0,
4144 length);
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004145 e1000_consume_page(buffer_info, skb,
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00004146 length);
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004147 }
4148 }
4149 }
4150
4151 /* Receive Checksum Offload XXX recompute due to CRC strip? */
4152 e1000_rx_checksum(adapter,
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00004153 (u32)(status) |
4154 ((u32)(rx_desc->errors) << 24),
4155 le16_to_cpu(rx_desc->csum), skb);
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004156
Ben Greearb0d15622012-02-11 15:40:11 +00004157 total_rx_bytes += (skb->len - 4); /* don't count FCS */
4158 if (likely(!(netdev->features & NETIF_F_RXFCS)))
4159 pskb_trim(skb, skb->len - 4);
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004160 total_rx_packets++;
4161
4162 /* eth type trans needs skb->data to point to something */
4163 if (!pskb_may_pull(skb, ETH_HLEN)) {
Emil Tantilovfeb8f472010-07-26 23:37:21 -07004164 e_err(drv, "pskb_may_pull failed.\n");
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004165 dev_kfree_skb(skb);
4166 goto next_desc;
4167 }
4168
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004169 e1000_receive_skb(adapter, status, rx_desc->special, skb);
4170
4171next_desc:
4172 rx_desc->status = 0;
4173
4174 /* return some buffers to hardware, one at a time is too slow */
4175 if (unlikely(cleaned_count >= E1000_RX_BUFFER_WRITE)) {
4176 adapter->alloc_rx_buf(adapter, rx_ring, cleaned_count);
4177 cleaned_count = 0;
4178 }
4179
4180 /* use prefetched values */
4181 rx_desc = next_rxd;
4182 buffer_info = next_buffer;
4183 }
4184 rx_ring->next_to_clean = i;
4185
4186 cleaned_count = E1000_DESC_UNUSED(rx_ring);
4187 if (cleaned_count)
4188 adapter->alloc_rx_buf(adapter, rx_ring, cleaned_count);
4189
4190 adapter->total_rx_packets += total_rx_packets;
4191 adapter->total_rx_bytes += total_rx_bytes;
Ajit Khaparde5fe31de2009-10-07 02:42:23 +00004192 netdev->stats.rx_bytes += total_rx_bytes;
4193 netdev->stats.rx_packets += total_rx_packets;
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004194 return cleaned;
4195}
4196
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00004197/* this should improve performance for small packets with large amounts
Joe Perches57bf6ee2010-05-13 15:26:17 +00004198 * of reassembly being done in the stack
4199 */
4200static void e1000_check_copybreak(struct net_device *netdev,
4201 struct e1000_buffer *buffer_info,
4202 u32 length, struct sk_buff **skb)
4203{
4204 struct sk_buff *new_skb;
4205
4206 if (length > copybreak)
4207 return;
4208
4209 new_skb = netdev_alloc_skb_ip_align(netdev, length);
4210 if (!new_skb)
4211 return;
4212
4213 skb_copy_to_linear_data_offset(new_skb, -NET_IP_ALIGN,
4214 (*skb)->data - NET_IP_ALIGN,
4215 length + NET_IP_ALIGN);
4216 /* save the skb in buffer_info as good */
4217 buffer_info->skb = *skb;
4218 *skb = new_skb;
4219}
4220
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004221/**
Malli Chilakala2d7edb92005-04-28 19:43:52 -07004222 * e1000_clean_rx_irq - Send received data up the network stack; legacy
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223 * @adapter: board private structure
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004224 * @rx_ring: ring to clean
4225 * @work_done: amount of napi work completed this call
4226 * @work_to_do: max amount of work allowed for this call to do
4227 */
Joe Perches64798842008-07-11 15:17:02 -07004228static bool e1000_clean_rx_irq(struct e1000_adapter *adapter,
4229 struct e1000_rx_ring *rx_ring,
4230 int *work_done, int work_to_do)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231{
Joe Perches1dc32912008-07-11 15:17:08 -07004232 struct e1000_hw *hw = &adapter->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233 struct net_device *netdev = adapter->netdev;
4234 struct pci_dev *pdev = adapter->pdev;
Jesse Brandeburg86c3d592006-01-18 13:01:43 -08004235 struct e1000_rx_desc *rx_desc, *next_rxd;
4236 struct e1000_buffer *buffer_info, *next_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237 unsigned long flags;
Joe Perches406874a2008-04-03 10:06:32 -07004238 u32 length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239 unsigned int i;
Jeff Kirsher72d64a42006-01-12 16:51:19 -08004240 int cleaned_count = 0;
Joe Perchesc3033b02008-03-21 11:06:25 -07004241 bool cleaned = false;
Jesse Brandeburg835bb122006-11-01 08:48:13 -08004242 unsigned int total_rx_bytes=0, total_rx_packets=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243
4244 i = rx_ring->next_to_clean;
4245 rx_desc = E1000_RX_DESC(*rx_ring, i);
Jesse Brandeburgb92ff8e2006-01-18 13:01:32 -08004246 buffer_info = &rx_ring->buffer_info[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247
Jesse Brandeburgb92ff8e2006-01-18 13:01:32 -08004248 while (rx_desc->status & E1000_RXD_STAT_DD) {
Auke Kok24f476e2006-06-08 09:28:47 -07004249 struct sk_buff *skb;
Jeff Kirshera292ca62006-01-12 16:51:30 -08004250 u8 status;
Auke Kok90fb5132006-11-01 08:47:30 -08004251
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004252 if (*work_done >= work_to_do)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253 break;
4254 (*work_done)++;
Jeff Kirsher2d0bb1c2010-08-08 16:02:31 +00004255 rmb(); /* read descriptor and rx_buffer_info after status DD */
Francois Romieuc3570ac2008-07-11 15:17:38 -07004256
Jeff Kirshera292ca62006-01-12 16:51:30 -08004257 status = rx_desc->status;
Jesse Brandeburgb92ff8e2006-01-18 13:01:32 -08004258 skb = buffer_info->skb;
Jesse Brandeburg86c3d592006-01-18 13:01:43 -08004259 buffer_info->skb = NULL;
4260
Jeff Kirsher30320be2006-03-02 18:21:57 -08004261 prefetch(skb->data - NET_IP_ALIGN);
4262
Jesse Brandeburg86c3d592006-01-18 13:01:43 -08004263 if (++i == rx_ring->count) i = 0;
4264 next_rxd = E1000_RX_DESC(*rx_ring, i);
Jeff Kirsher30320be2006-03-02 18:21:57 -08004265 prefetch(next_rxd);
4266
Jesse Brandeburg86c3d592006-01-18 13:01:43 -08004267 next_buffer = &rx_ring->buffer_info[i];
Jesse Brandeburg86c3d592006-01-18 13:01:43 -08004268
Joe Perchesc3033b02008-03-21 11:06:25 -07004269 cleaned = true;
Jeff Kirsher72d64a42006-01-12 16:51:19 -08004270 cleaned_count++;
Nick Nunleyb16f53b2010-04-27 13:08:45 +00004271 dma_unmap_single(&pdev->dev, buffer_info->dma,
4272 buffer_info->length, DMA_FROM_DEVICE);
Jesse Brandeburg679be3b2009-06-30 12:45:34 +00004273 buffer_info->dma = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275 length = le16_to_cpu(rx_desc->length);
Neil Hormanea30e112009-06-02 01:29:58 -07004276 /* !EOP means multiple descriptors were used to store a single
Jesse Brandeburg40a14de2010-01-19 14:15:38 +00004277 * packet, if thats the case we need to toss it. In fact, we
4278 * to toss every packet with the EOP bit clear and the next
4279 * frame that _does_ have the EOP bit set, as it is by
4280 * definition only a frame fragment
4281 */
4282 if (unlikely(!(status & E1000_RXD_STAT_EOP)))
4283 adapter->discarding = true;
4284
4285 if (adapter->discarding) {
Jeff Kirshera1415ee2006-02-28 20:24:07 -08004286 /* All receives must fit into a single buffer */
Emil Tantilovfeb8f472010-07-26 23:37:21 -07004287 e_dbg("Receive packet consumed multiple buffers\n");
Auke Kok864c4e42006-06-27 09:06:53 -07004288 /* recycle */
Auke Kok8fc897b2006-08-28 14:56:16 -07004289 buffer_info->skb = skb;
Jesse Brandeburg40a14de2010-01-19 14:15:38 +00004290 if (status & E1000_RXD_STAT_EOP)
4291 adapter->discarding = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292 goto next_desc;
4293 }
4294
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004295 if (unlikely(rx_desc->errors & E1000_RXD_ERR_FRAME_ERR_MASK)) {
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004296 u8 last_byte = *(skb->data + length - 1);
Joe Perches1dc32912008-07-11 15:17:08 -07004297 if (TBI_ACCEPT(hw, status, rx_desc->errors, length,
4298 last_byte)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299 spin_lock_irqsave(&adapter->stats_lock, flags);
Joe Perches1dc32912008-07-11 15:17:08 -07004300 e1000_tbi_adjust_stats(hw, &adapter->stats,
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00004301 length, skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302 spin_unlock_irqrestore(&adapter->stats_lock,
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00004303 flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304 length--;
4305 } else {
Ben Greeare825b732012-04-04 06:01:29 +00004306 if (netdev->features & NETIF_F_RXALL)
4307 goto process_skb;
Auke Kok9e2feac2006-04-14 19:05:18 -07004308 /* recycle */
4309 buffer_info->skb = skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310 goto next_desc;
4311 }
Auke Kok1cb58212006-04-18 12:31:04 -07004312 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313
Ben Greeare825b732012-04-04 06:01:29 +00004314process_skb:
Ben Greearb0d15622012-02-11 15:40:11 +00004315 total_rx_bytes += (length - 4); /* don't count FCS */
Jesse Brandeburg835bb122006-11-01 08:48:13 -08004316 total_rx_packets++;
4317
Ben Greearb0d15622012-02-11 15:40:11 +00004318 if (likely(!(netdev->features & NETIF_F_RXFCS)))
4319 /* adjust length to remove Ethernet CRC, this must be
4320 * done after the TBI_ACCEPT workaround above
4321 */
4322 length -= 4;
4323
Joe Perches57bf6ee2010-05-13 15:26:17 +00004324 e1000_check_copybreak(netdev, buffer_info, length, &skb);
4325
Auke Kok996695d2006-11-01 08:47:50 -08004326 skb_put(skb, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327
4328 /* Receive Checksum Offload */
Jeff Kirshera292ca62006-01-12 16:51:30 -08004329 e1000_rx_checksum(adapter,
Joe Perches406874a2008-04-03 10:06:32 -07004330 (u32)(status) |
4331 ((u32)(rx_desc->errors) << 24),
David S. Millerc3d7a3a2006-03-15 14:26:28 -08004332 le16_to_cpu(rx_desc->csum), skb);
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004333
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004334 e1000_receive_skb(adapter, status, rx_desc->special, skb);
Francois Romieuc3570ac2008-07-11 15:17:38 -07004335
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336next_desc:
4337 rx_desc->status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338
Jeff Kirsher72d64a42006-01-12 16:51:19 -08004339 /* return some buffers to hardware, one at a time is too slow */
4340 if (unlikely(cleaned_count >= E1000_RX_BUFFER_WRITE)) {
4341 adapter->alloc_rx_buf(adapter, rx_ring, cleaned_count);
4342 cleaned_count = 0;
4343 }
4344
Jeff Kirsher30320be2006-03-02 18:21:57 -08004345 /* use prefetched values */
Jesse Brandeburg86c3d592006-01-18 13:01:43 -08004346 rx_desc = next_rxd;
4347 buffer_info = next_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349 rx_ring->next_to_clean = i;
Jeff Kirsher72d64a42006-01-12 16:51:19 -08004350
4351 cleaned_count = E1000_DESC_UNUSED(rx_ring);
4352 if (cleaned_count)
4353 adapter->alloc_rx_buf(adapter, rx_ring, cleaned_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354
Jesse Brandeburg835bb122006-11-01 08:48:13 -08004355 adapter->total_rx_packets += total_rx_packets;
4356 adapter->total_rx_bytes += total_rx_bytes;
Ajit Khaparde5fe31de2009-10-07 02:42:23 +00004357 netdev->stats.rx_bytes += total_rx_bytes;
4358 netdev->stats.rx_packets += total_rx_packets;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359 return cleaned;
4360}
4361
4362/**
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004363 * e1000_alloc_jumbo_rx_buffers - Replace used jumbo receive buffers
4364 * @adapter: address of board private structure
4365 * @rx_ring: pointer to receive ring structure
4366 * @cleaned_count: number of buffers to allocate this pass
4367 **/
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004368static void
4369e1000_alloc_jumbo_rx_buffers(struct e1000_adapter *adapter,
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00004370 struct e1000_rx_ring *rx_ring, int cleaned_count)
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004371{
4372 struct net_device *netdev = adapter->netdev;
4373 struct pci_dev *pdev = adapter->pdev;
4374 struct e1000_rx_desc *rx_desc;
4375 struct e1000_buffer *buffer_info;
4376 struct sk_buff *skb;
4377 unsigned int i;
Eric Dumazet89d71a62009-10-13 05:34:20 +00004378 unsigned int bufsz = 256 - 16 /*for skb_reserve */ ;
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004379
4380 i = rx_ring->next_to_use;
4381 buffer_info = &rx_ring->buffer_info[i];
4382
4383 while (cleaned_count--) {
4384 skb = buffer_info->skb;
4385 if (skb) {
4386 skb_trim(skb, 0);
4387 goto check_page;
4388 }
4389
Eric Dumazet89d71a62009-10-13 05:34:20 +00004390 skb = netdev_alloc_skb_ip_align(netdev, bufsz);
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004391 if (unlikely(!skb)) {
4392 /* Better luck next round */
4393 adapter->alloc_rx_buff_failed++;
4394 break;
4395 }
4396
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004397 buffer_info->skb = skb;
4398 buffer_info->length = adapter->rx_buffer_len;
4399check_page:
4400 /* allocate a new page if necessary */
4401 if (!buffer_info->page) {
4402 buffer_info->page = alloc_page(GFP_ATOMIC);
4403 if (unlikely(!buffer_info->page)) {
4404 adapter->alloc_rx_buff_failed++;
4405 break;
4406 }
4407 }
4408
Anton Blanchardb5abb022010-02-19 17:54:53 +00004409 if (!buffer_info->dma) {
Nick Nunleyb16f53b2010-04-27 13:08:45 +00004410 buffer_info->dma = dma_map_page(&pdev->dev,
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00004411 buffer_info->page, 0,
Nick Nunleyb16f53b2010-04-27 13:08:45 +00004412 buffer_info->length,
4413 DMA_FROM_DEVICE);
4414 if (dma_mapping_error(&pdev->dev, buffer_info->dma)) {
Anton Blanchardb5abb022010-02-19 17:54:53 +00004415 put_page(buffer_info->page);
4416 dev_kfree_skb(skb);
4417 buffer_info->page = NULL;
4418 buffer_info->skb = NULL;
4419 buffer_info->dma = 0;
4420 adapter->alloc_rx_buff_failed++;
4421 break; /* while !buffer_info->skb */
4422 }
4423 }
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004424
4425 rx_desc = E1000_RX_DESC(*rx_ring, i);
4426 rx_desc->buffer_addr = cpu_to_le64(buffer_info->dma);
4427
4428 if (unlikely(++i == rx_ring->count))
4429 i = 0;
4430 buffer_info = &rx_ring->buffer_info[i];
4431 }
4432
4433 if (likely(rx_ring->next_to_use != i)) {
4434 rx_ring->next_to_use = i;
4435 if (unlikely(i-- == 0))
4436 i = (rx_ring->count - 1);
4437
4438 /* Force memory writes to complete before letting h/w
4439 * know there are new descriptors to fetch. (Only
4440 * applicable for weak-ordered memory model archs,
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00004441 * such as IA-64).
4442 */
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004443 wmb();
4444 writel(i, adapter->hw.hw_addr + rx_ring->rdt);
4445 }
4446}
4447
4448/**
Malli Chilakala2d7edb92005-04-28 19:43:52 -07004449 * e1000_alloc_rx_buffers - Replace used receive buffers; legacy & extended
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450 * @adapter: address of board private structure
4451 **/
Joe Perches64798842008-07-11 15:17:02 -07004452static void e1000_alloc_rx_buffers(struct e1000_adapter *adapter,
4453 struct e1000_rx_ring *rx_ring,
4454 int cleaned_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455{
Joe Perches1dc32912008-07-11 15:17:08 -07004456 struct e1000_hw *hw = &adapter->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457 struct net_device *netdev = adapter->netdev;
4458 struct pci_dev *pdev = adapter->pdev;
4459 struct e1000_rx_desc *rx_desc;
4460 struct e1000_buffer *buffer_info;
4461 struct sk_buff *skb;
Malli Chilakala26483452005-04-28 19:44:46 -07004462 unsigned int i;
Eric Dumazet89d71a62009-10-13 05:34:20 +00004463 unsigned int bufsz = adapter->rx_buffer_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464
4465 i = rx_ring->next_to_use;
4466 buffer_info = &rx_ring->buffer_info[i];
4467
Jeff Kirshera292ca62006-01-12 16:51:30 -08004468 while (cleaned_count--) {
Christoph Hellwigca6f7222006-08-31 14:27:47 -07004469 skb = buffer_info->skb;
4470 if (skb) {
Jeff Kirshera292ca62006-01-12 16:51:30 -08004471 skb_trim(skb, 0);
4472 goto map_skb;
4473 }
4474
Eric Dumazet89d71a62009-10-13 05:34:20 +00004475 skb = netdev_alloc_skb_ip_align(netdev, bufsz);
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004476 if (unlikely(!skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477 /* Better luck next round */
Jeff Kirsher72d64a42006-01-12 16:51:19 -08004478 adapter->alloc_rx_buff_failed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479 break;
4480 }
4481
Malli Chilakala26483452005-04-28 19:44:46 -07004482 /* Fix for errata 23, can't cross 64kB boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483 if (!e1000_check_64k_bound(adapter, skb->data, bufsz)) {
4484 struct sk_buff *oldskb = skb;
Emil Tantilovfeb8f472010-07-26 23:37:21 -07004485 e_err(rx_err, "skb align check failed: %u bytes at "
4486 "%p\n", bufsz, skb->data);
Malli Chilakala26483452005-04-28 19:44:46 -07004487 /* Try again, without freeing the previous */
Eric Dumazet89d71a62009-10-13 05:34:20 +00004488 skb = netdev_alloc_skb_ip_align(netdev, bufsz);
Malli Chilakala26483452005-04-28 19:44:46 -07004489 /* Failed allocation, critical failure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490 if (!skb) {
4491 dev_kfree_skb(oldskb);
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004492 adapter->alloc_rx_buff_failed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493 break;
4494 }
Malli Chilakala26483452005-04-28 19:44:46 -07004495
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496 if (!e1000_check_64k_bound(adapter, skb->data, bufsz)) {
4497 /* give up */
4498 dev_kfree_skb(skb);
4499 dev_kfree_skb(oldskb);
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004500 adapter->alloc_rx_buff_failed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501 break; /* while !buffer_info->skb */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502 }
Christoph Hellwigca6f7222006-08-31 14:27:47 -07004503
4504 /* Use new allocation */
4505 dev_kfree_skb(oldskb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507 buffer_info->skb = skb;
4508 buffer_info->length = adapter->rx_buffer_len;
Jeff Kirshera292ca62006-01-12 16:51:30 -08004509map_skb:
Nick Nunleyb16f53b2010-04-27 13:08:45 +00004510 buffer_info->dma = dma_map_single(&pdev->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511 skb->data,
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004512 buffer_info->length,
Nick Nunleyb16f53b2010-04-27 13:08:45 +00004513 DMA_FROM_DEVICE);
4514 if (dma_mapping_error(&pdev->dev, buffer_info->dma)) {
Anton Blanchardb5abb022010-02-19 17:54:53 +00004515 dev_kfree_skb(skb);
4516 buffer_info->skb = NULL;
4517 buffer_info->dma = 0;
4518 adapter->alloc_rx_buff_failed++;
4519 break; /* while !buffer_info->skb */
4520 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00004522 /* XXX if it was allocated cleanly it will never map to a
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004523 * boundary crossing
4524 */
4525
Malli Chilakala26483452005-04-28 19:44:46 -07004526 /* Fix for errata 23, can't cross 64kB boundary */
4527 if (!e1000_check_64k_bound(adapter,
4528 (void *)(unsigned long)buffer_info->dma,
4529 adapter->rx_buffer_len)) {
Emil Tantilovfeb8f472010-07-26 23:37:21 -07004530 e_err(rx_err, "dma align check failed: %u bytes at "
4531 "%p\n", adapter->rx_buffer_len,
Emil Tantilov675ad472010-04-27 14:02:58 +00004532 (void *)(unsigned long)buffer_info->dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533 dev_kfree_skb(skb);
4534 buffer_info->skb = NULL;
4535
Nick Nunleyb16f53b2010-04-27 13:08:45 +00004536 dma_unmap_single(&pdev->dev, buffer_info->dma,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537 adapter->rx_buffer_len,
Nick Nunleyb16f53b2010-04-27 13:08:45 +00004538 DMA_FROM_DEVICE);
Jesse Brandeburg679be3b2009-06-30 12:45:34 +00004539 buffer_info->dma = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004541 adapter->alloc_rx_buff_failed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542 break; /* while !buffer_info->skb */
4543 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544 rx_desc = E1000_RX_DESC(*rx_ring, i);
4545 rx_desc->buffer_addr = cpu_to_le64(buffer_info->dma);
4546
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004547 if (unlikely(++i == rx_ring->count))
4548 i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549 buffer_info = &rx_ring->buffer_info[i];
4550 }
4551
Jesse Brandeburgb92ff8e2006-01-18 13:01:32 -08004552 if (likely(rx_ring->next_to_use != i)) {
4553 rx_ring->next_to_use = i;
4554 if (unlikely(i-- == 0))
4555 i = (rx_ring->count - 1);
4556
4557 /* Force memory writes to complete before letting h/w
4558 * know there are new descriptors to fetch. (Only
4559 * applicable for weak-ordered memory model archs,
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00004560 * such as IA-64).
4561 */
Jesse Brandeburgb92ff8e2006-01-18 13:01:32 -08004562 wmb();
Joe Perches1dc32912008-07-11 15:17:08 -07004563 writel(i, hw->hw_addr + rx_ring->rdt);
Jesse Brandeburgb92ff8e2006-01-18 13:01:32 -08004564 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565}
4566
4567/**
4568 * e1000_smartspeed - Workaround for SmartSpeed on 82541 and 82547 controllers.
4569 * @adapter:
4570 **/
Joe Perches64798842008-07-11 15:17:02 -07004571static void e1000_smartspeed(struct e1000_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572{
Joe Perches1dc32912008-07-11 15:17:08 -07004573 struct e1000_hw *hw = &adapter->hw;
Joe Perches406874a2008-04-03 10:06:32 -07004574 u16 phy_status;
4575 u16 phy_ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576
Joe Perches1dc32912008-07-11 15:17:08 -07004577 if ((hw->phy_type != e1000_phy_igp) || !hw->autoneg ||
4578 !(hw->autoneg_advertised & ADVERTISE_1000_FULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579 return;
4580
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004581 if (adapter->smartspeed == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582 /* If Master/Slave config fault is asserted twice,
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00004583 * we assume back-to-back
4584 */
Joe Perches1dc32912008-07-11 15:17:08 -07004585 e1000_read_phy_reg(hw, PHY_1000T_STATUS, &phy_status);
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004586 if (!(phy_status & SR_1000T_MS_CONFIG_FAULT)) return;
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_CTRL, &phy_ctrl);
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004590 if (phy_ctrl & CR_1000T_MS_ENABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591 phy_ctrl &= ~CR_1000T_MS_ENABLE;
Joe Perches1dc32912008-07-11 15:17:08 -07004592 e1000_write_phy_reg(hw, PHY_1000T_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593 phy_ctrl);
4594 adapter->smartspeed++;
Joe Perches1dc32912008-07-11 15:17:08 -07004595 if (!e1000_phy_setup_autoneg(hw) &&
4596 !e1000_read_phy_reg(hw, PHY_CTRL,
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00004597 &phy_ctrl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598 phy_ctrl |= (MII_CR_AUTO_NEG_EN |
4599 MII_CR_RESTART_AUTO_NEG);
Joe Perches1dc32912008-07-11 15:17:08 -07004600 e1000_write_phy_reg(hw, PHY_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601 phy_ctrl);
4602 }
4603 }
4604 return;
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004605 } else if (adapter->smartspeed == E1000_SMARTSPEED_DOWNSHIFT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606 /* If still no link, perhaps using 2/3 pair cable */
Joe Perches1dc32912008-07-11 15:17:08 -07004607 e1000_read_phy_reg(hw, PHY_1000T_CTRL, &phy_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608 phy_ctrl |= CR_1000T_MS_ENABLE;
Joe Perches1dc32912008-07-11 15:17:08 -07004609 e1000_write_phy_reg(hw, PHY_1000T_CTRL, phy_ctrl);
4610 if (!e1000_phy_setup_autoneg(hw) &&
4611 !e1000_read_phy_reg(hw, PHY_CTRL, &phy_ctrl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612 phy_ctrl |= (MII_CR_AUTO_NEG_EN |
4613 MII_CR_RESTART_AUTO_NEG);
Joe Perches1dc32912008-07-11 15:17:08 -07004614 e1000_write_phy_reg(hw, PHY_CTRL, phy_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615 }
4616 }
4617 /* Restart process after E1000_SMARTSPEED_MAX iterations */
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004618 if (adapter->smartspeed++ == E1000_SMARTSPEED_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619 adapter->smartspeed = 0;
4620}
4621
4622/**
4623 * e1000_ioctl -
4624 * @netdev:
4625 * @ifreq:
4626 * @cmd:
4627 **/
Joe Perches64798842008-07-11 15:17:02 -07004628static int e1000_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629{
4630 switch (cmd) {
4631 case SIOCGMIIPHY:
4632 case SIOCGMIIREG:
4633 case SIOCSMIIREG:
4634 return e1000_mii_ioctl(netdev, ifr, cmd);
4635 default:
4636 return -EOPNOTSUPP;
4637 }
4638}
4639
4640/**
4641 * e1000_mii_ioctl -
4642 * @netdev:
4643 * @ifreq:
4644 * @cmd:
4645 **/
Joe Perches64798842008-07-11 15:17:02 -07004646static int e1000_mii_ioctl(struct net_device *netdev, struct ifreq *ifr,
4647 int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648{
Malli Chilakala60490fe2005-06-17 17:41:45 -07004649 struct e1000_adapter *adapter = netdev_priv(netdev);
Joe Perches1dc32912008-07-11 15:17:08 -07004650 struct e1000_hw *hw = &adapter->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651 struct mii_ioctl_data *data = if_mii(ifr);
4652 int retval;
Joe Perches406874a2008-04-03 10:06:32 -07004653 u16 mii_reg;
Malli Chilakala97876fc2005-06-17 17:40:19 -07004654 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655
Joe Perches1dc32912008-07-11 15:17:08 -07004656 if (hw->media_type != e1000_media_type_copper)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657 return -EOPNOTSUPP;
4658
4659 switch (cmd) {
4660 case SIOCGMIIPHY:
Joe Perches1dc32912008-07-11 15:17:08 -07004661 data->phy_id = hw->phy_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662 break;
4663 case SIOCGMIIREG:
Malli Chilakala97876fc2005-06-17 17:40:19 -07004664 spin_lock_irqsave(&adapter->stats_lock, flags);
Joe Perches1dc32912008-07-11 15:17:08 -07004665 if (e1000_read_phy_reg(hw, data->reg_num & 0x1F,
Malli Chilakala97876fc2005-06-17 17:40:19 -07004666 &data->val_out)) {
4667 spin_unlock_irqrestore(&adapter->stats_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668 return -EIO;
Malli Chilakala97876fc2005-06-17 17:40:19 -07004669 }
4670 spin_unlock_irqrestore(&adapter->stats_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671 break;
4672 case SIOCSMIIREG:
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004673 if (data->reg_num & ~(0x1F))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674 return -EFAULT;
4675 mii_reg = data->val_in;
Malli Chilakala97876fc2005-06-17 17:40:19 -07004676 spin_lock_irqsave(&adapter->stats_lock, flags);
Joe Perches1dc32912008-07-11 15:17:08 -07004677 if (e1000_write_phy_reg(hw, data->reg_num,
Malli Chilakala97876fc2005-06-17 17:40:19 -07004678 mii_reg)) {
4679 spin_unlock_irqrestore(&adapter->stats_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680 return -EIO;
Malli Chilakala97876fc2005-06-17 17:40:19 -07004681 }
Jesse Brandeburgf0163ac2007-11-13 21:00:09 -08004682 spin_unlock_irqrestore(&adapter->stats_lock, flags);
Joe Perches1dc32912008-07-11 15:17:08 -07004683 if (hw->media_type == e1000_media_type_copper) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684 switch (data->reg_num) {
4685 case PHY_CTRL:
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004686 if (mii_reg & MII_CR_POWER_DOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687 break;
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004688 if (mii_reg & MII_CR_AUTO_NEG_EN) {
Joe Perches1dc32912008-07-11 15:17:08 -07004689 hw->autoneg = 1;
4690 hw->autoneg_advertised = 0x2F;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691 } else {
David Decotigny14ad2512011-04-27 18:32:43 +00004692 u32 speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693 if (mii_reg & 0x40)
David Decotigny14ad2512011-04-27 18:32:43 +00004694 speed = SPEED_1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695 else if (mii_reg & 0x2000)
David Decotigny14ad2512011-04-27 18:32:43 +00004696 speed = SPEED_100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697 else
David Decotigny14ad2512011-04-27 18:32:43 +00004698 speed = SPEED_10;
4699 retval = e1000_set_spd_dplx(
4700 adapter, speed,
4701 ((mii_reg & 0x100)
4702 ? DUPLEX_FULL :
4703 DUPLEX_HALF));
Jesse Brandeburgf0163ac2007-11-13 21:00:09 -08004704 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705 return retval;
4706 }
Auke Kok2db10a02006-06-27 09:06:28 -07004707 if (netif_running(adapter->netdev))
4708 e1000_reinit_locked(adapter);
4709 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710 e1000_reset(adapter);
4711 break;
4712 case M88E1000_PHY_SPEC_CTRL:
4713 case M88E1000_EXT_PHY_SPEC_CTRL:
Joe Perches1dc32912008-07-11 15:17:08 -07004714 if (e1000_phy_reset(hw))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715 return -EIO;
4716 break;
4717 }
4718 } else {
4719 switch (data->reg_num) {
4720 case PHY_CTRL:
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004721 if (mii_reg & MII_CR_POWER_DOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722 break;
Auke Kok2db10a02006-06-27 09:06:28 -07004723 if (netif_running(adapter->netdev))
4724 e1000_reinit_locked(adapter);
4725 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004726 e1000_reset(adapter);
4727 break;
4728 }
4729 }
4730 break;
4731 default:
4732 return -EOPNOTSUPP;
4733 }
4734 return E1000_SUCCESS;
4735}
4736
Joe Perches64798842008-07-11 15:17:02 -07004737void e1000_pci_set_mwi(struct e1000_hw *hw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738{
4739 struct e1000_adapter *adapter = hw->back;
Malli Chilakala26483452005-04-28 19:44:46 -07004740 int ret_val = pci_set_mwi(adapter->pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004742 if (ret_val)
Emil Tantilovfeb8f472010-07-26 23:37:21 -07004743 e_err(probe, "Error in setting MWI\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744}
4745
Joe Perches64798842008-07-11 15:17:02 -07004746void e1000_pci_clear_mwi(struct e1000_hw *hw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004747{
4748 struct e1000_adapter *adapter = hw->back;
4749
4750 pci_clear_mwi(adapter->pdev);
4751}
4752
Joe Perches64798842008-07-11 15:17:02 -07004753int e1000_pcix_get_mmrbc(struct e1000_hw *hw)
Peter Oruba007755e2007-09-28 22:42:06 -07004754{
4755 struct e1000_adapter *adapter = hw->back;
4756 return pcix_get_mmrbc(adapter->pdev);
4757}
4758
Joe Perches64798842008-07-11 15:17:02 -07004759void e1000_pcix_set_mmrbc(struct e1000_hw *hw, int mmrbc)
Peter Oruba007755e2007-09-28 22:42:06 -07004760{
4761 struct e1000_adapter *adapter = hw->back;
4762 pcix_set_mmrbc(adapter->pdev, mmrbc);
4763}
4764
Joe Perches64798842008-07-11 15:17:02 -07004765void e1000_io_write(struct e1000_hw *hw, unsigned long port, u32 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766{
4767 outl(value, port);
4768}
4769
Jiri Pirko5622e402011-07-21 03:26:31 +00004770static bool e1000_vlan_used(struct e1000_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771{
Jiri Pirko5622e402011-07-21 03:26:31 +00004772 u16 vid;
4773
4774 for_each_set_bit(vid, adapter->active_vlans, VLAN_N_VID)
4775 return true;
4776 return false;
4777}
4778
Jiri Pirko52f55092012-03-20 18:10:01 +00004779static void __e1000_vlan_mode(struct e1000_adapter *adapter,
4780 netdev_features_t features)
4781{
4782 struct e1000_hw *hw = &adapter->hw;
4783 u32 ctrl;
4784
4785 ctrl = er32(CTRL);
Patrick McHardyf6469682013-04-19 02:04:27 +00004786 if (features & NETIF_F_HW_VLAN_CTAG_RX) {
Jiri Pirko52f55092012-03-20 18:10:01 +00004787 /* enable VLAN tag insert/strip */
4788 ctrl |= E1000_CTRL_VME;
4789 } else {
4790 /* disable VLAN tag insert/strip */
4791 ctrl &= ~E1000_CTRL_VME;
4792 }
4793 ew32(CTRL, ctrl);
4794}
Jiri Pirko5622e402011-07-21 03:26:31 +00004795static void e1000_vlan_filter_on_off(struct e1000_adapter *adapter,
4796 bool filter_on)
4797{
Joe Perches1dc32912008-07-11 15:17:08 -07004798 struct e1000_hw *hw = &adapter->hw;
Jiri Pirko5622e402011-07-21 03:26:31 +00004799 u32 rctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800
Jesse Brandeburg9150b762008-03-21 11:06:58 -07004801 if (!test_bit(__E1000_DOWN, &adapter->flags))
4802 e1000_irq_disable(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803
Jiri Pirko52f55092012-03-20 18:10:01 +00004804 __e1000_vlan_mode(adapter, adapter->netdev->features);
Jiri Pirko5622e402011-07-21 03:26:31 +00004805 if (filter_on) {
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00004806 /* enable VLAN receive filtering */
4807 rctl = er32(RCTL);
4808 rctl &= ~E1000_RCTL_CFIEN;
Jiri Pirko5622e402011-07-21 03:26:31 +00004809 if (!(adapter->netdev->flags & IFF_PROMISC))
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00004810 rctl |= E1000_RCTL_VFE;
4811 ew32(RCTL, rctl);
4812 e1000_update_mng_vlan(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813 } else {
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00004814 /* disable VLAN receive filtering */
4815 rctl = er32(RCTL);
4816 rctl &= ~E1000_RCTL_VFE;
4817 ew32(RCTL, rctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004818 }
4819
Jesse Brandeburg9150b762008-03-21 11:06:58 -07004820 if (!test_bit(__E1000_DOWN, &adapter->flags))
4821 e1000_irq_enable(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004822}
4823
Michał Mirosławc8f44af2011-11-15 15:29:55 +00004824static void e1000_vlan_mode(struct net_device *netdev,
Jiri Pirko52f55092012-03-20 18:10:01 +00004825 netdev_features_t features)
Jiri Pirko5622e402011-07-21 03:26:31 +00004826{
4827 struct e1000_adapter *adapter = netdev_priv(netdev);
Jiri Pirko5622e402011-07-21 03:26:31 +00004828
4829 if (!test_bit(__E1000_DOWN, &adapter->flags))
4830 e1000_irq_disable(adapter);
4831
Jiri Pirko52f55092012-03-20 18:10:01 +00004832 __e1000_vlan_mode(adapter, features);
Jiri Pirko5622e402011-07-21 03:26:31 +00004833
4834 if (!test_bit(__E1000_DOWN, &adapter->flags))
4835 e1000_irq_enable(adapter);
4836}
4837
Patrick McHardy80d5c362013-04-19 02:04:28 +00004838static int e1000_vlan_rx_add_vid(struct net_device *netdev,
4839 __be16 proto, u16 vid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840{
Malli Chilakala60490fe2005-06-17 17:41:45 -07004841 struct e1000_adapter *adapter = netdev_priv(netdev);
Joe Perches1dc32912008-07-11 15:17:08 -07004842 struct e1000_hw *hw = &adapter->hw;
Joe Perches406874a2008-04-03 10:06:32 -07004843 u32 vfta, index;
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004844
Joe Perches1dc32912008-07-11 15:17:08 -07004845 if ((hw->mng_cookie.status &
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004846 E1000_MNG_DHCP_COOKIE_STATUS_VLAN_SUPPORT) &&
4847 (vid == adapter->mng_vlan_id))
Jiri Pirko8e586132011-12-08 19:52:37 -05004848 return 0;
Jiri Pirko5622e402011-07-21 03:26:31 +00004849
4850 if (!e1000_vlan_used(adapter))
4851 e1000_vlan_filter_on_off(adapter, true);
4852
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853 /* add VID to filter table */
4854 index = (vid >> 5) & 0x7F;
Joe Perches1dc32912008-07-11 15:17:08 -07004855 vfta = E1000_READ_REG_ARRAY(hw, VFTA, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856 vfta |= (1 << (vid & 0x1F));
Joe Perches1dc32912008-07-11 15:17:08 -07004857 e1000_write_vfta(hw, index, vfta);
Jiri Pirko5622e402011-07-21 03:26:31 +00004858
4859 set_bit(vid, adapter->active_vlans);
Jiri Pirko8e586132011-12-08 19:52:37 -05004860
4861 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862}
4863
Patrick McHardy80d5c362013-04-19 02:04:28 +00004864static int e1000_vlan_rx_kill_vid(struct net_device *netdev,
4865 __be16 proto, 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)
Patrick McHardy80d5c362013-04-19 02:04:28 +00004899 e1000_vlan_rx_add_vid(adapter->netdev, htons(ETH_P_8021Q), 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 */