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