rtl81x9.c revision 1.29 1 /* $NetBSD: rtl81x9.c,v 1.29 2001/02/01 04:45:17 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 <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_PROMISC) {
568 allmulti:
569 ifp->if_flags |= IFF_ALLMULTI;
570 rxfilt |= RTK_RXCFG_RX_MULTI;
571 CSR_WRITE_4(sc, RTK_RXCFG, rxfilt);
572 CSR_WRITE_4(sc, RTK_MAR0, 0xFFFFFFFF);
573 CSR_WRITE_4(sc, RTK_MAR4, 0xFFFFFFFF);
574 return;
575 }
576
577 /* first, zot all the existing hash bits */
578 CSR_WRITE_4(sc, RTK_MAR0, 0);
579 CSR_WRITE_4(sc, RTK_MAR4, 0);
580
581 /* now program new ones */
582 ETHER_FIRST_MULTI(step, &sc->ethercom, enm);
583 while (enm != NULL) {
584 if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
585 ETHER_ADDR_LEN) != 0)
586 goto allmulti;
587
588 h = rtk_calchash(enm->enm_addrlo);
589 if (h < 32)
590 hashes[0] |= (1 << h);
591 else
592 hashes[1] |= (1 << (h - 32));
593 mcnt++;
594 ETHER_NEXT_MULTI(step, enm);
595 }
596
597 ifp->if_flags &= ~IFF_ALLMULTI;
598
599 if (mcnt)
600 rxfilt |= RTK_RXCFG_RX_MULTI;
601 else
602 rxfilt &= ~RTK_RXCFG_RX_MULTI;
603
604 CSR_WRITE_4(sc, RTK_RXCFG, rxfilt);
605 CSR_WRITE_4(sc, RTK_MAR0, hashes[0]);
606 CSR_WRITE_4(sc, RTK_MAR4, hashes[1]);
607 }
608
609 void rtk_reset(sc)
610 struct rtk_softc *sc;
611 {
612 int i;
613
614 CSR_WRITE_1(sc, RTK_COMMAND, RTK_CMD_RESET);
615
616 for (i = 0; i < RTK_TIMEOUT; i++) {
617 DELAY(10);
618 if ((CSR_READ_1(sc, RTK_COMMAND) & RTK_CMD_RESET) == 0)
619 break;
620 }
621 if (i == RTK_TIMEOUT)
622 printf("%s: reset never completed!\n", sc->sc_dev.dv_xname);
623 }
624
625 /*
626 * Attach the interface. Allocate softc structures, do ifmedia
627 * setup and ethernet/BPF attach.
628 */
629 void
630 rtk_attach(sc)
631 struct rtk_softc *sc;
632 {
633 struct ifnet *ifp;
634 u_int16_t val;
635 u_int8_t eaddr[ETHER_ADDR_LEN];
636 int error;
637 int i, addr_len;
638
639 callout_init(&sc->rtk_tick_ch);
640
641 /*
642 * Check EEPROM type 9346 or 9356.
643 */
644 if (rtk_read_eeprom(sc, RTK_EE_ID, RTK_EEADDR_LEN1) == 0x8129)
645 addr_len = RTK_EEADDR_LEN1;
646 else
647 addr_len = RTK_EEADDR_LEN0;
648
649 /*
650 * Get station address.
651 */
652 val = rtk_read_eeprom(sc, RTK_EE_EADDR0, addr_len);
653 eaddr[0] = val & 0xff;
654 eaddr[1] = val >> 8;
655 val = rtk_read_eeprom(sc, RTK_EE_EADDR1, addr_len);
656 eaddr[2] = val & 0xff;
657 eaddr[3] = val >> 8;
658 val = rtk_read_eeprom(sc, RTK_EE_EADDR2, addr_len);
659 eaddr[4] = val & 0xff;
660 eaddr[5] = val >> 8;
661
662 if ((error = bus_dmamem_alloc(sc->sc_dmat,
663 RTK_RXBUFLEN + 16, PAGE_SIZE, 0, &sc->sc_dmaseg, 1, &sc->sc_dmanseg,
664 BUS_DMA_NOWAIT)) != 0) {
665 printf("%s: can't allocate recv buffer, error = %d\n",
666 sc->sc_dev.dv_xname, error);
667 goto fail_0;
668 }
669
670 if ((error = bus_dmamem_map(sc->sc_dmat, &sc->sc_dmaseg, sc->sc_dmanseg,
671 RTK_RXBUFLEN + 16, (caddr_t *)&sc->rtk_cdata.rtk_rx_buf,
672 BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
673 printf("%s: can't map recv buffer, error = %d\n",
674 sc->sc_dev.dv_xname, error);
675 goto fail_1;
676 }
677
678 if ((error = bus_dmamap_create(sc->sc_dmat,
679 RTK_RXBUFLEN + 16, 1, RTK_RXBUFLEN + 16, 0, BUS_DMA_NOWAIT,
680 &sc->recv_dmamap)) != 0) {
681 printf("%s: can't create recv buffer DMA map, error = %d\n",
682 sc->sc_dev.dv_xname, error);
683 goto fail_2;
684 }
685
686 if ((error = bus_dmamap_load(sc->sc_dmat, sc->recv_dmamap,
687 sc->rtk_cdata.rtk_rx_buf, RTK_RXBUFLEN + 16,
688 NULL, BUS_DMA_NOWAIT)) != 0) {
689 printf("%s: can't load recv buffer DMA map, error = %d\n",
690 sc->sc_dev.dv_xname, error);
691 goto fail_3;
692 }
693
694 for (i = 0; i < RTK_TX_LIST_CNT; i++)
695 if ((error = bus_dmamap_create(sc->sc_dmat,
696 MCLBYTES, 1, MCLBYTES, 0, BUS_DMA_NOWAIT,
697 &sc->snd_dmamap[i])) != 0) {
698 printf("%s: can't create snd buffer DMA map,"
699 " error = %d\n", sc->sc_dev.dv_xname, error);
700 goto fail_4;
701 }
702 /*
703 * From this point forward, the attachment cannot fail. A failure
704 * before this releases all resources thar may have been
705 * allocated.
706 */
707 sc->sc_flags |= RTK_ATTACHED;
708
709 /* Reset the adapter. */
710 rtk_reset(sc);
711
712 printf("%s: Ethernet address %s\n",
713 sc->sc_dev.dv_xname, ether_sprintf(eaddr));
714
715 ifp = &sc->ethercom.ec_if;
716 ifp->if_softc = sc;
717 bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
718 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
719 ifp->if_ioctl = rtk_ioctl;
720 ifp->if_start = rtk_start;
721 ifp->if_watchdog = rtk_watchdog;
722 ifp->if_init = rtk_init;
723 ifp->if_stop = rtk_stop;
724 IFQ_SET_READY(&ifp->if_snd);
725
726 /*
727 * Do ifmedia setup.
728 */
729 sc->mii.mii_ifp = ifp;
730 sc->mii.mii_readreg = rtk_phy_readreg;
731 sc->mii.mii_writereg = rtk_phy_writereg;
732 sc->mii.mii_statchg = rtk_phy_statchg;
733 ifmedia_init(&sc->mii.mii_media, 0, rtk_ifmedia_upd, rtk_ifmedia_sts);
734 mii_attach(&sc->sc_dev, &sc->mii, 0xffffffff,
735 MII_PHY_ANY, MII_OFFSET_ANY, 0);
736
737 /* Choose a default media. */
738 if (LIST_FIRST(&sc->mii.mii_phys) == NULL) {
739 ifmedia_add(&sc->mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
740 ifmedia_set(&sc->mii.mii_media, IFM_ETHER|IFM_NONE);
741 } else {
742 ifmedia_set(&sc->mii.mii_media, IFM_ETHER|IFM_AUTO);
743 }
744
745 /*
746 * Call MI attach routines.
747 */
748 if_attach(ifp);
749 ether_ifattach(ifp, eaddr);
750
751 /*
752 * Make sure the interface is shutdown during reboot.
753 */
754 sc->sc_sdhook = shutdownhook_establish(rtk_shutdown, sc);
755 if (sc->sc_sdhook == NULL)
756 printf("%s: WARNING: unbale to establish shutdown hook\n",
757 sc->sc_dev.dv_xname);
758 /*
759 * Add a suspend hook to make sure we come back up after a
760 * resume.
761 */
762 sc->sc_powerhook = powerhook_establish(rtk_power, sc);
763 if (sc->sc_powerhook == NULL)
764 printf("%s: WARNING: unable to establish power hook\n",
765 sc->sc_dev.dv_xname);
766
767 return;
768 fail_4:
769 for (i = 0; i < RTK_TX_LIST_CNT; i++)
770 if (sc->snd_dmamap[i] != NULL)
771 bus_dmamap_destroy(sc->sc_dmat, sc->snd_dmamap[i]);
772 fail_3:
773 bus_dmamap_destroy(sc->sc_dmat, sc->recv_dmamap);
774 fail_2:
775 bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->rtk_cdata.rtk_rx_buf,
776 RTK_RXBUFLEN + 16);
777 fail_1:
778 bus_dmamem_free(sc->sc_dmat, &sc->sc_dmaseg, sc->sc_dmanseg);
779 fail_0:
780 return;
781 }
782
783 /*
784 * Initialize the transmit descriptors.
785 */
786 STATIC int rtk_list_tx_init(sc)
787 struct rtk_softc *sc;
788 {
789 struct rtk_chain_data *cd;
790 int i;
791
792 cd = &sc->rtk_cdata;
793 for (i = 0; i < RTK_TX_LIST_CNT; i++) {
794 cd->rtk_tx_chain[i] = NULL;
795 CSR_WRITE_4(sc,
796 RTK_TXADDR0 + (i * sizeof(u_int32_t)), 0x0000000);
797 }
798
799 sc->rtk_cdata.cur_tx = 0;
800 sc->rtk_cdata.last_tx = 0;
801
802 return (0);
803 }
804
805 /*
806 * rtk_activate:
807 * Handle device activation/deactivation requests.
808 */
809 int
810 rtk_activate(self, act)
811 struct device *self;
812 enum devact act;
813 {
814 struct rtk_softc *sc = (void *) self;
815 int s, error = 0;
816
817 s = splnet();
818 switch (act) {
819 case DVACT_ACTIVATE:
820 error = EOPNOTSUPP;
821 break;
822 case DVACT_DEACTIVATE:
823 mii_activate(&sc->mii, act, MII_PHY_ANY, MII_OFFSET_ANY);
824 if_deactivate(&sc->ethercom.ec_if);
825 break;
826 }
827 splx(s);
828
829 return (error);
830 }
831
832 /*
833 * rtk_detach:
834 * Detach a rtk interface.
835 */
836 int
837 rtk_detach(sc)
838 struct rtk_softc *sc;
839 {
840 struct ifnet *ifp = &sc->ethercom.ec_if;
841 int i;
842
843 /*
844 * Succeed now if thereisn't any work to do.
845 */
846 if ((sc->sc_flags & RTK_ATTACHED) == 0)
847 return (0);
848
849 /* Unhook our tick handler. */
850 callout_stop(&sc->rtk_tick_ch);
851
852 /* Detach all PHYs. */
853 mii_detach(&sc->mii, MII_PHY_ANY, MII_OFFSET_ANY);
854
855 /* Delete all remaining media. */
856 ifmedia_delete_instance(&sc->mii.mii_media, IFM_INST_ANY);
857
858 ether_ifdetach(ifp);
859 if_detach(ifp);
860
861 for (i = 0; i < RTK_TX_LIST_CNT; i++)
862 if (sc->snd_dmamap[i] != NULL)
863 bus_dmamap_destroy(sc->sc_dmat, sc->snd_dmamap[i]);
864 bus_dmamap_destroy(sc->sc_dmat, sc->recv_dmamap);
865 bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->rtk_cdata.rtk_rx_buf,
866 RTK_RXBUFLEN + 16);
867 bus_dmamem_free(sc->sc_dmat, &sc->sc_dmaseg, sc->sc_dmanseg);
868
869 shutdownhook_disestablish(sc->sc_sdhook);
870 powerhook_disestablish(sc->sc_powerhook);
871
872 return (0);
873 }
874
875 /*
876 * rtk_enable:
877 * Enable the RTL81X9 chip.
878 */
879 int
880 rtk_enable(sc)
881 struct rtk_softc *sc;
882 {
883
884 if (RTK_IS_ENABLED(sc) == 0 && sc->sc_enable != NULL) {
885 if ((*sc->sc_enable)(sc) != 0) {
886 printf("%s: device enable failed\n",
887 sc->sc_dev.dv_xname);
888 return (EIO);
889 }
890 sc->sc_flags |= RTK_ENABLED;
891 }
892 return (0);
893 }
894
895 /*
896 * rtk_disable:
897 * Disable the RTL81X9 chip.
898 */
899 void
900 rtk_disable(sc)
901 struct rtk_softc *sc;
902 {
903
904 if (RTK_IS_ENABLED(sc) && sc->sc_disable != NULL) {
905 (*sc->sc_disable)(sc);
906 sc->sc_flags &= ~RTK_ENABLED;
907 }
908 }
909
910 /*
911 * rtk_power:
912 * Power management (suspend/resume) hook.
913 */
914 void
915 rtk_power(why, arg)
916 int why;
917 void *arg;
918 {
919 struct rtk_softc *sc = (void *) arg;
920 struct ifnet *ifp = &sc->ethercom.ec_if;
921 int s;
922
923 s = splnet();
924 switch (why) {
925 case PWR_SUSPEND:
926 case PWR_STANDBY:
927 rtk_stop(ifp, 0);
928 if (sc->sc_power != NULL)
929 (*sc->sc_power)(sc, why);
930 break;
931 case PWR_RESUME:
932 if (ifp->if_flags & IFF_UP) {
933 if (sc->sc_power != NULL)
934 (*sc->sc_power)(sc, why);
935 rtk_init(ifp);
936 }
937 break;
938 case PWR_SOFTSUSPEND:
939 case PWR_SOFTSTANDBY:
940 case PWR_SOFTRESUME:
941 break;
942 }
943 splx(s);
944 }
945
946 /*
947 * A frame has been uploaded: pass the resulting mbuf chain up to
948 * the higher level protocols.
949 *
950 * You know there's something wrong with a PCI bus-master chip design.
951 *
952 * The receive operation is badly documented in the datasheet, so I'll
953 * attempt to document it here. The driver provides a buffer area and
954 * places its base address in the RX buffer start address register.
955 * The chip then begins copying frames into the RX buffer. Each frame
956 * is preceeded by a 32-bit RX status word which specifies the length
957 * of the frame and certain other status bits. Each frame (starting with
958 * the status word) is also 32-bit aligned. The frame length is in the
959 * first 16 bits of the status word; the lower 15 bits correspond with
960 * the 'rx status register' mentioned in the datasheet.
961 *
962 * Note: to make the Alpha happy, the frame payload needs to be aligned
963 * on a 32-bit boundary. To achieve this, we copy the data to mbuf
964 * shifted forward 2 bytes.
965 */
966 STATIC void rtk_rxeof(sc)
967 struct rtk_softc *sc;
968 {
969 struct mbuf *m;
970 struct ifnet *ifp;
971 caddr_t rxbufpos, dst;
972 int total_len, wrap = 0;
973 u_int32_t rxstat;
974 u_int16_t cur_rx, new_rx;
975 u_int16_t limit;
976 u_int16_t rx_bytes = 0, max_bytes;
977
978 ifp = &sc->ethercom.ec_if;
979
980 cur_rx = (CSR_READ_2(sc, RTK_CURRXADDR) + 16) % RTK_RXBUFLEN;
981
982 /* Do not try to read past this point. */
983 limit = CSR_READ_2(sc, RTK_CURRXBUF) % RTK_RXBUFLEN;
984
985 if (limit < cur_rx)
986 max_bytes = (RTK_RXBUFLEN - cur_rx) + limit;
987 else
988 max_bytes = limit - cur_rx;
989
990 while((CSR_READ_1(sc, RTK_COMMAND) & RTK_CMD_EMPTY_RXBUF) == 0) {
991 rxbufpos = sc->rtk_cdata.rtk_rx_buf + cur_rx;
992 bus_dmamap_sync(sc->sc_dmat, sc->recv_dmamap, cur_rx,
993 RTK_RXSTAT_LEN, BUS_DMASYNC_POSTREAD);
994 rxstat = le32toh(*(u_int32_t *)rxbufpos);
995 bus_dmamap_sync(sc->sc_dmat, sc->recv_dmamap, cur_rx,
996 RTK_RXSTAT_LEN, BUS_DMASYNC_PREREAD);
997
998 /*
999 * Here's a totally undocumented fact for you. When the
1000 * RealTek chip is in the process of copying a packet into
1001 * RAM for you, the length will be 0xfff0. If you spot a
1002 * packet header with this value, you need to stop. The
1003 * datasheet makes absolutely no mention of this and
1004 * RealTek should be shot for this.
1005 */
1006 total_len = rxstat >> 16;
1007 if (total_len == RTK_RXSTAT_UNFINISHED)
1008 break;
1009
1010 if ((rxstat & RTK_RXSTAT_RXOK) == 0 ||
1011 total_len > ETHER_MAX_LEN) {
1012 ifp->if_ierrors++;
1013
1014 /*
1015 * submitted by:[netbsd-pcmcia:00484]
1016 * Takahiro Kambe <taca (at) sky.yamashina.kyoto.jp>
1017 * obtain from:
1018 * FreeBSD if_rl.c rev 1.24->1.25
1019 *
1020 */
1021 #if 0
1022 if (rxstat & (RTK_RXSTAT_BADSYM|RTK_RXSTAT_RUNT|
1023 RTK_RXSTAT_GIANT|RTK_RXSTAT_CRCERR|
1024 RTK_RXSTAT_ALIGNERR)) {
1025 CSR_WRITE_2(sc, RTK_COMMAND, RTK_CMD_TX_ENB);
1026 CSR_WRITE_2(sc, RTK_COMMAND,
1027 RTK_CMD_TX_ENB|RTK_CMD_RX_ENB);
1028 CSR_WRITE_4(sc, RTK_RXCFG, RTK_RXCFG_CONFIG);
1029 CSR_WRITE_4(sc, RTK_RXADDR,
1030 sc->recv_dmamap->dm_segs[0].ds_addr);
1031 cur_rx = 0;
1032 }
1033 break;
1034 #else
1035 rtk_init(ifp);
1036 return;
1037 #endif
1038 }
1039
1040 /* No errors; receive the packet. */
1041 rx_bytes += total_len + RTK_RXSTAT_LEN;
1042
1043 /*
1044 * Avoid trying to read more bytes than we know
1045 * the chip has prepared for us.
1046 */
1047 if (rx_bytes > max_bytes)
1048 break;
1049
1050 /*
1051 * Skip the status word, wrapping around to the beginning
1052 * of the Rx area, if necessary.
1053 */
1054 cur_rx = (cur_rx + RTK_RXSTAT_LEN) % RTK_RXBUFLEN;
1055 rxbufpos = sc->rtk_cdata.rtk_rx_buf + cur_rx;
1056
1057 /*
1058 * Compute the number of bytes at which the packet
1059 * will wrap to the beginning of the ring buffer.
1060 */
1061 wrap = RTK_RXBUFLEN - cur_rx;
1062
1063 /*
1064 * Compute where the next pending packet is.
1065 */
1066 if (total_len > wrap)
1067 new_rx = total_len - wrap;
1068 else
1069 new_rx = cur_rx + total_len;
1070 /* Round up to 32-bit boundary. */
1071 new_rx = (new_rx + 3) & ~3;
1072
1073 /*
1074 * Now allocate an mbuf (and possibly a cluster) to hold
1075 * the packet. Note we offset the packet 2 bytes so that
1076 * data after the Ethernet header will be 4-byte aligned.
1077 */
1078 MGETHDR(m, M_DONTWAIT, MT_DATA);
1079 if (m == NULL) {
1080 printf("%s: unable to allocate Rx mbuf\n",
1081 sc->sc_dev.dv_xname);
1082 ifp->if_ierrors++;
1083 goto next_packet;
1084 }
1085 if (total_len > (MHLEN - RTK_ETHER_ALIGN)) {
1086 MCLGET(m, M_DONTWAIT);
1087 if ((m->m_flags & M_EXT) == 0) {
1088 printf("%s: unable to allocate Rx cluster\n",
1089 sc->sc_dev.dv_xname);
1090 ifp->if_ierrors++;
1091 m_freem(m);
1092 m = NULL;
1093 goto next_packet;
1094 }
1095 }
1096 m->m_data += RTK_ETHER_ALIGN; /* for alignment */
1097 m->m_pkthdr.rcvif = ifp;
1098 m->m_pkthdr.len = m->m_len = total_len;
1099 dst = mtod(m, caddr_t);
1100
1101 /*
1102 * If the packet wraps, copy up to the wrapping point.
1103 */
1104 if (total_len > wrap) {
1105 bus_dmamap_sync(sc->sc_dmat, sc->recv_dmamap,
1106 cur_rx, wrap, BUS_DMASYNC_POSTREAD);
1107 memcpy(dst, rxbufpos, wrap);
1108 bus_dmamap_sync(sc->sc_dmat, sc->recv_dmamap,
1109 cur_rx, wrap, BUS_DMASYNC_PREREAD);
1110 cur_rx = 0;
1111 rxbufpos = sc->rtk_cdata.rtk_rx_buf;
1112 total_len -= wrap;
1113 dst += wrap;
1114 }
1115
1116 /*
1117 * ...and now the rest.
1118 */
1119 bus_dmamap_sync(sc->sc_dmat, sc->recv_dmamap,
1120 cur_rx, total_len, BUS_DMASYNC_POSTREAD);
1121 memcpy(dst, rxbufpos, total_len);
1122 bus_dmamap_sync(sc->sc_dmat, sc->recv_dmamap,
1123 cur_rx, total_len, BUS_DMASYNC_PREREAD);
1124
1125 next_packet:
1126 CSR_WRITE_2(sc, RTK_CURRXADDR, new_rx - 16);
1127 cur_rx = new_rx;
1128
1129 if (m == NULL)
1130 continue;
1131
1132 /*
1133 * The RealTek chip includes the CRC with every
1134 * incoming packet.
1135 */
1136 m->m_flags |= M_HASFCS;
1137
1138 ifp->if_ipackets++;
1139
1140 #if NBPFILTER > 0
1141 if (ifp->if_bpf)
1142 bpf_mtap(ifp->if_bpf, m);
1143 #endif
1144 /* pass it on. */
1145 (*ifp->if_input)(ifp, m);
1146 }
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)) == 0)
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|RTK_TXSTAT_OUTOFWIN))
1190 CSR_WRITE_4(sc, RTK_TXCFG, RTK_TXCFG_CONFIG);
1191 }
1192 RTK_INC(sc->rtk_cdata.last_tx);
1193 ifp->if_flags &= ~IFF_OACTIVE;
1194 } while (sc->rtk_cdata.last_tx != sc->rtk_cdata.cur_tx);
1195 }
1196
1197 int rtk_intr(arg)
1198 void *arg;
1199 {
1200 struct rtk_softc *sc;
1201 struct ifnet *ifp;
1202 u_int16_t status;
1203 int handled = 0;
1204
1205 sc = arg;
1206 ifp = &sc->ethercom.ec_if;
1207
1208 /* Disable interrupts. */
1209 CSR_WRITE_2(sc, RTK_IMR, 0x0000);
1210
1211 for (;;) {
1212
1213 status = CSR_READ_2(sc, RTK_ISR);
1214 if (status)
1215 CSR_WRITE_2(sc, RTK_ISR, status);
1216
1217 handled = 1;
1218
1219 if ((status & RTK_INTRS) == 0)
1220 break;
1221
1222 if (status & RTK_ISR_RX_OK)
1223 rtk_rxeof(sc);
1224
1225 if (status & RTK_ISR_RX_ERR)
1226 rtk_rxeof(sc);
1227
1228 if (status & (RTK_ISR_TX_OK|RTK_ISR_TX_ERR))
1229 rtk_txeof(sc);
1230
1231 if (status & RTK_ISR_SYSTEM_ERR) {
1232 rtk_reset(sc);
1233 rtk_init(ifp);
1234 }
1235 }
1236
1237 /* Re-enable interrupts. */
1238 CSR_WRITE_2(sc, RTK_IMR, RTK_INTRS);
1239
1240 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1241 rtk_start(ifp);
1242
1243 return (handled);
1244 }
1245
1246 /*
1247 * Main transmit routine.
1248 */
1249
1250 STATIC void rtk_start(ifp)
1251 struct ifnet *ifp;
1252 {
1253 struct rtk_softc *sc;
1254 struct mbuf *m_head = NULL, *m_new;
1255 int error, idx, len;
1256
1257 sc = ifp->if_softc;
1258
1259 while(RTK_CUR_TXMBUF(sc) == NULL) {
1260 IFQ_POLL(&ifp->if_snd, m_head);
1261 if (m_head == NULL)
1262 break;
1263 m_new = NULL;
1264
1265 idx = sc->rtk_cdata.cur_tx;
1266
1267 /*
1268 * Load the DMA map. If this fails, the packet didn't
1269 * fit in one DMA segment, and we need to copy. Note,
1270 * the packet must also be aligned.
1271 */
1272 if ((mtod(m_head, bus_addr_t) & 3) != 0 ||
1273 bus_dmamap_load_mbuf(sc->sc_dmat, sc->snd_dmamap[idx],
1274 m_head, BUS_DMA_NOWAIT) != 0) {
1275 MGETHDR(m_new, M_DONTWAIT, MT_DATA);
1276 if (m_new == NULL) {
1277 printf("%s: unable to allocate Tx mbuf\n",
1278 sc->sc_dev.dv_xname);
1279 break;
1280 }
1281 if (m_head->m_pkthdr.len > MHLEN) {
1282 MCLGET(m_new, M_DONTWAIT);
1283 if ((m_new->m_flags & M_EXT) == 0) {
1284 printf("%s: unable to allocate Tx "
1285 "cluster\n", sc->sc_dev.dv_xname);
1286 m_freem(m_new);
1287 break;
1288 }
1289 }
1290 m_copydata(m_head, 0, m_head->m_pkthdr.len,
1291 mtod(m_new, caddr_t));
1292 m_new->m_pkthdr.len = m_new->m_len =
1293 m_head->m_pkthdr.len;
1294 error = bus_dmamap_load_mbuf(sc->sc_dmat,
1295 sc->snd_dmamap[idx], m_new, 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 break;
1300 }
1301 }
1302 IFQ_DEQUEUE(&ifp->if_snd, m_head);
1303 if (m_new != NULL) {
1304 m_freem(m_head);
1305 m_head = m_new;
1306 }
1307
1308 RTK_CUR_TXMBUF(sc) = m_head;
1309
1310 #if NBPFILTER > 0
1311 /*
1312 * If there's a BPF listener, bounce a copy of this frame
1313 * to him.
1314 */
1315 if (ifp->if_bpf)
1316 bpf_mtap(ifp->if_bpf, RTK_CUR_TXMBUF(sc));
1317 #endif
1318 /*
1319 * Transmit the frame.
1320 */
1321 bus_dmamap_sync(sc->sc_dmat,
1322 sc->snd_dmamap[idx], 0, sc->snd_dmamap[idx]->dm_mapsize,
1323 BUS_DMASYNC_PREWRITE);
1324
1325 len = sc->snd_dmamap[idx]->dm_segs[0].ds_len;
1326 if (len < (ETHER_MIN_LEN - ETHER_CRC_LEN))
1327 len = (ETHER_MIN_LEN - ETHER_CRC_LEN);
1328
1329 CSR_WRITE_4(sc, RTK_CUR_TXADDR(sc),
1330 sc->snd_dmamap[idx]->dm_segs[0].ds_addr);
1331 CSR_WRITE_4(sc, RTK_CUR_TXSTAT(sc), RTK_TX_EARLYTHRESH | len);
1332
1333 RTK_INC(sc->rtk_cdata.cur_tx);
1334 }
1335
1336 /*
1337 * We broke out of the loop because all our TX slots are
1338 * full. Mark the NIC as busy until it drains some of the
1339 * packets from the queue.
1340 */
1341 if (RTK_CUR_TXMBUF(sc) != NULL)
1342 ifp->if_flags |= IFF_OACTIVE;
1343
1344 /*
1345 * Set a timeout in case the chip goes out to lunch.
1346 */
1347 ifp->if_timer = 5;
1348 }
1349
1350 STATIC int rtk_init(ifp)
1351 struct ifnet *ifp;
1352 {
1353 struct rtk_softc *sc = ifp->if_softc;
1354 int error = 0, i;
1355 u_int32_t rxcfg;
1356
1357 if ((error = rtk_enable(sc)) != 0)
1358 goto out;
1359
1360 /*
1361 * Cancel pending I/O.
1362 */
1363 rtk_stop(ifp, 0);
1364
1365 /* Init our MAC address */
1366 for (i = 0; i < ETHER_ADDR_LEN; i++) {
1367 CSR_WRITE_1(sc, RTK_IDR0 + i, LLADDR(ifp->if_sadl)[i]);
1368 }
1369
1370 /* Init the RX buffer pointer register. */
1371 bus_dmamap_sync(sc->sc_dmat, sc->recv_dmamap, 0,
1372 sc->recv_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
1373 CSR_WRITE_4(sc, RTK_RXADDR, sc->recv_dmamap->dm_segs[0].ds_addr);
1374
1375 /* Init TX descriptors. */
1376 rtk_list_tx_init(sc);
1377
1378 /*
1379 * Enable transmit and receive.
1380 */
1381 CSR_WRITE_1(sc, RTK_COMMAND, RTK_CMD_TX_ENB|RTK_CMD_RX_ENB);
1382
1383 /*
1384 * Set the initial TX and RX configuration.
1385 */
1386 CSR_WRITE_4(sc, RTK_TXCFG, RTK_TXCFG_CONFIG);
1387 CSR_WRITE_4(sc, RTK_RXCFG, RTK_RXCFG_CONFIG);
1388
1389 /* Set the individual bit to receive frames for this host only. */
1390 rxcfg = CSR_READ_4(sc, RTK_RXCFG);
1391 rxcfg |= RTK_RXCFG_RX_INDIV;
1392
1393 /* If we want promiscuous mode, set the allframes bit. */
1394 if (ifp->if_flags & IFF_PROMISC) {
1395 rxcfg |= RTK_RXCFG_RX_ALLPHYS;
1396 CSR_WRITE_4(sc, RTK_RXCFG, rxcfg);
1397 } else {
1398 rxcfg &= ~RTK_RXCFG_RX_ALLPHYS;
1399 CSR_WRITE_4(sc, RTK_RXCFG, rxcfg);
1400 }
1401
1402 /*
1403 * Set capture broadcast bit to capture broadcast frames.
1404 */
1405 if (ifp->if_flags & IFF_BROADCAST) {
1406 rxcfg |= RTK_RXCFG_RX_BROAD;
1407 CSR_WRITE_4(sc, RTK_RXCFG, rxcfg);
1408 } else {
1409 rxcfg &= ~RTK_RXCFG_RX_BROAD;
1410 CSR_WRITE_4(sc, RTK_RXCFG, rxcfg);
1411 }
1412
1413 /*
1414 * Program the multicast filter, if necessary.
1415 */
1416 rtk_setmulti(sc);
1417
1418 /*
1419 * Enable interrupts.
1420 */
1421 CSR_WRITE_2(sc, RTK_IMR, RTK_INTRS);
1422
1423 /* Start RX/TX process. */
1424 CSR_WRITE_4(sc, RTK_MISSEDPKT, 0);
1425
1426 /* Enable receiver and transmitter. */
1427 CSR_WRITE_1(sc, RTK_COMMAND, RTK_CMD_TX_ENB|RTK_CMD_RX_ENB);
1428
1429 CSR_WRITE_1(sc, RTK_CFG1, RTK_CFG1_DRVLOAD|RTK_CFG1_FULLDUPLEX);
1430
1431 /*
1432 * Set current media.
1433 */
1434 mii_mediachg(&sc->mii);
1435
1436 ifp->if_flags |= IFF_RUNNING;
1437 ifp->if_flags &= ~IFF_OACTIVE;
1438
1439 callout_reset(&sc->rtk_tick_ch, hz, rtk_tick, sc);
1440
1441 out:
1442 if (error) {
1443 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1444 ifp->if_timer = 0;
1445 printf("%s: interface not running\n", sc->sc_dev.dv_xname);
1446 }
1447 return (error);
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 rtk_ioctl(ifp, command, data)
1480 struct ifnet *ifp;
1481 u_long command;
1482 caddr_t data;
1483 {
1484 struct rtk_softc *sc = ifp->if_softc;
1485 struct ifreq *ifr = (struct ifreq *) data;
1486 int s, error = 0;
1487
1488 s = splnet();
1489
1490 switch (command) {
1491 case SIOCGIFMEDIA:
1492 case SIOCSIFMEDIA:
1493 error = ifmedia_ioctl(ifp, ifr, &sc->mii.mii_media, command);
1494 break;
1495
1496 default:
1497 error = ether_ioctl(ifp, command, data);
1498 if (error == ENETRESET) {
1499 if (RTK_IS_ENABLED(sc)) {
1500 /*
1501 * Multicast list has changed. Set the
1502 * hardware filter accordingly.
1503 */
1504 rtk_setmulti(sc);
1505 }
1506 error = 0;
1507 }
1508 break;
1509 }
1510
1511 splx(s);
1512
1513 return (error);
1514 }
1515
1516 STATIC void rtk_watchdog(ifp)
1517 struct ifnet *ifp;
1518 {
1519 struct rtk_softc *sc;
1520
1521 sc = ifp->if_softc;
1522
1523 printf("%s: watchdog timeout\n", sc->sc_dev.dv_xname);
1524 ifp->if_oerrors++;
1525 rtk_txeof(sc);
1526 rtk_rxeof(sc);
1527 rtk_init(ifp);
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