Add basic support for 8111C; hardware checksum offload does not seems to work
[dfdiff.git] / sys / dev / netif / re / if_re.c
blob4f4f50982fc5cd4950bd1311b8513ee11b15119a
1 /*
2 * Copyright (c) 2004
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
10 * are met:
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 $
36 * $DragonFly: src/sys/dev/netif/re/if_re.c,v 1.41 2008/04/27 15:10:37 sephe Exp $
40 * RealTek 8139C+/8169/8169S/8110S/8168/8111/8101E PCI NIC driver
42 * Written by Bill Paul <wpaul@windriver.com>
43 * Senior Networking Software Engineer
44 * Wind River Systems
48 * This driver is designed to support RealTek's next generation of
49 * 10/100 and 10/100/1000 PCI ethernet controllers. There are currently
50 * seven devices in this family: the RTL8139C+, the RTL8169, the RTL8169S,
51 * RTL8110S, the RTL8168, the RTL8111 and the RTL8101E.
53 * The 8139C+ is a 10/100 ethernet chip. It is backwards compatible
54 * with the older 8139 family, however it also supports a special
55 * C+ mode of operation that provides several new performance enhancing
56 * features. These include:
58 * o Descriptor based DMA mechanism. Each descriptor represents
59 * a single packet fragment. Data buffers may be aligned on
60 * any byte boundary.
62 * o 64-bit DMA
64 * o TCP/IP checksum offload for both RX and TX
66 * o High and normal priority transmit DMA rings
68 * o VLAN tag insertion and extraction
70 * o TCP large send (segmentation offload)
72 * Like the 8139, the 8139C+ also has a built-in 10/100 PHY. The C+
73 * programming API is fairly straightforward. The RX filtering, EEPROM
74 * access and PHY access is the same as it is on the older 8139 series
75 * chips.
77 * The 8169 is a 64-bit 10/100/1000 gigabit ethernet MAC. It has almost the
78 * same programming API and feature set as the 8139C+ with the following
79 * differences and additions:
81 * o 1000Mbps mode
83 * o Jumbo frames
85 * o GMII and TBI ports/registers for interfacing with copper
86 * or fiber PHYs
88 * o RX and TX DMA rings can have up to 1024 descriptors
89 * (the 8139C+ allows a maximum of 64)
91 * o Slight differences in register layout from the 8139C+
93 * The TX start and timer interrupt registers are at different locations
94 * on the 8169 than they are on the 8139C+. Also, the status word in the
95 * RX descriptor has a slightly different bit layout. The 8169 does not
96 * have a built-in PHY. Most reference boards use a Marvell 88E1000 'Alaska'
97 * copper gigE PHY.
99 * The 8169S/8110S 10/100/1000 devices have built-in copper gigE PHYs
100 * (the 'S' stands for 'single-chip'). These devices have the same
101 * programming API as the older 8169, but also have some vendor-specific
102 * registers for the on-board PHY. The 8110S is a LAN-on-motherboard
103 * part designed to be pin-compatible with the RealTek 8100 10/100 chip.
105 * This driver takes advantage of the RX and TX checksum offload and
106 * VLAN tag insertion/extraction features. It also implements TX
107 * interrupt moderation using the timer interrupt registers, which
108 * significantly reduces TX interrupt load. There is also support
109 * for jumbo frames, however the 8169/8169S/8110S can not transmit
110 * jumbo frames larger than 7440, so the max MTU possible with this
111 * driver is 7422 bytes.
114 #include "opt_polling.h"
116 #include <sys/param.h>
117 #include <sys/bus.h>
118 #include <sys/endian.h>
119 #include <sys/kernel.h>
120 #include <sys/malloc.h>
121 #include <sys/mbuf.h>
122 /* #include <sys/module.h> */
123 #include <sys/rman.h>
124 #include <sys/serialize.h>
125 #include <sys/socket.h>
126 #include <sys/sockio.h>
127 #include <sys/sysctl.h>
129 #include <net/bpf.h>
130 #include <net/ethernet.h>
131 #include <net/if.h>
132 #include <net/ifq_var.h>
133 #include <net/if_arp.h>
134 #include <net/if_dl.h>
135 #include <net/if_media.h>
136 #include <net/if_types.h>
137 #include <net/vlan/if_vlan_var.h>
138 #include <net/vlan/if_vlan_ether.h>
140 #include <dev/netif/mii_layer/mii.h>
141 #include <dev/netif/mii_layer/miivar.h>
143 #include <bus/pci/pcidevs.h>
144 #include <bus/pci/pcireg.h>
145 #include <bus/pci/pcivar.h>
147 /* "device miibus" required. See GENERIC if you get errors here. */
148 #include "miibus_if.h"
150 #include <dev/netif/re/if_rereg.h>
151 #include <dev/netif/re/if_revar.h>
153 #define RE_CSUM_FEATURES (CSUM_IP | CSUM_TCP | CSUM_UDP)
154 #if 0
155 #define RE_DISABLE_HWCSUM
156 #endif
159 * Various supported device vendors/types and their names.
161 static const struct re_type re_devs[] = {
162 { PCI_VENDOR_DLINK, PCI_PRODUCT_DLINK_DGE528T, RE_HWREV_8169S,
163 "D-Link DGE-528(T) Gigabit Ethernet Adapter" },
164 { PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8139, RE_HWREV_8139CPLUS,
165 "RealTek 8139C+ 10/100BaseTX" },
166 { PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8101E, RE_HWREV_8101E,
167 "RealTek 8101E PCIe 10/100baseTX" },
168 { PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8168, RE_HWREV_8168_SPIN1,
169 "RealTek 8168/8111B PCIe Gigabit Ethernet" },
170 { PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8168, RE_HWREV_8168_SPIN2,
171 "RealTek 8168/8111B PCIe Gigabit Ethernet" },
172 { PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8168, RE_HWREV_8168_SPIN3,
173 "RealTek 8168B/8111B PCIe Gigabit Ethernet" },
174 { PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8168, RE_HWREV_8168C,
175 "RealTek 8168C/8111C PCIe Gigabit Ethernet" },
176 { PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8169, RE_HWREV_8169,
177 "RealTek 8169 Gigabit Ethernet" },
178 { PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8169, RE_HWREV_8169S,
179 "RealTek 8169S Single-chip Gigabit Ethernet" },
180 { PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8169, RE_HWREV_8169_8110SB,
181 "RealTek 8169SB/8110SB Single-chip Gigabit Ethernet" },
182 { PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8169, RE_HWREV_8169_8110SC,
183 "RealTek 8169SC/8110SC Single-chip Gigabit Ethernet" },
184 { PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8169SC, RE_HWREV_8169_8110SC,
185 "RealTek 8169SC/8110SC Single-chip Gigabit Ethernet" },
186 { PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8169, RE_HWREV_8110S,
187 "RealTek 8110S Single-chip Gigabit Ethernet" },
188 { PCI_VENDOR_COREGA, PCI_PRODUCT_COREGA_CG_LAPCIGT, RE_HWREV_8169S,
189 "Corega CG-LAPCIGT Gigabit Ethernet" },
190 { PCI_VENDOR_LINKSYS, PCI_PRODUCT_LINKSYS_EG1032, RE_HWREV_8169S,
191 "Linksys EG1032 Gigabit Ethernet" },
192 { PCI_VENDOR_USR2, PCI_PRODUCT_USR2_997902, RE_HWREV_8169S,
193 "US Robotics 997902 Gigabit Ethernet" },
194 { 0, 0, 0, NULL }
197 static const struct re_hwrev re_hwrevs[] = {
198 { RE_HWREV_8139CPLUS, RE_8139CPLUS, RE_F_HASMPC, "C+" },
199 { RE_HWREV_8168_SPIN1, RE_8169, RE_F_PCIE, "8168" },
200 { RE_HWREV_8168_SPIN2, RE_8169, RE_F_PCIE, "8168" },
201 { RE_HWREV_8168_SPIN3, RE_8169, RE_F_PCIE, "8168" },
202 { RE_HWREV_8168C, RE_8169, RE_F_PCIE, "8168C" },
203 { RE_HWREV_8169, RE_8169, RE_F_HASMPC, "8169" },
204 { RE_HWREV_8169S, RE_8169, RE_F_HASMPC, "8169S" },
205 { RE_HWREV_8110S, RE_8169, RE_F_HASMPC, "8110S" },
206 { RE_HWREV_8169_8110SB, RE_8169, RE_F_HASMPC, "8169SB" },
207 { RE_HWREV_8169_8110SC, RE_8169, 0, "8169SC" },
208 { RE_HWREV_8100E, RE_8169, RE_F_HASMPC, "8100E" },
209 { RE_HWREV_8101E, RE_8169, RE_F_PCIE, "8101E" },
210 { 0, 0, 0, NULL }
213 static int re_probe(device_t);
214 static int re_attach(device_t);
215 static int re_detach(device_t);
217 static int re_encap(struct re_softc *, struct mbuf **, int *, int *);
219 static void re_dma_map_addr(void *, bus_dma_segment_t *, int, int);
220 static void re_dma_map_desc(void *, bus_dma_segment_t *, int,
221 bus_size_t, int);
222 static int re_allocmem(device_t, struct re_softc *);
223 static int re_newbuf(struct re_softc *, int, struct mbuf *);
224 static int re_rx_list_init(struct re_softc *);
225 static int re_tx_list_init(struct re_softc *);
226 static void re_rxeof(struct re_softc *);
227 static void re_txeof(struct re_softc *);
228 static void re_intr(void *);
229 static void re_tick(void *);
230 static void re_tick_serialized(void *);
231 static void re_start(struct ifnet *);
232 static int re_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
233 static void re_init(void *);
234 static void re_stop(struct re_softc *);
235 static void re_watchdog(struct ifnet *);
236 static int re_suspend(device_t);
237 static int re_resume(device_t);
238 static void re_shutdown(device_t);
239 static int re_ifmedia_upd(struct ifnet *);
240 static void re_ifmedia_sts(struct ifnet *, struct ifmediareq *);
242 static void re_eeprom_putbyte(struct re_softc *, int);
243 static void re_eeprom_getword(struct re_softc *, int, u_int16_t *);
244 static void re_read_eeprom(struct re_softc *, caddr_t, int, int);
245 static int re_gmii_readreg(device_t, int, int);
246 static int re_gmii_writereg(device_t, int, int, int);
248 static int re_miibus_readreg(device_t, int, int);
249 static int re_miibus_writereg(device_t, int, int, int);
250 static void re_miibus_statchg(device_t);
252 static void re_setmulti(struct re_softc *);
253 static void re_reset(struct re_softc *);
255 #ifdef RE_DIAG
256 static int re_diag(struct re_softc *);
257 #endif
259 #ifdef DEVICE_POLLING
260 static void re_poll(struct ifnet *ifp, enum poll_cmd cmd, int count);
261 #endif
263 static int re_sysctl_tx_moderation(SYSCTL_HANDLER_ARGS);
265 static device_method_t re_methods[] = {
266 /* Device interface */
267 DEVMETHOD(device_probe, re_probe),
268 DEVMETHOD(device_attach, re_attach),
269 DEVMETHOD(device_detach, re_detach),
270 DEVMETHOD(device_suspend, re_suspend),
271 DEVMETHOD(device_resume, re_resume),
272 DEVMETHOD(device_shutdown, re_shutdown),
274 /* bus interface */
275 DEVMETHOD(bus_print_child, bus_generic_print_child),
276 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
278 /* MII interface */
279 DEVMETHOD(miibus_readreg, re_miibus_readreg),
280 DEVMETHOD(miibus_writereg, re_miibus_writereg),
281 DEVMETHOD(miibus_statchg, re_miibus_statchg),
283 { 0, 0 }
286 static driver_t re_driver = {
287 "re",
288 re_methods,
289 sizeof(struct re_softc)
292 static devclass_t re_devclass;
294 DECLARE_DUMMY_MODULE(if_re);
295 DRIVER_MODULE(if_re, pci, re_driver, re_devclass, 0, 0);
296 DRIVER_MODULE(if_re, cardbus, re_driver, re_devclass, 0, 0);
297 DRIVER_MODULE(miibus, re, miibus_driver, miibus_devclass, 0, 0);
299 #define EE_SET(x) \
300 CSR_WRITE_1(sc, RE_EECMD, CSR_READ_1(sc, RE_EECMD) | (x))
302 #define EE_CLR(x) \
303 CSR_WRITE_1(sc, RE_EECMD, CSR_READ_1(sc, RE_EECMD) & ~(x))
306 * Send a read command and address to the EEPROM, check for ACK.
308 static void
309 re_eeprom_putbyte(struct re_softc *sc, int addr)
311 int d, i;
313 d = addr | (RE_9346_READ << sc->re_eewidth);
316 * Feed in each bit and strobe the clock.
318 for (i = 1 << (sc->re_eewidth + 3); i; i >>= 1) {
319 if (d & i)
320 EE_SET(RE_EE_DATAIN);
321 else
322 EE_CLR(RE_EE_DATAIN);
323 DELAY(100);
324 EE_SET(RE_EE_CLK);
325 DELAY(150);
326 EE_CLR(RE_EE_CLK);
327 DELAY(100);
332 * Read a word of data stored in the EEPROM at address 'addr.'
334 static void
335 re_eeprom_getword(struct re_softc *sc, int addr, uint16_t *dest)
337 int i;
338 uint16_t word = 0;
341 * Send address of word we want to read.
343 re_eeprom_putbyte(sc, addr);
346 * Start reading bits from EEPROM.
348 for (i = 0x8000; i != 0; i >>= 1) {
349 EE_SET(RE_EE_CLK);
350 DELAY(100);
351 if (CSR_READ_1(sc, RE_EECMD) & RE_EE_DATAOUT)
352 word |= i;
353 EE_CLR(RE_EE_CLK);
354 DELAY(100);
357 *dest = word;
361 * Read a sequence of words from the EEPROM.
363 static void
364 re_read_eeprom(struct re_softc *sc, caddr_t dest, int off, int cnt)
366 int i;
367 uint16_t word = 0, *ptr;
369 CSR_SETBIT_1(sc, RE_EECMD, RE_EEMODE_PROGRAM);
370 DELAY(100);
372 for (i = 0; i < cnt; i++) {
373 CSR_SETBIT_1(sc, RE_EECMD, RE_EE_SEL);
374 re_eeprom_getword(sc, off + i, &word);
375 CSR_CLRBIT_1(sc, RE_EECMD, RE_EE_SEL);
376 ptr = (uint16_t *)(dest + (i * 2));
377 *ptr = word;
380 CSR_CLRBIT_1(sc, RE_EECMD, RE_EEMODE_PROGRAM);
383 static int
384 re_gmii_readreg(device_t dev, int phy, int reg)
386 struct re_softc *sc = device_get_softc(dev);
387 u_int32_t rval;
388 int i;
390 if (phy != 1)
391 return(0);
393 /* Let the rgephy driver read the GMEDIASTAT register */
395 if (reg == RE_GMEDIASTAT)
396 return(CSR_READ_1(sc, RE_GMEDIASTAT));
398 CSR_WRITE_4(sc, RE_PHYAR, reg << 16);
399 DELAY(1000);
401 for (i = 0; i < RE_TIMEOUT; i++) {
402 rval = CSR_READ_4(sc, RE_PHYAR);
403 if (rval & RE_PHYAR_BUSY)
404 break;
405 DELAY(100);
408 if (i == RE_TIMEOUT) {
409 device_printf(dev, "PHY read failed\n");
410 return(0);
413 return(rval & RE_PHYAR_PHYDATA);
416 static int
417 re_gmii_writereg(device_t dev, int phy, int reg, int data)
419 struct re_softc *sc = device_get_softc(dev);
420 uint32_t rval;
421 int i;
423 CSR_WRITE_4(sc, RE_PHYAR,
424 (reg << 16) | (data & RE_PHYAR_PHYDATA) | RE_PHYAR_BUSY);
425 DELAY(1000);
427 for (i = 0; i < RE_TIMEOUT; i++) {
428 rval = CSR_READ_4(sc, RE_PHYAR);
429 if ((rval & RE_PHYAR_BUSY) == 0)
430 break;
431 DELAY(100);
434 if (i == RE_TIMEOUT)
435 device_printf(dev, "PHY write failed\n");
437 return(0);
440 static int
441 re_miibus_readreg(device_t dev, int phy, int reg)
443 struct re_softc *sc = device_get_softc(dev);
444 uint16_t rval = 0;
445 uint16_t re8139_reg = 0;
447 if (sc->re_type == RE_8169) {
448 rval = re_gmii_readreg(dev, phy, reg);
449 return(rval);
452 /* Pretend the internal PHY is only at address 0 */
453 if (phy)
454 return(0);
456 switch(reg) {
457 case MII_BMCR:
458 re8139_reg = RE_BMCR;
459 break;
460 case MII_BMSR:
461 re8139_reg = RE_BMSR;
462 break;
463 case MII_ANAR:
464 re8139_reg = RE_ANAR;
465 break;
466 case MII_ANER:
467 re8139_reg = RE_ANER;
468 break;
469 case MII_ANLPAR:
470 re8139_reg = RE_LPAR;
471 break;
472 case MII_PHYIDR1:
473 case MII_PHYIDR2:
474 return(0);
476 * Allow the rlphy driver to read the media status
477 * register. If we have a link partner which does not
478 * support NWAY, this is the register which will tell
479 * us the results of parallel detection.
481 case RE_MEDIASTAT:
482 return(CSR_READ_1(sc, RE_MEDIASTAT));
483 default:
484 device_printf(dev, "bad phy register\n");
485 return(0);
487 rval = CSR_READ_2(sc, re8139_reg);
488 if (sc->re_type == RE_8139CPLUS && re8139_reg == RE_BMCR) {
489 /* 8139C+ has different bit layout. */
490 rval &= ~(BMCR_LOOP | BMCR_ISO);
492 return(rval);
495 static int
496 re_miibus_writereg(device_t dev, int phy, int reg, int data)
498 struct re_softc *sc= device_get_softc(dev);
499 u_int16_t re8139_reg = 0;
501 if (sc->re_type == RE_8169)
502 return(re_gmii_writereg(dev, phy, reg, data));
504 /* Pretend the internal PHY is only at address 0 */
505 if (phy)
506 return(0);
508 switch(reg) {
509 case MII_BMCR:
510 re8139_reg = RE_BMCR;
511 if (sc->re_type == RE_8139CPLUS) {
512 /* 8139C+ has different bit layout. */
513 data &= ~(BMCR_LOOP | BMCR_ISO);
515 break;
516 case MII_BMSR:
517 re8139_reg = RE_BMSR;
518 break;
519 case MII_ANAR:
520 re8139_reg = RE_ANAR;
521 break;
522 case MII_ANER:
523 re8139_reg = RE_ANER;
524 break;
525 case MII_ANLPAR:
526 re8139_reg = RE_LPAR;
527 break;
528 case MII_PHYIDR1:
529 case MII_PHYIDR2:
530 return(0);
531 default:
532 device_printf(dev, "bad phy register\n");
533 return(0);
535 CSR_WRITE_2(sc, re8139_reg, data);
536 return(0);
539 static void
540 re_miibus_statchg(device_t dev)
545 * Program the 64-bit multicast hash filter.
547 static void
548 re_setmulti(struct re_softc *sc)
550 struct ifnet *ifp = &sc->arpcom.ac_if;
551 int h = 0;
552 uint32_t hashes[2] = { 0, 0 };
553 struct ifmultiaddr *ifma;
554 uint32_t rxfilt;
555 int mcnt = 0;
557 rxfilt = CSR_READ_4(sc, RE_RXCFG);
559 if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
560 rxfilt |= RE_RXCFG_RX_MULTI;
561 CSR_WRITE_4(sc, RE_RXCFG, rxfilt);
562 CSR_WRITE_4(sc, RE_MAR0, 0xFFFFFFFF);
563 CSR_WRITE_4(sc, RE_MAR4, 0xFFFFFFFF);
564 return;
567 /* first, zot all the existing hash bits */
568 CSR_WRITE_4(sc, RE_MAR0, 0);
569 CSR_WRITE_4(sc, RE_MAR4, 0);
571 /* now program new ones */
572 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
573 if (ifma->ifma_addr->sa_family != AF_LINK)
574 continue;
575 h = ether_crc32_be(LLADDR((struct sockaddr_dl *)
576 ifma->ifma_addr), ETHER_ADDR_LEN) >> 26;
577 if (h < 32)
578 hashes[0] |= (1 << h);
579 else
580 hashes[1] |= (1 << (h - 32));
581 mcnt++;
584 if (mcnt)
585 rxfilt |= RE_RXCFG_RX_MULTI;
586 else
587 rxfilt &= ~RE_RXCFG_RX_MULTI;
589 CSR_WRITE_4(sc, RE_RXCFG, rxfilt);
592 * For some unfathomable reason, RealTek decided to reverse
593 * the order of the multicast hash registers in the PCI Express
594 * parts. This means we have to write the hash pattern in reverse
595 * order for those devices.
597 if (sc->re_flags & RE_F_PCIE) {
598 CSR_WRITE_4(sc, RE_MAR0, bswap32(hashes[0]));
599 CSR_WRITE_4(sc, RE_MAR4, bswap32(hashes[1]));
600 } else {
601 CSR_WRITE_4(sc, RE_MAR0, hashes[0]);
602 CSR_WRITE_4(sc, RE_MAR4, hashes[1]);
606 static void
607 re_reset(struct re_softc *sc)
609 int i;
611 CSR_WRITE_1(sc, RE_COMMAND, RE_CMD_RESET);
613 for (i = 0; i < RE_TIMEOUT; i++) {
614 DELAY(10);
615 if ((CSR_READ_1(sc, RE_COMMAND) & RE_CMD_RESET) == 0)
616 break;
618 if (i == RE_TIMEOUT)
619 if_printf(&sc->arpcom.ac_if, "reset never completed!\n");
621 CSR_WRITE_1(sc, 0x82, 1);
624 #ifdef RE_DIAG
626 * The following routine is designed to test for a defect on some
627 * 32-bit 8169 cards. Some of these NICs have the REQ64# and ACK64#
628 * lines connected to the bus, however for a 32-bit only card, they
629 * should be pulled high. The result of this defect is that the
630 * NIC will not work right if you plug it into a 64-bit slot: DMA
631 * operations will be done with 64-bit transfers, which will fail
632 * because the 64-bit data lines aren't connected.
634 * There's no way to work around this (short of talking a soldering
635 * iron to the board), however we can detect it. The method we use
636 * here is to put the NIC into digital loopback mode, set the receiver
637 * to promiscuous mode, and then try to send a frame. We then compare
638 * the frame data we sent to what was received. If the data matches,
639 * then the NIC is working correctly, otherwise we know the user has
640 * a defective NIC which has been mistakenly plugged into a 64-bit PCI
641 * slot. In the latter case, there's no way the NIC can work correctly,
642 * so we print out a message on the console and abort the device attach.
645 static int
646 re_diag(struct re_softc *sc)
648 struct ifnet *ifp = &sc->arpcom.ac_if;
649 struct mbuf *m0;
650 struct ether_header *eh;
651 struct re_desc *cur_rx;
652 uint16_t status;
653 uint32_t rxstat;
654 int total_len, i, error = 0, phyaddr;
655 uint8_t dst[ETHER_ADDR_LEN] = { 0x00, 'h', 'e', 'l', 'l', 'o' };
656 uint8_t src[ETHER_ADDR_LEN] = { 0x00, 'w', 'o', 'r', 'l', 'd' };
658 /* Allocate a single mbuf */
660 MGETHDR(m0, MB_DONTWAIT, MT_DATA);
661 if (m0 == NULL)
662 return(ENOBUFS);
665 * Initialize the NIC in test mode. This sets the chip up
666 * so that it can send and receive frames, but performs the
667 * following special functions:
668 * - Puts receiver in promiscuous mode
669 * - Enables digital loopback mode
670 * - Leaves interrupts turned off
673 ifp->if_flags |= IFF_PROMISC;
674 sc->re_testmode = 1;
675 re_reset(sc);
676 re_init(sc);
677 sc->re_link = 1;
678 if (sc->re_type == RE_8169)
679 phyaddr = 1;
680 else
681 phyaddr = 0;
683 re_miibus_writereg(sc->re_dev, phyaddr, MII_BMCR, BMCR_RESET);
684 for (i = 0; i < RE_TIMEOUT; i++) {
685 status = re_miibus_readreg(sc->re_dev, phyaddr, MII_BMCR);
686 if (!(status & BMCR_RESET))
687 break;
690 re_miibus_writereg(sc->re_dev, phyaddr, MII_BMCR, BMCR_LOOP);
691 CSR_WRITE_2(sc, RE_ISR, RE_INTRS_DIAG);
693 DELAY(100000);
695 /* Put some data in the mbuf */
697 eh = mtod(m0, struct ether_header *);
698 bcopy (dst, eh->ether_dhost, ETHER_ADDR_LEN);
699 bcopy (src, eh->ether_shost, ETHER_ADDR_LEN);
700 eh->ether_type = htons(ETHERTYPE_IP);
701 m0->m_pkthdr.len = m0->m_len = ETHER_MIN_LEN - ETHER_CRC_LEN;
704 * Queue the packet, start transmission.
705 * Note: ifq_handoff() ultimately calls re_start() for us.
708 CSR_WRITE_2(sc, RE_ISR, 0xFFFF);
709 error = ifq_handoff(ifp, m0, NULL);
710 if (error) {
711 m0 = NULL;
712 goto done;
714 m0 = NULL;
716 /* Wait for it to propagate through the chip */
718 DELAY(100000);
719 for (i = 0; i < RE_TIMEOUT; i++) {
720 status = CSR_READ_2(sc, RE_ISR);
721 CSR_WRITE_2(sc, RE_ISR, status);
722 if ((status & (RE_ISR_TIMEOUT_EXPIRED|RE_ISR_RX_OK)) ==
723 (RE_ISR_TIMEOUT_EXPIRED|RE_ISR_RX_OK))
724 break;
725 DELAY(10);
728 if (i == RE_TIMEOUT) {
729 if_printf(ifp, "diagnostic failed to receive packet "
730 "in loopback mode\n");
731 error = EIO;
732 goto done;
736 * The packet should have been dumped into the first
737 * entry in the RX DMA ring. Grab it from there.
740 bus_dmamap_sync(sc->re_ldata.re_rx_list_tag,
741 sc->re_ldata.re_rx_list_map, BUS_DMASYNC_POSTREAD);
742 bus_dmamap_sync(sc->re_ldata.re_mtag, sc->re_ldata.re_rx_dmamap[0],
743 BUS_DMASYNC_POSTWRITE);
744 bus_dmamap_unload(sc->re_ldata.re_mtag, sc->re_ldata.re_rx_dmamap[0]);
746 m0 = sc->re_ldata.re_rx_mbuf[0];
747 sc->re_ldata.re_rx_mbuf[0] = NULL;
748 eh = mtod(m0, struct ether_header *);
750 cur_rx = &sc->re_ldata.re_rx_list[0];
751 total_len = RE_RXBYTES(cur_rx);
752 rxstat = le32toh(cur_rx->re_cmdstat);
754 if (total_len != ETHER_MIN_LEN) {
755 if_printf(ifp, "diagnostic failed, received short packet\n");
756 error = EIO;
757 goto done;
760 /* Test that the received packet data matches what we sent. */
762 if (bcmp(eh->ether_dhost, dst, ETHER_ADDR_LEN) ||
763 bcmp(eh->ether_shost, &src, ETHER_ADDR_LEN) ||
764 be16toh(eh->ether_type) != ETHERTYPE_IP) {
765 if_printf(ifp, "WARNING, DMA FAILURE!\n");
766 if_printf(ifp, "expected TX data: %6D/%6D/0x%x\n",
767 dst, ":", src, ":", ETHERTYPE_IP);
768 if_printf(ifp, "received RX data: %6D/%6D/0x%x\n",
769 eh->ether_dhost, ":", eh->ether_shost, ":",
770 ntohs(eh->ether_type));
771 if_printf(ifp, "You may have a defective 32-bit NIC plugged "
772 "into a 64-bit PCI slot.\n");
773 if_printf(ifp, "Please re-install the NIC in a 32-bit slot "
774 "for proper operation.\n");
775 if_printf(ifp, "Read the re(4) man page for more details.\n");
776 error = EIO;
779 done:
780 /* Turn interface off, release resources */
782 sc->re_testmode = 0;
783 sc->re_link = 0;
784 ifp->if_flags &= ~IFF_PROMISC;
785 re_stop(sc);
786 if (m0 != NULL)
787 m_freem(m0);
789 return (error);
791 #endif /* RE_DIAG */
794 * Probe for a RealTek 8139C+/8169/8110 chip. Check the PCI vendor and device
795 * IDs against our list and return a device name if we find a match.
797 static int
798 re_probe(device_t dev)
800 const struct re_type *t;
801 struct re_softc *sc;
802 int rid;
803 uint32_t hwrev;
804 uint16_t vendor, product;
806 t = re_devs;
808 vendor = pci_get_vendor(dev);
809 product = pci_get_device(dev);
812 * Only attach to rev.3 of the Linksys EG1032 adapter.
813 * Rev.2 is supported by sk(4).
815 if (vendor == PCI_VENDOR_LINKSYS &&
816 product == PCI_PRODUCT_LINKSYS_EG1032 &&
817 pci_get_subdevice(dev) != PCI_SUBDEVICE_LINKSYS_EG1032_REV3)
818 return ENXIO;
820 for (t = re_devs; t->re_name != NULL; t++) {
821 if (product == t->re_did && vendor == t->re_vid)
822 break;
826 * Check if we found a RealTek device.
828 if (t->re_name == NULL)
829 return(ENXIO);
832 * Temporarily map the I/O space so we can read the chip ID register.
834 sc = kmalloc(sizeof(*sc), M_TEMP, M_WAITOK | M_ZERO);
835 rid = RE_PCI_LOIO;
836 sc->re_res = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid,
837 RF_ACTIVE);
838 if (sc->re_res == NULL) {
839 device_printf(dev, "couldn't map ports/memory\n");
840 kfree(sc, M_TEMP);
841 return(ENXIO);
844 sc->re_btag = rman_get_bustag(sc->re_res);
845 sc->re_bhandle = rman_get_bushandle(sc->re_res);
847 hwrev = CSR_READ_4(sc, RE_TXCFG) & RE_TXCFG_HWREV;
848 bus_release_resource(dev, SYS_RES_IOPORT, RE_PCI_LOIO, sc->re_res);
849 kfree(sc, M_TEMP);
852 * and continue matching for the specific chip...
854 for (; t->re_name != NULL; t++) {
855 if (product == t->re_did && vendor == t->re_vid &&
856 t->re_basetype == hwrev) {
857 device_set_desc(dev, t->re_name);
858 return(0);
862 if (bootverbose)
863 kprintf("re: unknown hwrev %#x\n", hwrev);
864 return(ENXIO);
868 * This routine takes the segment list provided as the result of
869 * a bus_dma_map_load() operation and assigns the addresses/lengths
870 * to RealTek DMA descriptors. This can be called either by the RX
871 * code or the TX code. In the RX case, we'll probably wind up mapping
872 * at most one segment. For the TX case, there could be any number of
873 * segments since TX packets may span multiple mbufs. In either case,
874 * if the number of segments is larger than the re_maxsegs limit
875 * specified by the caller, we abort the mapping operation. Sadly,
876 * whoever designed the buffer mapping API did not provide a way to
877 * return an error from here, so we have to fake it a bit.
880 static void
881 re_dma_map_desc(void *arg, bus_dma_segment_t *segs, int nseg,
882 bus_size_t mapsize, int error)
884 struct re_dmaload_arg *ctx;
885 struct re_desc *d = NULL;
886 int i = 0, idx;
887 uint32_t cmdstat;
889 if (error)
890 return;
892 ctx = arg;
894 /* Signal error to caller if there's too many segments */
895 if (nseg > ctx->re_maxsegs) {
896 ctx->re_maxsegs = 0;
897 return;
901 * Map the segment array into descriptors. Note that we set the
902 * start-of-frame and end-of-frame markers for either TX or RX, but
903 * they really only have meaning in the TX case. (In the RX case,
904 * it's the chip that tells us where packets begin and end.)
905 * We also keep track of the end of the ring and set the
906 * end-of-ring bits as needed, and we set the ownership bits
907 * in all except the very first descriptor. (The caller will
908 * set this descriptor later when it start transmission or
909 * reception.)
911 idx = ctx->re_idx;
912 for (;;) {
913 d = &ctx->re_ring[idx];
914 if (le32toh(d->re_cmdstat) & RE_RDESC_STAT_OWN) {
915 ctx->re_maxsegs = 0;
916 return;
918 cmdstat = segs[i].ds_len;
919 d->re_bufaddr_lo = htole32(RE_ADDR_LO(segs[i].ds_addr));
920 d->re_bufaddr_hi = htole32(RE_ADDR_HI(segs[i].ds_addr));
921 if (i == 0)
922 cmdstat |= RE_TDESC_CMD_SOF;
923 else
924 cmdstat |= RE_TDESC_CMD_OWN;
925 if (idx == (RE_RX_DESC_CNT - 1))
926 cmdstat |= RE_TDESC_CMD_EOR;
927 d->re_cmdstat = htole32(cmdstat | ctx->re_flags);
928 i++;
929 if (i == nseg)
930 break;
931 RE_DESC_INC(idx);
934 d->re_cmdstat |= htole32(RE_TDESC_CMD_EOF);
935 ctx->re_maxsegs = nseg;
936 ctx->re_idx = idx;
940 * Map a single buffer address.
943 static void
944 re_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
946 uint32_t *addr;
948 if (error)
949 return;
951 KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
952 addr = arg;
953 *addr = segs->ds_addr;
956 static int
957 re_allocmem(device_t dev, struct re_softc *sc)
959 int error, i, nseg;
962 * Allocate map for RX mbufs.
964 nseg = 32;
965 error = bus_dma_tag_create(sc->re_parent_tag, ETHER_ALIGN, 0,
966 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL,
967 NULL, MCLBYTES * nseg, nseg, MCLBYTES, BUS_DMA_ALLOCNOW,
968 &sc->re_ldata.re_mtag);
969 if (error) {
970 device_printf(dev, "could not allocate dma tag\n");
971 return(error);
975 * Allocate map for TX descriptor list.
977 error = bus_dma_tag_create(sc->re_parent_tag, RE_RING_ALIGN,
978 0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL,
979 NULL, RE_TX_LIST_SZ, 1, RE_TX_LIST_SZ, BUS_DMA_ALLOCNOW,
980 &sc->re_ldata.re_tx_list_tag);
981 if (error) {
982 device_printf(dev, "could not allocate dma tag\n");
983 return(error);
986 /* Allocate DMA'able memory for the TX ring */
988 error = bus_dmamem_alloc(sc->re_ldata.re_tx_list_tag,
989 (void **)&sc->re_ldata.re_tx_list, BUS_DMA_WAITOK | BUS_DMA_ZERO,
990 &sc->re_ldata.re_tx_list_map);
991 if (error) {
992 device_printf(dev, "could not allocate TX ring\n");
993 return(error);
996 /* Load the map for the TX ring. */
998 error = bus_dmamap_load(sc->re_ldata.re_tx_list_tag,
999 sc->re_ldata.re_tx_list_map, sc->re_ldata.re_tx_list,
1000 RE_TX_LIST_SZ, re_dma_map_addr,
1001 &sc->re_ldata.re_tx_list_addr, BUS_DMA_NOWAIT);
1002 if (error) {
1003 device_printf(dev, "could not get address of TX ring\n");
1004 return(error);
1007 /* Create DMA maps for TX buffers */
1009 for (i = 0; i < RE_TX_DESC_CNT; i++) {
1010 error = bus_dmamap_create(sc->re_ldata.re_mtag, 0,
1011 &sc->re_ldata.re_tx_dmamap[i]);
1012 if (error) {
1013 device_printf(dev, "can't create DMA map for TX\n");
1014 return(error);
1019 * Allocate map for RX descriptor list.
1021 error = bus_dma_tag_create(sc->re_parent_tag, RE_RING_ALIGN,
1022 0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL,
1023 NULL, RE_RX_LIST_SZ, 1, RE_RX_LIST_SZ, BUS_DMA_ALLOCNOW,
1024 &sc->re_ldata.re_rx_list_tag);
1025 if (error) {
1026 device_printf(dev, "could not allocate dma tag\n");
1027 return(error);
1030 /* Allocate DMA'able memory for the RX ring */
1032 error = bus_dmamem_alloc(sc->re_ldata.re_rx_list_tag,
1033 (void **)&sc->re_ldata.re_rx_list, BUS_DMA_WAITOK | BUS_DMA_ZERO,
1034 &sc->re_ldata.re_rx_list_map);
1035 if (error) {
1036 device_printf(dev, "could not allocate RX ring\n");
1037 return(error);
1040 /* Load the map for the RX ring. */
1042 error = bus_dmamap_load(sc->re_ldata.re_rx_list_tag,
1043 sc->re_ldata.re_rx_list_map, sc->re_ldata.re_rx_list,
1044 RE_RX_LIST_SZ, re_dma_map_addr,
1045 &sc->re_ldata.re_rx_list_addr, BUS_DMA_NOWAIT);
1046 if (error) {
1047 device_printf(dev, "could not get address of RX ring\n");
1048 return(error);
1051 /* Create DMA maps for RX buffers */
1053 for (i = 0; i < RE_RX_DESC_CNT; i++) {
1054 error = bus_dmamap_create(sc->re_ldata.re_mtag, 0,
1055 &sc->re_ldata.re_rx_dmamap[i]);
1056 if (error) {
1057 device_printf(dev, "can't create DMA map for RX\n");
1058 return(ENOMEM);
1062 return(0);
1066 * Attach the interface. Allocate softc structures, do ifmedia
1067 * setup and ethernet/BPF attach.
1069 static int
1070 re_attach(device_t dev)
1072 struct re_softc *sc = device_get_softc(dev);
1073 struct ifnet *ifp;
1074 const struct re_hwrev *hw_rev;
1075 uint8_t eaddr[ETHER_ADDR_LEN];
1076 uint16_t as[ETHER_ADDR_LEN / 2];
1077 uint16_t re_did = 0;
1078 uint32_t hwrev;
1079 int error = 0, rid, i;
1081 callout_init(&sc->re_timer);
1082 #ifdef RE_DIAG
1083 sc->re_dev = dev;
1084 #endif
1086 RE_ENABLE_TX_MODERATION(sc);
1088 sysctl_ctx_init(&sc->re_sysctl_ctx);
1089 sc->re_sysctl_tree = SYSCTL_ADD_NODE(&sc->re_sysctl_ctx,
1090 SYSCTL_STATIC_CHILDREN(_hw),
1091 OID_AUTO,
1092 device_get_nameunit(dev),
1093 CTLFLAG_RD, 0, "");
1094 if (sc->re_sysctl_tree == NULL) {
1095 device_printf(dev, "can't add sysctl node\n");
1096 error = ENXIO;
1097 goto fail;
1099 SYSCTL_ADD_PROC(&sc->re_sysctl_ctx,
1100 SYSCTL_CHILDREN(sc->re_sysctl_tree),
1101 OID_AUTO, "tx_moderation",
1102 CTLTYPE_INT | CTLFLAG_RW,
1103 sc, 0, re_sysctl_tx_moderation, "I",
1104 "Enable/Disable TX moderation");
1106 #ifndef BURN_BRIDGES
1108 * Handle power management nonsense.
1111 if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
1112 uint32_t membase, irq;
1114 /* Save important PCI config data. */
1115 membase = pci_read_config(dev, RE_PCI_LOMEM, 4);
1116 irq = pci_read_config(dev, PCIR_INTLINE, 4);
1118 /* Reset the power state. */
1119 device_printf(dev, "chip is in D%d power mode "
1120 "-- setting to D0\n", pci_get_powerstate(dev));
1122 pci_set_powerstate(dev, PCI_POWERSTATE_D0);
1124 /* Restore PCI config data. */
1125 pci_write_config(dev, RE_PCI_LOMEM, membase, 4);
1126 pci_write_config(dev, PCIR_INTLINE, irq, 4);
1128 #endif
1130 * Map control/status registers.
1132 pci_enable_busmaster(dev);
1134 rid = RE_PCI_LOIO;
1135 sc->re_res = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid,
1136 RF_ACTIVE);
1138 if (sc->re_res == NULL) {
1139 device_printf(dev, "couldn't map ports\n");
1140 error = ENXIO;
1141 goto fail;
1144 sc->re_btag = rman_get_bustag(sc->re_res);
1145 sc->re_bhandle = rman_get_bushandle(sc->re_res);
1147 /* Allocate interrupt */
1148 rid = 0;
1149 sc->re_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
1150 RF_SHAREABLE | RF_ACTIVE);
1152 if (sc->re_irq == NULL) {
1153 device_printf(dev, "couldn't map interrupt\n");
1154 error = ENXIO;
1155 goto fail;
1158 /* Reset the adapter. */
1159 re_reset(sc);
1161 hwrev = CSR_READ_4(sc, RE_TXCFG) & RE_TXCFG_HWREV;
1162 for (hw_rev = re_hwrevs; hw_rev->re_desc != NULL; hw_rev++) {
1163 if (hw_rev->re_rev == hwrev) {
1164 sc->re_type = hw_rev->re_type;
1165 sc->re_flags = hw_rev->re_flags;
1166 break;
1170 sc->re_eewidth = 6;
1171 re_read_eeprom(sc, (caddr_t)&re_did, 0, 1);
1172 if (re_did != 0x8129)
1173 sc->re_eewidth = 8;
1176 * Get station address from the EEPROM.
1178 re_read_eeprom(sc, (caddr_t)as, RE_EE_EADDR, 3);
1179 for (i = 0; i < ETHER_ADDR_LEN / 2; i++)
1180 as[i] = le16toh(as[i]);
1181 bcopy(as, eaddr, sizeof(eaddr));
1183 if (sc->re_type == RE_8169) {
1184 /* Set RX length mask */
1185 sc->re_rxlenmask = RE_RDESC_STAT_GFRAGLEN;
1186 sc->re_txstart = RE_GTXSTART;
1187 } else {
1188 /* Set RX length mask */
1189 sc->re_rxlenmask = RE_RDESC_STAT_FRAGLEN;
1190 sc->re_txstart = RE_TXSTART;
1194 * Allocate the parent bus DMA tag appropriate for PCI.
1196 #define RE_NSEG_NEW 32
1197 error = bus_dma_tag_create(NULL, /* parent */
1198 1, 0, /* alignment, boundary */
1199 BUS_SPACE_MAXADDR_32BIT,/* lowaddr */
1200 BUS_SPACE_MAXADDR, /* highaddr */
1201 NULL, NULL, /* filter, filterarg */
1202 MAXBSIZE, RE_NSEG_NEW, /* maxsize, nsegments */
1203 BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */
1204 BUS_DMA_ALLOCNOW, /* flags */
1205 &sc->re_parent_tag);
1206 if (error)
1207 goto fail;
1209 error = re_allocmem(dev, sc);
1211 if (error)
1212 goto fail;
1214 /* Do MII setup */
1215 if (mii_phy_probe(dev, &sc->re_miibus,
1216 re_ifmedia_upd, re_ifmedia_sts)) {
1217 device_printf(dev, "MII without any phy!\n");
1218 error = ENXIO;
1219 goto fail;
1222 ifp = &sc->arpcom.ac_if;
1223 ifp->if_softc = sc;
1224 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
1225 ifp->if_mtu = ETHERMTU;
1226 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1227 ifp->if_ioctl = re_ioctl;
1228 ifp->if_start = re_start;
1229 ifp->if_capabilities = IFCAP_VLAN_MTU | IFCAP_VLAN_HWTAGGING;
1230 if (hwrev != RE_HWREV_8168C) /* XXX does not work yet */
1231 ifp->if_capabilities |= IFCAP_HWCSUM;
1232 #ifdef DEVICE_POLLING
1233 ifp->if_poll = re_poll;
1234 #endif
1235 ifp->if_watchdog = re_watchdog;
1236 ifp->if_init = re_init;
1237 if (sc->re_type == RE_8169)
1238 ifp->if_baudrate = 1000000000;
1239 else
1240 ifp->if_baudrate = 100000000;
1241 ifq_set_maxlen(&ifp->if_snd, RE_IFQ_MAXLEN);
1242 ifq_set_ready(&ifp->if_snd);
1244 #ifdef RE_DISABLE_HWCSUM
1245 ifp->if_capenable = ifp->if_capabilities & ~IFCAP_HWCSUM;
1246 ifp->if_hwassist = 0;
1247 #else
1248 ifp->if_capenable = ifp->if_capabilities;
1249 if (ifp->if_capabilities & IFCAP_HWCSUM)
1250 ifp->if_hwassist = RE_CSUM_FEATURES;
1251 else
1252 ifp->if_hwassist = 0;
1253 #endif /* RE_DISABLE_HWCSUM */
1256 * Call MI attach routine.
1258 ether_ifattach(ifp, eaddr, NULL);
1260 #ifdef RE_DIAG
1262 * Perform hardware diagnostic on the original RTL8169.
1263 * Some 32-bit cards were incorrectly wired and would
1264 * malfunction if plugged into a 64-bit slot.
1266 if (hwrev == RE_HWREV_8169) {
1267 lwkt_serialize_enter(ifp->if_serializer);
1268 error = re_diag(sc);
1269 lwkt_serialize_exit(ifp->if_serializer);
1271 if (error) {
1272 device_printf(dev, "hardware diagnostic failure\n");
1273 ether_ifdetach(ifp);
1274 goto fail;
1277 #endif /* RE_DIAG */
1279 /* Hook interrupt last to avoid having to lock softc */
1280 error = bus_setup_intr(dev, sc->re_irq, INTR_NETSAFE, re_intr, sc,
1281 &sc->re_intrhand, ifp->if_serializer);
1283 if (error) {
1284 device_printf(dev, "couldn't set up irq\n");
1285 ether_ifdetach(ifp);
1286 goto fail;
1289 fail:
1290 if (error)
1291 re_detach(dev);
1293 return (error);
1297 * Shutdown hardware and free up resources. This can be called any
1298 * time after the mutex has been initialized. It is called in both
1299 * the error case in attach and the normal detach case so it needs
1300 * to be careful about only freeing resources that have actually been
1301 * allocated.
1303 static int
1304 re_detach(device_t dev)
1306 struct re_softc *sc = device_get_softc(dev);
1307 struct ifnet *ifp = &sc->arpcom.ac_if;
1308 int i;
1310 /* These should only be active if attach succeeded */
1311 if (device_is_attached(dev)) {
1312 lwkt_serialize_enter(ifp->if_serializer);
1313 re_stop(sc);
1314 bus_teardown_intr(dev, sc->re_irq, sc->re_intrhand);
1315 lwkt_serialize_exit(ifp->if_serializer);
1317 ether_ifdetach(ifp);
1319 if (sc->re_miibus)
1320 device_delete_child(dev, sc->re_miibus);
1321 bus_generic_detach(dev);
1323 if (sc->re_irq)
1324 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->re_irq);
1325 if (sc->re_res) {
1326 bus_release_resource(dev, SYS_RES_IOPORT, RE_PCI_LOIO,
1327 sc->re_res);
1330 /* Unload and free the RX DMA ring memory and map */
1332 if (sc->re_ldata.re_rx_list_tag) {
1333 bus_dmamap_unload(sc->re_ldata.re_rx_list_tag,
1334 sc->re_ldata.re_rx_list_map);
1335 bus_dmamem_free(sc->re_ldata.re_rx_list_tag,
1336 sc->re_ldata.re_rx_list,
1337 sc->re_ldata.re_rx_list_map);
1338 bus_dma_tag_destroy(sc->re_ldata.re_rx_list_tag);
1341 /* Unload and free the TX DMA ring memory and map */
1343 if (sc->re_ldata.re_tx_list_tag) {
1344 bus_dmamap_unload(sc->re_ldata.re_tx_list_tag,
1345 sc->re_ldata.re_tx_list_map);
1346 bus_dmamem_free(sc->re_ldata.re_tx_list_tag,
1347 sc->re_ldata.re_tx_list,
1348 sc->re_ldata.re_tx_list_map);
1349 bus_dma_tag_destroy(sc->re_ldata.re_tx_list_tag);
1352 /* Destroy all the RX and TX buffer maps */
1354 if (sc->re_ldata.re_mtag) {
1355 for (i = 0; i < RE_TX_DESC_CNT; i++)
1356 bus_dmamap_destroy(sc->re_ldata.re_mtag,
1357 sc->re_ldata.re_tx_dmamap[i]);
1358 for (i = 0; i < RE_RX_DESC_CNT; i++)
1359 bus_dmamap_destroy(sc->re_ldata.re_mtag,
1360 sc->re_ldata.re_rx_dmamap[i]);
1361 bus_dma_tag_destroy(sc->re_ldata.re_mtag);
1364 /* Unload and free the stats buffer and map */
1366 if (sc->re_ldata.re_stag) {
1367 bus_dmamap_unload(sc->re_ldata.re_stag,
1368 sc->re_ldata.re_rx_list_map);
1369 bus_dmamem_free(sc->re_ldata.re_stag,
1370 sc->re_ldata.re_stats,
1371 sc->re_ldata.re_smap);
1372 bus_dma_tag_destroy(sc->re_ldata.re_stag);
1375 if (sc->re_parent_tag)
1376 bus_dma_tag_destroy(sc->re_parent_tag);
1378 return(0);
1381 static int
1382 re_newbuf(struct re_softc *sc, int idx, struct mbuf *m)
1384 struct re_dmaload_arg arg;
1385 struct mbuf *n = NULL;
1386 int error;
1388 if (m == NULL) {
1389 n = m_getcl(MB_DONTWAIT, MT_DATA, M_PKTHDR);
1390 if (n == NULL)
1391 return(ENOBUFS);
1392 m = n;
1393 } else
1394 m->m_data = m->m_ext.ext_buf;
1396 m->m_len = m->m_pkthdr.len = MCLBYTES;
1399 * NOTE:
1400 * Some re(4) chips(e.g. RTL8101E) need address of the receive buffer
1401 * to be 8-byte aligned, so don't call m_adj(m, ETHER_ALIGN) here.
1404 arg.sc = sc;
1405 arg.re_idx = idx;
1406 arg.re_maxsegs = 1;
1407 arg.re_flags = 0;
1408 arg.re_ring = sc->re_ldata.re_rx_list;
1410 error = bus_dmamap_load_mbuf(sc->re_ldata.re_mtag,
1411 sc->re_ldata.re_rx_dmamap[idx], m, re_dma_map_desc,
1412 &arg, BUS_DMA_NOWAIT);
1413 if (error || arg.re_maxsegs != 1) {
1414 if (n != NULL)
1415 m_freem(n);
1416 return (ENOMEM);
1419 sc->re_ldata.re_rx_list[idx].re_cmdstat |= htole32(RE_RDESC_CMD_OWN);
1420 sc->re_ldata.re_rx_mbuf[idx] = m;
1422 bus_dmamap_sync(sc->re_ldata.re_mtag, sc->re_ldata.re_rx_dmamap[idx],
1423 BUS_DMASYNC_PREREAD);
1425 return(0);
1428 static int
1429 re_tx_list_init(struct re_softc *sc)
1431 bzero(sc->re_ldata.re_tx_list, RE_TX_LIST_SZ);
1432 bzero(&sc->re_ldata.re_tx_mbuf, RE_TX_DESC_CNT * sizeof(struct mbuf *));
1434 bus_dmamap_sync(sc->re_ldata.re_tx_list_tag,
1435 sc->re_ldata.re_tx_list_map, BUS_DMASYNC_PREWRITE);
1436 sc->re_ldata.re_tx_prodidx = 0;
1437 sc->re_ldata.re_tx_considx = 0;
1438 sc->re_ldata.re_tx_free = RE_TX_DESC_CNT;
1440 return(0);
1443 static int
1444 re_rx_list_init(struct re_softc *sc)
1446 int i, error;
1448 bzero(sc->re_ldata.re_rx_list, RE_RX_LIST_SZ);
1449 bzero(&sc->re_ldata.re_rx_mbuf, RE_RX_DESC_CNT * sizeof(struct mbuf *));
1451 for (i = 0; i < RE_RX_DESC_CNT; i++) {
1452 error = re_newbuf(sc, i, NULL);
1453 if (error)
1454 return(error);
1457 /* Flush the RX descriptors */
1459 bus_dmamap_sync(sc->re_ldata.re_rx_list_tag,
1460 sc->re_ldata.re_rx_list_map, BUS_DMASYNC_PREWRITE);
1462 sc->re_ldata.re_rx_prodidx = 0;
1463 sc->re_head = sc->re_tail = NULL;
1465 return(0);
1469 * RX handler for C+ and 8169. For the gigE chips, we support
1470 * the reception of jumbo frames that have been fragmented
1471 * across multiple 2K mbuf cluster buffers.
1473 static void
1474 re_rxeof(struct re_softc *sc)
1476 struct ifnet *ifp = &sc->arpcom.ac_if;
1477 struct mbuf *m;
1478 struct re_desc *cur_rx;
1479 uint32_t rxstat, rxvlan;
1480 int i, total_len;
1482 /* Invalidate the descriptor memory */
1484 bus_dmamap_sync(sc->re_ldata.re_rx_list_tag,
1485 sc->re_ldata.re_rx_list_map, BUS_DMASYNC_POSTREAD);
1487 for (i = sc->re_ldata.re_rx_prodidx;
1488 RE_OWN(&sc->re_ldata.re_rx_list[i]) == 0 ; RE_DESC_INC(i)) {
1489 cur_rx = &sc->re_ldata.re_rx_list[i];
1490 m = sc->re_ldata.re_rx_mbuf[i];
1491 total_len = RE_RXBYTES(cur_rx);
1492 rxstat = le32toh(cur_rx->re_cmdstat);
1493 rxvlan = le32toh(cur_rx->re_vlanctl);
1495 /* Invalidate the RX mbuf and unload its map */
1497 bus_dmamap_sync(sc->re_ldata.re_mtag,
1498 sc->re_ldata.re_rx_dmamap[i],
1499 BUS_DMASYNC_POSTWRITE);
1500 bus_dmamap_unload(sc->re_ldata.re_mtag,
1501 sc->re_ldata.re_rx_dmamap[i]);
1503 if ((rxstat & RE_RDESC_STAT_EOF) == 0) {
1504 m->m_len = MCLBYTES - ETHER_ALIGN;
1505 if (sc->re_head == NULL) {
1506 sc->re_head = sc->re_tail = m;
1507 } else {
1508 sc->re_tail->m_next = m;
1509 sc->re_tail = m;
1511 re_newbuf(sc, i, NULL);
1512 continue;
1516 * NOTE: for the 8139C+, the frame length field
1517 * is always 12 bits in size, but for the gigE chips,
1518 * it is 13 bits (since the max RX frame length is 16K).
1519 * Unfortunately, all 32 bits in the status word
1520 * were already used, so to make room for the extra
1521 * length bit, RealTek took out the 'frame alignment
1522 * error' bit and shifted the other status bits
1523 * over one slot. The OWN, EOR, FS and LS bits are
1524 * still in the same places. We have already extracted
1525 * the frame length and checked the OWN bit, so rather
1526 * than using an alternate bit mapping, we shift the
1527 * status bits one space to the right so we can evaluate
1528 * them using the 8169 status as though it was in the
1529 * same format as that of the 8139C+.
1531 if (sc->re_type == RE_8169)
1532 rxstat >>= 1;
1534 if (rxstat & RE_RDESC_STAT_RXERRSUM) {
1535 ifp->if_ierrors++;
1537 * If this is part of a multi-fragment packet,
1538 * discard all the pieces.
1540 if (sc->re_head != NULL) {
1541 m_freem(sc->re_head);
1542 sc->re_head = sc->re_tail = NULL;
1544 re_newbuf(sc, i, m);
1545 continue;
1549 * If allocating a replacement mbuf fails,
1550 * reload the current one.
1553 if (re_newbuf(sc, i, NULL)) {
1554 ifp->if_ierrors++;
1555 if (sc->re_head != NULL) {
1556 m_freem(sc->re_head);
1557 sc->re_head = sc->re_tail = NULL;
1559 re_newbuf(sc, i, m);
1560 continue;
1563 if (sc->re_head != NULL) {
1564 m->m_len = total_len % (MCLBYTES - ETHER_ALIGN);
1566 * Special case: if there's 4 bytes or less
1567 * in this buffer, the mbuf can be discarded:
1568 * the last 4 bytes is the CRC, which we don't
1569 * care about anyway.
1571 if (m->m_len <= ETHER_CRC_LEN) {
1572 sc->re_tail->m_len -=
1573 (ETHER_CRC_LEN - m->m_len);
1574 m_freem(m);
1575 } else {
1576 m->m_len -= ETHER_CRC_LEN;
1577 sc->re_tail->m_next = m;
1579 m = sc->re_head;
1580 sc->re_head = sc->re_tail = NULL;
1581 m->m_pkthdr.len = total_len - ETHER_CRC_LEN;
1582 } else
1583 m->m_pkthdr.len = m->m_len =
1584 (total_len - ETHER_CRC_LEN);
1586 ifp->if_ipackets++;
1587 m->m_pkthdr.rcvif = ifp;
1589 /* Do RX checksumming if enabled */
1591 if (ifp->if_capenable & IFCAP_RXCSUM) {
1593 /* Check IP header checksum */
1594 if (rxstat & RE_RDESC_STAT_PROTOID)
1595 m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
1596 if ((rxstat & RE_RDESC_STAT_IPSUMBAD) == 0)
1597 m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
1599 /* Check TCP/UDP checksum */
1600 if ((RE_TCPPKT(rxstat) &&
1601 (rxstat & RE_RDESC_STAT_TCPSUMBAD) == 0) ||
1602 (RE_UDPPKT(rxstat) &&
1603 (rxstat & RE_RDESC_STAT_UDPSUMBAD)) == 0) {
1604 m->m_pkthdr.csum_flags |=
1605 CSUM_DATA_VALID|CSUM_PSEUDO_HDR|
1606 CSUM_FRAG_NOT_CHECKED;
1607 m->m_pkthdr.csum_data = 0xffff;
1611 if (rxvlan & RE_RDESC_VLANCTL_TAG) {
1612 VLAN_INPUT_TAG(m,
1613 be16toh((rxvlan & RE_RDESC_VLANCTL_DATA)));
1614 } else {
1615 ifp->if_input(ifp, m);
1619 /* Flush the RX DMA ring */
1621 bus_dmamap_sync(sc->re_ldata.re_rx_list_tag,
1622 sc->re_ldata.re_rx_list_map, BUS_DMASYNC_PREWRITE);
1624 sc->re_ldata.re_rx_prodidx = i;
1627 static void
1628 re_txeof(struct re_softc *sc)
1630 struct ifnet *ifp = &sc->arpcom.ac_if;
1631 uint32_t txstat;
1632 int idx;
1634 /* Invalidate the TX descriptor list */
1636 bus_dmamap_sync(sc->re_ldata.re_tx_list_tag,
1637 sc->re_ldata.re_tx_list_map, BUS_DMASYNC_POSTREAD);
1639 for (idx = sc->re_ldata.re_tx_considx;
1640 sc->re_ldata.re_tx_free < RE_TX_DESC_CNT; RE_DESC_INC(idx)) {
1641 txstat = le32toh(sc->re_ldata.re_tx_list[idx].re_cmdstat);
1642 if (txstat & RE_TDESC_CMD_OWN)
1643 break;
1645 sc->re_ldata.re_tx_list[idx].re_bufaddr_lo = 0;
1648 * We only stash mbufs in the last descriptor
1649 * in a fragment chain, which also happens to
1650 * be the only place where the TX status bits
1651 * are valid.
1653 if (txstat & RE_TDESC_CMD_EOF) {
1654 m_freem(sc->re_ldata.re_tx_mbuf[idx]);
1655 sc->re_ldata.re_tx_mbuf[idx] = NULL;
1656 bus_dmamap_unload(sc->re_ldata.re_mtag,
1657 sc->re_ldata.re_tx_dmamap[idx]);
1658 if (txstat & (RE_TDESC_STAT_EXCESSCOL|
1659 RE_TDESC_STAT_COLCNT))
1660 ifp->if_collisions++;
1661 if (txstat & RE_TDESC_STAT_TXERRSUM)
1662 ifp->if_oerrors++;
1663 else
1664 ifp->if_opackets++;
1666 sc->re_ldata.re_tx_free++;
1669 /* No changes made to the TX ring, so no flush needed */
1670 if (sc->re_ldata.re_tx_free) {
1671 sc->re_ldata.re_tx_considx = idx;
1672 ifp->if_flags &= ~IFF_OACTIVE;
1673 ifp->if_timer = 0;
1677 * Some chips will ignore a second TX request issued while an
1678 * existing transmission is in progress. If the transmitter goes
1679 * idle but there are still packets waiting to be sent, we need
1680 * to restart the channel here to flush them out. This only seems
1681 * to be required with the PCIe devices.
1683 if (sc->re_ldata.re_tx_free < RE_TX_DESC_CNT)
1684 CSR_WRITE_1(sc, sc->re_txstart, RE_TXSTART_START);
1687 * If not all descriptors have been released reaped yet,
1688 * reload the timer so that we will eventually get another
1689 * interrupt that will cause us to re-enter this routine.
1690 * This is done in case the transmitter has gone idle.
1692 if (RE_TX_MODERATION_IS_ENABLED(sc) &&
1693 sc->re_ldata.re_tx_free < RE_TX_DESC_CNT)
1694 CSR_WRITE_4(sc, RE_TIMERCNT, 1);
1697 static void
1698 re_tick(void *xsc)
1700 struct re_softc *sc = xsc;
1702 lwkt_serialize_enter(sc->arpcom.ac_if.if_serializer);
1703 re_tick_serialized(xsc);
1704 lwkt_serialize_exit(sc->arpcom.ac_if.if_serializer);
1707 static void
1708 re_tick_serialized(void *xsc)
1710 struct re_softc *sc = xsc;
1711 struct ifnet *ifp = &sc->arpcom.ac_if;
1712 struct mii_data *mii;
1714 mii = device_get_softc(sc->re_miibus);
1715 mii_tick(mii);
1716 if (sc->re_link) {
1717 if (!(mii->mii_media_status & IFM_ACTIVE))
1718 sc->re_link = 0;
1719 } else {
1720 if (mii->mii_media_status & IFM_ACTIVE &&
1721 IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
1722 sc->re_link = 1;
1723 if (!ifq_is_empty(&ifp->if_snd))
1724 ifp->if_start(ifp);
1728 callout_reset(&sc->re_timer, hz, re_tick, sc);
1731 #ifdef DEVICE_POLLING
1733 static void
1734 re_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
1736 struct re_softc *sc = ifp->if_softc;
1738 switch(cmd) {
1739 case POLL_REGISTER:
1740 /* disable interrupts */
1741 CSR_WRITE_2(sc, RE_IMR, 0x0000);
1742 break;
1743 case POLL_DEREGISTER:
1744 /* enable interrupts */
1745 CSR_WRITE_2(sc, RE_IMR, sc->re_intrs);
1746 break;
1747 default:
1748 sc->rxcycles = count;
1749 re_rxeof(sc);
1750 re_txeof(sc);
1752 if (!ifq_is_empty(&ifp->if_snd))
1753 (*ifp->if_start)(ifp);
1755 if (cmd == POLL_AND_CHECK_STATUS) { /* also check status register */
1756 uint16_t status;
1758 status = CSR_READ_2(sc, RE_ISR);
1759 if (status == 0xffff)
1760 return;
1761 if (status)
1762 CSR_WRITE_2(sc, RE_ISR, status);
1765 * XXX check behaviour on receiver stalls.
1768 if (status & RE_ISR_SYSTEM_ERR) {
1769 re_reset(sc);
1770 re_init(sc);
1773 break;
1776 #endif /* DEVICE_POLLING */
1778 static void
1779 re_intr(void *arg)
1781 struct re_softc *sc = arg;
1782 struct ifnet *ifp = &sc->arpcom.ac_if;
1783 uint16_t status;
1785 if (sc->suspended || (ifp->if_flags & IFF_UP) == 0)
1786 return;
1788 for (;;) {
1789 status = CSR_READ_2(sc, RE_ISR);
1790 /* If the card has gone away the read returns 0xffff. */
1791 if (status == 0xffff)
1792 break;
1793 if (status)
1794 CSR_WRITE_2(sc, RE_ISR, status);
1796 if ((status & sc->re_intrs) == 0)
1797 break;
1799 if (status & (RE_ISR_RX_OK | RE_ISR_RX_ERR | RE_ISR_FIFO_OFLOW))
1800 re_rxeof(sc);
1802 if ((status & sc->re_tx_ack) ||
1803 (status & RE_ISR_TX_ERR) ||
1804 (status & RE_ISR_TX_DESC_UNAVAIL))
1805 re_txeof(sc);
1807 if (status & RE_ISR_SYSTEM_ERR) {
1808 re_reset(sc);
1809 re_init(sc);
1812 if (status & RE_ISR_LINKCHG) {
1813 callout_stop(&sc->re_timer);
1814 re_tick_serialized(sc);
1818 if (!ifq_is_empty(&ifp->if_snd))
1819 (*ifp->if_start)(ifp);
1822 static int
1823 re_encap(struct re_softc *sc, struct mbuf **m_head, int *idx, int *called_defrag)
1825 struct ifnet *ifp = &sc->arpcom.ac_if;
1826 struct mbuf *m, *m_new = NULL;
1827 struct re_dmaload_arg arg;
1828 bus_dmamap_t map;
1829 int error;
1831 *called_defrag = 0;
1832 if (sc->re_ldata.re_tx_free <= 4)
1833 return(EFBIG);
1835 m = *m_head;
1838 * Set up checksum offload. Note: checksum offload bits must
1839 * appear in all descriptors of a multi-descriptor transmit
1840 * attempt. (This is according to testing done with an 8169
1841 * chip. I'm not sure if this is a requirement or a bug.)
1844 arg.re_flags = 0;
1846 if (m->m_pkthdr.csum_flags & CSUM_IP)
1847 arg.re_flags |= RE_TDESC_CMD_IPCSUM;
1848 if (m->m_pkthdr.csum_flags & CSUM_TCP)
1849 arg.re_flags |= RE_TDESC_CMD_TCPCSUM;
1850 if (m->m_pkthdr.csum_flags & CSUM_UDP)
1851 arg.re_flags |= RE_TDESC_CMD_UDPCSUM;
1853 arg.sc = sc;
1854 arg.re_idx = *idx;
1855 arg.re_maxsegs = sc->re_ldata.re_tx_free;
1856 if (arg.re_maxsegs > 4)
1857 arg.re_maxsegs -= 4;
1858 arg.re_ring = sc->re_ldata.re_tx_list;
1860 map = sc->re_ldata.re_tx_dmamap[*idx];
1863 * With some of the RealTek chips, using the checksum offload
1864 * support in conjunction with the autopadding feature results
1865 * in the transmission of corrupt frames. For example, if we
1866 * need to send a really small IP fragment that's less than 60
1867 * bytes in size, and IP header checksumming is enabled, the
1868 * resulting ethernet frame that appears on the wire will
1869 * have garbled payload. To work around this, if TX checksum
1870 * offload is enabled, we always manually pad short frames out
1871 * to the minimum ethernet frame size. We do this by pretending
1872 * the mbuf chain has too many fragments so the coalescing code
1873 * below can assemble the packet into a single buffer that's
1874 * padded out to the mininum frame size.
1876 * Note: this appears unnecessary for TCP, and doing it for TCP
1877 * with PCIe adapters seems to result in bad checksums.
1879 if (arg.re_flags && !(arg.re_flags & RE_TDESC_CMD_TCPCSUM) &&
1880 m->m_pkthdr.len < RE_MIN_FRAMELEN) {
1881 error = EFBIG;
1882 } else {
1883 error = bus_dmamap_load_mbuf(sc->re_ldata.re_mtag, map,
1884 m, re_dma_map_desc, &arg, BUS_DMA_NOWAIT);
1887 if (error && error != EFBIG) {
1888 if_printf(ifp, "can't map mbuf (error %d)\n", error);
1889 return(ENOBUFS);
1892 /* Too many segments to map, coalesce into a single mbuf */
1894 if (error || arg.re_maxsegs == 0) {
1895 m_new = m_defrag_nofree(m, MB_DONTWAIT);
1896 if (m_new == NULL) {
1897 return(1);
1898 } else {
1899 m = m_new;
1900 *m_head = m;
1904 * Manually pad short frames, and zero the pad space
1905 * to avoid leaking data.
1907 if (m_new->m_pkthdr.len < RE_MIN_FRAMELEN) {
1908 bzero(mtod(m_new, char *) + m_new->m_pkthdr.len,
1909 RE_MIN_FRAMELEN - m_new->m_pkthdr.len);
1910 m_new->m_pkthdr.len += RE_MIN_FRAMELEN -
1911 m_new->m_pkthdr.len;
1912 m_new->m_len = m_new->m_pkthdr.len;
1915 *called_defrag = 1;
1916 arg.sc = sc;
1917 arg.re_idx = *idx;
1918 arg.re_maxsegs = sc->re_ldata.re_tx_free;
1919 arg.re_ring = sc->re_ldata.re_tx_list;
1921 error = bus_dmamap_load_mbuf(sc->re_ldata.re_mtag, map,
1922 m, re_dma_map_desc, &arg, BUS_DMA_NOWAIT);
1923 if (error) {
1924 m_freem(m);
1925 if_printf(ifp, "can't map mbuf (error %d)\n", error);
1926 return(EFBIG);
1931 * Insure that the map for this transmission
1932 * is placed at the array index of the last descriptor
1933 * in this chain.
1935 sc->re_ldata.re_tx_dmamap[*idx] =
1936 sc->re_ldata.re_tx_dmamap[arg.re_idx];
1937 sc->re_ldata.re_tx_dmamap[arg.re_idx] = map;
1939 sc->re_ldata.re_tx_mbuf[arg.re_idx] = m;
1940 sc->re_ldata.re_tx_free -= arg.re_maxsegs;
1943 * Set up hardware VLAN tagging. Note: vlan tag info must
1944 * appear in the first descriptor of a multi-descriptor
1945 * transmission attempt.
1948 if (m->m_flags & M_VLANTAG) {
1949 sc->re_ldata.re_tx_list[*idx].re_vlanctl =
1950 htole32(htobe16(m->m_pkthdr.ether_vlantag) |
1951 RE_TDESC_VLANCTL_TAG);
1954 /* Transfer ownership of packet to the chip. */
1956 sc->re_ldata.re_tx_list[arg.re_idx].re_cmdstat |=
1957 htole32(RE_TDESC_CMD_OWN);
1958 if (*idx != arg.re_idx)
1959 sc->re_ldata.re_tx_list[*idx].re_cmdstat |=
1960 htole32(RE_TDESC_CMD_OWN);
1962 RE_DESC_INC(arg.re_idx);
1963 *idx = arg.re_idx;
1965 return(0);
1969 * Main transmit routine for C+ and gigE NICs.
1972 static void
1973 re_start(struct ifnet *ifp)
1975 struct re_softc *sc = ifp->if_softc;
1976 struct mbuf *m_head;
1977 struct mbuf *m_head2;
1978 int called_defrag, idx, need_trans;
1980 if (!sc->re_link || (ifp->if_flags & IFF_OACTIVE))
1981 return;
1983 idx = sc->re_ldata.re_tx_prodidx;
1985 need_trans = 0;
1986 while (sc->re_ldata.re_tx_mbuf[idx] == NULL) {
1987 m_head = ifq_poll(&ifp->if_snd);
1988 if (m_head == NULL)
1989 break;
1990 m_head2 = m_head;
1991 if (re_encap(sc, &m_head2, &idx, &called_defrag)) {
1993 * If we could not encapsulate the defragged packet,
1994 * the returned m_head2 is garbage and we must dequeue
1995 * and throw away the original packet.
1997 if (called_defrag) {
1998 ifq_dequeue(&ifp->if_snd, m_head);
1999 m_freem(m_head);
2001 ifp->if_flags |= IFF_OACTIVE;
2002 break;
2006 * Clean out the packet we encapsulated. If we defragged
2007 * the packet the m_head2 is the one that got encapsulated
2008 * and the original must be thrown away. Otherwise m_head2
2009 * *IS* the original.
2011 ifq_dequeue(&ifp->if_snd, m_head);
2012 if (called_defrag)
2013 m_freem(m_head);
2014 need_trans = 1;
2017 * If there's a BPF listener, bounce a copy of this frame
2018 * to him.
2020 ETHER_BPF_MTAP(ifp, m_head2);
2023 if (!need_trans) {
2024 if (RE_TX_MODERATION_IS_ENABLED(sc) &&
2025 sc->re_ldata.re_tx_free != RE_TX_DESC_CNT)
2026 CSR_WRITE_4(sc, RE_TIMERCNT, 1);
2027 return;
2030 /* Flush the TX descriptors */
2031 bus_dmamap_sync(sc->re_ldata.re_tx_list_tag,
2032 sc->re_ldata.re_tx_list_map, BUS_DMASYNC_PREWRITE);
2034 sc->re_ldata.re_tx_prodidx = idx;
2037 * RealTek put the TX poll request register in a different
2038 * location on the 8169 gigE chip. I don't know why.
2040 CSR_WRITE_1(sc, sc->re_txstart, RE_TXSTART_START);
2042 if (RE_TX_MODERATION_IS_ENABLED(sc)) {
2044 * Use the countdown timer for interrupt moderation.
2045 * 'TX done' interrupts are disabled. Instead, we reset the
2046 * countdown timer, which will begin counting until it hits
2047 * the value in the TIMERINT register, and then trigger an
2048 * interrupt. Each time we write to the TIMERCNT register,
2049 * the timer count is reset to 0.
2051 CSR_WRITE_4(sc, RE_TIMERCNT, 1);
2055 * Set a timeout in case the chip goes out to lunch.
2057 ifp->if_timer = 5;
2060 static void
2061 re_init(void *xsc)
2063 struct re_softc *sc = xsc;
2064 struct ifnet *ifp = &sc->arpcom.ac_if;
2065 struct mii_data *mii;
2066 uint32_t rxcfg = 0;
2068 mii = device_get_softc(sc->re_miibus);
2071 * Cancel pending I/O and free all RX/TX buffers.
2073 re_stop(sc);
2076 * Enable C+ RX and TX mode, as well as VLAN stripping and
2077 * RX checksum offload. We must configure the C+ register
2078 * before all others.
2080 CSR_WRITE_2(sc, RE_CPLUS_CMD, RE_CPLUSCMD_RXENB | RE_CPLUSCMD_TXENB |
2081 RE_CPLUSCMD_PCI_MRW | RE_CPLUSCMD_VLANSTRIP |
2082 (ifp->if_capenable & IFCAP_RXCSUM ?
2083 RE_CPLUSCMD_RXCSUM_ENB : 0));
2086 * Init our MAC address. Even though the chipset
2087 * documentation doesn't mention it, we need to enter "Config
2088 * register write enable" mode to modify the ID registers.
2090 CSR_WRITE_1(sc, RE_EECMD, RE_EEMODE_WRITECFG);
2091 CSR_WRITE_4(sc, RE_IDR0,
2092 htole32(*(uint32_t *)(&sc->arpcom.ac_enaddr[0])));
2093 CSR_WRITE_2(sc, RE_IDR4,
2094 htole16(*(uint16_t *)(&sc->arpcom.ac_enaddr[4])));
2095 CSR_WRITE_1(sc, RE_EECMD, RE_EEMODE_OFF);
2098 * For C+ mode, initialize the RX descriptors and mbufs.
2100 re_rx_list_init(sc);
2101 re_tx_list_init(sc);
2104 * Load the addresses of the RX and TX lists into the chip.
2106 CSR_WRITE_4(sc, RE_RXLIST_ADDR_HI,
2107 RE_ADDR_HI(sc->re_ldata.re_rx_list_addr));
2108 CSR_WRITE_4(sc, RE_RXLIST_ADDR_LO,
2109 RE_ADDR_LO(sc->re_ldata.re_rx_list_addr));
2111 CSR_WRITE_4(sc, RE_TXLIST_ADDR_HI,
2112 RE_ADDR_HI(sc->re_ldata.re_tx_list_addr));
2113 CSR_WRITE_4(sc, RE_TXLIST_ADDR_LO,
2114 RE_ADDR_LO(sc->re_ldata.re_tx_list_addr));
2117 * Enable transmit and receive.
2119 CSR_WRITE_1(sc, RE_COMMAND, RE_CMD_TX_ENB|RE_CMD_RX_ENB);
2122 * Set the initial TX and RX configuration.
2124 if (sc->re_testmode) {
2125 if (sc->re_type == RE_8169)
2126 CSR_WRITE_4(sc, RE_TXCFG,
2127 RE_TXCFG_CONFIG | RE_LOOPTEST_ON);
2128 else
2129 CSR_WRITE_4(sc, RE_TXCFG,
2130 RE_TXCFG_CONFIG | RE_LOOPTEST_ON_CPLUS);
2131 } else
2132 CSR_WRITE_4(sc, RE_TXCFG, RE_TXCFG_CONFIG);
2134 CSR_WRITE_1(sc, RE_EARLY_TX_THRESH, 16);
2136 CSR_WRITE_4(sc, RE_RXCFG, RE_RXCFG_CONFIG);
2138 /* Set the individual bit to receive frames for this host only. */
2139 rxcfg = CSR_READ_4(sc, RE_RXCFG);
2140 rxcfg |= RE_RXCFG_RX_INDIV;
2142 /* If we want promiscuous mode, set the allframes bit. */
2143 if (ifp->if_flags & IFF_PROMISC) {
2144 rxcfg |= RE_RXCFG_RX_ALLPHYS;
2145 CSR_WRITE_4(sc, RE_RXCFG, rxcfg);
2146 } else {
2147 rxcfg &= ~RE_RXCFG_RX_ALLPHYS;
2148 CSR_WRITE_4(sc, RE_RXCFG, rxcfg);
2152 * Set capture broadcast bit to capture broadcast frames.
2154 if (ifp->if_flags & IFF_BROADCAST) {
2155 rxcfg |= RE_RXCFG_RX_BROAD;
2156 CSR_WRITE_4(sc, RE_RXCFG, rxcfg);
2157 } else {
2158 rxcfg &= ~RE_RXCFG_RX_BROAD;
2159 CSR_WRITE_4(sc, RE_RXCFG, rxcfg);
2163 * Program the multicast filter, if necessary.
2165 re_setmulti(sc);
2167 #ifdef DEVICE_POLLING
2169 * Disable interrupts if we are polling.
2171 if (ifp->if_flags & IFF_POLLING)
2172 CSR_WRITE_2(sc, RE_IMR, 0);
2173 else /* otherwise ... */
2174 #endif /* DEVICE_POLLING */
2176 * Enable interrupts.
2178 if (sc->re_testmode)
2179 CSR_WRITE_2(sc, RE_IMR, 0);
2180 else
2181 CSR_WRITE_2(sc, RE_IMR, sc->re_intrs);
2182 CSR_WRITE_2(sc, RE_ISR, sc->re_intrs);
2184 /* Set initial TX threshold */
2185 sc->re_txthresh = RE_TX_THRESH_INIT;
2187 /* Start RX/TX process. */
2188 if (sc->re_flags & RE_F_HASMPC)
2189 CSR_WRITE_4(sc, RE_MISSEDPKT, 0);
2190 #ifdef notdef
2191 /* Enable receiver and transmitter. */
2192 CSR_WRITE_1(sc, RE_COMMAND, RE_CMD_TX_ENB|RE_CMD_RX_ENB);
2193 #endif
2195 if (RE_TX_MODERATION_IS_ENABLED(sc)) {
2197 * Initialize the timer interrupt register so that
2198 * a timer interrupt will be generated once the timer
2199 * reaches a certain number of ticks. The timer is
2200 * reloaded on each transmit. This gives us TX interrupt
2201 * moderation, which dramatically improves TX frame rate.
2203 if (sc->re_type == RE_8169)
2204 CSR_WRITE_4(sc, RE_TIMERINT_8169, 0x800);
2205 else
2206 CSR_WRITE_4(sc, RE_TIMERINT, 0x400);
2210 * For 8169 gigE NICs, set the max allowed RX packet
2211 * size so we can receive jumbo frames.
2213 if (sc->re_type == RE_8169)
2214 CSR_WRITE_2(sc, RE_MAXRXPKTLEN, 16383);
2216 if (sc->re_testmode) {
2217 return;
2220 mii_mediachg(mii);
2222 CSR_WRITE_1(sc, RE_CFG1, RE_CFG1_DRVLOAD|RE_CFG1_FULLDUPLEX);
2224 ifp->if_flags |= IFF_RUNNING;
2225 ifp->if_flags &= ~IFF_OACTIVE;
2227 sc->re_link = 0;
2228 callout_reset(&sc->re_timer, hz, re_tick, sc);
2232 * Set media options.
2234 static int
2235 re_ifmedia_upd(struct ifnet *ifp)
2237 struct re_softc *sc = ifp->if_softc;
2238 struct mii_data *mii;
2240 mii = device_get_softc(sc->re_miibus);
2241 mii_mediachg(mii);
2243 return(0);
2247 * Report current media status.
2249 static void
2250 re_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
2252 struct re_softc *sc = ifp->if_softc;
2253 struct mii_data *mii;
2255 mii = device_get_softc(sc->re_miibus);
2257 mii_pollstat(mii);
2258 ifmr->ifm_active = mii->mii_media_active;
2259 ifmr->ifm_status = mii->mii_media_status;
2262 static int
2263 re_ioctl(struct ifnet *ifp, u_long command, caddr_t data, struct ucred *cr)
2265 struct re_softc *sc = ifp->if_softc;
2266 struct ifreq *ifr = (struct ifreq *) data;
2267 struct mii_data *mii;
2268 int error = 0;
2270 switch(command) {
2271 case SIOCSIFMTU:
2272 if (ifr->ifr_mtu > RE_JUMBO_MTU)
2273 error = EINVAL;
2274 ifp->if_mtu = ifr->ifr_mtu;
2275 break;
2276 case SIOCSIFFLAGS:
2277 if (ifp->if_flags & IFF_UP)
2278 re_init(sc);
2279 else if (ifp->if_flags & IFF_RUNNING)
2280 re_stop(sc);
2281 break;
2282 case SIOCADDMULTI:
2283 case SIOCDELMULTI:
2284 re_setmulti(sc);
2285 error = 0;
2286 break;
2287 case SIOCGIFMEDIA:
2288 case SIOCSIFMEDIA:
2289 mii = device_get_softc(sc->re_miibus);
2290 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
2291 break;
2292 case SIOCSIFCAP:
2293 ifp->if_capenable &= ~(IFCAP_HWCSUM);
2294 ifp->if_capenable |=
2295 ifr->ifr_reqcap & (IFCAP_HWCSUM);
2296 if (ifp->if_capenable & IFCAP_TXCSUM)
2297 ifp->if_hwassist = RE_CSUM_FEATURES;
2298 else
2299 ifp->if_hwassist = 0;
2300 if (ifp->if_flags & IFF_RUNNING)
2301 re_init(sc);
2302 break;
2303 default:
2304 error = ether_ioctl(ifp, command, data);
2305 break;
2307 return(error);
2310 static void
2311 re_watchdog(struct ifnet *ifp)
2313 struct re_softc *sc = ifp->if_softc;
2315 if_printf(ifp, "watchdog timeout\n");
2317 ifp->if_oerrors++;
2319 re_txeof(sc);
2320 re_rxeof(sc);
2322 re_init(sc);
2324 if (!ifq_is_empty(&ifp->if_snd))
2325 ifp->if_start(ifp);
2329 * Stop the adapter and free any mbufs allocated to the
2330 * RX and TX lists.
2332 static void
2333 re_stop(struct re_softc *sc)
2335 struct ifnet *ifp = &sc->arpcom.ac_if;
2336 int i;
2338 ifp->if_timer = 0;
2339 callout_stop(&sc->re_timer);
2341 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
2343 CSR_WRITE_1(sc, RE_COMMAND, 0x00);
2344 CSR_WRITE_2(sc, RE_IMR, 0x0000);
2345 CSR_WRITE_2(sc, RE_ISR, 0xFFFF);
2347 if (sc->re_head != NULL) {
2348 m_freem(sc->re_head);
2349 sc->re_head = sc->re_tail = NULL;
2352 /* Free the TX list buffers. */
2353 for (i = 0; i < RE_TX_DESC_CNT; i++) {
2354 if (sc->re_ldata.re_tx_mbuf[i] != NULL) {
2355 bus_dmamap_unload(sc->re_ldata.re_mtag,
2356 sc->re_ldata.re_tx_dmamap[i]);
2357 m_freem(sc->re_ldata.re_tx_mbuf[i]);
2358 sc->re_ldata.re_tx_mbuf[i] = NULL;
2362 /* Free the RX list buffers. */
2363 for (i = 0; i < RE_RX_DESC_CNT; i++) {
2364 if (sc->re_ldata.re_rx_mbuf[i] != NULL) {
2365 bus_dmamap_unload(sc->re_ldata.re_mtag,
2366 sc->re_ldata.re_rx_dmamap[i]);
2367 m_freem(sc->re_ldata.re_rx_mbuf[i]);
2368 sc->re_ldata.re_rx_mbuf[i] = NULL;
2374 * Device suspend routine. Stop the interface and save some PCI
2375 * settings in case the BIOS doesn't restore them properly on
2376 * resume.
2378 static int
2379 re_suspend(device_t dev)
2381 #ifndef BURN_BRIDGES
2382 int i;
2383 #endif
2384 struct re_softc *sc = device_get_softc(dev);
2386 re_stop(sc);
2388 #ifndef BURN_BRIDGES
2389 for (i = 0; i < 5; i++)
2390 sc->saved_maps[i] = pci_read_config(dev, PCIR_MAPS + i * 4, 4);
2391 sc->saved_biosaddr = pci_read_config(dev, PCIR_BIOS, 4);
2392 sc->saved_intline = pci_read_config(dev, PCIR_INTLINE, 1);
2393 sc->saved_cachelnsz = pci_read_config(dev, PCIR_CACHELNSZ, 1);
2394 sc->saved_lattimer = pci_read_config(dev, PCIR_LATTIMER, 1);
2395 #endif
2397 sc->suspended = 1;
2399 return (0);
2403 * Device resume routine. Restore some PCI settings in case the BIOS
2404 * doesn't, re-enable busmastering, and restart the interface if
2405 * appropriate.
2407 static int
2408 re_resume(device_t dev)
2410 struct re_softc *sc = device_get_softc(dev);
2411 struct ifnet *ifp = &sc->arpcom.ac_if;
2412 #ifndef BURN_BRIDGES
2413 int i;
2414 #endif
2416 #ifndef BURN_BRIDGES
2417 /* better way to do this? */
2418 for (i = 0; i < 5; i++)
2419 pci_write_config(dev, PCIR_MAPS + i * 4, sc->saved_maps[i], 4);
2420 pci_write_config(dev, PCIR_BIOS, sc->saved_biosaddr, 4);
2421 pci_write_config(dev, PCIR_INTLINE, sc->saved_intline, 1);
2422 pci_write_config(dev, PCIR_CACHELNSZ, sc->saved_cachelnsz, 1);
2423 pci_write_config(dev, PCIR_LATTIMER, sc->saved_lattimer, 1);
2425 /* reenable busmastering */
2426 pci_enable_busmaster(dev);
2427 pci_enable_io(dev, SYS_RES_IOPORT);
2428 #endif
2430 /* reinitialize interface if necessary */
2431 if (ifp->if_flags & IFF_UP)
2432 re_init(sc);
2434 sc->suspended = 0;
2436 return (0);
2440 * Stop all chip I/O so that the kernel's probe routines don't
2441 * get confused by errant DMAs when rebooting.
2443 static void
2444 re_shutdown(device_t dev)
2446 struct re_softc *sc = device_get_softc(dev);
2447 struct ifnet *ifp = &sc->arpcom.ac_if;
2449 lwkt_serialize_enter(ifp->if_serializer);
2450 re_stop(sc);
2451 lwkt_serialize_exit(ifp->if_serializer);
2454 static int
2455 re_sysctl_tx_moderation(SYSCTL_HANDLER_ARGS)
2457 struct re_softc *sc = arg1;
2458 struct ifnet *ifp = &sc->arpcom.ac_if;
2459 int error = 0, mod, mod_old;
2461 lwkt_serialize_enter(ifp->if_serializer);
2463 mod_old = mod = RE_TX_MODERATION_IS_ENABLED(sc);
2465 error = sysctl_handle_int(oidp, &mod, 0, req);
2466 if (error || req->newptr == NULL || mod == mod_old)
2467 goto back;
2468 if (mod != 0 && mod != 1) {
2469 error = EINVAL;
2470 goto back;
2473 if (mod)
2474 RE_ENABLE_TX_MODERATION(sc);
2475 else
2476 RE_DISABLE_TX_MODERATION(sc);
2478 if ((ifp->if_flags & (IFF_RUNNING | IFF_UP)) == (IFF_RUNNING | IFF_UP))
2479 re_init(sc);
2480 back:
2481 lwkt_serialize_exit(ifp->if_serializer);
2482 return error;