rtl8169.c revision 1.8 1 /* $NetBSD: rtl8169.c,v 1.8 2005/02/13 16:04:18 jdolecek Exp $ */
2
3 /*
4 * Copyright (c) 1997, 1998-2003
5 * Bill Paul <wpaul (at) windriver.com>. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Bill Paul.
18 * 4. Neither the name of the author nor the names of any co-contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32 * THE POSSIBILITY OF SUCH DAMAGE.
33 */
34
35 #include <sys/cdefs.h>
36 /* $FreeBSD: /repoman/r/ncvs/src/sys/dev/re/if_re.c,v 1.20 2004/04/11 20:34:08 ru Exp $ */
37
38 /*
39 * RealTek 8139C+/8169/8169S/8110S PCI NIC driver
40 *
41 * Written by Bill Paul <wpaul (at) windriver.com>
42 * Senior Networking Software Engineer
43 * Wind River Systems
44 */
45
46 /*
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 * four devices in this family: the RTL8139C+, the RTL8169, the RTL8169S
50 * and the RTL8110S.
51 *
52 * The 8139C+ is a 10/100 ethernet chip. It is backwards compatible
53 * with the older 8139 family, however it also supports a special
54 * C+ mode of operation that provides several new performance enhancing
55 * features. These include:
56 *
57 * o Descriptor based DMA mechanism. Each descriptor represents
58 * a single packet fragment. Data buffers may be aligned on
59 * any byte boundary.
60 *
61 * o 64-bit DMA
62 *
63 * o TCP/IP checksum offload for both RX and TX
64 *
65 * o High and normal priority transmit DMA rings
66 *
67 * o VLAN tag insertion and extraction
68 *
69 * o TCP large send (segmentation offload)
70 *
71 * Like the 8139, the 8139C+ also has a built-in 10/100 PHY. The C+
72 * programming API is fairly straightforward. The RX filtering, EEPROM
73 * access and PHY access is the same as it is on the older 8139 series
74 * chips.
75 *
76 * The 8169 is a 64-bit 10/100/1000 gigabit ethernet MAC. It has almost the
77 * same programming API and feature set as the 8139C+ with the following
78 * differences and additions:
79 *
80 * o 1000Mbps mode
81 *
82 * o Jumbo frames
83 *
84 * o GMII and TBI ports/registers for interfacing with copper
85 * or fiber PHYs
86 *
87 * o RX and TX DMA rings can have up to 1024 descriptors
88 * (the 8139C+ allows a maximum of 64)
89 *
90 * o Slight differences in register layout from the 8139C+
91 *
92 * The TX start and timer interrupt registers are at different locations
93 * on the 8169 than they are on the 8139C+. Also, the status word in the
94 * RX descriptor has a slightly different bit layout. The 8169 does not
95 * have a built-in PHY. Most reference boards use a Marvell 88E1000 'Alaska'
96 * copper gigE PHY.
97 *
98 * The 8169S/8110S 10/100/1000 devices have built-in copper gigE PHYs
99 * (the 'S' stands for 'single-chip'). These devices have the same
100 * programming API as the older 8169, but also have some vendor-specific
101 * registers for the on-board PHY. The 8110S is a LAN-on-motherboard
102 * part designed to be pin-compatible with the RealTek 8100 10/100 chip.
103 *
104 * This driver takes advantage of the RX and TX checksum offload and
105 * VLAN tag insertion/extraction features. It also implements TX
106 * interrupt moderation using the timer interrupt registers, which
107 * significantly reduces TX interrupt load. There is also support
108 * for jumbo frames, however the 8169/8169S/8110S can not transmit
109 * jumbo frames larger than 7.5K, so the max MTU possible with this
110 * driver is 7500 bytes.
111 */
112
113 #include "bpfilter.h"
114 #include "vlan.h"
115
116 #include <sys/param.h>
117 #include <sys/endian.h>
118 #include <sys/systm.h>
119 #include <sys/sockio.h>
120 #include <sys/mbuf.h>
121 #include <sys/malloc.h>
122 #include <sys/kernel.h>
123 #include <sys/socket.h>
124 #include <sys/device.h>
125
126 #include <net/if.h>
127 #include <net/if_arp.h>
128 #include <net/if_dl.h>
129 #include <net/if_ether.h>
130 #include <net/if_media.h>
131 #include <net/if_vlanvar.h>
132
133 #if NBPFILTER > 0
134 #include <net/bpf.h>
135 #endif
136
137 #include <machine/bus.h>
138
139 #include <dev/mii/mii.h>
140 #include <dev/mii/miivar.h>
141
142 #include <dev/pci/pcireg.h>
143 #include <dev/pci/pcivar.h>
144 #include <dev/pci/pcidevs.h>
145
146 /*
147 * Default to using PIO access for this driver.
148 */
149 #define RE_USEIOSPACE
150
151 #include <dev/ic/rtl81x9reg.h>
152 #include <dev/ic/rtl81x9var.h>
153
154 #include <dev/ic/rtl8169var.h>
155
156
157 static int re_encap(struct rtk_softc *, struct mbuf *, int *);
158
159 static int re_newbuf(struct rtk_softc *, int, struct mbuf *);
160 static int re_rx_list_init(struct rtk_softc *);
161 static int re_tx_list_init(struct rtk_softc *);
162 static void re_rxeof(struct rtk_softc *);
163 static void re_txeof(struct rtk_softc *);
164 static void re_tick(void *);
165 static void re_start(struct ifnet *);
166 static int re_ioctl(struct ifnet *, u_long, caddr_t);
167 static int re_init(struct ifnet *);
168 static void re_stop(struct ifnet *, int);
169 static void re_watchdog(struct ifnet *);
170
171 static void re_shutdown(void *);
172 static int re_enable(struct rtk_softc *);
173 static void re_disable(struct rtk_softc *);
174 static void re_power(int, void *);
175
176 static int re_ifmedia_upd(struct ifnet *);
177 static void re_ifmedia_sts(struct ifnet *, struct ifmediareq *);
178
179 static int re_gmii_readreg(struct device *, int, int);
180 static void re_gmii_writereg(struct device *, int, int, int);
181
182 static int re_miibus_readreg(struct device *, int, int);
183 static void re_miibus_writereg(struct device *, int, int, int);
184 static void re_miibus_statchg(struct device *);
185
186 static void re_reset(struct rtk_softc *);
187
188
189 #ifdef RE_USEIOSPACE
190 #define RTK_RES SYS_RES_IOPORT
191 #define RTK_RID RTK_PCI_LOIO
192 #else
193 #define RTK_RES SYS_RES_MEMORY
194 #define RTK_RID RTK_PCI_LOMEM
195 #endif
196
197 #define EE_SET(x) \
198 CSR_WRITE_1(sc, RTK_EECMD, \
199 CSR_READ_1(sc, RTK_EECMD) | x)
200
201 #define EE_CLR(x) \
202 CSR_WRITE_1(sc, RTK_EECMD, \
203 CSR_READ_1(sc, RTK_EECMD) & ~x)
204
205 static int
206 re_gmii_readreg(struct device *self, int phy, int reg)
207 {
208 struct rtk_softc *sc = (void *)self;
209 u_int32_t rval;
210 int i;
211
212 if (phy != 7)
213 return 0;
214
215 /* Let the rgephy driver read the GMEDIASTAT register */
216
217 if (reg == RTK_GMEDIASTAT) {
218 rval = CSR_READ_1(sc, RTK_GMEDIASTAT);
219 return rval;
220 }
221
222 CSR_WRITE_4(sc, RTK_PHYAR, reg << 16);
223 DELAY(1000);
224
225 for (i = 0; i < RTK_TIMEOUT; i++) {
226 rval = CSR_READ_4(sc, RTK_PHYAR);
227 if (rval & RTK_PHYAR_BUSY)
228 break;
229 DELAY(100);
230 }
231
232 if (i == RTK_TIMEOUT) {
233 aprint_error("%s: PHY read failed\n", sc->sc_dev.dv_xname);
234 return 0;
235 }
236
237 return rval & RTK_PHYAR_PHYDATA;
238 }
239
240 static void
241 re_gmii_writereg(struct device *dev, int phy, int reg, int data)
242 {
243 struct rtk_softc *sc = (void *)dev;
244 u_int32_t rval;
245 int i;
246
247 CSR_WRITE_4(sc, RTK_PHYAR, (reg << 16) |
248 (data & RTK_PHYAR_PHYDATA) | RTK_PHYAR_BUSY);
249 DELAY(1000);
250
251 for (i = 0; i < RTK_TIMEOUT; i++) {
252 rval = CSR_READ_4(sc, RTK_PHYAR);
253 if (!(rval & RTK_PHYAR_BUSY))
254 break;
255 DELAY(100);
256 }
257
258 if (i == RTK_TIMEOUT) {
259 aprint_error("%s: PHY write reg %x <- %x failed\n",
260 sc->sc_dev.dv_xname, reg, data);
261 return;
262 }
263
264 return;
265 }
266
267 static int
268 re_miibus_readreg(struct device *dev, int phy, int reg)
269 {
270 struct rtk_softc *sc = (void *)dev;
271 u_int16_t rval = 0;
272 u_int16_t re8139_reg = 0;
273 int s;
274
275 s = splnet();
276
277 if (sc->rtk_type == RTK_8169) {
278 rval = re_gmii_readreg(dev, phy, reg);
279 splx(s);
280 return rval;
281 }
282
283 /* Pretend the internal PHY is only at address 0 */
284 if (phy) {
285 splx(s);
286 return 0;
287 }
288 switch (reg) {
289 case MII_BMCR:
290 re8139_reg = RTK_BMCR;
291 break;
292 case MII_BMSR:
293 re8139_reg = RTK_BMSR;
294 break;
295 case MII_ANAR:
296 re8139_reg = RTK_ANAR;
297 break;
298 case MII_ANER:
299 re8139_reg = RTK_ANER;
300 break;
301 case MII_ANLPAR:
302 re8139_reg = RTK_LPAR;
303 break;
304 case MII_PHYIDR1:
305 case MII_PHYIDR2:
306 splx(s);
307 return 0;
308 /*
309 * Allow the rlphy driver to read the media status
310 * register. If we have a link partner which does not
311 * support NWAY, this is the register which will tell
312 * us the results of parallel detection.
313 */
314 case RTK_MEDIASTAT:
315 rval = CSR_READ_1(sc, RTK_MEDIASTAT);
316 splx(s);
317 return rval;
318 default:
319 aprint_error("%s: bad phy register\n", sc->sc_dev.dv_xname);
320 splx(s);
321 return 0;
322 }
323 rval = CSR_READ_2(sc, re8139_reg);
324 splx(s);
325 return rval;
326 }
327
328 static void
329 re_miibus_writereg(struct device *dev, int phy, int reg, int data)
330 {
331 struct rtk_softc *sc = (void *)dev;
332 u_int16_t re8139_reg = 0;
333 int s;
334
335 s = splnet();
336
337 if (sc->rtk_type == RTK_8169) {
338 re_gmii_writereg(dev, phy, reg, data);
339 splx(s);
340 return;
341 }
342
343 /* Pretend the internal PHY is only at address 0 */
344 if (phy) {
345 splx(s);
346 return;
347 }
348 switch (reg) {
349 case MII_BMCR:
350 re8139_reg = RTK_BMCR;
351 break;
352 case MII_BMSR:
353 re8139_reg = RTK_BMSR;
354 break;
355 case MII_ANAR:
356 re8139_reg = RTK_ANAR;
357 break;
358 case MII_ANER:
359 re8139_reg = RTK_ANER;
360 break;
361 case MII_ANLPAR:
362 re8139_reg = RTK_LPAR;
363 break;
364 case MII_PHYIDR1:
365 case MII_PHYIDR2:
366 splx(s);
367 return;
368 break;
369 default:
370 aprint_error("%s: bad phy register\n", sc->sc_dev.dv_xname);
371 splx(s);
372 return;
373 }
374 CSR_WRITE_2(sc, re8139_reg, data);
375 splx(s);
376 return;
377 }
378
379 static void
380 re_miibus_statchg(struct device *dev)
381 {
382
383 return;
384 }
385
386 static void
387 re_reset(struct rtk_softc *sc)
388 {
389 register int i;
390
391 CSR_WRITE_1(sc, RTK_COMMAND, RTK_CMD_RESET);
392
393 for (i = 0; i < RTK_TIMEOUT; i++) {
394 DELAY(10);
395 if (!(CSR_READ_1(sc, RTK_COMMAND) & RTK_CMD_RESET))
396 break;
397 }
398 if (i == RTK_TIMEOUT)
399 aprint_error("%s: reset never completed!\n",
400 sc->sc_dev.dv_xname);
401
402 /*
403 * NB: Realtek-supplied Linux driver does this only for
404 * MCFG_METHOD_2, which corresponds to sc->sc_rev == 2.
405 */
406 if (1) /* XXX check softc flag for 8169s version */
407 CSR_WRITE_1(sc, 0x82, 1);
408
409 return;
410 }
411
412 /*
413 * The following routine is designed to test for a defect on some
414 * 32-bit 8169 cards. Some of these NICs have the REQ64# and ACK64#
415 * lines connected to the bus, however for a 32-bit only card, they
416 * should be pulled high. The result of this defect is that the
417 * NIC will not work right if you plug it into a 64-bit slot: DMA
418 * operations will be done with 64-bit transfers, which will fail
419 * because the 64-bit data lines aren't connected.
420 *
421 * There's no way to work around this (short of talking a soldering
422 * iron to the board), however we can detect it. The method we use
423 * here is to put the NIC into digital loopback mode, set the receiver
424 * to promiscuous mode, and then try to send a frame. We then compare
425 * the frame data we sent to what was received. If the data matches,
426 * then the NIC is working correctly, otherwise we know the user has
427 * a defective NIC which has been mistakenly plugged into a 64-bit PCI
428 * slot. In the latter case, there's no way the NIC can work correctly,
429 * so we print out a message on the console and abort the device attach.
430 */
431
432 int
433 re_diag(struct rtk_softc *sc)
434 {
435 struct ifnet *ifp = &sc->ethercom.ec_if;
436 struct mbuf *m0;
437 struct ether_header *eh;
438 struct rtk_desc *cur_rx;
439 bus_dmamap_t dmamap;
440 u_int16_t status;
441 u_int32_t rxstat;
442 int total_len, i, s, error = 0;
443 u_int8_t dst[] = { 0x00, 'h', 'e', 'l', 'l', 'o' };
444 u_int8_t src[] = { 0x00, 'w', 'o', 'r', 'l', 'd' };
445
446 /* Allocate a single mbuf */
447
448 MGETHDR(m0, M_DONTWAIT, MT_DATA);
449 if (m0 == NULL)
450 return ENOBUFS;
451
452 /*
453 * Initialize the NIC in test mode. This sets the chip up
454 * so that it can send and receive frames, but performs the
455 * following special functions:
456 * - Puts receiver in promiscuous mode
457 * - Enables digital loopback mode
458 * - Leaves interrupts turned off
459 */
460
461 ifp->if_flags |= IFF_PROMISC;
462 sc->rtk_testmode = 1;
463 re_init(ifp);
464 re_stop(ifp, 0);
465 DELAY(100000);
466 re_init(ifp);
467
468 /* Put some data in the mbuf */
469
470 eh = mtod(m0, struct ether_header *);
471 bcopy((char *)&dst, eh->ether_dhost, ETHER_ADDR_LEN);
472 bcopy((char *)&src, eh->ether_shost, ETHER_ADDR_LEN);
473 eh->ether_type = htons(ETHERTYPE_IP);
474 m0->m_pkthdr.len = m0->m_len = ETHER_MIN_LEN - ETHER_CRC_LEN;
475
476 /*
477 * Queue the packet, start transmission.
478 */
479
480 CSR_WRITE_2(sc, RTK_ISR, 0xFFFF);
481 s = splnet();
482 IF_ENQUEUE(&ifp->if_snd, m0);
483 re_start(ifp);
484 splx(s);
485 m0 = NULL;
486
487 /* Wait for it to propagate through the chip */
488
489 DELAY(100000);
490 for (i = 0; i < RTK_TIMEOUT; i++) {
491 status = CSR_READ_2(sc, RTK_ISR);
492 if ((status & (RTK_ISR_TIMEOUT_EXPIRED | RTK_ISR_RX_OK)) ==
493 (RTK_ISR_TIMEOUT_EXPIRED | RTK_ISR_RX_OK))
494 break;
495 DELAY(10);
496 }
497 if (i == RTK_TIMEOUT) {
498 aprint_error("%s: diagnostic failed, failed to receive packet "
499 "in loopback mode\n", sc->sc_dev.dv_xname);
500 error = EIO;
501 goto done;
502 }
503
504 /*
505 * The packet should have been dumped into the first
506 * entry in the RX DMA ring. Grab it from there.
507 */
508
509 dmamap = sc->rtk_ldata.rtk_rx_list_map;
510 bus_dmamap_sync(sc->sc_dmat,
511 dmamap, 0, dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
512 dmamap = sc->rtk_ldata.rtk_rx_dmamap[0];
513 bus_dmamap_sync(sc->sc_dmat, dmamap, 0, dmamap->dm_mapsize,
514 BUS_DMASYNC_POSTWRITE);
515 bus_dmamap_unload(sc->sc_dmat,
516 sc->rtk_ldata.rtk_rx_dmamap[0]);
517
518 m0 = sc->rtk_ldata.rtk_rx_mbuf[0];
519 sc->rtk_ldata.rtk_rx_mbuf[0] = NULL;
520 eh = mtod(m0, struct ether_header *);
521
522 cur_rx = &sc->rtk_ldata.rtk_rx_list[0];
523 total_len = RTK_RXBYTES(cur_rx);
524 rxstat = le32toh(cur_rx->rtk_cmdstat);
525
526 if (total_len != ETHER_MIN_LEN) {
527 aprint_error("%s: diagnostic failed, received short packet\n",
528 sc->sc_dev.dv_xname);
529 error = EIO;
530 goto done;
531 }
532
533 /* Test that the received packet data matches what we sent. */
534
535 if (bcmp((char *)&eh->ether_dhost, (char *)&dst, ETHER_ADDR_LEN) ||
536 bcmp((char *)&eh->ether_shost, (char *)&src, ETHER_ADDR_LEN) ||
537 ntohs(eh->ether_type) != ETHERTYPE_IP) {
538 aprint_error("%s: WARNING, DMA FAILURE!\n",
539 sc->sc_dev.dv_xname);
540 aprint_error("%s: expected TX data: %s",
541 sc->sc_dev.dv_xname, ether_sprintf(dst));
542 aprint_error("/%s/0x%x\n", ether_sprintf(src), ETHERTYPE_IP);
543 aprint_error("%s: received RX data: %s",
544 sc->sc_dev.dv_xname,
545 ether_sprintf(eh->ether_dhost));
546 aprint_error("/%s/0x%x\n", ether_sprintf(eh->ether_shost),
547 ntohs(eh->ether_type));
548 aprint_error("%s: You may have a defective 32-bit NIC plugged "
549 "into a 64-bit PCI slot.\n", sc->sc_dev.dv_xname);
550 aprint_error("%s: Please re-install the NIC in a 32-bit slot "
551 "for proper operation.\n", sc->sc_dev.dv_xname);
552 aprint_error("%s: Read the re(4) man page for more details.\n",
553 sc->sc_dev.dv_xname);
554 error = EIO;
555 }
556
557 done:
558 /* Turn interface off, release resources */
559
560 sc->rtk_testmode = 0;
561 ifp->if_flags &= ~IFF_PROMISC;
562 re_stop(ifp, 0);
563 if (m0 != NULL)
564 m_freem(m0);
565
566 return error;
567 }
568
569
570 /*
571 * Attach the interface. Allocate softc structures, do ifmedia
572 * setup and ethernet/BPF attach.
573 */
574 void
575 re_attach(struct rtk_softc *sc)
576 {
577 u_char eaddr[ETHER_ADDR_LEN];
578 u_int16_t val;
579 struct ifnet *ifp;
580 int error = 0, i, addr_len;
581
582
583 /* XXX JRS: bus-attach-independent code begins approximately here */
584
585 /* Reset the adapter. */
586 re_reset(sc);
587
588 if (sc->rtk_type == RTK_8169) {
589 uint32_t hwrev;
590
591 /* Revision of 8169/8169S/8110s in bits 30..26, 23 */
592 hwrev = CSR_READ_4(sc, RTK_TXCFG) & 0x7c800000;
593 if (hwrev == (0x1 << 28)) {
594 sc->sc_rev = 4;
595 } else if (hwrev == (0x1 << 26)) {
596 sc->sc_rev = 3;
597 } else if (hwrev == (0x1 << 23)) {
598 sc->sc_rev = 2;
599 } else
600 sc->sc_rev = 1;
601
602 /* Set RX length mask */
603
604 sc->rtk_rxlenmask = RTK_RDESC_STAT_GFRAGLEN;
605
606 /* Force station address autoload from the EEPROM */
607
608 CSR_WRITE_1(sc, RTK_EECMD, RTK_EEMODE_AUTOLOAD);
609 for (i = 0; i < RTK_TIMEOUT; i++) {
610 if (!(CSR_READ_1(sc, RTK_EECMD) & RTK_EEMODE_AUTOLOAD))
611 break;
612 DELAY(100);
613 }
614 if (i == RTK_TIMEOUT)
615 aprint_error("%s: eeprom autoload timed out\n",
616 sc->sc_dev.dv_xname);
617
618 for (i = 0; i < ETHER_ADDR_LEN; i++)
619 eaddr[i] = CSR_READ_1(sc, RTK_IDR0 + i);
620 } else {
621
622 /* Set RX length mask */
623
624 sc->rtk_rxlenmask = RTK_RDESC_STAT_FRAGLEN;
625
626 if (rtk_read_eeprom(sc, RTK_EE_ID, RTK_EEADDR_LEN1) == 0x8129)
627 addr_len = RTK_EEADDR_LEN1;
628 else
629 addr_len = RTK_EEADDR_LEN0;
630
631 /*
632 * Get station address from the EEPROM.
633 */
634 for (i = 0; i < 3; i++) {
635 val = rtk_read_eeprom(sc, RTK_EE_EADDR0 + i, addr_len);
636 eaddr[(i * 2) + 0] = val & 0xff;
637 eaddr[(i * 2) + 1] = val >> 8;
638 }
639 }
640
641 aprint_normal("%s: Ethernet address %s\n",
642 sc->sc_dev.dv_xname, ether_sprintf(eaddr));
643
644
645 /* Allocate DMA'able memory for the TX ring */
646 if ((error = bus_dmamem_alloc(sc->sc_dmat, RTK_TX_LIST_SZ,
647 RTK_ETHER_ALIGN, 0, &sc->rtk_ldata.rtk_tx_listseg,
648 1, &sc->rtk_ldata.rtk_tx_listnseg, BUS_DMA_NOWAIT)) != 0) {
649 aprint_error("%s: can't allocate tx listseg, error = %d\n",
650 sc->sc_dev.dv_xname, error);
651 goto fail_0;
652 }
653
654 /* Load the map for the TX ring. */
655 if ((error = bus_dmamem_map(sc->sc_dmat, &sc->rtk_ldata.rtk_tx_listseg,
656 sc->rtk_ldata.rtk_tx_listnseg, RTK_TX_LIST_SZ,
657 (caddr_t *)&sc->rtk_ldata.rtk_tx_list,
658 BUS_DMA_NOWAIT)) != 0) {
659 aprint_error("%s: can't map tx list, error = %d\n",
660 sc->sc_dev.dv_xname, error);
661 goto fail_1;
662 }
663 memset(sc->rtk_ldata.rtk_tx_list, 0, RTK_TX_LIST_SZ);
664
665 if ((error = bus_dmamap_create(sc->sc_dmat, RTK_TX_LIST_SZ, 1,
666 RTK_TX_LIST_SZ, 0, BUS_DMA_ALLOCNOW,
667 &sc->rtk_ldata.rtk_tx_list_map)) != 0) {
668 aprint_error("%s: can't create tx list map, error = %d\n",
669 sc->sc_dev.dv_xname, error);
670 goto fail_2;
671 }
672
673
674 if ((error = bus_dmamap_load(sc->sc_dmat,
675 sc->rtk_ldata.rtk_tx_list_map, sc->rtk_ldata.rtk_tx_list,
676 RTK_TX_LIST_SZ, NULL, BUS_DMA_NOWAIT)) != 0) {
677 aprint_error("%s: can't load tx list, error = %d\n",
678 sc->sc_dev.dv_xname, error);
679 goto fail_3;
680 }
681
682 /* Create DMA maps for TX buffers */
683 for (i = 0; i < RTK_TX_DESC_CNT; i++) {
684 error = bus_dmamap_create(sc->sc_dmat, MCLBYTES * RTK_NTXSEGS,
685 RTK_NTXSEGS, MCLBYTES, 0, BUS_DMA_ALLOCNOW,
686 &sc->rtk_ldata.rtk_tx_dmamap[i]);
687 if (error) {
688 aprint_error("%s: can't create DMA map for TX\n",
689 sc->sc_dev.dv_xname);
690 goto fail_4;
691 }
692 }
693
694 /* Allocate DMA'able memory for the RX ring */
695 if ((error = bus_dmamem_alloc(sc->sc_dmat, RTK_RX_LIST_SZ,
696 RTK_RING_ALIGN, 0, &sc->rtk_ldata.rtk_rx_listseg, 1,
697 &sc->rtk_ldata.rtk_rx_listnseg, BUS_DMA_NOWAIT)) != 0) {
698 aprint_error("%s: can't allocate rx listseg, error = %d\n",
699 sc->sc_dev.dv_xname, error);
700 goto fail_4;
701 }
702
703 /* Load the map for the RX ring. */
704 if ((error = bus_dmamem_map(sc->sc_dmat, &sc->rtk_ldata.rtk_rx_listseg,
705 sc->rtk_ldata.rtk_rx_listnseg, RTK_RX_LIST_SZ,
706 (caddr_t *)&sc->rtk_ldata.rtk_rx_list,
707 BUS_DMA_NOWAIT)) != 0) {
708 aprint_error("%s: can't map rx list, error = %d\n",
709 sc->sc_dev.dv_xname, error);
710 goto fail_5;
711 }
712 memset(sc->rtk_ldata.rtk_rx_list, 0, RTK_TX_LIST_SZ);
713
714 if ((error = bus_dmamap_create(sc->sc_dmat, RTK_RX_LIST_SZ, 1,
715 RTK_RX_LIST_SZ, 0, BUS_DMA_ALLOCNOW,
716 &sc->rtk_ldata.rtk_rx_list_map)) != 0) {
717 aprint_error("%s: can't create rx list map, error = %d\n",
718 sc->sc_dev.dv_xname, error);
719 goto fail_6;
720 }
721
722 if ((error = bus_dmamap_load(sc->sc_dmat,
723 sc->rtk_ldata.rtk_rx_list_map, sc->rtk_ldata.rtk_rx_list,
724 RTK_RX_LIST_SZ, NULL, BUS_DMA_NOWAIT)) != 0) {
725 aprint_error("%s: can't load rx list, error = %d\n",
726 sc->sc_dev.dv_xname, error);
727 goto fail_7;
728 }
729
730 /* Create DMA maps for RX buffers */
731 for (i = 0; i < RTK_RX_DESC_CNT; i++) {
732 error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1, MCLBYTES,
733 0, BUS_DMA_ALLOCNOW, &sc->rtk_ldata.rtk_rx_dmamap[i]);
734 if (error) {
735 aprint_error("%s: can't create DMA map for RX\n",
736 sc->sc_dev.dv_xname);
737 goto fail_8;
738 }
739 }
740
741 /*
742 * Record interface as attached. From here, we should not fail.
743 */
744 sc->sc_flags |= RTK_ATTACHED;
745
746 ifp = &sc->ethercom.ec_if;
747 ifp->if_softc = sc;
748 strcpy(ifp->if_xname, sc->sc_dev.dv_xname);
749 ifp->if_mtu = ETHERMTU;
750 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
751 ifp->if_ioctl = re_ioctl;
752 sc->ethercom.ec_capabilities |=
753 ETHERCAP_VLAN_MTU | ETHERCAP_VLAN_HWTAGGING;
754 ifp->if_start = re_start;
755 ifp->if_stop = re_stop;
756 ifp->if_capabilities |=
757 IFCAP_CSUM_IPv4 | IFCAP_CSUM_TCPv4 | IFCAP_CSUM_UDPv4;
758 ifp->if_watchdog = re_watchdog;
759 ifp->if_init = re_init;
760 if (sc->rtk_type == RTK_8169)
761 ifp->if_baudrate = 1000000000;
762 else
763 ifp->if_baudrate = 100000000;
764 ifp->if_snd.ifq_maxlen = RTK_IFQ_MAXLEN;
765 ifp->if_capenable = ifp->if_capabilities;
766 IFQ_SET_READY(&ifp->if_snd);
767
768 callout_init(&sc->rtk_tick_ch);
769
770 /* Do MII setup */
771 sc->mii.mii_ifp = ifp;
772 sc->mii.mii_readreg = re_miibus_readreg;
773 sc->mii.mii_writereg = re_miibus_writereg;
774 sc->mii.mii_statchg = re_miibus_statchg;
775 ifmedia_init(&sc->mii.mii_media, IFM_IMASK, re_ifmedia_upd,
776 re_ifmedia_sts);
777 mii_attach(&sc->sc_dev, &sc->mii, 0xffffffff, MII_PHY_ANY,
778 MII_OFFSET_ANY, 0);
779 ifmedia_set(&sc->mii.mii_media, IFM_ETHER | IFM_AUTO);
780
781 /*
782 * Call MI attach routine.
783 */
784 if_attach(ifp);
785 ether_ifattach(ifp, eaddr);
786
787
788 /*
789 * Make sure the interface is shutdown during reboot.
790 */
791 sc->sc_sdhook = shutdownhook_establish(re_shutdown, sc);
792 if (sc->sc_sdhook == NULL)
793 aprint_error("%s: WARNING: unable to establish shutdown hook\n",
794 sc->sc_dev.dv_xname);
795 /*
796 * Add a suspend hook to make sure we come back up after a
797 * resume.
798 */
799 sc->sc_powerhook = powerhook_establish(re_power, sc);
800 if (sc->sc_powerhook == NULL)
801 aprint_error("%s: WARNING: unable to establish power hook\n",
802 sc->sc_dev.dv_xname);
803
804
805 return;
806
807 fail_8:
808 /* Destroy DMA maps for RX buffers. */
809 for (i = 0; i < RTK_RX_DESC_CNT; i++)
810 if (sc->rtk_ldata.rtk_rx_dmamap[i] != NULL)
811 bus_dmamap_destroy(sc->sc_dmat,
812 sc->rtk_ldata.rtk_rx_dmamap[i]);
813
814 /* Free DMA'able memory for the RX ring. */
815 bus_dmamap_unload(sc->sc_dmat, sc->rtk_ldata.rtk_rx_list_map);
816 fail_7:
817 bus_dmamap_destroy(sc->sc_dmat, sc->rtk_ldata.rtk_rx_list_map);
818 fail_6:
819 bus_dmamem_unmap(sc->sc_dmat,
820 (caddr_t)sc->rtk_ldata.rtk_rx_list, RTK_RX_LIST_SZ);
821 fail_5:
822 bus_dmamem_free(sc->sc_dmat,
823 &sc->rtk_ldata.rtk_rx_listseg, sc->rtk_ldata.rtk_rx_listnseg);
824
825 fail_4:
826 /* Destroy DMA maps for TX buffers. */
827 for (i = 0; i < RTK_TX_DESC_CNT; i++)
828 if (sc->rtk_ldata.rtk_tx_dmamap[i] != NULL)
829 bus_dmamap_destroy(sc->sc_dmat,
830 sc->rtk_ldata.rtk_tx_dmamap[i]);
831
832 /* Free DMA'able memory for the TX ring. */
833 bus_dmamap_unload(sc->sc_dmat, sc->rtk_ldata.rtk_tx_list_map);
834 fail_3:
835 bus_dmamap_destroy(sc->sc_dmat, sc->rtk_ldata.rtk_tx_list_map);
836 fail_2:
837 bus_dmamem_unmap(sc->sc_dmat,
838 (caddr_t)sc->rtk_ldata.rtk_tx_list, RTK_TX_LIST_SZ);
839 fail_1:
840 bus_dmamem_free(sc->sc_dmat,
841 &sc->rtk_ldata.rtk_tx_listseg, sc->rtk_ldata.rtk_tx_listnseg);
842 fail_0:
843 return;
844 }
845
846
847 /*
848 * re_activate:
849 * Handle device activation/deactivation requests.
850 */
851 int
852 re_activate(struct device *self, enum devact act)
853 {
854 struct rtk_softc *sc = (void *) self;
855 int s, error = 0;
856
857 s = splnet();
858 switch (act) {
859 case DVACT_ACTIVATE:
860 error = EOPNOTSUPP;
861 break;
862 case DVACT_DEACTIVATE:
863 mii_activate(&sc->mii, act, MII_PHY_ANY, MII_OFFSET_ANY);
864 if_deactivate(&sc->ethercom.ec_if);
865 break;
866 }
867 splx(s);
868
869 return error;
870 }
871
872 /*
873 * re_detach:
874 * Detach a rtk interface.
875 */
876 int
877 re_detach(struct rtk_softc *sc)
878 {
879 struct ifnet *ifp = &sc->ethercom.ec_if;
880 int i;
881
882 /*
883 * Succeed now if there isn't any work to do.
884 */
885 if ((sc->sc_flags & RTK_ATTACHED) == 0)
886 return 0;
887
888 /* Unhook our tick handler. */
889 callout_stop(&sc->rtk_tick_ch);
890
891 /* Detach all PHYs. */
892 mii_detach(&sc->mii, MII_PHY_ANY, MII_OFFSET_ANY);
893
894 /* Delete all remaining media. */
895 ifmedia_delete_instance(&sc->mii.mii_media, IFM_INST_ANY);
896
897 ether_ifdetach(ifp);
898 if_detach(ifp);
899
900 /* XXX undo re_allocmem() */
901
902 /* Destroy DMA maps for RX buffers. */
903 for (i = 0; i < RTK_RX_DESC_CNT; i++)
904 if (sc->rtk_ldata.rtk_rx_dmamap[i] != NULL)
905 bus_dmamap_destroy(sc->sc_dmat,
906 sc->rtk_ldata.rtk_rx_dmamap[i]);
907
908 /* Free DMA'able memory for the RX ring. */
909 bus_dmamap_unload(sc->sc_dmat, sc->rtk_ldata.rtk_rx_list_map);
910 bus_dmamap_destroy(sc->sc_dmat, sc->rtk_ldata.rtk_rx_list_map);
911 bus_dmamem_unmap(sc->sc_dmat,
912 (caddr_t)sc->rtk_ldata.rtk_rx_list, RTK_RX_LIST_SZ);
913 bus_dmamem_free(sc->sc_dmat,
914 &sc->rtk_ldata.rtk_rx_listseg, sc->rtk_ldata.rtk_rx_listnseg);
915
916 /* Destroy DMA maps for TX buffers. */
917 for (i = 0; i < RTK_TX_DESC_CNT; i++)
918 if (sc->rtk_ldata.rtk_tx_dmamap[i] != NULL)
919 bus_dmamap_destroy(sc->sc_dmat,
920 sc->rtk_ldata.rtk_tx_dmamap[i]);
921
922 /* Free DMA'able memory for the TX ring. */
923 bus_dmamap_unload(sc->sc_dmat, sc->rtk_ldata.rtk_tx_list_map);
924 bus_dmamap_destroy(sc->sc_dmat, sc->rtk_ldata.rtk_tx_list_map);
925 bus_dmamem_unmap(sc->sc_dmat,
926 (caddr_t)sc->rtk_ldata.rtk_tx_list, RTK_TX_LIST_SZ);
927 bus_dmamem_free(sc->sc_dmat,
928 &sc->rtk_ldata.rtk_tx_listseg, sc->rtk_ldata.rtk_tx_listnseg);
929
930
931 shutdownhook_disestablish(sc->sc_sdhook);
932 powerhook_disestablish(sc->sc_powerhook);
933
934 return 0;
935 }
936
937 /*
938 * re_enable:
939 * Enable the RTL81X9 chip.
940 */
941 static int
942 re_enable(struct rtk_softc *sc)
943 {
944 if (RTK_IS_ENABLED(sc) == 0 && sc->sc_enable != NULL) {
945 if ((*sc->sc_enable)(sc) != 0) {
946 aprint_error("%s: device enable failed\n",
947 sc->sc_dev.dv_xname);
948 return EIO;
949 }
950 sc->sc_flags |= RTK_ENABLED;
951 }
952 return 0;
953 }
954
955 /*
956 * re_disable:
957 * Disable the RTL81X9 chip.
958 */
959 static void
960 re_disable(struct rtk_softc *sc)
961 {
962
963 if (RTK_IS_ENABLED(sc) && sc->sc_disable != NULL) {
964 (*sc->sc_disable)(sc);
965 sc->sc_flags &= ~RTK_ENABLED;
966 }
967 }
968
969 /*
970 * re_power:
971 * Power management (suspend/resume) hook.
972 */
973 void
974 re_power(int why, void *arg)
975 {
976 struct rtk_softc *sc = (void *) arg;
977 struct ifnet *ifp = &sc->ethercom.ec_if;
978 int s;
979
980 s = splnet();
981 switch (why) {
982 case PWR_SUSPEND:
983 case PWR_STANDBY:
984 re_stop(ifp, 0);
985 if (sc->sc_power != NULL)
986 (*sc->sc_power)(sc, why);
987 break;
988 case PWR_RESUME:
989 if (ifp->if_flags & IFF_UP) {
990 if (sc->sc_power != NULL)
991 (*sc->sc_power)(sc, why);
992 re_init(ifp);
993 }
994 break;
995 case PWR_SOFTSUSPEND:
996 case PWR_SOFTSTANDBY:
997 case PWR_SOFTRESUME:
998 break;
999 }
1000 splx(s);
1001 }
1002
1003
1004 static int
1005 re_newbuf(struct rtk_softc *sc, int idx, struct mbuf *m)
1006 {
1007 struct mbuf *n = NULL;
1008 bus_dmamap_t map;
1009 struct rtk_desc *d;
1010 u_int32_t cmdstat;
1011 int error;
1012
1013 if (m == NULL) {
1014 MGETHDR(n, M_DONTWAIT, MT_DATA);
1015 if (n == NULL)
1016 return ENOBUFS;
1017 m = n;
1018
1019 MCLGET(m, M_DONTWAIT);
1020 if (!(m->m_flags & M_EXT)) {
1021 m_freem(m);
1022 return ENOBUFS;
1023 }
1024 } else
1025 m->m_data = m->m_ext.ext_buf;
1026
1027 /*
1028 * Initialize mbuf length fields and fixup
1029 * alignment so that the frame payload is
1030 * longword aligned.
1031 */
1032 m->m_len = m->m_pkthdr.len = MCLBYTES;
1033 m_adj(m, RTK_ETHER_ALIGN);
1034
1035 map = sc->rtk_ldata.rtk_rx_dmamap[idx];
1036 error = bus_dmamap_load_mbuf(sc->sc_dmat, map, m, BUS_DMA_NOWAIT);
1037
1038 if (error)
1039 goto out;
1040
1041 d = &sc->rtk_ldata.rtk_rx_list[idx];
1042 if (le32toh(d->rtk_cmdstat) & RTK_RDESC_STAT_OWN)
1043 goto out;
1044
1045 cmdstat = map->dm_segs[0].ds_len;
1046 d->rtk_bufaddr_lo = htole32(RTK_ADDR_LO(map->dm_segs[0].ds_addr));
1047 d->rtk_bufaddr_hi = htole32(RTK_ADDR_HI(map->dm_segs[0].ds_addr));
1048 cmdstat |= RTK_TDESC_CMD_SOF;
1049 if (idx == (RTK_RX_DESC_CNT - 1))
1050 cmdstat |= RTK_TDESC_CMD_EOR;
1051 d->rtk_cmdstat = htole32(cmdstat);
1052
1053 d->rtk_cmdstat |= htole32(RTK_TDESC_CMD_EOF);
1054
1055
1056 sc->rtk_ldata.rtk_rx_list[idx].rtk_cmdstat |=
1057 htole32(RTK_RDESC_CMD_OWN);
1058 sc->rtk_ldata.rtk_rx_mbuf[idx] = m;
1059
1060 bus_dmamap_sync(sc->sc_dmat, sc->rtk_ldata.rtk_rx_dmamap[idx], 0,
1061 sc->rtk_ldata.rtk_rx_dmamap[idx]->dm_mapsize,
1062 BUS_DMASYNC_PREREAD);
1063
1064 return 0;
1065 out:
1066 if (n != NULL)
1067 m_freem(n);
1068 return ENOMEM;
1069 }
1070
1071 static int
1072 re_tx_list_init(struct rtk_softc *sc)
1073 {
1074 memset((char *)sc->rtk_ldata.rtk_tx_list, 0, RTK_TX_LIST_SZ);
1075 memset((char *)&sc->rtk_ldata.rtk_tx_mbuf, 0,
1076 (RTK_TX_DESC_CNT * sizeof(struct mbuf *)));
1077
1078 bus_dmamap_sync(sc->sc_dmat,
1079 sc->rtk_ldata.rtk_tx_list_map, 0,
1080 sc->rtk_ldata.rtk_tx_list_map->dm_mapsize, BUS_DMASYNC_PREWRITE);
1081 sc->rtk_ldata.rtk_tx_prodidx = 0;
1082 sc->rtk_ldata.rtk_tx_considx = 0;
1083 sc->rtk_ldata.rtk_tx_free = RTK_TX_DESC_CNT;
1084
1085 return 0;
1086 }
1087
1088 static int
1089 re_rx_list_init(struct rtk_softc *sc)
1090 {
1091 int i;
1092
1093 memset((char *)sc->rtk_ldata.rtk_rx_list, 0, RTK_RX_LIST_SZ);
1094 memset((char *)&sc->rtk_ldata.rtk_rx_mbuf, 0,
1095 (RTK_RX_DESC_CNT * sizeof(struct mbuf *)));
1096
1097 for (i = 0; i < RTK_RX_DESC_CNT; i++) {
1098 if (re_newbuf(sc, i, NULL) == ENOBUFS)
1099 return ENOBUFS;
1100 }
1101
1102 /* Flush the RX descriptors */
1103
1104 bus_dmamap_sync(sc->sc_dmat,
1105 sc->rtk_ldata.rtk_rx_list_map,
1106 0, sc->rtk_ldata.rtk_rx_list_map->dm_mapsize,
1107 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
1108
1109 sc->rtk_ldata.rtk_rx_prodidx = 0;
1110 sc->rtk_head = sc->rtk_tail = NULL;
1111
1112 return 0;
1113 }
1114
1115 /*
1116 * RX handler for C+ and 8169. For the gigE chips, we support
1117 * the reception of jumbo frames that have been fragmented
1118 * across multiple 2K mbuf cluster buffers.
1119 */
1120 static void
1121 re_rxeof(struct rtk_softc *sc)
1122 {
1123 struct mbuf *m;
1124 struct ifnet *ifp;
1125 int i, total_len;
1126 struct rtk_desc *cur_rx;
1127 struct m_tag *mtag;
1128 u_int32_t rxstat, rxvlan;
1129
1130 ifp = &sc->ethercom.ec_if;
1131 i = sc->rtk_ldata.rtk_rx_prodidx;
1132
1133 /* Invalidate the descriptor memory */
1134
1135 bus_dmamap_sync(sc->sc_dmat,
1136 sc->rtk_ldata.rtk_rx_list_map,
1137 0, sc->rtk_ldata.rtk_rx_list_map->dm_mapsize,
1138 BUS_DMASYNC_POSTREAD);
1139
1140 while (!RTK_OWN(&sc->rtk_ldata.rtk_rx_list[i])) {
1141
1142 cur_rx = &sc->rtk_ldata.rtk_rx_list[i];
1143 m = sc->rtk_ldata.rtk_rx_mbuf[i];
1144 total_len = RTK_RXBYTES(cur_rx);
1145 rxstat = le32toh(cur_rx->rtk_cmdstat);
1146 rxvlan = le32toh(cur_rx->rtk_vlanctl);
1147
1148 /* Invalidate the RX mbuf and unload its map */
1149
1150 bus_dmamap_sync(sc->sc_dmat,
1151 sc->rtk_ldata.rtk_rx_dmamap[i],
1152 0, sc->rtk_ldata.rtk_rx_dmamap[i]->dm_mapsize,
1153 BUS_DMASYNC_POSTWRITE);
1154 bus_dmamap_unload(sc->sc_dmat,
1155 sc->rtk_ldata.rtk_rx_dmamap[i]);
1156
1157 if (!(rxstat & RTK_RDESC_STAT_EOF)) {
1158 m->m_len = MCLBYTES - RTK_ETHER_ALIGN;
1159 if (sc->rtk_head == NULL)
1160 sc->rtk_head = sc->rtk_tail = m;
1161 else {
1162 m->m_flags &= ~M_PKTHDR;
1163 sc->rtk_tail->m_next = m;
1164 sc->rtk_tail = m;
1165 }
1166 re_newbuf(sc, i, NULL);
1167 RTK_DESC_INC(i);
1168 continue;
1169 }
1170
1171 /*
1172 * NOTE: for the 8139C+, the frame length field
1173 * is always 12 bits in size, but for the gigE chips,
1174 * it is 13 bits (since the max RX frame length is 16K).
1175 * Unfortunately, all 32 bits in the status word
1176 * were already used, so to make room for the extra
1177 * length bit, RealTek took out the 'frame alignment
1178 * error' bit and shifted the other status bits
1179 * over one slot. The OWN, EOR, FS and LS bits are
1180 * still in the same places. We have already extracted
1181 * the frame length and checked the OWN bit, so rather
1182 * than using an alternate bit mapping, we shift the
1183 * status bits one space to the right so we can evaluate
1184 * them using the 8169 status as though it was in the
1185 * same format as that of the 8139C+.
1186 */
1187 if (sc->rtk_type == RTK_8169)
1188 rxstat >>= 1;
1189
1190 if (rxstat & RTK_RDESC_STAT_RXERRSUM) {
1191 ifp->if_ierrors++;
1192 /*
1193 * If this is part of a multi-fragment packet,
1194 * discard all the pieces.
1195 */
1196 if (sc->rtk_head != NULL) {
1197 m_freem(sc->rtk_head);
1198 sc->rtk_head = sc->rtk_tail = NULL;
1199 }
1200 re_newbuf(sc, i, m);
1201 RTK_DESC_INC(i);
1202 continue;
1203 }
1204
1205 /*
1206 * If allocating a replacement mbuf fails,
1207 * reload the current one.
1208 */
1209
1210 if (re_newbuf(sc, i, NULL)) {
1211 ifp->if_ierrors++;
1212 if (sc->rtk_head != NULL) {
1213 m_freem(sc->rtk_head);
1214 sc->rtk_head = sc->rtk_tail = NULL;
1215 }
1216 re_newbuf(sc, i, m);
1217 RTK_DESC_INC(i);
1218 continue;
1219 }
1220
1221 RTK_DESC_INC(i);
1222
1223 if (sc->rtk_head != NULL) {
1224 m->m_len = total_len % (MCLBYTES - RTK_ETHER_ALIGN);
1225 /*
1226 * Special case: if there's 4 bytes or less
1227 * in this buffer, the mbuf can be discarded:
1228 * the last 4 bytes is the CRC, which we don't
1229 * care about anyway.
1230 */
1231 if (m->m_len <= ETHER_CRC_LEN) {
1232 sc->rtk_tail->m_len -=
1233 (ETHER_CRC_LEN - m->m_len);
1234 m_freem(m);
1235 } else {
1236 m->m_len -= ETHER_CRC_LEN;
1237 m->m_flags &= ~M_PKTHDR;
1238 sc->rtk_tail->m_next = m;
1239 }
1240 m = sc->rtk_head;
1241 sc->rtk_head = sc->rtk_tail = NULL;
1242 m->m_pkthdr.len = total_len - ETHER_CRC_LEN;
1243 } else
1244 m->m_pkthdr.len = m->m_len =
1245 (total_len - ETHER_CRC_LEN);
1246
1247 ifp->if_ipackets++;
1248 m->m_pkthdr.rcvif = ifp;
1249
1250 /* Do RX checksumming if enabled */
1251
1252 if (ifp->if_capenable & IFCAP_CSUM_IPv4) {
1253
1254 /* Check IP header checksum */
1255 if (rxstat & RTK_RDESC_STAT_PROTOID)
1256 m->m_pkthdr.csum_flags |= M_CSUM_IPv4;;
1257 if (rxstat & RTK_RDESC_STAT_IPSUMBAD)
1258 m->m_pkthdr.csum_flags |= M_CSUM_IPv4_BAD;
1259 }
1260
1261 /* Check TCP/UDP checksum */
1262 if (RTK_TCPPKT(rxstat) &&
1263 (ifp->if_capenable & IFCAP_CSUM_TCPv4)) {
1264 m->m_pkthdr.csum_flags |= M_CSUM_TCPv4;
1265 if (rxstat & RTK_RDESC_STAT_TCPSUMBAD)
1266 m->m_pkthdr.csum_flags |= M_CSUM_TCP_UDP_BAD;
1267 }
1268 if (RTK_UDPPKT(rxstat) &&
1269 (ifp->if_capenable & IFCAP_CSUM_UDPv4)) {
1270 m->m_pkthdr.csum_flags |= M_CSUM_UDPv4;
1271 if (rxstat & RTK_RDESC_STAT_UDPSUMBAD)
1272 m->m_pkthdr.csum_flags |= M_CSUM_TCP_UDP_BAD;
1273 }
1274
1275 if (rxvlan & RTK_RDESC_VLANCTL_TAG) {
1276 mtag = m_tag_get(PACKET_TAG_VLAN, sizeof(u_int),
1277 M_NOWAIT);
1278 if (mtag == NULL) {
1279 ifp->if_ierrors++;
1280 m_freem(m);
1281 continue;
1282 }
1283 *(u_int *)(mtag + 1) =
1284 be16toh(rxvlan & RTK_RDESC_VLANCTL_DATA);
1285 m_tag_prepend(m, mtag);
1286 }
1287 #if NBPFILTER > 0
1288 if (ifp->if_bpf)
1289 bpf_mtap(ifp->if_bpf, m);
1290 #endif
1291 (*ifp->if_input)(ifp, m);
1292 }
1293
1294 /* Flush the RX DMA ring */
1295
1296 bus_dmamap_sync(sc->sc_dmat,
1297 sc->rtk_ldata.rtk_rx_list_map,
1298 0, sc->rtk_ldata.rtk_rx_list_map->dm_mapsize,
1299 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
1300
1301 sc->rtk_ldata.rtk_rx_prodidx = i;
1302
1303 return;
1304 }
1305
1306 static void
1307 re_txeof(struct rtk_softc *sc)
1308 {
1309 struct ifnet *ifp;
1310 u_int32_t txstat;
1311 int idx;
1312
1313 ifp = &sc->ethercom.ec_if;
1314 idx = sc->rtk_ldata.rtk_tx_considx;
1315
1316 /* Invalidate the TX descriptor list */
1317
1318 bus_dmamap_sync(sc->sc_dmat,
1319 sc->rtk_ldata.rtk_tx_list_map,
1320 0, sc->rtk_ldata.rtk_tx_list_map->dm_mapsize,
1321 BUS_DMASYNC_POSTREAD);
1322
1323 while (idx != sc->rtk_ldata.rtk_tx_prodidx) {
1324
1325 txstat = le32toh(sc->rtk_ldata.rtk_tx_list[idx].rtk_cmdstat);
1326 if (txstat & RTK_TDESC_CMD_OWN)
1327 break;
1328
1329 /*
1330 * We only stash mbufs in the last descriptor
1331 * in a fragment chain, which also happens to
1332 * be the only place where the TX status bits
1333 * are valid.
1334 */
1335
1336 if (txstat & RTK_TDESC_CMD_EOF) {
1337 m_freem(sc->rtk_ldata.rtk_tx_mbuf[idx]);
1338 sc->rtk_ldata.rtk_tx_mbuf[idx] = NULL;
1339 bus_dmamap_unload(sc->sc_dmat,
1340 sc->rtk_ldata.rtk_tx_dmamap[idx]);
1341 if (txstat & (RTK_TDESC_STAT_EXCESSCOL |
1342 RTK_TDESC_STAT_COLCNT))
1343 ifp->if_collisions++;
1344 if (txstat & RTK_TDESC_STAT_TXERRSUM)
1345 ifp->if_oerrors++;
1346 else
1347 ifp->if_opackets++;
1348 }
1349 sc->rtk_ldata.rtk_tx_free++;
1350 RTK_DESC_INC(idx);
1351 }
1352
1353 /* No changes made to the TX ring, so no flush needed */
1354
1355 if (idx != sc->rtk_ldata.rtk_tx_considx) {
1356 sc->rtk_ldata.rtk_tx_considx = idx;
1357 ifp->if_flags &= ~IFF_OACTIVE;
1358 ifp->if_timer = 0;
1359 }
1360
1361 /*
1362 * If not all descriptors have been released reaped yet,
1363 * reload the timer so that we will eventually get another
1364 * interrupt that will cause us to re-enter this routine.
1365 * This is done in case the transmitter has gone idle.
1366 */
1367 if (sc->rtk_ldata.rtk_tx_free != RTK_TX_DESC_CNT)
1368 CSR_WRITE_4(sc, RTK_TIMERCNT, 1);
1369
1370 return;
1371 }
1372
1373 /*
1374 * Stop all chip I/O so that the kernel's probe routines don't
1375 * get confused by errant DMAs when rebooting.
1376 */
1377 static void
1378 re_shutdown(void *vsc)
1379
1380 {
1381 struct rtk_softc *sc = (struct rtk_softc *)vsc;
1382
1383 re_stop(&sc->ethercom.ec_if, 0);
1384 }
1385
1386
1387 static void
1388 re_tick(void *xsc)
1389 {
1390 struct rtk_softc *sc = xsc;
1391 int s;
1392
1393 /*XXX: just return for 8169S/8110S with rev 2 or newer phy */
1394 s = splnet();
1395
1396 mii_tick(&sc->mii);
1397 splx(s);
1398
1399 callout_reset(&sc->rtk_tick_ch, hz, re_tick, sc);
1400 }
1401
1402 #ifdef DEVICE_POLLING
1403 static void
1404 re_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
1405 {
1406 struct rtk_softc *sc = ifp->if_softc;
1407
1408 RTK_LOCK(sc);
1409 if (!(ifp->if_capenable & IFCAP_POLLING)) {
1410 ether_poll_deregister(ifp);
1411 cmd = POLL_DEREGISTER;
1412 }
1413 if (cmd == POLL_DEREGISTER) { /* final call, enable interrupts */
1414 CSR_WRITE_2(sc, RTK_IMR, RTK_INTRS_CPLUS);
1415 goto done;
1416 }
1417
1418 sc->rxcycles = count;
1419 re_rxeof(sc);
1420 re_txeof(sc);
1421
1422 if (ifp->if_snd.ifq_head != NULL)
1423 (*ifp->if_start)(ifp);
1424
1425 if (cmd == POLL_AND_CHECK_STATUS) { /* also check status register */
1426 u_int16_t status;
1427
1428 status = CSR_READ_2(sc, RTK_ISR);
1429 if (status == 0xffff)
1430 goto done;
1431 if (status)
1432 CSR_WRITE_2(sc, RTK_ISR, status);
1433
1434 /*
1435 * XXX check behaviour on receiver stalls.
1436 */
1437
1438 if (status & RTK_ISR_SYSTEM_ERR) {
1439 re_reset(sc);
1440 re_init(sc);
1441 }
1442 }
1443 done:
1444 RTK_UNLOCK(sc);
1445 }
1446 #endif /* DEVICE_POLLING */
1447
1448 int
1449 re_intr(void *arg)
1450 {
1451 struct rtk_softc *sc = arg;
1452 struct ifnet *ifp;
1453 u_int16_t status;
1454 int handled = 0;
1455
1456 ifp = &sc->ethercom.ec_if;
1457
1458 if (!(ifp->if_flags & IFF_UP))
1459 return 0;
1460
1461 #ifdef DEVICE_POLLING
1462 if (ifp->if_flags & IFF_POLLING)
1463 goto done;
1464 if ((ifp->if_capenable & IFCAP_POLLING) &&
1465 ether_poll_register(re_poll, ifp)) { /* ok, disable interrupts */
1466 CSR_WRITE_2(sc, RTK_IMR, 0x0000);
1467 re_poll(ifp, 0, 1);
1468 goto done;
1469 }
1470 #endif /* DEVICE_POLLING */
1471
1472 for (;;) {
1473
1474 status = CSR_READ_2(sc, RTK_ISR);
1475 /* If the card has gone away the read returns 0xffff. */
1476 if (status == 0xffff)
1477 break;
1478 if (status) {
1479 handled = 1;
1480 CSR_WRITE_2(sc, RTK_ISR, status);
1481 }
1482
1483 if ((status & RTK_INTRS_CPLUS) == 0)
1484 break;
1485
1486 if ((status & RTK_ISR_RX_OK) ||
1487 (status & RTK_ISR_RX_ERR))
1488 re_rxeof(sc);
1489
1490 if ((status & RTK_ISR_TIMEOUT_EXPIRED) ||
1491 (status & RTK_ISR_TX_ERR) ||
1492 (status & RTK_ISR_TX_DESC_UNAVAIL))
1493 re_txeof(sc);
1494
1495 if (status & RTK_ISR_SYSTEM_ERR) {
1496 re_reset(sc);
1497 re_init(ifp);
1498 }
1499
1500 if (status & RTK_ISR_LINKCHG) {
1501 callout_stop(&sc->rtk_tick_ch);
1502 re_tick(sc);
1503 }
1504 }
1505
1506 if (ifp->if_flags & IFF_UP) /* kludge for interrupt during re_init() */
1507 if (ifp->if_snd.ifq_head != NULL)
1508 (*ifp->if_start)(ifp);
1509
1510 #ifdef DEVICE_POLLING
1511 done:
1512 #endif
1513
1514 return handled;
1515 }
1516
1517 static int
1518 re_encap(struct rtk_softc *sc, struct mbuf *m_head, int *idx)
1519 {
1520 bus_dmamap_t map;
1521 int error, i, curidx;
1522 struct m_tag *mtag;
1523 struct rtk_desc *d;
1524 u_int32_t cmdstat, rtk_flags;
1525
1526 if (sc->rtk_ldata.rtk_tx_free <= 4)
1527 return EFBIG;
1528
1529 /*
1530 * Set up checksum offload. Note: checksum offload bits must
1531 * appear in all descriptors of a multi-descriptor transmit
1532 * attempt. (This is according to testing done with an 8169
1533 * chip. I'm not sure if this is a requirement or a bug.)
1534 */
1535
1536 rtk_flags = 0;
1537
1538 if (m_head->m_pkthdr.csum_flags & M_CSUM_IPv4)
1539 rtk_flags |= RTK_TDESC_CMD_IPCSUM;
1540 if (m_head->m_pkthdr.csum_flags & M_CSUM_TCPv4)
1541 rtk_flags |= RTK_TDESC_CMD_TCPCSUM;
1542 if (m_head->m_pkthdr.csum_flags & M_CSUM_UDPv4)
1543 rtk_flags |= RTK_TDESC_CMD_UDPCSUM;
1544
1545 map = sc->rtk_ldata.rtk_tx_dmamap[*idx];
1546 error = bus_dmamap_load_mbuf(sc->sc_dmat, map,
1547 m_head, BUS_DMA_NOWAIT);
1548
1549 if (error) {
1550 aprint_error("%s: can't map mbuf (error %d)\n",
1551 sc->sc_dev.dv_xname, error);
1552 return ENOBUFS;
1553 }
1554
1555 if (map->dm_nsegs > sc->rtk_ldata.rtk_tx_free - 4)
1556 return ENOBUFS;
1557 /*
1558 * Map the segment array into descriptors. Note that we set the
1559 * start-of-frame and end-of-frame markers for either TX or RX, but
1560 * they really only have meaning in the TX case. (In the RX case,
1561 * it's the chip that tells us where packets begin and end.)
1562 * We also keep track of the end of the ring and set the
1563 * end-of-ring bits as needed, and we set the ownership bits
1564 * in all except the very first descriptor. (The caller will
1565 * set this descriptor later when it start transmission or
1566 * reception.)
1567 */
1568 i = 0;
1569 curidx = *idx;
1570 while (1) {
1571 d = &sc->rtk_ldata.rtk_tx_list[curidx];
1572 if (le32toh(d->rtk_cmdstat) & RTK_RDESC_STAT_OWN)
1573 return ENOBUFS;
1574
1575 cmdstat = map->dm_segs[i].ds_len;
1576 d->rtk_bufaddr_lo =
1577 htole32(RTK_ADDR_LO(map->dm_segs[i].ds_addr));
1578 d->rtk_bufaddr_hi =
1579 htole32(RTK_ADDR_HI(map->dm_segs[i].ds_addr));
1580 if (i == 0)
1581 cmdstat |= RTK_TDESC_CMD_SOF;
1582 else
1583 cmdstat |= RTK_TDESC_CMD_OWN;
1584 if (curidx == (RTK_RX_DESC_CNT - 1))
1585 cmdstat |= RTK_TDESC_CMD_EOR;
1586 d->rtk_cmdstat = htole32(cmdstat | rtk_flags);
1587 i++;
1588 if (i == map->dm_nsegs)
1589 break;
1590 RTK_DESC_INC(curidx);
1591 }
1592
1593 d->rtk_cmdstat |= htole32(RTK_TDESC_CMD_EOF);
1594
1595 /*
1596 * Insure that the map for this transmission
1597 * is placed at the array index of the last descriptor
1598 * in this chain.
1599 */
1600 sc->rtk_ldata.rtk_tx_dmamap[*idx] =
1601 sc->rtk_ldata.rtk_tx_dmamap[curidx];
1602 sc->rtk_ldata.rtk_tx_dmamap[curidx] = map;
1603 sc->rtk_ldata.rtk_tx_mbuf[curidx] = m_head;
1604 sc->rtk_ldata.rtk_tx_free -= map->dm_nsegs;
1605
1606 /*
1607 * Set up hardware VLAN tagging. Note: vlan tag info must
1608 * appear in the first descriptor of a multi-descriptor
1609 * transmission attempt.
1610 */
1611
1612 if (sc->ethercom.ec_nvlans &&
1613 (mtag = m_tag_find(m_head, PACKET_TAG_VLAN, NULL)) != NULL)
1614 sc->rtk_ldata.rtk_tx_list[*idx].rtk_vlanctl =
1615 htole32(htons(*(u_int *)(mtag + 1)) |
1616 RTK_TDESC_VLANCTL_TAG);
1617
1618 /* Transfer ownership of packet to the chip. */
1619
1620 sc->rtk_ldata.rtk_tx_list[curidx].rtk_cmdstat |=
1621 htole32(RTK_TDESC_CMD_OWN);
1622 if (*idx != curidx)
1623 sc->rtk_ldata.rtk_tx_list[*idx].rtk_cmdstat |=
1624 htole32(RTK_TDESC_CMD_OWN);
1625
1626 RTK_DESC_INC(curidx);
1627 *idx = curidx;
1628
1629 return 0;
1630 }
1631
1632 /*
1633 * Main transmit routine for C+ and gigE NICs.
1634 */
1635
1636 static void
1637 re_start(struct ifnet *ifp)
1638 {
1639 struct rtk_softc *sc;
1640 struct mbuf *m_head = NULL;
1641 int idx;
1642
1643 sc = ifp->if_softc;
1644
1645 idx = sc->rtk_ldata.rtk_tx_prodidx;
1646 while (sc->rtk_ldata.rtk_tx_mbuf[idx] == NULL) {
1647 IF_DEQUEUE(&ifp->if_snd, m_head);
1648 if (m_head == NULL)
1649 break;
1650
1651 if (re_encap(sc, m_head, &idx)) {
1652 IF_PREPEND(&ifp->if_snd, m_head);
1653 ifp->if_flags |= IFF_OACTIVE;
1654 break;
1655 }
1656 #if NBPFILTER > 0
1657 /*
1658 * If there's a BPF listener, bounce a copy of this frame
1659 * to him.
1660 */
1661 if (ifp->if_bpf)
1662 bpf_mtap(ifp->if_bpf, m_head);
1663 #endif
1664 }
1665
1666 /* Flush the TX descriptors */
1667
1668 bus_dmamap_sync(sc->sc_dmat,
1669 sc->rtk_ldata.rtk_tx_list_map,
1670 0, sc->rtk_ldata.rtk_tx_list_map->dm_mapsize,
1671 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
1672
1673 sc->rtk_ldata.rtk_tx_prodidx = idx;
1674
1675 /*
1676 * RealTek put the TX poll request register in a different
1677 * location on the 8169 gigE chip. I don't know why.
1678 */
1679
1680 if (sc->rtk_type == RTK_8169)
1681 CSR_WRITE_2(sc, RTK_GTXSTART, RTK_TXSTART_START);
1682 else
1683 CSR_WRITE_2(sc, RTK_TXSTART, RTK_TXSTART_START);
1684
1685 /*
1686 * Use the countdown timer for interrupt moderation.
1687 * 'TX done' interrupts are disabled. Instead, we reset the
1688 * countdown timer, which will begin counting until it hits
1689 * the value in the TIMERINT register, and then trigger an
1690 * interrupt. Each time we write to the TIMERCNT register,
1691 * the timer count is reset to 0.
1692 */
1693 CSR_WRITE_4(sc, RTK_TIMERCNT, 1);
1694
1695 /*
1696 * Set a timeout in case the chip goes out to lunch.
1697 */
1698 ifp->if_timer = 5;
1699
1700 return;
1701 }
1702
1703 static int
1704 re_init(struct ifnet *ifp)
1705 {
1706 struct rtk_softc *sc = ifp->if_softc;
1707 u_int32_t rxcfg = 0;
1708 u_int32_t reg;
1709 int error;
1710
1711 if ((error = re_enable(sc)) != 0)
1712 goto out;
1713
1714 /*
1715 * Cancel pending I/O and free all RX/TX buffers.
1716 */
1717 re_stop(ifp, 0);
1718
1719 /*
1720 * Enable C+ RX and TX mode, as well as VLAN stripping and
1721 * RX checksum offload. We must configure the C+ register
1722 * before all others.
1723 */
1724 reg = 0;
1725
1726 /*
1727 * XXX: Realtek docs say bits 0 and 1 are reserved, for 8169S/8110S.
1728 * FreeBSD drivers set these bits anyway (for 8139C+?).
1729 * So far, it works.
1730 */
1731
1732 /*
1733 * XXX: For 8169 and 8196S revs below 2, set bit 14.
1734 * For 8169S/8110S rev 2 and above, do not set bit 14.
1735 */
1736 if (sc->rtk_type == RTK_8169 && sc->sc_rev == 1)
1737 reg |= (0x1 << 14) | RTK_CPLUSCMD_PCI_MRW;;
1738
1739 if (1) {/* not for 8169S ? */
1740 reg |= RTK_CPLUSCMD_VLANSTRIP |
1741 (ifp->if_capenable &
1742 (IFCAP_CSUM_IPv4 | IFCAP_CSUM_TCPv4 | IFCAP_CSUM_UDPv4) ?
1743 RTK_CPLUSCMD_RXCSUM_ENB : 0);
1744 }
1745
1746 CSR_WRITE_2(sc, RTK_CPLUS_CMD,
1747 reg | RTK_CPLUSCMD_RXENB | RTK_CPLUSCMD_TXENB);
1748
1749 /* XXX: from Realtek-supplied Linux driver. Wholly undocumented. */
1750 if (sc->rtk_type == RTK_8169)
1751 CSR_WRITE_2(sc, RTK_CPLUS_CMD+0x2, 0x0000);
1752
1753 DELAY(10000);
1754
1755 /*
1756 * Init our MAC address. Even though the chipset
1757 * documentation doesn't mention it, we need to enter "Config
1758 * register write enable" mode to modify the ID registers.
1759 */
1760 CSR_WRITE_1(sc, RTK_EECMD, RTK_EEMODE_WRITECFG);
1761 memcpy(®, LLADDR(ifp->if_sadl), 4);
1762 CSR_WRITE_STREAM_4(sc, RTK_IDR0, reg);
1763 reg = 0;
1764 memcpy(®, LLADDR(ifp->if_sadl) + 4, 4);
1765 CSR_WRITE_STREAM_4(sc, RTK_IDR4, reg);
1766 CSR_WRITE_1(sc, RTK_EECMD, RTK_EEMODE_OFF);
1767
1768 /*
1769 * For C+ mode, initialize the RX descriptors and mbufs.
1770 */
1771 re_rx_list_init(sc);
1772 re_tx_list_init(sc);
1773
1774 /*
1775 * Enable transmit and receive.
1776 */
1777 CSR_WRITE_1(sc, RTK_COMMAND, RTK_CMD_TX_ENB | RTK_CMD_RX_ENB);
1778
1779 /*
1780 * Set the initial TX and RX configuration.
1781 */
1782 if (sc->rtk_testmode) {
1783 if (sc->rtk_type == RTK_8169)
1784 CSR_WRITE_4(sc, RTK_TXCFG,
1785 RTK_TXCFG_CONFIG | RTK_LOOPTEST_ON);
1786 else
1787 CSR_WRITE_4(sc, RTK_TXCFG,
1788 RTK_TXCFG_CONFIG | RTK_LOOPTEST_ON_CPLUS);
1789 } else
1790 CSR_WRITE_4(sc, RTK_TXCFG, RTK_TXCFG_CONFIG);
1791 CSR_WRITE_4(sc, RTK_RXCFG, RTK_RXCFG_CONFIG);
1792
1793 /* Set the individual bit to receive frames for this host only. */
1794 rxcfg = CSR_READ_4(sc, RTK_RXCFG);
1795 rxcfg |= RTK_RXCFG_RX_INDIV;
1796
1797 /* If we want promiscuous mode, set the allframes bit. */
1798 if (ifp->if_flags & IFF_PROMISC)
1799 rxcfg |= RTK_RXCFG_RX_ALLPHYS;
1800 else
1801 rxcfg &= ~RTK_RXCFG_RX_ALLPHYS;
1802 CSR_WRITE_4(sc, RTK_RXCFG, rxcfg);
1803
1804 /*
1805 * Set capture broadcast bit to capture broadcast frames.
1806 */
1807 if (ifp->if_flags & IFF_BROADCAST)
1808 rxcfg |= RTK_RXCFG_RX_BROAD;
1809 else
1810 rxcfg &= ~RTK_RXCFG_RX_BROAD;
1811 CSR_WRITE_4(sc, RTK_RXCFG, rxcfg);
1812
1813 /*
1814 * Program the multicast filter, if necessary.
1815 */
1816 rtk_setmulti(sc);
1817
1818 #ifdef DEVICE_POLLING
1819 /*
1820 * Disable interrupts if we are polling.
1821 */
1822 if (ifp->if_flags & IFF_POLLING)
1823 CSR_WRITE_2(sc, RTK_IMR, 0);
1824 else /* otherwise ... */
1825 #endif /* DEVICE_POLLING */
1826 /*
1827 * Enable interrupts.
1828 */
1829 if (sc->rtk_testmode)
1830 CSR_WRITE_2(sc, RTK_IMR, 0);
1831 else
1832 CSR_WRITE_2(sc, RTK_IMR, RTK_INTRS_CPLUS);
1833
1834 /* Start RX/TX process. */
1835 CSR_WRITE_4(sc, RTK_MISSEDPKT, 0);
1836 #ifdef notdef
1837 /* Enable receiver and transmitter. */
1838 CSR_WRITE_1(sc, RTK_COMMAND, RTK_CMD_TX_ENB | RTK_CMD_RX_ENB);
1839 #endif
1840 /*
1841 * Load the addresses of the RX and TX lists into the chip.
1842 */
1843
1844 CSR_WRITE_4(sc, RTK_RXLIST_ADDR_HI,
1845 RTK_ADDR_HI(sc->rtk_ldata.rtk_rx_listseg.ds_addr));
1846 CSR_WRITE_4(sc, RTK_RXLIST_ADDR_LO,
1847 RTK_ADDR_LO(sc->rtk_ldata.rtk_rx_listseg.ds_addr));
1848
1849 CSR_WRITE_4(sc, RTK_TXLIST_ADDR_HI,
1850 RTK_ADDR_HI(sc->rtk_ldata.rtk_tx_listseg.ds_addr));
1851 CSR_WRITE_4(sc, RTK_TXLIST_ADDR_LO,
1852 RTK_ADDR_LO(sc->rtk_ldata.rtk_tx_listseg.ds_addr));
1853
1854 CSR_WRITE_1(sc, RTK_EARLY_TX_THRESH, 16);
1855
1856 /*
1857 * Initialize the timer interrupt register so that
1858 * a timer interrupt will be generated once the timer
1859 * reaches a certain number of ticks. The timer is
1860 * reloaded on each transmit. This gives us TX interrupt
1861 * moderation, which dramatically improves TX frame rate.
1862 */
1863
1864 if (sc->rtk_type == RTK_8169)
1865 CSR_WRITE_4(sc, RTK_TIMERINT_8169, 0x800);
1866 else
1867 CSR_WRITE_4(sc, RTK_TIMERINT, 0x400);
1868
1869 /*
1870 * For 8169 gigE NICs, set the max allowed RX packet
1871 * size so we can receive jumbo frames.
1872 */
1873 if (sc->rtk_type == RTK_8169)
1874 CSR_WRITE_2(sc, RTK_MAXRXPKTLEN, 16383);
1875
1876 if (sc->rtk_testmode)
1877 return 0;
1878
1879 mii_mediachg(&sc->mii);
1880
1881 CSR_WRITE_1(sc, RTK_CFG1, RTK_CFG1_DRVLOAD | RTK_CFG1_FULLDUPLEX);
1882
1883 ifp->if_flags |= IFF_RUNNING;
1884 ifp->if_flags &= ~IFF_OACTIVE;
1885
1886 callout_reset(&sc->rtk_tick_ch, hz, re_tick, sc);
1887
1888 out:
1889 if (error) {
1890 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1891 ifp->if_timer = 0;
1892 aprint_error("%s: interface not running\n",
1893 sc->sc_dev.dv_xname);
1894 }
1895
1896 return error;
1897
1898 }
1899
1900 /*
1901 * Set media options.
1902 */
1903 static int
1904 re_ifmedia_upd(struct ifnet *ifp)
1905 {
1906 struct rtk_softc *sc;
1907
1908 sc = ifp->if_softc;
1909
1910 return mii_mediachg(&sc->mii);
1911 }
1912
1913 /*
1914 * Report current media status.
1915 */
1916 static void
1917 re_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
1918 {
1919 struct rtk_softc *sc;
1920
1921 sc = ifp->if_softc;
1922
1923 mii_pollstat(&sc->mii);
1924 ifmr->ifm_active = sc->mii.mii_media_active;
1925 ifmr->ifm_status = sc->mii.mii_media_status;
1926
1927 return;
1928 }
1929
1930 static int
1931 re_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
1932 {
1933 struct rtk_softc *sc = ifp->if_softc;
1934 struct ifreq *ifr = (struct ifreq *) data;
1935 int s, error = 0;
1936
1937 s = splnet();
1938
1939 switch (command) {
1940 case SIOCSIFMTU:
1941 if (ifr->ifr_mtu > RTK_JUMBO_MTU)
1942 error = EINVAL;
1943 ifp->if_mtu = ifr->ifr_mtu;
1944 break;
1945 case SIOCGIFMEDIA:
1946 case SIOCSIFMEDIA:
1947 error = ifmedia_ioctl(ifp, ifr, &sc->mii.mii_media, command);
1948 break;
1949 default:
1950 error = ether_ioctl(ifp, command, data);
1951 if (error == ENETRESET) {
1952 if (ifp->if_flags & IFF_RUNNING)
1953 rtk_setmulti(sc);
1954 error = 0;
1955 }
1956 break;
1957 }
1958
1959 splx(s);
1960
1961 return error;
1962 }
1963
1964 static void
1965 re_watchdog(struct ifnet *ifp)
1966 {
1967 struct rtk_softc *sc;
1968 int s;
1969
1970 sc = ifp->if_softc;
1971 s = splnet();
1972 aprint_error("%s: watchdog timeout\n", sc->sc_dev.dv_xname);
1973 ifp->if_oerrors++;
1974
1975 re_txeof(sc);
1976 re_rxeof(sc);
1977
1978 re_init(ifp);
1979
1980 splx(s);
1981 }
1982
1983 /*
1984 * Stop the adapter and free any mbufs allocated to the
1985 * RX and TX lists.
1986 */
1987 static void
1988 re_stop(struct ifnet *ifp, int disable)
1989 {
1990 register int i;
1991 struct rtk_softc *sc = ifp->if_softc;
1992
1993 callout_stop(&sc->rtk_tick_ch);
1994
1995 #ifdef DEVICE_POLLING
1996 ether_poll_deregister(ifp);
1997 #endif /* DEVICE_POLLING */
1998
1999 mii_down(&sc->mii);
2000
2001 CSR_WRITE_1(sc, RTK_COMMAND, 0x00);
2002 CSR_WRITE_2(sc, RTK_IMR, 0x0000);
2003
2004 if (sc->rtk_head != NULL) {
2005 m_freem(sc->rtk_head);
2006 sc->rtk_head = sc->rtk_tail = NULL;
2007 }
2008
2009 /* Free the TX list buffers. */
2010 for (i = 0; i < RTK_TX_DESC_CNT; i++) {
2011 if (sc->rtk_ldata.rtk_tx_mbuf[i] != NULL) {
2012 bus_dmamap_unload(sc->sc_dmat,
2013 sc->rtk_ldata.rtk_tx_dmamap[i]);
2014 m_freem(sc->rtk_ldata.rtk_tx_mbuf[i]);
2015 sc->rtk_ldata.rtk_tx_mbuf[i] = NULL;
2016 }
2017 }
2018
2019 /* Free the RX list buffers. */
2020 for (i = 0; i < RTK_RX_DESC_CNT; i++) {
2021 if (sc->rtk_ldata.rtk_rx_mbuf[i] != NULL) {
2022 bus_dmamap_unload(sc->sc_dmat,
2023 sc->rtk_ldata.rtk_rx_dmamap[i]);
2024 m_freem(sc->rtk_ldata.rtk_rx_mbuf[i]);
2025 sc->rtk_ldata.rtk_rx_mbuf[i] = NULL;
2026 }
2027 }
2028
2029 if (disable)
2030 re_disable(sc);
2031
2032 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
2033 ifp->if_timer = 0;
2034
2035 return;
2036 }
2037