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