rtl81x9.c revision 1.14 1 /* $NetBSD: rtl81x9.c,v 1.14 2000/10/01 23:32:42 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 1997, 1998
5 * Bill Paul <wpaul (at) ctr.columbia.edu>. 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 * FreeBSD Id: if_rl.c,v 1.17 1999/06/19 20:17:37 wpaul Exp
35 */
36
37 /*
38 * RealTek 8129/8139 PCI NIC driver
39 *
40 * Supports several extremely cheap PCI 10/100 adapters based on
41 * the RealTek chipset. Datasheets can be obtained from
42 * www.realtek.com.tw.
43 *
44 * Written by Bill Paul <wpaul (at) ctr.columbia.edu>
45 * Electrical Engineering Department
46 * Columbia University, New York City
47 */
48
49 /*
50 * The RealTek 8139 PCI NIC redefines the meaning of 'low end.' This is
51 * probably the worst PCI ethernet controller ever made, with the possible
52 * exception of the FEAST chip made by SMC. The 8139 supports bus-master
53 * DMA, but it has a terrible interface that nullifies any performance
54 * gains that bus-master DMA usually offers.
55 *
56 * For transmission, the chip offers a series of four TX descriptor
57 * registers. Each transmit frame must be in a contiguous buffer, aligned
58 * on a longword (32-bit) boundary. This means we almost always have to
59 * do mbuf copies in order to transmit a frame, except in the unlikely
60 * case where a) the packet fits into a single mbuf, and b) the packet
61 * is 32-bit aligned within the mbuf's data area. The presence of only
62 * four descriptor registers means that we can never have more than four
63 * packets queued for transmission at any one time.
64 *
65 * Reception is not much better. The driver has to allocate a single large
66 * buffer area (up to 64K in size) into which the chip will DMA received
67 * frames. Because we don't know where within this region received packets
68 * will begin or end, we have no choice but to copy data from the buffer
69 * area into mbufs in order to pass the packets up to the higher protocol
70 * levels.
71 *
72 * It's impossible given this rotten design to really achieve decent
73 * performance at 100Mbps, unless you happen to have a 400Mhz PII or
74 * some equally overmuscled CPU to drive it.
75 *
76 * On the bright side, the 8139 does have a built-in PHY, although
77 * rather than using an MDIO serial interface like most other NICs, the
78 * PHY registers are directly accessible through the 8139's register
79 * space. The 8139 supports autonegotiation, as well as a 64-bit multicast
80 * filter.
81 *
82 * The 8129 chip is an older version of the 8139 that uses an external PHY
83 * chip. The 8129 has a serial MDIO interface for accessing the MII where
84 * the 8139 lets you directly access the on-board PHY registers. We need
85 * to select which interface to use depending on the chip type.
86 */
87
88 #include "opt_inet.h"
89 #include "opt_ns.h"
90 #include "bpfilter.h"
91 #include "rnd.h"
92
93 #include <sys/param.h>
94 #include <sys/systm.h>
95 #include <sys/callout.h>
96 #include <sys/device.h>
97 #include <sys/sockio.h>
98 #include <sys/mbuf.h>
99 #include <sys/malloc.h>
100 #include <sys/kernel.h>
101 #include <sys/socket.h>
102
103 #include <net/if.h>
104 #include <net/if_arp.h>
105 #include <net/if_ether.h>
106 #include <net/if_dl.h>
107 #include <net/if_media.h>
108 #ifdef INET
109 #include <netinet/in.h>
110 #include <netinet/if_inarp.h>
111 #endif
112 #ifdef NS
113 #include <netns/ns.h>
114 #include <netns/ns_if.h>
115 #endif
116
117 #if NBPFILTER > 0
118 #include <net/bpf.h>
119 #endif
120 #if NRND > 0
121 #include <sys/rnd.h>
122 #endif
123
124 #include <machine/bus.h>
125 #include <machine/endian.h>
126
127 #include <dev/mii/mii.h>
128 #include <dev/mii/miivar.h>
129
130 #include <dev/ic/rtl81x9reg.h>
131 #include <dev/ic/rtl81x9var.h>
132
133 #if defined DEBUG
134 #define STATIC
135 #else
136 #define STATIC static
137 #endif
138
139 STATIC void rtk_reset __P((struct rtk_softc *));
140 STATIC void rtk_rxeof __P((struct rtk_softc *));
141 STATIC void rtk_txeof __P((struct rtk_softc *));
142 STATIC void rtk_start __P((struct ifnet *));
143 STATIC int rtk_ioctl __P((struct ifnet *, u_long, caddr_t));
144 STATIC void rtk_init __P((void *));
145 STATIC void rtk_stop __P((struct rtk_softc *));
146 STATIC void rtk_watchdog __P((struct ifnet *));
147 STATIC void rtk_shutdown __P((void *));
148 STATIC int rtk_ifmedia_upd __P((struct ifnet *));
149 STATIC void rtk_ifmedia_sts __P((struct ifnet *, struct ifmediareq *));
150
151 STATIC u_int16_t rtk_read_eeprom __P((struct rtk_softc *, int, int));
152 STATIC void rtk_eeprom_putbyte __P((struct rtk_softc *, int, int));
153 STATIC void rtk_mii_sync __P((struct rtk_softc *));
154 STATIC void rtk_mii_send __P((struct rtk_softc *, u_int32_t, int));
155 STATIC int rtk_mii_readreg __P((struct rtk_softc *, struct rtk_mii_frame *));
156 STATIC int rtk_mii_writereg __P((struct rtk_softc *, struct rtk_mii_frame *));
157
158 STATIC int rtk_phy_readreg __P((struct device *, int, int));
159 STATIC void rtk_phy_writereg __P((struct device *, int, int, int));
160 STATIC void rtk_phy_statchg __P((struct device *));
161 STATIC void rtk_tick __P((void *));
162
163 STATIC int rtk_enable __P((struct rtk_softc *));
164 STATIC void rtk_disable __P((struct rtk_softc *));
165 STATIC void rtk_power __P((int, void *));
166
167 STATIC void rtk_setmulti __P((struct rtk_softc *));
168 STATIC int rtk_list_tx_init __P((struct rtk_softc *));
169
170 STATIC int rtk_ether_ioctl __P((struct ifnet *, u_long, caddr_t));
171
172
173 #define EE_SET(x) \
174 CSR_WRITE_1(sc, RTK_EECMD, \
175 CSR_READ_1(sc, RTK_EECMD) | (x))
176
177 #define EE_CLR(x) \
178 CSR_WRITE_1(sc, RTK_EECMD, \
179 CSR_READ_1(sc, RTK_EECMD) & ~(x))
180
181 /*
182 * Send a read command and address to the EEPROM, check for ACK.
183 */
184 STATIC void rtk_eeprom_putbyte(sc, addr, addr_len)
185 struct rtk_softc *sc;
186 int addr, addr_len;
187 {
188 int d, i;
189
190 d = (RTK_EECMD_READ << addr_len) | addr;
191
192 /*
193 * Feed in each bit and stobe the clock.
194 */
195 for (i = RTK_EECMD_LEN + addr_len - 1; i >= 0; i--) {
196 if (d & (1 << i)) {
197 EE_SET(RTK_EE_DATAIN);
198 } else {
199 EE_CLR(RTK_EE_DATAIN);
200 }
201 DELAY(100);
202 EE_SET(RTK_EE_CLK);
203 DELAY(150);
204 EE_CLR(RTK_EE_CLK);
205 DELAY(100);
206 }
207 }
208
209 /*
210 * Read a word of data stored in the EEPROM at address 'addr.'
211 */
212 u_int16_t rtk_read_eeprom(sc, addr, addr_len)
213 struct rtk_softc *sc;
214 int addr, addr_len;
215 {
216 u_int16_t word = 0;
217 int i;
218
219 /* Enter EEPROM access mode. */
220 CSR_WRITE_1(sc, RTK_EECMD, RTK_EEMODE_PROGRAM|RTK_EE_SEL);
221
222 /*
223 * Send address of word we want to read.
224 */
225 rtk_eeprom_putbyte(sc, addr, addr_len);
226
227 CSR_WRITE_1(sc, RTK_EECMD, RTK_EEMODE_PROGRAM|RTK_EE_SEL);
228
229 /*
230 * Start reading bits from EEPROM.
231 */
232 for (i = 15; i >= 0; i--) {
233 EE_SET(RTK_EE_CLK);
234 DELAY(100);
235 if (CSR_READ_1(sc, RTK_EECMD) & RTK_EE_DATAOUT)
236 word |= (1 << i);
237 EE_CLR(RTK_EE_CLK);
238 DELAY(100);
239 }
240
241 /* Turn off EEPROM access mode. */
242 CSR_WRITE_1(sc, RTK_EECMD, RTK_EEMODE_OFF);
243
244 return (word);
245 }
246
247 /*
248 * MII access routines are provided for the 8129, which
249 * doesn't have a built-in PHY. For the 8139, we fake things
250 * up by diverting rtk_phy_readreg()/rtk_phy_writereg() to the
251 * direct access PHY registers.
252 */
253 #define MII_SET(x) \
254 CSR_WRITE_1(sc, RTK_MII, \
255 CSR_READ_1(sc, RTK_MII) | (x))
256
257 #define MII_CLR(x) \
258 CSR_WRITE_1(sc, RTK_MII, \
259 CSR_READ_1(sc, RTK_MII) & ~(x))
260
261 /*
262 * Sync the PHYs by setting data bit and strobing the clock 32 times.
263 */
264 STATIC void rtk_mii_sync(sc)
265 struct rtk_softc *sc;
266 {
267 int i;
268
269 MII_SET(RTK_MII_DIR|RTK_MII_DATAOUT);
270
271 for (i = 0; i < 32; i++) {
272 MII_SET(RTK_MII_CLK);
273 DELAY(1);
274 MII_CLR(RTK_MII_CLK);
275 DELAY(1);
276 }
277
278 return;
279 }
280
281 /*
282 * Clock a series of bits through the MII.
283 */
284 STATIC void rtk_mii_send(sc, bits, cnt)
285 struct rtk_softc *sc;
286 u_int32_t bits;
287 int cnt;
288 {
289 int i;
290
291 MII_CLR(RTK_MII_CLK);
292
293 for (i = (0x1 << (cnt - 1)); i; i >>= 1) {
294 if (bits & i) {
295 MII_SET(RTK_MII_DATAOUT);
296 } else {
297 MII_CLR(RTK_MII_DATAOUT);
298 }
299 DELAY(1);
300 MII_CLR(RTK_MII_CLK);
301 DELAY(1);
302 MII_SET(RTK_MII_CLK);
303 }
304 }
305
306 /*
307 * Read an PHY register through the MII.
308 */
309 STATIC int rtk_mii_readreg(sc, frame)
310 struct rtk_softc *sc;
311 struct rtk_mii_frame *frame;
312
313 {
314 int i, ack, s;
315
316 s = splnet();
317
318 /*
319 * Set up frame for RX.
320 */
321 frame->mii_stdelim = RTK_MII_STARTDELIM;
322 frame->mii_opcode = RTK_MII_READOP;
323 frame->mii_turnaround = 0;
324 frame->mii_data = 0;
325
326 CSR_WRITE_2(sc, RTK_MII, 0);
327
328 /*
329 * Turn on data xmit.
330 */
331 MII_SET(RTK_MII_DIR);
332
333 rtk_mii_sync(sc);
334
335 /*
336 * Send command/address info.
337 */
338 rtk_mii_send(sc, frame->mii_stdelim, 2);
339 rtk_mii_send(sc, frame->mii_opcode, 2);
340 rtk_mii_send(sc, frame->mii_phyaddr, 5);
341 rtk_mii_send(sc, frame->mii_regaddr, 5);
342
343 /* Idle bit */
344 MII_CLR((RTK_MII_CLK|RTK_MII_DATAOUT));
345 DELAY(1);
346 MII_SET(RTK_MII_CLK);
347 DELAY(1);
348
349 /* Turn off xmit. */
350 MII_CLR(RTK_MII_DIR);
351
352 /* Check for ack */
353 MII_CLR(RTK_MII_CLK);
354 DELAY(1);
355 MII_SET(RTK_MII_CLK);
356 DELAY(1);
357 ack = CSR_READ_2(sc, RTK_MII) & RTK_MII_DATAIN;
358
359 /*
360 * Now try reading data bits. If the ack failed, we still
361 * need to clock through 16 cycles to keep the PHY(s) in sync.
362 */
363 if (ack) {
364 for(i = 0; i < 16; i++) {
365 MII_CLR(RTK_MII_CLK);
366 DELAY(1);
367 MII_SET(RTK_MII_CLK);
368 DELAY(1);
369 }
370 goto fail;
371 }
372
373 for (i = 0x8000; i; i >>= 1) {
374 MII_CLR(RTK_MII_CLK);
375 DELAY(1);
376 if (!ack) {
377 if (CSR_READ_2(sc, RTK_MII) & RTK_MII_DATAIN)
378 frame->mii_data |= i;
379 DELAY(1);
380 }
381 MII_SET(RTK_MII_CLK);
382 DELAY(1);
383 }
384
385 fail:
386
387 MII_CLR(RTK_MII_CLK);
388 DELAY(1);
389 MII_SET(RTK_MII_CLK);
390 DELAY(1);
391
392 splx(s);
393
394 if (ack)
395 return(1);
396 return(0);
397 }
398
399 /*
400 * Write to a PHY register through the MII.
401 */
402 STATIC int rtk_mii_writereg(sc, frame)
403 struct rtk_softc *sc;
404 struct rtk_mii_frame *frame;
405
406 {
407 int s;
408
409 s = splnet();
410 /*
411 * Set up frame for TX.
412 */
413
414 frame->mii_stdelim = RTK_MII_STARTDELIM;
415 frame->mii_opcode = RTK_MII_WRITEOP;
416 frame->mii_turnaround = RTK_MII_TURNAROUND;
417
418 /*
419 * Turn on data output.
420 */
421 MII_SET(RTK_MII_DIR);
422
423 rtk_mii_sync(sc);
424
425 rtk_mii_send(sc, frame->mii_stdelim, 2);
426 rtk_mii_send(sc, frame->mii_opcode, 2);
427 rtk_mii_send(sc, frame->mii_phyaddr, 5);
428 rtk_mii_send(sc, frame->mii_regaddr, 5);
429 rtk_mii_send(sc, frame->mii_turnaround, 2);
430 rtk_mii_send(sc, frame->mii_data, 16);
431
432 /* Idle bit. */
433 MII_SET(RTK_MII_CLK);
434 DELAY(1);
435 MII_CLR(RTK_MII_CLK);
436 DELAY(1);
437
438 /*
439 * Turn off xmit.
440 */
441 MII_CLR(RTK_MII_DIR);
442
443 splx(s);
444
445 return(0);
446 }
447
448 STATIC int rtk_phy_readreg(self, phy, reg)
449 struct device *self;
450 int phy, reg;
451 {
452 struct rtk_softc *sc = (void *)self;
453 struct rtk_mii_frame frame;
454 u_int16_t rval = 0;
455 u_int16_t rtk8139_reg = 0;
456
457 if (sc->rtk_type == RTK_8139) {
458 if (phy != 7)
459 return (0);
460
461 switch(reg) {
462 case MII_BMCR:
463 rtk8139_reg = RTK_BMCR;
464 break;
465 case MII_BMSR:
466 rtk8139_reg = RTK_BMSR;
467 break;
468 case MII_ANAR:
469 rtk8139_reg = RTK_ANAR;
470 break;
471 case MII_ANER:
472 rtk8139_reg = RTK_ANER;
473 break;
474 case MII_ANLPAR:
475 rtk8139_reg = RTK_LPAR;
476 break;
477 default:
478 #if 0
479 printf("%s: bad phy register\n", sc->sc_dev.dv_xname);
480 #endif
481 return(0);
482 }
483 rval = CSR_READ_2(sc, rtk8139_reg);
484 return(rval);
485 }
486
487 bzero((char *)&frame, sizeof(frame));
488
489 frame.mii_phyaddr = phy;
490 frame.mii_regaddr = reg;
491 rtk_mii_readreg(sc, &frame);
492
493 return(frame.mii_data);
494 }
495
496 STATIC void rtk_phy_writereg(self, phy, reg, data)
497 struct device *self;
498 int phy, reg;
499 int data;
500 {
501 struct rtk_softc *sc = (void *)self;
502 struct rtk_mii_frame frame;
503 u_int16_t rtk8139_reg = 0;
504
505 if (sc->rtk_type == RTK_8139) {
506 if (phy != 7)
507 return;
508
509 switch(reg) {
510 case MII_BMCR:
511 rtk8139_reg = RTK_BMCR;
512 break;
513 case MII_BMSR:
514 rtk8139_reg = RTK_BMSR;
515 break;
516 case MII_ANAR:
517 rtk8139_reg = RTK_ANAR;
518 break;
519 case MII_ANER:
520 rtk8139_reg = RTK_ANER;
521 break;
522 case MII_ANLPAR:
523 rtk8139_reg = RTK_LPAR;
524 break;
525 default:
526 #if 0
527 printf("%s: bad phy register\n", sc->sc_dev.dv_xname);
528 #endif
529 return;
530 }
531 CSR_WRITE_2(sc, rtk8139_reg, data);
532 return;
533 }
534
535 bzero((char *)&frame, sizeof(frame));
536
537 frame.mii_phyaddr = phy;
538 frame.mii_regaddr = reg;
539 frame.mii_data = data;
540
541 rtk_mii_writereg(sc, &frame);
542
543 return;
544 }
545
546 STATIC void
547 rtk_phy_statchg(v)
548 struct device *v;
549 {
550
551 /* Nothing to do. */
552 }
553
554 #define rtk_calchash(addr) \
555 (ether_crc32_be((addr), ETHER_ADDR_LEN) >> 26)
556
557 /*
558 * Program the 64-bit multicast hash filter.
559 */
560 STATIC void rtk_setmulti(sc)
561 struct rtk_softc *sc;
562 {
563 struct ifnet *ifp;
564 int h = 0;
565 u_int32_t hashes[2] = { 0, 0 };
566 u_int32_t rxfilt;
567 int mcnt = 0;
568 struct ether_multi *enm;
569 struct ether_multistep step;
570
571 ifp = &sc->ethercom.ec_if;
572
573 rxfilt = CSR_READ_4(sc, RTK_RXCFG);
574
575 if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
576 rxfilt |= RTK_RXCFG_RX_MULTI;
577 CSR_WRITE_4(sc, RTK_RXCFG, rxfilt);
578 CSR_WRITE_4(sc, RTK_MAR0, 0xFFFFFFFF);
579 CSR_WRITE_4(sc, RTK_MAR4, 0xFFFFFFFF);
580 return;
581 }
582
583 /* first, zot all the existing hash bits */
584 CSR_WRITE_4(sc, RTK_MAR0, 0);
585 CSR_WRITE_4(sc, RTK_MAR4, 0);
586
587 /* now program new ones */
588 ETHER_FIRST_MULTI(step, &sc->ethercom, enm);
589 while (enm != NULL) {
590 if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
591 ETHER_ADDR_LEN) != 0)
592 continue;
593
594 h = rtk_calchash(enm->enm_addrlo);
595 if (h < 32)
596 hashes[0] |= (1 << h);
597 else
598 hashes[1] |= (1 << (h - 32));
599 mcnt++;
600 ETHER_NEXT_MULTI(step, enm);
601 }
602
603 if (mcnt)
604 rxfilt |= RTK_RXCFG_RX_MULTI;
605 else
606 rxfilt &= ~RTK_RXCFG_RX_MULTI;
607
608 CSR_WRITE_4(sc, RTK_RXCFG, rxfilt);
609 CSR_WRITE_4(sc, RTK_MAR0, hashes[0]);
610 CSR_WRITE_4(sc, RTK_MAR4, hashes[1]);
611
612 return;
613 }
614
615 void rtk_reset(sc)
616 struct rtk_softc *sc;
617 {
618 int i;
619
620 CSR_WRITE_1(sc, RTK_COMMAND, RTK_CMD_RESET);
621
622 for (i = 0; i < RTK_TIMEOUT; i++) {
623 DELAY(10);
624 if (!(CSR_READ_1(sc, RTK_COMMAND) & RTK_CMD_RESET))
625 break;
626 }
627 if (i == RTK_TIMEOUT)
628 printf("%s: reset never completed!\n", sc->sc_dev.dv_xname);
629
630 return;
631 }
632
633 /*
634 * Attach the interface. Allocate softc structures, do ifmedia
635 * setup and ethernet/BPF attach.
636 */
637 void
638 rtk_attach(sc)
639 struct rtk_softc *sc;
640 {
641
642 struct ifnet *ifp;
643 u_int16_t val;
644 u_int8_t eaddr[ETHER_ADDR_LEN];
645 int error;
646 int i,addr_len;
647
648 callout_init(&sc->rtk_tick_ch);
649
650 /*
651 * Check EEPROM type 9346 or 9356.
652 */
653 if (rtk_read_eeprom(sc, RTK_EE_ID, RTK_EEADDR_LEN1) == 0x8129)
654 addr_len = RTK_EEADDR_LEN1;
655 else
656 addr_len = RTK_EEADDR_LEN0;
657
658 /*
659 * Get station address.
660 */
661 val = rtk_read_eeprom(sc, RTK_EE_EADDR0, addr_len);
662 eaddr[0] = val & 0xff;
663 eaddr[1] = val >> 8;
664 val = rtk_read_eeprom(sc, RTK_EE_EADDR1, addr_len);
665 eaddr[2] = val & 0xff;
666 eaddr[3] = val >> 8;
667 val = rtk_read_eeprom(sc, RTK_EE_EADDR2, addr_len);
668 eaddr[4] = val & 0xff;
669 eaddr[5] = val >> 8;
670
671 if ((error = bus_dmamem_alloc(sc->sc_dmat,
672 RTK_RXBUFLEN + 32, NBPG, 0, &sc->sc_dmaseg, 1, &sc->sc_dmanseg,
673 BUS_DMA_NOWAIT)) != 0) {
674 printf("%s: can't allocate recv buffer, error = %d\n",
675 sc->sc_dev.dv_xname, error);
676 goto fail_0;
677 }
678
679 if ((error = bus_dmamem_map(sc->sc_dmat, &sc->sc_dmaseg, sc->sc_dmanseg,
680 RTK_RXBUFLEN + 32, (caddr_t *)&sc->rtk_cdata.rtk_rx_buf,
681 BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
682 printf("%s: can't map recv buffer, error = %d\n",
683 sc->sc_dev.dv_xname, error);
684 goto fail_1;
685 }
686
687 /* Leave a few bytes before the start of the RX ring buffer. */
688 sc->rtk_cdata.rtk_rx_buf_ptr = sc->rtk_cdata.rtk_rx_buf;
689 sc->rtk_cdata.rtk_rx_buf += sizeof(u_int64_t);
690
691 if ((error = bus_dmamap_create(sc->sc_dmat,
692 RTK_RXBUFLEN + 32 - sizeof(u_int64_t), 1,
693 RTK_RXBUFLEN + 32 - sizeof(u_int64_t), 0, BUS_DMA_NOWAIT,
694 &sc->recv_dmamap)) != 0) {
695 printf("%s: can't create recv buffer DMA map, error = %d\n",
696 sc->sc_dev.dv_xname, error);
697 goto fail_2;
698 }
699
700 if ((error = bus_dmamap_load(sc->sc_dmat, sc->recv_dmamap,
701 sc->rtk_cdata.rtk_rx_buf, RTK_RXBUFLEN + 32 - sizeof(u_int64_t),
702 NULL, BUS_DMA_NOWAIT)) != 0) {
703 printf("%s: can't load recv buffer DMA map, error = %d\n",
704 sc->sc_dev.dv_xname, error);
705 goto fail_3;
706 }
707
708 for (i = 0; i < RTK_TX_LIST_CNT; i++)
709 if ((error = bus_dmamap_create(sc->sc_dmat,
710 MCLBYTES, 1, MCLBYTES, 0, BUS_DMA_NOWAIT,
711 &sc->snd_dmamap[i])) != 0) {
712 printf("%s: can't create snd buffer DMA map,"
713 " error = %d\n", sc->sc_dev.dv_xname, error);
714 goto fail_4;
715 }
716 /*
717 * From this point forward, the attachment cannot fail. A failure
718 * before this releases all resources thar may have been
719 * allocated.
720 */
721 sc->sc_flags |= RTK_ATTACHED;
722
723 /* Reset the adapter. */
724 rtk_reset(sc);
725
726 printf("%s: Ethernet address %s\n", sc->sc_dev.dv_xname,
727 ether_sprintf(eaddr));
728
729 ifp = &sc->ethercom.ec_if;
730 ifp->if_softc = sc;
731 bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
732 ifp->if_mtu = ETHERMTU;
733 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
734 ifp->if_ioctl = rtk_ioctl;
735 #if 0
736 ifp->if_output = ether_output;
737 #endif
738 ifp->if_start = rtk_start;
739 ifp->if_watchdog = rtk_watchdog;
740 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
741
742 /*
743 * Do ifmedia setup.
744 */
745 sc->mii.mii_ifp = ifp;
746 sc->mii.mii_readreg = rtk_phy_readreg;
747 sc->mii.mii_writereg = rtk_phy_writereg;
748 sc->mii.mii_statchg = rtk_phy_statchg;
749 ifmedia_init(&sc->mii.mii_media, 0, rtk_ifmedia_upd, rtk_ifmedia_sts);
750 mii_attach(&sc->sc_dev, &sc->mii, 0xffffffff,
751 MII_PHY_ANY, MII_OFFSET_ANY, 0);
752
753 /* Choose a default media. */
754 if (LIST_FIRST(&sc->mii.mii_phys) == NULL) {
755 ifmedia_add(&sc->mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
756 ifmedia_set(&sc->mii.mii_media, IFM_ETHER|IFM_NONE);
757 } else {
758 ifmedia_set(&sc->mii.mii_media, IFM_ETHER|IFM_AUTO);
759 }
760
761 /*
762 * Call MI attach routines.
763 */
764 if_attach(ifp);
765 ether_ifattach(ifp, eaddr);
766
767 #if NBPFILTER > 0
768 bpfattach(&sc->ethercom.ec_if.if_bpf, ifp, DLT_EN10MB,
769 sizeof(struct ether_header));
770 #endif
771 /*
772 * Make sure the interface is shutdown during reboot.
773 */
774 sc->sc_sdhook = shutdownhook_establish(rtk_shutdown, sc);
775 if (sc->sc_sdhook == NULL)
776 printf("%s: WARNING: unbale to establish shutdown hook\n",
777 sc->sc_dev.dv_xname);
778 /*
779 * Add a suspend hook to make sure we come back up after a
780 * resume.
781 */
782 sc->sc_powerhook = powerhook_establish(rtk_power, sc);
783 if (sc->sc_powerhook == NULL)
784 printf("%s: WARNING: unable to establish power hook\n",
785 sc->sc_dev.dv_xname);
786
787 return;
788 fail_4:
789 for (i = 0; i < RTK_TX_LIST_CNT; i++)
790 if (sc->snd_dmamap[i] != NULL)
791 bus_dmamap_destroy(sc->sc_dmat, sc->snd_dmamap[i]);
792 fail_3:
793 bus_dmamap_destroy(sc->sc_dmat, sc->recv_dmamap);
794 fail_2:
795 bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->rtk_cdata.rtk_rx_buf_ptr,
796 RTK_RXBUFLEN + 32 - sizeof(u_int64_t));
797 fail_1:
798 bus_dmamem_free(sc->sc_dmat, &sc->sc_dmaseg, sc->sc_dmanseg);
799 fail_0:
800 return;
801 }
802
803 /*
804 * Initialize the transmit descriptors.
805 */
806 STATIC int rtk_list_tx_init(sc)
807 struct rtk_softc *sc;
808 {
809 struct rtk_chain_data *cd;
810 int i;
811
812 cd = &sc->rtk_cdata;
813 for (i = 0; i < RTK_TX_LIST_CNT; i++) {
814 cd->rtk_tx_chain[i] = NULL;
815 CSR_WRITE_4(sc,
816 RTK_TXADDR0 + (i * sizeof(u_int32_t)), 0x0000000);
817 }
818
819 sc->rtk_cdata.cur_tx = 0;
820 sc->rtk_cdata.last_tx = 0;
821
822 return(0);
823 }
824
825 /*
826 * rtk_activate:
827 * Handle device activation/deactivation requests.
828 */
829 int
830 rtk_activate(self, act)
831 struct device *self;
832 enum devact act;
833 {
834 struct rtk_softc *sc = (void *) self;
835 int s, error = 0;
836
837 s = splnet();
838 switch (act) {
839 case DVACT_ACTIVATE:
840 error = EOPNOTSUPP;
841 break;
842 case DVACT_DEACTIVATE:
843 mii_activate(&sc->mii, act, MII_PHY_ANY, MII_OFFSET_ANY);
844 if_deactivate(&sc->ethercom.ec_if);
845 break;
846 }
847 splx(s);
848
849 return (error);
850 }
851
852 /*
853 * rtk_detach:
854 * Detach a rtk interface.
855 */
856 int
857 rtk_detach(sc)
858 struct rtk_softc *sc;
859 {
860 struct ifnet *ifp = &sc->ethercom.ec_if;
861 int i;
862
863 /*
864 * Succeed now if thereisn't any work to do.
865 */
866 if ((sc->sc_flags & RTK_ATTACHED) == 0)
867 return (0);
868
869 /* Unhook our tick handler. */
870 callout_stop(&sc->rtk_tick_ch);
871
872 /* Detach all PHYs. */
873 mii_detach(&sc->mii, MII_PHY_ANY, MII_OFFSET_ANY);
874
875 /* Delete all remaining media. */
876 ifmedia_delete_instance(&sc->mii.mii_media, IFM_INST_ANY);
877
878 #if NBPFILTER > 0
879 bpfdetach(ifp);
880 #endif
881 ether_ifdetach(ifp);
882 if_detach(ifp);
883
884 for (i = 0; i < RTK_TX_LIST_CNT; i++)
885 if (sc->snd_dmamap[i] != NULL)
886 bus_dmamap_destroy(sc->sc_dmat, sc->snd_dmamap[i]);
887 bus_dmamap_destroy(sc->sc_dmat, sc->recv_dmamap);
888 bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->rtk_cdata.rtk_rx_buf_ptr,
889 RTK_RXBUFLEN + 32 - sizeof(u_int64_t));
890
891 shutdownhook_disestablish(sc->sc_sdhook);
892 powerhook_disestablish(sc->sc_powerhook);
893
894 return (0);
895 }
896
897 /*
898 * rtk_enable:
899 * Enable the RTL81X9 chip.
900 */
901 int
902 rtk_enable(sc)
903 struct rtk_softc *sc;
904 {
905 if (RTK_IS_ENABLED(sc) == 0 && sc->sc_enable != NULL) {
906 if ((*sc->sc_enable)(sc) != 0) {
907 printf("%s: device enable failed\n",
908 sc->sc_dev.dv_xname);
909 return(EIO);
910 }
911 sc->sc_flags |= RTK_ENABLED;
912 }
913 return (0);
914 }
915
916 /*
917 * rtk_disable:
918 * Disable the RTL81X9 chip.
919 */
920 void
921 rtk_disable(sc)
922 struct rtk_softc *sc;
923 {
924 if (RTK_IS_ENABLED(sc) && sc->sc_disable != NULL) {
925 (*sc->sc_disable)(sc);
926 sc->sc_flags &= ~RTK_ENABLED;
927 }
928 }
929
930 /*
931 * rtk_power:
932 * Power management (suspend/resume) hook.
933 */
934 void
935 rtk_power(why, arg)
936 int why;
937 void *arg;
938 {
939 struct rtk_softc *sc = (void *) arg;
940 struct ifnet *ifp = &sc->ethercom.ec_if;
941 int s;
942
943 s = splnet();
944 if (why != PWR_RESUME) {
945 rtk_stop(sc);
946 if (sc->sc_power != NULL)
947 (*sc->sc_power)(sc, why);
948 } else if (ifp->if_flags & IFF_UP) {
949 if (sc->sc_power != NULL)
950 (*sc->sc_power)(sc, why);
951 rtk_init(sc);
952 }
953 splx(s);
954
955 }
956
957 /*
958 * A frame has been uploaded: pass the resulting mbuf chain up to
959 * the higher level protocols.
960 *
961 * You know there's something wrong with a PCI bus-master chip design
962 * when you have to use m_devget().
963 *
964 * The receive operation is badly documented in the datasheet, so I'll
965 * attempt to document it here. The driver provides a buffer area and
966 * places its base address in the RX buffer start address register.
967 * The chip then begins copying frames into the RX buffer. Each frame
968 * is preceeded by a 32-bit RX status word which specifies the length
969 * of the frame and certain other status bits. Each frame (starting with
970 * the status word) is also 32-bit aligned. The frame length is in the
971 * first 16 bits of the status word; the lower 15 bits correspond with
972 * the 'rx status register' mentioned in the datasheet.
973 *
974 * Note: to make the Alpha happy, the frame payload needs to be aligned
975 * on a 32-bit boundary. To achieve this, we cheat a bit by copying from
976 * the ring buffer starting at an address two bytes before the actual
977 * data location. We can then shave off the first two bytes using m_adj().
978 * The reason we do this is because m_devget() doesn't let us specify an
979 * offset into the mbuf storage space, so we have to artificially create
980 * one. The ring is allocated in such a way that there are a few unused
981 * bytes of space preceecing it so that it will be safe for us to do the
982 * 2-byte backstep even if reading from the ring at offset 0.
983 */
984 STATIC void rtk_rxeof(sc)
985 struct rtk_softc *sc;
986 {
987 struct mbuf *m;
988 struct ifnet *ifp;
989 int total_len = 0;
990 u_int32_t rxstat;
991 caddr_t rxbufpos;
992 int wrap = 0;
993 u_int16_t cur_rx;
994 u_int16_t limit;
995 u_int16_t rx_bytes = 0, max_bytes;
996
997 ifp = &sc->ethercom.ec_if;
998
999 cur_rx = (CSR_READ_2(sc, RTK_CURRXADDR) + 16) % RTK_RXBUFLEN;
1000
1001 /* Do not try to read past this point. */
1002 limit = CSR_READ_2(sc, RTK_CURRXBUF) % RTK_RXBUFLEN;
1003
1004 if (limit < cur_rx)
1005 max_bytes = (RTK_RXBUFLEN - cur_rx) + limit;
1006 else
1007 max_bytes = limit - cur_rx;
1008
1009 while((CSR_READ_1(sc, RTK_COMMAND) & RTK_CMD_EMPTY_RXBUF) == 0) {
1010 rxbufpos = sc->rtk_cdata.rtk_rx_buf + cur_rx;
1011 bus_dmamap_sync(sc->sc_dmat, sc->recv_dmamap, cur_rx,
1012 sizeof(u_int32_t *), BUS_DMASYNC_POSTREAD);
1013 rxstat = le32toh(*(u_int32_t *)rxbufpos);
1014 bus_dmamap_sync(sc->sc_dmat, sc->recv_dmamap, cur_rx,
1015 sizeof(u_int32_t *), BUS_DMASYNC_PREREAD);
1016
1017 /*
1018 * Here's a totally undocumented fact for you. When the
1019 * RealTek chip is in the process of copying a packet into
1020 * RAM for you, the length will be 0xfff0. If you spot a
1021 * packet header with this value, you need to stop. The
1022 * datasheet makes absolutely no mention of this and
1023 * RealTek should be shot for this.
1024 */
1025 if ((u_int16_t)(rxstat >> 16) == RTK_RXSTAT_UNFINISHED)
1026 break;
1027
1028 if (!(rxstat & RTK_RXSTAT_RXOK)) {
1029 ifp->if_ierrors++;
1030
1031 /*
1032 * submitted by:[netbsd-pcmcia:00484]
1033 * Takahiro Kambe <taca (at) sky.yamashina.kyoto.jp>
1034 * obtain from:
1035 * FreeBSD if_rl.c rev 1.24->1.25
1036 *
1037 */
1038 #if 0
1039 if (rxstat & (RTK_RXSTAT_BADSYM|RTK_RXSTAT_RUNT|
1040 RTK_RXSTAT_GIANT|RTK_RXSTAT_CRCERR|
1041 RTK_RXSTAT_ALIGNERR)) {
1042 CSR_WRITE_2(sc, RTK_COMMAND, RTK_CMD_TX_ENB);
1043 CSR_WRITE_2(sc, RTK_COMMAND, RTK_CMD_TX_ENB|
1044 RTK_CMD_RX_ENB);
1045 CSR_WRITE_4(sc, RTK_RXCFG, RTK_RXCFG_CONFIG);
1046 CSR_WRITE_4(sc, RTK_RXADDR,
1047 sc->recv_dmamap->dm_segs[0].ds_addr);
1048 CSR_WRITE_2(sc, RTK_CURRXADDR, cur_rx - 16);
1049 cur_rx = 0;
1050 }
1051 break;
1052 #else
1053 rtk_init(sc);
1054 return;
1055 #endif
1056 }
1057
1058 /* No errors; receive the packet. */
1059 total_len = rxstat >> 16;
1060 rx_bytes += total_len + 4;
1061
1062 /*
1063 * XXX The RealTek chip includes the CRC with every
1064 * received frame, and there's no way to turn this
1065 * behavior off (at least, I can't find anything in
1066 * the manual that explains how to do it) so we have
1067 * to trim off the CRC manually.
1068 */
1069 total_len -= ETHER_CRC_LEN;
1070
1071 /*
1072 * Avoid trying to read more bytes than we know
1073 * the chip has prepared for us.
1074 */
1075 if (rx_bytes > max_bytes)
1076 break;
1077
1078 bus_dmamap_sync(sc->sc_dmat, sc->recv_dmamap,
1079 cur_rx + sizeof(u_int32_t), total_len,
1080 BUS_DMASYNC_POSTREAD);
1081
1082 rxbufpos = sc->rtk_cdata.rtk_rx_buf +
1083 ((cur_rx + sizeof(u_int32_t)) % RTK_RXBUFLEN);
1084
1085 if (rxbufpos == (sc->rtk_cdata.rtk_rx_buf + RTK_RXBUFLEN))
1086 rxbufpos = sc->rtk_cdata.rtk_rx_buf;
1087
1088 wrap = (sc->rtk_cdata.rtk_rx_buf + RTK_RXBUFLEN) - rxbufpos;
1089
1090 if (total_len > wrap) {
1091 m = m_devget(rxbufpos - RTK_ETHER_ALIGN,
1092 wrap + RTK_ETHER_ALIGN, 0, ifp, NULL);
1093 if (m == NULL) {
1094 ifp->if_ierrors++;
1095 printf("%s: out of mbufs, tried to "
1096 "copy %d bytes\n", sc->sc_dev.dv_xname,
1097 wrap);
1098 } else {
1099 m_adj(m, RTK_ETHER_ALIGN);
1100 m_copyback(m, wrap, total_len - wrap,
1101 sc->rtk_cdata.rtk_rx_buf);
1102 }
1103 cur_rx = (total_len - wrap + ETHER_CRC_LEN);
1104 } else {
1105 m = m_devget(rxbufpos - RTK_ETHER_ALIGN,
1106 total_len + RTK_ETHER_ALIGN, 0, ifp, NULL);
1107 if (m == NULL) {
1108 ifp->if_ierrors++;
1109 printf("%s: out of mbufs, tried to "
1110 "copy %d bytes\n", sc->sc_dev.dv_xname,
1111 total_len);
1112 } else
1113 m_adj(m, RTK_ETHER_ALIGN);
1114 cur_rx += total_len + 4 + ETHER_CRC_LEN;
1115 }
1116
1117 /*
1118 * Round up to 32-bit boundary.
1119 */
1120 cur_rx = (cur_rx + 3) & ~3;
1121 CSR_WRITE_2(sc, RTK_CURRXADDR, cur_rx - 16);
1122
1123 if (m == NULL)
1124 continue;
1125
1126 ifp->if_ipackets++;
1127
1128 #if NBPFILTER > 0
1129 /*
1130 * Handle BPF listeners. Let the BPF user see the packet, but
1131 * don't pass it up to the ether_input() layer unless it's
1132 * a broadcast packet, multicast packet, matches our ethernet
1133 * address or the interface is in promiscuous mode.
1134 */
1135 if (ifp->if_bpf)
1136 bpf_mtap(ifp->if_bpf, m);
1137 #endif
1138 /* pass it on. */
1139 (*ifp->if_input)(ifp, m);
1140
1141 bus_dmamap_sync(sc->sc_dmat, sc->recv_dmamap,
1142 cur_rx + sizeof(u_int32_t),
1143 total_len, BUS_DMASYNC_PREREAD);
1144 }
1145
1146 return;
1147 }
1148
1149 /*
1150 * A frame was downloaded to the chip. It's safe for us to clean up
1151 * the list buffers.
1152 */
1153 STATIC void rtk_txeof(sc)
1154 struct rtk_softc *sc;
1155 {
1156 struct ifnet *ifp;
1157 u_int32_t txstat;
1158
1159 ifp = &sc->ethercom.ec_if;
1160
1161 /* Clear the timeout timer. */
1162 ifp->if_timer = 0;
1163
1164 /*
1165 * Go through our tx list and free mbufs for those
1166 * frames that have been uploaded.
1167 */
1168 do {
1169 txstat = CSR_READ_4(sc, RTK_LAST_TXSTAT(sc));
1170 if (!(txstat & (RTK_TXSTAT_TX_OK|
1171 RTK_TXSTAT_TX_UNDERRUN|RTK_TXSTAT_TXABRT)))
1172 break;
1173
1174 bus_dmamap_sync(sc->sc_dmat,
1175 sc->snd_dmamap[sc->rtk_cdata.last_tx], 0,
1176 sc->snd_dmamap[sc->rtk_cdata.last_tx]->dm_mapsize,
1177 BUS_DMASYNC_POSTWRITE);
1178 bus_dmamap_unload(sc->sc_dmat,
1179 sc->snd_dmamap[sc->rtk_cdata.last_tx]);
1180 m_freem(RTK_LAST_TXMBUF(sc));
1181 RTK_LAST_TXMBUF(sc) = NULL;
1182
1183 ifp->if_collisions += (txstat & RTK_TXSTAT_COLLCNT) >> 24;
1184
1185 if (txstat & RTK_TXSTAT_TX_OK)
1186 ifp->if_opackets++;
1187 else {
1188 ifp->if_oerrors++;
1189 if ((txstat & RTK_TXSTAT_TXABRT) ||
1190 (txstat & RTK_TXSTAT_OUTOFWIN))
1191 CSR_WRITE_4(sc, RTK_TXCFG, RTK_TXCFG_CONFIG);
1192 }
1193 RTK_INC(sc->rtk_cdata.last_tx);
1194 ifp->if_flags &= ~IFF_OACTIVE;
1195 } while (sc->rtk_cdata.last_tx != sc->rtk_cdata.cur_tx);
1196
1197 return;
1198 }
1199
1200 int rtk_intr(arg)
1201 void *arg;
1202 {
1203 struct rtk_softc *sc;
1204 struct ifnet *ifp;
1205 u_int16_t status;
1206 int handled = 0;
1207
1208 sc = arg;
1209 ifp = &sc->ethercom.ec_if;
1210
1211 /* Disable interrupts. */
1212 CSR_WRITE_2(sc, RTK_IMR, 0x0000);
1213
1214 for (;;) {
1215
1216 status = CSR_READ_2(sc, RTK_ISR);
1217 if (status)
1218 CSR_WRITE_2(sc, RTK_ISR, status);
1219
1220 handled = 1;
1221
1222 if ((status & RTK_INTRS) == 0)
1223 break;
1224
1225 if (status & RTK_ISR_RX_OK)
1226 rtk_rxeof(sc);
1227
1228 if (status & RTK_ISR_RX_ERR)
1229 rtk_rxeof(sc);
1230
1231 if ((status & RTK_ISR_TX_OK) || (status & RTK_ISR_TX_ERR))
1232 rtk_txeof(sc);
1233
1234 if (status & RTK_ISR_SYSTEM_ERR) {
1235 rtk_reset(sc);
1236 rtk_init(sc);
1237 }
1238
1239 }
1240
1241 /* Re-enable interrupts. */
1242 CSR_WRITE_2(sc, RTK_IMR, RTK_INTRS);
1243
1244 if (ifp->if_snd.ifq_head != NULL) {
1245 rtk_start(ifp);
1246 }
1247
1248 return (handled);
1249 }
1250
1251 /*
1252 * Main transmit routine.
1253 */
1254
1255 STATIC void rtk_start(ifp)
1256 struct ifnet *ifp;
1257 {
1258 struct rtk_softc *sc;
1259 struct mbuf *m_head = NULL, *m_new;
1260 int error, idx, len;
1261
1262 sc = ifp->if_softc;
1263
1264 while(RTK_CUR_TXMBUF(sc) == NULL) {
1265 IF_DEQUEUE(&ifp->if_snd, m_head);
1266 if (m_head == NULL)
1267 break;
1268
1269 idx = sc->rtk_cdata.cur_tx;
1270
1271 /*
1272 * Load the DMA map. If this fails, the packet didn't
1273 * fit in one DMA segment, and we need to copy. Note,
1274 * the packet must also be aligned.
1275 */
1276 if ((mtod(m_head, bus_addr_t) & 3) != 0 ||
1277 bus_dmamap_load_mbuf(sc->sc_dmat, sc->snd_dmamap[idx],
1278 m_head, BUS_DMA_NOWAIT) != 0) {
1279 MGETHDR(m_new, M_DONTWAIT, MT_DATA);
1280 if (m_new == NULL) {
1281 printf("%s: unable to allocate Tx mbuf\n",
1282 sc->sc_dev.dv_xname);
1283 IF_PREPEND(&ifp->if_snd, m_new);
1284 break;
1285 }
1286 if (m_head->m_pkthdr.len > MHLEN) {
1287 MCLGET(m_new, M_DONTWAIT);
1288 if ((m_new->m_flags & M_EXT) == 0) {
1289 printf("%s: unable to allocate Tx "
1290 "cluster\n", sc->sc_dev.dv_xname);
1291 m_freem(m_new);
1292 IF_PREPEND(&ifp->if_snd, m_head);
1293 break;
1294 }
1295 }
1296 m_copydata(m_head, 0, m_head->m_pkthdr.len,
1297 mtod(m_new, caddr_t));
1298 m_new->m_pkthdr.len = m_new->m_len =
1299 m_head->m_pkthdr.len;
1300 m_freem(m_head);
1301 m_head = m_new;
1302 error = bus_dmamap_load_mbuf(sc->sc_dmat,
1303 sc->snd_dmamap[idx], m_head, BUS_DMA_NOWAIT);
1304 if (error) {
1305 printf("%s: unable to load Tx buffer, "
1306 "error = %d\n", sc->sc_dev.dv_xname, error);
1307 IF_PREPEND(&ifp->if_snd, m_head);
1308 break;
1309 }
1310 }
1311
1312 RTK_CUR_TXMBUF(sc) = m_head;
1313
1314 #if NBPFILTER > 0
1315 /*
1316 * If there's a BPF listener, bounce a copy of this frame
1317 * to him.
1318 */
1319 if (ifp->if_bpf)
1320 bpf_mtap(ifp->if_bpf, RTK_CUR_TXMBUF(sc));
1321 #endif
1322 /*
1323 * Transmit the frame.
1324 */
1325 bus_dmamap_sync(sc->sc_dmat,
1326 sc->snd_dmamap[idx], 0, sc->snd_dmamap[idx]->dm_mapsize,
1327 BUS_DMASYNC_PREWRITE);
1328
1329 len = sc->snd_dmamap[idx]->dm_segs[0].ds_len;
1330 if (len < (ETHER_MIN_LEN - ETHER_CRC_LEN))
1331 len = (ETHER_MIN_LEN - ETHER_CRC_LEN);
1332
1333 CSR_WRITE_4(sc, RTK_CUR_TXADDR(sc),
1334 sc->snd_dmamap[idx]->dm_segs[0].ds_addr);
1335 CSR_WRITE_4(sc, RTK_CUR_TXSTAT(sc), RTK_TX_EARLYTHRESH | len);
1336
1337 RTK_INC(sc->rtk_cdata.cur_tx);
1338 }
1339
1340 /*
1341 * We broke out of the loop because all our TX slots are
1342 * full. Mark the NIC as busy until it drains some of the
1343 * packets from the queue.
1344 */
1345 if (RTK_CUR_TXMBUF(sc) != NULL)
1346 ifp->if_flags |= IFF_OACTIVE;
1347
1348 /*
1349 * Set a timeout in case the chip goes out to lunch.
1350 */
1351 ifp->if_timer = 5;
1352
1353 return;
1354 }
1355
1356 STATIC void rtk_init(xsc)
1357 void *xsc;
1358 {
1359 struct rtk_softc *sc = xsc;
1360 struct ifnet *ifp = &sc->ethercom.ec_if;
1361 int s, i;
1362 u_int32_t rxcfg;
1363
1364 s = splnet();
1365
1366 /*
1367 * Cancel pending I/O and free all RX/TX buffers.
1368 */
1369 rtk_stop(sc);
1370
1371 /* Init our MAC address */
1372 for (i = 0; i < ETHER_ADDR_LEN; i++) {
1373 CSR_WRITE_1(sc, RTK_IDR0 + i, LLADDR(ifp->if_sadl)[i]);
1374 }
1375
1376 /* Init the RX buffer pointer register. */
1377 bus_dmamap_sync(sc->sc_dmat, sc->recv_dmamap, 0,
1378 sc->recv_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
1379 CSR_WRITE_4(sc, RTK_RXADDR, sc->recv_dmamap->dm_segs[0].ds_addr);
1380
1381 /* Init TX descriptors. */
1382 rtk_list_tx_init(sc);
1383
1384 /*
1385 * Enable transmit and receive.
1386 */
1387 CSR_WRITE_1(sc, RTK_COMMAND, RTK_CMD_TX_ENB|RTK_CMD_RX_ENB);
1388
1389 /*
1390 * Set the initial TX and RX configuration.
1391 */
1392 CSR_WRITE_4(sc, RTK_TXCFG, RTK_TXCFG_CONFIG);
1393 CSR_WRITE_4(sc, RTK_RXCFG, RTK_RXCFG_CONFIG);
1394
1395 /* Set the individual bit to receive frames for this host only. */
1396 rxcfg = CSR_READ_4(sc, RTK_RXCFG);
1397 rxcfg |= RTK_RXCFG_RX_INDIV;
1398
1399 /* If we want promiscuous mode, set the allframes bit. */
1400 if (ifp->if_flags & IFF_PROMISC) {
1401 rxcfg |= RTK_RXCFG_RX_ALLPHYS;
1402 CSR_WRITE_4(sc, RTK_RXCFG, rxcfg);
1403 } else {
1404 rxcfg &= ~RTK_RXCFG_RX_ALLPHYS;
1405 CSR_WRITE_4(sc, RTK_RXCFG, rxcfg);
1406 }
1407
1408 /*
1409 * Set capture broadcast bit to capture broadcast frames.
1410 */
1411 if (ifp->if_flags & IFF_BROADCAST) {
1412 rxcfg |= RTK_RXCFG_RX_BROAD;
1413 CSR_WRITE_4(sc, RTK_RXCFG, rxcfg);
1414 } else {
1415 rxcfg &= ~RTK_RXCFG_RX_BROAD;
1416 CSR_WRITE_4(sc, RTK_RXCFG, rxcfg);
1417 }
1418
1419 /*
1420 * Program the multicast filter, if necessary.
1421 */
1422 rtk_setmulti(sc);
1423
1424 /*
1425 * Enable interrupts.
1426 */
1427 CSR_WRITE_2(sc, RTK_IMR, RTK_INTRS);
1428
1429 /* Start RX/TX process. */
1430 CSR_WRITE_4(sc, RTK_MISSEDPKT, 0);
1431
1432 /* Enable receiver and transmitter. */
1433 CSR_WRITE_1(sc, RTK_COMMAND, RTK_CMD_TX_ENB|RTK_CMD_RX_ENB);
1434
1435 CSR_WRITE_1(sc, RTK_CFG1, RTK_CFG1_DRVLOAD|RTK_CFG1_FULLDUPLEX);
1436
1437 /*
1438 * Set current media.
1439 */
1440 mii_mediachg(&sc->mii);
1441
1442 ifp->if_flags |= IFF_RUNNING;
1443 ifp->if_flags &= ~IFF_OACTIVE;
1444
1445 (void)splx(s);
1446
1447 callout_reset(&sc->rtk_tick_ch, hz, rtk_tick, sc);
1448 }
1449
1450 /*
1451 * Set media options.
1452 */
1453 STATIC int rtk_ifmedia_upd(ifp)
1454 struct ifnet *ifp;
1455 {
1456 struct rtk_softc *sc;
1457
1458 sc = ifp->if_softc;
1459
1460 return (mii_mediachg(&sc->mii));
1461 }
1462
1463 /*
1464 * Report current media status.
1465 */
1466 STATIC void rtk_ifmedia_sts(ifp, ifmr)
1467 struct ifnet *ifp;
1468 struct ifmediareq *ifmr;
1469 {
1470 struct rtk_softc *sc;
1471
1472 sc = ifp->if_softc;
1473
1474 mii_pollstat(&sc->mii);
1475 ifmr->ifm_status = sc->mii.mii_media_status;
1476 ifmr->ifm_active = sc->mii.mii_media_active;
1477 }
1478
1479 STATIC int
1480 rtk_ether_ioctl(ifp, cmd, data)
1481 struct ifnet *ifp;
1482 u_long cmd;
1483 caddr_t data;
1484 {
1485 struct ifaddr *ifa = (struct ifaddr *) data;
1486 struct rtk_softc *sc = ifp->if_softc;
1487 int error = 0;
1488
1489 switch (cmd) {
1490 case SIOCSIFADDR:
1491 if ((error = rtk_enable(sc)) != 0)
1492 break;
1493 ifp->if_flags |= IFF_UP;
1494
1495 switch (ifa->ifa_addr->sa_family) {
1496 #ifdef INET
1497 case AF_INET:
1498 rtk_init(sc);
1499 arp_ifinit(ifp, ifa);
1500 break;
1501 #endif
1502 #ifdef NS
1503 case AF_NS:
1504 {
1505 struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
1506
1507 if (ns_nullhost(*ina))
1508 ina->x_host = *(union ns_host *)
1509 LLADDR(ifp->if_sadl);
1510 else
1511 bcopy(ina->x_host.c_host, LLADDR(ifp->if_sadl),
1512 ifp->if_addrlen);
1513 /* Set new address. */
1514 rtk_init(sc);
1515 break;
1516 }
1517 #endif
1518 default:
1519 rtk_init(sc);
1520 break;
1521 }
1522 break;
1523
1524 default:
1525 return (EINVAL);
1526 }
1527
1528 return (error);
1529 }
1530
1531 STATIC int rtk_ioctl(ifp, command, data)
1532 struct ifnet *ifp;
1533 u_long command;
1534 caddr_t data;
1535 {
1536 struct rtk_softc *sc = ifp->if_softc;
1537 struct ifreq *ifr = (struct ifreq *) data;
1538 int s, error = 0;
1539
1540 s = splnet();
1541
1542 switch (command) {
1543 case SIOCSIFADDR:
1544 case SIOCGIFADDR:
1545 case SIOCSIFMTU:
1546 error = rtk_ether_ioctl(ifp, command, data);
1547 break;
1548 case SIOCSIFFLAGS:
1549 if (ifp->if_flags & IFF_UP) {
1550 if ((error = rtk_enable(sc)) != 0)
1551 break;
1552 rtk_init(sc);
1553 } else {
1554 if (ifp->if_flags & IFF_RUNNING) {
1555 rtk_stop(sc);
1556 rtk_disable(sc);
1557 }
1558 }
1559 error = 0;
1560 break;
1561 case SIOCADDMULTI:
1562 case SIOCDELMULTI:
1563 error = (command == SIOCADDMULTI) ?
1564 ether_addmulti(ifr, &sc->ethercom) :
1565 ether_delmulti(ifr, &sc->ethercom);
1566
1567 if (error == ENETRESET) {
1568 /*
1569 * Multicast list has changed; set the hardware filter
1570 * accordingly.
1571 */
1572 rtk_setmulti(sc);
1573 error = 0;
1574 }
1575 break;
1576 case SIOCGIFMEDIA:
1577 case SIOCSIFMEDIA:
1578 error = ifmedia_ioctl(ifp, ifr, &sc->mii.mii_media, command);
1579 break;
1580 default:
1581 error = EINVAL;
1582 break;
1583 }
1584
1585 splx(s);
1586
1587 return(error);
1588 }
1589
1590 STATIC void rtk_watchdog(ifp)
1591 struct ifnet *ifp;
1592 {
1593 struct rtk_softc *sc;
1594
1595 sc = ifp->if_softc;
1596
1597 printf("%s: watchdog timeout\n", sc->sc_dev.dv_xname);
1598 ifp->if_oerrors++;
1599 rtk_txeof(sc);
1600 rtk_rxeof(sc);
1601 rtk_init(sc);
1602
1603 return;
1604 }
1605
1606 /*
1607 * Stop the adapter and free any mbufs allocated to the
1608 * RX and TX lists.
1609 */
1610 STATIC void rtk_stop(sc)
1611 struct rtk_softc *sc;
1612 {
1613 int i;
1614 struct ifnet *ifp;
1615
1616 ifp = &sc->ethercom.ec_if;
1617 ifp->if_timer = 0;
1618
1619 callout_stop(&sc->rtk_tick_ch);
1620
1621 mii_down(&sc->mii);
1622
1623 CSR_WRITE_1(sc, RTK_COMMAND, 0x00);
1624 CSR_WRITE_2(sc, RTK_IMR, 0x0000);
1625
1626 /*
1627 * Free the TX list buffers.
1628 */
1629 for (i = 0; i < RTK_TX_LIST_CNT; i++) {
1630 if (sc->rtk_cdata.rtk_tx_chain[i] != NULL) {
1631 bus_dmamap_unload(sc->sc_dmat, sc->snd_dmamap[i]);
1632 m_freem(sc->rtk_cdata.rtk_tx_chain[i]);
1633 sc->rtk_cdata.rtk_tx_chain[i] = NULL;
1634 CSR_WRITE_4(sc, RTK_TXADDR0 + i, 0x0000000);
1635 }
1636 }
1637
1638 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1639
1640 return;
1641 }
1642
1643 /*
1644 * Stop all chip I/O so that the kernel's probe routines don't
1645 * get confused by errant DMAs when rebooting.
1646 */
1647 STATIC void rtk_shutdown(vsc)
1648 void *vsc;
1649 {
1650 struct rtk_softc *sc = (struct rtk_softc *)vsc;
1651
1652 rtk_stop(sc);
1653
1654 return;
1655 }
1656
1657 STATIC void
1658 rtk_tick(arg)
1659 void *arg;
1660 {
1661 struct rtk_softc *sc = arg;
1662 int s = splnet();
1663
1664 mii_tick(&sc->mii);
1665 splx(s);
1666
1667 callout_reset(&sc->rtk_tick_ch, hz, rtk_tick, sc);
1668 }
1669