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