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