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