1 /******************************************************************************
3 Copyright (c) 2001-2010, Intel Corporation
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
9 1. Redistributions of source code must retain the above copyright notice,
10 this list of conditions and the following disclaimer.
12 2. Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in the
14 documentation and/or other materials provided with the distribution.
16 3. Neither the name of the Intel Corporation nor the names of its
17 contributors may be used to endorse or promote products derived from
18 this software without specific prior written permission.
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 POSSIBILITY OF SUCH DAMAGE.
32 ******************************************************************************/
35 #include "opt_polling.h"
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #if __FreeBSD_version >= 800000
41 #include <sys/buf_ring.h>
44 #include <sys/endian.h>
46 #include <sys/kernel.h>
47 #include <sys/kthread.h>
48 #include <sys/malloc.h>
50 #include <sys/module.h>
52 #include <sys/socket.h>
53 #include <sys/sockio.h>
54 #include <sys/sysctl.h>
55 #include <sys/taskqueue.h>
56 #include <sys/eventhandler.h>
59 #include <sys/ieee1588.h>
63 #include <net/ethernet.h>
65 #include <net/if_arp.h>
66 #include <net/if_dl.h>
67 #include <net/if_media.h>
68 #include <net/ifq_var.h>
70 #include <net/if_types.h>
71 #include <net/vlan/if_vlan_var.h>
72 #include <net/vlan/if_vlan_ether.h>
74 #include <netinet/in_systm.h>
75 #include <netinet/in.h>
76 #include <netinet/if_ether.h>
77 #include <netinet/ip.h>
78 #include <netinet/ip6.h>
79 #include <netinet/tcp.h>
81 #include <netinet/tcp_lro.h>
83 #include <netinet/udp.h>
85 #include <sys/in_cksum.h>
86 #include <bus/pci/pcivar.h>
87 #include <bus/pci/pcireg.h>
89 #include "e1000_api.h"
90 #include "e1000_82575.h"
92 #include "ifcap_defines.h" // XXX
94 /*********************************************************************
95 * Set this to one to display debug statistics
96 *********************************************************************/
97 int igb_display_debug_stats = 0;
99 /*********************************************************************
101 *********************************************************************/
102 char igb_driver_version[] = "version - 1.9.1";
105 /*********************************************************************
106 * PCI Device ID Table
108 * Used by probe to select devices to load on
109 * Last field stores an index into e1000_strings
110 * Last entry must be all 0s
112 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID, String Index }
113 *********************************************************************/
115 static igb_vendor_info_t igb_vendor_info_array[] =
117 { 0x8086, E1000_DEV_ID_82575EB_COPPER, PCI_ANY_ID, PCI_ANY_ID, 0},
118 { 0x8086, E1000_DEV_ID_82575EB_FIBER_SERDES,
119 PCI_ANY_ID, PCI_ANY_ID, 0},
120 { 0x8086, E1000_DEV_ID_82575GB_QUAD_COPPER,
121 PCI_ANY_ID, PCI_ANY_ID, 0},
122 { 0x8086, E1000_DEV_ID_82576, PCI_ANY_ID, PCI_ANY_ID, 0},
123 { 0x8086, E1000_DEV_ID_82576_NS, PCI_ANY_ID, PCI_ANY_ID, 0},
124 { 0x8086, E1000_DEV_ID_82576_NS_SERDES, PCI_ANY_ID, PCI_ANY_ID, 0},
125 { 0x8086, E1000_DEV_ID_82576_FIBER, PCI_ANY_ID, PCI_ANY_ID, 0},
126 { 0x8086, E1000_DEV_ID_82576_SERDES, PCI_ANY_ID, PCI_ANY_ID, 0},
127 { 0x8086, E1000_DEV_ID_82576_SERDES_QUAD,
128 PCI_ANY_ID, PCI_ANY_ID, 0},
129 { 0x8086, E1000_DEV_ID_82576_QUAD_COPPER,
130 PCI_ANY_ID, PCI_ANY_ID, 0},
131 { 0x8086, E1000_DEV_ID_82580_COPPER, PCI_ANY_ID, PCI_ANY_ID, 0},
132 { 0x8086, E1000_DEV_ID_82580_FIBER, PCI_ANY_ID, PCI_ANY_ID, 0},
133 { 0x8086, E1000_DEV_ID_82580_SERDES, PCI_ANY_ID, PCI_ANY_ID, 0},
134 { 0x8086, E1000_DEV_ID_82580_SGMII, PCI_ANY_ID, PCI_ANY_ID, 0},
135 { 0x8086, E1000_DEV_ID_82580_COPPER_DUAL,
136 PCI_ANY_ID, PCI_ANY_ID, 0},
137 /* required last entry */
141 /*********************************************************************
142 * Table of branding strings for all supported NICs.
143 *********************************************************************/
145 static char *igb_strings[] = {
146 "Intel(R) PRO/1000 Network Connection"
149 /*********************************************************************
150 * Function prototypes
151 *********************************************************************/
152 static int igb_probe(device_t);
153 static int igb_attach(device_t);
154 static int igb_detach(device_t);
155 static int igb_shutdown(device_t);
156 static int igb_suspend(device_t);
157 static int igb_resume(device_t);
158 static void igb_start(struct ifnet *);
159 static void igb_start_locked(struct tx_ring *, struct ifnet *ifp);
160 #if __FreeBSD_version >= 800000
161 static int igb_mq_start(struct ifnet *, struct mbuf *);
162 static int igb_mq_start_locked(struct ifnet *,
163 struct tx_ring *, struct mbuf *);
164 static void igb_qflush(struct ifnet *);
166 static int igb_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
167 static void igb_init(void *);
168 static void igb_init_locked(struct adapter *);
169 static void igb_stop(void *);
170 static void igb_media_status(struct ifnet *, struct ifmediareq *);
171 static int igb_media_change(struct ifnet *);
172 static void igb_identify_hardware(struct adapter *);
173 static int igb_allocate_pci_resources(struct adapter *);
174 static int igb_allocate_msix(struct adapter *);
175 static int igb_allocate_legacy(struct adapter *);
176 static int igb_setup_msix(struct adapter *);
177 static void igb_free_pci_resources(struct adapter *);
178 static void igb_local_timer(void *);
179 static void igb_reset(struct adapter *);
180 static void igb_setup_interface(device_t, struct adapter *);
181 static int igb_allocate_queues(struct adapter *);
182 static void igb_configure_queues(struct adapter *);
184 static int igb_allocate_transmit_buffers(struct tx_ring *);
185 static void igb_setup_transmit_structures(struct adapter *);
186 static void igb_setup_transmit_ring(struct tx_ring *);
187 static void igb_initialize_transmit_units(struct adapter *);
188 static void igb_free_transmit_structures(struct adapter *);
189 static void igb_free_transmit_buffers(struct tx_ring *);
191 static int igb_allocate_receive_buffers(struct rx_ring *);
192 static int igb_setup_receive_structures(struct adapter *);
193 static int igb_setup_receive_ring(struct rx_ring *);
194 static void igb_initialize_receive_units(struct adapter *);
195 static void igb_free_receive_structures(struct adapter *);
196 static void igb_free_receive_buffers(struct rx_ring *);
197 static void igb_free_receive_ring(struct rx_ring *);
199 static void igb_enable_intr(struct adapter *);
200 static void igb_disable_intr(struct adapter *);
201 static void igb_update_stats_counters(struct adapter *);
202 static bool igb_txeof(struct tx_ring *);
204 static __inline void igb_rx_discard(struct rx_ring *,
205 union e1000_adv_rx_desc *, int);
206 static __inline void igb_rx_input(struct rx_ring *,
207 struct ifnet *, struct mbuf *, u32);
209 static bool igb_rxeof(struct rx_ring *, int);
210 static void igb_rx_checksum(u32, struct mbuf *, u32);
211 static int igb_tx_ctx_setup(struct tx_ring *, struct mbuf *);
213 static bool igb_tso_setup(struct tx_ring *, struct mbuf *, u32 *);
215 static void igb_set_promisc(struct adapter *);
216 static void igb_disable_promisc(struct adapter *);
217 static void igb_set_multi(struct adapter *);
218 static void igb_print_hw_stats(struct adapter *);
219 static void igb_update_link_status(struct adapter *);
220 static int igb_get_buf(struct rx_ring *, int, u8);
222 static void igb_register_vlan(void *, struct ifnet *, u16);
223 static void igb_unregister_vlan(void *, struct ifnet *, u16);
224 static void igb_setup_vlan_hw_support(struct adapter *);
226 static int igb_xmit(struct tx_ring *, struct mbuf **);
227 static int igb_dma_malloc(struct adapter *, bus_size_t,
228 struct igb_dma_alloc *, int);
229 static void igb_dma_free(struct adapter *, struct igb_dma_alloc *);
230 static void igb_print_debug_info(struct adapter *);
231 static void igb_print_nvm_info(struct adapter *);
232 static int igb_is_valid_ether_addr(u8 *);
233 static int igb_sysctl_stats(SYSCTL_HANDLER_ARGS);
234 static int igb_sysctl_debug_info(SYSCTL_HANDLER_ARGS);
235 /* Management and WOL Support */
236 static void igb_init_manageability(struct adapter *);
237 static void igb_release_manageability(struct adapter *);
238 static void igb_get_hw_control(struct adapter *);
239 static void igb_release_hw_control(struct adapter *);
240 static void igb_enable_wakeup(device_t);
242 static void igb_irq_fast(void *);
243 static void igb_add_rx_process_limit(struct adapter *, const char *,
244 const char *, int *, int);
245 static void igb_handle_rxtx(void *context, int pending);
246 static void igb_handle_que(void *context, int pending);
247 static void igb_handle_link(void *context, int pending);
249 /* These are MSIX only irq handlers */
250 static void igb_msix_que(void *);
251 static void igb_msix_link(void *);
253 #ifdef DEVICE_POLLING
254 static poll_handler_t igb_poll;
257 /*********************************************************************
258 * FreeBSD Device Interface Entry Points
259 *********************************************************************/
261 static device_method_t igb_methods[] = {
262 /* Device interface */
263 DEVMETHOD(device_probe, igb_probe),
264 DEVMETHOD(device_attach, igb_attach),
265 DEVMETHOD(device_detach, igb_detach),
266 DEVMETHOD(device_shutdown, igb_shutdown),
267 DEVMETHOD(device_suspend, igb_suspend),
268 DEVMETHOD(device_resume, igb_resume),
272 static driver_t igb_driver = {
273 "igb", igb_methods, sizeof(struct adapter),
276 static devclass_t igb_devclass;
277 DRIVER_MODULE(igb, pci, igb_driver, igb_devclass, 0, 0);
278 MODULE_DEPEND(igb, pci, 1, 1, 1);
279 MODULE_DEPEND(igb, ether, 1, 1, 1);
281 /*********************************************************************
282 * Tunable default values.
283 *********************************************************************/
285 /* Descriptor defaults */
286 static int igb_rxd = IGB_DEFAULT_RXD;
287 static int igb_txd = IGB_DEFAULT_TXD;
288 TUNABLE_INT("hw.igb.rxd", &igb_rxd);
289 TUNABLE_INT("hw.igb.txd", &igb_txd);
292 ** AIM: Adaptive Interrupt Moderation
293 ** which means that the interrupt rate
294 ** is varied over time based on the
295 ** traffic for that interrupt vector
297 static int igb_enable_aim = TRUE;
298 TUNABLE_INT("hw.igb.enable_aim", &igb_enable_aim);
301 * MSIX should be the default for best performance,
302 * but this allows it to be forced off for testing.
304 static int igb_enable_msix = 0;
305 TUNABLE_INT("hw.igb.enable_msix", &igb_enable_msix);
308 * Header split has seemed to be beneficial in
309 * many circumstances tested, however there have
310 * been some stability issues, so the default is
313 static bool igb_header_split = FALSE;
314 TUNABLE_INT("hw.igb.hdr_split", &igb_header_split);
317 ** This will autoconfigure based on
318 ** the number of CPUs if left at 0.
320 static int igb_num_queues = 0;
321 TUNABLE_INT("hw.igb.num_queues", &igb_num_queues);
323 /* How many packets rxeof tries to clean at a time */
324 static int igb_rx_process_limit = 100;
325 TUNABLE_INT("hw.igb.rx_process_limit", &igb_rx_process_limit);
327 /* Flow control setting - default to FULL */
328 static int igb_fc_setting = e1000_fc_full;
329 TUNABLE_INT("hw.igb.fc_setting", &igb_fc_setting);
332 ** Shadow VFTA table, this is needed because
333 ** the real filter table gets cleared during
334 ** a soft reset and the driver needs to be able
337 static u32 igb_shadow_vfta[IGB_VFTA_SIZE];
340 /*********************************************************************
341 * Device identification routine
343 * igb_probe determines if the driver should be loaded on
344 * adapter based on PCI vendor/device id of the adapter.
346 * return BUS_PROBE_DEFAULT on success, positive on failure
347 *********************************************************************/
350 igb_probe(device_t dev)
352 char adapter_name[60];
353 uint16_t pci_vendor_id = 0;
354 uint16_t pci_device_id = 0;
355 uint16_t pci_subvendor_id = 0;
356 uint16_t pci_subdevice_id = 0;
357 igb_vendor_info_t *ent;
359 INIT_DEBUGOUT("igb_probe: begin");
361 pci_vendor_id = pci_get_vendor(dev);
362 if (pci_vendor_id != IGB_VENDOR_ID)
365 pci_device_id = pci_get_device(dev);
366 pci_subvendor_id = pci_get_subvendor(dev);
367 pci_subdevice_id = pci_get_subdevice(dev);
369 ent = igb_vendor_info_array;
370 while (ent->vendor_id != 0) {
371 if ((pci_vendor_id == ent->vendor_id) &&
372 (pci_device_id == ent->device_id) &&
374 ((pci_subvendor_id == ent->subvendor_id) ||
375 (ent->subvendor_id == PCI_ANY_ID)) &&
377 ((pci_subdevice_id == ent->subdevice_id) ||
378 (ent->subdevice_id == PCI_ANY_ID))) {
379 ksprintf(adapter_name, "%s %s",
380 igb_strings[ent->index],
382 device_set_desc_copy(dev, adapter_name);
383 return (BUS_PROBE_DEFAULT);
391 /*********************************************************************
392 * Device initialization routine
394 * The attach entry point is called when the driver is being loaded.
395 * This routine identifies the type of hardware, allocates all resources
396 * and initializes the hardware.
398 * return 0 on success, positive on failure
399 *********************************************************************/
402 igb_attach(device_t dev)
404 struct adapter *adapter;
408 INIT_DEBUGOUT("igb_attach: begin");
410 adapter = device_get_softc(dev);
411 adapter->dev = adapter->osdep.dev = dev;
412 IGB_CORE_LOCK_INIT(adapter, device_get_nameunit(dev));
415 sysctl_ctx_init(&adapter->sysctl_ctx);
416 adapter->sysctl_tree = SYSCTL_ADD_NODE(&adapter->sysctl_ctx,
417 SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
418 device_get_nameunit(adapter->dev),
420 if (adapter->sysctl_tree == NULL) {
421 device_printf(adapter->dev, "can't add sysctl node\n");
426 SYSCTL_ADD_PROC(&adapter->sysctl_ctx,
427 SYSCTL_CHILDREN(adapter->sysctl_tree),
428 OID_AUTO, "debug", CTLTYPE_INT|CTLFLAG_RW, adapter, 0,
429 igb_sysctl_debug_info, "I", "Debug Information");
431 SYSCTL_ADD_PROC(&adapter->sysctl_ctx,
432 SYSCTL_CHILDREN(adapter->sysctl_tree),
433 OID_AUTO, "stats", CTLTYPE_INT|CTLFLAG_RW, adapter, 0,
434 igb_sysctl_stats, "I", "Statistics");
436 SYSCTL_ADD_INT(&adapter->sysctl_ctx,
437 SYSCTL_CHILDREN(adapter->sysctl_tree),
438 OID_AUTO, "flow_control", CTLTYPE_INT|CTLFLAG_RW,
439 &igb_fc_setting, 0, "Flow Control");
441 SYSCTL_ADD_INT(&adapter->sysctl_ctx,
442 SYSCTL_CHILDREN(adapter->sysctl_tree),
443 OID_AUTO, "enable_aim", CTLTYPE_INT|CTLFLAG_RW,
444 &igb_enable_aim, 1, "Interrupt Moderation");
446 callout_init(&adapter->timer);
448 /* Determine hardware and mac info */
449 igb_identify_hardware(adapter);
451 /* Setup PCI resources */
452 if (igb_allocate_pci_resources(adapter)) {
453 device_printf(dev, "Allocation of PCI resources failed\n");
458 /* Do Shared Code initialization */
459 if (e1000_setup_init_funcs(&adapter->hw, TRUE)) {
460 device_printf(dev, "Setup of Shared code failed\n");
465 e1000_get_bus_info(&adapter->hw);
467 /* Sysctls for limiting the amount of work done in the taskqueue */
468 igb_add_rx_process_limit(adapter, "rx_processing_limit",
469 "max number of rx packets to process", &adapter->rx_process_limit,
470 igb_rx_process_limit);
473 * Validate number of transmit and receive descriptors. It
474 * must not exceed hardware maximum, and must be multiple
475 * of E1000_DBA_ALIGN.
477 if (((igb_txd * sizeof(struct e1000_tx_desc)) % IGB_DBA_ALIGN) != 0 ||
478 (igb_txd > IGB_MAX_TXD) || (igb_txd < IGB_MIN_TXD)) {
479 device_printf(dev, "Using %d TX descriptors instead of %d!\n",
480 IGB_DEFAULT_TXD, igb_txd);
481 adapter->num_tx_desc = IGB_DEFAULT_TXD;
483 adapter->num_tx_desc = igb_txd;
484 if (((igb_rxd * sizeof(struct e1000_rx_desc)) % IGB_DBA_ALIGN) != 0 ||
485 (igb_rxd > IGB_MAX_RXD) || (igb_rxd < IGB_MIN_RXD)) {
486 device_printf(dev, "Using %d RX descriptors instead of %d!\n",
487 IGB_DEFAULT_RXD, igb_rxd);
488 adapter->num_rx_desc = IGB_DEFAULT_RXD;
490 adapter->num_rx_desc = igb_rxd;
492 adapter->hw.mac.autoneg = DO_AUTO_NEG;
493 adapter->hw.phy.autoneg_wait_to_complete = FALSE;
494 adapter->hw.phy.autoneg_advertised = AUTONEG_ADV_DEFAULT;
497 if (adapter->hw.phy.media_type == e1000_media_type_copper) {
498 adapter->hw.phy.mdix = AUTO_ALL_MODES;
499 adapter->hw.phy.disable_polarity_correction = FALSE;
500 adapter->hw.phy.ms_type = IGB_MASTER_SLAVE;
504 * Set the frame limits assuming
505 * standard ethernet sized frames.
507 adapter->max_frame_size = ETHERMTU + ETHER_HDR_LEN + ETHERNET_FCS_SIZE;
508 adapter->min_frame_size = ETH_ZLEN + ETHERNET_FCS_SIZE;
511 ** Allocate and Setup Queues
513 if (igb_allocate_queues(adapter)) {
519 ** Start from a known state, this is
520 ** important in reading the nvm and
523 e1000_reset_hw(&adapter->hw);
525 /* Make sure we have a good EEPROM before we read from it */
526 if (e1000_validate_nvm_checksum(&adapter->hw) < 0) {
528 ** Some PCI-E parts fail the first check due to
529 ** the link being in sleep state, call it again,
530 ** if it fails a second time its a real issue.
532 if (e1000_validate_nvm_checksum(&adapter->hw) < 0) {
534 "The EEPROM Checksum Is Not Valid\n");
541 ** Copy the permanent MAC address out of the EEPROM
543 if (e1000_read_mac_addr(&adapter->hw) < 0) {
544 device_printf(dev, "EEPROM read error while reading MAC"
549 /* Check its sanity */
550 if (!igb_is_valid_ether_addr(adapter->hw.mac.addr)) {
551 device_printf(dev, "Invalid MAC address\n");
557 ** Configure Interrupts
559 if ((adapter->msix > 1) && (igb_enable_msix))
560 error = igb_allocate_msix(adapter);
561 else /* MSI or Legacy */
562 error = igb_allocate_legacy(adapter);
566 /* Setup OS specific network interface */
567 igb_setup_interface(dev, adapter);
569 /* Now get a good starting state */
572 /* Initialize statistics */
573 igb_update_stats_counters(adapter);
575 adapter->hw.mac.get_link_status = 1;
576 igb_update_link_status(adapter);
578 /* Indicate SOL/IDER usage */
579 if (e1000_check_reset_block(&adapter->hw))
581 "PHY reset is blocked due to SOL/IDER session.\n");
583 /* Determine if we have to control management hardware */
584 adapter->has_manage = e1000_enable_mng_pass_thru(&adapter->hw);
589 /* APME bit in EEPROM is mapped to WUC.APME */
590 eeprom_data = E1000_READ_REG(&adapter->hw, E1000_WUC) & E1000_WUC_APME;
592 adapter->wol = E1000_WUFC_MAG;
594 /* Register for VLAN events */
595 adapter->vlan_attach = EVENTHANDLER_REGISTER(vlan_config,
596 igb_register_vlan, adapter, EVENTHANDLER_PRI_FIRST);
597 adapter->vlan_detach = EVENTHANDLER_REGISTER(vlan_unconfig,
598 igb_unregister_vlan, adapter, EVENTHANDLER_PRI_FIRST);
600 /* Tell the stack that the interface is not active */
601 adapter->ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
603 INIT_DEBUGOUT("igb_attach: end");
608 igb_free_transmit_structures(adapter);
609 igb_free_receive_structures(adapter);
610 igb_release_hw_control(adapter);
612 igb_free_pci_resources(adapter);
614 sysctl_ctx_free(&adapter->sysctl_ctx);
615 IGB_CORE_LOCK_DESTROY(adapter);
620 /*********************************************************************
621 * Device removal routine
623 * The detach entry point is called when the driver is being removed.
624 * This routine stops the adapter and deallocates all the resources
625 * that were allocated for driver operation.
627 * return 0 on success, positive on failure
628 *********************************************************************/
631 igb_detach(device_t dev)
633 struct adapter *adapter = device_get_softc(dev);
635 INIT_DEBUGOUT("igb_detach: begin");
637 /* Make sure VLANS are not using driver */
638 if (adapter->ifp->if_vlantrunks != NULL) {
639 device_printf(dev,"Vlan in use, detach first\n");
643 IGB_CORE_LOCK(adapter);
644 adapter->in_detach = 1;
646 IGB_CORE_UNLOCK(adapter);
648 e1000_phy_hw_reset(&adapter->hw);
650 /* Give control back to firmware */
651 igb_release_manageability(adapter);
652 igb_release_hw_control(adapter);
655 E1000_WRITE_REG(&adapter->hw, E1000_WUC, E1000_WUC_PME_EN);
656 E1000_WRITE_REG(&adapter->hw, E1000_WUFC, adapter->wol);
657 igb_enable_wakeup(dev);
660 /* Unregister VLAN events */
661 if (adapter->vlan_attach != NULL)
662 EVENTHANDLER_DEREGISTER(vlan_config, adapter->vlan_attach);
663 if (adapter->vlan_detach != NULL)
664 EVENTHANDLER_DEREGISTER(vlan_unconfig, adapter->vlan_detach);
666 ether_ifdetach(adapter->ifp);
668 //callout_drain(&adapter->timer);
669 callout_stop(&adapter->timer);
671 igb_free_pci_resources(adapter);
672 bus_generic_detach(dev);
674 igb_free_transmit_structures(adapter);
675 igb_free_receive_structures(adapter);
677 sysctl_ctx_free(&adapter->sysctl_ctx);
678 IGB_CORE_LOCK_DESTROY(adapter);
683 /*********************************************************************
685 * Shutdown entry point
687 **********************************************************************/
690 igb_shutdown(device_t dev)
692 return igb_suspend(dev);
696 * Suspend/resume device methods.
699 igb_suspend(device_t dev)
701 struct adapter *adapter = device_get_softc(dev);
703 IGB_CORE_LOCK(adapter);
707 igb_release_manageability(adapter);
708 igb_release_hw_control(adapter);
711 E1000_WRITE_REG(&adapter->hw, E1000_WUC, E1000_WUC_PME_EN);
712 E1000_WRITE_REG(&adapter->hw, E1000_WUFC, adapter->wol);
713 igb_enable_wakeup(dev);
716 IGB_CORE_UNLOCK(adapter);
718 return bus_generic_suspend(dev);
722 igb_resume(device_t dev)
724 struct adapter *adapter = device_get_softc(dev);
725 struct ifnet *ifp = adapter->ifp;
727 IGB_CORE_LOCK(adapter);
728 igb_init_locked(adapter);
729 igb_init_manageability(adapter);
731 if ((ifp->if_flags & IFF_UP) &&
732 (ifp->if_flags & IFF_RUNNING))
735 IGB_CORE_UNLOCK(adapter);
737 return bus_generic_resume(dev);
741 /*********************************************************************
742 * Transmit entry point
744 * igb_start is called by the stack to initiate a transmit.
745 * The driver will remain in this routine as long as there are
746 * packets to transmit and transmit resources are available.
747 * In case resources are not available stack is notified and
748 * the packet is requeued.
749 **********************************************************************/
752 igb_start_locked(struct tx_ring *txr, struct ifnet *ifp)
754 struct adapter *adapter = ifp->if_softc;
757 IGB_TX_LOCK_ASSERT(txr);
759 if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
763 * Must purge on abort from this point on or the netif will call
764 * us endlessly. Either that or set IFF_OACTIVE.
766 if (!adapter->link_active) {
767 ifq_purge(&ifp->if_snd);
771 while (!ifq_is_empty(&ifp->if_snd)) {
773 m_head = ifq_dequeue(&ifp->if_snd, NULL);
777 * Encapsulation can modify our pointer, and or make it
778 * NULL on failure. In that event, we can't requeue.
780 if (igb_xmit(txr, &m_head)) {
783 ifp->if_flags |= IFF_OACTIVE;
784 ifq_prepend(&ifp->if_snd, m_head);
788 /* Send a copy of the frame to the BPF listener */
789 ETHER_BPF_MTAP(ifp, m_head);
791 /* Set watchdog on */
792 txr->watchdog_check = TRUE;
797 * Legacy TX driver routine, called from the
798 * stack, always uses tx[0], and spins for it.
799 * Should not be used with multiqueue tx
802 igb_start(struct ifnet *ifp)
804 struct adapter *adapter = ifp->if_softc;
805 struct tx_ring *txr = adapter->tx_rings;
807 if (ifp->if_flags & IFF_RUNNING) {
809 igb_start_locked(txr, ifp);
815 #if __FreeBSD_version >= 800000
817 ** Multiqueue Transmit driver
821 igb_mq_start(struct ifnet *ifp, struct mbuf *m)
823 struct adapter *adapter = ifp->if_softc;
827 /* Which queue to use */
828 if ((m->m_flags & M_FLOWID) != 0)
829 i = m->m_pkthdr.flowid % adapter->num_queues;
830 txr = &adapter->tx_rings[i];
832 if (IGB_TX_TRYLOCK(txr)) {
833 err = igb_mq_start_locked(ifp, txr, m);
836 err = drbr_enqueue(ifp, txr->br, m);
842 igb_mq_start_locked(struct ifnet *ifp, struct tx_ring *txr, struct mbuf *m)
844 struct adapter *adapter = txr->adapter;
848 IGB_TX_LOCK_ASSERT(txr);
850 if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) !=
851 IFF_RUNNING || adapter->link_active == 0) {
853 err = drbr_enqueue(ifp, txr->br, m);
859 next = drbr_dequeue(ifp, txr->br);
860 } else if (drbr_needs_enqueue(ifp, txr->br)) {
861 if ((err = drbr_enqueue(ifp, txr->br, m)) != 0)
863 next = drbr_dequeue(ifp, txr->br);
866 /* Process the queue */
867 while (next != NULL) {
868 if ((err = igb_xmit(txr, &next)) != 0) {
870 err = drbr_enqueue(ifp, txr->br, next);
874 drbr_stats_update(ifp, next->m_pkthdr.len, next->m_flags);
875 ETHER_BPF_MTAP(ifp, next);
876 if ((ifp->if_flags & IFF_RUNNING) == 0)
878 if (txr->tx_avail <= IGB_TX_OP_THRESHOLD) {
879 ifp->if_flags |= IFF_OACTIVE;
882 next = drbr_dequeue(ifp, txr->br);
885 /* Set the watchdog */
886 txr->watchdog_check = TRUE;
892 ** Flush all ring buffers
895 igb_qflush(struct ifnet *ifp)
897 struct adapter *adapter = ifp->if_softc;
898 struct tx_ring *txr = adapter->tx_rings;
901 for (int i = 0; i < adapter->num_queues; i++, txr++) {
903 while ((m = buf_ring_dequeue_sc(txr->br)) != NULL)
909 #endif /* __FreeBSD_version >= 800000 */
911 /*********************************************************************
914 * igb_ioctl is called when the user wants to configure the
917 * return 0 on success, positive on failure
918 **********************************************************************/
921 igb_ioctl(struct ifnet *ifp, u_long command, caddr_t data, struct ucred *cred)
923 struct adapter *adapter = ifp->if_softc;
924 struct ifreq *ifr = (struct ifreq *)data;
926 struct ifaddr *ifa = (struct ifaddr *)data;
930 if (adapter->in_detach)
936 if (ifa->ifa_addr->sa_family == AF_INET) {
939 * Since resetting hardware takes a very long time
940 * and results in link renegotiation we only
941 * initialize the hardware only when it is absolutely
944 ifp->if_flags |= IFF_UP;
945 if (!(ifp->if_flags & IFF_RUNNING)) {
946 IGB_CORE_LOCK(adapter);
947 igb_init_locked(adapter);
948 IGB_CORE_UNLOCK(adapter);
950 if (!(ifp->if_flags & IFF_NOARP))
951 arp_ifinit(ifp, ifa);
954 error = ether_ioctl(ifp, command, data);
960 IOCTL_DEBUGOUT("ioctl rcv'd: SIOCSIFMTU (Set Interface MTU)");
962 IGB_CORE_LOCK(adapter);
963 max_frame_size = 9234;
964 if (ifr->ifr_mtu > max_frame_size - ETHER_HDR_LEN -
966 IGB_CORE_UNLOCK(adapter);
971 ifp->if_mtu = ifr->ifr_mtu;
972 adapter->max_frame_size =
973 ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN;
974 igb_init_locked(adapter);
975 IGB_CORE_UNLOCK(adapter);
979 IOCTL_DEBUGOUT("ioctl rcv'd:\
980 SIOCSIFFLAGS (Set Interface Flags)");
981 IGB_CORE_LOCK(adapter);
982 if (ifp->if_flags & IFF_UP) {
983 if ((ifp->if_flags & IFF_RUNNING)) {
984 if ((ifp->if_flags ^ adapter->if_flags) &
985 (IFF_PROMISC | IFF_ALLMULTI)) {
986 igb_disable_promisc(adapter);
987 igb_set_promisc(adapter);
990 igb_init_locked(adapter);
992 if (ifp->if_flags & IFF_RUNNING)
994 adapter->if_flags = ifp->if_flags;
995 IGB_CORE_UNLOCK(adapter);
999 IOCTL_DEBUGOUT("ioctl rcv'd: SIOC(ADD|DEL)MULTI");
1000 if (ifp->if_flags & IFF_RUNNING) {
1001 IGB_CORE_LOCK(adapter);
1002 igb_disable_intr(adapter);
1003 igb_set_multi(adapter);
1004 #ifdef DEVICE_POLLING
1005 if ((ifp->if_flags & IFF_POLLING) == 0)
1007 igb_enable_intr(adapter);
1008 IGB_CORE_UNLOCK(adapter);
1012 /* Check SOL/IDER usage */
1013 IGB_CORE_LOCK(adapter);
1014 if (e1000_check_reset_block(&adapter->hw)) {
1015 IGB_CORE_UNLOCK(adapter);
1016 device_printf(adapter->dev, "Media change is"
1017 " blocked due to SOL/IDER session.\n");
1020 IGB_CORE_UNLOCK(adapter);
1022 IOCTL_DEBUGOUT("ioctl rcv'd: \
1023 SIOCxIFMEDIA (Get/Set Interface Media)");
1024 error = ifmedia_ioctl(ifp, ifr, &adapter->media, command);
1030 IOCTL_DEBUGOUT("ioctl rcv'd: SIOCSIFCAP (Set Capabilities)");
1032 mask = ifr->ifr_reqcap ^ ifp->if_capenable;
1033 #ifdef DEVICE_POLLING
1034 if (ifp->if_flags & IFF_POLLING) {
1035 IGB_CORE_LOCK(adapter);
1036 igb_disable_intr(adapter);
1037 IGB_CORE_UNLOCK(adapter);
1040 if (mask & IFCAP_HWCSUM) {
1041 ifp->if_capenable ^= IFCAP_HWCSUM;
1045 if (mask & IFCAP_TSO4) {
1046 ifp->if_capenable ^= IFCAP_TSO4;
1050 if (mask & IFCAP_VLAN_HWTAGGING) {
1051 ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
1055 if (mask & IFCAP_LRO) {
1056 ifp->if_capenable ^= IFCAP_LRO;
1060 if (reinit && (ifp->if_flags & IFF_RUNNING))
1063 VLAN_CAPABILITIES(ifp);
1069 error = ether_ioctl(ifp, command, data);
1072 IOCTL_DEBUGOUT("ioctl done");
1078 /*********************************************************************
1081 * This routine is used in two ways. It is used by the stack as
1082 * init entry point in network interface structure. It is also used
1083 * by the driver as a hw/sw initialization routine to get to a
1086 * return 0 on success, positive on failure
1087 **********************************************************************/
1090 igb_init_locked(struct adapter *adapter)
1092 struct ifnet *ifp = adapter->ifp;
1093 device_t dev = adapter->dev;
1095 INIT_DEBUGOUT("igb_init: begin");
1097 IGB_CORE_LOCK_ASSERT(adapter);
1099 igb_disable_intr(adapter);
1100 callout_stop(&adapter->timer);
1102 /* Get the latest mac address, User can use a LAA */
1103 bcopy(IF_LLADDR(adapter->ifp), adapter->hw.mac.addr,
1106 /* Put the address into the Receive Address Array */
1107 e1000_rar_set(&adapter->hw, adapter->hw.mac.addr, 0);
1110 igb_update_link_status(adapter);
1112 E1000_WRITE_REG(&adapter->hw, E1000_VET, ETHERTYPE_VLAN);
1114 /* Set hardware offload abilities */
1115 ifp->if_hwassist = 0;
1116 if (ifp->if_capenable & IFCAP_TXCSUM) {
1117 ifp->if_hwassist |= (CSUM_TCP | CSUM_UDP);
1118 #if __FreeBSD_version >= 800000
1119 if (adapter->hw.mac.type == e1000_82576)
1120 ifp->if_hwassist |= CSUM_SCTP;
1125 if (ifp->if_capenable & IFCAP_TSO4)
1126 ifp->if_hwassist |= CSUM_TSO;
1129 /* Configure for OS presence */
1130 igb_init_manageability(adapter);
1132 /* Prepare transmit descriptors and buffers */
1133 igb_setup_transmit_structures(adapter);
1134 igb_initialize_transmit_units(adapter);
1136 /* Setup Multicast table */
1137 igb_set_multi(adapter);
1140 ** Figure out the desired mbuf pool
1141 ** for doing jumbo/packetsplit
1143 if (ifp->if_mtu > ETHERMTU)
1144 adapter->rx_mbuf_sz = MJUMPAGESIZE;
1146 adapter->rx_mbuf_sz = MCLBYTES;
1148 /* Prepare receive descriptors and buffers */
1149 if (igb_setup_receive_structures(adapter)) {
1150 device_printf(dev, "Could not setup receive structures\n");
1153 igb_initialize_receive_units(adapter);
1155 /* Don't lose promiscuous settings */
1156 igb_set_promisc(adapter);
1158 ifp->if_flags |= IFF_RUNNING;
1159 ifp->if_flags &= ~IFF_OACTIVE;
1161 callout_reset(&adapter->timer, hz, igb_local_timer, adapter);
1162 e1000_clear_hw_cntrs_base_generic(&adapter->hw);
1164 if (adapter->msix > 1) /* Set up queue routing */
1165 igb_configure_queues(adapter);
1167 /* Set up VLAN tag offload and filter */
1168 igb_setup_vlan_hw_support(adapter);
1170 /* this clears any pending interrupts */
1171 E1000_READ_REG(&adapter->hw, E1000_ICR);
1172 #ifdef DEVICE_POLLING
1174 * Only enable interrupts if we are not polling, make sure
1175 * they are off otherwise.
1177 if (ifp->if_flags & IFF_POLLING)
1178 igb_disable_intr(adapter);
1180 #endif /* DEVICE_POLLING */
1182 igb_enable_intr(adapter);
1183 E1000_WRITE_REG(&adapter->hw, E1000_ICS, E1000_ICS_LSC);
1186 /* Don't reset the phy next time init gets called */
1187 adapter->hw.phy.reset_disable = TRUE;
1188 INIT_DEBUGOUT("igb_init: end");
1194 struct adapter *adapter = arg;
1196 IGB_CORE_LOCK(adapter);
1197 igb_init_locked(adapter);
1198 IGB_CORE_UNLOCK(adapter);
1203 igb_handle_rxtx(void *context, int pending)
1205 struct adapter *adapter = context;
1206 struct tx_ring *txr = adapter->tx_rings;
1207 struct rx_ring *rxr = adapter->rx_rings;
1212 if (ifp->if_flags & IFF_RUNNING) {
1213 if (igb_rxeof(rxr, adapter->rx_process_limit))
1214 taskqueue_enqueue(adapter->tq, &adapter->rxtx_task);
1218 #if __FreeBSD_version >= 800000
1219 if (!drbr_empty(ifp, txr->br))
1220 igb_mq_start_locked(ifp, txr, NULL);
1222 if (!ifq_is_empty(&ifp->if_snd))
1223 igb_start_locked(txr, ifp);
1228 igb_enable_intr(adapter);
1232 igb_handle_que(void *context, int pending)
1234 struct igb_queue *que = context;
1235 struct adapter *adapter = que->adapter;
1236 struct tx_ring *txr = que->txr;
1237 struct rx_ring *rxr = que->rxr;
1238 struct ifnet *ifp = adapter->ifp;
1239 u32 loop = IGB_MAX_LOOP;
1244 more = igb_rxeof(rxr, -1);
1245 } while (loop-- && more);
1247 if (IGB_TX_TRYLOCK(txr)) {
1248 loop = IGB_MAX_LOOP;
1250 more = igb_txeof(txr);
1251 } while (loop-- && more);
1252 #if __FreeBSD_version >= 800000
1253 igb_mq_start_locked(ifp, txr, NULL);
1255 if (!ifq_is_empty(&ifp->if_snd))
1256 igb_start_locked(txr, ifp);
1261 /* Reenable this interrupt */
1262 #ifdef DEVICE_POLLING
1263 if ((ifp->if_flags & IFF_POLLING) == 0)
1265 E1000_WRITE_REG(&adapter->hw, E1000_EIMS, que->eims);
1268 /* Deal with link in a sleepable context */
1270 igb_handle_link(void *context, int pending)
1272 struct adapter *adapter = context;
1274 adapter->hw.mac.get_link_status = 1;
1275 igb_update_link_status(adapter);
1278 /*********************************************************************
1280 * MSI/Legacy Deferred
1281 * Interrupt Service routine
1283 *********************************************************************/
1284 #define FILTER_STRAY
1285 #define FILTER_HANDLED
1287 igb_irq_fast(void *arg)
1289 struct adapter *adapter = arg;
1293 reg_icr = E1000_READ_REG(&adapter->hw, E1000_ICR);
1296 if (reg_icr == 0xffffffff)
1297 return FILTER_STRAY;
1299 /* Definitely not our interrupt. */
1301 return FILTER_STRAY;
1303 if ((reg_icr & E1000_ICR_INT_ASSERTED) == 0)
1304 return FILTER_STRAY;
1307 * Mask interrupts until the taskqueue is finished running. This is
1308 * cheap, just assume that it is needed. This also works around the
1309 * MSI message reordering errata on certain systems.
1311 igb_disable_intr(adapter);
1312 taskqueue_enqueue(adapter->tq, &adapter->rxtx_task);
1314 /* Link status change */
1315 if (reg_icr & (E1000_ICR_RXSEQ | E1000_ICR_LSC))
1316 taskqueue_enqueue(adapter->tq, &adapter->link_task);
1318 if (reg_icr & E1000_ICR_RXO)
1319 adapter->rx_overruns++;
1320 return FILTER_HANDLED;
1323 #ifdef DEVICE_POLLING
1324 /*********************************************************************
1326 * Legacy polling routine
1328 *********************************************************************/
1329 #if __FreeBSD_version >= 800000
1330 #define POLL_RETURN_COUNT(a) (a)
1333 #define POLL_RETURN_COUNT(a)
1336 igb_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
1338 struct adapter *adapter = ifp->if_softc;
1339 struct rx_ring *rxr = adapter->rx_rings;
1340 struct tx_ring *txr = adapter->tx_rings;
1341 u32 reg_icr, rx_done = 0;
1342 u32 loop = IGB_MAX_LOOP;
1345 IGB_CORE_LOCK(adapter);
1346 if ((ifp->if_flags & IFF_RUNNING) == 0) {
1347 IGB_CORE_UNLOCK(adapter);
1348 return POLL_RETURN_COUNT(rx_done);
1351 if (cmd == POLL_AND_CHECK_STATUS) {
1352 reg_icr = E1000_READ_REG(&adapter->hw, E1000_ICR);
1353 /* Link status change */
1354 if (reg_icr & (E1000_ICR_RXSEQ | E1000_ICR_LSC))
1355 taskqueue_enqueue(adapter->tq, &adapter->link_task);
1357 if (reg_icr & E1000_ICR_RXO)
1358 adapter->rx_overruns++;
1360 IGB_CORE_UNLOCK(adapter);
1362 /* TODO: rx_count */
1363 rx_done = igb_rxeof(rxr, count) ? 1 : 0;
1367 more = igb_txeof(txr);
1368 } while (loop-- && more);
1369 #if __FreeBSD_version >= 800000
1370 if (!drbr_empty(ifp, txr->br))
1371 igb_mq_start_locked(ifp, txr, NULL);
1373 if (!ifq_is_empty(&ifp->if_snd))
1374 igb_start_locked(txr, ifp);
1377 return POLL_RETURN_COUNT(rx_done);
1379 #endif /* DEVICE_POLLING */
1381 /*********************************************************************
1383 * MSIX TX Interrupt Service routine
1385 **********************************************************************/
1387 igb_msix_que(void *arg)
1389 struct igb_queue *que = arg;
1390 struct adapter *adapter = que->adapter;
1391 struct tx_ring *txr = que->txr;
1392 struct rx_ring *rxr = que->rxr;
1394 bool more_tx, more_rx;
1396 E1000_WRITE_REG(&adapter->hw, E1000_EIMC, que->eims);
1400 more_tx = igb_txeof(txr);
1403 more_rx = igb_rxeof(rxr, adapter->rx_process_limit);
1405 if (igb_enable_aim == FALSE)
1408 ** Do Adaptive Interrupt Moderation:
1409 ** - Write out last calculated setting
1410 ** - Calculate based on average size over
1411 ** the last interval.
1413 if (que->eitr_setting)
1414 E1000_WRITE_REG(&adapter->hw,
1415 E1000_EITR(que->msix), que->eitr_setting);
1417 que->eitr_setting = 0;
1419 /* Idle, do nothing */
1420 if ((txr->bytes == 0) && (rxr->bytes == 0))
1423 /* Used half Default if sub-gig */
1424 if (adapter->link_speed != 1000)
1425 newitr = IGB_DEFAULT_ITR / 2;
1427 if ((txr->bytes) && (txr->packets))
1428 newitr = txr->bytes/txr->packets;
1429 if ((rxr->bytes) && (rxr->packets))
1430 newitr = max(newitr,
1431 (rxr->bytes / rxr->packets));
1432 newitr += 24; /* account for hardware frame, crc */
1433 /* set an upper boundary */
1434 newitr = min(newitr, 3000);
1435 /* Be nice to the mid range */
1436 if ((newitr > 300) && (newitr < 1200))
1437 newitr = (newitr / 3);
1439 newitr = (newitr / 2);
1441 newitr &= 0x7FFC; /* Mask invalid bits */
1442 if (adapter->hw.mac.type == e1000_82575)
1443 newitr |= newitr << 16;
1445 newitr |= 0x8000000;
1447 /* save for next interrupt */
1448 que->eitr_setting = newitr;
1457 /* Schedule a clean task if needed*/
1458 if (more_tx || more_rx)
1459 taskqueue_enqueue(que->tq, &que->que_task);
1461 /* Reenable this interrupt */
1462 E1000_WRITE_REG(&adapter->hw, E1000_EIMS, que->eims);
1467 /*********************************************************************
1469 * MSIX Link Interrupt Service routine
1471 **********************************************************************/
1474 igb_msix_link(void *arg)
1476 struct adapter *adapter = arg;
1479 ++adapter->link_irq;
1480 icr = E1000_READ_REG(&adapter->hw, E1000_ICR);
1481 if (!(icr & E1000_ICR_LSC))
1483 taskqueue_enqueue(adapter->tq, &adapter->link_task);
1487 E1000_WRITE_REG(&adapter->hw, E1000_IMS, E1000_IMS_LSC);
1488 E1000_WRITE_REG(&adapter->hw, E1000_EIMS, adapter->link_mask);
1493 /*********************************************************************
1495 * Media Ioctl callback
1497 * This routine is called whenever the user queries the status of
1498 * the interface using ifconfig.
1500 **********************************************************************/
1502 igb_media_status(struct ifnet *ifp, struct ifmediareq *ifmr)
1504 struct adapter *adapter = ifp->if_softc;
1505 u_char fiber_type = IFM_1000_SX;
1507 INIT_DEBUGOUT("igb_media_status: begin");
1509 IGB_CORE_LOCK(adapter);
1510 igb_update_link_status(adapter);
1512 ifmr->ifm_status = IFM_AVALID;
1513 ifmr->ifm_active = IFM_ETHER;
1515 if (!adapter->link_active) {
1516 IGB_CORE_UNLOCK(adapter);
1520 ifmr->ifm_status |= IFM_ACTIVE;
1522 if ((adapter->hw.phy.media_type == e1000_media_type_fiber) ||
1523 (adapter->hw.phy.media_type == e1000_media_type_internal_serdes))
1524 ifmr->ifm_active |= fiber_type | IFM_FDX;
1526 switch (adapter->link_speed) {
1528 ifmr->ifm_active |= IFM_10_T;
1531 ifmr->ifm_active |= IFM_100_TX;
1534 ifmr->ifm_active |= IFM_1000_T;
1537 if (adapter->link_duplex == FULL_DUPLEX)
1538 ifmr->ifm_active |= IFM_FDX;
1540 ifmr->ifm_active |= IFM_HDX;
1542 IGB_CORE_UNLOCK(adapter);
1545 /*********************************************************************
1547 * Media Ioctl callback
1549 * This routine is called when the user changes speed/duplex using
1550 * media/mediopt option with ifconfig.
1552 **********************************************************************/
1554 igb_media_change(struct ifnet *ifp)
1556 struct adapter *adapter = ifp->if_softc;
1557 struct ifmedia *ifm = &adapter->media;
1559 INIT_DEBUGOUT("igb_media_change: begin");
1561 if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
1564 IGB_CORE_LOCK(adapter);
1565 switch (IFM_SUBTYPE(ifm->ifm_media)) {
1567 adapter->hw.mac.autoneg = DO_AUTO_NEG;
1568 adapter->hw.phy.autoneg_advertised = AUTONEG_ADV_DEFAULT;
1573 adapter->hw.mac.autoneg = DO_AUTO_NEG;
1574 adapter->hw.phy.autoneg_advertised = ADVERTISE_1000_FULL;
1577 adapter->hw.mac.autoneg = FALSE;
1578 adapter->hw.phy.autoneg_advertised = 0;
1579 if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX)
1580 adapter->hw.mac.forced_speed_duplex = ADVERTISE_100_FULL;
1582 adapter->hw.mac.forced_speed_duplex = ADVERTISE_100_HALF;
1585 adapter->hw.mac.autoneg = FALSE;
1586 adapter->hw.phy.autoneg_advertised = 0;
1587 if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX)
1588 adapter->hw.mac.forced_speed_duplex = ADVERTISE_10_FULL;
1590 adapter->hw.mac.forced_speed_duplex = ADVERTISE_10_HALF;
1593 device_printf(adapter->dev, "Unsupported media type\n");
1596 /* As the speed/duplex settings my have changed we need to
1599 adapter->hw.phy.reset_disable = FALSE;
1601 igb_init_locked(adapter);
1602 IGB_CORE_UNLOCK(adapter);
1608 /*********************************************************************
1610 * This routine maps the mbufs to Advanced TX descriptors.
1611 * used by the 82575 adapter.
1613 **********************************************************************/
1616 igb_xmit(struct tx_ring *txr, struct mbuf **m_headp)
1618 struct adapter *adapter = txr->adapter;
1619 bus_dma_segment_t segs[IGB_MAX_SCATTER];
1621 struct igb_tx_buffer *tx_buffer, *tx_buffer_mapped;
1622 union e1000_adv_tx_desc *txd = NULL;
1623 struct mbuf *m_head;
1624 u32 olinfo_status = 0, cmd_type_len = 0;
1625 int nsegs, i, j, error, first, last = 0;
1631 /* Set basic descriptor constants */
1632 cmd_type_len |= E1000_ADVTXD_DTYP_DATA;
1633 cmd_type_len |= E1000_ADVTXD_DCMD_IFCS | E1000_ADVTXD_DCMD_DEXT;
1634 if (m_head->m_flags & M_VLANTAG)
1635 cmd_type_len |= E1000_ADVTXD_DCMD_VLE;
1638 * Force a cleanup if number of TX descriptors
1639 * available hits the threshold
1641 if (txr->tx_avail <= IGB_TX_CLEANUP_THRESHOLD) {
1643 /* Now do we at least have a minimal? */
1644 if (txr->tx_avail <= IGB_TX_OP_THRESHOLD) {
1645 txr->no_desc_avail++;
1651 * Map the packet for DMA.
1653 * Capture the first descriptor index,
1654 * this descriptor will have the index
1655 * of the EOP which is the only one that
1656 * now gets a DONE bit writeback.
1658 first = txr->next_avail_desc;
1659 tx_buffer = &txr->tx_buffers[first];
1660 tx_buffer_mapped = tx_buffer;
1661 map = tx_buffer->map;
1663 error = bus_dmamap_load_mbuf_segment(txr->txtag, map,
1664 *m_headp, segs, IGB_MAX_SCATTER, &nsegs, BUS_DMA_NOWAIT);
1666 if (error == EFBIG) {
1669 m = m_defrag(*m_headp, MB_DONTWAIT);
1671 adapter->mbuf_defrag_failed++;
1679 error = bus_dmamap_load_mbuf_segment(txr->txtag, map,
1680 *m_headp, segs, IGB_MAX_SCATTER, &nsegs, BUS_DMA_NOWAIT);
1682 if (error == ENOMEM) {
1683 adapter->no_tx_dma_setup++;
1685 } else if (error != 0) {
1686 adapter->no_tx_dma_setup++;
1691 } else if (error == ENOMEM) {
1692 adapter->no_tx_dma_setup++;
1694 } else if (error != 0) {
1695 adapter->no_tx_dma_setup++;
1701 /* Check again to be sure we have enough descriptors */
1702 if (nsegs > (txr->tx_avail - 2)) {
1703 txr->no_desc_avail++;
1704 bus_dmamap_unload(txr->txtag, map);
1710 * Set up the context descriptor:
1711 * used when any hardware offload is done.
1712 * This includes CSUM, VLAN, and TSO. It
1713 * will use the first descriptor.
1716 if (m_head->m_pkthdr.csum_flags & CSUM_TSO) {
1717 if (igb_tso_setup(txr, m_head, &hdrlen)) {
1718 cmd_type_len |= E1000_ADVTXD_DCMD_TSE;
1719 olinfo_status |= E1000_TXD_POPTS_IXSM << 8;
1720 olinfo_status |= E1000_TXD_POPTS_TXSM << 8;
1725 if (igb_tx_ctx_setup(txr, m_head))
1726 olinfo_status |= E1000_TXD_POPTS_TXSM << 8;
1728 /* Calculate payload length */
1729 olinfo_status |= ((m_head->m_pkthdr.len - hdrlen)
1730 << E1000_ADVTXD_PAYLEN_SHIFT);
1732 /* 82575 needs the queue index added */
1733 if (adapter->hw.mac.type == e1000_82575)
1734 olinfo_status |= txr->me << 4;
1736 /* Set up our transmit descriptors */
1737 i = txr->next_avail_desc;
1738 for (j = 0; j < nsegs; j++) {
1740 bus_addr_t seg_addr;
1742 tx_buffer = &txr->tx_buffers[i];
1743 txd = (union e1000_adv_tx_desc *)&txr->tx_base[i];
1744 seg_addr = segs[j].ds_addr;
1745 seg_len = segs[j].ds_len;
1747 txd->read.buffer_addr = htole64(seg_addr);
1748 txd->read.cmd_type_len = htole32(cmd_type_len | seg_len);
1749 txd->read.olinfo_status = htole32(olinfo_status);
1751 if (++i == adapter->num_tx_desc)
1753 tx_buffer->m_head = NULL;
1754 tx_buffer->next_eop = -1;
1757 txr->next_avail_desc = i;
1758 txr->tx_avail -= nsegs;
1760 tx_buffer->m_head = m_head;
1761 tx_buffer_mapped->map = tx_buffer->map;
1762 tx_buffer->map = map;
1763 bus_dmamap_sync(txr->txtag, map, BUS_DMASYNC_PREWRITE);
1766 * Last Descriptor of Packet
1767 * needs End Of Packet (EOP)
1768 * and Report Status (RS)
1770 txd->read.cmd_type_len |=
1771 htole32(E1000_ADVTXD_DCMD_EOP | E1000_ADVTXD_DCMD_RS);
1773 * Keep track in the first buffer which
1774 * descriptor will be written back
1776 tx_buffer = &txr->tx_buffers[first];
1777 tx_buffer->next_eop = last;
1778 txr->watchdog_time = ticks;
1781 * Advance the Transmit Descriptor Tail (TDT), this tells the E1000
1782 * that this frame is available to transmit.
1784 bus_dmamap_sync(txr->txdma.dma_tag, txr->txdma.dma_map,
1785 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1786 E1000_WRITE_REG(&adapter->hw, E1000_TDT(txr->me), i);
1794 igb_set_promisc(struct adapter *adapter)
1796 struct ifnet *ifp = adapter->ifp;
1799 reg_rctl = E1000_READ_REG(&adapter->hw, E1000_RCTL);
1801 if (ifp->if_flags & IFF_PROMISC) {
1802 reg_rctl |= (E1000_RCTL_UPE | E1000_RCTL_MPE);
1803 E1000_WRITE_REG(&adapter->hw, E1000_RCTL, reg_rctl);
1804 } else if (ifp->if_flags & IFF_ALLMULTI) {
1805 reg_rctl |= E1000_RCTL_MPE;
1806 reg_rctl &= ~E1000_RCTL_UPE;
1807 E1000_WRITE_REG(&adapter->hw, E1000_RCTL, reg_rctl);
1812 igb_disable_promisc(struct adapter *adapter)
1816 reg_rctl = E1000_READ_REG(&adapter->hw, E1000_RCTL);
1818 reg_rctl &= (~E1000_RCTL_UPE);
1819 reg_rctl &= (~E1000_RCTL_MPE);
1820 E1000_WRITE_REG(&adapter->hw, E1000_RCTL, reg_rctl);
1824 /*********************************************************************
1827 * This routine is called whenever multicast address list is updated.
1829 **********************************************************************/
1832 igb_set_multi(struct adapter *adapter)
1834 struct ifnet *ifp = adapter->ifp;
1835 struct ifmultiaddr *ifma;
1837 static u8 mta[MAX_NUM_MULTICAST_ADDRESSES * ETH_ADDR_LEN];
1841 IOCTL_DEBUGOUT("igb_set_multi: begin");
1844 #if __FreeBSD_version < 800000
1847 if_maddr_rlock(ifp);
1851 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1852 if (ifma->ifma_addr->sa_family != AF_LINK)
1855 if (mcnt == MAX_NUM_MULTICAST_ADDRESSES)
1858 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
1859 &mta[mcnt * ETH_ADDR_LEN], ETH_ADDR_LEN);
1863 #if __FreeBSD_version < 800000
1864 IF_ADDR_UNLOCK(ifp);
1866 if_maddr_runlock(ifp);
1870 if (mcnt >= MAX_NUM_MULTICAST_ADDRESSES) {
1871 reg_rctl = E1000_READ_REG(&adapter->hw, E1000_RCTL);
1872 reg_rctl |= E1000_RCTL_MPE;
1873 E1000_WRITE_REG(&adapter->hw, E1000_RCTL, reg_rctl);
1875 e1000_update_mc_addr_list(&adapter->hw, mta, mcnt);
1880 /*********************************************************************
1882 * This routine checks for link status,
1883 * updates statistics, and does the watchdog.
1885 **********************************************************************/
1888 igb_local_timer(void *arg)
1890 struct adapter *adapter = arg;
1892 IGB_CORE_LOCK(adapter);
1894 struct ifnet *ifp = adapter->ifp;
1895 device_t dev = adapter->dev;
1896 struct tx_ring *txr = adapter->tx_rings;
1899 IGB_CORE_LOCK_ASSERT(adapter);
1901 igb_update_link_status(adapter);
1902 igb_update_stats_counters(adapter);
1904 if (igb_display_debug_stats && ifp->if_flags & IFF_RUNNING)
1905 igb_print_hw_stats(adapter);
1908 ** Watchdog: check for time since any descriptor was cleaned
1910 for (int i = 0; i < adapter->num_queues; i++, txr++) {
1911 if (txr->watchdog_check == FALSE)
1913 if ((ticks - txr->watchdog_time) > IGB_WATCHDOG)
1917 /* Trigger an RX interrupt on all queues */
1918 #ifdef DEVICE_POLLING
1919 if ((ifp->if_flags & IFF_POLLING) == 0)
1921 E1000_WRITE_REG(&adapter->hw, E1000_EICS, adapter->rx_mask);
1922 callout_reset(&adapter->timer, hz, igb_local_timer, adapter);
1923 IGB_CORE_UNLOCK(adapter);
1927 device_printf(adapter->dev, "Watchdog timeout -- resetting\n");
1928 device_printf(dev,"Queue(%d) tdh = %d, hw tdt = %d\n", txr->me,
1929 E1000_READ_REG(&adapter->hw, E1000_TDH(txr->me)),
1930 E1000_READ_REG(&adapter->hw, E1000_TDT(txr->me)));
1931 device_printf(dev,"TX(%d) desc avail = %d,"
1932 "Next TX to Clean = %d\n",
1933 txr->me, txr->tx_avail, txr->next_to_clean);
1934 adapter->ifp->if_flags &= ~IFF_RUNNING;
1935 adapter->watchdog_events++;
1936 igb_init_locked(adapter);
1937 IGB_CORE_UNLOCK(adapter);
1941 igb_update_link_status(struct adapter *adapter)
1943 struct e1000_hw *hw = &adapter->hw;
1944 struct ifnet *ifp = adapter->ifp;
1945 device_t dev = adapter->dev;
1946 struct tx_ring *txr = adapter->tx_rings;
1949 /* Get the cached link value or read for real */
1950 switch (hw->phy.media_type) {
1951 case e1000_media_type_copper:
1952 if (hw->mac.get_link_status) {
1953 /* Do the work to read phy */
1954 e1000_check_for_link(hw);
1955 link_check = !hw->mac.get_link_status;
1959 case e1000_media_type_fiber:
1960 e1000_check_for_link(hw);
1961 link_check = (E1000_READ_REG(hw, E1000_STATUS) &
1964 case e1000_media_type_internal_serdes:
1965 e1000_check_for_link(hw);
1966 link_check = adapter->hw.mac.serdes_has_link;
1969 case e1000_media_type_unknown:
1973 /* Now we check if a transition has happened */
1974 if (link_check && (adapter->link_active == 0)) {
1975 e1000_get_speed_and_duplex(&adapter->hw,
1976 &adapter->link_speed, &adapter->link_duplex);
1978 device_printf(dev, "Link is up %d Mbps %s\n",
1979 adapter->link_speed,
1980 ((adapter->link_duplex == FULL_DUPLEX) ?
1981 "Full Duplex" : "Half Duplex"));
1982 adapter->link_active = 1;
1983 ifp->if_baudrate = adapter->link_speed * 1000000;
1984 ifp->if_link_state = LINK_STATE_UP;
1985 if_link_state_change(ifp);
1986 } else if (!link_check && (adapter->link_active == 1)) {
1987 ifp->if_baudrate = adapter->link_speed = 0;
1988 adapter->link_duplex = 0;
1990 device_printf(dev, "Link is Down\n");
1991 adapter->link_active = 0;
1992 ifp->if_link_state = LINK_STATE_DOWN;
1993 if_link_state_change(ifp);
1994 /* Turn off watchdogs */
1995 for (int i = 0; i < adapter->num_queues; i++, txr++)
1996 txr->watchdog_check = FALSE;
2000 /*********************************************************************
2002 * This routine disables all traffic on the adapter by issuing a
2003 * global reset on the MAC and deallocates TX/RX buffers.
2005 **********************************************************************/
2010 struct adapter *adapter = arg;
2011 struct ifnet *ifp = adapter->ifp;
2012 struct tx_ring *txr = adapter->tx_rings;
2014 IGB_CORE_LOCK_ASSERT(adapter);
2016 INIT_DEBUGOUT("igb_stop: begin");
2018 igb_disable_intr(adapter);
2020 callout_stop(&adapter->timer);
2022 /* Tell the stack that the interface is no longer active */
2023 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
2025 /* Unarm watchdog timer. */
2026 for (int i = 0; i < adapter->num_queues; i++, txr++) {
2028 txr->watchdog_check = FALSE;
2032 e1000_reset_hw(&adapter->hw);
2033 E1000_WRITE_REG(&adapter->hw, E1000_WUC, 0);
2037 /*********************************************************************
2039 * Determine hardware revision.
2041 **********************************************************************/
2043 igb_identify_hardware(struct adapter *adapter)
2045 device_t dev = adapter->dev;
2047 /* Make sure our PCI config space has the necessary stuff set */
2048 adapter->hw.bus.pci_cmd_word = pci_read_config(dev, PCIR_COMMAND, 2);
2049 if (!((adapter->hw.bus.pci_cmd_word & PCIM_CMD_BUSMASTEREN) &&
2050 (adapter->hw.bus.pci_cmd_word & PCIM_CMD_MEMEN))) {
2051 device_printf(dev, "Memory Access and/or Bus Master bits "
2053 adapter->hw.bus.pci_cmd_word |=
2054 (PCIM_CMD_BUSMASTEREN | PCIM_CMD_MEMEN);
2055 pci_write_config(dev, PCIR_COMMAND,
2056 adapter->hw.bus.pci_cmd_word, 2);
2059 /* Save off the information about this board */
2060 adapter->hw.vendor_id = pci_get_vendor(dev);
2061 adapter->hw.device_id = pci_get_device(dev);
2062 adapter->hw.revision_id = pci_read_config(dev, PCIR_REVID, 1);
2063 adapter->hw.subsystem_vendor_id =
2064 pci_read_config(dev, PCIR_SUBVEND_0, 2);
2065 adapter->hw.subsystem_device_id =
2066 pci_read_config(dev, PCIR_SUBDEV_0, 2);
2068 /* Do Shared Code Init and Setup */
2069 if (e1000_set_mac_type(&adapter->hw)) {
2070 device_printf(dev, "Setup init failure\n");
2076 igb_allocate_pci_resources(struct adapter *adapter)
2078 device_t dev = adapter->dev;
2082 adapter->pci_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
2084 if (adapter->pci_mem == NULL) {
2085 device_printf(dev, "Unable to allocate bus resource: memory\n");
2088 adapter->osdep.mem_bus_space_tag =
2089 rman_get_bustag(adapter->pci_mem);
2090 adapter->osdep.mem_bus_space_handle =
2091 rman_get_bushandle(adapter->pci_mem);
2092 adapter->hw.hw_addr = (u8 *)&adapter->osdep.mem_bus_space_handle;
2094 adapter->num_queues = 1; /* Defaults for Legacy or MSI */
2096 /* This will setup either MSI/X or MSI */
2097 adapter->msix = igb_setup_msix(adapter);
2098 adapter->hw.back = &adapter->osdep;
2103 /*********************************************************************
2105 * Setup the Legacy or MSI Interrupt handler
2107 **********************************************************************/
2109 igb_allocate_legacy(struct adapter *adapter)
2111 device_t dev = adapter->dev;
2114 /* Turn off all interrupts */
2115 E1000_WRITE_REG(&adapter->hw, E1000_IMC, 0xffffffff);
2119 if (adapter->msix == 1)
2123 /* We allocate a single interrupt resource */
2124 adapter->res = bus_alloc_resource_any(dev,
2125 SYS_RES_IRQ, &rid, RF_SHAREABLE | RF_ACTIVE);
2126 if (adapter->res == NULL) {
2127 device_printf(dev, "Unable to allocate bus resource: "
2133 * Try allocating a fast interrupt and the associated deferred
2134 * processing contexts.
2136 TASK_INIT(&adapter->rxtx_task, 0, igb_handle_rxtx, adapter);
2137 /* Make tasklet for deferred link handling */
2138 TASK_INIT(&adapter->link_task, 0, igb_handle_link, adapter);
2139 adapter->tq = taskqueue_create("igb_taskq", M_INTWAIT,
2140 taskqueue_thread_enqueue, &adapter->tq);
2141 taskqueue_start_threads(&adapter->tq, 1, TDPRI_KERN_DAEMON /*PI_NET*/, -1, "%s taskq",
2142 device_get_nameunit(adapter->dev));
2143 if ((error = bus_setup_intr(dev, adapter->res,
2144 /*INTR_TYPE_NET |*/ INTR_MPSAFE, igb_irq_fast,
2145 adapter, &adapter->tag, NULL)) != 0) {
2146 device_printf(dev, "Failed to register fast interrupt "
2147 "handler: %d\n", error);
2148 taskqueue_free(adapter->tq);
2157 /*********************************************************************
2159 * Setup the MSIX Queue Interrupt handlers:
2161 **********************************************************************/
2163 igb_allocate_msix(struct adapter *adapter)
2165 device_t dev = adapter->dev;
2166 struct igb_queue *que = adapter->queues;
2167 int error, rid, vector = 0;
2170 for (int i = 0; i < adapter->num_queues; i++, vector++, que++) {
2172 que->res = bus_alloc_resource_any(dev,
2173 SYS_RES_IRQ, &rid, RF_SHAREABLE | RF_ACTIVE);
2174 if (que->res == NULL) {
2176 "Unable to allocate bus resource: "
2177 "MSIX Queue Interrupt\n");
2180 error = bus_setup_intr(dev, que->res,
2181 /*INTR_TYPE_NET |*/ INTR_MPSAFE,
2182 igb_msix_que, que, &que->tag, NULL);
2185 device_printf(dev, "Failed to register Queue handler");
2189 if (adapter->hw.mac.type == e1000_82575)
2190 que->eims = E1000_EICR_TX_QUEUE0 << i;
2192 que->eims = 1 << vector;
2194 ** Bind the msix vector, and thus the
2195 ** rings to the corresponding cpu.
2198 if (adapter->num_queues > 1)
2199 bus_bind_intr(dev, que->res, i);
2201 /* Make tasklet for deferred handling */
2202 TASK_INIT(&que->que_task, 0, igb_handle_que, que);
2203 que->tq = taskqueue_create("igb_que", M_INTWAIT,
2204 taskqueue_thread_enqueue, &que->tq);
2205 taskqueue_start_threads(&que->tq, 1, TDPRI_KERN_DAEMON /*PI_NET*/, -1, "%s que",
2206 device_get_nameunit(adapter->dev));
2211 adapter->res = bus_alloc_resource_any(dev,
2212 SYS_RES_IRQ, &rid, RF_SHAREABLE | RF_ACTIVE);
2213 if (adapter->res == NULL) {
2215 "Unable to allocate bus resource: "
2216 "MSIX Link Interrupt\n");
2219 if ((error = bus_setup_intr(dev, adapter->res,
2220 /*INTR_TYPE_NET |*/ INTR_MPSAFE,
2221 igb_msix_link, adapter, &adapter->tag, NULL)) != 0) {
2222 device_printf(dev, "Failed to register Link handler");
2225 adapter->linkvec = vector;
2227 /* Make tasklet for deferred handling */
2228 TASK_INIT(&adapter->link_task, 0, igb_handle_link, adapter);
2229 adapter->tq = taskqueue_create("igb_link", M_INTWAIT,
2230 taskqueue_thread_enqueue, &adapter->tq);
2231 taskqueue_start_threads(&adapter->tq, 1, TDPRI_KERN_DAEMON /*PI_NET*/, -1, "%s link",
2232 device_get_nameunit(adapter->dev));
2239 igb_configure_queues(struct adapter *adapter)
2241 struct e1000_hw *hw = &adapter->hw;
2242 struct igb_queue *que;
2244 u32 newitr = IGB_DEFAULT_ITR;
2246 /* First turn on RSS capability */
2247 if (adapter->hw.mac.type > e1000_82575)
2248 E1000_WRITE_REG(hw, E1000_GPIE,
2249 E1000_GPIE_MSIX_MODE | E1000_GPIE_EIAME |
2250 E1000_GPIE_PBA | E1000_GPIE_NSICR);
2253 switch (adapter->hw.mac.type) {
2256 for (int i = 0; i < adapter->num_queues; i++) {
2258 ivar = E1000_READ_REG_ARRAY(hw, E1000_IVAR0, index);
2259 que = &adapter->queues[i];
2262 ivar |= (que->msix | E1000_IVAR_VALID) << 16;
2265 ivar |= que->msix | E1000_IVAR_VALID;
2267 E1000_WRITE_REG_ARRAY(hw, E1000_IVAR0, index, ivar);
2270 for (int i = 0; i < adapter->num_queues; i++) {
2272 ivar = E1000_READ_REG_ARRAY(hw, E1000_IVAR0, index);
2273 que = &adapter->queues[i];
2276 ivar |= (que->msix | E1000_IVAR_VALID) << 24;
2279 ivar |= (que->msix | E1000_IVAR_VALID) << 8;
2281 E1000_WRITE_REG_ARRAY(hw, E1000_IVAR0, index, ivar);
2282 adapter->eims_mask |= que->eims;
2285 /* And for the link interrupt */
2286 ivar = (adapter->linkvec | E1000_IVAR_VALID) << 8;
2287 adapter->link_mask = 1 << adapter->linkvec;
2288 adapter->eims_mask |= adapter->link_mask;
2289 E1000_WRITE_REG(hw, E1000_IVAR_MISC, ivar);
2293 for (int i = 0; i < adapter->num_queues; i++) {
2294 u32 index = i & 0x7; /* Each IVAR has two entries */
2295 ivar = E1000_READ_REG_ARRAY(hw, E1000_IVAR0, index);
2296 que = &adapter->queues[i];
2299 ivar |= que->msix | E1000_IVAR_VALID;
2302 ivar |= (que->msix | E1000_IVAR_VALID) << 16;
2304 E1000_WRITE_REG_ARRAY(hw, E1000_IVAR0, index, ivar);
2305 adapter->eims_mask |= que->eims;
2308 for (int i = 0; i < adapter->num_queues; i++) {
2309 u32 index = i & 0x7; /* Each IVAR has two entries */
2310 ivar = E1000_READ_REG_ARRAY(hw, E1000_IVAR0, index);
2311 que = &adapter->queues[i];
2314 ivar |= (que->msix | E1000_IVAR_VALID) << 8;
2317 ivar |= (que->msix | E1000_IVAR_VALID) << 24;
2319 E1000_WRITE_REG_ARRAY(hw, E1000_IVAR0, index, ivar);
2320 adapter->eims_mask |= que->eims;
2323 /* And for the link interrupt */
2324 ivar = (adapter->linkvec | E1000_IVAR_VALID) << 8;
2325 adapter->link_mask = 1 << adapter->linkvec;
2326 adapter->eims_mask |= adapter->link_mask;
2327 E1000_WRITE_REG(hw, E1000_IVAR_MISC, ivar);
2331 /* enable MSI-X support*/
2332 tmp = E1000_READ_REG(hw, E1000_CTRL_EXT);
2333 tmp |= E1000_CTRL_EXT_PBA_CLR;
2334 /* Auto-Mask interrupts upon ICR read. */
2335 tmp |= E1000_CTRL_EXT_EIAME;
2336 tmp |= E1000_CTRL_EXT_IRCA;
2337 E1000_WRITE_REG(hw, E1000_CTRL_EXT, tmp);
2340 for (int i = 0; i < adapter->num_queues; i++) {
2341 que = &adapter->queues[i];
2342 tmp = E1000_EICR_RX_QUEUE0 << i;
2343 tmp |= E1000_EICR_TX_QUEUE0 << i;
2345 E1000_WRITE_REG_ARRAY(hw, E1000_MSIXBM(0),
2347 adapter->eims_mask |= que->eims;
2351 E1000_WRITE_REG(hw, E1000_MSIXBM(adapter->linkvec),
2353 adapter->link_mask |= E1000_EIMS_OTHER;
2354 adapter->eims_mask |= adapter->link_mask;
2359 /* Set the starting interrupt rate */
2360 if (hw->mac.type == e1000_82575)
2361 newitr |= newitr << 16;
2363 newitr |= 0x8000000;
2365 for (int i = 0; i < adapter->num_queues; i++) {
2366 que = &adapter->queues[i];
2367 E1000_WRITE_REG(hw, E1000_EITR(que->msix), newitr);
2375 igb_free_pci_resources(struct adapter *adapter)
2377 struct igb_queue *que = adapter->queues;
2378 device_t dev = adapter->dev;
2382 ** There is a slight possibility of a failure mode
2383 ** in attach that will result in entering this function
2384 ** before interrupt resources have been initialized, and
2385 ** in that case we do not want to execute the loops below
2386 ** We can detect this reliably by the state of the adapter
2389 if (adapter->res == NULL)
2393 * First release all the interrupt resources:
2395 for (int i = 0; i < adapter->num_queues; i++, que++) {
2396 rid = que->msix + 1;
2397 if (que->tag != NULL) {
2398 bus_teardown_intr(dev, que->res, que->tag);
2401 if (que->res != NULL)
2402 bus_release_resource(dev,
2403 SYS_RES_IRQ, rid, que->res);
2406 /* Clean the Legacy or Link interrupt last */
2407 if (adapter->linkvec) /* we are doing MSIX */
2408 rid = adapter->linkvec + 1;
2410 (adapter->msix != 0) ? (rid = 1):(rid = 0);
2412 if (adapter->tag != NULL) {
2413 bus_teardown_intr(dev, adapter->res, adapter->tag);
2414 adapter->tag = NULL;
2416 if (adapter->res != NULL)
2417 bus_release_resource(dev, SYS_RES_IRQ, rid, adapter->res);
2421 pci_release_msi(dev);
2423 if (adapter->msix_mem != NULL)
2424 bus_release_resource(dev, SYS_RES_MEMORY,
2425 PCIR_BAR(IGB_MSIX_BAR), adapter->msix_mem);
2427 if (adapter->pci_mem != NULL)
2428 bus_release_resource(dev, SYS_RES_MEMORY,
2429 PCIR_BAR(0), adapter->pci_mem);
2434 * Setup Either MSI/X or MSI
2437 igb_setup_msix(struct adapter *adapter)
2439 device_t dev = adapter->dev;
2440 int rid, want, queues, msgs;
2442 /* tuneable override */
2443 if (igb_enable_msix == 0)
2446 /* First try MSI/X */
2447 rid = PCIR_BAR(IGB_MSIX_BAR);
2448 adapter->msix_mem = bus_alloc_resource_any(dev,
2449 SYS_RES_MEMORY, &rid, RF_ACTIVE);
2450 if (!adapter->msix_mem) {
2451 /* May not be enabled */
2452 device_printf(adapter->dev,
2453 "Unable to map MSIX table \n");
2457 msgs = pci_msix_count(dev);
2458 if (msgs == 0) { /* system has msix disabled */
2459 bus_release_resource(dev, SYS_RES_MEMORY,
2460 PCIR_BAR(IGB_MSIX_BAR), adapter->msix_mem);
2461 adapter->msix_mem = NULL;
2465 /* Figure out a reasonable auto config value */
2466 queues = (ncpus > (msgs-1)) ? (msgs-1) : ncpus;
2468 /* Can have max of 4 queues on 82575 */
2469 if (adapter->hw.mac.type == e1000_82575) {
2472 if (igb_num_queues > 4)
2476 if (igb_num_queues == 0)
2477 igb_num_queues = queues;
2480 ** One vector (RX/TX pair) per queue
2481 ** plus an additional for Link interrupt
2483 want = igb_num_queues + 1;
2487 device_printf(adapter->dev,
2488 "MSIX Configuration Problem, "
2489 "%d vectors configured, but %d queues wanted!\n",
2493 if ((msgs) && pci_alloc_msix(dev, &msgs) == 0) {
2494 device_printf(adapter->dev,
2495 "Using MSIX interrupts with %d vectors\n", msgs);
2496 adapter->num_queues = igb_num_queues;
2500 msgs = pci_msi_count(dev);
2501 if (msgs == 1 && pci_alloc_msi(dev, &msgs) == 0)
2502 device_printf(adapter->dev,"Using MSI interrupt\n");
2506 /*********************************************************************
2508 * Set up an fresh starting state
2510 **********************************************************************/
2512 igb_reset(struct adapter *adapter)
2514 device_t dev = adapter->dev;
2515 struct e1000_hw *hw = &adapter->hw;
2516 struct e1000_fc_info *fc = &hw->fc;
2517 struct ifnet *ifp = adapter->ifp;
2521 INIT_DEBUGOUT("igb_reset: begin");
2523 /* Let the firmware know the OS is in control */
2524 igb_get_hw_control(adapter);
2527 * Packet Buffer Allocation (PBA)
2528 * Writing PBA sets the receive portion of the buffer
2529 * the remainder is used for the transmit buffer.
2531 switch (hw->mac.type) {
2533 pba = E1000_PBA_32K;
2536 pba = E1000_PBA_64K;
2539 pba = E1000_PBA_35K;
2544 /* Special needs in case of Jumbo frames */
2545 if ((hw->mac.type == e1000_82575) && (ifp->if_mtu > ETHERMTU)) {
2546 u32 tx_space, min_tx, min_rx;
2547 pba = E1000_READ_REG(hw, E1000_PBA);
2548 tx_space = pba >> 16;
2550 min_tx = (adapter->max_frame_size +
2551 sizeof(struct e1000_tx_desc) - ETHERNET_FCS_SIZE) * 2;
2552 min_tx = roundup2(min_tx, 1024);
2554 min_rx = adapter->max_frame_size;
2555 min_rx = roundup2(min_rx, 1024);
2557 if (tx_space < min_tx &&
2558 ((min_tx - tx_space) < pba)) {
2559 pba = pba - (min_tx - tx_space);
2561 * if short on rx space, rx wins
2562 * and must trump tx adjustment
2567 E1000_WRITE_REG(hw, E1000_PBA, pba);
2570 INIT_DEBUGOUT1("igb_init: pba=%dK",pba);
2573 * These parameters control the automatic generation (Tx) and
2574 * response (Rx) to Ethernet PAUSE frames.
2575 * - High water mark should allow for at least two frames to be
2576 * received after sending an XOFF.
2577 * - Low water mark works best when it is very near the high water mark.
2578 * This allows the receiver to restart by sending XON when it has
2581 hwm = min(((pba << 10) * 9 / 10),
2582 ((pba << 10) - 2 * adapter->max_frame_size));
2584 if (hw->mac.type < e1000_82576) {
2585 fc->high_water = hwm & 0xFFF8; /* 8-byte granularity */
2586 fc->low_water = fc->high_water - 8;
2588 fc->high_water = hwm & 0xFFF0; /* 16-byte granularity */
2589 fc->low_water = fc->high_water - 16;
2592 fc->pause_time = IGB_FC_PAUSE_TIME;
2593 fc->send_xon = TRUE;
2595 /* Set Flow control, use the tunable location if sane */
2596 if ((igb_fc_setting >= 0) || (igb_fc_setting < 4))
2597 fc->requested_mode = igb_fc_setting;
2599 fc->requested_mode = e1000_fc_none;
2601 fc->current_mode = fc->requested_mode;
2603 /* Issue a global reset */
2605 E1000_WRITE_REG(hw, E1000_WUC, 0);
2607 if (e1000_init_hw(hw) < 0)
2608 device_printf(dev, "Hardware Initialization Failed\n");
2610 if (hw->mac.type == e1000_82580) {
2613 hwm = (pba << 10) - (2 * adapter->max_frame_size);
2615 * 0x80000000 - enable DMA COAL
2616 * 0x10000000 - use L0s as low power
2617 * 0x20000000 - use L1 as low power
2618 * X << 16 - exit dma coal when rx data exceeds X kB
2619 * Y - upper limit to stay in dma coal in units of 32usecs
2621 E1000_WRITE_REG(hw, E1000_DMACR,
2622 0xA0000006 | ((hwm << 6) & 0x00FF0000));
2624 /* set hwm to PBA - 2 * max frame size */
2625 E1000_WRITE_REG(hw, E1000_FCRTC, hwm);
2627 * This sets the time to wait before requesting transition to
2628 * low power state to number of usecs needed to receive 1 512
2629 * byte frame at gigabit line rate
2631 E1000_WRITE_REG(hw, E1000_DMCTLX, 4);
2633 /* free space in tx packet buffer to wake from DMA coal */
2634 E1000_WRITE_REG(hw, E1000_DMCTXTH,
2635 (20480 - (2 * adapter->max_frame_size)) >> 6);
2637 /* make low power state decision controlled by DMA coal */
2638 reg = E1000_READ_REG(hw, E1000_PCIEMISC);
2639 E1000_WRITE_REG(hw, E1000_PCIEMISC,
2640 reg | E1000_PCIEMISC_LX_DECISION);
2643 E1000_WRITE_REG(&adapter->hw, E1000_VET, ETHERTYPE_VLAN);
2644 e1000_get_phy_info(hw);
2645 e1000_check_for_link(hw);
2649 /*********************************************************************
2651 * Setup networking device structure and register an interface.
2653 **********************************************************************/
2655 igb_setup_interface(device_t dev, struct adapter *adapter)
2659 INIT_DEBUGOUT("igb_setup_interface: begin");
2661 ifp = adapter->ifp = &adapter->arpcom.ac_if;
2662 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
2663 ifp->if_mtu = ETHERMTU;
2664 ifp->if_init = igb_init;
2665 ifp->if_softc = adapter;
2666 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
2667 ifp->if_ioctl = igb_ioctl;
2668 ifp->if_start = igb_start;
2669 #ifdef DEVICE_POLLING
2670 ifp->if_poll = igb_poll;
2672 #if __FreeBSD_version >= 800000
2673 ifp->if_transmit = igb_mq_start;
2674 ifp->if_qflush = igb_qflush;
2676 ifq_set_maxlen(&ifp->if_snd, adapter->num_tx_desc - 1);
2677 ifq_set_ready(&ifp->if_snd);
2679 ether_ifattach(ifp, adapter->hw.mac.addr, NULL);
2681 ifp->if_capabilities = ifp->if_capenable = 0;
2683 ifp->if_capabilities = IFCAP_HWCSUM | IFCAP_VLAN_MTU;
2685 ifp->if_capabilities |= IFCAP_TSO4;
2687 ifp->if_capabilities |= IFCAP_JUMBO_MTU;
2689 if (igb_header_split)
2690 ifp->if_capabilities |= IFCAP_LRO;
2693 ifp->if_capenable = ifp->if_capabilities;
2696 * Tell the upper layer(s) we support long frames.
2698 ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
2699 ifp->if_capabilities |= IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_MTU;
2700 ifp->if_capenable |= IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_MTU;
2703 * Specify the media types supported by this adapter and register
2704 * callbacks to update media and link information
2706 ifmedia_init(&adapter->media, IFM_IMASK,
2707 igb_media_change, igb_media_status);
2708 if ((adapter->hw.phy.media_type == e1000_media_type_fiber) ||
2709 (adapter->hw.phy.media_type == e1000_media_type_internal_serdes)) {
2710 ifmedia_add(&adapter->media, IFM_ETHER | IFM_1000_SX | IFM_FDX,
2712 ifmedia_add(&adapter->media, IFM_ETHER | IFM_1000_SX, 0, NULL);
2714 ifmedia_add(&adapter->media, IFM_ETHER | IFM_10_T, 0, NULL);
2715 ifmedia_add(&adapter->media, IFM_ETHER | IFM_10_T | IFM_FDX,
2717 ifmedia_add(&adapter->media, IFM_ETHER | IFM_100_TX,
2719 ifmedia_add(&adapter->media, IFM_ETHER | IFM_100_TX | IFM_FDX,
2721 if (adapter->hw.phy.type != e1000_phy_ife) {
2722 ifmedia_add(&adapter->media,
2723 IFM_ETHER | IFM_1000_T | IFM_FDX, 0, NULL);
2724 ifmedia_add(&adapter->media,
2725 IFM_ETHER | IFM_1000_T, 0, NULL);
2728 ifmedia_add(&adapter->media, IFM_ETHER | IFM_AUTO, 0, NULL);
2729 ifmedia_set(&adapter->media, IFM_ETHER | IFM_AUTO);
2734 * Manage DMA'able memory.
2737 igb_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
2741 *(bus_addr_t *) arg = segs[0].ds_addr;
2745 igb_dma_malloc(struct adapter *adapter, bus_size_t size,
2746 struct igb_dma_alloc *dma, int mapflags)
2750 error = bus_dma_tag_create(NULL, /* parent */
2751 IGB_DBA_ALIGN, 0, /* alignment, bounds */
2752 BUS_SPACE_MAXADDR, /* lowaddr */
2753 BUS_SPACE_MAXADDR, /* highaddr */
2754 NULL, NULL, /* filter, filterarg */
2757 size, /* maxsegsize */
2761 device_printf(adapter->dev,
2762 "%s: bus_dma_tag_create failed: %d\n",
2767 error = bus_dmamem_alloc(dma->dma_tag, (void**) &dma->dma_vaddr,
2768 BUS_DMA_NOWAIT, &dma->dma_map);
2770 device_printf(adapter->dev,
2771 "%s: bus_dmamem_alloc(%ju) failed: %d\n",
2772 __func__, (uintmax_t)size, error);
2777 error = bus_dmamap_load(dma->dma_tag, dma->dma_map, dma->dma_vaddr,
2778 size, igb_dmamap_cb, &dma->dma_paddr, mapflags | BUS_DMA_NOWAIT);
2779 if (error || dma->dma_paddr == 0) {
2780 device_printf(adapter->dev,
2781 "%s: bus_dmamap_load failed: %d\n",
2789 bus_dmamap_unload(dma->dma_tag, dma->dma_map);
2791 bus_dmamem_free(dma->dma_tag, dma->dma_vaddr, dma->dma_map);
2792 bus_dma_tag_destroy(dma->dma_tag);
2794 dma->dma_map = NULL;
2795 dma->dma_tag = NULL;
2801 igb_dma_free(struct adapter *adapter, struct igb_dma_alloc *dma)
2803 if (dma->dma_tag == NULL)
2805 if (dma->dma_map != NULL) {
2806 bus_dmamap_sync(dma->dma_tag, dma->dma_map,
2807 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
2808 bus_dmamap_unload(dma->dma_tag, dma->dma_map);
2809 bus_dmamem_free(dma->dma_tag, dma->dma_vaddr, dma->dma_map);
2810 dma->dma_map = NULL;
2812 bus_dma_tag_destroy(dma->dma_tag);
2813 dma->dma_tag = NULL;
2817 /*********************************************************************
2819 * Allocate memory for the transmit and receive rings, and then
2820 * the descriptors associated with each, called only once at attach.
2822 **********************************************************************/
2824 igb_allocate_queues(struct adapter *adapter)
2826 device_t dev = adapter->dev;
2827 struct igb_queue *que = NULL;
2828 struct tx_ring *txr = NULL;
2829 struct rx_ring *rxr = NULL;
2830 int rsize, tsize, error = E1000_SUCCESS;
2831 int txconf = 0, rxconf = 0;
2833 /* First allocate the top level queue structs */
2834 if (!(adapter->queues =
2835 (struct igb_queue *) kmalloc(sizeof(struct igb_queue) *
2836 adapter->num_queues, M_DEVBUF, M_INTWAIT | M_ZERO))) {
2837 device_printf(dev, "Unable to allocate queue memory\n");
2842 /* Next allocate the TX ring struct memory */
2843 if (!(adapter->tx_rings =
2844 (struct tx_ring *) kmalloc(sizeof(struct tx_ring) *
2845 adapter->num_queues, M_DEVBUF, M_INTWAIT | M_ZERO))) {
2846 device_printf(dev, "Unable to allocate TX ring memory\n");
2851 /* Now allocate the RX */
2852 if (!(adapter->rx_rings =
2853 (struct rx_ring *) kmalloc(sizeof(struct rx_ring) *
2854 adapter->num_queues, M_DEVBUF, M_INTWAIT | M_ZERO))) {
2855 device_printf(dev, "Unable to allocate RX ring memory\n");
2860 tsize = roundup2(adapter->num_tx_desc *
2861 sizeof(union e1000_adv_tx_desc), IGB_DBA_ALIGN);
2863 * Now set up the TX queues, txconf is needed to handle the
2864 * possibility that things fail midcourse and we need to
2865 * undo memory gracefully
2867 for (int i = 0; i < adapter->num_queues; i++, txconf++) {
2868 /* Set up some basics */
2869 txr = &adapter->tx_rings[i];
2870 txr->adapter = adapter;
2873 /* Initialize the TX lock */
2874 ksnprintf(txr->spin_name, sizeof(txr->spin_name), "%s:tx(%d)",
2875 device_get_nameunit(dev), txr->me);
2877 IGB_TX_LOCK_INIT(txr);
2879 if (igb_dma_malloc(adapter, tsize,
2880 &txr->txdma, BUS_DMA_NOWAIT)) {
2882 "Unable to allocate TX Descriptor memory\n");
2886 txr->tx_base = (struct e1000_tx_desc *)txr->txdma.dma_vaddr;
2887 bzero((void *)txr->tx_base, tsize);
2889 /* Now allocate transmit buffers for the ring */
2890 if (igb_allocate_transmit_buffers(txr)) {
2892 "Critical Failure setting up transmit buffers\n");
2896 #if __FreeBSD_version >= 800000
2897 /* Allocate a buf ring */
2898 txr->br = buf_ring_alloc(IGB_BR_SIZE, M_DEVBUF,
2899 M_WAITOK, &txr->tx_mtx);
2904 * Next the RX queues...
2906 rsize = roundup2(adapter->num_rx_desc *
2907 sizeof(union e1000_adv_rx_desc), IGB_DBA_ALIGN);
2908 for (int i = 0; i < adapter->num_queues; i++, rxconf++) {
2909 rxr = &adapter->rx_rings[i];
2910 rxr->adapter = adapter;
2913 /* Initialize the RX lock */
2914 ksnprintf(rxr->spin_name, sizeof(rxr->spin_name), "%s:rx(%d)",
2915 device_get_nameunit(dev), txr->me);
2917 IGB_RX_LOCK_INIT(rxr);
2919 if (igb_dma_malloc(adapter, rsize,
2920 &rxr->rxdma, BUS_DMA_NOWAIT)) {
2922 "Unable to allocate RxDescriptor memory\n");
2926 rxr->rx_base = (union e1000_adv_rx_desc *)rxr->rxdma.dma_vaddr;
2927 bzero((void *)rxr->rx_base, rsize);
2929 /* Allocate receive buffers for the ring*/
2930 if (igb_allocate_receive_buffers(rxr)) {
2932 "Critical Failure setting up receive buffers\n");
2939 ** Finally set up the queue holding structs
2941 for (int i = 0; i < adapter->num_queues; i++) {
2942 que = &adapter->queues[i];
2943 que->adapter = adapter;
2944 que->txr = &adapter->tx_rings[i];
2945 que->rxr = &adapter->rx_rings[i];
2951 for (rxr = adapter->rx_rings; rxconf > 0; rxr++, rxconf--)
2952 igb_dma_free(adapter, &rxr->rxdma);
2954 for (txr = adapter->tx_rings; txconf > 0; txr++, txconf--)
2955 igb_dma_free(adapter, &txr->txdma);
2956 kfree(adapter->rx_rings, M_DEVBUF);
2958 #if __FreeBSD_version >= 800000
2959 buf_ring_free(txr->br, M_DEVBUF);
2961 kfree(adapter->tx_rings, M_DEVBUF);
2963 kfree(adapter->queues, M_DEVBUF);
2968 /*********************************************************************
2970 * Allocate memory for tx_buffer structures. The tx_buffer stores all
2971 * the information needed to transmit a packet on the wire. This is
2972 * called only once at attach, setup is done every reset.
2974 **********************************************************************/
2976 igb_allocate_transmit_buffers(struct tx_ring *txr)
2978 struct adapter *adapter = txr->adapter;
2979 device_t dev = adapter->dev;
2980 struct igb_tx_buffer *txbuf;
2984 * Setup DMA descriptor areas.
2986 if ((error = bus_dma_tag_create(NULL,
2987 1, 0, /* alignment, bounds */
2988 BUS_SPACE_MAXADDR, /* lowaddr */
2989 BUS_SPACE_MAXADDR, /* highaddr */
2990 NULL, NULL, /* filter, filterarg */
2991 IGB_TSO_SIZE, /* maxsize */
2992 IGB_MAX_SCATTER, /* nsegments */
2993 PAGE_SIZE, /* maxsegsize */
2996 device_printf(dev,"Unable to allocate TX DMA tag\n");
3000 if (!(txr->tx_buffers =
3001 (struct igb_tx_buffer *) kmalloc(sizeof(struct igb_tx_buffer) *
3002 adapter->num_tx_desc, M_DEVBUF, M_INTWAIT | M_ZERO))) {
3003 device_printf(dev, "Unable to allocate tx_buffer memory\n");
3008 /* Create the descriptor buffer dma maps */
3009 txbuf = txr->tx_buffers;
3010 for (i = 0; i < adapter->num_tx_desc; i++, txbuf++) {
3011 error = bus_dmamap_create(txr->txtag, 0, &txbuf->map);
3013 device_printf(dev, "Unable to create TX DMA map\n");
3020 /* We free all, it handles case where we are in the middle */
3021 igb_free_transmit_structures(adapter);
3025 /*********************************************************************
3027 * Initialize a transmit ring.
3029 **********************************************************************/
3031 igb_setup_transmit_ring(struct tx_ring *txr)
3033 struct adapter *adapter = txr->adapter;
3034 struct igb_tx_buffer *txbuf;
3037 /* Clear the old descriptor contents */
3039 bzero((void *)txr->tx_base,
3040 (sizeof(union e1000_adv_tx_desc)) * adapter->num_tx_desc);
3042 txr->next_avail_desc = 0;
3043 txr->next_to_clean = 0;
3045 /* Free any existing tx buffers. */
3046 txbuf = txr->tx_buffers;
3047 for (i = 0; i < adapter->num_tx_desc; i++, txbuf++) {
3048 if (txbuf->m_head != NULL) {
3049 bus_dmamap_sync(txr->txtag, txbuf->map,
3050 BUS_DMASYNC_POSTWRITE);
3051 bus_dmamap_unload(txr->txtag, txbuf->map);
3052 m_freem(txbuf->m_head);
3053 txbuf->m_head = NULL;
3055 /* clear the watch index */
3056 txbuf->next_eop = -1;
3059 /* Set number of descriptors available */
3060 txr->tx_avail = adapter->num_tx_desc;
3062 bus_dmamap_sync(txr->txdma.dma_tag, txr->txdma.dma_map,
3063 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
3067 /*********************************************************************
3069 * Initialize all transmit rings.
3071 **********************************************************************/
3073 igb_setup_transmit_structures(struct adapter *adapter)
3075 struct tx_ring *txr = adapter->tx_rings;
3077 for (int i = 0; i < adapter->num_queues; i++, txr++)
3078 igb_setup_transmit_ring(txr);
3083 /*********************************************************************
3085 * Enable transmit unit.
3087 **********************************************************************/
3089 igb_initialize_transmit_units(struct adapter *adapter)
3091 struct tx_ring *txr = adapter->tx_rings;
3092 struct e1000_hw *hw = &adapter->hw;
3095 INIT_DEBUGOUT("igb_initialize_transmit_units: begin");
3097 /* Setup the Tx Descriptor Rings */
3098 for (int i = 0; i < adapter->num_queues; i++, txr++) {
3099 u64 bus_addr = txr->txdma.dma_paddr;
3101 E1000_WRITE_REG(hw, E1000_TDLEN(i),
3102 adapter->num_tx_desc * sizeof(struct e1000_tx_desc));
3103 E1000_WRITE_REG(hw, E1000_TDBAH(i),
3104 (uint32_t)(bus_addr >> 32));
3105 E1000_WRITE_REG(hw, E1000_TDBAL(i),
3106 (uint32_t)bus_addr);
3108 /* Setup the HW Tx Head and Tail descriptor pointers */
3109 E1000_WRITE_REG(hw, E1000_TDT(i), 0);
3110 E1000_WRITE_REG(hw, E1000_TDH(i), 0);
3112 HW_DEBUGOUT2("Base = %x, Length = %x\n",
3113 E1000_READ_REG(hw, E1000_TDBAL(i)),
3114 E1000_READ_REG(hw, E1000_TDLEN(i)));
3116 txr->watchdog_check = FALSE;
3118 txdctl = E1000_READ_REG(hw, E1000_TXDCTL(i));
3119 txdctl |= IGB_TX_PTHRESH;
3120 txdctl |= IGB_TX_HTHRESH << 8;
3121 txdctl |= IGB_TX_WTHRESH << 16;
3122 txdctl |= E1000_TXDCTL_QUEUE_ENABLE;
3123 E1000_WRITE_REG(hw, E1000_TXDCTL(i), txdctl);
3126 /* Program the Transmit Control Register */
3127 tctl = E1000_READ_REG(hw, E1000_TCTL);
3128 tctl &= ~E1000_TCTL_CT;
3129 tctl |= (E1000_TCTL_PSP | E1000_TCTL_RTLC | E1000_TCTL_EN |
3130 (E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT));
3132 e1000_config_collision_dist(hw);
3134 /* This write will effectively turn on the transmit unit. */
3135 E1000_WRITE_REG(hw, E1000_TCTL, tctl);
3138 /*********************************************************************
3140 * Free all transmit rings.
3142 **********************************************************************/
3144 igb_free_transmit_structures(struct adapter *adapter)
3146 struct tx_ring *txr = adapter->tx_rings;
3148 for (int i = 0; i < adapter->num_queues; i++, txr++) {
3150 igb_free_transmit_buffers(txr);
3151 igb_dma_free(adapter, &txr->txdma);
3153 IGB_TX_LOCK_DESTROY(txr);
3155 kfree(adapter->tx_rings, M_DEVBUF);
3158 /*********************************************************************
3160 * Free transmit ring related data structures.
3162 **********************************************************************/
3164 igb_free_transmit_buffers(struct tx_ring *txr)
3166 struct adapter *adapter = txr->adapter;
3167 struct igb_tx_buffer *tx_buffer;
3170 INIT_DEBUGOUT("free_transmit_ring: begin");
3172 if (txr->tx_buffers == NULL)
3175 tx_buffer = txr->tx_buffers;
3176 for (i = 0; i < adapter->num_tx_desc; i++, tx_buffer++) {
3177 if (tx_buffer->m_head != NULL) {
3178 bus_dmamap_sync(txr->txtag, tx_buffer->map,
3179 BUS_DMASYNC_POSTWRITE);
3180 bus_dmamap_unload(txr->txtag,
3182 m_freem(tx_buffer->m_head);
3183 tx_buffer->m_head = NULL;
3184 if (tx_buffer->map != NULL) {
3185 bus_dmamap_destroy(txr->txtag,
3187 tx_buffer->map = NULL;
3189 } else if (tx_buffer->map != NULL) {
3190 bus_dmamap_unload(txr->txtag,
3192 bus_dmamap_destroy(txr->txtag,
3194 tx_buffer->map = NULL;
3197 #if __FreeBSD_version >= 800000
3198 if (txr->br != NULL)
3199 buf_ring_free(txr->br, M_DEVBUF);
3201 if (txr->tx_buffers != NULL) {
3202 kfree(txr->tx_buffers, M_DEVBUF);
3203 txr->tx_buffers = NULL;
3205 if (txr->txtag != NULL) {
3206 bus_dma_tag_destroy(txr->txtag);
3212 /**********************************************************************
3214 * Setup work for hardware segmentation offload (TSO)
3216 **********************************************************************/
3219 igb_tso_setup(struct tx_ring *txr, struct mbuf *mp, u32 *hdrlen)
3221 struct adapter *adapter = txr->adapter;
3222 struct e1000_adv_tx_context_desc *TXD;
3223 struct igb_tx_buffer *tx_buffer;
3224 u32 vlan_macip_lens = 0, type_tucmd_mlhl = 0;
3225 u32 mss_l4len_idx = 0;
3227 int ctxd, ehdrlen, ip_hlen, tcp_hlen;
3228 struct ether_vlan_header *eh;
3234 * Determine where frame payload starts.
3235 * Jump over vlan headers if already present
3237 eh = mtod(mp, struct ether_vlan_header *);
3238 if (eh->evl_encap_proto == htons(ETHERTYPE_VLAN))
3239 ehdrlen = ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN;
3241 ehdrlen = ETHER_HDR_LEN;
3243 /* Ensure we have at least the IP+TCP header in the first mbuf. */
3244 if (mp->m_len < ehdrlen + sizeof(struct ip) + sizeof(struct tcphdr))
3247 /* Only supports IPV4 for now */
3248 ctxd = txr->next_avail_desc;
3249 tx_buffer = &txr->tx_buffers[ctxd];
3250 TXD = (struct e1000_adv_tx_context_desc *) &txr->tx_base[ctxd];
3252 ip = (struct ip *)(mp->m_data + ehdrlen);
3253 if (ip->ip_p != IPPROTO_TCP)
3254 return FALSE; /* 0 */
3256 ip_hlen = ip->ip_hl << 2;
3257 th = (struct tcphdr *)((caddr_t)ip + ip_hlen);
3258 th->th_sum = in_pseudo(ip->ip_src.s_addr,
3259 ip->ip_dst.s_addr, htons(IPPROTO_TCP));
3260 tcp_hlen = th->th_off << 2;
3262 * Calculate header length, this is used
3263 * in the transmit desc in igb_xmit
3265 *hdrlen = ehdrlen + ip_hlen + tcp_hlen;
3267 /* VLAN MACLEN IPLEN */
3268 if (mp->m_flags & M_VLANTAG) {
3269 vtag = htole16(mp->m_pkthdr.ether_vlantag);
3270 vlan_macip_lens |= (vtag << E1000_ADVTXD_VLAN_SHIFT);
3273 vlan_macip_lens |= (ehdrlen << E1000_ADVTXD_MACLEN_SHIFT);
3274 vlan_macip_lens |= ip_hlen;
3275 TXD->vlan_macip_lens |= htole32(vlan_macip_lens);
3277 /* ADV DTYPE TUCMD */
3278 type_tucmd_mlhl |= E1000_ADVTXD_DCMD_DEXT | E1000_ADVTXD_DTYP_CTXT;
3279 type_tucmd_mlhl |= E1000_ADVTXD_TUCMD_L4T_TCP;
3280 type_tucmd_mlhl |= E1000_ADVTXD_TUCMD_IPV4;
3281 TXD->type_tucmd_mlhl |= htole32(type_tucmd_mlhl);
3284 mss_l4len_idx |= (mp->m_pkthdr.tso_segsz << E1000_ADVTXD_MSS_SHIFT);
3285 mss_l4len_idx |= (tcp_hlen << E1000_ADVTXD_L4LEN_SHIFT);
3286 /* 82575 needs the queue index added */
3287 if (adapter->hw.mac.type == e1000_82575)
3288 mss_l4len_idx |= txr->me << 4;
3289 TXD->mss_l4len_idx = htole32(mss_l4len_idx);
3291 TXD->seqnum_seed = htole32(0);
3292 tx_buffer->m_head = NULL;
3293 tx_buffer->next_eop = -1;
3295 if (++ctxd == adapter->num_tx_desc)
3299 txr->next_avail_desc = ctxd;
3304 /*********************************************************************
3306 * Context Descriptor setup for VLAN or CSUM
3308 **********************************************************************/
3311 igb_tx_ctx_setup(struct tx_ring *txr, struct mbuf *mp)
3313 struct adapter *adapter = txr->adapter;
3314 struct e1000_adv_tx_context_desc *TXD;
3315 struct igb_tx_buffer *tx_buffer;
3316 u32 vlan_macip_lens, type_tucmd_mlhl, mss_l4len_idx;
3317 struct ether_vlan_header *eh;
3318 struct ip *ip = NULL;
3319 struct ip6_hdr *ip6;
3320 int ehdrlen, ctxd, ip_hlen = 0;
3321 u16 etype, vtag = 0;
3323 bool offload = TRUE;
3325 if ((mp->m_pkthdr.csum_flags & CSUM_OFFLOAD) == 0)
3328 vlan_macip_lens = type_tucmd_mlhl = mss_l4len_idx = 0;
3329 ctxd = txr->next_avail_desc;
3330 tx_buffer = &txr->tx_buffers[ctxd];
3331 TXD = (struct e1000_adv_tx_context_desc *) &txr->tx_base[ctxd];
3334 ** In advanced descriptors the vlan tag must
3335 ** be placed into the context descriptor, thus
3336 ** we need to be here just for that setup.
3338 if (mp->m_flags & M_VLANTAG) {
3339 vtag = htole16(mp->m_pkthdr.ether_vlantag);
3340 vlan_macip_lens |= (vtag << E1000_ADVTXD_VLAN_SHIFT);
3341 } else if (offload == FALSE)
3345 * Determine where frame payload starts.
3346 * Jump over vlan headers if already present,
3347 * helpful for QinQ too.
3349 eh = mtod(mp, struct ether_vlan_header *);
3350 if (eh->evl_encap_proto == htons(ETHERTYPE_VLAN)) {
3351 etype = ntohs(eh->evl_proto);
3352 ehdrlen = ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN;
3354 etype = ntohs(eh->evl_encap_proto);
3355 ehdrlen = ETHER_HDR_LEN;
3358 /* Set the ether header length */
3359 vlan_macip_lens |= ehdrlen << E1000_ADVTXD_MACLEN_SHIFT;
3363 ip = (struct ip *)(mp->m_data + ehdrlen);
3364 ip_hlen = ip->ip_hl << 2;
3365 if (mp->m_len < ehdrlen + ip_hlen) {
3370 type_tucmd_mlhl |= E1000_ADVTXD_TUCMD_IPV4;
3372 case ETHERTYPE_IPV6:
3373 ip6 = (struct ip6_hdr *)(mp->m_data + ehdrlen);
3374 ip_hlen = sizeof(struct ip6_hdr);
3375 if (mp->m_len < ehdrlen + ip_hlen)
3377 ipproto = ip6->ip6_nxt;
3378 type_tucmd_mlhl |= E1000_ADVTXD_TUCMD_IPV6;
3385 vlan_macip_lens |= ip_hlen;
3386 type_tucmd_mlhl |= E1000_ADVTXD_DCMD_DEXT | E1000_ADVTXD_DTYP_CTXT;
3390 if (mp->m_pkthdr.csum_flags & CSUM_TCP)
3391 type_tucmd_mlhl |= E1000_ADVTXD_TUCMD_L4T_TCP;
3394 if (mp->m_pkthdr.csum_flags & CSUM_UDP)
3395 type_tucmd_mlhl |= E1000_ADVTXD_TUCMD_L4T_UDP;
3397 #if __FreeBSD_version >= 800000
3399 if (mp->m_pkthdr.csum_flags & CSUM_SCTP)
3400 type_tucmd_mlhl |= E1000_ADVTXD_TUCMD_L4T_SCTP;
3408 /* 82575 needs the queue index added */
3409 if (adapter->hw.mac.type == e1000_82575)
3410 mss_l4len_idx = txr->me << 4;
3412 /* Now copy bits into descriptor */
3413 TXD->vlan_macip_lens |= htole32(vlan_macip_lens);
3414 TXD->type_tucmd_mlhl |= htole32(type_tucmd_mlhl);
3415 TXD->seqnum_seed = htole32(0);
3416 TXD->mss_l4len_idx = htole32(mss_l4len_idx);
3418 tx_buffer->m_head = NULL;
3419 tx_buffer->next_eop = -1;
3421 /* We've consumed the first desc, adjust counters */
3422 if (++ctxd == adapter->num_tx_desc)
3424 txr->next_avail_desc = ctxd;
3431 /**********************************************************************
3433 * Examine each tx_buffer in the used queue. If the hardware is done
3434 * processing the packet then free associated resources. The
3435 * tx_buffer is put back on the free queue.
3437 * TRUE return means there's work in the ring to clean, FALSE its empty.
3438 **********************************************************************/
3440 igb_txeof(struct tx_ring *txr)
3442 struct adapter *adapter = txr->adapter;
3443 int first, last, done;
3444 struct igb_tx_buffer *tx_buffer;
3445 struct e1000_tx_desc *tx_desc, *eop_desc;
3446 struct ifnet *ifp = adapter->ifp;
3448 IGB_TX_LOCK_ASSERT(txr);
3450 if (txr->tx_avail == adapter->num_tx_desc)
3453 first = txr->next_to_clean;
3454 tx_desc = &txr->tx_base[first];
3455 tx_buffer = &txr->tx_buffers[first];
3456 last = tx_buffer->next_eop;
3457 eop_desc = &txr->tx_base[last];
3460 * What this does is get the index of the
3461 * first descriptor AFTER the EOP of the
3462 * first packet, that way we can do the
3463 * simple comparison on the inner while loop.
3465 if (++last == adapter->num_tx_desc)
3469 bus_dmamap_sync(txr->txdma.dma_tag, txr->txdma.dma_map,
3470 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
3472 while (eop_desc->upper.fields.status & E1000_TXD_STAT_DD) {
3473 /* We clean the range of the packet */
3474 while (first != done) {
3475 tx_desc->upper.data = 0;
3476 tx_desc->lower.data = 0;
3477 tx_desc->buffer_addr = 0;
3480 if (tx_buffer->m_head) {
3482 tx_buffer->m_head->m_pkthdr.len;
3483 bus_dmamap_sync(txr->txtag,
3485 BUS_DMASYNC_POSTWRITE);
3486 bus_dmamap_unload(txr->txtag,
3489 m_freem(tx_buffer->m_head);
3490 tx_buffer->m_head = NULL;
3492 tx_buffer->next_eop = -1;
3493 txr->watchdog_time = ticks;
3495 if (++first == adapter->num_tx_desc)
3498 tx_buffer = &txr->tx_buffers[first];
3499 tx_desc = &txr->tx_base[first];
3503 /* See if we can continue to the next packet */
3504 last = tx_buffer->next_eop;
3506 eop_desc = &txr->tx_base[last];
3507 /* Get new done point */
3508 if (++last == adapter->num_tx_desc) last = 0;
3513 bus_dmamap_sync(txr->txdma.dma_tag, txr->txdma.dma_map,
3514 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
3516 txr->next_to_clean = first;
3519 * If we have enough room, clear IFF_DRV_OACTIVE
3520 * to tell the stack that it is OK to send packets.
3522 if (txr->tx_avail > IGB_TX_CLEANUP_THRESHOLD) {
3523 ifp->if_flags &= ~IFF_OACTIVE;
3524 /* All clean, turn off the watchdog */
3525 if (txr->tx_avail == adapter->num_tx_desc) {
3526 txr->watchdog_check = FALSE;
3535 /*********************************************************************
3537 * Setup descriptor buffer(s) from system mbuf buffer pools.
3538 * i - designates the ring index
3539 * clean - tells the function whether to update
3540 * the header, the packet buffer, or both.
3542 **********************************************************************/
3544 igb_get_buf(struct rx_ring *rxr, int i, u8 clean)
3546 struct adapter *adapter = rxr->adapter;
3547 struct igb_rx_buf *rxbuf;
3548 struct mbuf *mh, *mp;
3549 bus_dma_segment_t hseg[1];
3550 bus_dma_segment_t pseg[1];
3556 * Init-time loads are allowed to use a blocking mbuf allocation,
3557 * otherwise the sheer number of mbufs allocated can lead to
3560 mbflags = (clean & IGB_CLEAN_INITIAL) ? MB_WAIT : MB_DONTWAIT;
3562 rxbuf = &rxr->rx_buffers[i];
3564 if ((clean & IGB_CLEAN_HEADER) != 0) {
3565 mh = m_gethdr(mbflags, MT_DATA);
3567 adapter->mbuf_header_failed++;
3570 mh->m_pkthdr.len = mh->m_len = MHLEN;
3572 * Because IGB_HDR_BUF size is less than MHLEN
3573 * and we configure controller to split headers
3574 * we can align mbuf on ETHER_ALIGN boundary.
3576 m_adj(mh, ETHER_ALIGN);
3577 error = bus_dmamap_load_mbuf_segment(rxr->rx_htag,
3578 rxr->rx_hspare_map, mh, hseg, 1, &nsegs, BUS_DMA_NOWAIT);
3583 mh->m_flags &= ~M_PKTHDR;
3585 if ((clean & IGB_CLEAN_PAYLOAD) != 0) {
3586 mp = m_getl(adapter->rx_mbuf_sz, mbflags, MT_DATA,
3589 mp = m_getjcl(MB_DONTWAIT, MT_DATA, M_PKTHDR,
3590 adapter->rx_mbuf_sz);
3594 adapter->mbuf_packet_failed++;
3595 bus_dmamap_unload(rxr->rx_htag,
3597 mh->m_flags |= M_PKTHDR;
3602 mp->m_pkthdr.len = mp->m_len = adapter->rx_mbuf_sz;
3603 error = bus_dmamap_load_mbuf_segment(rxr->rx_ptag,
3604 rxr->rx_pspare_map, mp, pseg, 1, &nsegs, BUS_DMA_NOWAIT);
3607 bus_dmamap_unload(rxr->rx_htag,
3609 mh->m_flags |= M_PKTHDR;
3615 mp->m_flags &= ~M_PKTHDR;
3618 /* Loading new DMA maps complete, unload maps for received buffers. */
3619 if ((clean & IGB_CLEAN_HEADER) != 0 && rxbuf->m_head != NULL) {
3620 bus_dmamap_sync(rxr->rx_htag, rxbuf->head_map,
3621 BUS_DMASYNC_POSTREAD);
3622 bus_dmamap_unload(rxr->rx_htag, rxbuf->head_map);
3624 if ((clean & IGB_CLEAN_PAYLOAD) != 0 && rxbuf->m_pack != NULL) {
3625 bus_dmamap_sync(rxr->rx_ptag, rxbuf->pack_map,
3626 BUS_DMASYNC_POSTREAD);
3627 bus_dmamap_unload(rxr->rx_ptag, rxbuf->pack_map);
3630 /* Reflect loaded dmamaps. */
3631 if ((clean & IGB_CLEAN_HEADER) != 0) {
3632 map = rxbuf->head_map;
3633 rxbuf->head_map = rxr->rx_hspare_map;
3634 rxr->rx_hspare_map = map;
3636 bus_dmamap_sync(rxr->rx_htag, rxbuf->head_map,
3637 BUS_DMASYNC_PREREAD);
3638 rxr->rx_base[i].read.hdr_addr = htole64(hseg[0].ds_addr);
3640 if ((clean & IGB_CLEAN_PAYLOAD) != 0) {
3641 map = rxbuf->pack_map;
3642 rxbuf->pack_map = rxr->rx_pspare_map;
3643 rxr->rx_pspare_map = map;
3645 bus_dmamap_sync(rxr->rx_ptag, rxbuf->pack_map,
3646 BUS_DMASYNC_PREREAD);
3647 rxr->rx_base[i].read.pkt_addr = htole64(pseg[0].ds_addr);
3653 /*********************************************************************
3655 * Allocate memory for rx_buffer structures. Since we use one
3656 * rx_buffer per received packet, the maximum number of rx_buffer's
3657 * that we'll need is equal to the number of receive descriptors
3658 * that we've allocated.
3660 **********************************************************************/
3662 igb_allocate_receive_buffers(struct rx_ring *rxr)
3664 struct adapter *adapter = rxr->adapter;
3665 device_t dev = adapter->dev;
3666 struct igb_rx_buf *rxbuf;
3667 int i, bsize, error;
3669 bsize = sizeof(struct igb_rx_buf) * adapter->num_rx_desc;
3670 if (!(rxr->rx_buffers =
3671 (struct igb_rx_buf *) kmalloc(bsize,
3672 M_DEVBUF, M_INTWAIT | M_ZERO))) {
3673 device_printf(dev, "Unable to allocate rx_buffer memory\n");
3678 if ((error = bus_dma_tag_create(NULL,
3679 1, 0, /* alignment, bounds */
3680 BUS_SPACE_MAXADDR, /* lowaddr */
3681 BUS_SPACE_MAXADDR, /* highaddr */
3682 NULL, NULL, /* filter, filterarg */
3683 MSIZE, /* maxsize */
3685 MSIZE, /* maxsegsize */
3688 device_printf(dev, "Unable to create RX DMA tag\n");
3692 if ((error = bus_dma_tag_create(NULL,
3693 1, 0, /* alignment, bounds */
3694 BUS_SPACE_MAXADDR, /* lowaddr */
3695 BUS_SPACE_MAXADDR, /* highaddr */
3696 NULL, NULL, /* filter, filterarg */
3697 MJUMPAGESIZE, /* maxsize */
3699 MJUMPAGESIZE, /* maxsegsize */
3702 device_printf(dev, "Unable to create RX payload DMA tag\n");
3706 /* Create the spare maps (used by getbuf) */
3707 error = bus_dmamap_create(rxr->rx_htag, BUS_DMA_NOWAIT,
3708 &rxr->rx_hspare_map);
3711 "%s: bus_dmamap_create header spare failed: %d\n",
3715 error = bus_dmamap_create(rxr->rx_ptag, BUS_DMA_NOWAIT,
3716 &rxr->rx_pspare_map);
3719 "%s: bus_dmamap_create packet spare failed: %d\n",
3724 for (i = 0; i < adapter->num_rx_desc; i++) {
3725 rxbuf = &rxr->rx_buffers[i];
3726 error = bus_dmamap_create(rxr->rx_htag,
3727 BUS_DMA_NOWAIT, &rxbuf->head_map);
3730 "Unable to create RX head DMA maps\n");
3733 error = bus_dmamap_create(rxr->rx_ptag,
3734 BUS_DMA_NOWAIT, &rxbuf->pack_map);
3737 "Unable to create RX packet DMA maps\n");
3745 /* Frees all, but can handle partial completion */
3746 igb_free_receive_structures(adapter);
3752 igb_free_receive_ring(struct rx_ring *rxr)
3754 struct adapter *adapter;
3755 struct igb_rx_buf *rxbuf;
3758 adapter = rxr->adapter;
3759 for (i = 0; i < adapter->num_rx_desc; i++) {
3760 rxbuf = &rxr->rx_buffers[i];
3761 if (rxbuf->m_head != NULL) {
3762 bus_dmamap_sync(rxr->rx_htag, rxbuf->head_map,
3763 BUS_DMASYNC_POSTREAD);
3764 bus_dmamap_unload(rxr->rx_htag, rxbuf->head_map);
3765 rxbuf->m_head->m_flags |= M_PKTHDR;
3766 m_freem(rxbuf->m_head);
3768 if (rxbuf->m_pack != NULL) {
3769 bus_dmamap_sync(rxr->rx_ptag, rxbuf->pack_map,
3770 BUS_DMASYNC_POSTREAD);
3771 bus_dmamap_unload(rxr->rx_ptag, rxbuf->pack_map);
3772 rxbuf->m_pack->m_flags |= M_PKTHDR;
3773 m_freem(rxbuf->m_pack);
3775 rxbuf->m_head = NULL;
3776 rxbuf->m_pack = NULL;
3781 /*********************************************************************
3783 * Initialize a receive ring and its buffers.
3785 **********************************************************************/
3787 igb_setup_receive_ring(struct rx_ring *rxr)
3789 struct adapter *adapter;
3793 struct lro_ctrl *lro = &rxr->lro;
3795 int j, rsize, error = 0;
3797 adapter = rxr->adapter;
3801 /* Clear the ring contents */
3803 rsize = roundup2(adapter->num_rx_desc *
3804 sizeof(union e1000_adv_rx_desc), IGB_DBA_ALIGN);
3805 bzero((void *)rxr->rx_base, rsize);
3808 ** Free current RX buffer structures and their mbufs
3810 igb_free_receive_ring(rxr);
3812 /* Now replenish the ring mbufs */
3813 for (j = 0; j < adapter->num_rx_desc; j++) {
3814 error = igb_get_buf(rxr, j, IGB_CLEAN_BOTH | IGB_CLEAN_INITIAL);
3819 /* Setup our descriptor indices */
3820 rxr->next_to_check = 0;
3821 rxr->last_cleaned = 0;
3822 rxr->lro_enabled = FALSE;
3824 if (igb_header_split)
3825 rxr->hdr_split = TRUE;
3828 ifp->if_capabilities &= ~IFCAP_LRO;
3833 rxr->discard = FALSE;
3835 bus_dmamap_sync(rxr->rxdma.dma_tag, rxr->rxdma.dma_map,
3836 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
3839 ** Now set up the LRO interface, we
3840 ** also only do head split when LRO
3841 ** is enabled, since so often they
3842 ** are undesireable in similar setups.
3845 if (ifp->if_capenable & IFCAP_LRO) {
3846 int err = tcp_lro_init(lro);
3848 device_printf(dev, "LRO Initialization failed!\n");
3851 INIT_DEBUGOUT("RX LRO Initialized\n");
3852 rxr->lro_enabled = TRUE;
3853 lro->ifp = adapter->ifp;
3861 igb_free_receive_ring(rxr);
3866 /*********************************************************************
3868 * Initialize all receive rings.
3870 **********************************************************************/
3872 igb_setup_receive_structures(struct adapter *adapter)
3874 struct rx_ring *rxr = adapter->rx_rings;
3877 for (i = 0; i < adapter->num_queues; i++, rxr++)
3878 if (igb_setup_receive_ring(rxr))
3884 * Free RX buffers allocated so far, we will only handle
3885 * the rings that completed, the failing case will have
3886 * cleaned up for itself. The value of 'i' will be the
3887 * failed ring so we must pre-decrement it.
3889 rxr = adapter->rx_rings;
3890 for (--i; i > 0; i--, rxr++) {
3891 for (j = 0; j < adapter->num_rx_desc; j++)
3892 igb_free_receive_ring(rxr);
3898 /*********************************************************************
3900 * Enable receive unit.
3902 **********************************************************************/
3904 igb_initialize_receive_units(struct adapter *adapter)
3906 struct rx_ring *rxr = adapter->rx_rings;
3907 struct ifnet *ifp = adapter->ifp;
3908 struct e1000_hw *hw = &adapter->hw;
3909 u32 rctl, rxcsum, psize, srrctl = 0;
3911 INIT_DEBUGOUT("igb_initialize_receive_unit: begin");
3914 * Make sure receives are disabled while setting
3915 * up the descriptor ring
3917 rctl = E1000_READ_REG(hw, E1000_RCTL);
3918 E1000_WRITE_REG(hw, E1000_RCTL, rctl & ~E1000_RCTL_EN);
3921 ** Set up for header split
3923 if (rxr->hdr_split) {
3924 /* Use a standard mbuf for the header */
3925 srrctl |= IGB_HDR_BUF << E1000_SRRCTL_BSIZEHDRSIZE_SHIFT;
3926 srrctl |= E1000_SRRCTL_DESCTYPE_HDR_SPLIT_ALWAYS;
3928 srrctl |= E1000_SRRCTL_DESCTYPE_ADV_ONEBUF;
3931 ** Set up for jumbo frames
3933 if (ifp->if_mtu > ETHERMTU) {
3934 rctl |= E1000_RCTL_LPE;
3935 srrctl |= 4096 >> E1000_SRRCTL_BSIZEPKT_SHIFT;
3936 rctl |= E1000_RCTL_SZ_4096 | E1000_RCTL_BSEX;
3938 /* Set maximum packet len */
3939 psize = adapter->max_frame_size;
3940 /* are we on a vlan? */
3941 if (adapter->ifp->if_vlantrunks != NULL)
3942 psize += VLAN_TAG_SIZE;
3943 E1000_WRITE_REG(&adapter->hw, E1000_RLPML, psize);
3945 rctl &= ~E1000_RCTL_LPE;
3946 srrctl |= 2048 >> E1000_SRRCTL_BSIZEPKT_SHIFT;
3947 rctl |= E1000_RCTL_SZ_2048;
3950 /* Setup the Base and Length of the Rx Descriptor Rings */
3951 for (int i = 0; i < adapter->num_queues; i++, rxr++) {
3952 u64 bus_addr = rxr->rxdma.dma_paddr;
3955 E1000_WRITE_REG(hw, E1000_RDLEN(i),
3956 adapter->num_rx_desc * sizeof(struct e1000_rx_desc));
3957 E1000_WRITE_REG(hw, E1000_RDBAH(i),
3958 (uint32_t)(bus_addr >> 32));
3959 E1000_WRITE_REG(hw, E1000_RDBAL(i),
3960 (uint32_t)bus_addr);
3961 E1000_WRITE_REG(hw, E1000_SRRCTL(i), srrctl);
3962 /* Enable this Queue */
3963 rxdctl = E1000_READ_REG(hw, E1000_RXDCTL(i));
3964 rxdctl |= E1000_RXDCTL_QUEUE_ENABLE;
3965 rxdctl &= 0xFFF00000;
3966 rxdctl |= IGB_RX_PTHRESH;
3967 rxdctl |= IGB_RX_HTHRESH << 8;
3968 rxdctl |= IGB_RX_WTHRESH << 16;
3969 E1000_WRITE_REG(hw, E1000_RXDCTL(i), rxdctl);
3973 ** Setup for RX MultiQueue
3975 rxcsum = E1000_READ_REG(hw, E1000_RXCSUM);
3976 if (adapter->num_queues >1) {
3977 u32 random[10], mrqc, shift = 0;
3983 karc4rand(&random, sizeof(random));
3984 if (adapter->hw.mac.type == e1000_82575)
3986 /* Warning FM follows */
3987 for (int i = 0; i < 128; i++) {
3989 (i % adapter->num_queues) << shift;
3992 E1000_RETA(i >> 2), reta.dword);
3994 /* Now fill in hash table */
3995 mrqc = E1000_MRQC_ENABLE_RSS_4Q;
3996 for (int i = 0; i < 10; i++)
3997 E1000_WRITE_REG_ARRAY(hw,
3998 E1000_RSSRK(0), i, random[i]);
4000 mrqc |= (E1000_MRQC_RSS_FIELD_IPV4 |
4001 E1000_MRQC_RSS_FIELD_IPV4_TCP);
4002 mrqc |= (E1000_MRQC_RSS_FIELD_IPV6 |
4003 E1000_MRQC_RSS_FIELD_IPV6_TCP);
4004 mrqc |=( E1000_MRQC_RSS_FIELD_IPV4_UDP |
4005 E1000_MRQC_RSS_FIELD_IPV6_UDP);
4006 mrqc |=( E1000_MRQC_RSS_FIELD_IPV6_UDP_EX |
4007 E1000_MRQC_RSS_FIELD_IPV6_TCP_EX);
4009 E1000_WRITE_REG(hw, E1000_MRQC, mrqc);
4012 ** NOTE: Receive Full-Packet Checksum Offload
4013 ** is mutually exclusive with Multiqueue. However
4014 ** this is not the same as TCP/IP checksums which
4017 rxcsum |= E1000_RXCSUM_PCSD;
4018 #if __FreeBSD_version >= 800000
4019 /* For SCTP Offload */
4020 if ((hw->mac.type == e1000_82576)
4021 && (ifp->if_capenable & IFCAP_RXCSUM))
4022 rxcsum |= E1000_RXCSUM_CRCOFL;
4026 if (ifp->if_capenable & IFCAP_RXCSUM) {
4027 rxcsum |= E1000_RXCSUM_IPPCSE;
4028 #if __FreeBSD_version >= 800000
4029 if (adapter->hw.mac.type == e1000_82576)
4030 rxcsum |= E1000_RXCSUM_CRCOFL;
4033 rxcsum &= ~E1000_RXCSUM_TUOFL;
4035 E1000_WRITE_REG(hw, E1000_RXCSUM, rxcsum);
4037 /* Setup the Receive Control Register */
4038 rctl &= ~(3 << E1000_RCTL_MO_SHIFT);
4039 rctl |= E1000_RCTL_EN | E1000_RCTL_BAM | E1000_RCTL_LBM_NO |
4040 E1000_RCTL_RDMTS_HALF |
4041 (hw->mac.mc_filter_type << E1000_RCTL_MO_SHIFT);
4042 /* Strip CRC bytes. */
4043 rctl |= E1000_RCTL_SECRC;
4044 /* Make sure VLAN Filters are off */
4045 rctl &= ~E1000_RCTL_VFE;
4046 /* Don't store bad packets */
4047 rctl &= ~E1000_RCTL_SBP;
4049 /* Enable Receives */
4050 E1000_WRITE_REG(hw, E1000_RCTL, rctl);
4053 * Setup the HW Rx Head and Tail Descriptor Pointers
4054 * - needs to be after enable
4056 for (int i = 0; i < adapter->num_queues; i++) {
4057 E1000_WRITE_REG(hw, E1000_RDH(i), 0);
4058 E1000_WRITE_REG(hw, E1000_RDT(i),
4059 adapter->num_rx_desc - 1);
4064 /*********************************************************************
4066 * Free receive rings.
4068 **********************************************************************/
4070 igb_free_receive_structures(struct adapter *adapter)
4072 struct rx_ring *rxr = adapter->rx_rings;
4074 for (int i = 0; i < adapter->num_queues; i++, rxr++) {
4076 struct lro_ctrl *lro = &rxr->lro;
4079 igb_free_receive_buffers(rxr);
4083 igb_dma_free(adapter, &rxr->rxdma);
4085 IGB_RX_LOCK_DESTROY(rxr);
4088 kfree(adapter->rx_rings, M_DEVBUF);
4091 /*********************************************************************
4093 * Free receive ring data structures.
4095 **********************************************************************/
4097 igb_free_receive_buffers(struct rx_ring *rxr)
4099 struct adapter *adapter = rxr->adapter;
4100 struct igb_rx_buf *rxbuf;
4103 INIT_DEBUGOUT("free_receive_structures: begin");
4105 if (rxr->rx_hspare_map != NULL) {
4106 bus_dmamap_destroy(rxr->rx_htag, rxr->rx_hspare_map);
4107 rxr->rx_hspare_map = NULL;
4110 if (rxr->rx_hspare_map != NULL) {
4111 bus_dmamap_destroy(rxr->rx_ptag, rxr->rx_pspare_map);
4112 rxr->rx_pspare_map = NULL;
4115 /* Cleanup any existing buffers */
4116 if (rxr->rx_buffers != NULL) {
4117 for (i = 0; i < adapter->num_rx_desc; i++) {
4118 rxbuf = &rxr->rx_buffers[i];
4119 if (rxbuf->m_head != NULL) {
4120 bus_dmamap_sync(rxr->rx_htag, rxbuf->head_map,
4121 BUS_DMASYNC_POSTREAD);
4122 bus_dmamap_unload(rxr->rx_htag,
4124 rxbuf->m_head->m_flags |= M_PKTHDR;
4125 m_freem(rxbuf->m_head);
4127 if (rxbuf->m_pack != NULL) {
4128 bus_dmamap_sync(rxr->rx_ptag, rxbuf->pack_map,
4129 BUS_DMASYNC_POSTREAD);
4130 bus_dmamap_unload(rxr->rx_ptag,
4132 rxbuf->m_pack->m_flags |= M_PKTHDR;
4133 m_freem(rxbuf->m_pack);
4135 rxbuf->m_head = NULL;
4136 rxbuf->m_pack = NULL;
4137 if (rxbuf->head_map != NULL) {
4138 bus_dmamap_destroy(rxr->rx_htag,
4140 rxbuf->head_map = NULL;
4142 if (rxbuf->pack_map != NULL) {
4143 bus_dmamap_destroy(rxr->rx_ptag,
4145 rxbuf->pack_map = NULL;
4148 if (rxr->rx_buffers != NULL) {
4149 kfree(rxr->rx_buffers, M_DEVBUF);
4150 rxr->rx_buffers = NULL;
4154 if (rxr->rx_htag != NULL) {
4155 bus_dma_tag_destroy(rxr->rx_htag);
4156 rxr->rx_htag = NULL;
4158 if (rxr->rx_ptag != NULL) {
4159 bus_dma_tag_destroy(rxr->rx_ptag);
4160 rxr->rx_ptag = NULL;
4164 static __inline void
4165 igb_rx_discard(struct rx_ring *rxr, union e1000_adv_rx_desc *cur, int i)
4168 if (rxr->fmp != NULL) {
4169 rxr->fmp->m_flags |= M_PKTHDR;
4176 static __inline void
4177 igb_rx_input(struct rx_ring *rxr, struct ifnet *ifp, struct mbuf *m, u32 ptype)
4181 * ATM LRO is only for IPv4/TCP packets and TCP checksum of the packet
4182 * should be computed by hardware. Also it should not have VLAN tag in
4186 if (rxr->lro_enabled &&
4187 (ifp->if_capenable & IFCAP_VLAN_HWTAGGING) != 0 &&
4188 (ptype & E1000_RXDADV_PKTTYPE_ETQF) == 0 &&
4189 (ptype & (E1000_RXDADV_PKTTYPE_IPV4 | E1000_RXDADV_PKTTYPE_TCP)) ==
4190 (E1000_RXDADV_PKTTYPE_IPV4 | E1000_RXDADV_PKTTYPE_TCP) &&
4191 (m->m_pkthdr.csum_flags & (CSUM_DATA_VALID | CSUM_PSEUDO_HDR)) ==
4192 (CSUM_DATA_VALID | CSUM_PSEUDO_HDR)) {
4194 * Send to the stack if:
4195 ** - LRO not enabled, or
4196 ** - no LRO resources, or
4197 ** - lro enqueue fails
4199 if (rxr->lro.lro_cnt != 0)
4200 if (tcp_lro_rx(&rxr->lro, m, 0) == 0)
4204 (*ifp->if_input)(ifp, m);
4207 /*********************************************************************
4209 * This routine executes in interrupt context. It replenishes
4210 * the mbufs in the descriptor and sends data which has been
4211 * dma'ed into host memory to upper layer.
4213 * We loop at most count times if count is > 0, or until done if
4216 * Return TRUE if more to clean, FALSE otherwise
4217 *********************************************************************/
4219 igb_rxeof(struct rx_ring *rxr, int count)
4221 struct adapter *adapter = rxr->adapter;
4222 struct ifnet *ifp = adapter->ifp;
4224 struct lro_ctrl *lro = &rxr->lro;
4225 struct lro_entry *queued;
4228 u32 ptype, staterr = 0;
4229 union e1000_adv_rx_desc *cur;
4233 /* Main clean loop */
4234 for (i = rxr->next_to_check; count > 0; prog++) {
4235 struct mbuf *sendmp, *mh, *mp;
4236 u16 hlen, plen, hdr, vtag;
4240 /* Sync the ring. */
4241 bus_dmamap_sync(rxr->rxdma.dma_tag, rxr->rxdma.dma_map,
4242 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
4243 cur = &rxr->rx_base[i];
4244 staterr = le32toh(cur->wb.upper.status_error);
4245 if ((staterr & E1000_RXD_STAT_DD) == 0)
4247 if ((ifp->if_flags & IFF_RUNNING) == 0)
4250 sendmp = mh = mp = NULL;
4251 cur->wb.upper.status_error = 0;
4252 plen = le16toh(cur->wb.upper.length);
4253 ptype = le32toh(cur->wb.lower.lo_dword.data) & IGB_PKTTYPE_MASK;
4254 hdr = le16toh(cur->wb.lower.lo_dword.hs_rss.hdr_info);
4255 eop = ((staterr & E1000_RXD_STAT_EOP) == E1000_RXD_STAT_EOP);
4257 /* Make sure all segments of a bad packet are discarded */
4258 if (((staterr & E1000_RXDEXT_ERR_FRAME_ERR_MASK) != 0) ||
4261 ++rxr->rx_discarded;
4262 if (!eop) /* Catch subsequent segs */
4263 rxr->discard = TRUE;
4265 rxr->discard = FALSE;
4266 igb_rx_discard(rxr, cur, i);
4271 ** The way the hardware is configured to
4272 ** split, it will ONLY use the header buffer
4273 ** when header split is enabled, otherwise we
4274 ** get normal behavior, ie, both header and
4275 ** payload are DMA'd into the payload buffer.
4277 ** The fmp test is to catch the case where a
4278 ** packet spans multiple descriptors, in that
4279 ** case only the first header is valid.
4281 if (rxr->hdr_split && rxr->fmp == NULL) {
4282 hlen = (hdr & E1000_RXDADV_HDRBUFLEN_MASK) >>
4283 E1000_RXDADV_HDRBUFLEN_SHIFT;
4284 if (hlen > IGB_HDR_BUF)
4286 /* Handle the header mbuf */
4287 mh = rxr->rx_buffers[i].m_head;
4289 dopayload = IGB_CLEAN_HEADER;
4291 ** Get the payload length, this
4292 ** could be zero if its a small
4296 mp = rxr->rx_buffers[i].m_pack;
4299 dopayload = IGB_CLEAN_BOTH;
4300 rxr->rx_split_packets++;
4304 ** Either no header split, or a
4305 ** secondary piece of a fragmented
4308 mh = rxr->rx_buffers[i].m_pack;
4310 dopayload = IGB_CLEAN_PAYLOAD;
4314 ** get_buf will overwrite the writeback
4315 ** descriptor so save the VLAN tag now.
4317 vtag = le16toh(cur->wb.upper.vlan);
4318 if (igb_get_buf(rxr, i, dopayload) != 0) {
4321 * We've dropped a frame due to lack of resources
4322 * so we should drop entire multi-segmented
4323 * frames until we encounter EOP.
4325 if ((staterr & E1000_RXD_STAT_EOP) != 0)
4326 rxr->discard = TRUE;
4327 igb_rx_discard(rxr, cur, i);
4331 /* Initial frame - setup */
4332 if (rxr->fmp == NULL) {
4333 mh->m_pkthdr.len = mh->m_len;
4334 /* Store the first mbuf */
4338 /* Add payload if split */
4339 mh->m_pkthdr.len += mp->m_len;
4340 rxr->lmp = mh->m_next;
4343 /* Chain mbuf's together */
4344 rxr->lmp->m_next = mh;
4345 rxr->lmp = rxr->lmp->m_next;
4346 rxr->fmp->m_pkthdr.len += mh->m_len;
4350 rxr->fmp->m_pkthdr.rcvif = ifp;
4353 /* capture data for AIM */
4355 rxr->bytes += rxr->fmp->m_pkthdr.len;
4356 rxr->rx_bytes += rxr->fmp->m_pkthdr.len;
4358 if ((ifp->if_capenable & IFCAP_RXCSUM) != 0)
4359 igb_rx_checksum(staterr, rxr->fmp, ptype);
4360 /* XXX igb(4) always strips VLAN. */
4361 if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) != 0 &&
4362 (staterr & E1000_RXD_STAT_VP) != 0) {
4363 rxr->fmp->m_pkthdr.ether_vlantag = vtag;
4364 rxr->fmp->m_flags |= M_VLANTAG;
4366 #if __FreeBSD_version >= 800000
4367 rxr->fmp->m_pkthdr.flowid = curcpu;
4368 rxr->fmp->m_flags |= M_FLOWID;
4371 /* Make sure to set M_PKTHDR. */
4372 sendmp->m_flags |= M_PKTHDR;
4378 bus_dmamap_sync(rxr->rxdma.dma_tag, rxr->rxdma.dma_map,
4379 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
4381 rxr->last_cleaned = i; /* For updating tail */
4383 /* Advance our pointers to the next descriptor. */
4384 if (++i == adapter->num_rx_desc)
4388 ** Note that we hold the RX lock thru
4389 ** the following call so this ring's
4390 ** next_to_check is not gonna change.
4393 igb_rx_input(rxr, ifp, sendmp, ptype);
4401 rxr->next_to_check = i;
4403 /* Advance the E1000's Receive Queue "Tail Pointer". */
4404 E1000_WRITE_REG(&adapter->hw, E1000_RDT(rxr->me), rxr->last_cleaned);
4407 * Flush any outstanding LRO work
4410 while ((queued = SLIST_FIRST(&lro->lro_active)) != NULL) {
4411 SLIST_REMOVE_HEAD(&lro->lro_active, next);
4412 tcp_lro_flush(lro, queued);
4419 ** We still have cleaning to do?
4420 ** Schedule another interrupt if so.
4422 if ((staterr & E1000_RXD_STAT_DD) != 0)
4428 /*********************************************************************
4430 * Verify that the hardware indicated that the checksum is valid.
4431 * Inform the stack about the status of checksum so that stack
4432 * doesn't spend time verifying the checksum.
4434 *********************************************************************/
4436 igb_rx_checksum(u32 staterr, struct mbuf *mp, u32 ptype)
4438 u16 status = (u16)staterr;
4439 u8 errors = (u8) (staterr >> 24);
4442 /* Ignore Checksum bit is set */
4443 if (status & E1000_RXD_STAT_IXSM) {
4444 mp->m_pkthdr.csum_flags = 0;
4448 if ((ptype & E1000_RXDADV_PKTTYPE_ETQF) == 0 &&
4449 (ptype & E1000_RXDADV_PKTTYPE_SCTP) != 0)
4453 if (status & E1000_RXD_STAT_IPCS) {
4455 if (!(errors & E1000_RXD_ERR_IPE)) {
4456 /* IP Checksum Good */
4457 mp->m_pkthdr.csum_flags = CSUM_IP_CHECKED;
4458 mp->m_pkthdr.csum_flags |= CSUM_IP_VALID;
4460 mp->m_pkthdr.csum_flags = 0;
4463 if (status & (E1000_RXD_STAT_TCPCS | E1000_RXD_STAT_UDPCS)) {
4464 u16 type = (CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
4465 #if __FreeBSD_version >= 800000
4466 if (sctp) /* reassign */
4467 type = CSUM_SCTP_VALID;
4470 if (!(errors & E1000_RXD_ERR_TCPE)) {
4471 mp->m_pkthdr.csum_flags |= type;
4473 mp->m_pkthdr.csum_data = htons(0xffff);
4480 * This routine is run via an vlan
4484 igb_register_vlan(void *arg, struct ifnet *ifp, u16 vtag)
4486 struct adapter *adapter = ifp->if_softc;
4489 if (ifp->if_softc != arg) /* Not our event */
4492 if ((vtag == 0) || (vtag > 4095)) /* Invalid */
4495 index = (vtag >> 5) & 0x7F;
4497 igb_shadow_vfta[index] |= (1 << bit);
4498 ++adapter->num_vlans;
4499 /* Re-init to load the changes */
4504 * This routine is run via an vlan
4508 igb_unregister_vlan(void *arg, struct ifnet *ifp, u16 vtag)
4510 struct adapter *adapter = ifp->if_softc;
4513 if (ifp->if_softc != arg)
4516 if ((vtag == 0) || (vtag > 4095)) /* Invalid */
4519 index = (vtag >> 5) & 0x7F;
4521 igb_shadow_vfta[index] &= ~(1 << bit);
4522 --adapter->num_vlans;
4523 /* Re-init to load the changes */
4528 igb_setup_vlan_hw_support(struct adapter *adapter)
4530 struct e1000_hw *hw = &adapter->hw;
4534 ** We get here thru init_locked, meaning
4535 ** a soft reset, this has already cleared
4536 ** the VFTA and other state, so if there
4537 ** have been no vlan's registered do nothing.
4539 if (adapter->num_vlans == 0)
4543 ** A soft reset zero's out the VFTA, so
4544 ** we need to repopulate it now.
4546 for (int i = 0; i < IGB_VFTA_SIZE; i++)
4547 if (igb_shadow_vfta[i] != 0)
4548 E1000_WRITE_REG_ARRAY(hw, E1000_VFTA,
4549 i, igb_shadow_vfta[i]);
4551 reg = E1000_READ_REG(hw, E1000_CTRL);
4552 reg |= E1000_CTRL_VME;
4553 E1000_WRITE_REG(hw, E1000_CTRL, reg);
4555 /* Enable the Filter Table */
4556 reg = E1000_READ_REG(hw, E1000_RCTL);
4557 reg &= ~E1000_RCTL_CFIEN;
4558 reg |= E1000_RCTL_VFE;
4559 E1000_WRITE_REG(hw, E1000_RCTL, reg);
4561 /* Update the frame size */
4562 E1000_WRITE_REG(&adapter->hw, E1000_RLPML,
4563 adapter->max_frame_size + VLAN_TAG_SIZE);
4567 igb_enable_intr(struct adapter *adapter)
4569 /* With RSS set up what to auto clear */
4570 if (adapter->msix_mem) {
4571 E1000_WRITE_REG(&adapter->hw, E1000_EIAC,
4572 adapter->eims_mask);
4573 E1000_WRITE_REG(&adapter->hw, E1000_EIAM,
4574 adapter->eims_mask);
4575 E1000_WRITE_REG(&adapter->hw, E1000_EIMS,
4576 adapter->eims_mask);
4577 E1000_WRITE_REG(&adapter->hw, E1000_IMS,
4580 E1000_WRITE_REG(&adapter->hw, E1000_IMS,
4583 E1000_WRITE_FLUSH(&adapter->hw);
4589 igb_disable_intr(struct adapter *adapter)
4591 if (adapter->msix_mem) {
4592 E1000_WRITE_REG(&adapter->hw, E1000_EIMC, ~0);
4593 E1000_WRITE_REG(&adapter->hw, E1000_EIAC, 0);
4595 E1000_WRITE_REG(&adapter->hw, E1000_IMC, ~0);
4596 E1000_WRITE_FLUSH(&adapter->hw);
4601 * Bit of a misnomer, what this really means is
4602 * to enable OS management of the system... aka
4603 * to disable special hardware management features
4606 igb_init_manageability(struct adapter *adapter)
4608 if (adapter->has_manage) {
4609 int manc2h = E1000_READ_REG(&adapter->hw, E1000_MANC2H);
4610 int manc = E1000_READ_REG(&adapter->hw, E1000_MANC);
4612 /* disable hardware interception of ARP */
4613 manc &= ~(E1000_MANC_ARP_EN);
4615 /* enable receiving management packets to the host */
4616 manc |= E1000_MANC_EN_MNG2HOST;
4617 manc2h |= 1 << 5; /* Mng Port 623 */
4618 manc2h |= 1 << 6; /* Mng Port 664 */
4619 E1000_WRITE_REG(&adapter->hw, E1000_MANC2H, manc2h);
4620 E1000_WRITE_REG(&adapter->hw, E1000_MANC, manc);
4625 * Give control back to hardware management
4626 * controller if there is one.
4629 igb_release_manageability(struct adapter *adapter)
4631 if (adapter->has_manage) {
4632 int manc = E1000_READ_REG(&adapter->hw, E1000_MANC);
4634 /* re-enable hardware interception of ARP */
4635 manc |= E1000_MANC_ARP_EN;
4636 manc &= ~E1000_MANC_EN_MNG2HOST;
4638 E1000_WRITE_REG(&adapter->hw, E1000_MANC, manc);
4643 * igb_get_hw_control sets CTRL_EXT:DRV_LOAD bit.
4644 * For ASF and Pass Through versions of f/w this means that
4645 * the driver is loaded.
4649 igb_get_hw_control(struct adapter *adapter)
4653 /* Let firmware know the driver has taken over */
4654 ctrl_ext = E1000_READ_REG(&adapter->hw, E1000_CTRL_EXT);
4655 E1000_WRITE_REG(&adapter->hw, E1000_CTRL_EXT,
4656 ctrl_ext | E1000_CTRL_EXT_DRV_LOAD);
4660 * igb_release_hw_control resets CTRL_EXT:DRV_LOAD bit.
4661 * For ASF and Pass Through versions of f/w this means that the
4662 * driver is no longer loaded.
4666 igb_release_hw_control(struct adapter *adapter)
4670 /* Let firmware taken over control of h/w */
4671 ctrl_ext = E1000_READ_REG(&adapter->hw, E1000_CTRL_EXT);
4672 E1000_WRITE_REG(&adapter->hw, E1000_CTRL_EXT,
4673 ctrl_ext & ~E1000_CTRL_EXT_DRV_LOAD);
4677 igb_is_valid_ether_addr(uint8_t *addr)
4679 char zero_addr[6] = { 0, 0, 0, 0, 0, 0 };
4681 if ((addr[0] & 1) || (!bcmp(addr, zero_addr, ETHER_ADDR_LEN))) {
4690 * Enable PCI Wake On Lan capability
4693 igb_enable_wakeup(device_t dev)
4698 /* First find the capabilities pointer*/
4699 cap = pci_read_config(dev, PCIR_CAP_PTR, 2);
4700 /* Read the PM Capabilities */
4701 id = pci_read_config(dev, cap, 1);
4702 if (id != PCIY_PMG) /* Something wrong */
4704 /* OK, we have the power capabilities, so
4705 now get the status register */
4706 cap += PCIR_POWER_STATUS;
4707 status = pci_read_config(dev, cap, 2);
4708 status |= PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE;
4709 pci_write_config(dev, cap, status, 2);
4714 /**********************************************************************
4716 * Update the board statistics counters.
4718 **********************************************************************/
4720 igb_update_stats_counters(struct adapter *adapter)
4724 if(adapter->hw.phy.media_type == e1000_media_type_copper ||
4725 (E1000_READ_REG(&adapter->hw, E1000_STATUS) & E1000_STATUS_LU)) {
4726 adapter->stats.symerrs += E1000_READ_REG(&adapter->hw, E1000_SYMERRS);
4727 adapter->stats.sec += E1000_READ_REG(&adapter->hw, E1000_SEC);
4729 adapter->stats.crcerrs += E1000_READ_REG(&adapter->hw, E1000_CRCERRS);
4730 adapter->stats.mpc += E1000_READ_REG(&adapter->hw, E1000_MPC);
4731 adapter->stats.scc += E1000_READ_REG(&adapter->hw, E1000_SCC);
4732 adapter->stats.ecol += E1000_READ_REG(&adapter->hw, E1000_ECOL);
4734 adapter->stats.mcc += E1000_READ_REG(&adapter->hw, E1000_MCC);
4735 adapter->stats.latecol += E1000_READ_REG(&adapter->hw, E1000_LATECOL);
4736 adapter->stats.colc += E1000_READ_REG(&adapter->hw, E1000_COLC);
4737 adapter->stats.dc += E1000_READ_REG(&adapter->hw, E1000_DC);
4738 adapter->stats.rlec += E1000_READ_REG(&adapter->hw, E1000_RLEC);
4739 adapter->stats.xonrxc += E1000_READ_REG(&adapter->hw, E1000_XONRXC);
4740 adapter->stats.xontxc += E1000_READ_REG(&adapter->hw, E1000_XONTXC);
4741 adapter->stats.xoffrxc += E1000_READ_REG(&adapter->hw, E1000_XOFFRXC);
4742 adapter->stats.xofftxc += E1000_READ_REG(&adapter->hw, E1000_XOFFTXC);
4743 adapter->stats.fcruc += E1000_READ_REG(&adapter->hw, E1000_FCRUC);
4744 adapter->stats.prc64 += E1000_READ_REG(&adapter->hw, E1000_PRC64);
4745 adapter->stats.prc127 += E1000_READ_REG(&adapter->hw, E1000_PRC127);
4746 adapter->stats.prc255 += E1000_READ_REG(&adapter->hw, E1000_PRC255);
4747 adapter->stats.prc511 += E1000_READ_REG(&adapter->hw, E1000_PRC511);
4748 adapter->stats.prc1023 += E1000_READ_REG(&adapter->hw, E1000_PRC1023);
4749 adapter->stats.prc1522 += E1000_READ_REG(&adapter->hw, E1000_PRC1522);
4750 adapter->stats.gprc += E1000_READ_REG(&adapter->hw, E1000_GPRC);
4751 adapter->stats.bprc += E1000_READ_REG(&adapter->hw, E1000_BPRC);
4752 adapter->stats.mprc += E1000_READ_REG(&adapter->hw, E1000_MPRC);
4753 adapter->stats.gptc += E1000_READ_REG(&adapter->hw, E1000_GPTC);
4755 /* For the 64-bit byte counters the low dword must be read first. */
4756 /* Both registers clear on the read of the high dword */
4758 adapter->stats.gorc += E1000_READ_REG(&adapter->hw, E1000_GORCH);
4759 adapter->stats.gotc += E1000_READ_REG(&adapter->hw, E1000_GOTCH);
4761 adapter->stats.rnbc += E1000_READ_REG(&adapter->hw, E1000_RNBC);
4762 adapter->stats.ruc += E1000_READ_REG(&adapter->hw, E1000_RUC);
4763 adapter->stats.rfc += E1000_READ_REG(&adapter->hw, E1000_RFC);
4764 adapter->stats.roc += E1000_READ_REG(&adapter->hw, E1000_ROC);
4765 adapter->stats.rjc += E1000_READ_REG(&adapter->hw, E1000_RJC);
4767 adapter->stats.tor += E1000_READ_REG(&adapter->hw, E1000_TORH);
4768 adapter->stats.tot += E1000_READ_REG(&adapter->hw, E1000_TOTH);
4770 adapter->stats.tpr += E1000_READ_REG(&adapter->hw, E1000_TPR);
4771 adapter->stats.tpt += E1000_READ_REG(&adapter->hw, E1000_TPT);
4772 adapter->stats.ptc64 += E1000_READ_REG(&adapter->hw, E1000_PTC64);
4773 adapter->stats.ptc127 += E1000_READ_REG(&adapter->hw, E1000_PTC127);
4774 adapter->stats.ptc255 += E1000_READ_REG(&adapter->hw, E1000_PTC255);
4775 adapter->stats.ptc511 += E1000_READ_REG(&adapter->hw, E1000_PTC511);
4776 adapter->stats.ptc1023 += E1000_READ_REG(&adapter->hw, E1000_PTC1023);
4777 adapter->stats.ptc1522 += E1000_READ_REG(&adapter->hw, E1000_PTC1522);
4778 adapter->stats.mptc += E1000_READ_REG(&adapter->hw, E1000_MPTC);
4779 adapter->stats.bptc += E1000_READ_REG(&adapter->hw, E1000_BPTC);
4781 adapter->stats.algnerrc +=
4782 E1000_READ_REG(&adapter->hw, E1000_ALGNERRC);
4783 adapter->stats.rxerrc +=
4784 E1000_READ_REG(&adapter->hw, E1000_RXERRC);
4785 adapter->stats.tncrs +=
4786 E1000_READ_REG(&adapter->hw, E1000_TNCRS);
4787 adapter->stats.cexterr +=
4788 E1000_READ_REG(&adapter->hw, E1000_CEXTERR);
4789 adapter->stats.tsctc +=
4790 E1000_READ_REG(&adapter->hw, E1000_TSCTC);
4791 adapter->stats.tsctfc +=
4792 E1000_READ_REG(&adapter->hw, E1000_TSCTFC);
4795 ifp->if_collisions = adapter->stats.colc;
4798 ifp->if_ierrors = adapter->dropped_pkts + adapter->stats.rxerrc +
4799 adapter->stats.crcerrs + adapter->stats.algnerrc +
4800 adapter->stats.ruc + adapter->stats.roc +
4801 adapter->stats.mpc + adapter->stats.cexterr;
4804 ifp->if_oerrors = adapter->stats.ecol +
4805 adapter->stats.latecol + adapter->watchdog_events;
4809 /**********************************************************************
4811 * This routine is called only when igb_display_debug_stats is enabled.
4812 * This routine provides a way to take a look at important statistics
4813 * maintained by the driver and hardware.
4815 **********************************************************************/
4817 igb_print_debug_info(struct adapter *adapter)
4819 device_t dev = adapter->dev;
4820 struct igb_queue *que = adapter->queues;
4821 struct rx_ring *rxr = adapter->rx_rings;
4822 struct tx_ring *txr = adapter->tx_rings;
4823 uint8_t *hw_addr = adapter->hw.hw_addr;
4825 device_printf(dev, "Adapter hardware address = %p \n", hw_addr);
4826 device_printf(dev, "CTRL = 0x%x RCTL = 0x%x \n",
4827 E1000_READ_REG(&adapter->hw, E1000_CTRL),
4828 E1000_READ_REG(&adapter->hw, E1000_RCTL));
4830 #if (DEBUG_HW > 0) /* Dont output these errors normally */
4831 device_printf(dev, "IMS = 0x%x EIMS = 0x%x \n",
4832 E1000_READ_REG(&adapter->hw, E1000_IMS),
4833 E1000_READ_REG(&adapter->hw, E1000_EIMS));
4836 device_printf(dev, "Packet buffer = Tx=%dk Rx=%dk \n",
4837 ((E1000_READ_REG(&adapter->hw, E1000_PBA) & 0xffff0000) >> 16),\
4838 (E1000_READ_REG(&adapter->hw, E1000_PBA) & 0xffff) );
4839 device_printf(dev, "Flow control watermarks high = %d low = %d\n",
4840 adapter->hw.fc.high_water,
4841 adapter->hw.fc.low_water);
4843 for (int i = 0; i < adapter->num_queues; i++, rxr++, txr++) {
4844 device_printf(dev, "Queue(%d) tdh = %d, tdt = %d ", i,
4845 E1000_READ_REG(&adapter->hw, E1000_TDH(i)),
4846 E1000_READ_REG(&adapter->hw, E1000_TDT(i)));
4847 device_printf(dev, "rdh = %d, rdt = %d\n",
4848 E1000_READ_REG(&adapter->hw, E1000_RDH(i)),
4849 E1000_READ_REG(&adapter->hw, E1000_RDT(i)));
4850 device_printf(dev, "TX(%d) no descriptors avail event = %lld\n",
4851 txr->me, (long long)txr->no_desc_avail);
4852 device_printf(dev, "TX(%d) Packets sent = %lld\n",
4853 txr->me, (long long)txr->tx_packets);
4854 device_printf(dev, "RX(%d) Packets received = %lld ",
4855 rxr->me, (long long)rxr->rx_packets);
4858 for (int i = 0; i < adapter->num_queues; i++, rxr++) {
4860 struct lro_ctrl *lro = &rxr->lro;
4862 device_printf(dev, "Queue(%d) rdh = %d, rdt = %d\n", i,
4863 E1000_READ_REG(&adapter->hw, E1000_RDH(i)),
4864 E1000_READ_REG(&adapter->hw, E1000_RDT(i)));
4865 device_printf(dev, "RX(%d) Packets received = %lld\n", rxr->me,
4866 (long long)rxr->rx_packets);
4867 device_printf(dev, " Split Packets = %lld ",
4868 (long long)rxr->rx_split_packets);
4869 device_printf(dev, " Byte count = %lld\n",
4870 (long long)rxr->rx_bytes);
4872 device_printf(dev,"RX(%d) LRO Queued= %d ",
4873 i, lro->lro_queued);
4874 device_printf(dev,"LRO Flushed= %d\n",lro->lro_flushed);
4878 for (int i = 0; i < adapter->num_queues; i++, que++)
4879 device_printf(dev,"QUE(%d) IRQs = %llx\n",
4880 i, (long long)que->irqs);
4882 device_printf(dev, "LINK MSIX IRQ Handled = %u\n", adapter->link_irq);
4883 device_printf(dev, "Mbuf defrag failed = %ld\n",
4884 adapter->mbuf_defrag_failed);
4885 device_printf(dev, "Std mbuf header failed = %ld\n",
4886 adapter->mbuf_header_failed);
4887 device_printf(dev, "Std mbuf packet failed = %ld\n",
4888 adapter->mbuf_packet_failed);
4889 device_printf(dev, "Driver dropped packets = %ld\n",
4890 adapter->dropped_pkts);
4891 device_printf(dev, "Driver tx dma failure in xmit = %ld\n",
4892 adapter->no_tx_dma_setup);
4896 igb_print_hw_stats(struct adapter *adapter)
4898 device_t dev = adapter->dev;
4900 device_printf(dev, "Excessive collisions = %lld\n",
4901 (long long)adapter->stats.ecol);
4902 #if (DEBUG_HW > 0) /* Dont output these errors normally */
4903 device_printf(dev, "Symbol errors = %lld\n",
4904 (long long)adapter->stats.symerrs);
4906 device_printf(dev, "Sequence errors = %lld\n",
4907 (long long)adapter->stats.sec);
4908 device_printf(dev, "Defer count = %lld\n",
4909 (long long)adapter->stats.dc);
4910 device_printf(dev, "Missed Packets = %lld\n",
4911 (long long)adapter->stats.mpc);
4912 device_printf(dev, "Receive No Buffers = %lld\n",
4913 (long long)adapter->stats.rnbc);
4914 /* RLEC is inaccurate on some hardware, calculate our own. */
4915 device_printf(dev, "Receive Length Errors = %lld\n",
4916 ((long long)adapter->stats.roc + (long long)adapter->stats.ruc));
4917 device_printf(dev, "Receive errors = %lld\n",
4918 (long long)adapter->stats.rxerrc);
4919 device_printf(dev, "Crc errors = %lld\n",
4920 (long long)adapter->stats.crcerrs);
4921 device_printf(dev, "Alignment errors = %lld\n",
4922 (long long)adapter->stats.algnerrc);
4923 /* On 82575 these are collision counts */
4924 device_printf(dev, "Collision/Carrier extension errors = %lld\n",
4925 (long long)adapter->stats.cexterr);
4926 device_printf(dev, "RX overruns = %ld\n", adapter->rx_overruns);
4927 device_printf(dev, "watchdog timeouts = %ld\n",
4928 adapter->watchdog_events);
4929 device_printf(dev, "XON Rcvd = %lld\n",
4930 (long long)adapter->stats.xonrxc);
4931 device_printf(dev, "XON Xmtd = %lld\n",
4932 (long long)adapter->stats.xontxc);
4933 device_printf(dev, "XOFF Rcvd = %lld\n",
4934 (long long)adapter->stats.xoffrxc);
4935 device_printf(dev, "XOFF Xmtd = %lld\n",
4936 (long long)adapter->stats.xofftxc);
4937 device_printf(dev, "Good Packets Rcvd = %lld\n",
4938 (long long)adapter->stats.gprc);
4939 device_printf(dev, "Good Packets Xmtd = %lld\n",
4940 (long long)adapter->stats.gptc);
4941 device_printf(dev, "TSO Contexts Xmtd = %lld\n",
4942 (long long)adapter->stats.tsctc);
4943 device_printf(dev, "TSO Contexts Failed = %lld\n",
4944 (long long)adapter->stats.tsctfc);
4947 /**********************************************************************
4949 * This routine provides a way to dump out the adapter eeprom,
4950 * often a useful debug/service tool. This only dumps the first
4951 * 32 words, stuff that matters is in that extent.
4953 **********************************************************************/
4955 igb_print_nvm_info(struct adapter *adapter)
4960 /* Its a bit crude, but it gets the job done */
4961 kprintf("\nInterface EEPROM Dump:\n");
4962 kprintf("Offset\n0x0000 ");
4963 for (i = 0, j = 0; i < 32; i++, j++) {
4964 if (j == 8) { /* Make the offset block */
4966 kprintf("\n0x00%x0 ",row);
4968 e1000_read_nvm(&adapter->hw, i, 1, &eeprom_data);
4969 kprintf("%04x ", eeprom_data);
4975 igb_sysctl_debug_info(SYSCTL_HANDLER_ARGS)
4977 struct adapter *adapter;
4982 error = sysctl_handle_int(oidp, &result, 0, req);
4984 if (error || !req->newptr)
4988 adapter = (struct adapter *)arg1;
4989 igb_print_debug_info(adapter);
4992 * This value will cause a hex dump of the
4993 * first 32 16-bit words of the EEPROM to
4997 adapter = (struct adapter *)arg1;
4998 igb_print_nvm_info(adapter);
5006 igb_sysctl_stats(SYSCTL_HANDLER_ARGS)
5008 struct adapter *adapter;
5013 error = sysctl_handle_int(oidp, &result, 0, req);
5015 if (error || !req->newptr)
5019 adapter = (struct adapter *)arg1;
5020 igb_print_hw_stats(adapter);
5027 igb_add_rx_process_limit(struct adapter *adapter, const char *name,
5028 const char *description, int *limit, int value)
5031 SYSCTL_ADD_INT(&adapter->sysctl_ctx,
5032 SYSCTL_CHILDREN(adapter->sysctl_tree),
5033 OID_AUTO, name, CTLTYPE_INT|CTLFLAG_RW, limit, value, description);