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