]> nv-tegra.nvidia Code Review - linux-3.10.git/blob - drivers/net/ixgb/ixgb_main.c
[PATCH] ixgb: Do not set the RS bit on context descriptors
[linux-3.10.git] / drivers / net / ixgb / ixgb_main.c
1 /*******************************************************************************
2
3   
4   Copyright(c) 1999 - 2005 Intel Corporation. All rights reserved.
5   
6   This program is free software; you can redistribute it and/or modify it 
7   under the terms of the GNU General Public License as published by the Free 
8   Software Foundation; either version 2 of the License, or (at your option) 
9   any later version.
10   
11   This program is distributed in the hope that it will be useful, but WITHOUT 
12   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
13   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for 
14   more details.
15   
16   You should have received a copy of the GNU General Public License along with
17   this program; if not, write to the Free Software Foundation, Inc., 59 
18   Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19   
20   The full GNU General Public License is included in this distribution in the
21   file called LICENSE.
22   
23   Contact Information:
24   Linux NICS <linux.nics@intel.com>
25   Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
26
27 *******************************************************************************/
28
29 #include "ixgb.h"
30
31 /* Change Log
32  * 1.0.88 01/05/05
33  * - include fix to the condition that determines when to quit NAPI - Robert Olsson
34  * - use netif_poll_{disable/enable} to synchronize between NAPI and i/f up/down
35  * 1.0.84 10/26/04
36  * - reset buffer_info->dma in Tx resource cleanup logic
37  * 1.0.83 10/12/04
38  * - sparse cleanup - shemminger@osdl.org
39  * - fix tx resource cleanup logic
40  */
41
42 char ixgb_driver_name[] = "ixgb";
43 char ixgb_driver_string[] = "Intel(R) PRO/10GbE Network Driver";
44
45 #ifndef CONFIG_IXGB_NAPI
46 #define DRIVERNAPI
47 #else
48 #define DRIVERNAPI "-NAPI"
49 #endif
50 char ixgb_driver_version[] = "1.0.90-k2"DRIVERNAPI;
51 char ixgb_copyright[] = "Copyright (c) 1999-2005 Intel Corporation.";
52
53 /* ixgb_pci_tbl - PCI Device ID Table
54  *
55  * Wildcard entries (PCI_ANY_ID) should come last
56  * Last entry must be all 0s
57  *
58  * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
59  *   Class, Class Mask, private data (not used) }
60  */
61 static struct pci_device_id ixgb_pci_tbl[] = {
62         {INTEL_VENDOR_ID, IXGB_DEVICE_ID_82597EX,
63          PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
64         {INTEL_VENDOR_ID, IXGB_DEVICE_ID_82597EX_SR,
65          PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
66         {INTEL_VENDOR_ID, IXGB_DEVICE_ID_82597EX_LR,  
67          PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
68
69         /* required last entry */
70         {0,}
71 };
72
73 MODULE_DEVICE_TABLE(pci, ixgb_pci_tbl);
74
75 /* Local Function Prototypes */
76
77 int ixgb_up(struct ixgb_adapter *adapter);
78 void ixgb_down(struct ixgb_adapter *adapter, boolean_t kill_watchdog);
79 void ixgb_reset(struct ixgb_adapter *adapter);
80 int ixgb_setup_tx_resources(struct ixgb_adapter *adapter);
81 int ixgb_setup_rx_resources(struct ixgb_adapter *adapter);
82 void ixgb_free_tx_resources(struct ixgb_adapter *adapter);
83 void ixgb_free_rx_resources(struct ixgb_adapter *adapter);
84 void ixgb_update_stats(struct ixgb_adapter *adapter);
85
86 static int ixgb_init_module(void);
87 static void ixgb_exit_module(void);
88 static int ixgb_probe(struct pci_dev *pdev, const struct pci_device_id *ent);
89 static void __devexit ixgb_remove(struct pci_dev *pdev);
90 static int ixgb_sw_init(struct ixgb_adapter *adapter);
91 static int ixgb_open(struct net_device *netdev);
92 static int ixgb_close(struct net_device *netdev);
93 static void ixgb_configure_tx(struct ixgb_adapter *adapter);
94 static void ixgb_configure_rx(struct ixgb_adapter *adapter);
95 static void ixgb_setup_rctl(struct ixgb_adapter *adapter);
96 static void ixgb_clean_tx_ring(struct ixgb_adapter *adapter);
97 static void ixgb_clean_rx_ring(struct ixgb_adapter *adapter);
98 static void ixgb_set_multi(struct net_device *netdev);
99 static void ixgb_watchdog(unsigned long data);
100 static int ixgb_xmit_frame(struct sk_buff *skb, struct net_device *netdev);
101 static struct net_device_stats *ixgb_get_stats(struct net_device *netdev);
102 static int ixgb_change_mtu(struct net_device *netdev, int new_mtu);
103 static int ixgb_set_mac(struct net_device *netdev, void *p);
104 static irqreturn_t ixgb_intr(int irq, void *data, struct pt_regs *regs);
105 static boolean_t ixgb_clean_tx_irq(struct ixgb_adapter *adapter);
106 #ifdef CONFIG_IXGB_NAPI
107 static int ixgb_clean(struct net_device *netdev, int *budget);
108 static boolean_t ixgb_clean_rx_irq(struct ixgb_adapter *adapter,
109                                    int *work_done, int work_to_do);
110 #else
111 static boolean_t ixgb_clean_rx_irq(struct ixgb_adapter *adapter);
112 #endif
113 static void ixgb_alloc_rx_buffers(struct ixgb_adapter *adapter);
114 void ixgb_set_ethtool_ops(struct net_device *netdev);
115 static void ixgb_tx_timeout(struct net_device *dev);
116 static void ixgb_tx_timeout_task(struct net_device *dev);
117 static void ixgb_vlan_rx_register(struct net_device *netdev,
118                                   struct vlan_group *grp);
119 static void ixgb_vlan_rx_add_vid(struct net_device *netdev, uint16_t vid);
120 static void ixgb_vlan_rx_kill_vid(struct net_device *netdev, uint16_t vid);
121 static void ixgb_restore_vlan(struct ixgb_adapter *adapter);
122
123 static int ixgb_notify_reboot(struct notifier_block *, unsigned long event,
124                               void *ptr);
125 static int ixgb_suspend(struct pci_dev *pdev, uint32_t state);
126
127 #ifdef CONFIG_NET_POLL_CONTROLLER
128 /* for netdump / net console */
129 static void ixgb_netpoll(struct net_device *dev);
130 #endif
131
132 struct notifier_block ixgb_notifier_reboot = {
133         .notifier_call = ixgb_notify_reboot,
134         .next = NULL,
135         .priority = 0
136 };
137
138 /* Exported from other modules */
139
140 extern void ixgb_check_options(struct ixgb_adapter *adapter);
141
142 static struct pci_driver ixgb_driver = {
143         .name = ixgb_driver_name,
144         .id_table = ixgb_pci_tbl,
145         .probe = ixgb_probe,
146         .remove = __devexit_p(ixgb_remove),
147         /* Power Managment Hooks */
148         .suspend = NULL,
149         .resume = NULL
150 };
151
152 MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
153 MODULE_DESCRIPTION("Intel(R) PRO/10GbE Network Driver");
154 MODULE_LICENSE("GPL");
155
156 /* some defines for controlling descriptor fetches in h/w */
157 #define RXDCTL_PTHRESH_DEFAULT 128      /* chip considers prefech below this */
158 #define RXDCTL_HTHRESH_DEFAULT 16       /* chip will only prefetch if tail is 
159                                            pushed this many descriptors from head */
160 #define RXDCTL_WTHRESH_DEFAULT 16       /* chip writes back at this many or RXT0 */
161
162 /**
163  * ixgb_init_module - Driver Registration Routine
164  *
165  * ixgb_init_module is the first routine called when the driver is
166  * loaded. All it does is register with the PCI subsystem.
167  **/
168
169 static int __init
170 ixgb_init_module(void)
171 {
172         int ret;
173         printk(KERN_INFO "%s - version %s\n",
174                ixgb_driver_string, ixgb_driver_version);
175
176         printk(KERN_INFO "%s\n", ixgb_copyright);
177
178         ret = pci_module_init(&ixgb_driver);
179         if(ret >= 0) {
180                 register_reboot_notifier(&ixgb_notifier_reboot);
181         }
182         return ret;
183 }
184
185 module_init(ixgb_init_module);
186
187 /**
188  * ixgb_exit_module - Driver Exit Cleanup Routine
189  *
190  * ixgb_exit_module is called just before the driver is removed
191  * from memory.
192  **/
193
194 static void __exit
195 ixgb_exit_module(void)
196 {
197         unregister_reboot_notifier(&ixgb_notifier_reboot);
198         pci_unregister_driver(&ixgb_driver);
199 }
200
201 module_exit(ixgb_exit_module);
202
203 /**
204  * ixgb_irq_disable - Mask off interrupt generation on the NIC
205  * @adapter: board private structure
206  **/
207
208 static inline void
209 ixgb_irq_disable(struct ixgb_adapter *adapter)
210 {
211         atomic_inc(&adapter->irq_sem);
212         IXGB_WRITE_REG(&adapter->hw, IMC, ~0);
213         IXGB_WRITE_FLUSH(&adapter->hw);
214         synchronize_irq(adapter->pdev->irq);
215 }
216
217 /**
218  * ixgb_irq_enable - Enable default interrupt generation settings
219  * @adapter: board private structure
220  **/
221
222 static inline void
223 ixgb_irq_enable(struct ixgb_adapter *adapter)
224 {
225         if(atomic_dec_and_test(&adapter->irq_sem)) {
226                 IXGB_WRITE_REG(&adapter->hw, IMS,
227                            IXGB_INT_RXT0 | IXGB_INT_RXDMT0 | IXGB_INT_TXDW |
228                            IXGB_INT_RXO | IXGB_INT_LSC);
229                 IXGB_WRITE_FLUSH(&adapter->hw);
230         }
231 }
232
233 int
234 ixgb_up(struct ixgb_adapter *adapter)
235 {
236         struct net_device *netdev = adapter->netdev;
237         int err;
238         int max_frame = netdev->mtu + ENET_HEADER_SIZE + ENET_FCS_LENGTH;
239         struct ixgb_hw *hw = &adapter->hw;
240
241         /* hardware has been reset, we need to reload some things */
242
243         ixgb_set_multi(netdev);
244
245         ixgb_restore_vlan(adapter);
246
247         ixgb_configure_tx(adapter);
248         ixgb_setup_rctl(adapter);
249         ixgb_configure_rx(adapter);
250         ixgb_alloc_rx_buffers(adapter);
251
252 #ifdef CONFIG_PCI_MSI
253         {
254         boolean_t pcix = (IXGB_READ_REG(&adapter->hw, STATUS) & 
255                                                   IXGB_STATUS_PCIX_MODE) ? TRUE : FALSE;
256         adapter->have_msi = TRUE;
257
258         if (!pcix)
259            adapter->have_msi = FALSE;
260         else if((err = pci_enable_msi(adapter->pdev))) {
261                 printk (KERN_ERR
262                  "Unable to allocate MSI interrupt Error: %d\n", err);
263                 adapter->have_msi = FALSE;
264                 /* proceed to try to request regular interrupt */
265         }
266         }
267
268 #endif
269         if((err = request_irq(adapter->pdev->irq, &ixgb_intr,
270                                   SA_SHIRQ | SA_SAMPLE_RANDOM,
271                                   netdev->name, netdev)))
272                 return err;
273
274         /* disable interrupts and get the hardware into a known state */
275         IXGB_WRITE_REG(&adapter->hw, IMC, 0xffffffff);
276
277         if((hw->max_frame_size != max_frame) ||
278                 (hw->max_frame_size !=
279                 (IXGB_READ_REG(hw, MFS) >> IXGB_MFS_SHIFT))) {
280
281                 hw->max_frame_size = max_frame;
282
283                 IXGB_WRITE_REG(hw, MFS, hw->max_frame_size << IXGB_MFS_SHIFT);
284
285                 if(hw->max_frame_size >
286                    IXGB_MAX_ENET_FRAME_SIZE_WITHOUT_FCS + ENET_FCS_LENGTH) {
287                         uint32_t ctrl0 = IXGB_READ_REG(hw, CTRL0);
288
289                         if(!(ctrl0 & IXGB_CTRL0_JFE)) {
290                                 ctrl0 |= IXGB_CTRL0_JFE;
291                                 IXGB_WRITE_REG(hw, CTRL0, ctrl0);
292                         }
293                 }
294         }
295
296         mod_timer(&adapter->watchdog_timer, jiffies);
297         ixgb_irq_enable(adapter);
298
299 #ifdef CONFIG_IXGB_NAPI
300         netif_poll_enable(netdev);
301 #endif
302         return 0;
303 }
304
305 void
306 ixgb_down(struct ixgb_adapter *adapter, boolean_t kill_watchdog)
307 {
308         struct net_device *netdev = adapter->netdev;
309
310         ixgb_irq_disable(adapter);
311         free_irq(adapter->pdev->irq, netdev);
312 #ifdef CONFIG_PCI_MSI
313         if(adapter->have_msi == TRUE)
314                 pci_disable_msi(adapter->pdev);
315
316 #endif
317         if(kill_watchdog)
318                 del_timer_sync(&adapter->watchdog_timer);
319 #ifdef CONFIG_IXGB_NAPI
320         netif_poll_disable(netdev);
321 #endif
322         adapter->link_speed = 0;
323         adapter->link_duplex = 0;
324         netif_carrier_off(netdev);
325         netif_stop_queue(netdev);
326
327         ixgb_reset(adapter);
328         ixgb_clean_tx_ring(adapter);
329         ixgb_clean_rx_ring(adapter);
330 }
331
332 void
333 ixgb_reset(struct ixgb_adapter *adapter)
334 {
335
336         ixgb_adapter_stop(&adapter->hw);
337         if(!ixgb_init_hw(&adapter->hw))
338                 IXGB_DBG("ixgb_init_hw failed.\n");
339 }
340
341 /**
342  * ixgb_probe - Device Initialization Routine
343  * @pdev: PCI device information struct
344  * @ent: entry in ixgb_pci_tbl
345  *
346  * Returns 0 on success, negative on failure
347  *
348  * ixgb_probe initializes an adapter identified by a pci_dev structure.
349  * The OS initialization, configuring of the adapter private structure,
350  * and a hardware reset occur.
351  **/
352
353 static int __devinit
354 ixgb_probe(struct pci_dev *pdev,
355                 const struct pci_device_id *ent)
356 {
357         struct net_device *netdev = NULL;
358         struct ixgb_adapter *adapter;
359         static int cards_found = 0;
360         unsigned long mmio_start;
361         int mmio_len;
362         int pci_using_dac;
363         int i;
364         int err;
365
366         if((err = pci_enable_device(pdev)))
367                 return err;
368
369         if(!(err = pci_set_dma_mask(pdev, DMA_64BIT_MASK))) {
370                 pci_using_dac = 1;
371         } else {
372                 if((err = pci_set_dma_mask(pdev, DMA_32BIT_MASK))) {
373                         IXGB_ERR("No usable DMA configuration, aborting\n");
374                         return err;
375                 }
376                 pci_using_dac = 0;
377         }
378
379         if((err = pci_request_regions(pdev, ixgb_driver_name)))
380                 return err;
381
382         pci_set_master(pdev);
383
384         netdev = alloc_etherdev(sizeof(struct ixgb_adapter));
385         if(!netdev) {
386                 err = -ENOMEM;
387                 goto err_alloc_etherdev;
388         }
389
390         SET_MODULE_OWNER(netdev);
391         SET_NETDEV_DEV(netdev, &pdev->dev);
392
393         pci_set_drvdata(pdev, netdev);
394         adapter = netdev->priv;
395         adapter->netdev = netdev;
396         adapter->pdev = pdev;
397         adapter->hw.back = adapter;
398
399         mmio_start = pci_resource_start(pdev, BAR_0);
400         mmio_len = pci_resource_len(pdev, BAR_0);
401
402         adapter->hw.hw_addr = ioremap(mmio_start, mmio_len);
403         if(!adapter->hw.hw_addr) {
404                 err = -EIO;
405                 goto err_ioremap;
406         }
407
408         for(i = BAR_1; i <= BAR_5; i++) {
409                 if(pci_resource_len(pdev, i) == 0)
410                         continue;
411                 if(pci_resource_flags(pdev, i) & IORESOURCE_IO) {
412                         adapter->hw.io_base = pci_resource_start(pdev, i);
413                         break;
414                 }
415         }
416
417         netdev->open = &ixgb_open;
418         netdev->stop = &ixgb_close;
419         netdev->hard_start_xmit = &ixgb_xmit_frame;
420         netdev->get_stats = &ixgb_get_stats;
421         netdev->set_multicast_list = &ixgb_set_multi;
422         netdev->set_mac_address = &ixgb_set_mac;
423         netdev->change_mtu = &ixgb_change_mtu;
424         ixgb_set_ethtool_ops(netdev);
425         netdev->tx_timeout = &ixgb_tx_timeout;
426         netdev->watchdog_timeo = HZ;
427 #ifdef CONFIG_IXGB_NAPI
428         netdev->poll = &ixgb_clean;
429         netdev->weight = 64;
430 #endif
431         netdev->vlan_rx_register = ixgb_vlan_rx_register;
432         netdev->vlan_rx_add_vid = ixgb_vlan_rx_add_vid;
433         netdev->vlan_rx_kill_vid = ixgb_vlan_rx_kill_vid;
434 #ifdef CONFIG_NET_POLL_CONTROLLER
435         netdev->poll_controller = ixgb_netpoll;
436 #endif
437
438         netdev->mem_start = mmio_start;
439         netdev->mem_end = mmio_start + mmio_len;
440         netdev->base_addr = adapter->hw.io_base;
441
442         adapter->bd_number = cards_found;
443         adapter->link_speed = 0;
444         adapter->link_duplex = 0;
445
446         /* setup the private structure */
447
448         if((err = ixgb_sw_init(adapter)))
449                 goto err_sw_init;
450
451         netdev->features = NETIF_F_SG |
452                            NETIF_F_HW_CSUM |
453                            NETIF_F_HW_VLAN_TX |
454                            NETIF_F_HW_VLAN_RX |
455                            NETIF_F_HW_VLAN_FILTER;
456 #ifdef NETIF_F_TSO
457         netdev->features |= NETIF_F_TSO;
458 #endif
459
460         if(pci_using_dac)
461                 netdev->features |= NETIF_F_HIGHDMA;
462
463         /* make sure the EEPROM is good */
464
465         if(!ixgb_validate_eeprom_checksum(&adapter->hw)) {
466                 printk(KERN_ERR "The EEPROM Checksum Is Not Valid\n");
467                 err = -EIO;
468                 goto err_eeprom;
469         }
470
471         ixgb_get_ee_mac_addr(&adapter->hw, netdev->dev_addr);
472
473         if(!is_valid_ether_addr(netdev->dev_addr)) {
474                 err = -EIO;
475                 goto err_eeprom;
476         }
477
478         adapter->part_num = ixgb_get_ee_pba_number(&adapter->hw);
479
480         init_timer(&adapter->watchdog_timer);
481         adapter->watchdog_timer.function = &ixgb_watchdog;
482         adapter->watchdog_timer.data = (unsigned long)adapter;
483
484         INIT_WORK(&adapter->tx_timeout_task,
485                   (void (*)(void *))ixgb_tx_timeout_task, netdev);
486
487         if((err = register_netdev(netdev)))
488                 goto err_register;
489
490         /* we're going to reset, so assume we have no link for now */
491
492         netif_carrier_off(netdev);
493         netif_stop_queue(netdev);
494
495         printk(KERN_INFO "%s: Intel(R) PRO/10GbE Network Connection\n",
496                    netdev->name);
497         ixgb_check_options(adapter);
498         /* reset the hardware with the new settings */
499
500         ixgb_reset(adapter);
501
502         cards_found++;
503         return 0;
504
505 err_register:
506 err_sw_init:
507 err_eeprom:
508         iounmap(adapter->hw.hw_addr);
509 err_ioremap:
510         free_netdev(netdev);
511 err_alloc_etherdev:
512         pci_release_regions(pdev);
513         return err;
514 }
515
516 /**
517  * ixgb_remove - Device Removal Routine
518  * @pdev: PCI device information struct
519  *
520  * ixgb_remove is called by the PCI subsystem to alert the driver
521  * that it should release a PCI device.  The could be caused by a
522  * Hot-Plug event, or because the driver is going to be removed from
523  * memory.
524  **/
525
526 static void __devexit
527 ixgb_remove(struct pci_dev *pdev)
528 {
529         struct net_device *netdev = pci_get_drvdata(pdev);
530         struct ixgb_adapter *adapter = netdev->priv;
531
532         unregister_netdev(netdev);
533
534         iounmap(adapter->hw.hw_addr);
535         pci_release_regions(pdev);
536
537         free_netdev(netdev);
538 }
539
540 /**
541  * ixgb_sw_init - Initialize general software structures (struct ixgb_adapter)
542  * @adapter: board private structure to initialize
543  *
544  * ixgb_sw_init initializes the Adapter private data structure.
545  * Fields are initialized based on PCI device information and
546  * OS network device settings (MTU size).
547  **/
548
549 static int __devinit
550 ixgb_sw_init(struct ixgb_adapter *adapter)
551 {
552         struct ixgb_hw *hw = &adapter->hw;
553         struct net_device *netdev = adapter->netdev;
554         struct pci_dev *pdev = adapter->pdev;
555
556         /* PCI config space info */
557
558         hw->vendor_id = pdev->vendor;
559         hw->device_id = pdev->device;
560         hw->subsystem_vendor_id = pdev->subsystem_vendor;
561         hw->subsystem_id = pdev->subsystem_device;
562
563         adapter->rx_buffer_len = IXGB_RXBUFFER_2048;
564
565         hw->max_frame_size = netdev->mtu + ENET_HEADER_SIZE + ENET_FCS_LENGTH;
566
567         if((hw->device_id == IXGB_DEVICE_ID_82597EX)
568            ||(hw->device_id == IXGB_DEVICE_ID_82597EX_LR)
569            ||(hw->device_id == IXGB_DEVICE_ID_82597EX_SR))
570                         hw->mac_type = ixgb_82597;
571         else {
572                 /* should never have loaded on this device */
573                 printk(KERN_ERR "ixgb: unsupported device id\n");
574         }
575
576         /* enable flow control to be programmed */
577         hw->fc.send_xon = 1;
578
579         atomic_set(&adapter->irq_sem, 1);
580         spin_lock_init(&adapter->tx_lock);
581
582         return 0;
583 }
584
585 /**
586  * ixgb_open - Called when a network interface is made active
587  * @netdev: network interface device structure
588  *
589  * Returns 0 on success, negative value on failure
590  *
591  * The open entry point is called when a network interface is made
592  * active by the system (IFF_UP).  At this point all resources needed
593  * for transmit and receive operations are allocated, the interrupt
594  * handler is registered with the OS, the watchdog timer is started,
595  * and the stack is notified that the interface is ready.
596  **/
597
598 static int
599 ixgb_open(struct net_device *netdev)
600 {
601         struct ixgb_adapter *adapter = netdev->priv;
602         int err;
603
604         /* allocate transmit descriptors */
605
606         if((err = ixgb_setup_tx_resources(adapter)))
607                 goto err_setup_tx;
608
609         /* allocate receive descriptors */
610
611         if((err = ixgb_setup_rx_resources(adapter)))
612                 goto err_setup_rx;
613
614         if((err = ixgb_up(adapter)))
615                 goto err_up;
616
617         return 0;
618
619 err_up:
620         ixgb_free_rx_resources(adapter);
621 err_setup_rx:
622         ixgb_free_tx_resources(adapter);
623 err_setup_tx:
624         ixgb_reset(adapter);
625
626         return err;
627 }
628
629 /**
630  * ixgb_close - Disables a network interface
631  * @netdev: network interface device structure
632  *
633  * Returns 0, this is not allowed to fail
634  *
635  * The close entry point is called when an interface is de-activated
636  * by the OS.  The hardware is still under the drivers control, but
637  * needs to be disabled.  A global MAC reset is issued to stop the
638  * hardware, and all transmit and receive resources are freed.
639  **/
640
641 static int
642 ixgb_close(struct net_device *netdev)
643 {
644         struct ixgb_adapter *adapter = netdev->priv;
645
646         ixgb_down(adapter, TRUE);
647
648         ixgb_free_tx_resources(adapter);
649         ixgb_free_rx_resources(adapter);
650
651         return 0;
652 }
653
654 /**
655  * ixgb_setup_tx_resources - allocate Tx resources (Descriptors)
656  * @adapter: board private structure
657  *
658  * Return 0 on success, negative on failure
659  **/
660
661 int
662 ixgb_setup_tx_resources(struct ixgb_adapter *adapter)
663 {
664         struct ixgb_desc_ring *txdr = &adapter->tx_ring;
665         struct pci_dev *pdev = adapter->pdev;
666         int size;
667
668         size = sizeof(struct ixgb_buffer) * txdr->count;
669         txdr->buffer_info = vmalloc(size);
670         if(!txdr->buffer_info) {
671                 return -ENOMEM;
672         }
673         memset(txdr->buffer_info, 0, size);
674
675         /* round up to nearest 4K */
676
677         txdr->size = txdr->count * sizeof(struct ixgb_tx_desc);
678         IXGB_ROUNDUP(txdr->size, 4096);
679
680         txdr->desc = pci_alloc_consistent(pdev, txdr->size, &txdr->dma);
681         if(!txdr->desc) {
682                 vfree(txdr->buffer_info);
683                 return -ENOMEM;
684         }
685         memset(txdr->desc, 0, txdr->size);
686
687         txdr->next_to_use = 0;
688         txdr->next_to_clean = 0;
689
690         return 0;
691 }
692
693 /**
694  * ixgb_configure_tx - Configure 82597 Transmit Unit after Reset.
695  * @adapter: board private structure
696  *
697  * Configure the Tx unit of the MAC after a reset.
698  **/
699
700 static void
701 ixgb_configure_tx(struct ixgb_adapter *adapter)
702 {
703         uint64_t tdba = adapter->tx_ring.dma;
704         uint32_t tdlen = adapter->tx_ring.count * sizeof(struct ixgb_tx_desc);
705         uint32_t tctl;
706         struct ixgb_hw *hw = &adapter->hw;
707
708         /* Setup the Base and Length of the Tx Descriptor Ring 
709          * tx_ring.dma can be either a 32 or 64 bit value 
710          */
711
712         IXGB_WRITE_REG(hw, TDBAL, (tdba & 0x00000000ffffffffULL));
713         IXGB_WRITE_REG(hw, TDBAH, (tdba >> 32));
714
715         IXGB_WRITE_REG(hw, TDLEN, tdlen);
716
717         /* Setup the HW Tx Head and Tail descriptor pointers */
718
719         IXGB_WRITE_REG(hw, TDH, 0);
720         IXGB_WRITE_REG(hw, TDT, 0);
721
722         /* don't set up txdctl, it induces performance problems if configured
723          * incorrectly */
724         /* Set the Tx Interrupt Delay register */
725
726         IXGB_WRITE_REG(hw, TIDV, adapter->tx_int_delay);
727
728         /* Program the Transmit Control Register */
729
730         tctl = IXGB_TCTL_TCE | IXGB_TCTL_TXEN | IXGB_TCTL_TPDE;
731         IXGB_WRITE_REG(hw, TCTL, tctl);
732
733         /* Setup Transmit Descriptor Settings for this adapter */
734         adapter->tx_cmd_type =
735                 IXGB_TX_DESC_TYPE 
736                 | (adapter->tx_int_delay_enable ? IXGB_TX_DESC_CMD_IDE : 0);
737 }
738
739 /**
740  * ixgb_setup_rx_resources - allocate Rx resources (Descriptors)
741  * @adapter: board private structure
742  *
743  * Returns 0 on success, negative on failure
744  **/
745
746 int
747 ixgb_setup_rx_resources(struct ixgb_adapter *adapter)
748 {
749         struct ixgb_desc_ring *rxdr = &adapter->rx_ring;
750         struct pci_dev *pdev = adapter->pdev;
751         int size;
752
753         size = sizeof(struct ixgb_buffer) * rxdr->count;
754         rxdr->buffer_info = vmalloc(size);
755         if(!rxdr->buffer_info) {
756                 return -ENOMEM;
757         }
758         memset(rxdr->buffer_info, 0, size);
759
760         /* Round up to nearest 4K */
761
762         rxdr->size = rxdr->count * sizeof(struct ixgb_rx_desc);
763         IXGB_ROUNDUP(rxdr->size, 4096);
764
765         rxdr->desc = pci_alloc_consistent(pdev, rxdr->size, &rxdr->dma);
766
767         if(!rxdr->desc) {
768                 vfree(rxdr->buffer_info);
769                 return -ENOMEM;
770         }
771         memset(rxdr->desc, 0, rxdr->size);
772
773         rxdr->next_to_clean = 0;
774         rxdr->next_to_use = 0;
775
776         return 0;
777 }
778
779 /**
780  * ixgb_setup_rctl - configure the receive control register
781  * @adapter: Board private structure
782  **/
783
784 static void
785 ixgb_setup_rctl(struct ixgb_adapter *adapter)
786 {
787         uint32_t rctl;
788
789         rctl = IXGB_READ_REG(&adapter->hw, RCTL);
790
791         rctl &= ~(3 << IXGB_RCTL_MO_SHIFT);
792
793         rctl |=
794                 IXGB_RCTL_BAM | IXGB_RCTL_RDMTS_1_2 | 
795                 IXGB_RCTL_RXEN | IXGB_RCTL_CFF | 
796                 (adapter->hw.mc_filter_type << IXGB_RCTL_MO_SHIFT);
797
798         rctl |= IXGB_RCTL_SECRC;
799
800         switch (adapter->rx_buffer_len) {
801         case IXGB_RXBUFFER_2048:
802         default:
803                 rctl |= IXGB_RCTL_BSIZE_2048;
804                 break;
805         case IXGB_RXBUFFER_4096:
806                 rctl |= IXGB_RCTL_BSIZE_4096;
807                 break;
808         case IXGB_RXBUFFER_8192:
809                 rctl |= IXGB_RCTL_BSIZE_8192;
810                 break;
811         case IXGB_RXBUFFER_16384:
812                 rctl |= IXGB_RCTL_BSIZE_16384;
813                 break;
814         }
815
816         IXGB_WRITE_REG(&adapter->hw, RCTL, rctl);
817 }
818
819 /**
820  * ixgb_configure_rx - Configure 82597 Receive Unit after Reset.
821  * @adapter: board private structure
822  *
823  * Configure the Rx unit of the MAC after a reset.
824  **/
825
826 static void
827 ixgb_configure_rx(struct ixgb_adapter *adapter)
828 {
829         uint64_t rdba = adapter->rx_ring.dma;
830         uint32_t rdlen = adapter->rx_ring.count * sizeof(struct ixgb_rx_desc);
831         struct ixgb_hw *hw = &adapter->hw;
832         uint32_t rctl;
833         uint32_t rxcsum;
834         uint32_t rxdctl;
835
836         /* make sure receives are disabled while setting up the descriptors */
837
838         rctl = IXGB_READ_REG(hw, RCTL);
839         IXGB_WRITE_REG(hw, RCTL, rctl & ~IXGB_RCTL_RXEN);
840
841         /* set the Receive Delay Timer Register */
842
843         IXGB_WRITE_REG(hw, RDTR, adapter->rx_int_delay);
844
845         /* Setup the Base and Length of the Rx Descriptor Ring */
846
847         IXGB_WRITE_REG(hw, RDBAL, (rdba & 0x00000000ffffffffULL));
848         IXGB_WRITE_REG(hw, RDBAH, (rdba >> 32));
849
850         IXGB_WRITE_REG(hw, RDLEN, rdlen);
851
852         /* Setup the HW Rx Head and Tail Descriptor Pointers */
853         IXGB_WRITE_REG(hw, RDH, 0);
854         IXGB_WRITE_REG(hw, RDT, 0);
855
856         /* set up pre-fetching of receive buffers so we get some before we
857          * run out (default hardware behavior is to run out before fetching
858          * more).  This sets up to fetch if HTHRESH rx descriptors are avail
859          * and the descriptors in hw cache are below PTHRESH.  This avoids
860          * the hardware behavior of fetching <=512 descriptors in a single
861          * burst that pre-empts all other activity, usually causing fifo
862          * overflows. */
863         /* use WTHRESH to burst write 16 descriptors or burst when RXT0 */
864         rxdctl = RXDCTL_WTHRESH_DEFAULT << IXGB_RXDCTL_WTHRESH_SHIFT |
865                  RXDCTL_HTHRESH_DEFAULT << IXGB_RXDCTL_HTHRESH_SHIFT |
866                  RXDCTL_PTHRESH_DEFAULT << IXGB_RXDCTL_PTHRESH_SHIFT;
867         IXGB_WRITE_REG(hw, RXDCTL, rxdctl);
868
869         /* Enable Receive Checksum Offload for TCP and UDP */
870         if(adapter->rx_csum == TRUE) {
871                 rxcsum = IXGB_READ_REG(hw, RXCSUM);
872                 rxcsum |= IXGB_RXCSUM_TUOFL;
873                 IXGB_WRITE_REG(hw, RXCSUM, rxcsum);
874         }
875
876         /* Enable Receives */
877
878         IXGB_WRITE_REG(hw, RCTL, rctl);
879 }
880
881 /**
882  * ixgb_free_tx_resources - Free Tx Resources
883  * @adapter: board private structure
884  *
885  * Free all transmit software resources
886  **/
887
888 void
889 ixgb_free_tx_resources(struct ixgb_adapter *adapter)
890 {
891         struct pci_dev *pdev = adapter->pdev;
892
893         ixgb_clean_tx_ring(adapter);
894
895         vfree(adapter->tx_ring.buffer_info);
896         adapter->tx_ring.buffer_info = NULL;
897
898         pci_free_consistent(pdev, adapter->tx_ring.size,
899                             adapter->tx_ring.desc, adapter->tx_ring.dma);
900
901         adapter->tx_ring.desc = NULL;
902 }
903
904 static inline void
905 ixgb_unmap_and_free_tx_resource(struct ixgb_adapter *adapter,
906                                         struct ixgb_buffer *buffer_info)
907 {
908         struct pci_dev *pdev = adapter->pdev;
909         if(buffer_info->dma) {
910                 pci_unmap_page(pdev,
911                            buffer_info->dma,
912                            buffer_info->length,
913                            PCI_DMA_TODEVICE);
914                 buffer_info->dma = 0;
915         }
916         if(buffer_info->skb) {
917                 dev_kfree_skb_any(buffer_info->skb);
918                 buffer_info->skb = NULL;
919         }
920 }
921
922 /**
923  * ixgb_clean_tx_ring - Free Tx Buffers
924  * @adapter: board private structure
925  **/
926
927 static void
928 ixgb_clean_tx_ring(struct ixgb_adapter *adapter)
929 {
930         struct ixgb_desc_ring *tx_ring = &adapter->tx_ring;
931         struct ixgb_buffer *buffer_info;
932         unsigned long size;
933         unsigned int i;
934
935         /* Free all the Tx ring sk_buffs */
936
937         for(i = 0; i < tx_ring->count; i++) {
938                 buffer_info = &tx_ring->buffer_info[i];
939                 ixgb_unmap_and_free_tx_resource(adapter, buffer_info);
940         }
941
942         size = sizeof(struct ixgb_buffer) * tx_ring->count;
943         memset(tx_ring->buffer_info, 0, size);
944
945         /* Zero out the descriptor ring */
946
947         memset(tx_ring->desc, 0, tx_ring->size);
948
949         tx_ring->next_to_use = 0;
950         tx_ring->next_to_clean = 0;
951
952         IXGB_WRITE_REG(&adapter->hw, TDH, 0);
953         IXGB_WRITE_REG(&adapter->hw, TDT, 0);
954 }
955
956 /**
957  * ixgb_free_rx_resources - Free Rx Resources
958  * @adapter: board private structure
959  *
960  * Free all receive software resources
961  **/
962
963 void
964 ixgb_free_rx_resources(struct ixgb_adapter *adapter)
965 {
966         struct ixgb_desc_ring *rx_ring = &adapter->rx_ring;
967         struct pci_dev *pdev = adapter->pdev;
968
969         ixgb_clean_rx_ring(adapter);
970
971         vfree(rx_ring->buffer_info);
972         rx_ring->buffer_info = NULL;
973
974         pci_free_consistent(pdev, rx_ring->size, rx_ring->desc, rx_ring->dma);
975
976         rx_ring->desc = NULL;
977 }
978
979 /**
980  * ixgb_clean_rx_ring - Free Rx Buffers
981  * @adapter: board private structure
982  **/
983
984 static void
985 ixgb_clean_rx_ring(struct ixgb_adapter *adapter)
986 {
987         struct ixgb_desc_ring *rx_ring = &adapter->rx_ring;
988         struct ixgb_buffer *buffer_info;
989         struct pci_dev *pdev = adapter->pdev;
990         unsigned long size;
991         unsigned int i;
992
993         /* Free all the Rx ring sk_buffs */
994
995         for(i = 0; i < rx_ring->count; i++) {
996                 buffer_info = &rx_ring->buffer_info[i];
997                 if(buffer_info->skb) {
998
999                         pci_unmap_single(pdev,
1000                                          buffer_info->dma,
1001                                          buffer_info->length,
1002                                          PCI_DMA_FROMDEVICE);
1003
1004                         dev_kfree_skb(buffer_info->skb);
1005
1006                         buffer_info->skb = NULL;
1007                 }
1008         }
1009
1010         size = sizeof(struct ixgb_buffer) * rx_ring->count;
1011         memset(rx_ring->buffer_info, 0, size);
1012
1013         /* Zero out the descriptor ring */
1014
1015         memset(rx_ring->desc, 0, rx_ring->size);
1016
1017         rx_ring->next_to_clean = 0;
1018         rx_ring->next_to_use = 0;
1019
1020         IXGB_WRITE_REG(&adapter->hw, RDH, 0);
1021         IXGB_WRITE_REG(&adapter->hw, RDT, 0);
1022 }
1023
1024 /**
1025  * ixgb_set_mac - Change the Ethernet Address of the NIC
1026  * @netdev: network interface device structure
1027  * @p: pointer to an address structure
1028  *
1029  * Returns 0 on success, negative on failure
1030  **/
1031
1032 static int
1033 ixgb_set_mac(struct net_device *netdev, void *p)
1034 {
1035         struct ixgb_adapter *adapter = netdev->priv;
1036         struct sockaddr *addr = p;
1037
1038         if(!is_valid_ether_addr(addr->sa_data))
1039                 return -EADDRNOTAVAIL;
1040
1041         memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
1042
1043         ixgb_rar_set(&adapter->hw, addr->sa_data, 0);
1044
1045         return 0;
1046 }
1047
1048 /**
1049  * ixgb_set_multi - Multicast and Promiscuous mode set
1050  * @netdev: network interface device structure
1051  *
1052  * The set_multi entry point is called whenever the multicast address
1053  * list or the network interface flags are updated.  This routine is
1054  * responsible for configuring the hardware for proper multicast,
1055  * promiscuous mode, and all-multi behavior.
1056  **/
1057
1058 static void
1059 ixgb_set_multi(struct net_device *netdev)
1060 {
1061         struct ixgb_adapter *adapter = netdev->priv;
1062         struct ixgb_hw *hw = &adapter->hw;
1063         struct dev_mc_list *mc_ptr;
1064         uint32_t rctl;
1065         int i;
1066
1067         /* Check for Promiscuous and All Multicast modes */
1068
1069         rctl = IXGB_READ_REG(hw, RCTL);
1070
1071         if(netdev->flags & IFF_PROMISC) {
1072                 rctl |= (IXGB_RCTL_UPE | IXGB_RCTL_MPE);
1073         } else if(netdev->flags & IFF_ALLMULTI) {
1074                 rctl |= IXGB_RCTL_MPE;
1075                 rctl &= ~IXGB_RCTL_UPE;
1076         } else {
1077                 rctl &= ~(IXGB_RCTL_UPE | IXGB_RCTL_MPE);
1078         }
1079
1080         if(netdev->mc_count > IXGB_MAX_NUM_MULTICAST_ADDRESSES) {
1081                 rctl |= IXGB_RCTL_MPE;
1082                 IXGB_WRITE_REG(hw, RCTL, rctl);
1083         } else {
1084                 uint8_t mta[netdev->mc_count * IXGB_ETH_LENGTH_OF_ADDRESS];
1085
1086                 IXGB_WRITE_REG(hw, RCTL, rctl);
1087
1088                 for(i = 0, mc_ptr = netdev->mc_list; mc_ptr;
1089                         i++, mc_ptr = mc_ptr->next)
1090                         memcpy(&mta[i * IXGB_ETH_LENGTH_OF_ADDRESS],
1091                                    mc_ptr->dmi_addr, IXGB_ETH_LENGTH_OF_ADDRESS);
1092
1093                 ixgb_mc_addr_list_update(hw, mta, netdev->mc_count, 0);
1094         }
1095 }
1096
1097 /**
1098  * ixgb_watchdog - Timer Call-back
1099  * @data: pointer to netdev cast into an unsigned long
1100  **/
1101
1102 static void
1103 ixgb_watchdog(unsigned long data)
1104 {
1105         struct ixgb_adapter *adapter = (struct ixgb_adapter *)data;
1106         struct net_device *netdev = adapter->netdev;
1107         struct ixgb_desc_ring *txdr = &adapter->tx_ring;
1108
1109         ixgb_check_for_link(&adapter->hw);
1110
1111         if (ixgb_check_for_bad_link(&adapter->hw)) {
1112                 /* force the reset path */
1113                 netif_stop_queue(netdev);
1114         }
1115
1116         if(adapter->hw.link_up) {
1117                 if(!netif_carrier_ok(netdev)) {
1118                         printk(KERN_INFO "ixgb: %s NIC Link is Up %d Mbps %s\n",
1119                                    netdev->name, 10000, "Full Duplex");
1120                         adapter->link_speed = 10000;
1121                         adapter->link_duplex = FULL_DUPLEX;
1122                         netif_carrier_on(netdev);
1123                         netif_wake_queue(netdev);
1124                 }
1125         } else {
1126                 if(netif_carrier_ok(netdev)) {
1127                         adapter->link_speed = 0;
1128                         adapter->link_duplex = 0;
1129                         printk(KERN_INFO
1130                                    "ixgb: %s NIC Link is Down\n",
1131                                    netdev->name);
1132                         netif_carrier_off(netdev);
1133                         netif_stop_queue(netdev);
1134
1135                 }
1136         }
1137
1138         ixgb_update_stats(adapter);
1139
1140         if(!netif_carrier_ok(netdev)) {
1141                 if(IXGB_DESC_UNUSED(txdr) + 1 < txdr->count) {
1142                         /* We've lost link, so the controller stops DMA,
1143                          * but we've got queued Tx work that's never going
1144                          * to get done, so reset controller to flush Tx.
1145                          * (Do the reset outside of interrupt context). */
1146                         schedule_work(&adapter->tx_timeout_task);
1147                 }
1148         }
1149
1150         /* Force detection of hung controller every watchdog period */
1151         adapter->detect_tx_hung = TRUE;
1152
1153         /* generate an interrupt to force clean up of any stragglers */
1154         IXGB_WRITE_REG(&adapter->hw, ICS, IXGB_INT_TXDW);
1155
1156         /* Reset the timer */
1157         mod_timer(&adapter->watchdog_timer, jiffies + 2 * HZ);
1158 }
1159
1160 #define IXGB_TX_FLAGS_CSUM              0x00000001
1161 #define IXGB_TX_FLAGS_VLAN              0x00000002
1162 #define IXGB_TX_FLAGS_TSO               0x00000004
1163
1164 static inline int
1165 ixgb_tso(struct ixgb_adapter *adapter, struct sk_buff *skb)
1166 {
1167 #ifdef NETIF_F_TSO
1168         struct ixgb_context_desc *context_desc;
1169         unsigned int i;
1170         uint8_t ipcss, ipcso, tucss, tucso, hdr_len;
1171         uint16_t ipcse, tucse, mss;
1172         int err;
1173
1174         if(likely(skb_shinfo(skb)->tso_size)) {
1175                 if (skb_header_cloned(skb)) {
1176                         err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
1177                         if (err)
1178                                 return err;
1179                 }
1180
1181                 hdr_len = ((skb->h.raw - skb->data) + (skb->h.th->doff << 2));
1182                 mss = skb_shinfo(skb)->tso_size;
1183                 skb->nh.iph->tot_len = 0;
1184                 skb->nh.iph->check = 0;
1185                 skb->h.th->check = ~csum_tcpudp_magic(skb->nh.iph->saddr,
1186                                                       skb->nh.iph->daddr,
1187                                                       0, IPPROTO_TCP, 0);
1188                 ipcss = skb->nh.raw - skb->data;
1189                 ipcso = (void *)&(skb->nh.iph->check) - (void *)skb->data;
1190                 ipcse = skb->h.raw - skb->data - 1;
1191                 tucss = skb->h.raw - skb->data;
1192                 tucso = (void *)&(skb->h.th->check) - (void *)skb->data;
1193                 tucse = 0;
1194
1195                 i = adapter->tx_ring.next_to_use;
1196                 context_desc = IXGB_CONTEXT_DESC(adapter->tx_ring, i);
1197
1198                 context_desc->ipcss = ipcss;
1199                 context_desc->ipcso = ipcso;
1200                 context_desc->ipcse = cpu_to_le16(ipcse);
1201                 context_desc->tucss = tucss;
1202                 context_desc->tucso = tucso;
1203                 context_desc->tucse = cpu_to_le16(tucse);
1204                 context_desc->mss = cpu_to_le16(mss);
1205                 context_desc->hdr_len = hdr_len;
1206                 context_desc->status = 0;
1207                 context_desc->cmd_type_len = cpu_to_le32(
1208                                                   IXGB_CONTEXT_DESC_TYPE 
1209                                                 | IXGB_CONTEXT_DESC_CMD_TSE
1210                                                 | IXGB_CONTEXT_DESC_CMD_IP
1211                                                 | IXGB_CONTEXT_DESC_CMD_TCP
1212                                                 | IXGB_CONTEXT_DESC_CMD_IDE
1213                                                 | (skb->len - (hdr_len)));
1214
1215
1216                 if(++i == adapter->tx_ring.count) i = 0;
1217                 adapter->tx_ring.next_to_use = i;
1218
1219                 return 1;
1220         }
1221 #endif
1222
1223         return 0;
1224 }
1225
1226 static inline boolean_t
1227 ixgb_tx_csum(struct ixgb_adapter *adapter, struct sk_buff *skb)
1228 {
1229         struct ixgb_context_desc *context_desc;
1230         unsigned int i;
1231         uint8_t css, cso;
1232
1233         if(likely(skb->ip_summed == CHECKSUM_HW)) {
1234                 css = skb->h.raw - skb->data;
1235                 cso = (skb->h.raw + skb->csum) - skb->data;
1236
1237                 i = adapter->tx_ring.next_to_use;
1238                 context_desc = IXGB_CONTEXT_DESC(adapter->tx_ring, i);
1239
1240                 context_desc->tucss = css;
1241                 context_desc->tucso = cso;
1242                 context_desc->tucse = 0;
1243                 /* zero out any previously existing data in one instruction */
1244                 *(uint32_t *)&(context_desc->ipcss) = 0;
1245                 context_desc->status = 0;
1246                 context_desc->hdr_len = 0;
1247                 context_desc->mss = 0;
1248                 context_desc->cmd_type_len =
1249                         cpu_to_le32(IXGB_CONTEXT_DESC_TYPE
1250                                     | IXGB_TX_DESC_CMD_IDE);
1251
1252                 if(++i == adapter->tx_ring.count) i = 0;
1253                 adapter->tx_ring.next_to_use = i;
1254
1255                 return TRUE;
1256         }
1257
1258         return FALSE;
1259 }
1260
1261 #define IXGB_MAX_TXD_PWR        14
1262 #define IXGB_MAX_DATA_PER_TXD   (1<<IXGB_MAX_TXD_PWR)
1263
1264 static inline int
1265 ixgb_tx_map(struct ixgb_adapter *adapter, struct sk_buff *skb,
1266             unsigned int first)
1267 {
1268         struct ixgb_desc_ring *tx_ring = &adapter->tx_ring;
1269         struct ixgb_buffer *buffer_info;
1270         int len = skb->len;
1271         unsigned int offset = 0, size, count = 0, i;
1272
1273         unsigned int nr_frags = skb_shinfo(skb)->nr_frags;
1274         unsigned int f;
1275         len -= skb->data_len;
1276
1277         i = tx_ring->next_to_use;
1278
1279         while(len) {
1280                 buffer_info = &tx_ring->buffer_info[i];
1281                 size = min(len, IXGB_MAX_JUMBO_FRAME_SIZE);
1282                 buffer_info->length = size;
1283                 buffer_info->dma =
1284                         pci_map_single(adapter->pdev,
1285                                 skb->data + offset,
1286                                 size,
1287                                 PCI_DMA_TODEVICE);
1288                 buffer_info->time_stamp = jiffies;
1289
1290                 len -= size;
1291                 offset += size;
1292                 count++;
1293                 if(++i == tx_ring->count) i = 0;
1294         }
1295
1296         for(f = 0; f < nr_frags; f++) {
1297                 struct skb_frag_struct *frag;
1298
1299                 frag = &skb_shinfo(skb)->frags[f];
1300                 len = frag->size;
1301                 offset = 0;
1302
1303                 while(len) {
1304                         buffer_info = &tx_ring->buffer_info[i];
1305                         size = min(len, IXGB_MAX_JUMBO_FRAME_SIZE);
1306                         buffer_info->length = size;
1307                         buffer_info->dma =
1308                                 pci_map_page(adapter->pdev,
1309                                         frag->page,
1310                                         frag->page_offset + offset,
1311                                         size,
1312                                         PCI_DMA_TODEVICE);
1313                         buffer_info->time_stamp = jiffies;
1314
1315                         len -= size;
1316                         offset += size;
1317                         count++;
1318                         if(++i == tx_ring->count) i = 0;
1319                 }
1320         }
1321         i = (i == 0) ? tx_ring->count - 1 : i - 1;
1322         tx_ring->buffer_info[i].skb = skb;
1323         tx_ring->buffer_info[first].next_to_watch = i;
1324
1325         return count;
1326 }
1327
1328 static inline void
1329 ixgb_tx_queue(struct ixgb_adapter *adapter, int count, int vlan_id,int tx_flags)
1330 {
1331         struct ixgb_desc_ring *tx_ring = &adapter->tx_ring;
1332         struct ixgb_tx_desc *tx_desc = NULL;
1333         struct ixgb_buffer *buffer_info;
1334         uint32_t cmd_type_len = adapter->tx_cmd_type;
1335         uint8_t status = 0;
1336         uint8_t popts = 0;
1337         unsigned int i;
1338
1339         if(tx_flags & IXGB_TX_FLAGS_TSO) {
1340                 cmd_type_len |= IXGB_TX_DESC_CMD_TSE;
1341                 popts |= (IXGB_TX_DESC_POPTS_IXSM | IXGB_TX_DESC_POPTS_TXSM);
1342         }
1343
1344         if(tx_flags & IXGB_TX_FLAGS_CSUM)
1345                 popts |= IXGB_TX_DESC_POPTS_TXSM;
1346
1347         if(tx_flags & IXGB_TX_FLAGS_VLAN) {
1348                 cmd_type_len |= IXGB_TX_DESC_CMD_VLE;
1349         }
1350
1351         i = tx_ring->next_to_use;
1352
1353         while(count--) {
1354                 buffer_info = &tx_ring->buffer_info[i];
1355                 tx_desc = IXGB_TX_DESC(*tx_ring, i);
1356                 tx_desc->buff_addr = cpu_to_le64(buffer_info->dma);
1357                 tx_desc->cmd_type_len =
1358                         cpu_to_le32(cmd_type_len | buffer_info->length);
1359                 tx_desc->status = status;
1360                 tx_desc->popts = popts;
1361                 tx_desc->vlan = cpu_to_le16(vlan_id);
1362
1363                 if(++i == tx_ring->count) i = 0;
1364         }
1365
1366         tx_desc->cmd_type_len |= cpu_to_le32(IXGB_TX_DESC_CMD_EOP 
1367                                 | IXGB_TX_DESC_CMD_RS );
1368
1369         /* Force memory writes to complete before letting h/w
1370          * know there are new descriptors to fetch.  (Only
1371          * applicable for weak-ordered memory model archs,
1372          * such as IA-64). */
1373         wmb();
1374
1375         tx_ring->next_to_use = i;
1376         IXGB_WRITE_REG(&adapter->hw, TDT, i);
1377 }
1378
1379 /* Tx Descriptors needed, worst case */
1380 #define TXD_USE_COUNT(S) (((S) >> IXGB_MAX_TXD_PWR) + \
1381                          (((S) & (IXGB_MAX_DATA_PER_TXD - 1)) ? 1 : 0))
1382 #define DESC_NEEDED TXD_USE_COUNT(IXGB_MAX_DATA_PER_TXD) + \
1383         MAX_SKB_FRAGS * TXD_USE_COUNT(PAGE_SIZE) + 1
1384
1385 static int
1386 ixgb_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
1387 {
1388         struct ixgb_adapter *adapter = netdev->priv;
1389         unsigned int first;
1390         unsigned int tx_flags = 0;
1391         unsigned long flags;
1392         int vlan_id = 0;
1393         int tso;
1394
1395         if(skb->len <= 0) {
1396                 dev_kfree_skb_any(skb);
1397                 return 0;
1398         }
1399
1400         spin_lock_irqsave(&adapter->tx_lock, flags);
1401         if(unlikely(IXGB_DESC_UNUSED(&adapter->tx_ring) < DESC_NEEDED)) {
1402                 netif_stop_queue(netdev);
1403                 spin_unlock_irqrestore(&adapter->tx_lock, flags);
1404                 return 1;
1405         }
1406         spin_unlock_irqrestore(&adapter->tx_lock, flags);
1407
1408         if(adapter->vlgrp && vlan_tx_tag_present(skb)) {
1409                 tx_flags |= IXGB_TX_FLAGS_VLAN;
1410                 vlan_id = vlan_tx_tag_get(skb);
1411         }
1412
1413         first = adapter->tx_ring.next_to_use;
1414         
1415         tso = ixgb_tso(adapter, skb);
1416         if (tso < 0) {
1417                 dev_kfree_skb_any(skb);
1418                 return NETDEV_TX_OK;
1419         }
1420
1421         if (tso)
1422                 tx_flags |= IXGB_TX_FLAGS_TSO;
1423         else if(ixgb_tx_csum(adapter, skb))
1424                 tx_flags |= IXGB_TX_FLAGS_CSUM;
1425
1426         ixgb_tx_queue(adapter, ixgb_tx_map(adapter, skb, first), vlan_id,
1427                         tx_flags);
1428
1429         netdev->trans_start = jiffies;
1430
1431         return 0;
1432 }
1433
1434 /**
1435  * ixgb_tx_timeout - Respond to a Tx Hang
1436  * @netdev: network interface device structure
1437  **/
1438
1439 static void
1440 ixgb_tx_timeout(struct net_device *netdev)
1441 {
1442         struct ixgb_adapter *adapter = netdev->priv;
1443
1444         /* Do the reset outside of interrupt context */
1445         schedule_work(&adapter->tx_timeout_task);
1446 }
1447
1448 static void
1449 ixgb_tx_timeout_task(struct net_device *netdev)
1450 {
1451         struct ixgb_adapter *adapter = netdev->priv;
1452
1453         ixgb_down(adapter, TRUE);
1454         ixgb_up(adapter);
1455 }
1456
1457 /**
1458  * ixgb_get_stats - Get System Network Statistics
1459  * @netdev: network interface device structure
1460  *
1461  * Returns the address of the device statistics structure.
1462  * The statistics are actually updated from the timer callback.
1463  **/
1464
1465 static struct net_device_stats *
1466 ixgb_get_stats(struct net_device *netdev)
1467 {
1468         struct ixgb_adapter *adapter = netdev->priv;
1469
1470         return &adapter->net_stats;
1471 }
1472
1473 /**
1474  * ixgb_change_mtu - Change the Maximum Transfer Unit
1475  * @netdev: network interface device structure
1476  * @new_mtu: new value for maximum frame size
1477  *
1478  * Returns 0 on success, negative on failure
1479  **/
1480
1481 static int
1482 ixgb_change_mtu(struct net_device *netdev, int new_mtu)
1483 {
1484         struct ixgb_adapter *adapter = netdev->priv;
1485         int max_frame = new_mtu + ENET_HEADER_SIZE + ENET_FCS_LENGTH;
1486         int old_max_frame = netdev->mtu + ENET_HEADER_SIZE + ENET_FCS_LENGTH;
1487
1488
1489         if((max_frame < IXGB_MIN_ENET_FRAME_SIZE_WITHOUT_FCS + ENET_FCS_LENGTH)
1490            || (max_frame > IXGB_MAX_JUMBO_FRAME_SIZE + ENET_FCS_LENGTH)) {
1491                 IXGB_ERR("Invalid MTU setting\n");
1492                 return -EINVAL;
1493         }
1494
1495         if((max_frame <= IXGB_MAX_ENET_FRAME_SIZE_WITHOUT_FCS + ENET_FCS_LENGTH)
1496            || (max_frame <= IXGB_RXBUFFER_2048)) {
1497                 adapter->rx_buffer_len = IXGB_RXBUFFER_2048;
1498
1499         } else if(max_frame <= IXGB_RXBUFFER_4096) {
1500                 adapter->rx_buffer_len = IXGB_RXBUFFER_4096;
1501
1502         } else if(max_frame <= IXGB_RXBUFFER_8192) {
1503                 adapter->rx_buffer_len = IXGB_RXBUFFER_8192;
1504
1505         } else {
1506                 adapter->rx_buffer_len = IXGB_RXBUFFER_16384;
1507         }
1508
1509         netdev->mtu = new_mtu;
1510
1511         if(old_max_frame != max_frame && netif_running(netdev)) {
1512
1513                 ixgb_down(adapter, TRUE);
1514                 ixgb_up(adapter);
1515         }
1516
1517         return 0;
1518 }
1519
1520 /**
1521  * ixgb_update_stats - Update the board statistics counters.
1522  * @adapter: board private structure
1523  **/
1524
1525 void
1526 ixgb_update_stats(struct ixgb_adapter *adapter)
1527 {
1528         struct net_device *netdev = adapter->netdev;
1529
1530         if((netdev->flags & IFF_PROMISC) || (netdev->flags & IFF_ALLMULTI) ||
1531            (netdev->mc_count > IXGB_MAX_NUM_MULTICAST_ADDRESSES)) {
1532                 u64 multi = IXGB_READ_REG(&adapter->hw, MPRCL);
1533                 u32 bcast_l = IXGB_READ_REG(&adapter->hw, BPRCL);
1534                 u32 bcast_h = IXGB_READ_REG(&adapter->hw, BPRCH);
1535                 u64 bcast = ((u64)bcast_h << 32) | bcast_l; 
1536
1537                 multi |= ((u64)IXGB_READ_REG(&adapter->hw, MPRCH) << 32);
1538                 /* fix up multicast stats by removing broadcasts */
1539                 multi -= bcast;
1540                 
1541                 adapter->stats.mprcl += (multi & 0xFFFFFFFF);
1542                 adapter->stats.mprch += (multi >> 32);
1543                 adapter->stats.bprcl += bcast_l; 
1544                 adapter->stats.bprch += bcast_h;
1545         } else {
1546                 adapter->stats.mprcl += IXGB_READ_REG(&adapter->hw, MPRCL);
1547                 adapter->stats.mprch += IXGB_READ_REG(&adapter->hw, MPRCH);
1548                 adapter->stats.bprcl += IXGB_READ_REG(&adapter->hw, BPRCL);
1549                 adapter->stats.bprch += IXGB_READ_REG(&adapter->hw, BPRCH);
1550         }
1551         adapter->stats.tprl += IXGB_READ_REG(&adapter->hw, TPRL);
1552         adapter->stats.tprh += IXGB_READ_REG(&adapter->hw, TPRH);
1553         adapter->stats.gprcl += IXGB_READ_REG(&adapter->hw, GPRCL);
1554         adapter->stats.gprch += IXGB_READ_REG(&adapter->hw, GPRCH);
1555         adapter->stats.uprcl += IXGB_READ_REG(&adapter->hw, UPRCL);
1556         adapter->stats.uprch += IXGB_READ_REG(&adapter->hw, UPRCH);
1557         adapter->stats.vprcl += IXGB_READ_REG(&adapter->hw, VPRCL);
1558         adapter->stats.vprch += IXGB_READ_REG(&adapter->hw, VPRCH);
1559         adapter->stats.jprcl += IXGB_READ_REG(&adapter->hw, JPRCL);
1560         adapter->stats.jprch += IXGB_READ_REG(&adapter->hw, JPRCH);
1561         adapter->stats.gorcl += IXGB_READ_REG(&adapter->hw, GORCL);
1562         adapter->stats.gorch += IXGB_READ_REG(&adapter->hw, GORCH);
1563         adapter->stats.torl += IXGB_READ_REG(&adapter->hw, TORL);
1564         adapter->stats.torh += IXGB_READ_REG(&adapter->hw, TORH);
1565         adapter->stats.rnbc += IXGB_READ_REG(&adapter->hw, RNBC);
1566         adapter->stats.ruc += IXGB_READ_REG(&adapter->hw, RUC);
1567         adapter->stats.roc += IXGB_READ_REG(&adapter->hw, ROC);
1568         adapter->stats.rlec += IXGB_READ_REG(&adapter->hw, RLEC);
1569         adapter->stats.crcerrs += IXGB_READ_REG(&adapter->hw, CRCERRS);
1570         adapter->stats.icbc += IXGB_READ_REG(&adapter->hw, ICBC);
1571         adapter->stats.ecbc += IXGB_READ_REG(&adapter->hw, ECBC);
1572         adapter->stats.mpc += IXGB_READ_REG(&adapter->hw, MPC);
1573         adapter->stats.tptl += IXGB_READ_REG(&adapter->hw, TPTL);
1574         adapter->stats.tpth += IXGB_READ_REG(&adapter->hw, TPTH);
1575         adapter->stats.gptcl += IXGB_READ_REG(&adapter->hw, GPTCL);
1576         adapter->stats.gptch += IXGB_READ_REG(&adapter->hw, GPTCH);
1577         adapter->stats.bptcl += IXGB_READ_REG(&adapter->hw, BPTCL);
1578         adapter->stats.bptch += IXGB_READ_REG(&adapter->hw, BPTCH);
1579         adapter->stats.mptcl += IXGB_READ_REG(&adapter->hw, MPTCL);
1580         adapter->stats.mptch += IXGB_READ_REG(&adapter->hw, MPTCH);
1581         adapter->stats.uptcl += IXGB_READ_REG(&adapter->hw, UPTCL);
1582         adapter->stats.uptch += IXGB_READ_REG(&adapter->hw, UPTCH);
1583         adapter->stats.vptcl += IXGB_READ_REG(&adapter->hw, VPTCL);
1584         adapter->stats.vptch += IXGB_READ_REG(&adapter->hw, VPTCH);
1585         adapter->stats.jptcl += IXGB_READ_REG(&adapter->hw, JPTCL);
1586         adapter->stats.jptch += IXGB_READ_REG(&adapter->hw, JPTCH);
1587         adapter->stats.gotcl += IXGB_READ_REG(&adapter->hw, GOTCL);
1588         adapter->stats.gotch += IXGB_READ_REG(&adapter->hw, GOTCH);
1589         adapter->stats.totl += IXGB_READ_REG(&adapter->hw, TOTL);
1590         adapter->stats.toth += IXGB_READ_REG(&adapter->hw, TOTH);
1591         adapter->stats.dc += IXGB_READ_REG(&adapter->hw, DC);
1592         adapter->stats.plt64c += IXGB_READ_REG(&adapter->hw, PLT64C);
1593         adapter->stats.tsctc += IXGB_READ_REG(&adapter->hw, TSCTC);
1594         adapter->stats.tsctfc += IXGB_READ_REG(&adapter->hw, TSCTFC);
1595         adapter->stats.ibic += IXGB_READ_REG(&adapter->hw, IBIC);
1596         adapter->stats.rfc += IXGB_READ_REG(&adapter->hw, RFC);
1597         adapter->stats.lfc += IXGB_READ_REG(&adapter->hw, LFC);
1598         adapter->stats.pfrc += IXGB_READ_REG(&adapter->hw, PFRC);
1599         adapter->stats.pftc += IXGB_READ_REG(&adapter->hw, PFTC);
1600         adapter->stats.mcfrc += IXGB_READ_REG(&adapter->hw, MCFRC);
1601         adapter->stats.mcftc += IXGB_READ_REG(&adapter->hw, MCFTC);
1602         adapter->stats.xonrxc += IXGB_READ_REG(&adapter->hw, XONRXC);
1603         adapter->stats.xontxc += IXGB_READ_REG(&adapter->hw, XONTXC);
1604         adapter->stats.xoffrxc += IXGB_READ_REG(&adapter->hw, XOFFRXC);
1605         adapter->stats.xofftxc += IXGB_READ_REG(&adapter->hw, XOFFTXC);
1606         adapter->stats.rjc += IXGB_READ_REG(&adapter->hw, RJC);
1607
1608         /* Fill out the OS statistics structure */
1609
1610         adapter->net_stats.rx_packets = adapter->stats.gprcl;
1611         adapter->net_stats.tx_packets = adapter->stats.gptcl;
1612         adapter->net_stats.rx_bytes = adapter->stats.gorcl;
1613         adapter->net_stats.tx_bytes = adapter->stats.gotcl;
1614         adapter->net_stats.multicast = adapter->stats.mprcl;
1615         adapter->net_stats.collisions = 0;
1616
1617         /* ignore RLEC as it reports errors for padded (<64bytes) frames
1618          * with a length in the type/len field */
1619         adapter->net_stats.rx_errors =
1620             /* adapter->stats.rnbc + */ adapter->stats.crcerrs +
1621             adapter->stats.ruc +
1622             adapter->stats.roc /*+ adapter->stats.rlec */  +
1623             adapter->stats.icbc +
1624             adapter->stats.ecbc + adapter->stats.mpc;
1625
1626         adapter->net_stats.rx_dropped = adapter->stats.mpc;
1627
1628         /* see above
1629          * adapter->net_stats.rx_length_errors = adapter->stats.rlec;
1630          */
1631
1632         adapter->net_stats.rx_crc_errors = adapter->stats.crcerrs;
1633         adapter->net_stats.rx_fifo_errors = adapter->stats.mpc;
1634         adapter->net_stats.rx_missed_errors = adapter->stats.mpc;
1635         adapter->net_stats.rx_over_errors = adapter->stats.mpc;
1636
1637         adapter->net_stats.tx_errors = 0;
1638         adapter->net_stats.rx_frame_errors = 0;
1639         adapter->net_stats.tx_aborted_errors = 0;
1640         adapter->net_stats.tx_carrier_errors = 0;
1641         adapter->net_stats.tx_fifo_errors = 0;
1642         adapter->net_stats.tx_heartbeat_errors = 0;
1643         adapter->net_stats.tx_window_errors = 0;
1644 }
1645
1646 #define IXGB_MAX_INTR 10
1647 /**
1648  * ixgb_intr - Interrupt Handler
1649  * @irq: interrupt number
1650  * @data: pointer to a network interface device structure
1651  * @pt_regs: CPU registers structure
1652  **/
1653
1654 static irqreturn_t
1655 ixgb_intr(int irq, void *data, struct pt_regs *regs)
1656 {
1657         struct net_device *netdev = data;
1658         struct ixgb_adapter *adapter = netdev->priv;
1659         struct ixgb_hw *hw = &adapter->hw;
1660         uint32_t icr = IXGB_READ_REG(hw, ICR);
1661 #ifndef CONFIG_IXGB_NAPI
1662         unsigned int i;
1663 #endif
1664
1665         if(unlikely(!icr))
1666                 return IRQ_NONE;  /* Not our interrupt */
1667
1668         if(unlikely(icr & (IXGB_INT_RXSEQ | IXGB_INT_LSC))) {
1669                 mod_timer(&adapter->watchdog_timer, jiffies);
1670         }
1671
1672 #ifdef CONFIG_IXGB_NAPI
1673         if(netif_rx_schedule_prep(netdev)) {
1674
1675                 /* Disable interrupts and register for poll. The flush 
1676                   of the posted write is intentionally left out.
1677                 */
1678
1679                 atomic_inc(&adapter->irq_sem);
1680                 IXGB_WRITE_REG(&adapter->hw, IMC, ~0);
1681                 __netif_rx_schedule(netdev);
1682         }
1683 #else
1684         /* yes, that is actually a & and it is meant to make sure that
1685          * every pass through this for loop checks both receive and
1686          * transmit queues for completed descriptors, intended to
1687          * avoid starvation issues and assist tx/rx fairness. */
1688         for(i = 0; i < IXGB_MAX_INTR; i++)
1689                 if(!ixgb_clean_rx_irq(adapter) &
1690                    !ixgb_clean_tx_irq(adapter))
1691                         break;
1692 #endif 
1693         return IRQ_HANDLED;
1694 }
1695
1696 #ifdef CONFIG_IXGB_NAPI
1697 /**
1698  * ixgb_clean - NAPI Rx polling callback
1699  * @adapter: board private structure
1700  **/
1701
1702 static int
1703 ixgb_clean(struct net_device *netdev, int *budget)
1704 {
1705         struct ixgb_adapter *adapter = netdev->priv;
1706         int work_to_do = min(*budget, netdev->quota);
1707         int tx_cleaned;
1708         int work_done = 0;
1709
1710         tx_cleaned = ixgb_clean_tx_irq(adapter);
1711         ixgb_clean_rx_irq(adapter, &work_done, work_to_do);
1712
1713         *budget -= work_done;
1714         netdev->quota -= work_done;
1715
1716         /* if no Tx and not enough Rx work done, exit the polling mode */
1717         if((!tx_cleaned && (work_done == 0)) || !netif_running(netdev)) {
1718                 netif_rx_complete(netdev);
1719                 ixgb_irq_enable(adapter);
1720                 return 0;
1721         }
1722
1723         return 1;
1724 }
1725 #endif
1726
1727 /**
1728  * ixgb_clean_tx_irq - Reclaim resources after transmit completes
1729  * @adapter: board private structure
1730  **/
1731
1732 static boolean_t
1733 ixgb_clean_tx_irq(struct ixgb_adapter *adapter)
1734 {
1735         struct ixgb_desc_ring *tx_ring = &adapter->tx_ring;
1736         struct net_device *netdev = adapter->netdev;
1737         struct ixgb_tx_desc *tx_desc, *eop_desc;
1738         struct ixgb_buffer *buffer_info;
1739         unsigned int i, eop;
1740         boolean_t cleaned = FALSE;
1741
1742         i = tx_ring->next_to_clean;
1743         eop = tx_ring->buffer_info[i].next_to_watch;
1744         eop_desc = IXGB_TX_DESC(*tx_ring, eop);
1745
1746         while(eop_desc->status & IXGB_TX_DESC_STATUS_DD) {
1747
1748                 for(cleaned = FALSE; !cleaned; ) {
1749                         tx_desc = IXGB_TX_DESC(*tx_ring, i);
1750                         buffer_info = &tx_ring->buffer_info[i];
1751
1752                         if (tx_desc->popts
1753                             & (IXGB_TX_DESC_POPTS_TXSM |
1754                                IXGB_TX_DESC_POPTS_IXSM))
1755                                 adapter->hw_csum_tx_good++;
1756
1757                         ixgb_unmap_and_free_tx_resource(adapter, buffer_info);
1758
1759                         *(uint32_t *)&(tx_desc->status) = 0;
1760
1761                         cleaned = (i == eop);
1762                         if(++i == tx_ring->count) i = 0;
1763                 }
1764
1765                 eop = tx_ring->buffer_info[i].next_to_watch;
1766                 eop_desc = IXGB_TX_DESC(*tx_ring, eop);
1767         }
1768
1769         tx_ring->next_to_clean = i;
1770
1771         spin_lock(&adapter->tx_lock);
1772         if(cleaned && netif_queue_stopped(netdev) && netif_carrier_ok(netdev) &&
1773            (IXGB_DESC_UNUSED(tx_ring) > IXGB_TX_QUEUE_WAKE)) {
1774
1775                 netif_wake_queue(netdev);
1776         }
1777         spin_unlock(&adapter->tx_lock);
1778
1779         if(adapter->detect_tx_hung) {
1780                 /* detect a transmit hang in hardware, this serializes the
1781                  * check with the clearing of time_stamp and movement of i */
1782                 adapter->detect_tx_hung = FALSE;
1783                 if(tx_ring->buffer_info[i].dma &&
1784                    time_after(jiffies, tx_ring->buffer_info[i].time_stamp + HZ)
1785                    && !(IXGB_READ_REG(&adapter->hw, STATUS) &
1786                         IXGB_STATUS_TXOFF))
1787                         netif_stop_queue(netdev);
1788         }
1789
1790         return cleaned;
1791 }
1792
1793 /**
1794  * ixgb_rx_checksum - Receive Checksum Offload for 82597.
1795  * @adapter: board private structure
1796  * @rx_desc: receive descriptor
1797  * @sk_buff: socket buffer with received data
1798  **/
1799
1800 static inline void
1801 ixgb_rx_checksum(struct ixgb_adapter *adapter,
1802                  struct ixgb_rx_desc *rx_desc,
1803                  struct sk_buff *skb)
1804 {
1805         /* Ignore Checksum bit is set OR
1806          * TCP Checksum has not been calculated
1807          */
1808         if((rx_desc->status & IXGB_RX_DESC_STATUS_IXSM) ||
1809            (!(rx_desc->status & IXGB_RX_DESC_STATUS_TCPCS))) {
1810                 skb->ip_summed = CHECKSUM_NONE;
1811                 return;
1812         }
1813
1814         /* At this point we know the hardware did the TCP checksum */
1815         /* now look at the TCP checksum error bit */
1816         if(rx_desc->errors & IXGB_RX_DESC_ERRORS_TCPE) {
1817                 /* let the stack verify checksum errors */
1818                 skb->ip_summed = CHECKSUM_NONE;
1819                 adapter->hw_csum_rx_error++;
1820         } else {
1821                 /* TCP checksum is good */
1822                 skb->ip_summed = CHECKSUM_UNNECESSARY;
1823                 adapter->hw_csum_rx_good++;
1824         }
1825 }
1826
1827 /**
1828  * ixgb_clean_rx_irq - Send received data up the network stack,
1829  * @adapter: board private structure
1830  **/
1831
1832 static boolean_t
1833 #ifdef CONFIG_IXGB_NAPI
1834 ixgb_clean_rx_irq(struct ixgb_adapter *adapter, int *work_done, int work_to_do)
1835 #else
1836 ixgb_clean_rx_irq(struct ixgb_adapter *adapter)
1837 #endif
1838 {
1839         struct ixgb_desc_ring *rx_ring = &adapter->rx_ring;
1840         struct net_device *netdev = adapter->netdev;
1841         struct pci_dev *pdev = adapter->pdev;
1842         struct ixgb_rx_desc *rx_desc, *next_rxd;
1843         struct ixgb_buffer *buffer_info, *next_buffer, *next2_buffer;
1844         struct sk_buff *skb, *next_skb;
1845         uint32_t length;
1846         unsigned int i, j;
1847         boolean_t cleaned = FALSE;
1848
1849         i = rx_ring->next_to_clean;
1850         rx_desc = IXGB_RX_DESC(*rx_ring, i);
1851         buffer_info = &rx_ring->buffer_info[i];
1852
1853         while(rx_desc->status & IXGB_RX_DESC_STATUS_DD) {
1854
1855 #ifdef CONFIG_IXGB_NAPI
1856                 if(*work_done >= work_to_do)
1857                         break;
1858
1859                 (*work_done)++;
1860 #endif
1861                 skb = buffer_info->skb;
1862                 prefetch(skb->data);
1863
1864                 if(++i == rx_ring->count) i = 0;
1865                 next_rxd = IXGB_RX_DESC(*rx_ring, i);
1866                 prefetch(next_rxd);
1867
1868                 if((j = i + 1) == rx_ring->count) j = 0;
1869                 next2_buffer = &rx_ring->buffer_info[j];
1870                 prefetch(next2_buffer);
1871
1872                 next_buffer = &rx_ring->buffer_info[i];
1873                 next_skb = next_buffer->skb;
1874                 prefetch(next_skb);
1875
1876
1877                 cleaned = TRUE;
1878
1879                 pci_unmap_single(pdev,
1880                                  buffer_info->dma,
1881                                  buffer_info->length,
1882                                  PCI_DMA_FROMDEVICE);
1883
1884                 length = le16_to_cpu(rx_desc->length);
1885
1886                 if(unlikely(!(rx_desc->status & IXGB_RX_DESC_STATUS_EOP))) {
1887
1888                         /* All receives must fit into a single buffer */
1889
1890                         IXGB_DBG("Receive packet consumed multiple buffers "
1891                                          "length<%x>\n", length);
1892
1893                         dev_kfree_skb_irq(skb);
1894                         rx_desc->status = 0;
1895                         buffer_info->skb = NULL;
1896
1897                         rx_desc = next_rxd;
1898                         buffer_info = next_buffer;
1899                         continue;
1900                 }
1901
1902                 if (unlikely(rx_desc->errors
1903                              & (IXGB_RX_DESC_ERRORS_CE | IXGB_RX_DESC_ERRORS_SE
1904                                 | IXGB_RX_DESC_ERRORS_P |
1905                                 IXGB_RX_DESC_ERRORS_RXE))) {
1906
1907                         dev_kfree_skb_irq(skb);
1908                         rx_desc->status = 0;
1909                         buffer_info->skb = NULL;
1910
1911                         rx_desc = next_rxd;
1912                         buffer_info = next_buffer;
1913                         continue;
1914                 }
1915
1916                 /* Good Receive */
1917                 skb_put(skb, length);
1918
1919                 /* Receive Checksum Offload */
1920                 ixgb_rx_checksum(adapter, rx_desc, skb);
1921
1922                 skb->protocol = eth_type_trans(skb, netdev);
1923 #ifdef CONFIG_IXGB_NAPI
1924                 if(adapter->vlgrp && (rx_desc->status & IXGB_RX_DESC_STATUS_VP)) {
1925                         vlan_hwaccel_receive_skb(skb, adapter->vlgrp,
1926                                 le16_to_cpu(rx_desc->special) &
1927                                         IXGB_RX_DESC_SPECIAL_VLAN_MASK);
1928                 } else {
1929                         netif_receive_skb(skb);
1930                 }
1931 #else /* CONFIG_IXGB_NAPI */
1932                 if(adapter->vlgrp && (rx_desc->status & IXGB_RX_DESC_STATUS_VP)) {
1933                         vlan_hwaccel_rx(skb, adapter->vlgrp,
1934                                 le16_to_cpu(rx_desc->special) &
1935                                         IXGB_RX_DESC_SPECIAL_VLAN_MASK);
1936                 } else {
1937                         netif_rx(skb);
1938                 }
1939 #endif /* CONFIG_IXGB_NAPI */
1940                 netdev->last_rx = jiffies;
1941
1942                 rx_desc->status = 0;
1943                 buffer_info->skb = NULL;
1944
1945                 rx_desc = next_rxd;
1946                 buffer_info = next_buffer;
1947         }
1948
1949         rx_ring->next_to_clean = i;
1950
1951         ixgb_alloc_rx_buffers(adapter);
1952
1953         return cleaned;
1954 }
1955
1956 /**
1957  * ixgb_alloc_rx_buffers - Replace used receive buffers
1958  * @adapter: address of board private structure
1959  **/
1960
1961 static void
1962 ixgb_alloc_rx_buffers(struct ixgb_adapter *adapter)
1963 {
1964         struct ixgb_desc_ring *rx_ring = &adapter->rx_ring;
1965         struct net_device *netdev = adapter->netdev;
1966         struct pci_dev *pdev = adapter->pdev;
1967         struct ixgb_rx_desc *rx_desc;
1968         struct ixgb_buffer *buffer_info;
1969         struct sk_buff *skb;
1970         unsigned int i;
1971         int num_group_tail_writes;
1972         long cleancount;
1973
1974         i = rx_ring->next_to_use;
1975         buffer_info = &rx_ring->buffer_info[i];
1976         cleancount = IXGB_DESC_UNUSED(rx_ring);
1977
1978         num_group_tail_writes = IXGB_RX_BUFFER_WRITE;
1979
1980         /* leave one descriptor unused */
1981         while(--cleancount > 0) {
1982                 rx_desc = IXGB_RX_DESC(*rx_ring, i);
1983
1984                 skb = dev_alloc_skb(adapter->rx_buffer_len + NET_IP_ALIGN);
1985
1986                 if(unlikely(!skb)) {
1987                         /* Better luck next round */
1988                         break;
1989                 }
1990
1991                 /* Make buffer alignment 2 beyond a 16 byte boundary
1992                  * this will result in a 16 byte aligned IP header after
1993                  * the 14 byte MAC header is removed
1994                  */
1995                 skb_reserve(skb, NET_IP_ALIGN);
1996
1997                 skb->dev = netdev;
1998
1999                 buffer_info->skb = skb;
2000                 buffer_info->length = adapter->rx_buffer_len;
2001                 buffer_info->dma =
2002                         pci_map_single(pdev,
2003                                    skb->data,
2004                                    adapter->rx_buffer_len,
2005                                    PCI_DMA_FROMDEVICE);
2006
2007                 rx_desc->buff_addr = cpu_to_le64(buffer_info->dma);
2008
2009                 if((i & ~(num_group_tail_writes- 1)) == i) {
2010                         /* Force memory writes to complete before letting h/w
2011                          * know there are new descriptors to fetch.  (Only
2012                          * applicable for weak-ordered memory model archs,
2013                          * such as IA-64). */
2014                         wmb();
2015
2016                         IXGB_WRITE_REG(&adapter->hw, RDT, i);
2017                 }
2018
2019                 if(++i == rx_ring->count) i = 0;
2020                 buffer_info = &rx_ring->buffer_info[i];
2021         }
2022
2023         rx_ring->next_to_use = i;
2024 }
2025
2026 /**
2027  * ixgb_vlan_rx_register - enables or disables vlan tagging/stripping.
2028  * 
2029  * @param netdev network interface device structure
2030  * @param grp indicates to enable or disable tagging/stripping
2031  **/
2032 static void
2033 ixgb_vlan_rx_register(struct net_device *netdev, struct vlan_group *grp)
2034 {
2035         struct ixgb_adapter *adapter = netdev->priv;
2036         uint32_t ctrl, rctl;
2037
2038         ixgb_irq_disable(adapter);
2039         adapter->vlgrp = grp;
2040
2041         if(grp) {
2042                 /* enable VLAN tag insert/strip */
2043                 ctrl = IXGB_READ_REG(&adapter->hw, CTRL0);
2044                 ctrl |= IXGB_CTRL0_VME;
2045                 IXGB_WRITE_REG(&adapter->hw, CTRL0, ctrl);
2046
2047                 /* enable VLAN receive filtering */
2048
2049                 rctl = IXGB_READ_REG(&adapter->hw, RCTL);
2050                 rctl |= IXGB_RCTL_VFE;
2051                 rctl &= ~IXGB_RCTL_CFIEN;
2052                 IXGB_WRITE_REG(&adapter->hw, RCTL, rctl);
2053         } else {
2054                 /* disable VLAN tag insert/strip */
2055
2056                 ctrl = IXGB_READ_REG(&adapter->hw, CTRL0);
2057                 ctrl &= ~IXGB_CTRL0_VME;
2058                 IXGB_WRITE_REG(&adapter->hw, CTRL0, ctrl);
2059
2060                 /* disable VLAN filtering */
2061
2062                 rctl = IXGB_READ_REG(&adapter->hw, RCTL);
2063                 rctl &= ~IXGB_RCTL_VFE;
2064                 IXGB_WRITE_REG(&adapter->hw, RCTL, rctl);
2065         }
2066
2067         ixgb_irq_enable(adapter);
2068 }
2069
2070 static void
2071 ixgb_vlan_rx_add_vid(struct net_device *netdev, uint16_t vid)
2072 {
2073         struct ixgb_adapter *adapter = netdev->priv;
2074         uint32_t vfta, index;
2075
2076         /* add VID to filter table */
2077
2078         index = (vid >> 5) & 0x7F;
2079         vfta = IXGB_READ_REG_ARRAY(&adapter->hw, VFTA, index);
2080         vfta |= (1 << (vid & 0x1F));
2081         ixgb_write_vfta(&adapter->hw, index, vfta);
2082 }
2083
2084 static void
2085 ixgb_vlan_rx_kill_vid(struct net_device *netdev, uint16_t vid)
2086 {
2087         struct ixgb_adapter *adapter = netdev->priv;
2088         uint32_t vfta, index;
2089
2090         ixgb_irq_disable(adapter);
2091
2092         if(adapter->vlgrp)
2093                 adapter->vlgrp->vlan_devices[vid] = NULL;
2094
2095         ixgb_irq_enable(adapter);
2096
2097         /* remove VID from filter table*/
2098
2099         index = (vid >> 5) & 0x7F;
2100         vfta = IXGB_READ_REG_ARRAY(&adapter->hw, VFTA, index);
2101         vfta &= ~(1 << (vid & 0x1F));
2102         ixgb_write_vfta(&adapter->hw, index, vfta);
2103 }
2104
2105 static void
2106 ixgb_restore_vlan(struct ixgb_adapter *adapter)
2107 {
2108         ixgb_vlan_rx_register(adapter->netdev, adapter->vlgrp);
2109
2110         if(adapter->vlgrp) {
2111                 uint16_t vid;
2112                 for(vid = 0; vid < VLAN_GROUP_ARRAY_LEN; vid++) {
2113                         if(!adapter->vlgrp->vlan_devices[vid])
2114                                 continue;
2115                         ixgb_vlan_rx_add_vid(adapter->netdev, vid);
2116                 }
2117         }
2118 }
2119
2120 /**
2121  * ixgb_notify_reboot - handles OS notification of reboot event.
2122  * @param nb notifier block, unused
2123  * @param event Event being passed to driver to act upon
2124  * @param p A pointer to our net device
2125  **/
2126 static int
2127 ixgb_notify_reboot(struct notifier_block *nb, unsigned long event, void *p)
2128 {
2129         struct pci_dev *pdev = NULL;
2130
2131         switch(event) {
2132         case SYS_DOWN:
2133         case SYS_HALT:
2134         case SYS_POWER_OFF:
2135                 while ((pdev = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, pdev))) {
2136                         if (pci_dev_driver(pdev) == &ixgb_driver)
2137                                 ixgb_suspend(pdev, 3);
2138                 }
2139         }
2140         return NOTIFY_DONE;
2141 }
2142
2143 /**
2144  * ixgb_suspend - driver suspend function called from notify.
2145  * @param pdev pci driver structure used for passing to
2146  * @param state power state to enter 
2147  **/
2148 static int
2149 ixgb_suspend(struct pci_dev *pdev, uint32_t state)
2150 {
2151         struct net_device *netdev = pci_get_drvdata(pdev);
2152         struct ixgb_adapter *adapter = netdev->priv;
2153
2154         netif_device_detach(netdev);
2155
2156         if(netif_running(netdev))
2157                 ixgb_down(adapter, TRUE);
2158
2159         pci_save_state(pdev);
2160
2161         state = (state > 0) ? 3 : 0;
2162         pci_set_power_state(pdev, state);
2163         msec_delay(200);
2164
2165         return 0;
2166 }
2167
2168 #ifdef CONFIG_NET_POLL_CONTROLLER
2169 /*
2170  * Polling 'interrupt' - used by things like netconsole to send skbs
2171  * without having to re-enable interrupts. It's not called while
2172  * the interrupt routine is executing.
2173  */
2174
2175 static void ixgb_netpoll(struct net_device *dev)
2176 {
2177         struct ixgb_adapter *adapter = dev->priv;
2178         disable_irq(adapter->pdev->irq);
2179         ixgb_intr(adapter->pdev->irq, dev, NULL);
2180         enable_irq(adapter->pdev->irq);
2181 }
2182 #endif
2183
2184 /* ixgb_main.c */