blob: 61ef4c2c4fca3b531284356300957478092bca65 [file] [log] [blame]
Auke Kok9d5c8242008-01-24 02:22:38 -08001/*******************************************************************************
2
3 Intel(R) Gigabit Ethernet Linux driver
Alexander Duyck86d5d382009-02-06 23:23:12 +00004 Copyright(c) 2007-2009 Intel Corporation.
Auke Kok9d5c8242008-01-24 02:22:38 -08005
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>
Patrick Ohlyc6cb0902009-02-12 05:03:42 +000037#include <linux/net_tstamp.h>
Auke Kok9d5c8242008-01-24 02:22:38 -080038#include <linux/mii.h>
39#include <linux/ethtool.h>
40#include <linux/if_vlan.h>
41#include <linux/pci.h>
Alexander Duyckc54106b2008-10-16 21:26:57 -070042#include <linux/pci-aspm.h>
Auke Kok9d5c8242008-01-24 02:22:38 -080043#include <linux/delay.h>
44#include <linux/interrupt.h>
45#include <linux/if_ether.h>
Alexander Duyck40a914f2008-11-27 00:24:37 -080046#include <linux/aer.h>
Jeff Kirsher421e02f2008-10-17 11:08:31 -070047#ifdef CONFIG_IGB_DCA
Jeb Cramerfe4506b2008-07-08 15:07:55 -070048#include <linux/dca.h>
49#endif
Auke Kok9d5c8242008-01-24 02:22:38 -080050#include "igb.h"
51
Alexander Duyck86d5d382009-02-06 23:23:12 +000052#define DRV_VERSION "1.3.16-k2"
Auke Kok9d5c8242008-01-24 02:22:38 -080053char igb_driver_name[] = "igb";
54char igb_driver_version[] = DRV_VERSION;
55static const char igb_driver_string[] =
56 "Intel(R) Gigabit Ethernet Network Driver";
Alexander Duyck86d5d382009-02-06 23:23:12 +000057static const char igb_copyright[] = "Copyright (c) 2007-2009 Intel Corporation.";
Auke Kok9d5c8242008-01-24 02:22:38 -080058
Auke Kok9d5c8242008-01-24 02:22:38 -080059static const struct e1000_info *igb_info_tbl[] = {
60 [board_82575] = &e1000_82575_info,
61};
62
63static struct pci_device_id igb_pci_tbl[] = {
Alexander Duyck2d064c02008-07-08 15:10:12 -070064 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576), board_82575 },
Alexander Duyck9eb23412009-03-13 20:42:15 +000065 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_NS), board_82575 },
Alexander Duyck747d49b2009-10-05 06:33:27 +000066 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_NS_SERDES), board_82575 },
Alexander Duyck2d064c02008-07-08 15:10:12 -070067 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_FIBER), board_82575 },
68 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_SERDES), board_82575 },
Alexander Duyck4703bf72009-07-23 18:09:48 +000069 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_SERDES_QUAD), board_82575 },
Alexander Duyckc8ea5ea2009-03-13 20:42:35 +000070 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_QUAD_COPPER), board_82575 },
Auke Kok9d5c8242008-01-24 02:22:38 -080071 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82575EB_COPPER), board_82575 },
72 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82575EB_FIBER_SERDES), board_82575 },
73 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82575GB_QUAD_COPPER), board_82575 },
74 /* required last entry */
75 {0, }
76};
77
78MODULE_DEVICE_TABLE(pci, igb_pci_tbl);
79
80void igb_reset(struct igb_adapter *);
81static int igb_setup_all_tx_resources(struct igb_adapter *);
82static int igb_setup_all_rx_resources(struct igb_adapter *);
83static void igb_free_all_tx_resources(struct igb_adapter *);
84static void igb_free_all_rx_resources(struct igb_adapter *);
Auke Kok9d5c8242008-01-24 02:22:38 -080085void igb_update_stats(struct igb_adapter *);
86static int igb_probe(struct pci_dev *, const struct pci_device_id *);
87static void __devexit igb_remove(struct pci_dev *pdev);
88static int igb_sw_init(struct igb_adapter *);
89static int igb_open(struct net_device *);
90static int igb_close(struct net_device *);
91static void igb_configure_tx(struct igb_adapter *);
92static void igb_configure_rx(struct igb_adapter *);
Alexander Duyck85b430b2009-10-27 15:50:29 +000093static void igb_setup_tctl(struct igb_adapter *);
Auke Kok9d5c8242008-01-24 02:22:38 -080094static void igb_setup_rctl(struct igb_adapter *);
95static void igb_clean_all_tx_rings(struct igb_adapter *);
96static void igb_clean_all_rx_rings(struct igb_adapter *);
Mitch Williams3b644cf2008-06-27 10:59:48 -070097static void igb_clean_tx_ring(struct igb_ring *);
98static void igb_clean_rx_ring(struct igb_ring *);
Alexander Duyckff41f8d2009-09-03 14:48:56 +000099static void igb_set_rx_mode(struct net_device *);
Auke Kok9d5c8242008-01-24 02:22:38 -0800100static void igb_update_phy_info(unsigned long);
101static void igb_watchdog(unsigned long);
102static void igb_watchdog_task(struct work_struct *);
Stephen Hemminger3b29a562009-08-31 19:50:55 +0000103static netdev_tx_t igb_xmit_frame_ring_adv(struct sk_buff *,
104 struct net_device *,
105 struct igb_ring *);
106static netdev_tx_t igb_xmit_frame_adv(struct sk_buff *skb,
107 struct net_device *);
Auke Kok9d5c8242008-01-24 02:22:38 -0800108static struct net_device_stats *igb_get_stats(struct net_device *);
109static int igb_change_mtu(struct net_device *, int);
110static int igb_set_mac(struct net_device *, void *);
Alexander Duyck68d480c2009-10-05 06:33:08 +0000111static void igb_set_uta(struct igb_adapter *adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -0800112static irqreturn_t igb_intr(int irq, void *);
113static irqreturn_t igb_intr_msi(int irq, void *);
114static irqreturn_t igb_msix_other(int irq, void *);
Alexander Duyck047e0032009-10-27 15:49:27 +0000115static irqreturn_t igb_msix_ring(int irq, void *);
Jeff Kirsher421e02f2008-10-17 11:08:31 -0700116#ifdef CONFIG_IGB_DCA
Alexander Duyck047e0032009-10-27 15:49:27 +0000117static void igb_update_dca(struct igb_q_vector *);
Jeb Cramerfe4506b2008-07-08 15:07:55 -0700118static void igb_setup_dca(struct igb_adapter *);
Jeff Kirsher421e02f2008-10-17 11:08:31 -0700119#endif /* CONFIG_IGB_DCA */
Alexander Duyck047e0032009-10-27 15:49:27 +0000120static bool igb_clean_tx_irq(struct igb_q_vector *);
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -0700121static int igb_poll(struct napi_struct *, int);
Alexander Duyck047e0032009-10-27 15:49:27 +0000122static bool igb_clean_rx_irq_adv(struct igb_q_vector *, int *, int);
Mitch Williams3b644cf2008-06-27 10:59:48 -0700123static void igb_alloc_rx_buffers_adv(struct igb_ring *, int);
Auke Kok9d5c8242008-01-24 02:22:38 -0800124static int igb_ioctl(struct net_device *, struct ifreq *, int cmd);
125static void igb_tx_timeout(struct net_device *);
126static void igb_reset_task(struct work_struct *);
127static void igb_vlan_rx_register(struct net_device *, struct vlan_group *);
128static void igb_vlan_rx_add_vid(struct net_device *, u16);
129static void igb_vlan_rx_kill_vid(struct net_device *, u16);
130static void igb_restore_vlan(struct igb_adapter *);
Alexander Duyck26ad9172009-10-05 06:32:49 +0000131static void igb_rar_set_qsel(struct igb_adapter *, u8 *, u32 , u8);
Alexander Duyck4ae196d2009-02-19 20:40:07 -0800132static void igb_ping_all_vfs(struct igb_adapter *);
133static void igb_msg_task(struct igb_adapter *);
134static int igb_rcv_msg_from_vf(struct igb_adapter *, u32);
Alexander Duyck4ae196d2009-02-19 20:40:07 -0800135static void igb_vmm_control(struct igb_adapter *);
Alexander Duyck4ae196d2009-02-19 20:40:07 -0800136static int igb_set_vf_mac(struct igb_adapter *adapter, int, unsigned char *);
137static void igb_restore_vf_multicasts(struct igb_adapter *adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -0800138
Eric Dumazetc8159b22009-07-13 11:11:41 -0700139static inline void igb_set_vmolr(struct e1000_hw *hw, int vfn)
140{
141 u32 reg_data;
142
143 reg_data = rd32(E1000_VMOLR(vfn));
144 reg_data |= E1000_VMOLR_BAM | /* Accept broadcast */
Eric Dumazetc8159b22009-07-13 11:11:41 -0700145 E1000_VMOLR_ROMPE | /* Accept packets matched in MTA */
146 E1000_VMOLR_AUPE | /* Accept untagged packets */
147 E1000_VMOLR_STRVLAN; /* Strip vlan tags */
148 wr32(E1000_VMOLR(vfn), reg_data);
149}
150
151static inline int igb_set_vf_rlpml(struct igb_adapter *adapter, int size,
152 int vfn)
153{
154 struct e1000_hw *hw = &adapter->hw;
155 u32 vmolr;
156
Alexander Duyckae641bd2009-09-03 14:49:33 +0000157 /* if it isn't the PF check to see if VFs are enabled and
158 * increase the size to support vlan tags */
159 if (vfn < adapter->vfs_allocated_count &&
160 adapter->vf_data[vfn].vlans_enabled)
161 size += VLAN_TAG_SIZE;
162
Eric Dumazetc8159b22009-07-13 11:11:41 -0700163 vmolr = rd32(E1000_VMOLR(vfn));
164 vmolr &= ~E1000_VMOLR_RLPML_MASK;
165 vmolr |= size | E1000_VMOLR_LPE;
166 wr32(E1000_VMOLR(vfn), vmolr);
167
168 return 0;
169}
170
Auke Kok9d5c8242008-01-24 02:22:38 -0800171#ifdef CONFIG_PM
Rafael J. Wysocki3fe7c4c2009-03-31 21:23:50 +0000172static int igb_suspend(struct pci_dev *, pm_message_t);
Auke Kok9d5c8242008-01-24 02:22:38 -0800173static int igb_resume(struct pci_dev *);
174#endif
175static void igb_shutdown(struct pci_dev *);
Jeff Kirsher421e02f2008-10-17 11:08:31 -0700176#ifdef CONFIG_IGB_DCA
Jeb Cramerfe4506b2008-07-08 15:07:55 -0700177static int igb_notify_dca(struct notifier_block *, unsigned long, void *);
178static struct notifier_block dca_notifier = {
179 .notifier_call = igb_notify_dca,
180 .next = NULL,
181 .priority = 0
182};
183#endif
Auke Kok9d5c8242008-01-24 02:22:38 -0800184#ifdef CONFIG_NET_POLL_CONTROLLER
185/* for netdump / net console */
186static void igb_netpoll(struct net_device *);
187#endif
Alexander Duyck37680112009-02-19 20:40:30 -0800188#ifdef CONFIG_PCI_IOV
Alexander Duyck2a3abf62009-04-07 14:37:52 +0000189static unsigned int max_vfs = 0;
190module_param(max_vfs, uint, 0);
191MODULE_PARM_DESC(max_vfs, "Maximum number of virtual functions to allocate "
192 "per physical function");
193#endif /* CONFIG_PCI_IOV */
194
Auke Kok9d5c8242008-01-24 02:22:38 -0800195static pci_ers_result_t igb_io_error_detected(struct pci_dev *,
196 pci_channel_state_t);
197static pci_ers_result_t igb_io_slot_reset(struct pci_dev *);
198static void igb_io_resume(struct pci_dev *);
199
200static struct pci_error_handlers igb_err_handler = {
201 .error_detected = igb_io_error_detected,
202 .slot_reset = igb_io_slot_reset,
203 .resume = igb_io_resume,
204};
205
206
207static struct pci_driver igb_driver = {
208 .name = igb_driver_name,
209 .id_table = igb_pci_tbl,
210 .probe = igb_probe,
211 .remove = __devexit_p(igb_remove),
212#ifdef CONFIG_PM
213 /* Power Managment Hooks */
214 .suspend = igb_suspend,
215 .resume = igb_resume,
216#endif
217 .shutdown = igb_shutdown,
218 .err_handler = &igb_err_handler
219};
220
Alexander Duyck7dfc16f2008-07-08 15:10:46 -0700221static int global_quad_port_a; /* global quad port a indication */
222
Auke Kok9d5c8242008-01-24 02:22:38 -0800223MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
224MODULE_DESCRIPTION("Intel(R) Gigabit Ethernet Network Driver");
225MODULE_LICENSE("GPL");
226MODULE_VERSION(DRV_VERSION);
227
Patrick Ohly38c845c2009-02-12 05:03:41 +0000228/**
229 * Scale the NIC clock cycle by a large factor so that
230 * relatively small clock corrections can be added or
231 * substracted at each clock tick. The drawbacks of a
232 * large factor are a) that the clock register overflows
233 * more quickly (not such a big deal) and b) that the
234 * increment per tick has to fit into 24 bits.
235 *
236 * Note that
237 * TIMINCA = IGB_TSYNC_CYCLE_TIME_IN_NANOSECONDS *
238 * IGB_TSYNC_SCALE
239 * TIMINCA += TIMINCA * adjustment [ppm] / 1e9
240 *
241 * The base scale factor is intentionally a power of two
242 * so that the division in %struct timecounter can be done with
243 * a shift.
244 */
245#define IGB_TSYNC_SHIFT (19)
246#define IGB_TSYNC_SCALE (1<<IGB_TSYNC_SHIFT)
247
248/**
249 * The duration of one clock cycle of the NIC.
250 *
251 * @todo This hard-coded value is part of the specification and might change
252 * in future hardware revisions. Add revision check.
253 */
254#define IGB_TSYNC_CYCLE_TIME_IN_NANOSECONDS 16
255
256#if (IGB_TSYNC_SCALE * IGB_TSYNC_CYCLE_TIME_IN_NANOSECONDS) >= (1<<24)
257# error IGB_TSYNC_SCALE and/or IGB_TSYNC_CYCLE_TIME_IN_NANOSECONDS are too large to fit into TIMINCA
258#endif
259
260/**
261 * igb_read_clock - read raw cycle counter (to be used by time counter)
262 */
263static cycle_t igb_read_clock(const struct cyclecounter *tc)
264{
265 struct igb_adapter *adapter =
266 container_of(tc, struct igb_adapter, cycles);
267 struct e1000_hw *hw = &adapter->hw;
268 u64 stamp;
269
270 stamp = rd32(E1000_SYSTIML);
271 stamp |= (u64)rd32(E1000_SYSTIMH) << 32ULL;
272
273 return stamp;
274}
275
Auke Kok9d5c8242008-01-24 02:22:38 -0800276#ifdef DEBUG
277/**
278 * igb_get_hw_dev_name - return device name string
279 * used by hardware layer to print debugging information
280 **/
281char *igb_get_hw_dev_name(struct e1000_hw *hw)
282{
283 struct igb_adapter *adapter = hw->back;
284 return adapter->netdev->name;
285}
Patrick Ohly38c845c2009-02-12 05:03:41 +0000286
287/**
288 * igb_get_time_str - format current NIC and system time as string
289 */
290static char *igb_get_time_str(struct igb_adapter *adapter,
291 char buffer[160])
292{
293 cycle_t hw = adapter->cycles.read(&adapter->cycles);
294 struct timespec nic = ns_to_timespec(timecounter_read(&adapter->clock));
295 struct timespec sys;
296 struct timespec delta;
297 getnstimeofday(&sys);
298
299 delta = timespec_sub(nic, sys);
300
301 sprintf(buffer,
Patrick Ohly33af6bc2009-02-12 05:03:43 +0000302 "HW %llu, NIC %ld.%09lus, SYS %ld.%09lus, NIC-SYS %lds + %09luns",
303 hw,
Patrick Ohly38c845c2009-02-12 05:03:41 +0000304 (long)nic.tv_sec, nic.tv_nsec,
305 (long)sys.tv_sec, sys.tv_nsec,
306 (long)delta.tv_sec, delta.tv_nsec);
307
308 return buffer;
309}
Auke Kok9d5c8242008-01-24 02:22:38 -0800310#endif
311
312/**
Alexander Duyckc493ea42009-03-20 00:16:50 +0000313 * igb_desc_unused - calculate if we have unused descriptors
314 **/
315static int igb_desc_unused(struct igb_ring *ring)
316{
317 if (ring->next_to_clean > ring->next_to_use)
318 return ring->next_to_clean - ring->next_to_use - 1;
319
320 return ring->count + ring->next_to_clean - ring->next_to_use - 1;
321}
322
323/**
Auke Kok9d5c8242008-01-24 02:22:38 -0800324 * igb_init_module - Driver Registration Routine
325 *
326 * igb_init_module is the first routine called when the driver is
327 * loaded. All it does is register with the PCI subsystem.
328 **/
329static int __init igb_init_module(void)
330{
331 int ret;
332 printk(KERN_INFO "%s - version %s\n",
333 igb_driver_string, igb_driver_version);
334
335 printk(KERN_INFO "%s\n", igb_copyright);
336
Alexander Duyck7dfc16f2008-07-08 15:10:46 -0700337 global_quad_port_a = 0;
338
Jeff Kirsher421e02f2008-10-17 11:08:31 -0700339#ifdef CONFIG_IGB_DCA
Jeb Cramerfe4506b2008-07-08 15:07:55 -0700340 dca_register_notify(&dca_notifier);
341#endif
Alexander Duyckbbd98fe2009-01-31 00:52:30 -0800342
343 ret = pci_register_driver(&igb_driver);
Auke Kok9d5c8242008-01-24 02:22:38 -0800344 return ret;
345}
346
347module_init(igb_init_module);
348
349/**
350 * igb_exit_module - Driver Exit Cleanup Routine
351 *
352 * igb_exit_module is called just before the driver is removed
353 * from memory.
354 **/
355static void __exit igb_exit_module(void)
356{
Jeff Kirsher421e02f2008-10-17 11:08:31 -0700357#ifdef CONFIG_IGB_DCA
Jeb Cramerfe4506b2008-07-08 15:07:55 -0700358 dca_unregister_notify(&dca_notifier);
359#endif
Auke Kok9d5c8242008-01-24 02:22:38 -0800360 pci_unregister_driver(&igb_driver);
361}
362
363module_exit(igb_exit_module);
364
Alexander Duyck26bc19e2008-12-26 01:34:11 -0800365#define Q_IDX_82576(i) (((i & 0x1) << 3) + (i >> 1))
366/**
367 * igb_cache_ring_register - Descriptor ring to register mapping
368 * @adapter: board private structure to initialize
369 *
370 * Once we know the feature-set enabled for the device, we'll cache
371 * the register offset the descriptor ring is assigned to.
372 **/
373static void igb_cache_ring_register(struct igb_adapter *adapter)
374{
375 int i;
Alexander Duyck047e0032009-10-27 15:49:27 +0000376 u32 rbase_offset = adapter->vfs_allocated_count;
Alexander Duyck26bc19e2008-12-26 01:34:11 -0800377
378 switch (adapter->hw.mac.type) {
379 case e1000_82576:
380 /* The queues are allocated for virtualization such that VF 0
381 * is allocated queues 0 and 8, VF 1 queues 1 and 9, etc.
382 * In order to avoid collision we start at the first free queue
383 * and continue consuming queues in the same sequence
384 */
385 for (i = 0; i < adapter->num_rx_queues; i++)
Alexander Duyck1bfaf072009-02-19 20:39:23 -0800386 adapter->rx_ring[i].reg_idx = rbase_offset +
387 Q_IDX_82576(i);
Alexander Duyck26bc19e2008-12-26 01:34:11 -0800388 for (i = 0; i < adapter->num_tx_queues; i++)
Alexander Duyck1bfaf072009-02-19 20:39:23 -0800389 adapter->tx_ring[i].reg_idx = rbase_offset +
390 Q_IDX_82576(i);
Alexander Duyck26bc19e2008-12-26 01:34:11 -0800391 break;
392 case e1000_82575:
393 default:
394 for (i = 0; i < adapter->num_rx_queues; i++)
395 adapter->rx_ring[i].reg_idx = i;
396 for (i = 0; i < adapter->num_tx_queues; i++)
397 adapter->tx_ring[i].reg_idx = i;
398 break;
399 }
400}
401
Alexander Duyck047e0032009-10-27 15:49:27 +0000402static void igb_free_queues(struct igb_adapter *adapter)
403{
404 kfree(adapter->tx_ring);
405 kfree(adapter->rx_ring);
406
407 adapter->tx_ring = NULL;
408 adapter->rx_ring = NULL;
409
410 adapter->num_rx_queues = 0;
411 adapter->num_tx_queues = 0;
412}
413
Auke Kok9d5c8242008-01-24 02:22:38 -0800414/**
415 * igb_alloc_queues - Allocate memory for all rings
416 * @adapter: board private structure to initialize
417 *
418 * We allocate one ring per queue at run-time since we don't know the
419 * number of queues at compile-time.
420 **/
421static int igb_alloc_queues(struct igb_adapter *adapter)
422{
423 int i;
424
425 adapter->tx_ring = kcalloc(adapter->num_tx_queues,
426 sizeof(struct igb_ring), GFP_KERNEL);
427 if (!adapter->tx_ring)
Alexander Duyck047e0032009-10-27 15:49:27 +0000428 goto err;
Auke Kok9d5c8242008-01-24 02:22:38 -0800429
430 adapter->rx_ring = kcalloc(adapter->num_rx_queues,
431 sizeof(struct igb_ring), GFP_KERNEL);
Alexander Duyck047e0032009-10-27 15:49:27 +0000432 if (!adapter->rx_ring)
433 goto err;
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -0700434
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -0700435 for (i = 0; i < adapter->num_tx_queues; i++) {
436 struct igb_ring *ring = &(adapter->tx_ring[i]);
Alexander Duyck68fd9912008-11-20 00:48:10 -0800437 ring->count = adapter->tx_ring_count;
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -0700438 ring->queue_index = i;
439 }
Auke Kok9d5c8242008-01-24 02:22:38 -0800440 for (i = 0; i < adapter->num_rx_queues; i++) {
441 struct igb_ring *ring = &(adapter->rx_ring[i]);
Alexander Duyck68fd9912008-11-20 00:48:10 -0800442 ring->count = adapter->rx_ring_count;
PJ Waskiewicz844290e2008-06-27 11:00:39 -0700443 ring->queue_index = i;
Auke Kok9d5c8242008-01-24 02:22:38 -0800444 }
Alexander Duyck26bc19e2008-12-26 01:34:11 -0800445
446 igb_cache_ring_register(adapter);
Alexander Duyck047e0032009-10-27 15:49:27 +0000447
Auke Kok9d5c8242008-01-24 02:22:38 -0800448 return 0;
Auke Kok9d5c8242008-01-24 02:22:38 -0800449
Alexander Duyck047e0032009-10-27 15:49:27 +0000450err:
451 igb_free_queues(adapter);
Alexander Duycka88f10e2008-07-08 15:13:38 -0700452
Alexander Duyck047e0032009-10-27 15:49:27 +0000453 return -ENOMEM;
Alexander Duycka88f10e2008-07-08 15:13:38 -0700454}
455
Auke Kok9d5c8242008-01-24 02:22:38 -0800456#define IGB_N0_QUEUE -1
Alexander Duyck047e0032009-10-27 15:49:27 +0000457static void igb_assign_vector(struct igb_q_vector *q_vector, int msix_vector)
Auke Kok9d5c8242008-01-24 02:22:38 -0800458{
459 u32 msixbm = 0;
Alexander Duyck047e0032009-10-27 15:49:27 +0000460 struct igb_adapter *adapter = q_vector->adapter;
Auke Kok9d5c8242008-01-24 02:22:38 -0800461 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck2d064c02008-07-08 15:10:12 -0700462 u32 ivar, index;
Alexander Duyck047e0032009-10-27 15:49:27 +0000463 int rx_queue = IGB_N0_QUEUE;
464 int tx_queue = IGB_N0_QUEUE;
465
466 if (q_vector->rx_ring)
467 rx_queue = q_vector->rx_ring->reg_idx;
468 if (q_vector->tx_ring)
469 tx_queue = q_vector->tx_ring->reg_idx;
Alexander Duyck2d064c02008-07-08 15:10:12 -0700470
471 switch (hw->mac.type) {
472 case e1000_82575:
Auke Kok9d5c8242008-01-24 02:22:38 -0800473 /* The 82575 assigns vectors using a bitmask, which matches the
474 bitmask for the EICR/EIMS/EIMC registers. To assign one
475 or more queues to a vector, we write the appropriate bits
476 into the MSIXBM register for that vector. */
Alexander Duyck047e0032009-10-27 15:49:27 +0000477 if (rx_queue > IGB_N0_QUEUE)
Auke Kok9d5c8242008-01-24 02:22:38 -0800478 msixbm = E1000_EICR_RX_QUEUE0 << rx_queue;
Alexander Duyck047e0032009-10-27 15:49:27 +0000479 if (tx_queue > IGB_N0_QUEUE)
Auke Kok9d5c8242008-01-24 02:22:38 -0800480 msixbm |= E1000_EICR_TX_QUEUE0 << tx_queue;
Auke Kok9d5c8242008-01-24 02:22:38 -0800481 array_wr32(E1000_MSIXBM(0), msix_vector, msixbm);
Alexander Duyck047e0032009-10-27 15:49:27 +0000482 q_vector->eims_value = msixbm;
Alexander Duyck2d064c02008-07-08 15:10:12 -0700483 break;
484 case e1000_82576:
Alexander Duyck26bc19e2008-12-26 01:34:11 -0800485 /* 82576 uses a table-based method for assigning vectors.
Alexander Duyck2d064c02008-07-08 15:10:12 -0700486 Each queue has a single entry in the table to which we write
487 a vector number along with a "valid" bit. Sadly, the layout
488 of the table is somewhat counterintuitive. */
489 if (rx_queue > IGB_N0_QUEUE) {
Alexander Duyck047e0032009-10-27 15:49:27 +0000490 index = (rx_queue & 0x7);
Alexander Duyck2d064c02008-07-08 15:10:12 -0700491 ivar = array_rd32(E1000_IVAR0, index);
Alexander Duyck047e0032009-10-27 15:49:27 +0000492 if (rx_queue < 8) {
Alexander Duyck26bc19e2008-12-26 01:34:11 -0800493 /* vector goes into low byte of register */
494 ivar = ivar & 0xFFFFFF00;
495 ivar |= msix_vector | E1000_IVAR_VALID;
Alexander Duyck047e0032009-10-27 15:49:27 +0000496 } else {
497 /* vector goes into third byte of register */
498 ivar = ivar & 0xFF00FFFF;
499 ivar |= (msix_vector | E1000_IVAR_VALID) << 16;
Alexander Duyck2d064c02008-07-08 15:10:12 -0700500 }
Alexander Duyck2d064c02008-07-08 15:10:12 -0700501 array_wr32(E1000_IVAR0, index, ivar);
502 }
503 if (tx_queue > IGB_N0_QUEUE) {
Alexander Duyck047e0032009-10-27 15:49:27 +0000504 index = (tx_queue & 0x7);
Alexander Duyck2d064c02008-07-08 15:10:12 -0700505 ivar = array_rd32(E1000_IVAR0, index);
Alexander Duyck047e0032009-10-27 15:49:27 +0000506 if (tx_queue < 8) {
Alexander Duyck26bc19e2008-12-26 01:34:11 -0800507 /* vector goes into second byte of register */
508 ivar = ivar & 0xFFFF00FF;
509 ivar |= (msix_vector | E1000_IVAR_VALID) << 8;
Alexander Duyck047e0032009-10-27 15:49:27 +0000510 } else {
511 /* vector goes into high byte of register */
512 ivar = ivar & 0x00FFFFFF;
513 ivar |= (msix_vector | E1000_IVAR_VALID) << 24;
Alexander Duyck2d064c02008-07-08 15:10:12 -0700514 }
Alexander Duyck2d064c02008-07-08 15:10:12 -0700515 array_wr32(E1000_IVAR0, index, ivar);
516 }
Alexander Duyck047e0032009-10-27 15:49:27 +0000517 q_vector->eims_value = 1 << msix_vector;
Alexander Duyck2d064c02008-07-08 15:10:12 -0700518 break;
519 default:
520 BUG();
521 break;
522 }
Auke Kok9d5c8242008-01-24 02:22:38 -0800523}
524
525/**
526 * igb_configure_msix - Configure MSI-X hardware
527 *
528 * igb_configure_msix sets up the hardware to properly
529 * generate MSI-X interrupts.
530 **/
531static void igb_configure_msix(struct igb_adapter *adapter)
532{
533 u32 tmp;
534 int i, vector = 0;
535 struct e1000_hw *hw = &adapter->hw;
536
537 adapter->eims_enable_mask = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -0800538
539 /* set vector for other causes, i.e. link changes */
Alexander Duyck2d064c02008-07-08 15:10:12 -0700540 switch (hw->mac.type) {
541 case e1000_82575:
Auke Kok9d5c8242008-01-24 02:22:38 -0800542 tmp = rd32(E1000_CTRL_EXT);
543 /* enable MSI-X PBA support*/
544 tmp |= E1000_CTRL_EXT_PBA_CLR;
545
546 /* Auto-Mask interrupts upon ICR read. */
547 tmp |= E1000_CTRL_EXT_EIAME;
548 tmp |= E1000_CTRL_EXT_IRCA;
549
550 wr32(E1000_CTRL_EXT, tmp);
Alexander Duyck047e0032009-10-27 15:49:27 +0000551
552 /* enable msix_other interrupt */
553 array_wr32(E1000_MSIXBM(0), vector++,
554 E1000_EIMS_OTHER);
PJ Waskiewicz844290e2008-06-27 11:00:39 -0700555 adapter->eims_other = E1000_EIMS_OTHER;
Auke Kok9d5c8242008-01-24 02:22:38 -0800556
Alexander Duyck2d064c02008-07-08 15:10:12 -0700557 break;
558
559 case e1000_82576:
Alexander Duyck047e0032009-10-27 15:49:27 +0000560 /* Turn on MSI-X capability first, or our settings
561 * won't stick. And it will take days to debug. */
562 wr32(E1000_GPIE, E1000_GPIE_MSIX_MODE |
563 E1000_GPIE_PBA | E1000_GPIE_EIAME |
564 E1000_GPIE_NSICR);
Alexander Duyck2d064c02008-07-08 15:10:12 -0700565
Alexander Duyck047e0032009-10-27 15:49:27 +0000566 /* enable msix_other interrupt */
567 adapter->eims_other = 1 << vector;
568 tmp = (vector++ | E1000_IVAR_VALID) << 8;
569
570 wr32(E1000_IVAR_MISC, tmp);
Alexander Duyck2d064c02008-07-08 15:10:12 -0700571 break;
572 default:
573 /* do nothing, since nothing else supports MSI-X */
574 break;
575 } /* switch (hw->mac.type) */
Alexander Duyck047e0032009-10-27 15:49:27 +0000576
577 adapter->eims_enable_mask |= adapter->eims_other;
578
579 for (i = 0; i < adapter->num_q_vectors; i++) {
580 struct igb_q_vector *q_vector = adapter->q_vector[i];
581 igb_assign_vector(q_vector, vector++);
582 adapter->eims_enable_mask |= q_vector->eims_value;
583 }
584
Auke Kok9d5c8242008-01-24 02:22:38 -0800585 wrfl();
586}
587
588/**
589 * igb_request_msix - Initialize MSI-X interrupts
590 *
591 * igb_request_msix allocates MSI-X vectors and requests interrupts from the
592 * kernel.
593 **/
594static int igb_request_msix(struct igb_adapter *adapter)
595{
596 struct net_device *netdev = adapter->netdev;
Alexander Duyck047e0032009-10-27 15:49:27 +0000597 struct e1000_hw *hw = &adapter->hw;
Auke Kok9d5c8242008-01-24 02:22:38 -0800598 int i, err = 0, vector = 0;
599
Auke Kok9d5c8242008-01-24 02:22:38 -0800600 err = request_irq(adapter->msix_entries[vector].vector,
Alexander Duyck047e0032009-10-27 15:49:27 +0000601 &igb_msix_other, 0, netdev->name, adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -0800602 if (err)
603 goto out;
Alexander Duyck047e0032009-10-27 15:49:27 +0000604 vector++;
605
606 for (i = 0; i < adapter->num_q_vectors; i++) {
607 struct igb_q_vector *q_vector = adapter->q_vector[i];
608
609 q_vector->itr_register = hw->hw_addr + E1000_EITR(vector);
610
611 if (q_vector->rx_ring && q_vector->tx_ring)
612 sprintf(q_vector->name, "%s-TxRx-%u", netdev->name,
613 q_vector->rx_ring->queue_index);
614 else if (q_vector->tx_ring)
615 sprintf(q_vector->name, "%s-tx-%u", netdev->name,
616 q_vector->tx_ring->queue_index);
617 else if (q_vector->rx_ring)
618 sprintf(q_vector->name, "%s-rx-%u", netdev->name,
619 q_vector->rx_ring->queue_index);
620 else
621 sprintf(q_vector->name, "%s-unused", netdev->name);
622
623 err = request_irq(adapter->msix_entries[vector].vector,
624 &igb_msix_ring, 0, q_vector->name,
625 q_vector);
626 if (err)
627 goto out;
628 vector++;
629 }
Auke Kok9d5c8242008-01-24 02:22:38 -0800630
Auke Kok9d5c8242008-01-24 02:22:38 -0800631 igb_configure_msix(adapter);
632 return 0;
633out:
634 return err;
635}
636
637static void igb_reset_interrupt_capability(struct igb_adapter *adapter)
638{
639 if (adapter->msix_entries) {
640 pci_disable_msix(adapter->pdev);
641 kfree(adapter->msix_entries);
642 adapter->msix_entries = NULL;
Alexander Duyck047e0032009-10-27 15:49:27 +0000643 } else if (adapter->flags & IGB_FLAG_HAS_MSI) {
Auke Kok9d5c8242008-01-24 02:22:38 -0800644 pci_disable_msi(adapter->pdev);
Alexander Duyck047e0032009-10-27 15:49:27 +0000645 }
Auke Kok9d5c8242008-01-24 02:22:38 -0800646}
647
Alexander Duyck047e0032009-10-27 15:49:27 +0000648/**
649 * igb_free_q_vectors - Free memory allocated for interrupt vectors
650 * @adapter: board private structure to initialize
651 *
652 * This function frees the memory allocated to the q_vectors. In addition if
653 * NAPI is enabled it will delete any references to the NAPI struct prior
654 * to freeing the q_vector.
655 **/
656static void igb_free_q_vectors(struct igb_adapter *adapter)
657{
658 int v_idx;
659
660 for (v_idx = 0; v_idx < adapter->num_q_vectors; v_idx++) {
661 struct igb_q_vector *q_vector = adapter->q_vector[v_idx];
662 adapter->q_vector[v_idx] = NULL;
663 netif_napi_del(&q_vector->napi);
664 kfree(q_vector);
665 }
666 adapter->num_q_vectors = 0;
667}
668
669/**
670 * igb_clear_interrupt_scheme - reset the device to a state of no interrupts
671 *
672 * This function resets the device so that it has 0 rx queues, tx queues, and
673 * MSI-X interrupts allocated.
674 */
675static void igb_clear_interrupt_scheme(struct igb_adapter *adapter)
676{
677 igb_free_queues(adapter);
678 igb_free_q_vectors(adapter);
679 igb_reset_interrupt_capability(adapter);
680}
Auke Kok9d5c8242008-01-24 02:22:38 -0800681
682/**
683 * igb_set_interrupt_capability - set MSI or MSI-X if supported
684 *
685 * Attempt to configure interrupts using the best available
686 * capabilities of the hardware and kernel.
687 **/
688static void igb_set_interrupt_capability(struct igb_adapter *adapter)
689{
690 int err;
691 int numvecs, i;
692
Alexander Duyck83b71802009-02-06 23:15:45 +0000693 /* Number of supported queues. */
Alexander Duyck83b71802009-02-06 23:15:45 +0000694 adapter->num_rx_queues = min_t(u32, IGB_MAX_RX_QUEUES, num_online_cpus());
695 adapter->num_tx_queues = min_t(u32, IGB_MAX_TX_QUEUES, num_online_cpus());
696
Alexander Duyck047e0032009-10-27 15:49:27 +0000697 /* start with one vector for every rx queue */
698 numvecs = adapter->num_rx_queues;
699
700 /* if tx handler is seperate add 1 for every tx queue */
701 numvecs += adapter->num_tx_queues;
702
703 /* store the number of vectors reserved for queues */
704 adapter->num_q_vectors = numvecs;
705
706 /* add 1 vector for link status interrupts */
707 numvecs++;
Auke Kok9d5c8242008-01-24 02:22:38 -0800708 adapter->msix_entries = kcalloc(numvecs, sizeof(struct msix_entry),
709 GFP_KERNEL);
710 if (!adapter->msix_entries)
711 goto msi_only;
712
713 for (i = 0; i < numvecs; i++)
714 adapter->msix_entries[i].entry = i;
715
716 err = pci_enable_msix(adapter->pdev,
717 adapter->msix_entries,
718 numvecs);
719 if (err == 0)
Alexander Duyck34a20e82008-08-26 04:25:13 -0700720 goto out;
Auke Kok9d5c8242008-01-24 02:22:38 -0800721
722 igb_reset_interrupt_capability(adapter);
723
724 /* If we can't do MSI-X, try MSI */
725msi_only:
Alexander Duyck2a3abf62009-04-07 14:37:52 +0000726#ifdef CONFIG_PCI_IOV
727 /* disable SR-IOV for non MSI-X configurations */
728 if (adapter->vf_data) {
729 struct e1000_hw *hw = &adapter->hw;
730 /* disable iov and allow time for transactions to clear */
731 pci_disable_sriov(adapter->pdev);
732 msleep(500);
733
734 kfree(adapter->vf_data);
735 adapter->vf_data = NULL;
736 wr32(E1000_IOVCTL, E1000_IOVCTL_REUSE_VFQ);
737 msleep(100);
738 dev_info(&adapter->pdev->dev, "IOV Disabled\n");
739 }
740#endif
Auke Kok9d5c8242008-01-24 02:22:38 -0800741 adapter->num_rx_queues = 1;
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -0700742 adapter->num_tx_queues = 1;
Alexander Duyck047e0032009-10-27 15:49:27 +0000743 adapter->num_q_vectors = 1;
Auke Kok9d5c8242008-01-24 02:22:38 -0800744 if (!pci_enable_msi(adapter->pdev))
Alexander Duyck7dfc16f2008-07-08 15:10:46 -0700745 adapter->flags |= IGB_FLAG_HAS_MSI;
Alexander Duyck34a20e82008-08-26 04:25:13 -0700746out:
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -0700747 /* Notify the stack of the (possibly) reduced Tx Queue count. */
David S. Millerfd2ea0a2008-07-17 01:56:23 -0700748 adapter->netdev->real_num_tx_queues = adapter->num_tx_queues;
Auke Kok9d5c8242008-01-24 02:22:38 -0800749 return;
750}
751
752/**
Alexander Duyck047e0032009-10-27 15:49:27 +0000753 * igb_alloc_q_vectors - Allocate memory for interrupt vectors
754 * @adapter: board private structure to initialize
755 *
756 * We allocate one q_vector per queue interrupt. If allocation fails we
757 * return -ENOMEM.
758 **/
759static int igb_alloc_q_vectors(struct igb_adapter *adapter)
760{
761 struct igb_q_vector *q_vector;
762 struct e1000_hw *hw = &adapter->hw;
763 int v_idx;
764
765 for (v_idx = 0; v_idx < adapter->num_q_vectors; v_idx++) {
766 q_vector = kzalloc(sizeof(struct igb_q_vector), GFP_KERNEL);
767 if (!q_vector)
768 goto err_out;
769 q_vector->adapter = adapter;
770 q_vector->itr_shift = (hw->mac.type == e1000_82575) ? 16 : 0;
771 q_vector->itr_register = hw->hw_addr + E1000_EITR(0);
772 q_vector->itr_val = IGB_START_ITR;
773 q_vector->set_itr = 1;
774 netif_napi_add(adapter->netdev, &q_vector->napi, igb_poll, 64);
775 adapter->q_vector[v_idx] = q_vector;
776 }
777 return 0;
778
779err_out:
780 while (v_idx) {
781 v_idx--;
782 q_vector = adapter->q_vector[v_idx];
783 netif_napi_del(&q_vector->napi);
784 kfree(q_vector);
785 adapter->q_vector[v_idx] = NULL;
786 }
787 return -ENOMEM;
788}
789
790static void igb_map_rx_ring_to_vector(struct igb_adapter *adapter,
791 int ring_idx, int v_idx)
792{
793 struct igb_q_vector *q_vector;
794
795 q_vector = adapter->q_vector[v_idx];
796 q_vector->rx_ring = &adapter->rx_ring[ring_idx];
797 q_vector->rx_ring->q_vector = q_vector;
798 q_vector->itr_val = adapter->itr;
799}
800
801static void igb_map_tx_ring_to_vector(struct igb_adapter *adapter,
802 int ring_idx, int v_idx)
803{
804 struct igb_q_vector *q_vector;
805
806 q_vector = adapter->q_vector[v_idx];
807 q_vector->tx_ring = &adapter->tx_ring[ring_idx];
808 q_vector->tx_ring->q_vector = q_vector;
809 q_vector->itr_val = adapter->itr;
810}
811
812/**
813 * igb_map_ring_to_vector - maps allocated queues to vectors
814 *
815 * This function maps the recently allocated queues to vectors.
816 **/
817static int igb_map_ring_to_vector(struct igb_adapter *adapter)
818{
819 int i;
820 int v_idx = 0;
821
822 if ((adapter->num_q_vectors < adapter->num_rx_queues) ||
823 (adapter->num_q_vectors < adapter->num_tx_queues))
824 return -ENOMEM;
825
826 if (adapter->num_q_vectors >=
827 (adapter->num_rx_queues + adapter->num_tx_queues)) {
828 for (i = 0; i < adapter->num_rx_queues; i++)
829 igb_map_rx_ring_to_vector(adapter, i, v_idx++);
830 for (i = 0; i < adapter->num_tx_queues; i++)
831 igb_map_tx_ring_to_vector(adapter, i, v_idx++);
832 } else {
833 for (i = 0; i < adapter->num_rx_queues; i++) {
834 if (i < adapter->num_tx_queues)
835 igb_map_tx_ring_to_vector(adapter, i, v_idx);
836 igb_map_rx_ring_to_vector(adapter, i, v_idx++);
837 }
838 for (; i < adapter->num_tx_queues; i++)
839 igb_map_tx_ring_to_vector(adapter, i, v_idx++);
840 }
841 return 0;
842}
843
844/**
845 * igb_init_interrupt_scheme - initialize interrupts, allocate queues/vectors
846 *
847 * This function initializes the interrupts and allocates all of the queues.
848 **/
849static int igb_init_interrupt_scheme(struct igb_adapter *adapter)
850{
851 struct pci_dev *pdev = adapter->pdev;
852 int err;
853
854 igb_set_interrupt_capability(adapter);
855
856 err = igb_alloc_q_vectors(adapter);
857 if (err) {
858 dev_err(&pdev->dev, "Unable to allocate memory for vectors\n");
859 goto err_alloc_q_vectors;
860 }
861
862 err = igb_alloc_queues(adapter);
863 if (err) {
864 dev_err(&pdev->dev, "Unable to allocate memory for queues\n");
865 goto err_alloc_queues;
866 }
867
868 err = igb_map_ring_to_vector(adapter);
869 if (err) {
870 dev_err(&pdev->dev, "Invalid q_vector to ring mapping\n");
871 goto err_map_queues;
872 }
873
874
875 return 0;
876err_map_queues:
877 igb_free_queues(adapter);
878err_alloc_queues:
879 igb_free_q_vectors(adapter);
880err_alloc_q_vectors:
881 igb_reset_interrupt_capability(adapter);
882 return err;
883}
884
885/**
Auke Kok9d5c8242008-01-24 02:22:38 -0800886 * igb_request_irq - initialize interrupts
887 *
888 * Attempts to configure interrupts using the best available
889 * capabilities of the hardware and kernel.
890 **/
891static int igb_request_irq(struct igb_adapter *adapter)
892{
893 struct net_device *netdev = adapter->netdev;
Alexander Duyck047e0032009-10-27 15:49:27 +0000894 struct pci_dev *pdev = adapter->pdev;
Auke Kok9d5c8242008-01-24 02:22:38 -0800895 struct e1000_hw *hw = &adapter->hw;
896 int err = 0;
897
898 if (adapter->msix_entries) {
899 err = igb_request_msix(adapter);
PJ Waskiewicz844290e2008-06-27 11:00:39 -0700900 if (!err)
Auke Kok9d5c8242008-01-24 02:22:38 -0800901 goto request_done;
Auke Kok9d5c8242008-01-24 02:22:38 -0800902 /* fall back to MSI */
Alexander Duyck047e0032009-10-27 15:49:27 +0000903 igb_clear_interrupt_scheme(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -0800904 if (!pci_enable_msi(adapter->pdev))
Alexander Duyck7dfc16f2008-07-08 15:10:46 -0700905 adapter->flags |= IGB_FLAG_HAS_MSI;
Auke Kok9d5c8242008-01-24 02:22:38 -0800906 igb_free_all_tx_resources(adapter);
907 igb_free_all_rx_resources(adapter);
Alexander Duyck047e0032009-10-27 15:49:27 +0000908 adapter->num_tx_queues = 1;
Auke Kok9d5c8242008-01-24 02:22:38 -0800909 adapter->num_rx_queues = 1;
Alexander Duyck047e0032009-10-27 15:49:27 +0000910 adapter->num_q_vectors = 1;
911 err = igb_alloc_q_vectors(adapter);
912 if (err) {
913 dev_err(&pdev->dev,
914 "Unable to allocate memory for vectors\n");
915 goto request_done;
916 }
917 err = igb_alloc_queues(adapter);
918 if (err) {
919 dev_err(&pdev->dev,
920 "Unable to allocate memory for queues\n");
921 igb_free_q_vectors(adapter);
922 goto request_done;
923 }
924 igb_setup_all_tx_resources(adapter);
925 igb_setup_all_rx_resources(adapter);
PJ Waskiewicz844290e2008-06-27 11:00:39 -0700926 } else {
Alexander Duyck2d064c02008-07-08 15:10:12 -0700927 switch (hw->mac.type) {
928 case e1000_82575:
929 wr32(E1000_MSIXBM(0),
Alexander Duyck047e0032009-10-27 15:49:27 +0000930 (E1000_EICR_RX_QUEUE0 |
931 E1000_EICR_TX_QUEUE0 |
932 E1000_EIMS_OTHER));
Alexander Duyck2d064c02008-07-08 15:10:12 -0700933 break;
934 case e1000_82576:
935 wr32(E1000_IVAR0, E1000_IVAR_VALID);
936 break;
937 default:
938 break;
939 }
Auke Kok9d5c8242008-01-24 02:22:38 -0800940 }
PJ Waskiewicz844290e2008-06-27 11:00:39 -0700941
Alexander Duyck7dfc16f2008-07-08 15:10:46 -0700942 if (adapter->flags & IGB_FLAG_HAS_MSI) {
Auke Kok9d5c8242008-01-24 02:22:38 -0800943 err = request_irq(adapter->pdev->irq, &igb_intr_msi, 0,
Alexander Duyck047e0032009-10-27 15:49:27 +0000944 netdev->name, adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -0800945 if (!err)
946 goto request_done;
Alexander Duyck047e0032009-10-27 15:49:27 +0000947
Auke Kok9d5c8242008-01-24 02:22:38 -0800948 /* fall back to legacy interrupts */
949 igb_reset_interrupt_capability(adapter);
Alexander Duyck7dfc16f2008-07-08 15:10:46 -0700950 adapter->flags &= ~IGB_FLAG_HAS_MSI;
Auke Kok9d5c8242008-01-24 02:22:38 -0800951 }
952
953 err = request_irq(adapter->pdev->irq, &igb_intr, IRQF_SHARED,
Alexander Duyck047e0032009-10-27 15:49:27 +0000954 netdev->name, adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -0800955
Andy Gospodarek6cb5e572008-02-15 14:05:25 -0800956 if (err)
Auke Kok9d5c8242008-01-24 02:22:38 -0800957 dev_err(&adapter->pdev->dev, "Error %d getting interrupt\n",
958 err);
Auke Kok9d5c8242008-01-24 02:22:38 -0800959
960request_done:
961 return err;
962}
963
964static void igb_free_irq(struct igb_adapter *adapter)
965{
Auke Kok9d5c8242008-01-24 02:22:38 -0800966 if (adapter->msix_entries) {
967 int vector = 0, i;
968
Alexander Duyck047e0032009-10-27 15:49:27 +0000969 free_irq(adapter->msix_entries[vector++].vector, adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -0800970
Alexander Duyck047e0032009-10-27 15:49:27 +0000971 for (i = 0; i < adapter->num_q_vectors; i++) {
972 struct igb_q_vector *q_vector = adapter->q_vector[i];
973 free_irq(adapter->msix_entries[vector++].vector,
974 q_vector);
975 }
976 } else {
977 free_irq(adapter->pdev->irq, adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -0800978 }
Auke Kok9d5c8242008-01-24 02:22:38 -0800979}
980
981/**
982 * igb_irq_disable - Mask off interrupt generation on the NIC
983 * @adapter: board private structure
984 **/
985static void igb_irq_disable(struct igb_adapter *adapter)
986{
987 struct e1000_hw *hw = &adapter->hw;
988
989 if (adapter->msix_entries) {
Alexander Duyck2dfd1212009-09-03 14:49:15 +0000990 u32 regval = rd32(E1000_EIAM);
991 wr32(E1000_EIAM, regval & ~adapter->eims_enable_mask);
992 wr32(E1000_EIMC, adapter->eims_enable_mask);
993 regval = rd32(E1000_EIAC);
994 wr32(E1000_EIAC, regval & ~adapter->eims_enable_mask);
Auke Kok9d5c8242008-01-24 02:22:38 -0800995 }
PJ Waskiewicz844290e2008-06-27 11:00:39 -0700996
997 wr32(E1000_IAM, 0);
Auke Kok9d5c8242008-01-24 02:22:38 -0800998 wr32(E1000_IMC, ~0);
999 wrfl();
1000 synchronize_irq(adapter->pdev->irq);
1001}
1002
1003/**
1004 * igb_irq_enable - Enable default interrupt generation settings
1005 * @adapter: board private structure
1006 **/
1007static void igb_irq_enable(struct igb_adapter *adapter)
1008{
1009 struct e1000_hw *hw = &adapter->hw;
1010
1011 if (adapter->msix_entries) {
Alexander Duyck2dfd1212009-09-03 14:49:15 +00001012 u32 regval = rd32(E1000_EIAC);
1013 wr32(E1000_EIAC, regval | adapter->eims_enable_mask);
1014 regval = rd32(E1000_EIAM);
1015 wr32(E1000_EIAM, regval | adapter->eims_enable_mask);
PJ Waskiewicz844290e2008-06-27 11:00:39 -07001016 wr32(E1000_EIMS, adapter->eims_enable_mask);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08001017 if (adapter->vfs_allocated_count)
1018 wr32(E1000_MBVFIMR, 0xFF);
1019 wr32(E1000_IMS, (E1000_IMS_LSC | E1000_IMS_VMMB |
1020 E1000_IMS_DOUTSYNC));
PJ Waskiewicz844290e2008-06-27 11:00:39 -07001021 } else {
1022 wr32(E1000_IMS, IMS_ENABLE_MASK);
1023 wr32(E1000_IAM, IMS_ENABLE_MASK);
1024 }
Auke Kok9d5c8242008-01-24 02:22:38 -08001025}
1026
1027static void igb_update_mng_vlan(struct igb_adapter *adapter)
1028{
1029 struct net_device *netdev = adapter->netdev;
1030 u16 vid = adapter->hw.mng_cookie.vlan_id;
1031 u16 old_vid = adapter->mng_vlan_id;
1032 if (adapter->vlgrp) {
1033 if (!vlan_group_get_device(adapter->vlgrp, vid)) {
1034 if (adapter->hw.mng_cookie.status &
1035 E1000_MNG_DHCP_COOKIE_STATUS_VLAN) {
1036 igb_vlan_rx_add_vid(netdev, vid);
1037 adapter->mng_vlan_id = vid;
1038 } else
1039 adapter->mng_vlan_id = IGB_MNG_VLAN_NONE;
1040
1041 if ((old_vid != (u16)IGB_MNG_VLAN_NONE) &&
1042 (vid != old_vid) &&
1043 !vlan_group_get_device(adapter->vlgrp, old_vid))
1044 igb_vlan_rx_kill_vid(netdev, old_vid);
1045 } else
1046 adapter->mng_vlan_id = vid;
1047 }
1048}
1049
1050/**
1051 * igb_release_hw_control - release control of the h/w to f/w
1052 * @adapter: address of board private structure
1053 *
1054 * igb_release_hw_control resets CTRL_EXT:DRV_LOAD bit.
1055 * For ASF and Pass Through versions of f/w this means that the
1056 * driver is no longer loaded.
1057 *
1058 **/
1059static void igb_release_hw_control(struct igb_adapter *adapter)
1060{
1061 struct e1000_hw *hw = &adapter->hw;
1062 u32 ctrl_ext;
1063
1064 /* Let firmware take over control of h/w */
1065 ctrl_ext = rd32(E1000_CTRL_EXT);
1066 wr32(E1000_CTRL_EXT,
1067 ctrl_ext & ~E1000_CTRL_EXT_DRV_LOAD);
1068}
1069
1070
1071/**
1072 * igb_get_hw_control - get control of the h/w from f/w
1073 * @adapter: address of board private structure
1074 *
1075 * igb_get_hw_control sets CTRL_EXT:DRV_LOAD bit.
1076 * For ASF and Pass Through versions of f/w this means that
1077 * the driver is loaded.
1078 *
1079 **/
1080static void igb_get_hw_control(struct igb_adapter *adapter)
1081{
1082 struct e1000_hw *hw = &adapter->hw;
1083 u32 ctrl_ext;
1084
1085 /* Let firmware know the driver has taken over */
1086 ctrl_ext = rd32(E1000_CTRL_EXT);
1087 wr32(E1000_CTRL_EXT,
1088 ctrl_ext | E1000_CTRL_EXT_DRV_LOAD);
1089}
1090
Auke Kok9d5c8242008-01-24 02:22:38 -08001091/**
1092 * igb_configure - configure the hardware for RX and TX
1093 * @adapter: private board structure
1094 **/
1095static void igb_configure(struct igb_adapter *adapter)
1096{
1097 struct net_device *netdev = adapter->netdev;
1098 int i;
1099
1100 igb_get_hw_control(adapter);
Alexander Duyckff41f8d2009-09-03 14:48:56 +00001101 igb_set_rx_mode(netdev);
Auke Kok9d5c8242008-01-24 02:22:38 -08001102
1103 igb_restore_vlan(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08001104
Alexander Duyck85b430b2009-10-27 15:50:29 +00001105 igb_setup_tctl(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08001106 igb_setup_rctl(adapter);
Alexander Duyck85b430b2009-10-27 15:50:29 +00001107
1108 igb_configure_tx(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08001109 igb_configure_rx(adapter);
Alexander Duyck662d7202008-06-27 11:00:29 -07001110
1111 igb_rx_fifo_flush_82575(&adapter->hw);
1112
Alexander Duyckc493ea42009-03-20 00:16:50 +00001113 /* call igb_desc_unused which always leaves
Auke Kok9d5c8242008-01-24 02:22:38 -08001114 * at least 1 descriptor unused to make sure
1115 * next_to_use != next_to_clean */
1116 for (i = 0; i < adapter->num_rx_queues; i++) {
1117 struct igb_ring *ring = &adapter->rx_ring[i];
Alexander Duyckc493ea42009-03-20 00:16:50 +00001118 igb_alloc_rx_buffers_adv(ring, igb_desc_unused(ring));
Auke Kok9d5c8242008-01-24 02:22:38 -08001119 }
1120
1121
1122 adapter->tx_queue_len = netdev->tx_queue_len;
1123}
1124
1125
1126/**
1127 * igb_up - Open the interface and prepare it to handle traffic
1128 * @adapter: board private structure
1129 **/
1130
1131int igb_up(struct igb_adapter *adapter)
1132{
1133 struct e1000_hw *hw = &adapter->hw;
1134 int i;
1135
1136 /* hardware has been reset, we need to reload some things */
1137 igb_configure(adapter);
1138
1139 clear_bit(__IGB_DOWN, &adapter->state);
1140
Alexander Duyck047e0032009-10-27 15:49:27 +00001141 for (i = 0; i < adapter->num_q_vectors; i++) {
1142 struct igb_q_vector *q_vector = adapter->q_vector[i];
1143 napi_enable(&q_vector->napi);
1144 }
PJ Waskiewicz844290e2008-06-27 11:00:39 -07001145 if (adapter->msix_entries)
Auke Kok9d5c8242008-01-24 02:22:38 -08001146 igb_configure_msix(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08001147
Alexander Duyck4ae196d2009-02-19 20:40:07 -08001148 igb_vmm_control(adapter);
Alexander Duycke1739522009-02-19 20:39:44 -08001149 igb_set_vmolr(hw, adapter->vfs_allocated_count);
1150
Auke Kok9d5c8242008-01-24 02:22:38 -08001151 /* Clear any pending interrupts. */
1152 rd32(E1000_ICR);
1153 igb_irq_enable(adapter);
1154
Jesse Brandeburg4cb9be72009-04-21 18:42:05 +00001155 netif_tx_start_all_queues(adapter->netdev);
1156
Auke Kok9d5c8242008-01-24 02:22:38 -08001157 /* Fire a link change interrupt to start the watchdog. */
1158 wr32(E1000_ICS, E1000_ICS_LSC);
1159 return 0;
1160}
1161
1162void igb_down(struct igb_adapter *adapter)
1163{
1164 struct e1000_hw *hw = &adapter->hw;
1165 struct net_device *netdev = adapter->netdev;
1166 u32 tctl, rctl;
1167 int i;
1168
1169 /* signal that we're down so the interrupt handler does not
1170 * reschedule our watchdog timer */
1171 set_bit(__IGB_DOWN, &adapter->state);
1172
1173 /* disable receives in the hardware */
1174 rctl = rd32(E1000_RCTL);
1175 wr32(E1000_RCTL, rctl & ~E1000_RCTL_EN);
1176 /* flush and sleep below */
1177
David S. Millerfd2ea0a2008-07-17 01:56:23 -07001178 netif_tx_stop_all_queues(netdev);
Auke Kok9d5c8242008-01-24 02:22:38 -08001179
1180 /* disable transmits in the hardware */
1181 tctl = rd32(E1000_TCTL);
1182 tctl &= ~E1000_TCTL_EN;
1183 wr32(E1000_TCTL, tctl);
1184 /* flush both disables and wait for them to finish */
1185 wrfl();
1186 msleep(10);
1187
Alexander Duyck047e0032009-10-27 15:49:27 +00001188 for (i = 0; i < adapter->num_q_vectors; i++) {
1189 struct igb_q_vector *q_vector = adapter->q_vector[i];
1190 napi_disable(&q_vector->napi);
1191 }
Auke Kok9d5c8242008-01-24 02:22:38 -08001192
Auke Kok9d5c8242008-01-24 02:22:38 -08001193 igb_irq_disable(adapter);
1194
1195 del_timer_sync(&adapter->watchdog_timer);
1196 del_timer_sync(&adapter->phy_info_timer);
1197
1198 netdev->tx_queue_len = adapter->tx_queue_len;
1199 netif_carrier_off(netdev);
Alexander Duyck04fe6352009-02-06 23:22:32 +00001200
1201 /* record the stats before reset*/
1202 igb_update_stats(adapter);
1203
Auke Kok9d5c8242008-01-24 02:22:38 -08001204 adapter->link_speed = 0;
1205 adapter->link_duplex = 0;
1206
Jeff Kirsher30236822008-06-24 17:01:15 -07001207 if (!pci_channel_offline(adapter->pdev))
1208 igb_reset(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08001209 igb_clean_all_tx_rings(adapter);
1210 igb_clean_all_rx_rings(adapter);
Alexander Duyck7e0e99e2009-05-21 13:06:56 +00001211#ifdef CONFIG_IGB_DCA
1212
1213 /* since we reset the hardware DCA settings were cleared */
1214 igb_setup_dca(adapter);
1215#endif
Auke Kok9d5c8242008-01-24 02:22:38 -08001216}
1217
1218void igb_reinit_locked(struct igb_adapter *adapter)
1219{
1220 WARN_ON(in_interrupt());
1221 while (test_and_set_bit(__IGB_RESETTING, &adapter->state))
1222 msleep(1);
1223 igb_down(adapter);
1224 igb_up(adapter);
1225 clear_bit(__IGB_RESETTING, &adapter->state);
1226}
1227
1228void igb_reset(struct igb_adapter *adapter)
1229{
1230 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck2d064c02008-07-08 15:10:12 -07001231 struct e1000_mac_info *mac = &hw->mac;
1232 struct e1000_fc_info *fc = &hw->fc;
Auke Kok9d5c8242008-01-24 02:22:38 -08001233 u32 pba = 0, tx_space, min_tx_space, min_rx_space;
1234 u16 hwm;
1235
1236 /* Repartition Pba for greater than 9k mtu
1237 * To take effect CTRL.RST is required.
1238 */
Alexander Duyckfa4dfae2009-02-06 23:21:31 +00001239 switch (mac->type) {
1240 case e1000_82576:
Alexander Duyck2d064c02008-07-08 15:10:12 -07001241 pba = E1000_PBA_64K;
Alexander Duyckfa4dfae2009-02-06 23:21:31 +00001242 break;
1243 case e1000_82575:
1244 default:
1245 pba = E1000_PBA_34K;
1246 break;
Alexander Duyck2d064c02008-07-08 15:10:12 -07001247 }
Auke Kok9d5c8242008-01-24 02:22:38 -08001248
Alexander Duyck2d064c02008-07-08 15:10:12 -07001249 if ((adapter->max_frame_size > ETH_FRAME_LEN + ETH_FCS_LEN) &&
1250 (mac->type < e1000_82576)) {
Auke Kok9d5c8242008-01-24 02:22:38 -08001251 /* adjust PBA for jumbo frames */
1252 wr32(E1000_PBA, pba);
1253
1254 /* To maintain wire speed transmits, the Tx FIFO should be
1255 * large enough to accommodate two full transmit packets,
1256 * rounded up to the next 1KB and expressed in KB. Likewise,
1257 * the Rx FIFO should be large enough to accommodate at least
1258 * one full receive packet and is similarly rounded up and
1259 * expressed in KB. */
1260 pba = rd32(E1000_PBA);
1261 /* upper 16 bits has Tx packet buffer allocation size in KB */
1262 tx_space = pba >> 16;
1263 /* lower 16 bits has Rx packet buffer allocation size in KB */
1264 pba &= 0xffff;
1265 /* the tx fifo also stores 16 bytes of information about the tx
1266 * but don't include ethernet FCS because hardware appends it */
1267 min_tx_space = (adapter->max_frame_size +
Alexander Duyck85e8d002009-02-16 00:00:20 -08001268 sizeof(union e1000_adv_tx_desc) -
Auke Kok9d5c8242008-01-24 02:22:38 -08001269 ETH_FCS_LEN) * 2;
1270 min_tx_space = ALIGN(min_tx_space, 1024);
1271 min_tx_space >>= 10;
1272 /* software strips receive CRC, so leave room for it */
1273 min_rx_space = adapter->max_frame_size;
1274 min_rx_space = ALIGN(min_rx_space, 1024);
1275 min_rx_space >>= 10;
1276
1277 /* If current Tx allocation is less than the min Tx FIFO size,
1278 * and the min Tx FIFO size is less than the current Rx FIFO
1279 * allocation, take space away from current Rx allocation */
1280 if (tx_space < min_tx_space &&
1281 ((min_tx_space - tx_space) < pba)) {
1282 pba = pba - (min_tx_space - tx_space);
1283
1284 /* if short on rx space, rx wins and must trump tx
1285 * adjustment */
1286 if (pba < min_rx_space)
1287 pba = min_rx_space;
1288 }
Alexander Duyck2d064c02008-07-08 15:10:12 -07001289 wr32(E1000_PBA, pba);
Auke Kok9d5c8242008-01-24 02:22:38 -08001290 }
Auke Kok9d5c8242008-01-24 02:22:38 -08001291
1292 /* flow control settings */
1293 /* The high water mark must be low enough to fit one full frame
1294 * (or the size used for early receive) above it in the Rx FIFO.
1295 * Set it to the lower of:
1296 * - 90% of the Rx FIFO size, or
1297 * - the full Rx FIFO size minus one full frame */
1298 hwm = min(((pba << 10) * 9 / 10),
Alexander Duyck2d064c02008-07-08 15:10:12 -07001299 ((pba << 10) - 2 * adapter->max_frame_size));
Auke Kok9d5c8242008-01-24 02:22:38 -08001300
Alexander Duyck2d064c02008-07-08 15:10:12 -07001301 if (mac->type < e1000_82576) {
1302 fc->high_water = hwm & 0xFFF8; /* 8-byte granularity */
1303 fc->low_water = fc->high_water - 8;
1304 } else {
1305 fc->high_water = hwm & 0xFFF0; /* 16-byte granularity */
1306 fc->low_water = fc->high_water - 16;
1307 }
Auke Kok9d5c8242008-01-24 02:22:38 -08001308 fc->pause_time = 0xFFFF;
1309 fc->send_xon = 1;
Alexander Duyck0cce1192009-07-23 18:10:24 +00001310 fc->current_mode = fc->requested_mode;
Auke Kok9d5c8242008-01-24 02:22:38 -08001311
Alexander Duyck4ae196d2009-02-19 20:40:07 -08001312 /* disable receive for all VFs and wait one second */
1313 if (adapter->vfs_allocated_count) {
1314 int i;
1315 for (i = 0 ; i < adapter->vfs_allocated_count; i++)
1316 adapter->vf_data[i].clear_to_send = false;
1317
1318 /* ping all the active vfs to let them know we are going down */
1319 igb_ping_all_vfs(adapter);
1320
1321 /* disable transmits and receives */
1322 wr32(E1000_VFRE, 0);
1323 wr32(E1000_VFTE, 0);
1324 }
1325
Auke Kok9d5c8242008-01-24 02:22:38 -08001326 /* Allow time for pending master requests to run */
1327 adapter->hw.mac.ops.reset_hw(&adapter->hw);
1328 wr32(E1000_WUC, 0);
1329
1330 if (adapter->hw.mac.ops.init_hw(&adapter->hw))
1331 dev_err(&adapter->pdev->dev, "Hardware Error\n");
1332
1333 igb_update_mng_vlan(adapter);
1334
1335 /* Enable h/w to recognize an 802.1Q VLAN Ethernet packet */
1336 wr32(E1000_VET, ETHERNET_IEEE_VLAN_TYPE);
1337
1338 igb_reset_adaptive(&adapter->hw);
Alexander Duyckf5f4cf02008-11-21 21:30:24 -08001339 igb_get_phy_info(&adapter->hw);
Auke Kok9d5c8242008-01-24 02:22:38 -08001340}
1341
Stephen Hemminger2e5c6922008-11-19 22:20:44 -08001342static const struct net_device_ops igb_netdev_ops = {
1343 .ndo_open = igb_open,
1344 .ndo_stop = igb_close,
Stephen Hemminger00829822008-11-20 20:14:53 -08001345 .ndo_start_xmit = igb_xmit_frame_adv,
Stephen Hemminger2e5c6922008-11-19 22:20:44 -08001346 .ndo_get_stats = igb_get_stats,
Alexander Duyckff41f8d2009-09-03 14:48:56 +00001347 .ndo_set_rx_mode = igb_set_rx_mode,
1348 .ndo_set_multicast_list = igb_set_rx_mode,
Stephen Hemminger2e5c6922008-11-19 22:20:44 -08001349 .ndo_set_mac_address = igb_set_mac,
1350 .ndo_change_mtu = igb_change_mtu,
1351 .ndo_do_ioctl = igb_ioctl,
1352 .ndo_tx_timeout = igb_tx_timeout,
1353 .ndo_validate_addr = eth_validate_addr,
1354 .ndo_vlan_rx_register = igb_vlan_rx_register,
1355 .ndo_vlan_rx_add_vid = igb_vlan_rx_add_vid,
1356 .ndo_vlan_rx_kill_vid = igb_vlan_rx_kill_vid,
1357#ifdef CONFIG_NET_POLL_CONTROLLER
1358 .ndo_poll_controller = igb_netpoll,
1359#endif
1360};
1361
Taku Izumi42bfd33a2008-06-20 12:10:30 +09001362/**
Auke Kok9d5c8242008-01-24 02:22:38 -08001363 * igb_probe - Device Initialization Routine
1364 * @pdev: PCI device information struct
1365 * @ent: entry in igb_pci_tbl
1366 *
1367 * Returns 0 on success, negative on failure
1368 *
1369 * igb_probe initializes an adapter identified by a pci_dev structure.
1370 * The OS initialization, configuring of the adapter private structure,
1371 * and a hardware reset occur.
1372 **/
1373static int __devinit igb_probe(struct pci_dev *pdev,
1374 const struct pci_device_id *ent)
1375{
1376 struct net_device *netdev;
1377 struct igb_adapter *adapter;
1378 struct e1000_hw *hw;
1379 const struct e1000_info *ei = igb_info_tbl[ent->driver_data];
1380 unsigned long mmio_start, mmio_len;
David S. Miller2d6a5e92009-03-17 15:01:30 -07001381 int err, pci_using_dac;
Alexander Duyck682337f2009-03-14 22:26:40 -07001382 u16 eeprom_data = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08001383 u16 eeprom_apme_mask = IGB_EEPROM_APME;
1384 u32 part_num;
1385
Alexander Duyckaed5dec2009-02-06 23:16:04 +00001386 err = pci_enable_device_mem(pdev);
Auke Kok9d5c8242008-01-24 02:22:38 -08001387 if (err)
1388 return err;
1389
1390 pci_using_dac = 0;
Yang Hongyang6a355282009-04-06 19:01:13 -07001391 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
Auke Kok9d5c8242008-01-24 02:22:38 -08001392 if (!err) {
Yang Hongyang6a355282009-04-06 19:01:13 -07001393 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
Auke Kok9d5c8242008-01-24 02:22:38 -08001394 if (!err)
1395 pci_using_dac = 1;
1396 } else {
Yang Hongyang284901a2009-04-06 19:01:15 -07001397 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Auke Kok9d5c8242008-01-24 02:22:38 -08001398 if (err) {
Yang Hongyang284901a2009-04-06 19:01:15 -07001399 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
Auke Kok9d5c8242008-01-24 02:22:38 -08001400 if (err) {
1401 dev_err(&pdev->dev, "No usable DMA "
1402 "configuration, aborting\n");
1403 goto err_dma;
1404 }
1405 }
1406 }
1407
Alexander Duyckaed5dec2009-02-06 23:16:04 +00001408 err = pci_request_selected_regions(pdev, pci_select_bars(pdev,
1409 IORESOURCE_MEM),
1410 igb_driver_name);
Auke Kok9d5c8242008-01-24 02:22:38 -08001411 if (err)
1412 goto err_pci_reg;
1413
Frans Pop19d5afd2009-10-02 10:04:12 -07001414 pci_enable_pcie_error_reporting(pdev);
Alexander Duyck40a914f2008-11-27 00:24:37 -08001415
Auke Kok9d5c8242008-01-24 02:22:38 -08001416 pci_set_master(pdev);
Auke Kokc682fc22008-04-23 11:09:34 -07001417 pci_save_state(pdev);
Auke Kok9d5c8242008-01-24 02:22:38 -08001418
1419 err = -ENOMEM;
Alexander Duyck1bfaf072009-02-19 20:39:23 -08001420 netdev = alloc_etherdev_mq(sizeof(struct igb_adapter),
1421 IGB_ABS_MAX_TX_QUEUES);
Auke Kok9d5c8242008-01-24 02:22:38 -08001422 if (!netdev)
1423 goto err_alloc_etherdev;
1424
1425 SET_NETDEV_DEV(netdev, &pdev->dev);
1426
1427 pci_set_drvdata(pdev, netdev);
1428 adapter = netdev_priv(netdev);
1429 adapter->netdev = netdev;
1430 adapter->pdev = pdev;
1431 hw = &adapter->hw;
1432 hw->back = adapter;
1433 adapter->msg_enable = NETIF_MSG_DRV | NETIF_MSG_PROBE;
1434
1435 mmio_start = pci_resource_start(pdev, 0);
1436 mmio_len = pci_resource_len(pdev, 0);
1437
1438 err = -EIO;
Alexander Duyck28b07592009-02-06 23:20:31 +00001439 hw->hw_addr = ioremap(mmio_start, mmio_len);
1440 if (!hw->hw_addr)
Auke Kok9d5c8242008-01-24 02:22:38 -08001441 goto err_ioremap;
1442
Stephen Hemminger2e5c6922008-11-19 22:20:44 -08001443 netdev->netdev_ops = &igb_netdev_ops;
Auke Kok9d5c8242008-01-24 02:22:38 -08001444 igb_set_ethtool_ops(netdev);
Auke Kok9d5c8242008-01-24 02:22:38 -08001445 netdev->watchdog_timeo = 5 * HZ;
Auke Kok9d5c8242008-01-24 02:22:38 -08001446
1447 strncpy(netdev->name, pci_name(pdev), sizeof(netdev->name) - 1);
1448
1449 netdev->mem_start = mmio_start;
1450 netdev->mem_end = mmio_start + mmio_len;
1451
Auke Kok9d5c8242008-01-24 02:22:38 -08001452 /* PCI config space info */
1453 hw->vendor_id = pdev->vendor;
1454 hw->device_id = pdev->device;
1455 hw->revision_id = pdev->revision;
1456 hw->subsystem_vendor_id = pdev->subsystem_vendor;
1457 hw->subsystem_device_id = pdev->subsystem_device;
1458
1459 /* setup the private structure */
1460 hw->back = adapter;
1461 /* Copy the default MAC, PHY and NVM function pointers */
1462 memcpy(&hw->mac.ops, ei->mac_ops, sizeof(hw->mac.ops));
1463 memcpy(&hw->phy.ops, ei->phy_ops, sizeof(hw->phy.ops));
1464 memcpy(&hw->nvm.ops, ei->nvm_ops, sizeof(hw->nvm.ops));
1465 /* Initialize skew-specific constants */
1466 err = ei->get_invariants(hw);
1467 if (err)
Alexander Duyck450c87c2009-02-06 23:22:11 +00001468 goto err_sw_init;
Auke Kok9d5c8242008-01-24 02:22:38 -08001469
Alexander Duyck2a3abf62009-04-07 14:37:52 +00001470#ifdef CONFIG_PCI_IOV
1471 /* since iov functionality isn't critical to base device function we
1472 * can accept failure. If it fails we don't allow iov to be enabled */
1473 if (hw->mac.type == e1000_82576) {
1474 /* 82576 supports a maximum of 7 VFs in addition to the PF */
1475 unsigned int num_vfs = (max_vfs > 7) ? 7 : max_vfs;
1476 int i;
1477 unsigned char mac_addr[ETH_ALEN];
1478
Alexander Duyck9ca046d2009-04-09 22:49:39 +00001479 if (num_vfs) {
Alexander Duyck2a3abf62009-04-07 14:37:52 +00001480 adapter->vf_data = kcalloc(num_vfs,
1481 sizeof(struct vf_data_storage),
1482 GFP_KERNEL);
Alexander Duyck9ca046d2009-04-09 22:49:39 +00001483 if (!adapter->vf_data) {
1484 dev_err(&pdev->dev,
1485 "Could not allocate VF private data - "
1486 "IOV enable failed\n");
Alexander Duyck2a3abf62009-04-07 14:37:52 +00001487 } else {
Alexander Duyck9ca046d2009-04-09 22:49:39 +00001488 err = pci_enable_sriov(pdev, num_vfs);
1489 if (!err) {
1490 adapter->vfs_allocated_count = num_vfs;
1491 dev_info(&pdev->dev,
1492 "%d vfs allocated\n",
1493 num_vfs);
1494 for (i = 0;
1495 i < adapter->vfs_allocated_count;
1496 i++) {
1497 random_ether_addr(mac_addr);
1498 igb_set_vf_mac(adapter, i,
1499 mac_addr);
1500 }
1501 } else {
1502 kfree(adapter->vf_data);
1503 adapter->vf_data = NULL;
1504 }
Alexander Duyck2a3abf62009-04-07 14:37:52 +00001505 }
1506 }
1507 }
1508
1509#endif
Alexander Duyck450c87c2009-02-06 23:22:11 +00001510 /* setup the private structure */
Auke Kok9d5c8242008-01-24 02:22:38 -08001511 err = igb_sw_init(adapter);
1512 if (err)
1513 goto err_sw_init;
1514
1515 igb_get_bus_info_pcie(hw);
1516
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07001517 /* set flags */
1518 switch (hw->mac.type) {
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07001519 case e1000_82575:
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07001520 adapter->flags |= IGB_FLAG_NEED_CTX_IDX;
1521 break;
Alexander Duyckbbd98fe2009-01-31 00:52:30 -08001522 case e1000_82576:
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07001523 default:
1524 break;
1525 }
1526
Auke Kok9d5c8242008-01-24 02:22:38 -08001527 hw->phy.autoneg_wait_to_complete = false;
1528 hw->mac.adaptive_ifs = true;
1529
1530 /* Copper options */
1531 if (hw->phy.media_type == e1000_media_type_copper) {
1532 hw->phy.mdix = AUTO_ALL_MODES;
1533 hw->phy.disable_polarity_correction = false;
1534 hw->phy.ms_type = e1000_ms_hw_default;
1535 }
1536
1537 if (igb_check_reset_block(hw))
1538 dev_info(&pdev->dev,
1539 "PHY reset is blocked due to SOL/IDER session.\n");
1540
1541 netdev->features = NETIF_F_SG |
Alexander Duyck7d8eb292009-02-06 23:18:27 +00001542 NETIF_F_IP_CSUM |
Auke Kok9d5c8242008-01-24 02:22:38 -08001543 NETIF_F_HW_VLAN_TX |
1544 NETIF_F_HW_VLAN_RX |
1545 NETIF_F_HW_VLAN_FILTER;
1546
Alexander Duyck7d8eb292009-02-06 23:18:27 +00001547 netdev->features |= NETIF_F_IPV6_CSUM;
Auke Kok9d5c8242008-01-24 02:22:38 -08001548 netdev->features |= NETIF_F_TSO;
Auke Kok9d5c8242008-01-24 02:22:38 -08001549 netdev->features |= NETIF_F_TSO6;
Jeff Kirsher48f29ff2008-06-05 04:06:27 -07001550
Herbert Xu5c0999b2009-01-19 15:20:57 -08001551 netdev->features |= NETIF_F_GRO;
Alexander Duyckd3352522008-07-08 15:12:13 -07001552
Jeff Kirsher48f29ff2008-06-05 04:06:27 -07001553 netdev->vlan_features |= NETIF_F_TSO;
1554 netdev->vlan_features |= NETIF_F_TSO6;
Alexander Duyck7d8eb292009-02-06 23:18:27 +00001555 netdev->vlan_features |= NETIF_F_IP_CSUM;
Alexander Duyckcd1da502009-08-25 04:47:50 +00001556 netdev->vlan_features |= NETIF_F_IPV6_CSUM;
Jeff Kirsher48f29ff2008-06-05 04:06:27 -07001557 netdev->vlan_features |= NETIF_F_SG;
1558
Auke Kok9d5c8242008-01-24 02:22:38 -08001559 if (pci_using_dac)
1560 netdev->features |= NETIF_F_HIGHDMA;
1561
Jesse Brandeburgb9473562009-04-27 22:36:13 +00001562 if (adapter->hw.mac.type == e1000_82576)
1563 netdev->features |= NETIF_F_SCTP_CSUM;
1564
Auke Kok9d5c8242008-01-24 02:22:38 -08001565 adapter->en_mng_pt = igb_enable_mng_pass_thru(&adapter->hw);
1566
1567 /* before reading the NVM, reset the controller to put the device in a
1568 * known good starting state */
1569 hw->mac.ops.reset_hw(hw);
1570
1571 /* make sure the NVM is good */
1572 if (igb_validate_nvm_checksum(hw) < 0) {
1573 dev_err(&pdev->dev, "The NVM Checksum Is Not Valid\n");
1574 err = -EIO;
1575 goto err_eeprom;
1576 }
1577
1578 /* copy the MAC address out of the NVM */
1579 if (hw->mac.ops.read_mac_addr(hw))
1580 dev_err(&pdev->dev, "NVM Read Error\n");
1581
1582 memcpy(netdev->dev_addr, hw->mac.addr, netdev->addr_len);
1583 memcpy(netdev->perm_addr, hw->mac.addr, netdev->addr_len);
1584
1585 if (!is_valid_ether_addr(netdev->perm_addr)) {
1586 dev_err(&pdev->dev, "Invalid MAC Address\n");
1587 err = -EIO;
1588 goto err_eeprom;
1589 }
1590
Alexander Duyck0e340482009-03-20 00:17:08 +00001591 setup_timer(&adapter->watchdog_timer, &igb_watchdog,
1592 (unsigned long) adapter);
1593 setup_timer(&adapter->phy_info_timer, &igb_update_phy_info,
1594 (unsigned long) adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08001595
1596 INIT_WORK(&adapter->reset_task, igb_reset_task);
1597 INIT_WORK(&adapter->watchdog_task, igb_watchdog_task);
1598
Alexander Duyck450c87c2009-02-06 23:22:11 +00001599 /* Initialize link properties that are user-changeable */
Auke Kok9d5c8242008-01-24 02:22:38 -08001600 adapter->fc_autoneg = true;
1601 hw->mac.autoneg = true;
1602 hw->phy.autoneg_advertised = 0x2f;
1603
Alexander Duyck0cce1192009-07-23 18:10:24 +00001604 hw->fc.requested_mode = e1000_fc_default;
1605 hw->fc.current_mode = e1000_fc_default;
Auke Kok9d5c8242008-01-24 02:22:38 -08001606
Alexander Duyckcbd347a2009-02-15 23:59:44 -08001607 adapter->itr_setting = IGB_DEFAULT_ITR;
Auke Kok9d5c8242008-01-24 02:22:38 -08001608 adapter->itr = IGB_START_ITR;
1609
1610 igb_validate_mdi_setting(hw);
1611
Auke Kok9d5c8242008-01-24 02:22:38 -08001612 /* Initial Wake on LAN setting If APM wake is enabled in the EEPROM,
1613 * enable the ACPI Magic Packet filter
1614 */
1615
Alexander Duycka2cf8b62009-03-13 20:41:17 +00001616 if (hw->bus.func == 0)
Alexander Duyck312c75a2009-02-06 23:17:47 +00001617 hw->nvm.ops.read(hw, NVM_INIT_CONTROL3_PORT_A, 1, &eeprom_data);
Alexander Duycka2cf8b62009-03-13 20:41:17 +00001618 else if (hw->bus.func == 1)
1619 hw->nvm.ops.read(hw, NVM_INIT_CONTROL3_PORT_B, 1, &eeprom_data);
Auke Kok9d5c8242008-01-24 02:22:38 -08001620
1621 if (eeprom_data & eeprom_apme_mask)
1622 adapter->eeprom_wol |= E1000_WUFC_MAG;
1623
1624 /* now that we have the eeprom settings, apply the special cases where
1625 * the eeprom may be wrong or the board simply won't support wake on
1626 * lan on a particular port */
1627 switch (pdev->device) {
1628 case E1000_DEV_ID_82575GB_QUAD_COPPER:
1629 adapter->eeprom_wol = 0;
1630 break;
1631 case E1000_DEV_ID_82575EB_FIBER_SERDES:
Alexander Duyck2d064c02008-07-08 15:10:12 -07001632 case E1000_DEV_ID_82576_FIBER:
1633 case E1000_DEV_ID_82576_SERDES:
Auke Kok9d5c8242008-01-24 02:22:38 -08001634 /* Wake events only supported on port A for dual fiber
1635 * regardless of eeprom setting */
1636 if (rd32(E1000_STATUS) & E1000_STATUS_FUNC_1)
1637 adapter->eeprom_wol = 0;
1638 break;
Alexander Duyckc8ea5ea2009-03-13 20:42:35 +00001639 case E1000_DEV_ID_82576_QUAD_COPPER:
1640 /* if quad port adapter, disable WoL on all but port A */
1641 if (global_quad_port_a != 0)
1642 adapter->eeprom_wol = 0;
1643 else
1644 adapter->flags |= IGB_FLAG_QUAD_PORT_A;
1645 /* Reset for multiple quad port adapters */
1646 if (++global_quad_port_a == 4)
1647 global_quad_port_a = 0;
1648 break;
Auke Kok9d5c8242008-01-24 02:22:38 -08001649 }
1650
1651 /* initialize the wol settings based on the eeprom settings */
1652 adapter->wol = adapter->eeprom_wol;
\"Rafael J. Wysocki\e1b86d82008-11-07 20:30:37 +00001653 device_set_wakeup_enable(&adapter->pdev->dev, adapter->wol);
Auke Kok9d5c8242008-01-24 02:22:38 -08001654
1655 /* reset the hardware with the new settings */
1656 igb_reset(adapter);
1657
1658 /* let the f/w know that the h/w is now under the control of the
1659 * driver. */
1660 igb_get_hw_control(adapter);
1661
Auke Kok9d5c8242008-01-24 02:22:38 -08001662 strcpy(netdev->name, "eth%d");
1663 err = register_netdev(netdev);
1664 if (err)
1665 goto err_register;
1666
Jesse Brandeburgb168dfc2009-04-17 20:44:32 +00001667 /* carrier off reporting is important to ethtool even BEFORE open */
1668 netif_carrier_off(netdev);
1669
Jeff Kirsher421e02f2008-10-17 11:08:31 -07001670#ifdef CONFIG_IGB_DCA
Alexander Duyckbbd98fe2009-01-31 00:52:30 -08001671 if (dca_add_requester(&pdev->dev) == 0) {
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07001672 adapter->flags |= IGB_FLAG_DCA_ENABLED;
Jeb Cramerfe4506b2008-07-08 15:07:55 -07001673 dev_info(&pdev->dev, "DCA enabled\n");
Jeb Cramerfe4506b2008-07-08 15:07:55 -07001674 igb_setup_dca(adapter);
1675 }
1676#endif
1677
Patrick Ohly38c845c2009-02-12 05:03:41 +00001678 /*
1679 * Initialize hardware timer: we keep it running just in case
1680 * that some program needs it later on.
1681 */
1682 memset(&adapter->cycles, 0, sizeof(adapter->cycles));
1683 adapter->cycles.read = igb_read_clock;
1684 adapter->cycles.mask = CLOCKSOURCE_MASK(64);
1685 adapter->cycles.mult = 1;
1686 adapter->cycles.shift = IGB_TSYNC_SHIFT;
1687 wr32(E1000_TIMINCA,
1688 (1<<24) |
1689 IGB_TSYNC_CYCLE_TIME_IN_NANOSECONDS * IGB_TSYNC_SCALE);
1690#if 0
1691 /*
1692 * Avoid rollover while we initialize by resetting the time counter.
1693 */
1694 wr32(E1000_SYSTIML, 0x00000000);
1695 wr32(E1000_SYSTIMH, 0x00000000);
1696#else
1697 /*
1698 * Set registers so that rollover occurs soon to test this.
1699 */
1700 wr32(E1000_SYSTIML, 0x00000000);
1701 wr32(E1000_SYSTIMH, 0xFF800000);
1702#endif
1703 wrfl();
1704 timecounter_init(&adapter->clock,
1705 &adapter->cycles,
1706 ktime_to_ns(ktime_get_real()));
1707
Patrick Ohly33af6bc2009-02-12 05:03:43 +00001708 /*
1709 * Synchronize our NIC clock against system wall clock. NIC
1710 * time stamp reading requires ~3us per sample, each sample
1711 * was pretty stable even under load => only require 10
1712 * samples for each offset comparison.
1713 */
1714 memset(&adapter->compare, 0, sizeof(adapter->compare));
1715 adapter->compare.source = &adapter->clock;
1716 adapter->compare.target = ktime_get_real;
1717 adapter->compare.num_samples = 10;
1718 timecompare_update(&adapter->compare, 0);
1719
Patrick Ohly38c845c2009-02-12 05:03:41 +00001720#ifdef DEBUG
1721 {
1722 char buffer[160];
1723 printk(KERN_DEBUG
1724 "igb: %s: hw %p initialized timer\n",
1725 igb_get_time_str(adapter, buffer),
1726 &adapter->hw);
1727 }
1728#endif
1729
Auke Kok9d5c8242008-01-24 02:22:38 -08001730 dev_info(&pdev->dev, "Intel(R) Gigabit Ethernet Network Connection\n");
1731 /* print bus type/speed/width info */
Johannes Berg7c510e42008-10-27 17:47:26 -07001732 dev_info(&pdev->dev, "%s: (PCIe:%s:%s) %pM\n",
Auke Kok9d5c8242008-01-24 02:22:38 -08001733 netdev->name,
1734 ((hw->bus.speed == e1000_bus_speed_2500)
1735 ? "2.5Gb/s" : "unknown"),
Alexander Duyck59c3de82009-03-31 20:38:00 +00001736 ((hw->bus.width == e1000_bus_width_pcie_x4) ? "Width x4" :
1737 (hw->bus.width == e1000_bus_width_pcie_x2) ? "Width x2" :
1738 (hw->bus.width == e1000_bus_width_pcie_x1) ? "Width x1" :
1739 "unknown"),
Johannes Berg7c510e42008-10-27 17:47:26 -07001740 netdev->dev_addr);
Auke Kok9d5c8242008-01-24 02:22:38 -08001741
1742 igb_read_part_num(hw, &part_num);
1743 dev_info(&pdev->dev, "%s: PBA No: %06x-%03x\n", netdev->name,
1744 (part_num >> 8), (part_num & 0xff));
1745
1746 dev_info(&pdev->dev,
1747 "Using %s interrupts. %d rx queue(s), %d tx queue(s)\n",
1748 adapter->msix_entries ? "MSI-X" :
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07001749 (adapter->flags & IGB_FLAG_HAS_MSI) ? "MSI" : "legacy",
Auke Kok9d5c8242008-01-24 02:22:38 -08001750 adapter->num_rx_queues, adapter->num_tx_queues);
1751
Auke Kok9d5c8242008-01-24 02:22:38 -08001752 return 0;
1753
1754err_register:
1755 igb_release_hw_control(adapter);
1756err_eeprom:
1757 if (!igb_check_reset_block(hw))
Alexander Duyckf5f4cf02008-11-21 21:30:24 -08001758 igb_reset_phy(hw);
Auke Kok9d5c8242008-01-24 02:22:38 -08001759
1760 if (hw->flash_address)
1761 iounmap(hw->flash_address);
Auke Kok9d5c8242008-01-24 02:22:38 -08001762err_sw_init:
Alexander Duyck047e0032009-10-27 15:49:27 +00001763 igb_clear_interrupt_scheme(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08001764 iounmap(hw->hw_addr);
1765err_ioremap:
1766 free_netdev(netdev);
1767err_alloc_etherdev:
Alexander Duyckaed5dec2009-02-06 23:16:04 +00001768 pci_release_selected_regions(pdev, pci_select_bars(pdev,
1769 IORESOURCE_MEM));
Auke Kok9d5c8242008-01-24 02:22:38 -08001770err_pci_reg:
1771err_dma:
1772 pci_disable_device(pdev);
1773 return err;
1774}
1775
1776/**
1777 * igb_remove - Device Removal Routine
1778 * @pdev: PCI device information struct
1779 *
1780 * igb_remove is called by the PCI subsystem to alert the driver
1781 * that it should release a PCI device. The could be caused by a
1782 * Hot-Plug event, or because the driver is going to be removed from
1783 * memory.
1784 **/
1785static void __devexit igb_remove(struct pci_dev *pdev)
1786{
1787 struct net_device *netdev = pci_get_drvdata(pdev);
1788 struct igb_adapter *adapter = netdev_priv(netdev);
Jeb Cramerfe4506b2008-07-08 15:07:55 -07001789 struct e1000_hw *hw = &adapter->hw;
Auke Kok9d5c8242008-01-24 02:22:38 -08001790
1791 /* flush_scheduled work may reschedule our watchdog task, so
1792 * explicitly disable watchdog tasks from being rescheduled */
1793 set_bit(__IGB_DOWN, &adapter->state);
1794 del_timer_sync(&adapter->watchdog_timer);
1795 del_timer_sync(&adapter->phy_info_timer);
1796
1797 flush_scheduled_work();
1798
Jeff Kirsher421e02f2008-10-17 11:08:31 -07001799#ifdef CONFIG_IGB_DCA
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07001800 if (adapter->flags & IGB_FLAG_DCA_ENABLED) {
Jeb Cramerfe4506b2008-07-08 15:07:55 -07001801 dev_info(&pdev->dev, "DCA disabled\n");
1802 dca_remove_requester(&pdev->dev);
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07001803 adapter->flags &= ~IGB_FLAG_DCA_ENABLED;
Alexander Duyckcbd347a2009-02-15 23:59:44 -08001804 wr32(E1000_DCA_CTRL, E1000_DCA_CTRL_DCA_MODE_DISABLE);
Jeb Cramerfe4506b2008-07-08 15:07:55 -07001805 }
1806#endif
1807
Auke Kok9d5c8242008-01-24 02:22:38 -08001808 /* Release control of h/w to f/w. If f/w is AMT enabled, this
1809 * would have already happened in close and is redundant. */
1810 igb_release_hw_control(adapter);
1811
1812 unregister_netdev(netdev);
1813
Alexander Duyckf5f4cf02008-11-21 21:30:24 -08001814 if (!igb_check_reset_block(&adapter->hw))
1815 igb_reset_phy(&adapter->hw);
Auke Kok9d5c8242008-01-24 02:22:38 -08001816
Alexander Duyck047e0032009-10-27 15:49:27 +00001817 igb_clear_interrupt_scheme(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08001818
Alexander Duyck37680112009-02-19 20:40:30 -08001819#ifdef CONFIG_PCI_IOV
1820 /* reclaim resources allocated to VFs */
1821 if (adapter->vf_data) {
1822 /* disable iov and allow time for transactions to clear */
1823 pci_disable_sriov(pdev);
1824 msleep(500);
1825
1826 kfree(adapter->vf_data);
1827 adapter->vf_data = NULL;
1828 wr32(E1000_IOVCTL, E1000_IOVCTL_REUSE_VFQ);
1829 msleep(100);
1830 dev_info(&pdev->dev, "IOV Disabled\n");
1831 }
1832#endif
Alexander Duyck28b07592009-02-06 23:20:31 +00001833 iounmap(hw->hw_addr);
1834 if (hw->flash_address)
1835 iounmap(hw->flash_address);
Alexander Duyckaed5dec2009-02-06 23:16:04 +00001836 pci_release_selected_regions(pdev, pci_select_bars(pdev,
1837 IORESOURCE_MEM));
Auke Kok9d5c8242008-01-24 02:22:38 -08001838
1839 free_netdev(netdev);
1840
Frans Pop19d5afd2009-10-02 10:04:12 -07001841 pci_disable_pcie_error_reporting(pdev);
Alexander Duyck40a914f2008-11-27 00:24:37 -08001842
Auke Kok9d5c8242008-01-24 02:22:38 -08001843 pci_disable_device(pdev);
1844}
1845
1846/**
1847 * igb_sw_init - Initialize general software structures (struct igb_adapter)
1848 * @adapter: board private structure to initialize
1849 *
1850 * igb_sw_init initializes the Adapter private data structure.
1851 * Fields are initialized based on PCI device information and
1852 * OS network device settings (MTU size).
1853 **/
1854static int __devinit igb_sw_init(struct igb_adapter *adapter)
1855{
1856 struct e1000_hw *hw = &adapter->hw;
1857 struct net_device *netdev = adapter->netdev;
1858 struct pci_dev *pdev = adapter->pdev;
1859
1860 pci_read_config_word(pdev, PCI_COMMAND, &hw->bus.pci_cmd_word);
1861
Alexander Duyck68fd9912008-11-20 00:48:10 -08001862 adapter->tx_ring_count = IGB_DEFAULT_TXD;
1863 adapter->rx_ring_count = IGB_DEFAULT_RXD;
Auke Kok9d5c8242008-01-24 02:22:38 -08001864 adapter->rx_buffer_len = MAXIMUM_ETHERNET_VLAN_SIZE;
1865 adapter->rx_ps_hdr_size = 0; /* disable packet split */
1866 adapter->max_frame_size = netdev->mtu + ETH_HLEN + ETH_FCS_LEN;
1867 adapter->min_frame_size = ETH_ZLEN + ETH_FCS_LEN;
1868
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07001869 /* This call may decrease the number of queues depending on
1870 * interrupt mode. */
Alexander Duyck047e0032009-10-27 15:49:27 +00001871 if (igb_init_interrupt_scheme(adapter)) {
Auke Kok9d5c8242008-01-24 02:22:38 -08001872 dev_err(&pdev->dev, "Unable to allocate memory for queues\n");
1873 return -ENOMEM;
1874 }
1875
1876 /* Explicitly disable IRQ since the NIC can be in any state. */
1877 igb_irq_disable(adapter);
1878
1879 set_bit(__IGB_DOWN, &adapter->state);
1880 return 0;
1881}
1882
1883/**
1884 * igb_open - Called when a network interface is made active
1885 * @netdev: network interface device structure
1886 *
1887 * Returns 0 on success, negative value on failure
1888 *
1889 * The open entry point is called when a network interface is made
1890 * active by the system (IFF_UP). At this point all resources needed
1891 * for transmit and receive operations are allocated, the interrupt
1892 * handler is registered with the OS, the watchdog timer is started,
1893 * and the stack is notified that the interface is ready.
1894 **/
1895static int igb_open(struct net_device *netdev)
1896{
1897 struct igb_adapter *adapter = netdev_priv(netdev);
1898 struct e1000_hw *hw = &adapter->hw;
1899 int err;
1900 int i;
1901
1902 /* disallow open during test */
1903 if (test_bit(__IGB_TESTING, &adapter->state))
1904 return -EBUSY;
1905
Jesse Brandeburgb168dfc2009-04-17 20:44:32 +00001906 netif_carrier_off(netdev);
1907
Auke Kok9d5c8242008-01-24 02:22:38 -08001908 /* allocate transmit descriptors */
1909 err = igb_setup_all_tx_resources(adapter);
1910 if (err)
1911 goto err_setup_tx;
1912
1913 /* allocate receive descriptors */
1914 err = igb_setup_all_rx_resources(adapter);
1915 if (err)
1916 goto err_setup_rx;
1917
1918 /* e1000_power_up_phy(adapter); */
1919
1920 adapter->mng_vlan_id = IGB_MNG_VLAN_NONE;
1921 if ((adapter->hw.mng_cookie.status &
1922 E1000_MNG_DHCP_COOKIE_STATUS_VLAN))
1923 igb_update_mng_vlan(adapter);
1924
1925 /* before we allocate an interrupt, we must be ready to handle it.
1926 * Setting DEBUG_SHIRQ in the kernel makes it fire an interrupt
1927 * as soon as we call pci_request_irq, so we have to setup our
1928 * clean_rx handler before we do so. */
1929 igb_configure(adapter);
1930
Alexander Duyck4ae196d2009-02-19 20:40:07 -08001931 igb_vmm_control(adapter);
Alexander Duycke1739522009-02-19 20:39:44 -08001932 igb_set_vmolr(hw, adapter->vfs_allocated_count);
1933
Auke Kok9d5c8242008-01-24 02:22:38 -08001934 err = igb_request_irq(adapter);
1935 if (err)
1936 goto err_req_irq;
1937
1938 /* From here on the code is the same as igb_up() */
1939 clear_bit(__IGB_DOWN, &adapter->state);
1940
Alexander Duyck047e0032009-10-27 15:49:27 +00001941 for (i = 0; i < adapter->num_q_vectors; i++) {
1942 struct igb_q_vector *q_vector = adapter->q_vector[i];
1943 napi_enable(&q_vector->napi);
1944 }
Auke Kok9d5c8242008-01-24 02:22:38 -08001945
1946 /* Clear any pending interrupts. */
1947 rd32(E1000_ICR);
PJ Waskiewicz844290e2008-06-27 11:00:39 -07001948
1949 igb_irq_enable(adapter);
1950
Jeff Kirsherd55b53f2008-07-18 04:33:03 -07001951 netif_tx_start_all_queues(netdev);
1952
Auke Kok9d5c8242008-01-24 02:22:38 -08001953 /* Fire a link status change interrupt to start the watchdog. */
1954 wr32(E1000_ICS, E1000_ICS_LSC);
1955
1956 return 0;
1957
1958err_req_irq:
1959 igb_release_hw_control(adapter);
1960 /* e1000_power_down_phy(adapter); */
1961 igb_free_all_rx_resources(adapter);
1962err_setup_rx:
1963 igb_free_all_tx_resources(adapter);
1964err_setup_tx:
1965 igb_reset(adapter);
1966
1967 return err;
1968}
1969
1970/**
1971 * igb_close - Disables a network interface
1972 * @netdev: network interface device structure
1973 *
1974 * Returns 0, this is not allowed to fail
1975 *
1976 * The close entry point is called when an interface is de-activated
1977 * by the OS. The hardware is still under the driver's control, but
1978 * needs to be disabled. A global MAC reset is issued to stop the
1979 * hardware, and all transmit and receive resources are freed.
1980 **/
1981static int igb_close(struct net_device *netdev)
1982{
1983 struct igb_adapter *adapter = netdev_priv(netdev);
1984
1985 WARN_ON(test_bit(__IGB_RESETTING, &adapter->state));
1986 igb_down(adapter);
1987
1988 igb_free_irq(adapter);
1989
1990 igb_free_all_tx_resources(adapter);
1991 igb_free_all_rx_resources(adapter);
1992
1993 /* kill manageability vlan ID if supported, but not if a vlan with
1994 * the same ID is registered on the host OS (let 8021q kill it) */
1995 if ((adapter->hw.mng_cookie.status &
1996 E1000_MNG_DHCP_COOKIE_STATUS_VLAN) &&
1997 !(adapter->vlgrp &&
1998 vlan_group_get_device(adapter->vlgrp, adapter->mng_vlan_id)))
1999 igb_vlan_rx_kill_vid(netdev, adapter->mng_vlan_id);
2000
2001 return 0;
2002}
2003
2004/**
2005 * igb_setup_tx_resources - allocate Tx resources (Descriptors)
2006 * @adapter: board private structure
2007 * @tx_ring: tx descriptor ring (for a specific queue) to setup
2008 *
2009 * Return 0 on success, negative on failure
2010 **/
Auke Kok9d5c8242008-01-24 02:22:38 -08002011int igb_setup_tx_resources(struct igb_adapter *adapter,
2012 struct igb_ring *tx_ring)
2013{
2014 struct pci_dev *pdev = adapter->pdev;
2015 int size;
2016
2017 size = sizeof(struct igb_buffer) * tx_ring->count;
2018 tx_ring->buffer_info = vmalloc(size);
2019 if (!tx_ring->buffer_info)
2020 goto err;
2021 memset(tx_ring->buffer_info, 0, size);
2022
2023 /* round up to nearest 4K */
Alexander Duyck85e8d002009-02-16 00:00:20 -08002024 tx_ring->size = tx_ring->count * sizeof(union e1000_adv_tx_desc);
Auke Kok9d5c8242008-01-24 02:22:38 -08002025 tx_ring->size = ALIGN(tx_ring->size, 4096);
2026
2027 tx_ring->desc = pci_alloc_consistent(pdev, tx_ring->size,
2028 &tx_ring->dma);
2029
2030 if (!tx_ring->desc)
2031 goto err;
2032
Auke Kok9d5c8242008-01-24 02:22:38 -08002033 tx_ring->next_to_use = 0;
2034 tx_ring->next_to_clean = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08002035 return 0;
2036
2037err:
2038 vfree(tx_ring->buffer_info);
Alexander Duyck047e0032009-10-27 15:49:27 +00002039 dev_err(&pdev->dev,
Auke Kok9d5c8242008-01-24 02:22:38 -08002040 "Unable to allocate memory for the transmit descriptor ring\n");
2041 return -ENOMEM;
2042}
2043
2044/**
2045 * igb_setup_all_tx_resources - wrapper to allocate Tx resources
2046 * (Descriptors) for all queues
2047 * @adapter: board private structure
2048 *
2049 * Return 0 on success, negative on failure
2050 **/
2051static int igb_setup_all_tx_resources(struct igb_adapter *adapter)
2052{
2053 int i, err = 0;
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07002054 int r_idx;
Auke Kok9d5c8242008-01-24 02:22:38 -08002055
2056 for (i = 0; i < adapter->num_tx_queues; i++) {
2057 err = igb_setup_tx_resources(adapter, &adapter->tx_ring[i]);
2058 if (err) {
2059 dev_err(&adapter->pdev->dev,
2060 "Allocation for Tx Queue %u failed\n", i);
2061 for (i--; i >= 0; i--)
Mitch Williams3b644cf2008-06-27 10:59:48 -07002062 igb_free_tx_resources(&adapter->tx_ring[i]);
Auke Kok9d5c8242008-01-24 02:22:38 -08002063 break;
2064 }
2065 }
2066
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07002067 for (i = 0; i < IGB_MAX_TX_QUEUES; i++) {
2068 r_idx = i % adapter->num_tx_queues;
2069 adapter->multi_tx_table[i] = &adapter->tx_ring[r_idx];
Alexander Duyckeebbbdb2009-02-06 23:19:29 +00002070 }
Auke Kok9d5c8242008-01-24 02:22:38 -08002071 return err;
2072}
2073
2074/**
Alexander Duyck85b430b2009-10-27 15:50:29 +00002075 * igb_setup_tctl - configure the transmit control registers
2076 * @adapter: Board private structure
Auke Kok9d5c8242008-01-24 02:22:38 -08002077 **/
Alexander Duyck85b430b2009-10-27 15:50:29 +00002078static void igb_setup_tctl(struct igb_adapter *adapter)
Auke Kok9d5c8242008-01-24 02:22:38 -08002079{
Auke Kok9d5c8242008-01-24 02:22:38 -08002080 struct e1000_hw *hw = &adapter->hw;
2081 u32 tctl;
Auke Kok9d5c8242008-01-24 02:22:38 -08002082
Alexander Duyck85b430b2009-10-27 15:50:29 +00002083 /* disable queue 0 which is enabled by default on 82575 and 82576 */
2084 wr32(E1000_TXDCTL(0), 0);
Auke Kok9d5c8242008-01-24 02:22:38 -08002085
2086 /* Program the Transmit Control Register */
Auke Kok9d5c8242008-01-24 02:22:38 -08002087 tctl = rd32(E1000_TCTL);
2088 tctl &= ~E1000_TCTL_CT;
2089 tctl |= E1000_TCTL_PSP | E1000_TCTL_RTLC |
2090 (E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT);
2091
2092 igb_config_collision_dist(hw);
2093
Auke Kok9d5c8242008-01-24 02:22:38 -08002094 /* Enable transmits */
2095 tctl |= E1000_TCTL_EN;
2096
2097 wr32(E1000_TCTL, tctl);
2098}
2099
2100/**
Alexander Duyck85b430b2009-10-27 15:50:29 +00002101 * igb_configure_tx_ring - Configure transmit ring after Reset
2102 * @adapter: board private structure
2103 * @ring: tx ring to configure
2104 *
2105 * Configure a transmit ring after a reset.
2106 **/
2107static void igb_configure_tx_ring(struct igb_adapter *adapter,
2108 struct igb_ring *ring)
2109{
2110 struct e1000_hw *hw = &adapter->hw;
2111 u32 txdctl;
2112 u64 tdba = ring->dma;
2113 int reg_idx = ring->reg_idx;
2114
2115 /* disable the queue */
2116 txdctl = rd32(E1000_TXDCTL(reg_idx));
2117 wr32(E1000_TXDCTL(reg_idx),
2118 txdctl & ~E1000_TXDCTL_QUEUE_ENABLE);
2119 wrfl();
2120 mdelay(10);
2121
2122 wr32(E1000_TDLEN(reg_idx),
2123 ring->count * sizeof(union e1000_adv_tx_desc));
2124 wr32(E1000_TDBAL(reg_idx),
2125 tdba & 0x00000000ffffffffULL);
2126 wr32(E1000_TDBAH(reg_idx), tdba >> 32);
2127
2128 ring->head = E1000_TDH(reg_idx);
2129 ring->tail = E1000_TDT(reg_idx);
2130 writel(0, hw->hw_addr + ring->tail);
2131 writel(0, hw->hw_addr + ring->head);
2132
2133 txdctl |= IGB_TX_PTHRESH;
2134 txdctl |= IGB_TX_HTHRESH << 8;
2135 txdctl |= IGB_TX_WTHRESH << 16;
2136
2137 txdctl |= E1000_TXDCTL_QUEUE_ENABLE;
2138 wr32(E1000_TXDCTL(reg_idx), txdctl);
2139}
2140
2141/**
2142 * igb_configure_tx - Configure transmit Unit after Reset
2143 * @adapter: board private structure
2144 *
2145 * Configure the Tx unit of the MAC after a reset.
2146 **/
2147static void igb_configure_tx(struct igb_adapter *adapter)
2148{
2149 int i;
2150
2151 for (i = 0; i < adapter->num_tx_queues; i++)
2152 igb_configure_tx_ring(adapter, &adapter->tx_ring[i]);
2153
2154 /* Setup Transmit Descriptor Settings for eop descriptor */
2155 adapter->txd_cmd = E1000_TXD_CMD_EOP | E1000_TXD_CMD_RS;
2156}
2157
2158/**
Auke Kok9d5c8242008-01-24 02:22:38 -08002159 * igb_setup_rx_resources - allocate Rx resources (Descriptors)
2160 * @adapter: board private structure
2161 * @rx_ring: rx descriptor ring (for a specific queue) to setup
2162 *
2163 * Returns 0 on success, negative on failure
2164 **/
Auke Kok9d5c8242008-01-24 02:22:38 -08002165int igb_setup_rx_resources(struct igb_adapter *adapter,
2166 struct igb_ring *rx_ring)
2167{
2168 struct pci_dev *pdev = adapter->pdev;
2169 int size, desc_len;
2170
2171 size = sizeof(struct igb_buffer) * rx_ring->count;
2172 rx_ring->buffer_info = vmalloc(size);
2173 if (!rx_ring->buffer_info)
2174 goto err;
2175 memset(rx_ring->buffer_info, 0, size);
2176
2177 desc_len = sizeof(union e1000_adv_rx_desc);
2178
2179 /* Round up to nearest 4K */
2180 rx_ring->size = rx_ring->count * desc_len;
2181 rx_ring->size = ALIGN(rx_ring->size, 4096);
2182
2183 rx_ring->desc = pci_alloc_consistent(pdev, rx_ring->size,
2184 &rx_ring->dma);
2185
2186 if (!rx_ring->desc)
2187 goto err;
2188
2189 rx_ring->next_to_clean = 0;
2190 rx_ring->next_to_use = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08002191
Auke Kok9d5c8242008-01-24 02:22:38 -08002192 return 0;
2193
2194err:
2195 vfree(rx_ring->buffer_info);
2196 dev_err(&adapter->pdev->dev, "Unable to allocate memory for "
2197 "the receive descriptor ring\n");
2198 return -ENOMEM;
2199}
2200
2201/**
2202 * igb_setup_all_rx_resources - wrapper to allocate Rx resources
2203 * (Descriptors) for all queues
2204 * @adapter: board private structure
2205 *
2206 * Return 0 on success, negative on failure
2207 **/
2208static int igb_setup_all_rx_resources(struct igb_adapter *adapter)
2209{
2210 int i, err = 0;
2211
2212 for (i = 0; i < adapter->num_rx_queues; i++) {
2213 err = igb_setup_rx_resources(adapter, &adapter->rx_ring[i]);
2214 if (err) {
2215 dev_err(&adapter->pdev->dev,
2216 "Allocation for Rx Queue %u failed\n", i);
2217 for (i--; i >= 0; i--)
Mitch Williams3b644cf2008-06-27 10:59:48 -07002218 igb_free_rx_resources(&adapter->rx_ring[i]);
Auke Kok9d5c8242008-01-24 02:22:38 -08002219 break;
2220 }
2221 }
2222
2223 return err;
2224}
2225
2226/**
2227 * igb_setup_rctl - configure the receive control registers
2228 * @adapter: Board private structure
2229 **/
2230static void igb_setup_rctl(struct igb_adapter *adapter)
2231{
2232 struct e1000_hw *hw = &adapter->hw;
2233 u32 rctl;
2234 u32 srrctl = 0;
Alexander Duyck77a22942009-05-06 16:43:48 -07002235 int i;
Auke Kok9d5c8242008-01-24 02:22:38 -08002236
2237 rctl = rd32(E1000_RCTL);
2238
2239 rctl &= ~(3 << E1000_RCTL_MO_SHIFT);
Alexander Duyck69d728b2008-11-25 01:04:03 -08002240 rctl &= ~(E1000_RCTL_LBM_TCVR | E1000_RCTL_LBM_MAC);
Auke Kok9d5c8242008-01-24 02:22:38 -08002241
Alexander Duyck69d728b2008-11-25 01:04:03 -08002242 rctl |= E1000_RCTL_EN | E1000_RCTL_BAM | E1000_RCTL_RDMTS_HALF |
Alexander Duyck28b07592009-02-06 23:20:31 +00002243 (hw->mac.mc_filter_type << E1000_RCTL_MO_SHIFT);
Auke Kok9d5c8242008-01-24 02:22:38 -08002244
Auke Kok87cb7e82008-07-08 15:08:29 -07002245 /*
2246 * enable stripping of CRC. It's unlikely this will break BMC
2247 * redirection as it did with e1000. Newer features require
2248 * that the HW strips the CRC.
Alexander Duyck73cd78f2009-02-12 18:16:59 +00002249 */
Auke Kok87cb7e82008-07-08 15:08:29 -07002250 rctl |= E1000_RCTL_SECRC;
Auke Kok9d5c8242008-01-24 02:22:38 -08002251
Alexander Duyck9b07f3d32008-11-25 01:03:26 -08002252 /*
Alexander Duyckec54d7d2009-01-31 00:52:57 -08002253 * disable store bad packets and clear size bits.
Alexander Duyck9b07f3d32008-11-25 01:03:26 -08002254 */
Alexander Duyckec54d7d2009-01-31 00:52:57 -08002255 rctl &= ~(E1000_RCTL_SBP | E1000_RCTL_SZ_256);
Auke Kok9d5c8242008-01-24 02:22:38 -08002256
Alexander Duyckec54d7d2009-01-31 00:52:57 -08002257 /* enable LPE when to prevent packets larger than max_frame_size */
Alexander Duyck9b07f3d32008-11-25 01:03:26 -08002258 rctl |= E1000_RCTL_LPE;
Alexander Duyckb4557be2008-12-10 01:08:59 -08002259
2260 /* Setup buffer sizes */
Alexander Duyck7d95b712009-10-27 15:50:08 +00002261 srrctl = ALIGN(adapter->rx_buffer_len, 1024)
2262 >> E1000_SRRCTL_BSIZEPKT_SHIFT;
Auke Kok9d5c8242008-01-24 02:22:38 -08002263
2264 /* 82575 and greater support packet-split where the protocol
2265 * header is placed in skb->data and the packet data is
2266 * placed in pages hanging off of skb_shinfo(skb)->nr_frags.
2267 * In the case of a non-split, skb->data is linearly filled,
2268 * followed by the page buffers. Therefore, skb->data is
2269 * sized to hold the largest protocol header.
2270 */
2271 /* allocations using alloc_page take too long for regular MTU
2272 * so only enable packet split for jumbo frames */
Alexander Duyckec54d7d2009-01-31 00:52:57 -08002273 if (adapter->netdev->mtu > ETH_DATA_LEN) {
Auke Kok9d5c8242008-01-24 02:22:38 -08002274 adapter->rx_ps_hdr_size = IGB_RXBUFFER_128;
Alexander Duyckbf36c1a2008-07-08 15:11:40 -07002275 srrctl |= adapter->rx_ps_hdr_size <<
Auke Kok9d5c8242008-01-24 02:22:38 -08002276 E1000_SRRCTL_BSIZEHDRSIZE_SHIFT;
Auke Kok9d5c8242008-01-24 02:22:38 -08002277 srrctl |= E1000_SRRCTL_DESCTYPE_HDR_SPLIT_ALWAYS;
2278 } else {
2279 adapter->rx_ps_hdr_size = 0;
2280 srrctl |= E1000_SRRCTL_DESCTYPE_ADV_ONEBUF;
2281 }
2282
Alexander Duycke1739522009-02-19 20:39:44 -08002283 /* Attention!!! For SR-IOV PF driver operations you must enable
2284 * queue drop for all VF and PF queues to prevent head of line blocking
2285 * if an un-trusted VF does not provide descriptors to hardware.
2286 */
2287 if (adapter->vfs_allocated_count) {
2288 u32 vmolr;
2289
Alexander Duycke1739522009-02-19 20:39:44 -08002290 /* set all queue drop enable bits */
2291 wr32(E1000_QDE, ALL_QUEUES);
2292 srrctl |= E1000_SRRCTL_DROP_EN;
2293
2294 /* disable queue 0 to prevent tail write w/o re-config */
2295 wr32(E1000_RXDCTL(0), 0);
2296
Alexander Duyck77a22942009-05-06 16:43:48 -07002297 vmolr = rd32(E1000_VMOLR(adapter->vfs_allocated_count));
Alexander Duycke1739522009-02-19 20:39:44 -08002298 if (rctl & E1000_RCTL_LPE)
2299 vmolr |= E1000_VMOLR_LPE;
Alexander Duyck77a22942009-05-06 16:43:48 -07002300 if (adapter->num_rx_queues > 1)
Alexander Duycke1739522009-02-19 20:39:44 -08002301 vmolr |= E1000_VMOLR_RSSE;
Alexander Duyck77a22942009-05-06 16:43:48 -07002302 wr32(E1000_VMOLR(adapter->vfs_allocated_count), vmolr);
Alexander Duycke1739522009-02-19 20:39:44 -08002303 }
2304
Alexander Duyck26bc19e2008-12-26 01:34:11 -08002305 for (i = 0; i < adapter->num_rx_queues; i++) {
Alexander Duyck77a22942009-05-06 16:43:48 -07002306 int j = adapter->rx_ring[i].reg_idx;
Alexander Duyck26bc19e2008-12-26 01:34:11 -08002307 wr32(E1000_SRRCTL(j), srrctl);
2308 }
Auke Kok9d5c8242008-01-24 02:22:38 -08002309
2310 wr32(E1000_RCTL, rctl);
2311}
2312
2313/**
Alexander Duycke1739522009-02-19 20:39:44 -08002314 * igb_rlpml_set - set maximum receive packet size
2315 * @adapter: board private structure
2316 *
2317 * Configure maximum receivable packet size.
2318 **/
2319static void igb_rlpml_set(struct igb_adapter *adapter)
2320{
2321 u32 max_frame_size = adapter->max_frame_size;
2322 struct e1000_hw *hw = &adapter->hw;
2323 u16 pf_id = adapter->vfs_allocated_count;
2324
2325 if (adapter->vlgrp)
2326 max_frame_size += VLAN_TAG_SIZE;
2327
2328 /* if vfs are enabled we set RLPML to the largest possible request
2329 * size and set the VMOLR RLPML to the size we need */
2330 if (pf_id) {
2331 igb_set_vf_rlpml(adapter, max_frame_size, pf_id);
2332 max_frame_size = MAX_STD_JUMBO_FRAME_SIZE + VLAN_TAG_SIZE;
2333 }
2334
2335 wr32(E1000_RLPML, max_frame_size);
2336}
2337
2338/**
2339 * igb_configure_vt_default_pool - Configure VT default pool
2340 * @adapter: board private structure
2341 *
2342 * Configure the default pool
2343 **/
2344static void igb_configure_vt_default_pool(struct igb_adapter *adapter)
2345{
2346 struct e1000_hw *hw = &adapter->hw;
2347 u16 pf_id = adapter->vfs_allocated_count;
2348 u32 vtctl;
2349
2350 /* not in sr-iov mode - do nothing */
2351 if (!pf_id)
2352 return;
2353
2354 vtctl = rd32(E1000_VT_CTL);
2355 vtctl &= ~(E1000_VT_CTL_DEFAULT_POOL_MASK |
2356 E1000_VT_CTL_DISABLE_DEF_POOL);
2357 vtctl |= pf_id << E1000_VT_CTL_DEFAULT_POOL_SHIFT;
2358 wr32(E1000_VT_CTL, vtctl);
2359}
2360
2361/**
Alexander Duyck85b430b2009-10-27 15:50:29 +00002362 * igb_configure_rx_ring - Configure a receive ring after Reset
2363 * @adapter: board private structure
2364 * @ring: receive ring to be configured
2365 *
2366 * Configure the Rx unit of the MAC after a reset.
2367 **/
2368static void igb_configure_rx_ring(struct igb_adapter *adapter,
2369 struct igb_ring *ring)
2370{
2371 struct e1000_hw *hw = &adapter->hw;
2372 u64 rdba = ring->dma;
2373 int reg_idx = ring->reg_idx;
2374 u32 rxdctl;
2375
2376 /* disable the queue */
2377 rxdctl = rd32(E1000_RXDCTL(reg_idx));
2378 wr32(E1000_RXDCTL(reg_idx),
2379 rxdctl & ~E1000_RXDCTL_QUEUE_ENABLE);
2380
2381 /* Set DMA base address registers */
2382 wr32(E1000_RDBAL(reg_idx),
2383 rdba & 0x00000000ffffffffULL);
2384 wr32(E1000_RDBAH(reg_idx), rdba >> 32);
2385 wr32(E1000_RDLEN(reg_idx),
2386 ring->count * sizeof(union e1000_adv_rx_desc));
2387
2388 /* initialize head and tail */
2389 ring->head = E1000_RDH(reg_idx);
2390 ring->tail = E1000_RDT(reg_idx);
2391 writel(0, hw->hw_addr + ring->head);
2392 writel(0, hw->hw_addr + ring->tail);
2393
2394 /* enable receive descriptor fetching */
2395 rxdctl = rd32(E1000_RXDCTL(reg_idx));
2396 rxdctl |= E1000_RXDCTL_QUEUE_ENABLE;
2397 rxdctl &= 0xFFF00000;
2398 rxdctl |= IGB_RX_PTHRESH;
2399 rxdctl |= IGB_RX_HTHRESH << 8;
2400 rxdctl |= IGB_RX_WTHRESH << 16;
2401 wr32(E1000_RXDCTL(reg_idx), rxdctl);
2402}
2403
2404/**
Auke Kok9d5c8242008-01-24 02:22:38 -08002405 * igb_configure_rx - Configure receive Unit after Reset
2406 * @adapter: board private structure
2407 *
2408 * Configure the Rx unit of the MAC after a reset.
2409 **/
2410static void igb_configure_rx(struct igb_adapter *adapter)
2411{
Auke Kok9d5c8242008-01-24 02:22:38 -08002412 struct e1000_hw *hw = &adapter->hw;
2413 u32 rctl, rxcsum;
Hannes Eder91075842009-02-18 19:36:04 -08002414 int i;
Auke Kok9d5c8242008-01-24 02:22:38 -08002415
2416 /* disable receives while setting up the descriptors */
2417 rctl = rd32(E1000_RCTL);
2418 wr32(E1000_RCTL, rctl & ~E1000_RCTL_EN);
2419 wrfl();
2420 mdelay(10);
2421
2422 if (adapter->itr_setting > 3)
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07002423 wr32(E1000_ITR, adapter->itr);
Auke Kok9d5c8242008-01-24 02:22:38 -08002424
2425 /* Setup the HW Rx Head and Tail Descriptor Pointers and
2426 * the Base and Length of the Rx Descriptor Ring */
Alexander Duyck85b430b2009-10-27 15:50:29 +00002427 for (i = 0; i < adapter->num_rx_queues; i++)
2428 igb_configure_rx_ring(adapter, &adapter->rx_ring[i]);
Auke Kok9d5c8242008-01-24 02:22:38 -08002429
2430 if (adapter->num_rx_queues > 1) {
2431 u32 random[10];
2432 u32 mrqc;
2433 u32 j, shift;
2434 union e1000_reta {
2435 u32 dword;
2436 u8 bytes[4];
2437 } reta;
2438
2439 get_random_bytes(&random[0], 40);
2440
Alexander Duyck2d064c02008-07-08 15:10:12 -07002441 if (hw->mac.type >= e1000_82576)
2442 shift = 0;
2443 else
2444 shift = 6;
Auke Kok9d5c8242008-01-24 02:22:38 -08002445 for (j = 0; j < (32 * 4); j++) {
2446 reta.bytes[j & 3] =
Alexander Duyck26bc19e2008-12-26 01:34:11 -08002447 adapter->rx_ring[(j % adapter->num_rx_queues)].reg_idx << shift;
Auke Kok9d5c8242008-01-24 02:22:38 -08002448 if ((j & 3) == 3)
2449 writel(reta.dword,
2450 hw->hw_addr + E1000_RETA(0) + (j & ~3));
2451 }
Alexander Duycke1739522009-02-19 20:39:44 -08002452 if (adapter->vfs_allocated_count)
2453 mrqc = E1000_MRQC_ENABLE_VMDQ_RSS_2Q;
2454 else
2455 mrqc = E1000_MRQC_ENABLE_RSS_4Q;
Auke Kok9d5c8242008-01-24 02:22:38 -08002456
2457 /* Fill out hash function seeds */
2458 for (j = 0; j < 10; j++)
2459 array_wr32(E1000_RSSRK(0), j, random[j]);
2460
2461 mrqc |= (E1000_MRQC_RSS_FIELD_IPV4 |
2462 E1000_MRQC_RSS_FIELD_IPV4_TCP);
2463 mrqc |= (E1000_MRQC_RSS_FIELD_IPV6 |
2464 E1000_MRQC_RSS_FIELD_IPV6_TCP);
2465 mrqc |= (E1000_MRQC_RSS_FIELD_IPV4_UDP |
2466 E1000_MRQC_RSS_FIELD_IPV6_UDP);
2467 mrqc |= (E1000_MRQC_RSS_FIELD_IPV6_UDP_EX |
2468 E1000_MRQC_RSS_FIELD_IPV6_TCP_EX);
2469
Auke Kok9d5c8242008-01-24 02:22:38 -08002470 wr32(E1000_MRQC, mrqc);
Alexander Duyck2844f792009-04-27 22:35:14 +00002471 } else if (adapter->vfs_allocated_count) {
Alexander Duycke1739522009-02-19 20:39:44 -08002472 /* Enable multi-queue for sr-iov */
Alexander Duyck2844f792009-04-27 22:35:14 +00002473 wr32(E1000_MRQC, E1000_MRQC_ENABLE_VMDQ);
Auke Kok9d5c8242008-01-24 02:22:38 -08002474 }
2475
Alexander Duyck2844f792009-04-27 22:35:14 +00002476 /* Enable Receive Checksum Offload for TCP and UDP */
2477 rxcsum = rd32(E1000_RXCSUM);
2478 /* Disable raw packet checksumming */
2479 rxcsum |= E1000_RXCSUM_PCSD;
Alexander Duyck7beb0142009-05-06 10:25:23 +00002480
2481 if (adapter->hw.mac.type == e1000_82576)
Jesse Brandeburgb9473562009-04-27 22:36:13 +00002482 /* Enable Receive Checksum Offload for SCTP */
2483 rxcsum |= E1000_RXCSUM_CRCOFL;
2484
Alexander Duyck7beb0142009-05-06 10:25:23 +00002485 /* Don't need to set TUOFL or IPOFL, they default to 1 */
Alexander Duyck2844f792009-04-27 22:35:14 +00002486 wr32(E1000_RXCSUM, rxcsum);
2487
Alexander Duycke1739522009-02-19 20:39:44 -08002488 /* Set the default pool for the PF's first queue */
2489 igb_configure_vt_default_pool(adapter);
2490
Alexander Duyck68d480c2009-10-05 06:33:08 +00002491 /* set UTA to appropriate mode */
2492 igb_set_uta(adapter);
2493
Alexander Duyck26ad9172009-10-05 06:32:49 +00002494 /* set the correct pool for the PF default MAC address in entry 0 */
2495 igb_rar_set_qsel(adapter, adapter->hw.mac.addr, 0,
2496 adapter->vfs_allocated_count);
2497
Alexander Duycke1739522009-02-19 20:39:44 -08002498 igb_rlpml_set(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08002499
2500 /* Enable Receives */
2501 wr32(E1000_RCTL, rctl);
2502}
2503
2504/**
2505 * igb_free_tx_resources - Free Tx Resources per Queue
Auke Kok9d5c8242008-01-24 02:22:38 -08002506 * @tx_ring: Tx descriptor ring for a specific queue
2507 *
2508 * Free all transmit software resources
2509 **/
Alexander Duyck68fd9912008-11-20 00:48:10 -08002510void igb_free_tx_resources(struct igb_ring *tx_ring)
Auke Kok9d5c8242008-01-24 02:22:38 -08002511{
Alexander Duyck047e0032009-10-27 15:49:27 +00002512 struct pci_dev *pdev = tx_ring->q_vector->adapter->pdev;
Auke Kok9d5c8242008-01-24 02:22:38 -08002513
Mitch Williams3b644cf2008-06-27 10:59:48 -07002514 igb_clean_tx_ring(tx_ring);
Auke Kok9d5c8242008-01-24 02:22:38 -08002515
2516 vfree(tx_ring->buffer_info);
2517 tx_ring->buffer_info = NULL;
2518
2519 pci_free_consistent(pdev, tx_ring->size, tx_ring->desc, tx_ring->dma);
2520
2521 tx_ring->desc = NULL;
2522}
2523
2524/**
2525 * igb_free_all_tx_resources - Free Tx Resources for All Queues
2526 * @adapter: board private structure
2527 *
2528 * Free all transmit software resources
2529 **/
2530static void igb_free_all_tx_resources(struct igb_adapter *adapter)
2531{
2532 int i;
2533
2534 for (i = 0; i < adapter->num_tx_queues; i++)
Mitch Williams3b644cf2008-06-27 10:59:48 -07002535 igb_free_tx_resources(&adapter->tx_ring[i]);
Auke Kok9d5c8242008-01-24 02:22:38 -08002536}
2537
2538static void igb_unmap_and_free_tx_resource(struct igb_adapter *adapter,
2539 struct igb_buffer *buffer_info)
2540{
Alexander Duyck65689fe2009-03-20 00:17:43 +00002541 buffer_info->dma = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08002542 if (buffer_info->skb) {
Alexander Duyck65689fe2009-03-20 00:17:43 +00002543 skb_dma_unmap(&adapter->pdev->dev, buffer_info->skb,
2544 DMA_TO_DEVICE);
Auke Kok9d5c8242008-01-24 02:22:38 -08002545 dev_kfree_skb_any(buffer_info->skb);
2546 buffer_info->skb = NULL;
2547 }
2548 buffer_info->time_stamp = 0;
2549 /* buffer_info must be completely set up in the transmit path */
2550}
2551
2552/**
2553 * igb_clean_tx_ring - Free Tx Buffers
Auke Kok9d5c8242008-01-24 02:22:38 -08002554 * @tx_ring: ring to be cleaned
2555 **/
Mitch Williams3b644cf2008-06-27 10:59:48 -07002556static void igb_clean_tx_ring(struct igb_ring *tx_ring)
Auke Kok9d5c8242008-01-24 02:22:38 -08002557{
Alexander Duyck047e0032009-10-27 15:49:27 +00002558 struct igb_adapter *adapter = tx_ring->q_vector->adapter;
Auke Kok9d5c8242008-01-24 02:22:38 -08002559 struct igb_buffer *buffer_info;
2560 unsigned long size;
2561 unsigned int i;
2562
2563 if (!tx_ring->buffer_info)
2564 return;
2565 /* Free all the Tx ring sk_buffs */
2566
2567 for (i = 0; i < tx_ring->count; i++) {
2568 buffer_info = &tx_ring->buffer_info[i];
2569 igb_unmap_and_free_tx_resource(adapter, buffer_info);
2570 }
2571
2572 size = sizeof(struct igb_buffer) * tx_ring->count;
2573 memset(tx_ring->buffer_info, 0, size);
2574
2575 /* Zero out the descriptor ring */
2576
2577 memset(tx_ring->desc, 0, tx_ring->size);
2578
2579 tx_ring->next_to_use = 0;
2580 tx_ring->next_to_clean = 0;
2581
2582 writel(0, adapter->hw.hw_addr + tx_ring->head);
2583 writel(0, adapter->hw.hw_addr + tx_ring->tail);
2584}
2585
2586/**
2587 * igb_clean_all_tx_rings - Free Tx Buffers for all queues
2588 * @adapter: board private structure
2589 **/
2590static void igb_clean_all_tx_rings(struct igb_adapter *adapter)
2591{
2592 int i;
2593
2594 for (i = 0; i < adapter->num_tx_queues; i++)
Mitch Williams3b644cf2008-06-27 10:59:48 -07002595 igb_clean_tx_ring(&adapter->tx_ring[i]);
Auke Kok9d5c8242008-01-24 02:22:38 -08002596}
2597
2598/**
2599 * igb_free_rx_resources - Free Rx Resources
Auke Kok9d5c8242008-01-24 02:22:38 -08002600 * @rx_ring: ring to clean the resources from
2601 *
2602 * Free all receive software resources
2603 **/
Alexander Duyck68fd9912008-11-20 00:48:10 -08002604void igb_free_rx_resources(struct igb_ring *rx_ring)
Auke Kok9d5c8242008-01-24 02:22:38 -08002605{
Alexander Duyck047e0032009-10-27 15:49:27 +00002606 struct pci_dev *pdev = rx_ring->q_vector->adapter->pdev;
Auke Kok9d5c8242008-01-24 02:22:38 -08002607
Mitch Williams3b644cf2008-06-27 10:59:48 -07002608 igb_clean_rx_ring(rx_ring);
Auke Kok9d5c8242008-01-24 02:22:38 -08002609
2610 vfree(rx_ring->buffer_info);
2611 rx_ring->buffer_info = NULL;
2612
2613 pci_free_consistent(pdev, rx_ring->size, rx_ring->desc, rx_ring->dma);
2614
2615 rx_ring->desc = NULL;
2616}
2617
2618/**
2619 * igb_free_all_rx_resources - Free Rx Resources for All Queues
2620 * @adapter: board private structure
2621 *
2622 * Free all receive software resources
2623 **/
2624static void igb_free_all_rx_resources(struct igb_adapter *adapter)
2625{
2626 int i;
2627
2628 for (i = 0; i < adapter->num_rx_queues; i++)
Mitch Williams3b644cf2008-06-27 10:59:48 -07002629 igb_free_rx_resources(&adapter->rx_ring[i]);
Auke Kok9d5c8242008-01-24 02:22:38 -08002630}
2631
2632/**
2633 * igb_clean_rx_ring - Free Rx Buffers per Queue
Auke Kok9d5c8242008-01-24 02:22:38 -08002634 * @rx_ring: ring to free buffers from
2635 **/
Mitch Williams3b644cf2008-06-27 10:59:48 -07002636static void igb_clean_rx_ring(struct igb_ring *rx_ring)
Auke Kok9d5c8242008-01-24 02:22:38 -08002637{
Alexander Duyck047e0032009-10-27 15:49:27 +00002638 struct igb_adapter *adapter = rx_ring->q_vector->adapter;
Auke Kok9d5c8242008-01-24 02:22:38 -08002639 struct igb_buffer *buffer_info;
2640 struct pci_dev *pdev = adapter->pdev;
2641 unsigned long size;
2642 unsigned int i;
2643
2644 if (!rx_ring->buffer_info)
2645 return;
2646 /* Free all the Rx ring sk_buffs */
2647 for (i = 0; i < rx_ring->count; i++) {
2648 buffer_info = &rx_ring->buffer_info[i];
2649 if (buffer_info->dma) {
2650 if (adapter->rx_ps_hdr_size)
2651 pci_unmap_single(pdev, buffer_info->dma,
2652 adapter->rx_ps_hdr_size,
2653 PCI_DMA_FROMDEVICE);
2654 else
2655 pci_unmap_single(pdev, buffer_info->dma,
2656 adapter->rx_buffer_len,
2657 PCI_DMA_FROMDEVICE);
2658 buffer_info->dma = 0;
2659 }
2660
2661 if (buffer_info->skb) {
2662 dev_kfree_skb(buffer_info->skb);
2663 buffer_info->skb = NULL;
2664 }
2665 if (buffer_info->page) {
Alexander Duyckbf36c1a2008-07-08 15:11:40 -07002666 if (buffer_info->page_dma)
2667 pci_unmap_page(pdev, buffer_info->page_dma,
2668 PAGE_SIZE / 2,
2669 PCI_DMA_FROMDEVICE);
Auke Kok9d5c8242008-01-24 02:22:38 -08002670 put_page(buffer_info->page);
2671 buffer_info->page = NULL;
2672 buffer_info->page_dma = 0;
Alexander Duyckbf36c1a2008-07-08 15:11:40 -07002673 buffer_info->page_offset = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08002674 }
2675 }
2676
Auke Kok9d5c8242008-01-24 02:22:38 -08002677 size = sizeof(struct igb_buffer) * rx_ring->count;
2678 memset(rx_ring->buffer_info, 0, size);
2679
2680 /* Zero out the descriptor ring */
2681 memset(rx_ring->desc, 0, rx_ring->size);
2682
2683 rx_ring->next_to_clean = 0;
2684 rx_ring->next_to_use = 0;
2685
2686 writel(0, adapter->hw.hw_addr + rx_ring->head);
2687 writel(0, adapter->hw.hw_addr + rx_ring->tail);
2688}
2689
2690/**
2691 * igb_clean_all_rx_rings - Free Rx Buffers for all queues
2692 * @adapter: board private structure
2693 **/
2694static void igb_clean_all_rx_rings(struct igb_adapter *adapter)
2695{
2696 int i;
2697
2698 for (i = 0; i < adapter->num_rx_queues; i++)
Mitch Williams3b644cf2008-06-27 10:59:48 -07002699 igb_clean_rx_ring(&adapter->rx_ring[i]);
Auke Kok9d5c8242008-01-24 02:22:38 -08002700}
2701
2702/**
2703 * igb_set_mac - Change the Ethernet Address of the NIC
2704 * @netdev: network interface device structure
2705 * @p: pointer to an address structure
2706 *
2707 * Returns 0 on success, negative on failure
2708 **/
2709static int igb_set_mac(struct net_device *netdev, void *p)
2710{
2711 struct igb_adapter *adapter = netdev_priv(netdev);
Alexander Duyck28b07592009-02-06 23:20:31 +00002712 struct e1000_hw *hw = &adapter->hw;
Auke Kok9d5c8242008-01-24 02:22:38 -08002713 struct sockaddr *addr = p;
2714
2715 if (!is_valid_ether_addr(addr->sa_data))
2716 return -EADDRNOTAVAIL;
2717
2718 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
Alexander Duyck28b07592009-02-06 23:20:31 +00002719 memcpy(hw->mac.addr, addr->sa_data, netdev->addr_len);
Auke Kok9d5c8242008-01-24 02:22:38 -08002720
Alexander Duyck26ad9172009-10-05 06:32:49 +00002721 /* set the correct pool for the new PF MAC address in entry 0 */
2722 igb_rar_set_qsel(adapter, hw->mac.addr, 0,
2723 adapter->vfs_allocated_count);
Alexander Duycke1739522009-02-19 20:39:44 -08002724
Auke Kok9d5c8242008-01-24 02:22:38 -08002725 return 0;
2726}
2727
2728/**
Alexander Duyck68d480c2009-10-05 06:33:08 +00002729 * igb_write_mc_addr_list - write multicast addresses to MTA
2730 * @netdev: network interface device structure
2731 *
2732 * Writes multicast address list to the MTA hash table.
2733 * Returns: -ENOMEM on failure
2734 * 0 on no addresses written
2735 * X on writing X addresses to MTA
2736 **/
2737static int igb_write_mc_addr_list(struct net_device *netdev)
2738{
2739 struct igb_adapter *adapter = netdev_priv(netdev);
2740 struct e1000_hw *hw = &adapter->hw;
2741 struct dev_mc_list *mc_ptr = netdev->mc_list;
2742 u8 *mta_list;
2743 u32 vmolr = 0;
2744 int i;
2745
2746 if (!netdev->mc_count) {
2747 /* nothing to program, so clear mc list */
2748 igb_update_mc_addr_list(hw, NULL, 0);
2749 igb_restore_vf_multicasts(adapter);
2750 return 0;
2751 }
2752
2753 mta_list = kzalloc(netdev->mc_count * 6, GFP_ATOMIC);
2754 if (!mta_list)
2755 return -ENOMEM;
2756
2757 /* set vmolr receive overflow multicast bit */
2758 vmolr |= E1000_VMOLR_ROMPE;
2759
2760 /* The shared function expects a packed array of only addresses. */
2761 mc_ptr = netdev->mc_list;
2762
2763 for (i = 0; i < netdev->mc_count; i++) {
2764 if (!mc_ptr)
2765 break;
2766 memcpy(mta_list + (i*ETH_ALEN), mc_ptr->dmi_addr, ETH_ALEN);
2767 mc_ptr = mc_ptr->next;
2768 }
2769 igb_update_mc_addr_list(hw, mta_list, i);
2770 kfree(mta_list);
2771
2772 return netdev->mc_count;
2773}
2774
2775/**
2776 * igb_write_uc_addr_list - write unicast addresses to RAR table
2777 * @netdev: network interface device structure
2778 *
2779 * Writes unicast address list to the RAR table.
2780 * Returns: -ENOMEM on failure/insufficient address space
2781 * 0 on no addresses written
2782 * X on writing X addresses to the RAR table
2783 **/
2784static int igb_write_uc_addr_list(struct net_device *netdev)
2785{
2786 struct igb_adapter *adapter = netdev_priv(netdev);
2787 struct e1000_hw *hw = &adapter->hw;
2788 unsigned int vfn = adapter->vfs_allocated_count;
2789 unsigned int rar_entries = hw->mac.rar_entry_count - (vfn + 1);
2790 int count = 0;
2791
2792 /* return ENOMEM indicating insufficient memory for addresses */
2793 if (netdev->uc.count > rar_entries)
2794 return -ENOMEM;
2795
2796 if (netdev->uc.count && rar_entries) {
2797 struct netdev_hw_addr *ha;
2798 list_for_each_entry(ha, &netdev->uc.list, list) {
2799 if (!rar_entries)
2800 break;
2801 igb_rar_set_qsel(adapter, ha->addr,
2802 rar_entries--,
2803 vfn);
2804 count++;
2805 }
2806 }
2807 /* write the addresses in reverse order to avoid write combining */
2808 for (; rar_entries > 0 ; rar_entries--) {
2809 wr32(E1000_RAH(rar_entries), 0);
2810 wr32(E1000_RAL(rar_entries), 0);
2811 }
2812 wrfl();
2813
2814 return count;
2815}
2816
2817/**
Alexander Duyckff41f8d2009-09-03 14:48:56 +00002818 * igb_set_rx_mode - Secondary Unicast, Multicast and Promiscuous mode set
Auke Kok9d5c8242008-01-24 02:22:38 -08002819 * @netdev: network interface device structure
2820 *
Alexander Duyckff41f8d2009-09-03 14:48:56 +00002821 * The set_rx_mode entry point is called whenever the unicast or multicast
2822 * address lists or the network interface flags are updated. This routine is
2823 * responsible for configuring the hardware for proper unicast, multicast,
Auke Kok9d5c8242008-01-24 02:22:38 -08002824 * promiscuous mode, and all-multi behavior.
2825 **/
Alexander Duyckff41f8d2009-09-03 14:48:56 +00002826static void igb_set_rx_mode(struct net_device *netdev)
Auke Kok9d5c8242008-01-24 02:22:38 -08002827{
2828 struct igb_adapter *adapter = netdev_priv(netdev);
2829 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck68d480c2009-10-05 06:33:08 +00002830 unsigned int vfn = adapter->vfs_allocated_count;
2831 u32 rctl, vmolr = 0;
2832 int count;
Auke Kok9d5c8242008-01-24 02:22:38 -08002833
2834 /* Check for Promiscuous and All Multicast modes */
Auke Kok9d5c8242008-01-24 02:22:38 -08002835 rctl = rd32(E1000_RCTL);
2836
Alexander Duyck68d480c2009-10-05 06:33:08 +00002837 /* clear the effected bits */
2838 rctl &= ~(E1000_RCTL_UPE | E1000_RCTL_MPE | E1000_RCTL_VFE);
2839
Patrick McHardy746b9f02008-07-16 20:15:45 -07002840 if (netdev->flags & IFF_PROMISC) {
Auke Kok9d5c8242008-01-24 02:22:38 -08002841 rctl |= (E1000_RCTL_UPE | E1000_RCTL_MPE);
Alexander Duyck68d480c2009-10-05 06:33:08 +00002842 vmolr |= (E1000_VMOLR_ROPE | E1000_VMOLR_MPME);
Patrick McHardy746b9f02008-07-16 20:15:45 -07002843 } else {
Alexander Duyck68d480c2009-10-05 06:33:08 +00002844 if (netdev->flags & IFF_ALLMULTI) {
Patrick McHardy746b9f02008-07-16 20:15:45 -07002845 rctl |= E1000_RCTL_MPE;
Alexander Duyck68d480c2009-10-05 06:33:08 +00002846 vmolr |= E1000_VMOLR_MPME;
2847 } else {
2848 /*
2849 * Write addresses to the MTA, if the attempt fails
2850 * then we should just turn on promiscous mode so
2851 * that we can at least receive multicast traffic
2852 */
2853 count = igb_write_mc_addr_list(netdev);
2854 if (count < 0) {
2855 rctl |= E1000_RCTL_MPE;
2856 vmolr |= E1000_VMOLR_MPME;
2857 } else if (count) {
2858 vmolr |= E1000_VMOLR_ROMPE;
2859 }
2860 }
2861 /*
2862 * Write addresses to available RAR registers, if there is not
2863 * sufficient space to store all the addresses then enable
2864 * unicast promiscous mode
2865 */
2866 count = igb_write_uc_addr_list(netdev);
2867 if (count < 0) {
Alexander Duyckff41f8d2009-09-03 14:48:56 +00002868 rctl |= E1000_RCTL_UPE;
Alexander Duyck68d480c2009-10-05 06:33:08 +00002869 vmolr |= E1000_VMOLR_ROPE;
2870 }
Patrick McHardy78ed11a2008-07-16 20:16:14 -07002871 rctl |= E1000_RCTL_VFE;
Patrick McHardy746b9f02008-07-16 20:15:45 -07002872 }
Auke Kok9d5c8242008-01-24 02:22:38 -08002873 wr32(E1000_RCTL, rctl);
2874
Alexander Duyck68d480c2009-10-05 06:33:08 +00002875 /*
2876 * In order to support SR-IOV and eventually VMDq it is necessary to set
2877 * the VMOLR to enable the appropriate modes. Without this workaround
2878 * we will have issues with VLAN tag stripping not being done for frames
2879 * that are only arriving because we are the default pool
2880 */
2881 if (hw->mac.type < e1000_82576)
Alexander Duyck28fc06f2009-07-23 18:08:54 +00002882 return;
Alexander Duyck28fc06f2009-07-23 18:08:54 +00002883
Alexander Duyck68d480c2009-10-05 06:33:08 +00002884 vmolr |= rd32(E1000_VMOLR(vfn)) &
2885 ~(E1000_VMOLR_ROPE | E1000_VMOLR_MPME | E1000_VMOLR_ROMPE);
2886 wr32(E1000_VMOLR(vfn), vmolr);
Alexander Duyck28fc06f2009-07-23 18:08:54 +00002887 igb_restore_vf_multicasts(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08002888}
2889
2890/* Need to wait a few seconds after link up to get diagnostic information from
2891 * the phy */
2892static void igb_update_phy_info(unsigned long data)
2893{
2894 struct igb_adapter *adapter = (struct igb_adapter *) data;
Alexander Duyckf5f4cf02008-11-21 21:30:24 -08002895 igb_get_phy_info(&adapter->hw);
Auke Kok9d5c8242008-01-24 02:22:38 -08002896}
2897
2898/**
Alexander Duyck4d6b7252009-02-06 23:16:24 +00002899 * igb_has_link - check shared code for link and determine up/down
2900 * @adapter: pointer to driver private info
2901 **/
2902static bool igb_has_link(struct igb_adapter *adapter)
2903{
2904 struct e1000_hw *hw = &adapter->hw;
2905 bool link_active = false;
2906 s32 ret_val = 0;
2907
2908 /* get_link_status is set on LSC (link status) interrupt or
2909 * rx sequence error interrupt. get_link_status will stay
2910 * false until the e1000_check_for_link establishes link
2911 * for copper adapters ONLY
2912 */
2913 switch (hw->phy.media_type) {
2914 case e1000_media_type_copper:
2915 if (hw->mac.get_link_status) {
2916 ret_val = hw->mac.ops.check_for_link(hw);
2917 link_active = !hw->mac.get_link_status;
2918 } else {
2919 link_active = true;
2920 }
2921 break;
Alexander Duyck4d6b7252009-02-06 23:16:24 +00002922 case e1000_media_type_internal_serdes:
2923 ret_val = hw->mac.ops.check_for_link(hw);
2924 link_active = hw->mac.serdes_has_link;
2925 break;
2926 default:
2927 case e1000_media_type_unknown:
2928 break;
2929 }
2930
2931 return link_active;
2932}
2933
2934/**
Auke Kok9d5c8242008-01-24 02:22:38 -08002935 * igb_watchdog - Timer Call-back
2936 * @data: pointer to adapter cast into an unsigned long
2937 **/
2938static void igb_watchdog(unsigned long data)
2939{
2940 struct igb_adapter *adapter = (struct igb_adapter *)data;
2941 /* Do the rest outside of interrupt context */
2942 schedule_work(&adapter->watchdog_task);
2943}
2944
2945static void igb_watchdog_task(struct work_struct *work)
2946{
2947 struct igb_adapter *adapter = container_of(work,
2948 struct igb_adapter, watchdog_task);
2949 struct e1000_hw *hw = &adapter->hw;
Auke Kok9d5c8242008-01-24 02:22:38 -08002950 struct net_device *netdev = adapter->netdev;
2951 struct igb_ring *tx_ring = adapter->tx_ring;
Auke Kok9d5c8242008-01-24 02:22:38 -08002952 u32 link;
Alexander Duyck7a6ea552008-08-26 04:25:03 -07002953 int i;
Auke Kok9d5c8242008-01-24 02:22:38 -08002954
Alexander Duyck4d6b7252009-02-06 23:16:24 +00002955 link = igb_has_link(adapter);
2956 if ((netif_carrier_ok(netdev)) && link)
Auke Kok9d5c8242008-01-24 02:22:38 -08002957 goto link_up;
2958
Auke Kok9d5c8242008-01-24 02:22:38 -08002959 if (link) {
2960 if (!netif_carrier_ok(netdev)) {
2961 u32 ctrl;
2962 hw->mac.ops.get_speed_and_duplex(&adapter->hw,
2963 &adapter->link_speed,
2964 &adapter->link_duplex);
2965
2966 ctrl = rd32(E1000_CTRL);
Alexander Duyck527d47c2008-11-27 00:21:39 -08002967 /* Links status message must follow this format */
2968 printk(KERN_INFO "igb: %s NIC Link is Up %d Mbps %s, "
Auke Kok9d5c8242008-01-24 02:22:38 -08002969 "Flow Control: %s\n",
Alexander Duyck527d47c2008-11-27 00:21:39 -08002970 netdev->name,
Auke Kok9d5c8242008-01-24 02:22:38 -08002971 adapter->link_speed,
2972 adapter->link_duplex == FULL_DUPLEX ?
2973 "Full Duplex" : "Half Duplex",
2974 ((ctrl & E1000_CTRL_TFCE) && (ctrl &
2975 E1000_CTRL_RFCE)) ? "RX/TX" : ((ctrl &
2976 E1000_CTRL_RFCE) ? "RX" : ((ctrl &
2977 E1000_CTRL_TFCE) ? "TX" : "None")));
2978
2979 /* tweak tx_queue_len according to speed/duplex and
2980 * adjust the timeout factor */
2981 netdev->tx_queue_len = adapter->tx_queue_len;
2982 adapter->tx_timeout_factor = 1;
2983 switch (adapter->link_speed) {
2984 case SPEED_10:
2985 netdev->tx_queue_len = 10;
2986 adapter->tx_timeout_factor = 14;
2987 break;
2988 case SPEED_100:
2989 netdev->tx_queue_len = 100;
2990 /* maybe add some timeout factor ? */
2991 break;
2992 }
2993
2994 netif_carrier_on(netdev);
Auke Kok9d5c8242008-01-24 02:22:38 -08002995
Alexander Duyck4ae196d2009-02-19 20:40:07 -08002996 igb_ping_all_vfs(adapter);
2997
Alexander Duyck4b1a9872009-02-06 23:19:50 +00002998 /* link state has changed, schedule phy info update */
Auke Kok9d5c8242008-01-24 02:22:38 -08002999 if (!test_bit(__IGB_DOWN, &adapter->state))
3000 mod_timer(&adapter->phy_info_timer,
3001 round_jiffies(jiffies + 2 * HZ));
3002 }
3003 } else {
3004 if (netif_carrier_ok(netdev)) {
3005 adapter->link_speed = 0;
3006 adapter->link_duplex = 0;
Alexander Duyck527d47c2008-11-27 00:21:39 -08003007 /* Links status message must follow this format */
3008 printk(KERN_INFO "igb: %s NIC Link is Down\n",
3009 netdev->name);
Auke Kok9d5c8242008-01-24 02:22:38 -08003010 netif_carrier_off(netdev);
Alexander Duyck4b1a9872009-02-06 23:19:50 +00003011
Alexander Duyck4ae196d2009-02-19 20:40:07 -08003012 igb_ping_all_vfs(adapter);
3013
Alexander Duyck4b1a9872009-02-06 23:19:50 +00003014 /* link state has changed, schedule phy info update */
Auke Kok9d5c8242008-01-24 02:22:38 -08003015 if (!test_bit(__IGB_DOWN, &adapter->state))
3016 mod_timer(&adapter->phy_info_timer,
3017 round_jiffies(jiffies + 2 * HZ));
3018 }
3019 }
3020
3021link_up:
3022 igb_update_stats(adapter);
3023
Alexander Duyck4b1a9872009-02-06 23:19:50 +00003024 hw->mac.tx_packet_delta = adapter->stats.tpt - adapter->tpt_old;
Auke Kok9d5c8242008-01-24 02:22:38 -08003025 adapter->tpt_old = adapter->stats.tpt;
Alexander Duyck4b1a9872009-02-06 23:19:50 +00003026 hw->mac.collision_delta = adapter->stats.colc - adapter->colc_old;
Auke Kok9d5c8242008-01-24 02:22:38 -08003027 adapter->colc_old = adapter->stats.colc;
3028
3029 adapter->gorc = adapter->stats.gorc - adapter->gorc_old;
3030 adapter->gorc_old = adapter->stats.gorc;
3031 adapter->gotc = adapter->stats.gotc - adapter->gotc_old;
3032 adapter->gotc_old = adapter->stats.gotc;
3033
3034 igb_update_adaptive(&adapter->hw);
3035
3036 if (!netif_carrier_ok(netdev)) {
Alexander Duyckc493ea42009-03-20 00:16:50 +00003037 if (igb_desc_unused(tx_ring) + 1 < tx_ring->count) {
Auke Kok9d5c8242008-01-24 02:22:38 -08003038 /* We've lost link, so the controller stops DMA,
3039 * but we've got queued Tx work that's never going
3040 * to get done, so reset controller to flush Tx.
3041 * (Do the reset outside of interrupt context). */
3042 adapter->tx_timeout_count++;
3043 schedule_work(&adapter->reset_task);
Jesse Brandeburgc2d5ab42009-05-07 11:07:35 +00003044 /* return immediately since reset is imminent */
3045 return;
Auke Kok9d5c8242008-01-24 02:22:38 -08003046 }
3047 }
3048
3049 /* Cause software interrupt to ensure rx ring is cleaned */
Alexander Duyck7a6ea552008-08-26 04:25:03 -07003050 if (adapter->msix_entries) {
Alexander Duyck047e0032009-10-27 15:49:27 +00003051 u32 eics = 0;
3052 for (i = 0; i < adapter->num_q_vectors; i++) {
3053 struct igb_q_vector *q_vector = adapter->q_vector[i];
3054 eics |= q_vector->eims_value;
3055 }
Alexander Duyck7a6ea552008-08-26 04:25:03 -07003056 wr32(E1000_EICS, eics);
3057 } else {
3058 wr32(E1000_ICS, E1000_ICS_RXDMT0);
3059 }
Auke Kok9d5c8242008-01-24 02:22:38 -08003060
3061 /* Force detection of hung controller every watchdog period */
3062 tx_ring->detect_tx_hung = true;
3063
3064 /* Reset the timer */
3065 if (!test_bit(__IGB_DOWN, &adapter->state))
3066 mod_timer(&adapter->watchdog_timer,
3067 round_jiffies(jiffies + 2 * HZ));
3068}
3069
3070enum latency_range {
3071 lowest_latency = 0,
3072 low_latency = 1,
3073 bulk_latency = 2,
3074 latency_invalid = 255
3075};
3076
3077
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07003078/**
3079 * igb_update_ring_itr - update the dynamic ITR value based on packet size
3080 *
3081 * Stores a new ITR value based on strictly on packet size. This
3082 * algorithm is less sophisticated than that used in igb_update_itr,
3083 * due to the difficulty of synchronizing statistics across multiple
3084 * receive rings. The divisors and thresholds used by this fuction
3085 * were determined based on theoretical maximum wire speed and testing
3086 * data, in order to minimize response time while increasing bulk
3087 * throughput.
3088 * This functionality is controlled by the InterruptThrottleRate module
3089 * parameter (see igb_param.c)
3090 * NOTE: This function is called only when operating in a multiqueue
3091 * receive environment.
Alexander Duyck047e0032009-10-27 15:49:27 +00003092 * @q_vector: pointer to q_vector
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07003093 **/
Alexander Duyck047e0032009-10-27 15:49:27 +00003094static void igb_update_ring_itr(struct igb_q_vector *q_vector)
Auke Kok9d5c8242008-01-24 02:22:38 -08003095{
Alexander Duyck047e0032009-10-27 15:49:27 +00003096 int new_val = q_vector->itr_val;
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07003097 int avg_wire_size = 0;
Alexander Duyck047e0032009-10-27 15:49:27 +00003098 struct igb_adapter *adapter = q_vector->adapter;
Auke Kok9d5c8242008-01-24 02:22:38 -08003099
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07003100 /* For non-gigabit speeds, just fix the interrupt rate at 4000
3101 * ints/sec - ITR timer value of 120 ticks.
3102 */
3103 if (adapter->link_speed != SPEED_1000) {
Alexander Duyck047e0032009-10-27 15:49:27 +00003104 new_val = 976;
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07003105 goto set_itr_val;
3106 }
Alexander Duyck047e0032009-10-27 15:49:27 +00003107
3108 if (q_vector->rx_ring && q_vector->rx_ring->total_packets) {
3109 struct igb_ring *ring = q_vector->rx_ring;
3110 avg_wire_size = ring->total_bytes / ring->total_packets;
3111 }
3112
3113 if (q_vector->tx_ring && q_vector->tx_ring->total_packets) {
3114 struct igb_ring *ring = q_vector->tx_ring;
3115 avg_wire_size = max_t(u32, avg_wire_size,
3116 (ring->total_bytes /
3117 ring->total_packets));
3118 }
3119
3120 /* if avg_wire_size isn't set no work was done */
3121 if (!avg_wire_size)
3122 goto clear_counts;
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07003123
3124 /* Add 24 bytes to size to account for CRC, preamble, and gap */
3125 avg_wire_size += 24;
3126
3127 /* Don't starve jumbo frames */
3128 avg_wire_size = min(avg_wire_size, 3000);
3129
3130 /* Give a little boost to mid-size frames */
3131 if ((avg_wire_size > 300) && (avg_wire_size < 1200))
3132 new_val = avg_wire_size / 3;
3133 else
3134 new_val = avg_wire_size / 2;
3135
3136set_itr_val:
Alexander Duyck047e0032009-10-27 15:49:27 +00003137 if (new_val != q_vector->itr_val) {
3138 q_vector->itr_val = new_val;
3139 q_vector->set_itr = 1;
Auke Kok9d5c8242008-01-24 02:22:38 -08003140 }
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07003141clear_counts:
Alexander Duyck047e0032009-10-27 15:49:27 +00003142 if (q_vector->rx_ring) {
3143 q_vector->rx_ring->total_bytes = 0;
3144 q_vector->rx_ring->total_packets = 0;
3145 }
3146 if (q_vector->tx_ring) {
3147 q_vector->tx_ring->total_bytes = 0;
3148 q_vector->tx_ring->total_packets = 0;
3149 }
Auke Kok9d5c8242008-01-24 02:22:38 -08003150}
3151
3152/**
3153 * igb_update_itr - update the dynamic ITR value based on statistics
3154 * Stores a new ITR value based on packets and byte
3155 * counts during the last interrupt. The advantage of per interrupt
3156 * computation is faster updates and more accurate ITR for the current
3157 * traffic pattern. Constants in this function were computed
3158 * based on theoretical maximum wire speed and thresholds were set based
3159 * on testing data as well as attempting to minimize response time
3160 * while increasing bulk throughput.
3161 * this functionality is controlled by the InterruptThrottleRate module
3162 * parameter (see igb_param.c)
3163 * NOTE: These calculations are only valid when operating in a single-
3164 * queue environment.
3165 * @adapter: pointer to adapter
Alexander Duyck047e0032009-10-27 15:49:27 +00003166 * @itr_setting: current q_vector->itr_val
Auke Kok9d5c8242008-01-24 02:22:38 -08003167 * @packets: the number of packets during this measurement interval
3168 * @bytes: the number of bytes during this measurement interval
3169 **/
3170static unsigned int igb_update_itr(struct igb_adapter *adapter, u16 itr_setting,
3171 int packets, int bytes)
3172{
3173 unsigned int retval = itr_setting;
3174
3175 if (packets == 0)
3176 goto update_itr_done;
3177
3178 switch (itr_setting) {
3179 case lowest_latency:
3180 /* handle TSO and jumbo frames */
3181 if (bytes/packets > 8000)
3182 retval = bulk_latency;
3183 else if ((packets < 5) && (bytes > 512))
3184 retval = low_latency;
3185 break;
3186 case low_latency: /* 50 usec aka 20000 ints/s */
3187 if (bytes > 10000) {
3188 /* this if handles the TSO accounting */
3189 if (bytes/packets > 8000) {
3190 retval = bulk_latency;
3191 } else if ((packets < 10) || ((bytes/packets) > 1200)) {
3192 retval = bulk_latency;
3193 } else if ((packets > 35)) {
3194 retval = lowest_latency;
3195 }
3196 } else if (bytes/packets > 2000) {
3197 retval = bulk_latency;
3198 } else if (packets <= 2 && bytes < 512) {
3199 retval = lowest_latency;
3200 }
3201 break;
3202 case bulk_latency: /* 250 usec aka 4000 ints/s */
3203 if (bytes > 25000) {
3204 if (packets > 35)
3205 retval = low_latency;
Alexander Duyck1e5c3d22009-02-12 18:17:21 +00003206 } else if (bytes < 1500) {
Auke Kok9d5c8242008-01-24 02:22:38 -08003207 retval = low_latency;
3208 }
3209 break;
3210 }
3211
3212update_itr_done:
3213 return retval;
3214}
3215
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07003216static void igb_set_itr(struct igb_adapter *adapter)
Auke Kok9d5c8242008-01-24 02:22:38 -08003217{
Alexander Duyck047e0032009-10-27 15:49:27 +00003218 struct igb_q_vector *q_vector = adapter->q_vector[0];
Auke Kok9d5c8242008-01-24 02:22:38 -08003219 u16 current_itr;
Alexander Duyck047e0032009-10-27 15:49:27 +00003220 u32 new_itr = q_vector->itr_val;
Auke Kok9d5c8242008-01-24 02:22:38 -08003221
3222 /* for non-gigabit speeds, just fix the interrupt rate at 4000 */
3223 if (adapter->link_speed != SPEED_1000) {
3224 current_itr = 0;
3225 new_itr = 4000;
3226 goto set_itr_now;
3227 }
3228
3229 adapter->rx_itr = igb_update_itr(adapter,
3230 adapter->rx_itr,
3231 adapter->rx_ring->total_packets,
3232 adapter->rx_ring->total_bytes);
Auke Kok9d5c8242008-01-24 02:22:38 -08003233
Alexander Duyck047e0032009-10-27 15:49:27 +00003234 adapter->tx_itr = igb_update_itr(adapter,
3235 adapter->tx_itr,
3236 adapter->tx_ring->total_packets,
3237 adapter->tx_ring->total_bytes);
3238 current_itr = max(adapter->rx_itr, adapter->tx_itr);
Auke Kok9d5c8242008-01-24 02:22:38 -08003239
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07003240 /* conservative mode (itr 3) eliminates the lowest_latency setting */
Alexander Duyck73cd78f2009-02-12 18:16:59 +00003241 if (adapter->itr_setting == 3 && current_itr == lowest_latency)
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07003242 current_itr = low_latency;
3243
Auke Kok9d5c8242008-01-24 02:22:38 -08003244 switch (current_itr) {
3245 /* counts and packets in update_itr are dependent on these numbers */
3246 case lowest_latency:
Alexander Duyck78b1f6072009-04-23 11:20:29 +00003247 new_itr = 56; /* aka 70,000 ints/sec */
Auke Kok9d5c8242008-01-24 02:22:38 -08003248 break;
3249 case low_latency:
Alexander Duyck78b1f6072009-04-23 11:20:29 +00003250 new_itr = 196; /* aka 20,000 ints/sec */
Auke Kok9d5c8242008-01-24 02:22:38 -08003251 break;
3252 case bulk_latency:
Alexander Duyck78b1f6072009-04-23 11:20:29 +00003253 new_itr = 980; /* aka 4,000 ints/sec */
Auke Kok9d5c8242008-01-24 02:22:38 -08003254 break;
3255 default:
3256 break;
3257 }
3258
3259set_itr_now:
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07003260 adapter->rx_ring->total_bytes = 0;
3261 adapter->rx_ring->total_packets = 0;
Alexander Duyck047e0032009-10-27 15:49:27 +00003262 adapter->tx_ring->total_bytes = 0;
3263 adapter->tx_ring->total_packets = 0;
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07003264
Alexander Duyck047e0032009-10-27 15:49:27 +00003265 if (new_itr != q_vector->itr_val) {
Auke Kok9d5c8242008-01-24 02:22:38 -08003266 /* this attempts to bias the interrupt rate towards Bulk
3267 * by adding intermediate steps when interrupt rate is
3268 * increasing */
Alexander Duyck047e0032009-10-27 15:49:27 +00003269 new_itr = new_itr > q_vector->itr_val ?
3270 max((new_itr * q_vector->itr_val) /
3271 (new_itr + (q_vector->itr_val >> 2)),
3272 new_itr) :
Auke Kok9d5c8242008-01-24 02:22:38 -08003273 new_itr;
3274 /* Don't write the value here; it resets the adapter's
3275 * internal timer, and causes us to delay far longer than
3276 * we should between interrupts. Instead, we write the ITR
3277 * value at the beginning of the next interrupt so the timing
3278 * ends up being correct.
3279 */
Alexander Duyck047e0032009-10-27 15:49:27 +00003280 q_vector->itr_val = new_itr;
3281 q_vector->set_itr = 1;
Auke Kok9d5c8242008-01-24 02:22:38 -08003282 }
3283
3284 return;
3285}
3286
Auke Kok9d5c8242008-01-24 02:22:38 -08003287#define IGB_TX_FLAGS_CSUM 0x00000001
3288#define IGB_TX_FLAGS_VLAN 0x00000002
3289#define IGB_TX_FLAGS_TSO 0x00000004
3290#define IGB_TX_FLAGS_IPV4 0x00000008
Patrick Ohly33af6bc2009-02-12 05:03:43 +00003291#define IGB_TX_FLAGS_TSTAMP 0x00000010
Auke Kok9d5c8242008-01-24 02:22:38 -08003292#define IGB_TX_FLAGS_VLAN_MASK 0xffff0000
3293#define IGB_TX_FLAGS_VLAN_SHIFT 16
3294
3295static inline int igb_tso_adv(struct igb_adapter *adapter,
3296 struct igb_ring *tx_ring,
3297 struct sk_buff *skb, u32 tx_flags, u8 *hdr_len)
3298{
3299 struct e1000_adv_tx_context_desc *context_desc;
3300 unsigned int i;
3301 int err;
3302 struct igb_buffer *buffer_info;
3303 u32 info = 0, tu_cmd = 0;
3304 u32 mss_l4len_idx, l4len;
3305 *hdr_len = 0;
3306
3307 if (skb_header_cloned(skb)) {
3308 err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
3309 if (err)
3310 return err;
3311 }
3312
3313 l4len = tcp_hdrlen(skb);
3314 *hdr_len += l4len;
3315
3316 if (skb->protocol == htons(ETH_P_IP)) {
3317 struct iphdr *iph = ip_hdr(skb);
3318 iph->tot_len = 0;
3319 iph->check = 0;
3320 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
3321 iph->daddr, 0,
3322 IPPROTO_TCP,
3323 0);
3324 } else if (skb_shinfo(skb)->gso_type == SKB_GSO_TCPV6) {
3325 ipv6_hdr(skb)->payload_len = 0;
3326 tcp_hdr(skb)->check = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
3327 &ipv6_hdr(skb)->daddr,
3328 0, IPPROTO_TCP, 0);
3329 }
3330
3331 i = tx_ring->next_to_use;
3332
3333 buffer_info = &tx_ring->buffer_info[i];
3334 context_desc = E1000_TX_CTXTDESC_ADV(*tx_ring, i);
3335 /* VLAN MACLEN IPLEN */
3336 if (tx_flags & IGB_TX_FLAGS_VLAN)
3337 info |= (tx_flags & IGB_TX_FLAGS_VLAN_MASK);
3338 info |= (skb_network_offset(skb) << E1000_ADVTXD_MACLEN_SHIFT);
3339 *hdr_len += skb_network_offset(skb);
3340 info |= skb_network_header_len(skb);
3341 *hdr_len += skb_network_header_len(skb);
3342 context_desc->vlan_macip_lens = cpu_to_le32(info);
3343
3344 /* ADV DTYP TUCMD MKRLOC/ISCSIHEDLEN */
3345 tu_cmd |= (E1000_TXD_CMD_DEXT | E1000_ADVTXD_DTYP_CTXT);
3346
3347 if (skb->protocol == htons(ETH_P_IP))
3348 tu_cmd |= E1000_ADVTXD_TUCMD_IPV4;
3349 tu_cmd |= E1000_ADVTXD_TUCMD_L4T_TCP;
3350
3351 context_desc->type_tucmd_mlhl = cpu_to_le32(tu_cmd);
3352
3353 /* MSS L4LEN IDX */
3354 mss_l4len_idx = (skb_shinfo(skb)->gso_size << E1000_ADVTXD_MSS_SHIFT);
3355 mss_l4len_idx |= (l4len << E1000_ADVTXD_L4LEN_SHIFT);
3356
Alexander Duyck73cd78f2009-02-12 18:16:59 +00003357 /* For 82575, context index must be unique per ring. */
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07003358 if (adapter->flags & IGB_FLAG_NEED_CTX_IDX)
3359 mss_l4len_idx |= tx_ring->queue_index << 4;
Auke Kok9d5c8242008-01-24 02:22:38 -08003360
3361 context_desc->mss_l4len_idx = cpu_to_le32(mss_l4len_idx);
3362 context_desc->seqnum_seed = 0;
3363
3364 buffer_info->time_stamp = jiffies;
Alexander Duyck0e014cb2008-12-26 01:33:18 -08003365 buffer_info->next_to_watch = i;
Auke Kok9d5c8242008-01-24 02:22:38 -08003366 buffer_info->dma = 0;
3367 i++;
3368 if (i == tx_ring->count)
3369 i = 0;
3370
3371 tx_ring->next_to_use = i;
3372
3373 return true;
3374}
3375
3376static inline bool igb_tx_csum_adv(struct igb_adapter *adapter,
3377 struct igb_ring *tx_ring,
3378 struct sk_buff *skb, u32 tx_flags)
3379{
3380 struct e1000_adv_tx_context_desc *context_desc;
3381 unsigned int i;
3382 struct igb_buffer *buffer_info;
3383 u32 info = 0, tu_cmd = 0;
3384
3385 if ((skb->ip_summed == CHECKSUM_PARTIAL) ||
3386 (tx_flags & IGB_TX_FLAGS_VLAN)) {
3387 i = tx_ring->next_to_use;
3388 buffer_info = &tx_ring->buffer_info[i];
3389 context_desc = E1000_TX_CTXTDESC_ADV(*tx_ring, i);
3390
3391 if (tx_flags & IGB_TX_FLAGS_VLAN)
3392 info |= (tx_flags & IGB_TX_FLAGS_VLAN_MASK);
3393 info |= (skb_network_offset(skb) << E1000_ADVTXD_MACLEN_SHIFT);
3394 if (skb->ip_summed == CHECKSUM_PARTIAL)
3395 info |= skb_network_header_len(skb);
3396
3397 context_desc->vlan_macip_lens = cpu_to_le32(info);
3398
3399 tu_cmd |= (E1000_TXD_CMD_DEXT | E1000_ADVTXD_DTYP_CTXT);
3400
3401 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Arthur Jonesfa4a7ef2009-03-21 16:55:07 -07003402 __be16 protocol;
3403
3404 if (skb->protocol == cpu_to_be16(ETH_P_8021Q)) {
3405 const struct vlan_ethhdr *vhdr =
3406 (const struct vlan_ethhdr*)skb->data;
3407
3408 protocol = vhdr->h_vlan_encapsulated_proto;
3409 } else {
3410 protocol = skb->protocol;
3411 }
3412
3413 switch (protocol) {
Harvey Harrison09640e62009-02-01 00:45:17 -08003414 case cpu_to_be16(ETH_P_IP):
Auke Kok9d5c8242008-01-24 02:22:38 -08003415 tu_cmd |= E1000_ADVTXD_TUCMD_IPV4;
Mitch Williams44b0cda2008-03-07 10:32:13 -08003416 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
3417 tu_cmd |= E1000_ADVTXD_TUCMD_L4T_TCP;
Jesse Brandeburgb9473562009-04-27 22:36:13 +00003418 else if (ip_hdr(skb)->protocol == IPPROTO_SCTP)
3419 tu_cmd |= E1000_ADVTXD_TUCMD_L4T_SCTP;
Mitch Williams44b0cda2008-03-07 10:32:13 -08003420 break;
Harvey Harrison09640e62009-02-01 00:45:17 -08003421 case cpu_to_be16(ETH_P_IPV6):
Mitch Williams44b0cda2008-03-07 10:32:13 -08003422 /* XXX what about other V6 headers?? */
3423 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
3424 tu_cmd |= E1000_ADVTXD_TUCMD_L4T_TCP;
Jesse Brandeburgb9473562009-04-27 22:36:13 +00003425 else if (ipv6_hdr(skb)->nexthdr == IPPROTO_SCTP)
3426 tu_cmd |= E1000_ADVTXD_TUCMD_L4T_SCTP;
Mitch Williams44b0cda2008-03-07 10:32:13 -08003427 break;
3428 default:
3429 if (unlikely(net_ratelimit()))
3430 dev_warn(&adapter->pdev->dev,
3431 "partial checksum but proto=%x!\n",
3432 skb->protocol);
3433 break;
3434 }
Auke Kok9d5c8242008-01-24 02:22:38 -08003435 }
3436
3437 context_desc->type_tucmd_mlhl = cpu_to_le32(tu_cmd);
3438 context_desc->seqnum_seed = 0;
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07003439 if (adapter->flags & IGB_FLAG_NEED_CTX_IDX)
3440 context_desc->mss_l4len_idx =
3441 cpu_to_le32(tx_ring->queue_index << 4);
Alexander Duyck265de402009-02-06 23:22:52 +00003442 else
3443 context_desc->mss_l4len_idx = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08003444
3445 buffer_info->time_stamp = jiffies;
Alexander Duyck0e014cb2008-12-26 01:33:18 -08003446 buffer_info->next_to_watch = i;
Auke Kok9d5c8242008-01-24 02:22:38 -08003447 buffer_info->dma = 0;
3448
3449 i++;
3450 if (i == tx_ring->count)
3451 i = 0;
3452 tx_ring->next_to_use = i;
3453
3454 return true;
3455 }
Auke Kok9d5c8242008-01-24 02:22:38 -08003456 return false;
3457}
3458
3459#define IGB_MAX_TXD_PWR 16
3460#define IGB_MAX_DATA_PER_TXD (1<<IGB_MAX_TXD_PWR)
3461
3462static inline int igb_tx_map_adv(struct igb_adapter *adapter,
Alexander Duyck0e014cb2008-12-26 01:33:18 -08003463 struct igb_ring *tx_ring, struct sk_buff *skb,
3464 unsigned int first)
Auke Kok9d5c8242008-01-24 02:22:38 -08003465{
3466 struct igb_buffer *buffer_info;
3467 unsigned int len = skb_headlen(skb);
3468 unsigned int count = 0, i;
3469 unsigned int f;
Alexander Duyck65689fe2009-03-20 00:17:43 +00003470 dma_addr_t *map;
Auke Kok9d5c8242008-01-24 02:22:38 -08003471
3472 i = tx_ring->next_to_use;
3473
Alexander Duyck65689fe2009-03-20 00:17:43 +00003474 if (skb_dma_map(&adapter->pdev->dev, skb, DMA_TO_DEVICE)) {
3475 dev_err(&adapter->pdev->dev, "TX DMA map failed\n");
3476 return 0;
3477 }
3478
3479 map = skb_shinfo(skb)->dma_maps;
3480
Auke Kok9d5c8242008-01-24 02:22:38 -08003481 buffer_info = &tx_ring->buffer_info[i];
3482 BUG_ON(len >= IGB_MAX_DATA_PER_TXD);
3483 buffer_info->length = len;
3484 /* set time_stamp *before* dma to help avoid a possible race */
3485 buffer_info->time_stamp = jiffies;
Alexander Duyck0e014cb2008-12-26 01:33:18 -08003486 buffer_info->next_to_watch = i;
Eric Dumazet042a53a2009-06-05 04:04:16 +00003487 buffer_info->dma = skb_shinfo(skb)->dma_head;
Auke Kok9d5c8242008-01-24 02:22:38 -08003488
3489 for (f = 0; f < skb_shinfo(skb)->nr_frags; f++) {
3490 struct skb_frag_struct *frag;
3491
Alexander Duyck65689fe2009-03-20 00:17:43 +00003492 i++;
3493 if (i == tx_ring->count)
3494 i = 0;
3495
Auke Kok9d5c8242008-01-24 02:22:38 -08003496 frag = &skb_shinfo(skb)->frags[f];
3497 len = frag->size;
3498
3499 buffer_info = &tx_ring->buffer_info[i];
3500 BUG_ON(len >= IGB_MAX_DATA_PER_TXD);
3501 buffer_info->length = len;
3502 buffer_info->time_stamp = jiffies;
Alexander Duyck0e014cb2008-12-26 01:33:18 -08003503 buffer_info->next_to_watch = i;
Alexander Duyck65689fe2009-03-20 00:17:43 +00003504 buffer_info->dma = map[count];
Auke Kok9d5c8242008-01-24 02:22:38 -08003505 count++;
Auke Kok9d5c8242008-01-24 02:22:38 -08003506 }
3507
Auke Kok9d5c8242008-01-24 02:22:38 -08003508 tx_ring->buffer_info[i].skb = skb;
Alexander Duyck0e014cb2008-12-26 01:33:18 -08003509 tx_ring->buffer_info[first].next_to_watch = i;
Auke Kok9d5c8242008-01-24 02:22:38 -08003510
Eric Dumazet042a53a2009-06-05 04:04:16 +00003511 return count + 1;
Auke Kok9d5c8242008-01-24 02:22:38 -08003512}
3513
3514static inline void igb_tx_queue_adv(struct igb_adapter *adapter,
3515 struct igb_ring *tx_ring,
3516 int tx_flags, int count, u32 paylen,
3517 u8 hdr_len)
3518{
3519 union e1000_adv_tx_desc *tx_desc = NULL;
3520 struct igb_buffer *buffer_info;
3521 u32 olinfo_status = 0, cmd_type_len;
3522 unsigned int i;
3523
3524 cmd_type_len = (E1000_ADVTXD_DTYP_DATA | E1000_ADVTXD_DCMD_IFCS |
3525 E1000_ADVTXD_DCMD_DEXT);
3526
3527 if (tx_flags & IGB_TX_FLAGS_VLAN)
3528 cmd_type_len |= E1000_ADVTXD_DCMD_VLE;
3529
Patrick Ohly33af6bc2009-02-12 05:03:43 +00003530 if (tx_flags & IGB_TX_FLAGS_TSTAMP)
3531 cmd_type_len |= E1000_ADVTXD_MAC_TSTAMP;
3532
Auke Kok9d5c8242008-01-24 02:22:38 -08003533 if (tx_flags & IGB_TX_FLAGS_TSO) {
3534 cmd_type_len |= E1000_ADVTXD_DCMD_TSE;
3535
3536 /* insert tcp checksum */
3537 olinfo_status |= E1000_TXD_POPTS_TXSM << 8;
3538
3539 /* insert ip checksum */
3540 if (tx_flags & IGB_TX_FLAGS_IPV4)
3541 olinfo_status |= E1000_TXD_POPTS_IXSM << 8;
3542
3543 } else if (tx_flags & IGB_TX_FLAGS_CSUM) {
3544 olinfo_status |= E1000_TXD_POPTS_TXSM << 8;
3545 }
3546
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07003547 if ((adapter->flags & IGB_FLAG_NEED_CTX_IDX) &&
3548 (tx_flags & (IGB_TX_FLAGS_CSUM | IGB_TX_FLAGS_TSO |
3549 IGB_TX_FLAGS_VLAN)))
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07003550 olinfo_status |= tx_ring->queue_index << 4;
Auke Kok9d5c8242008-01-24 02:22:38 -08003551
3552 olinfo_status |= ((paylen - hdr_len) << E1000_ADVTXD_PAYLEN_SHIFT);
3553
3554 i = tx_ring->next_to_use;
3555 while (count--) {
3556 buffer_info = &tx_ring->buffer_info[i];
3557 tx_desc = E1000_TX_DESC_ADV(*tx_ring, i);
3558 tx_desc->read.buffer_addr = cpu_to_le64(buffer_info->dma);
3559 tx_desc->read.cmd_type_len =
3560 cpu_to_le32(cmd_type_len | buffer_info->length);
3561 tx_desc->read.olinfo_status = cpu_to_le32(olinfo_status);
3562 i++;
3563 if (i == tx_ring->count)
3564 i = 0;
3565 }
3566
3567 tx_desc->read.cmd_type_len |= cpu_to_le32(adapter->txd_cmd);
3568 /* Force memory writes to complete before letting h/w
3569 * know there are new descriptors to fetch. (Only
3570 * applicable for weak-ordered memory model archs,
3571 * such as IA-64). */
3572 wmb();
3573
3574 tx_ring->next_to_use = i;
3575 writel(i, adapter->hw.hw_addr + tx_ring->tail);
3576 /* we need this if more than one processor can write to our tail
3577 * at a time, it syncronizes IO on IA64/Altix systems */
3578 mmiowb();
3579}
3580
3581static int __igb_maybe_stop_tx(struct net_device *netdev,
3582 struct igb_ring *tx_ring, int size)
3583{
3584 struct igb_adapter *adapter = netdev_priv(netdev);
3585
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07003586 netif_stop_subqueue(netdev, tx_ring->queue_index);
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07003587
Auke Kok9d5c8242008-01-24 02:22:38 -08003588 /* Herbert's original patch had:
3589 * smp_mb__after_netif_stop_queue();
3590 * but since that doesn't exist yet, just open code it. */
3591 smp_mb();
3592
3593 /* We need to check again in a case another CPU has just
3594 * made room available. */
Alexander Duyckc493ea42009-03-20 00:16:50 +00003595 if (igb_desc_unused(tx_ring) < size)
Auke Kok9d5c8242008-01-24 02:22:38 -08003596 return -EBUSY;
3597
3598 /* A reprieve! */
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07003599 netif_wake_subqueue(netdev, tx_ring->queue_index);
Auke Kok9d5c8242008-01-24 02:22:38 -08003600 ++adapter->restart_queue;
3601 return 0;
3602}
3603
3604static int igb_maybe_stop_tx(struct net_device *netdev,
3605 struct igb_ring *tx_ring, int size)
3606{
Alexander Duyckc493ea42009-03-20 00:16:50 +00003607 if (igb_desc_unused(tx_ring) >= size)
Auke Kok9d5c8242008-01-24 02:22:38 -08003608 return 0;
3609 return __igb_maybe_stop_tx(netdev, tx_ring, size);
3610}
3611
Stephen Hemminger3b29a562009-08-31 19:50:55 +00003612static netdev_tx_t igb_xmit_frame_ring_adv(struct sk_buff *skb,
3613 struct net_device *netdev,
3614 struct igb_ring *tx_ring)
Auke Kok9d5c8242008-01-24 02:22:38 -08003615{
3616 struct igb_adapter *adapter = netdev_priv(netdev);
Alexander Duyck0e014cb2008-12-26 01:33:18 -08003617 unsigned int first;
Auke Kok9d5c8242008-01-24 02:22:38 -08003618 unsigned int tx_flags = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08003619 u8 hdr_len = 0;
Alexander Duyck65689fe2009-03-20 00:17:43 +00003620 int count = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08003621 int tso = 0;
Patrick Ohly33af6bc2009-02-12 05:03:43 +00003622 union skb_shared_tx *shtx;
Auke Kok9d5c8242008-01-24 02:22:38 -08003623
Auke Kok9d5c8242008-01-24 02:22:38 -08003624 if (test_bit(__IGB_DOWN, &adapter->state)) {
3625 dev_kfree_skb_any(skb);
3626 return NETDEV_TX_OK;
3627 }
3628
3629 if (skb->len <= 0) {
3630 dev_kfree_skb_any(skb);
3631 return NETDEV_TX_OK;
3632 }
3633
Auke Kok9d5c8242008-01-24 02:22:38 -08003634 /* need: 1 descriptor per page,
3635 * + 2 desc gap to keep tail from touching head,
3636 * + 1 desc for skb->data,
3637 * + 1 desc for context descriptor,
3638 * otherwise try next time */
3639 if (igb_maybe_stop_tx(netdev, tx_ring, skb_shinfo(skb)->nr_frags + 4)) {
3640 /* this is a hard error */
Auke Kok9d5c8242008-01-24 02:22:38 -08003641 return NETDEV_TX_BUSY;
3642 }
Patrick Ohly33af6bc2009-02-12 05:03:43 +00003643
3644 /*
3645 * TODO: check that there currently is no other packet with
3646 * time stamping in the queue
3647 *
3648 * When doing time stamping, keep the connection to the socket
3649 * a while longer: it is still needed by skb_hwtstamp_tx(),
3650 * called either in igb_tx_hwtstamp() or by our caller when
3651 * doing software time stamping.
3652 */
3653 shtx = skb_tx(skb);
3654 if (unlikely(shtx->hardware)) {
3655 shtx->in_progress = 1;
3656 tx_flags |= IGB_TX_FLAGS_TSTAMP;
Patrick Ohly33af6bc2009-02-12 05:03:43 +00003657 }
Auke Kok9d5c8242008-01-24 02:22:38 -08003658
3659 if (adapter->vlgrp && vlan_tx_tag_present(skb)) {
3660 tx_flags |= IGB_TX_FLAGS_VLAN;
3661 tx_flags |= (vlan_tx_tag_get(skb) << IGB_TX_FLAGS_VLAN_SHIFT);
3662 }
3663
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07003664 if (skb->protocol == htons(ETH_P_IP))
3665 tx_flags |= IGB_TX_FLAGS_IPV4;
3666
Alexander Duyck0e014cb2008-12-26 01:33:18 -08003667 first = tx_ring->next_to_use;
Auke Kok9d5c8242008-01-24 02:22:38 -08003668 tso = skb_is_gso(skb) ? igb_tso_adv(adapter, tx_ring, skb, tx_flags,
3669 &hdr_len) : 0;
3670
3671 if (tso < 0) {
3672 dev_kfree_skb_any(skb);
Auke Kok9d5c8242008-01-24 02:22:38 -08003673 return NETDEV_TX_OK;
3674 }
3675
3676 if (tso)
3677 tx_flags |= IGB_TX_FLAGS_TSO;
Alexander Duyckbc1cbd32009-02-13 14:45:17 +00003678 else if (igb_tx_csum_adv(adapter, tx_ring, skb, tx_flags) &&
3679 (skb->ip_summed == CHECKSUM_PARTIAL))
3680 tx_flags |= IGB_TX_FLAGS_CSUM;
Auke Kok9d5c8242008-01-24 02:22:38 -08003681
Alexander Duyck65689fe2009-03-20 00:17:43 +00003682 /*
3683 * count reflects descriptors mapped, if 0 then mapping error
3684 * has occured and we need to rewind the descriptor queue
3685 */
3686 count = igb_tx_map_adv(adapter, tx_ring, skb, first);
Auke Kok9d5c8242008-01-24 02:22:38 -08003687
Alexander Duyck65689fe2009-03-20 00:17:43 +00003688 if (count) {
3689 igb_tx_queue_adv(adapter, tx_ring, tx_flags, count,
3690 skb->len, hdr_len);
Alexander Duyck65689fe2009-03-20 00:17:43 +00003691 /* Make sure there is space in the ring for the next send. */
3692 igb_maybe_stop_tx(netdev, tx_ring, MAX_SKB_FRAGS + 4);
3693 } else {
3694 dev_kfree_skb_any(skb);
3695 tx_ring->buffer_info[first].time_stamp = 0;
3696 tx_ring->next_to_use = first;
3697 }
Auke Kok9d5c8242008-01-24 02:22:38 -08003698
Auke Kok9d5c8242008-01-24 02:22:38 -08003699 return NETDEV_TX_OK;
3700}
3701
Stephen Hemminger3b29a562009-08-31 19:50:55 +00003702static netdev_tx_t igb_xmit_frame_adv(struct sk_buff *skb,
3703 struct net_device *netdev)
Auke Kok9d5c8242008-01-24 02:22:38 -08003704{
3705 struct igb_adapter *adapter = netdev_priv(netdev);
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07003706 struct igb_ring *tx_ring;
3707
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07003708 int r_idx = 0;
Alexander Duyck1bfaf072009-02-19 20:39:23 -08003709 r_idx = skb->queue_mapping & (IGB_ABS_MAX_TX_QUEUES - 1);
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07003710 tx_ring = adapter->multi_tx_table[r_idx];
Auke Kok9d5c8242008-01-24 02:22:38 -08003711
3712 /* This goes back to the question of how to logically map a tx queue
3713 * to a flow. Right now, performance is impacted slightly negatively
3714 * if using multiple tx queues. If the stack breaks away from a
3715 * single qdisc implementation, we can look at this again. */
Stephen Hemminger3b29a562009-08-31 19:50:55 +00003716 return igb_xmit_frame_ring_adv(skb, netdev, tx_ring);
Auke Kok9d5c8242008-01-24 02:22:38 -08003717}
3718
3719/**
3720 * igb_tx_timeout - Respond to a Tx Hang
3721 * @netdev: network interface device structure
3722 **/
3723static void igb_tx_timeout(struct net_device *netdev)
3724{
3725 struct igb_adapter *adapter = netdev_priv(netdev);
3726 struct e1000_hw *hw = &adapter->hw;
3727
3728 /* Do the reset outside of interrupt context */
3729 adapter->tx_timeout_count++;
3730 schedule_work(&adapter->reset_task);
Alexander Duyck265de402009-02-06 23:22:52 +00003731 wr32(E1000_EICS,
3732 (adapter->eims_enable_mask & ~adapter->eims_other));
Auke Kok9d5c8242008-01-24 02:22:38 -08003733}
3734
3735static void igb_reset_task(struct work_struct *work)
3736{
3737 struct igb_adapter *adapter;
3738 adapter = container_of(work, struct igb_adapter, reset_task);
3739
3740 igb_reinit_locked(adapter);
3741}
3742
3743/**
3744 * igb_get_stats - Get System Network Statistics
3745 * @netdev: network interface device structure
3746 *
3747 * Returns the address of the device statistics structure.
3748 * The statistics are actually updated from the timer callback.
3749 **/
Alexander Duyck73cd78f2009-02-12 18:16:59 +00003750static struct net_device_stats *igb_get_stats(struct net_device *netdev)
Auke Kok9d5c8242008-01-24 02:22:38 -08003751{
Auke Kok9d5c8242008-01-24 02:22:38 -08003752 /* only return the current stats */
Ajit Khaparde8d24e932009-10-07 02:42:56 +00003753 return &netdev->stats;
Auke Kok9d5c8242008-01-24 02:22:38 -08003754}
3755
3756/**
3757 * igb_change_mtu - Change the Maximum Transfer Unit
3758 * @netdev: network interface device structure
3759 * @new_mtu: new value for maximum frame size
3760 *
3761 * Returns 0 on success, negative on failure
3762 **/
3763static int igb_change_mtu(struct net_device *netdev, int new_mtu)
3764{
3765 struct igb_adapter *adapter = netdev_priv(netdev);
3766 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
3767
3768 if ((max_frame < ETH_ZLEN + ETH_FCS_LEN) ||
3769 (max_frame > MAX_JUMBO_FRAME_SIZE)) {
3770 dev_err(&adapter->pdev->dev, "Invalid MTU setting\n");
3771 return -EINVAL;
3772 }
3773
Auke Kok9d5c8242008-01-24 02:22:38 -08003774 if (max_frame > MAX_STD_JUMBO_FRAME_SIZE) {
3775 dev_err(&adapter->pdev->dev, "MTU > 9216 not supported.\n");
3776 return -EINVAL;
3777 }
3778
3779 while (test_and_set_bit(__IGB_RESETTING, &adapter->state))
3780 msleep(1);
Alexander Duyck73cd78f2009-02-12 18:16:59 +00003781
Auke Kok9d5c8242008-01-24 02:22:38 -08003782 /* igb_down has a dependency on max_frame_size */
3783 adapter->max_frame_size = max_frame;
3784 if (netif_running(netdev))
3785 igb_down(adapter);
3786
3787 /* NOTE: netdev_alloc_skb reserves 16 bytes, and typically NET_IP_ALIGN
3788 * means we reserve 2 more, this pushes us to allocate from the next
3789 * larger slab size.
3790 * i.e. RXBUFFER_2048 --> size-4096 slab
3791 */
3792
Alexander Duyck7d95b712009-10-27 15:50:08 +00003793 if (max_frame <= IGB_RXBUFFER_1024)
Auke Kok9d5c8242008-01-24 02:22:38 -08003794 adapter->rx_buffer_len = IGB_RXBUFFER_1024;
3795 else if (max_frame <= IGB_RXBUFFER_2048)
3796 adapter->rx_buffer_len = IGB_RXBUFFER_2048;
3797 else
Alexander Duyckbf36c1a2008-07-08 15:11:40 -07003798#if (PAGE_SIZE / 2) > IGB_RXBUFFER_16384
3799 adapter->rx_buffer_len = IGB_RXBUFFER_16384;
3800#else
3801 adapter->rx_buffer_len = PAGE_SIZE / 2;
3802#endif
Alexander Duycke1739522009-02-19 20:39:44 -08003803
Auke Kok9d5c8242008-01-24 02:22:38 -08003804 /* adjust allocation if LPE protects us, and we aren't using SBP */
3805 if ((max_frame == ETH_FRAME_LEN + ETH_FCS_LEN) ||
3806 (max_frame == MAXIMUM_ETHERNET_VLAN_SIZE))
3807 adapter->rx_buffer_len = MAXIMUM_ETHERNET_VLAN_SIZE;
3808
3809 dev_info(&adapter->pdev->dev, "changing MTU from %d to %d\n",
3810 netdev->mtu, new_mtu);
3811 netdev->mtu = new_mtu;
3812
3813 if (netif_running(netdev))
3814 igb_up(adapter);
3815 else
3816 igb_reset(adapter);
3817
3818 clear_bit(__IGB_RESETTING, &adapter->state);
3819
3820 return 0;
3821}
3822
3823/**
3824 * igb_update_stats - Update the board statistics counters
3825 * @adapter: board private structure
3826 **/
3827
3828void igb_update_stats(struct igb_adapter *adapter)
3829{
Ajit Khaparde8d24e932009-10-07 02:42:56 +00003830 struct net_device *netdev = adapter->netdev;
Auke Kok9d5c8242008-01-24 02:22:38 -08003831 struct e1000_hw *hw = &adapter->hw;
3832 struct pci_dev *pdev = adapter->pdev;
3833 u16 phy_tmp;
3834
3835#define PHY_IDLE_ERROR_COUNT_MASK 0x00FF
3836
3837 /*
3838 * Prevent stats update while adapter is being reset, or if the pci
3839 * connection is down.
3840 */
3841 if (adapter->link_speed == 0)
3842 return;
3843 if (pci_channel_offline(pdev))
3844 return;
3845
3846 adapter->stats.crcerrs += rd32(E1000_CRCERRS);
3847 adapter->stats.gprc += rd32(E1000_GPRC);
3848 adapter->stats.gorc += rd32(E1000_GORCL);
3849 rd32(E1000_GORCH); /* clear GORCL */
3850 adapter->stats.bprc += rd32(E1000_BPRC);
3851 adapter->stats.mprc += rd32(E1000_MPRC);
3852 adapter->stats.roc += rd32(E1000_ROC);
3853
3854 adapter->stats.prc64 += rd32(E1000_PRC64);
3855 adapter->stats.prc127 += rd32(E1000_PRC127);
3856 adapter->stats.prc255 += rd32(E1000_PRC255);
3857 adapter->stats.prc511 += rd32(E1000_PRC511);
3858 adapter->stats.prc1023 += rd32(E1000_PRC1023);
3859 adapter->stats.prc1522 += rd32(E1000_PRC1522);
3860 adapter->stats.symerrs += rd32(E1000_SYMERRS);
3861 adapter->stats.sec += rd32(E1000_SEC);
3862
3863 adapter->stats.mpc += rd32(E1000_MPC);
3864 adapter->stats.scc += rd32(E1000_SCC);
3865 adapter->stats.ecol += rd32(E1000_ECOL);
3866 adapter->stats.mcc += rd32(E1000_MCC);
3867 adapter->stats.latecol += rd32(E1000_LATECOL);
3868 adapter->stats.dc += rd32(E1000_DC);
3869 adapter->stats.rlec += rd32(E1000_RLEC);
3870 adapter->stats.xonrxc += rd32(E1000_XONRXC);
3871 adapter->stats.xontxc += rd32(E1000_XONTXC);
3872 adapter->stats.xoffrxc += rd32(E1000_XOFFRXC);
3873 adapter->stats.xofftxc += rd32(E1000_XOFFTXC);
3874 adapter->stats.fcruc += rd32(E1000_FCRUC);
3875 adapter->stats.gptc += rd32(E1000_GPTC);
3876 adapter->stats.gotc += rd32(E1000_GOTCL);
3877 rd32(E1000_GOTCH); /* clear GOTCL */
3878 adapter->stats.rnbc += rd32(E1000_RNBC);
3879 adapter->stats.ruc += rd32(E1000_RUC);
3880 adapter->stats.rfc += rd32(E1000_RFC);
3881 adapter->stats.rjc += rd32(E1000_RJC);
3882 adapter->stats.tor += rd32(E1000_TORH);
3883 adapter->stats.tot += rd32(E1000_TOTH);
3884 adapter->stats.tpr += rd32(E1000_TPR);
3885
3886 adapter->stats.ptc64 += rd32(E1000_PTC64);
3887 adapter->stats.ptc127 += rd32(E1000_PTC127);
3888 adapter->stats.ptc255 += rd32(E1000_PTC255);
3889 adapter->stats.ptc511 += rd32(E1000_PTC511);
3890 adapter->stats.ptc1023 += rd32(E1000_PTC1023);
3891 adapter->stats.ptc1522 += rd32(E1000_PTC1522);
3892
3893 adapter->stats.mptc += rd32(E1000_MPTC);
3894 adapter->stats.bptc += rd32(E1000_BPTC);
3895
3896 /* used for adaptive IFS */
3897
3898 hw->mac.tx_packet_delta = rd32(E1000_TPT);
3899 adapter->stats.tpt += hw->mac.tx_packet_delta;
3900 hw->mac.collision_delta = rd32(E1000_COLC);
3901 adapter->stats.colc += hw->mac.collision_delta;
3902
3903 adapter->stats.algnerrc += rd32(E1000_ALGNERRC);
3904 adapter->stats.rxerrc += rd32(E1000_RXERRC);
3905 adapter->stats.tncrs += rd32(E1000_TNCRS);
3906 adapter->stats.tsctc += rd32(E1000_TSCTC);
3907 adapter->stats.tsctfc += rd32(E1000_TSCTFC);
3908
3909 adapter->stats.iac += rd32(E1000_IAC);
3910 adapter->stats.icrxoc += rd32(E1000_ICRXOC);
3911 adapter->stats.icrxptc += rd32(E1000_ICRXPTC);
3912 adapter->stats.icrxatc += rd32(E1000_ICRXATC);
3913 adapter->stats.ictxptc += rd32(E1000_ICTXPTC);
3914 adapter->stats.ictxatc += rd32(E1000_ICTXATC);
3915 adapter->stats.ictxqec += rd32(E1000_ICTXQEC);
3916 adapter->stats.ictxqmtc += rd32(E1000_ICTXQMTC);
3917 adapter->stats.icrxdmtc += rd32(E1000_ICRXDMTC);
3918
3919 /* Fill out the OS statistics structure */
Ajit Khaparde8d24e932009-10-07 02:42:56 +00003920 netdev->stats.multicast = adapter->stats.mprc;
3921 netdev->stats.collisions = adapter->stats.colc;
Auke Kok9d5c8242008-01-24 02:22:38 -08003922
3923 /* Rx Errors */
3924
Jesper Dangaard Brouer8c0ab702009-05-26 13:50:31 +00003925 if (hw->mac.type != e1000_82575) {
3926 u32 rqdpc_tmp;
Jesper Dangaard Brouer3ea73af2009-05-26 13:50:48 +00003927 u64 rqdpc_total = 0;
Jesper Dangaard Brouer8c0ab702009-05-26 13:50:31 +00003928 int i;
3929 /* Read out drops stats per RX queue. Notice RQDPC (Receive
3930 * Queue Drop Packet Count) stats only gets incremented, if
3931 * the DROP_EN but it set (in the SRRCTL register for that
3932 * queue). If DROP_EN bit is NOT set, then the some what
3933 * equivalent count is stored in RNBC (not per queue basis).
3934 * Also note the drop count is due to lack of available
3935 * descriptors.
3936 */
3937 for (i = 0; i < adapter->num_rx_queues; i++) {
3938 rqdpc_tmp = rd32(E1000_RQDPC(i)) & 0xFFF;
3939 adapter->rx_ring[i].rx_stats.drops += rqdpc_tmp;
Jesper Dangaard Brouer3ea73af2009-05-26 13:50:48 +00003940 rqdpc_total += adapter->rx_ring[i].rx_stats.drops;
Jesper Dangaard Brouer8c0ab702009-05-26 13:50:31 +00003941 }
Ajit Khaparde8d24e932009-10-07 02:42:56 +00003942 netdev->stats.rx_fifo_errors = rqdpc_total;
Jesper Dangaard Brouer8c0ab702009-05-26 13:50:31 +00003943 }
3944
Jesper Dangaard Brouer3ea73af2009-05-26 13:50:48 +00003945 /* Note RNBC (Receive No Buffers Count) is an not an exact
3946 * drop count as the hardware FIFO might save the day. Thats
3947 * one of the reason for saving it in rx_fifo_errors, as its
3948 * potentially not a true drop.
3949 */
Ajit Khaparde8d24e932009-10-07 02:42:56 +00003950 netdev->stats.rx_fifo_errors += adapter->stats.rnbc;
Jesper Dangaard Brouer3ea73af2009-05-26 13:50:48 +00003951
Auke Kok9d5c8242008-01-24 02:22:38 -08003952 /* RLEC on some newer hardware can be incorrect so build
Jesper Dangaard Brouer8c0ab702009-05-26 13:50:31 +00003953 * our own version based on RUC and ROC */
Ajit Khaparde8d24e932009-10-07 02:42:56 +00003954 netdev->stats.rx_errors = adapter->stats.rxerrc +
Auke Kok9d5c8242008-01-24 02:22:38 -08003955 adapter->stats.crcerrs + adapter->stats.algnerrc +
3956 adapter->stats.ruc + adapter->stats.roc +
3957 adapter->stats.cexterr;
Ajit Khaparde8d24e932009-10-07 02:42:56 +00003958 netdev->stats.rx_length_errors = adapter->stats.ruc +
Auke Kok9d5c8242008-01-24 02:22:38 -08003959 adapter->stats.roc;
Ajit Khaparde8d24e932009-10-07 02:42:56 +00003960 netdev->stats.rx_crc_errors = adapter->stats.crcerrs;
3961 netdev->stats.rx_frame_errors = adapter->stats.algnerrc;
3962 netdev->stats.rx_missed_errors = adapter->stats.mpc;
Auke Kok9d5c8242008-01-24 02:22:38 -08003963
3964 /* Tx Errors */
Ajit Khaparde8d24e932009-10-07 02:42:56 +00003965 netdev->stats.tx_errors = adapter->stats.ecol +
Auke Kok9d5c8242008-01-24 02:22:38 -08003966 adapter->stats.latecol;
Ajit Khaparde8d24e932009-10-07 02:42:56 +00003967 netdev->stats.tx_aborted_errors = adapter->stats.ecol;
3968 netdev->stats.tx_window_errors = adapter->stats.latecol;
3969 netdev->stats.tx_carrier_errors = adapter->stats.tncrs;
Auke Kok9d5c8242008-01-24 02:22:38 -08003970
3971 /* Tx Dropped needs to be maintained elsewhere */
3972
3973 /* Phy Stats */
3974 if (hw->phy.media_type == e1000_media_type_copper) {
3975 if ((adapter->link_speed == SPEED_1000) &&
Alexander Duyck73cd78f2009-02-12 18:16:59 +00003976 (!igb_read_phy_reg(hw, PHY_1000T_STATUS, &phy_tmp))) {
Auke Kok9d5c8242008-01-24 02:22:38 -08003977 phy_tmp &= PHY_IDLE_ERROR_COUNT_MASK;
3978 adapter->phy_stats.idle_errors += phy_tmp;
3979 }
3980 }
3981
3982 /* Management Stats */
3983 adapter->stats.mgptc += rd32(E1000_MGTPTC);
3984 adapter->stats.mgprc += rd32(E1000_MGTPRC);
3985 adapter->stats.mgpdc += rd32(E1000_MGTPDC);
3986}
3987
Auke Kok9d5c8242008-01-24 02:22:38 -08003988static irqreturn_t igb_msix_other(int irq, void *data)
3989{
Alexander Duyck047e0032009-10-27 15:49:27 +00003990 struct igb_adapter *adapter = data;
Auke Kok9d5c8242008-01-24 02:22:38 -08003991 struct e1000_hw *hw = &adapter->hw;
PJ Waskiewicz844290e2008-06-27 11:00:39 -07003992 u32 icr = rd32(E1000_ICR);
PJ Waskiewicz844290e2008-06-27 11:00:39 -07003993 /* reading ICR causes bit 31 of EICR to be cleared */
Alexander Duyckdda0e082009-02-06 23:19:08 +00003994
Alexander Duyck047e0032009-10-27 15:49:27 +00003995 if (icr & E1000_ICR_DOUTSYNC) {
Alexander Duyckdda0e082009-02-06 23:19:08 +00003996 /* HW is reporting DMA is out of sync */
3997 adapter->stats.doosync++;
3998 }
Alexander Duyckeebbbdb2009-02-06 23:19:29 +00003999
Alexander Duyck4ae196d2009-02-19 20:40:07 -08004000 /* Check for a mailbox event */
4001 if (icr & E1000_ICR_VMMB)
4002 igb_msg_task(adapter);
4003
4004 if (icr & E1000_ICR_LSC) {
4005 hw->mac.get_link_status = 1;
4006 /* guard against interrupt when we're going down */
4007 if (!test_bit(__IGB_DOWN, &adapter->state))
4008 mod_timer(&adapter->watchdog_timer, jiffies + 1);
4009 }
4010
4011 wr32(E1000_IMS, E1000_IMS_LSC | E1000_IMS_DOUTSYNC | E1000_IMS_VMMB);
PJ Waskiewicz844290e2008-06-27 11:00:39 -07004012 wr32(E1000_EIMS, adapter->eims_other);
Auke Kok9d5c8242008-01-24 02:22:38 -08004013
4014 return IRQ_HANDLED;
4015}
4016
Alexander Duyck047e0032009-10-27 15:49:27 +00004017static void igb_write_itr(struct igb_q_vector *q_vector)
Auke Kok9d5c8242008-01-24 02:22:38 -08004018{
Alexander Duyck047e0032009-10-27 15:49:27 +00004019 u32 itr_val = q_vector->itr_val & 0x7FFC;
Auke Kok9d5c8242008-01-24 02:22:38 -08004020
Alexander Duyck047e0032009-10-27 15:49:27 +00004021 if (!q_vector->set_itr)
4022 return;
Alexander Duyck73cd78f2009-02-12 18:16:59 +00004023
Alexander Duyck047e0032009-10-27 15:49:27 +00004024 if (!itr_val)
4025 itr_val = 0x4;
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07004026
Alexander Duyck047e0032009-10-27 15:49:27 +00004027 if (q_vector->itr_shift)
4028 itr_val |= itr_val << q_vector->itr_shift;
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07004029 else
Alexander Duyck047e0032009-10-27 15:49:27 +00004030 itr_val |= 0x8000000;
4031
4032 writel(itr_val, q_vector->itr_register);
4033 q_vector->set_itr = 0;
4034}
4035
4036static irqreturn_t igb_msix_ring(int irq, void *data)
4037{
4038 struct igb_q_vector *q_vector = data;
4039
4040 /* Write the ITR value calculated from the previous interrupt. */
4041 igb_write_itr(q_vector);
4042
4043 napi_schedule(&q_vector->napi);
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07004044
Auke Kok9d5c8242008-01-24 02:22:38 -08004045 return IRQ_HANDLED;
4046}
4047
Jeff Kirsher421e02f2008-10-17 11:08:31 -07004048#ifdef CONFIG_IGB_DCA
Alexander Duyck047e0032009-10-27 15:49:27 +00004049static void igb_update_dca(struct igb_q_vector *q_vector)
Jeb Cramerfe4506b2008-07-08 15:07:55 -07004050{
Alexander Duyck047e0032009-10-27 15:49:27 +00004051 struct igb_adapter *adapter = q_vector->adapter;
Jeb Cramerfe4506b2008-07-08 15:07:55 -07004052 struct e1000_hw *hw = &adapter->hw;
4053 int cpu = get_cpu();
Jeb Cramerfe4506b2008-07-08 15:07:55 -07004054
Alexander Duyck047e0032009-10-27 15:49:27 +00004055 if (q_vector->cpu == cpu)
4056 goto out_no_update;
4057
4058 if (q_vector->tx_ring) {
4059 int q = q_vector->tx_ring->reg_idx;
4060 u32 dca_txctrl = rd32(E1000_DCA_TXCTRL(q));
4061 if (hw->mac.type == e1000_82575) {
4062 dca_txctrl &= ~E1000_DCA_TXCTRL_CPUID_MASK;
4063 dca_txctrl |= dca3_get_tag(&adapter->pdev->dev, cpu);
4064 } else {
4065 dca_txctrl &= ~E1000_DCA_TXCTRL_CPUID_MASK_82576;
4066 dca_txctrl |= dca3_get_tag(&adapter->pdev->dev, cpu) <<
4067 E1000_DCA_TXCTRL_CPUID_SHIFT;
4068 }
4069 dca_txctrl |= E1000_DCA_TXCTRL_DESC_DCA_EN;
4070 wr32(E1000_DCA_TXCTRL(q), dca_txctrl);
4071 }
4072 if (q_vector->rx_ring) {
4073 int q = q_vector->rx_ring->reg_idx;
4074 u32 dca_rxctrl = rd32(E1000_DCA_RXCTRL(q));
4075 if (hw->mac.type == e1000_82575) {
4076 dca_rxctrl &= ~E1000_DCA_RXCTRL_CPUID_MASK;
4077 dca_rxctrl |= dca3_get_tag(&adapter->pdev->dev, cpu);
4078 } else {
Alexander Duyck2d064c02008-07-08 15:10:12 -07004079 dca_rxctrl &= ~E1000_DCA_RXCTRL_CPUID_MASK_82576;
Maciej Sosnowski92be7912009-03-13 20:40:21 +00004080 dca_rxctrl |= dca3_get_tag(&adapter->pdev->dev, cpu) <<
Alexander Duyck2d064c02008-07-08 15:10:12 -07004081 E1000_DCA_RXCTRL_CPUID_SHIFT;
Alexander Duyck2d064c02008-07-08 15:10:12 -07004082 }
Jeb Cramerfe4506b2008-07-08 15:07:55 -07004083 dca_rxctrl |= E1000_DCA_RXCTRL_DESC_DCA_EN;
4084 dca_rxctrl |= E1000_DCA_RXCTRL_HEAD_DCA_EN;
4085 dca_rxctrl |= E1000_DCA_RXCTRL_DATA_DCA_EN;
4086 wr32(E1000_DCA_RXCTRL(q), dca_rxctrl);
Jeb Cramerfe4506b2008-07-08 15:07:55 -07004087 }
Alexander Duyck047e0032009-10-27 15:49:27 +00004088 q_vector->cpu = cpu;
4089out_no_update:
Jeb Cramerfe4506b2008-07-08 15:07:55 -07004090 put_cpu();
4091}
4092
4093static void igb_setup_dca(struct igb_adapter *adapter)
4094{
Alexander Duyck7e0e99e2009-05-21 13:06:56 +00004095 struct e1000_hw *hw = &adapter->hw;
Jeb Cramerfe4506b2008-07-08 15:07:55 -07004096 int i;
4097
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07004098 if (!(adapter->flags & IGB_FLAG_DCA_ENABLED))
Jeb Cramerfe4506b2008-07-08 15:07:55 -07004099 return;
4100
Alexander Duyck7e0e99e2009-05-21 13:06:56 +00004101 /* Always use CB2 mode, difference is masked in the CB driver. */
4102 wr32(E1000_DCA_CTRL, E1000_DCA_CTRL_DCA_MODE_CB2);
4103
Alexander Duyck047e0032009-10-27 15:49:27 +00004104 for (i = 0; i < adapter->num_q_vectors; i++) {
4105 struct igb_q_vector *q_vector = adapter->q_vector[i];
4106 q_vector->cpu = -1;
4107 igb_update_dca(q_vector);
Jeb Cramerfe4506b2008-07-08 15:07:55 -07004108 }
4109}
4110
4111static int __igb_notify_dca(struct device *dev, void *data)
4112{
4113 struct net_device *netdev = dev_get_drvdata(dev);
4114 struct igb_adapter *adapter = netdev_priv(netdev);
4115 struct e1000_hw *hw = &adapter->hw;
4116 unsigned long event = *(unsigned long *)data;
4117
4118 switch (event) {
4119 case DCA_PROVIDER_ADD:
4120 /* if already enabled, don't do it again */
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07004121 if (adapter->flags & IGB_FLAG_DCA_ENABLED)
Jeb Cramerfe4506b2008-07-08 15:07:55 -07004122 break;
Jeb Cramerfe4506b2008-07-08 15:07:55 -07004123 /* Always use CB2 mode, difference is masked
4124 * in the CB driver. */
Alexander Duyckcbd347a2009-02-15 23:59:44 -08004125 wr32(E1000_DCA_CTRL, E1000_DCA_CTRL_DCA_MODE_CB2);
Jeb Cramerfe4506b2008-07-08 15:07:55 -07004126 if (dca_add_requester(dev) == 0) {
Alexander Duyckbbd98fe2009-01-31 00:52:30 -08004127 adapter->flags |= IGB_FLAG_DCA_ENABLED;
Jeb Cramerfe4506b2008-07-08 15:07:55 -07004128 dev_info(&adapter->pdev->dev, "DCA enabled\n");
4129 igb_setup_dca(adapter);
4130 break;
4131 }
4132 /* Fall Through since DCA is disabled. */
4133 case DCA_PROVIDER_REMOVE:
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07004134 if (adapter->flags & IGB_FLAG_DCA_ENABLED) {
Jeb Cramerfe4506b2008-07-08 15:07:55 -07004135 /* without this a class_device is left
Alexander Duyck047e0032009-10-27 15:49:27 +00004136 * hanging around in the sysfs model */
Jeb Cramerfe4506b2008-07-08 15:07:55 -07004137 dca_remove_requester(dev);
4138 dev_info(&adapter->pdev->dev, "DCA disabled\n");
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07004139 adapter->flags &= ~IGB_FLAG_DCA_ENABLED;
Alexander Duyckcbd347a2009-02-15 23:59:44 -08004140 wr32(E1000_DCA_CTRL, E1000_DCA_CTRL_DCA_MODE_DISABLE);
Jeb Cramerfe4506b2008-07-08 15:07:55 -07004141 }
4142 break;
4143 }
Alexander Duyckbbd98fe2009-01-31 00:52:30 -08004144
Jeb Cramerfe4506b2008-07-08 15:07:55 -07004145 return 0;
4146}
4147
4148static int igb_notify_dca(struct notifier_block *nb, unsigned long event,
4149 void *p)
4150{
4151 int ret_val;
4152
4153 ret_val = driver_for_each_device(&igb_driver.driver, NULL, &event,
4154 __igb_notify_dca);
4155
4156 return ret_val ? NOTIFY_BAD : NOTIFY_DONE;
4157}
Jeff Kirsher421e02f2008-10-17 11:08:31 -07004158#endif /* CONFIG_IGB_DCA */
Auke Kok9d5c8242008-01-24 02:22:38 -08004159
Alexander Duyck4ae196d2009-02-19 20:40:07 -08004160static void igb_ping_all_vfs(struct igb_adapter *adapter)
4161{
4162 struct e1000_hw *hw = &adapter->hw;
4163 u32 ping;
4164 int i;
4165
4166 for (i = 0 ; i < adapter->vfs_allocated_count; i++) {
4167 ping = E1000_PF_CONTROL_MSG;
4168 if (adapter->vf_data[i].clear_to_send)
4169 ping |= E1000_VT_MSGTYPE_CTS;
4170 igb_write_mbx(hw, &ping, 1, i);
4171 }
4172}
4173
4174static int igb_set_vf_multicasts(struct igb_adapter *adapter,
4175 u32 *msgbuf, u32 vf)
4176{
4177 int n = (msgbuf[0] & E1000_VT_MSGINFO_MASK) >> E1000_VT_MSGINFO_SHIFT;
4178 u16 *hash_list = (u16 *)&msgbuf[1];
4179 struct vf_data_storage *vf_data = &adapter->vf_data[vf];
4180 int i;
4181
4182 /* only up to 30 hash values supported */
4183 if (n > 30)
4184 n = 30;
4185
4186 /* salt away the number of multi cast addresses assigned
4187 * to this VF for later use to restore when the PF multi cast
4188 * list changes
4189 */
4190 vf_data->num_vf_mc_hashes = n;
4191
4192 /* VFs are limited to using the MTA hash table for their multicast
4193 * addresses */
4194 for (i = 0; i < n; i++)
Joe Perchesa419aef2009-08-18 11:18:35 -07004195 vf_data->vf_mc_hashes[i] = hash_list[i];
Alexander Duyck4ae196d2009-02-19 20:40:07 -08004196
4197 /* Flush and reset the mta with the new values */
Alexander Duyckff41f8d2009-09-03 14:48:56 +00004198 igb_set_rx_mode(adapter->netdev);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08004199
4200 return 0;
4201}
4202
4203static void igb_restore_vf_multicasts(struct igb_adapter *adapter)
4204{
4205 struct e1000_hw *hw = &adapter->hw;
4206 struct vf_data_storage *vf_data;
4207 int i, j;
4208
4209 for (i = 0; i < adapter->vfs_allocated_count; i++) {
4210 vf_data = &adapter->vf_data[i];
Alexander Duyck75f4f382009-03-13 20:41:55 +00004211 for (j = 0; j < vf_data->num_vf_mc_hashes; j++)
Alexander Duyck4ae196d2009-02-19 20:40:07 -08004212 igb_mta_set(hw, vf_data->vf_mc_hashes[j]);
4213 }
4214}
4215
4216static void igb_clear_vf_vfta(struct igb_adapter *adapter, u32 vf)
4217{
4218 struct e1000_hw *hw = &adapter->hw;
4219 u32 pool_mask, reg, vid;
4220 int i;
4221
4222 pool_mask = 1 << (E1000_VLVF_POOLSEL_SHIFT + vf);
4223
4224 /* Find the vlan filter for this id */
4225 for (i = 0; i < E1000_VLVF_ARRAY_SIZE; i++) {
4226 reg = rd32(E1000_VLVF(i));
4227
4228 /* remove the vf from the pool */
4229 reg &= ~pool_mask;
4230
4231 /* if pool is empty then remove entry from vfta */
4232 if (!(reg & E1000_VLVF_POOLSEL_MASK) &&
4233 (reg & E1000_VLVF_VLANID_ENABLE)) {
4234 reg = 0;
4235 vid = reg & E1000_VLVF_VLANID_MASK;
4236 igb_vfta_set(hw, vid, false);
4237 }
4238
4239 wr32(E1000_VLVF(i), reg);
4240 }
Alexander Duyckae641bd2009-09-03 14:49:33 +00004241
4242 adapter->vf_data[vf].vlans_enabled = 0;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08004243}
4244
4245static s32 igb_vlvf_set(struct igb_adapter *adapter, u32 vid, bool add, u32 vf)
4246{
4247 struct e1000_hw *hw = &adapter->hw;
4248 u32 reg, i;
4249
4250 /* It is an error to call this function when VFs are not enabled */
4251 if (!adapter->vfs_allocated_count)
4252 return -1;
4253
4254 /* Find the vlan filter for this id */
4255 for (i = 0; i < E1000_VLVF_ARRAY_SIZE; i++) {
4256 reg = rd32(E1000_VLVF(i));
4257 if ((reg & E1000_VLVF_VLANID_ENABLE) &&
4258 vid == (reg & E1000_VLVF_VLANID_MASK))
4259 break;
4260 }
4261
4262 if (add) {
4263 if (i == E1000_VLVF_ARRAY_SIZE) {
4264 /* Did not find a matching VLAN ID entry that was
4265 * enabled. Search for a free filter entry, i.e.
4266 * one without the enable bit set
4267 */
4268 for (i = 0; i < E1000_VLVF_ARRAY_SIZE; i++) {
4269 reg = rd32(E1000_VLVF(i));
4270 if (!(reg & E1000_VLVF_VLANID_ENABLE))
4271 break;
4272 }
4273 }
4274 if (i < E1000_VLVF_ARRAY_SIZE) {
4275 /* Found an enabled/available entry */
4276 reg |= 1 << (E1000_VLVF_POOLSEL_SHIFT + vf);
4277
4278 /* if !enabled we need to set this up in vfta */
4279 if (!(reg & E1000_VLVF_VLANID_ENABLE)) {
Alexander Duyckcad6d052009-03-13 20:41:37 +00004280 /* add VID to filter table, if bit already set
4281 * PF must have added it outside of table */
4282 if (igb_vfta_set(hw, vid, true))
4283 reg |= 1 << (E1000_VLVF_POOLSEL_SHIFT +
4284 adapter->vfs_allocated_count);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08004285 reg |= E1000_VLVF_VLANID_ENABLE;
4286 }
Alexander Duyckcad6d052009-03-13 20:41:37 +00004287 reg &= ~E1000_VLVF_VLANID_MASK;
4288 reg |= vid;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08004289
4290 wr32(E1000_VLVF(i), reg);
Alexander Duyckae641bd2009-09-03 14:49:33 +00004291
4292 /* do not modify RLPML for PF devices */
4293 if (vf >= adapter->vfs_allocated_count)
4294 return 0;
4295
4296 if (!adapter->vf_data[vf].vlans_enabled) {
4297 u32 size;
4298 reg = rd32(E1000_VMOLR(vf));
4299 size = reg & E1000_VMOLR_RLPML_MASK;
4300 size += 4;
4301 reg &= ~E1000_VMOLR_RLPML_MASK;
4302 reg |= size;
4303 wr32(E1000_VMOLR(vf), reg);
4304 }
4305 adapter->vf_data[vf].vlans_enabled++;
4306
Alexander Duyck4ae196d2009-02-19 20:40:07 -08004307 return 0;
4308 }
4309 } else {
4310 if (i < E1000_VLVF_ARRAY_SIZE) {
4311 /* remove vf from the pool */
4312 reg &= ~(1 << (E1000_VLVF_POOLSEL_SHIFT + vf));
4313 /* if pool is empty then remove entry from vfta */
4314 if (!(reg & E1000_VLVF_POOLSEL_MASK)) {
4315 reg = 0;
4316 igb_vfta_set(hw, vid, false);
4317 }
4318 wr32(E1000_VLVF(i), reg);
Alexander Duyckae641bd2009-09-03 14:49:33 +00004319
4320 /* do not modify RLPML for PF devices */
4321 if (vf >= adapter->vfs_allocated_count)
4322 return 0;
4323
4324 adapter->vf_data[vf].vlans_enabled--;
4325 if (!adapter->vf_data[vf].vlans_enabled) {
4326 u32 size;
4327 reg = rd32(E1000_VMOLR(vf));
4328 size = reg & E1000_VMOLR_RLPML_MASK;
4329 size -= 4;
4330 reg &= ~E1000_VMOLR_RLPML_MASK;
4331 reg |= size;
4332 wr32(E1000_VMOLR(vf), reg);
4333 }
Alexander Duyck4ae196d2009-02-19 20:40:07 -08004334 return 0;
4335 }
4336 }
4337 return -1;
4338}
4339
4340static int igb_set_vf_vlan(struct igb_adapter *adapter, u32 *msgbuf, u32 vf)
4341{
4342 int add = (msgbuf[0] & E1000_VT_MSGINFO_MASK) >> E1000_VT_MSGINFO_SHIFT;
4343 int vid = (msgbuf[1] & E1000_VLVF_VLANID_MASK);
4344
4345 return igb_vlvf_set(adapter, vid, add, vf);
4346}
4347
4348static inline void igb_vf_reset_event(struct igb_adapter *adapter, u32 vf)
4349{
4350 struct e1000_hw *hw = &adapter->hw;
4351
4352 /* disable mailbox functionality for vf */
4353 adapter->vf_data[vf].clear_to_send = false;
4354
4355 /* reset offloads to defaults */
4356 igb_set_vmolr(hw, vf);
4357
4358 /* reset vlans for device */
4359 igb_clear_vf_vfta(adapter, vf);
4360
4361 /* reset multicast table array for vf */
4362 adapter->vf_data[vf].num_vf_mc_hashes = 0;
4363
4364 /* Flush and reset the mta with the new values */
Alexander Duyckff41f8d2009-09-03 14:48:56 +00004365 igb_set_rx_mode(adapter->netdev);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08004366}
4367
4368static inline void igb_vf_reset_msg(struct igb_adapter *adapter, u32 vf)
4369{
4370 struct e1000_hw *hw = &adapter->hw;
4371 unsigned char *vf_mac = adapter->vf_data[vf].vf_mac_addresses;
Alexander Duyckff41f8d2009-09-03 14:48:56 +00004372 int rar_entry = hw->mac.rar_entry_count - (vf + 1);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08004373 u32 reg, msgbuf[3];
4374 u8 *addr = (u8 *)(&msgbuf[1]);
4375
4376 /* process all the same items cleared in a function level reset */
4377 igb_vf_reset_event(adapter, vf);
4378
4379 /* set vf mac address */
Alexander Duyck26ad9172009-10-05 06:32:49 +00004380 igb_rar_set_qsel(adapter, vf_mac, rar_entry, vf);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08004381
4382 /* enable transmit and receive for vf */
4383 reg = rd32(E1000_VFTE);
4384 wr32(E1000_VFTE, reg | (1 << vf));
4385 reg = rd32(E1000_VFRE);
4386 wr32(E1000_VFRE, reg | (1 << vf));
4387
4388 /* enable mailbox functionality for vf */
4389 adapter->vf_data[vf].clear_to_send = true;
4390
4391 /* reply to reset with ack and vf mac address */
4392 msgbuf[0] = E1000_VF_RESET | E1000_VT_MSGTYPE_ACK;
4393 memcpy(addr, vf_mac, 6);
4394 igb_write_mbx(hw, msgbuf, 3, vf);
4395}
4396
4397static int igb_set_vf_mac_addr(struct igb_adapter *adapter, u32 *msg, int vf)
4398{
4399 unsigned char *addr = (char *)&msg[1];
4400 int err = -1;
4401
4402 if (is_valid_ether_addr(addr))
4403 err = igb_set_vf_mac(adapter, vf, addr);
4404
4405 return err;
4406
4407}
4408
4409static void igb_rcv_ack_from_vf(struct igb_adapter *adapter, u32 vf)
4410{
4411 struct e1000_hw *hw = &adapter->hw;
4412 u32 msg = E1000_VT_MSGTYPE_NACK;
4413
4414 /* if device isn't clear to send it shouldn't be reading either */
4415 if (!adapter->vf_data[vf].clear_to_send)
4416 igb_write_mbx(hw, &msg, 1, vf);
4417}
4418
4419
4420static void igb_msg_task(struct igb_adapter *adapter)
4421{
4422 struct e1000_hw *hw = &adapter->hw;
4423 u32 vf;
4424
4425 for (vf = 0; vf < adapter->vfs_allocated_count; vf++) {
4426 /* process any reset requests */
4427 if (!igb_check_for_rst(hw, vf)) {
4428 adapter->vf_data[vf].clear_to_send = false;
4429 igb_vf_reset_event(adapter, vf);
4430 }
4431
4432 /* process any messages pending */
4433 if (!igb_check_for_msg(hw, vf))
4434 igb_rcv_msg_from_vf(adapter, vf);
4435
4436 /* process any acks */
4437 if (!igb_check_for_ack(hw, vf))
4438 igb_rcv_ack_from_vf(adapter, vf);
4439
4440 }
4441}
4442
4443static int igb_rcv_msg_from_vf(struct igb_adapter *adapter, u32 vf)
4444{
4445 u32 mbx_size = E1000_VFMAILBOX_SIZE;
4446 u32 msgbuf[mbx_size];
4447 struct e1000_hw *hw = &adapter->hw;
4448 s32 retval;
4449
4450 retval = igb_read_mbx(hw, msgbuf, mbx_size, vf);
4451
4452 if (retval)
4453 dev_err(&adapter->pdev->dev,
4454 "Error receiving message from VF\n");
4455
4456 /* this is a message we already processed, do nothing */
4457 if (msgbuf[0] & (E1000_VT_MSGTYPE_ACK | E1000_VT_MSGTYPE_NACK))
4458 return retval;
4459
4460 /*
4461 * until the vf completes a reset it should not be
4462 * allowed to start any configuration.
4463 */
4464
4465 if (msgbuf[0] == E1000_VF_RESET) {
4466 igb_vf_reset_msg(adapter, vf);
4467
4468 return retval;
4469 }
4470
4471 if (!adapter->vf_data[vf].clear_to_send) {
4472 msgbuf[0] |= E1000_VT_MSGTYPE_NACK;
4473 igb_write_mbx(hw, msgbuf, 1, vf);
4474 return retval;
4475 }
4476
4477 switch ((msgbuf[0] & 0xFFFF)) {
4478 case E1000_VF_SET_MAC_ADDR:
4479 retval = igb_set_vf_mac_addr(adapter, msgbuf, vf);
4480 break;
4481 case E1000_VF_SET_MULTICAST:
4482 retval = igb_set_vf_multicasts(adapter, msgbuf, vf);
4483 break;
4484 case E1000_VF_SET_LPE:
4485 retval = igb_set_vf_rlpml(adapter, msgbuf[1], vf);
4486 break;
4487 case E1000_VF_SET_VLAN:
4488 retval = igb_set_vf_vlan(adapter, msgbuf, vf);
4489 break;
4490 default:
4491 dev_err(&adapter->pdev->dev, "Unhandled Msg %08x\n", msgbuf[0]);
4492 retval = -1;
4493 break;
4494 }
4495
4496 /* notify the VF of the results of what it sent us */
4497 if (retval)
4498 msgbuf[0] |= E1000_VT_MSGTYPE_NACK;
4499 else
4500 msgbuf[0] |= E1000_VT_MSGTYPE_ACK;
4501
4502 msgbuf[0] |= E1000_VT_MSGTYPE_CTS;
4503
4504 igb_write_mbx(hw, msgbuf, 1, vf);
4505
4506 return retval;
4507}
4508
Auke Kok9d5c8242008-01-24 02:22:38 -08004509/**
Alexander Duyck68d480c2009-10-05 06:33:08 +00004510 * igb_set_uta - Set unicast filter table address
4511 * @adapter: board private structure
4512 *
4513 * The unicast table address is a register array of 32-bit registers.
4514 * The table is meant to be used in a way similar to how the MTA is used
4515 * however due to certain limitations in the hardware it is necessary to
4516 * set all the hash bits to 1 and use the VMOLR ROPE bit as a promiscous
4517 * enable bit to allow vlan tag stripping when promiscous mode is enabled
4518 **/
4519static void igb_set_uta(struct igb_adapter *adapter)
4520{
4521 struct e1000_hw *hw = &adapter->hw;
4522 int i;
4523
4524 /* The UTA table only exists on 82576 hardware and newer */
4525 if (hw->mac.type < e1000_82576)
4526 return;
4527
4528 /* we only need to do this if VMDq is enabled */
4529 if (!adapter->vfs_allocated_count)
4530 return;
4531
4532 for (i = 0; i < hw->mac.uta_reg_count; i++)
4533 array_wr32(E1000_UTA, i, ~0);
4534}
4535
4536/**
Auke Kok9d5c8242008-01-24 02:22:38 -08004537 * igb_intr_msi - Interrupt Handler
4538 * @irq: interrupt number
4539 * @data: pointer to a network interface device structure
4540 **/
4541static irqreturn_t igb_intr_msi(int irq, void *data)
4542{
Alexander Duyck047e0032009-10-27 15:49:27 +00004543 struct igb_adapter *adapter = data;
4544 struct igb_q_vector *q_vector = adapter->q_vector[0];
Auke Kok9d5c8242008-01-24 02:22:38 -08004545 struct e1000_hw *hw = &adapter->hw;
4546 /* read ICR disables interrupts using IAM */
4547 u32 icr = rd32(E1000_ICR);
4548
Alexander Duyck047e0032009-10-27 15:49:27 +00004549 igb_write_itr(q_vector);
Auke Kok9d5c8242008-01-24 02:22:38 -08004550
Alexander Duyck047e0032009-10-27 15:49:27 +00004551 if (icr & E1000_ICR_DOUTSYNC) {
Alexander Duyckdda0e082009-02-06 23:19:08 +00004552 /* HW is reporting DMA is out of sync */
4553 adapter->stats.doosync++;
4554 }
4555
Auke Kok9d5c8242008-01-24 02:22:38 -08004556 if (icr & (E1000_ICR_RXSEQ | E1000_ICR_LSC)) {
4557 hw->mac.get_link_status = 1;
4558 if (!test_bit(__IGB_DOWN, &adapter->state))
4559 mod_timer(&adapter->watchdog_timer, jiffies + 1);
4560 }
4561
Alexander Duyck047e0032009-10-27 15:49:27 +00004562 napi_schedule(&q_vector->napi);
Auke Kok9d5c8242008-01-24 02:22:38 -08004563
4564 return IRQ_HANDLED;
4565}
4566
4567/**
Alexander Duyck4a3c6432009-02-06 23:20:49 +00004568 * igb_intr - Legacy Interrupt Handler
Auke Kok9d5c8242008-01-24 02:22:38 -08004569 * @irq: interrupt number
4570 * @data: pointer to a network interface device structure
4571 **/
4572static irqreturn_t igb_intr(int irq, void *data)
4573{
Alexander Duyck047e0032009-10-27 15:49:27 +00004574 struct igb_adapter *adapter = data;
4575 struct igb_q_vector *q_vector = adapter->q_vector[0];
Auke Kok9d5c8242008-01-24 02:22:38 -08004576 struct e1000_hw *hw = &adapter->hw;
4577 /* Interrupt Auto-Mask...upon reading ICR, interrupts are masked. No
4578 * need for the IMC write */
4579 u32 icr = rd32(E1000_ICR);
Auke Kok9d5c8242008-01-24 02:22:38 -08004580 if (!icr)
4581 return IRQ_NONE; /* Not our interrupt */
4582
Alexander Duyck047e0032009-10-27 15:49:27 +00004583 igb_write_itr(q_vector);
Auke Kok9d5c8242008-01-24 02:22:38 -08004584
4585 /* IMS will not auto-mask if INT_ASSERTED is not set, and if it is
4586 * not set, then the adapter didn't send an interrupt */
4587 if (!(icr & E1000_ICR_INT_ASSERTED))
4588 return IRQ_NONE;
4589
Alexander Duyck047e0032009-10-27 15:49:27 +00004590 if (icr & E1000_ICR_DOUTSYNC) {
Alexander Duyckdda0e082009-02-06 23:19:08 +00004591 /* HW is reporting DMA is out of sync */
4592 adapter->stats.doosync++;
4593 }
4594
Auke Kok9d5c8242008-01-24 02:22:38 -08004595 if (icr & (E1000_ICR_RXSEQ | E1000_ICR_LSC)) {
4596 hw->mac.get_link_status = 1;
4597 /* guard against interrupt when we're going down */
4598 if (!test_bit(__IGB_DOWN, &adapter->state))
4599 mod_timer(&adapter->watchdog_timer, jiffies + 1);
4600 }
4601
Alexander Duyck047e0032009-10-27 15:49:27 +00004602 napi_schedule(&q_vector->napi);
Auke Kok9d5c8242008-01-24 02:22:38 -08004603
4604 return IRQ_HANDLED;
4605}
4606
Alexander Duyck047e0032009-10-27 15:49:27 +00004607static inline void igb_ring_irq_enable(struct igb_q_vector *q_vector)
Alexander Duyck46544252009-02-19 20:39:04 -08004608{
Alexander Duyck047e0032009-10-27 15:49:27 +00004609 struct igb_adapter *adapter = q_vector->adapter;
Alexander Duyck46544252009-02-19 20:39:04 -08004610 struct e1000_hw *hw = &adapter->hw;
4611
4612 if (adapter->itr_setting & 3) {
Alexander Duyck047e0032009-10-27 15:49:27 +00004613 if (!adapter->msix_entries)
Alexander Duyck46544252009-02-19 20:39:04 -08004614 igb_set_itr(adapter);
4615 else
Alexander Duyck047e0032009-10-27 15:49:27 +00004616 igb_update_ring_itr(q_vector);
Alexander Duyck46544252009-02-19 20:39:04 -08004617 }
4618
4619 if (!test_bit(__IGB_DOWN, &adapter->state)) {
4620 if (adapter->msix_entries)
Alexander Duyck047e0032009-10-27 15:49:27 +00004621 wr32(E1000_EIMS, q_vector->eims_value);
Alexander Duyck46544252009-02-19 20:39:04 -08004622 else
4623 igb_irq_enable(adapter);
4624 }
4625}
4626
Auke Kok9d5c8242008-01-24 02:22:38 -08004627/**
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07004628 * igb_poll - NAPI Rx polling callback
4629 * @napi: napi polling structure
4630 * @budget: count of how many packets we should handle
Auke Kok9d5c8242008-01-24 02:22:38 -08004631 **/
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07004632static int igb_poll(struct napi_struct *napi, int budget)
Auke Kok9d5c8242008-01-24 02:22:38 -08004633{
Alexander Duyck047e0032009-10-27 15:49:27 +00004634 struct igb_q_vector *q_vector = container_of(napi,
4635 struct igb_q_vector,
4636 napi);
4637 int tx_clean_complete = 1, work_done = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08004638
Jeff Kirsher421e02f2008-10-17 11:08:31 -07004639#ifdef CONFIG_IGB_DCA
Alexander Duyck047e0032009-10-27 15:49:27 +00004640 if (q_vector->adapter->flags & IGB_FLAG_DCA_ENABLED)
4641 igb_update_dca(q_vector);
Jeb Cramerfe4506b2008-07-08 15:07:55 -07004642#endif
Alexander Duyck047e0032009-10-27 15:49:27 +00004643 if (q_vector->tx_ring)
4644 tx_clean_complete = igb_clean_tx_irq(q_vector);
Auke Kok9d5c8242008-01-24 02:22:38 -08004645
Alexander Duyck047e0032009-10-27 15:49:27 +00004646 if (q_vector->rx_ring)
4647 igb_clean_rx_irq_adv(q_vector, &work_done, budget);
4648
4649 if (!tx_clean_complete)
4650 work_done = budget;
Auke Kok9d5c8242008-01-24 02:22:38 -08004651
Alexander Duyck46544252009-02-19 20:39:04 -08004652 /* If not enough Rx work done, exit the polling mode */
Alexander Duyck5e6d5b12009-03-13 20:40:38 +00004653 if (work_done < budget) {
Alexander Duyck46544252009-02-19 20:39:04 -08004654 napi_complete(napi);
Alexander Duyck047e0032009-10-27 15:49:27 +00004655 igb_ring_irq_enable(q_vector);
Alexander Duyck46544252009-02-19 20:39:04 -08004656 }
4657
4658 return work_done;
Auke Kok9d5c8242008-01-24 02:22:38 -08004659}
Al Viro6d8126f2008-03-16 22:23:24 +00004660
Auke Kok9d5c8242008-01-24 02:22:38 -08004661/**
Patrick Ohly33af6bc2009-02-12 05:03:43 +00004662 * igb_hwtstamp - utility function which checks for TX time stamp
4663 * @adapter: board private structure
4664 * @skb: packet that was just sent
4665 *
4666 * If we were asked to do hardware stamping and such a time stamp is
4667 * available, then it must have been for this skb here because we only
4668 * allow only one such packet into the queue.
4669 */
4670static void igb_tx_hwtstamp(struct igb_adapter *adapter, struct sk_buff *skb)
4671{
4672 union skb_shared_tx *shtx = skb_tx(skb);
4673 struct e1000_hw *hw = &adapter->hw;
4674
4675 if (unlikely(shtx->hardware)) {
4676 u32 valid = rd32(E1000_TSYNCTXCTL) & E1000_TSYNCTXCTL_VALID;
4677 if (valid) {
4678 u64 regval = rd32(E1000_TXSTMPL);
4679 u64 ns;
4680 struct skb_shared_hwtstamps shhwtstamps;
4681
4682 memset(&shhwtstamps, 0, sizeof(shhwtstamps));
4683 regval |= (u64)rd32(E1000_TXSTMPH) << 32;
4684 ns = timecounter_cyc2time(&adapter->clock,
4685 regval);
4686 timecompare_update(&adapter->compare, ns);
4687 shhwtstamps.hwtstamp = ns_to_ktime(ns);
4688 shhwtstamps.syststamp =
4689 timecompare_transform(&adapter->compare, ns);
4690 skb_tstamp_tx(skb, &shhwtstamps);
4691 }
Patrick Ohly33af6bc2009-02-12 05:03:43 +00004692 }
4693}
4694
4695/**
Auke Kok9d5c8242008-01-24 02:22:38 -08004696 * igb_clean_tx_irq - Reclaim resources after transmit completes
Alexander Duyck047e0032009-10-27 15:49:27 +00004697 * @q_vector: pointer to q_vector containing needed info
Auke Kok9d5c8242008-01-24 02:22:38 -08004698 * returns true if ring is completely cleaned
4699 **/
Alexander Duyck047e0032009-10-27 15:49:27 +00004700static bool igb_clean_tx_irq(struct igb_q_vector *q_vector)
Auke Kok9d5c8242008-01-24 02:22:38 -08004701{
Alexander Duyck047e0032009-10-27 15:49:27 +00004702 struct igb_adapter *adapter = q_vector->adapter;
4703 struct igb_ring *tx_ring = q_vector->tx_ring;
Mitch Williams3b644cf2008-06-27 10:59:48 -07004704 struct net_device *netdev = adapter->netdev;
Alexander Duyck0e014cb2008-12-26 01:33:18 -08004705 struct e1000_hw *hw = &adapter->hw;
Auke Kok9d5c8242008-01-24 02:22:38 -08004706 struct igb_buffer *buffer_info;
4707 struct sk_buff *skb;
Alexander Duyck0e014cb2008-12-26 01:33:18 -08004708 union e1000_adv_tx_desc *tx_desc, *eop_desc;
Auke Kok9d5c8242008-01-24 02:22:38 -08004709 unsigned int total_bytes = 0, total_packets = 0;
Alexander Duyck0e014cb2008-12-26 01:33:18 -08004710 unsigned int i, eop, count = 0;
4711 bool cleaned = false;
Auke Kok9d5c8242008-01-24 02:22:38 -08004712
Auke Kok9d5c8242008-01-24 02:22:38 -08004713 i = tx_ring->next_to_clean;
Alexander Duyck0e014cb2008-12-26 01:33:18 -08004714 eop = tx_ring->buffer_info[i].next_to_watch;
4715 eop_desc = E1000_TX_DESC_ADV(*tx_ring, eop);
4716
4717 while ((eop_desc->wb.status & cpu_to_le32(E1000_TXD_STAT_DD)) &&
4718 (count < tx_ring->count)) {
4719 for (cleaned = false; !cleaned; count++) {
4720 tx_desc = E1000_TX_DESC_ADV(*tx_ring, i);
Auke Kok9d5c8242008-01-24 02:22:38 -08004721 buffer_info = &tx_ring->buffer_info[i];
Alexander Duyck0e014cb2008-12-26 01:33:18 -08004722 cleaned = (i == eop);
Auke Kok9d5c8242008-01-24 02:22:38 -08004723 skb = buffer_info->skb;
4724
4725 if (skb) {
4726 unsigned int segs, bytecount;
4727 /* gso_segs is currently only valid for tcp */
4728 segs = skb_shinfo(skb)->gso_segs ?: 1;
4729 /* multiply data chunks by size of headers */
4730 bytecount = ((segs - 1) * skb_headlen(skb)) +
4731 skb->len;
4732 total_packets += segs;
4733 total_bytes += bytecount;
Patrick Ohly33af6bc2009-02-12 05:03:43 +00004734
4735 igb_tx_hwtstamp(adapter, skb);
Auke Kok9d5c8242008-01-24 02:22:38 -08004736 }
4737
4738 igb_unmap_and_free_tx_resource(adapter, buffer_info);
Alexander Duyck0e014cb2008-12-26 01:33:18 -08004739 tx_desc->wb.status = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08004740
4741 i++;
4742 if (i == tx_ring->count)
4743 i = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08004744 }
Alexander Duyck0e014cb2008-12-26 01:33:18 -08004745 eop = tx_ring->buffer_info[i].next_to_watch;
4746 eop_desc = E1000_TX_DESC_ADV(*tx_ring, eop);
4747 }
4748
Auke Kok9d5c8242008-01-24 02:22:38 -08004749 tx_ring->next_to_clean = i;
4750
Alexander Duyckfc7d3452008-08-26 04:25:08 -07004751 if (unlikely(count &&
Auke Kok9d5c8242008-01-24 02:22:38 -08004752 netif_carrier_ok(netdev) &&
Alexander Duyckc493ea42009-03-20 00:16:50 +00004753 igb_desc_unused(tx_ring) >= IGB_TX_QUEUE_WAKE)) {
Auke Kok9d5c8242008-01-24 02:22:38 -08004754 /* Make sure that anybody stopping the queue after this
4755 * sees the new next_to_clean.
4756 */
4757 smp_mb();
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07004758 if (__netif_subqueue_stopped(netdev, tx_ring->queue_index) &&
4759 !(test_bit(__IGB_DOWN, &adapter->state))) {
4760 netif_wake_subqueue(netdev, tx_ring->queue_index);
4761 ++adapter->restart_queue;
4762 }
Auke Kok9d5c8242008-01-24 02:22:38 -08004763 }
4764
4765 if (tx_ring->detect_tx_hung) {
4766 /* Detect a transmit hang in hardware, this serializes the
4767 * check with the clearing of time_stamp and movement of i */
4768 tx_ring->detect_tx_hung = false;
4769 if (tx_ring->buffer_info[i].time_stamp &&
4770 time_after(jiffies, tx_ring->buffer_info[i].time_stamp +
4771 (adapter->tx_timeout_factor * HZ))
4772 && !(rd32(E1000_STATUS) &
4773 E1000_STATUS_TXOFF)) {
4774
Auke Kok9d5c8242008-01-24 02:22:38 -08004775 /* detected Tx unit hang */
4776 dev_err(&adapter->pdev->dev,
4777 "Detected Tx Unit Hang\n"
Alexander Duyck2d064c02008-07-08 15:10:12 -07004778 " Tx Queue <%d>\n"
Auke Kok9d5c8242008-01-24 02:22:38 -08004779 " TDH <%x>\n"
4780 " TDT <%x>\n"
4781 " next_to_use <%x>\n"
4782 " next_to_clean <%x>\n"
Auke Kok9d5c8242008-01-24 02:22:38 -08004783 "buffer_info[next_to_clean]\n"
4784 " time_stamp <%lx>\n"
Alexander Duyck0e014cb2008-12-26 01:33:18 -08004785 " next_to_watch <%x>\n"
Auke Kok9d5c8242008-01-24 02:22:38 -08004786 " jiffies <%lx>\n"
4787 " desc.status <%x>\n",
Alexander Duyck2d064c02008-07-08 15:10:12 -07004788 tx_ring->queue_index,
Auke Kok9d5c8242008-01-24 02:22:38 -08004789 readl(adapter->hw.hw_addr + tx_ring->head),
4790 readl(adapter->hw.hw_addr + tx_ring->tail),
4791 tx_ring->next_to_use,
4792 tx_ring->next_to_clean,
Auke Kok9d5c8242008-01-24 02:22:38 -08004793 tx_ring->buffer_info[i].time_stamp,
Alexander Duyck0e014cb2008-12-26 01:33:18 -08004794 eop,
Auke Kok9d5c8242008-01-24 02:22:38 -08004795 jiffies,
Alexander Duyck0e014cb2008-12-26 01:33:18 -08004796 eop_desc->wb.status);
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07004797 netif_stop_subqueue(netdev, tx_ring->queue_index);
Auke Kok9d5c8242008-01-24 02:22:38 -08004798 }
4799 }
4800 tx_ring->total_bytes += total_bytes;
4801 tx_ring->total_packets += total_packets;
Alexander Duycke21ed352008-07-08 15:07:24 -07004802 tx_ring->tx_stats.bytes += total_bytes;
4803 tx_ring->tx_stats.packets += total_packets;
Ajit Khaparde8d24e932009-10-07 02:42:56 +00004804 netdev->stats.tx_bytes += total_bytes;
4805 netdev->stats.tx_packets += total_packets;
Alexander Duyck0e014cb2008-12-26 01:33:18 -08004806 return (count < tx_ring->count);
Auke Kok9d5c8242008-01-24 02:22:38 -08004807}
4808
Auke Kok9d5c8242008-01-24 02:22:38 -08004809/**
4810 * igb_receive_skb - helper function to handle rx indications
Alexander Duyck047e0032009-10-27 15:49:27 +00004811 * @q_vector: structure containing interrupt and ring information
4812 * @skb: packet to send up
4813 * @vlan_tag: vlan tag for packet
Auke Kok9d5c8242008-01-24 02:22:38 -08004814 **/
Alexander Duyck047e0032009-10-27 15:49:27 +00004815static void igb_receive_skb(struct igb_q_vector *q_vector,
4816 struct sk_buff *skb,
4817 u16 vlan_tag)
Auke Kok9d5c8242008-01-24 02:22:38 -08004818{
Alexander Duyck047e0032009-10-27 15:49:27 +00004819 struct igb_adapter *adapter = q_vector->adapter;
Alexander Duyckd3352522008-07-08 15:12:13 -07004820
Alexander Duyck047e0032009-10-27 15:49:27 +00004821 if (vlan_tag)
4822 vlan_gro_receive(&q_vector->napi, adapter->vlgrp,
4823 vlan_tag, skb);
Alexander Duyck182ff8d2009-04-27 22:35:33 +00004824 else
Alexander Duyck047e0032009-10-27 15:49:27 +00004825 napi_gro_receive(&q_vector->napi, skb);
Auke Kok9d5c8242008-01-24 02:22:38 -08004826}
4827
Auke Kok9d5c8242008-01-24 02:22:38 -08004828static inline void igb_rx_checksum_adv(struct igb_adapter *adapter,
4829 u32 status_err, struct sk_buff *skb)
4830{
4831 skb->ip_summed = CHECKSUM_NONE;
4832
4833 /* Ignore Checksum bit is set or checksum is disabled through ethtool */
Alexander Duyck7beb0142009-05-06 10:25:23 +00004834 if ((status_err & E1000_RXD_STAT_IXSM) ||
4835 (adapter->flags & IGB_FLAG_RX_CSUM_DISABLED))
Auke Kok9d5c8242008-01-24 02:22:38 -08004836 return;
4837 /* TCP/UDP checksum error bit is set */
4838 if (status_err &
4839 (E1000_RXDEXT_STATERR_TCPE | E1000_RXDEXT_STATERR_IPE)) {
Jesse Brandeburgb9473562009-04-27 22:36:13 +00004840 /*
4841 * work around errata with sctp packets where the TCPE aka
4842 * L4E bit is set incorrectly on 64 byte (60 byte w/o crc)
4843 * packets, (aka let the stack check the crc32c)
4844 */
4845 if (!((adapter->hw.mac.type == e1000_82576) &&
4846 (skb->len == 60)))
4847 adapter->hw_csum_err++;
Auke Kok9d5c8242008-01-24 02:22:38 -08004848 /* let the stack verify checksum errors */
Auke Kok9d5c8242008-01-24 02:22:38 -08004849 return;
4850 }
4851 /* It must be a TCP or UDP packet with a valid checksum */
4852 if (status_err & (E1000_RXD_STAT_TCPCS | E1000_RXD_STAT_UDPCS))
4853 skb->ip_summed = CHECKSUM_UNNECESSARY;
4854
Jesse Brandeburgb9473562009-04-27 22:36:13 +00004855 dev_dbg(&adapter->pdev->dev, "cksum success: bits %08X\n", status_err);
Auke Kok9d5c8242008-01-24 02:22:38 -08004856}
4857
Alexander Duyck2d94d8a2009-07-23 18:10:06 +00004858static inline u16 igb_get_hlen(struct igb_adapter *adapter,
4859 union e1000_adv_rx_desc *rx_desc)
4860{
4861 /* HW will not DMA in data larger than the given buffer, even if it
4862 * parses the (NFS, of course) header to be larger. In that case, it
4863 * fills the header buffer and spills the rest into the page.
4864 */
4865 u16 hlen = (le16_to_cpu(rx_desc->wb.lower.lo_dword.hdr_info) &
4866 E1000_RXDADV_HDRBUFLEN_MASK) >> E1000_RXDADV_HDRBUFLEN_SHIFT;
4867 if (hlen > adapter->rx_ps_hdr_size)
4868 hlen = adapter->rx_ps_hdr_size;
4869 return hlen;
4870}
4871
Alexander Duyck047e0032009-10-27 15:49:27 +00004872static bool igb_clean_rx_irq_adv(struct igb_q_vector *q_vector,
4873 int *work_done, int budget)
Auke Kok9d5c8242008-01-24 02:22:38 -08004874{
Alexander Duyck047e0032009-10-27 15:49:27 +00004875 struct igb_adapter *adapter = q_vector->adapter;
Auke Kok9d5c8242008-01-24 02:22:38 -08004876 struct net_device *netdev = adapter->netdev;
Alexander Duyck047e0032009-10-27 15:49:27 +00004877 struct igb_ring *rx_ring = q_vector->rx_ring;
Patrick Ohly33af6bc2009-02-12 05:03:43 +00004878 struct e1000_hw *hw = &adapter->hw;
Auke Kok9d5c8242008-01-24 02:22:38 -08004879 struct pci_dev *pdev = adapter->pdev;
4880 union e1000_adv_rx_desc *rx_desc , *next_rxd;
4881 struct igb_buffer *buffer_info , *next_buffer;
4882 struct sk_buff *skb;
Auke Kok9d5c8242008-01-24 02:22:38 -08004883 bool cleaned = false;
4884 int cleaned_count = 0;
4885 unsigned int total_bytes = 0, total_packets = 0;
Alexander Duyck73cd78f2009-02-12 18:16:59 +00004886 unsigned int i;
Alexander Duyck2d94d8a2009-07-23 18:10:06 +00004887 u32 staterr;
4888 u16 length;
Alexander Duyck047e0032009-10-27 15:49:27 +00004889 u16 vlan_tag;
Auke Kok9d5c8242008-01-24 02:22:38 -08004890
4891 i = rx_ring->next_to_clean;
Alexander Duyck69d3ca52009-02-06 23:15:04 +00004892 buffer_info = &rx_ring->buffer_info[i];
Auke Kok9d5c8242008-01-24 02:22:38 -08004893 rx_desc = E1000_RX_DESC_ADV(*rx_ring, i);
4894 staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
4895
4896 while (staterr & E1000_RXD_STAT_DD) {
4897 if (*work_done >= budget)
4898 break;
4899 (*work_done)++;
Alexander Duyck69d3ca52009-02-06 23:15:04 +00004900
4901 skb = buffer_info->skb;
4902 prefetch(skb->data - NET_IP_ALIGN);
4903 buffer_info->skb = NULL;
4904
4905 i++;
4906 if (i == rx_ring->count)
4907 i = 0;
4908 next_rxd = E1000_RX_DESC_ADV(*rx_ring, i);
4909 prefetch(next_rxd);
4910 next_buffer = &rx_ring->buffer_info[i];
4911
4912 length = le16_to_cpu(rx_desc->wb.upper.length);
4913 cleaned = true;
4914 cleaned_count++;
4915
Jesse Brandeburg91615f72009-06-30 12:45:15 +00004916 /* this is the fast path for the non-packet split case */
Alexander Duyck69d3ca52009-02-06 23:15:04 +00004917 if (!adapter->rx_ps_hdr_size) {
4918 pci_unmap_single(pdev, buffer_info->dma,
Jesse Brandeburg91615f72009-06-30 12:45:15 +00004919 adapter->rx_buffer_len,
Alexander Duyck69d3ca52009-02-06 23:15:04 +00004920 PCI_DMA_FROMDEVICE);
Jesse Brandeburg91615f72009-06-30 12:45:15 +00004921 buffer_info->dma = 0;
Alexander Duyck69d3ca52009-02-06 23:15:04 +00004922 skb_put(skb, length);
4923 goto send_up;
4924 }
Auke Kok9d5c8242008-01-24 02:22:38 -08004925
Alexander Duyck2d94d8a2009-07-23 18:10:06 +00004926 if (buffer_info->dma) {
4927 u16 hlen = igb_get_hlen(adapter, rx_desc);
Alexander Duyckbf36c1a2008-07-08 15:11:40 -07004928 pci_unmap_single(pdev, buffer_info->dma,
Jesse Brandeburg91615f72009-06-30 12:45:15 +00004929 adapter->rx_ps_hdr_size,
Alexander Duyckbf36c1a2008-07-08 15:11:40 -07004930 PCI_DMA_FROMDEVICE);
Jesse Brandeburg91615f72009-06-30 12:45:15 +00004931 buffer_info->dma = 0;
Alexander Duyckbf36c1a2008-07-08 15:11:40 -07004932 skb_put(skb, hlen);
4933 }
4934
4935 if (length) {
Auke Kok9d5c8242008-01-24 02:22:38 -08004936 pci_unmap_page(pdev, buffer_info->page_dma,
Alexander Duyckbf36c1a2008-07-08 15:11:40 -07004937 PAGE_SIZE / 2, PCI_DMA_FROMDEVICE);
Auke Kok9d5c8242008-01-24 02:22:38 -08004938 buffer_info->page_dma = 0;
Alexander Duyckbf36c1a2008-07-08 15:11:40 -07004939
4940 skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags++,
4941 buffer_info->page,
4942 buffer_info->page_offset,
4943 length);
4944
4945 if ((adapter->rx_buffer_len > (PAGE_SIZE / 2)) ||
4946 (page_count(buffer_info->page) != 1))
4947 buffer_info->page = NULL;
4948 else
4949 get_page(buffer_info->page);
Auke Kok9d5c8242008-01-24 02:22:38 -08004950
4951 skb->len += length;
4952 skb->data_len += length;
Alexander Duyckbf36c1a2008-07-08 15:11:40 -07004953
Auke Kok9d5c8242008-01-24 02:22:38 -08004954 skb->truesize += length;
Auke Kok9d5c8242008-01-24 02:22:38 -08004955 }
Auke Kok9d5c8242008-01-24 02:22:38 -08004956
Alexander Duyckbf36c1a2008-07-08 15:11:40 -07004957 if (!(staterr & E1000_RXD_STAT_EOP)) {
Alexander Duyckb2d56532008-11-20 00:47:34 -08004958 buffer_info->skb = next_buffer->skb;
4959 buffer_info->dma = next_buffer->dma;
4960 next_buffer->skb = skb;
4961 next_buffer->dma = 0;
Alexander Duyckbf36c1a2008-07-08 15:11:40 -07004962 goto next_desc;
4963 }
Alexander Duyck69d3ca52009-02-06 23:15:04 +00004964send_up:
Patrick Ohly33af6bc2009-02-12 05:03:43 +00004965 /*
4966 * If this bit is set, then the RX registers contain
4967 * the time stamp. No other packet will be time
4968 * stamped until we read these registers, so read the
4969 * registers to make them available again. Because
4970 * only one packet can be time stamped at a time, we
4971 * know that the register values must belong to this
4972 * one here and therefore we don't need to compare
4973 * any of the additional attributes stored for it.
4974 *
4975 * If nothing went wrong, then it should have a
4976 * skb_shared_tx that we can turn into a
4977 * skb_shared_hwtstamps.
4978 *
4979 * TODO: can time stamping be triggered (thus locking
4980 * the registers) without the packet reaching this point
4981 * here? In that case RX time stamping would get stuck.
4982 *
4983 * TODO: in "time stamp all packets" mode this bit is
4984 * not set. Need a global flag for this mode and then
4985 * always read the registers. Cannot be done without
4986 * a race condition.
4987 */
4988 if (unlikely(staterr & E1000_RXD_STAT_TS)) {
4989 u64 regval;
4990 u64 ns;
4991 struct skb_shared_hwtstamps *shhwtstamps =
4992 skb_hwtstamps(skb);
4993
4994 WARN(!(rd32(E1000_TSYNCRXCTL) & E1000_TSYNCRXCTL_VALID),
4995 "igb: no RX time stamp available for time stamped packet");
4996 regval = rd32(E1000_RXSTMPL);
4997 regval |= (u64)rd32(E1000_RXSTMPH) << 32;
4998 ns = timecounter_cyc2time(&adapter->clock, regval);
4999 timecompare_update(&adapter->compare, ns);
5000 memset(shhwtstamps, 0, sizeof(*shhwtstamps));
5001 shhwtstamps->hwtstamp = ns_to_ktime(ns);
5002 shhwtstamps->syststamp =
5003 timecompare_transform(&adapter->compare, ns);
5004 }
5005
Auke Kok9d5c8242008-01-24 02:22:38 -08005006 if (staterr & E1000_RXDEXT_ERR_FRAME_ERR_MASK) {
5007 dev_kfree_skb_irq(skb);
5008 goto next_desc;
5009 }
Auke Kok9d5c8242008-01-24 02:22:38 -08005010
5011 total_bytes += skb->len;
5012 total_packets++;
5013
5014 igb_rx_checksum_adv(adapter, staterr, skb);
5015
5016 skb->protocol = eth_type_trans(skb, netdev);
Alexander Duyck047e0032009-10-27 15:49:27 +00005017 skb_record_rx_queue(skb, rx_ring->queue_index);
Auke Kok9d5c8242008-01-24 02:22:38 -08005018
Alexander Duyck047e0032009-10-27 15:49:27 +00005019 vlan_tag = ((staterr & E1000_RXD_STAT_VP) ?
5020 le16_to_cpu(rx_desc->wb.upper.vlan) : 0);
5021
5022 igb_receive_skb(q_vector, skb, vlan_tag);
Auke Kok9d5c8242008-01-24 02:22:38 -08005023
Auke Kok9d5c8242008-01-24 02:22:38 -08005024next_desc:
5025 rx_desc->wb.upper.status_error = 0;
5026
5027 /* return some buffers to hardware, one at a time is too slow */
5028 if (cleaned_count >= IGB_RX_BUFFER_WRITE) {
Mitch Williams3b644cf2008-06-27 10:59:48 -07005029 igb_alloc_rx_buffers_adv(rx_ring, cleaned_count);
Auke Kok9d5c8242008-01-24 02:22:38 -08005030 cleaned_count = 0;
5031 }
5032
5033 /* use prefetched values */
5034 rx_desc = next_rxd;
5035 buffer_info = next_buffer;
Auke Kok9d5c8242008-01-24 02:22:38 -08005036 staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
5037 }
Alexander Duyckbf36c1a2008-07-08 15:11:40 -07005038
Auke Kok9d5c8242008-01-24 02:22:38 -08005039 rx_ring->next_to_clean = i;
Alexander Duyckc493ea42009-03-20 00:16:50 +00005040 cleaned_count = igb_desc_unused(rx_ring);
Auke Kok9d5c8242008-01-24 02:22:38 -08005041
5042 if (cleaned_count)
Mitch Williams3b644cf2008-06-27 10:59:48 -07005043 igb_alloc_rx_buffers_adv(rx_ring, cleaned_count);
Auke Kok9d5c8242008-01-24 02:22:38 -08005044
5045 rx_ring->total_packets += total_packets;
5046 rx_ring->total_bytes += total_bytes;
5047 rx_ring->rx_stats.packets += total_packets;
5048 rx_ring->rx_stats.bytes += total_bytes;
Ajit Khaparde8d24e932009-10-07 02:42:56 +00005049 netdev->stats.rx_bytes += total_bytes;
5050 netdev->stats.rx_packets += total_packets;
Auke Kok9d5c8242008-01-24 02:22:38 -08005051 return cleaned;
5052}
5053
Auke Kok9d5c8242008-01-24 02:22:38 -08005054/**
5055 * igb_alloc_rx_buffers_adv - Replace used receive buffers; packet split
5056 * @adapter: address of board private structure
5057 **/
Mitch Williams3b644cf2008-06-27 10:59:48 -07005058static void igb_alloc_rx_buffers_adv(struct igb_ring *rx_ring,
Auke Kok9d5c8242008-01-24 02:22:38 -08005059 int cleaned_count)
5060{
Alexander Duyck047e0032009-10-27 15:49:27 +00005061 struct igb_adapter *adapter = rx_ring->q_vector->adapter;
Auke Kok9d5c8242008-01-24 02:22:38 -08005062 struct net_device *netdev = adapter->netdev;
5063 struct pci_dev *pdev = adapter->pdev;
5064 union e1000_adv_rx_desc *rx_desc;
5065 struct igb_buffer *buffer_info;
5066 struct sk_buff *skb;
5067 unsigned int i;
Alexander Duyckdb761762009-02-06 23:15:25 +00005068 int bufsz;
Auke Kok9d5c8242008-01-24 02:22:38 -08005069
5070 i = rx_ring->next_to_use;
5071 buffer_info = &rx_ring->buffer_info[i];
5072
Alexander Duyckdb761762009-02-06 23:15:25 +00005073 if (adapter->rx_ps_hdr_size)
5074 bufsz = adapter->rx_ps_hdr_size;
5075 else
5076 bufsz = adapter->rx_buffer_len;
Alexander Duyckdb761762009-02-06 23:15:25 +00005077
Auke Kok9d5c8242008-01-24 02:22:38 -08005078 while (cleaned_count--) {
5079 rx_desc = E1000_RX_DESC_ADV(*rx_ring, i);
5080
Alexander Duyckbf36c1a2008-07-08 15:11:40 -07005081 if (adapter->rx_ps_hdr_size && !buffer_info->page_dma) {
Auke Kok9d5c8242008-01-24 02:22:38 -08005082 if (!buffer_info->page) {
Alexander Duyckbf36c1a2008-07-08 15:11:40 -07005083 buffer_info->page = alloc_page(GFP_ATOMIC);
5084 if (!buffer_info->page) {
5085 adapter->alloc_rx_buff_failed++;
5086 goto no_buffers;
5087 }
5088 buffer_info->page_offset = 0;
5089 } else {
5090 buffer_info->page_offset ^= PAGE_SIZE / 2;
Auke Kok9d5c8242008-01-24 02:22:38 -08005091 }
5092 buffer_info->page_dma =
Alexander Duyckdb761762009-02-06 23:15:25 +00005093 pci_map_page(pdev, buffer_info->page,
Alexander Duyckbf36c1a2008-07-08 15:11:40 -07005094 buffer_info->page_offset,
5095 PAGE_SIZE / 2,
Auke Kok9d5c8242008-01-24 02:22:38 -08005096 PCI_DMA_FROMDEVICE);
5097 }
5098
5099 if (!buffer_info->skb) {
Eric Dumazet89d71a62009-10-13 05:34:20 +00005100 skb = netdev_alloc_skb_ip_align(netdev, bufsz);
Auke Kok9d5c8242008-01-24 02:22:38 -08005101 if (!skb) {
5102 adapter->alloc_rx_buff_failed++;
5103 goto no_buffers;
5104 }
5105
Auke Kok9d5c8242008-01-24 02:22:38 -08005106 buffer_info->skb = skb;
5107 buffer_info->dma = pci_map_single(pdev, skb->data,
5108 bufsz,
5109 PCI_DMA_FROMDEVICE);
Auke Kok9d5c8242008-01-24 02:22:38 -08005110 }
5111 /* Refresh the desc even if buffer_addrs didn't change because
5112 * each write-back erases this info. */
5113 if (adapter->rx_ps_hdr_size) {
5114 rx_desc->read.pkt_addr =
5115 cpu_to_le64(buffer_info->page_dma);
5116 rx_desc->read.hdr_addr = cpu_to_le64(buffer_info->dma);
5117 } else {
5118 rx_desc->read.pkt_addr =
5119 cpu_to_le64(buffer_info->dma);
5120 rx_desc->read.hdr_addr = 0;
5121 }
5122
5123 i++;
5124 if (i == rx_ring->count)
5125 i = 0;
5126 buffer_info = &rx_ring->buffer_info[i];
5127 }
5128
5129no_buffers:
5130 if (rx_ring->next_to_use != i) {
5131 rx_ring->next_to_use = i;
5132 if (i == 0)
5133 i = (rx_ring->count - 1);
5134 else
5135 i--;
5136
5137 /* Force memory writes to complete before letting h/w
5138 * know there are new descriptors to fetch. (Only
5139 * applicable for weak-ordered memory model archs,
5140 * such as IA-64). */
5141 wmb();
5142 writel(i, adapter->hw.hw_addr + rx_ring->tail);
5143 }
5144}
5145
5146/**
5147 * igb_mii_ioctl -
5148 * @netdev:
5149 * @ifreq:
5150 * @cmd:
5151 **/
5152static int igb_mii_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
5153{
5154 struct igb_adapter *adapter = netdev_priv(netdev);
5155 struct mii_ioctl_data *data = if_mii(ifr);
5156
5157 if (adapter->hw.phy.media_type != e1000_media_type_copper)
5158 return -EOPNOTSUPP;
5159
5160 switch (cmd) {
5161 case SIOCGMIIPHY:
5162 data->phy_id = adapter->hw.phy.addr;
5163 break;
5164 case SIOCGMIIREG:
Alexander Duyckf5f4cf02008-11-21 21:30:24 -08005165 if (igb_read_phy_reg(&adapter->hw, data->reg_num & 0x1F,
5166 &data->val_out))
Auke Kok9d5c8242008-01-24 02:22:38 -08005167 return -EIO;
5168 break;
5169 case SIOCSMIIREG:
5170 default:
5171 return -EOPNOTSUPP;
5172 }
5173 return 0;
5174}
5175
5176/**
Patrick Ohlyc6cb0902009-02-12 05:03:42 +00005177 * igb_hwtstamp_ioctl - control hardware time stamping
5178 * @netdev:
5179 * @ifreq:
5180 * @cmd:
5181 *
Patrick Ohly33af6bc2009-02-12 05:03:43 +00005182 * Outgoing time stamping can be enabled and disabled. Play nice and
5183 * disable it when requested, although it shouldn't case any overhead
5184 * when no packet needs it. At most one packet in the queue may be
5185 * marked for time stamping, otherwise it would be impossible to tell
5186 * for sure to which packet the hardware time stamp belongs.
5187 *
5188 * Incoming time stamping has to be configured via the hardware
5189 * filters. Not all combinations are supported, in particular event
5190 * type has to be specified. Matching the kind of event packet is
5191 * not supported, with the exception of "all V2 events regardless of
5192 * level 2 or 4".
5193 *
Patrick Ohlyc6cb0902009-02-12 05:03:42 +00005194 **/
5195static int igb_hwtstamp_ioctl(struct net_device *netdev,
5196 struct ifreq *ifr, int cmd)
5197{
Patrick Ohly33af6bc2009-02-12 05:03:43 +00005198 struct igb_adapter *adapter = netdev_priv(netdev);
5199 struct e1000_hw *hw = &adapter->hw;
Patrick Ohlyc6cb0902009-02-12 05:03:42 +00005200 struct hwtstamp_config config;
Patrick Ohly33af6bc2009-02-12 05:03:43 +00005201 u32 tsync_tx_ctl_bit = E1000_TSYNCTXCTL_ENABLED;
5202 u32 tsync_rx_ctl_bit = E1000_TSYNCRXCTL_ENABLED;
5203 u32 tsync_rx_ctl_type = 0;
5204 u32 tsync_rx_cfg = 0;
5205 int is_l4 = 0;
5206 int is_l2 = 0;
5207 short port = 319; /* PTP */
5208 u32 regval;
Patrick Ohlyc6cb0902009-02-12 05:03:42 +00005209
5210 if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
5211 return -EFAULT;
5212
5213 /* reserved for future extensions */
5214 if (config.flags)
5215 return -EINVAL;
5216
Patrick Ohly33af6bc2009-02-12 05:03:43 +00005217 switch (config.tx_type) {
5218 case HWTSTAMP_TX_OFF:
5219 tsync_tx_ctl_bit = 0;
5220 break;
5221 case HWTSTAMP_TX_ON:
5222 tsync_tx_ctl_bit = E1000_TSYNCTXCTL_ENABLED;
5223 break;
5224 default:
5225 return -ERANGE;
5226 }
Patrick Ohlyc6cb0902009-02-12 05:03:42 +00005227
Patrick Ohly33af6bc2009-02-12 05:03:43 +00005228 switch (config.rx_filter) {
5229 case HWTSTAMP_FILTER_NONE:
5230 tsync_rx_ctl_bit = 0;
5231 break;
5232 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
5233 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
5234 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
5235 case HWTSTAMP_FILTER_ALL:
5236 /*
5237 * register TSYNCRXCFG must be set, therefore it is not
5238 * possible to time stamp both Sync and Delay_Req messages
5239 * => fall back to time stamping all packets
5240 */
5241 tsync_rx_ctl_type = E1000_TSYNCRXCTL_TYPE_ALL;
5242 config.rx_filter = HWTSTAMP_FILTER_ALL;
5243 break;
5244 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
5245 tsync_rx_ctl_type = E1000_TSYNCRXCTL_TYPE_L4_V1;
5246 tsync_rx_cfg = E1000_TSYNCRXCFG_PTP_V1_SYNC_MESSAGE;
5247 is_l4 = 1;
5248 break;
5249 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
5250 tsync_rx_ctl_type = E1000_TSYNCRXCTL_TYPE_L4_V1;
5251 tsync_rx_cfg = E1000_TSYNCRXCFG_PTP_V1_DELAY_REQ_MESSAGE;
5252 is_l4 = 1;
5253 break;
5254 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
5255 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
5256 tsync_rx_ctl_type = E1000_TSYNCRXCTL_TYPE_L2_L4_V2;
5257 tsync_rx_cfg = E1000_TSYNCRXCFG_PTP_V2_SYNC_MESSAGE;
5258 is_l2 = 1;
5259 is_l4 = 1;
5260 config.rx_filter = HWTSTAMP_FILTER_SOME;
5261 break;
5262 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
5263 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
5264 tsync_rx_ctl_type = E1000_TSYNCRXCTL_TYPE_L2_L4_V2;
5265 tsync_rx_cfg = E1000_TSYNCRXCFG_PTP_V2_DELAY_REQ_MESSAGE;
5266 is_l2 = 1;
5267 is_l4 = 1;
5268 config.rx_filter = HWTSTAMP_FILTER_SOME;
5269 break;
5270 case HWTSTAMP_FILTER_PTP_V2_EVENT:
5271 case HWTSTAMP_FILTER_PTP_V2_SYNC:
5272 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
5273 tsync_rx_ctl_type = E1000_TSYNCRXCTL_TYPE_EVENT_V2;
5274 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
5275 is_l2 = 1;
5276 break;
5277 default:
5278 return -ERANGE;
5279 }
5280
5281 /* enable/disable TX */
5282 regval = rd32(E1000_TSYNCTXCTL);
5283 regval = (regval & ~E1000_TSYNCTXCTL_ENABLED) | tsync_tx_ctl_bit;
5284 wr32(E1000_TSYNCTXCTL, regval);
5285
5286 /* enable/disable RX, define which PTP packets are time stamped */
5287 regval = rd32(E1000_TSYNCRXCTL);
5288 regval = (regval & ~E1000_TSYNCRXCTL_ENABLED) | tsync_rx_ctl_bit;
5289 regval = (regval & ~0xE) | tsync_rx_ctl_type;
5290 wr32(E1000_TSYNCRXCTL, regval);
5291 wr32(E1000_TSYNCRXCFG, tsync_rx_cfg);
5292
5293 /*
5294 * Ethertype Filter Queue Filter[0][15:0] = 0x88F7
5295 * (Ethertype to filter on)
5296 * Ethertype Filter Queue Filter[0][26] = 0x1 (Enable filter)
5297 * Ethertype Filter Queue Filter[0][30] = 0x1 (Enable Timestamping)
5298 */
5299 wr32(E1000_ETQF0, is_l2 ? 0x440088f7 : 0);
5300
5301 /* L4 Queue Filter[0]: only filter by source and destination port */
5302 wr32(E1000_SPQF0, htons(port));
5303 wr32(E1000_IMIREXT(0), is_l4 ?
5304 ((1<<12) | (1<<19) /* bypass size and control flags */) : 0);
5305 wr32(E1000_IMIR(0), is_l4 ?
5306 (htons(port)
5307 | (0<<16) /* immediate interrupt disabled */
5308 | 0 /* (1<<17) bit cleared: do not bypass
5309 destination port check */)
5310 : 0);
5311 wr32(E1000_FTQF0, is_l4 ?
5312 (0x11 /* UDP */
5313 | (1<<15) /* VF not compared */
5314 | (1<<27) /* Enable Timestamping */
5315 | (7<<28) /* only source port filter enabled,
5316 source/target address and protocol
5317 masked */)
5318 : ((1<<15) | (15<<28) /* all mask bits set = filter not
5319 enabled */));
5320
5321 wrfl();
5322
5323 adapter->hwtstamp_config = config;
5324
5325 /* clear TX/RX time stamp registers, just to be sure */
5326 regval = rd32(E1000_TXSTMPH);
5327 regval = rd32(E1000_RXSTMPH);
5328
5329 return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
5330 -EFAULT : 0;
Patrick Ohlyc6cb0902009-02-12 05:03:42 +00005331}
5332
5333/**
Auke Kok9d5c8242008-01-24 02:22:38 -08005334 * igb_ioctl -
5335 * @netdev:
5336 * @ifreq:
5337 * @cmd:
5338 **/
5339static int igb_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
5340{
5341 switch (cmd) {
5342 case SIOCGMIIPHY:
5343 case SIOCGMIIREG:
5344 case SIOCSMIIREG:
5345 return igb_mii_ioctl(netdev, ifr, cmd);
Patrick Ohlyc6cb0902009-02-12 05:03:42 +00005346 case SIOCSHWTSTAMP:
5347 return igb_hwtstamp_ioctl(netdev, ifr, cmd);
Auke Kok9d5c8242008-01-24 02:22:38 -08005348 default:
5349 return -EOPNOTSUPP;
5350 }
5351}
5352
Alexander Duyck009bc062009-07-23 18:08:35 +00005353s32 igb_read_pcie_cap_reg(struct e1000_hw *hw, u32 reg, u16 *value)
5354{
5355 struct igb_adapter *adapter = hw->back;
5356 u16 cap_offset;
5357
5358 cap_offset = pci_find_capability(adapter->pdev, PCI_CAP_ID_EXP);
5359 if (!cap_offset)
5360 return -E1000_ERR_CONFIG;
5361
5362 pci_read_config_word(adapter->pdev, cap_offset + reg, value);
5363
5364 return 0;
5365}
5366
5367s32 igb_write_pcie_cap_reg(struct e1000_hw *hw, u32 reg, u16 *value)
5368{
5369 struct igb_adapter *adapter = hw->back;
5370 u16 cap_offset;
5371
5372 cap_offset = pci_find_capability(adapter->pdev, PCI_CAP_ID_EXP);
5373 if (!cap_offset)
5374 return -E1000_ERR_CONFIG;
5375
5376 pci_write_config_word(adapter->pdev, cap_offset + reg, *value);
5377
5378 return 0;
5379}
5380
Auke Kok9d5c8242008-01-24 02:22:38 -08005381static void igb_vlan_rx_register(struct net_device *netdev,
5382 struct vlan_group *grp)
5383{
5384 struct igb_adapter *adapter = netdev_priv(netdev);
5385 struct e1000_hw *hw = &adapter->hw;
5386 u32 ctrl, rctl;
5387
5388 igb_irq_disable(adapter);
5389 adapter->vlgrp = grp;
5390
5391 if (grp) {
5392 /* enable VLAN tag insert/strip */
5393 ctrl = rd32(E1000_CTRL);
5394 ctrl |= E1000_CTRL_VME;
5395 wr32(E1000_CTRL, ctrl);
5396
5397 /* enable VLAN receive filtering */
5398 rctl = rd32(E1000_RCTL);
Auke Kok9d5c8242008-01-24 02:22:38 -08005399 rctl &= ~E1000_RCTL_CFIEN;
5400 wr32(E1000_RCTL, rctl);
5401 igb_update_mng_vlan(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08005402 } else {
5403 /* disable VLAN tag insert/strip */
5404 ctrl = rd32(E1000_CTRL);
5405 ctrl &= ~E1000_CTRL_VME;
5406 wr32(E1000_CTRL, ctrl);
5407
Auke Kok9d5c8242008-01-24 02:22:38 -08005408 if (adapter->mng_vlan_id != (u16)IGB_MNG_VLAN_NONE) {
5409 igb_vlan_rx_kill_vid(netdev, adapter->mng_vlan_id);
5410 adapter->mng_vlan_id = IGB_MNG_VLAN_NONE;
5411 }
Auke Kok9d5c8242008-01-24 02:22:38 -08005412 }
5413
Alexander Duycke1739522009-02-19 20:39:44 -08005414 igb_rlpml_set(adapter);
5415
Auke Kok9d5c8242008-01-24 02:22:38 -08005416 if (!test_bit(__IGB_DOWN, &adapter->state))
5417 igb_irq_enable(adapter);
5418}
5419
5420static void igb_vlan_rx_add_vid(struct net_device *netdev, u16 vid)
5421{
5422 struct igb_adapter *adapter = netdev_priv(netdev);
5423 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005424 int pf_id = adapter->vfs_allocated_count;
Auke Kok9d5c8242008-01-24 02:22:38 -08005425
Alexander Duyck28b07592009-02-06 23:20:31 +00005426 if ((hw->mng_cookie.status &
Auke Kok9d5c8242008-01-24 02:22:38 -08005427 E1000_MNG_DHCP_COOKIE_STATUS_VLAN) &&
5428 (vid == adapter->mng_vlan_id))
5429 return;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005430
5431 /* add vid to vlvf if sr-iov is enabled,
5432 * if that fails add directly to filter table */
5433 if (igb_vlvf_set(adapter, vid, true, pf_id))
5434 igb_vfta_set(hw, vid, true);
5435
Auke Kok9d5c8242008-01-24 02:22:38 -08005436}
5437
5438static void igb_vlan_rx_kill_vid(struct net_device *netdev, u16 vid)
5439{
5440 struct igb_adapter *adapter = netdev_priv(netdev);
5441 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005442 int pf_id = adapter->vfs_allocated_count;
Auke Kok9d5c8242008-01-24 02:22:38 -08005443
5444 igb_irq_disable(adapter);
5445 vlan_group_set_device(adapter->vlgrp, vid, NULL);
5446
5447 if (!test_bit(__IGB_DOWN, &adapter->state))
5448 igb_irq_enable(adapter);
5449
5450 if ((adapter->hw.mng_cookie.status &
5451 E1000_MNG_DHCP_COOKIE_STATUS_VLAN) &&
5452 (vid == adapter->mng_vlan_id)) {
5453 /* release control to f/w */
5454 igb_release_hw_control(adapter);
5455 return;
5456 }
5457
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005458 /* remove vid from vlvf if sr-iov is enabled,
5459 * if not in vlvf remove from vfta */
5460 if (igb_vlvf_set(adapter, vid, false, pf_id))
5461 igb_vfta_set(hw, vid, false);
Auke Kok9d5c8242008-01-24 02:22:38 -08005462}
5463
5464static void igb_restore_vlan(struct igb_adapter *adapter)
5465{
5466 igb_vlan_rx_register(adapter->netdev, adapter->vlgrp);
5467
5468 if (adapter->vlgrp) {
5469 u16 vid;
5470 for (vid = 0; vid < VLAN_GROUP_ARRAY_LEN; vid++) {
5471 if (!vlan_group_get_device(adapter->vlgrp, vid))
5472 continue;
5473 igb_vlan_rx_add_vid(adapter->netdev, vid);
5474 }
5475 }
5476}
5477
5478int igb_set_spd_dplx(struct igb_adapter *adapter, u16 spddplx)
5479{
5480 struct e1000_mac_info *mac = &adapter->hw.mac;
5481
5482 mac->autoneg = 0;
5483
Auke Kok9d5c8242008-01-24 02:22:38 -08005484 switch (spddplx) {
5485 case SPEED_10 + DUPLEX_HALF:
5486 mac->forced_speed_duplex = ADVERTISE_10_HALF;
5487 break;
5488 case SPEED_10 + DUPLEX_FULL:
5489 mac->forced_speed_duplex = ADVERTISE_10_FULL;
5490 break;
5491 case SPEED_100 + DUPLEX_HALF:
5492 mac->forced_speed_duplex = ADVERTISE_100_HALF;
5493 break;
5494 case SPEED_100 + DUPLEX_FULL:
5495 mac->forced_speed_duplex = ADVERTISE_100_FULL;
5496 break;
5497 case SPEED_1000 + DUPLEX_FULL:
5498 mac->autoneg = 1;
5499 adapter->hw.phy.autoneg_advertised = ADVERTISE_1000_FULL;
5500 break;
5501 case SPEED_1000 + DUPLEX_HALF: /* not supported */
5502 default:
5503 dev_err(&adapter->pdev->dev,
5504 "Unsupported Speed/Duplex configuration\n");
5505 return -EINVAL;
5506 }
5507 return 0;
5508}
5509
Rafael J. Wysocki3fe7c4c2009-03-31 21:23:50 +00005510static int __igb_shutdown(struct pci_dev *pdev, bool *enable_wake)
Auke Kok9d5c8242008-01-24 02:22:38 -08005511{
5512 struct net_device *netdev = pci_get_drvdata(pdev);
5513 struct igb_adapter *adapter = netdev_priv(netdev);
5514 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck2d064c02008-07-08 15:10:12 -07005515 u32 ctrl, rctl, status;
Auke Kok9d5c8242008-01-24 02:22:38 -08005516 u32 wufc = adapter->wol;
5517#ifdef CONFIG_PM
5518 int retval = 0;
5519#endif
5520
5521 netif_device_detach(netdev);
5522
Alexander Duycka88f10e2008-07-08 15:13:38 -07005523 if (netif_running(netdev))
5524 igb_close(netdev);
5525
Alexander Duyck047e0032009-10-27 15:49:27 +00005526 igb_clear_interrupt_scheme(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08005527
5528#ifdef CONFIG_PM
5529 retval = pci_save_state(pdev);
5530 if (retval)
5531 return retval;
5532#endif
5533
5534 status = rd32(E1000_STATUS);
5535 if (status & E1000_STATUS_LU)
5536 wufc &= ~E1000_WUFC_LNKC;
5537
5538 if (wufc) {
5539 igb_setup_rctl(adapter);
Alexander Duyckff41f8d2009-09-03 14:48:56 +00005540 igb_set_rx_mode(netdev);
Auke Kok9d5c8242008-01-24 02:22:38 -08005541
5542 /* turn on all-multi mode if wake on multicast is enabled */
5543 if (wufc & E1000_WUFC_MC) {
5544 rctl = rd32(E1000_RCTL);
5545 rctl |= E1000_RCTL_MPE;
5546 wr32(E1000_RCTL, rctl);
5547 }
5548
5549 ctrl = rd32(E1000_CTRL);
5550 /* advertise wake from D3Cold */
5551 #define E1000_CTRL_ADVD3WUC 0x00100000
5552 /* phy power management enable */
5553 #define E1000_CTRL_EN_PHY_PWR_MGMT 0x00200000
5554 ctrl |= E1000_CTRL_ADVD3WUC;
5555 wr32(E1000_CTRL, ctrl);
5556
Auke Kok9d5c8242008-01-24 02:22:38 -08005557 /* Allow time for pending master requests to run */
5558 igb_disable_pcie_master(&adapter->hw);
5559
5560 wr32(E1000_WUC, E1000_WUC_PME_EN);
5561 wr32(E1000_WUFC, wufc);
Auke Kok9d5c8242008-01-24 02:22:38 -08005562 } else {
5563 wr32(E1000_WUC, 0);
5564 wr32(E1000_WUFC, 0);
Auke Kok9d5c8242008-01-24 02:22:38 -08005565 }
5566
Rafael J. Wysocki3fe7c4c2009-03-31 21:23:50 +00005567 *enable_wake = wufc || adapter->en_mng_pt;
5568 if (!*enable_wake)
Alexander Duyck2fb02a22009-09-14 08:22:54 +00005569 igb_shutdown_serdes_link_82575(hw);
Auke Kok9d5c8242008-01-24 02:22:38 -08005570
5571 /* Release control of h/w to f/w. If f/w is AMT enabled, this
5572 * would have already happened in close and is redundant. */
5573 igb_release_hw_control(adapter);
5574
5575 pci_disable_device(pdev);
5576
Auke Kok9d5c8242008-01-24 02:22:38 -08005577 return 0;
5578}
5579
5580#ifdef CONFIG_PM
Rafael J. Wysocki3fe7c4c2009-03-31 21:23:50 +00005581static int igb_suspend(struct pci_dev *pdev, pm_message_t state)
5582{
5583 int retval;
5584 bool wake;
5585
5586 retval = __igb_shutdown(pdev, &wake);
5587 if (retval)
5588 return retval;
5589
5590 if (wake) {
5591 pci_prepare_to_sleep(pdev);
5592 } else {
5593 pci_wake_from_d3(pdev, false);
5594 pci_set_power_state(pdev, PCI_D3hot);
5595 }
5596
5597 return 0;
5598}
5599
Auke Kok9d5c8242008-01-24 02:22:38 -08005600static int igb_resume(struct pci_dev *pdev)
5601{
5602 struct net_device *netdev = pci_get_drvdata(pdev);
5603 struct igb_adapter *adapter = netdev_priv(netdev);
5604 struct e1000_hw *hw = &adapter->hw;
5605 u32 err;
5606
5607 pci_set_power_state(pdev, PCI_D0);
5608 pci_restore_state(pdev);
Taku Izumi42bfd33a2008-06-20 12:10:30 +09005609
Alexander Duyckaed5dec2009-02-06 23:16:04 +00005610 err = pci_enable_device_mem(pdev);
Auke Kok9d5c8242008-01-24 02:22:38 -08005611 if (err) {
5612 dev_err(&pdev->dev,
5613 "igb: Cannot enable PCI device from suspend\n");
5614 return err;
5615 }
5616 pci_set_master(pdev);
5617
5618 pci_enable_wake(pdev, PCI_D3hot, 0);
5619 pci_enable_wake(pdev, PCI_D3cold, 0);
5620
Alexander Duyck047e0032009-10-27 15:49:27 +00005621 if (igb_init_interrupt_scheme(adapter)) {
Alexander Duycka88f10e2008-07-08 15:13:38 -07005622 dev_err(&pdev->dev, "Unable to allocate memory for queues\n");
5623 return -ENOMEM;
Auke Kok9d5c8242008-01-24 02:22:38 -08005624 }
5625
5626 /* e1000_power_up_phy(adapter); */
5627
5628 igb_reset(adapter);
Alexander Duycka8564f02009-02-06 23:21:10 +00005629
5630 /* let the f/w know that the h/w is now under the control of the
5631 * driver. */
5632 igb_get_hw_control(adapter);
5633
Auke Kok9d5c8242008-01-24 02:22:38 -08005634 wr32(E1000_WUS, ~0);
5635
Alexander Duycka88f10e2008-07-08 15:13:38 -07005636 if (netif_running(netdev)) {
5637 err = igb_open(netdev);
5638 if (err)
5639 return err;
5640 }
Auke Kok9d5c8242008-01-24 02:22:38 -08005641
5642 netif_device_attach(netdev);
5643
Auke Kok9d5c8242008-01-24 02:22:38 -08005644 return 0;
5645}
5646#endif
5647
5648static void igb_shutdown(struct pci_dev *pdev)
5649{
Rafael J. Wysocki3fe7c4c2009-03-31 21:23:50 +00005650 bool wake;
5651
5652 __igb_shutdown(pdev, &wake);
5653
5654 if (system_state == SYSTEM_POWER_OFF) {
5655 pci_wake_from_d3(pdev, wake);
5656 pci_set_power_state(pdev, PCI_D3hot);
5657 }
Auke Kok9d5c8242008-01-24 02:22:38 -08005658}
5659
5660#ifdef CONFIG_NET_POLL_CONTROLLER
5661/*
5662 * Polling 'interrupt' - used by things like netconsole to send skbs
5663 * without having to re-enable interrupts. It's not called while
5664 * the interrupt routine is executing.
5665 */
5666static void igb_netpoll(struct net_device *netdev)
5667{
5668 struct igb_adapter *adapter = netdev_priv(netdev);
Alexander Duyckeebbbdb2009-02-06 23:19:29 +00005669 struct e1000_hw *hw = &adapter->hw;
Auke Kok9d5c8242008-01-24 02:22:38 -08005670 int i;
Auke Kok9d5c8242008-01-24 02:22:38 -08005671
Alexander Duyckeebbbdb2009-02-06 23:19:29 +00005672 if (!adapter->msix_entries) {
Alexander Duyck047e0032009-10-27 15:49:27 +00005673 struct igb_q_vector *q_vector = adapter->q_vector[0];
Alexander Duyckeebbbdb2009-02-06 23:19:29 +00005674 igb_irq_disable(adapter);
Alexander Duyck047e0032009-10-27 15:49:27 +00005675 napi_schedule(&q_vector->napi);
Alexander Duyckeebbbdb2009-02-06 23:19:29 +00005676 return;
5677 }
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07005678
Alexander Duyck047e0032009-10-27 15:49:27 +00005679 for (i = 0; i < adapter->num_q_vectors; i++) {
5680 struct igb_q_vector *q_vector = adapter->q_vector[i];
5681 wr32(E1000_EIMC, q_vector->eims_value);
5682 napi_schedule(&q_vector->napi);
Alexander Duyckeebbbdb2009-02-06 23:19:29 +00005683 }
Auke Kok9d5c8242008-01-24 02:22:38 -08005684}
5685#endif /* CONFIG_NET_POLL_CONTROLLER */
5686
5687/**
5688 * igb_io_error_detected - called when PCI error is detected
5689 * @pdev: Pointer to PCI device
5690 * @state: The current pci connection state
5691 *
5692 * This function is called after a PCI bus error affecting
5693 * this device has been detected.
5694 */
5695static pci_ers_result_t igb_io_error_detected(struct pci_dev *pdev,
5696 pci_channel_state_t state)
5697{
5698 struct net_device *netdev = pci_get_drvdata(pdev);
5699 struct igb_adapter *adapter = netdev_priv(netdev);
5700
5701 netif_device_detach(netdev);
5702
Alexander Duyck59ed6ee2009-06-30 12:46:34 +00005703 if (state == pci_channel_io_perm_failure)
5704 return PCI_ERS_RESULT_DISCONNECT;
5705
Auke Kok9d5c8242008-01-24 02:22:38 -08005706 if (netif_running(netdev))
5707 igb_down(adapter);
5708 pci_disable_device(pdev);
5709
5710 /* Request a slot slot reset. */
5711 return PCI_ERS_RESULT_NEED_RESET;
5712}
5713
5714/**
5715 * igb_io_slot_reset - called after the pci bus has been reset.
5716 * @pdev: Pointer to PCI device
5717 *
5718 * Restart the card from scratch, as if from a cold-boot. Implementation
5719 * resembles the first-half of the igb_resume routine.
5720 */
5721static pci_ers_result_t igb_io_slot_reset(struct pci_dev *pdev)
5722{
5723 struct net_device *netdev = pci_get_drvdata(pdev);
5724 struct igb_adapter *adapter = netdev_priv(netdev);
5725 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck40a914f2008-11-27 00:24:37 -08005726 pci_ers_result_t result;
Taku Izumi42bfd33a2008-06-20 12:10:30 +09005727 int err;
Auke Kok9d5c8242008-01-24 02:22:38 -08005728
Alexander Duyckaed5dec2009-02-06 23:16:04 +00005729 if (pci_enable_device_mem(pdev)) {
Auke Kok9d5c8242008-01-24 02:22:38 -08005730 dev_err(&pdev->dev,
5731 "Cannot re-enable PCI device after reset.\n");
Alexander Duyck40a914f2008-11-27 00:24:37 -08005732 result = PCI_ERS_RESULT_DISCONNECT;
5733 } else {
5734 pci_set_master(pdev);
5735 pci_restore_state(pdev);
5736
5737 pci_enable_wake(pdev, PCI_D3hot, 0);
5738 pci_enable_wake(pdev, PCI_D3cold, 0);
5739
5740 igb_reset(adapter);
5741 wr32(E1000_WUS, ~0);
5742 result = PCI_ERS_RESULT_RECOVERED;
Auke Kok9d5c8242008-01-24 02:22:38 -08005743 }
Auke Kok9d5c8242008-01-24 02:22:38 -08005744
Jeff Kirsherea943d42008-12-11 20:34:19 -08005745 err = pci_cleanup_aer_uncorrect_error_status(pdev);
5746 if (err) {
5747 dev_err(&pdev->dev, "pci_cleanup_aer_uncorrect_error_status "
5748 "failed 0x%0x\n", err);
5749 /* non-fatal, continue */
5750 }
Auke Kok9d5c8242008-01-24 02:22:38 -08005751
Alexander Duyck40a914f2008-11-27 00:24:37 -08005752 return result;
Auke Kok9d5c8242008-01-24 02:22:38 -08005753}
5754
5755/**
5756 * igb_io_resume - called when traffic can start flowing again.
5757 * @pdev: Pointer to PCI device
5758 *
5759 * This callback is called when the error recovery driver tells us that
5760 * its OK to resume normal operation. Implementation resembles the
5761 * second-half of the igb_resume routine.
5762 */
5763static void igb_io_resume(struct pci_dev *pdev)
5764{
5765 struct net_device *netdev = pci_get_drvdata(pdev);
5766 struct igb_adapter *adapter = netdev_priv(netdev);
5767
Auke Kok9d5c8242008-01-24 02:22:38 -08005768 if (netif_running(netdev)) {
5769 if (igb_up(adapter)) {
5770 dev_err(&pdev->dev, "igb_up failed after reset\n");
5771 return;
5772 }
5773 }
5774
5775 netif_device_attach(netdev);
5776
5777 /* let the f/w know that the h/w is now under the control of the
5778 * driver. */
5779 igb_get_hw_control(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08005780}
5781
Alexander Duyck26ad9172009-10-05 06:32:49 +00005782static void igb_rar_set_qsel(struct igb_adapter *adapter, u8 *addr, u32 index,
5783 u8 qsel)
5784{
5785 u32 rar_low, rar_high;
5786 struct e1000_hw *hw = &adapter->hw;
5787
5788 /* HW expects these in little endian so we reverse the byte order
5789 * from network order (big endian) to little endian
5790 */
5791 rar_low = ((u32) addr[0] | ((u32) addr[1] << 8) |
5792 ((u32) addr[2] << 16) | ((u32) addr[3] << 24));
5793 rar_high = ((u32) addr[4] | ((u32) addr[5] << 8));
5794
5795 /* Indicate to hardware the Address is Valid. */
5796 rar_high |= E1000_RAH_AV;
5797
5798 if (hw->mac.type == e1000_82575)
5799 rar_high |= E1000_RAH_POOL_1 * qsel;
5800 else
5801 rar_high |= E1000_RAH_POOL_1 << qsel;
5802
5803 wr32(E1000_RAL(index), rar_low);
5804 wrfl();
5805 wr32(E1000_RAH(index), rar_high);
5806 wrfl();
5807}
5808
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005809static int igb_set_vf_mac(struct igb_adapter *adapter,
5810 int vf, unsigned char *mac_addr)
5811{
5812 struct e1000_hw *hw = &adapter->hw;
Alexander Duyckff41f8d2009-09-03 14:48:56 +00005813 /* VF MAC addresses start at end of receive addresses and moves
5814 * torwards the first, as a result a collision should not be possible */
5815 int rar_entry = hw->mac.rar_entry_count - (vf + 1);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005816
Alexander Duyck37680112009-02-19 20:40:30 -08005817 memcpy(adapter->vf_data[vf].vf_mac_addresses, mac_addr, ETH_ALEN);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005818
Alexander Duyck26ad9172009-10-05 06:32:49 +00005819 igb_rar_set_qsel(adapter, mac_addr, rar_entry, vf);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005820
5821 return 0;
5822}
5823
5824static void igb_vmm_control(struct igb_adapter *adapter)
5825{
5826 struct e1000_hw *hw = &adapter->hw;
5827 u32 reg_data;
5828
5829 if (!adapter->vfs_allocated_count)
5830 return;
5831
5832 /* VF's need PF reset indication before they
5833 * can send/receive mail */
5834 reg_data = rd32(E1000_CTRL_EXT);
5835 reg_data |= E1000_CTRL_EXT_PFRSTD;
5836 wr32(E1000_CTRL_EXT, reg_data);
5837
5838 igb_vmdq_set_loopback_pf(hw, true);
5839 igb_vmdq_set_replication_pf(hw, true);
5840}
5841
Auke Kok9d5c8242008-01-24 02:22:38 -08005842/* igb_main.c */