3 * Joerg Sonnenberger <joerg@bec.de>. All rights reserved.
5 * Copyright (c) 1997, 1998-2003
6 * Bill Paul <wpaul@windriver.com>. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by Bill Paul.
19 * 4. Neither the name of the author nor the names of any co-contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
27 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
33 * THE POSSIBILITY OF SUCH DAMAGE.
35 * $FreeBSD: src/sys/dev/re/if_re.c,v 1.25 2004/06/09 14:34:01 naddy Exp $
39 * RealTek 8169S/8110S/8168/8111/8101E PCI NIC driver
41 * Written by Bill Paul <wpaul@windriver.com>
42 * Senior Networking Software Engineer
47 * This driver is designed to support RealTek's next generation of
48 * 10/100 and 10/100/1000 PCI ethernet controllers. There are currently
49 * seven devices in this family: the the RTL8169, the RTL8169S, RTL8110S,
50 * the RTL8168, the RTL8111 and the RTL8101E.
52 * The 8169 is a 64-bit 10/100/1000 gigabit ethernet MAC:
54 * o Descriptor based DMA mechanism. Each descriptor represents
55 * a single packet fragment. Data buffers may be aligned on
60 * o TCP/IP checksum offload for both RX and TX.
62 * o High and normal priority transmit DMA rings.
64 * o VLAN tag insertion and extraction.
66 * o TCP large send (segmentation offload).
72 * o GMII and TBI ports/registers for interfacing with copper
75 * o RX and TX DMA rings can have up to 1024 descriptors.
77 * The 8169 does not have a built-in PHY. Most reference boards use a
78 * Marvell 88E1000 'Alaska' copper gigE PHY. 8169/8110 is _no longer_
81 * The 8169S/8110S 10/100/1000 devices have built-in copper gigE PHYs
82 * (the 'S' stands for 'single-chip'). These devices have the same
83 * programming API as the older 8169, but also have some vendor-specific
84 * registers for the on-board PHY. The 8110S is a LAN-on-motherboard
85 * part designed to be pin-compatible with the RealTek 8100 10/100 chip.
87 * This driver takes advantage of the RX and TX checksum offload and
88 * VLAN tag insertion/extraction features. It also implements
89 * interrupt moderation using the timer interrupt registers, which
90 * significantly reduces interrupt load.
95 #include "opt_ifpoll.h"
97 #include <sys/param.h>
99 #include <sys/endian.h>
100 #include <sys/kernel.h>
101 #include <sys/in_cksum.h>
102 #include <sys/interrupt.h>
103 #include <sys/malloc.h>
104 #include <sys/mbuf.h>
105 #include <sys/rman.h>
106 #include <sys/serialize.h>
107 #include <sys/socket.h>
108 #include <sys/sockio.h>
109 #include <sys/sysctl.h>
112 #include <net/ethernet.h>
114 #include <net/ifq_var.h>
115 #include <net/if_arp.h>
116 #include <net/if_dl.h>
117 #include <net/if_media.h>
118 #include <net/if_poll.h>
119 #include <net/if_types.h>
120 #include <net/vlan/if_vlan_var.h>
121 #include <net/vlan/if_vlan_ether.h>
123 #include <netinet/ip.h>
126 #include <bus/pci/pcireg.h>
127 #include <bus/pci/pcivar.h>
129 #include <dev/netif/re/if_rereg.h>
130 #include <dev/netif/re/if_revar.h>
131 #include <dev/netif/re/re.h>
132 #include <dev/netif/re/re_dragonfly.h>
135 * Various supported device vendors/types and their names.
137 static const struct re_type
{
142 { PCI_VENDOR_DLINK
, PCI_PRODUCT_DLINK_DGE528T
,
143 "D-Link DGE-528(T) Gigabit Ethernet Adapter" },
145 { PCI_VENDOR_REALTEK
, PCI_PRODUCT_REALTEK_RT8101E
,
146 "RealTek 810x PCIe 10/100baseTX" },
148 { PCI_VENDOR_REALTEK
, PCI_PRODUCT_REALTEK_RT8168
,
149 "RealTek 8111/8168 PCIe Gigabit Ethernet" },
151 { PCI_VENDOR_REALTEK
, PCI_PRODUCT_REALTEK_RT8168_1
,
152 "RealTek 8168 PCIe Gigabit Ethernet" },
156 * This driver now only supports built-in PHYs.
158 { PCI_VENDOR_REALTEK
, PCI_PRODUCT_REALTEK_RT8169
,
159 "RealTek 8110/8169 Gigabit Ethernet" },
162 { PCI_VENDOR_REALTEK
, PCI_PRODUCT_REALTEK_RT8169SC
,
163 "RealTek 8169SC/8110SC Single-chip Gigabit Ethernet" },
165 { PCI_VENDOR_COREGA
, PCI_PRODUCT_COREGA_CG_LAPCIGT
,
166 "Corega CG-LAPCIGT Gigabit Ethernet" },
168 { PCI_VENDOR_LINKSYS
, PCI_PRODUCT_LINKSYS_EG1032
,
169 "Linksys EG1032 Gigabit Ethernet" },
171 { PCI_VENDOR_USR2
, PCI_PRODUCT_USR2_997902
,
172 "US Robotics 997902 Gigabit Ethernet" },
174 { PCI_VENDOR_TTTECH
, PCI_PRODUCT_TTTECH_MC322
,
175 "TTTech MC322 Gigabit Ethernet" },
180 static int re_probe(device_t
);
181 static int re_attach(device_t
);
182 static int re_detach(device_t
);
183 static int re_suspend(device_t
);
184 static int re_resume(device_t
);
185 static void re_shutdown(device_t
);
187 static int re_allocmem(device_t
);
188 static void re_freemem(device_t
);
189 static void re_freebufmem(struct re_softc
*, int, int);
190 static int re_encap(struct re_softc
*, struct mbuf
**, int *);
191 static int re_newbuf_std(struct re_softc
*, int, int);
193 static int re_newbuf_jumbo(struct re_softc
*, int, int);
195 static void re_setup_rxdesc(struct re_softc
*, int);
196 static int re_rx_list_init(struct re_softc
*);
197 static int re_tx_list_init(struct re_softc
*);
198 static int re_rxeof(struct re_softc
*);
199 static int re_txeof(struct re_softc
*);
200 static int re_tx_collect(struct re_softc
*);
201 static void re_intr(void *);
202 static void re_tick(void *);
203 static void re_tick_serialized(void *);
204 static void re_disable_aspm(device_t
);
205 static void re_link_up(struct re_softc
*);
206 static void re_link_down(struct re_softc
*);
208 static void re_start(struct ifnet
*, struct ifaltq_subque
*);
209 static int re_ioctl(struct ifnet
*, u_long
, caddr_t
, struct ucred
*);
210 static void re_init(void *);
211 static void re_stop(struct re_softc
*, boolean_t
);
212 static void re_watchdog(struct ifnet
*);
214 static void re_setup_hw_im(struct re_softc
*);
215 static void re_setup_sim_im(struct re_softc
*);
216 static void re_disable_hw_im(struct re_softc
*);
217 static void re_disable_sim_im(struct re_softc
*);
218 static void re_config_imtype(struct re_softc
*, int);
219 static void re_setup_intr(struct re_softc
*, int, int);
221 static int re_sysctl_hwtime(SYSCTL_HANDLER_ARGS
, int *);
222 static int re_sysctl_rxtime(SYSCTL_HANDLER_ARGS
);
223 static int re_sysctl_txtime(SYSCTL_HANDLER_ARGS
);
224 static int re_sysctl_simtime(SYSCTL_HANDLER_ARGS
);
225 static int re_sysctl_imtype(SYSCTL_HANDLER_ARGS
);
227 static int re_jpool_alloc(struct re_softc
*);
228 static void re_jpool_free(struct re_softc
*);
230 static struct re_jbuf
*re_jbuf_alloc(struct re_softc
*);
231 static void re_jbuf_free(void *);
232 static void re_jbuf_ref(void *);
236 static void re_npoll(struct ifnet
*, struct ifpoll_info
*);
237 static void re_npoll_compat(struct ifnet
*, void *, int);
240 static device_method_t re_methods
[] = {
241 /* Device interface */
242 DEVMETHOD(device_probe
, re_probe
),
243 DEVMETHOD(device_attach
, re_attach
),
244 DEVMETHOD(device_detach
, re_detach
),
245 DEVMETHOD(device_suspend
, re_suspend
),
246 DEVMETHOD(device_resume
, re_resume
),
247 DEVMETHOD(device_shutdown
, re_shutdown
),
251 static driver_t re_driver
= {
254 sizeof(struct re_softc
)
257 static devclass_t re_devclass
;
259 DECLARE_DUMMY_MODULE(if_re
);
260 DRIVER_MODULE(if_re
, pci
, re_driver
, re_devclass
, NULL
, NULL
);
261 DRIVER_MODULE(if_re
, cardbus
, re_driver
, re_devclass
, NULL
, NULL
);
263 static int re_rx_desc_count
= RE_RX_DESC_CNT_DEF
;
264 static int re_tx_desc_count
= RE_TX_DESC_CNT_DEF
;
265 static int re_msi_enable
= 1;
267 TUNABLE_INT("hw.re.rx_desc_count", &re_rx_desc_count
);
268 TUNABLE_INT("hw.re.tx_desc_count", &re_tx_desc_count
);
269 TUNABLE_INT("hw.re.msi.enable", &re_msi_enable
);
272 re_free_rxchain(struct re_softc
*sc
)
274 if (sc
->re_head
!= NULL
) {
275 m_freem(sc
->re_head
);
276 sc
->re_head
= sc
->re_tail
= NULL
;
281 re_probe(device_t dev
)
283 const struct re_type
*t
;
284 uint16_t vendor
, product
;
286 vendor
= pci_get_vendor(dev
);
287 product
= pci_get_device(dev
);
290 * Only attach to rev.3 of the Linksys EG1032 adapter.
291 * Rev.2 is supported by sk(4).
293 if (vendor
== PCI_VENDOR_LINKSYS
&&
294 product
== PCI_PRODUCT_LINKSYS_EG1032
&&
295 pci_get_subdevice(dev
) != PCI_SUBDEVICE_LINKSYS_EG1032_REV3
)
298 for (t
= re_devs
; t
->re_name
!= NULL
; t
++) {
299 if (product
== t
->re_did
&& vendor
== t
->re_vid
)
302 if (t
->re_name
== NULL
)
305 device_set_desc(dev
, t
->re_name
);
310 re_allocmem(device_t dev
)
312 struct re_softc
*sc
= device_get_softc(dev
);
319 sc
->re_ldata
.re_tx_mbuf
=
320 kmalloc(sc
->re_tx_desc_cnt
* sizeof(struct mbuf
*),
321 M_DEVBUF
, M_ZERO
| M_WAITOK
);
323 sc
->re_ldata
.re_rx_mbuf
=
324 kmalloc(sc
->re_rx_desc_cnt
* sizeof(struct mbuf
*),
325 M_DEVBUF
, M_ZERO
| M_WAITOK
);
327 sc
->re_ldata
.re_rx_paddr
=
328 kmalloc(sc
->re_rx_desc_cnt
* sizeof(bus_addr_t
),
329 M_DEVBUF
, M_ZERO
| M_WAITOK
);
331 sc
->re_ldata
.re_tx_dmamap
=
332 kmalloc(sc
->re_tx_desc_cnt
* sizeof(bus_dmamap_t
),
333 M_DEVBUF
, M_ZERO
| M_WAITOK
);
335 sc
->re_ldata
.re_rx_dmamap
=
336 kmalloc(sc
->re_rx_desc_cnt
* sizeof(bus_dmamap_t
),
337 M_DEVBUF
, M_ZERO
| M_WAITOK
);
340 * Allocate the parent bus DMA tag appropriate for PCI.
342 error
= bus_dma_tag_create(NULL
, /* parent */
343 1, 0, /* alignment, boundary */
344 BUS_SPACE_MAXADDR
, /* lowaddr */
345 BUS_SPACE_MAXADDR
, /* highaddr */
346 NULL
, NULL
, /* filter, filterarg */
347 BUS_SPACE_MAXSIZE_32BIT
,/* maxsize */
349 BUS_SPACE_MAXSIZE_32BIT
,/* maxsegsize */
353 device_printf(dev
, "could not allocate parent dma tag\n");
357 /* Allocate TX descriptor list. */
358 error
= bus_dmamem_coherent(sc
->re_parent_tag
,
360 BUS_SPACE_MAXADDR
, BUS_SPACE_MAXADDR
,
361 RE_TX_LIST_SZ(sc
), BUS_DMA_WAITOK
| BUS_DMA_ZERO
,
364 device_printf(dev
, "could not allocate TX ring\n");
367 sc
->re_ldata
.re_tx_list_tag
= dmem
.dmem_tag
;
368 sc
->re_ldata
.re_tx_list_map
= dmem
.dmem_map
;
369 sc
->re_ldata
.re_tx_list
= dmem
.dmem_addr
;
370 sc
->re_ldata
.re_tx_list_addr
= dmem
.dmem_busaddr
;
372 /* Allocate RX descriptor list. */
373 error
= bus_dmamem_coherent(sc
->re_parent_tag
,
375 BUS_SPACE_MAXADDR
, BUS_SPACE_MAXADDR
,
376 RE_RX_LIST_SZ(sc
), BUS_DMA_WAITOK
| BUS_DMA_ZERO
,
379 device_printf(dev
, "could not allocate RX ring\n");
382 sc
->re_ldata
.re_rx_list_tag
= dmem
.dmem_tag
;
383 sc
->re_ldata
.re_rx_list_map
= dmem
.dmem_map
;
384 sc
->re_ldata
.re_rx_list
= dmem
.dmem_addr
;
385 sc
->re_ldata
.re_rx_list_addr
= dmem
.dmem_busaddr
;
387 /* Allocate maps for TX mbufs. */
388 error
= bus_dma_tag_create(sc
->re_parent_tag
,
390 BUS_SPACE_MAXADDR
, BUS_SPACE_MAXADDR
,
392 RE_FRAMELEN_MAX
, RE_MAXSEGS
, MCLBYTES
,
393 BUS_DMA_ALLOCNOW
| BUS_DMA_WAITOK
| BUS_DMA_ONEBPAGE
,
394 &sc
->re_ldata
.re_tx_mtag
);
396 device_printf(dev
, "could not allocate TX buf dma tag\n");
400 /* Create DMA maps for TX buffers */
401 for (i
= 0; i
< sc
->re_tx_desc_cnt
; i
++) {
402 error
= bus_dmamap_create(sc
->re_ldata
.re_tx_mtag
,
403 BUS_DMA_WAITOK
| BUS_DMA_ONEBPAGE
,
404 &sc
->re_ldata
.re_tx_dmamap
[i
]);
406 device_printf(dev
, "can't create DMA map for TX buf\n");
407 re_freebufmem(sc
, i
, 0);
412 /* Allocate maps for RX mbufs. */
413 error
= bus_dma_tag_create(sc
->re_parent_tag
,
415 BUS_SPACE_MAXADDR
, BUS_SPACE_MAXADDR
,
417 MCLBYTES
, 1, MCLBYTES
,
418 BUS_DMA_ALLOCNOW
| BUS_DMA_WAITOK
| BUS_DMA_ALIGNED
,
419 &sc
->re_ldata
.re_rx_mtag
);
421 device_printf(dev
, "could not allocate RX buf dma tag\n");
425 /* Create spare DMA map for RX */
426 error
= bus_dmamap_create(sc
->re_ldata
.re_rx_mtag
, BUS_DMA_WAITOK
,
427 &sc
->re_ldata
.re_rx_spare
);
429 device_printf(dev
, "can't create spare DMA map for RX\n");
430 bus_dma_tag_destroy(sc
->re_ldata
.re_rx_mtag
);
431 sc
->re_ldata
.re_rx_mtag
= NULL
;
435 /* Create DMA maps for RX buffers */
436 for (i
= 0; i
< sc
->re_rx_desc_cnt
; i
++) {
437 error
= bus_dmamap_create(sc
->re_ldata
.re_rx_mtag
,
438 BUS_DMA_WAITOK
, &sc
->re_ldata
.re_rx_dmamap
[i
]);
440 device_printf(dev
, "can't create DMA map for RX buf\n");
441 re_freebufmem(sc
, sc
->re_tx_desc_cnt
, i
);
446 /* Create jumbo buffer pool for RX if required */
447 if (sc
->re_caps
& RE_C_CONTIGRX
) {
448 error
= re_jpool_alloc(sc
);
452 /* Disable jumbo frame support */
453 sc
->re_maxmtu
= ETHERMTU
;
461 re_freebufmem(struct re_softc
*sc
, int tx_cnt
, int rx_cnt
)
465 /* Destroy all the RX and TX buffer maps */
466 if (sc
->re_ldata
.re_tx_mtag
) {
467 for (i
= 0; i
< tx_cnt
; i
++) {
468 bus_dmamap_destroy(sc
->re_ldata
.re_tx_mtag
,
469 sc
->re_ldata
.re_tx_dmamap
[i
]);
471 bus_dma_tag_destroy(sc
->re_ldata
.re_tx_mtag
);
472 sc
->re_ldata
.re_tx_mtag
= NULL
;
475 if (sc
->re_ldata
.re_rx_mtag
) {
476 for (i
= 0; i
< rx_cnt
; i
++) {
477 bus_dmamap_destroy(sc
->re_ldata
.re_rx_mtag
,
478 sc
->re_ldata
.re_rx_dmamap
[i
]);
480 bus_dmamap_destroy(sc
->re_ldata
.re_rx_mtag
,
481 sc
->re_ldata
.re_rx_spare
);
482 bus_dma_tag_destroy(sc
->re_ldata
.re_rx_mtag
);
483 sc
->re_ldata
.re_rx_mtag
= NULL
;
488 re_freemem(device_t dev
)
490 struct re_softc
*sc
= device_get_softc(dev
);
492 /* Unload and free the RX DMA ring memory and map */
493 if (sc
->re_ldata
.re_rx_list_tag
) {
494 bus_dmamap_unload(sc
->re_ldata
.re_rx_list_tag
,
495 sc
->re_ldata
.re_rx_list_map
);
496 bus_dmamem_free(sc
->re_ldata
.re_rx_list_tag
,
497 sc
->re_ldata
.re_rx_list
,
498 sc
->re_ldata
.re_rx_list_map
);
499 bus_dma_tag_destroy(sc
->re_ldata
.re_rx_list_tag
);
502 /* Unload and free the TX DMA ring memory and map */
503 if (sc
->re_ldata
.re_tx_list_tag
) {
504 bus_dmamap_unload(sc
->re_ldata
.re_tx_list_tag
,
505 sc
->re_ldata
.re_tx_list_map
);
506 bus_dmamem_free(sc
->re_ldata
.re_tx_list_tag
,
507 sc
->re_ldata
.re_tx_list
,
508 sc
->re_ldata
.re_tx_list_map
);
509 bus_dma_tag_destroy(sc
->re_ldata
.re_tx_list_tag
);
512 /* Free RX/TX buf DMA stuffs */
513 re_freebufmem(sc
, sc
->re_tx_desc_cnt
, sc
->re_rx_desc_cnt
);
515 /* Unload and free the stats buffer and map */
516 if (sc
->re_ldata
.re_stag
) {
517 bus_dmamap_unload(sc
->re_ldata
.re_stag
, sc
->re_ldata
.re_smap
);
518 bus_dmamem_free(sc
->re_ldata
.re_stag
,
519 sc
->re_ldata
.re_stats
,
520 sc
->re_ldata
.re_smap
);
521 bus_dma_tag_destroy(sc
->re_ldata
.re_stag
);
524 if (sc
->re_caps
& RE_C_CONTIGRX
)
527 if (sc
->re_parent_tag
)
528 bus_dma_tag_destroy(sc
->re_parent_tag
);
530 if (sc
->re_ldata
.re_tx_mbuf
!= NULL
)
531 kfree(sc
->re_ldata
.re_tx_mbuf
, M_DEVBUF
);
532 if (sc
->re_ldata
.re_rx_mbuf
!= NULL
)
533 kfree(sc
->re_ldata
.re_rx_mbuf
, M_DEVBUF
);
534 if (sc
->re_ldata
.re_rx_paddr
!= NULL
)
535 kfree(sc
->re_ldata
.re_rx_paddr
, M_DEVBUF
);
536 if (sc
->re_ldata
.re_tx_dmamap
!= NULL
)
537 kfree(sc
->re_ldata
.re_tx_dmamap
, M_DEVBUF
);
538 if (sc
->re_ldata
.re_rx_dmamap
!= NULL
)
539 kfree(sc
->re_ldata
.re_rx_dmamap
, M_DEVBUF
);
543 re_is_faste(struct re_softc
*sc
)
545 if (pci_get_vendor(sc
->dev
) == PCI_VENDOR_REALTEK
) {
546 switch (sc
->re_device_id
) {
547 case PCI_PRODUCT_REALTEK_RT8169
:
548 case PCI_PRODUCT_REALTEK_RT8169SC
:
549 case PCI_PRODUCT_REALTEK_RT8168
:
550 case PCI_PRODUCT_REALTEK_RT8168_1
:
561 * Attach the interface. Allocate softc structures, do ifmedia
562 * setup and ethernet/BPF attach.
565 re_attach(device_t dev
)
567 struct re_softc
*sc
= device_get_softc(dev
);
569 struct sysctl_ctx_list
*ctx
;
570 struct sysctl_oid
*tree
;
571 uint8_t eaddr
[ETHER_ADDR_LEN
];
572 int error
= 0, qlen
, msi_enable
;
575 callout_init_mp(&sc
->re_timer
);
577 sc
->re_device_id
= pci_get_device(dev
);
578 sc
->re_unit
= device_get_unit(dev
);
579 ifmedia_init(&sc
->media
, IFM_IMASK
, rtl_ifmedia_upd
, rtl_ifmedia_sts
);
581 sc
->re_caps
= RE_C_HWIM
;
583 sc
->re_rx_desc_cnt
= re_rx_desc_count
;
584 if (sc
->re_rx_desc_cnt
> RE_RX_DESC_CNT_MAX
)
585 sc
->re_rx_desc_cnt
= RE_RX_DESC_CNT_MAX
;
587 sc
->re_tx_desc_cnt
= re_tx_desc_count
;
588 if (sc
->re_tx_desc_cnt
> RE_TX_DESC_CNT_MAX
)
589 sc
->re_tx_desc_cnt
= RE_TX_DESC_CNT_MAX
;
591 qlen
= RE_IFQ_MAXLEN
;
592 if (sc
->re_tx_desc_cnt
> qlen
)
593 qlen
= sc
->re_tx_desc_cnt
;
595 sc
->re_rxbuf_size
= MCLBYTES
;
596 sc
->re_newbuf
= re_newbuf_std
;
599 * Hardware interrupt moderation settings.
600 * XXX does not seem correct, undocumented.
602 sc
->re_tx_time
= 5; /* 125us */
603 sc
->re_rx_time
= 2; /* 50us */
605 /* Simulated interrupt moderation setting. */
606 sc
->re_sim_time
= 150; /* 150us */
608 /* Use simulated interrupt moderation by default. */
609 sc
->re_imtype
= RE_IMTYPE_SIM
;
610 re_config_imtype(sc
, sc
->re_imtype
);
612 ctx
= device_get_sysctl_ctx(dev
);
613 tree
= device_get_sysctl_tree(dev
);
614 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(tree
), OID_AUTO
,
615 "rx_desc_count", CTLFLAG_RD
, &sc
->re_rx_desc_cnt
,
617 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(tree
), OID_AUTO
,
618 "tx_desc_count", CTLFLAG_RD
, &sc
->re_tx_desc_cnt
,
620 SYSCTL_ADD_PROC(ctx
, SYSCTL_CHILDREN(tree
), OID_AUTO
, "sim_time",
621 CTLTYPE_INT
| CTLFLAG_RW
,
622 sc
, 0, re_sysctl_simtime
, "I",
623 "Simulated interrupt moderation time (usec).");
624 SYSCTL_ADD_PROC(ctx
, SYSCTL_CHILDREN(tree
), OID_AUTO
, "imtype",
625 CTLTYPE_INT
| CTLFLAG_RW
,
626 sc
, 0, re_sysctl_imtype
, "I",
627 "Interrupt moderation type -- "
628 "0:disable, 1:simulated, "
629 "2:hardware(if supported)");
630 if (sc
->re_caps
& RE_C_HWIM
) {
631 SYSCTL_ADD_PROC(ctx
, SYSCTL_CHILDREN(tree
),
632 OID_AUTO
, "hw_rxtime",
633 CTLTYPE_INT
| CTLFLAG_RW
,
634 sc
, 0, re_sysctl_rxtime
, "I",
635 "Hardware interrupt moderation time "
637 SYSCTL_ADD_PROC(ctx
, SYSCTL_CHILDREN(tree
),
638 OID_AUTO
, "hw_txtime",
639 CTLTYPE_INT
| CTLFLAG_RW
,
640 sc
, 0, re_sysctl_txtime
, "I",
641 "Hardware interrupt moderation time "
647 * Handle power management nonsense.
650 if (pci_get_powerstate(dev
) != PCI_POWERSTATE_D0
) {
651 uint32_t membase
, irq
;
653 /* Save important PCI config data. */
654 membase
= pci_read_config(dev
, RE_PCI_LOMEM
, 4);
655 irq
= pci_read_config(dev
, PCIR_INTLINE
, 4);
657 /* Reset the power state. */
658 device_printf(dev
, "chip is in D%d power mode "
659 "-- setting to D0\n", pci_get_powerstate(dev
));
661 pci_set_powerstate(dev
, PCI_POWERSTATE_D0
);
663 /* Restore PCI config data. */
664 pci_write_config(dev
, RE_PCI_LOMEM
, membase
, 4);
665 pci_write_config(dev
, PCIR_INTLINE
, irq
, 4);
669 * Map control/status registers.
671 pci_enable_busmaster(dev
);
673 if (pci_is_pcie(dev
)) {
674 sc
->re_res_rid
= PCIR_BAR(2);
675 sc
->re_res_type
= SYS_RES_MEMORY
;
677 sc
->re_res_rid
= PCIR_BAR(0);
678 sc
->re_res_type
= SYS_RES_IOPORT
;
680 sc
->re_res
= bus_alloc_resource_any(dev
, sc
->re_res_type
,
681 &sc
->re_res_rid
, RF_ACTIVE
);
682 if (sc
->re_res
== NULL
) {
683 device_printf(dev
, "couldn't map IO\n");
688 sc
->re_btag
= rman_get_bustag(sc
->re_res
);
689 sc
->re_bhandle
= rman_get_bushandle(sc
->re_res
);
691 error
= rtl_check_mac_version(sc
);
693 device_printf(dev
, "check mac version failed\n");
697 rtl_init_software_variable(sc
);
698 if (pci_is_pcie(dev
))
699 sc
->re_if_flags
|= RL_FLAG_PCIE
;
701 sc
->re_if_flags
&= ~RL_FLAG_PCIE
;
702 device_printf(dev
, "MAC version 0x%08x, MACFG %u%s%s\n",
703 (CSR_READ_4(sc
, RE_TXCFG
) & 0xFCF00000), sc
->re_type
,
704 sc
->re_coalesce_tx_pkt
? ", software TX defrag" : "",
705 sc
->re_hw_enable_msi_msix
? ", support MSI" : "");
710 if (pci_is_pcie(dev
) && sc
->re_hw_enable_msi_msix
)
711 msi_enable
= re_msi_enable
;
714 sc
->re_irq_type
= pci_alloc_1intr(dev
, msi_enable
,
715 &sc
->re_irq_rid
, &irq_flags
);
717 sc
->re_irq
= bus_alloc_resource_any(dev
, SYS_RES_IRQ
, &sc
->re_irq_rid
,
719 if (sc
->re_irq
== NULL
) {
720 device_printf(dev
, "couldn't map interrupt\n");
726 re_disable_aspm(dev
);
731 /* Reset the adapter. */
734 rtl_get_hw_mac_address(sc
, eaddr
);
735 if (sc
->re_type
== MACFG_3
) /* Change PCI Latency time*/
736 pci_write_config(dev
, PCIR_LATTIMER
, 0x40, 1);
738 /* Allocate DMA stuffs */
739 error
= re_allocmem(dev
);
743 if (pci_is_pcie(dev
)) {
744 sc
->re_bus_speed
= 125;
748 cfg2
= CSR_READ_1(sc
, RE_CFG2
);
749 switch (cfg2
& RE_CFG2_PCICLK_MASK
) {
750 case RE_CFG2_PCICLK_33MHZ
:
751 sc
->re_bus_speed
= 33;
753 case RE_CFG2_PCICLK_66MHZ
:
754 sc
->re_bus_speed
= 66;
757 device_printf(dev
, "unknown bus speed, assume 33MHz\n");
758 sc
->re_bus_speed
= 33;
762 device_printf(dev
, "bus speed %dMHz\n", sc
->re_bus_speed
);
764 rtl_phy_power_up(sc
);
765 rtl_hw_phy_config(sc
);
768 /* TODO: jumbo frame */
769 CSR_WRITE_2(sc
, RE_RxMaxSize
, sc
->re_rxbuf_size
);
771 /* Enable hardware checksum if available. */
775 ifp
= &sc
->arpcom
.ac_if
;
777 if_initname(ifp
, device_get_name(dev
), device_get_unit(dev
));
778 ifp
->if_flags
= IFF_BROADCAST
| IFF_SIMPLEX
| IFF_MULTICAST
;
779 ifp
->if_ioctl
= re_ioctl
;
780 ifp
->if_start
= re_start
;
782 ifp
->if_npoll
= re_npoll
;
784 ifp
->if_watchdog
= re_watchdog
;
785 ifp
->if_init
= re_init
;
786 if (!re_is_faste(sc
))
787 ifp
->if_baudrate
= 1000000000;
789 ifp
->if_baudrate
= 100000000;
790 ifp
->if_nmbclusters
= sc
->re_rx_desc_cnt
;
791 ifq_set_maxlen(&ifp
->if_snd
, qlen
);
792 ifq_set_ready(&ifp
->if_snd
);
794 ifp
->if_capabilities
= IFCAP_VLAN_MTU
| IFCAP_VLAN_HWTAGGING
|
795 IFCAP_RXCSUM
| IFCAP_TXCSUM
;
796 ifp
->if_capenable
= ifp
->if_capabilities
;
797 /* NOTE: if_hwassist will be setup after the interface is up. */
800 * Call MI attach routine.
802 ether_ifattach(ifp
, eaddr
, NULL
);
804 ifq_set_cpuid(&ifp
->if_snd
, rman_get_cpuid(sc
->re_irq
));
807 ifpoll_compat_setup(&sc
->re_npoll
, ctx
, (struct sysctl_oid
*)tree
,
808 device_get_unit(dev
), ifp
->if_serializer
);
811 /* Hook interrupt last to avoid having to lock softc */
812 error
= bus_setup_intr(dev
, sc
->re_irq
, INTR_MPSAFE
| INTR_HIFREQ
,
813 re_intr
, sc
, &sc
->re_intrhand
, ifp
->if_serializer
);
815 device_printf(dev
, "couldn't set up irq\n");
820 ifmedia_add(&sc
->media
, IFM_ETHER
| IFM_10_T
, 0, NULL
);
821 ifmedia_add(&sc
->media
, IFM_ETHER
| IFM_10_T
| IFM_FDX
, 0, NULL
);
822 ifmedia_add(&sc
->media
, IFM_ETHER
| IFM_100_TX
, 0, NULL
);
823 ifmedia_add(&sc
->media
, IFM_ETHER
| IFM_100_TX
| IFM_FDX
, 0, NULL
);
824 if (!re_is_faste(sc
)) {
825 ifmedia_add(&sc
->media
, IFM_ETHER
| IFM_1000_T
| IFM_FDX
,
828 ifmedia_add(&sc
->media
, IFM_ETHER
| IFM_AUTO
, 0, NULL
);
829 ifmedia_set(&sc
->media
, IFM_ETHER
| IFM_AUTO
);
830 rtl_ifmedia_upd(ifp
);
840 * Shutdown hardware and free up resources. This can be called any
841 * time after the mutex has been initialized. It is called in both
842 * the error case in attach and the normal detach case so it needs
843 * to be careful about only freeing resources that have actually been
847 re_detach(device_t dev
)
849 struct re_softc
*sc
= device_get_softc(dev
);
850 struct ifnet
*ifp
= &sc
->arpcom
.ac_if
;
852 /* These should only be active if attach succeeded */
853 if (device_is_attached(dev
)) {
854 lwkt_serialize_enter(ifp
->if_serializer
);
856 bus_teardown_intr(dev
, sc
->re_irq
, sc
->re_intrhand
);
857 lwkt_serialize_exit(ifp
->if_serializer
);
861 ifmedia_removeall(&sc
->media
);
864 bus_release_resource(dev
, SYS_RES_IRQ
, sc
->re_irq_rid
,
867 if (sc
->re_irq_type
== PCI_INTR_TYPE_MSI
)
868 pci_release_msi(dev
);
871 bus_release_resource(dev
, sc
->re_res_type
, sc
->re_res_rid
,
875 /* Free DMA stuffs */
882 re_setup_rxdesc(struct re_softc
*sc
, int idx
)
888 paddr
= sc
->re_ldata
.re_rx_paddr
[idx
];
889 d
= &sc
->re_ldata
.re_rx_list
[idx
];
891 d
->re_bufaddr_lo
= htole32(RE_ADDR_LO(paddr
));
892 d
->re_bufaddr_hi
= htole32(RE_ADDR_HI(paddr
));
894 cmdstat
= sc
->re_rxbuf_size
| RE_RDESC_CMD_OWN
;
895 if (idx
== (sc
->re_rx_desc_cnt
- 1))
896 cmdstat
|= RE_RDESC_CMD_EOR
;
897 d
->re_cmdstat
= htole32(cmdstat
);
901 re_newbuf_std(struct re_softc
*sc
, int idx
, int init
)
903 bus_dma_segment_t seg
;
908 m
= m_getcl(init
? M_WAITOK
: M_NOWAIT
, MT_DATA
, M_PKTHDR
);
913 if_printf(&sc
->arpcom
.ac_if
, "m_getcl failed\n");
919 m
->m_len
= m
->m_pkthdr
.len
= MCLBYTES
;
923 * re(4) chips need address of the receive buffer to be 8-byte
924 * aligned, so don't call m_adj(m, ETHER_ALIGN) here.
927 error
= bus_dmamap_load_mbuf_segment(sc
->re_ldata
.re_rx_mtag
,
928 sc
->re_ldata
.re_rx_spare
, m
,
929 &seg
, 1, &nsegs
, BUS_DMA_NOWAIT
);
933 if_printf(&sc
->arpcom
.ac_if
, "can't load RX mbuf\n");
941 bus_dmamap_sync(sc
->re_ldata
.re_rx_mtag
,
942 sc
->re_ldata
.re_rx_dmamap
[idx
],
943 BUS_DMASYNC_POSTREAD
);
944 bus_dmamap_unload(sc
->re_ldata
.re_rx_mtag
,
945 sc
->re_ldata
.re_rx_dmamap
[idx
]);
947 sc
->re_ldata
.re_rx_mbuf
[idx
] = m
;
948 sc
->re_ldata
.re_rx_paddr
[idx
] = seg
.ds_addr
;
950 map
= sc
->re_ldata
.re_rx_dmamap
[idx
];
951 sc
->re_ldata
.re_rx_dmamap
[idx
] = sc
->re_ldata
.re_rx_spare
;
952 sc
->re_ldata
.re_rx_spare
= map
;
954 re_setup_rxdesc(sc
, idx
);
960 re_newbuf_jumbo(struct re_softc
*sc
, int idx
, int init
)
963 struct re_jbuf
*jbuf
;
966 MGETHDR(m
, init
? M_WAITOK
: M_NOWAIT
, MT_DATA
);
970 if_printf(&sc
->arpcom
.ac_if
, "MGETHDR failed\n");
977 jbuf
= re_jbuf_alloc(sc
);
983 if_printf(&sc
->arpcom
.ac_if
, "jpool is empty\n");
990 m
->m_ext
.ext_arg
= jbuf
;
991 m
->m_ext
.ext_buf
= jbuf
->re_buf
;
992 m
->m_ext
.ext_free
= re_jbuf_free
;
993 m
->m_ext
.ext_ref
= re_jbuf_ref
;
994 m
->m_ext
.ext_size
= sc
->re_rxbuf_size
;
996 m
->m_data
= m
->m_ext
.ext_buf
;
998 m
->m_len
= m
->m_pkthdr
.len
= m
->m_ext
.ext_size
;
1002 * Some re(4) chips(e.g. RTL8101E) need address of the receive buffer
1003 * to be 8-byte aligned, so don't call m_adj(m, ETHER_ALIGN) here.
1006 sc
->re_ldata
.re_rx_mbuf
[idx
] = m
;
1007 sc
->re_ldata
.re_rx_paddr
[idx
] = jbuf
->re_paddr
;
1009 re_setup_rxdesc(sc
, idx
);
1012 #endif /* RE_JUMBO */
1015 re_tx_list_init(struct re_softc
*sc
)
1017 bzero(sc
->re_ldata
.re_tx_list
, RE_TX_LIST_SZ(sc
));
1019 sc
->re_ldata
.re_tx_prodidx
= 0;
1020 sc
->re_ldata
.re_tx_considx
= 0;
1021 sc
->re_ldata
.re_tx_free
= sc
->re_tx_desc_cnt
;
1027 re_rx_list_init(struct re_softc
*sc
)
1031 bzero(sc
->re_ldata
.re_rx_list
, RE_RX_LIST_SZ(sc
));
1033 for (i
= 0; i
< sc
->re_rx_desc_cnt
; i
++) {
1034 error
= sc
->re_newbuf(sc
, i
, 1);
1039 sc
->re_ldata
.re_rx_prodidx
= 0;
1040 sc
->re_head
= sc
->re_tail
= NULL
;
1045 #define RE_IP4_PACKET 0x1
1046 #define RE_TCP_PACKET 0x2
1047 #define RE_UDP_PACKET 0x4
1049 static __inline
uint8_t
1050 re_packet_type(struct re_softc
*sc
, uint32_t rxstat
, uint32_t rxctrl
)
1052 uint8_t packet_type
= 0;
1054 if (sc
->re_if_flags
& RL_FLAG_DESCV2
) {
1055 if (rxctrl
& RE_RDESC_CTL_PROTOIP4
)
1056 packet_type
|= RE_IP4_PACKET
;
1058 if (rxstat
& RE_RDESC_STAT_PROTOID
)
1059 packet_type
|= RE_IP4_PACKET
;
1061 if (RE_TCPPKT(rxstat
))
1062 packet_type
|= RE_TCP_PACKET
;
1063 else if (RE_UDPPKT(rxstat
))
1064 packet_type
|= RE_UDP_PACKET
;
1069 * RX handler for C+ and 8169. For the gigE chips, we support
1070 * the reception of jumbo frames that have been fragmented
1071 * across multiple 2K mbuf cluster buffers.
1074 re_rxeof(struct re_softc
*sc
)
1076 struct ifnet
*ifp
= &sc
->arpcom
.ac_if
;
1078 struct re_desc
*cur_rx
;
1079 uint32_t rxstat
, rxctrl
;
1080 int i
, total_len
, rx
= 0;
1082 for (i
= sc
->re_ldata
.re_rx_prodidx
;
1083 RE_OWN(&sc
->re_ldata
.re_rx_list
[i
]) == 0; RE_RXDESC_INC(sc
, i
)) {
1084 cur_rx
= &sc
->re_ldata
.re_rx_list
[i
];
1085 m
= sc
->re_ldata
.re_rx_mbuf
[i
];
1086 total_len
= RE_RXBYTES(cur_rx
);
1087 rxstat
= le32toh(cur_rx
->re_cmdstat
);
1088 rxctrl
= le32toh(cur_rx
->re_control
);
1093 if (sc
->re_flags
& RE_F_USE_JPOOL
)
1094 KKASSERT(rxstat
& RE_RDESC_STAT_EOF
);
1097 if ((rxstat
& RE_RDESC_STAT_EOF
) == 0) {
1098 if (sc
->re_flags
& RE_F_DROP_RXFRAG
) {
1099 re_setup_rxdesc(sc
, i
);
1103 if (sc
->re_newbuf(sc
, i
, 0)) {
1104 /* Drop upcoming fragments */
1105 sc
->re_flags
|= RE_F_DROP_RXFRAG
;
1109 m
->m_len
= MCLBYTES
;
1110 if (sc
->re_head
== NULL
) {
1111 sc
->re_head
= sc
->re_tail
= m
;
1113 sc
->re_tail
->m_next
= m
;
1117 } else if (sc
->re_flags
& RE_F_DROP_RXFRAG
) {
1119 * Last fragment of a multi-fragment packet.
1121 * Since error already happened, this fragment
1122 * must be dropped as well as the fragment chain.
1124 re_setup_rxdesc(sc
, i
);
1125 re_free_rxchain(sc
);
1126 sc
->re_flags
&= ~RE_F_DROP_RXFRAG
;
1131 if (rxstat
& RE_RDESC_STAT_RXERRSUM
) {
1132 IFNET_STAT_INC(ifp
, ierrors
, 1);
1134 * If this is part of a multi-fragment packet,
1135 * discard all the pieces.
1137 re_free_rxchain(sc
);
1138 re_setup_rxdesc(sc
, i
);
1143 * If allocating a replacement mbuf fails,
1144 * reload the current one.
1147 if (sc
->re_newbuf(sc
, i
, 0)) {
1148 IFNET_STAT_INC(ifp
, ierrors
, 1);
1152 if (sc
->re_head
!= NULL
) {
1153 m
->m_len
= total_len
% MCLBYTES
;
1155 * Special case: if there's 4 bytes or less
1156 * in this buffer, the mbuf can be discarded:
1157 * the last 4 bytes is the CRC, which we don't
1158 * care about anyway.
1160 if (m
->m_len
<= ETHER_CRC_LEN
) {
1161 sc
->re_tail
->m_len
-=
1162 (ETHER_CRC_LEN
- m
->m_len
);
1165 m
->m_len
-= ETHER_CRC_LEN
;
1166 sc
->re_tail
->m_next
= m
;
1169 sc
->re_head
= sc
->re_tail
= NULL
;
1170 m
->m_pkthdr
.len
= total_len
- ETHER_CRC_LEN
;
1172 m
->m_pkthdr
.len
= m
->m_len
=
1173 (total_len
- ETHER_CRC_LEN
);
1176 IFNET_STAT_INC(ifp
, ipackets
, 1);
1177 m
->m_pkthdr
.rcvif
= ifp
;
1179 /* Do RX checksumming if enabled */
1181 if (ifp
->if_capenable
& IFCAP_RXCSUM
) {
1182 uint8_t packet_type
;
1184 packet_type
= re_packet_type(sc
, rxstat
, rxctrl
);
1186 /* Check IP header checksum */
1187 if (packet_type
& RE_IP4_PACKET
) {
1188 m
->m_pkthdr
.csum_flags
|= CSUM_IP_CHECKED
;
1189 if ((rxstat
& RE_RDESC_STAT_IPSUMBAD
) == 0)
1190 m
->m_pkthdr
.csum_flags
|= CSUM_IP_VALID
;
1193 /* Check TCP/UDP checksum */
1194 if (((packet_type
& RE_TCP_PACKET
) &&
1195 (rxstat
& RE_RDESC_STAT_TCPSUMBAD
) == 0) ||
1196 ((packet_type
& RE_UDP_PACKET
) &&
1197 (rxstat
& RE_RDESC_STAT_UDPSUMBAD
) == 0)) {
1198 m
->m_pkthdr
.csum_flags
|=
1199 CSUM_DATA_VALID
|CSUM_PSEUDO_HDR
|
1200 CSUM_FRAG_NOT_CHECKED
;
1201 m
->m_pkthdr
.csum_data
= 0xffff;
1205 if (rxctrl
& RE_RDESC_CTL_HASTAG
) {
1206 m
->m_flags
|= M_VLANTAG
;
1207 m
->m_pkthdr
.ether_vlantag
=
1208 be16toh((rxctrl
& RE_RDESC_CTL_TAGDATA
));
1210 ifp
->if_input(ifp
, m
, NULL
, -1);
1213 sc
->re_ldata
.re_rx_prodidx
= i
;
1218 #undef RE_IP4_PACKET
1219 #undef RE_TCP_PACKET
1220 #undef RE_UDP_PACKET
1223 re_tx_collect(struct re_softc
*sc
)
1225 struct ifnet
*ifp
= &sc
->arpcom
.ac_if
;
1229 for (idx
= sc
->re_ldata
.re_tx_considx
;
1230 sc
->re_ldata
.re_tx_free
< sc
->re_tx_desc_cnt
;
1231 RE_TXDESC_INC(sc
, idx
)) {
1232 txstat
= le32toh(sc
->re_ldata
.re_tx_list
[idx
].re_cmdstat
);
1233 if (txstat
& RE_TDESC_CMD_OWN
)
1238 sc
->re_ldata
.re_tx_list
[idx
].re_bufaddr_lo
= 0;
1241 * We only stash mbufs in the last descriptor
1242 * in a fragment chain, which also happens to
1243 * be the only place where the TX status bits
1246 if (txstat
& RE_TDESC_CMD_EOF
) {
1247 bus_dmamap_unload(sc
->re_ldata
.re_tx_mtag
,
1248 sc
->re_ldata
.re_tx_dmamap
[idx
]);
1249 m_freem(sc
->re_ldata
.re_tx_mbuf
[idx
]);
1250 sc
->re_ldata
.re_tx_mbuf
[idx
] = NULL
;
1251 if (txstat
& (RE_TDESC_STAT_EXCESSCOL
|
1252 RE_TDESC_STAT_COLCNT
))
1253 IFNET_STAT_INC(ifp
, collisions
, 1);
1254 if (txstat
& RE_TDESC_STAT_TXERRSUM
)
1255 IFNET_STAT_INC(ifp
, oerrors
, 1);
1257 IFNET_STAT_INC(ifp
, opackets
, 1);
1259 sc
->re_ldata
.re_tx_free
++;
1261 sc
->re_ldata
.re_tx_considx
= idx
;
1267 re_txeof(struct re_softc
*sc
)
1269 struct ifnet
*ifp
= &sc
->arpcom
.ac_if
;
1272 tx
= re_tx_collect(sc
);
1274 /* There is enough free TX descs */
1275 if (sc
->re_ldata
.re_tx_free
> RE_TXDESC_SPARE
)
1276 ifq_clr_oactive(&ifp
->if_snd
);
1279 * Some chips will ignore a second TX request issued while an
1280 * existing transmission is in progress. If the transmitter goes
1281 * idle but there are still packets waiting to be sent, we need
1282 * to restart the channel here to flush them out. This only seems
1283 * to be required with the PCIe devices.
1285 if (sc
->re_ldata
.re_tx_free
< sc
->re_tx_desc_cnt
)
1286 CSR_WRITE_1(sc
, RE_TPPOLL
, RE_NPQ
);
1296 struct re_softc
*sc
= xsc
;
1298 lwkt_serialize_enter(sc
->arpcom
.ac_if
.if_serializer
);
1299 re_tick_serialized(xsc
);
1300 lwkt_serialize_exit(sc
->arpcom
.ac_if
.if_serializer
);
1304 re_tick_serialized(void *xsc
)
1306 struct re_softc
*sc
= xsc
;
1307 struct ifnet
*ifp
= &sc
->arpcom
.ac_if
;
1309 ASSERT_SERIALIZED(ifp
->if_serializer
);
1311 if ((ifp
->if_flags
& IFF_RUNNING
) == 0)
1314 if (rtl_link_ok(sc
)) {
1315 if ((sc
->re_flags
& RE_F_LINKED
) == 0)
1317 } else if (sc
->re_flags
& RE_F_LINKED
) {
1320 callout_reset(&sc
->re_timer
, hz
, re_tick
, sc
);
1323 #ifdef IFPOLL_ENABLE
1326 re_npoll_compat(struct ifnet
*ifp
, void *arg __unused
, int count
)
1328 struct re_softc
*sc
= ifp
->if_softc
;
1330 ASSERT_SERIALIZED(ifp
->if_serializer
);
1332 if (sc
->re_npoll
.ifpc_stcount
-- == 0) {
1335 sc
->re_npoll
.ifpc_stcount
= sc
->re_npoll
.ifpc_stfrac
;
1337 status
= CSR_READ_2(sc
, RE_ISR
);
1339 CSR_WRITE_2(sc
, RE_ISR
, status
);
1342 * XXX check behaviour on receiver stalls.
1345 if (status
& RE_ISR_SYSTEM_ERR
) {
1353 sc
->rxcycles
= count
;
1357 if (!ifq_is_empty(&ifp
->if_snd
))
1362 re_npoll(struct ifnet
*ifp
, struct ifpoll_info
*info
)
1364 struct re_softc
*sc
= ifp
->if_softc
;
1366 ASSERT_SERIALIZED(ifp
->if_serializer
);
1369 int cpuid
= sc
->re_npoll
.ifpc_cpuid
;
1371 info
->ifpi_rx
[cpuid
].poll_func
= re_npoll_compat
;
1372 info
->ifpi_rx
[cpuid
].arg
= NULL
;
1373 info
->ifpi_rx
[cpuid
].serializer
= ifp
->if_serializer
;
1375 if (ifp
->if_flags
& IFF_RUNNING
)
1376 re_setup_intr(sc
, 0, RE_IMTYPE_NONE
);
1377 ifq_set_cpuid(&ifp
->if_snd
, cpuid
);
1379 if (ifp
->if_flags
& IFF_RUNNING
)
1380 re_setup_intr(sc
, 1, sc
->re_imtype
);
1381 ifq_set_cpuid(&ifp
->if_snd
, rman_get_cpuid(sc
->re_irq
));
1384 #endif /* IFPOLL_ENABLE */
1389 struct re_softc
*sc
= arg
;
1390 struct ifnet
*ifp
= &sc
->arpcom
.ac_if
;
1394 ASSERT_SERIALIZED(ifp
->if_serializer
);
1396 if ((sc
->re_flags
& RE_F_SUSPENDED
) ||
1397 (ifp
->if_flags
& IFF_RUNNING
) == 0)
1400 /* Disable interrupts. */
1401 CSR_WRITE_2(sc
, RE_IMR
, 0);
1403 status
= CSR_READ_2(sc
, RE_ISR
);
1407 CSR_WRITE_2(sc
, RE_ISR
, status
);
1408 if (status
& sc
->re_intrs
) {
1409 if (status
& RE_ISR_SYSTEM_ERR
) {
1415 proc
|= re_rxeof(sc
);
1416 proc
|= re_txeof(sc
);
1419 if (sc
->re_imtype
== RE_IMTYPE_SIM
) {
1420 if ((sc
->re_flags
& RE_F_TIMER_INTR
)) {
1423 * Nothing needs to be processed, fallback
1424 * to use TX/RX interrupts.
1426 * NOTE: This will re-enable interrupts.
1428 re_setup_intr(sc
, 1, RE_IMTYPE_NONE
);
1431 * Recollect, mainly to avoid the possible
1432 * race introduced by changing interrupt
1438 /* Re-enable interrupts. */
1439 CSR_WRITE_2(sc
, RE_IMR
, sc
->re_intrs
);
1440 CSR_WRITE_4(sc
, RE_TIMERCNT
, 1); /* reload */
1444 * Assume that using simulated interrupt moderation
1445 * (hardware timer based) could reduce the interript
1448 * NOTE: This will re-enable interrupts.
1450 re_setup_intr(sc
, 1, RE_IMTYPE_SIM
);
1452 /* Re-enable interrupts. */
1453 CSR_WRITE_2(sc
, RE_IMR
, sc
->re_intrs
);
1456 status
= CSR_READ_2(sc
, RE_ISR
);
1457 if (status
& sc
->re_intrs
) {
1458 if (!ifq_is_empty(&ifp
->if_snd
))
1460 /* NOTE: Interrupts are still disabled. */
1463 /* Re-enable interrupts. */
1464 CSR_WRITE_2(sc
, RE_IMR
, sc
->re_intrs
);
1467 if (!ifq_is_empty(&ifp
->if_snd
))
1472 re_encap(struct re_softc
*sc
, struct mbuf
**m_head
, int *idx0
)
1474 struct mbuf
*m
= *m_head
;
1475 bus_dma_segment_t segs
[RE_MAXSEGS
];
1477 int error
, maxsegs
, idx
, i
, nsegs
;
1478 struct re_desc
*d
, *tx_ring
;
1479 uint32_t cmd_csum
, ctl_csum
, vlantag
;
1481 KASSERT(sc
->re_ldata
.re_tx_free
> RE_TXDESC_SPARE
,
1482 ("not enough free TX desc"));
1484 if (sc
->re_coalesce_tx_pkt
&& m
->m_pkthdr
.len
!= m
->m_len
) {
1487 m_new
= m_defrag(m
, M_NOWAIT
);
1488 if (m_new
== NULL
) {
1492 *m_head
= m
= m_new
;
1493 if (m
->m_pkthdr
.len
!= m
->m_len
) {
1494 /* Still not configuous; give up. */
1501 map
= sc
->re_ldata
.re_tx_dmamap
[*idx0
];
1504 * Set up checksum offload. Note: checksum offload bits must
1505 * appear in all descriptors of a multi-descriptor transmit
1506 * attempt. (This is according to testing done with an 8169
1507 * chip. I'm not sure if this is a requirement or a bug.)
1509 cmd_csum
= ctl_csum
= 0;
1510 if (m
->m_pkthdr
.csum_flags
& CSUM_IP
) {
1511 cmd_csum
|= RE_TDESC_CMD_IPCSUM
;
1512 ctl_csum
|= RE_TDESC_CTL_IPCSUM
;
1514 if (m
->m_pkthdr
.csum_flags
& CSUM_TCP
) {
1515 cmd_csum
|= RE_TDESC_CMD_TCPCSUM
;
1516 ctl_csum
|= RE_TDESC_CTL_TCPCSUM
;
1518 if (m
->m_pkthdr
.csum_flags
& CSUM_UDP
) {
1519 cmd_csum
|= RE_TDESC_CMD_UDPCSUM
;
1520 ctl_csum
|= RE_TDESC_CTL_UDPCSUM
;
1523 /* For version2 descriptor, csum flags are set on re_control */
1524 if (sc
->re_if_flags
& RL_FLAG_DESCV2
)
1530 * With some of the RealTek chips, using the checksum offload
1531 * support in conjunction with the autopadding feature results
1532 * in the transmission of corrupt frames. For example, if we
1533 * need to send a really small IP fragment that's less than 60
1534 * bytes in size, and IP header checksumming is enabled, the
1535 * resulting ethernet frame that appears on the wire will
1536 * have garbled payload. To work around this, if TX checksum
1537 * offload is enabled, we always manually pad short frames out
1538 * to the minimum ethernet frame size.
1540 * Note: this appears unnecessary for TCP, and doing it for TCP
1541 * with PCIe adapters seems to result in bad checksums.
1543 if ((m
->m_pkthdr
.csum_flags
& (CSUM_DELAY_IP
| CSUM_DELAY_DATA
)) &&
1544 (m
->m_pkthdr
.csum_flags
& CSUM_TCP
) == 0 &&
1545 m
->m_pkthdr
.len
< RE_MIN_FRAMELEN
) {
1546 error
= m_devpad(m
, RE_MIN_FRAMELEN
);
1552 if (m
->m_flags
& M_VLANTAG
) {
1553 vlantag
= htobe16(m
->m_pkthdr
.ether_vlantag
) |
1554 RE_TDESC_CTL_INSTAG
;
1557 maxsegs
= sc
->re_ldata
.re_tx_free
;
1558 if (maxsegs
> RE_MAXSEGS
)
1559 maxsegs
= RE_MAXSEGS
;
1561 error
= bus_dmamap_load_mbuf_defrag(sc
->re_ldata
.re_tx_mtag
, map
,
1562 m_head
, segs
, maxsegs
, &nsegs
, BUS_DMA_NOWAIT
);
1567 bus_dmamap_sync(sc
->re_ldata
.re_tx_mtag
, map
, BUS_DMASYNC_PREWRITE
);
1570 * Map the segment array into descriptors. We also keep track
1571 * of the end of the ring and set the end-of-ring bits as needed,
1572 * and we set the ownership bits in all except the very first
1573 * descriptor, whose ownership bits will be turned on later.
1575 tx_ring
= sc
->re_ldata
.re_tx_list
;
1583 cmdstat
= segs
[i
].ds_len
;
1584 d
->re_bufaddr_lo
= htole32(RE_ADDR_LO(segs
[i
].ds_addr
));
1585 d
->re_bufaddr_hi
= htole32(RE_ADDR_HI(segs
[i
].ds_addr
));
1587 cmdstat
|= RE_TDESC_CMD_SOF
;
1589 cmdstat
|= RE_TDESC_CMD_OWN
;
1590 if (idx
== (sc
->re_tx_desc_cnt
- 1))
1591 cmdstat
|= RE_TDESC_CMD_EOR
;
1592 d
->re_cmdstat
= htole32(cmdstat
| cmd_csum
);
1593 d
->re_control
= htole32(ctl_csum
| vlantag
);
1598 RE_TXDESC_INC(sc
, idx
);
1600 d
->re_cmdstat
|= htole32(RE_TDESC_CMD_EOF
);
1602 /* Transfer ownership of packet to the chip. */
1603 d
->re_cmdstat
|= htole32(RE_TDESC_CMD_OWN
);
1605 tx_ring
[*idx0
].re_cmdstat
|= htole32(RE_TDESC_CMD_OWN
);
1608 * Insure that the map for this transmission
1609 * is placed at the array index of the last descriptor
1612 sc
->re_ldata
.re_tx_dmamap
[*idx0
] = sc
->re_ldata
.re_tx_dmamap
[idx
];
1613 sc
->re_ldata
.re_tx_dmamap
[idx
] = map
;
1615 sc
->re_ldata
.re_tx_mbuf
[idx
] = m
;
1616 sc
->re_ldata
.re_tx_free
-= nsegs
;
1618 RE_TXDESC_INC(sc
, idx
);
1629 * Main transmit routine for C+ and gigE NICs.
1633 re_start(struct ifnet
*ifp
, struct ifaltq_subque
*ifsq
)
1635 struct re_softc
*sc
= ifp
->if_softc
;
1636 struct mbuf
*m_head
;
1637 int idx
, need_trans
, oactive
, error
;
1639 ASSERT_ALTQ_SQ_DEFAULT(ifp
, ifsq
);
1640 ASSERT_SERIALIZED(ifp
->if_serializer
);
1642 if ((sc
->re_flags
& RE_F_LINKED
) == 0) {
1643 ifq_purge(&ifp
->if_snd
);
1647 if ((ifp
->if_flags
& IFF_RUNNING
) == 0 || ifq_is_oactive(&ifp
->if_snd
))
1650 idx
= sc
->re_ldata
.re_tx_prodidx
;
1654 while (sc
->re_ldata
.re_tx_mbuf
[idx
] == NULL
) {
1655 if (sc
->re_ldata
.re_tx_free
<= RE_TXDESC_SPARE
) {
1657 if (re_tx_collect(sc
)) {
1662 ifq_set_oactive(&ifp
->if_snd
);
1666 m_head
= ifq_dequeue(&ifp
->if_snd
);
1670 error
= re_encap(sc
, &m_head
, &idx
);
1672 /* m_head is freed by re_encap(), if we reach here */
1673 IFNET_STAT_INC(ifp
, oerrors
, 1);
1675 if (error
== EFBIG
&& !oactive
) {
1676 if (re_tx_collect(sc
)) {
1681 ifq_set_oactive(&ifp
->if_snd
);
1689 * If there's a BPF listener, bounce a copy of this frame
1692 ETHER_BPF_MTAP(ifp
, m_head
);
1696 * If sc->re_ldata.re_tx_mbuf[idx] is not NULL it is possible
1697 * for OACTIVE to not be properly set when we also do not
1698 * have sufficient free tx descriptors, leaving packet in
1699 * ifp->if_snd. This can cause if_start_dispatch() to loop
1700 * infinitely so make sure OACTIVE is set properly.
1702 if (sc
->re_ldata
.re_tx_free
<= RE_TXDESC_SPARE
) {
1703 if (!ifq_is_oactive(&ifp
->if_snd
)) {
1705 if_printf(ifp
, "Debug: OACTIVE was not set when "
1706 "re_tx_free was below minimum!\n");
1708 ifq_set_oactive(&ifp
->if_snd
);
1714 sc
->re_ldata
.re_tx_prodidx
= idx
;
1717 * RealTek put the TX poll request register in a different
1718 * location on the 8169 gigE chip. I don't know why.
1720 CSR_WRITE_1(sc
, RE_TPPOLL
, RE_NPQ
);
1723 * Set a timeout in case the chip goes out to lunch.
1729 re_link_up(struct re_softc
*sc
)
1731 struct ifnet
*ifp
= &sc
->arpcom
.ac_if
;
1734 ASSERT_SERIALIZED(ifp
->if_serializer
);
1736 rtl_link_on_patch(sc
);
1740 error
= re_rx_list_init(sc
);
1745 error
= re_tx_list_init(sc
);
1752 * Load the addresses of the RX and TX lists into the chip.
1754 CSR_WRITE_4(sc
, RE_RXLIST_ADDR_HI
,
1755 RE_ADDR_HI(sc
->re_ldata
.re_rx_list_addr
));
1756 CSR_WRITE_4(sc
, RE_RXLIST_ADDR_LO
,
1757 RE_ADDR_LO(sc
->re_ldata
.re_rx_list_addr
));
1759 CSR_WRITE_4(sc
, RE_TXLIST_ADDR_HI
,
1760 RE_ADDR_HI(sc
->re_ldata
.re_tx_list_addr
));
1761 CSR_WRITE_4(sc
, RE_TXLIST_ADDR_LO
,
1762 RE_ADDR_LO(sc
->re_ldata
.re_tx_list_addr
));
1766 #ifdef IFPOLL_ENABLE
1768 * Disable interrupts if we are polling.
1770 if (ifp
->if_flags
& IFF_NPOLLING
)
1771 re_setup_intr(sc
, 0, RE_IMTYPE_NONE
);
1772 else /* otherwise ... */
1773 #endif /* IFPOLL_ENABLE */
1775 * Enable interrupts.
1777 re_setup_intr(sc
, 1, sc
->re_imtype
);
1778 CSR_WRITE_2(sc
, RE_ISR
, sc
->re_intrs
);
1780 sc
->re_flags
|= RE_F_LINKED
;
1781 ifp
->if_link_state
= LINK_STATE_UP
;
1782 if_link_state_change(ifp
);
1785 if_printf(ifp
, "link UP\n");
1787 if (!ifq_is_empty(&ifp
->if_snd
))
1792 re_link_down(struct re_softc
*sc
)
1794 struct ifnet
*ifp
= &sc
->arpcom
.ac_if
;
1796 /* NOTE: re_stop() will reset RE_F_LINKED. */
1797 ifp
->if_link_state
= LINK_STATE_DOWN
;
1798 if_link_state_change(ifp
);
1801 rtl_ifmedia_upd(ifp
);
1804 if_printf(ifp
, "link DOWN\n");
1810 struct re_softc
*sc
= xsc
;
1811 struct ifnet
*ifp
= &sc
->arpcom
.ac_if
;
1813 ASSERT_SERIALIZED(ifp
->if_serializer
);
1816 if (rtl_link_ok(sc
)) {
1818 if_printf(ifp
, "link is UP in if_init\n");
1822 ifp
->if_flags
|= IFF_RUNNING
;
1823 ifq_clr_oactive(&ifp
->if_snd
);
1825 callout_reset(&sc
->re_timer
, hz
, re_tick
, sc
);
1829 re_ioctl(struct ifnet
*ifp
, u_long command
, caddr_t data
, struct ucred
*cr
)
1831 struct re_softc
*sc
= ifp
->if_softc
;
1832 struct ifreq
*ifr
= (struct ifreq
*)data
;
1833 int error
= 0, mask
;
1835 ASSERT_SERIALIZED(ifp
->if_serializer
);
1840 if (ifr
->ifr_mtu
> sc
->re_maxmtu
) {
1842 } else if (ifp
->if_mtu
!= ifr
->ifr_mtu
) {
1843 ifp
->if_mtu
= ifr
->ifr_mtu
;
1844 if (ifp
->if_flags
& IFF_RUNNING
)
1853 if (ifp
->if_flags
& IFF_UP
) {
1854 if (ifp
->if_flags
& IFF_RUNNING
) {
1855 if ((ifp
->if_flags
^ sc
->re_saved_ifflags
) &
1856 (IFF_PROMISC
| IFF_ALLMULTI
))
1857 rtl_set_rx_packet_filter(sc
);
1861 } else if (ifp
->if_flags
& IFF_RUNNING
) {
1864 sc
->re_saved_ifflags
= ifp
->if_flags
;
1869 rtl_set_rx_packet_filter(sc
);
1874 error
= ifmedia_ioctl(ifp
, ifr
, &sc
->media
, command
);
1878 mask
= (ifr
->ifr_reqcap
^ ifp
->if_capenable
) &
1879 ifp
->if_capabilities
;
1880 ifp
->if_capenable
^= mask
;
1882 /* NOTE: re_init will setup if_hwassist. */
1883 ifp
->if_hwassist
= 0;
1885 /* Setup flags for the backend. */
1886 if (ifp
->if_capenable
& IFCAP_RXCSUM
)
1887 sc
->re_rx_cstag
= 1;
1889 sc
->re_rx_cstag
= 0;
1890 if (ifp
->if_capenable
& IFCAP_TXCSUM
)
1891 sc
->re_tx_cstag
= 1;
1893 sc
->re_rx_cstag
= 0;
1895 if (mask
&& (ifp
->if_flags
& IFF_RUNNING
))
1900 error
= ether_ioctl(ifp
, command
, data
);
1907 re_watchdog(struct ifnet
*ifp
)
1909 struct re_softc
*sc
= ifp
->if_softc
;
1911 ASSERT_SERIALIZED(ifp
->if_serializer
);
1913 IFNET_STAT_INC(ifp
, oerrors
, 1);
1918 if (sc
->re_ldata
.re_tx_free
!= sc
->re_tx_desc_cnt
) {
1919 if_printf(ifp
, "watchdog timeout, txd free %d\n",
1920 sc
->re_ldata
.re_tx_free
);
1927 * Stop the adapter and free any mbufs allocated to the
1931 re_stop(struct re_softc
*sc
, boolean_t full_stop
)
1933 struct ifnet
*ifp
= &sc
->arpcom
.ac_if
;
1936 ASSERT_SERIALIZED(ifp
->if_serializer
);
1938 /* Stop the adapter. */
1943 callout_stop(&sc
->re_timer
);
1944 ifp
->if_flags
&= ~IFF_RUNNING
;
1946 ifq_clr_oactive(&ifp
->if_snd
);
1947 sc
->re_flags
&= ~(RE_F_TIMER_INTR
| RE_F_DROP_RXFRAG
| RE_F_LINKED
);
1949 re_free_rxchain(sc
);
1951 /* Free the TX list buffers. */
1952 for (i
= 0; i
< sc
->re_tx_desc_cnt
; i
++) {
1953 if (sc
->re_ldata
.re_tx_mbuf
[i
] != NULL
) {
1954 bus_dmamap_unload(sc
->re_ldata
.re_tx_mtag
,
1955 sc
->re_ldata
.re_tx_dmamap
[i
]);
1956 m_freem(sc
->re_ldata
.re_tx_mbuf
[i
]);
1957 sc
->re_ldata
.re_tx_mbuf
[i
] = NULL
;
1961 /* Free the RX list buffers. */
1962 for (i
= 0; i
< sc
->re_rx_desc_cnt
; i
++) {
1963 if (sc
->re_ldata
.re_rx_mbuf
[i
] != NULL
) {
1964 if ((sc
->re_flags
& RE_F_USE_JPOOL
) == 0) {
1965 bus_dmamap_unload(sc
->re_ldata
.re_rx_mtag
,
1966 sc
->re_ldata
.re_rx_dmamap
[i
]);
1968 m_freem(sc
->re_ldata
.re_rx_mbuf
[i
]);
1969 sc
->re_ldata
.re_rx_mbuf
[i
] = NULL
;
1975 * Device suspend routine. Stop the interface and save some PCI
1976 * settings in case the BIOS doesn't restore them properly on
1980 re_suspend(device_t dev
)
1982 #ifndef BURN_BRIDGES
1985 struct re_softc
*sc
= device_get_softc(dev
);
1986 struct ifnet
*ifp
= &sc
->arpcom
.ac_if
;
1988 lwkt_serialize_enter(ifp
->if_serializer
);
1992 #ifndef BURN_BRIDGES
1993 for (i
= 0; i
< 5; i
++)
1994 sc
->saved_maps
[i
] = pci_read_config(dev
, PCIR_MAPS
+ i
* 4, 4);
1995 sc
->saved_biosaddr
= pci_read_config(dev
, PCIR_BIOS
, 4);
1996 sc
->saved_intline
= pci_read_config(dev
, PCIR_INTLINE
, 1);
1997 sc
->saved_cachelnsz
= pci_read_config(dev
, PCIR_CACHELNSZ
, 1);
1998 sc
->saved_lattimer
= pci_read_config(dev
, PCIR_LATTIMER
, 1);
2001 sc
->re_flags
|= RE_F_SUSPENDED
;
2003 lwkt_serialize_exit(ifp
->if_serializer
);
2009 * Device resume routine. Restore some PCI settings in case the BIOS
2010 * doesn't, re-enable busmastering, and restart the interface if
2014 re_resume(device_t dev
)
2016 struct re_softc
*sc
= device_get_softc(dev
);
2017 struct ifnet
*ifp
= &sc
->arpcom
.ac_if
;
2018 #ifndef BURN_BRIDGES
2022 lwkt_serialize_enter(ifp
->if_serializer
);
2024 #ifndef BURN_BRIDGES
2025 /* better way to do this? */
2026 for (i
= 0; i
< 5; i
++)
2027 pci_write_config(dev
, PCIR_MAPS
+ i
* 4, sc
->saved_maps
[i
], 4);
2028 pci_write_config(dev
, PCIR_BIOS
, sc
->saved_biosaddr
, 4);
2029 pci_write_config(dev
, PCIR_INTLINE
, sc
->saved_intline
, 1);
2030 pci_write_config(dev
, PCIR_CACHELNSZ
, sc
->saved_cachelnsz
, 1);
2031 pci_write_config(dev
, PCIR_LATTIMER
, sc
->saved_lattimer
, 1);
2033 /* reenable busmastering */
2034 pci_enable_busmaster(dev
);
2035 pci_enable_io(dev
, SYS_RES_IOPORT
);
2038 /* reinitialize interface if necessary */
2039 if (ifp
->if_flags
& IFF_UP
)
2042 sc
->re_flags
&= ~RE_F_SUSPENDED
;
2044 lwkt_serialize_exit(ifp
->if_serializer
);
2050 * Stop all chip I/O so that the kernel's probe routines don't
2051 * get confused by errant DMAs when rebooting.
2054 re_shutdown(device_t dev
)
2056 struct re_softc
*sc
= device_get_softc(dev
);
2057 struct ifnet
*ifp
= &sc
->arpcom
.ac_if
;
2059 lwkt_serialize_enter(ifp
->if_serializer
);
2062 rtl_phy_power_down(sc
);
2063 lwkt_serialize_exit(ifp
->if_serializer
);
2067 re_sysctl_rxtime(SYSCTL_HANDLER_ARGS
)
2069 struct re_softc
*sc
= arg1
;
2071 return re_sysctl_hwtime(oidp
, arg1
, arg2
, req
, &sc
->re_rx_time
);
2075 re_sysctl_txtime(SYSCTL_HANDLER_ARGS
)
2077 struct re_softc
*sc
= arg1
;
2079 return re_sysctl_hwtime(oidp
, arg1
, arg2
, req
, &sc
->re_tx_time
);
2083 re_sysctl_hwtime(SYSCTL_HANDLER_ARGS
, int *hwtime
)
2085 struct re_softc
*sc
= arg1
;
2086 struct ifnet
*ifp
= &sc
->arpcom
.ac_if
;
2089 lwkt_serialize_enter(ifp
->if_serializer
);
2092 error
= sysctl_handle_int(oidp
, &v
, 0, req
);
2093 if (error
|| req
->newptr
== NULL
)
2104 if ((ifp
->if_flags
& (IFF_RUNNING
| IFF_NPOLLING
)) ==
2105 IFF_RUNNING
&& sc
->re_imtype
== RE_IMTYPE_HW
)
2109 lwkt_serialize_exit(ifp
->if_serializer
);
2114 re_sysctl_simtime(SYSCTL_HANDLER_ARGS
)
2116 struct re_softc
*sc
= arg1
;
2117 struct ifnet
*ifp
= &sc
->arpcom
.ac_if
;
2120 lwkt_serialize_enter(ifp
->if_serializer
);
2122 v
= sc
->re_sim_time
;
2123 error
= sysctl_handle_int(oidp
, &v
, 0, req
);
2124 if (error
|| req
->newptr
== NULL
)
2132 if (v
!= sc
->re_sim_time
) {
2133 sc
->re_sim_time
= v
;
2135 if ((ifp
->if_flags
& (IFF_RUNNING
| IFF_NPOLLING
)) ==
2136 IFF_RUNNING
&& sc
->re_imtype
== RE_IMTYPE_SIM
) {
2139 * Following code causes various strange
2140 * performance problems. Hmm ...
2142 CSR_WRITE_2(sc
, RE_IMR
, 0);
2143 CSR_WRITE_4(sc
, RE_TIMERINT
, 0);
2144 CSR_READ_4(sc
, RE_TIMERINT
); /* flush */
2146 CSR_WRITE_2(sc
, RE_IMR
, sc
->re_intrs
);
2147 re_setup_sim_im(sc
);
2149 re_setup_intr(sc
, 0, RE_IMTYPE_NONE
);
2151 re_setup_intr(sc
, 1, RE_IMTYPE_SIM
);
2156 lwkt_serialize_exit(ifp
->if_serializer
);
2161 re_sysctl_imtype(SYSCTL_HANDLER_ARGS
)
2163 struct re_softc
*sc
= arg1
;
2164 struct ifnet
*ifp
= &sc
->arpcom
.ac_if
;
2167 lwkt_serialize_enter(ifp
->if_serializer
);
2170 error
= sysctl_handle_int(oidp
, &v
, 0, req
);
2171 if (error
|| req
->newptr
== NULL
)
2174 if (v
!= RE_IMTYPE_HW
&& v
!= RE_IMTYPE_SIM
&& v
!= RE_IMTYPE_NONE
) {
2178 if (v
== RE_IMTYPE_HW
&& (sc
->re_caps
& RE_C_HWIM
) == 0) {
2179 /* Can't do hardware interrupt moderation */
2184 if (v
!= sc
->re_imtype
) {
2186 if ((ifp
->if_flags
& (IFF_RUNNING
| IFF_NPOLLING
)) ==
2188 re_setup_intr(sc
, 1, sc
->re_imtype
);
2191 lwkt_serialize_exit(ifp
->if_serializer
);
2196 re_setup_hw_im(struct re_softc
*sc
)
2198 KKASSERT(sc
->re_caps
& RE_C_HWIM
);
2201 * Interrupt moderation
2204 * A - unknown (maybe TX related)
2205 * B - TX timer (unit: 25us)
2206 * C - unknown (maybe RX related)
2207 * D - RX timer (unit: 25us)
2210 * re(4)'s interrupt moderation is actually controlled by
2211 * two variables, like most other NICs (bge, bce etc.)
2213 * o number of packets [P]
2215 * The logic relationship between these two variables is
2216 * similar to other NICs too:
2217 * if (timer expire || packets > [P])
2218 * Interrupt is delivered
2220 * Currently we only know how to set 'timer', but not
2221 * 'number of packets', which should be ~30, as far as I
2222 * tested (sink ~900Kpps, interrupt rate is 30KHz)
2224 CSR_WRITE_2(sc
, RE_IM
,
2225 RE_IM_RXTIME(sc
->re_rx_time
) |
2226 RE_IM_TXTIME(sc
->re_tx_time
) |
2231 re_disable_hw_im(struct re_softc
*sc
)
2233 if (sc
->re_caps
& RE_C_HWIM
)
2234 CSR_WRITE_2(sc
, RE_IM
, 0);
2238 re_setup_sim_im(struct re_softc
*sc
)
2242 if (sc
->re_if_flags
& RL_FLAG_PCIE
) {
2243 ticks
= sc
->re_sim_time
* sc
->re_bus_speed
;
2246 * Datasheet says tick decreases at bus speed,
2247 * but it seems the clock runs a little bit
2248 * faster, so we do some compensation here.
2250 ticks
= (sc
->re_sim_time
* sc
->re_bus_speed
* 8) / 5;
2252 CSR_WRITE_4(sc
, RE_TIMERINT
, ticks
);
2254 CSR_WRITE_4(sc
, RE_TIMERCNT
, 1); /* reload */
2255 sc
->re_flags
|= RE_F_TIMER_INTR
;
2259 re_disable_sim_im(struct re_softc
*sc
)
2261 CSR_WRITE_4(sc
, RE_TIMERINT
, 0);
2262 sc
->re_flags
&= ~RE_F_TIMER_INTR
;
2266 re_config_imtype(struct re_softc
*sc
, int imtype
)
2270 KKASSERT(sc
->re_caps
& RE_C_HWIM
);
2272 case RE_IMTYPE_NONE
:
2273 sc
->re_intrs
= RE_INTRS
;
2274 sc
->re_rx_ack
= RE_ISR_RX_OK
| RE_ISR_FIFO_OFLOW
|
2276 sc
->re_tx_ack
= RE_ISR_TX_OK
;
2280 sc
->re_intrs
= RE_INTRS_TIMER
;
2281 sc
->re_rx_ack
= RE_ISR_PCS_TIMEOUT
;
2282 sc
->re_tx_ack
= RE_ISR_PCS_TIMEOUT
;
2286 panic("%s: unknown imtype %d",
2287 sc
->arpcom
.ac_if
.if_xname
, imtype
);
2292 re_setup_intr(struct re_softc
*sc
, int enable_intrs
, int imtype
)
2294 re_config_imtype(sc
, imtype
);
2297 CSR_WRITE_2(sc
, RE_IMR
, sc
->re_intrs
);
2299 CSR_WRITE_2(sc
, RE_IMR
, 0);
2301 sc
->re_npoll
.ifpc_stcount
= 0;
2304 case RE_IMTYPE_NONE
:
2305 re_disable_sim_im(sc
);
2306 re_disable_hw_im(sc
);
2310 KKASSERT(sc
->re_caps
& RE_C_HWIM
);
2311 re_disable_sim_im(sc
);
2316 re_disable_hw_im(sc
);
2317 re_setup_sim_im(sc
);
2321 panic("%s: unknown imtype %d",
2322 sc
->arpcom
.ac_if
.if_xname
, imtype
);
2327 re_jpool_alloc(struct re_softc
*sc
)
2329 struct re_list_data
*ldata
= &sc
->re_ldata
;
2330 struct re_jbuf
*jbuf
;
2332 bus_size_t jpool_size
;
2337 lwkt_serialize_init(&ldata
->re_jbuf_serializer
);
2339 ldata
->re_jbuf
= kmalloc(sizeof(struct re_jbuf
) * RE_JBUF_COUNT(sc
),
2340 M_DEVBUF
, M_WAITOK
| M_ZERO
);
2342 jpool_size
= RE_JBUF_COUNT(sc
) * RE_JBUF_SIZE
;
2344 error
= bus_dmamem_coherent(sc
->re_parent_tag
,
2346 BUS_SPACE_MAXADDR
, BUS_SPACE_MAXADDR
,
2347 jpool_size
, BUS_DMA_WAITOK
, &dmem
);
2349 device_printf(sc
->dev
, "could not allocate jumbo memory\n");
2352 ldata
->re_jpool_tag
= dmem
.dmem_tag
;
2353 ldata
->re_jpool_map
= dmem
.dmem_map
;
2354 ldata
->re_jpool
= dmem
.dmem_addr
;
2355 paddr
= dmem
.dmem_busaddr
;
2357 /* ..and split it into 9KB chunks */
2358 SLIST_INIT(&ldata
->re_jbuf_free
);
2360 buf
= ldata
->re_jpool
;
2361 for (i
= 0; i
< RE_JBUF_COUNT(sc
); i
++) {
2362 jbuf
= &ldata
->re_jbuf
[i
];
2368 jbuf
->re_paddr
= paddr
;
2370 SLIST_INSERT_HEAD(&ldata
->re_jbuf_free
, jbuf
, re_link
);
2372 buf
+= RE_JBUF_SIZE
;
2373 paddr
+= RE_JBUF_SIZE
;
2379 re_jpool_free(struct re_softc
*sc
)
2381 struct re_list_data
*ldata
= &sc
->re_ldata
;
2383 if (ldata
->re_jpool_tag
!= NULL
) {
2384 bus_dmamap_unload(ldata
->re_jpool_tag
, ldata
->re_jpool_map
);
2385 bus_dmamem_free(ldata
->re_jpool_tag
, ldata
->re_jpool
,
2386 ldata
->re_jpool_map
);
2387 bus_dma_tag_destroy(ldata
->re_jpool_tag
);
2388 ldata
->re_jpool_tag
= NULL
;
2391 if (ldata
->re_jbuf
!= NULL
) {
2392 kfree(ldata
->re_jbuf
, M_DEVBUF
);
2393 ldata
->re_jbuf
= NULL
;
2398 static struct re_jbuf
*
2399 re_jbuf_alloc(struct re_softc
*sc
)
2401 struct re_list_data
*ldata
= &sc
->re_ldata
;
2402 struct re_jbuf
*jbuf
;
2404 lwkt_serialize_enter(&ldata
->re_jbuf_serializer
);
2406 jbuf
= SLIST_FIRST(&ldata
->re_jbuf_free
);
2408 SLIST_REMOVE_HEAD(&ldata
->re_jbuf_free
, re_link
);
2412 lwkt_serialize_exit(&ldata
->re_jbuf_serializer
);
2418 re_jbuf_free(void *arg
)
2420 struct re_jbuf
*jbuf
= arg
;
2421 struct re_softc
*sc
= jbuf
->re_sc
;
2422 struct re_list_data
*ldata
= &sc
->re_ldata
;
2424 if (&ldata
->re_jbuf
[jbuf
->re_slot
] != jbuf
) {
2425 panic("%s: free wrong jumbo buffer",
2426 sc
->arpcom
.ac_if
.if_xname
);
2427 } else if (jbuf
->re_inuse
== 0) {
2428 panic("%s: jumbo buffer already freed",
2429 sc
->arpcom
.ac_if
.if_xname
);
2432 lwkt_serialize_enter(&ldata
->re_jbuf_serializer
);
2433 atomic_subtract_int(&jbuf
->re_inuse
, 1);
2434 if (jbuf
->re_inuse
== 0)
2435 SLIST_INSERT_HEAD(&ldata
->re_jbuf_free
, jbuf
, re_link
);
2436 lwkt_serialize_exit(&ldata
->re_jbuf_serializer
);
2440 re_jbuf_ref(void *arg
)
2442 struct re_jbuf
*jbuf
= arg
;
2443 struct re_softc
*sc
= jbuf
->re_sc
;
2444 struct re_list_data
*ldata
= &sc
->re_ldata
;
2446 if (&ldata
->re_jbuf
[jbuf
->re_slot
] != jbuf
) {
2447 panic("%s: ref wrong jumbo buffer",
2448 sc
->arpcom
.ac_if
.if_xname
);
2449 } else if (jbuf
->re_inuse
== 0) {
2450 panic("%s: jumbo buffer already freed",
2451 sc
->arpcom
.ac_if
.if_xname
);
2453 atomic_add_int(&jbuf
->re_inuse
, 1);
2455 #endif /* RE_JUMBO */
2458 re_disable_aspm(device_t dev
)
2460 uint16_t link_cap
, link_ctrl
;
2461 uint8_t pcie_ptr
, reg
;
2463 pcie_ptr
= pci_get_pciecap_ptr(dev
);
2467 link_cap
= pci_read_config(dev
, pcie_ptr
+ PCIER_LINKCAP
, 2);
2468 if ((link_cap
& PCIEM_LNKCAP_ASPM_MASK
) == 0)
2472 device_printf(dev
, "disable ASPM\n");
2474 reg
= pcie_ptr
+ PCIER_LINKCTRL
;
2475 link_ctrl
= pci_read_config(dev
, reg
, 2);
2476 link_ctrl
&= ~(PCIEM_LNKCTL_ASPM_L0S
| PCIEM_LNKCTL_ASPM_L1
);
2477 pci_write_config(dev
, reg
, link_ctrl
, 2);