1 /******************************************************************************
3 Copyright (c) 2001-2010, Intel Corporation
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
9 1. Redistributions of source code must retain the above copyright notice,
10 this list of conditions and the following disclaimer.
12 2. Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in the
14 documentation and/or other materials provided with the distribution.
16 3. Neither the name of the Intel Corporation nor the names of its
17 contributors may be used to endorse or promote products derived from
18 this software without specific prior written permission.
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 POSSIBILITY OF SUCH DAMAGE.
32 ******************************************************************************/
35 #include "opt_polling.h"
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #if __FreeBSD_version >= 800000
41 #include <sys/buf_ring.h>
44 #include <sys/endian.h>
46 #include <sys/kernel.h>
47 #include <sys/kthread.h>
48 #include <sys/malloc.h>
50 #include <sys/module.h>
52 #include <sys/socket.h>
53 #include <sys/sockio.h>
54 #include <sys/sysctl.h>
55 #include <sys/taskqueue.h>
56 #include <sys/eventhandler.h>
59 #include <sys/ieee1588.h>
63 #include <net/ethernet.h>
65 #include <net/if_arp.h>
66 #include <net/if_dl.h>
67 #include <net/if_media.h>
68 #include <net/ifq_var.h>
70 #include <net/if_types.h>
71 #include <net/vlan/if_vlan_var.h>
72 #include <net/vlan/if_vlan_ether.h>
74 #include <netinet/in_systm.h>
75 #include <netinet/in.h>
76 #include <netinet/if_ether.h>
77 #include <netinet/ip.h>
78 #include <netinet/ip6.h>
79 #include <netinet/tcp.h>
81 #include <netinet/tcp_lro.h>
83 #include <netinet/udp.h>
85 #include <sys/in_cksum.h>
86 #include <bus/pci/pcivar.h>
87 #include <bus/pci/pcireg.h>
89 #include "e1000_api.h"
90 #include "e1000_82575.h"
92 #include "ifcap_defines.h" // XXX
94 /*********************************************************************
95 * Set this to one to display debug statistics
96 *********************************************************************/
97 int igb_display_debug_stats = 0;
99 /*********************************************************************
101 *********************************************************************/
102 char igb_driver_version[] = "version - 1.9.1";
105 /*********************************************************************
106 * PCI Device ID Table
108 * Used by probe to select devices to load on
109 * Last field stores an index into e1000_strings
110 * Last entry must be all 0s
112 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID, String Index }
113 *********************************************************************/
115 static igb_vendor_info_t igb_vendor_info_array[] =
117 { 0x8086, E1000_DEV_ID_82575EB_COPPER, PCI_ANY_ID, PCI_ANY_ID, 0},
118 { 0x8086, E1000_DEV_ID_82575EB_FIBER_SERDES,
119 PCI_ANY_ID, PCI_ANY_ID, 0},
120 { 0x8086, E1000_DEV_ID_82575GB_QUAD_COPPER,
121 PCI_ANY_ID, PCI_ANY_ID, 0},
122 { 0x8086, E1000_DEV_ID_82576, PCI_ANY_ID, PCI_ANY_ID, 0},
123 { 0x8086, E1000_DEV_ID_82576_NS, PCI_ANY_ID, PCI_ANY_ID, 0},
124 { 0x8086, E1000_DEV_ID_82576_NS_SERDES, PCI_ANY_ID, PCI_ANY_ID, 0},
125 { 0x8086, E1000_DEV_ID_82576_FIBER, PCI_ANY_ID, PCI_ANY_ID, 0},
126 { 0x8086, E1000_DEV_ID_82576_SERDES, PCI_ANY_ID, PCI_ANY_ID, 0},
127 { 0x8086, E1000_DEV_ID_82576_SERDES_QUAD,
128 PCI_ANY_ID, PCI_ANY_ID, 0},
129 { 0x8086, E1000_DEV_ID_82576_QUAD_COPPER,
130 PCI_ANY_ID, PCI_ANY_ID, 0},
131 { 0x8086, E1000_DEV_ID_82576_QUAD_COPPER_ET2,
132 PCI_ANY_ID, PCI_ANY_ID, 0},
133 { 0x8086, E1000_DEV_ID_82580_COPPER, PCI_ANY_ID, PCI_ANY_ID, 0},
134 { 0x8086, E1000_DEV_ID_82580_FIBER, PCI_ANY_ID, PCI_ANY_ID, 0},
135 { 0x8086, E1000_DEV_ID_82580_SERDES, PCI_ANY_ID, PCI_ANY_ID, 0},
136 { 0x8086, E1000_DEV_ID_82580_SGMII, PCI_ANY_ID, PCI_ANY_ID, 0},
137 { 0x8086, E1000_DEV_ID_82580_COPPER_DUAL,
138 PCI_ANY_ID, PCI_ANY_ID, 0},
139 /* required last entry */
143 /*********************************************************************
144 * Table of branding strings for all supported NICs.
145 *********************************************************************/
147 static char *igb_strings[] = {
148 "Intel(R) PRO/1000 Network Connection"
151 /*********************************************************************
152 * Function prototypes
153 *********************************************************************/
154 static int igb_probe(device_t);
155 static int igb_attach(device_t);
156 static int igb_detach(device_t);
157 static int igb_shutdown(device_t);
158 static int igb_suspend(device_t);
159 static int igb_resume(device_t);
160 static void igb_start(struct ifnet *);
161 static void igb_start_locked(struct tx_ring *, struct ifnet *ifp);
162 #if __FreeBSD_version >= 800000
163 static int igb_mq_start(struct ifnet *, struct mbuf *);
164 static int igb_mq_start_locked(struct ifnet *,
165 struct tx_ring *, struct mbuf *);
166 static void igb_qflush(struct ifnet *);
168 static int igb_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
169 static void igb_init(void *);
170 static void igb_init_locked(struct adapter *);
171 static void igb_stop(void *);
172 static void igb_media_status(struct ifnet *, struct ifmediareq *);
173 static int igb_media_change(struct ifnet *);
174 static void igb_identify_hardware(struct adapter *);
175 static int igb_allocate_pci_resources(struct adapter *);
176 static int igb_allocate_msix(struct adapter *);
177 static int igb_allocate_legacy(struct adapter *);
178 static int igb_setup_msix(struct adapter *);
179 static void igb_free_pci_resources(struct adapter *);
180 static void igb_local_timer(void *);
181 static void igb_reset(struct adapter *);
182 static void igb_setup_interface(device_t, struct adapter *);
183 static int igb_allocate_queues(struct adapter *);
184 static void igb_configure_queues(struct adapter *);
186 static int igb_allocate_transmit_buffers(struct tx_ring *);
187 static void igb_setup_transmit_structures(struct adapter *);
188 static void igb_setup_transmit_ring(struct tx_ring *);
189 static void igb_initialize_transmit_units(struct adapter *);
190 static void igb_free_transmit_structures(struct adapter *);
191 static void igb_free_transmit_buffers(struct tx_ring *);
193 static int igb_allocate_receive_buffers(struct rx_ring *);
194 static int igb_setup_receive_structures(struct adapter *);
195 static int igb_setup_receive_ring(struct rx_ring *);
196 static void igb_initialize_receive_units(struct adapter *);
197 static void igb_free_receive_structures(struct adapter *);
198 static void igb_free_receive_buffers(struct rx_ring *);
199 static void igb_free_receive_ring(struct rx_ring *);
201 static void igb_enable_intr(struct adapter *);
202 static void igb_disable_intr(struct adapter *);
203 static void igb_update_stats_counters(struct adapter *);
204 static bool igb_txeof(struct tx_ring *);
206 static __inline void igb_rx_discard(struct rx_ring *,
207 union e1000_adv_rx_desc *, int);
208 static __inline void igb_rx_input(struct rx_ring *,
209 struct ifnet *, struct mbuf *, u32);
211 static bool igb_rxeof(struct rx_ring *, int);
212 static void igb_rx_checksum(u32, struct mbuf *, u32);
213 static int igb_tx_ctx_setup(struct tx_ring *, struct mbuf *);
215 static bool igb_tso_setup(struct tx_ring *, struct mbuf *, u32 *);
217 static void igb_set_promisc(struct adapter *);
218 static void igb_disable_promisc(struct adapter *);
219 static void igb_set_multi(struct adapter *);
220 static void igb_print_hw_stats(struct adapter *);
221 static void igb_update_link_status(struct adapter *);
222 static int igb_get_buf(struct rx_ring *, int, u8);
224 static void igb_register_vlan(void *, struct ifnet *, u16);
225 static void igb_unregister_vlan(void *, struct ifnet *, u16);
226 static void igb_setup_vlan_hw_support(struct adapter *);
228 static int igb_xmit(struct tx_ring *, struct mbuf **);
229 static int igb_dma_malloc(struct adapter *, bus_size_t,
230 struct igb_dma_alloc *, int);
231 static void igb_dma_free(struct adapter *, struct igb_dma_alloc *);
232 static void igb_print_debug_info(struct adapter *);
233 static void igb_print_nvm_info(struct adapter *);
234 static int igb_is_valid_ether_addr(u8 *);
235 static int igb_sysctl_stats(SYSCTL_HANDLER_ARGS);
236 static int igb_sysctl_debug_info(SYSCTL_HANDLER_ARGS);
237 /* Management and WOL Support */
238 static void igb_init_manageability(struct adapter *);
239 static void igb_release_manageability(struct adapter *);
240 static void igb_get_hw_control(struct adapter *);
241 static void igb_release_hw_control(struct adapter *);
242 static void igb_enable_wakeup(device_t);
244 static void igb_irq_fast(void *);
245 static void igb_add_rx_process_limit(struct adapter *, const char *,
246 const char *, int *, int);
247 static void igb_handle_rxtx(void *context, int pending);
248 static void igb_handle_que(void *context, int pending);
249 static void igb_handle_link(void *context, int pending);
251 /* These are MSIX only irq handlers */
252 static void igb_msix_que(void *);
253 static void igb_msix_link(void *);
255 #ifdef DEVICE_POLLING
256 static poll_handler_t igb_poll;
259 /*********************************************************************
260 * FreeBSD Device Interface Entry Points
261 *********************************************************************/
263 static device_method_t igb_methods[] = {
264 /* Device interface */
265 DEVMETHOD(device_probe, igb_probe),
266 DEVMETHOD(device_attach, igb_attach),
267 DEVMETHOD(device_detach, igb_detach),
268 DEVMETHOD(device_shutdown, igb_shutdown),
269 DEVMETHOD(device_suspend, igb_suspend),
270 DEVMETHOD(device_resume, igb_resume),
274 static driver_t igb_driver = {
275 "igb", igb_methods, sizeof(struct adapter),
278 static devclass_t igb_devclass;
279 DRIVER_MODULE(igb, pci, igb_driver, igb_devclass, NULL, NULL);
280 MODULE_DEPEND(igb, pci, 1, 1, 1);
281 MODULE_DEPEND(igb, ether, 1, 1, 1);
283 /*********************************************************************
284 * Tunable default values.
285 *********************************************************************/
287 /* Descriptor defaults */
288 static int igb_rxd = IGB_DEFAULT_RXD;
289 static int igb_txd = IGB_DEFAULT_TXD;
290 TUNABLE_INT("hw.igb.rxd", &igb_rxd);
291 TUNABLE_INT("hw.igb.txd", &igb_txd);
294 ** AIM: Adaptive Interrupt Moderation
295 ** which means that the interrupt rate
296 ** is varied over time based on the
297 ** traffic for that interrupt vector
299 static int igb_enable_aim = TRUE;
300 TUNABLE_INT("hw.igb.enable_aim", &igb_enable_aim);
303 * MSIX should be the default for best performance,
304 * but this allows it to be forced off for testing.
306 static int igb_enable_msix = 0;
307 TUNABLE_INT("hw.igb.enable_msix", &igb_enable_msix);
310 * Header split has seemed to be beneficial in
311 * many circumstances tested, however there have
312 * been some stability issues, so the default is
315 static bool igb_header_split = FALSE;
316 TUNABLE_INT("hw.igb.hdr_split", &igb_header_split);
319 ** This will autoconfigure based on
320 ** the number of CPUs if left at 0.
322 static int igb_num_queues = 0;
323 TUNABLE_INT("hw.igb.num_queues", &igb_num_queues);
325 /* How many packets rxeof tries to clean at a time */
326 static int igb_rx_process_limit = 100;
327 TUNABLE_INT("hw.igb.rx_process_limit", &igb_rx_process_limit);
329 /* Flow control setting - default to FULL */
330 static int igb_fc_setting = e1000_fc_full;
331 TUNABLE_INT("hw.igb.fc_setting", &igb_fc_setting);
334 ** Shadow VFTA table, this is needed because
335 ** the real filter table gets cleared during
336 ** a soft reset and the driver needs to be able
339 static u32 igb_shadow_vfta[IGB_VFTA_SIZE];
342 /*********************************************************************
343 * Device identification routine
345 * igb_probe determines if the driver should be loaded on
346 * adapter based on PCI vendor/device id of the adapter.
348 * return BUS_PROBE_DEFAULT on success, positive on failure
349 *********************************************************************/
352 igb_probe(device_t dev)
354 char adapter_name[60];
355 uint16_t pci_vendor_id = 0;
356 uint16_t pci_device_id = 0;
357 uint16_t pci_subvendor_id = 0;
358 uint16_t pci_subdevice_id = 0;
359 igb_vendor_info_t *ent;
361 INIT_DEBUGOUT("igb_probe: begin");
363 pci_vendor_id = pci_get_vendor(dev);
364 if (pci_vendor_id != IGB_VENDOR_ID)
367 pci_device_id = pci_get_device(dev);
368 pci_subvendor_id = pci_get_subvendor(dev);
369 pci_subdevice_id = pci_get_subdevice(dev);
371 ent = igb_vendor_info_array;
372 while (ent->vendor_id != 0) {
373 if ((pci_vendor_id == ent->vendor_id) &&
374 (pci_device_id == ent->device_id) &&
376 ((pci_subvendor_id == ent->subvendor_id) ||
377 (ent->subvendor_id == PCI_ANY_ID)) &&
379 ((pci_subdevice_id == ent->subdevice_id) ||
380 (ent->subdevice_id == PCI_ANY_ID))) {
381 ksprintf(adapter_name, "%s %s",
382 igb_strings[ent->index],
384 device_set_desc_copy(dev, adapter_name);
385 return (BUS_PROBE_DEFAULT);
393 /*********************************************************************
394 * Device initialization routine
396 * The attach entry point is called when the driver is being loaded.
397 * This routine identifies the type of hardware, allocates all resources
398 * and initializes the hardware.
400 * return 0 on success, positive on failure
401 *********************************************************************/
404 igb_attach(device_t dev)
406 struct adapter *adapter;
410 INIT_DEBUGOUT("igb_attach: begin");
412 adapter = device_get_softc(dev);
413 adapter->dev = adapter->osdep.dev = dev;
414 IGB_CORE_LOCK_INIT(adapter, device_get_nameunit(dev));
417 sysctl_ctx_init(&adapter->sysctl_ctx);
418 adapter->sysctl_tree = SYSCTL_ADD_NODE(&adapter->sysctl_ctx,
419 SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
420 device_get_nameunit(adapter->dev),
422 if (adapter->sysctl_tree == NULL) {
423 device_printf(adapter->dev, "can't add sysctl node\n");
428 SYSCTL_ADD_PROC(&adapter->sysctl_ctx,
429 SYSCTL_CHILDREN(adapter->sysctl_tree),
430 OID_AUTO, "debug", CTLTYPE_INT|CTLFLAG_RW, adapter, 0,
431 igb_sysctl_debug_info, "I", "Debug Information");
433 SYSCTL_ADD_PROC(&adapter->sysctl_ctx,
434 SYSCTL_CHILDREN(adapter->sysctl_tree),
435 OID_AUTO, "stats", CTLTYPE_INT|CTLFLAG_RW, adapter, 0,
436 igb_sysctl_stats, "I", "Statistics");
438 SYSCTL_ADD_INT(&adapter->sysctl_ctx,
439 SYSCTL_CHILDREN(adapter->sysctl_tree),
440 OID_AUTO, "flow_control", CTLTYPE_INT|CTLFLAG_RW,
441 &igb_fc_setting, 0, "Flow Control");
443 SYSCTL_ADD_INT(&adapter->sysctl_ctx,
444 SYSCTL_CHILDREN(adapter->sysctl_tree),
445 OID_AUTO, "enable_aim", CTLTYPE_INT|CTLFLAG_RW,
446 &igb_enable_aim, 1, "Interrupt Moderation");
448 callout_init_mp(&adapter->timer);
450 /* Determine hardware and mac info */
451 igb_identify_hardware(adapter);
453 /* Setup PCI resources */
454 if (igb_allocate_pci_resources(adapter)) {
455 device_printf(dev, "Allocation of PCI resources failed\n");
460 /* Do Shared Code initialization */
461 if (e1000_setup_init_funcs(&adapter->hw, TRUE)) {
462 device_printf(dev, "Setup of Shared code failed\n");
467 e1000_get_bus_info(&adapter->hw);
469 /* Sysctls for limiting the amount of work done in the taskqueue */
470 igb_add_rx_process_limit(adapter, "rx_processing_limit",
471 "max number of rx packets to process", &adapter->rx_process_limit,
472 igb_rx_process_limit);
475 * Validate number of transmit and receive descriptors. It
476 * must not exceed hardware maximum, and must be multiple
477 * of E1000_DBA_ALIGN.
479 if (((igb_txd * sizeof(struct e1000_tx_desc)) % IGB_DBA_ALIGN) != 0 ||
480 (igb_txd > IGB_MAX_TXD) || (igb_txd < IGB_MIN_TXD)) {
481 device_printf(dev, "Using %d TX descriptors instead of %d!\n",
482 IGB_DEFAULT_TXD, igb_txd);
483 adapter->num_tx_desc = IGB_DEFAULT_TXD;
485 adapter->num_tx_desc = igb_txd;
486 if (((igb_rxd * sizeof(struct e1000_rx_desc)) % IGB_DBA_ALIGN) != 0 ||
487 (igb_rxd > IGB_MAX_RXD) || (igb_rxd < IGB_MIN_RXD)) {
488 device_printf(dev, "Using %d RX descriptors instead of %d!\n",
489 IGB_DEFAULT_RXD, igb_rxd);
490 adapter->num_rx_desc = IGB_DEFAULT_RXD;
492 adapter->num_rx_desc = igb_rxd;
494 adapter->hw.mac.autoneg = DO_AUTO_NEG;
495 adapter->hw.phy.autoneg_wait_to_complete = FALSE;
496 adapter->hw.phy.autoneg_advertised = AUTONEG_ADV_DEFAULT;
499 if (adapter->hw.phy.media_type == e1000_media_type_copper) {
500 adapter->hw.phy.mdix = AUTO_ALL_MODES;
501 adapter->hw.phy.disable_polarity_correction = FALSE;
502 adapter->hw.phy.ms_type = IGB_MASTER_SLAVE;
506 * Set the frame limits assuming
507 * standard ethernet sized frames.
509 adapter->max_frame_size = ETHERMTU + ETHER_HDR_LEN + ETHERNET_FCS_SIZE;
510 adapter->min_frame_size = ETH_ZLEN + ETHERNET_FCS_SIZE;
513 ** Allocate and Setup Queues
515 if (igb_allocate_queues(adapter)) {
521 ** Start from a known state, this is
522 ** important in reading the nvm and
525 e1000_reset_hw(&adapter->hw);
527 /* Make sure we have a good EEPROM before we read from it */
528 if (e1000_validate_nvm_checksum(&adapter->hw) < 0) {
530 ** Some PCI-E parts fail the first check due to
531 ** the link being in sleep state, call it again,
532 ** if it fails a second time its a real issue.
534 if (e1000_validate_nvm_checksum(&adapter->hw) < 0) {
536 "The EEPROM Checksum Is Not Valid\n");
543 ** Copy the permanent MAC address out of the EEPROM
545 if (e1000_read_mac_addr(&adapter->hw) < 0) {
546 device_printf(dev, "EEPROM read error while reading MAC"
551 /* Check its sanity */
552 if (!igb_is_valid_ether_addr(adapter->hw.mac.addr)) {
553 device_printf(dev, "Invalid MAC address\n");
559 ** Configure Interrupts
561 if ((adapter->msix > 1) && (igb_enable_msix))
562 error = igb_allocate_msix(adapter);
563 else /* MSI or Legacy */
564 error = igb_allocate_legacy(adapter);
568 /* Setup OS specific network interface */
569 igb_setup_interface(dev, adapter);
571 /* Now get a good starting state */
574 /* Initialize statistics */
575 igb_update_stats_counters(adapter);
577 adapter->hw.mac.get_link_status = 1;
578 igb_update_link_status(adapter);
580 /* Indicate SOL/IDER usage */
581 if (e1000_check_reset_block(&adapter->hw))
583 "PHY reset is blocked due to SOL/IDER session.\n");
585 /* Determine if we have to control management hardware */
586 adapter->has_manage = e1000_enable_mng_pass_thru(&adapter->hw);
591 /* APME bit in EEPROM is mapped to WUC.APME */
592 eeprom_data = E1000_READ_REG(&adapter->hw, E1000_WUC) & E1000_WUC_APME;
594 adapter->wol = E1000_WUFC_MAG;
596 /* Register for VLAN events */
597 adapter->vlan_attach = EVENTHANDLER_REGISTER(vlan_config,
598 igb_register_vlan, adapter, EVENTHANDLER_PRI_FIRST);
599 adapter->vlan_detach = EVENTHANDLER_REGISTER(vlan_unconfig,
600 igb_unregister_vlan, adapter, EVENTHANDLER_PRI_FIRST);
602 /* Tell the stack that the interface is not active */
603 adapter->ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
605 INIT_DEBUGOUT("igb_attach: end");
610 igb_free_transmit_structures(adapter);
611 igb_free_receive_structures(adapter);
612 igb_release_hw_control(adapter);
614 igb_free_pci_resources(adapter);
616 sysctl_ctx_free(&adapter->sysctl_ctx);
617 IGB_CORE_LOCK_DESTROY(adapter);
622 /*********************************************************************
623 * Device removal routine
625 * The detach entry point is called when the driver is being removed.
626 * This routine stops the adapter and deallocates all the resources
627 * that were allocated for driver operation.
629 * return 0 on success, positive on failure
630 *********************************************************************/
633 igb_detach(device_t dev)
635 struct adapter *adapter = device_get_softc(dev);
637 INIT_DEBUGOUT("igb_detach: begin");
639 /* Make sure VLANS are not using driver */
640 if (adapter->ifp->if_vlantrunks != NULL) {
641 device_printf(dev,"Vlan in use, detach first\n");
645 IGB_CORE_LOCK(adapter);
646 adapter->in_detach = 1;
648 IGB_CORE_UNLOCK(adapter);
650 e1000_phy_hw_reset(&adapter->hw);
652 /* Give control back to firmware */
653 igb_release_manageability(adapter);
654 igb_release_hw_control(adapter);
657 E1000_WRITE_REG(&adapter->hw, E1000_WUC, E1000_WUC_PME_EN);
658 E1000_WRITE_REG(&adapter->hw, E1000_WUFC, adapter->wol);
659 igb_enable_wakeup(dev);
662 /* Unregister VLAN events */
663 if (adapter->vlan_attach != NULL)
664 EVENTHANDLER_DEREGISTER(vlan_config, adapter->vlan_attach);
665 if (adapter->vlan_detach != NULL)
666 EVENTHANDLER_DEREGISTER(vlan_unconfig, adapter->vlan_detach);
668 ether_ifdetach(adapter->ifp);
670 //callout_drain(&adapter->timer);
671 callout_stop(&adapter->timer);
673 igb_free_pci_resources(adapter);
674 bus_generic_detach(dev);
676 igb_free_transmit_structures(adapter);
677 igb_free_receive_structures(adapter);
679 sysctl_ctx_free(&adapter->sysctl_ctx);
680 IGB_CORE_LOCK_DESTROY(adapter);
685 /*********************************************************************
687 * Shutdown entry point
689 **********************************************************************/
692 igb_shutdown(device_t dev)
694 return igb_suspend(dev);
698 * Suspend/resume device methods.
701 igb_suspend(device_t dev)
703 struct adapter *adapter = device_get_softc(dev);
705 IGB_CORE_LOCK(adapter);
709 igb_release_manageability(adapter);
710 igb_release_hw_control(adapter);
713 E1000_WRITE_REG(&adapter->hw, E1000_WUC, E1000_WUC_PME_EN);
714 E1000_WRITE_REG(&adapter->hw, E1000_WUFC, adapter->wol);
715 igb_enable_wakeup(dev);
718 IGB_CORE_UNLOCK(adapter);
720 return bus_generic_suspend(dev);
724 igb_resume(device_t dev)
726 struct adapter *adapter = device_get_softc(dev);
727 struct ifnet *ifp = adapter->ifp;
729 IGB_CORE_LOCK(adapter);
730 igb_init_locked(adapter);
731 igb_init_manageability(adapter);
733 if ((ifp->if_flags & IFF_UP) &&
734 (ifp->if_flags & IFF_RUNNING))
737 IGB_CORE_UNLOCK(adapter);
739 return bus_generic_resume(dev);
743 /*********************************************************************
744 * Transmit entry point
746 * igb_start is called by the stack to initiate a transmit.
747 * The driver will remain in this routine as long as there are
748 * packets to transmit and transmit resources are available.
749 * In case resources are not available stack is notified and
750 * the packet is requeued.
751 **********************************************************************/
754 igb_start_locked(struct tx_ring *txr, struct ifnet *ifp)
756 struct adapter *adapter = ifp->if_softc;
759 IGB_TX_LOCK_ASSERT(txr);
761 if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
765 * Must purge on abort from this point on or the netif will call
766 * us endlessly. Either that or set IFF_OACTIVE.
768 if (!adapter->link_active) {
769 ifq_purge(&ifp->if_snd);
773 while (!ifq_is_empty(&ifp->if_snd)) {
775 m_head = ifq_dequeue(&ifp->if_snd, NULL);
779 * Encapsulation can modify our pointer, and or make it
780 * NULL on failure. In that event, we can't requeue.
782 if (igb_xmit(txr, &m_head)) {
785 ifp->if_flags |= IFF_OACTIVE;
786 ifq_prepend(&ifp->if_snd, m_head);
790 /* Send a copy of the frame to the BPF listener */
791 ETHER_BPF_MTAP(ifp, m_head);
793 /* Set watchdog on */
794 txr->watchdog_check = TRUE;
799 * Legacy TX driver routine, called from the
800 * stack, always uses tx[0], and spins for it.
801 * Should not be used with multiqueue tx
804 igb_start(struct ifnet *ifp)
806 struct adapter *adapter = ifp->if_softc;
807 struct tx_ring *txr = adapter->tx_rings;
809 if (ifp->if_flags & IFF_RUNNING) {
811 igb_start_locked(txr, ifp);
817 #if __FreeBSD_version >= 800000
819 ** Multiqueue Transmit driver
823 igb_mq_start(struct ifnet *ifp, struct mbuf *m)
825 struct adapter *adapter = ifp->if_softc;
829 /* Which queue to use */
830 if ((m->m_flags & M_FLOWID) != 0)
831 i = m->m_pkthdr.flowid % adapter->num_queues;
832 txr = &adapter->tx_rings[i];
834 if (IGB_TX_TRYLOCK(txr)) {
835 err = igb_mq_start_locked(ifp, txr, m);
838 err = drbr_enqueue(ifp, txr->br, m);
844 igb_mq_start_locked(struct ifnet *ifp, struct tx_ring *txr, struct mbuf *m)
846 struct adapter *adapter = txr->adapter;
850 IGB_TX_LOCK_ASSERT(txr);
852 if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) !=
853 IFF_RUNNING || adapter->link_active == 0) {
855 err = drbr_enqueue(ifp, txr->br, m);
861 next = drbr_dequeue(ifp, txr->br);
862 } else if (drbr_needs_enqueue(ifp, txr->br)) {
863 if ((err = drbr_enqueue(ifp, txr->br, m)) != 0)
865 next = drbr_dequeue(ifp, txr->br);
868 /* Process the queue */
869 while (next != NULL) {
870 if ((err = igb_xmit(txr, &next)) != 0) {
872 err = drbr_enqueue(ifp, txr->br, next);
876 drbr_stats_update(ifp, next->m_pkthdr.len, next->m_flags);
877 ETHER_BPF_MTAP(ifp, next);
878 if ((ifp->if_flags & IFF_RUNNING) == 0)
880 if (txr->tx_avail <= IGB_TX_OP_THRESHOLD) {
881 ifp->if_flags |= IFF_OACTIVE;
884 next = drbr_dequeue(ifp, txr->br);
887 /* Set the watchdog */
888 txr->watchdog_check = TRUE;
894 ** Flush all ring buffers
897 igb_qflush(struct ifnet *ifp)
899 struct adapter *adapter = ifp->if_softc;
900 struct tx_ring *txr = adapter->tx_rings;
903 for (int i = 0; i < adapter->num_queues; i++, txr++) {
905 while ((m = buf_ring_dequeue_sc(txr->br)) != NULL)
911 #endif /* __FreeBSD_version >= 800000 */
913 /*********************************************************************
916 * igb_ioctl is called when the user wants to configure the
919 * return 0 on success, positive on failure
920 **********************************************************************/
923 igb_ioctl(struct ifnet *ifp, u_long command, caddr_t data, struct ucred *cred)
925 struct adapter *adapter = ifp->if_softc;
926 struct ifreq *ifr = (struct ifreq *)data;
928 struct ifaddr *ifa = (struct ifaddr *)data;
932 if (adapter->in_detach)
938 if (ifa->ifa_addr->sa_family == AF_INET) {
941 * Since resetting hardware takes a very long time
942 * and results in link renegotiation we only
943 * initialize the hardware only when it is absolutely
946 ifp->if_flags |= IFF_UP;
947 if (!(ifp->if_flags & IFF_RUNNING)) {
948 IGB_CORE_LOCK(adapter);
949 igb_init_locked(adapter);
950 IGB_CORE_UNLOCK(adapter);
952 if (!(ifp->if_flags & IFF_NOARP))
953 arp_ifinit(ifp, ifa);
956 error = ether_ioctl(ifp, command, data);
962 IOCTL_DEBUGOUT("ioctl rcv'd: SIOCSIFMTU (Set Interface MTU)");
964 IGB_CORE_LOCK(adapter);
965 max_frame_size = 9234;
966 if (ifr->ifr_mtu > max_frame_size - ETHER_HDR_LEN -
968 IGB_CORE_UNLOCK(adapter);
973 ifp->if_mtu = ifr->ifr_mtu;
974 adapter->max_frame_size =
975 ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN;
976 igb_init_locked(adapter);
977 IGB_CORE_UNLOCK(adapter);
981 IOCTL_DEBUGOUT("ioctl rcv'd:\
982 SIOCSIFFLAGS (Set Interface Flags)");
983 IGB_CORE_LOCK(adapter);
984 if (ifp->if_flags & IFF_UP) {
985 if ((ifp->if_flags & IFF_RUNNING)) {
986 if ((ifp->if_flags ^ adapter->if_flags) &
987 (IFF_PROMISC | IFF_ALLMULTI)) {
988 igb_disable_promisc(adapter);
989 igb_set_promisc(adapter);
992 igb_init_locked(adapter);
994 if (ifp->if_flags & IFF_RUNNING)
996 adapter->if_flags = ifp->if_flags;
997 IGB_CORE_UNLOCK(adapter);
1001 IOCTL_DEBUGOUT("ioctl rcv'd: SIOC(ADD|DEL)MULTI");
1002 if (ifp->if_flags & IFF_RUNNING) {
1003 IGB_CORE_LOCK(adapter);
1004 igb_disable_intr(adapter);
1005 igb_set_multi(adapter);
1006 #ifdef DEVICE_POLLING
1007 if ((ifp->if_flags & IFF_POLLING) == 0)
1009 igb_enable_intr(adapter);
1010 IGB_CORE_UNLOCK(adapter);
1014 /* Check SOL/IDER usage */
1015 IGB_CORE_LOCK(adapter);
1016 if (e1000_check_reset_block(&adapter->hw)) {
1017 IGB_CORE_UNLOCK(adapter);
1018 device_printf(adapter->dev, "Media change is"
1019 " blocked due to SOL/IDER session.\n");
1022 IGB_CORE_UNLOCK(adapter);
1024 IOCTL_DEBUGOUT("ioctl rcv'd: \
1025 SIOCxIFMEDIA (Get/Set Interface Media)");
1026 error = ifmedia_ioctl(ifp, ifr, &adapter->media, command);
1032 IOCTL_DEBUGOUT("ioctl rcv'd: SIOCSIFCAP (Set Capabilities)");
1034 mask = ifr->ifr_reqcap ^ ifp->if_capenable;
1035 #ifdef DEVICE_POLLING
1036 if (ifp->if_flags & IFF_POLLING) {
1037 IGB_CORE_LOCK(adapter);
1038 igb_disable_intr(adapter);
1039 IGB_CORE_UNLOCK(adapter);
1042 if (mask & IFCAP_HWCSUM) {
1043 ifp->if_capenable ^= IFCAP_HWCSUM;
1047 if (mask & IFCAP_TSO4) {
1048 ifp->if_capenable ^= IFCAP_TSO4;
1052 if (mask & IFCAP_VLAN_HWTAGGING) {
1053 ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
1057 if (mask & IFCAP_LRO) {
1058 ifp->if_capenable ^= IFCAP_LRO;
1062 if (reinit && (ifp->if_flags & IFF_RUNNING))
1065 VLAN_CAPABILITIES(ifp);
1071 error = ether_ioctl(ifp, command, data);
1074 IOCTL_DEBUGOUT("ioctl done");
1080 /*********************************************************************
1083 * This routine is used in two ways. It is used by the stack as
1084 * init entry point in network interface structure. It is also used
1085 * by the driver as a hw/sw initialization routine to get to a
1088 * return 0 on success, positive on failure
1089 **********************************************************************/
1092 igb_init_locked(struct adapter *adapter)
1094 struct ifnet *ifp = adapter->ifp;
1095 device_t dev = adapter->dev;
1097 INIT_DEBUGOUT("igb_init: begin");
1099 IGB_CORE_LOCK_ASSERT(adapter);
1101 igb_disable_intr(adapter);
1102 callout_stop(&adapter->timer);
1104 /* Get the latest mac address, User can use a LAA */
1105 bcopy(IF_LLADDR(adapter->ifp), adapter->hw.mac.addr,
1108 /* Put the address into the Receive Address Array */
1109 e1000_rar_set(&adapter->hw, adapter->hw.mac.addr, 0);
1112 igb_update_link_status(adapter);
1114 E1000_WRITE_REG(&adapter->hw, E1000_VET, ETHERTYPE_VLAN);
1116 /* Set hardware offload abilities */
1117 ifp->if_hwassist = 0;
1118 if (ifp->if_capenable & IFCAP_TXCSUM) {
1119 ifp->if_hwassist |= (CSUM_TCP | CSUM_UDP);
1120 #if __FreeBSD_version >= 800000
1121 if (adapter->hw.mac.type == e1000_82576)
1122 ifp->if_hwassist |= CSUM_SCTP;
1127 if (ifp->if_capenable & IFCAP_TSO4)
1128 ifp->if_hwassist |= CSUM_TSO;
1131 /* Configure for OS presence */
1132 igb_init_manageability(adapter);
1134 /* Prepare transmit descriptors and buffers */
1135 igb_setup_transmit_structures(adapter);
1136 igb_initialize_transmit_units(adapter);
1138 /* Setup Multicast table */
1139 igb_set_multi(adapter);
1142 ** Figure out the desired mbuf pool
1143 ** for doing jumbo/packetsplit
1145 if (ifp->if_mtu > ETHERMTU)
1146 adapter->rx_mbuf_sz = MJUMPAGESIZE;
1148 adapter->rx_mbuf_sz = MCLBYTES;
1150 /* Prepare receive descriptors and buffers */
1151 if (igb_setup_receive_structures(adapter)) {
1152 device_printf(dev, "Could not setup receive structures\n");
1155 igb_initialize_receive_units(adapter);
1157 /* Don't lose promiscuous settings */
1158 igb_set_promisc(adapter);
1160 ifp->if_flags |= IFF_RUNNING;
1161 ifp->if_flags &= ~IFF_OACTIVE;
1163 callout_reset(&adapter->timer, hz, igb_local_timer, adapter);
1164 e1000_clear_hw_cntrs_base_generic(&adapter->hw);
1166 if (adapter->msix > 1) /* Set up queue routing */
1167 igb_configure_queues(adapter);
1169 /* Set up VLAN tag offload and filter */
1170 igb_setup_vlan_hw_support(adapter);
1172 /* this clears any pending interrupts */
1173 E1000_READ_REG(&adapter->hw, E1000_ICR);
1174 #ifdef DEVICE_POLLING
1176 * Only enable interrupts if we are not polling, make sure
1177 * they are off otherwise.
1179 if (ifp->if_flags & IFF_POLLING)
1180 igb_disable_intr(adapter);
1182 #endif /* DEVICE_POLLING */
1184 igb_enable_intr(adapter);
1185 E1000_WRITE_REG(&adapter->hw, E1000_ICS, E1000_ICS_LSC);
1188 /* Don't reset the phy next time init gets called */
1189 adapter->hw.phy.reset_disable = TRUE;
1190 INIT_DEBUGOUT("igb_init: end");
1196 struct adapter *adapter = arg;
1198 IGB_CORE_LOCK(adapter);
1199 igb_init_locked(adapter);
1200 IGB_CORE_UNLOCK(adapter);
1205 igb_handle_rxtx(void *context, int pending)
1207 struct adapter *adapter = context;
1208 struct tx_ring *txr = adapter->tx_rings;
1209 struct rx_ring *rxr = adapter->rx_rings;
1214 if (ifp->if_flags & IFF_RUNNING) {
1215 if (igb_rxeof(rxr, adapter->rx_process_limit))
1216 taskqueue_enqueue(adapter->tq, &adapter->rxtx_task);
1220 #if __FreeBSD_version >= 800000
1221 if (!drbr_empty(ifp, txr->br))
1222 igb_mq_start_locked(ifp, txr, NULL);
1224 if (!ifq_is_empty(&ifp->if_snd))
1225 igb_start_locked(txr, ifp);
1230 igb_enable_intr(adapter);
1234 igb_handle_que(void *context, int pending)
1236 struct igb_queue *que = context;
1237 struct adapter *adapter = que->adapter;
1238 struct tx_ring *txr = que->txr;
1239 struct rx_ring *rxr = que->rxr;
1240 struct ifnet *ifp = adapter->ifp;
1241 u32 loop = IGB_MAX_LOOP;
1246 more = igb_rxeof(rxr, -1);
1247 } while (loop-- && more);
1249 if (IGB_TX_TRYLOCK(txr)) {
1250 loop = IGB_MAX_LOOP;
1252 more = igb_txeof(txr);
1253 } while (loop-- && more);
1254 #if __FreeBSD_version >= 800000
1255 igb_mq_start_locked(ifp, txr, NULL);
1257 if (!ifq_is_empty(&ifp->if_snd))
1258 igb_start_locked(txr, ifp);
1263 /* Reenable this interrupt */
1264 #ifdef DEVICE_POLLING
1265 if ((ifp->if_flags & IFF_POLLING) == 0)
1267 E1000_WRITE_REG(&adapter->hw, E1000_EIMS, que->eims);
1270 /* Deal with link in a sleepable context */
1272 igb_handle_link(void *context, int pending)
1274 struct adapter *adapter = context;
1276 adapter->hw.mac.get_link_status = 1;
1277 igb_update_link_status(adapter);
1280 /*********************************************************************
1282 * MSI/Legacy Deferred
1283 * Interrupt Service routine
1285 *********************************************************************/
1286 #define FILTER_STRAY
1287 #define FILTER_HANDLED
1289 igb_irq_fast(void *arg)
1291 struct adapter *adapter = arg;
1295 reg_icr = E1000_READ_REG(&adapter->hw, E1000_ICR);
1298 if (reg_icr == 0xffffffff)
1299 return FILTER_STRAY;
1301 /* Definitely not our interrupt. */
1303 return FILTER_STRAY;
1305 if ((reg_icr & E1000_ICR_INT_ASSERTED) == 0)
1306 return FILTER_STRAY;
1309 * Mask interrupts until the taskqueue is finished running. This is
1310 * cheap, just assume that it is needed. This also works around the
1311 * MSI message reordering errata on certain systems.
1313 igb_disable_intr(adapter);
1314 taskqueue_enqueue(adapter->tq, &adapter->rxtx_task);
1316 /* Link status change */
1317 if (reg_icr & (E1000_ICR_RXSEQ | E1000_ICR_LSC))
1318 taskqueue_enqueue(adapter->tq, &adapter->link_task);
1320 if (reg_icr & E1000_ICR_RXO)
1321 adapter->rx_overruns++;
1322 return FILTER_HANDLED;
1325 #ifdef DEVICE_POLLING
1326 /*********************************************************************
1328 * Legacy polling routine
1330 *********************************************************************/
1331 #if __FreeBSD_version >= 800000
1332 #define POLL_RETURN_COUNT(a) (a)
1335 #define POLL_RETURN_COUNT(a)
1338 igb_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
1340 struct adapter *adapter = ifp->if_softc;
1341 struct rx_ring *rxr = adapter->rx_rings;
1342 struct tx_ring *txr = adapter->tx_rings;
1343 u32 reg_icr, rx_done = 0;
1344 u32 loop = IGB_MAX_LOOP;
1347 IGB_CORE_LOCK(adapter);
1348 if ((ifp->if_flags & IFF_RUNNING) == 0) {
1349 IGB_CORE_UNLOCK(adapter);
1350 return POLL_RETURN_COUNT(rx_done);
1353 if (cmd == POLL_AND_CHECK_STATUS) {
1354 reg_icr = E1000_READ_REG(&adapter->hw, E1000_ICR);
1355 /* Link status change */
1356 if (reg_icr & (E1000_ICR_RXSEQ | E1000_ICR_LSC))
1357 taskqueue_enqueue(adapter->tq, &adapter->link_task);
1359 if (reg_icr & E1000_ICR_RXO)
1360 adapter->rx_overruns++;
1362 IGB_CORE_UNLOCK(adapter);
1364 /* TODO: rx_count */
1365 rx_done = igb_rxeof(rxr, count) ? 1 : 0;
1369 more = igb_txeof(txr);
1370 } while (loop-- && more);
1371 #if __FreeBSD_version >= 800000
1372 if (!drbr_empty(ifp, txr->br))
1373 igb_mq_start_locked(ifp, txr, NULL);
1375 if (!ifq_is_empty(&ifp->if_snd))
1376 igb_start_locked(txr, ifp);
1379 return POLL_RETURN_COUNT(rx_done);
1381 #endif /* DEVICE_POLLING */
1383 /*********************************************************************
1385 * MSIX TX Interrupt Service routine
1387 **********************************************************************/
1389 igb_msix_que(void *arg)
1391 struct igb_queue *que = arg;
1392 struct adapter *adapter = que->adapter;
1393 struct tx_ring *txr = que->txr;
1394 struct rx_ring *rxr = que->rxr;
1396 bool more_tx, more_rx;
1398 E1000_WRITE_REG(&adapter->hw, E1000_EIMC, que->eims);
1402 more_tx = igb_txeof(txr);
1405 more_rx = igb_rxeof(rxr, adapter->rx_process_limit);
1407 if (igb_enable_aim == FALSE)
1410 ** Do Adaptive Interrupt Moderation:
1411 ** - Write out last calculated setting
1412 ** - Calculate based on average size over
1413 ** the last interval.
1415 if (que->eitr_setting)
1416 E1000_WRITE_REG(&adapter->hw,
1417 E1000_EITR(que->msix), que->eitr_setting);
1419 que->eitr_setting = 0;
1421 /* Idle, do nothing */
1422 if ((txr->bytes == 0) && (rxr->bytes == 0))
1425 /* Used half Default if sub-gig */
1426 if (adapter->link_speed != 1000)
1427 newitr = IGB_DEFAULT_ITR / 2;
1429 if ((txr->bytes) && (txr->packets))
1430 newitr = txr->bytes/txr->packets;
1431 if ((rxr->bytes) && (rxr->packets))
1432 newitr = max(newitr,
1433 (rxr->bytes / rxr->packets));
1434 newitr += 24; /* account for hardware frame, crc */
1435 /* set an upper boundary */
1436 newitr = min(newitr, 3000);
1437 /* Be nice to the mid range */
1438 if ((newitr > 300) && (newitr < 1200))
1439 newitr = (newitr / 3);
1441 newitr = (newitr / 2);
1443 newitr &= 0x7FFC; /* Mask invalid bits */
1444 if (adapter->hw.mac.type == e1000_82575)
1445 newitr |= newitr << 16;
1447 newitr |= 0x8000000;
1449 /* save for next interrupt */
1450 que->eitr_setting = newitr;
1459 /* Schedule a clean task if needed*/
1460 if (more_tx || more_rx)
1461 taskqueue_enqueue(que->tq, &que->que_task);
1463 /* Reenable this interrupt */
1464 E1000_WRITE_REG(&adapter->hw, E1000_EIMS, que->eims);
1469 /*********************************************************************
1471 * MSIX Link Interrupt Service routine
1473 **********************************************************************/
1476 igb_msix_link(void *arg)
1478 struct adapter *adapter = arg;
1481 ++adapter->link_irq;
1482 icr = E1000_READ_REG(&adapter->hw, E1000_ICR);
1483 if (!(icr & E1000_ICR_LSC))
1485 taskqueue_enqueue(adapter->tq, &adapter->link_task);
1489 E1000_WRITE_REG(&adapter->hw, E1000_IMS, E1000_IMS_LSC);
1490 E1000_WRITE_REG(&adapter->hw, E1000_EIMS, adapter->link_mask);
1495 /*********************************************************************
1497 * Media Ioctl callback
1499 * This routine is called whenever the user queries the status of
1500 * the interface using ifconfig.
1502 **********************************************************************/
1504 igb_media_status(struct ifnet *ifp, struct ifmediareq *ifmr)
1506 struct adapter *adapter = ifp->if_softc;
1507 u_char fiber_type = IFM_1000_SX;
1509 INIT_DEBUGOUT("igb_media_status: begin");
1511 IGB_CORE_LOCK(adapter);
1512 igb_update_link_status(adapter);
1514 ifmr->ifm_status = IFM_AVALID;
1515 ifmr->ifm_active = IFM_ETHER;
1517 if (!adapter->link_active) {
1518 IGB_CORE_UNLOCK(adapter);
1522 ifmr->ifm_status |= IFM_ACTIVE;
1524 if ((adapter->hw.phy.media_type == e1000_media_type_fiber) ||
1525 (adapter->hw.phy.media_type == e1000_media_type_internal_serdes))
1526 ifmr->ifm_active |= fiber_type | IFM_FDX;
1528 switch (adapter->link_speed) {
1530 ifmr->ifm_active |= IFM_10_T;
1533 ifmr->ifm_active |= IFM_100_TX;
1536 ifmr->ifm_active |= IFM_1000_T;
1539 if (adapter->link_duplex == FULL_DUPLEX)
1540 ifmr->ifm_active |= IFM_FDX;
1542 ifmr->ifm_active |= IFM_HDX;
1544 IGB_CORE_UNLOCK(adapter);
1547 /*********************************************************************
1549 * Media Ioctl callback
1551 * This routine is called when the user changes speed/duplex using
1552 * media/mediopt option with ifconfig.
1554 **********************************************************************/
1556 igb_media_change(struct ifnet *ifp)
1558 struct adapter *adapter = ifp->if_softc;
1559 struct ifmedia *ifm = &adapter->media;
1561 INIT_DEBUGOUT("igb_media_change: begin");
1563 if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
1566 IGB_CORE_LOCK(adapter);
1567 switch (IFM_SUBTYPE(ifm->ifm_media)) {
1569 adapter->hw.mac.autoneg = DO_AUTO_NEG;
1570 adapter->hw.phy.autoneg_advertised = AUTONEG_ADV_DEFAULT;
1575 adapter->hw.mac.autoneg = DO_AUTO_NEG;
1576 adapter->hw.phy.autoneg_advertised = ADVERTISE_1000_FULL;
1579 adapter->hw.mac.autoneg = FALSE;
1580 adapter->hw.phy.autoneg_advertised = 0;
1581 if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX)
1582 adapter->hw.mac.forced_speed_duplex = ADVERTISE_100_FULL;
1584 adapter->hw.mac.forced_speed_duplex = ADVERTISE_100_HALF;
1587 adapter->hw.mac.autoneg = FALSE;
1588 adapter->hw.phy.autoneg_advertised = 0;
1589 if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX)
1590 adapter->hw.mac.forced_speed_duplex = ADVERTISE_10_FULL;
1592 adapter->hw.mac.forced_speed_duplex = ADVERTISE_10_HALF;
1595 device_printf(adapter->dev, "Unsupported media type\n");
1598 /* As the speed/duplex settings my have changed we need to
1601 adapter->hw.phy.reset_disable = FALSE;
1603 igb_init_locked(adapter);
1604 IGB_CORE_UNLOCK(adapter);
1610 /*********************************************************************
1612 * This routine maps the mbufs to Advanced TX descriptors.
1613 * used by the 82575 adapter.
1615 **********************************************************************/
1618 igb_xmit(struct tx_ring *txr, struct mbuf **m_headp)
1620 struct adapter *adapter = txr->adapter;
1621 bus_dma_segment_t segs[IGB_MAX_SCATTER];
1623 struct igb_tx_buffer *tx_buffer, *tx_buffer_mapped;
1624 union e1000_adv_tx_desc *txd = NULL;
1625 struct mbuf *m_head;
1626 u32 olinfo_status = 0, cmd_type_len = 0;
1627 int nsegs, i, j, error, first, last = 0;
1633 /* Set basic descriptor constants */
1634 cmd_type_len |= E1000_ADVTXD_DTYP_DATA;
1635 cmd_type_len |= E1000_ADVTXD_DCMD_IFCS | E1000_ADVTXD_DCMD_DEXT;
1636 if (m_head->m_flags & M_VLANTAG)
1637 cmd_type_len |= E1000_ADVTXD_DCMD_VLE;
1640 * Force a cleanup if number of TX descriptors
1641 * available hits the threshold
1643 if (txr->tx_avail <= IGB_TX_CLEANUP_THRESHOLD) {
1645 /* Now do we at least have a minimal? */
1646 if (txr->tx_avail <= IGB_TX_OP_THRESHOLD) {
1647 txr->no_desc_avail++;
1653 * Map the packet for DMA.
1655 * Capture the first descriptor index,
1656 * this descriptor will have the index
1657 * of the EOP which is the only one that
1658 * now gets a DONE bit writeback.
1660 first = txr->next_avail_desc;
1661 tx_buffer = &txr->tx_buffers[first];
1662 tx_buffer_mapped = tx_buffer;
1663 map = tx_buffer->map;
1665 error = bus_dmamap_load_mbuf_segment(txr->txtag, map,
1666 *m_headp, segs, IGB_MAX_SCATTER, &nsegs, BUS_DMA_NOWAIT);
1668 if (error == EFBIG) {
1671 m = m_defrag(*m_headp, MB_DONTWAIT);
1673 adapter->mbuf_defrag_failed++;
1681 error = bus_dmamap_load_mbuf_segment(txr->txtag, map,
1682 *m_headp, segs, IGB_MAX_SCATTER, &nsegs, BUS_DMA_NOWAIT);
1684 if (error == ENOMEM) {
1685 adapter->no_tx_dma_setup++;
1687 } else if (error != 0) {
1688 adapter->no_tx_dma_setup++;
1693 } else if (error == ENOMEM) {
1694 adapter->no_tx_dma_setup++;
1696 } else if (error != 0) {
1697 adapter->no_tx_dma_setup++;
1703 /* Check again to be sure we have enough descriptors */
1704 if (nsegs > (txr->tx_avail - 2)) {
1705 txr->no_desc_avail++;
1706 bus_dmamap_unload(txr->txtag, map);
1712 * Set up the context descriptor:
1713 * used when any hardware offload is done.
1714 * This includes CSUM, VLAN, and TSO. It
1715 * will use the first descriptor.
1718 if (m_head->m_pkthdr.csum_flags & CSUM_TSO) {
1719 if (igb_tso_setup(txr, m_head, &hdrlen)) {
1720 cmd_type_len |= E1000_ADVTXD_DCMD_TSE;
1721 olinfo_status |= E1000_TXD_POPTS_IXSM << 8;
1722 olinfo_status |= E1000_TXD_POPTS_TXSM << 8;
1727 if (igb_tx_ctx_setup(txr, m_head))
1728 olinfo_status |= E1000_TXD_POPTS_TXSM << 8;
1730 /* Calculate payload length */
1731 olinfo_status |= ((m_head->m_pkthdr.len - hdrlen)
1732 << E1000_ADVTXD_PAYLEN_SHIFT);
1734 /* 82575 needs the queue index added */
1735 if (adapter->hw.mac.type == e1000_82575)
1736 olinfo_status |= txr->me << 4;
1738 /* Set up our transmit descriptors */
1739 i = txr->next_avail_desc;
1740 for (j = 0; j < nsegs; j++) {
1742 bus_addr_t seg_addr;
1744 tx_buffer = &txr->tx_buffers[i];
1745 txd = (union e1000_adv_tx_desc *)&txr->tx_base[i];
1746 seg_addr = segs[j].ds_addr;
1747 seg_len = segs[j].ds_len;
1749 txd->read.buffer_addr = htole64(seg_addr);
1750 txd->read.cmd_type_len = htole32(cmd_type_len | seg_len);
1751 txd->read.olinfo_status = htole32(olinfo_status);
1753 if (++i == adapter->num_tx_desc)
1755 tx_buffer->m_head = NULL;
1756 tx_buffer->next_eop = -1;
1759 txr->next_avail_desc = i;
1760 txr->tx_avail -= nsegs;
1762 tx_buffer->m_head = m_head;
1763 tx_buffer_mapped->map = tx_buffer->map;
1764 tx_buffer->map = map;
1765 bus_dmamap_sync(txr->txtag, map, BUS_DMASYNC_PREWRITE);
1768 * Last Descriptor of Packet
1769 * needs End Of Packet (EOP)
1770 * and Report Status (RS)
1772 txd->read.cmd_type_len |=
1773 htole32(E1000_ADVTXD_DCMD_EOP | E1000_ADVTXD_DCMD_RS);
1775 * Keep track in the first buffer which
1776 * descriptor will be written back
1778 tx_buffer = &txr->tx_buffers[first];
1779 tx_buffer->next_eop = last;
1780 txr->watchdog_time = ticks;
1783 * Advance the Transmit Descriptor Tail (TDT), this tells the E1000
1784 * that this frame is available to transmit.
1786 bus_dmamap_sync(txr->txdma.dma_tag, txr->txdma.dma_map,
1787 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1788 E1000_WRITE_REG(&adapter->hw, E1000_TDT(txr->me), i);
1796 igb_set_promisc(struct adapter *adapter)
1798 struct ifnet *ifp = adapter->ifp;
1801 reg_rctl = E1000_READ_REG(&adapter->hw, E1000_RCTL);
1803 if (ifp->if_flags & IFF_PROMISC) {
1804 reg_rctl |= (E1000_RCTL_UPE | E1000_RCTL_MPE);
1805 E1000_WRITE_REG(&adapter->hw, E1000_RCTL, reg_rctl);
1806 } else if (ifp->if_flags & IFF_ALLMULTI) {
1807 reg_rctl |= E1000_RCTL_MPE;
1808 reg_rctl &= ~E1000_RCTL_UPE;
1809 E1000_WRITE_REG(&adapter->hw, E1000_RCTL, reg_rctl);
1814 igb_disable_promisc(struct adapter *adapter)
1818 reg_rctl = E1000_READ_REG(&adapter->hw, E1000_RCTL);
1820 reg_rctl &= (~E1000_RCTL_UPE);
1821 reg_rctl &= (~E1000_RCTL_MPE);
1822 E1000_WRITE_REG(&adapter->hw, E1000_RCTL, reg_rctl);
1826 /*********************************************************************
1829 * This routine is called whenever multicast address list is updated.
1831 **********************************************************************/
1834 igb_set_multi(struct adapter *adapter)
1836 struct ifnet *ifp = adapter->ifp;
1837 struct ifmultiaddr *ifma;
1839 static u8 mta[MAX_NUM_MULTICAST_ADDRESSES * ETH_ADDR_LEN];
1843 IOCTL_DEBUGOUT("igb_set_multi: begin");
1846 #if __FreeBSD_version < 800000
1849 if_maddr_rlock(ifp);
1853 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1854 if (ifma->ifma_addr->sa_family != AF_LINK)
1857 if (mcnt == MAX_NUM_MULTICAST_ADDRESSES)
1860 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
1861 &mta[mcnt * ETH_ADDR_LEN], ETH_ADDR_LEN);
1865 #if __FreeBSD_version < 800000
1866 IF_ADDR_UNLOCK(ifp);
1868 if_maddr_runlock(ifp);
1872 if (mcnt >= MAX_NUM_MULTICAST_ADDRESSES) {
1873 reg_rctl = E1000_READ_REG(&adapter->hw, E1000_RCTL);
1874 reg_rctl |= E1000_RCTL_MPE;
1875 E1000_WRITE_REG(&adapter->hw, E1000_RCTL, reg_rctl);
1877 e1000_update_mc_addr_list(&adapter->hw, mta, mcnt);
1882 /*********************************************************************
1884 * This routine checks for link status,
1885 * updates statistics, and does the watchdog.
1887 **********************************************************************/
1890 igb_local_timer(void *arg)
1892 struct adapter *adapter = arg;
1894 IGB_CORE_LOCK(adapter);
1896 struct ifnet *ifp = adapter->ifp;
1897 device_t dev = adapter->dev;
1898 struct tx_ring *txr = adapter->tx_rings;
1901 IGB_CORE_LOCK_ASSERT(adapter);
1903 igb_update_link_status(adapter);
1904 igb_update_stats_counters(adapter);
1906 if (igb_display_debug_stats && ifp->if_flags & IFF_RUNNING)
1907 igb_print_hw_stats(adapter);
1910 ** Watchdog: check for time since any descriptor was cleaned
1912 for (int i = 0; i < adapter->num_queues; i++, txr++) {
1913 if (txr->watchdog_check == FALSE)
1915 if ((ticks - txr->watchdog_time) > IGB_WATCHDOG)
1919 /* Trigger an RX interrupt on all queues */
1920 #ifdef DEVICE_POLLING
1921 if ((ifp->if_flags & IFF_POLLING) == 0)
1923 E1000_WRITE_REG(&adapter->hw, E1000_EICS, adapter->rx_mask);
1924 callout_reset(&adapter->timer, hz, igb_local_timer, adapter);
1925 IGB_CORE_UNLOCK(adapter);
1929 device_printf(adapter->dev, "Watchdog timeout -- resetting\n");
1930 device_printf(dev,"Queue(%d) tdh = %d, hw tdt = %d\n", txr->me,
1931 E1000_READ_REG(&adapter->hw, E1000_TDH(txr->me)),
1932 E1000_READ_REG(&adapter->hw, E1000_TDT(txr->me)));
1933 device_printf(dev,"TX(%d) desc avail = %d,"
1934 "Next TX to Clean = %d\n",
1935 txr->me, txr->tx_avail, txr->next_to_clean);
1936 adapter->ifp->if_flags &= ~IFF_RUNNING;
1937 adapter->watchdog_events++;
1938 igb_init_locked(adapter);
1939 IGB_CORE_UNLOCK(adapter);
1943 igb_update_link_status(struct adapter *adapter)
1945 struct e1000_hw *hw = &adapter->hw;
1946 struct ifnet *ifp = adapter->ifp;
1947 device_t dev = adapter->dev;
1948 struct tx_ring *txr = adapter->tx_rings;
1951 /* Get the cached link value or read for real */
1952 switch (hw->phy.media_type) {
1953 case e1000_media_type_copper:
1954 if (hw->mac.get_link_status) {
1955 /* Do the work to read phy */
1956 e1000_check_for_link(hw);
1957 link_check = !hw->mac.get_link_status;
1961 case e1000_media_type_fiber:
1962 e1000_check_for_link(hw);
1963 link_check = (E1000_READ_REG(hw, E1000_STATUS) &
1966 case e1000_media_type_internal_serdes:
1967 e1000_check_for_link(hw);
1968 link_check = adapter->hw.mac.serdes_has_link;
1971 case e1000_media_type_unknown:
1975 /* Now we check if a transition has happened */
1976 if (link_check && (adapter->link_active == 0)) {
1977 e1000_get_speed_and_duplex(&adapter->hw,
1978 &adapter->link_speed, &adapter->link_duplex);
1980 device_printf(dev, "Link is up %d Mbps %s\n",
1981 adapter->link_speed,
1982 ((adapter->link_duplex == FULL_DUPLEX) ?
1983 "Full Duplex" : "Half Duplex"));
1984 adapter->link_active = 1;
1985 ifp->if_baudrate = adapter->link_speed * 1000000;
1986 ifp->if_link_state = LINK_STATE_UP;
1987 if_link_state_change(ifp);
1988 } else if (!link_check && (adapter->link_active == 1)) {
1989 ifp->if_baudrate = adapter->link_speed = 0;
1990 adapter->link_duplex = 0;
1992 device_printf(dev, "Link is Down\n");
1993 adapter->link_active = 0;
1994 ifp->if_link_state = LINK_STATE_DOWN;
1995 if_link_state_change(ifp);
1996 /* Turn off watchdogs */
1997 for (int i = 0; i < adapter->num_queues; i++, txr++)
1998 txr->watchdog_check = FALSE;
2002 /*********************************************************************
2004 * This routine disables all traffic on the adapter by issuing a
2005 * global reset on the MAC and deallocates TX/RX buffers.
2007 **********************************************************************/
2012 struct adapter *adapter = arg;
2013 struct ifnet *ifp = adapter->ifp;
2014 struct tx_ring *txr = adapter->tx_rings;
2016 IGB_CORE_LOCK_ASSERT(adapter);
2018 INIT_DEBUGOUT("igb_stop: begin");
2020 igb_disable_intr(adapter);
2022 callout_stop(&adapter->timer);
2024 /* Tell the stack that the interface is no longer active */
2025 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
2027 /* Unarm watchdog timer. */
2028 for (int i = 0; i < adapter->num_queues; i++, txr++) {
2030 txr->watchdog_check = FALSE;
2034 e1000_reset_hw(&adapter->hw);
2035 E1000_WRITE_REG(&adapter->hw, E1000_WUC, 0);
2039 /*********************************************************************
2041 * Determine hardware revision.
2043 **********************************************************************/
2045 igb_identify_hardware(struct adapter *adapter)
2047 device_t dev = adapter->dev;
2049 /* Make sure our PCI config space has the necessary stuff set */
2050 adapter->hw.bus.pci_cmd_word = pci_read_config(dev, PCIR_COMMAND, 2);
2051 if (!((adapter->hw.bus.pci_cmd_word & PCIM_CMD_BUSMASTEREN) &&
2052 (adapter->hw.bus.pci_cmd_word & PCIM_CMD_MEMEN))) {
2053 device_printf(dev, "Memory Access and/or Bus Master bits "
2055 adapter->hw.bus.pci_cmd_word |=
2056 (PCIM_CMD_BUSMASTEREN | PCIM_CMD_MEMEN);
2057 pci_write_config(dev, PCIR_COMMAND,
2058 adapter->hw.bus.pci_cmd_word, 2);
2061 /* Save off the information about this board */
2062 adapter->hw.vendor_id = pci_get_vendor(dev);
2063 adapter->hw.device_id = pci_get_device(dev);
2064 adapter->hw.revision_id = pci_read_config(dev, PCIR_REVID, 1);
2065 adapter->hw.subsystem_vendor_id =
2066 pci_read_config(dev, PCIR_SUBVEND_0, 2);
2067 adapter->hw.subsystem_device_id =
2068 pci_read_config(dev, PCIR_SUBDEV_0, 2);
2070 /* Do Shared Code Init and Setup */
2071 if (e1000_set_mac_type(&adapter->hw)) {
2072 device_printf(dev, "Setup init failure\n");
2078 igb_allocate_pci_resources(struct adapter *adapter)
2080 device_t dev = adapter->dev;
2084 adapter->pci_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
2086 if (adapter->pci_mem == NULL) {
2087 device_printf(dev, "Unable to allocate bus resource: memory\n");
2090 adapter->osdep.mem_bus_space_tag =
2091 rman_get_bustag(adapter->pci_mem);
2092 adapter->osdep.mem_bus_space_handle =
2093 rman_get_bushandle(adapter->pci_mem);
2094 adapter->hw.hw_addr = (u8 *)&adapter->osdep.mem_bus_space_handle;
2096 adapter->num_queues = 1; /* Defaults for Legacy or MSI */
2098 /* This will setup either MSI/X or MSI */
2099 adapter->msix = igb_setup_msix(adapter);
2100 adapter->hw.back = &adapter->osdep;
2105 /*********************************************************************
2107 * Setup the Legacy or MSI Interrupt handler
2109 **********************************************************************/
2111 igb_allocate_legacy(struct adapter *adapter)
2113 device_t dev = adapter->dev;
2116 /* Turn off all interrupts */
2117 E1000_WRITE_REG(&adapter->hw, E1000_IMC, 0xffffffff);
2121 if (adapter->msix == 1)
2125 /* We allocate a single interrupt resource */
2126 adapter->res = bus_alloc_resource_any(dev,
2127 SYS_RES_IRQ, &rid, RF_SHAREABLE | RF_ACTIVE);
2128 if (adapter->res == NULL) {
2129 device_printf(dev, "Unable to allocate bus resource: "
2135 * Try allocating a fast interrupt and the associated deferred
2136 * processing contexts.
2138 TASK_INIT(&adapter->rxtx_task, 0, igb_handle_rxtx, adapter);
2139 /* Make tasklet for deferred link handling */
2140 TASK_INIT(&adapter->link_task, 0, igb_handle_link, adapter);
2141 adapter->tq = taskqueue_create("igb_taskq", M_INTWAIT,
2142 taskqueue_thread_enqueue, &adapter->tq);
2143 taskqueue_start_threads(&adapter->tq, 1, TDPRI_KERN_DAEMON /*PI_NET*/, -1, "%s taskq",
2144 device_get_nameunit(adapter->dev));
2145 if ((error = bus_setup_intr(dev, adapter->res,
2146 /*INTR_TYPE_NET |*/ INTR_MPSAFE, igb_irq_fast,
2147 adapter, &adapter->tag, NULL)) != 0) {
2148 device_printf(dev, "Failed to register fast interrupt "
2149 "handler: %d\n", error);
2150 taskqueue_free(adapter->tq);
2159 /*********************************************************************
2161 * Setup the MSIX Queue Interrupt handlers:
2163 **********************************************************************/
2165 igb_allocate_msix(struct adapter *adapter)
2167 device_t dev = adapter->dev;
2168 struct igb_queue *que = adapter->queues;
2169 int error, rid, vector = 0;
2172 for (int i = 0; i < adapter->num_queues; i++, vector++, que++) {
2174 que->res = bus_alloc_resource_any(dev,
2175 SYS_RES_IRQ, &rid, RF_SHAREABLE | RF_ACTIVE);
2176 if (que->res == NULL) {
2178 "Unable to allocate bus resource: "
2179 "MSIX Queue Interrupt\n");
2182 error = bus_setup_intr(dev, que->res,
2183 /*INTR_TYPE_NET |*/ INTR_MPSAFE,
2184 igb_msix_que, que, &que->tag, NULL);
2187 device_printf(dev, "Failed to register Queue handler");
2191 if (adapter->hw.mac.type == e1000_82575)
2192 que->eims = E1000_EICR_TX_QUEUE0 << i;
2194 que->eims = 1 << vector;
2196 ** Bind the msix vector, and thus the
2197 ** rings to the corresponding cpu.
2200 if (adapter->num_queues > 1)
2201 bus_bind_intr(dev, que->res, i);
2203 /* Make tasklet for deferred handling */
2204 TASK_INIT(&que->que_task, 0, igb_handle_que, que);
2205 que->tq = taskqueue_create("igb_que", M_INTWAIT,
2206 taskqueue_thread_enqueue, &que->tq);
2207 taskqueue_start_threads(&que->tq, 1, TDPRI_KERN_DAEMON /*PI_NET*/, -1, "%s que",
2208 device_get_nameunit(adapter->dev));
2213 adapter->res = bus_alloc_resource_any(dev,
2214 SYS_RES_IRQ, &rid, RF_SHAREABLE | RF_ACTIVE);
2215 if (adapter->res == NULL) {
2217 "Unable to allocate bus resource: "
2218 "MSIX Link Interrupt\n");
2221 if ((error = bus_setup_intr(dev, adapter->res,
2222 /*INTR_TYPE_NET |*/ INTR_MPSAFE,
2223 igb_msix_link, adapter, &adapter->tag, NULL)) != 0) {
2224 device_printf(dev, "Failed to register Link handler");
2227 adapter->linkvec = vector;
2229 /* Make tasklet for deferred handling */
2230 TASK_INIT(&adapter->link_task, 0, igb_handle_link, adapter);
2231 adapter->tq = taskqueue_create("igb_link", M_INTWAIT,
2232 taskqueue_thread_enqueue, &adapter->tq);
2233 taskqueue_start_threads(&adapter->tq, 1, TDPRI_KERN_DAEMON /*PI_NET*/, -1, "%s link",
2234 device_get_nameunit(adapter->dev));
2241 igb_configure_queues(struct adapter *adapter)
2243 struct e1000_hw *hw = &adapter->hw;
2244 struct igb_queue *que;
2246 u32 newitr = IGB_DEFAULT_ITR;
2248 /* First turn on RSS capability */
2249 if (adapter->hw.mac.type > e1000_82575)
2250 E1000_WRITE_REG(hw, E1000_GPIE,
2251 E1000_GPIE_MSIX_MODE | E1000_GPIE_EIAME |
2252 E1000_GPIE_PBA | E1000_GPIE_NSICR);
2255 switch (adapter->hw.mac.type) {
2258 for (int i = 0; i < adapter->num_queues; i++) {
2260 ivar = E1000_READ_REG_ARRAY(hw, E1000_IVAR0, index);
2261 que = &adapter->queues[i];
2264 ivar |= (que->msix | E1000_IVAR_VALID) << 16;
2267 ivar |= que->msix | E1000_IVAR_VALID;
2269 E1000_WRITE_REG_ARRAY(hw, E1000_IVAR0, index, ivar);
2272 for (int i = 0; i < adapter->num_queues; i++) {
2274 ivar = E1000_READ_REG_ARRAY(hw, E1000_IVAR0, index);
2275 que = &adapter->queues[i];
2278 ivar |= (que->msix | E1000_IVAR_VALID) << 24;
2281 ivar |= (que->msix | E1000_IVAR_VALID) << 8;
2283 E1000_WRITE_REG_ARRAY(hw, E1000_IVAR0, index, ivar);
2284 adapter->eims_mask |= que->eims;
2287 /* And for the link interrupt */
2288 ivar = (adapter->linkvec | E1000_IVAR_VALID) << 8;
2289 adapter->link_mask = 1 << adapter->linkvec;
2290 adapter->eims_mask |= adapter->link_mask;
2291 E1000_WRITE_REG(hw, E1000_IVAR_MISC, ivar);
2295 for (int i = 0; i < adapter->num_queues; i++) {
2296 u32 index = i & 0x7; /* Each IVAR has two entries */
2297 ivar = E1000_READ_REG_ARRAY(hw, E1000_IVAR0, index);
2298 que = &adapter->queues[i];
2301 ivar |= que->msix | E1000_IVAR_VALID;
2304 ivar |= (que->msix | E1000_IVAR_VALID) << 16;
2306 E1000_WRITE_REG_ARRAY(hw, E1000_IVAR0, index, ivar);
2307 adapter->eims_mask |= que->eims;
2310 for (int i = 0; i < adapter->num_queues; i++) {
2311 u32 index = i & 0x7; /* Each IVAR has two entries */
2312 ivar = E1000_READ_REG_ARRAY(hw, E1000_IVAR0, index);
2313 que = &adapter->queues[i];
2316 ivar |= (que->msix | E1000_IVAR_VALID) << 8;
2319 ivar |= (que->msix | E1000_IVAR_VALID) << 24;
2321 E1000_WRITE_REG_ARRAY(hw, E1000_IVAR0, index, ivar);
2322 adapter->eims_mask |= que->eims;
2325 /* And for the link interrupt */
2326 ivar = (adapter->linkvec | E1000_IVAR_VALID) << 8;
2327 adapter->link_mask = 1 << adapter->linkvec;
2328 adapter->eims_mask |= adapter->link_mask;
2329 E1000_WRITE_REG(hw, E1000_IVAR_MISC, ivar);
2333 /* enable MSI-X support*/
2334 tmp = E1000_READ_REG(hw, E1000_CTRL_EXT);
2335 tmp |= E1000_CTRL_EXT_PBA_CLR;
2336 /* Auto-Mask interrupts upon ICR read. */
2337 tmp |= E1000_CTRL_EXT_EIAME;
2338 tmp |= E1000_CTRL_EXT_IRCA;
2339 E1000_WRITE_REG(hw, E1000_CTRL_EXT, tmp);
2342 for (int i = 0; i < adapter->num_queues; i++) {
2343 que = &adapter->queues[i];
2344 tmp = E1000_EICR_RX_QUEUE0 << i;
2345 tmp |= E1000_EICR_TX_QUEUE0 << i;
2347 E1000_WRITE_REG_ARRAY(hw, E1000_MSIXBM(0),
2349 adapter->eims_mask |= que->eims;
2353 E1000_WRITE_REG(hw, E1000_MSIXBM(adapter->linkvec),
2355 adapter->link_mask |= E1000_EIMS_OTHER;
2356 adapter->eims_mask |= adapter->link_mask;
2361 /* Set the starting interrupt rate */
2362 if (hw->mac.type == e1000_82575)
2363 newitr |= newitr << 16;
2365 newitr |= 0x8000000;
2367 for (int i = 0; i < adapter->num_queues; i++) {
2368 que = &adapter->queues[i];
2369 E1000_WRITE_REG(hw, E1000_EITR(que->msix), newitr);
2377 igb_free_pci_resources(struct adapter *adapter)
2379 struct igb_queue *que = adapter->queues;
2380 device_t dev = adapter->dev;
2384 ** There is a slight possibility of a failure mode
2385 ** in attach that will result in entering this function
2386 ** before interrupt resources have been initialized, and
2387 ** in that case we do not want to execute the loops below
2388 ** We can detect this reliably by the state of the adapter
2391 if (adapter->res == NULL)
2395 * First release all the interrupt resources:
2397 for (int i = 0; i < adapter->num_queues; i++, que++) {
2398 rid = que->msix + 1;
2399 if (que->tag != NULL) {
2400 bus_teardown_intr(dev, que->res, que->tag);
2403 if (que->res != NULL)
2404 bus_release_resource(dev,
2405 SYS_RES_IRQ, rid, que->res);
2408 /* Clean the Legacy or Link interrupt last */
2409 if (adapter->linkvec) /* we are doing MSIX */
2410 rid = adapter->linkvec + 1;
2412 (adapter->msix != 0) ? (rid = 1):(rid = 0);
2414 if (adapter->tag != NULL) {
2415 bus_teardown_intr(dev, adapter->res, adapter->tag);
2416 adapter->tag = NULL;
2418 if (adapter->res != NULL)
2419 bus_release_resource(dev, SYS_RES_IRQ, rid, adapter->res);
2423 pci_release_msi(dev);
2425 if (adapter->msix_mem != NULL)
2426 bus_release_resource(dev, SYS_RES_MEMORY,
2427 PCIR_BAR(IGB_MSIX_BAR), adapter->msix_mem);
2429 if (adapter->pci_mem != NULL)
2430 bus_release_resource(dev, SYS_RES_MEMORY,
2431 PCIR_BAR(0), adapter->pci_mem);
2436 * Setup Either MSI/X or MSI
2439 igb_setup_msix(struct adapter *adapter)
2441 device_t dev = adapter->dev;
2442 int rid, want, queues, msgs;
2444 /* tuneable override */
2445 if (igb_enable_msix == 0)
2448 /* First try MSI/X */
2449 rid = PCIR_BAR(IGB_MSIX_BAR);
2450 adapter->msix_mem = bus_alloc_resource_any(dev,
2451 SYS_RES_MEMORY, &rid, RF_ACTIVE);
2452 if (!adapter->msix_mem) {
2453 /* May not be enabled */
2454 device_printf(adapter->dev,
2455 "Unable to map MSIX table \n");
2459 msgs = pci_msix_count(dev);
2460 if (msgs == 0) { /* system has msix disabled */
2461 bus_release_resource(dev, SYS_RES_MEMORY,
2462 PCIR_BAR(IGB_MSIX_BAR), adapter->msix_mem);
2463 adapter->msix_mem = NULL;
2467 /* Figure out a reasonable auto config value */
2468 queues = (ncpus > (msgs-1)) ? (msgs-1) : ncpus;
2470 /* Can have max of 4 queues on 82575 */
2471 if (adapter->hw.mac.type == e1000_82575) {
2474 if (igb_num_queues > 4)
2478 if (igb_num_queues == 0)
2479 igb_num_queues = queues;
2482 ** One vector (RX/TX pair) per queue
2483 ** plus an additional for Link interrupt
2485 want = igb_num_queues + 1;
2489 device_printf(adapter->dev,
2490 "MSIX Configuration Problem, "
2491 "%d vectors configured, but %d queues wanted!\n",
2495 if ((msgs) && pci_alloc_msix(dev, &msgs) == 0) {
2496 device_printf(adapter->dev,
2497 "Using MSIX interrupts with %d vectors\n", msgs);
2498 adapter->num_queues = igb_num_queues;
2502 msgs = pci_msi_count(dev);
2503 if (msgs == 1 && pci_alloc_msi(dev, &msgs) == 0)
2504 device_printf(adapter->dev,"Using MSI interrupt\n");
2508 /*********************************************************************
2510 * Set up an fresh starting state
2512 **********************************************************************/
2514 igb_reset(struct adapter *adapter)
2516 device_t dev = adapter->dev;
2517 struct e1000_hw *hw = &adapter->hw;
2518 struct e1000_fc_info *fc = &hw->fc;
2519 struct ifnet *ifp = adapter->ifp;
2523 INIT_DEBUGOUT("igb_reset: begin");
2525 /* Let the firmware know the OS is in control */
2526 igb_get_hw_control(adapter);
2529 * Packet Buffer Allocation (PBA)
2530 * Writing PBA sets the receive portion of the buffer
2531 * the remainder is used for the transmit buffer.
2533 switch (hw->mac.type) {
2535 pba = E1000_PBA_32K;
2538 pba = E1000_PBA_64K;
2541 pba = E1000_PBA_35K;
2546 /* Special needs in case of Jumbo frames */
2547 if ((hw->mac.type == e1000_82575) && (ifp->if_mtu > ETHERMTU)) {
2548 u32 tx_space, min_tx, min_rx;
2549 pba = E1000_READ_REG(hw, E1000_PBA);
2550 tx_space = pba >> 16;
2552 min_tx = (adapter->max_frame_size +
2553 sizeof(struct e1000_tx_desc) - ETHERNET_FCS_SIZE) * 2;
2554 min_tx = roundup2(min_tx, 1024);
2556 min_rx = adapter->max_frame_size;
2557 min_rx = roundup2(min_rx, 1024);
2559 if (tx_space < min_tx &&
2560 ((min_tx - tx_space) < pba)) {
2561 pba = pba - (min_tx - tx_space);
2563 * if short on rx space, rx wins
2564 * and must trump tx adjustment
2569 E1000_WRITE_REG(hw, E1000_PBA, pba);
2572 INIT_DEBUGOUT1("igb_init: pba=%dK",pba);
2575 * These parameters control the automatic generation (Tx) and
2576 * response (Rx) to Ethernet PAUSE frames.
2577 * - High water mark should allow for at least two frames to be
2578 * received after sending an XOFF.
2579 * - Low water mark works best when it is very near the high water mark.
2580 * This allows the receiver to restart by sending XON when it has
2583 hwm = min(((pba << 10) * 9 / 10),
2584 ((pba << 10) - 2 * adapter->max_frame_size));
2586 if (hw->mac.type < e1000_82576) {
2587 fc->high_water = hwm & 0xFFF8; /* 8-byte granularity */
2588 fc->low_water = fc->high_water - 8;
2590 fc->high_water = hwm & 0xFFF0; /* 16-byte granularity */
2591 fc->low_water = fc->high_water - 16;
2594 fc->pause_time = IGB_FC_PAUSE_TIME;
2595 fc->send_xon = TRUE;
2597 /* Set Flow control, use the tunable location if sane */
2598 if ((igb_fc_setting >= 0) || (igb_fc_setting < 4))
2599 fc->requested_mode = igb_fc_setting;
2601 fc->requested_mode = e1000_fc_none;
2603 fc->current_mode = fc->requested_mode;
2605 /* Issue a global reset */
2607 E1000_WRITE_REG(hw, E1000_WUC, 0);
2609 if (e1000_init_hw(hw) < 0)
2610 device_printf(dev, "Hardware Initialization Failed\n");
2612 if (hw->mac.type == e1000_82580) {
2615 hwm = (pba << 10) - (2 * adapter->max_frame_size);
2617 * 0x80000000 - enable DMA COAL
2618 * 0x10000000 - use L0s as low power
2619 * 0x20000000 - use L1 as low power
2620 * X << 16 - exit dma coal when rx data exceeds X kB
2621 * Y - upper limit to stay in dma coal in units of 32usecs
2623 E1000_WRITE_REG(hw, E1000_DMACR,
2624 0xA0000006 | ((hwm << 6) & 0x00FF0000));
2626 /* set hwm to PBA - 2 * max frame size */
2627 E1000_WRITE_REG(hw, E1000_FCRTC, hwm);
2629 * This sets the time to wait before requesting transition to
2630 * low power state to number of usecs needed to receive 1 512
2631 * byte frame at gigabit line rate
2633 E1000_WRITE_REG(hw, E1000_DMCTLX, 4);
2635 /* free space in tx packet buffer to wake from DMA coal */
2636 E1000_WRITE_REG(hw, E1000_DMCTXTH,
2637 (20480 - (2 * adapter->max_frame_size)) >> 6);
2639 /* make low power state decision controlled by DMA coal */
2640 reg = E1000_READ_REG(hw, E1000_PCIEMISC);
2641 E1000_WRITE_REG(hw, E1000_PCIEMISC,
2642 reg | E1000_PCIEMISC_LX_DECISION);
2645 E1000_WRITE_REG(&adapter->hw, E1000_VET, ETHERTYPE_VLAN);
2646 e1000_get_phy_info(hw);
2647 e1000_check_for_link(hw);
2651 /*********************************************************************
2653 * Setup networking device structure and register an interface.
2655 **********************************************************************/
2657 igb_setup_interface(device_t dev, struct adapter *adapter)
2661 INIT_DEBUGOUT("igb_setup_interface: begin");
2663 ifp = adapter->ifp = &adapter->arpcom.ac_if;
2664 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
2665 ifp->if_mtu = ETHERMTU;
2666 ifp->if_init = igb_init;
2667 ifp->if_softc = adapter;
2668 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
2669 ifp->if_ioctl = igb_ioctl;
2670 ifp->if_start = igb_start;
2671 #ifdef DEVICE_POLLING
2672 ifp->if_poll = igb_poll;
2674 #if __FreeBSD_version >= 800000
2675 ifp->if_transmit = igb_mq_start;
2676 ifp->if_qflush = igb_qflush;
2678 ifq_set_maxlen(&ifp->if_snd, adapter->num_tx_desc - 1);
2679 ifq_set_ready(&ifp->if_snd);
2681 ether_ifattach(ifp, adapter->hw.mac.addr, NULL);
2683 ifp->if_capabilities = ifp->if_capenable = 0;
2685 ifp->if_capabilities = IFCAP_HWCSUM | IFCAP_VLAN_MTU;
2687 ifp->if_capabilities |= IFCAP_TSO4;
2689 ifp->if_capabilities |= IFCAP_JUMBO_MTU;
2691 if (igb_header_split)
2692 ifp->if_capabilities |= IFCAP_LRO;
2695 ifp->if_capenable = ifp->if_capabilities;
2698 * Tell the upper layer(s) we support long frames.
2700 ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
2701 ifp->if_capabilities |= IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_MTU;
2702 ifp->if_capenable |= IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_MTU;
2705 * Specify the media types supported by this adapter and register
2706 * callbacks to update media and link information
2708 ifmedia_init(&adapter->media, IFM_IMASK,
2709 igb_media_change, igb_media_status);
2710 if ((adapter->hw.phy.media_type == e1000_media_type_fiber) ||
2711 (adapter->hw.phy.media_type == e1000_media_type_internal_serdes)) {
2712 ifmedia_add(&adapter->media, IFM_ETHER | IFM_1000_SX | IFM_FDX,
2714 ifmedia_add(&adapter->media, IFM_ETHER | IFM_1000_SX, 0, NULL);
2716 ifmedia_add(&adapter->media, IFM_ETHER | IFM_10_T, 0, NULL);
2717 ifmedia_add(&adapter->media, IFM_ETHER | IFM_10_T | IFM_FDX,
2719 ifmedia_add(&adapter->media, IFM_ETHER | IFM_100_TX,
2721 ifmedia_add(&adapter->media, IFM_ETHER | IFM_100_TX | IFM_FDX,
2723 if (adapter->hw.phy.type != e1000_phy_ife) {
2724 ifmedia_add(&adapter->media,
2725 IFM_ETHER | IFM_1000_T | IFM_FDX, 0, NULL);
2726 ifmedia_add(&adapter->media,
2727 IFM_ETHER | IFM_1000_T, 0, NULL);
2730 ifmedia_add(&adapter->media, IFM_ETHER | IFM_AUTO, 0, NULL);
2731 ifmedia_set(&adapter->media, IFM_ETHER | IFM_AUTO);
2736 * Manage DMA'able memory.
2739 igb_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
2743 *(bus_addr_t *) arg = segs[0].ds_addr;
2747 igb_dma_malloc(struct adapter *adapter, bus_size_t size,
2748 struct igb_dma_alloc *dma, int mapflags)
2752 error = bus_dma_tag_create(NULL, /* parent */
2753 IGB_DBA_ALIGN, 0, /* alignment, bounds */
2754 BUS_SPACE_MAXADDR, /* lowaddr */
2755 BUS_SPACE_MAXADDR, /* highaddr */
2756 NULL, NULL, /* filter, filterarg */
2759 size, /* maxsegsize */
2763 device_printf(adapter->dev,
2764 "%s: bus_dma_tag_create failed: %d\n",
2769 error = bus_dmamem_alloc(dma->dma_tag, (void**) &dma->dma_vaddr,
2770 BUS_DMA_NOWAIT, &dma->dma_map);
2772 device_printf(adapter->dev,
2773 "%s: bus_dmamem_alloc(%ju) failed: %d\n",
2774 __func__, (uintmax_t)size, error);
2779 error = bus_dmamap_load(dma->dma_tag, dma->dma_map, dma->dma_vaddr,
2780 size, igb_dmamap_cb, &dma->dma_paddr, mapflags | BUS_DMA_NOWAIT);
2781 if (error || dma->dma_paddr == 0) {
2782 device_printf(adapter->dev,
2783 "%s: bus_dmamap_load failed: %d\n",
2791 bus_dmamap_unload(dma->dma_tag, dma->dma_map);
2793 bus_dmamem_free(dma->dma_tag, dma->dma_vaddr, dma->dma_map);
2794 bus_dma_tag_destroy(dma->dma_tag);
2796 dma->dma_map = NULL;
2797 dma->dma_tag = NULL;
2803 igb_dma_free(struct adapter *adapter, struct igb_dma_alloc *dma)
2805 if (dma->dma_tag == NULL)
2807 if (dma->dma_map != NULL) {
2808 bus_dmamap_sync(dma->dma_tag, dma->dma_map,
2809 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
2810 bus_dmamap_unload(dma->dma_tag, dma->dma_map);
2811 bus_dmamem_free(dma->dma_tag, dma->dma_vaddr, dma->dma_map);
2812 dma->dma_map = NULL;
2814 bus_dma_tag_destroy(dma->dma_tag);
2815 dma->dma_tag = NULL;
2819 /*********************************************************************
2821 * Allocate memory for the transmit and receive rings, and then
2822 * the descriptors associated with each, called only once at attach.
2824 **********************************************************************/
2826 igb_allocate_queues(struct adapter *adapter)
2828 device_t dev = adapter->dev;
2829 struct igb_queue *que = NULL;
2830 struct tx_ring *txr = NULL;
2831 struct rx_ring *rxr = NULL;
2832 int rsize, tsize, error = E1000_SUCCESS;
2833 int txconf = 0, rxconf = 0;
2835 /* First allocate the top level queue structs */
2836 if (!(adapter->queues =
2837 (struct igb_queue *) kmalloc(sizeof(struct igb_queue) *
2838 adapter->num_queues, M_DEVBUF, M_INTWAIT | M_ZERO))) {
2839 device_printf(dev, "Unable to allocate queue memory\n");
2844 /* Next allocate the TX ring struct memory */
2845 if (!(adapter->tx_rings =
2846 (struct tx_ring *) kmalloc(sizeof(struct tx_ring) *
2847 adapter->num_queues, M_DEVBUF, M_INTWAIT | M_ZERO))) {
2848 device_printf(dev, "Unable to allocate TX ring memory\n");
2853 /* Now allocate the RX */
2854 if (!(adapter->rx_rings =
2855 (struct rx_ring *) kmalloc(sizeof(struct rx_ring) *
2856 adapter->num_queues, M_DEVBUF, M_INTWAIT | M_ZERO))) {
2857 device_printf(dev, "Unable to allocate RX ring memory\n");
2862 tsize = roundup2(adapter->num_tx_desc *
2863 sizeof(union e1000_adv_tx_desc), IGB_DBA_ALIGN);
2865 * Now set up the TX queues, txconf is needed to handle the
2866 * possibility that things fail midcourse and we need to
2867 * undo memory gracefully
2869 for (int i = 0; i < adapter->num_queues; i++, txconf++) {
2870 /* Set up some basics */
2871 txr = &adapter->tx_rings[i];
2872 txr->adapter = adapter;
2875 /* Initialize the TX lock */
2876 ksnprintf(txr->spin_name, sizeof(txr->spin_name), "%s:tx(%d)",
2877 device_get_nameunit(dev), txr->me);
2879 IGB_TX_LOCK_INIT(txr);
2881 if (igb_dma_malloc(adapter, tsize,
2882 &txr->txdma, BUS_DMA_NOWAIT)) {
2884 "Unable to allocate TX Descriptor memory\n");
2888 txr->tx_base = (struct e1000_tx_desc *)txr->txdma.dma_vaddr;
2889 bzero((void *)txr->tx_base, tsize);
2891 /* Now allocate transmit buffers for the ring */
2892 if (igb_allocate_transmit_buffers(txr)) {
2894 "Critical Failure setting up transmit buffers\n");
2898 #if __FreeBSD_version >= 800000
2899 /* Allocate a buf ring */
2900 txr->br = buf_ring_alloc(IGB_BR_SIZE, M_DEVBUF,
2901 M_WAITOK, &txr->tx_mtx);
2906 * Next the RX queues...
2908 rsize = roundup2(adapter->num_rx_desc *
2909 sizeof(union e1000_adv_rx_desc), IGB_DBA_ALIGN);
2910 for (int i = 0; i < adapter->num_queues; i++, rxconf++) {
2911 rxr = &adapter->rx_rings[i];
2912 rxr->adapter = adapter;
2915 /* Initialize the RX lock */
2916 ksnprintf(rxr->spin_name, sizeof(rxr->spin_name), "%s:rx(%d)",
2917 device_get_nameunit(dev), txr->me);
2919 IGB_RX_LOCK_INIT(rxr);
2921 if (igb_dma_malloc(adapter, rsize,
2922 &rxr->rxdma, BUS_DMA_NOWAIT)) {
2924 "Unable to allocate RxDescriptor memory\n");
2928 rxr->rx_base = (union e1000_adv_rx_desc *)rxr->rxdma.dma_vaddr;
2929 bzero((void *)rxr->rx_base, rsize);
2931 /* Allocate receive buffers for the ring*/
2932 if (igb_allocate_receive_buffers(rxr)) {
2934 "Critical Failure setting up receive buffers\n");
2941 ** Finally set up the queue holding structs
2943 for (int i = 0; i < adapter->num_queues; i++) {
2944 que = &adapter->queues[i];
2945 que->adapter = adapter;
2946 que->txr = &adapter->tx_rings[i];
2947 que->rxr = &adapter->rx_rings[i];
2953 for (rxr = adapter->rx_rings; rxconf > 0; rxr++, rxconf--)
2954 igb_dma_free(adapter, &rxr->rxdma);
2956 for (txr = adapter->tx_rings; txconf > 0; txr++, txconf--)
2957 igb_dma_free(adapter, &txr->txdma);
2958 kfree(adapter->rx_rings, M_DEVBUF);
2960 #if __FreeBSD_version >= 800000
2961 buf_ring_free(txr->br, M_DEVBUF);
2963 kfree(adapter->tx_rings, M_DEVBUF);
2965 kfree(adapter->queues, M_DEVBUF);
2970 /*********************************************************************
2972 * Allocate memory for tx_buffer structures. The tx_buffer stores all
2973 * the information needed to transmit a packet on the wire. This is
2974 * called only once at attach, setup is done every reset.
2976 **********************************************************************/
2978 igb_allocate_transmit_buffers(struct tx_ring *txr)
2980 struct adapter *adapter = txr->adapter;
2981 device_t dev = adapter->dev;
2982 struct igb_tx_buffer *txbuf;
2986 * Setup DMA descriptor areas.
2988 if ((error = bus_dma_tag_create(NULL,
2989 1, 0, /* alignment, bounds */
2990 BUS_SPACE_MAXADDR, /* lowaddr */
2991 BUS_SPACE_MAXADDR, /* highaddr */
2992 NULL, NULL, /* filter, filterarg */
2993 IGB_TSO_SIZE, /* maxsize */
2994 IGB_MAX_SCATTER, /* nsegments */
2995 PAGE_SIZE, /* maxsegsize */
2998 device_printf(dev,"Unable to allocate TX DMA tag\n");
3002 if (!(txr->tx_buffers =
3003 (struct igb_tx_buffer *) kmalloc(sizeof(struct igb_tx_buffer) *
3004 adapter->num_tx_desc, M_DEVBUF, M_INTWAIT | M_ZERO))) {
3005 device_printf(dev, "Unable to allocate tx_buffer memory\n");
3010 /* Create the descriptor buffer dma maps */
3011 txbuf = txr->tx_buffers;
3012 for (i = 0; i < adapter->num_tx_desc; i++, txbuf++) {
3013 error = bus_dmamap_create(txr->txtag, 0, &txbuf->map);
3015 device_printf(dev, "Unable to create TX DMA map\n");
3022 /* We free all, it handles case where we are in the middle */
3023 igb_free_transmit_structures(adapter);
3027 /*********************************************************************
3029 * Initialize a transmit ring.
3031 **********************************************************************/
3033 igb_setup_transmit_ring(struct tx_ring *txr)
3035 struct adapter *adapter = txr->adapter;
3036 struct igb_tx_buffer *txbuf;
3039 /* Clear the old descriptor contents */
3041 bzero((void *)txr->tx_base,
3042 (sizeof(union e1000_adv_tx_desc)) * adapter->num_tx_desc);
3044 txr->next_avail_desc = 0;
3045 txr->next_to_clean = 0;
3047 /* Free any existing tx buffers. */
3048 txbuf = txr->tx_buffers;
3049 for (i = 0; i < adapter->num_tx_desc; i++, txbuf++) {
3050 if (txbuf->m_head != NULL) {
3051 bus_dmamap_sync(txr->txtag, txbuf->map,
3052 BUS_DMASYNC_POSTWRITE);
3053 bus_dmamap_unload(txr->txtag, txbuf->map);
3054 m_freem(txbuf->m_head);
3055 txbuf->m_head = NULL;
3057 /* clear the watch index */
3058 txbuf->next_eop = -1;
3061 /* Set number of descriptors available */
3062 txr->tx_avail = adapter->num_tx_desc;
3064 bus_dmamap_sync(txr->txdma.dma_tag, txr->txdma.dma_map,
3065 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
3069 /*********************************************************************
3071 * Initialize all transmit rings.
3073 **********************************************************************/
3075 igb_setup_transmit_structures(struct adapter *adapter)
3077 struct tx_ring *txr = adapter->tx_rings;
3079 for (int i = 0; i < adapter->num_queues; i++, txr++)
3080 igb_setup_transmit_ring(txr);
3085 /*********************************************************************
3087 * Enable transmit unit.
3089 **********************************************************************/
3091 igb_initialize_transmit_units(struct adapter *adapter)
3093 struct tx_ring *txr = adapter->tx_rings;
3094 struct e1000_hw *hw = &adapter->hw;
3097 INIT_DEBUGOUT("igb_initialize_transmit_units: begin");
3099 /* Setup the Tx Descriptor Rings */
3100 for (int i = 0; i < adapter->num_queues; i++, txr++) {
3101 u64 bus_addr = txr->txdma.dma_paddr;
3103 E1000_WRITE_REG(hw, E1000_TDLEN(i),
3104 adapter->num_tx_desc * sizeof(struct e1000_tx_desc));
3105 E1000_WRITE_REG(hw, E1000_TDBAH(i),
3106 (uint32_t)(bus_addr >> 32));
3107 E1000_WRITE_REG(hw, E1000_TDBAL(i),
3108 (uint32_t)bus_addr);
3110 /* Setup the HW Tx Head and Tail descriptor pointers */
3111 E1000_WRITE_REG(hw, E1000_TDT(i), 0);
3112 E1000_WRITE_REG(hw, E1000_TDH(i), 0);
3114 HW_DEBUGOUT2("Base = %x, Length = %x\n",
3115 E1000_READ_REG(hw, E1000_TDBAL(i)),
3116 E1000_READ_REG(hw, E1000_TDLEN(i)));
3118 txr->watchdog_check = FALSE;
3120 txdctl = E1000_READ_REG(hw, E1000_TXDCTL(i));
3121 txdctl |= IGB_TX_PTHRESH;
3122 txdctl |= IGB_TX_HTHRESH << 8;
3123 txdctl |= IGB_TX_WTHRESH << 16;
3124 txdctl |= E1000_TXDCTL_QUEUE_ENABLE;
3125 E1000_WRITE_REG(hw, E1000_TXDCTL(i), txdctl);
3128 /* Program the Transmit Control Register */
3129 tctl = E1000_READ_REG(hw, E1000_TCTL);
3130 tctl &= ~E1000_TCTL_CT;
3131 tctl |= (E1000_TCTL_PSP | E1000_TCTL_RTLC | E1000_TCTL_EN |
3132 (E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT));
3134 e1000_config_collision_dist(hw);
3136 /* This write will effectively turn on the transmit unit. */
3137 E1000_WRITE_REG(hw, E1000_TCTL, tctl);
3140 /*********************************************************************
3142 * Free all transmit rings.
3144 **********************************************************************/
3146 igb_free_transmit_structures(struct adapter *adapter)
3148 struct tx_ring *txr = adapter->tx_rings;
3150 for (int i = 0; i < adapter->num_queues; i++, txr++) {
3152 igb_free_transmit_buffers(txr);
3153 igb_dma_free(adapter, &txr->txdma);
3155 IGB_TX_LOCK_DESTROY(txr);
3157 kfree(adapter->tx_rings, M_DEVBUF);
3160 /*********************************************************************
3162 * Free transmit ring related data structures.
3164 **********************************************************************/
3166 igb_free_transmit_buffers(struct tx_ring *txr)
3168 struct adapter *adapter = txr->adapter;
3169 struct igb_tx_buffer *tx_buffer;
3172 INIT_DEBUGOUT("free_transmit_ring: begin");
3174 if (txr->tx_buffers == NULL)
3177 tx_buffer = txr->tx_buffers;
3178 for (i = 0; i < adapter->num_tx_desc; i++, tx_buffer++) {
3179 if (tx_buffer->m_head != NULL) {
3180 bus_dmamap_sync(txr->txtag, tx_buffer->map,
3181 BUS_DMASYNC_POSTWRITE);
3182 bus_dmamap_unload(txr->txtag,
3184 m_freem(tx_buffer->m_head);
3185 tx_buffer->m_head = NULL;
3186 if (tx_buffer->map != NULL) {
3187 bus_dmamap_destroy(txr->txtag,
3189 tx_buffer->map = NULL;
3191 } else if (tx_buffer->map != NULL) {
3192 bus_dmamap_unload(txr->txtag,
3194 bus_dmamap_destroy(txr->txtag,
3196 tx_buffer->map = NULL;
3199 #if __FreeBSD_version >= 800000
3200 if (txr->br != NULL)
3201 buf_ring_free(txr->br, M_DEVBUF);
3203 if (txr->tx_buffers != NULL) {
3204 kfree(txr->tx_buffers, M_DEVBUF);
3205 txr->tx_buffers = NULL;
3207 if (txr->txtag != NULL) {
3208 bus_dma_tag_destroy(txr->txtag);
3214 /**********************************************************************
3216 * Setup work for hardware segmentation offload (TSO)
3218 **********************************************************************/
3221 igb_tso_setup(struct tx_ring *txr, struct mbuf *mp, u32 *hdrlen)
3223 struct adapter *adapter = txr->adapter;
3224 struct e1000_adv_tx_context_desc *TXD;
3225 struct igb_tx_buffer *tx_buffer;
3226 u32 vlan_macip_lens = 0, type_tucmd_mlhl = 0;
3227 u32 mss_l4len_idx = 0;
3229 int ctxd, ehdrlen, ip_hlen, tcp_hlen;
3230 struct ether_vlan_header *eh;
3236 * Determine where frame payload starts.
3237 * Jump over vlan headers if already present
3239 eh = mtod(mp, struct ether_vlan_header *);
3240 if (eh->evl_encap_proto == htons(ETHERTYPE_VLAN))
3241 ehdrlen = ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN;
3243 ehdrlen = ETHER_HDR_LEN;
3245 /* Ensure we have at least the IP+TCP header in the first mbuf. */
3246 if (mp->m_len < ehdrlen + sizeof(struct ip) + sizeof(struct tcphdr))
3249 /* Only supports IPV4 for now */
3250 ctxd = txr->next_avail_desc;
3251 tx_buffer = &txr->tx_buffers[ctxd];
3252 TXD = (struct e1000_adv_tx_context_desc *) &txr->tx_base[ctxd];
3254 ip = (struct ip *)(mp->m_data + ehdrlen);
3255 if (ip->ip_p != IPPROTO_TCP)
3256 return FALSE; /* 0 */
3258 ip_hlen = ip->ip_hl << 2;
3259 th = (struct tcphdr *)((caddr_t)ip + ip_hlen);
3260 th->th_sum = in_pseudo(ip->ip_src.s_addr,
3261 ip->ip_dst.s_addr, htons(IPPROTO_TCP));
3262 tcp_hlen = th->th_off << 2;
3264 * Calculate header length, this is used
3265 * in the transmit desc in igb_xmit
3267 *hdrlen = ehdrlen + ip_hlen + tcp_hlen;
3269 /* VLAN MACLEN IPLEN */
3270 if (mp->m_flags & M_VLANTAG) {
3271 vtag = htole16(mp->m_pkthdr.ether_vlantag);
3272 vlan_macip_lens |= (vtag << E1000_ADVTXD_VLAN_SHIFT);
3275 vlan_macip_lens |= (ehdrlen << E1000_ADVTXD_MACLEN_SHIFT);
3276 vlan_macip_lens |= ip_hlen;
3277 TXD->vlan_macip_lens |= htole32(vlan_macip_lens);
3279 /* ADV DTYPE TUCMD */
3280 type_tucmd_mlhl |= E1000_ADVTXD_DCMD_DEXT | E1000_ADVTXD_DTYP_CTXT;
3281 type_tucmd_mlhl |= E1000_ADVTXD_TUCMD_L4T_TCP;
3282 type_tucmd_mlhl |= E1000_ADVTXD_TUCMD_IPV4;
3283 TXD->type_tucmd_mlhl |= htole32(type_tucmd_mlhl);
3286 mss_l4len_idx |= (mp->m_pkthdr.tso_segsz << E1000_ADVTXD_MSS_SHIFT);
3287 mss_l4len_idx |= (tcp_hlen << E1000_ADVTXD_L4LEN_SHIFT);
3288 /* 82575 needs the queue index added */
3289 if (adapter->hw.mac.type == e1000_82575)
3290 mss_l4len_idx |= txr->me << 4;
3291 TXD->mss_l4len_idx = htole32(mss_l4len_idx);
3293 TXD->seqnum_seed = htole32(0);
3294 tx_buffer->m_head = NULL;
3295 tx_buffer->next_eop = -1;
3297 if (++ctxd == adapter->num_tx_desc)
3301 txr->next_avail_desc = ctxd;
3306 /*********************************************************************
3308 * Context Descriptor setup for VLAN or CSUM
3310 **********************************************************************/
3313 igb_tx_ctx_setup(struct tx_ring *txr, struct mbuf *mp)
3315 struct adapter *adapter = txr->adapter;
3316 struct e1000_adv_tx_context_desc *TXD;
3317 struct igb_tx_buffer *tx_buffer;
3318 u32 vlan_macip_lens, type_tucmd_mlhl, mss_l4len_idx;
3319 struct ether_vlan_header *eh;
3320 struct ip *ip = NULL;
3321 struct ip6_hdr *ip6;
3322 int ehdrlen, ctxd, ip_hlen = 0;
3323 u16 etype, vtag = 0;
3325 bool offload = TRUE;
3327 if ((mp->m_pkthdr.csum_flags & CSUM_OFFLOAD) == 0)
3330 vlan_macip_lens = type_tucmd_mlhl = mss_l4len_idx = 0;
3331 ctxd = txr->next_avail_desc;
3332 tx_buffer = &txr->tx_buffers[ctxd];
3333 TXD = (struct e1000_adv_tx_context_desc *) &txr->tx_base[ctxd];
3336 ** In advanced descriptors the vlan tag must
3337 ** be placed into the context descriptor, thus
3338 ** we need to be here just for that setup.
3340 if (mp->m_flags & M_VLANTAG) {
3341 vtag = htole16(mp->m_pkthdr.ether_vlantag);
3342 vlan_macip_lens |= (vtag << E1000_ADVTXD_VLAN_SHIFT);
3343 } else if (offload == FALSE)
3347 * Determine where frame payload starts.
3348 * Jump over vlan headers if already present,
3349 * helpful for QinQ too.
3351 eh = mtod(mp, struct ether_vlan_header *);
3352 if (eh->evl_encap_proto == htons(ETHERTYPE_VLAN)) {
3353 etype = ntohs(eh->evl_proto);
3354 ehdrlen = ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN;
3356 etype = ntohs(eh->evl_encap_proto);
3357 ehdrlen = ETHER_HDR_LEN;
3360 /* Set the ether header length */
3361 vlan_macip_lens |= ehdrlen << E1000_ADVTXD_MACLEN_SHIFT;
3365 ip = (struct ip *)(mp->m_data + ehdrlen);
3366 ip_hlen = ip->ip_hl << 2;
3367 if (mp->m_len < ehdrlen + ip_hlen) {
3372 type_tucmd_mlhl |= E1000_ADVTXD_TUCMD_IPV4;
3374 case ETHERTYPE_IPV6:
3375 ip6 = (struct ip6_hdr *)(mp->m_data + ehdrlen);
3376 ip_hlen = sizeof(struct ip6_hdr);
3377 if (mp->m_len < ehdrlen + ip_hlen)
3379 ipproto = ip6->ip6_nxt;
3380 type_tucmd_mlhl |= E1000_ADVTXD_TUCMD_IPV6;
3387 vlan_macip_lens |= ip_hlen;
3388 type_tucmd_mlhl |= E1000_ADVTXD_DCMD_DEXT | E1000_ADVTXD_DTYP_CTXT;
3392 if (mp->m_pkthdr.csum_flags & CSUM_TCP)
3393 type_tucmd_mlhl |= E1000_ADVTXD_TUCMD_L4T_TCP;
3396 if (mp->m_pkthdr.csum_flags & CSUM_UDP)
3397 type_tucmd_mlhl |= E1000_ADVTXD_TUCMD_L4T_UDP;
3399 #if __FreeBSD_version >= 800000
3401 if (mp->m_pkthdr.csum_flags & CSUM_SCTP)
3402 type_tucmd_mlhl |= E1000_ADVTXD_TUCMD_L4T_SCTP;
3410 /* 82575 needs the queue index added */
3411 if (adapter->hw.mac.type == e1000_82575)
3412 mss_l4len_idx = txr->me << 4;
3414 /* Now copy bits into descriptor */
3415 TXD->vlan_macip_lens |= htole32(vlan_macip_lens);
3416 TXD->type_tucmd_mlhl |= htole32(type_tucmd_mlhl);
3417 TXD->seqnum_seed = htole32(0);
3418 TXD->mss_l4len_idx = htole32(mss_l4len_idx);
3420 tx_buffer->m_head = NULL;
3421 tx_buffer->next_eop = -1;
3423 /* We've consumed the first desc, adjust counters */
3424 if (++ctxd == adapter->num_tx_desc)
3426 txr->next_avail_desc = ctxd;
3433 /**********************************************************************
3435 * Examine each tx_buffer in the used queue. If the hardware is done
3436 * processing the packet then free associated resources. The
3437 * tx_buffer is put back on the free queue.
3439 * TRUE return means there's work in the ring to clean, FALSE its empty.
3440 **********************************************************************/
3442 igb_txeof(struct tx_ring *txr)
3444 struct adapter *adapter = txr->adapter;
3445 int first, last, done;
3446 struct igb_tx_buffer *tx_buffer;
3447 struct e1000_tx_desc *tx_desc, *eop_desc;
3448 struct ifnet *ifp = adapter->ifp;
3450 IGB_TX_LOCK_ASSERT(txr);
3452 if (txr->tx_avail == adapter->num_tx_desc)
3455 first = txr->next_to_clean;
3456 tx_desc = &txr->tx_base[first];
3457 tx_buffer = &txr->tx_buffers[first];
3458 last = tx_buffer->next_eop;
3459 eop_desc = &txr->tx_base[last];
3462 * What this does is get the index of the
3463 * first descriptor AFTER the EOP of the
3464 * first packet, that way we can do the
3465 * simple comparison on the inner while loop.
3467 if (++last == adapter->num_tx_desc)
3471 bus_dmamap_sync(txr->txdma.dma_tag, txr->txdma.dma_map,
3472 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
3474 while (eop_desc->upper.fields.status & E1000_TXD_STAT_DD) {
3475 /* We clean the range of the packet */
3476 while (first != done) {
3477 tx_desc->upper.data = 0;
3478 tx_desc->lower.data = 0;
3479 tx_desc->buffer_addr = 0;
3482 if (tx_buffer->m_head) {
3484 tx_buffer->m_head->m_pkthdr.len;
3485 bus_dmamap_sync(txr->txtag,
3487 BUS_DMASYNC_POSTWRITE);
3488 bus_dmamap_unload(txr->txtag,
3491 m_freem(tx_buffer->m_head);
3492 tx_buffer->m_head = NULL;
3494 tx_buffer->next_eop = -1;
3495 txr->watchdog_time = ticks;
3497 if (++first == adapter->num_tx_desc)
3500 tx_buffer = &txr->tx_buffers[first];
3501 tx_desc = &txr->tx_base[first];
3505 /* See if we can continue to the next packet */
3506 last = tx_buffer->next_eop;
3508 eop_desc = &txr->tx_base[last];
3509 /* Get new done point */
3510 if (++last == adapter->num_tx_desc) last = 0;
3515 bus_dmamap_sync(txr->txdma.dma_tag, txr->txdma.dma_map,
3516 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
3518 txr->next_to_clean = first;
3521 * If we have enough room, clear IFF_DRV_OACTIVE
3522 * to tell the stack that it is OK to send packets.
3524 if (txr->tx_avail > IGB_TX_CLEANUP_THRESHOLD) {
3525 ifp->if_flags &= ~IFF_OACTIVE;
3526 /* All clean, turn off the watchdog */
3527 if (txr->tx_avail == adapter->num_tx_desc) {
3528 txr->watchdog_check = FALSE;
3537 /*********************************************************************
3539 * Setup descriptor buffer(s) from system mbuf buffer pools.
3540 * i - designates the ring index
3541 * clean - tells the function whether to update
3542 * the header, the packet buffer, or both.
3544 **********************************************************************/
3546 igb_get_buf(struct rx_ring *rxr, int i, u8 clean)
3548 struct adapter *adapter = rxr->adapter;
3549 struct igb_rx_buf *rxbuf;
3550 struct mbuf *mh, *mp;
3551 bus_dma_segment_t hseg[1];
3552 bus_dma_segment_t pseg[1];
3558 * Init-time loads are allowed to use a blocking mbuf allocation,
3559 * otherwise the sheer number of mbufs allocated can lead to
3562 mbflags = (clean & IGB_CLEAN_INITIAL) ? MB_WAIT : MB_DONTWAIT;
3564 rxbuf = &rxr->rx_buffers[i];
3566 if ((clean & IGB_CLEAN_HEADER) != 0) {
3567 mh = m_gethdr(mbflags, MT_DATA);
3569 adapter->mbuf_header_failed++;
3572 mh->m_pkthdr.len = mh->m_len = MHLEN;
3574 * Because IGB_HDR_BUF size is less than MHLEN
3575 * and we configure controller to split headers
3576 * we can align mbuf on ETHER_ALIGN boundary.
3578 m_adj(mh, ETHER_ALIGN);
3579 error = bus_dmamap_load_mbuf_segment(rxr->rx_htag,
3580 rxr->rx_hspare_map, mh, hseg, 1, &nsegs, BUS_DMA_NOWAIT);
3585 mh->m_flags &= ~M_PKTHDR;
3587 if ((clean & IGB_CLEAN_PAYLOAD) != 0) {
3588 mp = m_getl(adapter->rx_mbuf_sz, mbflags, MT_DATA,
3591 mp = m_getjcl(MB_DONTWAIT, MT_DATA, M_PKTHDR,
3592 adapter->rx_mbuf_sz);
3596 adapter->mbuf_packet_failed++;
3597 bus_dmamap_unload(rxr->rx_htag,
3599 mh->m_flags |= M_PKTHDR;
3604 mp->m_pkthdr.len = mp->m_len = adapter->rx_mbuf_sz;
3605 error = bus_dmamap_load_mbuf_segment(rxr->rx_ptag,
3606 rxr->rx_pspare_map, mp, pseg, 1, &nsegs, BUS_DMA_NOWAIT);
3609 bus_dmamap_unload(rxr->rx_htag,
3611 mh->m_flags |= M_PKTHDR;
3617 mp->m_flags &= ~M_PKTHDR;
3620 /* Loading new DMA maps complete, unload maps for received buffers. */
3621 if ((clean & IGB_CLEAN_HEADER) != 0 && rxbuf->m_head != NULL) {
3622 bus_dmamap_sync(rxr->rx_htag, rxbuf->head_map,
3623 BUS_DMASYNC_POSTREAD);
3624 bus_dmamap_unload(rxr->rx_htag, rxbuf->head_map);
3626 if ((clean & IGB_CLEAN_PAYLOAD) != 0 && rxbuf->m_pack != NULL) {
3627 bus_dmamap_sync(rxr->rx_ptag, rxbuf->pack_map,
3628 BUS_DMASYNC_POSTREAD);
3629 bus_dmamap_unload(rxr->rx_ptag, rxbuf->pack_map);
3632 /* Reflect loaded dmamaps. */
3633 if ((clean & IGB_CLEAN_HEADER) != 0) {
3634 map = rxbuf->head_map;
3635 rxbuf->head_map = rxr->rx_hspare_map;
3636 rxr->rx_hspare_map = map;
3638 bus_dmamap_sync(rxr->rx_htag, rxbuf->head_map,
3639 BUS_DMASYNC_PREREAD);
3640 rxr->rx_base[i].read.hdr_addr = htole64(hseg[0].ds_addr);
3642 if ((clean & IGB_CLEAN_PAYLOAD) != 0) {
3643 map = rxbuf->pack_map;
3644 rxbuf->pack_map = rxr->rx_pspare_map;
3645 rxr->rx_pspare_map = map;
3647 bus_dmamap_sync(rxr->rx_ptag, rxbuf->pack_map,
3648 BUS_DMASYNC_PREREAD);
3649 rxr->rx_base[i].read.pkt_addr = htole64(pseg[0].ds_addr);
3655 /*********************************************************************
3657 * Allocate memory for rx_buffer structures. Since we use one
3658 * rx_buffer per received packet, the maximum number of rx_buffer's
3659 * that we'll need is equal to the number of receive descriptors
3660 * that we've allocated.
3662 **********************************************************************/
3664 igb_allocate_receive_buffers(struct rx_ring *rxr)
3666 struct adapter *adapter = rxr->adapter;
3667 device_t dev = adapter->dev;
3668 struct igb_rx_buf *rxbuf;
3669 int i, bsize, error;
3671 bsize = sizeof(struct igb_rx_buf) * adapter->num_rx_desc;
3672 if (!(rxr->rx_buffers =
3673 (struct igb_rx_buf *) kmalloc(bsize,
3674 M_DEVBUF, M_INTWAIT | M_ZERO))) {
3675 device_printf(dev, "Unable to allocate rx_buffer memory\n");
3680 if ((error = bus_dma_tag_create(NULL,
3681 1, 0, /* alignment, bounds */
3682 BUS_SPACE_MAXADDR, /* lowaddr */
3683 BUS_SPACE_MAXADDR, /* highaddr */
3684 NULL, NULL, /* filter, filterarg */
3685 MSIZE, /* maxsize */
3687 MSIZE, /* maxsegsize */
3690 device_printf(dev, "Unable to create RX DMA tag\n");
3694 if ((error = bus_dma_tag_create(NULL,
3695 1, 0, /* alignment, bounds */
3696 BUS_SPACE_MAXADDR, /* lowaddr */
3697 BUS_SPACE_MAXADDR, /* highaddr */
3698 NULL, NULL, /* filter, filterarg */
3699 MJUMPAGESIZE, /* maxsize */
3701 MJUMPAGESIZE, /* maxsegsize */
3704 device_printf(dev, "Unable to create RX payload DMA tag\n");
3708 /* Create the spare maps (used by getbuf) */
3709 error = bus_dmamap_create(rxr->rx_htag, BUS_DMA_NOWAIT,
3710 &rxr->rx_hspare_map);
3713 "%s: bus_dmamap_create header spare failed: %d\n",
3717 error = bus_dmamap_create(rxr->rx_ptag, BUS_DMA_NOWAIT,
3718 &rxr->rx_pspare_map);
3721 "%s: bus_dmamap_create packet spare failed: %d\n",
3726 for (i = 0; i < adapter->num_rx_desc; i++) {
3727 rxbuf = &rxr->rx_buffers[i];
3728 error = bus_dmamap_create(rxr->rx_htag,
3729 BUS_DMA_NOWAIT, &rxbuf->head_map);
3732 "Unable to create RX head DMA maps\n");
3735 error = bus_dmamap_create(rxr->rx_ptag,
3736 BUS_DMA_NOWAIT, &rxbuf->pack_map);
3739 "Unable to create RX packet DMA maps\n");
3747 /* Frees all, but can handle partial completion */
3748 igb_free_receive_structures(adapter);
3754 igb_free_receive_ring(struct rx_ring *rxr)
3756 struct adapter *adapter;
3757 struct igb_rx_buf *rxbuf;
3760 adapter = rxr->adapter;
3761 for (i = 0; i < adapter->num_rx_desc; i++) {
3762 rxbuf = &rxr->rx_buffers[i];
3763 if (rxbuf->m_head != NULL) {
3764 bus_dmamap_sync(rxr->rx_htag, rxbuf->head_map,
3765 BUS_DMASYNC_POSTREAD);
3766 bus_dmamap_unload(rxr->rx_htag, rxbuf->head_map);
3767 rxbuf->m_head->m_flags |= M_PKTHDR;
3768 m_freem(rxbuf->m_head);
3770 if (rxbuf->m_pack != NULL) {
3771 bus_dmamap_sync(rxr->rx_ptag, rxbuf->pack_map,
3772 BUS_DMASYNC_POSTREAD);
3773 bus_dmamap_unload(rxr->rx_ptag, rxbuf->pack_map);
3774 rxbuf->m_pack->m_flags |= M_PKTHDR;
3775 m_freem(rxbuf->m_pack);
3777 rxbuf->m_head = NULL;
3778 rxbuf->m_pack = NULL;
3783 /*********************************************************************
3785 * Initialize a receive ring and its buffers.
3787 **********************************************************************/
3789 igb_setup_receive_ring(struct rx_ring *rxr)
3791 struct adapter *adapter;
3795 struct lro_ctrl *lro = &rxr->lro;
3797 int j, rsize, error = 0;
3799 adapter = rxr->adapter;
3803 /* Clear the ring contents */
3805 rsize = roundup2(adapter->num_rx_desc *
3806 sizeof(union e1000_adv_rx_desc), IGB_DBA_ALIGN);
3807 bzero((void *)rxr->rx_base, rsize);
3810 ** Free current RX buffer structures and their mbufs
3812 igb_free_receive_ring(rxr);
3814 /* Now replenish the ring mbufs */
3815 for (j = 0; j < adapter->num_rx_desc; j++) {
3816 error = igb_get_buf(rxr, j, IGB_CLEAN_BOTH | IGB_CLEAN_INITIAL);
3821 /* Setup our descriptor indices */
3822 rxr->next_to_check = 0;
3823 rxr->last_cleaned = 0;
3824 rxr->lro_enabled = FALSE;
3826 if (igb_header_split)
3827 rxr->hdr_split = TRUE;
3830 ifp->if_capabilities &= ~IFCAP_LRO;
3835 rxr->discard = FALSE;
3837 bus_dmamap_sync(rxr->rxdma.dma_tag, rxr->rxdma.dma_map,
3838 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
3841 ** Now set up the LRO interface, we
3842 ** also only do head split when LRO
3843 ** is enabled, since so often they
3844 ** are undesireable in similar setups.
3847 if (ifp->if_capenable & IFCAP_LRO) {
3848 int err = tcp_lro_init(lro);
3850 device_printf(dev, "LRO Initialization failed!\n");
3853 INIT_DEBUGOUT("RX LRO Initialized\n");
3854 rxr->lro_enabled = TRUE;
3855 lro->ifp = adapter->ifp;
3863 igb_free_receive_ring(rxr);
3868 /*********************************************************************
3870 * Initialize all receive rings.
3872 **********************************************************************/
3874 igb_setup_receive_structures(struct adapter *adapter)
3876 struct rx_ring *rxr = adapter->rx_rings;
3879 for (i = 0; i < adapter->num_queues; i++, rxr++)
3880 if (igb_setup_receive_ring(rxr))
3886 * Free RX buffers allocated so far, we will only handle
3887 * the rings that completed, the failing case will have
3888 * cleaned up for itself. The value of 'i' will be the
3889 * failed ring so we must pre-decrement it.
3891 rxr = adapter->rx_rings;
3892 for (--i; i > 0; i--, rxr++) {
3893 for (j = 0; j < adapter->num_rx_desc; j++)
3894 igb_free_receive_ring(rxr);
3900 /*********************************************************************
3902 * Enable receive unit.
3904 **********************************************************************/
3906 igb_initialize_receive_units(struct adapter *adapter)
3908 struct rx_ring *rxr = adapter->rx_rings;
3909 struct ifnet *ifp = adapter->ifp;
3910 struct e1000_hw *hw = &adapter->hw;
3911 u32 rctl, rxcsum, psize, srrctl = 0;
3913 INIT_DEBUGOUT("igb_initialize_receive_unit: begin");
3916 * Make sure receives are disabled while setting
3917 * up the descriptor ring
3919 rctl = E1000_READ_REG(hw, E1000_RCTL);
3920 E1000_WRITE_REG(hw, E1000_RCTL, rctl & ~E1000_RCTL_EN);
3923 ** Set up for header split
3925 if (rxr->hdr_split) {
3926 /* Use a standard mbuf for the header */
3927 srrctl |= IGB_HDR_BUF << E1000_SRRCTL_BSIZEHDRSIZE_SHIFT;
3928 srrctl |= E1000_SRRCTL_DESCTYPE_HDR_SPLIT_ALWAYS;
3930 srrctl |= E1000_SRRCTL_DESCTYPE_ADV_ONEBUF;
3933 ** Set up for jumbo frames
3935 if (ifp->if_mtu > ETHERMTU) {
3936 rctl |= E1000_RCTL_LPE;
3937 srrctl |= 4096 >> E1000_SRRCTL_BSIZEPKT_SHIFT;
3938 rctl |= E1000_RCTL_SZ_4096 | E1000_RCTL_BSEX;
3940 /* Set maximum packet len */
3941 psize = adapter->max_frame_size;
3942 /* are we on a vlan? */
3943 if (adapter->ifp->if_vlantrunks != NULL)
3944 psize += VLAN_TAG_SIZE;
3945 E1000_WRITE_REG(&adapter->hw, E1000_RLPML, psize);
3947 rctl &= ~E1000_RCTL_LPE;
3948 srrctl |= 2048 >> E1000_SRRCTL_BSIZEPKT_SHIFT;
3949 rctl |= E1000_RCTL_SZ_2048;
3952 /* Setup the Base and Length of the Rx Descriptor Rings */
3953 for (int i = 0; i < adapter->num_queues; i++, rxr++) {
3954 u64 bus_addr = rxr->rxdma.dma_paddr;
3957 E1000_WRITE_REG(hw, E1000_RDLEN(i),
3958 adapter->num_rx_desc * sizeof(struct e1000_rx_desc));
3959 E1000_WRITE_REG(hw, E1000_RDBAH(i),
3960 (uint32_t)(bus_addr >> 32));
3961 E1000_WRITE_REG(hw, E1000_RDBAL(i),
3962 (uint32_t)bus_addr);
3963 E1000_WRITE_REG(hw, E1000_SRRCTL(i), srrctl);
3964 /* Enable this Queue */
3965 rxdctl = E1000_READ_REG(hw, E1000_RXDCTL(i));
3966 rxdctl |= E1000_RXDCTL_QUEUE_ENABLE;
3967 rxdctl &= 0xFFF00000;
3968 rxdctl |= IGB_RX_PTHRESH;
3969 rxdctl |= IGB_RX_HTHRESH << 8;
3970 rxdctl |= IGB_RX_WTHRESH << 16;
3971 E1000_WRITE_REG(hw, E1000_RXDCTL(i), rxdctl);
3975 ** Setup for RX MultiQueue
3977 rxcsum = E1000_READ_REG(hw, E1000_RXCSUM);
3978 if (adapter->num_queues >1) {
3979 u32 random[10], mrqc, shift = 0;
3985 karc4rand(&random, sizeof(random));
3986 if (adapter->hw.mac.type == e1000_82575)
3988 /* Warning FM follows */
3989 for (int i = 0; i < 128; i++) {
3991 (i % adapter->num_queues) << shift;
3994 E1000_RETA(i >> 2), reta.dword);
3996 /* Now fill in hash table */
3997 mrqc = E1000_MRQC_ENABLE_RSS_4Q;
3998 for (int i = 0; i < 10; i++)
3999 E1000_WRITE_REG_ARRAY(hw,
4000 E1000_RSSRK(0), i, random[i]);
4002 mrqc |= (E1000_MRQC_RSS_FIELD_IPV4 |
4003 E1000_MRQC_RSS_FIELD_IPV4_TCP);
4004 mrqc |= (E1000_MRQC_RSS_FIELD_IPV6 |
4005 E1000_MRQC_RSS_FIELD_IPV6_TCP);
4006 mrqc |=( E1000_MRQC_RSS_FIELD_IPV4_UDP |
4007 E1000_MRQC_RSS_FIELD_IPV6_UDP);
4008 mrqc |=( E1000_MRQC_RSS_FIELD_IPV6_UDP_EX |
4009 E1000_MRQC_RSS_FIELD_IPV6_TCP_EX);
4011 E1000_WRITE_REG(hw, E1000_MRQC, mrqc);
4014 ** NOTE: Receive Full-Packet Checksum Offload
4015 ** is mutually exclusive with Multiqueue. However
4016 ** this is not the same as TCP/IP checksums which
4019 rxcsum |= E1000_RXCSUM_PCSD;
4020 #if __FreeBSD_version >= 800000
4021 /* For SCTP Offload */
4022 if ((hw->mac.type == e1000_82576)
4023 && (ifp->if_capenable & IFCAP_RXCSUM))
4024 rxcsum |= E1000_RXCSUM_CRCOFL;
4028 if (ifp->if_capenable & IFCAP_RXCSUM) {
4029 rxcsum |= E1000_RXCSUM_IPPCSE;
4030 #if __FreeBSD_version >= 800000
4031 if (adapter->hw.mac.type == e1000_82576)
4032 rxcsum |= E1000_RXCSUM_CRCOFL;
4035 rxcsum &= ~E1000_RXCSUM_TUOFL;
4037 E1000_WRITE_REG(hw, E1000_RXCSUM, rxcsum);
4039 /* Setup the Receive Control Register */
4040 rctl &= ~(3 << E1000_RCTL_MO_SHIFT);
4041 rctl |= E1000_RCTL_EN | E1000_RCTL_BAM | E1000_RCTL_LBM_NO |
4042 E1000_RCTL_RDMTS_HALF |
4043 (hw->mac.mc_filter_type << E1000_RCTL_MO_SHIFT);
4044 /* Strip CRC bytes. */
4045 rctl |= E1000_RCTL_SECRC;
4046 /* Make sure VLAN Filters are off */
4047 rctl &= ~E1000_RCTL_VFE;
4048 /* Don't store bad packets */
4049 rctl &= ~E1000_RCTL_SBP;
4051 /* Enable Receives */
4052 E1000_WRITE_REG(hw, E1000_RCTL, rctl);
4055 * Setup the HW Rx Head and Tail Descriptor Pointers
4056 * - needs to be after enable
4058 for (int i = 0; i < adapter->num_queues; i++) {
4059 E1000_WRITE_REG(hw, E1000_RDH(i), 0);
4060 E1000_WRITE_REG(hw, E1000_RDT(i),
4061 adapter->num_rx_desc - 1);
4066 /*********************************************************************
4068 * Free receive rings.
4070 **********************************************************************/
4072 igb_free_receive_structures(struct adapter *adapter)
4074 struct rx_ring *rxr = adapter->rx_rings;
4076 for (int i = 0; i < adapter->num_queues; i++, rxr++) {
4078 struct lro_ctrl *lro = &rxr->lro;
4081 igb_free_receive_buffers(rxr);
4085 igb_dma_free(adapter, &rxr->rxdma);
4087 IGB_RX_LOCK_DESTROY(rxr);
4090 kfree(adapter->rx_rings, M_DEVBUF);
4093 /*********************************************************************
4095 * Free receive ring data structures.
4097 **********************************************************************/
4099 igb_free_receive_buffers(struct rx_ring *rxr)
4101 struct adapter *adapter = rxr->adapter;
4102 struct igb_rx_buf *rxbuf;
4105 INIT_DEBUGOUT("free_receive_structures: begin");
4107 if (rxr->rx_hspare_map != NULL) {
4108 bus_dmamap_destroy(rxr->rx_htag, rxr->rx_hspare_map);
4109 rxr->rx_hspare_map = NULL;
4112 if (rxr->rx_hspare_map != NULL) {
4113 bus_dmamap_destroy(rxr->rx_ptag, rxr->rx_pspare_map);
4114 rxr->rx_pspare_map = NULL;
4117 /* Cleanup any existing buffers */
4118 if (rxr->rx_buffers != NULL) {
4119 for (i = 0; i < adapter->num_rx_desc; i++) {
4120 rxbuf = &rxr->rx_buffers[i];
4121 if (rxbuf->m_head != NULL) {
4122 bus_dmamap_sync(rxr->rx_htag, rxbuf->head_map,
4123 BUS_DMASYNC_POSTREAD);
4124 bus_dmamap_unload(rxr->rx_htag,
4126 rxbuf->m_head->m_flags |= M_PKTHDR;
4127 m_freem(rxbuf->m_head);
4129 if (rxbuf->m_pack != NULL) {
4130 bus_dmamap_sync(rxr->rx_ptag, rxbuf->pack_map,
4131 BUS_DMASYNC_POSTREAD);
4132 bus_dmamap_unload(rxr->rx_ptag,
4134 rxbuf->m_pack->m_flags |= M_PKTHDR;
4135 m_freem(rxbuf->m_pack);
4137 rxbuf->m_head = NULL;
4138 rxbuf->m_pack = NULL;
4139 if (rxbuf->head_map != NULL) {
4140 bus_dmamap_destroy(rxr->rx_htag,
4142 rxbuf->head_map = NULL;
4144 if (rxbuf->pack_map != NULL) {
4145 bus_dmamap_destroy(rxr->rx_ptag,
4147 rxbuf->pack_map = NULL;
4150 if (rxr->rx_buffers != NULL) {
4151 kfree(rxr->rx_buffers, M_DEVBUF);
4152 rxr->rx_buffers = NULL;
4156 if (rxr->rx_htag != NULL) {
4157 bus_dma_tag_destroy(rxr->rx_htag);
4158 rxr->rx_htag = NULL;
4160 if (rxr->rx_ptag != NULL) {
4161 bus_dma_tag_destroy(rxr->rx_ptag);
4162 rxr->rx_ptag = NULL;
4166 static __inline void
4167 igb_rx_discard(struct rx_ring *rxr, union e1000_adv_rx_desc *cur, int i)
4170 if (rxr->fmp != NULL) {
4171 rxr->fmp->m_flags |= M_PKTHDR;
4178 static __inline void
4179 igb_rx_input(struct rx_ring *rxr, struct ifnet *ifp, struct mbuf *m, u32 ptype)
4183 * ATM LRO is only for IPv4/TCP packets and TCP checksum of the packet
4184 * should be computed by hardware. Also it should not have VLAN tag in
4188 if (rxr->lro_enabled &&
4189 (ifp->if_capenable & IFCAP_VLAN_HWTAGGING) != 0 &&
4190 (ptype & E1000_RXDADV_PKTTYPE_ETQF) == 0 &&
4191 (ptype & (E1000_RXDADV_PKTTYPE_IPV4 | E1000_RXDADV_PKTTYPE_TCP)) ==
4192 (E1000_RXDADV_PKTTYPE_IPV4 | E1000_RXDADV_PKTTYPE_TCP) &&
4193 (m->m_pkthdr.csum_flags & (CSUM_DATA_VALID | CSUM_PSEUDO_HDR)) ==
4194 (CSUM_DATA_VALID | CSUM_PSEUDO_HDR)) {
4196 * Send to the stack if:
4197 ** - LRO not enabled, or
4198 ** - no LRO resources, or
4199 ** - lro enqueue fails
4201 if (rxr->lro.lro_cnt != 0)
4202 if (tcp_lro_rx(&rxr->lro, m, 0) == 0)
4206 (*ifp->if_input)(ifp, m);
4209 /*********************************************************************
4211 * This routine executes in interrupt context. It replenishes
4212 * the mbufs in the descriptor and sends data which has been
4213 * dma'ed into host memory to upper layer.
4215 * We loop at most count times if count is > 0, or until done if
4218 * Return TRUE if more to clean, FALSE otherwise
4219 *********************************************************************/
4221 igb_rxeof(struct rx_ring *rxr, int count)
4223 struct adapter *adapter = rxr->adapter;
4224 struct ifnet *ifp = adapter->ifp;
4226 struct lro_ctrl *lro = &rxr->lro;
4227 struct lro_entry *queued;
4230 u32 ptype, staterr = 0;
4231 union e1000_adv_rx_desc *cur;
4235 /* Main clean loop */
4236 for (i = rxr->next_to_check; count > 0; prog++) {
4237 struct mbuf *sendmp, *mh, *mp;
4238 u16 hlen, plen, hdr, vtag;
4242 /* Sync the ring. */
4243 bus_dmamap_sync(rxr->rxdma.dma_tag, rxr->rxdma.dma_map,
4244 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
4245 cur = &rxr->rx_base[i];
4246 staterr = le32toh(cur->wb.upper.status_error);
4247 if ((staterr & E1000_RXD_STAT_DD) == 0)
4249 if ((ifp->if_flags & IFF_RUNNING) == 0)
4252 sendmp = mh = mp = NULL;
4253 cur->wb.upper.status_error = 0;
4254 plen = le16toh(cur->wb.upper.length);
4255 ptype = le32toh(cur->wb.lower.lo_dword.data) & IGB_PKTTYPE_MASK;
4256 hdr = le16toh(cur->wb.lower.lo_dword.hs_rss.hdr_info);
4257 eop = ((staterr & E1000_RXD_STAT_EOP) == E1000_RXD_STAT_EOP);
4259 /* Make sure all segments of a bad packet are discarded */
4260 if (((staterr & E1000_RXDEXT_ERR_FRAME_ERR_MASK) != 0) ||
4263 ++rxr->rx_discarded;
4264 if (!eop) /* Catch subsequent segs */
4265 rxr->discard = TRUE;
4267 rxr->discard = FALSE;
4268 igb_rx_discard(rxr, cur, i);
4273 ** The way the hardware is configured to
4274 ** split, it will ONLY use the header buffer
4275 ** when header split is enabled, otherwise we
4276 ** get normal behavior, ie, both header and
4277 ** payload are DMA'd into the payload buffer.
4279 ** The fmp test is to catch the case where a
4280 ** packet spans multiple descriptors, in that
4281 ** case only the first header is valid.
4283 if (rxr->hdr_split && rxr->fmp == NULL) {
4284 hlen = (hdr & E1000_RXDADV_HDRBUFLEN_MASK) >>
4285 E1000_RXDADV_HDRBUFLEN_SHIFT;
4286 if (hlen > IGB_HDR_BUF)
4288 /* Handle the header mbuf */
4289 mh = rxr->rx_buffers[i].m_head;
4291 dopayload = IGB_CLEAN_HEADER;
4293 ** Get the payload length, this
4294 ** could be zero if its a small
4298 mp = rxr->rx_buffers[i].m_pack;
4301 dopayload = IGB_CLEAN_BOTH;
4302 rxr->rx_split_packets++;
4306 ** Either no header split, or a
4307 ** secondary piece of a fragmented
4310 mh = rxr->rx_buffers[i].m_pack;
4312 dopayload = IGB_CLEAN_PAYLOAD;
4316 ** get_buf will overwrite the writeback
4317 ** descriptor so save the VLAN tag now.
4319 vtag = le16toh(cur->wb.upper.vlan);
4320 if (igb_get_buf(rxr, i, dopayload) != 0) {
4323 * We've dropped a frame due to lack of resources
4324 * so we should drop entire multi-segmented
4325 * frames until we encounter EOP.
4327 if ((staterr & E1000_RXD_STAT_EOP) != 0)
4328 rxr->discard = TRUE;
4329 igb_rx_discard(rxr, cur, i);
4333 /* Initial frame - setup */
4334 if (rxr->fmp == NULL) {
4335 mh->m_pkthdr.len = mh->m_len;
4336 /* Store the first mbuf */
4340 /* Add payload if split */
4341 mh->m_pkthdr.len += mp->m_len;
4342 rxr->lmp = mh->m_next;
4345 /* Chain mbuf's together */
4346 rxr->lmp->m_next = mh;
4347 rxr->lmp = rxr->lmp->m_next;
4348 rxr->fmp->m_pkthdr.len += mh->m_len;
4352 rxr->fmp->m_pkthdr.rcvif = ifp;
4355 /* capture data for AIM */
4357 rxr->bytes += rxr->fmp->m_pkthdr.len;
4358 rxr->rx_bytes += rxr->fmp->m_pkthdr.len;
4360 if ((ifp->if_capenable & IFCAP_RXCSUM) != 0)
4361 igb_rx_checksum(staterr, rxr->fmp, ptype);
4362 /* XXX igb(4) always strips VLAN. */
4363 if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) != 0 &&
4364 (staterr & E1000_RXD_STAT_VP) != 0) {
4365 rxr->fmp->m_pkthdr.ether_vlantag = vtag;
4366 rxr->fmp->m_flags |= M_VLANTAG;
4368 #if __FreeBSD_version >= 800000
4369 rxr->fmp->m_pkthdr.flowid = curcpu;
4370 rxr->fmp->m_flags |= M_FLOWID;
4373 /* Make sure to set M_PKTHDR. */
4374 sendmp->m_flags |= M_PKTHDR;
4380 bus_dmamap_sync(rxr->rxdma.dma_tag, rxr->rxdma.dma_map,
4381 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
4383 rxr->last_cleaned = i; /* For updating tail */
4385 /* Advance our pointers to the next descriptor. */
4386 if (++i == adapter->num_rx_desc)
4390 ** Note that we hold the RX lock thru
4391 ** the following call so this ring's
4392 ** next_to_check is not gonna change.
4395 igb_rx_input(rxr, ifp, sendmp, ptype);
4403 rxr->next_to_check = i;
4405 /* Advance the E1000's Receive Queue "Tail Pointer". */
4406 E1000_WRITE_REG(&adapter->hw, E1000_RDT(rxr->me), rxr->last_cleaned);
4409 * Flush any outstanding LRO work
4412 while ((queued = SLIST_FIRST(&lro->lro_active)) != NULL) {
4413 SLIST_REMOVE_HEAD(&lro->lro_active, next);
4414 tcp_lro_flush(lro, queued);
4421 ** We still have cleaning to do?
4422 ** Schedule another interrupt if so.
4424 if ((staterr & E1000_RXD_STAT_DD) != 0)
4430 /*********************************************************************
4432 * Verify that the hardware indicated that the checksum is valid.
4433 * Inform the stack about the status of checksum so that stack
4434 * doesn't spend time verifying the checksum.
4436 *********************************************************************/
4438 igb_rx_checksum(u32 staterr, struct mbuf *mp, u32 ptype)
4440 u16 status = (u16)staterr;
4441 u8 errors = (u8) (staterr >> 24);
4444 /* Ignore Checksum bit is set */
4445 if (status & E1000_RXD_STAT_IXSM) {
4446 mp->m_pkthdr.csum_flags = 0;
4450 if ((ptype & E1000_RXDADV_PKTTYPE_ETQF) == 0 &&
4451 (ptype & E1000_RXDADV_PKTTYPE_SCTP) != 0)
4455 if (status & E1000_RXD_STAT_IPCS) {
4457 if (!(errors & E1000_RXD_ERR_IPE)) {
4458 /* IP Checksum Good */
4459 mp->m_pkthdr.csum_flags = CSUM_IP_CHECKED;
4460 mp->m_pkthdr.csum_flags |= CSUM_IP_VALID;
4462 mp->m_pkthdr.csum_flags = 0;
4465 if (status & (E1000_RXD_STAT_TCPCS | E1000_RXD_STAT_UDPCS)) {
4466 u16 type = (CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
4467 #if __FreeBSD_version >= 800000
4468 if (sctp) /* reassign */
4469 type = CSUM_SCTP_VALID;
4472 if (!(errors & E1000_RXD_ERR_TCPE)) {
4473 mp->m_pkthdr.csum_flags |= type;
4475 mp->m_pkthdr.csum_data = htons(0xffff);
4482 * This routine is run via an vlan
4486 igb_register_vlan(void *arg, struct ifnet *ifp, u16 vtag)
4488 struct adapter *adapter = ifp->if_softc;
4491 if (ifp->if_softc != arg) /* Not our event */
4494 if ((vtag == 0) || (vtag > 4095)) /* Invalid */
4497 index = (vtag >> 5) & 0x7F;
4499 igb_shadow_vfta[index] |= (1 << bit);
4500 ++adapter->num_vlans;
4501 /* Re-init to load the changes */
4506 * This routine is run via an vlan
4510 igb_unregister_vlan(void *arg, struct ifnet *ifp, u16 vtag)
4512 struct adapter *adapter = ifp->if_softc;
4515 if (ifp->if_softc != arg)
4518 if ((vtag == 0) || (vtag > 4095)) /* Invalid */
4521 index = (vtag >> 5) & 0x7F;
4523 igb_shadow_vfta[index] &= ~(1 << bit);
4524 --adapter->num_vlans;
4525 /* Re-init to load the changes */
4530 igb_setup_vlan_hw_support(struct adapter *adapter)
4532 struct e1000_hw *hw = &adapter->hw;
4536 ** We get here thru init_locked, meaning
4537 ** a soft reset, this has already cleared
4538 ** the VFTA and other state, so if there
4539 ** have been no vlan's registered do nothing.
4541 if (adapter->num_vlans == 0)
4545 ** A soft reset zero's out the VFTA, so
4546 ** we need to repopulate it now.
4548 for (int i = 0; i < IGB_VFTA_SIZE; i++)
4549 if (igb_shadow_vfta[i] != 0)
4550 E1000_WRITE_REG_ARRAY(hw, E1000_VFTA,
4551 i, igb_shadow_vfta[i]);
4553 reg = E1000_READ_REG(hw, E1000_CTRL);
4554 reg |= E1000_CTRL_VME;
4555 E1000_WRITE_REG(hw, E1000_CTRL, reg);
4557 /* Enable the Filter Table */
4558 reg = E1000_READ_REG(hw, E1000_RCTL);
4559 reg &= ~E1000_RCTL_CFIEN;
4560 reg |= E1000_RCTL_VFE;
4561 E1000_WRITE_REG(hw, E1000_RCTL, reg);
4563 /* Update the frame size */
4564 E1000_WRITE_REG(&adapter->hw, E1000_RLPML,
4565 adapter->max_frame_size + VLAN_TAG_SIZE);
4569 igb_enable_intr(struct adapter *adapter)
4571 /* With RSS set up what to auto clear */
4572 if (adapter->msix_mem) {
4573 E1000_WRITE_REG(&adapter->hw, E1000_EIAC,
4574 adapter->eims_mask);
4575 E1000_WRITE_REG(&adapter->hw, E1000_EIAM,
4576 adapter->eims_mask);
4577 E1000_WRITE_REG(&adapter->hw, E1000_EIMS,
4578 adapter->eims_mask);
4579 E1000_WRITE_REG(&adapter->hw, E1000_IMS,
4582 E1000_WRITE_REG(&adapter->hw, E1000_IMS,
4585 E1000_WRITE_FLUSH(&adapter->hw);
4591 igb_disable_intr(struct adapter *adapter)
4593 if (adapter->msix_mem) {
4594 E1000_WRITE_REG(&adapter->hw, E1000_EIMC, ~0);
4595 E1000_WRITE_REG(&adapter->hw, E1000_EIAC, 0);
4597 E1000_WRITE_REG(&adapter->hw, E1000_IMC, ~0);
4598 E1000_WRITE_FLUSH(&adapter->hw);
4603 * Bit of a misnomer, what this really means is
4604 * to enable OS management of the system... aka
4605 * to disable special hardware management features
4608 igb_init_manageability(struct adapter *adapter)
4610 if (adapter->has_manage) {
4611 int manc2h = E1000_READ_REG(&adapter->hw, E1000_MANC2H);
4612 int manc = E1000_READ_REG(&adapter->hw, E1000_MANC);
4614 /* disable hardware interception of ARP */
4615 manc &= ~(E1000_MANC_ARP_EN);
4617 /* enable receiving management packets to the host */
4618 manc |= E1000_MANC_EN_MNG2HOST;
4619 manc2h |= 1 << 5; /* Mng Port 623 */
4620 manc2h |= 1 << 6; /* Mng Port 664 */
4621 E1000_WRITE_REG(&adapter->hw, E1000_MANC2H, manc2h);
4622 E1000_WRITE_REG(&adapter->hw, E1000_MANC, manc);
4627 * Give control back to hardware management
4628 * controller if there is one.
4631 igb_release_manageability(struct adapter *adapter)
4633 if (adapter->has_manage) {
4634 int manc = E1000_READ_REG(&adapter->hw, E1000_MANC);
4636 /* re-enable hardware interception of ARP */
4637 manc |= E1000_MANC_ARP_EN;
4638 manc &= ~E1000_MANC_EN_MNG2HOST;
4640 E1000_WRITE_REG(&adapter->hw, E1000_MANC, manc);
4645 * igb_get_hw_control sets CTRL_EXT:DRV_LOAD bit.
4646 * For ASF and Pass Through versions of f/w this means that
4647 * the driver is loaded.
4651 igb_get_hw_control(struct adapter *adapter)
4655 /* Let firmware know the driver has taken over */
4656 ctrl_ext = E1000_READ_REG(&adapter->hw, E1000_CTRL_EXT);
4657 E1000_WRITE_REG(&adapter->hw, E1000_CTRL_EXT,
4658 ctrl_ext | E1000_CTRL_EXT_DRV_LOAD);
4662 * igb_release_hw_control resets CTRL_EXT:DRV_LOAD bit.
4663 * For ASF and Pass Through versions of f/w this means that the
4664 * driver is no longer loaded.
4668 igb_release_hw_control(struct adapter *adapter)
4672 /* Let firmware taken over control of h/w */
4673 ctrl_ext = E1000_READ_REG(&adapter->hw, E1000_CTRL_EXT);
4674 E1000_WRITE_REG(&adapter->hw, E1000_CTRL_EXT,
4675 ctrl_ext & ~E1000_CTRL_EXT_DRV_LOAD);
4679 igb_is_valid_ether_addr(uint8_t *addr)
4681 char zero_addr[6] = { 0, 0, 0, 0, 0, 0 };
4683 if ((addr[0] & 1) || (!bcmp(addr, zero_addr, ETHER_ADDR_LEN))) {
4692 * Enable PCI Wake On Lan capability
4695 igb_enable_wakeup(device_t dev)
4700 /* First find the capabilities pointer*/
4701 cap = pci_read_config(dev, PCIR_CAP_PTR, 2);
4702 /* Read the PM Capabilities */
4703 id = pci_read_config(dev, cap, 1);
4704 if (id != PCIY_PMG) /* Something wrong */
4706 /* OK, we have the power capabilities, so
4707 now get the status register */
4708 cap += PCIR_POWER_STATUS;
4709 status = pci_read_config(dev, cap, 2);
4710 status |= PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE;
4711 pci_write_config(dev, cap, status, 2);
4716 /**********************************************************************
4718 * Update the board statistics counters.
4720 **********************************************************************/
4722 igb_update_stats_counters(struct adapter *adapter)
4726 if(adapter->hw.phy.media_type == e1000_media_type_copper ||
4727 (E1000_READ_REG(&adapter->hw, E1000_STATUS) & E1000_STATUS_LU)) {
4728 adapter->stats.symerrs += E1000_READ_REG(&adapter->hw, E1000_SYMERRS);
4729 adapter->stats.sec += E1000_READ_REG(&adapter->hw, E1000_SEC);
4731 adapter->stats.crcerrs += E1000_READ_REG(&adapter->hw, E1000_CRCERRS);
4732 adapter->stats.mpc += E1000_READ_REG(&adapter->hw, E1000_MPC);
4733 adapter->stats.scc += E1000_READ_REG(&adapter->hw, E1000_SCC);
4734 adapter->stats.ecol += E1000_READ_REG(&adapter->hw, E1000_ECOL);
4736 adapter->stats.mcc += E1000_READ_REG(&adapter->hw, E1000_MCC);
4737 adapter->stats.latecol += E1000_READ_REG(&adapter->hw, E1000_LATECOL);
4738 adapter->stats.colc += E1000_READ_REG(&adapter->hw, E1000_COLC);
4739 adapter->stats.dc += E1000_READ_REG(&adapter->hw, E1000_DC);
4740 adapter->stats.rlec += E1000_READ_REG(&adapter->hw, E1000_RLEC);
4741 adapter->stats.xonrxc += E1000_READ_REG(&adapter->hw, E1000_XONRXC);
4742 adapter->stats.xontxc += E1000_READ_REG(&adapter->hw, E1000_XONTXC);
4743 adapter->stats.xoffrxc += E1000_READ_REG(&adapter->hw, E1000_XOFFRXC);
4744 adapter->stats.xofftxc += E1000_READ_REG(&adapter->hw, E1000_XOFFTXC);
4745 adapter->stats.fcruc += E1000_READ_REG(&adapter->hw, E1000_FCRUC);
4746 adapter->stats.prc64 += E1000_READ_REG(&adapter->hw, E1000_PRC64);
4747 adapter->stats.prc127 += E1000_READ_REG(&adapter->hw, E1000_PRC127);
4748 adapter->stats.prc255 += E1000_READ_REG(&adapter->hw, E1000_PRC255);
4749 adapter->stats.prc511 += E1000_READ_REG(&adapter->hw, E1000_PRC511);
4750 adapter->stats.prc1023 += E1000_READ_REG(&adapter->hw, E1000_PRC1023);
4751 adapter->stats.prc1522 += E1000_READ_REG(&adapter->hw, E1000_PRC1522);
4752 adapter->stats.gprc += E1000_READ_REG(&adapter->hw, E1000_GPRC);
4753 adapter->stats.bprc += E1000_READ_REG(&adapter->hw, E1000_BPRC);
4754 adapter->stats.mprc += E1000_READ_REG(&adapter->hw, E1000_MPRC);
4755 adapter->stats.gptc += E1000_READ_REG(&adapter->hw, E1000_GPTC);
4757 /* For the 64-bit byte counters the low dword must be read first. */
4758 /* Both registers clear on the read of the high dword */
4760 adapter->stats.gorc += E1000_READ_REG(&adapter->hw, E1000_GORCH);
4761 adapter->stats.gotc += E1000_READ_REG(&adapter->hw, E1000_GOTCH);
4763 adapter->stats.rnbc += E1000_READ_REG(&adapter->hw, E1000_RNBC);
4764 adapter->stats.ruc += E1000_READ_REG(&adapter->hw, E1000_RUC);
4765 adapter->stats.rfc += E1000_READ_REG(&adapter->hw, E1000_RFC);
4766 adapter->stats.roc += E1000_READ_REG(&adapter->hw, E1000_ROC);
4767 adapter->stats.rjc += E1000_READ_REG(&adapter->hw, E1000_RJC);
4769 adapter->stats.tor += E1000_READ_REG(&adapter->hw, E1000_TORH);
4770 adapter->stats.tot += E1000_READ_REG(&adapter->hw, E1000_TOTH);
4772 adapter->stats.tpr += E1000_READ_REG(&adapter->hw, E1000_TPR);
4773 adapter->stats.tpt += E1000_READ_REG(&adapter->hw, E1000_TPT);
4774 adapter->stats.ptc64 += E1000_READ_REG(&adapter->hw, E1000_PTC64);
4775 adapter->stats.ptc127 += E1000_READ_REG(&adapter->hw, E1000_PTC127);
4776 adapter->stats.ptc255 += E1000_READ_REG(&adapter->hw, E1000_PTC255);
4777 adapter->stats.ptc511 += E1000_READ_REG(&adapter->hw, E1000_PTC511);
4778 adapter->stats.ptc1023 += E1000_READ_REG(&adapter->hw, E1000_PTC1023);
4779 adapter->stats.ptc1522 += E1000_READ_REG(&adapter->hw, E1000_PTC1522);
4780 adapter->stats.mptc += E1000_READ_REG(&adapter->hw, E1000_MPTC);
4781 adapter->stats.bptc += E1000_READ_REG(&adapter->hw, E1000_BPTC);
4783 adapter->stats.algnerrc +=
4784 E1000_READ_REG(&adapter->hw, E1000_ALGNERRC);
4785 adapter->stats.rxerrc +=
4786 E1000_READ_REG(&adapter->hw, E1000_RXERRC);
4787 adapter->stats.tncrs +=
4788 E1000_READ_REG(&adapter->hw, E1000_TNCRS);
4789 adapter->stats.cexterr +=
4790 E1000_READ_REG(&adapter->hw, E1000_CEXTERR);
4791 adapter->stats.tsctc +=
4792 E1000_READ_REG(&adapter->hw, E1000_TSCTC);
4793 adapter->stats.tsctfc +=
4794 E1000_READ_REG(&adapter->hw, E1000_TSCTFC);
4797 ifp->if_collisions = adapter->stats.colc;
4800 ifp->if_ierrors = adapter->dropped_pkts + adapter->stats.rxerrc +
4801 adapter->stats.crcerrs + adapter->stats.algnerrc +
4802 adapter->stats.ruc + adapter->stats.roc +
4803 adapter->stats.mpc + adapter->stats.cexterr;
4806 ifp->if_oerrors = adapter->stats.ecol +
4807 adapter->stats.latecol + adapter->watchdog_events;
4811 /**********************************************************************
4813 * This routine is called only when igb_display_debug_stats is enabled.
4814 * This routine provides a way to take a look at important statistics
4815 * maintained by the driver and hardware.
4817 **********************************************************************/
4819 igb_print_debug_info(struct adapter *adapter)
4821 device_t dev = adapter->dev;
4822 struct igb_queue *que = adapter->queues;
4823 struct rx_ring *rxr = adapter->rx_rings;
4824 struct tx_ring *txr = adapter->tx_rings;
4825 uint8_t *hw_addr = adapter->hw.hw_addr;
4827 device_printf(dev, "Adapter hardware address = %p \n", hw_addr);
4828 device_printf(dev, "CTRL = 0x%x RCTL = 0x%x \n",
4829 E1000_READ_REG(&adapter->hw, E1000_CTRL),
4830 E1000_READ_REG(&adapter->hw, E1000_RCTL));
4832 #if (DEBUG_HW > 0) /* Dont output these errors normally */
4833 device_printf(dev, "IMS = 0x%x EIMS = 0x%x \n",
4834 E1000_READ_REG(&adapter->hw, E1000_IMS),
4835 E1000_READ_REG(&adapter->hw, E1000_EIMS));
4838 device_printf(dev, "Packet buffer = Tx=%dk Rx=%dk \n",
4839 ((E1000_READ_REG(&adapter->hw, E1000_PBA) & 0xffff0000) >> 16),\
4840 (E1000_READ_REG(&adapter->hw, E1000_PBA) & 0xffff) );
4841 device_printf(dev, "Flow control watermarks high = %d low = %d\n",
4842 adapter->hw.fc.high_water,
4843 adapter->hw.fc.low_water);
4845 for (int i = 0; i < adapter->num_queues; i++, rxr++, txr++) {
4846 device_printf(dev, "Queue(%d) tdh = %d, tdt = %d ", i,
4847 E1000_READ_REG(&adapter->hw, E1000_TDH(i)),
4848 E1000_READ_REG(&adapter->hw, E1000_TDT(i)));
4849 device_printf(dev, "rdh = %d, rdt = %d\n",
4850 E1000_READ_REG(&adapter->hw, E1000_RDH(i)),
4851 E1000_READ_REG(&adapter->hw, E1000_RDT(i)));
4852 device_printf(dev, "TX(%d) no descriptors avail event = %lld\n",
4853 txr->me, (long long)txr->no_desc_avail);
4854 device_printf(dev, "TX(%d) Packets sent = %lld\n",
4855 txr->me, (long long)txr->tx_packets);
4856 device_printf(dev, "RX(%d) Packets received = %lld ",
4857 rxr->me, (long long)rxr->rx_packets);
4860 for (int i = 0; i < adapter->num_queues; i++, rxr++) {
4862 struct lro_ctrl *lro = &rxr->lro;
4864 device_printf(dev, "Queue(%d) rdh = %d, rdt = %d\n", i,
4865 E1000_READ_REG(&adapter->hw, E1000_RDH(i)),
4866 E1000_READ_REG(&adapter->hw, E1000_RDT(i)));
4867 device_printf(dev, "RX(%d) Packets received = %lld\n", rxr->me,
4868 (long long)rxr->rx_packets);
4869 device_printf(dev, " Split Packets = %lld ",
4870 (long long)rxr->rx_split_packets);
4871 device_printf(dev, " Byte count = %lld\n",
4872 (long long)rxr->rx_bytes);
4874 device_printf(dev,"RX(%d) LRO Queued= %d ",
4875 i, lro->lro_queued);
4876 device_printf(dev,"LRO Flushed= %d\n",lro->lro_flushed);
4880 for (int i = 0; i < adapter->num_queues; i++, que++)
4881 device_printf(dev,"QUE(%d) IRQs = %llx\n",
4882 i, (long long)que->irqs);
4884 device_printf(dev, "LINK MSIX IRQ Handled = %u\n", adapter->link_irq);
4885 device_printf(dev, "Mbuf defrag failed = %ld\n",
4886 adapter->mbuf_defrag_failed);
4887 device_printf(dev, "Std mbuf header failed = %ld\n",
4888 adapter->mbuf_header_failed);
4889 device_printf(dev, "Std mbuf packet failed = %ld\n",
4890 adapter->mbuf_packet_failed);
4891 device_printf(dev, "Driver dropped packets = %ld\n",
4892 adapter->dropped_pkts);
4893 device_printf(dev, "Driver tx dma failure in xmit = %ld\n",
4894 adapter->no_tx_dma_setup);
4898 igb_print_hw_stats(struct adapter *adapter)
4900 device_t dev = adapter->dev;
4902 device_printf(dev, "Excessive collisions = %lld\n",
4903 (long long)adapter->stats.ecol);
4904 #if (DEBUG_HW > 0) /* Dont output these errors normally */
4905 device_printf(dev, "Symbol errors = %lld\n",
4906 (long long)adapter->stats.symerrs);
4908 device_printf(dev, "Sequence errors = %lld\n",
4909 (long long)adapter->stats.sec);
4910 device_printf(dev, "Defer count = %lld\n",
4911 (long long)adapter->stats.dc);
4912 device_printf(dev, "Missed Packets = %lld\n",
4913 (long long)adapter->stats.mpc);
4914 device_printf(dev, "Receive No Buffers = %lld\n",
4915 (long long)adapter->stats.rnbc);
4916 /* RLEC is inaccurate on some hardware, calculate our own. */
4917 device_printf(dev, "Receive Length Errors = %lld\n",
4918 ((long long)adapter->stats.roc + (long long)adapter->stats.ruc));
4919 device_printf(dev, "Receive errors = %lld\n",
4920 (long long)adapter->stats.rxerrc);
4921 device_printf(dev, "Crc errors = %lld\n",
4922 (long long)adapter->stats.crcerrs);
4923 device_printf(dev, "Alignment errors = %lld\n",
4924 (long long)adapter->stats.algnerrc);
4925 /* On 82575 these are collision counts */
4926 device_printf(dev, "Collision/Carrier extension errors = %lld\n",
4927 (long long)adapter->stats.cexterr);
4928 device_printf(dev, "RX overruns = %ld\n", adapter->rx_overruns);
4929 device_printf(dev, "watchdog timeouts = %ld\n",
4930 adapter->watchdog_events);
4931 device_printf(dev, "XON Rcvd = %lld\n",
4932 (long long)adapter->stats.xonrxc);
4933 device_printf(dev, "XON Xmtd = %lld\n",
4934 (long long)adapter->stats.xontxc);
4935 device_printf(dev, "XOFF Rcvd = %lld\n",
4936 (long long)adapter->stats.xoffrxc);
4937 device_printf(dev, "XOFF Xmtd = %lld\n",
4938 (long long)adapter->stats.xofftxc);
4939 device_printf(dev, "Good Packets Rcvd = %lld\n",
4940 (long long)adapter->stats.gprc);
4941 device_printf(dev, "Good Packets Xmtd = %lld\n",
4942 (long long)adapter->stats.gptc);
4943 device_printf(dev, "TSO Contexts Xmtd = %lld\n",
4944 (long long)adapter->stats.tsctc);
4945 device_printf(dev, "TSO Contexts Failed = %lld\n",
4946 (long long)adapter->stats.tsctfc);
4949 /**********************************************************************
4951 * This routine provides a way to dump out the adapter eeprom,
4952 * often a useful debug/service tool. This only dumps the first
4953 * 32 words, stuff that matters is in that extent.
4955 **********************************************************************/
4957 igb_print_nvm_info(struct adapter *adapter)
4962 /* Its a bit crude, but it gets the job done */
4963 kprintf("\nInterface EEPROM Dump:\n");
4964 kprintf("Offset\n0x0000 ");
4965 for (i = 0, j = 0; i < 32; i++, j++) {
4966 if (j == 8) { /* Make the offset block */
4968 kprintf("\n0x00%x0 ",row);
4970 e1000_read_nvm(&adapter->hw, i, 1, &eeprom_data);
4971 kprintf("%04x ", eeprom_data);
4977 igb_sysctl_debug_info(SYSCTL_HANDLER_ARGS)
4979 struct adapter *adapter;
4984 error = sysctl_handle_int(oidp, &result, 0, req);
4986 if (error || !req->newptr)
4990 adapter = (struct adapter *)arg1;
4991 igb_print_debug_info(adapter);
4994 * This value will cause a hex dump of the
4995 * first 32 16-bit words of the EEPROM to
4999 adapter = (struct adapter *)arg1;
5000 igb_print_nvm_info(adapter);
5008 igb_sysctl_stats(SYSCTL_HANDLER_ARGS)
5010 struct adapter *adapter;
5015 error = sysctl_handle_int(oidp, &result, 0, req);
5017 if (error || !req->newptr)
5021 adapter = (struct adapter *)arg1;
5022 igb_print_hw_stats(adapter);
5029 igb_add_rx_process_limit(struct adapter *adapter, const char *name,
5030 const char *description, int *limit, int value)
5033 SYSCTL_ADD_INT(&adapter->sysctl_ctx,
5034 SYSCTL_CHILDREN(adapter->sysctl_tree),
5035 OID_AUTO, name, CTLTYPE_INT|CTLFLAG_RW, limit, value, description);