2 * Copyright (c) 2004 Joerg Sonnenberger <joerg@bec.de>. All rights reserved.
4 * Copyright (c) 2001-2014, Intel Corporation
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
10 * 1. Redistributions of source code must retain the above copyright notice,
11 * this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived from
19 * this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
34 * Copyright (c) 2005 The DragonFly Project. All rights reserved.
36 * This code is derived from software contributed to The DragonFly Project
37 * by Matthew Dillon <dillon@backplane.com>
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in
47 * the documentation and/or other materials provided with the
49 * 3. Neither the name of The DragonFly Project nor the names of its
50 * contributors may be used to endorse or promote products derived
51 * from this software without specific, prior written permission.
53 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
54 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
55 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
56 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
57 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
58 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
59 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
60 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
61 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
62 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
63 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 * SERIALIZATION API RULES:
70 * - We must call lwkt_serialize_handler_enable() prior to enabling the
71 * hardware interrupt and lwkt_serialize_handler_disable() after disabling
72 * the hardware interrupt in order to avoid handler execution races from
73 * scheduled interrupt threads.
76 #include "opt_ifpoll.h"
78 #include <sys/param.h>
80 #include <sys/endian.h>
81 #include <sys/interrupt.h>
82 #include <sys/kernel.h>
84 #include <sys/malloc.h>
88 #include <sys/serialize.h>
89 #include <sys/socket.h>
90 #include <sys/sockio.h>
91 #include <sys/sysctl.h>
92 #include <sys/systm.h>
95 #include <net/ethernet.h>
97 #include <net/if_arp.h>
98 #include <net/if_dl.h>
99 #include <net/if_media.h>
100 #include <net/if_poll.h>
101 #include <net/ifq_var.h>
102 #include <net/vlan/if_vlan_var.h>
103 #include <net/vlan/if_vlan_ether.h>
105 #include <netinet/ip.h>
106 #include <netinet/tcp.h>
107 #include <netinet/udp.h>
109 #include <bus/pci/pcivar.h>
110 #include <bus/pci/pcireg.h>
112 #include <dev/netif/ig_hal/e1000_api.h>
113 #include <dev/netif/ig_hal/e1000_82571.h>
114 #include <dev/netif/em/if_em.h>
118 #define EM_FLOWCTRL_STRLEN 16
120 #define EM_NAME "Intel(R) PRO/1000 Network Connection "
121 #define EM_VER " 7.4.2"
123 #define _EM_DEVICE(id, ret) \
124 { EM_VENDOR_ID, E1000_DEV_ID_##id, ret, EM_NAME #id EM_VER }
125 #define EM_EMX_DEVICE(id) _EM_DEVICE(id, -100)
126 #define EM_DEVICE(id) _EM_DEVICE(id, 0)
127 #define EM_DEVICE_NULL { 0, 0, 0, NULL }
129 static const struct em_vendor_info em_vendor_info_array[] = {
131 EM_DEVICE(82540EM_LOM),
133 EM_DEVICE(82540EP_LOM),
134 EM_DEVICE(82540EP_LP),
138 EM_DEVICE(82541ER_LOM),
139 EM_DEVICE(82541EI_MOBILE),
141 EM_DEVICE(82541GI_LF),
142 EM_DEVICE(82541GI_MOBILE),
146 EM_DEVICE(82543GC_FIBER),
147 EM_DEVICE(82543GC_COPPER),
149 EM_DEVICE(82544EI_COPPER),
150 EM_DEVICE(82544EI_FIBER),
151 EM_DEVICE(82544GC_COPPER),
152 EM_DEVICE(82544GC_LOM),
154 EM_DEVICE(82545EM_COPPER),
155 EM_DEVICE(82545EM_FIBER),
156 EM_DEVICE(82545GM_COPPER),
157 EM_DEVICE(82545GM_FIBER),
158 EM_DEVICE(82545GM_SERDES),
160 EM_DEVICE(82546EB_COPPER),
161 EM_DEVICE(82546EB_FIBER),
162 EM_DEVICE(82546EB_QUAD_COPPER),
163 EM_DEVICE(82546GB_COPPER),
164 EM_DEVICE(82546GB_FIBER),
165 EM_DEVICE(82546GB_SERDES),
166 EM_DEVICE(82546GB_PCIE),
167 EM_DEVICE(82546GB_QUAD_COPPER),
168 EM_DEVICE(82546GB_QUAD_COPPER_KSP3),
171 EM_DEVICE(82547EI_MOBILE),
174 EM_EMX_DEVICE(82571EB_COPPER),
175 EM_EMX_DEVICE(82571EB_FIBER),
176 EM_EMX_DEVICE(82571EB_SERDES),
177 EM_EMX_DEVICE(82571EB_SERDES_DUAL),
178 EM_EMX_DEVICE(82571EB_SERDES_QUAD),
179 EM_EMX_DEVICE(82571EB_QUAD_COPPER),
180 EM_EMX_DEVICE(82571EB_QUAD_COPPER_BP),
181 EM_EMX_DEVICE(82571EB_QUAD_COPPER_LP),
182 EM_EMX_DEVICE(82571EB_QUAD_FIBER),
183 EM_EMX_DEVICE(82571PT_QUAD_COPPER),
185 EM_EMX_DEVICE(82572EI_COPPER),
186 EM_EMX_DEVICE(82572EI_FIBER),
187 EM_EMX_DEVICE(82572EI_SERDES),
188 EM_EMX_DEVICE(82572EI),
190 EM_EMX_DEVICE(82573E),
191 EM_EMX_DEVICE(82573E_IAMT),
192 EM_EMX_DEVICE(82573L),
196 EM_EMX_DEVICE(80003ES2LAN_COPPER_SPT),
197 EM_EMX_DEVICE(80003ES2LAN_SERDES_SPT),
198 EM_EMX_DEVICE(80003ES2LAN_COPPER_DPT),
199 EM_EMX_DEVICE(80003ES2LAN_SERDES_DPT),
201 EM_DEVICE(ICH8_IGP_M_AMT),
202 EM_DEVICE(ICH8_IGP_AMT),
203 EM_DEVICE(ICH8_IGP_C),
205 EM_DEVICE(ICH8_IFE_GT),
206 EM_DEVICE(ICH8_IFE_G),
207 EM_DEVICE(ICH8_IGP_M),
208 EM_DEVICE(ICH8_82567V_3),
210 EM_DEVICE(ICH9_IGP_M_AMT),
211 EM_DEVICE(ICH9_IGP_AMT),
212 EM_DEVICE(ICH9_IGP_C),
213 EM_DEVICE(ICH9_IGP_M),
214 EM_DEVICE(ICH9_IGP_M_V),
216 EM_DEVICE(ICH9_IFE_GT),
217 EM_DEVICE(ICH9_IFE_G),
220 EM_EMX_DEVICE(82574L),
221 EM_EMX_DEVICE(82574LA),
223 EM_DEVICE(ICH10_R_BM_LM),
224 EM_DEVICE(ICH10_R_BM_LF),
225 EM_DEVICE(ICH10_R_BM_V),
226 EM_DEVICE(ICH10_D_BM_LM),
227 EM_DEVICE(ICH10_D_BM_LF),
228 EM_DEVICE(ICH10_D_BM_V),
230 EM_DEVICE(PCH_M_HV_LM),
231 EM_DEVICE(PCH_M_HV_LC),
232 EM_DEVICE(PCH_D_HV_DM),
233 EM_DEVICE(PCH_D_HV_DC),
235 EM_DEVICE(PCH2_LV_LM),
236 EM_DEVICE(PCH2_LV_V),
238 EM_EMX_DEVICE(PCH_LPT_I217_LM),
239 EM_EMX_DEVICE(PCH_LPT_I217_V),
240 EM_EMX_DEVICE(PCH_LPTLP_I218_LM),
241 EM_EMX_DEVICE(PCH_LPTLP_I218_V),
242 EM_EMX_DEVICE(PCH_I218_LM2),
243 EM_EMX_DEVICE(PCH_I218_V2),
244 EM_EMX_DEVICE(PCH_I218_LM3),
245 EM_EMX_DEVICE(PCH_I218_V3),
247 /* required last entry */
251 static int em_probe(device_t);
252 static int em_attach(device_t);
253 static int em_detach(device_t);
254 static int em_shutdown(device_t);
255 static int em_suspend(device_t);
256 static int em_resume(device_t);
258 static void em_init(void *);
259 static void em_stop(struct adapter *);
260 static int em_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
261 static void em_start(struct ifnet *, struct ifaltq_subque *);
263 static void em_npoll(struct ifnet *, struct ifpoll_info *);
264 static void em_npoll_compat(struct ifnet *, void *, int);
266 static void em_watchdog(struct ifnet *);
267 static void em_media_status(struct ifnet *, struct ifmediareq *);
268 static int em_media_change(struct ifnet *);
269 static void em_timer(void *);
271 static void em_intr(void *);
272 static void em_intr_mask(void *);
273 static void em_intr_body(struct adapter *, boolean_t);
274 static void em_rxeof(struct adapter *, int);
275 static void em_txeof(struct adapter *);
276 static void em_tx_collect(struct adapter *);
277 static void em_tx_purge(struct adapter *);
278 static void em_enable_intr(struct adapter *);
279 static void em_disable_intr(struct adapter *);
281 static int em_dma_malloc(struct adapter *, bus_size_t,
282 struct em_dma_alloc *);
283 static void em_dma_free(struct adapter *, struct em_dma_alloc *);
284 static void em_init_tx_ring(struct adapter *);
285 static int em_init_rx_ring(struct adapter *);
286 static int em_create_tx_ring(struct adapter *);
287 static int em_create_rx_ring(struct adapter *);
288 static void em_destroy_tx_ring(struct adapter *, int);
289 static void em_destroy_rx_ring(struct adapter *, int);
290 static int em_newbuf(struct adapter *, int, int);
291 static int em_encap(struct adapter *, struct mbuf **, int *, int *);
292 static void em_rxcsum(struct adapter *, struct e1000_rx_desc *,
294 static int em_txcsum(struct adapter *, struct mbuf *,
295 uint32_t *, uint32_t *);
296 static int em_tso_pullup(struct adapter *, struct mbuf **);
297 static int em_tso_setup(struct adapter *, struct mbuf *,
298 uint32_t *, uint32_t *);
300 static int em_get_hw_info(struct adapter *);
301 static int em_is_valid_eaddr(const uint8_t *);
302 static int em_alloc_pci_res(struct adapter *);
303 static void em_free_pci_res(struct adapter *);
304 static int em_reset(struct adapter *);
305 static void em_setup_ifp(struct adapter *);
306 static void em_init_tx_unit(struct adapter *);
307 static void em_init_rx_unit(struct adapter *);
308 static void em_update_stats(struct adapter *);
309 static void em_set_promisc(struct adapter *);
310 static void em_disable_promisc(struct adapter *);
311 static void em_set_multi(struct adapter *);
312 static void em_update_link_status(struct adapter *);
313 static void em_smartspeed(struct adapter *);
314 static void em_set_itr(struct adapter *, uint32_t);
315 static void em_disable_aspm(struct adapter *);
316 static enum e1000_fc_mode em_str2fc(const char *);
317 static void em_fc2str(enum e1000_fc_mode, char *, int);
319 /* Hardware workarounds */
320 static int em_82547_fifo_workaround(struct adapter *, int);
321 static void em_82547_update_fifo_head(struct adapter *, int);
322 static int em_82547_tx_fifo_reset(struct adapter *);
323 static void em_82547_move_tail(void *);
324 static void em_82547_move_tail_serialized(struct adapter *);
325 static uint32_t em_82544_fill_desc(bus_addr_t, uint32_t, PDESC_ARRAY);
327 static void em_print_debug_info(struct adapter *);
328 static void em_print_nvm_info(struct adapter *);
329 static void em_print_hw_stats(struct adapter *);
331 static int em_sysctl_stats(SYSCTL_HANDLER_ARGS);
332 static int em_sysctl_debug_info(SYSCTL_HANDLER_ARGS);
333 static int em_sysctl_int_throttle(SYSCTL_HANDLER_ARGS);
334 static int em_sysctl_int_tx_nsegs(SYSCTL_HANDLER_ARGS);
335 static int em_sysctl_flowctrl(SYSCTL_HANDLER_ARGS);
336 static void em_add_sysctl(struct adapter *adapter);
338 /* Management and WOL Support */
339 static void em_get_mgmt(struct adapter *);
340 static void em_rel_mgmt(struct adapter *);
341 static void em_get_hw_control(struct adapter *);
342 static void em_rel_hw_control(struct adapter *);
343 static void em_enable_wol(device_t);
345 static device_method_t em_methods[] = {
346 /* Device interface */
347 DEVMETHOD(device_probe, em_probe),
348 DEVMETHOD(device_attach, em_attach),
349 DEVMETHOD(device_detach, em_detach),
350 DEVMETHOD(device_shutdown, em_shutdown),
351 DEVMETHOD(device_suspend, em_suspend),
352 DEVMETHOD(device_resume, em_resume),
356 static driver_t em_driver = {
359 sizeof(struct adapter),
362 static devclass_t em_devclass;
364 DECLARE_DUMMY_MODULE(if_em);
365 MODULE_DEPEND(em, ig_hal, 1, 1, 1);
366 DRIVER_MODULE(if_em, pci, em_driver, em_devclass, NULL, NULL);
371 static int em_int_throttle_ceil = EM_DEFAULT_ITR;
372 static int em_rxd = EM_DEFAULT_RXD;
373 static int em_txd = EM_DEFAULT_TXD;
374 static int em_smart_pwr_down = 0;
376 /* Controls whether promiscuous also shows bad packets */
377 static int em_debug_sbp = FALSE;
379 static int em_82573_workaround = 1;
380 static int em_msi_enable = 1;
382 static char em_flowctrl[EM_FLOWCTRL_STRLEN] = "rx_pause";
384 TUNABLE_INT("hw.em.int_throttle_ceil", &em_int_throttle_ceil);
385 TUNABLE_INT("hw.em.rxd", &em_rxd);
386 TUNABLE_INT("hw.em.txd", &em_txd);
387 TUNABLE_INT("hw.em.smart_pwr_down", &em_smart_pwr_down);
388 TUNABLE_INT("hw.em.sbp", &em_debug_sbp);
389 TUNABLE_INT("hw.em.82573_workaround", &em_82573_workaround);
390 TUNABLE_INT("hw.em.msi.enable", &em_msi_enable);
391 TUNABLE_STR("hw.em.flow_ctrl", em_flowctrl, sizeof(em_flowctrl));
393 /* Global used in WOL setup with multiport cards */
394 static int em_global_quad_port_a = 0;
396 /* Set this to one to display debug statistics */
397 static int em_display_debug_stats = 0;
399 #if !defined(KTR_IF_EM)
400 #define KTR_IF_EM KTR_ALL
402 KTR_INFO_MASTER(if_em);
403 KTR_INFO(KTR_IF_EM, if_em, intr_beg, 0, "intr begin");
404 KTR_INFO(KTR_IF_EM, if_em, intr_end, 1, "intr end");
405 KTR_INFO(KTR_IF_EM, if_em, pkt_receive, 4, "rx packet");
406 KTR_INFO(KTR_IF_EM, if_em, pkt_txqueue, 5, "tx packet");
407 KTR_INFO(KTR_IF_EM, if_em, pkt_txclean, 6, "tx clean");
408 #define logif(name) KTR_LOG(if_em_ ## name)
411 em_probe(device_t dev)
413 const struct em_vendor_info *ent;
416 vid = pci_get_vendor(dev);
417 did = pci_get_device(dev);
419 for (ent = em_vendor_info_array; ent->desc != NULL; ++ent) {
420 if (vid == ent->vendor_id && did == ent->device_id) {
421 device_set_desc(dev, ent->desc);
422 device_set_async_attach(dev, TRUE);
430 em_attach(device_t dev)
432 struct adapter *adapter = device_get_softc(dev);
433 struct ifnet *ifp = &adapter->arpcom.ac_if;
436 uint16_t eeprom_data, device_id, apme_mask;
437 driver_intr_t *intr_func;
438 char flowctrl[EM_FLOWCTRL_STRLEN];
440 adapter->dev = adapter->osdep.dev = dev;
442 callout_init_mp(&adapter->timer);
443 callout_init_mp(&adapter->tx_fifo_timer);
445 ifmedia_init(&adapter->media, IFM_IMASK,
446 em_media_change, em_media_status);
448 /* Determine hardware and mac info */
449 error = em_get_hw_info(adapter);
451 device_printf(dev, "Identify hardware failed\n");
455 /* Setup PCI resources */
456 error = em_alloc_pci_res(adapter);
458 device_printf(dev, "Allocation of PCI resources failed\n");
463 * For ICH8 and family we need to map the flash memory,
464 * and this must happen after the MAC is identified.
466 if (adapter->hw.mac.type == e1000_ich8lan ||
467 adapter->hw.mac.type == e1000_ich9lan ||
468 adapter->hw.mac.type == e1000_ich10lan ||
469 adapter->hw.mac.type == e1000_pchlan ||
470 adapter->hw.mac.type == e1000_pch2lan ||
471 adapter->hw.mac.type == e1000_pch_lpt) {
472 adapter->flash_rid = EM_BAR_FLASH;
474 adapter->flash = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
475 &adapter->flash_rid, RF_ACTIVE);
476 if (adapter->flash == NULL) {
477 device_printf(dev, "Mapping of Flash failed\n");
481 adapter->osdep.flash_bus_space_tag =
482 rman_get_bustag(adapter->flash);
483 adapter->osdep.flash_bus_space_handle =
484 rman_get_bushandle(adapter->flash);
487 * This is used in the shared code
488 * XXX this goof is actually not used.
490 adapter->hw.flash_address = (uint8_t *)adapter->flash;
493 switch (adapter->hw.mac.type) {
498 * Pullup extra 4bytes into the first data segment for
500 * 82571/82572 specification update errata #7
502 * Same applies to I217 (and maybe I218).
505 * 4bytes instead of 2bytes, which are mentioned in the
506 * errata, are pulled; mainly to keep rest of the data
509 adapter->flags |= EM_FLAG_TSO_PULLEX;
513 if (pci_is_pcie(dev))
514 adapter->flags |= EM_FLAG_TSO;
518 /* Do Shared Code initialization */
519 if (e1000_setup_init_funcs(&adapter->hw, TRUE)) {
520 device_printf(dev, "Setup of Shared code failed\n");
525 e1000_get_bus_info(&adapter->hw);
528 * Validate number of transmit and receive descriptors. It
529 * must not exceed hardware maximum, and must be multiple
530 * of E1000_DBA_ALIGN.
532 if ((em_txd * sizeof(struct e1000_tx_desc)) % EM_DBA_ALIGN != 0 ||
533 (adapter->hw.mac.type >= e1000_82544 && em_txd > EM_MAX_TXD) ||
534 (adapter->hw.mac.type < e1000_82544 && em_txd > EM_MAX_TXD_82543) ||
535 em_txd < EM_MIN_TXD) {
536 if (adapter->hw.mac.type < e1000_82544)
537 adapter->num_tx_desc = EM_MAX_TXD_82543;
539 adapter->num_tx_desc = EM_DEFAULT_TXD;
540 device_printf(dev, "Using %d TX descriptors instead of %d!\n",
541 adapter->num_tx_desc, em_txd);
543 adapter->num_tx_desc = em_txd;
545 if ((em_rxd * sizeof(struct e1000_rx_desc)) % EM_DBA_ALIGN != 0 ||
546 (adapter->hw.mac.type >= e1000_82544 && em_rxd > EM_MAX_RXD) ||
547 (adapter->hw.mac.type < e1000_82544 && em_rxd > EM_MAX_RXD_82543) ||
548 em_rxd < EM_MIN_RXD) {
549 if (adapter->hw.mac.type < e1000_82544)
550 adapter->num_rx_desc = EM_MAX_RXD_82543;
552 adapter->num_rx_desc = EM_DEFAULT_RXD;
553 device_printf(dev, "Using %d RX descriptors instead of %d!\n",
554 adapter->num_rx_desc, em_rxd);
556 adapter->num_rx_desc = em_rxd;
559 adapter->hw.mac.autoneg = DO_AUTO_NEG;
560 adapter->hw.phy.autoneg_wait_to_complete = FALSE;
561 adapter->hw.phy.autoneg_advertised = AUTONEG_ADV_DEFAULT;
562 adapter->rx_buffer_len = MCLBYTES;
565 * Interrupt throttle rate
567 if (em_int_throttle_ceil == 0) {
568 adapter->int_throttle_ceil = 0;
570 int throttle = em_int_throttle_ceil;
573 throttle = EM_DEFAULT_ITR;
575 /* Recalculate the tunable value to get the exact frequency. */
576 throttle = 1000000000 / 256 / throttle;
578 /* Upper 16bits of ITR is reserved and should be zero */
579 if (throttle & 0xffff0000)
580 throttle = 1000000000 / 256 / EM_DEFAULT_ITR;
582 adapter->int_throttle_ceil = 1000000000 / 256 / throttle;
585 e1000_init_script_state_82541(&adapter->hw, TRUE);
586 e1000_set_tbi_compatibility_82543(&adapter->hw, TRUE);
589 if (adapter->hw.phy.media_type == e1000_media_type_copper) {
590 adapter->hw.phy.mdix = AUTO_ALL_MODES;
591 adapter->hw.phy.disable_polarity_correction = FALSE;
592 adapter->hw.phy.ms_type = EM_MASTER_SLAVE;
595 /* Set the frame limits assuming standard ethernet sized frames. */
596 adapter->hw.mac.max_frame_size =
597 ETHERMTU + ETHER_HDR_LEN + ETHER_CRC_LEN;
598 adapter->min_frame_size = ETH_ZLEN + ETHER_CRC_LEN;
600 /* This controls when hardware reports transmit completion status. */
601 adapter->hw.mac.report_tx_early = 1;
604 * Create top level busdma tag
606 error = bus_dma_tag_create(NULL, 1, 0,
607 BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
609 BUS_SPACE_MAXSIZE_32BIT, 0, BUS_SPACE_MAXSIZE_32BIT,
610 0, &adapter->parent_dtag);
612 device_printf(dev, "could not create top level DMA tag\n");
617 * Allocate Transmit Descriptor ring
619 tsize = roundup2(adapter->num_tx_desc * sizeof(struct e1000_tx_desc),
621 error = em_dma_malloc(adapter, tsize, &adapter->txdma);
623 device_printf(dev, "Unable to allocate tx_desc memory\n");
626 adapter->tx_desc_base = adapter->txdma.dma_vaddr;
629 * Allocate Receive Descriptor ring
631 rsize = roundup2(adapter->num_rx_desc * sizeof(struct e1000_rx_desc),
633 error = em_dma_malloc(adapter, rsize, &adapter->rxdma);
635 device_printf(dev, "Unable to allocate rx_desc memory\n");
638 adapter->rx_desc_base = adapter->rxdma.dma_vaddr;
640 /* Allocate multicast array memory. */
641 adapter->mta = kmalloc(ETH_ADDR_LEN * MAX_NUM_MULTICAST_ADDRESSES,
644 /* Indicate SOL/IDER usage */
645 if (e1000_check_reset_block(&adapter->hw)) {
647 "PHY reset is blocked due to SOL/IDER session.\n");
651 adapter->hw.dev_spec.ich8lan.eee_disable = 1;
654 * Start from a known state, this is important in reading the
655 * nvm and mac from that.
657 e1000_reset_hw(&adapter->hw);
659 /* Make sure we have a good EEPROM before we read from it */
660 if (e1000_validate_nvm_checksum(&adapter->hw) < 0) {
662 * Some PCI-E parts fail the first check due to
663 * the link being in sleep state, call it again,
664 * if it fails a second time its a real issue.
666 if (e1000_validate_nvm_checksum(&adapter->hw) < 0) {
668 "The EEPROM Checksum Is Not Valid\n");
674 /* Copy the permanent MAC address out of the EEPROM */
675 if (e1000_read_mac_addr(&adapter->hw) < 0) {
676 device_printf(dev, "EEPROM read error while reading MAC"
681 if (!em_is_valid_eaddr(adapter->hw.mac.addr)) {
682 device_printf(dev, "Invalid MAC address\n");
687 /* Disable ULP support */
688 e1000_disable_ulp_lpt_lp(&adapter->hw, TRUE);
690 /* Allocate transmit descriptors and buffers */
691 error = em_create_tx_ring(adapter);
693 device_printf(dev, "Could not setup transmit structures\n");
697 /* Allocate receive descriptors and buffers */
698 error = em_create_rx_ring(adapter);
700 device_printf(dev, "Could not setup receive structures\n");
704 /* Manually turn off all interrupts */
705 E1000_WRITE_REG(&adapter->hw, E1000_IMC, 0xffffffff);
707 /* Determine if we have to control management hardware */
708 if (e1000_enable_mng_pass_thru(&adapter->hw))
709 adapter->flags |= EM_FLAG_HAS_MGMT;
714 apme_mask = EM_EEPROM_APME;
716 switch (adapter->hw.mac.type) {
723 adapter->flags |= EM_FLAG_HAS_AMT;
727 case e1000_82546_rev_3:
730 case e1000_80003es2lan:
731 if (adapter->hw.bus.func == 1) {
732 e1000_read_nvm(&adapter->hw,
733 NVM_INIT_CONTROL3_PORT_B, 1, &eeprom_data);
735 e1000_read_nvm(&adapter->hw,
736 NVM_INIT_CONTROL3_PORT_A, 1, &eeprom_data);
745 apme_mask = E1000_WUC_APME;
746 adapter->flags |= EM_FLAG_HAS_AMT;
747 eeprom_data = E1000_READ_REG(&adapter->hw, E1000_WUC);
751 e1000_read_nvm(&adapter->hw,
752 NVM_INIT_CONTROL3_PORT_A, 1, &eeprom_data);
755 if (eeprom_data & apme_mask)
756 adapter->wol = E1000_WUFC_MAG | E1000_WUFC_MC;
759 * We have the eeprom settings, now apply the special cases
760 * where the eeprom may be wrong or the board won't support
761 * wake on lan on a particular port
763 device_id = pci_get_device(dev);
765 case E1000_DEV_ID_82546GB_PCIE:
769 case E1000_DEV_ID_82546EB_FIBER:
770 case E1000_DEV_ID_82546GB_FIBER:
771 case E1000_DEV_ID_82571EB_FIBER:
773 * Wake events only supported on port A for dual fiber
774 * regardless of eeprom setting
776 if (E1000_READ_REG(&adapter->hw, E1000_STATUS) &
781 case E1000_DEV_ID_82546GB_QUAD_COPPER_KSP3:
782 case E1000_DEV_ID_82571EB_QUAD_COPPER:
783 case E1000_DEV_ID_82571EB_QUAD_FIBER:
784 case E1000_DEV_ID_82571EB_QUAD_COPPER_LP:
785 /* if quad port adapter, disable WoL on all but port A */
786 if (em_global_quad_port_a != 0)
788 /* Reset for multiple quad port adapters */
789 if (++em_global_quad_port_a == 4)
790 em_global_quad_port_a = 0;
794 /* XXX disable wol */
797 /* Setup flow control. */
798 device_getenv_string(dev, "flow_ctrl", flowctrl, sizeof(flowctrl),
800 adapter->flow_ctrl = em_str2fc(flowctrl);
801 if (adapter->hw.mac.type == e1000_pchlan) {
802 /* Only pause reception is supported */
803 adapter->flow_ctrl = e1000_fc_rx_pause;
806 /* Setup OS specific network interface */
807 em_setup_ifp(adapter);
809 /* Add sysctl tree, must after em_setup_ifp() */
810 em_add_sysctl(adapter);
814 ifpoll_compat_setup(&adapter->npoll,
815 device_get_sysctl_ctx(dev), device_get_sysctl_tree(dev),
816 device_get_unit(dev), ifp->if_serializer);
819 /* Reset the hardware */
820 error = em_reset(adapter);
823 * Some 82573 parts fail the first reset, call it again,
824 * if it fails a second time its a real issue.
826 error = em_reset(adapter);
828 device_printf(dev, "Unable to reset the hardware\n");
834 /* Initialize statistics */
835 em_update_stats(adapter);
837 adapter->hw.mac.get_link_status = 1;
838 em_update_link_status(adapter);
840 /* Do we need workaround for 82544 PCI-X adapter? */
841 if (adapter->hw.bus.type == e1000_bus_type_pcix &&
842 adapter->hw.mac.type == e1000_82544)
843 adapter->pcix_82544 = TRUE;
845 adapter->pcix_82544 = FALSE;
847 if (adapter->pcix_82544) {
849 * 82544 on PCI-X may split one TX segment
850 * into two TX descs, so we double its number
851 * of spare TX desc here.
853 adapter->spare_tx_desc = 2 * EM_TX_SPARE;
855 adapter->spare_tx_desc = EM_TX_SPARE;
857 if (adapter->flags & EM_FLAG_TSO)
858 adapter->spare_tx_desc = EM_TX_SPARE_TSO;
859 adapter->tx_wreg_nsegs = EM_DEFAULT_TXWREG;
862 * Keep following relationship between spare_tx_desc, oact_tx_desc
864 * (spare_tx_desc + EM_TX_RESERVED) <=
865 * oact_tx_desc <= EM_TX_OACTIVE_MAX <= tx_int_nsegs
867 adapter->oact_tx_desc = adapter->num_tx_desc / 8;
868 if (adapter->oact_tx_desc > EM_TX_OACTIVE_MAX)
869 adapter->oact_tx_desc = EM_TX_OACTIVE_MAX;
870 if (adapter->oact_tx_desc < adapter->spare_tx_desc + EM_TX_RESERVED)
871 adapter->oact_tx_desc = adapter->spare_tx_desc + EM_TX_RESERVED;
873 adapter->tx_int_nsegs = adapter->num_tx_desc / 16;
874 if (adapter->tx_int_nsegs < adapter->oact_tx_desc)
875 adapter->tx_int_nsegs = adapter->oact_tx_desc;
877 /* Non-AMT based hardware can now take control from firmware */
878 if ((adapter->flags & (EM_FLAG_HAS_MGMT | EM_FLAG_HAS_AMT)) ==
879 EM_FLAG_HAS_MGMT && adapter->hw.mac.type >= e1000_82571)
880 em_get_hw_control(adapter);
882 ifq_set_cpuid(&ifp->if_snd, rman_get_cpuid(adapter->intr_res));
885 * Missing Interrupt Following ICR read:
887 * 82571/82572 specification update errata #76
888 * 82573 specification update errata #31
889 * 82574 specification update errata #12
890 * 82583 specification update errata #4
893 if ((adapter->flags & EM_FLAG_SHARED_INTR) &&
894 (adapter->hw.mac.type == e1000_82571 ||
895 adapter->hw.mac.type == e1000_82572 ||
896 adapter->hw.mac.type == e1000_82573 ||
897 adapter->hw.mac.type == e1000_82574 ||
898 adapter->hw.mac.type == e1000_82583))
899 intr_func = em_intr_mask;
901 error = bus_setup_intr(dev, adapter->intr_res, INTR_MPSAFE,
902 intr_func, adapter, &adapter->intr_tag,
905 device_printf(dev, "Failed to register interrupt handler");
916 em_detach(device_t dev)
918 struct adapter *adapter = device_get_softc(dev);
920 if (device_is_attached(dev)) {
921 struct ifnet *ifp = &adapter->arpcom.ac_if;
923 lwkt_serialize_enter(ifp->if_serializer);
927 e1000_phy_hw_reset(&adapter->hw);
929 em_rel_mgmt(adapter);
930 em_rel_hw_control(adapter);
933 E1000_WRITE_REG(&adapter->hw, E1000_WUC,
935 E1000_WRITE_REG(&adapter->hw, E1000_WUFC, adapter->wol);
939 bus_teardown_intr(dev, adapter->intr_res, adapter->intr_tag);
941 lwkt_serialize_exit(ifp->if_serializer);
944 } else if (adapter->memory != NULL) {
945 em_rel_hw_control(adapter);
948 ifmedia_removeall(&adapter->media);
949 bus_generic_detach(dev);
951 em_free_pci_res(adapter);
953 em_destroy_tx_ring(adapter, adapter->num_tx_desc);
954 em_destroy_rx_ring(adapter, adapter->num_rx_desc);
956 /* Free Transmit Descriptor ring */
957 if (adapter->tx_desc_base)
958 em_dma_free(adapter, &adapter->txdma);
960 /* Free Receive Descriptor ring */
961 if (adapter->rx_desc_base)
962 em_dma_free(adapter, &adapter->rxdma);
964 /* Free top level busdma tag */
965 if (adapter->parent_dtag != NULL)
966 bus_dma_tag_destroy(adapter->parent_dtag);
968 if (adapter->mta != NULL)
969 kfree(adapter->mta, M_DEVBUF);
975 em_shutdown(device_t dev)
977 return em_suspend(dev);
981 em_suspend(device_t dev)
983 struct adapter *adapter = device_get_softc(dev);
984 struct ifnet *ifp = &adapter->arpcom.ac_if;
986 lwkt_serialize_enter(ifp->if_serializer);
990 em_rel_mgmt(adapter);
991 em_rel_hw_control(adapter);
994 E1000_WRITE_REG(&adapter->hw, E1000_WUC, E1000_WUC_PME_EN);
995 E1000_WRITE_REG(&adapter->hw, E1000_WUFC, adapter->wol);
999 lwkt_serialize_exit(ifp->if_serializer);
1001 return bus_generic_suspend(dev);
1005 em_resume(device_t dev)
1007 struct adapter *adapter = device_get_softc(dev);
1008 struct ifnet *ifp = &adapter->arpcom.ac_if;
1010 lwkt_serialize_enter(ifp->if_serializer);
1012 if (adapter->hw.mac.type == e1000_pch2lan)
1013 e1000_resume_workarounds_pchlan(&adapter->hw);
1016 em_get_mgmt(adapter);
1019 lwkt_serialize_exit(ifp->if_serializer);
1021 return bus_generic_resume(dev);
1025 em_start(struct ifnet *ifp, struct ifaltq_subque *ifsq)
1027 struct adapter *adapter = ifp->if_softc;
1028 struct mbuf *m_head;
1029 int idx = -1, nsegs = 0;
1031 ASSERT_ALTQ_SQ_DEFAULT(ifp, ifsq);
1032 ASSERT_SERIALIZED(ifp->if_serializer);
1034 if ((ifp->if_flags & IFF_RUNNING) == 0 || ifq_is_oactive(&ifp->if_snd))
1037 if (!adapter->link_active) {
1038 ifq_purge(&ifp->if_snd);
1042 while (!ifq_is_empty(&ifp->if_snd)) {
1043 /* Now do we at least have a minimal? */
1044 if (EM_IS_OACTIVE(adapter)) {
1045 em_tx_collect(adapter);
1046 if (EM_IS_OACTIVE(adapter)) {
1047 ifq_set_oactive(&ifp->if_snd);
1048 adapter->no_tx_desc_avail1++;
1054 m_head = ifq_dequeue(&ifp->if_snd);
1058 if (em_encap(adapter, &m_head, &nsegs, &idx)) {
1059 IFNET_STAT_INC(ifp, oerrors, 1);
1060 em_tx_collect(adapter);
1065 * TX interrupt are aggressively aggregated, so increasing
1066 * opackets at TX interrupt time will make the opackets
1067 * statistics vastly inaccurate; we do the opackets increment
1070 IFNET_STAT_INC(ifp, opackets, 1);
1072 if (nsegs >= adapter->tx_wreg_nsegs && idx >= 0) {
1073 E1000_WRITE_REG(&adapter->hw, E1000_TDT(0), idx);
1078 /* Send a copy of the frame to the BPF listener */
1079 ETHER_BPF_MTAP(ifp, m_head);
1081 /* Set timeout in case hardware has problems transmitting. */
1082 ifp->if_timer = EM_TX_TIMEOUT;
1085 E1000_WRITE_REG(&adapter->hw, E1000_TDT(0), idx);
1089 em_ioctl(struct ifnet *ifp, u_long command, caddr_t data, struct ucred *cr)
1091 struct adapter *adapter = ifp->if_softc;
1092 struct ifreq *ifr = (struct ifreq *)data;
1093 uint16_t eeprom_data = 0;
1094 int max_frame_size, mask, reinit;
1097 ASSERT_SERIALIZED(ifp->if_serializer);
1101 switch (adapter->hw.mac.type) {
1104 * 82573 only supports jumbo frames
1105 * if ASPM is disabled.
1107 e1000_read_nvm(&adapter->hw,
1108 NVM_INIT_3GIO_3, 1, &eeprom_data);
1109 if (eeprom_data & NVM_WORD1A_ASPM_MASK) {
1110 max_frame_size = ETHER_MAX_LEN;
1115 /* Limit Jumbo Frame size */
1119 case e1000_ich10lan:
1124 case e1000_80003es2lan:
1125 max_frame_size = 9234;
1129 max_frame_size = 4096;
1132 /* Adapters that do not support jumbo frames */
1135 max_frame_size = ETHER_MAX_LEN;
1139 max_frame_size = MAX_JUMBO_FRAME_SIZE;
1142 if (ifr->ifr_mtu > max_frame_size - ETHER_HDR_LEN -
1148 ifp->if_mtu = ifr->ifr_mtu;
1149 adapter->hw.mac.max_frame_size =
1150 ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN;
1152 if (ifp->if_flags & IFF_RUNNING)
1157 if (ifp->if_flags & IFF_UP) {
1158 if ((ifp->if_flags & IFF_RUNNING)) {
1159 if ((ifp->if_flags ^ adapter->if_flags) &
1160 (IFF_PROMISC | IFF_ALLMULTI)) {
1161 em_disable_promisc(adapter);
1162 em_set_promisc(adapter);
1167 } else if (ifp->if_flags & IFF_RUNNING) {
1170 adapter->if_flags = ifp->if_flags;
1175 if (ifp->if_flags & IFF_RUNNING) {
1176 em_disable_intr(adapter);
1177 em_set_multi(adapter);
1178 if (adapter->hw.mac.type == e1000_82542 &&
1179 adapter->hw.revision_id == E1000_REVISION_2)
1180 em_init_rx_unit(adapter);
1181 #ifdef IFPOLL_ENABLE
1182 if (!(ifp->if_flags & IFF_NPOLLING))
1184 em_enable_intr(adapter);
1189 /* Check SOL/IDER usage */
1190 if (e1000_check_reset_block(&adapter->hw)) {
1191 device_printf(adapter->dev, "Media change is"
1192 " blocked due to SOL/IDER session.\n");
1198 error = ifmedia_ioctl(ifp, ifr, &adapter->media, command);
1203 mask = ifr->ifr_reqcap ^ ifp->if_capenable;
1204 if (mask & IFCAP_RXCSUM) {
1205 ifp->if_capenable ^= IFCAP_RXCSUM;
1208 if (mask & IFCAP_TXCSUM) {
1209 ifp->if_capenable ^= IFCAP_TXCSUM;
1210 if (ifp->if_capenable & IFCAP_TXCSUM)
1211 ifp->if_hwassist |= EM_CSUM_FEATURES;
1213 ifp->if_hwassist &= ~EM_CSUM_FEATURES;
1215 if (mask & IFCAP_TSO) {
1216 ifp->if_capenable ^= IFCAP_TSO;
1217 if (ifp->if_capenable & IFCAP_TSO)
1218 ifp->if_hwassist |= CSUM_TSO;
1220 ifp->if_hwassist &= ~CSUM_TSO;
1222 if (mask & IFCAP_VLAN_HWTAGGING) {
1223 ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
1226 if (reinit && (ifp->if_flags & IFF_RUNNING))
1231 error = ether_ioctl(ifp, command, data);
1238 em_watchdog(struct ifnet *ifp)
1240 struct adapter *adapter = ifp->if_softc;
1242 ASSERT_SERIALIZED(ifp->if_serializer);
1245 * The timer is set to 5 every time start queues a packet.
1246 * Then txeof keeps resetting it as long as it cleans at
1247 * least one descriptor.
1248 * Finally, anytime all descriptors are clean the timer is
1252 if (E1000_READ_REG(&adapter->hw, E1000_TDT(0)) ==
1253 E1000_READ_REG(&adapter->hw, E1000_TDH(0))) {
1255 * If we reach here, all TX jobs are completed and
1256 * the TX engine should have been idled for some time.
1257 * We don't need to call if_devstart() here.
1259 ifq_clr_oactive(&ifp->if_snd);
1265 * If we are in this routine because of pause frames, then
1266 * don't reset the hardware.
1268 if (E1000_READ_REG(&adapter->hw, E1000_STATUS) &
1269 E1000_STATUS_TXOFF) {
1270 ifp->if_timer = EM_TX_TIMEOUT;
1274 if (e1000_check_for_link(&adapter->hw) == 0)
1275 if_printf(ifp, "watchdog timeout -- resetting\n");
1277 IFNET_STAT_INC(ifp, oerrors, 1);
1278 adapter->watchdog_events++;
1282 if (!ifq_is_empty(&ifp->if_snd))
1289 struct adapter *adapter = xsc;
1290 struct ifnet *ifp = &adapter->arpcom.ac_if;
1291 device_t dev = adapter->dev;
1293 ASSERT_SERIALIZED(ifp->if_serializer);
1297 /* Get the latest mac address, User can use a LAA */
1298 bcopy(IF_LLADDR(ifp), adapter->hw.mac.addr, ETHER_ADDR_LEN);
1300 /* Put the address into the Receive Address Array */
1301 e1000_rar_set(&adapter->hw, adapter->hw.mac.addr, 0);
1304 * With the 82571 adapter, RAR[0] may be overwritten
1305 * when the other port is reset, we make a duplicate
1306 * in RAR[14] for that eventuality, this assures
1307 * the interface continues to function.
1309 if (adapter->hw.mac.type == e1000_82571) {
1310 e1000_set_laa_state_82571(&adapter->hw, TRUE);
1311 e1000_rar_set(&adapter->hw, adapter->hw.mac.addr,
1312 E1000_RAR_ENTRIES - 1);
1315 /* Reset the hardware */
1316 if (em_reset(adapter)) {
1317 device_printf(dev, "Unable to reset the hardware\n");
1318 /* XXX em_stop()? */
1321 em_update_link_status(adapter);
1323 /* Setup VLAN support, basic and offload if available */
1324 E1000_WRITE_REG(&adapter->hw, E1000_VET, ETHERTYPE_VLAN);
1326 if (ifp->if_capenable & IFCAP_VLAN_HWTAGGING) {
1329 ctrl = E1000_READ_REG(&adapter->hw, E1000_CTRL);
1330 ctrl |= E1000_CTRL_VME;
1331 E1000_WRITE_REG(&adapter->hw, E1000_CTRL, ctrl);
1334 /* Configure for OS presence */
1335 em_get_mgmt(adapter);
1337 /* Prepare transmit descriptors and buffers */
1338 em_init_tx_ring(adapter);
1339 em_init_tx_unit(adapter);
1341 /* Setup Multicast table */
1342 em_set_multi(adapter);
1344 /* Prepare receive descriptors and buffers */
1345 if (em_init_rx_ring(adapter)) {
1346 device_printf(dev, "Could not setup receive structures\n");
1350 em_init_rx_unit(adapter);
1352 /* Don't lose promiscuous settings */
1353 em_set_promisc(adapter);
1355 ifp->if_flags |= IFF_RUNNING;
1356 ifq_clr_oactive(&ifp->if_snd);
1358 callout_reset(&adapter->timer, hz, em_timer, adapter);
1359 e1000_clear_hw_cntrs_base_generic(&adapter->hw);
1361 /* MSI/X configuration for 82574 */
1362 if (adapter->hw.mac.type == e1000_82574) {
1365 tmp = E1000_READ_REG(&adapter->hw, E1000_CTRL_EXT);
1366 tmp |= E1000_CTRL_EXT_PBA_CLR;
1367 E1000_WRITE_REG(&adapter->hw, E1000_CTRL_EXT, tmp);
1370 * Set the IVAR - interrupt vector routing.
1371 * Each nibble represents a vector, high bit
1372 * is enable, other 3 bits are the MSIX table
1373 * entry, we map RXQ0 to 0, TXQ0 to 1, and
1374 * Link (other) to 2, hence the magic number.
1376 E1000_WRITE_REG(&adapter->hw, E1000_IVAR, 0x800A0908);
1379 #ifdef IFPOLL_ENABLE
1381 * Only enable interrupts if we are not polling, make sure
1382 * they are off otherwise.
1384 if (ifp->if_flags & IFF_NPOLLING)
1385 em_disable_intr(adapter);
1387 #endif /* IFPOLL_ENABLE */
1388 em_enable_intr(adapter);
1390 /* AMT based hardware can now take control from firmware */
1391 if ((adapter->flags & (EM_FLAG_HAS_MGMT | EM_FLAG_HAS_AMT)) ==
1392 (EM_FLAG_HAS_MGMT | EM_FLAG_HAS_AMT) &&
1393 adapter->hw.mac.type >= e1000_82571)
1394 em_get_hw_control(adapter);
1397 #ifdef IFPOLL_ENABLE
1400 em_npoll_compat(struct ifnet *ifp, void *arg __unused, int count)
1402 struct adapter *adapter = ifp->if_softc;
1404 ASSERT_SERIALIZED(ifp->if_serializer);
1406 if (adapter->npoll.ifpc_stcount-- == 0) {
1409 adapter->npoll.ifpc_stcount = adapter->npoll.ifpc_stfrac;
1411 reg_icr = E1000_READ_REG(&adapter->hw, E1000_ICR);
1412 if (reg_icr & (E1000_ICR_RXSEQ | E1000_ICR_LSC)) {
1413 callout_stop(&adapter->timer);
1414 adapter->hw.mac.get_link_status = 1;
1415 em_update_link_status(adapter);
1416 callout_reset(&adapter->timer, hz, em_timer, adapter);
1420 em_rxeof(adapter, count);
1423 if (!ifq_is_empty(&ifp->if_snd))
1428 em_npoll(struct ifnet *ifp, struct ifpoll_info *info)
1430 struct adapter *adapter = ifp->if_softc;
1432 ASSERT_SERIALIZED(ifp->if_serializer);
1435 int cpuid = adapter->npoll.ifpc_cpuid;
1437 info->ifpi_rx[cpuid].poll_func = em_npoll_compat;
1438 info->ifpi_rx[cpuid].arg = NULL;
1439 info->ifpi_rx[cpuid].serializer = ifp->if_serializer;
1441 if (ifp->if_flags & IFF_RUNNING)
1442 em_disable_intr(adapter);
1443 ifq_set_cpuid(&ifp->if_snd, cpuid);
1445 if (ifp->if_flags & IFF_RUNNING)
1446 em_enable_intr(adapter);
1447 ifq_set_cpuid(&ifp->if_snd, rman_get_cpuid(adapter->intr_res));
1451 #endif /* IFPOLL_ENABLE */
1456 em_intr_body(xsc, TRUE);
1460 em_intr_body(struct adapter *adapter, boolean_t chk_asserted)
1462 struct ifnet *ifp = &adapter->arpcom.ac_if;
1466 ASSERT_SERIALIZED(ifp->if_serializer);
1468 reg_icr = E1000_READ_REG(&adapter->hw, E1000_ICR);
1471 ((adapter->hw.mac.type >= e1000_82571 &&
1472 (reg_icr & E1000_ICR_INT_ASSERTED) == 0) ||
1479 * XXX: some laptops trigger several spurious interrupts
1480 * on em(4) when in the resume cycle. The ICR register
1481 * reports all-ones value in this case. Processing such
1482 * interrupts would lead to a freeze. I don't know why.
1484 if (reg_icr == 0xffffffff) {
1489 if (ifp->if_flags & IFF_RUNNING) {
1491 (E1000_ICR_RXT0 | E1000_ICR_RXDMT0 | E1000_ICR_RXO))
1492 em_rxeof(adapter, -1);
1493 if (reg_icr & E1000_ICR_TXDW) {
1495 if (!ifq_is_empty(&ifp->if_snd))
1500 /* Link status change */
1501 if (reg_icr & (E1000_ICR_RXSEQ | E1000_ICR_LSC)) {
1502 callout_stop(&adapter->timer);
1503 adapter->hw.mac.get_link_status = 1;
1504 em_update_link_status(adapter);
1506 /* Deal with TX cruft when link lost */
1507 em_tx_purge(adapter);
1509 callout_reset(&adapter->timer, hz, em_timer, adapter);
1512 if (reg_icr & E1000_ICR_RXO)
1513 adapter->rx_overruns++;
1519 em_intr_mask(void *xsc)
1521 struct adapter *adapter = xsc;
1523 E1000_WRITE_REG(&adapter->hw, E1000_IMC, 0xffffffff);
1526 * ICR.INT_ASSERTED bit will never be set if IMS is 0,
1527 * so don't check it.
1529 em_intr_body(adapter, FALSE);
1530 E1000_WRITE_REG(&adapter->hw, E1000_IMS, IMS_ENABLE_MASK);
1534 em_media_status(struct ifnet *ifp, struct ifmediareq *ifmr)
1536 struct adapter *adapter = ifp->if_softc;
1537 u_char fiber_type = IFM_1000_SX;
1539 ASSERT_SERIALIZED(ifp->if_serializer);
1541 em_update_link_status(adapter);
1543 ifmr->ifm_status = IFM_AVALID;
1544 ifmr->ifm_active = IFM_ETHER;
1546 if (!adapter->link_active)
1549 ifmr->ifm_status |= IFM_ACTIVE;
1551 if (adapter->hw.phy.media_type == e1000_media_type_fiber ||
1552 adapter->hw.phy.media_type == e1000_media_type_internal_serdes) {
1553 if (adapter->hw.mac.type == e1000_82545)
1554 fiber_type = IFM_1000_LX;
1555 ifmr->ifm_active |= fiber_type | IFM_FDX;
1557 switch (adapter->link_speed) {
1559 ifmr->ifm_active |= IFM_10_T;
1562 ifmr->ifm_active |= IFM_100_TX;
1566 ifmr->ifm_active |= IFM_1000_T;
1569 if (adapter->link_duplex == FULL_DUPLEX)
1570 ifmr->ifm_active |= IFM_FDX;
1572 ifmr->ifm_active |= IFM_HDX;
1577 em_media_change(struct ifnet *ifp)
1579 struct adapter *adapter = ifp->if_softc;
1580 struct ifmedia *ifm = &adapter->media;
1582 ASSERT_SERIALIZED(ifp->if_serializer);
1584 if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
1587 switch (IFM_SUBTYPE(ifm->ifm_media)) {
1589 adapter->hw.mac.autoneg = DO_AUTO_NEG;
1590 adapter->hw.phy.autoneg_advertised = AUTONEG_ADV_DEFAULT;
1596 adapter->hw.mac.autoneg = DO_AUTO_NEG;
1597 adapter->hw.phy.autoneg_advertised = ADVERTISE_1000_FULL;
1601 adapter->hw.mac.autoneg = FALSE;
1602 adapter->hw.phy.autoneg_advertised = 0;
1603 if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX)
1604 adapter->hw.mac.forced_speed_duplex = ADVERTISE_100_FULL;
1606 adapter->hw.mac.forced_speed_duplex = ADVERTISE_100_HALF;
1610 adapter->hw.mac.autoneg = FALSE;
1611 adapter->hw.phy.autoneg_advertised = 0;
1612 if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX)
1613 adapter->hw.mac.forced_speed_duplex = ADVERTISE_10_FULL;
1615 adapter->hw.mac.forced_speed_duplex = ADVERTISE_10_HALF;
1619 if_printf(ifp, "Unsupported media type\n");
1629 em_encap(struct adapter *adapter, struct mbuf **m_headp,
1630 int *segs_used, int *idx)
1632 bus_dma_segment_t segs[EM_MAX_SCATTER];
1634 struct em_buffer *tx_buffer, *tx_buffer_mapped;
1635 struct e1000_tx_desc *ctxd = NULL;
1636 struct mbuf *m_head = *m_headp;
1637 uint32_t txd_upper, txd_lower, txd_used, cmd = 0;
1638 int maxsegs, nsegs, i, j, first, last = 0, error;
1640 if (m_head->m_pkthdr.csum_flags & CSUM_TSO) {
1641 error = em_tso_pullup(adapter, m_headp);
1647 txd_upper = txd_lower = 0;
1651 * Capture the first descriptor index, this descriptor
1652 * will have the index of the EOP which is the only one
1653 * that now gets a DONE bit writeback.
1655 first = adapter->next_avail_tx_desc;
1656 tx_buffer = &adapter->tx_buffer_area[first];
1657 tx_buffer_mapped = tx_buffer;
1658 map = tx_buffer->map;
1660 maxsegs = adapter->num_tx_desc_avail - EM_TX_RESERVED;
1661 KASSERT(maxsegs >= adapter->spare_tx_desc,
1662 ("not enough spare TX desc"));
1663 if (adapter->pcix_82544) {
1664 /* Half it; see the comment in em_attach() */
1667 if (maxsegs > EM_MAX_SCATTER)
1668 maxsegs = EM_MAX_SCATTER;
1670 error = bus_dmamap_load_mbuf_defrag(adapter->txtag, map, m_headp,
1671 segs, maxsegs, &nsegs, BUS_DMA_NOWAIT);
1673 if (error == ENOBUFS)
1674 adapter->mbuf_alloc_failed++;
1676 adapter->no_tx_dma_setup++;
1682 bus_dmamap_sync(adapter->txtag, map, BUS_DMASYNC_PREWRITE);
1685 adapter->tx_nsegs += nsegs;
1686 *segs_used += nsegs;
1688 if (m_head->m_pkthdr.csum_flags & CSUM_TSO) {
1689 /* TSO will consume one TX desc */
1690 i = em_tso_setup(adapter, m_head, &txd_upper, &txd_lower);
1691 adapter->tx_nsegs += i;
1693 } else if (m_head->m_pkthdr.csum_flags & EM_CSUM_FEATURES) {
1694 /* TX csum offloading will consume one TX desc */
1695 i = em_txcsum(adapter, m_head, &txd_upper, &txd_lower);
1696 adapter->tx_nsegs += i;
1700 /* Handle VLAN tag */
1701 if (m_head->m_flags & M_VLANTAG) {
1702 /* Set the vlan id. */
1703 txd_upper |= (htole16(m_head->m_pkthdr.ether_vlantag) << 16);
1704 /* Tell hardware to add tag */
1705 txd_lower |= htole32(E1000_TXD_CMD_VLE);
1708 i = adapter->next_avail_tx_desc;
1710 /* Set up our transmit descriptors */
1711 for (j = 0; j < nsegs; j++) {
1712 /* If adapter is 82544 and on PCIX bus */
1713 if(adapter->pcix_82544) {
1714 DESC_ARRAY desc_array;
1715 uint32_t array_elements, counter;
1718 * Check the Address and Length combination and
1719 * split the data accordingly
1721 array_elements = em_82544_fill_desc(segs[j].ds_addr,
1722 segs[j].ds_len, &desc_array);
1723 for (counter = 0; counter < array_elements; counter++) {
1724 KKASSERT(txd_used < adapter->num_tx_desc_avail);
1726 tx_buffer = &adapter->tx_buffer_area[i];
1727 ctxd = &adapter->tx_desc_base[i];
1729 ctxd->buffer_addr = htole64(
1730 desc_array.descriptor[counter].address);
1731 ctxd->lower.data = htole32(
1732 E1000_TXD_CMD_IFCS | txd_lower |
1733 desc_array.descriptor[counter].length);
1734 ctxd->upper.data = htole32(txd_upper);
1737 if (++i == adapter->num_tx_desc)
1743 tx_buffer = &adapter->tx_buffer_area[i];
1744 ctxd = &adapter->tx_desc_base[i];
1746 ctxd->buffer_addr = htole64(segs[j].ds_addr);
1747 ctxd->lower.data = htole32(E1000_TXD_CMD_IFCS |
1748 txd_lower | segs[j].ds_len);
1749 ctxd->upper.data = htole32(txd_upper);
1752 if (++i == adapter->num_tx_desc)
1757 adapter->next_avail_tx_desc = i;
1758 if (adapter->pcix_82544) {
1759 KKASSERT(adapter->num_tx_desc_avail > txd_used);
1760 adapter->num_tx_desc_avail -= txd_used;
1762 KKASSERT(adapter->num_tx_desc_avail > nsegs);
1763 adapter->num_tx_desc_avail -= nsegs;
1766 tx_buffer->m_head = m_head;
1767 tx_buffer_mapped->map = tx_buffer->map;
1768 tx_buffer->map = map;
1770 if (adapter->tx_nsegs >= adapter->tx_int_nsegs) {
1771 adapter->tx_nsegs = 0;
1774 * Report Status (RS) is turned on
1775 * every tx_int_nsegs descriptors.
1777 cmd = E1000_TXD_CMD_RS;
1780 * Keep track of the descriptor, which will
1781 * be written back by hardware.
1783 adapter->tx_dd[adapter->tx_dd_tail] = last;
1784 EM_INC_TXDD_IDX(adapter->tx_dd_tail);
1785 KKASSERT(adapter->tx_dd_tail != adapter->tx_dd_head);
1789 * Last Descriptor of Packet needs End Of Packet (EOP)
1791 ctxd->lower.data |= htole32(E1000_TXD_CMD_EOP | cmd);
1793 if (adapter->hw.mac.type == e1000_82547) {
1795 * Advance the Transmit Descriptor Tail (TDT), this tells the
1796 * E1000 that this frame is available to transmit.
1798 if (adapter->link_duplex == HALF_DUPLEX) {
1799 em_82547_move_tail_serialized(adapter);
1801 E1000_WRITE_REG(&adapter->hw, E1000_TDT(0), i);
1802 em_82547_update_fifo_head(adapter,
1803 m_head->m_pkthdr.len);
1807 * Defer TDT updating, until enough descriptors are setup
1815 * 82547 workaround to avoid controller hang in half-duplex environment.
1816 * The workaround is to avoid queuing a large packet that would span
1817 * the internal Tx FIFO ring boundary. We need to reset the FIFO pointers
1818 * in this case. We do that only when FIFO is quiescent.
1821 em_82547_move_tail_serialized(struct adapter *adapter)
1823 struct e1000_tx_desc *tx_desc;
1824 uint16_t hw_tdt, sw_tdt, length = 0;
1827 ASSERT_SERIALIZED(adapter->arpcom.ac_if.if_serializer);
1829 hw_tdt = E1000_READ_REG(&adapter->hw, E1000_TDT(0));
1830 sw_tdt = adapter->next_avail_tx_desc;
1832 while (hw_tdt != sw_tdt) {
1833 tx_desc = &adapter->tx_desc_base[hw_tdt];
1834 length += tx_desc->lower.flags.length;
1835 eop = tx_desc->lower.data & E1000_TXD_CMD_EOP;
1836 if (++hw_tdt == adapter->num_tx_desc)
1840 if (em_82547_fifo_workaround(adapter, length)) {
1841 adapter->tx_fifo_wrk_cnt++;
1842 callout_reset(&adapter->tx_fifo_timer, 1,
1843 em_82547_move_tail, adapter);
1846 E1000_WRITE_REG(&adapter->hw, E1000_TDT(0), hw_tdt);
1847 em_82547_update_fifo_head(adapter, length);
1854 em_82547_move_tail(void *xsc)
1856 struct adapter *adapter = xsc;
1857 struct ifnet *ifp = &adapter->arpcom.ac_if;
1859 lwkt_serialize_enter(ifp->if_serializer);
1860 em_82547_move_tail_serialized(adapter);
1861 lwkt_serialize_exit(ifp->if_serializer);
1865 em_82547_fifo_workaround(struct adapter *adapter, int len)
1867 int fifo_space, fifo_pkt_len;
1869 fifo_pkt_len = roundup2(len + EM_FIFO_HDR, EM_FIFO_HDR);
1871 if (adapter->link_duplex == HALF_DUPLEX) {
1872 fifo_space = adapter->tx_fifo_size - adapter->tx_fifo_head;
1874 if (fifo_pkt_len >= (EM_82547_PKT_THRESH + fifo_space)) {
1875 if (em_82547_tx_fifo_reset(adapter))
1885 em_82547_update_fifo_head(struct adapter *adapter, int len)
1887 int fifo_pkt_len = roundup2(len + EM_FIFO_HDR, EM_FIFO_HDR);
1889 /* tx_fifo_head is always 16 byte aligned */
1890 adapter->tx_fifo_head += fifo_pkt_len;
1891 if (adapter->tx_fifo_head >= adapter->tx_fifo_size)
1892 adapter->tx_fifo_head -= adapter->tx_fifo_size;
1896 em_82547_tx_fifo_reset(struct adapter *adapter)
1900 if ((E1000_READ_REG(&adapter->hw, E1000_TDT(0)) ==
1901 E1000_READ_REG(&adapter->hw, E1000_TDH(0))) &&
1902 (E1000_READ_REG(&adapter->hw, E1000_TDFT) ==
1903 E1000_READ_REG(&adapter->hw, E1000_TDFH)) &&
1904 (E1000_READ_REG(&adapter->hw, E1000_TDFTS) ==
1905 E1000_READ_REG(&adapter->hw, E1000_TDFHS)) &&
1906 (E1000_READ_REG(&adapter->hw, E1000_TDFPC) == 0)) {
1907 /* Disable TX unit */
1908 tctl = E1000_READ_REG(&adapter->hw, E1000_TCTL);
1909 E1000_WRITE_REG(&adapter->hw, E1000_TCTL,
1910 tctl & ~E1000_TCTL_EN);
1912 /* Reset FIFO pointers */
1913 E1000_WRITE_REG(&adapter->hw, E1000_TDFT,
1914 adapter->tx_head_addr);
1915 E1000_WRITE_REG(&adapter->hw, E1000_TDFH,
1916 adapter->tx_head_addr);
1917 E1000_WRITE_REG(&adapter->hw, E1000_TDFTS,
1918 adapter->tx_head_addr);
1919 E1000_WRITE_REG(&adapter->hw, E1000_TDFHS,
1920 adapter->tx_head_addr);
1922 /* Re-enable TX unit */
1923 E1000_WRITE_REG(&adapter->hw, E1000_TCTL, tctl);
1924 E1000_WRITE_FLUSH(&adapter->hw);
1926 adapter->tx_fifo_head = 0;
1927 adapter->tx_fifo_reset_cnt++;
1936 em_set_promisc(struct adapter *adapter)
1938 struct ifnet *ifp = &adapter->arpcom.ac_if;
1941 reg_rctl = E1000_READ_REG(&adapter->hw, E1000_RCTL);
1943 if (ifp->if_flags & IFF_PROMISC) {
1944 reg_rctl |= (E1000_RCTL_UPE | E1000_RCTL_MPE);
1945 /* Turn this on if you want to see bad packets */
1947 reg_rctl |= E1000_RCTL_SBP;
1948 E1000_WRITE_REG(&adapter->hw, E1000_RCTL, reg_rctl);
1949 } else if (ifp->if_flags & IFF_ALLMULTI) {
1950 reg_rctl |= E1000_RCTL_MPE;
1951 reg_rctl &= ~E1000_RCTL_UPE;
1952 E1000_WRITE_REG(&adapter->hw, E1000_RCTL, reg_rctl);
1957 em_disable_promisc(struct adapter *adapter)
1961 reg_rctl = E1000_READ_REG(&adapter->hw, E1000_RCTL);
1963 reg_rctl &= ~E1000_RCTL_UPE;
1964 reg_rctl &= ~E1000_RCTL_MPE;
1965 reg_rctl &= ~E1000_RCTL_SBP;
1966 E1000_WRITE_REG(&adapter->hw, E1000_RCTL, reg_rctl);
1970 em_set_multi(struct adapter *adapter)
1972 struct ifnet *ifp = &adapter->arpcom.ac_if;
1973 struct ifmultiaddr *ifma;
1974 uint32_t reg_rctl = 0;
1979 bzero(mta, ETH_ADDR_LEN * MAX_NUM_MULTICAST_ADDRESSES);
1981 if (adapter->hw.mac.type == e1000_82542 &&
1982 adapter->hw.revision_id == E1000_REVISION_2) {
1983 reg_rctl = E1000_READ_REG(&adapter->hw, E1000_RCTL);
1984 if (adapter->hw.bus.pci_cmd_word & CMD_MEM_WRT_INVALIDATE)
1985 e1000_pci_clear_mwi(&adapter->hw);
1986 reg_rctl |= E1000_RCTL_RST;
1987 E1000_WRITE_REG(&adapter->hw, E1000_RCTL, reg_rctl);
1991 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1992 if (ifma->ifma_addr->sa_family != AF_LINK)
1995 if (mcnt == MAX_NUM_MULTICAST_ADDRESSES)
1998 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
1999 &mta[mcnt * ETHER_ADDR_LEN], ETHER_ADDR_LEN);
2003 if (mcnt >= MAX_NUM_MULTICAST_ADDRESSES) {
2004 reg_rctl = E1000_READ_REG(&adapter->hw, E1000_RCTL);
2005 reg_rctl |= E1000_RCTL_MPE;
2006 E1000_WRITE_REG(&adapter->hw, E1000_RCTL, reg_rctl);
2008 e1000_update_mc_addr_list(&adapter->hw, mta, mcnt);
2011 if (adapter->hw.mac.type == e1000_82542 &&
2012 adapter->hw.revision_id == E1000_REVISION_2) {
2013 reg_rctl = E1000_READ_REG(&adapter->hw, E1000_RCTL);
2014 reg_rctl &= ~E1000_RCTL_RST;
2015 E1000_WRITE_REG(&adapter->hw, E1000_RCTL, reg_rctl);
2017 if (adapter->hw.bus.pci_cmd_word & CMD_MEM_WRT_INVALIDATE)
2018 e1000_pci_set_mwi(&adapter->hw);
2023 * This routine checks for link status and updates statistics.
2028 struct adapter *adapter = xsc;
2029 struct ifnet *ifp = &adapter->arpcom.ac_if;
2031 lwkt_serialize_enter(ifp->if_serializer);
2033 em_update_link_status(adapter);
2034 em_update_stats(adapter);
2036 /* Reset LAA into RAR[0] on 82571 */
2037 if (e1000_get_laa_state_82571(&adapter->hw) == TRUE)
2038 e1000_rar_set(&adapter->hw, adapter->hw.mac.addr, 0);
2040 if (em_display_debug_stats && (ifp->if_flags & IFF_RUNNING))
2041 em_print_hw_stats(adapter);
2043 em_smartspeed(adapter);
2045 callout_reset(&adapter->timer, hz, em_timer, adapter);
2047 lwkt_serialize_exit(ifp->if_serializer);
2051 em_update_link_status(struct adapter *adapter)
2053 struct e1000_hw *hw = &adapter->hw;
2054 struct ifnet *ifp = &adapter->arpcom.ac_if;
2055 device_t dev = adapter->dev;
2056 uint32_t link_check = 0;
2058 /* Get the cached link value or read phy for real */
2059 switch (hw->phy.media_type) {
2060 case e1000_media_type_copper:
2061 if (hw->mac.get_link_status) {
2062 /* Do the work to read phy */
2063 e1000_check_for_link(hw);
2064 link_check = !hw->mac.get_link_status;
2065 if (link_check) /* ESB2 fix */
2066 e1000_cfg_on_link_up(hw);
2072 case e1000_media_type_fiber:
2073 e1000_check_for_link(hw);
2075 E1000_READ_REG(hw, E1000_STATUS) & E1000_STATUS_LU;
2078 case e1000_media_type_internal_serdes:
2079 e1000_check_for_link(hw);
2080 link_check = adapter->hw.mac.serdes_has_link;
2083 case e1000_media_type_unknown:
2088 /* Now check for a transition */
2089 if (link_check && adapter->link_active == 0) {
2090 e1000_get_speed_and_duplex(hw, &adapter->link_speed,
2091 &adapter->link_duplex);
2094 * Check if we should enable/disable SPEED_MODE bit on
2097 if (adapter->link_speed != SPEED_1000 &&
2098 (hw->mac.type == e1000_82571 ||
2099 hw->mac.type == e1000_82572)) {
2102 tarc0 = E1000_READ_REG(hw, E1000_TARC(0));
2103 tarc0 &= ~SPEED_MODE_BIT;
2104 E1000_WRITE_REG(hw, E1000_TARC(0), tarc0);
2107 device_printf(dev, "Link is up %d Mbps %s\n",
2108 adapter->link_speed,
2109 ((adapter->link_duplex == FULL_DUPLEX) ?
2110 "Full Duplex" : "Half Duplex"));
2112 adapter->link_active = 1;
2113 adapter->smartspeed = 0;
2114 ifp->if_baudrate = adapter->link_speed * 1000000;
2115 ifp->if_link_state = LINK_STATE_UP;
2116 if_link_state_change(ifp);
2117 } else if (!link_check && adapter->link_active == 1) {
2118 ifp->if_baudrate = adapter->link_speed = 0;
2119 adapter->link_duplex = 0;
2121 device_printf(dev, "Link is Down\n");
2122 adapter->link_active = 0;
2124 /* Link down, disable watchdog */
2127 ifp->if_link_state = LINK_STATE_DOWN;
2128 if_link_state_change(ifp);
2133 em_stop(struct adapter *adapter)
2135 struct ifnet *ifp = &adapter->arpcom.ac_if;
2138 ASSERT_SERIALIZED(ifp->if_serializer);
2140 em_disable_intr(adapter);
2142 callout_stop(&adapter->timer);
2143 callout_stop(&adapter->tx_fifo_timer);
2145 ifp->if_flags &= ~IFF_RUNNING;
2146 ifq_clr_oactive(&ifp->if_snd);
2149 e1000_reset_hw(&adapter->hw);
2150 if (adapter->hw.mac.type >= e1000_82544)
2151 E1000_WRITE_REG(&adapter->hw, E1000_WUC, 0);
2153 for (i = 0; i < adapter->num_tx_desc; i++) {
2154 struct em_buffer *tx_buffer = &adapter->tx_buffer_area[i];
2156 if (tx_buffer->m_head != NULL) {
2157 bus_dmamap_unload(adapter->txtag, tx_buffer->map);
2158 m_freem(tx_buffer->m_head);
2159 tx_buffer->m_head = NULL;
2163 for (i = 0; i < adapter->num_rx_desc; i++) {
2164 struct em_buffer *rx_buffer = &adapter->rx_buffer_area[i];
2166 if (rx_buffer->m_head != NULL) {
2167 bus_dmamap_unload(adapter->rxtag, rx_buffer->map);
2168 m_freem(rx_buffer->m_head);
2169 rx_buffer->m_head = NULL;
2173 if (adapter->fmp != NULL)
2174 m_freem(adapter->fmp);
2175 adapter->fmp = NULL;
2176 adapter->lmp = NULL;
2178 adapter->csum_flags = 0;
2179 adapter->csum_lhlen = 0;
2180 adapter->csum_iphlen = 0;
2181 adapter->csum_thlen = 0;
2182 adapter->csum_mss = 0;
2183 adapter->csum_pktlen = 0;
2185 adapter->tx_dd_head = 0;
2186 adapter->tx_dd_tail = 0;
2187 adapter->tx_nsegs = 0;
2191 em_get_hw_info(struct adapter *adapter)
2193 device_t dev = adapter->dev;
2195 /* Save off the information about this board */
2196 adapter->hw.vendor_id = pci_get_vendor(dev);
2197 adapter->hw.device_id = pci_get_device(dev);
2198 adapter->hw.revision_id = pci_get_revid(dev);
2199 adapter->hw.subsystem_vendor_id = pci_get_subvendor(dev);
2200 adapter->hw.subsystem_device_id = pci_get_subdevice(dev);
2202 /* Do Shared Code Init and Setup */
2203 if (e1000_set_mac_type(&adapter->hw))
2209 em_alloc_pci_res(struct adapter *adapter)
2211 device_t dev = adapter->dev;
2213 int val, rid, msi_enable;
2215 /* Enable bus mastering */
2216 pci_enable_busmaster(dev);
2218 adapter->memory_rid = EM_BAR_MEM;
2219 adapter->memory = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
2220 &adapter->memory_rid, RF_ACTIVE);
2221 if (adapter->memory == NULL) {
2222 device_printf(dev, "Unable to allocate bus resource: memory\n");
2225 adapter->osdep.mem_bus_space_tag =
2226 rman_get_bustag(adapter->memory);
2227 adapter->osdep.mem_bus_space_handle =
2228 rman_get_bushandle(adapter->memory);
2230 /* XXX This is quite goofy, it is not actually used */
2231 adapter->hw.hw_addr = (uint8_t *)&adapter->osdep.mem_bus_space_handle;
2233 /* Only older adapters use IO mapping */
2234 if (adapter->hw.mac.type > e1000_82543 &&
2235 adapter->hw.mac.type < e1000_82571) {
2236 /* Figure our where our IO BAR is ? */
2237 for (rid = PCIR_BAR(0); rid < PCIR_CARDBUSCIS;) {
2238 val = pci_read_config(dev, rid, 4);
2239 if (EM_BAR_TYPE(val) == EM_BAR_TYPE_IO) {
2240 adapter->io_rid = rid;
2244 /* check for 64bit BAR */
2245 if (EM_BAR_MEM_TYPE(val) == EM_BAR_MEM_TYPE_64BIT)
2248 if (rid >= PCIR_CARDBUSCIS) {
2249 device_printf(dev, "Unable to locate IO BAR\n");
2252 adapter->ioport = bus_alloc_resource_any(dev, SYS_RES_IOPORT,
2253 &adapter->io_rid, RF_ACTIVE);
2254 if (adapter->ioport == NULL) {
2255 device_printf(dev, "Unable to allocate bus resource: "
2259 adapter->hw.io_base = 0;
2260 adapter->osdep.io_bus_space_tag =
2261 rman_get_bustag(adapter->ioport);
2262 adapter->osdep.io_bus_space_handle =
2263 rman_get_bushandle(adapter->ioport);
2267 * Don't enable MSI-X on 82574, see:
2268 * 82574 specification update errata #15
2270 * Don't enable MSI on PCI/PCI-X chips, see:
2271 * 82540 specification update errata #6
2272 * 82545 specification update errata #4
2274 * Don't enable MSI on 82571/82572, see:
2275 * 82571/82572 specification update errata #63
2277 msi_enable = em_msi_enable;
2279 (!pci_is_pcie(dev) ||
2280 adapter->hw.mac.type == e1000_82571 ||
2281 adapter->hw.mac.type == e1000_82572))
2284 adapter->intr_type = pci_alloc_1intr(dev, msi_enable,
2285 &adapter->intr_rid, &intr_flags);
2287 if (adapter->intr_type == PCI_INTR_TYPE_LEGACY) {
2290 unshared = device_getenv_int(dev, "irq.unshared", 0);
2292 adapter->flags |= EM_FLAG_SHARED_INTR;
2294 device_printf(dev, "IRQ shared\n");
2296 intr_flags &= ~RF_SHAREABLE;
2298 device_printf(dev, "IRQ unshared\n");
2302 adapter->intr_res = bus_alloc_resource_any(dev, SYS_RES_IRQ,
2303 &adapter->intr_rid, intr_flags);
2304 if (adapter->intr_res == NULL) {
2305 device_printf(dev, "Unable to allocate bus resource: "
2310 adapter->hw.bus.pci_cmd_word = pci_read_config(dev, PCIR_COMMAND, 2);
2311 adapter->hw.back = &adapter->osdep;
2316 em_free_pci_res(struct adapter *adapter)
2318 device_t dev = adapter->dev;
2320 if (adapter->intr_res != NULL) {
2321 bus_release_resource(dev, SYS_RES_IRQ,
2322 adapter->intr_rid, adapter->intr_res);
2325 if (adapter->intr_type == PCI_INTR_TYPE_MSI)
2326 pci_release_msi(dev);
2328 if (adapter->memory != NULL) {
2329 bus_release_resource(dev, SYS_RES_MEMORY,
2330 adapter->memory_rid, adapter->memory);
2333 if (adapter->flash != NULL) {
2334 bus_release_resource(dev, SYS_RES_MEMORY,
2335 adapter->flash_rid, adapter->flash);
2338 if (adapter->ioport != NULL) {
2339 bus_release_resource(dev, SYS_RES_IOPORT,
2340 adapter->io_rid, adapter->ioport);
2345 em_reset(struct adapter *adapter)
2347 device_t dev = adapter->dev;
2348 uint16_t rx_buffer_size;
2351 /* When hardware is reset, fifo_head is also reset */
2352 adapter->tx_fifo_head = 0;
2354 /* Set up smart power down as default off on newer adapters. */
2355 if (!em_smart_pwr_down &&
2356 (adapter->hw.mac.type == e1000_82571 ||
2357 adapter->hw.mac.type == e1000_82572)) {
2358 uint16_t phy_tmp = 0;
2360 /* Speed up time to link by disabling smart power down. */
2361 e1000_read_phy_reg(&adapter->hw,
2362 IGP02E1000_PHY_POWER_MGMT, &phy_tmp);
2363 phy_tmp &= ~IGP02E1000_PM_SPD;
2364 e1000_write_phy_reg(&adapter->hw,
2365 IGP02E1000_PHY_POWER_MGMT, phy_tmp);
2369 * Packet Buffer Allocation (PBA)
2370 * Writing PBA sets the receive portion of the buffer
2371 * the remainder is used for the transmit buffer.
2373 * Devices before the 82547 had a Packet Buffer of 64K.
2374 * Default allocation: PBA=48K for Rx, leaving 16K for Tx.
2375 * After the 82547 the buffer was reduced to 40K.
2376 * Default allocation: PBA=30K for Rx, leaving 10K for Tx.
2377 * Note: default does not leave enough room for Jumbo Frame >10k.
2379 switch (adapter->hw.mac.type) {
2381 case e1000_82547_rev_2: /* 82547: Total Packet Buffer is 40K */
2382 if (adapter->hw.mac.max_frame_size > 8192)
2383 pba = E1000_PBA_22K; /* 22K for Rx, 18K for Tx */
2385 pba = E1000_PBA_30K; /* 30K for Rx, 10K for Tx */
2386 adapter->tx_fifo_head = 0;
2387 adapter->tx_head_addr = pba << EM_TX_HEAD_ADDR_SHIFT;
2388 adapter->tx_fifo_size =
2389 (E1000_PBA_40K - pba) << EM_PBA_BYTES_SHIFT;
2392 /* Total Packet Buffer on these is 48K */
2395 case e1000_80003es2lan:
2396 pba = E1000_PBA_32K; /* 32K for Rx, 16K for Tx */
2399 case e1000_82573: /* 82573: Total Packet Buffer is 32K */
2400 pba = E1000_PBA_12K; /* 12K for Rx, 20K for Tx */
2405 pba = E1000_PBA_20K; /* 20K for Rx, 20K for Tx */
2413 case e1000_ich10lan:
2414 #define E1000_PBA_10K 0x000A
2415 pba = E1000_PBA_10K;
2421 pba = E1000_PBA_26K;
2425 /* Devices before 82547 had a Packet Buffer of 64K. */
2426 if (adapter->hw.mac.max_frame_size > 8192)
2427 pba = E1000_PBA_40K; /* 40K for Rx, 24K for Tx */
2429 pba = E1000_PBA_48K; /* 48K for Rx, 16K for Tx */
2431 E1000_WRITE_REG(&adapter->hw, E1000_PBA, pba);
2434 * These parameters control the automatic generation (Tx) and
2435 * response (Rx) to Ethernet PAUSE frames.
2436 * - High water mark should allow for at least two frames to be
2437 * received after sending an XOFF.
2438 * - Low water mark works best when it is very near the high water mark.
2439 * This allows the receiver to restart by sending XON when it has
2440 * drained a bit. Here we use an arbitary value of 1500 which will
2441 * restart after one full frame is pulled from the buffer. There
2442 * could be several smaller frames in the buffer and if so they will
2443 * not trigger the XON until their total number reduces the buffer
2445 * - The pause time is fairly large at 1000 x 512ns = 512 usec.
2448 (E1000_READ_REG(&adapter->hw, E1000_PBA) & 0xffff) << 10;
2450 adapter->hw.fc.high_water = rx_buffer_size -
2451 roundup2(adapter->hw.mac.max_frame_size, 1024);
2452 adapter->hw.fc.low_water = adapter->hw.fc.high_water - 1500;
2454 if (adapter->hw.mac.type == e1000_80003es2lan)
2455 adapter->hw.fc.pause_time = 0xFFFF;
2457 adapter->hw.fc.pause_time = EM_FC_PAUSE_TIME;
2459 adapter->hw.fc.send_xon = TRUE;
2461 adapter->hw.fc.requested_mode = adapter->flow_ctrl;
2464 * Device specific overrides/settings
2466 switch (adapter->hw.mac.type) {
2468 /* Workaround: no TX flow ctrl for PCH */
2469 adapter->hw.fc.requested_mode = e1000_fc_rx_pause;
2470 adapter->hw.fc.pause_time = 0xFFFF; /* override */
2471 if (adapter->arpcom.ac_if.if_mtu > ETHERMTU) {
2472 adapter->hw.fc.high_water = 0x3500;
2473 adapter->hw.fc.low_water = 0x1500;
2475 adapter->hw.fc.high_water = 0x5000;
2476 adapter->hw.fc.low_water = 0x3000;
2478 adapter->hw.fc.refresh_time = 0x1000;
2483 adapter->hw.fc.high_water = 0x5C20;
2484 adapter->hw.fc.low_water = 0x5048;
2485 adapter->hw.fc.pause_time = 0x0650;
2486 adapter->hw.fc.refresh_time = 0x0400;
2487 /* Jumbos need adjusted PBA */
2488 if (adapter->arpcom.ac_if.if_mtu > ETHERMTU)
2489 E1000_WRITE_REG(&adapter->hw, E1000_PBA, 12);
2491 E1000_WRITE_REG(&adapter->hw, E1000_PBA, 26);
2495 case e1000_ich10lan:
2496 if (adapter->arpcom.ac_if.if_mtu > ETHERMTU) {
2497 adapter->hw.fc.high_water = 0x2800;
2498 adapter->hw.fc.low_water =
2499 adapter->hw.fc.high_water - 8;
2504 if (adapter->hw.mac.type == e1000_80003es2lan)
2505 adapter->hw.fc.pause_time = 0xFFFF;
2509 /* Issue a global reset */
2510 e1000_reset_hw(&adapter->hw);
2511 if (adapter->hw.mac.type >= e1000_82544)
2512 E1000_WRITE_REG(&adapter->hw, E1000_WUC, 0);
2513 em_disable_aspm(adapter);
2515 if (e1000_init_hw(&adapter->hw) < 0) {
2516 device_printf(dev, "Hardware Initialization Failed\n");
2520 E1000_WRITE_REG(&adapter->hw, E1000_VET, ETHERTYPE_VLAN);
2521 e1000_get_phy_info(&adapter->hw);
2522 e1000_check_for_link(&adapter->hw);
2528 em_setup_ifp(struct adapter *adapter)
2530 struct ifnet *ifp = &adapter->arpcom.ac_if;
2532 if_initname(ifp, device_get_name(adapter->dev),
2533 device_get_unit(adapter->dev));
2534 ifp->if_softc = adapter;
2535 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
2536 ifp->if_init = em_init;
2537 ifp->if_ioctl = em_ioctl;
2538 ifp->if_start = em_start;
2539 #ifdef IFPOLL_ENABLE
2540 ifp->if_npoll = em_npoll;
2542 ifp->if_watchdog = em_watchdog;
2543 ifp->if_nmbclusters = adapter->num_rx_desc;
2544 ifq_set_maxlen(&ifp->if_snd, adapter->num_tx_desc - 1);
2545 ifq_set_ready(&ifp->if_snd);
2547 ether_ifattach(ifp, adapter->hw.mac.addr, NULL);
2549 ifp->if_capabilities = IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_MTU;
2550 if (adapter->hw.mac.type >= e1000_82543)
2551 ifp->if_capabilities |= IFCAP_HWCSUM;
2552 if (adapter->flags & EM_FLAG_TSO)
2553 ifp->if_capabilities |= IFCAP_TSO;
2554 ifp->if_capenable = ifp->if_capabilities;
2556 if (ifp->if_capenable & IFCAP_TXCSUM)
2557 ifp->if_hwassist |= EM_CSUM_FEATURES;
2558 if (ifp->if_capenable & IFCAP_TSO)
2559 ifp->if_hwassist |= CSUM_TSO;
2562 * Tell the upper layer(s) we support long frames.
2564 ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
2567 * Specify the media types supported by this adapter and register
2568 * callbacks to update media and link information
2570 if (adapter->hw.phy.media_type == e1000_media_type_fiber ||
2571 adapter->hw.phy.media_type == e1000_media_type_internal_serdes) {
2572 u_char fiber_type = IFM_1000_SX; /* default type */
2574 if (adapter->hw.mac.type == e1000_82545)
2575 fiber_type = IFM_1000_LX;
2576 ifmedia_add(&adapter->media, IFM_ETHER | fiber_type | IFM_FDX,
2578 ifmedia_add(&adapter->media, IFM_ETHER | fiber_type, 0, NULL);
2580 ifmedia_add(&adapter->media, IFM_ETHER | IFM_10_T, 0, NULL);
2581 ifmedia_add(&adapter->media, IFM_ETHER | IFM_10_T | IFM_FDX,
2583 ifmedia_add(&adapter->media, IFM_ETHER | IFM_100_TX,
2585 ifmedia_add(&adapter->media, IFM_ETHER | IFM_100_TX | IFM_FDX,
2587 if (adapter->hw.phy.type != e1000_phy_ife) {
2588 ifmedia_add(&adapter->media,
2589 IFM_ETHER | IFM_1000_T | IFM_FDX, 0, NULL);
2590 ifmedia_add(&adapter->media,
2591 IFM_ETHER | IFM_1000_T, 0, NULL);
2594 ifmedia_add(&adapter->media, IFM_ETHER | IFM_AUTO, 0, NULL);
2595 ifmedia_set(&adapter->media, IFM_ETHER | IFM_AUTO);
2600 * Workaround for SmartSpeed on 82541 and 82547 controllers
2603 em_smartspeed(struct adapter *adapter)
2607 if (adapter->link_active || adapter->hw.phy.type != e1000_phy_igp ||
2608 adapter->hw.mac.autoneg == 0 ||
2609 (adapter->hw.phy.autoneg_advertised & ADVERTISE_1000_FULL) == 0)
2612 if (adapter->smartspeed == 0) {
2614 * If Master/Slave config fault is asserted twice,
2615 * we assume back-to-back
2617 e1000_read_phy_reg(&adapter->hw, PHY_1000T_STATUS, &phy_tmp);
2618 if (!(phy_tmp & SR_1000T_MS_CONFIG_FAULT))
2620 e1000_read_phy_reg(&adapter->hw, PHY_1000T_STATUS, &phy_tmp);
2621 if (phy_tmp & SR_1000T_MS_CONFIG_FAULT) {
2622 e1000_read_phy_reg(&adapter->hw,
2623 PHY_1000T_CTRL, &phy_tmp);
2624 if (phy_tmp & CR_1000T_MS_ENABLE) {
2625 phy_tmp &= ~CR_1000T_MS_ENABLE;
2626 e1000_write_phy_reg(&adapter->hw,
2627 PHY_1000T_CTRL, phy_tmp);
2628 adapter->smartspeed++;
2629 if (adapter->hw.mac.autoneg &&
2630 !e1000_phy_setup_autoneg(&adapter->hw) &&
2631 !e1000_read_phy_reg(&adapter->hw,
2632 PHY_CONTROL, &phy_tmp)) {
2633 phy_tmp |= MII_CR_AUTO_NEG_EN |
2634 MII_CR_RESTART_AUTO_NEG;
2635 e1000_write_phy_reg(&adapter->hw,
2636 PHY_CONTROL, phy_tmp);
2641 } else if (adapter->smartspeed == EM_SMARTSPEED_DOWNSHIFT) {
2642 /* If still no link, perhaps using 2/3 pair cable */
2643 e1000_read_phy_reg(&adapter->hw, PHY_1000T_CTRL, &phy_tmp);
2644 phy_tmp |= CR_1000T_MS_ENABLE;
2645 e1000_write_phy_reg(&adapter->hw, PHY_1000T_CTRL, phy_tmp);
2646 if (adapter->hw.mac.autoneg &&
2647 !e1000_phy_setup_autoneg(&adapter->hw) &&
2648 !e1000_read_phy_reg(&adapter->hw, PHY_CONTROL, &phy_tmp)) {
2649 phy_tmp |= MII_CR_AUTO_NEG_EN | MII_CR_RESTART_AUTO_NEG;
2650 e1000_write_phy_reg(&adapter->hw, PHY_CONTROL, phy_tmp);
2654 /* Restart process after EM_SMARTSPEED_MAX iterations */
2655 if (adapter->smartspeed++ == EM_SMARTSPEED_MAX)
2656 adapter->smartspeed = 0;
2660 em_dma_malloc(struct adapter *adapter, bus_size_t size,
2661 struct em_dma_alloc *dma)
2663 dma->dma_vaddr = bus_dmamem_coherent_any(adapter->parent_dtag,
2664 EM_DBA_ALIGN, size, BUS_DMA_WAITOK,
2665 &dma->dma_tag, &dma->dma_map,
2667 if (dma->dma_vaddr == NULL)
2674 em_dma_free(struct adapter *adapter, struct em_dma_alloc *dma)
2676 if (dma->dma_tag == NULL)
2678 bus_dmamap_unload(dma->dma_tag, dma->dma_map);
2679 bus_dmamem_free(dma->dma_tag, dma->dma_vaddr, dma->dma_map);
2680 bus_dma_tag_destroy(dma->dma_tag);
2684 em_create_tx_ring(struct adapter *adapter)
2686 device_t dev = adapter->dev;
2687 struct em_buffer *tx_buffer;
2690 adapter->tx_buffer_area =
2691 kmalloc(sizeof(struct em_buffer) * adapter->num_tx_desc,
2692 M_DEVBUF, M_WAITOK | M_ZERO);
2695 * Create DMA tags for tx buffers
2697 error = bus_dma_tag_create(adapter->parent_dtag, /* parent */
2698 1, 0, /* alignment, bounds */
2699 BUS_SPACE_MAXADDR, /* lowaddr */
2700 BUS_SPACE_MAXADDR, /* highaddr */
2701 NULL, NULL, /* filter, filterarg */
2702 EM_TSO_SIZE, /* maxsize */
2703 EM_MAX_SCATTER, /* nsegments */
2704 PAGE_SIZE, /* maxsegsize */
2705 BUS_DMA_WAITOK | BUS_DMA_ALLOCNOW |
2706 BUS_DMA_ONEBPAGE, /* flags */
2709 device_printf(dev, "Unable to allocate TX DMA tag\n");
2710 kfree(adapter->tx_buffer_area, M_DEVBUF);
2711 adapter->tx_buffer_area = NULL;
2716 * Create DMA maps for tx buffers
2718 for (i = 0; i < adapter->num_tx_desc; i++) {
2719 tx_buffer = &adapter->tx_buffer_area[i];
2721 error = bus_dmamap_create(adapter->txtag,
2722 BUS_DMA_WAITOK | BUS_DMA_ONEBPAGE,
2725 device_printf(dev, "Unable to create TX DMA map\n");
2726 em_destroy_tx_ring(adapter, i);
2734 em_init_tx_ring(struct adapter *adapter)
2736 /* Clear the old ring contents */
2737 bzero(adapter->tx_desc_base,
2738 (sizeof(struct e1000_tx_desc)) * adapter->num_tx_desc);
2741 adapter->next_avail_tx_desc = 0;
2742 adapter->next_tx_to_clean = 0;
2743 adapter->num_tx_desc_avail = adapter->num_tx_desc;
2747 em_init_tx_unit(struct adapter *adapter)
2749 uint32_t tctl, tarc, tipg = 0;
2752 /* Setup the Base and Length of the Tx Descriptor Ring */
2753 bus_addr = adapter->txdma.dma_paddr;
2754 E1000_WRITE_REG(&adapter->hw, E1000_TDLEN(0),
2755 adapter->num_tx_desc * sizeof(struct e1000_tx_desc));
2756 E1000_WRITE_REG(&adapter->hw, E1000_TDBAH(0),
2757 (uint32_t)(bus_addr >> 32));
2758 E1000_WRITE_REG(&adapter->hw, E1000_TDBAL(0),
2759 (uint32_t)bus_addr);
2760 /* Setup the HW Tx Head and Tail descriptor pointers */
2761 E1000_WRITE_REG(&adapter->hw, E1000_TDT(0), 0);
2762 E1000_WRITE_REG(&adapter->hw, E1000_TDH(0), 0);
2764 /* Set the default values for the Tx Inter Packet Gap timer */
2765 switch (adapter->hw.mac.type) {
2767 tipg = DEFAULT_82542_TIPG_IPGT;
2768 tipg |= DEFAULT_82542_TIPG_IPGR1 << E1000_TIPG_IPGR1_SHIFT;
2769 tipg |= DEFAULT_82542_TIPG_IPGR2 << E1000_TIPG_IPGR2_SHIFT;
2772 case e1000_80003es2lan:
2773 tipg = DEFAULT_82543_TIPG_IPGR1;
2774 tipg |= DEFAULT_80003ES2LAN_TIPG_IPGR2 <<
2775 E1000_TIPG_IPGR2_SHIFT;
2779 if (adapter->hw.phy.media_type == e1000_media_type_fiber ||
2780 adapter->hw.phy.media_type ==
2781 e1000_media_type_internal_serdes)
2782 tipg = DEFAULT_82543_TIPG_IPGT_FIBER;
2784 tipg = DEFAULT_82543_TIPG_IPGT_COPPER;
2785 tipg |= DEFAULT_82543_TIPG_IPGR1 << E1000_TIPG_IPGR1_SHIFT;
2786 tipg |= DEFAULT_82543_TIPG_IPGR2 << E1000_TIPG_IPGR2_SHIFT;
2790 E1000_WRITE_REG(&adapter->hw, E1000_TIPG, tipg);
2792 /* NOTE: 0 is not allowed for TIDV */
2793 E1000_WRITE_REG(&adapter->hw, E1000_TIDV, 1);
2794 if(adapter->hw.mac.type >= e1000_82540)
2795 E1000_WRITE_REG(&adapter->hw, E1000_TADV, 0);
2797 if (adapter->hw.mac.type == e1000_82571 ||
2798 adapter->hw.mac.type == e1000_82572) {
2799 tarc = E1000_READ_REG(&adapter->hw, E1000_TARC(0));
2800 tarc |= SPEED_MODE_BIT;
2801 E1000_WRITE_REG(&adapter->hw, E1000_TARC(0), tarc);
2802 } else if (adapter->hw.mac.type == e1000_80003es2lan) {
2803 tarc = E1000_READ_REG(&adapter->hw, E1000_TARC(0));
2805 E1000_WRITE_REG(&adapter->hw, E1000_TARC(0), tarc);
2806 tarc = E1000_READ_REG(&adapter->hw, E1000_TARC(1));
2808 E1000_WRITE_REG(&adapter->hw, E1000_TARC(1), tarc);
2811 /* Program the Transmit Control Register */
2812 tctl = E1000_READ_REG(&adapter->hw, E1000_TCTL);
2813 tctl &= ~E1000_TCTL_CT;
2814 tctl |= E1000_TCTL_PSP | E1000_TCTL_RTLC | E1000_TCTL_EN |
2815 (E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT);
2817 if (adapter->hw.mac.type >= e1000_82571)
2818 tctl |= E1000_TCTL_MULR;
2820 /* This write will effectively turn on the transmit unit. */
2821 E1000_WRITE_REG(&adapter->hw, E1000_TCTL, tctl);
2823 if (adapter->hw.mac.type == e1000_82571 ||
2824 adapter->hw.mac.type == e1000_82572 ||
2825 adapter->hw.mac.type == e1000_80003es2lan) {
2826 /* Bit 28 of TARC1 must be cleared when MULR is enabled */
2827 tarc = E1000_READ_REG(&adapter->hw, E1000_TARC(1));
2829 E1000_WRITE_REG(&adapter->hw, E1000_TARC(1), tarc);
2834 em_destroy_tx_ring(struct adapter *adapter, int ndesc)
2836 struct em_buffer *tx_buffer;
2839 if (adapter->tx_buffer_area == NULL)
2842 for (i = 0; i < ndesc; i++) {
2843 tx_buffer = &adapter->tx_buffer_area[i];
2845 KKASSERT(tx_buffer->m_head == NULL);
2846 bus_dmamap_destroy(adapter->txtag, tx_buffer->map);
2848 bus_dma_tag_destroy(adapter->txtag);
2850 kfree(adapter->tx_buffer_area, M_DEVBUF);
2851 adapter->tx_buffer_area = NULL;
2855 * The offload context needs to be set when we transfer the first
2856 * packet of a particular protocol (TCP/UDP). This routine has been
2857 * enhanced to deal with inserted VLAN headers.
2859 * If the new packet's ether header length, ip header length and
2860 * csum offloading type are same as the previous packet, we should
2861 * avoid allocating a new csum context descriptor; mainly to take
2862 * advantage of the pipeline effect of the TX data read request.
2864 * This function returns number of TX descrptors allocated for
2868 em_txcsum(struct adapter *adapter, struct mbuf *mp,
2869 uint32_t *txd_upper, uint32_t *txd_lower)
2871 struct e1000_context_desc *TXD;
2872 int curr_txd, ehdrlen, csum_flags;
2873 uint32_t cmd, hdr_len, ip_hlen;
2875 csum_flags = mp->m_pkthdr.csum_flags & EM_CSUM_FEATURES;
2876 ip_hlen = mp->m_pkthdr.csum_iphlen;
2877 ehdrlen = mp->m_pkthdr.csum_lhlen;
2879 if (adapter->csum_lhlen == ehdrlen &&
2880 adapter->csum_iphlen == ip_hlen &&
2881 adapter->csum_flags == csum_flags) {
2883 * Same csum offload context as the previous packets;
2886 *txd_upper = adapter->csum_txd_upper;
2887 *txd_lower = adapter->csum_txd_lower;
2892 * Setup a new csum offload context.
2895 curr_txd = adapter->next_avail_tx_desc;
2896 TXD = (struct e1000_context_desc *)&adapter->tx_desc_base[curr_txd];
2900 /* Setup of IP header checksum. */
2901 if (csum_flags & CSUM_IP) {
2903 * Start offset for header checksum calculation.
2904 * End offset for header checksum calculation.
2905 * Offset of place to put the checksum.
2907 TXD->lower_setup.ip_fields.ipcss = ehdrlen;
2908 TXD->lower_setup.ip_fields.ipcse =
2909 htole16(ehdrlen + ip_hlen - 1);
2910 TXD->lower_setup.ip_fields.ipcso =
2911 ehdrlen + offsetof(struct ip, ip_sum);
2912 cmd |= E1000_TXD_CMD_IP;
2913 *txd_upper |= E1000_TXD_POPTS_IXSM << 8;
2915 hdr_len = ehdrlen + ip_hlen;
2917 if (csum_flags & CSUM_TCP) {
2919 * Start offset for payload checksum calculation.
2920 * End offset for payload checksum calculation.
2921 * Offset of place to put the checksum.
2923 TXD->upper_setup.tcp_fields.tucss = hdr_len;
2924 TXD->upper_setup.tcp_fields.tucse = htole16(0);
2925 TXD->upper_setup.tcp_fields.tucso =
2926 hdr_len + offsetof(struct tcphdr, th_sum);
2927 cmd |= E1000_TXD_CMD_TCP;
2928 *txd_upper |= E1000_TXD_POPTS_TXSM << 8;
2929 } else if (csum_flags & CSUM_UDP) {
2931 * Start offset for header checksum calculation.
2932 * End offset for header checksum calculation.
2933 * Offset of place to put the checksum.
2935 TXD->upper_setup.tcp_fields.tucss = hdr_len;
2936 TXD->upper_setup.tcp_fields.tucse = htole16(0);
2937 TXD->upper_setup.tcp_fields.tucso =
2938 hdr_len + offsetof(struct udphdr, uh_sum);
2939 *txd_upper |= E1000_TXD_POPTS_TXSM << 8;
2942 *txd_lower = E1000_TXD_CMD_DEXT | /* Extended descr type */
2943 E1000_TXD_DTYP_D; /* Data descr */
2945 /* Save the information for this csum offloading context */
2946 adapter->csum_lhlen = ehdrlen;
2947 adapter->csum_iphlen = ip_hlen;
2948 adapter->csum_flags = csum_flags;
2949 adapter->csum_txd_upper = *txd_upper;
2950 adapter->csum_txd_lower = *txd_lower;
2952 TXD->tcp_seg_setup.data = htole32(0);
2953 TXD->cmd_and_length =
2954 htole32(E1000_TXD_CMD_IFCS | E1000_TXD_CMD_DEXT | cmd);
2956 if (++curr_txd == adapter->num_tx_desc)
2959 KKASSERT(adapter->num_tx_desc_avail > 0);
2960 adapter->num_tx_desc_avail--;
2962 adapter->next_avail_tx_desc = curr_txd;
2967 em_txeof(struct adapter *adapter)
2969 struct ifnet *ifp = &adapter->arpcom.ac_if;
2970 struct em_buffer *tx_buffer;
2971 int first, num_avail;
2973 if (adapter->tx_dd_head == adapter->tx_dd_tail)
2976 if (adapter->num_tx_desc_avail == adapter->num_tx_desc)
2979 num_avail = adapter->num_tx_desc_avail;
2980 first = adapter->next_tx_to_clean;
2982 while (adapter->tx_dd_head != adapter->tx_dd_tail) {
2983 struct e1000_tx_desc *tx_desc;
2984 int dd_idx = adapter->tx_dd[adapter->tx_dd_head];
2986 tx_desc = &adapter->tx_desc_base[dd_idx];
2987 if (tx_desc->upper.fields.status & E1000_TXD_STAT_DD) {
2988 EM_INC_TXDD_IDX(adapter->tx_dd_head);
2990 if (++dd_idx == adapter->num_tx_desc)
2993 while (first != dd_idx) {
2998 tx_buffer = &adapter->tx_buffer_area[first];
2999 if (tx_buffer->m_head) {
3000 bus_dmamap_unload(adapter->txtag,
3002 m_freem(tx_buffer->m_head);
3003 tx_buffer->m_head = NULL;
3006 if (++first == adapter->num_tx_desc)
3013 adapter->next_tx_to_clean = first;
3014 adapter->num_tx_desc_avail = num_avail;
3016 if (adapter->tx_dd_head == adapter->tx_dd_tail) {
3017 adapter->tx_dd_head = 0;
3018 adapter->tx_dd_tail = 0;
3021 if (!EM_IS_OACTIVE(adapter)) {
3022 ifq_clr_oactive(&ifp->if_snd);
3024 /* All clean, turn off the timer */
3025 if (adapter->num_tx_desc_avail == adapter->num_tx_desc)
3031 em_tx_collect(struct adapter *adapter)
3033 struct ifnet *ifp = &adapter->arpcom.ac_if;
3034 struct em_buffer *tx_buffer;
3035 int tdh, first, num_avail, dd_idx = -1;
3037 if (adapter->num_tx_desc_avail == adapter->num_tx_desc)
3040 tdh = E1000_READ_REG(&adapter->hw, E1000_TDH(0));
3041 if (tdh == adapter->next_tx_to_clean)
3044 if (adapter->tx_dd_head != adapter->tx_dd_tail)
3045 dd_idx = adapter->tx_dd[adapter->tx_dd_head];
3047 num_avail = adapter->num_tx_desc_avail;
3048 first = adapter->next_tx_to_clean;
3050 while (first != tdh) {
3055 tx_buffer = &adapter->tx_buffer_area[first];
3056 if (tx_buffer->m_head) {
3057 bus_dmamap_unload(adapter->txtag,
3059 m_freem(tx_buffer->m_head);
3060 tx_buffer->m_head = NULL;
3063 if (first == dd_idx) {
3064 EM_INC_TXDD_IDX(adapter->tx_dd_head);
3065 if (adapter->tx_dd_head == adapter->tx_dd_tail) {
3066 adapter->tx_dd_head = 0;
3067 adapter->tx_dd_tail = 0;
3070 dd_idx = adapter->tx_dd[adapter->tx_dd_head];
3074 if (++first == adapter->num_tx_desc)
3077 adapter->next_tx_to_clean = first;
3078 adapter->num_tx_desc_avail = num_avail;
3080 if (!EM_IS_OACTIVE(adapter)) {
3081 ifq_clr_oactive(&ifp->if_snd);
3083 /* All clean, turn off the timer */
3084 if (adapter->num_tx_desc_avail == adapter->num_tx_desc)
3090 * When Link is lost sometimes there is work still in the TX ring
3091 * which will result in a watchdog, rather than allow that do an
3092 * attempted cleanup and then reinit here. Note that this has been
3093 * seens mostly with fiber adapters.
3096 em_tx_purge(struct adapter *adapter)
3098 struct ifnet *ifp = &adapter->arpcom.ac_if;
3100 if (!adapter->link_active && ifp->if_timer) {
3101 em_tx_collect(adapter);
3102 if (ifp->if_timer) {
3103 if_printf(ifp, "Link lost, TX pending, reinit\n");
3111 em_newbuf(struct adapter *adapter, int i, int init)
3114 bus_dma_segment_t seg;
3116 struct em_buffer *rx_buffer;
3119 m = m_getcl(init ? M_WAITOK : M_NOWAIT, MT_DATA, M_PKTHDR);
3121 adapter->mbuf_cluster_failed++;
3123 if_printf(&adapter->arpcom.ac_if,
3124 "Unable to allocate RX mbuf\n");
3128 m->m_len = m->m_pkthdr.len = MCLBYTES;
3130 if (adapter->hw.mac.max_frame_size <= MCLBYTES - ETHER_ALIGN)
3131 m_adj(m, ETHER_ALIGN);
3133 error = bus_dmamap_load_mbuf_segment(adapter->rxtag,
3134 adapter->rx_sparemap, m,
3135 &seg, 1, &nseg, BUS_DMA_NOWAIT);
3139 if_printf(&adapter->arpcom.ac_if,
3140 "Unable to load RX mbuf\n");
3145 rx_buffer = &adapter->rx_buffer_area[i];
3146 if (rx_buffer->m_head != NULL)
3147 bus_dmamap_unload(adapter->rxtag, rx_buffer->map);
3149 map = rx_buffer->map;
3150 rx_buffer->map = adapter->rx_sparemap;
3151 adapter->rx_sparemap = map;
3153 rx_buffer->m_head = m;
3155 adapter->rx_desc_base[i].buffer_addr = htole64(seg.ds_addr);
3160 em_create_rx_ring(struct adapter *adapter)
3162 device_t dev = adapter->dev;
3163 struct em_buffer *rx_buffer;
3166 adapter->rx_buffer_area =
3167 kmalloc(sizeof(struct em_buffer) * adapter->num_rx_desc,
3168 M_DEVBUF, M_WAITOK | M_ZERO);
3171 * Create DMA tag for rx buffers
3173 error = bus_dma_tag_create(adapter->parent_dtag, /* parent */
3174 1, 0, /* alignment, bounds */
3175 BUS_SPACE_MAXADDR, /* lowaddr */
3176 BUS_SPACE_MAXADDR, /* highaddr */
3177 NULL, NULL, /* filter, filterarg */
3178 MCLBYTES, /* maxsize */
3180 MCLBYTES, /* maxsegsize */
3181 BUS_DMA_WAITOK | BUS_DMA_ALLOCNOW, /* flags */
3184 device_printf(dev, "Unable to allocate RX DMA tag\n");
3185 kfree(adapter->rx_buffer_area, M_DEVBUF);
3186 adapter->rx_buffer_area = NULL;
3191 * Create spare DMA map for rx buffers
3193 error = bus_dmamap_create(adapter->rxtag, BUS_DMA_WAITOK,
3194 &adapter->rx_sparemap);
3196 device_printf(dev, "Unable to create spare RX DMA map\n");
3197 bus_dma_tag_destroy(adapter->rxtag);
3198 kfree(adapter->rx_buffer_area, M_DEVBUF);
3199 adapter->rx_buffer_area = NULL;
3204 * Create DMA maps for rx buffers
3206 for (i = 0; i < adapter->num_rx_desc; i++) {
3207 rx_buffer = &adapter->rx_buffer_area[i];
3209 error = bus_dmamap_create(adapter->rxtag, BUS_DMA_WAITOK,
3212 device_printf(dev, "Unable to create RX DMA map\n");
3213 em_destroy_rx_ring(adapter, i);
3221 em_init_rx_ring(struct adapter *adapter)
3225 /* Reset descriptor ring */
3226 bzero(adapter->rx_desc_base,
3227 (sizeof(struct e1000_rx_desc)) * adapter->num_rx_desc);
3229 /* Allocate new ones. */
3230 for (i = 0; i < adapter->num_rx_desc; i++) {
3231 error = em_newbuf(adapter, i, 1);
3236 /* Setup our descriptor pointers */
3237 adapter->next_rx_desc_to_check = 0;
3243 em_init_rx_unit(struct adapter *adapter)
3245 struct ifnet *ifp = &adapter->arpcom.ac_if;
3250 * Make sure receives are disabled while setting
3251 * up the descriptor ring
3253 rctl = E1000_READ_REG(&adapter->hw, E1000_RCTL);
3254 E1000_WRITE_REG(&adapter->hw, E1000_RCTL, rctl & ~E1000_RCTL_EN);
3256 if (adapter->hw.mac.type >= e1000_82540) {
3260 * Set the interrupt throttling rate. Value is calculated
3261 * as ITR = 1 / (INT_THROTTLE_CEIL * 256ns)
3263 if (adapter->int_throttle_ceil)
3264 itr = 1000000000 / 256 / adapter->int_throttle_ceil;
3267 em_set_itr(adapter, itr);
3270 /* Disable accelerated ackknowledge */
3271 if (adapter->hw.mac.type == e1000_82574) {
3272 E1000_WRITE_REG(&adapter->hw,
3273 E1000_RFCTL, E1000_RFCTL_ACK_DIS);
3276 /* Receive Checksum Offload for TCP and UDP */
3277 if (ifp->if_capenable & IFCAP_RXCSUM) {
3280 rxcsum = E1000_READ_REG(&adapter->hw, E1000_RXCSUM);
3281 rxcsum |= (E1000_RXCSUM_IPOFL | E1000_RXCSUM_TUOFL);
3282 E1000_WRITE_REG(&adapter->hw, E1000_RXCSUM, rxcsum);
3286 * XXX TEMPORARY WORKAROUND: on some systems with 82573
3287 * long latencies are observed, like Lenovo X60. This
3288 * change eliminates the problem, but since having positive
3289 * values in RDTR is a known source of problems on other
3290 * platforms another solution is being sought.
3292 if (em_82573_workaround && adapter->hw.mac.type == e1000_82573) {
3293 E1000_WRITE_REG(&adapter->hw, E1000_RADV, EM_RADV_82573);
3294 E1000_WRITE_REG(&adapter->hw, E1000_RDTR, EM_RDTR_82573);
3298 * Setup the Base and Length of the Rx Descriptor Ring
3300 bus_addr = adapter->rxdma.dma_paddr;
3301 E1000_WRITE_REG(&adapter->hw, E1000_RDLEN(0),
3302 adapter->num_rx_desc * sizeof(struct e1000_rx_desc));
3303 E1000_WRITE_REG(&adapter->hw, E1000_RDBAH(0),
3304 (uint32_t)(bus_addr >> 32));
3305 E1000_WRITE_REG(&adapter->hw, E1000_RDBAL(0),
3306 (uint32_t)bus_addr);
3309 * Setup the HW Rx Head and Tail Descriptor Pointers
3311 E1000_WRITE_REG(&adapter->hw, E1000_RDH(0), 0);
3312 E1000_WRITE_REG(&adapter->hw, E1000_RDT(0), adapter->num_rx_desc - 1);
3314 /* Set PTHRESH for improved jumbo performance */
3315 if (((adapter->hw.mac.type == e1000_ich9lan) ||
3316 (adapter->hw.mac.type == e1000_pch2lan) ||
3317 (adapter->hw.mac.type == e1000_ich10lan)) &&
3318 (ifp->if_mtu > ETHERMTU)) {
3321 rxdctl = E1000_READ_REG(&adapter->hw, E1000_RXDCTL(0));
3322 E1000_WRITE_REG(&adapter->hw, E1000_RXDCTL(0), rxdctl | 3);
3325 if (adapter->hw.mac.type >= e1000_pch2lan) {
3326 if (ifp->if_mtu > ETHERMTU)
3327 e1000_lv_jumbo_workaround_ich8lan(&adapter->hw, TRUE);
3329 e1000_lv_jumbo_workaround_ich8lan(&adapter->hw, FALSE);
3332 /* Setup the Receive Control Register */
3333 rctl &= ~(3 << E1000_RCTL_MO_SHIFT);
3334 rctl |= E1000_RCTL_EN | E1000_RCTL_BAM | E1000_RCTL_LBM_NO |
3335 E1000_RCTL_RDMTS_HALF |
3336 (adapter->hw.mac.mc_filter_type << E1000_RCTL_MO_SHIFT);
3338 /* Make sure VLAN Filters are off */
3339 rctl &= ~E1000_RCTL_VFE;
3341 if (e1000_tbi_sbp_enabled_82543(&adapter->hw))
3342 rctl |= E1000_RCTL_SBP;
3344 rctl &= ~E1000_RCTL_SBP;
3346 switch (adapter->rx_buffer_len) {
3349 rctl |= E1000_RCTL_SZ_2048;
3353 rctl |= E1000_RCTL_SZ_4096 |
3354 E1000_RCTL_BSEX | E1000_RCTL_LPE;
3358 rctl |= E1000_RCTL_SZ_8192 |
3359 E1000_RCTL_BSEX | E1000_RCTL_LPE;
3363 rctl |= E1000_RCTL_SZ_16384 |
3364 E1000_RCTL_BSEX | E1000_RCTL_LPE;
3368 if (ifp->if_mtu > ETHERMTU)
3369 rctl |= E1000_RCTL_LPE;
3371 rctl &= ~E1000_RCTL_LPE;
3373 /* Enable Receives */
3374 E1000_WRITE_REG(&adapter->hw, E1000_RCTL, rctl);
3378 em_destroy_rx_ring(struct adapter *adapter, int ndesc)
3380 struct em_buffer *rx_buffer;
3383 if (adapter->rx_buffer_area == NULL)
3386 for (i = 0; i < ndesc; i++) {
3387 rx_buffer = &adapter->rx_buffer_area[i];
3389 KKASSERT(rx_buffer->m_head == NULL);
3390 bus_dmamap_destroy(adapter->rxtag, rx_buffer->map);
3392 bus_dmamap_destroy(adapter->rxtag, adapter->rx_sparemap);
3393 bus_dma_tag_destroy(adapter->rxtag);
3395 kfree(adapter->rx_buffer_area, M_DEVBUF);
3396 adapter->rx_buffer_area = NULL;
3400 em_rxeof(struct adapter *adapter, int count)
3402 struct ifnet *ifp = &adapter->arpcom.ac_if;
3403 uint8_t status, accept_frame = 0, eop = 0;
3404 uint16_t len, desc_len, prev_len_adj;
3405 struct e1000_rx_desc *current_desc;
3409 i = adapter->next_rx_desc_to_check;
3410 current_desc = &adapter->rx_desc_base[i];
3412 if (!(current_desc->status & E1000_RXD_STAT_DD))
3415 while ((current_desc->status & E1000_RXD_STAT_DD) && count != 0) {
3416 struct mbuf *m = NULL;
3420 mp = adapter->rx_buffer_area[i].m_head;
3423 * Can't defer bus_dmamap_sync(9) because TBI_ACCEPT
3424 * needs to access the last received byte in the mbuf.
3426 bus_dmamap_sync(adapter->rxtag, adapter->rx_buffer_area[i].map,
3427 BUS_DMASYNC_POSTREAD);
3431 desc_len = le16toh(current_desc->length);
3432 status = current_desc->status;
3433 if (status & E1000_RXD_STAT_EOP) {
3436 if (desc_len < ETHER_CRC_LEN) {
3438 prev_len_adj = ETHER_CRC_LEN - desc_len;
3440 len = desc_len - ETHER_CRC_LEN;
3447 if (current_desc->errors & E1000_RXD_ERR_FRAME_ERR_MASK) {
3449 uint32_t pkt_len = desc_len;
3451 if (adapter->fmp != NULL)
3452 pkt_len += adapter->fmp->m_pkthdr.len;
3454 last_byte = *(mtod(mp, caddr_t) + desc_len - 1);
3455 if (TBI_ACCEPT(&adapter->hw, status,
3456 current_desc->errors, pkt_len, last_byte,
3457 adapter->min_frame_size,
3458 adapter->hw.mac.max_frame_size)) {
3459 e1000_tbi_adjust_stats_82543(&adapter->hw,
3460 &adapter->stats, pkt_len,
3461 adapter->hw.mac.addr,
3462 adapter->hw.mac.max_frame_size);
3471 if (em_newbuf(adapter, i, 0) != 0) {
3472 IFNET_STAT_INC(ifp, iqdrops, 1);
3476 /* Assign correct length to the current fragment */
3479 if (adapter->fmp == NULL) {
3480 mp->m_pkthdr.len = len;
3481 adapter->fmp = mp; /* Store the first mbuf */
3485 * Chain mbuf's together
3489 * Adjust length of previous mbuf in chain if
3490 * we received less than 4 bytes in the last
3493 if (prev_len_adj > 0) {
3494 adapter->lmp->m_len -= prev_len_adj;
3495 adapter->fmp->m_pkthdr.len -=
3498 adapter->lmp->m_next = mp;
3499 adapter->lmp = adapter->lmp->m_next;
3500 adapter->fmp->m_pkthdr.len += len;
3504 adapter->fmp->m_pkthdr.rcvif = ifp;
3505 IFNET_STAT_INC(ifp, ipackets, 1);
3507 if (ifp->if_capenable & IFCAP_RXCSUM) {
3508 em_rxcsum(adapter, current_desc,
3512 if (status & E1000_RXD_STAT_VP) {
3513 adapter->fmp->m_pkthdr.ether_vlantag =
3514 (le16toh(current_desc->special) &
3515 E1000_RXD_SPC_VLAN_MASK);
3516 adapter->fmp->m_flags |= M_VLANTAG;
3519 adapter->fmp = NULL;
3520 adapter->lmp = NULL;
3523 IFNET_STAT_INC(ifp, ierrors, 1);
3526 /* Reuse loaded DMA map and just update mbuf chain */
3527 mp = adapter->rx_buffer_area[i].m_head;
3528 mp->m_len = mp->m_pkthdr.len = MCLBYTES;
3529 mp->m_data = mp->m_ext.ext_buf;
3531 if (adapter->hw.mac.max_frame_size <=
3532 (MCLBYTES - ETHER_ALIGN))
3533 m_adj(mp, ETHER_ALIGN);
3535 if (adapter->fmp != NULL) {
3536 m_freem(adapter->fmp);
3537 adapter->fmp = NULL;
3538 adapter->lmp = NULL;
3543 /* Zero out the receive descriptors status. */
3544 current_desc->status = 0;
3547 ifp->if_input(ifp, m, NULL, -1);
3549 /* Advance our pointers to the next descriptor. */
3550 if (++i == adapter->num_rx_desc)
3552 current_desc = &adapter->rx_desc_base[i];
3554 adapter->next_rx_desc_to_check = i;
3556 /* Advance the E1000's Receive Queue #0 "Tail Pointer". */
3558 i = adapter->num_rx_desc - 1;
3559 E1000_WRITE_REG(&adapter->hw, E1000_RDT(0), i);
3563 em_rxcsum(struct adapter *adapter, struct e1000_rx_desc *rx_desc,
3566 /* 82543 or newer only */
3567 if (adapter->hw.mac.type < e1000_82543 ||
3568 /* Ignore Checksum bit is set */
3569 (rx_desc->status & E1000_RXD_STAT_IXSM))
3572 if ((rx_desc->status & E1000_RXD_STAT_IPCS) &&
3573 !(rx_desc->errors & E1000_RXD_ERR_IPE)) {
3574 /* IP Checksum Good */
3575 mp->m_pkthdr.csum_flags |= CSUM_IP_CHECKED | CSUM_IP_VALID;
3578 if ((rx_desc->status & E1000_RXD_STAT_TCPCS) &&
3579 !(rx_desc->errors & E1000_RXD_ERR_TCPE)) {
3580 mp->m_pkthdr.csum_flags |= CSUM_DATA_VALID |
3582 CSUM_FRAG_NOT_CHECKED;
3583 mp->m_pkthdr.csum_data = htons(0xffff);
3588 em_enable_intr(struct adapter *adapter)
3590 uint32_t ims_mask = IMS_ENABLE_MASK;
3592 lwkt_serialize_handler_enable(adapter->arpcom.ac_if.if_serializer);
3596 if (adapter->hw.mac.type == e1000_82574) {
3597 E1000_WRITE_REG(&adapter->hw, EM_EIAC, EM_MSIX_MASK);
3598 ims_mask |= EM_MSIX_MASK;
3601 E1000_WRITE_REG(&adapter->hw, E1000_IMS, ims_mask);
3605 em_disable_intr(struct adapter *adapter)
3607 uint32_t clear = 0xffffffff;
3610 * The first version of 82542 had an errata where when link was forced
3611 * it would stay up even up even if the cable was disconnected.
3612 * Sequence errors were used to detect the disconnect and then the
3613 * driver would unforce the link. This code in the in the ISR. For
3614 * this to work correctly the Sequence error interrupt had to be
3615 * enabled all the time.
3617 if (adapter->hw.mac.type == e1000_82542 &&
3618 adapter->hw.revision_id == E1000_REVISION_2)
3619 clear &= ~E1000_ICR_RXSEQ;
3620 else if (adapter->hw.mac.type == e1000_82574)
3621 E1000_WRITE_REG(&adapter->hw, EM_EIAC, 0);
3623 E1000_WRITE_REG(&adapter->hw, E1000_IMC, clear);
3625 adapter->npoll.ifpc_stcount = 0;
3627 lwkt_serialize_handler_disable(adapter->arpcom.ac_if.if_serializer);
3631 * Bit of a misnomer, what this really means is
3632 * to enable OS management of the system... aka
3633 * to disable special hardware management features
3636 em_get_mgmt(struct adapter *adapter)
3638 /* A shared code workaround */
3639 #define E1000_82542_MANC2H E1000_MANC2H
3640 if (adapter->flags & EM_FLAG_HAS_MGMT) {
3641 int manc2h = E1000_READ_REG(&adapter->hw, E1000_MANC2H);
3642 int manc = E1000_READ_REG(&adapter->hw, E1000_MANC);
3644 /* disable hardware interception of ARP */
3645 manc &= ~(E1000_MANC_ARP_EN);
3647 /* enable receiving management packets to the host */
3648 if (adapter->hw.mac.type >= e1000_82571) {
3649 manc |= E1000_MANC_EN_MNG2HOST;
3650 #define E1000_MNG2HOST_PORT_623 (1 << 5)
3651 #define E1000_MNG2HOST_PORT_664 (1 << 6)
3652 manc2h |= E1000_MNG2HOST_PORT_623;
3653 manc2h |= E1000_MNG2HOST_PORT_664;
3654 E1000_WRITE_REG(&adapter->hw, E1000_MANC2H, manc2h);
3657 E1000_WRITE_REG(&adapter->hw, E1000_MANC, manc);
3662 * Give control back to hardware management
3663 * controller if there is one.
3666 em_rel_mgmt(struct adapter *adapter)
3668 if (adapter->flags & EM_FLAG_HAS_MGMT) {
3669 int manc = E1000_READ_REG(&adapter->hw, E1000_MANC);
3671 /* re-enable hardware interception of ARP */
3672 manc |= E1000_MANC_ARP_EN;
3674 if (adapter->hw.mac.type >= e1000_82571)
3675 manc &= ~E1000_MANC_EN_MNG2HOST;
3677 E1000_WRITE_REG(&adapter->hw, E1000_MANC, manc);
3682 * em_get_hw_control() sets {CTRL_EXT|FWSM}:DRV_LOAD bit.
3683 * For ASF and Pass Through versions of f/w this means that
3684 * the driver is loaded. For AMT version (only with 82573)
3685 * of the f/w this means that the network i/f is open.
3688 em_get_hw_control(struct adapter *adapter)
3690 /* Let firmware know the driver has taken over */
3691 if (adapter->hw.mac.type == e1000_82573) {
3694 swsm = E1000_READ_REG(&adapter->hw, E1000_SWSM);
3695 E1000_WRITE_REG(&adapter->hw, E1000_SWSM,
3696 swsm | E1000_SWSM_DRV_LOAD);
3700 ctrl_ext = E1000_READ_REG(&adapter->hw, E1000_CTRL_EXT);
3701 E1000_WRITE_REG(&adapter->hw, E1000_CTRL_EXT,
3702 ctrl_ext | E1000_CTRL_EXT_DRV_LOAD);
3704 adapter->flags |= EM_FLAG_HW_CTRL;
3708 * em_rel_hw_control() resets {CTRL_EXT|FWSM}:DRV_LOAD bit.
3709 * For ASF and Pass Through versions of f/w this means that the
3710 * driver is no longer loaded. For AMT version (only with 82573)
3711 * of the f/w this means that the network i/f is closed.
3714 em_rel_hw_control(struct adapter *adapter)
3716 if ((adapter->flags & EM_FLAG_HW_CTRL) == 0)
3718 adapter->flags &= ~EM_FLAG_HW_CTRL;
3720 /* Let firmware taken over control of h/w */
3721 if (adapter->hw.mac.type == e1000_82573) {
3724 swsm = E1000_READ_REG(&adapter->hw, E1000_SWSM);
3725 E1000_WRITE_REG(&adapter->hw, E1000_SWSM,
3726 swsm & ~E1000_SWSM_DRV_LOAD);
3730 ctrl_ext = E1000_READ_REG(&adapter->hw, E1000_CTRL_EXT);
3731 E1000_WRITE_REG(&adapter->hw, E1000_CTRL_EXT,
3732 ctrl_ext & ~E1000_CTRL_EXT_DRV_LOAD);
3737 em_is_valid_eaddr(const uint8_t *addr)
3739 char zero_addr[ETHER_ADDR_LEN] = { 0, 0, 0, 0, 0, 0 };
3741 if ((addr[0] & 1) || !bcmp(addr, zero_addr, ETHER_ADDR_LEN))
3748 * Enable PCI Wake On Lan capability
3751 em_enable_wol(device_t dev)
3753 uint16_t cap, status;
3756 /* First find the capabilities pointer*/
3757 cap = pci_read_config(dev, PCIR_CAP_PTR, 2);
3759 /* Read the PM Capabilities */
3760 id = pci_read_config(dev, cap, 1);
3761 if (id != PCIY_PMG) /* Something wrong */
3765 * OK, we have the power capabilities,
3766 * so now get the status register
3768 cap += PCIR_POWER_STATUS;
3769 status = pci_read_config(dev, cap, 2);
3770 status |= PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE;
3771 pci_write_config(dev, cap, status, 2);
3776 * 82544 Coexistence issue workaround.
3777 * There are 2 issues.
3778 * 1. Transmit Hang issue.
3779 * To detect this issue, following equation can be used...
3780 * SIZE[3:0] + ADDR[2:0] = SUM[3:0].
3781 * If SUM[3:0] is in between 1 to 4, we will have this issue.
3784 * To detect this issue, following equation can be used...
3785 * SIZE[3:0] + ADDR[2:0] = SUM[3:0].
3786 * If SUM[3:0] is in between 9 to c, we will have this issue.
3789 * Make sure we do not have ending address
3790 * as 1,2,3,4(Hang) or 9,a,b,c (DAC)
3793 em_82544_fill_desc(bus_addr_t address, uint32_t length, PDESC_ARRAY desc_array)
3795 uint32_t safe_terminator;
3798 * Since issue is sensitive to length and address.
3799 * Let us first check the address...
3802 desc_array->descriptor[0].address = address;
3803 desc_array->descriptor[0].length = length;
3804 desc_array->elements = 1;
3805 return (desc_array->elements);
3809 (uint32_t)((((uint32_t)address & 0x7) + (length & 0xF)) & 0xF);
3811 /* If it does not fall between 0x1 to 0x4 and 0x9 to 0xC then return */
3812 if (safe_terminator == 0 ||
3813 (safe_terminator > 4 && safe_terminator < 9) ||
3814 (safe_terminator > 0xC && safe_terminator <= 0xF)) {
3815 desc_array->descriptor[0].address = address;
3816 desc_array->descriptor[0].length = length;
3817 desc_array->elements = 1;
3818 return (desc_array->elements);
3821 desc_array->descriptor[0].address = address;
3822 desc_array->descriptor[0].length = length - 4;
3823 desc_array->descriptor[1].address = address + (length - 4);
3824 desc_array->descriptor[1].length = 4;
3825 desc_array->elements = 2;
3826 return (desc_array->elements);
3830 em_update_stats(struct adapter *adapter)
3832 struct ifnet *ifp = &adapter->arpcom.ac_if;
3834 if (adapter->hw.phy.media_type == e1000_media_type_copper ||
3835 (E1000_READ_REG(&adapter->hw, E1000_STATUS) & E1000_STATUS_LU)) {
3836 adapter->stats.symerrs +=
3837 E1000_READ_REG(&adapter->hw, E1000_SYMERRS);
3838 adapter->stats.sec += E1000_READ_REG(&adapter->hw, E1000_SEC);
3840 adapter->stats.crcerrs += E1000_READ_REG(&adapter->hw, E1000_CRCERRS);
3841 adapter->stats.mpc += E1000_READ_REG(&adapter->hw, E1000_MPC);
3842 adapter->stats.scc += E1000_READ_REG(&adapter->hw, E1000_SCC);
3843 adapter->stats.ecol += E1000_READ_REG(&adapter->hw, E1000_ECOL);
3845 adapter->stats.mcc += E1000_READ_REG(&adapter->hw, E1000_MCC);
3846 adapter->stats.latecol += E1000_READ_REG(&adapter->hw, E1000_LATECOL);
3847 adapter->stats.colc += E1000_READ_REG(&adapter->hw, E1000_COLC);
3848 adapter->stats.dc += E1000_READ_REG(&adapter->hw, E1000_DC);
3849 adapter->stats.rlec += E1000_READ_REG(&adapter->hw, E1000_RLEC);
3850 adapter->stats.xonrxc += E1000_READ_REG(&adapter->hw, E1000_XONRXC);
3851 adapter->stats.xontxc += E1000_READ_REG(&adapter->hw, E1000_XONTXC);
3852 adapter->stats.xoffrxc += E1000_READ_REG(&adapter->hw, E1000_XOFFRXC);
3853 adapter->stats.xofftxc += E1000_READ_REG(&adapter->hw, E1000_XOFFTXC);
3854 adapter->stats.fcruc += E1000_READ_REG(&adapter->hw, E1000_FCRUC);
3855 adapter->stats.prc64 += E1000_READ_REG(&adapter->hw, E1000_PRC64);
3856 adapter->stats.prc127 += E1000_READ_REG(&adapter->hw, E1000_PRC127);
3857 adapter->stats.prc255 += E1000_READ_REG(&adapter->hw, E1000_PRC255);
3858 adapter->stats.prc511 += E1000_READ_REG(&adapter->hw, E1000_PRC511);
3859 adapter->stats.prc1023 += E1000_READ_REG(&adapter->hw, E1000_PRC1023);
3860 adapter->stats.prc1522 += E1000_READ_REG(&adapter->hw, E1000_PRC1522);
3861 adapter->stats.gprc += E1000_READ_REG(&adapter->hw, E1000_GPRC);
3862 adapter->stats.bprc += E1000_READ_REG(&adapter->hw, E1000_BPRC);
3863 adapter->stats.mprc += E1000_READ_REG(&adapter->hw, E1000_MPRC);
3864 adapter->stats.gptc += E1000_READ_REG(&adapter->hw, E1000_GPTC);
3866 /* For the 64-bit byte counters the low dword must be read first. */
3867 /* Both registers clear on the read of the high dword */
3869 adapter->stats.gorc += E1000_READ_REG(&adapter->hw, E1000_GORCH);
3870 adapter->stats.gotc += E1000_READ_REG(&adapter->hw, E1000_GOTCH);
3872 adapter->stats.rnbc += E1000_READ_REG(&adapter->hw, E1000_RNBC);
3873 adapter->stats.ruc += E1000_READ_REG(&adapter->hw, E1000_RUC);
3874 adapter->stats.rfc += E1000_READ_REG(&adapter->hw, E1000_RFC);
3875 adapter->stats.roc += E1000_READ_REG(&adapter->hw, E1000_ROC);
3876 adapter->stats.rjc += E1000_READ_REG(&adapter->hw, E1000_RJC);
3878 adapter->stats.tor += E1000_READ_REG(&adapter->hw, E1000_TORH);
3879 adapter->stats.tot += E1000_READ_REG(&adapter->hw, E1000_TOTH);
3881 adapter->stats.tpr += E1000_READ_REG(&adapter->hw, E1000_TPR);
3882 adapter->stats.tpt += E1000_READ_REG(&adapter->hw, E1000_TPT);
3883 adapter->stats.ptc64 += E1000_READ_REG(&adapter->hw, E1000_PTC64);
3884 adapter->stats.ptc127 += E1000_READ_REG(&adapter->hw, E1000_PTC127);
3885 adapter->stats.ptc255 += E1000_READ_REG(&adapter->hw, E1000_PTC255);
3886 adapter->stats.ptc511 += E1000_READ_REG(&adapter->hw, E1000_PTC511);
3887 adapter->stats.ptc1023 += E1000_READ_REG(&adapter->hw, E1000_PTC1023);
3888 adapter->stats.ptc1522 += E1000_READ_REG(&adapter->hw, E1000_PTC1522);
3889 adapter->stats.mptc += E1000_READ_REG(&adapter->hw, E1000_MPTC);
3890 adapter->stats.bptc += E1000_READ_REG(&adapter->hw, E1000_BPTC);
3892 if (adapter->hw.mac.type >= e1000_82543) {
3893 adapter->stats.algnerrc +=
3894 E1000_READ_REG(&adapter->hw, E1000_ALGNERRC);
3895 adapter->stats.rxerrc +=
3896 E1000_READ_REG(&adapter->hw, E1000_RXERRC);
3897 adapter->stats.tncrs +=
3898 E1000_READ_REG(&adapter->hw, E1000_TNCRS);
3899 adapter->stats.cexterr +=
3900 E1000_READ_REG(&adapter->hw, E1000_CEXTERR);
3901 adapter->stats.tsctc +=
3902 E1000_READ_REG(&adapter->hw, E1000_TSCTC);
3903 adapter->stats.tsctfc +=
3904 E1000_READ_REG(&adapter->hw, E1000_TSCTFC);
3907 IFNET_STAT_SET(ifp, collisions, adapter->stats.colc);
3910 IFNET_STAT_SET(ifp, ierrors,
3911 adapter->dropped_pkts + adapter->stats.rxerrc +
3912 adapter->stats.crcerrs + adapter->stats.algnerrc +
3913 adapter->stats.ruc + adapter->stats.roc +
3914 adapter->stats.mpc + adapter->stats.cexterr);
3917 IFNET_STAT_SET(ifp, oerrors,
3918 adapter->stats.ecol + adapter->stats.latecol +
3919 adapter->watchdog_events);
3923 em_print_debug_info(struct adapter *adapter)
3925 device_t dev = adapter->dev;
3926 uint8_t *hw_addr = adapter->hw.hw_addr;
3928 device_printf(dev, "Adapter hardware address = %p \n", hw_addr);
3929 device_printf(dev, "CTRL = 0x%x RCTL = 0x%x \n",
3930 E1000_READ_REG(&adapter->hw, E1000_CTRL),
3931 E1000_READ_REG(&adapter->hw, E1000_RCTL));
3932 device_printf(dev, "Packet buffer = Tx=%dk Rx=%dk \n",
3933 ((E1000_READ_REG(&adapter->hw, E1000_PBA) & 0xffff0000) >> 16),\
3934 (E1000_READ_REG(&adapter->hw, E1000_PBA) & 0xffff) );
3935 device_printf(dev, "Flow control watermarks high = %d low = %d\n",
3936 adapter->hw.fc.high_water,
3937 adapter->hw.fc.low_water);
3938 device_printf(dev, "tx_int_delay = %d, tx_abs_int_delay = %d\n",
3939 E1000_READ_REG(&adapter->hw, E1000_TIDV),
3940 E1000_READ_REG(&adapter->hw, E1000_TADV));
3941 device_printf(dev, "rx_int_delay = %d, rx_abs_int_delay = %d\n",
3942 E1000_READ_REG(&adapter->hw, E1000_RDTR),
3943 E1000_READ_REG(&adapter->hw, E1000_RADV));
3944 device_printf(dev, "fifo workaround = %lld, fifo_reset_count = %lld\n",
3945 (long long)adapter->tx_fifo_wrk_cnt,
3946 (long long)adapter->tx_fifo_reset_cnt);
3947 device_printf(dev, "hw tdh = %d, hw tdt = %d\n",
3948 E1000_READ_REG(&adapter->hw, E1000_TDH(0)),
3949 E1000_READ_REG(&adapter->hw, E1000_TDT(0)));
3950 device_printf(dev, "hw rdh = %d, hw rdt = %d\n",
3951 E1000_READ_REG(&adapter->hw, E1000_RDH(0)),
3952 E1000_READ_REG(&adapter->hw, E1000_RDT(0)));
3953 device_printf(dev, "Num Tx descriptors avail = %d\n",
3954 adapter->num_tx_desc_avail);
3955 device_printf(dev, "Tx Descriptors not avail1 = %ld\n",
3956 adapter->no_tx_desc_avail1);
3957 device_printf(dev, "Tx Descriptors not avail2 = %ld\n",
3958 adapter->no_tx_desc_avail2);
3959 device_printf(dev, "Std mbuf failed = %ld\n",
3960 adapter->mbuf_alloc_failed);
3961 device_printf(dev, "Std mbuf cluster failed = %ld\n",
3962 adapter->mbuf_cluster_failed);
3963 device_printf(dev, "Driver dropped packets = %ld\n",
3964 adapter->dropped_pkts);
3965 device_printf(dev, "Driver tx dma failure in encap = %ld\n",
3966 adapter->no_tx_dma_setup);
3970 em_print_hw_stats(struct adapter *adapter)
3972 device_t dev = adapter->dev;
3974 device_printf(dev, "Excessive collisions = %lld\n",
3975 (long long)adapter->stats.ecol);
3976 #if (DEBUG_HW > 0) /* Dont output these errors normally */
3977 device_printf(dev, "Symbol errors = %lld\n",
3978 (long long)adapter->stats.symerrs);
3980 device_printf(dev, "Sequence errors = %lld\n",
3981 (long long)adapter->stats.sec);
3982 device_printf(dev, "Defer count = %lld\n",
3983 (long long)adapter->stats.dc);
3984 device_printf(dev, "Missed Packets = %lld\n",
3985 (long long)adapter->stats.mpc);
3986 device_printf(dev, "Receive No Buffers = %lld\n",
3987 (long long)adapter->stats.rnbc);
3988 /* RLEC is inaccurate on some hardware, calculate our own. */
3989 device_printf(dev, "Receive Length Errors = %lld\n",
3990 ((long long)adapter->stats.roc + (long long)adapter->stats.ruc));
3991 device_printf(dev, "Receive errors = %lld\n",
3992 (long long)adapter->stats.rxerrc);
3993 device_printf(dev, "Crc errors = %lld\n",
3994 (long long)adapter->stats.crcerrs);
3995 device_printf(dev, "Alignment errors = %lld\n",
3996 (long long)adapter->stats.algnerrc);
3997 device_printf(dev, "Collision/Carrier extension errors = %lld\n",
3998 (long long)adapter->stats.cexterr);
3999 device_printf(dev, "RX overruns = %ld\n", adapter->rx_overruns);
4000 device_printf(dev, "watchdog timeouts = %ld\n",
4001 adapter->watchdog_events);
4002 device_printf(dev, "XON Rcvd = %lld\n",
4003 (long long)adapter->stats.xonrxc);
4004 device_printf(dev, "XON Xmtd = %lld\n",
4005 (long long)adapter->stats.xontxc);
4006 device_printf(dev, "XOFF Rcvd = %lld\n",
4007 (long long)adapter->stats.xoffrxc);
4008 device_printf(dev, "XOFF Xmtd = %lld\n",
4009 (long long)adapter->stats.xofftxc);
4010 device_printf(dev, "Good Packets Rcvd = %lld\n",
4011 (long long)adapter->stats.gprc);
4012 device_printf(dev, "Good Packets Xmtd = %lld\n",
4013 (long long)adapter->stats.gptc);
4017 em_print_nvm_info(struct adapter *adapter)
4019 uint16_t eeprom_data;
4022 /* Its a bit crude, but it gets the job done */
4023 kprintf("\nInterface EEPROM Dump:\n");
4024 kprintf("Offset\n0x0000 ");
4025 for (i = 0, j = 0; i < 32; i++, j++) {
4026 if (j == 8) { /* Make the offset block */
4028 kprintf("\n0x00%x0 ",row);
4030 e1000_read_nvm(&adapter->hw, i, 1, &eeprom_data);
4031 kprintf("%04x ", eeprom_data);
4037 em_sysctl_debug_info(SYSCTL_HANDLER_ARGS)
4039 struct adapter *adapter;
4044 error = sysctl_handle_int(oidp, &result, 0, req);
4045 if (error || !req->newptr)
4048 adapter = (struct adapter *)arg1;
4049 ifp = &adapter->arpcom.ac_if;
4051 lwkt_serialize_enter(ifp->if_serializer);
4054 em_print_debug_info(adapter);
4057 * This value will cause a hex dump of the
4058 * first 32 16-bit words of the EEPROM to
4062 em_print_nvm_info(adapter);
4064 lwkt_serialize_exit(ifp->if_serializer);
4070 em_sysctl_stats(SYSCTL_HANDLER_ARGS)
4075 error = sysctl_handle_int(oidp, &result, 0, req);
4076 if (error || !req->newptr)
4080 struct adapter *adapter = (struct adapter *)arg1;
4081 struct ifnet *ifp = &adapter->arpcom.ac_if;
4083 lwkt_serialize_enter(ifp->if_serializer);
4084 em_print_hw_stats(adapter);
4085 lwkt_serialize_exit(ifp->if_serializer);
4091 em_add_sysctl(struct adapter *adapter)
4093 struct sysctl_ctx_list *ctx;
4094 struct sysctl_oid *tree;
4097 ctx = device_get_sysctl_ctx(adapter->dev);
4098 tree = device_get_sysctl_tree(adapter->dev);
4099 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree),
4100 OID_AUTO, "debug", CTLTYPE_INT|CTLFLAG_RW, adapter, 0,
4101 em_sysctl_debug_info, "I", "Debug Information");
4103 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree),
4104 OID_AUTO, "stats", CTLTYPE_INT|CTLFLAG_RW, adapter, 0,
4105 em_sysctl_stats, "I", "Statistics");
4107 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree),
4108 OID_AUTO, "rxd", CTLFLAG_RD,
4109 &adapter->num_rx_desc, 0, NULL);
4110 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree),
4111 OID_AUTO, "txd", CTLFLAG_RD,
4112 &adapter->num_tx_desc, 0, NULL);
4114 if (adapter->hw.mac.type >= e1000_82540) {
4115 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree),
4116 OID_AUTO, "int_throttle_ceil",
4117 CTLTYPE_INT|CTLFLAG_RW, adapter, 0,
4118 em_sysctl_int_throttle, "I",
4119 "interrupt throttling rate");
4121 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree),
4122 OID_AUTO, "int_tx_nsegs",
4123 CTLTYPE_INT|CTLFLAG_RW, adapter, 0,
4124 em_sysctl_int_tx_nsegs, "I",
4125 "# segments per TX interrupt");
4126 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree),
4127 OID_AUTO, "wreg_tx_nsegs", CTLFLAG_RW,
4128 &adapter->tx_wreg_nsegs, 0,
4129 "# segments before write to hardware register");
4131 access = CTLFLAG_RW;
4132 if (adapter->hw.mac.type == e1000_pchlan) {
4134 * Only pause reception is supported, so make this
4135 * sysctl read-only for PCH.
4137 access = CTLFLAG_RD;
4139 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree),
4140 OID_AUTO, "flow_ctrl", CTLTYPE_STRING|access, adapter, 0,
4141 em_sysctl_flowctrl, "A",
4142 "flow control: full, rx_pause, tx_pause, none");
4146 em_sysctl_int_throttle(SYSCTL_HANDLER_ARGS)
4148 struct adapter *adapter = (void *)arg1;
4149 struct ifnet *ifp = &adapter->arpcom.ac_if;
4150 int error, throttle;
4152 throttle = adapter->int_throttle_ceil;
4153 error = sysctl_handle_int(oidp, &throttle, 0, req);
4154 if (error || req->newptr == NULL)
4156 if (throttle < 0 || throttle > 1000000000 / 256)
4161 * Set the interrupt throttling rate in 256ns increments,
4162 * recalculate sysctl value assignment to get exact frequency.
4164 throttle = 1000000000 / 256 / throttle;
4166 /* Upper 16bits of ITR is reserved and should be zero */
4167 if (throttle & 0xffff0000)
4171 lwkt_serialize_enter(ifp->if_serializer);
4174 adapter->int_throttle_ceil = 1000000000 / 256 / throttle;
4176 adapter->int_throttle_ceil = 0;
4178 if (ifp->if_flags & IFF_RUNNING)
4179 em_set_itr(adapter, throttle);
4181 lwkt_serialize_exit(ifp->if_serializer);
4184 if_printf(ifp, "Interrupt moderation set to %d/sec\n",
4185 adapter->int_throttle_ceil);
4191 em_sysctl_int_tx_nsegs(SYSCTL_HANDLER_ARGS)
4193 struct adapter *adapter = (void *)arg1;
4194 struct ifnet *ifp = &adapter->arpcom.ac_if;
4197 segs = adapter->tx_int_nsegs;
4198 error = sysctl_handle_int(oidp, &segs, 0, req);
4199 if (error || req->newptr == NULL)
4204 lwkt_serialize_enter(ifp->if_serializer);
4207 * Don't allow int_tx_nsegs to become:
4208 * o Less the oact_tx_desc
4209 * o Too large that no TX desc will cause TX interrupt to
4210 * be generated (OACTIVE will never recover)
4211 * o Too small that will cause tx_dd[] overflow
4213 if (segs < adapter->oact_tx_desc ||
4214 segs >= adapter->num_tx_desc - adapter->oact_tx_desc ||
4215 segs < adapter->num_tx_desc / EM_TXDD_SAFE) {
4219 adapter->tx_int_nsegs = segs;
4222 lwkt_serialize_exit(ifp->if_serializer);
4228 em_set_itr(struct adapter *adapter, uint32_t itr)
4230 E1000_WRITE_REG(&adapter->hw, E1000_ITR, itr);
4231 if (adapter->hw.mac.type == e1000_82574) {
4235 * When using MSIX interrupts we need to
4236 * throttle using the EITR register
4238 for (i = 0; i < 4; ++i) {
4239 E1000_WRITE_REG(&adapter->hw,
4240 E1000_EITR_82574(i), itr);
4246 em_disable_aspm(struct adapter *adapter)
4248 uint16_t link_cap, link_ctrl, disable;
4249 uint8_t pcie_ptr, reg;
4250 device_t dev = adapter->dev;
4252 switch (adapter->hw.mac.type) {
4257 * 82573 specification update
4258 * errata #8 disable L0s
4259 * errata #41 disable L1
4261 * 82571/82572 specification update
4262 # errata #13 disable L1
4263 * errata #68 disable L0s
4265 disable = PCIEM_LNKCTL_ASPM_L0S | PCIEM_LNKCTL_ASPM_L1;
4271 * 82574 specification update errata #20
4272 * 82583 specification update errata #9
4274 * There is no need to disable L1
4276 disable = PCIEM_LNKCTL_ASPM_L0S;
4283 pcie_ptr = pci_get_pciecap_ptr(dev);
4287 link_cap = pci_read_config(dev, pcie_ptr + PCIER_LINKCAP, 2);
4288 if ((link_cap & PCIEM_LNKCAP_ASPM_MASK) == 0)
4292 if_printf(&adapter->arpcom.ac_if,
4293 "disable ASPM %#02x\n", disable);
4296 reg = pcie_ptr + PCIER_LINKCTRL;
4297 link_ctrl = pci_read_config(dev, reg, 2);
4298 link_ctrl &= ~disable;
4299 pci_write_config(dev, reg, link_ctrl, 2);
4303 em_tso_pullup(struct adapter *adapter, struct mbuf **mp)
4305 int iphlen, hoff, thoff, ex = 0;
4310 KASSERT(M_WRITABLE(m), ("TSO mbuf not writable"));
4312 iphlen = m->m_pkthdr.csum_iphlen;
4313 thoff = m->m_pkthdr.csum_thlen;
4314 hoff = m->m_pkthdr.csum_lhlen;
4316 KASSERT(iphlen > 0, ("invalid ip hlen"));
4317 KASSERT(thoff > 0, ("invalid tcp hlen"));
4318 KASSERT(hoff > 0, ("invalid ether hlen"));
4320 if (adapter->flags & EM_FLAG_TSO_PULLEX)
4323 if (m->m_len < hoff + iphlen + thoff + ex) {
4324 m = m_pullup(m, hoff + iphlen + thoff + ex);
4331 ip = mtodoff(m, struct ip *, hoff);
4338 em_tso_setup(struct adapter *adapter, struct mbuf *mp,
4339 uint32_t *txd_upper, uint32_t *txd_lower)
4341 struct e1000_context_desc *TXD;
4342 int hoff, iphlen, thoff, hlen;
4343 int mss, pktlen, curr_txd;
4345 iphlen = mp->m_pkthdr.csum_iphlen;
4346 thoff = mp->m_pkthdr.csum_thlen;
4347 hoff = mp->m_pkthdr.csum_lhlen;
4348 mss = mp->m_pkthdr.tso_segsz;
4349 pktlen = mp->m_pkthdr.len;
4351 if (adapter->csum_flags == CSUM_TSO &&
4352 adapter->csum_iphlen == iphlen &&
4353 adapter->csum_lhlen == hoff &&
4354 adapter->csum_thlen == thoff &&
4355 adapter->csum_mss == mss &&
4356 adapter->csum_pktlen == pktlen) {
4357 *txd_upper = adapter->csum_txd_upper;
4358 *txd_lower = adapter->csum_txd_lower;
4361 hlen = hoff + iphlen + thoff;
4364 * Setup a new TSO context.
4367 curr_txd = adapter->next_avail_tx_desc;
4368 TXD = (struct e1000_context_desc *)&adapter->tx_desc_base[curr_txd];
4370 *txd_lower = E1000_TXD_CMD_DEXT | /* Extended descr type */
4371 E1000_TXD_DTYP_D | /* Data descr type */
4372 E1000_TXD_CMD_TSE; /* Do TSE on this packet */
4374 /* IP and/or TCP header checksum calculation and insertion. */
4375 *txd_upper = (E1000_TXD_POPTS_IXSM | E1000_TXD_POPTS_TXSM) << 8;
4378 * Start offset for header checksum calculation.
4379 * End offset for header checksum calculation.
4380 * Offset of place put the checksum.
4382 TXD->lower_setup.ip_fields.ipcss = hoff;
4383 TXD->lower_setup.ip_fields.ipcse = htole16(hoff + iphlen - 1);
4384 TXD->lower_setup.ip_fields.ipcso = hoff + offsetof(struct ip, ip_sum);
4387 * Start offset for payload checksum calculation.
4388 * End offset for payload checksum calculation.
4389 * Offset of place to put the checksum.
4391 TXD->upper_setup.tcp_fields.tucss = hoff + iphlen;
4392 TXD->upper_setup.tcp_fields.tucse = 0;
4393 TXD->upper_setup.tcp_fields.tucso =
4394 hoff + iphlen + offsetof(struct tcphdr, th_sum);
4397 * Payload size per packet w/o any headers.
4398 * Length of all headers up to payload.
4400 TXD->tcp_seg_setup.fields.mss = htole16(mss);
4401 TXD->tcp_seg_setup.fields.hdr_len = hlen;
4402 TXD->cmd_and_length = htole32(E1000_TXD_CMD_IFCS |
4403 E1000_TXD_CMD_DEXT | /* Extended descr */
4404 E1000_TXD_CMD_TSE | /* TSE context */
4405 E1000_TXD_CMD_IP | /* Do IP csum */
4406 E1000_TXD_CMD_TCP | /* Do TCP checksum */
4407 (pktlen - hlen)); /* Total len */
4409 /* Save the information for this TSO context */
4410 adapter->csum_flags = CSUM_TSO;
4411 adapter->csum_lhlen = hoff;
4412 adapter->csum_iphlen = iphlen;
4413 adapter->csum_thlen = thoff;
4414 adapter->csum_mss = mss;
4415 adapter->csum_pktlen = pktlen;
4416 adapter->csum_txd_upper = *txd_upper;
4417 adapter->csum_txd_lower = *txd_lower;
4419 if (++curr_txd == adapter->num_tx_desc)
4422 KKASSERT(adapter->num_tx_desc_avail > 0);
4423 adapter->num_tx_desc_avail--;
4425 adapter->next_avail_tx_desc = curr_txd;
4429 static enum e1000_fc_mode
4430 em_str2fc(const char *str)
4432 if (strcmp(str, "none") == 0)
4433 return e1000_fc_none;
4434 else if (strcmp(str, "rx_pause") == 0)
4435 return e1000_fc_rx_pause;
4436 else if (strcmp(str, "tx_pause") == 0)
4437 return e1000_fc_tx_pause;
4439 return e1000_fc_full;
4443 em_fc2str(enum e1000_fc_mode fc, char *str, int len)
4445 const char *fc_str = "full";
4452 case e1000_fc_rx_pause:
4453 fc_str = "rx_pause";
4456 case e1000_fc_tx_pause:
4457 fc_str = "tx_pause";
4463 strlcpy(str, fc_str, len);
4467 em_sysctl_flowctrl(SYSCTL_HANDLER_ARGS)
4469 struct adapter *adapter = arg1;
4470 struct ifnet *ifp = &adapter->arpcom.ac_if;
4471 char flowctrl[EM_FLOWCTRL_STRLEN];
4472 enum e1000_fc_mode fc;
4475 em_fc2str(adapter->flow_ctrl, flowctrl, sizeof(flowctrl));
4476 error = sysctl_handle_string(oidp, flowctrl, sizeof(flowctrl), req);
4477 if (error != 0 || req->newptr == NULL)
4480 fc = em_str2fc(flowctrl);
4482 ifnet_serialize_all(ifp);
4483 if (fc == adapter->flow_ctrl)
4486 adapter->flow_ctrl = fc;
4487 adapter->hw.fc.requested_mode = adapter->flow_ctrl;
4488 adapter->hw.fc.current_mode = adapter->flow_ctrl;
4489 e1000_force_mac_fc(&adapter->hw);
4491 ifnet_deserialize_all(ifp);