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