rtl81x9.c revision 1.23 1 /* $NetBSD: rtl81x9.c,v 1.23 2000/12/05 11:11:49 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
721 /*
722 * Do ifmedia setup.
723 */
724 sc->mii.mii_ifp = ifp;
725 sc->mii.mii_readreg = rtk_phy_readreg;
726 sc->mii.mii_writereg = rtk_phy_writereg;
727 sc->mii.mii_statchg = rtk_phy_statchg;
728 ifmedia_init(&sc->mii.mii_media, 0, rtk_ifmedia_upd, rtk_ifmedia_sts);
729 mii_attach(&sc->sc_dev, &sc->mii, 0xffffffff,
730 MII_PHY_ANY, MII_OFFSET_ANY, 0);
731
732 /* Choose a default media. */
733 if (LIST_FIRST(&sc->mii.mii_phys) == NULL) {
734 ifmedia_add(&sc->mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
735 ifmedia_set(&sc->mii.mii_media, IFM_ETHER|IFM_NONE);
736 } else {
737 ifmedia_set(&sc->mii.mii_media, IFM_ETHER|IFM_AUTO);
738 }
739
740 /*
741 * Call MI attach routines.
742 */
743 if_attach(ifp);
744 ether_ifattach(ifp, eaddr);
745
746 /*
747 * Make sure the interface is shutdown during reboot.
748 */
749 sc->sc_sdhook = shutdownhook_establish(rtk_shutdown, sc);
750 if (sc->sc_sdhook == NULL)
751 printf("%s: WARNING: unbale to establish shutdown hook\n",
752 sc->sc_dev.dv_xname);
753 /*
754 * Add a suspend hook to make sure we come back up after a
755 * resume.
756 */
757 sc->sc_powerhook = powerhook_establish(rtk_power, sc);
758 if (sc->sc_powerhook == NULL)
759 printf("%s: WARNING: unable to establish power hook\n",
760 sc->sc_dev.dv_xname);
761
762 return;
763 fail_4:
764 for (i = 0; i < RTK_TX_LIST_CNT; i++)
765 if (sc->snd_dmamap[i] != NULL)
766 bus_dmamap_destroy(sc->sc_dmat, sc->snd_dmamap[i]);
767 fail_3:
768 bus_dmamap_destroy(sc->sc_dmat, sc->recv_dmamap);
769 fail_2:
770 bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->rtk_cdata.rtk_rx_buf,
771 RTK_RXBUFLEN + 16);
772 fail_1:
773 bus_dmamem_free(sc->sc_dmat, &sc->sc_dmaseg, sc->sc_dmanseg);
774 fail_0:
775 return;
776 }
777
778 /*
779 * Initialize the transmit descriptors.
780 */
781 STATIC int rtk_list_tx_init(sc)
782 struct rtk_softc *sc;
783 {
784 struct rtk_chain_data *cd;
785 int i;
786
787 cd = &sc->rtk_cdata;
788 for (i = 0; i < RTK_TX_LIST_CNT; i++) {
789 cd->rtk_tx_chain[i] = NULL;
790 CSR_WRITE_4(sc,
791 RTK_TXADDR0 + (i * sizeof(u_int32_t)), 0x0000000);
792 }
793
794 sc->rtk_cdata.cur_tx = 0;
795 sc->rtk_cdata.last_tx = 0;
796
797 return (0);
798 }
799
800 /*
801 * rtk_activate:
802 * Handle device activation/deactivation requests.
803 */
804 int
805 rtk_activate(self, act)
806 struct device *self;
807 enum devact act;
808 {
809 struct rtk_softc *sc = (void *) self;
810 int s, error = 0;
811
812 s = splnet();
813 switch (act) {
814 case DVACT_ACTIVATE:
815 error = EOPNOTSUPP;
816 break;
817 case DVACT_DEACTIVATE:
818 mii_activate(&sc->mii, act, MII_PHY_ANY, MII_OFFSET_ANY);
819 if_deactivate(&sc->ethercom.ec_if);
820 break;
821 }
822 splx(s);
823
824 return (error);
825 }
826
827 /*
828 * rtk_detach:
829 * Detach a rtk interface.
830 */
831 int
832 rtk_detach(sc)
833 struct rtk_softc *sc;
834 {
835 struct ifnet *ifp = &sc->ethercom.ec_if;
836 int i;
837
838 /*
839 * Succeed now if thereisn't any work to do.
840 */
841 if ((sc->sc_flags & RTK_ATTACHED) == 0)
842 return (0);
843
844 /* Unhook our tick handler. */
845 callout_stop(&sc->rtk_tick_ch);
846
847 /* Detach all PHYs. */
848 mii_detach(&sc->mii, MII_PHY_ANY, MII_OFFSET_ANY);
849
850 /* Delete all remaining media. */
851 ifmedia_delete_instance(&sc->mii.mii_media, IFM_INST_ANY);
852
853 ether_ifdetach(ifp);
854 if_detach(ifp);
855
856 for (i = 0; i < RTK_TX_LIST_CNT; i++)
857 if (sc->snd_dmamap[i] != NULL)
858 bus_dmamap_destroy(sc->sc_dmat, sc->snd_dmamap[i]);
859 bus_dmamap_destroy(sc->sc_dmat, sc->recv_dmamap);
860 bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->rtk_cdata.rtk_rx_buf,
861 RTK_RXBUFLEN + 16);
862
863 shutdownhook_disestablish(sc->sc_sdhook);
864 powerhook_disestablish(sc->sc_powerhook);
865
866 return (0);
867 }
868
869 /*
870 * rtk_enable:
871 * Enable the RTL81X9 chip.
872 */
873 int
874 rtk_enable(sc)
875 struct rtk_softc *sc;
876 {
877
878 if (RTK_IS_ENABLED(sc) == 0 && sc->sc_enable != NULL) {
879 if ((*sc->sc_enable)(sc) != 0) {
880 printf("%s: device enable failed\n",
881 sc->sc_dev.dv_xname);
882 return (EIO);
883 }
884 sc->sc_flags |= RTK_ENABLED;
885 }
886 return (0);
887 }
888
889 /*
890 * rtk_disable:
891 * Disable the RTL81X9 chip.
892 */
893 void
894 rtk_disable(sc)
895 struct rtk_softc *sc;
896 {
897
898 if (RTK_IS_ENABLED(sc) && sc->sc_disable != NULL) {
899 (*sc->sc_disable)(sc);
900 sc->sc_flags &= ~RTK_ENABLED;
901 }
902 }
903
904 /*
905 * rtk_power:
906 * Power management (suspend/resume) hook.
907 */
908 void
909 rtk_power(why, arg)
910 int why;
911 void *arg;
912 {
913 struct rtk_softc *sc = (void *) arg;
914 struct ifnet *ifp = &sc->ethercom.ec_if;
915 int s;
916
917 s = splnet();
918 switch (why) {
919 case PWR_SUSPEND:
920 case PWR_STANDBY:
921 rtk_stop(ifp, 0);
922 if (sc->sc_power != NULL)
923 (*sc->sc_power)(sc, why);
924 break;
925 case PWR_RESUME:
926 if (ifp->if_flags & IFF_UP) {
927 if (sc->sc_power != NULL)
928 (*sc->sc_power)(sc, why);
929 rtk_init(ifp);
930 }
931 break;
932 case PWR_SOFTSUSPEND:
933 case PWR_SOFTSTANDBY:
934 case PWR_SOFTRESUME:
935 break;
936 }
937 splx(s);
938 }
939
940 /*
941 * A frame has been uploaded: pass the resulting mbuf chain up to
942 * the higher level protocols.
943 *
944 * You know there's something wrong with a PCI bus-master chip design.
945 *
946 * The receive operation is badly documented in the datasheet, so I'll
947 * attempt to document it here. The driver provides a buffer area and
948 * places its base address in the RX buffer start address register.
949 * The chip then begins copying frames into the RX buffer. Each frame
950 * is preceeded by a 32-bit RX status word which specifies the length
951 * of the frame and certain other status bits. Each frame (starting with
952 * the status word) is also 32-bit aligned. The frame length is in the
953 * first 16 bits of the status word; the lower 15 bits correspond with
954 * the 'rx status register' mentioned in the datasheet.
955 *
956 * Note: to make the Alpha happy, the frame payload needs to be aligned
957 * on a 32-bit boundary. To achieve this, we copy the data to mbuf
958 * shifted forward 2 bytes.
959 */
960 STATIC void rtk_rxeof(sc)
961 struct rtk_softc *sc;
962 {
963 struct mbuf *m;
964 struct ifnet *ifp;
965 caddr_t rxbufpos, dst;
966 int total_len, wrap = 0;
967 u_int32_t rxstat;
968 u_int16_t cur_rx, new_rx;
969 u_int16_t limit;
970 u_int16_t rx_bytes = 0, max_bytes;
971
972 ifp = &sc->ethercom.ec_if;
973
974 cur_rx = (CSR_READ_2(sc, RTK_CURRXADDR) + 16) % RTK_RXBUFLEN;
975
976 /* Do not try to read past this point. */
977 limit = CSR_READ_2(sc, RTK_CURRXBUF) % RTK_RXBUFLEN;
978
979 if (limit < cur_rx)
980 max_bytes = (RTK_RXBUFLEN - cur_rx) + limit;
981 else
982 max_bytes = limit - cur_rx;
983
984 while((CSR_READ_1(sc, RTK_COMMAND) & RTK_CMD_EMPTY_RXBUF) == 0) {
985 rxbufpos = sc->rtk_cdata.rtk_rx_buf + cur_rx;
986 bus_dmamap_sync(sc->sc_dmat, sc->recv_dmamap, cur_rx,
987 RTK_RXSTAT_LEN, BUS_DMASYNC_POSTREAD);
988 rxstat = le32toh(*(u_int32_t *)rxbufpos);
989 bus_dmamap_sync(sc->sc_dmat, sc->recv_dmamap, cur_rx,
990 RTK_RXSTAT_LEN, BUS_DMASYNC_PREREAD);
991
992 /*
993 * Here's a totally undocumented fact for you. When the
994 * RealTek chip is in the process of copying a packet into
995 * RAM for you, the length will be 0xfff0. If you spot a
996 * packet header with this value, you need to stop. The
997 * datasheet makes absolutely no mention of this and
998 * RealTek should be shot for this.
999 */
1000 total_len = rxstat >> 16;
1001 if (total_len == RTK_RXSTAT_UNFINISHED)
1002 break;
1003
1004 if ((rxstat & RTK_RXSTAT_RXOK) == 0) {
1005 ifp->if_ierrors++;
1006
1007 /*
1008 * submitted by:[netbsd-pcmcia:00484]
1009 * Takahiro Kambe <taca (at) sky.yamashina.kyoto.jp>
1010 * obtain from:
1011 * FreeBSD if_rl.c rev 1.24->1.25
1012 *
1013 */
1014 #if 0
1015 if (rxstat & (RTK_RXSTAT_BADSYM|RTK_RXSTAT_RUNT|
1016 RTK_RXSTAT_GIANT|RTK_RXSTAT_CRCERR|
1017 RTK_RXSTAT_ALIGNERR)) {
1018 CSR_WRITE_2(sc, RTK_COMMAND, RTK_CMD_TX_ENB);
1019 CSR_WRITE_2(sc, RTK_COMMAND,
1020 RTK_CMD_TX_ENB|RTK_CMD_RX_ENB);
1021 CSR_WRITE_4(sc, RTK_RXCFG, RTK_RXCFG_CONFIG);
1022 CSR_WRITE_4(sc, RTK_RXADDR,
1023 sc->recv_dmamap->dm_segs[0].ds_addr);
1024 cur_rx = 0;
1025 }
1026 break;
1027 #else
1028 rtk_init(ifp);
1029 return;
1030 #endif
1031 }
1032
1033 /* No errors; receive the packet. */
1034 rx_bytes += total_len + RTK_RXSTAT_LEN;
1035
1036 /*
1037 * Avoid trying to read more bytes than we know
1038 * the chip has prepared for us.
1039 */
1040 if (rx_bytes > max_bytes)
1041 break;
1042
1043 /*
1044 * Skip the status word, wrapping around to the beginning
1045 * of the Rx area, if necessary.
1046 */
1047 cur_rx += RTK_RXSTAT_LEN;
1048 rxbufpos = sc->rtk_cdata.rtk_rx_buf + (cur_rx % RTK_RXBUFLEN);
1049
1050 /*
1051 * Compute the number of bytes at which the packet
1052 * will wrap to the beginning of the ring buffer.
1053 */
1054 wrap = RTK_RXBUFLEN - (cur_rx % RTK_RXBUFLEN);
1055
1056 /*
1057 * Compute where the next pending packet is.
1058 */
1059 if (total_len > wrap)
1060 new_rx = total_len - wrap;
1061 else
1062 new_rx = cur_rx + total_len;
1063 /* Round up to 32-bit boundary. */
1064 new_rx = (new_rx + 3) & ~3;
1065
1066 /*
1067 * Now allocate an mbuf (and possibly a cluster) to hold
1068 * the packet. Note we offset the packet 2 bytes so that
1069 * data after the Ethernet header will be 4-byte aligned.
1070 */
1071 MGETHDR(m, M_DONTWAIT, MT_DATA);
1072 if (m == NULL) {
1073 printf("%s: unable to allocate Rx mbuf\n",
1074 sc->sc_dev.dv_xname);
1075 ifp->if_ierrors++;
1076 goto next_packet;
1077 }
1078 if (total_len > (MHLEN - RTK_ETHER_ALIGN)) {
1079 MCLGET(m, M_DONTWAIT);
1080 if ((m->m_flags & M_EXT) == 0) {
1081 printf("%s: unable to allocate Rx cluster\n",
1082 sc->sc_dev.dv_xname);
1083 ifp->if_ierrors++;
1084 m_freem(m);
1085 m = NULL;
1086 goto next_packet;
1087 }
1088 }
1089 m->m_data += RTK_ETHER_ALIGN; /* for alignment */
1090 m->m_pkthdr.rcvif = ifp;
1091 m->m_pkthdr.len = m->m_len = total_len;
1092 dst = mtod(m, caddr_t);
1093
1094 /*
1095 * If the packet wraps, copy up to the wrapping point.
1096 */
1097 if (total_len > wrap) {
1098 bus_dmamap_sync(sc->sc_dmat, sc->recv_dmamap,
1099 cur_rx, wrap, BUS_DMASYNC_POSTREAD);
1100 memcpy(dst, rxbufpos, wrap);
1101 bus_dmamap_sync(sc->sc_dmat, sc->recv_dmamap,
1102 cur_rx, wrap, BUS_DMASYNC_PREREAD);
1103 cur_rx = 0;
1104 rxbufpos = sc->rtk_cdata.rtk_rx_buf;
1105 total_len -= wrap;
1106 dst += wrap;
1107 }
1108
1109 /*
1110 * ...and now the rest.
1111 */
1112 bus_dmamap_sync(sc->sc_dmat, sc->recv_dmamap,
1113 cur_rx, total_len, BUS_DMASYNC_POSTREAD);
1114 memcpy(dst, rxbufpos, total_len);
1115 bus_dmamap_sync(sc->sc_dmat, sc->recv_dmamap,
1116 cur_rx, total_len, BUS_DMASYNC_PREREAD);
1117
1118 next_packet:
1119 CSR_WRITE_2(sc, RTK_CURRXADDR, new_rx - 16);
1120 cur_rx = new_rx;
1121
1122 if (m == NULL)
1123 continue;
1124
1125 /*
1126 * The RealTek chip includes the CRC with every
1127 * incoming packet.
1128 */
1129 m->m_flags |= M_HASFCS;
1130
1131 ifp->if_ipackets++;
1132
1133 #if NBPFILTER > 0
1134 if (ifp->if_bpf)
1135 bpf_mtap(ifp->if_bpf, m);
1136 #endif
1137 /* pass it on. */
1138 (*ifp->if_input)(ifp, m);
1139 }
1140 }
1141
1142 /*
1143 * A frame was downloaded to the chip. It's safe for us to clean up
1144 * the list buffers.
1145 */
1146 STATIC void rtk_txeof(sc)
1147 struct rtk_softc *sc;
1148 {
1149 struct ifnet *ifp;
1150 u_int32_t txstat;
1151
1152 ifp = &sc->ethercom.ec_if;
1153
1154 /* Clear the timeout timer. */
1155 ifp->if_timer = 0;
1156
1157 /*
1158 * Go through our tx list and free mbufs for those
1159 * frames that have been uploaded.
1160 */
1161 do {
1162 txstat = CSR_READ_4(sc, RTK_LAST_TXSTAT(sc));
1163 if ((txstat & (RTK_TXSTAT_TX_OK|
1164 RTK_TXSTAT_TX_UNDERRUN|RTK_TXSTAT_TXABRT)) == 0)
1165 break;
1166
1167 bus_dmamap_sync(sc->sc_dmat,
1168 sc->snd_dmamap[sc->rtk_cdata.last_tx], 0,
1169 sc->snd_dmamap[sc->rtk_cdata.last_tx]->dm_mapsize,
1170 BUS_DMASYNC_POSTWRITE);
1171 bus_dmamap_unload(sc->sc_dmat,
1172 sc->snd_dmamap[sc->rtk_cdata.last_tx]);
1173 m_freem(RTK_LAST_TXMBUF(sc));
1174 RTK_LAST_TXMBUF(sc) = NULL;
1175
1176 ifp->if_collisions += (txstat & RTK_TXSTAT_COLLCNT) >> 24;
1177
1178 if (txstat & RTK_TXSTAT_TX_OK)
1179 ifp->if_opackets++;
1180 else {
1181 ifp->if_oerrors++;
1182 if (txstat & (RTK_TXSTAT_TXABRT|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
1190 int rtk_intr(arg)
1191 void *arg;
1192 {
1193 struct rtk_softc *sc;
1194 struct ifnet *ifp;
1195 u_int16_t status;
1196 int handled = 0;
1197
1198 sc = arg;
1199 ifp = &sc->ethercom.ec_if;
1200
1201 /* Disable interrupts. */
1202 CSR_WRITE_2(sc, RTK_IMR, 0x0000);
1203
1204 for (;;) {
1205
1206 status = CSR_READ_2(sc, RTK_ISR);
1207 if (status)
1208 CSR_WRITE_2(sc, RTK_ISR, status);
1209
1210 handled = 1;
1211
1212 if ((status & RTK_INTRS) == 0)
1213 break;
1214
1215 if (status & RTK_ISR_RX_OK)
1216 rtk_rxeof(sc);
1217
1218 if (status & RTK_ISR_RX_ERR)
1219 rtk_rxeof(sc);
1220
1221 if (status & (RTK_ISR_TX_OK|RTK_ISR_TX_ERR))
1222 rtk_txeof(sc);
1223
1224 if (status & RTK_ISR_SYSTEM_ERR) {
1225 rtk_reset(sc);
1226 rtk_init(ifp);
1227 }
1228 }
1229
1230 /* Re-enable interrupts. */
1231 CSR_WRITE_2(sc, RTK_IMR, RTK_INTRS);
1232
1233 if (ifp->if_snd.ifq_head != NULL) {
1234 rtk_start(ifp);
1235 }
1236
1237 return (handled);
1238 }
1239
1240 /*
1241 * Main transmit routine.
1242 */
1243
1244 STATIC void rtk_start(ifp)
1245 struct ifnet *ifp;
1246 {
1247 struct rtk_softc *sc;
1248 struct mbuf *m_head = NULL, *m_new;
1249 int error, idx, len;
1250
1251 sc = ifp->if_softc;
1252
1253 while(RTK_CUR_TXMBUF(sc) == NULL) {
1254 IF_DEQUEUE(&ifp->if_snd, m_head);
1255 if (m_head == NULL)
1256 break;
1257
1258 idx = sc->rtk_cdata.cur_tx;
1259
1260 /*
1261 * Load the DMA map. If this fails, the packet didn't
1262 * fit in one DMA segment, and we need to copy. Note,
1263 * the packet must also be aligned.
1264 */
1265 if ((mtod(m_head, bus_addr_t) & 3) != 0 ||
1266 bus_dmamap_load_mbuf(sc->sc_dmat, sc->snd_dmamap[idx],
1267 m_head, BUS_DMA_NOWAIT) != 0) {
1268 MGETHDR(m_new, M_DONTWAIT, MT_DATA);
1269 if (m_new == NULL) {
1270 printf("%s: unable to allocate Tx mbuf\n",
1271 sc->sc_dev.dv_xname);
1272 IF_PREPEND(&ifp->if_snd, m_new);
1273 break;
1274 }
1275 if (m_head->m_pkthdr.len > MHLEN) {
1276 MCLGET(m_new, M_DONTWAIT);
1277 if ((m_new->m_flags & M_EXT) == 0) {
1278 printf("%s: unable to allocate Tx "
1279 "cluster\n", sc->sc_dev.dv_xname);
1280 m_freem(m_new);
1281 IF_PREPEND(&ifp->if_snd, m_head);
1282 break;
1283 }
1284 }
1285 m_copydata(m_head, 0, m_head->m_pkthdr.len,
1286 mtod(m_new, caddr_t));
1287 m_new->m_pkthdr.len = m_new->m_len =
1288 m_head->m_pkthdr.len;
1289 m_freem(m_head);
1290 m_head = m_new;
1291 error = bus_dmamap_load_mbuf(sc->sc_dmat,
1292 sc->snd_dmamap[idx], m_head, BUS_DMA_NOWAIT);
1293 if (error) {
1294 printf("%s: unable to load Tx buffer, "
1295 "error = %d\n", sc->sc_dev.dv_xname, error);
1296 IF_PREPEND(&ifp->if_snd, m_head);
1297 break;
1298 }
1299 }
1300
1301 RTK_CUR_TXMBUF(sc) = m_head;
1302
1303 #if NBPFILTER > 0
1304 /*
1305 * If there's a BPF listener, bounce a copy of this frame
1306 * to him.
1307 */
1308 if (ifp->if_bpf)
1309 bpf_mtap(ifp->if_bpf, RTK_CUR_TXMBUF(sc));
1310 #endif
1311 /*
1312 * Transmit the frame.
1313 */
1314 bus_dmamap_sync(sc->sc_dmat,
1315 sc->snd_dmamap[idx], 0, sc->snd_dmamap[idx]->dm_mapsize,
1316 BUS_DMASYNC_PREWRITE);
1317
1318 len = sc->snd_dmamap[idx]->dm_segs[0].ds_len;
1319 if (len < (ETHER_MIN_LEN - ETHER_CRC_LEN))
1320 len = (ETHER_MIN_LEN - ETHER_CRC_LEN);
1321
1322 CSR_WRITE_4(sc, RTK_CUR_TXADDR(sc),
1323 sc->snd_dmamap[idx]->dm_segs[0].ds_addr);
1324 CSR_WRITE_4(sc, RTK_CUR_TXSTAT(sc), RTK_TX_EARLYTHRESH | len);
1325
1326 RTK_INC(sc->rtk_cdata.cur_tx);
1327 }
1328
1329 /*
1330 * We broke out of the loop because all our TX slots are
1331 * full. Mark the NIC as busy until it drains some of the
1332 * packets from the queue.
1333 */
1334 if (RTK_CUR_TXMBUF(sc) != NULL)
1335 ifp->if_flags |= IFF_OACTIVE;
1336
1337 /*
1338 * Set a timeout in case the chip goes out to lunch.
1339 */
1340 ifp->if_timer = 5;
1341 }
1342
1343 STATIC int rtk_init(ifp)
1344 struct ifnet *ifp;
1345 {
1346 struct rtk_softc *sc = ifp->if_softc;
1347 int error = 0, i;
1348 u_int32_t rxcfg;
1349
1350 if ((error = rtk_enable(sc)) != 0)
1351 goto out;
1352
1353 /*
1354 * Cancel pending I/O.
1355 */
1356 rtk_stop(ifp, 0);
1357
1358 /* Init our MAC address */
1359 for (i = 0; i < ETHER_ADDR_LEN; i++) {
1360 CSR_WRITE_1(sc, RTK_IDR0 + i, LLADDR(ifp->if_sadl)[i]);
1361 }
1362
1363 /* Init the RX buffer pointer register. */
1364 bus_dmamap_sync(sc->sc_dmat, sc->recv_dmamap, 0,
1365 sc->recv_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
1366 CSR_WRITE_4(sc, RTK_RXADDR, sc->recv_dmamap->dm_segs[0].ds_addr);
1367
1368 /* Init TX descriptors. */
1369 rtk_list_tx_init(sc);
1370
1371 /*
1372 * Enable transmit and receive.
1373 */
1374 CSR_WRITE_1(sc, RTK_COMMAND, RTK_CMD_TX_ENB|RTK_CMD_RX_ENB);
1375
1376 /*
1377 * Set the initial TX and RX configuration.
1378 */
1379 CSR_WRITE_4(sc, RTK_TXCFG, RTK_TXCFG_CONFIG);
1380 CSR_WRITE_4(sc, RTK_RXCFG, RTK_RXCFG_CONFIG);
1381
1382 /* Set the individual bit to receive frames for this host only. */
1383 rxcfg = CSR_READ_4(sc, RTK_RXCFG);
1384 rxcfg |= RTK_RXCFG_RX_INDIV;
1385
1386 /* If we want promiscuous mode, set the allframes bit. */
1387 if (ifp->if_flags & IFF_PROMISC) {
1388 rxcfg |= RTK_RXCFG_RX_ALLPHYS;
1389 CSR_WRITE_4(sc, RTK_RXCFG, rxcfg);
1390 } else {
1391 rxcfg &= ~RTK_RXCFG_RX_ALLPHYS;
1392 CSR_WRITE_4(sc, RTK_RXCFG, rxcfg);
1393 }
1394
1395 /*
1396 * Set capture broadcast bit to capture broadcast frames.
1397 */
1398 if (ifp->if_flags & IFF_BROADCAST) {
1399 rxcfg |= RTK_RXCFG_RX_BROAD;
1400 CSR_WRITE_4(sc, RTK_RXCFG, rxcfg);
1401 } else {
1402 rxcfg &= ~RTK_RXCFG_RX_BROAD;
1403 CSR_WRITE_4(sc, RTK_RXCFG, rxcfg);
1404 }
1405
1406 /*
1407 * Program the multicast filter, if necessary.
1408 */
1409 rtk_setmulti(sc);
1410
1411 /*
1412 * Enable interrupts.
1413 */
1414 CSR_WRITE_2(sc, RTK_IMR, RTK_INTRS);
1415
1416 /* Start RX/TX process. */
1417 CSR_WRITE_4(sc, RTK_MISSEDPKT, 0);
1418
1419 /* Enable receiver and transmitter. */
1420 CSR_WRITE_1(sc, RTK_COMMAND, RTK_CMD_TX_ENB|RTK_CMD_RX_ENB);
1421
1422 CSR_WRITE_1(sc, RTK_CFG1, RTK_CFG1_DRVLOAD|RTK_CFG1_FULLDUPLEX);
1423
1424 /*
1425 * Set current media.
1426 */
1427 mii_mediachg(&sc->mii);
1428
1429 ifp->if_flags |= IFF_RUNNING;
1430 ifp->if_flags &= ~IFF_OACTIVE;
1431
1432 callout_reset(&sc->rtk_tick_ch, hz, rtk_tick, sc);
1433
1434 out:
1435 if (error) {
1436 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1437 ifp->if_timer = 0;
1438 printf("%s: interface not running\n", sc->sc_dev.dv_xname);
1439 }
1440 return (error);
1441 }
1442
1443 /*
1444 * Set media options.
1445 */
1446 STATIC int rtk_ifmedia_upd(ifp)
1447 struct ifnet *ifp;
1448 {
1449 struct rtk_softc *sc;
1450
1451 sc = ifp->if_softc;
1452
1453 return (mii_mediachg(&sc->mii));
1454 }
1455
1456 /*
1457 * Report current media status.
1458 */
1459 STATIC void rtk_ifmedia_sts(ifp, ifmr)
1460 struct ifnet *ifp;
1461 struct ifmediareq *ifmr;
1462 {
1463 struct rtk_softc *sc;
1464
1465 sc = ifp->if_softc;
1466
1467 mii_pollstat(&sc->mii);
1468 ifmr->ifm_status = sc->mii.mii_media_status;
1469 ifmr->ifm_active = sc->mii.mii_media_active;
1470 }
1471
1472 STATIC int rtk_ioctl(ifp, command, data)
1473 struct ifnet *ifp;
1474 u_long command;
1475 caddr_t data;
1476 {
1477 struct rtk_softc *sc = ifp->if_softc;
1478 struct ifreq *ifr = (struct ifreq *) data;
1479 int s, error = 0;
1480
1481 s = splnet();
1482
1483 switch (command) {
1484 case SIOCGIFMEDIA:
1485 case SIOCSIFMEDIA:
1486 error = ifmedia_ioctl(ifp, ifr, &sc->mii.mii_media, command);
1487 break;
1488
1489 default:
1490 error = ether_ioctl(ifp, command, data);
1491 if (error == ENETRESET) {
1492 if (RTK_IS_ENABLED(sc)) {
1493 /*
1494 * Multicast list has changed. Set the
1495 * hardware filter accordingly.
1496 */
1497 rtk_setmulti(sc);
1498 }
1499 error = 0;
1500 }
1501 break;
1502 }
1503
1504 splx(s);
1505
1506 return (error);
1507 }
1508
1509 STATIC void rtk_watchdog(ifp)
1510 struct ifnet *ifp;
1511 {
1512 struct rtk_softc *sc;
1513
1514 sc = ifp->if_softc;
1515
1516 printf("%s: watchdog timeout\n", sc->sc_dev.dv_xname);
1517 ifp->if_oerrors++;
1518 rtk_txeof(sc);
1519 rtk_rxeof(sc);
1520 rtk_init(ifp);
1521 }
1522
1523 /*
1524 * Stop the adapter and free any mbufs allocated to the
1525 * RX and TX lists.
1526 */
1527 STATIC void rtk_stop(ifp, disable)
1528 struct ifnet *ifp;
1529 int disable;
1530 {
1531 struct rtk_softc *sc = ifp->if_softc;
1532 int i;
1533
1534 callout_stop(&sc->rtk_tick_ch);
1535
1536 mii_down(&sc->mii);
1537
1538 CSR_WRITE_1(sc, RTK_COMMAND, 0x00);
1539 CSR_WRITE_2(sc, RTK_IMR, 0x0000);
1540
1541 /*
1542 * Free the TX list buffers.
1543 */
1544 for (i = 0; i < RTK_TX_LIST_CNT; i++) {
1545 if (sc->rtk_cdata.rtk_tx_chain[i] != NULL) {
1546 bus_dmamap_unload(sc->sc_dmat, sc->snd_dmamap[i]);
1547 m_freem(sc->rtk_cdata.rtk_tx_chain[i]);
1548 sc->rtk_cdata.rtk_tx_chain[i] = NULL;
1549 CSR_WRITE_4(sc, RTK_TXADDR0 + i, 0x0000000);
1550 }
1551 }
1552
1553 if (disable)
1554 rtk_disable(sc);
1555
1556 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1557 ifp->if_timer = 0;
1558 }
1559
1560 /*
1561 * Stop all chip I/O so that the kernel's probe routines don't
1562 * get confused by errant DMAs when rebooting.
1563 */
1564 STATIC void rtk_shutdown(vsc)
1565 void *vsc;
1566 {
1567 struct rtk_softc *sc = (struct rtk_softc *)vsc;
1568
1569 rtk_stop(&sc->ethercom.ec_if, 0);
1570 }
1571
1572 STATIC void
1573 rtk_tick(arg)
1574 void *arg;
1575 {
1576 struct rtk_softc *sc = arg;
1577 int s = splnet();
1578
1579 mii_tick(&sc->mii);
1580 splx(s);
1581
1582 callout_reset(&sc->rtk_tick_ch, hz, rtk_tick, sc);
1583 }
1584