2 * All Rights Reserved, Copyright (C) Fujitsu Limited 1995
4 * This software may be used, modified, copied, distributed, and sold, in
5 * both source and binary form provided that the above copyright, these
6 * terms and the following disclaimer are retained. The name of the author
7 * and/or the contributor may not be used to endorse or promote products
8 * derived from this software without specific prior written permission.
10 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND THE CONTRIBUTOR ``AS IS'' AND
11 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
12 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
13 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR THE CONTRIBUTOR BE LIABLE
14 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
15 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
16 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION.
17 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
18 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
19 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * $FreeBSD: src/sys/dev/fe/if_fe.c,v 1.65.2.1 2000/09/22 10:01:47 nyan Exp $
26 * Device driver for Fujitsu MB86960A/MB86965A based Ethernet cards.
27 * Contributed by M. Sekiguchi. <seki@sysrap.cs.fujitsu.co.jp>
29 * This version is intended to be a generic template for various
30 * MB86960A/MB86965A based Ethernet cards. It currently supports
31 * Fujitsu FMV-180 series for ISA and Allied-Telesis AT1700/RE2000
32 * series for ISA, as well as Fujitsu MBH10302 PC card.
33 * There are some currently-
34 * unused hooks embedded, which are primarily intended to support
35 * other types of Ethernet cards, but the author is not sure whether
38 * This version also includes some alignments to support RE1000,
39 * C-NET(98)P2 and so on. These cards are not for AT-compatibles,
40 * but for NEC PC-98 bus -- a proprietary bus architecture available
41 * only in Japan. Confusingly, it is different from the Microsoft's
42 * PC98 architecture. :-{
43 * Further work for PC-98 version will be available as a part of
44 * FreeBSD(98) project.
46 * This software is a derivative work of if_ed.c version 1.56 by David
47 * Greenman available as a part of FreeBSD 2.0 RELEASE source distribution.
49 * The following lines are retained from the original if_ed.c:
51 * Copyright (C) 1993, David Greenman. This software may be used, modified,
52 * copied, distributed, and sold, in both source and binary form provided
53 * that the above copyright and these terms are retained. Under no
54 * circumstances is the author responsible for the proper functioning
55 * of this software, nor does the author assume any responsibility
56 * for damages incurred with its use.
61 * o To support ISA PnP auto configuration for FMV-183/184.
62 * o To support REX-9886/87(PC-98 only).
63 * o To reconsider mbuf usage.
64 * o To reconsider transmission buffer usage, including
65 * transmission buffer size (currently 4KB x 2) and pros-and-
66 * cons of multiple frame transmission.
67 * o To test IPX codes.
68 * o To test new-bus frontend.
75 #include <sys/param.h>
76 #include <sys/systm.h>
77 #include <sys/socket.h>
78 #include <sys/sockio.h>
80 #include <sys/interrupt.h>
81 #include <sys/linker_set.h>
82 #include <sys/module.h>
85 #include <sys/thread2.h>
87 #include <net/ethernet.h>
89 #include <net/ifq_var.h>
90 #include <net/if_dl.h>
91 #include <net/if_mib.h>
92 #include <net/if_media.h>
94 #include <netinet/in.h>
95 #include <netinet/if_ether.h>
99 #include <machine_base/isa/ic/mb86960.h>
100 #include "if_fereg.h"
101 #include "if_fevar.h"
104 * Transmit just one packet per a "send" command to 86960.
105 * This option is intended for performance test. An EXPERIMENTAL option.
107 #ifndef FE_SINGLE_TRANSMISSION
108 #define FE_SINGLE_TRANSMISSION 0
112 * Maximum loops when interrupt.
113 * This option prevents an infinite loop due to hardware failure.
114 * (Some laptops make an infinite loop after PC-Card is ejected.)
117 #define FE_MAX_LOOP 0x800
121 * If you define this option, 8-bit cards are also supported.
123 /*#define FE_8BIT_SUPPORT*/
126 * Device configuration flags.
129 /* DLCR6 settings. */
130 #define FE_FLAGS_DLCR6_VALUE 0x007F
132 /* Force DLCR6 override. */
133 #define FE_FLAGS_OVERRIDE_DLCR6 0x0080
136 devclass_t fe_devclass;
139 * Special filter values.
141 static struct fe_filter const fe_filter_nothing = { FE_FILTER_NOTHING };
142 static struct fe_filter const fe_filter_all = { FE_FILTER_ALL };
144 /* Standard driver entry points. These can be static. */
145 static void fe_init (void *);
146 static void fe_intr (void *);
147 static int fe_ioctl (struct ifnet *, u_long, caddr_t,
149 static void fe_start (struct ifnet *,
150 struct ifaltq_subque *);
151 static void fe_watchdog (struct ifnet *);
152 static int fe_medchange (struct ifnet *);
153 static void fe_medstat (struct ifnet *, struct ifmediareq *);
155 /* Local functions. Order of declaration is confused. FIXME. */
156 static int fe_get_packet ( struct fe_softc *, u_short );
157 static void fe_tint ( struct fe_softc *, u_char );
158 static void fe_rint ( struct fe_softc *, u_char );
159 static void fe_xmit ( struct fe_softc * );
160 static void fe_write_mbufs ( struct fe_softc *, struct mbuf * );
161 static void fe_setmode ( struct fe_softc * );
162 static void fe_loadmar ( struct fe_softc * );
165 static void fe_emptybuffer ( struct fe_softc * );
168 DECLARE_DUMMY_MODULE(if_fe);
171 * Fe driver specific constants which relate to 86960/86965.
174 /* Interrupt masks */
175 #define FE_TMASK ( FE_D2_COLL16 | FE_D2_TXDONE )
176 #define FE_RMASK ( FE_D3_OVRFLO | FE_D3_CRCERR \
177 | FE_D3_ALGERR | FE_D3_SRTPKT | FE_D3_PKTRDY )
179 /* Maximum number of iterations for a receive interrupt. */
180 #define FE_MAX_RECV_COUNT ( ( 65536 - 2048 * 2 ) / 64 )
182 * Maximum size of SRAM is 65536,
183 * minimum size of transmission buffer in fe is 2x2KB,
184 * and minimum amount of received packet including headers
185 * added by the chip is 64 bytes.
186 * Hence FE_MAX_RECV_COUNT is the upper limit for number
187 * of packets in the receive buffer.
191 * Miscellaneous definitions not directly related to hardware.
194 /* The following line must be delete when "net/if_media.h" support it. */
196 #define IFM_10_FL /* 13 */ IFM_10_5
200 /* Mapping between media bitmap (in fe_softc.mbitmap) and ifm_media. */
201 static int const bit2media [] = {
202 IFM_HDX | IFM_ETHER | IFM_AUTO,
203 IFM_HDX | IFM_ETHER | IFM_MANUAL,
204 IFM_HDX | IFM_ETHER | IFM_10_T,
205 IFM_HDX | IFM_ETHER | IFM_10_2,
206 IFM_HDX | IFM_ETHER | IFM_10_5,
207 IFM_HDX | IFM_ETHER | IFM_10_FL,
208 IFM_FDX | IFM_ETHER | IFM_10_T,
209 /* More can be come here... */
213 /* Mapping between media bitmap (in fe_softc.mbitmap) and ifm_media. */
214 static int const bit2media [] = {
215 IFM_ETHER | IFM_AUTO,
216 IFM_ETHER | IFM_MANUAL,
217 IFM_ETHER | IFM_10_T,
218 IFM_ETHER | IFM_10_2,
219 IFM_ETHER | IFM_10_5,
220 IFM_ETHER | IFM_10_FL,
221 IFM_ETHER | IFM_10_T,
222 /* More can be come here... */
228 * Check for specific bits in specific registers have specific values.
229 * A common utility function called from various sub-probe routines.
232 fe_simple_probe (struct fe_softc const * sc,
233 struct fe_simple_probe_struct const * sp)
235 struct fe_simple_probe_struct const *p;
237 for (p = sp; p->mask != 0; p++) {
238 if ((fe_inb(sc, p->port) & p->mask) != p->bits)
244 /* Test if a given 6 byte value is a valid Ethernet station (MAC)
245 address. "Vendor" is an expected vendor code (first three bytes,)
246 or a zero when nothing expected. */
248 valid_Ether_p (u_char const * addr, unsigned vendor)
251 char ethstr[ETHER_ADDRSTRLEN + 1];
252 kprintf("fe?: validating %s against %06x\n", kether_ntoa(addr, ethstr),
256 /* All zero is not allowed as a vendor code. */
257 if (addr[0] == 0 && addr[1] == 0 && addr[2] == 0) return 0;
261 /* Legal Ethernet address (stored in ROM) must have
262 its Group and Local bits cleared. */
263 if ((addr[0] & 0x03) != 0) return 0;
266 /* Same as above, but a local address is allowed in
268 if ((addr[0] & 0x01) != 0) return 0;
271 /* Make sure the vendor part matches if one is given. */
272 if ( addr[0] != ((vendor >> 16) & 0xFF)
273 || addr[1] != ((vendor >> 8) & 0xFF)
274 || addr[2] != ((vendor ) & 0xFF)) return 0;
278 /* Host part must not be all-zeros nor all-ones. */
279 if (addr[3] == 0xFF && addr[4] == 0xFF && addr[5] == 0xFF) return 0;
280 if (addr[3] == 0x00 && addr[4] == 0x00 && addr[5] == 0x00) return 0;
282 /* Given addr looks like an Ethernet address. */
286 /* Fill our softc struct with default value. */
288 fe_softc_defaults (struct fe_softc *sc)
290 /* Prepare for typical register prototypes. We assume a
291 "typical" board has <32KB> of <fast> SRAM connected with a
292 <byte-wide> data lines. */
293 sc->proto_dlcr4 = FE_D4_LBC_DISABLE | FE_D4_CNTRL;
295 sc->proto_dlcr6 = FE_D6_BUFSIZ_32KB | FE_D6_TXBSIZ_2x4KB
296 | FE_D6_BBW_BYTE | FE_D6_SBW_WORD | FE_D6_SRAM_100ns;
297 sc->proto_dlcr7 = FE_D7_BYTSWP_LH;
298 sc->proto_bmpr13 = 0;
300 /* Assume the probe process (to be done later) is stable. */
303 /* A typical board needs no hooks. */
307 /* Assume the board has no software-controllable media selection. */
309 sc->defmedia = MB_HM;
313 /* Common error reporting routine used in probe routines for
314 "soft configured IRQ"-type boards. */
316 fe_irq_failure (char const *name, int unit, int irq, char const *list)
318 kprintf("fe%d: %s board is detected, but %s IRQ was given\n",
319 unit, name, (irq == NO_IRQ ? "no" : "invalid"));
321 kprintf("fe%d: specify an IRQ from %s in kernel config\n",
327 * Hardware (vendor) specific hooks.
331 * Generic media selection scheme for MB86965 based boards.
334 fe_msel_965 (struct fe_softc *sc)
338 /* Find the appropriate bits for BMPR13 tranceiver control. */
339 switch (IFM_SUBTYPE(sc->media.ifm_media)) {
340 case IFM_AUTO: b13 = FE_B13_PORT_AUTO | FE_B13_TPTYPE_UTP; break;
341 case IFM_10_T: b13 = FE_B13_PORT_TP | FE_B13_TPTYPE_UTP; break;
342 default: b13 = FE_B13_PORT_AUI; break;
345 /* Write it into the register. It takes effect immediately. */
346 fe_outb(sc, FE_BMPR13, sc->proto_bmpr13 | b13);
351 * Fujitsu MB86965 JLI mode support routines.
355 * Routines to read all bytes from the config EEPROM through MB86965A.
356 * It is a MicroWire (3-wire) serial EEPROM with 6-bit address.
360 fe_strobe_eeprom_jli (struct fe_softc *sc, u_short bmpr16)
363 * We must guarantee 1us (or more) interval to access slow
364 * EEPROMs. The following redundant code provides enough
365 * delay with ISA timing. (Even if the bus clock is "tuned.")
366 * Some modification will be needed on faster busses.
368 fe_outb(sc, bmpr16, FE_B16_SELECT);
369 fe_outb(sc, bmpr16, FE_B16_SELECT | FE_B16_CLOCK);
370 fe_outb(sc, bmpr16, FE_B16_SELECT | FE_B16_CLOCK);
371 fe_outb(sc, bmpr16, FE_B16_SELECT);
375 fe_read_eeprom_jli (struct fe_softc * sc, u_char * data)
378 u_char save16, save17;
381 /* Save the current value of the EEPROM interface registers. */
382 save16 = fe_inb(sc, FE_BMPR16);
383 save17 = fe_inb(sc, FE_BMPR17);
385 /* Read bytes from EEPROM; two bytes per an iteration. */
386 for (n = 0; n < JLI_EEPROM_SIZE / 2; n++) {
388 /* Reset the EEPROM interface. */
389 fe_outb(sc, FE_BMPR16, 0x00);
390 fe_outb(sc, FE_BMPR17, 0x00);
392 /* Start EEPROM access. */
393 fe_outb(sc, FE_BMPR16, FE_B16_SELECT);
394 fe_outb(sc, FE_BMPR17, FE_B17_DATA);
395 fe_strobe_eeprom_jli(sc, FE_BMPR16);
397 /* Pass the iteration count as well as a READ command. */
399 for (bit = 0x80; bit != 0x00; bit >>= 1) {
400 fe_outb(sc, FE_BMPR17, (val & bit) ? FE_B17_DATA : 0);
401 fe_strobe_eeprom_jli(sc, FE_BMPR16);
403 fe_outb(sc, FE_BMPR17, 0x00);
407 for (bit = 0x80; bit != 0x00; bit >>= 1) {
408 fe_strobe_eeprom_jli(sc, FE_BMPR16);
409 if (fe_inb(sc, FE_BMPR17) & FE_B17_DATA)
414 /* Read one more byte. */
416 for (bit = 0x80; bit != 0x00; bit >>= 1) {
417 fe_strobe_eeprom_jli(sc, FE_BMPR16);
418 if (fe_inb(sc, FE_BMPR17) & FE_B17_DATA)
425 /* Reset the EEPROM interface, again. */
426 fe_outb(sc, FE_BMPR16, 0x00);
427 fe_outb(sc, FE_BMPR17, 0x00);
429 /* Make sure to restore the original value of EEPROM interface
430 registers, since we are not yet sure we have MB86965A on
432 fe_outb(sc, FE_BMPR17, save17);
433 fe_outb(sc, FE_BMPR16, save16);
437 /* Report what we got. */
440 data -= JLI_EEPROM_SIZE;
441 for (i = 0; i < JLI_EEPROM_SIZE; i += 16) {
442 hexncpy(data + i, 16, hexstr, 48, " ");
443 kprintf("fe%d: EEPROM(JLI):%3x: %s\n",
444 sc->sc_unit, i, hexstr);
451 fe_init_jli (struct fe_softc * sc)
453 /* "Reset" by writing into a magic location. */
455 fe_outb(sc, 0x1E, fe_inb(sc, 0x1E));
461 * SSi 78Q8377A support routines.
465 * Routines to read all bytes from the config EEPROM through 78Q8377A.
466 * It is a MicroWire (3-wire) serial EEPROM with 8-bit address. (I.e.,
469 * As I don't have SSi manuals, (hmm, an old song again!) I'm not exactly
470 * sure the following code is correct... It is just stolen from the
471 * C-NET(98)P2 support routine in FreeBSD(98).
475 fe_read_eeprom_ssi (struct fe_softc *sc, u_char *data)
479 u_char save6, save7, save12;
482 /* Save the current value for the DLCR registers we are about
484 save6 = fe_inb(sc, FE_DLCR6);
485 save7 = fe_inb(sc, FE_DLCR7);
487 /* Put the 78Q8377A into a state that we can access the EEPROM. */
488 fe_outb(sc, FE_DLCR6,
489 FE_D6_BBW_WORD | FE_D6_SBW_WORD | FE_D6_DLC_DISABLE);
490 fe_outb(sc, FE_DLCR7,
491 FE_D7_BYTSWP_LH | FE_D7_RBS_BMPR | FE_D7_RDYPNS | FE_D7_POWER_UP);
493 /* Save the current value for the BMPR12 register, too. */
494 save12 = fe_inb(sc, FE_DLCR12);
496 /* Read bytes from EEPROM; two bytes per an iteration. */
497 for (n = 0; n < SSI_EEPROM_SIZE / 2; n++) {
499 /* Start EEPROM access */
500 fe_outb(sc, FE_DLCR12, SSI_EEP);
501 fe_outb(sc, FE_DLCR12, SSI_EEP | SSI_CSL);
503 /* Send the following four bits to the EEPROM in the
504 specified order: a dummy bit, a start bit, and
505 command bits (10) for READ. */
506 fe_outb(sc, FE_DLCR12, SSI_EEP | SSI_CSL );
507 fe_outb(sc, FE_DLCR12, SSI_EEP | SSI_CSL | SSI_CLK ); /* 0 */
508 fe_outb(sc, FE_DLCR12, SSI_EEP | SSI_CSL | SSI_DAT);
509 fe_outb(sc, FE_DLCR12, SSI_EEP | SSI_CSL | SSI_CLK | SSI_DAT); /* 1 */
510 fe_outb(sc, FE_DLCR12, SSI_EEP | SSI_CSL | SSI_DAT);
511 fe_outb(sc, FE_DLCR12, SSI_EEP | SSI_CSL | SSI_CLK | SSI_DAT); /* 1 */
512 fe_outb(sc, FE_DLCR12, SSI_EEP | SSI_CSL );
513 fe_outb(sc, FE_DLCR12, SSI_EEP | SSI_CSL | SSI_CLK ); /* 0 */
515 /* Pass the iteration count to the chip. */
516 for (bit = 0x80; bit != 0x00; bit >>= 1) {
517 val = ( n & bit ) ? SSI_DAT : 0;
518 fe_outb(sc, FE_DLCR12, SSI_EEP | SSI_CSL | val);
519 fe_outb(sc, FE_DLCR12, SSI_EEP | SSI_CSL | SSI_CLK | val);
524 for (bit = 0x80; bit != 0x00; bit >>= 1) {
525 fe_outb(sc, FE_DLCR12, SSI_EEP | SSI_CSL);
526 fe_outb(sc, FE_DLCR12, SSI_EEP | SSI_CSL | SSI_CLK);
527 if (fe_inb(sc, FE_DLCR12) & SSI_DIN)
532 /* Read one more byte. */
534 for (bit = 0x80; bit != 0x00; bit >>= 1) {
535 fe_outb(sc, FE_DLCR12, SSI_EEP | SSI_CSL);
536 fe_outb(sc, FE_DLCR12, SSI_EEP | SSI_CSL | SSI_CLK);
537 if (fe_inb(sc, FE_DLCR12) & SSI_DIN)
542 fe_outb(sc, FE_DLCR12, SSI_EEP);
545 /* Reset the EEPROM interface. (For now.) */
546 fe_outb(sc, FE_DLCR12, 0x00);
548 /* Restore the saved register values, for the case that we
549 didn't have 78Q8377A at the given address. */
550 fe_outb(sc, FE_DLCR12, save12);
551 fe_outb(sc, FE_DLCR7, save7);
552 fe_outb(sc, FE_DLCR6, save6);
555 /* Report what we got. */
558 data -= SSI_EEPROM_SIZE;
559 for (i = 0; i < SSI_EEPROM_SIZE; i += 16) {
560 hexncpy(data + i, 16, hexstr, 48, " ");
561 kprintf("fe%d: EEPROM(SSI):%3x: %s\n",
562 sc->sc_unit, i, hexstr);
569 * TDK/LANX boards support routines.
572 /* It is assumed that the CLK line is low and SDA is high (float) upon entry. */
573 #define LNX_PH(D,K,N) \
574 ((LNX_SDA_##D | LNX_CLK_##K) << N)
575 #define LNX_CYCLE(D1,D2,D3,D4,K1,K2,K3,K4) \
576 (LNX_PH(D1,K1,0)|LNX_PH(D2,K2,8)|LNX_PH(D3,K3,16)|LNX_PH(D4,K4,24))
578 #define LNX_CYCLE_START LNX_CYCLE(HI,LO,LO,HI, HI,HI,LO,LO)
579 #define LNX_CYCLE_STOP LNX_CYCLE(LO,LO,HI,HI, LO,HI,HI,LO)
580 #define LNX_CYCLE_HI LNX_CYCLE(HI,HI,HI,HI, LO,HI,LO,LO)
581 #define LNX_CYCLE_LO LNX_CYCLE(LO,LO,LO,HI, LO,HI,LO,LO)
582 #define LNX_CYCLE_INIT LNX_CYCLE(LO,HI,HI,HI, LO,LO,LO,LO)
585 fe_eeprom_cycle_lnx (struct fe_softc *sc, u_short reg20, u_long cycle)
587 fe_outb(sc, reg20, (cycle ) & 0xFF);
589 fe_outb(sc, reg20, (cycle >> 8) & 0xFF);
591 fe_outb(sc, reg20, (cycle >> 16) & 0xFF);
593 fe_outb(sc, reg20, (cycle >> 24) & 0xFF);
598 fe_eeprom_receive_lnx (struct fe_softc *sc, u_short reg20)
602 fe_outb(sc, reg20, LNX_CLK_HI | LNX_SDA_FL);
604 dat = fe_inb(sc, reg20);
605 fe_outb(sc, reg20, LNX_CLK_LO | LNX_SDA_FL);
607 return (dat & LNX_SDA_IN);
611 fe_read_eeprom_lnx (struct fe_softc *sc, u_char *data)
616 u_short reg20 = 0x14;
619 save20 = fe_inb(sc, reg20);
621 /* NOTE: DELAY() timing constants are approximately three
622 times longer (slower) than the required minimum. This is
623 to guarantee a reliable operation under some tough
624 conditions... Fortunately, this routine is only called
625 during the boot phase, so the speed is less important than
629 /* Reset the X24C01's internal state machine and put it into
630 the IDLE state. We usually don't need this, but *if*
631 someone (e.g., probe routine of other driver) write some
632 garbage into the register at 0x14, synchronization will be
633 lost, and the normal EEPROM access protocol won't work.
634 Moreover, as there are no easy way to reset, we need a
635 _manoeuvre_ here. (It even lacks a reset pin, so pushing
636 the RESET button on the PC doesn't help!) */
637 fe_eeprom_cycle_lnx(sc, reg20, LNX_CYCLE_INIT);
638 for (i = 0; i < 10; i++)
639 fe_eeprom_cycle_lnx(sc, reg20, LNX_CYCLE_START);
640 fe_eeprom_cycle_lnx(sc, reg20, LNX_CYCLE_STOP);
644 /* Issue a start condition. */
645 fe_eeprom_cycle_lnx(sc, reg20, LNX_CYCLE_START);
647 /* Send seven bits of the starting address (zero, in this
648 case) and a command bit for READ. */
650 for (bit = 0x80; bit != 0x00; bit >>= 1) {
652 fe_eeprom_cycle_lnx(sc, reg20, LNX_CYCLE_HI);
654 fe_eeprom_cycle_lnx(sc, reg20, LNX_CYCLE_LO);
658 /* Receive an ACK bit. */
659 if (fe_eeprom_receive_lnx(sc, reg20)) {
660 /* ACK was not received. EEPROM is not present (i.e.,
661 this board was not a TDK/LANX) or not working
664 kprintf("fe%d: no ACK received from EEPROM(LNX)\n",
667 /* Clear the given buffer to indicate we could not get
668 any info. and return. */
669 bzero(data, LNX_EEPROM_SIZE);
673 /* Read bytes from EEPROM. */
674 for (n = 0; n < LNX_EEPROM_SIZE; n++) {
676 /* Read a byte and store it into the buffer. */
678 for (bit = 0x80; bit != 0x00; bit >>= 1) {
679 if (fe_eeprom_receive_lnx(sc, reg20))
684 /* Acknowledge if we have to read more. */
685 if (n < LNX_EEPROM_SIZE - 1) {
686 fe_eeprom_cycle_lnx(sc, reg20, LNX_CYCLE_LO);
690 /* Issue a STOP condition, de-activating the clock line.
691 It will be safer to keep the clock line low than to leave
693 fe_eeprom_cycle_lnx(sc, reg20, LNX_CYCLE_STOP);
696 fe_outb(sc, reg20, save20);
699 /* Report what we got. */
701 data -= LNX_EEPROM_SIZE;
702 for (i = 0; i < LNX_EEPROM_SIZE; i += 16) {
703 hexncpy(data + i, 16, hexstr, 48, " ");
704 kprintf("fe%d: EEPROM(LNX):%3x: %s\n",
705 sc->sc_unit, i, hexstr);
712 fe_init_lnx (struct fe_softc * sc)
714 /* Reset the 86960. Do we need this? FIXME. */
715 fe_outb(sc, 0x12, 0x06);
717 fe_outb(sc, 0x12, 0x07);
720 /* Setup IRQ control register on the ASIC. */
721 fe_outb(sc, 0x14, sc->priv_info);
726 * Ungermann-Bass boards support routine.
729 fe_init_ubn (struct fe_softc * sc)
731 /* Do we need this? FIXME. */
732 fe_outb(sc, FE_DLCR7,
733 sc->proto_dlcr7 | FE_D7_RBS_BMPR | FE_D7_POWER_UP);
734 fe_outb(sc, 0x18, 0x00);
737 /* Setup IRQ control register on the ASIC. */
738 fe_outb(sc, 0x14, sc->priv_info);
743 * Install interface into kernel networking data structures
746 fe_attach (device_t dev)
748 struct fe_softc *sc = device_get_softc(dev);
749 int flags = device_get_flags(dev);
753 * Initialize ifnet structure
755 sc->sc_if.if_softc = sc;
756 if_initname(&(sc->sc_if), "fe", sc->sc_unit);
757 sc->sc_if.if_start = fe_start;
758 sc->sc_if.if_ioctl = fe_ioctl;
759 sc->sc_if.if_watchdog = fe_watchdog;
760 sc->sc_if.if_init = fe_init;
761 sc->sc_if.if_linkmib = &sc->mibdata;
762 sc->sc_if.if_linkmiblen = sizeof (sc->mibdata);
764 #if 0 /* I'm not sure... */
765 sc->mibdata.dot3Compliance = DOT3COMPLIANCE_COLLS;
769 * Set fixed interface flags.
771 sc->sc_if.if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
772 ifq_set_maxlen(&sc->sc_if.if_snd, IFQ_MAXLEN);
773 ifq_set_ready(&sc->sc_if.if_snd);
775 #if FE_SINGLE_TRANSMISSION
776 /* Override txb config to allocate minimum. */
777 sc->proto_dlcr6 &= ~FE_D6_TXBSIZ
778 sc->proto_dlcr6 |= FE_D6_TXBSIZ_2x2KB;
781 /* Modify hardware config if it is requested. */
782 if (flags & FE_FLAGS_OVERRIDE_DLCR6)
783 sc->proto_dlcr6 = flags & FE_FLAGS_DLCR6_VALUE;
785 /* Find TX buffer size, based on the hardware dependent proto. */
786 switch (sc->proto_dlcr6 & FE_D6_TXBSIZ) {
787 case FE_D6_TXBSIZ_2x2KB: sc->txb_size = 2048; break;
788 case FE_D6_TXBSIZ_2x4KB: sc->txb_size = 4096; break;
789 case FE_D6_TXBSIZ_2x8KB: sc->txb_size = 8192; break;
791 /* Oops, we can't work with single buffer configuration. */
793 kprintf("fe%d: strange TXBSIZ config; fixing\n",
796 sc->proto_dlcr6 &= ~FE_D6_TXBSIZ;
797 sc->proto_dlcr6 |= FE_D6_TXBSIZ_2x2KB;
802 /* Initialize the if_media interface. */
803 ifmedia_init(&sc->media, 0, fe_medchange, fe_medstat);
804 for (b = 0; bit2media[b] != 0; b++) {
805 if (sc->mbitmap & (1 << b)) {
806 ifmedia_add(&sc->media, bit2media[b], 0, NULL);
809 for (b = 0; bit2media[b] != 0; b++) {
810 if (sc->defmedia & (1 << b)) {
811 ifmedia_set(&sc->media, bit2media[b]);
815 #if 0 /* Turned off; this is called later, when the interface UPs. */
819 /* Attach and stop the interface. */
820 ether_ifattach(&sc->sc_if, sc->sc_enaddr, NULL);
822 ifq_set_cpuid(&sc->sc_if.if_snd, rman_get_cpuid(sc->irq_res));
826 error = bus_setup_intr(dev, sc->irq_res, INTR_MPSAFE,
827 fe_intr, sc, &sc->irq_handle,
828 sc->sc_if.if_serializer);
830 if_detach(&sc->sc_if);
831 fe_release_resource(dev);
835 /* Print additional info when attached. */
836 device_printf(dev, "type %s%s\n", sc->typestr,
837 (sc->proto_dlcr4 & FE_D4_DSC) ? ", full duplex" : "");
839 int buf, txb, bbw, sbw, ram;
841 buf = txb = bbw = sbw = ram = -1;
842 switch ( sc->proto_dlcr6 & FE_D6_BUFSIZ ) {
843 case FE_D6_BUFSIZ_8KB: buf = 8; break;
844 case FE_D6_BUFSIZ_16KB: buf = 16; break;
845 case FE_D6_BUFSIZ_32KB: buf = 32; break;
846 case FE_D6_BUFSIZ_64KB: buf = 64; break;
848 switch ( sc->proto_dlcr6 & FE_D6_TXBSIZ ) {
849 case FE_D6_TXBSIZ_2x2KB: txb = 2; break;
850 case FE_D6_TXBSIZ_2x4KB: txb = 4; break;
851 case FE_D6_TXBSIZ_2x8KB: txb = 8; break;
853 switch ( sc->proto_dlcr6 & FE_D6_BBW ) {
854 case FE_D6_BBW_BYTE: bbw = 8; break;
855 case FE_D6_BBW_WORD: bbw = 16; break;
857 switch ( sc->proto_dlcr6 & FE_D6_SBW ) {
858 case FE_D6_SBW_BYTE: sbw = 8; break;
859 case FE_D6_SBW_WORD: sbw = 16; break;
861 switch ( sc->proto_dlcr6 & FE_D6_SRAM ) {
862 case FE_D6_SRAM_100ns: ram = 100; break;
863 case FE_D6_SRAM_150ns: ram = 150; break;
865 device_printf(dev, "SRAM %dKB %dbit %dns, TXB %dKBx2, %dbit I/O\n",
866 buf, bbw, ram, txb, sbw);
868 if (sc->stability & UNSTABLE_IRQ)
869 device_printf(dev, "warning: IRQ number may be incorrect\n");
870 if (sc->stability & UNSTABLE_MAC)
871 device_printf(dev, "warning: above MAC address may be incorrect\n");
872 if (sc->stability & UNSTABLE_TYPE)
873 device_printf(dev, "warning: hardware type was not validated\n");
879 fe_alloc_port(device_t dev, int size)
881 struct fe_softc *sc = device_get_softc(dev);
882 struct resource *res;
886 res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
887 0ul, ~0ul, size, RF_ACTIVE);
889 sc->port_used = size;
891 sc->iot = rman_get_bustag(res);
892 sc->ioh = rman_get_bushandle(res);
900 fe_alloc_irq(device_t dev, int flags)
902 struct fe_softc *sc = device_get_softc(dev);
903 struct resource *res;
907 res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE | flags);
917 fe_release_resource(device_t dev)
919 struct fe_softc *sc = device_get_softc(dev);
922 bus_release_resource(dev, SYS_RES_IOPORT, 0, sc->port_res);
926 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->irq_res);
932 * Reset interface, after some (hardware) trouble is deteced.
935 fe_reset (struct fe_softc *sc)
937 /* Record how many packets are lost by this accident. */
938 IFNET_STAT_INC(&sc->sc_if, oerrors, sc->txb_sched + sc->txb_count);
939 sc->mibdata.dot3StatsInternalMacTransmitErrors++;
941 /* Put the interface into known initial state. */
943 if (sc->sc_if.if_flags & IFF_UP)
948 * Stop everything on the interface.
950 * All buffered packets, both transmitting and receiving,
951 * if any, will be lost by stopping the interface.
954 fe_stop (struct fe_softc *sc)
956 /* Disable interrupts. */
957 fe_outb(sc, FE_DLCR2, 0x00);
958 fe_outb(sc, FE_DLCR3, 0x00);
960 /* Stop interface hardware. */
962 fe_outb(sc, FE_DLCR6, sc->proto_dlcr6 | FE_D6_DLC_DISABLE);
965 /* Clear all interrupt status. */
966 fe_outb(sc, FE_DLCR0, 0xFF);
967 fe_outb(sc, FE_DLCR1, 0xFF);
969 /* Put the chip in stand-by mode. */
971 fe_outb(sc, FE_DLCR7, sc->proto_dlcr7 | FE_D7_POWER_DOWN);
974 /* Reset transmitter variables and interface flags. */
975 sc->sc_if.if_flags &= ~IFF_RUNNING;
976 ifq_clr_oactive(&sc->sc_if.if_snd);
977 sc->sc_if.if_timer = 0;
978 sc->txb_free = sc->txb_size;
982 /* MAR loading can be delayed. */
983 sc->filter_change = 0;
985 /* Call a device-specific hook. */
991 * Device timeout/watchdog routine. Entered if the device neglects to
992 * generate an interrupt after a transmit has been started on it.
995 fe_watchdog ( struct ifnet *ifp )
997 struct fe_softc *sc = (struct fe_softc *)ifp;
1000 /* A "debug" message. */
1001 kprintf("%s: transmission timeout (%d+%d)%s\n",
1002 ifp->if_xname, sc->txb_sched, sc->txb_count,
1003 (ifp->if_flags & IFF_UP) ? "" : " when down");
1004 IFNET_STAT_GET(&sc->sc_if, opackets, opkts);
1005 IFNET_STAT_GET(&sc->sc_if, ipackets, ipkts);
1006 if (opkts == 0 && ipkts == 0)
1007 kprintf("%s: wrong IRQ setting in config?\n", ifp->if_xname);
1012 * Initialize device.
1015 fe_init (void * xsc)
1017 struct fe_softc *sc = xsc;
1019 /* Start initializing 86960. */
1020 /* Call a hook before we start initializing the chip. */
1025 * Make sure to disable the chip, also.
1026 * This may also help re-programming the chip after
1027 * hot insertion of PCMCIAs.
1030 fe_outb(sc, FE_DLCR6, sc->proto_dlcr6 | FE_D6_DLC_DISABLE);
1033 /* Power up the chip and select register bank for DLCRs. */
1035 fe_outb(sc, FE_DLCR7,
1036 sc->proto_dlcr7 | FE_D7_RBS_DLCR | FE_D7_POWER_UP);
1039 /* Feed the station address. */
1040 fe_outblk(sc, FE_DLCR8, sc->sc_enaddr, ETHER_ADDR_LEN);
1042 /* Clear multicast address filter to receive nothing. */
1043 fe_outb(sc, FE_DLCR7,
1044 sc->proto_dlcr7 | FE_D7_RBS_MAR | FE_D7_POWER_UP);
1045 fe_outblk(sc, FE_MAR8, fe_filter_nothing.data, FE_FILTER_LEN);
1047 /* Select the BMPR bank for runtime register access. */
1048 fe_outb(sc, FE_DLCR7,
1049 sc->proto_dlcr7 | FE_D7_RBS_BMPR | FE_D7_POWER_UP);
1051 /* Initialize registers. */
1052 fe_outb(sc, FE_DLCR0, 0xFF); /* Clear all bits. */
1053 fe_outb(sc, FE_DLCR1, 0xFF); /* ditto. */
1054 fe_outb(sc, FE_DLCR2, 0x00);
1055 fe_outb(sc, FE_DLCR3, 0x00);
1056 fe_outb(sc, FE_DLCR4, sc->proto_dlcr4);
1057 fe_outb(sc, FE_DLCR5, sc->proto_dlcr5);
1058 fe_outb(sc, FE_BMPR10, 0x00);
1059 fe_outb(sc, FE_BMPR11, FE_B11_CTRL_SKIP | FE_B11_MODE1);
1060 fe_outb(sc, FE_BMPR12, 0x00);
1061 fe_outb(sc, FE_BMPR13, sc->proto_bmpr13);
1062 fe_outb(sc, FE_BMPR14, 0x00);
1063 fe_outb(sc, FE_BMPR15, 0x00);
1065 /* Enable interrupts. */
1066 fe_outb(sc, FE_DLCR2, FE_TMASK);
1067 fe_outb(sc, FE_DLCR3, FE_RMASK);
1069 /* Select requested media, just before enabling DLC. */
1073 /* Enable transmitter and receiver. */
1075 fe_outb(sc, FE_DLCR6, sc->proto_dlcr6 | FE_D6_DLC_ENABLE);
1080 * Make sure to empty the receive buffer.
1082 * This may be redundant, but *if* the receive buffer were full
1083 * at this point, then the driver would hang. I have experienced
1084 * some strange hang-up just after UP. I hope the following
1085 * code solve the problem.
1087 * I have changed the order of hardware initialization.
1088 * I think the receive buffer cannot have any packets at this
1089 * point in this version. The following code *must* be
1090 * redundant now. FIXME.
1092 * I've heard a rumore that on some PC card implementation of
1093 * 8696x, the receive buffer can have some data at this point.
1094 * The following message helps discovering the fact. FIXME.
1096 if (!(fe_inb(sc, FE_DLCR5) & FE_D5_BUFEMP)) {
1097 kprintf("fe%d: receive buffer has some data after reset\n",
1102 /* Do we need this here? Actually, no. I must be paranoia. */
1103 fe_outb(sc, FE_DLCR0, 0xFF); /* Clear all bits. */
1104 fe_outb(sc, FE_DLCR1, 0xFF); /* ditto. */
1107 /* Set 'running' flag, because we are now running. */
1108 sc->sc_if.if_flags |= IFF_RUNNING;
1111 * At this point, the interface is running properly,
1112 * except that it receives *no* packets. we then call
1113 * fe_setmode() to tell the chip what packets to be
1114 * received, based on the if_flags and multicast group
1115 * list. It completes the initialization process.
1120 /* ...and attempt to start output queued packets. */
1121 /* TURNED OFF, because the semi-auto media prober wants to UP
1122 the interface keeping it idle. The upper layer will soon
1123 start the interface anyway, and there are no significant
1125 if_devstart(&sc->sc_if);
1130 * This routine actually starts the transmission on the interface
1133 fe_xmit (struct fe_softc *sc)
1136 * Set a timer just in case we never hear from the board again.
1137 * We use longer timeout for multiple packet transmission.
1138 * I'm not sure this timer value is appropriate. FIXME.
1140 sc->sc_if.if_timer = 1 + sc->txb_count;
1142 /* Update txb variables. */
1143 sc->txb_sched = sc->txb_count;
1145 sc->txb_free = sc->txb_size;
1148 /* Start transmitter, passing packets in TX buffer. */
1149 fe_outb(sc, FE_BMPR10, sc->txb_sched | FE_B10_START);
1153 * Start output on interface.
1154 * We make two assumptions here:
1155 * 1) that the current priority is set to splimp _before_ this code
1156 * is called *and* is returned to the appropriate priority after
1158 * 2) that the OACTIVE flag is checked before this code is called
1159 * (i.e. that the output part of the interface is idle)
1162 fe_start (struct ifnet *ifp, struct ifaltq_subque *ifsq)
1164 struct fe_softc *sc = ifp->if_softc;
1167 ASSERT_ALTQ_SQ_DEFAULT(ifp, ifsq);
1170 /* Just a sanity check. */
1171 if ((sc->txb_count == 0) != (sc->txb_free == sc->txb_size)) {
1173 * Txb_count and txb_free co-works to manage the
1174 * transmission buffer. Txb_count keeps track of the
1175 * used potion of the buffer, while txb_free does unused
1176 * potion. So, as long as the driver runs properly,
1177 * txb_count is zero if and only if txb_free is same
1178 * as txb_size (which represents whole buffer.)
1180 kprintf("fe%d: inconsistent txb variables (%d, %d)\n",
1181 sc->sc_unit, sc->txb_count, sc->txb_free);
1183 * So, what should I do, then?
1185 * We now know txb_count and txb_free contradicts. We
1186 * cannot, however, tell which is wrong. More
1187 * over, we cannot peek 86960 transmission buffer or
1188 * reset the transmission buffer. (In fact, we can
1189 * reset the entire interface. I don't want to do it.)
1191 * If txb_count is incorrect, leaving it as-is will cause
1192 * sending of garbage after next interrupt. We have to
1193 * avoid it. Hence, we reset the txb_count here. If
1194 * txb_free was incorrect, resetting txb_count just loose
1195 * some packets. We can live with it.
1202 * First, see if there are buffered packets and an idle
1203 * transmitter - should never happen at this point.
1205 if ((sc->txb_count > 0) && (sc->txb_sched == 0)) {
1206 kprintf("fe%d: transmitter idle with %d buffered packets\n",
1207 sc->sc_unit, sc->txb_count);
1212 * Stop accepting more transmission packets temporarily, when
1213 * a filter change request is delayed. Updating the MARs on
1214 * 86960 flushes the transmission buffer, so it is delayed
1215 * until all buffered transmission packets have been sent
1218 if (sc->filter_change) {
1220 * Filter change request is delayed only when the DLC is
1221 * working. DLC soon raise an interrupt after finishing
1224 goto indicate_active;
1230 * See if there is room to put another packet in the buffer.
1231 * We *could* do better job by peeking the send queue to
1232 * know the length of the next packet. Current version just
1233 * tests against the worst case (i.e., longest packet). FIXME.
1235 * When adding the packet-peek feature, don't forget adding a
1236 * test on txb_count against QUEUEING_MAX.
1237 * There is a little chance the packet count exceeds
1238 * the limit. Assume transmission buffer is 8KB (2x8KB
1239 * configuration) and an application sends a bunch of small
1240 * (i.e., minimum packet sized) packets rapidly. An 8KB
1241 * buffer can hold 130 blocks of 62 bytes long...
1244 < ETHER_MAX_LEN - ETHER_CRC_LEN + FE_DATA_LEN_LEN) {
1246 goto indicate_active;
1249 #if FE_SINGLE_TRANSMISSION
1250 if (sc->txb_count > 0) {
1251 /* Just one packet per a transmission buffer. */
1252 goto indicate_active;
1257 * Get the next mbuf chain for a packet to send.
1259 m = ifq_dequeue(&sc->sc_if.if_snd, NULL);
1261 /* No more packets to send. */
1262 goto indicate_inactive;
1266 * Copy the mbuf chain into the transmission buffer.
1267 * txb_* variables are updated as necessary.
1269 fe_write_mbufs(sc, m);
1271 /* Start transmitter if it's idle. */
1272 if ((sc->txb_count > 0) && (sc->txb_sched == 0))
1276 * Tap off here if there is a bpf listener,
1277 * and the device is *not* in promiscuous mode.
1278 * (86960 receives self-generated packets if
1279 * and only if it is in "receive everything"
1282 if ((sc->sc_if.if_flags & IFF_PROMISC) == 0)
1283 BPF_MTAP(&sc->sc_if, m);
1290 * We are using the !OACTIVE flag to indicate to
1291 * the outside world that we can accept an
1292 * additional packet rather than that the
1293 * transmitter is _actually_ active. Indeed, the
1294 * transmitter may be active, but if we haven't
1295 * filled all the buffers with data then we still
1296 * want to accept more.
1298 ifq_clr_oactive(&sc->sc_if.if_snd);
1303 * The transmitter is active, and there are no room for
1304 * more outgoing packets in the transmission buffer.
1306 ifq_set_oactive(&sc->sc_if.if_snd);
1311 * Drop (skip) a packet from receive buffer in 86960 memory.
1314 fe_droppacket (struct fe_softc * sc, int len)
1319 * 86960 manual says that we have to read 8 bytes from the buffer
1320 * before skip the packets and that there must be more than 8 bytes
1321 * remaining in the buffer when issue a skip command.
1322 * Remember, we have already read 4 bytes before come here.
1325 /* Read 4 more bytes, and skip the rest of the packet. */
1326 #ifdef FE_8BIT_SUPPORT
1327 if ((sc->proto_dlcr6 & FE_D6_SBW) == FE_D6_SBW_BYTE)
1329 fe_inb(sc, FE_BMPR8);
1330 fe_inb(sc, FE_BMPR8);
1331 fe_inb(sc, FE_BMPR8);
1332 fe_inb(sc, FE_BMPR8);
1337 fe_inw(sc, FE_BMPR8);
1338 fe_inw(sc, FE_BMPR8);
1340 fe_outb(sc, FE_BMPR14, FE_B14_SKIP);
1342 /* We should not come here unless receiving RUNTs. */
1343 #ifdef FE_8BIT_SUPPORT
1344 if ((sc->proto_dlcr6 & FE_D6_SBW) == FE_D6_SBW_BYTE)
1346 for (i = 0; i < len; i++)
1347 fe_inb(sc, FE_BMPR8);
1352 for (i = 0; i < len; i += 2)
1353 fe_inw(sc, FE_BMPR8);
1360 * Empty receiving buffer.
1363 fe_emptybuffer (struct fe_softc * sc)
1369 kprintf("fe%d: emptying receive buffer\n", sc->sc_unit);
1373 * Stop receiving packets, temporarily.
1375 saved_dlcr5 = fe_inb(sc, FE_DLCR5);
1376 fe_outb(sc, FE_DLCR5, sc->proto_dlcr5);
1380 * When we come here, the receive buffer management may
1381 * have been broken. So, we cannot use skip operation.
1382 * Just discard everything in the buffer.
1384 #ifdef FE_8BIT_SUPPORT
1385 if ((sc->proto_dlcr6 & FE_D6_SBW) == FE_D6_SBW_BYTE)
1387 for (i = 0; i < 65536; i++) {
1388 if (fe_inb(sc, FE_DLCR5) & FE_D5_BUFEMP)
1390 fe_inb(sc, FE_BMPR8);
1396 for (i = 0; i < 65536; i += 2) {
1397 if (fe_inb(sc, FE_DLCR5) & FE_D5_BUFEMP)
1399 fe_inw(sc, FE_BMPR8);
1406 if (fe_inb(sc, FE_DLCR5) & FE_D5_BUFEMP) {
1407 kprintf("fe%d: could not empty receive buffer\n", sc->sc_unit);
1408 /* Hmm. What should I do if this happens? FIXME. */
1412 * Restart receiving packets.
1414 fe_outb(sc, FE_DLCR5, saved_dlcr5);
1419 * Transmission interrupt handler
1420 * The control flow of this function looks silly. FIXME.
1423 fe_tint (struct fe_softc * sc, u_char tstat)
1429 * Handle "excessive collision" interrupt.
1431 if (tstat & FE_D0_COLL16) {
1434 * Find how many packets (including this collided one)
1435 * are left unsent in transmission buffer.
1437 left = fe_inb(sc, FE_BMPR10);
1438 kprintf("fe%d: excessive collision (%d/%d)\n",
1439 sc->sc_unit, left, sc->txb_sched);
1442 * Clear the collision flag (in 86960) here
1443 * to avoid confusing statistics.
1445 fe_outb(sc, FE_DLCR0, FE_D0_COLLID);
1448 * Restart transmitter, skipping the
1451 * We *must* skip the packet to keep network running
1452 * properly. Excessive collision error is an
1453 * indication of the network overload. If we
1454 * tried sending the same packet after excessive
1455 * collision, the network would be filled with
1456 * out-of-time packets. Packets belonging
1457 * to reliable transport (such as TCP) are resent
1458 * by some upper layer.
1460 fe_outb(sc, FE_BMPR11, FE_B11_CTRL_SKIP | FE_B11_MODE1);
1462 /* Update statistics. */
1467 * Handle "transmission complete" interrupt.
1469 if (tstat & FE_D0_TXDONE) {
1472 * Add in total number of collisions on last
1473 * transmission. We also clear "collision occurred" flag
1476 * 86960 has a design flaw on collision count on multiple
1477 * packet transmission. When we send two or more packets
1478 * with one start command (that's what we do when the
1479 * transmission queue is crowded), 86960 informs us number
1480 * of collisions occurred on the last packet on the
1481 * transmission only. Number of collisions on previous
1482 * packets are lost. I have told that the fact is clearly
1483 * stated in the Fujitsu document.
1485 * I considered not to mind it seriously. Collision
1486 * count is not so important, anyway. Any comments? FIXME.
1489 if (fe_inb(sc, FE_DLCR0) & FE_D0_COLLID) {
1491 /* Clear collision flag. */
1492 fe_outb(sc, FE_DLCR0, FE_D0_COLLID);
1494 /* Extract collision count from 86960. */
1495 col = fe_inb(sc, FE_DLCR4);
1496 col = (col & FE_D4_COL) >> FE_D4_COL_SHIFT;
1499 * Status register indicates collisions,
1500 * while the collision count is zero.
1501 * This can happen after multiple packet
1502 * transmission, indicating that one or more
1503 * previous packet(s) had been collided.
1505 * Since the accurate number of collisions
1506 * has been lost, we just guess it as 1;
1507 * Am I too optimistic? FIXME.
1511 sc->sc_if.if_collisions += col;
1513 sc->mibdata.dot3StatsSingleCollisionFrames++;
1515 sc->mibdata.dot3StatsMultipleCollisionFrames++;
1516 sc->mibdata.dot3StatsCollFrequencies[col-1]++;
1520 * Update transmission statistics.
1521 * Be sure to reflect number of excessive collisions.
1523 col = sc->tx_excolls;
1524 IFNET_STAT_INC(&sc->sc_if, opackets, sc->txb_sched - col);
1525 IFNET_STAT_INC(&sc->sc_if, oerrors, col);
1526 IFNET_STAT_INC(&sc->sc_if, collisions, col * 16);
1527 sc->mibdata.dot3StatsExcessiveCollisions += col;
1528 sc->mibdata.dot3StatsCollFrequencies[15] += col;
1532 * The transmitter is no more active.
1533 * Reset output active flag and watchdog timer.
1535 ifq_clr_oactive(&sc->sc_if.if_snd);
1536 sc->sc_if.if_timer = 0;
1539 * If more data is ready to transmit in the buffer, start
1540 * transmitting them. Otherwise keep transmitter idle,
1541 * even if more data is queued. This gives receive
1542 * process a slight priority.
1544 if (sc->txb_count > 0)
1550 * Ethernet interface receiver interrupt.
1553 fe_rint (struct fe_softc * sc, u_char rstat)
1560 * Update statistics if this interrupt is caused by an error.
1561 * Note that, when the system was not sufficiently fast, the
1562 * receive interrupt might not be acknowledged immediately. If
1563 * one or more errornous frames were received before this routine
1564 * was scheduled, they are ignored, and the following error stats
1565 * give less than real values.
1567 if (rstat & (FE_D1_OVRFLO | FE_D1_CRCERR | FE_D1_ALGERR | FE_D1_SRTPKT)) {
1568 if (rstat & FE_D1_OVRFLO)
1569 sc->mibdata.dot3StatsInternalMacReceiveErrors++;
1570 if (rstat & FE_D1_CRCERR)
1571 sc->mibdata.dot3StatsFCSErrors++;
1572 if (rstat & FE_D1_ALGERR)
1573 sc->mibdata.dot3StatsAlignmentErrors++;
1575 /* The reference MAC receiver defined in 802.3
1576 silently ignores short frames (RUNTs) without
1577 notifying upper layer. RFC 1650 (dot3 MIB) is
1578 based on the 802.3, and it has no stats entry for
1580 if (rstat & FE_D1_SRTPKT)
1581 sc->mibdata.dot3StatsFrameTooShorts++; /* :-) */
1583 IFNET_STAT_INC(&sc->sc_if, ierrors, 1);
1587 * MB86960 has a flag indicating "receive queue empty."
1588 * We just loop, checking the flag, to pull out all received
1591 * We limit the number of iterations to avoid infinite-loop.
1592 * The upper bound is set to unrealistic high value.
1594 for (i = 0; i < FE_MAX_RECV_COUNT * 2; i++) {
1596 /* Stop the iteration if 86960 indicates no packets. */
1597 if (fe_inb(sc, FE_DLCR5) & FE_D5_BUFEMP)
1601 * Extract a receive status byte.
1602 * As our 86960 is in 16 bit bus access mode, we have to
1603 * use inw() to get the status byte. The significant
1604 * value is returned in lower 8 bits.
1606 #ifdef FE_8BIT_SUPPORT
1607 if ((sc->proto_dlcr6 & FE_D6_SBW) == FE_D6_SBW_BYTE)
1609 status = fe_inb(sc, FE_BMPR8);
1610 fe_inb(sc, FE_BMPR8);
1615 status = (u_char) fe_inw(sc, FE_BMPR8);
1619 * Extract the packet length.
1620 * It is a sum of a header (14 bytes) and a payload.
1621 * CRC has been stripped off by the 86960.
1623 #ifdef FE_8BIT_SUPPORT
1624 if ((sc->proto_dlcr6 & FE_D6_SBW) == FE_D6_SBW_BYTE)
1626 len = fe_inb(sc, FE_BMPR8);
1627 len |= (fe_inb(sc, FE_BMPR8) << 8);
1632 len = fe_inw(sc, FE_BMPR8);
1636 * AS our 86960 is programed to ignore errored frame,
1637 * we must not see any error indication in the
1638 * receive buffer. So, any error condition is a
1639 * serious error, e.g., out-of-sync of the receive
1642 if ((status & 0xF0) != 0x20 ||
1643 len > ETHER_MAX_LEN - ETHER_CRC_LEN ||
1644 len < ETHER_MIN_LEN - ETHER_CRC_LEN) {
1645 kprintf("fe%d: RX buffer out-of-sync\n", sc->sc_unit);
1646 IFNET_STAT_INC(&sc->sc_if, ierrors, 1);
1647 sc->mibdata.dot3StatsInternalMacReceiveErrors++;
1655 if (fe_get_packet(sc, len) < 0) {
1657 * Negative return from fe_get_packet()
1658 * indicates no available mbuf. We stop
1659 * receiving packets, even if there are more
1660 * in the buffer. We hope we can get more
1663 IFNET_STAT_INC(&sc->sc_if, ierrors, 1);
1664 sc->mibdata.dot3StatsMissedFrames++;
1665 fe_droppacket(sc, len);
1669 /* Successfully received a packet. Update stat. */
1670 IFNET_STAT_INC(&sc->sc_if, ipackets, 1);
1673 /* Maximum number of frames has been received. Something
1674 strange is happening here... */
1675 kprintf("fe%d: unusual receive flood\n", sc->sc_unit);
1676 sc->mibdata.dot3StatsInternalMacReceiveErrors++;
1681 * Ethernet interface interrupt processor
1686 struct fe_softc *sc = arg;
1687 u_char tstat, rstat;
1688 int loop_count = FE_MAX_LOOP;
1690 /* Loop until there are no more new interrupt conditions. */
1691 while (loop_count-- > 0) {
1693 * Get interrupt conditions, masking unneeded flags.
1695 tstat = fe_inb(sc, FE_DLCR0) & FE_TMASK;
1696 rstat = fe_inb(sc, FE_DLCR1) & FE_RMASK;
1697 if (tstat == 0 && rstat == 0)
1701 * Reset the conditions we are acknowledging.
1703 fe_outb(sc, FE_DLCR0, tstat);
1704 fe_outb(sc, FE_DLCR1, rstat);
1707 * Handle transmitter interrupts.
1713 * Handle receiver interrupts
1719 * Update the multicast address filter if it is
1720 * needed and possible. We do it now, because
1721 * we can make sure the transmission buffer is empty,
1722 * and there is a good chance that the receive queue
1723 * is empty. It will minimize the possibility of
1726 if (sc->filter_change &&
1727 sc->txb_count == 0 && sc->txb_sched == 0) {
1729 ifq_clr_oactive(&sc->sc_if.if_snd);
1733 * If it looks like the transmitter can take more data,
1734 * attempt to start output on the interface. This is done
1735 * after handling the receiver interrupt to give the
1736 * receive operation priority.
1738 * BTW, I'm not sure in what case the OACTIVE is on at
1739 * this point. Is the following test redundant?
1741 * No. This routine polls for both transmitter and
1742 * receiver interrupts. 86960 can raise a receiver
1743 * interrupt when the transmission buffer is full.
1745 if (!ifq_is_oactive(&sc->sc_if.if_snd))
1746 if_devstart(&sc->sc_if);
1749 kprintf("fe%d: too many loops\n", sc->sc_unit);
1753 * Process an ioctl request. This code needs some work - it looks
1757 fe_ioctl (struct ifnet * ifp, u_long command, caddr_t data, struct ucred *cr)
1759 struct fe_softc *sc = ifp->if_softc;
1760 struct ifreq *ifr = (struct ifreq *)data;
1766 * Switch interface state between "running" and
1767 * "stopped", reflecting the UP flag.
1769 if (sc->sc_if.if_flags & IFF_UP) {
1770 if ((sc->sc_if.if_flags & IFF_RUNNING) == 0)
1773 if ((sc->sc_if.if_flags & IFF_RUNNING) != 0)
1778 * Promiscuous and/or multicast flags may have changed,
1779 * so reprogram the multicast filter and/or receive mode.
1789 * Multicast list has changed; set the hardware filter
1797 /* Let if_media to handle these commands and to call
1799 error = ifmedia_ioctl(ifp, ifr, &sc->media, command);
1803 error = ether_ioctl(ifp, command, data);
1810 * Retrieve packet from receive buffer and send to the next level up via
1812 * Returns 0 if success, -1 if error (i.e., mbuf allocation failure).
1815 fe_get_packet (struct fe_softc * sc, u_short len)
1817 struct ether_header *eh;
1821 * NFS wants the data be aligned to the word (4 byte)
1822 * boundary. Ethernet header has 14 bytes. There is a
1825 #define NFS_MAGIC_OFFSET 2
1828 * This function assumes that an Ethernet packet fits in an
1829 * mbuf (with a cluster attached when necessary.) On FreeBSD
1830 * 2.0 for x86, which is the primary target of this driver, an
1831 * mbuf cluster has 4096 bytes, and we are happy. On ancient
1832 * BSDs, such as vanilla 4.3 for 386, a cluster size was 1024,
1833 * however. If the following #error message were printed upon
1834 * compile, you need to rewrite this function.
1836 #if ( MCLBYTES < ETHER_MAX_LEN - ETHER_CRC_LEN + NFS_MAGIC_OFFSET )
1837 #error "Too small MCLBYTES to use fe driver."
1841 * Our strategy has one more problem. There is a policy on
1842 * mbuf cluster allocation. It says that we must have at
1843 * least MINCLSIZE (208 bytes on FreeBSD 2.0 for x86) to
1844 * allocate a cluster. For a packet of a size between
1845 * (MHLEN - 2) to (MINCLSIZE - 2), our code violates the rule...
1846 * On the other hand, the current code is short, simple,
1847 * and fast, however. It does no harmful thing, just waists
1848 * some memory. Any comments? FIXME.
1851 /* Allocate an mbuf with packet header info. */
1852 MGETHDR(m, MB_DONTWAIT, MT_DATA);
1856 /* Attach a cluster if this packet doesn't fit in a normal mbuf. */
1857 if (len > MHLEN - NFS_MAGIC_OFFSET) {
1858 MCLGET(m, MB_DONTWAIT);
1859 if (!(m->m_flags & M_EXT)) {
1865 /* Initialize packet header info. */
1866 m->m_pkthdr.rcvif = &sc->sc_if;
1867 m->m_pkthdr.len = len;
1869 /* Set the length of this packet. */
1872 /* The following silliness is to make NFS happy */
1873 m->m_data += NFS_MAGIC_OFFSET;
1875 /* Get (actually just point to) the header part. */
1876 eh = mtod(m, struct ether_header *);
1879 #ifdef FE_8BIT_SUPPORT
1880 if ((sc->proto_dlcr6 & FE_D6_SBW) == FE_D6_SBW_BYTE)
1882 fe_insb(sc, FE_BMPR8, (u_int8_t *)eh, len);
1887 fe_insw(sc, FE_BMPR8, (u_int16_t *)eh, (len + 1) >> 1);
1890 /* Feed the packet to upper layer. */
1891 sc->sc_if.if_input(&sc->sc_if, m);
1896 * Write an mbuf chain to the transmission buffer memory using 16 bit PIO.
1897 * Returns number of bytes actually written, including length word.
1899 * If an mbuf chain is too long for an Ethernet frame, it is not sent.
1900 * Packets shorter than Ethernet minimum are legal, and we pad them
1901 * before sending out. An exception is "partial" packets which are
1902 * shorter than mandatory Ethernet header.
1905 fe_write_mbufs (struct fe_softc *sc, struct mbuf *m)
1907 u_short length, len;
1910 u_short savebyte; /* WARNING: Architecture dependent! */
1911 #define NO_PENDING_BYTE 0xFFFF
1913 static u_char padding [ETHER_MIN_LEN - ETHER_CRC_LEN - ETHER_HDR_LEN];
1916 /* First, count up the total number of bytes to copy */
1918 for (mp = m; mp != NULL; mp = mp->m_next)
1919 length += mp->m_len;
1921 /* Check if this matches the one in the packet header. */
1922 if (length != m->m_pkthdr.len) {
1923 kprintf("fe%d: packet length mismatch? (%d/%d)\n", sc->sc_unit,
1924 length, m->m_pkthdr.len);
1927 /* Just use the length value in the packet header. */
1928 length = m->m_pkthdr.len;
1933 * Should never send big packets. If such a packet is passed,
1934 * it should be a bug of upper layer. We just ignore it.
1935 * ... Partial (too short) packets, neither.
1937 if (length < ETHER_HDR_LEN ||
1938 length > ETHER_MAX_LEN - ETHER_CRC_LEN) {
1939 kprintf("fe%d: got an out-of-spec packet (%u bytes) to send\n",
1940 sc->sc_unit, length);
1941 IFNET_STAT_INC(&sc->sc_if, oerrors, 1);
1942 sc->mibdata.dot3StatsInternalMacTransmitErrors++;
1948 * Put the length word for this frame.
1949 * Does 86960 accept odd length? -- Yes.
1950 * Do we need to pad the length to minimum size by ourselves?
1951 * -- Generally yes. But for (or will be) the last
1952 * packet in the transmission buffer, we can skip the
1953 * padding process. It may gain performance slightly. FIXME.
1955 #ifdef FE_8BIT_SUPPORT
1956 if ((sc->proto_dlcr6 & FE_D6_SBW) == FE_D6_SBW_BYTE)
1958 len = max(length, ETHER_MIN_LEN - ETHER_CRC_LEN);
1959 fe_outb(sc, FE_BMPR8, len & 0x00ff);
1960 fe_outb(sc, FE_BMPR8, (len & 0xff00) >> 8);
1965 fe_outw(sc, FE_BMPR8,
1966 max(length, ETHER_MIN_LEN - ETHER_CRC_LEN));
1970 * Update buffer status now.
1971 * Truncate the length up to an even number, since we use outw().
1973 #ifdef FE_8BIT_SUPPORT
1974 if ((sc->proto_dlcr6 & FE_D6_SBW) != FE_D6_SBW_BYTE)
1977 length = (length + 1) & ~1;
1979 sc->txb_free -= FE_DATA_LEN_LEN +
1980 max(length, ETHER_MIN_LEN - ETHER_CRC_LEN);
1984 * Transfer the data from mbuf chain to the transmission buffer.
1985 * MB86960 seems to require that data be transferred as words, and
1986 * only words. So that we require some extra code to patch
1987 * over odd-length mbufs.
1989 #ifdef FE_8BIT_SUPPORT
1990 if ((sc->proto_dlcr6 & FE_D6_SBW) == FE_D6_SBW_BYTE)
1992 /* 8-bit cards are easy. */
1993 for (mp = m; mp != NULL; mp = mp->m_next) {
1995 fe_outsb(sc, FE_BMPR8, mtod(mp, caddr_t),
2002 /* 16-bit cards are a pain. */
2003 savebyte = NO_PENDING_BYTE;
2004 for (mp = m; mp != NULL; mp = mp->m_next) {
2006 /* Ignore empty mbuf. */
2011 /* Find the actual data to send. */
2012 data = mtod(mp, caddr_t);
2014 /* Finish the last byte. */
2015 if (savebyte != NO_PENDING_BYTE) {
2016 fe_outw(sc, FE_BMPR8, savebyte | (*data << 8));
2019 savebyte = NO_PENDING_BYTE;
2022 /* output contiguous words */
2024 fe_outsw(sc, FE_BMPR8, (u_int16_t *)data,
2030 /* Save a remaining byte, if there is one. */
2035 /* Spit the last byte, if the length is odd. */
2036 if (savebyte != NO_PENDING_BYTE)
2037 fe_outw(sc, FE_BMPR8, savebyte);
2040 /* Pad to the Ethernet minimum length, if the packet is too short. */
2041 if (length < ETHER_MIN_LEN - ETHER_CRC_LEN) {
2042 #ifdef FE_8BIT_SUPPORT
2043 if ((sc->proto_dlcr6 & FE_D6_SBW) == FE_D6_SBW_BYTE)
2045 fe_outsb(sc, FE_BMPR8, padding,
2046 ETHER_MIN_LEN - ETHER_CRC_LEN - length);
2051 fe_outsw(sc, FE_BMPR8, (u_int16_t *)padding,
2052 (ETHER_MIN_LEN - ETHER_CRC_LEN - length) >> 1);
2058 * Compute hash value for an Ethernet address
2061 fe_hash ( u_char * ep )
2063 #define FE_HASH_MAGIC_NUMBER 0xEDB88320L
2065 u_long hash = 0xFFFFFFFFL;
2070 for ( i = ETHER_ADDR_LEN; --i >= 0; ) {
2072 for ( j = 8; --j >= 0; ) {
2075 if ( ( m ^ b ) & 1 ) hash ^= FE_HASH_MAGIC_NUMBER;
2079 return ( ( int )( hash >> 26 ) );
2083 * Compute the multicast address filter from the
2084 * list of multicast addresses we need to listen to.
2086 static struct fe_filter
2087 fe_mcaf ( struct fe_softc *sc )
2090 struct fe_filter filter;
2091 struct ifmultiaddr *ifma;
2093 char ethstr[ETHER_ADDRSTRLEN + 1];
2095 filter = fe_filter_nothing;
2096 TAILQ_FOREACH(ifma, &sc->arpcom.ac_if.if_multiaddrs, ifma_link) {
2097 if (ifma->ifma_addr->sa_family != AF_LINK)
2099 index = fe_hash(LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
2101 kprintf("fe%d: hash(%s) == %d\n",
2102 sc->sc_unit, kether_ntoa(enm->enm_addrlo, ethstr), index);
2105 filter.data[index >> 3] |= 1 << (index & 7);
2111 * Calculate a new "multicast packet filter" and put the 86960
2112 * receiver in appropriate mode.
2115 fe_setmode (struct fe_softc *sc)
2117 int flags = sc->sc_if.if_flags;
2120 * If the interface is not running, we postpone the update
2121 * process for receive modes and multicast address filter
2122 * until the interface is restarted. It reduces some
2123 * complicated job on maintaining chip states. (Earlier versions
2124 * of this driver had a bug on that point...)
2126 * To complete the trick, fe_init() calls fe_setmode() after
2127 * restarting the interface.
2129 if (!(flags & IFF_RUNNING))
2133 * Promiscuous mode is handled separately.
2135 if (flags & IFF_PROMISC) {
2137 * Program 86960 to receive all packets on the segment
2138 * including those directed to other stations.
2139 * Multicast filter stored in MARs are ignored
2140 * under this setting, so we don't need to update it.
2142 * Promiscuous mode in FreeBSD 2 is used solely by
2143 * BPF, and BPF only listens to valid (no error) packets.
2144 * So, we ignore erroneous ones even in this mode.
2145 * (Older versions of fe driver mistook the point.)
2147 fe_outb(sc, FE_DLCR5,
2148 sc->proto_dlcr5 | FE_D5_AFM0 | FE_D5_AFM1);
2149 sc->filter_change = 0;
2154 * Turn the chip to the normal (non-promiscuous) mode.
2156 fe_outb(sc, FE_DLCR5, sc->proto_dlcr5 | FE_D5_AFM1);
2159 * Find the new multicast filter value.
2161 if (flags & IFF_ALLMULTI)
2162 sc->filter = fe_filter_all;
2164 sc->filter = fe_mcaf(sc);
2165 sc->filter_change = 1;
2168 * We have to update the multicast filter in the 86960, A.S.A.P.
2170 * Note that the DLC (Data Link Control unit, i.e. transmitter
2171 * and receiver) must be stopped when feeding the filter, and
2172 * DLC trashes all packets in both transmission and receive
2173 * buffers when stopped.
2175 * To reduce the packet loss, we delay the filter update
2176 * process until buffers are empty.
2178 if (sc->txb_sched == 0 && sc->txb_count == 0 &&
2179 !(fe_inb(sc, FE_DLCR1) & FE_D1_PKTRDY)) {
2181 * Buffers are (apparently) empty. Load
2182 * the new filter value into MARs now.
2187 * Buffers are not empty. Mark that we have to update
2188 * the MARs. The new filter will be loaded by feintr()
2195 * Load a new multicast address filter into MARs.
2197 * The caller must have splimp'ed before fe_loadmar.
2198 * This function starts the DLC upon return. So it can be called only
2199 * when the chip is working, i.e., from the driver's point of view, when
2200 * a device is RUNNING. (I mistook the point in previous versions.)
2203 fe_loadmar (struct fe_softc * sc)
2205 /* Stop the DLC (transmitter and receiver). */
2207 fe_outb(sc, FE_DLCR6, sc->proto_dlcr6 | FE_D6_DLC_DISABLE);
2210 /* Select register bank 1 for MARs. */
2211 fe_outb(sc, FE_DLCR7, sc->proto_dlcr7 | FE_D7_RBS_MAR | FE_D7_POWER_UP);
2213 /* Copy filter value into the registers. */
2214 fe_outblk(sc, FE_MAR8, sc->filter.data, FE_FILTER_LEN);
2216 /* Restore the bank selection for BMPRs (i.e., runtime registers). */
2217 fe_outb(sc, FE_DLCR7,
2218 sc->proto_dlcr7 | FE_D7_RBS_BMPR | FE_D7_POWER_UP);
2220 /* Restart the DLC. */
2222 fe_outb(sc, FE_DLCR6, sc->proto_dlcr6 | FE_D6_DLC_ENABLE);
2225 /* We have just updated the filter. */
2226 sc->filter_change = 0;
2229 /* Change the media selection. */
2231 fe_medchange (struct ifnet *ifp)
2233 struct fe_softc *sc = (struct fe_softc *)ifp->if_softc;
2236 /* If_media should not pass any request for a media which this
2237 interface doesn't support. */
2240 for (b = 0; bit2media[b] != 0; b++) {
2241 if (bit2media[b] == sc->media.ifm_media) break;
2243 if (((1 << b) & sc->mbitmap) == 0) {
2244 kprintf("fe%d: got an unsupported media request (0x%x)\n",
2245 sc->sc_unit, sc->media.ifm_media);
2250 /* We don't actually change media when the interface is down.
2251 fe_init() will do the job, instead. Should we also wait
2252 until the transmission buffer being empty? Changing the
2253 media when we are sending a frame will cause two garbages
2254 on wires, one on old media and another on new. FIXME */
2255 if (sc->sc_if.if_flags & IFF_UP) {
2256 if (sc->msel) sc->msel(sc);
2262 /* I don't know how I can support media status callback... FIXME. */
2264 fe_medstat (struct ifnet *ifp __unused, struct ifmediareq *ifmr __unused)