2 * Copyright (c) 2001-2011, Intel Corporation
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the Intel Corporation nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include "opt_ifpoll.h"
35 #include <sys/param.h>
37 #include <sys/endian.h>
38 #include <sys/interrupt.h>
39 #include <sys/kernel.h>
40 #include <sys/malloc.h>
44 #include <sys/serialize.h>
45 #include <sys/serialize2.h>
46 #include <sys/socket.h>
47 #include <sys/sockio.h>
48 #include <sys/sysctl.h>
49 #include <sys/systm.h>
52 #include <net/ethernet.h>
54 #include <net/if_arp.h>
55 #include <net/if_dl.h>
56 #include <net/if_media.h>
57 #include <net/ifq_var.h>
58 #include <net/toeplitz.h>
59 #include <net/toeplitz2.h>
60 #include <net/vlan/if_vlan_var.h>
61 #include <net/vlan/if_vlan_ether.h>
62 #include <net/if_poll.h>
64 #include <netinet/in_systm.h>
65 #include <netinet/in.h>
66 #include <netinet/ip.h>
68 #include <bus/pci/pcivar.h>
69 #include <bus/pci/pcireg.h>
71 #include <dev/netif/ig_hal/e1000_api.h>
72 #include <dev/netif/ig_hal/e1000_82575.h>
73 #include <dev/netif/ig_hal/e1000_dragonfly.h>
74 #include <dev/netif/igb/if_igb.h>
77 #define IGB_RSS_DPRINTF(sc, lvl, fmt, ...) \
79 if (sc->rss_debug >= lvl) \
80 if_printf(&sc->arpcom.ac_if, fmt, __VA_ARGS__); \
82 #else /* !IGB_RSS_DEBUG */
83 #define IGB_RSS_DPRINTF(sc, lvl, fmt, ...) ((void)0)
84 #endif /* IGB_RSS_DEBUG */
86 #define IGB_NAME "Intel(R) PRO/1000 "
87 #define IGB_DEVICE(id) \
88 { IGB_VENDOR_ID, E1000_DEV_ID_##id, IGB_NAME #id }
89 #define IGB_DEVICE_NULL { 0, 0, NULL }
91 static struct igb_device {
96 IGB_DEVICE(82575EB_COPPER),
97 IGB_DEVICE(82575EB_FIBER_SERDES),
98 IGB_DEVICE(82575GB_QUAD_COPPER),
100 IGB_DEVICE(82576_NS),
101 IGB_DEVICE(82576_NS_SERDES),
102 IGB_DEVICE(82576_FIBER),
103 IGB_DEVICE(82576_SERDES),
104 IGB_DEVICE(82576_SERDES_QUAD),
105 IGB_DEVICE(82576_QUAD_COPPER),
106 IGB_DEVICE(82576_QUAD_COPPER_ET2),
107 IGB_DEVICE(82576_VF),
108 IGB_DEVICE(82580_COPPER),
109 IGB_DEVICE(82580_FIBER),
110 IGB_DEVICE(82580_SERDES),
111 IGB_DEVICE(82580_SGMII),
112 IGB_DEVICE(82580_COPPER_DUAL),
113 IGB_DEVICE(82580_QUAD_FIBER),
114 IGB_DEVICE(DH89XXCC_SERDES),
115 IGB_DEVICE(DH89XXCC_SGMII),
116 IGB_DEVICE(DH89XXCC_SFP),
117 IGB_DEVICE(DH89XXCC_BACKPLANE),
118 IGB_DEVICE(I350_COPPER),
119 IGB_DEVICE(I350_FIBER),
120 IGB_DEVICE(I350_SERDES),
121 IGB_DEVICE(I350_SGMII),
123 IGB_DEVICE(I210_COPPER),
124 IGB_DEVICE(I210_COPPER_IT),
125 IGB_DEVICE(I210_COPPER_OEM1),
126 IGB_DEVICE(I210_COPPER_FLASHLESS),
127 IGB_DEVICE(I210_SERDES_FLASHLESS),
128 IGB_DEVICE(I210_FIBER),
129 IGB_DEVICE(I210_SERDES),
130 IGB_DEVICE(I210_SGMII),
131 IGB_DEVICE(I211_COPPER),
132 IGB_DEVICE(I354_BACKPLANE_1GBPS),
133 IGB_DEVICE(I354_SGMII),
135 /* required last entry */
139 static int igb_probe(device_t);
140 static int igb_attach(device_t);
141 static int igb_detach(device_t);
142 static int igb_shutdown(device_t);
143 static int igb_suspend(device_t);
144 static int igb_resume(device_t);
146 static boolean_t igb_is_valid_ether_addr(const uint8_t *);
147 static void igb_setup_ifp(struct igb_softc *);
148 static boolean_t igb_txcsum_ctx(struct igb_tx_ring *, struct mbuf *);
149 static int igb_tso_pullup(struct igb_tx_ring *, struct mbuf **);
150 static void igb_tso_ctx(struct igb_tx_ring *, struct mbuf *, uint32_t *);
151 static void igb_add_sysctl(struct igb_softc *);
152 static int igb_sysctl_intr_rate(SYSCTL_HANDLER_ARGS);
153 static int igb_sysctl_msix_rate(SYSCTL_HANDLER_ARGS);
154 static int igb_sysctl_tx_intr_nsegs(SYSCTL_HANDLER_ARGS);
155 static int igb_sysctl_tx_wreg_nsegs(SYSCTL_HANDLER_ARGS);
156 static int igb_sysctl_rx_wreg_nsegs(SYSCTL_HANDLER_ARGS);
157 static void igb_set_ring_inuse(struct igb_softc *, boolean_t);
158 static int igb_get_rxring_inuse(const struct igb_softc *, boolean_t);
159 static int igb_get_txring_inuse(const struct igb_softc *, boolean_t);
160 static void igb_set_timer_cpuid(struct igb_softc *, boolean_t);
162 static int igb_sysctl_npoll_rxoff(SYSCTL_HANDLER_ARGS);
163 static int igb_sysctl_npoll_txoff(SYSCTL_HANDLER_ARGS);
166 static void igb_vf_init_stats(struct igb_softc *);
167 static void igb_reset(struct igb_softc *);
168 static void igb_update_stats_counters(struct igb_softc *);
169 static void igb_update_vf_stats_counters(struct igb_softc *);
170 static void igb_update_link_status(struct igb_softc *);
171 static void igb_init_tx_unit(struct igb_softc *);
172 static void igb_init_rx_unit(struct igb_softc *);
174 static void igb_set_vlan(struct igb_softc *);
175 static void igb_set_multi(struct igb_softc *);
176 static void igb_set_promisc(struct igb_softc *);
177 static void igb_disable_promisc(struct igb_softc *);
179 static int igb_alloc_rings(struct igb_softc *);
180 static void igb_free_rings(struct igb_softc *);
181 static int igb_create_tx_ring(struct igb_tx_ring *);
182 static int igb_create_rx_ring(struct igb_rx_ring *);
183 static void igb_free_tx_ring(struct igb_tx_ring *);
184 static void igb_free_rx_ring(struct igb_rx_ring *);
185 static void igb_destroy_tx_ring(struct igb_tx_ring *, int);
186 static void igb_destroy_rx_ring(struct igb_rx_ring *, int);
187 static void igb_init_tx_ring(struct igb_tx_ring *);
188 static int igb_init_rx_ring(struct igb_rx_ring *);
189 static int igb_newbuf(struct igb_rx_ring *, int, boolean_t);
190 static int igb_encap(struct igb_tx_ring *, struct mbuf **, int *, int *);
191 static void igb_rx_refresh(struct igb_rx_ring *, int);
192 static void igb_setup_serializer(struct igb_softc *);
194 static void igb_stop(struct igb_softc *);
195 static void igb_init(void *);
196 static int igb_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
197 static void igb_media_status(struct ifnet *, struct ifmediareq *);
198 static int igb_media_change(struct ifnet *);
199 static void igb_timer(void *);
200 static void igb_watchdog(struct ifaltq_subque *);
201 static void igb_start(struct ifnet *, struct ifaltq_subque *);
203 static void igb_npoll(struct ifnet *, struct ifpoll_info *);
204 static void igb_npoll_rx(struct ifnet *, void *, int);
205 static void igb_npoll_tx(struct ifnet *, void *, int);
206 static void igb_npoll_status(struct ifnet *);
208 static void igb_serialize(struct ifnet *, enum ifnet_serialize);
209 static void igb_deserialize(struct ifnet *, enum ifnet_serialize);
210 static int igb_tryserialize(struct ifnet *, enum ifnet_serialize);
212 static void igb_serialize_assert(struct ifnet *, enum ifnet_serialize,
216 static void igb_intr(void *);
217 static void igb_intr_shared(void *);
218 static void igb_rxeof(struct igb_rx_ring *, int);
219 static void igb_txeof(struct igb_tx_ring *);
220 static void igb_set_eitr(struct igb_softc *, int, int);
221 static void igb_enable_intr(struct igb_softc *);
222 static void igb_disable_intr(struct igb_softc *);
223 static void igb_init_unshared_intr(struct igb_softc *);
224 static void igb_init_intr(struct igb_softc *);
225 static int igb_setup_intr(struct igb_softc *);
226 static void igb_set_txintr_mask(struct igb_tx_ring *, int *, int);
227 static void igb_set_rxintr_mask(struct igb_rx_ring *, int *, int);
228 static void igb_set_intr_mask(struct igb_softc *);
229 static int igb_alloc_intr(struct igb_softc *);
230 static void igb_free_intr(struct igb_softc *);
231 static void igb_teardown_intr(struct igb_softc *);
232 static void igb_msix_try_alloc(struct igb_softc *);
233 static void igb_msix_rx_conf(struct igb_softc *, int, int *, int);
234 static void igb_msix_tx_conf(struct igb_softc *, int, int *, int);
235 static void igb_msix_free(struct igb_softc *, boolean_t);
236 static int igb_msix_setup(struct igb_softc *);
237 static void igb_msix_teardown(struct igb_softc *, int);
238 static void igb_msix_rx(void *);
239 static void igb_msix_tx(void *);
240 static void igb_msix_status(void *);
241 static void igb_msix_rxtx(void *);
243 /* Management and WOL Support */
244 static void igb_get_mgmt(struct igb_softc *);
245 static void igb_rel_mgmt(struct igb_softc *);
246 static void igb_get_hw_control(struct igb_softc *);
247 static void igb_rel_hw_control(struct igb_softc *);
248 static void igb_enable_wol(device_t);
250 static device_method_t igb_methods[] = {
251 /* Device interface */
252 DEVMETHOD(device_probe, igb_probe),
253 DEVMETHOD(device_attach, igb_attach),
254 DEVMETHOD(device_detach, igb_detach),
255 DEVMETHOD(device_shutdown, igb_shutdown),
256 DEVMETHOD(device_suspend, igb_suspend),
257 DEVMETHOD(device_resume, igb_resume),
261 static driver_t igb_driver = {
264 sizeof(struct igb_softc),
267 static devclass_t igb_devclass;
269 DECLARE_DUMMY_MODULE(if_igb);
270 MODULE_DEPEND(igb, ig_hal, 1, 1, 1);
271 DRIVER_MODULE(if_igb, pci, igb_driver, igb_devclass, NULL, NULL);
273 static int igb_rxd = IGB_DEFAULT_RXD;
274 static int igb_txd = IGB_DEFAULT_TXD;
275 static int igb_rxr = 0;
276 static int igb_txr = 0;
277 static int igb_msi_enable = 1;
278 static int igb_msix_enable = 1;
279 static int igb_eee_disabled = 1; /* Energy Efficient Ethernet */
281 static char igb_flowctrl[IFM_ETH_FC_STRLEN] = IFM_ETH_FC_RXPAUSE;
284 * DMA Coalescing, only for i350 - default to off,
285 * this feature is for power savings
287 static int igb_dma_coalesce = 0;
289 TUNABLE_INT("hw.igb.rxd", &igb_rxd);
290 TUNABLE_INT("hw.igb.txd", &igb_txd);
291 TUNABLE_INT("hw.igb.rxr", &igb_rxr);
292 TUNABLE_INT("hw.igb.txr", &igb_txr);
293 TUNABLE_INT("hw.igb.msi.enable", &igb_msi_enable);
294 TUNABLE_INT("hw.igb.msix.enable", &igb_msix_enable);
295 TUNABLE_STR("hw.igb.flow_ctrl", igb_flowctrl, sizeof(igb_flowctrl));
298 TUNABLE_INT("hw.igb.eee_disabled", &igb_eee_disabled);
299 TUNABLE_INT("hw.igb.dma_coalesce", &igb_dma_coalesce);
302 igb_rxcsum(uint32_t staterr, struct mbuf *mp)
304 /* Ignore Checksum bit is set */
305 if (staterr & E1000_RXD_STAT_IXSM)
308 if ((staterr & (E1000_RXD_STAT_IPCS | E1000_RXDEXT_STATERR_IPE)) ==
310 mp->m_pkthdr.csum_flags |= CSUM_IP_CHECKED | CSUM_IP_VALID;
312 if (staterr & (E1000_RXD_STAT_TCPCS | E1000_RXD_STAT_UDPCS)) {
313 if ((staterr & E1000_RXDEXT_STATERR_TCPE) == 0) {
314 mp->m_pkthdr.csum_flags |= CSUM_DATA_VALID |
315 CSUM_PSEUDO_HDR | CSUM_FRAG_NOT_CHECKED;
316 mp->m_pkthdr.csum_data = htons(0xffff);
321 static __inline struct pktinfo *
322 igb_rssinfo(struct mbuf *m, struct pktinfo *pi,
323 uint32_t hash, uint32_t hashtype, uint32_t staterr)
326 case E1000_RXDADV_RSSTYPE_IPV4_TCP:
327 pi->pi_netisr = NETISR_IP;
329 pi->pi_l3proto = IPPROTO_TCP;
332 case E1000_RXDADV_RSSTYPE_IPV4:
333 if (staterr & E1000_RXD_STAT_IXSM)
337 (E1000_RXD_STAT_TCPCS | E1000_RXDEXT_STATERR_TCPE)) ==
338 E1000_RXD_STAT_TCPCS) {
339 pi->pi_netisr = NETISR_IP;
341 pi->pi_l3proto = IPPROTO_UDP;
349 m->m_flags |= M_HASH;
350 m->m_pkthdr.hash = toeplitz_hash(hash);
355 igb_probe(device_t dev)
357 const struct igb_device *d;
360 vid = pci_get_vendor(dev);
361 did = pci_get_device(dev);
363 for (d = igb_devices; d->desc != NULL; ++d) {
364 if (vid == d->vid && did == d->did) {
365 device_set_desc(dev, d->desc);
373 igb_attach(device_t dev)
375 struct igb_softc *sc = device_get_softc(dev);
376 uint16_t eeprom_data;
377 int error = 0, ring_max;
378 char flowctrl[IFM_ETH_FC_STRLEN];
380 int offset, offset_def;
385 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
386 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
387 OID_AUTO, "nvm", CTLTYPE_INT|CTLFLAG_RW, adapter, 0,
388 igb_sysctl_nvm_info, "I", "NVM Information");
391 ifmedia_init(&sc->media, IFM_IMASK | IFM_ETH_FCMASK,
392 igb_media_change, igb_media_status);
393 callout_init_mp(&sc->timer);
394 lwkt_serialize_init(&sc->main_serialize);
396 if_initname(&sc->arpcom.ac_if, device_get_name(dev),
397 device_get_unit(dev));
398 sc->dev = sc->osdep.dev = dev;
401 * Determine hardware and mac type
403 sc->hw.vendor_id = pci_get_vendor(dev);
404 sc->hw.device_id = pci_get_device(dev);
405 sc->hw.revision_id = pci_read_config(dev, PCIR_REVID, 1);
406 sc->hw.subsystem_vendor_id = pci_read_config(dev, PCIR_SUBVEND_0, 2);
407 sc->hw.subsystem_device_id = pci_read_config(dev, PCIR_SUBDEV_0, 2);
409 if (e1000_set_mac_type(&sc->hw))
412 /* Are we a VF device? */
413 if (sc->hw.mac.type == e1000_vfadapt ||
414 sc->hw.mac.type == e1000_vfadapt_i350)
420 * Configure total supported RX/TX ring count
422 switch (sc->hw.mac.type) {
424 ring_max = IGB_MAX_RING_82575;
428 ring_max = IGB_MAX_RING_82576;
432 ring_max = IGB_MAX_RING_82580;
436 ring_max = IGB_MAX_RING_I350;
440 ring_max = IGB_MAX_RING_I354;
444 ring_max = IGB_MAX_RING_I210;
448 ring_max = IGB_MAX_RING_I211;
452 ring_max = IGB_MIN_RING;
456 sc->rx_ring_cnt = device_getenv_int(dev, "rxr", igb_rxr);
457 sc->rx_ring_cnt = if_ring_count2(sc->rx_ring_cnt, ring_max);
459 sc->rx_ring_cnt = device_getenv_int(dev, "rxr_debug", sc->rx_ring_cnt);
461 sc->rx_ring_inuse = sc->rx_ring_cnt;
463 sc->tx_ring_cnt = device_getenv_int(dev, "txr", igb_txr);
464 sc->tx_ring_cnt = if_ring_count2(sc->tx_ring_cnt, ring_max);
466 sc->tx_ring_cnt = device_getenv_int(dev, "txr_debug", sc->tx_ring_cnt);
468 sc->tx_ring_inuse = sc->tx_ring_cnt;
470 /* Setup flow control. */
471 device_getenv_string(dev, "flow_ctrl", flowctrl, sizeof(flowctrl),
473 sc->ifm_flowctrl = ifmedia_str2ethfc(flowctrl);
475 /* Enable bus mastering */
476 pci_enable_busmaster(dev);
481 sc->mem_rid = PCIR_BAR(0);
482 sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->mem_rid,
484 if (sc->mem_res == NULL) {
485 device_printf(dev, "Unable to allocate bus resource: memory\n");
489 sc->osdep.mem_bus_space_tag = rman_get_bustag(sc->mem_res);
490 sc->osdep.mem_bus_space_handle = rman_get_bushandle(sc->mem_res);
492 sc->hw.hw_addr = (uint8_t *)&sc->osdep.mem_bus_space_handle;
494 /* Save PCI command register for Shared Code */
495 sc->hw.bus.pci_cmd_word = pci_read_config(dev, PCIR_COMMAND, 2);
496 sc->hw.back = &sc->osdep;
498 /* Do Shared Code initialization */
499 if (e1000_setup_init_funcs(&sc->hw, TRUE)) {
500 device_printf(dev, "Setup of Shared code failed\n");
505 e1000_get_bus_info(&sc->hw);
507 sc->hw.mac.autoneg = DO_AUTO_NEG;
508 sc->hw.phy.autoneg_wait_to_complete = FALSE;
509 sc->hw.phy.autoneg_advertised = AUTONEG_ADV_DEFAULT;
512 if (sc->hw.phy.media_type == e1000_media_type_copper) {
513 sc->hw.phy.mdix = AUTO_ALL_MODES;
514 sc->hw.phy.disable_polarity_correction = FALSE;
515 sc->hw.phy.ms_type = IGB_MASTER_SLAVE;
518 /* Set the frame limits assuming standard ethernet sized frames. */
519 sc->max_frame_size = ETHERMTU + ETHER_HDR_LEN + ETHER_CRC_LEN;
521 /* Allocate RX/TX rings */
522 error = igb_alloc_rings(sc);
528 * NPOLLING RX CPU offset
530 if (sc->rx_ring_cnt == ncpus2) {
533 offset_def = (sc->rx_ring_cnt * device_get_unit(dev)) % ncpus2;
534 offset = device_getenv_int(dev, "npoll.rxoff", offset_def);
535 if (offset >= ncpus2 ||
536 offset % sc->rx_ring_cnt != 0) {
537 device_printf(dev, "invalid npoll.rxoff %d, use %d\n",
542 sc->rx_npoll_off = offset;
545 * NPOLLING TX CPU offset
547 if (sc->tx_ring_cnt == ncpus2) {
550 offset_def = (sc->tx_ring_cnt * device_get_unit(dev)) % ncpus2;
551 offset = device_getenv_int(dev, "npoll.txoff", offset_def);
552 if (offset >= ncpus2 ||
553 offset % sc->tx_ring_cnt != 0) {
554 device_printf(dev, "invalid npoll.txoff %d, use %d\n",
559 sc->tx_npoll_off = offset;
562 /* Allocate interrupt */
563 error = igb_alloc_intr(sc);
567 /* Setup serializers */
568 igb_setup_serializer(sc);
570 /* Allocate the appropriate stats memory */
572 sc->stats = kmalloc(sizeof(struct e1000_vf_stats), M_DEVBUF,
574 igb_vf_init_stats(sc);
576 sc->stats = kmalloc(sizeof(struct e1000_hw_stats), M_DEVBUF,
580 /* Allocate multicast array memory. */
581 sc->mta = kmalloc(ETHER_ADDR_LEN * MAX_NUM_MULTICAST_ADDRESSES,
584 /* Some adapter-specific advanced features */
585 if (sc->hw.mac.type >= e1000_i350) {
587 igb_set_sysctl_value(adapter, "dma_coalesce",
588 "configure dma coalesce",
589 &adapter->dma_coalesce, igb_dma_coalesce);
590 igb_set_sysctl_value(adapter, "eee_disabled",
591 "enable Energy Efficient Ethernet",
592 &adapter->hw.dev_spec._82575.eee_disable,
595 sc->dma_coalesce = igb_dma_coalesce;
596 sc->hw.dev_spec._82575.eee_disable = igb_eee_disabled;
598 if (sc->hw.phy.media_type == e1000_media_type_copper) {
599 if (sc->hw.mac.type == e1000_i354)
600 e1000_set_eee_i354(&sc->hw);
602 e1000_set_eee_i350(&sc->hw);
607 * Start from a known state, this is important in reading the nvm and
610 e1000_reset_hw(&sc->hw);
612 /* Make sure we have a good EEPROM before we read from it */
613 if (sc->hw.mac.type != e1000_i210 && sc->hw.mac.type != e1000_i211 &&
614 e1000_validate_nvm_checksum(&sc->hw) < 0) {
616 * Some PCI-E parts fail the first check due to
617 * the link being in sleep state, call it again,
618 * if it fails a second time its a real issue.
620 if (e1000_validate_nvm_checksum(&sc->hw) < 0) {
622 "The EEPROM Checksum Is Not Valid\n");
628 /* Copy the permanent MAC address out of the EEPROM */
629 if (e1000_read_mac_addr(&sc->hw) < 0) {
630 device_printf(dev, "EEPROM read error while reading MAC"
635 if (!igb_is_valid_ether_addr(sc->hw.mac.addr)) {
636 device_printf(dev, "Invalid MAC address\n");
641 /* Setup OS specific network interface */
644 /* Add sysctl tree, must after igb_setup_ifp() */
647 /* Now get a good starting state */
650 /* Initialize statistics */
651 igb_update_stats_counters(sc);
653 sc->hw.mac.get_link_status = 1;
654 igb_update_link_status(sc);
656 /* Indicate SOL/IDER usage */
657 if (e1000_check_reset_block(&sc->hw)) {
659 "PHY reset is blocked due to SOL/IDER session.\n");
662 /* Determine if we have to control management hardware */
663 if (e1000_enable_mng_pass_thru(&sc->hw))
664 sc->flags |= IGB_FLAG_HAS_MGMT;
669 /* APME bit in EEPROM is mapped to WUC.APME */
670 eeprom_data = E1000_READ_REG(&sc->hw, E1000_WUC) & E1000_WUC_APME;
672 sc->wol = E1000_WUFC_MAG;
673 /* XXX disable WOL */
677 /* Register for VLAN events */
678 adapter->vlan_attach = EVENTHANDLER_REGISTER(vlan_config,
679 igb_register_vlan, adapter, EVENTHANDLER_PRI_FIRST);
680 adapter->vlan_detach = EVENTHANDLER_REGISTER(vlan_unconfig,
681 igb_unregister_vlan, adapter, EVENTHANDLER_PRI_FIRST);
685 igb_add_hw_stats(adapter);
689 * Disable interrupt to prevent spurious interrupts (line based
690 * interrupt, MSI or even MSI-X), which had been observed on
691 * several types of LOMs, from being handled.
693 igb_disable_intr(sc);
695 error = igb_setup_intr(sc);
697 ether_ifdetach(&sc->arpcom.ac_if);
708 igb_detach(device_t dev)
710 struct igb_softc *sc = device_get_softc(dev);
712 if (device_is_attached(dev)) {
713 struct ifnet *ifp = &sc->arpcom.ac_if;
715 ifnet_serialize_all(ifp);
719 e1000_phy_hw_reset(&sc->hw);
721 /* Give control back to firmware */
723 igb_rel_hw_control(sc);
726 E1000_WRITE_REG(&sc->hw, E1000_WUC, E1000_WUC_PME_EN);
727 E1000_WRITE_REG(&sc->hw, E1000_WUFC, sc->wol);
731 igb_teardown_intr(sc);
733 ifnet_deserialize_all(ifp);
736 } else if (sc->mem_res != NULL) {
737 igb_rel_hw_control(sc);
740 ifmedia_removeall(&sc->media);
741 bus_generic_detach(dev);
745 if (sc->msix_mem_res != NULL) {
746 bus_release_resource(dev, SYS_RES_MEMORY, sc->msix_mem_rid,
749 if (sc->mem_res != NULL) {
750 bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid,
757 kfree(sc->mta, M_DEVBUF);
758 if (sc->stats != NULL)
759 kfree(sc->stats, M_DEVBUF);
760 if (sc->serializes != NULL)
761 kfree(sc->serializes, M_DEVBUF);
767 igb_shutdown(device_t dev)
769 return igb_suspend(dev);
773 igb_suspend(device_t dev)
775 struct igb_softc *sc = device_get_softc(dev);
776 struct ifnet *ifp = &sc->arpcom.ac_if;
778 ifnet_serialize_all(ifp);
783 igb_rel_hw_control(sc);
786 E1000_WRITE_REG(&sc->hw, E1000_WUC, E1000_WUC_PME_EN);
787 E1000_WRITE_REG(&sc->hw, E1000_WUFC, sc->wol);
791 ifnet_deserialize_all(ifp);
793 return bus_generic_suspend(dev);
797 igb_resume(device_t dev)
799 struct igb_softc *sc = device_get_softc(dev);
800 struct ifnet *ifp = &sc->arpcom.ac_if;
803 ifnet_serialize_all(ifp);
808 for (i = 0; i < sc->tx_ring_inuse; ++i)
809 ifsq_devstart_sched(sc->tx_rings[i].ifsq);
811 ifnet_deserialize_all(ifp);
813 return bus_generic_resume(dev);
817 igb_ioctl(struct ifnet *ifp, u_long command, caddr_t data, struct ucred *cr)
819 struct igb_softc *sc = ifp->if_softc;
820 struct ifreq *ifr = (struct ifreq *)data;
821 int max_frame_size, mask, reinit;
824 ASSERT_IFNET_SERIALIZED_ALL(ifp);
828 max_frame_size = 9234;
829 if (ifr->ifr_mtu > max_frame_size - ETHER_HDR_LEN -
835 ifp->if_mtu = ifr->ifr_mtu;
836 sc->max_frame_size = ifp->if_mtu + ETHER_HDR_LEN +
839 if (ifp->if_flags & IFF_RUNNING)
844 if (ifp->if_flags & IFF_UP) {
845 if (ifp->if_flags & IFF_RUNNING) {
846 if ((ifp->if_flags ^ sc->if_flags) &
847 (IFF_PROMISC | IFF_ALLMULTI)) {
848 igb_disable_promisc(sc);
854 } else if (ifp->if_flags & IFF_RUNNING) {
857 sc->if_flags = ifp->if_flags;
862 if (ifp->if_flags & IFF_RUNNING) {
863 igb_disable_intr(sc);
866 if (!(ifp->if_flags & IFF_NPOLLING))
873 /* Check SOL/IDER usage */
874 if (e1000_check_reset_block(&sc->hw)) {
875 if_printf(ifp, "Media change is "
876 "blocked due to SOL/IDER session.\n");
882 error = ifmedia_ioctl(ifp, ifr, &sc->media, command);
887 mask = ifr->ifr_reqcap ^ ifp->if_capenable;
888 if (mask & IFCAP_RXCSUM) {
889 ifp->if_capenable ^= IFCAP_RXCSUM;
892 if (mask & IFCAP_VLAN_HWTAGGING) {
893 ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
896 if (mask & IFCAP_TXCSUM) {
897 ifp->if_capenable ^= IFCAP_TXCSUM;
898 if (ifp->if_capenable & IFCAP_TXCSUM)
899 ifp->if_hwassist |= IGB_CSUM_FEATURES;
901 ifp->if_hwassist &= ~IGB_CSUM_FEATURES;
903 if (mask & IFCAP_TSO) {
904 ifp->if_capenable ^= IFCAP_TSO;
905 if (ifp->if_capenable & IFCAP_TSO)
906 ifp->if_hwassist |= CSUM_TSO;
908 ifp->if_hwassist &= ~CSUM_TSO;
910 if (mask & IFCAP_RSS)
911 ifp->if_capenable ^= IFCAP_RSS;
912 if (reinit && (ifp->if_flags & IFF_RUNNING))
917 error = ether_ioctl(ifp, command, data);
926 struct igb_softc *sc = xsc;
927 struct ifnet *ifp = &sc->arpcom.ac_if;
931 ASSERT_IFNET_SERIALIZED_ALL(ifp);
935 /* Get the latest mac address, User can use a LAA */
936 bcopy(IF_LLADDR(ifp), sc->hw.mac.addr, ETHER_ADDR_LEN);
938 /* Put the address into the Receive Address Array */
939 e1000_rar_set(&sc->hw, sc->hw.mac.addr, 0);
942 igb_update_link_status(sc);
944 E1000_WRITE_REG(&sc->hw, E1000_VET, ETHERTYPE_VLAN);
946 /* Configure for OS presence */
951 if (ifp->if_flags & IFF_NPOLLING)
955 /* Configured used RX/TX rings */
956 igb_set_ring_inuse(sc, polling);
957 ifq_set_subq_mask(&ifp->if_snd, sc->tx_ring_inuse - 1);
959 /* Initialize interrupt */
962 /* Prepare transmit descriptors and buffers */
963 for (i = 0; i < sc->tx_ring_inuse; ++i)
964 igb_init_tx_ring(&sc->tx_rings[i]);
965 igb_init_tx_unit(sc);
967 /* Setup Multicast table */
972 * Figure out the desired mbuf pool
973 * for doing jumbo/packetsplit
975 if (adapter->max_frame_size <= 2048)
976 adapter->rx_mbuf_sz = MCLBYTES;
977 else if (adapter->max_frame_size <= 4096)
978 adapter->rx_mbuf_sz = MJUMPAGESIZE;
980 adapter->rx_mbuf_sz = MJUM9BYTES;
983 /* Prepare receive descriptors and buffers */
984 for (i = 0; i < sc->rx_ring_inuse; ++i) {
987 error = igb_init_rx_ring(&sc->rx_rings[i]);
989 if_printf(ifp, "Could not setup receive structures\n");
994 igb_init_rx_unit(sc);
996 /* Enable VLAN support */
997 if (ifp->if_capenable & IFCAP_VLAN_HWTAGGING)
1000 /* Don't lose promiscuous settings */
1001 igb_set_promisc(sc);
1003 ifp->if_flags |= IFF_RUNNING;
1004 for (i = 0; i < sc->tx_ring_inuse; ++i) {
1005 ifsq_clr_oactive(sc->tx_rings[i].ifsq);
1006 ifsq_watchdog_start(&sc->tx_rings[i].tx_watchdog);
1009 igb_set_timer_cpuid(sc, polling);
1010 callout_reset_bycpu(&sc->timer, hz, igb_timer, sc, sc->timer_cpuid);
1011 e1000_clear_hw_cntrs_base_generic(&sc->hw);
1013 /* This clears any pending interrupts */
1014 E1000_READ_REG(&sc->hw, E1000_ICR);
1017 * Only enable interrupts if we are not polling, make sure
1018 * they are off otherwise.
1021 igb_disable_intr(sc);
1023 igb_enable_intr(sc);
1024 E1000_WRITE_REG(&sc->hw, E1000_ICS, E1000_ICS_LSC);
1027 /* Set Energy Efficient Ethernet */
1028 if (sc->hw.phy.media_type == e1000_media_type_copper) {
1029 if (sc->hw.mac.type == e1000_i354)
1030 e1000_set_eee_i354(&sc->hw);
1032 e1000_set_eee_i350(&sc->hw);
1037 igb_media_status(struct ifnet *ifp, struct ifmediareq *ifmr)
1039 struct igb_softc *sc = ifp->if_softc;
1041 ASSERT_IFNET_SERIALIZED_ALL(ifp);
1043 if ((ifp->if_flags & IFF_RUNNING) == 0)
1044 sc->hw.mac.get_link_status = 1;
1045 igb_update_link_status(sc);
1047 ifmr->ifm_status = IFM_AVALID;
1048 ifmr->ifm_active = IFM_ETHER;
1050 if (!sc->link_active) {
1051 if (sc->hw.mac.autoneg)
1052 ifmr->ifm_active |= IFM_NONE;
1054 ifmr->ifm_active |= sc->media.ifm_media;
1058 ifmr->ifm_status |= IFM_ACTIVE;
1059 if (sc->ifm_flowctrl & IFM_ETH_FORCEPAUSE)
1060 ifmr->ifm_active |= sc->ifm_flowctrl;
1062 switch (sc->link_speed) {
1064 ifmr->ifm_active |= IFM_10_T;
1069 * Support for 100Mb SFP - these are Fiber
1070 * but the media type appears as serdes
1072 if (sc->hw.phy.media_type == e1000_media_type_fiber ||
1073 sc->hw.phy.media_type == e1000_media_type_internal_serdes)
1074 ifmr->ifm_active |= IFM_100_FX;
1076 ifmr->ifm_active |= IFM_100_TX;
1080 if (sc->hw.phy.media_type == e1000_media_type_fiber ||
1081 sc->hw.phy.media_type == e1000_media_type_internal_serdes)
1082 ifmr->ifm_active |= IFM_1000_SX;
1084 ifmr->ifm_active |= IFM_1000_T;
1088 if (sc->link_duplex == FULL_DUPLEX)
1089 ifmr->ifm_active |= IFM_FDX;
1091 ifmr->ifm_active |= IFM_HDX;
1093 if (sc->link_duplex == FULL_DUPLEX)
1094 ifmr->ifm_active |= e1000_fc2ifmedia(sc->hw.fc.current_mode);
1098 igb_media_change(struct ifnet *ifp)
1100 struct igb_softc *sc = ifp->if_softc;
1101 struct ifmedia *ifm = &sc->media;
1103 ASSERT_IFNET_SERIALIZED_ALL(ifp);
1105 if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
1108 switch (IFM_SUBTYPE(ifm->ifm_media)) {
1110 sc->hw.mac.autoneg = DO_AUTO_NEG;
1111 sc->hw.phy.autoneg_advertised = AUTONEG_ADV_DEFAULT;
1116 sc->hw.mac.autoneg = DO_AUTO_NEG;
1117 sc->hw.phy.autoneg_advertised = ADVERTISE_1000_FULL;
1121 if (IFM_OPTIONS(ifm->ifm_media) & IFM_FDX) {
1122 sc->hw.mac.forced_speed_duplex = ADVERTISE_100_FULL;
1124 if (IFM_OPTIONS(ifm->ifm_media) &
1125 (IFM_ETH_RXPAUSE | IFM_ETH_TXPAUSE)) {
1127 if_printf(ifp, "Flow control is not "
1128 "allowed for half-duplex\n");
1132 sc->hw.mac.forced_speed_duplex = ADVERTISE_100_HALF;
1134 sc->hw.mac.autoneg = FALSE;
1135 sc->hw.phy.autoneg_advertised = 0;
1139 if (IFM_OPTIONS(ifm->ifm_media) & IFM_FDX) {
1140 sc->hw.mac.forced_speed_duplex = ADVERTISE_10_FULL;
1142 if (IFM_OPTIONS(ifm->ifm_media) &
1143 (IFM_ETH_RXPAUSE | IFM_ETH_TXPAUSE)) {
1145 if_printf(ifp, "Flow control is not "
1146 "allowed for half-duplex\n");
1150 sc->hw.mac.forced_speed_duplex = ADVERTISE_10_HALF;
1152 sc->hw.mac.autoneg = FALSE;
1153 sc->hw.phy.autoneg_advertised = 0;
1158 if_printf(ifp, "Unsupported media type %d\n",
1159 IFM_SUBTYPE(ifm->ifm_media));
1163 sc->ifm_flowctrl = ifm->ifm_media & IFM_ETH_FCMASK;
1165 if (ifp->if_flags & IFF_RUNNING)
1172 igb_set_promisc(struct igb_softc *sc)
1174 struct ifnet *ifp = &sc->arpcom.ac_if;
1175 struct e1000_hw *hw = &sc->hw;
1179 e1000_promisc_set_vf(hw, e1000_promisc_enabled);
1183 reg = E1000_READ_REG(hw, E1000_RCTL);
1184 if (ifp->if_flags & IFF_PROMISC) {
1185 reg |= (E1000_RCTL_UPE | E1000_RCTL_MPE);
1186 E1000_WRITE_REG(hw, E1000_RCTL, reg);
1187 } else if (ifp->if_flags & IFF_ALLMULTI) {
1188 reg |= E1000_RCTL_MPE;
1189 reg &= ~E1000_RCTL_UPE;
1190 E1000_WRITE_REG(hw, E1000_RCTL, reg);
1195 igb_disable_promisc(struct igb_softc *sc)
1197 struct e1000_hw *hw = &sc->hw;
1198 struct ifnet *ifp = &sc->arpcom.ac_if;
1203 e1000_promisc_set_vf(hw, e1000_promisc_disabled);
1206 reg = E1000_READ_REG(hw, E1000_RCTL);
1207 reg &= ~E1000_RCTL_UPE;
1208 if (ifp->if_flags & IFF_ALLMULTI) {
1209 mcnt = MAX_NUM_MULTICAST_ADDRESSES;
1211 struct ifmultiaddr *ifma;
1212 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1213 if (ifma->ifma_addr->sa_family != AF_LINK)
1215 if (mcnt == MAX_NUM_MULTICAST_ADDRESSES)
1220 /* Don't disable if in MAX groups */
1221 if (mcnt < MAX_NUM_MULTICAST_ADDRESSES)
1222 reg &= ~E1000_RCTL_MPE;
1223 E1000_WRITE_REG(hw, E1000_RCTL, reg);
1227 igb_set_multi(struct igb_softc *sc)
1229 struct ifnet *ifp = &sc->arpcom.ac_if;
1230 struct ifmultiaddr *ifma;
1231 uint32_t reg_rctl = 0;
1236 bzero(mta, ETH_ADDR_LEN * MAX_NUM_MULTICAST_ADDRESSES);
1238 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1239 if (ifma->ifma_addr->sa_family != AF_LINK)
1242 if (mcnt == MAX_NUM_MULTICAST_ADDRESSES)
1245 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
1246 &mta[mcnt * ETH_ADDR_LEN], ETH_ADDR_LEN);
1250 if (mcnt >= MAX_NUM_MULTICAST_ADDRESSES) {
1251 reg_rctl = E1000_READ_REG(&sc->hw, E1000_RCTL);
1252 reg_rctl |= E1000_RCTL_MPE;
1253 E1000_WRITE_REG(&sc->hw, E1000_RCTL, reg_rctl);
1255 e1000_update_mc_addr_list(&sc->hw, mta, mcnt);
1260 igb_timer(void *xsc)
1262 struct igb_softc *sc = xsc;
1264 lwkt_serialize_enter(&sc->main_serialize);
1266 igb_update_link_status(sc);
1267 igb_update_stats_counters(sc);
1269 callout_reset_bycpu(&sc->timer, hz, igb_timer, sc, sc->timer_cpuid);
1271 lwkt_serialize_exit(&sc->main_serialize);
1275 igb_update_link_status(struct igb_softc *sc)
1277 struct ifnet *ifp = &sc->arpcom.ac_if;
1278 struct e1000_hw *hw = &sc->hw;
1279 uint32_t link_check, thstat, ctrl;
1281 link_check = thstat = ctrl = 0;
1283 /* Get the cached link value or read for real */
1284 switch (hw->phy.media_type) {
1285 case e1000_media_type_copper:
1286 if (hw->mac.get_link_status) {
1287 /* Do the work to read phy */
1288 e1000_check_for_link(hw);
1289 link_check = !hw->mac.get_link_status;
1295 case e1000_media_type_fiber:
1296 e1000_check_for_link(hw);
1297 link_check = E1000_READ_REG(hw, E1000_STATUS) & E1000_STATUS_LU;
1300 case e1000_media_type_internal_serdes:
1301 e1000_check_for_link(hw);
1302 link_check = hw->mac.serdes_has_link;
1305 /* VF device is type_unknown */
1306 case e1000_media_type_unknown:
1307 e1000_check_for_link(hw);
1308 link_check = !hw->mac.get_link_status;
1314 /* Check for thermal downshift or shutdown */
1315 if (hw->mac.type == e1000_i350) {
1316 thstat = E1000_READ_REG(hw, E1000_THSTAT);
1317 ctrl = E1000_READ_REG(hw, E1000_CTRL_EXT);
1320 /* Now we check if a transition has happened */
1321 if (link_check && sc->link_active == 0) {
1322 e1000_get_speed_and_duplex(hw,
1323 &sc->link_speed, &sc->link_duplex);
1325 char flowctrl[IFM_ETH_FC_STRLEN];
1327 /* Get the flow control for display */
1328 e1000_fc2str(hw->fc.current_mode, flowctrl,
1331 if_printf(ifp, "Link is up %d Mbps %s, "
1332 "Flow control: %s\n",
1334 sc->link_duplex == FULL_DUPLEX ?
1335 "Full Duplex" : "Half Duplex",
1338 if (sc->ifm_flowctrl & IFM_ETH_FORCEPAUSE)
1339 e1000_force_flowctrl(hw, sc->ifm_flowctrl);
1340 sc->link_active = 1;
1342 ifp->if_baudrate = sc->link_speed * 1000000;
1343 if ((ctrl & E1000_CTRL_EXT_LINK_MODE_GMII) &&
1344 (thstat & E1000_THSTAT_LINK_THROTTLE))
1345 if_printf(ifp, "Link: thermal downshift\n");
1346 /* Delay Link Up for Phy update */
1347 if ((hw->mac.type == e1000_i210 ||
1348 hw->mac.type == e1000_i211) &&
1349 hw->phy.id == I210_I_PHY_ID)
1350 msec_delay(IGB_I210_LINK_DELAY);
1351 /* This can sleep */
1352 ifp->if_link_state = LINK_STATE_UP;
1353 if_link_state_change(ifp);
1354 } else if (!link_check && sc->link_active == 1) {
1355 ifp->if_baudrate = sc->link_speed = 0;
1356 sc->link_duplex = 0;
1358 if_printf(ifp, "Link is Down\n");
1359 if ((ctrl & E1000_CTRL_EXT_LINK_MODE_GMII) &&
1360 (thstat & E1000_THSTAT_PWR_DOWN))
1361 if_printf(ifp, "Link: thermal shutdown\n");
1362 sc->link_active = 0;
1363 /* This can sleep */
1364 ifp->if_link_state = LINK_STATE_DOWN;
1365 if_link_state_change(ifp);
1370 igb_stop(struct igb_softc *sc)
1372 struct ifnet *ifp = &sc->arpcom.ac_if;
1375 ASSERT_IFNET_SERIALIZED_ALL(ifp);
1377 igb_disable_intr(sc);
1379 callout_stop(&sc->timer);
1381 ifp->if_flags &= ~IFF_RUNNING;
1382 for (i = 0; i < sc->tx_ring_cnt; ++i) {
1383 ifsq_clr_oactive(sc->tx_rings[i].ifsq);
1384 ifsq_watchdog_stop(&sc->tx_rings[i].tx_watchdog);
1385 sc->tx_rings[i].tx_flags &= ~IGB_TXFLAG_ENABLED;
1388 e1000_reset_hw(&sc->hw);
1389 E1000_WRITE_REG(&sc->hw, E1000_WUC, 0);
1391 e1000_led_off(&sc->hw);
1392 e1000_cleanup_led(&sc->hw);
1394 for (i = 0; i < sc->tx_ring_cnt; ++i)
1395 igb_free_tx_ring(&sc->tx_rings[i]);
1396 for (i = 0; i < sc->rx_ring_cnt; ++i)
1397 igb_free_rx_ring(&sc->rx_rings[i]);
1401 igb_reset(struct igb_softc *sc)
1403 struct ifnet *ifp = &sc->arpcom.ac_if;
1404 struct e1000_hw *hw = &sc->hw;
1405 struct e1000_fc_info *fc = &hw->fc;
1409 /* Let the firmware know the OS is in control */
1410 igb_get_hw_control(sc);
1413 * Packet Buffer Allocation (PBA)
1414 * Writing PBA sets the receive portion of the buffer
1415 * the remainder is used for the transmit buffer.
1417 switch (hw->mac.type) {
1419 pba = E1000_PBA_32K;
1424 pba = E1000_READ_REG(hw, E1000_RXPBS);
1425 pba &= E1000_RXPBS_SIZE_MASK_82576;
1431 case e1000_vfadapt_i350:
1432 pba = E1000_READ_REG(hw, E1000_RXPBS);
1433 pba = e1000_rxpbs_adjust_82580(pba);
1438 pba = E1000_PBA_34K;
1445 /* Special needs in case of Jumbo frames */
1446 if (hw->mac.type == e1000_82575 && ifp->if_mtu > ETHERMTU) {
1447 uint32_t tx_space, min_tx, min_rx;
1449 pba = E1000_READ_REG(hw, E1000_PBA);
1450 tx_space = pba >> 16;
1453 min_tx = (sc->max_frame_size +
1454 sizeof(struct e1000_tx_desc) - ETHER_CRC_LEN) * 2;
1455 min_tx = roundup2(min_tx, 1024);
1457 min_rx = sc->max_frame_size;
1458 min_rx = roundup2(min_rx, 1024);
1460 if (tx_space < min_tx && (min_tx - tx_space) < pba) {
1461 pba = pba - (min_tx - tx_space);
1463 * if short on rx space, rx wins
1464 * and must trump tx adjustment
1469 E1000_WRITE_REG(hw, E1000_PBA, pba);
1473 * These parameters control the automatic generation (Tx) and
1474 * response (Rx) to Ethernet PAUSE frames.
1475 * - High water mark should allow for at least two frames to be
1476 * received after sending an XOFF.
1477 * - Low water mark works best when it is very near the high water mark.
1478 * This allows the receiver to restart by sending XON when it has
1481 hwm = min(((pba << 10) * 9 / 10),
1482 ((pba << 10) - 2 * sc->max_frame_size));
1484 if (hw->mac.type < e1000_82576) {
1485 fc->high_water = hwm & 0xFFF8; /* 8-byte granularity */
1486 fc->low_water = fc->high_water - 8;
1488 fc->high_water = hwm & 0xFFF0; /* 16-byte granularity */
1489 fc->low_water = fc->high_water - 16;
1491 fc->pause_time = IGB_FC_PAUSE_TIME;
1492 fc->send_xon = TRUE;
1493 fc->requested_mode = e1000_ifmedia2fc(sc->ifm_flowctrl);
1495 /* Issue a global reset */
1497 E1000_WRITE_REG(hw, E1000_WUC, 0);
1499 if (e1000_init_hw(hw) < 0)
1500 if_printf(ifp, "Hardware Initialization Failed\n");
1502 /* Setup DMA Coalescing */
1503 if (hw->mac.type > e1000_82580 && hw->mac.type != e1000_i211) {
1507 if (sc->dma_coalesce == 0) {
1511 reg = E1000_READ_REG(hw, E1000_DMACR);
1512 reg &= ~E1000_DMACR_DMAC_EN;
1513 E1000_WRITE_REG(hw, E1000_DMACR, reg);
1517 /* Set starting thresholds */
1518 E1000_WRITE_REG(hw, E1000_DMCTXTH, 0);
1519 E1000_WRITE_REG(hw, E1000_DMCRTRH, 0);
1521 hwm = 64 * pba - sc->max_frame_size / 16;
1522 if (hwm < 64 * (pba - 6))
1523 hwm = 64 * (pba - 6);
1524 reg = E1000_READ_REG(hw, E1000_FCRTC);
1525 reg &= ~E1000_FCRTC_RTH_COAL_MASK;
1526 reg |= ((hwm << E1000_FCRTC_RTH_COAL_SHIFT)
1527 & E1000_FCRTC_RTH_COAL_MASK);
1528 E1000_WRITE_REG(hw, E1000_FCRTC, reg);
1530 dmac = pba - sc->max_frame_size / 512;
1531 if (dmac < pba - 10)
1533 reg = E1000_READ_REG(hw, E1000_DMACR);
1534 reg &= ~E1000_DMACR_DMACTHR_MASK;
1535 reg = ((dmac << E1000_DMACR_DMACTHR_SHIFT)
1536 & E1000_DMACR_DMACTHR_MASK);
1537 /* Transition to L0x or L1 if available.. */
1538 reg |= (E1000_DMACR_DMAC_EN | E1000_DMACR_DMAC_LX_MASK);
1539 /* timer = value in sc->dma_coalesce in 32usec intervals */
1540 reg |= (sc->dma_coalesce >> 5);
1541 E1000_WRITE_REG(hw, E1000_DMACR, reg);
1543 /* Set the interval before transition */
1544 reg = E1000_READ_REG(hw, E1000_DMCTLX);
1546 E1000_WRITE_REG(hw, E1000_DMCTLX, reg);
1548 /* Free space in tx packet buffer to wake from DMA coal */
1549 E1000_WRITE_REG(hw, E1000_DMCTXTH,
1550 (20480 - (2 * sc->max_frame_size)) >> 6);
1552 /* Make low power state decision controlled by DMA coal */
1553 reg = E1000_READ_REG(hw, E1000_PCIEMISC);
1554 reg &= ~E1000_PCIEMISC_LX_DECISION;
1555 E1000_WRITE_REG(hw, E1000_PCIEMISC, reg);
1556 if_printf(ifp, "DMA Coalescing enabled\n");
1557 } else if (hw->mac.type == e1000_82580) {
1558 uint32_t reg = E1000_READ_REG(hw, E1000_PCIEMISC);
1560 E1000_WRITE_REG(hw, E1000_DMACR, 0);
1561 E1000_WRITE_REG(hw, E1000_PCIEMISC,
1562 reg & ~E1000_PCIEMISC_LX_DECISION);
1566 E1000_WRITE_REG(&sc->hw, E1000_VET, ETHERTYPE_VLAN);
1567 e1000_get_phy_info(hw);
1568 e1000_check_for_link(hw);
1572 igb_setup_ifp(struct igb_softc *sc)
1574 struct ifnet *ifp = &sc->arpcom.ac_if;
1578 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1579 ifp->if_init = igb_init;
1580 ifp->if_ioctl = igb_ioctl;
1581 ifp->if_start = igb_start;
1582 ifp->if_serialize = igb_serialize;
1583 ifp->if_deserialize = igb_deserialize;
1584 ifp->if_tryserialize = igb_tryserialize;
1586 ifp->if_serialize_assert = igb_serialize_assert;
1588 #ifdef IFPOLL_ENABLE
1589 ifp->if_npoll = igb_npoll;
1592 ifp->if_nmbclusters = sc->rx_ring_cnt * sc->rx_rings[0].num_rx_desc;
1594 ifq_set_maxlen(&ifp->if_snd, sc->tx_rings[0].num_tx_desc - 1);
1595 ifq_set_ready(&ifp->if_snd);
1596 ifq_set_subq_cnt(&ifp->if_snd, sc->tx_ring_cnt);
1598 ifp->if_mapsubq = ifq_mapsubq_mask;
1599 ifq_set_subq_mask(&ifp->if_snd, 0);
1601 ether_ifattach(ifp, sc->hw.mac.addr, NULL);
1603 ifp->if_capabilities =
1604 IFCAP_HWCSUM | IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_MTU | IFCAP_TSO;
1605 if (IGB_ENABLE_HWRSS(sc))
1606 ifp->if_capabilities |= IFCAP_RSS;
1607 ifp->if_capenable = ifp->if_capabilities;
1608 ifp->if_hwassist = IGB_CSUM_FEATURES | CSUM_TSO;
1611 * Tell the upper layer(s) we support long frames
1613 ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
1615 /* Setup TX rings and subqueues */
1616 for (i = 0; i < sc->tx_ring_cnt; ++i) {
1617 struct ifaltq_subque *ifsq = ifq_get_subq(&ifp->if_snd, i);
1618 struct igb_tx_ring *txr = &sc->tx_rings[i];
1620 ifsq_set_cpuid(ifsq, txr->tx_intr_cpuid);
1621 ifsq_set_priv(ifsq, txr);
1622 ifsq_set_hw_serialize(ifsq, &txr->tx_serialize);
1625 ifsq_watchdog_init(&txr->tx_watchdog, ifsq, igb_watchdog);
1629 * Specify the media types supported by this adapter and register
1630 * callbacks to update media and link information
1632 if (sc->hw.phy.media_type == e1000_media_type_fiber ||
1633 sc->hw.phy.media_type == e1000_media_type_internal_serdes) {
1634 ifmedia_add(&sc->media, IFM_ETHER | IFM_1000_SX | IFM_FDX,
1637 ifmedia_add(&sc->media, IFM_ETHER | IFM_10_T, 0, NULL);
1638 ifmedia_add(&sc->media, IFM_ETHER | IFM_10_T | IFM_FDX,
1640 ifmedia_add(&sc->media, IFM_ETHER | IFM_100_TX, 0, NULL);
1641 ifmedia_add(&sc->media, IFM_ETHER | IFM_100_TX | IFM_FDX,
1643 if (sc->hw.phy.type != e1000_phy_ife) {
1644 ifmedia_add(&sc->media,
1645 IFM_ETHER | IFM_1000_T | IFM_FDX, 0, NULL);
1648 ifmedia_add(&sc->media, IFM_ETHER | IFM_AUTO, 0, NULL);
1649 ifmedia_set(&sc->media, IFM_ETHER | IFM_AUTO | sc->ifm_flowctrl);
1653 igb_add_sysctl(struct igb_softc *sc)
1655 struct sysctl_ctx_list *ctx;
1656 struct sysctl_oid *tree;
1660 ctx = device_get_sysctl_ctx(sc->dev);
1661 tree = device_get_sysctl_tree(sc->dev);
1662 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree),
1663 OID_AUTO, "rxr", CTLFLAG_RD, &sc->rx_ring_cnt, 0, "# of RX rings");
1664 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree),
1665 OID_AUTO, "rxr_inuse", CTLFLAG_RD, &sc->rx_ring_inuse, 0,
1666 "# of RX rings used");
1667 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree),
1668 OID_AUTO, "txr", CTLFLAG_RD, &sc->tx_ring_cnt, 0, "# of TX rings");
1669 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree),
1670 OID_AUTO, "txr_inuse", CTLFLAG_RD, &sc->tx_ring_inuse, 0,
1671 "# of TX rings used");
1672 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree),
1673 OID_AUTO, "rxd", CTLFLAG_RD, &sc->rx_rings[0].num_rx_desc, 0,
1675 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree),
1676 OID_AUTO, "txd", CTLFLAG_RD, &sc->tx_rings[0].num_tx_desc, 0,
1679 if (sc->intr_type != PCI_INTR_TYPE_MSIX) {
1680 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree),
1681 OID_AUTO, "intr_rate", CTLTYPE_INT | CTLFLAG_RW,
1682 sc, 0, igb_sysctl_intr_rate, "I", "interrupt rate");
1684 for (i = 0; i < sc->msix_cnt; ++i) {
1685 struct igb_msix_data *msix = &sc->msix_data[i];
1687 ksnprintf(node, sizeof(node), "msix%d_rate", i);
1688 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree),
1689 OID_AUTO, node, CTLTYPE_INT | CTLFLAG_RW,
1690 msix, 0, igb_sysctl_msix_rate, "I",
1691 msix->msix_rate_desc);
1695 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree),
1696 OID_AUTO, "tx_intr_nsegs", CTLTYPE_INT | CTLFLAG_RW,
1697 sc, 0, igb_sysctl_tx_intr_nsegs, "I",
1698 "# of segments per TX interrupt");
1700 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree),
1701 OID_AUTO, "tx_wreg_nsegs", CTLTYPE_INT | CTLFLAG_RW,
1702 sc, 0, igb_sysctl_tx_wreg_nsegs, "I",
1703 "# of segments sent before write to hardware register");
1705 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree),
1706 OID_AUTO, "rx_wreg_nsegs", CTLTYPE_INT | CTLFLAG_RW,
1707 sc, 0, igb_sysctl_rx_wreg_nsegs, "I",
1708 "# of segments received before write to hardware register");
1710 #ifdef IFPOLL_ENABLE
1711 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree),
1712 OID_AUTO, "npoll_rxoff", CTLTYPE_INT|CTLFLAG_RW,
1713 sc, 0, igb_sysctl_npoll_rxoff, "I", "NPOLLING RX cpu offset");
1714 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree),
1715 OID_AUTO, "npoll_txoff", CTLTYPE_INT|CTLFLAG_RW,
1716 sc, 0, igb_sysctl_npoll_txoff, "I", "NPOLLING TX cpu offset");
1719 #ifdef IGB_RSS_DEBUG
1720 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree),
1721 OID_AUTO, "rss_debug", CTLFLAG_RW, &sc->rss_debug, 0,
1723 for (i = 0; i < sc->rx_ring_cnt; ++i) {
1724 ksnprintf(node, sizeof(node), "rx%d_pkt", i);
1725 SYSCTL_ADD_ULONG(ctx,
1726 SYSCTL_CHILDREN(tree), OID_AUTO, node,
1727 CTLFLAG_RW, &sc->rx_rings[i].rx_packets, "RXed packets");
1730 #ifdef IGB_TSS_DEBUG
1731 for (i = 0; i < sc->tx_ring_cnt; ++i) {
1732 ksnprintf(node, sizeof(node), "tx%d_pkt", i);
1733 SYSCTL_ADD_ULONG(ctx,
1734 SYSCTL_CHILDREN(tree), OID_AUTO, node,
1735 CTLFLAG_RW, &sc->tx_rings[i].tx_packets, "TXed packets");
1741 igb_alloc_rings(struct igb_softc *sc)
1746 * Create top level busdma tag
1748 error = bus_dma_tag_create(NULL, 1, 0,
1749 BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
1750 BUS_SPACE_MAXSIZE_32BIT, 0, BUS_SPACE_MAXSIZE_32BIT, 0,
1753 device_printf(sc->dev, "could not create top level DMA tag\n");
1758 * Allocate TX descriptor rings and buffers
1760 sc->tx_rings = kmalloc_cachealign(
1761 sizeof(struct igb_tx_ring) * sc->tx_ring_cnt,
1762 M_DEVBUF, M_WAITOK | M_ZERO);
1763 for (i = 0; i < sc->tx_ring_cnt; ++i) {
1764 struct igb_tx_ring *txr = &sc->tx_rings[i];
1766 /* Set up some basics */
1769 lwkt_serialize_init(&txr->tx_serialize);
1771 error = igb_create_tx_ring(txr);
1777 * Allocate RX descriptor rings and buffers
1779 sc->rx_rings = kmalloc_cachealign(
1780 sizeof(struct igb_rx_ring) * sc->rx_ring_cnt,
1781 M_DEVBUF, M_WAITOK | M_ZERO);
1782 for (i = 0; i < sc->rx_ring_cnt; ++i) {
1783 struct igb_rx_ring *rxr = &sc->rx_rings[i];
1785 /* Set up some basics */
1788 lwkt_serialize_init(&rxr->rx_serialize);
1790 error = igb_create_rx_ring(rxr);
1799 igb_free_rings(struct igb_softc *sc)
1803 if (sc->tx_rings != NULL) {
1804 for (i = 0; i < sc->tx_ring_cnt; ++i) {
1805 struct igb_tx_ring *txr = &sc->tx_rings[i];
1807 igb_destroy_tx_ring(txr, txr->num_tx_desc);
1809 kfree(sc->tx_rings, M_DEVBUF);
1812 if (sc->rx_rings != NULL) {
1813 for (i = 0; i < sc->rx_ring_cnt; ++i) {
1814 struct igb_rx_ring *rxr = &sc->rx_rings[i];
1816 igb_destroy_rx_ring(rxr, rxr->num_rx_desc);
1818 kfree(sc->rx_rings, M_DEVBUF);
1823 igb_create_tx_ring(struct igb_tx_ring *txr)
1825 int tsize, error, i, ntxd;
1828 * Validate number of transmit descriptors. It must not exceed
1829 * hardware maximum, and must be multiple of IGB_DBA_ALIGN.
1831 ntxd = device_getenv_int(txr->sc->dev, "txd", igb_txd);
1832 if ((ntxd * sizeof(struct e1000_tx_desc)) % IGB_DBA_ALIGN != 0 ||
1833 ntxd > IGB_MAX_TXD || ntxd < IGB_MIN_TXD) {
1834 device_printf(txr->sc->dev,
1835 "Using %d TX descriptors instead of %d!\n",
1836 IGB_DEFAULT_TXD, ntxd);
1837 txr->num_tx_desc = IGB_DEFAULT_TXD;
1839 txr->num_tx_desc = ntxd;
1843 * Allocate TX descriptor ring
1845 tsize = roundup2(txr->num_tx_desc * sizeof(union e1000_adv_tx_desc),
1847 txr->txdma.dma_vaddr = bus_dmamem_coherent_any(txr->sc->parent_tag,
1848 IGB_DBA_ALIGN, tsize, BUS_DMA_WAITOK,
1849 &txr->txdma.dma_tag, &txr->txdma.dma_map, &txr->txdma.dma_paddr);
1850 if (txr->txdma.dma_vaddr == NULL) {
1851 device_printf(txr->sc->dev,
1852 "Unable to allocate TX Descriptor memory\n");
1855 txr->tx_base = txr->txdma.dma_vaddr;
1856 bzero(txr->tx_base, tsize);
1858 tsize = __VM_CACHELINE_ALIGN(
1859 sizeof(struct igb_tx_buf) * txr->num_tx_desc);
1860 txr->tx_buf = kmalloc_cachealign(tsize, M_DEVBUF, M_WAITOK | M_ZERO);
1863 * Allocate TX head write-back buffer
1865 txr->tx_hdr = bus_dmamem_coherent_any(txr->sc->parent_tag,
1866 __VM_CACHELINE_SIZE, __VM_CACHELINE_SIZE, BUS_DMA_WAITOK,
1867 &txr->tx_hdr_dtag, &txr->tx_hdr_dmap, &txr->tx_hdr_paddr);
1868 if (txr->tx_hdr == NULL) {
1869 device_printf(txr->sc->dev,
1870 "Unable to allocate TX head write-back buffer\n");
1875 * Create DMA tag for TX buffers
1877 error = bus_dma_tag_create(txr->sc->parent_tag,
1878 1, 0, /* alignment, bounds */
1879 BUS_SPACE_MAXADDR, /* lowaddr */
1880 BUS_SPACE_MAXADDR, /* highaddr */
1881 NULL, NULL, /* filter, filterarg */
1882 IGB_TSO_SIZE, /* maxsize */
1883 IGB_MAX_SCATTER, /* nsegments */
1884 PAGE_SIZE, /* maxsegsize */
1885 BUS_DMA_WAITOK | BUS_DMA_ALLOCNOW |
1886 BUS_DMA_ONEBPAGE, /* flags */
1889 device_printf(txr->sc->dev, "Unable to allocate TX DMA tag\n");
1890 kfree(txr->tx_buf, M_DEVBUF);
1896 * Create DMA maps for TX buffers
1898 for (i = 0; i < txr->num_tx_desc; ++i) {
1899 struct igb_tx_buf *txbuf = &txr->tx_buf[i];
1901 error = bus_dmamap_create(txr->tx_tag,
1902 BUS_DMA_WAITOK | BUS_DMA_ONEBPAGE, &txbuf->map);
1904 device_printf(txr->sc->dev,
1905 "Unable to create TX DMA map\n");
1906 igb_destroy_tx_ring(txr, i);
1911 if (txr->sc->hw.mac.type == e1000_82575)
1912 txr->tx_flags |= IGB_TXFLAG_TSO_IPLEN0;
1915 * Initialize various watermark
1917 txr->spare_desc = IGB_TX_SPARE;
1918 txr->intr_nsegs = txr->num_tx_desc / 16;
1919 txr->wreg_nsegs = IGB_DEF_TXWREG_NSEGS;
1920 txr->oact_hi_desc = txr->num_tx_desc / 2;
1921 txr->oact_lo_desc = txr->num_tx_desc / 8;
1922 if (txr->oact_lo_desc > IGB_TX_OACTIVE_MAX)
1923 txr->oact_lo_desc = IGB_TX_OACTIVE_MAX;
1924 if (txr->oact_lo_desc < txr->spare_desc + IGB_TX_RESERVED)
1925 txr->oact_lo_desc = txr->spare_desc + IGB_TX_RESERVED;
1931 igb_free_tx_ring(struct igb_tx_ring *txr)
1935 for (i = 0; i < txr->num_tx_desc; ++i) {
1936 struct igb_tx_buf *txbuf = &txr->tx_buf[i];
1938 if (txbuf->m_head != NULL) {
1939 bus_dmamap_unload(txr->tx_tag, txbuf->map);
1940 m_freem(txbuf->m_head);
1941 txbuf->m_head = NULL;
1947 igb_destroy_tx_ring(struct igb_tx_ring *txr, int ndesc)
1951 if (txr->txdma.dma_vaddr != NULL) {
1952 bus_dmamap_unload(txr->txdma.dma_tag, txr->txdma.dma_map);
1953 bus_dmamem_free(txr->txdma.dma_tag, txr->txdma.dma_vaddr,
1954 txr->txdma.dma_map);
1955 bus_dma_tag_destroy(txr->txdma.dma_tag);
1956 txr->txdma.dma_vaddr = NULL;
1959 if (txr->tx_hdr != NULL) {
1960 bus_dmamap_unload(txr->tx_hdr_dtag, txr->tx_hdr_dmap);
1961 bus_dmamem_free(txr->tx_hdr_dtag, txr->tx_hdr,
1963 bus_dma_tag_destroy(txr->tx_hdr_dtag);
1967 if (txr->tx_buf == NULL)
1970 for (i = 0; i < ndesc; ++i) {
1971 struct igb_tx_buf *txbuf = &txr->tx_buf[i];
1973 KKASSERT(txbuf->m_head == NULL);
1974 bus_dmamap_destroy(txr->tx_tag, txbuf->map);
1976 bus_dma_tag_destroy(txr->tx_tag);
1978 kfree(txr->tx_buf, M_DEVBUF);
1983 igb_init_tx_ring(struct igb_tx_ring *txr)
1985 /* Clear the old descriptor contents */
1987 sizeof(union e1000_adv_tx_desc) * txr->num_tx_desc);
1989 /* Clear TX head write-back buffer */
1993 txr->next_avail_desc = 0;
1994 txr->next_to_clean = 0;
1997 /* Set number of descriptors available */
1998 txr->tx_avail = txr->num_tx_desc;
2000 /* Enable this TX ring */
2001 txr->tx_flags |= IGB_TXFLAG_ENABLED;
2005 igb_init_tx_unit(struct igb_softc *sc)
2007 struct e1000_hw *hw = &sc->hw;
2011 /* Setup the Tx Descriptor Rings */
2012 for (i = 0; i < sc->tx_ring_inuse; ++i) {
2013 struct igb_tx_ring *txr = &sc->tx_rings[i];
2014 uint64_t bus_addr = txr->txdma.dma_paddr;
2015 uint64_t hdr_paddr = txr->tx_hdr_paddr;
2016 uint32_t txdctl = 0;
2017 uint32_t dca_txctrl;
2019 E1000_WRITE_REG(hw, E1000_TDLEN(i),
2020 txr->num_tx_desc * sizeof(struct e1000_tx_desc));
2021 E1000_WRITE_REG(hw, E1000_TDBAH(i),
2022 (uint32_t)(bus_addr >> 32));
2023 E1000_WRITE_REG(hw, E1000_TDBAL(i),
2024 (uint32_t)bus_addr);
2026 /* Setup the HW Tx Head and Tail descriptor pointers */
2027 E1000_WRITE_REG(hw, E1000_TDT(i), 0);
2028 E1000_WRITE_REG(hw, E1000_TDH(i), 0);
2030 dca_txctrl = E1000_READ_REG(hw, E1000_DCA_TXCTRL(i));
2031 dca_txctrl &= ~E1000_DCA_TXCTRL_TX_WB_RO_EN;
2032 E1000_WRITE_REG(hw, E1000_DCA_TXCTRL(i), dca_txctrl);
2035 * Don't set WB_on_EITR:
2036 * - 82575 does not have it
2037 * - It almost has no effect on 82576, see:
2038 * 82576 specification update errata #26
2039 * - It causes unnecessary bus traffic
2041 E1000_WRITE_REG(hw, E1000_TDWBAH(i),
2042 (uint32_t)(hdr_paddr >> 32));
2043 E1000_WRITE_REG(hw, E1000_TDWBAL(i),
2044 ((uint32_t)hdr_paddr) | E1000_TX_HEAD_WB_ENABLE);
2047 * WTHRESH is ignored by the hardware, since header
2048 * write back mode is used.
2050 txdctl |= IGB_TX_PTHRESH;
2051 txdctl |= IGB_TX_HTHRESH << 8;
2052 txdctl |= IGB_TX_WTHRESH << 16;
2053 txdctl |= E1000_TXDCTL_QUEUE_ENABLE;
2054 E1000_WRITE_REG(hw, E1000_TXDCTL(i), txdctl);
2060 e1000_config_collision_dist(hw);
2062 /* Program the Transmit Control Register */
2063 tctl = E1000_READ_REG(hw, E1000_TCTL);
2064 tctl &= ~E1000_TCTL_CT;
2065 tctl |= (E1000_TCTL_PSP | E1000_TCTL_RTLC | E1000_TCTL_EN |
2066 (E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT));
2068 /* This write will effectively turn on the transmit unit. */
2069 E1000_WRITE_REG(hw, E1000_TCTL, tctl);
2073 igb_txcsum_ctx(struct igb_tx_ring *txr, struct mbuf *mp)
2075 struct e1000_adv_tx_context_desc *TXD;
2076 uint32_t vlan_macip_lens, type_tucmd_mlhl, mss_l4len_idx;
2077 int ehdrlen, ctxd, ip_hlen = 0;
2078 boolean_t offload = TRUE;
2080 if ((mp->m_pkthdr.csum_flags & IGB_CSUM_FEATURES) == 0)
2083 vlan_macip_lens = type_tucmd_mlhl = mss_l4len_idx = 0;
2085 ctxd = txr->next_avail_desc;
2086 TXD = (struct e1000_adv_tx_context_desc *)&txr->tx_base[ctxd];
2089 * In advanced descriptors the vlan tag must
2090 * be placed into the context descriptor, thus
2091 * we need to be here just for that setup.
2093 if (mp->m_flags & M_VLANTAG) {
2096 vlantag = htole16(mp->m_pkthdr.ether_vlantag);
2097 vlan_macip_lens |= (vlantag << E1000_ADVTXD_VLAN_SHIFT);
2098 } else if (!offload) {
2102 ehdrlen = mp->m_pkthdr.csum_lhlen;
2103 KASSERT(ehdrlen > 0, ("invalid ether hlen"));
2105 /* Set the ether header length */
2106 vlan_macip_lens |= ehdrlen << E1000_ADVTXD_MACLEN_SHIFT;
2107 if (mp->m_pkthdr.csum_flags & CSUM_IP) {
2108 type_tucmd_mlhl |= E1000_ADVTXD_TUCMD_IPV4;
2109 ip_hlen = mp->m_pkthdr.csum_iphlen;
2110 KASSERT(ip_hlen > 0, ("invalid ip hlen"));
2112 vlan_macip_lens |= ip_hlen;
2114 type_tucmd_mlhl |= E1000_ADVTXD_DCMD_DEXT | E1000_ADVTXD_DTYP_CTXT;
2115 if (mp->m_pkthdr.csum_flags & CSUM_TCP)
2116 type_tucmd_mlhl |= E1000_ADVTXD_TUCMD_L4T_TCP;
2117 else if (mp->m_pkthdr.csum_flags & CSUM_UDP)
2118 type_tucmd_mlhl |= E1000_ADVTXD_TUCMD_L4T_UDP;
2121 * 82575 needs the TX context index added; the queue
2122 * index is used as TX context index here.
2124 if (txr->sc->hw.mac.type == e1000_82575)
2125 mss_l4len_idx = txr->me << 4;
2127 /* Now copy bits into descriptor */
2128 TXD->vlan_macip_lens = htole32(vlan_macip_lens);
2129 TXD->type_tucmd_mlhl = htole32(type_tucmd_mlhl);
2130 TXD->seqnum_seed = htole32(0);
2131 TXD->mss_l4len_idx = htole32(mss_l4len_idx);
2133 /* We've consumed the first desc, adjust counters */
2134 if (++ctxd == txr->num_tx_desc)
2136 txr->next_avail_desc = ctxd;
2143 igb_txeof(struct igb_tx_ring *txr)
2145 int first, hdr, avail;
2147 if (txr->tx_avail == txr->num_tx_desc)
2150 first = txr->next_to_clean;
2151 hdr = *(txr->tx_hdr);
2156 avail = txr->tx_avail;
2157 while (first != hdr) {
2158 struct igb_tx_buf *txbuf = &txr->tx_buf[first];
2161 if (txbuf->m_head) {
2162 bus_dmamap_unload(txr->tx_tag, txbuf->map);
2163 m_freem(txbuf->m_head);
2164 txbuf->m_head = NULL;
2166 if (++first == txr->num_tx_desc)
2169 txr->next_to_clean = first;
2170 txr->tx_avail = avail;
2173 * If we have a minimum free, clear OACTIVE
2174 * to tell the stack that it is OK to send packets.
2176 if (IGB_IS_NOT_OACTIVE(txr)) {
2177 ifsq_clr_oactive(txr->ifsq);
2180 * We have enough TX descriptors, turn off
2181 * the watchdog. We allow small amount of
2182 * packets (roughly intr_nsegs) pending on
2183 * the transmit ring.
2185 txr->tx_watchdog.wd_timer = 0;
2190 igb_create_rx_ring(struct igb_rx_ring *rxr)
2192 int rsize, i, error, nrxd;
2195 * Validate number of receive descriptors. It must not exceed
2196 * hardware maximum, and must be multiple of IGB_DBA_ALIGN.
2198 nrxd = device_getenv_int(rxr->sc->dev, "rxd", igb_rxd);
2199 if ((nrxd * sizeof(struct e1000_rx_desc)) % IGB_DBA_ALIGN != 0 ||
2200 nrxd > IGB_MAX_RXD || nrxd < IGB_MIN_RXD) {
2201 device_printf(rxr->sc->dev,
2202 "Using %d RX descriptors instead of %d!\n",
2203 IGB_DEFAULT_RXD, nrxd);
2204 rxr->num_rx_desc = IGB_DEFAULT_RXD;
2206 rxr->num_rx_desc = nrxd;
2210 * Allocate RX descriptor ring
2212 rsize = roundup2(rxr->num_rx_desc * sizeof(union e1000_adv_rx_desc),
2214 rxr->rxdma.dma_vaddr = bus_dmamem_coherent_any(rxr->sc->parent_tag,
2215 IGB_DBA_ALIGN, rsize, BUS_DMA_WAITOK,
2216 &rxr->rxdma.dma_tag, &rxr->rxdma.dma_map,
2217 &rxr->rxdma.dma_paddr);
2218 if (rxr->rxdma.dma_vaddr == NULL) {
2219 device_printf(rxr->sc->dev,
2220 "Unable to allocate RxDescriptor memory\n");
2223 rxr->rx_base = rxr->rxdma.dma_vaddr;
2224 bzero(rxr->rx_base, rsize);
2226 rsize = __VM_CACHELINE_ALIGN(
2227 sizeof(struct igb_rx_buf) * rxr->num_rx_desc);
2228 rxr->rx_buf = kmalloc_cachealign(rsize, M_DEVBUF, M_WAITOK | M_ZERO);
2231 * Create DMA tag for RX buffers
2233 error = bus_dma_tag_create(rxr->sc->parent_tag,
2234 1, 0, /* alignment, bounds */
2235 BUS_SPACE_MAXADDR, /* lowaddr */
2236 BUS_SPACE_MAXADDR, /* highaddr */
2237 NULL, NULL, /* filter, filterarg */
2238 MCLBYTES, /* maxsize */
2240 MCLBYTES, /* maxsegsize */
2241 BUS_DMA_WAITOK | BUS_DMA_ALLOCNOW, /* flags */
2244 device_printf(rxr->sc->dev,
2245 "Unable to create RX payload DMA tag\n");
2246 kfree(rxr->rx_buf, M_DEVBUF);
2252 * Create spare DMA map for RX buffers
2254 error = bus_dmamap_create(rxr->rx_tag, BUS_DMA_WAITOK,
2257 device_printf(rxr->sc->dev,
2258 "Unable to create spare RX DMA maps\n");
2259 bus_dma_tag_destroy(rxr->rx_tag);
2260 kfree(rxr->rx_buf, M_DEVBUF);
2266 * Create DMA maps for RX buffers
2268 for (i = 0; i < rxr->num_rx_desc; i++) {
2269 struct igb_rx_buf *rxbuf = &rxr->rx_buf[i];
2271 error = bus_dmamap_create(rxr->rx_tag,
2272 BUS_DMA_WAITOK, &rxbuf->map);
2274 device_printf(rxr->sc->dev,
2275 "Unable to create RX DMA maps\n");
2276 igb_destroy_rx_ring(rxr, i);
2282 * Initialize various watermark
2284 rxr->wreg_nsegs = IGB_DEF_RXWREG_NSEGS;
2290 igb_free_rx_ring(struct igb_rx_ring *rxr)
2294 for (i = 0; i < rxr->num_rx_desc; ++i) {
2295 struct igb_rx_buf *rxbuf = &rxr->rx_buf[i];
2297 if (rxbuf->m_head != NULL) {
2298 bus_dmamap_unload(rxr->rx_tag, rxbuf->map);
2299 m_freem(rxbuf->m_head);
2300 rxbuf->m_head = NULL;
2304 if (rxr->fmp != NULL)
2311 igb_destroy_rx_ring(struct igb_rx_ring *rxr, int ndesc)
2315 if (rxr->rxdma.dma_vaddr != NULL) {
2316 bus_dmamap_unload(rxr->rxdma.dma_tag, rxr->rxdma.dma_map);
2317 bus_dmamem_free(rxr->rxdma.dma_tag, rxr->rxdma.dma_vaddr,
2318 rxr->rxdma.dma_map);
2319 bus_dma_tag_destroy(rxr->rxdma.dma_tag);
2320 rxr->rxdma.dma_vaddr = NULL;
2323 if (rxr->rx_buf == NULL)
2326 for (i = 0; i < ndesc; ++i) {
2327 struct igb_rx_buf *rxbuf = &rxr->rx_buf[i];
2329 KKASSERT(rxbuf->m_head == NULL);
2330 bus_dmamap_destroy(rxr->rx_tag, rxbuf->map);
2332 bus_dmamap_destroy(rxr->rx_tag, rxr->rx_sparemap);
2333 bus_dma_tag_destroy(rxr->rx_tag);
2335 kfree(rxr->rx_buf, M_DEVBUF);
2340 igb_setup_rxdesc(union e1000_adv_rx_desc *rxd, const struct igb_rx_buf *rxbuf)
2342 rxd->read.pkt_addr = htole64(rxbuf->paddr);
2343 rxd->wb.upper.status_error = 0;
2347 igb_newbuf(struct igb_rx_ring *rxr, int i, boolean_t wait)
2350 bus_dma_segment_t seg;
2352 struct igb_rx_buf *rxbuf;
2355 m = m_getcl(wait ? M_WAITOK : M_NOWAIT, MT_DATA, M_PKTHDR);
2358 if_printf(&rxr->sc->arpcom.ac_if,
2359 "Unable to allocate RX mbuf\n");
2363 m->m_len = m->m_pkthdr.len = MCLBYTES;
2365 if (rxr->sc->max_frame_size <= MCLBYTES - ETHER_ALIGN)
2366 m_adj(m, ETHER_ALIGN);
2368 error = bus_dmamap_load_mbuf_segment(rxr->rx_tag,
2369 rxr->rx_sparemap, m, &seg, 1, &nseg, BUS_DMA_NOWAIT);
2373 if_printf(&rxr->sc->arpcom.ac_if,
2374 "Unable to load RX mbuf\n");
2379 rxbuf = &rxr->rx_buf[i];
2380 if (rxbuf->m_head != NULL)
2381 bus_dmamap_unload(rxr->rx_tag, rxbuf->map);
2384 rxbuf->map = rxr->rx_sparemap;
2385 rxr->rx_sparemap = map;
2388 rxbuf->paddr = seg.ds_addr;
2390 igb_setup_rxdesc(&rxr->rx_base[i], rxbuf);
2395 igb_init_rx_ring(struct igb_rx_ring *rxr)
2399 /* Clear the ring contents */
2401 rxr->num_rx_desc * sizeof(union e1000_adv_rx_desc));
2403 /* Now replenish the ring mbufs */
2404 for (i = 0; i < rxr->num_rx_desc; ++i) {
2407 error = igb_newbuf(rxr, i, TRUE);
2412 /* Setup our descriptor indices */
2413 rxr->next_to_check = 0;
2417 rxr->discard = FALSE;
2423 igb_init_rx_unit(struct igb_softc *sc)
2425 struct ifnet *ifp = &sc->arpcom.ac_if;
2426 struct e1000_hw *hw = &sc->hw;
2427 uint32_t rctl, rxcsum, srrctl = 0;
2431 * Make sure receives are disabled while setting
2432 * up the descriptor ring
2434 rctl = E1000_READ_REG(hw, E1000_RCTL);
2435 E1000_WRITE_REG(hw, E1000_RCTL, rctl & ~E1000_RCTL_EN);
2439 ** Set up for header split
2441 if (igb_header_split) {
2442 /* Use a standard mbuf for the header */
2443 srrctl |= IGB_HDR_BUF << E1000_SRRCTL_BSIZEHDRSIZE_SHIFT;
2444 srrctl |= E1000_SRRCTL_DESCTYPE_HDR_SPLIT_ALWAYS;
2447 srrctl |= E1000_SRRCTL_DESCTYPE_ADV_ONEBUF;
2450 ** Set up for jumbo frames
2452 if (ifp->if_mtu > ETHERMTU) {
2453 rctl |= E1000_RCTL_LPE;
2455 if (adapter->rx_mbuf_sz == MJUMPAGESIZE) {
2456 srrctl |= 4096 >> E1000_SRRCTL_BSIZEPKT_SHIFT;
2457 rctl |= E1000_RCTL_SZ_4096 | E1000_RCTL_BSEX;
2458 } else if (adapter->rx_mbuf_sz > MJUMPAGESIZE) {
2459 srrctl |= 8192 >> E1000_SRRCTL_BSIZEPKT_SHIFT;
2460 rctl |= E1000_RCTL_SZ_8192 | E1000_RCTL_BSEX;
2462 /* Set maximum packet len */
2463 psize = adapter->max_frame_size;
2464 /* are we on a vlan? */
2465 if (adapter->ifp->if_vlantrunk != NULL)
2466 psize += VLAN_TAG_SIZE;
2467 E1000_WRITE_REG(&adapter->hw, E1000_RLPML, psize);
2469 srrctl |= 2048 >> E1000_SRRCTL_BSIZEPKT_SHIFT;
2470 rctl |= E1000_RCTL_SZ_2048;
2473 rctl &= ~E1000_RCTL_LPE;
2474 srrctl |= 2048 >> E1000_SRRCTL_BSIZEPKT_SHIFT;
2475 rctl |= E1000_RCTL_SZ_2048;
2478 /* Setup the Base and Length of the Rx Descriptor Rings */
2479 for (i = 0; i < sc->rx_ring_inuse; ++i) {
2480 struct igb_rx_ring *rxr = &sc->rx_rings[i];
2481 uint64_t bus_addr = rxr->rxdma.dma_paddr;
2484 E1000_WRITE_REG(hw, E1000_RDLEN(i),
2485 rxr->num_rx_desc * sizeof(struct e1000_rx_desc));
2486 E1000_WRITE_REG(hw, E1000_RDBAH(i),
2487 (uint32_t)(bus_addr >> 32));
2488 E1000_WRITE_REG(hw, E1000_RDBAL(i),
2489 (uint32_t)bus_addr);
2490 E1000_WRITE_REG(hw, E1000_SRRCTL(i), srrctl);
2491 /* Enable this Queue */
2492 rxdctl = E1000_READ_REG(hw, E1000_RXDCTL(i));
2493 rxdctl |= E1000_RXDCTL_QUEUE_ENABLE;
2494 rxdctl &= 0xFFF00000;
2495 rxdctl |= IGB_RX_PTHRESH;
2496 rxdctl |= IGB_RX_HTHRESH << 8;
2498 * Don't set WTHRESH to a value above 1 on 82576, see:
2499 * 82576 specification update errata #26
2501 rxdctl |= IGB_RX_WTHRESH << 16;
2502 E1000_WRITE_REG(hw, E1000_RXDCTL(i), rxdctl);
2505 rxcsum = E1000_READ_REG(&sc->hw, E1000_RXCSUM);
2506 rxcsum &= ~(E1000_RXCSUM_PCSS_MASK | E1000_RXCSUM_IPPCSE);
2509 * Receive Checksum Offload for TCP and UDP
2511 * Checksum offloading is also enabled if multiple receive
2512 * queue is to be supported, since we need it to figure out
2515 if ((ifp->if_capenable & IFCAP_RXCSUM) || IGB_ENABLE_HWRSS(sc)) {
2518 * PCSD must be enabled to enable multiple
2521 rxcsum |= E1000_RXCSUM_IPOFL | E1000_RXCSUM_TUOFL |
2524 rxcsum &= ~(E1000_RXCSUM_IPOFL | E1000_RXCSUM_TUOFL |
2527 E1000_WRITE_REG(&sc->hw, E1000_RXCSUM, rxcsum);
2529 if (IGB_ENABLE_HWRSS(sc)) {
2530 uint8_t key[IGB_NRSSRK * IGB_RSSRK_SIZE];
2531 uint32_t reta_shift;
2536 * When we reach here, RSS has already been disabled
2537 * in igb_stop(), so we could safely configure RSS key
2538 * and redirect table.
2544 toeplitz_get_key(key, sizeof(key));
2545 for (i = 0; i < IGB_NRSSRK; ++i) {
2548 rssrk = IGB_RSSRK_VAL(key, i);
2549 IGB_RSS_DPRINTF(sc, 1, "rssrk%d 0x%08x\n", i, rssrk);
2551 E1000_WRITE_REG(hw, E1000_RSSRK(i), rssrk);
2555 * Configure RSS redirect table in following fashion:
2556 * (hash & ring_cnt_mask) == rdr_table[(hash & rdr_table_mask)]
2558 reta_shift = IGB_RETA_SHIFT;
2559 if (hw->mac.type == e1000_82575)
2560 reta_shift = IGB_RETA_SHIFT_82575;
2563 for (j = 0; j < IGB_NRETA; ++j) {
2566 for (i = 0; i < IGB_RETA_SIZE; ++i) {
2569 q = (r % sc->rx_ring_inuse) << reta_shift;
2570 reta |= q << (8 * i);
2573 IGB_RSS_DPRINTF(sc, 1, "reta 0x%08x\n", reta);
2574 E1000_WRITE_REG(hw, E1000_RETA(j), reta);
2578 * Enable multiple receive queues.
2579 * Enable IPv4 RSS standard hash functions.
2580 * Disable RSS interrupt on 82575
2582 E1000_WRITE_REG(&sc->hw, E1000_MRQC,
2583 E1000_MRQC_ENABLE_RSS_4Q |
2584 E1000_MRQC_RSS_FIELD_IPV4_TCP |
2585 E1000_MRQC_RSS_FIELD_IPV4);
2588 /* Setup the Receive Control Register */
2589 rctl &= ~(3 << E1000_RCTL_MO_SHIFT);
2590 rctl |= E1000_RCTL_EN | E1000_RCTL_BAM | E1000_RCTL_LBM_NO |
2591 E1000_RCTL_RDMTS_HALF |
2592 (hw->mac.mc_filter_type << E1000_RCTL_MO_SHIFT);
2593 /* Strip CRC bytes. */
2594 rctl |= E1000_RCTL_SECRC;
2595 /* Make sure VLAN Filters are off */
2596 rctl &= ~E1000_RCTL_VFE;
2597 /* Don't store bad packets */
2598 rctl &= ~E1000_RCTL_SBP;
2600 /* Enable Receives */
2601 E1000_WRITE_REG(hw, E1000_RCTL, rctl);
2604 * Setup the HW Rx Head and Tail Descriptor Pointers
2605 * - needs to be after enable
2607 for (i = 0; i < sc->rx_ring_inuse; ++i) {
2608 struct igb_rx_ring *rxr = &sc->rx_rings[i];
2610 E1000_WRITE_REG(hw, E1000_RDH(i), rxr->next_to_check);
2611 E1000_WRITE_REG(hw, E1000_RDT(i), rxr->num_rx_desc - 1);
2616 igb_rx_refresh(struct igb_rx_ring *rxr, int i)
2619 i = rxr->num_rx_desc - 1;
2620 E1000_WRITE_REG(&rxr->sc->hw, E1000_RDT(rxr->me), i);
2624 igb_rxeof(struct igb_rx_ring *rxr, int count)
2626 struct ifnet *ifp = &rxr->sc->arpcom.ac_if;
2627 union e1000_adv_rx_desc *cur;
2629 int i, ncoll = 0, cpuid = mycpuid;
2631 i = rxr->next_to_check;
2632 cur = &rxr->rx_base[i];
2633 staterr = le32toh(cur->wb.upper.status_error);
2635 if ((staterr & E1000_RXD_STAT_DD) == 0)
2638 while ((staterr & E1000_RXD_STAT_DD) && count != 0) {
2639 struct pktinfo *pi = NULL, pi0;
2640 struct igb_rx_buf *rxbuf = &rxr->rx_buf[i];
2641 struct mbuf *m = NULL;
2644 eop = (staterr & E1000_RXD_STAT_EOP) ? TRUE : FALSE;
2649 if ((staterr & E1000_RXDEXT_ERR_FRAME_ERR_MASK) == 0 &&
2651 struct mbuf *mp = rxbuf->m_head;
2652 uint32_t hash, hashtype;
2656 len = le16toh(cur->wb.upper.length);
2657 if ((rxr->sc->hw.mac.type == e1000_i350 ||
2658 rxr->sc->hw.mac.type == e1000_i354) &&
2659 (staterr & E1000_RXDEXT_STATERR_LB))
2660 vlan = be16toh(cur->wb.upper.vlan);
2662 vlan = le16toh(cur->wb.upper.vlan);
2664 hash = le32toh(cur->wb.lower.hi_dword.rss);
2665 hashtype = le32toh(cur->wb.lower.lo_dword.data) &
2666 E1000_RXDADV_RSSTYPE_MASK;
2668 IGB_RSS_DPRINTF(rxr->sc, 10,
2669 "ring%d, hash 0x%08x, hashtype %u\n",
2670 rxr->me, hash, hashtype);
2672 bus_dmamap_sync(rxr->rx_tag, rxbuf->map,
2673 BUS_DMASYNC_POSTREAD);
2675 if (igb_newbuf(rxr, i, FALSE) != 0) {
2676 IFNET_STAT_INC(ifp, iqdrops, 1);
2681 if (rxr->fmp == NULL) {
2682 mp->m_pkthdr.len = len;
2686 rxr->lmp->m_next = mp;
2687 rxr->lmp = rxr->lmp->m_next;
2688 rxr->fmp->m_pkthdr.len += len;
2696 m->m_pkthdr.rcvif = ifp;
2697 IFNET_STAT_INC(ifp, ipackets, 1);
2699 if (ifp->if_capenable & IFCAP_RXCSUM)
2700 igb_rxcsum(staterr, m);
2702 if (staterr & E1000_RXD_STAT_VP) {
2703 m->m_pkthdr.ether_vlantag = vlan;
2704 m->m_flags |= M_VLANTAG;
2707 if (ifp->if_capenable & IFCAP_RSS) {
2708 pi = igb_rssinfo(m, &pi0,
2709 hash, hashtype, staterr);
2711 #ifdef IGB_RSS_DEBUG
2716 IFNET_STAT_INC(ifp, ierrors, 1);
2718 igb_setup_rxdesc(cur, rxbuf);
2720 rxr->discard = TRUE;
2722 rxr->discard = FALSE;
2723 if (rxr->fmp != NULL) {
2732 ifp->if_input(ifp, m, pi, cpuid);
2734 /* Advance our pointers to the next descriptor. */
2735 if (++i == rxr->num_rx_desc)
2738 if (ncoll >= rxr->wreg_nsegs) {
2739 igb_rx_refresh(rxr, i);
2743 cur = &rxr->rx_base[i];
2744 staterr = le32toh(cur->wb.upper.status_error);
2746 rxr->next_to_check = i;
2749 igb_rx_refresh(rxr, i);
2754 igb_set_vlan(struct igb_softc *sc)
2756 struct e1000_hw *hw = &sc->hw;
2759 struct ifnet *ifp = sc->arpcom.ac_if;
2763 e1000_rlpml_set_vf(hw, sc->max_frame_size + VLAN_TAG_SIZE);
2767 reg = E1000_READ_REG(hw, E1000_CTRL);
2768 reg |= E1000_CTRL_VME;
2769 E1000_WRITE_REG(hw, E1000_CTRL, reg);
2772 /* Enable the Filter Table */
2773 if (ifp->if_capenable & IFCAP_VLAN_HWFILTER) {
2774 reg = E1000_READ_REG(hw, E1000_RCTL);
2775 reg &= ~E1000_RCTL_CFIEN;
2776 reg |= E1000_RCTL_VFE;
2777 E1000_WRITE_REG(hw, E1000_RCTL, reg);
2781 /* Update the frame size */
2782 E1000_WRITE_REG(&sc->hw, E1000_RLPML,
2783 sc->max_frame_size + VLAN_TAG_SIZE);
2786 /* Don't bother with table if no vlans */
2787 if ((adapter->num_vlans == 0) ||
2788 ((ifp->if_capenable & IFCAP_VLAN_HWFILTER) == 0))
2791 ** A soft reset zero's out the VFTA, so
2792 ** we need to repopulate it now.
2794 for (int i = 0; i < IGB_VFTA_SIZE; i++)
2795 if (adapter->shadow_vfta[i] != 0) {
2796 if (adapter->vf_ifp)
2797 e1000_vfta_set_vf(hw,
2798 adapter->shadow_vfta[i], TRUE);
2800 E1000_WRITE_REG_ARRAY(hw, E1000_VFTA,
2801 i, adapter->shadow_vfta[i]);
2807 igb_enable_intr(struct igb_softc *sc)
2809 if (sc->intr_type != PCI_INTR_TYPE_MSIX) {
2810 lwkt_serialize_handler_enable(&sc->main_serialize);
2814 for (i = 0; i < sc->msix_cnt; ++i) {
2815 lwkt_serialize_handler_enable(
2816 sc->msix_data[i].msix_serialize);
2820 if ((sc->flags & IGB_FLAG_SHARED_INTR) == 0) {
2821 if (sc->intr_type == PCI_INTR_TYPE_MSIX)
2822 E1000_WRITE_REG(&sc->hw, E1000_EIAC, sc->intr_mask);
2824 E1000_WRITE_REG(&sc->hw, E1000_EIAC, 0);
2825 E1000_WRITE_REG(&sc->hw, E1000_EIAM, sc->intr_mask);
2826 E1000_WRITE_REG(&sc->hw, E1000_EIMS, sc->intr_mask);
2827 E1000_WRITE_REG(&sc->hw, E1000_IMS, E1000_IMS_LSC);
2829 E1000_WRITE_REG(&sc->hw, E1000_IMS, IMS_ENABLE_MASK);
2831 E1000_WRITE_FLUSH(&sc->hw);
2835 igb_disable_intr(struct igb_softc *sc)
2837 if ((sc->flags & IGB_FLAG_SHARED_INTR) == 0) {
2838 E1000_WRITE_REG(&sc->hw, E1000_EIMC, 0xffffffff);
2839 E1000_WRITE_REG(&sc->hw, E1000_EIAC, 0);
2841 E1000_WRITE_REG(&sc->hw, E1000_IMC, 0xffffffff);
2842 E1000_WRITE_FLUSH(&sc->hw);
2844 if (sc->intr_type != PCI_INTR_TYPE_MSIX) {
2845 lwkt_serialize_handler_disable(&sc->main_serialize);
2849 for (i = 0; i < sc->msix_cnt; ++i) {
2850 lwkt_serialize_handler_disable(
2851 sc->msix_data[i].msix_serialize);
2857 * Bit of a misnomer, what this really means is
2858 * to enable OS management of the system... aka
2859 * to disable special hardware management features
2862 igb_get_mgmt(struct igb_softc *sc)
2864 if (sc->flags & IGB_FLAG_HAS_MGMT) {
2865 int manc2h = E1000_READ_REG(&sc->hw, E1000_MANC2H);
2866 int manc = E1000_READ_REG(&sc->hw, E1000_MANC);
2868 /* disable hardware interception of ARP */
2869 manc &= ~E1000_MANC_ARP_EN;
2871 /* enable receiving management packets to the host */
2872 manc |= E1000_MANC_EN_MNG2HOST;
2873 manc2h |= 1 << 5; /* Mng Port 623 */
2874 manc2h |= 1 << 6; /* Mng Port 664 */
2875 E1000_WRITE_REG(&sc->hw, E1000_MANC2H, manc2h);
2876 E1000_WRITE_REG(&sc->hw, E1000_MANC, manc);
2881 * Give control back to hardware management controller
2885 igb_rel_mgmt(struct igb_softc *sc)
2887 if (sc->flags & IGB_FLAG_HAS_MGMT) {
2888 int manc = E1000_READ_REG(&sc->hw, E1000_MANC);
2890 /* Re-enable hardware interception of ARP */
2891 manc |= E1000_MANC_ARP_EN;
2892 manc &= ~E1000_MANC_EN_MNG2HOST;
2894 E1000_WRITE_REG(&sc->hw, E1000_MANC, manc);
2899 * Sets CTRL_EXT:DRV_LOAD bit.
2901 * For ASF and Pass Through versions of f/w this means that
2902 * the driver is loaded.
2905 igb_get_hw_control(struct igb_softc *sc)
2912 /* Let firmware know the driver has taken over */
2913 ctrl_ext = E1000_READ_REG(&sc->hw, E1000_CTRL_EXT);
2914 E1000_WRITE_REG(&sc->hw, E1000_CTRL_EXT,
2915 ctrl_ext | E1000_CTRL_EXT_DRV_LOAD);
2919 * Resets CTRL_EXT:DRV_LOAD bit.
2921 * For ASF and Pass Through versions of f/w this means that the
2922 * driver is no longer loaded.
2925 igb_rel_hw_control(struct igb_softc *sc)
2932 /* Let firmware taken over control of h/w */
2933 ctrl_ext = E1000_READ_REG(&sc->hw, E1000_CTRL_EXT);
2934 E1000_WRITE_REG(&sc->hw, E1000_CTRL_EXT,
2935 ctrl_ext & ~E1000_CTRL_EXT_DRV_LOAD);
2939 igb_is_valid_ether_addr(const uint8_t *addr)
2941 uint8_t zero_addr[ETHER_ADDR_LEN] = { 0, 0, 0, 0, 0, 0 };
2943 if ((addr[0] & 1) || !bcmp(addr, zero_addr, ETHER_ADDR_LEN))
2949 * Enable PCI Wake On Lan capability
2952 igb_enable_wol(device_t dev)
2954 uint16_t cap, status;
2957 /* First find the capabilities pointer*/
2958 cap = pci_read_config(dev, PCIR_CAP_PTR, 2);
2960 /* Read the PM Capabilities */
2961 id = pci_read_config(dev, cap, 1);
2962 if (id != PCIY_PMG) /* Something wrong */
2966 * OK, we have the power capabilities,
2967 * so now get the status register
2969 cap += PCIR_POWER_STATUS;
2970 status = pci_read_config(dev, cap, 2);
2971 status |= PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE;
2972 pci_write_config(dev, cap, status, 2);
2976 igb_update_stats_counters(struct igb_softc *sc)
2978 struct e1000_hw *hw = &sc->hw;
2979 struct e1000_hw_stats *stats;
2980 struct ifnet *ifp = &sc->arpcom.ac_if;
2983 * The virtual function adapter has only a
2984 * small controlled set of stats, do only
2988 igb_update_vf_stats_counters(sc);
2993 if (sc->hw.phy.media_type == e1000_media_type_copper ||
2994 (E1000_READ_REG(hw, E1000_STATUS) & E1000_STATUS_LU)) {
2996 E1000_READ_REG(hw,E1000_SYMERRS);
2997 stats->sec += E1000_READ_REG(hw, E1000_SEC);
3000 stats->crcerrs += E1000_READ_REG(hw, E1000_CRCERRS);
3001 stats->mpc += E1000_READ_REG(hw, E1000_MPC);
3002 stats->scc += E1000_READ_REG(hw, E1000_SCC);
3003 stats->ecol += E1000_READ_REG(hw, E1000_ECOL);
3005 stats->mcc += E1000_READ_REG(hw, E1000_MCC);
3006 stats->latecol += E1000_READ_REG(hw, E1000_LATECOL);
3007 stats->colc += E1000_READ_REG(hw, E1000_COLC);
3008 stats->dc += E1000_READ_REG(hw, E1000_DC);
3009 stats->rlec += E1000_READ_REG(hw, E1000_RLEC);
3010 stats->xonrxc += E1000_READ_REG(hw, E1000_XONRXC);
3011 stats->xontxc += E1000_READ_REG(hw, E1000_XONTXC);
3014 * For watchdog management we need to know if we have been
3015 * paused during the last interval, so capture that here.
3017 sc->pause_frames = E1000_READ_REG(hw, E1000_XOFFRXC);
3018 stats->xoffrxc += sc->pause_frames;
3019 stats->xofftxc += E1000_READ_REG(hw, E1000_XOFFTXC);
3020 stats->fcruc += E1000_READ_REG(hw, E1000_FCRUC);
3021 stats->prc64 += E1000_READ_REG(hw, E1000_PRC64);
3022 stats->prc127 += E1000_READ_REG(hw, E1000_PRC127);
3023 stats->prc255 += E1000_READ_REG(hw, E1000_PRC255);
3024 stats->prc511 += E1000_READ_REG(hw, E1000_PRC511);
3025 stats->prc1023 += E1000_READ_REG(hw, E1000_PRC1023);
3026 stats->prc1522 += E1000_READ_REG(hw, E1000_PRC1522);
3027 stats->gprc += E1000_READ_REG(hw, E1000_GPRC);
3028 stats->bprc += E1000_READ_REG(hw, E1000_BPRC);
3029 stats->mprc += E1000_READ_REG(hw, E1000_MPRC);
3030 stats->gptc += E1000_READ_REG(hw, E1000_GPTC);
3032 /* For the 64-bit byte counters the low dword must be read first. */
3033 /* Both registers clear on the read of the high dword */
3035 stats->gorc += E1000_READ_REG(hw, E1000_GORCL) +
3036 ((uint64_t)E1000_READ_REG(hw, E1000_GORCH) << 32);
3037 stats->gotc += E1000_READ_REG(hw, E1000_GOTCL) +
3038 ((uint64_t)E1000_READ_REG(hw, E1000_GOTCH) << 32);
3040 stats->rnbc += E1000_READ_REG(hw, E1000_RNBC);
3041 stats->ruc += E1000_READ_REG(hw, E1000_RUC);
3042 stats->rfc += E1000_READ_REG(hw, E1000_RFC);
3043 stats->roc += E1000_READ_REG(hw, E1000_ROC);
3044 stats->rjc += E1000_READ_REG(hw, E1000_RJC);
3046 stats->tor += E1000_READ_REG(hw, E1000_TORH);
3047 stats->tot += E1000_READ_REG(hw, E1000_TOTH);
3049 stats->tpr += E1000_READ_REG(hw, E1000_TPR);
3050 stats->tpt += E1000_READ_REG(hw, E1000_TPT);
3051 stats->ptc64 += E1000_READ_REG(hw, E1000_PTC64);
3052 stats->ptc127 += E1000_READ_REG(hw, E1000_PTC127);
3053 stats->ptc255 += E1000_READ_REG(hw, E1000_PTC255);
3054 stats->ptc511 += E1000_READ_REG(hw, E1000_PTC511);
3055 stats->ptc1023 += E1000_READ_REG(hw, E1000_PTC1023);
3056 stats->ptc1522 += E1000_READ_REG(hw, E1000_PTC1522);
3057 stats->mptc += E1000_READ_REG(hw, E1000_MPTC);
3058 stats->bptc += E1000_READ_REG(hw, E1000_BPTC);
3060 /* Interrupt Counts */
3062 stats->iac += E1000_READ_REG(hw, E1000_IAC);
3063 stats->icrxptc += E1000_READ_REG(hw, E1000_ICRXPTC);
3064 stats->icrxatc += E1000_READ_REG(hw, E1000_ICRXATC);
3065 stats->ictxptc += E1000_READ_REG(hw, E1000_ICTXPTC);
3066 stats->ictxatc += E1000_READ_REG(hw, E1000_ICTXATC);
3067 stats->ictxqec += E1000_READ_REG(hw, E1000_ICTXQEC);
3068 stats->ictxqmtc += E1000_READ_REG(hw, E1000_ICTXQMTC);
3069 stats->icrxdmtc += E1000_READ_REG(hw, E1000_ICRXDMTC);
3070 stats->icrxoc += E1000_READ_REG(hw, E1000_ICRXOC);
3072 /* Host to Card Statistics */
3074 stats->cbtmpc += E1000_READ_REG(hw, E1000_CBTMPC);
3075 stats->htdpmc += E1000_READ_REG(hw, E1000_HTDPMC);
3076 stats->cbrdpc += E1000_READ_REG(hw, E1000_CBRDPC);
3077 stats->cbrmpc += E1000_READ_REG(hw, E1000_CBRMPC);
3078 stats->rpthc += E1000_READ_REG(hw, E1000_RPTHC);
3079 stats->hgptc += E1000_READ_REG(hw, E1000_HGPTC);
3080 stats->htcbdpc += E1000_READ_REG(hw, E1000_HTCBDPC);
3081 stats->hgorc += (E1000_READ_REG(hw, E1000_HGORCL) +
3082 ((uint64_t)E1000_READ_REG(hw, E1000_HGORCH) << 32));
3083 stats->hgotc += (E1000_READ_REG(hw, E1000_HGOTCL) +
3084 ((uint64_t)E1000_READ_REG(hw, E1000_HGOTCH) << 32));
3085 stats->lenerrs += E1000_READ_REG(hw, E1000_LENERRS);
3086 stats->scvpc += E1000_READ_REG(hw, E1000_SCVPC);
3087 stats->hrmpc += E1000_READ_REG(hw, E1000_HRMPC);
3089 stats->algnerrc += E1000_READ_REG(hw, E1000_ALGNERRC);
3090 stats->rxerrc += E1000_READ_REG(hw, E1000_RXERRC);
3091 stats->tncrs += E1000_READ_REG(hw, E1000_TNCRS);
3092 stats->cexterr += E1000_READ_REG(hw, E1000_CEXTERR);
3093 stats->tsctc += E1000_READ_REG(hw, E1000_TSCTC);
3094 stats->tsctfc += E1000_READ_REG(hw, E1000_TSCTFC);
3096 IFNET_STAT_SET(ifp, collisions, stats->colc);
3099 IFNET_STAT_SET(ifp, ierrors,
3100 stats->rxerrc + stats->crcerrs + stats->algnerrc +
3101 stats->ruc + stats->roc + stats->mpc + stats->cexterr);
3104 IFNET_STAT_SET(ifp, oerrors,
3105 stats->ecol + stats->latecol + sc->watchdog_events);
3107 /* Driver specific counters */
3108 sc->device_control = E1000_READ_REG(hw, E1000_CTRL);
3109 sc->rx_control = E1000_READ_REG(hw, E1000_RCTL);
3110 sc->int_mask = E1000_READ_REG(hw, E1000_IMS);
3111 sc->eint_mask = E1000_READ_REG(hw, E1000_EIMS);
3112 sc->packet_buf_alloc_tx =
3113 ((E1000_READ_REG(hw, E1000_PBA) & 0xffff0000) >> 16);
3114 sc->packet_buf_alloc_rx =
3115 (E1000_READ_REG(hw, E1000_PBA) & 0xffff);
3119 igb_vf_init_stats(struct igb_softc *sc)
3121 struct e1000_hw *hw = &sc->hw;
3122 struct e1000_vf_stats *stats;
3125 stats->last_gprc = E1000_READ_REG(hw, E1000_VFGPRC);
3126 stats->last_gorc = E1000_READ_REG(hw, E1000_VFGORC);
3127 stats->last_gptc = E1000_READ_REG(hw, E1000_VFGPTC);
3128 stats->last_gotc = E1000_READ_REG(hw, E1000_VFGOTC);
3129 stats->last_mprc = E1000_READ_REG(hw, E1000_VFMPRC);
3133 igb_update_vf_stats_counters(struct igb_softc *sc)
3135 struct e1000_hw *hw = &sc->hw;
3136 struct e1000_vf_stats *stats;
3138 if (sc->link_speed == 0)
3142 UPDATE_VF_REG(E1000_VFGPRC, stats->last_gprc, stats->gprc);
3143 UPDATE_VF_REG(E1000_VFGORC, stats->last_gorc, stats->gorc);
3144 UPDATE_VF_REG(E1000_VFGPTC, stats->last_gptc, stats->gptc);
3145 UPDATE_VF_REG(E1000_VFGOTC, stats->last_gotc, stats->gotc);
3146 UPDATE_VF_REG(E1000_VFMPRC, stats->last_mprc, stats->mprc);
3149 #ifdef IFPOLL_ENABLE
3152 igb_npoll_status(struct ifnet *ifp)
3154 struct igb_softc *sc = ifp->if_softc;
3157 ASSERT_SERIALIZED(&sc->main_serialize);
3159 reg_icr = E1000_READ_REG(&sc->hw, E1000_ICR);
3160 if (reg_icr & (E1000_ICR_RXSEQ | E1000_ICR_LSC)) {
3161 sc->hw.mac.get_link_status = 1;
3162 igb_update_link_status(sc);
3167 igb_npoll_tx(struct ifnet *ifp, void *arg, int cycle __unused)
3169 struct igb_tx_ring *txr = arg;
3171 ASSERT_SERIALIZED(&txr->tx_serialize);
3174 if (!ifsq_is_empty(txr->ifsq))
3175 ifsq_devstart(txr->ifsq);
3179 igb_npoll_rx(struct ifnet *ifp __unused, void *arg, int cycle)
3181 struct igb_rx_ring *rxr = arg;
3183 ASSERT_SERIALIZED(&rxr->rx_serialize);
3185 igb_rxeof(rxr, cycle);
3189 igb_npoll(struct ifnet *ifp, struct ifpoll_info *info)
3191 struct igb_softc *sc = ifp->if_softc;
3192 int i, txr_cnt, rxr_cnt;
3194 ASSERT_IFNET_SERIALIZED_ALL(ifp);
3199 info->ifpi_status.status_func = igb_npoll_status;
3200 info->ifpi_status.serializer = &sc->main_serialize;
3202 txr_cnt = igb_get_txring_inuse(sc, TRUE);
3203 off = sc->tx_npoll_off;
3204 for (i = 0; i < txr_cnt; ++i) {
3205 struct igb_tx_ring *txr = &sc->tx_rings[i];
3208 KKASSERT(idx < ncpus2);
3209 info->ifpi_tx[idx].poll_func = igb_npoll_tx;
3210 info->ifpi_tx[idx].arg = txr;
3211 info->ifpi_tx[idx].serializer = &txr->tx_serialize;
3212 ifsq_set_cpuid(txr->ifsq, idx);
3215 rxr_cnt = igb_get_rxring_inuse(sc, TRUE);
3216 off = sc->rx_npoll_off;
3217 for (i = 0; i < rxr_cnt; ++i) {
3218 struct igb_rx_ring *rxr = &sc->rx_rings[i];
3221 KKASSERT(idx < ncpus2);
3222 info->ifpi_rx[idx].poll_func = igb_npoll_rx;
3223 info->ifpi_rx[idx].arg = rxr;
3224 info->ifpi_rx[idx].serializer = &rxr->rx_serialize;
3227 if (ifp->if_flags & IFF_RUNNING) {
3228 if (rxr_cnt == sc->rx_ring_inuse &&
3229 txr_cnt == sc->tx_ring_inuse) {
3230 igb_set_timer_cpuid(sc, TRUE);
3231 igb_disable_intr(sc);
3237 for (i = 0; i < sc->tx_ring_cnt; ++i) {
3238 struct igb_tx_ring *txr = &sc->tx_rings[i];
3240 ifsq_set_cpuid(txr->ifsq, txr->tx_intr_cpuid);
3243 if (ifp->if_flags & IFF_RUNNING) {
3244 txr_cnt = igb_get_txring_inuse(sc, FALSE);
3245 rxr_cnt = igb_get_rxring_inuse(sc, FALSE);
3247 if (rxr_cnt == sc->rx_ring_inuse &&
3248 txr_cnt == sc->tx_ring_inuse) {
3249 igb_set_timer_cpuid(sc, FALSE);
3250 igb_enable_intr(sc);
3258 #endif /* IFPOLL_ENABLE */
3263 struct igb_softc *sc = xsc;
3264 struct ifnet *ifp = &sc->arpcom.ac_if;
3267 ASSERT_SERIALIZED(&sc->main_serialize);
3269 eicr = E1000_READ_REG(&sc->hw, E1000_EICR);
3274 if (ifp->if_flags & IFF_RUNNING) {
3275 struct igb_tx_ring *txr = &sc->tx_rings[0];
3278 for (i = 0; i < sc->rx_ring_inuse; ++i) {
3279 struct igb_rx_ring *rxr = &sc->rx_rings[i];
3281 if (eicr & rxr->rx_intr_mask) {
3282 lwkt_serialize_enter(&rxr->rx_serialize);
3284 lwkt_serialize_exit(&rxr->rx_serialize);
3288 if (eicr & txr->tx_intr_mask) {
3289 lwkt_serialize_enter(&txr->tx_serialize);
3291 if (!ifsq_is_empty(txr->ifsq))
3292 ifsq_devstart(txr->ifsq);
3293 lwkt_serialize_exit(&txr->tx_serialize);
3297 if (eicr & E1000_EICR_OTHER) {
3298 uint32_t icr = E1000_READ_REG(&sc->hw, E1000_ICR);
3300 /* Link status change */
3301 if (icr & E1000_ICR_LSC) {
3302 sc->hw.mac.get_link_status = 1;
3303 igb_update_link_status(sc);
3308 * Reading EICR has the side effect to clear interrupt mask,
3309 * so all interrupts need to be enabled here.
3311 E1000_WRITE_REG(&sc->hw, E1000_EIMS, sc->intr_mask);
3315 igb_intr_shared(void *xsc)
3317 struct igb_softc *sc = xsc;
3318 struct ifnet *ifp = &sc->arpcom.ac_if;
3321 ASSERT_SERIALIZED(&sc->main_serialize);
3323 reg_icr = E1000_READ_REG(&sc->hw, E1000_ICR);
3326 if (reg_icr == 0xffffffff)
3329 /* Definitely not our interrupt. */
3333 if ((reg_icr & E1000_ICR_INT_ASSERTED) == 0)
3336 if (ifp->if_flags & IFF_RUNNING) {
3338 (E1000_ICR_RXT0 | E1000_ICR_RXDMT0 | E1000_ICR_RXO)) {
3341 for (i = 0; i < sc->rx_ring_inuse; ++i) {
3342 struct igb_rx_ring *rxr = &sc->rx_rings[i];
3344 lwkt_serialize_enter(&rxr->rx_serialize);
3346 lwkt_serialize_exit(&rxr->rx_serialize);
3350 if (reg_icr & E1000_ICR_TXDW) {
3351 struct igb_tx_ring *txr = &sc->tx_rings[0];
3353 lwkt_serialize_enter(&txr->tx_serialize);
3355 if (!ifsq_is_empty(txr->ifsq))
3356 ifsq_devstart(txr->ifsq);
3357 lwkt_serialize_exit(&txr->tx_serialize);
3361 /* Link status change */
3362 if (reg_icr & (E1000_ICR_RXSEQ | E1000_ICR_LSC)) {
3363 sc->hw.mac.get_link_status = 1;
3364 igb_update_link_status(sc);
3367 if (reg_icr & E1000_ICR_RXO)
3372 igb_encap(struct igb_tx_ring *txr, struct mbuf **m_headp,
3373 int *segs_used, int *idx)
3375 bus_dma_segment_t segs[IGB_MAX_SCATTER];
3377 struct igb_tx_buf *tx_buf, *tx_buf_mapped;
3378 union e1000_adv_tx_desc *txd = NULL;
3379 struct mbuf *m_head = *m_headp;
3380 uint32_t olinfo_status = 0, cmd_type_len = 0, cmd_rs = 0;
3381 int maxsegs, nsegs, i, j, error;
3382 uint32_t hdrlen = 0;
3384 if (m_head->m_pkthdr.csum_flags & CSUM_TSO) {
3385 error = igb_tso_pullup(txr, m_headp);
3391 /* Set basic descriptor constants */
3392 cmd_type_len |= E1000_ADVTXD_DTYP_DATA;
3393 cmd_type_len |= E1000_ADVTXD_DCMD_IFCS | E1000_ADVTXD_DCMD_DEXT;
3394 if (m_head->m_flags & M_VLANTAG)
3395 cmd_type_len |= E1000_ADVTXD_DCMD_VLE;
3398 * Map the packet for DMA.
3400 tx_buf = &txr->tx_buf[txr->next_avail_desc];
3401 tx_buf_mapped = tx_buf;
3404 maxsegs = txr->tx_avail - IGB_TX_RESERVED;
3405 KASSERT(maxsegs >= txr->spare_desc, ("not enough spare TX desc\n"));
3406 if (maxsegs > IGB_MAX_SCATTER)
3407 maxsegs = IGB_MAX_SCATTER;
3409 error = bus_dmamap_load_mbuf_defrag(txr->tx_tag, map, m_headp,
3410 segs, maxsegs, &nsegs, BUS_DMA_NOWAIT);
3412 if (error == ENOBUFS)
3413 txr->sc->mbuf_defrag_failed++;
3415 txr->sc->no_tx_dma_setup++;
3421 bus_dmamap_sync(txr->tx_tag, map, BUS_DMASYNC_PREWRITE);
3426 * Set up the TX context descriptor, if any hardware offloading is
3427 * needed. This includes CSUM, VLAN, and TSO. It will consume one
3430 * Unlike these chips' predecessors (em/emx), TX context descriptor
3431 * will _not_ interfere TX data fetching pipelining.
3433 if (m_head->m_pkthdr.csum_flags & CSUM_TSO) {
3434 igb_tso_ctx(txr, m_head, &hdrlen);
3435 cmd_type_len |= E1000_ADVTXD_DCMD_TSE;
3436 olinfo_status |= E1000_TXD_POPTS_IXSM << 8;
3437 olinfo_status |= E1000_TXD_POPTS_TXSM << 8;
3440 } else if (igb_txcsum_ctx(txr, m_head)) {
3441 if (m_head->m_pkthdr.csum_flags & CSUM_IP)
3442 olinfo_status |= (E1000_TXD_POPTS_IXSM << 8);
3443 if (m_head->m_pkthdr.csum_flags & (CSUM_UDP | CSUM_TCP))
3444 olinfo_status |= (E1000_TXD_POPTS_TXSM << 8);
3449 *segs_used += nsegs;
3450 txr->tx_nsegs += nsegs;
3451 if (txr->tx_nsegs >= txr->intr_nsegs) {
3453 * Report Status (RS) is turned on every intr_nsegs
3454 * descriptors (roughly).
3457 cmd_rs = E1000_ADVTXD_DCMD_RS;
3460 /* Calculate payload length */
3461 olinfo_status |= ((m_head->m_pkthdr.len - hdrlen)
3462 << E1000_ADVTXD_PAYLEN_SHIFT);
3465 * 82575 needs the TX context index added; the queue
3466 * index is used as TX context index here.
3468 if (txr->sc->hw.mac.type == e1000_82575)
3469 olinfo_status |= txr->me << 4;
3471 /* Set up our transmit descriptors */
3472 i = txr->next_avail_desc;
3473 for (j = 0; j < nsegs; j++) {
3475 bus_addr_t seg_addr;
3477 tx_buf = &txr->tx_buf[i];
3478 txd = (union e1000_adv_tx_desc *)&txr->tx_base[i];
3479 seg_addr = segs[j].ds_addr;
3480 seg_len = segs[j].ds_len;
3482 txd->read.buffer_addr = htole64(seg_addr);
3483 txd->read.cmd_type_len = htole32(cmd_type_len | seg_len);
3484 txd->read.olinfo_status = htole32(olinfo_status);
3485 if (++i == txr->num_tx_desc)
3487 tx_buf->m_head = NULL;
3490 KASSERT(txr->tx_avail > nsegs, ("invalid avail TX desc\n"));
3491 txr->next_avail_desc = i;
3492 txr->tx_avail -= nsegs;
3494 tx_buf->m_head = m_head;
3495 tx_buf_mapped->map = tx_buf->map;
3499 * Last Descriptor of Packet needs End Of Packet (EOP)
3501 txd->read.cmd_type_len |= htole32(E1000_ADVTXD_DCMD_EOP | cmd_rs);
3504 * Defer TDT updating, until enough descrptors are setup
3507 #ifdef IGB_TSS_DEBUG
3515 igb_start(struct ifnet *ifp, struct ifaltq_subque *ifsq)
3517 struct igb_softc *sc = ifp->if_softc;
3518 struct igb_tx_ring *txr = ifsq_get_priv(ifsq);
3519 struct mbuf *m_head;
3520 int idx = -1, nsegs = 0;
3522 KKASSERT(txr->ifsq == ifsq);
3523 ASSERT_SERIALIZED(&txr->tx_serialize);
3525 if ((ifp->if_flags & IFF_RUNNING) == 0 || ifsq_is_oactive(ifsq))
3528 if (!sc->link_active || (txr->tx_flags & IGB_TXFLAG_ENABLED) == 0) {
3533 if (!IGB_IS_NOT_OACTIVE(txr))
3536 while (!ifsq_is_empty(ifsq)) {
3537 if (IGB_IS_OACTIVE(txr)) {
3538 ifsq_set_oactive(ifsq);
3539 /* Set watchdog on */
3540 txr->tx_watchdog.wd_timer = 5;
3544 m_head = ifsq_dequeue(ifsq);
3548 if (igb_encap(txr, &m_head, &nsegs, &idx)) {
3549 IFNET_STAT_INC(ifp, oerrors, 1);
3554 * TX interrupt are aggressively aggregated, so increasing
3555 * opackets at TX interrupt time will make the opackets
3556 * statistics vastly inaccurate; we do the opackets increment
3559 IFNET_STAT_INC(ifp, opackets, 1);
3561 if (nsegs >= txr->wreg_nsegs) {
3562 E1000_WRITE_REG(&txr->sc->hw, E1000_TDT(txr->me), idx);
3567 /* Send a copy of the frame to the BPF listener */
3568 ETHER_BPF_MTAP(ifp, m_head);
3571 E1000_WRITE_REG(&txr->sc->hw, E1000_TDT(txr->me), idx);
3575 igb_watchdog(struct ifaltq_subque *ifsq)
3577 struct igb_tx_ring *txr = ifsq_get_priv(ifsq);
3578 struct ifnet *ifp = ifsq_get_ifp(ifsq);
3579 struct igb_softc *sc = ifp->if_softc;
3582 KKASSERT(txr->ifsq == ifsq);
3583 ASSERT_IFNET_SERIALIZED_ALL(ifp);
3586 * If flow control has paused us since last checking
3587 * it invalidates the watchdog timing, so dont run it.
3589 if (sc->pause_frames) {
3590 sc->pause_frames = 0;
3591 txr->tx_watchdog.wd_timer = 5;
3595 if_printf(ifp, "Watchdog timeout -- resetting\n");
3596 if_printf(ifp, "Queue(%d) tdh = %d, hw tdt = %d\n", txr->me,
3597 E1000_READ_REG(&sc->hw, E1000_TDH(txr->me)),
3598 E1000_READ_REG(&sc->hw, E1000_TDT(txr->me)));
3599 if_printf(ifp, "TX(%d) desc avail = %d, "
3600 "Next TX to Clean = %d\n",
3601 txr->me, txr->tx_avail, txr->next_to_clean);
3603 IFNET_STAT_INC(ifp, oerrors, 1);
3604 sc->watchdog_events++;
3607 for (i = 0; i < sc->tx_ring_inuse; ++i)
3608 ifsq_devstart_sched(sc->tx_rings[i].ifsq);
3612 igb_set_eitr(struct igb_softc *sc, int idx, int rate)
3617 if (sc->hw.mac.type == e1000_82575) {
3618 eitr = 1000000000 / 256 / rate;
3621 * Document is wrong on the 2 bits left shift
3624 eitr = 1000000 / rate;
3625 eitr <<= IGB_EITR_INTVL_SHIFT;
3629 /* Don't disable it */
3630 eitr = 1 << IGB_EITR_INTVL_SHIFT;
3631 } else if (eitr > IGB_EITR_INTVL_MASK) {
3632 /* Don't allow it to be too large */
3633 eitr = IGB_EITR_INTVL_MASK;
3636 if (sc->hw.mac.type == e1000_82575)
3639 eitr |= E1000_EITR_CNT_IGNR;
3640 E1000_WRITE_REG(&sc->hw, E1000_EITR(idx), eitr);
3644 igb_sysctl_intr_rate(SYSCTL_HANDLER_ARGS)
3646 struct igb_softc *sc = (void *)arg1;
3647 struct ifnet *ifp = &sc->arpcom.ac_if;
3648 int error, intr_rate;
3650 intr_rate = sc->intr_rate;
3651 error = sysctl_handle_int(oidp, &intr_rate, 0, req);
3652 if (error || req->newptr == NULL)
3657 ifnet_serialize_all(ifp);
3659 sc->intr_rate = intr_rate;
3660 if (ifp->if_flags & IFF_RUNNING)
3661 igb_set_eitr(sc, 0, sc->intr_rate);
3664 if_printf(ifp, "interrupt rate set to %d/sec\n", sc->intr_rate);
3666 ifnet_deserialize_all(ifp);
3672 igb_sysctl_msix_rate(SYSCTL_HANDLER_ARGS)
3674 struct igb_msix_data *msix = (void *)arg1;
3675 struct igb_softc *sc = msix->msix_sc;
3676 struct ifnet *ifp = &sc->arpcom.ac_if;
3677 int error, msix_rate;
3679 msix_rate = msix->msix_rate;
3680 error = sysctl_handle_int(oidp, &msix_rate, 0, req);
3681 if (error || req->newptr == NULL)
3686 lwkt_serialize_enter(msix->msix_serialize);
3688 msix->msix_rate = msix_rate;
3689 if (ifp->if_flags & IFF_RUNNING)
3690 igb_set_eitr(sc, msix->msix_vector, msix->msix_rate);
3693 if_printf(ifp, "%s set to %d/sec\n", msix->msix_rate_desc,
3697 lwkt_serialize_exit(msix->msix_serialize);
3703 igb_sysctl_tx_intr_nsegs(SYSCTL_HANDLER_ARGS)
3705 struct igb_softc *sc = (void *)arg1;
3706 struct ifnet *ifp = &sc->arpcom.ac_if;
3707 struct igb_tx_ring *txr = &sc->tx_rings[0];
3710 nsegs = txr->intr_nsegs;
3711 error = sysctl_handle_int(oidp, &nsegs, 0, req);
3712 if (error || req->newptr == NULL)
3717 ifnet_serialize_all(ifp);
3719 if (nsegs >= txr->num_tx_desc - txr->oact_lo_desc ||
3720 nsegs >= txr->oact_hi_desc - IGB_MAX_SCATTER) {
3726 for (i = 0; i < sc->tx_ring_cnt; ++i)
3727 sc->tx_rings[i].intr_nsegs = nsegs;
3730 ifnet_deserialize_all(ifp);
3736 igb_sysctl_rx_wreg_nsegs(SYSCTL_HANDLER_ARGS)
3738 struct igb_softc *sc = (void *)arg1;
3739 struct ifnet *ifp = &sc->arpcom.ac_if;
3740 int error, nsegs, i;
3742 nsegs = sc->rx_rings[0].wreg_nsegs;
3743 error = sysctl_handle_int(oidp, &nsegs, 0, req);
3744 if (error || req->newptr == NULL)
3747 ifnet_serialize_all(ifp);
3748 for (i = 0; i < sc->rx_ring_cnt; ++i)
3749 sc->rx_rings[i].wreg_nsegs =nsegs;
3750 ifnet_deserialize_all(ifp);
3756 igb_sysctl_tx_wreg_nsegs(SYSCTL_HANDLER_ARGS)
3758 struct igb_softc *sc = (void *)arg1;
3759 struct ifnet *ifp = &sc->arpcom.ac_if;
3760 int error, nsegs, i;
3762 nsegs = sc->tx_rings[0].wreg_nsegs;
3763 error = sysctl_handle_int(oidp, &nsegs, 0, req);
3764 if (error || req->newptr == NULL)
3767 ifnet_serialize_all(ifp);
3768 for (i = 0; i < sc->tx_ring_cnt; ++i)
3769 sc->tx_rings[i].wreg_nsegs =nsegs;
3770 ifnet_deserialize_all(ifp);
3775 #ifdef IFPOLL_ENABLE
3778 igb_sysctl_npoll_rxoff(SYSCTL_HANDLER_ARGS)
3780 struct igb_softc *sc = (void *)arg1;
3781 struct ifnet *ifp = &sc->arpcom.ac_if;
3784 off = sc->rx_npoll_off;
3785 error = sysctl_handle_int(oidp, &off, 0, req);
3786 if (error || req->newptr == NULL)
3791 ifnet_serialize_all(ifp);
3792 if (off >= ncpus2 || off % sc->rx_ring_cnt != 0) {
3796 sc->rx_npoll_off = off;
3798 ifnet_deserialize_all(ifp);
3804 igb_sysctl_npoll_txoff(SYSCTL_HANDLER_ARGS)
3806 struct igb_softc *sc = (void *)arg1;
3807 struct ifnet *ifp = &sc->arpcom.ac_if;
3810 off = sc->tx_npoll_off;
3811 error = sysctl_handle_int(oidp, &off, 0, req);
3812 if (error || req->newptr == NULL)
3817 ifnet_serialize_all(ifp);
3818 if (off >= ncpus2 || off % sc->tx_ring_cnt != 0) {
3822 sc->tx_npoll_off = off;
3824 ifnet_deserialize_all(ifp);
3829 #endif /* IFPOLL_ENABLE */
3832 igb_init_intr(struct igb_softc *sc)
3834 igb_set_intr_mask(sc);
3836 if ((sc->flags & IGB_FLAG_SHARED_INTR) == 0)
3837 igb_init_unshared_intr(sc);
3839 if (sc->intr_type != PCI_INTR_TYPE_MSIX) {
3840 igb_set_eitr(sc, 0, sc->intr_rate);
3844 for (i = 0; i < sc->msix_cnt; ++i)
3845 igb_set_eitr(sc, i, sc->msix_data[i].msix_rate);
3850 igb_init_unshared_intr(struct igb_softc *sc)
3852 struct e1000_hw *hw = &sc->hw;
3853 const struct igb_rx_ring *rxr;
3854 const struct igb_tx_ring *txr;
3855 uint32_t ivar, index;
3859 * Enable extended mode
3861 if (sc->hw.mac.type != e1000_82575) {
3865 gpie = E1000_GPIE_NSICR;
3866 if (sc->intr_type == PCI_INTR_TYPE_MSIX) {
3867 gpie |= E1000_GPIE_MSIX_MODE |
3871 E1000_WRITE_REG(hw, E1000_GPIE, gpie);
3876 switch (sc->hw.mac.type) {
3878 ivar_max = IGB_MAX_IVAR_82576;
3882 ivar_max = IGB_MAX_IVAR_82580;
3886 ivar_max = IGB_MAX_IVAR_I350;
3890 ivar_max = IGB_MAX_IVAR_I354;
3894 case e1000_vfadapt_i350:
3895 ivar_max = IGB_MAX_IVAR_VF;
3899 ivar_max = IGB_MAX_IVAR_I210;
3903 ivar_max = IGB_MAX_IVAR_I211;
3907 panic("unknown mac type %d\n", sc->hw.mac.type);
3909 for (i = 0; i < ivar_max; ++i)
3910 E1000_WRITE_REG_ARRAY(hw, E1000_IVAR0, i, 0);
3911 E1000_WRITE_REG(hw, E1000_IVAR_MISC, 0);
3915 KASSERT(sc->intr_type != PCI_INTR_TYPE_MSIX,
3916 ("82575 w/ MSI-X"));
3917 tmp = E1000_READ_REG(hw, E1000_CTRL_EXT);
3918 tmp |= E1000_CTRL_EXT_IRCA;
3919 E1000_WRITE_REG(hw, E1000_CTRL_EXT, tmp);
3923 * Map TX/RX interrupts to EICR
3925 switch (sc->hw.mac.type) {
3930 case e1000_vfadapt_i350:
3934 for (i = 0; i < sc->rx_ring_inuse; ++i) {
3935 rxr = &sc->rx_rings[i];
3938 ivar = E1000_READ_REG_ARRAY(hw, E1000_IVAR0, index);
3943 (rxr->rx_intr_bit | E1000_IVAR_VALID) << 16;
3947 (rxr->rx_intr_bit | E1000_IVAR_VALID);
3949 E1000_WRITE_REG_ARRAY(hw, E1000_IVAR0, index, ivar);
3952 for (i = 0; i < sc->tx_ring_inuse; ++i) {
3953 txr = &sc->tx_rings[i];
3956 ivar = E1000_READ_REG_ARRAY(hw, E1000_IVAR0, index);
3961 (txr->tx_intr_bit | E1000_IVAR_VALID) << 24;
3965 (txr->tx_intr_bit | E1000_IVAR_VALID) << 8;
3967 E1000_WRITE_REG_ARRAY(hw, E1000_IVAR0, index, ivar);
3969 if (sc->intr_type == PCI_INTR_TYPE_MSIX) {
3970 ivar = (sc->sts_intr_bit | E1000_IVAR_VALID) << 8;
3971 E1000_WRITE_REG(hw, E1000_IVAR_MISC, ivar);
3977 for (i = 0; i < sc->rx_ring_inuse; ++i) {
3978 rxr = &sc->rx_rings[i];
3980 index = i & 0x7; /* Each IVAR has two entries */
3981 ivar = E1000_READ_REG_ARRAY(hw, E1000_IVAR0, index);
3986 (rxr->rx_intr_bit | E1000_IVAR_VALID);
3990 (rxr->rx_intr_bit | E1000_IVAR_VALID) << 16;
3992 E1000_WRITE_REG_ARRAY(hw, E1000_IVAR0, index, ivar);
3995 for (i = 0; i < sc->tx_ring_inuse; ++i) {
3996 txr = &sc->tx_rings[i];
3998 index = i & 0x7; /* Each IVAR has two entries */
3999 ivar = E1000_READ_REG_ARRAY(hw, E1000_IVAR0, index);
4004 (txr->tx_intr_bit | E1000_IVAR_VALID) << 8;
4008 (txr->tx_intr_bit | E1000_IVAR_VALID) << 24;
4010 E1000_WRITE_REG_ARRAY(hw, E1000_IVAR0, index, ivar);
4012 if (sc->intr_type == PCI_INTR_TYPE_MSIX) {
4013 ivar = (sc->sts_intr_bit | E1000_IVAR_VALID) << 8;
4014 E1000_WRITE_REG(hw, E1000_IVAR_MISC, ivar);
4020 * Enable necessary interrupt bits.
4022 * The name of the register is confusing; in addition to
4023 * configuring the first vector of MSI-X, it also configures
4024 * which bits of EICR could be set by the hardware even when
4025 * MSI or line interrupt is used; it thus controls interrupt
4026 * generation. It MUST be configured explicitly; the default
4027 * value mentioned in the datasheet is wrong: RX queue0 and
4028 * TX queue0 are NOT enabled by default.
4030 E1000_WRITE_REG(&sc->hw, E1000_MSIXBM(0), sc->intr_mask);
4034 panic("unknown mac type %d\n", sc->hw.mac.type);
4039 igb_setup_intr(struct igb_softc *sc)
4043 if (sc->intr_type == PCI_INTR_TYPE_MSIX)
4044 return igb_msix_setup(sc);
4046 error = bus_setup_intr(sc->dev, sc->intr_res, INTR_MPSAFE,
4047 (sc->flags & IGB_FLAG_SHARED_INTR) ? igb_intr_shared : igb_intr,
4048 sc, &sc->intr_tag, &sc->main_serialize);
4050 device_printf(sc->dev, "Failed to register interrupt handler");
4057 igb_set_txintr_mask(struct igb_tx_ring *txr, int *intr_bit0, int intr_bitmax)
4059 if (txr->sc->hw.mac.type == e1000_82575) {
4060 txr->tx_intr_bit = 0; /* unused */
4063 txr->tx_intr_mask = E1000_EICR_TX_QUEUE0;
4066 txr->tx_intr_mask = E1000_EICR_TX_QUEUE1;
4069 txr->tx_intr_mask = E1000_EICR_TX_QUEUE2;
4072 txr->tx_intr_mask = E1000_EICR_TX_QUEUE3;
4075 panic("unsupported # of TX ring, %d\n", txr->me);
4078 int intr_bit = *intr_bit0;
4080 txr->tx_intr_bit = intr_bit % intr_bitmax;
4081 txr->tx_intr_mask = 1 << txr->tx_intr_bit;
4083 *intr_bit0 = intr_bit + 1;
4088 igb_set_rxintr_mask(struct igb_rx_ring *rxr, int *intr_bit0, int intr_bitmax)
4090 if (rxr->sc->hw.mac.type == e1000_82575) {
4091 rxr->rx_intr_bit = 0; /* unused */
4094 rxr->rx_intr_mask = E1000_EICR_RX_QUEUE0;
4097 rxr->rx_intr_mask = E1000_EICR_RX_QUEUE1;
4100 rxr->rx_intr_mask = E1000_EICR_RX_QUEUE2;
4103 rxr->rx_intr_mask = E1000_EICR_RX_QUEUE3;
4106 panic("unsupported # of RX ring, %d\n", rxr->me);
4109 int intr_bit = *intr_bit0;
4111 rxr->rx_intr_bit = intr_bit % intr_bitmax;
4112 rxr->rx_intr_mask = 1 << rxr->rx_intr_bit;
4114 *intr_bit0 = intr_bit + 1;
4119 igb_serialize(struct ifnet *ifp, enum ifnet_serialize slz)
4121 struct igb_softc *sc = ifp->if_softc;
4123 ifnet_serialize_array_enter(sc->serializes, sc->serialize_cnt, slz);
4127 igb_deserialize(struct ifnet *ifp, enum ifnet_serialize slz)
4129 struct igb_softc *sc = ifp->if_softc;
4131 ifnet_serialize_array_exit(sc->serializes, sc->serialize_cnt, slz);
4135 igb_tryserialize(struct ifnet *ifp, enum ifnet_serialize slz)
4137 struct igb_softc *sc = ifp->if_softc;
4139 return ifnet_serialize_array_try(sc->serializes, sc->serialize_cnt,
4146 igb_serialize_assert(struct ifnet *ifp, enum ifnet_serialize slz,
4147 boolean_t serialized)
4149 struct igb_softc *sc = ifp->if_softc;
4151 ifnet_serialize_array_assert(sc->serializes, sc->serialize_cnt,
4155 #endif /* INVARIANTS */
4158 igb_set_intr_mask(struct igb_softc *sc)
4162 sc->intr_mask = sc->sts_intr_mask;
4163 for (i = 0; i < sc->rx_ring_inuse; ++i)
4164 sc->intr_mask |= sc->rx_rings[i].rx_intr_mask;
4165 for (i = 0; i < sc->tx_ring_inuse; ++i)
4166 sc->intr_mask |= sc->tx_rings[i].tx_intr_mask;
4168 if_printf(&sc->arpcom.ac_if, "intr mask 0x%08x\n",
4174 igb_alloc_intr(struct igb_softc *sc)
4176 int i, intr_bit, intr_bitmax;
4179 igb_msix_try_alloc(sc);
4180 if (sc->intr_type == PCI_INTR_TYPE_MSIX)
4184 * Allocate MSI/legacy interrupt resource
4186 sc->intr_type = pci_alloc_1intr(sc->dev, igb_msi_enable,
4187 &sc->intr_rid, &intr_flags);
4189 if (sc->intr_type == PCI_INTR_TYPE_LEGACY) {
4192 unshared = device_getenv_int(sc->dev, "irq.unshared", 0);
4194 sc->flags |= IGB_FLAG_SHARED_INTR;
4196 device_printf(sc->dev, "IRQ shared\n");
4198 intr_flags &= ~RF_SHAREABLE;
4200 device_printf(sc->dev, "IRQ unshared\n");
4204 sc->intr_res = bus_alloc_resource_any(sc->dev, SYS_RES_IRQ,
4205 &sc->intr_rid, intr_flags);
4206 if (sc->intr_res == NULL) {
4207 device_printf(sc->dev, "Unable to allocate bus resource: "
4212 for (i = 0; i < sc->tx_ring_cnt; ++i)
4213 sc->tx_rings[i].tx_intr_cpuid = rman_get_cpuid(sc->intr_res);
4216 * Setup MSI/legacy interrupt mask
4218 switch (sc->hw.mac.type) {
4220 intr_bitmax = IGB_MAX_TXRXINT_82575;
4224 intr_bitmax = IGB_MAX_TXRXINT_82576;
4228 intr_bitmax = IGB_MAX_TXRXINT_82580;
4232 intr_bitmax = IGB_MAX_TXRXINT_I350;
4236 intr_bitmax = IGB_MAX_TXRXINT_I354;
4240 intr_bitmax = IGB_MAX_TXRXINT_I210;
4244 intr_bitmax = IGB_MAX_TXRXINT_I211;
4248 intr_bitmax = IGB_MIN_TXRXINT;
4252 for (i = 0; i < sc->tx_ring_cnt; ++i)
4253 igb_set_txintr_mask(&sc->tx_rings[i], &intr_bit, intr_bitmax);
4254 for (i = 0; i < sc->rx_ring_cnt; ++i)
4255 igb_set_rxintr_mask(&sc->rx_rings[i], &intr_bit, intr_bitmax);
4256 sc->sts_intr_bit = 0;
4257 sc->sts_intr_mask = E1000_EICR_OTHER;
4259 /* Initialize interrupt rate */
4260 sc->intr_rate = IGB_INTR_RATE;
4262 igb_set_ring_inuse(sc, FALSE);
4263 igb_set_intr_mask(sc);
4268 igb_free_intr(struct igb_softc *sc)
4270 if (sc->intr_type != PCI_INTR_TYPE_MSIX) {
4271 if (sc->intr_res != NULL) {
4272 bus_release_resource(sc->dev, SYS_RES_IRQ, sc->intr_rid,
4275 if (sc->intr_type == PCI_INTR_TYPE_MSI)
4276 pci_release_msi(sc->dev);
4278 igb_msix_free(sc, TRUE);
4283 igb_teardown_intr(struct igb_softc *sc)
4285 if (sc->intr_type != PCI_INTR_TYPE_MSIX)
4286 bus_teardown_intr(sc->dev, sc->intr_res, sc->intr_tag);
4288 igb_msix_teardown(sc, sc->msix_cnt);
4292 igb_msix_try_alloc(struct igb_softc *sc)
4294 int msix_enable, msix_cnt, msix_cnt2, alloc_cnt;
4296 int offset, offset_def;
4297 struct igb_msix_data *msix;
4298 boolean_t aggregate, setup = FALSE;
4301 * Don't enable MSI-X on 82575, see:
4302 * 82575 specification update errata #25
4304 if (sc->hw.mac.type == e1000_82575)
4307 /* Don't enable MSI-X on VF */
4311 msix_enable = device_getenv_int(sc->dev, "msix.enable",
4316 msix_cnt = pci_msix_count(sc->dev);
4317 #ifdef IGB_MSIX_DEBUG
4318 msix_cnt = device_getenv_int(sc->dev, "msix.count", msix_cnt);
4320 if (msix_cnt <= 1) {
4321 /* One MSI-X model does not make sense */
4326 while ((1 << (i + 1)) <= msix_cnt)
4331 device_printf(sc->dev, "MSI-X count %d/%d\n",
4332 msix_cnt2, msix_cnt);
4335 KKASSERT(msix_cnt2 <= msix_cnt);
4336 if (msix_cnt == msix_cnt2) {
4337 /* We need at least one MSI-X for link status */
4339 if (msix_cnt2 <= 1) {
4340 /* One MSI-X for RX/TX does not make sense */
4341 device_printf(sc->dev, "not enough MSI-X for TX/RX, "
4342 "MSI-X count %d/%d\n", msix_cnt2, msix_cnt);
4345 KKASSERT(msix_cnt > msix_cnt2);
4348 device_printf(sc->dev, "MSI-X count fixup %d/%d\n",
4349 msix_cnt2, msix_cnt);
4353 sc->rx_ring_msix = sc->rx_ring_cnt;
4354 if (sc->rx_ring_msix > msix_cnt2)
4355 sc->rx_ring_msix = msix_cnt2;
4357 sc->tx_ring_msix = sc->tx_ring_cnt;
4358 if (sc->tx_ring_msix > msix_cnt2)
4359 sc->tx_ring_msix = msix_cnt2;
4361 if (msix_cnt >= sc->tx_ring_msix + sc->rx_ring_msix + 1) {
4363 * Independent TX/RX MSI-X
4367 device_printf(sc->dev, "independent TX/RX MSI-X\n");
4368 alloc_cnt = sc->tx_ring_msix + sc->rx_ring_msix;
4371 * Aggregate TX/RX MSI-X
4375 device_printf(sc->dev, "aggregate TX/RX MSI-X\n");
4376 alloc_cnt = msix_cnt2;
4377 if (alloc_cnt > ncpus2)
4379 if (sc->rx_ring_msix > alloc_cnt)
4380 sc->rx_ring_msix = alloc_cnt;
4381 if (sc->tx_ring_msix > alloc_cnt)
4382 sc->tx_ring_msix = alloc_cnt;
4384 ++alloc_cnt; /* For link status */
4387 device_printf(sc->dev, "MSI-X alloc %d, "
4388 "RX ring %d, TX ring %d\n", alloc_cnt,
4389 sc->rx_ring_msix, sc->tx_ring_msix);
4392 sc->msix_mem_rid = PCIR_BAR(IGB_MSIX_BAR);
4393 sc->msix_mem_res = bus_alloc_resource_any(sc->dev, SYS_RES_MEMORY,
4394 &sc->msix_mem_rid, RF_ACTIVE);
4395 if (sc->msix_mem_res == NULL) {
4396 sc->msix_mem_rid = PCIR_BAR(IGB_MSIX_BAR_ALT);
4397 sc->msix_mem_res = bus_alloc_resource_any(sc->dev, SYS_RES_MEMORY,
4398 &sc->msix_mem_rid, RF_ACTIVE);
4399 if (sc->msix_mem_res == NULL) {
4400 device_printf(sc->dev, "Unable to map MSI-X table\n");
4405 sc->msix_cnt = alloc_cnt;
4406 sc->msix_data = kmalloc_cachealign(
4407 sizeof(struct igb_msix_data) * sc->msix_cnt,
4408 M_DEVBUF, M_WAITOK | M_ZERO);
4409 for (x = 0; x < sc->msix_cnt; ++x) {
4410 msix = &sc->msix_data[x];
4412 lwkt_serialize_init(&msix->msix_serialize0);
4414 msix->msix_rid = -1;
4415 msix->msix_vector = x;
4416 msix->msix_mask = 1 << msix->msix_vector;
4417 msix->msix_rate = IGB_INTR_RATE;
4425 if (sc->rx_ring_msix == ncpus2) {
4428 offset_def = (sc->rx_ring_msix *
4429 device_get_unit(sc->dev)) % ncpus2;
4431 offset = device_getenv_int(sc->dev,
4432 "msix.rxoff", offset_def);
4433 if (offset >= ncpus2 ||
4434 offset % sc->rx_ring_msix != 0) {
4435 device_printf(sc->dev,
4436 "invalid msix.rxoff %d, use %d\n",
4437 offset, offset_def);
4438 offset = offset_def;
4441 igb_msix_rx_conf(sc, 0, &x, offset);
4446 if (sc->tx_ring_msix == ncpus2) {
4449 offset_def = (sc->tx_ring_msix *
4450 device_get_unit(sc->dev)) % ncpus2;
4452 offset = device_getenv_int(sc->dev,
4453 "msix.txoff", offset_def);
4454 if (offset >= ncpus2 ||
4455 offset % sc->tx_ring_msix != 0) {
4456 device_printf(sc->dev,
4457 "invalid msix.txoff %d, use %d\n",
4458 offset, offset_def);
4459 offset = offset_def;
4462 igb_msix_tx_conf(sc, 0, &x, offset);
4464 int ring_agg, ring_max;
4466 ring_agg = sc->rx_ring_msix;
4467 if (ring_agg > sc->tx_ring_msix)
4468 ring_agg = sc->tx_ring_msix;
4470 ring_max = sc->rx_ring_msix;
4471 if (ring_max < sc->tx_ring_msix)
4472 ring_max = sc->tx_ring_msix;
4474 if (ring_max == ncpus2) {
4477 offset_def = (ring_max * device_get_unit(sc->dev)) %
4480 offset = device_getenv_int(sc->dev, "msix.off",
4482 if (offset >= ncpus2 || offset % ring_max != 0) {
4483 device_printf(sc->dev,
4484 "invalid msix.off %d, use %d\n",
4485 offset, offset_def);
4486 offset = offset_def;
4490 for (i = 0; i < ring_agg; ++i) {
4491 struct igb_tx_ring *txr = &sc->tx_rings[i];
4492 struct igb_rx_ring *rxr = &sc->rx_rings[i];
4494 KKASSERT(x < sc->msix_cnt);
4495 msix = &sc->msix_data[x++];
4497 txr->tx_intr_bit = msix->msix_vector;
4498 txr->tx_intr_mask = msix->msix_mask;
4499 rxr->rx_intr_bit = msix->msix_vector;
4500 rxr->rx_intr_mask = msix->msix_mask;
4502 msix->msix_serialize = &msix->msix_serialize0;
4503 msix->msix_func = igb_msix_rxtx;
4504 msix->msix_arg = msix;
4505 msix->msix_rx = rxr;
4506 msix->msix_tx = txr;
4508 msix->msix_cpuid = i + offset;
4509 KKASSERT(msix->msix_cpuid < ncpus2);
4510 txr->tx_intr_cpuid = msix->msix_cpuid;
4512 ksnprintf(msix->msix_desc, sizeof(msix->msix_desc),
4513 "%s rxtx%d", device_get_nameunit(sc->dev), i);
4514 msix->msix_rate = IGB_MSIX_RX_RATE;
4515 ksnprintf(msix->msix_rate_desc,
4516 sizeof(msix->msix_rate_desc),
4517 "RXTX%d interrupt rate", i);
4520 if (ring_agg != ring_max) {
4521 if (ring_max == sc->tx_ring_msix)
4522 igb_msix_tx_conf(sc, i, &x, offset);
4524 igb_msix_rx_conf(sc, i, &x, offset);
4531 KKASSERT(x < sc->msix_cnt);
4532 msix = &sc->msix_data[x++];
4533 sc->sts_intr_bit = msix->msix_vector;
4534 sc->sts_intr_mask = msix->msix_mask;
4536 msix->msix_serialize = &sc->main_serialize;
4537 msix->msix_func = igb_msix_status;
4538 msix->msix_arg = sc;
4539 msix->msix_cpuid = 0;
4540 ksnprintf(msix->msix_desc, sizeof(msix->msix_desc), "%s sts",
4541 device_get_nameunit(sc->dev));
4542 ksnprintf(msix->msix_rate_desc, sizeof(msix->msix_rate_desc),
4543 "status interrupt rate");
4545 KKASSERT(x == sc->msix_cnt);
4547 error = pci_setup_msix(sc->dev);
4549 device_printf(sc->dev, "Setup MSI-X failed\n");
4554 for (i = 0; i < sc->msix_cnt; ++i) {
4555 msix = &sc->msix_data[i];
4557 error = pci_alloc_msix_vector(sc->dev, msix->msix_vector,
4558 &msix->msix_rid, msix->msix_cpuid);
4560 device_printf(sc->dev,
4561 "Unable to allocate MSI-X %d on cpu%d\n",
4562 msix->msix_vector, msix->msix_cpuid);
4566 msix->msix_res = bus_alloc_resource_any(sc->dev, SYS_RES_IRQ,
4567 &msix->msix_rid, RF_ACTIVE);
4568 if (msix->msix_res == NULL) {
4569 device_printf(sc->dev,
4570 "Unable to allocate MSI-X %d resource\n",
4577 pci_enable_msix(sc->dev);
4578 sc->intr_type = PCI_INTR_TYPE_MSIX;
4581 igb_msix_free(sc, setup);
4585 igb_msix_free(struct igb_softc *sc, boolean_t setup)
4589 KKASSERT(sc->msix_cnt > 1);
4591 for (i = 0; i < sc->msix_cnt; ++i) {
4592 struct igb_msix_data *msix = &sc->msix_data[i];
4594 if (msix->msix_res != NULL) {
4595 bus_release_resource(sc->dev, SYS_RES_IRQ,
4596 msix->msix_rid, msix->msix_res);
4598 if (msix->msix_rid >= 0)
4599 pci_release_msix_vector(sc->dev, msix->msix_rid);
4602 pci_teardown_msix(sc->dev);
4605 kfree(sc->msix_data, M_DEVBUF);
4606 sc->msix_data = NULL;
4610 igb_msix_setup(struct igb_softc *sc)
4614 for (i = 0; i < sc->msix_cnt; ++i) {
4615 struct igb_msix_data *msix = &sc->msix_data[i];
4618 error = bus_setup_intr_descr(sc->dev, msix->msix_res,
4619 INTR_MPSAFE, msix->msix_func, msix->msix_arg,
4620 &msix->msix_handle, msix->msix_serialize, msix->msix_desc);
4622 device_printf(sc->dev, "could not set up %s "
4623 "interrupt handler.\n", msix->msix_desc);
4624 igb_msix_teardown(sc, i);
4632 igb_msix_teardown(struct igb_softc *sc, int msix_cnt)
4636 for (i = 0; i < msix_cnt; ++i) {
4637 struct igb_msix_data *msix = &sc->msix_data[i];
4639 bus_teardown_intr(sc->dev, msix->msix_res, msix->msix_handle);
4644 igb_msix_rx(void *arg)
4646 struct igb_rx_ring *rxr = arg;
4648 ASSERT_SERIALIZED(&rxr->rx_serialize);
4651 E1000_WRITE_REG(&rxr->sc->hw, E1000_EIMS, rxr->rx_intr_mask);
4655 igb_msix_tx(void *arg)
4657 struct igb_tx_ring *txr = arg;
4659 ASSERT_SERIALIZED(&txr->tx_serialize);
4662 if (!ifsq_is_empty(txr->ifsq))
4663 ifsq_devstart(txr->ifsq);
4665 E1000_WRITE_REG(&txr->sc->hw, E1000_EIMS, txr->tx_intr_mask);
4669 igb_msix_status(void *arg)
4671 struct igb_softc *sc = arg;
4674 ASSERT_SERIALIZED(&sc->main_serialize);
4676 icr = E1000_READ_REG(&sc->hw, E1000_ICR);
4677 if (icr & E1000_ICR_LSC) {
4678 sc->hw.mac.get_link_status = 1;
4679 igb_update_link_status(sc);
4682 E1000_WRITE_REG(&sc->hw, E1000_EIMS, sc->sts_intr_mask);
4686 igb_set_ring_inuse(struct igb_softc *sc, boolean_t polling)
4688 sc->rx_ring_inuse = igb_get_rxring_inuse(sc, polling);
4689 sc->tx_ring_inuse = igb_get_txring_inuse(sc, polling);
4691 if_printf(&sc->arpcom.ac_if, "RX rings %d/%d, TX rings %d/%d\n",
4692 sc->rx_ring_inuse, sc->rx_ring_cnt,
4693 sc->tx_ring_inuse, sc->tx_ring_cnt);
4698 igb_get_rxring_inuse(const struct igb_softc *sc, boolean_t polling)
4700 if (!IGB_ENABLE_HWRSS(sc))
4704 return sc->rx_ring_cnt;
4705 else if (sc->intr_type != PCI_INTR_TYPE_MSIX)
4706 return IGB_MIN_RING_RSS;
4708 return sc->rx_ring_msix;
4712 igb_get_txring_inuse(const struct igb_softc *sc, boolean_t polling)
4714 if (!IGB_ENABLE_HWTSS(sc))
4718 return sc->tx_ring_cnt;
4719 else if (sc->intr_type != PCI_INTR_TYPE_MSIX)
4720 return IGB_MIN_RING;
4722 return sc->tx_ring_msix;
4726 igb_tso_pullup(struct igb_tx_ring *txr, struct mbuf **mp)
4728 int hoff, iphlen, thoff;
4732 KASSERT(M_WRITABLE(m), ("TSO mbuf not writable"));
4734 iphlen = m->m_pkthdr.csum_iphlen;
4735 thoff = m->m_pkthdr.csum_thlen;
4736 hoff = m->m_pkthdr.csum_lhlen;
4738 KASSERT(iphlen > 0, ("invalid ip hlen"));
4739 KASSERT(thoff > 0, ("invalid tcp hlen"));
4740 KASSERT(hoff > 0, ("invalid ether hlen"));
4742 if (__predict_false(m->m_len < hoff + iphlen + thoff)) {
4743 m = m_pullup(m, hoff + iphlen + thoff);
4750 if (txr->tx_flags & IGB_TXFLAG_TSO_IPLEN0) {
4753 ip = mtodoff(m, struct ip *, hoff);
4761 igb_tso_ctx(struct igb_tx_ring *txr, struct mbuf *m, uint32_t *hlen)
4763 struct e1000_adv_tx_context_desc *TXD;
4764 uint32_t vlan_macip_lens, type_tucmd_mlhl, mss_l4len_idx;
4765 int hoff, ctxd, iphlen, thoff;
4767 iphlen = m->m_pkthdr.csum_iphlen;
4768 thoff = m->m_pkthdr.csum_thlen;
4769 hoff = m->m_pkthdr.csum_lhlen;
4771 vlan_macip_lens = type_tucmd_mlhl = mss_l4len_idx = 0;
4773 ctxd = txr->next_avail_desc;
4774 TXD = (struct e1000_adv_tx_context_desc *)&txr->tx_base[ctxd];
4776 if (m->m_flags & M_VLANTAG) {
4779 vlantag = htole16(m->m_pkthdr.ether_vlantag);
4780 vlan_macip_lens |= (vlantag << E1000_ADVTXD_VLAN_SHIFT);
4783 vlan_macip_lens |= (hoff << E1000_ADVTXD_MACLEN_SHIFT);
4784 vlan_macip_lens |= iphlen;
4786 type_tucmd_mlhl |= E1000_ADVTXD_DCMD_DEXT | E1000_ADVTXD_DTYP_CTXT;
4787 type_tucmd_mlhl |= E1000_ADVTXD_TUCMD_L4T_TCP;
4788 type_tucmd_mlhl |= E1000_ADVTXD_TUCMD_IPV4;
4790 mss_l4len_idx |= (m->m_pkthdr.tso_segsz << E1000_ADVTXD_MSS_SHIFT);
4791 mss_l4len_idx |= (thoff << E1000_ADVTXD_L4LEN_SHIFT);
4794 * 82575 needs the TX context index added; the queue
4795 * index is used as TX context index here.
4797 if (txr->sc->hw.mac.type == e1000_82575)
4798 mss_l4len_idx |= txr->me << 4;
4800 TXD->vlan_macip_lens = htole32(vlan_macip_lens);
4801 TXD->type_tucmd_mlhl = htole32(type_tucmd_mlhl);
4802 TXD->seqnum_seed = htole32(0);
4803 TXD->mss_l4len_idx = htole32(mss_l4len_idx);
4805 /* We've consumed the first desc, adjust counters */
4806 if (++ctxd == txr->num_tx_desc)
4808 txr->next_avail_desc = ctxd;
4811 *hlen = hoff + iphlen + thoff;
4815 igb_setup_serializer(struct igb_softc *sc)
4817 const struct igb_msix_data *msix;
4821 * Allocate serializer array
4824 /* Main + TX + RX */
4825 sc->serialize_cnt = 1 + sc->tx_ring_cnt + sc->rx_ring_cnt;
4827 /* Aggregate TX/RX MSI-X */
4828 for (i = 0; i < sc->msix_cnt; ++i) {
4829 msix = &sc->msix_data[i];
4830 if (msix->msix_serialize == &msix->msix_serialize0)
4831 sc->serialize_cnt++;
4835 kmalloc(sc->serialize_cnt * sizeof(struct lwkt_serialize *),
4836 M_DEVBUF, M_WAITOK | M_ZERO);
4841 * NOTE: Order is critical
4846 KKASSERT(i < sc->serialize_cnt);
4847 sc->serializes[i++] = &sc->main_serialize;
4849 for (j = 0; j < sc->msix_cnt; ++j) {
4850 msix = &sc->msix_data[j];
4851 if (msix->msix_serialize == &msix->msix_serialize0) {
4852 KKASSERT(i < sc->serialize_cnt);
4853 sc->serializes[i++] = msix->msix_serialize;
4857 for (j = 0; j < sc->tx_ring_cnt; ++j) {
4858 KKASSERT(i < sc->serialize_cnt);
4859 sc->serializes[i++] = &sc->tx_rings[j].tx_serialize;
4862 for (j = 0; j < sc->rx_ring_cnt; ++j) {
4863 KKASSERT(i < sc->serialize_cnt);
4864 sc->serializes[i++] = &sc->rx_rings[j].rx_serialize;
4867 KKASSERT(i == sc->serialize_cnt);
4871 igb_msix_rx_conf(struct igb_softc *sc, int i, int *x0, int offset)
4875 for (; i < sc->rx_ring_msix; ++i) {
4876 struct igb_rx_ring *rxr = &sc->rx_rings[i];
4877 struct igb_msix_data *msix;
4879 KKASSERT(x < sc->msix_cnt);
4880 msix = &sc->msix_data[x++];
4882 rxr->rx_intr_bit = msix->msix_vector;
4883 rxr->rx_intr_mask = msix->msix_mask;
4885 msix->msix_serialize = &rxr->rx_serialize;
4886 msix->msix_func = igb_msix_rx;
4887 msix->msix_arg = rxr;
4889 msix->msix_cpuid = i + offset;
4890 KKASSERT(msix->msix_cpuid < ncpus2);
4892 ksnprintf(msix->msix_desc, sizeof(msix->msix_desc), "%s rx%d",
4893 device_get_nameunit(sc->dev), i);
4895 msix->msix_rate = IGB_MSIX_RX_RATE;
4896 ksnprintf(msix->msix_rate_desc, sizeof(msix->msix_rate_desc),
4897 "RX%d interrupt rate", i);
4903 igb_msix_tx_conf(struct igb_softc *sc, int i, int *x0, int offset)
4907 for (; i < sc->tx_ring_msix; ++i) {
4908 struct igb_tx_ring *txr = &sc->tx_rings[i];
4909 struct igb_msix_data *msix;
4911 KKASSERT(x < sc->msix_cnt);
4912 msix = &sc->msix_data[x++];
4914 txr->tx_intr_bit = msix->msix_vector;
4915 txr->tx_intr_mask = msix->msix_mask;
4917 msix->msix_serialize = &txr->tx_serialize;
4918 msix->msix_func = igb_msix_tx;
4919 msix->msix_arg = txr;
4921 msix->msix_cpuid = i + offset;
4922 KKASSERT(msix->msix_cpuid < ncpus2);
4923 txr->tx_intr_cpuid = msix->msix_cpuid;
4925 ksnprintf(msix->msix_desc, sizeof(msix->msix_desc), "%s tx%d",
4926 device_get_nameunit(sc->dev), i);
4928 msix->msix_rate = IGB_MSIX_TX_RATE;
4929 ksnprintf(msix->msix_rate_desc, sizeof(msix->msix_rate_desc),
4930 "TX%d interrupt rate", i);
4936 igb_msix_rxtx(void *arg)
4938 struct igb_msix_data *msix = arg;
4939 struct igb_rx_ring *rxr = msix->msix_rx;
4940 struct igb_tx_ring *txr = msix->msix_tx;
4942 ASSERT_SERIALIZED(&msix->msix_serialize0);
4944 lwkt_serialize_enter(&rxr->rx_serialize);
4946 lwkt_serialize_exit(&rxr->rx_serialize);
4948 lwkt_serialize_enter(&txr->tx_serialize);
4950 if (!ifsq_is_empty(txr->ifsq))
4951 ifsq_devstart(txr->ifsq);
4952 lwkt_serialize_exit(&txr->tx_serialize);
4954 E1000_WRITE_REG(&msix->msix_sc->hw, E1000_EIMS, msix->msix_mask);
4958 igb_set_timer_cpuid(struct igb_softc *sc, boolean_t polling)
4960 if (polling || sc->intr_type == PCI_INTR_TYPE_MSIX)
4961 sc->timer_cpuid = 0; /* XXX fixed */
4963 sc->timer_cpuid = rman_get_cpuid(sc->intr_res);