blob: fdbe33228d62eee9f9f19d11cb3876ebaa6d0f88 [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 Duyck2d064c02008-07-08 15:10:12 -070066 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_FIBER), board_82575 },
67 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_SERDES), board_82575 },
Alexander Duyck4703bf72009-07-23 18:09:48 +000068 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_SERDES_QUAD), board_82575 },
Alexander Duyckc8ea5ea92009-03-13 20:42:35 +000069 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_QUAD_COPPER), board_82575 },
Auke Kok9d5c8242008-01-24 02:22:38 -080070 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82575EB_COPPER), board_82575 },
71 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82575EB_FIBER_SERDES), board_82575 },
72 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82575GB_QUAD_COPPER), board_82575 },
73 /* required last entry */
74 {0, }
75};
76
77MODULE_DEVICE_TABLE(pci, igb_pci_tbl);
78
79void igb_reset(struct igb_adapter *);
80static int igb_setup_all_tx_resources(struct igb_adapter *);
81static int igb_setup_all_rx_resources(struct igb_adapter *);
82static void igb_free_all_tx_resources(struct igb_adapter *);
83static void igb_free_all_rx_resources(struct igb_adapter *);
Auke Kok9d5c8242008-01-24 02:22:38 -080084void igb_update_stats(struct igb_adapter *);
85static int igb_probe(struct pci_dev *, const struct pci_device_id *);
86static void __devexit igb_remove(struct pci_dev *pdev);
87static int igb_sw_init(struct igb_adapter *);
88static int igb_open(struct net_device *);
89static int igb_close(struct net_device *);
90static void igb_configure_tx(struct igb_adapter *);
91static void igb_configure_rx(struct igb_adapter *);
92static void igb_setup_rctl(struct igb_adapter *);
93static void igb_clean_all_tx_rings(struct igb_adapter *);
94static void igb_clean_all_rx_rings(struct igb_adapter *);
Mitch Williams3b644cf2008-06-27 10:59:48 -070095static void igb_clean_tx_ring(struct igb_ring *);
96static void igb_clean_rx_ring(struct igb_ring *);
Alexander Duyckff41f8d2009-09-03 14:48:56 +000097static void igb_set_rx_mode(struct net_device *);
Auke Kok9d5c8242008-01-24 02:22:38 -080098static void igb_update_phy_info(unsigned long);
99static void igb_watchdog(unsigned long);
100static void igb_watchdog_task(struct work_struct *);
Stephen Hemminger3b29a562009-08-31 19:50:55 +0000101static netdev_tx_t igb_xmit_frame_ring_adv(struct sk_buff *,
102 struct net_device *,
103 struct igb_ring *);
104static netdev_tx_t igb_xmit_frame_adv(struct sk_buff *skb,
105 struct net_device *);
Auke Kok9d5c8242008-01-24 02:22:38 -0800106static struct net_device_stats *igb_get_stats(struct net_device *);
107static int igb_change_mtu(struct net_device *, int);
108static int igb_set_mac(struct net_device *, void *);
Alexander Duyck68d480c2009-10-05 06:33:08 +0000109static void igb_set_uta(struct igb_adapter *adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -0800110static irqreturn_t igb_intr(int irq, void *);
111static irqreturn_t igb_intr_msi(int irq, void *);
112static irqreturn_t igb_msix_other(int irq, void *);
113static irqreturn_t igb_msix_rx(int irq, void *);
114static irqreturn_t igb_msix_tx(int irq, void *);
Jeff Kirsher421e02f2008-10-17 11:08:31 -0700115#ifdef CONFIG_IGB_DCA
Jeb Cramerfe4506b2008-07-08 15:07:55 -0700116static void igb_update_rx_dca(struct igb_ring *);
117static void igb_update_tx_dca(struct igb_ring *);
118static void igb_setup_dca(struct igb_adapter *);
Jeff Kirsher421e02f2008-10-17 11:08:31 -0700119#endif /* CONFIG_IGB_DCA */
Mitch Williams3b644cf2008-06-27 10:59:48 -0700120static bool igb_clean_tx_irq(struct igb_ring *);
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -0700121static int igb_poll(struct napi_struct *, int);
Mitch Williams3b644cf2008-06-27 10:59:48 -0700122static bool igb_clean_rx_irq_adv(struct igb_ring *, int *, int);
123static 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 Duyck1bfaf072009-02-19 20:39:23 -0800376 unsigned int 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
Auke Kok9d5c8242008-01-24 02:22:38 -0800402/**
403 * igb_alloc_queues - Allocate memory for all rings
404 * @adapter: board private structure to initialize
405 *
406 * We allocate one ring per queue at run-time since we don't know the
407 * number of queues at compile-time.
408 **/
409static int igb_alloc_queues(struct igb_adapter *adapter)
410{
411 int i;
412
413 adapter->tx_ring = kcalloc(adapter->num_tx_queues,
414 sizeof(struct igb_ring), GFP_KERNEL);
415 if (!adapter->tx_ring)
416 return -ENOMEM;
417
418 adapter->rx_ring = kcalloc(adapter->num_rx_queues,
419 sizeof(struct igb_ring), GFP_KERNEL);
420 if (!adapter->rx_ring) {
421 kfree(adapter->tx_ring);
422 return -ENOMEM;
423 }
424
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -0700425 adapter->rx_ring->buddy = adapter->tx_ring;
426
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -0700427 for (i = 0; i < adapter->num_tx_queues; i++) {
428 struct igb_ring *ring = &(adapter->tx_ring[i]);
Alexander Duyck68fd9912008-11-20 00:48:10 -0800429 ring->count = adapter->tx_ring_count;
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -0700430 ring->adapter = adapter;
431 ring->queue_index = i;
432 }
Auke Kok9d5c8242008-01-24 02:22:38 -0800433 for (i = 0; i < adapter->num_rx_queues; i++) {
434 struct igb_ring *ring = &(adapter->rx_ring[i]);
Alexander Duyck68fd9912008-11-20 00:48:10 -0800435 ring->count = adapter->rx_ring_count;
Auke Kok9d5c8242008-01-24 02:22:38 -0800436 ring->adapter = adapter;
PJ Waskiewicz844290e2008-06-27 11:00:39 -0700437 ring->queue_index = i;
Auke Kok9d5c8242008-01-24 02:22:38 -0800438 ring->itr_register = E1000_ITR;
439
PJ Waskiewicz844290e2008-06-27 11:00:39 -0700440 /* set a default napi handler for each rx_ring */
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -0700441 netif_napi_add(adapter->netdev, &ring->napi, igb_poll, 64);
Auke Kok9d5c8242008-01-24 02:22:38 -0800442 }
Alexander Duyck26bc19e2008-12-26 01:34:11 -0800443
444 igb_cache_ring_register(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -0800445 return 0;
446}
447
Alexander Duycka88f10e2008-07-08 15:13:38 -0700448static void igb_free_queues(struct igb_adapter *adapter)
449{
450 int i;
451
452 for (i = 0; i < adapter->num_rx_queues; i++)
453 netif_napi_del(&adapter->rx_ring[i].napi);
454
Alexander Duyckd1a8c9e2009-03-31 20:38:19 +0000455 adapter->num_rx_queues = 0;
456 adapter->num_tx_queues = 0;
457
Alexander Duycka88f10e2008-07-08 15:13:38 -0700458 kfree(adapter->tx_ring);
459 kfree(adapter->rx_ring);
460}
461
Auke Kok9d5c8242008-01-24 02:22:38 -0800462#define IGB_N0_QUEUE -1
463static void igb_assign_vector(struct igb_adapter *adapter, int rx_queue,
464 int tx_queue, int msix_vector)
465{
466 u32 msixbm = 0;
467 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck2d064c02008-07-08 15:10:12 -0700468 u32 ivar, index;
469
470 switch (hw->mac.type) {
471 case e1000_82575:
Auke Kok9d5c8242008-01-24 02:22:38 -0800472 /* The 82575 assigns vectors using a bitmask, which matches the
473 bitmask for the EICR/EIMS/EIMC registers. To assign one
474 or more queues to a vector, we write the appropriate bits
475 into the MSIXBM register for that vector. */
476 if (rx_queue > IGB_N0_QUEUE) {
477 msixbm = E1000_EICR_RX_QUEUE0 << rx_queue;
478 adapter->rx_ring[rx_queue].eims_value = msixbm;
479 }
480 if (tx_queue > IGB_N0_QUEUE) {
481 msixbm |= E1000_EICR_TX_QUEUE0 << tx_queue;
482 adapter->tx_ring[tx_queue].eims_value =
483 E1000_EICR_TX_QUEUE0 << tx_queue;
484 }
485 array_wr32(E1000_MSIXBM(0), msix_vector, msixbm);
Alexander Duyck2d064c02008-07-08 15:10:12 -0700486 break;
487 case e1000_82576:
Alexander Duyck26bc19e2008-12-26 01:34:11 -0800488 /* 82576 uses a table-based method for assigning vectors.
Alexander Duyck2d064c02008-07-08 15:10:12 -0700489 Each queue has a single entry in the table to which we write
490 a vector number along with a "valid" bit. Sadly, the layout
491 of the table is somewhat counterintuitive. */
492 if (rx_queue > IGB_N0_QUEUE) {
Alexander Duyck1bfaf072009-02-19 20:39:23 -0800493 index = (rx_queue >> 1) + adapter->vfs_allocated_count;
Alexander Duyck2d064c02008-07-08 15:10:12 -0700494 ivar = array_rd32(E1000_IVAR0, index);
Alexander Duyck26bc19e2008-12-26 01:34:11 -0800495 if (rx_queue & 0x1) {
Alexander Duyck2d064c02008-07-08 15:10:12 -0700496 /* vector goes into third byte of register */
497 ivar = ivar & 0xFF00FFFF;
498 ivar |= (msix_vector | E1000_IVAR_VALID) << 16;
Alexander Duyck26bc19e2008-12-26 01:34:11 -0800499 } else {
500 /* vector goes into low byte of register */
501 ivar = ivar & 0xFFFFFF00;
502 ivar |= msix_vector | E1000_IVAR_VALID;
Alexander Duyck2d064c02008-07-08 15:10:12 -0700503 }
504 adapter->rx_ring[rx_queue].eims_value= 1 << msix_vector;
505 array_wr32(E1000_IVAR0, index, ivar);
506 }
507 if (tx_queue > IGB_N0_QUEUE) {
Alexander Duyck1bfaf072009-02-19 20:39:23 -0800508 index = (tx_queue >> 1) + adapter->vfs_allocated_count;
Alexander Duyck2d064c02008-07-08 15:10:12 -0700509 ivar = array_rd32(E1000_IVAR0, index);
Alexander Duyck26bc19e2008-12-26 01:34:11 -0800510 if (tx_queue & 0x1) {
Alexander Duyck2d064c02008-07-08 15:10:12 -0700511 /* vector goes into high byte of register */
512 ivar = ivar & 0x00FFFFFF;
513 ivar |= (msix_vector | E1000_IVAR_VALID) << 24;
Alexander Duyck26bc19e2008-12-26 01:34:11 -0800514 } else {
515 /* vector goes into second byte of register */
516 ivar = ivar & 0xFFFF00FF;
517 ivar |= (msix_vector | E1000_IVAR_VALID) << 8;
Alexander Duyck2d064c02008-07-08 15:10:12 -0700518 }
519 adapter->tx_ring[tx_queue].eims_value= 1 << msix_vector;
520 array_wr32(E1000_IVAR0, index, ivar);
521 }
522 break;
523 default:
524 BUG();
525 break;
526 }
Auke Kok9d5c8242008-01-24 02:22:38 -0800527}
528
529/**
530 * igb_configure_msix - Configure MSI-X hardware
531 *
532 * igb_configure_msix sets up the hardware to properly
533 * generate MSI-X interrupts.
534 **/
535static void igb_configure_msix(struct igb_adapter *adapter)
536{
537 u32 tmp;
538 int i, vector = 0;
539 struct e1000_hw *hw = &adapter->hw;
540
541 adapter->eims_enable_mask = 0;
Alexander Duyck2d064c02008-07-08 15:10:12 -0700542 if (hw->mac.type == e1000_82576)
543 /* Turn on MSI-X capability first, or our settings
544 * won't stick. And it will take days to debug. */
545 wr32(E1000_GPIE, E1000_GPIE_MSIX_MODE |
Alexander Duyckeebbbdb2009-02-06 23:19:29 +0000546 E1000_GPIE_PBA | E1000_GPIE_EIAME |
Alexander Duyck2d064c02008-07-08 15:10:12 -0700547 E1000_GPIE_NSICR);
Auke Kok9d5c8242008-01-24 02:22:38 -0800548
549 for (i = 0; i < adapter->num_tx_queues; i++) {
550 struct igb_ring *tx_ring = &adapter->tx_ring[i];
551 igb_assign_vector(adapter, IGB_N0_QUEUE, i, vector++);
552 adapter->eims_enable_mask |= tx_ring->eims_value;
553 if (tx_ring->itr_val)
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -0700554 writel(tx_ring->itr_val,
Auke Kok9d5c8242008-01-24 02:22:38 -0800555 hw->hw_addr + tx_ring->itr_register);
556 else
557 writel(1, hw->hw_addr + tx_ring->itr_register);
558 }
559
560 for (i = 0; i < adapter->num_rx_queues; i++) {
561 struct igb_ring *rx_ring = &adapter->rx_ring[i];
Harvey Harrison25ac3c22008-07-16 12:45:27 -0700562 rx_ring->buddy = NULL;
Auke Kok9d5c8242008-01-24 02:22:38 -0800563 igb_assign_vector(adapter, i, IGB_N0_QUEUE, vector++);
564 adapter->eims_enable_mask |= rx_ring->eims_value;
565 if (rx_ring->itr_val)
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -0700566 writel(rx_ring->itr_val,
Auke Kok9d5c8242008-01-24 02:22:38 -0800567 hw->hw_addr + rx_ring->itr_register);
568 else
569 writel(1, hw->hw_addr + rx_ring->itr_register);
570 }
571
572
573 /* set vector for other causes, i.e. link changes */
Alexander Duyck2d064c02008-07-08 15:10:12 -0700574 switch (hw->mac.type) {
575 case e1000_82575:
Auke Kok9d5c8242008-01-24 02:22:38 -0800576 array_wr32(E1000_MSIXBM(0), vector++,
577 E1000_EIMS_OTHER);
578
Auke Kok9d5c8242008-01-24 02:22:38 -0800579 tmp = rd32(E1000_CTRL_EXT);
580 /* enable MSI-X PBA support*/
581 tmp |= E1000_CTRL_EXT_PBA_CLR;
582
583 /* Auto-Mask interrupts upon ICR read. */
584 tmp |= E1000_CTRL_EXT_EIAME;
585 tmp |= E1000_CTRL_EXT_IRCA;
586
587 wr32(E1000_CTRL_EXT, tmp);
588 adapter->eims_enable_mask |= E1000_EIMS_OTHER;
PJ Waskiewicz844290e2008-06-27 11:00:39 -0700589 adapter->eims_other = E1000_EIMS_OTHER;
Auke Kok9d5c8242008-01-24 02:22:38 -0800590
Alexander Duyck2d064c02008-07-08 15:10:12 -0700591 break;
592
593 case e1000_82576:
594 tmp = (vector++ | E1000_IVAR_VALID) << 8;
595 wr32(E1000_IVAR_MISC, tmp);
596
597 adapter->eims_enable_mask = (1 << (vector)) - 1;
598 adapter->eims_other = 1 << (vector - 1);
599 break;
600 default:
601 /* do nothing, since nothing else supports MSI-X */
602 break;
603 } /* switch (hw->mac.type) */
Auke Kok9d5c8242008-01-24 02:22:38 -0800604 wrfl();
605}
606
607/**
608 * igb_request_msix - Initialize MSI-X interrupts
609 *
610 * igb_request_msix allocates MSI-X vectors and requests interrupts from the
611 * kernel.
612 **/
613static int igb_request_msix(struct igb_adapter *adapter)
614{
615 struct net_device *netdev = adapter->netdev;
616 int i, err = 0, vector = 0;
617
618 vector = 0;
619
620 for (i = 0; i < adapter->num_tx_queues; i++) {
621 struct igb_ring *ring = &(adapter->tx_ring[i]);
Alexander Duyckcb7b48f2008-12-05 15:08:03 -0800622 sprintf(ring->name, "%s-tx-%d", netdev->name, i);
Auke Kok9d5c8242008-01-24 02:22:38 -0800623 err = request_irq(adapter->msix_entries[vector].vector,
624 &igb_msix_tx, 0, ring->name,
625 &(adapter->tx_ring[i]));
626 if (err)
627 goto out;
628 ring->itr_register = E1000_EITR(0) + (vector << 2);
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -0700629 ring->itr_val = 976; /* ~4000 ints/sec */
Auke Kok9d5c8242008-01-24 02:22:38 -0800630 vector++;
631 }
632 for (i = 0; i < adapter->num_rx_queues; i++) {
633 struct igb_ring *ring = &(adapter->rx_ring[i]);
634 if (strlen(netdev->name) < (IFNAMSIZ - 5))
Alexander Duyckcb7b48f2008-12-05 15:08:03 -0800635 sprintf(ring->name, "%s-rx-%d", netdev->name, i);
Auke Kok9d5c8242008-01-24 02:22:38 -0800636 else
637 memcpy(ring->name, netdev->name, IFNAMSIZ);
638 err = request_irq(adapter->msix_entries[vector].vector,
639 &igb_msix_rx, 0, ring->name,
640 &(adapter->rx_ring[i]));
641 if (err)
642 goto out;
643 ring->itr_register = E1000_EITR(0) + (vector << 2);
644 ring->itr_val = adapter->itr;
645 vector++;
646 }
647
648 err = request_irq(adapter->msix_entries[vector].vector,
649 &igb_msix_other, 0, netdev->name, netdev);
650 if (err)
651 goto out;
652
Auke Kok9d5c8242008-01-24 02:22:38 -0800653 igb_configure_msix(adapter);
654 return 0;
655out:
656 return err;
657}
658
659static void igb_reset_interrupt_capability(struct igb_adapter *adapter)
660{
661 if (adapter->msix_entries) {
662 pci_disable_msix(adapter->pdev);
663 kfree(adapter->msix_entries);
664 adapter->msix_entries = NULL;
Alexander Duyck7dfc16f2008-07-08 15:10:46 -0700665 } else if (adapter->flags & IGB_FLAG_HAS_MSI)
Auke Kok9d5c8242008-01-24 02:22:38 -0800666 pci_disable_msi(adapter->pdev);
667 return;
668}
669
670
671/**
672 * igb_set_interrupt_capability - set MSI or MSI-X if supported
673 *
674 * Attempt to configure interrupts using the best available
675 * capabilities of the hardware and kernel.
676 **/
677static void igb_set_interrupt_capability(struct igb_adapter *adapter)
678{
679 int err;
680 int numvecs, i;
681
Alexander Duyck83b71802009-02-06 23:15:45 +0000682 /* Number of supported queues. */
683 /* Having more queues than CPUs doesn't make sense. */
684 adapter->num_rx_queues = min_t(u32, IGB_MAX_RX_QUEUES, num_online_cpus());
685 adapter->num_tx_queues = min_t(u32, IGB_MAX_TX_QUEUES, num_online_cpus());
686
Auke Kok9d5c8242008-01-24 02:22:38 -0800687 numvecs = adapter->num_tx_queues + adapter->num_rx_queues + 1;
688 adapter->msix_entries = kcalloc(numvecs, sizeof(struct msix_entry),
689 GFP_KERNEL);
690 if (!adapter->msix_entries)
691 goto msi_only;
692
693 for (i = 0; i < numvecs; i++)
694 adapter->msix_entries[i].entry = i;
695
696 err = pci_enable_msix(adapter->pdev,
697 adapter->msix_entries,
698 numvecs);
699 if (err == 0)
Alexander Duyck34a20e82008-08-26 04:25:13 -0700700 goto out;
Auke Kok9d5c8242008-01-24 02:22:38 -0800701
702 igb_reset_interrupt_capability(adapter);
703
704 /* If we can't do MSI-X, try MSI */
705msi_only:
Alexander Duyck2a3abf62009-04-07 14:37:52 +0000706#ifdef CONFIG_PCI_IOV
707 /* disable SR-IOV for non MSI-X configurations */
708 if (adapter->vf_data) {
709 struct e1000_hw *hw = &adapter->hw;
710 /* disable iov and allow time for transactions to clear */
711 pci_disable_sriov(adapter->pdev);
712 msleep(500);
713
714 kfree(adapter->vf_data);
715 adapter->vf_data = NULL;
716 wr32(E1000_IOVCTL, E1000_IOVCTL_REUSE_VFQ);
717 msleep(100);
718 dev_info(&adapter->pdev->dev, "IOV Disabled\n");
719 }
720#endif
Auke Kok9d5c8242008-01-24 02:22:38 -0800721 adapter->num_rx_queues = 1;
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -0700722 adapter->num_tx_queues = 1;
Auke Kok9d5c8242008-01-24 02:22:38 -0800723 if (!pci_enable_msi(adapter->pdev))
Alexander Duyck7dfc16f2008-07-08 15:10:46 -0700724 adapter->flags |= IGB_FLAG_HAS_MSI;
Alexander Duyck34a20e82008-08-26 04:25:13 -0700725out:
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -0700726 /* Notify the stack of the (possibly) reduced Tx Queue count. */
David S. Millerfd2ea0a2008-07-17 01:56:23 -0700727 adapter->netdev->real_num_tx_queues = adapter->num_tx_queues;
Auke Kok9d5c8242008-01-24 02:22:38 -0800728 return;
729}
730
731/**
732 * igb_request_irq - initialize interrupts
733 *
734 * Attempts to configure interrupts using the best available
735 * capabilities of the hardware and kernel.
736 **/
737static int igb_request_irq(struct igb_adapter *adapter)
738{
739 struct net_device *netdev = adapter->netdev;
740 struct e1000_hw *hw = &adapter->hw;
741 int err = 0;
742
743 if (adapter->msix_entries) {
744 err = igb_request_msix(adapter);
PJ Waskiewicz844290e2008-06-27 11:00:39 -0700745 if (!err)
Auke Kok9d5c8242008-01-24 02:22:38 -0800746 goto request_done;
Auke Kok9d5c8242008-01-24 02:22:38 -0800747 /* fall back to MSI */
748 igb_reset_interrupt_capability(adapter);
749 if (!pci_enable_msi(adapter->pdev))
Alexander Duyck7dfc16f2008-07-08 15:10:46 -0700750 adapter->flags |= IGB_FLAG_HAS_MSI;
Auke Kok9d5c8242008-01-24 02:22:38 -0800751 igb_free_all_tx_resources(adapter);
752 igb_free_all_rx_resources(adapter);
753 adapter->num_rx_queues = 1;
754 igb_alloc_queues(adapter);
PJ Waskiewicz844290e2008-06-27 11:00:39 -0700755 } else {
Alexander Duyck2d064c02008-07-08 15:10:12 -0700756 switch (hw->mac.type) {
757 case e1000_82575:
758 wr32(E1000_MSIXBM(0),
759 (E1000_EICR_RX_QUEUE0 | E1000_EIMS_OTHER));
760 break;
761 case e1000_82576:
762 wr32(E1000_IVAR0, E1000_IVAR_VALID);
763 break;
764 default:
765 break;
766 }
Auke Kok9d5c8242008-01-24 02:22:38 -0800767 }
PJ Waskiewicz844290e2008-06-27 11:00:39 -0700768
Alexander Duyck7dfc16f2008-07-08 15:10:46 -0700769 if (adapter->flags & IGB_FLAG_HAS_MSI) {
Auke Kok9d5c8242008-01-24 02:22:38 -0800770 err = request_irq(adapter->pdev->irq, &igb_intr_msi, 0,
771 netdev->name, netdev);
772 if (!err)
773 goto request_done;
774 /* fall back to legacy interrupts */
775 igb_reset_interrupt_capability(adapter);
Alexander Duyck7dfc16f2008-07-08 15:10:46 -0700776 adapter->flags &= ~IGB_FLAG_HAS_MSI;
Auke Kok9d5c8242008-01-24 02:22:38 -0800777 }
778
779 err = request_irq(adapter->pdev->irq, &igb_intr, IRQF_SHARED,
780 netdev->name, netdev);
781
Andy Gospodarek6cb5e572008-02-15 14:05:25 -0800782 if (err)
Auke Kok9d5c8242008-01-24 02:22:38 -0800783 dev_err(&adapter->pdev->dev, "Error %d getting interrupt\n",
784 err);
Auke Kok9d5c8242008-01-24 02:22:38 -0800785
786request_done:
787 return err;
788}
789
790static void igb_free_irq(struct igb_adapter *adapter)
791{
792 struct net_device *netdev = adapter->netdev;
793
794 if (adapter->msix_entries) {
795 int vector = 0, i;
796
797 for (i = 0; i < adapter->num_tx_queues; i++)
798 free_irq(adapter->msix_entries[vector++].vector,
799 &(adapter->tx_ring[i]));
800 for (i = 0; i < adapter->num_rx_queues; i++)
801 free_irq(adapter->msix_entries[vector++].vector,
802 &(adapter->rx_ring[i]));
803
804 free_irq(adapter->msix_entries[vector++].vector, netdev);
805 return;
806 }
807
808 free_irq(adapter->pdev->irq, netdev);
809}
810
811/**
812 * igb_irq_disable - Mask off interrupt generation on the NIC
813 * @adapter: board private structure
814 **/
815static void igb_irq_disable(struct igb_adapter *adapter)
816{
817 struct e1000_hw *hw = &adapter->hw;
818
819 if (adapter->msix_entries) {
Alexander Duyck2dfd1212009-09-03 14:49:15 +0000820 u32 regval = rd32(E1000_EIAM);
821 wr32(E1000_EIAM, regval & ~adapter->eims_enable_mask);
822 wr32(E1000_EIMC, adapter->eims_enable_mask);
823 regval = rd32(E1000_EIAC);
824 wr32(E1000_EIAC, regval & ~adapter->eims_enable_mask);
Auke Kok9d5c8242008-01-24 02:22:38 -0800825 }
PJ Waskiewicz844290e2008-06-27 11:00:39 -0700826
827 wr32(E1000_IAM, 0);
Auke Kok9d5c8242008-01-24 02:22:38 -0800828 wr32(E1000_IMC, ~0);
829 wrfl();
830 synchronize_irq(adapter->pdev->irq);
831}
832
833/**
834 * igb_irq_enable - Enable default interrupt generation settings
835 * @adapter: board private structure
836 **/
837static void igb_irq_enable(struct igb_adapter *adapter)
838{
839 struct e1000_hw *hw = &adapter->hw;
840
841 if (adapter->msix_entries) {
Alexander Duyck2dfd1212009-09-03 14:49:15 +0000842 u32 regval = rd32(E1000_EIAC);
843 wr32(E1000_EIAC, regval | adapter->eims_enable_mask);
844 regval = rd32(E1000_EIAM);
845 wr32(E1000_EIAM, regval | adapter->eims_enable_mask);
PJ Waskiewicz844290e2008-06-27 11:00:39 -0700846 wr32(E1000_EIMS, adapter->eims_enable_mask);
Alexander Duyck4ae196d2009-02-19 20:40:07 -0800847 if (adapter->vfs_allocated_count)
848 wr32(E1000_MBVFIMR, 0xFF);
849 wr32(E1000_IMS, (E1000_IMS_LSC | E1000_IMS_VMMB |
850 E1000_IMS_DOUTSYNC));
PJ Waskiewicz844290e2008-06-27 11:00:39 -0700851 } else {
852 wr32(E1000_IMS, IMS_ENABLE_MASK);
853 wr32(E1000_IAM, IMS_ENABLE_MASK);
854 }
Auke Kok9d5c8242008-01-24 02:22:38 -0800855}
856
857static void igb_update_mng_vlan(struct igb_adapter *adapter)
858{
859 struct net_device *netdev = adapter->netdev;
860 u16 vid = adapter->hw.mng_cookie.vlan_id;
861 u16 old_vid = adapter->mng_vlan_id;
862 if (adapter->vlgrp) {
863 if (!vlan_group_get_device(adapter->vlgrp, vid)) {
864 if (adapter->hw.mng_cookie.status &
865 E1000_MNG_DHCP_COOKIE_STATUS_VLAN) {
866 igb_vlan_rx_add_vid(netdev, vid);
867 adapter->mng_vlan_id = vid;
868 } else
869 adapter->mng_vlan_id = IGB_MNG_VLAN_NONE;
870
871 if ((old_vid != (u16)IGB_MNG_VLAN_NONE) &&
872 (vid != old_vid) &&
873 !vlan_group_get_device(adapter->vlgrp, old_vid))
874 igb_vlan_rx_kill_vid(netdev, old_vid);
875 } else
876 adapter->mng_vlan_id = vid;
877 }
878}
879
880/**
881 * igb_release_hw_control - release control of the h/w to f/w
882 * @adapter: address of board private structure
883 *
884 * igb_release_hw_control resets CTRL_EXT:DRV_LOAD bit.
885 * For ASF and Pass Through versions of f/w this means that the
886 * driver is no longer loaded.
887 *
888 **/
889static void igb_release_hw_control(struct igb_adapter *adapter)
890{
891 struct e1000_hw *hw = &adapter->hw;
892 u32 ctrl_ext;
893
894 /* Let firmware take over control of h/w */
895 ctrl_ext = rd32(E1000_CTRL_EXT);
896 wr32(E1000_CTRL_EXT,
897 ctrl_ext & ~E1000_CTRL_EXT_DRV_LOAD);
898}
899
900
901/**
902 * igb_get_hw_control - get control of the h/w from f/w
903 * @adapter: address of board private structure
904 *
905 * igb_get_hw_control sets CTRL_EXT:DRV_LOAD bit.
906 * For ASF and Pass Through versions of f/w this means that
907 * the driver is loaded.
908 *
909 **/
910static void igb_get_hw_control(struct igb_adapter *adapter)
911{
912 struct e1000_hw *hw = &adapter->hw;
913 u32 ctrl_ext;
914
915 /* Let firmware know the driver has taken over */
916 ctrl_ext = rd32(E1000_CTRL_EXT);
917 wr32(E1000_CTRL_EXT,
918 ctrl_ext | E1000_CTRL_EXT_DRV_LOAD);
919}
920
Auke Kok9d5c8242008-01-24 02:22:38 -0800921/**
922 * igb_configure - configure the hardware for RX and TX
923 * @adapter: private board structure
924 **/
925static void igb_configure(struct igb_adapter *adapter)
926{
927 struct net_device *netdev = adapter->netdev;
928 int i;
929
930 igb_get_hw_control(adapter);
Alexander Duyckff41f8d2009-09-03 14:48:56 +0000931 igb_set_rx_mode(netdev);
Auke Kok9d5c8242008-01-24 02:22:38 -0800932
933 igb_restore_vlan(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -0800934
935 igb_configure_tx(adapter);
936 igb_setup_rctl(adapter);
937 igb_configure_rx(adapter);
Alexander Duyck662d7202008-06-27 11:00:29 -0700938
939 igb_rx_fifo_flush_82575(&adapter->hw);
940
Alexander Duyckc493ea42009-03-20 00:16:50 +0000941 /* call igb_desc_unused which always leaves
Auke Kok9d5c8242008-01-24 02:22:38 -0800942 * at least 1 descriptor unused to make sure
943 * next_to_use != next_to_clean */
944 for (i = 0; i < adapter->num_rx_queues; i++) {
945 struct igb_ring *ring = &adapter->rx_ring[i];
Alexander Duyckc493ea42009-03-20 00:16:50 +0000946 igb_alloc_rx_buffers_adv(ring, igb_desc_unused(ring));
Auke Kok9d5c8242008-01-24 02:22:38 -0800947 }
948
949
950 adapter->tx_queue_len = netdev->tx_queue_len;
951}
952
953
954/**
955 * igb_up - Open the interface and prepare it to handle traffic
956 * @adapter: board private structure
957 **/
958
959int igb_up(struct igb_adapter *adapter)
960{
961 struct e1000_hw *hw = &adapter->hw;
962 int i;
963
964 /* hardware has been reset, we need to reload some things */
965 igb_configure(adapter);
966
967 clear_bit(__IGB_DOWN, &adapter->state);
968
PJ Waskiewicz844290e2008-06-27 11:00:39 -0700969 for (i = 0; i < adapter->num_rx_queues; i++)
970 napi_enable(&adapter->rx_ring[i].napi);
971 if (adapter->msix_entries)
Auke Kok9d5c8242008-01-24 02:22:38 -0800972 igb_configure_msix(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -0800973
Alexander Duyck4ae196d2009-02-19 20:40:07 -0800974 igb_vmm_control(adapter);
Alexander Duycke1739522009-02-19 20:39:44 -0800975 igb_set_vmolr(hw, adapter->vfs_allocated_count);
976
Auke Kok9d5c8242008-01-24 02:22:38 -0800977 /* Clear any pending interrupts. */
978 rd32(E1000_ICR);
979 igb_irq_enable(adapter);
980
Jesse Brandeburg4cb9be72009-04-21 18:42:05 +0000981 netif_tx_start_all_queues(adapter->netdev);
982
Auke Kok9d5c8242008-01-24 02:22:38 -0800983 /* Fire a link change interrupt to start the watchdog. */
984 wr32(E1000_ICS, E1000_ICS_LSC);
985 return 0;
986}
987
988void igb_down(struct igb_adapter *adapter)
989{
990 struct e1000_hw *hw = &adapter->hw;
991 struct net_device *netdev = adapter->netdev;
992 u32 tctl, rctl;
993 int i;
994
995 /* signal that we're down so the interrupt handler does not
996 * reschedule our watchdog timer */
997 set_bit(__IGB_DOWN, &adapter->state);
998
999 /* disable receives in the hardware */
1000 rctl = rd32(E1000_RCTL);
1001 wr32(E1000_RCTL, rctl & ~E1000_RCTL_EN);
1002 /* flush and sleep below */
1003
David S. Millerfd2ea0a2008-07-17 01:56:23 -07001004 netif_tx_stop_all_queues(netdev);
Auke Kok9d5c8242008-01-24 02:22:38 -08001005
1006 /* disable transmits in the hardware */
1007 tctl = rd32(E1000_TCTL);
1008 tctl &= ~E1000_TCTL_EN;
1009 wr32(E1000_TCTL, tctl);
1010 /* flush both disables and wait for them to finish */
1011 wrfl();
1012 msleep(10);
1013
PJ Waskiewicz844290e2008-06-27 11:00:39 -07001014 for (i = 0; i < adapter->num_rx_queues; i++)
1015 napi_disable(&adapter->rx_ring[i].napi);
Auke Kok9d5c8242008-01-24 02:22:38 -08001016
Auke Kok9d5c8242008-01-24 02:22:38 -08001017 igb_irq_disable(adapter);
1018
1019 del_timer_sync(&adapter->watchdog_timer);
1020 del_timer_sync(&adapter->phy_info_timer);
1021
1022 netdev->tx_queue_len = adapter->tx_queue_len;
1023 netif_carrier_off(netdev);
Alexander Duyck04fe6352009-02-06 23:22:32 +00001024
1025 /* record the stats before reset*/
1026 igb_update_stats(adapter);
1027
Auke Kok9d5c8242008-01-24 02:22:38 -08001028 adapter->link_speed = 0;
1029 adapter->link_duplex = 0;
1030
Jeff Kirsher30236822008-06-24 17:01:15 -07001031 if (!pci_channel_offline(adapter->pdev))
1032 igb_reset(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08001033 igb_clean_all_tx_rings(adapter);
1034 igb_clean_all_rx_rings(adapter);
Alexander Duyck7e0e99e2009-05-21 13:06:56 +00001035#ifdef CONFIG_IGB_DCA
1036
1037 /* since we reset the hardware DCA settings were cleared */
1038 igb_setup_dca(adapter);
1039#endif
Auke Kok9d5c8242008-01-24 02:22:38 -08001040}
1041
1042void igb_reinit_locked(struct igb_adapter *adapter)
1043{
1044 WARN_ON(in_interrupt());
1045 while (test_and_set_bit(__IGB_RESETTING, &adapter->state))
1046 msleep(1);
1047 igb_down(adapter);
1048 igb_up(adapter);
1049 clear_bit(__IGB_RESETTING, &adapter->state);
1050}
1051
1052void igb_reset(struct igb_adapter *adapter)
1053{
1054 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck2d064c02008-07-08 15:10:12 -07001055 struct e1000_mac_info *mac = &hw->mac;
1056 struct e1000_fc_info *fc = &hw->fc;
Auke Kok9d5c8242008-01-24 02:22:38 -08001057 u32 pba = 0, tx_space, min_tx_space, min_rx_space;
1058 u16 hwm;
1059
1060 /* Repartition Pba for greater than 9k mtu
1061 * To take effect CTRL.RST is required.
1062 */
Alexander Duyckfa4dfae2009-02-06 23:21:31 +00001063 switch (mac->type) {
1064 case e1000_82576:
Alexander Duyck2d064c02008-07-08 15:10:12 -07001065 pba = E1000_PBA_64K;
Alexander Duyckfa4dfae2009-02-06 23:21:31 +00001066 break;
1067 case e1000_82575:
1068 default:
1069 pba = E1000_PBA_34K;
1070 break;
Alexander Duyck2d064c02008-07-08 15:10:12 -07001071 }
Auke Kok9d5c8242008-01-24 02:22:38 -08001072
Alexander Duyck2d064c02008-07-08 15:10:12 -07001073 if ((adapter->max_frame_size > ETH_FRAME_LEN + ETH_FCS_LEN) &&
1074 (mac->type < e1000_82576)) {
Auke Kok9d5c8242008-01-24 02:22:38 -08001075 /* adjust PBA for jumbo frames */
1076 wr32(E1000_PBA, pba);
1077
1078 /* To maintain wire speed transmits, the Tx FIFO should be
1079 * large enough to accommodate two full transmit packets,
1080 * rounded up to the next 1KB and expressed in KB. Likewise,
1081 * the Rx FIFO should be large enough to accommodate at least
1082 * one full receive packet and is similarly rounded up and
1083 * expressed in KB. */
1084 pba = rd32(E1000_PBA);
1085 /* upper 16 bits has Tx packet buffer allocation size in KB */
1086 tx_space = pba >> 16;
1087 /* lower 16 bits has Rx packet buffer allocation size in KB */
1088 pba &= 0xffff;
1089 /* the tx fifo also stores 16 bytes of information about the tx
1090 * but don't include ethernet FCS because hardware appends it */
1091 min_tx_space = (adapter->max_frame_size +
Alexander Duyck85e8d002009-02-16 00:00:20 -08001092 sizeof(union e1000_adv_tx_desc) -
Auke Kok9d5c8242008-01-24 02:22:38 -08001093 ETH_FCS_LEN) * 2;
1094 min_tx_space = ALIGN(min_tx_space, 1024);
1095 min_tx_space >>= 10;
1096 /* software strips receive CRC, so leave room for it */
1097 min_rx_space = adapter->max_frame_size;
1098 min_rx_space = ALIGN(min_rx_space, 1024);
1099 min_rx_space >>= 10;
1100
1101 /* If current Tx allocation is less than the min Tx FIFO size,
1102 * and the min Tx FIFO size is less than the current Rx FIFO
1103 * allocation, take space away from current Rx allocation */
1104 if (tx_space < min_tx_space &&
1105 ((min_tx_space - tx_space) < pba)) {
1106 pba = pba - (min_tx_space - tx_space);
1107
1108 /* if short on rx space, rx wins and must trump tx
1109 * adjustment */
1110 if (pba < min_rx_space)
1111 pba = min_rx_space;
1112 }
Alexander Duyck2d064c02008-07-08 15:10:12 -07001113 wr32(E1000_PBA, pba);
Auke Kok9d5c8242008-01-24 02:22:38 -08001114 }
Auke Kok9d5c8242008-01-24 02:22:38 -08001115
1116 /* flow control settings */
1117 /* The high water mark must be low enough to fit one full frame
1118 * (or the size used for early receive) above it in the Rx FIFO.
1119 * Set it to the lower of:
1120 * - 90% of the Rx FIFO size, or
1121 * - the full Rx FIFO size minus one full frame */
1122 hwm = min(((pba << 10) * 9 / 10),
Alexander Duyck2d064c02008-07-08 15:10:12 -07001123 ((pba << 10) - 2 * adapter->max_frame_size));
Auke Kok9d5c8242008-01-24 02:22:38 -08001124
Alexander Duyck2d064c02008-07-08 15:10:12 -07001125 if (mac->type < e1000_82576) {
1126 fc->high_water = hwm & 0xFFF8; /* 8-byte granularity */
1127 fc->low_water = fc->high_water - 8;
1128 } else {
1129 fc->high_water = hwm & 0xFFF0; /* 16-byte granularity */
1130 fc->low_water = fc->high_water - 16;
1131 }
Auke Kok9d5c8242008-01-24 02:22:38 -08001132 fc->pause_time = 0xFFFF;
1133 fc->send_xon = 1;
Alexander Duyck0cce1192009-07-23 18:10:24 +00001134 fc->current_mode = fc->requested_mode;
Auke Kok9d5c8242008-01-24 02:22:38 -08001135
Alexander Duyck4ae196d2009-02-19 20:40:07 -08001136 /* disable receive for all VFs and wait one second */
1137 if (adapter->vfs_allocated_count) {
1138 int i;
1139 for (i = 0 ; i < adapter->vfs_allocated_count; i++)
1140 adapter->vf_data[i].clear_to_send = false;
1141
1142 /* ping all the active vfs to let them know we are going down */
1143 igb_ping_all_vfs(adapter);
1144
1145 /* disable transmits and receives */
1146 wr32(E1000_VFRE, 0);
1147 wr32(E1000_VFTE, 0);
1148 }
1149
Auke Kok9d5c8242008-01-24 02:22:38 -08001150 /* Allow time for pending master requests to run */
1151 adapter->hw.mac.ops.reset_hw(&adapter->hw);
1152 wr32(E1000_WUC, 0);
1153
1154 if (adapter->hw.mac.ops.init_hw(&adapter->hw))
1155 dev_err(&adapter->pdev->dev, "Hardware Error\n");
1156
1157 igb_update_mng_vlan(adapter);
1158
1159 /* Enable h/w to recognize an 802.1Q VLAN Ethernet packet */
1160 wr32(E1000_VET, ETHERNET_IEEE_VLAN_TYPE);
1161
1162 igb_reset_adaptive(&adapter->hw);
Alexander Duyckf5f4cf02008-11-21 21:30:24 -08001163 igb_get_phy_info(&adapter->hw);
Auke Kok9d5c8242008-01-24 02:22:38 -08001164}
1165
Stephen Hemminger2e5c6922008-11-19 22:20:44 -08001166static const struct net_device_ops igb_netdev_ops = {
1167 .ndo_open = igb_open,
1168 .ndo_stop = igb_close,
Stephen Hemminger00829822008-11-20 20:14:53 -08001169 .ndo_start_xmit = igb_xmit_frame_adv,
Stephen Hemminger2e5c6922008-11-19 22:20:44 -08001170 .ndo_get_stats = igb_get_stats,
Alexander Duyckff41f8d2009-09-03 14:48:56 +00001171 .ndo_set_rx_mode = igb_set_rx_mode,
1172 .ndo_set_multicast_list = igb_set_rx_mode,
Stephen Hemminger2e5c6922008-11-19 22:20:44 -08001173 .ndo_set_mac_address = igb_set_mac,
1174 .ndo_change_mtu = igb_change_mtu,
1175 .ndo_do_ioctl = igb_ioctl,
1176 .ndo_tx_timeout = igb_tx_timeout,
1177 .ndo_validate_addr = eth_validate_addr,
1178 .ndo_vlan_rx_register = igb_vlan_rx_register,
1179 .ndo_vlan_rx_add_vid = igb_vlan_rx_add_vid,
1180 .ndo_vlan_rx_kill_vid = igb_vlan_rx_kill_vid,
1181#ifdef CONFIG_NET_POLL_CONTROLLER
1182 .ndo_poll_controller = igb_netpoll,
1183#endif
1184};
1185
Taku Izumi42bfd33a2008-06-20 12:10:30 +09001186/**
Auke Kok9d5c8242008-01-24 02:22:38 -08001187 * igb_probe - Device Initialization Routine
1188 * @pdev: PCI device information struct
1189 * @ent: entry in igb_pci_tbl
1190 *
1191 * Returns 0 on success, negative on failure
1192 *
1193 * igb_probe initializes an adapter identified by a pci_dev structure.
1194 * The OS initialization, configuring of the adapter private structure,
1195 * and a hardware reset occur.
1196 **/
1197static int __devinit igb_probe(struct pci_dev *pdev,
1198 const struct pci_device_id *ent)
1199{
1200 struct net_device *netdev;
1201 struct igb_adapter *adapter;
1202 struct e1000_hw *hw;
1203 const struct e1000_info *ei = igb_info_tbl[ent->driver_data];
1204 unsigned long mmio_start, mmio_len;
David S. Miller2d6a5e92009-03-17 15:01:30 -07001205 int err, pci_using_dac;
Alexander Duyck682337f2009-03-14 22:26:40 -07001206 u16 eeprom_data = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08001207 u16 eeprom_apme_mask = IGB_EEPROM_APME;
1208 u32 part_num;
1209
Alexander Duyckaed5dec2009-02-06 23:16:04 +00001210 err = pci_enable_device_mem(pdev);
Auke Kok9d5c8242008-01-24 02:22:38 -08001211 if (err)
1212 return err;
1213
1214 pci_using_dac = 0;
Yang Hongyang6a355282009-04-06 19:01:13 -07001215 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
Auke Kok9d5c8242008-01-24 02:22:38 -08001216 if (!err) {
Yang Hongyang6a355282009-04-06 19:01:13 -07001217 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
Auke Kok9d5c8242008-01-24 02:22:38 -08001218 if (!err)
1219 pci_using_dac = 1;
1220 } else {
Yang Hongyang284901a2009-04-06 19:01:15 -07001221 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Auke Kok9d5c8242008-01-24 02:22:38 -08001222 if (err) {
Yang Hongyang284901a2009-04-06 19:01:15 -07001223 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
Auke Kok9d5c8242008-01-24 02:22:38 -08001224 if (err) {
1225 dev_err(&pdev->dev, "No usable DMA "
1226 "configuration, aborting\n");
1227 goto err_dma;
1228 }
1229 }
1230 }
1231
Alexander Duyckaed5dec2009-02-06 23:16:04 +00001232 err = pci_request_selected_regions(pdev, pci_select_bars(pdev,
1233 IORESOURCE_MEM),
1234 igb_driver_name);
Auke Kok9d5c8242008-01-24 02:22:38 -08001235 if (err)
1236 goto err_pci_reg;
1237
Frans Pop19d5afd2009-10-02 10:04:12 -07001238 pci_enable_pcie_error_reporting(pdev);
Alexander Duyck40a914f2008-11-27 00:24:37 -08001239
Auke Kok9d5c8242008-01-24 02:22:38 -08001240 pci_set_master(pdev);
Auke Kokc682fc22008-04-23 11:09:34 -07001241 pci_save_state(pdev);
Auke Kok9d5c8242008-01-24 02:22:38 -08001242
1243 err = -ENOMEM;
Alexander Duyck1bfaf072009-02-19 20:39:23 -08001244 netdev = alloc_etherdev_mq(sizeof(struct igb_adapter),
1245 IGB_ABS_MAX_TX_QUEUES);
Auke Kok9d5c8242008-01-24 02:22:38 -08001246 if (!netdev)
1247 goto err_alloc_etherdev;
1248
1249 SET_NETDEV_DEV(netdev, &pdev->dev);
1250
1251 pci_set_drvdata(pdev, netdev);
1252 adapter = netdev_priv(netdev);
1253 adapter->netdev = netdev;
1254 adapter->pdev = pdev;
1255 hw = &adapter->hw;
1256 hw->back = adapter;
1257 adapter->msg_enable = NETIF_MSG_DRV | NETIF_MSG_PROBE;
1258
1259 mmio_start = pci_resource_start(pdev, 0);
1260 mmio_len = pci_resource_len(pdev, 0);
1261
1262 err = -EIO;
Alexander Duyck28b07592009-02-06 23:20:31 +00001263 hw->hw_addr = ioremap(mmio_start, mmio_len);
1264 if (!hw->hw_addr)
Auke Kok9d5c8242008-01-24 02:22:38 -08001265 goto err_ioremap;
1266
Stephen Hemminger2e5c6922008-11-19 22:20:44 -08001267 netdev->netdev_ops = &igb_netdev_ops;
Auke Kok9d5c8242008-01-24 02:22:38 -08001268 igb_set_ethtool_ops(netdev);
Auke Kok9d5c8242008-01-24 02:22:38 -08001269 netdev->watchdog_timeo = 5 * HZ;
Auke Kok9d5c8242008-01-24 02:22:38 -08001270
1271 strncpy(netdev->name, pci_name(pdev), sizeof(netdev->name) - 1);
1272
1273 netdev->mem_start = mmio_start;
1274 netdev->mem_end = mmio_start + mmio_len;
1275
Auke Kok9d5c8242008-01-24 02:22:38 -08001276 /* PCI config space info */
1277 hw->vendor_id = pdev->vendor;
1278 hw->device_id = pdev->device;
1279 hw->revision_id = pdev->revision;
1280 hw->subsystem_vendor_id = pdev->subsystem_vendor;
1281 hw->subsystem_device_id = pdev->subsystem_device;
1282
1283 /* setup the private structure */
1284 hw->back = adapter;
1285 /* Copy the default MAC, PHY and NVM function pointers */
1286 memcpy(&hw->mac.ops, ei->mac_ops, sizeof(hw->mac.ops));
1287 memcpy(&hw->phy.ops, ei->phy_ops, sizeof(hw->phy.ops));
1288 memcpy(&hw->nvm.ops, ei->nvm_ops, sizeof(hw->nvm.ops));
1289 /* Initialize skew-specific constants */
1290 err = ei->get_invariants(hw);
1291 if (err)
Alexander Duyck450c87c2009-02-06 23:22:11 +00001292 goto err_sw_init;
Auke Kok9d5c8242008-01-24 02:22:38 -08001293
Alexander Duyck2a3abf62009-04-07 14:37:52 +00001294#ifdef CONFIG_PCI_IOV
1295 /* since iov functionality isn't critical to base device function we
1296 * can accept failure. If it fails we don't allow iov to be enabled */
1297 if (hw->mac.type == e1000_82576) {
1298 /* 82576 supports a maximum of 7 VFs in addition to the PF */
1299 unsigned int num_vfs = (max_vfs > 7) ? 7 : max_vfs;
1300 int i;
1301 unsigned char mac_addr[ETH_ALEN];
1302
Alexander Duyck9ca046d2009-04-09 22:49:39 +00001303 if (num_vfs) {
Alexander Duyck2a3abf62009-04-07 14:37:52 +00001304 adapter->vf_data = kcalloc(num_vfs,
1305 sizeof(struct vf_data_storage),
1306 GFP_KERNEL);
Alexander Duyck9ca046d2009-04-09 22:49:39 +00001307 if (!adapter->vf_data) {
1308 dev_err(&pdev->dev,
1309 "Could not allocate VF private data - "
1310 "IOV enable failed\n");
Alexander Duyck2a3abf62009-04-07 14:37:52 +00001311 } else {
Alexander Duyck9ca046d2009-04-09 22:49:39 +00001312 err = pci_enable_sriov(pdev, num_vfs);
1313 if (!err) {
1314 adapter->vfs_allocated_count = num_vfs;
1315 dev_info(&pdev->dev,
1316 "%d vfs allocated\n",
1317 num_vfs);
1318 for (i = 0;
1319 i < adapter->vfs_allocated_count;
1320 i++) {
1321 random_ether_addr(mac_addr);
1322 igb_set_vf_mac(adapter, i,
1323 mac_addr);
1324 }
1325 } else {
1326 kfree(adapter->vf_data);
1327 adapter->vf_data = NULL;
1328 }
Alexander Duyck2a3abf62009-04-07 14:37:52 +00001329 }
1330 }
1331 }
1332
1333#endif
Alexander Duyck450c87c2009-02-06 23:22:11 +00001334 /* setup the private structure */
Auke Kok9d5c8242008-01-24 02:22:38 -08001335 err = igb_sw_init(adapter);
1336 if (err)
1337 goto err_sw_init;
1338
1339 igb_get_bus_info_pcie(hw);
1340
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07001341 /* set flags */
1342 switch (hw->mac.type) {
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07001343 case e1000_82575:
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07001344 adapter->flags |= IGB_FLAG_NEED_CTX_IDX;
1345 break;
Alexander Duyckbbd98fe2009-01-31 00:52:30 -08001346 case e1000_82576:
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07001347 default:
1348 break;
1349 }
1350
Auke Kok9d5c8242008-01-24 02:22:38 -08001351 hw->phy.autoneg_wait_to_complete = false;
1352 hw->mac.adaptive_ifs = true;
1353
1354 /* Copper options */
1355 if (hw->phy.media_type == e1000_media_type_copper) {
1356 hw->phy.mdix = AUTO_ALL_MODES;
1357 hw->phy.disable_polarity_correction = false;
1358 hw->phy.ms_type = e1000_ms_hw_default;
1359 }
1360
1361 if (igb_check_reset_block(hw))
1362 dev_info(&pdev->dev,
1363 "PHY reset is blocked due to SOL/IDER session.\n");
1364
1365 netdev->features = NETIF_F_SG |
Alexander Duyck7d8eb292009-02-06 23:18:27 +00001366 NETIF_F_IP_CSUM |
Auke Kok9d5c8242008-01-24 02:22:38 -08001367 NETIF_F_HW_VLAN_TX |
1368 NETIF_F_HW_VLAN_RX |
1369 NETIF_F_HW_VLAN_FILTER;
1370
Alexander Duyck7d8eb292009-02-06 23:18:27 +00001371 netdev->features |= NETIF_F_IPV6_CSUM;
Auke Kok9d5c8242008-01-24 02:22:38 -08001372 netdev->features |= NETIF_F_TSO;
Auke Kok9d5c8242008-01-24 02:22:38 -08001373 netdev->features |= NETIF_F_TSO6;
Jeff Kirsher48f29ff2008-06-05 04:06:27 -07001374
Herbert Xu5c0999b2009-01-19 15:20:57 -08001375 netdev->features |= NETIF_F_GRO;
Alexander Duyckd3352522008-07-08 15:12:13 -07001376
Jeff Kirsher48f29ff2008-06-05 04:06:27 -07001377 netdev->vlan_features |= NETIF_F_TSO;
1378 netdev->vlan_features |= NETIF_F_TSO6;
Alexander Duyck7d8eb292009-02-06 23:18:27 +00001379 netdev->vlan_features |= NETIF_F_IP_CSUM;
Alexander Duyckcd1da502009-08-25 04:47:50 +00001380 netdev->vlan_features |= NETIF_F_IPV6_CSUM;
Jeff Kirsher48f29ff2008-06-05 04:06:27 -07001381 netdev->vlan_features |= NETIF_F_SG;
1382
Auke Kok9d5c8242008-01-24 02:22:38 -08001383 if (pci_using_dac)
1384 netdev->features |= NETIF_F_HIGHDMA;
1385
Jesse Brandeburgb9473562009-04-27 22:36:13 +00001386 if (adapter->hw.mac.type == e1000_82576)
1387 netdev->features |= NETIF_F_SCTP_CSUM;
1388
Auke Kok9d5c8242008-01-24 02:22:38 -08001389 adapter->en_mng_pt = igb_enable_mng_pass_thru(&adapter->hw);
1390
1391 /* before reading the NVM, reset the controller to put the device in a
1392 * known good starting state */
1393 hw->mac.ops.reset_hw(hw);
1394
1395 /* make sure the NVM is good */
1396 if (igb_validate_nvm_checksum(hw) < 0) {
1397 dev_err(&pdev->dev, "The NVM Checksum Is Not Valid\n");
1398 err = -EIO;
1399 goto err_eeprom;
1400 }
1401
1402 /* copy the MAC address out of the NVM */
1403 if (hw->mac.ops.read_mac_addr(hw))
1404 dev_err(&pdev->dev, "NVM Read Error\n");
1405
1406 memcpy(netdev->dev_addr, hw->mac.addr, netdev->addr_len);
1407 memcpy(netdev->perm_addr, hw->mac.addr, netdev->addr_len);
1408
1409 if (!is_valid_ether_addr(netdev->perm_addr)) {
1410 dev_err(&pdev->dev, "Invalid MAC Address\n");
1411 err = -EIO;
1412 goto err_eeprom;
1413 }
1414
Alexander Duyck0e340482009-03-20 00:17:08 +00001415 setup_timer(&adapter->watchdog_timer, &igb_watchdog,
1416 (unsigned long) adapter);
1417 setup_timer(&adapter->phy_info_timer, &igb_update_phy_info,
1418 (unsigned long) adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08001419
1420 INIT_WORK(&adapter->reset_task, igb_reset_task);
1421 INIT_WORK(&adapter->watchdog_task, igb_watchdog_task);
1422
Alexander Duyck450c87c2009-02-06 23:22:11 +00001423 /* Initialize link properties that are user-changeable */
Auke Kok9d5c8242008-01-24 02:22:38 -08001424 adapter->fc_autoneg = true;
1425 hw->mac.autoneg = true;
1426 hw->phy.autoneg_advertised = 0x2f;
1427
Alexander Duyck0cce1192009-07-23 18:10:24 +00001428 hw->fc.requested_mode = e1000_fc_default;
1429 hw->fc.current_mode = e1000_fc_default;
Auke Kok9d5c8242008-01-24 02:22:38 -08001430
Alexander Duyckcbd347a2009-02-15 23:59:44 -08001431 adapter->itr_setting = IGB_DEFAULT_ITR;
Auke Kok9d5c8242008-01-24 02:22:38 -08001432 adapter->itr = IGB_START_ITR;
1433
1434 igb_validate_mdi_setting(hw);
1435
Auke Kok9d5c8242008-01-24 02:22:38 -08001436 /* Initial Wake on LAN setting If APM wake is enabled in the EEPROM,
1437 * enable the ACPI Magic Packet filter
1438 */
1439
Alexander Duycka2cf8b62009-03-13 20:41:17 +00001440 if (hw->bus.func == 0)
Alexander Duyck312c75a2009-02-06 23:17:47 +00001441 hw->nvm.ops.read(hw, NVM_INIT_CONTROL3_PORT_A, 1, &eeprom_data);
Alexander Duycka2cf8b62009-03-13 20:41:17 +00001442 else if (hw->bus.func == 1)
1443 hw->nvm.ops.read(hw, NVM_INIT_CONTROL3_PORT_B, 1, &eeprom_data);
Auke Kok9d5c8242008-01-24 02:22:38 -08001444
1445 if (eeprom_data & eeprom_apme_mask)
1446 adapter->eeprom_wol |= E1000_WUFC_MAG;
1447
1448 /* now that we have the eeprom settings, apply the special cases where
1449 * the eeprom may be wrong or the board simply won't support wake on
1450 * lan on a particular port */
1451 switch (pdev->device) {
1452 case E1000_DEV_ID_82575GB_QUAD_COPPER:
1453 adapter->eeprom_wol = 0;
1454 break;
1455 case E1000_DEV_ID_82575EB_FIBER_SERDES:
Alexander Duyck2d064c02008-07-08 15:10:12 -07001456 case E1000_DEV_ID_82576_FIBER:
1457 case E1000_DEV_ID_82576_SERDES:
Auke Kok9d5c8242008-01-24 02:22:38 -08001458 /* Wake events only supported on port A for dual fiber
1459 * regardless of eeprom setting */
1460 if (rd32(E1000_STATUS) & E1000_STATUS_FUNC_1)
1461 adapter->eeprom_wol = 0;
1462 break;
Alexander Duyckc8ea5ea92009-03-13 20:42:35 +00001463 case E1000_DEV_ID_82576_QUAD_COPPER:
1464 /* if quad port adapter, disable WoL on all but port A */
1465 if (global_quad_port_a != 0)
1466 adapter->eeprom_wol = 0;
1467 else
1468 adapter->flags |= IGB_FLAG_QUAD_PORT_A;
1469 /* Reset for multiple quad port adapters */
1470 if (++global_quad_port_a == 4)
1471 global_quad_port_a = 0;
1472 break;
Auke Kok9d5c8242008-01-24 02:22:38 -08001473 }
1474
1475 /* initialize the wol settings based on the eeprom settings */
1476 adapter->wol = adapter->eeprom_wol;
\"Rafael J. Wysocki\e1b86d82008-11-07 20:30:37 +00001477 device_set_wakeup_enable(&adapter->pdev->dev, adapter->wol);
Auke Kok9d5c8242008-01-24 02:22:38 -08001478
1479 /* reset the hardware with the new settings */
1480 igb_reset(adapter);
1481
1482 /* let the f/w know that the h/w is now under the control of the
1483 * driver. */
1484 igb_get_hw_control(adapter);
1485
Auke Kok9d5c8242008-01-24 02:22:38 -08001486 strcpy(netdev->name, "eth%d");
1487 err = register_netdev(netdev);
1488 if (err)
1489 goto err_register;
1490
Jesse Brandeburgb168dfc2009-04-17 20:44:32 +00001491 /* carrier off reporting is important to ethtool even BEFORE open */
1492 netif_carrier_off(netdev);
1493
Jeff Kirsher421e02f2008-10-17 11:08:31 -07001494#ifdef CONFIG_IGB_DCA
Alexander Duyckbbd98fe2009-01-31 00:52:30 -08001495 if (dca_add_requester(&pdev->dev) == 0) {
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07001496 adapter->flags |= IGB_FLAG_DCA_ENABLED;
Jeb Cramerfe4506b2008-07-08 15:07:55 -07001497 dev_info(&pdev->dev, "DCA enabled\n");
Jeb Cramerfe4506b2008-07-08 15:07:55 -07001498 igb_setup_dca(adapter);
1499 }
1500#endif
1501
Patrick Ohly38c845c2009-02-12 05:03:41 +00001502 /*
1503 * Initialize hardware timer: we keep it running just in case
1504 * that some program needs it later on.
1505 */
1506 memset(&adapter->cycles, 0, sizeof(adapter->cycles));
1507 adapter->cycles.read = igb_read_clock;
1508 adapter->cycles.mask = CLOCKSOURCE_MASK(64);
1509 adapter->cycles.mult = 1;
1510 adapter->cycles.shift = IGB_TSYNC_SHIFT;
1511 wr32(E1000_TIMINCA,
1512 (1<<24) |
1513 IGB_TSYNC_CYCLE_TIME_IN_NANOSECONDS * IGB_TSYNC_SCALE);
1514#if 0
1515 /*
1516 * Avoid rollover while we initialize by resetting the time counter.
1517 */
1518 wr32(E1000_SYSTIML, 0x00000000);
1519 wr32(E1000_SYSTIMH, 0x00000000);
1520#else
1521 /*
1522 * Set registers so that rollover occurs soon to test this.
1523 */
1524 wr32(E1000_SYSTIML, 0x00000000);
1525 wr32(E1000_SYSTIMH, 0xFF800000);
1526#endif
1527 wrfl();
1528 timecounter_init(&adapter->clock,
1529 &adapter->cycles,
1530 ktime_to_ns(ktime_get_real()));
1531
Patrick Ohly33af6bc2009-02-12 05:03:43 +00001532 /*
1533 * Synchronize our NIC clock against system wall clock. NIC
1534 * time stamp reading requires ~3us per sample, each sample
1535 * was pretty stable even under load => only require 10
1536 * samples for each offset comparison.
1537 */
1538 memset(&adapter->compare, 0, sizeof(adapter->compare));
1539 adapter->compare.source = &adapter->clock;
1540 adapter->compare.target = ktime_get_real;
1541 adapter->compare.num_samples = 10;
1542 timecompare_update(&adapter->compare, 0);
1543
Patrick Ohly38c845c2009-02-12 05:03:41 +00001544#ifdef DEBUG
1545 {
1546 char buffer[160];
1547 printk(KERN_DEBUG
1548 "igb: %s: hw %p initialized timer\n",
1549 igb_get_time_str(adapter, buffer),
1550 &adapter->hw);
1551 }
1552#endif
1553
Auke Kok9d5c8242008-01-24 02:22:38 -08001554 dev_info(&pdev->dev, "Intel(R) Gigabit Ethernet Network Connection\n");
1555 /* print bus type/speed/width info */
Johannes Berg7c510e42008-10-27 17:47:26 -07001556 dev_info(&pdev->dev, "%s: (PCIe:%s:%s) %pM\n",
Auke Kok9d5c8242008-01-24 02:22:38 -08001557 netdev->name,
1558 ((hw->bus.speed == e1000_bus_speed_2500)
1559 ? "2.5Gb/s" : "unknown"),
Alexander Duyck59c3de82009-03-31 20:38:00 +00001560 ((hw->bus.width == e1000_bus_width_pcie_x4) ? "Width x4" :
1561 (hw->bus.width == e1000_bus_width_pcie_x2) ? "Width x2" :
1562 (hw->bus.width == e1000_bus_width_pcie_x1) ? "Width x1" :
1563 "unknown"),
Johannes Berg7c510e42008-10-27 17:47:26 -07001564 netdev->dev_addr);
Auke Kok9d5c8242008-01-24 02:22:38 -08001565
1566 igb_read_part_num(hw, &part_num);
1567 dev_info(&pdev->dev, "%s: PBA No: %06x-%03x\n", netdev->name,
1568 (part_num >> 8), (part_num & 0xff));
1569
1570 dev_info(&pdev->dev,
1571 "Using %s interrupts. %d rx queue(s), %d tx queue(s)\n",
1572 adapter->msix_entries ? "MSI-X" :
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07001573 (adapter->flags & IGB_FLAG_HAS_MSI) ? "MSI" : "legacy",
Auke Kok9d5c8242008-01-24 02:22:38 -08001574 adapter->num_rx_queues, adapter->num_tx_queues);
1575
Auke Kok9d5c8242008-01-24 02:22:38 -08001576 return 0;
1577
1578err_register:
1579 igb_release_hw_control(adapter);
1580err_eeprom:
1581 if (!igb_check_reset_block(hw))
Alexander Duyckf5f4cf02008-11-21 21:30:24 -08001582 igb_reset_phy(hw);
Auke Kok9d5c8242008-01-24 02:22:38 -08001583
1584 if (hw->flash_address)
1585 iounmap(hw->flash_address);
1586
Alexander Duycka88f10e2008-07-08 15:13:38 -07001587 igb_free_queues(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08001588err_sw_init:
Auke Kok9d5c8242008-01-24 02:22:38 -08001589 iounmap(hw->hw_addr);
1590err_ioremap:
1591 free_netdev(netdev);
1592err_alloc_etherdev:
Alexander Duyckaed5dec2009-02-06 23:16:04 +00001593 pci_release_selected_regions(pdev, pci_select_bars(pdev,
1594 IORESOURCE_MEM));
Auke Kok9d5c8242008-01-24 02:22:38 -08001595err_pci_reg:
1596err_dma:
1597 pci_disable_device(pdev);
1598 return err;
1599}
1600
1601/**
1602 * igb_remove - Device Removal Routine
1603 * @pdev: PCI device information struct
1604 *
1605 * igb_remove is called by the PCI subsystem to alert the driver
1606 * that it should release a PCI device. The could be caused by a
1607 * Hot-Plug event, or because the driver is going to be removed from
1608 * memory.
1609 **/
1610static void __devexit igb_remove(struct pci_dev *pdev)
1611{
1612 struct net_device *netdev = pci_get_drvdata(pdev);
1613 struct igb_adapter *adapter = netdev_priv(netdev);
Jeb Cramerfe4506b2008-07-08 15:07:55 -07001614 struct e1000_hw *hw = &adapter->hw;
Auke Kok9d5c8242008-01-24 02:22:38 -08001615
1616 /* flush_scheduled work may reschedule our watchdog task, so
1617 * explicitly disable watchdog tasks from being rescheduled */
1618 set_bit(__IGB_DOWN, &adapter->state);
1619 del_timer_sync(&adapter->watchdog_timer);
1620 del_timer_sync(&adapter->phy_info_timer);
1621
1622 flush_scheduled_work();
1623
Jeff Kirsher421e02f2008-10-17 11:08:31 -07001624#ifdef CONFIG_IGB_DCA
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07001625 if (adapter->flags & IGB_FLAG_DCA_ENABLED) {
Jeb Cramerfe4506b2008-07-08 15:07:55 -07001626 dev_info(&pdev->dev, "DCA disabled\n");
1627 dca_remove_requester(&pdev->dev);
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07001628 adapter->flags &= ~IGB_FLAG_DCA_ENABLED;
Alexander Duyckcbd347a2009-02-15 23:59:44 -08001629 wr32(E1000_DCA_CTRL, E1000_DCA_CTRL_DCA_MODE_DISABLE);
Jeb Cramerfe4506b2008-07-08 15:07:55 -07001630 }
1631#endif
1632
Auke Kok9d5c8242008-01-24 02:22:38 -08001633 /* Release control of h/w to f/w. If f/w is AMT enabled, this
1634 * would have already happened in close and is redundant. */
1635 igb_release_hw_control(adapter);
1636
1637 unregister_netdev(netdev);
1638
Alexander Duyckf5f4cf02008-11-21 21:30:24 -08001639 if (!igb_check_reset_block(&adapter->hw))
1640 igb_reset_phy(&adapter->hw);
Auke Kok9d5c8242008-01-24 02:22:38 -08001641
Auke Kok9d5c8242008-01-24 02:22:38 -08001642 igb_reset_interrupt_capability(adapter);
1643
Alexander Duycka88f10e2008-07-08 15:13:38 -07001644 igb_free_queues(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08001645
Alexander Duyck37680112009-02-19 20:40:30 -08001646#ifdef CONFIG_PCI_IOV
1647 /* reclaim resources allocated to VFs */
1648 if (adapter->vf_data) {
1649 /* disable iov and allow time for transactions to clear */
1650 pci_disable_sriov(pdev);
1651 msleep(500);
1652
1653 kfree(adapter->vf_data);
1654 adapter->vf_data = NULL;
1655 wr32(E1000_IOVCTL, E1000_IOVCTL_REUSE_VFQ);
1656 msleep(100);
1657 dev_info(&pdev->dev, "IOV Disabled\n");
1658 }
1659#endif
Alexander Duyck28b07592009-02-06 23:20:31 +00001660 iounmap(hw->hw_addr);
1661 if (hw->flash_address)
1662 iounmap(hw->flash_address);
Alexander Duyckaed5dec2009-02-06 23:16:04 +00001663 pci_release_selected_regions(pdev, pci_select_bars(pdev,
1664 IORESOURCE_MEM));
Auke Kok9d5c8242008-01-24 02:22:38 -08001665
1666 free_netdev(netdev);
1667
Frans Pop19d5afd2009-10-02 10:04:12 -07001668 pci_disable_pcie_error_reporting(pdev);
Alexander Duyck40a914f2008-11-27 00:24:37 -08001669
Auke Kok9d5c8242008-01-24 02:22:38 -08001670 pci_disable_device(pdev);
1671}
1672
1673/**
1674 * igb_sw_init - Initialize general software structures (struct igb_adapter)
1675 * @adapter: board private structure to initialize
1676 *
1677 * igb_sw_init initializes the Adapter private data structure.
1678 * Fields are initialized based on PCI device information and
1679 * OS network device settings (MTU size).
1680 **/
1681static int __devinit igb_sw_init(struct igb_adapter *adapter)
1682{
1683 struct e1000_hw *hw = &adapter->hw;
1684 struct net_device *netdev = adapter->netdev;
1685 struct pci_dev *pdev = adapter->pdev;
1686
1687 pci_read_config_word(pdev, PCI_COMMAND, &hw->bus.pci_cmd_word);
1688
Alexander Duyck68fd9912008-11-20 00:48:10 -08001689 adapter->tx_ring_count = IGB_DEFAULT_TXD;
1690 adapter->rx_ring_count = IGB_DEFAULT_RXD;
Auke Kok9d5c8242008-01-24 02:22:38 -08001691 adapter->rx_buffer_len = MAXIMUM_ETHERNET_VLAN_SIZE;
1692 adapter->rx_ps_hdr_size = 0; /* disable packet split */
1693 adapter->max_frame_size = netdev->mtu + ETH_HLEN + ETH_FCS_LEN;
1694 adapter->min_frame_size = ETH_ZLEN + ETH_FCS_LEN;
1695
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07001696 /* This call may decrease the number of queues depending on
1697 * interrupt mode. */
Auke Kok9d5c8242008-01-24 02:22:38 -08001698 igb_set_interrupt_capability(adapter);
1699
1700 if (igb_alloc_queues(adapter)) {
1701 dev_err(&pdev->dev, "Unable to allocate memory for queues\n");
1702 return -ENOMEM;
1703 }
1704
1705 /* Explicitly disable IRQ since the NIC can be in any state. */
1706 igb_irq_disable(adapter);
1707
1708 set_bit(__IGB_DOWN, &adapter->state);
1709 return 0;
1710}
1711
1712/**
1713 * igb_open - Called when a network interface is made active
1714 * @netdev: network interface device structure
1715 *
1716 * Returns 0 on success, negative value on failure
1717 *
1718 * The open entry point is called when a network interface is made
1719 * active by the system (IFF_UP). At this point all resources needed
1720 * for transmit and receive operations are allocated, the interrupt
1721 * handler is registered with the OS, the watchdog timer is started,
1722 * and the stack is notified that the interface is ready.
1723 **/
1724static int igb_open(struct net_device *netdev)
1725{
1726 struct igb_adapter *adapter = netdev_priv(netdev);
1727 struct e1000_hw *hw = &adapter->hw;
1728 int err;
1729 int i;
1730
1731 /* disallow open during test */
1732 if (test_bit(__IGB_TESTING, &adapter->state))
1733 return -EBUSY;
1734
Jesse Brandeburgb168dfc2009-04-17 20:44:32 +00001735 netif_carrier_off(netdev);
1736
Auke Kok9d5c8242008-01-24 02:22:38 -08001737 /* allocate transmit descriptors */
1738 err = igb_setup_all_tx_resources(adapter);
1739 if (err)
1740 goto err_setup_tx;
1741
1742 /* allocate receive descriptors */
1743 err = igb_setup_all_rx_resources(adapter);
1744 if (err)
1745 goto err_setup_rx;
1746
1747 /* e1000_power_up_phy(adapter); */
1748
1749 adapter->mng_vlan_id = IGB_MNG_VLAN_NONE;
1750 if ((adapter->hw.mng_cookie.status &
1751 E1000_MNG_DHCP_COOKIE_STATUS_VLAN))
1752 igb_update_mng_vlan(adapter);
1753
1754 /* before we allocate an interrupt, we must be ready to handle it.
1755 * Setting DEBUG_SHIRQ in the kernel makes it fire an interrupt
1756 * as soon as we call pci_request_irq, so we have to setup our
1757 * clean_rx handler before we do so. */
1758 igb_configure(adapter);
1759
Alexander Duyck4ae196d2009-02-19 20:40:07 -08001760 igb_vmm_control(adapter);
Alexander Duycke1739522009-02-19 20:39:44 -08001761 igb_set_vmolr(hw, adapter->vfs_allocated_count);
1762
Auke Kok9d5c8242008-01-24 02:22:38 -08001763 err = igb_request_irq(adapter);
1764 if (err)
1765 goto err_req_irq;
1766
1767 /* From here on the code is the same as igb_up() */
1768 clear_bit(__IGB_DOWN, &adapter->state);
1769
PJ Waskiewicz844290e2008-06-27 11:00:39 -07001770 for (i = 0; i < adapter->num_rx_queues; i++)
1771 napi_enable(&adapter->rx_ring[i].napi);
Auke Kok9d5c8242008-01-24 02:22:38 -08001772
1773 /* Clear any pending interrupts. */
1774 rd32(E1000_ICR);
PJ Waskiewicz844290e2008-06-27 11:00:39 -07001775
1776 igb_irq_enable(adapter);
1777
Jeff Kirsherd55b53f2008-07-18 04:33:03 -07001778 netif_tx_start_all_queues(netdev);
1779
Auke Kok9d5c8242008-01-24 02:22:38 -08001780 /* Fire a link status change interrupt to start the watchdog. */
1781 wr32(E1000_ICS, E1000_ICS_LSC);
1782
1783 return 0;
1784
1785err_req_irq:
1786 igb_release_hw_control(adapter);
1787 /* e1000_power_down_phy(adapter); */
1788 igb_free_all_rx_resources(adapter);
1789err_setup_rx:
1790 igb_free_all_tx_resources(adapter);
1791err_setup_tx:
1792 igb_reset(adapter);
1793
1794 return err;
1795}
1796
1797/**
1798 * igb_close - Disables a network interface
1799 * @netdev: network interface device structure
1800 *
1801 * Returns 0, this is not allowed to fail
1802 *
1803 * The close entry point is called when an interface is de-activated
1804 * by the OS. The hardware is still under the driver's control, but
1805 * needs to be disabled. A global MAC reset is issued to stop the
1806 * hardware, and all transmit and receive resources are freed.
1807 **/
1808static int igb_close(struct net_device *netdev)
1809{
1810 struct igb_adapter *adapter = netdev_priv(netdev);
1811
1812 WARN_ON(test_bit(__IGB_RESETTING, &adapter->state));
1813 igb_down(adapter);
1814
1815 igb_free_irq(adapter);
1816
1817 igb_free_all_tx_resources(adapter);
1818 igb_free_all_rx_resources(adapter);
1819
1820 /* kill manageability vlan ID if supported, but not if a vlan with
1821 * the same ID is registered on the host OS (let 8021q kill it) */
1822 if ((adapter->hw.mng_cookie.status &
1823 E1000_MNG_DHCP_COOKIE_STATUS_VLAN) &&
1824 !(adapter->vlgrp &&
1825 vlan_group_get_device(adapter->vlgrp, adapter->mng_vlan_id)))
1826 igb_vlan_rx_kill_vid(netdev, adapter->mng_vlan_id);
1827
1828 return 0;
1829}
1830
1831/**
1832 * igb_setup_tx_resources - allocate Tx resources (Descriptors)
1833 * @adapter: board private structure
1834 * @tx_ring: tx descriptor ring (for a specific queue) to setup
1835 *
1836 * Return 0 on success, negative on failure
1837 **/
Auke Kok9d5c8242008-01-24 02:22:38 -08001838int igb_setup_tx_resources(struct igb_adapter *adapter,
1839 struct igb_ring *tx_ring)
1840{
1841 struct pci_dev *pdev = adapter->pdev;
1842 int size;
1843
1844 size = sizeof(struct igb_buffer) * tx_ring->count;
1845 tx_ring->buffer_info = vmalloc(size);
1846 if (!tx_ring->buffer_info)
1847 goto err;
1848 memset(tx_ring->buffer_info, 0, size);
1849
1850 /* round up to nearest 4K */
Alexander Duyck85e8d002009-02-16 00:00:20 -08001851 tx_ring->size = tx_ring->count * sizeof(union e1000_adv_tx_desc);
Auke Kok9d5c8242008-01-24 02:22:38 -08001852 tx_ring->size = ALIGN(tx_ring->size, 4096);
1853
1854 tx_ring->desc = pci_alloc_consistent(pdev, tx_ring->size,
1855 &tx_ring->dma);
1856
1857 if (!tx_ring->desc)
1858 goto err;
1859
1860 tx_ring->adapter = adapter;
1861 tx_ring->next_to_use = 0;
1862 tx_ring->next_to_clean = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08001863 return 0;
1864
1865err:
1866 vfree(tx_ring->buffer_info);
1867 dev_err(&adapter->pdev->dev,
1868 "Unable to allocate memory for the transmit descriptor ring\n");
1869 return -ENOMEM;
1870}
1871
1872/**
1873 * igb_setup_all_tx_resources - wrapper to allocate Tx resources
1874 * (Descriptors) for all queues
1875 * @adapter: board private structure
1876 *
1877 * Return 0 on success, negative on failure
1878 **/
1879static int igb_setup_all_tx_resources(struct igb_adapter *adapter)
1880{
1881 int i, err = 0;
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07001882 int r_idx;
Auke Kok9d5c8242008-01-24 02:22:38 -08001883
1884 for (i = 0; i < adapter->num_tx_queues; i++) {
1885 err = igb_setup_tx_resources(adapter, &adapter->tx_ring[i]);
1886 if (err) {
1887 dev_err(&adapter->pdev->dev,
1888 "Allocation for Tx Queue %u failed\n", i);
1889 for (i--; i >= 0; i--)
Mitch Williams3b644cf2008-06-27 10:59:48 -07001890 igb_free_tx_resources(&adapter->tx_ring[i]);
Auke Kok9d5c8242008-01-24 02:22:38 -08001891 break;
1892 }
1893 }
1894
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07001895 for (i = 0; i < IGB_MAX_TX_QUEUES; i++) {
1896 r_idx = i % adapter->num_tx_queues;
1897 adapter->multi_tx_table[i] = &adapter->tx_ring[r_idx];
Alexander Duyckeebbbdb2009-02-06 23:19:29 +00001898 }
Auke Kok9d5c8242008-01-24 02:22:38 -08001899 return err;
1900}
1901
1902/**
1903 * igb_configure_tx - Configure transmit Unit after Reset
1904 * @adapter: board private structure
1905 *
1906 * Configure the Tx unit of the MAC after a reset.
1907 **/
1908static void igb_configure_tx(struct igb_adapter *adapter)
1909{
Alexander Duyck0e014cb2008-12-26 01:33:18 -08001910 u64 tdba;
Auke Kok9d5c8242008-01-24 02:22:38 -08001911 struct e1000_hw *hw = &adapter->hw;
1912 u32 tctl;
1913 u32 txdctl, txctrl;
Alexander Duyck26bc19e2008-12-26 01:34:11 -08001914 int i, j;
Auke Kok9d5c8242008-01-24 02:22:38 -08001915
1916 for (i = 0; i < adapter->num_tx_queues; i++) {
Alexander Duyck73cd78f2009-02-12 18:16:59 +00001917 struct igb_ring *ring = &adapter->tx_ring[i];
Alexander Duyck26bc19e2008-12-26 01:34:11 -08001918 j = ring->reg_idx;
1919 wr32(E1000_TDLEN(j),
Alexander Duyck85e8d002009-02-16 00:00:20 -08001920 ring->count * sizeof(union e1000_adv_tx_desc));
Auke Kok9d5c8242008-01-24 02:22:38 -08001921 tdba = ring->dma;
Alexander Duyck26bc19e2008-12-26 01:34:11 -08001922 wr32(E1000_TDBAL(j),
Alexander Duyck73cd78f2009-02-12 18:16:59 +00001923 tdba & 0x00000000ffffffffULL);
Alexander Duyck26bc19e2008-12-26 01:34:11 -08001924 wr32(E1000_TDBAH(j), tdba >> 32);
Auke Kok9d5c8242008-01-24 02:22:38 -08001925
Alexander Duyck26bc19e2008-12-26 01:34:11 -08001926 ring->head = E1000_TDH(j);
1927 ring->tail = E1000_TDT(j);
Auke Kok9d5c8242008-01-24 02:22:38 -08001928 writel(0, hw->hw_addr + ring->tail);
1929 writel(0, hw->hw_addr + ring->head);
Alexander Duyck26bc19e2008-12-26 01:34:11 -08001930 txdctl = rd32(E1000_TXDCTL(j));
Auke Kok9d5c8242008-01-24 02:22:38 -08001931 txdctl |= E1000_TXDCTL_QUEUE_ENABLE;
Alexander Duyck26bc19e2008-12-26 01:34:11 -08001932 wr32(E1000_TXDCTL(j), txdctl);
Auke Kok9d5c8242008-01-24 02:22:38 -08001933
1934 /* Turn off Relaxed Ordering on head write-backs. The
1935 * writebacks MUST be delivered in order or it will
1936 * completely screw up our bookeeping.
1937 */
Alexander Duyck26bc19e2008-12-26 01:34:11 -08001938 txctrl = rd32(E1000_DCA_TXCTRL(j));
Auke Kok9d5c8242008-01-24 02:22:38 -08001939 txctrl &= ~E1000_DCA_TXCTRL_TX_WB_RO_EN;
Alexander Duyck26bc19e2008-12-26 01:34:11 -08001940 wr32(E1000_DCA_TXCTRL(j), txctrl);
Auke Kok9d5c8242008-01-24 02:22:38 -08001941 }
1942
Alexander Duycke1739522009-02-19 20:39:44 -08001943 /* disable queue 0 to prevent tail bump w/o re-configuration */
1944 if (adapter->vfs_allocated_count)
1945 wr32(E1000_TXDCTL(0), 0);
Auke Kok9d5c8242008-01-24 02:22:38 -08001946
1947 /* Program the Transmit Control Register */
Auke Kok9d5c8242008-01-24 02:22:38 -08001948 tctl = rd32(E1000_TCTL);
1949 tctl &= ~E1000_TCTL_CT;
1950 tctl |= E1000_TCTL_PSP | E1000_TCTL_RTLC |
1951 (E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT);
1952
1953 igb_config_collision_dist(hw);
1954
1955 /* Setup Transmit Descriptor Settings for eop descriptor */
1956 adapter->txd_cmd = E1000_TXD_CMD_EOP | E1000_TXD_CMD_RS;
1957
1958 /* Enable transmits */
1959 tctl |= E1000_TCTL_EN;
1960
1961 wr32(E1000_TCTL, tctl);
1962}
1963
1964/**
1965 * igb_setup_rx_resources - allocate Rx resources (Descriptors)
1966 * @adapter: board private structure
1967 * @rx_ring: rx descriptor ring (for a specific queue) to setup
1968 *
1969 * Returns 0 on success, negative on failure
1970 **/
Auke Kok9d5c8242008-01-24 02:22:38 -08001971int igb_setup_rx_resources(struct igb_adapter *adapter,
1972 struct igb_ring *rx_ring)
1973{
1974 struct pci_dev *pdev = adapter->pdev;
1975 int size, desc_len;
1976
1977 size = sizeof(struct igb_buffer) * rx_ring->count;
1978 rx_ring->buffer_info = vmalloc(size);
1979 if (!rx_ring->buffer_info)
1980 goto err;
1981 memset(rx_ring->buffer_info, 0, size);
1982
1983 desc_len = sizeof(union e1000_adv_rx_desc);
1984
1985 /* Round up to nearest 4K */
1986 rx_ring->size = rx_ring->count * desc_len;
1987 rx_ring->size = ALIGN(rx_ring->size, 4096);
1988
1989 rx_ring->desc = pci_alloc_consistent(pdev, rx_ring->size,
1990 &rx_ring->dma);
1991
1992 if (!rx_ring->desc)
1993 goto err;
1994
1995 rx_ring->next_to_clean = 0;
1996 rx_ring->next_to_use = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08001997
1998 rx_ring->adapter = adapter;
Auke Kok9d5c8242008-01-24 02:22:38 -08001999
2000 return 0;
2001
2002err:
2003 vfree(rx_ring->buffer_info);
2004 dev_err(&adapter->pdev->dev, "Unable to allocate memory for "
2005 "the receive descriptor ring\n");
2006 return -ENOMEM;
2007}
2008
2009/**
2010 * igb_setup_all_rx_resources - wrapper to allocate Rx resources
2011 * (Descriptors) for all queues
2012 * @adapter: board private structure
2013 *
2014 * Return 0 on success, negative on failure
2015 **/
2016static int igb_setup_all_rx_resources(struct igb_adapter *adapter)
2017{
2018 int i, err = 0;
2019
2020 for (i = 0; i < adapter->num_rx_queues; i++) {
2021 err = igb_setup_rx_resources(adapter, &adapter->rx_ring[i]);
2022 if (err) {
2023 dev_err(&adapter->pdev->dev,
2024 "Allocation for Rx Queue %u failed\n", i);
2025 for (i--; i >= 0; i--)
Mitch Williams3b644cf2008-06-27 10:59:48 -07002026 igb_free_rx_resources(&adapter->rx_ring[i]);
Auke Kok9d5c8242008-01-24 02:22:38 -08002027 break;
2028 }
2029 }
2030
2031 return err;
2032}
2033
2034/**
2035 * igb_setup_rctl - configure the receive control registers
2036 * @adapter: Board private structure
2037 **/
2038static void igb_setup_rctl(struct igb_adapter *adapter)
2039{
2040 struct e1000_hw *hw = &adapter->hw;
2041 u32 rctl;
2042 u32 srrctl = 0;
Alexander Duyck77a22942009-05-06 16:43:48 -07002043 int i;
Auke Kok9d5c8242008-01-24 02:22:38 -08002044
2045 rctl = rd32(E1000_RCTL);
2046
2047 rctl &= ~(3 << E1000_RCTL_MO_SHIFT);
Alexander Duyck69d728b2008-11-25 01:04:03 -08002048 rctl &= ~(E1000_RCTL_LBM_TCVR | E1000_RCTL_LBM_MAC);
Auke Kok9d5c8242008-01-24 02:22:38 -08002049
Alexander Duyck69d728b2008-11-25 01:04:03 -08002050 rctl |= E1000_RCTL_EN | E1000_RCTL_BAM | E1000_RCTL_RDMTS_HALF |
Alexander Duyck28b07592009-02-06 23:20:31 +00002051 (hw->mac.mc_filter_type << E1000_RCTL_MO_SHIFT);
Auke Kok9d5c8242008-01-24 02:22:38 -08002052
Auke Kok87cb7e82008-07-08 15:08:29 -07002053 /*
2054 * enable stripping of CRC. It's unlikely this will break BMC
2055 * redirection as it did with e1000. Newer features require
2056 * that the HW strips the CRC.
Alexander Duyck73cd78f2009-02-12 18:16:59 +00002057 */
Auke Kok87cb7e82008-07-08 15:08:29 -07002058 rctl |= E1000_RCTL_SECRC;
Auke Kok9d5c8242008-01-24 02:22:38 -08002059
Alexander Duyck9b07f3d32008-11-25 01:03:26 -08002060 /*
Alexander Duyckec54d7d2009-01-31 00:52:57 -08002061 * disable store bad packets and clear size bits.
Alexander Duyck9b07f3d32008-11-25 01:03:26 -08002062 */
Alexander Duyckec54d7d2009-01-31 00:52:57 -08002063 rctl &= ~(E1000_RCTL_SBP | E1000_RCTL_SZ_256);
Auke Kok9d5c8242008-01-24 02:22:38 -08002064
Alexander Duyckec54d7d2009-01-31 00:52:57 -08002065 /* enable LPE when to prevent packets larger than max_frame_size */
Alexander Duyck9b07f3d32008-11-25 01:03:26 -08002066 rctl |= E1000_RCTL_LPE;
Alexander Duyckb4557be2008-12-10 01:08:59 -08002067
2068 /* Setup buffer sizes */
2069 switch (adapter->rx_buffer_len) {
2070 case IGB_RXBUFFER_256:
2071 rctl |= E1000_RCTL_SZ_256;
2072 break;
2073 case IGB_RXBUFFER_512:
2074 rctl |= E1000_RCTL_SZ_512;
2075 break;
2076 default:
2077 srrctl = ALIGN(adapter->rx_buffer_len, 1024)
2078 >> E1000_SRRCTL_BSIZEPKT_SHIFT;
2079 break;
Auke Kok9d5c8242008-01-24 02:22:38 -08002080 }
2081
2082 /* 82575 and greater support packet-split where the protocol
2083 * header is placed in skb->data and the packet data is
2084 * placed in pages hanging off of skb_shinfo(skb)->nr_frags.
2085 * In the case of a non-split, skb->data is linearly filled,
2086 * followed by the page buffers. Therefore, skb->data is
2087 * sized to hold the largest protocol header.
2088 */
2089 /* allocations using alloc_page take too long for regular MTU
2090 * so only enable packet split for jumbo frames */
Alexander Duyckec54d7d2009-01-31 00:52:57 -08002091 if (adapter->netdev->mtu > ETH_DATA_LEN) {
Auke Kok9d5c8242008-01-24 02:22:38 -08002092 adapter->rx_ps_hdr_size = IGB_RXBUFFER_128;
Alexander Duyckbf36c1a2008-07-08 15:11:40 -07002093 srrctl |= adapter->rx_ps_hdr_size <<
Auke Kok9d5c8242008-01-24 02:22:38 -08002094 E1000_SRRCTL_BSIZEHDRSIZE_SHIFT;
Auke Kok9d5c8242008-01-24 02:22:38 -08002095 srrctl |= E1000_SRRCTL_DESCTYPE_HDR_SPLIT_ALWAYS;
2096 } else {
2097 adapter->rx_ps_hdr_size = 0;
2098 srrctl |= E1000_SRRCTL_DESCTYPE_ADV_ONEBUF;
2099 }
2100
Alexander Duycke1739522009-02-19 20:39:44 -08002101 /* Attention!!! For SR-IOV PF driver operations you must enable
2102 * queue drop for all VF and PF queues to prevent head of line blocking
2103 * if an un-trusted VF does not provide descriptors to hardware.
2104 */
2105 if (adapter->vfs_allocated_count) {
2106 u32 vmolr;
2107
Alexander Duycke1739522009-02-19 20:39:44 -08002108 /* set all queue drop enable bits */
2109 wr32(E1000_QDE, ALL_QUEUES);
2110 srrctl |= E1000_SRRCTL_DROP_EN;
2111
2112 /* disable queue 0 to prevent tail write w/o re-config */
2113 wr32(E1000_RXDCTL(0), 0);
2114
Alexander Duyck77a22942009-05-06 16:43:48 -07002115 vmolr = rd32(E1000_VMOLR(adapter->vfs_allocated_count));
Alexander Duycke1739522009-02-19 20:39:44 -08002116 if (rctl & E1000_RCTL_LPE)
2117 vmolr |= E1000_VMOLR_LPE;
Alexander Duyck77a22942009-05-06 16:43:48 -07002118 if (adapter->num_rx_queues > 1)
Alexander Duycke1739522009-02-19 20:39:44 -08002119 vmolr |= E1000_VMOLR_RSSE;
Alexander Duyck77a22942009-05-06 16:43:48 -07002120 wr32(E1000_VMOLR(adapter->vfs_allocated_count), vmolr);
Alexander Duycke1739522009-02-19 20:39:44 -08002121 }
2122
Alexander Duyck26bc19e2008-12-26 01:34:11 -08002123 for (i = 0; i < adapter->num_rx_queues; i++) {
Alexander Duyck77a22942009-05-06 16:43:48 -07002124 int j = adapter->rx_ring[i].reg_idx;
Alexander Duyck26bc19e2008-12-26 01:34:11 -08002125 wr32(E1000_SRRCTL(j), srrctl);
2126 }
Auke Kok9d5c8242008-01-24 02:22:38 -08002127
2128 wr32(E1000_RCTL, rctl);
2129}
2130
2131/**
Alexander Duycke1739522009-02-19 20:39:44 -08002132 * igb_rlpml_set - set maximum receive packet size
2133 * @adapter: board private structure
2134 *
2135 * Configure maximum receivable packet size.
2136 **/
2137static void igb_rlpml_set(struct igb_adapter *adapter)
2138{
2139 u32 max_frame_size = adapter->max_frame_size;
2140 struct e1000_hw *hw = &adapter->hw;
2141 u16 pf_id = adapter->vfs_allocated_count;
2142
2143 if (adapter->vlgrp)
2144 max_frame_size += VLAN_TAG_SIZE;
2145
2146 /* if vfs are enabled we set RLPML to the largest possible request
2147 * size and set the VMOLR RLPML to the size we need */
2148 if (pf_id) {
2149 igb_set_vf_rlpml(adapter, max_frame_size, pf_id);
2150 max_frame_size = MAX_STD_JUMBO_FRAME_SIZE + VLAN_TAG_SIZE;
2151 }
2152
2153 wr32(E1000_RLPML, max_frame_size);
2154}
2155
2156/**
2157 * igb_configure_vt_default_pool - Configure VT default pool
2158 * @adapter: board private structure
2159 *
2160 * Configure the default pool
2161 **/
2162static void igb_configure_vt_default_pool(struct igb_adapter *adapter)
2163{
2164 struct e1000_hw *hw = &adapter->hw;
2165 u16 pf_id = adapter->vfs_allocated_count;
2166 u32 vtctl;
2167
2168 /* not in sr-iov mode - do nothing */
2169 if (!pf_id)
2170 return;
2171
2172 vtctl = rd32(E1000_VT_CTL);
2173 vtctl &= ~(E1000_VT_CTL_DEFAULT_POOL_MASK |
2174 E1000_VT_CTL_DISABLE_DEF_POOL);
2175 vtctl |= pf_id << E1000_VT_CTL_DEFAULT_POOL_SHIFT;
2176 wr32(E1000_VT_CTL, vtctl);
2177}
2178
2179/**
Auke Kok9d5c8242008-01-24 02:22:38 -08002180 * igb_configure_rx - Configure receive Unit after Reset
2181 * @adapter: board private structure
2182 *
2183 * Configure the Rx unit of the MAC after a reset.
2184 **/
2185static void igb_configure_rx(struct igb_adapter *adapter)
2186{
2187 u64 rdba;
2188 struct e1000_hw *hw = &adapter->hw;
2189 u32 rctl, rxcsum;
2190 u32 rxdctl;
Hannes Eder91075842009-02-18 19:36:04 -08002191 int i;
Auke Kok9d5c8242008-01-24 02:22:38 -08002192
2193 /* disable receives while setting up the descriptors */
2194 rctl = rd32(E1000_RCTL);
2195 wr32(E1000_RCTL, rctl & ~E1000_RCTL_EN);
2196 wrfl();
2197 mdelay(10);
2198
2199 if (adapter->itr_setting > 3)
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07002200 wr32(E1000_ITR, adapter->itr);
Auke Kok9d5c8242008-01-24 02:22:38 -08002201
2202 /* Setup the HW Rx Head and Tail Descriptor Pointers and
2203 * the Base and Length of the Rx Descriptor Ring */
2204 for (i = 0; i < adapter->num_rx_queues; i++) {
Alexander Duyck73cd78f2009-02-12 18:16:59 +00002205 struct igb_ring *ring = &adapter->rx_ring[i];
Hannes Eder91075842009-02-18 19:36:04 -08002206 int j = ring->reg_idx;
Auke Kok9d5c8242008-01-24 02:22:38 -08002207 rdba = ring->dma;
Alexander Duyck26bc19e2008-12-26 01:34:11 -08002208 wr32(E1000_RDBAL(j),
Alexander Duyck73cd78f2009-02-12 18:16:59 +00002209 rdba & 0x00000000ffffffffULL);
Alexander Duyck26bc19e2008-12-26 01:34:11 -08002210 wr32(E1000_RDBAH(j), rdba >> 32);
2211 wr32(E1000_RDLEN(j),
Alexander Duyck73cd78f2009-02-12 18:16:59 +00002212 ring->count * sizeof(union e1000_adv_rx_desc));
Auke Kok9d5c8242008-01-24 02:22:38 -08002213
Alexander Duyck26bc19e2008-12-26 01:34:11 -08002214 ring->head = E1000_RDH(j);
2215 ring->tail = E1000_RDT(j);
Auke Kok9d5c8242008-01-24 02:22:38 -08002216 writel(0, hw->hw_addr + ring->tail);
2217 writel(0, hw->hw_addr + ring->head);
2218
Alexander Duyck26bc19e2008-12-26 01:34:11 -08002219 rxdctl = rd32(E1000_RXDCTL(j));
Auke Kok9d5c8242008-01-24 02:22:38 -08002220 rxdctl |= E1000_RXDCTL_QUEUE_ENABLE;
2221 rxdctl &= 0xFFF00000;
2222 rxdctl |= IGB_RX_PTHRESH;
2223 rxdctl |= IGB_RX_HTHRESH << 8;
2224 rxdctl |= IGB_RX_WTHRESH << 16;
Alexander Duyck26bc19e2008-12-26 01:34:11 -08002225 wr32(E1000_RXDCTL(j), rxdctl);
Auke Kok9d5c8242008-01-24 02:22:38 -08002226 }
2227
2228 if (adapter->num_rx_queues > 1) {
2229 u32 random[10];
2230 u32 mrqc;
2231 u32 j, shift;
2232 union e1000_reta {
2233 u32 dword;
2234 u8 bytes[4];
2235 } reta;
2236
2237 get_random_bytes(&random[0], 40);
2238
Alexander Duyck2d064c02008-07-08 15:10:12 -07002239 if (hw->mac.type >= e1000_82576)
2240 shift = 0;
2241 else
2242 shift = 6;
Auke Kok9d5c8242008-01-24 02:22:38 -08002243 for (j = 0; j < (32 * 4); j++) {
2244 reta.bytes[j & 3] =
Alexander Duyck26bc19e2008-12-26 01:34:11 -08002245 adapter->rx_ring[(j % adapter->num_rx_queues)].reg_idx << shift;
Auke Kok9d5c8242008-01-24 02:22:38 -08002246 if ((j & 3) == 3)
2247 writel(reta.dword,
2248 hw->hw_addr + E1000_RETA(0) + (j & ~3));
2249 }
Alexander Duycke1739522009-02-19 20:39:44 -08002250 if (adapter->vfs_allocated_count)
2251 mrqc = E1000_MRQC_ENABLE_VMDQ_RSS_2Q;
2252 else
2253 mrqc = E1000_MRQC_ENABLE_RSS_4Q;
Auke Kok9d5c8242008-01-24 02:22:38 -08002254
2255 /* Fill out hash function seeds */
2256 for (j = 0; j < 10; j++)
2257 array_wr32(E1000_RSSRK(0), j, random[j]);
2258
2259 mrqc |= (E1000_MRQC_RSS_FIELD_IPV4 |
2260 E1000_MRQC_RSS_FIELD_IPV4_TCP);
2261 mrqc |= (E1000_MRQC_RSS_FIELD_IPV6 |
2262 E1000_MRQC_RSS_FIELD_IPV6_TCP);
2263 mrqc |= (E1000_MRQC_RSS_FIELD_IPV4_UDP |
2264 E1000_MRQC_RSS_FIELD_IPV6_UDP);
2265 mrqc |= (E1000_MRQC_RSS_FIELD_IPV6_UDP_EX |
2266 E1000_MRQC_RSS_FIELD_IPV6_TCP_EX);
2267
Auke Kok9d5c8242008-01-24 02:22:38 -08002268 wr32(E1000_MRQC, mrqc);
Alexander Duyck2844f792009-04-27 22:35:14 +00002269 } else if (adapter->vfs_allocated_count) {
Alexander Duycke1739522009-02-19 20:39:44 -08002270 /* Enable multi-queue for sr-iov */
Alexander Duyck2844f792009-04-27 22:35:14 +00002271 wr32(E1000_MRQC, E1000_MRQC_ENABLE_VMDQ);
Auke Kok9d5c8242008-01-24 02:22:38 -08002272 }
2273
Alexander Duyck2844f792009-04-27 22:35:14 +00002274 /* Enable Receive Checksum Offload for TCP and UDP */
2275 rxcsum = rd32(E1000_RXCSUM);
2276 /* Disable raw packet checksumming */
2277 rxcsum |= E1000_RXCSUM_PCSD;
Alexander Duyck7beb0142009-05-06 10:25:23 +00002278
2279 if (adapter->hw.mac.type == e1000_82576)
Jesse Brandeburgb9473562009-04-27 22:36:13 +00002280 /* Enable Receive Checksum Offload for SCTP */
2281 rxcsum |= E1000_RXCSUM_CRCOFL;
2282
Alexander Duyck7beb0142009-05-06 10:25:23 +00002283 /* Don't need to set TUOFL or IPOFL, they default to 1 */
Alexander Duyck2844f792009-04-27 22:35:14 +00002284 wr32(E1000_RXCSUM, rxcsum);
2285
Alexander Duycke1739522009-02-19 20:39:44 -08002286 /* Set the default pool for the PF's first queue */
2287 igb_configure_vt_default_pool(adapter);
2288
Alexander Duyck68d480c2009-10-05 06:33:08 +00002289 /* set UTA to appropriate mode */
2290 igb_set_uta(adapter);
2291
Alexander Duyck26ad9172009-10-05 06:32:49 +00002292 /* set the correct pool for the PF default MAC address in entry 0 */
2293 igb_rar_set_qsel(adapter, adapter->hw.mac.addr, 0,
2294 adapter->vfs_allocated_count);
2295
Alexander Duycke1739522009-02-19 20:39:44 -08002296 igb_rlpml_set(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08002297
2298 /* Enable Receives */
2299 wr32(E1000_RCTL, rctl);
2300}
2301
2302/**
2303 * igb_free_tx_resources - Free Tx Resources per Queue
Auke Kok9d5c8242008-01-24 02:22:38 -08002304 * @tx_ring: Tx descriptor ring for a specific queue
2305 *
2306 * Free all transmit software resources
2307 **/
Alexander Duyck68fd9912008-11-20 00:48:10 -08002308void igb_free_tx_resources(struct igb_ring *tx_ring)
Auke Kok9d5c8242008-01-24 02:22:38 -08002309{
Mitch Williams3b644cf2008-06-27 10:59:48 -07002310 struct pci_dev *pdev = tx_ring->adapter->pdev;
Auke Kok9d5c8242008-01-24 02:22:38 -08002311
Mitch Williams3b644cf2008-06-27 10:59:48 -07002312 igb_clean_tx_ring(tx_ring);
Auke Kok9d5c8242008-01-24 02:22:38 -08002313
2314 vfree(tx_ring->buffer_info);
2315 tx_ring->buffer_info = NULL;
2316
2317 pci_free_consistent(pdev, tx_ring->size, tx_ring->desc, tx_ring->dma);
2318
2319 tx_ring->desc = NULL;
2320}
2321
2322/**
2323 * igb_free_all_tx_resources - Free Tx Resources for All Queues
2324 * @adapter: board private structure
2325 *
2326 * Free all transmit software resources
2327 **/
2328static void igb_free_all_tx_resources(struct igb_adapter *adapter)
2329{
2330 int i;
2331
2332 for (i = 0; i < adapter->num_tx_queues; i++)
Mitch Williams3b644cf2008-06-27 10:59:48 -07002333 igb_free_tx_resources(&adapter->tx_ring[i]);
Auke Kok9d5c8242008-01-24 02:22:38 -08002334}
2335
2336static void igb_unmap_and_free_tx_resource(struct igb_adapter *adapter,
2337 struct igb_buffer *buffer_info)
2338{
Alexander Duyck65689fe2009-03-20 00:17:43 +00002339 buffer_info->dma = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08002340 if (buffer_info->skb) {
Alexander Duyck65689fe2009-03-20 00:17:43 +00002341 skb_dma_unmap(&adapter->pdev->dev, buffer_info->skb,
2342 DMA_TO_DEVICE);
Auke Kok9d5c8242008-01-24 02:22:38 -08002343 dev_kfree_skb_any(buffer_info->skb);
2344 buffer_info->skb = NULL;
2345 }
2346 buffer_info->time_stamp = 0;
2347 /* buffer_info must be completely set up in the transmit path */
2348}
2349
2350/**
2351 * igb_clean_tx_ring - Free Tx Buffers
Auke Kok9d5c8242008-01-24 02:22:38 -08002352 * @tx_ring: ring to be cleaned
2353 **/
Mitch Williams3b644cf2008-06-27 10:59:48 -07002354static void igb_clean_tx_ring(struct igb_ring *tx_ring)
Auke Kok9d5c8242008-01-24 02:22:38 -08002355{
Mitch Williams3b644cf2008-06-27 10:59:48 -07002356 struct igb_adapter *adapter = tx_ring->adapter;
Auke Kok9d5c8242008-01-24 02:22:38 -08002357 struct igb_buffer *buffer_info;
2358 unsigned long size;
2359 unsigned int i;
2360
2361 if (!tx_ring->buffer_info)
2362 return;
2363 /* Free all the Tx ring sk_buffs */
2364
2365 for (i = 0; i < tx_ring->count; i++) {
2366 buffer_info = &tx_ring->buffer_info[i];
2367 igb_unmap_and_free_tx_resource(adapter, buffer_info);
2368 }
2369
2370 size = sizeof(struct igb_buffer) * tx_ring->count;
2371 memset(tx_ring->buffer_info, 0, size);
2372
2373 /* Zero out the descriptor ring */
2374
2375 memset(tx_ring->desc, 0, tx_ring->size);
2376
2377 tx_ring->next_to_use = 0;
2378 tx_ring->next_to_clean = 0;
2379
2380 writel(0, adapter->hw.hw_addr + tx_ring->head);
2381 writel(0, adapter->hw.hw_addr + tx_ring->tail);
2382}
2383
2384/**
2385 * igb_clean_all_tx_rings - Free Tx Buffers for all queues
2386 * @adapter: board private structure
2387 **/
2388static void igb_clean_all_tx_rings(struct igb_adapter *adapter)
2389{
2390 int i;
2391
2392 for (i = 0; i < adapter->num_tx_queues; i++)
Mitch Williams3b644cf2008-06-27 10:59:48 -07002393 igb_clean_tx_ring(&adapter->tx_ring[i]);
Auke Kok9d5c8242008-01-24 02:22:38 -08002394}
2395
2396/**
2397 * igb_free_rx_resources - Free Rx Resources
Auke Kok9d5c8242008-01-24 02:22:38 -08002398 * @rx_ring: ring to clean the resources from
2399 *
2400 * Free all receive software resources
2401 **/
Alexander Duyck68fd9912008-11-20 00:48:10 -08002402void igb_free_rx_resources(struct igb_ring *rx_ring)
Auke Kok9d5c8242008-01-24 02:22:38 -08002403{
Mitch Williams3b644cf2008-06-27 10:59:48 -07002404 struct pci_dev *pdev = rx_ring->adapter->pdev;
Auke Kok9d5c8242008-01-24 02:22:38 -08002405
Mitch Williams3b644cf2008-06-27 10:59:48 -07002406 igb_clean_rx_ring(rx_ring);
Auke Kok9d5c8242008-01-24 02:22:38 -08002407
2408 vfree(rx_ring->buffer_info);
2409 rx_ring->buffer_info = NULL;
2410
2411 pci_free_consistent(pdev, rx_ring->size, rx_ring->desc, rx_ring->dma);
2412
2413 rx_ring->desc = NULL;
2414}
2415
2416/**
2417 * igb_free_all_rx_resources - Free Rx Resources for All Queues
2418 * @adapter: board private structure
2419 *
2420 * Free all receive software resources
2421 **/
2422static void igb_free_all_rx_resources(struct igb_adapter *adapter)
2423{
2424 int i;
2425
2426 for (i = 0; i < adapter->num_rx_queues; i++)
Mitch Williams3b644cf2008-06-27 10:59:48 -07002427 igb_free_rx_resources(&adapter->rx_ring[i]);
Auke Kok9d5c8242008-01-24 02:22:38 -08002428}
2429
2430/**
2431 * igb_clean_rx_ring - Free Rx Buffers per Queue
Auke Kok9d5c8242008-01-24 02:22:38 -08002432 * @rx_ring: ring to free buffers from
2433 **/
Mitch Williams3b644cf2008-06-27 10:59:48 -07002434static void igb_clean_rx_ring(struct igb_ring *rx_ring)
Auke Kok9d5c8242008-01-24 02:22:38 -08002435{
Mitch Williams3b644cf2008-06-27 10:59:48 -07002436 struct igb_adapter *adapter = rx_ring->adapter;
Auke Kok9d5c8242008-01-24 02:22:38 -08002437 struct igb_buffer *buffer_info;
2438 struct pci_dev *pdev = adapter->pdev;
2439 unsigned long size;
2440 unsigned int i;
2441
2442 if (!rx_ring->buffer_info)
2443 return;
2444 /* Free all the Rx ring sk_buffs */
2445 for (i = 0; i < rx_ring->count; i++) {
2446 buffer_info = &rx_ring->buffer_info[i];
2447 if (buffer_info->dma) {
2448 if (adapter->rx_ps_hdr_size)
2449 pci_unmap_single(pdev, buffer_info->dma,
2450 adapter->rx_ps_hdr_size,
2451 PCI_DMA_FROMDEVICE);
2452 else
2453 pci_unmap_single(pdev, buffer_info->dma,
2454 adapter->rx_buffer_len,
2455 PCI_DMA_FROMDEVICE);
2456 buffer_info->dma = 0;
2457 }
2458
2459 if (buffer_info->skb) {
2460 dev_kfree_skb(buffer_info->skb);
2461 buffer_info->skb = NULL;
2462 }
2463 if (buffer_info->page) {
Alexander Duyckbf36c1a2008-07-08 15:11:40 -07002464 if (buffer_info->page_dma)
2465 pci_unmap_page(pdev, buffer_info->page_dma,
2466 PAGE_SIZE / 2,
2467 PCI_DMA_FROMDEVICE);
Auke Kok9d5c8242008-01-24 02:22:38 -08002468 put_page(buffer_info->page);
2469 buffer_info->page = NULL;
2470 buffer_info->page_dma = 0;
Alexander Duyckbf36c1a2008-07-08 15:11:40 -07002471 buffer_info->page_offset = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08002472 }
2473 }
2474
Auke Kok9d5c8242008-01-24 02:22:38 -08002475 size = sizeof(struct igb_buffer) * rx_ring->count;
2476 memset(rx_ring->buffer_info, 0, size);
2477
2478 /* Zero out the descriptor ring */
2479 memset(rx_ring->desc, 0, rx_ring->size);
2480
2481 rx_ring->next_to_clean = 0;
2482 rx_ring->next_to_use = 0;
2483
2484 writel(0, adapter->hw.hw_addr + rx_ring->head);
2485 writel(0, adapter->hw.hw_addr + rx_ring->tail);
2486}
2487
2488/**
2489 * igb_clean_all_rx_rings - Free Rx Buffers for all queues
2490 * @adapter: board private structure
2491 **/
2492static void igb_clean_all_rx_rings(struct igb_adapter *adapter)
2493{
2494 int i;
2495
2496 for (i = 0; i < adapter->num_rx_queues; i++)
Mitch Williams3b644cf2008-06-27 10:59:48 -07002497 igb_clean_rx_ring(&adapter->rx_ring[i]);
Auke Kok9d5c8242008-01-24 02:22:38 -08002498}
2499
2500/**
2501 * igb_set_mac - Change the Ethernet Address of the NIC
2502 * @netdev: network interface device structure
2503 * @p: pointer to an address structure
2504 *
2505 * Returns 0 on success, negative on failure
2506 **/
2507static int igb_set_mac(struct net_device *netdev, void *p)
2508{
2509 struct igb_adapter *adapter = netdev_priv(netdev);
Alexander Duyck28b07592009-02-06 23:20:31 +00002510 struct e1000_hw *hw = &adapter->hw;
Auke Kok9d5c8242008-01-24 02:22:38 -08002511 struct sockaddr *addr = p;
2512
2513 if (!is_valid_ether_addr(addr->sa_data))
2514 return -EADDRNOTAVAIL;
2515
2516 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
Alexander Duyck28b07592009-02-06 23:20:31 +00002517 memcpy(hw->mac.addr, addr->sa_data, netdev->addr_len);
Auke Kok9d5c8242008-01-24 02:22:38 -08002518
Alexander Duyck26ad9172009-10-05 06:32:49 +00002519 /* set the correct pool for the new PF MAC address in entry 0 */
2520 igb_rar_set_qsel(adapter, hw->mac.addr, 0,
2521 adapter->vfs_allocated_count);
Alexander Duycke1739522009-02-19 20:39:44 -08002522
Auke Kok9d5c8242008-01-24 02:22:38 -08002523 return 0;
2524}
2525
2526/**
Alexander Duyck68d480c2009-10-05 06:33:08 +00002527 * igb_write_mc_addr_list - write multicast addresses to MTA
2528 * @netdev: network interface device structure
2529 *
2530 * Writes multicast address list to the MTA hash table.
2531 * Returns: -ENOMEM on failure
2532 * 0 on no addresses written
2533 * X on writing X addresses to MTA
2534 **/
2535static int igb_write_mc_addr_list(struct net_device *netdev)
2536{
2537 struct igb_adapter *adapter = netdev_priv(netdev);
2538 struct e1000_hw *hw = &adapter->hw;
2539 struct dev_mc_list *mc_ptr = netdev->mc_list;
2540 u8 *mta_list;
2541 u32 vmolr = 0;
2542 int i;
2543
2544 if (!netdev->mc_count) {
2545 /* nothing to program, so clear mc list */
2546 igb_update_mc_addr_list(hw, NULL, 0);
2547 igb_restore_vf_multicasts(adapter);
2548 return 0;
2549 }
2550
2551 mta_list = kzalloc(netdev->mc_count * 6, GFP_ATOMIC);
2552 if (!mta_list)
2553 return -ENOMEM;
2554
2555 /* set vmolr receive overflow multicast bit */
2556 vmolr |= E1000_VMOLR_ROMPE;
2557
2558 /* The shared function expects a packed array of only addresses. */
2559 mc_ptr = netdev->mc_list;
2560
2561 for (i = 0; i < netdev->mc_count; i++) {
2562 if (!mc_ptr)
2563 break;
2564 memcpy(mta_list + (i*ETH_ALEN), mc_ptr->dmi_addr, ETH_ALEN);
2565 mc_ptr = mc_ptr->next;
2566 }
2567 igb_update_mc_addr_list(hw, mta_list, i);
2568 kfree(mta_list);
2569
2570 return netdev->mc_count;
2571}
2572
2573/**
2574 * igb_write_uc_addr_list - write unicast addresses to RAR table
2575 * @netdev: network interface device structure
2576 *
2577 * Writes unicast address list to the RAR table.
2578 * Returns: -ENOMEM on failure/insufficient address space
2579 * 0 on no addresses written
2580 * X on writing X addresses to the RAR table
2581 **/
2582static int igb_write_uc_addr_list(struct net_device *netdev)
2583{
2584 struct igb_adapter *adapter = netdev_priv(netdev);
2585 struct e1000_hw *hw = &adapter->hw;
2586 unsigned int vfn = adapter->vfs_allocated_count;
2587 unsigned int rar_entries = hw->mac.rar_entry_count - (vfn + 1);
2588 int count = 0;
2589
2590 /* return ENOMEM indicating insufficient memory for addresses */
2591 if (netdev->uc.count > rar_entries)
2592 return -ENOMEM;
2593
2594 if (netdev->uc.count && rar_entries) {
2595 struct netdev_hw_addr *ha;
2596 list_for_each_entry(ha, &netdev->uc.list, list) {
2597 if (!rar_entries)
2598 break;
2599 igb_rar_set_qsel(adapter, ha->addr,
2600 rar_entries--,
2601 vfn);
2602 count++;
2603 }
2604 }
2605 /* write the addresses in reverse order to avoid write combining */
2606 for (; rar_entries > 0 ; rar_entries--) {
2607 wr32(E1000_RAH(rar_entries), 0);
2608 wr32(E1000_RAL(rar_entries), 0);
2609 }
2610 wrfl();
2611
2612 return count;
2613}
2614
2615/**
Alexander Duyckff41f8d2009-09-03 14:48:56 +00002616 * igb_set_rx_mode - Secondary Unicast, Multicast and Promiscuous mode set
Auke Kok9d5c8242008-01-24 02:22:38 -08002617 * @netdev: network interface device structure
2618 *
Alexander Duyckff41f8d2009-09-03 14:48:56 +00002619 * The set_rx_mode entry point is called whenever the unicast or multicast
2620 * address lists or the network interface flags are updated. This routine is
2621 * responsible for configuring the hardware for proper unicast, multicast,
Auke Kok9d5c8242008-01-24 02:22:38 -08002622 * promiscuous mode, and all-multi behavior.
2623 **/
Alexander Duyckff41f8d2009-09-03 14:48:56 +00002624static void igb_set_rx_mode(struct net_device *netdev)
Auke Kok9d5c8242008-01-24 02:22:38 -08002625{
2626 struct igb_adapter *adapter = netdev_priv(netdev);
2627 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck68d480c2009-10-05 06:33:08 +00002628 unsigned int vfn = adapter->vfs_allocated_count;
2629 u32 rctl, vmolr = 0;
2630 int count;
Auke Kok9d5c8242008-01-24 02:22:38 -08002631
2632 /* Check for Promiscuous and All Multicast modes */
Auke Kok9d5c8242008-01-24 02:22:38 -08002633 rctl = rd32(E1000_RCTL);
2634
Alexander Duyck68d480c2009-10-05 06:33:08 +00002635 /* clear the effected bits */
2636 rctl &= ~(E1000_RCTL_UPE | E1000_RCTL_MPE | E1000_RCTL_VFE);
2637
Patrick McHardy746b9f02008-07-16 20:15:45 -07002638 if (netdev->flags & IFF_PROMISC) {
Auke Kok9d5c8242008-01-24 02:22:38 -08002639 rctl |= (E1000_RCTL_UPE | E1000_RCTL_MPE);
Alexander Duyck68d480c2009-10-05 06:33:08 +00002640 vmolr |= (E1000_VMOLR_ROPE | E1000_VMOLR_MPME);
Patrick McHardy746b9f02008-07-16 20:15:45 -07002641 } else {
Alexander Duyck68d480c2009-10-05 06:33:08 +00002642 if (netdev->flags & IFF_ALLMULTI) {
Patrick McHardy746b9f02008-07-16 20:15:45 -07002643 rctl |= E1000_RCTL_MPE;
Alexander Duyck68d480c2009-10-05 06:33:08 +00002644 vmolr |= E1000_VMOLR_MPME;
2645 } else {
2646 /*
2647 * Write addresses to the MTA, if the attempt fails
2648 * then we should just turn on promiscous mode so
2649 * that we can at least receive multicast traffic
2650 */
2651 count = igb_write_mc_addr_list(netdev);
2652 if (count < 0) {
2653 rctl |= E1000_RCTL_MPE;
2654 vmolr |= E1000_VMOLR_MPME;
2655 } else if (count) {
2656 vmolr |= E1000_VMOLR_ROMPE;
2657 }
2658 }
2659 /*
2660 * Write addresses to available RAR registers, if there is not
2661 * sufficient space to store all the addresses then enable
2662 * unicast promiscous mode
2663 */
2664 count = igb_write_uc_addr_list(netdev);
2665 if (count < 0) {
Alexander Duyckff41f8d2009-09-03 14:48:56 +00002666 rctl |= E1000_RCTL_UPE;
Alexander Duyck68d480c2009-10-05 06:33:08 +00002667 vmolr |= E1000_VMOLR_ROPE;
2668 }
Patrick McHardy78ed11a2008-07-16 20:16:14 -07002669 rctl |= E1000_RCTL_VFE;
Patrick McHardy746b9f02008-07-16 20:15:45 -07002670 }
Auke Kok9d5c8242008-01-24 02:22:38 -08002671 wr32(E1000_RCTL, rctl);
2672
Alexander Duyck68d480c2009-10-05 06:33:08 +00002673 /*
2674 * In order to support SR-IOV and eventually VMDq it is necessary to set
2675 * the VMOLR to enable the appropriate modes. Without this workaround
2676 * we will have issues with VLAN tag stripping not being done for frames
2677 * that are only arriving because we are the default pool
2678 */
2679 if (hw->mac.type < e1000_82576)
Alexander Duyck28fc06f2009-07-23 18:08:54 +00002680 return;
Alexander Duyck28fc06f2009-07-23 18:08:54 +00002681
Alexander Duyck68d480c2009-10-05 06:33:08 +00002682 vmolr |= rd32(E1000_VMOLR(vfn)) &
2683 ~(E1000_VMOLR_ROPE | E1000_VMOLR_MPME | E1000_VMOLR_ROMPE);
2684 wr32(E1000_VMOLR(vfn), vmolr);
Alexander Duyck28fc06f2009-07-23 18:08:54 +00002685 igb_restore_vf_multicasts(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08002686}
2687
2688/* Need to wait a few seconds after link up to get diagnostic information from
2689 * the phy */
2690static void igb_update_phy_info(unsigned long data)
2691{
2692 struct igb_adapter *adapter = (struct igb_adapter *) data;
Alexander Duyckf5f4cf02008-11-21 21:30:24 -08002693 igb_get_phy_info(&adapter->hw);
Auke Kok9d5c8242008-01-24 02:22:38 -08002694}
2695
2696/**
Alexander Duyck4d6b7252009-02-06 23:16:24 +00002697 * igb_has_link - check shared code for link and determine up/down
2698 * @adapter: pointer to driver private info
2699 **/
2700static bool igb_has_link(struct igb_adapter *adapter)
2701{
2702 struct e1000_hw *hw = &adapter->hw;
2703 bool link_active = false;
2704 s32 ret_val = 0;
2705
2706 /* get_link_status is set on LSC (link status) interrupt or
2707 * rx sequence error interrupt. get_link_status will stay
2708 * false until the e1000_check_for_link establishes link
2709 * for copper adapters ONLY
2710 */
2711 switch (hw->phy.media_type) {
2712 case e1000_media_type_copper:
2713 if (hw->mac.get_link_status) {
2714 ret_val = hw->mac.ops.check_for_link(hw);
2715 link_active = !hw->mac.get_link_status;
2716 } else {
2717 link_active = true;
2718 }
2719 break;
Alexander Duyck4d6b7252009-02-06 23:16:24 +00002720 case e1000_media_type_internal_serdes:
2721 ret_val = hw->mac.ops.check_for_link(hw);
2722 link_active = hw->mac.serdes_has_link;
2723 break;
2724 default:
2725 case e1000_media_type_unknown:
2726 break;
2727 }
2728
2729 return link_active;
2730}
2731
2732/**
Auke Kok9d5c8242008-01-24 02:22:38 -08002733 * igb_watchdog - Timer Call-back
2734 * @data: pointer to adapter cast into an unsigned long
2735 **/
2736static void igb_watchdog(unsigned long data)
2737{
2738 struct igb_adapter *adapter = (struct igb_adapter *)data;
2739 /* Do the rest outside of interrupt context */
2740 schedule_work(&adapter->watchdog_task);
2741}
2742
2743static void igb_watchdog_task(struct work_struct *work)
2744{
2745 struct igb_adapter *adapter = container_of(work,
2746 struct igb_adapter, watchdog_task);
2747 struct e1000_hw *hw = &adapter->hw;
Auke Kok9d5c8242008-01-24 02:22:38 -08002748 struct net_device *netdev = adapter->netdev;
2749 struct igb_ring *tx_ring = adapter->tx_ring;
Auke Kok9d5c8242008-01-24 02:22:38 -08002750 u32 link;
Alexander Duyck7a6ea552008-08-26 04:25:03 -07002751 u32 eics = 0;
Alexander Duyck7a6ea552008-08-26 04:25:03 -07002752 int i;
Auke Kok9d5c8242008-01-24 02:22:38 -08002753
Alexander Duyck4d6b7252009-02-06 23:16:24 +00002754 link = igb_has_link(adapter);
2755 if ((netif_carrier_ok(netdev)) && link)
Auke Kok9d5c8242008-01-24 02:22:38 -08002756 goto link_up;
2757
Auke Kok9d5c8242008-01-24 02:22:38 -08002758 if (link) {
2759 if (!netif_carrier_ok(netdev)) {
2760 u32 ctrl;
2761 hw->mac.ops.get_speed_and_duplex(&adapter->hw,
2762 &adapter->link_speed,
2763 &adapter->link_duplex);
2764
2765 ctrl = rd32(E1000_CTRL);
Alexander Duyck527d47c2008-11-27 00:21:39 -08002766 /* Links status message must follow this format */
2767 printk(KERN_INFO "igb: %s NIC Link is Up %d Mbps %s, "
Auke Kok9d5c8242008-01-24 02:22:38 -08002768 "Flow Control: %s\n",
Alexander Duyck527d47c2008-11-27 00:21:39 -08002769 netdev->name,
Auke Kok9d5c8242008-01-24 02:22:38 -08002770 adapter->link_speed,
2771 adapter->link_duplex == FULL_DUPLEX ?
2772 "Full Duplex" : "Half Duplex",
2773 ((ctrl & E1000_CTRL_TFCE) && (ctrl &
2774 E1000_CTRL_RFCE)) ? "RX/TX" : ((ctrl &
2775 E1000_CTRL_RFCE) ? "RX" : ((ctrl &
2776 E1000_CTRL_TFCE) ? "TX" : "None")));
2777
2778 /* tweak tx_queue_len according to speed/duplex and
2779 * adjust the timeout factor */
2780 netdev->tx_queue_len = adapter->tx_queue_len;
2781 adapter->tx_timeout_factor = 1;
2782 switch (adapter->link_speed) {
2783 case SPEED_10:
2784 netdev->tx_queue_len = 10;
2785 adapter->tx_timeout_factor = 14;
2786 break;
2787 case SPEED_100:
2788 netdev->tx_queue_len = 100;
2789 /* maybe add some timeout factor ? */
2790 break;
2791 }
2792
2793 netif_carrier_on(netdev);
Auke Kok9d5c8242008-01-24 02:22:38 -08002794
Alexander Duyck4ae196d2009-02-19 20:40:07 -08002795 igb_ping_all_vfs(adapter);
2796
Alexander Duyck4b1a9872009-02-06 23:19:50 +00002797 /* link state has changed, schedule phy info update */
Auke Kok9d5c8242008-01-24 02:22:38 -08002798 if (!test_bit(__IGB_DOWN, &adapter->state))
2799 mod_timer(&adapter->phy_info_timer,
2800 round_jiffies(jiffies + 2 * HZ));
2801 }
2802 } else {
2803 if (netif_carrier_ok(netdev)) {
2804 adapter->link_speed = 0;
2805 adapter->link_duplex = 0;
Alexander Duyck527d47c2008-11-27 00:21:39 -08002806 /* Links status message must follow this format */
2807 printk(KERN_INFO "igb: %s NIC Link is Down\n",
2808 netdev->name);
Auke Kok9d5c8242008-01-24 02:22:38 -08002809 netif_carrier_off(netdev);
Alexander Duyck4b1a9872009-02-06 23:19:50 +00002810
Alexander Duyck4ae196d2009-02-19 20:40:07 -08002811 igb_ping_all_vfs(adapter);
2812
Alexander Duyck4b1a9872009-02-06 23:19:50 +00002813 /* link state has changed, schedule phy info update */
Auke Kok9d5c8242008-01-24 02:22:38 -08002814 if (!test_bit(__IGB_DOWN, &adapter->state))
2815 mod_timer(&adapter->phy_info_timer,
2816 round_jiffies(jiffies + 2 * HZ));
2817 }
2818 }
2819
2820link_up:
2821 igb_update_stats(adapter);
2822
Alexander Duyck4b1a9872009-02-06 23:19:50 +00002823 hw->mac.tx_packet_delta = adapter->stats.tpt - adapter->tpt_old;
Auke Kok9d5c8242008-01-24 02:22:38 -08002824 adapter->tpt_old = adapter->stats.tpt;
Alexander Duyck4b1a9872009-02-06 23:19:50 +00002825 hw->mac.collision_delta = adapter->stats.colc - adapter->colc_old;
Auke Kok9d5c8242008-01-24 02:22:38 -08002826 adapter->colc_old = adapter->stats.colc;
2827
2828 adapter->gorc = adapter->stats.gorc - adapter->gorc_old;
2829 adapter->gorc_old = adapter->stats.gorc;
2830 adapter->gotc = adapter->stats.gotc - adapter->gotc_old;
2831 adapter->gotc_old = adapter->stats.gotc;
2832
2833 igb_update_adaptive(&adapter->hw);
2834
2835 if (!netif_carrier_ok(netdev)) {
Alexander Duyckc493ea42009-03-20 00:16:50 +00002836 if (igb_desc_unused(tx_ring) + 1 < tx_ring->count) {
Auke Kok9d5c8242008-01-24 02:22:38 -08002837 /* We've lost link, so the controller stops DMA,
2838 * but we've got queued Tx work that's never going
2839 * to get done, so reset controller to flush Tx.
2840 * (Do the reset outside of interrupt context). */
2841 adapter->tx_timeout_count++;
2842 schedule_work(&adapter->reset_task);
Jesse Brandeburgc2d5ab42009-05-07 11:07:35 +00002843 /* return immediately since reset is imminent */
2844 return;
Auke Kok9d5c8242008-01-24 02:22:38 -08002845 }
2846 }
2847
2848 /* Cause software interrupt to ensure rx ring is cleaned */
Alexander Duyck7a6ea552008-08-26 04:25:03 -07002849 if (adapter->msix_entries) {
2850 for (i = 0; i < adapter->num_rx_queues; i++)
2851 eics |= adapter->rx_ring[i].eims_value;
2852 wr32(E1000_EICS, eics);
2853 } else {
2854 wr32(E1000_ICS, E1000_ICS_RXDMT0);
2855 }
Auke Kok9d5c8242008-01-24 02:22:38 -08002856
2857 /* Force detection of hung controller every watchdog period */
2858 tx_ring->detect_tx_hung = true;
2859
2860 /* Reset the timer */
2861 if (!test_bit(__IGB_DOWN, &adapter->state))
2862 mod_timer(&adapter->watchdog_timer,
2863 round_jiffies(jiffies + 2 * HZ));
2864}
2865
2866enum latency_range {
2867 lowest_latency = 0,
2868 low_latency = 1,
2869 bulk_latency = 2,
2870 latency_invalid = 255
2871};
2872
2873
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07002874/**
2875 * igb_update_ring_itr - update the dynamic ITR value based on packet size
2876 *
2877 * Stores a new ITR value based on strictly on packet size. This
2878 * algorithm is less sophisticated than that used in igb_update_itr,
2879 * due to the difficulty of synchronizing statistics across multiple
2880 * receive rings. The divisors and thresholds used by this fuction
2881 * were determined based on theoretical maximum wire speed and testing
2882 * data, in order to minimize response time while increasing bulk
2883 * throughput.
2884 * This functionality is controlled by the InterruptThrottleRate module
2885 * parameter (see igb_param.c)
2886 * NOTE: This function is called only when operating in a multiqueue
2887 * receive environment.
2888 * @rx_ring: pointer to ring
2889 **/
2890static void igb_update_ring_itr(struct igb_ring *rx_ring)
Auke Kok9d5c8242008-01-24 02:22:38 -08002891{
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07002892 int new_val = rx_ring->itr_val;
2893 int avg_wire_size = 0;
2894 struct igb_adapter *adapter = rx_ring->adapter;
Auke Kok9d5c8242008-01-24 02:22:38 -08002895
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07002896 if (!rx_ring->total_packets)
2897 goto clear_counts; /* no packets, so don't do anything */
Auke Kok9d5c8242008-01-24 02:22:38 -08002898
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07002899 /* For non-gigabit speeds, just fix the interrupt rate at 4000
2900 * ints/sec - ITR timer value of 120 ticks.
2901 */
2902 if (adapter->link_speed != SPEED_1000) {
2903 new_val = 120;
2904 goto set_itr_val;
2905 }
2906 avg_wire_size = rx_ring->total_bytes / rx_ring->total_packets;
2907
2908 /* Add 24 bytes to size to account for CRC, preamble, and gap */
2909 avg_wire_size += 24;
2910
2911 /* Don't starve jumbo frames */
2912 avg_wire_size = min(avg_wire_size, 3000);
2913
2914 /* Give a little boost to mid-size frames */
2915 if ((avg_wire_size > 300) && (avg_wire_size < 1200))
2916 new_val = avg_wire_size / 3;
2917 else
2918 new_val = avg_wire_size / 2;
2919
2920set_itr_val:
Auke Kok9d5c8242008-01-24 02:22:38 -08002921 if (new_val != rx_ring->itr_val) {
2922 rx_ring->itr_val = new_val;
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07002923 rx_ring->set_itr = 1;
Auke Kok9d5c8242008-01-24 02:22:38 -08002924 }
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07002925clear_counts:
2926 rx_ring->total_bytes = 0;
2927 rx_ring->total_packets = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08002928}
2929
2930/**
2931 * igb_update_itr - update the dynamic ITR value based on statistics
2932 * Stores a new ITR value based on packets and byte
2933 * counts during the last interrupt. The advantage of per interrupt
2934 * computation is faster updates and more accurate ITR for the current
2935 * traffic pattern. Constants in this function were computed
2936 * based on theoretical maximum wire speed and thresholds were set based
2937 * on testing data as well as attempting to minimize response time
2938 * while increasing bulk throughput.
2939 * this functionality is controlled by the InterruptThrottleRate module
2940 * parameter (see igb_param.c)
2941 * NOTE: These calculations are only valid when operating in a single-
2942 * queue environment.
2943 * @adapter: pointer to adapter
2944 * @itr_setting: current adapter->itr
2945 * @packets: the number of packets during this measurement interval
2946 * @bytes: the number of bytes during this measurement interval
2947 **/
2948static unsigned int igb_update_itr(struct igb_adapter *adapter, u16 itr_setting,
2949 int packets, int bytes)
2950{
2951 unsigned int retval = itr_setting;
2952
2953 if (packets == 0)
2954 goto update_itr_done;
2955
2956 switch (itr_setting) {
2957 case lowest_latency:
2958 /* handle TSO and jumbo frames */
2959 if (bytes/packets > 8000)
2960 retval = bulk_latency;
2961 else if ((packets < 5) && (bytes > 512))
2962 retval = low_latency;
2963 break;
2964 case low_latency: /* 50 usec aka 20000 ints/s */
2965 if (bytes > 10000) {
2966 /* this if handles the TSO accounting */
2967 if (bytes/packets > 8000) {
2968 retval = bulk_latency;
2969 } else if ((packets < 10) || ((bytes/packets) > 1200)) {
2970 retval = bulk_latency;
2971 } else if ((packets > 35)) {
2972 retval = lowest_latency;
2973 }
2974 } else if (bytes/packets > 2000) {
2975 retval = bulk_latency;
2976 } else if (packets <= 2 && bytes < 512) {
2977 retval = lowest_latency;
2978 }
2979 break;
2980 case bulk_latency: /* 250 usec aka 4000 ints/s */
2981 if (bytes > 25000) {
2982 if (packets > 35)
2983 retval = low_latency;
Alexander Duyck1e5c3d22009-02-12 18:17:21 +00002984 } else if (bytes < 1500) {
Auke Kok9d5c8242008-01-24 02:22:38 -08002985 retval = low_latency;
2986 }
2987 break;
2988 }
2989
2990update_itr_done:
2991 return retval;
2992}
2993
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07002994static void igb_set_itr(struct igb_adapter *adapter)
Auke Kok9d5c8242008-01-24 02:22:38 -08002995{
2996 u16 current_itr;
2997 u32 new_itr = adapter->itr;
2998
2999 /* for non-gigabit speeds, just fix the interrupt rate at 4000 */
3000 if (adapter->link_speed != SPEED_1000) {
3001 current_itr = 0;
3002 new_itr = 4000;
3003 goto set_itr_now;
3004 }
3005
3006 adapter->rx_itr = igb_update_itr(adapter,
3007 adapter->rx_itr,
3008 adapter->rx_ring->total_packets,
3009 adapter->rx_ring->total_bytes);
Auke Kok9d5c8242008-01-24 02:22:38 -08003010
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07003011 if (adapter->rx_ring->buddy) {
Auke Kok9d5c8242008-01-24 02:22:38 -08003012 adapter->tx_itr = igb_update_itr(adapter,
3013 adapter->tx_itr,
3014 adapter->tx_ring->total_packets,
3015 adapter->tx_ring->total_bytes);
Auke Kok9d5c8242008-01-24 02:22:38 -08003016 current_itr = max(adapter->rx_itr, adapter->tx_itr);
3017 } else {
3018 current_itr = adapter->rx_itr;
3019 }
3020
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07003021 /* conservative mode (itr 3) eliminates the lowest_latency setting */
Alexander Duyck73cd78f2009-02-12 18:16:59 +00003022 if (adapter->itr_setting == 3 && current_itr == lowest_latency)
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07003023 current_itr = low_latency;
3024
Auke Kok9d5c8242008-01-24 02:22:38 -08003025 switch (current_itr) {
3026 /* counts and packets in update_itr are dependent on these numbers */
3027 case lowest_latency:
Alexander Duyck78b1f6072009-04-23 11:20:29 +00003028 new_itr = 56; /* aka 70,000 ints/sec */
Auke Kok9d5c8242008-01-24 02:22:38 -08003029 break;
3030 case low_latency:
Alexander Duyck78b1f6072009-04-23 11:20:29 +00003031 new_itr = 196; /* aka 20,000 ints/sec */
Auke Kok9d5c8242008-01-24 02:22:38 -08003032 break;
3033 case bulk_latency:
Alexander Duyck78b1f6072009-04-23 11:20:29 +00003034 new_itr = 980; /* aka 4,000 ints/sec */
Auke Kok9d5c8242008-01-24 02:22:38 -08003035 break;
3036 default:
3037 break;
3038 }
3039
3040set_itr_now:
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07003041 adapter->rx_ring->total_bytes = 0;
3042 adapter->rx_ring->total_packets = 0;
3043 if (adapter->rx_ring->buddy) {
3044 adapter->rx_ring->buddy->total_bytes = 0;
3045 adapter->rx_ring->buddy->total_packets = 0;
3046 }
3047
Auke Kok9d5c8242008-01-24 02:22:38 -08003048 if (new_itr != adapter->itr) {
3049 /* this attempts to bias the interrupt rate towards Bulk
3050 * by adding intermediate steps when interrupt rate is
3051 * increasing */
3052 new_itr = new_itr > adapter->itr ?
Alexander Duyck78b1f6072009-04-23 11:20:29 +00003053 max((new_itr * adapter->itr) /
3054 (new_itr + (adapter->itr >> 2)), new_itr) :
Auke Kok9d5c8242008-01-24 02:22:38 -08003055 new_itr;
3056 /* Don't write the value here; it resets the adapter's
3057 * internal timer, and causes us to delay far longer than
3058 * we should between interrupts. Instead, we write the ITR
3059 * value at the beginning of the next interrupt so the timing
3060 * ends up being correct.
3061 */
3062 adapter->itr = new_itr;
Alexander Duyck78b1f6072009-04-23 11:20:29 +00003063 adapter->rx_ring->itr_val = new_itr;
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07003064 adapter->rx_ring->set_itr = 1;
Auke Kok9d5c8242008-01-24 02:22:38 -08003065 }
3066
3067 return;
3068}
3069
3070
3071#define IGB_TX_FLAGS_CSUM 0x00000001
3072#define IGB_TX_FLAGS_VLAN 0x00000002
3073#define IGB_TX_FLAGS_TSO 0x00000004
3074#define IGB_TX_FLAGS_IPV4 0x00000008
Patrick Ohly33af6bc2009-02-12 05:03:43 +00003075#define IGB_TX_FLAGS_TSTAMP 0x00000010
Auke Kok9d5c8242008-01-24 02:22:38 -08003076#define IGB_TX_FLAGS_VLAN_MASK 0xffff0000
3077#define IGB_TX_FLAGS_VLAN_SHIFT 16
3078
3079static inline int igb_tso_adv(struct igb_adapter *adapter,
3080 struct igb_ring *tx_ring,
3081 struct sk_buff *skb, u32 tx_flags, u8 *hdr_len)
3082{
3083 struct e1000_adv_tx_context_desc *context_desc;
3084 unsigned int i;
3085 int err;
3086 struct igb_buffer *buffer_info;
3087 u32 info = 0, tu_cmd = 0;
3088 u32 mss_l4len_idx, l4len;
3089 *hdr_len = 0;
3090
3091 if (skb_header_cloned(skb)) {
3092 err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
3093 if (err)
3094 return err;
3095 }
3096
3097 l4len = tcp_hdrlen(skb);
3098 *hdr_len += l4len;
3099
3100 if (skb->protocol == htons(ETH_P_IP)) {
3101 struct iphdr *iph = ip_hdr(skb);
3102 iph->tot_len = 0;
3103 iph->check = 0;
3104 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
3105 iph->daddr, 0,
3106 IPPROTO_TCP,
3107 0);
3108 } else if (skb_shinfo(skb)->gso_type == SKB_GSO_TCPV6) {
3109 ipv6_hdr(skb)->payload_len = 0;
3110 tcp_hdr(skb)->check = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
3111 &ipv6_hdr(skb)->daddr,
3112 0, IPPROTO_TCP, 0);
3113 }
3114
3115 i = tx_ring->next_to_use;
3116
3117 buffer_info = &tx_ring->buffer_info[i];
3118 context_desc = E1000_TX_CTXTDESC_ADV(*tx_ring, i);
3119 /* VLAN MACLEN IPLEN */
3120 if (tx_flags & IGB_TX_FLAGS_VLAN)
3121 info |= (tx_flags & IGB_TX_FLAGS_VLAN_MASK);
3122 info |= (skb_network_offset(skb) << E1000_ADVTXD_MACLEN_SHIFT);
3123 *hdr_len += skb_network_offset(skb);
3124 info |= skb_network_header_len(skb);
3125 *hdr_len += skb_network_header_len(skb);
3126 context_desc->vlan_macip_lens = cpu_to_le32(info);
3127
3128 /* ADV DTYP TUCMD MKRLOC/ISCSIHEDLEN */
3129 tu_cmd |= (E1000_TXD_CMD_DEXT | E1000_ADVTXD_DTYP_CTXT);
3130
3131 if (skb->protocol == htons(ETH_P_IP))
3132 tu_cmd |= E1000_ADVTXD_TUCMD_IPV4;
3133 tu_cmd |= E1000_ADVTXD_TUCMD_L4T_TCP;
3134
3135 context_desc->type_tucmd_mlhl = cpu_to_le32(tu_cmd);
3136
3137 /* MSS L4LEN IDX */
3138 mss_l4len_idx = (skb_shinfo(skb)->gso_size << E1000_ADVTXD_MSS_SHIFT);
3139 mss_l4len_idx |= (l4len << E1000_ADVTXD_L4LEN_SHIFT);
3140
Alexander Duyck73cd78f2009-02-12 18:16:59 +00003141 /* For 82575, context index must be unique per ring. */
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07003142 if (adapter->flags & IGB_FLAG_NEED_CTX_IDX)
3143 mss_l4len_idx |= tx_ring->queue_index << 4;
Auke Kok9d5c8242008-01-24 02:22:38 -08003144
3145 context_desc->mss_l4len_idx = cpu_to_le32(mss_l4len_idx);
3146 context_desc->seqnum_seed = 0;
3147
3148 buffer_info->time_stamp = jiffies;
Alexander Duyck0e014cb2008-12-26 01:33:18 -08003149 buffer_info->next_to_watch = i;
Auke Kok9d5c8242008-01-24 02:22:38 -08003150 buffer_info->dma = 0;
3151 i++;
3152 if (i == tx_ring->count)
3153 i = 0;
3154
3155 tx_ring->next_to_use = i;
3156
3157 return true;
3158}
3159
3160static inline bool igb_tx_csum_adv(struct igb_adapter *adapter,
3161 struct igb_ring *tx_ring,
3162 struct sk_buff *skb, u32 tx_flags)
3163{
3164 struct e1000_adv_tx_context_desc *context_desc;
3165 unsigned int i;
3166 struct igb_buffer *buffer_info;
3167 u32 info = 0, tu_cmd = 0;
3168
3169 if ((skb->ip_summed == CHECKSUM_PARTIAL) ||
3170 (tx_flags & IGB_TX_FLAGS_VLAN)) {
3171 i = tx_ring->next_to_use;
3172 buffer_info = &tx_ring->buffer_info[i];
3173 context_desc = E1000_TX_CTXTDESC_ADV(*tx_ring, i);
3174
3175 if (tx_flags & IGB_TX_FLAGS_VLAN)
3176 info |= (tx_flags & IGB_TX_FLAGS_VLAN_MASK);
3177 info |= (skb_network_offset(skb) << E1000_ADVTXD_MACLEN_SHIFT);
3178 if (skb->ip_summed == CHECKSUM_PARTIAL)
3179 info |= skb_network_header_len(skb);
3180
3181 context_desc->vlan_macip_lens = cpu_to_le32(info);
3182
3183 tu_cmd |= (E1000_TXD_CMD_DEXT | E1000_ADVTXD_DTYP_CTXT);
3184
3185 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Arthur Jonesfa4a7ef2009-03-21 16:55:07 -07003186 __be16 protocol;
3187
3188 if (skb->protocol == cpu_to_be16(ETH_P_8021Q)) {
3189 const struct vlan_ethhdr *vhdr =
3190 (const struct vlan_ethhdr*)skb->data;
3191
3192 protocol = vhdr->h_vlan_encapsulated_proto;
3193 } else {
3194 protocol = skb->protocol;
3195 }
3196
3197 switch (protocol) {
Harvey Harrison09640e632009-02-01 00:45:17 -08003198 case cpu_to_be16(ETH_P_IP):
Auke Kok9d5c8242008-01-24 02:22:38 -08003199 tu_cmd |= E1000_ADVTXD_TUCMD_IPV4;
Mitch Williams44b0cda2008-03-07 10:32:13 -08003200 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
3201 tu_cmd |= E1000_ADVTXD_TUCMD_L4T_TCP;
Jesse Brandeburgb9473562009-04-27 22:36:13 +00003202 else if (ip_hdr(skb)->protocol == IPPROTO_SCTP)
3203 tu_cmd |= E1000_ADVTXD_TUCMD_L4T_SCTP;
Mitch Williams44b0cda2008-03-07 10:32:13 -08003204 break;
Harvey Harrison09640e632009-02-01 00:45:17 -08003205 case cpu_to_be16(ETH_P_IPV6):
Mitch Williams44b0cda2008-03-07 10:32:13 -08003206 /* XXX what about other V6 headers?? */
3207 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
3208 tu_cmd |= E1000_ADVTXD_TUCMD_L4T_TCP;
Jesse Brandeburgb9473562009-04-27 22:36:13 +00003209 else if (ipv6_hdr(skb)->nexthdr == IPPROTO_SCTP)
3210 tu_cmd |= E1000_ADVTXD_TUCMD_L4T_SCTP;
Mitch Williams44b0cda2008-03-07 10:32:13 -08003211 break;
3212 default:
3213 if (unlikely(net_ratelimit()))
3214 dev_warn(&adapter->pdev->dev,
3215 "partial checksum but proto=%x!\n",
3216 skb->protocol);
3217 break;
3218 }
Auke Kok9d5c8242008-01-24 02:22:38 -08003219 }
3220
3221 context_desc->type_tucmd_mlhl = cpu_to_le32(tu_cmd);
3222 context_desc->seqnum_seed = 0;
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07003223 if (adapter->flags & IGB_FLAG_NEED_CTX_IDX)
3224 context_desc->mss_l4len_idx =
3225 cpu_to_le32(tx_ring->queue_index << 4);
Alexander Duyck265de402009-02-06 23:22:52 +00003226 else
3227 context_desc->mss_l4len_idx = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08003228
3229 buffer_info->time_stamp = jiffies;
Alexander Duyck0e014cb2008-12-26 01:33:18 -08003230 buffer_info->next_to_watch = i;
Auke Kok9d5c8242008-01-24 02:22:38 -08003231 buffer_info->dma = 0;
3232
3233 i++;
3234 if (i == tx_ring->count)
3235 i = 0;
3236 tx_ring->next_to_use = i;
3237
3238 return true;
3239 }
Auke Kok9d5c8242008-01-24 02:22:38 -08003240 return false;
3241}
3242
3243#define IGB_MAX_TXD_PWR 16
3244#define IGB_MAX_DATA_PER_TXD (1<<IGB_MAX_TXD_PWR)
3245
3246static inline int igb_tx_map_adv(struct igb_adapter *adapter,
Alexander Duyck0e014cb2008-12-26 01:33:18 -08003247 struct igb_ring *tx_ring, struct sk_buff *skb,
3248 unsigned int first)
Auke Kok9d5c8242008-01-24 02:22:38 -08003249{
3250 struct igb_buffer *buffer_info;
3251 unsigned int len = skb_headlen(skb);
3252 unsigned int count = 0, i;
3253 unsigned int f;
Alexander Duyck65689fe2009-03-20 00:17:43 +00003254 dma_addr_t *map;
Auke Kok9d5c8242008-01-24 02:22:38 -08003255
3256 i = tx_ring->next_to_use;
3257
Alexander Duyck65689fe2009-03-20 00:17:43 +00003258 if (skb_dma_map(&adapter->pdev->dev, skb, DMA_TO_DEVICE)) {
3259 dev_err(&adapter->pdev->dev, "TX DMA map failed\n");
3260 return 0;
3261 }
3262
3263 map = skb_shinfo(skb)->dma_maps;
3264
Auke Kok9d5c8242008-01-24 02:22:38 -08003265 buffer_info = &tx_ring->buffer_info[i];
3266 BUG_ON(len >= IGB_MAX_DATA_PER_TXD);
3267 buffer_info->length = len;
3268 /* set time_stamp *before* dma to help avoid a possible race */
3269 buffer_info->time_stamp = jiffies;
Alexander Duyck0e014cb2008-12-26 01:33:18 -08003270 buffer_info->next_to_watch = i;
Eric Dumazet042a53a2009-06-05 04:04:16 +00003271 buffer_info->dma = skb_shinfo(skb)->dma_head;
Auke Kok9d5c8242008-01-24 02:22:38 -08003272
3273 for (f = 0; f < skb_shinfo(skb)->nr_frags; f++) {
3274 struct skb_frag_struct *frag;
3275
Alexander Duyck65689fe2009-03-20 00:17:43 +00003276 i++;
3277 if (i == tx_ring->count)
3278 i = 0;
3279
Auke Kok9d5c8242008-01-24 02:22:38 -08003280 frag = &skb_shinfo(skb)->frags[f];
3281 len = frag->size;
3282
3283 buffer_info = &tx_ring->buffer_info[i];
3284 BUG_ON(len >= IGB_MAX_DATA_PER_TXD);
3285 buffer_info->length = len;
3286 buffer_info->time_stamp = jiffies;
Alexander Duyck0e014cb2008-12-26 01:33:18 -08003287 buffer_info->next_to_watch = i;
Alexander Duyck65689fe2009-03-20 00:17:43 +00003288 buffer_info->dma = map[count];
Auke Kok9d5c8242008-01-24 02:22:38 -08003289 count++;
Auke Kok9d5c8242008-01-24 02:22:38 -08003290 }
3291
Auke Kok9d5c8242008-01-24 02:22:38 -08003292 tx_ring->buffer_info[i].skb = skb;
Alexander Duyck0e014cb2008-12-26 01:33:18 -08003293 tx_ring->buffer_info[first].next_to_watch = i;
Auke Kok9d5c8242008-01-24 02:22:38 -08003294
Eric Dumazet042a53a2009-06-05 04:04:16 +00003295 return count + 1;
Auke Kok9d5c8242008-01-24 02:22:38 -08003296}
3297
3298static inline void igb_tx_queue_adv(struct igb_adapter *adapter,
3299 struct igb_ring *tx_ring,
3300 int tx_flags, int count, u32 paylen,
3301 u8 hdr_len)
3302{
3303 union e1000_adv_tx_desc *tx_desc = NULL;
3304 struct igb_buffer *buffer_info;
3305 u32 olinfo_status = 0, cmd_type_len;
3306 unsigned int i;
3307
3308 cmd_type_len = (E1000_ADVTXD_DTYP_DATA | E1000_ADVTXD_DCMD_IFCS |
3309 E1000_ADVTXD_DCMD_DEXT);
3310
3311 if (tx_flags & IGB_TX_FLAGS_VLAN)
3312 cmd_type_len |= E1000_ADVTXD_DCMD_VLE;
3313
Patrick Ohly33af6bc2009-02-12 05:03:43 +00003314 if (tx_flags & IGB_TX_FLAGS_TSTAMP)
3315 cmd_type_len |= E1000_ADVTXD_MAC_TSTAMP;
3316
Auke Kok9d5c8242008-01-24 02:22:38 -08003317 if (tx_flags & IGB_TX_FLAGS_TSO) {
3318 cmd_type_len |= E1000_ADVTXD_DCMD_TSE;
3319
3320 /* insert tcp checksum */
3321 olinfo_status |= E1000_TXD_POPTS_TXSM << 8;
3322
3323 /* insert ip checksum */
3324 if (tx_flags & IGB_TX_FLAGS_IPV4)
3325 olinfo_status |= E1000_TXD_POPTS_IXSM << 8;
3326
3327 } else if (tx_flags & IGB_TX_FLAGS_CSUM) {
3328 olinfo_status |= E1000_TXD_POPTS_TXSM << 8;
3329 }
3330
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07003331 if ((adapter->flags & IGB_FLAG_NEED_CTX_IDX) &&
3332 (tx_flags & (IGB_TX_FLAGS_CSUM | IGB_TX_FLAGS_TSO |
3333 IGB_TX_FLAGS_VLAN)))
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07003334 olinfo_status |= tx_ring->queue_index << 4;
Auke Kok9d5c8242008-01-24 02:22:38 -08003335
3336 olinfo_status |= ((paylen - hdr_len) << E1000_ADVTXD_PAYLEN_SHIFT);
3337
3338 i = tx_ring->next_to_use;
3339 while (count--) {
3340 buffer_info = &tx_ring->buffer_info[i];
3341 tx_desc = E1000_TX_DESC_ADV(*tx_ring, i);
3342 tx_desc->read.buffer_addr = cpu_to_le64(buffer_info->dma);
3343 tx_desc->read.cmd_type_len =
3344 cpu_to_le32(cmd_type_len | buffer_info->length);
3345 tx_desc->read.olinfo_status = cpu_to_le32(olinfo_status);
3346 i++;
3347 if (i == tx_ring->count)
3348 i = 0;
3349 }
3350
3351 tx_desc->read.cmd_type_len |= cpu_to_le32(adapter->txd_cmd);
3352 /* Force memory writes to complete before letting h/w
3353 * know there are new descriptors to fetch. (Only
3354 * applicable for weak-ordered memory model archs,
3355 * such as IA-64). */
3356 wmb();
3357
3358 tx_ring->next_to_use = i;
3359 writel(i, adapter->hw.hw_addr + tx_ring->tail);
3360 /* we need this if more than one processor can write to our tail
3361 * at a time, it syncronizes IO on IA64/Altix systems */
3362 mmiowb();
3363}
3364
3365static int __igb_maybe_stop_tx(struct net_device *netdev,
3366 struct igb_ring *tx_ring, int size)
3367{
3368 struct igb_adapter *adapter = netdev_priv(netdev);
3369
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07003370 netif_stop_subqueue(netdev, tx_ring->queue_index);
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07003371
Auke Kok9d5c8242008-01-24 02:22:38 -08003372 /* Herbert's original patch had:
3373 * smp_mb__after_netif_stop_queue();
3374 * but since that doesn't exist yet, just open code it. */
3375 smp_mb();
3376
3377 /* We need to check again in a case another CPU has just
3378 * made room available. */
Alexander Duyckc493ea42009-03-20 00:16:50 +00003379 if (igb_desc_unused(tx_ring) < size)
Auke Kok9d5c8242008-01-24 02:22:38 -08003380 return -EBUSY;
3381
3382 /* A reprieve! */
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07003383 netif_wake_subqueue(netdev, tx_ring->queue_index);
Auke Kok9d5c8242008-01-24 02:22:38 -08003384 ++adapter->restart_queue;
3385 return 0;
3386}
3387
3388static int igb_maybe_stop_tx(struct net_device *netdev,
3389 struct igb_ring *tx_ring, int size)
3390{
Alexander Duyckc493ea42009-03-20 00:16:50 +00003391 if (igb_desc_unused(tx_ring) >= size)
Auke Kok9d5c8242008-01-24 02:22:38 -08003392 return 0;
3393 return __igb_maybe_stop_tx(netdev, tx_ring, size);
3394}
3395
Stephen Hemminger3b29a562009-08-31 19:50:55 +00003396static netdev_tx_t igb_xmit_frame_ring_adv(struct sk_buff *skb,
3397 struct net_device *netdev,
3398 struct igb_ring *tx_ring)
Auke Kok9d5c8242008-01-24 02:22:38 -08003399{
3400 struct igb_adapter *adapter = netdev_priv(netdev);
Alexander Duyck0e014cb2008-12-26 01:33:18 -08003401 unsigned int first;
Auke Kok9d5c8242008-01-24 02:22:38 -08003402 unsigned int tx_flags = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08003403 u8 hdr_len = 0;
Alexander Duyck65689fe2009-03-20 00:17:43 +00003404 int count = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08003405 int tso = 0;
Patrick Ohly33af6bc2009-02-12 05:03:43 +00003406 union skb_shared_tx *shtx;
Auke Kok9d5c8242008-01-24 02:22:38 -08003407
Auke Kok9d5c8242008-01-24 02:22:38 -08003408 if (test_bit(__IGB_DOWN, &adapter->state)) {
3409 dev_kfree_skb_any(skb);
3410 return NETDEV_TX_OK;
3411 }
3412
3413 if (skb->len <= 0) {
3414 dev_kfree_skb_any(skb);
3415 return NETDEV_TX_OK;
3416 }
3417
Auke Kok9d5c8242008-01-24 02:22:38 -08003418 /* need: 1 descriptor per page,
3419 * + 2 desc gap to keep tail from touching head,
3420 * + 1 desc for skb->data,
3421 * + 1 desc for context descriptor,
3422 * otherwise try next time */
3423 if (igb_maybe_stop_tx(netdev, tx_ring, skb_shinfo(skb)->nr_frags + 4)) {
3424 /* this is a hard error */
Auke Kok9d5c8242008-01-24 02:22:38 -08003425 return NETDEV_TX_BUSY;
3426 }
Patrick Ohly33af6bc2009-02-12 05:03:43 +00003427
3428 /*
3429 * TODO: check that there currently is no other packet with
3430 * time stamping in the queue
3431 *
3432 * When doing time stamping, keep the connection to the socket
3433 * a while longer: it is still needed by skb_hwtstamp_tx(),
3434 * called either in igb_tx_hwtstamp() or by our caller when
3435 * doing software time stamping.
3436 */
3437 shtx = skb_tx(skb);
3438 if (unlikely(shtx->hardware)) {
3439 shtx->in_progress = 1;
3440 tx_flags |= IGB_TX_FLAGS_TSTAMP;
Patrick Ohly33af6bc2009-02-12 05:03:43 +00003441 }
Auke Kok9d5c8242008-01-24 02:22:38 -08003442
3443 if (adapter->vlgrp && vlan_tx_tag_present(skb)) {
3444 tx_flags |= IGB_TX_FLAGS_VLAN;
3445 tx_flags |= (vlan_tx_tag_get(skb) << IGB_TX_FLAGS_VLAN_SHIFT);
3446 }
3447
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07003448 if (skb->protocol == htons(ETH_P_IP))
3449 tx_flags |= IGB_TX_FLAGS_IPV4;
3450
Alexander Duyck0e014cb2008-12-26 01:33:18 -08003451 first = tx_ring->next_to_use;
Auke Kok9d5c8242008-01-24 02:22:38 -08003452 tso = skb_is_gso(skb) ? igb_tso_adv(adapter, tx_ring, skb, tx_flags,
3453 &hdr_len) : 0;
3454
3455 if (tso < 0) {
3456 dev_kfree_skb_any(skb);
Auke Kok9d5c8242008-01-24 02:22:38 -08003457 return NETDEV_TX_OK;
3458 }
3459
3460 if (tso)
3461 tx_flags |= IGB_TX_FLAGS_TSO;
Alexander Duyckbc1cbd32009-02-13 14:45:17 +00003462 else if (igb_tx_csum_adv(adapter, tx_ring, skb, tx_flags) &&
3463 (skb->ip_summed == CHECKSUM_PARTIAL))
3464 tx_flags |= IGB_TX_FLAGS_CSUM;
Auke Kok9d5c8242008-01-24 02:22:38 -08003465
Alexander Duyck65689fe2009-03-20 00:17:43 +00003466 /*
3467 * count reflects descriptors mapped, if 0 then mapping error
3468 * has occured and we need to rewind the descriptor queue
3469 */
3470 count = igb_tx_map_adv(adapter, tx_ring, skb, first);
Auke Kok9d5c8242008-01-24 02:22:38 -08003471
Alexander Duyck65689fe2009-03-20 00:17:43 +00003472 if (count) {
3473 igb_tx_queue_adv(adapter, tx_ring, tx_flags, count,
3474 skb->len, hdr_len);
Alexander Duyck65689fe2009-03-20 00:17:43 +00003475 /* Make sure there is space in the ring for the next send. */
3476 igb_maybe_stop_tx(netdev, tx_ring, MAX_SKB_FRAGS + 4);
3477 } else {
3478 dev_kfree_skb_any(skb);
3479 tx_ring->buffer_info[first].time_stamp = 0;
3480 tx_ring->next_to_use = first;
3481 }
Auke Kok9d5c8242008-01-24 02:22:38 -08003482
Auke Kok9d5c8242008-01-24 02:22:38 -08003483 return NETDEV_TX_OK;
3484}
3485
Stephen Hemminger3b29a562009-08-31 19:50:55 +00003486static netdev_tx_t igb_xmit_frame_adv(struct sk_buff *skb,
3487 struct net_device *netdev)
Auke Kok9d5c8242008-01-24 02:22:38 -08003488{
3489 struct igb_adapter *adapter = netdev_priv(netdev);
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07003490 struct igb_ring *tx_ring;
3491
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07003492 int r_idx = 0;
Alexander Duyck1bfaf072009-02-19 20:39:23 -08003493 r_idx = skb->queue_mapping & (IGB_ABS_MAX_TX_QUEUES - 1);
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07003494 tx_ring = adapter->multi_tx_table[r_idx];
Auke Kok9d5c8242008-01-24 02:22:38 -08003495
3496 /* This goes back to the question of how to logically map a tx queue
3497 * to a flow. Right now, performance is impacted slightly negatively
3498 * if using multiple tx queues. If the stack breaks away from a
3499 * single qdisc implementation, we can look at this again. */
Stephen Hemminger3b29a562009-08-31 19:50:55 +00003500 return igb_xmit_frame_ring_adv(skb, netdev, tx_ring);
Auke Kok9d5c8242008-01-24 02:22:38 -08003501}
3502
3503/**
3504 * igb_tx_timeout - Respond to a Tx Hang
3505 * @netdev: network interface device structure
3506 **/
3507static void igb_tx_timeout(struct net_device *netdev)
3508{
3509 struct igb_adapter *adapter = netdev_priv(netdev);
3510 struct e1000_hw *hw = &adapter->hw;
3511
3512 /* Do the reset outside of interrupt context */
3513 adapter->tx_timeout_count++;
3514 schedule_work(&adapter->reset_task);
Alexander Duyck265de402009-02-06 23:22:52 +00003515 wr32(E1000_EICS,
3516 (adapter->eims_enable_mask & ~adapter->eims_other));
Auke Kok9d5c8242008-01-24 02:22:38 -08003517}
3518
3519static void igb_reset_task(struct work_struct *work)
3520{
3521 struct igb_adapter *adapter;
3522 adapter = container_of(work, struct igb_adapter, reset_task);
3523
3524 igb_reinit_locked(adapter);
3525}
3526
3527/**
3528 * igb_get_stats - Get System Network Statistics
3529 * @netdev: network interface device structure
3530 *
3531 * Returns the address of the device statistics structure.
3532 * The statistics are actually updated from the timer callback.
3533 **/
Alexander Duyck73cd78f2009-02-12 18:16:59 +00003534static struct net_device_stats *igb_get_stats(struct net_device *netdev)
Auke Kok9d5c8242008-01-24 02:22:38 -08003535{
3536 struct igb_adapter *adapter = netdev_priv(netdev);
3537
3538 /* only return the current stats */
3539 return &adapter->net_stats;
3540}
3541
3542/**
3543 * igb_change_mtu - Change the Maximum Transfer Unit
3544 * @netdev: network interface device structure
3545 * @new_mtu: new value for maximum frame size
3546 *
3547 * Returns 0 on success, negative on failure
3548 **/
3549static int igb_change_mtu(struct net_device *netdev, int new_mtu)
3550{
3551 struct igb_adapter *adapter = netdev_priv(netdev);
3552 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
3553
3554 if ((max_frame < ETH_ZLEN + ETH_FCS_LEN) ||
3555 (max_frame > MAX_JUMBO_FRAME_SIZE)) {
3556 dev_err(&adapter->pdev->dev, "Invalid MTU setting\n");
3557 return -EINVAL;
3558 }
3559
Auke Kok9d5c8242008-01-24 02:22:38 -08003560 if (max_frame > MAX_STD_JUMBO_FRAME_SIZE) {
3561 dev_err(&adapter->pdev->dev, "MTU > 9216 not supported.\n");
3562 return -EINVAL;
3563 }
3564
3565 while (test_and_set_bit(__IGB_RESETTING, &adapter->state))
3566 msleep(1);
Alexander Duyck73cd78f2009-02-12 18:16:59 +00003567
Auke Kok9d5c8242008-01-24 02:22:38 -08003568 /* igb_down has a dependency on max_frame_size */
3569 adapter->max_frame_size = max_frame;
3570 if (netif_running(netdev))
3571 igb_down(adapter);
3572
3573 /* NOTE: netdev_alloc_skb reserves 16 bytes, and typically NET_IP_ALIGN
3574 * means we reserve 2 more, this pushes us to allocate from the next
3575 * larger slab size.
3576 * i.e. RXBUFFER_2048 --> size-4096 slab
3577 */
3578
3579 if (max_frame <= IGB_RXBUFFER_256)
3580 adapter->rx_buffer_len = IGB_RXBUFFER_256;
3581 else if (max_frame <= IGB_RXBUFFER_512)
3582 adapter->rx_buffer_len = IGB_RXBUFFER_512;
3583 else if (max_frame <= IGB_RXBUFFER_1024)
3584 adapter->rx_buffer_len = IGB_RXBUFFER_1024;
3585 else if (max_frame <= IGB_RXBUFFER_2048)
3586 adapter->rx_buffer_len = IGB_RXBUFFER_2048;
3587 else
Alexander Duyckbf36c1a2008-07-08 15:11:40 -07003588#if (PAGE_SIZE / 2) > IGB_RXBUFFER_16384
3589 adapter->rx_buffer_len = IGB_RXBUFFER_16384;
3590#else
3591 adapter->rx_buffer_len = PAGE_SIZE / 2;
3592#endif
Alexander Duycke1739522009-02-19 20:39:44 -08003593
3594 /* if sr-iov is enabled we need to force buffer size to 1K or larger */
3595 if (adapter->vfs_allocated_count &&
3596 (adapter->rx_buffer_len < IGB_RXBUFFER_1024))
3597 adapter->rx_buffer_len = IGB_RXBUFFER_1024;
3598
Auke Kok9d5c8242008-01-24 02:22:38 -08003599 /* adjust allocation if LPE protects us, and we aren't using SBP */
3600 if ((max_frame == ETH_FRAME_LEN + ETH_FCS_LEN) ||
3601 (max_frame == MAXIMUM_ETHERNET_VLAN_SIZE))
3602 adapter->rx_buffer_len = MAXIMUM_ETHERNET_VLAN_SIZE;
3603
3604 dev_info(&adapter->pdev->dev, "changing MTU from %d to %d\n",
3605 netdev->mtu, new_mtu);
3606 netdev->mtu = new_mtu;
3607
3608 if (netif_running(netdev))
3609 igb_up(adapter);
3610 else
3611 igb_reset(adapter);
3612
3613 clear_bit(__IGB_RESETTING, &adapter->state);
3614
3615 return 0;
3616}
3617
3618/**
3619 * igb_update_stats - Update the board statistics counters
3620 * @adapter: board private structure
3621 **/
3622
3623void igb_update_stats(struct igb_adapter *adapter)
3624{
3625 struct e1000_hw *hw = &adapter->hw;
3626 struct pci_dev *pdev = adapter->pdev;
3627 u16 phy_tmp;
3628
3629#define PHY_IDLE_ERROR_COUNT_MASK 0x00FF
3630
3631 /*
3632 * Prevent stats update while adapter is being reset, or if the pci
3633 * connection is down.
3634 */
3635 if (adapter->link_speed == 0)
3636 return;
3637 if (pci_channel_offline(pdev))
3638 return;
3639
3640 adapter->stats.crcerrs += rd32(E1000_CRCERRS);
3641 adapter->stats.gprc += rd32(E1000_GPRC);
3642 adapter->stats.gorc += rd32(E1000_GORCL);
3643 rd32(E1000_GORCH); /* clear GORCL */
3644 adapter->stats.bprc += rd32(E1000_BPRC);
3645 adapter->stats.mprc += rd32(E1000_MPRC);
3646 adapter->stats.roc += rd32(E1000_ROC);
3647
3648 adapter->stats.prc64 += rd32(E1000_PRC64);
3649 adapter->stats.prc127 += rd32(E1000_PRC127);
3650 adapter->stats.prc255 += rd32(E1000_PRC255);
3651 adapter->stats.prc511 += rd32(E1000_PRC511);
3652 adapter->stats.prc1023 += rd32(E1000_PRC1023);
3653 adapter->stats.prc1522 += rd32(E1000_PRC1522);
3654 adapter->stats.symerrs += rd32(E1000_SYMERRS);
3655 adapter->stats.sec += rd32(E1000_SEC);
3656
3657 adapter->stats.mpc += rd32(E1000_MPC);
3658 adapter->stats.scc += rd32(E1000_SCC);
3659 adapter->stats.ecol += rd32(E1000_ECOL);
3660 adapter->stats.mcc += rd32(E1000_MCC);
3661 adapter->stats.latecol += rd32(E1000_LATECOL);
3662 adapter->stats.dc += rd32(E1000_DC);
3663 adapter->stats.rlec += rd32(E1000_RLEC);
3664 adapter->stats.xonrxc += rd32(E1000_XONRXC);
3665 adapter->stats.xontxc += rd32(E1000_XONTXC);
3666 adapter->stats.xoffrxc += rd32(E1000_XOFFRXC);
3667 adapter->stats.xofftxc += rd32(E1000_XOFFTXC);
3668 adapter->stats.fcruc += rd32(E1000_FCRUC);
3669 adapter->stats.gptc += rd32(E1000_GPTC);
3670 adapter->stats.gotc += rd32(E1000_GOTCL);
3671 rd32(E1000_GOTCH); /* clear GOTCL */
3672 adapter->stats.rnbc += rd32(E1000_RNBC);
3673 adapter->stats.ruc += rd32(E1000_RUC);
3674 adapter->stats.rfc += rd32(E1000_RFC);
3675 adapter->stats.rjc += rd32(E1000_RJC);
3676 adapter->stats.tor += rd32(E1000_TORH);
3677 adapter->stats.tot += rd32(E1000_TOTH);
3678 adapter->stats.tpr += rd32(E1000_TPR);
3679
3680 adapter->stats.ptc64 += rd32(E1000_PTC64);
3681 adapter->stats.ptc127 += rd32(E1000_PTC127);
3682 adapter->stats.ptc255 += rd32(E1000_PTC255);
3683 adapter->stats.ptc511 += rd32(E1000_PTC511);
3684 adapter->stats.ptc1023 += rd32(E1000_PTC1023);
3685 adapter->stats.ptc1522 += rd32(E1000_PTC1522);
3686
3687 adapter->stats.mptc += rd32(E1000_MPTC);
3688 adapter->stats.bptc += rd32(E1000_BPTC);
3689
3690 /* used for adaptive IFS */
3691
3692 hw->mac.tx_packet_delta = rd32(E1000_TPT);
3693 adapter->stats.tpt += hw->mac.tx_packet_delta;
3694 hw->mac.collision_delta = rd32(E1000_COLC);
3695 adapter->stats.colc += hw->mac.collision_delta;
3696
3697 adapter->stats.algnerrc += rd32(E1000_ALGNERRC);
3698 adapter->stats.rxerrc += rd32(E1000_RXERRC);
3699 adapter->stats.tncrs += rd32(E1000_TNCRS);
3700 adapter->stats.tsctc += rd32(E1000_TSCTC);
3701 adapter->stats.tsctfc += rd32(E1000_TSCTFC);
3702
3703 adapter->stats.iac += rd32(E1000_IAC);
3704 adapter->stats.icrxoc += rd32(E1000_ICRXOC);
3705 adapter->stats.icrxptc += rd32(E1000_ICRXPTC);
3706 adapter->stats.icrxatc += rd32(E1000_ICRXATC);
3707 adapter->stats.ictxptc += rd32(E1000_ICTXPTC);
3708 adapter->stats.ictxatc += rd32(E1000_ICTXATC);
3709 adapter->stats.ictxqec += rd32(E1000_ICTXQEC);
3710 adapter->stats.ictxqmtc += rd32(E1000_ICTXQMTC);
3711 adapter->stats.icrxdmtc += rd32(E1000_ICRXDMTC);
3712
3713 /* Fill out the OS statistics structure */
3714 adapter->net_stats.multicast = adapter->stats.mprc;
3715 adapter->net_stats.collisions = adapter->stats.colc;
3716
3717 /* Rx Errors */
3718
Jesper Dangaard Brouer8c0ab702009-05-26 13:50:31 +00003719 if (hw->mac.type != e1000_82575) {
3720 u32 rqdpc_tmp;
Jesper Dangaard Brouer3ea73af2009-05-26 13:50:48 +00003721 u64 rqdpc_total = 0;
Jesper Dangaard Brouer8c0ab702009-05-26 13:50:31 +00003722 int i;
3723 /* Read out drops stats per RX queue. Notice RQDPC (Receive
3724 * Queue Drop Packet Count) stats only gets incremented, if
3725 * the DROP_EN but it set (in the SRRCTL register for that
3726 * queue). If DROP_EN bit is NOT set, then the some what
3727 * equivalent count is stored in RNBC (not per queue basis).
3728 * Also note the drop count is due to lack of available
3729 * descriptors.
3730 */
3731 for (i = 0; i < adapter->num_rx_queues; i++) {
3732 rqdpc_tmp = rd32(E1000_RQDPC(i)) & 0xFFF;
3733 adapter->rx_ring[i].rx_stats.drops += rqdpc_tmp;
Jesper Dangaard Brouer3ea73af2009-05-26 13:50:48 +00003734 rqdpc_total += adapter->rx_ring[i].rx_stats.drops;
Jesper Dangaard Brouer8c0ab702009-05-26 13:50:31 +00003735 }
Jesper Dangaard Brouer3ea73af2009-05-26 13:50:48 +00003736 adapter->net_stats.rx_fifo_errors = rqdpc_total;
Jesper Dangaard Brouer8c0ab702009-05-26 13:50:31 +00003737 }
3738
Jesper Dangaard Brouer3ea73af2009-05-26 13:50:48 +00003739 /* Note RNBC (Receive No Buffers Count) is an not an exact
3740 * drop count as the hardware FIFO might save the day. Thats
3741 * one of the reason for saving it in rx_fifo_errors, as its
3742 * potentially not a true drop.
3743 */
3744 adapter->net_stats.rx_fifo_errors += adapter->stats.rnbc;
3745
Auke Kok9d5c8242008-01-24 02:22:38 -08003746 /* RLEC on some newer hardware can be incorrect so build
Jesper Dangaard Brouer8c0ab702009-05-26 13:50:31 +00003747 * our own version based on RUC and ROC */
Auke Kok9d5c8242008-01-24 02:22:38 -08003748 adapter->net_stats.rx_errors = adapter->stats.rxerrc +
3749 adapter->stats.crcerrs + adapter->stats.algnerrc +
3750 adapter->stats.ruc + adapter->stats.roc +
3751 adapter->stats.cexterr;
3752 adapter->net_stats.rx_length_errors = adapter->stats.ruc +
3753 adapter->stats.roc;
3754 adapter->net_stats.rx_crc_errors = adapter->stats.crcerrs;
3755 adapter->net_stats.rx_frame_errors = adapter->stats.algnerrc;
3756 adapter->net_stats.rx_missed_errors = adapter->stats.mpc;
3757
3758 /* Tx Errors */
3759 adapter->net_stats.tx_errors = adapter->stats.ecol +
3760 adapter->stats.latecol;
3761 adapter->net_stats.tx_aborted_errors = adapter->stats.ecol;
3762 adapter->net_stats.tx_window_errors = adapter->stats.latecol;
3763 adapter->net_stats.tx_carrier_errors = adapter->stats.tncrs;
3764
3765 /* Tx Dropped needs to be maintained elsewhere */
3766
3767 /* Phy Stats */
3768 if (hw->phy.media_type == e1000_media_type_copper) {
3769 if ((adapter->link_speed == SPEED_1000) &&
Alexander Duyck73cd78f2009-02-12 18:16:59 +00003770 (!igb_read_phy_reg(hw, PHY_1000T_STATUS, &phy_tmp))) {
Auke Kok9d5c8242008-01-24 02:22:38 -08003771 phy_tmp &= PHY_IDLE_ERROR_COUNT_MASK;
3772 adapter->phy_stats.idle_errors += phy_tmp;
3773 }
3774 }
3775
3776 /* Management Stats */
3777 adapter->stats.mgptc += rd32(E1000_MGTPTC);
3778 adapter->stats.mgprc += rd32(E1000_MGTPRC);
3779 adapter->stats.mgpdc += rd32(E1000_MGTPDC);
3780}
3781
Auke Kok9d5c8242008-01-24 02:22:38 -08003782static irqreturn_t igb_msix_other(int irq, void *data)
3783{
3784 struct net_device *netdev = data;
3785 struct igb_adapter *adapter = netdev_priv(netdev);
3786 struct e1000_hw *hw = &adapter->hw;
PJ Waskiewicz844290e2008-06-27 11:00:39 -07003787 u32 icr = rd32(E1000_ICR);
Auke Kok9d5c8242008-01-24 02:22:38 -08003788
PJ Waskiewicz844290e2008-06-27 11:00:39 -07003789 /* reading ICR causes bit 31 of EICR to be cleared */
Alexander Duyckdda0e082009-02-06 23:19:08 +00003790
3791 if(icr & E1000_ICR_DOUTSYNC) {
3792 /* HW is reporting DMA is out of sync */
3793 adapter->stats.doosync++;
3794 }
Alexander Duyckeebbbdb2009-02-06 23:19:29 +00003795
Alexander Duyck4ae196d2009-02-19 20:40:07 -08003796 /* Check for a mailbox event */
3797 if (icr & E1000_ICR_VMMB)
3798 igb_msg_task(adapter);
3799
3800 if (icr & E1000_ICR_LSC) {
3801 hw->mac.get_link_status = 1;
3802 /* guard against interrupt when we're going down */
3803 if (!test_bit(__IGB_DOWN, &adapter->state))
3804 mod_timer(&adapter->watchdog_timer, jiffies + 1);
3805 }
3806
3807 wr32(E1000_IMS, E1000_IMS_LSC | E1000_IMS_DOUTSYNC | E1000_IMS_VMMB);
PJ Waskiewicz844290e2008-06-27 11:00:39 -07003808 wr32(E1000_EIMS, adapter->eims_other);
Auke Kok9d5c8242008-01-24 02:22:38 -08003809
3810 return IRQ_HANDLED;
3811}
3812
3813static irqreturn_t igb_msix_tx(int irq, void *data)
3814{
3815 struct igb_ring *tx_ring = data;
3816 struct igb_adapter *adapter = tx_ring->adapter;
3817 struct e1000_hw *hw = &adapter->hw;
3818
Jeff Kirsher421e02f2008-10-17 11:08:31 -07003819#ifdef CONFIG_IGB_DCA
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07003820 if (adapter->flags & IGB_FLAG_DCA_ENABLED)
Jeb Cramerfe4506b2008-07-08 15:07:55 -07003821 igb_update_tx_dca(tx_ring);
3822#endif
Alexander Duyck73cd78f2009-02-12 18:16:59 +00003823
Auke Kok9d5c8242008-01-24 02:22:38 -08003824 tx_ring->total_bytes = 0;
3825 tx_ring->total_packets = 0;
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07003826
3827 /* auto mask will automatically reenable the interrupt when we write
3828 * EICS */
Mitch Williams3b644cf2008-06-27 10:59:48 -07003829 if (!igb_clean_tx_irq(tx_ring))
Auke Kok9d5c8242008-01-24 02:22:38 -08003830 /* Ring was not completely cleaned, so fire another interrupt */
3831 wr32(E1000_EICS, tx_ring->eims_value);
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07003832 else
Auke Kok9d5c8242008-01-24 02:22:38 -08003833 wr32(E1000_EIMS, tx_ring->eims_value);
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07003834
Auke Kok9d5c8242008-01-24 02:22:38 -08003835 return IRQ_HANDLED;
3836}
3837
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07003838static void igb_write_itr(struct igb_ring *ring)
3839{
3840 struct e1000_hw *hw = &ring->adapter->hw;
3841 if ((ring->adapter->itr_setting & 3) && ring->set_itr) {
3842 switch (hw->mac.type) {
3843 case e1000_82576:
Alexander Duyck73cd78f2009-02-12 18:16:59 +00003844 wr32(ring->itr_register, ring->itr_val |
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07003845 0x80000000);
3846 break;
3847 default:
Alexander Duyck73cd78f2009-02-12 18:16:59 +00003848 wr32(ring->itr_register, ring->itr_val |
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07003849 (ring->itr_val << 16));
3850 break;
3851 }
3852 ring->set_itr = 0;
3853 }
3854}
3855
Auke Kok9d5c8242008-01-24 02:22:38 -08003856static irqreturn_t igb_msix_rx(int irq, void *data)
3857{
3858 struct igb_ring *rx_ring = data;
Auke Kok9d5c8242008-01-24 02:22:38 -08003859
PJ Waskiewicz844290e2008-06-27 11:00:39 -07003860 /* Write the ITR value calculated at the end of the
3861 * previous interrupt.
3862 */
Auke Kok9d5c8242008-01-24 02:22:38 -08003863
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07003864 igb_write_itr(rx_ring);
Auke Kok9d5c8242008-01-24 02:22:38 -08003865
Ben Hutchings288379f2009-01-19 16:43:59 -08003866 if (napi_schedule_prep(&rx_ring->napi))
3867 __napi_schedule(&rx_ring->napi);
PJ Waskiewicz844290e2008-06-27 11:00:39 -07003868
Jeff Kirsher421e02f2008-10-17 11:08:31 -07003869#ifdef CONFIG_IGB_DCA
David S. Miller8d253322008-12-26 15:13:55 -08003870 if (rx_ring->adapter->flags & IGB_FLAG_DCA_ENABLED)
Jeb Cramerfe4506b2008-07-08 15:07:55 -07003871 igb_update_rx_dca(rx_ring);
3872#endif
3873 return IRQ_HANDLED;
Auke Kok9d5c8242008-01-24 02:22:38 -08003874}
3875
Jeff Kirsher421e02f2008-10-17 11:08:31 -07003876#ifdef CONFIG_IGB_DCA
Jeb Cramerfe4506b2008-07-08 15:07:55 -07003877static void igb_update_rx_dca(struct igb_ring *rx_ring)
3878{
3879 u32 dca_rxctrl;
3880 struct igb_adapter *adapter = rx_ring->adapter;
3881 struct e1000_hw *hw = &adapter->hw;
3882 int cpu = get_cpu();
Alexander Duyck26bc19e2008-12-26 01:34:11 -08003883 int q = rx_ring->reg_idx;
Jeb Cramerfe4506b2008-07-08 15:07:55 -07003884
3885 if (rx_ring->cpu != cpu) {
3886 dca_rxctrl = rd32(E1000_DCA_RXCTRL(q));
Alexander Duyck2d064c02008-07-08 15:10:12 -07003887 if (hw->mac.type == e1000_82576) {
3888 dca_rxctrl &= ~E1000_DCA_RXCTRL_CPUID_MASK_82576;
Maciej Sosnowski92be7912009-03-13 20:40:21 +00003889 dca_rxctrl |= dca3_get_tag(&adapter->pdev->dev, cpu) <<
Alexander Duyck2d064c02008-07-08 15:10:12 -07003890 E1000_DCA_RXCTRL_CPUID_SHIFT;
3891 } else {
3892 dca_rxctrl &= ~E1000_DCA_RXCTRL_CPUID_MASK;
Maciej Sosnowski92be7912009-03-13 20:40:21 +00003893 dca_rxctrl |= dca3_get_tag(&adapter->pdev->dev, cpu);
Alexander Duyck2d064c02008-07-08 15:10:12 -07003894 }
Jeb Cramerfe4506b2008-07-08 15:07:55 -07003895 dca_rxctrl |= E1000_DCA_RXCTRL_DESC_DCA_EN;
3896 dca_rxctrl |= E1000_DCA_RXCTRL_HEAD_DCA_EN;
3897 dca_rxctrl |= E1000_DCA_RXCTRL_DATA_DCA_EN;
3898 wr32(E1000_DCA_RXCTRL(q), dca_rxctrl);
3899 rx_ring->cpu = cpu;
3900 }
3901 put_cpu();
3902}
3903
3904static void igb_update_tx_dca(struct igb_ring *tx_ring)
3905{
3906 u32 dca_txctrl;
3907 struct igb_adapter *adapter = tx_ring->adapter;
3908 struct e1000_hw *hw = &adapter->hw;
3909 int cpu = get_cpu();
Alexander Duyck26bc19e2008-12-26 01:34:11 -08003910 int q = tx_ring->reg_idx;
Jeb Cramerfe4506b2008-07-08 15:07:55 -07003911
3912 if (tx_ring->cpu != cpu) {
3913 dca_txctrl = rd32(E1000_DCA_TXCTRL(q));
Alexander Duyck2d064c02008-07-08 15:10:12 -07003914 if (hw->mac.type == e1000_82576) {
3915 dca_txctrl &= ~E1000_DCA_TXCTRL_CPUID_MASK_82576;
Maciej Sosnowski92be7912009-03-13 20:40:21 +00003916 dca_txctrl |= dca3_get_tag(&adapter->pdev->dev, cpu) <<
Alexander Duyck2d064c02008-07-08 15:10:12 -07003917 E1000_DCA_TXCTRL_CPUID_SHIFT;
3918 } else {
3919 dca_txctrl &= ~E1000_DCA_TXCTRL_CPUID_MASK;
Maciej Sosnowski92be7912009-03-13 20:40:21 +00003920 dca_txctrl |= dca3_get_tag(&adapter->pdev->dev, cpu);
Alexander Duyck2d064c02008-07-08 15:10:12 -07003921 }
Jeb Cramerfe4506b2008-07-08 15:07:55 -07003922 dca_txctrl |= E1000_DCA_TXCTRL_DESC_DCA_EN;
3923 wr32(E1000_DCA_TXCTRL(q), dca_txctrl);
3924 tx_ring->cpu = cpu;
3925 }
3926 put_cpu();
3927}
3928
3929static void igb_setup_dca(struct igb_adapter *adapter)
3930{
Alexander Duyck7e0e99e2009-05-21 13:06:56 +00003931 struct e1000_hw *hw = &adapter->hw;
Jeb Cramerfe4506b2008-07-08 15:07:55 -07003932 int i;
3933
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07003934 if (!(adapter->flags & IGB_FLAG_DCA_ENABLED))
Jeb Cramerfe4506b2008-07-08 15:07:55 -07003935 return;
3936
Alexander Duyck7e0e99e2009-05-21 13:06:56 +00003937 /* Always use CB2 mode, difference is masked in the CB driver. */
3938 wr32(E1000_DCA_CTRL, E1000_DCA_CTRL_DCA_MODE_CB2);
3939
Jeb Cramerfe4506b2008-07-08 15:07:55 -07003940 for (i = 0; i < adapter->num_tx_queues; i++) {
3941 adapter->tx_ring[i].cpu = -1;
3942 igb_update_tx_dca(&adapter->tx_ring[i]);
3943 }
3944 for (i = 0; i < adapter->num_rx_queues; i++) {
3945 adapter->rx_ring[i].cpu = -1;
3946 igb_update_rx_dca(&adapter->rx_ring[i]);
3947 }
3948}
3949
3950static int __igb_notify_dca(struct device *dev, void *data)
3951{
3952 struct net_device *netdev = dev_get_drvdata(dev);
3953 struct igb_adapter *adapter = netdev_priv(netdev);
3954 struct e1000_hw *hw = &adapter->hw;
3955 unsigned long event = *(unsigned long *)data;
3956
3957 switch (event) {
3958 case DCA_PROVIDER_ADD:
3959 /* if already enabled, don't do it again */
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07003960 if (adapter->flags & IGB_FLAG_DCA_ENABLED)
Jeb Cramerfe4506b2008-07-08 15:07:55 -07003961 break;
Jeb Cramerfe4506b2008-07-08 15:07:55 -07003962 /* Always use CB2 mode, difference is masked
3963 * in the CB driver. */
Alexander Duyckcbd347a2009-02-15 23:59:44 -08003964 wr32(E1000_DCA_CTRL, E1000_DCA_CTRL_DCA_MODE_CB2);
Jeb Cramerfe4506b2008-07-08 15:07:55 -07003965 if (dca_add_requester(dev) == 0) {
Alexander Duyckbbd98fe2009-01-31 00:52:30 -08003966 adapter->flags |= IGB_FLAG_DCA_ENABLED;
Jeb Cramerfe4506b2008-07-08 15:07:55 -07003967 dev_info(&adapter->pdev->dev, "DCA enabled\n");
3968 igb_setup_dca(adapter);
3969 break;
3970 }
3971 /* Fall Through since DCA is disabled. */
3972 case DCA_PROVIDER_REMOVE:
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07003973 if (adapter->flags & IGB_FLAG_DCA_ENABLED) {
Jeb Cramerfe4506b2008-07-08 15:07:55 -07003974 /* without this a class_device is left
3975 * hanging around in the sysfs model */
3976 dca_remove_requester(dev);
3977 dev_info(&adapter->pdev->dev, "DCA disabled\n");
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07003978 adapter->flags &= ~IGB_FLAG_DCA_ENABLED;
Alexander Duyckcbd347a2009-02-15 23:59:44 -08003979 wr32(E1000_DCA_CTRL, E1000_DCA_CTRL_DCA_MODE_DISABLE);
Jeb Cramerfe4506b2008-07-08 15:07:55 -07003980 }
3981 break;
3982 }
Alexander Duyckbbd98fe2009-01-31 00:52:30 -08003983
Jeb Cramerfe4506b2008-07-08 15:07:55 -07003984 return 0;
3985}
3986
3987static int igb_notify_dca(struct notifier_block *nb, unsigned long event,
3988 void *p)
3989{
3990 int ret_val;
3991
3992 ret_val = driver_for_each_device(&igb_driver.driver, NULL, &event,
3993 __igb_notify_dca);
3994
3995 return ret_val ? NOTIFY_BAD : NOTIFY_DONE;
3996}
Jeff Kirsher421e02f2008-10-17 11:08:31 -07003997#endif /* CONFIG_IGB_DCA */
Auke Kok9d5c8242008-01-24 02:22:38 -08003998
Alexander Duyck4ae196d2009-02-19 20:40:07 -08003999static void igb_ping_all_vfs(struct igb_adapter *adapter)
4000{
4001 struct e1000_hw *hw = &adapter->hw;
4002 u32 ping;
4003 int i;
4004
4005 for (i = 0 ; i < adapter->vfs_allocated_count; i++) {
4006 ping = E1000_PF_CONTROL_MSG;
4007 if (adapter->vf_data[i].clear_to_send)
4008 ping |= E1000_VT_MSGTYPE_CTS;
4009 igb_write_mbx(hw, &ping, 1, i);
4010 }
4011}
4012
4013static int igb_set_vf_multicasts(struct igb_adapter *adapter,
4014 u32 *msgbuf, u32 vf)
4015{
4016 int n = (msgbuf[0] & E1000_VT_MSGINFO_MASK) >> E1000_VT_MSGINFO_SHIFT;
4017 u16 *hash_list = (u16 *)&msgbuf[1];
4018 struct vf_data_storage *vf_data = &adapter->vf_data[vf];
4019 int i;
4020
4021 /* only up to 30 hash values supported */
4022 if (n > 30)
4023 n = 30;
4024
4025 /* salt away the number of multi cast addresses assigned
4026 * to this VF for later use to restore when the PF multi cast
4027 * list changes
4028 */
4029 vf_data->num_vf_mc_hashes = n;
4030
4031 /* VFs are limited to using the MTA hash table for their multicast
4032 * addresses */
4033 for (i = 0; i < n; i++)
Joe Perchesa419aef2009-08-18 11:18:35 -07004034 vf_data->vf_mc_hashes[i] = hash_list[i];
Alexander Duyck4ae196d2009-02-19 20:40:07 -08004035
4036 /* Flush and reset the mta with the new values */
Alexander Duyckff41f8d2009-09-03 14:48:56 +00004037 igb_set_rx_mode(adapter->netdev);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08004038
4039 return 0;
4040}
4041
4042static void igb_restore_vf_multicasts(struct igb_adapter *adapter)
4043{
4044 struct e1000_hw *hw = &adapter->hw;
4045 struct vf_data_storage *vf_data;
4046 int i, j;
4047
4048 for (i = 0; i < adapter->vfs_allocated_count; i++) {
4049 vf_data = &adapter->vf_data[i];
Alexander Duyck75f4f382009-03-13 20:41:55 +00004050 for (j = 0; j < vf_data->num_vf_mc_hashes; j++)
Alexander Duyck4ae196d2009-02-19 20:40:07 -08004051 igb_mta_set(hw, vf_data->vf_mc_hashes[j]);
4052 }
4053}
4054
4055static void igb_clear_vf_vfta(struct igb_adapter *adapter, u32 vf)
4056{
4057 struct e1000_hw *hw = &adapter->hw;
4058 u32 pool_mask, reg, vid;
4059 int i;
4060
4061 pool_mask = 1 << (E1000_VLVF_POOLSEL_SHIFT + vf);
4062
4063 /* Find the vlan filter for this id */
4064 for (i = 0; i < E1000_VLVF_ARRAY_SIZE; i++) {
4065 reg = rd32(E1000_VLVF(i));
4066
4067 /* remove the vf from the pool */
4068 reg &= ~pool_mask;
4069
4070 /* if pool is empty then remove entry from vfta */
4071 if (!(reg & E1000_VLVF_POOLSEL_MASK) &&
4072 (reg & E1000_VLVF_VLANID_ENABLE)) {
4073 reg = 0;
4074 vid = reg & E1000_VLVF_VLANID_MASK;
4075 igb_vfta_set(hw, vid, false);
4076 }
4077
4078 wr32(E1000_VLVF(i), reg);
4079 }
Alexander Duyckae641bd2009-09-03 14:49:33 +00004080
4081 adapter->vf_data[vf].vlans_enabled = 0;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08004082}
4083
4084static s32 igb_vlvf_set(struct igb_adapter *adapter, u32 vid, bool add, u32 vf)
4085{
4086 struct e1000_hw *hw = &adapter->hw;
4087 u32 reg, i;
4088
4089 /* It is an error to call this function when VFs are not enabled */
4090 if (!adapter->vfs_allocated_count)
4091 return -1;
4092
4093 /* Find the vlan filter for this id */
4094 for (i = 0; i < E1000_VLVF_ARRAY_SIZE; i++) {
4095 reg = rd32(E1000_VLVF(i));
4096 if ((reg & E1000_VLVF_VLANID_ENABLE) &&
4097 vid == (reg & E1000_VLVF_VLANID_MASK))
4098 break;
4099 }
4100
4101 if (add) {
4102 if (i == E1000_VLVF_ARRAY_SIZE) {
4103 /* Did not find a matching VLAN ID entry that was
4104 * enabled. Search for a free filter entry, i.e.
4105 * one without the enable bit set
4106 */
4107 for (i = 0; i < E1000_VLVF_ARRAY_SIZE; i++) {
4108 reg = rd32(E1000_VLVF(i));
4109 if (!(reg & E1000_VLVF_VLANID_ENABLE))
4110 break;
4111 }
4112 }
4113 if (i < E1000_VLVF_ARRAY_SIZE) {
4114 /* Found an enabled/available entry */
4115 reg |= 1 << (E1000_VLVF_POOLSEL_SHIFT + vf);
4116
4117 /* if !enabled we need to set this up in vfta */
4118 if (!(reg & E1000_VLVF_VLANID_ENABLE)) {
Alexander Duyckcad6d052009-03-13 20:41:37 +00004119 /* add VID to filter table, if bit already set
4120 * PF must have added it outside of table */
4121 if (igb_vfta_set(hw, vid, true))
4122 reg |= 1 << (E1000_VLVF_POOLSEL_SHIFT +
4123 adapter->vfs_allocated_count);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08004124 reg |= E1000_VLVF_VLANID_ENABLE;
4125 }
Alexander Duyckcad6d052009-03-13 20:41:37 +00004126 reg &= ~E1000_VLVF_VLANID_MASK;
4127 reg |= vid;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08004128
4129 wr32(E1000_VLVF(i), reg);
Alexander Duyckae641bd2009-09-03 14:49:33 +00004130
4131 /* do not modify RLPML for PF devices */
4132 if (vf >= adapter->vfs_allocated_count)
4133 return 0;
4134
4135 if (!adapter->vf_data[vf].vlans_enabled) {
4136 u32 size;
4137 reg = rd32(E1000_VMOLR(vf));
4138 size = reg & E1000_VMOLR_RLPML_MASK;
4139 size += 4;
4140 reg &= ~E1000_VMOLR_RLPML_MASK;
4141 reg |= size;
4142 wr32(E1000_VMOLR(vf), reg);
4143 }
4144 adapter->vf_data[vf].vlans_enabled++;
4145
Alexander Duyck4ae196d2009-02-19 20:40:07 -08004146 return 0;
4147 }
4148 } else {
4149 if (i < E1000_VLVF_ARRAY_SIZE) {
4150 /* remove vf from the pool */
4151 reg &= ~(1 << (E1000_VLVF_POOLSEL_SHIFT + vf));
4152 /* if pool is empty then remove entry from vfta */
4153 if (!(reg & E1000_VLVF_POOLSEL_MASK)) {
4154 reg = 0;
4155 igb_vfta_set(hw, vid, false);
4156 }
4157 wr32(E1000_VLVF(i), reg);
Alexander Duyckae641bd2009-09-03 14:49:33 +00004158
4159 /* do not modify RLPML for PF devices */
4160 if (vf >= adapter->vfs_allocated_count)
4161 return 0;
4162
4163 adapter->vf_data[vf].vlans_enabled--;
4164 if (!adapter->vf_data[vf].vlans_enabled) {
4165 u32 size;
4166 reg = rd32(E1000_VMOLR(vf));
4167 size = reg & E1000_VMOLR_RLPML_MASK;
4168 size -= 4;
4169 reg &= ~E1000_VMOLR_RLPML_MASK;
4170 reg |= size;
4171 wr32(E1000_VMOLR(vf), reg);
4172 }
Alexander Duyck4ae196d2009-02-19 20:40:07 -08004173 return 0;
4174 }
4175 }
4176 return -1;
4177}
4178
4179static int igb_set_vf_vlan(struct igb_adapter *adapter, u32 *msgbuf, u32 vf)
4180{
4181 int add = (msgbuf[0] & E1000_VT_MSGINFO_MASK) >> E1000_VT_MSGINFO_SHIFT;
4182 int vid = (msgbuf[1] & E1000_VLVF_VLANID_MASK);
4183
4184 return igb_vlvf_set(adapter, vid, add, vf);
4185}
4186
4187static inline void igb_vf_reset_event(struct igb_adapter *adapter, u32 vf)
4188{
4189 struct e1000_hw *hw = &adapter->hw;
4190
4191 /* disable mailbox functionality for vf */
4192 adapter->vf_data[vf].clear_to_send = false;
4193
4194 /* reset offloads to defaults */
4195 igb_set_vmolr(hw, vf);
4196
4197 /* reset vlans for device */
4198 igb_clear_vf_vfta(adapter, vf);
4199
4200 /* reset multicast table array for vf */
4201 adapter->vf_data[vf].num_vf_mc_hashes = 0;
4202
4203 /* Flush and reset the mta with the new values */
Alexander Duyckff41f8d2009-09-03 14:48:56 +00004204 igb_set_rx_mode(adapter->netdev);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08004205}
4206
4207static inline void igb_vf_reset_msg(struct igb_adapter *adapter, u32 vf)
4208{
4209 struct e1000_hw *hw = &adapter->hw;
4210 unsigned char *vf_mac = adapter->vf_data[vf].vf_mac_addresses;
Alexander Duyckff41f8d2009-09-03 14:48:56 +00004211 int rar_entry = hw->mac.rar_entry_count - (vf + 1);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08004212 u32 reg, msgbuf[3];
4213 u8 *addr = (u8 *)(&msgbuf[1]);
4214
4215 /* process all the same items cleared in a function level reset */
4216 igb_vf_reset_event(adapter, vf);
4217
4218 /* set vf mac address */
Alexander Duyck26ad9172009-10-05 06:32:49 +00004219 igb_rar_set_qsel(adapter, vf_mac, rar_entry, vf);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08004220
4221 /* enable transmit and receive for vf */
4222 reg = rd32(E1000_VFTE);
4223 wr32(E1000_VFTE, reg | (1 << vf));
4224 reg = rd32(E1000_VFRE);
4225 wr32(E1000_VFRE, reg | (1 << vf));
4226
4227 /* enable mailbox functionality for vf */
4228 adapter->vf_data[vf].clear_to_send = true;
4229
4230 /* reply to reset with ack and vf mac address */
4231 msgbuf[0] = E1000_VF_RESET | E1000_VT_MSGTYPE_ACK;
4232 memcpy(addr, vf_mac, 6);
4233 igb_write_mbx(hw, msgbuf, 3, vf);
4234}
4235
4236static int igb_set_vf_mac_addr(struct igb_adapter *adapter, u32 *msg, int vf)
4237{
4238 unsigned char *addr = (char *)&msg[1];
4239 int err = -1;
4240
4241 if (is_valid_ether_addr(addr))
4242 err = igb_set_vf_mac(adapter, vf, addr);
4243
4244 return err;
4245
4246}
4247
4248static void igb_rcv_ack_from_vf(struct igb_adapter *adapter, u32 vf)
4249{
4250 struct e1000_hw *hw = &adapter->hw;
4251 u32 msg = E1000_VT_MSGTYPE_NACK;
4252
4253 /* if device isn't clear to send it shouldn't be reading either */
4254 if (!adapter->vf_data[vf].clear_to_send)
4255 igb_write_mbx(hw, &msg, 1, vf);
4256}
4257
4258
4259static void igb_msg_task(struct igb_adapter *adapter)
4260{
4261 struct e1000_hw *hw = &adapter->hw;
4262 u32 vf;
4263
4264 for (vf = 0; vf < adapter->vfs_allocated_count; vf++) {
4265 /* process any reset requests */
4266 if (!igb_check_for_rst(hw, vf)) {
4267 adapter->vf_data[vf].clear_to_send = false;
4268 igb_vf_reset_event(adapter, vf);
4269 }
4270
4271 /* process any messages pending */
4272 if (!igb_check_for_msg(hw, vf))
4273 igb_rcv_msg_from_vf(adapter, vf);
4274
4275 /* process any acks */
4276 if (!igb_check_for_ack(hw, vf))
4277 igb_rcv_ack_from_vf(adapter, vf);
4278
4279 }
4280}
4281
4282static int igb_rcv_msg_from_vf(struct igb_adapter *adapter, u32 vf)
4283{
4284 u32 mbx_size = E1000_VFMAILBOX_SIZE;
4285 u32 msgbuf[mbx_size];
4286 struct e1000_hw *hw = &adapter->hw;
4287 s32 retval;
4288
4289 retval = igb_read_mbx(hw, msgbuf, mbx_size, vf);
4290
4291 if (retval)
4292 dev_err(&adapter->pdev->dev,
4293 "Error receiving message from VF\n");
4294
4295 /* this is a message we already processed, do nothing */
4296 if (msgbuf[0] & (E1000_VT_MSGTYPE_ACK | E1000_VT_MSGTYPE_NACK))
4297 return retval;
4298
4299 /*
4300 * until the vf completes a reset it should not be
4301 * allowed to start any configuration.
4302 */
4303
4304 if (msgbuf[0] == E1000_VF_RESET) {
4305 igb_vf_reset_msg(adapter, vf);
4306
4307 return retval;
4308 }
4309
4310 if (!adapter->vf_data[vf].clear_to_send) {
4311 msgbuf[0] |= E1000_VT_MSGTYPE_NACK;
4312 igb_write_mbx(hw, msgbuf, 1, vf);
4313 return retval;
4314 }
4315
4316 switch ((msgbuf[0] & 0xFFFF)) {
4317 case E1000_VF_SET_MAC_ADDR:
4318 retval = igb_set_vf_mac_addr(adapter, msgbuf, vf);
4319 break;
4320 case E1000_VF_SET_MULTICAST:
4321 retval = igb_set_vf_multicasts(adapter, msgbuf, vf);
4322 break;
4323 case E1000_VF_SET_LPE:
4324 retval = igb_set_vf_rlpml(adapter, msgbuf[1], vf);
4325 break;
4326 case E1000_VF_SET_VLAN:
4327 retval = igb_set_vf_vlan(adapter, msgbuf, vf);
4328 break;
4329 default:
4330 dev_err(&adapter->pdev->dev, "Unhandled Msg %08x\n", msgbuf[0]);
4331 retval = -1;
4332 break;
4333 }
4334
4335 /* notify the VF of the results of what it sent us */
4336 if (retval)
4337 msgbuf[0] |= E1000_VT_MSGTYPE_NACK;
4338 else
4339 msgbuf[0] |= E1000_VT_MSGTYPE_ACK;
4340
4341 msgbuf[0] |= E1000_VT_MSGTYPE_CTS;
4342
4343 igb_write_mbx(hw, msgbuf, 1, vf);
4344
4345 return retval;
4346}
4347
Auke Kok9d5c8242008-01-24 02:22:38 -08004348/**
Alexander Duyck68d480c2009-10-05 06:33:08 +00004349 * igb_set_uta - Set unicast filter table address
4350 * @adapter: board private structure
4351 *
4352 * The unicast table address is a register array of 32-bit registers.
4353 * The table is meant to be used in a way similar to how the MTA is used
4354 * however due to certain limitations in the hardware it is necessary to
4355 * set all the hash bits to 1 and use the VMOLR ROPE bit as a promiscous
4356 * enable bit to allow vlan tag stripping when promiscous mode is enabled
4357 **/
4358static void igb_set_uta(struct igb_adapter *adapter)
4359{
4360 struct e1000_hw *hw = &adapter->hw;
4361 int i;
4362
4363 /* The UTA table only exists on 82576 hardware and newer */
4364 if (hw->mac.type < e1000_82576)
4365 return;
4366
4367 /* we only need to do this if VMDq is enabled */
4368 if (!adapter->vfs_allocated_count)
4369 return;
4370
4371 for (i = 0; i < hw->mac.uta_reg_count; i++)
4372 array_wr32(E1000_UTA, i, ~0);
4373}
4374
4375/**
Auke Kok9d5c8242008-01-24 02:22:38 -08004376 * igb_intr_msi - Interrupt Handler
4377 * @irq: interrupt number
4378 * @data: pointer to a network interface device structure
4379 **/
4380static irqreturn_t igb_intr_msi(int irq, void *data)
4381{
4382 struct net_device *netdev = data;
4383 struct igb_adapter *adapter = netdev_priv(netdev);
Auke Kok9d5c8242008-01-24 02:22:38 -08004384 struct e1000_hw *hw = &adapter->hw;
4385 /* read ICR disables interrupts using IAM */
4386 u32 icr = rd32(E1000_ICR);
4387
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07004388 igb_write_itr(adapter->rx_ring);
Auke Kok9d5c8242008-01-24 02:22:38 -08004389
Alexander Duyckdda0e082009-02-06 23:19:08 +00004390 if(icr & E1000_ICR_DOUTSYNC) {
4391 /* HW is reporting DMA is out of sync */
4392 adapter->stats.doosync++;
4393 }
4394
Auke Kok9d5c8242008-01-24 02:22:38 -08004395 if (icr & (E1000_ICR_RXSEQ | E1000_ICR_LSC)) {
4396 hw->mac.get_link_status = 1;
4397 if (!test_bit(__IGB_DOWN, &adapter->state))
4398 mod_timer(&adapter->watchdog_timer, jiffies + 1);
4399 }
4400
Ben Hutchings288379f2009-01-19 16:43:59 -08004401 napi_schedule(&adapter->rx_ring[0].napi);
Auke Kok9d5c8242008-01-24 02:22:38 -08004402
4403 return IRQ_HANDLED;
4404}
4405
4406/**
Alexander Duyck4a3c6432009-02-06 23:20:49 +00004407 * igb_intr - Legacy Interrupt Handler
Auke Kok9d5c8242008-01-24 02:22:38 -08004408 * @irq: interrupt number
4409 * @data: pointer to a network interface device structure
4410 **/
4411static irqreturn_t igb_intr(int irq, void *data)
4412{
4413 struct net_device *netdev = data;
4414 struct igb_adapter *adapter = netdev_priv(netdev);
Auke Kok9d5c8242008-01-24 02:22:38 -08004415 struct e1000_hw *hw = &adapter->hw;
4416 /* Interrupt Auto-Mask...upon reading ICR, interrupts are masked. No
4417 * need for the IMC write */
4418 u32 icr = rd32(E1000_ICR);
Auke Kok9d5c8242008-01-24 02:22:38 -08004419 if (!icr)
4420 return IRQ_NONE; /* Not our interrupt */
4421
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07004422 igb_write_itr(adapter->rx_ring);
Auke Kok9d5c8242008-01-24 02:22:38 -08004423
4424 /* IMS will not auto-mask if INT_ASSERTED is not set, and if it is
4425 * not set, then the adapter didn't send an interrupt */
4426 if (!(icr & E1000_ICR_INT_ASSERTED))
4427 return IRQ_NONE;
4428
Alexander Duyckdda0e082009-02-06 23:19:08 +00004429 if(icr & E1000_ICR_DOUTSYNC) {
4430 /* HW is reporting DMA is out of sync */
4431 adapter->stats.doosync++;
4432 }
4433
Auke Kok9d5c8242008-01-24 02:22:38 -08004434 if (icr & (E1000_ICR_RXSEQ | E1000_ICR_LSC)) {
4435 hw->mac.get_link_status = 1;
4436 /* guard against interrupt when we're going down */
4437 if (!test_bit(__IGB_DOWN, &adapter->state))
4438 mod_timer(&adapter->watchdog_timer, jiffies + 1);
4439 }
4440
Ben Hutchings288379f2009-01-19 16:43:59 -08004441 napi_schedule(&adapter->rx_ring[0].napi);
Auke Kok9d5c8242008-01-24 02:22:38 -08004442
4443 return IRQ_HANDLED;
4444}
4445
Alexander Duyck46544252009-02-19 20:39:04 -08004446static inline void igb_rx_irq_enable(struct igb_ring *rx_ring)
4447{
4448 struct igb_adapter *adapter = rx_ring->adapter;
4449 struct e1000_hw *hw = &adapter->hw;
4450
4451 if (adapter->itr_setting & 3) {
4452 if (adapter->num_rx_queues == 1)
4453 igb_set_itr(adapter);
4454 else
4455 igb_update_ring_itr(rx_ring);
4456 }
4457
4458 if (!test_bit(__IGB_DOWN, &adapter->state)) {
4459 if (adapter->msix_entries)
4460 wr32(E1000_EIMS, rx_ring->eims_value);
4461 else
4462 igb_irq_enable(adapter);
4463 }
4464}
4465
Auke Kok9d5c8242008-01-24 02:22:38 -08004466/**
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07004467 * igb_poll - NAPI Rx polling callback
4468 * @napi: napi polling structure
4469 * @budget: count of how many packets we should handle
Auke Kok9d5c8242008-01-24 02:22:38 -08004470 **/
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07004471static int igb_poll(struct napi_struct *napi, int budget)
Auke Kok9d5c8242008-01-24 02:22:38 -08004472{
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07004473 struct igb_ring *rx_ring = container_of(napi, struct igb_ring, napi);
Auke Kok9d5c8242008-01-24 02:22:38 -08004474 int work_done = 0;
4475
Jeff Kirsher421e02f2008-10-17 11:08:31 -07004476#ifdef CONFIG_IGB_DCA
Alexander Duyckbd38e5d2009-03-13 20:40:58 +00004477 if (rx_ring->adapter->flags & IGB_FLAG_DCA_ENABLED)
Jeb Cramerfe4506b2008-07-08 15:07:55 -07004478 igb_update_rx_dca(rx_ring);
4479#endif
Mitch Williams3b644cf2008-06-27 10:59:48 -07004480 igb_clean_rx_irq_adv(rx_ring, &work_done, budget);
Auke Kok9d5c8242008-01-24 02:22:38 -08004481
Alexander Duyck46544252009-02-19 20:39:04 -08004482 if (rx_ring->buddy) {
4483#ifdef CONFIG_IGB_DCA
Alexander Duyckbd38e5d2009-03-13 20:40:58 +00004484 if (rx_ring->adapter->flags & IGB_FLAG_DCA_ENABLED)
Alexander Duyck46544252009-02-19 20:39:04 -08004485 igb_update_tx_dca(rx_ring->buddy);
4486#endif
4487 if (!igb_clean_tx_irq(rx_ring->buddy))
4488 work_done = budget;
Auke Kok9d5c8242008-01-24 02:22:38 -08004489 }
4490
Alexander Duyck46544252009-02-19 20:39:04 -08004491 /* If not enough Rx work done, exit the polling mode */
Alexander Duyck5e6d5b12009-03-13 20:40:38 +00004492 if (work_done < budget) {
Alexander Duyck46544252009-02-19 20:39:04 -08004493 napi_complete(napi);
4494 igb_rx_irq_enable(rx_ring);
4495 }
4496
4497 return work_done;
Auke Kok9d5c8242008-01-24 02:22:38 -08004498}
Al Viro6d8126f2008-03-16 22:23:24 +00004499
Auke Kok9d5c8242008-01-24 02:22:38 -08004500/**
Patrick Ohly33af6bc2009-02-12 05:03:43 +00004501 * igb_hwtstamp - utility function which checks for TX time stamp
4502 * @adapter: board private structure
4503 * @skb: packet that was just sent
4504 *
4505 * If we were asked to do hardware stamping and such a time stamp is
4506 * available, then it must have been for this skb here because we only
4507 * allow only one such packet into the queue.
4508 */
4509static void igb_tx_hwtstamp(struct igb_adapter *adapter, struct sk_buff *skb)
4510{
4511 union skb_shared_tx *shtx = skb_tx(skb);
4512 struct e1000_hw *hw = &adapter->hw;
4513
4514 if (unlikely(shtx->hardware)) {
4515 u32 valid = rd32(E1000_TSYNCTXCTL) & E1000_TSYNCTXCTL_VALID;
4516 if (valid) {
4517 u64 regval = rd32(E1000_TXSTMPL);
4518 u64 ns;
4519 struct skb_shared_hwtstamps shhwtstamps;
4520
4521 memset(&shhwtstamps, 0, sizeof(shhwtstamps));
4522 regval |= (u64)rd32(E1000_TXSTMPH) << 32;
4523 ns = timecounter_cyc2time(&adapter->clock,
4524 regval);
4525 timecompare_update(&adapter->compare, ns);
4526 shhwtstamps.hwtstamp = ns_to_ktime(ns);
4527 shhwtstamps.syststamp =
4528 timecompare_transform(&adapter->compare, ns);
4529 skb_tstamp_tx(skb, &shhwtstamps);
4530 }
Patrick Ohly33af6bc2009-02-12 05:03:43 +00004531 }
4532}
4533
4534/**
Auke Kok9d5c8242008-01-24 02:22:38 -08004535 * igb_clean_tx_irq - Reclaim resources after transmit completes
4536 * @adapter: board private structure
4537 * returns true if ring is completely cleaned
4538 **/
Mitch Williams3b644cf2008-06-27 10:59:48 -07004539static bool igb_clean_tx_irq(struct igb_ring *tx_ring)
Auke Kok9d5c8242008-01-24 02:22:38 -08004540{
Mitch Williams3b644cf2008-06-27 10:59:48 -07004541 struct igb_adapter *adapter = tx_ring->adapter;
Mitch Williams3b644cf2008-06-27 10:59:48 -07004542 struct net_device *netdev = adapter->netdev;
Alexander Duyck0e014cb2008-12-26 01:33:18 -08004543 struct e1000_hw *hw = &adapter->hw;
Auke Kok9d5c8242008-01-24 02:22:38 -08004544 struct igb_buffer *buffer_info;
4545 struct sk_buff *skb;
Alexander Duyck0e014cb2008-12-26 01:33:18 -08004546 union e1000_adv_tx_desc *tx_desc, *eop_desc;
Auke Kok9d5c8242008-01-24 02:22:38 -08004547 unsigned int total_bytes = 0, total_packets = 0;
Alexander Duyck0e014cb2008-12-26 01:33:18 -08004548 unsigned int i, eop, count = 0;
4549 bool cleaned = false;
Auke Kok9d5c8242008-01-24 02:22:38 -08004550
Auke Kok9d5c8242008-01-24 02:22:38 -08004551 i = tx_ring->next_to_clean;
Alexander Duyck0e014cb2008-12-26 01:33:18 -08004552 eop = tx_ring->buffer_info[i].next_to_watch;
4553 eop_desc = E1000_TX_DESC_ADV(*tx_ring, eop);
4554
4555 while ((eop_desc->wb.status & cpu_to_le32(E1000_TXD_STAT_DD)) &&
4556 (count < tx_ring->count)) {
4557 for (cleaned = false; !cleaned; count++) {
4558 tx_desc = E1000_TX_DESC_ADV(*tx_ring, i);
Auke Kok9d5c8242008-01-24 02:22:38 -08004559 buffer_info = &tx_ring->buffer_info[i];
Alexander Duyck0e014cb2008-12-26 01:33:18 -08004560 cleaned = (i == eop);
Auke Kok9d5c8242008-01-24 02:22:38 -08004561 skb = buffer_info->skb;
4562
4563 if (skb) {
4564 unsigned int segs, bytecount;
4565 /* gso_segs is currently only valid for tcp */
4566 segs = skb_shinfo(skb)->gso_segs ?: 1;
4567 /* multiply data chunks by size of headers */
4568 bytecount = ((segs - 1) * skb_headlen(skb)) +
4569 skb->len;
4570 total_packets += segs;
4571 total_bytes += bytecount;
Patrick Ohly33af6bc2009-02-12 05:03:43 +00004572
4573 igb_tx_hwtstamp(adapter, skb);
Auke Kok9d5c8242008-01-24 02:22:38 -08004574 }
4575
4576 igb_unmap_and_free_tx_resource(adapter, buffer_info);
Alexander Duyck0e014cb2008-12-26 01:33:18 -08004577 tx_desc->wb.status = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08004578
4579 i++;
4580 if (i == tx_ring->count)
4581 i = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08004582 }
Alexander Duyck0e014cb2008-12-26 01:33:18 -08004583 eop = tx_ring->buffer_info[i].next_to_watch;
4584 eop_desc = E1000_TX_DESC_ADV(*tx_ring, eop);
4585 }
4586
Auke Kok9d5c8242008-01-24 02:22:38 -08004587 tx_ring->next_to_clean = i;
4588
Alexander Duyckfc7d3452008-08-26 04:25:08 -07004589 if (unlikely(count &&
Auke Kok9d5c8242008-01-24 02:22:38 -08004590 netif_carrier_ok(netdev) &&
Alexander Duyckc493ea42009-03-20 00:16:50 +00004591 igb_desc_unused(tx_ring) >= IGB_TX_QUEUE_WAKE)) {
Auke Kok9d5c8242008-01-24 02:22:38 -08004592 /* Make sure that anybody stopping the queue after this
4593 * sees the new next_to_clean.
4594 */
4595 smp_mb();
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07004596 if (__netif_subqueue_stopped(netdev, tx_ring->queue_index) &&
4597 !(test_bit(__IGB_DOWN, &adapter->state))) {
4598 netif_wake_subqueue(netdev, tx_ring->queue_index);
4599 ++adapter->restart_queue;
4600 }
Auke Kok9d5c8242008-01-24 02:22:38 -08004601 }
4602
4603 if (tx_ring->detect_tx_hung) {
4604 /* Detect a transmit hang in hardware, this serializes the
4605 * check with the clearing of time_stamp and movement of i */
4606 tx_ring->detect_tx_hung = false;
4607 if (tx_ring->buffer_info[i].time_stamp &&
4608 time_after(jiffies, tx_ring->buffer_info[i].time_stamp +
4609 (adapter->tx_timeout_factor * HZ))
4610 && !(rd32(E1000_STATUS) &
4611 E1000_STATUS_TXOFF)) {
4612
Auke Kok9d5c8242008-01-24 02:22:38 -08004613 /* detected Tx unit hang */
4614 dev_err(&adapter->pdev->dev,
4615 "Detected Tx Unit Hang\n"
Alexander Duyck2d064c02008-07-08 15:10:12 -07004616 " Tx Queue <%d>\n"
Auke Kok9d5c8242008-01-24 02:22:38 -08004617 " TDH <%x>\n"
4618 " TDT <%x>\n"
4619 " next_to_use <%x>\n"
4620 " next_to_clean <%x>\n"
Auke Kok9d5c8242008-01-24 02:22:38 -08004621 "buffer_info[next_to_clean]\n"
4622 " time_stamp <%lx>\n"
Alexander Duyck0e014cb2008-12-26 01:33:18 -08004623 " next_to_watch <%x>\n"
Auke Kok9d5c8242008-01-24 02:22:38 -08004624 " jiffies <%lx>\n"
4625 " desc.status <%x>\n",
Alexander Duyck2d064c02008-07-08 15:10:12 -07004626 tx_ring->queue_index,
Auke Kok9d5c8242008-01-24 02:22:38 -08004627 readl(adapter->hw.hw_addr + tx_ring->head),
4628 readl(adapter->hw.hw_addr + tx_ring->tail),
4629 tx_ring->next_to_use,
4630 tx_ring->next_to_clean,
Auke Kok9d5c8242008-01-24 02:22:38 -08004631 tx_ring->buffer_info[i].time_stamp,
Alexander Duyck0e014cb2008-12-26 01:33:18 -08004632 eop,
Auke Kok9d5c8242008-01-24 02:22:38 -08004633 jiffies,
Alexander Duyck0e014cb2008-12-26 01:33:18 -08004634 eop_desc->wb.status);
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07004635 netif_stop_subqueue(netdev, tx_ring->queue_index);
Auke Kok9d5c8242008-01-24 02:22:38 -08004636 }
4637 }
4638 tx_ring->total_bytes += total_bytes;
4639 tx_ring->total_packets += total_packets;
Alexander Duycke21ed352008-07-08 15:07:24 -07004640 tx_ring->tx_stats.bytes += total_bytes;
4641 tx_ring->tx_stats.packets += total_packets;
Auke Kok9d5c8242008-01-24 02:22:38 -08004642 adapter->net_stats.tx_bytes += total_bytes;
4643 adapter->net_stats.tx_packets += total_packets;
Alexander Duyck0e014cb2008-12-26 01:33:18 -08004644 return (count < tx_ring->count);
Auke Kok9d5c8242008-01-24 02:22:38 -08004645}
4646
Auke Kok9d5c8242008-01-24 02:22:38 -08004647/**
4648 * igb_receive_skb - helper function to handle rx indications
Alexander Duyckeebbbdb2009-02-06 23:19:29 +00004649 * @ring: pointer to receive ring receving this packet
Auke Kok9d5c8242008-01-24 02:22:38 -08004650 * @status: descriptor status field as written by hardware
Alexander Duyck73cd78f2009-02-12 18:16:59 +00004651 * @rx_desc: receive descriptor containing vlan and type information.
Auke Kok9d5c8242008-01-24 02:22:38 -08004652 * @skb: pointer to sk_buff to be indicated to stack
4653 **/
Alexander Duyckd3352522008-07-08 15:12:13 -07004654static void igb_receive_skb(struct igb_ring *ring, u8 status,
4655 union e1000_adv_rx_desc * rx_desc,
4656 struct sk_buff *skb)
Auke Kok9d5c8242008-01-24 02:22:38 -08004657{
Alexander Duyckd3352522008-07-08 15:12:13 -07004658 struct igb_adapter * adapter = ring->adapter;
4659 bool vlan_extracted = (adapter->vlgrp && (status & E1000_RXD_STAT_VP));
4660
David S. Miller0c8dfc82009-01-27 16:22:32 -08004661 skb_record_rx_queue(skb, ring->queue_index);
Alexander Duyck182ff8d2009-04-27 22:35:33 +00004662 if (vlan_extracted)
4663 vlan_gro_receive(&ring->napi, adapter->vlgrp,
4664 le16_to_cpu(rx_desc->wb.upper.vlan),
4665 skb);
4666 else
4667 napi_gro_receive(&ring->napi, skb);
Auke Kok9d5c8242008-01-24 02:22:38 -08004668}
4669
Auke Kok9d5c8242008-01-24 02:22:38 -08004670static inline void igb_rx_checksum_adv(struct igb_adapter *adapter,
4671 u32 status_err, struct sk_buff *skb)
4672{
4673 skb->ip_summed = CHECKSUM_NONE;
4674
4675 /* Ignore Checksum bit is set or checksum is disabled through ethtool */
Alexander Duyck7beb0142009-05-06 10:25:23 +00004676 if ((status_err & E1000_RXD_STAT_IXSM) ||
4677 (adapter->flags & IGB_FLAG_RX_CSUM_DISABLED))
Auke Kok9d5c8242008-01-24 02:22:38 -08004678 return;
4679 /* TCP/UDP checksum error bit is set */
4680 if (status_err &
4681 (E1000_RXDEXT_STATERR_TCPE | E1000_RXDEXT_STATERR_IPE)) {
Jesse Brandeburgb9473562009-04-27 22:36:13 +00004682 /*
4683 * work around errata with sctp packets where the TCPE aka
4684 * L4E bit is set incorrectly on 64 byte (60 byte w/o crc)
4685 * packets, (aka let the stack check the crc32c)
4686 */
4687 if (!((adapter->hw.mac.type == e1000_82576) &&
4688 (skb->len == 60)))
4689 adapter->hw_csum_err++;
Auke Kok9d5c8242008-01-24 02:22:38 -08004690 /* let the stack verify checksum errors */
Auke Kok9d5c8242008-01-24 02:22:38 -08004691 return;
4692 }
4693 /* It must be a TCP or UDP packet with a valid checksum */
4694 if (status_err & (E1000_RXD_STAT_TCPCS | E1000_RXD_STAT_UDPCS))
4695 skb->ip_summed = CHECKSUM_UNNECESSARY;
4696
Jesse Brandeburgb9473562009-04-27 22:36:13 +00004697 dev_dbg(&adapter->pdev->dev, "cksum success: bits %08X\n", status_err);
Auke Kok9d5c8242008-01-24 02:22:38 -08004698 adapter->hw_csum_good++;
4699}
4700
Alexander Duyck2d94d8a2009-07-23 18:10:06 +00004701static inline u16 igb_get_hlen(struct igb_adapter *adapter,
4702 union e1000_adv_rx_desc *rx_desc)
4703{
4704 /* HW will not DMA in data larger than the given buffer, even if it
4705 * parses the (NFS, of course) header to be larger. In that case, it
4706 * fills the header buffer and spills the rest into the page.
4707 */
4708 u16 hlen = (le16_to_cpu(rx_desc->wb.lower.lo_dword.hdr_info) &
4709 E1000_RXDADV_HDRBUFLEN_MASK) >> E1000_RXDADV_HDRBUFLEN_SHIFT;
4710 if (hlen > adapter->rx_ps_hdr_size)
4711 hlen = adapter->rx_ps_hdr_size;
4712 return hlen;
4713}
4714
Mitch Williams3b644cf2008-06-27 10:59:48 -07004715static bool igb_clean_rx_irq_adv(struct igb_ring *rx_ring,
4716 int *work_done, int budget)
Auke Kok9d5c8242008-01-24 02:22:38 -08004717{
Mitch Williams3b644cf2008-06-27 10:59:48 -07004718 struct igb_adapter *adapter = rx_ring->adapter;
Auke Kok9d5c8242008-01-24 02:22:38 -08004719 struct net_device *netdev = adapter->netdev;
Patrick Ohly33af6bc2009-02-12 05:03:43 +00004720 struct e1000_hw *hw = &adapter->hw;
Auke Kok9d5c8242008-01-24 02:22:38 -08004721 struct pci_dev *pdev = adapter->pdev;
4722 union e1000_adv_rx_desc *rx_desc , *next_rxd;
4723 struct igb_buffer *buffer_info , *next_buffer;
4724 struct sk_buff *skb;
Auke Kok9d5c8242008-01-24 02:22:38 -08004725 bool cleaned = false;
4726 int cleaned_count = 0;
4727 unsigned int total_bytes = 0, total_packets = 0;
Alexander Duyck73cd78f2009-02-12 18:16:59 +00004728 unsigned int i;
Alexander Duyck2d94d8a2009-07-23 18:10:06 +00004729 u32 staterr;
4730 u16 length;
Auke Kok9d5c8242008-01-24 02:22:38 -08004731
4732 i = rx_ring->next_to_clean;
Alexander Duyck69d3ca52009-02-06 23:15:04 +00004733 buffer_info = &rx_ring->buffer_info[i];
Auke Kok9d5c8242008-01-24 02:22:38 -08004734 rx_desc = E1000_RX_DESC_ADV(*rx_ring, i);
4735 staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
4736
4737 while (staterr & E1000_RXD_STAT_DD) {
4738 if (*work_done >= budget)
4739 break;
4740 (*work_done)++;
Alexander Duyck69d3ca52009-02-06 23:15:04 +00004741
4742 skb = buffer_info->skb;
4743 prefetch(skb->data - NET_IP_ALIGN);
4744 buffer_info->skb = NULL;
4745
4746 i++;
4747 if (i == rx_ring->count)
4748 i = 0;
4749 next_rxd = E1000_RX_DESC_ADV(*rx_ring, i);
4750 prefetch(next_rxd);
4751 next_buffer = &rx_ring->buffer_info[i];
4752
4753 length = le16_to_cpu(rx_desc->wb.upper.length);
4754 cleaned = true;
4755 cleaned_count++;
4756
Jesse Brandeburg91615f72009-06-30 12:45:15 +00004757 /* this is the fast path for the non-packet split case */
Alexander Duyck69d3ca52009-02-06 23:15:04 +00004758 if (!adapter->rx_ps_hdr_size) {
4759 pci_unmap_single(pdev, buffer_info->dma,
Jesse Brandeburg91615f72009-06-30 12:45:15 +00004760 adapter->rx_buffer_len,
Alexander Duyck69d3ca52009-02-06 23:15:04 +00004761 PCI_DMA_FROMDEVICE);
Jesse Brandeburg91615f72009-06-30 12:45:15 +00004762 buffer_info->dma = 0;
Alexander Duyck69d3ca52009-02-06 23:15:04 +00004763 skb_put(skb, length);
4764 goto send_up;
4765 }
Auke Kok9d5c8242008-01-24 02:22:38 -08004766
Alexander Duyck2d94d8a2009-07-23 18:10:06 +00004767 if (buffer_info->dma) {
4768 u16 hlen = igb_get_hlen(adapter, rx_desc);
Alexander Duyckbf36c1a2008-07-08 15:11:40 -07004769 pci_unmap_single(pdev, buffer_info->dma,
Jesse Brandeburg91615f72009-06-30 12:45:15 +00004770 adapter->rx_ps_hdr_size,
Alexander Duyckbf36c1a2008-07-08 15:11:40 -07004771 PCI_DMA_FROMDEVICE);
Jesse Brandeburg91615f72009-06-30 12:45:15 +00004772 buffer_info->dma = 0;
Alexander Duyckbf36c1a2008-07-08 15:11:40 -07004773 skb_put(skb, hlen);
4774 }
4775
4776 if (length) {
Auke Kok9d5c8242008-01-24 02:22:38 -08004777 pci_unmap_page(pdev, buffer_info->page_dma,
Alexander Duyckbf36c1a2008-07-08 15:11:40 -07004778 PAGE_SIZE / 2, PCI_DMA_FROMDEVICE);
Auke Kok9d5c8242008-01-24 02:22:38 -08004779 buffer_info->page_dma = 0;
Alexander Duyckbf36c1a2008-07-08 15:11:40 -07004780
4781 skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags++,
4782 buffer_info->page,
4783 buffer_info->page_offset,
4784 length);
4785
4786 if ((adapter->rx_buffer_len > (PAGE_SIZE / 2)) ||
4787 (page_count(buffer_info->page) != 1))
4788 buffer_info->page = NULL;
4789 else
4790 get_page(buffer_info->page);
Auke Kok9d5c8242008-01-24 02:22:38 -08004791
4792 skb->len += length;
4793 skb->data_len += length;
Alexander Duyckbf36c1a2008-07-08 15:11:40 -07004794
Auke Kok9d5c8242008-01-24 02:22:38 -08004795 skb->truesize += length;
Auke Kok9d5c8242008-01-24 02:22:38 -08004796 }
Auke Kok9d5c8242008-01-24 02:22:38 -08004797
Alexander Duyckbf36c1a2008-07-08 15:11:40 -07004798 if (!(staterr & E1000_RXD_STAT_EOP)) {
Alexander Duyckb2d56532008-11-20 00:47:34 -08004799 buffer_info->skb = next_buffer->skb;
4800 buffer_info->dma = next_buffer->dma;
4801 next_buffer->skb = skb;
4802 next_buffer->dma = 0;
Alexander Duyckbf36c1a2008-07-08 15:11:40 -07004803 goto next_desc;
4804 }
Alexander Duyck69d3ca52009-02-06 23:15:04 +00004805send_up:
Patrick Ohly33af6bc2009-02-12 05:03:43 +00004806 /*
4807 * If this bit is set, then the RX registers contain
4808 * the time stamp. No other packet will be time
4809 * stamped until we read these registers, so read the
4810 * registers to make them available again. Because
4811 * only one packet can be time stamped at a time, we
4812 * know that the register values must belong to this
4813 * one here and therefore we don't need to compare
4814 * any of the additional attributes stored for it.
4815 *
4816 * If nothing went wrong, then it should have a
4817 * skb_shared_tx that we can turn into a
4818 * skb_shared_hwtstamps.
4819 *
4820 * TODO: can time stamping be triggered (thus locking
4821 * the registers) without the packet reaching this point
4822 * here? In that case RX time stamping would get stuck.
4823 *
4824 * TODO: in "time stamp all packets" mode this bit is
4825 * not set. Need a global flag for this mode and then
4826 * always read the registers. Cannot be done without
4827 * a race condition.
4828 */
4829 if (unlikely(staterr & E1000_RXD_STAT_TS)) {
4830 u64 regval;
4831 u64 ns;
4832 struct skb_shared_hwtstamps *shhwtstamps =
4833 skb_hwtstamps(skb);
4834
4835 WARN(!(rd32(E1000_TSYNCRXCTL) & E1000_TSYNCRXCTL_VALID),
4836 "igb: no RX time stamp available for time stamped packet");
4837 regval = rd32(E1000_RXSTMPL);
4838 regval |= (u64)rd32(E1000_RXSTMPH) << 32;
4839 ns = timecounter_cyc2time(&adapter->clock, regval);
4840 timecompare_update(&adapter->compare, ns);
4841 memset(shhwtstamps, 0, sizeof(*shhwtstamps));
4842 shhwtstamps->hwtstamp = ns_to_ktime(ns);
4843 shhwtstamps->syststamp =
4844 timecompare_transform(&adapter->compare, ns);
4845 }
4846
Auke Kok9d5c8242008-01-24 02:22:38 -08004847 if (staterr & E1000_RXDEXT_ERR_FRAME_ERR_MASK) {
4848 dev_kfree_skb_irq(skb);
4849 goto next_desc;
4850 }
Auke Kok9d5c8242008-01-24 02:22:38 -08004851
4852 total_bytes += skb->len;
4853 total_packets++;
4854
4855 igb_rx_checksum_adv(adapter, staterr, skb);
4856
4857 skb->protocol = eth_type_trans(skb, netdev);
4858
Alexander Duyckd3352522008-07-08 15:12:13 -07004859 igb_receive_skb(rx_ring, staterr, rx_desc, skb);
Auke Kok9d5c8242008-01-24 02:22:38 -08004860
Auke Kok9d5c8242008-01-24 02:22:38 -08004861next_desc:
4862 rx_desc->wb.upper.status_error = 0;
4863
4864 /* return some buffers to hardware, one at a time is too slow */
4865 if (cleaned_count >= IGB_RX_BUFFER_WRITE) {
Mitch Williams3b644cf2008-06-27 10:59:48 -07004866 igb_alloc_rx_buffers_adv(rx_ring, cleaned_count);
Auke Kok9d5c8242008-01-24 02:22:38 -08004867 cleaned_count = 0;
4868 }
4869
4870 /* use prefetched values */
4871 rx_desc = next_rxd;
4872 buffer_info = next_buffer;
Auke Kok9d5c8242008-01-24 02:22:38 -08004873 staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
4874 }
Alexander Duyckbf36c1a2008-07-08 15:11:40 -07004875
Auke Kok9d5c8242008-01-24 02:22:38 -08004876 rx_ring->next_to_clean = i;
Alexander Duyckc493ea42009-03-20 00:16:50 +00004877 cleaned_count = igb_desc_unused(rx_ring);
Auke Kok9d5c8242008-01-24 02:22:38 -08004878
4879 if (cleaned_count)
Mitch Williams3b644cf2008-06-27 10:59:48 -07004880 igb_alloc_rx_buffers_adv(rx_ring, cleaned_count);
Auke Kok9d5c8242008-01-24 02:22:38 -08004881
4882 rx_ring->total_packets += total_packets;
4883 rx_ring->total_bytes += total_bytes;
4884 rx_ring->rx_stats.packets += total_packets;
4885 rx_ring->rx_stats.bytes += total_bytes;
4886 adapter->net_stats.rx_bytes += total_bytes;
4887 adapter->net_stats.rx_packets += total_packets;
4888 return cleaned;
4889}
4890
Auke Kok9d5c8242008-01-24 02:22:38 -08004891/**
4892 * igb_alloc_rx_buffers_adv - Replace used receive buffers; packet split
4893 * @adapter: address of board private structure
4894 **/
Mitch Williams3b644cf2008-06-27 10:59:48 -07004895static void igb_alloc_rx_buffers_adv(struct igb_ring *rx_ring,
Auke Kok9d5c8242008-01-24 02:22:38 -08004896 int cleaned_count)
4897{
Mitch Williams3b644cf2008-06-27 10:59:48 -07004898 struct igb_adapter *adapter = rx_ring->adapter;
Auke Kok9d5c8242008-01-24 02:22:38 -08004899 struct net_device *netdev = adapter->netdev;
4900 struct pci_dev *pdev = adapter->pdev;
4901 union e1000_adv_rx_desc *rx_desc;
4902 struct igb_buffer *buffer_info;
4903 struct sk_buff *skb;
4904 unsigned int i;
Alexander Duyckdb761762009-02-06 23:15:25 +00004905 int bufsz;
Auke Kok9d5c8242008-01-24 02:22:38 -08004906
4907 i = rx_ring->next_to_use;
4908 buffer_info = &rx_ring->buffer_info[i];
4909
Alexander Duyckdb761762009-02-06 23:15:25 +00004910 if (adapter->rx_ps_hdr_size)
4911 bufsz = adapter->rx_ps_hdr_size;
4912 else
4913 bufsz = adapter->rx_buffer_len;
Alexander Duyckdb761762009-02-06 23:15:25 +00004914
Auke Kok9d5c8242008-01-24 02:22:38 -08004915 while (cleaned_count--) {
4916 rx_desc = E1000_RX_DESC_ADV(*rx_ring, i);
4917
Alexander Duyckbf36c1a2008-07-08 15:11:40 -07004918 if (adapter->rx_ps_hdr_size && !buffer_info->page_dma) {
Auke Kok9d5c8242008-01-24 02:22:38 -08004919 if (!buffer_info->page) {
Alexander Duyckbf36c1a2008-07-08 15:11:40 -07004920 buffer_info->page = alloc_page(GFP_ATOMIC);
4921 if (!buffer_info->page) {
4922 adapter->alloc_rx_buff_failed++;
4923 goto no_buffers;
4924 }
4925 buffer_info->page_offset = 0;
4926 } else {
4927 buffer_info->page_offset ^= PAGE_SIZE / 2;
Auke Kok9d5c8242008-01-24 02:22:38 -08004928 }
4929 buffer_info->page_dma =
Alexander Duyckdb761762009-02-06 23:15:25 +00004930 pci_map_page(pdev, buffer_info->page,
Alexander Duyckbf36c1a2008-07-08 15:11:40 -07004931 buffer_info->page_offset,
4932 PAGE_SIZE / 2,
Auke Kok9d5c8242008-01-24 02:22:38 -08004933 PCI_DMA_FROMDEVICE);
4934 }
4935
4936 if (!buffer_info->skb) {
Jesse Brandeburg91615f72009-06-30 12:45:15 +00004937 skb = netdev_alloc_skb(netdev, bufsz + NET_IP_ALIGN);
Auke Kok9d5c8242008-01-24 02:22:38 -08004938 if (!skb) {
4939 adapter->alloc_rx_buff_failed++;
4940 goto no_buffers;
4941 }
4942
4943 /* Make buffer alignment 2 beyond a 16 byte boundary
4944 * this will result in a 16 byte aligned IP header after
4945 * the 14 byte MAC header is removed
4946 */
4947 skb_reserve(skb, NET_IP_ALIGN);
4948
4949 buffer_info->skb = skb;
4950 buffer_info->dma = pci_map_single(pdev, skb->data,
4951 bufsz,
4952 PCI_DMA_FROMDEVICE);
Auke Kok9d5c8242008-01-24 02:22:38 -08004953 }
4954 /* Refresh the desc even if buffer_addrs didn't change because
4955 * each write-back erases this info. */
4956 if (adapter->rx_ps_hdr_size) {
4957 rx_desc->read.pkt_addr =
4958 cpu_to_le64(buffer_info->page_dma);
4959 rx_desc->read.hdr_addr = cpu_to_le64(buffer_info->dma);
4960 } else {
4961 rx_desc->read.pkt_addr =
4962 cpu_to_le64(buffer_info->dma);
4963 rx_desc->read.hdr_addr = 0;
4964 }
4965
4966 i++;
4967 if (i == rx_ring->count)
4968 i = 0;
4969 buffer_info = &rx_ring->buffer_info[i];
4970 }
4971
4972no_buffers:
4973 if (rx_ring->next_to_use != i) {
4974 rx_ring->next_to_use = i;
4975 if (i == 0)
4976 i = (rx_ring->count - 1);
4977 else
4978 i--;
4979
4980 /* Force memory writes to complete before letting h/w
4981 * know there are new descriptors to fetch. (Only
4982 * applicable for weak-ordered memory model archs,
4983 * such as IA-64). */
4984 wmb();
4985 writel(i, adapter->hw.hw_addr + rx_ring->tail);
4986 }
4987}
4988
4989/**
4990 * igb_mii_ioctl -
4991 * @netdev:
4992 * @ifreq:
4993 * @cmd:
4994 **/
4995static int igb_mii_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
4996{
4997 struct igb_adapter *adapter = netdev_priv(netdev);
4998 struct mii_ioctl_data *data = if_mii(ifr);
4999
5000 if (adapter->hw.phy.media_type != e1000_media_type_copper)
5001 return -EOPNOTSUPP;
5002
5003 switch (cmd) {
5004 case SIOCGMIIPHY:
5005 data->phy_id = adapter->hw.phy.addr;
5006 break;
5007 case SIOCGMIIREG:
Alexander Duyckf5f4cf02008-11-21 21:30:24 -08005008 if (igb_read_phy_reg(&adapter->hw, data->reg_num & 0x1F,
5009 &data->val_out))
Auke Kok9d5c8242008-01-24 02:22:38 -08005010 return -EIO;
5011 break;
5012 case SIOCSMIIREG:
5013 default:
5014 return -EOPNOTSUPP;
5015 }
5016 return 0;
5017}
5018
5019/**
Patrick Ohlyc6cb0902009-02-12 05:03:42 +00005020 * igb_hwtstamp_ioctl - control hardware time stamping
5021 * @netdev:
5022 * @ifreq:
5023 * @cmd:
5024 *
Patrick Ohly33af6bc2009-02-12 05:03:43 +00005025 * Outgoing time stamping can be enabled and disabled. Play nice and
5026 * disable it when requested, although it shouldn't case any overhead
5027 * when no packet needs it. At most one packet in the queue may be
5028 * marked for time stamping, otherwise it would be impossible to tell
5029 * for sure to which packet the hardware time stamp belongs.
5030 *
5031 * Incoming time stamping has to be configured via the hardware
5032 * filters. Not all combinations are supported, in particular event
5033 * type has to be specified. Matching the kind of event packet is
5034 * not supported, with the exception of "all V2 events regardless of
5035 * level 2 or 4".
5036 *
Patrick Ohlyc6cb0902009-02-12 05:03:42 +00005037 **/
5038static int igb_hwtstamp_ioctl(struct net_device *netdev,
5039 struct ifreq *ifr, int cmd)
5040{
Patrick Ohly33af6bc2009-02-12 05:03:43 +00005041 struct igb_adapter *adapter = netdev_priv(netdev);
5042 struct e1000_hw *hw = &adapter->hw;
Patrick Ohlyc6cb0902009-02-12 05:03:42 +00005043 struct hwtstamp_config config;
Patrick Ohly33af6bc2009-02-12 05:03:43 +00005044 u32 tsync_tx_ctl_bit = E1000_TSYNCTXCTL_ENABLED;
5045 u32 tsync_rx_ctl_bit = E1000_TSYNCRXCTL_ENABLED;
5046 u32 tsync_rx_ctl_type = 0;
5047 u32 tsync_rx_cfg = 0;
5048 int is_l4 = 0;
5049 int is_l2 = 0;
5050 short port = 319; /* PTP */
5051 u32 regval;
Patrick Ohlyc6cb0902009-02-12 05:03:42 +00005052
5053 if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
5054 return -EFAULT;
5055
5056 /* reserved for future extensions */
5057 if (config.flags)
5058 return -EINVAL;
5059
Patrick Ohly33af6bc2009-02-12 05:03:43 +00005060 switch (config.tx_type) {
5061 case HWTSTAMP_TX_OFF:
5062 tsync_tx_ctl_bit = 0;
5063 break;
5064 case HWTSTAMP_TX_ON:
5065 tsync_tx_ctl_bit = E1000_TSYNCTXCTL_ENABLED;
5066 break;
5067 default:
5068 return -ERANGE;
5069 }
Patrick Ohlyc6cb0902009-02-12 05:03:42 +00005070
Patrick Ohly33af6bc2009-02-12 05:03:43 +00005071 switch (config.rx_filter) {
5072 case HWTSTAMP_FILTER_NONE:
5073 tsync_rx_ctl_bit = 0;
5074 break;
5075 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
5076 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
5077 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
5078 case HWTSTAMP_FILTER_ALL:
5079 /*
5080 * register TSYNCRXCFG must be set, therefore it is not
5081 * possible to time stamp both Sync and Delay_Req messages
5082 * => fall back to time stamping all packets
5083 */
5084 tsync_rx_ctl_type = E1000_TSYNCRXCTL_TYPE_ALL;
5085 config.rx_filter = HWTSTAMP_FILTER_ALL;
5086 break;
5087 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
5088 tsync_rx_ctl_type = E1000_TSYNCRXCTL_TYPE_L4_V1;
5089 tsync_rx_cfg = E1000_TSYNCRXCFG_PTP_V1_SYNC_MESSAGE;
5090 is_l4 = 1;
5091 break;
5092 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
5093 tsync_rx_ctl_type = E1000_TSYNCRXCTL_TYPE_L4_V1;
5094 tsync_rx_cfg = E1000_TSYNCRXCFG_PTP_V1_DELAY_REQ_MESSAGE;
5095 is_l4 = 1;
5096 break;
5097 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
5098 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
5099 tsync_rx_ctl_type = E1000_TSYNCRXCTL_TYPE_L2_L4_V2;
5100 tsync_rx_cfg = E1000_TSYNCRXCFG_PTP_V2_SYNC_MESSAGE;
5101 is_l2 = 1;
5102 is_l4 = 1;
5103 config.rx_filter = HWTSTAMP_FILTER_SOME;
5104 break;
5105 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
5106 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
5107 tsync_rx_ctl_type = E1000_TSYNCRXCTL_TYPE_L2_L4_V2;
5108 tsync_rx_cfg = E1000_TSYNCRXCFG_PTP_V2_DELAY_REQ_MESSAGE;
5109 is_l2 = 1;
5110 is_l4 = 1;
5111 config.rx_filter = HWTSTAMP_FILTER_SOME;
5112 break;
5113 case HWTSTAMP_FILTER_PTP_V2_EVENT:
5114 case HWTSTAMP_FILTER_PTP_V2_SYNC:
5115 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
5116 tsync_rx_ctl_type = E1000_TSYNCRXCTL_TYPE_EVENT_V2;
5117 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
5118 is_l2 = 1;
5119 break;
5120 default:
5121 return -ERANGE;
5122 }
5123
5124 /* enable/disable TX */
5125 regval = rd32(E1000_TSYNCTXCTL);
5126 regval = (regval & ~E1000_TSYNCTXCTL_ENABLED) | tsync_tx_ctl_bit;
5127 wr32(E1000_TSYNCTXCTL, regval);
5128
5129 /* enable/disable RX, define which PTP packets are time stamped */
5130 regval = rd32(E1000_TSYNCRXCTL);
5131 regval = (regval & ~E1000_TSYNCRXCTL_ENABLED) | tsync_rx_ctl_bit;
5132 regval = (regval & ~0xE) | tsync_rx_ctl_type;
5133 wr32(E1000_TSYNCRXCTL, regval);
5134 wr32(E1000_TSYNCRXCFG, tsync_rx_cfg);
5135
5136 /*
5137 * Ethertype Filter Queue Filter[0][15:0] = 0x88F7
5138 * (Ethertype to filter on)
5139 * Ethertype Filter Queue Filter[0][26] = 0x1 (Enable filter)
5140 * Ethertype Filter Queue Filter[0][30] = 0x1 (Enable Timestamping)
5141 */
5142 wr32(E1000_ETQF0, is_l2 ? 0x440088f7 : 0);
5143
5144 /* L4 Queue Filter[0]: only filter by source and destination port */
5145 wr32(E1000_SPQF0, htons(port));
5146 wr32(E1000_IMIREXT(0), is_l4 ?
5147 ((1<<12) | (1<<19) /* bypass size and control flags */) : 0);
5148 wr32(E1000_IMIR(0), is_l4 ?
5149 (htons(port)
5150 | (0<<16) /* immediate interrupt disabled */
5151 | 0 /* (1<<17) bit cleared: do not bypass
5152 destination port check */)
5153 : 0);
5154 wr32(E1000_FTQF0, is_l4 ?
5155 (0x11 /* UDP */
5156 | (1<<15) /* VF not compared */
5157 | (1<<27) /* Enable Timestamping */
5158 | (7<<28) /* only source port filter enabled,
5159 source/target address and protocol
5160 masked */)
5161 : ((1<<15) | (15<<28) /* all mask bits set = filter not
5162 enabled */));
5163
5164 wrfl();
5165
5166 adapter->hwtstamp_config = config;
5167
5168 /* clear TX/RX time stamp registers, just to be sure */
5169 regval = rd32(E1000_TXSTMPH);
5170 regval = rd32(E1000_RXSTMPH);
5171
5172 return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
5173 -EFAULT : 0;
Patrick Ohlyc6cb0902009-02-12 05:03:42 +00005174}
5175
5176/**
Auke Kok9d5c8242008-01-24 02:22:38 -08005177 * igb_ioctl -
5178 * @netdev:
5179 * @ifreq:
5180 * @cmd:
5181 **/
5182static int igb_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
5183{
5184 switch (cmd) {
5185 case SIOCGMIIPHY:
5186 case SIOCGMIIREG:
5187 case SIOCSMIIREG:
5188 return igb_mii_ioctl(netdev, ifr, cmd);
Patrick Ohlyc6cb0902009-02-12 05:03:42 +00005189 case SIOCSHWTSTAMP:
5190 return igb_hwtstamp_ioctl(netdev, ifr, cmd);
Auke Kok9d5c8242008-01-24 02:22:38 -08005191 default:
5192 return -EOPNOTSUPP;
5193 }
5194}
5195
Alexander Duyck009bc062009-07-23 18:08:35 +00005196s32 igb_read_pcie_cap_reg(struct e1000_hw *hw, u32 reg, u16 *value)
5197{
5198 struct igb_adapter *adapter = hw->back;
5199 u16 cap_offset;
5200
5201 cap_offset = pci_find_capability(adapter->pdev, PCI_CAP_ID_EXP);
5202 if (!cap_offset)
5203 return -E1000_ERR_CONFIG;
5204
5205 pci_read_config_word(adapter->pdev, cap_offset + reg, value);
5206
5207 return 0;
5208}
5209
5210s32 igb_write_pcie_cap_reg(struct e1000_hw *hw, u32 reg, u16 *value)
5211{
5212 struct igb_adapter *adapter = hw->back;
5213 u16 cap_offset;
5214
5215 cap_offset = pci_find_capability(adapter->pdev, PCI_CAP_ID_EXP);
5216 if (!cap_offset)
5217 return -E1000_ERR_CONFIG;
5218
5219 pci_write_config_word(adapter->pdev, cap_offset + reg, *value);
5220
5221 return 0;
5222}
5223
Auke Kok9d5c8242008-01-24 02:22:38 -08005224static void igb_vlan_rx_register(struct net_device *netdev,
5225 struct vlan_group *grp)
5226{
5227 struct igb_adapter *adapter = netdev_priv(netdev);
5228 struct e1000_hw *hw = &adapter->hw;
5229 u32 ctrl, rctl;
5230
5231 igb_irq_disable(adapter);
5232 adapter->vlgrp = grp;
5233
5234 if (grp) {
5235 /* enable VLAN tag insert/strip */
5236 ctrl = rd32(E1000_CTRL);
5237 ctrl |= E1000_CTRL_VME;
5238 wr32(E1000_CTRL, ctrl);
5239
5240 /* enable VLAN receive filtering */
5241 rctl = rd32(E1000_RCTL);
Auke Kok9d5c8242008-01-24 02:22:38 -08005242 rctl &= ~E1000_RCTL_CFIEN;
5243 wr32(E1000_RCTL, rctl);
5244 igb_update_mng_vlan(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08005245 } else {
5246 /* disable VLAN tag insert/strip */
5247 ctrl = rd32(E1000_CTRL);
5248 ctrl &= ~E1000_CTRL_VME;
5249 wr32(E1000_CTRL, ctrl);
5250
Auke Kok9d5c8242008-01-24 02:22:38 -08005251 if (adapter->mng_vlan_id != (u16)IGB_MNG_VLAN_NONE) {
5252 igb_vlan_rx_kill_vid(netdev, adapter->mng_vlan_id);
5253 adapter->mng_vlan_id = IGB_MNG_VLAN_NONE;
5254 }
Auke Kok9d5c8242008-01-24 02:22:38 -08005255 }
5256
Alexander Duycke1739522009-02-19 20:39:44 -08005257 igb_rlpml_set(adapter);
5258
Auke Kok9d5c8242008-01-24 02:22:38 -08005259 if (!test_bit(__IGB_DOWN, &adapter->state))
5260 igb_irq_enable(adapter);
5261}
5262
5263static void igb_vlan_rx_add_vid(struct net_device *netdev, u16 vid)
5264{
5265 struct igb_adapter *adapter = netdev_priv(netdev);
5266 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005267 int pf_id = adapter->vfs_allocated_count;
Auke Kok9d5c8242008-01-24 02:22:38 -08005268
Alexander Duyck28b07592009-02-06 23:20:31 +00005269 if ((hw->mng_cookie.status &
Auke Kok9d5c8242008-01-24 02:22:38 -08005270 E1000_MNG_DHCP_COOKIE_STATUS_VLAN) &&
5271 (vid == adapter->mng_vlan_id))
5272 return;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005273
5274 /* add vid to vlvf if sr-iov is enabled,
5275 * if that fails add directly to filter table */
5276 if (igb_vlvf_set(adapter, vid, true, pf_id))
5277 igb_vfta_set(hw, vid, true);
5278
Auke Kok9d5c8242008-01-24 02:22:38 -08005279}
5280
5281static void igb_vlan_rx_kill_vid(struct net_device *netdev, u16 vid)
5282{
5283 struct igb_adapter *adapter = netdev_priv(netdev);
5284 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005285 int pf_id = adapter->vfs_allocated_count;
Auke Kok9d5c8242008-01-24 02:22:38 -08005286
5287 igb_irq_disable(adapter);
5288 vlan_group_set_device(adapter->vlgrp, vid, NULL);
5289
5290 if (!test_bit(__IGB_DOWN, &adapter->state))
5291 igb_irq_enable(adapter);
5292
5293 if ((adapter->hw.mng_cookie.status &
5294 E1000_MNG_DHCP_COOKIE_STATUS_VLAN) &&
5295 (vid == adapter->mng_vlan_id)) {
5296 /* release control to f/w */
5297 igb_release_hw_control(adapter);
5298 return;
5299 }
5300
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005301 /* remove vid from vlvf if sr-iov is enabled,
5302 * if not in vlvf remove from vfta */
5303 if (igb_vlvf_set(adapter, vid, false, pf_id))
5304 igb_vfta_set(hw, vid, false);
Auke Kok9d5c8242008-01-24 02:22:38 -08005305}
5306
5307static void igb_restore_vlan(struct igb_adapter *adapter)
5308{
5309 igb_vlan_rx_register(adapter->netdev, adapter->vlgrp);
5310
5311 if (adapter->vlgrp) {
5312 u16 vid;
5313 for (vid = 0; vid < VLAN_GROUP_ARRAY_LEN; vid++) {
5314 if (!vlan_group_get_device(adapter->vlgrp, vid))
5315 continue;
5316 igb_vlan_rx_add_vid(adapter->netdev, vid);
5317 }
5318 }
5319}
5320
5321int igb_set_spd_dplx(struct igb_adapter *adapter, u16 spddplx)
5322{
5323 struct e1000_mac_info *mac = &adapter->hw.mac;
5324
5325 mac->autoneg = 0;
5326
Auke Kok9d5c8242008-01-24 02:22:38 -08005327 switch (spddplx) {
5328 case SPEED_10 + DUPLEX_HALF:
5329 mac->forced_speed_duplex = ADVERTISE_10_HALF;
5330 break;
5331 case SPEED_10 + DUPLEX_FULL:
5332 mac->forced_speed_duplex = ADVERTISE_10_FULL;
5333 break;
5334 case SPEED_100 + DUPLEX_HALF:
5335 mac->forced_speed_duplex = ADVERTISE_100_HALF;
5336 break;
5337 case SPEED_100 + DUPLEX_FULL:
5338 mac->forced_speed_duplex = ADVERTISE_100_FULL;
5339 break;
5340 case SPEED_1000 + DUPLEX_FULL:
5341 mac->autoneg = 1;
5342 adapter->hw.phy.autoneg_advertised = ADVERTISE_1000_FULL;
5343 break;
5344 case SPEED_1000 + DUPLEX_HALF: /* not supported */
5345 default:
5346 dev_err(&adapter->pdev->dev,
5347 "Unsupported Speed/Duplex configuration\n");
5348 return -EINVAL;
5349 }
5350 return 0;
5351}
5352
Rafael J. Wysocki3fe7c4c2009-03-31 21:23:50 +00005353static int __igb_shutdown(struct pci_dev *pdev, bool *enable_wake)
Auke Kok9d5c8242008-01-24 02:22:38 -08005354{
5355 struct net_device *netdev = pci_get_drvdata(pdev);
5356 struct igb_adapter *adapter = netdev_priv(netdev);
5357 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck2d064c02008-07-08 15:10:12 -07005358 u32 ctrl, rctl, status;
Auke Kok9d5c8242008-01-24 02:22:38 -08005359 u32 wufc = adapter->wol;
5360#ifdef CONFIG_PM
5361 int retval = 0;
5362#endif
5363
5364 netif_device_detach(netdev);
5365
Alexander Duycka88f10e2008-07-08 15:13:38 -07005366 if (netif_running(netdev))
5367 igb_close(netdev);
5368
5369 igb_reset_interrupt_capability(adapter);
5370
5371 igb_free_queues(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08005372
5373#ifdef CONFIG_PM
5374 retval = pci_save_state(pdev);
5375 if (retval)
5376 return retval;
5377#endif
5378
5379 status = rd32(E1000_STATUS);
5380 if (status & E1000_STATUS_LU)
5381 wufc &= ~E1000_WUFC_LNKC;
5382
5383 if (wufc) {
5384 igb_setup_rctl(adapter);
Alexander Duyckff41f8d2009-09-03 14:48:56 +00005385 igb_set_rx_mode(netdev);
Auke Kok9d5c8242008-01-24 02:22:38 -08005386
5387 /* turn on all-multi mode if wake on multicast is enabled */
5388 if (wufc & E1000_WUFC_MC) {
5389 rctl = rd32(E1000_RCTL);
5390 rctl |= E1000_RCTL_MPE;
5391 wr32(E1000_RCTL, rctl);
5392 }
5393
5394 ctrl = rd32(E1000_CTRL);
5395 /* advertise wake from D3Cold */
5396 #define E1000_CTRL_ADVD3WUC 0x00100000
5397 /* phy power management enable */
5398 #define E1000_CTRL_EN_PHY_PWR_MGMT 0x00200000
5399 ctrl |= E1000_CTRL_ADVD3WUC;
5400 wr32(E1000_CTRL, ctrl);
5401
Auke Kok9d5c8242008-01-24 02:22:38 -08005402 /* Allow time for pending master requests to run */
5403 igb_disable_pcie_master(&adapter->hw);
5404
5405 wr32(E1000_WUC, E1000_WUC_PME_EN);
5406 wr32(E1000_WUFC, wufc);
Auke Kok9d5c8242008-01-24 02:22:38 -08005407 } else {
5408 wr32(E1000_WUC, 0);
5409 wr32(E1000_WUFC, 0);
Auke Kok9d5c8242008-01-24 02:22:38 -08005410 }
5411
Rafael J. Wysocki3fe7c4c2009-03-31 21:23:50 +00005412 *enable_wake = wufc || adapter->en_mng_pt;
5413 if (!*enable_wake)
Alexander Duyck2fb02a22009-09-14 08:22:54 +00005414 igb_shutdown_serdes_link_82575(hw);
Auke Kok9d5c8242008-01-24 02:22:38 -08005415
5416 /* Release control of h/w to f/w. If f/w is AMT enabled, this
5417 * would have already happened in close and is redundant. */
5418 igb_release_hw_control(adapter);
5419
5420 pci_disable_device(pdev);
5421
Auke Kok9d5c8242008-01-24 02:22:38 -08005422 return 0;
5423}
5424
5425#ifdef CONFIG_PM
Rafael J. Wysocki3fe7c4c2009-03-31 21:23:50 +00005426static int igb_suspend(struct pci_dev *pdev, pm_message_t state)
5427{
5428 int retval;
5429 bool wake;
5430
5431 retval = __igb_shutdown(pdev, &wake);
5432 if (retval)
5433 return retval;
5434
5435 if (wake) {
5436 pci_prepare_to_sleep(pdev);
5437 } else {
5438 pci_wake_from_d3(pdev, false);
5439 pci_set_power_state(pdev, PCI_D3hot);
5440 }
5441
5442 return 0;
5443}
5444
Auke Kok9d5c8242008-01-24 02:22:38 -08005445static int igb_resume(struct pci_dev *pdev)
5446{
5447 struct net_device *netdev = pci_get_drvdata(pdev);
5448 struct igb_adapter *adapter = netdev_priv(netdev);
5449 struct e1000_hw *hw = &adapter->hw;
5450 u32 err;
5451
5452 pci_set_power_state(pdev, PCI_D0);
5453 pci_restore_state(pdev);
Taku Izumi42bfd33a2008-06-20 12:10:30 +09005454
Alexander Duyckaed5dec2009-02-06 23:16:04 +00005455 err = pci_enable_device_mem(pdev);
Auke Kok9d5c8242008-01-24 02:22:38 -08005456 if (err) {
5457 dev_err(&pdev->dev,
5458 "igb: Cannot enable PCI device from suspend\n");
5459 return err;
5460 }
5461 pci_set_master(pdev);
5462
5463 pci_enable_wake(pdev, PCI_D3hot, 0);
5464 pci_enable_wake(pdev, PCI_D3cold, 0);
5465
Alexander Duycka88f10e2008-07-08 15:13:38 -07005466 igb_set_interrupt_capability(adapter);
5467
5468 if (igb_alloc_queues(adapter)) {
5469 dev_err(&pdev->dev, "Unable to allocate memory for queues\n");
5470 return -ENOMEM;
Auke Kok9d5c8242008-01-24 02:22:38 -08005471 }
5472
5473 /* e1000_power_up_phy(adapter); */
5474
5475 igb_reset(adapter);
Alexander Duycka8564f02009-02-06 23:21:10 +00005476
5477 /* let the f/w know that the h/w is now under the control of the
5478 * driver. */
5479 igb_get_hw_control(adapter);
5480
Auke Kok9d5c8242008-01-24 02:22:38 -08005481 wr32(E1000_WUS, ~0);
5482
Alexander Duycka88f10e2008-07-08 15:13:38 -07005483 if (netif_running(netdev)) {
5484 err = igb_open(netdev);
5485 if (err)
5486 return err;
5487 }
Auke Kok9d5c8242008-01-24 02:22:38 -08005488
5489 netif_device_attach(netdev);
5490
Auke Kok9d5c8242008-01-24 02:22:38 -08005491 return 0;
5492}
5493#endif
5494
5495static void igb_shutdown(struct pci_dev *pdev)
5496{
Rafael J. Wysocki3fe7c4c2009-03-31 21:23:50 +00005497 bool wake;
5498
5499 __igb_shutdown(pdev, &wake);
5500
5501 if (system_state == SYSTEM_POWER_OFF) {
5502 pci_wake_from_d3(pdev, wake);
5503 pci_set_power_state(pdev, PCI_D3hot);
5504 }
Auke Kok9d5c8242008-01-24 02:22:38 -08005505}
5506
5507#ifdef CONFIG_NET_POLL_CONTROLLER
5508/*
5509 * Polling 'interrupt' - used by things like netconsole to send skbs
5510 * without having to re-enable interrupts. It's not called while
5511 * the interrupt routine is executing.
5512 */
5513static void igb_netpoll(struct net_device *netdev)
5514{
5515 struct igb_adapter *adapter = netdev_priv(netdev);
Alexander Duyckeebbbdb2009-02-06 23:19:29 +00005516 struct e1000_hw *hw = &adapter->hw;
Auke Kok9d5c8242008-01-24 02:22:38 -08005517 int i;
Auke Kok9d5c8242008-01-24 02:22:38 -08005518
Alexander Duyckeebbbdb2009-02-06 23:19:29 +00005519 if (!adapter->msix_entries) {
5520 igb_irq_disable(adapter);
5521 napi_schedule(&adapter->rx_ring[0].napi);
5522 return;
5523 }
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07005524
Alexander Duyckeebbbdb2009-02-06 23:19:29 +00005525 for (i = 0; i < adapter->num_tx_queues; i++) {
5526 struct igb_ring *tx_ring = &adapter->tx_ring[i];
5527 wr32(E1000_EIMC, tx_ring->eims_value);
5528 igb_clean_tx_irq(tx_ring);
5529 wr32(E1000_EIMS, tx_ring->eims_value);
5530 }
Auke Kok9d5c8242008-01-24 02:22:38 -08005531
Alexander Duyckeebbbdb2009-02-06 23:19:29 +00005532 for (i = 0; i < adapter->num_rx_queues; i++) {
5533 struct igb_ring *rx_ring = &adapter->rx_ring[i];
5534 wr32(E1000_EIMC, rx_ring->eims_value);
5535 napi_schedule(&rx_ring->napi);
5536 }
Auke Kok9d5c8242008-01-24 02:22:38 -08005537}
5538#endif /* CONFIG_NET_POLL_CONTROLLER */
5539
5540/**
5541 * igb_io_error_detected - called when PCI error is detected
5542 * @pdev: Pointer to PCI device
5543 * @state: The current pci connection state
5544 *
5545 * This function is called after a PCI bus error affecting
5546 * this device has been detected.
5547 */
5548static pci_ers_result_t igb_io_error_detected(struct pci_dev *pdev,
5549 pci_channel_state_t state)
5550{
5551 struct net_device *netdev = pci_get_drvdata(pdev);
5552 struct igb_adapter *adapter = netdev_priv(netdev);
5553
5554 netif_device_detach(netdev);
5555
Alexander Duyck59ed6ee2009-06-30 12:46:34 +00005556 if (state == pci_channel_io_perm_failure)
5557 return PCI_ERS_RESULT_DISCONNECT;
5558
Auke Kok9d5c8242008-01-24 02:22:38 -08005559 if (netif_running(netdev))
5560 igb_down(adapter);
5561 pci_disable_device(pdev);
5562
5563 /* Request a slot slot reset. */
5564 return PCI_ERS_RESULT_NEED_RESET;
5565}
5566
5567/**
5568 * igb_io_slot_reset - called after the pci bus has been reset.
5569 * @pdev: Pointer to PCI device
5570 *
5571 * Restart the card from scratch, as if from a cold-boot. Implementation
5572 * resembles the first-half of the igb_resume routine.
5573 */
5574static pci_ers_result_t igb_io_slot_reset(struct pci_dev *pdev)
5575{
5576 struct net_device *netdev = pci_get_drvdata(pdev);
5577 struct igb_adapter *adapter = netdev_priv(netdev);
5578 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck40a914f2008-11-27 00:24:37 -08005579 pci_ers_result_t result;
Taku Izumi42bfd33a2008-06-20 12:10:30 +09005580 int err;
Auke Kok9d5c8242008-01-24 02:22:38 -08005581
Alexander Duyckaed5dec2009-02-06 23:16:04 +00005582 if (pci_enable_device_mem(pdev)) {
Auke Kok9d5c8242008-01-24 02:22:38 -08005583 dev_err(&pdev->dev,
5584 "Cannot re-enable PCI device after reset.\n");
Alexander Duyck40a914f2008-11-27 00:24:37 -08005585 result = PCI_ERS_RESULT_DISCONNECT;
5586 } else {
5587 pci_set_master(pdev);
5588 pci_restore_state(pdev);
5589
5590 pci_enable_wake(pdev, PCI_D3hot, 0);
5591 pci_enable_wake(pdev, PCI_D3cold, 0);
5592
5593 igb_reset(adapter);
5594 wr32(E1000_WUS, ~0);
5595 result = PCI_ERS_RESULT_RECOVERED;
Auke Kok9d5c8242008-01-24 02:22:38 -08005596 }
Auke Kok9d5c8242008-01-24 02:22:38 -08005597
Jeff Kirsherea943d42008-12-11 20:34:19 -08005598 err = pci_cleanup_aer_uncorrect_error_status(pdev);
5599 if (err) {
5600 dev_err(&pdev->dev, "pci_cleanup_aer_uncorrect_error_status "
5601 "failed 0x%0x\n", err);
5602 /* non-fatal, continue */
5603 }
Auke Kok9d5c8242008-01-24 02:22:38 -08005604
Alexander Duyck40a914f2008-11-27 00:24:37 -08005605 return result;
Auke Kok9d5c8242008-01-24 02:22:38 -08005606}
5607
5608/**
5609 * igb_io_resume - called when traffic can start flowing again.
5610 * @pdev: Pointer to PCI device
5611 *
5612 * This callback is called when the error recovery driver tells us that
5613 * its OK to resume normal operation. Implementation resembles the
5614 * second-half of the igb_resume routine.
5615 */
5616static void igb_io_resume(struct pci_dev *pdev)
5617{
5618 struct net_device *netdev = pci_get_drvdata(pdev);
5619 struct igb_adapter *adapter = netdev_priv(netdev);
5620
Auke Kok9d5c8242008-01-24 02:22:38 -08005621 if (netif_running(netdev)) {
5622 if (igb_up(adapter)) {
5623 dev_err(&pdev->dev, "igb_up failed after reset\n");
5624 return;
5625 }
5626 }
5627
5628 netif_device_attach(netdev);
5629
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);
Auke Kok9d5c8242008-01-24 02:22:38 -08005633}
5634
Alexander Duyck26ad9172009-10-05 06:32:49 +00005635static void igb_rar_set_qsel(struct igb_adapter *adapter, u8 *addr, u32 index,
5636 u8 qsel)
5637{
5638 u32 rar_low, rar_high;
5639 struct e1000_hw *hw = &adapter->hw;
5640
5641 /* HW expects these in little endian so we reverse the byte order
5642 * from network order (big endian) to little endian
5643 */
5644 rar_low = ((u32) addr[0] | ((u32) addr[1] << 8) |
5645 ((u32) addr[2] << 16) | ((u32) addr[3] << 24));
5646 rar_high = ((u32) addr[4] | ((u32) addr[5] << 8));
5647
5648 /* Indicate to hardware the Address is Valid. */
5649 rar_high |= E1000_RAH_AV;
5650
5651 if (hw->mac.type == e1000_82575)
5652 rar_high |= E1000_RAH_POOL_1 * qsel;
5653 else
5654 rar_high |= E1000_RAH_POOL_1 << qsel;
5655
5656 wr32(E1000_RAL(index), rar_low);
5657 wrfl();
5658 wr32(E1000_RAH(index), rar_high);
5659 wrfl();
5660}
5661
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005662static int igb_set_vf_mac(struct igb_adapter *adapter,
5663 int vf, unsigned char *mac_addr)
5664{
5665 struct e1000_hw *hw = &adapter->hw;
Alexander Duyckff41f8d2009-09-03 14:48:56 +00005666 /* VF MAC addresses start at end of receive addresses and moves
5667 * torwards the first, as a result a collision should not be possible */
5668 int rar_entry = hw->mac.rar_entry_count - (vf + 1);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005669
Alexander Duyck37680112009-02-19 20:40:30 -08005670 memcpy(adapter->vf_data[vf].vf_mac_addresses, mac_addr, ETH_ALEN);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005671
Alexander Duyck26ad9172009-10-05 06:32:49 +00005672 igb_rar_set_qsel(adapter, mac_addr, rar_entry, vf);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005673
5674 return 0;
5675}
5676
5677static void igb_vmm_control(struct igb_adapter *adapter)
5678{
5679 struct e1000_hw *hw = &adapter->hw;
5680 u32 reg_data;
5681
5682 if (!adapter->vfs_allocated_count)
5683 return;
5684
5685 /* VF's need PF reset indication before they
5686 * can send/receive mail */
5687 reg_data = rd32(E1000_CTRL_EXT);
5688 reg_data |= E1000_CTRL_EXT_PFRSTD;
5689 wr32(E1000_CTRL_EXT, reg_data);
5690
5691 igb_vmdq_set_loopback_pf(hw, true);
5692 igb_vmdq_set_replication_pf(hw, true);
5693}
5694
Auke Kok9d5c8242008-01-24 02:22:38 -08005695/* igb_main.c */