igb: use timecompare to implement hardware time stamping
[safe/jmp/linux-2.6] / drivers / net / igb / igb_main.c
1 /*******************************************************************************
2
3   Intel(R) Gigabit Ethernet Linux driver
4   Copyright(c) 2007-2009 Intel Corporation.
5
6   This program is free software; you can redistribute it and/or modify it
7   under the terms and conditions of the GNU General Public License,
8   version 2, as published by the Free Software Foundation.
9
10   This program is distributed in the hope it will be useful, but WITHOUT
11   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13   more details.
14
15   You should have received a copy of the GNU General Public License along with
16   this program; if not, write to the Free Software Foundation, Inc.,
17   51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
18
19   The full GNU General Public License is included in this distribution in
20   the file called "COPYING".
21
22   Contact Information:
23   e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
24   Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
25
26 *******************************************************************************/
27
28 #include <linux/module.h>
29 #include <linux/types.h>
30 #include <linux/init.h>
31 #include <linux/vmalloc.h>
32 #include <linux/pagemap.h>
33 #include <linux/netdevice.h>
34 #include <linux/ipv6.h>
35 #include <net/checksum.h>
36 #include <net/ip6_checksum.h>
37 #include <linux/net_tstamp.h>
38 #include <linux/mii.h>
39 #include <linux/ethtool.h>
40 #include <linux/if_vlan.h>
41 #include <linux/pci.h>
42 #include <linux/pci-aspm.h>
43 #include <linux/delay.h>
44 #include <linux/interrupt.h>
45 #include <linux/if_ether.h>
46 #include <linux/aer.h>
47 #ifdef CONFIG_IGB_DCA
48 #include <linux/dca.h>
49 #endif
50 #include "igb.h"
51
52 #define DRV_VERSION "1.3.16-k2"
53 char igb_driver_name[] = "igb";
54 char igb_driver_version[] = DRV_VERSION;
55 static const char igb_driver_string[] =
56                                 "Intel(R) Gigabit Ethernet Network Driver";
57 static const char igb_copyright[] = "Copyright (c) 2007-2009 Intel Corporation.";
58
59 static const struct e1000_info *igb_info_tbl[] = {
60         [board_82575] = &e1000_82575_info,
61 };
62
63 static struct pci_device_id igb_pci_tbl[] = {
64         { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576), board_82575 },
65         { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_FIBER), board_82575 },
66         { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_SERDES), board_82575 },
67         { PCI_VDEVICE(INTEL, E1000_DEV_ID_82575EB_COPPER), board_82575 },
68         { PCI_VDEVICE(INTEL, E1000_DEV_ID_82575EB_FIBER_SERDES), board_82575 },
69         { PCI_VDEVICE(INTEL, E1000_DEV_ID_82575GB_QUAD_COPPER), board_82575 },
70         /* required last entry */
71         {0, }
72 };
73
74 MODULE_DEVICE_TABLE(pci, igb_pci_tbl);
75
76 void igb_reset(struct igb_adapter *);
77 static int igb_setup_all_tx_resources(struct igb_adapter *);
78 static int igb_setup_all_rx_resources(struct igb_adapter *);
79 static void igb_free_all_tx_resources(struct igb_adapter *);
80 static void igb_free_all_rx_resources(struct igb_adapter *);
81 void igb_update_stats(struct igb_adapter *);
82 static int igb_probe(struct pci_dev *, const struct pci_device_id *);
83 static void __devexit igb_remove(struct pci_dev *pdev);
84 static int igb_sw_init(struct igb_adapter *);
85 static int igb_open(struct net_device *);
86 static int igb_close(struct net_device *);
87 static void igb_configure_tx(struct igb_adapter *);
88 static void igb_configure_rx(struct igb_adapter *);
89 static void igb_setup_rctl(struct igb_adapter *);
90 static void igb_clean_all_tx_rings(struct igb_adapter *);
91 static void igb_clean_all_rx_rings(struct igb_adapter *);
92 static void igb_clean_tx_ring(struct igb_ring *);
93 static void igb_clean_rx_ring(struct igb_ring *);
94 static void igb_set_multi(struct net_device *);
95 static void igb_update_phy_info(unsigned long);
96 static void igb_watchdog(unsigned long);
97 static void igb_watchdog_task(struct work_struct *);
98 static int igb_xmit_frame_ring_adv(struct sk_buff *, struct net_device *,
99                                   struct igb_ring *);
100 static int igb_xmit_frame_adv(struct sk_buff *skb, struct net_device *);
101 static struct net_device_stats *igb_get_stats(struct net_device *);
102 static int igb_change_mtu(struct net_device *, int);
103 static int igb_set_mac(struct net_device *, void *);
104 static irqreturn_t igb_intr(int irq, void *);
105 static irqreturn_t igb_intr_msi(int irq, void *);
106 static irqreturn_t igb_msix_other(int irq, void *);
107 static irqreturn_t igb_msix_rx(int irq, void *);
108 static irqreturn_t igb_msix_tx(int irq, void *);
109 static int igb_clean_rx_ring_msix(struct napi_struct *, int);
110 #ifdef CONFIG_IGB_DCA
111 static void igb_update_rx_dca(struct igb_ring *);
112 static void igb_update_tx_dca(struct igb_ring *);
113 static void igb_setup_dca(struct igb_adapter *);
114 #endif /* CONFIG_IGB_DCA */
115 static bool igb_clean_tx_irq(struct igb_ring *);
116 static int igb_poll(struct napi_struct *, int);
117 static bool igb_clean_rx_irq_adv(struct igb_ring *, int *, int);
118 static void igb_alloc_rx_buffers_adv(struct igb_ring *, int);
119 static int igb_ioctl(struct net_device *, struct ifreq *, int cmd);
120 static void igb_tx_timeout(struct net_device *);
121 static void igb_reset_task(struct work_struct *);
122 static void igb_vlan_rx_register(struct net_device *, struct vlan_group *);
123 static void igb_vlan_rx_add_vid(struct net_device *, u16);
124 static void igb_vlan_rx_kill_vid(struct net_device *, u16);
125 static void igb_restore_vlan(struct igb_adapter *);
126
127 static int igb_suspend(struct pci_dev *, pm_message_t);
128 #ifdef CONFIG_PM
129 static int igb_resume(struct pci_dev *);
130 #endif
131 static void igb_shutdown(struct pci_dev *);
132 #ifdef CONFIG_IGB_DCA
133 static int igb_notify_dca(struct notifier_block *, unsigned long, void *);
134 static struct notifier_block dca_notifier = {
135         .notifier_call  = igb_notify_dca,
136         .next           = NULL,
137         .priority       = 0
138 };
139 #endif
140
141 #ifdef CONFIG_NET_POLL_CONTROLLER
142 /* for netdump / net console */
143 static void igb_netpoll(struct net_device *);
144 #endif
145
146 static pci_ers_result_t igb_io_error_detected(struct pci_dev *,
147                      pci_channel_state_t);
148 static pci_ers_result_t igb_io_slot_reset(struct pci_dev *);
149 static void igb_io_resume(struct pci_dev *);
150
151 static struct pci_error_handlers igb_err_handler = {
152         .error_detected = igb_io_error_detected,
153         .slot_reset = igb_io_slot_reset,
154         .resume = igb_io_resume,
155 };
156
157
158 static struct pci_driver igb_driver = {
159         .name     = igb_driver_name,
160         .id_table = igb_pci_tbl,
161         .probe    = igb_probe,
162         .remove   = __devexit_p(igb_remove),
163 #ifdef CONFIG_PM
164         /* Power Managment Hooks */
165         .suspend  = igb_suspend,
166         .resume   = igb_resume,
167 #endif
168         .shutdown = igb_shutdown,
169         .err_handler = &igb_err_handler
170 };
171
172 static int global_quad_port_a; /* global quad port a indication */
173
174 MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
175 MODULE_DESCRIPTION("Intel(R) Gigabit Ethernet Network Driver");
176 MODULE_LICENSE("GPL");
177 MODULE_VERSION(DRV_VERSION);
178
179 /**
180  * Scale the NIC clock cycle by a large factor so that
181  * relatively small clock corrections can be added or
182  * substracted at each clock tick. The drawbacks of a
183  * large factor are a) that the clock register overflows
184  * more quickly (not such a big deal) and b) that the
185  * increment per tick has to fit into 24 bits.
186  *
187  * Note that
188  *   TIMINCA = IGB_TSYNC_CYCLE_TIME_IN_NANOSECONDS *
189  *             IGB_TSYNC_SCALE
190  *   TIMINCA += TIMINCA * adjustment [ppm] / 1e9
191  *
192  * The base scale factor is intentionally a power of two
193  * so that the division in %struct timecounter can be done with
194  * a shift.
195  */
196 #define IGB_TSYNC_SHIFT (19)
197 #define IGB_TSYNC_SCALE (1<<IGB_TSYNC_SHIFT)
198
199 /**
200  * The duration of one clock cycle of the NIC.
201  *
202  * @todo This hard-coded value is part of the specification and might change
203  * in future hardware revisions. Add revision check.
204  */
205 #define IGB_TSYNC_CYCLE_TIME_IN_NANOSECONDS 16
206
207 #if (IGB_TSYNC_SCALE * IGB_TSYNC_CYCLE_TIME_IN_NANOSECONDS) >= (1<<24)
208 # error IGB_TSYNC_SCALE and/or IGB_TSYNC_CYCLE_TIME_IN_NANOSECONDS are too large to fit into TIMINCA
209 #endif
210
211 /**
212  * igb_read_clock - read raw cycle counter (to be used by time counter)
213  */
214 static cycle_t igb_read_clock(const struct cyclecounter *tc)
215 {
216         struct igb_adapter *adapter =
217                 container_of(tc, struct igb_adapter, cycles);
218         struct e1000_hw *hw = &adapter->hw;
219         u64 stamp;
220
221         stamp =  rd32(E1000_SYSTIML);
222         stamp |= (u64)rd32(E1000_SYSTIMH) << 32ULL;
223
224         return stamp;
225 }
226
227 #ifdef DEBUG
228 /**
229  * igb_get_hw_dev_name - return device name string
230  * used by hardware layer to print debugging information
231  **/
232 char *igb_get_hw_dev_name(struct e1000_hw *hw)
233 {
234         struct igb_adapter *adapter = hw->back;
235         return adapter->netdev->name;
236 }
237
238 /**
239  * igb_get_time_str - format current NIC and system time as string
240  */
241 static char *igb_get_time_str(struct igb_adapter *adapter,
242                               char buffer[160])
243 {
244         cycle_t hw = adapter->cycles.read(&adapter->cycles);
245         struct timespec nic = ns_to_timespec(timecounter_read(&adapter->clock));
246         struct timespec sys;
247         struct timespec delta;
248         getnstimeofday(&sys);
249
250         delta = timespec_sub(nic, sys);
251
252         sprintf(buffer,
253                 "HW %llu, NIC %ld.%09lus, SYS %ld.%09lus, NIC-SYS %lds + %09luns",
254                 hw,
255                 (long)nic.tv_sec, nic.tv_nsec,
256                 (long)sys.tv_sec, sys.tv_nsec,
257                 (long)delta.tv_sec, delta.tv_nsec);
258
259         return buffer;
260 }
261 #endif
262
263 /**
264  * igb_init_module - Driver Registration Routine
265  *
266  * igb_init_module is the first routine called when the driver is
267  * loaded. All it does is register with the PCI subsystem.
268  **/
269 static int __init igb_init_module(void)
270 {
271         int ret;
272         printk(KERN_INFO "%s - version %s\n",
273                igb_driver_string, igb_driver_version);
274
275         printk(KERN_INFO "%s\n", igb_copyright);
276
277         global_quad_port_a = 0;
278
279 #ifdef CONFIG_IGB_DCA
280         dca_register_notify(&dca_notifier);
281 #endif
282
283         ret = pci_register_driver(&igb_driver);
284         return ret;
285 }
286
287 module_init(igb_init_module);
288
289 /**
290  * igb_exit_module - Driver Exit Cleanup Routine
291  *
292  * igb_exit_module is called just before the driver is removed
293  * from memory.
294  **/
295 static void __exit igb_exit_module(void)
296 {
297 #ifdef CONFIG_IGB_DCA
298         dca_unregister_notify(&dca_notifier);
299 #endif
300         pci_unregister_driver(&igb_driver);
301 }
302
303 module_exit(igb_exit_module);
304
305 #define Q_IDX_82576(i) (((i & 0x1) << 3) + (i >> 1))
306 /**
307  * igb_cache_ring_register - Descriptor ring to register mapping
308  * @adapter: board private structure to initialize
309  *
310  * Once we know the feature-set enabled for the device, we'll cache
311  * the register offset the descriptor ring is assigned to.
312  **/
313 static void igb_cache_ring_register(struct igb_adapter *adapter)
314 {
315         int i;
316
317         switch (adapter->hw.mac.type) {
318         case e1000_82576:
319                 /* The queues are allocated for virtualization such that VF 0
320                  * is allocated queues 0 and 8, VF 1 queues 1 and 9, etc.
321                  * In order to avoid collision we start at the first free queue
322                  * and continue consuming queues in the same sequence
323                  */
324                 for (i = 0; i < adapter->num_rx_queues; i++)
325                         adapter->rx_ring[i].reg_idx = Q_IDX_82576(i);
326                 for (i = 0; i < adapter->num_tx_queues; i++)
327                         adapter->tx_ring[i].reg_idx = Q_IDX_82576(i);
328                 break;
329         case e1000_82575:
330         default:
331                 for (i = 0; i < adapter->num_rx_queues; i++)
332                         adapter->rx_ring[i].reg_idx = i;
333                 for (i = 0; i < adapter->num_tx_queues; i++)
334                         adapter->tx_ring[i].reg_idx = i;
335                 break;
336         }
337 }
338
339 /**
340  * igb_alloc_queues - Allocate memory for all rings
341  * @adapter: board private structure to initialize
342  *
343  * We allocate one ring per queue at run-time since we don't know the
344  * number of queues at compile-time.
345  **/
346 static int igb_alloc_queues(struct igb_adapter *adapter)
347 {
348         int i;
349
350         adapter->tx_ring = kcalloc(adapter->num_tx_queues,
351                                    sizeof(struct igb_ring), GFP_KERNEL);
352         if (!adapter->tx_ring)
353                 return -ENOMEM;
354
355         adapter->rx_ring = kcalloc(adapter->num_rx_queues,
356                                    sizeof(struct igb_ring), GFP_KERNEL);
357         if (!adapter->rx_ring) {
358                 kfree(adapter->tx_ring);
359                 return -ENOMEM;
360         }
361
362         adapter->rx_ring->buddy = adapter->tx_ring;
363
364         for (i = 0; i < adapter->num_tx_queues; i++) {
365                 struct igb_ring *ring = &(adapter->tx_ring[i]);
366                 ring->count = adapter->tx_ring_count;
367                 ring->adapter = adapter;
368                 ring->queue_index = i;
369         }
370         for (i = 0; i < adapter->num_rx_queues; i++) {
371                 struct igb_ring *ring = &(adapter->rx_ring[i]);
372                 ring->count = adapter->rx_ring_count;
373                 ring->adapter = adapter;
374                 ring->queue_index = i;
375                 ring->itr_register = E1000_ITR;
376
377                 /* set a default napi handler for each rx_ring */
378                 netif_napi_add(adapter->netdev, &ring->napi, igb_poll, 64);
379         }
380
381         igb_cache_ring_register(adapter);
382         return 0;
383 }
384
385 static void igb_free_queues(struct igb_adapter *adapter)
386 {
387         int i;
388
389         for (i = 0; i < adapter->num_rx_queues; i++)
390                 netif_napi_del(&adapter->rx_ring[i].napi);
391
392         kfree(adapter->tx_ring);
393         kfree(adapter->rx_ring);
394 }
395
396 #define IGB_N0_QUEUE -1
397 static void igb_assign_vector(struct igb_adapter *adapter, int rx_queue,
398                               int tx_queue, int msix_vector)
399 {
400         u32 msixbm = 0;
401         struct e1000_hw *hw = &adapter->hw;
402         u32 ivar, index;
403
404         switch (hw->mac.type) {
405         case e1000_82575:
406                 /* The 82575 assigns vectors using a bitmask, which matches the
407                    bitmask for the EICR/EIMS/EIMC registers.  To assign one
408                    or more queues to a vector, we write the appropriate bits
409                    into the MSIXBM register for that vector. */
410                 if (rx_queue > IGB_N0_QUEUE) {
411                         msixbm = E1000_EICR_RX_QUEUE0 << rx_queue;
412                         adapter->rx_ring[rx_queue].eims_value = msixbm;
413                 }
414                 if (tx_queue > IGB_N0_QUEUE) {
415                         msixbm |= E1000_EICR_TX_QUEUE0 << tx_queue;
416                         adapter->tx_ring[tx_queue].eims_value =
417                                   E1000_EICR_TX_QUEUE0 << tx_queue;
418                 }
419                 array_wr32(E1000_MSIXBM(0), msix_vector, msixbm);
420                 break;
421         case e1000_82576:
422                 /* 82576 uses a table-based method for assigning vectors.
423                    Each queue has a single entry in the table to which we write
424                    a vector number along with a "valid" bit.  Sadly, the layout
425                    of the table is somewhat counterintuitive. */
426                 if (rx_queue > IGB_N0_QUEUE) {
427                         index = (rx_queue >> 1);
428                         ivar = array_rd32(E1000_IVAR0, index);
429                         if (rx_queue & 0x1) {
430                                 /* vector goes into third byte of register */
431                                 ivar = ivar & 0xFF00FFFF;
432                                 ivar |= (msix_vector | E1000_IVAR_VALID) << 16;
433                         } else {
434                                 /* vector goes into low byte of register */
435                                 ivar = ivar & 0xFFFFFF00;
436                                 ivar |= msix_vector | E1000_IVAR_VALID;
437                         }
438                         adapter->rx_ring[rx_queue].eims_value= 1 << msix_vector;
439                         array_wr32(E1000_IVAR0, index, ivar);
440                 }
441                 if (tx_queue > IGB_N0_QUEUE) {
442                         index = (tx_queue >> 1);
443                         ivar = array_rd32(E1000_IVAR0, index);
444                         if (tx_queue & 0x1) {
445                                 /* vector goes into high byte of register */
446                                 ivar = ivar & 0x00FFFFFF;
447                                 ivar |= (msix_vector | E1000_IVAR_VALID) << 24;
448                         } else {
449                                 /* vector goes into second byte of register */
450                                 ivar = ivar & 0xFFFF00FF;
451                                 ivar |= (msix_vector | E1000_IVAR_VALID) << 8;
452                         }
453                         adapter->tx_ring[tx_queue].eims_value= 1 << msix_vector;
454                         array_wr32(E1000_IVAR0, index, ivar);
455                 }
456                 break;
457         default:
458                 BUG();
459                 break;
460         }
461 }
462
463 /**
464  * igb_configure_msix - Configure MSI-X hardware
465  *
466  * igb_configure_msix sets up the hardware to properly
467  * generate MSI-X interrupts.
468  **/
469 static void igb_configure_msix(struct igb_adapter *adapter)
470 {
471         u32 tmp;
472         int i, vector = 0;
473         struct e1000_hw *hw = &adapter->hw;
474
475         adapter->eims_enable_mask = 0;
476         if (hw->mac.type == e1000_82576)
477                 /* Turn on MSI-X capability first, or our settings
478                  * won't stick.  And it will take days to debug. */
479                 wr32(E1000_GPIE, E1000_GPIE_MSIX_MODE |
480                                    E1000_GPIE_PBA | E1000_GPIE_EIAME |
481                                    E1000_GPIE_NSICR);
482
483         for (i = 0; i < adapter->num_tx_queues; i++) {
484                 struct igb_ring *tx_ring = &adapter->tx_ring[i];
485                 igb_assign_vector(adapter, IGB_N0_QUEUE, i, vector++);
486                 adapter->eims_enable_mask |= tx_ring->eims_value;
487                 if (tx_ring->itr_val)
488                         writel(tx_ring->itr_val,
489                                hw->hw_addr + tx_ring->itr_register);
490                 else
491                         writel(1, hw->hw_addr + tx_ring->itr_register);
492         }
493
494         for (i = 0; i < adapter->num_rx_queues; i++) {
495                 struct igb_ring *rx_ring = &adapter->rx_ring[i];
496                 rx_ring->buddy = NULL;
497                 igb_assign_vector(adapter, i, IGB_N0_QUEUE, vector++);
498                 adapter->eims_enable_mask |= rx_ring->eims_value;
499                 if (rx_ring->itr_val)
500                         writel(rx_ring->itr_val,
501                                hw->hw_addr + rx_ring->itr_register);
502                 else
503                         writel(1, hw->hw_addr + rx_ring->itr_register);
504         }
505
506
507         /* set vector for other causes, i.e. link changes */
508         switch (hw->mac.type) {
509         case e1000_82575:
510                 array_wr32(E1000_MSIXBM(0), vector++,
511                                       E1000_EIMS_OTHER);
512
513                 tmp = rd32(E1000_CTRL_EXT);
514                 /* enable MSI-X PBA support*/
515                 tmp |= E1000_CTRL_EXT_PBA_CLR;
516
517                 /* Auto-Mask interrupts upon ICR read. */
518                 tmp |= E1000_CTRL_EXT_EIAME;
519                 tmp |= E1000_CTRL_EXT_IRCA;
520
521                 wr32(E1000_CTRL_EXT, tmp);
522                 adapter->eims_enable_mask |= E1000_EIMS_OTHER;
523                 adapter->eims_other = E1000_EIMS_OTHER;
524
525                 break;
526
527         case e1000_82576:
528                 tmp = (vector++ | E1000_IVAR_VALID) << 8;
529                 wr32(E1000_IVAR_MISC, tmp);
530
531                 adapter->eims_enable_mask = (1 << (vector)) - 1;
532                 adapter->eims_other = 1 << (vector - 1);
533                 break;
534         default:
535                 /* do nothing, since nothing else supports MSI-X */
536                 break;
537         } /* switch (hw->mac.type) */
538         wrfl();
539 }
540
541 /**
542  * igb_request_msix - Initialize MSI-X interrupts
543  *
544  * igb_request_msix allocates MSI-X vectors and requests interrupts from the
545  * kernel.
546  **/
547 static int igb_request_msix(struct igb_adapter *adapter)
548 {
549         struct net_device *netdev = adapter->netdev;
550         int i, err = 0, vector = 0;
551
552         vector = 0;
553
554         for (i = 0; i < adapter->num_tx_queues; i++) {
555                 struct igb_ring *ring = &(adapter->tx_ring[i]);
556                 sprintf(ring->name, "%s-tx-%d", netdev->name, i);
557                 err = request_irq(adapter->msix_entries[vector].vector,
558                                   &igb_msix_tx, 0, ring->name,
559                                   &(adapter->tx_ring[i]));
560                 if (err)
561                         goto out;
562                 ring->itr_register = E1000_EITR(0) + (vector << 2);
563                 ring->itr_val = 976; /* ~4000 ints/sec */
564                 vector++;
565         }
566         for (i = 0; i < adapter->num_rx_queues; i++) {
567                 struct igb_ring *ring = &(adapter->rx_ring[i]);
568                 if (strlen(netdev->name) < (IFNAMSIZ - 5))
569                         sprintf(ring->name, "%s-rx-%d", netdev->name, i);
570                 else
571                         memcpy(ring->name, netdev->name, IFNAMSIZ);
572                 err = request_irq(adapter->msix_entries[vector].vector,
573                                   &igb_msix_rx, 0, ring->name,
574                                   &(adapter->rx_ring[i]));
575                 if (err)
576                         goto out;
577                 ring->itr_register = E1000_EITR(0) + (vector << 2);
578                 ring->itr_val = adapter->itr;
579                 /* overwrite the poll routine for MSIX, we've already done
580                  * netif_napi_add */
581                 ring->napi.poll = &igb_clean_rx_ring_msix;
582                 vector++;
583         }
584
585         err = request_irq(adapter->msix_entries[vector].vector,
586                           &igb_msix_other, 0, netdev->name, netdev);
587         if (err)
588                 goto out;
589
590         igb_configure_msix(adapter);
591         return 0;
592 out:
593         return err;
594 }
595
596 static void igb_reset_interrupt_capability(struct igb_adapter *adapter)
597 {
598         if (adapter->msix_entries) {
599                 pci_disable_msix(adapter->pdev);
600                 kfree(adapter->msix_entries);
601                 adapter->msix_entries = NULL;
602         } else if (adapter->flags & IGB_FLAG_HAS_MSI)
603                 pci_disable_msi(adapter->pdev);
604         return;
605 }
606
607
608 /**
609  * igb_set_interrupt_capability - set MSI or MSI-X if supported
610  *
611  * Attempt to configure interrupts using the best available
612  * capabilities of the hardware and kernel.
613  **/
614 static void igb_set_interrupt_capability(struct igb_adapter *adapter)
615 {
616         int err;
617         int numvecs, i;
618
619         /* Number of supported queues. */
620         /* Having more queues than CPUs doesn't make sense. */
621         adapter->num_rx_queues = min_t(u32, IGB_MAX_RX_QUEUES, num_online_cpus());
622         adapter->num_tx_queues = min_t(u32, IGB_MAX_TX_QUEUES, num_online_cpus());
623
624         numvecs = adapter->num_tx_queues + adapter->num_rx_queues + 1;
625         adapter->msix_entries = kcalloc(numvecs, sizeof(struct msix_entry),
626                                         GFP_KERNEL);
627         if (!adapter->msix_entries)
628                 goto msi_only;
629
630         for (i = 0; i < numvecs; i++)
631                 adapter->msix_entries[i].entry = i;
632
633         err = pci_enable_msix(adapter->pdev,
634                               adapter->msix_entries,
635                               numvecs);
636         if (err == 0)
637                 goto out;
638
639         igb_reset_interrupt_capability(adapter);
640
641         /* If we can't do MSI-X, try MSI */
642 msi_only:
643         adapter->num_rx_queues = 1;
644         adapter->num_tx_queues = 1;
645         if (!pci_enable_msi(adapter->pdev))
646                 adapter->flags |= IGB_FLAG_HAS_MSI;
647 out:
648         /* Notify the stack of the (possibly) reduced Tx Queue count. */
649         adapter->netdev->real_num_tx_queues = adapter->num_tx_queues;
650         return;
651 }
652
653 /**
654  * igb_request_irq - initialize interrupts
655  *
656  * Attempts to configure interrupts using the best available
657  * capabilities of the hardware and kernel.
658  **/
659 static int igb_request_irq(struct igb_adapter *adapter)
660 {
661         struct net_device *netdev = adapter->netdev;
662         struct e1000_hw *hw = &adapter->hw;
663         int err = 0;
664
665         if (adapter->msix_entries) {
666                 err = igb_request_msix(adapter);
667                 if (!err)
668                         goto request_done;
669                 /* fall back to MSI */
670                 igb_reset_interrupt_capability(adapter);
671                 if (!pci_enable_msi(adapter->pdev))
672                         adapter->flags |= IGB_FLAG_HAS_MSI;
673                 igb_free_all_tx_resources(adapter);
674                 igb_free_all_rx_resources(adapter);
675                 adapter->num_rx_queues = 1;
676                 igb_alloc_queues(adapter);
677         } else {
678                 switch (hw->mac.type) {
679                 case e1000_82575:
680                         wr32(E1000_MSIXBM(0),
681                              (E1000_EICR_RX_QUEUE0 | E1000_EIMS_OTHER));
682                         break;
683                 case e1000_82576:
684                         wr32(E1000_IVAR0, E1000_IVAR_VALID);
685                         break;
686                 default:
687                         break;
688                 }
689         }
690
691         if (adapter->flags & IGB_FLAG_HAS_MSI) {
692                 err = request_irq(adapter->pdev->irq, &igb_intr_msi, 0,
693                                   netdev->name, netdev);
694                 if (!err)
695                         goto request_done;
696                 /* fall back to legacy interrupts */
697                 igb_reset_interrupt_capability(adapter);
698                 adapter->flags &= ~IGB_FLAG_HAS_MSI;
699         }
700
701         err = request_irq(adapter->pdev->irq, &igb_intr, IRQF_SHARED,
702                           netdev->name, netdev);
703
704         if (err)
705                 dev_err(&adapter->pdev->dev, "Error %d getting interrupt\n",
706                         err);
707
708 request_done:
709         return err;
710 }
711
712 static void igb_free_irq(struct igb_adapter *adapter)
713 {
714         struct net_device *netdev = adapter->netdev;
715
716         if (adapter->msix_entries) {
717                 int vector = 0, i;
718
719                 for (i = 0; i < adapter->num_tx_queues; i++)
720                         free_irq(adapter->msix_entries[vector++].vector,
721                                 &(adapter->tx_ring[i]));
722                 for (i = 0; i < adapter->num_rx_queues; i++)
723                         free_irq(adapter->msix_entries[vector++].vector,
724                                 &(adapter->rx_ring[i]));
725
726                 free_irq(adapter->msix_entries[vector++].vector, netdev);
727                 return;
728         }
729
730         free_irq(adapter->pdev->irq, netdev);
731 }
732
733 /**
734  * igb_irq_disable - Mask off interrupt generation on the NIC
735  * @adapter: board private structure
736  **/
737 static void igb_irq_disable(struct igb_adapter *adapter)
738 {
739         struct e1000_hw *hw = &adapter->hw;
740
741         if (adapter->msix_entries) {
742                 wr32(E1000_EIAM, 0);
743                 wr32(E1000_EIMC, ~0);
744                 wr32(E1000_EIAC, 0);
745         }
746
747         wr32(E1000_IAM, 0);
748         wr32(E1000_IMC, ~0);
749         wrfl();
750         synchronize_irq(adapter->pdev->irq);
751 }
752
753 /**
754  * igb_irq_enable - Enable default interrupt generation settings
755  * @adapter: board private structure
756  **/
757 static void igb_irq_enable(struct igb_adapter *adapter)
758 {
759         struct e1000_hw *hw = &adapter->hw;
760
761         if (adapter->msix_entries) {
762                 wr32(E1000_EIAC, adapter->eims_enable_mask);
763                 wr32(E1000_EIAM, adapter->eims_enable_mask);
764                 wr32(E1000_EIMS, adapter->eims_enable_mask);
765                 wr32(E1000_IMS, E1000_IMS_LSC | E1000_IMS_DOUTSYNC);
766         } else {
767                 wr32(E1000_IMS, IMS_ENABLE_MASK);
768                 wr32(E1000_IAM, IMS_ENABLE_MASK);
769         }
770 }
771
772 static void igb_update_mng_vlan(struct igb_adapter *adapter)
773 {
774         struct net_device *netdev = adapter->netdev;
775         u16 vid = adapter->hw.mng_cookie.vlan_id;
776         u16 old_vid = adapter->mng_vlan_id;
777         if (adapter->vlgrp) {
778                 if (!vlan_group_get_device(adapter->vlgrp, vid)) {
779                         if (adapter->hw.mng_cookie.status &
780                                 E1000_MNG_DHCP_COOKIE_STATUS_VLAN) {
781                                 igb_vlan_rx_add_vid(netdev, vid);
782                                 adapter->mng_vlan_id = vid;
783                         } else
784                                 adapter->mng_vlan_id = IGB_MNG_VLAN_NONE;
785
786                         if ((old_vid != (u16)IGB_MNG_VLAN_NONE) &&
787                                         (vid != old_vid) &&
788                             !vlan_group_get_device(adapter->vlgrp, old_vid))
789                                 igb_vlan_rx_kill_vid(netdev, old_vid);
790                 } else
791                         adapter->mng_vlan_id = vid;
792         }
793 }
794
795 /**
796  * igb_release_hw_control - release control of the h/w to f/w
797  * @adapter: address of board private structure
798  *
799  * igb_release_hw_control resets CTRL_EXT:DRV_LOAD bit.
800  * For ASF and Pass Through versions of f/w this means that the
801  * driver is no longer loaded.
802  *
803  **/
804 static void igb_release_hw_control(struct igb_adapter *adapter)
805 {
806         struct e1000_hw *hw = &adapter->hw;
807         u32 ctrl_ext;
808
809         /* Let firmware take over control of h/w */
810         ctrl_ext = rd32(E1000_CTRL_EXT);
811         wr32(E1000_CTRL_EXT,
812                         ctrl_ext & ~E1000_CTRL_EXT_DRV_LOAD);
813 }
814
815
816 /**
817  * igb_get_hw_control - get control of the h/w from f/w
818  * @adapter: address of board private structure
819  *
820  * igb_get_hw_control sets CTRL_EXT:DRV_LOAD bit.
821  * For ASF and Pass Through versions of f/w this means that
822  * the driver is loaded.
823  *
824  **/
825 static void igb_get_hw_control(struct igb_adapter *adapter)
826 {
827         struct e1000_hw *hw = &adapter->hw;
828         u32 ctrl_ext;
829
830         /* Let firmware know the driver has taken over */
831         ctrl_ext = rd32(E1000_CTRL_EXT);
832         wr32(E1000_CTRL_EXT,
833                         ctrl_ext | E1000_CTRL_EXT_DRV_LOAD);
834 }
835
836 /**
837  * igb_configure - configure the hardware for RX and TX
838  * @adapter: private board structure
839  **/
840 static void igb_configure(struct igb_adapter *adapter)
841 {
842         struct net_device *netdev = adapter->netdev;
843         int i;
844
845         igb_get_hw_control(adapter);
846         igb_set_multi(netdev);
847
848         igb_restore_vlan(adapter);
849
850         igb_configure_tx(adapter);
851         igb_setup_rctl(adapter);
852         igb_configure_rx(adapter);
853
854         igb_rx_fifo_flush_82575(&adapter->hw);
855
856         /* call IGB_DESC_UNUSED which always leaves
857          * at least 1 descriptor unused to make sure
858          * next_to_use != next_to_clean */
859         for (i = 0; i < adapter->num_rx_queues; i++) {
860                 struct igb_ring *ring = &adapter->rx_ring[i];
861                 igb_alloc_rx_buffers_adv(ring, IGB_DESC_UNUSED(ring));
862         }
863
864
865         adapter->tx_queue_len = netdev->tx_queue_len;
866 }
867
868
869 /**
870  * igb_up - Open the interface and prepare it to handle traffic
871  * @adapter: board private structure
872  **/
873
874 int igb_up(struct igb_adapter *adapter)
875 {
876         struct e1000_hw *hw = &adapter->hw;
877         int i;
878
879         /* hardware has been reset, we need to reload some things */
880         igb_configure(adapter);
881
882         clear_bit(__IGB_DOWN, &adapter->state);
883
884         for (i = 0; i < adapter->num_rx_queues; i++)
885                 napi_enable(&adapter->rx_ring[i].napi);
886         if (adapter->msix_entries)
887                 igb_configure_msix(adapter);
888
889         /* Clear any pending interrupts. */
890         rd32(E1000_ICR);
891         igb_irq_enable(adapter);
892
893         /* Fire a link change interrupt to start the watchdog. */
894         wr32(E1000_ICS, E1000_ICS_LSC);
895         return 0;
896 }
897
898 void igb_down(struct igb_adapter *adapter)
899 {
900         struct e1000_hw *hw = &adapter->hw;
901         struct net_device *netdev = adapter->netdev;
902         u32 tctl, rctl;
903         int i;
904
905         /* signal that we're down so the interrupt handler does not
906          * reschedule our watchdog timer */
907         set_bit(__IGB_DOWN, &adapter->state);
908
909         /* disable receives in the hardware */
910         rctl = rd32(E1000_RCTL);
911         wr32(E1000_RCTL, rctl & ~E1000_RCTL_EN);
912         /* flush and sleep below */
913
914         netif_tx_stop_all_queues(netdev);
915
916         /* disable transmits in the hardware */
917         tctl = rd32(E1000_TCTL);
918         tctl &= ~E1000_TCTL_EN;
919         wr32(E1000_TCTL, tctl);
920         /* flush both disables and wait for them to finish */
921         wrfl();
922         msleep(10);
923
924         for (i = 0; i < adapter->num_rx_queues; i++)
925                 napi_disable(&adapter->rx_ring[i].napi);
926
927         igb_irq_disable(adapter);
928
929         del_timer_sync(&adapter->watchdog_timer);
930         del_timer_sync(&adapter->phy_info_timer);
931
932         netdev->tx_queue_len = adapter->tx_queue_len;
933         netif_carrier_off(netdev);
934
935         /* record the stats before reset*/
936         igb_update_stats(adapter);
937
938         adapter->link_speed = 0;
939         adapter->link_duplex = 0;
940
941         if (!pci_channel_offline(adapter->pdev))
942                 igb_reset(adapter);
943         igb_clean_all_tx_rings(adapter);
944         igb_clean_all_rx_rings(adapter);
945 }
946
947 void igb_reinit_locked(struct igb_adapter *adapter)
948 {
949         WARN_ON(in_interrupt());
950         while (test_and_set_bit(__IGB_RESETTING, &adapter->state))
951                 msleep(1);
952         igb_down(adapter);
953         igb_up(adapter);
954         clear_bit(__IGB_RESETTING, &adapter->state);
955 }
956
957 void igb_reset(struct igb_adapter *adapter)
958 {
959         struct e1000_hw *hw = &adapter->hw;
960         struct e1000_mac_info *mac = &hw->mac;
961         struct e1000_fc_info *fc = &hw->fc;
962         u32 pba = 0, tx_space, min_tx_space, min_rx_space;
963         u16 hwm;
964
965         /* Repartition Pba for greater than 9k mtu
966          * To take effect CTRL.RST is required.
967          */
968         switch (mac->type) {
969         case e1000_82576:
970                 pba = E1000_PBA_64K;
971                 break;
972         case e1000_82575:
973         default:
974                 pba = E1000_PBA_34K;
975                 break;
976         }
977
978         if ((adapter->max_frame_size > ETH_FRAME_LEN + ETH_FCS_LEN) &&
979             (mac->type < e1000_82576)) {
980                 /* adjust PBA for jumbo frames */
981                 wr32(E1000_PBA, pba);
982
983                 /* To maintain wire speed transmits, the Tx FIFO should be
984                  * large enough to accommodate two full transmit packets,
985                  * rounded up to the next 1KB and expressed in KB.  Likewise,
986                  * the Rx FIFO should be large enough to accommodate at least
987                  * one full receive packet and is similarly rounded up and
988                  * expressed in KB. */
989                 pba = rd32(E1000_PBA);
990                 /* upper 16 bits has Tx packet buffer allocation size in KB */
991                 tx_space = pba >> 16;
992                 /* lower 16 bits has Rx packet buffer allocation size in KB */
993                 pba &= 0xffff;
994                 /* the tx fifo also stores 16 bytes of information about the tx
995                  * but don't include ethernet FCS because hardware appends it */
996                 min_tx_space = (adapter->max_frame_size +
997                                 sizeof(struct e1000_tx_desc) -
998                                 ETH_FCS_LEN) * 2;
999                 min_tx_space = ALIGN(min_tx_space, 1024);
1000                 min_tx_space >>= 10;
1001                 /* software strips receive CRC, so leave room for it */
1002                 min_rx_space = adapter->max_frame_size;
1003                 min_rx_space = ALIGN(min_rx_space, 1024);
1004                 min_rx_space >>= 10;
1005
1006                 /* If current Tx allocation is less than the min Tx FIFO size,
1007                  * and the min Tx FIFO size is less than the current Rx FIFO
1008                  * allocation, take space away from current Rx allocation */
1009                 if (tx_space < min_tx_space &&
1010                     ((min_tx_space - tx_space) < pba)) {
1011                         pba = pba - (min_tx_space - tx_space);
1012
1013                         /* if short on rx space, rx wins and must trump tx
1014                          * adjustment */
1015                         if (pba < min_rx_space)
1016                                 pba = min_rx_space;
1017                 }
1018                 wr32(E1000_PBA, pba);
1019         }
1020
1021         /* flow control settings */
1022         /* The high water mark must be low enough to fit one full frame
1023          * (or the size used for early receive) above it in the Rx FIFO.
1024          * Set it to the lower of:
1025          * - 90% of the Rx FIFO size, or
1026          * - the full Rx FIFO size minus one full frame */
1027         hwm = min(((pba << 10) * 9 / 10),
1028                         ((pba << 10) - 2 * adapter->max_frame_size));
1029
1030         if (mac->type < e1000_82576) {
1031                 fc->high_water = hwm & 0xFFF8;  /* 8-byte granularity */
1032                 fc->low_water = fc->high_water - 8;
1033         } else {
1034                 fc->high_water = hwm & 0xFFF0;  /* 16-byte granularity */
1035                 fc->low_water = fc->high_water - 16;
1036         }
1037         fc->pause_time = 0xFFFF;
1038         fc->send_xon = 1;
1039         fc->type = fc->original_type;
1040
1041         /* Allow time for pending master requests to run */
1042         adapter->hw.mac.ops.reset_hw(&adapter->hw);
1043         wr32(E1000_WUC, 0);
1044
1045         if (adapter->hw.mac.ops.init_hw(&adapter->hw))
1046                 dev_err(&adapter->pdev->dev, "Hardware Error\n");
1047
1048         igb_update_mng_vlan(adapter);
1049
1050         /* Enable h/w to recognize an 802.1Q VLAN Ethernet packet */
1051         wr32(E1000_VET, ETHERNET_IEEE_VLAN_TYPE);
1052
1053         igb_reset_adaptive(&adapter->hw);
1054         igb_get_phy_info(&adapter->hw);
1055 }
1056
1057 static const struct net_device_ops igb_netdev_ops = {
1058         .ndo_open               = igb_open,
1059         .ndo_stop               = igb_close,
1060         .ndo_start_xmit         = igb_xmit_frame_adv,
1061         .ndo_get_stats          = igb_get_stats,
1062         .ndo_set_multicast_list = igb_set_multi,
1063         .ndo_set_mac_address    = igb_set_mac,
1064         .ndo_change_mtu         = igb_change_mtu,
1065         .ndo_do_ioctl           = igb_ioctl,
1066         .ndo_tx_timeout         = igb_tx_timeout,
1067         .ndo_validate_addr      = eth_validate_addr,
1068         .ndo_vlan_rx_register   = igb_vlan_rx_register,
1069         .ndo_vlan_rx_add_vid    = igb_vlan_rx_add_vid,
1070         .ndo_vlan_rx_kill_vid   = igb_vlan_rx_kill_vid,
1071 #ifdef CONFIG_NET_POLL_CONTROLLER
1072         .ndo_poll_controller    = igb_netpoll,
1073 #endif
1074 };
1075
1076 /**
1077  * igb_probe - Device Initialization Routine
1078  * @pdev: PCI device information struct
1079  * @ent: entry in igb_pci_tbl
1080  *
1081  * Returns 0 on success, negative on failure
1082  *
1083  * igb_probe initializes an adapter identified by a pci_dev structure.
1084  * The OS initialization, configuring of the adapter private structure,
1085  * and a hardware reset occur.
1086  **/
1087 static int __devinit igb_probe(struct pci_dev *pdev,
1088                                const struct pci_device_id *ent)
1089 {
1090         struct net_device *netdev;
1091         struct igb_adapter *adapter;
1092         struct e1000_hw *hw;
1093         struct pci_dev *us_dev;
1094         const struct e1000_info *ei = igb_info_tbl[ent->driver_data];
1095         unsigned long mmio_start, mmio_len;
1096         int err, pci_using_dac, pos;
1097         u16 eeprom_data = 0, state = 0;
1098         u16 eeprom_apme_mask = IGB_EEPROM_APME;
1099         u32 part_num;
1100
1101         err = pci_enable_device_mem(pdev);
1102         if (err)
1103                 return err;
1104
1105         pci_using_dac = 0;
1106         err = pci_set_dma_mask(pdev, DMA_64BIT_MASK);
1107         if (!err) {
1108                 err = pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK);
1109                 if (!err)
1110                         pci_using_dac = 1;
1111         } else {
1112                 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
1113                 if (err) {
1114                         err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
1115                         if (err) {
1116                                 dev_err(&pdev->dev, "No usable DMA "
1117                                         "configuration, aborting\n");
1118                                 goto err_dma;
1119                         }
1120                 }
1121         }
1122
1123         /* 82575 requires that the pci-e link partner disable the L0s state */
1124         switch (pdev->device) {
1125         case E1000_DEV_ID_82575EB_COPPER:
1126         case E1000_DEV_ID_82575EB_FIBER_SERDES:
1127         case E1000_DEV_ID_82575GB_QUAD_COPPER:
1128                 us_dev = pdev->bus->self;
1129                 pos = pci_find_capability(us_dev, PCI_CAP_ID_EXP);
1130                 if (pos) {
1131                         pci_read_config_word(us_dev, pos + PCI_EXP_LNKCTL,
1132                                              &state);
1133                         state &= ~PCIE_LINK_STATE_L0S;
1134                         pci_write_config_word(us_dev, pos + PCI_EXP_LNKCTL,
1135                                               state);
1136                         dev_info(&pdev->dev,
1137                                  "Disabling ASPM L0s upstream switch port %s\n",
1138                                  pci_name(us_dev));
1139                 }
1140         default:
1141                 break;
1142         }
1143
1144         err = pci_request_selected_regions(pdev, pci_select_bars(pdev,
1145                                            IORESOURCE_MEM),
1146                                            igb_driver_name);
1147         if (err)
1148                 goto err_pci_reg;
1149
1150         err = pci_enable_pcie_error_reporting(pdev);
1151         if (err) {
1152                 dev_err(&pdev->dev, "pci_enable_pcie_error_reporting failed "
1153                         "0x%x\n", err);
1154                 /* non-fatal, continue */
1155         }
1156
1157         pci_set_master(pdev);
1158         pci_save_state(pdev);
1159
1160         err = -ENOMEM;
1161         netdev = alloc_etherdev_mq(sizeof(struct igb_adapter), IGB_MAX_TX_QUEUES);
1162         if (!netdev)
1163                 goto err_alloc_etherdev;
1164
1165         SET_NETDEV_DEV(netdev, &pdev->dev);
1166
1167         pci_set_drvdata(pdev, netdev);
1168         adapter = netdev_priv(netdev);
1169         adapter->netdev = netdev;
1170         adapter->pdev = pdev;
1171         hw = &adapter->hw;
1172         hw->back = adapter;
1173         adapter->msg_enable = NETIF_MSG_DRV | NETIF_MSG_PROBE;
1174
1175         mmio_start = pci_resource_start(pdev, 0);
1176         mmio_len = pci_resource_len(pdev, 0);
1177
1178         err = -EIO;
1179         hw->hw_addr = ioremap(mmio_start, mmio_len);
1180         if (!hw->hw_addr)
1181                 goto err_ioremap;
1182
1183         netdev->netdev_ops = &igb_netdev_ops;
1184         igb_set_ethtool_ops(netdev);
1185         netdev->watchdog_timeo = 5 * HZ;
1186
1187         strncpy(netdev->name, pci_name(pdev), sizeof(netdev->name) - 1);
1188
1189         netdev->mem_start = mmio_start;
1190         netdev->mem_end = mmio_start + mmio_len;
1191
1192         /* PCI config space info */
1193         hw->vendor_id = pdev->vendor;
1194         hw->device_id = pdev->device;
1195         hw->revision_id = pdev->revision;
1196         hw->subsystem_vendor_id = pdev->subsystem_vendor;
1197         hw->subsystem_device_id = pdev->subsystem_device;
1198
1199         /* setup the private structure */
1200         hw->back = adapter;
1201         /* Copy the default MAC, PHY and NVM function pointers */
1202         memcpy(&hw->mac.ops, ei->mac_ops, sizeof(hw->mac.ops));
1203         memcpy(&hw->phy.ops, ei->phy_ops, sizeof(hw->phy.ops));
1204         memcpy(&hw->nvm.ops, ei->nvm_ops, sizeof(hw->nvm.ops));
1205         /* Initialize skew-specific constants */
1206         err = ei->get_invariants(hw);
1207         if (err)
1208                 goto err_sw_init;
1209
1210         /* setup the private structure */
1211         err = igb_sw_init(adapter);
1212         if (err)
1213                 goto err_sw_init;
1214
1215         igb_get_bus_info_pcie(hw);
1216
1217         /* set flags */
1218         switch (hw->mac.type) {
1219         case e1000_82575:
1220                 adapter->flags |= IGB_FLAG_NEED_CTX_IDX;
1221                 break;
1222         case e1000_82576:
1223         default:
1224                 break;
1225         }
1226
1227         hw->phy.autoneg_wait_to_complete = false;
1228         hw->mac.adaptive_ifs = true;
1229
1230         /* Copper options */
1231         if (hw->phy.media_type == e1000_media_type_copper) {
1232                 hw->phy.mdix = AUTO_ALL_MODES;
1233                 hw->phy.disable_polarity_correction = false;
1234                 hw->phy.ms_type = e1000_ms_hw_default;
1235         }
1236
1237         if (igb_check_reset_block(hw))
1238                 dev_info(&pdev->dev,
1239                         "PHY reset is blocked due to SOL/IDER session.\n");
1240
1241         netdev->features = NETIF_F_SG |
1242                            NETIF_F_IP_CSUM |
1243                            NETIF_F_HW_VLAN_TX |
1244                            NETIF_F_HW_VLAN_RX |
1245                            NETIF_F_HW_VLAN_FILTER;
1246
1247         netdev->features |= NETIF_F_IPV6_CSUM;
1248         netdev->features |= NETIF_F_TSO;
1249         netdev->features |= NETIF_F_TSO6;
1250
1251 #ifdef CONFIG_IGB_LRO
1252         netdev->features |= NETIF_F_GRO;
1253 #endif
1254
1255         netdev->vlan_features |= NETIF_F_TSO;
1256         netdev->vlan_features |= NETIF_F_TSO6;
1257         netdev->vlan_features |= NETIF_F_IP_CSUM;
1258         netdev->vlan_features |= NETIF_F_SG;
1259
1260         if (pci_using_dac)
1261                 netdev->features |= NETIF_F_HIGHDMA;
1262
1263         adapter->en_mng_pt = igb_enable_mng_pass_thru(&adapter->hw);
1264
1265         /* before reading the NVM, reset the controller to put the device in a
1266          * known good starting state */
1267         hw->mac.ops.reset_hw(hw);
1268
1269         /* make sure the NVM is good */
1270         if (igb_validate_nvm_checksum(hw) < 0) {
1271                 dev_err(&pdev->dev, "The NVM Checksum Is Not Valid\n");
1272                 err = -EIO;
1273                 goto err_eeprom;
1274         }
1275
1276         /* copy the MAC address out of the NVM */
1277         if (hw->mac.ops.read_mac_addr(hw))
1278                 dev_err(&pdev->dev, "NVM Read Error\n");
1279
1280         memcpy(netdev->dev_addr, hw->mac.addr, netdev->addr_len);
1281         memcpy(netdev->perm_addr, hw->mac.addr, netdev->addr_len);
1282
1283         if (!is_valid_ether_addr(netdev->perm_addr)) {
1284                 dev_err(&pdev->dev, "Invalid MAC Address\n");
1285                 err = -EIO;
1286                 goto err_eeprom;
1287         }
1288
1289         init_timer(&adapter->watchdog_timer);
1290         adapter->watchdog_timer.function = &igb_watchdog;
1291         adapter->watchdog_timer.data = (unsigned long) adapter;
1292
1293         init_timer(&adapter->phy_info_timer);
1294         adapter->phy_info_timer.function = &igb_update_phy_info;
1295         adapter->phy_info_timer.data = (unsigned long) adapter;
1296
1297         INIT_WORK(&adapter->reset_task, igb_reset_task);
1298         INIT_WORK(&adapter->watchdog_task, igb_watchdog_task);
1299
1300         /* Initialize link properties that are user-changeable */
1301         adapter->fc_autoneg = true;
1302         hw->mac.autoneg = true;
1303         hw->phy.autoneg_advertised = 0x2f;
1304
1305         hw->fc.original_type = e1000_fc_default;
1306         hw->fc.type = e1000_fc_default;
1307
1308         adapter->itr_setting = 3;
1309         adapter->itr = IGB_START_ITR;
1310
1311         igb_validate_mdi_setting(hw);
1312
1313         adapter->rx_csum = 1;
1314
1315         /* Initial Wake on LAN setting If APM wake is enabled in the EEPROM,
1316          * enable the ACPI Magic Packet filter
1317          */
1318
1319         if (hw->bus.func == 0 ||
1320             hw->device_id == E1000_DEV_ID_82575EB_COPPER)
1321                 hw->nvm.ops.read(hw, NVM_INIT_CONTROL3_PORT_A, 1, &eeprom_data);
1322
1323         if (eeprom_data & eeprom_apme_mask)
1324                 adapter->eeprom_wol |= E1000_WUFC_MAG;
1325
1326         /* now that we have the eeprom settings, apply the special cases where
1327          * the eeprom may be wrong or the board simply won't support wake on
1328          * lan on a particular port */
1329         switch (pdev->device) {
1330         case E1000_DEV_ID_82575GB_QUAD_COPPER:
1331                 adapter->eeprom_wol = 0;
1332                 break;
1333         case E1000_DEV_ID_82575EB_FIBER_SERDES:
1334         case E1000_DEV_ID_82576_FIBER:
1335         case E1000_DEV_ID_82576_SERDES:
1336                 /* Wake events only supported on port A for dual fiber
1337                  * regardless of eeprom setting */
1338                 if (rd32(E1000_STATUS) & E1000_STATUS_FUNC_1)
1339                         adapter->eeprom_wol = 0;
1340                 break;
1341         }
1342
1343         /* initialize the wol settings based on the eeprom settings */
1344         adapter->wol = adapter->eeprom_wol;
1345         device_set_wakeup_enable(&adapter->pdev->dev, adapter->wol);
1346
1347         /* reset the hardware with the new settings */
1348         igb_reset(adapter);
1349
1350         /* let the f/w know that the h/w is now under the control of the
1351          * driver. */
1352         igb_get_hw_control(adapter);
1353
1354         /* tell the stack to leave us alone until igb_open() is called */
1355         netif_carrier_off(netdev);
1356         netif_tx_stop_all_queues(netdev);
1357
1358         strcpy(netdev->name, "eth%d");
1359         err = register_netdev(netdev);
1360         if (err)
1361                 goto err_register;
1362
1363 #ifdef CONFIG_IGB_DCA
1364         if (dca_add_requester(&pdev->dev) == 0) {
1365                 adapter->flags |= IGB_FLAG_DCA_ENABLED;
1366                 dev_info(&pdev->dev, "DCA enabled\n");
1367                 /* Always use CB2 mode, difference is masked
1368                  * in the CB driver. */
1369                 wr32(E1000_DCA_CTRL, 2);
1370                 igb_setup_dca(adapter);
1371         }
1372 #endif
1373
1374         /*
1375          * Initialize hardware timer: we keep it running just in case
1376          * that some program needs it later on.
1377          */
1378         memset(&adapter->cycles, 0, sizeof(adapter->cycles));
1379         adapter->cycles.read = igb_read_clock;
1380         adapter->cycles.mask = CLOCKSOURCE_MASK(64);
1381         adapter->cycles.mult = 1;
1382         adapter->cycles.shift = IGB_TSYNC_SHIFT;
1383         wr32(E1000_TIMINCA,
1384              (1<<24) |
1385              IGB_TSYNC_CYCLE_TIME_IN_NANOSECONDS * IGB_TSYNC_SCALE);
1386 #if 0
1387         /*
1388          * Avoid rollover while we initialize by resetting the time counter.
1389          */
1390         wr32(E1000_SYSTIML, 0x00000000);
1391         wr32(E1000_SYSTIMH, 0x00000000);
1392 #else
1393         /*
1394          * Set registers so that rollover occurs soon to test this.
1395          */
1396         wr32(E1000_SYSTIML, 0x00000000);
1397         wr32(E1000_SYSTIMH, 0xFF800000);
1398 #endif
1399         wrfl();
1400         timecounter_init(&adapter->clock,
1401                          &adapter->cycles,
1402                          ktime_to_ns(ktime_get_real()));
1403
1404         /*
1405          * Synchronize our NIC clock against system wall clock. NIC
1406          * time stamp reading requires ~3us per sample, each sample
1407          * was pretty stable even under load => only require 10
1408          * samples for each offset comparison.
1409          */
1410         memset(&adapter->compare, 0, sizeof(adapter->compare));
1411         adapter->compare.source = &adapter->clock;
1412         adapter->compare.target = ktime_get_real;
1413         adapter->compare.num_samples = 10;
1414         timecompare_update(&adapter->compare, 0);
1415
1416 #ifdef DEBUG
1417         {
1418                 char buffer[160];
1419                 printk(KERN_DEBUG
1420                         "igb: %s: hw %p initialized timer\n",
1421                         igb_get_time_str(adapter, buffer),
1422                         &adapter->hw);
1423         }
1424 #endif
1425
1426         dev_info(&pdev->dev, "Intel(R) Gigabit Ethernet Network Connection\n");
1427         /* print bus type/speed/width info */
1428         dev_info(&pdev->dev, "%s: (PCIe:%s:%s) %pM\n",
1429                  netdev->name,
1430                  ((hw->bus.speed == e1000_bus_speed_2500)
1431                   ? "2.5Gb/s" : "unknown"),
1432                  ((hw->bus.width == e1000_bus_width_pcie_x4)
1433                   ? "Width x4" : (hw->bus.width == e1000_bus_width_pcie_x1)
1434                   ? "Width x1" : "unknown"),
1435                  netdev->dev_addr);
1436
1437         igb_read_part_num(hw, &part_num);
1438         dev_info(&pdev->dev, "%s: PBA No: %06x-%03x\n", netdev->name,
1439                 (part_num >> 8), (part_num & 0xff));
1440
1441         dev_info(&pdev->dev,
1442                 "Using %s interrupts. %d rx queue(s), %d tx queue(s)\n",
1443                 adapter->msix_entries ? "MSI-X" :
1444                 (adapter->flags & IGB_FLAG_HAS_MSI) ? "MSI" : "legacy",
1445                 adapter->num_rx_queues, adapter->num_tx_queues);
1446
1447         return 0;
1448
1449 err_register:
1450         igb_release_hw_control(adapter);
1451 err_eeprom:
1452         if (!igb_check_reset_block(hw))
1453                 igb_reset_phy(hw);
1454
1455         if (hw->flash_address)
1456                 iounmap(hw->flash_address);
1457
1458         igb_free_queues(adapter);
1459 err_sw_init:
1460         iounmap(hw->hw_addr);
1461 err_ioremap:
1462         free_netdev(netdev);
1463 err_alloc_etherdev:
1464         pci_release_selected_regions(pdev, pci_select_bars(pdev,
1465                                      IORESOURCE_MEM));
1466 err_pci_reg:
1467 err_dma:
1468         pci_disable_device(pdev);
1469         return err;
1470 }
1471
1472 /**
1473  * igb_remove - Device Removal Routine
1474  * @pdev: PCI device information struct
1475  *
1476  * igb_remove is called by the PCI subsystem to alert the driver
1477  * that it should release a PCI device.  The could be caused by a
1478  * Hot-Plug event, or because the driver is going to be removed from
1479  * memory.
1480  **/
1481 static void __devexit igb_remove(struct pci_dev *pdev)
1482 {
1483         struct net_device *netdev = pci_get_drvdata(pdev);
1484         struct igb_adapter *adapter = netdev_priv(netdev);
1485         struct e1000_hw *hw = &adapter->hw;
1486         int err;
1487
1488         /* flush_scheduled work may reschedule our watchdog task, so
1489          * explicitly disable watchdog tasks from being rescheduled  */
1490         set_bit(__IGB_DOWN, &adapter->state);
1491         del_timer_sync(&adapter->watchdog_timer);
1492         del_timer_sync(&adapter->phy_info_timer);
1493
1494         flush_scheduled_work();
1495
1496 #ifdef CONFIG_IGB_DCA
1497         if (adapter->flags & IGB_FLAG_DCA_ENABLED) {
1498                 dev_info(&pdev->dev, "DCA disabled\n");
1499                 dca_remove_requester(&pdev->dev);
1500                 adapter->flags &= ~IGB_FLAG_DCA_ENABLED;
1501                 wr32(E1000_DCA_CTRL, 1);
1502         }
1503 #endif
1504
1505         /* Release control of h/w to f/w.  If f/w is AMT enabled, this
1506          * would have already happened in close and is redundant. */
1507         igb_release_hw_control(adapter);
1508
1509         unregister_netdev(netdev);
1510
1511         if (!igb_check_reset_block(&adapter->hw))
1512                 igb_reset_phy(&adapter->hw);
1513
1514         igb_reset_interrupt_capability(adapter);
1515
1516         igb_free_queues(adapter);
1517
1518         iounmap(hw->hw_addr);
1519         if (hw->flash_address)
1520                 iounmap(hw->flash_address);
1521         pci_release_selected_regions(pdev, pci_select_bars(pdev,
1522                                      IORESOURCE_MEM));
1523
1524         free_netdev(netdev);
1525
1526         err = pci_disable_pcie_error_reporting(pdev);
1527         if (err)
1528                 dev_err(&pdev->dev,
1529                         "pci_disable_pcie_error_reporting failed 0x%x\n", err);
1530
1531         pci_disable_device(pdev);
1532 }
1533
1534 /**
1535  * igb_sw_init - Initialize general software structures (struct igb_adapter)
1536  * @adapter: board private structure to initialize
1537  *
1538  * igb_sw_init initializes the Adapter private data structure.
1539  * Fields are initialized based on PCI device information and
1540  * OS network device settings (MTU size).
1541  **/
1542 static int __devinit igb_sw_init(struct igb_adapter *adapter)
1543 {
1544         struct e1000_hw *hw = &adapter->hw;
1545         struct net_device *netdev = adapter->netdev;
1546         struct pci_dev *pdev = adapter->pdev;
1547
1548         pci_read_config_word(pdev, PCI_COMMAND, &hw->bus.pci_cmd_word);
1549
1550         adapter->tx_ring_count = IGB_DEFAULT_TXD;
1551         adapter->rx_ring_count = IGB_DEFAULT_RXD;
1552         adapter->rx_buffer_len = MAXIMUM_ETHERNET_VLAN_SIZE;
1553         adapter->rx_ps_hdr_size = 0; /* disable packet split */
1554         adapter->max_frame_size = netdev->mtu + ETH_HLEN + ETH_FCS_LEN;
1555         adapter->min_frame_size = ETH_ZLEN + ETH_FCS_LEN;
1556
1557         /* This call may decrease the number of queues depending on
1558          * interrupt mode. */
1559         igb_set_interrupt_capability(adapter);
1560
1561         if (igb_alloc_queues(adapter)) {
1562                 dev_err(&pdev->dev, "Unable to allocate memory for queues\n");
1563                 return -ENOMEM;
1564         }
1565
1566         /* Explicitly disable IRQ since the NIC can be in any state. */
1567         igb_irq_disable(adapter);
1568
1569         set_bit(__IGB_DOWN, &adapter->state);
1570         return 0;
1571 }
1572
1573 /**
1574  * igb_open - Called when a network interface is made active
1575  * @netdev: network interface device structure
1576  *
1577  * Returns 0 on success, negative value on failure
1578  *
1579  * The open entry point is called when a network interface is made
1580  * active by the system (IFF_UP).  At this point all resources needed
1581  * for transmit and receive operations are allocated, the interrupt
1582  * handler is registered with the OS, the watchdog timer is started,
1583  * and the stack is notified that the interface is ready.
1584  **/
1585 static int igb_open(struct net_device *netdev)
1586 {
1587         struct igb_adapter *adapter = netdev_priv(netdev);
1588         struct e1000_hw *hw = &adapter->hw;
1589         int err;
1590         int i;
1591
1592         /* disallow open during test */
1593         if (test_bit(__IGB_TESTING, &adapter->state))
1594                 return -EBUSY;
1595
1596         /* allocate transmit descriptors */
1597         err = igb_setup_all_tx_resources(adapter);
1598         if (err)
1599                 goto err_setup_tx;
1600
1601         /* allocate receive descriptors */
1602         err = igb_setup_all_rx_resources(adapter);
1603         if (err)
1604                 goto err_setup_rx;
1605
1606         /* e1000_power_up_phy(adapter); */
1607
1608         adapter->mng_vlan_id = IGB_MNG_VLAN_NONE;
1609         if ((adapter->hw.mng_cookie.status &
1610              E1000_MNG_DHCP_COOKIE_STATUS_VLAN))
1611                 igb_update_mng_vlan(adapter);
1612
1613         /* before we allocate an interrupt, we must be ready to handle it.
1614          * Setting DEBUG_SHIRQ in the kernel makes it fire an interrupt
1615          * as soon as we call pci_request_irq, so we have to setup our
1616          * clean_rx handler before we do so.  */
1617         igb_configure(adapter);
1618
1619         err = igb_request_irq(adapter);
1620         if (err)
1621                 goto err_req_irq;
1622
1623         /* From here on the code is the same as igb_up() */
1624         clear_bit(__IGB_DOWN, &adapter->state);
1625
1626         for (i = 0; i < adapter->num_rx_queues; i++)
1627                 napi_enable(&adapter->rx_ring[i].napi);
1628
1629         /* Clear any pending interrupts. */
1630         rd32(E1000_ICR);
1631
1632         igb_irq_enable(adapter);
1633
1634         netif_tx_start_all_queues(netdev);
1635
1636         /* Fire a link status change interrupt to start the watchdog. */
1637         wr32(E1000_ICS, E1000_ICS_LSC);
1638
1639         return 0;
1640
1641 err_req_irq:
1642         igb_release_hw_control(adapter);
1643         /* e1000_power_down_phy(adapter); */
1644         igb_free_all_rx_resources(adapter);
1645 err_setup_rx:
1646         igb_free_all_tx_resources(adapter);
1647 err_setup_tx:
1648         igb_reset(adapter);
1649
1650         return err;
1651 }
1652
1653 /**
1654  * igb_close - Disables a network interface
1655  * @netdev: network interface device structure
1656  *
1657  * Returns 0, this is not allowed to fail
1658  *
1659  * The close entry point is called when an interface is de-activated
1660  * by the OS.  The hardware is still under the driver's control, but
1661  * needs to be disabled.  A global MAC reset is issued to stop the
1662  * hardware, and all transmit and receive resources are freed.
1663  **/
1664 static int igb_close(struct net_device *netdev)
1665 {
1666         struct igb_adapter *adapter = netdev_priv(netdev);
1667
1668         WARN_ON(test_bit(__IGB_RESETTING, &adapter->state));
1669         igb_down(adapter);
1670
1671         igb_free_irq(adapter);
1672
1673         igb_free_all_tx_resources(adapter);
1674         igb_free_all_rx_resources(adapter);
1675
1676         /* kill manageability vlan ID if supported, but not if a vlan with
1677          * the same ID is registered on the host OS (let 8021q kill it) */
1678         if ((adapter->hw.mng_cookie.status &
1679                           E1000_MNG_DHCP_COOKIE_STATUS_VLAN) &&
1680              !(adapter->vlgrp &&
1681                vlan_group_get_device(adapter->vlgrp, adapter->mng_vlan_id)))
1682                 igb_vlan_rx_kill_vid(netdev, adapter->mng_vlan_id);
1683
1684         return 0;
1685 }
1686
1687 /**
1688  * igb_setup_tx_resources - allocate Tx resources (Descriptors)
1689  * @adapter: board private structure
1690  * @tx_ring: tx descriptor ring (for a specific queue) to setup
1691  *
1692  * Return 0 on success, negative on failure
1693  **/
1694
1695 int igb_setup_tx_resources(struct igb_adapter *adapter,
1696                            struct igb_ring *tx_ring)
1697 {
1698         struct pci_dev *pdev = adapter->pdev;
1699         int size;
1700
1701         size = sizeof(struct igb_buffer) * tx_ring->count;
1702         tx_ring->buffer_info = vmalloc(size);
1703         if (!tx_ring->buffer_info)
1704                 goto err;
1705         memset(tx_ring->buffer_info, 0, size);
1706
1707         /* round up to nearest 4K */
1708         tx_ring->size = tx_ring->count * sizeof(struct e1000_tx_desc);
1709         tx_ring->size = ALIGN(tx_ring->size, 4096);
1710
1711         tx_ring->desc = pci_alloc_consistent(pdev, tx_ring->size,
1712                                              &tx_ring->dma);
1713
1714         if (!tx_ring->desc)
1715                 goto err;
1716
1717         tx_ring->adapter = adapter;
1718         tx_ring->next_to_use = 0;
1719         tx_ring->next_to_clean = 0;
1720         return 0;
1721
1722 err:
1723         vfree(tx_ring->buffer_info);
1724         dev_err(&adapter->pdev->dev,
1725                 "Unable to allocate memory for the transmit descriptor ring\n");
1726         return -ENOMEM;
1727 }
1728
1729 /**
1730  * igb_setup_all_tx_resources - wrapper to allocate Tx resources
1731  *                                (Descriptors) for all queues
1732  * @adapter: board private structure
1733  *
1734  * Return 0 on success, negative on failure
1735  **/
1736 static int igb_setup_all_tx_resources(struct igb_adapter *adapter)
1737 {
1738         int i, err = 0;
1739         int r_idx;
1740
1741         for (i = 0; i < adapter->num_tx_queues; i++) {
1742                 err = igb_setup_tx_resources(adapter, &adapter->tx_ring[i]);
1743                 if (err) {
1744                         dev_err(&adapter->pdev->dev,
1745                                 "Allocation for Tx Queue %u failed\n", i);
1746                         for (i--; i >= 0; i--)
1747                                 igb_free_tx_resources(&adapter->tx_ring[i]);
1748                         break;
1749                 }
1750         }
1751
1752         for (i = 0; i < IGB_MAX_TX_QUEUES; i++) {
1753                 r_idx = i % adapter->num_tx_queues;
1754                 adapter->multi_tx_table[i] = &adapter->tx_ring[r_idx];
1755         }
1756         return err;
1757 }
1758
1759 /**
1760  * igb_configure_tx - Configure transmit Unit after Reset
1761  * @adapter: board private structure
1762  *
1763  * Configure the Tx unit of the MAC after a reset.
1764  **/
1765 static void igb_configure_tx(struct igb_adapter *adapter)
1766 {
1767         u64 tdba;
1768         struct e1000_hw *hw = &adapter->hw;
1769         u32 tctl;
1770         u32 txdctl, txctrl;
1771         int i, j;
1772
1773         for (i = 0; i < adapter->num_tx_queues; i++) {
1774                 struct igb_ring *ring = &(adapter->tx_ring[i]);
1775                 j = ring->reg_idx;
1776                 wr32(E1000_TDLEN(j),
1777                                 ring->count * sizeof(struct e1000_tx_desc));
1778                 tdba = ring->dma;
1779                 wr32(E1000_TDBAL(j),
1780                                 tdba & 0x00000000ffffffffULL);
1781                 wr32(E1000_TDBAH(j), tdba >> 32);
1782
1783                 ring->head = E1000_TDH(j);
1784                 ring->tail = E1000_TDT(j);
1785                 writel(0, hw->hw_addr + ring->tail);
1786                 writel(0, hw->hw_addr + ring->head);
1787                 txdctl = rd32(E1000_TXDCTL(j));
1788                 txdctl |= E1000_TXDCTL_QUEUE_ENABLE;
1789                 wr32(E1000_TXDCTL(j), txdctl);
1790
1791                 /* Turn off Relaxed Ordering on head write-backs.  The
1792                  * writebacks MUST be delivered in order or it will
1793                  * completely screw up our bookeeping.
1794                  */
1795                 txctrl = rd32(E1000_DCA_TXCTRL(j));
1796                 txctrl &= ~E1000_DCA_TXCTRL_TX_WB_RO_EN;
1797                 wr32(E1000_DCA_TXCTRL(j), txctrl);
1798         }
1799
1800
1801
1802         /* Use the default values for the Tx Inter Packet Gap (IPG) timer */
1803
1804         /* Program the Transmit Control Register */
1805
1806         tctl = rd32(E1000_TCTL);
1807         tctl &= ~E1000_TCTL_CT;
1808         tctl |= E1000_TCTL_PSP | E1000_TCTL_RTLC |
1809                 (E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT);
1810
1811         igb_config_collision_dist(hw);
1812
1813         /* Setup Transmit Descriptor Settings for eop descriptor */
1814         adapter->txd_cmd = E1000_TXD_CMD_EOP | E1000_TXD_CMD_RS;
1815
1816         /* Enable transmits */
1817         tctl |= E1000_TCTL_EN;
1818
1819         wr32(E1000_TCTL, tctl);
1820 }
1821
1822 /**
1823  * igb_setup_rx_resources - allocate Rx resources (Descriptors)
1824  * @adapter: board private structure
1825  * @rx_ring:    rx descriptor ring (for a specific queue) to setup
1826  *
1827  * Returns 0 on success, negative on failure
1828  **/
1829
1830 int igb_setup_rx_resources(struct igb_adapter *adapter,
1831                            struct igb_ring *rx_ring)
1832 {
1833         struct pci_dev *pdev = adapter->pdev;
1834         int size, desc_len;
1835
1836         size = sizeof(struct igb_buffer) * rx_ring->count;
1837         rx_ring->buffer_info = vmalloc(size);
1838         if (!rx_ring->buffer_info)
1839                 goto err;
1840         memset(rx_ring->buffer_info, 0, size);
1841
1842         desc_len = sizeof(union e1000_adv_rx_desc);
1843
1844         /* Round up to nearest 4K */
1845         rx_ring->size = rx_ring->count * desc_len;
1846         rx_ring->size = ALIGN(rx_ring->size, 4096);
1847
1848         rx_ring->desc = pci_alloc_consistent(pdev, rx_ring->size,
1849                                              &rx_ring->dma);
1850
1851         if (!rx_ring->desc)
1852                 goto err;
1853
1854         rx_ring->next_to_clean = 0;
1855         rx_ring->next_to_use = 0;
1856
1857         rx_ring->adapter = adapter;
1858
1859         return 0;
1860
1861 err:
1862         vfree(rx_ring->buffer_info);
1863         dev_err(&adapter->pdev->dev, "Unable to allocate memory for "
1864                 "the receive descriptor ring\n");
1865         return -ENOMEM;
1866 }
1867
1868 /**
1869  * igb_setup_all_rx_resources - wrapper to allocate Rx resources
1870  *                                (Descriptors) for all queues
1871  * @adapter: board private structure
1872  *
1873  * Return 0 on success, negative on failure
1874  **/
1875 static int igb_setup_all_rx_resources(struct igb_adapter *adapter)
1876 {
1877         int i, err = 0;
1878
1879         for (i = 0; i < adapter->num_rx_queues; i++) {
1880                 err = igb_setup_rx_resources(adapter, &adapter->rx_ring[i]);
1881                 if (err) {
1882                         dev_err(&adapter->pdev->dev,
1883                                 "Allocation for Rx Queue %u failed\n", i);
1884                         for (i--; i >= 0; i--)
1885                                 igb_free_rx_resources(&adapter->rx_ring[i]);
1886                         break;
1887                 }
1888         }
1889
1890         return err;
1891 }
1892
1893 /**
1894  * igb_setup_rctl - configure the receive control registers
1895  * @adapter: Board private structure
1896  **/
1897 static void igb_setup_rctl(struct igb_adapter *adapter)
1898 {
1899         struct e1000_hw *hw = &adapter->hw;
1900         u32 rctl;
1901         u32 srrctl = 0;
1902         int i, j;
1903
1904         rctl = rd32(E1000_RCTL);
1905
1906         rctl &= ~(3 << E1000_RCTL_MO_SHIFT);
1907         rctl &= ~(E1000_RCTL_LBM_TCVR | E1000_RCTL_LBM_MAC);
1908
1909         rctl |= E1000_RCTL_EN | E1000_RCTL_BAM | E1000_RCTL_RDMTS_HALF |
1910                 (hw->mac.mc_filter_type << E1000_RCTL_MO_SHIFT);
1911
1912         /*
1913          * enable stripping of CRC. It's unlikely this will break BMC
1914          * redirection as it did with e1000. Newer features require
1915          * that the HW strips the CRC.
1916         */
1917         rctl |= E1000_RCTL_SECRC;
1918
1919         /*
1920          * disable store bad packets and clear size bits.
1921          */
1922         rctl &= ~(E1000_RCTL_SBP | E1000_RCTL_SZ_256);
1923
1924         /* enable LPE when to prevent packets larger than max_frame_size */
1925                 rctl |= E1000_RCTL_LPE;
1926
1927         /* Setup buffer sizes */
1928         switch (adapter->rx_buffer_len) {
1929         case IGB_RXBUFFER_256:
1930                 rctl |= E1000_RCTL_SZ_256;
1931                 break;
1932         case IGB_RXBUFFER_512:
1933                 rctl |= E1000_RCTL_SZ_512;
1934                 break;
1935         default:
1936                 srrctl = ALIGN(adapter->rx_buffer_len, 1024)
1937                          >> E1000_SRRCTL_BSIZEPKT_SHIFT;
1938                 break;
1939         }
1940
1941         /* 82575 and greater support packet-split where the protocol
1942          * header is placed in skb->data and the packet data is
1943          * placed in pages hanging off of skb_shinfo(skb)->nr_frags.
1944          * In the case of a non-split, skb->data is linearly filled,
1945          * followed by the page buffers.  Therefore, skb->data is
1946          * sized to hold the largest protocol header.
1947          */
1948         /* allocations using alloc_page take too long for regular MTU
1949          * so only enable packet split for jumbo frames */
1950         if (adapter->netdev->mtu > ETH_DATA_LEN) {
1951                 adapter->rx_ps_hdr_size = IGB_RXBUFFER_128;
1952                 srrctl |= adapter->rx_ps_hdr_size <<
1953                          E1000_SRRCTL_BSIZEHDRSIZE_SHIFT;
1954                 srrctl |= E1000_SRRCTL_DESCTYPE_HDR_SPLIT_ALWAYS;
1955         } else {
1956                 adapter->rx_ps_hdr_size = 0;
1957                 srrctl |= E1000_SRRCTL_DESCTYPE_ADV_ONEBUF;
1958         }
1959
1960         for (i = 0; i < adapter->num_rx_queues; i++) {
1961                 j = adapter->rx_ring[i].reg_idx;
1962                 wr32(E1000_SRRCTL(j), srrctl);
1963         }
1964
1965         wr32(E1000_RCTL, rctl);
1966 }
1967
1968 /**
1969  * igb_configure_rx - Configure receive Unit after Reset
1970  * @adapter: board private structure
1971  *
1972  * Configure the Rx unit of the MAC after a reset.
1973  **/
1974 static void igb_configure_rx(struct igb_adapter *adapter)
1975 {
1976         u64 rdba;
1977         struct e1000_hw *hw = &adapter->hw;
1978         u32 rctl, rxcsum;
1979         u32 rxdctl;
1980         int i, j;
1981
1982         /* disable receives while setting up the descriptors */
1983         rctl = rd32(E1000_RCTL);
1984         wr32(E1000_RCTL, rctl & ~E1000_RCTL_EN);
1985         wrfl();
1986         mdelay(10);
1987
1988         if (adapter->itr_setting > 3)
1989                 wr32(E1000_ITR, adapter->itr);
1990
1991         /* Setup the HW Rx Head and Tail Descriptor Pointers and
1992          * the Base and Length of the Rx Descriptor Ring */
1993         for (i = 0; i < adapter->num_rx_queues; i++) {
1994                 struct igb_ring *ring = &(adapter->rx_ring[i]);
1995                 j = ring->reg_idx;
1996                 rdba = ring->dma;
1997                 wr32(E1000_RDBAL(j),
1998                                 rdba & 0x00000000ffffffffULL);
1999                 wr32(E1000_RDBAH(j), rdba >> 32);
2000                 wr32(E1000_RDLEN(j),
2001                                ring->count * sizeof(union e1000_adv_rx_desc));
2002
2003                 ring->head = E1000_RDH(j);
2004                 ring->tail = E1000_RDT(j);
2005                 writel(0, hw->hw_addr + ring->tail);
2006                 writel(0, hw->hw_addr + ring->head);
2007
2008                 rxdctl = rd32(E1000_RXDCTL(j));
2009                 rxdctl |= E1000_RXDCTL_QUEUE_ENABLE;
2010                 rxdctl &= 0xFFF00000;
2011                 rxdctl |= IGB_RX_PTHRESH;
2012                 rxdctl |= IGB_RX_HTHRESH << 8;
2013                 rxdctl |= IGB_RX_WTHRESH << 16;
2014                 wr32(E1000_RXDCTL(j), rxdctl);
2015         }
2016
2017         if (adapter->num_rx_queues > 1) {
2018                 u32 random[10];
2019                 u32 mrqc;
2020                 u32 j, shift;
2021                 union e1000_reta {
2022                         u32 dword;
2023                         u8  bytes[4];
2024                 } reta;
2025
2026                 get_random_bytes(&random[0], 40);
2027
2028                 if (hw->mac.type >= e1000_82576)
2029                         shift = 0;
2030                 else
2031                         shift = 6;
2032                 for (j = 0; j < (32 * 4); j++) {
2033                         reta.bytes[j & 3] =
2034                                 adapter->rx_ring[(j % adapter->num_rx_queues)].reg_idx << shift;
2035                         if ((j & 3) == 3)
2036                                 writel(reta.dword,
2037                                        hw->hw_addr + E1000_RETA(0) + (j & ~3));
2038                 }
2039                 mrqc = E1000_MRQC_ENABLE_RSS_4Q;
2040
2041                 /* Fill out hash function seeds */
2042                 for (j = 0; j < 10; j++)
2043                         array_wr32(E1000_RSSRK(0), j, random[j]);
2044
2045                 mrqc |= (E1000_MRQC_RSS_FIELD_IPV4 |
2046                          E1000_MRQC_RSS_FIELD_IPV4_TCP);
2047                 mrqc |= (E1000_MRQC_RSS_FIELD_IPV6 |
2048                          E1000_MRQC_RSS_FIELD_IPV6_TCP);
2049                 mrqc |= (E1000_MRQC_RSS_FIELD_IPV4_UDP |
2050                          E1000_MRQC_RSS_FIELD_IPV6_UDP);
2051                 mrqc |= (E1000_MRQC_RSS_FIELD_IPV6_UDP_EX |
2052                          E1000_MRQC_RSS_FIELD_IPV6_TCP_EX);
2053
2054
2055                 wr32(E1000_MRQC, mrqc);
2056
2057                 /* Multiqueue and raw packet checksumming are mutually
2058                  * exclusive.  Note that this not the same as TCP/IP
2059                  * checksumming, which works fine. */
2060                 rxcsum = rd32(E1000_RXCSUM);
2061                 rxcsum |= E1000_RXCSUM_PCSD;
2062                 wr32(E1000_RXCSUM, rxcsum);
2063         } else {
2064                 /* Enable Receive Checksum Offload for TCP and UDP */
2065                 rxcsum = rd32(E1000_RXCSUM);
2066                 if (adapter->rx_csum) {
2067                         rxcsum |= E1000_RXCSUM_TUOFL;
2068
2069                         /* Enable IPv4 payload checksum for UDP fragments
2070                          * Must be used in conjunction with packet-split. */
2071                         if (adapter->rx_ps_hdr_size)
2072                                 rxcsum |= E1000_RXCSUM_IPPCSE;
2073                 } else {
2074                         rxcsum &= ~E1000_RXCSUM_TUOFL;
2075                         /* don't need to clear IPPCSE as it defaults to 0 */
2076                 }
2077                 wr32(E1000_RXCSUM, rxcsum);
2078         }
2079
2080         if (adapter->vlgrp)
2081                 wr32(E1000_RLPML,
2082                                 adapter->max_frame_size + VLAN_TAG_SIZE);
2083         else
2084                 wr32(E1000_RLPML, adapter->max_frame_size);
2085
2086         /* Enable Receives */
2087         wr32(E1000_RCTL, rctl);
2088 }
2089
2090 /**
2091  * igb_free_tx_resources - Free Tx Resources per Queue
2092  * @tx_ring: Tx descriptor ring for a specific queue
2093  *
2094  * Free all transmit software resources
2095  **/
2096 void igb_free_tx_resources(struct igb_ring *tx_ring)
2097 {
2098         struct pci_dev *pdev = tx_ring->adapter->pdev;
2099
2100         igb_clean_tx_ring(tx_ring);
2101
2102         vfree(tx_ring->buffer_info);
2103         tx_ring->buffer_info = NULL;
2104
2105         pci_free_consistent(pdev, tx_ring->size, tx_ring->desc, tx_ring->dma);
2106
2107         tx_ring->desc = NULL;
2108 }
2109
2110 /**
2111  * igb_free_all_tx_resources - Free Tx Resources for All Queues
2112  * @adapter: board private structure
2113  *
2114  * Free all transmit software resources
2115  **/
2116 static void igb_free_all_tx_resources(struct igb_adapter *adapter)
2117 {
2118         int i;
2119
2120         for (i = 0; i < adapter->num_tx_queues; i++)
2121                 igb_free_tx_resources(&adapter->tx_ring[i]);
2122 }
2123
2124 static void igb_unmap_and_free_tx_resource(struct igb_adapter *adapter,
2125                                            struct igb_buffer *buffer_info)
2126 {
2127         if (buffer_info->dma) {
2128                 pci_unmap_page(adapter->pdev,
2129                                 buffer_info->dma,
2130                                 buffer_info->length,
2131                                 PCI_DMA_TODEVICE);
2132                 buffer_info->dma = 0;
2133         }
2134         if (buffer_info->skb) {
2135                 dev_kfree_skb_any(buffer_info->skb);
2136                 buffer_info->skb = NULL;
2137         }
2138         buffer_info->time_stamp = 0;
2139         /* buffer_info must be completely set up in the transmit path */
2140 }
2141
2142 /**
2143  * igb_clean_tx_ring - Free Tx Buffers
2144  * @tx_ring: ring to be cleaned
2145  **/
2146 static void igb_clean_tx_ring(struct igb_ring *tx_ring)
2147 {
2148         struct igb_adapter *adapter = tx_ring->adapter;
2149         struct igb_buffer *buffer_info;
2150         unsigned long size;
2151         unsigned int i;
2152
2153         if (!tx_ring->buffer_info)
2154                 return;
2155         /* Free all the Tx ring sk_buffs */
2156
2157         for (i = 0; i < tx_ring->count; i++) {
2158                 buffer_info = &tx_ring->buffer_info[i];
2159                 igb_unmap_and_free_tx_resource(adapter, buffer_info);
2160         }
2161
2162         size = sizeof(struct igb_buffer) * tx_ring->count;
2163         memset(tx_ring->buffer_info, 0, size);
2164
2165         /* Zero out the descriptor ring */
2166
2167         memset(tx_ring->desc, 0, tx_ring->size);
2168
2169         tx_ring->next_to_use = 0;
2170         tx_ring->next_to_clean = 0;
2171
2172         writel(0, adapter->hw.hw_addr + tx_ring->head);
2173         writel(0, adapter->hw.hw_addr + tx_ring->tail);
2174 }
2175
2176 /**
2177  * igb_clean_all_tx_rings - Free Tx Buffers for all queues
2178  * @adapter: board private structure
2179  **/
2180 static void igb_clean_all_tx_rings(struct igb_adapter *adapter)
2181 {
2182         int i;
2183
2184         for (i = 0; i < adapter->num_tx_queues; i++)
2185                 igb_clean_tx_ring(&adapter->tx_ring[i]);
2186 }
2187
2188 /**
2189  * igb_free_rx_resources - Free Rx Resources
2190  * @rx_ring: ring to clean the resources from
2191  *
2192  * Free all receive software resources
2193  **/
2194 void igb_free_rx_resources(struct igb_ring *rx_ring)
2195 {
2196         struct pci_dev *pdev = rx_ring->adapter->pdev;
2197
2198         igb_clean_rx_ring(rx_ring);
2199
2200         vfree(rx_ring->buffer_info);
2201         rx_ring->buffer_info = NULL;
2202
2203         pci_free_consistent(pdev, rx_ring->size, rx_ring->desc, rx_ring->dma);
2204
2205         rx_ring->desc = NULL;
2206 }
2207
2208 /**
2209  * igb_free_all_rx_resources - Free Rx Resources for All Queues
2210  * @adapter: board private structure
2211  *
2212  * Free all receive software resources
2213  **/
2214 static void igb_free_all_rx_resources(struct igb_adapter *adapter)
2215 {
2216         int i;
2217
2218         for (i = 0; i < adapter->num_rx_queues; i++)
2219                 igb_free_rx_resources(&adapter->rx_ring[i]);
2220 }
2221
2222 /**
2223  * igb_clean_rx_ring - Free Rx Buffers per Queue
2224  * @rx_ring: ring to free buffers from
2225  **/
2226 static void igb_clean_rx_ring(struct igb_ring *rx_ring)
2227 {
2228         struct igb_adapter *adapter = rx_ring->adapter;
2229         struct igb_buffer *buffer_info;
2230         struct pci_dev *pdev = adapter->pdev;
2231         unsigned long size;
2232         unsigned int i;
2233
2234         if (!rx_ring->buffer_info)
2235                 return;
2236         /* Free all the Rx ring sk_buffs */
2237         for (i = 0; i < rx_ring->count; i++) {
2238                 buffer_info = &rx_ring->buffer_info[i];
2239                 if (buffer_info->dma) {
2240                         if (adapter->rx_ps_hdr_size)
2241                                 pci_unmap_single(pdev, buffer_info->dma,
2242                                                  adapter->rx_ps_hdr_size,
2243                                                  PCI_DMA_FROMDEVICE);
2244                         else
2245                                 pci_unmap_single(pdev, buffer_info->dma,
2246                                                  adapter->rx_buffer_len,
2247                                                  PCI_DMA_FROMDEVICE);
2248                         buffer_info->dma = 0;
2249                 }
2250
2251                 if (buffer_info->skb) {
2252                         dev_kfree_skb(buffer_info->skb);
2253                         buffer_info->skb = NULL;
2254                 }
2255                 if (buffer_info->page) {
2256                         if (buffer_info->page_dma)
2257                                 pci_unmap_page(pdev, buffer_info->page_dma,
2258                                                PAGE_SIZE / 2,
2259                                                PCI_DMA_FROMDEVICE);
2260                         put_page(buffer_info->page);
2261                         buffer_info->page = NULL;
2262                         buffer_info->page_dma = 0;
2263                         buffer_info->page_offset = 0;
2264                 }
2265         }
2266
2267         size = sizeof(struct igb_buffer) * rx_ring->count;
2268         memset(rx_ring->buffer_info, 0, size);
2269
2270         /* Zero out the descriptor ring */
2271         memset(rx_ring->desc, 0, rx_ring->size);
2272
2273         rx_ring->next_to_clean = 0;
2274         rx_ring->next_to_use = 0;
2275
2276         writel(0, adapter->hw.hw_addr + rx_ring->head);
2277         writel(0, adapter->hw.hw_addr + rx_ring->tail);
2278 }
2279
2280 /**
2281  * igb_clean_all_rx_rings - Free Rx Buffers for all queues
2282  * @adapter: board private structure
2283  **/
2284 static void igb_clean_all_rx_rings(struct igb_adapter *adapter)
2285 {
2286         int i;
2287
2288         for (i = 0; i < adapter->num_rx_queues; i++)
2289                 igb_clean_rx_ring(&adapter->rx_ring[i]);
2290 }
2291
2292 /**
2293  * igb_set_mac - Change the Ethernet Address of the NIC
2294  * @netdev: network interface device structure
2295  * @p: pointer to an address structure
2296  *
2297  * Returns 0 on success, negative on failure
2298  **/
2299 static int igb_set_mac(struct net_device *netdev, void *p)
2300 {
2301         struct igb_adapter *adapter = netdev_priv(netdev);
2302         struct e1000_hw *hw = &adapter->hw;
2303         struct sockaddr *addr = p;
2304
2305         if (!is_valid_ether_addr(addr->sa_data))
2306                 return -EADDRNOTAVAIL;
2307
2308         memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
2309         memcpy(hw->mac.addr, addr->sa_data, netdev->addr_len);
2310
2311         hw->mac.ops.rar_set(hw, hw->mac.addr, 0);
2312
2313         return 0;
2314 }
2315
2316 /**
2317  * igb_set_multi - Multicast and Promiscuous mode set
2318  * @netdev: network interface device structure
2319  *
2320  * The set_multi entry point is called whenever the multicast address
2321  * list or the network interface flags are updated.  This routine is
2322  * responsible for configuring the hardware for proper multicast,
2323  * promiscuous mode, and all-multi behavior.
2324  **/
2325 static void igb_set_multi(struct net_device *netdev)
2326 {
2327         struct igb_adapter *adapter = netdev_priv(netdev);
2328         struct e1000_hw *hw = &adapter->hw;
2329         struct e1000_mac_info *mac = &hw->mac;
2330         struct dev_mc_list *mc_ptr;
2331         u8  *mta_list;
2332         u32 rctl;
2333         int i;
2334
2335         /* Check for Promiscuous and All Multicast modes */
2336
2337         rctl = rd32(E1000_RCTL);
2338
2339         if (netdev->flags & IFF_PROMISC) {
2340                 rctl |= (E1000_RCTL_UPE | E1000_RCTL_MPE);
2341                 rctl &= ~E1000_RCTL_VFE;
2342         } else {
2343                 if (netdev->flags & IFF_ALLMULTI) {
2344                         rctl |= E1000_RCTL_MPE;
2345                         rctl &= ~E1000_RCTL_UPE;
2346                 } else
2347                         rctl &= ~(E1000_RCTL_UPE | E1000_RCTL_MPE);
2348                 rctl |= E1000_RCTL_VFE;
2349         }
2350         wr32(E1000_RCTL, rctl);
2351
2352         if (!netdev->mc_count) {
2353                 /* nothing to program, so clear mc list */
2354                 igb_update_mc_addr_list(hw, NULL, 0, 1,
2355                                         mac->rar_entry_count);
2356                 return;
2357         }
2358
2359         mta_list = kzalloc(netdev->mc_count * 6, GFP_ATOMIC);
2360         if (!mta_list)
2361                 return;
2362
2363         /* The shared function expects a packed array of only addresses. */
2364         mc_ptr = netdev->mc_list;
2365
2366         for (i = 0; i < netdev->mc_count; i++) {
2367                 if (!mc_ptr)
2368                         break;
2369                 memcpy(mta_list + (i*ETH_ALEN), mc_ptr->dmi_addr, ETH_ALEN);
2370                 mc_ptr = mc_ptr->next;
2371         }
2372         igb_update_mc_addr_list(hw, mta_list, i, 1, mac->rar_entry_count);
2373         kfree(mta_list);
2374 }
2375
2376 /* Need to wait a few seconds after link up to get diagnostic information from
2377  * the phy */
2378 static void igb_update_phy_info(unsigned long data)
2379 {
2380         struct igb_adapter *adapter = (struct igb_adapter *) data;
2381         igb_get_phy_info(&adapter->hw);
2382 }
2383
2384 /**
2385  * igb_has_link - check shared code for link and determine up/down
2386  * @adapter: pointer to driver private info
2387  **/
2388 static bool igb_has_link(struct igb_adapter *adapter)
2389 {
2390         struct e1000_hw *hw = &adapter->hw;
2391         bool link_active = false;
2392         s32 ret_val = 0;
2393
2394         /* get_link_status is set on LSC (link status) interrupt or
2395          * rx sequence error interrupt.  get_link_status will stay
2396          * false until the e1000_check_for_link establishes link
2397          * for copper adapters ONLY
2398          */
2399         switch (hw->phy.media_type) {
2400         case e1000_media_type_copper:
2401                 if (hw->mac.get_link_status) {
2402                         ret_val = hw->mac.ops.check_for_link(hw);
2403                         link_active = !hw->mac.get_link_status;
2404                 } else {
2405                         link_active = true;
2406                 }
2407                 break;
2408         case e1000_media_type_fiber:
2409                 ret_val = hw->mac.ops.check_for_link(hw);
2410                 link_active = !!(rd32(E1000_STATUS) & E1000_STATUS_LU);
2411                 break;
2412         case e1000_media_type_internal_serdes:
2413                 ret_val = hw->mac.ops.check_for_link(hw);
2414                 link_active = hw->mac.serdes_has_link;
2415                 break;
2416         default:
2417         case e1000_media_type_unknown:
2418                 break;
2419         }
2420
2421         return link_active;
2422 }
2423
2424 /**
2425  * igb_watchdog - Timer Call-back
2426  * @data: pointer to adapter cast into an unsigned long
2427  **/
2428 static void igb_watchdog(unsigned long data)
2429 {
2430         struct igb_adapter *adapter = (struct igb_adapter *)data;
2431         /* Do the rest outside of interrupt context */
2432         schedule_work(&adapter->watchdog_task);
2433 }
2434
2435 static void igb_watchdog_task(struct work_struct *work)
2436 {
2437         struct igb_adapter *adapter = container_of(work,
2438                                         struct igb_adapter, watchdog_task);
2439         struct e1000_hw *hw = &adapter->hw;
2440         struct net_device *netdev = adapter->netdev;
2441         struct igb_ring *tx_ring = adapter->tx_ring;
2442         u32 link;
2443         u32 eics = 0;
2444         int i;
2445
2446         link = igb_has_link(adapter);
2447         if ((netif_carrier_ok(netdev)) && link)
2448                 goto link_up;
2449
2450         if (link) {
2451                 if (!netif_carrier_ok(netdev)) {
2452                         u32 ctrl;
2453                         hw->mac.ops.get_speed_and_duplex(&adapter->hw,
2454                                                    &adapter->link_speed,
2455                                                    &adapter->link_duplex);
2456
2457                         ctrl = rd32(E1000_CTRL);
2458                         /* Links status message must follow this format */
2459                         printk(KERN_INFO "igb: %s NIC Link is Up %d Mbps %s, "
2460                                  "Flow Control: %s\n",
2461                                  netdev->name,
2462                                  adapter->link_speed,
2463                                  adapter->link_duplex == FULL_DUPLEX ?
2464                                  "Full Duplex" : "Half Duplex",
2465                                  ((ctrl & E1000_CTRL_TFCE) && (ctrl &
2466                                  E1000_CTRL_RFCE)) ? "RX/TX" : ((ctrl &
2467                                  E1000_CTRL_RFCE) ? "RX" : ((ctrl &
2468                                  E1000_CTRL_TFCE) ? "TX" : "None")));
2469
2470                         /* tweak tx_queue_len according to speed/duplex and
2471                          * adjust the timeout factor */
2472                         netdev->tx_queue_len = adapter->tx_queue_len;
2473                         adapter->tx_timeout_factor = 1;
2474                         switch (adapter->link_speed) {
2475                         case SPEED_10:
2476                                 netdev->tx_queue_len = 10;
2477                                 adapter->tx_timeout_factor = 14;
2478                                 break;
2479                         case SPEED_100:
2480                                 netdev->tx_queue_len = 100;
2481                                 /* maybe add some timeout factor ? */
2482                                 break;
2483                         }
2484
2485                         netif_carrier_on(netdev);
2486                         netif_tx_wake_all_queues(netdev);
2487
2488                         /* link state has changed, schedule phy info update */
2489                         if (!test_bit(__IGB_DOWN, &adapter->state))
2490                                 mod_timer(&adapter->phy_info_timer,
2491                                           round_jiffies(jiffies + 2 * HZ));
2492                 }
2493         } else {
2494                 if (netif_carrier_ok(netdev)) {
2495                         adapter->link_speed = 0;
2496                         adapter->link_duplex = 0;
2497                         /* Links status message must follow this format */
2498                         printk(KERN_INFO "igb: %s NIC Link is Down\n",
2499                                netdev->name);
2500                         netif_carrier_off(netdev);
2501                         netif_tx_stop_all_queues(netdev);
2502
2503                         /* link state has changed, schedule phy info update */
2504                         if (!test_bit(__IGB_DOWN, &adapter->state))
2505                                 mod_timer(&adapter->phy_info_timer,
2506                                           round_jiffies(jiffies + 2 * HZ));
2507                 }
2508         }
2509
2510 link_up:
2511         igb_update_stats(adapter);
2512
2513         hw->mac.tx_packet_delta = adapter->stats.tpt - adapter->tpt_old;
2514         adapter->tpt_old = adapter->stats.tpt;
2515         hw->mac.collision_delta = adapter->stats.colc - adapter->colc_old;
2516         adapter->colc_old = adapter->stats.colc;
2517
2518         adapter->gorc = adapter->stats.gorc - adapter->gorc_old;
2519         adapter->gorc_old = adapter->stats.gorc;
2520         adapter->gotc = adapter->stats.gotc - adapter->gotc_old;
2521         adapter->gotc_old = adapter->stats.gotc;
2522
2523         igb_update_adaptive(&adapter->hw);
2524
2525         if (!netif_carrier_ok(netdev)) {
2526                 if (IGB_DESC_UNUSED(tx_ring) + 1 < tx_ring->count) {
2527                         /* We've lost link, so the controller stops DMA,
2528                          * but we've got queued Tx work that's never going
2529                          * to get done, so reset controller to flush Tx.
2530                          * (Do the reset outside of interrupt context). */
2531                         adapter->tx_timeout_count++;
2532                         schedule_work(&adapter->reset_task);
2533                 }
2534         }
2535
2536         /* Cause software interrupt to ensure rx ring is cleaned */
2537         if (adapter->msix_entries) {
2538                 for (i = 0; i < adapter->num_rx_queues; i++)
2539                         eics |= adapter->rx_ring[i].eims_value;
2540                 wr32(E1000_EICS, eics);
2541         } else {
2542                 wr32(E1000_ICS, E1000_ICS_RXDMT0);
2543         }
2544
2545         /* Force detection of hung controller every watchdog period */
2546         tx_ring->detect_tx_hung = true;
2547
2548         /* Reset the timer */
2549         if (!test_bit(__IGB_DOWN, &adapter->state))
2550                 mod_timer(&adapter->watchdog_timer,
2551                           round_jiffies(jiffies + 2 * HZ));
2552 }
2553
2554 enum latency_range {
2555         lowest_latency = 0,
2556         low_latency = 1,
2557         bulk_latency = 2,
2558         latency_invalid = 255
2559 };
2560
2561
2562 /**
2563  * igb_update_ring_itr - update the dynamic ITR value based on packet size
2564  *
2565  *      Stores a new ITR value based on strictly on packet size.  This
2566  *      algorithm is less sophisticated than that used in igb_update_itr,
2567  *      due to the difficulty of synchronizing statistics across multiple
2568  *      receive rings.  The divisors and thresholds used by this fuction
2569  *      were determined based on theoretical maximum wire speed and testing
2570  *      data, in order to minimize response time while increasing bulk
2571  *      throughput.
2572  *      This functionality is controlled by the InterruptThrottleRate module
2573  *      parameter (see igb_param.c)
2574  *      NOTE:  This function is called only when operating in a multiqueue
2575  *             receive environment.
2576  * @rx_ring: pointer to ring
2577  **/
2578 static void igb_update_ring_itr(struct igb_ring *rx_ring)
2579 {
2580         int new_val = rx_ring->itr_val;
2581         int avg_wire_size = 0;
2582         struct igb_adapter *adapter = rx_ring->adapter;
2583
2584         if (!rx_ring->total_packets)
2585                 goto clear_counts; /* no packets, so don't do anything */
2586
2587         /* For non-gigabit speeds, just fix the interrupt rate at 4000
2588          * ints/sec - ITR timer value of 120 ticks.
2589          */
2590         if (adapter->link_speed != SPEED_1000) {
2591                 new_val = 120;
2592                 goto set_itr_val;
2593         }
2594         avg_wire_size = rx_ring->total_bytes / rx_ring->total_packets;
2595
2596         /* Add 24 bytes to size to account for CRC, preamble, and gap */
2597         avg_wire_size += 24;
2598
2599         /* Don't starve jumbo frames */
2600         avg_wire_size = min(avg_wire_size, 3000);
2601
2602         /* Give a little boost to mid-size frames */
2603         if ((avg_wire_size > 300) && (avg_wire_size < 1200))
2604                 new_val = avg_wire_size / 3;
2605         else
2606                 new_val = avg_wire_size / 2;
2607
2608 set_itr_val:
2609         if (new_val != rx_ring->itr_val) {
2610                 rx_ring->itr_val = new_val;
2611                 rx_ring->set_itr = 1;
2612         }
2613 clear_counts:
2614         rx_ring->total_bytes = 0;
2615         rx_ring->total_packets = 0;
2616 }
2617
2618 /**
2619  * igb_update_itr - update the dynamic ITR value based on statistics
2620  *      Stores a new ITR value based on packets and byte
2621  *      counts during the last interrupt.  The advantage of per interrupt
2622  *      computation is faster updates and more accurate ITR for the current
2623  *      traffic pattern.  Constants in this function were computed
2624  *      based on theoretical maximum wire speed and thresholds were set based
2625  *      on testing data as well as attempting to minimize response time
2626  *      while increasing bulk throughput.
2627  *      this functionality is controlled by the InterruptThrottleRate module
2628  *      parameter (see igb_param.c)
2629  *      NOTE:  These calculations are only valid when operating in a single-
2630  *             queue environment.
2631  * @adapter: pointer to adapter
2632  * @itr_setting: current adapter->itr
2633  * @packets: the number of packets during this measurement interval
2634  * @bytes: the number of bytes during this measurement interval
2635  **/
2636 static unsigned int igb_update_itr(struct igb_adapter *adapter, u16 itr_setting,
2637                                    int packets, int bytes)
2638 {
2639         unsigned int retval = itr_setting;
2640
2641         if (packets == 0)
2642                 goto update_itr_done;
2643
2644         switch (itr_setting) {
2645         case lowest_latency:
2646                 /* handle TSO and jumbo frames */
2647                 if (bytes/packets > 8000)
2648                         retval = bulk_latency;
2649                 else if ((packets < 5) && (bytes > 512))
2650                         retval = low_latency;
2651                 break;
2652         case low_latency:  /* 50 usec aka 20000 ints/s */
2653                 if (bytes > 10000) {
2654                         /* this if handles the TSO accounting */
2655                         if (bytes/packets > 8000) {
2656                                 retval = bulk_latency;
2657                         } else if ((packets < 10) || ((bytes/packets) > 1200)) {
2658                                 retval = bulk_latency;
2659                         } else if ((packets > 35)) {
2660                                 retval = lowest_latency;
2661                         }
2662                 } else if (bytes/packets > 2000) {
2663                         retval = bulk_latency;
2664                 } else if (packets <= 2 && bytes < 512) {
2665                         retval = lowest_latency;
2666                 }
2667                 break;
2668         case bulk_latency: /* 250 usec aka 4000 ints/s */
2669                 if (bytes > 25000) {
2670                         if (packets > 35)
2671                                 retval = low_latency;
2672                 } else if (bytes < 6000) {
2673                         retval = low_latency;
2674                 }
2675                 break;
2676         }
2677
2678 update_itr_done:
2679         return retval;
2680 }
2681
2682 static void igb_set_itr(struct igb_adapter *adapter)
2683 {
2684         u16 current_itr;
2685         u32 new_itr = adapter->itr;
2686
2687         /* for non-gigabit speeds, just fix the interrupt rate at 4000 */
2688         if (adapter->link_speed != SPEED_1000) {
2689                 current_itr = 0;
2690                 new_itr = 4000;
2691                 goto set_itr_now;
2692         }
2693
2694         adapter->rx_itr = igb_update_itr(adapter,
2695                                     adapter->rx_itr,
2696                                     adapter->rx_ring->total_packets,
2697                                     adapter->rx_ring->total_bytes);
2698
2699         if (adapter->rx_ring->buddy) {
2700                 adapter->tx_itr = igb_update_itr(adapter,
2701                                             adapter->tx_itr,
2702                                             adapter->tx_ring->total_packets,
2703                                             adapter->tx_ring->total_bytes);
2704
2705                 current_itr = max(adapter->rx_itr, adapter->tx_itr);
2706         } else {
2707                 current_itr = adapter->rx_itr;
2708         }
2709
2710         /* conservative mode (itr 3) eliminates the lowest_latency setting */
2711         if (adapter->itr_setting == 3 &&
2712             current_itr == lowest_latency)
2713                 current_itr = low_latency;
2714
2715         switch (current_itr) {
2716         /* counts and packets in update_itr are dependent on these numbers */
2717         case lowest_latency:
2718                 new_itr = 70000;
2719                 break;
2720         case low_latency:
2721                 new_itr = 20000; /* aka hwitr = ~200 */
2722                 break;
2723         case bulk_latency:
2724                 new_itr = 4000;
2725                 break;
2726         default:
2727                 break;
2728         }
2729
2730 set_itr_now:
2731         adapter->rx_ring->total_bytes = 0;
2732         adapter->rx_ring->total_packets = 0;
2733         if (adapter->rx_ring->buddy) {
2734                 adapter->rx_ring->buddy->total_bytes = 0;
2735                 adapter->rx_ring->buddy->total_packets = 0;
2736         }
2737
2738         if (new_itr != adapter->itr) {
2739                 /* this attempts to bias the interrupt rate towards Bulk
2740                  * by adding intermediate steps when interrupt rate is
2741                  * increasing */
2742                 new_itr = new_itr > adapter->itr ?
2743                              min(adapter->itr + (new_itr >> 2), new_itr) :
2744                              new_itr;
2745                 /* Don't write the value here; it resets the adapter's
2746                  * internal timer, and causes us to delay far longer than
2747                  * we should between interrupts.  Instead, we write the ITR
2748                  * value at the beginning of the next interrupt so the timing
2749                  * ends up being correct.
2750                  */
2751                 adapter->itr = new_itr;
2752                 adapter->rx_ring->itr_val = 1000000000 / (new_itr * 256);
2753                 adapter->rx_ring->set_itr = 1;
2754         }
2755
2756         return;
2757 }
2758
2759
2760 #define IGB_TX_FLAGS_CSUM               0x00000001
2761 #define IGB_TX_FLAGS_VLAN               0x00000002
2762 #define IGB_TX_FLAGS_TSO                0x00000004
2763 #define IGB_TX_FLAGS_IPV4               0x00000008
2764 #define IGB_TX_FLAGS_TSTAMP             0x00000010
2765 #define IGB_TX_FLAGS_VLAN_MASK  0xffff0000
2766 #define IGB_TX_FLAGS_VLAN_SHIFT 16
2767
2768 static inline int igb_tso_adv(struct igb_adapter *adapter,
2769                               struct igb_ring *tx_ring,
2770                               struct sk_buff *skb, u32 tx_flags, u8 *hdr_len)
2771 {
2772         struct e1000_adv_tx_context_desc *context_desc;
2773         unsigned int i;
2774         int err;
2775         struct igb_buffer *buffer_info;
2776         u32 info = 0, tu_cmd = 0;
2777         u32 mss_l4len_idx, l4len;
2778         *hdr_len = 0;
2779
2780         if (skb_header_cloned(skb)) {
2781                 err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2782                 if (err)
2783                         return err;
2784         }
2785
2786         l4len = tcp_hdrlen(skb);
2787         *hdr_len += l4len;
2788
2789         if (skb->protocol == htons(ETH_P_IP)) {
2790                 struct iphdr *iph = ip_hdr(skb);
2791                 iph->tot_len = 0;
2792                 iph->check = 0;
2793                 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
2794                                                          iph->daddr, 0,
2795                                                          IPPROTO_TCP,
2796                                                          0);
2797         } else if (skb_shinfo(skb)->gso_type == SKB_GSO_TCPV6) {
2798                 ipv6_hdr(skb)->payload_len = 0;
2799                 tcp_hdr(skb)->check = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
2800                                                        &ipv6_hdr(skb)->daddr,
2801                                                        0, IPPROTO_TCP, 0);
2802         }
2803
2804         i = tx_ring->next_to_use;
2805
2806         buffer_info = &tx_ring->buffer_info[i];
2807         context_desc = E1000_TX_CTXTDESC_ADV(*tx_ring, i);
2808         /* VLAN MACLEN IPLEN */
2809         if (tx_flags & IGB_TX_FLAGS_VLAN)
2810                 info |= (tx_flags & IGB_TX_FLAGS_VLAN_MASK);
2811         info |= (skb_network_offset(skb) << E1000_ADVTXD_MACLEN_SHIFT);
2812         *hdr_len += skb_network_offset(skb);
2813         info |= skb_network_header_len(skb);
2814         *hdr_len += skb_network_header_len(skb);
2815         context_desc->vlan_macip_lens = cpu_to_le32(info);
2816
2817         /* ADV DTYP TUCMD MKRLOC/ISCSIHEDLEN */
2818         tu_cmd |= (E1000_TXD_CMD_DEXT | E1000_ADVTXD_DTYP_CTXT);
2819
2820         if (skb->protocol == htons(ETH_P_IP))
2821                 tu_cmd |= E1000_ADVTXD_TUCMD_IPV4;
2822         tu_cmd |= E1000_ADVTXD_TUCMD_L4T_TCP;
2823
2824         context_desc->type_tucmd_mlhl = cpu_to_le32(tu_cmd);
2825
2826         /* MSS L4LEN IDX */
2827         mss_l4len_idx = (skb_shinfo(skb)->gso_size << E1000_ADVTXD_MSS_SHIFT);
2828         mss_l4len_idx |= (l4len << E1000_ADVTXD_L4LEN_SHIFT);
2829
2830         /* Context index must be unique per ring. */
2831         if (adapter->flags & IGB_FLAG_NEED_CTX_IDX)
2832                 mss_l4len_idx |= tx_ring->queue_index << 4;
2833
2834         context_desc->mss_l4len_idx = cpu_to_le32(mss_l4len_idx);
2835         context_desc->seqnum_seed = 0;
2836
2837         buffer_info->time_stamp = jiffies;
2838         buffer_info->next_to_watch = i;
2839         buffer_info->dma = 0;
2840         i++;
2841         if (i == tx_ring->count)
2842                 i = 0;
2843
2844         tx_ring->next_to_use = i;
2845
2846         return true;
2847 }
2848
2849 static inline bool igb_tx_csum_adv(struct igb_adapter *adapter,
2850                                         struct igb_ring *tx_ring,
2851                                         struct sk_buff *skb, u32 tx_flags)
2852 {
2853         struct e1000_adv_tx_context_desc *context_desc;
2854         unsigned int i;
2855         struct igb_buffer *buffer_info;
2856         u32 info = 0, tu_cmd = 0;
2857
2858         if ((skb->ip_summed == CHECKSUM_PARTIAL) ||
2859             (tx_flags & IGB_TX_FLAGS_VLAN)) {
2860                 i = tx_ring->next_to_use;
2861                 buffer_info = &tx_ring->buffer_info[i];
2862                 context_desc = E1000_TX_CTXTDESC_ADV(*tx_ring, i);
2863
2864                 if (tx_flags & IGB_TX_FLAGS_VLAN)
2865                         info |= (tx_flags & IGB_TX_FLAGS_VLAN_MASK);
2866                 info |= (skb_network_offset(skb) << E1000_ADVTXD_MACLEN_SHIFT);
2867                 if (skb->ip_summed == CHECKSUM_PARTIAL)
2868                         info |= skb_network_header_len(skb);
2869
2870                 context_desc->vlan_macip_lens = cpu_to_le32(info);
2871
2872                 tu_cmd |= (E1000_TXD_CMD_DEXT | E1000_ADVTXD_DTYP_CTXT);
2873
2874                 if (skb->ip_summed == CHECKSUM_PARTIAL) {
2875                         switch (skb->protocol) {
2876                         case cpu_to_be16(ETH_P_IP):
2877                                 tu_cmd |= E1000_ADVTXD_TUCMD_IPV4;
2878                                 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
2879                                         tu_cmd |= E1000_ADVTXD_TUCMD_L4T_TCP;
2880                                 break;
2881                         case cpu_to_be16(ETH_P_IPV6):
2882                                 /* XXX what about other V6 headers?? */
2883                                 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
2884                                         tu_cmd |= E1000_ADVTXD_TUCMD_L4T_TCP;
2885                                 break;
2886                         default:
2887                                 if (unlikely(net_ratelimit()))
2888                                         dev_warn(&adapter->pdev->dev,
2889                                             "partial checksum but proto=%x!\n",
2890                                             skb->protocol);
2891                                 break;
2892                         }
2893                 }
2894
2895                 context_desc->type_tucmd_mlhl = cpu_to_le32(tu_cmd);
2896                 context_desc->seqnum_seed = 0;
2897                 if (adapter->flags & IGB_FLAG_NEED_CTX_IDX)
2898                         context_desc->mss_l4len_idx =
2899                                 cpu_to_le32(tx_ring->queue_index << 4);
2900                 else
2901                         context_desc->mss_l4len_idx = 0;
2902
2903                 buffer_info->time_stamp = jiffies;
2904                 buffer_info->next_to_watch = i;
2905                 buffer_info->dma = 0;
2906
2907                 i++;
2908                 if (i == tx_ring->count)
2909                         i = 0;
2910                 tx_ring->next_to_use = i;
2911
2912                 return true;
2913         }
2914
2915
2916         return false;
2917 }
2918
2919 #define IGB_MAX_TXD_PWR 16
2920 #define IGB_MAX_DATA_PER_TXD    (1<<IGB_MAX_TXD_PWR)
2921
2922 static inline int igb_tx_map_adv(struct igb_adapter *adapter,
2923                                  struct igb_ring *tx_ring, struct sk_buff *skb,
2924                                  unsigned int first)
2925 {
2926         struct igb_buffer *buffer_info;
2927         unsigned int len = skb_headlen(skb);
2928         unsigned int count = 0, i;
2929         unsigned int f;
2930
2931         i = tx_ring->next_to_use;
2932
2933         buffer_info = &tx_ring->buffer_info[i];
2934         BUG_ON(len >= IGB_MAX_DATA_PER_TXD);
2935         buffer_info->length = len;
2936         /* set time_stamp *before* dma to help avoid a possible race */
2937         buffer_info->time_stamp = jiffies;
2938         buffer_info->next_to_watch = i;
2939         buffer_info->dma = pci_map_single(adapter->pdev, skb->data, len,
2940                                           PCI_DMA_TODEVICE);
2941         count++;
2942         i++;
2943         if (i == tx_ring->count)
2944                 i = 0;
2945
2946         for (f = 0; f < skb_shinfo(skb)->nr_frags; f++) {
2947                 struct skb_frag_struct *frag;
2948
2949                 frag = &skb_shinfo(skb)->frags[f];
2950                 len = frag->size;
2951
2952                 buffer_info = &tx_ring->buffer_info[i];
2953                 BUG_ON(len >= IGB_MAX_DATA_PER_TXD);
2954                 buffer_info->length = len;
2955                 buffer_info->time_stamp = jiffies;
2956                 buffer_info->next_to_watch = i;
2957                 buffer_info->dma = pci_map_page(adapter->pdev,
2958                                                 frag->page,
2959                                                 frag->page_offset,
2960                                                 len,
2961                                                 PCI_DMA_TODEVICE);
2962
2963                 count++;
2964                 i++;
2965                 if (i == tx_ring->count)
2966                         i = 0;
2967         }
2968
2969         i = ((i == 0) ? tx_ring->count - 1 : i - 1);
2970         tx_ring->buffer_info[i].skb = skb;
2971         tx_ring->buffer_info[first].next_to_watch = i;
2972
2973         return count;
2974 }
2975
2976 static inline void igb_tx_queue_adv(struct igb_adapter *adapter,
2977                                     struct igb_ring *tx_ring,
2978                                     int tx_flags, int count, u32 paylen,
2979                                     u8 hdr_len)
2980 {
2981         union e1000_adv_tx_desc *tx_desc = NULL;
2982         struct igb_buffer *buffer_info;
2983         u32 olinfo_status = 0, cmd_type_len;
2984         unsigned int i;
2985
2986         cmd_type_len = (E1000_ADVTXD_DTYP_DATA | E1000_ADVTXD_DCMD_IFCS |
2987                         E1000_ADVTXD_DCMD_DEXT);
2988
2989         if (tx_flags & IGB_TX_FLAGS_VLAN)
2990                 cmd_type_len |= E1000_ADVTXD_DCMD_VLE;
2991
2992         if (tx_flags & IGB_TX_FLAGS_TSTAMP)
2993                 cmd_type_len |= E1000_ADVTXD_MAC_TSTAMP;
2994
2995         if (tx_flags & IGB_TX_FLAGS_TSO) {
2996                 cmd_type_len |= E1000_ADVTXD_DCMD_TSE;
2997
2998                 /* insert tcp checksum */
2999                 olinfo_status |= E1000_TXD_POPTS_TXSM << 8;
3000
3001                 /* insert ip checksum */
3002                 if (tx_flags & IGB_TX_FLAGS_IPV4)
3003                         olinfo_status |= E1000_TXD_POPTS_IXSM << 8;
3004
3005         } else if (tx_flags & IGB_TX_FLAGS_CSUM) {
3006                 olinfo_status |= E1000_TXD_POPTS_TXSM << 8;
3007         }
3008
3009         if ((adapter->flags & IGB_FLAG_NEED_CTX_IDX) &&
3010             (tx_flags & (IGB_TX_FLAGS_CSUM | IGB_TX_FLAGS_TSO |
3011                          IGB_TX_FLAGS_VLAN)))
3012                 olinfo_status |= tx_ring->queue_index << 4;
3013
3014         olinfo_status |= ((paylen - hdr_len) << E1000_ADVTXD_PAYLEN_SHIFT);
3015
3016         i = tx_ring->next_to_use;
3017         while (count--) {
3018                 buffer_info = &tx_ring->buffer_info[i];
3019                 tx_desc = E1000_TX_DESC_ADV(*tx_ring, i);
3020                 tx_desc->read.buffer_addr = cpu_to_le64(buffer_info->dma);
3021                 tx_desc->read.cmd_type_len =
3022                         cpu_to_le32(cmd_type_len | buffer_info->length);
3023                 tx_desc->read.olinfo_status = cpu_to_le32(olinfo_status);
3024                 i++;
3025                 if (i == tx_ring->count)
3026                         i = 0;
3027         }
3028
3029         tx_desc->read.cmd_type_len |= cpu_to_le32(adapter->txd_cmd);
3030         /* Force memory writes to complete before letting h/w
3031          * know there are new descriptors to fetch.  (Only
3032          * applicable for weak-ordered memory model archs,
3033          * such as IA-64). */
3034         wmb();
3035
3036         tx_ring->next_to_use = i;
3037         writel(i, adapter->hw.hw_addr + tx_ring->tail);
3038         /* we need this if more than one processor can write to our tail
3039          * at a time, it syncronizes IO on IA64/Altix systems */
3040         mmiowb();
3041 }
3042
3043 static int __igb_maybe_stop_tx(struct net_device *netdev,
3044                                struct igb_ring *tx_ring, int size)
3045 {
3046         struct igb_adapter *adapter = netdev_priv(netdev);
3047
3048         netif_stop_subqueue(netdev, tx_ring->queue_index);
3049
3050         /* Herbert's original patch had:
3051          *  smp_mb__after_netif_stop_queue();
3052          * but since that doesn't exist yet, just open code it. */
3053         smp_mb();
3054
3055         /* We need to check again in a case another CPU has just
3056          * made room available. */
3057         if (IGB_DESC_UNUSED(tx_ring) < size)
3058                 return -EBUSY;
3059
3060         /* A reprieve! */
3061         netif_wake_subqueue(netdev, tx_ring->queue_index);
3062         ++adapter->restart_queue;
3063         return 0;
3064 }
3065
3066 static int igb_maybe_stop_tx(struct net_device *netdev,
3067                              struct igb_ring *tx_ring, int size)
3068 {
3069         if (IGB_DESC_UNUSED(tx_ring) >= size)
3070                 return 0;
3071         return __igb_maybe_stop_tx(netdev, tx_ring, size);
3072 }
3073
3074 #define TXD_USE_COUNT(S) (((S) >> (IGB_MAX_TXD_PWR)) + 1)
3075
3076 static int igb_xmit_frame_ring_adv(struct sk_buff *skb,
3077                                    struct net_device *netdev,
3078                                    struct igb_ring *tx_ring)
3079 {
3080         struct igb_adapter *adapter = netdev_priv(netdev);
3081         unsigned int first;
3082         unsigned int tx_flags = 0;
3083         u8 hdr_len = 0;
3084         int tso = 0;
3085         union skb_shared_tx *shtx;
3086
3087         if (test_bit(__IGB_DOWN, &adapter->state)) {
3088                 dev_kfree_skb_any(skb);
3089                 return NETDEV_TX_OK;
3090         }
3091
3092         if (skb->len <= 0) {
3093                 dev_kfree_skb_any(skb);
3094                 return NETDEV_TX_OK;
3095         }
3096
3097         /* need: 1 descriptor per page,
3098          *       + 2 desc gap to keep tail from touching head,
3099          *       + 1 desc for skb->data,
3100          *       + 1 desc for context descriptor,
3101          * otherwise try next time */
3102         if (igb_maybe_stop_tx(netdev, tx_ring, skb_shinfo(skb)->nr_frags + 4)) {
3103                 /* this is a hard error */
3104                 return NETDEV_TX_BUSY;
3105         }
3106
3107         /*
3108          * TODO: check that there currently is no other packet with
3109          * time stamping in the queue
3110          *
3111          * When doing time stamping, keep the connection to the socket
3112          * a while longer: it is still needed by skb_hwtstamp_tx(),
3113          * called either in igb_tx_hwtstamp() or by our caller when
3114          * doing software time stamping.
3115          */
3116         shtx = skb_tx(skb);
3117         if (unlikely(shtx->hardware)) {
3118                 shtx->in_progress = 1;
3119                 tx_flags |= IGB_TX_FLAGS_TSTAMP;
3120         } else if (likely(!shtx->software)) {
3121                 /*
3122                  * TODO: can this be solved in dev.c:dev_hard_start_xmit()?
3123                  * There are probably unmodified driver which do something
3124                  * like this and thus don't work in combination with
3125                  * SOF_TIMESTAMPING_TX_SOFTWARE.
3126                  */
3127                 skb_orphan(skb);
3128         }
3129
3130         if (adapter->vlgrp && vlan_tx_tag_present(skb)) {
3131                 tx_flags |= IGB_TX_FLAGS_VLAN;
3132                 tx_flags |= (vlan_tx_tag_get(skb) << IGB_TX_FLAGS_VLAN_SHIFT);
3133         }
3134
3135         if (skb->protocol == htons(ETH_P_IP))
3136                 tx_flags |= IGB_TX_FLAGS_IPV4;
3137
3138         first = tx_ring->next_to_use;
3139
3140         tso = skb_is_gso(skb) ? igb_tso_adv(adapter, tx_ring, skb, tx_flags,
3141                                               &hdr_len) : 0;
3142
3143         if (tso < 0) {
3144                 dev_kfree_skb_any(skb);
3145                 return NETDEV_TX_OK;
3146         }
3147
3148         if (tso)
3149                 tx_flags |= IGB_TX_FLAGS_TSO;
3150         else if (igb_tx_csum_adv(adapter, tx_ring, skb, tx_flags))
3151                         if (skb->ip_summed == CHECKSUM_PARTIAL)
3152                                 tx_flags |= IGB_TX_FLAGS_CSUM;
3153
3154         igb_tx_queue_adv(adapter, tx_ring, tx_flags,
3155                          igb_tx_map_adv(adapter, tx_ring, skb, first),
3156                          skb->len, hdr_len);
3157
3158         netdev->trans_start = jiffies;
3159
3160         /* Make sure there is space in the ring for the next send. */
3161         igb_maybe_stop_tx(netdev, tx_ring, MAX_SKB_FRAGS + 4);
3162
3163         return NETDEV_TX_OK;
3164 }
3165
3166 static int igb_xmit_frame_adv(struct sk_buff *skb, struct net_device *netdev)
3167 {
3168         struct igb_adapter *adapter = netdev_priv(netdev);
3169         struct igb_ring *tx_ring;
3170
3171         int r_idx = 0;
3172         r_idx = skb->queue_mapping & (IGB_MAX_TX_QUEUES - 1);
3173         tx_ring = adapter->multi_tx_table[r_idx];
3174
3175         /* This goes back to the question of how to logically map a tx queue
3176          * to a flow.  Right now, performance is impacted slightly negatively
3177          * if using multiple tx queues.  If the stack breaks away from a
3178          * single qdisc implementation, we can look at this again. */
3179         return (igb_xmit_frame_ring_adv(skb, netdev, tx_ring));
3180 }
3181
3182 /**
3183  * igb_tx_timeout - Respond to a Tx Hang
3184  * @netdev: network interface device structure
3185  **/
3186 static void igb_tx_timeout(struct net_device *netdev)
3187 {
3188         struct igb_adapter *adapter = netdev_priv(netdev);
3189         struct e1000_hw *hw = &adapter->hw;
3190
3191         /* Do the reset outside of interrupt context */
3192         adapter->tx_timeout_count++;
3193         schedule_work(&adapter->reset_task);
3194         wr32(E1000_EICS,
3195              (adapter->eims_enable_mask & ~adapter->eims_other));
3196 }
3197
3198 static void igb_reset_task(struct work_struct *work)
3199 {
3200         struct igb_adapter *adapter;
3201         adapter = container_of(work, struct igb_adapter, reset_task);
3202
3203         igb_reinit_locked(adapter);
3204 }
3205
3206 /**
3207  * igb_get_stats - Get System Network Statistics
3208  * @netdev: network interface device structure
3209  *
3210  * Returns the address of the device statistics structure.
3211  * The statistics are actually updated from the timer callback.
3212  **/
3213 static struct net_device_stats *
3214 igb_get_stats(struct net_device *netdev)
3215 {
3216         struct igb_adapter *adapter = netdev_priv(netdev);
3217
3218         /* only return the current stats */
3219         return &adapter->net_stats;
3220 }
3221
3222 /**
3223  * igb_change_mtu - Change the Maximum Transfer Unit
3224  * @netdev: network interface device structure
3225  * @new_mtu: new value for maximum frame size
3226  *
3227  * Returns 0 on success, negative on failure
3228  **/
3229 static int igb_change_mtu(struct net_device *netdev, int new_mtu)
3230 {
3231         struct igb_adapter *adapter = netdev_priv(netdev);
3232         int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
3233
3234         if ((max_frame < ETH_ZLEN + ETH_FCS_LEN) ||
3235             (max_frame > MAX_JUMBO_FRAME_SIZE)) {
3236                 dev_err(&adapter->pdev->dev, "Invalid MTU setting\n");
3237                 return -EINVAL;
3238         }
3239
3240 #define MAX_STD_JUMBO_FRAME_SIZE 9234
3241         if (max_frame > MAX_STD_JUMBO_FRAME_SIZE) {
3242                 dev_err(&adapter->pdev->dev, "MTU > 9216 not supported.\n");
3243                 return -EINVAL;
3244         }
3245
3246         while (test_and_set_bit(__IGB_RESETTING, &adapter->state))
3247                 msleep(1);
3248         /* igb_down has a dependency on max_frame_size */
3249         adapter->max_frame_size = max_frame;
3250         if (netif_running(netdev))
3251                 igb_down(adapter);
3252
3253         /* NOTE: netdev_alloc_skb reserves 16 bytes, and typically NET_IP_ALIGN
3254          * means we reserve 2 more, this pushes us to allocate from the next
3255          * larger slab size.
3256          * i.e. RXBUFFER_2048 --> size-4096 slab
3257          */
3258
3259         if (max_frame <= IGB_RXBUFFER_256)
3260                 adapter->rx_buffer_len = IGB_RXBUFFER_256;
3261         else if (max_frame <= IGB_RXBUFFER_512)
3262                 adapter->rx_buffer_len = IGB_RXBUFFER_512;
3263         else if (max_frame <= IGB_RXBUFFER_1024)
3264                 adapter->rx_buffer_len = IGB_RXBUFFER_1024;
3265         else if (max_frame <= IGB_RXBUFFER_2048)
3266                 adapter->rx_buffer_len = IGB_RXBUFFER_2048;
3267         else
3268 #if (PAGE_SIZE / 2) > IGB_RXBUFFER_16384
3269                 adapter->rx_buffer_len = IGB_RXBUFFER_16384;
3270 #else
3271                 adapter->rx_buffer_len = PAGE_SIZE / 2;
3272 #endif
3273         /* adjust allocation if LPE protects us, and we aren't using SBP */
3274         if ((max_frame == ETH_FRAME_LEN + ETH_FCS_LEN) ||
3275              (max_frame == MAXIMUM_ETHERNET_VLAN_SIZE))
3276                 adapter->rx_buffer_len = MAXIMUM_ETHERNET_VLAN_SIZE;
3277
3278         dev_info(&adapter->pdev->dev, "changing MTU from %d to %d\n",
3279                  netdev->mtu, new_mtu);
3280         netdev->mtu = new_mtu;
3281
3282         if (netif_running(netdev))
3283                 igb_up(adapter);
3284         else
3285                 igb_reset(adapter);
3286
3287         clear_bit(__IGB_RESETTING, &adapter->state);
3288
3289         return 0;
3290 }
3291
3292 /**
3293  * igb_update_stats - Update the board statistics counters
3294  * @adapter: board private structure
3295  **/
3296
3297 void igb_update_stats(struct igb_adapter *adapter)
3298 {
3299         struct e1000_hw *hw = &adapter->hw;
3300         struct pci_dev *pdev = adapter->pdev;
3301         u16 phy_tmp;
3302
3303 #define PHY_IDLE_ERROR_COUNT_MASK 0x00FF
3304
3305         /*
3306          * Prevent stats update while adapter is being reset, or if the pci
3307          * connection is down.
3308          */
3309         if (adapter->link_speed == 0)
3310                 return;
3311         if (pci_channel_offline(pdev))
3312                 return;
3313
3314         adapter->stats.crcerrs += rd32(E1000_CRCERRS);
3315         adapter->stats.gprc += rd32(E1000_GPRC);
3316         adapter->stats.gorc += rd32(E1000_GORCL);
3317         rd32(E1000_GORCH); /* clear GORCL */
3318         adapter->stats.bprc += rd32(E1000_BPRC);
3319         adapter->stats.mprc += rd32(E1000_MPRC);
3320         adapter->stats.roc += rd32(E1000_ROC);
3321
3322         adapter->stats.prc64 += rd32(E1000_PRC64);
3323         adapter->stats.prc127 += rd32(E1000_PRC127);
3324         adapter->stats.prc255 += rd32(E1000_PRC255);
3325         adapter->stats.prc511 += rd32(E1000_PRC511);
3326         adapter->stats.prc1023 += rd32(E1000_PRC1023);
3327         adapter->stats.prc1522 += rd32(E1000_PRC1522);
3328         adapter->stats.symerrs += rd32(E1000_SYMERRS);
3329         adapter->stats.sec += rd32(E1000_SEC);
3330
3331         adapter->stats.mpc += rd32(E1000_MPC);
3332         adapter->stats.scc += rd32(E1000_SCC);
3333         adapter->stats.ecol += rd32(E1000_ECOL);
3334         adapter->stats.mcc += rd32(E1000_MCC);
3335         adapter->stats.latecol += rd32(E1000_LATECOL);
3336         adapter->stats.dc += rd32(E1000_DC);
3337         adapter->stats.rlec += rd32(E1000_RLEC);
3338         adapter->stats.xonrxc += rd32(E1000_XONRXC);
3339         adapter->stats.xontxc += rd32(E1000_XONTXC);
3340         adapter->stats.xoffrxc += rd32(E1000_XOFFRXC);
3341         adapter->stats.xofftxc += rd32(E1000_XOFFTXC);
3342         adapter->stats.fcruc += rd32(E1000_FCRUC);
3343         adapter->stats.gptc += rd32(E1000_GPTC);
3344         adapter->stats.gotc += rd32(E1000_GOTCL);
3345         rd32(E1000_GOTCH); /* clear GOTCL */
3346         adapter->stats.rnbc += rd32(E1000_RNBC);
3347         adapter->stats.ruc += rd32(E1000_RUC);
3348         adapter->stats.rfc += rd32(E1000_RFC);
3349         adapter->stats.rjc += rd32(E1000_RJC);
3350         adapter->stats.tor += rd32(E1000_TORH);
3351         adapter->stats.tot += rd32(E1000_TOTH);
3352         adapter->stats.tpr += rd32(E1000_TPR);
3353
3354         adapter->stats.ptc64 += rd32(E1000_PTC64);
3355         adapter->stats.ptc127 += rd32(E1000_PTC127);
3356         adapter->stats.ptc255 += rd32(E1000_PTC255);
3357         adapter->stats.ptc511 += rd32(E1000_PTC511);
3358         adapter->stats.ptc1023 += rd32(E1000_PTC1023);
3359         adapter->stats.ptc1522 += rd32(E1000_PTC1522);
3360
3361         adapter->stats.mptc += rd32(E1000_MPTC);
3362         adapter->stats.bptc += rd32(E1000_BPTC);
3363
3364         /* used for adaptive IFS */
3365
3366         hw->mac.tx_packet_delta = rd32(E1000_TPT);
3367         adapter->stats.tpt += hw->mac.tx_packet_delta;
3368         hw->mac.collision_delta = rd32(E1000_COLC);
3369         adapter->stats.colc += hw->mac.collision_delta;
3370
3371         adapter->stats.algnerrc += rd32(E1000_ALGNERRC);
3372         adapter->stats.rxerrc += rd32(E1000_RXERRC);
3373         adapter->stats.tncrs += rd32(E1000_TNCRS);
3374         adapter->stats.tsctc += rd32(E1000_TSCTC);
3375         adapter->stats.tsctfc += rd32(E1000_TSCTFC);
3376
3377         adapter->stats.iac += rd32(E1000_IAC);
3378         adapter->stats.icrxoc += rd32(E1000_ICRXOC);
3379         adapter->stats.icrxptc += rd32(E1000_ICRXPTC);
3380         adapter->stats.icrxatc += rd32(E1000_ICRXATC);
3381         adapter->stats.ictxptc += rd32(E1000_ICTXPTC);
3382         adapter->stats.ictxatc += rd32(E1000_ICTXATC);
3383         adapter->stats.ictxqec += rd32(E1000_ICTXQEC);
3384         adapter->stats.ictxqmtc += rd32(E1000_ICTXQMTC);
3385         adapter->stats.icrxdmtc += rd32(E1000_ICRXDMTC);
3386
3387         /* Fill out the OS statistics structure */
3388         adapter->net_stats.multicast = adapter->stats.mprc;
3389         adapter->net_stats.collisions = adapter->stats.colc;
3390
3391         /* Rx Errors */
3392
3393         /* RLEC on some newer hardware can be incorrect so build
3394         * our own version based on RUC and ROC */
3395         adapter->net_stats.rx_errors = adapter->stats.rxerrc +
3396                 adapter->stats.crcerrs + adapter->stats.algnerrc +
3397                 adapter->stats.ruc + adapter->stats.roc +
3398                 adapter->stats.cexterr;
3399         adapter->net_stats.rx_length_errors = adapter->stats.ruc +
3400                                               adapter->stats.roc;
3401         adapter->net_stats.rx_crc_errors = adapter->stats.crcerrs;
3402         adapter->net_stats.rx_frame_errors = adapter->stats.algnerrc;
3403         adapter->net_stats.rx_missed_errors = adapter->stats.mpc;
3404
3405         /* Tx Errors */
3406         adapter->net_stats.tx_errors = adapter->stats.ecol +
3407                                        adapter->stats.latecol;
3408         adapter->net_stats.tx_aborted_errors = adapter->stats.ecol;
3409         adapter->net_stats.tx_window_errors = adapter->stats.latecol;
3410         adapter->net_stats.tx_carrier_errors = adapter->stats.tncrs;
3411
3412         /* Tx Dropped needs to be maintained elsewhere */
3413
3414         /* Phy Stats */
3415         if (hw->phy.media_type == e1000_media_type_copper) {
3416                 if ((adapter->link_speed == SPEED_1000) &&
3417                    (!igb_read_phy_reg(hw, PHY_1000T_STATUS,
3418                                               &phy_tmp))) {
3419                         phy_tmp &= PHY_IDLE_ERROR_COUNT_MASK;
3420                         adapter->phy_stats.idle_errors += phy_tmp;
3421                 }
3422         }
3423
3424         /* Management Stats */
3425         adapter->stats.mgptc += rd32(E1000_MGTPTC);
3426         adapter->stats.mgprc += rd32(E1000_MGTPRC);
3427         adapter->stats.mgpdc += rd32(E1000_MGTPDC);
3428 }
3429
3430
3431 static irqreturn_t igb_msix_other(int irq, void *data)
3432 {
3433         struct net_device *netdev = data;
3434         struct igb_adapter *adapter = netdev_priv(netdev);
3435         struct e1000_hw *hw = &adapter->hw;
3436         u32 icr = rd32(E1000_ICR);
3437
3438         /* reading ICR causes bit 31 of EICR to be cleared */
3439
3440         if(icr & E1000_ICR_DOUTSYNC) {
3441                 /* HW is reporting DMA is out of sync */
3442                 adapter->stats.doosync++;
3443         }
3444         if (!(icr & E1000_ICR_LSC))
3445                 goto no_link_interrupt;
3446         hw->mac.get_link_status = 1;
3447         /* guard against interrupt when we're going down */
3448         if (!test_bit(__IGB_DOWN, &adapter->state))
3449                 mod_timer(&adapter->watchdog_timer, jiffies + 1);
3450
3451 no_link_interrupt:
3452         wr32(E1000_IMS, E1000_IMS_LSC | E1000_IMS_DOUTSYNC);
3453         wr32(E1000_EIMS, adapter->eims_other);
3454
3455         return IRQ_HANDLED;
3456 }
3457
3458 static irqreturn_t igb_msix_tx(int irq, void *data)
3459 {
3460         struct igb_ring *tx_ring = data;
3461         struct igb_adapter *adapter = tx_ring->adapter;
3462         struct e1000_hw *hw = &adapter->hw;
3463
3464 #ifdef CONFIG_IGB_DCA
3465         if (adapter->flags & IGB_FLAG_DCA_ENABLED)
3466                 igb_update_tx_dca(tx_ring);
3467 #endif
3468         tx_ring->total_bytes = 0;
3469         tx_ring->total_packets = 0;
3470
3471         /* auto mask will automatically reenable the interrupt when we write
3472          * EICS */
3473         if (!igb_clean_tx_irq(tx_ring))
3474                 /* Ring was not completely cleaned, so fire another interrupt */
3475                 wr32(E1000_EICS, tx_ring->eims_value);
3476         else
3477                 wr32(E1000_EIMS, tx_ring->eims_value);
3478
3479         return IRQ_HANDLED;
3480 }
3481
3482 static void igb_write_itr(struct igb_ring *ring)
3483 {
3484         struct e1000_hw *hw = &ring->adapter->hw;
3485         if ((ring->adapter->itr_setting & 3) && ring->set_itr) {
3486                 switch (hw->mac.type) {
3487                 case e1000_82576:
3488                         wr32(ring->itr_register,
3489                              ring->itr_val |
3490                              0x80000000);
3491                         break;
3492                 default:
3493                         wr32(ring->itr_register,
3494                              ring->itr_val |
3495                              (ring->itr_val << 16));
3496                         break;
3497                 }
3498                 ring->set_itr = 0;
3499         }
3500 }
3501
3502 static irqreturn_t igb_msix_rx(int irq, void *data)
3503 {
3504         struct igb_ring *rx_ring = data;
3505
3506         /* Write the ITR value calculated at the end of the
3507          * previous interrupt.
3508          */
3509
3510         igb_write_itr(rx_ring);
3511
3512         if (napi_schedule_prep(&rx_ring->napi))
3513                 __napi_schedule(&rx_ring->napi);
3514
3515 #ifdef CONFIG_IGB_DCA
3516         if (rx_ring->adapter->flags & IGB_FLAG_DCA_ENABLED)
3517                 igb_update_rx_dca(rx_ring);
3518 #endif
3519                 return IRQ_HANDLED;
3520 }
3521
3522 #ifdef CONFIG_IGB_DCA
3523 static void igb_update_rx_dca(struct igb_ring *rx_ring)
3524 {
3525         u32 dca_rxctrl;
3526         struct igb_adapter *adapter = rx_ring->adapter;
3527         struct e1000_hw *hw = &adapter->hw;
3528         int cpu = get_cpu();
3529         int q = rx_ring->reg_idx;
3530
3531         if (rx_ring->cpu != cpu) {
3532                 dca_rxctrl = rd32(E1000_DCA_RXCTRL(q));
3533                 if (hw->mac.type == e1000_82576) {
3534                         dca_rxctrl &= ~E1000_DCA_RXCTRL_CPUID_MASK_82576;
3535                         dca_rxctrl |= dca_get_tag(cpu) <<
3536                                       E1000_DCA_RXCTRL_CPUID_SHIFT;
3537                 } else {
3538                         dca_rxctrl &= ~E1000_DCA_RXCTRL_CPUID_MASK;
3539                         dca_rxctrl |= dca_get_tag(cpu);
3540                 }
3541                 dca_rxctrl |= E1000_DCA_RXCTRL_DESC_DCA_EN;
3542                 dca_rxctrl |= E1000_DCA_RXCTRL_HEAD_DCA_EN;
3543                 dca_rxctrl |= E1000_DCA_RXCTRL_DATA_DCA_EN;
3544                 wr32(E1000_DCA_RXCTRL(q), dca_rxctrl);
3545                 rx_ring->cpu = cpu;
3546         }
3547         put_cpu();
3548 }
3549
3550 static void igb_update_tx_dca(struct igb_ring *tx_ring)
3551 {
3552         u32 dca_txctrl;
3553         struct igb_adapter *adapter = tx_ring->adapter;
3554         struct e1000_hw *hw = &adapter->hw;
3555         int cpu = get_cpu();
3556         int q = tx_ring->reg_idx;
3557
3558         if (tx_ring->cpu != cpu) {
3559                 dca_txctrl = rd32(E1000_DCA_TXCTRL(q));
3560                 if (hw->mac.type == e1000_82576) {
3561                         dca_txctrl &= ~E1000_DCA_TXCTRL_CPUID_MASK_82576;
3562                         dca_txctrl |= dca_get_tag(cpu) <<
3563                                       E1000_DCA_TXCTRL_CPUID_SHIFT;
3564                 } else {
3565                         dca_txctrl &= ~E1000_DCA_TXCTRL_CPUID_MASK;
3566                         dca_txctrl |= dca_get_tag(cpu);
3567                 }
3568                 dca_txctrl |= E1000_DCA_TXCTRL_DESC_DCA_EN;
3569                 wr32(E1000_DCA_TXCTRL(q), dca_txctrl);
3570                 tx_ring->cpu = cpu;
3571         }
3572         put_cpu();
3573 }
3574
3575 static void igb_setup_dca(struct igb_adapter *adapter)
3576 {
3577         int i;
3578
3579         if (!(adapter->flags & IGB_FLAG_DCA_ENABLED))
3580                 return;
3581
3582         for (i = 0; i < adapter->num_tx_queues; i++) {
3583                 adapter->tx_ring[i].cpu = -1;
3584                 igb_update_tx_dca(&adapter->tx_ring[i]);
3585         }
3586         for (i = 0; i < adapter->num_rx_queues; i++) {
3587                 adapter->rx_ring[i].cpu = -1;
3588                 igb_update_rx_dca(&adapter->rx_ring[i]);
3589         }
3590 }
3591
3592 static int __igb_notify_dca(struct device *dev, void *data)
3593 {
3594         struct net_device *netdev = dev_get_drvdata(dev);
3595         struct igb_adapter *adapter = netdev_priv(netdev);
3596         struct e1000_hw *hw = &adapter->hw;
3597         unsigned long event = *(unsigned long *)data;
3598
3599         switch (event) {
3600         case DCA_PROVIDER_ADD:
3601                 /* if already enabled, don't do it again */
3602                 if (adapter->flags & IGB_FLAG_DCA_ENABLED)
3603                         break;
3604                 /* Always use CB2 mode, difference is masked
3605                  * in the CB driver. */
3606                 wr32(E1000_DCA_CTRL, 2);
3607                 if (dca_add_requester(dev) == 0) {
3608                         adapter->flags |= IGB_FLAG_DCA_ENABLED;
3609                         dev_info(&adapter->pdev->dev, "DCA enabled\n");
3610                         igb_setup_dca(adapter);
3611                         break;
3612                 }
3613                 /* Fall Through since DCA is disabled. */
3614         case DCA_PROVIDER_REMOVE:
3615                 if (adapter->flags & IGB_FLAG_DCA_ENABLED) {
3616                         /* without this a class_device is left
3617                          * hanging around in the sysfs model */
3618                         dca_remove_requester(dev);
3619                         dev_info(&adapter->pdev->dev, "DCA disabled\n");
3620                         adapter->flags &= ~IGB_FLAG_DCA_ENABLED;
3621                         wr32(E1000_DCA_CTRL, 1);
3622                 }
3623                 break;
3624         }
3625
3626         return 0;
3627 }
3628
3629 static int igb_notify_dca(struct notifier_block *nb, unsigned long event,
3630                           void *p)
3631 {
3632         int ret_val;
3633
3634         ret_val = driver_for_each_device(&igb_driver.driver, NULL, &event,
3635                                          __igb_notify_dca);
3636
3637         return ret_val ? NOTIFY_BAD : NOTIFY_DONE;
3638 }
3639 #endif /* CONFIG_IGB_DCA */
3640
3641 /**
3642  * igb_intr_msi - Interrupt Handler
3643  * @irq: interrupt number
3644  * @data: pointer to a network interface device structure
3645  **/
3646 static irqreturn_t igb_intr_msi(int irq, void *data)
3647 {
3648         struct net_device *netdev = data;
3649         struct igb_adapter *adapter = netdev_priv(netdev);
3650         struct e1000_hw *hw = &adapter->hw;
3651         /* read ICR disables interrupts using IAM */
3652         u32 icr = rd32(E1000_ICR);
3653
3654         igb_write_itr(adapter->rx_ring);
3655
3656         if(icr & E1000_ICR_DOUTSYNC) {
3657                 /* HW is reporting DMA is out of sync */
3658                 adapter->stats.doosync++;
3659         }
3660
3661         if (icr & (E1000_ICR_RXSEQ | E1000_ICR_LSC)) {
3662                 hw->mac.get_link_status = 1;
3663                 if (!test_bit(__IGB_DOWN, &adapter->state))
3664                         mod_timer(&adapter->watchdog_timer, jiffies + 1);
3665         }
3666
3667         napi_schedule(&adapter->rx_ring[0].napi);
3668
3669         return IRQ_HANDLED;
3670 }
3671
3672 /**
3673  * igb_intr - Legacy Interrupt Handler
3674  * @irq: interrupt number
3675  * @data: pointer to a network interface device structure
3676  **/
3677 static irqreturn_t igb_intr(int irq, void *data)
3678 {
3679         struct net_device *netdev = data;
3680         struct igb_adapter *adapter = netdev_priv(netdev);
3681         struct e1000_hw *hw = &adapter->hw;
3682         /* Interrupt Auto-Mask...upon reading ICR, interrupts are masked.  No
3683          * need for the IMC write */
3684         u32 icr = rd32(E1000_ICR);
3685         if (!icr)
3686                 return IRQ_NONE;  /* Not our interrupt */
3687
3688         igb_write_itr(adapter->rx_ring);
3689
3690         /* IMS will not auto-mask if INT_ASSERTED is not set, and if it is
3691          * not set, then the adapter didn't send an interrupt */
3692         if (!(icr & E1000_ICR_INT_ASSERTED))
3693                 return IRQ_NONE;
3694
3695         if(icr & E1000_ICR_DOUTSYNC) {
3696                 /* HW is reporting DMA is out of sync */
3697                 adapter->stats.doosync++;
3698         }
3699
3700         if (icr & (E1000_ICR_RXSEQ | E1000_ICR_LSC)) {
3701                 hw->mac.get_link_status = 1;
3702                 /* guard against interrupt when we're going down */
3703                 if (!test_bit(__IGB_DOWN, &adapter->state))
3704                         mod_timer(&adapter->watchdog_timer, jiffies + 1);
3705         }
3706
3707         napi_schedule(&adapter->rx_ring[0].napi);
3708
3709         return IRQ_HANDLED;
3710 }
3711
3712 /**
3713  * igb_poll - NAPI Rx polling callback
3714  * @napi: napi polling structure
3715  * @budget: count of how many packets we should handle
3716  **/
3717 static int igb_poll(struct napi_struct *napi, int budget)
3718 {
3719         struct igb_ring *rx_ring = container_of(napi, struct igb_ring, napi);
3720         struct igb_adapter *adapter = rx_ring->adapter;
3721         struct net_device *netdev = adapter->netdev;
3722         int tx_clean_complete, work_done = 0;
3723
3724         /* this poll routine only supports one tx and one rx queue */
3725 #ifdef CONFIG_IGB_DCA
3726         if (adapter->flags & IGB_FLAG_DCA_ENABLED)
3727                 igb_update_tx_dca(&adapter->tx_ring[0]);
3728 #endif
3729         tx_clean_complete = igb_clean_tx_irq(&adapter->tx_ring[0]);
3730
3731 #ifdef CONFIG_IGB_DCA
3732         if (adapter->flags & IGB_FLAG_DCA_ENABLED)
3733                 igb_update_rx_dca(&adapter->rx_ring[0]);
3734 #endif
3735         igb_clean_rx_irq_adv(&adapter->rx_ring[0], &work_done, budget);
3736
3737         /* If no Tx and not enough Rx work done, exit the polling mode */
3738         if ((tx_clean_complete && (work_done < budget)) ||
3739             !netif_running(netdev)) {
3740                 if (adapter->itr_setting & 3)
3741                         igb_set_itr(adapter);
3742                 napi_complete(napi);
3743                 if (!test_bit(__IGB_DOWN, &adapter->state))
3744                         igb_irq_enable(adapter);
3745                 return 0;
3746         }
3747
3748         return 1;
3749 }
3750
3751 static int igb_clean_rx_ring_msix(struct napi_struct *napi, int budget)
3752 {
3753         struct igb_ring *rx_ring = container_of(napi, struct igb_ring, napi);
3754         struct igb_adapter *adapter = rx_ring->adapter;
3755         struct e1000_hw *hw = &adapter->hw;
3756         struct net_device *netdev = adapter->netdev;
3757         int work_done = 0;
3758
3759 #ifdef CONFIG_IGB_DCA
3760         if (adapter->flags & IGB_FLAG_DCA_ENABLED)
3761                 igb_update_rx_dca(rx_ring);
3762 #endif
3763         igb_clean_rx_irq_adv(rx_ring, &work_done, budget);
3764
3765
3766         /* If not enough Rx work done, exit the polling mode */
3767         if ((work_done == 0) || !netif_running(netdev)) {
3768                 napi_complete(napi);
3769
3770                 if (adapter->itr_setting & 3) {
3771                         if (adapter->num_rx_queues == 1)
3772                                 igb_set_itr(adapter);
3773                         else
3774                                 igb_update_ring_itr(rx_ring);
3775                 }
3776
3777                 if (!test_bit(__IGB_DOWN, &adapter->state))
3778                         wr32(E1000_EIMS, rx_ring->eims_value);
3779
3780                 return 0;
3781         }
3782
3783         return 1;
3784 }
3785
3786 /**
3787  * igb_hwtstamp - utility function which checks for TX time stamp
3788  * @adapter: board private structure
3789  * @skb: packet that was just sent
3790  *
3791  * If we were asked to do hardware stamping and such a time stamp is
3792  * available, then it must have been for this skb here because we only
3793  * allow only one such packet into the queue.
3794  */
3795 static void igb_tx_hwtstamp(struct igb_adapter *adapter, struct sk_buff *skb)
3796 {
3797         union skb_shared_tx *shtx = skb_tx(skb);
3798         struct e1000_hw *hw = &adapter->hw;
3799
3800         if (unlikely(shtx->hardware)) {
3801                 u32 valid = rd32(E1000_TSYNCTXCTL) & E1000_TSYNCTXCTL_VALID;
3802                 if (valid) {
3803                         u64 regval = rd32(E1000_TXSTMPL);
3804                         u64 ns;
3805                         struct skb_shared_hwtstamps shhwtstamps;
3806
3807                         memset(&shhwtstamps, 0, sizeof(shhwtstamps));
3808                         regval |= (u64)rd32(E1000_TXSTMPH) << 32;
3809                         ns = timecounter_cyc2time(&adapter->clock,
3810                                                   regval);
3811                         timecompare_update(&adapter->compare, ns);
3812                         shhwtstamps.hwtstamp = ns_to_ktime(ns);
3813                         shhwtstamps.syststamp =
3814                                 timecompare_transform(&adapter->compare, ns);
3815                         skb_tstamp_tx(skb, &shhwtstamps);
3816                 }
3817
3818                 /* delayed orphaning: skb_tstamp_tx() needs the socket */
3819                 skb_orphan(skb);
3820         }
3821 }
3822
3823 /**
3824  * igb_clean_tx_irq - Reclaim resources after transmit completes
3825  * @adapter: board private structure
3826  * returns true if ring is completely cleaned
3827  **/
3828 static bool igb_clean_tx_irq(struct igb_ring *tx_ring)
3829 {
3830         struct igb_adapter *adapter = tx_ring->adapter;
3831         struct net_device *netdev = adapter->netdev;
3832         struct e1000_hw *hw = &adapter->hw;
3833         struct igb_buffer *buffer_info;
3834         struct sk_buff *skb;
3835         union e1000_adv_tx_desc *tx_desc, *eop_desc;
3836         unsigned int total_bytes = 0, total_packets = 0;
3837         unsigned int i, eop, count = 0;
3838         bool cleaned = false;
3839
3840         i = tx_ring->next_to_clean;
3841         eop = tx_ring->buffer_info[i].next_to_watch;
3842         eop_desc = E1000_TX_DESC_ADV(*tx_ring, eop);
3843
3844         while ((eop_desc->wb.status & cpu_to_le32(E1000_TXD_STAT_DD)) &&
3845                (count < tx_ring->count)) {
3846                 for (cleaned = false; !cleaned; count++) {
3847                         tx_desc = E1000_TX_DESC_ADV(*tx_ring, i);
3848                         buffer_info = &tx_ring->buffer_info[i];
3849                         cleaned = (i == eop);
3850                         skb = buffer_info->skb;
3851
3852                         if (skb) {
3853                                 unsigned int segs, bytecount;
3854                                 /* gso_segs is currently only valid for tcp */
3855                                 segs = skb_shinfo(skb)->gso_segs ?: 1;
3856                                 /* multiply data chunks by size of headers */
3857                                 bytecount = ((segs - 1) * skb_headlen(skb)) +
3858                                             skb->len;
3859                                 total_packets += segs;
3860                                 total_bytes += bytecount;
3861
3862                                 igb_tx_hwtstamp(adapter, skb);
3863                         }
3864
3865                         igb_unmap_and_free_tx_resource(adapter, buffer_info);
3866                         tx_desc->wb.status = 0;
3867
3868                         i++;
3869                         if (i == tx_ring->count)
3870                                 i = 0;
3871                 }
3872
3873                 eop = tx_ring->buffer_info[i].next_to_watch;
3874                 eop_desc = E1000_TX_DESC_ADV(*tx_ring, eop);
3875         }
3876
3877         tx_ring->next_to_clean = i;
3878
3879         if (unlikely(count &&
3880                      netif_carrier_ok(netdev) &&
3881                      IGB_DESC_UNUSED(tx_ring) >= IGB_TX_QUEUE_WAKE)) {
3882                 /* Make sure that anybody stopping the queue after this
3883                  * sees the new next_to_clean.
3884                  */
3885                 smp_mb();
3886                 if (__netif_subqueue_stopped(netdev, tx_ring->queue_index) &&
3887                     !(test_bit(__IGB_DOWN, &adapter->state))) {
3888                         netif_wake_subqueue(netdev, tx_ring->queue_index);
3889                         ++adapter->restart_queue;
3890                 }
3891         }
3892
3893         if (tx_ring->detect_tx_hung) {
3894                 /* Detect a transmit hang in hardware, this serializes the
3895                  * check with the clearing of time_stamp and movement of i */
3896                 tx_ring->detect_tx_hung = false;
3897                 if (tx_ring->buffer_info[i].time_stamp &&
3898                     time_after(jiffies, tx_ring->buffer_info[i].time_stamp +
3899                                (adapter->tx_timeout_factor * HZ))
3900                     && !(rd32(E1000_STATUS) &
3901                          E1000_STATUS_TXOFF)) {
3902
3903                         /* detected Tx unit hang */
3904                         dev_err(&adapter->pdev->dev,
3905                                 "Detected Tx Unit Hang\n"
3906                                 "  Tx Queue             <%d>\n"
3907                                 "  TDH                  <%x>\n"
3908                                 "  TDT                  <%x>\n"
3909                                 "  next_to_use          <%x>\n"
3910                                 "  next_to_clean        <%x>\n"
3911                                 "buffer_info[next_to_clean]\n"
3912                                 "  time_stamp           <%lx>\n"
3913                                 "  next_to_watch        <%x>\n"
3914                                 "  jiffies              <%lx>\n"
3915                                 "  desc.status          <%x>\n",
3916                                 tx_ring->queue_index,
3917                                 readl(adapter->hw.hw_addr + tx_ring->head),
3918                                 readl(adapter->hw.hw_addr + tx_ring->tail),
3919                                 tx_ring->next_to_use,
3920                                 tx_ring->next_to_clean,
3921                                 tx_ring->buffer_info[i].time_stamp,
3922                                 eop,
3923                                 jiffies,
3924                                 eop_desc->wb.status);
3925                         netif_stop_subqueue(netdev, tx_ring->queue_index);
3926                 }
3927         }
3928         tx_ring->total_bytes += total_bytes;
3929         tx_ring->total_packets += total_packets;
3930         tx_ring->tx_stats.bytes += total_bytes;
3931         tx_ring->tx_stats.packets += total_packets;
3932         adapter->net_stats.tx_bytes += total_bytes;
3933         adapter->net_stats.tx_packets += total_packets;
3934         return (count < tx_ring->count);
3935 }
3936
3937 /**
3938  * igb_receive_skb - helper function to handle rx indications
3939  * @ring: pointer to receive ring receving this packet
3940  * @status: descriptor status field as written by hardware
3941  * @vlan: descriptor vlan field as written by hardware (no le/be conversion)
3942  * @skb: pointer to sk_buff to be indicated to stack
3943  **/
3944 static void igb_receive_skb(struct igb_ring *ring, u8 status,
3945                             union e1000_adv_rx_desc * rx_desc,
3946                             struct sk_buff *skb)
3947 {
3948         struct igb_adapter * adapter = ring->adapter;
3949         bool vlan_extracted = (adapter->vlgrp && (status & E1000_RXD_STAT_VP));
3950
3951         skb_record_rx_queue(skb, ring->queue_index);
3952         if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
3953                 if (vlan_extracted)
3954                         vlan_gro_receive(&ring->napi, adapter->vlgrp,
3955                                          le16_to_cpu(rx_desc->wb.upper.vlan),
3956                                          skb);
3957                 else
3958                         napi_gro_receive(&ring->napi, skb);
3959         } else {
3960                 if (vlan_extracted)
3961                         vlan_hwaccel_receive_skb(skb, adapter->vlgrp,
3962                                           le16_to_cpu(rx_desc->wb.upper.vlan));
3963                 else
3964                         netif_receive_skb(skb);
3965         }
3966 }
3967
3968
3969 static inline void igb_rx_checksum_adv(struct igb_adapter *adapter,
3970                                        u32 status_err, struct sk_buff *skb)
3971 {
3972         skb->ip_summed = CHECKSUM_NONE;
3973
3974         /* Ignore Checksum bit is set or checksum is disabled through ethtool */
3975         if ((status_err & E1000_RXD_STAT_IXSM) || !adapter->rx_csum)
3976                 return;
3977         /* TCP/UDP checksum error bit is set */
3978         if (status_err &
3979             (E1000_RXDEXT_STATERR_TCPE | E1000_RXDEXT_STATERR_IPE)) {
3980                 /* let the stack verify checksum errors */
3981                 adapter->hw_csum_err++;
3982                 return;
3983         }
3984         /* It must be a TCP or UDP packet with a valid checksum */
3985         if (status_err & (E1000_RXD_STAT_TCPCS | E1000_RXD_STAT_UDPCS))
3986                 skb->ip_summed = CHECKSUM_UNNECESSARY;
3987
3988         adapter->hw_csum_good++;
3989 }
3990
3991 static bool igb_clean_rx_irq_adv(struct igb_ring *rx_ring,
3992                                  int *work_done, int budget)
3993 {
3994         struct igb_adapter *adapter = rx_ring->adapter;
3995         struct net_device *netdev = adapter->netdev;
3996         struct e1000_hw *hw = &adapter->hw;
3997         struct pci_dev *pdev = adapter->pdev;
3998         union e1000_adv_rx_desc *rx_desc , *next_rxd;
3999         struct igb_buffer *buffer_info , *next_buffer;
4000         struct sk_buff *skb;
4001         unsigned int i;
4002         u32 length, hlen, staterr;
4003         bool cleaned = false;
4004         int cleaned_count = 0;
4005         unsigned int total_bytes = 0, total_packets = 0;
4006
4007         i = rx_ring->next_to_clean;
4008         buffer_info = &rx_ring->buffer_info[i];
4009         rx_desc = E1000_RX_DESC_ADV(*rx_ring, i);
4010         staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
4011
4012         while (staterr & E1000_RXD_STAT_DD) {
4013                 if (*work_done >= budget)
4014                         break;
4015                 (*work_done)++;
4016
4017                 skb = buffer_info->skb;
4018                 prefetch(skb->data - NET_IP_ALIGN);
4019                 buffer_info->skb = NULL;
4020
4021                 i++;
4022                 if (i == rx_ring->count)
4023                         i = 0;
4024                 next_rxd = E1000_RX_DESC_ADV(*rx_ring, i);
4025                 prefetch(next_rxd);
4026                 next_buffer = &rx_ring->buffer_info[i];
4027
4028                 length = le16_to_cpu(rx_desc->wb.upper.length);
4029                 cleaned = true;
4030                 cleaned_count++;
4031
4032                 if (!adapter->rx_ps_hdr_size) {
4033                         pci_unmap_single(pdev, buffer_info->dma,
4034                                          adapter->rx_buffer_len +
4035                                            NET_IP_ALIGN,
4036                                          PCI_DMA_FROMDEVICE);
4037                         skb_put(skb, length);
4038                         goto send_up;
4039                 }
4040
4041                 /* HW will not DMA in data larger than the given buffer, even
4042                  * if it parses the (NFS, of course) header to be larger.  In
4043                  * that case, it fills the header buffer and spills the rest
4044                  * into the page.
4045                  */
4046                 hlen = (le16_to_cpu(rx_desc->wb.lower.lo_dword.hdr_info) &
4047                   E1000_RXDADV_HDRBUFLEN_MASK) >> E1000_RXDADV_HDRBUFLEN_SHIFT;
4048                 if (hlen > adapter->rx_ps_hdr_size)
4049                         hlen = adapter->rx_ps_hdr_size;
4050
4051                 if (!skb_shinfo(skb)->nr_frags) {
4052                         pci_unmap_single(pdev, buffer_info->dma,
4053                                          adapter->rx_ps_hdr_size +
4054                                            NET_IP_ALIGN,
4055                                          PCI_DMA_FROMDEVICE);
4056                         skb_put(skb, hlen);
4057                 }
4058
4059                 if (length) {
4060                         pci_unmap_page(pdev, buffer_info->page_dma,
4061                                        PAGE_SIZE / 2, PCI_DMA_FROMDEVICE);
4062                         buffer_info->page_dma = 0;
4063
4064                         skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags++,
4065                                                 buffer_info->page,
4066                                                 buffer_info->page_offset,
4067                                                 length);
4068
4069                         if ((adapter->rx_buffer_len > (PAGE_SIZE / 2)) ||
4070                             (page_count(buffer_info->page) != 1))
4071                                 buffer_info->page = NULL;
4072                         else
4073                                 get_page(buffer_info->page);
4074
4075                         skb->len += length;
4076                         skb->data_len += length;
4077
4078                         skb->truesize += length;
4079                 }
4080
4081                 if (!(staterr & E1000_RXD_STAT_EOP)) {
4082                         buffer_info->skb = next_buffer->skb;
4083                         buffer_info->dma = next_buffer->dma;
4084                         next_buffer->skb = skb;
4085                         next_buffer->dma = 0;
4086                         goto next_desc;
4087                 }
4088 send_up:
4089                 /*
4090                  * If this bit is set, then the RX registers contain
4091                  * the time stamp. No other packet will be time
4092                  * stamped until we read these registers, so read the
4093                  * registers to make them available again. Because
4094                  * only one packet can be time stamped at a time, we
4095                  * know that the register values must belong to this
4096                  * one here and therefore we don't need to compare
4097                  * any of the additional attributes stored for it.
4098                  *
4099                  * If nothing went wrong, then it should have a
4100                  * skb_shared_tx that we can turn into a
4101                  * skb_shared_hwtstamps.
4102                  *
4103                  * TODO: can time stamping be triggered (thus locking
4104                  * the registers) without the packet reaching this point
4105                  * here? In that case RX time stamping would get stuck.
4106                  *
4107                  * TODO: in "time stamp all packets" mode this bit is
4108                  * not set. Need a global flag for this mode and then
4109                  * always read the registers. Cannot be done without
4110                  * a race condition.
4111                  */
4112                 if (unlikely(staterr & E1000_RXD_STAT_TS)) {
4113                         u64 regval;
4114                         u64 ns;
4115                         struct skb_shared_hwtstamps *shhwtstamps =
4116                                 skb_hwtstamps(skb);
4117
4118                         WARN(!(rd32(E1000_TSYNCRXCTL) & E1000_TSYNCRXCTL_VALID),
4119                              "igb: no RX time stamp available for time stamped packet");
4120                         regval = rd32(E1000_RXSTMPL);
4121                         regval |= (u64)rd32(E1000_RXSTMPH) << 32;
4122                         ns = timecounter_cyc2time(&adapter->clock, regval);
4123                         timecompare_update(&adapter->compare, ns);
4124                         memset(shhwtstamps, 0, sizeof(*shhwtstamps));
4125                         shhwtstamps->hwtstamp = ns_to_ktime(ns);
4126                         shhwtstamps->syststamp =
4127                                 timecompare_transform(&adapter->compare, ns);
4128                 }
4129
4130                 if (staterr & E1000_RXDEXT_ERR_FRAME_ERR_MASK) {
4131                         dev_kfree_skb_irq(skb);
4132                         goto next_desc;
4133                 }
4134
4135                 total_bytes += skb->len;
4136                 total_packets++;
4137
4138                 igb_rx_checksum_adv(adapter, staterr, skb);
4139
4140                 skb->protocol = eth_type_trans(skb, netdev);
4141
4142                 igb_receive_skb(rx_ring, staterr, rx_desc, skb);
4143
4144 next_desc:
4145                 rx_desc->wb.upper.status_error = 0;
4146
4147                 /* return some buffers to hardware, one at a time is too slow */
4148                 if (cleaned_count >= IGB_RX_BUFFER_WRITE) {
4149                         igb_alloc_rx_buffers_adv(rx_ring, cleaned_count);
4150                         cleaned_count = 0;
4151                 }
4152
4153                 /* use prefetched values */
4154                 rx_desc = next_rxd;
4155                 buffer_info = next_buffer;
4156                 staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
4157         }
4158
4159         rx_ring->next_to_clean = i;
4160         cleaned_count = IGB_DESC_UNUSED(rx_ring);
4161
4162         if (cleaned_count)
4163                 igb_alloc_rx_buffers_adv(rx_ring, cleaned_count);
4164
4165         rx_ring->total_packets += total_packets;
4166         rx_ring->total_bytes += total_bytes;
4167         rx_ring->rx_stats.packets += total_packets;
4168         rx_ring->rx_stats.bytes += total_bytes;
4169         adapter->net_stats.rx_bytes += total_bytes;
4170         adapter->net_stats.rx_packets += total_packets;
4171         return cleaned;
4172 }
4173
4174
4175 /**
4176  * igb_alloc_rx_buffers_adv - Replace used receive buffers; packet split
4177  * @adapter: address of board private structure
4178  **/
4179 static void igb_alloc_rx_buffers_adv(struct igb_ring *rx_ring,
4180                                      int cleaned_count)
4181 {
4182         struct igb_adapter *adapter = rx_ring->adapter;
4183         struct net_device *netdev = adapter->netdev;
4184         struct pci_dev *pdev = adapter->pdev;
4185         union e1000_adv_rx_desc *rx_desc;
4186         struct igb_buffer *buffer_info;
4187         struct sk_buff *skb;
4188         unsigned int i;
4189         int bufsz;
4190
4191         i = rx_ring->next_to_use;
4192         buffer_info = &rx_ring->buffer_info[i];
4193
4194         if (adapter->rx_ps_hdr_size)
4195                 bufsz = adapter->rx_ps_hdr_size;
4196         else
4197                 bufsz = adapter->rx_buffer_len;
4198         bufsz += NET_IP_ALIGN;
4199
4200         while (cleaned_count--) {
4201                 rx_desc = E1000_RX_DESC_ADV(*rx_ring, i);
4202
4203                 if (adapter->rx_ps_hdr_size && !buffer_info->page_dma) {
4204                         if (!buffer_info->page) {
4205                                 buffer_info->page = alloc_page(GFP_ATOMIC);
4206                                 if (!buffer_info->page) {
4207                                         adapter->alloc_rx_buff_failed++;
4208                                         goto no_buffers;
4209                                 }
4210                                 buffer_info->page_offset = 0;
4211                         } else {
4212                                 buffer_info->page_offset ^= PAGE_SIZE / 2;
4213                         }
4214                         buffer_info->page_dma =
4215                                 pci_map_page(pdev, buffer_info->page,
4216                                              buffer_info->page_offset,
4217                                              PAGE_SIZE / 2,
4218                                              PCI_DMA_FROMDEVICE);
4219                 }
4220
4221                 if (!buffer_info->skb) {
4222                         skb = netdev_alloc_skb(netdev, bufsz);
4223                         if (!skb) {
4224                                 adapter->alloc_rx_buff_failed++;
4225                                 goto no_buffers;
4226                         }
4227
4228                         /* Make buffer alignment 2 beyond a 16 byte boundary
4229                          * this will result in a 16 byte aligned IP header after
4230                          * the 14 byte MAC header is removed
4231                          */
4232                         skb_reserve(skb, NET_IP_ALIGN);
4233
4234                         buffer_info->skb = skb;
4235                         buffer_info->dma = pci_map_single(pdev, skb->data,
4236                                                           bufsz,
4237                                                           PCI_DMA_FROMDEVICE);
4238                 }
4239                 /* Refresh the desc even if buffer_addrs didn't change because
4240                  * each write-back erases this info. */
4241                 if (adapter->rx_ps_hdr_size) {
4242                         rx_desc->read.pkt_addr =
4243                              cpu_to_le64(buffer_info->page_dma);
4244                         rx_desc->read.hdr_addr = cpu_to_le64(buffer_info->dma);
4245                 } else {
4246                         rx_desc->read.pkt_addr =
4247                              cpu_to_le64(buffer_info->dma);
4248                         rx_desc->read.hdr_addr = 0;
4249                 }
4250
4251                 i++;
4252                 if (i == rx_ring->count)
4253                         i = 0;
4254                 buffer_info = &rx_ring->buffer_info[i];
4255         }
4256
4257 no_buffers:
4258         if (rx_ring->next_to_use != i) {
4259                 rx_ring->next_to_use = i;
4260                 if (i == 0)
4261                         i = (rx_ring->count - 1);
4262                 else
4263                         i--;
4264
4265                 /* Force memory writes to complete before letting h/w
4266                  * know there are new descriptors to fetch.  (Only
4267                  * applicable for weak-ordered memory model archs,
4268                  * such as IA-64). */
4269                 wmb();
4270                 writel(i, adapter->hw.hw_addr + rx_ring->tail);
4271         }
4272 }
4273
4274 /**
4275  * igb_mii_ioctl -
4276  * @netdev:
4277  * @ifreq:
4278  * @cmd:
4279  **/
4280 static int igb_mii_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
4281 {
4282         struct igb_adapter *adapter = netdev_priv(netdev);
4283         struct mii_ioctl_data *data = if_mii(ifr);
4284
4285         if (adapter->hw.phy.media_type != e1000_media_type_copper)
4286                 return -EOPNOTSUPP;
4287
4288         switch (cmd) {
4289         case SIOCGMIIPHY:
4290                 data->phy_id = adapter->hw.phy.addr;
4291                 break;
4292         case SIOCGMIIREG:
4293                 if (!capable(CAP_NET_ADMIN))
4294                         return -EPERM;
4295                 if (igb_read_phy_reg(&adapter->hw, data->reg_num & 0x1F,
4296                                      &data->val_out))
4297                         return -EIO;
4298                 break;
4299         case SIOCSMIIREG:
4300         default:
4301                 return -EOPNOTSUPP;
4302         }
4303         return 0;
4304 }
4305
4306 /**
4307  * igb_hwtstamp_ioctl - control hardware time stamping
4308  * @netdev:
4309  * @ifreq:
4310  * @cmd:
4311  *
4312  * Outgoing time stamping can be enabled and disabled. Play nice and
4313  * disable it when requested, although it shouldn't case any overhead
4314  * when no packet needs it. At most one packet in the queue may be
4315  * marked for time stamping, otherwise it would be impossible to tell
4316  * for sure to which packet the hardware time stamp belongs.
4317  *
4318  * Incoming time stamping has to be configured via the hardware
4319  * filters. Not all combinations are supported, in particular event
4320  * type has to be specified. Matching the kind of event packet is
4321  * not supported, with the exception of "all V2 events regardless of
4322  * level 2 or 4".
4323  *
4324  **/
4325 static int igb_hwtstamp_ioctl(struct net_device *netdev,
4326                               struct ifreq *ifr, int cmd)
4327 {
4328         struct igb_adapter *adapter = netdev_priv(netdev);
4329         struct e1000_hw *hw = &adapter->hw;
4330         struct hwtstamp_config config;
4331         u32 tsync_tx_ctl_bit = E1000_TSYNCTXCTL_ENABLED;
4332         u32 tsync_rx_ctl_bit = E1000_TSYNCRXCTL_ENABLED;
4333         u32 tsync_rx_ctl_type = 0;
4334         u32 tsync_rx_cfg = 0;
4335         int is_l4 = 0;
4336         int is_l2 = 0;
4337         short port = 319; /* PTP */
4338         u32 regval;
4339
4340         if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
4341                 return -EFAULT;
4342
4343         /* reserved for future extensions */
4344         if (config.flags)
4345                 return -EINVAL;
4346
4347         switch (config.tx_type) {
4348         case HWTSTAMP_TX_OFF:
4349                 tsync_tx_ctl_bit = 0;
4350                 break;
4351         case HWTSTAMP_TX_ON:
4352                 tsync_tx_ctl_bit = E1000_TSYNCTXCTL_ENABLED;
4353                 break;
4354         default:
4355                 return -ERANGE;
4356         }
4357
4358         switch (config.rx_filter) {
4359         case HWTSTAMP_FILTER_NONE:
4360                 tsync_rx_ctl_bit = 0;
4361                 break;
4362         case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
4363         case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
4364         case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
4365         case HWTSTAMP_FILTER_ALL:
4366                 /*
4367                  * register TSYNCRXCFG must be set, therefore it is not
4368                  * possible to time stamp both Sync and Delay_Req messages
4369                  * => fall back to time stamping all packets
4370                  */
4371                 tsync_rx_ctl_type = E1000_TSYNCRXCTL_TYPE_ALL;
4372                 config.rx_filter = HWTSTAMP_FILTER_ALL;
4373                 break;
4374         case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
4375                 tsync_rx_ctl_type = E1000_TSYNCRXCTL_TYPE_L4_V1;
4376                 tsync_rx_cfg = E1000_TSYNCRXCFG_PTP_V1_SYNC_MESSAGE;
4377                 is_l4 = 1;
4378                 break;
4379         case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
4380                 tsync_rx_ctl_type = E1000_TSYNCRXCTL_TYPE_L4_V1;
4381                 tsync_rx_cfg = E1000_TSYNCRXCFG_PTP_V1_DELAY_REQ_MESSAGE;
4382                 is_l4 = 1;
4383                 break;
4384         case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
4385         case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
4386                 tsync_rx_ctl_type = E1000_TSYNCRXCTL_TYPE_L2_L4_V2;
4387                 tsync_rx_cfg = E1000_TSYNCRXCFG_PTP_V2_SYNC_MESSAGE;
4388                 is_l2 = 1;
4389                 is_l4 = 1;
4390                 config.rx_filter = HWTSTAMP_FILTER_SOME;
4391                 break;
4392         case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
4393         case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
4394                 tsync_rx_ctl_type = E1000_TSYNCRXCTL_TYPE_L2_L4_V2;
4395                 tsync_rx_cfg = E1000_TSYNCRXCFG_PTP_V2_DELAY_REQ_MESSAGE;
4396                 is_l2 = 1;
4397                 is_l4 = 1;
4398                 config.rx_filter = HWTSTAMP_FILTER_SOME;
4399                 break;
4400         case HWTSTAMP_FILTER_PTP_V2_EVENT:
4401         case HWTSTAMP_FILTER_PTP_V2_SYNC:
4402         case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
4403                 tsync_rx_ctl_type = E1000_TSYNCRXCTL_TYPE_EVENT_V2;
4404                 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
4405                 is_l2 = 1;
4406                 break;
4407         default:
4408                 return -ERANGE;
4409         }
4410
4411         /* enable/disable TX */
4412         regval = rd32(E1000_TSYNCTXCTL);
4413         regval = (regval & ~E1000_TSYNCTXCTL_ENABLED) | tsync_tx_ctl_bit;
4414         wr32(E1000_TSYNCTXCTL, regval);
4415
4416         /* enable/disable RX, define which PTP packets are time stamped */
4417         regval = rd32(E1000_TSYNCRXCTL);
4418         regval = (regval & ~E1000_TSYNCRXCTL_ENABLED) | tsync_rx_ctl_bit;
4419         regval = (regval & ~0xE) | tsync_rx_ctl_type;
4420         wr32(E1000_TSYNCRXCTL, regval);
4421         wr32(E1000_TSYNCRXCFG, tsync_rx_cfg);
4422
4423         /*
4424          * Ethertype Filter Queue Filter[0][15:0] = 0x88F7
4425          *                                          (Ethertype to filter on)
4426          * Ethertype Filter Queue Filter[0][26] = 0x1 (Enable filter)
4427          * Ethertype Filter Queue Filter[0][30] = 0x1 (Enable Timestamping)
4428          */
4429         wr32(E1000_ETQF0, is_l2 ? 0x440088f7 : 0);
4430
4431         /* L4 Queue Filter[0]: only filter by source and destination port */
4432         wr32(E1000_SPQF0, htons(port));
4433         wr32(E1000_IMIREXT(0), is_l4 ?
4434              ((1<<12) | (1<<19) /* bypass size and control flags */) : 0);
4435         wr32(E1000_IMIR(0), is_l4 ?
4436              (htons(port)
4437               | (0<<16) /* immediate interrupt disabled */
4438               | 0 /* (1<<17) bit cleared: do not bypass
4439                      destination port check */)
4440                 : 0);
4441         wr32(E1000_FTQF0, is_l4 ?
4442              (0x11 /* UDP */
4443               | (1<<15) /* VF not compared */
4444               | (1<<27) /* Enable Timestamping */
4445               | (7<<28) /* only source port filter enabled,
4446                            source/target address and protocol
4447                            masked */)
4448              : ((1<<15) | (15<<28) /* all mask bits set = filter not
4449                                       enabled */));
4450
4451         wrfl();
4452
4453         adapter->hwtstamp_config = config;
4454
4455         /* clear TX/RX time stamp registers, just to be sure */
4456         regval = rd32(E1000_TXSTMPH);
4457         regval = rd32(E1000_RXSTMPH);
4458
4459         return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
4460                 -EFAULT : 0;
4461 }
4462
4463 /**
4464  * igb_ioctl -
4465  * @netdev:
4466  * @ifreq:
4467  * @cmd:
4468  **/
4469 static int igb_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
4470 {
4471         switch (cmd) {
4472         case SIOCGMIIPHY:
4473         case SIOCGMIIREG:
4474         case SIOCSMIIREG:
4475                 return igb_mii_ioctl(netdev, ifr, cmd);
4476         case SIOCSHWTSTAMP:
4477                 return igb_hwtstamp_ioctl(netdev, ifr, cmd);
4478         default:
4479                 return -EOPNOTSUPP;
4480         }
4481 }
4482
4483 static void igb_vlan_rx_register(struct net_device *netdev,
4484                                  struct vlan_group *grp)
4485 {
4486         struct igb_adapter *adapter = netdev_priv(netdev);
4487         struct e1000_hw *hw = &adapter->hw;
4488         u32 ctrl, rctl;
4489
4490         igb_irq_disable(adapter);
4491         adapter->vlgrp = grp;
4492
4493         if (grp) {
4494                 /* enable VLAN tag insert/strip */
4495                 ctrl = rd32(E1000_CTRL);
4496                 ctrl |= E1000_CTRL_VME;
4497                 wr32(E1000_CTRL, ctrl);
4498
4499                 /* enable VLAN receive filtering */
4500                 rctl = rd32(E1000_RCTL);
4501                 rctl &= ~E1000_RCTL_CFIEN;
4502                 wr32(E1000_RCTL, rctl);
4503                 igb_update_mng_vlan(adapter);
4504                 wr32(E1000_RLPML,
4505                                 adapter->max_frame_size + VLAN_TAG_SIZE);
4506         } else {
4507                 /* disable VLAN tag insert/strip */
4508                 ctrl = rd32(E1000_CTRL);
4509                 ctrl &= ~E1000_CTRL_VME;
4510                 wr32(E1000_CTRL, ctrl);
4511
4512                 if (adapter->mng_vlan_id != (u16)IGB_MNG_VLAN_NONE) {
4513                         igb_vlan_rx_kill_vid(netdev, adapter->mng_vlan_id);
4514                         adapter->mng_vlan_id = IGB_MNG_VLAN_NONE;
4515                 }
4516                 wr32(E1000_RLPML,
4517                                 adapter->max_frame_size);
4518         }
4519
4520         if (!test_bit(__IGB_DOWN, &adapter->state))
4521                 igb_irq_enable(adapter);
4522 }
4523
4524 static void igb_vlan_rx_add_vid(struct net_device *netdev, u16 vid)
4525 {
4526         struct igb_adapter *adapter = netdev_priv(netdev);
4527         struct e1000_hw *hw = &adapter->hw;
4528         u32 vfta, index;
4529
4530         if ((hw->mng_cookie.status &
4531              E1000_MNG_DHCP_COOKIE_STATUS_VLAN) &&
4532             (vid == adapter->mng_vlan_id))
4533                 return;
4534         /* add VID to filter table */
4535         index = (vid >> 5) & 0x7F;
4536         vfta = array_rd32(E1000_VFTA, index);
4537         vfta |= (1 << (vid & 0x1F));
4538         igb_write_vfta(&adapter->hw, index, vfta);
4539 }
4540
4541 static void igb_vlan_rx_kill_vid(struct net_device *netdev, u16 vid)
4542 {
4543         struct igb_adapter *adapter = netdev_priv(netdev);
4544         struct e1000_hw *hw = &adapter->hw;
4545         u32 vfta, index;
4546
4547         igb_irq_disable(adapter);
4548         vlan_group_set_device(adapter->vlgrp, vid, NULL);
4549
4550         if (!test_bit(__IGB_DOWN, &adapter->state))
4551                 igb_irq_enable(adapter);
4552
4553         if ((adapter->hw.mng_cookie.status &
4554              E1000_MNG_DHCP_COOKIE_STATUS_VLAN) &&
4555             (vid == adapter->mng_vlan_id)) {
4556                 /* release control to f/w */
4557                 igb_release_hw_control(adapter);
4558                 return;
4559         }
4560
4561         /* remove VID from filter table */
4562         index = (vid >> 5) & 0x7F;
4563         vfta = array_rd32(E1000_VFTA, index);
4564         vfta &= ~(1 << (vid & 0x1F));
4565         igb_write_vfta(&adapter->hw, index, vfta);
4566 }
4567
4568 static void igb_restore_vlan(struct igb_adapter *adapter)
4569 {
4570         igb_vlan_rx_register(adapter->netdev, adapter->vlgrp);
4571
4572         if (adapter->vlgrp) {
4573                 u16 vid;
4574                 for (vid = 0; vid < VLAN_GROUP_ARRAY_LEN; vid++) {
4575                         if (!vlan_group_get_device(adapter->vlgrp, vid))
4576                                 continue;
4577                         igb_vlan_rx_add_vid(adapter->netdev, vid);
4578                 }
4579         }
4580 }
4581
4582 int igb_set_spd_dplx(struct igb_adapter *adapter, u16 spddplx)
4583 {
4584         struct e1000_mac_info *mac = &adapter->hw.mac;
4585
4586         mac->autoneg = 0;
4587
4588         /* Fiber NICs only allow 1000 gbps Full duplex */
4589         if ((adapter->hw.phy.media_type == e1000_media_type_fiber) &&
4590                 spddplx != (SPEED_1000 + DUPLEX_FULL)) {
4591                 dev_err(&adapter->pdev->dev,
4592                         "Unsupported Speed/Duplex configuration\n");
4593                 return -EINVAL;
4594         }
4595
4596         switch (spddplx) {
4597         case SPEED_10 + DUPLEX_HALF:
4598                 mac->forced_speed_duplex = ADVERTISE_10_HALF;
4599                 break;
4600         case SPEED_10 + DUPLEX_FULL:
4601                 mac->forced_speed_duplex = ADVERTISE_10_FULL;
4602                 break;
4603         case SPEED_100 + DUPLEX_HALF:
4604                 mac->forced_speed_duplex = ADVERTISE_100_HALF;
4605                 break;
4606         case SPEED_100 + DUPLEX_FULL:
4607                 mac->forced_speed_duplex = ADVERTISE_100_FULL;
4608                 break;
4609         case SPEED_1000 + DUPLEX_FULL:
4610                 mac->autoneg = 1;
4611                 adapter->hw.phy.autoneg_advertised = ADVERTISE_1000_FULL;
4612                 break;
4613         case SPEED_1000 + DUPLEX_HALF: /* not supported */
4614         default:
4615                 dev_err(&adapter->pdev->dev,
4616                         "Unsupported Speed/Duplex configuration\n");
4617                 return -EINVAL;
4618         }
4619         return 0;
4620 }
4621
4622
4623 static int igb_suspend(struct pci_dev *pdev, pm_message_t state)
4624 {
4625         struct net_device *netdev = pci_get_drvdata(pdev);
4626         struct igb_adapter *adapter = netdev_priv(netdev);
4627         struct e1000_hw *hw = &adapter->hw;
4628         u32 ctrl, rctl, status;
4629         u32 wufc = adapter->wol;
4630 #ifdef CONFIG_PM
4631         int retval = 0;
4632 #endif
4633
4634         netif_device_detach(netdev);
4635
4636         if (netif_running(netdev))
4637                 igb_close(netdev);
4638
4639         igb_reset_interrupt_capability(adapter);
4640
4641         igb_free_queues(adapter);
4642
4643 #ifdef CONFIG_PM
4644         retval = pci_save_state(pdev);
4645         if (retval)
4646                 return retval;
4647 #endif
4648
4649         status = rd32(E1000_STATUS);
4650         if (status & E1000_STATUS_LU)
4651                 wufc &= ~E1000_WUFC_LNKC;
4652
4653         if (wufc) {
4654                 igb_setup_rctl(adapter);
4655                 igb_set_multi(netdev);
4656
4657                 /* turn on all-multi mode if wake on multicast is enabled */
4658                 if (wufc & E1000_WUFC_MC) {
4659                         rctl = rd32(E1000_RCTL);
4660                         rctl |= E1000_RCTL_MPE;
4661                         wr32(E1000_RCTL, rctl);
4662                 }
4663
4664                 ctrl = rd32(E1000_CTRL);
4665                 /* advertise wake from D3Cold */
4666                 #define E1000_CTRL_ADVD3WUC 0x00100000
4667                 /* phy power management enable */
4668                 #define E1000_CTRL_EN_PHY_PWR_MGMT 0x00200000
4669                 ctrl |= E1000_CTRL_ADVD3WUC;
4670                 wr32(E1000_CTRL, ctrl);
4671
4672                 /* Allow time for pending master requests to run */
4673                 igb_disable_pcie_master(&adapter->hw);
4674
4675                 wr32(E1000_WUC, E1000_WUC_PME_EN);
4676                 wr32(E1000_WUFC, wufc);
4677         } else {
4678                 wr32(E1000_WUC, 0);
4679                 wr32(E1000_WUFC, 0);
4680         }
4681
4682         /* make sure adapter isn't asleep if manageability/wol is enabled */
4683         if (wufc || adapter->en_mng_pt) {
4684                 pci_enable_wake(pdev, PCI_D3hot, 1);
4685                 pci_enable_wake(pdev, PCI_D3cold, 1);
4686         } else {
4687                 igb_shutdown_fiber_serdes_link_82575(hw);
4688                 pci_enable_wake(pdev, PCI_D3hot, 0);
4689                 pci_enable_wake(pdev, PCI_D3cold, 0);
4690         }
4691
4692         /* Release control of h/w to f/w.  If f/w is AMT enabled, this
4693          * would have already happened in close and is redundant. */
4694         igb_release_hw_control(adapter);
4695
4696         pci_disable_device(pdev);
4697
4698         pci_set_power_state(pdev, pci_choose_state(pdev, state));
4699
4700         return 0;
4701 }
4702
4703 #ifdef CONFIG_PM
4704 static int igb_resume(struct pci_dev *pdev)
4705 {
4706         struct net_device *netdev = pci_get_drvdata(pdev);
4707         struct igb_adapter *adapter = netdev_priv(netdev);
4708         struct e1000_hw *hw = &adapter->hw;
4709         u32 err;
4710
4711         pci_set_power_state(pdev, PCI_D0);
4712         pci_restore_state(pdev);
4713
4714         err = pci_enable_device_mem(pdev);
4715         if (err) {
4716                 dev_err(&pdev->dev,
4717                         "igb: Cannot enable PCI device from suspend\n");
4718                 return err;
4719         }
4720         pci_set_master(pdev);
4721
4722         pci_enable_wake(pdev, PCI_D3hot, 0);
4723         pci_enable_wake(pdev, PCI_D3cold, 0);
4724
4725         igb_set_interrupt_capability(adapter);
4726
4727         if (igb_alloc_queues(adapter)) {
4728                 dev_err(&pdev->dev, "Unable to allocate memory for queues\n");
4729                 return -ENOMEM;
4730         }
4731
4732         /* e1000_power_up_phy(adapter); */
4733
4734         igb_reset(adapter);
4735
4736         /* let the f/w know that the h/w is now under the control of the
4737          * driver. */
4738         igb_get_hw_control(adapter);
4739
4740         wr32(E1000_WUS, ~0);
4741
4742         if (netif_running(netdev)) {
4743                 err = igb_open(netdev);
4744                 if (err)
4745                         return err;
4746         }
4747
4748         netif_device_attach(netdev);
4749
4750         return 0;
4751 }
4752 #endif
4753
4754 static void igb_shutdown(struct pci_dev *pdev)
4755 {
4756         igb_suspend(pdev, PMSG_SUSPEND);
4757 }
4758
4759 #ifdef CONFIG_NET_POLL_CONTROLLER
4760 /*
4761  * Polling 'interrupt' - used by things like netconsole to send skbs
4762  * without having to re-enable interrupts. It's not called while
4763  * the interrupt routine is executing.
4764  */
4765 static void igb_netpoll(struct net_device *netdev)
4766 {
4767         struct igb_adapter *adapter = netdev_priv(netdev);
4768         struct e1000_hw *hw = &adapter->hw;
4769         int i;
4770
4771         if (!adapter->msix_entries) {
4772                 igb_irq_disable(adapter);
4773                 napi_schedule(&adapter->rx_ring[0].napi);
4774                 return;
4775         }
4776
4777         for (i = 0; i < adapter->num_tx_queues; i++) {
4778                 struct igb_ring *tx_ring = &adapter->tx_ring[i];
4779                 wr32(E1000_EIMC, tx_ring->eims_value);
4780                 igb_clean_tx_irq(tx_ring);
4781                 wr32(E1000_EIMS, tx_ring->eims_value);
4782         }
4783
4784         for (i = 0; i < adapter->num_rx_queues; i++) {
4785                 struct igb_ring *rx_ring = &adapter->rx_ring[i];
4786                 wr32(E1000_EIMC, rx_ring->eims_value);
4787                 napi_schedule(&rx_ring->napi);
4788         }
4789 }
4790 #endif /* CONFIG_NET_POLL_CONTROLLER */
4791
4792 /**
4793  * igb_io_error_detected - called when PCI error is detected
4794  * @pdev: Pointer to PCI device
4795  * @state: The current pci connection state
4796  *
4797  * This function is called after a PCI bus error affecting
4798  * this device has been detected.
4799  */
4800 static pci_ers_result_t igb_io_error_detected(struct pci_dev *pdev,
4801                                               pci_channel_state_t state)
4802 {
4803         struct net_device *netdev = pci_get_drvdata(pdev);
4804         struct igb_adapter *adapter = netdev_priv(netdev);
4805
4806         netif_device_detach(netdev);
4807
4808         if (netif_running(netdev))
4809                 igb_down(adapter);
4810         pci_disable_device(pdev);
4811
4812         /* Request a slot slot reset. */
4813         return PCI_ERS_RESULT_NEED_RESET;
4814 }
4815
4816 /**
4817  * igb_io_slot_reset - called after the pci bus has been reset.
4818  * @pdev: Pointer to PCI device
4819  *
4820  * Restart the card from scratch, as if from a cold-boot. Implementation
4821  * resembles the first-half of the igb_resume routine.
4822  */
4823 static pci_ers_result_t igb_io_slot_reset(struct pci_dev *pdev)
4824 {
4825         struct net_device *netdev = pci_get_drvdata(pdev);
4826         struct igb_adapter *adapter = netdev_priv(netdev);
4827         struct e1000_hw *hw = &adapter->hw;
4828         pci_ers_result_t result;
4829         int err;
4830
4831         if (pci_enable_device_mem(pdev)) {
4832                 dev_err(&pdev->dev,
4833                         "Cannot re-enable PCI device after reset.\n");
4834                 result = PCI_ERS_RESULT_DISCONNECT;
4835         } else {
4836                 pci_set_master(pdev);
4837                 pci_restore_state(pdev);
4838
4839                 pci_enable_wake(pdev, PCI_D3hot, 0);
4840                 pci_enable_wake(pdev, PCI_D3cold, 0);
4841
4842                 igb_reset(adapter);
4843                 wr32(E1000_WUS, ~0);
4844                 result = PCI_ERS_RESULT_RECOVERED;
4845         }
4846
4847         err = pci_cleanup_aer_uncorrect_error_status(pdev);
4848         if (err) {
4849                 dev_err(&pdev->dev, "pci_cleanup_aer_uncorrect_error_status "
4850                         "failed 0x%0x\n", err);
4851                 /* non-fatal, continue */
4852         }
4853
4854         return result;
4855 }
4856
4857 /**
4858  * igb_io_resume - called when traffic can start flowing again.
4859  * @pdev: Pointer to PCI device
4860  *
4861  * This callback is called when the error recovery driver tells us that
4862  * its OK to resume normal operation. Implementation resembles the
4863  * second-half of the igb_resume routine.
4864  */
4865 static void igb_io_resume(struct pci_dev *pdev)
4866 {
4867         struct net_device *netdev = pci_get_drvdata(pdev);
4868         struct igb_adapter *adapter = netdev_priv(netdev);
4869
4870         if (netif_running(netdev)) {
4871                 if (igb_up(adapter)) {
4872                         dev_err(&pdev->dev, "igb_up failed after reset\n");
4873                         return;
4874                 }
4875         }
4876
4877         netif_device_attach(netdev);
4878
4879         /* let the f/w know that the h/w is now under the control of the
4880          * driver. */
4881         igb_get_hw_control(adapter);
4882 }
4883
4884 /* igb_main.c */