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