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