2 * Copyright 1998, Joerg Wunsch
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * Device driver for RealTek RTL 8002 (`REDP') based pocket-ethernet
32 * adapters, hooked up to a printer port. `rdp' is a shorthand for
33 * REDP since some tools like netstat work best if the interface name
34 * has no more than three letters.
36 * Driver configuration flags so far:
37 * flags 0x1 -- assume 74S288 EEPROM (default 94C46)
38 * flags 0x2 -- use `slow' mode (mode 3 of the packet driver, default 0)
40 * Maybe this driver will some day also work with the successor, RTL
41 * 8012 (`AREDP'), which is unfortunately not fully register-
42 * compatible with the 8002. The 8012 offers support for faster
43 * transfer modi like bidirectional SPP and EPP, 64 K x 4 buffer
44 * memory as opposed to 16 K x 4 for the 8002, a multicast filter, and
45 * a builtin multiplexer that allows chaining a printer behind the
48 * About the only documentation i've been able to find about the RTL
49 * 8002 was the packet driver source code at ftp.realtek.com.tw, so
50 * this driver is somewhat based on the way the packet driver handles
51 * the chip. The exact author of the packet driver is unknown, the
52 * only name that i could find in the source was someone called Chiu,
53 * supposedly an employee of RealTek. So credits to them for that
54 * piece of code which has proven valuable to me.
56 * Later on, Leo kuo <leo@realtek.com.tw> has been very helpful to me
57 * by sending me a readable (PDF) file documenting the RTL 8012, which
58 * helped me to also understand the 8002, as well as by providing me
59 * with the source code of the 8012 packet driver that i haven't been
60 * able to find on the FTP site. A big Thanks! goes here to RealTek
61 * for this kind of service.
66 #include <sys/param.h>
67 #include <sys/systm.h>
68 #include <sys/kernel.h>
69 #include <sys/sockio.h>
71 #include <sys/socket.h>
72 #include <sys/syslog.h>
75 #include <net/ethernet.h>
77 #include <net/if_arp.h>
78 #include <net/if_dl.h>
79 #include <net/if_mib.h>
82 #include <netinet/in.h>
83 #include <netinet/if_ether.h>
88 #include <netns/ns_if.h>
93 #include <machine/md_var.h>
95 #include <i386/isa/isa_device.h>
96 #include <i386/isa/icu.h>
97 #include <i386/isa/if_rdpreg.h>
98 #include <i386/isa/intr_machdep.h>
100 #ifndef COMPAT_OLDISA
101 #error "The rdp device requires the old isa compatibility shims"
104 #define IOCTL_CMD_T u_long
107 * Debug levels (ORed together):
108 * != 0 - general (bad packets etc.)
109 * 2 - debug EEPROM IO
110 * 4 - debug interrupt status
116 * rdp_softc: per interface info and status
119 struct arpcom arpcom; /*
120 * Ethernet common, always goes first so
121 * a rdp_softc * can be cast into an
122 * arpcom * or into an ifnet *.
126 * local stuff, somewhat sorted by memory alignment class
128 u_short baseaddr; /* IO port address */
129 u_short txsize; /* tx size for next (buffered) packet,
130 * there's only one additional packet
131 * we can buffer, thus a single variable
132 * ought to be enough */
133 int txbusy; /* tx is transmitting */
134 int txbuffered; /* # of packets in tx buffer */
135 int slow; /* use lpt_control to send data */
136 u_char irqenbit; /* mirror of current Ctrl_IRQEN */
138 * type of parameter EEPROM; device flags 0x1 selects 74S288
141 EEPROM_93C46, EEPROM_74S288 /* or 82S123 */
145 static struct rdp_softc rdp_softc[NRDP];
148 * Since there's no fixed location in the EEPROM about where to find
149 * the ethernet hardware address, we drop a table of valid OUIs here,
150 * and search through the EEPROM until we find a possible valid
151 * Ethernet address. Only the first 16 bits of all possible OUIs are
152 * recorded in the table (as obtained from
153 * http://standards.ieee.org/regauth/oui/oui.txt).
156 static u_short allowed_ouis[] = {
157 0x0000, 0x0001, 0x0002, 0x0004, 0x0005, 0x0006, 0x0007,
158 0x0008, 0x0010, 0x001C, 0x0020, 0x0040, 0x0050, 0x0060,
159 0x0070, 0x0080, 0x0090, 0x009D, 0x00A0, 0x00AA, 0x00BB,
160 0x00C0, 0x00CF, 0x00DD, 0x00E0, 0x00E6, 0x0207, 0x021C,
161 0x0260, 0x0270, 0x029D, 0x02AA, 0x02BB, 0x02C0, 0x02CF,
162 0x02E6, 0x040A, 0x04E0, 0x0800, 0x08BB, 0x1000, 0x1100,
169 static int rdp_probe __P((struct isa_device *));
170 static int rdp_attach __P((struct isa_device *));
173 * Required entry points.
175 static void rdp_init(void *);
176 static int rdp_ioctl(struct ifnet *, IOCTL_CMD_T, caddr_t);
177 static void rdp_start(struct ifnet *);
178 static void rdp_reset(struct ifnet *);
179 static void rdp_watchdog(struct ifnet *);
180 static void rdpintr(int);
183 * REDP private functions.
186 static void rdp_stop(struct rdp_softc *);
187 static void rdp_rint(struct rdp_softc *);
188 static void rdp_get_packet(struct rdp_softc *, unsigned);
189 static u_short rdp_write_mbufs(struct rdp_softc *, struct mbuf *);
190 static int rdp_gethwaddr_93c46(struct rdp_softc *, u_char *);
191 static void rdp_gethwaddr_74s288(struct rdp_softc *, u_char *);
192 static void rdp_93c46_cmd(struct rdp_softc *, u_short, unsigned);
193 static u_short rdp_93c46_read(struct rdp_softc *);
195 struct isa_driver rdpdriver = {
200 1 /* we wanna get a chance before lptN */
202 COMPAT_ISA_DRIVER(rdp, rdpdriver);
205 * REDP-specific functions.
207 * They are inlined, thus go first in this file. Together with gcc's
208 * usual optimization, these functions probably come close to the
209 * packet driver's hand-optimized code. ;-)
211 * Comments are partially obtained from the packet driver as well.
212 * Some of the function names contain register names which don't make
213 * much sense for us, but i've kept them for easier reference in
214 * comparision to the packet driver.
216 * Some of the functions are currently not used by the driver; it's
217 * not quite clear whether we ever need them at all. They are
218 * supposedly even slower than what is currently implemented as `slow'
219 * mode. Right now, `fast' (default) mode is what the packet driver
220 * calls mode 0, slow mode is mode 3 (writing through lpt_control,
223 * We should autoprobe the modi, as opposed to making them dependent
224 * on a kernel configuration flag.
228 * read a nibble from rreg; end-of-data cmd is not issued;
229 * used for general register read.
231 * Unlike the packet driver's version, i'm shifting the result
232 * by 3 here (as opposed to within the caller's code) for clarity.
235 static __inline u_char
236 RdNib(struct rdp_softc *sc, u_char rreg)
239 outb(sc->baseaddr + lpt_data, EOC + rreg);
240 outb(sc->baseaddr + lpt_data, RdAddr + rreg); /* write addr */
241 (void)inb(sc->baseaddr + lpt_status);
242 return (inb(sc->baseaddr + lpt_status) >> 3) & 0x0f;
247 * read a byte from MAR register through lpt_data; the low nibble is
248 * read prior to the high one; end-of-read command is not issued; used
249 * for remote DMA in mode 4 + 5
251 static __inline u_char
252 RdByte(struct rdp_softc *sc)
256 outb(sc->baseaddr + lpt_data, RdAddr + MAR); /* cmd for low nibble */
257 lonib = (inb(sc->baseaddr + lpt_status) >> 3) & 0x0f;
258 outb(sc->baseaddr + lpt_data, RdAddr + MAR + HNib);
259 hinib = (inb(sc->baseaddr + lpt_status) << 1) & 0xf0;
260 return hinib + lonib;
265 * read a byte from MAR register through lpt_data; the low nibble is
266 * read prior to the high one; end-of-read command is not issued; used
267 * for remote DMA in mode 6 + 7
269 static __inline u_char
270 RdByte1(struct rdp_softc *sc)
274 outb(sc->baseaddr + lpt_data, RdAddr + MAR); /* cmd for low nibble */
275 (void)inb(sc->baseaddr + lpt_status);
276 lonib = (inb(sc->baseaddr + lpt_status) >> 3) & 0x0f;
277 outb(sc->baseaddr + lpt_data, RdAddr + MAR + HNib);
278 (void)inb(sc->baseaddr + lpt_status);
279 hinib = (inb(sc->baseaddr + lpt_status) << 1) & 0xf0;
280 return hinib + lonib;
286 * read a byte from MAR register through lpt_control; the low nibble is
287 * read prior to the high one; end-of-read command is not issued; used
288 * for remote DMA in mode 0 + 1
290 static __inline u_char
291 RdByteA1(struct rdp_softc *sc)
295 outb(sc->baseaddr + lpt_control, Ctrl_LNibRead);
296 lonib = (inb(sc->baseaddr + lpt_status) >> 3) & 0x0f;
297 outb(sc->baseaddr + lpt_control, Ctrl_HNibRead);
298 hinib = (inb(sc->baseaddr + lpt_status) << 1) & 0xf0;
299 return hinib + lonib;
304 * read a byte from MAR register through lpt_control; the low nibble is
305 * read prior to the high one; end-of-read command is not issued; used
306 * for remote DMA in mode 2 + 3
308 static __inline u_char
309 RdByteA2(struct rdp_softc *sc)
313 outb(sc->baseaddr + lpt_control, Ctrl_LNibRead);
314 (void)inb(sc->baseaddr + lpt_status);
315 lonib = (inb(sc->baseaddr + lpt_status) >> 3) & 0x0f;
316 outb(sc->baseaddr + lpt_control, Ctrl_HNibRead);
317 (void)inb(sc->baseaddr + lpt_status);
318 hinib = (inb(sc->baseaddr + lpt_status) << 1) & 0xf0;
319 return hinib + lonib;
326 RdEnd(struct rdp_softc *sc, u_char rreg)
329 outb(sc->baseaddr + lpt_data, EOC + rreg);
333 * Write a nibble to a register; end-of-write is issued.
334 * Used for general register write.
337 WrNib(struct rdp_softc *sc, u_char wreg, u_char wdata)
340 /* prepare and write address */
341 outb(sc->baseaddr + lpt_data, EOC + wreg);
342 outb(sc->baseaddr + lpt_data, WrAddr + wreg);
343 outb(sc->baseaddr + lpt_data, WrAddr + wreg);
344 /* prepare and write data */
345 outb(sc->baseaddr + lpt_data, WrAddr + wdata);
346 outb(sc->baseaddr + lpt_data, wdata);
347 outb(sc->baseaddr + lpt_data, wdata);
349 outb(sc->baseaddr + lpt_data, EOC + wdata);
353 * Write a byte to a register; end-of-write is issued.
354 * Used for general register write.
357 WrByte(struct rdp_softc *sc, u_char wreg, u_char wdata)
360 /* prepare and write address */
361 outb(sc->baseaddr + lpt_data, EOC + wreg);
362 outb(sc->baseaddr + lpt_data, WrAddr + wreg);
363 outb(sc->baseaddr + lpt_data, WrAddr + wreg);
364 /* prepare and write low nibble */
365 outb(sc->baseaddr + lpt_data, WrAddr + (wdata & 0x0F));
366 outb(sc->baseaddr + lpt_data, (wdata & 0x0F));
367 outb(sc->baseaddr + lpt_data, (wdata & 0x0F));
368 /* prepare and write high nibble */
370 outb(sc->baseaddr + lpt_data, wdata);
371 outb(sc->baseaddr + lpt_data, wdata + HNib);
372 outb(sc->baseaddr + lpt_data, wdata + HNib);
374 outb(sc->baseaddr + lpt_data, EOC + wdata + HNib);
378 * Write the byte to DRAM via lpt_data;
379 * used for remote DMA write in mode 0 / 2 / 4
382 WrByteALToDRAM(struct rdp_softc *sc, u_char val)
385 outb(sc->baseaddr + lpt_data, val & 0x0F);
386 outb(sc->baseaddr + lpt_data, MkHi(val));
390 * Write the byte to DRAM via lpt_control;
391 * used for remote DMA write in mode 1 / 3 / 5
394 WrByteALToDRAMA(struct rdp_softc *sc, u_char val)
397 outb(sc->baseaddr + lpt_data, val & 0x0F);
398 outb(sc->baseaddr + lpt_control, Ctrl_LNibRead | sc->irqenbit);
399 outb(sc->baseaddr + lpt_data, val >> 4);
400 outb(sc->baseaddr + lpt_control, Ctrl_HNibRead | sc->irqenbit);
403 #if 0 /* they could be used for the RAM test */
405 * Write the u_short to DRAM via lpt_data;
406 * used for remote DMA write in mode 0 / 2 / 4
409 WrWordbxToDRAM(struct rdp_softc *sc, u_short val)
412 outb(sc->baseaddr + lpt_data, val & 0x0F);
414 outb(sc->baseaddr + lpt_data, (val & 0x0F) + HNib);
416 outb(sc->baseaddr + lpt_data, val & 0x0F);
418 outb(sc->baseaddr + lpt_data, val + HNib);
423 * Write the u_short to DRAM via lpt_control;
424 * used for remote DMA write in mode 1 / 3 / 5
427 WrWordbxToDRAMA(struct rdp_softc *sc, u_short val)
430 outb(sc->baseaddr + lpt_data, val & 0x0F);
431 outb(sc->baseaddr + lpt_control, Ctrl_LNibRead | sc->irqenbit);
433 outb(sc->baseaddr + lpt_data, (val & 0x0F) + HNib);
434 outb(sc->baseaddr + lpt_control, Ctrl_HNibRead | sc->irqenbit);
436 outb(sc->baseaddr + lpt_data, val & 0x0F);
437 outb(sc->baseaddr + lpt_control, Ctrl_LNibRead | sc->irqenbit);
439 outb(sc->baseaddr + lpt_data, val + HNib);
440 outb(sc->baseaddr + lpt_control, Ctrl_HNibRead | sc->irqenbit);
446 * Determine if the device is present
449 * a pointer to an isa_device struct
451 * 0 if device not found
452 * or # of i/o addresses used (if found)
455 rdp_probe(struct isa_device *isa_dev)
457 int unit = isa_dev->id_unit;
458 struct rdp_softc *sc = &rdp_softc[unit];
460 intrmask_t irqmap[3];
463 if (unit < 0 || unit >= NRDP)
466 sc->baseaddr = isa_dev->id_iobase;
467 if (isa_dev->id_flags & 1)
468 sc->eeprom = EEPROM_74S288;
469 /* else defaults to 93C46 */
470 if (isa_dev->id_flags & 2)
473 /* let R/WB = A/DB = CSB = high to be ready for next r/w cycle */
474 outb(sc->baseaddr + lpt_data, 0xFF);
475 /* DIR = 0 for write mode, IRQEN=0, SLCT=INIT=AUTOFEED=STB=high */
476 outb(sc->baseaddr + lpt_control, Ctrl_SelData);
478 WrNib(sc, CMR1 + HNib, MkHi(CMR1_RST));
481 b1 = RdNib(sc, CMR1);
483 b2 = RdNib(sc, CMR2) & 0x0f;
484 b2 |= RdNib(sc, CMR2 + HNib) << 4;
485 RdEnd(sc, CMR2 + HNib);
487 * After the reset, we expect CMR1 & 7 to be 1 (rx buffer empty),
488 * and CMR2 & 0xf7 to be 0x20 (receive mode set to physical and
492 printf("rdp%d: CMR1 = %#x, CMR2 = %#x\n", unit, b1, b2);
494 if ((b1 & (CMR1_BUFE | CMR1_IRQ | CMR1_TRA)) != CMR1_BUFE
495 || (b2 & ~CMR2_IRQINV) != CMR2_AM_PB)
499 * We have found something that could be a RTL 80[01]2, now
500 * see whether we can generate an interrupt.
505 * Test whether our configured IRQ is working.
507 * Set to no acception mode + IRQout, then enable RxE + TxE,
508 * then cause RBER (by advancing the read pointer although
509 * the read buffer is empty) to generate an interrupt.
511 WrByte(sc, CMR2, CMR2_IRQOUT);
512 WrNib(sc, CMR1 + HNib, MkHi(CMR1_TE | CMR1_RE));
513 WrNib(sc, CMR1, CMR1_RDPAC);
516 irqmap[0] = isa_irq_pending();
517 sval[0] = inb(sc->baseaddr + lpt_status);
519 /* allow IRQs to pass the parallel interface */
520 outb(sc->baseaddr + lpt_control, Ctrl_IRQEN + Ctrl_SelData);
522 /* generate interrupt */
523 WrNib(sc, IMR + HNib, MkHi(ISR_RBER));
526 irqmap[1] = isa_irq_pending();
527 sval[1] = inb(sc->baseaddr + lpt_status);
529 /* de-assert and disable IRQ */
530 WrNib(sc, IMR + HNib, MkHi(0));
531 (void)inb(sc->baseaddr + lpt_status); /* might be necessary to
534 irqmap[2] = isa_irq_pending();
535 sval[2] = inb(sc->baseaddr + lpt_status);
537 WrNib(sc, CMR1 + HNib, MkHi(0));
538 outb(sc->baseaddr + lpt_control, Ctrl_SelData);
539 WrNib(sc, CMR2, CMR2_IRQINV);
544 printf("rdp%d: irq maps / lpt status "
545 "%#x/%#x - %#x/%#x - %#x/%#x (id_irq %#x)\n",
546 unit, irqmap[0], sval[0], irqmap[1], sval[1],
547 irqmap[2], sval[2], isa_dev->id_irq);
549 if ((irqmap[1] & isa_dev->id_irq) == 0) {
550 printf("rdp%d: configured IRQ (%d) cannot be asserted "
552 unit, ffs(isa_dev->id_irq) - 1);
554 printf(" (probable IRQ: %d)", ffs(irqmap[1]) - 1);
560 * XXX should do RAMtest here
563 switch (sc->eeprom) {
565 if (rdp_gethwaddr_93c46(sc, sc->arpcom.ac_enaddr) == 0) {
566 printf("rdp%d: failed to find a valid hardware "
567 "address in EEPROM\n",
574 rdp_gethwaddr_74s288(sc, sc->arpcom.ac_enaddr);
578 return lpt_control + 1;
582 * Install interface into kernel networking data structures
585 rdp_attach(struct isa_device *isa_dev)
587 int unit = isa_dev->id_unit;
588 struct rdp_softc *sc = &rdp_softc[unit];
589 struct ifnet *ifp = &sc->arpcom.ac_if;
591 isa_dev->id_ointr = rdpintr;
600 * Initialize ifnet structure
604 ifp->if_name = "rdp";
605 ifp->if_output = ether_output;
606 ifp->if_start = rdp_start;
607 ifp->if_ioctl = rdp_ioctl;
608 ifp->if_watchdog = rdp_watchdog;
609 ifp->if_init = rdp_init;
610 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
611 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX;
614 * Attach the interface
616 ether_ifattach(ifp, ETHER_BPF_SUPPORTED);
620 * Print additional info when attached
622 printf("%s%d: RealTek RTL%s pocket ethernet, EEPROM %s, %s mode\n",
623 ifp->if_name, ifp->if_unit,
624 "8002", /* hook for 8012 */
625 sc->eeprom == EEPROM_93C46? "93C46": "74S288",
626 sc->slow? "slow": "fast");
627 printf("%s%d: address %6D\n", ifp->if_name, ifp->if_unit,
628 sc->arpcom.ac_enaddr, ":");
637 rdp_reset(struct ifnet *ifp)
639 struct rdp_softc *sc = ifp->if_softc;
645 * Stop interface and re-initialize.
654 * Take interface offline.
657 rdp_stop(struct rdp_softc *sc)
660 sc->txbusy = sc->txbusy = 0;
662 /* disable printer interface interrupts */
664 outb(sc->baseaddr + lpt_control, Ctrl_SelData);
665 outb(sc->baseaddr + lpt_data, 0xff);
667 /* reset the RTL 8002 */
668 WrNib(sc, CMR1 + HNib, MkHi(CMR1_RST));
673 * Device timeout/watchdog routine. Entered if the device neglects to
674 * generate an interrupt after a transmit has been started on it.
677 rdp_watchdog(struct ifnet *ifp)
680 log(LOG_ERR, "rdp%d: device timeout\n", ifp->if_unit);
692 struct rdp_softc *sc = xsc;
693 struct ifnet *ifp = &sc->arpcom.ac_if;
697 /* address not known */
698 if (TAILQ_EMPTY(&ifp->if_addrhead))
705 /* program ethernet ID into the chip */
706 for (i = 0, reg = IDR0; i < 6; i++, reg++)
707 WrByte(sc, reg, sc->arpcom.ac_enaddr[i]);
709 /* set accept mode */
710 WrNib(sc, CMR2 + HNib,
711 MkHi((ifp->if_flags & IFF_PROMISC)? CMR2_AM_ALL: CMR2_AM_PB));
713 /* enable tx and rx */
714 WrNib(sc, CMR1 + HNib, MkHi(CMR1_TE | CMR1_RE));
716 /* allow interrupts to happen */
717 WrNib(sc, CMR2, CMR2_IRQOUT | CMR2_IRQINV);
718 WrNib(sc, IMR, ISR_TOK | ISR_TER | ISR_ROK | ISR_RER);
719 WrNib(sc, IMR + HNib, MkHi(ISR_RBER));
721 /* allow IRQs to pass the parallel interface */
722 sc->irqenbit = Ctrl_IRQEN;
723 outb(sc->baseaddr + lpt_control, sc->irqenbit + Ctrl_SelData);
725 /* clear all flags */
726 sc->txbusy = sc->txbuffered = 0;
729 * Set 'running' flag, and clear output active flag.
731 ifp->if_flags |= IFF_RUNNING;
732 ifp->if_flags &= ~IFF_OACTIVE;
735 * ...and attempt to start output
743 * Start output on interface.
744 * We make two assumptions here:
745 * 1) that the current priority is set to splimp _before_ this code
746 * is called *and* is returned to the appropriate priority after
748 * 2) that the IFF_OACTIVE flag is checked before this code is called
749 * (i.e. that the output part of the interface is idle)
752 rdp_start(struct ifnet *ifp)
754 struct rdp_softc *sc = ifp->if_softc;
761 * See if there is room to put another packet in the buffer.
763 if (sc->txbuffered) {
765 * No room. Indicate this to the outside world and exit.
767 ifp->if_flags |= IFF_OACTIVE;
770 IF_DEQUEUE(&ifp->if_snd, m);
773 * We are using the !OACTIVE flag to indicate to the outside
774 * world that we can accept an additional packet rather than
775 * that the transmitter is _actually_ active. Indeed, the
776 * transmitter may be active, but if we haven't filled all the
777 * buffers with data then we still want to accept more.
779 ifp->if_flags &= ~IFF_OACTIVE;
784 * Copy the mbuf chain into the transmit buffer
787 len = rdp_write_mbufs(sc, m);
791 /* ensure minimal valid ethernet length */
792 len = max(len, (ETHER_MIN_LEN-ETHER_CRC_LEN));
795 * Actually start the transceiver. Set a timeout in case the
796 * Tx interrupt never arrives.
799 WrNib(sc, TBCR1, len >> 8);
800 WrByte(sc, TBCR0, len & 0xff);
801 WrNib(sc, CMR1, CMR1_TRA);
810 * Tap off here if there is a bpf listener.
819 * Loop back to the top to possibly buffer more packets
825 * Process an ioctl request.
828 rdp_ioctl(struct ifnet *ifp, IOCTL_CMD_T command, caddr_t data)
830 struct rdp_softc *sc = ifp->if_softc;
840 error = ether_ioctl(ifp, command, data);
845 * If the interface is marked up and stopped, then start it.
846 * If it is marked down and running, then stop it.
848 if (ifp->if_flags & IFF_UP) {
849 if ((ifp->if_flags & IFF_RUNNING) == 0)
852 if (ifp->if_flags & IFF_RUNNING) {
854 ifp->if_flags &= ~IFF_RUNNING;
859 * Promiscuous flag may have changed, propagage this
862 if (ifp->if_flags & IFF_UP)
863 WrNib(sc, CMR2 + HNib,
864 MkHi((ifp->if_flags & IFF_PROMISC)?
865 CMR2_AM_ALL: CMR2_AM_PB));
872 * Multicast list has changed; we don't support it.
885 * External interrupt service routine.
890 struct rdp_softc *sc = rdp_softc + unit;
891 struct ifnet *ifp = (struct ifnet *)sc;
892 u_char isr, tsr, rsr, colls;
894 /* disable interrupts, so SD3 can be routed to the pin */
896 outb(sc->baseaddr + lpt_control, Ctrl_SelData);
897 WrNib(sc, CMR2, CMR2_IRQINV);
899 * loop until there are no more new interrupts
902 isr = RdNib(sc, ISR);
903 isr |= RdNib(sc, ISR + HNib) << 4;
904 RdEnd(sc, ISR + HNib);
909 printf("rdp%d: ISR = %#x\n", unit, isr);
913 * Clear the pending interrupt bits.
915 WrNib(sc, ISR, isr & 0x0f);
917 WrNib(sc, ISR + HNib, MkHi(isr));
920 * Handle transmitter interrupts.
922 if (isr & (ISR_TOK | ISR_TER)) {
923 tsr = RdNib(sc, TSR);
927 printf("rdp%d: tsr %#x\n", unit, tsr);
933 * Update total number of successfully
934 * transmitted packets.
939 colls = RdNib(sc, COLR);
941 ifp->if_collisions += colls;
945 * reset tx busy and output active flags
948 ifp->if_flags &= ~IFF_OACTIVE;
951 * If we had already queued up another packet,
952 * start sending it now.
954 if (sc->txbuffered) {
955 WrNib(sc, TBCR1, sc->txsize >> 8);
956 WrByte(sc, TBCR0, sc->txsize & 0xff);
957 WrNib(sc, CMR1, CMR1_TRA);
963 * clear watchdog timer
971 * Handle receiver interrupts
973 if (isr & (ISR_ROK | ISR_RER | ISR_RBER)) {
974 rsr = RdNib(sc, RSR);
975 rsr |= RdNib(sc, RSR + HNib) << 4;
976 RdEnd(sc, RSR + HNib);
978 if (isr & (ISR_RER | ISR_RBER))
979 printf("rdp%d: rsr %#x\n", unit, rsr);
982 if (rsr & (RSR_PUN | RSR_POV)) {
983 printf("rdp%d: rsr %#x, resetting\n",
991 * CRC and FA errors are recorded in
992 * rdp_rint() on a per-packet basis
995 if (isr & (ISR_ROK | ISR_RER))
1000 * If it looks like the transmitter can take more data,
1001 * attempt to start output on the interface. This is done
1002 * after handling the receiver to give the receiver priority.
1004 if ((ifp->if_flags & IFF_OACTIVE) == 0)
1008 /* re-enable interrupts */
1009 WrNib(sc, CMR2, CMR2_IRQOUT | CMR2_IRQINV);
1010 sc->irqenbit = Ctrl_IRQEN;
1011 outb(sc->baseaddr + lpt_control, Ctrl_SelData + sc->irqenbit);
1015 * Ethernet interface receiver interrupt.
1018 rdp_rint(struct rdp_softc *sc)
1020 struct ifnet *ifp = &sc->arpcom.ac_if;
1024 u_char *packet_ptr, b, status;
1025 int excessive_bad_pkts = 0;
1028 * Fetch the packets from the NIC's buffer.
1031 b = RdNib(sc, CMR1);
1035 /* no more packets */
1038 /* first, obtain the buffer header */
1040 outb(sc->baseaddr + lpt_data, MAR + EOC); /* prepare addr */
1041 outb(sc->baseaddr + lpt_control, Ctrl_LNibRead);
1042 outb(sc->baseaddr + lpt_data, MAR + RdAddr + HNib);
1044 packet_ptr = (u_char *)&rh;
1046 for (i = 0; i < sizeof rh; i++, packet_ptr++)
1047 *packet_ptr = RdByteA2(sc);
1049 for (i = 0; i < sizeof rh; i++, packet_ptr++)
1050 *packet_ptr = RdByteA1(sc);
1052 RdEnd(sc, MAR + HNib);
1053 outb(sc->baseaddr + lpt_control, Ctrl_SelData);
1055 len = rh.pktlen - ETHER_CRC_LEN;
1058 if ((status & (RSR_ROK | RSR_CRC | RSR_FA)) != RSR_ROK ||
1059 len > (ETHER_MAX_LEN - ETHER_CRC_LEN) ||
1060 len < (ETHER_MIN_LEN - ETHER_CRC_LEN) ||
1063 printf("rdp%d: bad packet in buffer, "
1064 "len %d, status %#x\n",
1065 ifp->if_unit, (int)len, (int)status);
1068 /* rx jump packet */
1069 WrNib(sc, CMR1, CMR1_RDPAC);
1070 if (++excessive_bad_pkts > 5) {
1072 * the chip seems to be stuck, we are
1073 * probably seeing the same bad packet
1074 * over and over again
1077 printf("rdp%d: resetting due to an "
1078 "excessive number of bad packets\n",
1090 excessive_bad_pkts = 0;
1091 rdp_get_packet(sc, len);
1097 * Retreive packet from NIC memory and send to the next level up via
1101 rdp_get_packet(struct rdp_softc *sc, unsigned len)
1103 struct ether_header *eh;
1108 /* Allocate a header mbuf */
1109 MGETHDR(m, M_DONTWAIT, MT_DATA);
1112 m->m_pkthdr.rcvif = &sc->arpcom.ac_if;
1113 m->m_pkthdr.len = m->m_len = len;
1116 * We always put the received packet in a single buffer -
1117 * either with just an mbuf header or in a cluster attached
1118 * to the header. The +2 is to compensate for the alignment
1121 if ((len + 2) > MHLEN) {
1122 /* Attach an mbuf cluster */
1123 MCLGET(m, M_DONTWAIT);
1125 /* Insist on getting a cluster */
1126 if ((m->m_flags & M_EXT) == 0) {
1133 * The +2 is to longword align the start of the real packet.
1134 * This is important for NFS.
1137 eh = mtod(m, struct ether_header *);
1140 * Get packet, including link layer address, from interface.
1142 outb(sc->baseaddr + lpt_control, Ctrl_LNibRead);
1143 outb(sc->baseaddr + lpt_data, RdAddr + MAR);
1145 packet_ptr = (u_char *)eh;
1147 for (s = 0; s < len; s++, packet_ptr++)
1148 *packet_ptr = RdByteA2(sc);
1150 for (s = 0; s < len; s++, packet_ptr++)
1151 *packet_ptr = RdByteA1(sc);
1153 RdEnd(sc, MAR + HNib);
1154 outb(sc->baseaddr + lpt_control, Ctrl_SelData);
1155 WrNib(sc, CMR1, CMR1_RDPAC);
1158 * Remove link layer address.
1160 m->m_pkthdr.len = m->m_len = len - sizeof(struct ether_header);
1161 m->m_data += sizeof(struct ether_header);
1163 ether_input(&sc->arpcom.ac_if, eh, m);
1167 * Write an mbuf chain to the NIC's tx buffer.
1170 rdp_write_mbufs(struct rdp_softc *sc, struct mbuf *m)
1177 /* First, count up the total number of bytes to copy */
1178 for (total_len = 0, mp = m; mp; mp = mp->m_next)
1179 total_len += mp->m_len;
1184 outb(sc->baseaddr + lpt_data, MAR | EOC);
1187 * Transfer the mbuf chain to the NIC memory.
1190 /* writing the first byte is complicated */
1191 outb(sc->baseaddr + lpt_control,
1192 Ctrl_LNibRead | sc->irqenbit);
1193 outb(sc->baseaddr + lpt_data, MAR | WrAddr);
1194 b = *(u_char *)m->m_data;
1195 outb(sc->baseaddr + lpt_data, (b & 0x0f) | 0x40);
1196 outb(sc->baseaddr + lpt_data, b & 0x0f);
1197 outb(sc->baseaddr + lpt_data, b >> 4);
1198 outb(sc->baseaddr + lpt_control,
1199 Ctrl_HNibRead | sc->irqenbit);
1200 /* advance the mbuf pointer */
1204 /* write the remaining bytes */
1206 for (i = 0, dp = (u_char *)m->m_data;
1209 WrByteALToDRAMA(sc, *dp);
1213 * restore old mbuf in case we have to hand it off to
1220 /* the RTL 8002 requires an even byte-count remote DMA */
1222 WrByteALToDRAMA(sc, 0);
1224 outb(sc->baseaddr + lpt_data, MAR | WrAddr);
1226 for (i = 0, dp = (u_char *)m->m_data;
1229 WrByteALToDRAM(sc, *dp);
1233 /* the RTL 8002 requires an even byte-count remote DMA */
1235 WrByteALToDRAM(sc, 0);
1238 outb(sc->baseaddr + lpt_data, 0xff);
1239 outb(sc->baseaddr + lpt_control,
1240 Ctrl_HNibRead | Ctrl_SelData | sc->irqenbit);
1246 * Read the designated ethernet hardware address out of a 93C46
1248 * Note that the 93C46 uses 16-bit words in big-endian notation.
1251 rdp_gethwaddr_93c46(struct rdp_softc *sc, u_char *etheraddr)
1257 WrNib(sc, CMR2, CMR2_PAGE | CMR2_IRQINV); /* select page 1 */
1260 * The original RealTek packet driver had the ethernet address
1261 * starting at EEPROM address 0. Other vendors seem to have
1262 * gone `creative' here -- while they didn't do anything else
1263 * than changing a few strings in the entire driver, compared
1264 * to the RealTek version, they also moved out the ethernet
1265 * address to a different location in the EEPROM, so the
1266 * original RealTek driver won't work correctly with them, and
1267 * vice versa. Sounds pretty cool, eh? $@%&!
1269 * Anyway, we walk through the EEPROM, until we find some
1270 * allowable value based upon our table of IEEE OUI assignments.
1272 for (i = magic = 0; magic < 3 && i < 32; i++) {
1273 /* read cmd (+ 6 bit address) */
1274 rdp_93c46_cmd(sc, 0x180 + i, 10);
1275 w = rdp_93c46_read(sc);
1279 j < sizeof allowed_ouis / sizeof(u_short);
1281 if (w == allowed_ouis[j]) {
1282 etheraddr[0] = (w >> 8) & 0xff;
1283 etheraddr[1] = w & 0xff;
1291 * If the first two bytes have been 00:00, we
1292 * discard the match iff the next two bytes
1293 * are also 00:00, so we won't get fooled by
1294 * an EEPROM that has been filled with zeros.
1295 * This in theory would disallow 64 K of legal
1296 * addresses assigned to Xerox, but it's
1297 * almost certain that those addresses haven't
1298 * been used for RTL80[01]2 chips anyway.
1300 if ((etheraddr[0] | etheraddr[1]) == 0 && w == 0) {
1305 etheraddr[2] = (w >> 8) & 0xff;
1306 etheraddr[3] = w & 0xff;
1311 etheraddr[4] = (w >> 8) & 0xff;
1312 etheraddr[5] = w & 0xff;
1318 WrNib(sc, CMR2, CMR2_IRQINV); /* back to page 0 */
1324 * Read the designated ethernet hardware address out of a 74S288
1327 * This is untested, since i haven't seen any adapter actually using
1328 * a 74S288. In the RTL 8012, only the serial EEPROM (94C46) is
1329 * supported anymore.
1332 rdp_gethwaddr_74s288(struct rdp_softc *sc, u_char *etheraddr)
1337 WrNib(sc, CMR2, CMR2_PAGE | CMR2_IRQINV); /* select page 1 */
1339 for (i = 0; i < 6; i++) {
1340 WrNib(sc, PCMR, i & 0x0f); /* lower 4 bit of addr */
1341 WrNib(sc, PCMR + HNib, HNib + 4); /* upper 2 bit addr + /CS */
1342 WrNib(sc, PCMR + HNib, HNib); /* latch data now */
1343 b = RdNib(sc, PDR) & 0x0f;
1344 b |= (RdNib(sc, PDR + HNib) & 0x0f) << 4;
1348 RdEnd(sc, PDR + HNib);
1349 WrNib(sc, CMR2, CMR2_IRQINV); /* reselect page 0 */
1353 * Send nbits of data (starting with MSB) out to the 93c46 as a
1354 * command. Assumes register page 1 has already been selected.
1357 rdp_93c46_cmd(struct rdp_softc *sc, u_short data, unsigned nbits)
1359 u_short mask = 1 << (nbits - 1);
1364 printf("rdp_93c46_cmd(): ");
1366 for (i = 0; i < nbits; i++, mask >>= 1) {
1367 b = HNib + PCMR_SK + PCMR_CS;
1371 printf("%d", b & 1);
1373 WrNib(sc, PCMR + HNib, b);
1375 WrNib(sc, PCMR + HNib, b & ~PCMR_SK);
1384 * Read one word of data from the 93c46. Actually, we have to read
1385 * 17 bits, and discard the very first bit. Assumes register page 1
1386 * to be selected as well.
1389 rdp_93c46_read(struct rdp_softc *sc)
1396 printf("rdp_93c46_read(): ");
1398 for (i = 0; i < 17; i++) {
1399 WrNib(sc, PCMR + HNib, PCMR_SK + PCMR_CS + HNib);
1401 WrNib(sc, PCMR + HNib, PCMR_CS + HNib);
1408 printf("%d", b & 1);
1418 WrNib(sc, PCMR + HNib, PCMR_SK + HNib);