]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/net/ethernet/intel/ixgb/ixgb_main.c
ixgb: Omit private ndo_get_stats function
[karo-tx-linux.git] / drivers / net / ethernet / intel / ixgb / ixgb_main.c
1 /*******************************************************************************
2
3   Intel PRO/10GbE Linux driver
4   Copyright(c) 1999 - 2008 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   Linux NICS <linux.nics@intel.com>
24   e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
25   Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
26
27 *******************************************************************************/
28
29 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
30
31 #include <linux/prefetch.h>
32 #include "ixgb.h"
33
34 char ixgb_driver_name[] = "ixgb";
35 static char ixgb_driver_string[] = "Intel(R) PRO/10GbE Network Driver";
36
37 #define DRIVERNAPI "-NAPI"
38 #define DRV_VERSION "1.0.135-k2" DRIVERNAPI
39 const char ixgb_driver_version[] = DRV_VERSION;
40 static const char ixgb_copyright[] = "Copyright (c) 1999-2008 Intel Corporation.";
41
42 #define IXGB_CB_LENGTH 256
43 static unsigned int copybreak __read_mostly = IXGB_CB_LENGTH;
44 module_param(copybreak, uint, 0644);
45 MODULE_PARM_DESC(copybreak,
46         "Maximum size of packet that is copied to a new buffer on receive");
47
48 /* ixgb_pci_tbl - PCI Device ID Table
49  *
50  * Wildcard entries (PCI_ANY_ID) should come last
51  * Last entry must be all 0s
52  *
53  * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
54  *   Class, Class Mask, private data (not used) }
55  */
56 static const struct pci_device_id ixgb_pci_tbl[] = {
57         {PCI_VENDOR_ID_INTEL, IXGB_DEVICE_ID_82597EX,
58          PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
59         {PCI_VENDOR_ID_INTEL, IXGB_DEVICE_ID_82597EX_CX4,
60          PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
61         {PCI_VENDOR_ID_INTEL, IXGB_DEVICE_ID_82597EX_SR,
62          PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
63         {PCI_VENDOR_ID_INTEL, IXGB_DEVICE_ID_82597EX_LR,
64          PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
65
66         /* required last entry */
67         {0,}
68 };
69
70 MODULE_DEVICE_TABLE(pci, ixgb_pci_tbl);
71
72 /* Local Function Prototypes */
73 static int ixgb_init_module(void);
74 static void ixgb_exit_module(void);
75 static int ixgb_probe(struct pci_dev *pdev, const struct pci_device_id *ent);
76 static void ixgb_remove(struct pci_dev *pdev);
77 static int ixgb_sw_init(struct ixgb_adapter *adapter);
78 static int ixgb_open(struct net_device *netdev);
79 static int ixgb_close(struct net_device *netdev);
80 static void ixgb_configure_tx(struct ixgb_adapter *adapter);
81 static void ixgb_configure_rx(struct ixgb_adapter *adapter);
82 static void ixgb_setup_rctl(struct ixgb_adapter *adapter);
83 static void ixgb_clean_tx_ring(struct ixgb_adapter *adapter);
84 static void ixgb_clean_rx_ring(struct ixgb_adapter *adapter);
85 static void ixgb_set_multi(struct net_device *netdev);
86 static void ixgb_watchdog(unsigned long data);
87 static netdev_tx_t ixgb_xmit_frame(struct sk_buff *skb,
88                                    struct net_device *netdev);
89 static int ixgb_change_mtu(struct net_device *netdev, int new_mtu);
90 static int ixgb_set_mac(struct net_device *netdev, void *p);
91 static irqreturn_t ixgb_intr(int irq, void *data);
92 static bool ixgb_clean_tx_irq(struct ixgb_adapter *adapter);
93
94 static int ixgb_clean(struct napi_struct *, int);
95 static bool ixgb_clean_rx_irq(struct ixgb_adapter *, int *, int);
96 static void ixgb_alloc_rx_buffers(struct ixgb_adapter *, int);
97
98 static void ixgb_tx_timeout(struct net_device *dev);
99 static void ixgb_tx_timeout_task(struct work_struct *work);
100
101 static void ixgb_vlan_strip_enable(struct ixgb_adapter *adapter);
102 static void ixgb_vlan_strip_disable(struct ixgb_adapter *adapter);
103 static int ixgb_vlan_rx_add_vid(struct net_device *netdev,
104                                 __be16 proto, u16 vid);
105 static int ixgb_vlan_rx_kill_vid(struct net_device *netdev,
106                                  __be16 proto, u16 vid);
107 static void ixgb_restore_vlan(struct ixgb_adapter *adapter);
108
109 #ifdef CONFIG_NET_POLL_CONTROLLER
110 /* for netdump / net console */
111 static void ixgb_netpoll(struct net_device *dev);
112 #endif
113
114 static pci_ers_result_t ixgb_io_error_detected (struct pci_dev *pdev,
115                              enum pci_channel_state state);
116 static pci_ers_result_t ixgb_io_slot_reset (struct pci_dev *pdev);
117 static void ixgb_io_resume (struct pci_dev *pdev);
118
119 static const struct pci_error_handlers ixgb_err_handler = {
120         .error_detected = ixgb_io_error_detected,
121         .slot_reset = ixgb_io_slot_reset,
122         .resume = ixgb_io_resume,
123 };
124
125 static struct pci_driver ixgb_driver = {
126         .name     = ixgb_driver_name,
127         .id_table = ixgb_pci_tbl,
128         .probe    = ixgb_probe,
129         .remove   = ixgb_remove,
130         .err_handler = &ixgb_err_handler
131 };
132
133 MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
134 MODULE_DESCRIPTION("Intel(R) PRO/10GbE Network Driver");
135 MODULE_LICENSE("GPL");
136 MODULE_VERSION(DRV_VERSION);
137
138 #define DEFAULT_MSG_ENABLE (NETIF_MSG_DRV|NETIF_MSG_PROBE|NETIF_MSG_LINK)
139 static int debug = -1;
140 module_param(debug, int, 0);
141 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
142
143 /**
144  * ixgb_init_module - Driver Registration Routine
145  *
146  * ixgb_init_module is the first routine called when the driver is
147  * loaded. All it does is register with the PCI subsystem.
148  **/
149
150 static int __init
151 ixgb_init_module(void)
152 {
153         pr_info("%s - version %s\n", ixgb_driver_string, ixgb_driver_version);
154         pr_info("%s\n", ixgb_copyright);
155
156         return pci_register_driver(&ixgb_driver);
157 }
158
159 module_init(ixgb_init_module);
160
161 /**
162  * ixgb_exit_module - Driver Exit Cleanup Routine
163  *
164  * ixgb_exit_module is called just before the driver is removed
165  * from memory.
166  **/
167
168 static void __exit
169 ixgb_exit_module(void)
170 {
171         pci_unregister_driver(&ixgb_driver);
172 }
173
174 module_exit(ixgb_exit_module);
175
176 /**
177  * ixgb_irq_disable - Mask off interrupt generation on the NIC
178  * @adapter: board private structure
179  **/
180
181 static void
182 ixgb_irq_disable(struct ixgb_adapter *adapter)
183 {
184         IXGB_WRITE_REG(&adapter->hw, IMC, ~0);
185         IXGB_WRITE_FLUSH(&adapter->hw);
186         synchronize_irq(adapter->pdev->irq);
187 }
188
189 /**
190  * ixgb_irq_enable - Enable default interrupt generation settings
191  * @adapter: board private structure
192  **/
193
194 static void
195 ixgb_irq_enable(struct ixgb_adapter *adapter)
196 {
197         u32 val = IXGB_INT_RXT0 | IXGB_INT_RXDMT0 |
198                   IXGB_INT_TXDW | IXGB_INT_LSC;
199         if (adapter->hw.subsystem_vendor_id == PCI_VENDOR_ID_SUN)
200                 val |= IXGB_INT_GPI0;
201         IXGB_WRITE_REG(&adapter->hw, IMS, val);
202         IXGB_WRITE_FLUSH(&adapter->hw);
203 }
204
205 int
206 ixgb_up(struct ixgb_adapter *adapter)
207 {
208         struct net_device *netdev = adapter->netdev;
209         int err, irq_flags = IRQF_SHARED;
210         int max_frame = netdev->mtu + ENET_HEADER_SIZE + ENET_FCS_LENGTH;
211         struct ixgb_hw *hw = &adapter->hw;
212
213         /* hardware has been reset, we need to reload some things */
214
215         ixgb_rar_set(hw, netdev->dev_addr, 0);
216         ixgb_set_multi(netdev);
217
218         ixgb_restore_vlan(adapter);
219
220         ixgb_configure_tx(adapter);
221         ixgb_setup_rctl(adapter);
222         ixgb_configure_rx(adapter);
223         ixgb_alloc_rx_buffers(adapter, IXGB_DESC_UNUSED(&adapter->rx_ring));
224
225         /* disable interrupts and get the hardware into a known state */
226         IXGB_WRITE_REG(&adapter->hw, IMC, 0xffffffff);
227
228         /* only enable MSI if bus is in PCI-X mode */
229         if (IXGB_READ_REG(&adapter->hw, STATUS) & IXGB_STATUS_PCIX_MODE) {
230                 err = pci_enable_msi(adapter->pdev);
231                 if (!err) {
232                         adapter->have_msi = true;
233                         irq_flags = 0;
234                 }
235                 /* proceed to try to request regular interrupt */
236         }
237
238         err = request_irq(adapter->pdev->irq, ixgb_intr, irq_flags,
239                           netdev->name, netdev);
240         if (err) {
241                 if (adapter->have_msi)
242                         pci_disable_msi(adapter->pdev);
243                 netif_err(adapter, probe, adapter->netdev,
244                           "Unable to allocate interrupt Error: %d\n", err);
245                 return err;
246         }
247
248         if ((hw->max_frame_size != max_frame) ||
249                 (hw->max_frame_size !=
250                 (IXGB_READ_REG(hw, MFS) >> IXGB_MFS_SHIFT))) {
251
252                 hw->max_frame_size = max_frame;
253
254                 IXGB_WRITE_REG(hw, MFS, hw->max_frame_size << IXGB_MFS_SHIFT);
255
256                 if (hw->max_frame_size >
257                    IXGB_MAX_ENET_FRAME_SIZE_WITHOUT_FCS + ENET_FCS_LENGTH) {
258                         u32 ctrl0 = IXGB_READ_REG(hw, CTRL0);
259
260                         if (!(ctrl0 & IXGB_CTRL0_JFE)) {
261                                 ctrl0 |= IXGB_CTRL0_JFE;
262                                 IXGB_WRITE_REG(hw, CTRL0, ctrl0);
263                         }
264                 }
265         }
266
267         clear_bit(__IXGB_DOWN, &adapter->flags);
268
269         napi_enable(&adapter->napi);
270         ixgb_irq_enable(adapter);
271
272         netif_wake_queue(netdev);
273
274         mod_timer(&adapter->watchdog_timer, jiffies);
275
276         return 0;
277 }
278
279 void
280 ixgb_down(struct ixgb_adapter *adapter, bool kill_watchdog)
281 {
282         struct net_device *netdev = adapter->netdev;
283
284         /* prevent the interrupt handler from restarting watchdog */
285         set_bit(__IXGB_DOWN, &adapter->flags);
286
287         netif_carrier_off(netdev);
288
289         napi_disable(&adapter->napi);
290         /* waiting for NAPI to complete can re-enable interrupts */
291         ixgb_irq_disable(adapter);
292         free_irq(adapter->pdev->irq, netdev);
293
294         if (adapter->have_msi)
295                 pci_disable_msi(adapter->pdev);
296
297         if (kill_watchdog)
298                 del_timer_sync(&adapter->watchdog_timer);
299
300         adapter->link_speed = 0;
301         adapter->link_duplex = 0;
302         netif_stop_queue(netdev);
303
304         ixgb_reset(adapter);
305         ixgb_clean_tx_ring(adapter);
306         ixgb_clean_rx_ring(adapter);
307 }
308
309 void
310 ixgb_reset(struct ixgb_adapter *adapter)
311 {
312         struct ixgb_hw *hw = &adapter->hw;
313
314         ixgb_adapter_stop(hw);
315         if (!ixgb_init_hw(hw))
316                 netif_err(adapter, probe, adapter->netdev, "ixgb_init_hw failed\n");
317
318         /* restore frame size information */
319         IXGB_WRITE_REG(hw, MFS, hw->max_frame_size << IXGB_MFS_SHIFT);
320         if (hw->max_frame_size >
321             IXGB_MAX_ENET_FRAME_SIZE_WITHOUT_FCS + ENET_FCS_LENGTH) {
322                 u32 ctrl0 = IXGB_READ_REG(hw, CTRL0);
323                 if (!(ctrl0 & IXGB_CTRL0_JFE)) {
324                         ctrl0 |= IXGB_CTRL0_JFE;
325                         IXGB_WRITE_REG(hw, CTRL0, ctrl0);
326                 }
327         }
328 }
329
330 static netdev_features_t
331 ixgb_fix_features(struct net_device *netdev, netdev_features_t features)
332 {
333         /*
334          * Tx VLAN insertion does not work per HW design when Rx stripping is
335          * disabled.
336          */
337         if (!(features & NETIF_F_HW_VLAN_CTAG_RX))
338                 features &= ~NETIF_F_HW_VLAN_CTAG_TX;
339
340         return features;
341 }
342
343 static int
344 ixgb_set_features(struct net_device *netdev, netdev_features_t features)
345 {
346         struct ixgb_adapter *adapter = netdev_priv(netdev);
347         netdev_features_t changed = features ^ netdev->features;
348
349         if (!(changed & (NETIF_F_RXCSUM|NETIF_F_HW_VLAN_CTAG_RX)))
350                 return 0;
351
352         adapter->rx_csum = !!(features & NETIF_F_RXCSUM);
353
354         if (netif_running(netdev)) {
355                 ixgb_down(adapter, true);
356                 ixgb_up(adapter);
357                 ixgb_set_speed_duplex(netdev);
358         } else
359                 ixgb_reset(adapter);
360
361         return 0;
362 }
363
364
365 static const struct net_device_ops ixgb_netdev_ops = {
366         .ndo_open               = ixgb_open,
367         .ndo_stop               = ixgb_close,
368         .ndo_start_xmit         = ixgb_xmit_frame,
369         .ndo_set_rx_mode        = ixgb_set_multi,
370         .ndo_validate_addr      = eth_validate_addr,
371         .ndo_set_mac_address    = ixgb_set_mac,
372         .ndo_change_mtu         = ixgb_change_mtu,
373         .ndo_tx_timeout         = ixgb_tx_timeout,
374         .ndo_vlan_rx_add_vid    = ixgb_vlan_rx_add_vid,
375         .ndo_vlan_rx_kill_vid   = ixgb_vlan_rx_kill_vid,
376 #ifdef CONFIG_NET_POLL_CONTROLLER
377         .ndo_poll_controller    = ixgb_netpoll,
378 #endif
379         .ndo_fix_features       = ixgb_fix_features,
380         .ndo_set_features       = ixgb_set_features,
381 };
382
383 /**
384  * ixgb_probe - Device Initialization Routine
385  * @pdev: PCI device information struct
386  * @ent: entry in ixgb_pci_tbl
387  *
388  * Returns 0 on success, negative on failure
389  *
390  * ixgb_probe initializes an adapter identified by a pci_dev structure.
391  * The OS initialization, configuring of the adapter private structure,
392  * and a hardware reset occur.
393  **/
394
395 static int
396 ixgb_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
397 {
398         struct net_device *netdev = NULL;
399         struct ixgb_adapter *adapter;
400         static int cards_found = 0;
401         int pci_using_dac;
402         int i;
403         int err;
404
405         err = pci_enable_device(pdev);
406         if (err)
407                 return err;
408
409         pci_using_dac = 0;
410         err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
411         if (!err) {
412                 pci_using_dac = 1;
413         } else {
414                 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
415                 if (err) {
416                         pr_err("No usable DMA configuration, aborting\n");
417                         goto err_dma_mask;
418                 }
419         }
420
421         err = pci_request_regions(pdev, ixgb_driver_name);
422         if (err)
423                 goto err_request_regions;
424
425         pci_set_master(pdev);
426
427         netdev = alloc_etherdev(sizeof(struct ixgb_adapter));
428         if (!netdev) {
429                 err = -ENOMEM;
430                 goto err_alloc_etherdev;
431         }
432
433         SET_NETDEV_DEV(netdev, &pdev->dev);
434
435         pci_set_drvdata(pdev, netdev);
436         adapter = netdev_priv(netdev);
437         adapter->netdev = netdev;
438         adapter->pdev = pdev;
439         adapter->hw.back = adapter;
440         adapter->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
441
442         adapter->hw.hw_addr = pci_ioremap_bar(pdev, BAR_0);
443         if (!adapter->hw.hw_addr) {
444                 err = -EIO;
445                 goto err_ioremap;
446         }
447
448         for (i = BAR_1; i <= BAR_5; i++) {
449                 if (pci_resource_len(pdev, i) == 0)
450                         continue;
451                 if (pci_resource_flags(pdev, i) & IORESOURCE_IO) {
452                         adapter->hw.io_base = pci_resource_start(pdev, i);
453                         break;
454                 }
455         }
456
457         netdev->netdev_ops = &ixgb_netdev_ops;
458         ixgb_set_ethtool_ops(netdev);
459         netdev->watchdog_timeo = 5 * HZ;
460         netif_napi_add(netdev, &adapter->napi, ixgb_clean, 64);
461
462         strncpy(netdev->name, pci_name(pdev), sizeof(netdev->name) - 1);
463
464         adapter->bd_number = cards_found;
465         adapter->link_speed = 0;
466         adapter->link_duplex = 0;
467
468         /* setup the private structure */
469
470         err = ixgb_sw_init(adapter);
471         if (err)
472                 goto err_sw_init;
473
474         netdev->hw_features = NETIF_F_SG |
475                            NETIF_F_TSO |
476                            NETIF_F_HW_CSUM |
477                            NETIF_F_HW_VLAN_CTAG_TX |
478                            NETIF_F_HW_VLAN_CTAG_RX;
479         netdev->features = netdev->hw_features |
480                            NETIF_F_HW_VLAN_CTAG_FILTER;
481         netdev->hw_features |= NETIF_F_RXCSUM;
482
483         if (pci_using_dac) {
484                 netdev->features |= NETIF_F_HIGHDMA;
485                 netdev->vlan_features |= NETIF_F_HIGHDMA;
486         }
487
488         /* MTU range: 68 - 16114 */
489         netdev->min_mtu = ETH_MIN_MTU;
490         netdev->max_mtu = IXGB_MAX_JUMBO_FRAME_SIZE - ETH_HLEN;
491
492         /* make sure the EEPROM is good */
493
494         if (!ixgb_validate_eeprom_checksum(&adapter->hw)) {
495                 netif_err(adapter, probe, adapter->netdev,
496                           "The EEPROM Checksum Is Not Valid\n");
497                 err = -EIO;
498                 goto err_eeprom;
499         }
500
501         ixgb_get_ee_mac_addr(&adapter->hw, netdev->dev_addr);
502
503         if (!is_valid_ether_addr(netdev->dev_addr)) {
504                 netif_err(adapter, probe, adapter->netdev, "Invalid MAC Address\n");
505                 err = -EIO;
506                 goto err_eeprom;
507         }
508
509         adapter->part_num = ixgb_get_ee_pba_number(&adapter->hw);
510
511         init_timer(&adapter->watchdog_timer);
512         adapter->watchdog_timer.function = ixgb_watchdog;
513         adapter->watchdog_timer.data = (unsigned long)adapter;
514
515         INIT_WORK(&adapter->tx_timeout_task, ixgb_tx_timeout_task);
516
517         strcpy(netdev->name, "eth%d");
518         err = register_netdev(netdev);
519         if (err)
520                 goto err_register;
521
522         /* carrier off reporting is important to ethtool even BEFORE open */
523         netif_carrier_off(netdev);
524
525         netif_info(adapter, probe, adapter->netdev,
526                    "Intel(R) PRO/10GbE Network Connection\n");
527         ixgb_check_options(adapter);
528         /* reset the hardware with the new settings */
529
530         ixgb_reset(adapter);
531
532         cards_found++;
533         return 0;
534
535 err_register:
536 err_sw_init:
537 err_eeprom:
538         iounmap(adapter->hw.hw_addr);
539 err_ioremap:
540         free_netdev(netdev);
541 err_alloc_etherdev:
542         pci_release_regions(pdev);
543 err_request_regions:
544 err_dma_mask:
545         pci_disable_device(pdev);
546         return err;
547 }
548
549 /**
550  * ixgb_remove - Device Removal Routine
551  * @pdev: PCI device information struct
552  *
553  * ixgb_remove is called by the PCI subsystem to alert the driver
554  * that it should release a PCI device.  The could be caused by a
555  * Hot-Plug event, or because the driver is going to be removed from
556  * memory.
557  **/
558
559 static void
560 ixgb_remove(struct pci_dev *pdev)
561 {
562         struct net_device *netdev = pci_get_drvdata(pdev);
563         struct ixgb_adapter *adapter = netdev_priv(netdev);
564
565         cancel_work_sync(&adapter->tx_timeout_task);
566
567         unregister_netdev(netdev);
568
569         iounmap(adapter->hw.hw_addr);
570         pci_release_regions(pdev);
571
572         free_netdev(netdev);
573         pci_disable_device(pdev);
574 }
575
576 /**
577  * ixgb_sw_init - Initialize general software structures (struct ixgb_adapter)
578  * @adapter: board private structure to initialize
579  *
580  * ixgb_sw_init initializes the Adapter private data structure.
581  * Fields are initialized based on PCI device information and
582  * OS network device settings (MTU size).
583  **/
584
585 static int
586 ixgb_sw_init(struct ixgb_adapter *adapter)
587 {
588         struct ixgb_hw *hw = &adapter->hw;
589         struct net_device *netdev = adapter->netdev;
590         struct pci_dev *pdev = adapter->pdev;
591
592         /* PCI config space info */
593
594         hw->vendor_id = pdev->vendor;
595         hw->device_id = pdev->device;
596         hw->subsystem_vendor_id = pdev->subsystem_vendor;
597         hw->subsystem_id = pdev->subsystem_device;
598
599         hw->max_frame_size = netdev->mtu + ENET_HEADER_SIZE + ENET_FCS_LENGTH;
600         adapter->rx_buffer_len = hw->max_frame_size + 8; /* + 8 for errata */
601
602         if ((hw->device_id == IXGB_DEVICE_ID_82597EX) ||
603             (hw->device_id == IXGB_DEVICE_ID_82597EX_CX4) ||
604             (hw->device_id == IXGB_DEVICE_ID_82597EX_LR) ||
605             (hw->device_id == IXGB_DEVICE_ID_82597EX_SR))
606                 hw->mac_type = ixgb_82597;
607         else {
608                 /* should never have loaded on this device */
609                 netif_err(adapter, probe, adapter->netdev, "unsupported device id\n");
610         }
611
612         /* enable flow control to be programmed */
613         hw->fc.send_xon = 1;
614
615         set_bit(__IXGB_DOWN, &adapter->flags);
616         return 0;
617 }
618
619 /**
620  * ixgb_open - Called when a network interface is made active
621  * @netdev: network interface device structure
622  *
623  * Returns 0 on success, negative value on failure
624  *
625  * The open entry point is called when a network interface is made
626  * active by the system (IFF_UP).  At this point all resources needed
627  * for transmit and receive operations are allocated, the interrupt
628  * handler is registered with the OS, the watchdog timer is started,
629  * and the stack is notified that the interface is ready.
630  **/
631
632 static int
633 ixgb_open(struct net_device *netdev)
634 {
635         struct ixgb_adapter *adapter = netdev_priv(netdev);
636         int err;
637
638         /* allocate transmit descriptors */
639         err = ixgb_setup_tx_resources(adapter);
640         if (err)
641                 goto err_setup_tx;
642
643         netif_carrier_off(netdev);
644
645         /* allocate receive descriptors */
646
647         err = ixgb_setup_rx_resources(adapter);
648         if (err)
649                 goto err_setup_rx;
650
651         err = ixgb_up(adapter);
652         if (err)
653                 goto err_up;
654
655         netif_start_queue(netdev);
656
657         return 0;
658
659 err_up:
660         ixgb_free_rx_resources(adapter);
661 err_setup_rx:
662         ixgb_free_tx_resources(adapter);
663 err_setup_tx:
664         ixgb_reset(adapter);
665
666         return err;
667 }
668
669 /**
670  * ixgb_close - Disables a network interface
671  * @netdev: network interface device structure
672  *
673  * Returns 0, this is not allowed to fail
674  *
675  * The close entry point is called when an interface is de-activated
676  * by the OS.  The hardware is still under the drivers control, but
677  * needs to be disabled.  A global MAC reset is issued to stop the
678  * hardware, and all transmit and receive resources are freed.
679  **/
680
681 static int
682 ixgb_close(struct net_device *netdev)
683 {
684         struct ixgb_adapter *adapter = netdev_priv(netdev);
685
686         ixgb_down(adapter, true);
687
688         ixgb_free_tx_resources(adapter);
689         ixgb_free_rx_resources(adapter);
690
691         return 0;
692 }
693
694 /**
695  * ixgb_setup_tx_resources - allocate Tx resources (Descriptors)
696  * @adapter: board private structure
697  *
698  * Return 0 on success, negative on failure
699  **/
700
701 int
702 ixgb_setup_tx_resources(struct ixgb_adapter *adapter)
703 {
704         struct ixgb_desc_ring *txdr = &adapter->tx_ring;
705         struct pci_dev *pdev = adapter->pdev;
706         int size;
707
708         size = sizeof(struct ixgb_buffer) * txdr->count;
709         txdr->buffer_info = vzalloc(size);
710         if (!txdr->buffer_info)
711                 return -ENOMEM;
712
713         /* round up to nearest 4K */
714
715         txdr->size = txdr->count * sizeof(struct ixgb_tx_desc);
716         txdr->size = ALIGN(txdr->size, 4096);
717
718         txdr->desc = dma_zalloc_coherent(&pdev->dev, txdr->size, &txdr->dma,
719                                          GFP_KERNEL);
720         if (!txdr->desc) {
721                 vfree(txdr->buffer_info);
722                 return -ENOMEM;
723         }
724
725         txdr->next_to_use = 0;
726         txdr->next_to_clean = 0;
727
728         return 0;
729 }
730
731 /**
732  * ixgb_configure_tx - Configure 82597 Transmit Unit after Reset.
733  * @adapter: board private structure
734  *
735  * Configure the Tx unit of the MAC after a reset.
736  **/
737
738 static void
739 ixgb_configure_tx(struct ixgb_adapter *adapter)
740 {
741         u64 tdba = adapter->tx_ring.dma;
742         u32 tdlen = adapter->tx_ring.count * sizeof(struct ixgb_tx_desc);
743         u32 tctl;
744         struct ixgb_hw *hw = &adapter->hw;
745
746         /* Setup the Base and Length of the Tx Descriptor Ring
747          * tx_ring.dma can be either a 32 or 64 bit value
748          */
749
750         IXGB_WRITE_REG(hw, TDBAL, (tdba & 0x00000000ffffffffULL));
751         IXGB_WRITE_REG(hw, TDBAH, (tdba >> 32));
752
753         IXGB_WRITE_REG(hw, TDLEN, tdlen);
754
755         /* Setup the HW Tx Head and Tail descriptor pointers */
756
757         IXGB_WRITE_REG(hw, TDH, 0);
758         IXGB_WRITE_REG(hw, TDT, 0);
759
760         /* don't set up txdctl, it induces performance problems if configured
761          * incorrectly */
762         /* Set the Tx Interrupt Delay register */
763
764         IXGB_WRITE_REG(hw, TIDV, adapter->tx_int_delay);
765
766         /* Program the Transmit Control Register */
767
768         tctl = IXGB_TCTL_TCE | IXGB_TCTL_TXEN | IXGB_TCTL_TPDE;
769         IXGB_WRITE_REG(hw, TCTL, tctl);
770
771         /* Setup Transmit Descriptor Settings for this adapter */
772         adapter->tx_cmd_type =
773                 IXGB_TX_DESC_TYPE |
774                 (adapter->tx_int_delay_enable ? IXGB_TX_DESC_CMD_IDE : 0);
775 }
776
777 /**
778  * ixgb_setup_rx_resources - allocate Rx resources (Descriptors)
779  * @adapter: board private structure
780  *
781  * Returns 0 on success, negative on failure
782  **/
783
784 int
785 ixgb_setup_rx_resources(struct ixgb_adapter *adapter)
786 {
787         struct ixgb_desc_ring *rxdr = &adapter->rx_ring;
788         struct pci_dev *pdev = adapter->pdev;
789         int size;
790
791         size = sizeof(struct ixgb_buffer) * rxdr->count;
792         rxdr->buffer_info = vzalloc(size);
793         if (!rxdr->buffer_info)
794                 return -ENOMEM;
795
796         /* Round up to nearest 4K */
797
798         rxdr->size = rxdr->count * sizeof(struct ixgb_rx_desc);
799         rxdr->size = ALIGN(rxdr->size, 4096);
800
801         rxdr->desc = dma_alloc_coherent(&pdev->dev, rxdr->size, &rxdr->dma,
802                                         GFP_KERNEL);
803
804         if (!rxdr->desc) {
805                 vfree(rxdr->buffer_info);
806                 return -ENOMEM;
807         }
808         memset(rxdr->desc, 0, rxdr->size);
809
810         rxdr->next_to_clean = 0;
811         rxdr->next_to_use = 0;
812
813         return 0;
814 }
815
816 /**
817  * ixgb_setup_rctl - configure the receive control register
818  * @adapter: Board private structure
819  **/
820
821 static void
822 ixgb_setup_rctl(struct ixgb_adapter *adapter)
823 {
824         u32 rctl;
825
826         rctl = IXGB_READ_REG(&adapter->hw, RCTL);
827
828         rctl &= ~(3 << IXGB_RCTL_MO_SHIFT);
829
830         rctl |=
831                 IXGB_RCTL_BAM | IXGB_RCTL_RDMTS_1_2 |
832                 IXGB_RCTL_RXEN | IXGB_RCTL_CFF |
833                 (adapter->hw.mc_filter_type << IXGB_RCTL_MO_SHIFT);
834
835         rctl |= IXGB_RCTL_SECRC;
836
837         if (adapter->rx_buffer_len <= IXGB_RXBUFFER_2048)
838                 rctl |= IXGB_RCTL_BSIZE_2048;
839         else if (adapter->rx_buffer_len <= IXGB_RXBUFFER_4096)
840                 rctl |= IXGB_RCTL_BSIZE_4096;
841         else if (adapter->rx_buffer_len <= IXGB_RXBUFFER_8192)
842                 rctl |= IXGB_RCTL_BSIZE_8192;
843         else if (adapter->rx_buffer_len <= IXGB_RXBUFFER_16384)
844                 rctl |= IXGB_RCTL_BSIZE_16384;
845
846         IXGB_WRITE_REG(&adapter->hw, RCTL, rctl);
847 }
848
849 /**
850  * ixgb_configure_rx - Configure 82597 Receive Unit after Reset.
851  * @adapter: board private structure
852  *
853  * Configure the Rx unit of the MAC after a reset.
854  **/
855
856 static void
857 ixgb_configure_rx(struct ixgb_adapter *adapter)
858 {
859         u64 rdba = adapter->rx_ring.dma;
860         u32 rdlen = adapter->rx_ring.count * sizeof(struct ixgb_rx_desc);
861         struct ixgb_hw *hw = &adapter->hw;
862         u32 rctl;
863         u32 rxcsum;
864
865         /* make sure receives are disabled while setting up the descriptors */
866
867         rctl = IXGB_READ_REG(hw, RCTL);
868         IXGB_WRITE_REG(hw, RCTL, rctl & ~IXGB_RCTL_RXEN);
869
870         /* set the Receive Delay Timer Register */
871
872         IXGB_WRITE_REG(hw, RDTR, adapter->rx_int_delay);
873
874         /* Setup the Base and Length of the Rx Descriptor Ring */
875
876         IXGB_WRITE_REG(hw, RDBAL, (rdba & 0x00000000ffffffffULL));
877         IXGB_WRITE_REG(hw, RDBAH, (rdba >> 32));
878
879         IXGB_WRITE_REG(hw, RDLEN, rdlen);
880
881         /* Setup the HW Rx Head and Tail Descriptor Pointers */
882         IXGB_WRITE_REG(hw, RDH, 0);
883         IXGB_WRITE_REG(hw, RDT, 0);
884
885         /* due to the hardware errata with RXDCTL, we are unable to use any of
886          * the performance enhancing features of it without causing other
887          * subtle bugs, some of the bugs could include receive length
888          * corruption at high data rates (WTHRESH > 0) and/or receive
889          * descriptor ring irregularites (particularly in hardware cache) */
890         IXGB_WRITE_REG(hw, RXDCTL, 0);
891
892         /* Enable Receive Checksum Offload for TCP and UDP */
893         if (adapter->rx_csum) {
894                 rxcsum = IXGB_READ_REG(hw, RXCSUM);
895                 rxcsum |= IXGB_RXCSUM_TUOFL;
896                 IXGB_WRITE_REG(hw, RXCSUM, rxcsum);
897         }
898
899         /* Enable Receives */
900
901         IXGB_WRITE_REG(hw, RCTL, rctl);
902 }
903
904 /**
905  * ixgb_free_tx_resources - Free Tx Resources
906  * @adapter: board private structure
907  *
908  * Free all transmit software resources
909  **/
910
911 void
912 ixgb_free_tx_resources(struct ixgb_adapter *adapter)
913 {
914         struct pci_dev *pdev = adapter->pdev;
915
916         ixgb_clean_tx_ring(adapter);
917
918         vfree(adapter->tx_ring.buffer_info);
919         adapter->tx_ring.buffer_info = NULL;
920
921         dma_free_coherent(&pdev->dev, adapter->tx_ring.size,
922                           adapter->tx_ring.desc, adapter->tx_ring.dma);
923
924         adapter->tx_ring.desc = NULL;
925 }
926
927 static void
928 ixgb_unmap_and_free_tx_resource(struct ixgb_adapter *adapter,
929                                 struct ixgb_buffer *buffer_info)
930 {
931         if (buffer_info->dma) {
932                 if (buffer_info->mapped_as_page)
933                         dma_unmap_page(&adapter->pdev->dev, buffer_info->dma,
934                                        buffer_info->length, DMA_TO_DEVICE);
935                 else
936                         dma_unmap_single(&adapter->pdev->dev, buffer_info->dma,
937                                          buffer_info->length, DMA_TO_DEVICE);
938                 buffer_info->dma = 0;
939         }
940
941         if (buffer_info->skb) {
942                 dev_kfree_skb_any(buffer_info->skb);
943                 buffer_info->skb = NULL;
944         }
945         buffer_info->time_stamp = 0;
946         /* these fields must always be initialized in tx
947          * buffer_info->length = 0;
948          * buffer_info->next_to_watch = 0; */
949 }
950
951 /**
952  * ixgb_clean_tx_ring - Free Tx Buffers
953  * @adapter: board private structure
954  **/
955
956 static void
957 ixgb_clean_tx_ring(struct ixgb_adapter *adapter)
958 {
959         struct ixgb_desc_ring *tx_ring = &adapter->tx_ring;
960         struct ixgb_buffer *buffer_info;
961         unsigned long size;
962         unsigned int i;
963
964         /* Free all the Tx ring sk_buffs */
965
966         for (i = 0; i < tx_ring->count; i++) {
967                 buffer_info = &tx_ring->buffer_info[i];
968                 ixgb_unmap_and_free_tx_resource(adapter, buffer_info);
969         }
970
971         size = sizeof(struct ixgb_buffer) * tx_ring->count;
972         memset(tx_ring->buffer_info, 0, size);
973
974         /* Zero out the descriptor ring */
975
976         memset(tx_ring->desc, 0, tx_ring->size);
977
978         tx_ring->next_to_use = 0;
979         tx_ring->next_to_clean = 0;
980
981         IXGB_WRITE_REG(&adapter->hw, TDH, 0);
982         IXGB_WRITE_REG(&adapter->hw, TDT, 0);
983 }
984
985 /**
986  * ixgb_free_rx_resources - Free Rx Resources
987  * @adapter: board private structure
988  *
989  * Free all receive software resources
990  **/
991
992 void
993 ixgb_free_rx_resources(struct ixgb_adapter *adapter)
994 {
995         struct ixgb_desc_ring *rx_ring = &adapter->rx_ring;
996         struct pci_dev *pdev = adapter->pdev;
997
998         ixgb_clean_rx_ring(adapter);
999
1000         vfree(rx_ring->buffer_info);
1001         rx_ring->buffer_info = NULL;
1002
1003         dma_free_coherent(&pdev->dev, rx_ring->size, rx_ring->desc,
1004                           rx_ring->dma);
1005
1006         rx_ring->desc = NULL;
1007 }
1008
1009 /**
1010  * ixgb_clean_rx_ring - Free Rx Buffers
1011  * @adapter: board private structure
1012  **/
1013
1014 static void
1015 ixgb_clean_rx_ring(struct ixgb_adapter *adapter)
1016 {
1017         struct ixgb_desc_ring *rx_ring = &adapter->rx_ring;
1018         struct ixgb_buffer *buffer_info;
1019         struct pci_dev *pdev = adapter->pdev;
1020         unsigned long size;
1021         unsigned int i;
1022
1023         /* Free all the Rx ring sk_buffs */
1024
1025         for (i = 0; i < rx_ring->count; i++) {
1026                 buffer_info = &rx_ring->buffer_info[i];
1027                 if (buffer_info->dma) {
1028                         dma_unmap_single(&pdev->dev,
1029                                          buffer_info->dma,
1030                                          buffer_info->length,
1031                                          DMA_FROM_DEVICE);
1032                         buffer_info->dma = 0;
1033                         buffer_info->length = 0;
1034                 }
1035
1036                 if (buffer_info->skb) {
1037                         dev_kfree_skb(buffer_info->skb);
1038                         buffer_info->skb = NULL;
1039                 }
1040         }
1041
1042         size = sizeof(struct ixgb_buffer) * rx_ring->count;
1043         memset(rx_ring->buffer_info, 0, size);
1044
1045         /* Zero out the descriptor ring */
1046
1047         memset(rx_ring->desc, 0, rx_ring->size);
1048
1049         rx_ring->next_to_clean = 0;
1050         rx_ring->next_to_use = 0;
1051
1052         IXGB_WRITE_REG(&adapter->hw, RDH, 0);
1053         IXGB_WRITE_REG(&adapter->hw, RDT, 0);
1054 }
1055
1056 /**
1057  * ixgb_set_mac - Change the Ethernet Address of the NIC
1058  * @netdev: network interface device structure
1059  * @p: pointer to an address structure
1060  *
1061  * Returns 0 on success, negative on failure
1062  **/
1063
1064 static int
1065 ixgb_set_mac(struct net_device *netdev, void *p)
1066 {
1067         struct ixgb_adapter *adapter = netdev_priv(netdev);
1068         struct sockaddr *addr = p;
1069
1070         if (!is_valid_ether_addr(addr->sa_data))
1071                 return -EADDRNOTAVAIL;
1072
1073         memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
1074
1075         ixgb_rar_set(&adapter->hw, addr->sa_data, 0);
1076
1077         return 0;
1078 }
1079
1080 /**
1081  * ixgb_set_multi - Multicast and Promiscuous mode set
1082  * @netdev: network interface device structure
1083  *
1084  * The set_multi entry point is called whenever the multicast address
1085  * list or the network interface flags are updated.  This routine is
1086  * responsible for configuring the hardware for proper multicast,
1087  * promiscuous mode, and all-multi behavior.
1088  **/
1089
1090 static void
1091 ixgb_set_multi(struct net_device *netdev)
1092 {
1093         struct ixgb_adapter *adapter = netdev_priv(netdev);
1094         struct ixgb_hw *hw = &adapter->hw;
1095         struct netdev_hw_addr *ha;
1096         u32 rctl;
1097
1098         /* Check for Promiscuous and All Multicast modes */
1099
1100         rctl = IXGB_READ_REG(hw, RCTL);
1101
1102         if (netdev->flags & IFF_PROMISC) {
1103                 rctl |= (IXGB_RCTL_UPE | IXGB_RCTL_MPE);
1104                 /* disable VLAN filtering */
1105                 rctl &= ~IXGB_RCTL_CFIEN;
1106                 rctl &= ~IXGB_RCTL_VFE;
1107         } else {
1108                 if (netdev->flags & IFF_ALLMULTI) {
1109                         rctl |= IXGB_RCTL_MPE;
1110                         rctl &= ~IXGB_RCTL_UPE;
1111                 } else {
1112                         rctl &= ~(IXGB_RCTL_UPE | IXGB_RCTL_MPE);
1113                 }
1114                 /* enable VLAN filtering */
1115                 rctl |= IXGB_RCTL_VFE;
1116                 rctl &= ~IXGB_RCTL_CFIEN;
1117         }
1118
1119         if (netdev_mc_count(netdev) > IXGB_MAX_NUM_MULTICAST_ADDRESSES) {
1120                 rctl |= IXGB_RCTL_MPE;
1121                 IXGB_WRITE_REG(hw, RCTL, rctl);
1122         } else {
1123                 u8 *mta = kmalloc(IXGB_MAX_NUM_MULTICAST_ADDRESSES *
1124                               ETH_ALEN, GFP_ATOMIC);
1125                 u8 *addr;
1126                 if (!mta)
1127                         goto alloc_failed;
1128
1129                 IXGB_WRITE_REG(hw, RCTL, rctl);
1130
1131                 addr = mta;
1132                 netdev_for_each_mc_addr(ha, netdev) {
1133                         memcpy(addr, ha->addr, ETH_ALEN);
1134                         addr += ETH_ALEN;
1135                 }
1136
1137                 ixgb_mc_addr_list_update(hw, mta, netdev_mc_count(netdev), 0);
1138                 kfree(mta);
1139         }
1140
1141 alloc_failed:
1142         if (netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
1143                 ixgb_vlan_strip_enable(adapter);
1144         else
1145                 ixgb_vlan_strip_disable(adapter);
1146
1147 }
1148
1149 /**
1150  * ixgb_watchdog - Timer Call-back
1151  * @data: pointer to netdev cast into an unsigned long
1152  **/
1153
1154 static void
1155 ixgb_watchdog(unsigned long data)
1156 {
1157         struct ixgb_adapter *adapter = (struct ixgb_adapter *)data;
1158         struct net_device *netdev = adapter->netdev;
1159         struct ixgb_desc_ring *txdr = &adapter->tx_ring;
1160
1161         ixgb_check_for_link(&adapter->hw);
1162
1163         if (ixgb_check_for_bad_link(&adapter->hw)) {
1164                 /* force the reset path */
1165                 netif_stop_queue(netdev);
1166         }
1167
1168         if (adapter->hw.link_up) {
1169                 if (!netif_carrier_ok(netdev)) {
1170                         netdev_info(netdev,
1171                                     "NIC Link is Up 10 Gbps Full Duplex, Flow Control: %s\n",
1172                                     (adapter->hw.fc.type == ixgb_fc_full) ?
1173                                     "RX/TX" :
1174                                     (adapter->hw.fc.type == ixgb_fc_rx_pause) ?
1175                                      "RX" :
1176                                     (adapter->hw.fc.type == ixgb_fc_tx_pause) ?
1177                                     "TX" : "None");
1178                         adapter->link_speed = 10000;
1179                         adapter->link_duplex = FULL_DUPLEX;
1180                         netif_carrier_on(netdev);
1181                 }
1182         } else {
1183                 if (netif_carrier_ok(netdev)) {
1184                         adapter->link_speed = 0;
1185                         adapter->link_duplex = 0;
1186                         netdev_info(netdev, "NIC Link is Down\n");
1187                         netif_carrier_off(netdev);
1188                 }
1189         }
1190
1191         ixgb_update_stats(adapter);
1192
1193         if (!netif_carrier_ok(netdev)) {
1194                 if (IXGB_DESC_UNUSED(txdr) + 1 < txdr->count) {
1195                         /* We've lost link, so the controller stops DMA,
1196                          * but we've got queued Tx work that's never going
1197                          * to get done, so reset controller to flush Tx.
1198                          * (Do the reset outside of interrupt context). */
1199                         schedule_work(&adapter->tx_timeout_task);
1200                         /* return immediately since reset is imminent */
1201                         return;
1202                 }
1203         }
1204
1205         /* Force detection of hung controller every watchdog period */
1206         adapter->detect_tx_hung = true;
1207
1208         /* generate an interrupt to force clean up of any stragglers */
1209         IXGB_WRITE_REG(&adapter->hw, ICS, IXGB_INT_TXDW);
1210
1211         /* Reset the timer */
1212         mod_timer(&adapter->watchdog_timer, jiffies + 2 * HZ);
1213 }
1214
1215 #define IXGB_TX_FLAGS_CSUM              0x00000001
1216 #define IXGB_TX_FLAGS_VLAN              0x00000002
1217 #define IXGB_TX_FLAGS_TSO               0x00000004
1218
1219 static int
1220 ixgb_tso(struct ixgb_adapter *adapter, struct sk_buff *skb)
1221 {
1222         struct ixgb_context_desc *context_desc;
1223         unsigned int i;
1224         u8 ipcss, ipcso, tucss, tucso, hdr_len;
1225         u16 ipcse, tucse, mss;
1226
1227         if (likely(skb_is_gso(skb))) {
1228                 struct ixgb_buffer *buffer_info;
1229                 struct iphdr *iph;
1230                 int err;
1231
1232                 err = skb_cow_head(skb, 0);
1233                 if (err < 0)
1234                         return err;
1235
1236                 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
1237                 mss = skb_shinfo(skb)->gso_size;
1238                 iph = ip_hdr(skb);
1239                 iph->tot_len = 0;
1240                 iph->check = 0;
1241                 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
1242                                                          iph->daddr, 0,
1243                                                          IPPROTO_TCP, 0);
1244                 ipcss = skb_network_offset(skb);
1245                 ipcso = (void *)&(iph->check) - (void *)skb->data;
1246                 ipcse = skb_transport_offset(skb) - 1;
1247                 tucss = skb_transport_offset(skb);
1248                 tucso = (void *)&(tcp_hdr(skb)->check) - (void *)skb->data;
1249                 tucse = 0;
1250
1251                 i = adapter->tx_ring.next_to_use;
1252                 context_desc = IXGB_CONTEXT_DESC(adapter->tx_ring, i);
1253                 buffer_info = &adapter->tx_ring.buffer_info[i];
1254                 WARN_ON(buffer_info->dma != 0);
1255
1256                 context_desc->ipcss = ipcss;
1257                 context_desc->ipcso = ipcso;
1258                 context_desc->ipcse = cpu_to_le16(ipcse);
1259                 context_desc->tucss = tucss;
1260                 context_desc->tucso = tucso;
1261                 context_desc->tucse = cpu_to_le16(tucse);
1262                 context_desc->mss = cpu_to_le16(mss);
1263                 context_desc->hdr_len = hdr_len;
1264                 context_desc->status = 0;
1265                 context_desc->cmd_type_len = cpu_to_le32(
1266                                                   IXGB_CONTEXT_DESC_TYPE
1267                                                 | IXGB_CONTEXT_DESC_CMD_TSE
1268                                                 | IXGB_CONTEXT_DESC_CMD_IP
1269                                                 | IXGB_CONTEXT_DESC_CMD_TCP
1270                                                 | IXGB_CONTEXT_DESC_CMD_IDE
1271                                                 | (skb->len - (hdr_len)));
1272
1273
1274                 if (++i == adapter->tx_ring.count) i = 0;
1275                 adapter->tx_ring.next_to_use = i;
1276
1277                 return 1;
1278         }
1279
1280         return 0;
1281 }
1282
1283 static bool
1284 ixgb_tx_csum(struct ixgb_adapter *adapter, struct sk_buff *skb)
1285 {
1286         struct ixgb_context_desc *context_desc;
1287         unsigned int i;
1288         u8 css, cso;
1289
1290         if (likely(skb->ip_summed == CHECKSUM_PARTIAL)) {
1291                 struct ixgb_buffer *buffer_info;
1292                 css = skb_checksum_start_offset(skb);
1293                 cso = css + skb->csum_offset;
1294
1295                 i = adapter->tx_ring.next_to_use;
1296                 context_desc = IXGB_CONTEXT_DESC(adapter->tx_ring, i);
1297                 buffer_info = &adapter->tx_ring.buffer_info[i];
1298                 WARN_ON(buffer_info->dma != 0);
1299
1300                 context_desc->tucss = css;
1301                 context_desc->tucso = cso;
1302                 context_desc->tucse = 0;
1303                 /* zero out any previously existing data in one instruction */
1304                 *(u32 *)&(context_desc->ipcss) = 0;
1305                 context_desc->status = 0;
1306                 context_desc->hdr_len = 0;
1307                 context_desc->mss = 0;
1308                 context_desc->cmd_type_len =
1309                         cpu_to_le32(IXGB_CONTEXT_DESC_TYPE
1310                                     | IXGB_TX_DESC_CMD_IDE);
1311
1312                 if (++i == adapter->tx_ring.count) i = 0;
1313                 adapter->tx_ring.next_to_use = i;
1314
1315                 return true;
1316         }
1317
1318         return false;
1319 }
1320
1321 #define IXGB_MAX_TXD_PWR        14
1322 #define IXGB_MAX_DATA_PER_TXD   (1<<IXGB_MAX_TXD_PWR)
1323
1324 static int
1325 ixgb_tx_map(struct ixgb_adapter *adapter, struct sk_buff *skb,
1326             unsigned int first)
1327 {
1328         struct ixgb_desc_ring *tx_ring = &adapter->tx_ring;
1329         struct pci_dev *pdev = adapter->pdev;
1330         struct ixgb_buffer *buffer_info;
1331         int len = skb_headlen(skb);
1332         unsigned int offset = 0, size, count = 0, i;
1333         unsigned int mss = skb_shinfo(skb)->gso_size;
1334         unsigned int nr_frags = skb_shinfo(skb)->nr_frags;
1335         unsigned int f;
1336
1337         i = tx_ring->next_to_use;
1338
1339         while (len) {
1340                 buffer_info = &tx_ring->buffer_info[i];
1341                 size = min(len, IXGB_MAX_DATA_PER_TXD);
1342                 /* Workaround for premature desc write-backs
1343                  * in TSO mode.  Append 4-byte sentinel desc */
1344                 if (unlikely(mss && !nr_frags && size == len && size > 8))
1345                         size -= 4;
1346
1347                 buffer_info->length = size;
1348                 WARN_ON(buffer_info->dma != 0);
1349                 buffer_info->time_stamp = jiffies;
1350                 buffer_info->mapped_as_page = false;
1351                 buffer_info->dma = dma_map_single(&pdev->dev,
1352                                                   skb->data + offset,
1353                                                   size, DMA_TO_DEVICE);
1354                 if (dma_mapping_error(&pdev->dev, buffer_info->dma))
1355                         goto dma_error;
1356                 buffer_info->next_to_watch = 0;
1357
1358                 len -= size;
1359                 offset += size;
1360                 count++;
1361                 if (len) {
1362                         i++;
1363                         if (i == tx_ring->count)
1364                                 i = 0;
1365                 }
1366         }
1367
1368         for (f = 0; f < nr_frags; f++) {
1369                 const struct skb_frag_struct *frag;
1370
1371                 frag = &skb_shinfo(skb)->frags[f];
1372                 len = skb_frag_size(frag);
1373                 offset = 0;
1374
1375                 while (len) {
1376                         i++;
1377                         if (i == tx_ring->count)
1378                                 i = 0;
1379
1380                         buffer_info = &tx_ring->buffer_info[i];
1381                         size = min(len, IXGB_MAX_DATA_PER_TXD);
1382
1383                         /* Workaround for premature desc write-backs
1384                          * in TSO mode.  Append 4-byte sentinel desc */
1385                         if (unlikely(mss && (f == (nr_frags - 1))
1386                                      && size == len && size > 8))
1387                                 size -= 4;
1388
1389                         buffer_info->length = size;
1390                         buffer_info->time_stamp = jiffies;
1391                         buffer_info->mapped_as_page = true;
1392                         buffer_info->dma =
1393                                 skb_frag_dma_map(&pdev->dev, frag, offset, size,
1394                                                  DMA_TO_DEVICE);
1395                         if (dma_mapping_error(&pdev->dev, buffer_info->dma))
1396                                 goto dma_error;
1397                         buffer_info->next_to_watch = 0;
1398
1399                         len -= size;
1400                         offset += size;
1401                         count++;
1402                 }
1403         }
1404         tx_ring->buffer_info[i].skb = skb;
1405         tx_ring->buffer_info[first].next_to_watch = i;
1406
1407         return count;
1408
1409 dma_error:
1410         dev_err(&pdev->dev, "TX DMA map failed\n");
1411         buffer_info->dma = 0;
1412         if (count)
1413                 count--;
1414
1415         while (count--) {
1416                 if (i==0)
1417                         i += tx_ring->count;
1418                 i--;
1419                 buffer_info = &tx_ring->buffer_info[i];
1420                 ixgb_unmap_and_free_tx_resource(adapter, buffer_info);
1421         }
1422
1423         return 0;
1424 }
1425
1426 static void
1427 ixgb_tx_queue(struct ixgb_adapter *adapter, int count, int vlan_id,int tx_flags)
1428 {
1429         struct ixgb_desc_ring *tx_ring = &adapter->tx_ring;
1430         struct ixgb_tx_desc *tx_desc = NULL;
1431         struct ixgb_buffer *buffer_info;
1432         u32 cmd_type_len = adapter->tx_cmd_type;
1433         u8 status = 0;
1434         u8 popts = 0;
1435         unsigned int i;
1436
1437         if (tx_flags & IXGB_TX_FLAGS_TSO) {
1438                 cmd_type_len |= IXGB_TX_DESC_CMD_TSE;
1439                 popts |= (IXGB_TX_DESC_POPTS_IXSM | IXGB_TX_DESC_POPTS_TXSM);
1440         }
1441
1442         if (tx_flags & IXGB_TX_FLAGS_CSUM)
1443                 popts |= IXGB_TX_DESC_POPTS_TXSM;
1444
1445         if (tx_flags & IXGB_TX_FLAGS_VLAN)
1446                 cmd_type_len |= IXGB_TX_DESC_CMD_VLE;
1447
1448         i = tx_ring->next_to_use;
1449
1450         while (count--) {
1451                 buffer_info = &tx_ring->buffer_info[i];
1452                 tx_desc = IXGB_TX_DESC(*tx_ring, i);
1453                 tx_desc->buff_addr = cpu_to_le64(buffer_info->dma);
1454                 tx_desc->cmd_type_len =
1455                         cpu_to_le32(cmd_type_len | buffer_info->length);
1456                 tx_desc->status = status;
1457                 tx_desc->popts = popts;
1458                 tx_desc->vlan = cpu_to_le16(vlan_id);
1459
1460                 if (++i == tx_ring->count) i = 0;
1461         }
1462
1463         tx_desc->cmd_type_len |=
1464                 cpu_to_le32(IXGB_TX_DESC_CMD_EOP | IXGB_TX_DESC_CMD_RS);
1465
1466         /* Force memory writes to complete before letting h/w
1467          * know there are new descriptors to fetch.  (Only
1468          * applicable for weak-ordered memory model archs,
1469          * such as IA-64). */
1470         wmb();
1471
1472         tx_ring->next_to_use = i;
1473         IXGB_WRITE_REG(&adapter->hw, TDT, i);
1474 }
1475
1476 static int __ixgb_maybe_stop_tx(struct net_device *netdev, int size)
1477 {
1478         struct ixgb_adapter *adapter = netdev_priv(netdev);
1479         struct ixgb_desc_ring *tx_ring = &adapter->tx_ring;
1480
1481         netif_stop_queue(netdev);
1482         /* Herbert's original patch had:
1483          *  smp_mb__after_netif_stop_queue();
1484          * but since that doesn't exist yet, just open code it. */
1485         smp_mb();
1486
1487         /* We need to check again in a case another CPU has just
1488          * made room available. */
1489         if (likely(IXGB_DESC_UNUSED(tx_ring) < size))
1490                 return -EBUSY;
1491
1492         /* A reprieve! */
1493         netif_start_queue(netdev);
1494         ++adapter->restart_queue;
1495         return 0;
1496 }
1497
1498 static int ixgb_maybe_stop_tx(struct net_device *netdev,
1499                               struct ixgb_desc_ring *tx_ring, int size)
1500 {
1501         if (likely(IXGB_DESC_UNUSED(tx_ring) >= size))
1502                 return 0;
1503         return __ixgb_maybe_stop_tx(netdev, size);
1504 }
1505
1506
1507 /* Tx Descriptors needed, worst case */
1508 #define TXD_USE_COUNT(S) (((S) >> IXGB_MAX_TXD_PWR) + \
1509                          (((S) & (IXGB_MAX_DATA_PER_TXD - 1)) ? 1 : 0))
1510 #define DESC_NEEDED TXD_USE_COUNT(IXGB_MAX_DATA_PER_TXD) /* skb->date */ + \
1511         MAX_SKB_FRAGS * TXD_USE_COUNT(PAGE_SIZE) + 1 /* for context */ \
1512         + 1 /* one more needed for sentinel TSO workaround */
1513
1514 static netdev_tx_t
1515 ixgb_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
1516 {
1517         struct ixgb_adapter *adapter = netdev_priv(netdev);
1518         unsigned int first;
1519         unsigned int tx_flags = 0;
1520         int vlan_id = 0;
1521         int count = 0;
1522         int tso;
1523
1524         if (test_bit(__IXGB_DOWN, &adapter->flags)) {
1525                 dev_kfree_skb_any(skb);
1526                 return NETDEV_TX_OK;
1527         }
1528
1529         if (skb->len <= 0) {
1530                 dev_kfree_skb_any(skb);
1531                 return NETDEV_TX_OK;
1532         }
1533
1534         if (unlikely(ixgb_maybe_stop_tx(netdev, &adapter->tx_ring,
1535                      DESC_NEEDED)))
1536                 return NETDEV_TX_BUSY;
1537
1538         if (skb_vlan_tag_present(skb)) {
1539                 tx_flags |= IXGB_TX_FLAGS_VLAN;
1540                 vlan_id = skb_vlan_tag_get(skb);
1541         }
1542
1543         first = adapter->tx_ring.next_to_use;
1544
1545         tso = ixgb_tso(adapter, skb);
1546         if (tso < 0) {
1547                 dev_kfree_skb_any(skb);
1548                 return NETDEV_TX_OK;
1549         }
1550
1551         if (likely(tso))
1552                 tx_flags |= IXGB_TX_FLAGS_TSO;
1553         else if (ixgb_tx_csum(adapter, skb))
1554                 tx_flags |= IXGB_TX_FLAGS_CSUM;
1555
1556         count = ixgb_tx_map(adapter, skb, first);
1557
1558         if (count) {
1559                 ixgb_tx_queue(adapter, count, vlan_id, tx_flags);
1560                 /* Make sure there is space in the ring for the next send. */
1561                 ixgb_maybe_stop_tx(netdev, &adapter->tx_ring, DESC_NEEDED);
1562
1563         } else {
1564                 dev_kfree_skb_any(skb);
1565                 adapter->tx_ring.buffer_info[first].time_stamp = 0;
1566                 adapter->tx_ring.next_to_use = first;
1567         }
1568
1569         return NETDEV_TX_OK;
1570 }
1571
1572 /**
1573  * ixgb_tx_timeout - Respond to a Tx Hang
1574  * @netdev: network interface device structure
1575  **/
1576
1577 static void
1578 ixgb_tx_timeout(struct net_device *netdev)
1579 {
1580         struct ixgb_adapter *adapter = netdev_priv(netdev);
1581
1582         /* Do the reset outside of interrupt context */
1583         schedule_work(&adapter->tx_timeout_task);
1584 }
1585
1586 static void
1587 ixgb_tx_timeout_task(struct work_struct *work)
1588 {
1589         struct ixgb_adapter *adapter =
1590                 container_of(work, struct ixgb_adapter, tx_timeout_task);
1591
1592         adapter->tx_timeout_count++;
1593         ixgb_down(adapter, true);
1594         ixgb_up(adapter);
1595 }
1596
1597 /**
1598  * ixgb_change_mtu - Change the Maximum Transfer Unit
1599  * @netdev: network interface device structure
1600  * @new_mtu: new value for maximum frame size
1601  *
1602  * Returns 0 on success, negative on failure
1603  **/
1604
1605 static int
1606 ixgb_change_mtu(struct net_device *netdev, int new_mtu)
1607 {
1608         struct ixgb_adapter *adapter = netdev_priv(netdev);
1609         int max_frame = new_mtu + ENET_HEADER_SIZE + ENET_FCS_LENGTH;
1610
1611         if (netif_running(netdev))
1612                 ixgb_down(adapter, true);
1613
1614         adapter->rx_buffer_len = max_frame + 8; /* + 8 for errata */
1615
1616         netdev->mtu = new_mtu;
1617
1618         if (netif_running(netdev))
1619                 ixgb_up(adapter);
1620
1621         return 0;
1622 }
1623
1624 /**
1625  * ixgb_update_stats - Update the board statistics counters.
1626  * @adapter: board private structure
1627  **/
1628
1629 void
1630 ixgb_update_stats(struct ixgb_adapter *adapter)
1631 {
1632         struct net_device *netdev = adapter->netdev;
1633         struct pci_dev *pdev = adapter->pdev;
1634
1635         /* Prevent stats update while adapter is being reset */
1636         if (pci_channel_offline(pdev))
1637                 return;
1638
1639         if ((netdev->flags & IFF_PROMISC) || (netdev->flags & IFF_ALLMULTI) ||
1640            (netdev_mc_count(netdev) > IXGB_MAX_NUM_MULTICAST_ADDRESSES)) {
1641                 u64 multi = IXGB_READ_REG(&adapter->hw, MPRCL);
1642                 u32 bcast_l = IXGB_READ_REG(&adapter->hw, BPRCL);
1643                 u32 bcast_h = IXGB_READ_REG(&adapter->hw, BPRCH);
1644                 u64 bcast = ((u64)bcast_h << 32) | bcast_l;
1645
1646                 multi |= ((u64)IXGB_READ_REG(&adapter->hw, MPRCH) << 32);
1647                 /* fix up multicast stats by removing broadcasts */
1648                 if (multi >= bcast)
1649                         multi -= bcast;
1650
1651                 adapter->stats.mprcl += (multi & 0xFFFFFFFF);
1652                 adapter->stats.mprch += (multi >> 32);
1653                 adapter->stats.bprcl += bcast_l;
1654                 adapter->stats.bprch += bcast_h;
1655         } else {
1656                 adapter->stats.mprcl += IXGB_READ_REG(&adapter->hw, MPRCL);
1657                 adapter->stats.mprch += IXGB_READ_REG(&adapter->hw, MPRCH);
1658                 adapter->stats.bprcl += IXGB_READ_REG(&adapter->hw, BPRCL);
1659                 adapter->stats.bprch += IXGB_READ_REG(&adapter->hw, BPRCH);
1660         }
1661         adapter->stats.tprl += IXGB_READ_REG(&adapter->hw, TPRL);
1662         adapter->stats.tprh += IXGB_READ_REG(&adapter->hw, TPRH);
1663         adapter->stats.gprcl += IXGB_READ_REG(&adapter->hw, GPRCL);
1664         adapter->stats.gprch += IXGB_READ_REG(&adapter->hw, GPRCH);
1665         adapter->stats.uprcl += IXGB_READ_REG(&adapter->hw, UPRCL);
1666         adapter->stats.uprch += IXGB_READ_REG(&adapter->hw, UPRCH);
1667         adapter->stats.vprcl += IXGB_READ_REG(&adapter->hw, VPRCL);
1668         adapter->stats.vprch += IXGB_READ_REG(&adapter->hw, VPRCH);
1669         adapter->stats.jprcl += IXGB_READ_REG(&adapter->hw, JPRCL);
1670         adapter->stats.jprch += IXGB_READ_REG(&adapter->hw, JPRCH);
1671         adapter->stats.gorcl += IXGB_READ_REG(&adapter->hw, GORCL);
1672         adapter->stats.gorch += IXGB_READ_REG(&adapter->hw, GORCH);
1673         adapter->stats.torl += IXGB_READ_REG(&adapter->hw, TORL);
1674         adapter->stats.torh += IXGB_READ_REG(&adapter->hw, TORH);
1675         adapter->stats.rnbc += IXGB_READ_REG(&adapter->hw, RNBC);
1676         adapter->stats.ruc += IXGB_READ_REG(&adapter->hw, RUC);
1677         adapter->stats.roc += IXGB_READ_REG(&adapter->hw, ROC);
1678         adapter->stats.rlec += IXGB_READ_REG(&adapter->hw, RLEC);
1679         adapter->stats.crcerrs += IXGB_READ_REG(&adapter->hw, CRCERRS);
1680         adapter->stats.icbc += IXGB_READ_REG(&adapter->hw, ICBC);
1681         adapter->stats.ecbc += IXGB_READ_REG(&adapter->hw, ECBC);
1682         adapter->stats.mpc += IXGB_READ_REG(&adapter->hw, MPC);
1683         adapter->stats.tptl += IXGB_READ_REG(&adapter->hw, TPTL);
1684         adapter->stats.tpth += IXGB_READ_REG(&adapter->hw, TPTH);
1685         adapter->stats.gptcl += IXGB_READ_REG(&adapter->hw, GPTCL);
1686         adapter->stats.gptch += IXGB_READ_REG(&adapter->hw, GPTCH);
1687         adapter->stats.bptcl += IXGB_READ_REG(&adapter->hw, BPTCL);
1688         adapter->stats.bptch += IXGB_READ_REG(&adapter->hw, BPTCH);
1689         adapter->stats.mptcl += IXGB_READ_REG(&adapter->hw, MPTCL);
1690         adapter->stats.mptch += IXGB_READ_REG(&adapter->hw, MPTCH);
1691         adapter->stats.uptcl += IXGB_READ_REG(&adapter->hw, UPTCL);
1692         adapter->stats.uptch += IXGB_READ_REG(&adapter->hw, UPTCH);
1693         adapter->stats.vptcl += IXGB_READ_REG(&adapter->hw, VPTCL);
1694         adapter->stats.vptch += IXGB_READ_REG(&adapter->hw, VPTCH);
1695         adapter->stats.jptcl += IXGB_READ_REG(&adapter->hw, JPTCL);
1696         adapter->stats.jptch += IXGB_READ_REG(&adapter->hw, JPTCH);
1697         adapter->stats.gotcl += IXGB_READ_REG(&adapter->hw, GOTCL);
1698         adapter->stats.gotch += IXGB_READ_REG(&adapter->hw, GOTCH);
1699         adapter->stats.totl += IXGB_READ_REG(&adapter->hw, TOTL);
1700         adapter->stats.toth += IXGB_READ_REG(&adapter->hw, TOTH);
1701         adapter->stats.dc += IXGB_READ_REG(&adapter->hw, DC);
1702         adapter->stats.plt64c += IXGB_READ_REG(&adapter->hw, PLT64C);
1703         adapter->stats.tsctc += IXGB_READ_REG(&adapter->hw, TSCTC);
1704         adapter->stats.tsctfc += IXGB_READ_REG(&adapter->hw, TSCTFC);
1705         adapter->stats.ibic += IXGB_READ_REG(&adapter->hw, IBIC);
1706         adapter->stats.rfc += IXGB_READ_REG(&adapter->hw, RFC);
1707         adapter->stats.lfc += IXGB_READ_REG(&adapter->hw, LFC);
1708         adapter->stats.pfrc += IXGB_READ_REG(&adapter->hw, PFRC);
1709         adapter->stats.pftc += IXGB_READ_REG(&adapter->hw, PFTC);
1710         adapter->stats.mcfrc += IXGB_READ_REG(&adapter->hw, MCFRC);
1711         adapter->stats.mcftc += IXGB_READ_REG(&adapter->hw, MCFTC);
1712         adapter->stats.xonrxc += IXGB_READ_REG(&adapter->hw, XONRXC);
1713         adapter->stats.xontxc += IXGB_READ_REG(&adapter->hw, XONTXC);
1714         adapter->stats.xoffrxc += IXGB_READ_REG(&adapter->hw, XOFFRXC);
1715         adapter->stats.xofftxc += IXGB_READ_REG(&adapter->hw, XOFFTXC);
1716         adapter->stats.rjc += IXGB_READ_REG(&adapter->hw, RJC);
1717
1718         /* Fill out the OS statistics structure */
1719
1720         netdev->stats.rx_packets = adapter->stats.gprcl;
1721         netdev->stats.tx_packets = adapter->stats.gptcl;
1722         netdev->stats.rx_bytes = adapter->stats.gorcl;
1723         netdev->stats.tx_bytes = adapter->stats.gotcl;
1724         netdev->stats.multicast = adapter->stats.mprcl;
1725         netdev->stats.collisions = 0;
1726
1727         /* ignore RLEC as it reports errors for padded (<64bytes) frames
1728          * with a length in the type/len field */
1729         netdev->stats.rx_errors =
1730             /* adapter->stats.rnbc + */ adapter->stats.crcerrs +
1731             adapter->stats.ruc +
1732             adapter->stats.roc /*+ adapter->stats.rlec */  +
1733             adapter->stats.icbc +
1734             adapter->stats.ecbc + adapter->stats.mpc;
1735
1736         /* see above
1737          * netdev->stats.rx_length_errors = adapter->stats.rlec;
1738          */
1739
1740         netdev->stats.rx_crc_errors = adapter->stats.crcerrs;
1741         netdev->stats.rx_fifo_errors = adapter->stats.mpc;
1742         netdev->stats.rx_missed_errors = adapter->stats.mpc;
1743         netdev->stats.rx_over_errors = adapter->stats.mpc;
1744
1745         netdev->stats.tx_errors = 0;
1746         netdev->stats.rx_frame_errors = 0;
1747         netdev->stats.tx_aborted_errors = 0;
1748         netdev->stats.tx_carrier_errors = 0;
1749         netdev->stats.tx_fifo_errors = 0;
1750         netdev->stats.tx_heartbeat_errors = 0;
1751         netdev->stats.tx_window_errors = 0;
1752 }
1753
1754 #define IXGB_MAX_INTR 10
1755 /**
1756  * ixgb_intr - Interrupt Handler
1757  * @irq: interrupt number
1758  * @data: pointer to a network interface device structure
1759  **/
1760
1761 static irqreturn_t
1762 ixgb_intr(int irq, void *data)
1763 {
1764         struct net_device *netdev = data;
1765         struct ixgb_adapter *adapter = netdev_priv(netdev);
1766         struct ixgb_hw *hw = &adapter->hw;
1767         u32 icr = IXGB_READ_REG(hw, ICR);
1768
1769         if (unlikely(!icr))
1770                 return IRQ_NONE;  /* Not our interrupt */
1771
1772         if (unlikely(icr & (IXGB_INT_RXSEQ | IXGB_INT_LSC)))
1773                 if (!test_bit(__IXGB_DOWN, &adapter->flags))
1774                         mod_timer(&adapter->watchdog_timer, jiffies);
1775
1776         if (napi_schedule_prep(&adapter->napi)) {
1777
1778                 /* Disable interrupts and register for poll. The flush
1779                   of the posted write is intentionally left out.
1780                 */
1781
1782                 IXGB_WRITE_REG(&adapter->hw, IMC, ~0);
1783                 __napi_schedule(&adapter->napi);
1784         }
1785         return IRQ_HANDLED;
1786 }
1787
1788 /**
1789  * ixgb_clean - NAPI Rx polling callback
1790  * @adapter: board private structure
1791  **/
1792
1793 static int
1794 ixgb_clean(struct napi_struct *napi, int budget)
1795 {
1796         struct ixgb_adapter *adapter = container_of(napi, struct ixgb_adapter, napi);
1797         int work_done = 0;
1798
1799         ixgb_clean_tx_irq(adapter);
1800         ixgb_clean_rx_irq(adapter, &work_done, budget);
1801
1802         /* If budget not fully consumed, exit the polling mode */
1803         if (work_done < budget) {
1804                 napi_complete_done(napi, work_done);
1805                 if (!test_bit(__IXGB_DOWN, &adapter->flags))
1806                         ixgb_irq_enable(adapter);
1807         }
1808
1809         return work_done;
1810 }
1811
1812 /**
1813  * ixgb_clean_tx_irq - Reclaim resources after transmit completes
1814  * @adapter: board private structure
1815  **/
1816
1817 static bool
1818 ixgb_clean_tx_irq(struct ixgb_adapter *adapter)
1819 {
1820         struct ixgb_desc_ring *tx_ring = &adapter->tx_ring;
1821         struct net_device *netdev = adapter->netdev;
1822         struct ixgb_tx_desc *tx_desc, *eop_desc;
1823         struct ixgb_buffer *buffer_info;
1824         unsigned int i, eop;
1825         bool cleaned = false;
1826
1827         i = tx_ring->next_to_clean;
1828         eop = tx_ring->buffer_info[i].next_to_watch;
1829         eop_desc = IXGB_TX_DESC(*tx_ring, eop);
1830
1831         while (eop_desc->status & IXGB_TX_DESC_STATUS_DD) {
1832
1833                 rmb(); /* read buffer_info after eop_desc */
1834                 for (cleaned = false; !cleaned; ) {
1835                         tx_desc = IXGB_TX_DESC(*tx_ring, i);
1836                         buffer_info = &tx_ring->buffer_info[i];
1837
1838                         if (tx_desc->popts &
1839                            (IXGB_TX_DESC_POPTS_TXSM |
1840                             IXGB_TX_DESC_POPTS_IXSM))
1841                                 adapter->hw_csum_tx_good++;
1842
1843                         ixgb_unmap_and_free_tx_resource(adapter, buffer_info);
1844
1845                         *(u32 *)&(tx_desc->status) = 0;
1846
1847                         cleaned = (i == eop);
1848                         if (++i == tx_ring->count) i = 0;
1849                 }
1850
1851                 eop = tx_ring->buffer_info[i].next_to_watch;
1852                 eop_desc = IXGB_TX_DESC(*tx_ring, eop);
1853         }
1854
1855         tx_ring->next_to_clean = i;
1856
1857         if (unlikely(cleaned && netif_carrier_ok(netdev) &&
1858                      IXGB_DESC_UNUSED(tx_ring) >= DESC_NEEDED)) {
1859                 /* Make sure that anybody stopping the queue after this
1860                  * sees the new next_to_clean. */
1861                 smp_mb();
1862
1863                 if (netif_queue_stopped(netdev) &&
1864                     !(test_bit(__IXGB_DOWN, &adapter->flags))) {
1865                         netif_wake_queue(netdev);
1866                         ++adapter->restart_queue;
1867                 }
1868         }
1869
1870         if (adapter->detect_tx_hung) {
1871                 /* detect a transmit hang in hardware, this serializes the
1872                  * check with the clearing of time_stamp and movement of i */
1873                 adapter->detect_tx_hung = false;
1874                 if (tx_ring->buffer_info[eop].time_stamp &&
1875                    time_after(jiffies, tx_ring->buffer_info[eop].time_stamp + HZ)
1876                    && !(IXGB_READ_REG(&adapter->hw, STATUS) &
1877                         IXGB_STATUS_TXOFF)) {
1878                         /* detected Tx unit hang */
1879                         netif_err(adapter, drv, adapter->netdev,
1880                                   "Detected Tx Unit Hang\n"
1881                                   "  TDH                  <%x>\n"
1882                                   "  TDT                  <%x>\n"
1883                                   "  next_to_use          <%x>\n"
1884                                   "  next_to_clean        <%x>\n"
1885                                   "buffer_info[next_to_clean]\n"
1886                                   "  time_stamp           <%lx>\n"
1887                                   "  next_to_watch        <%x>\n"
1888                                   "  jiffies              <%lx>\n"
1889                                   "  next_to_watch.status <%x>\n",
1890                                   IXGB_READ_REG(&adapter->hw, TDH),
1891                                   IXGB_READ_REG(&adapter->hw, TDT),
1892                                   tx_ring->next_to_use,
1893                                   tx_ring->next_to_clean,
1894                                   tx_ring->buffer_info[eop].time_stamp,
1895                                   eop,
1896                                   jiffies,
1897                                   eop_desc->status);
1898                         netif_stop_queue(netdev);
1899                 }
1900         }
1901
1902         return cleaned;
1903 }
1904
1905 /**
1906  * ixgb_rx_checksum - Receive Checksum Offload for 82597.
1907  * @adapter: board private structure
1908  * @rx_desc: receive descriptor
1909  * @sk_buff: socket buffer with received data
1910  **/
1911
1912 static void
1913 ixgb_rx_checksum(struct ixgb_adapter *adapter,
1914                  struct ixgb_rx_desc *rx_desc,
1915                  struct sk_buff *skb)
1916 {
1917         /* Ignore Checksum bit is set OR
1918          * TCP Checksum has not been calculated
1919          */
1920         if ((rx_desc->status & IXGB_RX_DESC_STATUS_IXSM) ||
1921            (!(rx_desc->status & IXGB_RX_DESC_STATUS_TCPCS))) {
1922                 skb_checksum_none_assert(skb);
1923                 return;
1924         }
1925
1926         /* At this point we know the hardware did the TCP checksum */
1927         /* now look at the TCP checksum error bit */
1928         if (rx_desc->errors & IXGB_RX_DESC_ERRORS_TCPE) {
1929                 /* let the stack verify checksum errors */
1930                 skb_checksum_none_assert(skb);
1931                 adapter->hw_csum_rx_error++;
1932         } else {
1933                 /* TCP checksum is good */
1934                 skb->ip_summed = CHECKSUM_UNNECESSARY;
1935                 adapter->hw_csum_rx_good++;
1936         }
1937 }
1938
1939 /*
1940  * this should improve performance for small packets with large amounts
1941  * of reassembly being done in the stack
1942  */
1943 static void ixgb_check_copybreak(struct napi_struct *napi,
1944                                  struct ixgb_buffer *buffer_info,
1945                                  u32 length, struct sk_buff **skb)
1946 {
1947         struct sk_buff *new_skb;
1948
1949         if (length > copybreak)
1950                 return;
1951
1952         new_skb = napi_alloc_skb(napi, length);
1953         if (!new_skb)
1954                 return;
1955
1956         skb_copy_to_linear_data_offset(new_skb, -NET_IP_ALIGN,
1957                                        (*skb)->data - NET_IP_ALIGN,
1958                                        length + NET_IP_ALIGN);
1959         /* save the skb in buffer_info as good */
1960         buffer_info->skb = *skb;
1961         *skb = new_skb;
1962 }
1963
1964 /**
1965  * ixgb_clean_rx_irq - Send received data up the network stack,
1966  * @adapter: board private structure
1967  **/
1968
1969 static bool
1970 ixgb_clean_rx_irq(struct ixgb_adapter *adapter, int *work_done, int work_to_do)
1971 {
1972         struct ixgb_desc_ring *rx_ring = &adapter->rx_ring;
1973         struct net_device *netdev = adapter->netdev;
1974         struct pci_dev *pdev = adapter->pdev;
1975         struct ixgb_rx_desc *rx_desc, *next_rxd;
1976         struct ixgb_buffer *buffer_info, *next_buffer, *next2_buffer;
1977         u32 length;
1978         unsigned int i, j;
1979         int cleaned_count = 0;
1980         bool cleaned = false;
1981
1982         i = rx_ring->next_to_clean;
1983         rx_desc = IXGB_RX_DESC(*rx_ring, i);
1984         buffer_info = &rx_ring->buffer_info[i];
1985
1986         while (rx_desc->status & IXGB_RX_DESC_STATUS_DD) {
1987                 struct sk_buff *skb;
1988                 u8 status;
1989
1990                 if (*work_done >= work_to_do)
1991                         break;
1992
1993                 (*work_done)++;
1994                 rmb();  /* read descriptor and rx_buffer_info after status DD */
1995                 status = rx_desc->status;
1996                 skb = buffer_info->skb;
1997                 buffer_info->skb = NULL;
1998
1999                 prefetch(skb->data - NET_IP_ALIGN);
2000
2001                 if (++i == rx_ring->count)
2002                         i = 0;
2003                 next_rxd = IXGB_RX_DESC(*rx_ring, i);
2004                 prefetch(next_rxd);
2005
2006                 j = i + 1;
2007                 if (j == rx_ring->count)
2008                         j = 0;
2009                 next2_buffer = &rx_ring->buffer_info[j];
2010                 prefetch(next2_buffer);
2011
2012                 next_buffer = &rx_ring->buffer_info[i];
2013
2014                 cleaned = true;
2015                 cleaned_count++;
2016
2017                 dma_unmap_single(&pdev->dev,
2018                                  buffer_info->dma,
2019                                  buffer_info->length,
2020                                  DMA_FROM_DEVICE);
2021                 buffer_info->dma = 0;
2022
2023                 length = le16_to_cpu(rx_desc->length);
2024                 rx_desc->length = 0;
2025
2026                 if (unlikely(!(status & IXGB_RX_DESC_STATUS_EOP))) {
2027
2028                         /* All receives must fit into a single buffer */
2029
2030                         pr_debug("Receive packet consumed multiple buffers length<%x>\n",
2031                                  length);
2032
2033                         dev_kfree_skb_irq(skb);
2034                         goto rxdesc_done;
2035                 }
2036
2037                 if (unlikely(rx_desc->errors &
2038                     (IXGB_RX_DESC_ERRORS_CE | IXGB_RX_DESC_ERRORS_SE |
2039                      IXGB_RX_DESC_ERRORS_P | IXGB_RX_DESC_ERRORS_RXE))) {
2040                         dev_kfree_skb_irq(skb);
2041                         goto rxdesc_done;
2042                 }
2043
2044                 ixgb_check_copybreak(&adapter->napi, buffer_info, length, &skb);
2045
2046                 /* Good Receive */
2047                 skb_put(skb, length);
2048
2049                 /* Receive Checksum Offload */
2050                 ixgb_rx_checksum(adapter, rx_desc, skb);
2051
2052                 skb->protocol = eth_type_trans(skb, netdev);
2053                 if (status & IXGB_RX_DESC_STATUS_VP)
2054                         __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
2055                                        le16_to_cpu(rx_desc->special));
2056
2057                 netif_receive_skb(skb);
2058
2059 rxdesc_done:
2060                 /* clean up descriptor, might be written over by hw */
2061                 rx_desc->status = 0;
2062
2063                 /* return some buffers to hardware, one at a time is too slow */
2064                 if (unlikely(cleaned_count >= IXGB_RX_BUFFER_WRITE)) {
2065                         ixgb_alloc_rx_buffers(adapter, cleaned_count);
2066                         cleaned_count = 0;
2067                 }
2068
2069                 /* use prefetched values */
2070                 rx_desc = next_rxd;
2071                 buffer_info = next_buffer;
2072         }
2073
2074         rx_ring->next_to_clean = i;
2075
2076         cleaned_count = IXGB_DESC_UNUSED(rx_ring);
2077         if (cleaned_count)
2078                 ixgb_alloc_rx_buffers(adapter, cleaned_count);
2079
2080         return cleaned;
2081 }
2082
2083 /**
2084  * ixgb_alloc_rx_buffers - Replace used receive buffers
2085  * @adapter: address of board private structure
2086  **/
2087
2088 static void
2089 ixgb_alloc_rx_buffers(struct ixgb_adapter *adapter, int cleaned_count)
2090 {
2091         struct ixgb_desc_ring *rx_ring = &adapter->rx_ring;
2092         struct net_device *netdev = adapter->netdev;
2093         struct pci_dev *pdev = adapter->pdev;
2094         struct ixgb_rx_desc *rx_desc;
2095         struct ixgb_buffer *buffer_info;
2096         struct sk_buff *skb;
2097         unsigned int i;
2098         long cleancount;
2099
2100         i = rx_ring->next_to_use;
2101         buffer_info = &rx_ring->buffer_info[i];
2102         cleancount = IXGB_DESC_UNUSED(rx_ring);
2103
2104
2105         /* leave three descriptors unused */
2106         while (--cleancount > 2 && cleaned_count--) {
2107                 /* recycle! its good for you */
2108                 skb = buffer_info->skb;
2109                 if (skb) {
2110                         skb_trim(skb, 0);
2111                         goto map_skb;
2112                 }
2113
2114                 skb = netdev_alloc_skb_ip_align(netdev, adapter->rx_buffer_len);
2115                 if (unlikely(!skb)) {
2116                         /* Better luck next round */
2117                         adapter->alloc_rx_buff_failed++;
2118                         break;
2119                 }
2120
2121                 buffer_info->skb = skb;
2122                 buffer_info->length = adapter->rx_buffer_len;
2123 map_skb:
2124                 buffer_info->dma = dma_map_single(&pdev->dev,
2125                                                   skb->data,
2126                                                   adapter->rx_buffer_len,
2127                                                   DMA_FROM_DEVICE);
2128                 if (dma_mapping_error(&pdev->dev, buffer_info->dma)) {
2129                         adapter->alloc_rx_buff_failed++;
2130                         break;
2131                 }
2132
2133                 rx_desc = IXGB_RX_DESC(*rx_ring, i);
2134                 rx_desc->buff_addr = cpu_to_le64(buffer_info->dma);
2135                 /* guarantee DD bit not set now before h/w gets descriptor
2136                  * this is the rest of the workaround for h/w double
2137                  * writeback. */
2138                 rx_desc->status = 0;
2139
2140
2141                 if (++i == rx_ring->count)
2142                         i = 0;
2143                 buffer_info = &rx_ring->buffer_info[i];
2144         }
2145
2146         if (likely(rx_ring->next_to_use != i)) {
2147                 rx_ring->next_to_use = i;
2148                 if (unlikely(i-- == 0))
2149                         i = (rx_ring->count - 1);
2150
2151                 /* Force memory writes to complete before letting h/w
2152                  * know there are new descriptors to fetch.  (Only
2153                  * applicable for weak-ordered memory model archs, such
2154                  * as IA-64). */
2155                 wmb();
2156                 IXGB_WRITE_REG(&adapter->hw, RDT, i);
2157         }
2158 }
2159
2160 static void
2161 ixgb_vlan_strip_enable(struct ixgb_adapter *adapter)
2162 {
2163         u32 ctrl;
2164
2165         /* enable VLAN tag insert/strip */
2166         ctrl = IXGB_READ_REG(&adapter->hw, CTRL0);
2167         ctrl |= IXGB_CTRL0_VME;
2168         IXGB_WRITE_REG(&adapter->hw, CTRL0, ctrl);
2169 }
2170
2171 static void
2172 ixgb_vlan_strip_disable(struct ixgb_adapter *adapter)
2173 {
2174         u32 ctrl;
2175
2176         /* disable VLAN tag insert/strip */
2177         ctrl = IXGB_READ_REG(&adapter->hw, CTRL0);
2178         ctrl &= ~IXGB_CTRL0_VME;
2179         IXGB_WRITE_REG(&adapter->hw, CTRL0, ctrl);
2180 }
2181
2182 static int
2183 ixgb_vlan_rx_add_vid(struct net_device *netdev, __be16 proto, u16 vid)
2184 {
2185         struct ixgb_adapter *adapter = netdev_priv(netdev);
2186         u32 vfta, index;
2187
2188         /* add VID to filter table */
2189
2190         index = (vid >> 5) & 0x7F;
2191         vfta = IXGB_READ_REG_ARRAY(&adapter->hw, VFTA, index);
2192         vfta |= (1 << (vid & 0x1F));
2193         ixgb_write_vfta(&adapter->hw, index, vfta);
2194         set_bit(vid, adapter->active_vlans);
2195
2196         return 0;
2197 }
2198
2199 static int
2200 ixgb_vlan_rx_kill_vid(struct net_device *netdev, __be16 proto, u16 vid)
2201 {
2202         struct ixgb_adapter *adapter = netdev_priv(netdev);
2203         u32 vfta, index;
2204
2205         /* remove VID from filter table */
2206
2207         index = (vid >> 5) & 0x7F;
2208         vfta = IXGB_READ_REG_ARRAY(&adapter->hw, VFTA, index);
2209         vfta &= ~(1 << (vid & 0x1F));
2210         ixgb_write_vfta(&adapter->hw, index, vfta);
2211         clear_bit(vid, adapter->active_vlans);
2212
2213         return 0;
2214 }
2215
2216 static void
2217 ixgb_restore_vlan(struct ixgb_adapter *adapter)
2218 {
2219         u16 vid;
2220
2221         for_each_set_bit(vid, adapter->active_vlans, VLAN_N_VID)
2222                 ixgb_vlan_rx_add_vid(adapter->netdev, htons(ETH_P_8021Q), vid);
2223 }
2224
2225 #ifdef CONFIG_NET_POLL_CONTROLLER
2226 /*
2227  * Polling 'interrupt' - used by things like netconsole to send skbs
2228  * without having to re-enable interrupts. It's not called while
2229  * the interrupt routine is executing.
2230  */
2231
2232 static void ixgb_netpoll(struct net_device *dev)
2233 {
2234         struct ixgb_adapter *adapter = netdev_priv(dev);
2235
2236         disable_irq(adapter->pdev->irq);
2237         ixgb_intr(adapter->pdev->irq, dev);
2238         enable_irq(adapter->pdev->irq);
2239 }
2240 #endif
2241
2242 /**
2243  * ixgb_io_error_detected - called when PCI error is detected
2244  * @pdev:    pointer to pci device with error
2245  * @state:   pci channel state after error
2246  *
2247  * This callback is called by the PCI subsystem whenever
2248  * a PCI bus error is detected.
2249  */
2250 static pci_ers_result_t ixgb_io_error_detected(struct pci_dev *pdev,
2251                                                enum pci_channel_state state)
2252 {
2253         struct net_device *netdev = pci_get_drvdata(pdev);
2254         struct ixgb_adapter *adapter = netdev_priv(netdev);
2255
2256         netif_device_detach(netdev);
2257
2258         if (state == pci_channel_io_perm_failure)
2259                 return PCI_ERS_RESULT_DISCONNECT;
2260
2261         if (netif_running(netdev))
2262                 ixgb_down(adapter, true);
2263
2264         pci_disable_device(pdev);
2265
2266         /* Request a slot reset. */
2267         return PCI_ERS_RESULT_NEED_RESET;
2268 }
2269
2270 /**
2271  * ixgb_io_slot_reset - called after the pci bus has been reset.
2272  * @pdev    pointer to pci device with error
2273  *
2274  * This callback is called after the PCI bus has been reset.
2275  * Basically, this tries to restart the card from scratch.
2276  * This is a shortened version of the device probe/discovery code,
2277  * it resembles the first-half of the ixgb_probe() routine.
2278  */
2279 static pci_ers_result_t ixgb_io_slot_reset(struct pci_dev *pdev)
2280 {
2281         struct net_device *netdev = pci_get_drvdata(pdev);
2282         struct ixgb_adapter *adapter = netdev_priv(netdev);
2283
2284         if (pci_enable_device(pdev)) {
2285                 netif_err(adapter, probe, adapter->netdev,
2286                           "Cannot re-enable PCI device after reset\n");
2287                 return PCI_ERS_RESULT_DISCONNECT;
2288         }
2289
2290         /* Perform card reset only on one instance of the card */
2291         if (0 != PCI_FUNC (pdev->devfn))
2292                 return PCI_ERS_RESULT_RECOVERED;
2293
2294         pci_set_master(pdev);
2295
2296         netif_carrier_off(netdev);
2297         netif_stop_queue(netdev);
2298         ixgb_reset(adapter);
2299
2300         /* Make sure the EEPROM is good */
2301         if (!ixgb_validate_eeprom_checksum(&adapter->hw)) {
2302                 netif_err(adapter, probe, adapter->netdev,
2303                           "After reset, the EEPROM checksum is not valid\n");
2304                 return PCI_ERS_RESULT_DISCONNECT;
2305         }
2306         ixgb_get_ee_mac_addr(&adapter->hw, netdev->dev_addr);
2307         memcpy(netdev->perm_addr, netdev->dev_addr, netdev->addr_len);
2308
2309         if (!is_valid_ether_addr(netdev->perm_addr)) {
2310                 netif_err(adapter, probe, adapter->netdev,
2311                           "After reset, invalid MAC address\n");
2312                 return PCI_ERS_RESULT_DISCONNECT;
2313         }
2314
2315         return PCI_ERS_RESULT_RECOVERED;
2316 }
2317
2318 /**
2319  * ixgb_io_resume - called when its OK to resume normal operations
2320  * @pdev    pointer to pci device with error
2321  *
2322  * The error recovery driver tells us that its OK to resume
2323  * normal operation. Implementation resembles the second-half
2324  * of the ixgb_probe() routine.
2325  */
2326 static void ixgb_io_resume(struct pci_dev *pdev)
2327 {
2328         struct net_device *netdev = pci_get_drvdata(pdev);
2329         struct ixgb_adapter *adapter = netdev_priv(netdev);
2330
2331         pci_set_master(pdev);
2332
2333         if (netif_running(netdev)) {
2334                 if (ixgb_up(adapter)) {
2335                         pr_err("can't bring device back up after reset\n");
2336                         return;
2337                 }
2338         }
2339
2340         netif_device_attach(netdev);
2341         mod_timer(&adapter->watchdog_timer, jiffies);
2342 }
2343
2344 /* ixgb_main.c */