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 ******************************************************************************/
36 #ifdef HAVE_KERNEL_OPTION_HEADERS
37 #include "opt_device_polling.h"
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #if __FreeBSD_version >= 800000
45 #include <sys/buf_ring.h>
48 #include <sys/endian.h>
49 #include <sys/kernel.h>
50 #include <sys/kthread.h>
51 #include <sys/malloc.h>
53 #include <sys/module.h>
55 #include <sys/socket.h>
56 #include <sys/sockio.h>
57 #include <sys/sysctl.h>
58 #include <sys/taskqueue.h>
59 #include <sys/eventhandler.h>
62 #include <machine/smp.h>
63 #include <machine/bus.h>
64 #include <machine/resource.h>
67 #include <sys/ieee1588.h>
71 #include <net/ethernet.h>
73 #include <net/if_arp.h>
74 #include <net/if_dl.h>
75 #include <net/if_media.h>
77 #include <net/if_types.h>
78 #include <net/if_vlan_var.h>
80 #include <netinet/in_systm.h>
81 #include <netinet/in.h>
82 #include <netinet/if_ether.h>
83 #include <netinet/ip.h>
84 #include <netinet/ip6.h>
85 #include <netinet/tcp.h>
86 #include <netinet/tcp_lro.h>
87 #include <netinet/udp.h>
89 #include <machine/in_cksum.h>
90 #include <dev/pci/pcivar.h>
91 #include <dev/pci/pcireg.h>
93 #include "e1000_api.h"
94 #include "e1000_82575.h"
97 /*********************************************************************
98 * Set this to one to display debug statistics
99 *********************************************************************/
100 int igb_display_debug_stats = 0;
102 /*********************************************************************
104 *********************************************************************/
105 char igb_driver_version[] = "version - 1.9.1";
108 /*********************************************************************
109 * PCI Device ID Table
111 * Used by probe to select devices to load on
112 * Last field stores an index into e1000_strings
113 * Last entry must be all 0s
115 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID, String Index }
116 *********************************************************************/
118 static igb_vendor_info_t igb_vendor_info_array[] =
120 { 0x8086, E1000_DEV_ID_82575EB_COPPER, PCI_ANY_ID, PCI_ANY_ID, 0},
121 { 0x8086, E1000_DEV_ID_82575EB_FIBER_SERDES,
122 PCI_ANY_ID, PCI_ANY_ID, 0},
123 { 0x8086, E1000_DEV_ID_82575GB_QUAD_COPPER,
124 PCI_ANY_ID, PCI_ANY_ID, 0},
125 { 0x8086, E1000_DEV_ID_82576, PCI_ANY_ID, PCI_ANY_ID, 0},
126 { 0x8086, E1000_DEV_ID_82576_NS, PCI_ANY_ID, PCI_ANY_ID, 0},
127 { 0x8086, E1000_DEV_ID_82576_NS_SERDES, PCI_ANY_ID, PCI_ANY_ID, 0},
128 { 0x8086, E1000_DEV_ID_82576_FIBER, PCI_ANY_ID, PCI_ANY_ID, 0},
129 { 0x8086, E1000_DEV_ID_82576_SERDES, PCI_ANY_ID, PCI_ANY_ID, 0},
130 { 0x8086, E1000_DEV_ID_82576_SERDES_QUAD,
131 PCI_ANY_ID, PCI_ANY_ID, 0},
132 { 0x8086, E1000_DEV_ID_82576_QUAD_COPPER,
133 PCI_ANY_ID, PCI_ANY_ID, 0},
134 { 0x8086, E1000_DEV_ID_82580_COPPER, PCI_ANY_ID, PCI_ANY_ID, 0},
135 { 0x8086, E1000_DEV_ID_82580_FIBER, PCI_ANY_ID, PCI_ANY_ID, 0},
136 { 0x8086, E1000_DEV_ID_82580_SERDES, PCI_ANY_ID, PCI_ANY_ID, 0},
137 { 0x8086, E1000_DEV_ID_82580_SGMII, PCI_ANY_ID, PCI_ANY_ID, 0},
138 { 0x8086, E1000_DEV_ID_82580_COPPER_DUAL,
139 PCI_ANY_ID, PCI_ANY_ID, 0},
140 /* required last entry */
144 /*********************************************************************
145 * Table of branding strings for all supported NICs.
146 *********************************************************************/
148 static char *igb_strings[] = {
149 "Intel(R) PRO/1000 Network Connection"
152 /*********************************************************************
153 * Function prototypes
154 *********************************************************************/
155 static int igb_probe(device_t);
156 static int igb_attach(device_t);
157 static int igb_detach(device_t);
158 static int igb_shutdown(device_t);
159 static int igb_suspend(device_t);
160 static int igb_resume(device_t);
161 static void igb_start(struct ifnet *);
162 static void igb_start_locked(struct tx_ring *, struct ifnet *ifp);
163 #if __FreeBSD_version >= 800000
164 static int igb_mq_start(struct ifnet *, struct mbuf *);
165 static int igb_mq_start_locked(struct ifnet *,
166 struct tx_ring *, struct mbuf *);
167 static void igb_qflush(struct ifnet *);
169 static int igb_ioctl(struct ifnet *, u_long, caddr_t);
170 static void igb_init(void *);
171 static void igb_init_locked(struct adapter *);
172 static void igb_stop(void *);
173 static void igb_media_status(struct ifnet *, struct ifmediareq *);
174 static int igb_media_change(struct ifnet *);
175 static void igb_identify_hardware(struct adapter *);
176 static int igb_allocate_pci_resources(struct adapter *);
177 static int igb_allocate_msix(struct adapter *);
178 static int igb_allocate_legacy(struct adapter *);
179 static int igb_setup_msix(struct adapter *);
180 static void igb_free_pci_resources(struct adapter *);
181 static void igb_local_timer(void *);
182 static void igb_reset(struct adapter *);
183 static void igb_setup_interface(device_t, struct adapter *);
184 static int igb_allocate_queues(struct adapter *);
185 static void igb_configure_queues(struct adapter *);
187 static int igb_allocate_transmit_buffers(struct tx_ring *);
188 static void igb_setup_transmit_structures(struct adapter *);
189 static void igb_setup_transmit_ring(struct tx_ring *);
190 static void igb_initialize_transmit_units(struct adapter *);
191 static void igb_free_transmit_structures(struct adapter *);
192 static void igb_free_transmit_buffers(struct tx_ring *);
194 static int igb_allocate_receive_buffers(struct rx_ring *);
195 static int igb_setup_receive_structures(struct adapter *);
196 static int igb_setup_receive_ring(struct rx_ring *);
197 static void igb_initialize_receive_units(struct adapter *);
198 static void igb_free_receive_structures(struct adapter *);
199 static void igb_free_receive_buffers(struct rx_ring *);
200 static void igb_free_receive_ring(struct rx_ring *);
202 static void igb_enable_intr(struct adapter *);
203 static void igb_disable_intr(struct adapter *);
204 static void igb_update_stats_counters(struct adapter *);
205 static bool igb_txeof(struct tx_ring *);
207 static __inline void igb_rx_discard(struct rx_ring *,
208 union e1000_adv_rx_desc *, int);
209 static __inline void igb_rx_input(struct rx_ring *,
210 struct ifnet *, struct mbuf *, u32);
212 static bool igb_rxeof(struct rx_ring *, int);
213 static void igb_rx_checksum(u32, struct mbuf *, u32);
214 static int igb_tx_ctx_setup(struct tx_ring *, struct mbuf *);
215 static bool igb_tso_setup(struct tx_ring *, struct mbuf *, u32 *);
216 static void igb_set_promisc(struct adapter *);
217 static void igb_disable_promisc(struct adapter *);
218 static void igb_set_multi(struct adapter *);
219 static void igb_print_hw_stats(struct adapter *);
220 static void igb_update_link_status(struct adapter *);
221 static int igb_get_buf(struct rx_ring *, int, u8);
223 static void igb_register_vlan(void *, struct ifnet *, u16);
224 static void igb_unregister_vlan(void *, struct ifnet *, u16);
225 static void igb_setup_vlan_hw_support(struct adapter *);
227 static int igb_xmit(struct tx_ring *, struct mbuf **);
228 static int igb_dma_malloc(struct adapter *, bus_size_t,
229 struct igb_dma_alloc *, int);
230 static void igb_dma_free(struct adapter *, struct igb_dma_alloc *);
231 static void igb_print_debug_info(struct adapter *);
232 static void igb_print_nvm_info(struct adapter *);
233 static int igb_is_valid_ether_addr(u8 *);
234 static int igb_sysctl_stats(SYSCTL_HANDLER_ARGS);
235 static int igb_sysctl_debug_info(SYSCTL_HANDLER_ARGS);
236 /* Management and WOL Support */
237 static void igb_init_manageability(struct adapter *);
238 static void igb_release_manageability(struct adapter *);
239 static void igb_get_hw_control(struct adapter *);
240 static void igb_release_hw_control(struct adapter *);
241 static void igb_enable_wakeup(device_t);
243 static int igb_irq_fast(void *);
244 static void igb_add_rx_process_limit(struct adapter *, const char *,
245 const char *, int *, int);
246 static void igb_handle_rxtx(void *context, int pending);
247 static void igb_handle_que(void *context, int pending);
248 static void igb_handle_link(void *context, int pending);
250 /* These are MSIX only irq handlers */
251 static void igb_msix_que(void *);
252 static void igb_msix_link(void *);
254 #ifdef DEVICE_POLLING
255 static poll_handler_t igb_poll;
258 /*********************************************************************
259 * FreeBSD Device Interface Entry Points
260 *********************************************************************/
262 static device_method_t igb_methods[] = {
263 /* Device interface */
264 DEVMETHOD(device_probe, igb_probe),
265 DEVMETHOD(device_attach, igb_attach),
266 DEVMETHOD(device_detach, igb_detach),
267 DEVMETHOD(device_shutdown, igb_shutdown),
268 DEVMETHOD(device_suspend, igb_suspend),
269 DEVMETHOD(device_resume, igb_resume),
273 static driver_t igb_driver = {
274 "igb", igb_methods, sizeof(struct adapter),
277 static devclass_t igb_devclass;
278 DRIVER_MODULE(igb, pci, igb_driver, igb_devclass, 0, 0);
279 MODULE_DEPEND(igb, pci, 1, 1, 1);
280 MODULE_DEPEND(igb, ether, 1, 1, 1);
282 /*********************************************************************
283 * Tunable default values.
284 *********************************************************************/
286 /* Descriptor defaults */
287 static int igb_rxd = IGB_DEFAULT_RXD;
288 static int igb_txd = IGB_DEFAULT_TXD;
289 TUNABLE_INT("hw.igb.rxd", &igb_rxd);
290 TUNABLE_INT("hw.igb.txd", &igb_txd);
293 ** AIM: Adaptive Interrupt Moderation
294 ** which means that the interrupt rate
295 ** is varied over time based on the
296 ** traffic for that interrupt vector
298 static int igb_enable_aim = TRUE;
299 TUNABLE_INT("hw.igb.enable_aim", &igb_enable_aim);
302 * MSIX should be the default for best performance,
303 * but this allows it to be forced off for testing.
305 static int igb_enable_msix = 1;
306 TUNABLE_INT("hw.igb.enable_msix", &igb_enable_msix);
309 * Header split has seemed to be beneficial in
310 * many circumstances tested, however there have
311 * been some stability issues, so the default is
314 static bool igb_header_split = FALSE;
315 TUNABLE_INT("hw.igb.hdr_split", &igb_header_split);
318 ** This will autoconfigure based on
319 ** the number of CPUs if left at 0.
321 static int igb_num_queues = 0;
322 TUNABLE_INT("hw.igb.num_queues", &igb_num_queues);
324 /* How many packets rxeof tries to clean at a time */
325 static int igb_rx_process_limit = 100;
326 TUNABLE_INT("hw.igb.rx_process_limit", &igb_rx_process_limit);
328 /* Flow control setting - default to FULL */
329 static int igb_fc_setting = e1000_fc_full;
330 TUNABLE_INT("hw.igb.fc_setting", &igb_fc_setting);
333 ** Shadow VFTA table, this is needed because
334 ** the real filter table gets cleared during
335 ** a soft reset and the driver needs to be able
338 static u32 igb_shadow_vfta[IGB_VFTA_SIZE];
341 /*********************************************************************
342 * Device identification routine
344 * igb_probe determines if the driver should be loaded on
345 * adapter based on PCI vendor/device id of the adapter.
347 * return BUS_PROBE_DEFAULT on success, positive on failure
348 *********************************************************************/
351 igb_probe(device_t dev)
353 char adapter_name[60];
354 uint16_t pci_vendor_id = 0;
355 uint16_t pci_device_id = 0;
356 uint16_t pci_subvendor_id = 0;
357 uint16_t pci_subdevice_id = 0;
358 igb_vendor_info_t *ent;
360 INIT_DEBUGOUT("igb_probe: begin");
362 pci_vendor_id = pci_get_vendor(dev);
363 if (pci_vendor_id != IGB_VENDOR_ID)
366 pci_device_id = pci_get_device(dev);
367 pci_subvendor_id = pci_get_subvendor(dev);
368 pci_subdevice_id = pci_get_subdevice(dev);
370 ent = igb_vendor_info_array;
371 while (ent->vendor_id != 0) {
372 if ((pci_vendor_id == ent->vendor_id) &&
373 (pci_device_id == ent->device_id) &&
375 ((pci_subvendor_id == ent->subvendor_id) ||
376 (ent->subvendor_id == PCI_ANY_ID)) &&
378 ((pci_subdevice_id == ent->subdevice_id) ||
379 (ent->subdevice_id == PCI_ANY_ID))) {
380 sprintf(adapter_name, "%s %s",
381 igb_strings[ent->index],
383 device_set_desc_copy(dev, adapter_name);
384 return (BUS_PROBE_DEFAULT);
392 /*********************************************************************
393 * Device initialization routine
395 * The attach entry point is called when the driver is being loaded.
396 * This routine identifies the type of hardware, allocates all resources
397 * and initializes the hardware.
399 * return 0 on success, positive on failure
400 *********************************************************************/
403 igb_attach(device_t dev)
405 struct adapter *adapter;
409 INIT_DEBUGOUT("igb_attach: begin");
411 adapter = device_get_softc(dev);
412 adapter->dev = adapter->osdep.dev = dev;
413 IGB_CORE_LOCK_INIT(adapter, device_get_nameunit(dev));
416 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
417 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
418 OID_AUTO, "debug", CTLTYPE_INT|CTLFLAG_RW, adapter, 0,
419 igb_sysctl_debug_info, "I", "Debug Information");
421 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
422 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
423 OID_AUTO, "stats", CTLTYPE_INT|CTLFLAG_RW, adapter, 0,
424 igb_sysctl_stats, "I", "Statistics");
426 SYSCTL_ADD_INT(device_get_sysctl_ctx(adapter->dev),
427 SYSCTL_CHILDREN(device_get_sysctl_tree(adapter->dev)),
428 OID_AUTO, "flow_control", CTLTYPE_INT|CTLFLAG_RW,
429 &igb_fc_setting, 0, "Flow Control");
431 SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
432 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
433 OID_AUTO, "enable_aim", CTLTYPE_INT|CTLFLAG_RW,
434 &igb_enable_aim, 1, "Interrupt Moderation");
436 callout_init_mtx(&adapter->timer, &adapter->core_mtx, 0);
438 /* Determine hardware and mac info */
439 igb_identify_hardware(adapter);
441 /* Setup PCI resources */
442 if (igb_allocate_pci_resources(adapter)) {
443 device_printf(dev, "Allocation of PCI resources failed\n");
448 /* Do Shared Code initialization */
449 if (e1000_setup_init_funcs(&adapter->hw, TRUE)) {
450 device_printf(dev, "Setup of Shared code failed\n");
455 e1000_get_bus_info(&adapter->hw);
457 /* Sysctls for limiting the amount of work done in the taskqueue */
458 igb_add_rx_process_limit(adapter, "rx_processing_limit",
459 "max number of rx packets to process", &adapter->rx_process_limit,
460 igb_rx_process_limit);
463 * Validate number of transmit and receive descriptors. It
464 * must not exceed hardware maximum, and must be multiple
465 * of E1000_DBA_ALIGN.
467 if (((igb_txd * sizeof(struct e1000_tx_desc)) % IGB_DBA_ALIGN) != 0 ||
468 (igb_txd > IGB_MAX_TXD) || (igb_txd < IGB_MIN_TXD)) {
469 device_printf(dev, "Using %d TX descriptors instead of %d!\n",
470 IGB_DEFAULT_TXD, igb_txd);
471 adapter->num_tx_desc = IGB_DEFAULT_TXD;
473 adapter->num_tx_desc = igb_txd;
474 if (((igb_rxd * sizeof(struct e1000_rx_desc)) % IGB_DBA_ALIGN) != 0 ||
475 (igb_rxd > IGB_MAX_RXD) || (igb_rxd < IGB_MIN_RXD)) {
476 device_printf(dev, "Using %d RX descriptors instead of %d!\n",
477 IGB_DEFAULT_RXD, igb_rxd);
478 adapter->num_rx_desc = IGB_DEFAULT_RXD;
480 adapter->num_rx_desc = igb_rxd;
482 adapter->hw.mac.autoneg = DO_AUTO_NEG;
483 adapter->hw.phy.autoneg_wait_to_complete = FALSE;
484 adapter->hw.phy.autoneg_advertised = AUTONEG_ADV_DEFAULT;
487 if (adapter->hw.phy.media_type == e1000_media_type_copper) {
488 adapter->hw.phy.mdix = AUTO_ALL_MODES;
489 adapter->hw.phy.disable_polarity_correction = FALSE;
490 adapter->hw.phy.ms_type = IGB_MASTER_SLAVE;
494 * Set the frame limits assuming
495 * standard ethernet sized frames.
497 adapter->max_frame_size = ETHERMTU + ETHER_HDR_LEN + ETHERNET_FCS_SIZE;
498 adapter->min_frame_size = ETH_ZLEN + ETHERNET_FCS_SIZE;
501 ** Allocate and Setup Queues
503 if (igb_allocate_queues(adapter)) {
509 ** Start from a known state, this is
510 ** important in reading the nvm and
513 e1000_reset_hw(&adapter->hw);
515 /* Make sure we have a good EEPROM before we read from it */
516 if (e1000_validate_nvm_checksum(&adapter->hw) < 0) {
518 ** Some PCI-E parts fail the first check due to
519 ** the link being in sleep state, call it again,
520 ** if it fails a second time its a real issue.
522 if (e1000_validate_nvm_checksum(&adapter->hw) < 0) {
524 "The EEPROM Checksum Is Not Valid\n");
531 ** Copy the permanent MAC address out of the EEPROM
533 if (e1000_read_mac_addr(&adapter->hw) < 0) {
534 device_printf(dev, "EEPROM read error while reading MAC"
539 /* Check its sanity */
540 if (!igb_is_valid_ether_addr(adapter->hw.mac.addr)) {
541 device_printf(dev, "Invalid MAC address\n");
547 ** Configure Interrupts
549 if ((adapter->msix > 1) && (igb_enable_msix))
550 error = igb_allocate_msix(adapter);
551 else /* MSI or Legacy */
552 error = igb_allocate_legacy(adapter);
556 /* Setup OS specific network interface */
557 igb_setup_interface(dev, adapter);
559 /* Now get a good starting state */
562 /* Initialize statistics */
563 igb_update_stats_counters(adapter);
565 adapter->hw.mac.get_link_status = 1;
566 igb_update_link_status(adapter);
568 /* Indicate SOL/IDER usage */
569 if (e1000_check_reset_block(&adapter->hw))
571 "PHY reset is blocked due to SOL/IDER session.\n");
573 /* Determine if we have to control management hardware */
574 adapter->has_manage = e1000_enable_mng_pass_thru(&adapter->hw);
579 /* APME bit in EEPROM is mapped to WUC.APME */
580 eeprom_data = E1000_READ_REG(&adapter->hw, E1000_WUC) & E1000_WUC_APME;
582 adapter->wol = E1000_WUFC_MAG;
584 /* Register for VLAN events */
585 adapter->vlan_attach = EVENTHANDLER_REGISTER(vlan_config,
586 igb_register_vlan, adapter, EVENTHANDLER_PRI_FIRST);
587 adapter->vlan_detach = EVENTHANDLER_REGISTER(vlan_unconfig,
588 igb_unregister_vlan, adapter, EVENTHANDLER_PRI_FIRST);
590 /* Tell the stack that the interface is not active */
591 adapter->ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
593 INIT_DEBUGOUT("igb_attach: end");
598 igb_free_transmit_structures(adapter);
599 igb_free_receive_structures(adapter);
600 igb_release_hw_control(adapter);
602 igb_free_pci_resources(adapter);
603 IGB_CORE_LOCK_DESTROY(adapter);
608 /*********************************************************************
609 * Device removal routine
611 * The detach entry point is called when the driver is being removed.
612 * This routine stops the adapter and deallocates all the resources
613 * that were allocated for driver operation.
615 * return 0 on success, positive on failure
616 *********************************************************************/
619 igb_detach(device_t dev)
621 struct adapter *adapter = device_get_softc(dev);
622 struct ifnet *ifp = adapter->ifp;
624 INIT_DEBUGOUT("igb_detach: begin");
626 /* Make sure VLANS are not using driver */
627 if (adapter->ifp->if_vlantrunk != NULL) {
628 device_printf(dev,"Vlan in use, detach first\n");
632 #ifdef DEVICE_POLLING
633 if (ifp->if_capenable & IFCAP_POLLING)
634 ether_poll_deregister(ifp);
637 IGB_CORE_LOCK(adapter);
638 adapter->in_detach = 1;
640 IGB_CORE_UNLOCK(adapter);
642 e1000_phy_hw_reset(&adapter->hw);
644 /* Give control back to firmware */
645 igb_release_manageability(adapter);
646 igb_release_hw_control(adapter);
649 E1000_WRITE_REG(&adapter->hw, E1000_WUC, E1000_WUC_PME_EN);
650 E1000_WRITE_REG(&adapter->hw, E1000_WUFC, adapter->wol);
651 igb_enable_wakeup(dev);
654 /* Unregister VLAN events */
655 if (adapter->vlan_attach != NULL)
656 EVENTHANDLER_DEREGISTER(vlan_config, adapter->vlan_attach);
657 if (adapter->vlan_detach != NULL)
658 EVENTHANDLER_DEREGISTER(vlan_unconfig, adapter->vlan_detach);
660 ether_ifdetach(adapter->ifp);
662 //callout_drain(&adapter->timer);
663 callout_stop(&adapter->timer);
665 igb_free_pci_resources(adapter);
666 bus_generic_detach(dev);
669 igb_free_transmit_structures(adapter);
670 igb_free_receive_structures(adapter);
672 IGB_CORE_LOCK_DESTROY(adapter);
677 /*********************************************************************
679 * Shutdown entry point
681 **********************************************************************/
684 igb_shutdown(device_t dev)
686 return igb_suspend(dev);
690 * Suspend/resume device methods.
693 igb_suspend(device_t dev)
695 struct adapter *adapter = device_get_softc(dev);
697 IGB_CORE_LOCK(adapter);
701 igb_release_manageability(adapter);
702 igb_release_hw_control(adapter);
705 E1000_WRITE_REG(&adapter->hw, E1000_WUC, E1000_WUC_PME_EN);
706 E1000_WRITE_REG(&adapter->hw, E1000_WUFC, adapter->wol);
707 igb_enable_wakeup(dev);
710 IGB_CORE_UNLOCK(adapter);
712 return bus_generic_suspend(dev);
716 igb_resume(device_t dev)
718 struct adapter *adapter = device_get_softc(dev);
719 struct ifnet *ifp = adapter->ifp;
721 IGB_CORE_LOCK(adapter);
722 igb_init_locked(adapter);
723 igb_init_manageability(adapter);
725 if ((ifp->if_flags & IFF_UP) &&
726 (ifp->if_drv_flags & IFF_DRV_RUNNING))
729 IGB_CORE_UNLOCK(adapter);
731 return bus_generic_resume(dev);
735 /*********************************************************************
736 * Transmit entry point
738 * igb_start is called by the stack to initiate a transmit.
739 * The driver will remain in this routine as long as there are
740 * packets to transmit and transmit resources are available.
741 * In case resources are not available stack is notified and
742 * the packet is requeued.
743 **********************************************************************/
746 igb_start_locked(struct tx_ring *txr, struct ifnet *ifp)
748 struct adapter *adapter = ifp->if_softc;
751 IGB_TX_LOCK_ASSERT(txr);
753 if ((ifp->if_drv_flags & (IFF_DRV_RUNNING|IFF_DRV_OACTIVE)) !=
756 if (!adapter->link_active)
759 while (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) {
761 IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
765 * Encapsulation can modify our pointer, and or make it
766 * NULL on failure. In that event, we can't requeue.
768 if (igb_xmit(txr, &m_head)) {
771 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
772 IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
776 /* Send a copy of the frame to the BPF listener */
777 ETHER_BPF_MTAP(ifp, m_head);
779 /* Set watchdog on */
780 txr->watchdog_check = TRUE;
785 * Legacy TX driver routine, called from the
786 * stack, always uses tx[0], and spins for it.
787 * Should not be used with multiqueue tx
790 igb_start(struct ifnet *ifp)
792 struct adapter *adapter = ifp->if_softc;
793 struct tx_ring *txr = adapter->tx_rings;
795 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
797 igb_start_locked(txr, ifp);
803 #if __FreeBSD_version >= 800000
805 ** Multiqueue Transmit driver
809 igb_mq_start(struct ifnet *ifp, struct mbuf *m)
811 struct adapter *adapter = ifp->if_softc;
815 /* Which queue to use */
816 if ((m->m_flags & M_FLOWID) != 0)
817 i = m->m_pkthdr.flowid % adapter->num_queues;
818 txr = &adapter->tx_rings[i];
820 if (IGB_TX_TRYLOCK(txr)) {
821 err = igb_mq_start_locked(ifp, txr, m);
824 err = drbr_enqueue(ifp, txr->br, m);
830 igb_mq_start_locked(struct ifnet *ifp, struct tx_ring *txr, struct mbuf *m)
832 struct adapter *adapter = txr->adapter;
836 IGB_TX_LOCK_ASSERT(txr);
838 if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
839 IFF_DRV_RUNNING || adapter->link_active == 0) {
841 err = drbr_enqueue(ifp, txr->br, m);
847 next = drbr_dequeue(ifp, txr->br);
848 } else if (drbr_needs_enqueue(ifp, txr->br)) {
849 if ((err = drbr_enqueue(ifp, txr->br, m)) != 0)
851 next = drbr_dequeue(ifp, txr->br);
854 /* Process the queue */
855 while (next != NULL) {
856 if ((err = igb_xmit(txr, &next)) != 0) {
858 err = drbr_enqueue(ifp, txr->br, next);
862 drbr_stats_update(ifp, next->m_pkthdr.len, next->m_flags);
863 ETHER_BPF_MTAP(ifp, next);
864 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
866 if (txr->tx_avail <= IGB_TX_OP_THRESHOLD) {
867 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
870 next = drbr_dequeue(ifp, txr->br);
873 /* Set the watchdog */
874 txr->watchdog_check = TRUE;
880 ** Flush all ring buffers
883 igb_qflush(struct ifnet *ifp)
885 struct adapter *adapter = ifp->if_softc;
886 struct tx_ring *txr = adapter->tx_rings;
889 for (int i = 0; i < adapter->num_queues; i++, txr++) {
891 while ((m = buf_ring_dequeue_sc(txr->br)) != NULL)
897 #endif /* __FreeBSD_version >= 800000 */
899 /*********************************************************************
902 * igb_ioctl is called when the user wants to configure the
905 * return 0 on success, positive on failure
906 **********************************************************************/
909 igb_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
911 struct adapter *adapter = ifp->if_softc;
912 struct ifreq *ifr = (struct ifreq *)data;
914 struct ifaddr *ifa = (struct ifaddr *)data;
918 if (adapter->in_detach)
924 if (ifa->ifa_addr->sa_family == AF_INET) {
927 * Since resetting hardware takes a very long time
928 * and results in link renegotiation we only
929 * initialize the hardware only when it is absolutely
932 ifp->if_flags |= IFF_UP;
933 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
934 IGB_CORE_LOCK(adapter);
935 igb_init_locked(adapter);
936 IGB_CORE_UNLOCK(adapter);
938 if (!(ifp->if_flags & IFF_NOARP))
939 arp_ifinit(ifp, ifa);
942 error = ether_ioctl(ifp, command, data);
948 IOCTL_DEBUGOUT("ioctl rcv'd: SIOCSIFMTU (Set Interface MTU)");
950 IGB_CORE_LOCK(adapter);
951 max_frame_size = 9234;
952 if (ifr->ifr_mtu > max_frame_size - ETHER_HDR_LEN -
954 IGB_CORE_UNLOCK(adapter);
959 ifp->if_mtu = ifr->ifr_mtu;
960 adapter->max_frame_size =
961 ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN;
962 igb_init_locked(adapter);
963 IGB_CORE_UNLOCK(adapter);
967 IOCTL_DEBUGOUT("ioctl rcv'd:\
968 SIOCSIFFLAGS (Set Interface Flags)");
969 IGB_CORE_LOCK(adapter);
970 if (ifp->if_flags & IFF_UP) {
971 if ((ifp->if_drv_flags & IFF_DRV_RUNNING)) {
972 if ((ifp->if_flags ^ adapter->if_flags) &
973 (IFF_PROMISC | IFF_ALLMULTI)) {
974 igb_disable_promisc(adapter);
975 igb_set_promisc(adapter);
978 igb_init_locked(adapter);
980 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
982 adapter->if_flags = ifp->if_flags;
983 IGB_CORE_UNLOCK(adapter);
987 IOCTL_DEBUGOUT("ioctl rcv'd: SIOC(ADD|DEL)MULTI");
988 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
989 IGB_CORE_LOCK(adapter);
990 igb_disable_intr(adapter);
991 igb_set_multi(adapter);
992 #ifdef DEVICE_POLLING
993 if (!(ifp->if_capenable & IFCAP_POLLING))
995 igb_enable_intr(adapter);
996 IGB_CORE_UNLOCK(adapter);
1000 /* Check SOL/IDER usage */
1001 IGB_CORE_LOCK(adapter);
1002 if (e1000_check_reset_block(&adapter->hw)) {
1003 IGB_CORE_UNLOCK(adapter);
1004 device_printf(adapter->dev, "Media change is"
1005 " blocked due to SOL/IDER session.\n");
1008 IGB_CORE_UNLOCK(adapter);
1010 IOCTL_DEBUGOUT("ioctl rcv'd: \
1011 SIOCxIFMEDIA (Get/Set Interface Media)");
1012 error = ifmedia_ioctl(ifp, ifr, &adapter->media, command);
1018 IOCTL_DEBUGOUT("ioctl rcv'd: SIOCSIFCAP (Set Capabilities)");
1020 mask = ifr->ifr_reqcap ^ ifp->if_capenable;
1021 #ifdef DEVICE_POLLING
1022 if (mask & IFCAP_POLLING) {
1023 if (ifr->ifr_reqcap & IFCAP_POLLING) {
1024 error = ether_poll_register(igb_poll, ifp);
1027 IGB_CORE_LOCK(adapter);
1028 igb_disable_intr(adapter);
1029 ifp->if_capenable |= IFCAP_POLLING;
1030 IGB_CORE_UNLOCK(adapter);
1032 error = ether_poll_deregister(ifp);
1033 /* Enable interrupt even in error case */
1034 IGB_CORE_LOCK(adapter);
1035 igb_enable_intr(adapter);
1036 ifp->if_capenable &= ~IFCAP_POLLING;
1037 IGB_CORE_UNLOCK(adapter);
1041 if (mask & IFCAP_HWCSUM) {
1042 ifp->if_capenable ^= IFCAP_HWCSUM;
1045 if (mask & IFCAP_TSO4) {
1046 ifp->if_capenable ^= IFCAP_TSO4;
1049 if (mask & IFCAP_VLAN_HWTAGGING) {
1050 ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
1053 if (mask & IFCAP_LRO) {
1054 ifp->if_capenable ^= IFCAP_LRO;
1057 if (reinit && (ifp->if_drv_flags & IFF_DRV_RUNNING))
1059 VLAN_CAPABILITIES(ifp);
1064 error = ether_ioctl(ifp, command, data);
1072 /*********************************************************************
1075 * This routine is used in two ways. It is used by the stack as
1076 * init entry point in network interface structure. It is also used
1077 * by the driver as a hw/sw initialization routine to get to a
1080 * return 0 on success, positive on failure
1081 **********************************************************************/
1084 igb_init_locked(struct adapter *adapter)
1086 struct ifnet *ifp = adapter->ifp;
1087 device_t dev = adapter->dev;
1089 INIT_DEBUGOUT("igb_init: begin");
1091 IGB_CORE_LOCK_ASSERT(adapter);
1093 igb_disable_intr(adapter);
1094 callout_stop(&adapter->timer);
1096 /* Get the latest mac address, User can use a LAA */
1097 bcopy(IF_LLADDR(adapter->ifp), adapter->hw.mac.addr,
1100 /* Put the address into the Receive Address Array */
1101 e1000_rar_set(&adapter->hw, adapter->hw.mac.addr, 0);
1104 igb_update_link_status(adapter);
1106 E1000_WRITE_REG(&adapter->hw, E1000_VET, ETHERTYPE_VLAN);
1108 /* Set hardware offload abilities */
1109 ifp->if_hwassist = 0;
1110 if (ifp->if_capenable & IFCAP_TXCSUM) {
1111 ifp->if_hwassist |= (CSUM_TCP | CSUM_UDP);
1112 #if __FreeBSD_version >= 800000
1113 if (adapter->hw.mac.type == e1000_82576)
1114 ifp->if_hwassist |= CSUM_SCTP;
1118 if (ifp->if_capenable & IFCAP_TSO4)
1119 ifp->if_hwassist |= CSUM_TSO;
1121 /* Configure for OS presence */
1122 igb_init_manageability(adapter);
1124 /* Prepare transmit descriptors and buffers */
1125 igb_setup_transmit_structures(adapter);
1126 igb_initialize_transmit_units(adapter);
1128 /* Setup Multicast table */
1129 igb_set_multi(adapter);
1132 ** Figure out the desired mbuf pool
1133 ** for doing jumbo/packetsplit
1135 if (ifp->if_mtu > ETHERMTU)
1136 adapter->rx_mbuf_sz = MJUMPAGESIZE;
1138 adapter->rx_mbuf_sz = MCLBYTES;
1140 /* Prepare receive descriptors and buffers */
1141 if (igb_setup_receive_structures(adapter)) {
1142 device_printf(dev, "Could not setup receive structures\n");
1145 igb_initialize_receive_units(adapter);
1147 /* Don't lose promiscuous settings */
1148 igb_set_promisc(adapter);
1150 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1151 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1153 callout_reset(&adapter->timer, hz, igb_local_timer, adapter);
1154 e1000_clear_hw_cntrs_base_generic(&adapter->hw);
1156 if (adapter->msix > 1) /* Set up queue routing */
1157 igb_configure_queues(adapter);
1159 /* Set up VLAN tag offload and filter */
1160 igb_setup_vlan_hw_support(adapter);
1162 /* this clears any pending interrupts */
1163 E1000_READ_REG(&adapter->hw, E1000_ICR);
1164 #ifdef DEVICE_POLLING
1166 * Only enable interrupts if we are not polling, make sure
1167 * they are off otherwise.
1169 if (ifp->if_capenable & IFCAP_POLLING)
1170 igb_disable_intr(adapter);
1172 #endif /* DEVICE_POLLING */
1174 igb_enable_intr(adapter);
1175 E1000_WRITE_REG(&adapter->hw, E1000_ICS, E1000_ICS_LSC);
1178 /* Don't reset the phy next time init gets called */
1179 adapter->hw.phy.reset_disable = TRUE;
1185 struct adapter *adapter = arg;
1187 IGB_CORE_LOCK(adapter);
1188 igb_init_locked(adapter);
1189 IGB_CORE_UNLOCK(adapter);
1194 igb_handle_rxtx(void *context, int pending)
1196 struct adapter *adapter = context;
1197 struct tx_ring *txr = adapter->tx_rings;
1198 struct rx_ring *rxr = adapter->rx_rings;
1203 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1204 if (igb_rxeof(rxr, adapter->rx_process_limit))
1205 taskqueue_enqueue(adapter->tq, &adapter->rxtx_task);
1209 #if __FreeBSD_version >= 800000
1210 if (!drbr_empty(ifp, txr->br))
1211 igb_mq_start_locked(ifp, txr, NULL);
1213 if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
1214 igb_start_locked(txr, ifp);
1219 igb_enable_intr(adapter);
1223 igb_handle_que(void *context, int pending)
1225 struct igb_queue *que = context;
1226 struct adapter *adapter = que->adapter;
1227 struct tx_ring *txr = que->txr;
1228 struct rx_ring *rxr = que->rxr;
1229 struct ifnet *ifp = adapter->ifp;
1230 u32 loop = IGB_MAX_LOOP;
1235 more = igb_rxeof(rxr, -1);
1236 } while (loop-- && more);
1238 if (IGB_TX_TRYLOCK(txr)) {
1239 loop = IGB_MAX_LOOP;
1241 more = igb_txeof(txr);
1242 } while (loop-- && more);
1243 #if __FreeBSD_version >= 800000
1244 igb_mq_start_locked(ifp, txr, NULL);
1246 if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
1247 igb_start_locked(txr, ifp);
1252 /* Reenable this interrupt */
1253 #ifdef DEVICE_POLLING
1254 if (!(ifp->if_capenable & IFCAP_POLLING))
1256 E1000_WRITE_REG(&adapter->hw, E1000_EIMS, que->eims);
1259 /* Deal with link in a sleepable context */
1261 igb_handle_link(void *context, int pending)
1263 struct adapter *adapter = context;
1265 adapter->hw.mac.get_link_status = 1;
1266 igb_update_link_status(adapter);
1269 /*********************************************************************
1271 * MSI/Legacy Deferred
1272 * Interrupt Service routine
1274 *********************************************************************/
1276 igb_irq_fast(void *arg)
1278 struct adapter *adapter = arg;
1282 reg_icr = E1000_READ_REG(&adapter->hw, E1000_ICR);
1285 if (reg_icr == 0xffffffff)
1286 return FILTER_STRAY;
1288 /* Definitely not our interrupt. */
1290 return FILTER_STRAY;
1292 if ((reg_icr & E1000_ICR_INT_ASSERTED) == 0)
1293 return FILTER_STRAY;
1296 * Mask interrupts until the taskqueue is finished running. This is
1297 * cheap, just assume that it is needed. This also works around the
1298 * MSI message reordering errata on certain systems.
1300 igb_disable_intr(adapter);
1301 taskqueue_enqueue(adapter->tq, &adapter->rxtx_task);
1303 /* Link status change */
1304 if (reg_icr & (E1000_ICR_RXSEQ | E1000_ICR_LSC))
1305 taskqueue_enqueue(adapter->tq, &adapter->link_task);
1307 if (reg_icr & E1000_ICR_RXO)
1308 adapter->rx_overruns++;
1309 return FILTER_HANDLED;
1312 #ifdef DEVICE_POLLING
1313 /*********************************************************************
1315 * Legacy polling routine
1317 *********************************************************************/
1318 #if __FreeBSD_version >= 800000
1319 #define POLL_RETURN_COUNT(a) (a)
1322 #define POLL_RETURN_COUNT(a)
1325 igb_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
1327 struct adapter *adapter = ifp->if_softc;
1328 struct rx_ring *rxr = adapter->rx_rings;
1329 struct tx_ring *txr = adapter->tx_rings;
1330 u32 reg_icr, rx_done = 0;
1331 u32 loop = IGB_MAX_LOOP;
1334 IGB_CORE_LOCK(adapter);
1335 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1336 IGB_CORE_UNLOCK(adapter);
1337 return POLL_RETURN_COUNT(rx_done);
1340 if (cmd == POLL_AND_CHECK_STATUS) {
1341 reg_icr = E1000_READ_REG(&adapter->hw, E1000_ICR);
1342 /* Link status change */
1343 if (reg_icr & (E1000_ICR_RXSEQ | E1000_ICR_LSC))
1344 taskqueue_enqueue(adapter->tq, &adapter->link_task);
1346 if (reg_icr & E1000_ICR_RXO)
1347 adapter->rx_overruns++;
1349 IGB_CORE_UNLOCK(adapter);
1351 /* TODO: rx_count */
1352 rx_done = igb_rxeof(rxr, count) ? 1 : 0;
1356 more = igb_txeof(txr);
1357 } while (loop-- && more);
1358 #if __FreeBSD_version >= 800000
1359 if (!drbr_empty(ifp, txr->br))
1360 igb_mq_start_locked(ifp, txr, NULL);
1362 if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
1363 igb_start_locked(txr, ifp);
1366 return POLL_RETURN_COUNT(rx_done);
1368 #endif /* DEVICE_POLLING */
1370 /*********************************************************************
1372 * MSIX TX Interrupt Service routine
1374 **********************************************************************/
1376 igb_msix_que(void *arg)
1378 struct igb_queue *que = arg;
1379 struct adapter *adapter = que->adapter;
1380 struct tx_ring *txr = que->txr;
1381 struct rx_ring *rxr = que->rxr;
1383 bool more_tx, more_rx;
1385 E1000_WRITE_REG(&adapter->hw, E1000_EIMC, que->eims);
1389 more_tx = igb_txeof(txr);
1392 more_rx = igb_rxeof(rxr, adapter->rx_process_limit);
1394 if (igb_enable_aim == FALSE)
1397 ** Do Adaptive Interrupt Moderation:
1398 ** - Write out last calculated setting
1399 ** - Calculate based on average size over
1400 ** the last interval.
1402 if (que->eitr_setting)
1403 E1000_WRITE_REG(&adapter->hw,
1404 E1000_EITR(que->msix), que->eitr_setting);
1406 que->eitr_setting = 0;
1408 /* Idle, do nothing */
1409 if ((txr->bytes == 0) && (rxr->bytes == 0))
1412 /* Used half Default if sub-gig */
1413 if (adapter->link_speed != 1000)
1414 newitr = IGB_DEFAULT_ITR / 2;
1416 if ((txr->bytes) && (txr->packets))
1417 newitr = txr->bytes/txr->packets;
1418 if ((rxr->bytes) && (rxr->packets))
1419 newitr = max(newitr,
1420 (rxr->bytes / rxr->packets));
1421 newitr += 24; /* account for hardware frame, crc */
1422 /* set an upper boundary */
1423 newitr = min(newitr, 3000);
1424 /* Be nice to the mid range */
1425 if ((newitr > 300) && (newitr < 1200))
1426 newitr = (newitr / 3);
1428 newitr = (newitr / 2);
1430 newitr &= 0x7FFC; /* Mask invalid bits */
1431 if (adapter->hw.mac.type == e1000_82575)
1432 newitr |= newitr << 16;
1434 newitr |= 0x8000000;
1436 /* save for next interrupt */
1437 que->eitr_setting = newitr;
1446 /* Schedule a clean task if needed*/
1447 if (more_tx || more_rx)
1448 taskqueue_enqueue(que->tq, &que->que_task);
1450 /* Reenable this interrupt */
1451 E1000_WRITE_REG(&adapter->hw, E1000_EIMS, que->eims);
1456 /*********************************************************************
1458 * MSIX Link Interrupt Service routine
1460 **********************************************************************/
1463 igb_msix_link(void *arg)
1465 struct adapter *adapter = arg;
1468 ++adapter->link_irq;
1469 icr = E1000_READ_REG(&adapter->hw, E1000_ICR);
1470 if (!(icr & E1000_ICR_LSC))
1472 taskqueue_enqueue(adapter->tq, &adapter->link_task);
1476 E1000_WRITE_REG(&adapter->hw, E1000_IMS, E1000_IMS_LSC);
1477 E1000_WRITE_REG(&adapter->hw, E1000_EIMS, adapter->link_mask);
1482 /*********************************************************************
1484 * Media Ioctl callback
1486 * This routine is called whenever the user queries the status of
1487 * the interface using ifconfig.
1489 **********************************************************************/
1491 igb_media_status(struct ifnet *ifp, struct ifmediareq *ifmr)
1493 struct adapter *adapter = ifp->if_softc;
1494 u_char fiber_type = IFM_1000_SX;
1496 INIT_DEBUGOUT("igb_media_status: begin");
1498 IGB_CORE_LOCK(adapter);
1499 igb_update_link_status(adapter);
1501 ifmr->ifm_status = IFM_AVALID;
1502 ifmr->ifm_active = IFM_ETHER;
1504 if (!adapter->link_active) {
1505 IGB_CORE_UNLOCK(adapter);
1509 ifmr->ifm_status |= IFM_ACTIVE;
1511 if ((adapter->hw.phy.media_type == e1000_media_type_fiber) ||
1512 (adapter->hw.phy.media_type == e1000_media_type_internal_serdes))
1513 ifmr->ifm_active |= fiber_type | IFM_FDX;
1515 switch (adapter->link_speed) {
1517 ifmr->ifm_active |= IFM_10_T;
1520 ifmr->ifm_active |= IFM_100_TX;
1523 ifmr->ifm_active |= IFM_1000_T;
1526 if (adapter->link_duplex == FULL_DUPLEX)
1527 ifmr->ifm_active |= IFM_FDX;
1529 ifmr->ifm_active |= IFM_HDX;
1531 IGB_CORE_UNLOCK(adapter);
1534 /*********************************************************************
1536 * Media Ioctl callback
1538 * This routine is called when the user changes speed/duplex using
1539 * media/mediopt option with ifconfig.
1541 **********************************************************************/
1543 igb_media_change(struct ifnet *ifp)
1545 struct adapter *adapter = ifp->if_softc;
1546 struct ifmedia *ifm = &adapter->media;
1548 INIT_DEBUGOUT("igb_media_change: begin");
1550 if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
1553 IGB_CORE_LOCK(adapter);
1554 switch (IFM_SUBTYPE(ifm->ifm_media)) {
1556 adapter->hw.mac.autoneg = DO_AUTO_NEG;
1557 adapter->hw.phy.autoneg_advertised = AUTONEG_ADV_DEFAULT;
1562 adapter->hw.mac.autoneg = DO_AUTO_NEG;
1563 adapter->hw.phy.autoneg_advertised = ADVERTISE_1000_FULL;
1566 adapter->hw.mac.autoneg = FALSE;
1567 adapter->hw.phy.autoneg_advertised = 0;
1568 if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX)
1569 adapter->hw.mac.forced_speed_duplex = ADVERTISE_100_FULL;
1571 adapter->hw.mac.forced_speed_duplex = ADVERTISE_100_HALF;
1574 adapter->hw.mac.autoneg = FALSE;
1575 adapter->hw.phy.autoneg_advertised = 0;
1576 if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX)
1577 adapter->hw.mac.forced_speed_duplex = ADVERTISE_10_FULL;
1579 adapter->hw.mac.forced_speed_duplex = ADVERTISE_10_HALF;
1582 device_printf(adapter->dev, "Unsupported media type\n");
1585 /* As the speed/duplex settings my have changed we need to
1588 adapter->hw.phy.reset_disable = FALSE;
1590 igb_init_locked(adapter);
1591 IGB_CORE_UNLOCK(adapter);
1597 /*********************************************************************
1599 * This routine maps the mbufs to Advanced TX descriptors.
1600 * used by the 82575 adapter.
1602 **********************************************************************/
1605 igb_xmit(struct tx_ring *txr, struct mbuf **m_headp)
1607 struct adapter *adapter = txr->adapter;
1608 bus_dma_segment_t segs[IGB_MAX_SCATTER];
1610 struct igb_tx_buffer *tx_buffer, *tx_buffer_mapped;
1611 union e1000_adv_tx_desc *txd = NULL;
1612 struct mbuf *m_head;
1613 u32 olinfo_status = 0, cmd_type_len = 0;
1614 int nsegs, i, j, error, first, last = 0;
1620 /* Set basic descriptor constants */
1621 cmd_type_len |= E1000_ADVTXD_DTYP_DATA;
1622 cmd_type_len |= E1000_ADVTXD_DCMD_IFCS | E1000_ADVTXD_DCMD_DEXT;
1623 if (m_head->m_flags & M_VLANTAG)
1624 cmd_type_len |= E1000_ADVTXD_DCMD_VLE;
1627 * Force a cleanup if number of TX descriptors
1628 * available hits the threshold
1630 if (txr->tx_avail <= IGB_TX_CLEANUP_THRESHOLD) {
1632 /* Now do we at least have a minimal? */
1633 if (txr->tx_avail <= IGB_TX_OP_THRESHOLD) {
1634 txr->no_desc_avail++;
1640 * Map the packet for DMA.
1642 * Capture the first descriptor index,
1643 * this descriptor will have the index
1644 * of the EOP which is the only one that
1645 * now gets a DONE bit writeback.
1647 first = txr->next_avail_desc;
1648 tx_buffer = &txr->tx_buffers[first];
1649 tx_buffer_mapped = tx_buffer;
1650 map = tx_buffer->map;
1652 error = bus_dmamap_load_mbuf_sg(txr->txtag, map,
1653 *m_headp, segs, &nsegs, BUS_DMA_NOWAIT);
1655 if (error == EFBIG) {
1658 m = m_defrag(*m_headp, M_DONTWAIT);
1660 adapter->mbuf_defrag_failed++;
1668 error = bus_dmamap_load_mbuf_sg(txr->txtag, map,
1669 *m_headp, segs, &nsegs, BUS_DMA_NOWAIT);
1671 if (error == ENOMEM) {
1672 adapter->no_tx_dma_setup++;
1674 } else if (error != 0) {
1675 adapter->no_tx_dma_setup++;
1680 } else if (error == ENOMEM) {
1681 adapter->no_tx_dma_setup++;
1683 } else if (error != 0) {
1684 adapter->no_tx_dma_setup++;
1690 /* Check again to be sure we have enough descriptors */
1691 if (nsegs > (txr->tx_avail - 2)) {
1692 txr->no_desc_avail++;
1693 bus_dmamap_unload(txr->txtag, map);
1699 * Set up the context descriptor:
1700 * used when any hardware offload is done.
1701 * This includes CSUM, VLAN, and TSO. It
1702 * will use the first descriptor.
1704 if (m_head->m_pkthdr.csum_flags & CSUM_TSO) {
1705 if (igb_tso_setup(txr, m_head, &hdrlen)) {
1706 cmd_type_len |= E1000_ADVTXD_DCMD_TSE;
1707 olinfo_status |= E1000_TXD_POPTS_IXSM << 8;
1708 olinfo_status |= E1000_TXD_POPTS_TXSM << 8;
1711 } else if (igb_tx_ctx_setup(txr, m_head))
1712 olinfo_status |= E1000_TXD_POPTS_TXSM << 8;
1714 /* Calculate payload length */
1715 olinfo_status |= ((m_head->m_pkthdr.len - hdrlen)
1716 << E1000_ADVTXD_PAYLEN_SHIFT);
1718 /* 82575 needs the queue index added */
1719 if (adapter->hw.mac.type == e1000_82575)
1720 olinfo_status |= txr->me << 4;
1722 /* Set up our transmit descriptors */
1723 i = txr->next_avail_desc;
1724 for (j = 0; j < nsegs; j++) {
1726 bus_addr_t seg_addr;
1728 tx_buffer = &txr->tx_buffers[i];
1729 txd = (union e1000_adv_tx_desc *)&txr->tx_base[i];
1730 seg_addr = segs[j].ds_addr;
1731 seg_len = segs[j].ds_len;
1733 txd->read.buffer_addr = htole64(seg_addr);
1734 txd->read.cmd_type_len = htole32(cmd_type_len | seg_len);
1735 txd->read.olinfo_status = htole32(olinfo_status);
1737 if (++i == adapter->num_tx_desc)
1739 tx_buffer->m_head = NULL;
1740 tx_buffer->next_eop = -1;
1743 txr->next_avail_desc = i;
1744 txr->tx_avail -= nsegs;
1746 tx_buffer->m_head = m_head;
1747 tx_buffer_mapped->map = tx_buffer->map;
1748 tx_buffer->map = map;
1749 bus_dmamap_sync(txr->txtag, map, BUS_DMASYNC_PREWRITE);
1752 * Last Descriptor of Packet
1753 * needs End Of Packet (EOP)
1754 * and Report Status (RS)
1756 txd->read.cmd_type_len |=
1757 htole32(E1000_ADVTXD_DCMD_EOP | E1000_ADVTXD_DCMD_RS);
1759 * Keep track in the first buffer which
1760 * descriptor will be written back
1762 tx_buffer = &txr->tx_buffers[first];
1763 tx_buffer->next_eop = last;
1764 txr->watchdog_time = ticks;
1767 * Advance the Transmit Descriptor Tail (TDT), this tells the E1000
1768 * that this frame is available to transmit.
1770 bus_dmamap_sync(txr->txdma.dma_tag, txr->txdma.dma_map,
1771 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1772 E1000_WRITE_REG(&adapter->hw, E1000_TDT(txr->me), i);
1780 igb_set_promisc(struct adapter *adapter)
1782 struct ifnet *ifp = adapter->ifp;
1785 reg_rctl = E1000_READ_REG(&adapter->hw, E1000_RCTL);
1787 if (ifp->if_flags & IFF_PROMISC) {
1788 reg_rctl |= (E1000_RCTL_UPE | E1000_RCTL_MPE);
1789 E1000_WRITE_REG(&adapter->hw, E1000_RCTL, reg_rctl);
1790 } else if (ifp->if_flags & IFF_ALLMULTI) {
1791 reg_rctl |= E1000_RCTL_MPE;
1792 reg_rctl &= ~E1000_RCTL_UPE;
1793 E1000_WRITE_REG(&adapter->hw, E1000_RCTL, reg_rctl);
1798 igb_disable_promisc(struct adapter *adapter)
1802 reg_rctl = E1000_READ_REG(&adapter->hw, E1000_RCTL);
1804 reg_rctl &= (~E1000_RCTL_UPE);
1805 reg_rctl &= (~E1000_RCTL_MPE);
1806 E1000_WRITE_REG(&adapter->hw, E1000_RCTL, reg_rctl);
1810 /*********************************************************************
1813 * This routine is called whenever multicast address list is updated.
1815 **********************************************************************/
1818 igb_set_multi(struct adapter *adapter)
1820 struct ifnet *ifp = adapter->ifp;
1821 struct ifmultiaddr *ifma;
1823 u8 mta[MAX_NUM_MULTICAST_ADDRESSES * ETH_ADDR_LEN];
1827 IOCTL_DEBUGOUT("igb_set_multi: begin");
1829 #if __FreeBSD_version < 800000
1832 if_maddr_rlock(ifp);
1834 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1835 if (ifma->ifma_addr->sa_family != AF_LINK)
1838 if (mcnt == MAX_NUM_MULTICAST_ADDRESSES)
1841 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
1842 &mta[mcnt * ETH_ADDR_LEN], ETH_ADDR_LEN);
1845 #if __FreeBSD_version < 800000
1846 IF_ADDR_UNLOCK(ifp);
1848 if_maddr_runlock(ifp);
1851 if (mcnt >= MAX_NUM_MULTICAST_ADDRESSES) {
1852 reg_rctl = E1000_READ_REG(&adapter->hw, E1000_RCTL);
1853 reg_rctl |= E1000_RCTL_MPE;
1854 E1000_WRITE_REG(&adapter->hw, E1000_RCTL, reg_rctl);
1856 e1000_update_mc_addr_list(&adapter->hw, mta, mcnt);
1860 /*********************************************************************
1862 * This routine checks for link status,
1863 * updates statistics, and does the watchdog.
1865 **********************************************************************/
1868 igb_local_timer(void *arg)
1870 struct adapter *adapter = arg;
1871 struct ifnet *ifp = adapter->ifp;
1872 device_t dev = adapter->dev;
1873 struct tx_ring *txr = adapter->tx_rings;
1876 IGB_CORE_LOCK_ASSERT(adapter);
1878 igb_update_link_status(adapter);
1879 igb_update_stats_counters(adapter);
1881 if (igb_display_debug_stats && ifp->if_drv_flags & IFF_DRV_RUNNING)
1882 igb_print_hw_stats(adapter);
1885 ** Watchdog: check for time since any descriptor was cleaned
1887 for (int i = 0; i < adapter->num_queues; i++, txr++) {
1888 if (txr->watchdog_check == FALSE)
1890 if ((ticks - txr->watchdog_time) > IGB_WATCHDOG)
1894 /* Trigger an RX interrupt on all queues */
1895 #ifdef DEVICE_POLLING
1896 if (!(ifp->if_capenable & IFCAP_POLLING))
1898 E1000_WRITE_REG(&adapter->hw, E1000_EICS, adapter->rx_mask);
1899 callout_reset(&adapter->timer, hz, igb_local_timer, adapter);
1903 device_printf(adapter->dev, "Watchdog timeout -- resetting\n");
1904 device_printf(dev,"Queue(%d) tdh = %d, hw tdt = %d\n", txr->me,
1905 E1000_READ_REG(&adapter->hw, E1000_TDH(txr->me)),
1906 E1000_READ_REG(&adapter->hw, E1000_TDT(txr->me)));
1907 device_printf(dev,"TX(%d) desc avail = %d,"
1908 "Next TX to Clean = %d\n",
1909 txr->me, txr->tx_avail, txr->next_to_clean);
1910 adapter->ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1911 adapter->watchdog_events++;
1912 igb_init_locked(adapter);
1916 igb_update_link_status(struct adapter *adapter)
1918 struct e1000_hw *hw = &adapter->hw;
1919 struct ifnet *ifp = adapter->ifp;
1920 device_t dev = adapter->dev;
1921 struct tx_ring *txr = adapter->tx_rings;
1924 /* Get the cached link value or read for real */
1925 switch (hw->phy.media_type) {
1926 case e1000_media_type_copper:
1927 if (hw->mac.get_link_status) {
1928 /* Do the work to read phy */
1929 e1000_check_for_link(hw);
1930 link_check = !hw->mac.get_link_status;
1934 case e1000_media_type_fiber:
1935 e1000_check_for_link(hw);
1936 link_check = (E1000_READ_REG(hw, E1000_STATUS) &
1939 case e1000_media_type_internal_serdes:
1940 e1000_check_for_link(hw);
1941 link_check = adapter->hw.mac.serdes_has_link;
1944 case e1000_media_type_unknown:
1948 /* Now we check if a transition has happened */
1949 if (link_check && (adapter->link_active == 0)) {
1950 e1000_get_speed_and_duplex(&adapter->hw,
1951 &adapter->link_speed, &adapter->link_duplex);
1953 device_printf(dev, "Link is up %d Mbps %s\n",
1954 adapter->link_speed,
1955 ((adapter->link_duplex == FULL_DUPLEX) ?
1956 "Full Duplex" : "Half Duplex"));
1957 adapter->link_active = 1;
1958 ifp->if_baudrate = adapter->link_speed * 1000000;
1959 if_link_state_change(ifp, LINK_STATE_UP);
1960 } else if (!link_check && (adapter->link_active == 1)) {
1961 ifp->if_baudrate = adapter->link_speed = 0;
1962 adapter->link_duplex = 0;
1964 device_printf(dev, "Link is Down\n");
1965 adapter->link_active = 0;
1966 if_link_state_change(ifp, LINK_STATE_DOWN);
1967 /* Turn off watchdogs */
1968 for (int i = 0; i < adapter->num_queues; i++, txr++)
1969 txr->watchdog_check = FALSE;
1973 /*********************************************************************
1975 * This routine disables all traffic on the adapter by issuing a
1976 * global reset on the MAC and deallocates TX/RX buffers.
1978 **********************************************************************/
1983 struct adapter *adapter = arg;
1984 struct ifnet *ifp = adapter->ifp;
1985 struct tx_ring *txr = adapter->tx_rings;
1987 IGB_CORE_LOCK_ASSERT(adapter);
1989 INIT_DEBUGOUT("igb_stop: begin");
1991 igb_disable_intr(adapter);
1993 callout_stop(&adapter->timer);
1995 /* Tell the stack that the interface is no longer active */
1996 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
1998 /* Unarm watchdog timer. */
1999 for (int i = 0; i < adapter->num_queues; i++, txr++) {
2001 txr->watchdog_check = FALSE;
2005 e1000_reset_hw(&adapter->hw);
2006 E1000_WRITE_REG(&adapter->hw, E1000_WUC, 0);
2010 /*********************************************************************
2012 * Determine hardware revision.
2014 **********************************************************************/
2016 igb_identify_hardware(struct adapter *adapter)
2018 device_t dev = adapter->dev;
2020 /* Make sure our PCI config space has the necessary stuff set */
2021 adapter->hw.bus.pci_cmd_word = pci_read_config(dev, PCIR_COMMAND, 2);
2022 if (!((adapter->hw.bus.pci_cmd_word & PCIM_CMD_BUSMASTEREN) &&
2023 (adapter->hw.bus.pci_cmd_word & PCIM_CMD_MEMEN))) {
2024 device_printf(dev, "Memory Access and/or Bus Master bits "
2026 adapter->hw.bus.pci_cmd_word |=
2027 (PCIM_CMD_BUSMASTEREN | PCIM_CMD_MEMEN);
2028 pci_write_config(dev, PCIR_COMMAND,
2029 adapter->hw.bus.pci_cmd_word, 2);
2032 /* Save off the information about this board */
2033 adapter->hw.vendor_id = pci_get_vendor(dev);
2034 adapter->hw.device_id = pci_get_device(dev);
2035 adapter->hw.revision_id = pci_read_config(dev, PCIR_REVID, 1);
2036 adapter->hw.subsystem_vendor_id =
2037 pci_read_config(dev, PCIR_SUBVEND_0, 2);
2038 adapter->hw.subsystem_device_id =
2039 pci_read_config(dev, PCIR_SUBDEV_0, 2);
2041 /* Do Shared Code Init and Setup */
2042 if (e1000_set_mac_type(&adapter->hw)) {
2043 device_printf(dev, "Setup init failure\n");
2049 igb_allocate_pci_resources(struct adapter *adapter)
2051 device_t dev = adapter->dev;
2055 adapter->pci_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
2057 if (adapter->pci_mem == NULL) {
2058 device_printf(dev, "Unable to allocate bus resource: memory\n");
2061 adapter->osdep.mem_bus_space_tag =
2062 rman_get_bustag(adapter->pci_mem);
2063 adapter->osdep.mem_bus_space_handle =
2064 rman_get_bushandle(adapter->pci_mem);
2065 adapter->hw.hw_addr = (u8 *)&adapter->osdep.mem_bus_space_handle;
2067 adapter->num_queues = 1; /* Defaults for Legacy or MSI */
2069 /* This will setup either MSI/X or MSI */
2070 adapter->msix = igb_setup_msix(adapter);
2071 adapter->hw.back = &adapter->osdep;
2076 /*********************************************************************
2078 * Setup the Legacy or MSI Interrupt handler
2080 **********************************************************************/
2082 igb_allocate_legacy(struct adapter *adapter)
2084 device_t dev = adapter->dev;
2087 /* Turn off all interrupts */
2088 E1000_WRITE_REG(&adapter->hw, E1000_IMC, 0xffffffff);
2091 if (adapter->msix == 1)
2094 /* We allocate a single interrupt resource */
2095 adapter->res = bus_alloc_resource_any(dev,
2096 SYS_RES_IRQ, &rid, RF_SHAREABLE | RF_ACTIVE);
2097 if (adapter->res == NULL) {
2098 device_printf(dev, "Unable to allocate bus resource: "
2104 * Try allocating a fast interrupt and the associated deferred
2105 * processing contexts.
2107 TASK_INIT(&adapter->rxtx_task, 0, igb_handle_rxtx, adapter);
2108 /* Make tasklet for deferred link handling */
2109 TASK_INIT(&adapter->link_task, 0, igb_handle_link, adapter);
2110 adapter->tq = taskqueue_create_fast("igb_taskq", M_NOWAIT,
2111 taskqueue_thread_enqueue, &adapter->tq);
2112 taskqueue_start_threads(&adapter->tq, 1, PI_NET, "%s taskq",
2113 device_get_nameunit(adapter->dev));
2114 if ((error = bus_setup_intr(dev, adapter->res,
2115 INTR_TYPE_NET | INTR_MPSAFE, igb_irq_fast, NULL,
2116 adapter, &adapter->tag)) != 0) {
2117 device_printf(dev, "Failed to register fast interrupt "
2118 "handler: %d\n", error);
2119 taskqueue_free(adapter->tq);
2128 /*********************************************************************
2130 * Setup the MSIX Queue Interrupt handlers:
2132 **********************************************************************/
2134 igb_allocate_msix(struct adapter *adapter)
2136 device_t dev = adapter->dev;
2137 struct igb_queue *que = adapter->queues;
2138 int error, rid, vector = 0;
2141 for (int i = 0; i < adapter->num_queues; i++, vector++, que++) {
2143 que->res = bus_alloc_resource_any(dev,
2144 SYS_RES_IRQ, &rid, RF_SHAREABLE | RF_ACTIVE);
2145 if (que->res == NULL) {
2147 "Unable to allocate bus resource: "
2148 "MSIX Queue Interrupt\n");
2151 error = bus_setup_intr(dev, que->res,
2152 INTR_TYPE_NET | INTR_MPSAFE, NULL,
2153 igb_msix_que, que, &que->tag);
2156 device_printf(dev, "Failed to register Queue handler");
2160 if (adapter->hw.mac.type == e1000_82575)
2161 que->eims = E1000_EICR_TX_QUEUE0 << i;
2163 que->eims = 1 << vector;
2165 ** Bind the msix vector, and thus the
2166 ** rings to the corresponding cpu.
2168 if (adapter->num_queues > 1)
2169 bus_bind_intr(dev, que->res, i);
2170 /* Make tasklet for deferred handling */
2171 TASK_INIT(&que->que_task, 0, igb_handle_que, que);
2172 que->tq = taskqueue_create_fast("igb_que", M_NOWAIT,
2173 taskqueue_thread_enqueue, &que->tq);
2174 taskqueue_start_threads(&que->tq, 1, PI_NET, "%s que",
2175 device_get_nameunit(adapter->dev));
2180 adapter->res = bus_alloc_resource_any(dev,
2181 SYS_RES_IRQ, &rid, RF_SHAREABLE | RF_ACTIVE);
2182 if (adapter->res == NULL) {
2184 "Unable to allocate bus resource: "
2185 "MSIX Link Interrupt\n");
2188 if ((error = bus_setup_intr(dev, adapter->res,
2189 INTR_TYPE_NET | INTR_MPSAFE, NULL,
2190 igb_msix_link, adapter, &adapter->tag)) != 0) {
2191 device_printf(dev, "Failed to register Link handler");
2194 adapter->linkvec = vector;
2196 /* Make tasklet for deferred handling */
2197 TASK_INIT(&adapter->link_task, 0, igb_handle_link, adapter);
2198 adapter->tq = taskqueue_create_fast("igb_link", M_NOWAIT,
2199 taskqueue_thread_enqueue, &adapter->tq);
2200 taskqueue_start_threads(&adapter->tq, 1, PI_NET, "%s link",
2201 device_get_nameunit(adapter->dev));
2208 igb_configure_queues(struct adapter *adapter)
2210 struct e1000_hw *hw = &adapter->hw;
2211 struct igb_queue *que;
2213 u32 newitr = IGB_DEFAULT_ITR;
2215 /* First turn on RSS capability */
2216 if (adapter->hw.mac.type > e1000_82575)
2217 E1000_WRITE_REG(hw, E1000_GPIE,
2218 E1000_GPIE_MSIX_MODE | E1000_GPIE_EIAME |
2219 E1000_GPIE_PBA | E1000_GPIE_NSICR);
2222 switch (adapter->hw.mac.type) {
2225 for (int i = 0; i < adapter->num_queues; i++) {
2227 ivar = E1000_READ_REG_ARRAY(hw, E1000_IVAR0, index);
2228 que = &adapter->queues[i];
2231 ivar |= (que->msix | E1000_IVAR_VALID) << 16;
2234 ivar |= que->msix | E1000_IVAR_VALID;
2236 E1000_WRITE_REG_ARRAY(hw, E1000_IVAR0, index, ivar);
2239 for (int i = 0; i < adapter->num_queues; i++) {
2241 ivar = E1000_READ_REG_ARRAY(hw, E1000_IVAR0, index);
2242 que = &adapter->queues[i];
2245 ivar |= (que->msix | E1000_IVAR_VALID) << 24;
2248 ivar |= (que->msix | E1000_IVAR_VALID) << 8;
2250 E1000_WRITE_REG_ARRAY(hw, E1000_IVAR0, index, ivar);
2251 adapter->eims_mask |= que->eims;
2254 /* And for the link interrupt */
2255 ivar = (adapter->linkvec | E1000_IVAR_VALID) << 8;
2256 adapter->link_mask = 1 << adapter->linkvec;
2257 adapter->eims_mask |= adapter->link_mask;
2258 E1000_WRITE_REG(hw, E1000_IVAR_MISC, ivar);
2262 for (int i = 0; i < adapter->num_queues; i++) {
2263 u32 index = i & 0x7; /* Each IVAR has two entries */
2264 ivar = E1000_READ_REG_ARRAY(hw, E1000_IVAR0, index);
2265 que = &adapter->queues[i];
2268 ivar |= que->msix | E1000_IVAR_VALID;
2271 ivar |= (que->msix | E1000_IVAR_VALID) << 16;
2273 E1000_WRITE_REG_ARRAY(hw, E1000_IVAR0, index, ivar);
2274 adapter->eims_mask |= que->eims;
2277 for (int i = 0; i < adapter->num_queues; i++) {
2278 u32 index = i & 0x7; /* Each IVAR has two entries */
2279 ivar = E1000_READ_REG_ARRAY(hw, E1000_IVAR0, index);
2280 que = &adapter->queues[i];
2283 ivar |= (que->msix | E1000_IVAR_VALID) << 8;
2286 ivar |= (que->msix | E1000_IVAR_VALID) << 24;
2288 E1000_WRITE_REG_ARRAY(hw, E1000_IVAR0, index, ivar);
2289 adapter->eims_mask |= que->eims;
2292 /* And for the link interrupt */
2293 ivar = (adapter->linkvec | E1000_IVAR_VALID) << 8;
2294 adapter->link_mask = 1 << adapter->linkvec;
2295 adapter->eims_mask |= adapter->link_mask;
2296 E1000_WRITE_REG(hw, E1000_IVAR_MISC, ivar);
2300 /* enable MSI-X support*/
2301 tmp = E1000_READ_REG(hw, E1000_CTRL_EXT);
2302 tmp |= E1000_CTRL_EXT_PBA_CLR;
2303 /* Auto-Mask interrupts upon ICR read. */
2304 tmp |= E1000_CTRL_EXT_EIAME;
2305 tmp |= E1000_CTRL_EXT_IRCA;
2306 E1000_WRITE_REG(hw, E1000_CTRL_EXT, tmp);
2309 for (int i = 0; i < adapter->num_queues; i++) {
2310 que = &adapter->queues[i];
2311 tmp = E1000_EICR_RX_QUEUE0 << i;
2312 tmp |= E1000_EICR_TX_QUEUE0 << i;
2314 E1000_WRITE_REG_ARRAY(hw, E1000_MSIXBM(0),
2316 adapter->eims_mask |= que->eims;
2320 E1000_WRITE_REG(hw, E1000_MSIXBM(adapter->linkvec),
2322 adapter->link_mask |= E1000_EIMS_OTHER;
2323 adapter->eims_mask |= adapter->link_mask;
2328 /* Set the starting interrupt rate */
2329 if (hw->mac.type == e1000_82575)
2330 newitr |= newitr << 16;
2332 newitr |= 0x8000000;
2334 for (int i = 0; i < adapter->num_queues; i++) {
2335 que = &adapter->queues[i];
2336 E1000_WRITE_REG(hw, E1000_EITR(que->msix), newitr);
2344 igb_free_pci_resources(struct adapter *adapter)
2346 struct igb_queue *que = adapter->queues;
2347 device_t dev = adapter->dev;
2351 ** There is a slight possibility of a failure mode
2352 ** in attach that will result in entering this function
2353 ** before interrupt resources have been initialized, and
2354 ** in that case we do not want to execute the loops below
2355 ** We can detect this reliably by the state of the adapter
2358 if (adapter->res == NULL)
2362 * First release all the interrupt resources:
2364 for (int i = 0; i < adapter->num_queues; i++, que++) {
2365 rid = que->msix + 1;
2366 if (que->tag != NULL) {
2367 bus_teardown_intr(dev, que->res, que->tag);
2370 if (que->res != NULL)
2371 bus_release_resource(dev,
2372 SYS_RES_IRQ, rid, que->res);
2375 /* Clean the Legacy or Link interrupt last */
2376 if (adapter->linkvec) /* we are doing MSIX */
2377 rid = adapter->linkvec + 1;
2379 (adapter->msix != 0) ? (rid = 1):(rid = 0);
2381 if (adapter->tag != NULL) {
2382 bus_teardown_intr(dev, adapter->res, adapter->tag);
2383 adapter->tag = NULL;
2385 if (adapter->res != NULL)
2386 bus_release_resource(dev, SYS_RES_IRQ, rid, adapter->res);
2390 pci_release_msi(dev);
2392 if (adapter->msix_mem != NULL)
2393 bus_release_resource(dev, SYS_RES_MEMORY,
2394 PCIR_BAR(IGB_MSIX_BAR), adapter->msix_mem);
2396 if (adapter->pci_mem != NULL)
2397 bus_release_resource(dev, SYS_RES_MEMORY,
2398 PCIR_BAR(0), adapter->pci_mem);
2403 * Setup Either MSI/X or MSI
2406 igb_setup_msix(struct adapter *adapter)
2408 device_t dev = adapter->dev;
2409 int rid, want, queues, msgs;
2411 /* tuneable override */
2412 if (igb_enable_msix == 0)
2415 /* First try MSI/X */
2416 rid = PCIR_BAR(IGB_MSIX_BAR);
2417 adapter->msix_mem = bus_alloc_resource_any(dev,
2418 SYS_RES_MEMORY, &rid, RF_ACTIVE);
2419 if (!adapter->msix_mem) {
2420 /* May not be enabled */
2421 device_printf(adapter->dev,
2422 "Unable to map MSIX table \n");
2426 msgs = pci_msix_count(dev);
2427 if (msgs == 0) { /* system has msix disabled */
2428 bus_release_resource(dev, SYS_RES_MEMORY,
2429 PCIR_BAR(IGB_MSIX_BAR), adapter->msix_mem);
2430 adapter->msix_mem = NULL;
2434 /* Figure out a reasonable auto config value */
2435 queues = (mp_ncpus > (msgs-1)) ? (msgs-1) : mp_ncpus;
2437 /* Can have max of 4 queues on 82575 */
2438 if (adapter->hw.mac.type == e1000_82575) {
2441 if (igb_num_queues > 4)
2445 if (igb_num_queues == 0)
2446 igb_num_queues = queues;
2449 ** One vector (RX/TX pair) per queue
2450 ** plus an additional for Link interrupt
2452 want = igb_num_queues + 1;
2456 device_printf(adapter->dev,
2457 "MSIX Configuration Problem, "
2458 "%d vectors configured, but %d queues wanted!\n",
2462 if ((msgs) && pci_alloc_msix(dev, &msgs) == 0) {
2463 device_printf(adapter->dev,
2464 "Using MSIX interrupts with %d vectors\n", msgs);
2465 adapter->num_queues = igb_num_queues;
2469 msgs = pci_msi_count(dev);
2470 if (msgs == 1 && pci_alloc_msi(dev, &msgs) == 0)
2471 device_printf(adapter->dev,"Using MSI interrupt\n");
2475 /*********************************************************************
2477 * Set up an fresh starting state
2479 **********************************************************************/
2481 igb_reset(struct adapter *adapter)
2483 device_t dev = adapter->dev;
2484 struct e1000_hw *hw = &adapter->hw;
2485 struct e1000_fc_info *fc = &hw->fc;
2486 struct ifnet *ifp = adapter->ifp;
2490 INIT_DEBUGOUT("igb_reset: begin");
2492 /* Let the firmware know the OS is in control */
2493 igb_get_hw_control(adapter);
2496 * Packet Buffer Allocation (PBA)
2497 * Writing PBA sets the receive portion of the buffer
2498 * the remainder is used for the transmit buffer.
2500 switch (hw->mac.type) {
2502 pba = E1000_PBA_32K;
2505 pba = E1000_PBA_64K;
2508 pba = E1000_PBA_35K;
2513 /* Special needs in case of Jumbo frames */
2514 if ((hw->mac.type == e1000_82575) && (ifp->if_mtu > ETHERMTU)) {
2515 u32 tx_space, min_tx, min_rx;
2516 pba = E1000_READ_REG(hw, E1000_PBA);
2517 tx_space = pba >> 16;
2519 min_tx = (adapter->max_frame_size +
2520 sizeof(struct e1000_tx_desc) - ETHERNET_FCS_SIZE) * 2;
2521 min_tx = roundup2(min_tx, 1024);
2523 min_rx = adapter->max_frame_size;
2524 min_rx = roundup2(min_rx, 1024);
2526 if (tx_space < min_tx &&
2527 ((min_tx - tx_space) < pba)) {
2528 pba = pba - (min_tx - tx_space);
2530 * if short on rx space, rx wins
2531 * and must trump tx adjustment
2536 E1000_WRITE_REG(hw, E1000_PBA, pba);
2539 INIT_DEBUGOUT1("igb_init: pba=%dK",pba);
2542 * These parameters control the automatic generation (Tx) and
2543 * response (Rx) to Ethernet PAUSE frames.
2544 * - High water mark should allow for at least two frames to be
2545 * received after sending an XOFF.
2546 * - Low water mark works best when it is very near the high water mark.
2547 * This allows the receiver to restart by sending XON when it has
2550 hwm = min(((pba << 10) * 9 / 10),
2551 ((pba << 10) - 2 * adapter->max_frame_size));
2553 if (hw->mac.type < e1000_82576) {
2554 fc->high_water = hwm & 0xFFF8; /* 8-byte granularity */
2555 fc->low_water = fc->high_water - 8;
2557 fc->high_water = hwm & 0xFFF0; /* 16-byte granularity */
2558 fc->low_water = fc->high_water - 16;
2561 fc->pause_time = IGB_FC_PAUSE_TIME;
2562 fc->send_xon = TRUE;
2564 /* Set Flow control, use the tunable location if sane */
2565 if ((igb_fc_setting >= 0) || (igb_fc_setting < 4))
2566 fc->requested_mode = igb_fc_setting;
2568 fc->requested_mode = e1000_fc_none;
2570 fc->current_mode = fc->requested_mode;
2572 /* Issue a global reset */
2574 E1000_WRITE_REG(hw, E1000_WUC, 0);
2576 if (e1000_init_hw(hw) < 0)
2577 device_printf(dev, "Hardware Initialization Failed\n");
2579 if (hw->mac.type == e1000_82580) {
2582 hwm = (pba << 10) - (2 * adapter->max_frame_size);
2584 * 0x80000000 - enable DMA COAL
2585 * 0x10000000 - use L0s as low power
2586 * 0x20000000 - use L1 as low power
2587 * X << 16 - exit dma coal when rx data exceeds X kB
2588 * Y - upper limit to stay in dma coal in units of 32usecs
2590 E1000_WRITE_REG(hw, E1000_DMACR,
2591 0xA0000006 | ((hwm << 6) & 0x00FF0000));
2593 /* set hwm to PBA - 2 * max frame size */
2594 E1000_WRITE_REG(hw, E1000_FCRTC, hwm);
2596 * This sets the time to wait before requesting transition to
2597 * low power state to number of usecs needed to receive 1 512
2598 * byte frame at gigabit line rate
2600 E1000_WRITE_REG(hw, E1000_DMCTLX, 4);
2602 /* free space in tx packet buffer to wake from DMA coal */
2603 E1000_WRITE_REG(hw, E1000_DMCTXTH,
2604 (20480 - (2 * adapter->max_frame_size)) >> 6);
2606 /* make low power state decision controlled by DMA coal */
2607 reg = E1000_READ_REG(hw, E1000_PCIEMISC);
2608 E1000_WRITE_REG(hw, E1000_PCIEMISC,
2609 reg | E1000_PCIEMISC_LX_DECISION);
2612 E1000_WRITE_REG(&adapter->hw, E1000_VET, ETHERTYPE_VLAN);
2613 e1000_get_phy_info(hw);
2614 e1000_check_for_link(hw);
2618 /*********************************************************************
2620 * Setup networking device structure and register an interface.
2622 **********************************************************************/
2624 igb_setup_interface(device_t dev, struct adapter *adapter)
2628 INIT_DEBUGOUT("igb_setup_interface: begin");
2630 ifp = adapter->ifp = if_alloc(IFT_ETHER);
2632 panic("%s: can not if_alloc()", device_get_nameunit(dev));
2633 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
2634 ifp->if_mtu = ETHERMTU;
2635 ifp->if_init = igb_init;
2636 ifp->if_softc = adapter;
2637 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
2638 ifp->if_ioctl = igb_ioctl;
2639 ifp->if_start = igb_start;
2640 #if __FreeBSD_version >= 800000
2641 ifp->if_transmit = igb_mq_start;
2642 ifp->if_qflush = igb_qflush;
2644 IFQ_SET_MAXLEN(&ifp->if_snd, adapter->num_tx_desc - 1);
2645 ifp->if_snd.ifq_drv_maxlen = adapter->num_tx_desc - 1;
2646 IFQ_SET_READY(&ifp->if_snd);
2648 ether_ifattach(ifp, adapter->hw.mac.addr);
2650 ifp->if_capabilities = ifp->if_capenable = 0;
2652 ifp->if_capabilities = IFCAP_HWCSUM | IFCAP_VLAN_MTU;
2653 ifp->if_capabilities |= IFCAP_TSO4;
2654 ifp->if_capabilities |= IFCAP_JUMBO_MTU;
2655 if (igb_header_split)
2656 ifp->if_capabilities |= IFCAP_LRO;
2658 ifp->if_capenable = ifp->if_capabilities;
2659 #ifdef DEVICE_POLLING
2660 ifp->if_capabilities |= IFCAP_POLLING;
2664 * Tell the upper layer(s) we support long frames.
2666 ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
2667 ifp->if_capabilities |= IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_MTU;
2668 ifp->if_capenable |= IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_MTU;
2671 * Specify the media types supported by this adapter and register
2672 * callbacks to update media and link information
2674 ifmedia_init(&adapter->media, IFM_IMASK,
2675 igb_media_change, igb_media_status);
2676 if ((adapter->hw.phy.media_type == e1000_media_type_fiber) ||
2677 (adapter->hw.phy.media_type == e1000_media_type_internal_serdes)) {
2678 ifmedia_add(&adapter->media, IFM_ETHER | IFM_1000_SX | IFM_FDX,
2680 ifmedia_add(&adapter->media, IFM_ETHER | IFM_1000_SX, 0, NULL);
2682 ifmedia_add(&adapter->media, IFM_ETHER | IFM_10_T, 0, NULL);
2683 ifmedia_add(&adapter->media, IFM_ETHER | IFM_10_T | IFM_FDX,
2685 ifmedia_add(&adapter->media, IFM_ETHER | IFM_100_TX,
2687 ifmedia_add(&adapter->media, IFM_ETHER | IFM_100_TX | IFM_FDX,
2689 if (adapter->hw.phy.type != e1000_phy_ife) {
2690 ifmedia_add(&adapter->media,
2691 IFM_ETHER | IFM_1000_T | IFM_FDX, 0, NULL);
2692 ifmedia_add(&adapter->media,
2693 IFM_ETHER | IFM_1000_T, 0, NULL);
2696 ifmedia_add(&adapter->media, IFM_ETHER | IFM_AUTO, 0, NULL);
2697 ifmedia_set(&adapter->media, IFM_ETHER | IFM_AUTO);
2702 * Manage DMA'able memory.
2705 igb_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
2709 *(bus_addr_t *) arg = segs[0].ds_addr;
2713 igb_dma_malloc(struct adapter *adapter, bus_size_t size,
2714 struct igb_dma_alloc *dma, int mapflags)
2718 error = bus_dma_tag_create(bus_get_dma_tag(adapter->dev), /* parent */
2719 IGB_DBA_ALIGN, 0, /* alignment, bounds */
2720 BUS_SPACE_MAXADDR, /* lowaddr */
2721 BUS_SPACE_MAXADDR, /* highaddr */
2722 NULL, NULL, /* filter, filterarg */
2725 size, /* maxsegsize */
2727 NULL, /* lockfunc */
2731 device_printf(adapter->dev,
2732 "%s: bus_dma_tag_create failed: %d\n",
2737 error = bus_dmamem_alloc(dma->dma_tag, (void**) &dma->dma_vaddr,
2738 BUS_DMA_NOWAIT, &dma->dma_map);
2740 device_printf(adapter->dev,
2741 "%s: bus_dmamem_alloc(%ju) failed: %d\n",
2742 __func__, (uintmax_t)size, error);
2747 error = bus_dmamap_load(dma->dma_tag, dma->dma_map, dma->dma_vaddr,
2748 size, igb_dmamap_cb, &dma->dma_paddr, mapflags | BUS_DMA_NOWAIT);
2749 if (error || dma->dma_paddr == 0) {
2750 device_printf(adapter->dev,
2751 "%s: bus_dmamap_load failed: %d\n",
2759 bus_dmamap_unload(dma->dma_tag, dma->dma_map);
2761 bus_dmamem_free(dma->dma_tag, dma->dma_vaddr, dma->dma_map);
2762 bus_dma_tag_destroy(dma->dma_tag);
2764 dma->dma_map = NULL;
2765 dma->dma_tag = NULL;
2771 igb_dma_free(struct adapter *adapter, struct igb_dma_alloc *dma)
2773 if (dma->dma_tag == NULL)
2775 if (dma->dma_map != NULL) {
2776 bus_dmamap_sync(dma->dma_tag, dma->dma_map,
2777 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
2778 bus_dmamap_unload(dma->dma_tag, dma->dma_map);
2779 bus_dmamem_free(dma->dma_tag, dma->dma_vaddr, dma->dma_map);
2780 dma->dma_map = NULL;
2782 bus_dma_tag_destroy(dma->dma_tag);
2783 dma->dma_tag = NULL;
2787 /*********************************************************************
2789 * Allocate memory for the transmit and receive rings, and then
2790 * the descriptors associated with each, called only once at attach.
2792 **********************************************************************/
2794 igb_allocate_queues(struct adapter *adapter)
2796 device_t dev = adapter->dev;
2797 struct igb_queue *que = NULL;
2798 struct tx_ring *txr = NULL;
2799 struct rx_ring *rxr = NULL;
2800 int rsize, tsize, error = E1000_SUCCESS;
2801 int txconf = 0, rxconf = 0;
2803 /* First allocate the top level queue structs */
2804 if (!(adapter->queues =
2805 (struct igb_queue *) malloc(sizeof(struct igb_queue) *
2806 adapter->num_queues, M_DEVBUF, M_NOWAIT | M_ZERO))) {
2807 device_printf(dev, "Unable to allocate queue memory\n");
2812 /* Next allocate the TX ring struct memory */
2813 if (!(adapter->tx_rings =
2814 (struct tx_ring *) malloc(sizeof(struct tx_ring) *
2815 adapter->num_queues, M_DEVBUF, M_NOWAIT | M_ZERO))) {
2816 device_printf(dev, "Unable to allocate TX ring memory\n");
2821 /* Now allocate the RX */
2822 if (!(adapter->rx_rings =
2823 (struct rx_ring *) malloc(sizeof(struct rx_ring) *
2824 adapter->num_queues, M_DEVBUF, M_NOWAIT | M_ZERO))) {
2825 device_printf(dev, "Unable to allocate RX ring memory\n");
2830 tsize = roundup2(adapter->num_tx_desc *
2831 sizeof(union e1000_adv_tx_desc), IGB_DBA_ALIGN);
2833 * Now set up the TX queues, txconf is needed to handle the
2834 * possibility that things fail midcourse and we need to
2835 * undo memory gracefully
2837 for (int i = 0; i < adapter->num_queues; i++, txconf++) {
2838 /* Set up some basics */
2839 txr = &adapter->tx_rings[i];
2840 txr->adapter = adapter;
2843 /* Initialize the TX lock */
2844 snprintf(txr->mtx_name, sizeof(txr->mtx_name), "%s:tx(%d)",
2845 device_get_nameunit(dev), txr->me);
2846 mtx_init(&txr->tx_mtx, txr->mtx_name, NULL, MTX_DEF);
2848 if (igb_dma_malloc(adapter, tsize,
2849 &txr->txdma, BUS_DMA_NOWAIT)) {
2851 "Unable to allocate TX Descriptor memory\n");
2855 txr->tx_base = (struct e1000_tx_desc *)txr->txdma.dma_vaddr;
2856 bzero((void *)txr->tx_base, tsize);
2858 /* Now allocate transmit buffers for the ring */
2859 if (igb_allocate_transmit_buffers(txr)) {
2861 "Critical Failure setting up transmit buffers\n");
2865 #if __FreeBSD_version >= 800000
2866 /* Allocate a buf ring */
2867 txr->br = buf_ring_alloc(IGB_BR_SIZE, M_DEVBUF,
2868 M_WAITOK, &txr->tx_mtx);
2873 * Next the RX queues...
2875 rsize = roundup2(adapter->num_rx_desc *
2876 sizeof(union e1000_adv_rx_desc), IGB_DBA_ALIGN);
2877 for (int i = 0; i < adapter->num_queues; i++, rxconf++) {
2878 rxr = &adapter->rx_rings[i];
2879 rxr->adapter = adapter;
2882 /* Initialize the RX lock */
2883 snprintf(rxr->mtx_name, sizeof(rxr->mtx_name), "%s:rx(%d)",
2884 device_get_nameunit(dev), txr->me);
2885 mtx_init(&rxr->rx_mtx, rxr->mtx_name, NULL, MTX_DEF);
2887 if (igb_dma_malloc(adapter, rsize,
2888 &rxr->rxdma, BUS_DMA_NOWAIT)) {
2890 "Unable to allocate RxDescriptor memory\n");
2894 rxr->rx_base = (union e1000_adv_rx_desc *)rxr->rxdma.dma_vaddr;
2895 bzero((void *)rxr->rx_base, rsize);
2897 /* Allocate receive buffers for the ring*/
2898 if (igb_allocate_receive_buffers(rxr)) {
2900 "Critical Failure setting up receive buffers\n");
2907 ** Finally set up the queue holding structs
2909 for (int i = 0; i < adapter->num_queues; i++) {
2910 que = &adapter->queues[i];
2911 que->adapter = adapter;
2912 que->txr = &adapter->tx_rings[i];
2913 que->rxr = &adapter->rx_rings[i];
2919 for (rxr = adapter->rx_rings; rxconf > 0; rxr++, rxconf--)
2920 igb_dma_free(adapter, &rxr->rxdma);
2922 for (txr = adapter->tx_rings; txconf > 0; txr++, txconf--)
2923 igb_dma_free(adapter, &txr->txdma);
2924 free(adapter->rx_rings, M_DEVBUF);
2926 #if __FreeBSD_version >= 800000
2927 buf_ring_free(txr->br, M_DEVBUF);
2929 free(adapter->tx_rings, M_DEVBUF);
2931 free(adapter->queues, M_DEVBUF);
2936 /*********************************************************************
2938 * Allocate memory for tx_buffer structures. The tx_buffer stores all
2939 * the information needed to transmit a packet on the wire. This is
2940 * called only once at attach, setup is done every reset.
2942 **********************************************************************/
2944 igb_allocate_transmit_buffers(struct tx_ring *txr)
2946 struct adapter *adapter = txr->adapter;
2947 device_t dev = adapter->dev;
2948 struct igb_tx_buffer *txbuf;
2952 * Setup DMA descriptor areas.
2954 if ((error = bus_dma_tag_create(bus_get_dma_tag(dev),
2955 1, 0, /* alignment, bounds */
2956 BUS_SPACE_MAXADDR, /* lowaddr */
2957 BUS_SPACE_MAXADDR, /* highaddr */
2958 NULL, NULL, /* filter, filterarg */
2959 IGB_TSO_SIZE, /* maxsize */
2960 IGB_MAX_SCATTER, /* nsegments */
2961 PAGE_SIZE, /* maxsegsize */
2963 NULL, /* lockfunc */
2964 NULL, /* lockfuncarg */
2966 device_printf(dev,"Unable to allocate TX DMA tag\n");
2970 if (!(txr->tx_buffers =
2971 (struct igb_tx_buffer *) malloc(sizeof(struct igb_tx_buffer) *
2972 adapter->num_tx_desc, M_DEVBUF, M_NOWAIT | M_ZERO))) {
2973 device_printf(dev, "Unable to allocate tx_buffer memory\n");
2978 /* Create the descriptor buffer dma maps */
2979 txbuf = txr->tx_buffers;
2980 for (i = 0; i < adapter->num_tx_desc; i++, txbuf++) {
2981 error = bus_dmamap_create(txr->txtag, 0, &txbuf->map);
2983 device_printf(dev, "Unable to create TX DMA map\n");
2990 /* We free all, it handles case where we are in the middle */
2991 igb_free_transmit_structures(adapter);
2995 /*********************************************************************
2997 * Initialize a transmit ring.
2999 **********************************************************************/
3001 igb_setup_transmit_ring(struct tx_ring *txr)
3003 struct adapter *adapter = txr->adapter;
3004 struct igb_tx_buffer *txbuf;
3007 /* Clear the old descriptor contents */
3009 bzero((void *)txr->tx_base,
3010 (sizeof(union e1000_adv_tx_desc)) * adapter->num_tx_desc);
3012 txr->next_avail_desc = 0;
3013 txr->next_to_clean = 0;
3015 /* Free any existing tx buffers. */
3016 txbuf = txr->tx_buffers;
3017 for (i = 0; i < adapter->num_tx_desc; i++, txbuf++) {
3018 if (txbuf->m_head != NULL) {
3019 bus_dmamap_sync(txr->txtag, txbuf->map,
3020 BUS_DMASYNC_POSTWRITE);
3021 bus_dmamap_unload(txr->txtag, txbuf->map);
3022 m_freem(txbuf->m_head);
3023 txbuf->m_head = NULL;
3025 /* clear the watch index */
3026 txbuf->next_eop = -1;
3029 /* Set number of descriptors available */
3030 txr->tx_avail = adapter->num_tx_desc;
3032 bus_dmamap_sync(txr->txdma.dma_tag, txr->txdma.dma_map,
3033 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
3037 /*********************************************************************
3039 * Initialize all transmit rings.
3041 **********************************************************************/
3043 igb_setup_transmit_structures(struct adapter *adapter)
3045 struct tx_ring *txr = adapter->tx_rings;
3047 for (int i = 0; i < adapter->num_queues; i++, txr++)
3048 igb_setup_transmit_ring(txr);
3053 /*********************************************************************
3055 * Enable transmit unit.
3057 **********************************************************************/
3059 igb_initialize_transmit_units(struct adapter *adapter)
3061 struct tx_ring *txr = adapter->tx_rings;
3062 struct e1000_hw *hw = &adapter->hw;
3065 INIT_DEBUGOUT("igb_initialize_transmit_units: begin");
3067 /* Setup the Tx Descriptor Rings */
3068 for (int i = 0; i < adapter->num_queues; i++, txr++) {
3069 u64 bus_addr = txr->txdma.dma_paddr;
3071 E1000_WRITE_REG(hw, E1000_TDLEN(i),
3072 adapter->num_tx_desc * sizeof(struct e1000_tx_desc));
3073 E1000_WRITE_REG(hw, E1000_TDBAH(i),
3074 (uint32_t)(bus_addr >> 32));
3075 E1000_WRITE_REG(hw, E1000_TDBAL(i),
3076 (uint32_t)bus_addr);
3078 /* Setup the HW Tx Head and Tail descriptor pointers */
3079 E1000_WRITE_REG(hw, E1000_TDT(i), 0);
3080 E1000_WRITE_REG(hw, E1000_TDH(i), 0);
3082 HW_DEBUGOUT2("Base = %x, Length = %x\n",
3083 E1000_READ_REG(hw, E1000_TDBAL(i)),
3084 E1000_READ_REG(hw, E1000_TDLEN(i)));
3086 txr->watchdog_check = FALSE;
3088 txdctl = E1000_READ_REG(hw, E1000_TXDCTL(i));
3089 txdctl |= IGB_TX_PTHRESH;
3090 txdctl |= IGB_TX_HTHRESH << 8;
3091 txdctl |= IGB_TX_WTHRESH << 16;
3092 txdctl |= E1000_TXDCTL_QUEUE_ENABLE;
3093 E1000_WRITE_REG(hw, E1000_TXDCTL(i), txdctl);
3096 /* Program the Transmit Control Register */
3097 tctl = E1000_READ_REG(hw, E1000_TCTL);
3098 tctl &= ~E1000_TCTL_CT;
3099 tctl |= (E1000_TCTL_PSP | E1000_TCTL_RTLC | E1000_TCTL_EN |
3100 (E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT));
3102 e1000_config_collision_dist(hw);
3104 /* This write will effectively turn on the transmit unit. */
3105 E1000_WRITE_REG(hw, E1000_TCTL, tctl);
3108 /*********************************************************************
3110 * Free all transmit rings.
3112 **********************************************************************/
3114 igb_free_transmit_structures(struct adapter *adapter)
3116 struct tx_ring *txr = adapter->tx_rings;
3118 for (int i = 0; i < adapter->num_queues; i++, txr++) {
3120 igb_free_transmit_buffers(txr);
3121 igb_dma_free(adapter, &txr->txdma);
3123 IGB_TX_LOCK_DESTROY(txr);
3125 free(adapter->tx_rings, M_DEVBUF);
3128 /*********************************************************************
3130 * Free transmit ring related data structures.
3132 **********************************************************************/
3134 igb_free_transmit_buffers(struct tx_ring *txr)
3136 struct adapter *adapter = txr->adapter;
3137 struct igb_tx_buffer *tx_buffer;
3140 INIT_DEBUGOUT("free_transmit_ring: begin");
3142 if (txr->tx_buffers == NULL)
3145 tx_buffer = txr->tx_buffers;
3146 for (i = 0; i < adapter->num_tx_desc; i++, tx_buffer++) {
3147 if (tx_buffer->m_head != NULL) {
3148 bus_dmamap_sync(txr->txtag, tx_buffer->map,
3149 BUS_DMASYNC_POSTWRITE);
3150 bus_dmamap_unload(txr->txtag,
3152 m_freem(tx_buffer->m_head);
3153 tx_buffer->m_head = NULL;
3154 if (tx_buffer->map != NULL) {
3155 bus_dmamap_destroy(txr->txtag,
3157 tx_buffer->map = NULL;
3159 } else if (tx_buffer->map != NULL) {
3160 bus_dmamap_unload(txr->txtag,
3162 bus_dmamap_destroy(txr->txtag,
3164 tx_buffer->map = NULL;
3167 #if __FreeBSD_version >= 800000
3168 if (txr->br != NULL)
3169 buf_ring_free(txr->br, M_DEVBUF);
3171 if (txr->tx_buffers != NULL) {
3172 free(txr->tx_buffers, M_DEVBUF);
3173 txr->tx_buffers = NULL;
3175 if (txr->txtag != NULL) {
3176 bus_dma_tag_destroy(txr->txtag);
3182 /**********************************************************************
3184 * Setup work for hardware segmentation offload (TSO)
3186 **********************************************************************/
3188 igb_tso_setup(struct tx_ring *txr, struct mbuf *mp, u32 *hdrlen)
3190 struct adapter *adapter = txr->adapter;
3191 struct e1000_adv_tx_context_desc *TXD;
3192 struct igb_tx_buffer *tx_buffer;
3193 u32 vlan_macip_lens = 0, type_tucmd_mlhl = 0;
3194 u32 mss_l4len_idx = 0;
3196 int ctxd, ehdrlen, ip_hlen, tcp_hlen;
3197 struct ether_vlan_header *eh;
3203 * Determine where frame payload starts.
3204 * Jump over vlan headers if already present
3206 eh = mtod(mp, struct ether_vlan_header *);
3207 if (eh->evl_encap_proto == htons(ETHERTYPE_VLAN))
3208 ehdrlen = ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN;
3210 ehdrlen = ETHER_HDR_LEN;
3212 /* Ensure we have at least the IP+TCP header in the first mbuf. */
3213 if (mp->m_len < ehdrlen + sizeof(struct ip) + sizeof(struct tcphdr))
3216 /* Only supports IPV4 for now */
3217 ctxd = txr->next_avail_desc;
3218 tx_buffer = &txr->tx_buffers[ctxd];
3219 TXD = (struct e1000_adv_tx_context_desc *) &txr->tx_base[ctxd];
3221 ip = (struct ip *)(mp->m_data + ehdrlen);
3222 if (ip->ip_p != IPPROTO_TCP)
3223 return FALSE; /* 0 */
3225 ip_hlen = ip->ip_hl << 2;
3226 th = (struct tcphdr *)((caddr_t)ip + ip_hlen);
3227 th->th_sum = in_pseudo(ip->ip_src.s_addr,
3228 ip->ip_dst.s_addr, htons(IPPROTO_TCP));
3229 tcp_hlen = th->th_off << 2;
3231 * Calculate header length, this is used
3232 * in the transmit desc in igb_xmit
3234 *hdrlen = ehdrlen + ip_hlen + tcp_hlen;
3236 /* VLAN MACLEN IPLEN */
3237 if (mp->m_flags & M_VLANTAG) {
3238 vtag = htole16(mp->m_pkthdr.ether_vtag);
3239 vlan_macip_lens |= (vtag << E1000_ADVTXD_VLAN_SHIFT);
3242 vlan_macip_lens |= (ehdrlen << E1000_ADVTXD_MACLEN_SHIFT);
3243 vlan_macip_lens |= ip_hlen;
3244 TXD->vlan_macip_lens |= htole32(vlan_macip_lens);
3246 /* ADV DTYPE TUCMD */
3247 type_tucmd_mlhl |= E1000_ADVTXD_DCMD_DEXT | E1000_ADVTXD_DTYP_CTXT;
3248 type_tucmd_mlhl |= E1000_ADVTXD_TUCMD_L4T_TCP;
3249 type_tucmd_mlhl |= E1000_ADVTXD_TUCMD_IPV4;
3250 TXD->type_tucmd_mlhl |= htole32(type_tucmd_mlhl);
3253 mss_l4len_idx |= (mp->m_pkthdr.tso_segsz << E1000_ADVTXD_MSS_SHIFT);
3254 mss_l4len_idx |= (tcp_hlen << E1000_ADVTXD_L4LEN_SHIFT);
3255 /* 82575 needs the queue index added */
3256 if (adapter->hw.mac.type == e1000_82575)
3257 mss_l4len_idx |= txr->me << 4;
3258 TXD->mss_l4len_idx = htole32(mss_l4len_idx);
3260 TXD->seqnum_seed = htole32(0);
3261 tx_buffer->m_head = NULL;
3262 tx_buffer->next_eop = -1;
3264 if (++ctxd == adapter->num_tx_desc)
3268 txr->next_avail_desc = ctxd;
3273 /*********************************************************************
3275 * Context Descriptor setup for VLAN or CSUM
3277 **********************************************************************/
3280 igb_tx_ctx_setup(struct tx_ring *txr, struct mbuf *mp)
3282 struct adapter *adapter = txr->adapter;
3283 struct e1000_adv_tx_context_desc *TXD;
3284 struct igb_tx_buffer *tx_buffer;
3285 u32 vlan_macip_lens, type_tucmd_mlhl, mss_l4len_idx;
3286 struct ether_vlan_header *eh;
3287 struct ip *ip = NULL;
3288 struct ip6_hdr *ip6;
3289 int ehdrlen, ctxd, ip_hlen = 0;
3290 u16 etype, vtag = 0;
3292 bool offload = TRUE;
3294 if ((mp->m_pkthdr.csum_flags & CSUM_OFFLOAD) == 0)
3297 vlan_macip_lens = type_tucmd_mlhl = mss_l4len_idx = 0;
3298 ctxd = txr->next_avail_desc;
3299 tx_buffer = &txr->tx_buffers[ctxd];
3300 TXD = (struct e1000_adv_tx_context_desc *) &txr->tx_base[ctxd];
3303 ** In advanced descriptors the vlan tag must
3304 ** be placed into the context descriptor, thus
3305 ** we need to be here just for that setup.
3307 if (mp->m_flags & M_VLANTAG) {
3308 vtag = htole16(mp->m_pkthdr.ether_vtag);
3309 vlan_macip_lens |= (vtag << E1000_ADVTXD_VLAN_SHIFT);
3310 } else if (offload == FALSE)
3314 * Determine where frame payload starts.
3315 * Jump over vlan headers if already present,
3316 * helpful for QinQ too.
3318 eh = mtod(mp, struct ether_vlan_header *);
3319 if (eh->evl_encap_proto == htons(ETHERTYPE_VLAN)) {
3320 etype = ntohs(eh->evl_proto);
3321 ehdrlen = ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN;
3323 etype = ntohs(eh->evl_encap_proto);
3324 ehdrlen = ETHER_HDR_LEN;
3327 /* Set the ether header length */
3328 vlan_macip_lens |= ehdrlen << E1000_ADVTXD_MACLEN_SHIFT;
3332 ip = (struct ip *)(mp->m_data + ehdrlen);
3333 ip_hlen = ip->ip_hl << 2;
3334 if (mp->m_len < ehdrlen + ip_hlen) {
3339 type_tucmd_mlhl |= E1000_ADVTXD_TUCMD_IPV4;
3341 case ETHERTYPE_IPV6:
3342 ip6 = (struct ip6_hdr *)(mp->m_data + ehdrlen);
3343 ip_hlen = sizeof(struct ip6_hdr);
3344 if (mp->m_len < ehdrlen + ip_hlen)
3346 ipproto = ip6->ip6_nxt;
3347 type_tucmd_mlhl |= E1000_ADVTXD_TUCMD_IPV6;
3354 vlan_macip_lens |= ip_hlen;
3355 type_tucmd_mlhl |= E1000_ADVTXD_DCMD_DEXT | E1000_ADVTXD_DTYP_CTXT;
3359 if (mp->m_pkthdr.csum_flags & CSUM_TCP)
3360 type_tucmd_mlhl |= E1000_ADVTXD_TUCMD_L4T_TCP;
3363 if (mp->m_pkthdr.csum_flags & CSUM_UDP)
3364 type_tucmd_mlhl |= E1000_ADVTXD_TUCMD_L4T_UDP;
3366 #if __FreeBSD_version >= 800000
3368 if (mp->m_pkthdr.csum_flags & CSUM_SCTP)
3369 type_tucmd_mlhl |= E1000_ADVTXD_TUCMD_L4T_SCTP;
3377 /* 82575 needs the queue index added */
3378 if (adapter->hw.mac.type == e1000_82575)
3379 mss_l4len_idx = txr->me << 4;
3381 /* Now copy bits into descriptor */
3382 TXD->vlan_macip_lens |= htole32(vlan_macip_lens);
3383 TXD->type_tucmd_mlhl |= htole32(type_tucmd_mlhl);
3384 TXD->seqnum_seed = htole32(0);
3385 TXD->mss_l4len_idx = htole32(mss_l4len_idx);
3387 tx_buffer->m_head = NULL;
3388 tx_buffer->next_eop = -1;
3390 /* We've consumed the first desc, adjust counters */
3391 if (++ctxd == adapter->num_tx_desc)
3393 txr->next_avail_desc = ctxd;
3400 /**********************************************************************
3402 * Examine each tx_buffer in the used queue. If the hardware is done
3403 * processing the packet then free associated resources. The
3404 * tx_buffer is put back on the free queue.
3406 * TRUE return means there's work in the ring to clean, FALSE its empty.
3407 **********************************************************************/
3409 igb_txeof(struct tx_ring *txr)
3411 struct adapter *adapter = txr->adapter;
3412 int first, last, done;
3413 struct igb_tx_buffer *tx_buffer;
3414 struct e1000_tx_desc *tx_desc, *eop_desc;
3415 struct ifnet *ifp = adapter->ifp;
3417 IGB_TX_LOCK_ASSERT(txr);
3419 if (txr->tx_avail == adapter->num_tx_desc)
3422 first = txr->next_to_clean;
3423 tx_desc = &txr->tx_base[first];
3424 tx_buffer = &txr->tx_buffers[first];
3425 last = tx_buffer->next_eop;
3426 eop_desc = &txr->tx_base[last];
3429 * What this does is get the index of the
3430 * first descriptor AFTER the EOP of the
3431 * first packet, that way we can do the
3432 * simple comparison on the inner while loop.
3434 if (++last == adapter->num_tx_desc)
3438 bus_dmamap_sync(txr->txdma.dma_tag, txr->txdma.dma_map,
3439 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
3441 while (eop_desc->upper.fields.status & E1000_TXD_STAT_DD) {
3442 /* We clean the range of the packet */
3443 while (first != done) {
3444 tx_desc->upper.data = 0;
3445 tx_desc->lower.data = 0;
3446 tx_desc->buffer_addr = 0;
3449 if (tx_buffer->m_head) {
3451 tx_buffer->m_head->m_pkthdr.len;
3452 bus_dmamap_sync(txr->txtag,
3454 BUS_DMASYNC_POSTWRITE);
3455 bus_dmamap_unload(txr->txtag,
3458 m_freem(tx_buffer->m_head);
3459 tx_buffer->m_head = NULL;
3461 tx_buffer->next_eop = -1;
3462 txr->watchdog_time = ticks;
3464 if (++first == adapter->num_tx_desc)
3467 tx_buffer = &txr->tx_buffers[first];
3468 tx_desc = &txr->tx_base[first];
3472 /* See if we can continue to the next packet */
3473 last = tx_buffer->next_eop;
3475 eop_desc = &txr->tx_base[last];
3476 /* Get new done point */
3477 if (++last == adapter->num_tx_desc) last = 0;
3482 bus_dmamap_sync(txr->txdma.dma_tag, txr->txdma.dma_map,
3483 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
3485 txr->next_to_clean = first;
3488 * If we have enough room, clear IFF_DRV_OACTIVE
3489 * to tell the stack that it is OK to send packets.
3491 if (txr->tx_avail > IGB_TX_CLEANUP_THRESHOLD) {
3492 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
3493 /* All clean, turn off the watchdog */
3494 if (txr->tx_avail == adapter->num_tx_desc) {
3495 txr->watchdog_check = FALSE;
3504 /*********************************************************************
3506 * Setup descriptor buffer(s) from system mbuf buffer pools.
3507 * i - designates the ring index
3508 * clean - tells the function whether to update
3509 * the header, the packet buffer, or both.
3511 **********************************************************************/
3513 igb_get_buf(struct rx_ring *rxr, int i, u8 clean)
3515 struct adapter *adapter = rxr->adapter;
3516 struct igb_rx_buf *rxbuf;
3517 struct mbuf *mh, *mp;
3518 bus_dma_segment_t hseg[1];
3519 bus_dma_segment_t pseg[1];
3524 rxbuf = &rxr->rx_buffers[i];
3526 if ((clean & IGB_CLEAN_HEADER) != 0) {
3527 mh = m_gethdr(M_DONTWAIT, MT_DATA);
3529 adapter->mbuf_header_failed++;
3532 mh->m_pkthdr.len = mh->m_len = MHLEN;
3534 * Because IGB_HDR_BUF size is less than MHLEN
3535 * and we configure controller to split headers
3536 * we can align mbuf on ETHER_ALIGN boundary.
3538 m_adj(mh, ETHER_ALIGN);
3539 error = bus_dmamap_load_mbuf_sg(rxr->rx_htag,
3540 rxr->rx_hspare_map, mh, hseg, &nsegs, 0);
3545 mh->m_flags &= ~M_PKTHDR;
3547 if ((clean & IGB_CLEAN_PAYLOAD) != 0) {
3548 mp = m_getjcl(M_DONTWAIT, MT_DATA, M_PKTHDR,
3549 adapter->rx_mbuf_sz);
3552 adapter->mbuf_packet_failed++;
3553 bus_dmamap_unload(rxr->rx_htag,
3555 mh->m_flags |= M_PKTHDR;
3560 mp->m_pkthdr.len = mp->m_len = adapter->rx_mbuf_sz;
3561 error = bus_dmamap_load_mbuf_sg(rxr->rx_ptag,
3562 rxr->rx_pspare_map, mp, pseg, &nsegs, 0);
3565 bus_dmamap_unload(rxr->rx_htag,
3567 mh->m_flags |= M_PKTHDR;
3573 mp->m_flags &= ~M_PKTHDR;
3576 /* Loading new DMA maps complete, unload maps for received buffers. */
3577 if ((clean & IGB_CLEAN_HEADER) != 0 && rxbuf->m_head != NULL) {
3578 bus_dmamap_sync(rxr->rx_htag, rxbuf->head_map,
3579 BUS_DMASYNC_POSTREAD);
3580 bus_dmamap_unload(rxr->rx_htag, rxbuf->head_map);
3582 if ((clean & IGB_CLEAN_PAYLOAD) != 0 && rxbuf->m_pack != NULL) {
3583 bus_dmamap_sync(rxr->rx_ptag, rxbuf->pack_map,
3584 BUS_DMASYNC_POSTREAD);
3585 bus_dmamap_unload(rxr->rx_ptag, rxbuf->pack_map);
3588 /* Reflect loaded dmamaps. */
3589 if ((clean & IGB_CLEAN_HEADER) != 0) {
3590 map = rxbuf->head_map;
3591 rxbuf->head_map = rxr->rx_hspare_map;
3592 rxr->rx_hspare_map = map;
3594 bus_dmamap_sync(rxr->rx_htag, rxbuf->head_map,
3595 BUS_DMASYNC_PREREAD);
3596 rxr->rx_base[i].read.hdr_addr = htole64(hseg[0].ds_addr);
3598 if ((clean & IGB_CLEAN_PAYLOAD) != 0) {
3599 map = rxbuf->pack_map;
3600 rxbuf->pack_map = rxr->rx_pspare_map;
3601 rxr->rx_pspare_map = map;
3603 bus_dmamap_sync(rxr->rx_ptag, rxbuf->pack_map,
3604 BUS_DMASYNC_PREREAD);
3605 rxr->rx_base[i].read.pkt_addr = htole64(pseg[0].ds_addr);
3611 /*********************************************************************
3613 * Allocate memory for rx_buffer structures. Since we use one
3614 * rx_buffer per received packet, the maximum number of rx_buffer's
3615 * that we'll need is equal to the number of receive descriptors
3616 * that we've allocated.
3618 **********************************************************************/
3620 igb_allocate_receive_buffers(struct rx_ring *rxr)
3622 struct adapter *adapter = rxr->adapter;
3623 device_t dev = adapter->dev;
3624 struct igb_rx_buf *rxbuf;
3625 int i, bsize, error;
3627 bsize = sizeof(struct igb_rx_buf) * adapter->num_rx_desc;
3628 if (!(rxr->rx_buffers =
3629 (struct igb_rx_buf *) malloc(bsize,
3630 M_DEVBUF, M_NOWAIT | M_ZERO))) {
3631 device_printf(dev, "Unable to allocate rx_buffer memory\n");
3636 if ((error = bus_dma_tag_create(bus_get_dma_tag(dev),
3637 1, 0, /* alignment, bounds */
3638 BUS_SPACE_MAXADDR, /* lowaddr */
3639 BUS_SPACE_MAXADDR, /* highaddr */
3640 NULL, NULL, /* filter, filterarg */
3641 MSIZE, /* maxsize */
3643 MSIZE, /* maxsegsize */
3645 NULL, /* lockfunc */
3646 NULL, /* lockfuncarg */
3648 device_printf(dev, "Unable to create RX DMA tag\n");
3652 if ((error = bus_dma_tag_create(bus_get_dma_tag(dev),
3653 1, 0, /* alignment, bounds */
3654 BUS_SPACE_MAXADDR, /* lowaddr */
3655 BUS_SPACE_MAXADDR, /* highaddr */
3656 NULL, NULL, /* filter, filterarg */
3657 MJUMPAGESIZE, /* maxsize */
3659 MJUMPAGESIZE, /* maxsegsize */
3661 NULL, /* lockfunc */
3662 NULL, /* lockfuncarg */
3664 device_printf(dev, "Unable to create RX payload DMA tag\n");
3668 /* Create the spare maps (used by getbuf) */
3669 error = bus_dmamap_create(rxr->rx_htag, BUS_DMA_NOWAIT,
3670 &rxr->rx_hspare_map);
3673 "%s: bus_dmamap_create header spare failed: %d\n",
3677 error = bus_dmamap_create(rxr->rx_ptag, BUS_DMA_NOWAIT,
3678 &rxr->rx_pspare_map);
3681 "%s: bus_dmamap_create packet spare failed: %d\n",
3686 for (i = 0; i < adapter->num_rx_desc; i++) {
3687 rxbuf = &rxr->rx_buffers[i];
3688 error = bus_dmamap_create(rxr->rx_htag,
3689 BUS_DMA_NOWAIT, &rxbuf->head_map);
3692 "Unable to create RX head DMA maps\n");
3695 error = bus_dmamap_create(rxr->rx_ptag,
3696 BUS_DMA_NOWAIT, &rxbuf->pack_map);
3699 "Unable to create RX packet DMA maps\n");
3707 /* Frees all, but can handle partial completion */
3708 igb_free_receive_structures(adapter);
3714 igb_free_receive_ring(struct rx_ring *rxr)
3716 struct adapter *adapter;
3717 struct igb_rx_buf *rxbuf;
3720 adapter = rxr->adapter;
3721 for (i = 0; i < adapter->num_rx_desc; i++) {
3722 rxbuf = &rxr->rx_buffers[i];
3723 if (rxbuf->m_head != NULL) {
3724 bus_dmamap_sync(rxr->rx_htag, rxbuf->head_map,
3725 BUS_DMASYNC_POSTREAD);
3726 bus_dmamap_unload(rxr->rx_htag, rxbuf->head_map);
3727 rxbuf->m_head->m_flags |= M_PKTHDR;
3728 m_freem(rxbuf->m_head);
3730 if (rxbuf->m_pack != NULL) {
3731 bus_dmamap_sync(rxr->rx_ptag, rxbuf->pack_map,
3732 BUS_DMASYNC_POSTREAD);
3733 bus_dmamap_unload(rxr->rx_ptag, rxbuf->pack_map);
3734 rxbuf->m_pack->m_flags |= M_PKTHDR;
3735 m_freem(rxbuf->m_pack);
3737 rxbuf->m_head = NULL;
3738 rxbuf->m_pack = NULL;
3743 /*********************************************************************
3745 * Initialize a receive ring and its buffers.
3747 **********************************************************************/
3749 igb_setup_receive_ring(struct rx_ring *rxr)
3751 struct adapter *adapter;
3754 struct lro_ctrl *lro = &rxr->lro;
3755 int j, rsize, error = 0;
3757 adapter = rxr->adapter;
3761 /* Clear the ring contents */
3763 rsize = roundup2(adapter->num_rx_desc *
3764 sizeof(union e1000_adv_rx_desc), IGB_DBA_ALIGN);
3765 bzero((void *)rxr->rx_base, rsize);
3768 ** Free current RX buffer structures and their mbufs
3770 igb_free_receive_ring(rxr);
3772 /* Now replenish the ring mbufs */
3773 for (j = 0; j < adapter->num_rx_desc; j++) {
3774 if ((error = igb_get_buf(rxr, j, IGB_CLEAN_BOTH)) != 0)
3778 /* Setup our descriptor indices */
3779 rxr->next_to_check = 0;
3780 rxr->last_cleaned = 0;
3781 rxr->lro_enabled = FALSE;
3783 if (igb_header_split)
3784 rxr->hdr_split = TRUE;
3786 ifp->if_capabilities &= ~IFCAP_LRO;
3790 rxr->discard = FALSE;
3792 bus_dmamap_sync(rxr->rxdma.dma_tag, rxr->rxdma.dma_map,
3793 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
3796 ** Now set up the LRO interface, we
3797 ** also only do head split when LRO
3798 ** is enabled, since so often they
3799 ** are undesireable in similar setups.
3801 if (ifp->if_capenable & IFCAP_LRO) {
3802 int err = tcp_lro_init(lro);
3804 device_printf(dev, "LRO Initialization failed!\n");
3807 INIT_DEBUGOUT("RX LRO Initialized\n");
3808 rxr->lro_enabled = TRUE;
3809 lro->ifp = adapter->ifp;
3816 igb_free_receive_ring(rxr);
3821 /*********************************************************************
3823 * Initialize all receive rings.
3825 **********************************************************************/
3827 igb_setup_receive_structures(struct adapter *adapter)
3829 struct rx_ring *rxr = adapter->rx_rings;
3832 for (i = 0; i < adapter->num_queues; i++, rxr++)
3833 if (igb_setup_receive_ring(rxr))
3839 * Free RX buffers allocated so far, we will only handle
3840 * the rings that completed, the failing case will have
3841 * cleaned up for itself. The value of 'i' will be the
3842 * failed ring so we must pre-decrement it.
3844 rxr = adapter->rx_rings;
3845 for (--i; i > 0; i--, rxr++) {
3846 for (j = 0; j < adapter->num_rx_desc; j++)
3847 igb_free_receive_ring(rxr);
3853 /*********************************************************************
3855 * Enable receive unit.
3857 **********************************************************************/
3859 igb_initialize_receive_units(struct adapter *adapter)
3861 struct rx_ring *rxr = adapter->rx_rings;
3862 struct ifnet *ifp = adapter->ifp;
3863 struct e1000_hw *hw = &adapter->hw;
3864 u32 rctl, rxcsum, psize, srrctl = 0;
3866 INIT_DEBUGOUT("igb_initialize_receive_unit: begin");
3869 * Make sure receives are disabled while setting
3870 * up the descriptor ring
3872 rctl = E1000_READ_REG(hw, E1000_RCTL);
3873 E1000_WRITE_REG(hw, E1000_RCTL, rctl & ~E1000_RCTL_EN);
3876 ** Set up for header split
3878 if (rxr->hdr_split) {
3879 /* Use a standard mbuf for the header */
3880 srrctl |= IGB_HDR_BUF << E1000_SRRCTL_BSIZEHDRSIZE_SHIFT;
3881 srrctl |= E1000_SRRCTL_DESCTYPE_HDR_SPLIT_ALWAYS;
3883 srrctl |= E1000_SRRCTL_DESCTYPE_ADV_ONEBUF;
3886 ** Set up for jumbo frames
3888 if (ifp->if_mtu > ETHERMTU) {
3889 rctl |= E1000_RCTL_LPE;
3890 srrctl |= 4096 >> E1000_SRRCTL_BSIZEPKT_SHIFT;
3891 rctl |= E1000_RCTL_SZ_4096 | E1000_RCTL_BSEX;
3893 /* Set maximum packet len */
3894 psize = adapter->max_frame_size;
3895 /* are we on a vlan? */
3896 if (adapter->ifp->if_vlantrunk != NULL)
3897 psize += VLAN_TAG_SIZE;
3898 E1000_WRITE_REG(&adapter->hw, E1000_RLPML, psize);
3900 rctl &= ~E1000_RCTL_LPE;
3901 srrctl |= 2048 >> E1000_SRRCTL_BSIZEPKT_SHIFT;
3902 rctl |= E1000_RCTL_SZ_2048;
3905 /* Setup the Base and Length of the Rx Descriptor Rings */
3906 for (int i = 0; i < adapter->num_queues; i++, rxr++) {
3907 u64 bus_addr = rxr->rxdma.dma_paddr;
3910 E1000_WRITE_REG(hw, E1000_RDLEN(i),
3911 adapter->num_rx_desc * sizeof(struct e1000_rx_desc));
3912 E1000_WRITE_REG(hw, E1000_RDBAH(i),
3913 (uint32_t)(bus_addr >> 32));
3914 E1000_WRITE_REG(hw, E1000_RDBAL(i),
3915 (uint32_t)bus_addr);
3916 E1000_WRITE_REG(hw, E1000_SRRCTL(i), srrctl);
3917 /* Enable this Queue */
3918 rxdctl = E1000_READ_REG(hw, E1000_RXDCTL(i));
3919 rxdctl |= E1000_RXDCTL_QUEUE_ENABLE;
3920 rxdctl &= 0xFFF00000;
3921 rxdctl |= IGB_RX_PTHRESH;
3922 rxdctl |= IGB_RX_HTHRESH << 8;
3923 rxdctl |= IGB_RX_WTHRESH << 16;
3924 E1000_WRITE_REG(hw, E1000_RXDCTL(i), rxdctl);
3928 ** Setup for RX MultiQueue
3930 rxcsum = E1000_READ_REG(hw, E1000_RXCSUM);
3931 if (adapter->num_queues >1) {
3932 u32 random[10], mrqc, shift = 0;
3938 arc4rand(&random, sizeof(random), 0);
3939 if (adapter->hw.mac.type == e1000_82575)
3941 /* Warning FM follows */
3942 for (int i = 0; i < 128; i++) {
3944 (i % adapter->num_queues) << shift;
3947 E1000_RETA(i >> 2), reta.dword);
3949 /* Now fill in hash table */
3950 mrqc = E1000_MRQC_ENABLE_RSS_4Q;
3951 for (int i = 0; i < 10; i++)
3952 E1000_WRITE_REG_ARRAY(hw,
3953 E1000_RSSRK(0), i, random[i]);
3955 mrqc |= (E1000_MRQC_RSS_FIELD_IPV4 |
3956 E1000_MRQC_RSS_FIELD_IPV4_TCP);
3957 mrqc |= (E1000_MRQC_RSS_FIELD_IPV6 |
3958 E1000_MRQC_RSS_FIELD_IPV6_TCP);
3959 mrqc |=( E1000_MRQC_RSS_FIELD_IPV4_UDP |
3960 E1000_MRQC_RSS_FIELD_IPV6_UDP);
3961 mrqc |=( E1000_MRQC_RSS_FIELD_IPV6_UDP_EX |
3962 E1000_MRQC_RSS_FIELD_IPV6_TCP_EX);
3964 E1000_WRITE_REG(hw, E1000_MRQC, mrqc);
3967 ** NOTE: Receive Full-Packet Checksum Offload
3968 ** is mutually exclusive with Multiqueue. However
3969 ** this is not the same as TCP/IP checksums which
3972 rxcsum |= E1000_RXCSUM_PCSD;
3973 #if __FreeBSD_version >= 800000
3974 /* For SCTP Offload */
3975 if ((hw->mac.type == e1000_82576)
3976 && (ifp->if_capenable & IFCAP_RXCSUM))
3977 rxcsum |= E1000_RXCSUM_CRCOFL;
3981 if (ifp->if_capenable & IFCAP_RXCSUM) {
3982 rxcsum |= E1000_RXCSUM_IPPCSE;
3983 #if __FreeBSD_version >= 800000
3984 if (adapter->hw.mac.type == e1000_82576)
3985 rxcsum |= E1000_RXCSUM_CRCOFL;
3988 rxcsum &= ~E1000_RXCSUM_TUOFL;
3990 E1000_WRITE_REG(hw, E1000_RXCSUM, rxcsum);
3992 /* Setup the Receive Control Register */
3993 rctl &= ~(3 << E1000_RCTL_MO_SHIFT);
3994 rctl |= E1000_RCTL_EN | E1000_RCTL_BAM | E1000_RCTL_LBM_NO |
3995 E1000_RCTL_RDMTS_HALF |
3996 (hw->mac.mc_filter_type << E1000_RCTL_MO_SHIFT);
3997 /* Strip CRC bytes. */
3998 rctl |= E1000_RCTL_SECRC;
3999 /* Make sure VLAN Filters are off */
4000 rctl &= ~E1000_RCTL_VFE;
4001 /* Don't store bad packets */
4002 rctl &= ~E1000_RCTL_SBP;
4004 /* Enable Receives */
4005 E1000_WRITE_REG(hw, E1000_RCTL, rctl);
4008 * Setup the HW Rx Head and Tail Descriptor Pointers
4009 * - needs to be after enable
4011 for (int i = 0; i < adapter->num_queues; i++) {
4012 E1000_WRITE_REG(hw, E1000_RDH(i), 0);
4013 E1000_WRITE_REG(hw, E1000_RDT(i),
4014 adapter->num_rx_desc - 1);
4019 /*********************************************************************
4021 * Free receive rings.
4023 **********************************************************************/
4025 igb_free_receive_structures(struct adapter *adapter)
4027 struct rx_ring *rxr = adapter->rx_rings;
4029 for (int i = 0; i < adapter->num_queues; i++, rxr++) {
4030 struct lro_ctrl *lro = &rxr->lro;
4031 igb_free_receive_buffers(rxr);
4033 igb_dma_free(adapter, &rxr->rxdma);
4036 free(adapter->rx_rings, M_DEVBUF);
4039 /*********************************************************************
4041 * Free receive ring data structures.
4043 **********************************************************************/
4045 igb_free_receive_buffers(struct rx_ring *rxr)
4047 struct adapter *adapter = rxr->adapter;
4048 struct igb_rx_buf *rxbuf;
4051 INIT_DEBUGOUT("free_receive_structures: begin");
4053 if (rxr->rx_hspare_map != NULL) {
4054 bus_dmamap_destroy(rxr->rx_htag, rxr->rx_hspare_map);
4055 rxr->rx_hspare_map = NULL;
4058 if (rxr->rx_hspare_map != NULL) {
4059 bus_dmamap_destroy(rxr->rx_ptag, rxr->rx_pspare_map);
4060 rxr->rx_pspare_map = NULL;
4063 /* Cleanup any existing buffers */
4064 if (rxr->rx_buffers != NULL) {
4065 for (i = 0; i < adapter->num_rx_desc; i++) {
4066 rxbuf = &rxr->rx_buffers[i];
4067 if (rxbuf->m_head != NULL) {
4068 bus_dmamap_sync(rxr->rx_htag, rxbuf->head_map,
4069 BUS_DMASYNC_POSTREAD);
4070 bus_dmamap_unload(rxr->rx_htag,
4072 rxbuf->m_head->m_flags |= M_PKTHDR;
4073 m_freem(rxbuf->m_head);
4075 if (rxbuf->m_pack != NULL) {
4076 bus_dmamap_sync(rxr->rx_ptag, rxbuf->pack_map,
4077 BUS_DMASYNC_POSTREAD);
4078 bus_dmamap_unload(rxr->rx_ptag,
4080 rxbuf->m_pack->m_flags |= M_PKTHDR;
4081 m_freem(rxbuf->m_pack);
4083 rxbuf->m_head = NULL;
4084 rxbuf->m_pack = NULL;
4085 if (rxbuf->head_map != NULL) {
4086 bus_dmamap_destroy(rxr->rx_htag,
4088 rxbuf->head_map = NULL;
4090 if (rxbuf->pack_map != NULL) {
4091 bus_dmamap_destroy(rxr->rx_ptag,
4093 rxbuf->pack_map = NULL;
4096 if (rxr->rx_buffers != NULL) {
4097 free(rxr->rx_buffers, M_DEVBUF);
4098 rxr->rx_buffers = NULL;
4102 if (rxr->rx_htag != NULL) {
4103 bus_dma_tag_destroy(rxr->rx_htag);
4104 rxr->rx_htag = NULL;
4106 if (rxr->rx_ptag != NULL) {
4107 bus_dma_tag_destroy(rxr->rx_ptag);
4108 rxr->rx_ptag = NULL;
4112 static __inline void
4113 igb_rx_discard(struct rx_ring *rxr, union e1000_adv_rx_desc *cur, int i)
4116 if (rxr->fmp != NULL) {
4117 rxr->fmp->m_flags |= M_PKTHDR;
4124 static __inline void
4125 igb_rx_input(struct rx_ring *rxr, struct ifnet *ifp, struct mbuf *m, u32 ptype)
4129 * ATM LRO is only for IPv4/TCP packets and TCP checksum of the packet
4130 * should be computed by hardware. Also it should not have VLAN tag in
4133 if (rxr->lro_enabled &&
4134 (ifp->if_capenable & IFCAP_VLAN_HWTAGGING) != 0 &&
4135 (ptype & E1000_RXDADV_PKTTYPE_ETQF) == 0 &&
4136 (ptype & (E1000_RXDADV_PKTTYPE_IPV4 | E1000_RXDADV_PKTTYPE_TCP)) ==
4137 (E1000_RXDADV_PKTTYPE_IPV4 | E1000_RXDADV_PKTTYPE_TCP) &&
4138 (m->m_pkthdr.csum_flags & (CSUM_DATA_VALID | CSUM_PSEUDO_HDR)) ==
4139 (CSUM_DATA_VALID | CSUM_PSEUDO_HDR)) {
4141 * Send to the stack if:
4142 ** - LRO not enabled, or
4143 ** - no LRO resources, or
4144 ** - lro enqueue fails
4146 if (rxr->lro.lro_cnt != 0)
4147 if (tcp_lro_rx(&rxr->lro, m, 0) == 0)
4150 (*ifp->if_input)(ifp, m);
4153 /*********************************************************************
4155 * This routine executes in interrupt context. It replenishes
4156 * the mbufs in the descriptor and sends data which has been
4157 * dma'ed into host memory to upper layer.
4159 * We loop at most count times if count is > 0, or until done if
4162 * Return TRUE if more to clean, FALSE otherwise
4163 *********************************************************************/
4165 igb_rxeof(struct rx_ring *rxr, int count)
4167 struct adapter *adapter = rxr->adapter;
4168 struct ifnet *ifp = adapter->ifp;
4169 struct lro_ctrl *lro = &rxr->lro;
4170 struct lro_entry *queued;
4172 u32 ptype, staterr = 0;
4173 union e1000_adv_rx_desc *cur;
4177 /* Main clean loop */
4178 for (i = rxr->next_to_check; count > 0; prog++) {
4179 struct mbuf *sendmp, *mh, *mp;
4180 u16 hlen, plen, hdr, vtag;
4184 /* Sync the ring. */
4185 bus_dmamap_sync(rxr->rxdma.dma_tag, rxr->rxdma.dma_map,
4186 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
4187 cur = &rxr->rx_base[i];
4188 staterr = le32toh(cur->wb.upper.status_error);
4189 if ((staterr & E1000_RXD_STAT_DD) == 0)
4191 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
4194 sendmp = mh = mp = NULL;
4195 cur->wb.upper.status_error = 0;
4196 plen = le16toh(cur->wb.upper.length);
4197 ptype = le32toh(cur->wb.lower.lo_dword.data) & IGB_PKTTYPE_MASK;
4198 hdr = le16toh(cur->wb.lower.lo_dword.hs_rss.hdr_info);
4199 eop = ((staterr & E1000_RXD_STAT_EOP) == E1000_RXD_STAT_EOP);
4201 /* Make sure all segments of a bad packet are discarded */
4202 if (((staterr & E1000_RXDEXT_ERR_FRAME_ERR_MASK) != 0) ||
4205 ++rxr->rx_discarded;
4206 if (!eop) /* Catch subsequent segs */
4207 rxr->discard = TRUE;
4209 rxr->discard = FALSE;
4210 igb_rx_discard(rxr, cur, i);
4215 ** The way the hardware is configured to
4216 ** split, it will ONLY use the header buffer
4217 ** when header split is enabled, otherwise we
4218 ** get normal behavior, ie, both header and
4219 ** payload are DMA'd into the payload buffer.
4221 ** The fmp test is to catch the case where a
4222 ** packet spans multiple descriptors, in that
4223 ** case only the first header is valid.
4225 if (rxr->hdr_split && rxr->fmp == NULL) {
4226 hlen = (hdr & E1000_RXDADV_HDRBUFLEN_MASK) >>
4227 E1000_RXDADV_HDRBUFLEN_SHIFT;
4228 if (hlen > IGB_HDR_BUF)
4230 /* Handle the header mbuf */
4231 mh = rxr->rx_buffers[i].m_head;
4233 dopayload = IGB_CLEAN_HEADER;
4235 ** Get the payload length, this
4236 ** could be zero if its a small
4240 mp = rxr->rx_buffers[i].m_pack;
4243 dopayload = IGB_CLEAN_BOTH;
4244 rxr->rx_split_packets++;
4248 ** Either no header split, or a
4249 ** secondary piece of a fragmented
4252 mh = rxr->rx_buffers[i].m_pack;
4254 dopayload = IGB_CLEAN_PAYLOAD;
4258 ** get_buf will overwrite the writeback
4259 ** descriptor so save the VLAN tag now.
4261 vtag = le16toh(cur->wb.upper.vlan);
4262 if (igb_get_buf(rxr, i, dopayload) != 0) {
4265 * We've dropped a frame due to lack of resources
4266 * so we should drop entire multi-segmented
4267 * frames until we encounter EOP.
4269 if ((staterr & E1000_RXD_STAT_EOP) != 0)
4270 rxr->discard = TRUE;
4271 igb_rx_discard(rxr, cur, i);
4275 /* Initial frame - setup */
4276 if (rxr->fmp == NULL) {
4277 mh->m_pkthdr.len = mh->m_len;
4278 /* Store the first mbuf */
4282 /* Add payload if split */
4283 mh->m_pkthdr.len += mp->m_len;
4284 rxr->lmp = mh->m_next;
4287 /* Chain mbuf's together */
4288 rxr->lmp->m_next = mh;
4289 rxr->lmp = rxr->lmp->m_next;
4290 rxr->fmp->m_pkthdr.len += mh->m_len;
4294 rxr->fmp->m_pkthdr.rcvif = ifp;
4297 /* capture data for AIM */
4299 rxr->bytes += rxr->fmp->m_pkthdr.len;
4300 rxr->rx_bytes += rxr->fmp->m_pkthdr.len;
4302 if ((ifp->if_capenable & IFCAP_RXCSUM) != 0)
4303 igb_rx_checksum(staterr, rxr->fmp, ptype);
4304 /* XXX igb(4) always strips VLAN. */
4305 if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) != 0 &&
4306 (staterr & E1000_RXD_STAT_VP) != 0) {
4307 rxr->fmp->m_pkthdr.ether_vtag = vtag;
4308 rxr->fmp->m_flags |= M_VLANTAG;
4310 #if __FreeBSD_version >= 800000
4311 rxr->fmp->m_pkthdr.flowid = curcpu;
4312 rxr->fmp->m_flags |= M_FLOWID;
4315 /* Make sure to set M_PKTHDR. */
4316 sendmp->m_flags |= M_PKTHDR;
4322 bus_dmamap_sync(rxr->rxdma.dma_tag, rxr->rxdma.dma_map,
4323 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
4325 rxr->last_cleaned = i; /* For updating tail */
4327 /* Advance our pointers to the next descriptor. */
4328 if (++i == adapter->num_rx_desc)
4332 ** Note that we hold the RX lock thru
4333 ** the following call so this ring's
4334 ** next_to_check is not gonna change.
4337 igb_rx_input(rxr, ifp, sendmp, ptype);
4345 rxr->next_to_check = i;
4347 /* Advance the E1000's Receive Queue "Tail Pointer". */
4348 E1000_WRITE_REG(&adapter->hw, E1000_RDT(rxr->me), rxr->last_cleaned);
4351 * Flush any outstanding LRO work
4353 while ((queued = SLIST_FIRST(&lro->lro_active)) != NULL) {
4354 SLIST_REMOVE_HEAD(&lro->lro_active, next);
4355 tcp_lro_flush(lro, queued);
4361 ** We still have cleaning to do?
4362 ** Schedule another interrupt if so.
4364 if ((staterr & E1000_RXD_STAT_DD) != 0)
4370 /*********************************************************************
4372 * Verify that the hardware indicated that the checksum is valid.
4373 * Inform the stack about the status of checksum so that stack
4374 * doesn't spend time verifying the checksum.
4376 *********************************************************************/
4378 igb_rx_checksum(u32 staterr, struct mbuf *mp, u32 ptype)
4380 u16 status = (u16)staterr;
4381 u8 errors = (u8) (staterr >> 24);
4384 /* Ignore Checksum bit is set */
4385 if (status & E1000_RXD_STAT_IXSM) {
4386 mp->m_pkthdr.csum_flags = 0;
4390 if ((ptype & E1000_RXDADV_PKTTYPE_ETQF) == 0 &&
4391 (ptype & E1000_RXDADV_PKTTYPE_SCTP) != 0)
4395 if (status & E1000_RXD_STAT_IPCS) {
4397 if (!(errors & E1000_RXD_ERR_IPE)) {
4398 /* IP Checksum Good */
4399 mp->m_pkthdr.csum_flags = CSUM_IP_CHECKED;
4400 mp->m_pkthdr.csum_flags |= CSUM_IP_VALID;
4402 mp->m_pkthdr.csum_flags = 0;
4405 if (status & (E1000_RXD_STAT_TCPCS | E1000_RXD_STAT_UDPCS)) {
4406 u16 type = (CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
4407 #if __FreeBSD_version >= 800000
4408 if (sctp) /* reassign */
4409 type = CSUM_SCTP_VALID;
4412 if (!(errors & E1000_RXD_ERR_TCPE)) {
4413 mp->m_pkthdr.csum_flags |= type;
4415 mp->m_pkthdr.csum_data = htons(0xffff);
4422 * This routine is run via an vlan
4426 igb_register_vlan(void *arg, struct ifnet *ifp, u16 vtag)
4428 struct adapter *adapter = ifp->if_softc;
4431 if (ifp->if_softc != arg) /* Not our event */
4434 if ((vtag == 0) || (vtag > 4095)) /* Invalid */
4437 index = (vtag >> 5) & 0x7F;
4439 igb_shadow_vfta[index] |= (1 << bit);
4440 ++adapter->num_vlans;
4441 /* Re-init to load the changes */
4446 * This routine is run via an vlan
4450 igb_unregister_vlan(void *arg, struct ifnet *ifp, u16 vtag)
4452 struct adapter *adapter = ifp->if_softc;
4455 if (ifp->if_softc != arg)
4458 if ((vtag == 0) || (vtag > 4095)) /* Invalid */
4461 index = (vtag >> 5) & 0x7F;
4463 igb_shadow_vfta[index] &= ~(1 << bit);
4464 --adapter->num_vlans;
4465 /* Re-init to load the changes */
4470 igb_setup_vlan_hw_support(struct adapter *adapter)
4472 struct e1000_hw *hw = &adapter->hw;
4476 ** We get here thru init_locked, meaning
4477 ** a soft reset, this has already cleared
4478 ** the VFTA and other state, so if there
4479 ** have been no vlan's registered do nothing.
4481 if (adapter->num_vlans == 0)
4485 ** A soft reset zero's out the VFTA, so
4486 ** we need to repopulate it now.
4488 for (int i = 0; i < IGB_VFTA_SIZE; i++)
4489 if (igb_shadow_vfta[i] != 0)
4490 E1000_WRITE_REG_ARRAY(hw, E1000_VFTA,
4491 i, igb_shadow_vfta[i]);
4493 reg = E1000_READ_REG(hw, E1000_CTRL);
4494 reg |= E1000_CTRL_VME;
4495 E1000_WRITE_REG(hw, E1000_CTRL, reg);
4497 /* Enable the Filter Table */
4498 reg = E1000_READ_REG(hw, E1000_RCTL);
4499 reg &= ~E1000_RCTL_CFIEN;
4500 reg |= E1000_RCTL_VFE;
4501 E1000_WRITE_REG(hw, E1000_RCTL, reg);
4503 /* Update the frame size */
4504 E1000_WRITE_REG(&adapter->hw, E1000_RLPML,
4505 adapter->max_frame_size + VLAN_TAG_SIZE);
4509 igb_enable_intr(struct adapter *adapter)
4511 /* With RSS set up what to auto clear */
4512 if (adapter->msix_mem) {
4513 E1000_WRITE_REG(&adapter->hw, E1000_EIAC,
4514 adapter->eims_mask);
4515 E1000_WRITE_REG(&adapter->hw, E1000_EIAM,
4516 adapter->eims_mask);
4517 E1000_WRITE_REG(&adapter->hw, E1000_EIMS,
4518 adapter->eims_mask);
4519 E1000_WRITE_REG(&adapter->hw, E1000_IMS,
4522 E1000_WRITE_REG(&adapter->hw, E1000_IMS,
4525 E1000_WRITE_FLUSH(&adapter->hw);
4531 igb_disable_intr(struct adapter *adapter)
4533 if (adapter->msix_mem) {
4534 E1000_WRITE_REG(&adapter->hw, E1000_EIMC, ~0);
4535 E1000_WRITE_REG(&adapter->hw, E1000_EIAC, 0);
4537 E1000_WRITE_REG(&adapter->hw, E1000_IMC, ~0);
4538 E1000_WRITE_FLUSH(&adapter->hw);
4543 * Bit of a misnomer, what this really means is
4544 * to enable OS management of the system... aka
4545 * to disable special hardware management features
4548 igb_init_manageability(struct adapter *adapter)
4550 if (adapter->has_manage) {
4551 int manc2h = E1000_READ_REG(&adapter->hw, E1000_MANC2H);
4552 int manc = E1000_READ_REG(&adapter->hw, E1000_MANC);
4554 /* disable hardware interception of ARP */
4555 manc &= ~(E1000_MANC_ARP_EN);
4557 /* enable receiving management packets to the host */
4558 manc |= E1000_MANC_EN_MNG2HOST;
4559 manc2h |= 1 << 5; /* Mng Port 623 */
4560 manc2h |= 1 << 6; /* Mng Port 664 */
4561 E1000_WRITE_REG(&adapter->hw, E1000_MANC2H, manc2h);
4562 E1000_WRITE_REG(&adapter->hw, E1000_MANC, manc);
4567 * Give control back to hardware management
4568 * controller if there is one.
4571 igb_release_manageability(struct adapter *adapter)
4573 if (adapter->has_manage) {
4574 int manc = E1000_READ_REG(&adapter->hw, E1000_MANC);
4576 /* re-enable hardware interception of ARP */
4577 manc |= E1000_MANC_ARP_EN;
4578 manc &= ~E1000_MANC_EN_MNG2HOST;
4580 E1000_WRITE_REG(&adapter->hw, E1000_MANC, manc);
4585 * igb_get_hw_control sets CTRL_EXT:DRV_LOAD bit.
4586 * For ASF and Pass Through versions of f/w this means that
4587 * the driver is loaded.
4591 igb_get_hw_control(struct adapter *adapter)
4595 /* Let firmware know the driver has taken over */
4596 ctrl_ext = E1000_READ_REG(&adapter->hw, E1000_CTRL_EXT);
4597 E1000_WRITE_REG(&adapter->hw, E1000_CTRL_EXT,
4598 ctrl_ext | E1000_CTRL_EXT_DRV_LOAD);
4602 * igb_release_hw_control resets CTRL_EXT:DRV_LOAD bit.
4603 * For ASF and Pass Through versions of f/w this means that the
4604 * driver is no longer loaded.
4608 igb_release_hw_control(struct adapter *adapter)
4612 /* Let firmware taken over control of h/w */
4613 ctrl_ext = E1000_READ_REG(&adapter->hw, E1000_CTRL_EXT);
4614 E1000_WRITE_REG(&adapter->hw, E1000_CTRL_EXT,
4615 ctrl_ext & ~E1000_CTRL_EXT_DRV_LOAD);
4619 igb_is_valid_ether_addr(uint8_t *addr)
4621 char zero_addr[6] = { 0, 0, 0, 0, 0, 0 };
4623 if ((addr[0] & 1) || (!bcmp(addr, zero_addr, ETHER_ADDR_LEN))) {
4632 * Enable PCI Wake On Lan capability
4635 igb_enable_wakeup(device_t dev)
4640 /* First find the capabilities pointer*/
4641 cap = pci_read_config(dev, PCIR_CAP_PTR, 2);
4642 /* Read the PM Capabilities */
4643 id = pci_read_config(dev, cap, 1);
4644 if (id != PCIY_PMG) /* Something wrong */
4646 /* OK, we have the power capabilities, so
4647 now get the status register */
4648 cap += PCIR_POWER_STATUS;
4649 status = pci_read_config(dev, cap, 2);
4650 status |= PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE;
4651 pci_write_config(dev, cap, status, 2);
4656 /**********************************************************************
4658 * Update the board statistics counters.
4660 **********************************************************************/
4662 igb_update_stats_counters(struct adapter *adapter)
4666 if(adapter->hw.phy.media_type == e1000_media_type_copper ||
4667 (E1000_READ_REG(&adapter->hw, E1000_STATUS) & E1000_STATUS_LU)) {
4668 adapter->stats.symerrs += E1000_READ_REG(&adapter->hw, E1000_SYMERRS);
4669 adapter->stats.sec += E1000_READ_REG(&adapter->hw, E1000_SEC);
4671 adapter->stats.crcerrs += E1000_READ_REG(&adapter->hw, E1000_CRCERRS);
4672 adapter->stats.mpc += E1000_READ_REG(&adapter->hw, E1000_MPC);
4673 adapter->stats.scc += E1000_READ_REG(&adapter->hw, E1000_SCC);
4674 adapter->stats.ecol += E1000_READ_REG(&adapter->hw, E1000_ECOL);
4676 adapter->stats.mcc += E1000_READ_REG(&adapter->hw, E1000_MCC);
4677 adapter->stats.latecol += E1000_READ_REG(&adapter->hw, E1000_LATECOL);
4678 adapter->stats.colc += E1000_READ_REG(&adapter->hw, E1000_COLC);
4679 adapter->stats.dc += E1000_READ_REG(&adapter->hw, E1000_DC);
4680 adapter->stats.rlec += E1000_READ_REG(&adapter->hw, E1000_RLEC);
4681 adapter->stats.xonrxc += E1000_READ_REG(&adapter->hw, E1000_XONRXC);
4682 adapter->stats.xontxc += E1000_READ_REG(&adapter->hw, E1000_XONTXC);
4683 adapter->stats.xoffrxc += E1000_READ_REG(&adapter->hw, E1000_XOFFRXC);
4684 adapter->stats.xofftxc += E1000_READ_REG(&adapter->hw, E1000_XOFFTXC);
4685 adapter->stats.fcruc += E1000_READ_REG(&adapter->hw, E1000_FCRUC);
4686 adapter->stats.prc64 += E1000_READ_REG(&adapter->hw, E1000_PRC64);
4687 adapter->stats.prc127 += E1000_READ_REG(&adapter->hw, E1000_PRC127);
4688 adapter->stats.prc255 += E1000_READ_REG(&adapter->hw, E1000_PRC255);
4689 adapter->stats.prc511 += E1000_READ_REG(&adapter->hw, E1000_PRC511);
4690 adapter->stats.prc1023 += E1000_READ_REG(&adapter->hw, E1000_PRC1023);
4691 adapter->stats.prc1522 += E1000_READ_REG(&adapter->hw, E1000_PRC1522);
4692 adapter->stats.gprc += E1000_READ_REG(&adapter->hw, E1000_GPRC);
4693 adapter->stats.bprc += E1000_READ_REG(&adapter->hw, E1000_BPRC);
4694 adapter->stats.mprc += E1000_READ_REG(&adapter->hw, E1000_MPRC);
4695 adapter->stats.gptc += E1000_READ_REG(&adapter->hw, E1000_GPTC);
4697 /* For the 64-bit byte counters the low dword must be read first. */
4698 /* Both registers clear on the read of the high dword */
4700 adapter->stats.gorc += E1000_READ_REG(&adapter->hw, E1000_GORCH);
4701 adapter->stats.gotc += E1000_READ_REG(&adapter->hw, E1000_GOTCH);
4703 adapter->stats.rnbc += E1000_READ_REG(&adapter->hw, E1000_RNBC);
4704 adapter->stats.ruc += E1000_READ_REG(&adapter->hw, E1000_RUC);
4705 adapter->stats.rfc += E1000_READ_REG(&adapter->hw, E1000_RFC);
4706 adapter->stats.roc += E1000_READ_REG(&adapter->hw, E1000_ROC);
4707 adapter->stats.rjc += E1000_READ_REG(&adapter->hw, E1000_RJC);
4709 adapter->stats.tor += E1000_READ_REG(&adapter->hw, E1000_TORH);
4710 adapter->stats.tot += E1000_READ_REG(&adapter->hw, E1000_TOTH);
4712 adapter->stats.tpr += E1000_READ_REG(&adapter->hw, E1000_TPR);
4713 adapter->stats.tpt += E1000_READ_REG(&adapter->hw, E1000_TPT);
4714 adapter->stats.ptc64 += E1000_READ_REG(&adapter->hw, E1000_PTC64);
4715 adapter->stats.ptc127 += E1000_READ_REG(&adapter->hw, E1000_PTC127);
4716 adapter->stats.ptc255 += E1000_READ_REG(&adapter->hw, E1000_PTC255);
4717 adapter->stats.ptc511 += E1000_READ_REG(&adapter->hw, E1000_PTC511);
4718 adapter->stats.ptc1023 += E1000_READ_REG(&adapter->hw, E1000_PTC1023);
4719 adapter->stats.ptc1522 += E1000_READ_REG(&adapter->hw, E1000_PTC1522);
4720 adapter->stats.mptc += E1000_READ_REG(&adapter->hw, E1000_MPTC);
4721 adapter->stats.bptc += E1000_READ_REG(&adapter->hw, E1000_BPTC);
4723 adapter->stats.algnerrc +=
4724 E1000_READ_REG(&adapter->hw, E1000_ALGNERRC);
4725 adapter->stats.rxerrc +=
4726 E1000_READ_REG(&adapter->hw, E1000_RXERRC);
4727 adapter->stats.tncrs +=
4728 E1000_READ_REG(&adapter->hw, E1000_TNCRS);
4729 adapter->stats.cexterr +=
4730 E1000_READ_REG(&adapter->hw, E1000_CEXTERR);
4731 adapter->stats.tsctc +=
4732 E1000_READ_REG(&adapter->hw, E1000_TSCTC);
4733 adapter->stats.tsctfc +=
4734 E1000_READ_REG(&adapter->hw, E1000_TSCTFC);
4737 ifp->if_collisions = adapter->stats.colc;
4740 ifp->if_ierrors = adapter->dropped_pkts + adapter->stats.rxerrc +
4741 adapter->stats.crcerrs + adapter->stats.algnerrc +
4742 adapter->stats.ruc + adapter->stats.roc +
4743 adapter->stats.mpc + adapter->stats.cexterr;
4746 ifp->if_oerrors = adapter->stats.ecol +
4747 adapter->stats.latecol + adapter->watchdog_events;
4751 /**********************************************************************
4753 * This routine is called only when igb_display_debug_stats is enabled.
4754 * This routine provides a way to take a look at important statistics
4755 * maintained by the driver and hardware.
4757 **********************************************************************/
4759 igb_print_debug_info(struct adapter *adapter)
4761 device_t dev = adapter->dev;
4762 struct igb_queue *que = adapter->queues;
4763 struct rx_ring *rxr = adapter->rx_rings;
4764 struct tx_ring *txr = adapter->tx_rings;
4765 uint8_t *hw_addr = adapter->hw.hw_addr;
4767 device_printf(dev, "Adapter hardware address = %p \n", hw_addr);
4768 device_printf(dev, "CTRL = 0x%x RCTL = 0x%x \n",
4769 E1000_READ_REG(&adapter->hw, E1000_CTRL),
4770 E1000_READ_REG(&adapter->hw, E1000_RCTL));
4772 #if (DEBUG_HW > 0) /* Dont output these errors normally */
4773 device_printf(dev, "IMS = 0x%x EIMS = 0x%x \n",
4774 E1000_READ_REG(&adapter->hw, E1000_IMS),
4775 E1000_READ_REG(&adapter->hw, E1000_EIMS));
4778 device_printf(dev, "Packet buffer = Tx=%dk Rx=%dk \n",
4779 ((E1000_READ_REG(&adapter->hw, E1000_PBA) & 0xffff0000) >> 16),\
4780 (E1000_READ_REG(&adapter->hw, E1000_PBA) & 0xffff) );
4781 device_printf(dev, "Flow control watermarks high = %d low = %d\n",
4782 adapter->hw.fc.high_water,
4783 adapter->hw.fc.low_water);
4785 for (int i = 0; i < adapter->num_queues; i++, rxr++, txr++) {
4786 device_printf(dev, "Queue(%d) tdh = %d, tdt = %d ", i,
4787 E1000_READ_REG(&adapter->hw, E1000_TDH(i)),
4788 E1000_READ_REG(&adapter->hw, E1000_TDT(i)));
4789 device_printf(dev, "rdh = %d, rdt = %d\n",
4790 E1000_READ_REG(&adapter->hw, E1000_RDH(i)),
4791 E1000_READ_REG(&adapter->hw, E1000_RDT(i)));
4792 device_printf(dev, "TX(%d) no descriptors avail event = %lld\n",
4793 txr->me, (long long)txr->no_desc_avail);
4794 device_printf(dev, "TX(%d) Packets sent = %lld\n",
4795 txr->me, (long long)txr->tx_packets);
4796 device_printf(dev, "RX(%d) Packets received = %lld ",
4797 rxr->me, (long long)rxr->rx_packets);
4800 for (int i = 0; i < adapter->num_queues; i++, rxr++) {
4801 struct lro_ctrl *lro = &rxr->lro;
4802 device_printf(dev, "Queue(%d) rdh = %d, rdt = %d\n", i,
4803 E1000_READ_REG(&adapter->hw, E1000_RDH(i)),
4804 E1000_READ_REG(&adapter->hw, E1000_RDT(i)));
4805 device_printf(dev, "RX(%d) Packets received = %lld\n", rxr->me,
4806 (long long)rxr->rx_packets);
4807 device_printf(dev, " Split Packets = %lld ",
4808 (long long)rxr->rx_split_packets);
4809 device_printf(dev, " Byte count = %lld\n",
4810 (long long)rxr->rx_bytes);
4811 device_printf(dev,"RX(%d) LRO Queued= %d ",
4812 i, lro->lro_queued);
4813 device_printf(dev,"LRO Flushed= %d\n",lro->lro_flushed);
4816 for (int i = 0; i < adapter->num_queues; i++, que++)
4817 device_printf(dev,"QUE(%d) IRQs = %llx\n",
4818 i, (long long)que->irqs);
4820 device_printf(dev, "LINK MSIX IRQ Handled = %u\n", adapter->link_irq);
4821 device_printf(dev, "Mbuf defrag failed = %ld\n",
4822 adapter->mbuf_defrag_failed);
4823 device_printf(dev, "Std mbuf header failed = %ld\n",
4824 adapter->mbuf_header_failed);
4825 device_printf(dev, "Std mbuf packet failed = %ld\n",
4826 adapter->mbuf_packet_failed);
4827 device_printf(dev, "Driver dropped packets = %ld\n",
4828 adapter->dropped_pkts);
4829 device_printf(dev, "Driver tx dma failure in xmit = %ld\n",
4830 adapter->no_tx_dma_setup);
4834 igb_print_hw_stats(struct adapter *adapter)
4836 device_t dev = adapter->dev;
4838 device_printf(dev, "Excessive collisions = %lld\n",
4839 (long long)adapter->stats.ecol);
4840 #if (DEBUG_HW > 0) /* Dont output these errors normally */
4841 device_printf(dev, "Symbol errors = %lld\n",
4842 (long long)adapter->stats.symerrs);
4844 device_printf(dev, "Sequence errors = %lld\n",
4845 (long long)adapter->stats.sec);
4846 device_printf(dev, "Defer count = %lld\n",
4847 (long long)adapter->stats.dc);
4848 device_printf(dev, "Missed Packets = %lld\n",
4849 (long long)adapter->stats.mpc);
4850 device_printf(dev, "Receive No Buffers = %lld\n",
4851 (long long)adapter->stats.rnbc);
4852 /* RLEC is inaccurate on some hardware, calculate our own. */
4853 device_printf(dev, "Receive Length Errors = %lld\n",
4854 ((long long)adapter->stats.roc + (long long)adapter->stats.ruc));
4855 device_printf(dev, "Receive errors = %lld\n",
4856 (long long)adapter->stats.rxerrc);
4857 device_printf(dev, "Crc errors = %lld\n",
4858 (long long)adapter->stats.crcerrs);
4859 device_printf(dev, "Alignment errors = %lld\n",
4860 (long long)adapter->stats.algnerrc);
4861 /* On 82575 these are collision counts */
4862 device_printf(dev, "Collision/Carrier extension errors = %lld\n",
4863 (long long)adapter->stats.cexterr);
4864 device_printf(dev, "RX overruns = %ld\n", adapter->rx_overruns);
4865 device_printf(dev, "watchdog timeouts = %ld\n",
4866 adapter->watchdog_events);
4867 device_printf(dev, "XON Rcvd = %lld\n",
4868 (long long)adapter->stats.xonrxc);
4869 device_printf(dev, "XON Xmtd = %lld\n",
4870 (long long)adapter->stats.xontxc);
4871 device_printf(dev, "XOFF Rcvd = %lld\n",
4872 (long long)adapter->stats.xoffrxc);
4873 device_printf(dev, "XOFF Xmtd = %lld\n",
4874 (long long)adapter->stats.xofftxc);
4875 device_printf(dev, "Good Packets Rcvd = %lld\n",
4876 (long long)adapter->stats.gprc);
4877 device_printf(dev, "Good Packets Xmtd = %lld\n",
4878 (long long)adapter->stats.gptc);
4879 device_printf(dev, "TSO Contexts Xmtd = %lld\n",
4880 (long long)adapter->stats.tsctc);
4881 device_printf(dev, "TSO Contexts Failed = %lld\n",
4882 (long long)adapter->stats.tsctfc);
4885 /**********************************************************************
4887 * This routine provides a way to dump out the adapter eeprom,
4888 * often a useful debug/service tool. This only dumps the first
4889 * 32 words, stuff that matters is in that extent.
4891 **********************************************************************/
4893 igb_print_nvm_info(struct adapter *adapter)
4898 /* Its a bit crude, but it gets the job done */
4899 printf("\nInterface EEPROM Dump:\n");
4900 printf("Offset\n0x0000 ");
4901 for (i = 0, j = 0; i < 32; i++, j++) {
4902 if (j == 8) { /* Make the offset block */
4904 printf("\n0x00%x0 ",row);
4906 e1000_read_nvm(&adapter->hw, i, 1, &eeprom_data);
4907 printf("%04x ", eeprom_data);
4913 igb_sysctl_debug_info(SYSCTL_HANDLER_ARGS)
4915 struct adapter *adapter;
4920 error = sysctl_handle_int(oidp, &result, 0, req);
4922 if (error || !req->newptr)
4926 adapter = (struct adapter *)arg1;
4927 igb_print_debug_info(adapter);
4930 * This value will cause a hex dump of the
4931 * first 32 16-bit words of the EEPROM to
4935 adapter = (struct adapter *)arg1;
4936 igb_print_nvm_info(adapter);
4944 igb_sysctl_stats(SYSCTL_HANDLER_ARGS)
4946 struct adapter *adapter;
4951 error = sysctl_handle_int(oidp, &result, 0, req);
4953 if (error || !req->newptr)
4957 adapter = (struct adapter *)arg1;
4958 igb_print_hw_stats(adapter);
4965 igb_add_rx_process_limit(struct adapter *adapter, const char *name,
4966 const char *description, int *limit, int value)
4969 SYSCTL_ADD_INT(device_get_sysctl_ctx(adapter->dev),
4970 SYSCTL_CHILDREN(device_get_sysctl_tree(adapter->dev)),
4971 OID_AUTO, name, CTLTYPE_INT|CTLFLAG_RW, limit, value, description);