BPicture: Fix archive constructor.
[haiku.git] / src / add-ons / kernel / drivers / network / ipro1000 / if_em.c
blobbd6f1aae6235289c3a7fbec553c048b9ecc09e1a
1 /**************************************************************************
3 Copyright (c) 2001-2003, Intel Corporation
4 All rights reserved.
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 ***************************************************************************/
34 /*$FreeBSD: /repoman/r/ncvs/src/sys/dev/em/if_em.c,v 1.2.2.19 2004/04/22 22:03:26 ru Exp $*/
36 #include "debug.h"
37 #include "if_em.h"
38 #include "util.h"
41 /*********************************************************************
42 * Function prototypes
43 *********************************************************************/
44 int em_attach(device_t);
45 int em_detach(device_t);
46 static int32 em_intr(void *);
47 static int32 event_handler(void *);
48 static int start_event_thread(struct adapter *);
49 static void stop_event_thread(struct adapter *);
50 static void em_start(struct ifnet *);
51 static int em_ioctl(struct ifnet *, u_long, caddr_t);
52 static void em_watchdog(struct ifnet *);
53 static void em_init(void *);
54 static void em_stop(void *);
55 void em_media_status(struct ifnet *, struct ifmediareq *);
56 //static int em_media_change(struct ifnet *);
57 static void em_identify_hardware(struct adapter *);
58 static int em_allocate_pci_resources(struct adapter *);
59 static void em_free_pci_resources(struct adapter *);
60 static void em_local_timer(void *);
61 static int em_hardware_init(struct adapter *);
62 static void em_setup_interface(device_t, struct adapter *);
63 static int em_setup_transmit_structures(struct adapter *);
64 static void em_initialize_transmit_unit(struct adapter *);
65 static int em_setup_receive_structures(struct adapter *);
66 static void em_initialize_receive_unit(struct adapter *);
67 static void em_enable_intr(struct adapter *);
68 static void em_disable_intr(struct adapter *);
69 static void em_free_transmit_structures(struct adapter *);
70 static void em_free_receive_structures(struct adapter *);
71 static void em_update_stats_counters(struct adapter *);
72 static void em_clean_transmit_interrupts(struct adapter *);
73 static int em_allocate_receive_structures(struct adapter *);
74 static int em_allocate_transmit_structures(struct adapter *);
75 static void em_process_receive_interrupts(struct adapter *, int);
76 static void em_receive_checksum(struct adapter *,
77 struct em_rx_desc *,
78 struct mbuf *);
79 static void em_transmit_checksum_setup(struct adapter *,
80 struct mbuf *,
81 u_int32_t *,
82 u_int32_t *);
83 static void em_set_promisc(struct adapter *);
84 static void em_disable_promisc(struct adapter *);
85 static void em_set_multi(struct adapter *);
86 static void em_print_hw_stats(struct adapter *);
87 static void em_print_link_status(struct adapter *);
88 static int em_get_buf(int i, struct adapter *,
89 struct mbuf *);
90 static void em_enable_vlans(struct adapter *);
91 static int em_encap(struct adapter *, struct mbuf *);
92 static void em_smartspeed(struct adapter *);
93 static int em_82547_fifo_workaround(struct adapter *, int);
94 static void em_82547_update_fifo_head(struct adapter *, int);
95 static int em_82547_tx_fifo_reset(struct adapter *);
96 static void em_82547_move_tail(void *arg);
97 static void em_print_debug_info(struct adapter *);
98 static int em_is_valid_ether_addr(u_int8_t *);
99 //static int em_sysctl_stats(SYSCTL_HANDLER_ARGS);
100 //static int em_sysctl_debug_info(SYSCTL_HANDLER_ARGS);
101 static u_int32_t em_fill_descriptors (u_int64_t address,
102 u_int32_t length,
103 PDESC_ARRAY desc_array);
104 //static int em_sysctl_int_delay(SYSCTL_HANDLER_ARGS);
105 static void em_add_int_delay_sysctl(struct adapter *, const char *,
106 const char *, struct em_int_delay_info *,
107 int, int);
109 /*********************************************************************
110 * Tunable default values.
111 *********************************************************************/
113 #define E1000_TICKS_TO_USECS(ticks) ((1024 * (ticks) + 500) / 1000)
114 #define E1000_USECS_TO_TICKS(usecs) ((1000 * (usecs) + 512) / 1024)
116 static int em_tx_int_delay_dflt = E1000_TICKS_TO_USECS(EM_TIDV);
117 static int em_rx_int_delay_dflt = E1000_TICKS_TO_USECS(EM_RDTR);
118 static int em_tx_abs_int_delay_dflt = E1000_TICKS_TO_USECS(EM_TADV);
119 static int em_rx_abs_int_delay_dflt = E1000_TICKS_TO_USECS(EM_RADV);
121 TUNABLE_INT("hw.em.tx_int_delay", &em_tx_int_delay_dflt);
122 TUNABLE_INT("hw.em.rx_int_delay", &em_rx_int_delay_dflt);
123 TUNABLE_INT("hw.em.tx_abs_int_delay", &em_tx_abs_int_delay_dflt);
124 TUNABLE_INT("hw.em.rx_abs_int_delay", &em_rx_abs_int_delay_dflt);
126 /* sysctl vars */
128 SYSCTL_INT(_hw, OID_AUTO, em_tx_int_delay, CTLFLAG_RD, &em_tx_int_delay_dflt, 0,
129 "Transmit interrupt delay");
130 SYSCTL_INT(_hw, OID_AUTO, em_rx_int_delay, CTLFLAG_RD, &em_rx_int_delay_dflt, 0,
131 "Receive interrupt delay");
132 SYSCTL_INT(_hw, OID_AUTO, em_tx_abs_int_delay, CTLFLAG_RD, &em_tx_abs_int_delay_dflt,
133 0, "Transmit absolute interrupt delay");
134 SYSCTL_INT(_hw, OID_AUTO, em_rx_ans_int_delay, CTLFLAG_RD, &em_rx_abs_int_delay_dflt,
136 "Receive absolute interrupt delay");
139 /*********************************************************************
140 * Device initialization routine
142 * The attach entry point is called when the driver is being loaded.
143 * This routine identifies the type of hardware, allocates all resources
144 * and initializes the hardware.
146 * return 0 on success, positive on failure
147 *********************************************************************/
150 em_attach(device_t dev)
152 struct adapter * adapter;
153 int s;
154 int tsize, rsize;
155 int error = 0;
157 INIT_DEBUGOUT("em_attach: begin");
158 s = splimp();
160 /* Allocate, clear, and link in our adapter structure */
161 if (!(adapter = device_get_softc(dev))) {
162 ERROROUT("adapter structure allocation failed");
163 splx(s);
164 return(ENOMEM);
166 bzero(adapter, sizeof(struct adapter ));
167 adapter->dev = dev;
168 adapter->osdep.dev = dev;
169 adapter->unit = device_get_unit(dev);
171 /* SYSCTL stuff */
172 sysctl_ctx_init(&adapter->sysctl_ctx);
173 adapter->sysctl_tree = SYSCTL_ADD_NODE(&adapter->sysctl_ctx,
174 SYSCTL_STATIC_CHILDREN(_hw),
175 OID_AUTO,
176 device_get_nameunit(dev),
177 CTLFLAG_RD,
178 0, "");
179 if (adapter->sysctl_tree == NULL) {
180 error = EIO;
181 goto err_sysctl;
184 SYSCTL_ADD_PROC(&adapter->sysctl_ctx,
185 SYSCTL_CHILDREN(adapter->sysctl_tree),
186 OID_AUTO, "debug_info", CTLTYPE_INT|CTLFLAG_RW,
187 (void *)adapter, 0,
188 em_sysctl_debug_info, "I", "Debug Information");
190 SYSCTL_ADD_PROC(&adapter->sysctl_ctx,
191 SYSCTL_CHILDREN(adapter->sysctl_tree),
192 OID_AUTO, "stats", CTLTYPE_INT|CTLFLAG_RW,
193 (void *)adapter, 0,
194 em_sysctl_stats, "I", "Statistics");
196 callout_handle_init(&adapter->timer_handle);
197 callout_handle_init(&adapter->tx_fifo_timer_handle);
199 /* create event processing thread */
200 if (start_event_thread(adapter) < 0) {
201 error = EIO;
202 goto err_event;
205 /* Determine hardware revision */
206 em_identify_hardware(adapter);
208 /* Set up some sysctls for the tunable interrupt delays */
209 em_add_int_delay_sysctl(adapter, "rx_int_delay",
210 "receive interrupt delay in usecs", &adapter->rx_int_delay,
211 E1000_REG_OFFSET(&adapter->hw, RDTR), em_rx_int_delay_dflt);
212 em_add_int_delay_sysctl(adapter, "tx_int_delay",
213 "transmit interrupt delay in usecs", &adapter->tx_int_delay,
214 E1000_REG_OFFSET(&adapter->hw, TIDV), em_tx_int_delay_dflt);
215 if (adapter->hw.mac_type >= em_82540) {
216 em_add_int_delay_sysctl(adapter, "rx_abs_int_delay",
217 "receive interrupt delay limit in usecs",
218 &adapter->rx_abs_int_delay,
219 E1000_REG_OFFSET(&adapter->hw, RADV),
220 em_rx_abs_int_delay_dflt);
221 em_add_int_delay_sysctl(adapter, "tx_abs_int_delay",
222 "transmit interrupt delay limit in usecs",
223 &adapter->tx_abs_int_delay,
224 E1000_REG_OFFSET(&adapter->hw, TADV),
225 em_tx_abs_int_delay_dflt);
228 /* Parameters (to be read from user) */
229 adapter->num_tx_desc = EM_MAX_TXD;
230 adapter->num_rx_desc = EM_MAX_RXD;
231 adapter->hw.autoneg = DO_AUTO_NEG;
232 adapter->hw.wait_autoneg_complete = WAIT_FOR_AUTO_NEG_DEFAULT;
233 adapter->hw.autoneg_advertised = AUTONEG_ADV_DEFAULT;
234 adapter->hw.tbi_compatibility_en = TRUE;
235 adapter->rx_buffer_len = EM_RXBUFFER_2048;
239 * These parameters control the automatic generation(Tx) and
240 * response(Rx) to Ethernet PAUSE frames.
242 adapter->hw.fc_high_water = FC_DEFAULT_HI_THRESH;
243 adapter->hw.fc_low_water = FC_DEFAULT_LO_THRESH;
244 adapter->hw.fc_pause_time = FC_DEFAULT_TX_TIMER;
245 adapter->hw.fc_send_xon = TRUE;
246 adapter->hw.fc = em_fc_full;
248 adapter->hw.phy_init_script = 1;
249 adapter->hw.phy_reset_disable = FALSE;
251 #ifndef EM_MASTER_SLAVE
252 adapter->hw.master_slave = em_ms_hw_default;
253 #else
254 adapter->hw.master_slave = EM_MASTER_SLAVE;
255 #endif
258 * Set the max frame size assuming standard ethernet
259 * sized frames
261 adapter->hw.max_frame_size =
262 ETHERMTU + ETHER_HDR_LEN + ETHER_CRC_LEN;
264 adapter->hw.min_frame_size =
265 MINIMUM_ETHERNET_PACKET_SIZE + ETHER_CRC_LEN;
268 * This controls when hardware reports transmit completion
269 * status.
271 adapter->hw.report_tx_early = 1;
274 if (em_allocate_pci_resources(adapter)) {
275 dprintf("ipro1000/%d: Allocation of PCI resources failed\n",
276 adapter->unit);
277 error = ENXIO;
278 goto err_pci;
281 em_init_eeprom_params(&adapter->hw);
283 tsize = EM_ROUNDUP(adapter->num_tx_desc *
284 sizeof(struct em_tx_desc), 4096);
286 /* Allocate Transmit Descriptor ring */
287 if (!(adapter->tx_desc_base = (struct em_tx_desc *)
288 contigmalloc(tsize, M_DEVBUF, M_NOWAIT, 0, ~0,
289 PAGE_SIZE, 0))) {
290 dprintf("ipro1000/%d: Unable to allocate TxDescriptor memory\n",
291 adapter->unit);
292 error = ENOMEM;
293 goto err_tx_desc;
296 rsize = EM_ROUNDUP(adapter->num_rx_desc *
297 sizeof(struct em_rx_desc), 4096);
299 /* Allocate Receive Descriptor ring */
300 if (!(adapter->rx_desc_base = (struct em_rx_desc *)
301 contigmalloc(rsize, M_DEVBUF, M_NOWAIT, 0, ~0,
302 PAGE_SIZE, 0))) {
303 dprintf("ipro1000/%d: Unable to allocate rx_desc memory\n",
304 adapter->unit);
305 error = ENOMEM;
306 goto err_rx_desc;
309 /* Initialize the hardware */
310 if (em_hardware_init(adapter)) {
311 dprintf("ipro1000/%d: Unable to initialize the hardware\n",
312 adapter->unit);
313 error = EIO;
314 goto err_hw_init;
317 /* Copy the permanent MAC address out of the EEPROM */
318 if (em_read_mac_addr(&adapter->hw) < 0) {
319 dprintf("ipro1000/%d: EEPROM read error while reading mac address\n",
320 adapter->unit);
321 error = EIO;
322 goto err_mac_addr;
325 if (!em_is_valid_ether_addr(adapter->hw.mac_addr)) {
326 dprintf("ipro1000/%d: Invalid mac address\n", adapter->unit);
327 error = EIO;
328 goto err_mac_addr;
332 bcopy(adapter->hw.mac_addr, adapter->interface_data.ac_enaddr,
333 ETHER_ADDR_LEN);
335 /* Setup OS specific network interface */
336 em_setup_interface(dev, adapter);
338 /* Initialize statistics */
339 em_clear_hw_cntrs(&adapter->hw);
340 em_update_stats_counters(adapter);
341 adapter->hw.get_link_status = 1;
342 em_check_for_link(&adapter->hw);
344 /* Print the link status */
345 if (adapter->link_active == 1) {
346 em_get_speed_and_duplex(&adapter->hw, &adapter->link_speed,
347 &adapter->link_duplex);
348 dprintf("ipro1000/%d: Speed:%d Mbps Duplex:%s\n",
349 adapter->unit,
350 adapter->link_speed,
351 adapter->link_duplex == FULL_DUPLEX ? "Full" : "Half");
352 } else
353 dprintf("ipro1000/%d: Speed:N/A Duplex:N/A\n", adapter->unit);
355 /* Identify 82544 on PCIX */
356 em_get_bus_info(&adapter->hw);
357 if(adapter->hw.bus_type == em_bus_type_pcix &&
358 adapter->hw.mac_type == em_82544) {
359 adapter->pcix_82544 = TRUE;
361 else {
362 adapter->pcix_82544 = FALSE;
364 INIT_DEBUGOUT("em_attach: end");
365 splx(s);
366 return(error);
369 err_mac_addr:
370 err_hw_init:
371 contigfree(adapter->rx_desc_base, rsize, M_DEVBUF);
372 err_rx_desc:
373 contigfree(adapter->tx_desc_base, tsize, M_DEVBUF);
374 err_tx_desc:
375 err_pci:
376 em_free_pci_resources(adapter);
377 err_event:
378 sysctl_ctx_free(&adapter->sysctl_ctx);
379 err_sysctl:
380 splx(s);
381 return(error);
384 /*********************************************************************
385 * Device removal routine
387 * The detach entry point is called when the driver is being removed.
388 * This routine stops the adapter and deallocates all the resources
389 * that were allocated for driver operation.
391 * return 0 on success, positive on failure
392 *********************************************************************/
395 em_detach(device_t dev)
397 struct adapter * adapter = device_get_softc(dev);
398 struct ifnet *ifp = &adapter->interface_data.ac_if;
399 int s;
400 int size;
402 INIT_DEBUGOUT("em_detach: begin");
403 s = splimp();
405 adapter->in_detach = 1;
407 em_stop(adapter);
408 em_phy_hw_reset(&adapter->hw);
410 stop_event_thread(adapter);
412 #if __FreeBSD_version < 500000
413 ether_ifdetach(&adapter->interface_data.ac_if, ETHER_BPF_SUPPORTED);
414 #else
415 ether_ifdetach(&adapter->interface_data.ac_if);
416 #endif
417 em_free_pci_resources(adapter);
418 bus_generic_detach(dev);
420 size = EM_ROUNDUP(adapter->num_tx_desc *
421 sizeof(struct em_tx_desc), 4096);
423 /* Free Transmit Descriptor ring */
424 if (adapter->tx_desc_base) {
425 contigfree(adapter->tx_desc_base, size, M_DEVBUF);
426 adapter->tx_desc_base = NULL;
429 size = EM_ROUNDUP(adapter->num_rx_desc *
430 sizeof(struct em_rx_desc), 4096);
432 /* Free Receive Descriptor ring */
433 if (adapter->rx_desc_base) {
434 contigfree(adapter->rx_desc_base, size, M_DEVBUF);
435 adapter->rx_desc_base = NULL;
438 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
439 ifp->if_timer = 0;
441 adapter->sysctl_tree = NULL;
442 sysctl_ctx_free(&adapter->sysctl_ctx);
444 splx(s);
445 return(0);
448 static int
449 start_event_thread(struct adapter *adapter)
451 INIT_DEBUGOUT("start_event_thread enter");
453 adapter->event_thread = spawn_kernel_thread(event_handler, "ipro1000 event", 80, adapter);
454 adapter->event_flags = 0;
455 adapter->event_sem = create_sem(0, "ipro1000 event");
456 set_sem_owner(adapter->event_sem, B_SYSTEM_TEAM);
458 if (adapter->event_thread >= 0 && adapter->event_sem >= 0) {
459 resume_thread(adapter->event_thread);
460 INIT_DEBUGOUT("start_event_thread leave");
461 return 0;
464 ERROROUT("start_event_thread failed");
466 delete_sem(adapter->event_sem);
467 kill_thread(adapter->event_thread);
469 INIT_DEBUGOUT("start_event_thread leave");
470 return -1;
473 static void
474 stop_event_thread(struct adapter *adapter)
476 status_t thread_return_value;
478 INIT_DEBUGOUT("stop_event_thread enter");
480 delete_sem(adapter->event_sem);
481 wait_for_thread(adapter->event_thread, &thread_return_value);
482 adapter->event_thread = -1;
483 adapter->event_sem = -1;
485 INIT_DEBUGOUT("stop_event_thread leave");
489 /*********************************************************************
490 * Transmit entry point
492 * em_start is called by the stack to initiate a transmit.
493 * The driver will remain in this routine as long as there are
494 * packets to transmit and transmit resources are available.
495 * In case resources are not available stack is notified and
496 * the packet is requeued.
497 **********************************************************************/
499 static void
500 em_start(struct ifnet *ifp)
502 int s;
503 struct mbuf *m_head;
504 struct adapter *adapter = ifp->if_softc;
506 if (!adapter->link_active)
507 return;
509 s = splimp();
510 while (ifp->if_snd.ifq_head != NULL) {
512 IF_DEQUEUE(&ifp->if_snd, m_head);
514 if (m_head == NULL) break;
516 if (em_encap(adapter, m_head)) {
517 ifp->if_flags |= IFF_OACTIVE;
518 IF_PREPEND(&ifp->if_snd, m_head);
519 break;
522 /* Send a copy of the frame to the BPF listener */
523 #if __FreeBSD_version < 500000
524 if (ifp->if_bpf)
525 bpf_mtap(ifp, m_head);
526 #else
527 BPF_MTAP(ifp, m_head);
528 #endif
530 /* Set timeout in case hardware has problems transmitting */
531 ifp->if_timer = EM_TX_TIMEOUT;
534 splx(s);
535 return;
538 /*********************************************************************
539 * Ioctl entry point
541 * em_ioctl is called when the user wants to configure the
542 * interface.
544 * return 0 on success, positive on failure
545 **********************************************************************/
547 static int
548 em_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
550 int s, mask, error = 0;
551 struct ifreq *ifr = (struct ifreq *) data;
552 struct adapter * adapter = ifp->if_softc;
554 s = splimp();
556 if (adapter->in_detach) goto out;
558 switch (command) {
559 case SIOCSIFADDR:
560 case SIOCGIFADDR:
561 IOCTL_DEBUGOUT("ioctl rcv'd: SIOCxIFADDR (Get/Set Interface Addr)");
562 ether_ioctl(ifp, command, data);
563 break;
564 case SIOCSIFMTU:
565 IOCTL_DEBUGOUT("ioctl rcv'd: SIOCSIFMTU (Set Interface MTU)");
566 if (ifr->ifr_mtu > MAX_JUMBO_FRAME_SIZE - ETHER_HDR_LEN) {
567 error = EINVAL;
568 } else {
569 ifp->if_mtu = ifr->ifr_mtu;
570 adapter->hw.max_frame_size =
571 ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN;
572 em_init(adapter);
574 break;
575 case SIOCSIFFLAGS:
576 IOCTL_DEBUGOUT("ioctl rcv'd: SIOCSIFFLAGS (Set Interface Flags)");
577 if (ifp->if_flags & IFF_UP) {
578 if (!(ifp->if_flags & IFF_RUNNING)) {
579 em_init(adapter);
581 em_disable_promisc(adapter);
582 em_set_promisc(adapter);
583 } else {
584 if (ifp->if_flags & IFF_RUNNING) {
585 em_stop(adapter);
588 break;
589 case SIOCADDMULTI:
590 case SIOCDELMULTI:
591 IOCTL_DEBUGOUT("ioctl rcv'd: SIOC(ADD|DEL)MULTI");
592 if (ifp->if_flags & IFF_RUNNING) {
593 em_disable_intr(adapter);
594 em_set_multi(adapter);
595 if (adapter->hw.mac_type == em_82542_rev2_0) {
596 em_initialize_receive_unit(adapter);
598 em_enable_intr(adapter);
600 break;
601 case SIOCSIFMEDIA:
602 case SIOCGIFMEDIA:
603 IOCTL_DEBUGOUT("ioctl rcv'd: SIOCxIFMEDIA (Get/Set Interface Media)");
604 error = ifmedia_ioctl(ifp, ifr, &adapter->media, command);
605 break;
606 case SIOCSIFCAP:
607 IOCTL_DEBUGOUT("ioctl rcv'd: SIOCSIFCAP (Set Capabilities)");
608 mask = ifr->ifr_reqcap ^ ifp->if_capenable;
609 if (mask & IFCAP_HWCSUM) {
610 ifp->if_capenable ^= IFCAP_HWCSUM;
611 if (ifp->if_flags & IFF_RUNNING)
612 em_init(adapter);
614 break;
615 default:
616 IOCTL_DEBUGOUT1("ioctl received: UNKNOWN (0x%x)", (int)command);
617 error = EINVAL;
620 out:
621 splx(s);
622 return(error);
625 /*********************************************************************
626 * Watchdog entry point
628 * This routine is called whenever hardware quits transmitting.
630 **********************************************************************/
632 static void
633 em_watchdog(struct ifnet *ifp)
635 struct adapter * adapter;
636 adapter = ifp->if_softc;
638 /* If we are in this routine because of pause frames, then
639 * don't reset the hardware.
641 if (E1000_READ_REG(&adapter->hw, STATUS) & E1000_STATUS_TXOFF) {
642 ifp->if_timer = EM_TX_TIMEOUT;
643 return;
646 if (em_check_for_link(&adapter->hw))
647 dprintf("ipro1000/%d: watchdog timeout -- resetting\n", adapter->unit);
649 ifp->if_flags &= ~IFF_RUNNING;
651 em_stop(adapter);
652 em_init(adapter);
654 ifp->if_oerrors++;
655 return;
658 /*********************************************************************
659 * Init entry point
661 * This routine is used in two ways. It is used by the stack as
662 * init entry point in network interface structure. It is also used
663 * by the driver as a hw/sw initialization routine to get to a
664 * consistent state.
666 * return 0 on success, positive on failure
667 **********************************************************************/
669 static void
670 em_init(void *arg)
672 int s;
673 struct ifnet *ifp;
674 struct adapter * adapter = arg;
676 INIT_DEBUGOUT("em_init: begin");
678 s = splimp();
680 em_stop(adapter);
682 /* Get the latest mac address, User can use a LAA */
683 bcopy(adapter->interface_data.ac_enaddr, adapter->hw.mac_addr,
684 ETHER_ADDR_LEN);
686 /* Initialize the hardware */
687 if (em_hardware_init(adapter)) {
688 dprintf("ipro1000/%d: Unable to initialize the hardware\n",
689 adapter->unit);
690 splx(s);
691 return;
694 em_enable_vlans(adapter);
696 /* Prepare transmit descriptors and buffers */
697 if (em_setup_transmit_structures(adapter)) {
698 dprintf("ipro1000/%d: Could not setup transmit structures\n",
699 adapter->unit);
700 em_stop(adapter);
701 splx(s);
702 return;
704 em_initialize_transmit_unit(adapter);
706 /* Setup Multicast table */
707 em_set_multi(adapter);
709 /* Prepare receive descriptors and buffers */
710 if (em_setup_receive_structures(adapter)) {
711 dprintf("ipro1000/%d: Could not setup receive structures\n",
712 adapter->unit);
713 em_stop(adapter);
714 splx(s);
715 return;
717 em_initialize_receive_unit(adapter);
719 /* Don't loose promiscuous settings */
720 em_set_promisc(adapter);
722 ifp = &adapter->interface_data.ac_if;
723 ifp->if_flags |= IFF_RUNNING;
724 ifp->if_flags &= ~IFF_OACTIVE;
726 if (adapter->hw.mac_type >= em_82543) {
727 if (ifp->if_capenable & IFCAP_TXCSUM)
728 ifp->if_hwassist = EM_CHECKSUM_FEATURES;
729 else
730 ifp->if_hwassist = 0;
733 adapter->timer_handle = timeout(em_local_timer, adapter, 2*hz);
734 em_clear_hw_cntrs(&adapter->hw);
735 em_enable_intr(adapter);
737 /* Don't reset the phy next time init gets called */
738 adapter->hw.phy_reset_disable = TRUE;
740 splx(s);
741 return;
745 /*********************************************************************
747 * Interrupt Service routine
749 **********************************************************************/
750 static int32
751 em_intr(void *arg)
753 u_int32_t loop_cnt = EM_MAX_INTR;
754 u_int32_t reg_icr;
755 struct ifnet *ifp;
756 struct adapter *adapter = arg;
757 bool release_event_sem = false;
759 ifp = &adapter->interface_data.ac_if;
761 reg_icr = E1000_READ_REG(&adapter->hw, ICR);
762 if (!reg_icr) {
763 return B_UNHANDLED_INTERRUPT;
766 /* Link status change */
767 if (reg_icr & (E1000_ICR_RXSEQ | E1000_ICR_LSC)) {
768 atomic_or(&adapter->event_flags, EVENT_LINK_CHANGED);
769 release_event_sem = true;
772 while (loop_cnt > 0) {
773 if (ifp->if_flags & IFF_RUNNING) {
774 em_process_receive_interrupts(adapter, -1);
775 em_clean_transmit_interrupts(adapter);
777 loop_cnt--;
780 // if (ifp->if_flags & IFF_RUNNING && ifp->if_snd.ifq_head != NULL) {
781 // atomic_or(&adapter->event_flags, EVENT_RESTART_TX);
782 // release_event_sem = true;
783 // }
785 if (release_event_sem)
786 release_sem_etc(adapter->event_sem, 1, B_DO_NOT_RESCHEDULE);
788 return B_INVOKE_SCHEDULER;
791 static int32
792 event_handler(void *cookie)
794 struct adapter * adapter = cookie;
795 int32 events;
797 for (;;) {
798 if (acquire_sem_etc(adapter->event_sem, 1, B_CAN_INTERRUPT, 0) == B_BAD_SEM_ID)
799 return 0;
801 events = atomic_read(&adapter->event_flags); // read
802 atomic_and(&adapter->event_flags, ~events); // and clear
804 if (events & EVENT_LINK_CHANGED) {
805 INIT_DEBUGOUT("EVENT_LINK_CHANGED");
806 untimeout(em_local_timer, adapter, adapter->timer_handle);
807 adapter->hw.get_link_status = 1;
808 em_check_for_link(&adapter->hw);
809 em_print_link_status(adapter);
810 adapter->timer_handle = timeout(em_local_timer, adapter, 2*hz);
812 #ifdef HAIKU_TARGET_PLATFORM_HAIKU
813 if (adapter->osdep.dev->linkChangeSem != -1)
814 release_sem_etc(adapter->osdep.dev->linkChangeSem, 1,
815 B_DO_NOT_RESCHEDULE);
816 #endif
823 /*********************************************************************
825 * Media Ioctl callback
827 * This routine is called whenever the user queries the status of
828 * the interface using ifconfig.
830 **********************************************************************/
831 void
832 em_media_status(struct ifnet *ifp, struct ifmediareq *ifmr)
834 struct adapter * adapter = ifp->if_softc;
836 INIT_DEBUGOUT("em_media_status: begin");
838 em_check_for_link(&adapter->hw);
839 if (E1000_READ_REG(&adapter->hw, STATUS) & E1000_STATUS_LU) {
840 if (adapter->link_active == 0) {
841 em_get_speed_and_duplex(&adapter->hw,
842 &adapter->link_speed,
843 &adapter->link_duplex);
844 adapter->link_active = 1;
846 } else {
847 if (adapter->link_active == 1) {
848 adapter->link_speed = 0;
849 adapter->link_duplex = 0;
850 adapter->link_active = 0;
854 ifmr->ifm_status = IFM_AVALID;
855 ifmr->ifm_active = IFM_ETHER;
857 if (!adapter->link_active)
858 return;
860 ifmr->ifm_status |= IFM_ACTIVE;
862 if (adapter->hw.media_type == em_media_type_fiber) {
863 ifmr->ifm_active |= IFM_1000_SX | IFM_FDX;
864 } else {
865 switch (adapter->link_speed) {
866 case 10:
867 ifmr->ifm_active |= IFM_10_T;
868 break;
869 case 100:
870 ifmr->ifm_active |= IFM_100_TX;
871 break;
872 case 1000:
873 #if __FreeBSD_version < 500000
874 ifmr->ifm_active |= IFM_1000_TX;
875 #else
876 ifmr->ifm_active |= IFM_1000_T;
877 #endif
878 break;
880 if (adapter->link_duplex == FULL_DUPLEX)
881 ifmr->ifm_active |= IFM_FDX;
882 else
883 ifmr->ifm_active |= IFM_HDX;
885 return;
888 #if 0
889 /*********************************************************************
891 * Media Ioctl callback
893 * This routine is called when the user changes speed/duplex using
894 * media/mediopt option with ifconfig.
896 **********************************************************************/
897 static int
898 em_media_change(struct ifnet *ifp)
900 struct adapter * adapter = ifp->if_softc;
901 struct ifmedia *ifm = &adapter->media;
903 INIT_DEBUGOUT("em_media_change: begin");
905 if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
906 return(EINVAL);
908 switch (IFM_SUBTYPE(ifm->ifm_media)) {
909 case IFM_AUTO:
910 adapter->hw.autoneg = DO_AUTO_NEG;
911 adapter->hw.autoneg_advertised = AUTONEG_ADV_DEFAULT;
912 break;
913 case IFM_1000_SX:
914 #if __FreeBSD_version < 500000
915 case IFM_1000_TX:
916 #else
917 case IFM_1000_T:
918 #endif
919 adapter->hw.autoneg = DO_AUTO_NEG;
920 adapter->hw.autoneg_advertised = ADVERTISE_1000_FULL;
921 break;
922 case IFM_100_TX:
923 adapter->hw.autoneg = FALSE;
924 adapter->hw.autoneg_advertised = 0;
925 if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX)
926 adapter->hw.forced_speed_duplex = em_100_full;
927 else
928 adapter->hw.forced_speed_duplex = em_100_half;
929 break;
930 case IFM_10_T:
931 adapter->hw.autoneg = FALSE;
932 adapter->hw.autoneg_advertised = 0;
933 if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX)
934 adapter->hw.forced_speed_duplex = em_10_full;
935 else
936 adapter->hw.forced_speed_duplex = em_10_half;
937 break;
938 default:
939 dprintf("ipro1000/%d: Unsupported media type\n", adapter->unit);
942 /* As the speed/duplex settings my have changed we nee to
943 * reset the PHY.
945 adapter->hw.phy_reset_disable = FALSE;
947 em_init(adapter);
949 return(0);
951 #endif // #if 0
953 #define EM_FIFO_HDR 0x10
954 #define EM_82547_PKT_THRESH 0x3e0
955 #define EM_82547_TX_FIFO_SIZE 0x2800
956 #define EM_82547_TX_FIFO_BEGIN 0xf00
957 /*********************************************************************
959 * This routine maps the mbufs to tx descriptors.
961 * return 0 on success, positive on failure
962 **********************************************************************/
964 static int
965 em_encap(struct adapter *adapter, struct mbuf *m_head)
967 vm_offset_t virtual_addr;
968 u_int32_t txd_upper;
969 u_int32_t txd_lower;
970 int txd_used, i, txd_saved;
971 struct mbuf *mp;
972 u_int64_t address;
974 /* For 82544 Workaround */
975 DESC_ARRAY desc_array;
976 u_int32_t array_elements;
977 u_int32_t counter;
979 #if __FreeBSD_version < 500000
980 struct ifvlan *ifv = NULL;
981 #else
982 struct m_tag *mtag;
983 #endif
984 struct em_buffer *tx_buffer = NULL;
985 struct em_tx_desc *current_tx_desc = NULL;
986 struct ifnet *ifp = &adapter->interface_data.ac_if;
989 * Force a cleanup if number of TX descriptors
990 * available hits the threshold
992 if (adapter->num_tx_desc_avail <= EM_TX_CLEANUP_THRESHOLD)
993 em_clean_transmit_interrupts(adapter);
995 if (adapter->num_tx_desc_avail <= EM_TX_CLEANUP_THRESHOLD) {
996 adapter->no_tx_desc_avail1++;
997 return (ENOBUFS);
1000 if (ifp->if_hwassist > 0) {
1001 em_transmit_checksum_setup(adapter, m_head,
1002 &txd_upper, &txd_lower);
1004 else
1005 txd_upper = txd_lower = 0;
1008 /* Find out if we are in vlan mode */
1009 #if __FreeBSD_version < 500000
1010 if ((m_head->m_flags & (M_PROTO1|M_PKTHDR)) == (M_PROTO1|M_PKTHDR) &&
1011 m_head->m_pkthdr.rcvif != NULL &&
1012 m_head->m_pkthdr.rcvif->if_type == IFT_L2VLAN)
1013 ifv = m_head->m_pkthdr.rcvif->if_softc;
1014 #else
1015 mtag = VLAN_OUTPUT_TAG(ifp, m_head);
1016 #endif
1018 i = adapter->next_avail_tx_desc;
1019 txd_saved = i;
1020 txd_used = 0;
1022 for (mp = m_head; mp != NULL; mp = mp->m_next) {
1023 if (mp->m_len == 0)
1024 continue;
1025 /* If adapter is 82544 and on PCIX bus */
1026 if(adapter->pcix_82544) {
1027 array_elements = 0;
1028 virtual_addr= mtod(mp, vm_offset_t);
1029 address = vtophys(virtual_addr);
1030 /* Check the Address and Length combination and split the data accordingly */
1031 array_elements = em_fill_descriptors(
1032 address,
1033 mp->m_len,
1034 &desc_array);
1036 for (counter = 0; counter < array_elements; counter++) {
1037 if (txd_used == adapter->num_tx_desc_avail) {
1038 adapter->next_avail_tx_desc = txd_saved;
1039 adapter->no_tx_desc_avail2++;
1040 return (ENOBUFS);
1043 tx_buffer = &adapter->tx_buffer_area[i];
1044 current_tx_desc = &adapter->tx_desc_base[i];
1045 /* Put in the buffer address*/
1046 current_tx_desc->buffer_addr = desc_array.descriptor[counter].address;
1047 /* Put in the length */
1048 current_tx_desc->lower.data = (adapter->txd_cmd | txd_lower
1049 | (u_int16_t)desc_array.descriptor[counter].length);
1050 current_tx_desc->upper.data = (txd_upper);
1051 if (++i == adapter->num_tx_desc)
1052 i = 0;
1053 tx_buffer->m_head = NULL;
1054 txd_used++;
1057 else {
1058 if (txd_used == adapter->num_tx_desc_avail) {
1059 adapter->next_avail_tx_desc = txd_saved;
1060 adapter->no_tx_desc_avail2++;
1061 return (ENOBUFS);
1064 tx_buffer = &adapter->tx_buffer_area[i];
1065 current_tx_desc = &adapter->tx_desc_base[i];
1066 virtual_addr = mtod(mp, vm_offset_t);
1067 current_tx_desc->buffer_addr = vtophys(virtual_addr);
1068 current_tx_desc->lower.data = (adapter->txd_cmd | txd_lower | mp->m_len);
1069 current_tx_desc->upper.data = (txd_upper);
1071 if (++i == adapter->num_tx_desc)
1072 i = 0;
1074 tx_buffer->m_head = NULL;
1076 txd_used++;
1079 adapter->num_tx_desc_avail -= txd_used;
1080 adapter->next_avail_tx_desc = i;
1082 #if __FreeBSD_version < 500000
1083 if (ifv != NULL) {
1084 /* Set the vlan id */
1085 current_tx_desc->upper.fields.special = ifv->ifv_tag;
1086 #else
1087 if (mtag != NULL) {
1088 /* Set the vlan id */
1089 current_tx_desc->upper.fields.special = VLAN_TAG_VALUE(mtag);
1090 #endif
1091 /* Tell hardware to add tag */
1092 current_tx_desc->lower.data |= E1000_TXD_CMD_VLE;
1095 tx_buffer->m_head = m_head;
1098 * Last Descriptor of Packet needs End Of Packet (EOP)
1100 current_tx_desc->lower.data |= (E1000_TXD_CMD_EOP);
1103 * Advance the Transmit Descriptor Tail (Tdt), this tells the E1000
1104 * that this frame is available to transmit.
1106 if (adapter->hw.mac_type == em_82547 &&
1107 adapter->link_duplex == HALF_DUPLEX) {
1108 em_82547_move_tail(adapter);
1110 else {
1111 E1000_WRITE_REG(&adapter->hw, TDT, i);
1112 if (adapter->hw.mac_type == em_82547) {
1113 em_82547_update_fifo_head(adapter, m_head->m_pkthdr.len);
1117 return (0);
1121 /*********************************************************************
1123 * 82547 workaround to avoid controller hang in half-duplex environment.
1124 * The workaround is to avoid queuing a large packet that would span
1125 * the internal Tx FIFO ring boundary. We need to reset the FIFO pointers
1126 * in this case. We do that only when FIFO is queiced.
1128 **********************************************************************/
1129 static void
1130 em_82547_move_tail(void *arg)
1132 int s;
1133 struct adapter *adapter = arg;
1134 uint16_t hw_tdt;
1135 uint16_t sw_tdt;
1136 struct em_tx_desc *tx_desc;
1137 uint16_t length = 0;
1138 boolean_t eop = 0;
1140 s = splimp();
1141 hw_tdt = E1000_READ_REG(&adapter->hw, TDT);
1142 sw_tdt = adapter->next_avail_tx_desc;
1144 while (hw_tdt != sw_tdt) {
1145 tx_desc = &adapter->tx_desc_base[hw_tdt];
1146 length += tx_desc->lower.flags.length;
1147 eop = tx_desc->lower.data & E1000_TXD_CMD_EOP;
1148 if(++hw_tdt == adapter->num_tx_desc)
1149 hw_tdt = 0;
1151 if(eop) {
1152 if (em_82547_fifo_workaround(adapter, length)) {
1153 adapter->tx_fifo_wrk++;
1154 adapter->tx_fifo_timer_handle =
1155 timeout(em_82547_move_tail,
1156 adapter, 100);
1157 splx(s);
1158 return;
1160 else {
1161 E1000_WRITE_REG(&adapter->hw, TDT, hw_tdt);
1162 em_82547_update_fifo_head(adapter, length);
1163 length = 0;
1167 splx(s);
1168 return;
1171 static int
1172 em_82547_fifo_workaround(struct adapter *adapter, int len)
1174 int fifo_space, fifo_pkt_len;
1176 fifo_pkt_len = EM_ROUNDUP(len + EM_FIFO_HDR, EM_FIFO_HDR);
1178 if (adapter->link_duplex == HALF_DUPLEX) {
1179 fifo_space = EM_82547_TX_FIFO_SIZE - adapter->tx_fifo_head;
1181 if (fifo_pkt_len >= (EM_82547_PKT_THRESH + fifo_space)) {
1182 if (em_82547_tx_fifo_reset(adapter)) {
1183 return(0);
1185 else {
1186 return(1);
1191 return(0);
1194 static void
1195 em_82547_update_fifo_head(struct adapter *adapter, int len)
1197 int fifo_pkt_len = EM_ROUNDUP(len + EM_FIFO_HDR, EM_FIFO_HDR);
1199 /* tx_fifo_head is always 16 byte aligned */
1200 adapter->tx_fifo_head += fifo_pkt_len;
1201 if (adapter->tx_fifo_head >= EM_82547_TX_FIFO_SIZE) {
1202 adapter->tx_fifo_head -= EM_82547_TX_FIFO_SIZE;
1205 return;
1209 static int
1210 em_82547_tx_fifo_reset(struct adapter *adapter)
1212 uint32_t tctl;
1214 if ( (E1000_READ_REG(&adapter->hw, TDT) ==
1215 E1000_READ_REG(&adapter->hw, TDH)) &&
1216 (E1000_READ_REG(&adapter->hw, TDFT) ==
1217 E1000_READ_REG(&adapter->hw, TDFH)) &&
1218 (E1000_READ_REG(&adapter->hw, TDFTS) ==
1219 E1000_READ_REG(&adapter->hw, TDFHS)) &&
1220 (E1000_READ_REG(&adapter->hw, TDFPC) == 0)) {
1222 /* Disable TX unit */
1223 tctl = E1000_READ_REG(&adapter->hw, TCTL);
1224 E1000_WRITE_REG(&adapter->hw, TCTL, tctl & ~E1000_TCTL_EN);
1226 /* Reset FIFO pointers */
1227 E1000_WRITE_REG(&adapter->hw, TDFT, EM_82547_TX_FIFO_BEGIN);
1228 E1000_WRITE_REG(&adapter->hw, TDFH, EM_82547_TX_FIFO_BEGIN);
1229 E1000_WRITE_REG(&adapter->hw, TDFTS, EM_82547_TX_FIFO_BEGIN);
1230 E1000_WRITE_REG(&adapter->hw, TDFHS, EM_82547_TX_FIFO_BEGIN);
1232 /* Re-enable TX unit */
1233 E1000_WRITE_REG(&adapter->hw, TCTL, tctl);
1234 E1000_WRITE_FLUSH(&adapter->hw);
1236 adapter->tx_fifo_head = 0;
1237 adapter->tx_fifo_reset++;
1239 return(TRUE);
1241 else {
1242 return(FALSE);
1246 static void
1247 em_set_promisc(struct adapter * adapter)
1250 u_int32_t reg_rctl;
1251 struct ifnet *ifp = &adapter->interface_data.ac_if;
1253 reg_rctl = E1000_READ_REG(&adapter->hw, RCTL);
1255 if (ifp->if_flags & IFF_PROMISC) {
1256 reg_rctl |= (E1000_RCTL_UPE | E1000_RCTL_MPE);
1257 E1000_WRITE_REG(&adapter->hw, RCTL, reg_rctl);
1258 } else if (ifp->if_flags & IFF_ALLMULTI) {
1259 reg_rctl |= E1000_RCTL_MPE;
1260 reg_rctl &= ~E1000_RCTL_UPE;
1261 E1000_WRITE_REG(&adapter->hw, RCTL, reg_rctl);
1264 return;
1267 static void
1268 em_disable_promisc(struct adapter * adapter)
1270 u_int32_t reg_rctl;
1272 reg_rctl = E1000_READ_REG(&adapter->hw, RCTL);
1274 reg_rctl &= (~E1000_RCTL_UPE);
1275 reg_rctl &= (~E1000_RCTL_MPE);
1276 E1000_WRITE_REG(&adapter->hw, RCTL, reg_rctl);
1278 return;
1282 /*********************************************************************
1283 * Multicast Update
1285 * This routine is called whenever multicast address list is updated.
1287 **********************************************************************/
1289 static void
1290 em_set_multi(struct adapter * adapter)
1292 u_int32_t reg_rctl = 0;
1293 u_int8_t mta[MAX_NUM_MULTICAST_ADDRESSES * ETH_LENGTH_OF_ADDRESS];
1294 struct ifmultiaddr *ifma;
1295 int mcnt = 0;
1296 struct ifnet *ifp = &adapter->interface_data.ac_if;
1298 IOCTL_DEBUGOUT("em_set_multi: begin");
1300 if (adapter->hw.mac_type == em_82542_rev2_0) {
1301 reg_rctl = E1000_READ_REG(&adapter->hw, RCTL);
1302 if (adapter->hw.pci_cmd_word & CMD_MEM_WRT_INVALIDATE) {
1303 em_pci_clear_mwi(&adapter->hw);
1305 reg_rctl |= E1000_RCTL_RST;
1306 E1000_WRITE_REG(&adapter->hw, RCTL, reg_rctl);
1307 msec_delay(5);
1310 #if __FreeBSD_version < 500000
1311 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1312 #else
1313 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1314 #endif
1315 if (ifma->ifma_addr->sa_family != AF_LINK)
1316 continue;
1318 if (mcnt == MAX_NUM_MULTICAST_ADDRESSES) break;
1320 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
1321 &mta[mcnt*ETH_LENGTH_OF_ADDRESS], ETH_LENGTH_OF_ADDRESS);
1322 mcnt++;
1325 if (mcnt >= MAX_NUM_MULTICAST_ADDRESSES) {
1326 reg_rctl = E1000_READ_REG(&adapter->hw, RCTL);
1327 reg_rctl |= E1000_RCTL_MPE;
1328 E1000_WRITE_REG(&adapter->hw, RCTL, reg_rctl);
1329 } else
1330 em_mc_addr_list_update(&adapter->hw, mta, mcnt, 0, 1);
1332 if (adapter->hw.mac_type == em_82542_rev2_0) {
1333 reg_rctl = E1000_READ_REG(&adapter->hw, RCTL);
1334 reg_rctl &= ~E1000_RCTL_RST;
1335 E1000_WRITE_REG(&adapter->hw, RCTL, reg_rctl);
1336 msec_delay(5);
1337 if (adapter->hw.pci_cmd_word & CMD_MEM_WRT_INVALIDATE) {
1338 em_pci_set_mwi(&adapter->hw);
1342 return;
1346 /*********************************************************************
1347 * Timer routine
1349 * This routine checks for link status and updates statistics.
1351 **********************************************************************/
1353 static void
1354 em_local_timer(void *arg)
1356 int s;
1357 struct ifnet *ifp;
1358 struct adapter * adapter = arg;
1359 ifp = &adapter->interface_data.ac_if;
1361 s = splimp();
1363 em_check_for_link(&adapter->hw);
1364 em_print_link_status(adapter);
1365 em_update_stats_counters(adapter);
1366 if (DEBUG_DISPLAY_STATS && ifp->if_flags & IFF_RUNNING) {
1367 em_print_hw_stats(adapter);
1368 em_print_debug_info(adapter);
1370 em_smartspeed(adapter);
1372 adapter->timer_handle = timeout(em_local_timer, adapter, 2*hz);
1374 splx(s);
1375 return;
1378 static void
1379 em_print_link_status(struct adapter * adapter)
1381 if (E1000_READ_REG(&adapter->hw, STATUS) & E1000_STATUS_LU) {
1382 if (adapter->link_active == 0) {
1383 em_get_speed_and_duplex(&adapter->hw,
1384 &adapter->link_speed,
1385 &adapter->link_duplex);
1386 dprintf("ipro1000/%d: Link is up %d Mbps %s\n",
1387 adapter->unit,
1388 adapter->link_speed,
1389 ((adapter->link_duplex == FULL_DUPLEX) ?
1390 "Full Duplex" : "Half Duplex"));
1391 adapter->link_active = 1;
1392 adapter->smartspeed = 0;
1394 } else {
1395 if (adapter->link_active == 1) {
1396 adapter->link_speed = 0;
1397 adapter->link_duplex = 0;
1398 dprintf("ipro1000/%d: Link is Down\n", adapter->unit);
1399 adapter->link_active = 0;
1403 return;
1406 /*********************************************************************
1408 * This routine disables all traffic on the adapter by issuing a
1409 * global reset on the MAC and deallocates TX/RX buffers.
1411 **********************************************************************/
1413 static void
1414 em_stop(void *arg)
1416 struct ifnet *ifp;
1417 struct adapter * adapter = arg;
1418 ifp = &adapter->interface_data.ac_if;
1420 INIT_DEBUGOUT("em_stop: begin");
1421 em_disable_intr(adapter);
1422 em_reset_hw(&adapter->hw);
1423 untimeout(em_local_timer, adapter, adapter->timer_handle);
1424 untimeout(em_82547_move_tail, adapter,
1425 adapter->tx_fifo_timer_handle);
1426 em_free_transmit_structures(adapter);
1427 em_free_receive_structures(adapter);
1430 /* Tell the stack that the interface is no longer active */
1431 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1433 return;
1437 /*********************************************************************
1439 * Determine hardware revision.
1441 **********************************************************************/
1442 static void
1443 em_identify_hardware(struct adapter * adapter)
1445 device_t dev = adapter->dev;
1447 /* Make sure our PCI config space has the necessary stuff set */
1448 adapter->hw.pci_cmd_word = pci_read_config(dev, PCIR_COMMAND, 2);
1449 if (!((adapter->hw.pci_cmd_word & PCIM_CMD_BUSMASTEREN) &&
1450 (adapter->hw.pci_cmd_word & PCIM_CMD_MEMEN))) {
1451 dprintf("ipro1000/%d: Memory Access and/or Bus Master bits were not set!\n",
1452 adapter->unit);
1453 adapter->hw.pci_cmd_word |=
1454 (PCIM_CMD_BUSMASTEREN | PCIM_CMD_MEMEN);
1455 pci_write_config(dev, PCIR_COMMAND, adapter->hw.pci_cmd_word, 2);
1458 /* Save off the information about this board */
1459 adapter->hw.vendor_id = pci_get_vendor(dev);
1460 adapter->hw.device_id = pci_get_device(dev);
1461 adapter->hw.revision_id = pci_read_config(dev, PCIR_REVID, 1);
1462 adapter->hw.subsystem_vendor_id = pci_read_config(dev, PCIR_SUBVEND_0, 2);
1463 adapter->hw.subsystem_id = pci_read_config(dev, PCIR_SUBDEV_0, 2);
1465 /* Identify the MAC */
1466 if (em_set_mac_type(&adapter->hw))
1467 dprintf("ipro1000/%d: Unknown MAC Type\n", adapter->unit);
1469 if(adapter->hw.mac_type == em_82541 || adapter->hw.mac_type == em_82541_rev_2 ||
1470 adapter->hw.mac_type == em_82547 || adapter->hw.mac_type == em_82547_rev_2)
1471 adapter->hw.phy_init_script = TRUE;
1474 return;
1477 static int
1478 em_allocate_pci_resources(struct adapter * adapter)
1480 int i, val, rid;
1481 device_t dev = adapter->dev;
1483 rid = EM_MMBA;
1484 adapter->res_memory = bus_alloc_resource(dev, SYS_RES_MEMORY,
1485 &rid, 0, ~0, 1,
1486 RF_ACTIVE);
1487 if (!(adapter->res_memory)) {
1488 dprintf("ipro1000/%d: Unable to allocate bus resource: memory\n",
1489 adapter->unit);
1490 return(ENXIO);
1493 dev->regAddr = adapter->res_memory;
1495 if (adapter->hw.mac_type > em_82543) {
1496 /* Enable IO space access, added for BeOS */
1497 if (!(adapter->hw.pci_cmd_word & PCIM_CMD_IOEN)) {
1498 dprintf("ipro1000/%d: IO Access bit was not set!\n",
1499 adapter->unit);
1500 adapter->hw.pci_cmd_word |= PCIM_CMD_IOEN;
1501 pci_write_config(dev, PCIR_COMMAND, adapter->hw.pci_cmd_word, 2);
1504 /* Figure our where our IO BAR is ? */
1505 rid = EM_MMBA;
1506 for (i = 0; i < 5; i++) {
1507 val = pci_read_config(dev, rid, 4);
1508 if (val & 0x00000001) {
1509 adapter->io_rid = rid;
1510 break;
1512 rid += 4;
1515 adapter->res_ioport = bus_alloc_resource(dev, SYS_RES_IOPORT,
1516 &adapter->io_rid, 0, ~0, 1,
1517 RF_ACTIVE);
1518 if (!(adapter->res_ioport)) {
1519 dprintf("ipro1000/%d: Unable to allocate bus resource: ioport\n",
1520 adapter->unit);
1521 return(ENXIO);
1524 adapter->hw.io_base =
1525 rman_get_start(adapter->res_ioport);
1528 rid = 0x0;
1529 adapter->res_interrupt = bus_alloc_resource(dev, SYS_RES_IRQ,
1530 &rid, 0, ~0, 1,
1531 RF_SHAREABLE | RF_ACTIVE);
1532 if (!(adapter->res_interrupt)) {
1533 dprintf("ipro1000/%d: Unable to allocate bus resource: interrupt\n",
1534 adapter->unit);
1535 return(ENXIO);
1538 adapter->hw.back = &adapter->osdep;
1540 if (bus_setup_intr(dev, adapter->res_interrupt, INTR_TYPE_NET,
1541 em_intr, adapter,
1542 &adapter->int_handler_tag)) {
1543 dprintf("ipro1000/%d: Error registering interrupt handler!\n",
1544 adapter->unit);
1545 return(ENXIO);
1548 return 0;
1551 static void
1552 em_free_pci_resources(struct adapter * adapter)
1554 device_t dev = adapter->dev;
1556 if (adapter->res_interrupt != NULL) {
1557 bus_teardown_intr(dev, adapter->res_interrupt,
1558 adapter->int_handler_tag);
1559 bus_release_resource(dev, SYS_RES_IRQ, 0,
1560 adapter->res_interrupt);
1562 if (adapter->res_memory != NULL) {
1563 bus_release_resource(dev, SYS_RES_MEMORY, EM_MMBA,
1564 adapter->res_memory);
1567 if (adapter->res_ioport != NULL) {
1568 bus_release_resource(dev, SYS_RES_IOPORT, adapter->io_rid,
1569 adapter->res_ioport);
1571 return;
1574 /*********************************************************************
1576 * Initialize the hardware to a configuration as specified by the
1577 * adapter structure. The controller is reset, the EEPROM is
1578 * verified, the MAC address is set, then the shared initialization
1579 * routines are called.
1581 **********************************************************************/
1582 static int
1583 em_hardware_init(struct adapter * adapter)
1585 INIT_DEBUGOUT("em_hardware_init: begin");
1586 /* Issue a global reset */
1587 em_reset_hw(&adapter->hw);
1589 /* When hardware is reset, fifo_head is also reset */
1590 adapter->tx_fifo_head = 0;
1592 /* Make sure we have a good EEPROM before we read from it */
1593 if (em_validate_eeprom_checksum(&adapter->hw) < 0) {
1594 dprintf("ipro1000/%d: The EEPROM Checksum Is Not Valid\n",
1595 adapter->unit);
1596 return(EIO);
1599 if (em_read_part_num(&adapter->hw, &(adapter->part_num)) < 0) {
1600 dprintf("ipro1000/%d: EEPROM read error while reading part number\n",
1601 adapter->unit);
1602 return(EIO);
1605 if (em_init_hw(&adapter->hw) < 0) {
1606 dprintf("ipro1000/%d: Hardware Initialization Failed",
1607 adapter->unit);
1608 return(EIO);
1611 em_check_for_link(&adapter->hw);
1612 if (E1000_READ_REG(&adapter->hw, STATUS) & E1000_STATUS_LU)
1613 adapter->link_active = 1;
1614 else
1615 adapter->link_active = 0;
1617 if (adapter->link_active) {
1618 em_get_speed_and_duplex(&adapter->hw,
1619 &adapter->link_speed,
1620 &adapter->link_duplex);
1621 } else {
1622 adapter->link_speed = 0;
1623 adapter->link_duplex = 0;
1626 return(0);
1629 /*********************************************************************
1631 * Setup networking device structure and register an interface.
1633 **********************************************************************/
1634 static void
1635 em_setup_interface(device_t dev, struct adapter * adapter)
1637 struct ifnet *ifp;
1638 INIT_DEBUGOUT("em_setup_interface: begin");
1640 ifp = &adapter->interface_data.ac_if;
1641 ifp->if_unit = adapter->unit;
1642 ifp->if_name = "em";
1643 ifp->if_mtu = ETHERMTU;
1644 ifp->if_output = ether_output;
1645 ifp->if_baudrate = 1000000000;
1646 ifp->if_init = em_init;
1647 ifp->if_softc = adapter;
1648 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1649 ifp->if_ioctl = em_ioctl;
1650 ifp->if_start = em_start;
1651 ifp->if_watchdog = em_watchdog;
1652 ifp->if_snd.ifq_maxlen = adapter->num_tx_desc - 1;
1654 #if __FreeBSD_version < 500000
1655 ether_ifattach(ifp, ETHER_BPF_SUPPORTED);
1656 #else
1657 ether_ifattach(ifp, adapter->interface_data.ac_enaddr);
1658 #endif
1660 if (adapter->hw.mac_type >= em_82543) {
1661 ifp->if_capabilities = IFCAP_HWCSUM;
1662 ifp->if_capenable = ifp->if_capabilities;
1666 * Tell the upper layer(s) we support long frames.
1668 ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
1669 #if __FreeBSD_version >= 500000
1670 ifp->if_capabilities |= IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_MTU;
1671 #endif
1674 * Specify the media types supported by this adapter and register
1675 * callbacks to update media and link information
1677 ifmedia_init(&adapter->media, IFM_IMASK, em_media_change,
1678 em_media_status);
1679 if (adapter->hw.media_type == em_media_type_fiber) {
1680 ifmedia_add(&adapter->media, IFM_ETHER | IFM_1000_SX | IFM_FDX,
1681 0, NULL);
1682 ifmedia_add(&adapter->media, IFM_ETHER | IFM_1000_SX,
1683 0, NULL);
1684 } else {
1685 ifmedia_add(&adapter->media, IFM_ETHER | IFM_10_T, 0, NULL);
1686 ifmedia_add(&adapter->media, IFM_ETHER | IFM_10_T | IFM_FDX,
1687 0, NULL);
1688 ifmedia_add(&adapter->media, IFM_ETHER | IFM_100_TX,
1689 0, NULL);
1690 ifmedia_add(&adapter->media, IFM_ETHER | IFM_100_TX | IFM_FDX,
1691 0, NULL);
1692 #if __FreeBSD_version < 500000
1693 ifmedia_add(&adapter->media, IFM_ETHER | IFM_1000_TX | IFM_FDX,
1694 0, NULL);
1695 ifmedia_add(&adapter->media, IFM_ETHER | IFM_1000_TX, 0, NULL);
1696 #else
1697 ifmedia_add(&adapter->media, IFM_ETHER | IFM_1000_T | IFM_FDX,
1698 0, NULL);
1699 ifmedia_add(&adapter->media, IFM_ETHER | IFM_1000_T, 0, NULL);
1700 #endif
1702 ifmedia_add(&adapter->media, IFM_ETHER | IFM_AUTO, 0, NULL);
1703 ifmedia_set(&adapter->media, IFM_ETHER | IFM_AUTO);
1705 return;
1709 /*********************************************************************
1711 * Workaround for SmartSpeed on 82541 and 82547 controllers
1713 **********************************************************************/
1714 static void
1715 em_smartspeed(struct adapter *adapter)
1717 uint16_t phy_tmp;
1719 if(adapter->link_active || (adapter->hw.phy_type != em_phy_igp) ||
1720 !adapter->hw.autoneg || !(adapter->hw.autoneg_advertised & ADVERTISE_1000_FULL))
1721 return;
1723 if(adapter->smartspeed == 0) {
1724 /* If Master/Slave config fault is asserted twice,
1725 * we assume back-to-back */
1726 em_read_phy_reg(&adapter->hw, PHY_1000T_STATUS, &phy_tmp);
1727 if(!(phy_tmp & SR_1000T_MS_CONFIG_FAULT)) return;
1728 em_read_phy_reg(&adapter->hw, PHY_1000T_STATUS, &phy_tmp);
1729 if(phy_tmp & SR_1000T_MS_CONFIG_FAULT) {
1730 em_read_phy_reg(&adapter->hw, PHY_1000T_CTRL,
1731 &phy_tmp);
1732 if(phy_tmp & CR_1000T_MS_ENABLE) {
1733 phy_tmp &= ~CR_1000T_MS_ENABLE;
1734 em_write_phy_reg(&adapter->hw,
1735 PHY_1000T_CTRL, phy_tmp);
1736 adapter->smartspeed++;
1737 if(adapter->hw.autoneg &&
1738 !em_phy_setup_autoneg(&adapter->hw) &&
1739 !em_read_phy_reg(&adapter->hw, PHY_CTRL,
1740 &phy_tmp)) {
1741 phy_tmp |= (MII_CR_AUTO_NEG_EN |
1742 MII_CR_RESTART_AUTO_NEG);
1743 em_write_phy_reg(&adapter->hw,
1744 PHY_CTRL, phy_tmp);
1748 return;
1749 } else if(adapter->smartspeed == EM_SMARTSPEED_DOWNSHIFT) {
1750 /* If still no link, perhaps using 2/3 pair cable */
1751 em_read_phy_reg(&adapter->hw, PHY_1000T_CTRL, &phy_tmp);
1752 phy_tmp |= CR_1000T_MS_ENABLE;
1753 em_write_phy_reg(&adapter->hw, PHY_1000T_CTRL, phy_tmp);
1754 if(adapter->hw.autoneg &&
1755 !em_phy_setup_autoneg(&adapter->hw) &&
1756 !em_read_phy_reg(&adapter->hw, PHY_CTRL, &phy_tmp)) {
1757 phy_tmp |= (MII_CR_AUTO_NEG_EN |
1758 MII_CR_RESTART_AUTO_NEG);
1759 em_write_phy_reg(&adapter->hw, PHY_CTRL, phy_tmp);
1762 /* Restart process after EM_SMARTSPEED_MAX iterations */
1763 if(adapter->smartspeed++ == EM_SMARTSPEED_MAX)
1764 adapter->smartspeed = 0;
1766 return;
1770 /*********************************************************************
1772 * Allocate memory for tx_buffer structures. The tx_buffer stores all
1773 * the information needed to transmit a packet on the wire.
1775 **********************************************************************/
1776 static int
1777 em_allocate_transmit_structures(struct adapter * adapter)
1779 if (!(adapter->tx_buffer_area =
1780 (struct em_buffer *) malloc(sizeof(struct em_buffer) *
1781 adapter->num_tx_desc, M_DEVBUF,
1782 M_NOWAIT))) {
1783 dprintf("ipro1000/%d: Unable to allocate tx_buffer memory\n",
1784 adapter->unit);
1785 return ENOMEM;
1788 bzero(adapter->tx_buffer_area,
1789 sizeof(struct em_buffer) * adapter->num_tx_desc);
1791 return 0;
1794 /*********************************************************************
1796 * Allocate and initialize transmit structures.
1798 **********************************************************************/
1799 static int
1800 em_setup_transmit_structures(struct adapter * adapter)
1802 if (em_allocate_transmit_structures(adapter))
1803 return ENOMEM;
1805 bzero((void *) adapter->tx_desc_base,
1806 (sizeof(struct em_tx_desc)) * adapter->num_tx_desc);
1808 adapter->next_avail_tx_desc = 0;
1809 adapter->oldest_used_tx_desc = 0;
1811 /* Set number of descriptors available */
1812 adapter->num_tx_desc_avail = adapter->num_tx_desc;
1814 /* Set checksum context */
1815 adapter->active_checksum_context = OFFLOAD_NONE;
1817 return 0;
1820 /*********************************************************************
1822 * Enable transmit unit.
1824 **********************************************************************/
1825 static void
1826 em_initialize_transmit_unit(struct adapter * adapter)
1828 u_int32_t reg_tctl;
1829 u_int32_t reg_tipg = 0;
1830 u_int64_t tdba = vtophys((vm_offset_t)adapter->tx_desc_base);
1832 INIT_DEBUGOUT("em_initialize_transmit_unit: begin");
1834 /* Setup the Base and Length of the Tx Descriptor Ring */
1835 E1000_WRITE_REG(&adapter->hw, TDBAL,
1836 (tdba & 0x00000000ffffffffULL));
1837 E1000_WRITE_REG(&adapter->hw, TDBAH, (tdba >> 32));
1838 E1000_WRITE_REG(&adapter->hw, TDLEN,
1839 adapter->num_tx_desc *
1840 sizeof(struct em_tx_desc));
1842 /* Setup the HW Tx Head and Tail descriptor pointers */
1843 E1000_WRITE_REG(&adapter->hw, TDH, 0);
1844 E1000_WRITE_REG(&adapter->hw, TDT, 0);
1847 HW_DEBUGOUT2("Base = %lx, Length = %lx",
1848 E1000_READ_REG(&adapter->hw, TDBAL),
1849 E1000_READ_REG(&adapter->hw, TDLEN));
1852 /* Set the default values for the Tx Inter Packet Gap timer */
1853 switch (adapter->hw.mac_type) {
1854 case em_82542_rev2_0:
1855 case em_82542_rev2_1:
1856 reg_tipg = DEFAULT_82542_TIPG_IPGT;
1857 reg_tipg |= DEFAULT_82542_TIPG_IPGR1 << E1000_TIPG_IPGR1_SHIFT;
1858 reg_tipg |= DEFAULT_82542_TIPG_IPGR2 << E1000_TIPG_IPGR2_SHIFT;
1859 break;
1860 default:
1861 if (adapter->hw.media_type == em_media_type_fiber)
1862 reg_tipg = DEFAULT_82543_TIPG_IPGT_FIBER;
1863 else
1864 reg_tipg = DEFAULT_82543_TIPG_IPGT_COPPER;
1865 reg_tipg |= DEFAULT_82543_TIPG_IPGR1 << E1000_TIPG_IPGR1_SHIFT;
1866 reg_tipg |= DEFAULT_82543_TIPG_IPGR2 << E1000_TIPG_IPGR2_SHIFT;
1869 E1000_WRITE_REG(&adapter->hw, TIPG, reg_tipg);
1870 E1000_WRITE_REG(&adapter->hw, TIDV, adapter->tx_int_delay.value);
1871 if(adapter->hw.mac_type >= em_82540)
1872 E1000_WRITE_REG(&adapter->hw, TADV, adapter->tx_abs_int_delay.value);
1874 /* Program the Transmit Control Register */
1875 reg_tctl = E1000_TCTL_PSP | E1000_TCTL_EN |
1876 (E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT);
1877 if (adapter->link_duplex == 1) {
1878 reg_tctl |= E1000_FDX_COLLISION_DISTANCE << E1000_COLD_SHIFT;
1879 } else {
1880 reg_tctl |= E1000_HDX_COLLISION_DISTANCE << E1000_COLD_SHIFT;
1882 E1000_WRITE_REG(&adapter->hw, TCTL, reg_tctl);
1884 /* Setup Transmit Descriptor Settings for this adapter */
1885 adapter->txd_cmd = E1000_TXD_CMD_IFCS | E1000_TXD_CMD_RS;
1887 if (adapter->tx_int_delay.value > 0)
1888 adapter->txd_cmd |= E1000_TXD_CMD_IDE;
1890 return;
1893 /*********************************************************************
1895 * Free all transmit related data structures.
1897 **********************************************************************/
1898 static void
1899 em_free_transmit_structures(struct adapter * adapter)
1901 struct em_buffer *tx_buffer;
1902 int i;
1904 INIT_DEBUGOUT("free_transmit_structures: begin");
1906 if (adapter->tx_buffer_area != NULL) {
1907 tx_buffer = adapter->tx_buffer_area;
1908 for (i = 0; i < adapter->num_tx_desc; i++, tx_buffer++) {
1909 if (tx_buffer->m_head != NULL)
1910 m_freem(tx_buffer->m_head);
1911 tx_buffer->m_head = NULL;
1914 if (adapter->tx_buffer_area != NULL) {
1915 free(adapter->tx_buffer_area, M_DEVBUF);
1916 adapter->tx_buffer_area = NULL;
1918 return;
1921 /*********************************************************************
1923 * The offload context needs to be set when we transfer the first
1924 * packet of a particular protocol (TCP/UDP). We change the
1925 * context only if the protocol type changes.
1927 **********************************************************************/
1928 static void
1929 em_transmit_checksum_setup(struct adapter * adapter,
1930 struct mbuf *mp,
1931 u_int32_t *txd_upper,
1932 u_int32_t *txd_lower)
1934 struct em_context_desc *TXD;
1935 struct em_buffer *tx_buffer;
1936 int curr_txd;
1938 if (mp->m_pkthdr.csum_flags) {
1940 if (mp->m_pkthdr.csum_flags & CSUM_TCP) {
1941 *txd_upper = E1000_TXD_POPTS_TXSM << 8;
1942 *txd_lower = E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D;
1943 if (adapter->active_checksum_context == OFFLOAD_TCP_IP)
1944 return;
1945 else
1946 adapter->active_checksum_context = OFFLOAD_TCP_IP;
1948 } else if (mp->m_pkthdr.csum_flags & CSUM_UDP) {
1949 *txd_upper = E1000_TXD_POPTS_TXSM << 8;
1950 *txd_lower = E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D;
1951 if (adapter->active_checksum_context == OFFLOAD_UDP_IP)
1952 return;
1953 else
1954 adapter->active_checksum_context = OFFLOAD_UDP_IP;
1955 } else {
1956 *txd_upper = 0;
1957 *txd_lower = 0;
1958 return;
1960 } else {
1961 *txd_upper = 0;
1962 *txd_lower = 0;
1963 return;
1966 /* If we reach this point, the checksum offload context
1967 * needs to be reset.
1969 curr_txd = adapter->next_avail_tx_desc;
1970 tx_buffer = &adapter->tx_buffer_area[curr_txd];
1971 TXD = (struct em_context_desc *) &adapter->tx_desc_base[curr_txd];
1973 TXD->lower_setup.ip_fields.ipcss = ETHER_HDR_LEN;
1974 TXD->lower_setup.ip_fields.ipcso =
1975 ETHER_HDR_LEN + OFFSETOF_IPHDR_SUM;
1976 TXD->lower_setup.ip_fields.ipcse =
1977 ETHER_HDR_LEN + IP_HEADER_SIZE - 1;
1979 TXD->upper_setup.tcp_fields.tucss =
1980 ETHER_HDR_LEN + IP_HEADER_SIZE;
1981 TXD->upper_setup.tcp_fields.tucse = 0;
1983 if (adapter->active_checksum_context == OFFLOAD_TCP_IP) {
1984 TXD->upper_setup.tcp_fields.tucso =
1985 ETHER_HDR_LEN + IP_HEADER_SIZE +
1986 OFFSETOF_TCPHDR_SUM;
1987 } else if (adapter->active_checksum_context == OFFLOAD_UDP_IP) {
1988 TXD->upper_setup.tcp_fields.tucso =
1989 ETHER_HDR_LEN + IP_HEADER_SIZE +
1990 OFFSETOF_UDPHDR_SUM;
1993 TXD->tcp_seg_setup.data = 0;
1994 TXD->cmd_and_length = (adapter->txd_cmd | E1000_TXD_CMD_DEXT);
1996 tx_buffer->m_head = NULL;
1998 if (++curr_txd == adapter->num_tx_desc)
1999 curr_txd = 0;
2001 adapter->num_tx_desc_avail--;
2002 adapter->next_avail_tx_desc = curr_txd;
2004 return;
2007 /**********************************************************************
2009 * Examine each tx_buffer in the used queue. If the hardware is done
2010 * processing the packet then free associated resources. The
2011 * tx_buffer is put back on the free queue.
2013 **********************************************************************/
2014 static void
2015 em_clean_transmit_interrupts(struct adapter * adapter)
2017 int s;
2018 int i, num_avail;
2019 struct em_buffer *tx_buffer;
2020 struct em_tx_desc *tx_desc;
2021 struct ifnet *ifp = &adapter->interface_data.ac_if;
2023 if (adapter->num_tx_desc_avail == adapter->num_tx_desc)
2024 return;
2026 s = splimp();
2027 #if DEBUG_DISPLAY_STATS
2028 adapter->clean_tx_interrupts++;
2029 #endif
2030 num_avail = adapter->num_tx_desc_avail;
2031 i = adapter->oldest_used_tx_desc;
2033 tx_buffer = &adapter->tx_buffer_area[i];
2034 tx_desc = &adapter->tx_desc_base[i];
2036 while(tx_desc->upper.fields.status & E1000_TXD_STAT_DD) {
2038 tx_desc->upper.data = 0;
2039 num_avail++;
2041 if (tx_buffer->m_head) {
2042 ifp->if_opackets++;
2043 m_freem(tx_buffer->m_head);
2044 tx_buffer->m_head = NULL;
2047 if (++i == adapter->num_tx_desc)
2048 i = 0;
2050 tx_buffer = &adapter->tx_buffer_area[i];
2051 tx_desc = &adapter->tx_desc_base[i];
2054 adapter->oldest_used_tx_desc = i;
2057 * If we have enough room, clear IFF_OACTIVE to tell the stack
2058 * that it is OK to send packets.
2059 * If there are no pending descriptors, clear the timeout. Otherwise,
2060 * if some descriptors have been freed, restart the timeout.
2062 if (num_avail > EM_TX_CLEANUP_THRESHOLD) {
2063 ifp->if_flags &= ~IFF_OACTIVE;
2064 if (num_avail == adapter->num_tx_desc)
2065 ifp->if_timer = 0;
2066 else if (num_avail == adapter->num_tx_desc_avail)
2067 ifp->if_timer = EM_TX_TIMEOUT;
2069 adapter->num_tx_desc_avail = num_avail;
2070 splx(s);
2071 return;
2074 /*********************************************************************
2076 * Get a buffer from system mbuf buffer pool.
2078 **********************************************************************/
2079 static int
2080 em_get_buf(int i, struct adapter *adapter,
2081 struct mbuf *nmp)
2083 register struct mbuf *mp = nmp;
2084 struct ifnet *ifp;
2086 ifp = &adapter->interface_data.ac_if;
2088 if (mp == NULL) {
2089 MGETHDR(mp, M_DONTWAIT, MT_DATA);
2090 if (mp == NULL) {
2091 adapter->mbuf_alloc_failed++;
2092 return(ENOBUFS);
2094 MCLGET(mp, M_DONTWAIT);
2095 if ((mp->m_flags & M_EXT) == 0) {
2096 m_freem(mp);
2097 adapter->mbuf_cluster_failed++;
2098 return(ENOBUFS);
2100 mp->m_len = mp->m_pkthdr.len = MCLBYTES;
2101 } else {
2102 mp->m_len = mp->m_pkthdr.len = MCLBYTES;
2103 mp->m_data = mp->m_ext.ext_buf;
2104 mp->m_next = NULL;
2107 if (ifp->if_mtu <= ETHERMTU) {
2108 m_adj(mp, ETHER_ALIGN);
2111 adapter->rx_buffer_area[i].m_head = mp;
2112 adapter->rx_desc_base[i].buffer_addr =
2113 vtophys(mtod(mp, vm_offset_t));
2115 return(0);
2118 /*********************************************************************
2120 * Allocate memory for rx_buffer structures. Since we use one
2121 * rx_buffer per received packet, the maximum number of rx_buffer's
2122 * that we'll need is equal to the number of receive descriptors
2123 * that we've allocated.
2125 **********************************************************************/
2126 static int
2127 em_allocate_receive_structures(struct adapter * adapter)
2129 int i;
2131 if (!(adapter->rx_buffer_area =
2132 (struct em_buffer *) malloc(sizeof(struct em_buffer) *
2133 adapter->num_rx_desc, M_DEVBUF,
2134 M_NOWAIT))) {
2135 dprintf("ipro1000/%d: Unable to allocate rx_buffer memory\n",
2136 adapter->unit);
2137 return(ENOMEM);
2140 bzero(adapter->rx_buffer_area,
2141 sizeof(struct em_buffer) * adapter->num_rx_desc);
2143 for (i = 0; i < adapter->num_rx_desc; i++) {
2144 if (em_get_buf(i, adapter, NULL) == ENOBUFS) {
2145 adapter->rx_buffer_area[i].m_head = NULL;
2146 adapter->rx_desc_base[i].buffer_addr = 0;
2147 return(ENOBUFS);
2151 return(0);
2154 /*********************************************************************
2156 * Allocate and initialize receive structures.
2158 **********************************************************************/
2159 static int
2160 em_setup_receive_structures(struct adapter * adapter)
2162 bzero((void *) adapter->rx_desc_base,
2163 (sizeof(struct em_rx_desc)) * adapter->num_rx_desc);
2165 if (em_allocate_receive_structures(adapter))
2166 return ENOMEM;
2168 /* Setup our descriptor pointers */
2169 adapter->next_rx_desc_to_check = 0;
2170 return(0);
2173 /*********************************************************************
2175 * Enable receive unit.
2177 **********************************************************************/
2178 static void
2179 em_initialize_receive_unit(struct adapter * adapter)
2181 u_int32_t reg_rctl;
2182 u_int32_t reg_rxcsum;
2183 struct ifnet *ifp;
2184 u_int64_t rdba = vtophys((vm_offset_t)adapter->rx_desc_base);
2186 INIT_DEBUGOUT("em_initialize_receive_unit: begin");
2188 ifp = &adapter->interface_data.ac_if;
2190 /* Make sure receives are disabled while setting up the descriptor ring */
2191 E1000_WRITE_REG(&adapter->hw, RCTL, 0);
2193 /* Set the Receive Delay Timer Register */
2194 E1000_WRITE_REG(&adapter->hw, RDTR,
2195 adapter->rx_int_delay.value | E1000_RDT_FPDB);
2197 if(adapter->hw.mac_type >= em_82540) {
2198 E1000_WRITE_REG(&adapter->hw, RADV, adapter->rx_abs_int_delay.value);
2200 /* Set the interrupt throttling rate. Value is calculated
2201 * as DEFAULT_ITR = 1/(MAX_INTS_PER_SEC * 256ns) */
2202 #define MAX_INTS_PER_SEC 8000
2203 #define DEFAULT_ITR 1000000000/(MAX_INTS_PER_SEC * 256)
2204 E1000_WRITE_REG(&adapter->hw, ITR, DEFAULT_ITR);
2207 /* Setup the Base and Length of the Rx Descriptor Ring */
2208 E1000_WRITE_REG(&adapter->hw, RDBAL,
2209 (rdba & 0x00000000ffffffffULL));
2210 E1000_WRITE_REG(&adapter->hw, RDBAH, (rdba >> 32));
2211 E1000_WRITE_REG(&adapter->hw, RDLEN,
2212 adapter->num_rx_desc *
2213 sizeof(struct em_rx_desc));
2215 /* Setup the HW Rx Head and Tail Descriptor Pointers */
2216 E1000_WRITE_REG(&adapter->hw, RDH, 0);
2217 E1000_WRITE_REG(&adapter->hw, RDT, adapter->num_rx_desc - 1);
2219 /* Setup the Receive Control Register */
2220 reg_rctl = E1000_RCTL_EN | E1000_RCTL_BAM | E1000_RCTL_LBM_NO |
2221 E1000_RCTL_RDMTS_HALF |
2222 (adapter->hw.mc_filter_type << E1000_RCTL_MO_SHIFT);
2224 if (adapter->hw.tbi_compatibility_on == TRUE)
2225 reg_rctl |= E1000_RCTL_SBP;
2228 switch (adapter->rx_buffer_len) {
2229 default:
2230 case EM_RXBUFFER_2048:
2231 reg_rctl |= E1000_RCTL_SZ_2048;
2232 break;
2233 case EM_RXBUFFER_4096:
2234 reg_rctl |= E1000_RCTL_SZ_4096 | E1000_RCTL_BSEX | E1000_RCTL_LPE;
2235 break;
2236 case EM_RXBUFFER_8192:
2237 reg_rctl |= E1000_RCTL_SZ_8192 | E1000_RCTL_BSEX | E1000_RCTL_LPE;
2238 break;
2239 case EM_RXBUFFER_16384:
2240 reg_rctl |= E1000_RCTL_SZ_16384 | E1000_RCTL_BSEX | E1000_RCTL_LPE;
2241 break;
2244 if (ifp->if_mtu > ETHERMTU)
2245 reg_rctl |= E1000_RCTL_LPE;
2247 /* Enable 82543 Receive Checksum Offload for TCP and UDP */
2248 if ((adapter->hw.mac_type >= em_82543) &&
2249 (ifp->if_capenable & IFCAP_RXCSUM)) {
2250 reg_rxcsum = E1000_READ_REG(&adapter->hw, RXCSUM);
2251 reg_rxcsum |= (E1000_RXCSUM_IPOFL | E1000_RXCSUM_TUOFL);
2252 E1000_WRITE_REG(&adapter->hw, RXCSUM, reg_rxcsum);
2255 /* Enable Receives */
2256 E1000_WRITE_REG(&adapter->hw, RCTL, reg_rctl);
2258 return;
2261 /*********************************************************************
2263 * Free receive related data structures.
2265 **********************************************************************/
2266 static void
2267 em_free_receive_structures(struct adapter *adapter)
2269 struct em_buffer *rx_buffer;
2270 int i;
2272 INIT_DEBUGOUT("free_receive_structures: begin");
2274 if (adapter->rx_buffer_area != NULL) {
2275 rx_buffer = adapter->rx_buffer_area;
2276 for (i = 0; i < adapter->num_rx_desc; i++, rx_buffer++) {
2277 if (rx_buffer->m_head != NULL)
2278 m_freem(rx_buffer->m_head);
2279 rx_buffer->m_head = NULL;
2282 if (adapter->rx_buffer_area != NULL) {
2283 free(adapter->rx_buffer_area, M_DEVBUF);
2284 adapter->rx_buffer_area = NULL;
2286 return;
2289 /*********************************************************************
2291 * This routine executes in interrupt context. It replenishes
2292 * the mbufs in the descriptor and sends data which has been
2293 * dma'ed into host memory to upper layer.
2295 * We loop at most count times if count is > 0, or until done if
2296 * count < 0.
2298 *********************************************************************/
2299 static void
2300 em_process_receive_interrupts(struct adapter * adapter, int count)
2302 struct ifnet *ifp;
2303 struct mbuf *mp;
2304 #if __FreeBSD_version < 500000
2305 struct ether_header *eh;
2306 #endif
2307 u_int8_t accept_frame = 0;
2308 u_int8_t eop = 0;
2309 u_int16_t len, desc_len, prev_len_adj;
2310 int i;
2312 /* Pointer to the receive descriptor being examined. */
2313 struct em_rx_desc *current_desc;
2315 ifp = &adapter->interface_data.ac_if;
2316 i = adapter->next_rx_desc_to_check;
2317 current_desc = &adapter->rx_desc_base[i];
2319 if (!((current_desc->status) & E1000_RXD_STAT_DD)) {
2320 #if DEBUG_DISPLAY_STATS
2321 adapter->no_pkts_avail++;
2322 #endif
2323 return;
2326 while ((current_desc->status & E1000_RXD_STAT_DD) && (count != 0)) {
2328 mp = adapter->rx_buffer_area[i].m_head;
2330 accept_frame = 1;
2331 prev_len_adj = 0;
2332 desc_len = current_desc->length;
2333 if (current_desc->status & E1000_RXD_STAT_EOP) {
2334 count--;
2335 eop = 1;
2336 if (desc_len < ETHER_CRC_LEN) {
2337 len = 0;
2338 prev_len_adj = ETHER_CRC_LEN - desc_len;
2340 else {
2341 len = desc_len - ETHER_CRC_LEN;
2343 } else {
2344 eop = 0;
2345 len = desc_len;
2348 if (current_desc->errors & E1000_RXD_ERR_FRAME_ERR_MASK) {
2349 u_int8_t last_byte;
2350 u_int32_t pkt_len = desc_len;
2352 if (adapter->fmp != NULL)
2353 pkt_len += adapter->fmp->m_pkthdr.len;
2355 last_byte = *(mtod(mp, caddr_t) + desc_len - 1);
2357 if (TBI_ACCEPT(&adapter->hw, current_desc->status,
2358 current_desc->errors,
2359 pkt_len, last_byte)) {
2360 em_tbi_adjust_stats(&adapter->hw,
2361 &adapter->stats,
2362 pkt_len,
2363 adapter->hw.mac_addr);
2364 if (len > 0) len--;
2366 else {
2367 accept_frame = 0;
2371 if (accept_frame) {
2373 if (em_get_buf(i, adapter, NULL) == ENOBUFS) {
2374 adapter->dropped_pkts++;
2375 em_get_buf(i, adapter, mp);
2376 if (adapter->fmp != NULL)
2377 m_freem(adapter->fmp);
2378 adapter->fmp = NULL;
2379 adapter->lmp = NULL;
2380 break;
2383 /* Assign correct length to the current fragment */
2384 mp->m_len = len;
2386 if (adapter->fmp == NULL) {
2387 mp->m_pkthdr.len = len;
2388 adapter->fmp = mp; /* Store the first mbuf */
2389 adapter->lmp = mp;
2390 } else {
2391 /* Chain mbuf's together */
2392 mp->m_flags &= ~M_PKTHDR;
2394 * Adjust length of previous mbuf in chain if we
2395 * received less than 4 bytes in the last descriptor.
2397 if (prev_len_adj > 0) {
2398 adapter->lmp->m_len -= prev_len_adj;
2399 adapter->fmp->m_pkthdr.len -= prev_len_adj;
2401 adapter->lmp->m_next = mp;
2402 adapter->lmp = adapter->lmp->m_next;
2403 adapter->fmp->m_pkthdr.len += len;
2406 if (eop) {
2407 adapter->fmp->m_pkthdr.rcvif = ifp;
2408 ifp->if_ipackets++;
2410 #if __FreeBSD_version < 500000
2411 eh = mtod(adapter->fmp, struct ether_header *);
2412 /* Remove ethernet header from mbuf */
2413 m_adj(adapter->fmp, sizeof(struct ether_header));
2414 em_receive_checksum(adapter, current_desc,
2415 adapter->fmp);
2416 if (current_desc->status & E1000_RXD_STAT_VP)
2417 VLAN_INPUT_TAG(eh, adapter->fmp,
2418 (current_desc->special &
2419 E1000_RXD_SPC_VLAN_MASK));
2420 else
2421 ether_input(ifp, eh, adapter->fmp);
2422 #else
2424 em_receive_checksum(adapter, current_desc,
2425 adapter->fmp);
2426 if (current_desc->status & E1000_RXD_STAT_VP)
2427 VLAN_INPUT_TAG(ifp, adapter->fmp,
2428 (current_desc->special &
2429 E1000_RXD_SPC_VLAN_MASK),
2430 adapter->fmp = NULL);
2432 if (adapter->fmp != NULL)
2433 (*ifp->if_input)(ifp, adapter->fmp);
2434 #endif
2435 adapter->fmp = NULL;
2436 adapter->lmp = NULL;
2438 } else {
2439 adapter->dropped_pkts++;
2440 em_get_buf(i, adapter, mp);
2441 if (adapter->fmp != NULL)
2442 m_freem(adapter->fmp);
2443 adapter->fmp = NULL;
2444 adapter->lmp = NULL;
2447 /* Zero out the receive descriptors status */
2448 current_desc->status = 0;
2450 /* Advance the E1000's Receive Queue #0 "Tail Pointer". */
2451 E1000_WRITE_REG(&adapter->hw, RDT, i);
2453 /* Advance our pointers to the next descriptor */
2454 if (++i == adapter->num_rx_desc) {
2455 i = 0;
2456 current_desc = adapter->rx_desc_base;
2457 } else
2458 current_desc++;
2460 adapter->next_rx_desc_to_check = i;
2461 return;
2464 /*********************************************************************
2466 * Verify that the hardware indicated that the checksum is valid.
2467 * Inform the stack about the status of checksum so that stack
2468 * doesn't spend time verifying the checksum.
2470 *********************************************************************/
2471 static void
2472 em_receive_checksum(struct adapter *adapter,
2473 struct em_rx_desc *rx_desc,
2474 struct mbuf *mp)
2476 /* 82543 or newer only */
2477 if ((adapter->hw.mac_type < em_82543) ||
2478 /* Ignore Checksum bit is set */
2479 (rx_desc->status & E1000_RXD_STAT_IXSM)) {
2480 mp->m_pkthdr.csum_flags = 0;
2481 return;
2484 if (rx_desc->status & E1000_RXD_STAT_IPCS) {
2485 /* Did it pass? */
2486 if (!(rx_desc->errors & E1000_RXD_ERR_IPE)) {
2487 /* IP Checksum Good */
2488 mp->m_pkthdr.csum_flags = CSUM_IP_CHECKED;
2489 mp->m_pkthdr.csum_flags |= CSUM_IP_VALID;
2491 } else {
2492 mp->m_pkthdr.csum_flags = 0;
2496 if (rx_desc->status & E1000_RXD_STAT_TCPCS) {
2497 /* Did it pass? */
2498 if (!(rx_desc->errors & E1000_RXD_ERR_TCPE)) {
2499 mp->m_pkthdr.csum_flags |=
2500 (CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
2501 mp->m_pkthdr.csum_data = htons(0xffff);
2505 return;
2509 static void
2510 em_enable_vlans(struct adapter *adapter)
2512 uint32_t ctrl;
2514 E1000_WRITE_REG(&adapter->hw, VET, ETHERTYPE_VLAN);
2516 ctrl = E1000_READ_REG(&adapter->hw, CTRL);
2517 ctrl |= E1000_CTRL_VME;
2518 E1000_WRITE_REG(&adapter->hw, CTRL, ctrl);
2520 return;
2523 // can be called from within interrupt
2524 static void
2525 em_enable_intr(struct adapter * adapter)
2527 E1000_WRITE_REG(&adapter->hw, IMS, (IMS_ENABLE_MASK));
2528 return;
2531 // can be called from within interrupt
2532 static void
2533 em_disable_intr(struct adapter *adapter)
2535 E1000_WRITE_REG(&adapter->hw, IMC,
2536 (0xffffffff & ~E1000_IMC_RXSEQ));
2537 return;
2540 static int
2541 em_is_valid_ether_addr(u_int8_t *addr)
2543 char zero_addr[6] = { 0, 0, 0, 0, 0, 0 };
2545 if ((addr[0] & 1) || (!bcmp(addr, zero_addr, ETHER_ADDR_LEN))) {
2546 return (FALSE);
2549 return(TRUE);
2552 void
2553 em_write_pci_cfg(struct em_hw *hw,
2554 uint32_t reg,
2555 uint16_t *value)
2557 pci_write_config(((struct em_osdep *)hw->back)->dev, reg,
2558 *value, 2);
2561 void
2562 em_read_pci_cfg(struct em_hw *hw, uint32_t reg,
2563 uint16_t *value)
2565 *value = pci_read_config(((struct em_osdep *)hw->back)->dev,
2566 reg, 2);
2567 return;
2570 void
2571 em_pci_set_mwi(struct em_hw *hw)
2573 pci_write_config(((struct em_osdep *)hw->back)->dev,
2574 PCIR_COMMAND,
2575 (hw->pci_cmd_word | CMD_MEM_WRT_INVALIDATE), 2);
2576 return;
2579 void
2580 em_pci_clear_mwi(struct em_hw *hw)
2582 pci_write_config(((struct em_osdep *)hw->back)->dev,
2583 PCIR_COMMAND,
2584 (hw->pci_cmd_word & ~CMD_MEM_WRT_INVALIDATE), 2);
2585 return;
2588 uint32_t
2589 em_io_read(struct em_hw *hw, unsigned long port)
2591 return(inl(port));
2594 void
2595 em_io_write(struct em_hw *hw, unsigned long port, uint32_t value)
2597 outl(port, value);
2598 return;
2601 /*********************************************************************
2602 * 82544 Coexistence issue workaround.
2603 * There are 2 issues.
2604 * 1. Transmit Hang issue.
2605 * To detect this issue, following equation can be used...
2606 * SIZE[3:0] + ADDR[2:0] = SUM[3:0].
2607 * If SUM[3:0] is in between 1 to 4, we will have this issue.
2609 * 2. DAC issue.
2610 * To detect this issue, following equation can be used...
2611 * SIZE[3:0] + ADDR[2:0] = SUM[3:0].
2612 * If SUM[3:0] is in between 9 to c, we will have this issue.
2615 * WORKAROUND:
2616 * Make sure we do not have ending address as 1,2,3,4(Hang) or 9,a,b,c (DAC)
2618 *** *********************************************************************/
2619 static u_int32_t
2620 em_fill_descriptors (u_int64_t address,
2621 u_int32_t length,
2622 PDESC_ARRAY desc_array)
2624 /* Since issue is sensitive to length and address.*/
2625 /* Let us first check the address...*/
2626 u_int32_t safe_terminator;
2627 if (length <= 4) {
2628 desc_array->descriptor[0].address = address;
2629 desc_array->descriptor[0].length = length;
2630 desc_array->elements = 1;
2631 return desc_array->elements;
2633 safe_terminator = (u_int32_t)((((u_int32_t)address & 0x7) + (length & 0xF)) & 0xF);
2634 /* if it does not fall between 0x1 to 0x4 and 0x9 to 0xC then return */
2635 if (safe_terminator == 0 ||
2636 (safe_terminator > 4 &&
2637 safe_terminator < 9) ||
2638 (safe_terminator > 0xC &&
2639 safe_terminator <= 0xF)) {
2640 desc_array->descriptor[0].address = address;
2641 desc_array->descriptor[0].length = length;
2642 desc_array->elements = 1;
2643 return desc_array->elements;
2646 desc_array->descriptor[0].address = address;
2647 desc_array->descriptor[0].length = length - 4;
2648 desc_array->descriptor[1].address = address + (length - 4);
2649 desc_array->descriptor[1].length = 4;
2650 desc_array->elements = 2;
2651 return desc_array->elements;
2657 /**********************************************************************
2659 * Update the board statistics counters.
2661 **********************************************************************/
2662 static void
2663 em_update_stats_counters(struct adapter *adapter)
2665 struct ifnet *ifp;
2667 if(adapter->hw.media_type == em_media_type_copper ||
2668 (E1000_READ_REG(&adapter->hw, STATUS) & E1000_STATUS_LU)) {
2669 adapter->stats.symerrs += E1000_READ_REG(&adapter->hw, SYMERRS);
2670 adapter->stats.sec += E1000_READ_REG(&adapter->hw, SEC);
2672 adapter->stats.crcerrs += E1000_READ_REG(&adapter->hw, CRCERRS);
2673 adapter->stats.mpc += E1000_READ_REG(&adapter->hw, MPC);
2674 adapter->stats.scc += E1000_READ_REG(&adapter->hw, SCC);
2675 adapter->stats.ecol += E1000_READ_REG(&adapter->hw, ECOL);
2677 adapter->stats.mcc += E1000_READ_REG(&adapter->hw, MCC);
2678 adapter->stats.latecol += E1000_READ_REG(&adapter->hw, LATECOL);
2679 adapter->stats.colc += E1000_READ_REG(&adapter->hw, COLC);
2680 adapter->stats.dc += E1000_READ_REG(&adapter->hw, DC);
2681 adapter->stats.rlec += E1000_READ_REG(&adapter->hw, RLEC);
2682 adapter->stats.xonrxc += E1000_READ_REG(&adapter->hw, XONRXC);
2683 adapter->stats.xontxc += E1000_READ_REG(&adapter->hw, XONTXC);
2684 adapter->stats.xoffrxc += E1000_READ_REG(&adapter->hw, XOFFRXC);
2685 adapter->stats.xofftxc += E1000_READ_REG(&adapter->hw, XOFFTXC);
2686 adapter->stats.fcruc += E1000_READ_REG(&adapter->hw, FCRUC);
2687 adapter->stats.prc64 += E1000_READ_REG(&adapter->hw, PRC64);
2688 adapter->stats.prc127 += E1000_READ_REG(&adapter->hw, PRC127);
2689 adapter->stats.prc255 += E1000_READ_REG(&adapter->hw, PRC255);
2690 adapter->stats.prc511 += E1000_READ_REG(&adapter->hw, PRC511);
2691 adapter->stats.prc1023 += E1000_READ_REG(&adapter->hw, PRC1023);
2692 adapter->stats.prc1522 += E1000_READ_REG(&adapter->hw, PRC1522);
2693 adapter->stats.gprc += E1000_READ_REG(&adapter->hw, GPRC);
2694 adapter->stats.bprc += E1000_READ_REG(&adapter->hw, BPRC);
2695 adapter->stats.mprc += E1000_READ_REG(&adapter->hw, MPRC);
2696 adapter->stats.gptc += E1000_READ_REG(&adapter->hw, GPTC);
2698 /* For the 64-bit byte counters the low dword must be read first. */
2699 /* Both registers clear on the read of the high dword */
2701 adapter->stats.gorcl += E1000_READ_REG(&adapter->hw, GORCL);
2702 adapter->stats.gorch += E1000_READ_REG(&adapter->hw, GORCH);
2703 adapter->stats.gotcl += E1000_READ_REG(&adapter->hw, GOTCL);
2704 adapter->stats.gotch += E1000_READ_REG(&adapter->hw, GOTCH);
2706 adapter->stats.rnbc += E1000_READ_REG(&adapter->hw, RNBC);
2707 adapter->stats.ruc += E1000_READ_REG(&adapter->hw, RUC);
2708 adapter->stats.rfc += E1000_READ_REG(&adapter->hw, RFC);
2709 adapter->stats.roc += E1000_READ_REG(&adapter->hw, ROC);
2710 adapter->stats.rjc += E1000_READ_REG(&adapter->hw, RJC);
2712 adapter->stats.torl += E1000_READ_REG(&adapter->hw, TORL);
2713 adapter->stats.torh += E1000_READ_REG(&adapter->hw, TORH);
2714 adapter->stats.totl += E1000_READ_REG(&adapter->hw, TOTL);
2715 adapter->stats.toth += E1000_READ_REG(&adapter->hw, TOTH);
2717 adapter->stats.tpr += E1000_READ_REG(&adapter->hw, TPR);
2718 adapter->stats.tpt += E1000_READ_REG(&adapter->hw, TPT);
2719 adapter->stats.ptc64 += E1000_READ_REG(&adapter->hw, PTC64);
2720 adapter->stats.ptc127 += E1000_READ_REG(&adapter->hw, PTC127);
2721 adapter->stats.ptc255 += E1000_READ_REG(&adapter->hw, PTC255);
2722 adapter->stats.ptc511 += E1000_READ_REG(&adapter->hw, PTC511);
2723 adapter->stats.ptc1023 += E1000_READ_REG(&adapter->hw, PTC1023);
2724 adapter->stats.ptc1522 += E1000_READ_REG(&adapter->hw, PTC1522);
2725 adapter->stats.mptc += E1000_READ_REG(&adapter->hw, MPTC);
2726 adapter->stats.bptc += E1000_READ_REG(&adapter->hw, BPTC);
2728 if (adapter->hw.mac_type >= em_82543) {
2729 adapter->stats.algnerrc +=
2730 E1000_READ_REG(&adapter->hw, ALGNERRC);
2731 adapter->stats.rxerrc +=
2732 E1000_READ_REG(&adapter->hw, RXERRC);
2733 adapter->stats.tncrs +=
2734 E1000_READ_REG(&adapter->hw, TNCRS);
2735 adapter->stats.cexterr +=
2736 E1000_READ_REG(&adapter->hw, CEXTERR);
2737 adapter->stats.tsctc +=
2738 E1000_READ_REG(&adapter->hw, TSCTC);
2739 adapter->stats.tsctfc +=
2740 E1000_READ_REG(&adapter->hw, TSCTFC);
2742 ifp = &adapter->interface_data.ac_if;
2744 /* Fill out the OS statistics structure */
2745 ifp->if_ibytes = adapter->stats.gorcl;
2746 ifp->if_obytes = adapter->stats.gotcl;
2747 ifp->if_imcasts = adapter->stats.mprc;
2748 ifp->if_collisions = adapter->stats.colc;
2750 /* Rx Errors */
2751 ifp->if_ierrors =
2752 adapter->dropped_pkts +
2753 adapter->stats.rxerrc +
2754 adapter->stats.crcerrs +
2755 adapter->stats.algnerrc +
2756 adapter->stats.rlec + adapter->stats.rnbc +
2757 adapter->stats.mpc + adapter->stats.cexterr;
2759 /* Tx Errors */
2760 ifp->if_oerrors = adapter->stats.ecol + adapter->stats.latecol;
2765 /**********************************************************************
2767 * This routine is called only when DEBUG_DISPLAY_STATS is enabled.
2768 * This routine provides a way to take a look at important statistics
2769 * maintained by the driver and hardware.
2771 **********************************************************************/
2772 static void
2773 em_print_debug_info(struct adapter *adapter)
2775 #if DEBUG_DISPLAY_STATS
2776 int unit = adapter->unit;
2777 uint8_t *hw_addr = adapter->hw.hw_addr;
2779 dprintf("ipro1000/%d: Adapter hardware address = %p \n", unit, hw_addr);
2780 dprintf("ipro1000/%d:tx_int_delay = %ld, tx_abs_int_delay = %ld\n", unit,
2781 E1000_READ_REG(&adapter->hw, TIDV),
2782 E1000_READ_REG(&adapter->hw, TADV));
2783 dprintf("ipro1000/%d:rx_int_delay = %ld, rx_abs_int_delay = %ld\n", unit,
2784 E1000_READ_REG(&adapter->hw, RDTR),
2785 E1000_READ_REG(&adapter->hw, RADV));
2786 dprintf("ipro1000/%d: Packets not Avail = %ld\n", unit,
2787 adapter->no_pkts_avail);
2788 dprintf("ipro1000/%d: CleanTxInterrupts = %ld\n", unit,
2789 adapter->clean_tx_interrupts);
2790 snooze(10000); // give syslog reader some time to catchup
2791 dprintf("ipro1000/%d: fifo workaround = %Ld, fifo_reset = %Ld\n", unit,
2792 (long long)adapter->tx_fifo_wrk,
2793 (long long)adapter->tx_fifo_reset);
2794 dprintf("ipro1000/%d: hw tdh = %ld, hw tdt = %ld\n", unit,
2795 E1000_READ_REG(&adapter->hw, TDH),
2796 E1000_READ_REG(&adapter->hw, TDT));
2797 dprintf("ipro1000/%d: Num Tx descriptors avail = %d\n", unit,
2798 adapter->num_tx_desc_avail);
2799 dprintf("ipro1000/%d: Tx Descriptors not avail1 = %ld\n", unit,
2800 adapter->no_tx_desc_avail1);
2801 dprintf("ipro1000/%d: Tx Descriptors not avail2 = %ld\n", unit,
2802 adapter->no_tx_desc_avail2);
2803 dprintf("ipro1000/%d: Std mbuf failed = %ld\n", unit,
2804 adapter->mbuf_alloc_failed);
2805 dprintf("ipro1000/%d: Std mbuf cluster failed = %ld\n", unit,
2806 adapter->mbuf_cluster_failed);
2807 dprintf("ipro1000/%d: Driver dropped packets = %ld\n", unit,
2808 adapter->dropped_pkts);
2809 snooze(10000); // give syslog reader some time to catchup
2810 #endif
2813 static void
2814 em_print_hw_stats(struct adapter *adapter)
2816 #if DEBUG_DISPLAY_STATS
2817 int unit = adapter->unit;
2819 dprintf("ipro1000/%d: Excessive collisions = %Ld\n", unit,
2820 (long long)adapter->stats.ecol);
2821 dprintf("ipro1000/%d: Symbol errors = %Ld\n", unit,
2822 (long long)adapter->stats.symerrs);
2823 dprintf("ipro1000/%d: Sequence errors = %Ld\n", unit,
2824 (long long)adapter->stats.sec);
2825 dprintf("ipro1000/%d: Defer count = %Ld\n", unit,
2826 (long long)adapter->stats.dc);
2827 snooze(10000); // give syslog reader some time to catchup
2829 dprintf("ipro1000/%d: Missed Packets = %Ld\n", unit,
2830 (long long)adapter->stats.mpc);
2831 dprintf("ipro1000/%d: Receive No Buffers = %Ld\n", unit,
2832 (long long)adapter->stats.rnbc);
2833 dprintf("ipro1000/%d: Receive length errors = %Ld\n", unit,
2834 (long long)adapter->stats.rlec);
2835 dprintf("ipro1000/%d: Receive errors = %Ld\n", unit,
2836 (long long)adapter->stats.rxerrc);
2837 dprintf("ipro1000/%d: Crc errors = %Ld\n", unit,
2838 (long long)adapter->stats.crcerrs);
2839 dprintf("ipro1000/%d: Alignment errors = %Ld\n", unit,
2840 (long long)adapter->stats.algnerrc);
2841 dprintf("ipro1000/%d: Carrier extension errors = %Ld\n", unit,
2842 (long long)adapter->stats.cexterr);
2843 snooze(10000); // give syslog reader some time to catchup
2845 dprintf("ipro1000/%d: XON Rcvd = %Ld\n", unit,
2846 (long long)adapter->stats.xonrxc);
2847 dprintf("ipro1000/%d: XON Xmtd = %Ld\n", unit,
2848 (long long)adapter->stats.xontxc);
2849 dprintf("ipro1000/%d: XOFF Rcvd = %Ld\n", unit,
2850 (long long)adapter->stats.xoffrxc);
2851 dprintf("ipro1000/%d: XOFF Xmtd = %Ld\n", unit,
2852 (long long)adapter->stats.xofftxc);
2853 snooze(10000); // give syslog reader some time to catchup
2855 dprintf("ipro1000/%d: Good Packets Rcvd = %Ld\n", unit,
2856 (long long)adapter->stats.gprc);
2857 dprintf("ipro1000/%d: Good Packets Xmtd = %Ld\n", unit,
2858 (long long)adapter->stats.gptc);
2859 #endif
2862 #if 0
2864 static int
2865 em_sysctl_debug_info(SYSCTL_HANDLER_ARGS)
2867 int error;
2868 int result;
2869 struct adapter *adapter;
2871 result = -1;
2872 error = sysctl_handle_int(oidp, &result, 0, req);
2874 if (error || !req->newptr)
2875 return (error);
2877 if (result == 1) {
2878 adapter = (struct adapter *)arg1;
2879 em_print_debug_info(adapter);
2882 return error;
2886 static int
2887 em_sysctl_stats(SYSCTL_HANDLER_ARGS)
2889 int error;
2890 int result;
2891 struct adapter *adapter;
2893 result = -1;
2894 error = sysctl_handle_int(oidp, &result, 0, req);
2896 if (error || !req->newptr)
2897 return (error);
2899 if (result == 1) {
2900 adapter = (struct adapter *)arg1;
2901 em_print_hw_stats(adapter);
2904 return error;
2907 static int
2908 em_sysctl_int_delay(SYSCTL_HANDLER_ARGS)
2910 struct em_int_delay_info *info;
2911 struct adapter *adapter;
2912 u_int32_t regval;
2913 int error;
2914 int usecs;
2915 int ticks;
2916 int s;
2918 info = (struct em_int_delay_info *)arg1;
2919 adapter = info->adapter;
2920 usecs = info->value;
2921 error = sysctl_handle_int(oidp, &usecs, 0, req);
2922 if (error != 0 || req->newptr == NULL)
2923 return error;
2924 if (usecs < 0 || usecs > E1000_TICKS_TO_USECS(65535))
2925 return EINVAL;
2926 info->value = usecs;
2927 ticks = E1000_USECS_TO_TICKS(usecs);
2929 s = splimp();
2930 regval = E1000_READ_OFFSET(&adapter->hw, info->offset);
2931 regval = (regval & ~0xffff) | (ticks & 0xffff);
2932 /* Handle a few special cases. */
2933 switch (info->offset) {
2934 case E1000_RDTR:
2935 case E1000_82542_RDTR:
2936 regval |= E1000_RDT_FPDB;
2937 break;
2938 case E1000_TIDV:
2939 case E1000_82542_TIDV:
2940 if (ticks == 0) {
2941 adapter->txd_cmd &= ~E1000_TXD_CMD_IDE;
2942 /* Don't write 0 into the TIDV register. */
2943 regval++;
2944 } else
2945 adapter->txd_cmd |= E1000_TXD_CMD_IDE;
2946 break;
2948 E1000_WRITE_OFFSET(&adapter->hw, info->offset, regval);
2949 splx(s);
2950 return 0;
2953 #endif
2955 static void
2956 em_add_int_delay_sysctl(struct adapter *adapter, const char *name,
2957 const char *description, struct em_int_delay_info *info,
2958 int offset, int value)
2960 info->adapter = adapter;
2961 info->offset = offset;
2962 info->value = value;
2963 SYSCTL_ADD_PROC(&adapter->sysctl_ctx,
2964 SYSCTL_CHILDREN(adapter->sysctl_tree),
2965 OID_AUTO, name, CTLTYPE_INT|CTLFLAG_RW,
2966 info, 0, em_sysctl_int_delay, "I", description);