blob: 660a786532878e8b45bc3f5d0c881e02929e74cc [file] [log] [blame]
Auke Kok9d5c8242008-01-24 02:22:38 -08001/*******************************************************************************
2
3 Intel(R) Gigabit Ethernet Linux driver
4 Copyright(c) 2007 Intel Corporation.
5
6 This program is free software; you can redistribute it and/or modify it
7 under the terms and conditions of the GNU General Public License,
8 version 2, as published by the Free Software Foundation.
9
10 This program is distributed in the hope it will be useful, but WITHOUT
11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 more details.
14
15 You should have received a copy of the GNU General Public License along with
16 this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
18
19 The full GNU General Public License is included in this distribution in
20 the file called "COPYING".
21
22 Contact Information:
23 e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
24 Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
25
26*******************************************************************************/
27
28#include <linux/module.h>
29#include <linux/types.h>
30#include <linux/init.h>
31#include <linux/vmalloc.h>
32#include <linux/pagemap.h>
33#include <linux/netdevice.h>
Auke Kok9d5c8242008-01-24 02:22:38 -080034#include <linux/ipv6.h>
35#include <net/checksum.h>
36#include <net/ip6_checksum.h>
37#include <linux/mii.h>
38#include <linux/ethtool.h>
39#include <linux/if_vlan.h>
40#include <linux/pci.h>
41#include <linux/delay.h>
42#include <linux/interrupt.h>
43#include <linux/if_ether.h>
Jeb Cramerfe4506b2008-07-08 15:07:55 -070044#ifdef CONFIG_DCA
45#include <linux/dca.h>
46#endif
Auke Kok9d5c8242008-01-24 02:22:38 -080047#include "igb.h"
48
Auke Kok0024fd02008-07-08 15:09:37 -070049#define DRV_VERSION "1.2.45-k2"
Auke Kok9d5c8242008-01-24 02:22:38 -080050char igb_driver_name[] = "igb";
51char igb_driver_version[] = DRV_VERSION;
52static const char igb_driver_string[] =
53 "Intel(R) Gigabit Ethernet Network Driver";
Alexander Duyck2d064c02008-07-08 15:10:12 -070054static const char igb_copyright[] = "Copyright (c) 2008 Intel Corporation.";
Auke Kok9d5c8242008-01-24 02:22:38 -080055
Auke Kok9d5c8242008-01-24 02:22:38 -080056static const struct e1000_info *igb_info_tbl[] = {
57 [board_82575] = &e1000_82575_info,
58};
59
60static struct pci_device_id igb_pci_tbl[] = {
Alexander Duyck2d064c02008-07-08 15:10:12 -070061 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576), board_82575 },
62 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_FIBER), board_82575 },
63 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_SERDES), board_82575 },
64 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_QUAD_COPPER), board_82575 },
Auke Kok9d5c8242008-01-24 02:22:38 -080065 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82575EB_COPPER), board_82575 },
66 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82575EB_FIBER_SERDES), board_82575 },
67 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82575GB_QUAD_COPPER), board_82575 },
68 /* required last entry */
69 {0, }
70};
71
72MODULE_DEVICE_TABLE(pci, igb_pci_tbl);
73
74void igb_reset(struct igb_adapter *);
75static int igb_setup_all_tx_resources(struct igb_adapter *);
76static int igb_setup_all_rx_resources(struct igb_adapter *);
77static void igb_free_all_tx_resources(struct igb_adapter *);
78static void igb_free_all_rx_resources(struct igb_adapter *);
Mitch Williams3b644cf2008-06-27 10:59:48 -070079static void igb_free_tx_resources(struct igb_ring *);
80static void igb_free_rx_resources(struct igb_ring *);
Auke Kok9d5c8242008-01-24 02:22:38 -080081void igb_update_stats(struct igb_adapter *);
82static int igb_probe(struct pci_dev *, const struct pci_device_id *);
83static void __devexit igb_remove(struct pci_dev *pdev);
84static int igb_sw_init(struct igb_adapter *);
85static int igb_open(struct net_device *);
86static int igb_close(struct net_device *);
87static void igb_configure_tx(struct igb_adapter *);
88static void igb_configure_rx(struct igb_adapter *);
89static void igb_setup_rctl(struct igb_adapter *);
90static void igb_clean_all_tx_rings(struct igb_adapter *);
91static void igb_clean_all_rx_rings(struct igb_adapter *);
Mitch Williams3b644cf2008-06-27 10:59:48 -070092static void igb_clean_tx_ring(struct igb_ring *);
93static void igb_clean_rx_ring(struct igb_ring *);
Auke Kok9d5c8242008-01-24 02:22:38 -080094static void igb_set_multi(struct net_device *);
95static void igb_update_phy_info(unsigned long);
96static void igb_watchdog(unsigned long);
97static void igb_watchdog_task(struct work_struct *);
98static int igb_xmit_frame_ring_adv(struct sk_buff *, struct net_device *,
99 struct igb_ring *);
100static int igb_xmit_frame_adv(struct sk_buff *skb, struct net_device *);
101static struct net_device_stats *igb_get_stats(struct net_device *);
102static int igb_change_mtu(struct net_device *, int);
103static int igb_set_mac(struct net_device *, void *);
104static irqreturn_t igb_intr(int irq, void *);
105static irqreturn_t igb_intr_msi(int irq, void *);
106static irqreturn_t igb_msix_other(int irq, void *);
107static irqreturn_t igb_msix_rx(int irq, void *);
108static irqreturn_t igb_msix_tx(int irq, void *);
109static int igb_clean_rx_ring_msix(struct napi_struct *, int);
Jeb Cramerfe4506b2008-07-08 15:07:55 -0700110#ifdef CONFIG_DCA
111static void igb_update_rx_dca(struct igb_ring *);
112static void igb_update_tx_dca(struct igb_ring *);
113static void igb_setup_dca(struct igb_adapter *);
114#endif /* CONFIG_DCA */
Mitch Williams3b644cf2008-06-27 10:59:48 -0700115static bool igb_clean_tx_irq(struct igb_ring *);
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -0700116static int igb_poll(struct napi_struct *, int);
Mitch Williams3b644cf2008-06-27 10:59:48 -0700117static bool igb_clean_rx_irq_adv(struct igb_ring *, int *, int);
118static void igb_alloc_rx_buffers_adv(struct igb_ring *, int);
Auke Kok9d5c8242008-01-24 02:22:38 -0800119static int igb_ioctl(struct net_device *, struct ifreq *, int cmd);
120static void igb_tx_timeout(struct net_device *);
121static void igb_reset_task(struct work_struct *);
122static void igb_vlan_rx_register(struct net_device *, struct vlan_group *);
123static void igb_vlan_rx_add_vid(struct net_device *, u16);
124static void igb_vlan_rx_kill_vid(struct net_device *, u16);
125static void igb_restore_vlan(struct igb_adapter *);
126
127static int igb_suspend(struct pci_dev *, pm_message_t);
128#ifdef CONFIG_PM
129static int igb_resume(struct pci_dev *);
130#endif
131static void igb_shutdown(struct pci_dev *);
Jeb Cramerfe4506b2008-07-08 15:07:55 -0700132#ifdef CONFIG_DCA
133static int igb_notify_dca(struct notifier_block *, unsigned long, void *);
134static struct notifier_block dca_notifier = {
135 .notifier_call = igb_notify_dca,
136 .next = NULL,
137 .priority = 0
138};
139#endif
Auke Kok9d5c8242008-01-24 02:22:38 -0800140
141#ifdef CONFIG_NET_POLL_CONTROLLER
142/* for netdump / net console */
143static void igb_netpoll(struct net_device *);
144#endif
145
146static pci_ers_result_t igb_io_error_detected(struct pci_dev *,
147 pci_channel_state_t);
148static pci_ers_result_t igb_io_slot_reset(struct pci_dev *);
149static void igb_io_resume(struct pci_dev *);
150
151static struct pci_error_handlers igb_err_handler = {
152 .error_detected = igb_io_error_detected,
153 .slot_reset = igb_io_slot_reset,
154 .resume = igb_io_resume,
155};
156
157
158static struct pci_driver igb_driver = {
159 .name = igb_driver_name,
160 .id_table = igb_pci_tbl,
161 .probe = igb_probe,
162 .remove = __devexit_p(igb_remove),
163#ifdef CONFIG_PM
164 /* Power Managment Hooks */
165 .suspend = igb_suspend,
166 .resume = igb_resume,
167#endif
168 .shutdown = igb_shutdown,
169 .err_handler = &igb_err_handler
170};
171
Alexander Duyck7dfc16f2008-07-08 15:10:46 -0700172static int global_quad_port_a; /* global quad port a indication */
173
Auke Kok9d5c8242008-01-24 02:22:38 -0800174MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
175MODULE_DESCRIPTION("Intel(R) Gigabit Ethernet Network Driver");
176MODULE_LICENSE("GPL");
177MODULE_VERSION(DRV_VERSION);
178
179#ifdef DEBUG
180/**
181 * igb_get_hw_dev_name - return device name string
182 * used by hardware layer to print debugging information
183 **/
184char *igb_get_hw_dev_name(struct e1000_hw *hw)
185{
186 struct igb_adapter *adapter = hw->back;
187 return adapter->netdev->name;
188}
189#endif
190
191/**
192 * igb_init_module - Driver Registration Routine
193 *
194 * igb_init_module is the first routine called when the driver is
195 * loaded. All it does is register with the PCI subsystem.
196 **/
197static int __init igb_init_module(void)
198{
199 int ret;
200 printk(KERN_INFO "%s - version %s\n",
201 igb_driver_string, igb_driver_version);
202
203 printk(KERN_INFO "%s\n", igb_copyright);
204
Alexander Duyck7dfc16f2008-07-08 15:10:46 -0700205 global_quad_port_a = 0;
206
Auke Kok9d5c8242008-01-24 02:22:38 -0800207 ret = pci_register_driver(&igb_driver);
Jeb Cramerfe4506b2008-07-08 15:07:55 -0700208#ifdef CONFIG_DCA
209 dca_register_notify(&dca_notifier);
210#endif
Auke Kok9d5c8242008-01-24 02:22:38 -0800211 return ret;
212}
213
214module_init(igb_init_module);
215
216/**
217 * igb_exit_module - Driver Exit Cleanup Routine
218 *
219 * igb_exit_module is called just before the driver is removed
220 * from memory.
221 **/
222static void __exit igb_exit_module(void)
223{
Jeb Cramerfe4506b2008-07-08 15:07:55 -0700224#ifdef CONFIG_DCA
225 dca_unregister_notify(&dca_notifier);
226#endif
Auke Kok9d5c8242008-01-24 02:22:38 -0800227 pci_unregister_driver(&igb_driver);
228}
229
230module_exit(igb_exit_module);
231
232/**
233 * igb_alloc_queues - Allocate memory for all rings
234 * @adapter: board private structure to initialize
235 *
236 * We allocate one ring per queue at run-time since we don't know the
237 * number of queues at compile-time.
238 **/
239static int igb_alloc_queues(struct igb_adapter *adapter)
240{
241 int i;
242
243 adapter->tx_ring = kcalloc(adapter->num_tx_queues,
244 sizeof(struct igb_ring), GFP_KERNEL);
245 if (!adapter->tx_ring)
246 return -ENOMEM;
247
248 adapter->rx_ring = kcalloc(adapter->num_rx_queues,
249 sizeof(struct igb_ring), GFP_KERNEL);
250 if (!adapter->rx_ring) {
251 kfree(adapter->tx_ring);
252 return -ENOMEM;
253 }
254
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -0700255 for (i = 0; i < adapter->num_tx_queues; i++) {
256 struct igb_ring *ring = &(adapter->tx_ring[i]);
257 ring->adapter = adapter;
258 ring->queue_index = i;
259 }
Auke Kok9d5c8242008-01-24 02:22:38 -0800260 for (i = 0; i < adapter->num_rx_queues; i++) {
261 struct igb_ring *ring = &(adapter->rx_ring[i]);
262 ring->adapter = adapter;
PJ Waskiewicz844290e2008-06-27 11:00:39 -0700263 ring->queue_index = i;
Auke Kok9d5c8242008-01-24 02:22:38 -0800264 ring->itr_register = E1000_ITR;
265
PJ Waskiewicz844290e2008-06-27 11:00:39 -0700266 /* set a default napi handler for each rx_ring */
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -0700267 netif_napi_add(adapter->netdev, &ring->napi, igb_poll, 64);
Auke Kok9d5c8242008-01-24 02:22:38 -0800268 }
269 return 0;
270}
271
272#define IGB_N0_QUEUE -1
273static void igb_assign_vector(struct igb_adapter *adapter, int rx_queue,
274 int tx_queue, int msix_vector)
275{
276 u32 msixbm = 0;
277 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck2d064c02008-07-08 15:10:12 -0700278 u32 ivar, index;
279
280 switch (hw->mac.type) {
281 case e1000_82575:
Auke Kok9d5c8242008-01-24 02:22:38 -0800282 /* The 82575 assigns vectors using a bitmask, which matches the
283 bitmask for the EICR/EIMS/EIMC registers. To assign one
284 or more queues to a vector, we write the appropriate bits
285 into the MSIXBM register for that vector. */
286 if (rx_queue > IGB_N0_QUEUE) {
287 msixbm = E1000_EICR_RX_QUEUE0 << rx_queue;
288 adapter->rx_ring[rx_queue].eims_value = msixbm;
289 }
290 if (tx_queue > IGB_N0_QUEUE) {
291 msixbm |= E1000_EICR_TX_QUEUE0 << tx_queue;
292 adapter->tx_ring[tx_queue].eims_value =
293 E1000_EICR_TX_QUEUE0 << tx_queue;
294 }
295 array_wr32(E1000_MSIXBM(0), msix_vector, msixbm);
Alexander Duyck2d064c02008-07-08 15:10:12 -0700296 break;
297 case e1000_82576:
298 /* Kawela uses a table-based method for assigning vectors.
299 Each queue has a single entry in the table to which we write
300 a vector number along with a "valid" bit. Sadly, the layout
301 of the table is somewhat counterintuitive. */
302 if (rx_queue > IGB_N0_QUEUE) {
303 index = (rx_queue & 0x7);
304 ivar = array_rd32(E1000_IVAR0, index);
305 if (rx_queue < 8) {
306 /* vector goes into low byte of register */
307 ivar = ivar & 0xFFFFFF00;
308 ivar |= msix_vector | E1000_IVAR_VALID;
309 } else {
310 /* vector goes into third byte of register */
311 ivar = ivar & 0xFF00FFFF;
312 ivar |= (msix_vector | E1000_IVAR_VALID) << 16;
313 }
314 adapter->rx_ring[rx_queue].eims_value= 1 << msix_vector;
315 array_wr32(E1000_IVAR0, index, ivar);
316 }
317 if (tx_queue > IGB_N0_QUEUE) {
318 index = (tx_queue & 0x7);
319 ivar = array_rd32(E1000_IVAR0, index);
320 if (tx_queue < 8) {
321 /* vector goes into second byte of register */
322 ivar = ivar & 0xFFFF00FF;
323 ivar |= (msix_vector | E1000_IVAR_VALID) << 8;
324 } else {
325 /* vector goes into high byte of register */
326 ivar = ivar & 0x00FFFFFF;
327 ivar |= (msix_vector | E1000_IVAR_VALID) << 24;
328 }
329 adapter->tx_ring[tx_queue].eims_value= 1 << msix_vector;
330 array_wr32(E1000_IVAR0, index, ivar);
331 }
332 break;
333 default:
334 BUG();
335 break;
336 }
Auke Kok9d5c8242008-01-24 02:22:38 -0800337}
338
339/**
340 * igb_configure_msix - Configure MSI-X hardware
341 *
342 * igb_configure_msix sets up the hardware to properly
343 * generate MSI-X interrupts.
344 **/
345static void igb_configure_msix(struct igb_adapter *adapter)
346{
347 u32 tmp;
348 int i, vector = 0;
349 struct e1000_hw *hw = &adapter->hw;
350
351 adapter->eims_enable_mask = 0;
Alexander Duyck2d064c02008-07-08 15:10:12 -0700352 if (hw->mac.type == e1000_82576)
353 /* Turn on MSI-X capability first, or our settings
354 * won't stick. And it will take days to debug. */
355 wr32(E1000_GPIE, E1000_GPIE_MSIX_MODE |
356 E1000_GPIE_PBA | E1000_GPIE_EIAME |
357 E1000_GPIE_NSICR);
Auke Kok9d5c8242008-01-24 02:22:38 -0800358
359 for (i = 0; i < adapter->num_tx_queues; i++) {
360 struct igb_ring *tx_ring = &adapter->tx_ring[i];
361 igb_assign_vector(adapter, IGB_N0_QUEUE, i, vector++);
362 adapter->eims_enable_mask |= tx_ring->eims_value;
363 if (tx_ring->itr_val)
364 writel(1000000000 / (tx_ring->itr_val * 256),
365 hw->hw_addr + tx_ring->itr_register);
366 else
367 writel(1, hw->hw_addr + tx_ring->itr_register);
368 }
369
370 for (i = 0; i < adapter->num_rx_queues; i++) {
371 struct igb_ring *rx_ring = &adapter->rx_ring[i];
372 igb_assign_vector(adapter, i, IGB_N0_QUEUE, vector++);
373 adapter->eims_enable_mask |= rx_ring->eims_value;
374 if (rx_ring->itr_val)
375 writel(1000000000 / (rx_ring->itr_val * 256),
376 hw->hw_addr + rx_ring->itr_register);
377 else
378 writel(1, hw->hw_addr + rx_ring->itr_register);
379 }
380
381
382 /* set vector for other causes, i.e. link changes */
Alexander Duyck2d064c02008-07-08 15:10:12 -0700383 switch (hw->mac.type) {
384 case e1000_82575:
Auke Kok9d5c8242008-01-24 02:22:38 -0800385 array_wr32(E1000_MSIXBM(0), vector++,
386 E1000_EIMS_OTHER);
387
Auke Kok9d5c8242008-01-24 02:22:38 -0800388 tmp = rd32(E1000_CTRL_EXT);
389 /* enable MSI-X PBA support*/
390 tmp |= E1000_CTRL_EXT_PBA_CLR;
391
392 /* Auto-Mask interrupts upon ICR read. */
393 tmp |= E1000_CTRL_EXT_EIAME;
394 tmp |= E1000_CTRL_EXT_IRCA;
395
396 wr32(E1000_CTRL_EXT, tmp);
397 adapter->eims_enable_mask |= E1000_EIMS_OTHER;
PJ Waskiewicz844290e2008-06-27 11:00:39 -0700398 adapter->eims_other = E1000_EIMS_OTHER;
Auke Kok9d5c8242008-01-24 02:22:38 -0800399
Alexander Duyck2d064c02008-07-08 15:10:12 -0700400 break;
401
402 case e1000_82576:
403 tmp = (vector++ | E1000_IVAR_VALID) << 8;
404 wr32(E1000_IVAR_MISC, tmp);
405
406 adapter->eims_enable_mask = (1 << (vector)) - 1;
407 adapter->eims_other = 1 << (vector - 1);
408 break;
409 default:
410 /* do nothing, since nothing else supports MSI-X */
411 break;
412 } /* switch (hw->mac.type) */
Auke Kok9d5c8242008-01-24 02:22:38 -0800413 wrfl();
414}
415
416/**
417 * igb_request_msix - Initialize MSI-X interrupts
418 *
419 * igb_request_msix allocates MSI-X vectors and requests interrupts from the
420 * kernel.
421 **/
422static int igb_request_msix(struct igb_adapter *adapter)
423{
424 struct net_device *netdev = adapter->netdev;
425 int i, err = 0, vector = 0;
426
427 vector = 0;
428
429 for (i = 0; i < adapter->num_tx_queues; i++) {
430 struct igb_ring *ring = &(adapter->tx_ring[i]);
431 sprintf(ring->name, "%s-tx%d", netdev->name, i);
432 err = request_irq(adapter->msix_entries[vector].vector,
433 &igb_msix_tx, 0, ring->name,
434 &(adapter->tx_ring[i]));
435 if (err)
436 goto out;
437 ring->itr_register = E1000_EITR(0) + (vector << 2);
438 ring->itr_val = adapter->itr;
439 vector++;
440 }
441 for (i = 0; i < adapter->num_rx_queues; i++) {
442 struct igb_ring *ring = &(adapter->rx_ring[i]);
443 if (strlen(netdev->name) < (IFNAMSIZ - 5))
444 sprintf(ring->name, "%s-rx%d", netdev->name, i);
445 else
446 memcpy(ring->name, netdev->name, IFNAMSIZ);
447 err = request_irq(adapter->msix_entries[vector].vector,
448 &igb_msix_rx, 0, ring->name,
449 &(adapter->rx_ring[i]));
450 if (err)
451 goto out;
452 ring->itr_register = E1000_EITR(0) + (vector << 2);
453 ring->itr_val = adapter->itr;
PJ Waskiewicz844290e2008-06-27 11:00:39 -0700454 /* overwrite the poll routine for MSIX, we've already done
455 * netif_napi_add */
456 ring->napi.poll = &igb_clean_rx_ring_msix;
Auke Kok9d5c8242008-01-24 02:22:38 -0800457 vector++;
458 }
459
460 err = request_irq(adapter->msix_entries[vector].vector,
461 &igb_msix_other, 0, netdev->name, netdev);
462 if (err)
463 goto out;
464
Auke Kok9d5c8242008-01-24 02:22:38 -0800465 igb_configure_msix(adapter);
466 return 0;
467out:
468 return err;
469}
470
471static void igb_reset_interrupt_capability(struct igb_adapter *adapter)
472{
473 if (adapter->msix_entries) {
474 pci_disable_msix(adapter->pdev);
475 kfree(adapter->msix_entries);
476 adapter->msix_entries = NULL;
Alexander Duyck7dfc16f2008-07-08 15:10:46 -0700477 } else if (adapter->flags & IGB_FLAG_HAS_MSI)
Auke Kok9d5c8242008-01-24 02:22:38 -0800478 pci_disable_msi(adapter->pdev);
479 return;
480}
481
482
483/**
484 * igb_set_interrupt_capability - set MSI or MSI-X if supported
485 *
486 * Attempt to configure interrupts using the best available
487 * capabilities of the hardware and kernel.
488 **/
489static void igb_set_interrupt_capability(struct igb_adapter *adapter)
490{
491 int err;
492 int numvecs, i;
493
494 numvecs = adapter->num_tx_queues + adapter->num_rx_queues + 1;
495 adapter->msix_entries = kcalloc(numvecs, sizeof(struct msix_entry),
496 GFP_KERNEL);
497 if (!adapter->msix_entries)
498 goto msi_only;
499
500 for (i = 0; i < numvecs; i++)
501 adapter->msix_entries[i].entry = i;
502
503 err = pci_enable_msix(adapter->pdev,
504 adapter->msix_entries,
505 numvecs);
506 if (err == 0)
507 return;
508
509 igb_reset_interrupt_capability(adapter);
510
511 /* If we can't do MSI-X, try MSI */
512msi_only:
513 adapter->num_rx_queues = 1;
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -0700514 adapter->num_tx_queues = 1;
Auke Kok9d5c8242008-01-24 02:22:38 -0800515 if (!pci_enable_msi(adapter->pdev))
Alexander Duyck7dfc16f2008-07-08 15:10:46 -0700516 adapter->flags |= IGB_FLAG_HAS_MSI;
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -0700517
518#ifdef CONFIG_NETDEVICES_MULTIQUEUE
519 /* Notify the stack of the (possibly) reduced Tx Queue count. */
520 adapter->netdev->egress_subqueue_count = adapter->num_tx_queues;
521#endif
Auke Kok9d5c8242008-01-24 02:22:38 -0800522 return;
523}
524
525/**
526 * igb_request_irq - initialize interrupts
527 *
528 * Attempts to configure interrupts using the best available
529 * capabilities of the hardware and kernel.
530 **/
531static int igb_request_irq(struct igb_adapter *adapter)
532{
533 struct net_device *netdev = adapter->netdev;
534 struct e1000_hw *hw = &adapter->hw;
535 int err = 0;
536
537 if (adapter->msix_entries) {
538 err = igb_request_msix(adapter);
PJ Waskiewicz844290e2008-06-27 11:00:39 -0700539 if (!err)
Auke Kok9d5c8242008-01-24 02:22:38 -0800540 goto request_done;
Auke Kok9d5c8242008-01-24 02:22:38 -0800541 /* fall back to MSI */
542 igb_reset_interrupt_capability(adapter);
543 if (!pci_enable_msi(adapter->pdev))
Alexander Duyck7dfc16f2008-07-08 15:10:46 -0700544 adapter->flags |= IGB_FLAG_HAS_MSI;
Auke Kok9d5c8242008-01-24 02:22:38 -0800545 igb_free_all_tx_resources(adapter);
546 igb_free_all_rx_resources(adapter);
547 adapter->num_rx_queues = 1;
548 igb_alloc_queues(adapter);
PJ Waskiewicz844290e2008-06-27 11:00:39 -0700549 } else {
Alexander Duyck2d064c02008-07-08 15:10:12 -0700550 switch (hw->mac.type) {
551 case e1000_82575:
552 wr32(E1000_MSIXBM(0),
553 (E1000_EICR_RX_QUEUE0 | E1000_EIMS_OTHER));
554 break;
555 case e1000_82576:
556 wr32(E1000_IVAR0, E1000_IVAR_VALID);
557 break;
558 default:
559 break;
560 }
Auke Kok9d5c8242008-01-24 02:22:38 -0800561 }
PJ Waskiewicz844290e2008-06-27 11:00:39 -0700562
Alexander Duyck7dfc16f2008-07-08 15:10:46 -0700563 if (adapter->flags & IGB_FLAG_HAS_MSI) {
Auke Kok9d5c8242008-01-24 02:22:38 -0800564 err = request_irq(adapter->pdev->irq, &igb_intr_msi, 0,
565 netdev->name, netdev);
566 if (!err)
567 goto request_done;
568 /* fall back to legacy interrupts */
569 igb_reset_interrupt_capability(adapter);
Alexander Duyck7dfc16f2008-07-08 15:10:46 -0700570 adapter->flags &= ~IGB_FLAG_HAS_MSI;
Auke Kok9d5c8242008-01-24 02:22:38 -0800571 }
572
573 err = request_irq(adapter->pdev->irq, &igb_intr, IRQF_SHARED,
574 netdev->name, netdev);
575
Andy Gospodarek6cb5e572008-02-15 14:05:25 -0800576 if (err)
Auke Kok9d5c8242008-01-24 02:22:38 -0800577 dev_err(&adapter->pdev->dev, "Error %d getting interrupt\n",
578 err);
Auke Kok9d5c8242008-01-24 02:22:38 -0800579
580request_done:
581 return err;
582}
583
584static void igb_free_irq(struct igb_adapter *adapter)
585{
586 struct net_device *netdev = adapter->netdev;
587
588 if (adapter->msix_entries) {
589 int vector = 0, i;
590
591 for (i = 0; i < adapter->num_tx_queues; i++)
592 free_irq(adapter->msix_entries[vector++].vector,
593 &(adapter->tx_ring[i]));
594 for (i = 0; i < adapter->num_rx_queues; i++)
595 free_irq(adapter->msix_entries[vector++].vector,
596 &(adapter->rx_ring[i]));
597
598 free_irq(adapter->msix_entries[vector++].vector, netdev);
599 return;
600 }
601
602 free_irq(adapter->pdev->irq, netdev);
603}
604
605/**
606 * igb_irq_disable - Mask off interrupt generation on the NIC
607 * @adapter: board private structure
608 **/
609static void igb_irq_disable(struct igb_adapter *adapter)
610{
611 struct e1000_hw *hw = &adapter->hw;
612
613 if (adapter->msix_entries) {
PJ Waskiewicz844290e2008-06-27 11:00:39 -0700614 wr32(E1000_EIAM, 0);
Auke Kok9d5c8242008-01-24 02:22:38 -0800615 wr32(E1000_EIMC, ~0);
616 wr32(E1000_EIAC, 0);
617 }
PJ Waskiewicz844290e2008-06-27 11:00:39 -0700618
619 wr32(E1000_IAM, 0);
Auke Kok9d5c8242008-01-24 02:22:38 -0800620 wr32(E1000_IMC, ~0);
621 wrfl();
622 synchronize_irq(adapter->pdev->irq);
623}
624
625/**
626 * igb_irq_enable - Enable default interrupt generation settings
627 * @adapter: board private structure
628 **/
629static void igb_irq_enable(struct igb_adapter *adapter)
630{
631 struct e1000_hw *hw = &adapter->hw;
632
633 if (adapter->msix_entries) {
PJ Waskiewicz844290e2008-06-27 11:00:39 -0700634 wr32(E1000_EIAC, adapter->eims_enable_mask);
635 wr32(E1000_EIAM, adapter->eims_enable_mask);
636 wr32(E1000_EIMS, adapter->eims_enable_mask);
Auke Kok9d5c8242008-01-24 02:22:38 -0800637 wr32(E1000_IMS, E1000_IMS_LSC);
PJ Waskiewicz844290e2008-06-27 11:00:39 -0700638 } else {
639 wr32(E1000_IMS, IMS_ENABLE_MASK);
640 wr32(E1000_IAM, IMS_ENABLE_MASK);
641 }
Auke Kok9d5c8242008-01-24 02:22:38 -0800642}
643
644static void igb_update_mng_vlan(struct igb_adapter *adapter)
645{
646 struct net_device *netdev = adapter->netdev;
647 u16 vid = adapter->hw.mng_cookie.vlan_id;
648 u16 old_vid = adapter->mng_vlan_id;
649 if (adapter->vlgrp) {
650 if (!vlan_group_get_device(adapter->vlgrp, vid)) {
651 if (adapter->hw.mng_cookie.status &
652 E1000_MNG_DHCP_COOKIE_STATUS_VLAN) {
653 igb_vlan_rx_add_vid(netdev, vid);
654 adapter->mng_vlan_id = vid;
655 } else
656 adapter->mng_vlan_id = IGB_MNG_VLAN_NONE;
657
658 if ((old_vid != (u16)IGB_MNG_VLAN_NONE) &&
659 (vid != old_vid) &&
660 !vlan_group_get_device(adapter->vlgrp, old_vid))
661 igb_vlan_rx_kill_vid(netdev, old_vid);
662 } else
663 adapter->mng_vlan_id = vid;
664 }
665}
666
667/**
668 * igb_release_hw_control - release control of the h/w to f/w
669 * @adapter: address of board private structure
670 *
671 * igb_release_hw_control resets CTRL_EXT:DRV_LOAD bit.
672 * For ASF and Pass Through versions of f/w this means that the
673 * driver is no longer loaded.
674 *
675 **/
676static void igb_release_hw_control(struct igb_adapter *adapter)
677{
678 struct e1000_hw *hw = &adapter->hw;
679 u32 ctrl_ext;
680
681 /* Let firmware take over control of h/w */
682 ctrl_ext = rd32(E1000_CTRL_EXT);
683 wr32(E1000_CTRL_EXT,
684 ctrl_ext & ~E1000_CTRL_EXT_DRV_LOAD);
685}
686
687
688/**
689 * igb_get_hw_control - get control of the h/w from f/w
690 * @adapter: address of board private structure
691 *
692 * igb_get_hw_control sets CTRL_EXT:DRV_LOAD bit.
693 * For ASF and Pass Through versions of f/w this means that
694 * the driver is loaded.
695 *
696 **/
697static void igb_get_hw_control(struct igb_adapter *adapter)
698{
699 struct e1000_hw *hw = &adapter->hw;
700 u32 ctrl_ext;
701
702 /* Let firmware know the driver has taken over */
703 ctrl_ext = rd32(E1000_CTRL_EXT);
704 wr32(E1000_CTRL_EXT,
705 ctrl_ext | E1000_CTRL_EXT_DRV_LOAD);
706}
707
708static void igb_init_manageability(struct igb_adapter *adapter)
709{
710 struct e1000_hw *hw = &adapter->hw;
711
712 if (adapter->en_mng_pt) {
713 u32 manc2h = rd32(E1000_MANC2H);
714 u32 manc = rd32(E1000_MANC);
715
Auke Kok9d5c8242008-01-24 02:22:38 -0800716 /* enable receiving management packets to the host */
717 /* this will probably generate destination unreachable messages
718 * from the host OS, but the packets will be handled on SMBUS */
719 manc |= E1000_MANC_EN_MNG2HOST;
720#define E1000_MNG2HOST_PORT_623 (1 << 5)
721#define E1000_MNG2HOST_PORT_664 (1 << 6)
722 manc2h |= E1000_MNG2HOST_PORT_623;
723 manc2h |= E1000_MNG2HOST_PORT_664;
724 wr32(E1000_MANC2H, manc2h);
725
726 wr32(E1000_MANC, manc);
727 }
728}
729
Auke Kok9d5c8242008-01-24 02:22:38 -0800730/**
731 * igb_configure - configure the hardware for RX and TX
732 * @adapter: private board structure
733 **/
734static void igb_configure(struct igb_adapter *adapter)
735{
736 struct net_device *netdev = adapter->netdev;
737 int i;
738
739 igb_get_hw_control(adapter);
740 igb_set_multi(netdev);
741
742 igb_restore_vlan(adapter);
743 igb_init_manageability(adapter);
744
745 igb_configure_tx(adapter);
746 igb_setup_rctl(adapter);
747 igb_configure_rx(adapter);
Alexander Duyck662d7202008-06-27 11:00:29 -0700748
749 igb_rx_fifo_flush_82575(&adapter->hw);
750
Auke Kok9d5c8242008-01-24 02:22:38 -0800751 /* call IGB_DESC_UNUSED which always leaves
752 * at least 1 descriptor unused to make sure
753 * next_to_use != next_to_clean */
754 for (i = 0; i < adapter->num_rx_queues; i++) {
755 struct igb_ring *ring = &adapter->rx_ring[i];
Mitch Williams3b644cf2008-06-27 10:59:48 -0700756 igb_alloc_rx_buffers_adv(ring, IGB_DESC_UNUSED(ring));
Auke Kok9d5c8242008-01-24 02:22:38 -0800757 }
758
759
760 adapter->tx_queue_len = netdev->tx_queue_len;
761}
762
763
764/**
765 * igb_up - Open the interface and prepare it to handle traffic
766 * @adapter: board private structure
767 **/
768
769int igb_up(struct igb_adapter *adapter)
770{
771 struct e1000_hw *hw = &adapter->hw;
772 int i;
773
774 /* hardware has been reset, we need to reload some things */
775 igb_configure(adapter);
776
777 clear_bit(__IGB_DOWN, &adapter->state);
778
PJ Waskiewicz844290e2008-06-27 11:00:39 -0700779 for (i = 0; i < adapter->num_rx_queues; i++)
780 napi_enable(&adapter->rx_ring[i].napi);
781 if (adapter->msix_entries)
Auke Kok9d5c8242008-01-24 02:22:38 -0800782 igb_configure_msix(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -0800783
784 /* Clear any pending interrupts. */
785 rd32(E1000_ICR);
786 igb_irq_enable(adapter);
787
788 /* Fire a link change interrupt to start the watchdog. */
789 wr32(E1000_ICS, E1000_ICS_LSC);
790 return 0;
791}
792
793void igb_down(struct igb_adapter *adapter)
794{
795 struct e1000_hw *hw = &adapter->hw;
796 struct net_device *netdev = adapter->netdev;
797 u32 tctl, rctl;
798 int i;
799
800 /* signal that we're down so the interrupt handler does not
801 * reschedule our watchdog timer */
802 set_bit(__IGB_DOWN, &adapter->state);
803
804 /* disable receives in the hardware */
805 rctl = rd32(E1000_RCTL);
806 wr32(E1000_RCTL, rctl & ~E1000_RCTL_EN);
807 /* flush and sleep below */
808
809 netif_stop_queue(netdev);
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -0700810#ifdef CONFIG_NETDEVICES_MULTIQUEUE
811 for (i = 0; i < adapter->num_tx_queues; i++)
812 netif_stop_subqueue(netdev, i);
813#endif
Auke Kok9d5c8242008-01-24 02:22:38 -0800814
815 /* disable transmits in the hardware */
816 tctl = rd32(E1000_TCTL);
817 tctl &= ~E1000_TCTL_EN;
818 wr32(E1000_TCTL, tctl);
819 /* flush both disables and wait for them to finish */
820 wrfl();
821 msleep(10);
822
PJ Waskiewicz844290e2008-06-27 11:00:39 -0700823 for (i = 0; i < adapter->num_rx_queues; i++)
824 napi_disable(&adapter->rx_ring[i].napi);
Auke Kok9d5c8242008-01-24 02:22:38 -0800825
Auke Kok9d5c8242008-01-24 02:22:38 -0800826 igb_irq_disable(adapter);
827
828 del_timer_sync(&adapter->watchdog_timer);
829 del_timer_sync(&adapter->phy_info_timer);
830
831 netdev->tx_queue_len = adapter->tx_queue_len;
832 netif_carrier_off(netdev);
833 adapter->link_speed = 0;
834 adapter->link_duplex = 0;
835
Jeff Kirsher30236822008-06-24 17:01:15 -0700836 if (!pci_channel_offline(adapter->pdev))
837 igb_reset(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -0800838 igb_clean_all_tx_rings(adapter);
839 igb_clean_all_rx_rings(adapter);
840}
841
842void igb_reinit_locked(struct igb_adapter *adapter)
843{
844 WARN_ON(in_interrupt());
845 while (test_and_set_bit(__IGB_RESETTING, &adapter->state))
846 msleep(1);
847 igb_down(adapter);
848 igb_up(adapter);
849 clear_bit(__IGB_RESETTING, &adapter->state);
850}
851
852void igb_reset(struct igb_adapter *adapter)
853{
854 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck2d064c02008-07-08 15:10:12 -0700855 struct e1000_mac_info *mac = &hw->mac;
856 struct e1000_fc_info *fc = &hw->fc;
Auke Kok9d5c8242008-01-24 02:22:38 -0800857 u32 pba = 0, tx_space, min_tx_space, min_rx_space;
858 u16 hwm;
859
860 /* Repartition Pba for greater than 9k mtu
861 * To take effect CTRL.RST is required.
862 */
Alexander Duyck2d064c02008-07-08 15:10:12 -0700863 if (mac->type != e1000_82576) {
Auke Kok9d5c8242008-01-24 02:22:38 -0800864 pba = E1000_PBA_34K;
Alexander Duyck2d064c02008-07-08 15:10:12 -0700865 }
866 else {
867 pba = E1000_PBA_64K;
868 }
Auke Kok9d5c8242008-01-24 02:22:38 -0800869
Alexander Duyck2d064c02008-07-08 15:10:12 -0700870 if ((adapter->max_frame_size > ETH_FRAME_LEN + ETH_FCS_LEN) &&
871 (mac->type < e1000_82576)) {
Auke Kok9d5c8242008-01-24 02:22:38 -0800872 /* adjust PBA for jumbo frames */
873 wr32(E1000_PBA, pba);
874
875 /* To maintain wire speed transmits, the Tx FIFO should be
876 * large enough to accommodate two full transmit packets,
877 * rounded up to the next 1KB and expressed in KB. Likewise,
878 * the Rx FIFO should be large enough to accommodate at least
879 * one full receive packet and is similarly rounded up and
880 * expressed in KB. */
881 pba = rd32(E1000_PBA);
882 /* upper 16 bits has Tx packet buffer allocation size in KB */
883 tx_space = pba >> 16;
884 /* lower 16 bits has Rx packet buffer allocation size in KB */
885 pba &= 0xffff;
886 /* the tx fifo also stores 16 bytes of information about the tx
887 * but don't include ethernet FCS because hardware appends it */
888 min_tx_space = (adapter->max_frame_size +
889 sizeof(struct e1000_tx_desc) -
890 ETH_FCS_LEN) * 2;
891 min_tx_space = ALIGN(min_tx_space, 1024);
892 min_tx_space >>= 10;
893 /* software strips receive CRC, so leave room for it */
894 min_rx_space = adapter->max_frame_size;
895 min_rx_space = ALIGN(min_rx_space, 1024);
896 min_rx_space >>= 10;
897
898 /* If current Tx allocation is less than the min Tx FIFO size,
899 * and the min Tx FIFO size is less than the current Rx FIFO
900 * allocation, take space away from current Rx allocation */
901 if (tx_space < min_tx_space &&
902 ((min_tx_space - tx_space) < pba)) {
903 pba = pba - (min_tx_space - tx_space);
904
905 /* if short on rx space, rx wins and must trump tx
906 * adjustment */
907 if (pba < min_rx_space)
908 pba = min_rx_space;
909 }
Alexander Duyck2d064c02008-07-08 15:10:12 -0700910 wr32(E1000_PBA, pba);
Auke Kok9d5c8242008-01-24 02:22:38 -0800911 }
Auke Kok9d5c8242008-01-24 02:22:38 -0800912
913 /* flow control settings */
914 /* The high water mark must be low enough to fit one full frame
915 * (or the size used for early receive) above it in the Rx FIFO.
916 * Set it to the lower of:
917 * - 90% of the Rx FIFO size, or
918 * - the full Rx FIFO size minus one full frame */
919 hwm = min(((pba << 10) * 9 / 10),
Alexander Duyck2d064c02008-07-08 15:10:12 -0700920 ((pba << 10) - 2 * adapter->max_frame_size));
Auke Kok9d5c8242008-01-24 02:22:38 -0800921
Alexander Duyck2d064c02008-07-08 15:10:12 -0700922 if (mac->type < e1000_82576) {
923 fc->high_water = hwm & 0xFFF8; /* 8-byte granularity */
924 fc->low_water = fc->high_water - 8;
925 } else {
926 fc->high_water = hwm & 0xFFF0; /* 16-byte granularity */
927 fc->low_water = fc->high_water - 16;
928 }
Auke Kok9d5c8242008-01-24 02:22:38 -0800929 fc->pause_time = 0xFFFF;
930 fc->send_xon = 1;
931 fc->type = fc->original_type;
932
933 /* Allow time for pending master requests to run */
934 adapter->hw.mac.ops.reset_hw(&adapter->hw);
935 wr32(E1000_WUC, 0);
936
937 if (adapter->hw.mac.ops.init_hw(&adapter->hw))
938 dev_err(&adapter->pdev->dev, "Hardware Error\n");
939
940 igb_update_mng_vlan(adapter);
941
942 /* Enable h/w to recognize an 802.1Q VLAN Ethernet packet */
943 wr32(E1000_VET, ETHERNET_IEEE_VLAN_TYPE);
944
945 igb_reset_adaptive(&adapter->hw);
Bill Hayes68707ac2008-02-19 10:24:41 -0800946 if (adapter->hw.phy.ops.get_phy_info)
947 adapter->hw.phy.ops.get_phy_info(&adapter->hw);
Auke Kok9d5c8242008-01-24 02:22:38 -0800948}
949
950/**
Taku Izumi42bfd33a2008-06-20 12:10:30 +0900951 * igb_is_need_ioport - determine if an adapter needs ioport resources or not
952 * @pdev: PCI device information struct
953 *
954 * Returns true if an adapter needs ioport resources
955 **/
956static int igb_is_need_ioport(struct pci_dev *pdev)
957{
958 switch (pdev->device) {
959 /* Currently there are no adapters that need ioport resources */
960 default:
961 return false;
962 }
963}
964
965/**
Auke Kok9d5c8242008-01-24 02:22:38 -0800966 * igb_probe - Device Initialization Routine
967 * @pdev: PCI device information struct
968 * @ent: entry in igb_pci_tbl
969 *
970 * Returns 0 on success, negative on failure
971 *
972 * igb_probe initializes an adapter identified by a pci_dev structure.
973 * The OS initialization, configuring of the adapter private structure,
974 * and a hardware reset occur.
975 **/
976static int __devinit igb_probe(struct pci_dev *pdev,
977 const struct pci_device_id *ent)
978{
979 struct net_device *netdev;
980 struct igb_adapter *adapter;
981 struct e1000_hw *hw;
982 const struct e1000_info *ei = igb_info_tbl[ent->driver_data];
983 unsigned long mmio_start, mmio_len;
Auke Kok9d5c8242008-01-24 02:22:38 -0800984 int i, err, pci_using_dac;
985 u16 eeprom_data = 0;
986 u16 eeprom_apme_mask = IGB_EEPROM_APME;
987 u32 part_num;
Taku Izumi42bfd33a2008-06-20 12:10:30 +0900988 int bars, need_ioport;
Auke Kok9d5c8242008-01-24 02:22:38 -0800989
Taku Izumi42bfd33a2008-06-20 12:10:30 +0900990 /* do not allocate ioport bars when not needed */
991 need_ioport = igb_is_need_ioport(pdev);
992 if (need_ioport) {
993 bars = pci_select_bars(pdev, IORESOURCE_MEM | IORESOURCE_IO);
994 err = pci_enable_device(pdev);
995 } else {
996 bars = pci_select_bars(pdev, IORESOURCE_MEM);
997 err = pci_enable_device_mem(pdev);
998 }
Auke Kok9d5c8242008-01-24 02:22:38 -0800999 if (err)
1000 return err;
1001
1002 pci_using_dac = 0;
1003 err = pci_set_dma_mask(pdev, DMA_64BIT_MASK);
1004 if (!err) {
1005 err = pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK);
1006 if (!err)
1007 pci_using_dac = 1;
1008 } else {
1009 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
1010 if (err) {
1011 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
1012 if (err) {
1013 dev_err(&pdev->dev, "No usable DMA "
1014 "configuration, aborting\n");
1015 goto err_dma;
1016 }
1017 }
1018 }
1019
Taku Izumi42bfd33a2008-06-20 12:10:30 +09001020 err = pci_request_selected_regions(pdev, bars, igb_driver_name);
Auke Kok9d5c8242008-01-24 02:22:38 -08001021 if (err)
1022 goto err_pci_reg;
1023
1024 pci_set_master(pdev);
Auke Kokc682fc22008-04-23 11:09:34 -07001025 pci_save_state(pdev);
Auke Kok9d5c8242008-01-24 02:22:38 -08001026
1027 err = -ENOMEM;
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07001028#ifdef CONFIG_NETDEVICES_MULTIQUEUE
1029 netdev = alloc_etherdev_mq(sizeof(struct igb_adapter), IGB_MAX_TX_QUEUES);
1030#else
Auke Kok9d5c8242008-01-24 02:22:38 -08001031 netdev = alloc_etherdev(sizeof(struct igb_adapter));
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07001032#endif /* CONFIG_NETDEVICES_MULTIQUEUE */
Auke Kok9d5c8242008-01-24 02:22:38 -08001033 if (!netdev)
1034 goto err_alloc_etherdev;
1035
1036 SET_NETDEV_DEV(netdev, &pdev->dev);
1037
1038 pci_set_drvdata(pdev, netdev);
1039 adapter = netdev_priv(netdev);
1040 adapter->netdev = netdev;
1041 adapter->pdev = pdev;
1042 hw = &adapter->hw;
1043 hw->back = adapter;
1044 adapter->msg_enable = NETIF_MSG_DRV | NETIF_MSG_PROBE;
Taku Izumi42bfd33a2008-06-20 12:10:30 +09001045 adapter->bars = bars;
1046 adapter->need_ioport = need_ioport;
Auke Kok9d5c8242008-01-24 02:22:38 -08001047
1048 mmio_start = pci_resource_start(pdev, 0);
1049 mmio_len = pci_resource_len(pdev, 0);
1050
1051 err = -EIO;
1052 adapter->hw.hw_addr = ioremap(mmio_start, mmio_len);
1053 if (!adapter->hw.hw_addr)
1054 goto err_ioremap;
1055
1056 netdev->open = &igb_open;
1057 netdev->stop = &igb_close;
1058 netdev->get_stats = &igb_get_stats;
1059 netdev->set_multicast_list = &igb_set_multi;
1060 netdev->set_mac_address = &igb_set_mac;
1061 netdev->change_mtu = &igb_change_mtu;
1062 netdev->do_ioctl = &igb_ioctl;
1063 igb_set_ethtool_ops(netdev);
1064 netdev->tx_timeout = &igb_tx_timeout;
1065 netdev->watchdog_timeo = 5 * HZ;
Auke Kok9d5c8242008-01-24 02:22:38 -08001066 netdev->vlan_rx_register = igb_vlan_rx_register;
1067 netdev->vlan_rx_add_vid = igb_vlan_rx_add_vid;
1068 netdev->vlan_rx_kill_vid = igb_vlan_rx_kill_vid;
1069#ifdef CONFIG_NET_POLL_CONTROLLER
1070 netdev->poll_controller = igb_netpoll;
1071#endif
1072 netdev->hard_start_xmit = &igb_xmit_frame_adv;
1073
1074 strncpy(netdev->name, pci_name(pdev), sizeof(netdev->name) - 1);
1075
1076 netdev->mem_start = mmio_start;
1077 netdev->mem_end = mmio_start + mmio_len;
1078
Auke Kok9d5c8242008-01-24 02:22:38 -08001079 /* PCI config space info */
1080 hw->vendor_id = pdev->vendor;
1081 hw->device_id = pdev->device;
1082 hw->revision_id = pdev->revision;
1083 hw->subsystem_vendor_id = pdev->subsystem_vendor;
1084 hw->subsystem_device_id = pdev->subsystem_device;
1085
1086 /* setup the private structure */
1087 hw->back = adapter;
1088 /* Copy the default MAC, PHY and NVM function pointers */
1089 memcpy(&hw->mac.ops, ei->mac_ops, sizeof(hw->mac.ops));
1090 memcpy(&hw->phy.ops, ei->phy_ops, sizeof(hw->phy.ops));
1091 memcpy(&hw->nvm.ops, ei->nvm_ops, sizeof(hw->nvm.ops));
1092 /* Initialize skew-specific constants */
1093 err = ei->get_invariants(hw);
1094 if (err)
1095 goto err_hw_init;
1096
1097 err = igb_sw_init(adapter);
1098 if (err)
1099 goto err_sw_init;
1100
1101 igb_get_bus_info_pcie(hw);
1102
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07001103 /* set flags */
1104 switch (hw->mac.type) {
1105 case e1000_82576:
1106 case e1000_82575:
1107 adapter->flags |= IGB_FLAG_HAS_DCA;
1108 adapter->flags |= IGB_FLAG_NEED_CTX_IDX;
1109 break;
1110 default:
1111 break;
1112 }
1113
Auke Kok9d5c8242008-01-24 02:22:38 -08001114 hw->phy.autoneg_wait_to_complete = false;
1115 hw->mac.adaptive_ifs = true;
1116
1117 /* Copper options */
1118 if (hw->phy.media_type == e1000_media_type_copper) {
1119 hw->phy.mdix = AUTO_ALL_MODES;
1120 hw->phy.disable_polarity_correction = false;
1121 hw->phy.ms_type = e1000_ms_hw_default;
1122 }
1123
1124 if (igb_check_reset_block(hw))
1125 dev_info(&pdev->dev,
1126 "PHY reset is blocked due to SOL/IDER session.\n");
1127
1128 netdev->features = NETIF_F_SG |
1129 NETIF_F_HW_CSUM |
1130 NETIF_F_HW_VLAN_TX |
1131 NETIF_F_HW_VLAN_RX |
1132 NETIF_F_HW_VLAN_FILTER;
1133
1134 netdev->features |= NETIF_F_TSO;
Auke Kok9d5c8242008-01-24 02:22:38 -08001135 netdev->features |= NETIF_F_TSO6;
Jeff Kirsher48f29ff2008-06-05 04:06:27 -07001136
1137 netdev->vlan_features |= NETIF_F_TSO;
1138 netdev->vlan_features |= NETIF_F_TSO6;
1139 netdev->vlan_features |= NETIF_F_HW_CSUM;
1140 netdev->vlan_features |= NETIF_F_SG;
1141
Auke Kok9d5c8242008-01-24 02:22:38 -08001142 if (pci_using_dac)
1143 netdev->features |= NETIF_F_HIGHDMA;
1144
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07001145#ifdef CONFIG_NETDEVICES_MULTIQUEUE
1146 netdev->features |= NETIF_F_MULTI_QUEUE;
1147#endif
1148
Auke Kok9d5c8242008-01-24 02:22:38 -08001149 netdev->features |= NETIF_F_LLTX;
1150 adapter->en_mng_pt = igb_enable_mng_pass_thru(&adapter->hw);
1151
1152 /* before reading the NVM, reset the controller to put the device in a
1153 * known good starting state */
1154 hw->mac.ops.reset_hw(hw);
1155
1156 /* make sure the NVM is good */
1157 if (igb_validate_nvm_checksum(hw) < 0) {
1158 dev_err(&pdev->dev, "The NVM Checksum Is Not Valid\n");
1159 err = -EIO;
1160 goto err_eeprom;
1161 }
1162
1163 /* copy the MAC address out of the NVM */
1164 if (hw->mac.ops.read_mac_addr(hw))
1165 dev_err(&pdev->dev, "NVM Read Error\n");
1166
1167 memcpy(netdev->dev_addr, hw->mac.addr, netdev->addr_len);
1168 memcpy(netdev->perm_addr, hw->mac.addr, netdev->addr_len);
1169
1170 if (!is_valid_ether_addr(netdev->perm_addr)) {
1171 dev_err(&pdev->dev, "Invalid MAC Address\n");
1172 err = -EIO;
1173 goto err_eeprom;
1174 }
1175
1176 init_timer(&adapter->watchdog_timer);
1177 adapter->watchdog_timer.function = &igb_watchdog;
1178 adapter->watchdog_timer.data = (unsigned long) adapter;
1179
1180 init_timer(&adapter->phy_info_timer);
1181 adapter->phy_info_timer.function = &igb_update_phy_info;
1182 adapter->phy_info_timer.data = (unsigned long) adapter;
1183
1184 INIT_WORK(&adapter->reset_task, igb_reset_task);
1185 INIT_WORK(&adapter->watchdog_task, igb_watchdog_task);
1186
1187 /* Initialize link & ring properties that are user-changeable */
1188 adapter->tx_ring->count = 256;
1189 for (i = 0; i < adapter->num_tx_queues; i++)
1190 adapter->tx_ring[i].count = adapter->tx_ring->count;
1191 adapter->rx_ring->count = 256;
1192 for (i = 0; i < adapter->num_rx_queues; i++)
1193 adapter->rx_ring[i].count = adapter->rx_ring->count;
1194
1195 adapter->fc_autoneg = true;
1196 hw->mac.autoneg = true;
1197 hw->phy.autoneg_advertised = 0x2f;
1198
1199 hw->fc.original_type = e1000_fc_default;
1200 hw->fc.type = e1000_fc_default;
1201
1202 adapter->itr_setting = 3;
1203 adapter->itr = IGB_START_ITR;
1204
1205 igb_validate_mdi_setting(hw);
1206
1207 adapter->rx_csum = 1;
1208
1209 /* Initial Wake on LAN setting If APM wake is enabled in the EEPROM,
1210 * enable the ACPI Magic Packet filter
1211 */
1212
1213 if (hw->bus.func == 0 ||
1214 hw->device_id == E1000_DEV_ID_82575EB_COPPER)
1215 hw->nvm.ops.read_nvm(hw, NVM_INIT_CONTROL3_PORT_A, 1,
1216 &eeprom_data);
1217
1218 if (eeprom_data & eeprom_apme_mask)
1219 adapter->eeprom_wol |= E1000_WUFC_MAG;
1220
1221 /* now that we have the eeprom settings, apply the special cases where
1222 * the eeprom may be wrong or the board simply won't support wake on
1223 * lan on a particular port */
1224 switch (pdev->device) {
1225 case E1000_DEV_ID_82575GB_QUAD_COPPER:
1226 adapter->eeprom_wol = 0;
1227 break;
1228 case E1000_DEV_ID_82575EB_FIBER_SERDES:
Alexander Duyck2d064c02008-07-08 15:10:12 -07001229 case E1000_DEV_ID_82576_FIBER:
1230 case E1000_DEV_ID_82576_SERDES:
Auke Kok9d5c8242008-01-24 02:22:38 -08001231 /* Wake events only supported on port A for dual fiber
1232 * regardless of eeprom setting */
1233 if (rd32(E1000_STATUS) & E1000_STATUS_FUNC_1)
1234 adapter->eeprom_wol = 0;
1235 break;
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07001236 case E1000_DEV_ID_82576_QUAD_COPPER:
1237 /* if quad port adapter, disable WoL on all but port A */
1238 if (global_quad_port_a != 0)
1239 adapter->eeprom_wol = 0;
1240 else
1241 adapter->flags |= IGB_FLAG_QUAD_PORT_A;
1242 /* Reset for multiple quad port adapters */
1243 if (++global_quad_port_a == 4)
1244 global_quad_port_a = 0;
1245 break;
Auke Kok9d5c8242008-01-24 02:22:38 -08001246 }
1247
1248 /* initialize the wol settings based on the eeprom settings */
1249 adapter->wol = adapter->eeprom_wol;
1250
1251 /* reset the hardware with the new settings */
1252 igb_reset(adapter);
1253
1254 /* let the f/w know that the h/w is now under the control of the
1255 * driver. */
1256 igb_get_hw_control(adapter);
1257
1258 /* tell the stack to leave us alone until igb_open() is called */
1259 netif_carrier_off(netdev);
1260 netif_stop_queue(netdev);
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07001261#ifdef CONFIG_NETDEVICES_MULTIQUEUE
1262 for (i = 0; i < adapter->num_tx_queues; i++)
1263 netif_stop_subqueue(netdev, i);
1264#endif
Auke Kok9d5c8242008-01-24 02:22:38 -08001265
1266 strcpy(netdev->name, "eth%d");
1267 err = register_netdev(netdev);
1268 if (err)
1269 goto err_register;
1270
Jeb Cramerfe4506b2008-07-08 15:07:55 -07001271#ifdef CONFIG_DCA
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07001272 if ((adapter->flags & IGB_FLAG_HAS_DCA) &&
1273 (dca_add_requester(&pdev->dev) == 0)) {
1274 adapter->flags |= IGB_FLAG_DCA_ENABLED;
Jeb Cramerfe4506b2008-07-08 15:07:55 -07001275 dev_info(&pdev->dev, "DCA enabled\n");
1276 /* Always use CB2 mode, difference is masked
1277 * in the CB driver. */
1278 wr32(E1000_DCA_CTRL, 2);
1279 igb_setup_dca(adapter);
1280 }
1281#endif
1282
Auke Kok9d5c8242008-01-24 02:22:38 -08001283 dev_info(&pdev->dev, "Intel(R) Gigabit Ethernet Network Connection\n");
1284 /* print bus type/speed/width info */
1285 dev_info(&pdev->dev,
1286 "%s: (PCIe:%s:%s) %02x:%02x:%02x:%02x:%02x:%02x\n",
1287 netdev->name,
1288 ((hw->bus.speed == e1000_bus_speed_2500)
1289 ? "2.5Gb/s" : "unknown"),
1290 ((hw->bus.width == e1000_bus_width_pcie_x4)
1291 ? "Width x4" : (hw->bus.width == e1000_bus_width_pcie_x1)
1292 ? "Width x1" : "unknown"),
1293 netdev->dev_addr[0], netdev->dev_addr[1], netdev->dev_addr[2],
1294 netdev->dev_addr[3], netdev->dev_addr[4], netdev->dev_addr[5]);
1295
1296 igb_read_part_num(hw, &part_num);
1297 dev_info(&pdev->dev, "%s: PBA No: %06x-%03x\n", netdev->name,
1298 (part_num >> 8), (part_num & 0xff));
1299
1300 dev_info(&pdev->dev,
1301 "Using %s interrupts. %d rx queue(s), %d tx queue(s)\n",
1302 adapter->msix_entries ? "MSI-X" :
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07001303 (adapter->flags & IGB_FLAG_HAS_MSI) ? "MSI" : "legacy",
Auke Kok9d5c8242008-01-24 02:22:38 -08001304 adapter->num_rx_queues, adapter->num_tx_queues);
1305
Auke Kok9d5c8242008-01-24 02:22:38 -08001306 return 0;
1307
1308err_register:
1309 igb_release_hw_control(adapter);
1310err_eeprom:
1311 if (!igb_check_reset_block(hw))
1312 hw->phy.ops.reset_phy(hw);
1313
1314 if (hw->flash_address)
1315 iounmap(hw->flash_address);
1316
1317 igb_remove_device(hw);
1318 kfree(adapter->tx_ring);
1319 kfree(adapter->rx_ring);
1320err_sw_init:
1321err_hw_init:
1322 iounmap(hw->hw_addr);
1323err_ioremap:
1324 free_netdev(netdev);
1325err_alloc_etherdev:
Taku Izumi42bfd33a2008-06-20 12:10:30 +09001326 pci_release_selected_regions(pdev, bars);
Auke Kok9d5c8242008-01-24 02:22:38 -08001327err_pci_reg:
1328err_dma:
1329 pci_disable_device(pdev);
1330 return err;
1331}
1332
1333/**
1334 * igb_remove - Device Removal Routine
1335 * @pdev: PCI device information struct
1336 *
1337 * igb_remove is called by the PCI subsystem to alert the driver
1338 * that it should release a PCI device. The could be caused by a
1339 * Hot-Plug event, or because the driver is going to be removed from
1340 * memory.
1341 **/
1342static void __devexit igb_remove(struct pci_dev *pdev)
1343{
1344 struct net_device *netdev = pci_get_drvdata(pdev);
1345 struct igb_adapter *adapter = netdev_priv(netdev);
Jeb Cramerfe4506b2008-07-08 15:07:55 -07001346 struct e1000_hw *hw = &adapter->hw;
Auke Kok9d5c8242008-01-24 02:22:38 -08001347
1348 /* flush_scheduled work may reschedule our watchdog task, so
1349 * explicitly disable watchdog tasks from being rescheduled */
1350 set_bit(__IGB_DOWN, &adapter->state);
1351 del_timer_sync(&adapter->watchdog_timer);
1352 del_timer_sync(&adapter->phy_info_timer);
1353
1354 flush_scheduled_work();
1355
Jeb Cramerfe4506b2008-07-08 15:07:55 -07001356#ifdef CONFIG_DCA
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07001357 if (adapter->flags & IGB_FLAG_DCA_ENABLED) {
Jeb Cramerfe4506b2008-07-08 15:07:55 -07001358 dev_info(&pdev->dev, "DCA disabled\n");
1359 dca_remove_requester(&pdev->dev);
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07001360 adapter->flags &= ~IGB_FLAG_DCA_ENABLED;
Jeb Cramerfe4506b2008-07-08 15:07:55 -07001361 wr32(E1000_DCA_CTRL, 1);
1362 }
1363#endif
1364
Auke Kok9d5c8242008-01-24 02:22:38 -08001365 /* Release control of h/w to f/w. If f/w is AMT enabled, this
1366 * would have already happened in close and is redundant. */
1367 igb_release_hw_control(adapter);
1368
1369 unregister_netdev(netdev);
1370
1371 if (!igb_check_reset_block(&adapter->hw))
1372 adapter->hw.phy.ops.reset_phy(&adapter->hw);
1373
1374 igb_remove_device(&adapter->hw);
1375 igb_reset_interrupt_capability(adapter);
1376
1377 kfree(adapter->tx_ring);
1378 kfree(adapter->rx_ring);
1379
1380 iounmap(adapter->hw.hw_addr);
1381 if (adapter->hw.flash_address)
1382 iounmap(adapter->hw.flash_address);
Taku Izumi42bfd33a2008-06-20 12:10:30 +09001383 pci_release_selected_regions(pdev, adapter->bars);
Auke Kok9d5c8242008-01-24 02:22:38 -08001384
1385 free_netdev(netdev);
1386
1387 pci_disable_device(pdev);
1388}
1389
1390/**
1391 * igb_sw_init - Initialize general software structures (struct igb_adapter)
1392 * @adapter: board private structure to initialize
1393 *
1394 * igb_sw_init initializes the Adapter private data structure.
1395 * Fields are initialized based on PCI device information and
1396 * OS network device settings (MTU size).
1397 **/
1398static int __devinit igb_sw_init(struct igb_adapter *adapter)
1399{
1400 struct e1000_hw *hw = &adapter->hw;
1401 struct net_device *netdev = adapter->netdev;
1402 struct pci_dev *pdev = adapter->pdev;
1403
1404 pci_read_config_word(pdev, PCI_COMMAND, &hw->bus.pci_cmd_word);
1405
1406 adapter->rx_buffer_len = MAXIMUM_ETHERNET_VLAN_SIZE;
1407 adapter->rx_ps_hdr_size = 0; /* disable packet split */
1408 adapter->max_frame_size = netdev->mtu + ETH_HLEN + ETH_FCS_LEN;
1409 adapter->min_frame_size = ETH_ZLEN + ETH_FCS_LEN;
1410
1411 /* Number of supported queues. */
1412 /* Having more queues than CPUs doesn't make sense. */
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07001413 adapter->num_rx_queues = min((u32)IGB_MAX_RX_QUEUES, (u32)num_online_cpus());
1414#ifdef CONFIG_NETDEVICES_MULTIQUEUE
1415 adapter->num_tx_queues = min(IGB_MAX_TX_QUEUES, num_online_cpus());
1416#else
Auke Kok9d5c8242008-01-24 02:22:38 -08001417 adapter->num_tx_queues = 1;
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07001418#endif /* CONFIG_NET_MULTI_QUEUE_DEVICE */
Auke Kok9d5c8242008-01-24 02:22:38 -08001419
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07001420 /* This call may decrease the number of queues depending on
1421 * interrupt mode. */
Auke Kok9d5c8242008-01-24 02:22:38 -08001422 igb_set_interrupt_capability(adapter);
1423
1424 if (igb_alloc_queues(adapter)) {
1425 dev_err(&pdev->dev, "Unable to allocate memory for queues\n");
1426 return -ENOMEM;
1427 }
1428
1429 /* Explicitly disable IRQ since the NIC can be in any state. */
1430 igb_irq_disable(adapter);
1431
1432 set_bit(__IGB_DOWN, &adapter->state);
1433 return 0;
1434}
1435
1436/**
1437 * igb_open - Called when a network interface is made active
1438 * @netdev: network interface device structure
1439 *
1440 * Returns 0 on success, negative value on failure
1441 *
1442 * The open entry point is called when a network interface is made
1443 * active by the system (IFF_UP). At this point all resources needed
1444 * for transmit and receive operations are allocated, the interrupt
1445 * handler is registered with the OS, the watchdog timer is started,
1446 * and the stack is notified that the interface is ready.
1447 **/
1448static int igb_open(struct net_device *netdev)
1449{
1450 struct igb_adapter *adapter = netdev_priv(netdev);
1451 struct e1000_hw *hw = &adapter->hw;
1452 int err;
1453 int i;
1454
1455 /* disallow open during test */
1456 if (test_bit(__IGB_TESTING, &adapter->state))
1457 return -EBUSY;
1458
1459 /* allocate transmit descriptors */
1460 err = igb_setup_all_tx_resources(adapter);
1461 if (err)
1462 goto err_setup_tx;
1463
1464 /* allocate receive descriptors */
1465 err = igb_setup_all_rx_resources(adapter);
1466 if (err)
1467 goto err_setup_rx;
1468
1469 /* e1000_power_up_phy(adapter); */
1470
1471 adapter->mng_vlan_id = IGB_MNG_VLAN_NONE;
1472 if ((adapter->hw.mng_cookie.status &
1473 E1000_MNG_DHCP_COOKIE_STATUS_VLAN))
1474 igb_update_mng_vlan(adapter);
1475
1476 /* before we allocate an interrupt, we must be ready to handle it.
1477 * Setting DEBUG_SHIRQ in the kernel makes it fire an interrupt
1478 * as soon as we call pci_request_irq, so we have to setup our
1479 * clean_rx handler before we do so. */
1480 igb_configure(adapter);
1481
1482 err = igb_request_irq(adapter);
1483 if (err)
1484 goto err_req_irq;
1485
1486 /* From here on the code is the same as igb_up() */
1487 clear_bit(__IGB_DOWN, &adapter->state);
1488
PJ Waskiewicz844290e2008-06-27 11:00:39 -07001489 for (i = 0; i < adapter->num_rx_queues; i++)
1490 napi_enable(&adapter->rx_ring[i].napi);
Auke Kok9d5c8242008-01-24 02:22:38 -08001491
1492 /* Clear any pending interrupts. */
1493 rd32(E1000_ICR);
PJ Waskiewicz844290e2008-06-27 11:00:39 -07001494
1495 igb_irq_enable(adapter);
1496
Auke Kok9d5c8242008-01-24 02:22:38 -08001497 /* Fire a link status change interrupt to start the watchdog. */
1498 wr32(E1000_ICS, E1000_ICS_LSC);
1499
1500 return 0;
1501
1502err_req_irq:
1503 igb_release_hw_control(adapter);
1504 /* e1000_power_down_phy(adapter); */
1505 igb_free_all_rx_resources(adapter);
1506err_setup_rx:
1507 igb_free_all_tx_resources(adapter);
1508err_setup_tx:
1509 igb_reset(adapter);
1510
1511 return err;
1512}
1513
1514/**
1515 * igb_close - Disables a network interface
1516 * @netdev: network interface device structure
1517 *
1518 * Returns 0, this is not allowed to fail
1519 *
1520 * The close entry point is called when an interface is de-activated
1521 * by the OS. The hardware is still under the driver's control, but
1522 * needs to be disabled. A global MAC reset is issued to stop the
1523 * hardware, and all transmit and receive resources are freed.
1524 **/
1525static int igb_close(struct net_device *netdev)
1526{
1527 struct igb_adapter *adapter = netdev_priv(netdev);
1528
1529 WARN_ON(test_bit(__IGB_RESETTING, &adapter->state));
1530 igb_down(adapter);
1531
1532 igb_free_irq(adapter);
1533
1534 igb_free_all_tx_resources(adapter);
1535 igb_free_all_rx_resources(adapter);
1536
1537 /* kill manageability vlan ID if supported, but not if a vlan with
1538 * the same ID is registered on the host OS (let 8021q kill it) */
1539 if ((adapter->hw.mng_cookie.status &
1540 E1000_MNG_DHCP_COOKIE_STATUS_VLAN) &&
1541 !(adapter->vlgrp &&
1542 vlan_group_get_device(adapter->vlgrp, adapter->mng_vlan_id)))
1543 igb_vlan_rx_kill_vid(netdev, adapter->mng_vlan_id);
1544
1545 return 0;
1546}
1547
1548/**
1549 * igb_setup_tx_resources - allocate Tx resources (Descriptors)
1550 * @adapter: board private structure
1551 * @tx_ring: tx descriptor ring (for a specific queue) to setup
1552 *
1553 * Return 0 on success, negative on failure
1554 **/
1555
1556int igb_setup_tx_resources(struct igb_adapter *adapter,
1557 struct igb_ring *tx_ring)
1558{
1559 struct pci_dev *pdev = adapter->pdev;
1560 int size;
1561
1562 size = sizeof(struct igb_buffer) * tx_ring->count;
1563 tx_ring->buffer_info = vmalloc(size);
1564 if (!tx_ring->buffer_info)
1565 goto err;
1566 memset(tx_ring->buffer_info, 0, size);
1567
1568 /* round up to nearest 4K */
1569 tx_ring->size = tx_ring->count * sizeof(struct e1000_tx_desc)
1570 + sizeof(u32);
1571 tx_ring->size = ALIGN(tx_ring->size, 4096);
1572
1573 tx_ring->desc = pci_alloc_consistent(pdev, tx_ring->size,
1574 &tx_ring->dma);
1575
1576 if (!tx_ring->desc)
1577 goto err;
1578
1579 tx_ring->adapter = adapter;
1580 tx_ring->next_to_use = 0;
1581 tx_ring->next_to_clean = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08001582 return 0;
1583
1584err:
1585 vfree(tx_ring->buffer_info);
1586 dev_err(&adapter->pdev->dev,
1587 "Unable to allocate memory for the transmit descriptor ring\n");
1588 return -ENOMEM;
1589}
1590
1591/**
1592 * igb_setup_all_tx_resources - wrapper to allocate Tx resources
1593 * (Descriptors) for all queues
1594 * @adapter: board private structure
1595 *
1596 * Return 0 on success, negative on failure
1597 **/
1598static int igb_setup_all_tx_resources(struct igb_adapter *adapter)
1599{
1600 int i, err = 0;
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07001601#ifdef CONFIG_NETDEVICES_MULTIQUEUE
1602 int r_idx;
1603#endif
Auke Kok9d5c8242008-01-24 02:22:38 -08001604
1605 for (i = 0; i < adapter->num_tx_queues; i++) {
1606 err = igb_setup_tx_resources(adapter, &adapter->tx_ring[i]);
1607 if (err) {
1608 dev_err(&adapter->pdev->dev,
1609 "Allocation for Tx Queue %u failed\n", i);
1610 for (i--; i >= 0; i--)
Mitch Williams3b644cf2008-06-27 10:59:48 -07001611 igb_free_tx_resources(&adapter->tx_ring[i]);
Auke Kok9d5c8242008-01-24 02:22:38 -08001612 break;
1613 }
1614 }
1615
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07001616#ifdef CONFIG_NETDEVICES_MULTIQUEUE
1617 for (i = 0; i < IGB_MAX_TX_QUEUES; i++) {
1618 r_idx = i % adapter->num_tx_queues;
1619 adapter->multi_tx_table[i] = &adapter->tx_ring[r_idx];
1620 }
1621#endif
Auke Kok9d5c8242008-01-24 02:22:38 -08001622 return err;
1623}
1624
1625/**
1626 * igb_configure_tx - Configure transmit Unit after Reset
1627 * @adapter: board private structure
1628 *
1629 * Configure the Tx unit of the MAC after a reset.
1630 **/
1631static void igb_configure_tx(struct igb_adapter *adapter)
1632{
1633 u64 tdba, tdwba;
1634 struct e1000_hw *hw = &adapter->hw;
1635 u32 tctl;
1636 u32 txdctl, txctrl;
1637 int i;
1638
1639 for (i = 0; i < adapter->num_tx_queues; i++) {
1640 struct igb_ring *ring = &(adapter->tx_ring[i]);
1641
1642 wr32(E1000_TDLEN(i),
1643 ring->count * sizeof(struct e1000_tx_desc));
1644 tdba = ring->dma;
1645 wr32(E1000_TDBAL(i),
1646 tdba & 0x00000000ffffffffULL);
1647 wr32(E1000_TDBAH(i), tdba >> 32);
1648
1649 tdwba = ring->dma + ring->count * sizeof(struct e1000_tx_desc);
1650 tdwba |= 1; /* enable head wb */
1651 wr32(E1000_TDWBAL(i),
1652 tdwba & 0x00000000ffffffffULL);
1653 wr32(E1000_TDWBAH(i), tdwba >> 32);
1654
1655 ring->head = E1000_TDH(i);
1656 ring->tail = E1000_TDT(i);
1657 writel(0, hw->hw_addr + ring->tail);
1658 writel(0, hw->hw_addr + ring->head);
1659 txdctl = rd32(E1000_TXDCTL(i));
1660 txdctl |= E1000_TXDCTL_QUEUE_ENABLE;
1661 wr32(E1000_TXDCTL(i), txdctl);
1662
1663 /* Turn off Relaxed Ordering on head write-backs. The
1664 * writebacks MUST be delivered in order or it will
1665 * completely screw up our bookeeping.
1666 */
1667 txctrl = rd32(E1000_DCA_TXCTRL(i));
1668 txctrl &= ~E1000_DCA_TXCTRL_TX_WB_RO_EN;
1669 wr32(E1000_DCA_TXCTRL(i), txctrl);
1670 }
1671
1672
1673
1674 /* Use the default values for the Tx Inter Packet Gap (IPG) timer */
1675
1676 /* Program the Transmit Control Register */
1677
1678 tctl = rd32(E1000_TCTL);
1679 tctl &= ~E1000_TCTL_CT;
1680 tctl |= E1000_TCTL_PSP | E1000_TCTL_RTLC |
1681 (E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT);
1682
1683 igb_config_collision_dist(hw);
1684
1685 /* Setup Transmit Descriptor Settings for eop descriptor */
1686 adapter->txd_cmd = E1000_TXD_CMD_EOP | E1000_TXD_CMD_RS;
1687
1688 /* Enable transmits */
1689 tctl |= E1000_TCTL_EN;
1690
1691 wr32(E1000_TCTL, tctl);
1692}
1693
1694/**
1695 * igb_setup_rx_resources - allocate Rx resources (Descriptors)
1696 * @adapter: board private structure
1697 * @rx_ring: rx descriptor ring (for a specific queue) to setup
1698 *
1699 * Returns 0 on success, negative on failure
1700 **/
1701
1702int igb_setup_rx_resources(struct igb_adapter *adapter,
1703 struct igb_ring *rx_ring)
1704{
1705 struct pci_dev *pdev = adapter->pdev;
1706 int size, desc_len;
1707
1708 size = sizeof(struct igb_buffer) * rx_ring->count;
1709 rx_ring->buffer_info = vmalloc(size);
1710 if (!rx_ring->buffer_info)
1711 goto err;
1712 memset(rx_ring->buffer_info, 0, size);
1713
1714 desc_len = sizeof(union e1000_adv_rx_desc);
1715
1716 /* Round up to nearest 4K */
1717 rx_ring->size = rx_ring->count * desc_len;
1718 rx_ring->size = ALIGN(rx_ring->size, 4096);
1719
1720 rx_ring->desc = pci_alloc_consistent(pdev, rx_ring->size,
1721 &rx_ring->dma);
1722
1723 if (!rx_ring->desc)
1724 goto err;
1725
1726 rx_ring->next_to_clean = 0;
1727 rx_ring->next_to_use = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08001728
1729 rx_ring->adapter = adapter;
Auke Kok9d5c8242008-01-24 02:22:38 -08001730
1731 return 0;
1732
1733err:
1734 vfree(rx_ring->buffer_info);
1735 dev_err(&adapter->pdev->dev, "Unable to allocate memory for "
1736 "the receive descriptor ring\n");
1737 return -ENOMEM;
1738}
1739
1740/**
1741 * igb_setup_all_rx_resources - wrapper to allocate Rx resources
1742 * (Descriptors) for all queues
1743 * @adapter: board private structure
1744 *
1745 * Return 0 on success, negative on failure
1746 **/
1747static int igb_setup_all_rx_resources(struct igb_adapter *adapter)
1748{
1749 int i, err = 0;
1750
1751 for (i = 0; i < adapter->num_rx_queues; i++) {
1752 err = igb_setup_rx_resources(adapter, &adapter->rx_ring[i]);
1753 if (err) {
1754 dev_err(&adapter->pdev->dev,
1755 "Allocation for Rx Queue %u failed\n", i);
1756 for (i--; i >= 0; i--)
Mitch Williams3b644cf2008-06-27 10:59:48 -07001757 igb_free_rx_resources(&adapter->rx_ring[i]);
Auke Kok9d5c8242008-01-24 02:22:38 -08001758 break;
1759 }
1760 }
1761
1762 return err;
1763}
1764
1765/**
1766 * igb_setup_rctl - configure the receive control registers
1767 * @adapter: Board private structure
1768 **/
1769static void igb_setup_rctl(struct igb_adapter *adapter)
1770{
1771 struct e1000_hw *hw = &adapter->hw;
1772 u32 rctl;
1773 u32 srrctl = 0;
1774 int i;
1775
1776 rctl = rd32(E1000_RCTL);
1777
1778 rctl &= ~(3 << E1000_RCTL_MO_SHIFT);
1779
1780 rctl |= E1000_RCTL_EN | E1000_RCTL_BAM |
1781 E1000_RCTL_LBM_NO | E1000_RCTL_RDMTS_HALF |
1782 (adapter->hw.mac.mc_filter_type << E1000_RCTL_MO_SHIFT);
1783
Auke Kok87cb7e82008-07-08 15:08:29 -07001784 /*
1785 * enable stripping of CRC. It's unlikely this will break BMC
1786 * redirection as it did with e1000. Newer features require
1787 * that the HW strips the CRC.
Auke Kok9d5c8242008-01-24 02:22:38 -08001788 */
Auke Kok87cb7e82008-07-08 15:08:29 -07001789 rctl |= E1000_RCTL_SECRC;
Auke Kok9d5c8242008-01-24 02:22:38 -08001790
1791 rctl &= ~E1000_RCTL_SBP;
1792
1793 if (adapter->netdev->mtu <= ETH_DATA_LEN)
1794 rctl &= ~E1000_RCTL_LPE;
1795 else
1796 rctl |= E1000_RCTL_LPE;
1797 if (adapter->rx_buffer_len <= IGB_RXBUFFER_2048) {
1798 /* Setup buffer sizes */
1799 rctl &= ~E1000_RCTL_SZ_4096;
1800 rctl |= E1000_RCTL_BSEX;
1801 switch (adapter->rx_buffer_len) {
1802 case IGB_RXBUFFER_256:
1803 rctl |= E1000_RCTL_SZ_256;
1804 rctl &= ~E1000_RCTL_BSEX;
1805 break;
1806 case IGB_RXBUFFER_512:
1807 rctl |= E1000_RCTL_SZ_512;
1808 rctl &= ~E1000_RCTL_BSEX;
1809 break;
1810 case IGB_RXBUFFER_1024:
1811 rctl |= E1000_RCTL_SZ_1024;
1812 rctl &= ~E1000_RCTL_BSEX;
1813 break;
1814 case IGB_RXBUFFER_2048:
1815 default:
1816 rctl |= E1000_RCTL_SZ_2048;
1817 rctl &= ~E1000_RCTL_BSEX;
1818 break;
Auke Kok9d5c8242008-01-24 02:22:38 -08001819 }
1820 } else {
1821 rctl &= ~E1000_RCTL_BSEX;
1822 srrctl = adapter->rx_buffer_len >> E1000_SRRCTL_BSIZEPKT_SHIFT;
1823 }
1824
1825 /* 82575 and greater support packet-split where the protocol
1826 * header is placed in skb->data and the packet data is
1827 * placed in pages hanging off of skb_shinfo(skb)->nr_frags.
1828 * In the case of a non-split, skb->data is linearly filled,
1829 * followed by the page buffers. Therefore, skb->data is
1830 * sized to hold the largest protocol header.
1831 */
1832 /* allocations using alloc_page take too long for regular MTU
1833 * so only enable packet split for jumbo frames */
1834 if (rctl & E1000_RCTL_LPE) {
1835 adapter->rx_ps_hdr_size = IGB_RXBUFFER_128;
Alexander Duyckbf36c1a2008-07-08 15:11:40 -07001836 srrctl |= adapter->rx_ps_hdr_size <<
Auke Kok9d5c8242008-01-24 02:22:38 -08001837 E1000_SRRCTL_BSIZEHDRSIZE_SHIFT;
Auke Kok9d5c8242008-01-24 02:22:38 -08001838 srrctl |= E1000_SRRCTL_DESCTYPE_HDR_SPLIT_ALWAYS;
1839 } else {
1840 adapter->rx_ps_hdr_size = 0;
1841 srrctl |= E1000_SRRCTL_DESCTYPE_ADV_ONEBUF;
1842 }
1843
1844 for (i = 0; i < adapter->num_rx_queues; i++)
1845 wr32(E1000_SRRCTL(i), srrctl);
1846
1847 wr32(E1000_RCTL, rctl);
1848}
1849
1850/**
1851 * igb_configure_rx - Configure receive Unit after Reset
1852 * @adapter: board private structure
1853 *
1854 * Configure the Rx unit of the MAC after a reset.
1855 **/
1856static void igb_configure_rx(struct igb_adapter *adapter)
1857{
1858 u64 rdba;
1859 struct e1000_hw *hw = &adapter->hw;
1860 u32 rctl, rxcsum;
1861 u32 rxdctl;
1862 int i;
1863
1864 /* disable receives while setting up the descriptors */
1865 rctl = rd32(E1000_RCTL);
1866 wr32(E1000_RCTL, rctl & ~E1000_RCTL_EN);
1867 wrfl();
1868 mdelay(10);
1869
1870 if (adapter->itr_setting > 3)
1871 wr32(E1000_ITR,
1872 1000000000 / (adapter->itr * 256));
1873
1874 /* Setup the HW Rx Head and Tail Descriptor Pointers and
1875 * the Base and Length of the Rx Descriptor Ring */
1876 for (i = 0; i < adapter->num_rx_queues; i++) {
1877 struct igb_ring *ring = &(adapter->rx_ring[i]);
1878 rdba = ring->dma;
1879 wr32(E1000_RDBAL(i),
1880 rdba & 0x00000000ffffffffULL);
1881 wr32(E1000_RDBAH(i), rdba >> 32);
1882 wr32(E1000_RDLEN(i),
1883 ring->count * sizeof(union e1000_adv_rx_desc));
1884
1885 ring->head = E1000_RDH(i);
1886 ring->tail = E1000_RDT(i);
1887 writel(0, hw->hw_addr + ring->tail);
1888 writel(0, hw->hw_addr + ring->head);
1889
1890 rxdctl = rd32(E1000_RXDCTL(i));
1891 rxdctl |= E1000_RXDCTL_QUEUE_ENABLE;
1892 rxdctl &= 0xFFF00000;
1893 rxdctl |= IGB_RX_PTHRESH;
1894 rxdctl |= IGB_RX_HTHRESH << 8;
1895 rxdctl |= IGB_RX_WTHRESH << 16;
1896 wr32(E1000_RXDCTL(i), rxdctl);
1897 }
1898
1899 if (adapter->num_rx_queues > 1) {
1900 u32 random[10];
1901 u32 mrqc;
1902 u32 j, shift;
1903 union e1000_reta {
1904 u32 dword;
1905 u8 bytes[4];
1906 } reta;
1907
1908 get_random_bytes(&random[0], 40);
1909
Alexander Duyck2d064c02008-07-08 15:10:12 -07001910 if (hw->mac.type >= e1000_82576)
1911 shift = 0;
1912 else
1913 shift = 6;
Auke Kok9d5c8242008-01-24 02:22:38 -08001914 for (j = 0; j < (32 * 4); j++) {
1915 reta.bytes[j & 3] =
1916 (j % adapter->num_rx_queues) << shift;
1917 if ((j & 3) == 3)
1918 writel(reta.dword,
1919 hw->hw_addr + E1000_RETA(0) + (j & ~3));
1920 }
1921 mrqc = E1000_MRQC_ENABLE_RSS_4Q;
1922
1923 /* Fill out hash function seeds */
1924 for (j = 0; j < 10; j++)
1925 array_wr32(E1000_RSSRK(0), j, random[j]);
1926
1927 mrqc |= (E1000_MRQC_RSS_FIELD_IPV4 |
1928 E1000_MRQC_RSS_FIELD_IPV4_TCP);
1929 mrqc |= (E1000_MRQC_RSS_FIELD_IPV6 |
1930 E1000_MRQC_RSS_FIELD_IPV6_TCP);
1931 mrqc |= (E1000_MRQC_RSS_FIELD_IPV4_UDP |
1932 E1000_MRQC_RSS_FIELD_IPV6_UDP);
1933 mrqc |= (E1000_MRQC_RSS_FIELD_IPV6_UDP_EX |
1934 E1000_MRQC_RSS_FIELD_IPV6_TCP_EX);
1935
1936
1937 wr32(E1000_MRQC, mrqc);
1938
1939 /* Multiqueue and raw packet checksumming are mutually
1940 * exclusive. Note that this not the same as TCP/IP
1941 * checksumming, which works fine. */
1942 rxcsum = rd32(E1000_RXCSUM);
1943 rxcsum |= E1000_RXCSUM_PCSD;
1944 wr32(E1000_RXCSUM, rxcsum);
1945 } else {
1946 /* Enable Receive Checksum Offload for TCP and UDP */
1947 rxcsum = rd32(E1000_RXCSUM);
1948 if (adapter->rx_csum) {
1949 rxcsum |= E1000_RXCSUM_TUOFL;
1950
1951 /* Enable IPv4 payload checksum for UDP fragments
1952 * Must be used in conjunction with packet-split. */
1953 if (adapter->rx_ps_hdr_size)
1954 rxcsum |= E1000_RXCSUM_IPPCSE;
1955 } else {
1956 rxcsum &= ~E1000_RXCSUM_TUOFL;
1957 /* don't need to clear IPPCSE as it defaults to 0 */
1958 }
1959 wr32(E1000_RXCSUM, rxcsum);
1960 }
1961
1962 if (adapter->vlgrp)
1963 wr32(E1000_RLPML,
1964 adapter->max_frame_size + VLAN_TAG_SIZE);
1965 else
1966 wr32(E1000_RLPML, adapter->max_frame_size);
1967
1968 /* Enable Receives */
1969 wr32(E1000_RCTL, rctl);
1970}
1971
1972/**
1973 * igb_free_tx_resources - Free Tx Resources per Queue
1974 * @adapter: board private structure
1975 * @tx_ring: Tx descriptor ring for a specific queue
1976 *
1977 * Free all transmit software resources
1978 **/
Mitch Williams3b644cf2008-06-27 10:59:48 -07001979static void igb_free_tx_resources(struct igb_ring *tx_ring)
Auke Kok9d5c8242008-01-24 02:22:38 -08001980{
Mitch Williams3b644cf2008-06-27 10:59:48 -07001981 struct pci_dev *pdev = tx_ring->adapter->pdev;
Auke Kok9d5c8242008-01-24 02:22:38 -08001982
Mitch Williams3b644cf2008-06-27 10:59:48 -07001983 igb_clean_tx_ring(tx_ring);
Auke Kok9d5c8242008-01-24 02:22:38 -08001984
1985 vfree(tx_ring->buffer_info);
1986 tx_ring->buffer_info = NULL;
1987
1988 pci_free_consistent(pdev, tx_ring->size, tx_ring->desc, tx_ring->dma);
1989
1990 tx_ring->desc = NULL;
1991}
1992
1993/**
1994 * igb_free_all_tx_resources - Free Tx Resources for All Queues
1995 * @adapter: board private structure
1996 *
1997 * Free all transmit software resources
1998 **/
1999static void igb_free_all_tx_resources(struct igb_adapter *adapter)
2000{
2001 int i;
2002
2003 for (i = 0; i < adapter->num_tx_queues; i++)
Mitch Williams3b644cf2008-06-27 10:59:48 -07002004 igb_free_tx_resources(&adapter->tx_ring[i]);
Auke Kok9d5c8242008-01-24 02:22:38 -08002005}
2006
2007static void igb_unmap_and_free_tx_resource(struct igb_adapter *adapter,
2008 struct igb_buffer *buffer_info)
2009{
2010 if (buffer_info->dma) {
2011 pci_unmap_page(adapter->pdev,
2012 buffer_info->dma,
2013 buffer_info->length,
2014 PCI_DMA_TODEVICE);
2015 buffer_info->dma = 0;
2016 }
2017 if (buffer_info->skb) {
2018 dev_kfree_skb_any(buffer_info->skb);
2019 buffer_info->skb = NULL;
2020 }
2021 buffer_info->time_stamp = 0;
2022 /* buffer_info must be completely set up in the transmit path */
2023}
2024
2025/**
2026 * igb_clean_tx_ring - Free Tx Buffers
2027 * @adapter: board private structure
2028 * @tx_ring: ring to be cleaned
2029 **/
Mitch Williams3b644cf2008-06-27 10:59:48 -07002030static void igb_clean_tx_ring(struct igb_ring *tx_ring)
Auke Kok9d5c8242008-01-24 02:22:38 -08002031{
Mitch Williams3b644cf2008-06-27 10:59:48 -07002032 struct igb_adapter *adapter = tx_ring->adapter;
Auke Kok9d5c8242008-01-24 02:22:38 -08002033 struct igb_buffer *buffer_info;
2034 unsigned long size;
2035 unsigned int i;
2036
2037 if (!tx_ring->buffer_info)
2038 return;
2039 /* Free all the Tx ring sk_buffs */
2040
2041 for (i = 0; i < tx_ring->count; i++) {
2042 buffer_info = &tx_ring->buffer_info[i];
2043 igb_unmap_and_free_tx_resource(adapter, buffer_info);
2044 }
2045
2046 size = sizeof(struct igb_buffer) * tx_ring->count;
2047 memset(tx_ring->buffer_info, 0, size);
2048
2049 /* Zero out the descriptor ring */
2050
2051 memset(tx_ring->desc, 0, tx_ring->size);
2052
2053 tx_ring->next_to_use = 0;
2054 tx_ring->next_to_clean = 0;
2055
2056 writel(0, adapter->hw.hw_addr + tx_ring->head);
2057 writel(0, adapter->hw.hw_addr + tx_ring->tail);
2058}
2059
2060/**
2061 * igb_clean_all_tx_rings - Free Tx Buffers for all queues
2062 * @adapter: board private structure
2063 **/
2064static void igb_clean_all_tx_rings(struct igb_adapter *adapter)
2065{
2066 int i;
2067
2068 for (i = 0; i < adapter->num_tx_queues; i++)
Mitch Williams3b644cf2008-06-27 10:59:48 -07002069 igb_clean_tx_ring(&adapter->tx_ring[i]);
Auke Kok9d5c8242008-01-24 02:22:38 -08002070}
2071
2072/**
2073 * igb_free_rx_resources - Free Rx Resources
2074 * @adapter: board private structure
2075 * @rx_ring: ring to clean the resources from
2076 *
2077 * Free all receive software resources
2078 **/
Mitch Williams3b644cf2008-06-27 10:59:48 -07002079static void igb_free_rx_resources(struct igb_ring *rx_ring)
Auke Kok9d5c8242008-01-24 02:22:38 -08002080{
Mitch Williams3b644cf2008-06-27 10:59:48 -07002081 struct pci_dev *pdev = rx_ring->adapter->pdev;
Auke Kok9d5c8242008-01-24 02:22:38 -08002082
Mitch Williams3b644cf2008-06-27 10:59:48 -07002083 igb_clean_rx_ring(rx_ring);
Auke Kok9d5c8242008-01-24 02:22:38 -08002084
2085 vfree(rx_ring->buffer_info);
2086 rx_ring->buffer_info = NULL;
2087
2088 pci_free_consistent(pdev, rx_ring->size, rx_ring->desc, rx_ring->dma);
2089
2090 rx_ring->desc = NULL;
2091}
2092
2093/**
2094 * igb_free_all_rx_resources - Free Rx Resources for All Queues
2095 * @adapter: board private structure
2096 *
2097 * Free all receive software resources
2098 **/
2099static void igb_free_all_rx_resources(struct igb_adapter *adapter)
2100{
2101 int i;
2102
2103 for (i = 0; i < adapter->num_rx_queues; i++)
Mitch Williams3b644cf2008-06-27 10:59:48 -07002104 igb_free_rx_resources(&adapter->rx_ring[i]);
Auke Kok9d5c8242008-01-24 02:22:38 -08002105}
2106
2107/**
2108 * igb_clean_rx_ring - Free Rx Buffers per Queue
2109 * @adapter: board private structure
2110 * @rx_ring: ring to free buffers from
2111 **/
Mitch Williams3b644cf2008-06-27 10:59:48 -07002112static void igb_clean_rx_ring(struct igb_ring *rx_ring)
Auke Kok9d5c8242008-01-24 02:22:38 -08002113{
Mitch Williams3b644cf2008-06-27 10:59:48 -07002114 struct igb_adapter *adapter = rx_ring->adapter;
Auke Kok9d5c8242008-01-24 02:22:38 -08002115 struct igb_buffer *buffer_info;
2116 struct pci_dev *pdev = adapter->pdev;
2117 unsigned long size;
2118 unsigned int i;
2119
2120 if (!rx_ring->buffer_info)
2121 return;
2122 /* Free all the Rx ring sk_buffs */
2123 for (i = 0; i < rx_ring->count; i++) {
2124 buffer_info = &rx_ring->buffer_info[i];
2125 if (buffer_info->dma) {
2126 if (adapter->rx_ps_hdr_size)
2127 pci_unmap_single(pdev, buffer_info->dma,
2128 adapter->rx_ps_hdr_size,
2129 PCI_DMA_FROMDEVICE);
2130 else
2131 pci_unmap_single(pdev, buffer_info->dma,
2132 adapter->rx_buffer_len,
2133 PCI_DMA_FROMDEVICE);
2134 buffer_info->dma = 0;
2135 }
2136
2137 if (buffer_info->skb) {
2138 dev_kfree_skb(buffer_info->skb);
2139 buffer_info->skb = NULL;
2140 }
2141 if (buffer_info->page) {
Alexander Duyckbf36c1a2008-07-08 15:11:40 -07002142 if (buffer_info->page_dma)
2143 pci_unmap_page(pdev, buffer_info->page_dma,
2144 PAGE_SIZE / 2,
2145 PCI_DMA_FROMDEVICE);
Auke Kok9d5c8242008-01-24 02:22:38 -08002146 put_page(buffer_info->page);
2147 buffer_info->page = NULL;
2148 buffer_info->page_dma = 0;
Alexander Duyckbf36c1a2008-07-08 15:11:40 -07002149 buffer_info->page_offset = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08002150 }
2151 }
2152
Auke Kok9d5c8242008-01-24 02:22:38 -08002153 size = sizeof(struct igb_buffer) * rx_ring->count;
2154 memset(rx_ring->buffer_info, 0, size);
2155
2156 /* Zero out the descriptor ring */
2157 memset(rx_ring->desc, 0, rx_ring->size);
2158
2159 rx_ring->next_to_clean = 0;
2160 rx_ring->next_to_use = 0;
2161
2162 writel(0, adapter->hw.hw_addr + rx_ring->head);
2163 writel(0, adapter->hw.hw_addr + rx_ring->tail);
2164}
2165
2166/**
2167 * igb_clean_all_rx_rings - Free Rx Buffers for all queues
2168 * @adapter: board private structure
2169 **/
2170static void igb_clean_all_rx_rings(struct igb_adapter *adapter)
2171{
2172 int i;
2173
2174 for (i = 0; i < adapter->num_rx_queues; i++)
Mitch Williams3b644cf2008-06-27 10:59:48 -07002175 igb_clean_rx_ring(&adapter->rx_ring[i]);
Auke Kok9d5c8242008-01-24 02:22:38 -08002176}
2177
2178/**
2179 * igb_set_mac - Change the Ethernet Address of the NIC
2180 * @netdev: network interface device structure
2181 * @p: pointer to an address structure
2182 *
2183 * Returns 0 on success, negative on failure
2184 **/
2185static int igb_set_mac(struct net_device *netdev, void *p)
2186{
2187 struct igb_adapter *adapter = netdev_priv(netdev);
2188 struct sockaddr *addr = p;
2189
2190 if (!is_valid_ether_addr(addr->sa_data))
2191 return -EADDRNOTAVAIL;
2192
2193 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
2194 memcpy(adapter->hw.mac.addr, addr->sa_data, netdev->addr_len);
2195
2196 adapter->hw.mac.ops.rar_set(&adapter->hw, adapter->hw.mac.addr, 0);
2197
2198 return 0;
2199}
2200
2201/**
2202 * igb_set_multi - Multicast and Promiscuous mode set
2203 * @netdev: network interface device structure
2204 *
2205 * The set_multi entry point is called whenever the multicast address
2206 * list or the network interface flags are updated. This routine is
2207 * responsible for configuring the hardware for proper multicast,
2208 * promiscuous mode, and all-multi behavior.
2209 **/
2210static void igb_set_multi(struct net_device *netdev)
2211{
2212 struct igb_adapter *adapter = netdev_priv(netdev);
2213 struct e1000_hw *hw = &adapter->hw;
2214 struct e1000_mac_info *mac = &hw->mac;
2215 struct dev_mc_list *mc_ptr;
2216 u8 *mta_list;
2217 u32 rctl;
2218 int i;
2219
2220 /* Check for Promiscuous and All Multicast modes */
2221
2222 rctl = rd32(E1000_RCTL);
2223
2224 if (netdev->flags & IFF_PROMISC)
2225 rctl |= (E1000_RCTL_UPE | E1000_RCTL_MPE);
2226 else if (netdev->flags & IFF_ALLMULTI) {
2227 rctl |= E1000_RCTL_MPE;
2228 rctl &= ~E1000_RCTL_UPE;
2229 } else
2230 rctl &= ~(E1000_RCTL_UPE | E1000_RCTL_MPE);
2231
2232 wr32(E1000_RCTL, rctl);
2233
2234 if (!netdev->mc_count) {
2235 /* nothing to program, so clear mc list */
Alexander Duyck2d064c02008-07-08 15:10:12 -07002236 igb_update_mc_addr_list_82575(hw, NULL, 0, 1,
Auke Kok9d5c8242008-01-24 02:22:38 -08002237 mac->rar_entry_count);
2238 return;
2239 }
2240
2241 mta_list = kzalloc(netdev->mc_count * 6, GFP_ATOMIC);
2242 if (!mta_list)
2243 return;
2244
2245 /* The shared function expects a packed array of only addresses. */
2246 mc_ptr = netdev->mc_list;
2247
2248 for (i = 0; i < netdev->mc_count; i++) {
2249 if (!mc_ptr)
2250 break;
2251 memcpy(mta_list + (i*ETH_ALEN), mc_ptr->dmi_addr, ETH_ALEN);
2252 mc_ptr = mc_ptr->next;
2253 }
Alexander Duyck2d064c02008-07-08 15:10:12 -07002254 igb_update_mc_addr_list_82575(hw, mta_list, i, 1,
2255 mac->rar_entry_count);
Auke Kok9d5c8242008-01-24 02:22:38 -08002256 kfree(mta_list);
2257}
2258
2259/* Need to wait a few seconds after link up to get diagnostic information from
2260 * the phy */
2261static void igb_update_phy_info(unsigned long data)
2262{
2263 struct igb_adapter *adapter = (struct igb_adapter *) data;
Bill Hayes68707ac2008-02-19 10:24:41 -08002264 if (adapter->hw.phy.ops.get_phy_info)
2265 adapter->hw.phy.ops.get_phy_info(&adapter->hw);
Auke Kok9d5c8242008-01-24 02:22:38 -08002266}
2267
2268/**
2269 * igb_watchdog - Timer Call-back
2270 * @data: pointer to adapter cast into an unsigned long
2271 **/
2272static void igb_watchdog(unsigned long data)
2273{
2274 struct igb_adapter *adapter = (struct igb_adapter *)data;
2275 /* Do the rest outside of interrupt context */
2276 schedule_work(&adapter->watchdog_task);
2277}
2278
2279static void igb_watchdog_task(struct work_struct *work)
2280{
2281 struct igb_adapter *adapter = container_of(work,
2282 struct igb_adapter, watchdog_task);
2283 struct e1000_hw *hw = &adapter->hw;
2284
2285 struct net_device *netdev = adapter->netdev;
2286 struct igb_ring *tx_ring = adapter->tx_ring;
2287 struct e1000_mac_info *mac = &adapter->hw.mac;
2288 u32 link;
2289 s32 ret_val;
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07002290#ifdef CONFIG_NETDEVICES_MULTIQUEUE
2291 int i;
2292#endif
Auke Kok9d5c8242008-01-24 02:22:38 -08002293
2294 if ((netif_carrier_ok(netdev)) &&
2295 (rd32(E1000_STATUS) & E1000_STATUS_LU))
2296 goto link_up;
2297
2298 ret_val = hw->mac.ops.check_for_link(&adapter->hw);
2299 if ((ret_val == E1000_ERR_PHY) &&
2300 (hw->phy.type == e1000_phy_igp_3) &&
2301 (rd32(E1000_CTRL) &
2302 E1000_PHY_CTRL_GBE_DISABLE))
2303 dev_info(&adapter->pdev->dev,
2304 "Gigabit has been disabled, downgrading speed\n");
2305
2306 if ((hw->phy.media_type == e1000_media_type_internal_serdes) &&
2307 !(rd32(E1000_TXCW) & E1000_TXCW_ANE))
2308 link = mac->serdes_has_link;
2309 else
2310 link = rd32(E1000_STATUS) &
2311 E1000_STATUS_LU;
2312
2313 if (link) {
2314 if (!netif_carrier_ok(netdev)) {
2315 u32 ctrl;
2316 hw->mac.ops.get_speed_and_duplex(&adapter->hw,
2317 &adapter->link_speed,
2318 &adapter->link_duplex);
2319
2320 ctrl = rd32(E1000_CTRL);
2321 dev_info(&adapter->pdev->dev,
2322 "NIC Link is Up %d Mbps %s, "
2323 "Flow Control: %s\n",
2324 adapter->link_speed,
2325 adapter->link_duplex == FULL_DUPLEX ?
2326 "Full Duplex" : "Half Duplex",
2327 ((ctrl & E1000_CTRL_TFCE) && (ctrl &
2328 E1000_CTRL_RFCE)) ? "RX/TX" : ((ctrl &
2329 E1000_CTRL_RFCE) ? "RX" : ((ctrl &
2330 E1000_CTRL_TFCE) ? "TX" : "None")));
2331
2332 /* tweak tx_queue_len according to speed/duplex and
2333 * adjust the timeout factor */
2334 netdev->tx_queue_len = adapter->tx_queue_len;
2335 adapter->tx_timeout_factor = 1;
2336 switch (adapter->link_speed) {
2337 case SPEED_10:
2338 netdev->tx_queue_len = 10;
2339 adapter->tx_timeout_factor = 14;
2340 break;
2341 case SPEED_100:
2342 netdev->tx_queue_len = 100;
2343 /* maybe add some timeout factor ? */
2344 break;
2345 }
2346
2347 netif_carrier_on(netdev);
2348 netif_wake_queue(netdev);
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07002349#ifdef CONFIG_NETDEVICES_MULTIQUEUE
2350 for (i = 0; i < adapter->num_tx_queues; i++)
2351 netif_wake_subqueue(netdev, i);
2352#endif
Auke Kok9d5c8242008-01-24 02:22:38 -08002353
2354 if (!test_bit(__IGB_DOWN, &adapter->state))
2355 mod_timer(&adapter->phy_info_timer,
2356 round_jiffies(jiffies + 2 * HZ));
2357 }
2358 } else {
2359 if (netif_carrier_ok(netdev)) {
2360 adapter->link_speed = 0;
2361 adapter->link_duplex = 0;
2362 dev_info(&adapter->pdev->dev, "NIC Link is Down\n");
2363 netif_carrier_off(netdev);
2364 netif_stop_queue(netdev);
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07002365#ifdef CONFIG_NETDEVICES_MULTIQUEUE
2366 for (i = 0; i < adapter->num_tx_queues; i++)
2367 netif_stop_subqueue(netdev, i);
2368#endif
Auke Kok9d5c8242008-01-24 02:22:38 -08002369 if (!test_bit(__IGB_DOWN, &adapter->state))
2370 mod_timer(&adapter->phy_info_timer,
2371 round_jiffies(jiffies + 2 * HZ));
2372 }
2373 }
2374
2375link_up:
2376 igb_update_stats(adapter);
2377
2378 mac->tx_packet_delta = adapter->stats.tpt - adapter->tpt_old;
2379 adapter->tpt_old = adapter->stats.tpt;
2380 mac->collision_delta = adapter->stats.colc - adapter->colc_old;
2381 adapter->colc_old = adapter->stats.colc;
2382
2383 adapter->gorc = adapter->stats.gorc - adapter->gorc_old;
2384 adapter->gorc_old = adapter->stats.gorc;
2385 adapter->gotc = adapter->stats.gotc - adapter->gotc_old;
2386 adapter->gotc_old = adapter->stats.gotc;
2387
2388 igb_update_adaptive(&adapter->hw);
2389
2390 if (!netif_carrier_ok(netdev)) {
2391 if (IGB_DESC_UNUSED(tx_ring) + 1 < tx_ring->count) {
2392 /* We've lost link, so the controller stops DMA,
2393 * but we've got queued Tx work that's never going
2394 * to get done, so reset controller to flush Tx.
2395 * (Do the reset outside of interrupt context). */
2396 adapter->tx_timeout_count++;
2397 schedule_work(&adapter->reset_task);
2398 }
2399 }
2400
2401 /* Cause software interrupt to ensure rx ring is cleaned */
2402 wr32(E1000_ICS, E1000_ICS_RXDMT0);
2403
2404 /* Force detection of hung controller every watchdog period */
2405 tx_ring->detect_tx_hung = true;
2406
2407 /* Reset the timer */
2408 if (!test_bit(__IGB_DOWN, &adapter->state))
2409 mod_timer(&adapter->watchdog_timer,
2410 round_jiffies(jiffies + 2 * HZ));
2411}
2412
2413enum latency_range {
2414 lowest_latency = 0,
2415 low_latency = 1,
2416 bulk_latency = 2,
2417 latency_invalid = 255
2418};
2419
2420
2421static void igb_lower_rx_eitr(struct igb_adapter *adapter,
2422 struct igb_ring *rx_ring)
2423{
2424 struct e1000_hw *hw = &adapter->hw;
2425 int new_val;
2426
2427 new_val = rx_ring->itr_val / 2;
2428 if (new_val < IGB_MIN_DYN_ITR)
2429 new_val = IGB_MIN_DYN_ITR;
2430
2431 if (new_val != rx_ring->itr_val) {
2432 rx_ring->itr_val = new_val;
2433 wr32(rx_ring->itr_register,
2434 1000000000 / (new_val * 256));
2435 }
2436}
2437
2438static void igb_raise_rx_eitr(struct igb_adapter *adapter,
2439 struct igb_ring *rx_ring)
2440{
2441 struct e1000_hw *hw = &adapter->hw;
2442 int new_val;
2443
2444 new_val = rx_ring->itr_val * 2;
2445 if (new_val > IGB_MAX_DYN_ITR)
2446 new_val = IGB_MAX_DYN_ITR;
2447
2448 if (new_val != rx_ring->itr_val) {
2449 rx_ring->itr_val = new_val;
2450 wr32(rx_ring->itr_register,
2451 1000000000 / (new_val * 256));
2452 }
2453}
2454
2455/**
2456 * igb_update_itr - update the dynamic ITR value based on statistics
2457 * Stores a new ITR value based on packets and byte
2458 * counts during the last interrupt. The advantage of per interrupt
2459 * computation is faster updates and more accurate ITR for the current
2460 * traffic pattern. Constants in this function were computed
2461 * based on theoretical maximum wire speed and thresholds were set based
2462 * on testing data as well as attempting to minimize response time
2463 * while increasing bulk throughput.
2464 * this functionality is controlled by the InterruptThrottleRate module
2465 * parameter (see igb_param.c)
2466 * NOTE: These calculations are only valid when operating in a single-
2467 * queue environment.
2468 * @adapter: pointer to adapter
2469 * @itr_setting: current adapter->itr
2470 * @packets: the number of packets during this measurement interval
2471 * @bytes: the number of bytes during this measurement interval
2472 **/
2473static unsigned int igb_update_itr(struct igb_adapter *adapter, u16 itr_setting,
2474 int packets, int bytes)
2475{
2476 unsigned int retval = itr_setting;
2477
2478 if (packets == 0)
2479 goto update_itr_done;
2480
2481 switch (itr_setting) {
2482 case lowest_latency:
2483 /* handle TSO and jumbo frames */
2484 if (bytes/packets > 8000)
2485 retval = bulk_latency;
2486 else if ((packets < 5) && (bytes > 512))
2487 retval = low_latency;
2488 break;
2489 case low_latency: /* 50 usec aka 20000 ints/s */
2490 if (bytes > 10000) {
2491 /* this if handles the TSO accounting */
2492 if (bytes/packets > 8000) {
2493 retval = bulk_latency;
2494 } else if ((packets < 10) || ((bytes/packets) > 1200)) {
2495 retval = bulk_latency;
2496 } else if ((packets > 35)) {
2497 retval = lowest_latency;
2498 }
2499 } else if (bytes/packets > 2000) {
2500 retval = bulk_latency;
2501 } else if (packets <= 2 && bytes < 512) {
2502 retval = lowest_latency;
2503 }
2504 break;
2505 case bulk_latency: /* 250 usec aka 4000 ints/s */
2506 if (bytes > 25000) {
2507 if (packets > 35)
2508 retval = low_latency;
2509 } else if (bytes < 6000) {
2510 retval = low_latency;
2511 }
2512 break;
2513 }
2514
2515update_itr_done:
2516 return retval;
2517}
2518
2519static void igb_set_itr(struct igb_adapter *adapter, u16 itr_register,
2520 int rx_only)
2521{
2522 u16 current_itr;
2523 u32 new_itr = adapter->itr;
2524
2525 /* for non-gigabit speeds, just fix the interrupt rate at 4000 */
2526 if (adapter->link_speed != SPEED_1000) {
2527 current_itr = 0;
2528 new_itr = 4000;
2529 goto set_itr_now;
2530 }
2531
2532 adapter->rx_itr = igb_update_itr(adapter,
2533 adapter->rx_itr,
2534 adapter->rx_ring->total_packets,
2535 adapter->rx_ring->total_bytes);
2536 /* conservative mode (itr 3) eliminates the lowest_latency setting */
2537 if (adapter->itr_setting == 3 && adapter->rx_itr == lowest_latency)
2538 adapter->rx_itr = low_latency;
2539
2540 if (!rx_only) {
2541 adapter->tx_itr = igb_update_itr(adapter,
2542 adapter->tx_itr,
2543 adapter->tx_ring->total_packets,
2544 adapter->tx_ring->total_bytes);
2545 /* conservative mode (itr 3) eliminates the
2546 * lowest_latency setting */
2547 if (adapter->itr_setting == 3 &&
2548 adapter->tx_itr == lowest_latency)
2549 adapter->tx_itr = low_latency;
2550
2551 current_itr = max(adapter->rx_itr, adapter->tx_itr);
2552 } else {
2553 current_itr = adapter->rx_itr;
2554 }
2555
2556 switch (current_itr) {
2557 /* counts and packets in update_itr are dependent on these numbers */
2558 case lowest_latency:
2559 new_itr = 70000;
2560 break;
2561 case low_latency:
2562 new_itr = 20000; /* aka hwitr = ~200 */
2563 break;
2564 case bulk_latency:
2565 new_itr = 4000;
2566 break;
2567 default:
2568 break;
2569 }
2570
2571set_itr_now:
2572 if (new_itr != adapter->itr) {
2573 /* this attempts to bias the interrupt rate towards Bulk
2574 * by adding intermediate steps when interrupt rate is
2575 * increasing */
2576 new_itr = new_itr > adapter->itr ?
2577 min(adapter->itr + (new_itr >> 2), new_itr) :
2578 new_itr;
2579 /* Don't write the value here; it resets the adapter's
2580 * internal timer, and causes us to delay far longer than
2581 * we should between interrupts. Instead, we write the ITR
2582 * value at the beginning of the next interrupt so the timing
2583 * ends up being correct.
2584 */
2585 adapter->itr = new_itr;
2586 adapter->set_itr = 1;
2587 }
2588
2589 return;
2590}
2591
2592
2593#define IGB_TX_FLAGS_CSUM 0x00000001
2594#define IGB_TX_FLAGS_VLAN 0x00000002
2595#define IGB_TX_FLAGS_TSO 0x00000004
2596#define IGB_TX_FLAGS_IPV4 0x00000008
2597#define IGB_TX_FLAGS_VLAN_MASK 0xffff0000
2598#define IGB_TX_FLAGS_VLAN_SHIFT 16
2599
2600static inline int igb_tso_adv(struct igb_adapter *adapter,
2601 struct igb_ring *tx_ring,
2602 struct sk_buff *skb, u32 tx_flags, u8 *hdr_len)
2603{
2604 struct e1000_adv_tx_context_desc *context_desc;
2605 unsigned int i;
2606 int err;
2607 struct igb_buffer *buffer_info;
2608 u32 info = 0, tu_cmd = 0;
2609 u32 mss_l4len_idx, l4len;
2610 *hdr_len = 0;
2611
2612 if (skb_header_cloned(skb)) {
2613 err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2614 if (err)
2615 return err;
2616 }
2617
2618 l4len = tcp_hdrlen(skb);
2619 *hdr_len += l4len;
2620
2621 if (skb->protocol == htons(ETH_P_IP)) {
2622 struct iphdr *iph = ip_hdr(skb);
2623 iph->tot_len = 0;
2624 iph->check = 0;
2625 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
2626 iph->daddr, 0,
2627 IPPROTO_TCP,
2628 0);
2629 } else if (skb_shinfo(skb)->gso_type == SKB_GSO_TCPV6) {
2630 ipv6_hdr(skb)->payload_len = 0;
2631 tcp_hdr(skb)->check = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
2632 &ipv6_hdr(skb)->daddr,
2633 0, IPPROTO_TCP, 0);
2634 }
2635
2636 i = tx_ring->next_to_use;
2637
2638 buffer_info = &tx_ring->buffer_info[i];
2639 context_desc = E1000_TX_CTXTDESC_ADV(*tx_ring, i);
2640 /* VLAN MACLEN IPLEN */
2641 if (tx_flags & IGB_TX_FLAGS_VLAN)
2642 info |= (tx_flags & IGB_TX_FLAGS_VLAN_MASK);
2643 info |= (skb_network_offset(skb) << E1000_ADVTXD_MACLEN_SHIFT);
2644 *hdr_len += skb_network_offset(skb);
2645 info |= skb_network_header_len(skb);
2646 *hdr_len += skb_network_header_len(skb);
2647 context_desc->vlan_macip_lens = cpu_to_le32(info);
2648
2649 /* ADV DTYP TUCMD MKRLOC/ISCSIHEDLEN */
2650 tu_cmd |= (E1000_TXD_CMD_DEXT | E1000_ADVTXD_DTYP_CTXT);
2651
2652 if (skb->protocol == htons(ETH_P_IP))
2653 tu_cmd |= E1000_ADVTXD_TUCMD_IPV4;
2654 tu_cmd |= E1000_ADVTXD_TUCMD_L4T_TCP;
2655
2656 context_desc->type_tucmd_mlhl = cpu_to_le32(tu_cmd);
2657
2658 /* MSS L4LEN IDX */
2659 mss_l4len_idx = (skb_shinfo(skb)->gso_size << E1000_ADVTXD_MSS_SHIFT);
2660 mss_l4len_idx |= (l4len << E1000_ADVTXD_L4LEN_SHIFT);
2661
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07002662 /* Context index must be unique per ring. */
2663 if (adapter->flags & IGB_FLAG_NEED_CTX_IDX)
2664 mss_l4len_idx |= tx_ring->queue_index << 4;
Auke Kok9d5c8242008-01-24 02:22:38 -08002665
2666 context_desc->mss_l4len_idx = cpu_to_le32(mss_l4len_idx);
2667 context_desc->seqnum_seed = 0;
2668
2669 buffer_info->time_stamp = jiffies;
2670 buffer_info->dma = 0;
2671 i++;
2672 if (i == tx_ring->count)
2673 i = 0;
2674
2675 tx_ring->next_to_use = i;
2676
2677 return true;
2678}
2679
2680static inline bool igb_tx_csum_adv(struct igb_adapter *adapter,
2681 struct igb_ring *tx_ring,
2682 struct sk_buff *skb, u32 tx_flags)
2683{
2684 struct e1000_adv_tx_context_desc *context_desc;
2685 unsigned int i;
2686 struct igb_buffer *buffer_info;
2687 u32 info = 0, tu_cmd = 0;
2688
2689 if ((skb->ip_summed == CHECKSUM_PARTIAL) ||
2690 (tx_flags & IGB_TX_FLAGS_VLAN)) {
2691 i = tx_ring->next_to_use;
2692 buffer_info = &tx_ring->buffer_info[i];
2693 context_desc = E1000_TX_CTXTDESC_ADV(*tx_ring, i);
2694
2695 if (tx_flags & IGB_TX_FLAGS_VLAN)
2696 info |= (tx_flags & IGB_TX_FLAGS_VLAN_MASK);
2697 info |= (skb_network_offset(skb) << E1000_ADVTXD_MACLEN_SHIFT);
2698 if (skb->ip_summed == CHECKSUM_PARTIAL)
2699 info |= skb_network_header_len(skb);
2700
2701 context_desc->vlan_macip_lens = cpu_to_le32(info);
2702
2703 tu_cmd |= (E1000_TXD_CMD_DEXT | E1000_ADVTXD_DTYP_CTXT);
2704
2705 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Mitch Williams44b0cda2008-03-07 10:32:13 -08002706 switch (skb->protocol) {
2707 case __constant_htons(ETH_P_IP):
Auke Kok9d5c8242008-01-24 02:22:38 -08002708 tu_cmd |= E1000_ADVTXD_TUCMD_IPV4;
Mitch Williams44b0cda2008-03-07 10:32:13 -08002709 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
2710 tu_cmd |= E1000_ADVTXD_TUCMD_L4T_TCP;
2711 break;
2712 case __constant_htons(ETH_P_IPV6):
2713 /* XXX what about other V6 headers?? */
2714 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
2715 tu_cmd |= E1000_ADVTXD_TUCMD_L4T_TCP;
2716 break;
2717 default:
2718 if (unlikely(net_ratelimit()))
2719 dev_warn(&adapter->pdev->dev,
2720 "partial checksum but proto=%x!\n",
2721 skb->protocol);
2722 break;
2723 }
Auke Kok9d5c8242008-01-24 02:22:38 -08002724 }
2725
2726 context_desc->type_tucmd_mlhl = cpu_to_le32(tu_cmd);
2727 context_desc->seqnum_seed = 0;
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07002728 if (adapter->flags & IGB_FLAG_NEED_CTX_IDX)
2729 context_desc->mss_l4len_idx =
2730 cpu_to_le32(tx_ring->queue_index << 4);
Auke Kok9d5c8242008-01-24 02:22:38 -08002731
2732 buffer_info->time_stamp = jiffies;
2733 buffer_info->dma = 0;
2734
2735 i++;
2736 if (i == tx_ring->count)
2737 i = 0;
2738 tx_ring->next_to_use = i;
2739
2740 return true;
2741 }
2742
2743
2744 return false;
2745}
2746
2747#define IGB_MAX_TXD_PWR 16
2748#define IGB_MAX_DATA_PER_TXD (1<<IGB_MAX_TXD_PWR)
2749
2750static inline int igb_tx_map_adv(struct igb_adapter *adapter,
2751 struct igb_ring *tx_ring,
2752 struct sk_buff *skb)
2753{
2754 struct igb_buffer *buffer_info;
2755 unsigned int len = skb_headlen(skb);
2756 unsigned int count = 0, i;
2757 unsigned int f;
2758
2759 i = tx_ring->next_to_use;
2760
2761 buffer_info = &tx_ring->buffer_info[i];
2762 BUG_ON(len >= IGB_MAX_DATA_PER_TXD);
2763 buffer_info->length = len;
2764 /* set time_stamp *before* dma to help avoid a possible race */
2765 buffer_info->time_stamp = jiffies;
2766 buffer_info->dma = pci_map_single(adapter->pdev, skb->data, len,
2767 PCI_DMA_TODEVICE);
2768 count++;
2769 i++;
2770 if (i == tx_ring->count)
2771 i = 0;
2772
2773 for (f = 0; f < skb_shinfo(skb)->nr_frags; f++) {
2774 struct skb_frag_struct *frag;
2775
2776 frag = &skb_shinfo(skb)->frags[f];
2777 len = frag->size;
2778
2779 buffer_info = &tx_ring->buffer_info[i];
2780 BUG_ON(len >= IGB_MAX_DATA_PER_TXD);
2781 buffer_info->length = len;
2782 buffer_info->time_stamp = jiffies;
2783 buffer_info->dma = pci_map_page(adapter->pdev,
2784 frag->page,
2785 frag->page_offset,
2786 len,
2787 PCI_DMA_TODEVICE);
2788
2789 count++;
2790 i++;
2791 if (i == tx_ring->count)
2792 i = 0;
2793 }
2794
2795 i = (i == 0) ? tx_ring->count - 1 : i - 1;
2796 tx_ring->buffer_info[i].skb = skb;
2797
2798 return count;
2799}
2800
2801static inline void igb_tx_queue_adv(struct igb_adapter *adapter,
2802 struct igb_ring *tx_ring,
2803 int tx_flags, int count, u32 paylen,
2804 u8 hdr_len)
2805{
2806 union e1000_adv_tx_desc *tx_desc = NULL;
2807 struct igb_buffer *buffer_info;
2808 u32 olinfo_status = 0, cmd_type_len;
2809 unsigned int i;
2810
2811 cmd_type_len = (E1000_ADVTXD_DTYP_DATA | E1000_ADVTXD_DCMD_IFCS |
2812 E1000_ADVTXD_DCMD_DEXT);
2813
2814 if (tx_flags & IGB_TX_FLAGS_VLAN)
2815 cmd_type_len |= E1000_ADVTXD_DCMD_VLE;
2816
2817 if (tx_flags & IGB_TX_FLAGS_TSO) {
2818 cmd_type_len |= E1000_ADVTXD_DCMD_TSE;
2819
2820 /* insert tcp checksum */
2821 olinfo_status |= E1000_TXD_POPTS_TXSM << 8;
2822
2823 /* insert ip checksum */
2824 if (tx_flags & IGB_TX_FLAGS_IPV4)
2825 olinfo_status |= E1000_TXD_POPTS_IXSM << 8;
2826
2827 } else if (tx_flags & IGB_TX_FLAGS_CSUM) {
2828 olinfo_status |= E1000_TXD_POPTS_TXSM << 8;
2829 }
2830
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07002831 if ((adapter->flags & IGB_FLAG_NEED_CTX_IDX) &&
2832 (tx_flags & (IGB_TX_FLAGS_CSUM | IGB_TX_FLAGS_TSO |
2833 IGB_TX_FLAGS_VLAN)))
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07002834 olinfo_status |= tx_ring->queue_index << 4;
Auke Kok9d5c8242008-01-24 02:22:38 -08002835
2836 olinfo_status |= ((paylen - hdr_len) << E1000_ADVTXD_PAYLEN_SHIFT);
2837
2838 i = tx_ring->next_to_use;
2839 while (count--) {
2840 buffer_info = &tx_ring->buffer_info[i];
2841 tx_desc = E1000_TX_DESC_ADV(*tx_ring, i);
2842 tx_desc->read.buffer_addr = cpu_to_le64(buffer_info->dma);
2843 tx_desc->read.cmd_type_len =
2844 cpu_to_le32(cmd_type_len | buffer_info->length);
2845 tx_desc->read.olinfo_status = cpu_to_le32(olinfo_status);
2846 i++;
2847 if (i == tx_ring->count)
2848 i = 0;
2849 }
2850
2851 tx_desc->read.cmd_type_len |= cpu_to_le32(adapter->txd_cmd);
2852 /* Force memory writes to complete before letting h/w
2853 * know there are new descriptors to fetch. (Only
2854 * applicable for weak-ordered memory model archs,
2855 * such as IA-64). */
2856 wmb();
2857
2858 tx_ring->next_to_use = i;
2859 writel(i, adapter->hw.hw_addr + tx_ring->tail);
2860 /* we need this if more than one processor can write to our tail
2861 * at a time, it syncronizes IO on IA64/Altix systems */
2862 mmiowb();
2863}
2864
2865static int __igb_maybe_stop_tx(struct net_device *netdev,
2866 struct igb_ring *tx_ring, int size)
2867{
2868 struct igb_adapter *adapter = netdev_priv(netdev);
2869
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07002870#ifdef CONFIG_NETDEVICES_MULTIQUEUE
2871 netif_stop_subqueue(netdev, tx_ring->queue_index);
2872#else
Auke Kok9d5c8242008-01-24 02:22:38 -08002873 netif_stop_queue(netdev);
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07002874#endif
2875
Auke Kok9d5c8242008-01-24 02:22:38 -08002876 /* Herbert's original patch had:
2877 * smp_mb__after_netif_stop_queue();
2878 * but since that doesn't exist yet, just open code it. */
2879 smp_mb();
2880
2881 /* We need to check again in a case another CPU has just
2882 * made room available. */
2883 if (IGB_DESC_UNUSED(tx_ring) < size)
2884 return -EBUSY;
2885
2886 /* A reprieve! */
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07002887#ifdef CONFIG_NETDEVICES_MULTIQUEUE
2888 netif_wake_subqueue(netdev, tx_ring->queue_index);
2889#else
2890 netif_wake_queue(netdev);
2891#endif
Auke Kok9d5c8242008-01-24 02:22:38 -08002892 ++adapter->restart_queue;
2893 return 0;
2894}
2895
2896static int igb_maybe_stop_tx(struct net_device *netdev,
2897 struct igb_ring *tx_ring, int size)
2898{
2899 if (IGB_DESC_UNUSED(tx_ring) >= size)
2900 return 0;
2901 return __igb_maybe_stop_tx(netdev, tx_ring, size);
2902}
2903
2904#define TXD_USE_COUNT(S) (((S) >> (IGB_MAX_TXD_PWR)) + 1)
2905
2906static int igb_xmit_frame_ring_adv(struct sk_buff *skb,
2907 struct net_device *netdev,
2908 struct igb_ring *tx_ring)
2909{
2910 struct igb_adapter *adapter = netdev_priv(netdev);
2911 unsigned int tx_flags = 0;
2912 unsigned int len;
Auke Kok9d5c8242008-01-24 02:22:38 -08002913 u8 hdr_len = 0;
2914 int tso = 0;
2915
2916 len = skb_headlen(skb);
2917
2918 if (test_bit(__IGB_DOWN, &adapter->state)) {
2919 dev_kfree_skb_any(skb);
2920 return NETDEV_TX_OK;
2921 }
2922
2923 if (skb->len <= 0) {
2924 dev_kfree_skb_any(skb);
2925 return NETDEV_TX_OK;
2926 }
2927
Auke Kok9d5c8242008-01-24 02:22:38 -08002928 /* need: 1 descriptor per page,
2929 * + 2 desc gap to keep tail from touching head,
2930 * + 1 desc for skb->data,
2931 * + 1 desc for context descriptor,
2932 * otherwise try next time */
2933 if (igb_maybe_stop_tx(netdev, tx_ring, skb_shinfo(skb)->nr_frags + 4)) {
2934 /* this is a hard error */
Auke Kok9d5c8242008-01-24 02:22:38 -08002935 return NETDEV_TX_BUSY;
2936 }
2937
2938 if (adapter->vlgrp && vlan_tx_tag_present(skb)) {
2939 tx_flags |= IGB_TX_FLAGS_VLAN;
2940 tx_flags |= (vlan_tx_tag_get(skb) << IGB_TX_FLAGS_VLAN_SHIFT);
2941 }
2942
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07002943 if (skb->protocol == htons(ETH_P_IP))
2944 tx_flags |= IGB_TX_FLAGS_IPV4;
2945
Auke Kok9d5c8242008-01-24 02:22:38 -08002946 tso = skb_is_gso(skb) ? igb_tso_adv(adapter, tx_ring, skb, tx_flags,
2947 &hdr_len) : 0;
2948
2949 if (tso < 0) {
2950 dev_kfree_skb_any(skb);
Auke Kok9d5c8242008-01-24 02:22:38 -08002951 return NETDEV_TX_OK;
2952 }
2953
2954 if (tso)
2955 tx_flags |= IGB_TX_FLAGS_TSO;
2956 else if (igb_tx_csum_adv(adapter, tx_ring, skb, tx_flags))
2957 if (skb->ip_summed == CHECKSUM_PARTIAL)
2958 tx_flags |= IGB_TX_FLAGS_CSUM;
2959
Auke Kok9d5c8242008-01-24 02:22:38 -08002960 igb_tx_queue_adv(adapter, tx_ring, tx_flags,
2961 igb_tx_map_adv(adapter, tx_ring, skb),
2962 skb->len, hdr_len);
2963
2964 netdev->trans_start = jiffies;
2965
2966 /* Make sure there is space in the ring for the next send. */
2967 igb_maybe_stop_tx(netdev, tx_ring, MAX_SKB_FRAGS + 4);
2968
Auke Kok9d5c8242008-01-24 02:22:38 -08002969 return NETDEV_TX_OK;
2970}
2971
2972static int igb_xmit_frame_adv(struct sk_buff *skb, struct net_device *netdev)
2973{
2974 struct igb_adapter *adapter = netdev_priv(netdev);
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07002975 struct igb_ring *tx_ring;
2976
2977#ifdef CONFIG_NETDEVICES_MULTIQUEUE
2978 int r_idx = 0;
2979 r_idx = skb->queue_mapping & (IGB_MAX_TX_QUEUES - 1);
2980 tx_ring = adapter->multi_tx_table[r_idx];
2981#else
2982 tx_ring = &adapter->tx_ring[0];
2983#endif
2984
Auke Kok9d5c8242008-01-24 02:22:38 -08002985
2986 /* This goes back to the question of how to logically map a tx queue
2987 * to a flow. Right now, performance is impacted slightly negatively
2988 * if using multiple tx queues. If the stack breaks away from a
2989 * single qdisc implementation, we can look at this again. */
2990 return (igb_xmit_frame_ring_adv(skb, netdev, tx_ring));
2991}
2992
2993/**
2994 * igb_tx_timeout - Respond to a Tx Hang
2995 * @netdev: network interface device structure
2996 **/
2997static void igb_tx_timeout(struct net_device *netdev)
2998{
2999 struct igb_adapter *adapter = netdev_priv(netdev);
3000 struct e1000_hw *hw = &adapter->hw;
3001
3002 /* Do the reset outside of interrupt context */
3003 adapter->tx_timeout_count++;
3004 schedule_work(&adapter->reset_task);
3005 wr32(E1000_EICS, adapter->eims_enable_mask &
3006 ~(E1000_EIMS_TCP_TIMER | E1000_EIMS_OTHER));
3007}
3008
3009static void igb_reset_task(struct work_struct *work)
3010{
3011 struct igb_adapter *adapter;
3012 adapter = container_of(work, struct igb_adapter, reset_task);
3013
3014 igb_reinit_locked(adapter);
3015}
3016
3017/**
3018 * igb_get_stats - Get System Network Statistics
3019 * @netdev: network interface device structure
3020 *
3021 * Returns the address of the device statistics structure.
3022 * The statistics are actually updated from the timer callback.
3023 **/
3024static struct net_device_stats *
3025igb_get_stats(struct net_device *netdev)
3026{
3027 struct igb_adapter *adapter = netdev_priv(netdev);
3028
3029 /* only return the current stats */
3030 return &adapter->net_stats;
3031}
3032
3033/**
3034 * igb_change_mtu - Change the Maximum Transfer Unit
3035 * @netdev: network interface device structure
3036 * @new_mtu: new value for maximum frame size
3037 *
3038 * Returns 0 on success, negative on failure
3039 **/
3040static int igb_change_mtu(struct net_device *netdev, int new_mtu)
3041{
3042 struct igb_adapter *adapter = netdev_priv(netdev);
3043 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
3044
3045 if ((max_frame < ETH_ZLEN + ETH_FCS_LEN) ||
3046 (max_frame > MAX_JUMBO_FRAME_SIZE)) {
3047 dev_err(&adapter->pdev->dev, "Invalid MTU setting\n");
3048 return -EINVAL;
3049 }
3050
3051#define MAX_STD_JUMBO_FRAME_SIZE 9234
3052 if (max_frame > MAX_STD_JUMBO_FRAME_SIZE) {
3053 dev_err(&adapter->pdev->dev, "MTU > 9216 not supported.\n");
3054 return -EINVAL;
3055 }
3056
3057 while (test_and_set_bit(__IGB_RESETTING, &adapter->state))
3058 msleep(1);
3059 /* igb_down has a dependency on max_frame_size */
3060 adapter->max_frame_size = max_frame;
3061 if (netif_running(netdev))
3062 igb_down(adapter);
3063
3064 /* NOTE: netdev_alloc_skb reserves 16 bytes, and typically NET_IP_ALIGN
3065 * means we reserve 2 more, this pushes us to allocate from the next
3066 * larger slab size.
3067 * i.e. RXBUFFER_2048 --> size-4096 slab
3068 */
3069
3070 if (max_frame <= IGB_RXBUFFER_256)
3071 adapter->rx_buffer_len = IGB_RXBUFFER_256;
3072 else if (max_frame <= IGB_RXBUFFER_512)
3073 adapter->rx_buffer_len = IGB_RXBUFFER_512;
3074 else if (max_frame <= IGB_RXBUFFER_1024)
3075 adapter->rx_buffer_len = IGB_RXBUFFER_1024;
3076 else if (max_frame <= IGB_RXBUFFER_2048)
3077 adapter->rx_buffer_len = IGB_RXBUFFER_2048;
3078 else
Alexander Duyckbf36c1a2008-07-08 15:11:40 -07003079#if (PAGE_SIZE / 2) > IGB_RXBUFFER_16384
3080 adapter->rx_buffer_len = IGB_RXBUFFER_16384;
3081#else
3082 adapter->rx_buffer_len = PAGE_SIZE / 2;
3083#endif
Auke Kok9d5c8242008-01-24 02:22:38 -08003084 /* adjust allocation if LPE protects us, and we aren't using SBP */
3085 if ((max_frame == ETH_FRAME_LEN + ETH_FCS_LEN) ||
3086 (max_frame == MAXIMUM_ETHERNET_VLAN_SIZE))
3087 adapter->rx_buffer_len = MAXIMUM_ETHERNET_VLAN_SIZE;
3088
3089 dev_info(&adapter->pdev->dev, "changing MTU from %d to %d\n",
3090 netdev->mtu, new_mtu);
3091 netdev->mtu = new_mtu;
3092
3093 if (netif_running(netdev))
3094 igb_up(adapter);
3095 else
3096 igb_reset(adapter);
3097
3098 clear_bit(__IGB_RESETTING, &adapter->state);
3099
3100 return 0;
3101}
3102
3103/**
3104 * igb_update_stats - Update the board statistics counters
3105 * @adapter: board private structure
3106 **/
3107
3108void igb_update_stats(struct igb_adapter *adapter)
3109{
3110 struct e1000_hw *hw = &adapter->hw;
3111 struct pci_dev *pdev = adapter->pdev;
3112 u16 phy_tmp;
3113
3114#define PHY_IDLE_ERROR_COUNT_MASK 0x00FF
3115
3116 /*
3117 * Prevent stats update while adapter is being reset, or if the pci
3118 * connection is down.
3119 */
3120 if (adapter->link_speed == 0)
3121 return;
3122 if (pci_channel_offline(pdev))
3123 return;
3124
3125 adapter->stats.crcerrs += rd32(E1000_CRCERRS);
3126 adapter->stats.gprc += rd32(E1000_GPRC);
3127 adapter->stats.gorc += rd32(E1000_GORCL);
3128 rd32(E1000_GORCH); /* clear GORCL */
3129 adapter->stats.bprc += rd32(E1000_BPRC);
3130 adapter->stats.mprc += rd32(E1000_MPRC);
3131 adapter->stats.roc += rd32(E1000_ROC);
3132
3133 adapter->stats.prc64 += rd32(E1000_PRC64);
3134 adapter->stats.prc127 += rd32(E1000_PRC127);
3135 adapter->stats.prc255 += rd32(E1000_PRC255);
3136 adapter->stats.prc511 += rd32(E1000_PRC511);
3137 adapter->stats.prc1023 += rd32(E1000_PRC1023);
3138 adapter->stats.prc1522 += rd32(E1000_PRC1522);
3139 adapter->stats.symerrs += rd32(E1000_SYMERRS);
3140 adapter->stats.sec += rd32(E1000_SEC);
3141
3142 adapter->stats.mpc += rd32(E1000_MPC);
3143 adapter->stats.scc += rd32(E1000_SCC);
3144 adapter->stats.ecol += rd32(E1000_ECOL);
3145 adapter->stats.mcc += rd32(E1000_MCC);
3146 adapter->stats.latecol += rd32(E1000_LATECOL);
3147 adapter->stats.dc += rd32(E1000_DC);
3148 adapter->stats.rlec += rd32(E1000_RLEC);
3149 adapter->stats.xonrxc += rd32(E1000_XONRXC);
3150 adapter->stats.xontxc += rd32(E1000_XONTXC);
3151 adapter->stats.xoffrxc += rd32(E1000_XOFFRXC);
3152 adapter->stats.xofftxc += rd32(E1000_XOFFTXC);
3153 adapter->stats.fcruc += rd32(E1000_FCRUC);
3154 adapter->stats.gptc += rd32(E1000_GPTC);
3155 adapter->stats.gotc += rd32(E1000_GOTCL);
3156 rd32(E1000_GOTCH); /* clear GOTCL */
3157 adapter->stats.rnbc += rd32(E1000_RNBC);
3158 adapter->stats.ruc += rd32(E1000_RUC);
3159 adapter->stats.rfc += rd32(E1000_RFC);
3160 adapter->stats.rjc += rd32(E1000_RJC);
3161 adapter->stats.tor += rd32(E1000_TORH);
3162 adapter->stats.tot += rd32(E1000_TOTH);
3163 adapter->stats.tpr += rd32(E1000_TPR);
3164
3165 adapter->stats.ptc64 += rd32(E1000_PTC64);
3166 adapter->stats.ptc127 += rd32(E1000_PTC127);
3167 adapter->stats.ptc255 += rd32(E1000_PTC255);
3168 adapter->stats.ptc511 += rd32(E1000_PTC511);
3169 adapter->stats.ptc1023 += rd32(E1000_PTC1023);
3170 adapter->stats.ptc1522 += rd32(E1000_PTC1522);
3171
3172 adapter->stats.mptc += rd32(E1000_MPTC);
3173 adapter->stats.bptc += rd32(E1000_BPTC);
3174
3175 /* used for adaptive IFS */
3176
3177 hw->mac.tx_packet_delta = rd32(E1000_TPT);
3178 adapter->stats.tpt += hw->mac.tx_packet_delta;
3179 hw->mac.collision_delta = rd32(E1000_COLC);
3180 adapter->stats.colc += hw->mac.collision_delta;
3181
3182 adapter->stats.algnerrc += rd32(E1000_ALGNERRC);
3183 adapter->stats.rxerrc += rd32(E1000_RXERRC);
3184 adapter->stats.tncrs += rd32(E1000_TNCRS);
3185 adapter->stats.tsctc += rd32(E1000_TSCTC);
3186 adapter->stats.tsctfc += rd32(E1000_TSCTFC);
3187
3188 adapter->stats.iac += rd32(E1000_IAC);
3189 adapter->stats.icrxoc += rd32(E1000_ICRXOC);
3190 adapter->stats.icrxptc += rd32(E1000_ICRXPTC);
3191 adapter->stats.icrxatc += rd32(E1000_ICRXATC);
3192 adapter->stats.ictxptc += rd32(E1000_ICTXPTC);
3193 adapter->stats.ictxatc += rd32(E1000_ICTXATC);
3194 adapter->stats.ictxqec += rd32(E1000_ICTXQEC);
3195 adapter->stats.ictxqmtc += rd32(E1000_ICTXQMTC);
3196 adapter->stats.icrxdmtc += rd32(E1000_ICRXDMTC);
3197
3198 /* Fill out the OS statistics structure */
3199 adapter->net_stats.multicast = adapter->stats.mprc;
3200 adapter->net_stats.collisions = adapter->stats.colc;
3201
3202 /* Rx Errors */
3203
3204 /* RLEC on some newer hardware can be incorrect so build
3205 * our own version based on RUC and ROC */
3206 adapter->net_stats.rx_errors = adapter->stats.rxerrc +
3207 adapter->stats.crcerrs + adapter->stats.algnerrc +
3208 adapter->stats.ruc + adapter->stats.roc +
3209 adapter->stats.cexterr;
3210 adapter->net_stats.rx_length_errors = adapter->stats.ruc +
3211 adapter->stats.roc;
3212 adapter->net_stats.rx_crc_errors = adapter->stats.crcerrs;
3213 adapter->net_stats.rx_frame_errors = adapter->stats.algnerrc;
3214 adapter->net_stats.rx_missed_errors = adapter->stats.mpc;
3215
3216 /* Tx Errors */
3217 adapter->net_stats.tx_errors = adapter->stats.ecol +
3218 adapter->stats.latecol;
3219 adapter->net_stats.tx_aborted_errors = adapter->stats.ecol;
3220 adapter->net_stats.tx_window_errors = adapter->stats.latecol;
3221 adapter->net_stats.tx_carrier_errors = adapter->stats.tncrs;
3222
3223 /* Tx Dropped needs to be maintained elsewhere */
3224
3225 /* Phy Stats */
3226 if (hw->phy.media_type == e1000_media_type_copper) {
3227 if ((adapter->link_speed == SPEED_1000) &&
3228 (!hw->phy.ops.read_phy_reg(hw, PHY_1000T_STATUS,
3229 &phy_tmp))) {
3230 phy_tmp &= PHY_IDLE_ERROR_COUNT_MASK;
3231 adapter->phy_stats.idle_errors += phy_tmp;
3232 }
3233 }
3234
3235 /* Management Stats */
3236 adapter->stats.mgptc += rd32(E1000_MGTPTC);
3237 adapter->stats.mgprc += rd32(E1000_MGTPRC);
3238 adapter->stats.mgpdc += rd32(E1000_MGTPDC);
3239}
3240
3241
3242static irqreturn_t igb_msix_other(int irq, void *data)
3243{
3244 struct net_device *netdev = data;
3245 struct igb_adapter *adapter = netdev_priv(netdev);
3246 struct e1000_hw *hw = &adapter->hw;
PJ Waskiewicz844290e2008-06-27 11:00:39 -07003247 u32 icr = rd32(E1000_ICR);
Auke Kok9d5c8242008-01-24 02:22:38 -08003248
PJ Waskiewicz844290e2008-06-27 11:00:39 -07003249 /* reading ICR causes bit 31 of EICR to be cleared */
3250 if (!(icr & E1000_ICR_LSC))
3251 goto no_link_interrupt;
3252 hw->mac.get_link_status = 1;
3253 /* guard against interrupt when we're going down */
3254 if (!test_bit(__IGB_DOWN, &adapter->state))
3255 mod_timer(&adapter->watchdog_timer, jiffies + 1);
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07003256
Auke Kok9d5c8242008-01-24 02:22:38 -08003257no_link_interrupt:
3258 wr32(E1000_IMS, E1000_IMS_LSC);
PJ Waskiewicz844290e2008-06-27 11:00:39 -07003259 wr32(E1000_EIMS, adapter->eims_other);
Auke Kok9d5c8242008-01-24 02:22:38 -08003260
3261 return IRQ_HANDLED;
3262}
3263
3264static irqreturn_t igb_msix_tx(int irq, void *data)
3265{
3266 struct igb_ring *tx_ring = data;
3267 struct igb_adapter *adapter = tx_ring->adapter;
3268 struct e1000_hw *hw = &adapter->hw;
3269
3270 if (!tx_ring->itr_val)
3271 wr32(E1000_EIMC, tx_ring->eims_value);
Jeb Cramerfe4506b2008-07-08 15:07:55 -07003272#ifdef CONFIG_DCA
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07003273 if (adapter->flags & IGB_FLAG_DCA_ENABLED)
Jeb Cramerfe4506b2008-07-08 15:07:55 -07003274 igb_update_tx_dca(tx_ring);
3275#endif
Auke Kok9d5c8242008-01-24 02:22:38 -08003276 tx_ring->total_bytes = 0;
3277 tx_ring->total_packets = 0;
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07003278
3279 /* auto mask will automatically reenable the interrupt when we write
3280 * EICS */
Mitch Williams3b644cf2008-06-27 10:59:48 -07003281 if (!igb_clean_tx_irq(tx_ring))
Auke Kok9d5c8242008-01-24 02:22:38 -08003282 /* Ring was not completely cleaned, so fire another interrupt */
3283 wr32(E1000_EICS, tx_ring->eims_value);
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07003284 else
Auke Kok9d5c8242008-01-24 02:22:38 -08003285 wr32(E1000_EIMS, tx_ring->eims_value);
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07003286
Auke Kok9d5c8242008-01-24 02:22:38 -08003287 return IRQ_HANDLED;
3288}
3289
3290static irqreturn_t igb_msix_rx(int irq, void *data)
3291{
3292 struct igb_ring *rx_ring = data;
3293 struct igb_adapter *adapter = rx_ring->adapter;
3294 struct e1000_hw *hw = &adapter->hw;
3295
PJ Waskiewicz844290e2008-06-27 11:00:39 -07003296 /* Write the ITR value calculated at the end of the
3297 * previous interrupt.
3298 */
Auke Kok9d5c8242008-01-24 02:22:38 -08003299
PJ Waskiewicz844290e2008-06-27 11:00:39 -07003300 if (adapter->set_itr) {
3301 wr32(rx_ring->itr_register,
3302 1000000000 / (rx_ring->itr_val * 256));
3303 adapter->set_itr = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08003304 }
3305
PJ Waskiewicz844290e2008-06-27 11:00:39 -07003306 if (netif_rx_schedule_prep(adapter->netdev, &rx_ring->napi))
3307 __netif_rx_schedule(adapter->netdev, &rx_ring->napi);
3308
Jeb Cramerfe4506b2008-07-08 15:07:55 -07003309#ifdef CONFIG_DCA
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07003310 if (adapter->flags & IGB_FLAG_DCA_ENABLED)
Jeb Cramerfe4506b2008-07-08 15:07:55 -07003311 igb_update_rx_dca(rx_ring);
3312#endif
3313 return IRQ_HANDLED;
Auke Kok9d5c8242008-01-24 02:22:38 -08003314}
3315
Jeb Cramerfe4506b2008-07-08 15:07:55 -07003316#ifdef CONFIG_DCA
3317static void igb_update_rx_dca(struct igb_ring *rx_ring)
3318{
3319 u32 dca_rxctrl;
3320 struct igb_adapter *adapter = rx_ring->adapter;
3321 struct e1000_hw *hw = &adapter->hw;
3322 int cpu = get_cpu();
3323 int q = rx_ring - adapter->rx_ring;
3324
3325 if (rx_ring->cpu != cpu) {
3326 dca_rxctrl = rd32(E1000_DCA_RXCTRL(q));
Alexander Duyck2d064c02008-07-08 15:10:12 -07003327 if (hw->mac.type == e1000_82576) {
3328 dca_rxctrl &= ~E1000_DCA_RXCTRL_CPUID_MASK_82576;
3329 dca_rxctrl |= dca_get_tag(cpu) <<
3330 E1000_DCA_RXCTRL_CPUID_SHIFT;
3331 } else {
3332 dca_rxctrl &= ~E1000_DCA_RXCTRL_CPUID_MASK;
3333 dca_rxctrl |= dca_get_tag(cpu);
3334 }
Jeb Cramerfe4506b2008-07-08 15:07:55 -07003335 dca_rxctrl |= E1000_DCA_RXCTRL_DESC_DCA_EN;
3336 dca_rxctrl |= E1000_DCA_RXCTRL_HEAD_DCA_EN;
3337 dca_rxctrl |= E1000_DCA_RXCTRL_DATA_DCA_EN;
3338 wr32(E1000_DCA_RXCTRL(q), dca_rxctrl);
3339 rx_ring->cpu = cpu;
3340 }
3341 put_cpu();
3342}
3343
3344static void igb_update_tx_dca(struct igb_ring *tx_ring)
3345{
3346 u32 dca_txctrl;
3347 struct igb_adapter *adapter = tx_ring->adapter;
3348 struct e1000_hw *hw = &adapter->hw;
3349 int cpu = get_cpu();
3350 int q = tx_ring - adapter->tx_ring;
3351
3352 if (tx_ring->cpu != cpu) {
3353 dca_txctrl = rd32(E1000_DCA_TXCTRL(q));
Alexander Duyck2d064c02008-07-08 15:10:12 -07003354 if (hw->mac.type == e1000_82576) {
3355 dca_txctrl &= ~E1000_DCA_TXCTRL_CPUID_MASK_82576;
3356 dca_txctrl |= dca_get_tag(cpu) <<
3357 E1000_DCA_TXCTRL_CPUID_SHIFT;
3358 } else {
3359 dca_txctrl &= ~E1000_DCA_TXCTRL_CPUID_MASK;
3360 dca_txctrl |= dca_get_tag(cpu);
3361 }
Jeb Cramerfe4506b2008-07-08 15:07:55 -07003362 dca_txctrl |= E1000_DCA_TXCTRL_DESC_DCA_EN;
3363 wr32(E1000_DCA_TXCTRL(q), dca_txctrl);
3364 tx_ring->cpu = cpu;
3365 }
3366 put_cpu();
3367}
3368
3369static void igb_setup_dca(struct igb_adapter *adapter)
3370{
3371 int i;
3372
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07003373 if (!(adapter->flags & IGB_FLAG_DCA_ENABLED))
Jeb Cramerfe4506b2008-07-08 15:07:55 -07003374 return;
3375
3376 for (i = 0; i < adapter->num_tx_queues; i++) {
3377 adapter->tx_ring[i].cpu = -1;
3378 igb_update_tx_dca(&adapter->tx_ring[i]);
3379 }
3380 for (i = 0; i < adapter->num_rx_queues; i++) {
3381 adapter->rx_ring[i].cpu = -1;
3382 igb_update_rx_dca(&adapter->rx_ring[i]);
3383 }
3384}
3385
3386static int __igb_notify_dca(struct device *dev, void *data)
3387{
3388 struct net_device *netdev = dev_get_drvdata(dev);
3389 struct igb_adapter *adapter = netdev_priv(netdev);
3390 struct e1000_hw *hw = &adapter->hw;
3391 unsigned long event = *(unsigned long *)data;
3392
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07003393 if (!(adapter->flags & IGB_FLAG_HAS_DCA))
3394 goto out;
3395
Jeb Cramerfe4506b2008-07-08 15:07:55 -07003396 switch (event) {
3397 case DCA_PROVIDER_ADD:
3398 /* if already enabled, don't do it again */
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07003399 if (adapter->flags & IGB_FLAG_DCA_ENABLED)
Jeb Cramerfe4506b2008-07-08 15:07:55 -07003400 break;
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07003401 adapter->flags |= IGB_FLAG_DCA_ENABLED;
Jeb Cramerfe4506b2008-07-08 15:07:55 -07003402 /* Always use CB2 mode, difference is masked
3403 * in the CB driver. */
3404 wr32(E1000_DCA_CTRL, 2);
3405 if (dca_add_requester(dev) == 0) {
3406 dev_info(&adapter->pdev->dev, "DCA enabled\n");
3407 igb_setup_dca(adapter);
3408 break;
3409 }
3410 /* Fall Through since DCA is disabled. */
3411 case DCA_PROVIDER_REMOVE:
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07003412 if (adapter->flags & IGB_FLAG_DCA_ENABLED) {
Jeb Cramerfe4506b2008-07-08 15:07:55 -07003413 /* without this a class_device is left
3414 * hanging around in the sysfs model */
3415 dca_remove_requester(dev);
3416 dev_info(&adapter->pdev->dev, "DCA disabled\n");
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07003417 adapter->flags &= ~IGB_FLAG_DCA_ENABLED;
Jeb Cramerfe4506b2008-07-08 15:07:55 -07003418 wr32(E1000_DCA_CTRL, 1);
3419 }
3420 break;
3421 }
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07003422out:
Jeb Cramerfe4506b2008-07-08 15:07:55 -07003423 return 0;
3424}
3425
3426static int igb_notify_dca(struct notifier_block *nb, unsigned long event,
3427 void *p)
3428{
3429 int ret_val;
3430
3431 ret_val = driver_for_each_device(&igb_driver.driver, NULL, &event,
3432 __igb_notify_dca);
3433
3434 return ret_val ? NOTIFY_BAD : NOTIFY_DONE;
3435}
3436#endif /* CONFIG_DCA */
Auke Kok9d5c8242008-01-24 02:22:38 -08003437
3438/**
3439 * igb_intr_msi - Interrupt Handler
3440 * @irq: interrupt number
3441 * @data: pointer to a network interface device structure
3442 **/
3443static irqreturn_t igb_intr_msi(int irq, void *data)
3444{
3445 struct net_device *netdev = data;
3446 struct igb_adapter *adapter = netdev_priv(netdev);
Auke Kok9d5c8242008-01-24 02:22:38 -08003447 struct e1000_hw *hw = &adapter->hw;
3448 /* read ICR disables interrupts using IAM */
3449 u32 icr = rd32(E1000_ICR);
3450
3451 /* Write the ITR value calculated at the end of the
3452 * previous interrupt.
3453 */
3454 if (adapter->set_itr) {
PJ Waskiewicz844290e2008-06-27 11:00:39 -07003455 wr32(E1000_ITR, 1000000000 / (adapter->itr * 256));
Auke Kok9d5c8242008-01-24 02:22:38 -08003456 adapter->set_itr = 0;
3457 }
3458
Auke Kok9d5c8242008-01-24 02:22:38 -08003459 if (icr & (E1000_ICR_RXSEQ | E1000_ICR_LSC)) {
3460 hw->mac.get_link_status = 1;
3461 if (!test_bit(__IGB_DOWN, &adapter->state))
3462 mod_timer(&adapter->watchdog_timer, jiffies + 1);
3463 }
3464
PJ Waskiewicz844290e2008-06-27 11:00:39 -07003465 netif_rx_schedule(netdev, &adapter->rx_ring[0].napi);
Auke Kok9d5c8242008-01-24 02:22:38 -08003466
3467 return IRQ_HANDLED;
3468}
3469
3470/**
3471 * igb_intr - Interrupt Handler
3472 * @irq: interrupt number
3473 * @data: pointer to a network interface device structure
3474 **/
3475static irqreturn_t igb_intr(int irq, void *data)
3476{
3477 struct net_device *netdev = data;
3478 struct igb_adapter *adapter = netdev_priv(netdev);
Auke Kok9d5c8242008-01-24 02:22:38 -08003479 struct e1000_hw *hw = &adapter->hw;
3480 /* Interrupt Auto-Mask...upon reading ICR, interrupts are masked. No
3481 * need for the IMC write */
3482 u32 icr = rd32(E1000_ICR);
3483 u32 eicr = 0;
3484 if (!icr)
3485 return IRQ_NONE; /* Not our interrupt */
3486
3487 /* Write the ITR value calculated at the end of the
3488 * previous interrupt.
3489 */
3490 if (adapter->set_itr) {
PJ Waskiewicz844290e2008-06-27 11:00:39 -07003491 wr32(E1000_ITR, 1000000000 / (adapter->itr * 256));
Auke Kok9d5c8242008-01-24 02:22:38 -08003492 adapter->set_itr = 0;
3493 }
3494
3495 /* IMS will not auto-mask if INT_ASSERTED is not set, and if it is
3496 * not set, then the adapter didn't send an interrupt */
3497 if (!(icr & E1000_ICR_INT_ASSERTED))
3498 return IRQ_NONE;
3499
3500 eicr = rd32(E1000_EICR);
3501
3502 if (icr & (E1000_ICR_RXSEQ | E1000_ICR_LSC)) {
3503 hw->mac.get_link_status = 1;
3504 /* guard against interrupt when we're going down */
3505 if (!test_bit(__IGB_DOWN, &adapter->state))
3506 mod_timer(&adapter->watchdog_timer, jiffies + 1);
3507 }
3508
PJ Waskiewicz844290e2008-06-27 11:00:39 -07003509 netif_rx_schedule(netdev, &adapter->rx_ring[0].napi);
Auke Kok9d5c8242008-01-24 02:22:38 -08003510
3511 return IRQ_HANDLED;
3512}
3513
3514/**
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07003515 * igb_poll - NAPI Rx polling callback
3516 * @napi: napi polling structure
3517 * @budget: count of how many packets we should handle
Auke Kok9d5c8242008-01-24 02:22:38 -08003518 **/
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07003519static int igb_poll(struct napi_struct *napi, int budget)
Auke Kok9d5c8242008-01-24 02:22:38 -08003520{
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07003521 struct igb_ring *rx_ring = container_of(napi, struct igb_ring, napi);
3522 struct igb_adapter *adapter = rx_ring->adapter;
Auke Kok9d5c8242008-01-24 02:22:38 -08003523 struct net_device *netdev = adapter->netdev;
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07003524 int tx_clean_complete, work_done = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08003525
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07003526 /* this poll routine only supports one tx and one rx queue */
Jeb Cramerfe4506b2008-07-08 15:07:55 -07003527#ifdef CONFIG_DCA
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07003528 if (adapter->flags & IGB_FLAG_DCA_ENABLED)
Jeb Cramerfe4506b2008-07-08 15:07:55 -07003529 igb_update_tx_dca(&adapter->tx_ring[0]);
3530#endif
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07003531 tx_clean_complete = igb_clean_tx_irq(&adapter->tx_ring[0]);
Jeb Cramerfe4506b2008-07-08 15:07:55 -07003532
3533#ifdef CONFIG_DCA
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07003534 if (adapter->flags & IGB_FLAG_DCA_ENABLED)
Jeb Cramerfe4506b2008-07-08 15:07:55 -07003535 igb_update_rx_dca(&adapter->rx_ring[0]);
3536#endif
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07003537 igb_clean_rx_irq_adv(&adapter->rx_ring[0], &work_done, budget);
Auke Kok9d5c8242008-01-24 02:22:38 -08003538
3539 /* If no Tx and not enough Rx work done, exit the polling mode */
3540 if ((tx_clean_complete && (work_done < budget)) ||
3541 !netif_running(netdev)) {
Auke Kok9d5c8242008-01-24 02:22:38 -08003542 if (adapter->itr_setting & 3)
3543 igb_set_itr(adapter, E1000_ITR, false);
3544 netif_rx_complete(netdev, napi);
3545 if (!test_bit(__IGB_DOWN, &adapter->state))
3546 igb_irq_enable(adapter);
3547 return 0;
3548 }
3549
3550 return 1;
3551}
3552
3553static int igb_clean_rx_ring_msix(struct napi_struct *napi, int budget)
3554{
3555 struct igb_ring *rx_ring = container_of(napi, struct igb_ring, napi);
3556 struct igb_adapter *adapter = rx_ring->adapter;
3557 struct e1000_hw *hw = &adapter->hw;
3558 struct net_device *netdev = adapter->netdev;
3559 int work_done = 0;
3560
3561 /* Keep link state information with original netdev */
3562 if (!netif_carrier_ok(netdev))
3563 goto quit_polling;
3564
Jeb Cramerfe4506b2008-07-08 15:07:55 -07003565#ifdef CONFIG_DCA
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07003566 if (adapter->flags & IGB_FLAG_DCA_ENABLED)
Jeb Cramerfe4506b2008-07-08 15:07:55 -07003567 igb_update_rx_dca(rx_ring);
3568#endif
Mitch Williams3b644cf2008-06-27 10:59:48 -07003569 igb_clean_rx_irq_adv(rx_ring, &work_done, budget);
Auke Kok9d5c8242008-01-24 02:22:38 -08003570
3571
3572 /* If not enough Rx work done, exit the polling mode */
3573 if ((work_done == 0) || !netif_running(netdev)) {
3574quit_polling:
3575 netif_rx_complete(netdev, napi);
3576
3577 wr32(E1000_EIMS, rx_ring->eims_value);
3578 if ((adapter->itr_setting & 3) && !rx_ring->no_itr_adjust &&
3579 (rx_ring->total_packets > IGB_DYN_ITR_PACKET_THRESHOLD)) {
3580 int mean_size = rx_ring->total_bytes /
3581 rx_ring->total_packets;
3582 if (mean_size < IGB_DYN_ITR_LENGTH_LOW)
3583 igb_raise_rx_eitr(adapter, rx_ring);
3584 else if (mean_size > IGB_DYN_ITR_LENGTH_HIGH)
3585 igb_lower_rx_eitr(adapter, rx_ring);
3586 }
PJ Waskiewicz844290e2008-06-27 11:00:39 -07003587
3588 if (!test_bit(__IGB_DOWN, &adapter->state))
3589 wr32(E1000_EIMS, rx_ring->eims_value);
3590
Auke Kok9d5c8242008-01-24 02:22:38 -08003591 return 0;
3592 }
3593
3594 return 1;
3595}
Al Viro6d8126f2008-03-16 22:23:24 +00003596
3597static inline u32 get_head(struct igb_ring *tx_ring)
3598{
3599 void *end = (struct e1000_tx_desc *)tx_ring->desc + tx_ring->count;
3600 return le32_to_cpu(*(volatile __le32 *)end);
3601}
3602
Auke Kok9d5c8242008-01-24 02:22:38 -08003603/**
3604 * igb_clean_tx_irq - Reclaim resources after transmit completes
3605 * @adapter: board private structure
3606 * returns true if ring is completely cleaned
3607 **/
Mitch Williams3b644cf2008-06-27 10:59:48 -07003608static bool igb_clean_tx_irq(struct igb_ring *tx_ring)
Auke Kok9d5c8242008-01-24 02:22:38 -08003609{
Mitch Williams3b644cf2008-06-27 10:59:48 -07003610 struct igb_adapter *adapter = tx_ring->adapter;
Auke Kok9d5c8242008-01-24 02:22:38 -08003611 struct e1000_hw *hw = &adapter->hw;
Mitch Williams3b644cf2008-06-27 10:59:48 -07003612 struct net_device *netdev = adapter->netdev;
Auke Kok9d5c8242008-01-24 02:22:38 -08003613 struct e1000_tx_desc *tx_desc;
3614 struct igb_buffer *buffer_info;
3615 struct sk_buff *skb;
3616 unsigned int i;
3617 u32 head, oldhead;
3618 unsigned int count = 0;
3619 bool cleaned = false;
3620 bool retval = true;
3621 unsigned int total_bytes = 0, total_packets = 0;
3622
3623 rmb();
Al Viro6d8126f2008-03-16 22:23:24 +00003624 head = get_head(tx_ring);
Auke Kok9d5c8242008-01-24 02:22:38 -08003625 i = tx_ring->next_to_clean;
3626 while (1) {
3627 while (i != head) {
3628 cleaned = true;
3629 tx_desc = E1000_TX_DESC(*tx_ring, i);
3630 buffer_info = &tx_ring->buffer_info[i];
3631 skb = buffer_info->skb;
3632
3633 if (skb) {
3634 unsigned int segs, bytecount;
3635 /* gso_segs is currently only valid for tcp */
3636 segs = skb_shinfo(skb)->gso_segs ?: 1;
3637 /* multiply data chunks by size of headers */
3638 bytecount = ((segs - 1) * skb_headlen(skb)) +
3639 skb->len;
3640 total_packets += segs;
3641 total_bytes += bytecount;
3642 }
3643
3644 igb_unmap_and_free_tx_resource(adapter, buffer_info);
3645 tx_desc->upper.data = 0;
3646
3647 i++;
3648 if (i == tx_ring->count)
3649 i = 0;
3650
3651 count++;
3652 if (count == IGB_MAX_TX_CLEAN) {
3653 retval = false;
3654 goto done_cleaning;
3655 }
3656 }
3657 oldhead = head;
3658 rmb();
Al Viro6d8126f2008-03-16 22:23:24 +00003659 head = get_head(tx_ring);
Auke Kok9d5c8242008-01-24 02:22:38 -08003660 if (head == oldhead)
3661 goto done_cleaning;
3662 } /* while (1) */
3663
3664done_cleaning:
3665 tx_ring->next_to_clean = i;
3666
3667 if (unlikely(cleaned &&
3668 netif_carrier_ok(netdev) &&
3669 IGB_DESC_UNUSED(tx_ring) >= IGB_TX_QUEUE_WAKE)) {
3670 /* Make sure that anybody stopping the queue after this
3671 * sees the new next_to_clean.
3672 */
3673 smp_mb();
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07003674#ifdef CONFIG_NETDEVICES_MULTIQUEUE
3675 if (__netif_subqueue_stopped(netdev, tx_ring->queue_index) &&
3676 !(test_bit(__IGB_DOWN, &adapter->state))) {
3677 netif_wake_subqueue(netdev, tx_ring->queue_index);
3678 ++adapter->restart_queue;
3679 }
3680#else
Auke Kok9d5c8242008-01-24 02:22:38 -08003681 if (netif_queue_stopped(netdev) &&
3682 !(test_bit(__IGB_DOWN, &adapter->state))) {
3683 netif_wake_queue(netdev);
3684 ++adapter->restart_queue;
3685 }
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07003686#endif
Auke Kok9d5c8242008-01-24 02:22:38 -08003687 }
3688
3689 if (tx_ring->detect_tx_hung) {
3690 /* Detect a transmit hang in hardware, this serializes the
3691 * check with the clearing of time_stamp and movement of i */
3692 tx_ring->detect_tx_hung = false;
3693 if (tx_ring->buffer_info[i].time_stamp &&
3694 time_after(jiffies, tx_ring->buffer_info[i].time_stamp +
3695 (adapter->tx_timeout_factor * HZ))
3696 && !(rd32(E1000_STATUS) &
3697 E1000_STATUS_TXOFF)) {
3698
3699 tx_desc = E1000_TX_DESC(*tx_ring, i);
3700 /* detected Tx unit hang */
3701 dev_err(&adapter->pdev->dev,
3702 "Detected Tx Unit Hang\n"
Alexander Duyck2d064c02008-07-08 15:10:12 -07003703 " Tx Queue <%d>\n"
Auke Kok9d5c8242008-01-24 02:22:38 -08003704 " TDH <%x>\n"
3705 " TDT <%x>\n"
3706 " next_to_use <%x>\n"
3707 " next_to_clean <%x>\n"
3708 " head (WB) <%x>\n"
3709 "buffer_info[next_to_clean]\n"
3710 " time_stamp <%lx>\n"
3711 " jiffies <%lx>\n"
3712 " desc.status <%x>\n",
Alexander Duyck2d064c02008-07-08 15:10:12 -07003713 tx_ring->queue_index,
Auke Kok9d5c8242008-01-24 02:22:38 -08003714 readl(adapter->hw.hw_addr + tx_ring->head),
3715 readl(adapter->hw.hw_addr + tx_ring->tail),
3716 tx_ring->next_to_use,
3717 tx_ring->next_to_clean,
3718 head,
3719 tx_ring->buffer_info[i].time_stamp,
3720 jiffies,
3721 tx_desc->upper.fields.status);
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07003722#ifdef CONFIG_NETDEVICES_MULTIQUEUE
3723 netif_stop_subqueue(netdev, tx_ring->queue_index);
3724#else
Auke Kok9d5c8242008-01-24 02:22:38 -08003725 netif_stop_queue(netdev);
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07003726#endif
Auke Kok9d5c8242008-01-24 02:22:38 -08003727 }
3728 }
3729 tx_ring->total_bytes += total_bytes;
3730 tx_ring->total_packets += total_packets;
Alexander Duycke21ed352008-07-08 15:07:24 -07003731 tx_ring->tx_stats.bytes += total_bytes;
3732 tx_ring->tx_stats.packets += total_packets;
Auke Kok9d5c8242008-01-24 02:22:38 -08003733 adapter->net_stats.tx_bytes += total_bytes;
3734 adapter->net_stats.tx_packets += total_packets;
3735 return retval;
3736}
3737
3738
3739/**
3740 * igb_receive_skb - helper function to handle rx indications
3741 * @adapter: board private structure
3742 * @status: descriptor status field as written by hardware
3743 * @vlan: descriptor vlan field as written by hardware (no le/be conversion)
3744 * @skb: pointer to sk_buff to be indicated to stack
3745 **/
Al Viro6d8126f2008-03-16 22:23:24 +00003746static void igb_receive_skb(struct igb_adapter *adapter, u8 status, __le16 vlan,
Auke Kok9d5c8242008-01-24 02:22:38 -08003747 struct sk_buff *skb)
3748{
3749 if (adapter->vlgrp && (status & E1000_RXD_STAT_VP))
3750 vlan_hwaccel_receive_skb(skb, adapter->vlgrp,
Patrick McHardy38b22192008-07-06 20:48:41 -07003751 le16_to_cpu(vlan));
Auke Kok9d5c8242008-01-24 02:22:38 -08003752 else
3753 netif_receive_skb(skb);
3754}
3755
3756
3757static inline void igb_rx_checksum_adv(struct igb_adapter *adapter,
3758 u32 status_err, struct sk_buff *skb)
3759{
3760 skb->ip_summed = CHECKSUM_NONE;
3761
3762 /* Ignore Checksum bit is set or checksum is disabled through ethtool */
3763 if ((status_err & E1000_RXD_STAT_IXSM) || !adapter->rx_csum)
3764 return;
3765 /* TCP/UDP checksum error bit is set */
3766 if (status_err &
3767 (E1000_RXDEXT_STATERR_TCPE | E1000_RXDEXT_STATERR_IPE)) {
3768 /* let the stack verify checksum errors */
3769 adapter->hw_csum_err++;
3770 return;
3771 }
3772 /* It must be a TCP or UDP packet with a valid checksum */
3773 if (status_err & (E1000_RXD_STAT_TCPCS | E1000_RXD_STAT_UDPCS))
3774 skb->ip_summed = CHECKSUM_UNNECESSARY;
3775
3776 adapter->hw_csum_good++;
3777}
3778
Mitch Williams3b644cf2008-06-27 10:59:48 -07003779static bool igb_clean_rx_irq_adv(struct igb_ring *rx_ring,
3780 int *work_done, int budget)
Auke Kok9d5c8242008-01-24 02:22:38 -08003781{
Mitch Williams3b644cf2008-06-27 10:59:48 -07003782 struct igb_adapter *adapter = rx_ring->adapter;
Auke Kok9d5c8242008-01-24 02:22:38 -08003783 struct net_device *netdev = adapter->netdev;
3784 struct pci_dev *pdev = adapter->pdev;
3785 union e1000_adv_rx_desc *rx_desc , *next_rxd;
3786 struct igb_buffer *buffer_info , *next_buffer;
3787 struct sk_buff *skb;
Alexander Duyckbf36c1a2008-07-08 15:11:40 -07003788 unsigned int i;
Auke Kok9d5c8242008-01-24 02:22:38 -08003789 u32 length, hlen, staterr;
3790 bool cleaned = false;
3791 int cleaned_count = 0;
3792 unsigned int total_bytes = 0, total_packets = 0;
3793
3794 i = rx_ring->next_to_clean;
3795 rx_desc = E1000_RX_DESC_ADV(*rx_ring, i);
3796 staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
3797
3798 while (staterr & E1000_RXD_STAT_DD) {
3799 if (*work_done >= budget)
3800 break;
3801 (*work_done)++;
3802 buffer_info = &rx_ring->buffer_info[i];
3803
3804 /* HW will not DMA in data larger than the given buffer, even
3805 * if it parses the (NFS, of course) header to be larger. In
3806 * that case, it fills the header buffer and spills the rest
3807 * into the page.
3808 */
Al Viro7deb07b2008-03-16 22:43:06 +00003809 hlen = (le16_to_cpu(rx_desc->wb.lower.lo_dword.hdr_info) &
3810 E1000_RXDADV_HDRBUFLEN_MASK) >> E1000_RXDADV_HDRBUFLEN_SHIFT;
Auke Kok9d5c8242008-01-24 02:22:38 -08003811 if (hlen > adapter->rx_ps_hdr_size)
3812 hlen = adapter->rx_ps_hdr_size;
3813
3814 length = le16_to_cpu(rx_desc->wb.upper.length);
3815 cleaned = true;
3816 cleaned_count++;
3817
Alexander Duyckbf36c1a2008-07-08 15:11:40 -07003818 skb = buffer_info->skb;
3819 prefetch(skb->data - NET_IP_ALIGN);
3820 buffer_info->skb = NULL;
3821 if (!adapter->rx_ps_hdr_size) {
3822 pci_unmap_single(pdev, buffer_info->dma,
3823 adapter->rx_buffer_len +
3824 NET_IP_ALIGN,
3825 PCI_DMA_FROMDEVICE);
3826 skb_put(skb, length);
3827 goto send_up;
Auke Kok9d5c8242008-01-24 02:22:38 -08003828 }
3829
Alexander Duyckbf36c1a2008-07-08 15:11:40 -07003830 if (!skb_shinfo(skb)->nr_frags) {
3831 pci_unmap_single(pdev, buffer_info->dma,
3832 adapter->rx_ps_hdr_size +
3833 NET_IP_ALIGN,
3834 PCI_DMA_FROMDEVICE);
3835 skb_put(skb, hlen);
3836 }
3837
3838 if (length) {
Auke Kok9d5c8242008-01-24 02:22:38 -08003839 pci_unmap_page(pdev, buffer_info->page_dma,
Alexander Duyckbf36c1a2008-07-08 15:11:40 -07003840 PAGE_SIZE / 2, PCI_DMA_FROMDEVICE);
Auke Kok9d5c8242008-01-24 02:22:38 -08003841 buffer_info->page_dma = 0;
Alexander Duyckbf36c1a2008-07-08 15:11:40 -07003842
3843 skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags++,
3844 buffer_info->page,
3845 buffer_info->page_offset,
3846 length);
3847
3848 if ((adapter->rx_buffer_len > (PAGE_SIZE / 2)) ||
3849 (page_count(buffer_info->page) != 1))
3850 buffer_info->page = NULL;
3851 else
3852 get_page(buffer_info->page);
Auke Kok9d5c8242008-01-24 02:22:38 -08003853
3854 skb->len += length;
3855 skb->data_len += length;
Alexander Duyckbf36c1a2008-07-08 15:11:40 -07003856
Auke Kok9d5c8242008-01-24 02:22:38 -08003857 skb->truesize += length;
Auke Kok9d5c8242008-01-24 02:22:38 -08003858 }
3859send_up:
Auke Kok9d5c8242008-01-24 02:22:38 -08003860 i++;
3861 if (i == rx_ring->count)
3862 i = 0;
3863 next_rxd = E1000_RX_DESC_ADV(*rx_ring, i);
3864 prefetch(next_rxd);
3865 next_buffer = &rx_ring->buffer_info[i];
3866
Alexander Duyckbf36c1a2008-07-08 15:11:40 -07003867 if (!(staterr & E1000_RXD_STAT_EOP)) {
3868 buffer_info->skb = xchg(&next_buffer->skb, skb);
3869 buffer_info->dma = xchg(&next_buffer->dma, 0);
3870 goto next_desc;
3871 }
3872
Auke Kok9d5c8242008-01-24 02:22:38 -08003873 if (staterr & E1000_RXDEXT_ERR_FRAME_ERR_MASK) {
3874 dev_kfree_skb_irq(skb);
3875 goto next_desc;
3876 }
3877 rx_ring->no_itr_adjust |= (staterr & E1000_RXD_STAT_DYNINT);
3878
3879 total_bytes += skb->len;
3880 total_packets++;
3881
3882 igb_rx_checksum_adv(adapter, staterr, skb);
3883
3884 skb->protocol = eth_type_trans(skb, netdev);
3885
3886 igb_receive_skb(adapter, staterr, rx_desc->wb.upper.vlan, skb);
3887
3888 netdev->last_rx = jiffies;
3889
3890next_desc:
3891 rx_desc->wb.upper.status_error = 0;
3892
3893 /* return some buffers to hardware, one at a time is too slow */
3894 if (cleaned_count >= IGB_RX_BUFFER_WRITE) {
Mitch Williams3b644cf2008-06-27 10:59:48 -07003895 igb_alloc_rx_buffers_adv(rx_ring, cleaned_count);
Auke Kok9d5c8242008-01-24 02:22:38 -08003896 cleaned_count = 0;
3897 }
3898
3899 /* use prefetched values */
3900 rx_desc = next_rxd;
3901 buffer_info = next_buffer;
3902
3903 staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
3904 }
Alexander Duyckbf36c1a2008-07-08 15:11:40 -07003905
Auke Kok9d5c8242008-01-24 02:22:38 -08003906 rx_ring->next_to_clean = i;
3907 cleaned_count = IGB_DESC_UNUSED(rx_ring);
3908
3909 if (cleaned_count)
Mitch Williams3b644cf2008-06-27 10:59:48 -07003910 igb_alloc_rx_buffers_adv(rx_ring, cleaned_count);
Auke Kok9d5c8242008-01-24 02:22:38 -08003911
3912 rx_ring->total_packets += total_packets;
3913 rx_ring->total_bytes += total_bytes;
3914 rx_ring->rx_stats.packets += total_packets;
3915 rx_ring->rx_stats.bytes += total_bytes;
3916 adapter->net_stats.rx_bytes += total_bytes;
3917 adapter->net_stats.rx_packets += total_packets;
3918 return cleaned;
3919}
3920
3921
3922/**
3923 * igb_alloc_rx_buffers_adv - Replace used receive buffers; packet split
3924 * @adapter: address of board private structure
3925 **/
Mitch Williams3b644cf2008-06-27 10:59:48 -07003926static void igb_alloc_rx_buffers_adv(struct igb_ring *rx_ring,
Auke Kok9d5c8242008-01-24 02:22:38 -08003927 int cleaned_count)
3928{
Mitch Williams3b644cf2008-06-27 10:59:48 -07003929 struct igb_adapter *adapter = rx_ring->adapter;
Auke Kok9d5c8242008-01-24 02:22:38 -08003930 struct net_device *netdev = adapter->netdev;
3931 struct pci_dev *pdev = adapter->pdev;
3932 union e1000_adv_rx_desc *rx_desc;
3933 struct igb_buffer *buffer_info;
3934 struct sk_buff *skb;
3935 unsigned int i;
3936
3937 i = rx_ring->next_to_use;
3938 buffer_info = &rx_ring->buffer_info[i];
3939
3940 while (cleaned_count--) {
3941 rx_desc = E1000_RX_DESC_ADV(*rx_ring, i);
3942
Alexander Duyckbf36c1a2008-07-08 15:11:40 -07003943 if (adapter->rx_ps_hdr_size && !buffer_info->page_dma) {
Auke Kok9d5c8242008-01-24 02:22:38 -08003944 if (!buffer_info->page) {
Alexander Duyckbf36c1a2008-07-08 15:11:40 -07003945 buffer_info->page = alloc_page(GFP_ATOMIC);
3946 if (!buffer_info->page) {
3947 adapter->alloc_rx_buff_failed++;
3948 goto no_buffers;
3949 }
3950 buffer_info->page_offset = 0;
3951 } else {
3952 buffer_info->page_offset ^= PAGE_SIZE / 2;
Auke Kok9d5c8242008-01-24 02:22:38 -08003953 }
3954 buffer_info->page_dma =
3955 pci_map_page(pdev,
3956 buffer_info->page,
Alexander Duyckbf36c1a2008-07-08 15:11:40 -07003957 buffer_info->page_offset,
3958 PAGE_SIZE / 2,
Auke Kok9d5c8242008-01-24 02:22:38 -08003959 PCI_DMA_FROMDEVICE);
3960 }
3961
3962 if (!buffer_info->skb) {
3963 int bufsz;
3964
3965 if (adapter->rx_ps_hdr_size)
3966 bufsz = adapter->rx_ps_hdr_size;
3967 else
3968 bufsz = adapter->rx_buffer_len;
3969 bufsz += NET_IP_ALIGN;
3970 skb = netdev_alloc_skb(netdev, bufsz);
3971
3972 if (!skb) {
3973 adapter->alloc_rx_buff_failed++;
3974 goto no_buffers;
3975 }
3976
3977 /* Make buffer alignment 2 beyond a 16 byte boundary
3978 * this will result in a 16 byte aligned IP header after
3979 * the 14 byte MAC header is removed
3980 */
3981 skb_reserve(skb, NET_IP_ALIGN);
3982
3983 buffer_info->skb = skb;
3984 buffer_info->dma = pci_map_single(pdev, skb->data,
3985 bufsz,
3986 PCI_DMA_FROMDEVICE);
3987
3988 }
3989 /* Refresh the desc even if buffer_addrs didn't change because
3990 * each write-back erases this info. */
3991 if (adapter->rx_ps_hdr_size) {
3992 rx_desc->read.pkt_addr =
3993 cpu_to_le64(buffer_info->page_dma);
3994 rx_desc->read.hdr_addr = cpu_to_le64(buffer_info->dma);
3995 } else {
3996 rx_desc->read.pkt_addr =
3997 cpu_to_le64(buffer_info->dma);
3998 rx_desc->read.hdr_addr = 0;
3999 }
4000
4001 i++;
4002 if (i == rx_ring->count)
4003 i = 0;
4004 buffer_info = &rx_ring->buffer_info[i];
4005 }
4006
4007no_buffers:
4008 if (rx_ring->next_to_use != i) {
4009 rx_ring->next_to_use = i;
4010 if (i == 0)
4011 i = (rx_ring->count - 1);
4012 else
4013 i--;
4014
4015 /* Force memory writes to complete before letting h/w
4016 * know there are new descriptors to fetch. (Only
4017 * applicable for weak-ordered memory model archs,
4018 * such as IA-64). */
4019 wmb();
4020 writel(i, adapter->hw.hw_addr + rx_ring->tail);
4021 }
4022}
4023
4024/**
4025 * igb_mii_ioctl -
4026 * @netdev:
4027 * @ifreq:
4028 * @cmd:
4029 **/
4030static int igb_mii_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
4031{
4032 struct igb_adapter *adapter = netdev_priv(netdev);
4033 struct mii_ioctl_data *data = if_mii(ifr);
4034
4035 if (adapter->hw.phy.media_type != e1000_media_type_copper)
4036 return -EOPNOTSUPP;
4037
4038 switch (cmd) {
4039 case SIOCGMIIPHY:
4040 data->phy_id = adapter->hw.phy.addr;
4041 break;
4042 case SIOCGMIIREG:
4043 if (!capable(CAP_NET_ADMIN))
4044 return -EPERM;
4045 if (adapter->hw.phy.ops.read_phy_reg(&adapter->hw,
4046 data->reg_num
4047 & 0x1F, &data->val_out))
4048 return -EIO;
4049 break;
4050 case SIOCSMIIREG:
4051 default:
4052 return -EOPNOTSUPP;
4053 }
4054 return 0;
4055}
4056
4057/**
4058 * igb_ioctl -
4059 * @netdev:
4060 * @ifreq:
4061 * @cmd:
4062 **/
4063static int igb_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
4064{
4065 switch (cmd) {
4066 case SIOCGMIIPHY:
4067 case SIOCGMIIREG:
4068 case SIOCSMIIREG:
4069 return igb_mii_ioctl(netdev, ifr, cmd);
4070 default:
4071 return -EOPNOTSUPP;
4072 }
4073}
4074
4075static void igb_vlan_rx_register(struct net_device *netdev,
4076 struct vlan_group *grp)
4077{
4078 struct igb_adapter *adapter = netdev_priv(netdev);
4079 struct e1000_hw *hw = &adapter->hw;
4080 u32 ctrl, rctl;
4081
4082 igb_irq_disable(adapter);
4083 adapter->vlgrp = grp;
4084
4085 if (grp) {
4086 /* enable VLAN tag insert/strip */
4087 ctrl = rd32(E1000_CTRL);
4088 ctrl |= E1000_CTRL_VME;
4089 wr32(E1000_CTRL, ctrl);
4090
4091 /* enable VLAN receive filtering */
4092 rctl = rd32(E1000_RCTL);
4093 rctl |= E1000_RCTL_VFE;
4094 rctl &= ~E1000_RCTL_CFIEN;
4095 wr32(E1000_RCTL, rctl);
4096 igb_update_mng_vlan(adapter);
4097 wr32(E1000_RLPML,
4098 adapter->max_frame_size + VLAN_TAG_SIZE);
4099 } else {
4100 /* disable VLAN tag insert/strip */
4101 ctrl = rd32(E1000_CTRL);
4102 ctrl &= ~E1000_CTRL_VME;
4103 wr32(E1000_CTRL, ctrl);
4104
4105 /* disable VLAN filtering */
4106 rctl = rd32(E1000_RCTL);
4107 rctl &= ~E1000_RCTL_VFE;
4108 wr32(E1000_RCTL, rctl);
4109 if (adapter->mng_vlan_id != (u16)IGB_MNG_VLAN_NONE) {
4110 igb_vlan_rx_kill_vid(netdev, adapter->mng_vlan_id);
4111 adapter->mng_vlan_id = IGB_MNG_VLAN_NONE;
4112 }
4113 wr32(E1000_RLPML,
4114 adapter->max_frame_size);
4115 }
4116
4117 if (!test_bit(__IGB_DOWN, &adapter->state))
4118 igb_irq_enable(adapter);
4119}
4120
4121static void igb_vlan_rx_add_vid(struct net_device *netdev, u16 vid)
4122{
4123 struct igb_adapter *adapter = netdev_priv(netdev);
4124 struct e1000_hw *hw = &adapter->hw;
4125 u32 vfta, index;
4126
4127 if ((adapter->hw.mng_cookie.status &
4128 E1000_MNG_DHCP_COOKIE_STATUS_VLAN) &&
4129 (vid == adapter->mng_vlan_id))
4130 return;
4131 /* add VID to filter table */
4132 index = (vid >> 5) & 0x7F;
4133 vfta = array_rd32(E1000_VFTA, index);
4134 vfta |= (1 << (vid & 0x1F));
4135 igb_write_vfta(&adapter->hw, index, vfta);
4136}
4137
4138static void igb_vlan_rx_kill_vid(struct net_device *netdev, u16 vid)
4139{
4140 struct igb_adapter *adapter = netdev_priv(netdev);
4141 struct e1000_hw *hw = &adapter->hw;
4142 u32 vfta, index;
4143
4144 igb_irq_disable(adapter);
4145 vlan_group_set_device(adapter->vlgrp, vid, NULL);
4146
4147 if (!test_bit(__IGB_DOWN, &adapter->state))
4148 igb_irq_enable(adapter);
4149
4150 if ((adapter->hw.mng_cookie.status &
4151 E1000_MNG_DHCP_COOKIE_STATUS_VLAN) &&
4152 (vid == adapter->mng_vlan_id)) {
4153 /* release control to f/w */
4154 igb_release_hw_control(adapter);
4155 return;
4156 }
4157
4158 /* remove VID from filter table */
4159 index = (vid >> 5) & 0x7F;
4160 vfta = array_rd32(E1000_VFTA, index);
4161 vfta &= ~(1 << (vid & 0x1F));
4162 igb_write_vfta(&adapter->hw, index, vfta);
4163}
4164
4165static void igb_restore_vlan(struct igb_adapter *adapter)
4166{
4167 igb_vlan_rx_register(adapter->netdev, adapter->vlgrp);
4168
4169 if (adapter->vlgrp) {
4170 u16 vid;
4171 for (vid = 0; vid < VLAN_GROUP_ARRAY_LEN; vid++) {
4172 if (!vlan_group_get_device(adapter->vlgrp, vid))
4173 continue;
4174 igb_vlan_rx_add_vid(adapter->netdev, vid);
4175 }
4176 }
4177}
4178
4179int igb_set_spd_dplx(struct igb_adapter *adapter, u16 spddplx)
4180{
4181 struct e1000_mac_info *mac = &adapter->hw.mac;
4182
4183 mac->autoneg = 0;
4184
4185 /* Fiber NICs only allow 1000 gbps Full duplex */
4186 if ((adapter->hw.phy.media_type == e1000_media_type_fiber) &&
4187 spddplx != (SPEED_1000 + DUPLEX_FULL)) {
4188 dev_err(&adapter->pdev->dev,
4189 "Unsupported Speed/Duplex configuration\n");
4190 return -EINVAL;
4191 }
4192
4193 switch (spddplx) {
4194 case SPEED_10 + DUPLEX_HALF:
4195 mac->forced_speed_duplex = ADVERTISE_10_HALF;
4196 break;
4197 case SPEED_10 + DUPLEX_FULL:
4198 mac->forced_speed_duplex = ADVERTISE_10_FULL;
4199 break;
4200 case SPEED_100 + DUPLEX_HALF:
4201 mac->forced_speed_duplex = ADVERTISE_100_HALF;
4202 break;
4203 case SPEED_100 + DUPLEX_FULL:
4204 mac->forced_speed_duplex = ADVERTISE_100_FULL;
4205 break;
4206 case SPEED_1000 + DUPLEX_FULL:
4207 mac->autoneg = 1;
4208 adapter->hw.phy.autoneg_advertised = ADVERTISE_1000_FULL;
4209 break;
4210 case SPEED_1000 + DUPLEX_HALF: /* not supported */
4211 default:
4212 dev_err(&adapter->pdev->dev,
4213 "Unsupported Speed/Duplex configuration\n");
4214 return -EINVAL;
4215 }
4216 return 0;
4217}
4218
4219
4220static int igb_suspend(struct pci_dev *pdev, pm_message_t state)
4221{
4222 struct net_device *netdev = pci_get_drvdata(pdev);
4223 struct igb_adapter *adapter = netdev_priv(netdev);
4224 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck2d064c02008-07-08 15:10:12 -07004225 u32 ctrl, rctl, status;
Auke Kok9d5c8242008-01-24 02:22:38 -08004226 u32 wufc = adapter->wol;
4227#ifdef CONFIG_PM
4228 int retval = 0;
4229#endif
4230
4231 netif_device_detach(netdev);
4232
4233 if (netif_running(netdev)) {
4234 WARN_ON(test_bit(__IGB_RESETTING, &adapter->state));
4235 igb_down(adapter);
4236 igb_free_irq(adapter);
4237 }
4238
4239#ifdef CONFIG_PM
4240 retval = pci_save_state(pdev);
4241 if (retval)
4242 return retval;
4243#endif
4244
4245 status = rd32(E1000_STATUS);
4246 if (status & E1000_STATUS_LU)
4247 wufc &= ~E1000_WUFC_LNKC;
4248
4249 if (wufc) {
4250 igb_setup_rctl(adapter);
4251 igb_set_multi(netdev);
4252
4253 /* turn on all-multi mode if wake on multicast is enabled */
4254 if (wufc & E1000_WUFC_MC) {
4255 rctl = rd32(E1000_RCTL);
4256 rctl |= E1000_RCTL_MPE;
4257 wr32(E1000_RCTL, rctl);
4258 }
4259
4260 ctrl = rd32(E1000_CTRL);
4261 /* advertise wake from D3Cold */
4262 #define E1000_CTRL_ADVD3WUC 0x00100000
4263 /* phy power management enable */
4264 #define E1000_CTRL_EN_PHY_PWR_MGMT 0x00200000
4265 ctrl |= E1000_CTRL_ADVD3WUC;
4266 wr32(E1000_CTRL, ctrl);
4267
Auke Kok9d5c8242008-01-24 02:22:38 -08004268 /* Allow time for pending master requests to run */
4269 igb_disable_pcie_master(&adapter->hw);
4270
4271 wr32(E1000_WUC, E1000_WUC_PME_EN);
4272 wr32(E1000_WUFC, wufc);
Auke Kok9d5c8242008-01-24 02:22:38 -08004273 } else {
4274 wr32(E1000_WUC, 0);
4275 wr32(E1000_WUFC, 0);
Auke Kok9d5c8242008-01-24 02:22:38 -08004276 }
4277
Alexander Duyck2d064c02008-07-08 15:10:12 -07004278 /* make sure adapter isn't asleep if manageability/wol is enabled */
4279 if (wufc || adapter->en_mng_pt) {
Auke Kok9d5c8242008-01-24 02:22:38 -08004280 pci_enable_wake(pdev, PCI_D3hot, 1);
4281 pci_enable_wake(pdev, PCI_D3cold, 1);
Alexander Duyck2d064c02008-07-08 15:10:12 -07004282 } else {
4283 igb_shutdown_fiber_serdes_link_82575(hw);
4284 pci_enable_wake(pdev, PCI_D3hot, 0);
4285 pci_enable_wake(pdev, PCI_D3cold, 0);
Auke Kok9d5c8242008-01-24 02:22:38 -08004286 }
4287
4288 /* Release control of h/w to f/w. If f/w is AMT enabled, this
4289 * would have already happened in close and is redundant. */
4290 igb_release_hw_control(adapter);
4291
4292 pci_disable_device(pdev);
4293
4294 pci_set_power_state(pdev, pci_choose_state(pdev, state));
4295
4296 return 0;
4297}
4298
4299#ifdef CONFIG_PM
4300static int igb_resume(struct pci_dev *pdev)
4301{
4302 struct net_device *netdev = pci_get_drvdata(pdev);
4303 struct igb_adapter *adapter = netdev_priv(netdev);
4304 struct e1000_hw *hw = &adapter->hw;
4305 u32 err;
4306
4307 pci_set_power_state(pdev, PCI_D0);
4308 pci_restore_state(pdev);
Taku Izumi42bfd33a2008-06-20 12:10:30 +09004309
4310 if (adapter->need_ioport)
4311 err = pci_enable_device(pdev);
4312 else
4313 err = pci_enable_device_mem(pdev);
Auke Kok9d5c8242008-01-24 02:22:38 -08004314 if (err) {
4315 dev_err(&pdev->dev,
4316 "igb: Cannot enable PCI device from suspend\n");
4317 return err;
4318 }
4319 pci_set_master(pdev);
4320
4321 pci_enable_wake(pdev, PCI_D3hot, 0);
4322 pci_enable_wake(pdev, PCI_D3cold, 0);
4323
4324 if (netif_running(netdev)) {
4325 err = igb_request_irq(adapter);
4326 if (err)
4327 return err;
4328 }
4329
4330 /* e1000_power_up_phy(adapter); */
4331
4332 igb_reset(adapter);
4333 wr32(E1000_WUS, ~0);
4334
4335 igb_init_manageability(adapter);
4336
4337 if (netif_running(netdev))
4338 igb_up(adapter);
4339
4340 netif_device_attach(netdev);
4341
4342 /* let the f/w know that the h/w is now under the control of the
4343 * driver. */
4344 igb_get_hw_control(adapter);
4345
4346 return 0;
4347}
4348#endif
4349
4350static void igb_shutdown(struct pci_dev *pdev)
4351{
4352 igb_suspend(pdev, PMSG_SUSPEND);
4353}
4354
4355#ifdef CONFIG_NET_POLL_CONTROLLER
4356/*
4357 * Polling 'interrupt' - used by things like netconsole to send skbs
4358 * without having to re-enable interrupts. It's not called while
4359 * the interrupt routine is executing.
4360 */
4361static void igb_netpoll(struct net_device *netdev)
4362{
4363 struct igb_adapter *adapter = netdev_priv(netdev);
4364 int i;
4365 int work_done = 0;
4366
4367 igb_irq_disable(adapter);
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07004368 adapter->flags |= IGB_FLAG_IN_NETPOLL;
4369
Auke Kok9d5c8242008-01-24 02:22:38 -08004370 for (i = 0; i < adapter->num_tx_queues; i++)
Mitch Williams3b644cf2008-06-27 10:59:48 -07004371 igb_clean_tx_irq(&adapter->tx_ring[i]);
Auke Kok9d5c8242008-01-24 02:22:38 -08004372
4373 for (i = 0; i < adapter->num_rx_queues; i++)
Mitch Williams3b644cf2008-06-27 10:59:48 -07004374 igb_clean_rx_irq_adv(&adapter->rx_ring[i],
Auke Kok9d5c8242008-01-24 02:22:38 -08004375 &work_done,
4376 adapter->rx_ring[i].napi.weight);
4377
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07004378 adapter->flags &= ~IGB_FLAG_IN_NETPOLL;
Auke Kok9d5c8242008-01-24 02:22:38 -08004379 igb_irq_enable(adapter);
4380}
4381#endif /* CONFIG_NET_POLL_CONTROLLER */
4382
4383/**
4384 * igb_io_error_detected - called when PCI error is detected
4385 * @pdev: Pointer to PCI device
4386 * @state: The current pci connection state
4387 *
4388 * This function is called after a PCI bus error affecting
4389 * this device has been detected.
4390 */
4391static pci_ers_result_t igb_io_error_detected(struct pci_dev *pdev,
4392 pci_channel_state_t state)
4393{
4394 struct net_device *netdev = pci_get_drvdata(pdev);
4395 struct igb_adapter *adapter = netdev_priv(netdev);
4396
4397 netif_device_detach(netdev);
4398
4399 if (netif_running(netdev))
4400 igb_down(adapter);
4401 pci_disable_device(pdev);
4402
4403 /* Request a slot slot reset. */
4404 return PCI_ERS_RESULT_NEED_RESET;
4405}
4406
4407/**
4408 * igb_io_slot_reset - called after the pci bus has been reset.
4409 * @pdev: Pointer to PCI device
4410 *
4411 * Restart the card from scratch, as if from a cold-boot. Implementation
4412 * resembles the first-half of the igb_resume routine.
4413 */
4414static pci_ers_result_t igb_io_slot_reset(struct pci_dev *pdev)
4415{
4416 struct net_device *netdev = pci_get_drvdata(pdev);
4417 struct igb_adapter *adapter = netdev_priv(netdev);
4418 struct e1000_hw *hw = &adapter->hw;
Taku Izumi42bfd33a2008-06-20 12:10:30 +09004419 int err;
Auke Kok9d5c8242008-01-24 02:22:38 -08004420
Taku Izumi42bfd33a2008-06-20 12:10:30 +09004421 if (adapter->need_ioport)
4422 err = pci_enable_device(pdev);
4423 else
4424 err = pci_enable_device_mem(pdev);
4425 if (err) {
Auke Kok9d5c8242008-01-24 02:22:38 -08004426 dev_err(&pdev->dev,
4427 "Cannot re-enable PCI device after reset.\n");
4428 return PCI_ERS_RESULT_DISCONNECT;
4429 }
4430 pci_set_master(pdev);
Auke Kokc682fc22008-04-23 11:09:34 -07004431 pci_restore_state(pdev);
Auke Kok9d5c8242008-01-24 02:22:38 -08004432
4433 pci_enable_wake(pdev, PCI_D3hot, 0);
4434 pci_enable_wake(pdev, PCI_D3cold, 0);
4435
4436 igb_reset(adapter);
4437 wr32(E1000_WUS, ~0);
4438
4439 return PCI_ERS_RESULT_RECOVERED;
4440}
4441
4442/**
4443 * igb_io_resume - called when traffic can start flowing again.
4444 * @pdev: Pointer to PCI device
4445 *
4446 * This callback is called when the error recovery driver tells us that
4447 * its OK to resume normal operation. Implementation resembles the
4448 * second-half of the igb_resume routine.
4449 */
4450static void igb_io_resume(struct pci_dev *pdev)
4451{
4452 struct net_device *netdev = pci_get_drvdata(pdev);
4453 struct igb_adapter *adapter = netdev_priv(netdev);
4454
4455 igb_init_manageability(adapter);
4456
4457 if (netif_running(netdev)) {
4458 if (igb_up(adapter)) {
4459 dev_err(&pdev->dev, "igb_up failed after reset\n");
4460 return;
4461 }
4462 }
4463
4464 netif_device_attach(netdev);
4465
4466 /* let the f/w know that the h/w is now under the control of the
4467 * driver. */
4468 igb_get_hw_control(adapter);
4469
4470}
4471
4472/* igb_main.c */