blob: d97ad89f5dd22ceb84d96b1f369c9b727b241e41 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*******************************************************************************
2
3
Malli Chilakala26483452005-04-28 19:44:46 -07004 Copyright(c) 1999 - 2005 Intel Corporation. All rights reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2 of the License, or (at your option)
9 any later version.
10
11 This program is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 more details.
15
16 You should have received a copy of the GNU General Public License along with
17 this program; if not, write to the Free Software Foundation, Inc., 59
18 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
20 The full GNU General Public License is included in this distribution in the
21 file called LICENSE.
22
23 Contact Information:
24 Linux NICS <linux.nics@intel.com>
25 Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
26
27*******************************************************************************/
28
29#include "e1000.h"
30
31/* Change Log
Malli Chilakala2b028932005-06-17 17:46:06 -070032 * 6.0.58 4/20/05
33 * o Accepted ethtool cleanup patch from Stephen Hemminger
Malli Chilakala26483452005-04-28 19:44:46 -070034 * 6.0.44+ 2/15/05
35 * o applied Anton's patch to resolve tx hang in hardware
36 * o Applied Andrew Mortons patch - e1000 stops working after resume
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 */
38
39char e1000_driver_name[] = "e1000";
40char e1000_driver_string[] = "Intel(R) PRO/1000 Network Driver";
41#ifndef CONFIG_E1000_NAPI
42#define DRIVERNAPI
43#else
44#define DRIVERNAPI "-NAPI"
45#endif
Mallikarjuna R Chilakala07b8fed2005-10-19 10:40:08 -040046#define DRV_VERSION "6.1.16-k2"DRIVERNAPI
Linus Torvalds1da177e2005-04-16 15:20:36 -070047char e1000_driver_version[] = DRV_VERSION;
Malli Chilakala2b028932005-06-17 17:46:06 -070048char e1000_copyright[] = "Copyright (c) 1999-2005 Intel Corporation.";
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50/* e1000_pci_tbl - PCI Device ID Table
51 *
52 * Last entry must be all 0s
53 *
54 * Macro expands to...
55 * {PCI_DEVICE(PCI_VENDOR_ID_INTEL, device_id)}
56 */
57static struct pci_device_id e1000_pci_tbl[] = {
58 INTEL_E1000_ETHERNET_DEVICE(0x1000),
59 INTEL_E1000_ETHERNET_DEVICE(0x1001),
60 INTEL_E1000_ETHERNET_DEVICE(0x1004),
61 INTEL_E1000_ETHERNET_DEVICE(0x1008),
62 INTEL_E1000_ETHERNET_DEVICE(0x1009),
63 INTEL_E1000_ETHERNET_DEVICE(0x100C),
64 INTEL_E1000_ETHERNET_DEVICE(0x100D),
65 INTEL_E1000_ETHERNET_DEVICE(0x100E),
66 INTEL_E1000_ETHERNET_DEVICE(0x100F),
67 INTEL_E1000_ETHERNET_DEVICE(0x1010),
68 INTEL_E1000_ETHERNET_DEVICE(0x1011),
69 INTEL_E1000_ETHERNET_DEVICE(0x1012),
70 INTEL_E1000_ETHERNET_DEVICE(0x1013),
71 INTEL_E1000_ETHERNET_DEVICE(0x1014),
72 INTEL_E1000_ETHERNET_DEVICE(0x1015),
73 INTEL_E1000_ETHERNET_DEVICE(0x1016),
74 INTEL_E1000_ETHERNET_DEVICE(0x1017),
75 INTEL_E1000_ETHERNET_DEVICE(0x1018),
76 INTEL_E1000_ETHERNET_DEVICE(0x1019),
Malli Chilakala26483452005-04-28 19:44:46 -070077 INTEL_E1000_ETHERNET_DEVICE(0x101A),
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 INTEL_E1000_ETHERNET_DEVICE(0x101D),
79 INTEL_E1000_ETHERNET_DEVICE(0x101E),
80 INTEL_E1000_ETHERNET_DEVICE(0x1026),
81 INTEL_E1000_ETHERNET_DEVICE(0x1027),
82 INTEL_E1000_ETHERNET_DEVICE(0x1028),
Mallikarjuna R Chilakala07b8fed2005-10-19 10:40:08 -040083 INTEL_E1000_ETHERNET_DEVICE(0x105E),
84 INTEL_E1000_ETHERNET_DEVICE(0x105F),
85 INTEL_E1000_ETHERNET_DEVICE(0x1060),
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 INTEL_E1000_ETHERNET_DEVICE(0x1075),
87 INTEL_E1000_ETHERNET_DEVICE(0x1076),
88 INTEL_E1000_ETHERNET_DEVICE(0x1077),
89 INTEL_E1000_ETHERNET_DEVICE(0x1078),
90 INTEL_E1000_ETHERNET_DEVICE(0x1079),
91 INTEL_E1000_ETHERNET_DEVICE(0x107A),
92 INTEL_E1000_ETHERNET_DEVICE(0x107B),
93 INTEL_E1000_ETHERNET_DEVICE(0x107C),
Mallikarjuna R Chilakala07b8fed2005-10-19 10:40:08 -040094 INTEL_E1000_ETHERNET_DEVICE(0x107D),
95 INTEL_E1000_ETHERNET_DEVICE(0x107E),
96 INTEL_E1000_ETHERNET_DEVICE(0x107F),
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 INTEL_E1000_ETHERNET_DEVICE(0x108A),
Malli Chilakala26483452005-04-28 19:44:46 -070098 INTEL_E1000_ETHERNET_DEVICE(0x108B),
99 INTEL_E1000_ETHERNET_DEVICE(0x108C),
Mallikarjuna R Chilakala07b8fed2005-10-19 10:40:08 -0400100 INTEL_E1000_ETHERNET_DEVICE(0x109A),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 /* required last entry */
102 {0,}
103};
104
105MODULE_DEVICE_TABLE(pci, e1000_pci_tbl);
106
107int e1000_up(struct e1000_adapter *adapter);
108void e1000_down(struct e1000_adapter *adapter);
109void e1000_reset(struct e1000_adapter *adapter);
110int e1000_set_spd_dplx(struct e1000_adapter *adapter, uint16_t spddplx);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -0400111int e1000_setup_all_tx_resources(struct e1000_adapter *adapter);
112int e1000_setup_all_rx_resources(struct e1000_adapter *adapter);
113void e1000_free_all_tx_resources(struct e1000_adapter *adapter);
114void e1000_free_all_rx_resources(struct e1000_adapter *adapter);
115int e1000_setup_tx_resources(struct e1000_adapter *adapter,
116 struct e1000_tx_ring *txdr);
117int e1000_setup_rx_resources(struct e1000_adapter *adapter,
118 struct e1000_rx_ring *rxdr);
119void e1000_free_tx_resources(struct e1000_adapter *adapter,
120 struct e1000_tx_ring *tx_ring);
121void e1000_free_rx_resources(struct e1000_adapter *adapter,
122 struct e1000_rx_ring *rx_ring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123void e1000_update_stats(struct e1000_adapter *adapter);
124
125/* Local Function Prototypes */
126
127static int e1000_init_module(void);
128static void e1000_exit_module(void);
129static int e1000_probe(struct pci_dev *pdev, const struct pci_device_id *ent);
130static void __devexit e1000_remove(struct pci_dev *pdev);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -0400131static int e1000_alloc_queues(struct e1000_adapter *adapter);
132#ifdef CONFIG_E1000_MQ
133static void e1000_setup_queue_mapping(struct e1000_adapter *adapter);
134#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135static int e1000_sw_init(struct e1000_adapter *adapter);
136static int e1000_open(struct net_device *netdev);
137static int e1000_close(struct net_device *netdev);
138static void e1000_configure_tx(struct e1000_adapter *adapter);
139static void e1000_configure_rx(struct e1000_adapter *adapter);
140static void e1000_setup_rctl(struct e1000_adapter *adapter);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -0400141static void e1000_clean_all_tx_rings(struct e1000_adapter *adapter);
142static void e1000_clean_all_rx_rings(struct e1000_adapter *adapter);
143static void e1000_clean_tx_ring(struct e1000_adapter *adapter,
144 struct e1000_tx_ring *tx_ring);
145static void e1000_clean_rx_ring(struct e1000_adapter *adapter,
146 struct e1000_rx_ring *rx_ring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147static void e1000_set_multi(struct net_device *netdev);
148static void e1000_update_phy_info(unsigned long data);
149static void e1000_watchdog(unsigned long data);
150static void e1000_watchdog_task(struct e1000_adapter *adapter);
151static void e1000_82547_tx_fifo_stall(unsigned long data);
152static int e1000_xmit_frame(struct sk_buff *skb, struct net_device *netdev);
153static struct net_device_stats * e1000_get_stats(struct net_device *netdev);
154static int e1000_change_mtu(struct net_device *netdev, int new_mtu);
155static int e1000_set_mac(struct net_device *netdev, void *p);
156static irqreturn_t e1000_intr(int irq, void *data, struct pt_regs *regs);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -0400157static boolean_t e1000_clean_tx_irq(struct e1000_adapter *adapter,
158 struct e1000_tx_ring *tx_ring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159#ifdef CONFIG_E1000_NAPI
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -0400160static int e1000_clean(struct net_device *poll_dev, int *budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161static boolean_t e1000_clean_rx_irq(struct e1000_adapter *adapter,
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -0400162 struct e1000_rx_ring *rx_ring,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 int *work_done, int work_to_do);
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700164static boolean_t e1000_clean_rx_irq_ps(struct e1000_adapter *adapter,
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -0400165 struct e1000_rx_ring *rx_ring,
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700166 int *work_done, int work_to_do);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167#else
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -0400168static boolean_t e1000_clean_rx_irq(struct e1000_adapter *adapter,
169 struct e1000_rx_ring *rx_ring);
170static boolean_t e1000_clean_rx_irq_ps(struct e1000_adapter *adapter,
171 struct e1000_rx_ring *rx_ring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172#endif
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -0400173static void e1000_alloc_rx_buffers(struct e1000_adapter *adapter,
174 struct e1000_rx_ring *rx_ring);
175static void e1000_alloc_rx_buffers_ps(struct e1000_adapter *adapter,
176 struct e1000_rx_ring *rx_ring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177static int e1000_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd);
178static int e1000_mii_ioctl(struct net_device *netdev, struct ifreq *ifr,
179 int cmd);
180void e1000_set_ethtool_ops(struct net_device *netdev);
181static void e1000_enter_82542_rst(struct e1000_adapter *adapter);
182static void e1000_leave_82542_rst(struct e1000_adapter *adapter);
183static void e1000_tx_timeout(struct net_device *dev);
184static void e1000_tx_timeout_task(struct net_device *dev);
185static void e1000_smartspeed(struct e1000_adapter *adapter);
186static inline int e1000_82547_fifo_workaround(struct e1000_adapter *adapter,
187 struct sk_buff *skb);
188
189static void e1000_vlan_rx_register(struct net_device *netdev, struct vlan_group *grp);
190static void e1000_vlan_rx_add_vid(struct net_device *netdev, uint16_t vid);
191static void e1000_vlan_rx_kill_vid(struct net_device *netdev, uint16_t vid);
192static void e1000_restore_vlan(struct e1000_adapter *adapter);
193
Pavel Machek829ca9a2005-09-03 15:56:56 -0700194static int e1000_suspend(struct pci_dev *pdev, pm_message_t state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195#ifdef CONFIG_PM
196static int e1000_resume(struct pci_dev *pdev);
197#endif
198
199#ifdef CONFIG_NET_POLL_CONTROLLER
200/* for netdump / net console */
201static void e1000_netpoll (struct net_device *netdev);
202#endif
203
Mallikarjuna R Chilakala24025e4ec2005-10-04 07:03:23 -0400204#ifdef CONFIG_E1000_MQ
205/* for multiple Rx queues */
206void e1000_rx_schedule(void *data);
207#endif
208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209/* Exported from other modules */
210
211extern void e1000_check_options(struct e1000_adapter *adapter);
212
213static struct pci_driver e1000_driver = {
214 .name = e1000_driver_name,
215 .id_table = e1000_pci_tbl,
216 .probe = e1000_probe,
217 .remove = __devexit_p(e1000_remove),
218 /* Power Managment Hooks */
219#ifdef CONFIG_PM
220 .suspend = e1000_suspend,
221 .resume = e1000_resume
222#endif
223};
224
225MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
226MODULE_DESCRIPTION("Intel(R) PRO/1000 Network Driver");
227MODULE_LICENSE("GPL");
228MODULE_VERSION(DRV_VERSION);
229
230static int debug = NETIF_MSG_DRV | NETIF_MSG_PROBE;
231module_param(debug, int, 0);
232MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
233
234/**
235 * e1000_init_module - Driver Registration Routine
236 *
237 * e1000_init_module is the first routine called when the driver is
238 * loaded. All it does is register with the PCI subsystem.
239 **/
240
241static int __init
242e1000_init_module(void)
243{
244 int ret;
245 printk(KERN_INFO "%s - version %s\n",
246 e1000_driver_string, e1000_driver_version);
247
248 printk(KERN_INFO "%s\n", e1000_copyright);
249
250 ret = pci_module_init(&e1000_driver);
Tony Luck8b378def2005-07-28 01:07:38 -0700251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 return ret;
253}
254
255module_init(e1000_init_module);
256
257/**
258 * e1000_exit_module - Driver Exit Cleanup Routine
259 *
260 * e1000_exit_module is called just before the driver is removed
261 * from memory.
262 **/
263
264static void __exit
265e1000_exit_module(void)
266{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 pci_unregister_driver(&e1000_driver);
268}
269
270module_exit(e1000_exit_module);
271
272/**
273 * e1000_irq_disable - Mask off interrupt generation on the NIC
274 * @adapter: board private structure
275 **/
276
277static inline void
278e1000_irq_disable(struct e1000_adapter *adapter)
279{
280 atomic_inc(&adapter->irq_sem);
281 E1000_WRITE_REG(&adapter->hw, IMC, ~0);
282 E1000_WRITE_FLUSH(&adapter->hw);
283 synchronize_irq(adapter->pdev->irq);
284}
285
286/**
287 * e1000_irq_enable - Enable default interrupt generation settings
288 * @adapter: board private structure
289 **/
290
291static inline void
292e1000_irq_enable(struct e1000_adapter *adapter)
293{
294 if(likely(atomic_dec_and_test(&adapter->irq_sem))) {
295 E1000_WRITE_REG(&adapter->hw, IMS, IMS_ENABLE_MASK);
296 E1000_WRITE_FLUSH(&adapter->hw);
297 }
298}
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700299void
300e1000_update_mng_vlan(struct e1000_adapter *adapter)
301{
302 struct net_device *netdev = adapter->netdev;
303 uint16_t vid = adapter->hw.mng_cookie.vlan_id;
304 uint16_t old_vid = adapter->mng_vlan_id;
305 if(adapter->vlgrp) {
306 if(!adapter->vlgrp->vlan_devices[vid]) {
307 if(adapter->hw.mng_cookie.status &
308 E1000_MNG_DHCP_COOKIE_STATUS_VLAN_SUPPORT) {
309 e1000_vlan_rx_add_vid(netdev, vid);
310 adapter->mng_vlan_id = vid;
311 } else
312 adapter->mng_vlan_id = E1000_MNG_VLAN_NONE;
313
314 if((old_vid != (uint16_t)E1000_MNG_VLAN_NONE) &&
315 (vid != old_vid) &&
316 !adapter->vlgrp->vlan_devices[old_vid])
317 e1000_vlan_rx_kill_vid(netdev, old_vid);
318 }
319 }
320}
321
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322int
323e1000_up(struct e1000_adapter *adapter)
324{
325 struct net_device *netdev = adapter->netdev;
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -0400326 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328 /* hardware has been reset, we need to reload some things */
329
330 /* Reset the PHY if it was previously powered down */
331 if(adapter->hw.media_type == e1000_media_type_copper) {
332 uint16_t mii_reg;
333 e1000_read_phy_reg(&adapter->hw, PHY_CTRL, &mii_reg);
334 if(mii_reg & MII_CR_POWER_DOWN)
335 e1000_phy_reset(&adapter->hw);
336 }
337
338 e1000_set_multi(netdev);
339
340 e1000_restore_vlan(adapter);
341
342 e1000_configure_tx(adapter);
343 e1000_setup_rctl(adapter);
344 e1000_configure_rx(adapter);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -0400345 for (i = 0; i < adapter->num_queues; i++)
346 adapter->alloc_rx_buf(adapter, &adapter->rx_ring[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
Malli Chilakalafa4f7ef2005-04-28 19:39:13 -0700348#ifdef CONFIG_PCI_MSI
349 if(adapter->hw.mac_type > e1000_82547_rev_2) {
350 adapter->have_msi = TRUE;
351 if((err = pci_enable_msi(adapter->pdev))) {
352 DPRINTK(PROBE, ERR,
353 "Unable to allocate MSI interrupt Error: %d\n", err);
354 adapter->have_msi = FALSE;
355 }
356 }
357#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 if((err = request_irq(adapter->pdev->irq, &e1000_intr,
359 SA_SHIRQ | SA_SAMPLE_RANDOM,
Malli Chilakala26483452005-04-28 19:44:46 -0700360 netdev->name, netdev))) {
361 DPRINTK(PROBE, ERR,
362 "Unable to allocate interrupt Error: %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 return err;
Malli Chilakala26483452005-04-28 19:44:46 -0700364 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
366 mod_timer(&adapter->watchdog_timer, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
368#ifdef CONFIG_E1000_NAPI
369 netif_poll_enable(netdev);
370#endif
Malli Chilakala5de55622005-04-28 19:39:30 -0700371 e1000_irq_enable(adapter);
372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 return 0;
374}
375
376void
377e1000_down(struct e1000_adapter *adapter)
378{
379 struct net_device *netdev = adapter->netdev;
380
381 e1000_irq_disable(adapter);
Mallikarjuna R Chilakala24025e4ec2005-10-04 07:03:23 -0400382#ifdef CONFIG_E1000_MQ
383 while (atomic_read(&adapter->rx_sched_call_data.count) != 0);
384#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 free_irq(adapter->pdev->irq, netdev);
Malli Chilakalafa4f7ef2005-04-28 19:39:13 -0700386#ifdef CONFIG_PCI_MSI
387 if(adapter->hw.mac_type > e1000_82547_rev_2 &&
388 adapter->have_msi == TRUE)
389 pci_disable_msi(adapter->pdev);
390#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 del_timer_sync(&adapter->tx_fifo_stall_timer);
392 del_timer_sync(&adapter->watchdog_timer);
393 del_timer_sync(&adapter->phy_info_timer);
394
395#ifdef CONFIG_E1000_NAPI
396 netif_poll_disable(netdev);
397#endif
398 adapter->link_speed = 0;
399 adapter->link_duplex = 0;
400 netif_carrier_off(netdev);
401 netif_stop_queue(netdev);
402
403 e1000_reset(adapter);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -0400404 e1000_clean_all_tx_rings(adapter);
405 e1000_clean_all_rx_rings(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Mallikarjuna R Chilakala07b8fed2005-10-19 10:40:08 -0400407 /* If WoL is not enabled and management mode is not IAMT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 * Power down the PHY so no link is implied when interface is down */
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700409 if(!adapter->wol && adapter->hw.mac_type >= e1000_82540 &&
410 adapter->hw.media_type == e1000_media_type_copper &&
411 !e1000_check_mng_mode(&adapter->hw) &&
412 !(E1000_READ_REG(&adapter->hw, MANC) & E1000_MANC_SMBUS_EN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 uint16_t mii_reg;
414 e1000_read_phy_reg(&adapter->hw, PHY_CTRL, &mii_reg);
415 mii_reg |= MII_CR_POWER_DOWN;
416 e1000_write_phy_reg(&adapter->hw, PHY_CTRL, mii_reg);
Malli Chilakala4e48a2b2005-04-28 19:39:53 -0700417 mdelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 }
419}
420
421void
422e1000_reset(struct e1000_adapter *adapter)
423{
Malli Chilakala1125ecb2005-04-28 19:44:25 -0700424 struct net_device *netdev = adapter->netdev;
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700425 uint32_t pba, manc;
Malli Chilakala1125ecb2005-04-28 19:44:25 -0700426 uint16_t fc_high_water_mark = E1000_FC_HIGH_DIFF;
427 uint16_t fc_low_water_mark = E1000_FC_LOW_DIFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
429 /* Repartition Pba for greater than 9k mtu
430 * To take effect CTRL.RST is required.
431 */
432
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700433 switch (adapter->hw.mac_type) {
434 case e1000_82547:
Malli Chilakala0e6ef3e2005-04-28 19:44:14 -0700435 case e1000_82547_rev_2:
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700436 pba = E1000_PBA_30K;
437 break;
Mallikarjuna R Chilakala868d5302005-10-04 06:58:59 -0400438 case e1000_82571:
439 case e1000_82572:
440 pba = E1000_PBA_38K;
441 break;
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700442 case e1000_82573:
443 pba = E1000_PBA_12K;
444 break;
445 default:
446 pba = E1000_PBA_48K;
447 break;
448 }
449
Malli Chilakala1125ecb2005-04-28 19:44:25 -0700450 if((adapter->hw.mac_type != e1000_82573) &&
451 (adapter->rx_buffer_len > E1000_RXBUFFER_8192)) {
452 pba -= 8; /* allocate more FIFO for Tx */
453 /* send an XOFF when there is enough space in the
454 * Rx FIFO to hold one extra full size Rx packet
455 */
456 fc_high_water_mark = netdev->mtu + ENET_HEADER_SIZE +
457 ETHERNET_FCS_SIZE + 1;
458 fc_low_water_mark = fc_high_water_mark + 8;
459 }
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700460
461
462 if(adapter->hw.mac_type == e1000_82547) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 adapter->tx_fifo_head = 0;
464 adapter->tx_head_addr = pba << E1000_TX_HEAD_ADDR_SHIFT;
465 adapter->tx_fifo_size =
466 (E1000_PBA_40K - pba) << E1000_PBA_BYTES_SHIFT;
467 atomic_set(&adapter->tx_fifo_stall, 0);
468 }
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 E1000_WRITE_REG(&adapter->hw, PBA, pba);
471
472 /* flow control settings */
473 adapter->hw.fc_high_water = (pba << E1000_PBA_BYTES_SHIFT) -
Malli Chilakala1125ecb2005-04-28 19:44:25 -0700474 fc_high_water_mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 adapter->hw.fc_low_water = (pba << E1000_PBA_BYTES_SHIFT) -
Malli Chilakala1125ecb2005-04-28 19:44:25 -0700476 fc_low_water_mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 adapter->hw.fc_pause_time = E1000_FC_PAUSE_TIME;
478 adapter->hw.fc_send_xon = 1;
479 adapter->hw.fc = adapter->hw.original_fc;
480
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700481 /* Allow time for pending master requests to run */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 e1000_reset_hw(&adapter->hw);
483 if(adapter->hw.mac_type >= e1000_82544)
484 E1000_WRITE_REG(&adapter->hw, WUC, 0);
485 if(e1000_init_hw(&adapter->hw))
486 DPRINTK(PROBE, ERR, "Hardware Error\n");
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700487 e1000_update_mng_vlan(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 /* Enable h/w to recognize an 802.1Q VLAN Ethernet packet */
489 E1000_WRITE_REG(&adapter->hw, VET, ETHERNET_IEEE_VLAN_TYPE);
490
491 e1000_reset_adaptive(&adapter->hw);
492 e1000_phy_get_info(&adapter->hw, &adapter->phy_info);
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700493 if (adapter->en_mng_pt) {
494 manc = E1000_READ_REG(&adapter->hw, MANC);
495 manc |= (E1000_MANC_ARP_EN | E1000_MANC_EN_MNG2HOST);
496 E1000_WRITE_REG(&adapter->hw, MANC, manc);
497 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498}
499
500/**
501 * e1000_probe - Device Initialization Routine
502 * @pdev: PCI device information struct
503 * @ent: entry in e1000_pci_tbl
504 *
505 * Returns 0 on success, negative on failure
506 *
507 * e1000_probe initializes an adapter identified by a pci_dev structure.
508 * The OS initialization, configuring of the adapter private structure,
509 * and a hardware reset occur.
510 **/
511
512static int __devinit
513e1000_probe(struct pci_dev *pdev,
514 const struct pci_device_id *ent)
515{
516 struct net_device *netdev;
517 struct e1000_adapter *adapter;
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700518 unsigned long mmio_start, mmio_len;
Mallikarjuna R Chilakala868d5302005-10-04 06:58:59 -0400519 uint32_t ctrl_ext;
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700520 uint32_t swsm;
521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 static int cards_found = 0;
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700523 int i, err, pci_using_dac;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 uint16_t eeprom_data;
525 uint16_t eeprom_apme_mask = E1000_EEPROM_APME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 if((err = pci_enable_device(pdev)))
527 return err;
528
529 if(!(err = pci_set_dma_mask(pdev, DMA_64BIT_MASK))) {
530 pci_using_dac = 1;
531 } else {
532 if((err = pci_set_dma_mask(pdev, DMA_32BIT_MASK))) {
533 E1000_ERR("No usable DMA configuration, aborting\n");
534 return err;
535 }
536 pci_using_dac = 0;
537 }
538
539 if((err = pci_request_regions(pdev, e1000_driver_name)))
540 return err;
541
542 pci_set_master(pdev);
543
544 netdev = alloc_etherdev(sizeof(struct e1000_adapter));
545 if(!netdev) {
546 err = -ENOMEM;
547 goto err_alloc_etherdev;
548 }
549
550 SET_MODULE_OWNER(netdev);
551 SET_NETDEV_DEV(netdev, &pdev->dev);
552
553 pci_set_drvdata(pdev, netdev);
Malli Chilakala60490fe2005-06-17 17:41:45 -0700554 adapter = netdev_priv(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 adapter->netdev = netdev;
556 adapter->pdev = pdev;
557 adapter->hw.back = adapter;
558 adapter->msg_enable = (1 << debug) - 1;
559
560 mmio_start = pci_resource_start(pdev, BAR_0);
561 mmio_len = pci_resource_len(pdev, BAR_0);
562
563 adapter->hw.hw_addr = ioremap(mmio_start, mmio_len);
564 if(!adapter->hw.hw_addr) {
565 err = -EIO;
566 goto err_ioremap;
567 }
568
569 for(i = BAR_1; i <= BAR_5; i++) {
570 if(pci_resource_len(pdev, i) == 0)
571 continue;
572 if(pci_resource_flags(pdev, i) & IORESOURCE_IO) {
573 adapter->hw.io_base = pci_resource_start(pdev, i);
574 break;
575 }
576 }
577
578 netdev->open = &e1000_open;
579 netdev->stop = &e1000_close;
580 netdev->hard_start_xmit = &e1000_xmit_frame;
581 netdev->get_stats = &e1000_get_stats;
582 netdev->set_multicast_list = &e1000_set_multi;
583 netdev->set_mac_address = &e1000_set_mac;
584 netdev->change_mtu = &e1000_change_mtu;
585 netdev->do_ioctl = &e1000_ioctl;
586 e1000_set_ethtool_ops(netdev);
587 netdev->tx_timeout = &e1000_tx_timeout;
588 netdev->watchdog_timeo = 5 * HZ;
589#ifdef CONFIG_E1000_NAPI
590 netdev->poll = &e1000_clean;
591 netdev->weight = 64;
592#endif
593 netdev->vlan_rx_register = e1000_vlan_rx_register;
594 netdev->vlan_rx_add_vid = e1000_vlan_rx_add_vid;
595 netdev->vlan_rx_kill_vid = e1000_vlan_rx_kill_vid;
596#ifdef CONFIG_NET_POLL_CONTROLLER
597 netdev->poll_controller = e1000_netpoll;
598#endif
599 strcpy(netdev->name, pci_name(pdev));
600
601 netdev->mem_start = mmio_start;
602 netdev->mem_end = mmio_start + mmio_len;
603 netdev->base_addr = adapter->hw.io_base;
604
605 adapter->bd_number = cards_found;
606
607 /* setup the private structure */
608
609 if((err = e1000_sw_init(adapter)))
610 goto err_sw_init;
611
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700612 if((err = e1000_check_phy_reset_block(&adapter->hw)))
613 DPRINTK(PROBE, INFO, "PHY reset is blocked due to SOL/IDER session.\n");
614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 if(adapter->hw.mac_type >= e1000_82543) {
616 netdev->features = NETIF_F_SG |
617 NETIF_F_HW_CSUM |
618 NETIF_F_HW_VLAN_TX |
619 NETIF_F_HW_VLAN_RX |
620 NETIF_F_HW_VLAN_FILTER;
621 }
622
623#ifdef NETIF_F_TSO
624 if((adapter->hw.mac_type >= e1000_82544) &&
625 (adapter->hw.mac_type != e1000_82547))
626 netdev->features |= NETIF_F_TSO;
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700627
628#ifdef NETIF_F_TSO_IPV6
629 if(adapter->hw.mac_type > e1000_82547_rev_2)
630 netdev->features |= NETIF_F_TSO_IPV6;
631#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632#endif
633 if(pci_using_dac)
634 netdev->features |= NETIF_F_HIGHDMA;
635
636 /* hard_start_xmit is safe against parallel locking */
637 netdev->features |= NETIF_F_LLTX;
638
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700639 adapter->en_mng_pt = e1000_enable_mng_pass_thru(&adapter->hw);
640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 /* before reading the EEPROM, reset the controller to
642 * put the device in a known good starting state */
643
644 e1000_reset_hw(&adapter->hw);
645
646 /* make sure the EEPROM is good */
647
648 if(e1000_validate_eeprom_checksum(&adapter->hw) < 0) {
649 DPRINTK(PROBE, ERR, "The EEPROM Checksum Is Not Valid\n");
650 err = -EIO;
651 goto err_eeprom;
652 }
653
654 /* copy the MAC address out of the EEPROM */
655
Malli Chilakala26483452005-04-28 19:44:46 -0700656 if(e1000_read_mac_addr(&adapter->hw))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 DPRINTK(PROBE, ERR, "EEPROM Read Error\n");
658 memcpy(netdev->dev_addr, adapter->hw.mac_addr, netdev->addr_len);
John W. Linville9beb0ac2005-09-12 10:48:55 -0400659 memcpy(netdev->perm_addr, adapter->hw.mac_addr, netdev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
John W. Linville9beb0ac2005-09-12 10:48:55 -0400661 if(!is_valid_ether_addr(netdev->perm_addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 DPRINTK(PROBE, ERR, "Invalid MAC Address\n");
663 err = -EIO;
664 goto err_eeprom;
665 }
666
667 e1000_read_part_num(&adapter->hw, &(adapter->part_num));
668
669 e1000_get_bus_info(&adapter->hw);
670
671 init_timer(&adapter->tx_fifo_stall_timer);
672 adapter->tx_fifo_stall_timer.function = &e1000_82547_tx_fifo_stall;
673 adapter->tx_fifo_stall_timer.data = (unsigned long) adapter;
674
675 init_timer(&adapter->watchdog_timer);
676 adapter->watchdog_timer.function = &e1000_watchdog;
677 adapter->watchdog_timer.data = (unsigned long) adapter;
678
679 INIT_WORK(&adapter->watchdog_task,
680 (void (*)(void *))e1000_watchdog_task, adapter);
681
682 init_timer(&adapter->phy_info_timer);
683 adapter->phy_info_timer.function = &e1000_update_phy_info;
684 adapter->phy_info_timer.data = (unsigned long) adapter;
685
686 INIT_WORK(&adapter->tx_timeout_task,
687 (void (*)(void *))e1000_tx_timeout_task, netdev);
688
689 /* we're going to reset, so assume we have no link for now */
690
691 netif_carrier_off(netdev);
692 netif_stop_queue(netdev);
693
694 e1000_check_options(adapter);
695
696 /* Initial Wake on LAN setting
697 * If APM wake is enabled in the EEPROM,
698 * enable the ACPI Magic Packet filter
699 */
700
701 switch(adapter->hw.mac_type) {
702 case e1000_82542_rev2_0:
703 case e1000_82542_rev2_1:
704 case e1000_82543:
705 break;
706 case e1000_82544:
707 e1000_read_eeprom(&adapter->hw,
708 EEPROM_INIT_CONTROL2_REG, 1, &eeprom_data);
709 eeprom_apme_mask = E1000_EEPROM_82544_APM;
710 break;
711 case e1000_82546:
712 case e1000_82546_rev_3:
713 if((E1000_READ_REG(&adapter->hw, STATUS) & E1000_STATUS_FUNC_1)
714 && (adapter->hw.media_type == e1000_media_type_copper)) {
715 e1000_read_eeprom(&adapter->hw,
716 EEPROM_INIT_CONTROL3_PORT_B, 1, &eeprom_data);
717 break;
718 }
719 /* Fall Through */
720 default:
721 e1000_read_eeprom(&adapter->hw,
722 EEPROM_INIT_CONTROL3_PORT_A, 1, &eeprom_data);
723 break;
724 }
725 if(eeprom_data & eeprom_apme_mask)
726 adapter->wol |= E1000_WUFC_MAG;
727
728 /* reset the hardware with the new settings */
729 e1000_reset(adapter);
730
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700731 /* Let firmware know the driver has taken over */
732 switch(adapter->hw.mac_type) {
Mallikarjuna R Chilakala868d5302005-10-04 06:58:59 -0400733 case e1000_82571:
734 case e1000_82572:
735 ctrl_ext = E1000_READ_REG(&adapter->hw, CTRL_EXT);
736 E1000_WRITE_REG(&adapter->hw, CTRL_EXT,
737 ctrl_ext | E1000_CTRL_EXT_DRV_LOAD);
738 break;
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700739 case e1000_82573:
740 swsm = E1000_READ_REG(&adapter->hw, SWSM);
741 E1000_WRITE_REG(&adapter->hw, SWSM,
742 swsm | E1000_SWSM_DRV_LOAD);
743 break;
744 default:
745 break;
746 }
747
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 strcpy(netdev->name, "eth%d");
749 if((err = register_netdev(netdev)))
750 goto err_register;
751
752 DPRINTK(PROBE, INFO, "Intel(R) PRO/1000 Network Connection\n");
753
754 cards_found++;
755 return 0;
756
757err_register:
758err_sw_init:
759err_eeprom:
760 iounmap(adapter->hw.hw_addr);
761err_ioremap:
762 free_netdev(netdev);
763err_alloc_etherdev:
764 pci_release_regions(pdev);
765 return err;
766}
767
768/**
769 * e1000_remove - Device Removal Routine
770 * @pdev: PCI device information struct
771 *
772 * e1000_remove is called by the PCI subsystem to alert the driver
773 * that it should release a PCI device. The could be caused by a
774 * Hot-Plug event, or because the driver is going to be removed from
775 * memory.
776 **/
777
778static void __devexit
779e1000_remove(struct pci_dev *pdev)
780{
781 struct net_device *netdev = pci_get_drvdata(pdev);
Malli Chilakala60490fe2005-06-17 17:41:45 -0700782 struct e1000_adapter *adapter = netdev_priv(netdev);
Mallikarjuna R Chilakala868d5302005-10-04 06:58:59 -0400783 uint32_t ctrl_ext;
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700784 uint32_t manc, swsm;
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -0400785#ifdef CONFIG_E1000_NAPI
786 int i;
787#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
Jeff Garzikbe2b28e2005-10-04 07:13:43 -0400789 flush_scheduled_work();
790
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 if(adapter->hw.mac_type >= e1000_82540 &&
792 adapter->hw.media_type == e1000_media_type_copper) {
793 manc = E1000_READ_REG(&adapter->hw, MANC);
794 if(manc & E1000_MANC_SMBUS_EN) {
795 manc |= E1000_MANC_ARP_EN;
796 E1000_WRITE_REG(&adapter->hw, MANC, manc);
797 }
798 }
799
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700800 switch(adapter->hw.mac_type) {
Mallikarjuna R Chilakala868d5302005-10-04 06:58:59 -0400801 case e1000_82571:
802 case e1000_82572:
803 ctrl_ext = E1000_READ_REG(&adapter->hw, CTRL_EXT);
804 E1000_WRITE_REG(&adapter->hw, CTRL_EXT,
805 ctrl_ext & ~E1000_CTRL_EXT_DRV_LOAD);
806 break;
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700807 case e1000_82573:
808 swsm = E1000_READ_REG(&adapter->hw, SWSM);
809 E1000_WRITE_REG(&adapter->hw, SWSM,
810 swsm & ~E1000_SWSM_DRV_LOAD);
811 break;
812
813 default:
814 break;
815 }
816
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 unregister_netdev(netdev);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -0400818#ifdef CONFIG_E1000_NAPI
819 for (i = 0; i < adapter->num_queues; i++)
820 __dev_put(&adapter->polling_netdev[i]);
821#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700823 if(!e1000_check_phy_reset_block(&adapter->hw))
824 e1000_phy_hw_reset(&adapter->hw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
Mallikarjuna R Chilakala24025e4ec2005-10-04 07:03:23 -0400826 kfree(adapter->tx_ring);
827 kfree(adapter->rx_ring);
828#ifdef CONFIG_E1000_NAPI
829 kfree(adapter->polling_netdev);
830#endif
831
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 iounmap(adapter->hw.hw_addr);
833 pci_release_regions(pdev);
834
Mallikarjuna R Chilakala24025e4ec2005-10-04 07:03:23 -0400835#ifdef CONFIG_E1000_MQ
836 free_percpu(adapter->cpu_netdev);
837 free_percpu(adapter->cpu_tx_ring);
838#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 free_netdev(netdev);
840
841 pci_disable_device(pdev);
842}
843
844/**
845 * e1000_sw_init - Initialize general software structures (struct e1000_adapter)
846 * @adapter: board private structure to initialize
847 *
848 * e1000_sw_init initializes the Adapter private data structure.
849 * Fields are initialized based on PCI device information and
850 * OS network device settings (MTU size).
851 **/
852
853static int __devinit
854e1000_sw_init(struct e1000_adapter *adapter)
855{
856 struct e1000_hw *hw = &adapter->hw;
857 struct net_device *netdev = adapter->netdev;
858 struct pci_dev *pdev = adapter->pdev;
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -0400859#ifdef CONFIG_E1000_NAPI
860 int i;
861#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
863 /* PCI config space info */
864
865 hw->vendor_id = pdev->vendor;
866 hw->device_id = pdev->device;
867 hw->subsystem_vendor_id = pdev->subsystem_vendor;
868 hw->subsystem_id = pdev->subsystem_device;
869
870 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
871
872 pci_read_config_word(pdev, PCI_COMMAND, &hw->pci_cmd_word);
873
874 adapter->rx_buffer_len = E1000_RXBUFFER_2048;
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700875 adapter->rx_ps_bsize0 = E1000_RXBUFFER_256;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 hw->max_frame_size = netdev->mtu +
877 ENET_HEADER_SIZE + ETHERNET_FCS_SIZE;
878 hw->min_frame_size = MINIMUM_ETHERNET_FRAME_SIZE;
879
880 /* identify the MAC */
881
882 if(e1000_set_mac_type(hw)) {
883 DPRINTK(PROBE, ERR, "Unknown MAC Type\n");
884 return -EIO;
885 }
886
887 /* initialize eeprom parameters */
888
Malli Chilakala2d7edb92005-04-28 19:43:52 -0700889 if(e1000_init_eeprom_params(hw)) {
890 E1000_ERR("EEPROM initialization failed\n");
891 return -EIO;
892 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
894 switch(hw->mac_type) {
895 default:
896 break;
897 case e1000_82541:
898 case e1000_82547:
899 case e1000_82541_rev_2:
900 case e1000_82547_rev_2:
901 hw->phy_init_script = 1;
902 break;
903 }
904
905 e1000_set_media_type(hw);
906
907 hw->wait_autoneg_complete = FALSE;
908 hw->tbi_compatibility_en = TRUE;
909 hw->adaptive_ifs = TRUE;
910
911 /* Copper options */
912
913 if(hw->media_type == e1000_media_type_copper) {
914 hw->mdix = AUTO_ALL_MODES;
915 hw->disable_polarity_correction = FALSE;
916 hw->master_slave = E1000_MASTER_SLAVE;
917 }
918
Mallikarjuna R Chilakala24025e4ec2005-10-04 07:03:23 -0400919#ifdef CONFIG_E1000_MQ
920 /* Number of supported queues */
921 switch (hw->mac_type) {
922 case e1000_82571:
923 case e1000_82572:
924 adapter->num_queues = 2;
925 break;
926 default:
927 adapter->num_queues = 1;
928 break;
929 }
930 adapter->num_queues = min(adapter->num_queues, num_online_cpus());
931#else
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -0400932 adapter->num_queues = 1;
Mallikarjuna R Chilakala24025e4ec2005-10-04 07:03:23 -0400933#endif
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -0400934
935 if (e1000_alloc_queues(adapter)) {
936 DPRINTK(PROBE, ERR, "Unable to allocate memory for queues\n");
937 return -ENOMEM;
938 }
939
940#ifdef CONFIG_E1000_NAPI
941 for (i = 0; i < adapter->num_queues; i++) {
942 adapter->polling_netdev[i].priv = adapter;
943 adapter->polling_netdev[i].poll = &e1000_clean;
944 adapter->polling_netdev[i].weight = 64;
945 dev_hold(&adapter->polling_netdev[i]);
946 set_bit(__LINK_STATE_START, &adapter->polling_netdev[i].state);
947 }
948#endif
Mallikarjuna R Chilakala24025e4ec2005-10-04 07:03:23 -0400949
950#ifdef CONFIG_E1000_MQ
951 e1000_setup_queue_mapping(adapter);
952#endif
953
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 atomic_set(&adapter->irq_sem, 1);
955 spin_lock_init(&adapter->stats_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
957 return 0;
958}
959
960/**
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -0400961 * e1000_alloc_queues - Allocate memory for all rings
962 * @adapter: board private structure to initialize
963 *
964 * We allocate one ring per queue at run-time since we don't know the
965 * number of queues at compile-time. The polling_netdev array is
966 * intended for Multiqueue, but should work fine with a single queue.
967 **/
968
969static int __devinit
970e1000_alloc_queues(struct e1000_adapter *adapter)
971{
972 int size;
973
974 size = sizeof(struct e1000_tx_ring) * adapter->num_queues;
975 adapter->tx_ring = kmalloc(size, GFP_KERNEL);
976 if (!adapter->tx_ring)
977 return -ENOMEM;
978 memset(adapter->tx_ring, 0, size);
979
980 size = sizeof(struct e1000_rx_ring) * adapter->num_queues;
981 adapter->rx_ring = kmalloc(size, GFP_KERNEL);
982 if (!adapter->rx_ring) {
983 kfree(adapter->tx_ring);
984 return -ENOMEM;
985 }
986 memset(adapter->rx_ring, 0, size);
987
988#ifdef CONFIG_E1000_NAPI
989 size = sizeof(struct net_device) * adapter->num_queues;
990 adapter->polling_netdev = kmalloc(size, GFP_KERNEL);
991 if (!adapter->polling_netdev) {
992 kfree(adapter->tx_ring);
993 kfree(adapter->rx_ring);
994 return -ENOMEM;
995 }
996 memset(adapter->polling_netdev, 0, size);
997#endif
998
999 return E1000_SUCCESS;
1000}
1001
Mallikarjuna R Chilakala24025e4ec2005-10-04 07:03:23 -04001002#ifdef CONFIG_E1000_MQ
1003static void __devinit
1004e1000_setup_queue_mapping(struct e1000_adapter *adapter)
1005{
1006 int i, cpu;
1007
1008 adapter->rx_sched_call_data.func = e1000_rx_schedule;
1009 adapter->rx_sched_call_data.info = adapter->netdev;
1010 cpus_clear(adapter->rx_sched_call_data.cpumask);
1011
1012 adapter->cpu_netdev = alloc_percpu(struct net_device *);
1013 adapter->cpu_tx_ring = alloc_percpu(struct e1000_tx_ring *);
1014
1015 lock_cpu_hotplug();
1016 i = 0;
1017 for_each_online_cpu(cpu) {
1018 *per_cpu_ptr(adapter->cpu_tx_ring, cpu) = &adapter->tx_ring[i % adapter->num_queues];
1019 /* This is incomplete because we'd like to assign separate
1020 * physical cpus to these netdev polling structures and
1021 * avoid saturating a subset of cpus.
1022 */
1023 if (i < adapter->num_queues) {
1024 *per_cpu_ptr(adapter->cpu_netdev, cpu) = &adapter->polling_netdev[i];
1025 adapter->cpu_for_queue[i] = cpu;
1026 } else
1027 *per_cpu_ptr(adapter->cpu_netdev, cpu) = NULL;
1028
1029 i++;
1030 }
1031 unlock_cpu_hotplug();
1032}
1033#endif
1034
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001035/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 * e1000_open - Called when a network interface is made active
1037 * @netdev: network interface device structure
1038 *
1039 * Returns 0 on success, negative value on failure
1040 *
1041 * The open entry point is called when a network interface is made
1042 * active by the system (IFF_UP). At this point all resources needed
1043 * for transmit and receive operations are allocated, the interrupt
1044 * handler is registered with the OS, the watchdog timer is started,
1045 * and the stack is notified that the interface is ready.
1046 **/
1047
1048static int
1049e1000_open(struct net_device *netdev)
1050{
Malli Chilakala60490fe2005-06-17 17:41:45 -07001051 struct e1000_adapter *adapter = netdev_priv(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 int err;
1053
1054 /* allocate transmit descriptors */
1055
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001056 if ((err = e1000_setup_all_tx_resources(adapter)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 goto err_setup_tx;
1058
1059 /* allocate receive descriptors */
1060
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001061 if ((err = e1000_setup_all_rx_resources(adapter)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 goto err_setup_rx;
1063
1064 if((err = e1000_up(adapter)))
1065 goto err_up;
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001066 adapter->mng_vlan_id = E1000_MNG_VLAN_NONE;
1067 if((adapter->hw.mng_cookie.status &
1068 E1000_MNG_DHCP_COOKIE_STATUS_VLAN_SUPPORT)) {
1069 e1000_update_mng_vlan(adapter);
1070 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
1072 return E1000_SUCCESS;
1073
1074err_up:
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001075 e1000_free_all_rx_resources(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076err_setup_rx:
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001077 e1000_free_all_tx_resources(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078err_setup_tx:
1079 e1000_reset(adapter);
1080
1081 return err;
1082}
1083
1084/**
1085 * e1000_close - Disables a network interface
1086 * @netdev: network interface device structure
1087 *
1088 * Returns 0, this is not allowed to fail
1089 *
1090 * The close entry point is called when an interface is de-activated
1091 * by the OS. The hardware is still under the drivers control, but
1092 * needs to be disabled. A global MAC reset is issued to stop the
1093 * hardware, and all transmit and receive resources are freed.
1094 **/
1095
1096static int
1097e1000_close(struct net_device *netdev)
1098{
Malli Chilakala60490fe2005-06-17 17:41:45 -07001099 struct e1000_adapter *adapter = netdev_priv(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
1101 e1000_down(adapter);
1102
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001103 e1000_free_all_tx_resources(adapter);
1104 e1000_free_all_rx_resources(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001106 if((adapter->hw.mng_cookie.status &
1107 E1000_MNG_DHCP_COOKIE_STATUS_VLAN_SUPPORT)) {
1108 e1000_vlan_rx_kill_vid(netdev, adapter->mng_vlan_id);
1109 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 return 0;
1111}
1112
1113/**
1114 * e1000_check_64k_bound - check that memory doesn't cross 64kB boundary
1115 * @adapter: address of board private structure
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001116 * @start: address of beginning of memory
1117 * @len: length of memory
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 **/
1119static inline boolean_t
1120e1000_check_64k_bound(struct e1000_adapter *adapter,
1121 void *start, unsigned long len)
1122{
1123 unsigned long begin = (unsigned long) start;
1124 unsigned long end = begin + len;
1125
Malli Chilakala26483452005-04-28 19:44:46 -07001126 /* First rev 82545 and 82546 need to not allow any memory
1127 * write location to cross 64k boundary due to errata 23 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 if (adapter->hw.mac_type == e1000_82545 ||
Malli Chilakala26483452005-04-28 19:44:46 -07001129 adapter->hw.mac_type == e1000_82546) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 return ((begin ^ (end - 1)) >> 16) != 0 ? FALSE : TRUE;
1131 }
1132
1133 return TRUE;
1134}
1135
1136/**
1137 * e1000_setup_tx_resources - allocate Tx resources (Descriptors)
1138 * @adapter: board private structure
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001139 * @txdr: tx descriptor ring (for a specific queue) to setup
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 *
1141 * Return 0 on success, negative on failure
1142 **/
1143
1144int
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001145e1000_setup_tx_resources(struct e1000_adapter *adapter,
1146 struct e1000_tx_ring *txdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 struct pci_dev *pdev = adapter->pdev;
1149 int size;
1150
1151 size = sizeof(struct e1000_buffer) * txdr->count;
Ravikiran G Thirumalaia7ec15d2005-10-28 15:14:49 -07001152
1153 txdr->buffer_info = vmalloc_node(size, pcibus_to_node(pdev->bus));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 if(!txdr->buffer_info) {
Malli Chilakala26483452005-04-28 19:44:46 -07001155 DPRINTK(PROBE, ERR,
1156 "Unable to allocate memory for the transmit descriptor ring\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 return -ENOMEM;
1158 }
1159 memset(txdr->buffer_info, 0, size);
Mallikarjuna R Chilakala2ae76d92005-10-04 07:04:22 -04001160 memset(&txdr->previous_buffer_info, 0, sizeof(struct e1000_buffer));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
1162 /* round up to nearest 4K */
1163
1164 txdr->size = txdr->count * sizeof(struct e1000_tx_desc);
1165 E1000_ROUNDUP(txdr->size, 4096);
1166
1167 txdr->desc = pci_alloc_consistent(pdev, txdr->size, &txdr->dma);
1168 if(!txdr->desc) {
1169setup_tx_desc_die:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 vfree(txdr->buffer_info);
Malli Chilakala26483452005-04-28 19:44:46 -07001171 DPRINTK(PROBE, ERR,
1172 "Unable to allocate memory for the transmit descriptor ring\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 return -ENOMEM;
1174 }
1175
Malli Chilakala26483452005-04-28 19:44:46 -07001176 /* Fix for errata 23, can't cross 64kB boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 if (!e1000_check_64k_bound(adapter, txdr->desc, txdr->size)) {
1178 void *olddesc = txdr->desc;
1179 dma_addr_t olddma = txdr->dma;
Malli Chilakala26483452005-04-28 19:44:46 -07001180 DPRINTK(TX_ERR, ERR, "txdr align check failed: %u bytes "
1181 "at %p\n", txdr->size, txdr->desc);
1182 /* Try again, without freeing the previous */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 txdr->desc = pci_alloc_consistent(pdev, txdr->size, &txdr->dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 if(!txdr->desc) {
Malli Chilakala26483452005-04-28 19:44:46 -07001185 /* Failed allocation, critical failure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 pci_free_consistent(pdev, txdr->size, olddesc, olddma);
1187 goto setup_tx_desc_die;
1188 }
1189
1190 if (!e1000_check_64k_bound(adapter, txdr->desc, txdr->size)) {
1191 /* give up */
Malli Chilakala26483452005-04-28 19:44:46 -07001192 pci_free_consistent(pdev, txdr->size, txdr->desc,
1193 txdr->dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 pci_free_consistent(pdev, txdr->size, olddesc, olddma);
1195 DPRINTK(PROBE, ERR,
Malli Chilakala26483452005-04-28 19:44:46 -07001196 "Unable to allocate aligned memory "
1197 "for the transmit descriptor ring\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 vfree(txdr->buffer_info);
1199 return -ENOMEM;
1200 } else {
Malli Chilakala26483452005-04-28 19:44:46 -07001201 /* Free old allocation, new allocation was successful */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 pci_free_consistent(pdev, txdr->size, olddesc, olddma);
1203 }
1204 }
1205 memset(txdr->desc, 0, txdr->size);
1206
1207 txdr->next_to_use = 0;
1208 txdr->next_to_clean = 0;
Mallikarjuna R Chilakala2ae76d92005-10-04 07:04:22 -04001209 spin_lock_init(&txdr->tx_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
1211 return 0;
1212}
1213
1214/**
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001215 * e1000_setup_all_tx_resources - wrapper to allocate Tx resources
1216 * (Descriptors) for all queues
1217 * @adapter: board private structure
1218 *
1219 * If this function returns with an error, then it's possible one or
1220 * more of the rings is populated (while the rest are not). It is the
1221 * callers duty to clean those orphaned rings.
1222 *
1223 * Return 0 on success, negative on failure
1224 **/
1225
1226int
1227e1000_setup_all_tx_resources(struct e1000_adapter *adapter)
1228{
1229 int i, err = 0;
1230
1231 for (i = 0; i < adapter->num_queues; i++) {
1232 err = e1000_setup_tx_resources(adapter, &adapter->tx_ring[i]);
1233 if (err) {
1234 DPRINTK(PROBE, ERR,
1235 "Allocation for Tx Queue %u failed\n", i);
1236 break;
1237 }
1238 }
1239
1240 return err;
1241}
1242
1243/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 * e1000_configure_tx - Configure 8254x Transmit Unit after Reset
1245 * @adapter: board private structure
1246 *
1247 * Configure the Tx unit of the MAC after a reset.
1248 **/
1249
1250static void
1251e1000_configure_tx(struct e1000_adapter *adapter)
1252{
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001253 uint64_t tdba;
1254 struct e1000_hw *hw = &adapter->hw;
1255 uint32_t tdlen, tctl, tipg, tarc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
1257 /* Setup the HW Tx Head and Tail descriptor pointers */
1258
Mallikarjuna R Chilakala24025e4ec2005-10-04 07:03:23 -04001259 switch (adapter->num_queues) {
1260 case 2:
1261 tdba = adapter->tx_ring[1].dma;
1262 tdlen = adapter->tx_ring[1].count *
1263 sizeof(struct e1000_tx_desc);
1264 E1000_WRITE_REG(hw, TDBAL1, (tdba & 0x00000000ffffffffULL));
1265 E1000_WRITE_REG(hw, TDBAH1, (tdba >> 32));
1266 E1000_WRITE_REG(hw, TDLEN1, tdlen);
1267 E1000_WRITE_REG(hw, TDH1, 0);
1268 E1000_WRITE_REG(hw, TDT1, 0);
1269 adapter->tx_ring[1].tdh = E1000_TDH1;
1270 adapter->tx_ring[1].tdt = E1000_TDT1;
1271 /* Fall Through */
1272 case 1:
1273 default:
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001274 tdba = adapter->tx_ring[0].dma;
1275 tdlen = adapter->tx_ring[0].count *
1276 sizeof(struct e1000_tx_desc);
1277 E1000_WRITE_REG(hw, TDBAL, (tdba & 0x00000000ffffffffULL));
1278 E1000_WRITE_REG(hw, TDBAH, (tdba >> 32));
1279 E1000_WRITE_REG(hw, TDLEN, tdlen);
1280 E1000_WRITE_REG(hw, TDH, 0);
1281 E1000_WRITE_REG(hw, TDT, 0);
1282 adapter->tx_ring[0].tdh = E1000_TDH;
1283 adapter->tx_ring[0].tdt = E1000_TDT;
Mallikarjuna R Chilakala24025e4ec2005-10-04 07:03:23 -04001284 break;
1285 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
1287 /* Set the default values for the Tx Inter Packet Gap timer */
1288
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001289 switch (hw->mac_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 case e1000_82542_rev2_0:
1291 case e1000_82542_rev2_1:
1292 tipg = DEFAULT_82542_TIPG_IPGT;
1293 tipg |= DEFAULT_82542_TIPG_IPGR1 << E1000_TIPG_IPGR1_SHIFT;
1294 tipg |= DEFAULT_82542_TIPG_IPGR2 << E1000_TIPG_IPGR2_SHIFT;
1295 break;
1296 default:
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001297 if (hw->media_type == e1000_media_type_fiber ||
1298 hw->media_type == e1000_media_type_internal_serdes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 tipg = DEFAULT_82543_TIPG_IPGT_FIBER;
1300 else
1301 tipg = DEFAULT_82543_TIPG_IPGT_COPPER;
1302 tipg |= DEFAULT_82543_TIPG_IPGR1 << E1000_TIPG_IPGR1_SHIFT;
1303 tipg |= DEFAULT_82543_TIPG_IPGR2 << E1000_TIPG_IPGR2_SHIFT;
1304 }
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001305 E1000_WRITE_REG(hw, TIPG, tipg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
1307 /* Set the Tx Interrupt Delay register */
1308
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001309 E1000_WRITE_REG(hw, TIDV, adapter->tx_int_delay);
1310 if (hw->mac_type >= e1000_82540)
1311 E1000_WRITE_REG(hw, TADV, adapter->tx_abs_int_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
1313 /* Program the Transmit Control Register */
1314
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001315 tctl = E1000_READ_REG(hw, TCTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
1317 tctl &= ~E1000_TCTL_CT;
Mallikarjuna R Chilakala24025e4ec2005-10-04 07:03:23 -04001318 tctl |= E1000_TCTL_EN | E1000_TCTL_PSP | E1000_TCTL_RTLC |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 (E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT);
1320
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001321 E1000_WRITE_REG(hw, TCTL, tctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
Mallikarjuna R Chilakala2ae76d92005-10-04 07:04:22 -04001323 if (hw->mac_type == e1000_82571 || hw->mac_type == e1000_82572) {
1324 tarc = E1000_READ_REG(hw, TARC0);
1325 tarc |= ((1 << 25) | (1 << 21));
1326 E1000_WRITE_REG(hw, TARC0, tarc);
1327 tarc = E1000_READ_REG(hw, TARC1);
1328 tarc |= (1 << 25);
1329 if (tctl & E1000_TCTL_MULR)
1330 tarc &= ~(1 << 28);
1331 else
1332 tarc |= (1 << 28);
1333 E1000_WRITE_REG(hw, TARC1, tarc);
1334 }
1335
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001336 e1000_config_collision_dist(hw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
1338 /* Setup Transmit Descriptor Settings for eop descriptor */
1339 adapter->txd_cmd = E1000_TXD_CMD_IDE | E1000_TXD_CMD_EOP |
1340 E1000_TXD_CMD_IFCS;
1341
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001342 if (hw->mac_type < e1000_82543)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 adapter->txd_cmd |= E1000_TXD_CMD_RPS;
1344 else
1345 adapter->txd_cmd |= E1000_TXD_CMD_RS;
1346
1347 /* Cache if we're 82544 running in PCI-X because we'll
1348 * need this to apply a workaround later in the send path. */
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001349 if (hw->mac_type == e1000_82544 &&
1350 hw->bus_type == e1000_bus_type_pcix)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 adapter->pcix_82544 = 1;
1352}
1353
1354/**
1355 * e1000_setup_rx_resources - allocate Rx resources (Descriptors)
1356 * @adapter: board private structure
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001357 * @rxdr: rx descriptor ring (for a specific queue) to setup
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 *
1359 * Returns 0 on success, negative on failure
1360 **/
1361
1362int
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001363e1000_setup_rx_resources(struct e1000_adapter *adapter,
1364 struct e1000_rx_ring *rxdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 struct pci_dev *pdev = adapter->pdev;
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001367 int size, desc_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
1369 size = sizeof(struct e1000_buffer) * rxdr->count;
Ravikiran G Thirumalaia7ec15d2005-10-28 15:14:49 -07001370 rxdr->buffer_info = vmalloc_node(size, pcibus_to_node(pdev->bus));
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001371 if (!rxdr->buffer_info) {
Malli Chilakala26483452005-04-28 19:44:46 -07001372 DPRINTK(PROBE, ERR,
1373 "Unable to allocate memory for the receive descriptor ring\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 return -ENOMEM;
1375 }
1376 memset(rxdr->buffer_info, 0, size);
1377
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001378 size = sizeof(struct e1000_ps_page) * rxdr->count;
1379 rxdr->ps_page = kmalloc(size, GFP_KERNEL);
1380 if(!rxdr->ps_page) {
1381 vfree(rxdr->buffer_info);
1382 DPRINTK(PROBE, ERR,
1383 "Unable to allocate memory for the receive descriptor ring\n");
1384 return -ENOMEM;
1385 }
1386 memset(rxdr->ps_page, 0, size);
1387
1388 size = sizeof(struct e1000_ps_page_dma) * rxdr->count;
1389 rxdr->ps_page_dma = kmalloc(size, GFP_KERNEL);
1390 if(!rxdr->ps_page_dma) {
1391 vfree(rxdr->buffer_info);
1392 kfree(rxdr->ps_page);
1393 DPRINTK(PROBE, ERR,
1394 "Unable to allocate memory for the receive descriptor ring\n");
1395 return -ENOMEM;
1396 }
1397 memset(rxdr->ps_page_dma, 0, size);
1398
1399 if(adapter->hw.mac_type <= e1000_82547_rev_2)
1400 desc_len = sizeof(struct e1000_rx_desc);
1401 else
1402 desc_len = sizeof(union e1000_rx_desc_packet_split);
1403
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 /* Round up to nearest 4K */
1405
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001406 rxdr->size = rxdr->count * desc_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 E1000_ROUNDUP(rxdr->size, 4096);
1408
1409 rxdr->desc = pci_alloc_consistent(pdev, rxdr->size, &rxdr->dma);
1410
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001411 if (!rxdr->desc) {
1412 DPRINTK(PROBE, ERR,
1413 "Unable to allocate memory for the receive descriptor ring\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414setup_rx_desc_die:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 vfree(rxdr->buffer_info);
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001416 kfree(rxdr->ps_page);
1417 kfree(rxdr->ps_page_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 return -ENOMEM;
1419 }
1420
Malli Chilakala26483452005-04-28 19:44:46 -07001421 /* Fix for errata 23, can't cross 64kB boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 if (!e1000_check_64k_bound(adapter, rxdr->desc, rxdr->size)) {
1423 void *olddesc = rxdr->desc;
1424 dma_addr_t olddma = rxdr->dma;
Malli Chilakala26483452005-04-28 19:44:46 -07001425 DPRINTK(RX_ERR, ERR, "rxdr align check failed: %u bytes "
1426 "at %p\n", rxdr->size, rxdr->desc);
1427 /* Try again, without freeing the previous */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 rxdr->desc = pci_alloc_consistent(pdev, rxdr->size, &rxdr->dma);
Malli Chilakala26483452005-04-28 19:44:46 -07001429 /* Failed allocation, critical failure */
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001430 if (!rxdr->desc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 pci_free_consistent(pdev, rxdr->size, olddesc, olddma);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001432 DPRINTK(PROBE, ERR,
1433 "Unable to allocate memory "
1434 "for the receive descriptor ring\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 goto setup_rx_desc_die;
1436 }
1437
1438 if (!e1000_check_64k_bound(adapter, rxdr->desc, rxdr->size)) {
1439 /* give up */
Malli Chilakala26483452005-04-28 19:44:46 -07001440 pci_free_consistent(pdev, rxdr->size, rxdr->desc,
1441 rxdr->dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 pci_free_consistent(pdev, rxdr->size, olddesc, olddma);
Malli Chilakala26483452005-04-28 19:44:46 -07001443 DPRINTK(PROBE, ERR,
1444 "Unable to allocate aligned memory "
1445 "for the receive descriptor ring\n");
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001446 goto setup_rx_desc_die;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 } else {
Malli Chilakala26483452005-04-28 19:44:46 -07001448 /* Free old allocation, new allocation was successful */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 pci_free_consistent(pdev, rxdr->size, olddesc, olddma);
1450 }
1451 }
1452 memset(rxdr->desc, 0, rxdr->size);
1453
1454 rxdr->next_to_clean = 0;
1455 rxdr->next_to_use = 0;
1456
1457 return 0;
1458}
1459
1460/**
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001461 * e1000_setup_all_rx_resources - wrapper to allocate Rx resources
1462 * (Descriptors) for all queues
1463 * @adapter: board private structure
1464 *
1465 * If this function returns with an error, then it's possible one or
1466 * more of the rings is populated (while the rest are not). It is the
1467 * callers duty to clean those orphaned rings.
1468 *
1469 * Return 0 on success, negative on failure
1470 **/
1471
1472int
1473e1000_setup_all_rx_resources(struct e1000_adapter *adapter)
1474{
1475 int i, err = 0;
1476
1477 for (i = 0; i < adapter->num_queues; i++) {
1478 err = e1000_setup_rx_resources(adapter, &adapter->rx_ring[i]);
1479 if (err) {
1480 DPRINTK(PROBE, ERR,
1481 "Allocation for Rx Queue %u failed\n", i);
1482 break;
1483 }
1484 }
1485
1486 return err;
1487}
1488
1489/**
Malli Chilakala26483452005-04-28 19:44:46 -07001490 * e1000_setup_rctl - configure the receive control registers
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 * @adapter: Board private structure
1492 **/
Mallikarjuna R Chilakalae4c811c2005-10-04 07:05:44 -04001493#define PAGE_USE_COUNT(S) (((S) >> PAGE_SHIFT) + \
1494 (((S) & (PAGE_SIZE - 1)) ? 1 : 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495static void
1496e1000_setup_rctl(struct e1000_adapter *adapter)
1497{
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001498 uint32_t rctl, rfctl;
1499 uint32_t psrctl = 0;
Mallikarjuna R Chilakalae4c811c2005-10-04 07:05:44 -04001500#ifdef CONFIG_E1000_PACKET_SPLIT
1501 uint32_t pages = 0;
1502#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
1504 rctl = E1000_READ_REG(&adapter->hw, RCTL);
1505
1506 rctl &= ~(3 << E1000_RCTL_MO_SHIFT);
1507
1508 rctl |= E1000_RCTL_EN | E1000_RCTL_BAM |
1509 E1000_RCTL_LBM_NO | E1000_RCTL_RDMTS_HALF |
1510 (adapter->hw.mc_filter_type << E1000_RCTL_MO_SHIFT);
1511
1512 if(adapter->hw.tbi_compatibility_on == 1)
1513 rctl |= E1000_RCTL_SBP;
1514 else
1515 rctl &= ~E1000_RCTL_SBP;
1516
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001517 if (adapter->netdev->mtu <= ETH_DATA_LEN)
1518 rctl &= ~E1000_RCTL_LPE;
1519 else
1520 rctl |= E1000_RCTL_LPE;
1521
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 /* Setup buffer sizes */
Mallikarjuna R Chilakala868d5302005-10-04 06:58:59 -04001523 if(adapter->hw.mac_type >= e1000_82571) {
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001524 /* We can now specify buffers in 1K increments.
1525 * BSIZE and BSEX are ignored in this case. */
1526 rctl |= adapter->rx_buffer_len << 0x11;
1527 } else {
1528 rctl &= ~E1000_RCTL_SZ_4096;
1529 rctl |= E1000_RCTL_BSEX;
1530 switch (adapter->rx_buffer_len) {
1531 case E1000_RXBUFFER_2048:
1532 default:
1533 rctl |= E1000_RCTL_SZ_2048;
1534 rctl &= ~E1000_RCTL_BSEX;
1535 break;
1536 case E1000_RXBUFFER_4096:
1537 rctl |= E1000_RCTL_SZ_4096;
1538 break;
1539 case E1000_RXBUFFER_8192:
1540 rctl |= E1000_RCTL_SZ_8192;
1541 break;
1542 case E1000_RXBUFFER_16384:
1543 rctl |= E1000_RCTL_SZ_16384;
1544 break;
1545 }
1546 }
1547
1548#ifdef CONFIG_E1000_PACKET_SPLIT
1549 /* 82571 and greater support packet-split where the protocol
1550 * header is placed in skb->data and the packet data is
1551 * placed in pages hanging off of skb_shinfo(skb)->nr_frags.
1552 * In the case of a non-split, skb->data is linearly filled,
1553 * followed by the page buffers. Therefore, skb->data is
1554 * sized to hold the largest protocol header.
1555 */
Mallikarjuna R Chilakalae4c811c2005-10-04 07:05:44 -04001556 pages = PAGE_USE_COUNT(adapter->netdev->mtu);
1557 if ((adapter->hw.mac_type > e1000_82547_rev_2) && (pages <= 3) &&
1558 PAGE_SIZE <= 16384)
1559 adapter->rx_ps_pages = pages;
1560 else
1561 adapter->rx_ps_pages = 0;
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001562#endif
Mallikarjuna R Chilakalae4c811c2005-10-04 07:05:44 -04001563 if (adapter->rx_ps_pages) {
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001564 /* Configure extra packet-split registers */
1565 rfctl = E1000_READ_REG(&adapter->hw, RFCTL);
1566 rfctl |= E1000_RFCTL_EXTEN;
1567 /* disable IPv6 packet split support */
1568 rfctl |= E1000_RFCTL_IPV6_DIS;
1569 E1000_WRITE_REG(&adapter->hw, RFCTL, rfctl);
1570
1571 rctl |= E1000_RCTL_DTYP_PS | E1000_RCTL_SECRC;
1572
1573 psrctl |= adapter->rx_ps_bsize0 >>
1574 E1000_PSRCTL_BSIZE0_SHIFT;
Mallikarjuna R Chilakalae4c811c2005-10-04 07:05:44 -04001575
1576 switch (adapter->rx_ps_pages) {
1577 case 3:
1578 psrctl |= PAGE_SIZE <<
1579 E1000_PSRCTL_BSIZE3_SHIFT;
1580 case 2:
1581 psrctl |= PAGE_SIZE <<
1582 E1000_PSRCTL_BSIZE2_SHIFT;
1583 case 1:
1584 psrctl |= PAGE_SIZE >>
1585 E1000_PSRCTL_BSIZE1_SHIFT;
1586 break;
1587 }
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001588
1589 E1000_WRITE_REG(&adapter->hw, PSRCTL, psrctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 }
1591
1592 E1000_WRITE_REG(&adapter->hw, RCTL, rctl);
1593}
1594
1595/**
1596 * e1000_configure_rx - Configure 8254x Receive Unit after Reset
1597 * @adapter: board private structure
1598 *
1599 * Configure the Rx unit of the MAC after a reset.
1600 **/
1601
1602static void
1603e1000_configure_rx(struct e1000_adapter *adapter)
1604{
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001605 uint64_t rdba;
1606 struct e1000_hw *hw = &adapter->hw;
1607 uint32_t rdlen, rctl, rxcsum, ctrl_ext;
1608#ifdef CONFIG_E1000_MQ
1609 uint32_t reta, mrqc;
1610 int i;
1611#endif
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001612
Mallikarjuna R Chilakalae4c811c2005-10-04 07:05:44 -04001613 if (adapter->rx_ps_pages) {
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001614 rdlen = adapter->rx_ring[0].count *
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001615 sizeof(union e1000_rx_desc_packet_split);
1616 adapter->clean_rx = e1000_clean_rx_irq_ps;
1617 adapter->alloc_rx_buf = e1000_alloc_rx_buffers_ps;
1618 } else {
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001619 rdlen = adapter->rx_ring[0].count *
1620 sizeof(struct e1000_rx_desc);
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001621 adapter->clean_rx = e1000_clean_rx_irq;
1622 adapter->alloc_rx_buf = e1000_alloc_rx_buffers;
1623 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
1625 /* disable receives while setting up the descriptors */
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001626 rctl = E1000_READ_REG(hw, RCTL);
1627 E1000_WRITE_REG(hw, RCTL, rctl & ~E1000_RCTL_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628
1629 /* set the Receive Delay Timer Register */
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001630 E1000_WRITE_REG(hw, RDTR, adapter->rx_int_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001632 if (hw->mac_type >= e1000_82540) {
1633 E1000_WRITE_REG(hw, RADV, adapter->rx_abs_int_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 if(adapter->itr > 1)
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001635 E1000_WRITE_REG(hw, ITR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 1000000000 / (adapter->itr * 256));
1637 }
1638
Mallikarjuna R Chilakala2ae76d92005-10-04 07:04:22 -04001639 if (hw->mac_type >= e1000_82571) {
1640 /* Reset delay timers after every interrupt */
1641 ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
1642 ctrl_ext |= E1000_CTRL_EXT_CANC;
1643 E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
1644 E1000_WRITE_FLUSH(hw);
1645 }
1646
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001647 /* Setup the HW Rx Head and Tail Descriptor Pointers and
1648 * the Base and Length of the Rx Descriptor Ring */
Mallikarjuna R Chilakala24025e4ec2005-10-04 07:03:23 -04001649 switch (adapter->num_queues) {
1650#ifdef CONFIG_E1000_MQ
1651 case 2:
1652 rdba = adapter->rx_ring[1].dma;
1653 E1000_WRITE_REG(hw, RDBAL1, (rdba & 0x00000000ffffffffULL));
1654 E1000_WRITE_REG(hw, RDBAH1, (rdba >> 32));
1655 E1000_WRITE_REG(hw, RDLEN1, rdlen);
1656 E1000_WRITE_REG(hw, RDH1, 0);
1657 E1000_WRITE_REG(hw, RDT1, 0);
1658 adapter->rx_ring[1].rdh = E1000_RDH1;
1659 adapter->rx_ring[1].rdt = E1000_RDT1;
1660 /* Fall Through */
1661#endif
1662 case 1:
1663 default:
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001664 rdba = adapter->rx_ring[0].dma;
1665 E1000_WRITE_REG(hw, RDBAL, (rdba & 0x00000000ffffffffULL));
1666 E1000_WRITE_REG(hw, RDBAH, (rdba >> 32));
1667 E1000_WRITE_REG(hw, RDLEN, rdlen);
1668 E1000_WRITE_REG(hw, RDH, 0);
1669 E1000_WRITE_REG(hw, RDT, 0);
1670 adapter->rx_ring[0].rdh = E1000_RDH;
1671 adapter->rx_ring[0].rdt = E1000_RDT;
1672 break;
Mallikarjuna R Chilakala24025e4ec2005-10-04 07:03:23 -04001673 }
1674
1675#ifdef CONFIG_E1000_MQ
1676 if (adapter->num_queues > 1) {
1677 uint32_t random[10];
1678
1679 get_random_bytes(&random[0], 40);
1680
1681 if (hw->mac_type <= e1000_82572) {
1682 E1000_WRITE_REG(hw, RSSIR, 0);
1683 E1000_WRITE_REG(hw, RSSIM, 0);
1684 }
1685
1686 switch (adapter->num_queues) {
1687 case 2:
1688 default:
1689 reta = 0x00800080;
1690 mrqc = E1000_MRQC_ENABLE_RSS_2Q;
1691 break;
1692 }
1693
1694 /* Fill out redirection table */
1695 for (i = 0; i < 32; i++)
1696 E1000_WRITE_REG_ARRAY(hw, RETA, i, reta);
1697 /* Fill out hash function seeds */
1698 for (i = 0; i < 10; i++)
1699 E1000_WRITE_REG_ARRAY(hw, RSSRK, i, random[i]);
1700
1701 mrqc |= (E1000_MRQC_RSS_FIELD_IPV4 |
1702 E1000_MRQC_RSS_FIELD_IPV4_TCP);
1703 E1000_WRITE_REG(hw, MRQC, mrqc);
1704 }
1705
1706 /* Multiqueue and packet checksumming are mutually exclusive. */
1707 if (hw->mac_type >= e1000_82571) {
1708 rxcsum = E1000_READ_REG(hw, RXCSUM);
1709 rxcsum |= E1000_RXCSUM_PCSD;
1710 E1000_WRITE_REG(hw, RXCSUM, rxcsum);
1711 }
1712
1713#else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
1715 /* Enable 82543 Receive Checksum Offload for TCP and UDP */
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001716 if (hw->mac_type >= e1000_82543) {
1717 rxcsum = E1000_READ_REG(hw, RXCSUM);
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001718 if(adapter->rx_csum == TRUE) {
1719 rxcsum |= E1000_RXCSUM_TUOFL;
1720
Mallikarjuna R Chilakala868d5302005-10-04 06:58:59 -04001721 /* Enable 82571 IPv4 payload checksum for UDP fragments
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001722 * Must be used in conjunction with packet-split. */
Mallikarjuna R Chilakalae4c811c2005-10-04 07:05:44 -04001723 if ((hw->mac_type >= e1000_82571) &&
1724 (adapter->rx_ps_pages)) {
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001725 rxcsum |= E1000_RXCSUM_IPPCSE;
1726 }
1727 } else {
1728 rxcsum &= ~E1000_RXCSUM_TUOFL;
1729 /* don't need to clear IPPCSE as it defaults to 0 */
1730 }
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001731 E1000_WRITE_REG(hw, RXCSUM, rxcsum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 }
Mallikarjuna R Chilakala24025e4ec2005-10-04 07:03:23 -04001733#endif /* CONFIG_E1000_MQ */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001735 if (hw->mac_type == e1000_82573)
1736 E1000_WRITE_REG(hw, ERT, 0x0100);
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001737
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 /* Enable Receives */
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001739 E1000_WRITE_REG(hw, RCTL, rctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740}
1741
1742/**
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001743 * e1000_free_tx_resources - Free Tx Resources per Queue
1744 * @adapter: board private structure
1745 * @tx_ring: Tx descriptor ring for a specific queue
1746 *
1747 * Free all transmit software resources
1748 **/
1749
1750void
1751e1000_free_tx_resources(struct e1000_adapter *adapter,
1752 struct e1000_tx_ring *tx_ring)
1753{
1754 struct pci_dev *pdev = adapter->pdev;
1755
1756 e1000_clean_tx_ring(adapter, tx_ring);
1757
1758 vfree(tx_ring->buffer_info);
1759 tx_ring->buffer_info = NULL;
1760
1761 pci_free_consistent(pdev, tx_ring->size, tx_ring->desc, tx_ring->dma);
1762
1763 tx_ring->desc = NULL;
1764}
1765
1766/**
1767 * e1000_free_all_tx_resources - Free Tx Resources for All Queues
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 * @adapter: board private structure
1769 *
1770 * Free all transmit software resources
1771 **/
1772
1773void
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001774e1000_free_all_tx_resources(struct e1000_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775{
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001776 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001778 for (i = 0; i < adapter->num_queues; i++)
1779 e1000_free_tx_resources(adapter, &adapter->tx_ring[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780}
1781
1782static inline void
1783e1000_unmap_and_free_tx_resource(struct e1000_adapter *adapter,
1784 struct e1000_buffer *buffer_info)
1785{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 if(buffer_info->dma) {
Malli Chilakala26483452005-04-28 19:44:46 -07001787 pci_unmap_page(adapter->pdev,
1788 buffer_info->dma,
1789 buffer_info->length,
1790 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 buffer_info->dma = 0;
1792 }
1793 if(buffer_info->skb) {
1794 dev_kfree_skb_any(buffer_info->skb);
1795 buffer_info->skb = NULL;
1796 }
1797}
1798
1799/**
1800 * e1000_clean_tx_ring - Free Tx Buffers
1801 * @adapter: board private structure
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001802 * @tx_ring: ring to be cleaned
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 **/
1804
1805static void
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001806e1000_clean_tx_ring(struct e1000_adapter *adapter,
1807 struct e1000_tx_ring *tx_ring)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 struct e1000_buffer *buffer_info;
1810 unsigned long size;
1811 unsigned int i;
1812
1813 /* Free all the Tx ring sk_buffs */
1814
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001815 if (likely(tx_ring->previous_buffer_info.skb != NULL)) {
Malli Chilakala26483452005-04-28 19:44:46 -07001816 e1000_unmap_and_free_tx_resource(adapter,
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001817 &tx_ring->previous_buffer_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 }
1819
1820 for(i = 0; i < tx_ring->count; i++) {
1821 buffer_info = &tx_ring->buffer_info[i];
1822 e1000_unmap_and_free_tx_resource(adapter, buffer_info);
1823 }
1824
1825 size = sizeof(struct e1000_buffer) * tx_ring->count;
1826 memset(tx_ring->buffer_info, 0, size);
1827
1828 /* Zero out the descriptor ring */
1829
1830 memset(tx_ring->desc, 0, tx_ring->size);
1831
1832 tx_ring->next_to_use = 0;
1833 tx_ring->next_to_clean = 0;
1834
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001835 writel(0, adapter->hw.hw_addr + tx_ring->tdh);
1836 writel(0, adapter->hw.hw_addr + tx_ring->tdt);
1837}
1838
1839/**
1840 * e1000_clean_all_tx_rings - Free Tx Buffers for all queues
1841 * @adapter: board private structure
1842 **/
1843
1844static void
1845e1000_clean_all_tx_rings(struct e1000_adapter *adapter)
1846{
1847 int i;
1848
1849 for (i = 0; i < adapter->num_queues; i++)
1850 e1000_clean_tx_ring(adapter, &adapter->tx_ring[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851}
1852
1853/**
1854 * e1000_free_rx_resources - Free Rx Resources
1855 * @adapter: board private structure
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001856 * @rx_ring: ring to clean the resources from
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 *
1858 * Free all receive software resources
1859 **/
1860
1861void
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001862e1000_free_rx_resources(struct e1000_adapter *adapter,
1863 struct e1000_rx_ring *rx_ring)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 struct pci_dev *pdev = adapter->pdev;
1866
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001867 e1000_clean_rx_ring(adapter, rx_ring);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
1869 vfree(rx_ring->buffer_info);
1870 rx_ring->buffer_info = NULL;
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001871 kfree(rx_ring->ps_page);
1872 rx_ring->ps_page = NULL;
1873 kfree(rx_ring->ps_page_dma);
1874 rx_ring->ps_page_dma = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
1876 pci_free_consistent(pdev, rx_ring->size, rx_ring->desc, rx_ring->dma);
1877
1878 rx_ring->desc = NULL;
1879}
1880
1881/**
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001882 * e1000_free_all_rx_resources - Free Rx Resources for All Queues
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 * @adapter: board private structure
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001884 *
1885 * Free all receive software resources
1886 **/
1887
1888void
1889e1000_free_all_rx_resources(struct e1000_adapter *adapter)
1890{
1891 int i;
1892
1893 for (i = 0; i < adapter->num_queues; i++)
1894 e1000_free_rx_resources(adapter, &adapter->rx_ring[i]);
1895}
1896
1897/**
1898 * e1000_clean_rx_ring - Free Rx Buffers per Queue
1899 * @adapter: board private structure
1900 * @rx_ring: ring to free buffers from
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 **/
1902
1903static void
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001904e1000_clean_rx_ring(struct e1000_adapter *adapter,
1905 struct e1000_rx_ring *rx_ring)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 struct e1000_buffer *buffer_info;
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001908 struct e1000_ps_page *ps_page;
1909 struct e1000_ps_page_dma *ps_page_dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 struct pci_dev *pdev = adapter->pdev;
1911 unsigned long size;
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001912 unsigned int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913
1914 /* Free all the Rx ring sk_buffs */
1915
1916 for(i = 0; i < rx_ring->count; i++) {
1917 buffer_info = &rx_ring->buffer_info[i];
1918 if(buffer_info->skb) {
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001919 ps_page = &rx_ring->ps_page[i];
1920 ps_page_dma = &rx_ring->ps_page_dma[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 pci_unmap_single(pdev,
1922 buffer_info->dma,
1923 buffer_info->length,
1924 PCI_DMA_FROMDEVICE);
1925
1926 dev_kfree_skb(buffer_info->skb);
1927 buffer_info->skb = NULL;
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001928
Mallikarjuna R Chilakalae4c811c2005-10-04 07:05:44 -04001929 for(j = 0; j < adapter->rx_ps_pages; j++) {
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001930 if(!ps_page->ps_page[j]) break;
1931 pci_unmap_single(pdev,
1932 ps_page_dma->ps_page_dma[j],
1933 PAGE_SIZE, PCI_DMA_FROMDEVICE);
1934 ps_page_dma->ps_page_dma[j] = 0;
1935 put_page(ps_page->ps_page[j]);
1936 ps_page->ps_page[j] = NULL;
1937 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 }
1939 }
1940
1941 size = sizeof(struct e1000_buffer) * rx_ring->count;
1942 memset(rx_ring->buffer_info, 0, size);
Malli Chilakala2d7edb92005-04-28 19:43:52 -07001943 size = sizeof(struct e1000_ps_page) * rx_ring->count;
1944 memset(rx_ring->ps_page, 0, size);
1945 size = sizeof(struct e1000_ps_page_dma) * rx_ring->count;
1946 memset(rx_ring->ps_page_dma, 0, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947
1948 /* Zero out the descriptor ring */
1949
1950 memset(rx_ring->desc, 0, rx_ring->size);
1951
1952 rx_ring->next_to_clean = 0;
1953 rx_ring->next_to_use = 0;
1954
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001955 writel(0, adapter->hw.hw_addr + rx_ring->rdh);
1956 writel(0, adapter->hw.hw_addr + rx_ring->rdt);
1957}
1958
1959/**
1960 * e1000_clean_all_rx_rings - Free Rx Buffers for all queues
1961 * @adapter: board private structure
1962 **/
1963
1964static void
1965e1000_clean_all_rx_rings(struct e1000_adapter *adapter)
1966{
1967 int i;
1968
1969 for (i = 0; i < adapter->num_queues; i++)
1970 e1000_clean_rx_ring(adapter, &adapter->rx_ring[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971}
1972
1973/* The 82542 2.0 (revision 2) needs to have the receive unit in reset
1974 * and memory write and invalidate disabled for certain operations
1975 */
1976static void
1977e1000_enter_82542_rst(struct e1000_adapter *adapter)
1978{
1979 struct net_device *netdev = adapter->netdev;
1980 uint32_t rctl;
1981
1982 e1000_pci_clear_mwi(&adapter->hw);
1983
1984 rctl = E1000_READ_REG(&adapter->hw, RCTL);
1985 rctl |= E1000_RCTL_RST;
1986 E1000_WRITE_REG(&adapter->hw, RCTL, rctl);
1987 E1000_WRITE_FLUSH(&adapter->hw);
1988 mdelay(5);
1989
1990 if(netif_running(netdev))
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04001991 e1000_clean_all_rx_rings(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992}
1993
1994static void
1995e1000_leave_82542_rst(struct e1000_adapter *adapter)
1996{
1997 struct net_device *netdev = adapter->netdev;
1998 uint32_t rctl;
1999
2000 rctl = E1000_READ_REG(&adapter->hw, RCTL);
2001 rctl &= ~E1000_RCTL_RST;
2002 E1000_WRITE_REG(&adapter->hw, RCTL, rctl);
2003 E1000_WRITE_FLUSH(&adapter->hw);
2004 mdelay(5);
2005
2006 if(adapter->hw.pci_cmd_word & PCI_COMMAND_INVALIDATE)
2007 e1000_pci_set_mwi(&adapter->hw);
2008
2009 if(netif_running(netdev)) {
2010 e1000_configure_rx(adapter);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002011 e1000_alloc_rx_buffers(adapter, &adapter->rx_ring[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 }
2013}
2014
2015/**
2016 * e1000_set_mac - Change the Ethernet Address of the NIC
2017 * @netdev: network interface device structure
2018 * @p: pointer to an address structure
2019 *
2020 * Returns 0 on success, negative on failure
2021 **/
2022
2023static int
2024e1000_set_mac(struct net_device *netdev, void *p)
2025{
Malli Chilakala60490fe2005-06-17 17:41:45 -07002026 struct e1000_adapter *adapter = netdev_priv(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 struct sockaddr *addr = p;
2028
2029 if(!is_valid_ether_addr(addr->sa_data))
2030 return -EADDRNOTAVAIL;
2031
2032 /* 82542 2.0 needs to be in reset to write receive address registers */
2033
2034 if(adapter->hw.mac_type == e1000_82542_rev2_0)
2035 e1000_enter_82542_rst(adapter);
2036
2037 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
2038 memcpy(adapter->hw.mac_addr, addr->sa_data, netdev->addr_len);
2039
2040 e1000_rar_set(&adapter->hw, adapter->hw.mac_addr, 0);
2041
Mallikarjuna R Chilakala868d5302005-10-04 06:58:59 -04002042 /* With 82571 controllers, LAA may be overwritten (with the default)
2043 * due to controller reset from the other port. */
2044 if (adapter->hw.mac_type == e1000_82571) {
2045 /* activate the work around */
2046 adapter->hw.laa_is_present = 1;
2047
2048 /* Hold a copy of the LAA in RAR[14] This is done so that
2049 * between the time RAR[0] gets clobbered and the time it
2050 * gets fixed (in e1000_watchdog), the actual LAA is in one
2051 * of the RARs and no incoming packets directed to this port
2052 * are dropped. Eventaully the LAA will be in RAR[0] and
2053 * RAR[14] */
2054 e1000_rar_set(&adapter->hw, adapter->hw.mac_addr,
2055 E1000_RAR_ENTRIES - 1);
2056 }
2057
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 if(adapter->hw.mac_type == e1000_82542_rev2_0)
2059 e1000_leave_82542_rst(adapter);
2060
2061 return 0;
2062}
2063
2064/**
2065 * e1000_set_multi - Multicast and Promiscuous mode set
2066 * @netdev: network interface device structure
2067 *
2068 * The set_multi entry point is called whenever the multicast address
2069 * list or the network interface flags are updated. This routine is
2070 * responsible for configuring the hardware for proper multicast,
2071 * promiscuous mode, and all-multi behavior.
2072 **/
2073
2074static void
2075e1000_set_multi(struct net_device *netdev)
2076{
Malli Chilakala60490fe2005-06-17 17:41:45 -07002077 struct e1000_adapter *adapter = netdev_priv(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 struct e1000_hw *hw = &adapter->hw;
2079 struct dev_mc_list *mc_ptr;
2080 uint32_t rctl;
2081 uint32_t hash_value;
Mallikarjuna R Chilakala868d5302005-10-04 06:58:59 -04002082 int i, rar_entries = E1000_RAR_ENTRIES;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083
Mallikarjuna R Chilakala868d5302005-10-04 06:58:59 -04002084 /* reserve RAR[14] for LAA over-write work-around */
2085 if (adapter->hw.mac_type == e1000_82571)
2086 rar_entries--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
Malli Chilakala26483452005-04-28 19:44:46 -07002088 /* Check for Promiscuous and All Multicast modes */
2089
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 rctl = E1000_READ_REG(hw, RCTL);
2091
2092 if(netdev->flags & IFF_PROMISC) {
2093 rctl |= (E1000_RCTL_UPE | E1000_RCTL_MPE);
2094 } else if(netdev->flags & IFF_ALLMULTI) {
2095 rctl |= E1000_RCTL_MPE;
2096 rctl &= ~E1000_RCTL_UPE;
2097 } else {
2098 rctl &= ~(E1000_RCTL_UPE | E1000_RCTL_MPE);
2099 }
2100
2101 E1000_WRITE_REG(hw, RCTL, rctl);
2102
2103 /* 82542 2.0 needs to be in reset to write receive address registers */
2104
2105 if(hw->mac_type == e1000_82542_rev2_0)
2106 e1000_enter_82542_rst(adapter);
2107
2108 /* load the first 14 multicast address into the exact filters 1-14
2109 * RAR 0 is used for the station MAC adddress
2110 * if there are not 14 addresses, go ahead and clear the filters
Mallikarjuna R Chilakala868d5302005-10-04 06:58:59 -04002111 * -- with 82571 controllers only 0-13 entries are filled here
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 */
2113 mc_ptr = netdev->mc_list;
2114
Mallikarjuna R Chilakala868d5302005-10-04 06:58:59 -04002115 for(i = 1; i < rar_entries; i++) {
2116 if (mc_ptr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 e1000_rar_set(hw, mc_ptr->dmi_addr, i);
2118 mc_ptr = mc_ptr->next;
2119 } else {
2120 E1000_WRITE_REG_ARRAY(hw, RA, i << 1, 0);
2121 E1000_WRITE_REG_ARRAY(hw, RA, (i << 1) + 1, 0);
2122 }
2123 }
2124
2125 /* clear the old settings from the multicast hash table */
2126
2127 for(i = 0; i < E1000_NUM_MTA_REGISTERS; i++)
2128 E1000_WRITE_REG_ARRAY(hw, MTA, i, 0);
2129
2130 /* load any remaining addresses into the hash table */
2131
2132 for(; mc_ptr; mc_ptr = mc_ptr->next) {
2133 hash_value = e1000_hash_mc_addr(hw, mc_ptr->dmi_addr);
2134 e1000_mta_set(hw, hash_value);
2135 }
2136
2137 if(hw->mac_type == e1000_82542_rev2_0)
2138 e1000_leave_82542_rst(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139}
2140
2141/* Need to wait a few seconds after link up to get diagnostic information from
2142 * the phy */
2143
2144static void
2145e1000_update_phy_info(unsigned long data)
2146{
2147 struct e1000_adapter *adapter = (struct e1000_adapter *) data;
2148 e1000_phy_get_info(&adapter->hw, &adapter->phy_info);
2149}
2150
2151/**
2152 * e1000_82547_tx_fifo_stall - Timer Call-back
2153 * @data: pointer to adapter cast into an unsigned long
2154 **/
2155
2156static void
2157e1000_82547_tx_fifo_stall(unsigned long data)
2158{
2159 struct e1000_adapter *adapter = (struct e1000_adapter *) data;
2160 struct net_device *netdev = adapter->netdev;
2161 uint32_t tctl;
2162
2163 if(atomic_read(&adapter->tx_fifo_stall)) {
2164 if((E1000_READ_REG(&adapter->hw, TDT) ==
2165 E1000_READ_REG(&adapter->hw, TDH)) &&
2166 (E1000_READ_REG(&adapter->hw, TDFT) ==
2167 E1000_READ_REG(&adapter->hw, TDFH)) &&
2168 (E1000_READ_REG(&adapter->hw, TDFTS) ==
2169 E1000_READ_REG(&adapter->hw, TDFHS))) {
2170 tctl = E1000_READ_REG(&adapter->hw, TCTL);
2171 E1000_WRITE_REG(&adapter->hw, TCTL,
2172 tctl & ~E1000_TCTL_EN);
2173 E1000_WRITE_REG(&adapter->hw, TDFT,
2174 adapter->tx_head_addr);
2175 E1000_WRITE_REG(&adapter->hw, TDFH,
2176 adapter->tx_head_addr);
2177 E1000_WRITE_REG(&adapter->hw, TDFTS,
2178 adapter->tx_head_addr);
2179 E1000_WRITE_REG(&adapter->hw, TDFHS,
2180 adapter->tx_head_addr);
2181 E1000_WRITE_REG(&adapter->hw, TCTL, tctl);
2182 E1000_WRITE_FLUSH(&adapter->hw);
2183
2184 adapter->tx_fifo_head = 0;
2185 atomic_set(&adapter->tx_fifo_stall, 0);
2186 netif_wake_queue(netdev);
2187 } else {
2188 mod_timer(&adapter->tx_fifo_stall_timer, jiffies + 1);
2189 }
2190 }
2191}
2192
2193/**
2194 * e1000_watchdog - Timer Call-back
2195 * @data: pointer to adapter cast into an unsigned long
2196 **/
2197static void
2198e1000_watchdog(unsigned long data)
2199{
2200 struct e1000_adapter *adapter = (struct e1000_adapter *) data;
2201
2202 /* Do the rest outside of interrupt context */
2203 schedule_work(&adapter->watchdog_task);
2204}
2205
2206static void
2207e1000_watchdog_task(struct e1000_adapter *adapter)
2208{
2209 struct net_device *netdev = adapter->netdev;
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002210 struct e1000_tx_ring *txdr = &adapter->tx_ring[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 uint32_t link;
2212
2213 e1000_check_for_link(&adapter->hw);
Malli Chilakala2d7edb92005-04-28 19:43:52 -07002214 if (adapter->hw.mac_type == e1000_82573) {
2215 e1000_enable_tx_pkt_filtering(&adapter->hw);
2216 if(adapter->mng_vlan_id != adapter->hw.mng_cookie.vlan_id)
2217 e1000_update_mng_vlan(adapter);
2218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219
2220 if((adapter->hw.media_type == e1000_media_type_internal_serdes) &&
2221 !(E1000_READ_REG(&adapter->hw, TXCW) & E1000_TXCW_ANE))
2222 link = !adapter->hw.serdes_link_down;
2223 else
2224 link = E1000_READ_REG(&adapter->hw, STATUS) & E1000_STATUS_LU;
2225
2226 if(link) {
2227 if(!netif_carrier_ok(netdev)) {
2228 e1000_get_speed_and_duplex(&adapter->hw,
2229 &adapter->link_speed,
2230 &adapter->link_duplex);
2231
2232 DPRINTK(LINK, INFO, "NIC Link is Up %d Mbps %s\n",
2233 adapter->link_speed,
2234 adapter->link_duplex == FULL_DUPLEX ?
2235 "Full Duplex" : "Half Duplex");
2236
2237 netif_carrier_on(netdev);
2238 netif_wake_queue(netdev);
2239 mod_timer(&adapter->phy_info_timer, jiffies + 2 * HZ);
2240 adapter->smartspeed = 0;
2241 }
2242 } else {
2243 if(netif_carrier_ok(netdev)) {
2244 adapter->link_speed = 0;
2245 adapter->link_duplex = 0;
2246 DPRINTK(LINK, INFO, "NIC Link is Down\n");
2247 netif_carrier_off(netdev);
2248 netif_stop_queue(netdev);
2249 mod_timer(&adapter->phy_info_timer, jiffies + 2 * HZ);
2250 }
2251
2252 e1000_smartspeed(adapter);
2253 }
2254
2255 e1000_update_stats(adapter);
2256
2257 adapter->hw.tx_packet_delta = adapter->stats.tpt - adapter->tpt_old;
2258 adapter->tpt_old = adapter->stats.tpt;
2259 adapter->hw.collision_delta = adapter->stats.colc - adapter->colc_old;
2260 adapter->colc_old = adapter->stats.colc;
2261
2262 adapter->gorcl = adapter->stats.gorcl - adapter->gorcl_old;
2263 adapter->gorcl_old = adapter->stats.gorcl;
2264 adapter->gotcl = adapter->stats.gotcl - adapter->gotcl_old;
2265 adapter->gotcl_old = adapter->stats.gotcl;
2266
2267 e1000_update_adaptive(&adapter->hw);
2268
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002269 if (adapter->num_queues == 1 && !netif_carrier_ok(netdev)) {
2270 if (E1000_DESC_UNUSED(txdr) + 1 < txdr->count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 /* We've lost link, so the controller stops DMA,
2272 * but we've got queued Tx work that's never going
2273 * to get done, so reset controller to flush Tx.
2274 * (Do the reset outside of interrupt context). */
2275 schedule_work(&adapter->tx_timeout_task);
2276 }
2277 }
2278
2279 /* Dynamic mode for Interrupt Throttle Rate (ITR) */
2280 if(adapter->hw.mac_type >= e1000_82540 && adapter->itr == 1) {
2281 /* Symmetric Tx/Rx gets a reduced ITR=2000; Total
2282 * asymmetrical Tx or Rx gets ITR=8000; everyone
2283 * else is between 2000-8000. */
2284 uint32_t goc = (adapter->gotcl + adapter->gorcl) / 10000;
2285 uint32_t dif = (adapter->gotcl > adapter->gorcl ?
2286 adapter->gotcl - adapter->gorcl :
2287 adapter->gorcl - adapter->gotcl) / 10000;
2288 uint32_t itr = goc > 0 ? (dif * 6000 / goc + 2000) : 8000;
2289 E1000_WRITE_REG(&adapter->hw, ITR, 1000000000 / (itr * 256));
2290 }
2291
2292 /* Cause software interrupt to ensure rx ring is cleaned */
2293 E1000_WRITE_REG(&adapter->hw, ICS, E1000_ICS_RXDMT0);
2294
Malli Chilakala26483452005-04-28 19:44:46 -07002295 /* Force detection of hung controller every watchdog period */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 adapter->detect_tx_hung = TRUE;
2297
Mallikarjuna R Chilakala868d5302005-10-04 06:58:59 -04002298 /* With 82571 controllers, LAA may be overwritten due to controller
2299 * reset from the other port. Set the appropriate LAA in RAR[0] */
2300 if (adapter->hw.mac_type == e1000_82571 && adapter->hw.laa_is_present)
2301 e1000_rar_set(&adapter->hw, adapter->hw.mac_addr, 0);
2302
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 /* Reset the timer */
2304 mod_timer(&adapter->watchdog_timer, jiffies + 2 * HZ);
2305}
2306
2307#define E1000_TX_FLAGS_CSUM 0x00000001
2308#define E1000_TX_FLAGS_VLAN 0x00000002
2309#define E1000_TX_FLAGS_TSO 0x00000004
Malli Chilakala2d7edb92005-04-28 19:43:52 -07002310#define E1000_TX_FLAGS_IPV4 0x00000008
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311#define E1000_TX_FLAGS_VLAN_MASK 0xffff0000
2312#define E1000_TX_FLAGS_VLAN_SHIFT 16
2313
2314static inline int
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002315e1000_tso(struct e1000_adapter *adapter, struct e1000_tx_ring *tx_ring,
2316 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317{
2318#ifdef NETIF_F_TSO
2319 struct e1000_context_desc *context_desc;
2320 unsigned int i;
2321 uint32_t cmd_length = 0;
Malli Chilakala2d7edb92005-04-28 19:43:52 -07002322 uint16_t ipcse = 0, tucse, mss;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 uint8_t ipcss, ipcso, tucss, tucso, hdr_len;
2324 int err;
2325
2326 if(skb_shinfo(skb)->tso_size) {
2327 if (skb_header_cloned(skb)) {
2328 err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2329 if (err)
2330 return err;
2331 }
2332
2333 hdr_len = ((skb->h.raw - skb->data) + (skb->h.th->doff << 2));
2334 mss = skb_shinfo(skb)->tso_size;
Malli Chilakala2d7edb92005-04-28 19:43:52 -07002335 if(skb->protocol == ntohs(ETH_P_IP)) {
2336 skb->nh.iph->tot_len = 0;
2337 skb->nh.iph->check = 0;
2338 skb->h.th->check =
2339 ~csum_tcpudp_magic(skb->nh.iph->saddr,
2340 skb->nh.iph->daddr,
2341 0,
2342 IPPROTO_TCP,
2343 0);
2344 cmd_length = E1000_TXD_CMD_IP;
2345 ipcse = skb->h.raw - skb->data - 1;
2346#ifdef NETIF_F_TSO_IPV6
2347 } else if(skb->protocol == ntohs(ETH_P_IPV6)) {
2348 skb->nh.ipv6h->payload_len = 0;
2349 skb->h.th->check =
2350 ~csum_ipv6_magic(&skb->nh.ipv6h->saddr,
2351 &skb->nh.ipv6h->daddr,
2352 0,
2353 IPPROTO_TCP,
2354 0);
2355 ipcse = 0;
2356#endif
2357 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 ipcss = skb->nh.raw - skb->data;
2359 ipcso = (void *)&(skb->nh.iph->check) - (void *)skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 tucss = skb->h.raw - skb->data;
2361 tucso = (void *)&(skb->h.th->check) - (void *)skb->data;
2362 tucse = 0;
2363
2364 cmd_length |= (E1000_TXD_CMD_DEXT | E1000_TXD_CMD_TSE |
Malli Chilakala2d7edb92005-04-28 19:43:52 -07002365 E1000_TXD_CMD_TCP | (skb->len - (hdr_len)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002367 i = tx_ring->next_to_use;
2368 context_desc = E1000_CONTEXT_DESC(*tx_ring, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369
2370 context_desc->lower_setup.ip_fields.ipcss = ipcss;
2371 context_desc->lower_setup.ip_fields.ipcso = ipcso;
2372 context_desc->lower_setup.ip_fields.ipcse = cpu_to_le16(ipcse);
2373 context_desc->upper_setup.tcp_fields.tucss = tucss;
2374 context_desc->upper_setup.tcp_fields.tucso = tucso;
2375 context_desc->upper_setup.tcp_fields.tucse = cpu_to_le16(tucse);
2376 context_desc->tcp_seg_setup.fields.mss = cpu_to_le16(mss);
2377 context_desc->tcp_seg_setup.fields.hdr_len = hdr_len;
2378 context_desc->cmd_and_length = cpu_to_le32(cmd_length);
2379
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002380 if (++i == tx_ring->count) i = 0;
2381 tx_ring->next_to_use = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382
2383 return 1;
2384 }
2385#endif
2386
2387 return 0;
2388}
2389
2390static inline boolean_t
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002391e1000_tx_csum(struct e1000_adapter *adapter, struct e1000_tx_ring *tx_ring,
2392 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393{
2394 struct e1000_context_desc *context_desc;
2395 unsigned int i;
2396 uint8_t css;
2397
2398 if(likely(skb->ip_summed == CHECKSUM_HW)) {
2399 css = skb->h.raw - skb->data;
2400
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002401 i = tx_ring->next_to_use;
2402 context_desc = E1000_CONTEXT_DESC(*tx_ring, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403
2404 context_desc->upper_setup.tcp_fields.tucss = css;
2405 context_desc->upper_setup.tcp_fields.tucso = css + skb->csum;
2406 context_desc->upper_setup.tcp_fields.tucse = 0;
2407 context_desc->tcp_seg_setup.data = 0;
2408 context_desc->cmd_and_length = cpu_to_le32(E1000_TXD_CMD_DEXT);
2409
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002410 if (unlikely(++i == tx_ring->count)) i = 0;
2411 tx_ring->next_to_use = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412
2413 return TRUE;
2414 }
2415
2416 return FALSE;
2417}
2418
2419#define E1000_MAX_TXD_PWR 12
2420#define E1000_MAX_DATA_PER_TXD (1<<E1000_MAX_TXD_PWR)
2421
2422static inline int
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002423e1000_tx_map(struct e1000_adapter *adapter, struct e1000_tx_ring *tx_ring,
2424 struct sk_buff *skb, unsigned int first, unsigned int max_per_txd,
2425 unsigned int nr_frags, unsigned int mss)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 struct e1000_buffer *buffer_info;
2428 unsigned int len = skb->len;
2429 unsigned int offset = 0, size, count = 0, i;
2430 unsigned int f;
2431 len -= skb->data_len;
2432
2433 i = tx_ring->next_to_use;
2434
2435 while(len) {
2436 buffer_info = &tx_ring->buffer_info[i];
2437 size = min(len, max_per_txd);
2438#ifdef NETIF_F_TSO
2439 /* Workaround for premature desc write-backs
2440 * in TSO mode. Append 4-byte sentinel desc */
2441 if(unlikely(mss && !nr_frags && size == len && size > 8))
2442 size -= 4;
2443#endif
Malli Chilakala97338bd2005-04-28 19:41:46 -07002444 /* work-around for errata 10 and it applies
2445 * to all controllers in PCI-X mode
2446 * The fix is to make sure that the first descriptor of a
2447 * packet is smaller than 2048 - 16 - 16 (or 2016) bytes
2448 */
2449 if(unlikely((adapter->hw.bus_type == e1000_bus_type_pcix) &&
2450 (size > 2015) && count == 0))
2451 size = 2015;
2452
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 /* Workaround for potential 82544 hang in PCI-X. Avoid
2454 * terminating buffers within evenly-aligned dwords. */
2455 if(unlikely(adapter->pcix_82544 &&
2456 !((unsigned long)(skb->data + offset + size - 1) & 4) &&
2457 size > 4))
2458 size -= 4;
2459
2460 buffer_info->length = size;
2461 buffer_info->dma =
2462 pci_map_single(adapter->pdev,
2463 skb->data + offset,
2464 size,
2465 PCI_DMA_TODEVICE);
2466 buffer_info->time_stamp = jiffies;
2467
2468 len -= size;
2469 offset += size;
2470 count++;
2471 if(unlikely(++i == tx_ring->count)) i = 0;
2472 }
2473
2474 for(f = 0; f < nr_frags; f++) {
2475 struct skb_frag_struct *frag;
2476
2477 frag = &skb_shinfo(skb)->frags[f];
2478 len = frag->size;
2479 offset = frag->page_offset;
2480
2481 while(len) {
2482 buffer_info = &tx_ring->buffer_info[i];
2483 size = min(len, max_per_txd);
2484#ifdef NETIF_F_TSO
2485 /* Workaround for premature desc write-backs
2486 * in TSO mode. Append 4-byte sentinel desc */
2487 if(unlikely(mss && f == (nr_frags-1) && size == len && size > 8))
2488 size -= 4;
2489#endif
2490 /* Workaround for potential 82544 hang in PCI-X.
2491 * Avoid terminating buffers within evenly-aligned
2492 * dwords. */
2493 if(unlikely(adapter->pcix_82544 &&
2494 !((unsigned long)(frag->page+offset+size-1) & 4) &&
2495 size > 4))
2496 size -= 4;
2497
2498 buffer_info->length = size;
2499 buffer_info->dma =
2500 pci_map_page(adapter->pdev,
2501 frag->page,
2502 offset,
2503 size,
2504 PCI_DMA_TODEVICE);
2505 buffer_info->time_stamp = jiffies;
2506
2507 len -= size;
2508 offset += size;
2509 count++;
2510 if(unlikely(++i == tx_ring->count)) i = 0;
2511 }
2512 }
2513
2514 i = (i == 0) ? tx_ring->count - 1 : i - 1;
2515 tx_ring->buffer_info[i].skb = skb;
2516 tx_ring->buffer_info[first].next_to_watch = i;
2517
2518 return count;
2519}
2520
2521static inline void
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002522e1000_tx_queue(struct e1000_adapter *adapter, struct e1000_tx_ring *tx_ring,
2523 int tx_flags, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 struct e1000_tx_desc *tx_desc = NULL;
2526 struct e1000_buffer *buffer_info;
2527 uint32_t txd_upper = 0, txd_lower = E1000_TXD_CMD_IFCS;
2528 unsigned int i;
2529
2530 if(likely(tx_flags & E1000_TX_FLAGS_TSO)) {
2531 txd_lower |= E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D |
2532 E1000_TXD_CMD_TSE;
Malli Chilakala2d7edb92005-04-28 19:43:52 -07002533 txd_upper |= E1000_TXD_POPTS_TXSM << 8;
2534
2535 if(likely(tx_flags & E1000_TX_FLAGS_IPV4))
2536 txd_upper |= E1000_TXD_POPTS_IXSM << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 }
2538
2539 if(likely(tx_flags & E1000_TX_FLAGS_CSUM)) {
2540 txd_lower |= E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D;
2541 txd_upper |= E1000_TXD_POPTS_TXSM << 8;
2542 }
2543
2544 if(unlikely(tx_flags & E1000_TX_FLAGS_VLAN)) {
2545 txd_lower |= E1000_TXD_CMD_VLE;
2546 txd_upper |= (tx_flags & E1000_TX_FLAGS_VLAN_MASK);
2547 }
2548
2549 i = tx_ring->next_to_use;
2550
2551 while(count--) {
2552 buffer_info = &tx_ring->buffer_info[i];
2553 tx_desc = E1000_TX_DESC(*tx_ring, i);
2554 tx_desc->buffer_addr = cpu_to_le64(buffer_info->dma);
2555 tx_desc->lower.data =
2556 cpu_to_le32(txd_lower | buffer_info->length);
2557 tx_desc->upper.data = cpu_to_le32(txd_upper);
2558 if(unlikely(++i == tx_ring->count)) i = 0;
2559 }
2560
2561 tx_desc->lower.data |= cpu_to_le32(adapter->txd_cmd);
2562
2563 /* Force memory writes to complete before letting h/w
2564 * know there are new descriptors to fetch. (Only
2565 * applicable for weak-ordered memory model archs,
2566 * such as IA-64). */
2567 wmb();
2568
2569 tx_ring->next_to_use = i;
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002570 writel(i, adapter->hw.hw_addr + tx_ring->tdt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571}
2572
2573/**
2574 * 82547 workaround to avoid controller hang in half-duplex environment.
2575 * The workaround is to avoid queuing a large packet that would span
2576 * the internal Tx FIFO ring boundary by notifying the stack to resend
2577 * the packet at a later time. This gives the Tx FIFO an opportunity to
2578 * flush all packets. When that occurs, we reset the Tx FIFO pointers
2579 * to the beginning of the Tx FIFO.
2580 **/
2581
2582#define E1000_FIFO_HDR 0x10
2583#define E1000_82547_PAD_LEN 0x3E0
2584
2585static inline int
2586e1000_82547_fifo_workaround(struct e1000_adapter *adapter, struct sk_buff *skb)
2587{
2588 uint32_t fifo_space = adapter->tx_fifo_size - adapter->tx_fifo_head;
2589 uint32_t skb_fifo_len = skb->len + E1000_FIFO_HDR;
2590
2591 E1000_ROUNDUP(skb_fifo_len, E1000_FIFO_HDR);
2592
2593 if(adapter->link_duplex != HALF_DUPLEX)
2594 goto no_fifo_stall_required;
2595
2596 if(atomic_read(&adapter->tx_fifo_stall))
2597 return 1;
2598
2599 if(skb_fifo_len >= (E1000_82547_PAD_LEN + fifo_space)) {
2600 atomic_set(&adapter->tx_fifo_stall, 1);
2601 return 1;
2602 }
2603
2604no_fifo_stall_required:
2605 adapter->tx_fifo_head += skb_fifo_len;
2606 if(adapter->tx_fifo_head >= adapter->tx_fifo_size)
2607 adapter->tx_fifo_head -= adapter->tx_fifo_size;
2608 return 0;
2609}
2610
Malli Chilakala2d7edb92005-04-28 19:43:52 -07002611#define MINIMUM_DHCP_PACKET_SIZE 282
2612static inline int
2613e1000_transfer_dhcp_info(struct e1000_adapter *adapter, struct sk_buff *skb)
2614{
2615 struct e1000_hw *hw = &adapter->hw;
2616 uint16_t length, offset;
2617 if(vlan_tx_tag_present(skb)) {
2618 if(!((vlan_tx_tag_get(skb) == adapter->hw.mng_cookie.vlan_id) &&
2619 ( adapter->hw.mng_cookie.status &
2620 E1000_MNG_DHCP_COOKIE_STATUS_VLAN_SUPPORT)) )
2621 return 0;
2622 }
2623 if(htons(ETH_P_IP) == skb->protocol) {
2624 const struct iphdr *ip = skb->nh.iph;
2625 if(IPPROTO_UDP == ip->protocol) {
2626 struct udphdr *udp = (struct udphdr *)(skb->h.uh);
2627 if(ntohs(udp->dest) == 67) {
2628 offset = (uint8_t *)udp + 8 - skb->data;
2629 length = skb->len - offset;
2630
2631 return e1000_mng_write_dhcp_info(hw,
2632 (uint8_t *)udp + 8, length);
2633 }
2634 }
2635 } else if((skb->len > MINIMUM_DHCP_PACKET_SIZE) && (!skb->protocol)) {
2636 struct ethhdr *eth = (struct ethhdr *) skb->data;
2637 if((htons(ETH_P_IP) == eth->h_proto)) {
2638 const struct iphdr *ip =
2639 (struct iphdr *)((uint8_t *)skb->data+14);
2640 if(IPPROTO_UDP == ip->protocol) {
2641 struct udphdr *udp =
2642 (struct udphdr *)((uint8_t *)ip +
2643 (ip->ihl << 2));
2644 if(ntohs(udp->dest) == 67) {
2645 offset = (uint8_t *)udp + 8 - skb->data;
2646 length = skb->len - offset;
2647
2648 return e1000_mng_write_dhcp_info(hw,
2649 (uint8_t *)udp + 8,
2650 length);
2651 }
2652 }
2653 }
2654 }
2655 return 0;
2656}
2657
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658#define TXD_USE_COUNT(S, X) (((S) >> (X)) + 1 )
2659static int
2660e1000_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
2661{
Malli Chilakala60490fe2005-06-17 17:41:45 -07002662 struct e1000_adapter *adapter = netdev_priv(netdev);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002663 struct e1000_tx_ring *tx_ring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 unsigned int first, max_per_txd = E1000_MAX_DATA_PER_TXD;
2665 unsigned int max_txd_pwr = E1000_MAX_TXD_PWR;
2666 unsigned int tx_flags = 0;
2667 unsigned int len = skb->len;
2668 unsigned long flags;
2669 unsigned int nr_frags = 0;
2670 unsigned int mss = 0;
2671 int count = 0;
2672 int tso;
2673 unsigned int f;
2674 len -= skb->data_len;
2675
Mallikarjuna R Chilakala24025e4ec2005-10-04 07:03:23 -04002676#ifdef CONFIG_E1000_MQ
2677 tx_ring = *per_cpu_ptr(adapter->cpu_tx_ring, smp_processor_id());
2678#else
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002679 tx_ring = adapter->tx_ring;
Mallikarjuna R Chilakala24025e4ec2005-10-04 07:03:23 -04002680#endif
2681
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002682 if (unlikely(skb->len <= 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 dev_kfree_skb_any(skb);
2684 return NETDEV_TX_OK;
2685 }
2686
2687#ifdef NETIF_F_TSO
2688 mss = skb_shinfo(skb)->tso_size;
Malli Chilakala26483452005-04-28 19:44:46 -07002689 /* The controller does a simple calculation to
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 * make sure there is enough room in the FIFO before
2691 * initiating the DMA for each buffer. The calc is:
2692 * 4 = ceil(buffer len/mss). To make sure we don't
2693 * overrun the FIFO, adjust the max buffer len if mss
2694 * drops. */
2695 if(mss) {
2696 max_per_txd = min(mss << 2, max_per_txd);
2697 max_txd_pwr = fls(max_per_txd) - 1;
2698 }
2699
2700 if((mss) || (skb->ip_summed == CHECKSUM_HW))
2701 count++;
Malli Chilakala26483452005-04-28 19:44:46 -07002702 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703#else
2704 if(skb->ip_summed == CHECKSUM_HW)
2705 count++;
2706#endif
2707 count += TXD_USE_COUNT(len, max_txd_pwr);
2708
2709 if(adapter->pcix_82544)
2710 count++;
2711
Malli Chilakala97338bd2005-04-28 19:41:46 -07002712 /* work-around for errata 10 and it applies to all controllers
2713 * in PCI-X mode, so add one more descriptor to the count
2714 */
2715 if(unlikely((adapter->hw.bus_type == e1000_bus_type_pcix) &&
2716 (len > 2015)))
2717 count++;
2718
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 nr_frags = skb_shinfo(skb)->nr_frags;
2720 for(f = 0; f < nr_frags; f++)
2721 count += TXD_USE_COUNT(skb_shinfo(skb)->frags[f].size,
2722 max_txd_pwr);
2723 if(adapter->pcix_82544)
2724 count += nr_frags;
2725
Mallikarjuna R Chilakala868d5302005-10-04 06:58:59 -04002726#ifdef NETIF_F_TSO
2727 /* TSO Workaround for 82571/2 Controllers -- if skb->data
2728 * points to just header, pull a few bytes of payload from
2729 * frags into skb->data */
2730 if (skb_shinfo(skb)->tso_size) {
2731 uint8_t hdr_len;
2732 hdr_len = ((skb->h.raw - skb->data) + (skb->h.th->doff << 2));
2733 if (skb->data_len && (hdr_len < (skb->len - skb->data_len)) &&
2734 (adapter->hw.mac_type == e1000_82571 ||
2735 adapter->hw.mac_type == e1000_82572)) {
2736 unsigned int pull_size;
2737 pull_size = min((unsigned int)4, skb->data_len);
2738 if (!__pskb_pull_tail(skb, pull_size)) {
2739 printk(KERN_ERR "__pskb_pull_tail failed.\n");
2740 dev_kfree_skb_any(skb);
2741 return -EFAULT;
2742 }
2743 }
2744 }
2745#endif
2746
Malli Chilakala2d7edb92005-04-28 19:43:52 -07002747 if(adapter->hw.tx_pkt_filtering && (adapter->hw.mac_type == e1000_82573) )
2748 e1000_transfer_dhcp_info(adapter, skb);
2749
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002750 local_irq_save(flags);
2751 if (!spin_trylock(&tx_ring->tx_lock)) {
2752 /* Collision - tell upper layer to requeue */
2753 local_irq_restore(flags);
2754 return NETDEV_TX_LOCKED;
2755 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756
2757 /* need: count + 2 desc gap to keep tail from touching
2758 * head, otherwise try next time */
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002759 if (unlikely(E1000_DESC_UNUSED(tx_ring) < count + 2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 netif_stop_queue(netdev);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002761 spin_unlock_irqrestore(&tx_ring->tx_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 return NETDEV_TX_BUSY;
2763 }
2764
2765 if(unlikely(adapter->hw.mac_type == e1000_82547)) {
2766 if(unlikely(e1000_82547_fifo_workaround(adapter, skb))) {
2767 netif_stop_queue(netdev);
2768 mod_timer(&adapter->tx_fifo_stall_timer, jiffies);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002769 spin_unlock_irqrestore(&tx_ring->tx_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 return NETDEV_TX_BUSY;
2771 }
2772 }
2773
2774 if(unlikely(adapter->vlgrp && vlan_tx_tag_present(skb))) {
2775 tx_flags |= E1000_TX_FLAGS_VLAN;
2776 tx_flags |= (vlan_tx_tag_get(skb) << E1000_TX_FLAGS_VLAN_SHIFT);
2777 }
2778
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002779 first = tx_ring->next_to_use;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002781 tso = e1000_tso(adapter, tx_ring, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 if (tso < 0) {
2783 dev_kfree_skb_any(skb);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002784 spin_unlock_irqrestore(&tx_ring->tx_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 return NETDEV_TX_OK;
2786 }
2787
2788 if (likely(tso))
2789 tx_flags |= E1000_TX_FLAGS_TSO;
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002790 else if (likely(e1000_tx_csum(adapter, tx_ring, skb)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 tx_flags |= E1000_TX_FLAGS_CSUM;
2792
Malli Chilakala2d7edb92005-04-28 19:43:52 -07002793 /* Old method was to assume IPv4 packet by default if TSO was enabled.
Mallikarjuna R Chilakala868d5302005-10-04 06:58:59 -04002794 * 82571 hardware supports TSO capabilities for IPv6 as well...
Malli Chilakala2d7edb92005-04-28 19:43:52 -07002795 * no longer assume, we must. */
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002796 if (likely(skb->protocol == ntohs(ETH_P_IP)))
Malli Chilakala2d7edb92005-04-28 19:43:52 -07002797 tx_flags |= E1000_TX_FLAGS_IPV4;
2798
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002799 e1000_tx_queue(adapter, tx_ring, tx_flags,
2800 e1000_tx_map(adapter, tx_ring, skb, first,
2801 max_per_txd, nr_frags, mss));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802
2803 netdev->trans_start = jiffies;
2804
2805 /* Make sure there is space in the ring for the next send. */
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002806 if (unlikely(E1000_DESC_UNUSED(tx_ring) < MAX_SKB_FRAGS + 2))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 netif_stop_queue(netdev);
2808
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04002809 spin_unlock_irqrestore(&tx_ring->tx_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 return NETDEV_TX_OK;
2811}
2812
2813/**
2814 * e1000_tx_timeout - Respond to a Tx Hang
2815 * @netdev: network interface device structure
2816 **/
2817
2818static void
2819e1000_tx_timeout(struct net_device *netdev)
2820{
Malli Chilakala60490fe2005-06-17 17:41:45 -07002821 struct e1000_adapter *adapter = netdev_priv(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822
2823 /* Do the reset outside of interrupt context */
2824 schedule_work(&adapter->tx_timeout_task);
2825}
2826
2827static void
2828e1000_tx_timeout_task(struct net_device *netdev)
2829{
Malli Chilakala60490fe2005-06-17 17:41:45 -07002830 struct e1000_adapter *adapter = netdev_priv(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831
2832 e1000_down(adapter);
2833 e1000_up(adapter);
2834}
2835
2836/**
2837 * e1000_get_stats - Get System Network Statistics
2838 * @netdev: network interface device structure
2839 *
2840 * Returns the address of the device statistics structure.
2841 * The statistics are actually updated from the timer callback.
2842 **/
2843
2844static struct net_device_stats *
2845e1000_get_stats(struct net_device *netdev)
2846{
Malli Chilakala60490fe2005-06-17 17:41:45 -07002847 struct e1000_adapter *adapter = netdev_priv(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848
2849 e1000_update_stats(adapter);
2850 return &adapter->net_stats;
2851}
2852
2853/**
2854 * e1000_change_mtu - Change the Maximum Transfer Unit
2855 * @netdev: network interface device structure
2856 * @new_mtu: new value for maximum frame size
2857 *
2858 * Returns 0 on success, negative on failure
2859 **/
2860
2861static int
2862e1000_change_mtu(struct net_device *netdev, int new_mtu)
2863{
Malli Chilakala60490fe2005-06-17 17:41:45 -07002864 struct e1000_adapter *adapter = netdev_priv(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 int max_frame = new_mtu + ENET_HEADER_SIZE + ETHERNET_FCS_SIZE;
2866
2867 if((max_frame < MINIMUM_ETHERNET_FRAME_SIZE) ||
2868 (max_frame > MAX_JUMBO_FRAME_SIZE)) {
2869 DPRINTK(PROBE, ERR, "Invalid MTU setting\n");
2870 return -EINVAL;
2871 }
2872
Mallikarjuna R Chilakala868d5302005-10-04 06:58:59 -04002873#define MAX_STD_JUMBO_FRAME_SIZE 9234
Malli Chilakala2d7edb92005-04-28 19:43:52 -07002874 /* might want this to be bigger enum check... */
Mallikarjuna R Chilakala868d5302005-10-04 06:58:59 -04002875 /* 82571 controllers limit jumbo frame size to 10500 bytes */
2876 if ((adapter->hw.mac_type == e1000_82571 ||
2877 adapter->hw.mac_type == e1000_82572) &&
2878 max_frame > MAX_STD_JUMBO_FRAME_SIZE) {
2879 DPRINTK(PROBE, ERR, "MTU > 9216 bytes not supported "
2880 "on 82571 and 82572 controllers.\n");
2881 return -EINVAL;
2882 }
2883
2884 if(adapter->hw.mac_type == e1000_82573 &&
Malli Chilakala2d7edb92005-04-28 19:43:52 -07002885 max_frame > MAXIMUM_ETHERNET_FRAME_SIZE) {
2886 DPRINTK(PROBE, ERR, "Jumbo Frames not supported "
2887 "on 82573\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 }
2890
Malli Chilakala2d7edb92005-04-28 19:43:52 -07002891 if(adapter->hw.mac_type > e1000_82547_rev_2) {
2892 adapter->rx_buffer_len = max_frame;
2893 E1000_ROUNDUP(adapter->rx_buffer_len, 1024);
2894 } else {
2895 if(unlikely((adapter->hw.mac_type < e1000_82543) &&
2896 (max_frame > MAXIMUM_ETHERNET_FRAME_SIZE))) {
2897 DPRINTK(PROBE, ERR, "Jumbo Frames not supported "
2898 "on 82542\n");
2899 return -EINVAL;
2900
2901 } else {
2902 if(max_frame <= E1000_RXBUFFER_2048) {
2903 adapter->rx_buffer_len = E1000_RXBUFFER_2048;
2904 } else if(max_frame <= E1000_RXBUFFER_4096) {
2905 adapter->rx_buffer_len = E1000_RXBUFFER_4096;
2906 } else if(max_frame <= E1000_RXBUFFER_8192) {
2907 adapter->rx_buffer_len = E1000_RXBUFFER_8192;
2908 } else if(max_frame <= E1000_RXBUFFER_16384) {
2909 adapter->rx_buffer_len = E1000_RXBUFFER_16384;
2910 }
2911 }
2912 }
2913
2914 netdev->mtu = new_mtu;
2915
2916 if(netif_running(netdev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 e1000_down(adapter);
2918 e1000_up(adapter);
2919 }
2920
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 adapter->hw.max_frame_size = max_frame;
2922
2923 return 0;
2924}
2925
2926/**
2927 * e1000_update_stats - Update the board statistics counters
2928 * @adapter: board private structure
2929 **/
2930
2931void
2932e1000_update_stats(struct e1000_adapter *adapter)
2933{
2934 struct e1000_hw *hw = &adapter->hw;
2935 unsigned long flags;
2936 uint16_t phy_tmp;
2937
2938#define PHY_IDLE_ERROR_COUNT_MASK 0x00FF
2939
2940 spin_lock_irqsave(&adapter->stats_lock, flags);
2941
2942 /* these counters are modified from e1000_adjust_tbi_stats,
2943 * called from the interrupt context, so they must only
2944 * be written while holding adapter->stats_lock
2945 */
2946
2947 adapter->stats.crcerrs += E1000_READ_REG(hw, CRCERRS);
2948 adapter->stats.gprc += E1000_READ_REG(hw, GPRC);
2949 adapter->stats.gorcl += E1000_READ_REG(hw, GORCL);
2950 adapter->stats.gorch += E1000_READ_REG(hw, GORCH);
2951 adapter->stats.bprc += E1000_READ_REG(hw, BPRC);
2952 adapter->stats.mprc += E1000_READ_REG(hw, MPRC);
2953 adapter->stats.roc += E1000_READ_REG(hw, ROC);
2954 adapter->stats.prc64 += E1000_READ_REG(hw, PRC64);
2955 adapter->stats.prc127 += E1000_READ_REG(hw, PRC127);
2956 adapter->stats.prc255 += E1000_READ_REG(hw, PRC255);
2957 adapter->stats.prc511 += E1000_READ_REG(hw, PRC511);
2958 adapter->stats.prc1023 += E1000_READ_REG(hw, PRC1023);
2959 adapter->stats.prc1522 += E1000_READ_REG(hw, PRC1522);
2960
2961 adapter->stats.symerrs += E1000_READ_REG(hw, SYMERRS);
2962 adapter->stats.mpc += E1000_READ_REG(hw, MPC);
2963 adapter->stats.scc += E1000_READ_REG(hw, SCC);
2964 adapter->stats.ecol += E1000_READ_REG(hw, ECOL);
2965 adapter->stats.mcc += E1000_READ_REG(hw, MCC);
2966 adapter->stats.latecol += E1000_READ_REG(hw, LATECOL);
2967 adapter->stats.dc += E1000_READ_REG(hw, DC);
2968 adapter->stats.sec += E1000_READ_REG(hw, SEC);
2969 adapter->stats.rlec += E1000_READ_REG(hw, RLEC);
2970 adapter->stats.xonrxc += E1000_READ_REG(hw, XONRXC);
2971 adapter->stats.xontxc += E1000_READ_REG(hw, XONTXC);
2972 adapter->stats.xoffrxc += E1000_READ_REG(hw, XOFFRXC);
2973 adapter->stats.xofftxc += E1000_READ_REG(hw, XOFFTXC);
2974 adapter->stats.fcruc += E1000_READ_REG(hw, FCRUC);
2975 adapter->stats.gptc += E1000_READ_REG(hw, GPTC);
2976 adapter->stats.gotcl += E1000_READ_REG(hw, GOTCL);
2977 adapter->stats.gotch += E1000_READ_REG(hw, GOTCH);
2978 adapter->stats.rnbc += E1000_READ_REG(hw, RNBC);
2979 adapter->stats.ruc += E1000_READ_REG(hw, RUC);
2980 adapter->stats.rfc += E1000_READ_REG(hw, RFC);
2981 adapter->stats.rjc += E1000_READ_REG(hw, RJC);
2982 adapter->stats.torl += E1000_READ_REG(hw, TORL);
2983 adapter->stats.torh += E1000_READ_REG(hw, TORH);
2984 adapter->stats.totl += E1000_READ_REG(hw, TOTL);
2985 adapter->stats.toth += E1000_READ_REG(hw, TOTH);
2986 adapter->stats.tpr += E1000_READ_REG(hw, TPR);
2987 adapter->stats.ptc64 += E1000_READ_REG(hw, PTC64);
2988 adapter->stats.ptc127 += E1000_READ_REG(hw, PTC127);
2989 adapter->stats.ptc255 += E1000_READ_REG(hw, PTC255);
2990 adapter->stats.ptc511 += E1000_READ_REG(hw, PTC511);
2991 adapter->stats.ptc1023 += E1000_READ_REG(hw, PTC1023);
2992 adapter->stats.ptc1522 += E1000_READ_REG(hw, PTC1522);
2993 adapter->stats.mptc += E1000_READ_REG(hw, MPTC);
2994 adapter->stats.bptc += E1000_READ_REG(hw, BPTC);
2995
2996 /* used for adaptive IFS */
2997
2998 hw->tx_packet_delta = E1000_READ_REG(hw, TPT);
2999 adapter->stats.tpt += hw->tx_packet_delta;
3000 hw->collision_delta = E1000_READ_REG(hw, COLC);
3001 adapter->stats.colc += hw->collision_delta;
3002
3003 if(hw->mac_type >= e1000_82543) {
3004 adapter->stats.algnerrc += E1000_READ_REG(hw, ALGNERRC);
3005 adapter->stats.rxerrc += E1000_READ_REG(hw, RXERRC);
3006 adapter->stats.tncrs += E1000_READ_REG(hw, TNCRS);
3007 adapter->stats.cexterr += E1000_READ_REG(hw, CEXTERR);
3008 adapter->stats.tsctc += E1000_READ_REG(hw, TSCTC);
3009 adapter->stats.tsctfc += E1000_READ_REG(hw, TSCTFC);
3010 }
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003011 if(hw->mac_type > e1000_82547_rev_2) {
3012 adapter->stats.iac += E1000_READ_REG(hw, IAC);
3013 adapter->stats.icrxoc += E1000_READ_REG(hw, ICRXOC);
3014 adapter->stats.icrxptc += E1000_READ_REG(hw, ICRXPTC);
3015 adapter->stats.icrxatc += E1000_READ_REG(hw, ICRXATC);
3016 adapter->stats.ictxptc += E1000_READ_REG(hw, ICTXPTC);
3017 adapter->stats.ictxatc += E1000_READ_REG(hw, ICTXATC);
3018 adapter->stats.ictxqec += E1000_READ_REG(hw, ICTXQEC);
3019 adapter->stats.ictxqmtc += E1000_READ_REG(hw, ICTXQMTC);
3020 adapter->stats.icrxdmtc += E1000_READ_REG(hw, ICRXDMTC);
3021 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022
3023 /* Fill out the OS statistics structure */
3024
3025 adapter->net_stats.rx_packets = adapter->stats.gprc;
3026 adapter->net_stats.tx_packets = adapter->stats.gptc;
3027 adapter->net_stats.rx_bytes = adapter->stats.gorcl;
3028 adapter->net_stats.tx_bytes = adapter->stats.gotcl;
3029 adapter->net_stats.multicast = adapter->stats.mprc;
3030 adapter->net_stats.collisions = adapter->stats.colc;
3031
3032 /* Rx Errors */
3033
3034 adapter->net_stats.rx_errors = adapter->stats.rxerrc +
3035 adapter->stats.crcerrs + adapter->stats.algnerrc +
Malli Chilakala6d915752005-04-28 19:41:11 -07003036 adapter->stats.rlec + adapter->stats.mpc +
3037 adapter->stats.cexterr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 adapter->net_stats.rx_length_errors = adapter->stats.rlec;
3039 adapter->net_stats.rx_crc_errors = adapter->stats.crcerrs;
3040 adapter->net_stats.rx_frame_errors = adapter->stats.algnerrc;
3041 adapter->net_stats.rx_fifo_errors = adapter->stats.mpc;
3042 adapter->net_stats.rx_missed_errors = adapter->stats.mpc;
3043
3044 /* Tx Errors */
3045
3046 adapter->net_stats.tx_errors = adapter->stats.ecol +
3047 adapter->stats.latecol;
3048 adapter->net_stats.tx_aborted_errors = adapter->stats.ecol;
3049 adapter->net_stats.tx_window_errors = adapter->stats.latecol;
3050 adapter->net_stats.tx_carrier_errors = adapter->stats.tncrs;
3051
3052 /* Tx Dropped needs to be maintained elsewhere */
3053
3054 /* Phy Stats */
3055
3056 if(hw->media_type == e1000_media_type_copper) {
3057 if((adapter->link_speed == SPEED_1000) &&
3058 (!e1000_read_phy_reg(hw, PHY_1000T_STATUS, &phy_tmp))) {
3059 phy_tmp &= PHY_IDLE_ERROR_COUNT_MASK;
3060 adapter->phy_stats.idle_errors += phy_tmp;
3061 }
3062
3063 if((hw->mac_type <= e1000_82546) &&
3064 (hw->phy_type == e1000_phy_m88) &&
3065 !e1000_read_phy_reg(hw, M88E1000_RX_ERR_CNTR, &phy_tmp))
3066 adapter->phy_stats.receive_errors += phy_tmp;
3067 }
3068
3069 spin_unlock_irqrestore(&adapter->stats_lock, flags);
3070}
3071
Mallikarjuna R Chilakala24025e4ec2005-10-04 07:03:23 -04003072#ifdef CONFIG_E1000_MQ
3073void
3074e1000_rx_schedule(void *data)
3075{
3076 struct net_device *poll_dev, *netdev = data;
3077 struct e1000_adapter *adapter = netdev->priv;
3078 int this_cpu = get_cpu();
3079
3080 poll_dev = *per_cpu_ptr(adapter->cpu_netdev, this_cpu);
3081 if (poll_dev == NULL) {
3082 put_cpu();
3083 return;
3084 }
3085
3086 if (likely(netif_rx_schedule_prep(poll_dev)))
3087 __netif_rx_schedule(poll_dev);
3088 else
3089 e1000_irq_enable(adapter);
3090
3091 put_cpu();
3092}
3093#endif
3094
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095/**
3096 * e1000_intr - Interrupt Handler
3097 * @irq: interrupt number
3098 * @data: pointer to a network interface device structure
3099 * @pt_regs: CPU registers structure
3100 **/
3101
3102static irqreturn_t
3103e1000_intr(int irq, void *data, struct pt_regs *regs)
3104{
3105 struct net_device *netdev = data;
Malli Chilakala60490fe2005-06-17 17:41:45 -07003106 struct e1000_adapter *adapter = netdev_priv(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 struct e1000_hw *hw = &adapter->hw;
3108 uint32_t icr = E1000_READ_REG(hw, ICR);
Andrew Morton166d8232005-10-16 13:32:14 -07003109#if defined(CONFIG_E1000_NAPI) && defined(CONFIG_E1000_MQ) || !defined(CONFIG_E1000_NAPI)
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003110 int i;
Jeff Garzikbe2b28e2005-10-04 07:13:43 -04003111#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112
3113 if(unlikely(!icr))
3114 return IRQ_NONE; /* Not our interrupt */
3115
3116 if(unlikely(icr & (E1000_ICR_RXSEQ | E1000_ICR_LSC))) {
3117 hw->get_link_status = 1;
3118 mod_timer(&adapter->watchdog_timer, jiffies);
3119 }
3120
3121#ifdef CONFIG_E1000_NAPI
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003122 atomic_inc(&adapter->irq_sem);
3123 E1000_WRITE_REG(hw, IMC, ~0);
3124 E1000_WRITE_FLUSH(hw);
Mallikarjuna R Chilakala24025e4ec2005-10-04 07:03:23 -04003125#ifdef CONFIG_E1000_MQ
3126 if (atomic_read(&adapter->rx_sched_call_data.count) == 0) {
3127 cpu_set(adapter->cpu_for_queue[0],
3128 adapter->rx_sched_call_data.cpumask);
3129 for (i = 1; i < adapter->num_queues; i++) {
3130 cpu_set(adapter->cpu_for_queue[i],
3131 adapter->rx_sched_call_data.cpumask);
3132 atomic_inc(&adapter->irq_sem);
3133 }
3134 atomic_set(&adapter->rx_sched_call_data.count, i);
3135 smp_call_async_mask(&adapter->rx_sched_call_data);
3136 } else {
3137 printk("call_data.count == %u\n", atomic_read(&adapter->rx_sched_call_data.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 }
Jeff Garzikbe2b28e2005-10-04 07:13:43 -04003139#else /* if !CONFIG_E1000_MQ */
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003140 if (likely(netif_rx_schedule_prep(&adapter->polling_netdev[0])))
3141 __netif_rx_schedule(&adapter->polling_netdev[0]);
3142 else
3143 e1000_irq_enable(adapter);
Jeff Garzikbe2b28e2005-10-04 07:13:43 -04003144#endif /* CONFIG_E1000_MQ */
3145
3146#else /* if !CONFIG_E1000_NAPI */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 /* Writing IMC and IMS is needed for 82547.
3148 Due to Hub Link bus being occupied, an interrupt
3149 de-assertion message is not able to be sent.
3150 When an interrupt assertion message is generated later,
3151 two messages are re-ordered and sent out.
3152 That causes APIC to think 82547 is in de-assertion
3153 state, while 82547 is in assertion state, resulting
3154 in dead lock. Writing IMC forces 82547 into
3155 de-assertion state.
3156 */
3157 if(hw->mac_type == e1000_82547 || hw->mac_type == e1000_82547_rev_2){
3158 atomic_inc(&adapter->irq_sem);
Malli Chilakala26483452005-04-28 19:44:46 -07003159 E1000_WRITE_REG(hw, IMC, ~0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 }
3161
3162 for(i = 0; i < E1000_MAX_INTR; i++)
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003163 if(unlikely(!adapter->clean_rx(adapter, adapter->rx_ring) &
3164 !e1000_clean_tx_irq(adapter, adapter->tx_ring)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 break;
3166
3167 if(hw->mac_type == e1000_82547 || hw->mac_type == e1000_82547_rev_2)
3168 e1000_irq_enable(adapter);
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003169
Jeff Garzikbe2b28e2005-10-04 07:13:43 -04003170#endif /* CONFIG_E1000_NAPI */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171
3172 return IRQ_HANDLED;
3173}
3174
3175#ifdef CONFIG_E1000_NAPI
3176/**
3177 * e1000_clean - NAPI Rx polling callback
3178 * @adapter: board private structure
3179 **/
3180
3181static int
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003182e1000_clean(struct net_device *poll_dev, int *budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183{
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003184 struct e1000_adapter *adapter;
3185 int work_to_do = min(*budget, poll_dev->quota);
3186 int tx_cleaned, i = 0, work_done = 0;
Malli Chilakala26483452005-04-28 19:44:46 -07003187
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003188 /* Must NOT use netdev_priv macro here. */
3189 adapter = poll_dev->priv;
3190
3191 /* Keep link state information with original netdev */
3192 if (!netif_carrier_ok(adapter->netdev))
3193 goto quit_polling;
3194
3195 while (poll_dev != &adapter->polling_netdev[i]) {
3196 i++;
3197 if (unlikely(i == adapter->num_queues))
3198 BUG();
3199 }
3200
3201 tx_cleaned = e1000_clean_tx_irq(adapter, &adapter->tx_ring[i]);
3202 adapter->clean_rx(adapter, &adapter->rx_ring[i],
3203 &work_done, work_to_do);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204
3205 *budget -= work_done;
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003206 poll_dev->quota -= work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207
Malli Chilakala2b028932005-06-17 17:46:06 -07003208 /* If no Tx and not enough Rx work done, exit the polling mode */
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003209 if((!tx_cleaned && (work_done == 0)) ||
3210 !netif_running(adapter->netdev)) {
3211quit_polling:
3212 netif_rx_complete(poll_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 e1000_irq_enable(adapter);
3214 return 0;
3215 }
3216
3217 return 1;
3218}
3219
3220#endif
3221/**
3222 * e1000_clean_tx_irq - Reclaim resources after transmit completes
3223 * @adapter: board private structure
3224 **/
3225
3226static boolean_t
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003227e1000_clean_tx_irq(struct e1000_adapter *adapter,
3228 struct e1000_tx_ring *tx_ring)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 struct net_device *netdev = adapter->netdev;
3231 struct e1000_tx_desc *tx_desc, *eop_desc;
3232 struct e1000_buffer *buffer_info;
3233 unsigned int i, eop;
3234 boolean_t cleaned = FALSE;
3235
3236 i = tx_ring->next_to_clean;
3237 eop = tx_ring->buffer_info[i].next_to_watch;
3238 eop_desc = E1000_TX_DESC(*tx_ring, eop);
3239
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003240 while (eop_desc->upper.data & cpu_to_le32(E1000_TXD_STAT_DD)) {
Malli Chilakala27012342005-04-28 19:40:28 -07003241 /* Premature writeback of Tx descriptors clear (free buffers
3242 * and unmap pci_mapping) previous_buffer_info */
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003243 if (likely(tx_ring->previous_buffer_info.skb != NULL)) {
Malli Chilakala27012342005-04-28 19:40:28 -07003244 e1000_unmap_and_free_tx_resource(adapter,
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003245 &tx_ring->previous_buffer_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 }
3247
3248 for(cleaned = FALSE; !cleaned; ) {
3249 tx_desc = E1000_TX_DESC(*tx_ring, i);
3250 buffer_info = &tx_ring->buffer_info[i];
3251 cleaned = (i == eop);
3252
Malli Chilakala27012342005-04-28 19:40:28 -07003253#ifdef NETIF_F_TSO
3254 if (!(netdev->features & NETIF_F_TSO)) {
3255#endif
3256 e1000_unmap_and_free_tx_resource(adapter,
3257 buffer_info);
3258#ifdef NETIF_F_TSO
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 } else {
Malli Chilakala27012342005-04-28 19:40:28 -07003260 if (cleaned) {
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003261 memcpy(&tx_ring->previous_buffer_info,
Malli Chilakala27012342005-04-28 19:40:28 -07003262 buffer_info,
3263 sizeof(struct e1000_buffer));
3264 memset(buffer_info, 0,
3265 sizeof(struct e1000_buffer));
3266 } else {
3267 e1000_unmap_and_free_tx_resource(
3268 adapter, buffer_info);
3269 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 }
Malli Chilakala27012342005-04-28 19:40:28 -07003271#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272
3273 tx_desc->buffer_addr = 0;
3274 tx_desc->lower.data = 0;
3275 tx_desc->upper.data = 0;
3276
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277 if(unlikely(++i == tx_ring->count)) i = 0;
3278 }
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003279
3280 tx_ring->pkt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281
3282 eop = tx_ring->buffer_info[i].next_to_watch;
3283 eop_desc = E1000_TX_DESC(*tx_ring, eop);
3284 }
3285
3286 tx_ring->next_to_clean = i;
3287
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003288 spin_lock(&tx_ring->tx_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289
3290 if(unlikely(cleaned && netif_queue_stopped(netdev) &&
3291 netif_carrier_ok(netdev)))
3292 netif_wake_queue(netdev);
3293
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003294 spin_unlock(&tx_ring->tx_lock);
Malli Chilakala26483452005-04-28 19:44:46 -07003295
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003296 if (adapter->detect_tx_hung) {
Malli Chilakala26483452005-04-28 19:44:46 -07003297 /* Detect a transmit hang in hardware, this serializes the
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 * check with the clearing of time_stamp and movement of i */
3299 adapter->detect_tx_hung = FALSE;
Malli Chilakala70b8f1e2005-04-28 19:40:40 -07003300 if (tx_ring->buffer_info[i].dma &&
3301 time_after(jiffies, tx_ring->buffer_info[i].time_stamp + HZ)
3302 && !(E1000_READ_REG(&adapter->hw, STATUS) &
3303 E1000_STATUS_TXOFF)) {
3304
3305 /* detected Tx unit hang */
3306 i = tx_ring->next_to_clean;
3307 eop = tx_ring->buffer_info[i].next_to_watch;
3308 eop_desc = E1000_TX_DESC(*tx_ring, eop);
Malli Chilakalac6963ef2005-06-17 17:42:07 -07003309 DPRINTK(DRV, ERR, "Detected Tx Unit Hang\n"
Malli Chilakala70b8f1e2005-04-28 19:40:40 -07003310 " TDH <%x>\n"
3311 " TDT <%x>\n"
3312 " next_to_use <%x>\n"
3313 " next_to_clean <%x>\n"
3314 "buffer_info[next_to_clean]\n"
Andrew Mortonb4ee21f2005-07-27 01:14:44 -07003315 " dma <%llx>\n"
Malli Chilakala70b8f1e2005-04-28 19:40:40 -07003316 " time_stamp <%lx>\n"
3317 " next_to_watch <%x>\n"
3318 " jiffies <%lx>\n"
3319 " next_to_watch.status <%x>\n",
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003320 readl(adapter->hw.hw_addr + tx_ring->tdh),
3321 readl(adapter->hw.hw_addr + tx_ring->tdt),
Malli Chilakala70b8f1e2005-04-28 19:40:40 -07003322 tx_ring->next_to_use,
3323 i,
Andrew Mortonb4ee21f2005-07-27 01:14:44 -07003324 (unsigned long long)tx_ring->buffer_info[i].dma,
Malli Chilakala70b8f1e2005-04-28 19:40:40 -07003325 tx_ring->buffer_info[i].time_stamp,
3326 eop,
3327 jiffies,
3328 eop_desc->upper.fields.status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 netif_stop_queue(netdev);
Malli Chilakala70b8f1e2005-04-28 19:40:40 -07003330 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331 }
Malli Chilakala27012342005-04-28 19:40:28 -07003332#ifdef NETIF_F_TSO
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003333 if (unlikely(!(eop_desc->upper.data & cpu_to_le32(E1000_TXD_STAT_DD)) &&
3334 time_after(jiffies, tx_ring->previous_buffer_info.time_stamp + HZ)))
Malli Chilakala27012342005-04-28 19:40:28 -07003335 e1000_unmap_and_free_tx_resource(
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003336 adapter, &tx_ring->previous_buffer_info);
Malli Chilakala27012342005-04-28 19:40:28 -07003337#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 return cleaned;
3339}
3340
3341/**
3342 * e1000_rx_checksum - Receive Checksum Offload for 82543
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003343 * @adapter: board private structure
3344 * @status_err: receive descriptor status and error fields
3345 * @csum: receive descriptor csum field
3346 * @sk_buff: socket buffer with received data
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347 **/
3348
3349static inline void
3350e1000_rx_checksum(struct e1000_adapter *adapter,
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003351 uint32_t status_err, uint32_t csum,
3352 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353{
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003354 uint16_t status = (uint16_t)status_err;
3355 uint8_t errors = (uint8_t)(status_err >> 24);
3356 skb->ip_summed = CHECKSUM_NONE;
3357
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 /* 82543 or newer only */
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003359 if(unlikely(adapter->hw.mac_type < e1000_82543)) return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 /* Ignore Checksum bit is set */
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003361 if(unlikely(status & E1000_RXD_STAT_IXSM)) return;
3362 /* TCP/UDP checksum error bit is set */
3363 if(unlikely(errors & E1000_RXD_ERR_TCPE)) {
3364 /* let the stack verify checksum errors */
3365 adapter->hw_csum_err++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366 return;
3367 }
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003368 /* TCP/UDP Checksum has not been calculated */
3369 if(adapter->hw.mac_type <= e1000_82547_rev_2) {
3370 if(!(status & E1000_RXD_STAT_TCPCS))
3371 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 } else {
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003373 if(!(status & (E1000_RXD_STAT_TCPCS | E1000_RXD_STAT_UDPCS)))
3374 return;
3375 }
3376 /* It must be a TCP or UDP packet with a valid checksum */
3377 if (likely(status & E1000_RXD_STAT_TCPCS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378 /* TCP checksum is good */
3379 skb->ip_summed = CHECKSUM_UNNECESSARY;
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003380 } else if (adapter->hw.mac_type > e1000_82547_rev_2) {
3381 /* IP fragment with UDP payload */
3382 /* Hardware complements the payload checksum, so we undo it
3383 * and then put the value in host order for further stack use.
3384 */
3385 csum = ntohl(csum ^ 0xFFFF);
3386 skb->csum = csum;
3387 skb->ip_summed = CHECKSUM_HW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388 }
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003389 adapter->hw_csum_good++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390}
3391
3392/**
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003393 * e1000_clean_rx_irq - Send received data up the network stack; legacy
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 * @adapter: board private structure
3395 **/
3396
3397static boolean_t
3398#ifdef CONFIG_E1000_NAPI
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003399e1000_clean_rx_irq(struct e1000_adapter *adapter,
3400 struct e1000_rx_ring *rx_ring,
3401 int *work_done, int work_to_do)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402#else
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003403e1000_clean_rx_irq(struct e1000_adapter *adapter,
3404 struct e1000_rx_ring *rx_ring)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405#endif
3406{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 struct net_device *netdev = adapter->netdev;
3408 struct pci_dev *pdev = adapter->pdev;
3409 struct e1000_rx_desc *rx_desc;
3410 struct e1000_buffer *buffer_info;
3411 struct sk_buff *skb;
3412 unsigned long flags;
3413 uint32_t length;
3414 uint8_t last_byte;
3415 unsigned int i;
3416 boolean_t cleaned = FALSE;
3417
3418 i = rx_ring->next_to_clean;
3419 rx_desc = E1000_RX_DESC(*rx_ring, i);
3420
3421 while(rx_desc->status & E1000_RXD_STAT_DD) {
3422 buffer_info = &rx_ring->buffer_info[i];
3423#ifdef CONFIG_E1000_NAPI
3424 if(*work_done >= work_to_do)
3425 break;
3426 (*work_done)++;
3427#endif
3428 cleaned = TRUE;
3429
3430 pci_unmap_single(pdev,
3431 buffer_info->dma,
3432 buffer_info->length,
3433 PCI_DMA_FROMDEVICE);
3434
3435 skb = buffer_info->skb;
3436 length = le16_to_cpu(rx_desc->length);
3437
3438 if(unlikely(!(rx_desc->status & E1000_RXD_STAT_EOP))) {
3439 /* All receives must fit into a single buffer */
3440 E1000_DBG("%s: Receive packet consumed multiple"
Malli Chilakala26483452005-04-28 19:44:46 -07003441 " buffers\n", netdev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442 dev_kfree_skb_irq(skb);
3443 goto next_desc;
3444 }
3445
3446 if(unlikely(rx_desc->errors & E1000_RXD_ERR_FRAME_ERR_MASK)) {
3447 last_byte = *(skb->data + length - 1);
3448 if(TBI_ACCEPT(&adapter->hw, rx_desc->status,
3449 rx_desc->errors, length, last_byte)) {
3450 spin_lock_irqsave(&adapter->stats_lock, flags);
3451 e1000_tbi_adjust_stats(&adapter->hw,
3452 &adapter->stats,
3453 length, skb->data);
3454 spin_unlock_irqrestore(&adapter->stats_lock,
3455 flags);
3456 length--;
3457 } else {
3458 dev_kfree_skb_irq(skb);
3459 goto next_desc;
3460 }
3461 }
3462
3463 /* Good Receive */
3464 skb_put(skb, length - ETHERNET_FCS_SIZE);
3465
3466 /* Receive Checksum Offload */
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003467 e1000_rx_checksum(adapter,
3468 (uint32_t)(rx_desc->status) |
3469 ((uint32_t)(rx_desc->errors) << 24),
3470 rx_desc->csum, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471 skb->protocol = eth_type_trans(skb, netdev);
3472#ifdef CONFIG_E1000_NAPI
3473 if(unlikely(adapter->vlgrp &&
3474 (rx_desc->status & E1000_RXD_STAT_VP))) {
3475 vlan_hwaccel_receive_skb(skb, adapter->vlgrp,
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003476 le16_to_cpu(rx_desc->special) &
3477 E1000_RXD_SPC_VLAN_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478 } else {
3479 netif_receive_skb(skb);
3480 }
3481#else /* CONFIG_E1000_NAPI */
3482 if(unlikely(adapter->vlgrp &&
3483 (rx_desc->status & E1000_RXD_STAT_VP))) {
3484 vlan_hwaccel_rx(skb, adapter->vlgrp,
3485 le16_to_cpu(rx_desc->special) &
3486 E1000_RXD_SPC_VLAN_MASK);
3487 } else {
3488 netif_rx(skb);
3489 }
3490#endif /* CONFIG_E1000_NAPI */
3491 netdev->last_rx = jiffies;
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003492 rx_ring->pkt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493
3494next_desc:
3495 rx_desc->status = 0;
3496 buffer_info->skb = NULL;
3497 if(unlikely(++i == rx_ring->count)) i = 0;
3498
3499 rx_desc = E1000_RX_DESC(*rx_ring, i);
3500 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501 rx_ring->next_to_clean = i;
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003502 adapter->alloc_rx_buf(adapter, rx_ring);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503
3504 return cleaned;
3505}
3506
3507/**
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003508 * e1000_clean_rx_irq_ps - Send received data up the network stack; packet split
3509 * @adapter: board private structure
3510 **/
3511
3512static boolean_t
3513#ifdef CONFIG_E1000_NAPI
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003514e1000_clean_rx_irq_ps(struct e1000_adapter *adapter,
3515 struct e1000_rx_ring *rx_ring,
3516 int *work_done, int work_to_do)
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003517#else
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003518e1000_clean_rx_irq_ps(struct e1000_adapter *adapter,
3519 struct e1000_rx_ring *rx_ring)
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003520#endif
3521{
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003522 union e1000_rx_desc_packet_split *rx_desc;
3523 struct net_device *netdev = adapter->netdev;
3524 struct pci_dev *pdev = adapter->pdev;
3525 struct e1000_buffer *buffer_info;
3526 struct e1000_ps_page *ps_page;
3527 struct e1000_ps_page_dma *ps_page_dma;
3528 struct sk_buff *skb;
3529 unsigned int i, j;
3530 uint32_t length, staterr;
3531 boolean_t cleaned = FALSE;
3532
3533 i = rx_ring->next_to_clean;
3534 rx_desc = E1000_RX_DESC_PS(*rx_ring, i);
Malli Chilakala683a38f2005-06-17 17:43:25 -07003535 staterr = le32_to_cpu(rx_desc->wb.middle.status_error);
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003536
3537 while(staterr & E1000_RXD_STAT_DD) {
3538 buffer_info = &rx_ring->buffer_info[i];
3539 ps_page = &rx_ring->ps_page[i];
3540 ps_page_dma = &rx_ring->ps_page_dma[i];
3541#ifdef CONFIG_E1000_NAPI
3542 if(unlikely(*work_done >= work_to_do))
3543 break;
3544 (*work_done)++;
3545#endif
3546 cleaned = TRUE;
3547 pci_unmap_single(pdev, buffer_info->dma,
3548 buffer_info->length,
3549 PCI_DMA_FROMDEVICE);
3550
3551 skb = buffer_info->skb;
3552
3553 if(unlikely(!(staterr & E1000_RXD_STAT_EOP))) {
3554 E1000_DBG("%s: Packet Split buffers didn't pick up"
3555 " the full packet\n", netdev->name);
3556 dev_kfree_skb_irq(skb);
3557 goto next_desc;
3558 }
3559
3560 if(unlikely(staterr & E1000_RXDEXT_ERR_FRAME_ERR_MASK)) {
3561 dev_kfree_skb_irq(skb);
3562 goto next_desc;
3563 }
3564
3565 length = le16_to_cpu(rx_desc->wb.middle.length0);
3566
3567 if(unlikely(!length)) {
3568 E1000_DBG("%s: Last part of the packet spanning"
3569 " multiple descriptors\n", netdev->name);
3570 dev_kfree_skb_irq(skb);
3571 goto next_desc;
3572 }
3573
3574 /* Good Receive */
3575 skb_put(skb, length);
3576
Mallikarjuna R Chilakalae4c811c2005-10-04 07:05:44 -04003577 for(j = 0; j < adapter->rx_ps_pages; j++) {
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003578 if(!(length = le16_to_cpu(rx_desc->wb.upper.length[j])))
3579 break;
3580
3581 pci_unmap_page(pdev, ps_page_dma->ps_page_dma[j],
3582 PAGE_SIZE, PCI_DMA_FROMDEVICE);
3583 ps_page_dma->ps_page_dma[j] = 0;
3584 skb_shinfo(skb)->frags[j].page =
3585 ps_page->ps_page[j];
3586 ps_page->ps_page[j] = NULL;
3587 skb_shinfo(skb)->frags[j].page_offset = 0;
3588 skb_shinfo(skb)->frags[j].size = length;
3589 skb_shinfo(skb)->nr_frags++;
3590 skb->len += length;
3591 skb->data_len += length;
3592 }
3593
3594 e1000_rx_checksum(adapter, staterr,
3595 rx_desc->wb.lower.hi_dword.csum_ip.csum, skb);
3596 skb->protocol = eth_type_trans(skb, netdev);
3597
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003598 if(likely(rx_desc->wb.upper.header_status &
Mallikarjuna R Chilakalae4c811c2005-10-04 07:05:44 -04003599 E1000_RXDPS_HDRSTAT_HDRSP)) {
3600 adapter->rx_hdr_split++;
3601#ifdef HAVE_RX_ZERO_COPY
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003602 skb_shinfo(skb)->zero_copy = TRUE;
3603#endif
Mallikarjuna R Chilakalae4c811c2005-10-04 07:05:44 -04003604 }
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003605#ifdef CONFIG_E1000_NAPI
3606 if(unlikely(adapter->vlgrp && (staterr & E1000_RXD_STAT_VP))) {
3607 vlan_hwaccel_receive_skb(skb, adapter->vlgrp,
Malli Chilakala683a38f2005-06-17 17:43:25 -07003608 le16_to_cpu(rx_desc->wb.middle.vlan) &
3609 E1000_RXD_SPC_VLAN_MASK);
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003610 } else {
3611 netif_receive_skb(skb);
3612 }
3613#else /* CONFIG_E1000_NAPI */
3614 if(unlikely(adapter->vlgrp && (staterr & E1000_RXD_STAT_VP))) {
3615 vlan_hwaccel_rx(skb, adapter->vlgrp,
Malli Chilakala683a38f2005-06-17 17:43:25 -07003616 le16_to_cpu(rx_desc->wb.middle.vlan) &
3617 E1000_RXD_SPC_VLAN_MASK);
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003618 } else {
3619 netif_rx(skb);
3620 }
3621#endif /* CONFIG_E1000_NAPI */
3622 netdev->last_rx = jiffies;
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003623 rx_ring->pkt++;
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003624
3625next_desc:
3626 rx_desc->wb.middle.status_error &= ~0xFF;
3627 buffer_info->skb = NULL;
3628 if(unlikely(++i == rx_ring->count)) i = 0;
3629
3630 rx_desc = E1000_RX_DESC_PS(*rx_ring, i);
Malli Chilakala683a38f2005-06-17 17:43:25 -07003631 staterr = le32_to_cpu(rx_desc->wb.middle.status_error);
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003632 }
3633 rx_ring->next_to_clean = i;
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003634 adapter->alloc_rx_buf(adapter, rx_ring);
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003635
3636 return cleaned;
3637}
3638
3639/**
3640 * e1000_alloc_rx_buffers - Replace used receive buffers; legacy & extended
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641 * @adapter: address of board private structure
3642 **/
3643
3644static void
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003645e1000_alloc_rx_buffers(struct e1000_adapter *adapter,
3646 struct e1000_rx_ring *rx_ring)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648 struct net_device *netdev = adapter->netdev;
3649 struct pci_dev *pdev = adapter->pdev;
3650 struct e1000_rx_desc *rx_desc;
3651 struct e1000_buffer *buffer_info;
3652 struct sk_buff *skb;
Malli Chilakala26483452005-04-28 19:44:46 -07003653 unsigned int i;
3654 unsigned int bufsz = adapter->rx_buffer_len + NET_IP_ALIGN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655
3656 i = rx_ring->next_to_use;
3657 buffer_info = &rx_ring->buffer_info[i];
3658
3659 while(!buffer_info->skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660 skb = dev_alloc_skb(bufsz);
Malli Chilakala26483452005-04-28 19:44:46 -07003661
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662 if(unlikely(!skb)) {
3663 /* Better luck next round */
3664 break;
3665 }
3666
Malli Chilakala26483452005-04-28 19:44:46 -07003667 /* Fix for errata 23, can't cross 64kB boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668 if (!e1000_check_64k_bound(adapter, skb->data, bufsz)) {
3669 struct sk_buff *oldskb = skb;
Malli Chilakala26483452005-04-28 19:44:46 -07003670 DPRINTK(RX_ERR, ERR, "skb align check failed: %u bytes "
3671 "at %p\n", bufsz, skb->data);
3672 /* Try again, without freeing the previous */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 skb = dev_alloc_skb(bufsz);
Malli Chilakala26483452005-04-28 19:44:46 -07003674 /* Failed allocation, critical failure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 if (!skb) {
3676 dev_kfree_skb(oldskb);
3677 break;
3678 }
Malli Chilakala26483452005-04-28 19:44:46 -07003679
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680 if (!e1000_check_64k_bound(adapter, skb->data, bufsz)) {
3681 /* give up */
3682 dev_kfree_skb(skb);
3683 dev_kfree_skb(oldskb);
3684 break; /* while !buffer_info->skb */
3685 } else {
Malli Chilakala26483452005-04-28 19:44:46 -07003686 /* Use new allocation */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687 dev_kfree_skb(oldskb);
3688 }
3689 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690 /* Make buffer alignment 2 beyond a 16 byte boundary
3691 * this will result in a 16 byte aligned IP header after
3692 * the 14 byte MAC header is removed
3693 */
3694 skb_reserve(skb, NET_IP_ALIGN);
3695
3696 skb->dev = netdev;
3697
3698 buffer_info->skb = skb;
3699 buffer_info->length = adapter->rx_buffer_len;
3700 buffer_info->dma = pci_map_single(pdev,
3701 skb->data,
3702 adapter->rx_buffer_len,
3703 PCI_DMA_FROMDEVICE);
3704
Malli Chilakala26483452005-04-28 19:44:46 -07003705 /* Fix for errata 23, can't cross 64kB boundary */
3706 if (!e1000_check_64k_bound(adapter,
3707 (void *)(unsigned long)buffer_info->dma,
3708 adapter->rx_buffer_len)) {
3709 DPRINTK(RX_ERR, ERR,
3710 "dma align check failed: %u bytes at %p\n",
3711 adapter->rx_buffer_len,
3712 (void *)(unsigned long)buffer_info->dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713 dev_kfree_skb(skb);
3714 buffer_info->skb = NULL;
3715
Malli Chilakala26483452005-04-28 19:44:46 -07003716 pci_unmap_single(pdev, buffer_info->dma,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717 adapter->rx_buffer_len,
3718 PCI_DMA_FROMDEVICE);
3719
3720 break; /* while !buffer_info->skb */
3721 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722 rx_desc = E1000_RX_DESC(*rx_ring, i);
3723 rx_desc->buffer_addr = cpu_to_le64(buffer_info->dma);
3724
3725 if(unlikely((i & ~(E1000_RX_BUFFER_WRITE - 1)) == i)) {
3726 /* Force memory writes to complete before letting h/w
3727 * know there are new descriptors to fetch. (Only
3728 * applicable for weak-ordered memory model archs,
3729 * such as IA-64). */
3730 wmb();
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003731 writel(i, adapter->hw.hw_addr + rx_ring->rdt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732 }
3733
3734 if(unlikely(++i == rx_ring->count)) i = 0;
3735 buffer_info = &rx_ring->buffer_info[i];
3736 }
3737
3738 rx_ring->next_to_use = i;
3739}
3740
3741/**
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003742 * e1000_alloc_rx_buffers_ps - Replace used receive buffers; packet split
3743 * @adapter: address of board private structure
3744 **/
3745
3746static void
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003747e1000_alloc_rx_buffers_ps(struct e1000_adapter *adapter,
3748 struct e1000_rx_ring *rx_ring)
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003749{
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003750 struct net_device *netdev = adapter->netdev;
3751 struct pci_dev *pdev = adapter->pdev;
3752 union e1000_rx_desc_packet_split *rx_desc;
3753 struct e1000_buffer *buffer_info;
3754 struct e1000_ps_page *ps_page;
3755 struct e1000_ps_page_dma *ps_page_dma;
3756 struct sk_buff *skb;
3757 unsigned int i, j;
3758
3759 i = rx_ring->next_to_use;
3760 buffer_info = &rx_ring->buffer_info[i];
3761 ps_page = &rx_ring->ps_page[i];
3762 ps_page_dma = &rx_ring->ps_page_dma[i];
3763
3764 while(!buffer_info->skb) {
3765 rx_desc = E1000_RX_DESC_PS(*rx_ring, i);
3766
3767 for(j = 0; j < PS_PAGE_BUFFERS; j++) {
Mallikarjuna R Chilakalae4c811c2005-10-04 07:05:44 -04003768 if (j < adapter->rx_ps_pages) {
3769 if (likely(!ps_page->ps_page[j])) {
3770 ps_page->ps_page[j] =
3771 alloc_page(GFP_ATOMIC);
3772 if (unlikely(!ps_page->ps_page[j]))
3773 goto no_buffers;
3774 ps_page_dma->ps_page_dma[j] =
3775 pci_map_page(pdev,
3776 ps_page->ps_page[j],
3777 0, PAGE_SIZE,
3778 PCI_DMA_FROMDEVICE);
3779 }
3780 /* Refresh the desc even if buffer_addrs didn't
3781 * change because each write-back erases
3782 * this info.
3783 */
3784 rx_desc->read.buffer_addr[j+1] =
3785 cpu_to_le64(ps_page_dma->ps_page_dma[j]);
3786 } else
3787 rx_desc->read.buffer_addr[j+1] = ~0;
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003788 }
3789
3790 skb = dev_alloc_skb(adapter->rx_ps_bsize0 + NET_IP_ALIGN);
3791
3792 if(unlikely(!skb))
3793 break;
3794
3795 /* Make buffer alignment 2 beyond a 16 byte boundary
3796 * this will result in a 16 byte aligned IP header after
3797 * the 14 byte MAC header is removed
3798 */
3799 skb_reserve(skb, NET_IP_ALIGN);
3800
3801 skb->dev = netdev;
3802
3803 buffer_info->skb = skb;
3804 buffer_info->length = adapter->rx_ps_bsize0;
3805 buffer_info->dma = pci_map_single(pdev, skb->data,
3806 adapter->rx_ps_bsize0,
3807 PCI_DMA_FROMDEVICE);
3808
3809 rx_desc->read.buffer_addr[0] = cpu_to_le64(buffer_info->dma);
3810
3811 if(unlikely((i & ~(E1000_RX_BUFFER_WRITE - 1)) == i)) {
3812 /* Force memory writes to complete before letting h/w
3813 * know there are new descriptors to fetch. (Only
3814 * applicable for weak-ordered memory model archs,
3815 * such as IA-64). */
3816 wmb();
3817 /* Hardware increments by 16 bytes, but packet split
3818 * descriptors are 32 bytes...so we increment tail
3819 * twice as much.
3820 */
Mallikarjuna R Chilakala581d7082005-10-04 07:01:55 -04003821 writel(i<<1, adapter->hw.hw_addr + rx_ring->rdt);
Malli Chilakala2d7edb92005-04-28 19:43:52 -07003822 }
3823
3824 if(unlikely(++i == rx_ring->count)) i = 0;
3825 buffer_info = &rx_ring->buffer_info[i];
3826 ps_page = &rx_ring->ps_page[i];
3827 ps_page_dma = &rx_ring->ps_page_dma[i];
3828 }
3829
3830no_buffers:
3831 rx_ring->next_to_use = i;
3832}
3833
3834/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835 * e1000_smartspeed - Workaround for SmartSpeed on 82541 and 82547 controllers.
3836 * @adapter:
3837 **/
3838
3839static void
3840e1000_smartspeed(struct e1000_adapter *adapter)
3841{
3842 uint16_t phy_status;
3843 uint16_t phy_ctrl;
3844
3845 if((adapter->hw.phy_type != e1000_phy_igp) || !adapter->hw.autoneg ||
3846 !(adapter->hw.autoneg_advertised & ADVERTISE_1000_FULL))
3847 return;
3848
3849 if(adapter->smartspeed == 0) {
3850 /* If Master/Slave config fault is asserted twice,
3851 * we assume back-to-back */
3852 e1000_read_phy_reg(&adapter->hw, PHY_1000T_STATUS, &phy_status);
3853 if(!(phy_status & SR_1000T_MS_CONFIG_FAULT)) return;
3854 e1000_read_phy_reg(&adapter->hw, PHY_1000T_STATUS, &phy_status);
3855 if(!(phy_status & SR_1000T_MS_CONFIG_FAULT)) return;
3856 e1000_read_phy_reg(&adapter->hw, PHY_1000T_CTRL, &phy_ctrl);
3857 if(phy_ctrl & CR_1000T_MS_ENABLE) {
3858 phy_ctrl &= ~CR_1000T_MS_ENABLE;
3859 e1000_write_phy_reg(&adapter->hw, PHY_1000T_CTRL,
3860 phy_ctrl);
3861 adapter->smartspeed++;
3862 if(!e1000_phy_setup_autoneg(&adapter->hw) &&
3863 !e1000_read_phy_reg(&adapter->hw, PHY_CTRL,
3864 &phy_ctrl)) {
3865 phy_ctrl |= (MII_CR_AUTO_NEG_EN |
3866 MII_CR_RESTART_AUTO_NEG);
3867 e1000_write_phy_reg(&adapter->hw, PHY_CTRL,
3868 phy_ctrl);
3869 }
3870 }
3871 return;
3872 } else if(adapter->smartspeed == E1000_SMARTSPEED_DOWNSHIFT) {
3873 /* If still no link, perhaps using 2/3 pair cable */
3874 e1000_read_phy_reg(&adapter->hw, PHY_1000T_CTRL, &phy_ctrl);
3875 phy_ctrl |= CR_1000T_MS_ENABLE;
3876 e1000_write_phy_reg(&adapter->hw, PHY_1000T_CTRL, phy_ctrl);
3877 if(!e1000_phy_setup_autoneg(&adapter->hw) &&
3878 !e1000_read_phy_reg(&adapter->hw, PHY_CTRL, &phy_ctrl)) {
3879 phy_ctrl |= (MII_CR_AUTO_NEG_EN |
3880 MII_CR_RESTART_AUTO_NEG);
3881 e1000_write_phy_reg(&adapter->hw, PHY_CTRL, phy_ctrl);
3882 }
3883 }
3884 /* Restart process after E1000_SMARTSPEED_MAX iterations */
3885 if(adapter->smartspeed++ == E1000_SMARTSPEED_MAX)
3886 adapter->smartspeed = 0;
3887}
3888
3889/**
3890 * e1000_ioctl -
3891 * @netdev:
3892 * @ifreq:
3893 * @cmd:
3894 **/
3895
3896static int
3897e1000_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
3898{
3899 switch (cmd) {
3900 case SIOCGMIIPHY:
3901 case SIOCGMIIREG:
3902 case SIOCSMIIREG:
3903 return e1000_mii_ioctl(netdev, ifr, cmd);
3904 default:
3905 return -EOPNOTSUPP;
3906 }
3907}
3908
3909/**
3910 * e1000_mii_ioctl -
3911 * @netdev:
3912 * @ifreq:
3913 * @cmd:
3914 **/
3915
3916static int
3917e1000_mii_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
3918{
Malli Chilakala60490fe2005-06-17 17:41:45 -07003919 struct e1000_adapter *adapter = netdev_priv(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920 struct mii_ioctl_data *data = if_mii(ifr);
3921 int retval;
3922 uint16_t mii_reg;
3923 uint16_t spddplx;
Malli Chilakala97876fc2005-06-17 17:40:19 -07003924 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925
3926 if(adapter->hw.media_type != e1000_media_type_copper)
3927 return -EOPNOTSUPP;
3928
3929 switch (cmd) {
3930 case SIOCGMIIPHY:
3931 data->phy_id = adapter->hw.phy_addr;
3932 break;
3933 case SIOCGMIIREG:
Malli Chilakala97876fc2005-06-17 17:40:19 -07003934 if(!capable(CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935 return -EPERM;
Malli Chilakala97876fc2005-06-17 17:40:19 -07003936 spin_lock_irqsave(&adapter->stats_lock, flags);
3937 if(e1000_read_phy_reg(&adapter->hw, data->reg_num & 0x1F,
3938 &data->val_out)) {
3939 spin_unlock_irqrestore(&adapter->stats_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940 return -EIO;
Malli Chilakala97876fc2005-06-17 17:40:19 -07003941 }
3942 spin_unlock_irqrestore(&adapter->stats_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943 break;
3944 case SIOCSMIIREG:
Malli Chilakala97876fc2005-06-17 17:40:19 -07003945 if(!capable(CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946 return -EPERM;
Malli Chilakala97876fc2005-06-17 17:40:19 -07003947 if(data->reg_num & ~(0x1F))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948 return -EFAULT;
3949 mii_reg = data->val_in;
Malli Chilakala97876fc2005-06-17 17:40:19 -07003950 spin_lock_irqsave(&adapter->stats_lock, flags);
3951 if(e1000_write_phy_reg(&adapter->hw, data->reg_num,
3952 mii_reg)) {
3953 spin_unlock_irqrestore(&adapter->stats_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954 return -EIO;
Malli Chilakala97876fc2005-06-17 17:40:19 -07003955 }
3956 if(adapter->hw.phy_type == e1000_phy_m88) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957 switch (data->reg_num) {
3958 case PHY_CTRL:
3959 if(mii_reg & MII_CR_POWER_DOWN)
3960 break;
3961 if(mii_reg & MII_CR_AUTO_NEG_EN) {
3962 adapter->hw.autoneg = 1;
3963 adapter->hw.autoneg_advertised = 0x2F;
3964 } else {
3965 if (mii_reg & 0x40)
3966 spddplx = SPEED_1000;
3967 else if (mii_reg & 0x2000)
3968 spddplx = SPEED_100;
3969 else
3970 spddplx = SPEED_10;
3971 spddplx += (mii_reg & 0x100)
3972 ? FULL_DUPLEX :
3973 HALF_DUPLEX;
3974 retval = e1000_set_spd_dplx(adapter,
3975 spddplx);
Malli Chilakala97876fc2005-06-17 17:40:19 -07003976 if(retval) {
3977 spin_unlock_irqrestore(
3978 &adapter->stats_lock,
3979 flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980 return retval;
Malli Chilakala97876fc2005-06-17 17:40:19 -07003981 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982 }
3983 if(netif_running(adapter->netdev)) {
3984 e1000_down(adapter);
3985 e1000_up(adapter);
3986 } else
3987 e1000_reset(adapter);
3988 break;
3989 case M88E1000_PHY_SPEC_CTRL:
3990 case M88E1000_EXT_PHY_SPEC_CTRL:
Malli Chilakala97876fc2005-06-17 17:40:19 -07003991 if(e1000_phy_reset(&adapter->hw)) {
3992 spin_unlock_irqrestore(
3993 &adapter->stats_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994 return -EIO;
Malli Chilakala97876fc2005-06-17 17:40:19 -07003995 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996 break;
3997 }
3998 } else {
3999 switch (data->reg_num) {
4000 case PHY_CTRL:
4001 if(mii_reg & MII_CR_POWER_DOWN)
4002 break;
4003 if(netif_running(adapter->netdev)) {
4004 e1000_down(adapter);
4005 e1000_up(adapter);
4006 } else
4007 e1000_reset(adapter);
4008 break;
4009 }
4010 }
Malli Chilakala97876fc2005-06-17 17:40:19 -07004011 spin_unlock_irqrestore(&adapter->stats_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012 break;
4013 default:
4014 return -EOPNOTSUPP;
4015 }
4016 return E1000_SUCCESS;
4017}
4018
4019void
4020e1000_pci_set_mwi(struct e1000_hw *hw)
4021{
4022 struct e1000_adapter *adapter = hw->back;
Malli Chilakala26483452005-04-28 19:44:46 -07004023 int ret_val = pci_set_mwi(adapter->pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024
Malli Chilakala26483452005-04-28 19:44:46 -07004025 if(ret_val)
4026 DPRINTK(PROBE, ERR, "Error in setting MWI\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027}
4028
4029void
4030e1000_pci_clear_mwi(struct e1000_hw *hw)
4031{
4032 struct e1000_adapter *adapter = hw->back;
4033
4034 pci_clear_mwi(adapter->pdev);
4035}
4036
4037void
4038e1000_read_pci_cfg(struct e1000_hw *hw, uint32_t reg, uint16_t *value)
4039{
4040 struct e1000_adapter *adapter = hw->back;
4041
4042 pci_read_config_word(adapter->pdev, reg, value);
4043}
4044
4045void
4046e1000_write_pci_cfg(struct e1000_hw *hw, uint32_t reg, uint16_t *value)
4047{
4048 struct e1000_adapter *adapter = hw->back;
4049
4050 pci_write_config_word(adapter->pdev, reg, *value);
4051}
4052
4053uint32_t
4054e1000_io_read(struct e1000_hw *hw, unsigned long port)
4055{
4056 return inl(port);
4057}
4058
4059void
4060e1000_io_write(struct e1000_hw *hw, unsigned long port, uint32_t value)
4061{
4062 outl(value, port);
4063}
4064
4065static void
4066e1000_vlan_rx_register(struct net_device *netdev, struct vlan_group *grp)
4067{
Malli Chilakala60490fe2005-06-17 17:41:45 -07004068 struct e1000_adapter *adapter = netdev_priv(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069 uint32_t ctrl, rctl;
4070
4071 e1000_irq_disable(adapter);
4072 adapter->vlgrp = grp;
4073
4074 if(grp) {
4075 /* enable VLAN tag insert/strip */
4076 ctrl = E1000_READ_REG(&adapter->hw, CTRL);
4077 ctrl |= E1000_CTRL_VME;
4078 E1000_WRITE_REG(&adapter->hw, CTRL, ctrl);
4079
4080 /* enable VLAN receive filtering */
4081 rctl = E1000_READ_REG(&adapter->hw, RCTL);
4082 rctl |= E1000_RCTL_VFE;
4083 rctl &= ~E1000_RCTL_CFIEN;
4084 E1000_WRITE_REG(&adapter->hw, RCTL, rctl);
Malli Chilakala2d7edb92005-04-28 19:43:52 -07004085 e1000_update_mng_vlan(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086 } else {
4087 /* disable VLAN tag insert/strip */
4088 ctrl = E1000_READ_REG(&adapter->hw, CTRL);
4089 ctrl &= ~E1000_CTRL_VME;
4090 E1000_WRITE_REG(&adapter->hw, CTRL, ctrl);
4091
4092 /* disable VLAN filtering */
4093 rctl = E1000_READ_REG(&adapter->hw, RCTL);
4094 rctl &= ~E1000_RCTL_VFE;
4095 E1000_WRITE_REG(&adapter->hw, RCTL, rctl);
Malli Chilakala2d7edb92005-04-28 19:43:52 -07004096 if(adapter->mng_vlan_id != (uint16_t)E1000_MNG_VLAN_NONE) {
4097 e1000_vlan_rx_kill_vid(netdev, adapter->mng_vlan_id);
4098 adapter->mng_vlan_id = E1000_MNG_VLAN_NONE;
4099 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100 }
4101
4102 e1000_irq_enable(adapter);
4103}
4104
4105static void
4106e1000_vlan_rx_add_vid(struct net_device *netdev, uint16_t vid)
4107{
Malli Chilakala60490fe2005-06-17 17:41:45 -07004108 struct e1000_adapter *adapter = netdev_priv(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109 uint32_t vfta, index;
Malli Chilakala2d7edb92005-04-28 19:43:52 -07004110 if((adapter->hw.mng_cookie.status &
4111 E1000_MNG_DHCP_COOKIE_STATUS_VLAN_SUPPORT) &&
4112 (vid == adapter->mng_vlan_id))
4113 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114 /* add VID to filter table */
4115 index = (vid >> 5) & 0x7F;
4116 vfta = E1000_READ_REG_ARRAY(&adapter->hw, VFTA, index);
4117 vfta |= (1 << (vid & 0x1F));
4118 e1000_write_vfta(&adapter->hw, index, vfta);
4119}
4120
4121static void
4122e1000_vlan_rx_kill_vid(struct net_device *netdev, uint16_t vid)
4123{
Malli Chilakala60490fe2005-06-17 17:41:45 -07004124 struct e1000_adapter *adapter = netdev_priv(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125 uint32_t vfta, index;
4126
4127 e1000_irq_disable(adapter);
4128
4129 if(adapter->vlgrp)
4130 adapter->vlgrp->vlan_devices[vid] = NULL;
4131
4132 e1000_irq_enable(adapter);
4133
Malli Chilakala2d7edb92005-04-28 19:43:52 -07004134 if((adapter->hw.mng_cookie.status &
4135 E1000_MNG_DHCP_COOKIE_STATUS_VLAN_SUPPORT) &&
4136 (vid == adapter->mng_vlan_id))
4137 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138 /* remove VID from filter table */
4139 index = (vid >> 5) & 0x7F;
4140 vfta = E1000_READ_REG_ARRAY(&adapter->hw, VFTA, index);
4141 vfta &= ~(1 << (vid & 0x1F));
4142 e1000_write_vfta(&adapter->hw, index, vfta);
4143}
4144
4145static void
4146e1000_restore_vlan(struct e1000_adapter *adapter)
4147{
4148 e1000_vlan_rx_register(adapter->netdev, adapter->vlgrp);
4149
4150 if(adapter->vlgrp) {
4151 uint16_t vid;
4152 for(vid = 0; vid < VLAN_GROUP_ARRAY_LEN; vid++) {
4153 if(!adapter->vlgrp->vlan_devices[vid])
4154 continue;
4155 e1000_vlan_rx_add_vid(adapter->netdev, vid);
4156 }
4157 }
4158}
4159
4160int
4161e1000_set_spd_dplx(struct e1000_adapter *adapter, uint16_t spddplx)
4162{
4163 adapter->hw.autoneg = 0;
4164
Malli Chilakala69213682005-06-17 17:44:20 -07004165 /* Fiber NICs only allow 1000 gbps Full duplex */
4166 if((adapter->hw.media_type == e1000_media_type_fiber) &&
4167 spddplx != (SPEED_1000 + DUPLEX_FULL)) {
4168 DPRINTK(PROBE, ERR, "Unsupported Speed/Duplex configuration\n");
4169 return -EINVAL;
4170 }
4171
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172 switch(spddplx) {
4173 case SPEED_10 + DUPLEX_HALF:
4174 adapter->hw.forced_speed_duplex = e1000_10_half;
4175 break;
4176 case SPEED_10 + DUPLEX_FULL:
4177 adapter->hw.forced_speed_duplex = e1000_10_full;
4178 break;
4179 case SPEED_100 + DUPLEX_HALF:
4180 adapter->hw.forced_speed_duplex = e1000_100_half;
4181 break;
4182 case SPEED_100 + DUPLEX_FULL:
4183 adapter->hw.forced_speed_duplex = e1000_100_full;
4184 break;
4185 case SPEED_1000 + DUPLEX_FULL:
4186 adapter->hw.autoneg = 1;
4187 adapter->hw.autoneg_advertised = ADVERTISE_1000_FULL;
4188 break;
4189 case SPEED_1000 + DUPLEX_HALF: /* not supported */
4190 default:
Malli Chilakala26483452005-04-28 19:44:46 -07004191 DPRINTK(PROBE, ERR, "Unsupported Speed/Duplex configuration\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192 return -EINVAL;
4193 }
4194 return 0;
4195}
4196
Martin J. Blighb6a1d5f2005-10-28 15:14:44 -07004197#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198static int
Pavel Machek829ca9a2005-09-03 15:56:56 -07004199e1000_suspend(struct pci_dev *pdev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200{
4201 struct net_device *netdev = pci_get_drvdata(pdev);
Malli Chilakala60490fe2005-06-17 17:41:45 -07004202 struct e1000_adapter *adapter = netdev_priv(netdev);
Malli Chilakala2d7edb92005-04-28 19:43:52 -07004203 uint32_t ctrl, ctrl_ext, rctl, manc, status, swsm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204 uint32_t wufc = adapter->wol;
4205
4206 netif_device_detach(netdev);
4207
4208 if(netif_running(netdev))
4209 e1000_down(adapter);
4210
4211 status = E1000_READ_REG(&adapter->hw, STATUS);
4212 if(status & E1000_STATUS_LU)
4213 wufc &= ~E1000_WUFC_LNKC;
4214
4215 if(wufc) {
4216 e1000_setup_rctl(adapter);
4217 e1000_set_multi(netdev);
4218
4219 /* turn on all-multi mode if wake on multicast is enabled */
4220 if(adapter->wol & E1000_WUFC_MC) {
4221 rctl = E1000_READ_REG(&adapter->hw, RCTL);
4222 rctl |= E1000_RCTL_MPE;
4223 E1000_WRITE_REG(&adapter->hw, RCTL, rctl);
4224 }
4225
4226 if(adapter->hw.mac_type >= e1000_82540) {
4227 ctrl = E1000_READ_REG(&adapter->hw, CTRL);
4228 /* advertise wake from D3Cold */
4229 #define E1000_CTRL_ADVD3WUC 0x00100000
4230 /* phy power management enable */
4231 #define E1000_CTRL_EN_PHY_PWR_MGMT 0x00200000
4232 ctrl |= E1000_CTRL_ADVD3WUC |
4233 E1000_CTRL_EN_PHY_PWR_MGMT;
4234 E1000_WRITE_REG(&adapter->hw, CTRL, ctrl);
4235 }
4236
4237 if(adapter->hw.media_type == e1000_media_type_fiber ||
4238 adapter->hw.media_type == e1000_media_type_internal_serdes) {
4239 /* keep the laser running in D3 */
4240 ctrl_ext = E1000_READ_REG(&adapter->hw, CTRL_EXT);
4241 ctrl_ext |= E1000_CTRL_EXT_SDP7_DATA;
4242 E1000_WRITE_REG(&adapter->hw, CTRL_EXT, ctrl_ext);
4243 }
4244
Malli Chilakala2d7edb92005-04-28 19:43:52 -07004245 /* Allow time for pending master requests to run */
4246 e1000_disable_pciex_master(&adapter->hw);
4247
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248 E1000_WRITE_REG(&adapter->hw, WUC, E1000_WUC_PME_EN);
4249 E1000_WRITE_REG(&adapter->hw, WUFC, wufc);
4250 pci_enable_wake(pdev, 3, 1);
4251 pci_enable_wake(pdev, 4, 1); /* 4 == D3 cold */
4252 } else {
4253 E1000_WRITE_REG(&adapter->hw, WUC, 0);
4254 E1000_WRITE_REG(&adapter->hw, WUFC, 0);
4255 pci_enable_wake(pdev, 3, 0);
4256 pci_enable_wake(pdev, 4, 0); /* 4 == D3 cold */
4257 }
4258
4259 pci_save_state(pdev);
4260
4261 if(adapter->hw.mac_type >= e1000_82540 &&
4262 adapter->hw.media_type == e1000_media_type_copper) {
4263 manc = E1000_READ_REG(&adapter->hw, MANC);
4264 if(manc & E1000_MANC_SMBUS_EN) {
4265 manc |= E1000_MANC_ARP_EN;
4266 E1000_WRITE_REG(&adapter->hw, MANC, manc);
4267 pci_enable_wake(pdev, 3, 1);
4268 pci_enable_wake(pdev, 4, 1); /* 4 == D3 cold */
4269 }
4270 }
4271
Malli Chilakala2d7edb92005-04-28 19:43:52 -07004272 switch(adapter->hw.mac_type) {
Mallikarjuna R Chilakala868d5302005-10-04 06:58:59 -04004273 case e1000_82571:
4274 case e1000_82572:
4275 ctrl_ext = E1000_READ_REG(&adapter->hw, CTRL_EXT);
4276 E1000_WRITE_REG(&adapter->hw, CTRL_EXT,
4277 ctrl_ext & ~E1000_CTRL_EXT_DRV_LOAD);
4278 break;
Malli Chilakala2d7edb92005-04-28 19:43:52 -07004279 case e1000_82573:
4280 swsm = E1000_READ_REG(&adapter->hw, SWSM);
4281 E1000_WRITE_REG(&adapter->hw, SWSM,
4282 swsm & ~E1000_SWSM_DRV_LOAD);
4283 break;
4284 default:
4285 break;
4286 }
4287
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288 pci_disable_device(pdev);
Pavel Machek829ca9a2005-09-03 15:56:56 -07004289 pci_set_power_state(pdev, pci_choose_state(pdev, state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290
4291 return 0;
4292}
4293
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294static int
4295e1000_resume(struct pci_dev *pdev)
4296{
4297 struct net_device *netdev = pci_get_drvdata(pdev);
Malli Chilakala60490fe2005-06-17 17:41:45 -07004298 struct e1000_adapter *adapter = netdev_priv(netdev);
Malli Chilakala2b028932005-06-17 17:46:06 -07004299 uint32_t manc, ret_val, swsm;
Mallikarjuna R Chilakala868d5302005-10-04 06:58:59 -04004300 uint32_t ctrl_ext;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301
Pavel Machek829ca9a2005-09-03 15:56:56 -07004302 pci_set_power_state(pdev, PCI_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303 pci_restore_state(pdev);
Malli Chilakala2b028932005-06-17 17:46:06 -07004304 ret_val = pci_enable_device(pdev);
Malli Chilakalaa4cb8472005-04-28 19:41:28 -07004305 pci_set_master(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306
Pavel Machek829ca9a2005-09-03 15:56:56 -07004307 pci_enable_wake(pdev, PCI_D3hot, 0);
4308 pci_enable_wake(pdev, PCI_D3cold, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309
4310 e1000_reset(adapter);
4311 E1000_WRITE_REG(&adapter->hw, WUS, ~0);
4312
4313 if(netif_running(netdev))
4314 e1000_up(adapter);
4315
4316 netif_device_attach(netdev);
4317
4318 if(adapter->hw.mac_type >= e1000_82540 &&
4319 adapter->hw.media_type == e1000_media_type_copper) {
4320 manc = E1000_READ_REG(&adapter->hw, MANC);
4321 manc &= ~(E1000_MANC_ARP_EN);
4322 E1000_WRITE_REG(&adapter->hw, MANC, manc);
4323 }
4324
Malli Chilakala2d7edb92005-04-28 19:43:52 -07004325 switch(adapter->hw.mac_type) {
Mallikarjuna R Chilakala868d5302005-10-04 06:58:59 -04004326 case e1000_82571:
4327 case e1000_82572:
4328 ctrl_ext = E1000_READ_REG(&adapter->hw, CTRL_EXT);
4329 E1000_WRITE_REG(&adapter->hw, CTRL_EXT,
4330 ctrl_ext | E1000_CTRL_EXT_DRV_LOAD);
4331 break;
Malli Chilakala2d7edb92005-04-28 19:43:52 -07004332 case e1000_82573:
4333 swsm = E1000_READ_REG(&adapter->hw, SWSM);
4334 E1000_WRITE_REG(&adapter->hw, SWSM,
4335 swsm | E1000_SWSM_DRV_LOAD);
4336 break;
4337 default:
4338 break;
4339 }
4340
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341 return 0;
4342}
4343#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344#ifdef CONFIG_NET_POLL_CONTROLLER
4345/*
4346 * Polling 'interrupt' - used by things like netconsole to send skbs
4347 * without having to re-enable interrupts. It's not called while
4348 * the interrupt routine is executing.
4349 */
4350static void
Malli Chilakala26483452005-04-28 19:44:46 -07004351e1000_netpoll(struct net_device *netdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352{
Malli Chilakala60490fe2005-06-17 17:41:45 -07004353 struct e1000_adapter *adapter = netdev_priv(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354 disable_irq(adapter->pdev->irq);
4355 e1000_intr(adapter->pdev->irq, netdev, NULL);
Andrew Mortonc4cfe562005-10-15 00:32:29 -07004356 e1000_clean_tx_irq(adapter, adapter->tx_ring);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357 enable_irq(adapter->pdev->irq);
4358}
4359#endif
4360
4361/* e1000_main.c */