blob: c0f52174e4c8b909711e68047da150d64b8ad5b6 [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;
yzhu16a7d64e2013-11-23 07:07:40 +00001443 int count = E1000_CHECK_RESET_COUNT;
1444
1445 while (test_bit(__E1000_RESETTING, &adapter->flags) && count--)
1446 usleep_range(10000, 20000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
Auke Kok2db10a02006-06-27 09:06:28 -07001448 WARN_ON(test_bit(__E1000_RESETTING, &adapter->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 e1000_down(adapter);
Auke Kok79f05bf2006-06-27 09:06:32 -07001450 e1000_power_down_phy(adapter);
Auke Kok2db10a02006-06-27 09:06:28 -07001451 e1000_free_irq(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001453 e1000_free_all_tx_resources(adapter);
1454 e1000_free_all_rx_resources(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
Bruce Allan46665602006-09-27 12:54:08 -07001456 /* kill manageability vlan ID if supported, but not if a vlan with
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00001457 * the same ID is registered on the host OS (let 8021q kill it)
1458 */
Joe Perches1dc32912008-07-11 15:17:08 -07001459 if ((hw->mng_cookie.status &
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00001460 E1000_MNG_DHCP_COOKIE_STATUS_VLAN_SUPPORT) &&
1461 !test_bit(adapter->mng_vlan_id, adapter->active_vlans)) {
Patrick McHardy80d5c362013-04-19 02:04:28 +00001462 e1000_vlan_rx_kill_vid(netdev, htons(ETH_P_8021Q),
1463 adapter->mng_vlan_id);
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001464 }
Jeff Kirsherb55ccb32006-01-12 16:50:30 -08001465
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 return 0;
1467}
1468
1469/**
1470 * e1000_check_64k_bound - check that memory doesn't cross 64kB boundary
1471 * @adapter: address of board private structure
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001472 * @start: address of beginning of memory
1473 * @len: length of memory
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 **/
Joe Perches64798842008-07-11 15:17:02 -07001475static bool e1000_check_64k_bound(struct e1000_adapter *adapter, void *start,
1476 unsigned long len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477{
Joe Perches1dc32912008-07-11 15:17:08 -07001478 struct e1000_hw *hw = &adapter->hw;
Joe Perchese982f172008-07-11 15:17:18 -07001479 unsigned long begin = (unsigned long)start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 unsigned long end = begin + len;
1481
Malli Chilakala26483452005-04-28 19:44:46 -07001482 /* First rev 82545 and 82546 need to not allow any memory
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00001483 * write location to cross 64k boundary due to errata 23
1484 */
Joe Perches1dc32912008-07-11 15:17:08 -07001485 if (hw->mac_type == e1000_82545 ||
Dirk Brandewie5377a412011-01-06 14:29:54 +00001486 hw->mac_type == e1000_ce4100 ||
Joe Perches1dc32912008-07-11 15:17:08 -07001487 hw->mac_type == e1000_82546) {
Joe Perchesc3033b02008-03-21 11:06:25 -07001488 return ((begin ^ (end - 1)) >> 16) != 0 ? false : true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 }
1490
Joe Perchesc3033b02008-03-21 11:06:25 -07001491 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492}
1493
1494/**
1495 * e1000_setup_tx_resources - allocate Tx resources (Descriptors)
1496 * @adapter: board private structure
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001497 * @txdr: tx descriptor ring (for a specific queue) to setup
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 *
1499 * Return 0 on success, negative on failure
1500 **/
Joe Perches64798842008-07-11 15:17:02 -07001501static int e1000_setup_tx_resources(struct e1000_adapter *adapter,
1502 struct e1000_tx_ring *txdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 struct pci_dev *pdev = adapter->pdev;
1505 int size;
1506
1507 size = sizeof(struct e1000_buffer) * txdr->count;
Eric Dumazet89bf67f2010-11-22 00:15:06 +00001508 txdr->buffer_info = vzalloc(size);
Joe Perches14f8dc42013-02-07 11:46:27 +00001509 if (!txdr->buffer_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
1512 /* round up to nearest 4K */
1513
1514 txdr->size = txdr->count * sizeof(struct e1000_tx_desc);
Milind Arun Choudhary9099cfb2007-04-27 13:55:29 -07001515 txdr->size = ALIGN(txdr->size, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516
Nick Nunleyb16f53b2010-04-27 13:08:45 +00001517 txdr->desc = dma_alloc_coherent(&pdev->dev, txdr->size, &txdr->dma,
1518 GFP_KERNEL);
Jesse Brandeburg96838a42006-01-18 13:01:39 -08001519 if (!txdr->desc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520setup_tx_desc_die:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 vfree(txdr->buffer_info);
1522 return -ENOMEM;
1523 }
1524
Malli Chilakala26483452005-04-28 19:44:46 -07001525 /* Fix for errata 23, can't cross 64kB boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 if (!e1000_check_64k_bound(adapter, txdr->desc, txdr->size)) {
1527 void *olddesc = txdr->desc;
1528 dma_addr_t olddma = txdr->dma;
Emil Tantilovfeb8f472010-07-26 23:37:21 -07001529 e_err(tx_err, "txdr align check failed: %u bytes at %p\n",
Emil Tantilov675ad472010-04-27 14:02:58 +00001530 txdr->size, txdr->desc);
Malli Chilakala26483452005-04-28 19:44:46 -07001531 /* Try again, without freeing the previous */
Nick Nunleyb16f53b2010-04-27 13:08:45 +00001532 txdr->desc = dma_alloc_coherent(&pdev->dev, txdr->size,
1533 &txdr->dma, GFP_KERNEL);
Malli Chilakala26483452005-04-28 19:44:46 -07001534 /* Failed allocation, critical failure */
Jesse Brandeburg96838a42006-01-18 13:01:39 -08001535 if (!txdr->desc) {
Nick Nunleyb16f53b2010-04-27 13:08:45 +00001536 dma_free_coherent(&pdev->dev, txdr->size, olddesc,
1537 olddma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 goto setup_tx_desc_die;
1539 }
1540
1541 if (!e1000_check_64k_bound(adapter, txdr->desc, txdr->size)) {
1542 /* give up */
Nick Nunleyb16f53b2010-04-27 13:08:45 +00001543 dma_free_coherent(&pdev->dev, txdr->size, txdr->desc,
1544 txdr->dma);
1545 dma_free_coherent(&pdev->dev, txdr->size, olddesc,
1546 olddma);
Emil Tantilovfeb8f472010-07-26 23:37:21 -07001547 e_err(probe, "Unable to allocate aligned memory "
Emil Tantilov675ad472010-04-27 14:02:58 +00001548 "for the transmit descriptor ring\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 vfree(txdr->buffer_info);
1550 return -ENOMEM;
1551 } else {
Malli Chilakala26483452005-04-28 19:44:46 -07001552 /* Free old allocation, new allocation was successful */
Nick Nunleyb16f53b2010-04-27 13:08:45 +00001553 dma_free_coherent(&pdev->dev, txdr->size, olddesc,
1554 olddma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 }
1556 }
1557 memset(txdr->desc, 0, txdr->size);
1558
1559 txdr->next_to_use = 0;
1560 txdr->next_to_clean = 0;
1561
1562 return 0;
1563}
1564
1565/**
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001566 * e1000_setup_all_tx_resources - wrapper to allocate Tx resources
1567 * (Descriptors) for all queues
1568 * @adapter: board private structure
1569 *
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001570 * Return 0 on success, negative on failure
1571 **/
Joe Perches64798842008-07-11 15:17:02 -07001572int e1000_setup_all_tx_resources(struct e1000_adapter *adapter)
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001573{
1574 int i, err = 0;
1575
Jeff Kirsherf56799e2006-01-12 16:50:39 -08001576 for (i = 0; i < adapter->num_tx_queues; i++) {
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001577 err = e1000_setup_tx_resources(adapter, &adapter->tx_ring[i]);
1578 if (err) {
Emil Tantilovfeb8f472010-07-26 23:37:21 -07001579 e_err(probe, "Allocation for Tx Queue %u failed\n", i);
Vasily Averin3fbbc722006-08-28 14:56:24 -07001580 for (i-- ; i >= 0; i--)
1581 e1000_free_tx_resources(adapter,
1582 &adapter->tx_ring[i]);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001583 break;
1584 }
1585 }
1586
1587 return err;
1588}
1589
1590/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 * e1000_configure_tx - Configure 8254x Transmit Unit after Reset
1592 * @adapter: board private structure
1593 *
1594 * Configure the Tx unit of the MAC after a reset.
1595 **/
Joe Perches64798842008-07-11 15:17:02 -07001596static void e1000_configure_tx(struct e1000_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597{
Joe Perches406874a2008-04-03 10:06:32 -07001598 u64 tdba;
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001599 struct e1000_hw *hw = &adapter->hw;
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00001600 u32 tdlen, tctl, tipg;
Joe Perches406874a2008-04-03 10:06:32 -07001601 u32 ipgr1, ipgr2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602
1603 /* Setup the HW Tx Head and Tail descriptor pointers */
1604
Jeff Kirsherf56799e2006-01-12 16:50:39 -08001605 switch (adapter->num_tx_queues) {
Mallikarjuna R Chilakala24025e4ec2005-10-04 07:03:23 -04001606 case 1:
1607 default:
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001608 tdba = adapter->tx_ring[0].dma;
1609 tdlen = adapter->tx_ring[0].count *
1610 sizeof(struct e1000_tx_desc);
Joe Perches1dc32912008-07-11 15:17:08 -07001611 ew32(TDLEN, tdlen);
1612 ew32(TDBAH, (tdba >> 32));
1613 ew32(TDBAL, (tdba & 0x00000000ffffffffULL));
1614 ew32(TDT, 0);
1615 ew32(TDH, 0);
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00001616 adapter->tx_ring[0].tdh = ((hw->mac_type >= e1000_82543) ?
1617 E1000_TDH : E1000_82542_TDH);
1618 adapter->tx_ring[0].tdt = ((hw->mac_type >= e1000_82543) ?
1619 E1000_TDT : E1000_82542_TDT);
Mallikarjuna R Chilakala24025e4ec2005-10-04 07:03:23 -04001620 break;
1621 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
1623 /* Set the default values for the Tx Inter Packet Gap timer */
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00001624 if ((hw->media_type == e1000_media_type_fiber ||
Jesse Brandeburgd89b6c62006-12-15 10:38:32 +01001625 hw->media_type == e1000_media_type_internal_serdes))
Jeff Kirsher0fadb052006-01-12 16:51:05 -08001626 tipg = DEFAULT_82543_TIPG_IPGT_FIBER;
1627 else
1628 tipg = DEFAULT_82543_TIPG_IPGT_COPPER;
1629
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001630 switch (hw->mac_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 case e1000_82542_rev2_0:
1632 case e1000_82542_rev2_1:
1633 tipg = DEFAULT_82542_TIPG_IPGT;
Jeff Kirsher0fadb052006-01-12 16:51:05 -08001634 ipgr1 = DEFAULT_82542_TIPG_IPGR1;
1635 ipgr2 = DEFAULT_82542_TIPG_IPGR2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 break;
1637 default:
Jeff Kirsher0fadb052006-01-12 16:51:05 -08001638 ipgr1 = DEFAULT_82543_TIPG_IPGR1;
1639 ipgr2 = DEFAULT_82543_TIPG_IPGR2;
1640 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 }
Jeff Kirsher0fadb052006-01-12 16:51:05 -08001642 tipg |= ipgr1 << E1000_TIPG_IPGR1_SHIFT;
1643 tipg |= ipgr2 << E1000_TIPG_IPGR2_SHIFT;
Joe Perches1dc32912008-07-11 15:17:08 -07001644 ew32(TIPG, tipg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
1646 /* Set the Tx Interrupt Delay register */
1647
Joe Perches1dc32912008-07-11 15:17:08 -07001648 ew32(TIDV, adapter->tx_int_delay);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001649 if (hw->mac_type >= e1000_82540)
Joe Perches1dc32912008-07-11 15:17:08 -07001650 ew32(TADV, adapter->tx_abs_int_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
1652 /* Program the Transmit Control Register */
1653
Joe Perches1dc32912008-07-11 15:17:08 -07001654 tctl = er32(TCTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 tctl &= ~E1000_TCTL_CT;
Jeff Kirsher7e6c9862006-03-02 18:19:30 -08001656 tctl |= E1000_TCTL_PSP | E1000_TCTL_RTLC |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 (E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT);
1658
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001659 e1000_config_collision_dist(hw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
1661 /* Setup Transmit Descriptor Settings for eop descriptor */
Jesse Brandeburg6a042da2006-11-01 08:48:04 -08001662 adapter->txd_cmd = E1000_TXD_CMD_EOP | E1000_TXD_CMD_IFCS;
1663
1664 /* only set IDE if we are delaying interrupts using the timers */
1665 if (adapter->tx_int_delay)
1666 adapter->txd_cmd |= E1000_TXD_CMD_IDE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001668 if (hw->mac_type < e1000_82543)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 adapter->txd_cmd |= E1000_TXD_CMD_RPS;
1670 else
1671 adapter->txd_cmd |= E1000_TXD_CMD_RS;
1672
1673 /* Cache if we're 82544 running in PCI-X because we'll
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00001674 * need this to apply a workaround later in the send path.
1675 */
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001676 if (hw->mac_type == e1000_82544 &&
1677 hw->bus_type == e1000_bus_type_pcix)
Rusty Russell3db1cd52011-12-19 13:56:45 +00001678 adapter->pcix_82544 = true;
Jeff Kirsher7e6c9862006-03-02 18:19:30 -08001679
Joe Perches1dc32912008-07-11 15:17:08 -07001680 ew32(TCTL, tctl);
Jeff Kirsher7e6c9862006-03-02 18:19:30 -08001681
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682}
1683
1684/**
1685 * e1000_setup_rx_resources - allocate Rx resources (Descriptors)
1686 * @adapter: board private structure
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001687 * @rxdr: rx descriptor ring (for a specific queue) to setup
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 *
1689 * Returns 0 on success, negative on failure
1690 **/
Joe Perches64798842008-07-11 15:17:02 -07001691static int e1000_setup_rx_resources(struct e1000_adapter *adapter,
1692 struct e1000_rx_ring *rxdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 struct pci_dev *pdev = adapter->pdev;
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001695 int size, desc_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
1697 size = sizeof(struct e1000_buffer) * rxdr->count;
Eric Dumazet89bf67f2010-11-22 00:15:06 +00001698 rxdr->buffer_info = vzalloc(size);
Joe Perches14f8dc42013-02-07 11:46:27 +00001699 if (!rxdr->buffer_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00001702 desc_len = sizeof(struct e1000_rx_desc);
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001703
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 /* Round up to nearest 4K */
1705
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001706 rxdr->size = rxdr->count * desc_len;
Milind Arun Choudhary9099cfb2007-04-27 13:55:29 -07001707 rxdr->size = ALIGN(rxdr->size, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708
Nick Nunleyb16f53b2010-04-27 13:08:45 +00001709 rxdr->desc = dma_alloc_coherent(&pdev->dev, rxdr->size, &rxdr->dma,
1710 GFP_KERNEL);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001711 if (!rxdr->desc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712setup_rx_desc_die:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 vfree(rxdr->buffer_info);
1714 return -ENOMEM;
1715 }
1716
Malli Chilakala26483452005-04-28 19:44:46 -07001717 /* Fix for errata 23, can't cross 64kB boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 if (!e1000_check_64k_bound(adapter, rxdr->desc, rxdr->size)) {
1719 void *olddesc = rxdr->desc;
1720 dma_addr_t olddma = rxdr->dma;
Emil Tantilovfeb8f472010-07-26 23:37:21 -07001721 e_err(rx_err, "rxdr align check failed: %u bytes at %p\n",
Emil Tantilov675ad472010-04-27 14:02:58 +00001722 rxdr->size, rxdr->desc);
Malli Chilakala26483452005-04-28 19:44:46 -07001723 /* Try again, without freeing the previous */
Nick Nunleyb16f53b2010-04-27 13:08:45 +00001724 rxdr->desc = dma_alloc_coherent(&pdev->dev, rxdr->size,
1725 &rxdr->dma, GFP_KERNEL);
Malli Chilakala26483452005-04-28 19:44:46 -07001726 /* Failed allocation, critical failure */
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001727 if (!rxdr->desc) {
Nick Nunleyb16f53b2010-04-27 13:08:45 +00001728 dma_free_coherent(&pdev->dev, rxdr->size, olddesc,
1729 olddma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 goto setup_rx_desc_die;
1731 }
1732
1733 if (!e1000_check_64k_bound(adapter, rxdr->desc, rxdr->size)) {
1734 /* give up */
Nick Nunleyb16f53b2010-04-27 13:08:45 +00001735 dma_free_coherent(&pdev->dev, rxdr->size, rxdr->desc,
1736 rxdr->dma);
1737 dma_free_coherent(&pdev->dev, rxdr->size, olddesc,
1738 olddma);
Emil Tantilovfeb8f472010-07-26 23:37:21 -07001739 e_err(probe, "Unable to allocate aligned memory for "
1740 "the Rx descriptor ring\n");
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001741 goto setup_rx_desc_die;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 } else {
Malli Chilakala26483452005-04-28 19:44:46 -07001743 /* Free old allocation, new allocation was successful */
Nick Nunleyb16f53b2010-04-27 13:08:45 +00001744 dma_free_coherent(&pdev->dev, rxdr->size, olddesc,
1745 olddma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 }
1747 }
1748 memset(rxdr->desc, 0, rxdr->size);
1749
1750 rxdr->next_to_clean = 0;
1751 rxdr->next_to_use = 0;
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00001752 rxdr->rx_skb_top = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
1754 return 0;
1755}
1756
1757/**
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001758 * e1000_setup_all_rx_resources - wrapper to allocate Rx resources
1759 * (Descriptors) for all queues
1760 * @adapter: board private structure
1761 *
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001762 * Return 0 on success, negative on failure
1763 **/
Joe Perches64798842008-07-11 15:17:02 -07001764int e1000_setup_all_rx_resources(struct e1000_adapter *adapter)
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001765{
1766 int i, err = 0;
1767
Jeff Kirsherf56799e2006-01-12 16:50:39 -08001768 for (i = 0; i < adapter->num_rx_queues; i++) {
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001769 err = e1000_setup_rx_resources(adapter, &adapter->rx_ring[i]);
1770 if (err) {
Emil Tantilovfeb8f472010-07-26 23:37:21 -07001771 e_err(probe, "Allocation for Rx Queue %u failed\n", i);
Vasily Averin3fbbc722006-08-28 14:56:24 -07001772 for (i-- ; i >= 0; i--)
1773 e1000_free_rx_resources(adapter,
1774 &adapter->rx_ring[i]);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001775 break;
1776 }
1777 }
1778
1779 return err;
1780}
1781
1782/**
Malli Chilakala26483452005-04-28 19:44:46 -07001783 * e1000_setup_rctl - configure the receive control registers
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 * @adapter: Board private structure
1785 **/
Joe Perches64798842008-07-11 15:17:02 -07001786static void e1000_setup_rctl(struct e1000_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787{
Joe Perches1dc32912008-07-11 15:17:08 -07001788 struct e1000_hw *hw = &adapter->hw;
Brandeburg, Jesse630b25c2008-09-16 13:01:28 -07001789 u32 rctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790
Joe Perches1dc32912008-07-11 15:17:08 -07001791 rctl = er32(RCTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
1793 rctl &= ~(3 << E1000_RCTL_MO_SHIFT);
1794
Dean Nelsond5bc77a2011-09-16 16:52:54 +00001795 rctl |= E1000_RCTL_BAM | E1000_RCTL_LBM_NO |
1796 E1000_RCTL_RDMTS_HALF |
Joe Perches1dc32912008-07-11 15:17:08 -07001797 (hw->mc_filter_type << E1000_RCTL_MO_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798
Joe Perches1dc32912008-07-11 15:17:08 -07001799 if (hw->tbi_compatibility_on == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 rctl |= E1000_RCTL_SBP;
1801 else
1802 rctl &= ~E1000_RCTL_SBP;
1803
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001804 if (adapter->netdev->mtu <= ETH_DATA_LEN)
1805 rctl &= ~E1000_RCTL_LPE;
1806 else
1807 rctl |= E1000_RCTL_LPE;
1808
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 /* Setup buffer sizes */
Auke Kok9e2feac2006-04-14 19:05:18 -07001810 rctl &= ~E1000_RCTL_SZ_4096;
1811 rctl |= E1000_RCTL_BSEX;
1812 switch (adapter->rx_buffer_len) {
Jeff Kirshera1415ee2006-02-28 20:24:07 -08001813 case E1000_RXBUFFER_2048:
1814 default:
1815 rctl |= E1000_RCTL_SZ_2048;
1816 rctl &= ~E1000_RCTL_BSEX;
1817 break;
1818 case E1000_RXBUFFER_4096:
1819 rctl |= E1000_RCTL_SZ_4096;
1820 break;
1821 case E1000_RXBUFFER_8192:
1822 rctl |= E1000_RCTL_SZ_8192;
1823 break;
1824 case E1000_RXBUFFER_16384:
1825 rctl |= E1000_RCTL_SZ_16384;
1826 break;
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001827 }
1828
Ben Greeare825b732012-04-04 06:01:29 +00001829 /* This is useful for sniffing bad packets. */
1830 if (adapter->netdev->features & NETIF_F_RXALL) {
1831 /* UPE and MPE will be handled by normal PROMISC logic
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00001832 * in e1000e_set_rx_mode
1833 */
Ben Greeare825b732012-04-04 06:01:29 +00001834 rctl |= (E1000_RCTL_SBP | /* Receive bad packets */
1835 E1000_RCTL_BAM | /* RX All Bcast Pkts */
1836 E1000_RCTL_PMCF); /* RX All MAC Ctrl Pkts */
1837
1838 rctl &= ~(E1000_RCTL_VFE | /* Disable VLAN filter */
1839 E1000_RCTL_DPF | /* Allow filtered pause */
1840 E1000_RCTL_CFIEN); /* Dis VLAN CFIEN Filter */
1841 /* Do not mess with E1000_CTRL_VME, it affects transmit as well,
1842 * and that breaks VLANs.
1843 */
1844 }
1845
Joe Perches1dc32912008-07-11 15:17:08 -07001846 ew32(RCTL, rctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847}
1848
1849/**
1850 * e1000_configure_rx - Configure 8254x Receive Unit after Reset
1851 * @adapter: board private structure
1852 *
1853 * Configure the Rx unit of the MAC after a reset.
1854 **/
Joe Perches64798842008-07-11 15:17:02 -07001855static void e1000_configure_rx(struct e1000_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856{
Joe Perches406874a2008-04-03 10:06:32 -07001857 u64 rdba;
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001858 struct e1000_hw *hw = &adapter->hw;
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00001859 u32 rdlen, rctl, rxcsum;
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001860
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00001861 if (adapter->netdev->mtu > ETH_DATA_LEN) {
1862 rdlen = adapter->rx_ring[0].count *
1863 sizeof(struct e1000_rx_desc);
1864 adapter->clean_rx = e1000_clean_jumbo_rx_irq;
1865 adapter->alloc_rx_buf = e1000_alloc_jumbo_rx_buffers;
1866 } else {
1867 rdlen = adapter->rx_ring[0].count *
1868 sizeof(struct e1000_rx_desc);
1869 adapter->clean_rx = e1000_clean_rx_irq;
1870 adapter->alloc_rx_buf = e1000_alloc_rx_buffers;
1871 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872
1873 /* disable receives while setting up the descriptors */
Joe Perches1dc32912008-07-11 15:17:08 -07001874 rctl = er32(RCTL);
1875 ew32(RCTL, rctl & ~E1000_RCTL_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876
1877 /* set the Receive Delay Timer Register */
Joe Perches1dc32912008-07-11 15:17:08 -07001878 ew32(RDTR, adapter->rx_int_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001880 if (hw->mac_type >= e1000_82540) {
Joe Perches1dc32912008-07-11 15:17:08 -07001881 ew32(RADV, adapter->rx_abs_int_delay);
Jesse Brandeburg835bb122006-11-01 08:48:13 -08001882 if (adapter->itr_setting != 0)
Joe Perches1dc32912008-07-11 15:17:08 -07001883 ew32(ITR, 1000000000 / (adapter->itr * 256));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 }
1885
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001886 /* Setup the HW Rx Head and Tail Descriptor Pointers and
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00001887 * the Base and Length of the Rx Descriptor Ring
1888 */
Jeff Kirsherf56799e2006-01-12 16:50:39 -08001889 switch (adapter->num_rx_queues) {
Mallikarjuna R Chilakala24025e4ec2005-10-04 07:03:23 -04001890 case 1:
1891 default:
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001892 rdba = adapter->rx_ring[0].dma;
Joe Perches1dc32912008-07-11 15:17:08 -07001893 ew32(RDLEN, rdlen);
1894 ew32(RDBAH, (rdba >> 32));
1895 ew32(RDBAL, (rdba & 0x00000000ffffffffULL));
1896 ew32(RDT, 0);
1897 ew32(RDH, 0);
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00001898 adapter->rx_ring[0].rdh = ((hw->mac_type >= e1000_82543) ?
1899 E1000_RDH : E1000_82542_RDH);
1900 adapter->rx_ring[0].rdt = ((hw->mac_type >= e1000_82543) ?
1901 E1000_RDT : E1000_82542_RDT);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001902 break;
Mallikarjuna R Chilakala24025e4ec2005-10-04 07:03:23 -04001903 }
1904
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 /* Enable 82543 Receive Checksum Offload for TCP and UDP */
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001906 if (hw->mac_type >= e1000_82543) {
Joe Perches1dc32912008-07-11 15:17:08 -07001907 rxcsum = er32(RXCSUM);
Brandeburg, Jesse630b25c2008-09-16 13:01:28 -07001908 if (adapter->rx_csum)
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001909 rxcsum |= E1000_RXCSUM_TUOFL;
Brandeburg, Jesse630b25c2008-09-16 13:01:28 -07001910 else
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001911 /* don't need to clear IPPCSE as it defaults to 0 */
Brandeburg, Jesse630b25c2008-09-16 13:01:28 -07001912 rxcsum &= ~E1000_RXCSUM_TUOFL;
Joe Perches1dc32912008-07-11 15:17:08 -07001913 ew32(RXCSUM, rxcsum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 }
1915
1916 /* Enable Receives */
Dean Nelsond5bc77a2011-09-16 16:52:54 +00001917 ew32(RCTL, rctl | E1000_RCTL_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918}
1919
1920/**
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001921 * e1000_free_tx_resources - Free Tx Resources per Queue
1922 * @adapter: board private structure
1923 * @tx_ring: Tx descriptor ring for a specific queue
1924 *
1925 * Free all transmit software resources
1926 **/
Joe Perches64798842008-07-11 15:17:02 -07001927static void e1000_free_tx_resources(struct e1000_adapter *adapter,
1928 struct e1000_tx_ring *tx_ring)
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001929{
1930 struct pci_dev *pdev = adapter->pdev;
1931
1932 e1000_clean_tx_ring(adapter, tx_ring);
1933
1934 vfree(tx_ring->buffer_info);
1935 tx_ring->buffer_info = NULL;
1936
Nick Nunleyb16f53b2010-04-27 13:08:45 +00001937 dma_free_coherent(&pdev->dev, tx_ring->size, tx_ring->desc,
1938 tx_ring->dma);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001939
1940 tx_ring->desc = NULL;
1941}
1942
1943/**
1944 * e1000_free_all_tx_resources - Free Tx Resources for All Queues
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 * @adapter: board private structure
1946 *
1947 * Free all transmit software resources
1948 **/
Joe Perches64798842008-07-11 15:17:02 -07001949void e1000_free_all_tx_resources(struct e1000_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950{
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001951 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952
Jeff Kirsherf56799e2006-01-12 16:50:39 -08001953 for (i = 0; i < adapter->num_tx_queues; i++)
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001954 e1000_free_tx_resources(adapter, &adapter->tx_ring[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955}
1956
Joe Perches64798842008-07-11 15:17:02 -07001957static void e1000_unmap_and_free_tx_resource(struct e1000_adapter *adapter,
1958 struct e1000_buffer *buffer_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959{
Alexander Duyck602c0552009-12-02 16:46:00 +00001960 if (buffer_info->dma) {
1961 if (buffer_info->mapped_as_page)
Nick Nunleyb16f53b2010-04-27 13:08:45 +00001962 dma_unmap_page(&adapter->pdev->dev, buffer_info->dma,
1963 buffer_info->length, DMA_TO_DEVICE);
Alexander Duyck602c0552009-12-02 16:46:00 +00001964 else
Nick Nunleyb16f53b2010-04-27 13:08:45 +00001965 dma_unmap_single(&adapter->pdev->dev, buffer_info->dma,
Alexander Duyck602c0552009-12-02 16:46:00 +00001966 buffer_info->length,
Nick Nunleyb16f53b2010-04-27 13:08:45 +00001967 DMA_TO_DEVICE);
Alexander Duyck602c0552009-12-02 16:46:00 +00001968 buffer_info->dma = 0;
1969 }
Jesse Brandeburga9ebadd2006-11-01 08:47:53 -08001970 if (buffer_info->skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 dev_kfree_skb_any(buffer_info->skb);
Jesse Brandeburga9ebadd2006-11-01 08:47:53 -08001972 buffer_info->skb = NULL;
1973 }
Alexander Duyck37e73df2009-03-25 21:58:45 +00001974 buffer_info->time_stamp = 0;
Jesse Brandeburga9ebadd2006-11-01 08:47:53 -08001975 /* buffer_info must be completely set up in the transmit path */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976}
1977
1978/**
1979 * e1000_clean_tx_ring - Free Tx Buffers
1980 * @adapter: board private structure
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001981 * @tx_ring: ring to be cleaned
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 **/
Joe Perches64798842008-07-11 15:17:02 -07001983static void e1000_clean_tx_ring(struct e1000_adapter *adapter,
1984 struct e1000_tx_ring *tx_ring)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985{
Joe Perches1dc32912008-07-11 15:17:08 -07001986 struct e1000_hw *hw = &adapter->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 struct e1000_buffer *buffer_info;
1988 unsigned long size;
1989 unsigned int i;
1990
1991 /* Free all the Tx ring sk_buffs */
1992
Jesse Brandeburg96838a42006-01-18 13:01:39 -08001993 for (i = 0; i < tx_ring->count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 buffer_info = &tx_ring->buffer_info[i];
1995 e1000_unmap_and_free_tx_resource(adapter, buffer_info);
1996 }
1997
Otto Estuardo Solares Cabrera2f66fd32012-08-24 08:04:58 +00001998 netdev_reset_queue(adapter->netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 size = sizeof(struct e1000_buffer) * tx_ring->count;
2000 memset(tx_ring->buffer_info, 0, size);
2001
2002 /* Zero out the descriptor ring */
2003
2004 memset(tx_ring->desc, 0, tx_ring->size);
2005
2006 tx_ring->next_to_use = 0;
2007 tx_ring->next_to_clean = 0;
Rusty Russell3db1cd52011-12-19 13:56:45 +00002008 tx_ring->last_tx_tso = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
Joe Perches1dc32912008-07-11 15:17:08 -07002010 writel(0, hw->hw_addr + tx_ring->tdh);
2011 writel(0, hw->hw_addr + tx_ring->tdt);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002012}
2013
2014/**
2015 * e1000_clean_all_tx_rings - Free Tx Buffers for all queues
2016 * @adapter: board private structure
2017 **/
Joe Perches64798842008-07-11 15:17:02 -07002018static void e1000_clean_all_tx_rings(struct e1000_adapter *adapter)
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002019{
2020 int i;
2021
Jeff Kirsherf56799e2006-01-12 16:50:39 -08002022 for (i = 0; i < adapter->num_tx_queues; i++)
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002023 e1000_clean_tx_ring(adapter, &adapter->tx_ring[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024}
2025
2026/**
2027 * e1000_free_rx_resources - Free Rx Resources
2028 * @adapter: board private structure
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002029 * @rx_ring: ring to clean the resources from
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 *
2031 * Free all receive software resources
2032 **/
Joe Perches64798842008-07-11 15:17:02 -07002033static void e1000_free_rx_resources(struct e1000_adapter *adapter,
2034 struct e1000_rx_ring *rx_ring)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 struct pci_dev *pdev = adapter->pdev;
2037
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002038 e1000_clean_rx_ring(adapter, rx_ring);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039
2040 vfree(rx_ring->buffer_info);
2041 rx_ring->buffer_info = NULL;
2042
Nick Nunleyb16f53b2010-04-27 13:08:45 +00002043 dma_free_coherent(&pdev->dev, rx_ring->size, rx_ring->desc,
2044 rx_ring->dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045
2046 rx_ring->desc = NULL;
2047}
2048
2049/**
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002050 * e1000_free_all_rx_resources - Free Rx Resources for All Queues
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 * @adapter: board private structure
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002052 *
2053 * Free all receive software resources
2054 **/
Joe Perches64798842008-07-11 15:17:02 -07002055void e1000_free_all_rx_resources(struct e1000_adapter *adapter)
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002056{
2057 int i;
2058
Jeff Kirsherf56799e2006-01-12 16:50:39 -08002059 for (i = 0; i < adapter->num_rx_queues; i++)
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002060 e1000_free_rx_resources(adapter, &adapter->rx_ring[i]);
2061}
2062
2063/**
2064 * e1000_clean_rx_ring - Free Rx Buffers per Queue
2065 * @adapter: board private structure
2066 * @rx_ring: ring to free buffers from
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 **/
Joe Perches64798842008-07-11 15:17:02 -07002068static void e1000_clean_rx_ring(struct e1000_adapter *adapter,
2069 struct e1000_rx_ring *rx_ring)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070{
Joe Perches1dc32912008-07-11 15:17:08 -07002071 struct e1000_hw *hw = &adapter->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 struct e1000_buffer *buffer_info;
2073 struct pci_dev *pdev = adapter->pdev;
2074 unsigned long size;
Brandeburg, Jesse630b25c2008-09-16 13:01:28 -07002075 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076
2077 /* Free all the Rx ring sk_buffs */
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002078 for (i = 0; i < rx_ring->count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 buffer_info = &rx_ring->buffer_info[i];
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00002080 if (buffer_info->dma &&
2081 adapter->clean_rx == e1000_clean_rx_irq) {
Nick Nunleyb16f53b2010-04-27 13:08:45 +00002082 dma_unmap_single(&pdev->dev, buffer_info->dma,
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00002083 buffer_info->length,
Nick Nunleyb16f53b2010-04-27 13:08:45 +00002084 DMA_FROM_DEVICE);
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00002085 } else if (buffer_info->dma &&
2086 adapter->clean_rx == e1000_clean_jumbo_rx_irq) {
Nick Nunleyb16f53b2010-04-27 13:08:45 +00002087 dma_unmap_page(&pdev->dev, buffer_info->dma,
2088 buffer_info->length,
2089 DMA_FROM_DEVICE);
Jesse Brandeburg679be3b2009-06-30 12:45:34 +00002090 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091
Jesse Brandeburg679be3b2009-06-30 12:45:34 +00002092 buffer_info->dma = 0;
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00002093 if (buffer_info->page) {
2094 put_page(buffer_info->page);
2095 buffer_info->page = NULL;
2096 }
Jesse Brandeburg679be3b2009-06-30 12:45:34 +00002097 if (buffer_info->skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 dev_kfree_skb(buffer_info->skb);
2099 buffer_info->skb = NULL;
Jeff Kirsher997f5cb2006-01-12 16:50:55 -08002100 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 }
2102
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00002103 /* there also may be some cached data from a chained receive */
2104 if (rx_ring->rx_skb_top) {
2105 dev_kfree_skb(rx_ring->rx_skb_top);
2106 rx_ring->rx_skb_top = NULL;
2107 }
2108
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 size = sizeof(struct e1000_buffer) * rx_ring->count;
2110 memset(rx_ring->buffer_info, 0, size);
2111
2112 /* Zero out the descriptor ring */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 memset(rx_ring->desc, 0, rx_ring->size);
2114
2115 rx_ring->next_to_clean = 0;
2116 rx_ring->next_to_use = 0;
2117
Joe Perches1dc32912008-07-11 15:17:08 -07002118 writel(0, hw->hw_addr + rx_ring->rdh);
2119 writel(0, hw->hw_addr + rx_ring->rdt);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002120}
2121
2122/**
2123 * e1000_clean_all_rx_rings - Free Rx Buffers for all queues
2124 * @adapter: board private structure
2125 **/
Joe Perches64798842008-07-11 15:17:02 -07002126static void e1000_clean_all_rx_rings(struct e1000_adapter *adapter)
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002127{
2128 int i;
2129
Jeff Kirsherf56799e2006-01-12 16:50:39 -08002130 for (i = 0; i < adapter->num_rx_queues; i++)
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002131 e1000_clean_rx_ring(adapter, &adapter->rx_ring[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132}
2133
2134/* The 82542 2.0 (revision 2) needs to have the receive unit in reset
2135 * and memory write and invalidate disabled for certain operations
2136 */
Joe Perches64798842008-07-11 15:17:02 -07002137static void e1000_enter_82542_rst(struct e1000_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138{
Joe Perches1dc32912008-07-11 15:17:08 -07002139 struct e1000_hw *hw = &adapter->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 struct net_device *netdev = adapter->netdev;
Joe Perches406874a2008-04-03 10:06:32 -07002141 u32 rctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142
Joe Perches1dc32912008-07-11 15:17:08 -07002143 e1000_pci_clear_mwi(hw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144
Joe Perches1dc32912008-07-11 15:17:08 -07002145 rctl = er32(RCTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 rctl |= E1000_RCTL_RST;
Joe Perches1dc32912008-07-11 15:17:08 -07002147 ew32(RCTL, rctl);
2148 E1000_WRITE_FLUSH();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 mdelay(5);
2150
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002151 if (netif_running(netdev))
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002152 e1000_clean_all_rx_rings(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153}
2154
Joe Perches64798842008-07-11 15:17:02 -07002155static void e1000_leave_82542_rst(struct e1000_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156{
Joe Perches1dc32912008-07-11 15:17:08 -07002157 struct e1000_hw *hw = &adapter->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 struct net_device *netdev = adapter->netdev;
Joe Perches406874a2008-04-03 10:06:32 -07002159 u32 rctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160
Joe Perches1dc32912008-07-11 15:17:08 -07002161 rctl = er32(RCTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 rctl &= ~E1000_RCTL_RST;
Joe Perches1dc32912008-07-11 15:17:08 -07002163 ew32(RCTL, rctl);
2164 E1000_WRITE_FLUSH();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 mdelay(5);
2166
Joe Perches1dc32912008-07-11 15:17:08 -07002167 if (hw->pci_cmd_word & PCI_COMMAND_INVALIDATE)
2168 e1000_pci_set_mwi(hw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002170 if (netif_running(netdev)) {
Jeff Kirsher72d64a42006-01-12 16:51:19 -08002171 /* No need to loop, because 82542 supports only 1 queue */
2172 struct e1000_rx_ring *ring = &adapter->rx_ring[0];
Jesse Brandeburg7c4d3362006-01-18 13:01:45 -08002173 e1000_configure_rx(adapter);
Jeff Kirsher72d64a42006-01-12 16:51:19 -08002174 adapter->alloc_rx_buf(adapter, ring, E1000_DESC_UNUSED(ring));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 }
2176}
2177
2178/**
2179 * e1000_set_mac - Change the Ethernet Address of the NIC
2180 * @netdev: network interface device structure
2181 * @p: pointer to an address structure
2182 *
2183 * Returns 0 on success, negative on failure
2184 **/
Joe Perches64798842008-07-11 15:17:02 -07002185static int e1000_set_mac(struct net_device *netdev, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186{
Malli Chilakala60490fe2005-06-17 17:41:45 -07002187 struct e1000_adapter *adapter = netdev_priv(netdev);
Joe Perches1dc32912008-07-11 15:17:08 -07002188 struct e1000_hw *hw = &adapter->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 struct sockaddr *addr = p;
2190
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002191 if (!is_valid_ether_addr(addr->sa_data))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 return -EADDRNOTAVAIL;
2193
2194 /* 82542 2.0 needs to be in reset to write receive address registers */
2195
Joe Perches1dc32912008-07-11 15:17:08 -07002196 if (hw->mac_type == e1000_82542_rev2_0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 e1000_enter_82542_rst(adapter);
2198
2199 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
Joe Perches1dc32912008-07-11 15:17:08 -07002200 memcpy(hw->mac_addr, addr->sa_data, netdev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201
Joe Perches1dc32912008-07-11 15:17:08 -07002202 e1000_rar_set(hw, hw->mac_addr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203
Joe Perches1dc32912008-07-11 15:17:08 -07002204 if (hw->mac_type == e1000_82542_rev2_0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 e1000_leave_82542_rst(adapter);
2206
2207 return 0;
2208}
2209
2210/**
Patrick McHardydb0ce502007-11-13 20:54:59 -08002211 * e1000_set_rx_mode - Secondary Unicast, Multicast and Promiscuous mode set
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 * @netdev: network interface device structure
2213 *
Patrick McHardydb0ce502007-11-13 20:54:59 -08002214 * The set_rx_mode entry point is called whenever the unicast or multicast
2215 * address lists or the network interface flags are updated. This routine is
2216 * responsible for configuring the hardware for proper unicast, multicast,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 * promiscuous mode, and all-multi behavior.
2218 **/
Joe Perches64798842008-07-11 15:17:02 -07002219static void e1000_set_rx_mode(struct net_device *netdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220{
Malli Chilakala60490fe2005-06-17 17:41:45 -07002221 struct e1000_adapter *adapter = netdev_priv(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 struct e1000_hw *hw = &adapter->hw;
Jiri Pirkoccffad252009-05-22 23:22:17 +00002223 struct netdev_hw_addr *ha;
2224 bool use_uc = false;
Joe Perches406874a2008-04-03 10:06:32 -07002225 u32 rctl;
2226 u32 hash_value;
Mallikarjuna R Chilakala868d5302005-10-04 06:58:59 -04002227 int i, rar_entries = E1000_RAR_ENTRIES;
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00002228 int mta_reg_count = E1000_NUM_MTA_REGISTERS;
Jesse Brandeburg81c52282009-04-04 16:36:53 -07002229 u32 *mcarray = kcalloc(mta_reg_count, sizeof(u32), GFP_ATOMIC);
2230
Joe Perches14f8dc42013-02-07 11:46:27 +00002231 if (!mcarray)
Jesse Brandeburg81c52282009-04-04 16:36:53 -07002232 return;
Auke Kokcd94dd02006-06-27 09:08:22 -07002233
Malli Chilakala26483452005-04-28 19:44:46 -07002234 /* Check for Promiscuous and All Multicast modes */
2235
Joe Perches1dc32912008-07-11 15:17:08 -07002236 rctl = er32(RCTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002238 if (netdev->flags & IFF_PROMISC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 rctl |= (E1000_RCTL_UPE | E1000_RCTL_MPE);
Patrick McHardy746b9f02008-07-16 20:15:45 -07002240 rctl &= ~E1000_RCTL_VFE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 } else {
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00002242 if (netdev->flags & IFF_ALLMULTI)
Patrick McHardy746b9f02008-07-16 20:15:45 -07002243 rctl |= E1000_RCTL_MPE;
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00002244 else
Patrick McHardy746b9f02008-07-16 20:15:45 -07002245 rctl &= ~E1000_RCTL_MPE;
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00002246 /* Enable VLAN filter if there is a VLAN */
Jiri Pirko5622e402011-07-21 03:26:31 +00002247 if (e1000_vlan_used(adapter))
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00002248 rctl |= E1000_RCTL_VFE;
Patrick McHardydb0ce502007-11-13 20:54:59 -08002249 }
2250
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08002251 if (netdev_uc_count(netdev) > rar_entries - 1) {
Patrick McHardydb0ce502007-11-13 20:54:59 -08002252 rctl |= E1000_RCTL_UPE;
2253 } else if (!(netdev->flags & IFF_PROMISC)) {
2254 rctl &= ~E1000_RCTL_UPE;
Jiri Pirkoccffad252009-05-22 23:22:17 +00002255 use_uc = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 }
2257
Joe Perches1dc32912008-07-11 15:17:08 -07002258 ew32(RCTL, rctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259
2260 /* 82542 2.0 needs to be in reset to write receive address registers */
2261
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002262 if (hw->mac_type == e1000_82542_rev2_0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 e1000_enter_82542_rst(adapter);
2264
Patrick McHardydb0ce502007-11-13 20:54:59 -08002265 /* load the first 14 addresses into the exact filters 1-14. Unicast
2266 * addresses take precedence to avoid disabling unicast filtering
2267 * when possible.
2268 *
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04002269 * RAR 0 is used for the station MAC address
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 * if there are not 14 addresses, go ahead and clear the filters
2271 */
Jiri Pirkoccffad252009-05-22 23:22:17 +00002272 i = 1;
2273 if (use_uc)
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08002274 netdev_for_each_uc_addr(ha, netdev) {
Jiri Pirkoccffad252009-05-22 23:22:17 +00002275 if (i == rar_entries)
2276 break;
2277 e1000_rar_set(hw, ha->addr, i++);
2278 }
2279
Jiri Pirko22bedad32010-04-01 21:22:57 +00002280 netdev_for_each_mc_addr(ha, netdev) {
Jiri Pirko7a81e9f2010-02-22 09:10:44 +00002281 if (i == rar_entries) {
2282 /* load any remaining addresses into the hash table */
2283 u32 hash_reg, hash_bit, mta;
Jiri Pirko22bedad32010-04-01 21:22:57 +00002284 hash_value = e1000_hash_mc_addr(hw, ha->addr);
Jiri Pirko7a81e9f2010-02-22 09:10:44 +00002285 hash_reg = (hash_value >> 5) & 0x7F;
2286 hash_bit = hash_value & 0x1F;
2287 mta = (1 << hash_bit);
2288 mcarray[hash_reg] |= mta;
Jiri Pirko10886af2010-02-23 01:19:22 -08002289 } else {
Jiri Pirko22bedad32010-04-01 21:22:57 +00002290 e1000_rar_set(hw, ha->addr, i++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 }
2292 }
2293
Jiri Pirko7a81e9f2010-02-22 09:10:44 +00002294 for (; i < rar_entries; i++) {
2295 E1000_WRITE_REG_ARRAY(hw, RA, i << 1, 0);
2296 E1000_WRITE_FLUSH();
2297 E1000_WRITE_REG_ARRAY(hw, RA, (i << 1) + 1, 0);
2298 E1000_WRITE_FLUSH();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 }
2300
Jesse Brandeburg81c52282009-04-04 16:36:53 -07002301 /* write the hash table completely, write from bottom to avoid
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00002302 * both stupid write combining chipsets, and flushing each write
2303 */
Jesse Brandeburg81c52282009-04-04 16:36:53 -07002304 for (i = mta_reg_count - 1; i >= 0 ; i--) {
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00002305 /* If we are on an 82544 has an errata where writing odd
Jesse Brandeburg81c52282009-04-04 16:36:53 -07002306 * offsets overwrites the previous even offset, but writing
2307 * backwards over the range solves the issue by always
2308 * writing the odd offset first
2309 */
2310 E1000_WRITE_REG_ARRAY(hw, MTA, i, mcarray[i]);
2311 }
2312 E1000_WRITE_FLUSH();
2313
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002314 if (hw->mac_type == e1000_82542_rev2_0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 e1000_leave_82542_rst(adapter);
Jesse Brandeburg81c52282009-04-04 16:36:53 -07002316
2317 kfree(mcarray);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318}
2319
Jesse Brandeburga4010af2011-10-05 07:24:41 +00002320/**
2321 * e1000_update_phy_info_task - get phy info
2322 * @work: work struct contained inside adapter struct
2323 *
2324 * Need to wait a few seconds after link up to get diagnostic information from
2325 * the phy
2326 */
Jesse Brandeburg5cf42fc2010-09-22 18:22:17 +00002327static void e1000_update_phy_info_task(struct work_struct *work)
2328{
2329 struct e1000_adapter *adapter = container_of(work,
Jesse Brandeburga4010af2011-10-05 07:24:41 +00002330 struct e1000_adapter,
2331 phy_info_task.work);
Jesse Brandeburg0ef4eed2011-10-05 07:24:51 +00002332 if (test_bit(__E1000_DOWN, &adapter->flags))
2333 return;
2334 mutex_lock(&adapter->mutex);
Jesse Brandeburga4010af2011-10-05 07:24:41 +00002335 e1000_phy_get_info(&adapter->hw, &adapter->phy_info);
Jesse Brandeburg0ef4eed2011-10-05 07:24:51 +00002336 mutex_unlock(&adapter->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337}
2338
2339/**
Jesse Brandeburg5cf42fc2010-09-22 18:22:17 +00002340 * e1000_82547_tx_fifo_stall_task - task to complete work
2341 * @work: work struct contained inside adapter struct
2342 **/
2343static void e1000_82547_tx_fifo_stall_task(struct work_struct *work)
2344{
2345 struct e1000_adapter *adapter = container_of(work,
Jesse Brandeburga4010af2011-10-05 07:24:41 +00002346 struct e1000_adapter,
2347 fifo_stall_task.work);
Joe Perches1dc32912008-07-11 15:17:08 -07002348 struct e1000_hw *hw = &adapter->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 struct net_device *netdev = adapter->netdev;
Joe Perches406874a2008-04-03 10:06:32 -07002350 u32 tctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351
Jesse Brandeburg0ef4eed2011-10-05 07:24:51 +00002352 if (test_bit(__E1000_DOWN, &adapter->flags))
2353 return;
2354 mutex_lock(&adapter->mutex);
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002355 if (atomic_read(&adapter->tx_fifo_stall)) {
Joe Perches1dc32912008-07-11 15:17:08 -07002356 if ((er32(TDT) == er32(TDH)) &&
2357 (er32(TDFT) == er32(TDFH)) &&
2358 (er32(TDFTS) == er32(TDFHS))) {
2359 tctl = er32(TCTL);
2360 ew32(TCTL, tctl & ~E1000_TCTL_EN);
2361 ew32(TDFT, adapter->tx_head_addr);
2362 ew32(TDFH, adapter->tx_head_addr);
2363 ew32(TDFTS, adapter->tx_head_addr);
2364 ew32(TDFHS, adapter->tx_head_addr);
2365 ew32(TCTL, tctl);
2366 E1000_WRITE_FLUSH();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367
2368 adapter->tx_fifo_head = 0;
2369 atomic_set(&adapter->tx_fifo_stall, 0);
2370 netif_wake_queue(netdev);
Jesse Brandeburgbaa34742009-09-25 12:17:23 +00002371 } else if (!test_bit(__E1000_DOWN, &adapter->flags)) {
Jesse Brandeburga4010af2011-10-05 07:24:41 +00002372 schedule_delayed_work(&adapter->fifo_stall_task, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 }
2374 }
Jesse Brandeburg0ef4eed2011-10-05 07:24:51 +00002375 mutex_unlock(&adapter->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376}
2377
Nick Nunleyb5481922010-02-03 14:49:28 +00002378bool e1000_has_link(struct e1000_adapter *adapter)
Jesse Brandeburgbe0f0712009-09-25 12:17:44 +00002379{
2380 struct e1000_hw *hw = &adapter->hw;
2381 bool link_active = false;
Jesse Brandeburgbe0f0712009-09-25 12:17:44 +00002382
Nicolas Schichan6d9e5132011-07-09 00:24:18 +00002383 /* get_link_status is set on LSC (link status) interrupt or rx
2384 * sequence error interrupt (except on intel ce4100).
2385 * get_link_status will stay false until the
2386 * e1000_check_for_link establishes link for copper adapters
2387 * ONLY
Jesse Brandeburgbe0f0712009-09-25 12:17:44 +00002388 */
2389 switch (hw->media_type) {
2390 case e1000_media_type_copper:
Nicolas Schichan6d9e5132011-07-09 00:24:18 +00002391 if (hw->mac_type == e1000_ce4100)
2392 hw->get_link_status = 1;
Jesse Brandeburgbe0f0712009-09-25 12:17:44 +00002393 if (hw->get_link_status) {
Jesse Brandeburg120a5d02009-09-25 15:19:46 -07002394 e1000_check_for_link(hw);
Jesse Brandeburgbe0f0712009-09-25 12:17:44 +00002395 link_active = !hw->get_link_status;
2396 } else {
2397 link_active = true;
2398 }
2399 break;
2400 case e1000_media_type_fiber:
Jesse Brandeburg120a5d02009-09-25 15:19:46 -07002401 e1000_check_for_link(hw);
Jesse Brandeburgbe0f0712009-09-25 12:17:44 +00002402 link_active = !!(er32(STATUS) & E1000_STATUS_LU);
2403 break;
2404 case e1000_media_type_internal_serdes:
Jesse Brandeburg120a5d02009-09-25 15:19:46 -07002405 e1000_check_for_link(hw);
Jesse Brandeburgbe0f0712009-09-25 12:17:44 +00002406 link_active = hw->serdes_has_link;
2407 break;
2408 default:
2409 break;
2410 }
2411
2412 return link_active;
2413}
2414
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415/**
Jesse Brandeburga4010af2011-10-05 07:24:41 +00002416 * e1000_watchdog - work function
2417 * @work: work struct contained inside adapter struct
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 **/
Jesse Brandeburga4010af2011-10-05 07:24:41 +00002419static void e1000_watchdog(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420{
Jesse Brandeburga4010af2011-10-05 07:24:41 +00002421 struct e1000_adapter *adapter = container_of(work,
2422 struct e1000_adapter,
2423 watchdog_task.work);
Joe Perches1dc32912008-07-11 15:17:08 -07002424 struct e1000_hw *hw = &adapter->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 struct net_device *netdev = adapter->netdev;
Jeff Kirsher545c67c2006-01-12 16:50:25 -08002426 struct e1000_tx_ring *txdr = adapter->tx_ring;
Joe Perches406874a2008-04-03 10:06:32 -07002427 u32 link, tctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428
Jesse Brandeburg0ef4eed2011-10-05 07:24:51 +00002429 if (test_bit(__E1000_DOWN, &adapter->flags))
2430 return;
2431
2432 mutex_lock(&adapter->mutex);
Jesse Brandeburgbe0f0712009-09-25 12:17:44 +00002433 link = e1000_has_link(adapter);
2434 if ((netif_carrier_ok(netdev)) && link)
2435 goto link_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002437 if (link) {
2438 if (!netif_carrier_ok(netdev)) {
Joe Perches406874a2008-04-03 10:06:32 -07002439 u32 ctrl;
Joe Perchesc3033b02008-03-21 11:06:25 -07002440 bool txb2b = true;
Jesse Brandeburgbe0f0712009-09-25 12:17:44 +00002441 /* update snapshot of PHY registers on LSC */
Joe Perches1dc32912008-07-11 15:17:08 -07002442 e1000_get_speed_and_duplex(hw,
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00002443 &adapter->link_speed,
2444 &adapter->link_duplex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445
Joe Perches1dc32912008-07-11 15:17:08 -07002446 ctrl = er32(CTRL);
Emil Tantilov675ad472010-04-27 14:02:58 +00002447 pr_info("%s NIC Link is Up %d Mbps %s, "
2448 "Flow Control: %s\n",
2449 netdev->name,
2450 adapter->link_speed,
2451 adapter->link_duplex == FULL_DUPLEX ?
2452 "Full Duplex" : "Half Duplex",
2453 ((ctrl & E1000_CTRL_TFCE) && (ctrl &
2454 E1000_CTRL_RFCE)) ? "RX/TX" : ((ctrl &
2455 E1000_CTRL_RFCE) ? "RX" : ((ctrl &
2456 E1000_CTRL_TFCE) ? "TX" : "None")));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457
Emil Tantilov39ca5f02010-03-26 11:25:58 +00002458 /* adjust timeout factor according to speed/duplex */
Jeff Kirsher66a2b0a2006-01-12 16:50:53 -08002459 adapter->tx_timeout_factor = 1;
Jeff Kirsher7e6c9862006-03-02 18:19:30 -08002460 switch (adapter->link_speed) {
2461 case SPEED_10:
Joe Perchesc3033b02008-03-21 11:06:25 -07002462 txb2b = false;
Jesse Brandeburgbe0f0712009-09-25 12:17:44 +00002463 adapter->tx_timeout_factor = 16;
Jeff Kirsher7e6c9862006-03-02 18:19:30 -08002464 break;
2465 case SPEED_100:
Joe Perchesc3033b02008-03-21 11:06:25 -07002466 txb2b = false;
Jeff Kirsher7e6c9862006-03-02 18:19:30 -08002467 /* maybe add some timeout factor ? */
2468 break;
2469 }
2470
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00002471 /* enable transmits in the hardware */
Joe Perches1dc32912008-07-11 15:17:08 -07002472 tctl = er32(TCTL);
Jeff Kirsher7e6c9862006-03-02 18:19:30 -08002473 tctl |= E1000_TCTL_EN;
Joe Perches1dc32912008-07-11 15:17:08 -07002474 ew32(TCTL, tctl);
Jeff Kirsher66a2b0a2006-01-12 16:50:53 -08002475
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 netif_carrier_on(netdev);
Jesse Brandeburgbaa34742009-09-25 12:17:23 +00002477 if (!test_bit(__E1000_DOWN, &adapter->flags))
Jesse Brandeburga4010af2011-10-05 07:24:41 +00002478 schedule_delayed_work(&adapter->phy_info_task,
2479 2 * HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 adapter->smartspeed = 0;
2481 }
2482 } else {
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002483 if (netif_carrier_ok(netdev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 adapter->link_speed = 0;
2485 adapter->link_duplex = 0;
Emil Tantilov675ad472010-04-27 14:02:58 +00002486 pr_info("%s NIC Link is Down\n",
2487 netdev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 netif_carrier_off(netdev);
Jesse Brandeburgbaa34742009-09-25 12:17:23 +00002489
2490 if (!test_bit(__E1000_DOWN, &adapter->flags))
Jesse Brandeburga4010af2011-10-05 07:24:41 +00002491 schedule_delayed_work(&adapter->phy_info_task,
2492 2 * HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 }
2494
2495 e1000_smartspeed(adapter);
2496 }
2497
Jesse Brandeburgbe0f0712009-09-25 12:17:44 +00002498link_up:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 e1000_update_stats(adapter);
2500
Joe Perches1dc32912008-07-11 15:17:08 -07002501 hw->tx_packet_delta = adapter->stats.tpt - adapter->tpt_old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 adapter->tpt_old = adapter->stats.tpt;
Joe Perches1dc32912008-07-11 15:17:08 -07002503 hw->collision_delta = adapter->stats.colc - adapter->colc_old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 adapter->colc_old = adapter->stats.colc;
2505
2506 adapter->gorcl = adapter->stats.gorcl - adapter->gorcl_old;
2507 adapter->gorcl_old = adapter->stats.gorcl;
2508 adapter->gotcl = adapter->stats.gotcl - adapter->gotcl_old;
2509 adapter->gotcl_old = adapter->stats.gotcl;
2510
Joe Perches1dc32912008-07-11 15:17:08 -07002511 e1000_update_adaptive(hw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512
Jeff Kirsherf56799e2006-01-12 16:50:39 -08002513 if (!netif_carrier_ok(netdev)) {
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002514 if (E1000_DESC_UNUSED(txdr) + 1 < txdr->count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 /* We've lost link, so the controller stops DMA,
2516 * but we've got queued Tx work that's never going
2517 * to get done, so reset controller to flush Tx.
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00002518 * (Do the reset outside of interrupt context).
2519 */
Jeff Kirsher87041632006-03-02 18:21:24 -08002520 adapter->tx_timeout_count++;
2521 schedule_work(&adapter->reset_task);
Jesse Brandeburg0ef4eed2011-10-05 07:24:51 +00002522 /* exit immediately since reset is imminent */
2523 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 }
2525 }
2526
Jesse Brandeburgeab2abf2010-05-04 22:26:03 +00002527 /* Simple mode for Interrupt Throttle Rate (ITR) */
2528 if (hw->mac_type >= e1000_82540 && adapter->itr_setting == 4) {
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00002529 /* Symmetric Tx/Rx gets a reduced ITR=2000;
Jesse Brandeburgeab2abf2010-05-04 22:26:03 +00002530 * Total asymmetrical Tx or Rx gets ITR=8000;
2531 * everyone else is between 2000-8000.
2532 */
2533 u32 goc = (adapter->gotcl + adapter->gorcl) / 10000;
2534 u32 dif = (adapter->gotcl > adapter->gorcl ?
2535 adapter->gotcl - adapter->gorcl :
2536 adapter->gorcl - adapter->gotcl) / 10000;
2537 u32 itr = goc > 0 ? (dif * 6000 / goc + 2000) : 8000;
2538
2539 ew32(ITR, 1000000000 / (itr * 256));
2540 }
2541
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 /* Cause software interrupt to ensure rx ring is cleaned */
Joe Perches1dc32912008-07-11 15:17:08 -07002543 ew32(ICS, E1000_ICS_RXDMT0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544
Malli Chilakala26483452005-04-28 19:44:46 -07002545 /* Force detection of hung controller every watchdog period */
Joe Perchesc3033b02008-03-21 11:06:25 -07002546 adapter->detect_tx_hung = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547
Jesse Brandeburga4010af2011-10-05 07:24:41 +00002548 /* Reschedule the task */
Jesse Brandeburgbaa34742009-09-25 12:17:23 +00002549 if (!test_bit(__E1000_DOWN, &adapter->flags))
Jesse Brandeburga4010af2011-10-05 07:24:41 +00002550 schedule_delayed_work(&adapter->watchdog_task, 2 * HZ);
Jesse Brandeburg0ef4eed2011-10-05 07:24:51 +00002551
2552unlock:
2553 mutex_unlock(&adapter->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554}
2555
Jesse Brandeburg835bb122006-11-01 08:48:13 -08002556enum latency_range {
2557 lowest_latency = 0,
2558 low_latency = 1,
2559 bulk_latency = 2,
2560 latency_invalid = 255
2561};
2562
2563/**
2564 * e1000_update_itr - update the dynamic ITR value based on statistics
Jesse Brandeburg8fce4732009-09-25 12:18:41 +00002565 * @adapter: pointer to adapter
2566 * @itr_setting: current adapter->itr
2567 * @packets: the number of packets during this measurement interval
2568 * @bytes: the number of bytes during this measurement interval
2569 *
Jesse Brandeburg835bb122006-11-01 08:48:13 -08002570 * Stores a new ITR value based on packets and byte
2571 * counts during the last interrupt. The advantage of per interrupt
2572 * computation is faster updates and more accurate ITR for the current
2573 * traffic pattern. Constants in this function were computed
2574 * based on theoretical maximum wire speed and thresholds were set based
2575 * on testing data as well as attempting to minimize response time
2576 * while increasing bulk throughput.
2577 * this functionality is controlled by the InterruptThrottleRate module
2578 * parameter (see e1000_param.c)
Jesse Brandeburg835bb122006-11-01 08:48:13 -08002579 **/
2580static unsigned int e1000_update_itr(struct e1000_adapter *adapter,
Joe Perches64798842008-07-11 15:17:02 -07002581 u16 itr_setting, int packets, int bytes)
Jesse Brandeburg835bb122006-11-01 08:48:13 -08002582{
2583 unsigned int retval = itr_setting;
2584 struct e1000_hw *hw = &adapter->hw;
2585
2586 if (unlikely(hw->mac_type < e1000_82540))
2587 goto update_itr_done;
2588
2589 if (packets == 0)
2590 goto update_itr_done;
2591
Jesse Brandeburg835bb122006-11-01 08:48:13 -08002592 switch (itr_setting) {
2593 case lowest_latency:
Jesse Brandeburg2b653262006-12-15 10:30:44 +01002594 /* jumbo frames get bulk treatment*/
2595 if (bytes/packets > 8000)
2596 retval = bulk_latency;
2597 else if ((packets < 5) && (bytes > 512))
Jesse Brandeburg835bb122006-11-01 08:48:13 -08002598 retval = low_latency;
2599 break;
2600 case low_latency: /* 50 usec aka 20000 ints/s */
2601 if (bytes > 10000) {
Jesse Brandeburg2b653262006-12-15 10:30:44 +01002602 /* jumbo frames need bulk latency setting */
2603 if (bytes/packets > 8000)
2604 retval = bulk_latency;
2605 else if ((packets < 10) || ((bytes/packets) > 1200))
Jesse Brandeburg835bb122006-11-01 08:48:13 -08002606 retval = bulk_latency;
2607 else if ((packets > 35))
2608 retval = lowest_latency;
Jesse Brandeburg2b653262006-12-15 10:30:44 +01002609 } else if (bytes/packets > 2000)
2610 retval = bulk_latency;
2611 else if (packets <= 2 && bytes < 512)
Jesse Brandeburg835bb122006-11-01 08:48:13 -08002612 retval = lowest_latency;
2613 break;
2614 case bulk_latency: /* 250 usec aka 4000 ints/s */
2615 if (bytes > 25000) {
2616 if (packets > 35)
2617 retval = low_latency;
Jesse Brandeburg2b653262006-12-15 10:30:44 +01002618 } else if (bytes < 6000) {
2619 retval = low_latency;
Jesse Brandeburg835bb122006-11-01 08:48:13 -08002620 }
2621 break;
2622 }
2623
2624update_itr_done:
2625 return retval;
2626}
2627
2628static void e1000_set_itr(struct e1000_adapter *adapter)
2629{
2630 struct e1000_hw *hw = &adapter->hw;
Joe Perches406874a2008-04-03 10:06:32 -07002631 u16 current_itr;
2632 u32 new_itr = adapter->itr;
Jesse Brandeburg835bb122006-11-01 08:48:13 -08002633
2634 if (unlikely(hw->mac_type < e1000_82540))
2635 return;
2636
2637 /* for non-gigabit speeds, just fix the interrupt rate at 4000 */
2638 if (unlikely(adapter->link_speed != SPEED_1000)) {
2639 current_itr = 0;
2640 new_itr = 4000;
2641 goto set_itr_now;
2642 }
2643
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00002644 adapter->tx_itr = e1000_update_itr(adapter, adapter->tx_itr,
2645 adapter->total_tx_packets,
2646 adapter->total_tx_bytes);
Jesse Brandeburg2b653262006-12-15 10:30:44 +01002647 /* conservative mode (itr 3) eliminates the lowest_latency setting */
2648 if (adapter->itr_setting == 3 && adapter->tx_itr == lowest_latency)
2649 adapter->tx_itr = low_latency;
2650
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00002651 adapter->rx_itr = e1000_update_itr(adapter, adapter->rx_itr,
2652 adapter->total_rx_packets,
2653 adapter->total_rx_bytes);
Jesse Brandeburg2b653262006-12-15 10:30:44 +01002654 /* conservative mode (itr 3) eliminates the lowest_latency setting */
2655 if (adapter->itr_setting == 3 && adapter->rx_itr == lowest_latency)
2656 adapter->rx_itr = low_latency;
Jesse Brandeburg835bb122006-11-01 08:48:13 -08002657
2658 current_itr = max(adapter->rx_itr, adapter->tx_itr);
2659
Jesse Brandeburg835bb122006-11-01 08:48:13 -08002660 switch (current_itr) {
2661 /* counts and packets in update_itr are dependent on these numbers */
2662 case lowest_latency:
2663 new_itr = 70000;
2664 break;
2665 case low_latency:
2666 new_itr = 20000; /* aka hwitr = ~200 */
2667 break;
2668 case bulk_latency:
2669 new_itr = 4000;
2670 break;
2671 default:
2672 break;
2673 }
2674
2675set_itr_now:
2676 if (new_itr != adapter->itr) {
2677 /* this attempts to bias the interrupt rate towards Bulk
2678 * by adding intermediate steps when interrupt rate is
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00002679 * increasing
2680 */
Jesse Brandeburg835bb122006-11-01 08:48:13 -08002681 new_itr = new_itr > adapter->itr ?
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00002682 min(adapter->itr + (new_itr >> 2), new_itr) :
2683 new_itr;
Jesse Brandeburg835bb122006-11-01 08:48:13 -08002684 adapter->itr = new_itr;
Joe Perches1dc32912008-07-11 15:17:08 -07002685 ew32(ITR, 1000000000 / (new_itr * 256));
Jesse Brandeburg835bb122006-11-01 08:48:13 -08002686 }
Jesse Brandeburg835bb122006-11-01 08:48:13 -08002687}
2688
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689#define E1000_TX_FLAGS_CSUM 0x00000001
2690#define E1000_TX_FLAGS_VLAN 0x00000002
2691#define E1000_TX_FLAGS_TSO 0x00000004
Malli Chilakala2d7edb92005-04-28 19:43:52 -07002692#define E1000_TX_FLAGS_IPV4 0x00000008
Ben Greear11a78dc2012-02-11 15:40:01 +00002693#define E1000_TX_FLAGS_NO_FCS 0x00000010
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694#define E1000_TX_FLAGS_VLAN_MASK 0xffff0000
2695#define E1000_TX_FLAGS_VLAN_SHIFT 16
2696
Joe Perches64798842008-07-11 15:17:02 -07002697static int e1000_tso(struct e1000_adapter *adapter,
2698 struct e1000_tx_ring *tx_ring, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 struct e1000_context_desc *context_desc;
Jeff Kirsher545c67c2006-01-12 16:50:25 -08002701 struct e1000_buffer *buffer_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 unsigned int i;
Joe Perches406874a2008-04-03 10:06:32 -07002703 u32 cmd_length = 0;
2704 u16 ipcse = 0, tucse, mss;
2705 u8 ipcss, ipcso, tucss, tucso, hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 int err;
2707
Herbert Xu89114af2006-07-08 13:34:32 -07002708 if (skb_is_gso(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 if (skb_header_cloned(skb)) {
2710 err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2711 if (err)
2712 return err;
2713 }
2714
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07002715 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
Herbert Xu79671682006-06-22 02:40:14 -07002716 mss = skb_shinfo(skb)->gso_size;
Alexey Dobriyan60828232006-05-23 14:52:21 -07002717 if (skb->protocol == htons(ETH_P_IP)) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002718 struct iphdr *iph = ip_hdr(skb);
2719 iph->tot_len = 0;
2720 iph->check = 0;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07002721 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
2722 iph->daddr, 0,
2723 IPPROTO_TCP,
2724 0);
Malli Chilakala2d7edb92005-04-28 19:43:52 -07002725 cmd_length = E1000_TXD_CMD_IP;
Arnaldo Carvalho de Meloea2ae172007-04-25 17:55:53 -07002726 ipcse = skb_transport_offset(skb) - 1;
Auke Koke15fdd02006-08-16 11:28:45 -07002727 } else if (skb->protocol == htons(ETH_P_IPV6)) {
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07002728 ipv6_hdr(skb)->payload_len = 0;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07002729 tcp_hdr(skb)->check =
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07002730 ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
2731 &ipv6_hdr(skb)->daddr,
2732 0, IPPROTO_TCP, 0);
Malli Chilakala2d7edb92005-04-28 19:43:52 -07002733 ipcse = 0;
Malli Chilakala2d7edb92005-04-28 19:43:52 -07002734 }
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03002735 ipcss = skb_network_offset(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002736 ipcso = (void *)&(ip_hdr(skb)->check) - (void *)skb->data;
Arnaldo Carvalho de Meloea2ae172007-04-25 17:55:53 -07002737 tucss = skb_transport_offset(skb);
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07002738 tucso = (void *)&(tcp_hdr(skb)->check) - (void *)skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 tucse = 0;
2740
2741 cmd_length |= (E1000_TXD_CMD_DEXT | E1000_TXD_CMD_TSE |
Malli Chilakala2d7edb92005-04-28 19:43:52 -07002742 E1000_TXD_CMD_TCP | (skb->len - (hdr_len)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002744 i = tx_ring->next_to_use;
2745 context_desc = E1000_CONTEXT_DESC(*tx_ring, i);
Jeff Kirsher545c67c2006-01-12 16:50:25 -08002746 buffer_info = &tx_ring->buffer_info[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747
2748 context_desc->lower_setup.ip_fields.ipcss = ipcss;
2749 context_desc->lower_setup.ip_fields.ipcso = ipcso;
2750 context_desc->lower_setup.ip_fields.ipcse = cpu_to_le16(ipcse);
2751 context_desc->upper_setup.tcp_fields.tucss = tucss;
2752 context_desc->upper_setup.tcp_fields.tucso = tucso;
2753 context_desc->upper_setup.tcp_fields.tucse = cpu_to_le16(tucse);
2754 context_desc->tcp_seg_setup.fields.mss = cpu_to_le16(mss);
2755 context_desc->tcp_seg_setup.fields.hdr_len = hdr_len;
2756 context_desc->cmd_and_length = cpu_to_le32(cmd_length);
2757
Jeff Kirsher545c67c2006-01-12 16:50:25 -08002758 buffer_info->time_stamp = jiffies;
Jesse Brandeburga9ebadd2006-11-01 08:47:53 -08002759 buffer_info->next_to_watch = i;
Jeff Kirsher545c67c2006-01-12 16:50:25 -08002760
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002761 if (++i == tx_ring->count) i = 0;
2762 tx_ring->next_to_use = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763
Joe Perchesc3033b02008-03-21 11:06:25 -07002764 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 }
Joe Perchesc3033b02008-03-21 11:06:25 -07002766 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767}
2768
Joe Perches64798842008-07-11 15:17:02 -07002769static bool e1000_tx_csum(struct e1000_adapter *adapter,
2770 struct e1000_tx_ring *tx_ring, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771{
2772 struct e1000_context_desc *context_desc;
Jeff Kirsher545c67c2006-01-12 16:50:25 -08002773 struct e1000_buffer *buffer_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 unsigned int i;
Joe Perches406874a2008-04-03 10:06:32 -07002775 u8 css;
Dave Graham3ed30672008-10-09 14:29:26 -07002776 u32 cmd_len = E1000_TXD_CMD_DEXT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777
Dave Graham3ed30672008-10-09 14:29:26 -07002778 if (skb->ip_summed != CHECKSUM_PARTIAL)
2779 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780
Dave Graham3ed30672008-10-09 14:29:26 -07002781 switch (skb->protocol) {
Harvey Harrison09640e62009-02-01 00:45:17 -08002782 case cpu_to_be16(ETH_P_IP):
Dave Graham3ed30672008-10-09 14:29:26 -07002783 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
2784 cmd_len |= E1000_TXD_CMD_TCP;
2785 break;
Harvey Harrison09640e62009-02-01 00:45:17 -08002786 case cpu_to_be16(ETH_P_IPV6):
Dave Graham3ed30672008-10-09 14:29:26 -07002787 /* XXX not handling all IPV6 headers */
2788 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
2789 cmd_len |= E1000_TXD_CMD_TCP;
2790 break;
2791 default:
2792 if (unlikely(net_ratelimit()))
Emil Tantilovfeb8f472010-07-26 23:37:21 -07002793 e_warn(drv, "checksum_partial proto=%x!\n",
2794 skb->protocol);
Dave Graham3ed30672008-10-09 14:29:26 -07002795 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 }
2797
Michał Mirosław0d0b1672010-12-14 15:24:08 +00002798 css = skb_checksum_start_offset(skb);
Dave Graham3ed30672008-10-09 14:29:26 -07002799
2800 i = tx_ring->next_to_use;
2801 buffer_info = &tx_ring->buffer_info[i];
2802 context_desc = E1000_CONTEXT_DESC(*tx_ring, i);
2803
2804 context_desc->lower_setup.ip_config = 0;
2805 context_desc->upper_setup.tcp_fields.tucss = css;
2806 context_desc->upper_setup.tcp_fields.tucso =
2807 css + skb->csum_offset;
2808 context_desc->upper_setup.tcp_fields.tucse = 0;
2809 context_desc->tcp_seg_setup.data = 0;
2810 context_desc->cmd_and_length = cpu_to_le32(cmd_len);
2811
2812 buffer_info->time_stamp = jiffies;
2813 buffer_info->next_to_watch = i;
2814
2815 if (unlikely(++i == tx_ring->count)) i = 0;
2816 tx_ring->next_to_use = i;
2817
2818 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819}
2820
2821#define E1000_MAX_TXD_PWR 12
2822#define E1000_MAX_DATA_PER_TXD (1<<E1000_MAX_TXD_PWR)
2823
Joe Perches64798842008-07-11 15:17:02 -07002824static int e1000_tx_map(struct e1000_adapter *adapter,
2825 struct e1000_tx_ring *tx_ring,
2826 struct sk_buff *skb, unsigned int first,
2827 unsigned int max_per_txd, unsigned int nr_frags,
2828 unsigned int mss)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829{
Joe Perches1dc32912008-07-11 15:17:08 -07002830 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck602c0552009-12-02 16:46:00 +00002831 struct pci_dev *pdev = adapter->pdev;
Alexander Duyck37e73df2009-03-25 21:58:45 +00002832 struct e1000_buffer *buffer_info;
Jesse Brandeburgd20b6062009-03-02 16:03:21 -08002833 unsigned int len = skb_headlen(skb);
Alexander Duyck602c0552009-12-02 16:46:00 +00002834 unsigned int offset = 0, size, count = 0, i;
Dean Nelson31c15a22011-08-25 14:39:24 +00002835 unsigned int f, bytecount, segs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836
2837 i = tx_ring->next_to_use;
2838
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002839 while (len) {
Alexander Duyck37e73df2009-03-25 21:58:45 +00002840 buffer_info = &tx_ring->buffer_info[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 size = min(len, max_per_txd);
Jeff Kirsherfd803242005-12-13 00:06:22 -05002842 /* Workaround for Controller erratum --
2843 * descriptor for non-tso packet in a linear SKB that follows a
2844 * tso gets written back prematurely before the data is fully
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00002845 * DMA'd to the controller
2846 */
Jeff Kirsherfd803242005-12-13 00:06:22 -05002847 if (!skb->data_len && tx_ring->last_tx_tso &&
Herbert Xu89114af2006-07-08 13:34:32 -07002848 !skb_is_gso(skb)) {
Rusty Russell3db1cd52011-12-19 13:56:45 +00002849 tx_ring->last_tx_tso = false;
Jeff Kirsherfd803242005-12-13 00:06:22 -05002850 size -= 4;
2851 }
2852
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 /* Workaround for premature desc write-backs
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00002854 * in TSO mode. Append 4-byte sentinel desc
2855 */
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002856 if (unlikely(mss && !nr_frags && size == len && size > 8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 size -= 4;
Malli Chilakala97338bd2005-04-28 19:41:46 -07002858 /* work-around for errata 10 and it applies
2859 * to all controllers in PCI-X mode
2860 * The fix is to make sure that the first descriptor of a
2861 * packet is smaller than 2048 - 16 - 16 (or 2016) bytes
2862 */
Joe Perches1dc32912008-07-11 15:17:08 -07002863 if (unlikely((hw->bus_type == e1000_bus_type_pcix) &&
Malli Chilakala97338bd2005-04-28 19:41:46 -07002864 (size > 2015) && count == 0))
2865 size = 2015;
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002866
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 /* Workaround for potential 82544 hang in PCI-X. Avoid
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00002868 * terminating buffers within evenly-aligned dwords.
2869 */
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002870 if (unlikely(adapter->pcix_82544 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 !((unsigned long)(skb->data + offset + size - 1) & 4) &&
2872 size > 4))
2873 size -= 4;
2874
2875 buffer_info->length = size;
Jesse Brandeburgcdd75492009-09-25 12:18:07 +00002876 /* set time_stamp *before* dma to help avoid a possible race */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 buffer_info->time_stamp = jiffies;
Alexander Duyck602c0552009-12-02 16:46:00 +00002878 buffer_info->mapped_as_page = false;
Nick Nunleyb16f53b2010-04-27 13:08:45 +00002879 buffer_info->dma = dma_map_single(&pdev->dev,
2880 skb->data + offset,
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00002881 size, DMA_TO_DEVICE);
Nick Nunleyb16f53b2010-04-27 13:08:45 +00002882 if (dma_mapping_error(&pdev->dev, buffer_info->dma))
Alexander Duyck602c0552009-12-02 16:46:00 +00002883 goto dma_error;
Jesse Brandeburga9ebadd2006-11-01 08:47:53 -08002884 buffer_info->next_to_watch = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885
2886 len -= size;
2887 offset += size;
2888 count++;
Alexander Duyck37e73df2009-03-25 21:58:45 +00002889 if (len) {
2890 i++;
2891 if (unlikely(i == tx_ring->count))
2892 i = 0;
2893 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 }
2895
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002896 for (f = 0; f < nr_frags; f++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00002897 const struct skb_frag_struct *frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898
2899 frag = &skb_shinfo(skb)->frags[f];
Eric Dumazet9e903e02011-10-18 21:00:24 +00002900 len = skb_frag_size(frag);
Ian Campbell877749b2011-08-29 23:18:26 +00002901 offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002903 while (len) {
Ian Campbell877749b2011-08-29 23:18:26 +00002904 unsigned long bufend;
Alexander Duyck37e73df2009-03-25 21:58:45 +00002905 i++;
2906 if (unlikely(i == tx_ring->count))
2907 i = 0;
2908
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 buffer_info = &tx_ring->buffer_info[i];
2910 size = min(len, max_per_txd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 /* Workaround for premature desc write-backs
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00002912 * in TSO mode. Append 4-byte sentinel desc
2913 */
2914 if (unlikely(mss && f == (nr_frags-1) &&
2915 size == len && size > 8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 size -= 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 /* Workaround for potential 82544 hang in PCI-X.
2918 * Avoid terminating buffers within evenly-aligned
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00002919 * dwords.
2920 */
Ian Campbell877749b2011-08-29 23:18:26 +00002921 bufend = (unsigned long)
2922 page_to_phys(skb_frag_page(frag));
2923 bufend += offset + size - 1;
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002924 if (unlikely(adapter->pcix_82544 &&
Ian Campbell877749b2011-08-29 23:18:26 +00002925 !(bufend & 4) &&
2926 size > 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 size -= 4;
2928
2929 buffer_info->length = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 buffer_info->time_stamp = jiffies;
Alexander Duyck602c0552009-12-02 16:46:00 +00002931 buffer_info->mapped_as_page = true;
Ian Campbell877749b2011-08-29 23:18:26 +00002932 buffer_info->dma = skb_frag_dma_map(&pdev->dev, frag,
2933 offset, size, DMA_TO_DEVICE);
Nick Nunleyb16f53b2010-04-27 13:08:45 +00002934 if (dma_mapping_error(&pdev->dev, buffer_info->dma))
Alexander Duyck602c0552009-12-02 16:46:00 +00002935 goto dma_error;
Jesse Brandeburga9ebadd2006-11-01 08:47:53 -08002936 buffer_info->next_to_watch = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937
2938 len -= size;
2939 offset += size;
2940 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 }
2942 }
2943
Dean Nelson31c15a22011-08-25 14:39:24 +00002944 segs = skb_shinfo(skb)->gso_segs ?: 1;
2945 /* multiply data chunks by size of headers */
2946 bytecount = ((segs - 1) * skb_headlen(skb)) + skb->len;
2947
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 tx_ring->buffer_info[i].skb = skb;
Dean Nelson31c15a22011-08-25 14:39:24 +00002949 tx_ring->buffer_info[i].segs = segs;
2950 tx_ring->buffer_info[i].bytecount = bytecount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 tx_ring->buffer_info[first].next_to_watch = i;
2952
2953 return count;
Alexander Duyck602c0552009-12-02 16:46:00 +00002954
2955dma_error:
2956 dev_err(&pdev->dev, "TX DMA map failed\n");
2957 buffer_info->dma = 0;
Roel Kluinc1fa3472010-01-19 14:21:45 +00002958 if (count)
Alexander Duyck602c0552009-12-02 16:46:00 +00002959 count--;
Roel Kluinc1fa3472010-01-19 14:21:45 +00002960
2961 while (count--) {
2962 if (i==0)
Alexander Duyck602c0552009-12-02 16:46:00 +00002963 i += tx_ring->count;
Roel Kluinc1fa3472010-01-19 14:21:45 +00002964 i--;
Alexander Duyck602c0552009-12-02 16:46:00 +00002965 buffer_info = &tx_ring->buffer_info[i];
2966 e1000_unmap_and_free_tx_resource(adapter, buffer_info);
2967 }
2968
2969 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970}
2971
Joe Perches64798842008-07-11 15:17:02 -07002972static void e1000_tx_queue(struct e1000_adapter *adapter,
2973 struct e1000_tx_ring *tx_ring, int tx_flags,
2974 int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975{
Joe Perches1dc32912008-07-11 15:17:08 -07002976 struct e1000_hw *hw = &adapter->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 struct e1000_tx_desc *tx_desc = NULL;
2978 struct e1000_buffer *buffer_info;
Joe Perches406874a2008-04-03 10:06:32 -07002979 u32 txd_upper = 0, txd_lower = E1000_TXD_CMD_IFCS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 unsigned int i;
2981
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002982 if (likely(tx_flags & E1000_TX_FLAGS_TSO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 txd_lower |= E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D |
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00002984 E1000_TXD_CMD_TSE;
Malli Chilakala2d7edb92005-04-28 19:43:52 -07002985 txd_upper |= E1000_TXD_POPTS_TXSM << 8;
2986
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002987 if (likely(tx_flags & E1000_TX_FLAGS_IPV4))
Malli Chilakala2d7edb92005-04-28 19:43:52 -07002988 txd_upper |= E1000_TXD_POPTS_IXSM << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 }
2990
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002991 if (likely(tx_flags & E1000_TX_FLAGS_CSUM)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 txd_lower |= E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D;
2993 txd_upper |= E1000_TXD_POPTS_TXSM << 8;
2994 }
2995
Jesse Brandeburg96838a42006-01-18 13:01:39 -08002996 if (unlikely(tx_flags & E1000_TX_FLAGS_VLAN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 txd_lower |= E1000_TXD_CMD_VLE;
2998 txd_upper |= (tx_flags & E1000_TX_FLAGS_VLAN_MASK);
2999 }
3000
Ben Greear11a78dc2012-02-11 15:40:01 +00003001 if (unlikely(tx_flags & E1000_TX_FLAGS_NO_FCS))
3002 txd_lower &= ~(E1000_TXD_CMD_IFCS);
3003
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 i = tx_ring->next_to_use;
3005
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003006 while (count--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 buffer_info = &tx_ring->buffer_info[i];
3008 tx_desc = E1000_TX_DESC(*tx_ring, i);
3009 tx_desc->buffer_addr = cpu_to_le64(buffer_info->dma);
3010 tx_desc->lower.data =
3011 cpu_to_le32(txd_lower | buffer_info->length);
3012 tx_desc->upper.data = cpu_to_le32(txd_upper);
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003013 if (unlikely(++i == tx_ring->count)) i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 }
3015
3016 tx_desc->lower.data |= cpu_to_le32(adapter->txd_cmd);
3017
Ben Greear11a78dc2012-02-11 15:40:01 +00003018 /* txd_cmd re-enables FCS, so we'll re-disable it here as desired. */
3019 if (unlikely(tx_flags & E1000_TX_FLAGS_NO_FCS))
3020 tx_desc->lower.data &= ~(cpu_to_le32(E1000_TXD_CMD_IFCS));
3021
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 /* Force memory writes to complete before letting h/w
3023 * know there are new descriptors to fetch. (Only
3024 * applicable for weak-ordered memory model archs,
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00003025 * such as IA-64).
3026 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 wmb();
3028
3029 tx_ring->next_to_use = i;
Joe Perches1dc32912008-07-11 15:17:08 -07003030 writel(i, hw->hw_addr + tx_ring->tdt);
Jesse Brandeburg2ce90472006-11-01 08:47:42 -08003031 /* we need this if more than one processor can write to our tail
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00003032 * at a time, it synchronizes IO on IA64/Altix systems
3033 */
Jesse Brandeburg2ce90472006-11-01 08:47:42 -08003034 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035}
3036
Ben Hutchings1aa8b472012-07-10 10:56:59 +00003037/* 82547 workaround to avoid controller hang in half-duplex environment.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 * The workaround is to avoid queuing a large packet that would span
3039 * the internal Tx FIFO ring boundary by notifying the stack to resend
3040 * the packet at a later time. This gives the Tx FIFO an opportunity to
3041 * flush all packets. When that occurs, we reset the Tx FIFO pointers
3042 * to the beginning of the Tx FIFO.
Ben Hutchings1aa8b472012-07-10 10:56:59 +00003043 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044
3045#define E1000_FIFO_HDR 0x10
3046#define E1000_82547_PAD_LEN 0x3E0
3047
Joe Perches64798842008-07-11 15:17:02 -07003048static int e1000_82547_fifo_workaround(struct e1000_adapter *adapter,
3049 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050{
Joe Perches406874a2008-04-03 10:06:32 -07003051 u32 fifo_space = adapter->tx_fifo_size - adapter->tx_fifo_head;
3052 u32 skb_fifo_len = skb->len + E1000_FIFO_HDR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053
Milind Arun Choudhary9099cfb2007-04-27 13:55:29 -07003054 skb_fifo_len = ALIGN(skb_fifo_len, E1000_FIFO_HDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003056 if (adapter->link_duplex != HALF_DUPLEX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 goto no_fifo_stall_required;
3058
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003059 if (atomic_read(&adapter->tx_fifo_stall))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 return 1;
3061
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003062 if (skb_fifo_len >= (E1000_82547_PAD_LEN + fifo_space)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 atomic_set(&adapter->tx_fifo_stall, 1);
3064 return 1;
3065 }
3066
3067no_fifo_stall_required:
3068 adapter->tx_fifo_head += skb_fifo_len;
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003069 if (adapter->tx_fifo_head >= adapter->tx_fifo_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 adapter->tx_fifo_head -= adapter->tx_fifo_size;
3071 return 0;
3072}
3073
Jesse Brandeburg65c79732006-09-27 12:53:48 -07003074static int __e1000_maybe_stop_tx(struct net_device *netdev, int size)
3075{
3076 struct e1000_adapter *adapter = netdev_priv(netdev);
3077 struct e1000_tx_ring *tx_ring = adapter->tx_ring;
3078
3079 netif_stop_queue(netdev);
3080 /* Herbert's original patch had:
3081 * smp_mb__after_netif_stop_queue();
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00003082 * but since that doesn't exist yet, just open code it.
3083 */
Jesse Brandeburg65c79732006-09-27 12:53:48 -07003084 smp_mb();
3085
3086 /* We need to check again in a case another CPU has just
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00003087 * made room available.
3088 */
Jesse Brandeburg65c79732006-09-27 12:53:48 -07003089 if (likely(E1000_DESC_UNUSED(tx_ring) < size))
3090 return -EBUSY;
3091
3092 /* A reprieve! */
3093 netif_start_queue(netdev);
Jesse Brandeburgfcfb1222006-11-01 08:47:59 -08003094 ++adapter->restart_queue;
Jesse Brandeburg65c79732006-09-27 12:53:48 -07003095 return 0;
3096}
3097
3098static int e1000_maybe_stop_tx(struct net_device *netdev,
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00003099 struct e1000_tx_ring *tx_ring, int size)
Jesse Brandeburg65c79732006-09-27 12:53:48 -07003100{
3101 if (likely(E1000_DESC_UNUSED(tx_ring) >= size))
3102 return 0;
3103 return __e1000_maybe_stop_tx(netdev, size);
3104}
3105
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106#define TXD_USE_COUNT(S, X) (((S) >> (X)) + 1 )
Stephen Hemminger3b29a562009-08-31 19:50:55 +00003107static netdev_tx_t e1000_xmit_frame(struct sk_buff *skb,
3108 struct net_device *netdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109{
Malli Chilakala60490fe2005-06-17 17:41:45 -07003110 struct e1000_adapter *adapter = netdev_priv(netdev);
Joe Perches1dc32912008-07-11 15:17:08 -07003111 struct e1000_hw *hw = &adapter->hw;
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003112 struct e1000_tx_ring *tx_ring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 unsigned int first, max_per_txd = E1000_MAX_DATA_PER_TXD;
3114 unsigned int max_txd_pwr = E1000_MAX_TXD_PWR;
3115 unsigned int tx_flags = 0;
Eric Dumazete743d312010-04-14 15:59:40 -07003116 unsigned int len = skb_headlen(skb);
Krishna Kumar6d1e3aa2007-10-05 14:15:16 -07003117 unsigned int nr_frags;
3118 unsigned int mss;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 int count = 0;
Auke Kok76c224b2006-05-23 13:36:06 -07003120 int tso;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 unsigned int f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00003123 /* This goes back to the question of how to logically map a Tx queue
Jesse Brandeburg65c79732006-09-27 12:53:48 -07003124 * to a flow. Right now, performance is impacted slightly negatively
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00003125 * if using multiple Tx queues. If the stack breaks away from a
3126 * single qdisc implementation, we can look at this again.
3127 */
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003128 tx_ring = adapter->tx_ring;
Mallikarjuna R Chilakala24025e4ec2005-10-04 07:03:23 -04003129
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003130 if (unlikely(skb->len <= 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 dev_kfree_skb_any(skb);
3132 return NETDEV_TX_OK;
3133 }
3134
Tushar Dave59d86c72012-09-15 10:16:57 +00003135 /* On PCI/PCI-X HW, if packet size is less than ETH_ZLEN,
3136 * packets may get corrupted during padding by HW.
3137 * To WA this issue, pad all small packets manually.
3138 */
3139 if (skb->len < ETH_ZLEN) {
3140 if (skb_pad(skb, ETH_ZLEN - skb->len))
3141 return NETDEV_TX_OK;
3142 skb->len = ETH_ZLEN;
3143 skb_set_tail_pointer(skb, ETH_ZLEN);
3144 }
3145
Herbert Xu79671682006-06-22 02:40:14 -07003146 mss = skb_shinfo(skb)->gso_size;
Auke Kok76c224b2006-05-23 13:36:06 -07003147 /* The controller does a simple calculation to
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 * make sure there is enough room in the FIFO before
3149 * initiating the DMA for each buffer. The calc is:
3150 * 4 = ceil(buffer len/mss). To make sure we don't
3151 * overrun the FIFO, adjust the max buffer len if mss
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00003152 * drops.
3153 */
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003154 if (mss) {
Joe Perches406874a2008-04-03 10:06:32 -07003155 u8 hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 max_per_txd = min(mss << 2, max_per_txd);
3157 max_txd_pwr = fls(max_per_txd) - 1;
Jeff Kirsher9a3056d2006-01-12 16:50:23 -08003158
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07003159 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
Krishna Kumar6d1e3aa2007-10-05 14:15:16 -07003160 if (skb->data_len && hdr_len == len) {
Joe Perches1dc32912008-07-11 15:17:08 -07003161 switch (hw->mac_type) {
Jeff Kirsher9f687882006-03-02 18:20:17 -08003162 unsigned int pull_size;
Herbert Xu683a2aa2006-12-16 12:04:33 +11003163 case e1000_82544:
3164 /* Make sure we have room to chop off 4 bytes,
3165 * and that the end alignment will work out to
3166 * this hardware's requirements
3167 * NOTE: this is a TSO only workaround
3168 * if end byte alignment not correct move us
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00003169 * into the next dword
3170 */
3171 if ((unsigned long)(skb_tail_pointer(skb) - 1)
3172 & 4)
Herbert Xu683a2aa2006-12-16 12:04:33 +11003173 break;
3174 /* fall through */
Jeff Kirsher9f687882006-03-02 18:20:17 -08003175 pull_size = min((unsigned int)4, skb->data_len);
3176 if (!__pskb_pull_tail(skb, pull_size)) {
Emil Tantilovfeb8f472010-07-26 23:37:21 -07003177 e_err(drv, "__pskb_pull_tail "
3178 "failed.\n");
Jeff Kirsher9f687882006-03-02 18:20:17 -08003179 dev_kfree_skb_any(skb);
Jeff Garzik749dfc702006-03-11 13:35:31 -05003180 return NETDEV_TX_OK;
Jeff Kirsher9f687882006-03-02 18:20:17 -08003181 }
Eric Dumazete743d312010-04-14 15:59:40 -07003182 len = skb_headlen(skb);
Jeff Kirsher9f687882006-03-02 18:20:17 -08003183 break;
3184 default:
3185 /* do nothing */
3186 break;
Jeff Kirsherd74bbd32006-01-12 16:51:07 -08003187 }
Jeff Kirsher9a3056d2006-01-12 16:50:23 -08003188 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 }
3190
Jeff Kirsher9a3056d2006-01-12 16:50:23 -08003191 /* reserve a descriptor for the offload context */
Patrick McHardy84fa7932006-08-29 16:44:56 -07003192 if ((mss) || (skb->ip_summed == CHECKSUM_PARTIAL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 count++;
Malli Chilakala26483452005-04-28 19:44:46 -07003194 count++;
Jeff Kirsherfd803242005-12-13 00:06:22 -05003195
Jeff Kirsherfd803242005-12-13 00:06:22 -05003196 /* Controller Erratum workaround */
Herbert Xu89114af2006-07-08 13:34:32 -07003197 if (!skb->data_len && tx_ring->last_tx_tso && !skb_is_gso(skb))
Jeff Kirsherfd803242005-12-13 00:06:22 -05003198 count++;
Jeff Kirsherfd803242005-12-13 00:06:22 -05003199
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 count += TXD_USE_COUNT(len, max_txd_pwr);
3201
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003202 if (adapter->pcix_82544)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 count++;
3204
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003205 /* work-around for errata 10 and it applies to all controllers
Malli Chilakala97338bd2005-04-28 19:41:46 -07003206 * in PCI-X mode, so add one more descriptor to the count
3207 */
Joe Perches1dc32912008-07-11 15:17:08 -07003208 if (unlikely((hw->bus_type == e1000_bus_type_pcix) &&
Malli Chilakala97338bd2005-04-28 19:41:46 -07003209 (len > 2015)))
3210 count++;
3211
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 nr_frags = skb_shinfo(skb)->nr_frags;
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003213 for (f = 0; f < nr_frags; f++)
Eric Dumazet9e903e02011-10-18 21:00:24 +00003214 count += TXD_USE_COUNT(skb_frag_size(&skb_shinfo(skb)->frags[f]),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 max_txd_pwr);
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003216 if (adapter->pcix_82544)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 count += nr_frags;
3218
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 /* need: count + 2 desc gap to keep tail from touching
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00003220 * head, otherwise try next time
3221 */
Alexander Duyck80179432009-01-21 14:42:47 -08003222 if (unlikely(e1000_maybe_stop_tx(netdev, tx_ring, count + 2)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224
Jesse Brandeburga4010af2011-10-05 07:24:41 +00003225 if (unlikely((hw->mac_type == e1000_82547) &&
3226 (e1000_82547_fifo_workaround(adapter, skb)))) {
3227 netif_stop_queue(netdev);
3228 if (!test_bit(__E1000_DOWN, &adapter->flags))
3229 schedule_delayed_work(&adapter->fifo_stall_task, 1);
3230 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 }
3232
Jiri Pirko5622e402011-07-21 03:26:31 +00003233 if (vlan_tx_tag_present(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 tx_flags |= E1000_TX_FLAGS_VLAN;
3235 tx_flags |= (vlan_tx_tag_get(skb) << E1000_TX_FLAGS_VLAN_SHIFT);
3236 }
3237
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003238 first = tx_ring->next_to_use;
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003239
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003240 tso = e1000_tso(adapter, tx_ring, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 if (tso < 0) {
3242 dev_kfree_skb_any(skb);
3243 return NETDEV_TX_OK;
3244 }
3245
Jeff Kirsherfd803242005-12-13 00:06:22 -05003246 if (likely(tso)) {
Jesse Brandeburg8fce4732009-09-25 12:18:41 +00003247 if (likely(hw->mac_type != e1000_82544))
Rusty Russell3db1cd52011-12-19 13:56:45 +00003248 tx_ring->last_tx_tso = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249 tx_flags |= E1000_TX_FLAGS_TSO;
Jeff Kirsherfd803242005-12-13 00:06:22 -05003250 } else if (likely(e1000_tx_csum(adapter, tx_ring, skb)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 tx_flags |= E1000_TX_FLAGS_CSUM;
3252
Alexey Dobriyan60828232006-05-23 14:52:21 -07003253 if (likely(skb->protocol == htons(ETH_P_IP)))
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003254 tx_flags |= E1000_TX_FLAGS_IPV4;
3255
Ben Greear11a78dc2012-02-11 15:40:01 +00003256 if (unlikely(skb->no_fcs))
3257 tx_flags |= E1000_TX_FLAGS_NO_FCS;
3258
Alexander Duyck37e73df2009-03-25 21:58:45 +00003259 count = e1000_tx_map(adapter, tx_ring, skb, first, max_per_txd,
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00003260 nr_frags, mss);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261
Alexander Duyck37e73df2009-03-25 21:58:45 +00003262 if (count) {
Otto Estuardo Solares Cabrera2f66fd32012-08-24 08:04:58 +00003263 netdev_sent_queue(netdev, skb->len);
Willem de Bruijneab467f2012-04-27 09:04:04 +00003264 skb_tx_timestamp(skb);
3265
Alexander Duyck37e73df2009-03-25 21:58:45 +00003266 e1000_tx_queue(adapter, tx_ring, tx_flags, count);
Alexander Duyck37e73df2009-03-25 21:58:45 +00003267 /* Make sure there is space in the ring for the next send. */
3268 e1000_maybe_stop_tx(netdev, tx_ring, MAX_SKB_FRAGS + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269
Alexander Duyck37e73df2009-03-25 21:58:45 +00003270 } else {
3271 dev_kfree_skb_any(skb);
3272 tx_ring->buffer_info[first].time_stamp = 0;
3273 tx_ring->next_to_use = first;
3274 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 return NETDEV_TX_OK;
3277}
3278
Tushar Daveb04e36b2012-01-27 09:00:46 +00003279#define NUM_REGS 38 /* 1 based count */
3280static void e1000_regdump(struct e1000_adapter *adapter)
3281{
3282 struct e1000_hw *hw = &adapter->hw;
3283 u32 regs[NUM_REGS];
3284 u32 *regs_buff = regs;
3285 int i = 0;
3286
Tushar Davee29b5d82012-02-10 08:06:36 +00003287 static const char * const reg_name[] = {
3288 "CTRL", "STATUS",
3289 "RCTL", "RDLEN", "RDH", "RDT", "RDTR",
3290 "TCTL", "TDBAL", "TDBAH", "TDLEN", "TDH", "TDT",
3291 "TIDV", "TXDCTL", "TADV", "TARC0",
3292 "TDBAL1", "TDBAH1", "TDLEN1", "TDH1", "TDT1",
3293 "TXDCTL1", "TARC1",
3294 "CTRL_EXT", "ERT", "RDBAL", "RDBAH",
3295 "TDFH", "TDFT", "TDFHS", "TDFTS", "TDFPC",
3296 "RDFH", "RDFT", "RDFHS", "RDFTS", "RDFPC"
Tushar Daveb04e36b2012-01-27 09:00:46 +00003297 };
3298
3299 regs_buff[0] = er32(CTRL);
3300 regs_buff[1] = er32(STATUS);
3301
3302 regs_buff[2] = er32(RCTL);
3303 regs_buff[3] = er32(RDLEN);
3304 regs_buff[4] = er32(RDH);
3305 regs_buff[5] = er32(RDT);
3306 regs_buff[6] = er32(RDTR);
3307
3308 regs_buff[7] = er32(TCTL);
3309 regs_buff[8] = er32(TDBAL);
3310 regs_buff[9] = er32(TDBAH);
3311 regs_buff[10] = er32(TDLEN);
3312 regs_buff[11] = er32(TDH);
3313 regs_buff[12] = er32(TDT);
3314 regs_buff[13] = er32(TIDV);
3315 regs_buff[14] = er32(TXDCTL);
3316 regs_buff[15] = er32(TADV);
3317 regs_buff[16] = er32(TARC0);
3318
3319 regs_buff[17] = er32(TDBAL1);
3320 regs_buff[18] = er32(TDBAH1);
3321 regs_buff[19] = er32(TDLEN1);
3322 regs_buff[20] = er32(TDH1);
3323 regs_buff[21] = er32(TDT1);
3324 regs_buff[22] = er32(TXDCTL1);
3325 regs_buff[23] = er32(TARC1);
3326 regs_buff[24] = er32(CTRL_EXT);
3327 regs_buff[25] = er32(ERT);
3328 regs_buff[26] = er32(RDBAL0);
3329 regs_buff[27] = er32(RDBAH0);
3330 regs_buff[28] = er32(TDFH);
3331 regs_buff[29] = er32(TDFT);
3332 regs_buff[30] = er32(TDFHS);
3333 regs_buff[31] = er32(TDFTS);
3334 regs_buff[32] = er32(TDFPC);
3335 regs_buff[33] = er32(RDFH);
3336 regs_buff[34] = er32(RDFT);
3337 regs_buff[35] = er32(RDFHS);
3338 regs_buff[36] = er32(RDFTS);
3339 regs_buff[37] = er32(RDFPC);
3340
3341 pr_info("Register dump\n");
Tushar Davee29b5d82012-02-10 08:06:36 +00003342 for (i = 0; i < NUM_REGS; i++)
3343 pr_info("%-15s %08x\n", reg_name[i], regs_buff[i]);
Tushar Daveb04e36b2012-01-27 09:00:46 +00003344}
3345
3346/*
3347 * e1000_dump: Print registers, tx ring and rx ring
3348 */
3349static void e1000_dump(struct e1000_adapter *adapter)
3350{
3351 /* this code doesn't handle multiple rings */
3352 struct e1000_tx_ring *tx_ring = adapter->tx_ring;
3353 struct e1000_rx_ring *rx_ring = adapter->rx_ring;
3354 int i;
3355
3356 if (!netif_msg_hw(adapter))
3357 return;
3358
3359 /* Print Registers */
3360 e1000_regdump(adapter);
3361
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00003362 /* transmit dump */
Tushar Daveb04e36b2012-01-27 09:00:46 +00003363 pr_info("TX Desc ring0 dump\n");
3364
3365 /* Transmit Descriptor Formats - DEXT[29] is 0 (Legacy) or 1 (Extended)
3366 *
3367 * Legacy Transmit Descriptor
3368 * +--------------------------------------------------------------+
3369 * 0 | Buffer Address [63:0] (Reserved on Write Back) |
3370 * +--------------------------------------------------------------+
3371 * 8 | Special | CSS | Status | CMD | CSO | Length |
3372 * +--------------------------------------------------------------+
3373 * 63 48 47 36 35 32 31 24 23 16 15 0
3374 *
3375 * Extended Context Descriptor (DTYP=0x0) for TSO or checksum offload
3376 * 63 48 47 40 39 32 31 16 15 8 7 0
3377 * +----------------------------------------------------------------+
3378 * 0 | TUCSE | TUCS0 | TUCSS | IPCSE | IPCS0 | IPCSS |
3379 * +----------------------------------------------------------------+
3380 * 8 | MSS | HDRLEN | RSV | STA | TUCMD | DTYP | PAYLEN |
3381 * +----------------------------------------------------------------+
3382 * 63 48 47 40 39 36 35 32 31 24 23 20 19 0
3383 *
3384 * Extended Data Descriptor (DTYP=0x1)
3385 * +----------------------------------------------------------------+
3386 * 0 | Buffer Address [63:0] |
3387 * +----------------------------------------------------------------+
3388 * 8 | VLAN tag | POPTS | Rsvd | Status | Command | DTYP | DTALEN |
3389 * +----------------------------------------------------------------+
3390 * 63 48 47 40 39 36 35 32 31 24 23 20 19 0
3391 */
Tushar Davee29b5d82012-02-10 08:06:36 +00003392 pr_info("Tc[desc] [Ce CoCsIpceCoS] [MssHlRSCm0Plen] [bi->dma ] leng ntw timestmp bi->skb\n");
3393 pr_info("Td[desc] [address 63:0 ] [VlaPoRSCm1Dlen] [bi->dma ] leng ntw timestmp bi->skb\n");
Tushar Daveb04e36b2012-01-27 09:00:46 +00003394
3395 if (!netif_msg_tx_done(adapter))
3396 goto rx_ring_summary;
3397
3398 for (i = 0; tx_ring->desc && (i < tx_ring->count); i++) {
3399 struct e1000_tx_desc *tx_desc = E1000_TX_DESC(*tx_ring, i);
3400 struct e1000_buffer *buffer_info = &tx_ring->buffer_info[i];
Andrei Emeltchenkodd7f5c92012-03-25 17:49:25 +00003401 struct my_u { __le64 a; __le64 b; };
Tushar Daveb04e36b2012-01-27 09:00:46 +00003402 struct my_u *u = (struct my_u *)tx_desc;
Tushar Davee29b5d82012-02-10 08:06:36 +00003403 const char *type;
3404
Tushar Daveb04e36b2012-01-27 09:00:46 +00003405 if (i == tx_ring->next_to_use && i == tx_ring->next_to_clean)
Tushar Davee29b5d82012-02-10 08:06:36 +00003406 type = "NTC/U";
Tushar Daveb04e36b2012-01-27 09:00:46 +00003407 else if (i == tx_ring->next_to_use)
Tushar Davee29b5d82012-02-10 08:06:36 +00003408 type = "NTU";
Tushar Daveb04e36b2012-01-27 09:00:46 +00003409 else if (i == tx_ring->next_to_clean)
Tushar Davee29b5d82012-02-10 08:06:36 +00003410 type = "NTC";
Tushar Daveb04e36b2012-01-27 09:00:46 +00003411 else
Tushar Davee29b5d82012-02-10 08:06:36 +00003412 type = "";
Tushar Daveb04e36b2012-01-27 09:00:46 +00003413
Tushar Davee29b5d82012-02-10 08:06:36 +00003414 pr_info("T%c[0x%03X] %016llX %016llX %016llX %04X %3X %016llX %p %s\n",
3415 ((le64_to_cpu(u->b) & (1<<20)) ? 'd' : 'c'), i,
3416 le64_to_cpu(u->a), le64_to_cpu(u->b),
3417 (u64)buffer_info->dma, buffer_info->length,
3418 buffer_info->next_to_watch,
3419 (u64)buffer_info->time_stamp, buffer_info->skb, type);
Tushar Daveb04e36b2012-01-27 09:00:46 +00003420 }
3421
3422rx_ring_summary:
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00003423 /* receive dump */
Tushar Daveb04e36b2012-01-27 09:00:46 +00003424 pr_info("\nRX Desc ring dump\n");
3425
3426 /* Legacy Receive Descriptor Format
3427 *
3428 * +-----------------------------------------------------+
3429 * | Buffer Address [63:0] |
3430 * +-----------------------------------------------------+
3431 * | VLAN Tag | Errors | Status 0 | Packet csum | Length |
3432 * +-----------------------------------------------------+
3433 * 63 48 47 40 39 32 31 16 15 0
3434 */
Tushar Davee29b5d82012-02-10 08:06:36 +00003435 pr_info("R[desc] [address 63:0 ] [vl er S cks ln] [bi->dma ] [bi->skb]\n");
Tushar Daveb04e36b2012-01-27 09:00:46 +00003436
3437 if (!netif_msg_rx_status(adapter))
3438 goto exit;
3439
3440 for (i = 0; rx_ring->desc && (i < rx_ring->count); i++) {
3441 struct e1000_rx_desc *rx_desc = E1000_RX_DESC(*rx_ring, i);
3442 struct e1000_buffer *buffer_info = &rx_ring->buffer_info[i];
Andrei Emeltchenkodd7f5c92012-03-25 17:49:25 +00003443 struct my_u { __le64 a; __le64 b; };
Tushar Daveb04e36b2012-01-27 09:00:46 +00003444 struct my_u *u = (struct my_u *)rx_desc;
Tushar Davee29b5d82012-02-10 08:06:36 +00003445 const char *type;
3446
Tushar Daveb04e36b2012-01-27 09:00:46 +00003447 if (i == rx_ring->next_to_use)
Tushar Davee29b5d82012-02-10 08:06:36 +00003448 type = "NTU";
Tushar Daveb04e36b2012-01-27 09:00:46 +00003449 else if (i == rx_ring->next_to_clean)
Tushar Davee29b5d82012-02-10 08:06:36 +00003450 type = "NTC";
Tushar Daveb04e36b2012-01-27 09:00:46 +00003451 else
Tushar Davee29b5d82012-02-10 08:06:36 +00003452 type = "";
Tushar Daveb04e36b2012-01-27 09:00:46 +00003453
Tushar Davee29b5d82012-02-10 08:06:36 +00003454 pr_info("R[0x%03X] %016llX %016llX %016llX %p %s\n",
3455 i, le64_to_cpu(u->a), le64_to_cpu(u->b),
3456 (u64)buffer_info->dma, buffer_info->skb, type);
Tushar Daveb04e36b2012-01-27 09:00:46 +00003457 } /* for */
3458
3459 /* dump the descriptor caches */
3460 /* rx */
Tushar Davee29b5d82012-02-10 08:06:36 +00003461 pr_info("Rx descriptor cache in 64bit format\n");
Tushar Daveb04e36b2012-01-27 09:00:46 +00003462 for (i = 0x6000; i <= 0x63FF ; i += 0x10) {
Tushar Davee29b5d82012-02-10 08:06:36 +00003463 pr_info("R%04X: %08X|%08X %08X|%08X\n",
3464 i,
3465 readl(adapter->hw.hw_addr + i+4),
3466 readl(adapter->hw.hw_addr + i),
3467 readl(adapter->hw.hw_addr + i+12),
3468 readl(adapter->hw.hw_addr + i+8));
Tushar Daveb04e36b2012-01-27 09:00:46 +00003469 }
3470 /* tx */
Tushar Davee29b5d82012-02-10 08:06:36 +00003471 pr_info("Tx descriptor cache in 64bit format\n");
Tushar Daveb04e36b2012-01-27 09:00:46 +00003472 for (i = 0x7000; i <= 0x73FF ; i += 0x10) {
Tushar Davee29b5d82012-02-10 08:06:36 +00003473 pr_info("T%04X: %08X|%08X %08X|%08X\n",
3474 i,
3475 readl(adapter->hw.hw_addr + i+4),
3476 readl(adapter->hw.hw_addr + i),
3477 readl(adapter->hw.hw_addr + i+12),
3478 readl(adapter->hw.hw_addr + i+8));
Tushar Daveb04e36b2012-01-27 09:00:46 +00003479 }
3480exit:
3481 return;
3482}
3483
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484/**
3485 * e1000_tx_timeout - Respond to a Tx Hang
3486 * @netdev: network interface device structure
3487 **/
Joe Perches64798842008-07-11 15:17:02 -07003488static void e1000_tx_timeout(struct net_device *netdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489{
Malli Chilakala60490fe2005-06-17 17:41:45 -07003490 struct e1000_adapter *adapter = netdev_priv(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491
3492 /* Do the reset outside of interrupt context */
Jeff Kirsher87041632006-03-02 18:21:24 -08003493 adapter->tx_timeout_count++;
3494 schedule_work(&adapter->reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495}
3496
Joe Perches64798842008-07-11 15:17:02 -07003497static void e1000_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498{
David Howells65f27f32006-11-22 14:55:48 +00003499 struct e1000_adapter *adapter =
3500 container_of(work, struct e1000_adapter, reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501
Jesse Brandeburg0ef4eed2011-10-05 07:24:51 +00003502 if (test_bit(__E1000_DOWN, &adapter->flags))
3503 return;
Tushar Daveb04e36b2012-01-27 09:00:46 +00003504 e_err(drv, "Reset adapter\n");
Jesse Brandeburg338c15e2010-09-22 18:22:42 +00003505 e1000_reinit_safe(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506}
3507
3508/**
3509 * e1000_get_stats - Get System Network Statistics
3510 * @netdev: network interface device structure
3511 *
3512 * Returns the address of the device statistics structure.
Jesse Brandeburga4010af2011-10-05 07:24:41 +00003513 * The statistics are actually updated from the watchdog.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514 **/
Joe Perches64798842008-07-11 15:17:02 -07003515static struct net_device_stats *e1000_get_stats(struct net_device *netdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516{
Jeff Kirsher6b7660c2006-01-12 16:50:35 -08003517 /* only return the current stats */
Ajit Khaparde5fe31de2009-10-07 02:42:23 +00003518 return &netdev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519}
3520
3521/**
3522 * e1000_change_mtu - Change the Maximum Transfer Unit
3523 * @netdev: network interface device structure
3524 * @new_mtu: new value for maximum frame size
3525 *
3526 * Returns 0 on success, negative on failure
3527 **/
Joe Perches64798842008-07-11 15:17:02 -07003528static int e1000_change_mtu(struct net_device *netdev, int new_mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529{
Malli Chilakala60490fe2005-06-17 17:41:45 -07003530 struct e1000_adapter *adapter = netdev_priv(netdev);
Joe Perches1dc32912008-07-11 15:17:08 -07003531 struct e1000_hw *hw = &adapter->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532 int max_frame = new_mtu + ENET_HEADER_SIZE + ETHERNET_FCS_SIZE;
3533
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003534 if ((max_frame < MINIMUM_ETHERNET_FRAME_SIZE) ||
3535 (max_frame > MAX_JUMBO_FRAME_SIZE)) {
Emil Tantilovfeb8f472010-07-26 23:37:21 -07003536 e_err(probe, "Invalid MTU setting\n");
Mallikarjuna R Chilakala868d5302005-10-04 06:58:59 -04003537 return -EINVAL;
3538 }
3539
Jeff Kirsher997f5cb2006-01-12 16:50:55 -08003540 /* Adapter-specific max frame size limits. */
Joe Perches1dc32912008-07-11 15:17:08 -07003541 switch (hw->mac_type) {
Auke Kok9e2feac2006-04-14 19:05:18 -07003542 case e1000_undefined ... e1000_82542_rev2_1:
Jesse Brandeburgb7cb8c22009-07-06 10:45:01 +00003543 if (max_frame > (ETH_FRAME_LEN + ETH_FCS_LEN)) {
Emil Tantilovfeb8f472010-07-26 23:37:21 -07003544 e_err(probe, "Jumbo Frames not supported.\n");
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003545 return -EINVAL;
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003546 }
Jeff Kirsher997f5cb2006-01-12 16:50:55 -08003547 break;
Jeff Kirsher997f5cb2006-01-12 16:50:55 -08003548 default:
3549 /* Capable of supporting up to MAX_JUMBO_FRAME_SIZE limit. */
3550 break;
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003551 }
3552
Jesse Brandeburg3d6114e2009-09-25 12:19:02 +00003553 while (test_and_set_bit(__E1000_RESETTING, &adapter->flags))
3554 msleep(1);
3555 /* e1000_down has a dependency on max_frame_size */
3556 hw->max_frame_size = max_frame;
3557 if (netif_running(netdev))
3558 e1000_down(adapter);
3559
David S. Miller87f50322006-07-31 22:39:40 -07003560 /* NOTE: netdev_alloc_skb reserves 16 bytes, and typically NET_IP_ALIGN
Auke Kok9e2feac2006-04-14 19:05:18 -07003561 * means we reserve 2 more, this pushes us to allocate from the next
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00003562 * larger slab size.
3563 * i.e. RXBUFFER_2048 --> size-4096 slab
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00003564 * however with the new *_jumbo_rx* routines, jumbo receives will use
3565 * fragmented skbs
3566 */
Jeff Kirsher997f5cb2006-01-12 16:50:55 -08003567
Jesse Brandeburg99261462010-01-22 22:56:16 +00003568 if (max_frame <= E1000_RXBUFFER_2048)
Auke Kok9e2feac2006-04-14 19:05:18 -07003569 adapter->rx_buffer_len = E1000_RXBUFFER_2048;
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00003570 else
3571#if (PAGE_SIZE >= E1000_RXBUFFER_16384)
Auke Kok9e2feac2006-04-14 19:05:18 -07003572 adapter->rx_buffer_len = E1000_RXBUFFER_16384;
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00003573#elif (PAGE_SIZE >= E1000_RXBUFFER_4096)
3574 adapter->rx_buffer_len = PAGE_SIZE;
3575#endif
Auke Kok9e2feac2006-04-14 19:05:18 -07003576
3577 /* adjust allocation if LPE protects us, and we aren't using SBP */
Joe Perches1dc32912008-07-11 15:17:08 -07003578 if (!hw->tbi_compatibility_on &&
Jesse Brandeburgb7cb8c22009-07-06 10:45:01 +00003579 ((max_frame == (ETH_FRAME_LEN + ETH_FCS_LEN)) ||
Auke Kok9e2feac2006-04-14 19:05:18 -07003580 (max_frame == MAXIMUM_ETHERNET_VLAN_SIZE)))
3581 adapter->rx_buffer_len = MAXIMUM_ETHERNET_VLAN_SIZE;
Jeff Kirsher997f5cb2006-01-12 16:50:55 -08003582
Emil Tantilov675ad472010-04-27 14:02:58 +00003583 pr_info("%s changing MTU from %d to %d\n",
3584 netdev->name, netdev->mtu, new_mtu);
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003585 netdev->mtu = new_mtu;
3586
Auke Kok2db10a02006-06-27 09:06:28 -07003587 if (netif_running(netdev))
Jesse Brandeburg3d6114e2009-09-25 12:19:02 +00003588 e1000_up(adapter);
3589 else
3590 e1000_reset(adapter);
3591
3592 clear_bit(__E1000_RESETTING, &adapter->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594 return 0;
3595}
3596
3597/**
3598 * e1000_update_stats - Update the board statistics counters
3599 * @adapter: board private structure
3600 **/
Joe Perches64798842008-07-11 15:17:02 -07003601void e1000_update_stats(struct e1000_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602{
Ajit Khaparde5fe31de2009-10-07 02:42:23 +00003603 struct net_device *netdev = adapter->netdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604 struct e1000_hw *hw = &adapter->hw;
Linas Vepstas282f33c2006-06-08 22:19:44 -07003605 struct pci_dev *pdev = adapter->pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606 unsigned long flags;
Joe Perches406874a2008-04-03 10:06:32 -07003607 u16 phy_tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608
3609#define PHY_IDLE_ERROR_COUNT_MASK 0x00FF
3610
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00003611 /* Prevent stats update while adapter is being reset, or if the pci
Linas Vepstas282f33c2006-06-08 22:19:44 -07003612 * connection is down.
3613 */
Auke Kok90267292006-06-08 09:30:24 -07003614 if (adapter->link_speed == 0)
3615 return;
Linas Vepstas81b19552006-12-12 18:29:15 -06003616 if (pci_channel_offline(pdev))
Linas Vepstas282f33c2006-06-08 22:19:44 -07003617 return;
Auke Kok90267292006-06-08 09:30:24 -07003618
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619 spin_lock_irqsave(&adapter->stats_lock, flags);
3620
Masatake YAMATO828d0552007-10-20 03:06:37 +02003621 /* these counters are modified from e1000_tbi_adjust_stats,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622 * called from the interrupt context, so they must only
3623 * be written while holding adapter->stats_lock
3624 */
3625
Joe Perches1dc32912008-07-11 15:17:08 -07003626 adapter->stats.crcerrs += er32(CRCERRS);
3627 adapter->stats.gprc += er32(GPRC);
3628 adapter->stats.gorcl += er32(GORCL);
3629 adapter->stats.gorch += er32(GORCH);
3630 adapter->stats.bprc += er32(BPRC);
3631 adapter->stats.mprc += er32(MPRC);
3632 adapter->stats.roc += er32(ROC);
Auke Kokcd94dd02006-06-27 09:08:22 -07003633
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00003634 adapter->stats.prc64 += er32(PRC64);
3635 adapter->stats.prc127 += er32(PRC127);
3636 adapter->stats.prc255 += er32(PRC255);
3637 adapter->stats.prc511 += er32(PRC511);
3638 adapter->stats.prc1023 += er32(PRC1023);
3639 adapter->stats.prc1522 += er32(PRC1522);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640
Joe Perches1dc32912008-07-11 15:17:08 -07003641 adapter->stats.symerrs += er32(SYMERRS);
3642 adapter->stats.mpc += er32(MPC);
3643 adapter->stats.scc += er32(SCC);
3644 adapter->stats.ecol += er32(ECOL);
3645 adapter->stats.mcc += er32(MCC);
3646 adapter->stats.latecol += er32(LATECOL);
3647 adapter->stats.dc += er32(DC);
3648 adapter->stats.sec += er32(SEC);
3649 adapter->stats.rlec += er32(RLEC);
3650 adapter->stats.xonrxc += er32(XONRXC);
3651 adapter->stats.xontxc += er32(XONTXC);
3652 adapter->stats.xoffrxc += er32(XOFFRXC);
3653 adapter->stats.xofftxc += er32(XOFFTXC);
3654 adapter->stats.fcruc += er32(FCRUC);
3655 adapter->stats.gptc += er32(GPTC);
3656 adapter->stats.gotcl += er32(GOTCL);
3657 adapter->stats.gotch += er32(GOTCH);
3658 adapter->stats.rnbc += er32(RNBC);
3659 adapter->stats.ruc += er32(RUC);
3660 adapter->stats.rfc += er32(RFC);
3661 adapter->stats.rjc += er32(RJC);
3662 adapter->stats.torl += er32(TORL);
3663 adapter->stats.torh += er32(TORH);
3664 adapter->stats.totl += er32(TOTL);
3665 adapter->stats.toth += er32(TOTH);
3666 adapter->stats.tpr += er32(TPR);
Auke Kokcd94dd02006-06-27 09:08:22 -07003667
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00003668 adapter->stats.ptc64 += er32(PTC64);
3669 adapter->stats.ptc127 += er32(PTC127);
3670 adapter->stats.ptc255 += er32(PTC255);
3671 adapter->stats.ptc511 += er32(PTC511);
3672 adapter->stats.ptc1023 += er32(PTC1023);
3673 adapter->stats.ptc1522 += er32(PTC1522);
Auke Kokcd94dd02006-06-27 09:08:22 -07003674
Joe Perches1dc32912008-07-11 15:17:08 -07003675 adapter->stats.mptc += er32(MPTC);
3676 adapter->stats.bptc += er32(BPTC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677
3678 /* used for adaptive IFS */
3679
Joe Perches1dc32912008-07-11 15:17:08 -07003680 hw->tx_packet_delta = er32(TPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681 adapter->stats.tpt += hw->tx_packet_delta;
Joe Perches1dc32912008-07-11 15:17:08 -07003682 hw->collision_delta = er32(COLC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683 adapter->stats.colc += hw->collision_delta;
3684
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003685 if (hw->mac_type >= e1000_82543) {
Joe Perches1dc32912008-07-11 15:17:08 -07003686 adapter->stats.algnerrc += er32(ALGNERRC);
3687 adapter->stats.rxerrc += er32(RXERRC);
3688 adapter->stats.tncrs += er32(TNCRS);
3689 adapter->stats.cexterr += er32(CEXTERR);
3690 adapter->stats.tsctc += er32(TSCTC);
3691 adapter->stats.tsctfc += er32(TSCTFC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692 }
3693
3694 /* Fill out the OS statistics structure */
Ajit Khaparde5fe31de2009-10-07 02:42:23 +00003695 netdev->stats.multicast = adapter->stats.mprc;
3696 netdev->stats.collisions = adapter->stats.colc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697
3698 /* Rx Errors */
3699
Jeff Kirsher87041632006-03-02 18:21:24 -08003700 /* RLEC on some newer hardware can be incorrect so build
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00003701 * our own version based on RUC and ROC
3702 */
Ajit Khaparde5fe31de2009-10-07 02:42:23 +00003703 netdev->stats.rx_errors = adapter->stats.rxerrc +
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704 adapter->stats.crcerrs + adapter->stats.algnerrc +
Jeff Kirsher87041632006-03-02 18:21:24 -08003705 adapter->stats.ruc + adapter->stats.roc +
3706 adapter->stats.cexterr;
Mitch Williams49559852006-09-27 12:53:37 -07003707 adapter->stats.rlerrc = adapter->stats.ruc + adapter->stats.roc;
Ajit Khaparde5fe31de2009-10-07 02:42:23 +00003708 netdev->stats.rx_length_errors = adapter->stats.rlerrc;
3709 netdev->stats.rx_crc_errors = adapter->stats.crcerrs;
3710 netdev->stats.rx_frame_errors = adapter->stats.algnerrc;
3711 netdev->stats.rx_missed_errors = adapter->stats.mpc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712
3713 /* Tx Errors */
Mitch Williams49559852006-09-27 12:53:37 -07003714 adapter->stats.txerrc = adapter->stats.ecol + adapter->stats.latecol;
Ajit Khaparde5fe31de2009-10-07 02:42:23 +00003715 netdev->stats.tx_errors = adapter->stats.txerrc;
3716 netdev->stats.tx_aborted_errors = adapter->stats.ecol;
3717 netdev->stats.tx_window_errors = adapter->stats.latecol;
3718 netdev->stats.tx_carrier_errors = adapter->stats.tncrs;
Joe Perches1dc32912008-07-11 15:17:08 -07003719 if (hw->bad_tx_carr_stats_fd &&
Jeff Garzik167fb282006-12-15 10:41:15 -05003720 adapter->link_duplex == FULL_DUPLEX) {
Ajit Khaparde5fe31de2009-10-07 02:42:23 +00003721 netdev->stats.tx_carrier_errors = 0;
Jeff Garzik167fb282006-12-15 10:41:15 -05003722 adapter->stats.tncrs = 0;
3723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724
3725 /* Tx Dropped needs to be maintained elsewhere */
3726
3727 /* Phy Stats */
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003728 if (hw->media_type == e1000_media_type_copper) {
3729 if ((adapter->link_speed == SPEED_1000) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730 (!e1000_read_phy_reg(hw, PHY_1000T_STATUS, &phy_tmp))) {
3731 phy_tmp &= PHY_IDLE_ERROR_COUNT_MASK;
3732 adapter->phy_stats.idle_errors += phy_tmp;
3733 }
3734
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003735 if ((hw->mac_type <= e1000_82546) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736 (hw->phy_type == e1000_phy_m88) &&
3737 !e1000_read_phy_reg(hw, M88E1000_RX_ERR_CNTR, &phy_tmp))
3738 adapter->phy_stats.receive_errors += phy_tmp;
3739 }
3740
Jeff Garzik15e376b2006-12-15 11:16:33 -05003741 /* Management Stats */
Joe Perches1dc32912008-07-11 15:17:08 -07003742 if (hw->has_smbus) {
3743 adapter->stats.mgptc += er32(MGTPTC);
3744 adapter->stats.mgprc += er32(MGTPRC);
3745 adapter->stats.mgpdc += er32(MGTPDC);
Jeff Garzik15e376b2006-12-15 11:16:33 -05003746 }
3747
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748 spin_unlock_irqrestore(&adapter->stats_lock, flags);
3749}
Jesse Brandeburg9ac98282006-11-01 08:48:10 -08003750
3751/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752 * e1000_intr - Interrupt Handler
3753 * @irq: interrupt number
3754 * @data: pointer to a network interface device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755 **/
Joe Perches64798842008-07-11 15:17:02 -07003756static irqreturn_t e1000_intr(int irq, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757{
3758 struct net_device *netdev = data;
Malli Chilakala60490fe2005-06-17 17:41:45 -07003759 struct e1000_adapter *adapter = netdev_priv(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760 struct e1000_hw *hw = &adapter->hw;
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00003761 u32 icr = er32(ICR);
Francois Romieuc3570ac2008-07-11 15:17:38 -07003762
Jesse Brandeburg4c11b8a2011-01-13 07:48:13 +00003763 if (unlikely((!icr)))
Jesse Brandeburg835bb122006-11-01 08:48:13 -08003764 return IRQ_NONE; /* Not our interrupt */
3765
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00003766 /* we might have caused the interrupt, but the above
Jesse Brandeburg4c11b8a2011-01-13 07:48:13 +00003767 * read cleared it, and just in case the driver is
3768 * down there is nothing to do so return handled
3769 */
3770 if (unlikely(test_bit(__E1000_DOWN, &adapter->flags)))
3771 return IRQ_HANDLED;
3772
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003773 if (unlikely(icr & (E1000_ICR_RXSEQ | E1000_ICR_LSC))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774 hw->get_link_status = 1;
Auke Kok1314bbf2006-09-27 12:54:02 -07003775 /* guard against interrupt when we're going down */
3776 if (!test_bit(__E1000_DOWN, &adapter->flags))
Jesse Brandeburga4010af2011-10-05 07:24:41 +00003777 schedule_delayed_work(&adapter->watchdog_task, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778 }
3779
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00003780 /* disable interrupts, without the synchronize_irq bit */
3781 ew32(IMC, ~0);
3782 E1000_WRITE_FLUSH();
3783
Ben Hutchings288379f2009-01-19 16:43:59 -08003784 if (likely(napi_schedule_prep(&adapter->napi))) {
Jesse Brandeburg835bb122006-11-01 08:48:13 -08003785 adapter->total_tx_bytes = 0;
3786 adapter->total_tx_packets = 0;
3787 adapter->total_rx_bytes = 0;
3788 adapter->total_rx_packets = 0;
Ben Hutchings288379f2009-01-19 16:43:59 -08003789 __napi_schedule(&adapter->napi);
Jesse Brandeburga6c42322009-03-25 21:59:22 +00003790 } else {
Auke Kok90fb5132006-11-01 08:47:30 -08003791 /* this really should not happen! if it does it is basically a
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00003792 * bug, but not a hard error, so enable ints and continue
3793 */
Jesse Brandeburga6c42322009-03-25 21:59:22 +00003794 if (!test_bit(__E1000_DOWN, &adapter->flags))
3795 e1000_irq_enable(adapter);
3796 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798 return IRQ_HANDLED;
3799}
3800
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801/**
3802 * e1000_clean - NAPI Rx polling callback
3803 * @adapter: board private structure
3804 **/
Joe Perches64798842008-07-11 15:17:02 -07003805static int e1000_clean(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806{
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00003807 struct e1000_adapter *adapter = container_of(napi, struct e1000_adapter,
3808 napi);
Jesse Brandeburg650b5a52009-09-25 12:19:23 +00003809 int tx_clean_complete = 0, work_done = 0;
Malli Chilakala26483452005-04-28 19:44:46 -07003810
Jesse Brandeburg650b5a52009-09-25 12:19:23 +00003811 tx_clean_complete = e1000_clean_tx_irq(adapter, &adapter->tx_ring[0]);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003812
Jesse Brandeburg650b5a52009-09-25 12:19:23 +00003813 adapter->clean_rx(adapter, &adapter->rx_ring[0], &work_done, budget);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003814
Jesse Brandeburg650b5a52009-09-25 12:19:23 +00003815 if (!tx_clean_complete)
David S. Millerd2c7ddd2008-01-15 22:43:24 -08003816 work_done = budget;
3817
David S. Miller53e52c72008-01-07 21:06:12 -08003818 /* If budget not fully consumed, exit the polling mode */
3819 if (work_done < budget) {
Jesse Brandeburg835bb122006-11-01 08:48:13 -08003820 if (likely(adapter->itr_setting & 3))
3821 e1000_set_itr(adapter);
Ben Hutchings288379f2009-01-19 16:43:59 -08003822 napi_complete(napi);
Jesse Brandeburga6c42322009-03-25 21:59:22 +00003823 if (!test_bit(__E1000_DOWN, &adapter->flags))
3824 e1000_irq_enable(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825 }
3826
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003827 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828}
3829
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830/**
3831 * e1000_clean_tx_irq - Reclaim resources after transmit completes
3832 * @adapter: board private structure
3833 **/
Joe Perches64798842008-07-11 15:17:02 -07003834static bool e1000_clean_tx_irq(struct e1000_adapter *adapter,
3835 struct e1000_tx_ring *tx_ring)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836{
Joe Perches1dc32912008-07-11 15:17:08 -07003837 struct e1000_hw *hw = &adapter->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838 struct net_device *netdev = adapter->netdev;
3839 struct e1000_tx_desc *tx_desc, *eop_desc;
3840 struct e1000_buffer *buffer_info;
3841 unsigned int i, eop;
Jeff Kirsher2a1af5d2006-03-02 18:20:43 -08003842 unsigned int count = 0;
Jesse Brandeburg835bb122006-11-01 08:48:13 -08003843 unsigned int total_tx_bytes=0, total_tx_packets=0;
Otto Estuardo Solares Cabrera2f66fd32012-08-24 08:04:58 +00003844 unsigned int bytes_compl = 0, pkts_compl = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845
3846 i = tx_ring->next_to_clean;
3847 eop = tx_ring->buffer_info[i].next_to_watch;
3848 eop_desc = E1000_TX_DESC(*tx_ring, eop);
3849
Alexander Duyckccfb3422009-03-25 21:59:04 +00003850 while ((eop_desc->upper.data & cpu_to_le32(E1000_TXD_STAT_DD)) &&
3851 (count < tx_ring->count)) {
Jesse Brandeburg843f4262009-04-16 16:59:47 +00003852 bool cleaned = false;
Jeff Kirsher2d0bb1c2010-08-08 16:02:31 +00003853 rmb(); /* read buffer_info after eop_desc */
Jesse Brandeburg843f4262009-04-16 16:59:47 +00003854 for ( ; !cleaned; count++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855 tx_desc = E1000_TX_DESC(*tx_ring, i);
3856 buffer_info = &tx_ring->buffer_info[i];
3857 cleaned = (i == eop);
3858
Jesse Brandeburg835bb122006-11-01 08:48:13 -08003859 if (cleaned) {
Dean Nelson31c15a22011-08-25 14:39:24 +00003860 total_tx_packets += buffer_info->segs;
3861 total_tx_bytes += buffer_info->bytecount;
Otto Estuardo Solares Cabrera2f66fd32012-08-24 08:04:58 +00003862 if (buffer_info->skb) {
3863 bytes_compl += buffer_info->skb->len;
3864 pkts_compl++;
3865 }
3866
Jesse Brandeburg835bb122006-11-01 08:48:13 -08003867 }
Jeff Kirsherfd803242005-12-13 00:06:22 -05003868 e1000_unmap_and_free_tx_resource(adapter, buffer_info);
Jesse Brandeburga9ebadd2006-11-01 08:47:53 -08003869 tx_desc->upper.data = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003871 if (unlikely(++i == tx_ring->count)) i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872 }
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003873
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874 eop = tx_ring->buffer_info[i].next_to_watch;
3875 eop_desc = E1000_TX_DESC(*tx_ring, eop);
3876 }
3877
3878 tx_ring->next_to_clean = i;
3879
Otto Estuardo Solares Cabrera2f66fd32012-08-24 08:04:58 +00003880 netdev_completed_queue(netdev, pkts_compl, bytes_compl);
3881
Auke Kok77b2aad2006-04-14 19:05:25 -07003882#define TX_WAKE_THRESHOLD 32
Jesse Brandeburg843f4262009-04-16 16:59:47 +00003883 if (unlikely(count && netif_carrier_ok(netdev) &&
Jesse Brandeburg65c79732006-09-27 12:53:48 -07003884 E1000_DESC_UNUSED(tx_ring) >= TX_WAKE_THRESHOLD)) {
3885 /* Make sure that anybody stopping the queue after this
3886 * sees the new next_to_clean.
3887 */
3888 smp_mb();
Jesse Brandeburgcdd75492009-09-25 12:18:07 +00003889
3890 if (netif_queue_stopped(netdev) &&
3891 !(test_bit(__E1000_DOWN, &adapter->flags))) {
Auke Kok77b2aad2006-04-14 19:05:25 -07003892 netif_wake_queue(netdev);
Jesse Brandeburgfcfb1222006-11-01 08:47:59 -08003893 ++adapter->restart_queue;
3894 }
Auke Kok77b2aad2006-04-14 19:05:25 -07003895 }
Malli Chilakala26483452005-04-28 19:44:46 -07003896
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003897 if (adapter->detect_tx_hung) {
Malli Chilakala26483452005-04-28 19:44:46 -07003898 /* Detect a transmit hang in hardware, this serializes the
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00003899 * check with the clearing of time_stamp and movement of i
3900 */
Joe Perchesc3033b02008-03-21 11:06:25 -07003901 adapter->detect_tx_hung = false;
Jesse Brandeburgcdd75492009-09-25 12:18:07 +00003902 if (tx_ring->buffer_info[eop].time_stamp &&
3903 time_after(jiffies, tx_ring->buffer_info[eop].time_stamp +
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00003904 (adapter->tx_timeout_factor * HZ)) &&
Joe Perches8e95a202009-12-03 07:58:21 +00003905 !(er32(STATUS) & E1000_STATUS_TXOFF)) {
Malli Chilakala70b8f1e2005-04-28 19:40:40 -07003906
3907 /* detected Tx unit hang */
Emil Tantilovfeb8f472010-07-26 23:37:21 -07003908 e_err(drv, "Detected Tx Unit Hang\n"
Emil Tantilov675ad472010-04-27 14:02:58 +00003909 " Tx Queue <%lu>\n"
3910 " TDH <%x>\n"
3911 " TDT <%x>\n"
3912 " next_to_use <%x>\n"
3913 " next_to_clean <%x>\n"
3914 "buffer_info[next_to_clean]\n"
3915 " time_stamp <%lx>\n"
3916 " next_to_watch <%x>\n"
3917 " jiffies <%lx>\n"
3918 " next_to_watch.status <%x>\n",
Hong Zhiguo49a45a02013-10-22 18:32:56 +00003919 (unsigned long)(tx_ring - adapter->tx_ring),
Joe Perches1dc32912008-07-11 15:17:08 -07003920 readl(hw->hw_addr + tx_ring->tdh),
3921 readl(hw->hw_addr + tx_ring->tdt),
Malli Chilakala70b8f1e2005-04-28 19:40:40 -07003922 tx_ring->next_to_use,
Jeff Kirsher392137f2006-01-12 16:50:57 -08003923 tx_ring->next_to_clean,
Jesse Brandeburgcdd75492009-09-25 12:18:07 +00003924 tx_ring->buffer_info[eop].time_stamp,
Malli Chilakala70b8f1e2005-04-28 19:40:40 -07003925 eop,
3926 jiffies,
3927 eop_desc->upper.fields.status);
Tushar Daveb04e36b2012-01-27 09:00:46 +00003928 e1000_dump(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929 netif_stop_queue(netdev);
Malli Chilakala70b8f1e2005-04-28 19:40:40 -07003930 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931 }
Jesse Brandeburg835bb122006-11-01 08:48:13 -08003932 adapter->total_tx_bytes += total_tx_bytes;
3933 adapter->total_tx_packets += total_tx_packets;
Ajit Khaparde5fe31de2009-10-07 02:42:23 +00003934 netdev->stats.tx_bytes += total_tx_bytes;
3935 netdev->stats.tx_packets += total_tx_packets;
Eric Dumazet807540b2010-09-23 05:40:09 +00003936 return count < tx_ring->count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937}
3938
3939/**
3940 * e1000_rx_checksum - Receive Checksum Offload for 82543
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003941 * @adapter: board private structure
3942 * @status_err: receive descriptor status and error fields
3943 * @csum: receive descriptor csum field
3944 * @sk_buff: socket buffer with received data
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945 **/
Joe Perches64798842008-07-11 15:17:02 -07003946static void e1000_rx_checksum(struct e1000_adapter *adapter, u32 status_err,
3947 u32 csum, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948{
Joe Perches1dc32912008-07-11 15:17:08 -07003949 struct e1000_hw *hw = &adapter->hw;
Joe Perches406874a2008-04-03 10:06:32 -07003950 u16 status = (u16)status_err;
3951 u8 errors = (u8)(status_err >> 24);
Eric Dumazetbc8acf22010-09-02 13:07:41 -07003952
3953 skb_checksum_none_assert(skb);
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003954
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955 /* 82543 or newer only */
Joe Perches1dc32912008-07-11 15:17:08 -07003956 if (unlikely(hw->mac_type < e1000_82543)) return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957 /* Ignore Checksum bit is set */
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003958 if (unlikely(status & E1000_RXD_STAT_IXSM)) return;
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003959 /* TCP/UDP checksum error bit is set */
Jesse Brandeburg96838a42006-01-18 13:01:39 -08003960 if (unlikely(errors & E1000_RXD_ERR_TCPE)) {
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003961 /* let the stack verify checksum errors */
3962 adapter->hw_csum_err++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963 return;
3964 }
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003965 /* TCP/UDP Checksum has not been calculated */
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00003966 if (!(status & E1000_RXD_STAT_TCPCS))
3967 return;
3968
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003969 /* It must be a TCP or UDP packet with a valid checksum */
3970 if (likely(status & E1000_RXD_STAT_TCPCS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971 /* TCP checksum is good */
3972 skb->ip_summed = CHECKSUM_UNNECESSARY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973 }
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003974 adapter->hw_csum_good++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975}
3976
3977/**
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00003978 * e1000_consume_page - helper function
3979 **/
3980static void e1000_consume_page(struct e1000_buffer *bi, struct sk_buff *skb,
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00003981 u16 length)
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00003982{
3983 bi->page = NULL;
3984 skb->len += length;
3985 skb->data_len += length;
Eric Dumazeted64b3c2011-10-13 07:53:42 +00003986 skb->truesize += PAGE_SIZE;
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00003987}
3988
3989/**
3990 * e1000_receive_skb - helper function to handle rx indications
3991 * @adapter: board private structure
3992 * @status: descriptor status field as written by hardware
3993 * @vlan: descriptor vlan field as written by hardware (no le/be conversion)
3994 * @skb: pointer to sk_buff to be indicated to stack
3995 */
3996static void e1000_receive_skb(struct e1000_adapter *adapter, u8 status,
3997 __le16 vlan, struct sk_buff *skb)
3998{
Jesse Brandeburg6a08d192010-09-22 18:23:05 +00003999 skb->protocol = eth_type_trans(skb, adapter->netdev);
4000
Jiri Pirko5622e402011-07-21 03:26:31 +00004001 if (status & E1000_RXD_STAT_VP) {
4002 u16 vid = le16_to_cpu(vlan) & E1000_RXD_SPC_VLAN_MASK;
4003
Patrick McHardy86a9bad2013-04-19 02:04:30 +00004004 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vid);
Jiri Pirko5622e402011-07-21 03:26:31 +00004005 }
4006 napi_gro_receive(&adapter->napi, skb);
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004007}
4008
4009/**
4010 * e1000_clean_jumbo_rx_irq - Send received data up the network stack; legacy
4011 * @adapter: board private structure
4012 * @rx_ring: ring to clean
4013 * @work_done: amount of napi work completed this call
4014 * @work_to_do: max amount of work allowed for this call to do
4015 *
4016 * the return value indicates whether actual cleaning was done, there
4017 * is no guarantee that everything was cleaned
4018 */
4019static bool e1000_clean_jumbo_rx_irq(struct e1000_adapter *adapter,
4020 struct e1000_rx_ring *rx_ring,
4021 int *work_done, int work_to_do)
4022{
4023 struct e1000_hw *hw = &adapter->hw;
4024 struct net_device *netdev = adapter->netdev;
4025 struct pci_dev *pdev = adapter->pdev;
4026 struct e1000_rx_desc *rx_desc, *next_rxd;
4027 struct e1000_buffer *buffer_info, *next_buffer;
4028 unsigned long irq_flags;
4029 u32 length;
4030 unsigned int i;
4031 int cleaned_count = 0;
4032 bool cleaned = false;
4033 unsigned int total_rx_bytes=0, total_rx_packets=0;
4034
4035 i = rx_ring->next_to_clean;
4036 rx_desc = E1000_RX_DESC(*rx_ring, i);
4037 buffer_info = &rx_ring->buffer_info[i];
4038
4039 while (rx_desc->status & E1000_RXD_STAT_DD) {
4040 struct sk_buff *skb;
4041 u8 status;
4042
4043 if (*work_done >= work_to_do)
4044 break;
4045 (*work_done)++;
Jeff Kirsher2d0bb1c2010-08-08 16:02:31 +00004046 rmb(); /* read descriptor and rx_buffer_info after status DD */
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004047
4048 status = rx_desc->status;
4049 skb = buffer_info->skb;
4050 buffer_info->skb = NULL;
4051
4052 if (++i == rx_ring->count) i = 0;
4053 next_rxd = E1000_RX_DESC(*rx_ring, i);
4054 prefetch(next_rxd);
4055
4056 next_buffer = &rx_ring->buffer_info[i];
4057
4058 cleaned = true;
4059 cleaned_count++;
Nick Nunleyb16f53b2010-04-27 13:08:45 +00004060 dma_unmap_page(&pdev->dev, buffer_info->dma,
4061 buffer_info->length, DMA_FROM_DEVICE);
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004062 buffer_info->dma = 0;
4063
4064 length = le16_to_cpu(rx_desc->length);
4065
4066 /* errors is only valid for DD + EOP descriptors */
4067 if (unlikely((status & E1000_RXD_STAT_EOP) &&
4068 (rx_desc->errors & E1000_RXD_ERR_FRAME_ERR_MASK))) {
Sebastian Andrzej Siewiora3060852012-05-11 16:30:46 +00004069 u8 *mapped;
4070 u8 last_byte;
4071
4072 mapped = page_address(buffer_info->page);
4073 last_byte = *(mapped + length - 1);
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004074 if (TBI_ACCEPT(hw, status, rx_desc->errors, length,
4075 last_byte)) {
4076 spin_lock_irqsave(&adapter->stats_lock,
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00004077 irq_flags);
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004078 e1000_tbi_adjust_stats(hw, &adapter->stats,
Sebastian Andrzej Siewior281a8f22012-05-15 09:18:55 +00004079 length, mapped);
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004080 spin_unlock_irqrestore(&adapter->stats_lock,
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00004081 irq_flags);
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004082 length--;
4083 } else {
Ben Greeare825b732012-04-04 06:01:29 +00004084 if (netdev->features & NETIF_F_RXALL)
4085 goto process_skb;
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004086 /* recycle both page and skb */
4087 buffer_info->skb = skb;
4088 /* an error means any chain goes out the window
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00004089 * too
4090 */
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004091 if (rx_ring->rx_skb_top)
4092 dev_kfree_skb(rx_ring->rx_skb_top);
4093 rx_ring->rx_skb_top = NULL;
4094 goto next_desc;
4095 }
4096 }
4097
4098#define rxtop rx_ring->rx_skb_top
Ben Greeare825b732012-04-04 06:01:29 +00004099process_skb:
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004100 if (!(status & E1000_RXD_STAT_EOP)) {
4101 /* this descriptor is only the beginning (or middle) */
4102 if (!rxtop) {
4103 /* this is the beginning of a chain */
4104 rxtop = skb;
4105 skb_fill_page_desc(rxtop, 0, buffer_info->page,
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00004106 0, length);
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004107 } else {
4108 /* this is the middle of a chain */
4109 skb_fill_page_desc(rxtop,
4110 skb_shinfo(rxtop)->nr_frags,
4111 buffer_info->page, 0, length);
4112 /* re-use the skb, only consumed the page */
4113 buffer_info->skb = skb;
4114 }
4115 e1000_consume_page(buffer_info, rxtop, length);
4116 goto next_desc;
4117 } else {
4118 if (rxtop) {
4119 /* end of the chain */
4120 skb_fill_page_desc(rxtop,
4121 skb_shinfo(rxtop)->nr_frags,
4122 buffer_info->page, 0, length);
4123 /* re-use the current skb, we only consumed the
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00004124 * page
4125 */
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004126 buffer_info->skb = skb;
4127 skb = rxtop;
4128 rxtop = NULL;
4129 e1000_consume_page(buffer_info, skb, length);
4130 } else {
4131 /* no chain, got EOP, this buf is the packet
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00004132 * copybreak to save the put_page/alloc_page
4133 */
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004134 if (length <= copybreak &&
4135 skb_tailroom(skb) >= length) {
4136 u8 *vaddr;
Cong Wang46790262011-11-25 23:14:23 +08004137 vaddr = kmap_atomic(buffer_info->page);
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00004138 memcpy(skb_tail_pointer(skb), vaddr,
4139 length);
Cong Wang46790262011-11-25 23:14:23 +08004140 kunmap_atomic(vaddr);
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004141 /* re-use the page, so don't erase
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00004142 * buffer_info->page
4143 */
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004144 skb_put(skb, length);
4145 } else {
4146 skb_fill_page_desc(skb, 0,
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00004147 buffer_info->page, 0,
4148 length);
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004149 e1000_consume_page(buffer_info, skb,
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00004150 length);
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004151 }
4152 }
4153 }
4154
4155 /* Receive Checksum Offload XXX recompute due to CRC strip? */
4156 e1000_rx_checksum(adapter,
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00004157 (u32)(status) |
4158 ((u32)(rx_desc->errors) << 24),
4159 le16_to_cpu(rx_desc->csum), skb);
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004160
Ben Greearb0d15622012-02-11 15:40:11 +00004161 total_rx_bytes += (skb->len - 4); /* don't count FCS */
4162 if (likely(!(netdev->features & NETIF_F_RXFCS)))
4163 pskb_trim(skb, skb->len - 4);
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004164 total_rx_packets++;
4165
4166 /* eth type trans needs skb->data to point to something */
4167 if (!pskb_may_pull(skb, ETH_HLEN)) {
Emil Tantilovfeb8f472010-07-26 23:37:21 -07004168 e_err(drv, "pskb_may_pull failed.\n");
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004169 dev_kfree_skb(skb);
4170 goto next_desc;
4171 }
4172
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004173 e1000_receive_skb(adapter, status, rx_desc->special, skb);
4174
4175next_desc:
4176 rx_desc->status = 0;
4177
4178 /* return some buffers to hardware, one at a time is too slow */
4179 if (unlikely(cleaned_count >= E1000_RX_BUFFER_WRITE)) {
4180 adapter->alloc_rx_buf(adapter, rx_ring, cleaned_count);
4181 cleaned_count = 0;
4182 }
4183
4184 /* use prefetched values */
4185 rx_desc = next_rxd;
4186 buffer_info = next_buffer;
4187 }
4188 rx_ring->next_to_clean = i;
4189
4190 cleaned_count = E1000_DESC_UNUSED(rx_ring);
4191 if (cleaned_count)
4192 adapter->alloc_rx_buf(adapter, rx_ring, cleaned_count);
4193
4194 adapter->total_rx_packets += total_rx_packets;
4195 adapter->total_rx_bytes += total_rx_bytes;
Ajit Khaparde5fe31de2009-10-07 02:42:23 +00004196 netdev->stats.rx_bytes += total_rx_bytes;
4197 netdev->stats.rx_packets += total_rx_packets;
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004198 return cleaned;
4199}
4200
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00004201/* this should improve performance for small packets with large amounts
Joe Perches57bf6ee2010-05-13 15:26:17 +00004202 * of reassembly being done in the stack
4203 */
4204static void e1000_check_copybreak(struct net_device *netdev,
4205 struct e1000_buffer *buffer_info,
4206 u32 length, struct sk_buff **skb)
4207{
4208 struct sk_buff *new_skb;
4209
4210 if (length > copybreak)
4211 return;
4212
4213 new_skb = netdev_alloc_skb_ip_align(netdev, length);
4214 if (!new_skb)
4215 return;
4216
4217 skb_copy_to_linear_data_offset(new_skb, -NET_IP_ALIGN,
4218 (*skb)->data - NET_IP_ALIGN,
4219 length + NET_IP_ALIGN);
4220 /* save the skb in buffer_info as good */
4221 buffer_info->skb = *skb;
4222 *skb = new_skb;
4223}
4224
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004225/**
Malli Chilakala2d7edb92005-04-28 19:43:52 -07004226 * e1000_clean_rx_irq - Send received data up the network stack; legacy
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227 * @adapter: board private structure
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004228 * @rx_ring: ring to clean
4229 * @work_done: amount of napi work completed this call
4230 * @work_to_do: max amount of work allowed for this call to do
4231 */
Joe Perches64798842008-07-11 15:17:02 -07004232static bool e1000_clean_rx_irq(struct e1000_adapter *adapter,
4233 struct e1000_rx_ring *rx_ring,
4234 int *work_done, int work_to_do)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235{
Joe Perches1dc32912008-07-11 15:17:08 -07004236 struct e1000_hw *hw = &adapter->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237 struct net_device *netdev = adapter->netdev;
4238 struct pci_dev *pdev = adapter->pdev;
Jesse Brandeburg86c3d592006-01-18 13:01:43 -08004239 struct e1000_rx_desc *rx_desc, *next_rxd;
4240 struct e1000_buffer *buffer_info, *next_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241 unsigned long flags;
Joe Perches406874a2008-04-03 10:06:32 -07004242 u32 length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243 unsigned int i;
Jeff Kirsher72d64a42006-01-12 16:51:19 -08004244 int cleaned_count = 0;
Joe Perchesc3033b02008-03-21 11:06:25 -07004245 bool cleaned = false;
Jesse Brandeburg835bb122006-11-01 08:48:13 -08004246 unsigned int total_rx_bytes=0, total_rx_packets=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247
4248 i = rx_ring->next_to_clean;
4249 rx_desc = E1000_RX_DESC(*rx_ring, i);
Jesse Brandeburgb92ff8e2006-01-18 13:01:32 -08004250 buffer_info = &rx_ring->buffer_info[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251
Jesse Brandeburgb92ff8e2006-01-18 13:01:32 -08004252 while (rx_desc->status & E1000_RXD_STAT_DD) {
Auke Kok24f476e2006-06-08 09:28:47 -07004253 struct sk_buff *skb;
Jeff Kirshera292ca62006-01-12 16:51:30 -08004254 u8 status;
Auke Kok90fb5132006-11-01 08:47:30 -08004255
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004256 if (*work_done >= work_to_do)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257 break;
4258 (*work_done)++;
Jeff Kirsher2d0bb1c2010-08-08 16:02:31 +00004259 rmb(); /* read descriptor and rx_buffer_info after status DD */
Francois Romieuc3570ac2008-07-11 15:17:38 -07004260
Jeff Kirshera292ca62006-01-12 16:51:30 -08004261 status = rx_desc->status;
Jesse Brandeburgb92ff8e2006-01-18 13:01:32 -08004262 skb = buffer_info->skb;
Jesse Brandeburg86c3d592006-01-18 13:01:43 -08004263 buffer_info->skb = NULL;
4264
Jeff Kirsher30320be2006-03-02 18:21:57 -08004265 prefetch(skb->data - NET_IP_ALIGN);
4266
Jesse Brandeburg86c3d592006-01-18 13:01:43 -08004267 if (++i == rx_ring->count) i = 0;
4268 next_rxd = E1000_RX_DESC(*rx_ring, i);
Jeff Kirsher30320be2006-03-02 18:21:57 -08004269 prefetch(next_rxd);
4270
Jesse Brandeburg86c3d592006-01-18 13:01:43 -08004271 next_buffer = &rx_ring->buffer_info[i];
Jesse Brandeburg86c3d592006-01-18 13:01:43 -08004272
Joe Perchesc3033b02008-03-21 11:06:25 -07004273 cleaned = true;
Jeff Kirsher72d64a42006-01-12 16:51:19 -08004274 cleaned_count++;
Nick Nunleyb16f53b2010-04-27 13:08:45 +00004275 dma_unmap_single(&pdev->dev, buffer_info->dma,
4276 buffer_info->length, DMA_FROM_DEVICE);
Jesse Brandeburg679be3b2009-06-30 12:45:34 +00004277 buffer_info->dma = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279 length = le16_to_cpu(rx_desc->length);
Neil Hormanea30e112009-06-02 01:29:58 -07004280 /* !EOP means multiple descriptors were used to store a single
Jesse Brandeburg40a14de2010-01-19 14:15:38 +00004281 * packet, if thats the case we need to toss it. In fact, we
4282 * to toss every packet with the EOP bit clear and the next
4283 * frame that _does_ have the EOP bit set, as it is by
4284 * definition only a frame fragment
4285 */
4286 if (unlikely(!(status & E1000_RXD_STAT_EOP)))
4287 adapter->discarding = true;
4288
4289 if (adapter->discarding) {
Jeff Kirshera1415ee2006-02-28 20:24:07 -08004290 /* All receives must fit into a single buffer */
Emil Tantilovfeb8f472010-07-26 23:37:21 -07004291 e_dbg("Receive packet consumed multiple buffers\n");
Auke Kok864c4e42006-06-27 09:06:53 -07004292 /* recycle */
Auke Kok8fc897b2006-08-28 14:56:16 -07004293 buffer_info->skb = skb;
Jesse Brandeburg40a14de2010-01-19 14:15:38 +00004294 if (status & E1000_RXD_STAT_EOP)
4295 adapter->discarding = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296 goto next_desc;
4297 }
4298
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004299 if (unlikely(rx_desc->errors & E1000_RXD_ERR_FRAME_ERR_MASK)) {
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004300 u8 last_byte = *(skb->data + length - 1);
Joe Perches1dc32912008-07-11 15:17:08 -07004301 if (TBI_ACCEPT(hw, status, rx_desc->errors, length,
4302 last_byte)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303 spin_lock_irqsave(&adapter->stats_lock, flags);
Joe Perches1dc32912008-07-11 15:17:08 -07004304 e1000_tbi_adjust_stats(hw, &adapter->stats,
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00004305 length, skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306 spin_unlock_irqrestore(&adapter->stats_lock,
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00004307 flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308 length--;
4309 } else {
Ben Greeare825b732012-04-04 06:01:29 +00004310 if (netdev->features & NETIF_F_RXALL)
4311 goto process_skb;
Auke Kok9e2feac2006-04-14 19:05:18 -07004312 /* recycle */
4313 buffer_info->skb = skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314 goto next_desc;
4315 }
Auke Kok1cb58212006-04-18 12:31:04 -07004316 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317
Ben Greeare825b732012-04-04 06:01:29 +00004318process_skb:
Ben Greearb0d15622012-02-11 15:40:11 +00004319 total_rx_bytes += (length - 4); /* don't count FCS */
Jesse Brandeburg835bb122006-11-01 08:48:13 -08004320 total_rx_packets++;
4321
Ben Greearb0d15622012-02-11 15:40:11 +00004322 if (likely(!(netdev->features & NETIF_F_RXFCS)))
4323 /* adjust length to remove Ethernet CRC, this must be
4324 * done after the TBI_ACCEPT workaround above
4325 */
4326 length -= 4;
4327
Joe Perches57bf6ee2010-05-13 15:26:17 +00004328 e1000_check_copybreak(netdev, buffer_info, length, &skb);
4329
Auke Kok996695d2006-11-01 08:47:50 -08004330 skb_put(skb, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331
4332 /* Receive Checksum Offload */
Jeff Kirshera292ca62006-01-12 16:51:30 -08004333 e1000_rx_checksum(adapter,
Joe Perches406874a2008-04-03 10:06:32 -07004334 (u32)(status) |
4335 ((u32)(rx_desc->errors) << 24),
David S. Millerc3d7a3a2006-03-15 14:26:28 -08004336 le16_to_cpu(rx_desc->csum), skb);
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004337
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004338 e1000_receive_skb(adapter, status, rx_desc->special, skb);
Francois Romieuc3570ac2008-07-11 15:17:38 -07004339
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340next_desc:
4341 rx_desc->status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342
Jeff Kirsher72d64a42006-01-12 16:51:19 -08004343 /* return some buffers to hardware, one at a time is too slow */
4344 if (unlikely(cleaned_count >= E1000_RX_BUFFER_WRITE)) {
4345 adapter->alloc_rx_buf(adapter, rx_ring, cleaned_count);
4346 cleaned_count = 0;
4347 }
4348
Jeff Kirsher30320be2006-03-02 18:21:57 -08004349 /* use prefetched values */
Jesse Brandeburg86c3d592006-01-18 13:01:43 -08004350 rx_desc = next_rxd;
4351 buffer_info = next_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353 rx_ring->next_to_clean = i;
Jeff Kirsher72d64a42006-01-12 16:51:19 -08004354
4355 cleaned_count = E1000_DESC_UNUSED(rx_ring);
4356 if (cleaned_count)
4357 adapter->alloc_rx_buf(adapter, rx_ring, cleaned_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004358
Jesse Brandeburg835bb122006-11-01 08:48:13 -08004359 adapter->total_rx_packets += total_rx_packets;
4360 adapter->total_rx_bytes += total_rx_bytes;
Ajit Khaparde5fe31de2009-10-07 02:42:23 +00004361 netdev->stats.rx_bytes += total_rx_bytes;
4362 netdev->stats.rx_packets += total_rx_packets;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363 return cleaned;
4364}
4365
4366/**
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004367 * e1000_alloc_jumbo_rx_buffers - Replace used jumbo receive buffers
4368 * @adapter: address of board private structure
4369 * @rx_ring: pointer to receive ring structure
4370 * @cleaned_count: number of buffers to allocate this pass
4371 **/
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004372static void
4373e1000_alloc_jumbo_rx_buffers(struct e1000_adapter *adapter,
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00004374 struct e1000_rx_ring *rx_ring, int cleaned_count)
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004375{
4376 struct net_device *netdev = adapter->netdev;
4377 struct pci_dev *pdev = adapter->pdev;
4378 struct e1000_rx_desc *rx_desc;
4379 struct e1000_buffer *buffer_info;
4380 struct sk_buff *skb;
4381 unsigned int i;
Eric Dumazet89d71a62009-10-13 05:34:20 +00004382 unsigned int bufsz = 256 - 16 /*for skb_reserve */ ;
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004383
4384 i = rx_ring->next_to_use;
4385 buffer_info = &rx_ring->buffer_info[i];
4386
4387 while (cleaned_count--) {
4388 skb = buffer_info->skb;
4389 if (skb) {
4390 skb_trim(skb, 0);
4391 goto check_page;
4392 }
4393
Eric Dumazet89d71a62009-10-13 05:34:20 +00004394 skb = netdev_alloc_skb_ip_align(netdev, bufsz);
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004395 if (unlikely(!skb)) {
4396 /* Better luck next round */
4397 adapter->alloc_rx_buff_failed++;
4398 break;
4399 }
4400
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004401 buffer_info->skb = skb;
4402 buffer_info->length = adapter->rx_buffer_len;
4403check_page:
4404 /* allocate a new page if necessary */
4405 if (!buffer_info->page) {
4406 buffer_info->page = alloc_page(GFP_ATOMIC);
4407 if (unlikely(!buffer_info->page)) {
4408 adapter->alloc_rx_buff_failed++;
4409 break;
4410 }
4411 }
4412
Anton Blanchardb5abb022010-02-19 17:54:53 +00004413 if (!buffer_info->dma) {
Nick Nunleyb16f53b2010-04-27 13:08:45 +00004414 buffer_info->dma = dma_map_page(&pdev->dev,
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00004415 buffer_info->page, 0,
Nick Nunleyb16f53b2010-04-27 13:08:45 +00004416 buffer_info->length,
4417 DMA_FROM_DEVICE);
4418 if (dma_mapping_error(&pdev->dev, buffer_info->dma)) {
Anton Blanchardb5abb022010-02-19 17:54:53 +00004419 put_page(buffer_info->page);
4420 dev_kfree_skb(skb);
4421 buffer_info->page = NULL;
4422 buffer_info->skb = NULL;
4423 buffer_info->dma = 0;
4424 adapter->alloc_rx_buff_failed++;
4425 break; /* while !buffer_info->skb */
4426 }
4427 }
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004428
4429 rx_desc = E1000_RX_DESC(*rx_ring, i);
4430 rx_desc->buffer_addr = cpu_to_le64(buffer_info->dma);
4431
4432 if (unlikely(++i == rx_ring->count))
4433 i = 0;
4434 buffer_info = &rx_ring->buffer_info[i];
4435 }
4436
4437 if (likely(rx_ring->next_to_use != i)) {
4438 rx_ring->next_to_use = i;
4439 if (unlikely(i-- == 0))
4440 i = (rx_ring->count - 1);
4441
4442 /* Force memory writes to complete before letting h/w
4443 * know there are new descriptors to fetch. (Only
4444 * applicable for weak-ordered memory model archs,
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00004445 * such as IA-64).
4446 */
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004447 wmb();
4448 writel(i, adapter->hw.hw_addr + rx_ring->rdt);
4449 }
4450}
4451
4452/**
Malli Chilakala2d7edb92005-04-28 19:43:52 -07004453 * e1000_alloc_rx_buffers - Replace used receive buffers; legacy & extended
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454 * @adapter: address of board private structure
4455 **/
Joe Perches64798842008-07-11 15:17:02 -07004456static void e1000_alloc_rx_buffers(struct e1000_adapter *adapter,
4457 struct e1000_rx_ring *rx_ring,
4458 int cleaned_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459{
Joe Perches1dc32912008-07-11 15:17:08 -07004460 struct e1000_hw *hw = &adapter->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461 struct net_device *netdev = adapter->netdev;
4462 struct pci_dev *pdev = adapter->pdev;
4463 struct e1000_rx_desc *rx_desc;
4464 struct e1000_buffer *buffer_info;
4465 struct sk_buff *skb;
Malli Chilakala26483452005-04-28 19:44:46 -07004466 unsigned int i;
Eric Dumazet89d71a62009-10-13 05:34:20 +00004467 unsigned int bufsz = adapter->rx_buffer_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468
4469 i = rx_ring->next_to_use;
4470 buffer_info = &rx_ring->buffer_info[i];
4471
Jeff Kirshera292ca62006-01-12 16:51:30 -08004472 while (cleaned_count--) {
Christoph Hellwigca6f7222006-08-31 14:27:47 -07004473 skb = buffer_info->skb;
4474 if (skb) {
Jeff Kirshera292ca62006-01-12 16:51:30 -08004475 skb_trim(skb, 0);
4476 goto map_skb;
4477 }
4478
Eric Dumazet89d71a62009-10-13 05:34:20 +00004479 skb = netdev_alloc_skb_ip_align(netdev, bufsz);
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004480 if (unlikely(!skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481 /* Better luck next round */
Jeff Kirsher72d64a42006-01-12 16:51:19 -08004482 adapter->alloc_rx_buff_failed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483 break;
4484 }
4485
Malli Chilakala26483452005-04-28 19:44:46 -07004486 /* Fix for errata 23, can't cross 64kB boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487 if (!e1000_check_64k_bound(adapter, skb->data, bufsz)) {
4488 struct sk_buff *oldskb = skb;
Emil Tantilovfeb8f472010-07-26 23:37:21 -07004489 e_err(rx_err, "skb align check failed: %u bytes at "
4490 "%p\n", bufsz, skb->data);
Malli Chilakala26483452005-04-28 19:44:46 -07004491 /* Try again, without freeing the previous */
Eric Dumazet89d71a62009-10-13 05:34:20 +00004492 skb = netdev_alloc_skb_ip_align(netdev, bufsz);
Malli Chilakala26483452005-04-28 19:44:46 -07004493 /* Failed allocation, critical failure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494 if (!skb) {
4495 dev_kfree_skb(oldskb);
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004496 adapter->alloc_rx_buff_failed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497 break;
4498 }
Malli Chilakala26483452005-04-28 19:44:46 -07004499
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500 if (!e1000_check_64k_bound(adapter, skb->data, bufsz)) {
4501 /* give up */
4502 dev_kfree_skb(skb);
4503 dev_kfree_skb(oldskb);
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004504 adapter->alloc_rx_buff_failed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505 break; /* while !buffer_info->skb */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506 }
Christoph Hellwigca6f7222006-08-31 14:27:47 -07004507
4508 /* Use new allocation */
4509 dev_kfree_skb(oldskb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511 buffer_info->skb = skb;
4512 buffer_info->length = adapter->rx_buffer_len;
Jeff Kirshera292ca62006-01-12 16:51:30 -08004513map_skb:
Nick Nunleyb16f53b2010-04-27 13:08:45 +00004514 buffer_info->dma = dma_map_single(&pdev->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515 skb->data,
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004516 buffer_info->length,
Nick Nunleyb16f53b2010-04-27 13:08:45 +00004517 DMA_FROM_DEVICE);
4518 if (dma_mapping_error(&pdev->dev, buffer_info->dma)) {
Anton Blanchardb5abb022010-02-19 17:54:53 +00004519 dev_kfree_skb(skb);
4520 buffer_info->skb = NULL;
4521 buffer_info->dma = 0;
4522 adapter->alloc_rx_buff_failed++;
4523 break; /* while !buffer_info->skb */
4524 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00004526 /* XXX if it was allocated cleanly it will never map to a
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004527 * boundary crossing
4528 */
4529
Malli Chilakala26483452005-04-28 19:44:46 -07004530 /* Fix for errata 23, can't cross 64kB boundary */
4531 if (!e1000_check_64k_bound(adapter,
4532 (void *)(unsigned long)buffer_info->dma,
4533 adapter->rx_buffer_len)) {
Emil Tantilovfeb8f472010-07-26 23:37:21 -07004534 e_err(rx_err, "dma align check failed: %u bytes at "
4535 "%p\n", adapter->rx_buffer_len,
Emil Tantilov675ad472010-04-27 14:02:58 +00004536 (void *)(unsigned long)buffer_info->dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537 dev_kfree_skb(skb);
4538 buffer_info->skb = NULL;
4539
Nick Nunleyb16f53b2010-04-27 13:08:45 +00004540 dma_unmap_single(&pdev->dev, buffer_info->dma,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541 adapter->rx_buffer_len,
Nick Nunleyb16f53b2010-04-27 13:08:45 +00004542 DMA_FROM_DEVICE);
Jesse Brandeburg679be3b2009-06-30 12:45:34 +00004543 buffer_info->dma = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544
Jesse Brandeburgedbbb3c2009-07-06 10:44:39 +00004545 adapter->alloc_rx_buff_failed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546 break; /* while !buffer_info->skb */
4547 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004548 rx_desc = E1000_RX_DESC(*rx_ring, i);
4549 rx_desc->buffer_addr = cpu_to_le64(buffer_info->dma);
4550
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004551 if (unlikely(++i == rx_ring->count))
4552 i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553 buffer_info = &rx_ring->buffer_info[i];
4554 }
4555
Jesse Brandeburgb92ff8e2006-01-18 13:01:32 -08004556 if (likely(rx_ring->next_to_use != i)) {
4557 rx_ring->next_to_use = i;
4558 if (unlikely(i-- == 0))
4559 i = (rx_ring->count - 1);
4560
4561 /* Force memory writes to complete before letting h/w
4562 * know there are new descriptors to fetch. (Only
4563 * applicable for weak-ordered memory model archs,
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00004564 * such as IA-64).
4565 */
Jesse Brandeburgb92ff8e2006-01-18 13:01:32 -08004566 wmb();
Joe Perches1dc32912008-07-11 15:17:08 -07004567 writel(i, hw->hw_addr + rx_ring->rdt);
Jesse Brandeburgb92ff8e2006-01-18 13:01:32 -08004568 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569}
4570
4571/**
4572 * e1000_smartspeed - Workaround for SmartSpeed on 82541 and 82547 controllers.
4573 * @adapter:
4574 **/
Joe Perches64798842008-07-11 15:17:02 -07004575static void e1000_smartspeed(struct e1000_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576{
Joe Perches1dc32912008-07-11 15:17:08 -07004577 struct e1000_hw *hw = &adapter->hw;
Joe Perches406874a2008-04-03 10:06:32 -07004578 u16 phy_status;
4579 u16 phy_ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580
Joe Perches1dc32912008-07-11 15:17:08 -07004581 if ((hw->phy_type != e1000_phy_igp) || !hw->autoneg ||
4582 !(hw->autoneg_advertised & ADVERTISE_1000_FULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583 return;
4584
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004585 if (adapter->smartspeed == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586 /* If Master/Slave config fault is asserted twice,
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00004587 * we assume back-to-back
4588 */
Joe Perches1dc32912008-07-11 15:17:08 -07004589 e1000_read_phy_reg(hw, PHY_1000T_STATUS, &phy_status);
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004590 if (!(phy_status & SR_1000T_MS_CONFIG_FAULT)) return;
Joe Perches1dc32912008-07-11 15:17:08 -07004591 e1000_read_phy_reg(hw, PHY_1000T_STATUS, &phy_status);
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004592 if (!(phy_status & SR_1000T_MS_CONFIG_FAULT)) return;
Joe Perches1dc32912008-07-11 15:17:08 -07004593 e1000_read_phy_reg(hw, PHY_1000T_CTRL, &phy_ctrl);
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004594 if (phy_ctrl & CR_1000T_MS_ENABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595 phy_ctrl &= ~CR_1000T_MS_ENABLE;
Joe Perches1dc32912008-07-11 15:17:08 -07004596 e1000_write_phy_reg(hw, PHY_1000T_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597 phy_ctrl);
4598 adapter->smartspeed++;
Joe Perches1dc32912008-07-11 15:17:08 -07004599 if (!e1000_phy_setup_autoneg(hw) &&
4600 !e1000_read_phy_reg(hw, PHY_CTRL,
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00004601 &phy_ctrl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602 phy_ctrl |= (MII_CR_AUTO_NEG_EN |
4603 MII_CR_RESTART_AUTO_NEG);
Joe Perches1dc32912008-07-11 15:17:08 -07004604 e1000_write_phy_reg(hw, PHY_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605 phy_ctrl);
4606 }
4607 }
4608 return;
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004609 } else if (adapter->smartspeed == E1000_SMARTSPEED_DOWNSHIFT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610 /* If still no link, perhaps using 2/3 pair cable */
Joe Perches1dc32912008-07-11 15:17:08 -07004611 e1000_read_phy_reg(hw, PHY_1000T_CTRL, &phy_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612 phy_ctrl |= CR_1000T_MS_ENABLE;
Joe Perches1dc32912008-07-11 15:17:08 -07004613 e1000_write_phy_reg(hw, PHY_1000T_CTRL, phy_ctrl);
4614 if (!e1000_phy_setup_autoneg(hw) &&
4615 !e1000_read_phy_reg(hw, PHY_CTRL, &phy_ctrl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616 phy_ctrl |= (MII_CR_AUTO_NEG_EN |
4617 MII_CR_RESTART_AUTO_NEG);
Joe Perches1dc32912008-07-11 15:17:08 -07004618 e1000_write_phy_reg(hw, PHY_CTRL, phy_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619 }
4620 }
4621 /* Restart process after E1000_SMARTSPEED_MAX iterations */
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004622 if (adapter->smartspeed++ == E1000_SMARTSPEED_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623 adapter->smartspeed = 0;
4624}
4625
4626/**
4627 * e1000_ioctl -
4628 * @netdev:
4629 * @ifreq:
4630 * @cmd:
4631 **/
Joe Perches64798842008-07-11 15:17:02 -07004632static int e1000_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633{
4634 switch (cmd) {
4635 case SIOCGMIIPHY:
4636 case SIOCGMIIREG:
4637 case SIOCSMIIREG:
4638 return e1000_mii_ioctl(netdev, ifr, cmd);
4639 default:
4640 return -EOPNOTSUPP;
4641 }
4642}
4643
4644/**
4645 * e1000_mii_ioctl -
4646 * @netdev:
4647 * @ifreq:
4648 * @cmd:
4649 **/
Joe Perches64798842008-07-11 15:17:02 -07004650static int e1000_mii_ioctl(struct net_device *netdev, struct ifreq *ifr,
4651 int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652{
Malli Chilakala60490fe2005-06-17 17:41:45 -07004653 struct e1000_adapter *adapter = netdev_priv(netdev);
Joe Perches1dc32912008-07-11 15:17:08 -07004654 struct e1000_hw *hw = &adapter->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655 struct mii_ioctl_data *data = if_mii(ifr);
4656 int retval;
Joe Perches406874a2008-04-03 10:06:32 -07004657 u16 mii_reg;
Malli Chilakala97876fc2005-06-17 17:40:19 -07004658 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659
Joe Perches1dc32912008-07-11 15:17:08 -07004660 if (hw->media_type != e1000_media_type_copper)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661 return -EOPNOTSUPP;
4662
4663 switch (cmd) {
4664 case SIOCGMIIPHY:
Joe Perches1dc32912008-07-11 15:17:08 -07004665 data->phy_id = hw->phy_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666 break;
4667 case SIOCGMIIREG:
Malli Chilakala97876fc2005-06-17 17:40:19 -07004668 spin_lock_irqsave(&adapter->stats_lock, flags);
Joe Perches1dc32912008-07-11 15:17:08 -07004669 if (e1000_read_phy_reg(hw, data->reg_num & 0x1F,
Malli Chilakala97876fc2005-06-17 17:40:19 -07004670 &data->val_out)) {
4671 spin_unlock_irqrestore(&adapter->stats_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672 return -EIO;
Malli Chilakala97876fc2005-06-17 17:40:19 -07004673 }
4674 spin_unlock_irqrestore(&adapter->stats_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675 break;
4676 case SIOCSMIIREG:
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004677 if (data->reg_num & ~(0x1F))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678 return -EFAULT;
4679 mii_reg = data->val_in;
Malli Chilakala97876fc2005-06-17 17:40:19 -07004680 spin_lock_irqsave(&adapter->stats_lock, flags);
Joe Perches1dc32912008-07-11 15:17:08 -07004681 if (e1000_write_phy_reg(hw, data->reg_num,
Malli Chilakala97876fc2005-06-17 17:40:19 -07004682 mii_reg)) {
4683 spin_unlock_irqrestore(&adapter->stats_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684 return -EIO;
Malli Chilakala97876fc2005-06-17 17:40:19 -07004685 }
Jesse Brandeburgf0163ac2007-11-13 21:00:09 -08004686 spin_unlock_irqrestore(&adapter->stats_lock, flags);
Joe Perches1dc32912008-07-11 15:17:08 -07004687 if (hw->media_type == e1000_media_type_copper) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688 switch (data->reg_num) {
4689 case PHY_CTRL:
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004690 if (mii_reg & MII_CR_POWER_DOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691 break;
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004692 if (mii_reg & MII_CR_AUTO_NEG_EN) {
Joe Perches1dc32912008-07-11 15:17:08 -07004693 hw->autoneg = 1;
4694 hw->autoneg_advertised = 0x2F;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695 } else {
David Decotigny14ad2512011-04-27 18:32:43 +00004696 u32 speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697 if (mii_reg & 0x40)
David Decotigny14ad2512011-04-27 18:32:43 +00004698 speed = SPEED_1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699 else if (mii_reg & 0x2000)
David Decotigny14ad2512011-04-27 18:32:43 +00004700 speed = SPEED_100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701 else
David Decotigny14ad2512011-04-27 18:32:43 +00004702 speed = SPEED_10;
4703 retval = e1000_set_spd_dplx(
4704 adapter, speed,
4705 ((mii_reg & 0x100)
4706 ? DUPLEX_FULL :
4707 DUPLEX_HALF));
Jesse Brandeburgf0163ac2007-11-13 21:00:09 -08004708 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709 return retval;
4710 }
Auke Kok2db10a02006-06-27 09:06:28 -07004711 if (netif_running(adapter->netdev))
4712 e1000_reinit_locked(adapter);
4713 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714 e1000_reset(adapter);
4715 break;
4716 case M88E1000_PHY_SPEC_CTRL:
4717 case M88E1000_EXT_PHY_SPEC_CTRL:
Joe Perches1dc32912008-07-11 15:17:08 -07004718 if (e1000_phy_reset(hw))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719 return -EIO;
4720 break;
4721 }
4722 } else {
4723 switch (data->reg_num) {
4724 case PHY_CTRL:
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004725 if (mii_reg & MII_CR_POWER_DOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004726 break;
Auke Kok2db10a02006-06-27 09:06:28 -07004727 if (netif_running(adapter->netdev))
4728 e1000_reinit_locked(adapter);
4729 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004730 e1000_reset(adapter);
4731 break;
4732 }
4733 }
4734 break;
4735 default:
4736 return -EOPNOTSUPP;
4737 }
4738 return E1000_SUCCESS;
4739}
4740
Joe Perches64798842008-07-11 15:17:02 -07004741void e1000_pci_set_mwi(struct e1000_hw *hw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742{
4743 struct e1000_adapter *adapter = hw->back;
Malli Chilakala26483452005-04-28 19:44:46 -07004744 int ret_val = pci_set_mwi(adapter->pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004745
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004746 if (ret_val)
Emil Tantilovfeb8f472010-07-26 23:37:21 -07004747 e_err(probe, "Error in setting MWI\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004748}
4749
Joe Perches64798842008-07-11 15:17:02 -07004750void e1000_pci_clear_mwi(struct e1000_hw *hw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751{
4752 struct e1000_adapter *adapter = hw->back;
4753
4754 pci_clear_mwi(adapter->pdev);
4755}
4756
Joe Perches64798842008-07-11 15:17:02 -07004757int e1000_pcix_get_mmrbc(struct e1000_hw *hw)
Peter Oruba007755e2007-09-28 22:42:06 -07004758{
4759 struct e1000_adapter *adapter = hw->back;
4760 return pcix_get_mmrbc(adapter->pdev);
4761}
4762
Joe Perches64798842008-07-11 15:17:02 -07004763void e1000_pcix_set_mmrbc(struct e1000_hw *hw, int mmrbc)
Peter Oruba007755e2007-09-28 22:42:06 -07004764{
4765 struct e1000_adapter *adapter = hw->back;
4766 pcix_set_mmrbc(adapter->pdev, mmrbc);
4767}
4768
Joe Perches64798842008-07-11 15:17:02 -07004769void e1000_io_write(struct e1000_hw *hw, unsigned long port, u32 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770{
4771 outl(value, port);
4772}
4773
Jiri Pirko5622e402011-07-21 03:26:31 +00004774static bool e1000_vlan_used(struct e1000_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775{
Jiri Pirko5622e402011-07-21 03:26:31 +00004776 u16 vid;
4777
4778 for_each_set_bit(vid, adapter->active_vlans, VLAN_N_VID)
4779 return true;
4780 return false;
4781}
4782
Jiri Pirko52f55092012-03-20 18:10:01 +00004783static void __e1000_vlan_mode(struct e1000_adapter *adapter,
4784 netdev_features_t features)
4785{
4786 struct e1000_hw *hw = &adapter->hw;
4787 u32 ctrl;
4788
4789 ctrl = er32(CTRL);
Patrick McHardyf6469682013-04-19 02:04:27 +00004790 if (features & NETIF_F_HW_VLAN_CTAG_RX) {
Jiri Pirko52f55092012-03-20 18:10:01 +00004791 /* enable VLAN tag insert/strip */
4792 ctrl |= E1000_CTRL_VME;
4793 } else {
4794 /* disable VLAN tag insert/strip */
4795 ctrl &= ~E1000_CTRL_VME;
4796 }
4797 ew32(CTRL, ctrl);
4798}
Jiri Pirko5622e402011-07-21 03:26:31 +00004799static void e1000_vlan_filter_on_off(struct e1000_adapter *adapter,
4800 bool filter_on)
4801{
Joe Perches1dc32912008-07-11 15:17:08 -07004802 struct e1000_hw *hw = &adapter->hw;
Jiri Pirko5622e402011-07-21 03:26:31 +00004803 u32 rctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804
Jesse Brandeburg9150b762008-03-21 11:06:58 -07004805 if (!test_bit(__E1000_DOWN, &adapter->flags))
4806 e1000_irq_disable(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807
Jiri Pirko52f55092012-03-20 18:10:01 +00004808 __e1000_vlan_mode(adapter, adapter->netdev->features);
Jiri Pirko5622e402011-07-21 03:26:31 +00004809 if (filter_on) {
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00004810 /* enable VLAN receive filtering */
4811 rctl = er32(RCTL);
4812 rctl &= ~E1000_RCTL_CFIEN;
Jiri Pirko5622e402011-07-21 03:26:31 +00004813 if (!(adapter->netdev->flags & IFF_PROMISC))
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00004814 rctl |= E1000_RCTL_VFE;
4815 ew32(RCTL, rctl);
4816 e1000_update_mng_vlan(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817 } else {
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00004818 /* disable VLAN receive filtering */
4819 rctl = er32(RCTL);
4820 rctl &= ~E1000_RCTL_VFE;
4821 ew32(RCTL, rctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004822 }
4823
Jesse Brandeburg9150b762008-03-21 11:06:58 -07004824 if (!test_bit(__E1000_DOWN, &adapter->flags))
4825 e1000_irq_enable(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826}
4827
Michał Mirosławc8f44af2011-11-15 15:29:55 +00004828static void e1000_vlan_mode(struct net_device *netdev,
Jiri Pirko52f55092012-03-20 18:10:01 +00004829 netdev_features_t features)
Jiri Pirko5622e402011-07-21 03:26:31 +00004830{
4831 struct e1000_adapter *adapter = netdev_priv(netdev);
Jiri Pirko5622e402011-07-21 03:26:31 +00004832
4833 if (!test_bit(__E1000_DOWN, &adapter->flags))
4834 e1000_irq_disable(adapter);
4835
Jiri Pirko52f55092012-03-20 18:10:01 +00004836 __e1000_vlan_mode(adapter, features);
Jiri Pirko5622e402011-07-21 03:26:31 +00004837
4838 if (!test_bit(__E1000_DOWN, &adapter->flags))
4839 e1000_irq_enable(adapter);
4840}
4841
Patrick McHardy80d5c362013-04-19 02:04:28 +00004842static int e1000_vlan_rx_add_vid(struct net_device *netdev,
4843 __be16 proto, u16 vid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844{
Malli Chilakala60490fe2005-06-17 17:41:45 -07004845 struct e1000_adapter *adapter = netdev_priv(netdev);
Joe Perches1dc32912008-07-11 15:17:08 -07004846 struct e1000_hw *hw = &adapter->hw;
Joe Perches406874a2008-04-03 10:06:32 -07004847 u32 vfta, index;
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004848
Joe Perches1dc32912008-07-11 15:17:08 -07004849 if ((hw->mng_cookie.status &
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004850 E1000_MNG_DHCP_COOKIE_STATUS_VLAN_SUPPORT) &&
4851 (vid == adapter->mng_vlan_id))
Jiri Pirko8e586132011-12-08 19:52:37 -05004852 return 0;
Jiri Pirko5622e402011-07-21 03:26:31 +00004853
4854 if (!e1000_vlan_used(adapter))
4855 e1000_vlan_filter_on_off(adapter, true);
4856
Linus Torvalds1da177e2005-04-16 15:20:36 -07004857 /* add VID to filter table */
4858 index = (vid >> 5) & 0x7F;
Joe Perches1dc32912008-07-11 15:17:08 -07004859 vfta = E1000_READ_REG_ARRAY(hw, VFTA, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860 vfta |= (1 << (vid & 0x1F));
Joe Perches1dc32912008-07-11 15:17:08 -07004861 e1000_write_vfta(hw, index, vfta);
Jiri Pirko5622e402011-07-21 03:26:31 +00004862
4863 set_bit(vid, adapter->active_vlans);
Jiri Pirko8e586132011-12-08 19:52:37 -05004864
4865 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004866}
4867
Patrick McHardy80d5c362013-04-19 02:04:28 +00004868static int e1000_vlan_rx_kill_vid(struct net_device *netdev,
4869 __be16 proto, u16 vid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870{
Malli Chilakala60490fe2005-06-17 17:41:45 -07004871 struct e1000_adapter *adapter = netdev_priv(netdev);
Joe Perches1dc32912008-07-11 15:17:08 -07004872 struct e1000_hw *hw = &adapter->hw;
Joe Perches406874a2008-04-03 10:06:32 -07004873 u32 vfta, index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874
Jesse Brandeburg9150b762008-03-21 11:06:58 -07004875 if (!test_bit(__E1000_DOWN, &adapter->flags))
4876 e1000_irq_disable(adapter);
Jesse Brandeburg9150b762008-03-21 11:06:58 -07004877 if (!test_bit(__E1000_DOWN, &adapter->flags))
4878 e1000_irq_enable(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879
4880 /* remove VID from filter table */
4881 index = (vid >> 5) & 0x7F;
Joe Perches1dc32912008-07-11 15:17:08 -07004882 vfta = E1000_READ_REG_ARRAY(hw, VFTA, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004883 vfta &= ~(1 << (vid & 0x1F));
Joe Perches1dc32912008-07-11 15:17:08 -07004884 e1000_write_vfta(hw, index, vfta);
Jiri Pirko5622e402011-07-21 03:26:31 +00004885
4886 clear_bit(vid, adapter->active_vlans);
4887
4888 if (!e1000_vlan_used(adapter))
4889 e1000_vlan_filter_on_off(adapter, false);
Jiri Pirko8e586132011-12-08 19:52:37 -05004890
4891 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004892}
4893
Joe Perches64798842008-07-11 15:17:02 -07004894static void e1000_restore_vlan(struct e1000_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895{
Jiri Pirko5622e402011-07-21 03:26:31 +00004896 u16 vid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897
Jiri Pirko5622e402011-07-21 03:26:31 +00004898 if (!e1000_vlan_used(adapter))
4899 return;
4900
4901 e1000_vlan_filter_on_off(adapter, true);
4902 for_each_set_bit(vid, adapter->active_vlans, VLAN_N_VID)
Patrick McHardy80d5c362013-04-19 02:04:28 +00004903 e1000_vlan_rx_add_vid(adapter->netdev, htons(ETH_P_8021Q), vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004904}
4905
David Decotigny14ad2512011-04-27 18:32:43 +00004906int e1000_set_spd_dplx(struct e1000_adapter *adapter, u32 spd, u8 dplx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907{
Joe Perches1dc32912008-07-11 15:17:08 -07004908 struct e1000_hw *hw = &adapter->hw;
4909
4910 hw->autoneg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004911
David Decotigny14ad2512011-04-27 18:32:43 +00004912 /* Make sure dplx is at most 1 bit and lsb of speed is not set
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00004913 * for the switch() below to work
4914 */
David Decotigny14ad2512011-04-27 18:32:43 +00004915 if ((spd & 1) || (dplx & ~1))
4916 goto err_inval;
4917
Malli Chilakala69213682005-06-17 17:44:20 -07004918 /* Fiber NICs only allow 1000 gbps Full duplex */
Joe Perches1dc32912008-07-11 15:17:08 -07004919 if ((hw->media_type == e1000_media_type_fiber) &&
David Decotigny14ad2512011-04-27 18:32:43 +00004920 spd != SPEED_1000 &&
4921 dplx != DUPLEX_FULL)
4922 goto err_inval;
Malli Chilakala69213682005-06-17 17:44:20 -07004923
David Decotigny14ad2512011-04-27 18:32:43 +00004924 switch (spd + dplx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925 case SPEED_10 + DUPLEX_HALF:
Joe Perches1dc32912008-07-11 15:17:08 -07004926 hw->forced_speed_duplex = e1000_10_half;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927 break;
4928 case SPEED_10 + DUPLEX_FULL:
Joe Perches1dc32912008-07-11 15:17:08 -07004929 hw->forced_speed_duplex = e1000_10_full;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930 break;
4931 case SPEED_100 + DUPLEX_HALF:
Joe Perches1dc32912008-07-11 15:17:08 -07004932 hw->forced_speed_duplex = e1000_100_half;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933 break;
4934 case SPEED_100 + DUPLEX_FULL:
Joe Perches1dc32912008-07-11 15:17:08 -07004935 hw->forced_speed_duplex = e1000_100_full;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936 break;
4937 case SPEED_1000 + DUPLEX_FULL:
Joe Perches1dc32912008-07-11 15:17:08 -07004938 hw->autoneg = 1;
4939 hw->autoneg_advertised = ADVERTISE_1000_FULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940 break;
4941 case SPEED_1000 + DUPLEX_HALF: /* not supported */
4942 default:
David Decotigny14ad2512011-04-27 18:32:43 +00004943 goto err_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944 }
Jesse Brandeburgc819bbd52012-07-26 02:31:09 +00004945
4946 /* clear MDI, MDI(-X) override is only allowed when autoneg enabled */
4947 hw->mdix = AUTO_ALL_MODES;
4948
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949 return 0;
David Decotigny14ad2512011-04-27 18:32:43 +00004950
4951err_inval:
4952 e_err(probe, "Unsupported Speed/Duplex configuration\n");
4953 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004954}
4955
Rafael J. Wysockib43fcd72009-04-15 17:43:24 +00004956static int __e1000_shutdown(struct pci_dev *pdev, bool *enable_wake)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957{
4958 struct net_device *netdev = pci_get_drvdata(pdev);
Malli Chilakala60490fe2005-06-17 17:41:45 -07004959 struct e1000_adapter *adapter = netdev_priv(netdev);
Joe Perches1dc32912008-07-11 15:17:08 -07004960 struct e1000_hw *hw = &adapter->hw;
Joe Perches406874a2008-04-03 10:06:32 -07004961 u32 ctrl, ctrl_ext, rctl, status;
4962 u32 wufc = adapter->wol;
Auke Kok6fdfef12006-06-27 09:06:36 -07004963#ifdef CONFIG_PM
Jeff Kirsher240b1712006-01-12 16:51:28 -08004964 int retval = 0;
Auke Kok6fdfef12006-06-27 09:06:36 -07004965#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004966
4967 netif_device_detach(netdev);
4968
Auke Kok2db10a02006-06-27 09:06:28 -07004969 if (netif_running(netdev)) {
yzhu16a7d64e2013-11-23 07:07:40 +00004970 int count = E1000_CHECK_RESET_COUNT;
4971
4972 while (test_bit(__E1000_RESETTING, &adapter->flags) && count--)
4973 usleep_range(10000, 20000);
4974
Auke Kok2db10a02006-06-27 09:06:28 -07004975 WARN_ON(test_bit(__E1000_RESETTING, &adapter->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004976 e1000_down(adapter);
Auke Kok2db10a02006-06-27 09:06:28 -07004977 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978
Jesse Brandeburg2f826652006-01-18 13:01:34 -08004979#ifdef CONFIG_PM
Kok, Auke1d33e9c2007-02-16 14:39:28 -08004980 retval = pci_save_state(pdev);
Jesse Brandeburg3a3847e2012-01-04 20:23:33 +00004981 if (retval)
Jesse Brandeburg2f826652006-01-18 13:01:34 -08004982 return retval;
4983#endif
4984
Joe Perches1dc32912008-07-11 15:17:08 -07004985 status = er32(STATUS);
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004986 if (status & E1000_STATUS_LU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004987 wufc &= ~E1000_WUFC_LNKC;
4988
Jesse Brandeburg96838a42006-01-18 13:01:39 -08004989 if (wufc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004990 e1000_setup_rctl(adapter);
Patrick McHardydb0ce502007-11-13 20:54:59 -08004991 e1000_set_rx_mode(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004992
Dean Nelsonb8681792012-01-19 17:47:24 +00004993 rctl = er32(RCTL);
4994
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995 /* turn on all-multi mode if wake on multicast is enabled */
Dean Nelsonb8681792012-01-19 17:47:24 +00004996 if (wufc & E1000_WUFC_MC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004997 rctl |= E1000_RCTL_MPE;
Dean Nelsonb8681792012-01-19 17:47:24 +00004998
4999 /* enable receives in the hardware */
5000 ew32(RCTL, rctl | E1000_RCTL_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005001
Joe Perches1dc32912008-07-11 15:17:08 -07005002 if (hw->mac_type >= e1000_82540) {
5003 ctrl = er32(CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005004 /* advertise wake from D3Cold */
5005 #define E1000_CTRL_ADVD3WUC 0x00100000
5006 /* phy power management enable */
5007 #define E1000_CTRL_EN_PHY_PWR_MGMT 0x00200000
5008 ctrl |= E1000_CTRL_ADVD3WUC |
5009 E1000_CTRL_EN_PHY_PWR_MGMT;
Joe Perches1dc32912008-07-11 15:17:08 -07005010 ew32(CTRL, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005011 }
5012
Joe Perches1dc32912008-07-11 15:17:08 -07005013 if (hw->media_type == e1000_media_type_fiber ||
Jesse Brandeburg1532ece2009-09-25 12:16:14 +00005014 hw->media_type == e1000_media_type_internal_serdes) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005015 /* keep the laser running in D3 */
Joe Perches1dc32912008-07-11 15:17:08 -07005016 ctrl_ext = er32(CTRL_EXT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005017 ctrl_ext |= E1000_CTRL_EXT_SDP7_DATA;
Joe Perches1dc32912008-07-11 15:17:08 -07005018 ew32(CTRL_EXT, ctrl_ext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019 }
5020
Joe Perches1dc32912008-07-11 15:17:08 -07005021 ew32(WUC, E1000_WUC_PME_EN);
5022 ew32(WUFC, wufc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023 } else {
Joe Perches1dc32912008-07-11 15:17:08 -07005024 ew32(WUC, 0);
5025 ew32(WUFC, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005026 }
5027
Jeff Garzik0fccd0e2006-12-15 10:56:10 -05005028 e1000_release_manageability(adapter);
5029
Rafael J. Wysockib43fcd72009-04-15 17:43:24 +00005030 *enable_wake = !!wufc;
5031
Jeff Garzik0fccd0e2006-12-15 10:56:10 -05005032 /* make sure adapter isn't asleep if manageability is enabled */
Rafael J. Wysockib43fcd72009-04-15 17:43:24 +00005033 if (adapter->en_mng_pt)
5034 *enable_wake = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005035
Auke Kokedd106f2006-11-06 08:57:12 -08005036 if (netif_running(netdev))
5037 e1000_free_irq(adapter);
5038
Linus Torvalds1da177e2005-04-16 15:20:36 -07005039 pci_disable_device(pdev);
Jeff Kirsher240b1712006-01-12 16:51:28 -08005040
Linus Torvalds1da177e2005-04-16 15:20:36 -07005041 return 0;
5042}
5043
Jesse Brandeburg2f826652006-01-18 13:01:34 -08005044#ifdef CONFIG_PM
Rafael J. Wysockib43fcd72009-04-15 17:43:24 +00005045static int e1000_suspend(struct pci_dev *pdev, pm_message_t state)
5046{
5047 int retval;
5048 bool wake;
5049
5050 retval = __e1000_shutdown(pdev, &wake);
5051 if (retval)
5052 return retval;
5053
5054 if (wake) {
5055 pci_prepare_to_sleep(pdev);
5056 } else {
5057 pci_wake_from_d3(pdev, false);
5058 pci_set_power_state(pdev, PCI_D3hot);
5059 }
5060
5061 return 0;
5062}
5063
Joe Perches64798842008-07-11 15:17:02 -07005064static int e1000_resume(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005065{
5066 struct net_device *netdev = pci_get_drvdata(pdev);
Malli Chilakala60490fe2005-06-17 17:41:45 -07005067 struct e1000_adapter *adapter = netdev_priv(netdev);
Joe Perches1dc32912008-07-11 15:17:08 -07005068 struct e1000_hw *hw = &adapter->hw;
Joe Perches406874a2008-04-03 10:06:32 -07005069 u32 err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005070
Auke Kokd0e027d2006-04-14 19:04:40 -07005071 pci_set_power_state(pdev, PCI_D0);
Kok, Auke1d33e9c2007-02-16 14:39:28 -08005072 pci_restore_state(pdev);
Nick Nunleydbb5aae2010-02-03 14:49:48 +00005073 pci_save_state(pdev);
Taku Izumi81250292008-07-11 15:17:44 -07005074
5075 if (adapter->need_ioport)
5076 err = pci_enable_device(pdev);
5077 else
5078 err = pci_enable_device_mem(pdev);
Joe Perchesc7be73b2008-07-11 15:17:28 -07005079 if (err) {
Emil Tantilov675ad472010-04-27 14:02:58 +00005080 pr_err("Cannot enable PCI device from suspend\n");
Auke Kok3d1dd8c2006-08-28 14:56:27 -07005081 return err;
5082 }
Malli Chilakalaa4cb8472005-04-28 19:41:28 -07005083 pci_set_master(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005084
Auke Kokd0e027d2006-04-14 19:04:40 -07005085 pci_enable_wake(pdev, PCI_D3hot, 0);
5086 pci_enable_wake(pdev, PCI_D3cold, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005087
Joe Perchesc7be73b2008-07-11 15:17:28 -07005088 if (netif_running(netdev)) {
5089 err = e1000_request_irq(adapter);
5090 if (err)
5091 return err;
5092 }
Auke Kokedd106f2006-11-06 08:57:12 -08005093
5094 e1000_power_up_phy(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095 e1000_reset(adapter);
Joe Perches1dc32912008-07-11 15:17:08 -07005096 ew32(WUS, ~0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005097
Jeff Garzik0fccd0e2006-12-15 10:56:10 -05005098 e1000_init_manageability(adapter);
5099
Jesse Brandeburg96838a42006-01-18 13:01:39 -08005100 if (netif_running(netdev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101 e1000_up(adapter);
5102
5103 netif_device_attach(netdev);
5104
Linus Torvalds1da177e2005-04-16 15:20:36 -07005105 return 0;
5106}
5107#endif
Auke Kokc653e632006-05-23 13:35:57 -07005108
5109static void e1000_shutdown(struct pci_dev *pdev)
5110{
Rafael J. Wysockib43fcd72009-04-15 17:43:24 +00005111 bool wake;
5112
5113 __e1000_shutdown(pdev, &wake);
5114
5115 if (system_state == SYSTEM_POWER_OFF) {
5116 pci_wake_from_d3(pdev, wake);
5117 pci_set_power_state(pdev, PCI_D3hot);
5118 }
Auke Kokc653e632006-05-23 13:35:57 -07005119}
5120
Linus Torvalds1da177e2005-04-16 15:20:36 -07005121#ifdef CONFIG_NET_POLL_CONTROLLER
Jeff Kirsher6cfbd972013-02-09 12:49:21 +00005122/* Polling 'interrupt' - used by things like netconsole to send skbs
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123 * without having to re-enable interrupts. It's not called while
5124 * the interrupt routine is executing.
5125 */
Joe Perches64798842008-07-11 15:17:02 -07005126static void e1000_netpoll(struct net_device *netdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005127{
Malli Chilakala60490fe2005-06-17 17:41:45 -07005128 struct e1000_adapter *adapter = netdev_priv(netdev);
Auke Kokd3d9e482006-07-14 16:14:23 -07005129
Linus Torvalds1da177e2005-04-16 15:20:36 -07005130 disable_irq(adapter->pdev->irq);
David Howells7d12e782006-10-05 14:55:46 +01005131 e1000_intr(adapter->pdev->irq, netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005132 enable_irq(adapter->pdev->irq);
5133}
5134#endif
5135
Auke Kok90267292006-06-08 09:30:24 -07005136/**
5137 * e1000_io_error_detected - called when PCI error is detected
5138 * @pdev: Pointer to PCI device
Jesse Brandeburg120a5d02009-09-25 15:19:46 -07005139 * @state: The current pci connection state
Auke Kok90267292006-06-08 09:30:24 -07005140 *
5141 * This function is called after a PCI bus error affecting
5142 * this device has been detected.
5143 */
Joe Perches64798842008-07-11 15:17:02 -07005144static pci_ers_result_t e1000_io_error_detected(struct pci_dev *pdev,
5145 pci_channel_state_t state)
Auke Kok90267292006-06-08 09:30:24 -07005146{
5147 struct net_device *netdev = pci_get_drvdata(pdev);
Wang Chen4cf16532008-11-12 23:38:14 -08005148 struct e1000_adapter *adapter = netdev_priv(netdev);
Auke Kok90267292006-06-08 09:30:24 -07005149
5150 netif_device_detach(netdev);
5151
Andre Detscheab63302009-06-30 12:46:13 +00005152 if (state == pci_channel_io_perm_failure)
5153 return PCI_ERS_RESULT_DISCONNECT;
5154
Auke Kok90267292006-06-08 09:30:24 -07005155 if (netif_running(netdev))
5156 e1000_down(adapter);
Linas Vepstas72e8d6b2006-09-18 20:58:06 -07005157 pci_disable_device(pdev);
Auke Kok90267292006-06-08 09:30:24 -07005158
5159 /* Request a slot slot reset. */
5160 return PCI_ERS_RESULT_NEED_RESET;
5161}
5162
5163/**
5164 * e1000_io_slot_reset - called after the pci bus has been reset.
5165 * @pdev: Pointer to PCI device
5166 *
5167 * Restart the card from scratch, as if from a cold-boot. Implementation
5168 * resembles the first-half of the e1000_resume routine.
5169 */
5170static pci_ers_result_t e1000_io_slot_reset(struct pci_dev *pdev)
5171{
5172 struct net_device *netdev = pci_get_drvdata(pdev);
Wang Chen4cf16532008-11-12 23:38:14 -08005173 struct e1000_adapter *adapter = netdev_priv(netdev);
Joe Perches1dc32912008-07-11 15:17:08 -07005174 struct e1000_hw *hw = &adapter->hw;
Taku Izumi81250292008-07-11 15:17:44 -07005175 int err;
Auke Kok90267292006-06-08 09:30:24 -07005176
Taku Izumi81250292008-07-11 15:17:44 -07005177 if (adapter->need_ioport)
5178 err = pci_enable_device(pdev);
5179 else
5180 err = pci_enable_device_mem(pdev);
5181 if (err) {
Emil Tantilov675ad472010-04-27 14:02:58 +00005182 pr_err("Cannot re-enable PCI device after reset.\n");
Auke Kok90267292006-06-08 09:30:24 -07005183 return PCI_ERS_RESULT_DISCONNECT;
5184 }
5185 pci_set_master(pdev);
5186
Linas Vepstasdbf38c92006-09-27 12:54:11 -07005187 pci_enable_wake(pdev, PCI_D3hot, 0);
5188 pci_enable_wake(pdev, PCI_D3cold, 0);
Auke Kok90267292006-06-08 09:30:24 -07005189
Auke Kok90267292006-06-08 09:30:24 -07005190 e1000_reset(adapter);
Joe Perches1dc32912008-07-11 15:17:08 -07005191 ew32(WUS, ~0);
Auke Kok90267292006-06-08 09:30:24 -07005192
5193 return PCI_ERS_RESULT_RECOVERED;
5194}
5195
5196/**
5197 * e1000_io_resume - called when traffic can start flowing again.
5198 * @pdev: Pointer to PCI device
5199 *
5200 * This callback is called when the error recovery driver tells us that
5201 * its OK to resume normal operation. Implementation resembles the
5202 * second-half of the e1000_resume routine.
5203 */
5204static void e1000_io_resume(struct pci_dev *pdev)
5205{
5206 struct net_device *netdev = pci_get_drvdata(pdev);
Wang Chen4cf16532008-11-12 23:38:14 -08005207 struct e1000_adapter *adapter = netdev_priv(netdev);
Jeff Garzik0fccd0e2006-12-15 10:56:10 -05005208
5209 e1000_init_manageability(adapter);
Auke Kok90267292006-06-08 09:30:24 -07005210
5211 if (netif_running(netdev)) {
5212 if (e1000_up(adapter)) {
Emil Tantilov675ad472010-04-27 14:02:58 +00005213 pr_info("can't bring device back up after reset\n");
Auke Kok90267292006-06-08 09:30:24 -07005214 return;
5215 }
5216 }
5217
5218 netif_device_attach(netdev);
Auke Kok90267292006-06-08 09:30:24 -07005219}
5220
Linus Torvalds1da177e2005-04-16 15:20:36 -07005221/* e1000_main.c */