1 /**************************************************************************
3 Copyright (c) 2001-2005, Intel Corporation
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
9 1. Redistributions of source code must retain the above copyright notice,
10 this list of conditions and the following disclaimer.
12 2. Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in the
14 documentation and/or other materials provided with the distribution.
16 3. Neither the name of the Intel Corporation nor the names of its
17 contributors may be used to endorse or promote products derived from
18 this software without specific prior written permission.
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 POSSIBILITY OF SUCH DAMAGE.
32 ***************************************************************************/
36 #include <dev/em/if_em.h>
38 /*********************************************************************
39 * Set this to one to display debug statistics
40 *********************************************************************/
41 int em_display_debug_stats = 0;
43 /*********************************************************************
44 * Linked list of board private structures for all NICs found
45 *********************************************************************/
47 struct adapter *em_adapter_list = NULL;
50 /*********************************************************************
52 *********************************************************************/
54 char em_driver_version[] = "2.1.7";
57 /*********************************************************************
60 * Used by probe to select devices to load on
61 * Last field stores an index into em_strings
62 * Last entry must be all 0s
64 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID, String Index }
65 *********************************************************************/
67 static em_vendor_info_t em_vendor_info_array[] =
69 /* Intel(R) PRO/1000 Network Connection */
70 { 0x8086, E1000_DEV_ID_82540EM, PCI_ANY_ID, PCI_ANY_ID, 0},
71 { 0x8086, E1000_DEV_ID_82540EM_LOM, PCI_ANY_ID, PCI_ANY_ID, 0},
72 { 0x8086, E1000_DEV_ID_82540EP, PCI_ANY_ID, PCI_ANY_ID, 0},
73 { 0x8086, E1000_DEV_ID_82540EP_LOM, PCI_ANY_ID, PCI_ANY_ID, 0},
74 { 0x8086, E1000_DEV_ID_82540EP_LP, PCI_ANY_ID, PCI_ANY_ID, 0},
76 { 0x8086, E1000_DEV_ID_82541EI, PCI_ANY_ID, PCI_ANY_ID, 0},
77 { 0x8086, E1000_DEV_ID_82541ER, PCI_ANY_ID, PCI_ANY_ID, 0},
78 { 0x8086, E1000_DEV_ID_82541ER_LOM, PCI_ANY_ID, PCI_ANY_ID, 0},
79 { 0x8086, E1000_DEV_ID_82541EI_MOBILE, PCI_ANY_ID, PCI_ANY_ID, 0},
80 { 0x8086, E1000_DEV_ID_82541GI, PCI_ANY_ID, PCI_ANY_ID, 0},
81 { 0x8086, E1000_DEV_ID_82541GI_LF, PCI_ANY_ID, PCI_ANY_ID, 0},
82 { 0x8086, E1000_DEV_ID_82541GI_MOBILE, PCI_ANY_ID, PCI_ANY_ID, 0},
84 { 0x8086, E1000_DEV_ID_82542, PCI_ANY_ID, PCI_ANY_ID, 0},
86 { 0x8086, E1000_DEV_ID_82543GC_FIBER, PCI_ANY_ID, PCI_ANY_ID, 0},
87 { 0x8086, E1000_DEV_ID_82543GC_COPPER, PCI_ANY_ID, PCI_ANY_ID, 0},
89 { 0x8086, E1000_DEV_ID_82544EI_COPPER, PCI_ANY_ID, PCI_ANY_ID, 0},
90 { 0x8086, E1000_DEV_ID_82544EI_FIBER, PCI_ANY_ID, PCI_ANY_ID, 0},
91 { 0x8086, E1000_DEV_ID_82544GC_COPPER, PCI_ANY_ID, PCI_ANY_ID, 0},
92 { 0x8086, E1000_DEV_ID_82544GC_LOM, PCI_ANY_ID, PCI_ANY_ID, 0},
94 { 0x8086, E1000_DEV_ID_82545EM_COPPER, PCI_ANY_ID, PCI_ANY_ID, 0},
95 { 0x8086, E1000_DEV_ID_82545EM_FIBER, PCI_ANY_ID, PCI_ANY_ID, 0},
96 { 0x8086, E1000_DEV_ID_82545GM_COPPER, PCI_ANY_ID, PCI_ANY_ID, 0},
97 { 0x8086, E1000_DEV_ID_82545GM_FIBER, PCI_ANY_ID, PCI_ANY_ID, 0},
98 { 0x8086, E1000_DEV_ID_82545GM_SERDES, PCI_ANY_ID, PCI_ANY_ID, 0},
100 { 0x8086, E1000_DEV_ID_82546EB_COPPER, PCI_ANY_ID, PCI_ANY_ID, 0},
101 { 0x8086, E1000_DEV_ID_82546EB_FIBER, PCI_ANY_ID, PCI_ANY_ID, 0},
102 { 0x8086, E1000_DEV_ID_82546EB_QUAD_COPPER, PCI_ANY_ID, PCI_ANY_ID, 0},
103 { 0x8086, E1000_DEV_ID_82546GB_COPPER, PCI_ANY_ID, PCI_ANY_ID, 0},
104 { 0x8086, E1000_DEV_ID_82546GB_FIBER, PCI_ANY_ID, PCI_ANY_ID, 0},
105 { 0x8086, E1000_DEV_ID_82546GB_SERDES, PCI_ANY_ID, PCI_ANY_ID, 0},
106 { 0x8086, E1000_DEV_ID_82546GB_PCIE, PCI_ANY_ID, PCI_ANY_ID, 0},
107 { 0x8086, E1000_DEV_ID_82546GB_QUAD_COPPER, PCI_ANY_ID, PCI_ANY_ID, 0},
109 { 0x8086, E1000_DEV_ID_82547EI, PCI_ANY_ID, PCI_ANY_ID, 0},
110 { 0x8086, E1000_DEV_ID_82547EI_MOBILE, PCI_ANY_ID, PCI_ANY_ID, 0},
111 { 0x8086, E1000_DEV_ID_82547GI, PCI_ANY_ID, PCI_ANY_ID, 0},
113 { 0x8086, E1000_DEV_ID_82573E, PCI_ANY_ID, PCI_ANY_ID, 0},
114 { 0x8086, E1000_DEV_ID_82573E_IAMT, PCI_ANY_ID, PCI_ANY_ID, 0},
116 /* required last entry */
120 /*********************************************************************
121 * Table of branding strings for all supported NICs.
122 *********************************************************************/
124 static char *em_strings[] = {
125 "Intel(R) PRO/1000 Network Connection"
128 /*********************************************************************
129 * Function prototypes
130 *********************************************************************/
131 static int em_probe(device_t);
132 static int em_attach(device_t);
133 static int em_detach(device_t);
134 static int em_shutdown(device_t);
135 static void em_intr(void *);
136 static void em_start(struct ifnet *);
137 static int em_ioctl(struct ifnet *, u_long, caddr_t);
138 static void em_watchdog(struct ifnet *);
139 static void em_init(void *);
140 static void em_init_locked(struct adapter *);
141 static void em_stop(void *);
142 static void em_media_status(struct ifnet *, struct ifmediareq *);
143 static int em_media_change(struct ifnet *);
144 static void em_identify_hardware(struct adapter *);
145 static int em_allocate_pci_resources(struct adapter *);
146 static void em_free_pci_resources(struct adapter *);
147 static void em_local_timer(void *);
148 static int em_hardware_init(struct adapter *);
149 static void em_setup_interface(device_t, struct adapter *);
150 static int em_setup_transmit_structures(struct adapter *);
151 static void em_initialize_transmit_unit(struct adapter *);
152 static int em_setup_receive_structures(struct adapter *);
153 static void em_initialize_receive_unit(struct adapter *);
154 static void em_enable_intr(struct adapter *);
155 static void em_disable_intr(struct adapter *);
156 static void em_free_transmit_structures(struct adapter *);
157 static void em_free_receive_structures(struct adapter *);
158 static void em_update_stats_counters(struct adapter *);
159 static void em_clean_transmit_interrupts(struct adapter *);
160 static int em_allocate_receive_structures(struct adapter *);
161 static int em_allocate_transmit_structures(struct adapter *);
162 static void em_process_receive_interrupts(struct adapter *, int);
163 static void em_receive_checksum(struct adapter *,
166 static void em_transmit_checksum_setup(struct adapter *,
170 static void em_set_promisc(struct adapter *);
171 static void em_disable_promisc(struct adapter *);
172 static void em_set_multi(struct adapter *);
173 static void em_print_hw_stats(struct adapter *);
174 static void em_print_link_status(struct adapter *);
175 static int em_get_buf(int i, struct adapter *,
177 static void em_enable_vlans(struct adapter *);
178 static void em_disable_vlans(struct adapter *);
179 static int em_encap(struct adapter *, struct mbuf **);
180 static void em_smartspeed(struct adapter *);
181 static int em_82547_fifo_workaround(struct adapter *, int);
182 static void em_82547_update_fifo_head(struct adapter *, int);
183 static int em_82547_tx_fifo_reset(struct adapter *);
184 static void em_82547_move_tail(void *arg);
185 static void em_82547_move_tail_locked(struct adapter *);
186 static int em_dma_malloc(struct adapter *, bus_size_t,
187 struct em_dma_alloc *, int);
188 static void em_dma_free(struct adapter *, struct em_dma_alloc *);
189 static void em_print_debug_info(struct adapter *);
190 static int em_is_valid_ether_addr(u_int8_t *);
191 static int em_sysctl_stats(SYSCTL_HANDLER_ARGS);
192 static int em_sysctl_debug_info(SYSCTL_HANDLER_ARGS);
193 static u_int32_t em_fill_descriptors (u_int64_t address,
195 PDESC_ARRAY desc_array);
196 static int em_sysctl_int_delay(SYSCTL_HANDLER_ARGS);
197 static void em_add_int_delay_sysctl(struct adapter *, const char *,
198 const char *, struct em_int_delay_info *,
201 /*********************************************************************
202 * FreeBSD Device Interface Entry Points
203 *********************************************************************/
205 static device_method_t em_methods[] = {
206 /* Device interface */
207 DEVMETHOD(device_probe, em_probe),
208 DEVMETHOD(device_attach, em_attach),
209 DEVMETHOD(device_detach, em_detach),
210 DEVMETHOD(device_shutdown, em_shutdown),
214 static driver_t em_driver = {
215 "em", em_methods, sizeof(struct adapter ),
218 static devclass_t em_devclass;
219 DRIVER_MODULE(em, pci, em_driver, em_devclass, 0, 0);
220 MODULE_DEPEND(em, pci, 1, 1, 1);
221 MODULE_DEPEND(em, ether, 1, 1, 1);
223 /*********************************************************************
224 * Tunable default values.
225 *********************************************************************/
227 #define E1000_TICKS_TO_USECS(ticks) ((1024 * (ticks) + 500) / 1000)
228 #define E1000_USECS_TO_TICKS(usecs) ((1000 * (usecs) + 512) / 1024)
230 static int em_tx_int_delay_dflt = E1000_TICKS_TO_USECS(EM_TIDV);
231 static int em_rx_int_delay_dflt = E1000_TICKS_TO_USECS(EM_RDTR);
232 static int em_tx_abs_int_delay_dflt = E1000_TICKS_TO_USECS(EM_TADV);
233 static int em_rx_abs_int_delay_dflt = E1000_TICKS_TO_USECS(EM_RADV);
235 TUNABLE_INT("hw.em.tx_int_delay", &em_tx_int_delay_dflt);
236 TUNABLE_INT("hw.em.rx_int_delay", &em_rx_int_delay_dflt);
237 TUNABLE_INT("hw.em.tx_abs_int_delay", &em_tx_abs_int_delay_dflt);
238 TUNABLE_INT("hw.em.rx_abs_int_delay", &em_rx_abs_int_delay_dflt);
240 /*********************************************************************
241 * Device identification routine
243 * em_probe determines if the driver should be loaded on
244 * adapter based on PCI vendor/device id of the adapter.
246 * return BUS_PROBE_DEFAULT on success, positive on failure
247 *********************************************************************/
250 em_probe(device_t dev)
252 em_vendor_info_t *ent;
254 u_int16_t pci_vendor_id = 0;
255 u_int16_t pci_device_id = 0;
256 u_int16_t pci_subvendor_id = 0;
257 u_int16_t pci_subdevice_id = 0;
258 char adapter_name[60];
260 INIT_DEBUGOUT("em_probe: begin");
262 pci_vendor_id = pci_get_vendor(dev);
263 if (pci_vendor_id != EM_VENDOR_ID)
266 pci_device_id = pci_get_device(dev);
267 pci_subvendor_id = pci_get_subvendor(dev);
268 pci_subdevice_id = pci_get_subdevice(dev);
270 ent = em_vendor_info_array;
271 while (ent->vendor_id != 0) {
272 if ((pci_vendor_id == ent->vendor_id) &&
273 (pci_device_id == ent->device_id) &&
275 ((pci_subvendor_id == ent->subvendor_id) ||
276 (ent->subvendor_id == PCI_ANY_ID)) &&
278 ((pci_subdevice_id == ent->subdevice_id) ||
279 (ent->subdevice_id == PCI_ANY_ID))) {
280 sprintf(adapter_name, "%s, Version - %s",
281 em_strings[ent->index],
283 device_set_desc_copy(dev, adapter_name);
284 return(BUS_PROBE_DEFAULT);
292 /*********************************************************************
293 * Device initialization routine
295 * The attach entry point is called when the driver is being loaded.
296 * This routine identifies the type of hardware, allocates all resources
297 * and initializes the hardware.
299 * return 0 on success, positive on failure
300 *********************************************************************/
303 em_attach(device_t dev)
305 struct adapter * adapter;
309 INIT_DEBUGOUT("em_attach: begin");
311 /* Allocate, clear, and link in our adapter structure */
312 if (!(adapter = device_get_softc(dev))) {
313 printf("em: adapter structure allocation failed\n");
316 bzero(adapter, sizeof(struct adapter ));
318 adapter->osdep.dev = dev;
319 adapter->unit = device_get_unit(dev);
320 EM_LOCK_INIT(adapter, device_get_nameunit(dev));
322 if (em_adapter_list != NULL)
323 em_adapter_list->prev = adapter;
324 adapter->next = em_adapter_list;
325 em_adapter_list = adapter;
328 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
329 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
330 OID_AUTO, "debug_info", CTLTYPE_INT|CTLFLAG_RW,
332 em_sysctl_debug_info, "I", "Debug Information");
334 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
335 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
336 OID_AUTO, "stats", CTLTYPE_INT|CTLFLAG_RW,
338 em_sysctl_stats, "I", "Statistics");
340 callout_init(&adapter->timer, CALLOUT_MPSAFE);
341 callout_init(&adapter->tx_fifo_timer, CALLOUT_MPSAFE);
343 /* Determine hardware revision */
344 em_identify_hardware(adapter);
346 /* Set up some sysctls for the tunable interrupt delays */
347 em_add_int_delay_sysctl(adapter, "rx_int_delay",
348 "receive interrupt delay in usecs", &adapter->rx_int_delay,
349 E1000_REG_OFFSET(&adapter->hw, RDTR), em_rx_int_delay_dflt);
350 em_add_int_delay_sysctl(adapter, "tx_int_delay",
351 "transmit interrupt delay in usecs", &adapter->tx_int_delay,
352 E1000_REG_OFFSET(&adapter->hw, TIDV), em_tx_int_delay_dflt);
353 if (adapter->hw.mac_type >= em_82540) {
354 em_add_int_delay_sysctl(adapter, "rx_abs_int_delay",
355 "receive interrupt delay limit in usecs",
356 &adapter->rx_abs_int_delay,
357 E1000_REG_OFFSET(&adapter->hw, RADV),
358 em_rx_abs_int_delay_dflt);
359 em_add_int_delay_sysctl(adapter, "tx_abs_int_delay",
360 "transmit interrupt delay limit in usecs",
361 &adapter->tx_abs_int_delay,
362 E1000_REG_OFFSET(&adapter->hw, TADV),
363 em_tx_abs_int_delay_dflt);
366 /* Parameters (to be read from user) */
367 adapter->num_tx_desc = EM_MAX_TXD;
368 adapter->num_rx_desc = EM_MAX_RXD;
369 adapter->hw.autoneg = DO_AUTO_NEG;
370 adapter->hw.wait_autoneg_complete = WAIT_FOR_AUTO_NEG_DEFAULT;
371 adapter->hw.autoneg_advertised = AUTONEG_ADV_DEFAULT;
372 adapter->hw.tbi_compatibility_en = TRUE;
373 adapter->rx_buffer_len = EM_RXBUFFER_2048;
376 * These parameters control the automatic generation(Tx) and
377 * response(Rx) to Ethernet PAUSE frames.
379 adapter->hw.fc_high_water = FC_DEFAULT_HI_THRESH;
380 adapter->hw.fc_low_water = FC_DEFAULT_LO_THRESH;
381 adapter->hw.fc_pause_time = FC_DEFAULT_TX_TIMER;
382 adapter->hw.fc_send_xon = TRUE;
383 adapter->hw.fc = em_fc_full;
385 adapter->hw.phy_init_script = 1;
386 adapter->hw.phy_reset_disable = FALSE;
388 #ifndef EM_MASTER_SLAVE
389 adapter->hw.master_slave = em_ms_hw_default;
391 adapter->hw.master_slave = EM_MASTER_SLAVE;
394 * Set the max frame size assuming standard ethernet
397 adapter->hw.max_frame_size =
398 ETHERMTU + ETHER_HDR_LEN + ETHER_CRC_LEN;
400 adapter->hw.min_frame_size =
401 MINIMUM_ETHERNET_PACKET_SIZE + ETHER_CRC_LEN;
404 * This controls when hardware reports transmit completion
407 adapter->hw.report_tx_early = 1;
410 if (em_allocate_pci_resources(adapter)) {
411 printf("em%d: Allocation of PCI resources failed\n",
418 /* Initialize eeprom parameters */
419 em_init_eeprom_params(&adapter->hw);
421 tsize = EM_ROUNDUP(adapter->num_tx_desc *
422 sizeof(struct em_tx_desc), 4096);
424 /* Allocate Transmit Descriptor ring */
425 if (em_dma_malloc(adapter, tsize, &adapter->txdma, BUS_DMA_NOWAIT)) {
426 printf("em%d: Unable to allocate tx_desc memory\n",
431 adapter->tx_desc_base = (struct em_tx_desc *) adapter->txdma.dma_vaddr;
433 rsize = EM_ROUNDUP(adapter->num_rx_desc *
434 sizeof(struct em_rx_desc), 4096);
436 /* Allocate Receive Descriptor ring */
437 if (em_dma_malloc(adapter, rsize, &adapter->rxdma, BUS_DMA_NOWAIT)) {
438 printf("em%d: Unable to allocate rx_desc memory\n",
443 adapter->rx_desc_base = (struct em_rx_desc *) adapter->rxdma.dma_vaddr;
445 /* Initialize the hardware */
446 if (em_hardware_init(adapter)) {
447 printf("em%d: Unable to initialize the hardware\n",
453 /* Copy the permanent MAC address out of the EEPROM */
454 if (em_read_mac_addr(&adapter->hw) < 0) {
455 printf("em%d: EEPROM read error while reading mac address\n",
461 if (!em_is_valid_ether_addr(adapter->hw.mac_addr)) {
462 printf("em%d: Invalid mac address\n", adapter->unit);
467 /* Setup OS specific network interface */
468 em_setup_interface(dev, adapter);
470 /* Initialize statistics */
471 em_clear_hw_cntrs(&adapter->hw);
472 em_update_stats_counters(adapter);
473 adapter->hw.get_link_status = 1;
474 em_check_for_link(&adapter->hw);
476 /* Print the link status */
477 if (adapter->link_active == 1) {
478 em_get_speed_and_duplex(&adapter->hw, &adapter->link_speed,
479 &adapter->link_duplex);
480 printf("em%d: Speed:%d Mbps Duplex:%s\n",
483 adapter->link_duplex == FULL_DUPLEX ? "Full" : "Half");
485 printf("em%d: Speed:N/A Duplex:N/A\n", adapter->unit);
487 /* Identify 82544 on PCIX */
488 em_get_bus_info(&adapter->hw);
489 if(adapter->hw.bus_type == em_bus_type_pcix &&
490 adapter->hw.mac_type == em_82544) {
491 adapter->pcix_82544 = TRUE;
494 adapter->pcix_82544 = FALSE;
496 INIT_DEBUGOUT("em_attach: end");
501 em_dma_free(adapter, &adapter->rxdma);
503 em_dma_free(adapter, &adapter->txdma);
506 em_free_pci_resources(adapter);
511 /*********************************************************************
512 * Device removal routine
514 * The detach entry point is called when the driver is being removed.
515 * This routine stops the adapter and deallocates all the resources
516 * that were allocated for driver operation.
518 * return 0 on success, positive on failure
519 *********************************************************************/
522 em_detach(device_t dev)
524 struct adapter * adapter = device_get_softc(dev);
525 struct ifnet *ifp = adapter->ifp;
527 INIT_DEBUGOUT("em_detach: begin");
530 adapter->in_detach = 1;
532 em_phy_hw_reset(&adapter->hw);
534 #if __FreeBSD_version < 500000
535 ether_ifdetach(adapter->ifp, ETHER_BPF_SUPPORTED);
537 ether_ifdetach(adapter->ifp);
540 em_free_pci_resources(adapter);
541 bus_generic_detach(dev);
543 /* Free Transmit Descriptor ring */
544 if (adapter->tx_desc_base) {
545 em_dma_free(adapter, &adapter->txdma);
546 adapter->tx_desc_base = NULL;
549 /* Free Receive Descriptor ring */
550 if (adapter->rx_desc_base) {
551 em_dma_free(adapter, &adapter->rxdma);
552 adapter->rx_desc_base = NULL;
555 /* Remove from the adapter list */
556 if (em_adapter_list == adapter)
557 em_adapter_list = adapter->next;
558 if (adapter->next != NULL)
559 adapter->next->prev = adapter->prev;
560 if (adapter->prev != NULL)
561 adapter->prev->next = adapter->next;
563 EM_LOCK_DESTROY(adapter);
568 /*********************************************************************
570 * Shutdown entry point
572 **********************************************************************/
575 em_shutdown(device_t dev)
577 struct adapter *adapter = device_get_softc(dev);
585 /*********************************************************************
586 * Transmit entry point
588 * em_start is called by the stack to initiate a transmit.
589 * The driver will remain in this routine as long as there are
590 * packets to transmit and transmit resources are available.
591 * In case resources are not available stack is notified and
592 * the packet is requeued.
593 **********************************************************************/
596 em_start_locked(struct ifnet *ifp)
599 struct adapter *adapter = ifp->if_softc;
601 mtx_assert(&adapter->mtx, MA_OWNED);
603 if (!adapter->link_active)
606 while (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) {
608 IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
610 if (m_head == NULL) break;
613 * em_encap() can modify our pointer, and or make it NULL on
614 * failure. In that event, we can't requeue.
616 if (em_encap(adapter, &m_head)) {
619 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
620 IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
624 /* Send a copy of the frame to the BPF listener */
625 #if __FreeBSD_version < 500000
627 bpf_mtap(ifp, m_head);
629 BPF_MTAP(ifp, m_head);
632 /* Set timeout in case hardware has problems transmitting */
633 ifp->if_timer = EM_TX_TIMEOUT;
640 em_start(struct ifnet *ifp)
642 struct adapter *adapter = ifp->if_softc;
645 em_start_locked(ifp);
650 /*********************************************************************
653 * em_ioctl is called when the user wants to configure the
656 * return 0 on success, positive on failure
657 **********************************************************************/
660 em_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
662 int mask, reinit, error = 0;
663 struct ifreq *ifr = (struct ifreq *) data;
664 struct adapter * adapter = ifp->if_softc;
666 if (adapter->in_detach) return(error);
671 IOCTL_DEBUGOUT("ioctl rcv'd: SIOCxIFADDR (Get/Set Interface Addr)");
672 ether_ioctl(ifp, command, data);
675 IOCTL_DEBUGOUT("ioctl rcv'd: SIOCSIFMTU (Set Interface MTU)");
676 if (ifr->ifr_mtu > MAX_JUMBO_FRAME_SIZE - ETHER_HDR_LEN || \
677 /* 82573 does not support jumbo frames */
678 (adapter->hw.mac_type == em_82573 && ifr->ifr_mtu > ETHERMTU) ) {
682 ifp->if_mtu = ifr->ifr_mtu;
683 adapter->hw.max_frame_size =
684 ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN;
685 em_init_locked(adapter);
690 IOCTL_DEBUGOUT("ioctl rcv'd: SIOCSIFFLAGS (Set Interface Flags)");
692 if (ifp->if_flags & IFF_UP) {
693 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
694 em_init_locked(adapter);
697 em_disable_promisc(adapter);
698 em_set_promisc(adapter);
700 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
708 IOCTL_DEBUGOUT("ioctl rcv'd: SIOC(ADD|DEL)MULTI");
709 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
711 em_disable_intr(adapter);
712 em_set_multi(adapter);
713 if (adapter->hw.mac_type == em_82542_rev2_0) {
714 em_initialize_receive_unit(adapter);
716 #ifdef DEVICE_POLLING
717 if (!(ifp->if_flags & IFF_POLLING))
719 em_enable_intr(adapter);
725 IOCTL_DEBUGOUT("ioctl rcv'd: SIOCxIFMEDIA (Get/Set Interface Media)");
726 error = ifmedia_ioctl(ifp, ifr, &adapter->media, command);
729 IOCTL_DEBUGOUT("ioctl rcv'd: SIOCSIFCAP (Set Capabilities)");
731 mask = ifr->ifr_reqcap ^ ifp->if_capenable;
732 if (mask & IFCAP_POLLING)
733 ifp->if_capenable ^= IFCAP_POLLING;
734 if (mask & IFCAP_HWCSUM) {
735 ifp->if_capenable ^= IFCAP_HWCSUM;
738 if (mask & IFCAP_VLAN_HWTAGGING) {
739 ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
742 if (reinit && (ifp->if_drv_flags & IFF_DRV_RUNNING))
746 IOCTL_DEBUGOUT1("ioctl received: UNKNOWN (0x%x)", (int)command);
753 /*********************************************************************
754 * Watchdog entry point
756 * This routine is called whenever hardware quits transmitting.
758 **********************************************************************/
761 em_watchdog(struct ifnet *ifp)
763 struct adapter * adapter;
764 adapter = ifp->if_softc;
766 /* If we are in this routine because of pause frames, then
767 * don't reset the hardware.
769 if (E1000_READ_REG(&adapter->hw, STATUS) & E1000_STATUS_TXOFF) {
770 ifp->if_timer = EM_TX_TIMEOUT;
774 if (em_check_for_link(&adapter->hw))
775 printf("em%d: watchdog timeout -- resetting\n", adapter->unit);
777 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
785 /*********************************************************************
788 * This routine is used in two ways. It is used by the stack as
789 * init entry point in network interface structure. It is also used
790 * by the driver as a hw/sw initialization routine to get to a
793 * return 0 on success, positive on failure
794 **********************************************************************/
797 em_init_locked(struct adapter * adapter)
804 INIT_DEBUGOUT("em_init: begin");
806 mtx_assert(&adapter->mtx, MA_OWNED);
810 /* Packet Buffer Allocation (PBA)
811 * Writing PBA sets the receive portion of the buffer
812 * the remainder is used for the transmit buffer.
814 * Devices before the 82547 had a Packet Buffer of 64K.
815 * Default allocation: PBA=48K for Rx, leaving 16K for Tx.
816 * After the 82547 the buffer was reduced to 40K.
817 * Default allocation: PBA=30K for Rx, leaving 10K for Tx.
818 * Note: default does not leave enough room for Jumbo Frame >10k.
820 if(adapter->hw.mac_type < em_82547) {
821 /* Total FIFO is 64K */
822 if(adapter->rx_buffer_len > EM_RXBUFFER_8192)
823 pba = E1000_PBA_40K; /* 40K for Rx, 24K for Tx */
825 pba = E1000_PBA_48K; /* 48K for Rx, 16K for Tx */
827 /* Total FIFO is 40K */
828 if(adapter->hw.max_frame_size > EM_RXBUFFER_8192) {
829 pba = E1000_PBA_22K; /* 22K for Rx, 18K for Tx */
831 pba = E1000_PBA_30K; /* 30K for Rx, 10K for Tx */
833 adapter->tx_fifo_head = 0;
834 adapter->tx_head_addr = pba << EM_TX_HEAD_ADDR_SHIFT;
835 adapter->tx_fifo_size = (E1000_PBA_40K - pba) << EM_PBA_BYTES_SHIFT;
837 INIT_DEBUGOUT1("em_init: pba=%dK",pba);
838 E1000_WRITE_REG(&adapter->hw, PBA, pba);
840 /* Get the latest mac address, User can use a LAA */
841 bcopy(IFP2ENADDR(adapter->ifp), adapter->hw.mac_addr,
844 /* Initialize the hardware */
845 if (em_hardware_init(adapter)) {
846 printf("em%d: Unable to initialize the hardware\n",
851 if (ifp->if_capenable & IFCAP_VLAN_HWTAGGING)
852 em_enable_vlans(adapter);
854 /* Prepare transmit descriptors and buffers */
855 if (em_setup_transmit_structures(adapter)) {
856 printf("em%d: Could not setup transmit structures\n",
861 em_initialize_transmit_unit(adapter);
863 /* Setup Multicast table */
864 em_set_multi(adapter);
866 /* Prepare receive descriptors and buffers */
867 if (em_setup_receive_structures(adapter)) {
868 printf("em%d: Could not setup receive structures\n",
873 em_initialize_receive_unit(adapter);
875 /* Don't loose promiscuous settings */
876 em_set_promisc(adapter);
878 ifp->if_drv_flags |= IFF_DRV_RUNNING;
879 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
881 if (adapter->hw.mac_type >= em_82543) {
882 if (ifp->if_capenable & IFCAP_TXCSUM)
883 ifp->if_hwassist = EM_CHECKSUM_FEATURES;
885 ifp->if_hwassist = 0;
888 callout_reset(&adapter->timer, hz, em_local_timer, adapter);
889 em_clear_hw_cntrs(&adapter->hw);
890 #ifdef DEVICE_POLLING
892 * Only enable interrupts if we are not polling, make sure
893 * they are off otherwise.
895 if (ifp->if_flags & IFF_POLLING)
896 em_disable_intr(adapter);
898 #endif /* DEVICE_POLLING */
899 em_enable_intr(adapter);
901 /* Don't reset the phy next time init gets called */
902 adapter->hw.phy_reset_disable = TRUE;
910 struct adapter * adapter = arg;
913 em_init_locked(adapter);
919 #ifdef DEVICE_POLLING
920 static poll_handler_t em_poll;
923 em_poll_locked(struct ifnet *ifp, enum poll_cmd cmd, int count)
925 struct adapter *adapter = ifp->if_softc;
928 mtx_assert(&adapter->mtx, MA_OWNED);
930 if (!(ifp->if_capenable & IFCAP_POLLING)) {
931 ether_poll_deregister(ifp);
932 cmd = POLL_DEREGISTER;
934 if (cmd == POLL_DEREGISTER) { /* final call, enable interrupts */
935 em_enable_intr(adapter);
938 if (cmd == POLL_AND_CHECK_STATUS) {
939 reg_icr = E1000_READ_REG(&adapter->hw, ICR);
940 if (reg_icr & (E1000_ICR_RXSEQ | E1000_ICR_LSC)) {
941 callout_stop(&adapter->timer);
942 adapter->hw.get_link_status = 1;
943 em_check_for_link(&adapter->hw);
944 em_print_link_status(adapter);
945 callout_reset(&adapter->timer, hz, em_local_timer, adapter);
948 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
949 em_process_receive_interrupts(adapter, count);
950 em_clean_transmit_interrupts(adapter);
953 if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
954 !IFQ_DRV_IS_EMPTY(&ifp->if_snd))
955 em_start_locked(ifp);
959 em_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
961 struct adapter *adapter = ifp->if_softc;
964 em_poll_locked(ifp, cmd, count);
967 #endif /* DEVICE_POLLING */
969 /*********************************************************************
971 * Interrupt Service routine
973 **********************************************************************/
977 u_int32_t loop_cnt = EM_MAX_INTR;
980 struct adapter *adapter = arg;
986 #ifdef DEVICE_POLLING
987 if (ifp->if_flags & IFF_POLLING) {
992 if ((ifp->if_capenable & IFCAP_POLLING) &&
993 ether_poll_register(em_poll, ifp)) {
994 em_disable_intr(adapter);
995 em_poll_locked(ifp, 0, 1);
999 #endif /* DEVICE_POLLING */
1001 reg_icr = E1000_READ_REG(&adapter->hw, ICR);
1007 /* Link status change */
1008 if (reg_icr & (E1000_ICR_RXSEQ | E1000_ICR_LSC)) {
1009 callout_stop(&adapter->timer);
1010 adapter->hw.get_link_status = 1;
1011 em_check_for_link(&adapter->hw);
1012 em_print_link_status(adapter);
1013 callout_reset(&adapter->timer, hz, em_local_timer, adapter);
1016 while (loop_cnt > 0) {
1017 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1018 em_process_receive_interrupts(adapter, -1);
1019 em_clean_transmit_interrupts(adapter);
1024 if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
1025 !IFQ_DRV_IS_EMPTY(&ifp->if_snd))
1026 em_start_locked(ifp);
1034 /*********************************************************************
1036 * Media Ioctl callback
1038 * This routine is called whenever the user queries the status of
1039 * the interface using ifconfig.
1041 **********************************************************************/
1043 em_media_status(struct ifnet *ifp, struct ifmediareq *ifmr)
1045 struct adapter * adapter = ifp->if_softc;
1047 INIT_DEBUGOUT("em_media_status: begin");
1049 em_check_for_link(&adapter->hw);
1050 if (E1000_READ_REG(&adapter->hw, STATUS) & E1000_STATUS_LU) {
1051 if (adapter->link_active == 0) {
1052 em_get_speed_and_duplex(&adapter->hw,
1053 &adapter->link_speed,
1054 &adapter->link_duplex);
1055 adapter->link_active = 1;
1058 if (adapter->link_active == 1) {
1059 adapter->link_speed = 0;
1060 adapter->link_duplex = 0;
1061 adapter->link_active = 0;
1065 ifmr->ifm_status = IFM_AVALID;
1066 ifmr->ifm_active = IFM_ETHER;
1068 if (!adapter->link_active)
1071 ifmr->ifm_status |= IFM_ACTIVE;
1073 if (adapter->hw.media_type == em_media_type_fiber) {
1074 ifmr->ifm_active |= IFM_1000_SX | IFM_FDX;
1076 switch (adapter->link_speed) {
1078 ifmr->ifm_active |= IFM_10_T;
1081 ifmr->ifm_active |= IFM_100_TX;
1084 #if __FreeBSD_version < 500000
1085 ifmr->ifm_active |= IFM_1000_TX;
1087 ifmr->ifm_active |= IFM_1000_T;
1091 if (adapter->link_duplex == FULL_DUPLEX)
1092 ifmr->ifm_active |= IFM_FDX;
1094 ifmr->ifm_active |= IFM_HDX;
1099 /*********************************************************************
1101 * Media Ioctl callback
1103 * This routine is called when the user changes speed/duplex using
1104 * media/mediopt option with ifconfig.
1106 **********************************************************************/
1108 em_media_change(struct ifnet *ifp)
1110 struct adapter * adapter = ifp->if_softc;
1111 struct ifmedia *ifm = &adapter->media;
1113 INIT_DEBUGOUT("em_media_change: begin");
1115 if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
1118 switch (IFM_SUBTYPE(ifm->ifm_media)) {
1120 adapter->hw.autoneg = DO_AUTO_NEG;
1121 adapter->hw.autoneg_advertised = AUTONEG_ADV_DEFAULT;
1124 #if __FreeBSD_version < 500000
1129 adapter->hw.autoneg = DO_AUTO_NEG;
1130 adapter->hw.autoneg_advertised = ADVERTISE_1000_FULL;
1133 adapter->hw.autoneg = FALSE;
1134 adapter->hw.autoneg_advertised = 0;
1135 if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX)
1136 adapter->hw.forced_speed_duplex = em_100_full;
1138 adapter->hw.forced_speed_duplex = em_100_half;
1141 adapter->hw.autoneg = FALSE;
1142 adapter->hw.autoneg_advertised = 0;
1143 if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX)
1144 adapter->hw.forced_speed_duplex = em_10_full;
1146 adapter->hw.forced_speed_duplex = em_10_half;
1149 printf("em%d: Unsupported media type\n", adapter->unit);
1152 /* As the speed/duplex settings my have changed we need to
1155 adapter->hw.phy_reset_disable = FALSE;
1162 /*********************************************************************
1164 * This routine maps the mbufs to tx descriptors.
1166 * return 0 on success, positive on failure
1167 **********************************************************************/
1169 em_encap(struct adapter *adapter, struct mbuf **m_headp)
1171 u_int32_t txd_upper;
1172 u_int32_t txd_lower, txd_used = 0, txd_saved = 0;
1176 struct mbuf *m_head;
1178 /* For 82544 Workaround */
1179 DESC_ARRAY desc_array;
1180 u_int32_t array_elements;
1183 #if __FreeBSD_version < 500000
1184 struct ifvlan *ifv = NULL;
1188 bus_dma_segment_t segs[EM_MAX_SCATTER];
1191 struct em_buffer *tx_buffer = NULL;
1192 struct em_tx_desc *current_tx_desc = NULL;
1193 struct ifnet *ifp = adapter->ifp;
1198 * Force a cleanup if number of TX descriptors
1199 * available hits the threshold
1201 if (adapter->num_tx_desc_avail <= EM_TX_CLEANUP_THRESHOLD) {
1202 em_clean_transmit_interrupts(adapter);
1203 if (adapter->num_tx_desc_avail <= EM_TX_CLEANUP_THRESHOLD) {
1204 adapter->no_tx_desc_avail1++;
1210 * Map the packet for DMA.
1212 if (bus_dmamap_create(adapter->txtag, BUS_DMA_NOWAIT, &map)) {
1213 adapter->no_tx_map_avail++;
1216 error = bus_dmamap_load_mbuf_sg(adapter->txtag, map, m_head, segs,
1217 &nsegs, BUS_DMA_NOWAIT);
1219 adapter->no_tx_dma_setup++;
1220 bus_dmamap_destroy(adapter->txtag, map);
1223 KASSERT(nsegs != 0, ("em_encap: empty packet"));
1225 if (nsegs > adapter->num_tx_desc_avail) {
1226 adapter->no_tx_desc_avail2++;
1227 bus_dmamap_destroy(adapter->txtag, map);
1232 if (ifp->if_hwassist > 0) {
1233 em_transmit_checksum_setup(adapter, m_head,
1234 &txd_upper, &txd_lower);
1236 txd_upper = txd_lower = 0;
1239 /* Find out if we are in vlan mode */
1240 #if __FreeBSD_version < 500000
1241 if ((m_head->m_flags & (M_PROTO1|M_PKTHDR)) == (M_PROTO1|M_PKTHDR) &&
1242 m_head->m_pkthdr.rcvif != NULL &&
1243 m_head->m_pkthdr.rcvif->if_type == IFT_L2VLAN)
1244 ifv = m_head->m_pkthdr.rcvif->if_softc;
1246 mtag = VLAN_OUTPUT_TAG(ifp, m_head);
1250 * When operating in promiscuous mode, hardware encapsulation for
1251 * packets is disabled. This means we have to add the vlan
1252 * encapsulation in the driver, since it will have come down from the
1253 * VLAN layer with a tag instead of a VLAN header.
1255 if (mtag != NULL && adapter->em_insert_vlan_header) {
1256 struct ether_vlan_header *evl;
1257 struct ether_header eh;
1259 m_head = m_pullup(m_head, sizeof(eh));
1260 if (m_head == NULL) {
1262 bus_dmamap_destroy(adapter->txtag, map);
1265 eh = *mtod(m_head, struct ether_header *);
1266 M_PREPEND(m_head, sizeof(*evl), M_DONTWAIT);
1267 if (m_head == NULL) {
1269 bus_dmamap_destroy(adapter->txtag, map);
1272 m_head = m_pullup(m_head, sizeof(*evl));
1273 if (m_head == NULL) {
1275 bus_dmamap_destroy(adapter->txtag, map);
1278 evl = mtod(m_head, struct ether_vlan_header *);
1279 bcopy(&eh, evl, sizeof(*evl));
1280 evl->evl_proto = evl->evl_encap_proto;
1281 evl->evl_encap_proto = htons(ETHERTYPE_VLAN);
1282 evl->evl_tag = htons(VLAN_TAG_VALUE(mtag));
1283 m_tag_delete(m_head, mtag);
1288 i = adapter->next_avail_tx_desc;
1289 if (adapter->pcix_82544) {
1293 for (j = 0; j < nsegs; j++) {
1294 /* If adapter is 82544 and on PCIX bus */
1295 if(adapter->pcix_82544) {
1297 address = htole64(segs[j].ds_addr);
1299 * Check the Address and Length combination and
1300 * split the data accordingly
1302 array_elements = em_fill_descriptors(address,
1303 htole32(segs[j].ds_len),
1305 for (counter = 0; counter < array_elements; counter++) {
1306 if (txd_used == adapter->num_tx_desc_avail) {
1307 adapter->next_avail_tx_desc = txd_saved;
1308 adapter->no_tx_desc_avail2++;
1309 bus_dmamap_destroy(adapter->txtag, map);
1312 tx_buffer = &adapter->tx_buffer_area[i];
1313 current_tx_desc = &adapter->tx_desc_base[i];
1314 current_tx_desc->buffer_addr = htole64(
1315 desc_array.descriptor[counter].address);
1316 current_tx_desc->lower.data = htole32(
1317 (adapter->txd_cmd | txd_lower |
1318 (u_int16_t)desc_array.descriptor[counter].length));
1319 current_tx_desc->upper.data = htole32((txd_upper));
1320 if (++i == adapter->num_tx_desc)
1323 tx_buffer->m_head = NULL;
1327 tx_buffer = &adapter->tx_buffer_area[i];
1328 current_tx_desc = &adapter->tx_desc_base[i];
1330 current_tx_desc->buffer_addr = htole64(segs[j].ds_addr);
1331 current_tx_desc->lower.data = htole32(
1332 adapter->txd_cmd | txd_lower | segs[j].ds_len);
1333 current_tx_desc->upper.data = htole32(txd_upper);
1335 if (++i == adapter->num_tx_desc)
1338 tx_buffer->m_head = NULL;
1342 adapter->next_avail_tx_desc = i;
1343 if (adapter->pcix_82544) {
1344 adapter->num_tx_desc_avail -= txd_used;
1347 adapter->num_tx_desc_avail -= nsegs;
1350 #if __FreeBSD_version < 500000
1352 /* Set the vlan id */
1353 current_tx_desc->upper.fields.special = htole16(ifv->ifv_tag);
1356 /* Set the vlan id */
1357 current_tx_desc->upper.fields.special = htole16(VLAN_TAG_VALUE(mtag));
1360 /* Tell hardware to add tag */
1361 current_tx_desc->lower.data |= htole32(E1000_TXD_CMD_VLE);
1364 tx_buffer->m_head = m_head;
1365 tx_buffer->map = map;
1366 bus_dmamap_sync(adapter->txtag, map, BUS_DMASYNC_PREWRITE);
1369 * Last Descriptor of Packet needs End Of Packet (EOP)
1371 current_tx_desc->lower.data |= htole32(E1000_TXD_CMD_EOP);
1374 * Advance the Transmit Descriptor Tail (Tdt), this tells the E1000
1375 * that this frame is available to transmit.
1377 bus_dmamap_sync(adapter->txdma.dma_tag, adapter->txdma.dma_map,
1378 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1379 if (adapter->hw.mac_type == em_82547 &&
1380 adapter->link_duplex == HALF_DUPLEX) {
1381 em_82547_move_tail_locked(adapter);
1383 E1000_WRITE_REG(&adapter->hw, TDT, i);
1384 if (adapter->hw.mac_type == em_82547) {
1385 em_82547_update_fifo_head(adapter, m_head->m_pkthdr.len);
1392 /*********************************************************************
1394 * 82547 workaround to avoid controller hang in half-duplex environment.
1395 * The workaround is to avoid queuing a large packet that would span
1396 * the internal Tx FIFO ring boundary. We need to reset the FIFO pointers
1397 * in this case. We do that only when FIFO is quiescent.
1399 **********************************************************************/
1401 em_82547_move_tail_locked(struct adapter *adapter)
1405 struct em_tx_desc *tx_desc;
1406 uint16_t length = 0;
1409 EM_LOCK_ASSERT(adapter);
1411 hw_tdt = E1000_READ_REG(&adapter->hw, TDT);
1412 sw_tdt = adapter->next_avail_tx_desc;
1414 while (hw_tdt != sw_tdt) {
1415 tx_desc = &adapter->tx_desc_base[hw_tdt];
1416 length += tx_desc->lower.flags.length;
1417 eop = tx_desc->lower.data & E1000_TXD_CMD_EOP;
1418 if(++hw_tdt == adapter->num_tx_desc)
1422 if (em_82547_fifo_workaround(adapter, length)) {
1423 adapter->tx_fifo_wrk_cnt++;
1424 callout_reset(&adapter->tx_fifo_timer, 1,
1425 em_82547_move_tail, adapter);
1428 E1000_WRITE_REG(&adapter->hw, TDT, hw_tdt);
1429 em_82547_update_fifo_head(adapter, length);
1437 em_82547_move_tail(void *arg)
1439 struct adapter *adapter = arg;
1442 em_82547_move_tail_locked(adapter);
1447 em_82547_fifo_workaround(struct adapter *adapter, int len)
1449 int fifo_space, fifo_pkt_len;
1451 fifo_pkt_len = EM_ROUNDUP(len + EM_FIFO_HDR, EM_FIFO_HDR);
1453 if (adapter->link_duplex == HALF_DUPLEX) {
1454 fifo_space = adapter->tx_fifo_size - adapter->tx_fifo_head;
1456 if (fifo_pkt_len >= (EM_82547_PKT_THRESH + fifo_space)) {
1457 if (em_82547_tx_fifo_reset(adapter)) {
1470 em_82547_update_fifo_head(struct adapter *adapter, int len)
1472 int fifo_pkt_len = EM_ROUNDUP(len + EM_FIFO_HDR, EM_FIFO_HDR);
1474 /* tx_fifo_head is always 16 byte aligned */
1475 adapter->tx_fifo_head += fifo_pkt_len;
1476 if (adapter->tx_fifo_head >= adapter->tx_fifo_size) {
1477 adapter->tx_fifo_head -= adapter->tx_fifo_size;
1485 em_82547_tx_fifo_reset(struct adapter *adapter)
1489 if ( (E1000_READ_REG(&adapter->hw, TDT) ==
1490 E1000_READ_REG(&adapter->hw, TDH)) &&
1491 (E1000_READ_REG(&adapter->hw, TDFT) ==
1492 E1000_READ_REG(&adapter->hw, TDFH)) &&
1493 (E1000_READ_REG(&adapter->hw, TDFTS) ==
1494 E1000_READ_REG(&adapter->hw, TDFHS)) &&
1495 (E1000_READ_REG(&adapter->hw, TDFPC) == 0)) {
1497 /* Disable TX unit */
1498 tctl = E1000_READ_REG(&adapter->hw, TCTL);
1499 E1000_WRITE_REG(&adapter->hw, TCTL, tctl & ~E1000_TCTL_EN);
1501 /* Reset FIFO pointers */
1502 E1000_WRITE_REG(&adapter->hw, TDFT, adapter->tx_head_addr);
1503 E1000_WRITE_REG(&adapter->hw, TDFH, adapter->tx_head_addr);
1504 E1000_WRITE_REG(&adapter->hw, TDFTS, adapter->tx_head_addr);
1505 E1000_WRITE_REG(&adapter->hw, TDFHS, adapter->tx_head_addr);
1507 /* Re-enable TX unit */
1508 E1000_WRITE_REG(&adapter->hw, TCTL, tctl);
1509 E1000_WRITE_FLUSH(&adapter->hw);
1511 adapter->tx_fifo_head = 0;
1512 adapter->tx_fifo_reset_cnt++;
1522 em_set_promisc(struct adapter * adapter)
1526 struct ifnet *ifp = adapter->ifp;
1528 reg_rctl = E1000_READ_REG(&adapter->hw, RCTL);
1530 if (ifp->if_flags & IFF_PROMISC) {
1531 reg_rctl |= (E1000_RCTL_UPE | E1000_RCTL_MPE);
1532 E1000_WRITE_REG(&adapter->hw, RCTL, reg_rctl);
1533 /* Disable VLAN stripping in promiscous mode
1534 * This enables bridging of vlan tagged frames to occur
1535 * and also allows vlan tags to be seen in tcpdump
1537 if (ifp->if_capenable & IFCAP_VLAN_HWTAGGING)
1538 em_disable_vlans(adapter);
1539 adapter->em_insert_vlan_header = 1;
1540 } else if (ifp->if_flags & IFF_ALLMULTI) {
1541 reg_rctl |= E1000_RCTL_MPE;
1542 reg_rctl &= ~E1000_RCTL_UPE;
1543 E1000_WRITE_REG(&adapter->hw, RCTL, reg_rctl);
1544 adapter->em_insert_vlan_header = 0;
1546 adapter->em_insert_vlan_header = 0;
1552 em_disable_promisc(struct adapter * adapter)
1555 struct ifnet *ifp = adapter->ifp;
1557 reg_rctl = E1000_READ_REG(&adapter->hw, RCTL);
1559 reg_rctl &= (~E1000_RCTL_UPE);
1560 reg_rctl &= (~E1000_RCTL_MPE);
1561 E1000_WRITE_REG(&adapter->hw, RCTL, reg_rctl);
1563 if (ifp->if_capenable & IFCAP_VLAN_HWTAGGING)
1564 em_enable_vlans(adapter);
1565 adapter->em_insert_vlan_header = 0;
1571 /*********************************************************************
1574 * This routine is called whenever multicast address list is updated.
1576 **********************************************************************/
1579 em_set_multi(struct adapter * adapter)
1581 u_int32_t reg_rctl = 0;
1582 u_int8_t mta[MAX_NUM_MULTICAST_ADDRESSES * ETH_LENGTH_OF_ADDRESS];
1583 struct ifmultiaddr *ifma;
1585 struct ifnet *ifp = adapter->ifp;
1587 IOCTL_DEBUGOUT("em_set_multi: begin");
1589 if (adapter->hw.mac_type == em_82542_rev2_0) {
1590 reg_rctl = E1000_READ_REG(&adapter->hw, RCTL);
1591 if (adapter->hw.pci_cmd_word & CMD_MEM_WRT_INVALIDATE) {
1592 em_pci_clear_mwi(&adapter->hw);
1594 reg_rctl |= E1000_RCTL_RST;
1595 E1000_WRITE_REG(&adapter->hw, RCTL, reg_rctl);
1600 #if __FreeBSD_version < 500000
1601 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1603 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1605 if (ifma->ifma_addr->sa_family != AF_LINK)
1608 if (mcnt == MAX_NUM_MULTICAST_ADDRESSES) break;
1610 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
1611 &mta[mcnt*ETH_LENGTH_OF_ADDRESS], ETH_LENGTH_OF_ADDRESS);
1614 IF_ADDR_UNLOCK(ifp);
1616 if (mcnt >= MAX_NUM_MULTICAST_ADDRESSES) {
1617 reg_rctl = E1000_READ_REG(&adapter->hw, RCTL);
1618 reg_rctl |= E1000_RCTL_MPE;
1619 E1000_WRITE_REG(&adapter->hw, RCTL, reg_rctl);
1621 em_mc_addr_list_update(&adapter->hw, mta, mcnt, 0, 1);
1623 if (adapter->hw.mac_type == em_82542_rev2_0) {
1624 reg_rctl = E1000_READ_REG(&adapter->hw, RCTL);
1625 reg_rctl &= ~E1000_RCTL_RST;
1626 E1000_WRITE_REG(&adapter->hw, RCTL, reg_rctl);
1628 if (adapter->hw.pci_cmd_word & CMD_MEM_WRT_INVALIDATE) {
1629 em_pci_set_mwi(&adapter->hw);
1637 /*********************************************************************
1640 * This routine checks for link status and updates statistics.
1642 **********************************************************************/
1645 em_local_timer(void *arg)
1648 struct adapter * adapter = arg;
1653 em_check_for_link(&adapter->hw);
1654 em_print_link_status(adapter);
1655 em_update_stats_counters(adapter);
1656 if (em_display_debug_stats && ifp->if_drv_flags & IFF_DRV_RUNNING) {
1657 em_print_hw_stats(adapter);
1659 em_smartspeed(adapter);
1661 callout_reset(&adapter->timer, hz, em_local_timer, adapter);
1668 em_print_link_status(struct adapter * adapter)
1670 struct ifnet *ifp = adapter->ifp;
1672 if (E1000_READ_REG(&adapter->hw, STATUS) & E1000_STATUS_LU) {
1673 if (adapter->link_active == 0) {
1674 em_get_speed_and_duplex(&adapter->hw,
1675 &adapter->link_speed,
1676 &adapter->link_duplex);
1678 printf("em%d: Link is up %d Mbps %s\n",
1680 adapter->link_speed,
1681 ((adapter->link_duplex == FULL_DUPLEX) ?
1682 "Full Duplex" : "Half Duplex"));
1683 adapter->link_active = 1;
1684 adapter->smartspeed = 0;
1685 if_link_state_change(ifp, LINK_STATE_UP);
1688 if (adapter->link_active == 1) {
1689 adapter->link_speed = 0;
1690 adapter->link_duplex = 0;
1692 printf("em%d: Link is Down\n", adapter->unit);
1693 adapter->link_active = 0;
1694 if_link_state_change(ifp, LINK_STATE_DOWN);
1701 /*********************************************************************
1703 * This routine disables all traffic on the adapter by issuing a
1704 * global reset on the MAC and deallocates TX/RX buffers.
1706 **********************************************************************/
1712 struct adapter * adapter = arg;
1715 mtx_assert(&adapter->mtx, MA_OWNED);
1717 INIT_DEBUGOUT("em_stop: begin");
1718 #ifdef DEVICE_POLLING
1719 ether_poll_deregister(ifp);
1721 em_disable_intr(adapter);
1722 em_reset_hw(&adapter->hw);
1723 callout_stop(&adapter->timer);
1724 callout_stop(&adapter->tx_fifo_timer);
1725 em_free_transmit_structures(adapter);
1726 em_free_receive_structures(adapter);
1729 /* Tell the stack that the interface is no longer active */
1730 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
1736 /*********************************************************************
1738 * Determine hardware revision.
1740 **********************************************************************/
1742 em_identify_hardware(struct adapter * adapter)
1744 device_t dev = adapter->dev;
1746 /* Make sure our PCI config space has the necessary stuff set */
1747 adapter->hw.pci_cmd_word = pci_read_config(dev, PCIR_COMMAND, 2);
1748 if (!((adapter->hw.pci_cmd_word & PCIM_CMD_BUSMASTEREN) &&
1749 (adapter->hw.pci_cmd_word & PCIM_CMD_MEMEN))) {
1750 printf("em%d: Memory Access and/or Bus Master bits were not set!\n",
1752 adapter->hw.pci_cmd_word |=
1753 (PCIM_CMD_BUSMASTEREN | PCIM_CMD_MEMEN);
1754 pci_write_config(dev, PCIR_COMMAND, adapter->hw.pci_cmd_word, 2);
1757 /* Save off the information about this board */
1758 adapter->hw.vendor_id = pci_get_vendor(dev);
1759 adapter->hw.device_id = pci_get_device(dev);
1760 adapter->hw.revision_id = pci_read_config(dev, PCIR_REVID, 1);
1761 adapter->hw.subsystem_vendor_id = pci_read_config(dev, PCIR_SUBVEND_0, 2);
1762 adapter->hw.subsystem_id = pci_read_config(dev, PCIR_SUBDEV_0, 2);
1764 /* Identify the MAC */
1765 if (em_set_mac_type(&adapter->hw))
1766 printf("em%d: Unknown MAC Type\n", adapter->unit);
1768 if(adapter->hw.mac_type == em_82541 ||
1769 adapter->hw.mac_type == em_82541_rev_2 ||
1770 adapter->hw.mac_type == em_82547 ||
1771 adapter->hw.mac_type == em_82547_rev_2)
1772 adapter->hw.phy_init_script = TRUE;
1778 em_allocate_pci_resources(struct adapter * adapter)
1781 device_t dev = adapter->dev;
1784 adapter->res_memory = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
1786 if (!(adapter->res_memory)) {
1787 printf("em%d: Unable to allocate bus resource: memory\n",
1791 adapter->osdep.mem_bus_space_tag =
1792 rman_get_bustag(adapter->res_memory);
1793 adapter->osdep.mem_bus_space_handle =
1794 rman_get_bushandle(adapter->res_memory);
1795 adapter->hw.hw_addr = (uint8_t *)&adapter->osdep.mem_bus_space_handle;
1798 if (adapter->hw.mac_type > em_82543) {
1799 /* Figure our where our IO BAR is ? */
1801 for (i = 0; i < 5; i++) {
1802 val = pci_read_config(dev, rid, 4);
1803 if (val & 0x00000001) {
1804 adapter->io_rid = rid;
1810 adapter->res_ioport = bus_alloc_resource_any(dev,
1814 if (!(adapter->res_ioport)) {
1815 printf("em%d: Unable to allocate bus resource: ioport\n",
1820 adapter->hw.io_base =
1821 rman_get_start(adapter->res_ioport);
1825 adapter->res_interrupt = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
1828 if (!(adapter->res_interrupt)) {
1829 printf("em%d: Unable to allocate bus resource: interrupt\n",
1833 if (bus_setup_intr(dev, adapter->res_interrupt,
1834 INTR_TYPE_NET | INTR_MPSAFE,
1835 (void (*)(void *)) em_intr, adapter,
1836 &adapter->int_handler_tag)) {
1837 printf("em%d: Error registering interrupt handler!\n",
1842 adapter->hw.back = &adapter->osdep;
1848 em_free_pci_resources(struct adapter * adapter)
1850 device_t dev = adapter->dev;
1852 if (adapter->res_interrupt != NULL) {
1853 bus_teardown_intr(dev, adapter->res_interrupt,
1854 adapter->int_handler_tag);
1855 bus_release_resource(dev, SYS_RES_IRQ, 0,
1856 adapter->res_interrupt);
1858 if (adapter->res_memory != NULL) {
1859 bus_release_resource(dev, SYS_RES_MEMORY, EM_MMBA,
1860 adapter->res_memory);
1863 if (adapter->res_ioport != NULL) {
1864 bus_release_resource(dev, SYS_RES_IOPORT, adapter->io_rid,
1865 adapter->res_ioport);
1870 /*********************************************************************
1872 * Initialize the hardware to a configuration as specified by the
1873 * adapter structure. The controller is reset, the EEPROM is
1874 * verified, the MAC address is set, then the shared initialization
1875 * routines are called.
1877 **********************************************************************/
1879 em_hardware_init(struct adapter * adapter)
1881 INIT_DEBUGOUT("em_hardware_init: begin");
1882 /* Issue a global reset */
1883 em_reset_hw(&adapter->hw);
1885 /* When hardware is reset, fifo_head is also reset */
1886 adapter->tx_fifo_head = 0;
1888 /* Make sure we have a good EEPROM before we read from it */
1889 if (em_validate_eeprom_checksum(&adapter->hw) < 0) {
1890 printf("em%d: The EEPROM Checksum Is Not Valid\n",
1895 if (em_read_part_num(&adapter->hw, &(adapter->part_num)) < 0) {
1896 printf("em%d: EEPROM read error while reading part number\n",
1901 if (em_init_hw(&adapter->hw) < 0) {
1902 printf("em%d: Hardware Initialization Failed",
1907 em_check_for_link(&adapter->hw);
1908 if (E1000_READ_REG(&adapter->hw, STATUS) & E1000_STATUS_LU)
1909 adapter->link_active = 1;
1911 adapter->link_active = 0;
1913 if (adapter->link_active) {
1914 em_get_speed_and_duplex(&adapter->hw,
1915 &adapter->link_speed,
1916 &adapter->link_duplex);
1918 adapter->link_speed = 0;
1919 adapter->link_duplex = 0;
1925 /*********************************************************************
1927 * Setup networking device structure and register an interface.
1929 **********************************************************************/
1931 em_setup_interface(device_t dev, struct adapter * adapter)
1934 INIT_DEBUGOUT("em_setup_interface: begin");
1936 ifp = adapter->ifp = if_alloc(IFT_ETHER);
1938 panic("%s: can not if_alloc()", device_get_nameunit(dev));
1939 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
1940 ifp->if_mtu = ETHERMTU;
1941 ifp->if_baudrate = 1000000000;
1942 ifp->if_init = em_init;
1943 ifp->if_softc = adapter;
1944 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1945 ifp->if_ioctl = em_ioctl;
1946 ifp->if_start = em_start;
1947 ifp->if_watchdog = em_watchdog;
1948 IFQ_SET_MAXLEN(&ifp->if_snd, adapter->num_tx_desc - 1);
1949 ifp->if_snd.ifq_drv_maxlen = adapter->num_tx_desc - 1;
1950 IFQ_SET_READY(&ifp->if_snd);
1952 #if __FreeBSD_version < 500000
1953 ether_ifattach(ifp, ETHER_BPF_SUPPORTED);
1955 ether_ifattach(ifp, adapter->hw.mac_addr);
1958 ifp->if_capabilities = ifp->if_capenable = 0;
1960 if (adapter->hw.mac_type >= em_82543) {
1961 ifp->if_capabilities |= IFCAP_HWCSUM;
1962 ifp->if_capenable |= IFCAP_HWCSUM;
1966 * Tell the upper layer(s) we support long frames.
1968 ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
1969 #if __FreeBSD_version >= 500000
1970 ifp->if_capabilities |= IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_MTU;
1971 ifp->if_capenable |= IFCAP_VLAN_MTU;
1974 #ifdef DEVICE_POLLING
1975 ifp->if_capabilities |= IFCAP_POLLING;
1976 ifp->if_capenable |= IFCAP_POLLING;
1980 * Specify the media types supported by this adapter and register
1981 * callbacks to update media and link information
1983 ifmedia_init(&adapter->media, IFM_IMASK, em_media_change,
1985 if (adapter->hw.media_type == em_media_type_fiber) {
1986 ifmedia_add(&adapter->media, IFM_ETHER | IFM_1000_SX | IFM_FDX,
1988 ifmedia_add(&adapter->media, IFM_ETHER | IFM_1000_SX,
1991 ifmedia_add(&adapter->media, IFM_ETHER | IFM_10_T, 0, NULL);
1992 ifmedia_add(&adapter->media, IFM_ETHER | IFM_10_T | IFM_FDX,
1994 ifmedia_add(&adapter->media, IFM_ETHER | IFM_100_TX,
1996 ifmedia_add(&adapter->media, IFM_ETHER | IFM_100_TX | IFM_FDX,
1998 #if __FreeBSD_version < 500000
1999 ifmedia_add(&adapter->media, IFM_ETHER | IFM_1000_TX | IFM_FDX,
2001 ifmedia_add(&adapter->media, IFM_ETHER | IFM_1000_TX, 0, NULL);
2003 ifmedia_add(&adapter->media, IFM_ETHER | IFM_1000_T | IFM_FDX,
2005 ifmedia_add(&adapter->media, IFM_ETHER | IFM_1000_T, 0, NULL);
2008 ifmedia_add(&adapter->media, IFM_ETHER | IFM_AUTO, 0, NULL);
2009 ifmedia_set(&adapter->media, IFM_ETHER | IFM_AUTO);
2015 /*********************************************************************
2017 * Workaround for SmartSpeed on 82541 and 82547 controllers
2019 **********************************************************************/
2021 em_smartspeed(struct adapter *adapter)
2025 if(adapter->link_active || (adapter->hw.phy_type != em_phy_igp) ||
2026 !adapter->hw.autoneg || !(adapter->hw.autoneg_advertised & ADVERTISE_1000_FULL))
2029 if(adapter->smartspeed == 0) {
2030 /* If Master/Slave config fault is asserted twice,
2031 * we assume back-to-back */
2032 em_read_phy_reg(&adapter->hw, PHY_1000T_STATUS, &phy_tmp);
2033 if(!(phy_tmp & SR_1000T_MS_CONFIG_FAULT)) return;
2034 em_read_phy_reg(&adapter->hw, PHY_1000T_STATUS, &phy_tmp);
2035 if(phy_tmp & SR_1000T_MS_CONFIG_FAULT) {
2036 em_read_phy_reg(&adapter->hw, PHY_1000T_CTRL,
2038 if(phy_tmp & CR_1000T_MS_ENABLE) {
2039 phy_tmp &= ~CR_1000T_MS_ENABLE;
2040 em_write_phy_reg(&adapter->hw,
2041 PHY_1000T_CTRL, phy_tmp);
2042 adapter->smartspeed++;
2043 if(adapter->hw.autoneg &&
2044 !em_phy_setup_autoneg(&adapter->hw) &&
2045 !em_read_phy_reg(&adapter->hw, PHY_CTRL,
2047 phy_tmp |= (MII_CR_AUTO_NEG_EN |
2048 MII_CR_RESTART_AUTO_NEG);
2049 em_write_phy_reg(&adapter->hw,
2055 } else if(adapter->smartspeed == EM_SMARTSPEED_DOWNSHIFT) {
2056 /* If still no link, perhaps using 2/3 pair cable */
2057 em_read_phy_reg(&adapter->hw, PHY_1000T_CTRL, &phy_tmp);
2058 phy_tmp |= CR_1000T_MS_ENABLE;
2059 em_write_phy_reg(&adapter->hw, PHY_1000T_CTRL, phy_tmp);
2060 if(adapter->hw.autoneg &&
2061 !em_phy_setup_autoneg(&adapter->hw) &&
2062 !em_read_phy_reg(&adapter->hw, PHY_CTRL, &phy_tmp)) {
2063 phy_tmp |= (MII_CR_AUTO_NEG_EN |
2064 MII_CR_RESTART_AUTO_NEG);
2065 em_write_phy_reg(&adapter->hw, PHY_CTRL, phy_tmp);
2068 /* Restart process after EM_SMARTSPEED_MAX iterations */
2069 if(adapter->smartspeed++ == EM_SMARTSPEED_MAX)
2070 adapter->smartspeed = 0;
2077 * Manage DMA'able memory.
2080 em_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
2084 *(bus_addr_t*) arg = segs->ds_addr;
2089 em_dma_malloc(struct adapter *adapter, bus_size_t size,
2090 struct em_dma_alloc *dma, int mapflags)
2094 r = bus_dma_tag_create(NULL, /* parent */
2095 PAGE_SIZE, 0, /* alignment, bounds */
2096 BUS_SPACE_MAXADDR, /* lowaddr */
2097 BUS_SPACE_MAXADDR, /* highaddr */
2098 NULL, NULL, /* filter, filterarg */
2101 size, /* maxsegsize */
2102 BUS_DMA_ALLOCNOW, /* flags */
2103 NULL, /* lockfunc */
2107 printf("em%d: em_dma_malloc: bus_dma_tag_create failed; "
2108 "error %u\n", adapter->unit, r);
2112 r = bus_dmamem_alloc(dma->dma_tag, (void**) &dma->dma_vaddr,
2113 BUS_DMA_NOWAIT, &dma->dma_map);
2115 printf("em%d: em_dma_malloc: bus_dmammem_alloc failed; "
2116 "size %ju, error %d\n", adapter->unit,
2117 (uintmax_t)size, r);
2121 r = bus_dmamap_load(dma->dma_tag, dma->dma_map, dma->dma_vaddr,
2125 mapflags | BUS_DMA_NOWAIT);
2127 printf("em%d: em_dma_malloc: bus_dmamap_load failed; "
2128 "error %u\n", adapter->unit, r);
2132 dma->dma_size = size;
2136 bus_dmamap_unload(dma->dma_tag, dma->dma_map);
2138 bus_dmamem_free(dma->dma_tag, dma->dma_vaddr, dma->dma_map);
2139 bus_dma_tag_destroy(dma->dma_tag);
2141 dma->dma_map = NULL;
2142 dma->dma_tag = NULL;
2147 em_dma_free(struct adapter *adapter, struct em_dma_alloc *dma)
2149 bus_dmamap_unload(dma->dma_tag, dma->dma_map);
2150 bus_dmamem_free(dma->dma_tag, dma->dma_vaddr, dma->dma_map);
2151 bus_dma_tag_destroy(dma->dma_tag);
2155 /*********************************************************************
2157 * Allocate memory for tx_buffer structures. The tx_buffer stores all
2158 * the information needed to transmit a packet on the wire.
2160 **********************************************************************/
2162 em_allocate_transmit_structures(struct adapter * adapter)
2164 if (!(adapter->tx_buffer_area =
2165 (struct em_buffer *) malloc(sizeof(struct em_buffer) *
2166 adapter->num_tx_desc, M_DEVBUF,
2168 printf("em%d: Unable to allocate tx_buffer memory\n",
2173 bzero(adapter->tx_buffer_area,
2174 sizeof(struct em_buffer) * adapter->num_tx_desc);
2179 /*********************************************************************
2181 * Allocate and initialize transmit structures.
2183 **********************************************************************/
2185 em_setup_transmit_structures(struct adapter * adapter)
2188 * Setup DMA descriptor areas.
2190 if (bus_dma_tag_create(NULL, /* parent */
2191 1, 0, /* alignment, bounds */
2192 BUS_SPACE_MAXADDR, /* lowaddr */
2193 BUS_SPACE_MAXADDR, /* highaddr */
2194 NULL, NULL, /* filter, filterarg */
2195 MCLBYTES * 8, /* maxsize */
2196 EM_MAX_SCATTER, /* nsegments */
2197 MCLBYTES * 8, /* maxsegsize */
2198 BUS_DMA_ALLOCNOW, /* flags */
2199 NULL, /* lockfunc */
2202 printf("em%d: Unable to allocate TX DMA tag\n", adapter->unit);
2206 if (em_allocate_transmit_structures(adapter))
2209 bzero((void *) adapter->tx_desc_base,
2210 (sizeof(struct em_tx_desc)) * adapter->num_tx_desc);
2212 adapter->next_avail_tx_desc = 0;
2213 adapter->oldest_used_tx_desc = 0;
2215 /* Set number of descriptors available */
2216 adapter->num_tx_desc_avail = adapter->num_tx_desc;
2218 /* Set checksum context */
2219 adapter->active_checksum_context = OFFLOAD_NONE;
2224 /*********************************************************************
2226 * Enable transmit unit.
2228 **********************************************************************/
2230 em_initialize_transmit_unit(struct adapter * adapter)
2233 u_int32_t reg_tipg = 0;
2236 INIT_DEBUGOUT("em_initialize_transmit_unit: begin");
2237 /* Setup the Base and Length of the Tx Descriptor Ring */
2238 bus_addr = adapter->txdma.dma_paddr;
2239 E1000_WRITE_REG(&adapter->hw, TDBAL, (u_int32_t)bus_addr);
2240 E1000_WRITE_REG(&adapter->hw, TDBAH, (u_int32_t)(bus_addr >> 32));
2241 E1000_WRITE_REG(&adapter->hw, TDLEN,
2242 adapter->num_tx_desc *
2243 sizeof(struct em_tx_desc));
2245 /* Setup the HW Tx Head and Tail descriptor pointers */
2246 E1000_WRITE_REG(&adapter->hw, TDH, 0);
2247 E1000_WRITE_REG(&adapter->hw, TDT, 0);
2250 HW_DEBUGOUT2("Base = %x, Length = %x\n",
2251 E1000_READ_REG(&adapter->hw, TDBAL),
2252 E1000_READ_REG(&adapter->hw, TDLEN));
2254 /* Set the default values for the Tx Inter Packet Gap timer */
2255 switch (adapter->hw.mac_type) {
2256 case em_82542_rev2_0:
2257 case em_82542_rev2_1:
2258 reg_tipg = DEFAULT_82542_TIPG_IPGT;
2259 reg_tipg |= DEFAULT_82542_TIPG_IPGR1 << E1000_TIPG_IPGR1_SHIFT;
2260 reg_tipg |= DEFAULT_82542_TIPG_IPGR2 << E1000_TIPG_IPGR2_SHIFT;
2263 if (adapter->hw.media_type == em_media_type_fiber)
2264 reg_tipg = DEFAULT_82543_TIPG_IPGT_FIBER;
2266 reg_tipg = DEFAULT_82543_TIPG_IPGT_COPPER;
2267 reg_tipg |= DEFAULT_82543_TIPG_IPGR1 << E1000_TIPG_IPGR1_SHIFT;
2268 reg_tipg |= DEFAULT_82543_TIPG_IPGR2 << E1000_TIPG_IPGR2_SHIFT;
2271 E1000_WRITE_REG(&adapter->hw, TIPG, reg_tipg);
2272 E1000_WRITE_REG(&adapter->hw, TIDV, adapter->tx_int_delay.value);
2273 if(adapter->hw.mac_type >= em_82540)
2274 E1000_WRITE_REG(&adapter->hw, TADV,
2275 adapter->tx_abs_int_delay.value);
2277 /* Program the Transmit Control Register */
2278 reg_tctl = E1000_TCTL_PSP | E1000_TCTL_EN |
2279 (E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT);
2280 if (adapter->hw.mac_type >= em_82573)
2281 reg_tctl |= E1000_TCTL_MULR;
2282 if (adapter->link_duplex == 1) {
2283 reg_tctl |= E1000_FDX_COLLISION_DISTANCE << E1000_COLD_SHIFT;
2285 reg_tctl |= E1000_HDX_COLLISION_DISTANCE << E1000_COLD_SHIFT;
2287 E1000_WRITE_REG(&adapter->hw, TCTL, reg_tctl);
2289 /* Setup Transmit Descriptor Settings for this adapter */
2290 adapter->txd_cmd = E1000_TXD_CMD_IFCS | E1000_TXD_CMD_RS;
2292 if (adapter->tx_int_delay.value > 0)
2293 adapter->txd_cmd |= E1000_TXD_CMD_IDE;
2298 /*********************************************************************
2300 * Free all transmit related data structures.
2302 **********************************************************************/
2304 em_free_transmit_structures(struct adapter * adapter)
2306 struct em_buffer *tx_buffer;
2309 INIT_DEBUGOUT("free_transmit_structures: begin");
2311 if (adapter->tx_buffer_area != NULL) {
2312 tx_buffer = adapter->tx_buffer_area;
2313 for (i = 0; i < adapter->num_tx_desc; i++, tx_buffer++) {
2314 if (tx_buffer->m_head != NULL) {
2315 bus_dmamap_unload(adapter->txtag, tx_buffer->map);
2316 bus_dmamap_destroy(adapter->txtag, tx_buffer->map);
2317 m_freem(tx_buffer->m_head);
2319 tx_buffer->m_head = NULL;
2322 if (adapter->tx_buffer_area != NULL) {
2323 free(adapter->tx_buffer_area, M_DEVBUF);
2324 adapter->tx_buffer_area = NULL;
2326 if (adapter->txtag != NULL) {
2327 bus_dma_tag_destroy(adapter->txtag);
2328 adapter->txtag = NULL;
2333 /*********************************************************************
2335 * The offload context needs to be set when we transfer the first
2336 * packet of a particular protocol (TCP/UDP). We change the
2337 * context only if the protocol type changes.
2339 **********************************************************************/
2341 em_transmit_checksum_setup(struct adapter * adapter,
2343 u_int32_t *txd_upper,
2344 u_int32_t *txd_lower)
2346 struct em_context_desc *TXD;
2347 struct em_buffer *tx_buffer;
2350 if (mp->m_pkthdr.csum_flags) {
2352 if (mp->m_pkthdr.csum_flags & CSUM_TCP) {
2353 *txd_upper = E1000_TXD_POPTS_TXSM << 8;
2354 *txd_lower = E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D;
2355 if (adapter->active_checksum_context == OFFLOAD_TCP_IP)
2358 adapter->active_checksum_context = OFFLOAD_TCP_IP;
2360 } else if (mp->m_pkthdr.csum_flags & CSUM_UDP) {
2361 *txd_upper = E1000_TXD_POPTS_TXSM << 8;
2362 *txd_lower = E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D;
2363 if (adapter->active_checksum_context == OFFLOAD_UDP_IP)
2366 adapter->active_checksum_context = OFFLOAD_UDP_IP;
2378 /* If we reach this point, the checksum offload context
2379 * needs to be reset.
2381 curr_txd = adapter->next_avail_tx_desc;
2382 tx_buffer = &adapter->tx_buffer_area[curr_txd];
2383 TXD = (struct em_context_desc *) &adapter->tx_desc_base[curr_txd];
2385 TXD->lower_setup.ip_fields.ipcss = ETHER_HDR_LEN;
2386 TXD->lower_setup.ip_fields.ipcso =
2387 ETHER_HDR_LEN + offsetof(struct ip, ip_sum);
2388 TXD->lower_setup.ip_fields.ipcse =
2389 htole16(ETHER_HDR_LEN + sizeof(struct ip) - 1);
2391 TXD->upper_setup.tcp_fields.tucss =
2392 ETHER_HDR_LEN + sizeof(struct ip);
2393 TXD->upper_setup.tcp_fields.tucse = htole16(0);
2395 if (adapter->active_checksum_context == OFFLOAD_TCP_IP) {
2396 TXD->upper_setup.tcp_fields.tucso =
2397 ETHER_HDR_LEN + sizeof(struct ip) +
2398 offsetof(struct tcphdr, th_sum);
2399 } else if (adapter->active_checksum_context == OFFLOAD_UDP_IP) {
2400 TXD->upper_setup.tcp_fields.tucso =
2401 ETHER_HDR_LEN + sizeof(struct ip) +
2402 offsetof(struct udphdr, uh_sum);
2405 TXD->tcp_seg_setup.data = htole32(0);
2406 TXD->cmd_and_length = htole32(adapter->txd_cmd | E1000_TXD_CMD_DEXT);
2408 tx_buffer->m_head = NULL;
2410 if (++curr_txd == adapter->num_tx_desc)
2413 adapter->num_tx_desc_avail--;
2414 adapter->next_avail_tx_desc = curr_txd;
2419 /**********************************************************************
2421 * Examine each tx_buffer in the used queue. If the hardware is done
2422 * processing the packet then free associated resources. The
2423 * tx_buffer is put back on the free queue.
2425 **********************************************************************/
2427 em_clean_transmit_interrupts(struct adapter * adapter)
2430 struct em_buffer *tx_buffer;
2431 struct em_tx_desc *tx_desc;
2432 struct ifnet *ifp = adapter->ifp;
2434 mtx_assert(&adapter->mtx, MA_OWNED);
2436 if (adapter->num_tx_desc_avail == adapter->num_tx_desc)
2439 num_avail = adapter->num_tx_desc_avail;
2440 i = adapter->oldest_used_tx_desc;
2442 tx_buffer = &adapter->tx_buffer_area[i];
2443 tx_desc = &adapter->tx_desc_base[i];
2445 bus_dmamap_sync(adapter->txdma.dma_tag, adapter->txdma.dma_map,
2446 BUS_DMASYNC_POSTREAD);
2447 while (tx_desc->upper.fields.status & E1000_TXD_STAT_DD) {
2449 tx_desc->upper.data = 0;
2452 if (tx_buffer->m_head) {
2454 bus_dmamap_unload(adapter->txtag, tx_buffer->map);
2455 bus_dmamap_destroy(adapter->txtag, tx_buffer->map);
2457 m_freem(tx_buffer->m_head);
2458 tx_buffer->m_head = NULL;
2461 if (++i == adapter->num_tx_desc)
2464 tx_buffer = &adapter->tx_buffer_area[i];
2465 tx_desc = &adapter->tx_desc_base[i];
2467 bus_dmamap_sync(adapter->txdma.dma_tag, adapter->txdma.dma_map,
2468 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
2470 adapter->oldest_used_tx_desc = i;
2473 * If we have enough room, clear IFF_DRV_OACTIVE to tell the stack
2474 * that it is OK to send packets.
2475 * If there are no pending descriptors, clear the timeout. Otherwise,
2476 * if some descriptors have been freed, restart the timeout.
2478 if (num_avail > EM_TX_CLEANUP_THRESHOLD) {
2479 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2480 if (num_avail == adapter->num_tx_desc)
2482 else if (num_avail == adapter->num_tx_desc_avail)
2483 ifp->if_timer = EM_TX_TIMEOUT;
2485 adapter->num_tx_desc_avail = num_avail;
2489 /*********************************************************************
2491 * Get a buffer from system mbuf buffer pool.
2493 **********************************************************************/
2495 em_get_buf(int i, struct adapter *adapter,
2498 register struct mbuf *mp = nmp;
2499 struct em_buffer *rx_buffer;
2507 mp = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
2509 adapter->mbuf_cluster_failed++;
2512 mp->m_len = mp->m_pkthdr.len = MCLBYTES;
2514 mp->m_len = mp->m_pkthdr.len = MCLBYTES;
2515 mp->m_data = mp->m_ext.ext_buf;
2519 if (ifp->if_mtu <= ETHERMTU) {
2520 m_adj(mp, ETHER_ALIGN);
2523 rx_buffer = &adapter->rx_buffer_area[i];
2526 * Using memory from the mbuf cluster pool, invoke the
2527 * bus_dma machinery to arrange the memory mapping.
2529 error = bus_dmamap_load(adapter->rxtag, rx_buffer->map,
2530 mtod(mp, void *), mp->m_len,
2531 em_dmamap_cb, &paddr, 0);
2536 rx_buffer->m_head = mp;
2537 adapter->rx_desc_base[i].buffer_addr = htole64(paddr);
2538 bus_dmamap_sync(adapter->rxtag, rx_buffer->map, BUS_DMASYNC_PREREAD);
2543 /*********************************************************************
2545 * Allocate memory for rx_buffer structures. Since we use one
2546 * rx_buffer per received packet, the maximum number of rx_buffer's
2547 * that we'll need is equal to the number of receive descriptors
2548 * that we've allocated.
2550 **********************************************************************/
2552 em_allocate_receive_structures(struct adapter * adapter)
2555 struct em_buffer *rx_buffer;
2557 if (!(adapter->rx_buffer_area =
2558 (struct em_buffer *) malloc(sizeof(struct em_buffer) *
2559 adapter->num_rx_desc, M_DEVBUF,
2561 printf("em%d: Unable to allocate rx_buffer memory\n",
2566 bzero(adapter->rx_buffer_area,
2567 sizeof(struct em_buffer) * adapter->num_rx_desc);
2569 error = bus_dma_tag_create(NULL, /* parent */
2570 1, 0, /* alignment, bounds */
2571 BUS_SPACE_MAXADDR, /* lowaddr */
2572 BUS_SPACE_MAXADDR, /* highaddr */
2573 NULL, NULL, /* filter, filterarg */
2574 MCLBYTES, /* maxsize */
2576 MCLBYTES, /* maxsegsize */
2577 BUS_DMA_ALLOCNOW, /* flags */
2578 NULL, /* lockfunc */
2582 printf("em%d: em_allocate_receive_structures: "
2583 "bus_dma_tag_create failed; error %u\n",
2584 adapter->unit, error);
2588 rx_buffer = adapter->rx_buffer_area;
2589 for (i = 0; i < adapter->num_rx_desc; i++, rx_buffer++) {
2590 error = bus_dmamap_create(adapter->rxtag, BUS_DMA_NOWAIT,
2593 printf("em%d: em_allocate_receive_structures: "
2594 "bus_dmamap_create failed; error %u\n",
2595 adapter->unit, error);
2600 for (i = 0; i < adapter->num_rx_desc; i++) {
2601 error = em_get_buf(i, adapter, NULL);
2603 adapter->rx_buffer_area[i].m_head = NULL;
2604 adapter->rx_desc_base[i].buffer_addr = 0;
2608 bus_dmamap_sync(adapter->rxdma.dma_tag, adapter->rxdma.dma_map,
2609 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
2614 bus_dma_tag_destroy(adapter->rxtag);
2616 adapter->rxtag = NULL;
2617 free(adapter->rx_buffer_area, M_DEVBUF);
2618 adapter->rx_buffer_area = NULL;
2622 /*********************************************************************
2624 * Allocate and initialize receive structures.
2626 **********************************************************************/
2628 em_setup_receive_structures(struct adapter * adapter)
2630 bzero((void *) adapter->rx_desc_base,
2631 (sizeof(struct em_rx_desc)) * adapter->num_rx_desc);
2633 if (em_allocate_receive_structures(adapter))
2636 /* Setup our descriptor pointers */
2637 adapter->next_rx_desc_to_check = 0;
2641 /*********************************************************************
2643 * Enable receive unit.
2645 **********************************************************************/
2647 em_initialize_receive_unit(struct adapter * adapter)
2650 u_int32_t reg_rxcsum;
2654 INIT_DEBUGOUT("em_initialize_receive_unit: begin");
2657 /* Make sure receives are disabled while setting up the descriptor ring */
2658 E1000_WRITE_REG(&adapter->hw, RCTL, 0);
2660 /* Set the Receive Delay Timer Register */
2661 E1000_WRITE_REG(&adapter->hw, RDTR,
2662 adapter->rx_int_delay.value | E1000_RDT_FPDB);
2664 if(adapter->hw.mac_type >= em_82540) {
2665 E1000_WRITE_REG(&adapter->hw, RADV,
2666 adapter->rx_abs_int_delay.value);
2668 /* Set the interrupt throttling rate. Value is calculated
2669 * as DEFAULT_ITR = 1/(MAX_INTS_PER_SEC * 256ns) */
2670 #define MAX_INTS_PER_SEC 8000
2671 #define DEFAULT_ITR 1000000000/(MAX_INTS_PER_SEC * 256)
2672 E1000_WRITE_REG(&adapter->hw, ITR, DEFAULT_ITR);
2675 /* Setup the Base and Length of the Rx Descriptor Ring */
2676 bus_addr = adapter->rxdma.dma_paddr;
2677 E1000_WRITE_REG(&adapter->hw, RDBAL, (u_int32_t)bus_addr);
2678 E1000_WRITE_REG(&adapter->hw, RDBAH, (u_int32_t)(bus_addr >> 32));
2679 E1000_WRITE_REG(&adapter->hw, RDLEN, adapter->num_rx_desc *
2680 sizeof(struct em_rx_desc));
2682 /* Setup the HW Rx Head and Tail Descriptor Pointers */
2683 E1000_WRITE_REG(&adapter->hw, RDH, 0);
2684 E1000_WRITE_REG(&adapter->hw, RDT, adapter->num_rx_desc - 1);
2686 /* Setup the Receive Control Register */
2687 reg_rctl = E1000_RCTL_EN | E1000_RCTL_BAM | E1000_RCTL_LBM_NO |
2688 E1000_RCTL_RDMTS_HALF |
2689 (adapter->hw.mc_filter_type << E1000_RCTL_MO_SHIFT);
2691 if (adapter->hw.tbi_compatibility_on == TRUE)
2692 reg_rctl |= E1000_RCTL_SBP;
2695 switch (adapter->rx_buffer_len) {
2697 case EM_RXBUFFER_2048:
2698 reg_rctl |= E1000_RCTL_SZ_2048;
2700 case EM_RXBUFFER_4096:
2701 reg_rctl |= E1000_RCTL_SZ_4096 | E1000_RCTL_BSEX | E1000_RCTL_LPE;
2703 case EM_RXBUFFER_8192:
2704 reg_rctl |= E1000_RCTL_SZ_8192 | E1000_RCTL_BSEX | E1000_RCTL_LPE;
2706 case EM_RXBUFFER_16384:
2707 reg_rctl |= E1000_RCTL_SZ_16384 | E1000_RCTL_BSEX | E1000_RCTL_LPE;
2711 if (ifp->if_mtu > ETHERMTU)
2712 reg_rctl |= E1000_RCTL_LPE;
2714 /* Enable 82543 Receive Checksum Offload for TCP and UDP */
2715 if ((adapter->hw.mac_type >= em_82543) &&
2716 (ifp->if_capenable & IFCAP_RXCSUM)) {
2717 reg_rxcsum = E1000_READ_REG(&adapter->hw, RXCSUM);
2718 reg_rxcsum |= (E1000_RXCSUM_IPOFL | E1000_RXCSUM_TUOFL);
2719 E1000_WRITE_REG(&adapter->hw, RXCSUM, reg_rxcsum);
2722 /* Enable Receives */
2723 E1000_WRITE_REG(&adapter->hw, RCTL, reg_rctl);
2728 /*********************************************************************
2730 * Free receive related data structures.
2732 **********************************************************************/
2734 em_free_receive_structures(struct adapter *adapter)
2736 struct em_buffer *rx_buffer;
2739 INIT_DEBUGOUT("free_receive_structures: begin");
2741 if (adapter->rx_buffer_area != NULL) {
2742 rx_buffer = adapter->rx_buffer_area;
2743 for (i = 0; i < adapter->num_rx_desc; i++, rx_buffer++) {
2744 if (rx_buffer->map != NULL) {
2745 bus_dmamap_unload(adapter->rxtag, rx_buffer->map);
2746 bus_dmamap_destroy(adapter->rxtag, rx_buffer->map);
2748 if (rx_buffer->m_head != NULL)
2749 m_freem(rx_buffer->m_head);
2750 rx_buffer->m_head = NULL;
2753 if (adapter->rx_buffer_area != NULL) {
2754 free(adapter->rx_buffer_area, M_DEVBUF);
2755 adapter->rx_buffer_area = NULL;
2757 if (adapter->rxtag != NULL) {
2758 bus_dma_tag_destroy(adapter->rxtag);
2759 adapter->rxtag = NULL;
2764 /*********************************************************************
2766 * This routine executes in interrupt context. It replenishes
2767 * the mbufs in the descriptor and sends data which has been
2768 * dma'ed into host memory to upper layer.
2770 * We loop at most count times if count is > 0, or until done if
2773 *********************************************************************/
2775 em_process_receive_interrupts(struct adapter * adapter, int count)
2779 #if __FreeBSD_version < 500000
2780 struct ether_header *eh;
2782 u_int8_t accept_frame = 0;
2784 u_int16_t len, desc_len, prev_len_adj;
2787 /* Pointer to the receive descriptor being examined. */
2788 struct em_rx_desc *current_desc;
2790 mtx_assert(&adapter->mtx, MA_OWNED);
2793 i = adapter->next_rx_desc_to_check;
2794 current_desc = &adapter->rx_desc_base[i];
2795 bus_dmamap_sync(adapter->rxdma.dma_tag, adapter->rxdma.dma_map,
2796 BUS_DMASYNC_POSTREAD);
2798 if (!((current_desc->status) & E1000_RXD_STAT_DD)) {
2802 while ((current_desc->status & E1000_RXD_STAT_DD) && (count != 0)) {
2804 mp = adapter->rx_buffer_area[i].m_head;
2805 bus_dmamap_sync(adapter->rxtag, adapter->rx_buffer_area[i].map,
2806 BUS_DMASYNC_POSTREAD);
2810 desc_len = le16toh(current_desc->length);
2811 if (current_desc->status & E1000_RXD_STAT_EOP) {
2814 if (desc_len < ETHER_CRC_LEN) {
2816 prev_len_adj = ETHER_CRC_LEN - desc_len;
2819 len = desc_len - ETHER_CRC_LEN;
2826 if (current_desc->errors & E1000_RXD_ERR_FRAME_ERR_MASK) {
2828 u_int32_t pkt_len = desc_len;
2830 if (adapter->fmp != NULL)
2831 pkt_len += adapter->fmp->m_pkthdr.len;
2833 last_byte = *(mtod(mp, caddr_t) + desc_len - 1);
2835 if (TBI_ACCEPT(&adapter->hw, current_desc->status,
2836 current_desc->errors,
2837 pkt_len, last_byte)) {
2838 em_tbi_adjust_stats(&adapter->hw,
2841 adapter->hw.mac_addr);
2851 if (em_get_buf(i, adapter, NULL) == ENOBUFS) {
2852 adapter->dropped_pkts++;
2853 em_get_buf(i, adapter, mp);
2854 if (adapter->fmp != NULL)
2855 m_freem(adapter->fmp);
2856 adapter->fmp = NULL;
2857 adapter->lmp = NULL;
2861 /* Assign correct length to the current fragment */
2864 if (adapter->fmp == NULL) {
2865 mp->m_pkthdr.len = len;
2866 adapter->fmp = mp; /* Store the first mbuf */
2869 /* Chain mbuf's together */
2870 mp->m_flags &= ~M_PKTHDR;
2872 * Adjust length of previous mbuf in chain if we
2873 * received less than 4 bytes in the last descriptor.
2875 if (prev_len_adj > 0) {
2876 adapter->lmp->m_len -= prev_len_adj;
2877 adapter->fmp->m_pkthdr.len -= prev_len_adj;
2879 adapter->lmp->m_next = mp;
2880 adapter->lmp = adapter->lmp->m_next;
2881 adapter->fmp->m_pkthdr.len += len;
2885 adapter->fmp->m_pkthdr.rcvif = ifp;
2888 #if __FreeBSD_version < 500000
2889 eh = mtod(adapter->fmp, struct ether_header *);
2890 /* Remove ethernet header from mbuf */
2891 m_adj(adapter->fmp, sizeof(struct ether_header));
2892 em_receive_checksum(adapter, current_desc,
2894 if (current_desc->status & E1000_RXD_STAT_VP)
2895 VLAN_INPUT_TAG(eh, adapter->fmp,
2896 (current_desc->special &
2897 E1000_RXD_SPC_VLAN_MASK));
2899 ether_input(ifp, eh, adapter->fmp);
2902 em_receive_checksum(adapter, current_desc,
2904 if (current_desc->status & E1000_RXD_STAT_VP)
2905 VLAN_INPUT_TAG(ifp, adapter->fmp,
2906 (current_desc->special &
2907 E1000_RXD_SPC_VLAN_MASK),
2908 adapter->fmp = NULL);
2910 if (adapter->fmp != NULL) {
2912 (*ifp->if_input)(ifp, adapter->fmp);
2916 adapter->fmp = NULL;
2917 adapter->lmp = NULL;
2920 adapter->dropped_pkts++;
2921 em_get_buf(i, adapter, mp);
2922 if (adapter->fmp != NULL)
2923 m_freem(adapter->fmp);
2924 adapter->fmp = NULL;
2925 adapter->lmp = NULL;
2928 /* Zero out the receive descriptors status */
2929 current_desc->status = 0;
2931 /* Advance the E1000's Receive Queue #0 "Tail Pointer". */
2932 E1000_WRITE_REG(&adapter->hw, RDT, i);
2934 /* Advance our pointers to the next descriptor */
2935 if (++i == adapter->num_rx_desc) {
2937 current_desc = adapter->rx_desc_base;
2941 bus_dmamap_sync(adapter->rxdma.dma_tag, adapter->rxdma.dma_map,
2942 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
2943 adapter->next_rx_desc_to_check = i;
2947 /*********************************************************************
2949 * Verify that the hardware indicated that the checksum is valid.
2950 * Inform the stack about the status of checksum so that stack
2951 * doesn't spend time verifying the checksum.
2953 *********************************************************************/
2955 em_receive_checksum(struct adapter *adapter,
2956 struct em_rx_desc *rx_desc,
2959 /* 82543 or newer only */
2960 if ((adapter->hw.mac_type < em_82543) ||
2961 /* Ignore Checksum bit is set */
2962 (rx_desc->status & E1000_RXD_STAT_IXSM)) {
2963 mp->m_pkthdr.csum_flags = 0;
2967 if (rx_desc->status & E1000_RXD_STAT_IPCS) {
2969 if (!(rx_desc->errors & E1000_RXD_ERR_IPE)) {
2970 /* IP Checksum Good */
2971 mp->m_pkthdr.csum_flags = CSUM_IP_CHECKED;
2972 mp->m_pkthdr.csum_flags |= CSUM_IP_VALID;
2975 mp->m_pkthdr.csum_flags = 0;
2979 if (rx_desc->status & E1000_RXD_STAT_TCPCS) {
2981 if (!(rx_desc->errors & E1000_RXD_ERR_TCPE)) {
2982 mp->m_pkthdr.csum_flags |=
2983 (CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
2984 mp->m_pkthdr.csum_data = htons(0xffff);
2993 em_enable_vlans(struct adapter *adapter)
2997 E1000_WRITE_REG(&adapter->hw, VET, ETHERTYPE_VLAN);
2999 ctrl = E1000_READ_REG(&adapter->hw, CTRL);
3000 ctrl |= E1000_CTRL_VME;
3001 E1000_WRITE_REG(&adapter->hw, CTRL, ctrl);
3007 em_disable_vlans(struct adapter *adapter)
3011 ctrl = E1000_READ_REG(&adapter->hw, CTRL);
3012 ctrl &= ~E1000_CTRL_VME;
3013 E1000_WRITE_REG(&adapter->hw, CTRL, ctrl);
3019 em_enable_intr(struct adapter * adapter)
3021 E1000_WRITE_REG(&adapter->hw, IMS, (IMS_ENABLE_MASK));
3026 em_disable_intr(struct adapter *adapter)
3029 * The first version of 82542 had an errata where when link was forced it
3030 * would stay up even up even if the cable was disconnected. Sequence errors
3031 * were used to detect the disconnect and then the driver would unforce the link.
3032 * This code in the in the ISR. For this to work correctly the Sequence error
3033 * interrupt had to be enabled all the time.
3036 if (adapter->hw.mac_type == em_82542_rev2_0)
3037 E1000_WRITE_REG(&adapter->hw, IMC,
3038 (0xffffffff & ~E1000_IMC_RXSEQ));
3040 E1000_WRITE_REG(&adapter->hw, IMC,
3046 em_is_valid_ether_addr(u_int8_t *addr)
3048 char zero_addr[6] = { 0, 0, 0, 0, 0, 0 };
3050 if ((addr[0] & 1) || (!bcmp(addr, zero_addr, ETHER_ADDR_LEN))) {
3058 em_write_pci_cfg(struct em_hw *hw,
3062 pci_write_config(((struct em_osdep *)hw->back)->dev, reg,
3067 em_read_pci_cfg(struct em_hw *hw, uint32_t reg,
3070 *value = pci_read_config(((struct em_osdep *)hw->back)->dev,
3076 em_pci_set_mwi(struct em_hw *hw)
3078 pci_write_config(((struct em_osdep *)hw->back)->dev,
3080 (hw->pci_cmd_word | CMD_MEM_WRT_INVALIDATE), 2);
3085 em_pci_clear_mwi(struct em_hw *hw)
3087 pci_write_config(((struct em_osdep *)hw->back)->dev,
3089 (hw->pci_cmd_word & ~CMD_MEM_WRT_INVALIDATE), 2);
3094 em_io_read(struct em_hw *hw, unsigned long port)
3100 em_io_write(struct em_hw *hw, unsigned long port, uint32_t value)
3106 /*********************************************************************
3107 * 82544 Coexistence issue workaround.
3108 * There are 2 issues.
3109 * 1. Transmit Hang issue.
3110 * To detect this issue, following equation can be used...
3111 * SIZE[3:0] + ADDR[2:0] = SUM[3:0].
3112 * If SUM[3:0] is in between 1 to 4, we will have this issue.
3115 * To detect this issue, following equation can be used...
3116 * SIZE[3:0] + ADDR[2:0] = SUM[3:0].
3117 * If SUM[3:0] is in between 9 to c, we will have this issue.
3121 * Make sure we do not have ending address as 1,2,3,4(Hang) or 9,a,b,c (DAC)
3123 *** *********************************************************************/
3125 em_fill_descriptors (u_int64_t address,
3127 PDESC_ARRAY desc_array)
3129 /* Since issue is sensitive to length and address.*/
3130 /* Let us first check the address...*/
3131 u_int32_t safe_terminator;
3133 desc_array->descriptor[0].address = address;
3134 desc_array->descriptor[0].length = length;
3135 desc_array->elements = 1;
3136 return desc_array->elements;
3138 safe_terminator = (u_int32_t)((((u_int32_t)address & 0x7) + (length & 0xF)) & 0xF);
3139 /* if it does not fall between 0x1 to 0x4 and 0x9 to 0xC then return */
3140 if (safe_terminator == 0 ||
3141 (safe_terminator > 4 &&
3142 safe_terminator < 9) ||
3143 (safe_terminator > 0xC &&
3144 safe_terminator <= 0xF)) {
3145 desc_array->descriptor[0].address = address;
3146 desc_array->descriptor[0].length = length;
3147 desc_array->elements = 1;
3148 return desc_array->elements;
3151 desc_array->descriptor[0].address = address;
3152 desc_array->descriptor[0].length = length - 4;
3153 desc_array->descriptor[1].address = address + (length - 4);
3154 desc_array->descriptor[1].length = 4;
3155 desc_array->elements = 2;
3156 return desc_array->elements;
3159 /**********************************************************************
3161 * Update the board statistics counters.
3163 **********************************************************************/
3165 em_update_stats_counters(struct adapter *adapter)
3169 if(adapter->hw.media_type == em_media_type_copper ||
3170 (E1000_READ_REG(&adapter->hw, STATUS) & E1000_STATUS_LU)) {
3171 adapter->stats.symerrs += E1000_READ_REG(&adapter->hw, SYMERRS);
3172 adapter->stats.sec += E1000_READ_REG(&adapter->hw, SEC);
3174 adapter->stats.crcerrs += E1000_READ_REG(&adapter->hw, CRCERRS);
3175 adapter->stats.mpc += E1000_READ_REG(&adapter->hw, MPC);
3176 adapter->stats.scc += E1000_READ_REG(&adapter->hw, SCC);
3177 adapter->stats.ecol += E1000_READ_REG(&adapter->hw, ECOL);
3179 adapter->stats.mcc += E1000_READ_REG(&adapter->hw, MCC);
3180 adapter->stats.latecol += E1000_READ_REG(&adapter->hw, LATECOL);
3181 adapter->stats.colc += E1000_READ_REG(&adapter->hw, COLC);
3182 adapter->stats.dc += E1000_READ_REG(&adapter->hw, DC);
3183 adapter->stats.rlec += E1000_READ_REG(&adapter->hw, RLEC);
3184 adapter->stats.xonrxc += E1000_READ_REG(&adapter->hw, XONRXC);
3185 adapter->stats.xontxc += E1000_READ_REG(&adapter->hw, XONTXC);
3186 adapter->stats.xoffrxc += E1000_READ_REG(&adapter->hw, XOFFRXC);
3187 adapter->stats.xofftxc += E1000_READ_REG(&adapter->hw, XOFFTXC);
3188 adapter->stats.fcruc += E1000_READ_REG(&adapter->hw, FCRUC);
3189 adapter->stats.prc64 += E1000_READ_REG(&adapter->hw, PRC64);
3190 adapter->stats.prc127 += E1000_READ_REG(&adapter->hw, PRC127);
3191 adapter->stats.prc255 += E1000_READ_REG(&adapter->hw, PRC255);
3192 adapter->stats.prc511 += E1000_READ_REG(&adapter->hw, PRC511);
3193 adapter->stats.prc1023 += E1000_READ_REG(&adapter->hw, PRC1023);
3194 adapter->stats.prc1522 += E1000_READ_REG(&adapter->hw, PRC1522);
3195 adapter->stats.gprc += E1000_READ_REG(&adapter->hw, GPRC);
3196 adapter->stats.bprc += E1000_READ_REG(&adapter->hw, BPRC);
3197 adapter->stats.mprc += E1000_READ_REG(&adapter->hw, MPRC);
3198 adapter->stats.gptc += E1000_READ_REG(&adapter->hw, GPTC);
3200 /* For the 64-bit byte counters the low dword must be read first. */
3201 /* Both registers clear on the read of the high dword */
3203 adapter->stats.gorcl += E1000_READ_REG(&adapter->hw, GORCL);
3204 adapter->stats.gorch += E1000_READ_REG(&adapter->hw, GORCH);
3205 adapter->stats.gotcl += E1000_READ_REG(&adapter->hw, GOTCL);
3206 adapter->stats.gotch += E1000_READ_REG(&adapter->hw, GOTCH);
3208 adapter->stats.rnbc += E1000_READ_REG(&adapter->hw, RNBC);
3209 adapter->stats.ruc += E1000_READ_REG(&adapter->hw, RUC);
3210 adapter->stats.rfc += E1000_READ_REG(&adapter->hw, RFC);
3211 adapter->stats.roc += E1000_READ_REG(&adapter->hw, ROC);
3212 adapter->stats.rjc += E1000_READ_REG(&adapter->hw, RJC);
3214 adapter->stats.torl += E1000_READ_REG(&adapter->hw, TORL);
3215 adapter->stats.torh += E1000_READ_REG(&adapter->hw, TORH);
3216 adapter->stats.totl += E1000_READ_REG(&adapter->hw, TOTL);
3217 adapter->stats.toth += E1000_READ_REG(&adapter->hw, TOTH);
3219 adapter->stats.tpr += E1000_READ_REG(&adapter->hw, TPR);
3220 adapter->stats.tpt += E1000_READ_REG(&adapter->hw, TPT);
3221 adapter->stats.ptc64 += E1000_READ_REG(&adapter->hw, PTC64);
3222 adapter->stats.ptc127 += E1000_READ_REG(&adapter->hw, PTC127);
3223 adapter->stats.ptc255 += E1000_READ_REG(&adapter->hw, PTC255);
3224 adapter->stats.ptc511 += E1000_READ_REG(&adapter->hw, PTC511);
3225 adapter->stats.ptc1023 += E1000_READ_REG(&adapter->hw, PTC1023);
3226 adapter->stats.ptc1522 += E1000_READ_REG(&adapter->hw, PTC1522);
3227 adapter->stats.mptc += E1000_READ_REG(&adapter->hw, MPTC);
3228 adapter->stats.bptc += E1000_READ_REG(&adapter->hw, BPTC);
3230 if (adapter->hw.mac_type >= em_82543) {
3231 adapter->stats.algnerrc +=
3232 E1000_READ_REG(&adapter->hw, ALGNERRC);
3233 adapter->stats.rxerrc +=
3234 E1000_READ_REG(&adapter->hw, RXERRC);
3235 adapter->stats.tncrs +=
3236 E1000_READ_REG(&adapter->hw, TNCRS);
3237 adapter->stats.cexterr +=
3238 E1000_READ_REG(&adapter->hw, CEXTERR);
3239 adapter->stats.tsctc +=
3240 E1000_READ_REG(&adapter->hw, TSCTC);
3241 adapter->stats.tsctfc +=
3242 E1000_READ_REG(&adapter->hw, TSCTFC);
3246 /* Fill out the OS statistics structure */
3247 ifp->if_ibytes = adapter->stats.gorcl;
3248 ifp->if_obytes = adapter->stats.gotcl;
3249 ifp->if_imcasts = adapter->stats.mprc;
3250 ifp->if_collisions = adapter->stats.colc;
3254 adapter->dropped_pkts +
3255 adapter->stats.rxerrc +
3256 adapter->stats.crcerrs +
3257 adapter->stats.algnerrc +
3258 adapter->stats.rlec +
3259 adapter->stats.mpc + adapter->stats.cexterr;
3262 ifp->if_oerrors = adapter->stats.ecol + adapter->stats.latecol;
3267 /**********************************************************************
3269 * This routine is called only when em_display_debug_stats is enabled.
3270 * This routine provides a way to take a look at important statistics
3271 * maintained by the driver and hardware.
3273 **********************************************************************/
3275 em_print_debug_info(struct adapter *adapter)
3277 int unit = adapter->unit;
3278 uint8_t *hw_addr = adapter->hw.hw_addr;
3280 printf("em%d: Adapter hardware address = %p \n", unit, hw_addr);
3281 printf("em%d:CTRL = 0x%x\n", unit,
3282 E1000_READ_REG(&adapter->hw, CTRL));
3283 printf("em%d:RCTL = 0x%x PS=(0x8402)\n", unit,
3284 E1000_READ_REG(&adapter->hw, RCTL));
3285 printf("em%d:tx_int_delay = %d, tx_abs_int_delay = %d\n", unit,
3286 E1000_READ_REG(&adapter->hw, TIDV),
3287 E1000_READ_REG(&adapter->hw, TADV));
3288 printf("em%d:rx_int_delay = %d, rx_abs_int_delay = %d\n", unit,
3289 E1000_READ_REG(&adapter->hw, RDTR),
3290 E1000_READ_REG(&adapter->hw, RADV));
3291 printf("em%d: fifo workaround = %lld, fifo_reset = %lld\n", unit,
3292 (long long)adapter->tx_fifo_wrk_cnt,
3293 (long long)adapter->tx_fifo_reset_cnt);
3294 printf("em%d: hw tdh = %d, hw tdt = %d\n", unit,
3295 E1000_READ_REG(&adapter->hw, TDH),
3296 E1000_READ_REG(&adapter->hw, TDT));
3297 printf("em%d: Num Tx descriptors avail = %d\n", unit,
3298 adapter->num_tx_desc_avail);
3299 printf("em%d: Tx Descriptors not avail1 = %ld\n", unit,
3300 adapter->no_tx_desc_avail1);
3301 printf("em%d: Tx Descriptors not avail2 = %ld\n", unit,
3302 adapter->no_tx_desc_avail2);
3303 printf("em%d: Std mbuf failed = %ld\n", unit,
3304 adapter->mbuf_alloc_failed);
3305 printf("em%d: Std mbuf cluster failed = %ld\n", unit,
3306 adapter->mbuf_cluster_failed);
3307 printf("em%d: Driver dropped packets = %ld\n", unit,
3308 adapter->dropped_pkts);
3314 em_print_hw_stats(struct adapter *adapter)
3316 int unit = adapter->unit;
3318 printf("em%d: Excessive collisions = %lld\n", unit,
3319 (long long)adapter->stats.ecol);
3320 printf("em%d: Symbol errors = %lld\n", unit,
3321 (long long)adapter->stats.symerrs);
3322 printf("em%d: Sequence errors = %lld\n", unit,
3323 (long long)adapter->stats.sec);
3324 printf("em%d: Defer count = %lld\n", unit,
3325 (long long)adapter->stats.dc);
3327 printf("em%d: Missed Packets = %lld\n", unit,
3328 (long long)adapter->stats.mpc);
3329 printf("em%d: Receive No Buffers = %lld\n", unit,
3330 (long long)adapter->stats.rnbc);
3331 printf("em%d: Receive length errors = %lld\n", unit,
3332 (long long)adapter->stats.rlec);
3333 printf("em%d: Receive errors = %lld\n", unit,
3334 (long long)adapter->stats.rxerrc);
3335 printf("em%d: Crc errors = %lld\n", unit,
3336 (long long)adapter->stats.crcerrs);
3337 printf("em%d: Alignment errors = %lld\n", unit,
3338 (long long)adapter->stats.algnerrc);
3339 printf("em%d: Carrier extension errors = %lld\n", unit,
3340 (long long)adapter->stats.cexterr);
3342 printf("em%d: XON Rcvd = %lld\n", unit,
3343 (long long)adapter->stats.xonrxc);
3344 printf("em%d: XON Xmtd = %lld\n", unit,
3345 (long long)adapter->stats.xontxc);
3346 printf("em%d: XOFF Rcvd = %lld\n", unit,
3347 (long long)adapter->stats.xoffrxc);
3348 printf("em%d: XOFF Xmtd = %lld\n", unit,
3349 (long long)adapter->stats.xofftxc);
3351 printf("em%d: Good Packets Rcvd = %lld\n", unit,
3352 (long long)adapter->stats.gprc);
3353 printf("em%d: Good Packets Xmtd = %lld\n", unit,
3354 (long long)adapter->stats.gptc);
3360 em_sysctl_debug_info(SYSCTL_HANDLER_ARGS)
3364 struct adapter *adapter;
3367 error = sysctl_handle_int(oidp, &result, 0, req);
3369 if (error || !req->newptr)
3373 adapter = (struct adapter *)arg1;
3374 em_print_debug_info(adapter);
3382 em_sysctl_stats(SYSCTL_HANDLER_ARGS)
3386 struct adapter *adapter;
3389 error = sysctl_handle_int(oidp, &result, 0, req);
3391 if (error || !req->newptr)
3395 adapter = (struct adapter *)arg1;
3396 em_print_hw_stats(adapter);
3403 em_sysctl_int_delay(SYSCTL_HANDLER_ARGS)
3405 struct em_int_delay_info *info;
3406 struct adapter *adapter;
3413 info = (struct em_int_delay_info *)arg1;
3414 adapter = info->adapter;
3415 usecs = info->value;
3416 error = sysctl_handle_int(oidp, &usecs, 0, req);
3417 if (error != 0 || req->newptr == NULL)
3419 if (usecs < 0 || usecs > E1000_TICKS_TO_USECS(65535))
3421 info->value = usecs;
3422 ticks = E1000_USECS_TO_TICKS(usecs);
3425 regval = E1000_READ_OFFSET(&adapter->hw, info->offset);
3426 regval = (regval & ~0xffff) | (ticks & 0xffff);
3427 /* Handle a few special cases. */
3428 switch (info->offset) {
3430 case E1000_82542_RDTR:
3431 regval |= E1000_RDT_FPDB;
3434 case E1000_82542_TIDV:
3436 adapter->txd_cmd &= ~E1000_TXD_CMD_IDE;
3437 /* Don't write 0 into the TIDV register. */
3440 adapter->txd_cmd |= E1000_TXD_CMD_IDE;
3443 E1000_WRITE_OFFSET(&adapter->hw, info->offset, regval);
3449 em_add_int_delay_sysctl(struct adapter *adapter, const char *name,
3450 const char *description, struct em_int_delay_info *info,
3451 int offset, int value)
3453 info->adapter = adapter;
3454 info->offset = offset;
3455 info->value = value;
3456 SYSCTL_ADD_PROC(device_get_sysctl_ctx(adapter->dev),
3457 SYSCTL_CHILDREN(device_get_sysctl_tree(adapter->dev)),
3458 OID_AUTO, name, CTLTYPE_INT|CTLFLAG_RW,
3459 info, 0, em_sysctl_int_delay, "I", description);