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 $
25 * $DragonFly: src/sys/dev/netif/fe/if_fe.c,v 1.23 2005/11/28 17:13:42 dillon Exp $
27 * Device driver for Fujitsu MB86960A/MB86965A based Ethernet cards.
28 * Contributed by M. Sekiguchi. <seki@sysrap.cs.fujitsu.co.jp>
30 * This version is intended to be a generic template for various
31 * MB86960A/MB86965A based Ethernet cards. It currently supports
32 * Fujitsu FMV-180 series for ISA and Allied-Telesis AT1700/RE2000
33 * series for ISA, as well as Fujitsu MBH10302 PC card.
34 * There are some currently-
35 * unused hooks embedded, which are primarily intended to support
36 * other types of Ethernet cards, but the author is not sure whether
39 * This version also includes some alignments to support RE1000,
40 * C-NET(98)P2 and so on. These cards are not for AT-compatibles,
41 * but for NEC PC-98 bus -- a proprietary bus architecture available
42 * only in Japan. Confusingly, it is different from the Microsoft's
43 * PC98 architecture. :-{
44 * Further work for PC-98 version will be available as a part of
45 * FreeBSD(98) project.
47 * This software is a derivative work of if_ed.c version 1.56 by David
48 * Greenman available as a part of FreeBSD 2.0 RELEASE source distribution.
50 * The following lines are retained from the original if_ed.c:
52 * Copyright (C) 1993, David Greenman. This software may be used, modified,
53 * copied, distributed, and sold, in both source and binary form provided
54 * that the above copyright and these terms are retained. Under no
55 * circumstances is the author responsible for the proper functioning
56 * of this software, nor does the author assume any responsibility
57 * for damages incurred with its use.
62 * o To support ISA PnP auto configuration for FMV-183/184.
63 * o To support REX-9886/87(PC-98 only).
64 * o To reconsider mbuf usage.
65 * o To reconsider transmission buffer usage, including
66 * transmission buffer size (currently 4KB x 2) and pros-and-
67 * cons of multiple frame transmission.
68 * o To test IPX codes.
69 * o To test new-bus frontend.
76 #include <sys/param.h>
77 #include <sys/systm.h>
78 #include <sys/socket.h>
79 #include <sys/sockio.h>
81 #include <sys/interrupt.h>
82 #include <sys/linker_set.h>
83 #include <sys/module.h>
84 #include <sys/thread2.h>
87 #include <machine/bus.h>
89 #include <machine/resource.h>
91 #include <net/ethernet.h>
93 #include <net/ifq_var.h>
94 #include <net/if_dl.h>
95 #include <net/if_mib.h>
96 #include <net/if_media.h>
98 #include <netinet/in.h>
99 #include <netinet/if_ether.h>
103 #include <i386/isa/ic/mb86960.h>
104 #include "if_fereg.h"
105 #include "if_fevar.h"
108 * Transmit just one packet per a "send" command to 86960.
109 * This option is intended for performance test. An EXPERIMENTAL option.
111 #ifndef FE_SINGLE_TRANSMISSION
112 #define FE_SINGLE_TRANSMISSION 0
116 * Maximum loops when interrupt.
117 * This option prevents an infinite loop due to hardware failure.
118 * (Some laptops make an infinite loop after PC-Card is ejected.)
121 #define FE_MAX_LOOP 0x800
125 * If you define this option, 8-bit cards are also supported.
127 /*#define FE_8BIT_SUPPORT*/
130 * Device configuration flags.
133 /* DLCR6 settings. */
134 #define FE_FLAGS_DLCR6_VALUE 0x007F
136 /* Force DLCR6 override. */
137 #define FE_FLAGS_OVERRIDE_DLCR6 0x0080
140 devclass_t fe_devclass;
143 * Special filter values.
145 static struct fe_filter const fe_filter_nothing = { FE_FILTER_NOTHING };
146 static struct fe_filter const fe_filter_all = { FE_FILTER_ALL };
148 /* Standard driver entry points. These can be static. */
149 static void fe_init (void *);
150 static void fe_intr (void *);
151 static int fe_ioctl (struct ifnet *, u_long, caddr_t,
153 static void fe_start (struct ifnet *);
154 static void fe_watchdog (struct ifnet *);
155 static int fe_medchange (struct ifnet *);
156 static void fe_medstat (struct ifnet *, struct ifmediareq *);
158 /* Local functions. Order of declaration is confused. FIXME. */
159 static int fe_get_packet ( struct fe_softc *, u_short );
160 static void fe_tint ( struct fe_softc *, u_char );
161 static void fe_rint ( struct fe_softc *, u_char );
162 static void fe_xmit ( struct fe_softc * );
163 static void fe_write_mbufs ( struct fe_softc *, struct mbuf * );
164 static void fe_setmode ( struct fe_softc * );
165 static void fe_loadmar ( struct fe_softc * );
168 static void fe_emptybuffer ( struct fe_softc * );
171 DECLARE_DUMMY_MODULE(if_fe);
174 * Fe driver specific constants which relate to 86960/86965.
177 /* Interrupt masks */
178 #define FE_TMASK ( FE_D2_COLL16 | FE_D2_TXDONE )
179 #define FE_RMASK ( FE_D3_OVRFLO | FE_D3_CRCERR \
180 | FE_D3_ALGERR | FE_D3_SRTPKT | FE_D3_PKTRDY )
182 /* Maximum number of iterations for a receive interrupt. */
183 #define FE_MAX_RECV_COUNT ( ( 65536 - 2048 * 2 ) / 64 )
185 * Maximum size of SRAM is 65536,
186 * minimum size of transmission buffer in fe is 2x2KB,
187 * and minimum amount of received packet including headers
188 * added by the chip is 64 bytes.
189 * Hence FE_MAX_RECV_COUNT is the upper limit for number
190 * of packets in the receive buffer.
194 * Miscellaneous definitions not directly related to hardware.
197 /* The following line must be delete when "net/if_media.h" support it. */
199 #define IFM_10_FL /* 13 */ IFM_10_5
203 /* Mapping between media bitmap (in fe_softc.mbitmap) and ifm_media. */
204 static int const bit2media [] = {
205 IFM_HDX | IFM_ETHER | IFM_AUTO,
206 IFM_HDX | IFM_ETHER | IFM_MANUAL,
207 IFM_HDX | IFM_ETHER | IFM_10_T,
208 IFM_HDX | IFM_ETHER | IFM_10_2,
209 IFM_HDX | IFM_ETHER | IFM_10_5,
210 IFM_HDX | IFM_ETHER | IFM_10_FL,
211 IFM_FDX | IFM_ETHER | IFM_10_T,
212 /* More can be come here... */
216 /* Mapping between media bitmap (in fe_softc.mbitmap) and ifm_media. */
217 static int const bit2media [] = {
218 IFM_ETHER | IFM_AUTO,
219 IFM_ETHER | IFM_MANUAL,
220 IFM_ETHER | IFM_10_T,
221 IFM_ETHER | IFM_10_2,
222 IFM_ETHER | IFM_10_5,
223 IFM_ETHER | IFM_10_FL,
224 IFM_ETHER | IFM_10_T,
225 /* More can be come here... */
231 * Check for specific bits in specific registers have specific values.
232 * A common utility function called from various sub-probe routines.
235 fe_simple_probe (struct fe_softc const * sc,
236 struct fe_simple_probe_struct const * sp)
238 struct fe_simple_probe_struct const *p;
240 for (p = sp; p->mask != 0; p++) {
241 if ((fe_inb(sc, p->port) & p->mask) != p->bits)
247 /* Test if a given 6 byte value is a valid Ethernet station (MAC)
248 address. "Vendor" is an expected vendor code (first three bytes,)
249 or a zero when nothing expected. */
251 valid_Ether_p (u_char const * addr, unsigned vendor)
254 printf("fe?: validating %6D against %06x\n", addr, ":", vendor);
257 /* All zero is not allowed as a vendor code. */
258 if (addr[0] == 0 && addr[1] == 0 && addr[2] == 0) return 0;
262 /* Legal Ethernet address (stored in ROM) must have
263 its Group and Local bits cleared. */
264 if ((addr[0] & 0x03) != 0) return 0;
267 /* Same as above, but a local address is allowed in
269 if ((addr[0] & 0x01) != 0) return 0;
272 /* Make sure the vendor part matches if one is given. */
273 if ( addr[0] != ((vendor >> 16) & 0xFF)
274 || addr[1] != ((vendor >> 8) & 0xFF)
275 || addr[2] != ((vendor ) & 0xFF)) return 0;
279 /* Host part must not be all-zeros nor all-ones. */
280 if (addr[3] == 0xFF && addr[4] == 0xFF && addr[5] == 0xFF) return 0;
281 if (addr[3] == 0x00 && addr[4] == 0x00 && addr[5] == 0x00) return 0;
283 /* Given addr looks like an Ethernet address. */
287 /* Fill our softc struct with default value. */
289 fe_softc_defaults (struct fe_softc *sc)
291 /* Prepare for typical register prototypes. We assume a
292 "typical" board has <32KB> of <fast> SRAM connected with a
293 <byte-wide> data lines. */
294 sc->proto_dlcr4 = FE_D4_LBC_DISABLE | FE_D4_CNTRL;
296 sc->proto_dlcr6 = FE_D6_BUFSIZ_32KB | FE_D6_TXBSIZ_2x4KB
297 | FE_D6_BBW_BYTE | FE_D6_SBW_WORD | FE_D6_SRAM_100ns;
298 sc->proto_dlcr7 = FE_D7_BYTSWP_LH;
299 sc->proto_bmpr13 = 0;
301 /* Assume the probe process (to be done later) is stable. */
304 /* A typical board needs no hooks. */
308 /* Assume the board has no software-controllable media selection. */
310 sc->defmedia = MB_HM;
314 /* Common error reporting routine used in probe routines for
315 "soft configured IRQ"-type boards. */
317 fe_irq_failure (char const *name, int unit, int irq, char const *list)
319 printf("fe%d: %s board is detected, but %s IRQ was given\n",
320 unit, name, (irq == NO_IRQ ? "no" : "invalid"));
322 printf("fe%d: specify an IRQ from %s in kernel config\n",
328 * Hardware (vendor) specific hooks.
332 * Generic media selection scheme for MB86965 based boards.
335 fe_msel_965 (struct fe_softc *sc)
339 /* Find the appropriate bits for BMPR13 tranceiver control. */
340 switch (IFM_SUBTYPE(sc->media.ifm_media)) {
341 case IFM_AUTO: b13 = FE_B13_PORT_AUTO | FE_B13_TPTYPE_UTP; break;
342 case IFM_10_T: b13 = FE_B13_PORT_TP | FE_B13_TPTYPE_UTP; break;
343 default: b13 = FE_B13_PORT_AUI; break;
346 /* Write it into the register. It takes effect immediately. */
347 fe_outb(sc, FE_BMPR13, sc->proto_bmpr13 | b13);
352 * Fujitsu MB86965 JLI mode support routines.
356 * Routines to read all bytes from the config EEPROM through MB86965A.
357 * It is a MicroWire (3-wire) serial EEPROM with 6-bit address.
361 fe_strobe_eeprom_jli (struct fe_softc *sc, u_short bmpr16)
364 * We must guarantee 1us (or more) interval to access slow
365 * EEPROMs. The following redundant code provides enough
366 * delay with ISA timing. (Even if the bus clock is "tuned.")
367 * Some modification will be needed on faster busses.
369 fe_outb(sc, bmpr16, FE_B16_SELECT);
370 fe_outb(sc, bmpr16, FE_B16_SELECT | FE_B16_CLOCK);
371 fe_outb(sc, bmpr16, FE_B16_SELECT | FE_B16_CLOCK);
372 fe_outb(sc, bmpr16, FE_B16_SELECT);
376 fe_read_eeprom_jli (struct fe_softc * sc, u_char * data)
379 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 printf("fe%d: EEPROM(JLI):%3x: %16D\n",
443 sc->sc_unit, i, data + i, " ");
450 fe_init_jli (struct fe_softc * sc)
452 /* "Reset" by writing into a magic location. */
454 fe_outb(sc, 0x1E, fe_inb(sc, 0x1E));
460 * SSi 78Q8377A support routines.
464 * Routines to read all bytes from the config EEPROM through 78Q8377A.
465 * It is a MicroWire (3-wire) serial EEPROM with 8-bit address. (I.e.,
468 * As I don't have SSi manuals, (hmm, an old song again!) I'm not exactly
469 * sure the following code is correct... It is just stolen from the
470 * C-NET(98)P2 support routine in FreeBSD(98).
474 fe_read_eeprom_ssi (struct fe_softc *sc, u_char *data)
478 u_char save6, save7, save12;
480 /* Save the current value for the DLCR registers we are about
482 save6 = fe_inb(sc, FE_DLCR6);
483 save7 = fe_inb(sc, FE_DLCR7);
485 /* Put the 78Q8377A into a state that we can access the EEPROM. */
486 fe_outb(sc, FE_DLCR6,
487 FE_D6_BBW_WORD | FE_D6_SBW_WORD | FE_D6_DLC_DISABLE);
488 fe_outb(sc, FE_DLCR7,
489 FE_D7_BYTSWP_LH | FE_D7_RBS_BMPR | FE_D7_RDYPNS | FE_D7_POWER_UP);
491 /* Save the current value for the BMPR12 register, too. */
492 save12 = fe_inb(sc, FE_DLCR12);
494 /* Read bytes from EEPROM; two bytes per an iteration. */
495 for (n = 0; n < SSI_EEPROM_SIZE / 2; n++) {
497 /* Start EEPROM access */
498 fe_outb(sc, FE_DLCR12, SSI_EEP);
499 fe_outb(sc, FE_DLCR12, SSI_EEP | SSI_CSL);
501 /* Send the following four bits to the EEPROM in the
502 specified order: a dummy bit, a start bit, and
503 command bits (10) for READ. */
504 fe_outb(sc, FE_DLCR12, SSI_EEP | SSI_CSL );
505 fe_outb(sc, FE_DLCR12, SSI_EEP | SSI_CSL | SSI_CLK ); /* 0 */
506 fe_outb(sc, FE_DLCR12, SSI_EEP | SSI_CSL | SSI_DAT);
507 fe_outb(sc, FE_DLCR12, SSI_EEP | SSI_CSL | SSI_CLK | SSI_DAT); /* 1 */
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 );
511 fe_outb(sc, FE_DLCR12, SSI_EEP | SSI_CSL | SSI_CLK ); /* 0 */
513 /* Pass the iteration count to the chip. */
514 for (bit = 0x80; bit != 0x00; bit >>= 1) {
515 val = ( n & bit ) ? SSI_DAT : 0;
516 fe_outb(sc, FE_DLCR12, SSI_EEP | SSI_CSL | val);
517 fe_outb(sc, FE_DLCR12, SSI_EEP | SSI_CSL | SSI_CLK | val);
522 for (bit = 0x80; bit != 0x00; bit >>= 1) {
523 fe_outb(sc, FE_DLCR12, SSI_EEP | SSI_CSL);
524 fe_outb(sc, FE_DLCR12, SSI_EEP | SSI_CSL | SSI_CLK);
525 if (fe_inb(sc, FE_DLCR12) & SSI_DIN)
530 /* Read one more byte. */
532 for (bit = 0x80; bit != 0x00; bit >>= 1) {
533 fe_outb(sc, FE_DLCR12, SSI_EEP | SSI_CSL);
534 fe_outb(sc, FE_DLCR12, SSI_EEP | SSI_CSL | SSI_CLK);
535 if (fe_inb(sc, FE_DLCR12) & SSI_DIN)
540 fe_outb(sc, FE_DLCR12, SSI_EEP);
543 /* Reset the EEPROM interface. (For now.) */
544 fe_outb(sc, FE_DLCR12, 0x00);
546 /* Restore the saved register values, for the case that we
547 didn't have 78Q8377A at the given address. */
548 fe_outb(sc, FE_DLCR12, save12);
549 fe_outb(sc, FE_DLCR7, save7);
550 fe_outb(sc, FE_DLCR6, save6);
553 /* Report what we got. */
556 data -= SSI_EEPROM_SIZE;
557 for (i = 0; i < SSI_EEPROM_SIZE; i += 16) {
558 printf("fe%d: EEPROM(SSI):%3x: %16D\n",
559 sc->sc_unit, i, data + i, " ");
566 * TDK/LANX boards support routines.
569 /* It is assumed that the CLK line is low and SDA is high (float) upon entry. */
570 #define LNX_PH(D,K,N) \
571 ((LNX_SDA_##D | LNX_CLK_##K) << N)
572 #define LNX_CYCLE(D1,D2,D3,D4,K1,K2,K3,K4) \
573 (LNX_PH(D1,K1,0)|LNX_PH(D2,K2,8)|LNX_PH(D3,K3,16)|LNX_PH(D4,K4,24))
575 #define LNX_CYCLE_START LNX_CYCLE(HI,LO,LO,HI, HI,HI,LO,LO)
576 #define LNX_CYCLE_STOP LNX_CYCLE(LO,LO,HI,HI, LO,HI,HI,LO)
577 #define LNX_CYCLE_HI LNX_CYCLE(HI,HI,HI,HI, LO,HI,LO,LO)
578 #define LNX_CYCLE_LO LNX_CYCLE(LO,LO,LO,HI, LO,HI,LO,LO)
579 #define LNX_CYCLE_INIT LNX_CYCLE(LO,HI,HI,HI, LO,LO,LO,LO)
582 fe_eeprom_cycle_lnx (struct fe_softc *sc, u_short reg20, u_long cycle)
584 fe_outb(sc, reg20, (cycle ) & 0xFF);
586 fe_outb(sc, reg20, (cycle >> 8) & 0xFF);
588 fe_outb(sc, reg20, (cycle >> 16) & 0xFF);
590 fe_outb(sc, reg20, (cycle >> 24) & 0xFF);
595 fe_eeprom_receive_lnx (struct fe_softc *sc, u_short reg20)
599 fe_outb(sc, reg20, LNX_CLK_HI | LNX_SDA_FL);
601 dat = fe_inb(sc, reg20);
602 fe_outb(sc, reg20, LNX_CLK_LO | LNX_SDA_FL);
604 return (dat & LNX_SDA_IN);
608 fe_read_eeprom_lnx (struct fe_softc *sc, u_char *data)
613 u_short reg20 = 0x14;
615 save20 = fe_inb(sc, reg20);
617 /* NOTE: DELAY() timing constants are approximately three
618 times longer (slower) than the required minimum. This is
619 to guarantee a reliable operation under some tough
620 conditions... Fortunately, this routine is only called
621 during the boot phase, so the speed is less important than
625 /* Reset the X24C01's internal state machine and put it into
626 the IDLE state. We usually don't need this, but *if*
627 someone (e.g., probe routine of other driver) write some
628 garbage into the register at 0x14, synchronization will be
629 lost, and the normal EEPROM access protocol won't work.
630 Moreover, as there are no easy way to reset, we need a
631 _manoeuvre_ here. (It even lacks a reset pin, so pushing
632 the RESET button on the PC doesn't help!) */
633 fe_eeprom_cycle_lnx(sc, reg20, LNX_CYCLE_INIT);
634 for (i = 0; i < 10; i++)
635 fe_eeprom_cycle_lnx(sc, reg20, LNX_CYCLE_START);
636 fe_eeprom_cycle_lnx(sc, reg20, LNX_CYCLE_STOP);
640 /* Issue a start condition. */
641 fe_eeprom_cycle_lnx(sc, reg20, LNX_CYCLE_START);
643 /* Send seven bits of the starting address (zero, in this
644 case) and a command bit for READ. */
646 for (bit = 0x80; bit != 0x00; bit >>= 1) {
648 fe_eeprom_cycle_lnx(sc, reg20, LNX_CYCLE_HI);
650 fe_eeprom_cycle_lnx(sc, reg20, LNX_CYCLE_LO);
654 /* Receive an ACK bit. */
655 if (fe_eeprom_receive_lnx(sc, reg20)) {
656 /* ACK was not received. EEPROM is not present (i.e.,
657 this board was not a TDK/LANX) or not working
660 printf("fe%d: no ACK received from EEPROM(LNX)\n",
663 /* Clear the given buffer to indicate we could not get
664 any info. and return. */
665 bzero(data, LNX_EEPROM_SIZE);
669 /* Read bytes from EEPROM. */
670 for (n = 0; n < LNX_EEPROM_SIZE; n++) {
672 /* Read a byte and store it into the buffer. */
674 for (bit = 0x80; bit != 0x00; bit >>= 1) {
675 if (fe_eeprom_receive_lnx(sc, reg20))
680 /* Acknowledge if we have to read more. */
681 if (n < LNX_EEPROM_SIZE - 1) {
682 fe_eeprom_cycle_lnx(sc, reg20, LNX_CYCLE_LO);
686 /* Issue a STOP condition, de-activating the clock line.
687 It will be safer to keep the clock line low than to leave
689 fe_eeprom_cycle_lnx(sc, reg20, LNX_CYCLE_STOP);
692 fe_outb(sc, reg20, save20);
695 /* Report what we got. */
697 data -= LNX_EEPROM_SIZE;
698 for (i = 0; i < LNX_EEPROM_SIZE; i += 16) {
699 printf("fe%d: EEPROM(LNX):%3x: %16D\n",
700 sc->sc_unit, i, data + i, " ");
707 fe_init_lnx (struct fe_softc * sc)
709 /* Reset the 86960. Do we need this? FIXME. */
710 fe_outb(sc, 0x12, 0x06);
712 fe_outb(sc, 0x12, 0x07);
715 /* Setup IRQ control register on the ASIC. */
716 fe_outb(sc, 0x14, sc->priv_info);
721 * Ungermann-Bass boards support routine.
724 fe_init_ubn (struct fe_softc * sc)
726 /* Do we need this? FIXME. */
727 fe_outb(sc, FE_DLCR7,
728 sc->proto_dlcr7 | FE_D7_RBS_BMPR | FE_D7_POWER_UP);
729 fe_outb(sc, 0x18, 0x00);
732 /* Setup IRQ control register on the ASIC. */
733 fe_outb(sc, 0x14, sc->priv_info);
738 * Install interface into kernel networking data structures
741 fe_attach (device_t dev)
743 struct fe_softc *sc = device_get_softc(dev);
744 int flags = device_get_flags(dev);
748 * Initialize ifnet structure
750 sc->sc_if.if_softc = sc;
751 if_initname(&(sc->sc_if), "fe", sc->sc_unit);
752 sc->sc_if.if_start = fe_start;
753 sc->sc_if.if_ioctl = fe_ioctl;
754 sc->sc_if.if_watchdog = fe_watchdog;
755 sc->sc_if.if_init = fe_init;
756 sc->sc_if.if_linkmib = &sc->mibdata;
757 sc->sc_if.if_linkmiblen = sizeof (sc->mibdata);
759 #if 0 /* I'm not sure... */
760 sc->mibdata.dot3Compliance = DOT3COMPLIANCE_COLLS;
764 * Set fixed interface flags.
766 sc->sc_if.if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
767 ifq_set_maxlen(&sc->sc_if.if_snd, IFQ_MAXLEN);
768 ifq_set_ready(&sc->sc_if.if_snd);
770 #if FE_SINGLE_TRANSMISSION
771 /* Override txb config to allocate minimum. */
772 sc->proto_dlcr6 &= ~FE_D6_TXBSIZ
773 sc->proto_dlcr6 |= FE_D6_TXBSIZ_2x2KB;
776 /* Modify hardware config if it is requested. */
777 if (flags & FE_FLAGS_OVERRIDE_DLCR6)
778 sc->proto_dlcr6 = flags & FE_FLAGS_DLCR6_VALUE;
780 /* Find TX buffer size, based on the hardware dependent proto. */
781 switch (sc->proto_dlcr6 & FE_D6_TXBSIZ) {
782 case FE_D6_TXBSIZ_2x2KB: sc->txb_size = 2048; break;
783 case FE_D6_TXBSIZ_2x4KB: sc->txb_size = 4096; break;
784 case FE_D6_TXBSIZ_2x8KB: sc->txb_size = 8192; break;
786 /* Oops, we can't work with single buffer configuration. */
788 printf("fe%d: strange TXBSIZ config; fixing\n",
791 sc->proto_dlcr6 &= ~FE_D6_TXBSIZ;
792 sc->proto_dlcr6 |= FE_D6_TXBSIZ_2x2KB;
797 /* Initialize the if_media interface. */
798 ifmedia_init(&sc->media, 0, fe_medchange, fe_medstat);
799 for (b = 0; bit2media[b] != 0; b++) {
800 if (sc->mbitmap & (1 << b)) {
801 ifmedia_add(&sc->media, bit2media[b], 0, NULL);
804 for (b = 0; bit2media[b] != 0; b++) {
805 if (sc->defmedia & (1 << b)) {
806 ifmedia_set(&sc->media, bit2media[b]);
810 #if 0 /* Turned off; this is called later, when the interface UPs. */
814 /* Attach and stop the interface. */
815 ether_ifattach(&sc->sc_if, sc->sc_enaddr, NULL);
818 error = bus_setup_intr(dev, sc->irq_res, INTR_NETSAFE,
819 fe_intr, sc, &sc->irq_handle,
820 sc->sc_if.if_serializer);
822 if_detach(&sc->sc_if);
823 fe_release_resource(dev);
828 /* Print additional info when attached. */
829 device_printf(dev, "type %s%s\n", sc->typestr,
830 (sc->proto_dlcr4 & FE_D4_DSC) ? ", full duplex" : "");
832 int buf, txb, bbw, sbw, ram;
834 buf = txb = bbw = sbw = ram = -1;
835 switch ( sc->proto_dlcr6 & FE_D6_BUFSIZ ) {
836 case FE_D6_BUFSIZ_8KB: buf = 8; break;
837 case FE_D6_BUFSIZ_16KB: buf = 16; break;
838 case FE_D6_BUFSIZ_32KB: buf = 32; break;
839 case FE_D6_BUFSIZ_64KB: buf = 64; break;
841 switch ( sc->proto_dlcr6 & FE_D6_TXBSIZ ) {
842 case FE_D6_TXBSIZ_2x2KB: txb = 2; break;
843 case FE_D6_TXBSIZ_2x4KB: txb = 4; break;
844 case FE_D6_TXBSIZ_2x8KB: txb = 8; break;
846 switch ( sc->proto_dlcr6 & FE_D6_BBW ) {
847 case FE_D6_BBW_BYTE: bbw = 8; break;
848 case FE_D6_BBW_WORD: bbw = 16; break;
850 switch ( sc->proto_dlcr6 & FE_D6_SBW ) {
851 case FE_D6_SBW_BYTE: sbw = 8; break;
852 case FE_D6_SBW_WORD: sbw = 16; break;
854 switch ( sc->proto_dlcr6 & FE_D6_SRAM ) {
855 case FE_D6_SRAM_100ns: ram = 100; break;
856 case FE_D6_SRAM_150ns: ram = 150; break;
858 device_printf(dev, "SRAM %dKB %dbit %dns, TXB %dKBx2, %dbit I/O\n",
859 buf, bbw, ram, txb, sbw);
861 if (sc->stability & UNSTABLE_IRQ)
862 device_printf(dev, "warning: IRQ number may be incorrect\n");
863 if (sc->stability & UNSTABLE_MAC)
864 device_printf(dev, "warning: above MAC address may be incorrect\n");
865 if (sc->stability & UNSTABLE_TYPE)
866 device_printf(dev, "warning: hardware type was not validated\n");
872 fe_alloc_port(device_t dev, int size)
874 struct fe_softc *sc = device_get_softc(dev);
875 struct resource *res;
879 res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
880 0ul, ~0ul, size, RF_ACTIVE);
882 sc->port_used = size;
884 sc->iot = rman_get_bustag(res);
885 sc->ioh = rman_get_bushandle(res);
893 fe_alloc_irq(device_t dev, int flags)
895 struct fe_softc *sc = device_get_softc(dev);
896 struct resource *res;
900 res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE | flags);
910 fe_release_resource(device_t dev)
912 struct fe_softc *sc = device_get_softc(dev);
915 bus_release_resource(dev, SYS_RES_IOPORT, 0, sc->port_res);
919 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->irq_res);
925 * Reset interface, after some (hardware) trouble is deteced.
928 fe_reset (struct fe_softc *sc)
930 /* Record how many packets are lost by this accident. */
931 sc->sc_if.if_oerrors += sc->txb_sched + sc->txb_count;
932 sc->mibdata.dot3StatsInternalMacTransmitErrors++;
934 /* Put the interface into known initial state. */
936 if (sc->sc_if.if_flags & IFF_UP)
941 * Stop everything on the interface.
943 * All buffered packets, both transmitting and receiving,
944 * if any, will be lost by stopping the interface.
947 fe_stop (struct fe_softc *sc)
949 /* Disable interrupts. */
950 fe_outb(sc, FE_DLCR2, 0x00);
951 fe_outb(sc, FE_DLCR3, 0x00);
953 /* Stop interface hardware. */
955 fe_outb(sc, FE_DLCR6, sc->proto_dlcr6 | FE_D6_DLC_DISABLE);
958 /* Clear all interrupt status. */
959 fe_outb(sc, FE_DLCR0, 0xFF);
960 fe_outb(sc, FE_DLCR1, 0xFF);
962 /* Put the chip in stand-by mode. */
964 fe_outb(sc, FE_DLCR7, sc->proto_dlcr7 | FE_D7_POWER_DOWN);
967 /* Reset transmitter variables and interface flags. */
968 sc->sc_if.if_flags &= ~(IFF_OACTIVE | IFF_RUNNING);
969 sc->sc_if.if_timer = 0;
970 sc->txb_free = sc->txb_size;
974 /* MAR loading can be delayed. */
975 sc->filter_change = 0;
977 /* Call a device-specific hook. */
983 * Device timeout/watchdog routine. Entered if the device neglects to
984 * generate an interrupt after a transmit has been started on it.
987 fe_watchdog ( struct ifnet *ifp )
989 struct fe_softc *sc = (struct fe_softc *)ifp;
991 /* A "debug" message. */
992 printf("%s: transmission timeout (%d+%d)%s\n",
993 ifp->if_xname, sc->txb_sched, sc->txb_count,
994 (ifp->if_flags & IFF_UP) ? "" : " when down");
995 if (sc->sc_if.if_opackets == 0 && sc->sc_if.if_ipackets == 0)
996 printf("%s: wrong IRQ setting in config?\n", ifp->if_xname);
1001 * Initialize device.
1004 fe_init (void * xsc)
1006 struct fe_softc *sc = xsc;
1008 /* Start initializing 86960. */
1009 /* Call a hook before we start initializing the chip. */
1014 * Make sure to disable the chip, also.
1015 * This may also help re-programming the chip after
1016 * hot insertion of PCMCIAs.
1019 fe_outb(sc, FE_DLCR6, sc->proto_dlcr6 | FE_D6_DLC_DISABLE);
1022 /* Power up the chip and select register bank for DLCRs. */
1024 fe_outb(sc, FE_DLCR7,
1025 sc->proto_dlcr7 | FE_D7_RBS_DLCR | FE_D7_POWER_UP);
1028 /* Feed the station address. */
1029 fe_outblk(sc, FE_DLCR8, sc->sc_enaddr, ETHER_ADDR_LEN);
1031 /* Clear multicast address filter to receive nothing. */
1032 fe_outb(sc, FE_DLCR7,
1033 sc->proto_dlcr7 | FE_D7_RBS_MAR | FE_D7_POWER_UP);
1034 fe_outblk(sc, FE_MAR8, fe_filter_nothing.data, FE_FILTER_LEN);
1036 /* Select the BMPR bank for runtime register access. */
1037 fe_outb(sc, FE_DLCR7,
1038 sc->proto_dlcr7 | FE_D7_RBS_BMPR | FE_D7_POWER_UP);
1040 /* Initialize registers. */
1041 fe_outb(sc, FE_DLCR0, 0xFF); /* Clear all bits. */
1042 fe_outb(sc, FE_DLCR1, 0xFF); /* ditto. */
1043 fe_outb(sc, FE_DLCR2, 0x00);
1044 fe_outb(sc, FE_DLCR3, 0x00);
1045 fe_outb(sc, FE_DLCR4, sc->proto_dlcr4);
1046 fe_outb(sc, FE_DLCR5, sc->proto_dlcr5);
1047 fe_outb(sc, FE_BMPR10, 0x00);
1048 fe_outb(sc, FE_BMPR11, FE_B11_CTRL_SKIP | FE_B11_MODE1);
1049 fe_outb(sc, FE_BMPR12, 0x00);
1050 fe_outb(sc, FE_BMPR13, sc->proto_bmpr13);
1051 fe_outb(sc, FE_BMPR14, 0x00);
1052 fe_outb(sc, FE_BMPR15, 0x00);
1054 /* Enable interrupts. */
1055 fe_outb(sc, FE_DLCR2, FE_TMASK);
1056 fe_outb(sc, FE_DLCR3, FE_RMASK);
1058 /* Select requested media, just before enabling DLC. */
1062 /* Enable transmitter and receiver. */
1064 fe_outb(sc, FE_DLCR6, sc->proto_dlcr6 | FE_D6_DLC_ENABLE);
1069 * Make sure to empty the receive buffer.
1071 * This may be redundant, but *if* the receive buffer were full
1072 * at this point, then the driver would hang. I have experienced
1073 * some strange hang-up just after UP. I hope the following
1074 * code solve the problem.
1076 * I have changed the order of hardware initialization.
1077 * I think the receive buffer cannot have any packets at this
1078 * point in this version. The following code *must* be
1079 * redundant now. FIXME.
1081 * I've heard a rumore that on some PC card implementation of
1082 * 8696x, the receive buffer can have some data at this point.
1083 * The following message helps discovering the fact. FIXME.
1085 if (!(fe_inb(sc, FE_DLCR5) & FE_D5_BUFEMP)) {
1086 printf("fe%d: receive buffer has some data after reset\n",
1091 /* Do we need this here? Actually, no. I must be paranoia. */
1092 fe_outb(sc, FE_DLCR0, 0xFF); /* Clear all bits. */
1093 fe_outb(sc, FE_DLCR1, 0xFF); /* ditto. */
1096 /* Set 'running' flag, because we are now running. */
1097 sc->sc_if.if_flags |= IFF_RUNNING;
1100 * At this point, the interface is running properly,
1101 * except that it receives *no* packets. we then call
1102 * fe_setmode() to tell the chip what packets to be
1103 * received, based on the if_flags and multicast group
1104 * list. It completes the initialization process.
1109 /* ...and attempt to start output queued packets. */
1110 /* TURNED OFF, because the semi-auto media prober wants to UP
1111 the interface keeping it idle. The upper layer will soon
1112 start the interface anyway, and there are no significant
1114 fe_start(&sc->sc_if);
1119 * This routine actually starts the transmission on the interface
1122 fe_xmit (struct fe_softc *sc)
1125 * Set a timer just in case we never hear from the board again.
1126 * We use longer timeout for multiple packet transmission.
1127 * I'm not sure this timer value is appropriate. FIXME.
1129 sc->sc_if.if_timer = 1 + sc->txb_count;
1131 /* Update txb variables. */
1132 sc->txb_sched = sc->txb_count;
1134 sc->txb_free = sc->txb_size;
1137 /* Start transmitter, passing packets in TX buffer. */
1138 fe_outb(sc, FE_BMPR10, sc->txb_sched | FE_B10_START);
1142 * Start output on interface.
1143 * We make two assumptions here:
1144 * 1) that the current priority is set to splimp _before_ this code
1145 * is called *and* is returned to the appropriate priority after
1147 * 2) that the IFF_OACTIVE flag is checked before this code is called
1148 * (i.e. that the output part of the interface is idle)
1151 fe_start (struct ifnet *ifp)
1153 struct fe_softc *sc = ifp->if_softc;
1157 /* Just a sanity check. */
1158 if ((sc->txb_count == 0) != (sc->txb_free == sc->txb_size)) {
1160 * Txb_count and txb_free co-works to manage the
1161 * transmission buffer. Txb_count keeps track of the
1162 * used potion of the buffer, while txb_free does unused
1163 * potion. So, as long as the driver runs properly,
1164 * txb_count is zero if and only if txb_free is same
1165 * as txb_size (which represents whole buffer.)
1167 printf("fe%d: inconsistent txb variables (%d, %d)\n",
1168 sc->sc_unit, sc->txb_count, sc->txb_free);
1170 * So, what should I do, then?
1172 * We now know txb_count and txb_free contradicts. We
1173 * cannot, however, tell which is wrong. More
1174 * over, we cannot peek 86960 transmission buffer or
1175 * reset the transmission buffer. (In fact, we can
1176 * reset the entire interface. I don't want to do it.)
1178 * If txb_count is incorrect, leaving it as-is will cause
1179 * sending of garbage after next interrupt. We have to
1180 * avoid it. Hence, we reset the txb_count here. If
1181 * txb_free was incorrect, resetting txb_count just loose
1182 * some packets. We can live with it.
1189 * First, see if there are buffered packets and an idle
1190 * transmitter - should never happen at this point.
1192 if ((sc->txb_count > 0) && (sc->txb_sched == 0)) {
1193 printf("fe%d: transmitter idle with %d buffered packets\n",
1194 sc->sc_unit, sc->txb_count);
1199 * Stop accepting more transmission packets temporarily, when
1200 * a filter change request is delayed. Updating the MARs on
1201 * 86960 flushes the transmission buffer, so it is delayed
1202 * until all buffered transmission packets have been sent
1205 if (sc->filter_change) {
1207 * Filter change request is delayed only when the DLC is
1208 * working. DLC soon raise an interrupt after finishing
1211 goto indicate_active;
1217 * See if there is room to put another packet in the buffer.
1218 * We *could* do better job by peeking the send queue to
1219 * know the length of the next packet. Current version just
1220 * tests against the worst case (i.e., longest packet). FIXME.
1222 * When adding the packet-peek feature, don't forget adding a
1223 * test on txb_count against QUEUEING_MAX.
1224 * There is a little chance the packet count exceeds
1225 * the limit. Assume transmission buffer is 8KB (2x8KB
1226 * configuration) and an application sends a bunch of small
1227 * (i.e., minimum packet sized) packets rapidly. An 8KB
1228 * buffer can hold 130 blocks of 62 bytes long...
1231 < ETHER_MAX_LEN - ETHER_CRC_LEN + FE_DATA_LEN_LEN) {
1233 goto indicate_active;
1236 #if FE_SINGLE_TRANSMISSION
1237 if (sc->txb_count > 0) {
1238 /* Just one packet per a transmission buffer. */
1239 goto indicate_active;
1244 * Get the next mbuf chain for a packet to send.
1246 m = ifq_dequeue(&sc->sc_if.if_snd, NULL);
1248 /* No more packets to send. */
1249 goto indicate_inactive;
1253 * Copy the mbuf chain into the transmission buffer.
1254 * txb_* variables are updated as necessary.
1256 fe_write_mbufs(sc, m);
1258 /* Start transmitter if it's idle. */
1259 if ((sc->txb_count > 0) && (sc->txb_sched == 0))
1263 * Tap off here if there is a bpf listener,
1264 * and the device is *not* in promiscuous mode.
1265 * (86960 receives self-generated packets if
1266 * and only if it is in "receive everything"
1269 if ((sc->sc_if.if_flags & IFF_PROMISC) == 0)
1270 BPF_MTAP(&sc->sc_if, m);
1277 * We are using the !OACTIVE flag to indicate to
1278 * the outside world that we can accept an
1279 * additional packet rather than that the
1280 * transmitter is _actually_ active. Indeed, the
1281 * transmitter may be active, but if we haven't
1282 * filled all the buffers with data then we still
1283 * want to accept more.
1285 sc->sc_if.if_flags &= ~IFF_OACTIVE;
1290 * The transmitter is active, and there are no room for
1291 * more outgoing packets in the transmission buffer.
1293 sc->sc_if.if_flags |= IFF_OACTIVE;
1298 * Drop (skip) a packet from receive buffer in 86960 memory.
1301 fe_droppacket (struct fe_softc * sc, int len)
1306 * 86960 manual says that we have to read 8 bytes from the buffer
1307 * before skip the packets and that there must be more than 8 bytes
1308 * remaining in the buffer when issue a skip command.
1309 * Remember, we have already read 4 bytes before come here.
1312 /* Read 4 more bytes, and skip the rest of the packet. */
1313 #ifdef FE_8BIT_SUPPORT
1314 if ((sc->proto_dlcr6 & FE_D6_SBW) == FE_D6_SBW_BYTE)
1316 (void) fe_inb(sc, FE_BMPR8);
1317 (void) fe_inb(sc, FE_BMPR8);
1318 (void) fe_inb(sc, FE_BMPR8);
1319 (void) fe_inb(sc, FE_BMPR8);
1324 (void) fe_inw(sc, FE_BMPR8);
1325 (void) fe_inw(sc, FE_BMPR8);
1327 fe_outb(sc, FE_BMPR14, FE_B14_SKIP);
1329 /* We should not come here unless receiving RUNTs. */
1330 #ifdef FE_8BIT_SUPPORT
1331 if ((sc->proto_dlcr6 & FE_D6_SBW) == FE_D6_SBW_BYTE)
1333 for (i = 0; i < len; i++)
1334 (void) fe_inb(sc, FE_BMPR8);
1339 for (i = 0; i < len; i += 2)
1340 (void) fe_inw(sc, FE_BMPR8);
1347 * Empty receiving buffer.
1350 fe_emptybuffer (struct fe_softc * sc)
1356 printf("fe%d: emptying receive buffer\n", sc->sc_unit);
1360 * Stop receiving packets, temporarily.
1362 saved_dlcr5 = fe_inb(sc, FE_DLCR5);
1363 fe_outb(sc, FE_DLCR5, sc->proto_dlcr5);
1367 * When we come here, the receive buffer management may
1368 * have been broken. So, we cannot use skip operation.
1369 * Just discard everything in the buffer.
1371 #ifdef FE_8BIT_SUPPORT
1372 if ((sc->proto_dlcr6 & FE_D6_SBW) == FE_D6_SBW_BYTE)
1374 for (i = 0; i < 65536; i++) {
1375 if (fe_inb(sc, FE_DLCR5) & FE_D5_BUFEMP)
1377 (void) fe_inb(sc, FE_BMPR8);
1383 for (i = 0; i < 65536; i += 2) {
1384 if (fe_inb(sc, FE_DLCR5) & FE_D5_BUFEMP)
1386 (void) fe_inw(sc, FE_BMPR8);
1393 if (fe_inb(sc, FE_DLCR5) & FE_D5_BUFEMP) {
1394 printf("fe%d: could not empty receive buffer\n", sc->sc_unit);
1395 /* Hmm. What should I do if this happens? FIXME. */
1399 * Restart receiving packets.
1401 fe_outb(sc, FE_DLCR5, saved_dlcr5);
1406 * Transmission interrupt handler
1407 * The control flow of this function looks silly. FIXME.
1410 fe_tint (struct fe_softc * sc, u_char tstat)
1416 * Handle "excessive collision" interrupt.
1418 if (tstat & FE_D0_COLL16) {
1421 * Find how many packets (including this collided one)
1422 * are left unsent in transmission buffer.
1424 left = fe_inb(sc, FE_BMPR10);
1425 printf("fe%d: excessive collision (%d/%d)\n",
1426 sc->sc_unit, left, sc->txb_sched);
1429 * Clear the collision flag (in 86960) here
1430 * to avoid confusing statistics.
1432 fe_outb(sc, FE_DLCR0, FE_D0_COLLID);
1435 * Restart transmitter, skipping the
1438 * We *must* skip the packet to keep network running
1439 * properly. Excessive collision error is an
1440 * indication of the network overload. If we
1441 * tried sending the same packet after excessive
1442 * collision, the network would be filled with
1443 * out-of-time packets. Packets belonging
1444 * to reliable transport (such as TCP) are resent
1445 * by some upper layer.
1447 fe_outb(sc, FE_BMPR11, FE_B11_CTRL_SKIP | FE_B11_MODE1);
1449 /* Update statistics. */
1454 * Handle "transmission complete" interrupt.
1456 if (tstat & FE_D0_TXDONE) {
1459 * Add in total number of collisions on last
1460 * transmission. We also clear "collision occurred" flag
1463 * 86960 has a design flaw on collision count on multiple
1464 * packet transmission. When we send two or more packets
1465 * with one start command (that's what we do when the
1466 * transmission queue is crowded), 86960 informs us number
1467 * of collisions occurred on the last packet on the
1468 * transmission only. Number of collisions on previous
1469 * packets are lost. I have told that the fact is clearly
1470 * stated in the Fujitsu document.
1472 * I considered not to mind it seriously. Collision
1473 * count is not so important, anyway. Any comments? FIXME.
1476 if (fe_inb(sc, FE_DLCR0) & FE_D0_COLLID) {
1478 /* Clear collision flag. */
1479 fe_outb(sc, FE_DLCR0, FE_D0_COLLID);
1481 /* Extract collision count from 86960. */
1482 col = fe_inb(sc, FE_DLCR4);
1483 col = (col & FE_D4_COL) >> FE_D4_COL_SHIFT;
1486 * Status register indicates collisions,
1487 * while the collision count is zero.
1488 * This can happen after multiple packet
1489 * transmission, indicating that one or more
1490 * previous packet(s) had been collided.
1492 * Since the accurate number of collisions
1493 * has been lost, we just guess it as 1;
1494 * Am I too optimistic? FIXME.
1498 sc->sc_if.if_collisions += col;
1500 sc->mibdata.dot3StatsSingleCollisionFrames++;
1502 sc->mibdata.dot3StatsMultipleCollisionFrames++;
1503 sc->mibdata.dot3StatsCollFrequencies[col-1]++;
1507 * Update transmission statistics.
1508 * Be sure to reflect number of excessive collisions.
1510 col = sc->tx_excolls;
1511 sc->sc_if.if_opackets += sc->txb_sched - col;
1512 sc->sc_if.if_oerrors += col;
1513 sc->sc_if.if_collisions += col * 16;
1514 sc->mibdata.dot3StatsExcessiveCollisions += col;
1515 sc->mibdata.dot3StatsCollFrequencies[15] += col;
1519 * The transmitter is no more active.
1520 * Reset output active flag and watchdog timer.
1522 sc->sc_if.if_flags &= ~IFF_OACTIVE;
1523 sc->sc_if.if_timer = 0;
1526 * If more data is ready to transmit in the buffer, start
1527 * transmitting them. Otherwise keep transmitter idle,
1528 * even if more data is queued. This gives receive
1529 * process a slight priority.
1531 if (sc->txb_count > 0)
1537 * Ethernet interface receiver interrupt.
1540 fe_rint (struct fe_softc * sc, u_char rstat)
1547 * Update statistics if this interrupt is caused by an error.
1548 * Note that, when the system was not sufficiently fast, the
1549 * receive interrupt might not be acknowledged immediately. If
1550 * one or more errornous frames were received before this routine
1551 * was scheduled, they are ignored, and the following error stats
1552 * give less than real values.
1554 if (rstat & (FE_D1_OVRFLO | FE_D1_CRCERR | FE_D1_ALGERR | FE_D1_SRTPKT)) {
1555 if (rstat & FE_D1_OVRFLO)
1556 sc->mibdata.dot3StatsInternalMacReceiveErrors++;
1557 if (rstat & FE_D1_CRCERR)
1558 sc->mibdata.dot3StatsFCSErrors++;
1559 if (rstat & FE_D1_ALGERR)
1560 sc->mibdata.dot3StatsAlignmentErrors++;
1562 /* The reference MAC receiver defined in 802.3
1563 silently ignores short frames (RUNTs) without
1564 notifying upper layer. RFC 1650 (dot3 MIB) is
1565 based on the 802.3, and it has no stats entry for
1567 if (rstat & FE_D1_SRTPKT)
1568 sc->mibdata.dot3StatsFrameTooShorts++; /* :-) */
1570 sc->sc_if.if_ierrors++;
1574 * MB86960 has a flag indicating "receive queue empty."
1575 * We just loop, checking the flag, to pull out all received
1578 * We limit the number of iterations to avoid infinite-loop.
1579 * The upper bound is set to unrealistic high value.
1581 for (i = 0; i < FE_MAX_RECV_COUNT * 2; i++) {
1583 /* Stop the iteration if 86960 indicates no packets. */
1584 if (fe_inb(sc, FE_DLCR5) & FE_D5_BUFEMP)
1588 * Extract a receive status byte.
1589 * As our 86960 is in 16 bit bus access mode, we have to
1590 * use inw() to get the status byte. The significant
1591 * value is returned in lower 8 bits.
1593 #ifdef FE_8BIT_SUPPORT
1594 if ((sc->proto_dlcr6 & FE_D6_SBW) == FE_D6_SBW_BYTE)
1596 status = fe_inb(sc, FE_BMPR8);
1597 (void) fe_inb(sc, FE_BMPR8);
1602 status = (u_char) fe_inw(sc, FE_BMPR8);
1606 * Extract the packet length.
1607 * It is a sum of a header (14 bytes) and a payload.
1608 * CRC has been stripped off by the 86960.
1610 #ifdef FE_8BIT_SUPPORT
1611 if ((sc->proto_dlcr6 & FE_D6_SBW) == FE_D6_SBW_BYTE)
1613 len = fe_inb(sc, FE_BMPR8);
1614 len |= (fe_inb(sc, FE_BMPR8) << 8);
1619 len = fe_inw(sc, FE_BMPR8);
1623 * AS our 86960 is programed to ignore errored frame,
1624 * we must not see any error indication in the
1625 * receive buffer. So, any error condition is a
1626 * serious error, e.g., out-of-sync of the receive
1629 if ((status & 0xF0) != 0x20 ||
1630 len > ETHER_MAX_LEN - ETHER_CRC_LEN ||
1631 len < ETHER_MIN_LEN - ETHER_CRC_LEN) {
1632 printf("fe%d: RX buffer out-of-sync\n", sc->sc_unit);
1633 sc->sc_if.if_ierrors++;
1634 sc->mibdata.dot3StatsInternalMacReceiveErrors++;
1642 if (fe_get_packet(sc, len) < 0) {
1644 * Negative return from fe_get_packet()
1645 * indicates no available mbuf. We stop
1646 * receiving packets, even if there are more
1647 * in the buffer. We hope we can get more
1650 sc->sc_if.if_ierrors++;
1651 sc->mibdata.dot3StatsMissedFrames++;
1652 fe_droppacket(sc, len);
1656 /* Successfully received a packet. Update stat. */
1657 sc->sc_if.if_ipackets++;
1660 /* Maximum number of frames has been received. Something
1661 strange is happening here... */
1662 printf("fe%d: unusual receive flood\n", sc->sc_unit);
1663 sc->mibdata.dot3StatsInternalMacReceiveErrors++;
1668 * Ethernet interface interrupt processor
1673 struct fe_softc *sc = arg;
1674 u_char tstat, rstat;
1675 int loop_count = FE_MAX_LOOP;
1677 /* Loop until there are no more new interrupt conditions. */
1678 while (loop_count-- > 0) {
1680 * Get interrupt conditions, masking unneeded flags.
1682 tstat = fe_inb(sc, FE_DLCR0) & FE_TMASK;
1683 rstat = fe_inb(sc, FE_DLCR1) & FE_RMASK;
1684 if (tstat == 0 && rstat == 0)
1688 * Reset the conditions we are acknowledging.
1690 fe_outb(sc, FE_DLCR0, tstat);
1691 fe_outb(sc, FE_DLCR1, rstat);
1694 * Handle transmitter interrupts.
1700 * Handle receiver interrupts
1706 * Update the multicast address filter if it is
1707 * needed and possible. We do it now, because
1708 * we can make sure the transmission buffer is empty,
1709 * and there is a good chance that the receive queue
1710 * is empty. It will minimize the possibility of
1713 if (sc->filter_change &&
1714 sc->txb_count == 0 && sc->txb_sched == 0) {
1716 sc->sc_if.if_flags &= ~IFF_OACTIVE;
1720 * If it looks like the transmitter can take more data,
1721 * attempt to start output on the interface. This is done
1722 * after handling the receiver interrupt to give the
1723 * receive operation priority.
1725 * BTW, I'm not sure in what case the OACTIVE is on at
1726 * this point. Is the following test redundant?
1728 * No. This routine polls for both transmitter and
1729 * receiver interrupts. 86960 can raise a receiver
1730 * interrupt when the transmission buffer is full.
1732 if ((sc->sc_if.if_flags & IFF_OACTIVE) == 0)
1733 fe_start(&sc->sc_if);
1736 printf("fe%d: too many loops\n", sc->sc_unit);
1740 * Process an ioctl request. This code needs some work - it looks
1744 fe_ioctl (struct ifnet * ifp, u_long command, caddr_t data, struct ucred *cr)
1746 struct fe_softc *sc = ifp->if_softc;
1747 struct ifreq *ifr = (struct ifreq *)data;
1753 * Switch interface state between "running" and
1754 * "stopped", reflecting the UP flag.
1756 if (sc->sc_if.if_flags & IFF_UP) {
1757 if ((sc->sc_if.if_flags & IFF_RUNNING) == 0)
1760 if ((sc->sc_if.if_flags & IFF_RUNNING) != 0)
1765 * Promiscuous and/or multicast flags may have changed,
1766 * so reprogram the multicast filter and/or receive mode.
1776 * Multicast list has changed; set the hardware filter
1784 /* Let if_media to handle these commands and to call
1786 error = ifmedia_ioctl(ifp, ifr, &sc->media, command);
1790 error = ether_ioctl(ifp, command, data);
1797 * Retrieve packet from receive buffer and send to the next level up via
1799 * Returns 0 if success, -1 if error (i.e., mbuf allocation failure).
1802 fe_get_packet (struct fe_softc * sc, u_short len)
1804 struct ether_header *eh;
1808 * NFS wants the data be aligned to the word (4 byte)
1809 * boundary. Ethernet header has 14 bytes. There is a
1812 #define NFS_MAGIC_OFFSET 2
1815 * This function assumes that an Ethernet packet fits in an
1816 * mbuf (with a cluster attached when necessary.) On FreeBSD
1817 * 2.0 for x86, which is the primary target of this driver, an
1818 * mbuf cluster has 4096 bytes, and we are happy. On ancient
1819 * BSDs, such as vanilla 4.3 for 386, a cluster size was 1024,
1820 * however. If the following #error message were printed upon
1821 * compile, you need to rewrite this function.
1823 #if ( MCLBYTES < ETHER_MAX_LEN - ETHER_CRC_LEN + NFS_MAGIC_OFFSET )
1824 #error "Too small MCLBYTES to use fe driver."
1828 * Our strategy has one more problem. There is a policy on
1829 * mbuf cluster allocation. It says that we must have at
1830 * least MINCLSIZE (208 bytes on FreeBSD 2.0 for x86) to
1831 * allocate a cluster. For a packet of a size between
1832 * (MHLEN - 2) to (MINCLSIZE - 2), our code violates the rule...
1833 * On the other hand, the current code is short, simple,
1834 * and fast, however. It does no harmful thing, just waists
1835 * some memory. Any comments? FIXME.
1838 /* Allocate an mbuf with packet header info. */
1839 MGETHDR(m, MB_DONTWAIT, MT_DATA);
1843 /* Attach a cluster if this packet doesn't fit in a normal mbuf. */
1844 if (len > MHLEN - NFS_MAGIC_OFFSET) {
1845 MCLGET(m, MB_DONTWAIT);
1846 if (!(m->m_flags & M_EXT)) {
1852 /* Initialize packet header info. */
1853 m->m_pkthdr.rcvif = &sc->sc_if;
1854 m->m_pkthdr.len = len;
1856 /* Set the length of this packet. */
1859 /* The following silliness is to make NFS happy */
1860 m->m_data += NFS_MAGIC_OFFSET;
1862 /* Get (actually just point to) the header part. */
1863 eh = mtod(m, struct ether_header *);
1866 #ifdef FE_8BIT_SUPPORT
1867 if ((sc->proto_dlcr6 & FE_D6_SBW) == FE_D6_SBW_BYTE)
1869 fe_insb(sc, FE_BMPR8, (u_int8_t *)eh, len);
1874 fe_insw(sc, FE_BMPR8, (u_int16_t *)eh, (len + 1) >> 1);
1877 /* Feed the packet to upper layer. */
1878 sc->sc_if.if_input(&sc->sc_if, m);
1883 * Write an mbuf chain to the transmission buffer memory using 16 bit PIO.
1884 * Returns number of bytes actually written, including length word.
1886 * If an mbuf chain is too long for an Ethernet frame, it is not sent.
1887 * Packets shorter than Ethernet minimum are legal, and we pad them
1888 * before sending out. An exception is "partial" packets which are
1889 * shorter than mandatory Ethernet header.
1892 fe_write_mbufs (struct fe_softc *sc, struct mbuf *m)
1894 u_short length, len;
1897 u_short savebyte; /* WARNING: Architecture dependent! */
1898 #define NO_PENDING_BYTE 0xFFFF
1900 static u_char padding [ETHER_MIN_LEN - ETHER_CRC_LEN - ETHER_HDR_LEN];
1903 /* First, count up the total number of bytes to copy */
1905 for (mp = m; mp != NULL; mp = mp->m_next)
1906 length += mp->m_len;
1908 /* Check if this matches the one in the packet header. */
1909 if (length != m->m_pkthdr.len) {
1910 printf("fe%d: packet length mismatch? (%d/%d)\n", sc->sc_unit,
1911 length, m->m_pkthdr.len);
1914 /* Just use the length value in the packet header. */
1915 length = m->m_pkthdr.len;
1920 * Should never send big packets. If such a packet is passed,
1921 * it should be a bug of upper layer. We just ignore it.
1922 * ... Partial (too short) packets, neither.
1924 if (length < ETHER_HDR_LEN ||
1925 length > ETHER_MAX_LEN - ETHER_CRC_LEN) {
1926 printf("fe%d: got an out-of-spec packet (%u bytes) to send\n",
1927 sc->sc_unit, length);
1928 sc->sc_if.if_oerrors++;
1929 sc->mibdata.dot3StatsInternalMacTransmitErrors++;
1935 * Put the length word for this frame.
1936 * Does 86960 accept odd length? -- Yes.
1937 * Do we need to pad the length to minimum size by ourselves?
1938 * -- Generally yes. But for (or will be) the last
1939 * packet in the transmission buffer, we can skip the
1940 * padding process. It may gain performance slightly. FIXME.
1942 #ifdef FE_8BIT_SUPPORT
1943 if ((sc->proto_dlcr6 & FE_D6_SBW) == FE_D6_SBW_BYTE)
1945 len = max(length, ETHER_MIN_LEN - ETHER_CRC_LEN);
1946 fe_outb(sc, FE_BMPR8, len & 0x00ff);
1947 fe_outb(sc, FE_BMPR8, (len & 0xff00) >> 8);
1952 fe_outw(sc, FE_BMPR8,
1953 max(length, ETHER_MIN_LEN - ETHER_CRC_LEN));
1957 * Update buffer status now.
1958 * Truncate the length up to an even number, since we use outw().
1960 #ifdef FE_8BIT_SUPPORT
1961 if ((sc->proto_dlcr6 & FE_D6_SBW) != FE_D6_SBW_BYTE)
1964 length = (length + 1) & ~1;
1966 sc->txb_free -= FE_DATA_LEN_LEN +
1967 max(length, ETHER_MIN_LEN - ETHER_CRC_LEN);
1971 * Transfer the data from mbuf chain to the transmission buffer.
1972 * MB86960 seems to require that data be transferred as words, and
1973 * only words. So that we require some extra code to patch
1974 * over odd-length mbufs.
1976 #ifdef FE_8BIT_SUPPORT
1977 if ((sc->proto_dlcr6 & FE_D6_SBW) == FE_D6_SBW_BYTE)
1979 /* 8-bit cards are easy. */
1980 for (mp = m; mp != 0; mp = mp->m_next) {
1982 fe_outsb(sc, FE_BMPR8, mtod(mp, caddr_t),
1989 /* 16-bit cards are a pain. */
1990 savebyte = NO_PENDING_BYTE;
1991 for (mp = m; mp != 0; mp = mp->m_next) {
1993 /* Ignore empty mbuf. */
1998 /* Find the actual data to send. */
1999 data = mtod(mp, caddr_t);
2001 /* Finish the last byte. */
2002 if (savebyte != NO_PENDING_BYTE) {
2003 fe_outw(sc, FE_BMPR8, savebyte | (*data << 8));
2006 savebyte = NO_PENDING_BYTE;
2009 /* output contiguous words */
2011 fe_outsw(sc, FE_BMPR8, (u_int16_t *)data,
2017 /* Save a remaining byte, if there is one. */
2022 /* Spit the last byte, if the length is odd. */
2023 if (savebyte != NO_PENDING_BYTE)
2024 fe_outw(sc, FE_BMPR8, savebyte);
2027 /* Pad to the Ethernet minimum length, if the packet is too short. */
2028 if (length < ETHER_MIN_LEN - ETHER_CRC_LEN) {
2029 #ifdef FE_8BIT_SUPPORT
2030 if ((sc->proto_dlcr6 & FE_D6_SBW) == FE_D6_SBW_BYTE)
2032 fe_outsb(sc, FE_BMPR8, padding,
2033 ETHER_MIN_LEN - ETHER_CRC_LEN - length);
2038 fe_outsw(sc, FE_BMPR8, (u_int16_t *)padding,
2039 (ETHER_MIN_LEN - ETHER_CRC_LEN - length) >> 1);
2045 * Compute hash value for an Ethernet address
2048 fe_hash ( u_char * ep )
2050 #define FE_HASH_MAGIC_NUMBER 0xEDB88320L
2052 u_long hash = 0xFFFFFFFFL;
2057 for ( i = ETHER_ADDR_LEN; --i >= 0; ) {
2059 for ( j = 8; --j >= 0; ) {
2062 if ( ( m ^ b ) & 1 ) hash ^= FE_HASH_MAGIC_NUMBER;
2066 return ( ( int )( hash >> 26 ) );
2070 * Compute the multicast address filter from the
2071 * list of multicast addresses we need to listen to.
2073 static struct fe_filter
2074 fe_mcaf ( struct fe_softc *sc )
2077 struct fe_filter filter;
2078 struct ifmultiaddr *ifma;
2080 filter = fe_filter_nothing;
2081 LIST_FOREACH(ifma, &sc->arpcom.ac_if.if_multiaddrs, ifma_link) {
2082 if (ifma->ifma_addr->sa_family != AF_LINK)
2084 index = fe_hash(LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
2086 printf("fe%d: hash(%6D) == %d\n",
2087 sc->sc_unit, enm->enm_addrlo , ":", index);
2090 filter.data[index >> 3] |= 1 << (index & 7);
2096 * Calculate a new "multicast packet filter" and put the 86960
2097 * receiver in appropriate mode.
2100 fe_setmode (struct fe_softc *sc)
2102 int flags = sc->sc_if.if_flags;
2105 * If the interface is not running, we postpone the update
2106 * process for receive modes and multicast address filter
2107 * until the interface is restarted. It reduces some
2108 * complicated job on maintaining chip states. (Earlier versions
2109 * of this driver had a bug on that point...)
2111 * To complete the trick, fe_init() calls fe_setmode() after
2112 * restarting the interface.
2114 if (!(flags & IFF_RUNNING))
2118 * Promiscuous mode is handled separately.
2120 if (flags & IFF_PROMISC) {
2122 * Program 86960 to receive all packets on the segment
2123 * including those directed to other stations.
2124 * Multicast filter stored in MARs are ignored
2125 * under this setting, so we don't need to update it.
2127 * Promiscuous mode in FreeBSD 2 is used solely by
2128 * BPF, and BPF only listens to valid (no error) packets.
2129 * So, we ignore erroneous ones even in this mode.
2130 * (Older versions of fe driver mistook the point.)
2132 fe_outb(sc, FE_DLCR5,
2133 sc->proto_dlcr5 | FE_D5_AFM0 | FE_D5_AFM1);
2134 sc->filter_change = 0;
2139 * Turn the chip to the normal (non-promiscuous) mode.
2141 fe_outb(sc, FE_DLCR5, sc->proto_dlcr5 | FE_D5_AFM1);
2144 * Find the new multicast filter value.
2146 if (flags & IFF_ALLMULTI)
2147 sc->filter = fe_filter_all;
2149 sc->filter = fe_mcaf(sc);
2150 sc->filter_change = 1;
2153 * We have to update the multicast filter in the 86960, A.S.A.P.
2155 * Note that the DLC (Data Link Control unit, i.e. transmitter
2156 * and receiver) must be stopped when feeding the filter, and
2157 * DLC trashes all packets in both transmission and receive
2158 * buffers when stopped.
2160 * To reduce the packet loss, we delay the filter update
2161 * process until buffers are empty.
2163 if (sc->txb_sched == 0 && sc->txb_count == 0 &&
2164 !(fe_inb(sc, FE_DLCR1) & FE_D1_PKTRDY)) {
2166 * Buffers are (apparently) empty. Load
2167 * the new filter value into MARs now.
2172 * Buffers are not empty. Mark that we have to update
2173 * the MARs. The new filter will be loaded by feintr()
2180 * Load a new multicast address filter into MARs.
2182 * The caller must have splimp'ed before fe_loadmar.
2183 * This function starts the DLC upon return. So it can be called only
2184 * when the chip is working, i.e., from the driver's point of view, when
2185 * a device is RUNNING. (I mistook the point in previous versions.)
2188 fe_loadmar (struct fe_softc * sc)
2190 /* Stop the DLC (transmitter and receiver). */
2192 fe_outb(sc, FE_DLCR6, sc->proto_dlcr6 | FE_D6_DLC_DISABLE);
2195 /* Select register bank 1 for MARs. */
2196 fe_outb(sc, FE_DLCR7, sc->proto_dlcr7 | FE_D7_RBS_MAR | FE_D7_POWER_UP);
2198 /* Copy filter value into the registers. */
2199 fe_outblk(sc, FE_MAR8, sc->filter.data, FE_FILTER_LEN);
2201 /* Restore the bank selection for BMPRs (i.e., runtime registers). */
2202 fe_outb(sc, FE_DLCR7,
2203 sc->proto_dlcr7 | FE_D7_RBS_BMPR | FE_D7_POWER_UP);
2205 /* Restart the DLC. */
2207 fe_outb(sc, FE_DLCR6, sc->proto_dlcr6 | FE_D6_DLC_ENABLE);
2210 /* We have just updated the filter. */
2211 sc->filter_change = 0;
2214 /* Change the media selection. */
2216 fe_medchange (struct ifnet *ifp)
2218 struct fe_softc *sc = (struct fe_softc *)ifp->if_softc;
2221 /* If_media should not pass any request for a media which this
2222 interface doesn't support. */
2225 for (b = 0; bit2media[b] != 0; b++) {
2226 if (bit2media[b] == sc->media.ifm_media) break;
2228 if (((1 << b) & sc->mbitmap) == 0) {
2229 printf("fe%d: got an unsupported media request (0x%x)\n",
2230 sc->sc_unit, sc->media.ifm_media);
2235 /* We don't actually change media when the interface is down.
2236 fe_init() will do the job, instead. Should we also wait
2237 until the transmission buffer being empty? Changing the
2238 media when we are sending a frame will cause two garbages
2239 on wires, one on old media and another on new. FIXME */
2240 if (sc->sc_if.if_flags & IFF_UP) {
2241 if (sc->msel) sc->msel(sc);
2247 /* I don't know how I can support media status callback... FIXME. */
2249 fe_medstat (struct ifnet *ifp, struct ifmediareq *ifmr)