1/*
2 * All Rights Reserved, Copyright (C) Fujitsu Limited 1995
3 *
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.
9 *
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
20 * SUCH DAMAGE.
21 */
22
23#define FE_VERSION "if_fe.c ver. 0.8a"
24
25/*
26 * Device driver for Fujitsu MB86960A/MB86965A based Ethernet cards.
27 * To be used with FreeBSD 2.0 RELEASE.
28 * Contributed by M.S. <seki@sysrap.cs.fujitsu.co.jp>
29 *
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 (i.e., FMV-181 and FMV-182) and Allied-
33 * Telesis AT1700 series and RE2000 series. There are some
34 * unnecessary hooks embedded, which are primarily intended to support
35 * other types of Ethernet cards, but the author is not sure whether
36 * they are useful.
37 *
38 * This software is a derivative work of if_ed.c version 1.56 by David
39 * Greenman available as a part of FreeBSD 2.0 RELEASE source distribution.
40 *
41 * The following lines are retained from the original if_ed.c:
42 *
43 * Copyright (C) 1993, David Greenman. This software may be used, modified,
44 * copied, distributed, and sold, in both source and binary form provided
45 * that the above copyright and these terms are retained. Under no
46 * circumstances is the author responsible for the proper functioning
47 * of this software, nor does the author assume any responsibility
48 * for damages incurred with its use.
49 */
50
51#include "fe.h"
52#include "bpfilter.h"
53
54#include <sys/param.h>
55#include <sys/systm.h>
56#include <sys/conf.h>
57#include <sys/errno.h>
58#include <sys/ioctl.h>
59#include <sys/mbuf.h>
60#include <sys/socket.h>
61#include <sys/syslog.h>
62#include <sys/devconf.h>
63
64#include <net/if.h>
65#include <net/if_dl.h>
66#include <net/if_types.h>
67
68#ifdef INET
69#include <netinet/in.h>
70#include <netinet/in_systm.h>
71#include <netinet/in_var.h>
72#include <netinet/ip.h>
73#include <netinet/if_ether.h>
74#endif
75
76#ifdef IPX
77#include <netipx/ipx.h>
78#include <netipx/ipx_if.h>
79#endif
80
81#ifdef NS
82#include <netns/ns.h>
83#include <netns/ns_if.h>
84#endif
85
86#if NBPFILTER > 0
87#include <net/bpf.h>
88#include <net/bpfdesc.h>
89#endif
90
91#include <machine/clock.h>
92
93#include <i386/isa/isa.h>
94#include <i386/isa/isa_device.h>
95#include <i386/isa/icu.h>
96
97#include <i386/isa/ic/mb86960.h>
98#include <i386/isa/if_fereg.h>
99
100#ifdef __GNUC__
101#define INLINE inline
102#else
103#define INLINE
104#endif
105
106/*
107 * Default settings for fe driver specific options.
108 * They can be set in config file by "options" statements.
109 */
110
111/*
112 * Debug control.
113 * 0: No debug at all. All debug specific codes are stripped off.
114 * 1: Silent. No debug messages are logged except emergent ones.
115 * 2: Brief. Lair events and/or important information are logged.
116 * 3: Detailed. Logs all information which *may* be useful for debugging.
117 * 4: Trace. All actions in the driver is logged. Super verbose.
118 */
119#ifndef FE_DEBUG
120#define FE_DEBUG 1
121#endif
122
123/*
124 * Delay padding of short transmission packets to minimum Ethernet size.
125 * This may or may not gain performance. An EXPERIMENTAL option.
126 */
127#ifndef FE_DELAYED_PADDING
128#define FE_DELAYED_PADDING 0
129#endif
130
131/*
132 * Transmit just one packet per a "send"command to 86960.
133 * This option is intended for performance test. An EXPERIMENTAL option.
134 */
135#ifndef FE_SINGLE_TRANSMISSION
136#define FE_SINGLE_TRANSMISSION 0
137#endif
138
139/*
140 * Device configuration flags.
141 */
142
143/* DLCR6 settings. */
144#define FE_FLAGS_DLCR6_VALUE 0x007F
145
146/* Force DLCR6 override. */
147#define FE_FLAGS_OVERRIDE_DLCR6 0x0080
148
149/* A cludge for PCMCIA support. */
150#define FE_FLAGS_PCMCIA 0x8000
151
152/* Shouldn't this be defined somewhere else such as isa_device.h? */
153#define NO_IOADDR 0xFFFFFFFF
154
155/* Identification of the driver version. */
156static char const fe_version [] = FE_VERSION " / " FE_REG_VERSION;
157
158/*
159 * Supported hardware (Ethernet card) types
160 * This information is currently used only for debugging
161 */
162enum fe_type
163{
164 /* For cards which are successfully probed but not identified. */
165 FE_TYPE_UNKNOWN,
166
167 /* Fujitsu FMV-180 series. */
168 FE_TYPE_FMV181,
169 FE_TYPE_FMV182,
170
171 /* Allied-Telesis AT1700 series and RE2000 series. */
172 FE_TYPE_AT1700,
173
174 /* PCMCIA by Fujitsu. */
175 FE_TYPE_MBH10302,
176 FE_TYPE_MBH10304,
177
178 /* More can be here. */
179};
180
181/*
182 * Data type for a multicast address filter on 86960.
183 */
184struct fe_filter { u_char data [ FE_FILTER_LEN ]; };
185
186/*
187 * Special filter values.
188 */
189static struct fe_filter const fe_filter_nothing = { FE_FILTER_NOTHING };
190static struct fe_filter const fe_filter_all = { FE_FILTER_ALL };
191
192/*
193 * fe_softc: per line info and status
194 */
195static struct fe_softc {
196
197 /* Used by "common" codes. */
198 struct arpcom arpcom; /* ethernet common */
199
200 /* Used by config codes. */
201 struct kern_devconf kdc;/* Kernel configuration database info. */
202
203 /* Set by probe() and not modified in later phases. */
204 enum fe_type type; /* interface type code */
205 char * typestr; /* printable name of the interface. */
206 u_short addr; /* MB86960A I/O base address */
207 u_short txb_size; /* size of TX buffer, in bytes */
208 u_char proto_dlcr4; /* DLCR4 prototype. */
209 u_char proto_dlcr5; /* DLCR5 prototype. */
210 u_char proto_dlcr6; /* DLCR6 prototype. */
211 u_char proto_dlcr7; /* DLCR7 prototype. */
212
213 /* Vendor specific hooks. */
214 void ( * init )( struct fe_softc * ); /* Just before fe_init(). */
215 void ( * stop )( struct fe_softc * ); /* Just after fe_stop(). */
216
217 /* For BPF. */
218 caddr_t bpf; /* BPF "magic cookie" */
219
220 /* Transmission buffer management. */
221 u_short txb_free; /* free bytes in TX buffer */
222 u_char txb_count; /* number of packets in TX buffer */
223 u_char txb_sched; /* number of scheduled packets */
224 u_char txb_padding; /* number of delayed padding bytes */
225
226 /* Multicast address filter management. */
227 u_char filter_change; /* MARs must be changed ASAP. */
228 struct fe_filter filter;/* new filter value. */
229
230} fe_softc[NFE];
231
232/* Frequently accessed members in arpcom and kdc. */
233#define sc_if arpcom.ac_if
234#define sc_unit arpcom.ac_if.if_unit
235#define sc_enaddr arpcom.ac_enaddr
236#define sc_dcstate kdc.kdc_state
237#define sc_description kdc.kdc_description
238
239/*
240 * Some entry functions receive a "struct ifnet *" typed pointer as an
241 * argument. It points to arpcom.ac_if of our softc. Remember arpcom.ac_if
242 * is located at very first of the fe_softc struct. So, there is no
243 * difference between "struct fe_softc *" and "struct ifnet *" at the machine
244 * language level. We just cast to turn a "struct ifnet *" value into "struct
245 * fe_softc * value". If this were C++, we would need no such cast at all.
246 */
247#define IFNET2SOFTC(P) ( ( struct fe_softc * )(P) )
248
249/* Standard driver entry points. These can be static. */
250static int fe_probe ( struct isa_device * );
251static int fe_attach ( struct isa_device * );
252static void fe_init ( int );
253static int fe_ioctl ( struct ifnet *, int, caddr_t );
254static void fe_start ( struct ifnet * );
255static void fe_reset ( int );
256static void fe_watchdog ( struct ifnet * );
257
258/* Local functions. Order of declaration is confused. FIXME. */
259static int fe_probe_fmv ( struct isa_device *, struct fe_softc * );
260static int fe_probe_ati ( struct isa_device *, struct fe_softc * );
261static int fe_probe_mbh ( struct isa_device *, struct fe_softc * );
262static void fe_init_mbh ( struct fe_softc * );
263static int fe_get_packet ( struct fe_softc *, u_short );
264static void fe_stop ( int );
265static void fe_tint ( struct fe_softc *, u_char );
266static void fe_rint ( struct fe_softc *, u_char );
267static void fe_xmit ( struct fe_softc * );
268static void fe_write_mbufs ( struct fe_softc *, struct mbuf * );
269static struct fe_filter
270 fe_mcaf ( struct fe_softc * );
271static int fe_hash ( u_char * );
272static void fe_setmode ( struct fe_softc * );
273static void fe_loadmar ( struct fe_softc * );
274static void fe_setlinkaddr ( struct fe_softc * );
275#if FE_DEBUG >= 1
276static void fe_dump ( int, struct fe_softc *, char * );
277#endif
278
279/* Ethernet constants. To be defined in if_ehter.h? FIXME. */
280#define ETHER_MIN_LEN 60 /* with header, without CRC. */
281#define ETHER_MAX_LEN 1514 /* with header, without CRC. */
282#define ETHER_ADDR_LEN 6 /* number of bytes in an address. */
283#define ETHER_TYPE_LEN 2 /* number of bytes in a data type field. */
284#define ETHER_HDR_SIZE 14 /* src addr, dst addr, and data type. */
285#define ETHER_CRC_LEN 4 /* number of bytes in CRC field. */
286
287/* Driver struct used in the config code. This must be public (external.) */
288struct isa_driver fedriver =
289{
290 fe_probe,
291 fe_attach,
292 "fe",
293 0 /* Assume we are insensitive. FIXME. */
294};
295
296/* Initial value for a kdc struct. */
297static struct kern_devconf const fe_kdc_template =
298{
299 0, 0, 0,
300 "fe", 0, { MDDT_ISA, 0, "net" },
301 isa_generic_externalize, 0, 0, ISA_EXTERNALLEN,
302 &kdc_isa0, /* We are an ISA device. */
303 0,
304 DC_UNCONFIGURED, /* Not yet configured. */
305 "Ethernet (fe)", /* Tentative description (filled in later.) */
306 DC_CLS_NETIF /* We are a network interface. */
307};
308
309/*
310 * Fe driver specific constants which relate to 86960/86965.
311 * They are here (not in if_fereg.h), since selection of those
312 * values depend on driver design. I want to keep definitions in
313 * if_fereg.h "clean", so that if someone wrote another driver
314 * for 86960/86965, if_fereg.h were usable unchanged.
315 *
316 * The above statement sounds somothing like it's better to name
317 * it "ic/mb86960.h" but "if_fereg.h"... Should I do so? FIXME.
318 */
319
320/* Interrupt masks */
321#define FE_TMASK ( FE_D2_COLL16 | FE_D2_TXDONE )
322#define FE_RMASK ( FE_D3_OVRFLO | FE_D3_CRCERR \
323 | FE_D3_ALGERR | FE_D3_SRTPKT | FE_D3_PKTRDY )
324
325/* Maximum number of iterrations for a receive interrupt. */
326#define FE_MAX_RECV_COUNT ( ( 65536 - 2048 * 2 ) / 64 )
327 /* Maximum size of SRAM is 65536,
328 * minimum size of transmission buffer in fe is 2x2KB,
329 * and minimum amount of received packet including headers
330 * added by the chip is 64 bytes.
331 * Hence FE_MAX_RECV_COUNT is the upper limit for number
332 * of packets in the receive buffer. */
333
334/*
335 * Convenient routines to access contiguous I/O ports.
336 */
337
338static INLINE void
339inblk ( u_short addr, u_char * mem, int len )
340{
341 while ( --len >= 0 ) {
342 *mem++ = inb( addr++ );
343 }
344}
345
346static INLINE void
347outblk ( u_short addr, u_char const * mem, int len )
348{
349 while ( --len >= 0 ) {
350 outb( addr++, *mem++ );
351 }
352}
353
354/*
355 * Hardware probe routines.
356 */
357
358/* How and where to probe; to support automatic I/O address detection. */
359struct fe_probe_list
360{
361 int ( * probe ) ( struct isa_device *, struct fe_softc * );
362 u_short const * addresses;
363};
364
365/* Lists of possible addresses. */
366static u_short const fe_fmv_addr [] =
367 { 0x220, 0x240, 0x260, 0x280, 0x2A0, 0x2C0, 0x300, 0x340, 0 };
368static u_short const fe_ati_addr [] =
369 { 0x240, 0x260, 0x280, 0x2A0, 0x300, 0x320, 0x340, 0x380, 0 };
370
371static struct fe_probe_list const fe_probe_list [] =
372{
373 { fe_probe_fmv, fe_fmv_addr },
374 { fe_probe_ati, fe_ati_addr },
375 { fe_probe_mbh, NULL }, /* PCMCIAs cannot be auto-detected. */
376 { NULL, NULL }
377};
378
379/*
380 * Determine if the device is present
381 *
382 * on entry:
383 * a pointer to an isa_device struct
384 * on exit:
385 * zero if device not found
386 * or number of i/o addresses used (if found)
387 */
388
389static int
390fe_probe ( struct isa_device * isa_dev )
391{
392 struct fe_softc * sc, * u;
393 int nports;
394 struct fe_probe_list const * list;
395 u_short const * addr;
396 u_short single [ 2 ];
397
398 /* Initialize "minimum" parts of our softc. */
399 sc = &fe_softc[ isa_dev->id_unit ];
400 sc->sc_unit = isa_dev->id_unit;
401
402#if FE_DEBUG >= 2
403 log( LOG_INFO, "fe%d: %s\n", sc->sc_unit, fe_version );
404#endif
405
406#ifndef DEV_LKM
407 /* Fill the device config data and register it. */
408 sc->kdc = fe_kdc_template;
409 sc->kdc.kdc_unit = sc->sc_unit;
410 sc->kdc.kdc_parentdata = isa_dev;
411 dev_attach( &sc->kdc );
412#endif
413
414 /* Probe each possibility, one at a time. */
415 for ( list = fe_probe_list; list->probe != NULL; list++ ) {
416
417 if ( isa_dev->id_iobase != NO_IOADDR ) {
418 /* Probe one specific address. */
419 single[ 0 ] = isa_dev->id_iobase;
420 single[ 1 ] = 0;
421 addr = single;
422 } else if ( list->addresses != NULL ) {
423 /* Auto detect. */
424 addr = list->addresses;
425 } else {
426 /* We need a list of addresses to do auto detect. */
427 continue;
428 }
429
430 /* Probe all possible addresses for the board. */
431 while ( *addr != 0 ) {
432
433 /* Don't probe already used address. */
434 for ( u = &fe_softc[0]; u < &fe_softc[NFE]; u++ ) {
435 if ( u->addr == *addr ) break;
436 }
437 if ( u < &fe_softc[NFE] ) continue;
438
439 /* Probe an address. */
440 sc->addr = *addr;
441 nports = list->probe( isa_dev, sc );
442 if ( nports > 0 ) {
443 /* Found. */
444 isa_dev->id_iobase = *addr;
445 return ( nports );
446 }
447
448 /* Try next. */
449 sc->addr = 0;
450 addr++;
451 }
452 }
453
454 /* Probe failed. */
455 return ( 0 );
456}
457
458/*
459 * Check for specific bits in specific registers have specific values.
460 */
461struct fe_simple_probe_struct
462{
463 u_char port; /* Offset from the base I/O address. */
464 u_char mask; /* Bits to be checked. */
465 u_char bits; /* Values to be compared against. */
466};
467
468static INLINE int
469fe_simple_probe ( u_short addr, struct fe_simple_probe_struct const * sp )
470{
471 struct fe_simple_probe_struct const * p;
472
473 for ( p = sp; p->mask != 0; p++ ) {
474 if ( ( inb( addr + p->port ) & p->mask ) != p->bits ) {
475 return ( 0 );
476 }
477 }
478 return ( 1 );
479}
480
481/*
482 * Routines to read all bytes from the config EEPROM through MB86965A.
483 * I'm not sure what exactly I'm doing here... I was told just to follow
484 * the steps, and it worked. Could someone tell me why the following
485 * code works? (Or, why all similar codes I tried previously doesn't
486 * work.) FIXME.
487 */
488
489static INLINE void
490strobe ( u_short bmpr16 )
491{
492 /*
493 * Output same value twice. To speed-down execution?
494 */
495 outb( bmpr16, FE_B16_SELECT );
496 outb( bmpr16, FE_B16_SELECT );
497 outb( bmpr16, FE_B16_SELECT | FE_B16_CLOCK );
498 outb( bmpr16, FE_B16_SELECT | FE_B16_CLOCK );
499 outb( bmpr16, FE_B16_SELECT );
500 outb( bmpr16, FE_B16_SELECT );
501}
502
503static void
504fe_read_eeprom ( struct fe_softc * sc, u_char * data )
505{
506 u_short bmpr16 = sc->addr + FE_BMPR16;
507 u_short bmpr17 = sc->addr + FE_BMPR17;
508 u_char n, val, bit;
509 u_char save16, save17;
510
511 /* Save old values of the registers. */
512 save16 = inb( bmpr16 );
513 save17 = inb( bmpr17 );
514
515 /* Read bytes from EEPROM; two bytes per an iterration. */
516 for ( n = 0; n < FE_EEPROM_SIZE / 2; n++ ) {
517
518 /* Reset the EEPROM interface. */
519 outb( bmpr16, 0x00 );
520 outb( bmpr17, 0x00 );
521 outb( bmpr16, FE_B16_SELECT );
522
523 /* Start EEPROM access. */
524 outb( bmpr17, FE_B17_DATA );
525 strobe( bmpr16 );
526
527 /* Pass the iterration count to the chip. */
528 val = 0x80 | n;
529 for ( bit = 0x80; bit != 0x00; bit >>= 1 ) {
530 outb( bmpr17, ( val & bit ) ? FE_B17_DATA : 0 );
531 strobe( bmpr16 );
532 }
533 outb( bmpr17, 0x00 );
534
535 /* Read a byte. */
536 val = 0;
537 for ( bit = 0x80; bit != 0x00; bit >>= 1 ) {
538 strobe( bmpr16 );
539 if ( inb( bmpr17 ) & FE_B17_DATA ) {
540 val |= bit;
541 }
542 }
543 *data++ = val;
544
545 /* Read one more byte. */
546 val = 0;
547 for ( bit = 0x80; bit != 0x00; bit >>= 1 ) {
548 strobe( bmpr16 );
549 if ( inb( bmpr17 ) & FE_B17_DATA ) {
550 val |= bit;
551 }
552 }
553 *data++ = val;
554 }
555
556 /* Restore register values, in the case we had no 86965. */
557 outb( bmpr16, save16 );
558 outb( bmpr17, save17 );
559
560#if FE_DEBUG >= 3
561 /* Report what we got. */
562 data -= FE_EEPROM_SIZE;
563 log( LOG_INFO, "fe%d: EEPROM:"
564 " %02x%02x%02x%02x %02x%02x%02x%02x -"
565 " %02x%02x%02x%02x %02x%02x%02x%02x -"
566 " %02x%02x%02x%02x %02x%02x%02x%02x -"
567 " %02x%02x%02x%02x %02x%02x%02x%02x\n",
568 sc->sc_unit,
569 data[ 0], data[ 1], data[ 2], data[ 3],
570 data[ 4], data[ 5], data[ 6], data[ 7],
571 data[ 8], data[ 9], data[10], data[11],
572 data[12], data[13], data[14], data[15],
573 data[16], data[17], data[18], data[19],
574 data[20], data[21], data[22], data[23],
575 data[24], data[25], data[26], data[27],
576 data[28], data[29], data[30], data[31] );
577#endif
578}
579
580/*
581 * Hardware (vendor) specific probe routines.
582 */
583
584/*
585 * Probe and initialization for Fujitsu FMV-180 series boards
586 */
587static int
588fe_probe_fmv ( struct isa_device *isa_dev, struct fe_softc * sc )
589{
590 int i, n;
591
592 static u_short const ioaddr [ 8 ] =
593 { 0x220, 0x240, 0x260, 0x280, 0x2A0, 0x2C0, 0x300, 0x340 };
594 static u_short const irqmap [ 4 ] =
595 { IRQ3, IRQ7, IRQ10, IRQ15 };
596
597 static struct fe_simple_probe_struct const probe_table [] = {
598 { FE_DLCR2, 0x70, 0x00 },
599 { FE_DLCR4, 0x08, 0x00 },
600 /* { FE_DLCR5, 0x80, 0x00 }, Doesn't work. */
601
602 { FE_FMV0, FE_FMV0_MAGIC_MASK, FE_FMV0_MAGIC_VALUE },
603 { FE_FMV1, FE_FMV1_CARDID_MASK, FE_FMV1_CARDID_ID },
604 { FE_FMV3, FE_FMV3_EXTRA_MASK, FE_FMV3_EXTRA_VALUE },
605#if 1
606 /*
607 * Test *vendor* part of the station address for Fujitsu.
608 * The test will gain reliability of probe process, but
609 * it rejects FMV-180 clone boards manufactured by other vendors.
610 * We have to turn the test off when such cards are made available.
611 */
612 { FE_FMV4, 0xFF, 0x00 },
613 { FE_FMV5, 0xFF, 0x00 },
614 { FE_FMV6, 0xFF, 0x0E },
615#else
616 /*
617 * We can always verify the *first* 2 bits (in Ehternet
618 * bit order) are "no multicast" and "no local" even for
619 * unknown vendors.
620 */
621 { FE_FMV4, 0x03, 0x00 },
622#endif
623 { 0 }
624 };
625
626#if 0
627 /*
628 * Dont probe at all if the config says we are PCMCIA...
629 */
630 if ( isa_dev->id_flags & FE_FLAGS_PCMCIA ) return ( 0 );
631#endif
632
633 /*
634 * See if the sepcified address is possible for FMV-180 series.
635 */
636 for ( i = 0; i < 8; i++ ) {
637 if ( ioaddr[ i ] == sc->addr ) break;
638 }
639 if ( i == 8 ) return 0;
640
641 /* Simple probe. */
642 if ( !fe_simple_probe( sc->addr, probe_table ) ) return 0;
643
644 /* Check if our I/O address matches config info on EEPROM. */
645 n = ( inb( sc->addr + FE_FMV2 ) & FE_FMV2_ADDR ) >> FE_FMV2_ADDR_SHIFT;
646 if ( ioaddr[ n ] != sc->addr ) return 0;
647
648 /* Determine the card type. */
649 switch ( inb( sc->addr + FE_FMV0 ) & FE_FMV0_MODEL ) {
650 case FE_FMV0_MODEL_FMV181:
651 sc->type = FE_TYPE_FMV181;
652 sc->typestr = "FMV-181";
653 sc->sc_description = "Ethernet adapter: FMV-181";
654 break;
655 case FE_FMV0_MODEL_FMV182:
656 sc->type = FE_TYPE_FMV182;
657 sc->typestr = "FMV-182";
658 sc->sc_description = "Ethernet adapter: FMV-182";
659 break;
660 default:
661 /* Unknown card type: maybe a new model, but... */
662 return 0;
663 }
664
665 /*
666 * An FMV-180 has successfully been proved.
667 * Determine which IRQ to be used.
668 *
669 * In this version, we always get an IRQ assignment from the
670 * FMV-180's configuration EEPROM, ignoring that specified in
671 * config file.
672 */
673 n = ( inb( sc->addr + FE_FMV2 ) & FE_FMV2_IRQ ) >> FE_FMV2_IRQ_SHIFT;
674 isa_dev->id_irq = irqmap[ n ];
675
676 /*
677 * Initialize constants in the per-line structure.
678 */
679
680 /* Get our station address from EEPROM. */
681 inblk( sc->addr + FE_FMV4, sc->sc_enaddr, ETHER_ADDR_LEN );
682
683 /* Make sure we got a valid station address. */
684 if ( ( sc->sc_enaddr[ 0 ] & 0x03 ) != 0x00
685 || ( sc->sc_enaddr[ 0 ] == 0x00
686 && sc->sc_enaddr[ 1 ] == 0x00
687 && sc->sc_enaddr[ 2 ] == 0x00 ) ) return 0;
688
689 /* Register values which depend on board design. */
690 sc->proto_dlcr4 = FE_D4_LBC_DISABLE | FE_D4_CNTRL;
691 sc->proto_dlcr5 = 0;
692 sc->proto_dlcr7 = FE_D7_BYTSWP_LH | FE_D7_IDENT_EC;
693
694 /*
695 * Program the 86960 as follows:
696 * SRAM: 32KB, 100ns, byte-wide access.
697 * Transmission buffer: 4KB x 2.
698 * System bus interface: 16 bits.
699 * We cannot change these values but TXBSIZE, because they
700 * are hard-wired on the board. Modifying TXBSIZE will affect
701 * the driver performance.
702 */
703 sc->proto_dlcr6 = FE_D6_BUFSIZ_32KB | FE_D6_TXBSIZ_2x4KB
704 | FE_D6_BBW_BYTE | FE_D6_SBW_WORD | FE_D6_SRAM_100ns;
705
706 /*
707 * Minimum initialization of the hardware.
708 * We write into registers; hope I/O ports have no
709 * overlap with other boards.
710 */
711
712 /* Initialize ASIC. */
713 outb( sc->addr + FE_FMV3, 0 );
714 outb( sc->addr + FE_FMV10, 0 );
715
716 /* Wait for a while. I'm not sure this is necessary. FIXME. */
717 DELAY(200);
718
719 /* Initialize 86960. */
720 outb( sc->addr + FE_DLCR6, sc->proto_dlcr6 | FE_D6_DLC_DISABLE );
721 DELAY(200);
722
723 /* Disable all interrupts. */
724 outb( sc->addr + FE_DLCR2, 0 );
725 outb( sc->addr + FE_DLCR3, 0 );
726
727 /* Turn the "master interrupt control" flag of ASIC on. */
728 outb( sc->addr + FE_FMV3, FE_FMV3_ENABLE_FLAG );
729
730 /*
731 * That's all. FMV-180 occupies 32 I/O addresses, by the way.
732 */
733 return 32;
734}
735
736/*
737 * Probe and initialization for Allied-Telesis AT1700/RE2000 series.
738 */
739static int
740fe_probe_ati ( struct isa_device * isa_dev, struct fe_softc * sc )
741{
742 int i, n;
743 u_char eeprom [ FE_EEPROM_SIZE ];
744
745 static u_short const ioaddr [ 8 ] =
746 { 0x260, 0x280, 0x2A0, 0x240, 0x340, 0x320, 0x380, 0x300 };
747 static u_short const irqmap_lo [ 4 ] =
748 { IRQ3, IRQ4, IRQ5, IRQ9 };
749 static u_short const irqmap_hi [ 4 ] =
750 { IRQ10, IRQ11, IRQ12, IRQ15 };
751 static struct fe_simple_probe_struct const probe_table [] = {
752 { FE_DLCR2, 0x70, 0x00 },
753 { FE_DLCR4, 0x08, 0x00 },
754 { FE_DLCR5, 0x80, 0x00 },
755#if 0
756 { FE_BMPR16, 0x1B, 0x00 },
757 { FE_BMPR17, 0x7F, 0x00 },
758#endif
759 { 0 }
760 };
761
762#if 0
763 /*
764 * Don't probe at all if the config says we are PCMCIA...
765 */
766 if ( isa_dev->id_flags & FE_FLAGS_PCMCIA ) return ( 0 );
767#endif
768
769#if FE_DEBUG >= 3
770 log( LOG_INFO, "fe%d: probe (0x%x) for ATI\n", sc->sc_unit, sc->addr );
771 fe_dump( LOG_INFO, sc, NULL );
772#endif
773
774 /*
775 * See if the sepcified address is possible for MB86965A JLI mode.
776 */
777 for ( i = 0; i < 8; i++ ) {
778 if ( ioaddr[ i ] == sc->addr ) break;
779 }
780 if ( i == 8 ) return 0;
781
782 /*
783 * We should test if MB86965A is on the base address now.
784 * Unfortunately, it is very hard to probe it reliably, since
785 * we have no way to reset the chip under software control.
786 * On cold boot, we could check the "signature" bit patterns
787 * described in the Fujitsu document. On warm boot, however,
788 * we can predict almost nothing about register values.
789 */
790 if ( !fe_simple_probe( sc->addr, probe_table ) ) return 0;
791
792 /* Check if our I/O address matches config info on 86965. */
793 n = ( inb( sc->addr + FE_BMPR19 ) & FE_B19_ADDR ) >> FE_B19_ADDR_SHIFT;
794 if ( ioaddr[ n ] != sc->addr ) return 0;
795
796 /*
797 * We are now almost sure we have an AT1700 at the given
798 * address. So, read EEPROM through 86965. We have to write
799 * into LSI registers to read from EEPROM. I want to avoid it
800 * at this stage, but I cannot test the presense of the chip
801 * any further without reading EEPROM. FIXME.
802 */
803 fe_read_eeprom( sc, eeprom );
804
805 /* Make sure that config info in EEPROM and 86965 agree. */
806 if ( eeprom[ FE_EEPROM_CONF ] != inb( sc->addr + FE_BMPR19 ) ) {
807 return 0;
808 }
809
810 /*
811 * Determine the card type.
812 * There may be a way to identify various models. FIXME.
813 */
814 sc->type = FE_TYPE_AT1700;
815 sc->typestr = "AT1700/RE2000";
816 sc->sc_description = "Ethernet adapter: AT1700 or RE2000";
817
818 /*
819 * I was told that RE2000 series has two variants on IRQ
820 * selection. They are 3/4/5/9 and 10/11/12/15. I don't know
821 * how we can distinguish which model is which. For now, we
822 * just trust irq setting in config. FIXME.
823 *
824 * I've heard that ATI puts an identification between these
825 * two models in the EEPROM. Sounds reasonable. I've also
826 * heard that Linux driver for AT1700 tests it. O.K. Let's
827 * try using it and see what happens. Anyway, we will use an
828 * IRQ value passed by config (i.e., user), if one is
829 * available. FIXME.
830 */
831 n = ( inb( sc->addr + FE_BMPR19 ) & FE_B19_IRQ ) >> FE_B19_IRQ_SHIFT;
832 if ( isa_dev->id_irq == 0 ) {
833 /* Try to determine IRQ settings. */
834 if ( eeprom[ FE_EEP_ATI_TYPE ] & FE_EEP_ATI_TYPE_HIGHIRQ ) {
835 isa_dev->id_irq = irqmap_hi[ n ];
836 } else {
837 isa_dev->id_irq = irqmap_lo[ n ];
838 }
839 }
840
841 /*
842 * Initialize constants in the per-line structure.
843 */
844
845 /* Get our station address from EEPROM. */
846 bcopy( eeprom + FE_EEP_ATI_ADDR, sc->sc_enaddr, ETHER_ADDR_LEN );
847
848#if 1
849 /*
850 * This test doesn't work well for AT1700 look-alike by
851 * other vendors.
852 */
853 /* Make sure the vendor part is for Allied-Telesis. */
854 if ( sc->sc_enaddr[ 0 ] != 0x00
855 || sc->sc_enaddr[ 1 ] != 0x00
856 || sc->sc_enaddr[ 2 ] != 0xF4 ) return 0;
857
858#else
859 /* Make sure we got a valid station address. */
860 if ( ( sc->sc_enaddr[ 0 ] & 0x03 ) != 0x00
861 || ( sc->sc_enaddr[ 0 ] == 0x00
862 && sc->sc_enaddr[ 1 ] == 0x00
863 && sc->sc_enaddr[ 2 ] == 0x00 ) ) return 0;
864#endif
865
866 /* Should find all register prototypes here. FIXME. */
867 sc->proto_dlcr4 = FE_D4_LBC_DISABLE | FE_D4_CNTRL; /* FIXME */
868 sc->proto_dlcr5 = 0;
869 sc->proto_dlcr7 = FE_D7_BYTSWP_LH | FE_D7_IDENT_EC;
870
871 /*
872 * Program the 86960 as follows:
873 * SRAM: 32KB, 100ns, byte-wide access.
874 * Transmission buffer: 4KB x 2.
875 * System bus interface: 16 bits.
876 * We cannot change these values but TXBSIZE, because they
877 * are hard-wired on the board. Modifying TXBSIZE will affect
878 * the driver performance.
879 */
880 sc->proto_dlcr6 = FE_D6_BUFSIZ_32KB | FE_D6_TXBSIZ_2x4KB
881 | FE_D6_BBW_BYTE | FE_D6_SBW_WORD | FE_D6_SRAM_100ns;
882
883#if FE_DEBUG >= 3
884 fe_dump( LOG_INFO, sc, "ATI found" );
885#endif
886
887 /* Initialize 86965. */
888 outb( sc->addr + FE_DLCR6, sc->proto_dlcr6 | FE_D6_DLC_DISABLE );
889 DELAY(200);
890
891 /* Disable all interrupts. */
892 outb( sc->addr + FE_DLCR2, 0 );
893 outb( sc->addr + FE_DLCR3, 0 );
894
895#if FE_DEBUG >= 3
896 fe_dump( LOG_INFO, sc, "end of fe_probe_ati()" );
897#endif
898
899 /*
900 * That's all. AT1700 occupies 32 I/O addresses, by the way.
901 */
902 return 32;
903}
904
905/*
906 * Probe and initialization for Fujitsu MBH10302 PCMCIA Ethernet interface.
907 */
908static int
909fe_probe_mbh ( struct isa_device * isa_dev, struct fe_softc * sc )
910{
911 static struct fe_simple_probe_struct probe_table [] = {
912 { FE_DLCR2, 0x70, 0x00 },
913 { FE_DLCR4, 0x08, 0x00 },
914 /* { FE_DLCR5, 0x80, 0x00 }, Does not work well. */
915#if 0
916 /*
917 * Test *vendor* part of the address for Fujitsu.
918 * The test will gain reliability of probe process, but
919 * it rejects clones by other vendors, or OEM product
920 * supplied by resalers other than Fujitsu.
921 */
922 { FE_MBH10, 0xFF, 0x00 },
923 { FE_MBH11, 0xFF, 0x00 },
924 { FE_MBH12, 0xFF, 0x0E },
925#else
926 /*
927 * We can always verify the *first* 2 bits (in Ehternet
928 * bit order) are "global" and "unicast" even for
929 * unknown vendors.
930 */
931 { FE_MBH10, 0x03, 0x00 },
932#endif
933 /* Just a gap? Seems reliable, anyway. */
934 { 0x12, 0xFF, 0x00 },
935 { 0x13, 0xFF, 0x00 },
936 { 0x14, 0xFF, 0x00 },
937 { 0x15, 0xFF, 0x00 },
938 { 0x16, 0xFF, 0x00 },
939 { 0x17, 0xFF, 0x00 },
940 { 0x18, 0xFF, 0xFF },
941 { 0x19, 0xFF, 0xFF },
942
943 { 0 }
944 };
945
946#if 0
947 /*
948 * We need a PCMCIA flag.
949 */
950 if ( ( isa_dev->id_flags & FE_FLAGS_PCMCIA ) == 0 ) return ( 0 );
951#endif
952
953 /*
954 * We need explicit IRQ and supported address.
955 */
956 if ( isa_dev->id_irq == 0 || ( sc->addr & ~0x3E0 ) != 0 ) return ( 0 );
957
958#if FE_DEBUG >= 3
959 fe_dump( LOG_INFO, sc, "top of probe" );
960#endif
961
962 /*
963 * See if MBH10302 is on its address.
964 * I'm not sure the following probe code works. FIXME.
965 */
966 if ( !fe_simple_probe( sc->addr, probe_table ) ) return 0;
967
968 /* Determine the card type. */
969 sc->type = FE_TYPE_MBH10302;
970 sc->typestr = "MBH10302 (PCMCIA)";
971 sc->sc_description = "Ethernet adapter: MBH10302 (PCMCIA)";
972
973 /*
974 * Initialize constants in the per-line structure.
975 */
976
977 /* Get our station address from EEPROM. */
978 inblk( sc->addr + FE_MBH10, sc->sc_enaddr, ETHER_ADDR_LEN );
979
980 /* Make sure we got a valid station address. */
981 if ( ( sc->sc_enaddr[ 0 ] & 0x03 ) != 0x00
982 || ( sc->sc_enaddr[ 0 ] == 0x00
983 && sc->sc_enaddr[ 1 ] == 0x00
984 && sc->sc_enaddr[ 2 ] == 0x00 ) ) return 0;
985
986 /* Should find all register prototypes here. FIXME. */
987 sc->proto_dlcr4 = FE_D4_LBC_DISABLE | FE_D4_CNTRL;
988 sc->proto_dlcr5 = 0;
989 sc->proto_dlcr7 = FE_D7_BYTSWP_LH | FE_D7_IDENT_NICE;
990
991 /*
992 * Program the 86960 as follows:
993 * SRAM: 32KB, 100ns, byte-wide access.
994 * Transmission buffer: 4KB x 2.
995 * System bus interface: 16 bits.
996 * We cannot change these values but TXBSIZE, because they
997 * are hard-wired on the board. Modifying TXBSIZE will affect
998 * the driver performance.
999 */
1000 sc->proto_dlcr6 = FE_D6_BUFSIZ_32KB | FE_D6_TXBSIZ_2x4KB
1001 | FE_D6_BBW_BYTE | FE_D6_SBW_WORD | FE_D6_SRAM_100ns;
1002
1003 /* Setup hooks. We need a special initialization procedure. */
1004 sc->init = fe_init_mbh;
1005
1006 /*
1007 * Minimum initialization.
1008 */
1009
1010 /* Wait for a while. I'm not sure this is necessary. FIXME. */
1011 DELAY(200);
1012
1013 /* Minimul initialization of 86960. */
1014 outb( sc->addr + FE_DLCR6, sc->proto_dlcr6 | FE_D6_DLC_DISABLE );
1015 DELAY( 200 );
1016
1017 /* Disable all interrupts. */
1018 outb( sc->addr + FE_DLCR2, 0 );
1019 outb( sc->addr + FE_DLCR3, 0 );
1020
1021#if 1 /* FIXME. */
1022 /* Initialize system bus interface and encoder/decoder operation. */
1023 outb( sc->addr + FE_MBH0, FE_MBH0_MAGIC | FE_MBH0_INTR_DISABLE );
1024#endif
1025
1026 /*
1027 * That's all. MBH10302 occupies 32 I/O addresses, by the way.
1028 */
1029 return 32;
1030}
1031
1032/* MBH specific initialization routine. */
1033static void
1034fe_init_mbh ( struct fe_softc * sc )
1035{
1036 /* Probably required after hot-insertion... */
1037
1038 /* Wait for a while. I'm not sure this is necessary. FIXME. */
1039 DELAY(200);
1040
1041 /* Minimul initialization of 86960. */
1042 outb( sc->addr + FE_DLCR6, sc->proto_dlcr6 | FE_D6_DLC_DISABLE );
1043 DELAY( 200 );
1044
1045 /* Disable all interrupts. */
1046 outb( sc->addr + FE_DLCR2, 0 );
1047 outb( sc->addr + FE_DLCR3, 0 );
1048
1049 /* Enable master interrupt flag. */
1050 outb( sc->addr + FE_MBH0, FE_MBH0_MAGIC | FE_MBH0_INTR_ENABLE );
1051}
1052
1053/*
1054 * Install interface into kernel networking data structures
1055 */
1056static int
1057fe_attach ( struct isa_device *isa_dev )
1058{
1059 struct fe_softc *sc = &fe_softc[isa_dev->id_unit];
1060
1061 /*
1062 * Initialize ifnet structure
1063 */
1064 sc->sc_if.if_unit = sc->sc_unit;
1065 sc->sc_if.if_name = "fe";
1066 sc->sc_if.if_output = ether_output;
1067 sc->sc_if.if_start = fe_start;
1068 sc->sc_if.if_ioctl = fe_ioctl;
1069 sc->sc_if.if_watchdog = fe_watchdog;
1070
1071 /*
1072 * Set default interface flags.
1073 */
1074 sc->sc_if.if_flags = IFF_BROADCAST | IFF_MULTICAST;
1075
1076 /*
1077 * Set maximum size of output queue, if it has not been set.
1078 * It is done here as this driver may be started after the
1079 * system intialization (i.e., the interface is PCMCIA.)
1080 *
1081 * I'm not sure this is really necessary, but, even if it is,
1082 * it should be done somewhere else, e.g., in if_attach(),
1083 * since it must be a common workaround for all network drivers.
1084 * FIXME.
1085 */
1086 if ( sc->sc_if.if_snd.ifq_maxlen == 0 ) {
1087 sc->sc_if.if_snd.ifq_maxlen = ifqmaxlen;
1088 }
1089
1090#if FE_DEBUG >= 3
1091 fe_dump( LOG_INFO, sc, "attach()" );
1092#endif
1093
1094#if FE_SINGLE_TRANSMISSION
1095 /* Override txb config to allocate minimum. */
1096 sc->proto_dlcr6 &= ~FE_D6_TXBSIZ
1097 sc->proto_dlcr6 |= FE_D6_TXBSIZ_2x2KB;
1098#endif
1099
1100 /* Modify hardware config if it is requested. */
1101 if ( isa_dev->id_flags & FE_FLAGS_OVERRIDE_DLCR6 ) {
1102 sc->proto_dlcr6 = isa_dev->id_flags & FE_FLAGS_DLCR6_VALUE;
1103 }
1104
1105 /* Find TX buffer size, based on the hardware dependent proto. */
1106 switch ( sc->proto_dlcr6 & FE_D6_TXBSIZ ) {
1107 case FE_D6_TXBSIZ_2x2KB: sc->txb_size = 2048; break;
1108 case FE_D6_TXBSIZ_2x4KB: sc->txb_size = 4096; break;
1109 case FE_D6_TXBSIZ_2x8KB: sc->txb_size = 8192; break;
1110 default:
1111 /* Oops, we can't work with single buffer configuration. */
1112#if FE_DEBUG >= 2
1113 log( LOG_WARNING, "fe%d: strange TXBSIZ config; fixing\n",
1114 sc->sc_unit );
1115#endif
1116 sc->proto_dlcr6 &= ~FE_D6_TXBSIZ;
1117 sc->proto_dlcr6 |= FE_D6_TXBSIZ_2x2KB;
1118 sc->txb_size = 2048;
1119 break;
1120 }
1121
1122 /* Attach and stop the interface. */
1123 if_attach( &sc->sc_if );
1124 fe_stop( sc->sc_unit ); /* This changes the state to IDLE. */
1125 fe_setlinkaddr( sc );
1126
1127 /* Print additional info when attached. */
1128 printf( "fe%d: address %6D, type %s\n", sc->sc_unit,
1129 sc->sc_enaddr, ":" , sc->typestr );
1130#if FE_DEBUG >= 3
1131 {
1132 int buf, txb, bbw, sbw, ram;
1133
1134 buf = txb = bbw = sbw = ram = -1;
1135 switch ( sc->proto_dlcr6 & FE_D6_BUFSIZ ) {
1136 case FE_D6_BUFSIZ_8KB: buf = 8; break;
1137 case FE_D6_BUFSIZ_16KB: buf = 16; break;
1138 case FE_D6_BUFSIZ_32KB: buf = 32; break;
1139 case FE_D6_BUFSIZ_64KB: buf = 64; break;
1140 }
1141 switch ( sc->proto_dlcr6 & FE_D6_TXBSIZ ) {
1142 case FE_D6_TXBSIZ_2x2KB: txb = 2; break;
1143 case FE_D6_TXBSIZ_2x4KB: txb = 4; break;
1144 case FE_D6_TXBSIZ_2x8KB: txb = 8; break;
1145 }
1146 switch ( sc->proto_dlcr6 & FE_D6_BBW ) {
1147 case FE_D6_BBW_BYTE: bbw = 8; break;
1148 case FE_D6_BBW_WORD: bbw = 16; break;
1149 }
1150 switch ( sc->proto_dlcr6 & FE_D6_SBW ) {
1151 case FE_D6_SBW_BYTE: sbw = 8; break;
1152 case FE_D6_SBW_WORD: sbw = 16; break;
1153 }
1154 switch ( sc->proto_dlcr6 & FE_D6_SRAM ) {
1155 case FE_D6_SRAM_100ns: ram = 100; break;
1156 case FE_D6_SRAM_150ns: ram = 150; break;
1157 }
1158 printf( "fe%d: SRAM %dKB %dbit %dns, TXB %dKBx2, %dbit I/O\n",
1159 sc->sc_unit, buf, bbw, ram, txb, sbw );
1160 }
1161#endif
1162
1163#if NBPFILTER > 0
1164 /* If BPF is in the kernel, call the attach for it. */
1165 bpfattach(&sc->bpf, &sc->sc_if, DLT_EN10MB,
1166 sizeof(struct ether_header));
1167#endif
1168 return 1;
1169}
1170
1171/*
1172 * Reset interface.
1173 */
1174static void
1175fe_reset ( int unit )
1176{
1177 /*
1178 * Stop interface and re-initialize.
1179 */
1180 fe_stop(unit);
1181 fe_init(unit);
1182}
1183
1184/*
1185 * Stop everything on the interface.
1186 *
1187 * All buffered packets, both transmitting and receiving,
1188 * if any, will be lost by stopping the interface.
1189 */
1190static void
1191fe_stop ( int unit )
1192{
1193 struct fe_softc *sc = &fe_softc[unit];
1194 int s;
1195
1196 s = splimp();
1197
1198#if FE_DEBUG >= 3
1199 fe_dump( LOG_INFO, sc, "stop()" );
1200#endif
1201
1202 /* Disable interrupts. */
1203 outb( sc->addr + FE_DLCR2, 0x00 );
1204 outb( sc->addr + FE_DLCR3, 0x00 );
1205
1206 /* Stop interface hardware. */
1207 DELAY( 200 );
1208 outb( sc->addr + FE_DLCR6, sc->proto_dlcr6 | FE_D6_DLC_DISABLE );
1209 DELAY( 200 );
1210
1211 /* Clear all interrupt status. */
1212 outb( sc->addr + FE_DLCR0, 0xFF );
1213 outb( sc->addr + FE_DLCR1, 0xFF );
1214
1215 /* Put the chip in stand-by mode. */
1216 DELAY( 200 );
1217 outb( sc->addr + FE_DLCR7, sc->proto_dlcr7 | FE_D7_POWER_DOWN );
1218 DELAY( 200 );
1219
1220 /* Reset transmitter variables and interface flags. */
1221 sc->sc_if.if_flags &= ~( IFF_OACTIVE | IFF_RUNNING );
1222 sc->sc_if.if_timer = 0;
1223 sc->txb_free = sc->txb_size;
1224 sc->txb_count = 0;
1225 sc->txb_sched = 0;
1226
1227 /* MAR loading can be delayed. */
1228 sc->filter_change = 0;
1229
1230 /* Update config status also. */
1231 sc->sc_dcstate = DC_IDLE;
1232
1233 /* Call a hook. */
1234 if ( sc->stop ) sc->stop( sc );
1235
1236#if FE_DEBUG >= 3
1237 fe_dump( LOG_INFO, sc, "end of stop()" );
1238#endif
1239
1240 (void) splx(s);
1241}
1242
1243/*
1244 * Device timeout/watchdog routine. Entered if the device neglects to
1245 * generate an interrupt after a transmit has been started on it.
1246 */
1247static void
1248fe_watchdog ( struct ifnet *ifp )
1249{
1250 struct fe_softc *sc = (struct fe_softc *)ifp;
1251
1252#if FE_DEBUG >= 1
1253 log( LOG_ERR, "fe%d: transmission timeout (%d+%d)%s\n",
1254 ifp->if_unit, sc->txb_sched, sc->txb_count,
1255 ( ifp->if_flags & IFF_UP ) ? "" : " when down" );
1256#endif
1257#if FE_DEBUG >= 3
1258 fe_dump( LOG_INFO, sc, NULL );
1259#endif
1260
1261 /* Record how many packets are lost by this accident. */
1262 ifp->if_oerrors += sc->txb_sched + sc->txb_count;
1263
1264 /* Put the interface into known initial state. */
1265 if ( ifp->if_flags & IFF_UP ) {
1266 fe_reset( ifp->if_unit );
1267 } else {
1268 fe_stop( ifp->if_unit );
1269 }
1270}
1271
1272/*
1273 * Initialize device.
1274 */
1275static void
1276fe_init ( int unit )
1277{
1278 struct fe_softc *sc = &fe_softc[unit];
1279 int i, s;
1280
1281#if FE_DEBUG >= 3
1282 fe_dump( LOG_INFO, sc, "init()" );
1283#endif
1284
1285 /* We need an address. */
1286 if (sc->sc_if.if_addrlist == 0) {
1287#if FE_DEBUG >= 1
1288 log( LOG_ERR, "fe%d: init() without any address\n",
1289 sc->sc_unit );
1290#endif
1291 return;
1292 }
1293
1294#if FE_DEBUG >= 1
1295 /*
1296 * Make sure we have a valid station address.
1297 * The following test is applicable for any Ethernet interfaces.
1298 * It can be done in somewhere common to all of them. FIXME.
1299 */
1300 if ( ( sc->sc_enaddr[ 0 ] & 0x01 ) != 0
1301 || ( sc->sc_enaddr[ 0 ] == 0x00
1302 && sc->sc_enaddr[ 1 ] == 0x00
1303 && sc->sc_enaddr[ 2 ] == 0x00 ) ) {
1304 log( LOG_ERR, "fe%d: invalid station address (%6D)\n",
1305 sc->sc_unit, sc->sc_enaddr, ":" );
1306 return;
1307 }
1308#endif
1309
1310 /* Start initializing 86960. */
1311 s = splimp();
1312
1313 /* Call a hook. */
1314 if ( sc->init ) sc->init( sc );
1315
1316#if FE_DEBUG >= 3
1317 fe_dump( LOG_INFO, sc, "after init hook" );
1318#endif
1319
1320 /*
1321 * Make sure to disable the chip, also.
1322 * This may also help re-programming the chip after
1323 * hot insertion of PCMCIAs.
1324 */
1325 outb( sc->addr + FE_DLCR6, sc->proto_dlcr6 | FE_D6_DLC_DISABLE );
1326
1327 /* Power up the chip and select register bank for DLCRs. */
1328 DELAY(200);
1329 outb( sc->addr + FE_DLCR7,
1330 sc->proto_dlcr7 | FE_D7_RBS_DLCR | FE_D7_POWER_UP );
1331 DELAY(200);
1332
1333 /* Feed the station address. */
1334 outblk( sc->addr + FE_DLCR8, sc->sc_enaddr, ETHER_ADDR_LEN );
1335
1336 /* Clear multicast address filter to receive nothing. */
1337 outb( sc->addr + FE_DLCR7,
1338 sc->proto_dlcr7 | FE_D7_RBS_MAR | FE_D7_POWER_UP );
1339 outblk( sc->addr + FE_MAR8, fe_filter_nothing.data, FE_FILTER_LEN );
1340
1341 /* Select the BMPR bank for runtime register access. */
1342 outb( sc->addr + FE_DLCR7,
1343 sc->proto_dlcr7 | FE_D7_RBS_BMPR | FE_D7_POWER_UP );
1344
1345 /* Initialize registers. */
1346 outb( sc->addr + FE_DLCR0, 0xFF ); /* Clear all bits. */
1347 outb( sc->addr + FE_DLCR1, 0xFF ); /* ditto. */
1348 outb( sc->addr + FE_DLCR2, 0x00 );
1349 outb( sc->addr + FE_DLCR3, 0x00 );
1350 outb( sc->addr + FE_DLCR4, sc->proto_dlcr4 );
1351 outb( sc->addr + FE_DLCR5, sc->proto_dlcr5 );
1352 outb( sc->addr + FE_BMPR10, 0x00 );
1353 outb( sc->addr + FE_BMPR11, FE_B11_CTRL_SKIP );
1354 outb( sc->addr + FE_BMPR12, 0x00 );
1355 outb( sc->addr + FE_BMPR13, FE_B13_TPTYPE_UTP | FE_B13_PORT_AUTO );
1356 outb( sc->addr + FE_BMPR14, 0x00 );
1357 outb( sc->addr + FE_BMPR15, 0x00 );
1358
1359#if FE_DEBUG >= 3
1360 fe_dump( LOG_INFO, sc, "just before enabling DLC" );
1361#endif
1362
1363 /* Enable interrupts. */
1364 outb( sc->addr + FE_DLCR2, FE_TMASK );
1365 outb( sc->addr + FE_DLCR3, FE_RMASK );
1366
1367 /* Enable transmitter and receiver. */
1368 DELAY(200);
1369 outb( sc->addr + FE_DLCR6, sc->proto_dlcr6 | FE_D6_DLC_ENABLE );
1370 DELAY(200);
1371
1372#if FE_DEBUG >= 3
1373 fe_dump( LOG_INFO, sc, "just after enabling DLC" );
1374#endif
1375 /*
1376 * Make sure to empty the receive buffer.
1377 *
1378 * This may be redundant, but *if* the receive buffer were full
1379 * at this point, the driver would hang. I have experienced
1380 * some strange hangups just after UP. I hope the following
1381 * code solve the problem.
1382 *
1383 * I have changed the order of hardware initialization.
1384 * I think the receive buffer cannot have any packets at this
1385 * point in this version. The following code *must* be
1386 * redundant now. FIXME.
1387 */
1388 for ( i = 0; i < FE_MAX_RECV_COUNT; i++ ) {
1389 if ( inb( sc->addr + FE_DLCR5 ) & FE_D5_BUFEMP ) break;
1390 outb( sc->addr + FE_BMPR14, FE_B14_SKIP );
1391 }
1392#if FE_DEBUG >= 1
1393 if ( i >= FE_MAX_RECV_COUNT ) {
1394 log( LOG_ERR, "fe%d: cannot empty receive buffer\n",
1395 sc->sc_unit );
1396 }
1397#endif
1398#if FE_DEBUG >= 3
1399 if ( i < FE_MAX_RECV_COUNT ) {
1400 log( LOG_INFO, "fe%d: receive buffer emptied (%d)\n",
1401 sc->sc_unit, i );
1402 }
1403#endif
1404
1405#if FE_DEBUG >= 3
1406 fe_dump( LOG_INFO, sc, "after ERB loop" );
1407#endif
1408
1409 /* Do we need this here? */
1410 outb( sc->addr + FE_DLCR0, 0xFF ); /* Clear all bits. */
1411 outb( sc->addr + FE_DLCR1, 0xFF ); /* ditto. */
1412
1413#if FE_DEBUG >= 3
1414 fe_dump( LOG_INFO, sc, "after FIXME" );
1415#endif
1416 /* Set 'running' flag, because we are now running. */
1417 sc->sc_if.if_flags |= IFF_RUNNING;
1418
1419 /* Update device config status. */
1420 sc->sc_dcstate = DC_BUSY;
1421
1422 /*
1423 * At this point, the interface is runnung properly,
1424 * except that it receives *no* packets. we then call
1425 * fe_setmode() to tell the chip what packets to be
1426 * received, based on the if_flags and multicast group
1427 * list. It completes the initialization process.
1428 */
1429 fe_setmode( sc );
1430
1431#if FE_DEBUG >= 3
1432 fe_dump( LOG_INFO, sc, "after setmode" );
1433#endif
1434
1435 /* ...and attempt to start output queued packets. */
1436 fe_start( &sc->sc_if );
1437
1438#if FE_DEBUG >= 3
1439 fe_dump( LOG_INFO, sc, "init() done" );
1440#endif
1441
1442 (void) splx(s);
1443}
1444
1445/*
1446 * This routine actually starts the transmission on the interface
1447 */
1448static INLINE void
1449fe_xmit ( struct fe_softc * sc )
1450{
1451 /*
1452 * Set a timer just in case we never hear from the board again.
1453 * We use longer timeout for multiple packet transmission.
1454 * I'm not sure this timer value is appropriate. FIXME.
1455 */
1456 sc->sc_if.if_timer = 1 + sc->txb_count;
1457
1458 /* Update txb variables. */
1459 sc->txb_sched = sc->txb_count;
1460 sc->txb_count = 0;
1461 sc->txb_free = sc->txb_size;
1462
1463#if FE_DELAYED_PADDING
1464 /* Omit the postponed padding process. */
1465 sc->txb_padding = 0;
1466#endif
1467
1468 /* Start transmitter, passing packets in TX buffer. */
1469 outb( sc->addr + FE_BMPR10, sc->txb_sched | FE_B10_START );
1470}
1471
1472/*
1473 * Start output on interface.
1474 * We make two assumptions here:
1475 * 1) that the current priority is set to splimp _before_ this code
1476 * is called *and* is returned to the appropriate priority after
1477 * return
1478 * 2) that the IFF_OACTIVE flag is checked before this code is called
1479 * (i.e. that the output part of the interface is idle)
1480 */
1481void
1482fe_start ( struct ifnet *ifp )
1483{
1484 struct fe_softc *sc = IFNET2SOFTC( ifp );
1485 struct mbuf *m;
1486
1487#if FE_DEBUG >= 1
1488 /* Just a sanity check. */
1489 if ( ( sc->txb_count == 0 ) != ( sc->txb_free == sc->txb_size ) ) {
1490 /*
1491 * Txb_count and txb_free co-works to manage the
1492 * transmission buffer. Txb_count keeps track of the
1493 * used potion of the buffer, while txb_free does unused
1494 * potion. So, as long as the driver runs properly,
1495 * txb_count is zero if and only if txb_free is same
1496 * as txb_size (which represents whole buffer.)
1497 */
1498 log( LOG_ERR, "fe%d: inconsistent txb variables (%d, %d)\n",
1499 sc->sc_unit, sc->txb_count, sc->txb_free );
1500 /*
1501 * So, what should I do, then?
1502 *
1503 * We now know txb_count and txb_free contradicts. We
1504 * cannot, however, tell which is wrong. More
1505 * over, we cannot peek 86960 transmission buffer or
1506 * reset the transmission buffer. (In fact, we can
1507 * reset the entire interface. I don't want to do it.)
1508 *
1509 * If txb_count is incorrect, leaving it as is will cause
1510 * sending of gabages after next interrupt. We have to
1511 * avoid it. Hence, we reset the txb_count here. If
1512 * txb_free was incorrect, resetting txb_count just loose
1513 * some packets. We can live with it.
1514 */
1515 sc->txb_count = 0;
1516 }
1517#endif
1518
1519#if FE_DEBUG >= 1
1520 /*
1521 * First, see if there are buffered packets and an idle
1522 * transmitter - should never happen at this point.
1523 */
1524 if ( ( sc->txb_count > 0 ) && ( sc->txb_sched == 0 ) ) {
1525 log( LOG_ERR,
1526 "fe%d: transmitter idle with %d buffered packets\n",
1527 sc->sc_unit, sc->txb_count );
1528 fe_xmit( sc );
1529 }
1530#endif
1531
1532 /*
1533 * Stop accepting more transmission packets temporarily, when
1534 * a filter change request is delayed. Updating the MARs on
1535 * 86960 flushes the transmisstion buffer, so it is delayed
1536 * until all buffered transmission packets have been sent
1537 * out.
1538 */
1539 if ( sc->filter_change ) {
1540 /*
1541 * Filter change requst is delayed only when the DLC is
1542 * working. DLC soon raise an interrupt after finishing
1543 * the work.
1544 */
1545 goto indicate_active;
1546 }
1547
1548 for (;;) {
1549
1550 /*
1551 * See if there is room to put another packet in the buffer.
1552 * We *could* do better job by peeking the send queue to
1553 * know the length of the next packet. Current version just
1554 * tests against the worst case (i.e., longest packet). FIXME.
1555 *
1556 * When adding the packet-peek feature, don't forget adding a
1557 * test on txb_count against QUEUEING_MAX.
1558 * There is a little chance the packet count exceeds
1559 * the limit. Assume transmission buffer is 8KB (2x8KB
1560 * configuration) and an application sends a bunch of small
1561 * (i.e., minimum packet sized) packets rapidly. An 8KB
1562 * buffer can hold 130 blocks of 62 bytes long...
1563 */
1564 if ( sc->txb_free < ETHER_MAX_LEN + FE_DATA_LEN_LEN ) {
1565 /* No room. */
1566 goto indicate_active;
1567 }
1568
1569#if FE_SINGLE_TRANSMISSION
1570 if ( sc->txb_count > 0 ) {
1571 /* Just one packet per a transmission buffer. */
1572 goto indicate_active;
1573 }
1574#endif
1575
1576 /*
1577 * Get the next mbuf chain for a packet to send.
1578 */
1579 IF_DEQUEUE( &sc->sc_if.if_snd, m );
1580 if ( m == NULL ) {
1581 /* No more packets to send. */
1582 goto indicate_inactive;
1583 }
1584
1585 /*
1586 * Copy the mbuf chain into the transmission buffer.
1587 * txb_* variables are updated as necessary.
1588 */
1589 fe_write_mbufs( sc, m );
1590
1591 /* Start transmitter if it's idle. */
1592 if ( sc->txb_sched == 0 ) fe_xmit( sc );
1593
1594#if 0 /* Turned of, since our interface is now duplex. */
1595 /*
1596 * Tap off here if there is a bpf listener.
1597 */
1598#if NBPFILTER > 0
1599 if ( sc->bpf ) bpf_mtap( sc->bpf, m );
1600#endif
1601#endif
1602
1603 m_freem( m );
1604 }
1605
1606 indicate_inactive:
1607 /*
1608 * We are using the !OACTIVE flag to indicate to
1609 * the outside world that we can accept an
1610 * additional packet rather than that the
1611 * transmitter is _actually_ active. Indeed, the
1612 * transmitter may be active, but if we haven't
1613 * filled all the buffers with data then we still
1614 * want to accept more.
1615 */
1616 sc->sc_if.if_flags &= ~IFF_OACTIVE;
1617 return;
1618
1619 indicate_active:
1620 /*
1621 * The transmitter is active, and there are no room for
1622 * more outgoing packets in the transmission buffer.
1623 */
1624 sc->sc_if.if_flags |= IFF_OACTIVE;
1625 return;
1626}
1627
1628/*
1629 * Drop (skip) a packet from receive buffer in 86960 memory.
1630 */
1631static INLINE void
1632fe_droppacket ( struct fe_softc * sc )
1633{
1634 outb( sc->addr + FE_BMPR14, FE_B14_SKIP );
1635}
1636
1637/*
1638 * Transmission interrupt handler
1639 * The control flow of this function looks silly. FIXME.
1640 */
1641static void
1642fe_tint ( struct fe_softc * sc, u_char tstat )
1643{
1644 int left;
1645 int col;
1646
1647 /*
1648 * Handle "excessive collision" interrupt.
1649 */
1650 if ( tstat & FE_D0_COLL16 ) {
1651
1652 /*
1653 * Find how many packets (including this collided one)
1654 * are left unsent in transmission buffer.
1655 */
1656 left = inb( sc->addr + FE_BMPR10 );
1657
1658#if FE_DEBUG >= 2
1659 log( LOG_WARNING, "fe%d: excessive collision (%d/%d)\n",
1660 sc->sc_unit, left, sc->txb_sched );
1661#endif
1662#if FE_DEBUG >= 3
1663 fe_dump( LOG_INFO, sc, NULL );
1664#endif
1665
1666 /*
1667 * Update statistics.
1668 */
1669 sc->sc_if.if_collisions += 16;
1670 sc->sc_if.if_oerrors++;
1671 sc->sc_if.if_opackets += sc->txb_sched - left;
1672
1673 /*
1674 * Collision statistics has been updated.
1675 * Clear the collision flag on 86960 now to avoid confusion.
1676 */
1677 outb( sc->addr + FE_DLCR0, FE_D0_COLLID );
1678
1679 /*
1680 * Restart transmitter, skipping the
1681 * collided packet.
1682 *
1683 * We *must* skip the packet to keep network running
1684 * properly. Excessive collision error is an
1685 * indication of the network overload. If we
1686 * tried sending the same packet after excessive
1687 * collision, the network would be filled with
1688 * out-of-time packets. Packets belonging
1689 * to reliable transport (such as TCP) are resent
1690 * by some upper layer.
1691 */
1692 outb( sc->addr + FE_BMPR11,
1693 FE_B11_CTRL_SKIP | FE_B11_MODE1 );
1694 sc->txb_sched = left - 1;
1695 }
1696
1697 /*
1698 * Handle "transmission complete" interrupt.
1699 */
1700 if ( tstat & FE_D0_TXDONE ) {
1701
1702 /*
1703 * Add in total number of collisions on last
1704 * transmission. We also clear "collision occurred" flag
1705 * here.
1706 *
1707 * 86960 has a design flaw on collision count on multiple
1708 * packet transmission. When we send two or more packets
1709 * with one start command (that's what we do when the
1710 * transmission queue is clauded), 86960 informs us number
1711 * of collisions occured on the last packet on the
1712 * transmission only. Number of collisions on previous
1713 * packets are lost. I have told that the fact is clearly
1714 * stated in the Fujitsu document.
1715 *
1716 * I considered not to mind it seriously. Collision
1717 * count is not so important, anyway. Any comments? FIXME.
1718 */
1719
1720 if ( inb( sc->addr + FE_DLCR0 ) & FE_D0_COLLID ) {
1721
1722 /* Clear collision flag. */
1723 outb( sc->addr + FE_DLCR0, FE_D0_COLLID );
1724
1725 /* Extract collision count from 86960. */
1726 col = inb( sc->addr + FE_DLCR4 );
1727 col = ( col & FE_D4_COL ) >> FE_D4_COL_SHIFT;
1728 if ( col == 0 ) {
1729 /*
1730 * Status register indicates collisions,
1731 * while the collision count is zero.
1732 * This can happen after multiple packet
1733 * transmission, indicating that one or more
1734 * previous packet(s) had been collided.
1735 *
1736 * Since the accurate number of collisions
1737 * has been lost, we just guess it as 1;
1738 * Am I too optimistic? FIXME.
1739 */
1740 col = 1;
1741 }
1742 sc->sc_if.if_collisions += col;
1743#if FE_DEBUG >= 3
1744 log( LOG_WARNING, "fe%d: %d collision(s) (%d)\n",
1745 sc->sc_unit, col, sc->txb_sched );
1746#endif
1747 }
1748
1749 /*
1750 * Update total number of successfully
1751 * transmitted packets.
1752 */
1753 sc->sc_if.if_opackets += sc->txb_sched;
1754 sc->txb_sched = 0;
1755
1756 /*
1757 * The transmitter is no more active.
1758 * Reset output active flag and watchdog timer.
1759 */
1760 sc->sc_if.if_flags &= ~IFF_OACTIVE;
1761 sc->sc_if.if_timer = 0;
1762
1763 /*
1764 * If more data is ready to transmit in the buffer, start
1765 * transmitting them. Otherwise keep transmitter idle,
1766 * even if more data is queued. This gives receive
1767 * process a slight priority.
1768 */
1769 if ( sc->txb_count > 0 ) fe_xmit( sc );
1770 }
1771}
1772
1773/*
1774 * Ethernet interface receiver interrupt.
1775 */
1776static void
1777fe_rint ( struct fe_softc * sc, u_char rstat )
1778{
1779 u_short len;
1780 u_char status;
1781 int i;
1782
1783 /*
1784 * Update statistics if this interrupt is caused by an error.
1785 */
1786 if ( rstat & ( FE_D1_OVRFLO | FE_D1_CRCERR
1787 | FE_D1_ALGERR | FE_D1_SRTPKT ) ) {
1788#if FE_DEBUG >= 3
1789 log( LOG_WARNING,
1790 "fe%d: receive error: %s%s%s%s(%02x)\n",
1791 sc->sc_unit,
1792 rstat & FE_D1_OVRFLO ? "OVR " : "",
1793 rstat & FE_D1_CRCERR ? "CRC " : "",
1794 rstat & FE_D1_ALGERR ? "ALG " : "",
1795 rstat & FE_D1_SRTPKT ? "LEN " : "",
1796 rstat );
1797#endif
1798 sc->sc_if.if_ierrors++;
1799 }
1800
1801 /*
1802 * MB86960 has a flag indicating "receive queue empty."
1803 * We just loop cheking the flag to pull out all received
1804 * packets.
1805 *
1806 * We limit the number of iterrations to avoid inifnit-loop.
1807 * It can be caused by a very slow CPU (some broken
1808 * peripheral may insert incredible number of wait cycles)
1809 * or, worse, by a broken MB86960 chip.
1810 */
1811 for ( i = 0; i < FE_MAX_RECV_COUNT; i++ ) {
1812
1813 /* Stop the iterration if 86960 indicates no packets. */
1814 if ( inb( sc->addr + FE_DLCR5 ) & FE_D5_BUFEMP ) break;
1815
1816 /*
1817 * Extract A receive status byte.
1818 * As our 86960 is in 16 bit bus access mode, we have to
1819 * use inw() to get the status byte. The significant
1820 * value is returned in lower 8 bits.
1821 */
1822 status = ( u_char )inw( sc->addr + FE_BMPR8 );
1823#if FE_DEBUG >= 4
1824 log( LOG_INFO, "fe%d: receive status = %04x\n",
1825 sc->sc_unit, status );
1826#endif
1827
1828 /*
1829 * If there was an error, update statistics and drop
1830 * the packet, unless the interface is in promiscuous
1831 * mode.
1832 */
1833 if ( ( status & 0xF0 ) != 0x20 ) {
1834 if ( !( sc->sc_if.if_flags & IFF_PROMISC ) ) {
1835 sc->sc_if.if_ierrors++;
1836 fe_droppacket(sc);
1837 continue;
1838 }
1839 }
1840
1841 /*
1842 * Extract the packet length.
1843 * It is a sum of a header (14 bytes) and a payload.
1844 * CRC has been stripped off by the 86960.
1845 */
1846 len = inw( sc->addr + FE_BMPR8 );
1847
1848 /*
1849 * MB86965 checks the packet length and drop big packet
1850 * before passing it to us. There are no chance we can
1851 * get [crufty] packets. Hence, if the length exceeds
1852 * the specified limit, it means some serious failure,
1853 * such as out-of-sync on receive buffer management.
1854 *
1855 * Is this statement true? FIXME.
1856 */
1857 if ( len > ETHER_MAX_LEN || len < ETHER_HDR_SIZE ) {
1858#if FE_DEBUG >= 2
1859 log( LOG_WARNING,
1860 "fe%d: received a %s packet? (%u bytes)\n",
1861 sc->sc_unit,
1862 len < ETHER_HDR_SIZE ? "partial" : "big",
1863 len );
1864#endif
1865 sc->sc_if.if_ierrors++;
1866 fe_droppacket( sc );
1867 continue;
1868 }
1869
1870 /*
1871 * Check for a short (RUNT) packet. We *do* check
1872 * but do nothing other than print a message.
1873 * Short packets are illegal, but does nothing bad
1874 * if it carries data for upper layer.
1875 */
1876#if FE_DEBUG >= 2
1877 if ( len < ETHER_MIN_LEN ) {
1878 log( LOG_WARNING,
1879 "fe%d: received a short packet? (%u bytes)\n",
1880 sc->sc_unit, len );
1881 }
1882#endif
1883
1884 /*
1885 * Go get a packet.
1886 */
1887 if ( fe_get_packet( sc, len ) < 0 ) {
1888 /* Skip a packet, updating statistics. */
1889#if FE_DEBUG >= 2
1890 log( LOG_WARNING, "%s%d: no enough mbuf;"
1891 " a packet (%u bytes) dropped\n",
1892 sc->sc_unit, len );
1893#endif
1894 sc->sc_if.if_ierrors++;
1895 fe_droppacket( sc );
1896
1897 /*
1898 * We stop receiving packets, even if there are
1899 * more in the buffer. We hope we can get more
1900 * mbuf next time.
1901 */
1902 return;
1903 }
1904
1905 /* Successfully received a packet. Update stat. */
1906 sc->sc_if.if_ipackets++;
1907 }
1908}
1909
1910/*
1911 * Ethernet interface interrupt processor
1912 */
1913void
1914feintr ( int unit )
1915{
1916 struct fe_softc *sc = &fe_softc[unit];
1917 u_char tstat, rstat;
1918
1919 /*
1920 * Loop until there are no more new interrupt conditions.
1921 */
1922 for (;;) {
1923
1924#if FE_DEBUG >= 4
1925 fe_dump( LOG_INFO, sc, "intr()" );
1926#endif
1927
1928 /*
1929 * Get interrupt conditions, masking unneeded flags.
1930 */
1931 tstat = inb( sc->addr + FE_DLCR0 ) & FE_TMASK;
1932 rstat = inb( sc->addr + FE_DLCR1 ) & FE_RMASK;
1933 if ( tstat == 0 && rstat == 0 ) break;
1934
1935 /*
1936 * Reset the conditions we are acknowledging.
1937 */
1938 outb( sc->addr + FE_DLCR0, tstat );
1939 outb( sc->addr + FE_DLCR1, rstat );
1940
1941 /*
1942 * Handle transmitter interrupts. Handle these first because
1943 * the receiver will reset the board under some conditions.
1944 */
1945 if ( tstat ) {
1946 fe_tint( sc, tstat );
1947 }
1948
1949 /*
1950 * Handle receiver interrupts
1951 */
1952 if ( rstat ) {
1953 fe_rint( sc, rstat );
1954 }
1955
1956 /*
1957 * Update the multicast address filter if it is
1958 * needed and possible. We do it now, because
1959 * we can make sure the transmission buffer is empty,
1960 * and there is a good chance that the receive queue
1961 * is empty. It will minimize the possibility of
1962 * packet lossage.
1963 */
1964 if ( sc->filter_change
1965 && sc->txb_count == 0 && sc->txb_sched == 0 ) {
1966 fe_loadmar(sc);
1967 sc->sc_if.if_flags &= ~IFF_OACTIVE;
1968 }
1969
1970 /*
1971 * If it looks like the transmitter can take more data,
1972 * attempt to start output on the interface. This is done
1973 * after handling the receiver interrupt to give the
1974 * receive operation priority.
1975 *
1976 * BTW, I'm not sure in what case the OACTIVE is on at
1977 * this point. Is the following test redundant?
1978 *
1979 * No. This routine polls for both transmitter and
1980 * receiver interrupts. 86960 can raise a receiver
1981 * interrupt when the transmission buffer is full.
1982 */
1983 if ( ( sc->sc_if.if_flags & IFF_OACTIVE ) == 0 ) {
1984 fe_start( &sc->sc_if );
1985 }
1986
1987 }
1988}
1989
1990/*
1991 * Process an ioctl request. This code needs some work - it looks
1992 * pretty ugly.
1993 */
1994int
1995fe_ioctl ( struct ifnet *ifp, int command, caddr_t data )
1996{
1997 struct fe_softc *sc = IFNET2SOFTC( ifp );
1998 int s, error = 0;
1999
2000#if FE_DEBUG >= 3
2001 log( LOG_INFO, "fe%d: ioctl(%x)\n", sc->sc_unit, command );
2002#endif
2003
2004 s = splimp();
2005
2006 switch (command) {
2007
2008 case SIOCSIFADDR:
2009 {
2010 struct ifaddr * ifa = ( struct ifaddr * )data;
2011
2012 sc->sc_if.if_flags |= IFF_UP;
2013
2014 switch (ifa->ifa_addr->sa_family) {
2015#ifdef INET
2016 case AF_INET:
2017 fe_init( sc->sc_unit ); /* before arpwhohas */
2018 arp_ifinit( &sc->arpcom, ifa );
2019 break;
2020#endif
2021#ifdef IPX
2022
2023 /*
2024 * XXX - This code is probably wrong
2025 */
2026 case AF_IPX:
2027 {
2028 register struct ipx_addr *ina
2029 = &(IA_SIPX(ifa)->sipx_addr);
2030
2031 if (ipx_nullhost(*ina))
2032 ina->x_host =
2033 *(union ipx_host *) (sc->sc_enaddr);
2034 else {
2035 bcopy((caddr_t) ina->x_host.c_host,
2036 (caddr_t) sc->sc_enaddr,
2037 sizeof(sc->sc_enaddr));
2038 }
2039
2040 /*
2041 * Set new address
2042 */
2043 fe_init(sc->sc_unit);
2044 break;
2045 }
2046#endif
2047#ifdef NS
2048
2049 /*
2050 * XXX - This code is probably wrong
2051 */
2052 case AF_NS:
2053 {
2054 register struct ns_addr *ina
2055 = &(IA_SNS(ifa)->sns_addr);
2056
2057 if (ns_nullhost(*ina))
2058 ina->x_host =
2059 *(union ns_host *) (sc->sc_enaddr);
2060 else {
2061 bcopy((caddr_t) ina->x_host.c_host,
2062 (caddr_t) sc->sc_enaddr,
2063 sizeof(sc->sc_enaddr));
2064 }
2065
2066 /*
2067 * Set new address
2068 */
2069 fe_init(sc->sc_unit);
2070 break;
2071 }
2072#endif
2073 default:
2074 fe_init( sc->sc_unit );
2075 break;
2076 }
2077 break;
2078 }
2079
2080#ifdef SIOCGIFADDR
2081 case SIOCGIFADDR:
2082 {
2083 struct ifreq * ifr = ( struct ifreq * )data;
2084 struct sockaddr * sa = ( struct sockaddr * )&ifr->ifr_data;
2085
2086 bcopy((caddr_t)sc->sc_enaddr,
2087 (caddr_t)sa->sa_data, ETHER_ADDR_LEN);
2088 break;
2089 }
2090#endif
2091
2092#ifdef SIOCGIFPHYSADDR
2093 case SIOCGIFPHYSADDR:
2094 {
2095 struct ifreq * ifr = ( struct ifreq * )data;
2096
2097 bcopy((caddr_t)sc->sc_enaddr,
2098 (caddr_t)&ifr->ifr_data, ETHER_ADDR_LEN);
2099 break;
2100 }
2101#endif
2102
2103#ifdef SIOCSIFPHYSADDR
2104 case SIOCSIFPHYSADDR:
2105 {
2106 /*
2107 * Set the physical (Ehternet) address of the interface.
2108 * When and by whom is this command used? FIXME.
2109 */
2110 struct ifreq * ifr = ( struct ifreq * )data;
2111
2112 bcopy((caddr_t)&ifr->ifr_data,
2113 (caddr_t)sc->sc_enaddr, ETHER_ADDR_LEN);
2114 fe_setlinkaddr( sc );
2115 break;
2116 }
2117#endif
2118
2119#ifdef SIOCSIFFLAGS
2120 case SIOCSIFFLAGS:
2121 {
2122 /*
2123 * Switch interface state between "running" and
2124 * "stopped", reflecting the UP flag.
2125 */
2126 if ( sc->sc_if.if_flags & IFF_UP ) {
2127 if ( ( sc->sc_if.if_flags & IFF_RUNNING ) == 0 ) {
2128 fe_init( sc->sc_unit );
2129 }
2130 } else {
2131 if ( ( sc->sc_if.if_flags & IFF_RUNNING ) != 0 ) {
2132 fe_stop( sc->sc_unit );
2133 }
2134 }
2135
2136 /*
2137 * Promiscuous and/or multicast flags may have changed,
2138 * so reprogram the multicast filter and/or receive mode.
2139 */
2140 fe_setmode( sc );
2141
2142#if FE_DEBUG >= 1
2143 /* "ifconfig fe0 debug" to print register dump. */
2144 if ( sc->sc_if.if_flags & IFF_DEBUG ) {
2145 fe_dump( LOG_DEBUG, sc, "SIOCSIFFLAGS(DEBUG)" );
2146 }
2147#endif
2148 break;
2149 }
2150#endif
2151
2152#ifdef SIOCADDMULTI
2153 case SIOCADDMULTI:
2154 case SIOCDELMULTI:
2155 {
2156 /*
2157 * Update out multicast list.
2158 */
2159 struct ifreq * ifr = ( struct ifreq * )data;
2160
2161 error = ( command == SIOCADDMULTI )
2162 ? ether_addmulti( ifr, &sc->arpcom )
2163 : ether_delmulti( ifr, &sc->arpcom );
2164
2165 if ( error == ENETRESET ) {
2166 /*
2167 * Multicast list has changed; set the hardware filter
2168 * accordingly.
2169 */
2170 fe_setmode( sc );
2171 error = 0;
2172 }
2173
2174 break;
2175 }
2176#endif
2177
2178#ifdef SIOCSIFMTU
2179 case SIOCSIFMTU:
2180 {
2181 /*
2182 * Set the interface MTU.
2183 */
2184 struct ifreq * ifr = ( struct ifreq * )data;
2185
2186 if ( ifr->ifr_mtu > ETHERMTU ) {
2187 error = EINVAL;
2188 } else {
2189 sc->sc_if.if_mtu = ifr->ifr_mtu;
2190 }
2191 break;
2192 }
2193#endif
2194
2195 default:
2196 error = EINVAL;
2197 }
2198
2199 (void) splx(s);
2200 return (error);
2201}
2202
2203/*
2204 * Retreive packet from receive buffer and send to the next level up via
2205 * ether_input(). If there is a BPF listener, give a copy to BPF, too.
2206 * Returns 0 if success, -1 if error (i.e., mbuf allocation failure).
2207 */
2208static int
2209fe_get_packet ( struct fe_softc * sc, u_short len )
2210{
2211 struct ether_header *eh;
2212 struct mbuf *m;
2213
2214 /*
2215 * NFS wants the data be aligned to the word (4 byte)
2216 * boundary. Ethernet header has 14 bytes. There is a
2217 * 2-byte gap.
2218 */
2219#define NFS_MAGIC_OFFSET 2
2220
2221 /*
2222 * This function assumes that an Ethernet packet fits in an
2223 * mbuf (with a cluster attached when necessary.) On FreeBSD
2224 * 2.0 for x86, which is the primary target of this driver, an
2225 * mbuf cluster has 4096 bytes, and we are happy. On ancient
2226 * BSDs, such as vanilla 4.3 for 386, a cluster size was 1024,
2227 * however. If the following #error message were printed upon
2228 * compile, you need to rewrite this function.
2229 */
2230#if ( MCLBYTES < ETHER_MAX_LEN + NFS_MAGIC_OFFSET )
2231#error "Too small MCLBYTES to use fe driver."
2232#endif
2233
2234 /*
2235 * Our strategy has one more problem. There is a policy on
2236 * mbuf cluster allocation. It says that we must have at
2237 * least MINCLSIZE (208 bytes on FreeBSD 2.0 for x86) to
2238 * allocate a cluster. For a packet of a size between
2239 * (MHLEN - 2) to (MINCLSIZE - 2), our code violates the rule...
2240 * On the other hand, the current code is short, simle,
2241 * and fast, however. It does no harmful thing, just waists
2242 * some memory. Any comments? FIXME.
2243 */
2244
2245 /* Allocate an mbuf with packet header info. */
2246 MGETHDR(m, M_DONTWAIT, MT_DATA);
2247 if ( m == NULL ) return -1;
2248
2249 /* Attach a cluster if this packet doesn't fit in a normal mbuf. */
2250 if ( len > MHLEN - NFS_MAGIC_OFFSET ) {
2251 MCLGET( m, M_DONTWAIT );
2252 if ( !( m->m_flags & M_EXT ) ) {
2253 m_freem( m );
2254 return -1;
2255 }
2256 }
2257
2258 /* Initialize packet header info. */
2259 m->m_pkthdr.rcvif = &sc->sc_if;
2260 m->m_pkthdr.len = len;
2261
2262 /* Set the length of this packet. */
2263 m->m_len = len;
2264
2265 /* The following sillines is to make NFS happy */
2266 m->m_data += NFS_MAGIC_OFFSET;
2267
2268 /* Get a packet. */
2269 insw( sc->addr + FE_BMPR8, m->m_data, ( len + 1 ) >> 1 );
2270
2271 /* Get (actually just point to) the header part. */
2272 eh = mtod( m, struct ether_header *);
2273
2274#define ETHER_ADDR_IS_MULTICAST(A) (*(char *)(A) & 1)
2275
2276#if NBPFILTER > 0
2277 /*
2278 * Check if there's a BPF listener on this interface.
2279 * If it is, hand off the raw packet to bpf.
2280 */
2281 if ( sc->bpf ) {
2282 bpf_mtap( sc->bpf, m );
2283 }
2284#endif
2285
2286 /*
2287 * Make sure this packet is (or may be) directed to us.
2288 * That is, the packet is either unicasted to our address,
2289 * or broad/multi-casted. If any other packets are
2290 * received, it is an indication of an error -- probably
2291 * 86960 is in a wrong operation mode.
2292 * Promiscuous mode is an exception. Under the mode, all
2293 * packets on the media must be received. (We must have
2294 * programmed the 86960 so.)
2295 */
2296
2297 if ( ( sc->sc_if.if_flags & IFF_PROMISC )
2298 && !ETHER_ADDR_IS_MULTICAST( eh->ether_dhost )
2299 && bcmp( eh->ether_dhost, sc->sc_enaddr, ETHER_ADDR_LEN ) != 0 ) {
2300 /*
2301 * The packet was not for us. This is normal since
2302 * we are now in promiscuous mode. Just drop the packet.
2303 */
2304 m_freem( m );
2305 return 0;
2306 }
2307
2308#if FE_DEBUG >= 3
2309 if ( !ETHER_ADDR_IS_MULTICAST( eh->ether_dhost )
2310 && bcmp( eh->ether_dhost, sc->sc_enaddr, ETHER_ADDR_LEN ) != 0 ) {
2311 /*
2312 * This packet was not for us. We can't be in promiscuous
2313 * mode since the case was handled by above test.
2314 * We found an error (of this driver.)
2315 */
2316 log( LOG_WARNING,
2317 "fe%d: got an unwanted packet, dst = %6D\n",
2318 sc->sc_unit,
2319 eh->ether_dhost , ":" );
2320 m_freem( m );
2321 return 0;
2322 }
2323#endif
2324
2325 /* Strip off the Ethernet header. */
2326 m->m_pkthdr.len -= sizeof ( struct ether_header );
2327 m->m_len -= sizeof ( struct ether_header );
2328 m->m_data += sizeof ( struct ether_header );
2329
2330 /* Feed the packet to upper layer. */
2331 ether_input( &sc->sc_if, eh, m );
2332 return 0;
2333}
2334
2335/*
2336 * Write an mbuf chain to the transmission buffer memory using 16 bit PIO.
2337 * Returns number of bytes actually written, including length word.
2338 *
2339 * If an mbuf chain is too long for an Ethernet frame, it is not sent.
2340 * Packets shorter than Ethernet minimum are legal, and we pad them
2341 * before sending out. An exception is "partial" packets which are
2342 * shorter than mandatory Ethernet header.
2343 *
2344 * I wrote a code for an experimental "delayed padding" technique.
2345 * When employed, it postpones the padding process for short packets.
2346 * If xmit() occured at the moment, the padding process is omitted, and
2347 * garbages are sent as pad data. If next packet is stored in the
2348 * transmission buffer before xmit(), write_mbuf() pads the previous
2349 * packet before transmitting new packet. This *may* gain the
2350 * system performance (slightly).
2351 */
2352static void
2353fe_write_mbufs ( struct fe_softc *sc, struct mbuf *m )
2354{
2355 u_short addr_bmpr8 = sc->addr + FE_BMPR8;
2356 u_short length, len;
2357 short pad;
2358 struct mbuf *mp;
2359 u_char *data;
2360 u_short savebyte; /* WARNING: Architecture dependent! */
2361#define NO_PENDING_BYTE 0xFFFF
2362
2363#if FE_DELAYED_PADDING
2364 /* Do the "delayed padding." */
2365 pad = sc->txb_padding >> 1;
2366 if ( pad > 0 ) {
2367 while ( --pad >= 0 ) {
2368 outw( addr_bmpr8, 0 );
2369 }
2370 sc->txb_padding = 0;
2371 }
2372#endif
2373
2374#if FE_DEBUG >= 2
2375 /* First, count up the total number of bytes to copy */
2376 length = 0;
2377 for ( mp = m; mp != NULL; mp = mp->m_next ) {
2378 length += mp->m_len;
2379 }
2380 /* Check if this matches the one in the packet header. */
2381 if ( length != m->m_pkthdr.len ) {
2382 log( LOG_WARNING, "fe%d: packet length mismatch? (%d/%d)\n",
2383 sc->sc_unit, length, m->m_pkthdr.len );
2384 }
2385#else
2386 /* Just use the length value in the packet header. */
2387 length = m->m_pkthdr.len;
2388#endif
2389
2390#if FE_DEBUG >= 1
2391 /*
2392 * Should never send big packets. If such a packet is passed,
2393 * it should be a bug of upper layer. We just ignore it.
2394 * ... Partial (too short) packets, neither.
2395 */
2396 if ( length > ETHER_MAX_LEN || length < ETHER_HDR_SIZE ) {
2397 log( LOG_ERR,
2398 "fe%d: got a %s packet (%u bytes) to send\n",
2399 sc->sc_unit,
2400 length < ETHER_HDR_SIZE ? "partial" : "big", length );
2401 sc->sc_if.if_oerrors++;
2402 return;
2403 }
2404#endif
2405
2406 /*
2407 * Put the length word for this frame.
2408 * Does 86960 accept odd length? -- Yes.
2409 * Do we need to pad the length to minimum size by ourselves?
2410 * -- Generally yes. But for (or will be) the last
2411 * packet in the transmission buffer, we can skip the
2412 * padding process. It may gain performance slightly. FIXME.
2413 */
2414 outw( addr_bmpr8, max( length, ETHER_MIN_LEN ) );
2415
2416 /*
2417 * Update buffer status now.
2418 * Truncate the length up to an even number, since we use outw().
2419 */
2420 length = ( length + 1 ) & ~1;
2421 sc->txb_free -= FE_DATA_LEN_LEN + max( length, ETHER_MIN_LEN );
2422 sc->txb_count++;
2423
2424#if FE_DELAYED_PADDING
2425 /* Postpone the packet padding if necessary. */
2426 if ( length < ETHER_MIN_LEN ) {
2427 sc->txb_padding = ETHER_MIN_LEN - length;
2428 }
2429#endif
2430
2431 /*
2432 * Transfer the data from mbuf chain to the transmission buffer.
2433 * MB86960 seems to require that data be transferred as words, and
2434 * only words. So that we require some extra code to patch
2435 * over odd-length mbufs.
2436 */
2437 savebyte = NO_PENDING_BYTE;
2438 for ( mp = m; mp != 0; mp = mp->m_next ) {
2439
2440 /* Ignore empty mbuf. */
2441 len = mp->m_len;
2442 if ( len == 0 ) continue;
2443
2444 /* Find the actual data to send. */
2445 data = mtod(mp, caddr_t);
2446
2447 /* Finish the last byte. */
2448 if ( savebyte != NO_PENDING_BYTE ) {
2449 outw( addr_bmpr8, savebyte | ( *data << 8 ) );
2450 data++;
2451 len--;
2452 savebyte = NO_PENDING_BYTE;
2453 }
2454
2455 /* output contiguous words */
2456 if (len > 1) {
2457 outsw( addr_bmpr8, data, len >> 1);
2458 data += len & ~1;
2459 len &= 1;
2460 }
2461
2462 /* Save a remaining byte, if there is one. */
2463 if ( len > 0 ) {
2464 savebyte = *data;
2465 }
2466 }
2467
2468 /* Spit the last byte, if the length is odd. */
2469 if ( savebyte != NO_PENDING_BYTE ) {
2470 outw( addr_bmpr8, savebyte );
2471 }
2472
2473#if ! FE_DELAYED_PADDING
2474 /*
2475 * Pad the packet to the minimum length if necessary.
2476 */
2477 pad = ( ETHER_MIN_LEN >> 1 ) - ( length >> 1 );
2478 while ( --pad >= 0 ) {
2479 outw( addr_bmpr8, 0 );
2480 }
2481#endif
2482}
2483
2484/*
2485 * Compute hash value for an Ethernet address
2486 */
2487static int
2488fe_hash ( u_char * ep )
2489{
2490#define FE_HASH_MAGIC_NUMBER 0xEDB88320L
2491
2492 u_long hash = 0xFFFFFFFFL;
2493 int i, j;
2494 u_char b;
2495 u_long m;
2496
2497 for ( i = ETHER_ADDR_LEN; --i >= 0; ) {
2498 b = *ep++;
2499 for ( j = 8; --j >= 0; ) {
2500 m = hash;
2501 hash >>= 1;
2502 if ( ( m ^ b ) & 1 ) hash ^= FE_HASH_MAGIC_NUMBER;
2503 b >>= 1;
2504 }
2505 }
2506 return ( ( int )( hash >> 26 ) );
2507}
2508
2509/*
2510 * Compute the multicast address filter from the
2511 * list of multicast addresses we need to listen to.
2512 */
2513static struct fe_filter
2514fe_mcaf ( struct fe_softc *sc )
2515{
2516 int index;
2517 struct fe_filter filter;
2518 struct ether_multi *enm;
2519 struct ether_multistep step;
2520
2521 filter = fe_filter_nothing;
2522 ETHER_FIRST_MULTI(step, &sc->arpcom, enm);
2523 while ( enm != NULL) {
2524 if ( bcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN) ) {
2525 return ( fe_filter_all );
2526 }
2527 index = fe_hash( enm->enm_addrlo );
2528#if FE_DEBUG >= 4
2529 log( LOG_INFO, "fe%d: hash(%6D) == %d\n",
2530 sc->sc_unit, enm->enm_addrlo , ":", index );
2531#endif
2532
2533 filter.data[index >> 3] |= 1 << (index & 7);
2534 ETHER_NEXT_MULTI(step, enm);
2535 }
2536 return ( filter );
2537}
2538
2539/*
2540 * Calculate a new "multicast packet filter" and put the 86960
2541 * receiver in appropriate mode.
2542 */
2543static void
2544fe_setmode ( struct fe_softc *sc )
2545{
2546 int flags = sc->sc_if.if_flags;
2547
2548 /*
2549 * If the interface is not running, we postpone the update
2550 * process for receive modes and multicast address filter
2551 * until the interface is restarted. It reduces some
2552 * complicated job on maintaining chip states. (Earlier versions
2553 * of this driver had a bug on that point...)
2554 *
2555 * To complete the trick, fe_init() calls fe_setmode() after
2556 * restarting the interface.
2557 */
2558 if ( !( flags & IFF_RUNNING ) ) return;
2559
2560 /*
2561 * Promiscuous mode is handled separately.
2562 */
2563 if ( flags & IFF_PROMISC ) {
2564 /*
2565 * Program 86960 to receive all packets on the segment
2566 * including those directed to other stations.
2567 * Multicast filter stored in MARs are ignored
2568 * under this setting, so we don't need to update it.
2569 *
2570 * Promiscuous mode in FreeBSD 2 is used solely by
2571 * BPF, and BPF only listens to valid (no error) packets.
2572 * So, we ignore errornous ones even in this mode.
2573 * (Older versions of fe driver mistook the point.)
2574 */
2575 outb( sc->addr + FE_DLCR5,
2576 sc->proto_dlcr5 | FE_D5_AFM0 | FE_D5_AFM1 );
2577 sc->filter_change = 0;
2578
2579#if FE_DEBUG >= 3
2580 log( LOG_INFO, "fe%d: promiscuous mode\n", sc->sc_unit );
2581#endif
2582 return;
2583 }
2584
2585 /*
2586 * Turn the chip to the normal (non-promiscuous) mode.
2587 */
2588 outb( sc->addr + FE_DLCR5, sc->proto_dlcr5 | FE_D5_AFM1 );
2589
2590 /*
2591 * Find the new multicast filter value.
2592 * I'm not sure we have to handle modes other than MULTICAST.
2593 * Who sets ALLMULTI? Who turns MULTICAST off? FIXME.
2594 */
2595 if ( flags & IFF_ALLMULTI ) {
2596 sc->filter = fe_filter_all;
2597 } else if ( flags & IFF_MULTICAST ) {
2598 sc->filter = fe_mcaf( sc );
2599 } else {
2600 sc->filter = fe_filter_nothing;
2601 }
2602 sc->filter_change = 1;
2603
2604#if FE_DEBUG >= 3
2605 log( LOG_INFO, "fe%d: address filter:"
2606 " [%02x %02x %02x %02x %02x %02x %02x %02x]\n",
2607 sc->sc_unit,
2608 sc->filter.data[0], sc->filter.data[1],
2609 sc->filter.data[2], sc->filter.data[3],
2610 sc->filter.data[4], sc->filter.data[5],
2611 sc->filter.data[6], sc->filter.data[7] );
2612#endif
2613
2614 /*
2615 * We have to update the multicast filter in the 86960, A.S.A.P.
2616 *
2617 * Note that the DLC (Data Linc Control unit, i.e. transmitter
2618 * and receiver) must be stopped when feeding the filter, and
2619 * DLC trushes all packets in both transmission and receive
2620 * buffers when stopped.
2621 *
2622 * ... Are the above sentenses correct? I have to check the
2623 * manual of the MB86960A. FIXME.
2624 *
2625 * To reduce the packet lossage, we delay the filter update
2626 * process until buffers are empty.
2627 */
2628 if ( sc->txb_sched == 0 && sc->txb_count == 0
2629 && !( inb( sc->addr + FE_DLCR1 ) & FE_D1_PKTRDY ) ) {
2630 /*
2631 * Buffers are (apparently) empty. Load
2632 * the new filter value into MARs now.
2633 */
2634 fe_loadmar(sc);
2635 } else {
2636 /*
2637 * Buffers are not empty. Mark that we have to update
2638 * the MARs. The new filter will be loaded by feintr()
2639 * later.
2640 */
2641#if FE_DEBUG >= 4
2642 log( LOG_INFO, "fe%d: filter change delayed\n", sc->sc_unit );
2643#endif
2644 }
2645}
2646
2647/*
2648 * Load a new multicast address filter into MARs.
2649 *
2650 * The caller must have splimp'ed befor fe_loadmar.
2651 * This function starts the DLC upon return. So it can be called only
2652 * when the chip is working, i.e., from the driver's point of view, when
2653 * a device is RUNNING. (I mistook the point in previous versions.)
2654 */
2655static void
2656fe_loadmar ( struct fe_softc * sc )
2657{
2658 /* Stop the DLC (transmitter and receiver). */
2659 outb( sc->addr + FE_DLCR6, sc->proto_dlcr6 | FE_D6_DLC_DISABLE );
2660
2661 /* Select register bank 1 for MARs. */
2662 outb( sc->addr + FE_DLCR7,
2663 sc->proto_dlcr7 | FE_D7_RBS_MAR | FE_D7_POWER_UP );
2664
2665 /* Copy filter value into the registers. */
2666 outblk( sc->addr + FE_MAR8, sc->filter.data, FE_FILTER_LEN );
2667
2668 /* Restore the bank selection for BMPRs (i.e., runtime registers). */
2669 outb( sc->addr + FE_DLCR7,
2670 sc->proto_dlcr7 | FE_D7_RBS_BMPR | FE_D7_POWER_UP );
2671
2672 /* Restart the DLC. */
2673 outb( sc->addr + FE_DLCR6, sc->proto_dlcr6 | FE_D6_DLC_ENABLE );
2674
2675 /* We have just updated the filter. */
2676 sc->filter_change = 0;
2677
2678#if FE_DEBUG >= 3
2679 log( LOG_INFO, "fe%d: address filter changed\n", sc->sc_unit );
2680#endif
2681}
2682
2683/*
2684 * Copy the physical (Ethernet) address into the "data link" address
2685 * entry of the address list for an interface.
2686 * This is (said to be) useful for netstat(1) to keep track of which
2687 * interface is which.
2688 *
2689 * What I'm not sure on this function is, why this is a driver's function.
2690 * Probably this should be moved to somewhere independent to a specific
2691 * hardware, such as if_ehtersubr.c. FIXME.
2692 */
2693static void
2694fe_setlinkaddr ( struct fe_softc * sc )
2695{
2696 struct ifaddr *ifa;
2697 struct sockaddr_dl * sdl;
2698
2699 /*
2700 * Search down the ifa address list looking for the AF_LINK type entry.
2701 */
2702 for ( ifa = sc->sc_if.if_addrlist; ifa != NULL; ifa = ifa->ifa_next ) {
2703 if ( ifa->ifa_addr != NULL
2704 && ifa->ifa_addr->sa_family == AF_LINK ) {
2705
2706 /*
2707 * We have found an AF_LINK type entry.
2708 * Fill in the link-level address for this interface
2709 */
2710 sdl = (struct sockaddr_dl *) ifa->ifa_addr;
2711 sdl->sdl_type = IFT_ETHER;
2712 sdl->sdl_alen = ETHER_ADDR_LEN;
2713 sdl->sdl_slen = 0;
2714 bcopy(sc->sc_enaddr, LLADDR(sdl), ETHER_ADDR_LEN);
2715#if FE_DEBUG >= 3
2716 log( LOG_INFO, "fe%d: link address set\n",
2717 sc->sc_unit );
2718#endif
2719 return;
2720 }
2721 }
2722}
2723
2724#if FE_DEBUG >= 1
2725static void
2726fe_dump ( int level, struct fe_softc * sc, char * message )
2727{
2728 log( level, "fe%d: %s,"
2729 " DLCR = %02x %02x %02x %02x %02x %02x %02x %02x,"
2730 " BMPR = xx xx %02x %02x %02x %02x %02x %02x,"
2731 " asic = %02x %02x %02x %02x %02x %02x %02x %02x"
2732 " + %02x %02x %02x %02x %02x %02x %02x %02x\n",
2733 sc->sc_unit, message ? message : "registers",
2734 inb( sc->addr + FE_DLCR0 ), inb( sc->addr + FE_DLCR1 ),
2735 inb( sc->addr + FE_DLCR2 ), inb( sc->addr + FE_DLCR3 ),
2736 inb( sc->addr + FE_DLCR4 ), inb( sc->addr + FE_DLCR5 ),
2737 inb( sc->addr + FE_DLCR6 ), inb( sc->addr + FE_DLCR7 ),
2738 inb( sc->addr + FE_BMPR10 ), inb( sc->addr + FE_BMPR11 ),
2739 inb( sc->addr + FE_BMPR12 ), inb( sc->addr + FE_BMPR13 ),
2740 inb( sc->addr + FE_BMPR14 ), inb( sc->addr + FE_BMPR15 ),
2741 inb( sc->addr + 0x10 ), inb( sc->addr + 0x11 ),
2742 inb( sc->addr + 0x12 ), inb( sc->addr + 0x13 ),
2743 inb( sc->addr + 0x14 ), inb( sc->addr + 0x15 ),
2744 inb( sc->addr + 0x16 ), inb( sc->addr + 0x17 ),
2745 inb( sc->addr + 0x18 ), inb( sc->addr + 0x19 ),
2746 inb( sc->addr + 0x1A ), inb( sc->addr + 0x1B ),
2747 inb( sc->addr + 0x1C ), inb( sc->addr + 0x1D ),
2748 inb( sc->addr + 0x1E ), inb( sc->addr + 0x1F ) );
2749}
2750#endif
2 * All Rights Reserved, Copyright (C) Fujitsu Limited 1995
3 *
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.
9 *
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
20 * SUCH DAMAGE.
21 */
22
23#define FE_VERSION "if_fe.c ver. 0.8a"
24
25/*
26 * Device driver for Fujitsu MB86960A/MB86965A based Ethernet cards.
27 * To be used with FreeBSD 2.0 RELEASE.
28 * Contributed by M.S. <seki@sysrap.cs.fujitsu.co.jp>
29 *
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 (i.e., FMV-181 and FMV-182) and Allied-
33 * Telesis AT1700 series and RE2000 series. There are some
34 * unnecessary hooks embedded, which are primarily intended to support
35 * other types of Ethernet cards, but the author is not sure whether
36 * they are useful.
37 *
38 * This software is a derivative work of if_ed.c version 1.56 by David
39 * Greenman available as a part of FreeBSD 2.0 RELEASE source distribution.
40 *
41 * The following lines are retained from the original if_ed.c:
42 *
43 * Copyright (C) 1993, David Greenman. This software may be used, modified,
44 * copied, distributed, and sold, in both source and binary form provided
45 * that the above copyright and these terms are retained. Under no
46 * circumstances is the author responsible for the proper functioning
47 * of this software, nor does the author assume any responsibility
48 * for damages incurred with its use.
49 */
50
51#include "fe.h"
52#include "bpfilter.h"
53
54#include <sys/param.h>
55#include <sys/systm.h>
56#include <sys/conf.h>
57#include <sys/errno.h>
58#include <sys/ioctl.h>
59#include <sys/mbuf.h>
60#include <sys/socket.h>
61#include <sys/syslog.h>
62#include <sys/devconf.h>
63
64#include <net/if.h>
65#include <net/if_dl.h>
66#include <net/if_types.h>
67
68#ifdef INET
69#include <netinet/in.h>
70#include <netinet/in_systm.h>
71#include <netinet/in_var.h>
72#include <netinet/ip.h>
73#include <netinet/if_ether.h>
74#endif
75
76#ifdef IPX
77#include <netipx/ipx.h>
78#include <netipx/ipx_if.h>
79#endif
80
81#ifdef NS
82#include <netns/ns.h>
83#include <netns/ns_if.h>
84#endif
85
86#if NBPFILTER > 0
87#include <net/bpf.h>
88#include <net/bpfdesc.h>
89#endif
90
91#include <machine/clock.h>
92
93#include <i386/isa/isa.h>
94#include <i386/isa/isa_device.h>
95#include <i386/isa/icu.h>
96
97#include <i386/isa/ic/mb86960.h>
98#include <i386/isa/if_fereg.h>
99
100#ifdef __GNUC__
101#define INLINE inline
102#else
103#define INLINE
104#endif
105
106/*
107 * Default settings for fe driver specific options.
108 * They can be set in config file by "options" statements.
109 */
110
111/*
112 * Debug control.
113 * 0: No debug at all. All debug specific codes are stripped off.
114 * 1: Silent. No debug messages are logged except emergent ones.
115 * 2: Brief. Lair events and/or important information are logged.
116 * 3: Detailed. Logs all information which *may* be useful for debugging.
117 * 4: Trace. All actions in the driver is logged. Super verbose.
118 */
119#ifndef FE_DEBUG
120#define FE_DEBUG 1
121#endif
122
123/*
124 * Delay padding of short transmission packets to minimum Ethernet size.
125 * This may or may not gain performance. An EXPERIMENTAL option.
126 */
127#ifndef FE_DELAYED_PADDING
128#define FE_DELAYED_PADDING 0
129#endif
130
131/*
132 * Transmit just one packet per a "send"command to 86960.
133 * This option is intended for performance test. An EXPERIMENTAL option.
134 */
135#ifndef FE_SINGLE_TRANSMISSION
136#define FE_SINGLE_TRANSMISSION 0
137#endif
138
139/*
140 * Device configuration flags.
141 */
142
143/* DLCR6 settings. */
144#define FE_FLAGS_DLCR6_VALUE 0x007F
145
146/* Force DLCR6 override. */
147#define FE_FLAGS_OVERRIDE_DLCR6 0x0080
148
149/* A cludge for PCMCIA support. */
150#define FE_FLAGS_PCMCIA 0x8000
151
152/* Shouldn't this be defined somewhere else such as isa_device.h? */
153#define NO_IOADDR 0xFFFFFFFF
154
155/* Identification of the driver version. */
156static char const fe_version [] = FE_VERSION " / " FE_REG_VERSION;
157
158/*
159 * Supported hardware (Ethernet card) types
160 * This information is currently used only for debugging
161 */
162enum fe_type
163{
164 /* For cards which are successfully probed but not identified. */
165 FE_TYPE_UNKNOWN,
166
167 /* Fujitsu FMV-180 series. */
168 FE_TYPE_FMV181,
169 FE_TYPE_FMV182,
170
171 /* Allied-Telesis AT1700 series and RE2000 series. */
172 FE_TYPE_AT1700,
173
174 /* PCMCIA by Fujitsu. */
175 FE_TYPE_MBH10302,
176 FE_TYPE_MBH10304,
177
178 /* More can be here. */
179};
180
181/*
182 * Data type for a multicast address filter on 86960.
183 */
184struct fe_filter { u_char data [ FE_FILTER_LEN ]; };
185
186/*
187 * Special filter values.
188 */
189static struct fe_filter const fe_filter_nothing = { FE_FILTER_NOTHING };
190static struct fe_filter const fe_filter_all = { FE_FILTER_ALL };
191
192/*
193 * fe_softc: per line info and status
194 */
195static struct fe_softc {
196
197 /* Used by "common" codes. */
198 struct arpcom arpcom; /* ethernet common */
199
200 /* Used by config codes. */
201 struct kern_devconf kdc;/* Kernel configuration database info. */
202
203 /* Set by probe() and not modified in later phases. */
204 enum fe_type type; /* interface type code */
205 char * typestr; /* printable name of the interface. */
206 u_short addr; /* MB86960A I/O base address */
207 u_short txb_size; /* size of TX buffer, in bytes */
208 u_char proto_dlcr4; /* DLCR4 prototype. */
209 u_char proto_dlcr5; /* DLCR5 prototype. */
210 u_char proto_dlcr6; /* DLCR6 prototype. */
211 u_char proto_dlcr7; /* DLCR7 prototype. */
212
213 /* Vendor specific hooks. */
214 void ( * init )( struct fe_softc * ); /* Just before fe_init(). */
215 void ( * stop )( struct fe_softc * ); /* Just after fe_stop(). */
216
217 /* For BPF. */
218 caddr_t bpf; /* BPF "magic cookie" */
219
220 /* Transmission buffer management. */
221 u_short txb_free; /* free bytes in TX buffer */
222 u_char txb_count; /* number of packets in TX buffer */
223 u_char txb_sched; /* number of scheduled packets */
224 u_char txb_padding; /* number of delayed padding bytes */
225
226 /* Multicast address filter management. */
227 u_char filter_change; /* MARs must be changed ASAP. */
228 struct fe_filter filter;/* new filter value. */
229
230} fe_softc[NFE];
231
232/* Frequently accessed members in arpcom and kdc. */
233#define sc_if arpcom.ac_if
234#define sc_unit arpcom.ac_if.if_unit
235#define sc_enaddr arpcom.ac_enaddr
236#define sc_dcstate kdc.kdc_state
237#define sc_description kdc.kdc_description
238
239/*
240 * Some entry functions receive a "struct ifnet *" typed pointer as an
241 * argument. It points to arpcom.ac_if of our softc. Remember arpcom.ac_if
242 * is located at very first of the fe_softc struct. So, there is no
243 * difference between "struct fe_softc *" and "struct ifnet *" at the machine
244 * language level. We just cast to turn a "struct ifnet *" value into "struct
245 * fe_softc * value". If this were C++, we would need no such cast at all.
246 */
247#define IFNET2SOFTC(P) ( ( struct fe_softc * )(P) )
248
249/* Standard driver entry points. These can be static. */
250static int fe_probe ( struct isa_device * );
251static int fe_attach ( struct isa_device * );
252static void fe_init ( int );
253static int fe_ioctl ( struct ifnet *, int, caddr_t );
254static void fe_start ( struct ifnet * );
255static void fe_reset ( int );
256static void fe_watchdog ( struct ifnet * );
257
258/* Local functions. Order of declaration is confused. FIXME. */
259static int fe_probe_fmv ( struct isa_device *, struct fe_softc * );
260static int fe_probe_ati ( struct isa_device *, struct fe_softc * );
261static int fe_probe_mbh ( struct isa_device *, struct fe_softc * );
262static void fe_init_mbh ( struct fe_softc * );
263static int fe_get_packet ( struct fe_softc *, u_short );
264static void fe_stop ( int );
265static void fe_tint ( struct fe_softc *, u_char );
266static void fe_rint ( struct fe_softc *, u_char );
267static void fe_xmit ( struct fe_softc * );
268static void fe_write_mbufs ( struct fe_softc *, struct mbuf * );
269static struct fe_filter
270 fe_mcaf ( struct fe_softc * );
271static int fe_hash ( u_char * );
272static void fe_setmode ( struct fe_softc * );
273static void fe_loadmar ( struct fe_softc * );
274static void fe_setlinkaddr ( struct fe_softc * );
275#if FE_DEBUG >= 1
276static void fe_dump ( int, struct fe_softc *, char * );
277#endif
278
279/* Ethernet constants. To be defined in if_ehter.h? FIXME. */
280#define ETHER_MIN_LEN 60 /* with header, without CRC. */
281#define ETHER_MAX_LEN 1514 /* with header, without CRC. */
282#define ETHER_ADDR_LEN 6 /* number of bytes in an address. */
283#define ETHER_TYPE_LEN 2 /* number of bytes in a data type field. */
284#define ETHER_HDR_SIZE 14 /* src addr, dst addr, and data type. */
285#define ETHER_CRC_LEN 4 /* number of bytes in CRC field. */
286
287/* Driver struct used in the config code. This must be public (external.) */
288struct isa_driver fedriver =
289{
290 fe_probe,
291 fe_attach,
292 "fe",
293 0 /* Assume we are insensitive. FIXME. */
294};
295
296/* Initial value for a kdc struct. */
297static struct kern_devconf const fe_kdc_template =
298{
299 0, 0, 0,
300 "fe", 0, { MDDT_ISA, 0, "net" },
301 isa_generic_externalize, 0, 0, ISA_EXTERNALLEN,
302 &kdc_isa0, /* We are an ISA device. */
303 0,
304 DC_UNCONFIGURED, /* Not yet configured. */
305 "Ethernet (fe)", /* Tentative description (filled in later.) */
306 DC_CLS_NETIF /* We are a network interface. */
307};
308
309/*
310 * Fe driver specific constants which relate to 86960/86965.
311 * They are here (not in if_fereg.h), since selection of those
312 * values depend on driver design. I want to keep definitions in
313 * if_fereg.h "clean", so that if someone wrote another driver
314 * for 86960/86965, if_fereg.h were usable unchanged.
315 *
316 * The above statement sounds somothing like it's better to name
317 * it "ic/mb86960.h" but "if_fereg.h"... Should I do so? FIXME.
318 */
319
320/* Interrupt masks */
321#define FE_TMASK ( FE_D2_COLL16 | FE_D2_TXDONE )
322#define FE_RMASK ( FE_D3_OVRFLO | FE_D3_CRCERR \
323 | FE_D3_ALGERR | FE_D3_SRTPKT | FE_D3_PKTRDY )
324
325/* Maximum number of iterrations for a receive interrupt. */
326#define FE_MAX_RECV_COUNT ( ( 65536 - 2048 * 2 ) / 64 )
327 /* Maximum size of SRAM is 65536,
328 * minimum size of transmission buffer in fe is 2x2KB,
329 * and minimum amount of received packet including headers
330 * added by the chip is 64 bytes.
331 * Hence FE_MAX_RECV_COUNT is the upper limit for number
332 * of packets in the receive buffer. */
333
334/*
335 * Convenient routines to access contiguous I/O ports.
336 */
337
338static INLINE void
339inblk ( u_short addr, u_char * mem, int len )
340{
341 while ( --len >= 0 ) {
342 *mem++ = inb( addr++ );
343 }
344}
345
346static INLINE void
347outblk ( u_short addr, u_char const * mem, int len )
348{
349 while ( --len >= 0 ) {
350 outb( addr++, *mem++ );
351 }
352}
353
354/*
355 * Hardware probe routines.
356 */
357
358/* How and where to probe; to support automatic I/O address detection. */
359struct fe_probe_list
360{
361 int ( * probe ) ( struct isa_device *, struct fe_softc * );
362 u_short const * addresses;
363};
364
365/* Lists of possible addresses. */
366static u_short const fe_fmv_addr [] =
367 { 0x220, 0x240, 0x260, 0x280, 0x2A0, 0x2C0, 0x300, 0x340, 0 };
368static u_short const fe_ati_addr [] =
369 { 0x240, 0x260, 0x280, 0x2A0, 0x300, 0x320, 0x340, 0x380, 0 };
370
371static struct fe_probe_list const fe_probe_list [] =
372{
373 { fe_probe_fmv, fe_fmv_addr },
374 { fe_probe_ati, fe_ati_addr },
375 { fe_probe_mbh, NULL }, /* PCMCIAs cannot be auto-detected. */
376 { NULL, NULL }
377};
378
379/*
380 * Determine if the device is present
381 *
382 * on entry:
383 * a pointer to an isa_device struct
384 * on exit:
385 * zero if device not found
386 * or number of i/o addresses used (if found)
387 */
388
389static int
390fe_probe ( struct isa_device * isa_dev )
391{
392 struct fe_softc * sc, * u;
393 int nports;
394 struct fe_probe_list const * list;
395 u_short const * addr;
396 u_short single [ 2 ];
397
398 /* Initialize "minimum" parts of our softc. */
399 sc = &fe_softc[ isa_dev->id_unit ];
400 sc->sc_unit = isa_dev->id_unit;
401
402#if FE_DEBUG >= 2
403 log( LOG_INFO, "fe%d: %s\n", sc->sc_unit, fe_version );
404#endif
405
406#ifndef DEV_LKM
407 /* Fill the device config data and register it. */
408 sc->kdc = fe_kdc_template;
409 sc->kdc.kdc_unit = sc->sc_unit;
410 sc->kdc.kdc_parentdata = isa_dev;
411 dev_attach( &sc->kdc );
412#endif
413
414 /* Probe each possibility, one at a time. */
415 for ( list = fe_probe_list; list->probe != NULL; list++ ) {
416
417 if ( isa_dev->id_iobase != NO_IOADDR ) {
418 /* Probe one specific address. */
419 single[ 0 ] = isa_dev->id_iobase;
420 single[ 1 ] = 0;
421 addr = single;
422 } else if ( list->addresses != NULL ) {
423 /* Auto detect. */
424 addr = list->addresses;
425 } else {
426 /* We need a list of addresses to do auto detect. */
427 continue;
428 }
429
430 /* Probe all possible addresses for the board. */
431 while ( *addr != 0 ) {
432
433 /* Don't probe already used address. */
434 for ( u = &fe_softc[0]; u < &fe_softc[NFE]; u++ ) {
435 if ( u->addr == *addr ) break;
436 }
437 if ( u < &fe_softc[NFE] ) continue;
438
439 /* Probe an address. */
440 sc->addr = *addr;
441 nports = list->probe( isa_dev, sc );
442 if ( nports > 0 ) {
443 /* Found. */
444 isa_dev->id_iobase = *addr;
445 return ( nports );
446 }
447
448 /* Try next. */
449 sc->addr = 0;
450 addr++;
451 }
452 }
453
454 /* Probe failed. */
455 return ( 0 );
456}
457
458/*
459 * Check for specific bits in specific registers have specific values.
460 */
461struct fe_simple_probe_struct
462{
463 u_char port; /* Offset from the base I/O address. */
464 u_char mask; /* Bits to be checked. */
465 u_char bits; /* Values to be compared against. */
466};
467
468static INLINE int
469fe_simple_probe ( u_short addr, struct fe_simple_probe_struct const * sp )
470{
471 struct fe_simple_probe_struct const * p;
472
473 for ( p = sp; p->mask != 0; p++ ) {
474 if ( ( inb( addr + p->port ) & p->mask ) != p->bits ) {
475 return ( 0 );
476 }
477 }
478 return ( 1 );
479}
480
481/*
482 * Routines to read all bytes from the config EEPROM through MB86965A.
483 * I'm not sure what exactly I'm doing here... I was told just to follow
484 * the steps, and it worked. Could someone tell me why the following
485 * code works? (Or, why all similar codes I tried previously doesn't
486 * work.) FIXME.
487 */
488
489static INLINE void
490strobe ( u_short bmpr16 )
491{
492 /*
493 * Output same value twice. To speed-down execution?
494 */
495 outb( bmpr16, FE_B16_SELECT );
496 outb( bmpr16, FE_B16_SELECT );
497 outb( bmpr16, FE_B16_SELECT | FE_B16_CLOCK );
498 outb( bmpr16, FE_B16_SELECT | FE_B16_CLOCK );
499 outb( bmpr16, FE_B16_SELECT );
500 outb( bmpr16, FE_B16_SELECT );
501}
502
503static void
504fe_read_eeprom ( struct fe_softc * sc, u_char * data )
505{
506 u_short bmpr16 = sc->addr + FE_BMPR16;
507 u_short bmpr17 = sc->addr + FE_BMPR17;
508 u_char n, val, bit;
509 u_char save16, save17;
510
511 /* Save old values of the registers. */
512 save16 = inb( bmpr16 );
513 save17 = inb( bmpr17 );
514
515 /* Read bytes from EEPROM; two bytes per an iterration. */
516 for ( n = 0; n < FE_EEPROM_SIZE / 2; n++ ) {
517
518 /* Reset the EEPROM interface. */
519 outb( bmpr16, 0x00 );
520 outb( bmpr17, 0x00 );
521 outb( bmpr16, FE_B16_SELECT );
522
523 /* Start EEPROM access. */
524 outb( bmpr17, FE_B17_DATA );
525 strobe( bmpr16 );
526
527 /* Pass the iterration count to the chip. */
528 val = 0x80 | n;
529 for ( bit = 0x80; bit != 0x00; bit >>= 1 ) {
530 outb( bmpr17, ( val & bit ) ? FE_B17_DATA : 0 );
531 strobe( bmpr16 );
532 }
533 outb( bmpr17, 0x00 );
534
535 /* Read a byte. */
536 val = 0;
537 for ( bit = 0x80; bit != 0x00; bit >>= 1 ) {
538 strobe( bmpr16 );
539 if ( inb( bmpr17 ) & FE_B17_DATA ) {
540 val |= bit;
541 }
542 }
543 *data++ = val;
544
545 /* Read one more byte. */
546 val = 0;
547 for ( bit = 0x80; bit != 0x00; bit >>= 1 ) {
548 strobe( bmpr16 );
549 if ( inb( bmpr17 ) & FE_B17_DATA ) {
550 val |= bit;
551 }
552 }
553 *data++ = val;
554 }
555
556 /* Restore register values, in the case we had no 86965. */
557 outb( bmpr16, save16 );
558 outb( bmpr17, save17 );
559
560#if FE_DEBUG >= 3
561 /* Report what we got. */
562 data -= FE_EEPROM_SIZE;
563 log( LOG_INFO, "fe%d: EEPROM:"
564 " %02x%02x%02x%02x %02x%02x%02x%02x -"
565 " %02x%02x%02x%02x %02x%02x%02x%02x -"
566 " %02x%02x%02x%02x %02x%02x%02x%02x -"
567 " %02x%02x%02x%02x %02x%02x%02x%02x\n",
568 sc->sc_unit,
569 data[ 0], data[ 1], data[ 2], data[ 3],
570 data[ 4], data[ 5], data[ 6], data[ 7],
571 data[ 8], data[ 9], data[10], data[11],
572 data[12], data[13], data[14], data[15],
573 data[16], data[17], data[18], data[19],
574 data[20], data[21], data[22], data[23],
575 data[24], data[25], data[26], data[27],
576 data[28], data[29], data[30], data[31] );
577#endif
578}
579
580/*
581 * Hardware (vendor) specific probe routines.
582 */
583
584/*
585 * Probe and initialization for Fujitsu FMV-180 series boards
586 */
587static int
588fe_probe_fmv ( struct isa_device *isa_dev, struct fe_softc * sc )
589{
590 int i, n;
591
592 static u_short const ioaddr [ 8 ] =
593 { 0x220, 0x240, 0x260, 0x280, 0x2A0, 0x2C0, 0x300, 0x340 };
594 static u_short const irqmap [ 4 ] =
595 { IRQ3, IRQ7, IRQ10, IRQ15 };
596
597 static struct fe_simple_probe_struct const probe_table [] = {
598 { FE_DLCR2, 0x70, 0x00 },
599 { FE_DLCR4, 0x08, 0x00 },
600 /* { FE_DLCR5, 0x80, 0x00 }, Doesn't work. */
601
602 { FE_FMV0, FE_FMV0_MAGIC_MASK, FE_FMV0_MAGIC_VALUE },
603 { FE_FMV1, FE_FMV1_CARDID_MASK, FE_FMV1_CARDID_ID },
604 { FE_FMV3, FE_FMV3_EXTRA_MASK, FE_FMV3_EXTRA_VALUE },
605#if 1
606 /*
607 * Test *vendor* part of the station address for Fujitsu.
608 * The test will gain reliability of probe process, but
609 * it rejects FMV-180 clone boards manufactured by other vendors.
610 * We have to turn the test off when such cards are made available.
611 */
612 { FE_FMV4, 0xFF, 0x00 },
613 { FE_FMV5, 0xFF, 0x00 },
614 { FE_FMV6, 0xFF, 0x0E },
615#else
616 /*
617 * We can always verify the *first* 2 bits (in Ehternet
618 * bit order) are "no multicast" and "no local" even for
619 * unknown vendors.
620 */
621 { FE_FMV4, 0x03, 0x00 },
622#endif
623 { 0 }
624 };
625
626#if 0
627 /*
628 * Dont probe at all if the config says we are PCMCIA...
629 */
630 if ( isa_dev->id_flags & FE_FLAGS_PCMCIA ) return ( 0 );
631#endif
632
633 /*
634 * See if the sepcified address is possible for FMV-180 series.
635 */
636 for ( i = 0; i < 8; i++ ) {
637 if ( ioaddr[ i ] == sc->addr ) break;
638 }
639 if ( i == 8 ) return 0;
640
641 /* Simple probe. */
642 if ( !fe_simple_probe( sc->addr, probe_table ) ) return 0;
643
644 /* Check if our I/O address matches config info on EEPROM. */
645 n = ( inb( sc->addr + FE_FMV2 ) & FE_FMV2_ADDR ) >> FE_FMV2_ADDR_SHIFT;
646 if ( ioaddr[ n ] != sc->addr ) return 0;
647
648 /* Determine the card type. */
649 switch ( inb( sc->addr + FE_FMV0 ) & FE_FMV0_MODEL ) {
650 case FE_FMV0_MODEL_FMV181:
651 sc->type = FE_TYPE_FMV181;
652 sc->typestr = "FMV-181";
653 sc->sc_description = "Ethernet adapter: FMV-181";
654 break;
655 case FE_FMV0_MODEL_FMV182:
656 sc->type = FE_TYPE_FMV182;
657 sc->typestr = "FMV-182";
658 sc->sc_description = "Ethernet adapter: FMV-182";
659 break;
660 default:
661 /* Unknown card type: maybe a new model, but... */
662 return 0;
663 }
664
665 /*
666 * An FMV-180 has successfully been proved.
667 * Determine which IRQ to be used.
668 *
669 * In this version, we always get an IRQ assignment from the
670 * FMV-180's configuration EEPROM, ignoring that specified in
671 * config file.
672 */
673 n = ( inb( sc->addr + FE_FMV2 ) & FE_FMV2_IRQ ) >> FE_FMV2_IRQ_SHIFT;
674 isa_dev->id_irq = irqmap[ n ];
675
676 /*
677 * Initialize constants in the per-line structure.
678 */
679
680 /* Get our station address from EEPROM. */
681 inblk( sc->addr + FE_FMV4, sc->sc_enaddr, ETHER_ADDR_LEN );
682
683 /* Make sure we got a valid station address. */
684 if ( ( sc->sc_enaddr[ 0 ] & 0x03 ) != 0x00
685 || ( sc->sc_enaddr[ 0 ] == 0x00
686 && sc->sc_enaddr[ 1 ] == 0x00
687 && sc->sc_enaddr[ 2 ] == 0x00 ) ) return 0;
688
689 /* Register values which depend on board design. */
690 sc->proto_dlcr4 = FE_D4_LBC_DISABLE | FE_D4_CNTRL;
691 sc->proto_dlcr5 = 0;
692 sc->proto_dlcr7 = FE_D7_BYTSWP_LH | FE_D7_IDENT_EC;
693
694 /*
695 * Program the 86960 as follows:
696 * SRAM: 32KB, 100ns, byte-wide access.
697 * Transmission buffer: 4KB x 2.
698 * System bus interface: 16 bits.
699 * We cannot change these values but TXBSIZE, because they
700 * are hard-wired on the board. Modifying TXBSIZE will affect
701 * the driver performance.
702 */
703 sc->proto_dlcr6 = FE_D6_BUFSIZ_32KB | FE_D6_TXBSIZ_2x4KB
704 | FE_D6_BBW_BYTE | FE_D6_SBW_WORD | FE_D6_SRAM_100ns;
705
706 /*
707 * Minimum initialization of the hardware.
708 * We write into registers; hope I/O ports have no
709 * overlap with other boards.
710 */
711
712 /* Initialize ASIC. */
713 outb( sc->addr + FE_FMV3, 0 );
714 outb( sc->addr + FE_FMV10, 0 );
715
716 /* Wait for a while. I'm not sure this is necessary. FIXME. */
717 DELAY(200);
718
719 /* Initialize 86960. */
720 outb( sc->addr + FE_DLCR6, sc->proto_dlcr6 | FE_D6_DLC_DISABLE );
721 DELAY(200);
722
723 /* Disable all interrupts. */
724 outb( sc->addr + FE_DLCR2, 0 );
725 outb( sc->addr + FE_DLCR3, 0 );
726
727 /* Turn the "master interrupt control" flag of ASIC on. */
728 outb( sc->addr + FE_FMV3, FE_FMV3_ENABLE_FLAG );
729
730 /*
731 * That's all. FMV-180 occupies 32 I/O addresses, by the way.
732 */
733 return 32;
734}
735
736/*
737 * Probe and initialization for Allied-Telesis AT1700/RE2000 series.
738 */
739static int
740fe_probe_ati ( struct isa_device * isa_dev, struct fe_softc * sc )
741{
742 int i, n;
743 u_char eeprom [ FE_EEPROM_SIZE ];
744
745 static u_short const ioaddr [ 8 ] =
746 { 0x260, 0x280, 0x2A0, 0x240, 0x340, 0x320, 0x380, 0x300 };
747 static u_short const irqmap_lo [ 4 ] =
748 { IRQ3, IRQ4, IRQ5, IRQ9 };
749 static u_short const irqmap_hi [ 4 ] =
750 { IRQ10, IRQ11, IRQ12, IRQ15 };
751 static struct fe_simple_probe_struct const probe_table [] = {
752 { FE_DLCR2, 0x70, 0x00 },
753 { FE_DLCR4, 0x08, 0x00 },
754 { FE_DLCR5, 0x80, 0x00 },
755#if 0
756 { FE_BMPR16, 0x1B, 0x00 },
757 { FE_BMPR17, 0x7F, 0x00 },
758#endif
759 { 0 }
760 };
761
762#if 0
763 /*
764 * Don't probe at all if the config says we are PCMCIA...
765 */
766 if ( isa_dev->id_flags & FE_FLAGS_PCMCIA ) return ( 0 );
767#endif
768
769#if FE_DEBUG >= 3
770 log( LOG_INFO, "fe%d: probe (0x%x) for ATI\n", sc->sc_unit, sc->addr );
771 fe_dump( LOG_INFO, sc, NULL );
772#endif
773
774 /*
775 * See if the sepcified address is possible for MB86965A JLI mode.
776 */
777 for ( i = 0; i < 8; i++ ) {
778 if ( ioaddr[ i ] == sc->addr ) break;
779 }
780 if ( i == 8 ) return 0;
781
782 /*
783 * We should test if MB86965A is on the base address now.
784 * Unfortunately, it is very hard to probe it reliably, since
785 * we have no way to reset the chip under software control.
786 * On cold boot, we could check the "signature" bit patterns
787 * described in the Fujitsu document. On warm boot, however,
788 * we can predict almost nothing about register values.
789 */
790 if ( !fe_simple_probe( sc->addr, probe_table ) ) return 0;
791
792 /* Check if our I/O address matches config info on 86965. */
793 n = ( inb( sc->addr + FE_BMPR19 ) & FE_B19_ADDR ) >> FE_B19_ADDR_SHIFT;
794 if ( ioaddr[ n ] != sc->addr ) return 0;
795
796 /*
797 * We are now almost sure we have an AT1700 at the given
798 * address. So, read EEPROM through 86965. We have to write
799 * into LSI registers to read from EEPROM. I want to avoid it
800 * at this stage, but I cannot test the presense of the chip
801 * any further without reading EEPROM. FIXME.
802 */
803 fe_read_eeprom( sc, eeprom );
804
805 /* Make sure that config info in EEPROM and 86965 agree. */
806 if ( eeprom[ FE_EEPROM_CONF ] != inb( sc->addr + FE_BMPR19 ) ) {
807 return 0;
808 }
809
810 /*
811 * Determine the card type.
812 * There may be a way to identify various models. FIXME.
813 */
814 sc->type = FE_TYPE_AT1700;
815 sc->typestr = "AT1700/RE2000";
816 sc->sc_description = "Ethernet adapter: AT1700 or RE2000";
817
818 /*
819 * I was told that RE2000 series has two variants on IRQ
820 * selection. They are 3/4/5/9 and 10/11/12/15. I don't know
821 * how we can distinguish which model is which. For now, we
822 * just trust irq setting in config. FIXME.
823 *
824 * I've heard that ATI puts an identification between these
825 * two models in the EEPROM. Sounds reasonable. I've also
826 * heard that Linux driver for AT1700 tests it. O.K. Let's
827 * try using it and see what happens. Anyway, we will use an
828 * IRQ value passed by config (i.e., user), if one is
829 * available. FIXME.
830 */
831 n = ( inb( sc->addr + FE_BMPR19 ) & FE_B19_IRQ ) >> FE_B19_IRQ_SHIFT;
832 if ( isa_dev->id_irq == 0 ) {
833 /* Try to determine IRQ settings. */
834 if ( eeprom[ FE_EEP_ATI_TYPE ] & FE_EEP_ATI_TYPE_HIGHIRQ ) {
835 isa_dev->id_irq = irqmap_hi[ n ];
836 } else {
837 isa_dev->id_irq = irqmap_lo[ n ];
838 }
839 }
840
841 /*
842 * Initialize constants in the per-line structure.
843 */
844
845 /* Get our station address from EEPROM. */
846 bcopy( eeprom + FE_EEP_ATI_ADDR, sc->sc_enaddr, ETHER_ADDR_LEN );
847
848#if 1
849 /*
850 * This test doesn't work well for AT1700 look-alike by
851 * other vendors.
852 */
853 /* Make sure the vendor part is for Allied-Telesis. */
854 if ( sc->sc_enaddr[ 0 ] != 0x00
855 || sc->sc_enaddr[ 1 ] != 0x00
856 || sc->sc_enaddr[ 2 ] != 0xF4 ) return 0;
857
858#else
859 /* Make sure we got a valid station address. */
860 if ( ( sc->sc_enaddr[ 0 ] & 0x03 ) != 0x00
861 || ( sc->sc_enaddr[ 0 ] == 0x00
862 && sc->sc_enaddr[ 1 ] == 0x00
863 && sc->sc_enaddr[ 2 ] == 0x00 ) ) return 0;
864#endif
865
866 /* Should find all register prototypes here. FIXME. */
867 sc->proto_dlcr4 = FE_D4_LBC_DISABLE | FE_D4_CNTRL; /* FIXME */
868 sc->proto_dlcr5 = 0;
869 sc->proto_dlcr7 = FE_D7_BYTSWP_LH | FE_D7_IDENT_EC;
870
871 /*
872 * Program the 86960 as follows:
873 * SRAM: 32KB, 100ns, byte-wide access.
874 * Transmission buffer: 4KB x 2.
875 * System bus interface: 16 bits.
876 * We cannot change these values but TXBSIZE, because they
877 * are hard-wired on the board. Modifying TXBSIZE will affect
878 * the driver performance.
879 */
880 sc->proto_dlcr6 = FE_D6_BUFSIZ_32KB | FE_D6_TXBSIZ_2x4KB
881 | FE_D6_BBW_BYTE | FE_D6_SBW_WORD | FE_D6_SRAM_100ns;
882
883#if FE_DEBUG >= 3
884 fe_dump( LOG_INFO, sc, "ATI found" );
885#endif
886
887 /* Initialize 86965. */
888 outb( sc->addr + FE_DLCR6, sc->proto_dlcr6 | FE_D6_DLC_DISABLE );
889 DELAY(200);
890
891 /* Disable all interrupts. */
892 outb( sc->addr + FE_DLCR2, 0 );
893 outb( sc->addr + FE_DLCR3, 0 );
894
895#if FE_DEBUG >= 3
896 fe_dump( LOG_INFO, sc, "end of fe_probe_ati()" );
897#endif
898
899 /*
900 * That's all. AT1700 occupies 32 I/O addresses, by the way.
901 */
902 return 32;
903}
904
905/*
906 * Probe and initialization for Fujitsu MBH10302 PCMCIA Ethernet interface.
907 */
908static int
909fe_probe_mbh ( struct isa_device * isa_dev, struct fe_softc * sc )
910{
911 static struct fe_simple_probe_struct probe_table [] = {
912 { FE_DLCR2, 0x70, 0x00 },
913 { FE_DLCR4, 0x08, 0x00 },
914 /* { FE_DLCR5, 0x80, 0x00 }, Does not work well. */
915#if 0
916 /*
917 * Test *vendor* part of the address for Fujitsu.
918 * The test will gain reliability of probe process, but
919 * it rejects clones by other vendors, or OEM product
920 * supplied by resalers other than Fujitsu.
921 */
922 { FE_MBH10, 0xFF, 0x00 },
923 { FE_MBH11, 0xFF, 0x00 },
924 { FE_MBH12, 0xFF, 0x0E },
925#else
926 /*
927 * We can always verify the *first* 2 bits (in Ehternet
928 * bit order) are "global" and "unicast" even for
929 * unknown vendors.
930 */
931 { FE_MBH10, 0x03, 0x00 },
932#endif
933 /* Just a gap? Seems reliable, anyway. */
934 { 0x12, 0xFF, 0x00 },
935 { 0x13, 0xFF, 0x00 },
936 { 0x14, 0xFF, 0x00 },
937 { 0x15, 0xFF, 0x00 },
938 { 0x16, 0xFF, 0x00 },
939 { 0x17, 0xFF, 0x00 },
940 { 0x18, 0xFF, 0xFF },
941 { 0x19, 0xFF, 0xFF },
942
943 { 0 }
944 };
945
946#if 0
947 /*
948 * We need a PCMCIA flag.
949 */
950 if ( ( isa_dev->id_flags & FE_FLAGS_PCMCIA ) == 0 ) return ( 0 );
951#endif
952
953 /*
954 * We need explicit IRQ and supported address.
955 */
956 if ( isa_dev->id_irq == 0 || ( sc->addr & ~0x3E0 ) != 0 ) return ( 0 );
957
958#if FE_DEBUG >= 3
959 fe_dump( LOG_INFO, sc, "top of probe" );
960#endif
961
962 /*
963 * See if MBH10302 is on its address.
964 * I'm not sure the following probe code works. FIXME.
965 */
966 if ( !fe_simple_probe( sc->addr, probe_table ) ) return 0;
967
968 /* Determine the card type. */
969 sc->type = FE_TYPE_MBH10302;
970 sc->typestr = "MBH10302 (PCMCIA)";
971 sc->sc_description = "Ethernet adapter: MBH10302 (PCMCIA)";
972
973 /*
974 * Initialize constants in the per-line structure.
975 */
976
977 /* Get our station address from EEPROM. */
978 inblk( sc->addr + FE_MBH10, sc->sc_enaddr, ETHER_ADDR_LEN );
979
980 /* Make sure we got a valid station address. */
981 if ( ( sc->sc_enaddr[ 0 ] & 0x03 ) != 0x00
982 || ( sc->sc_enaddr[ 0 ] == 0x00
983 && sc->sc_enaddr[ 1 ] == 0x00
984 && sc->sc_enaddr[ 2 ] == 0x00 ) ) return 0;
985
986 /* Should find all register prototypes here. FIXME. */
987 sc->proto_dlcr4 = FE_D4_LBC_DISABLE | FE_D4_CNTRL;
988 sc->proto_dlcr5 = 0;
989 sc->proto_dlcr7 = FE_D7_BYTSWP_LH | FE_D7_IDENT_NICE;
990
991 /*
992 * Program the 86960 as follows:
993 * SRAM: 32KB, 100ns, byte-wide access.
994 * Transmission buffer: 4KB x 2.
995 * System bus interface: 16 bits.
996 * We cannot change these values but TXBSIZE, because they
997 * are hard-wired on the board. Modifying TXBSIZE will affect
998 * the driver performance.
999 */
1000 sc->proto_dlcr6 = FE_D6_BUFSIZ_32KB | FE_D6_TXBSIZ_2x4KB
1001 | FE_D6_BBW_BYTE | FE_D6_SBW_WORD | FE_D6_SRAM_100ns;
1002
1003 /* Setup hooks. We need a special initialization procedure. */
1004 sc->init = fe_init_mbh;
1005
1006 /*
1007 * Minimum initialization.
1008 */
1009
1010 /* Wait for a while. I'm not sure this is necessary. FIXME. */
1011 DELAY(200);
1012
1013 /* Minimul initialization of 86960. */
1014 outb( sc->addr + FE_DLCR6, sc->proto_dlcr6 | FE_D6_DLC_DISABLE );
1015 DELAY( 200 );
1016
1017 /* Disable all interrupts. */
1018 outb( sc->addr + FE_DLCR2, 0 );
1019 outb( sc->addr + FE_DLCR3, 0 );
1020
1021#if 1 /* FIXME. */
1022 /* Initialize system bus interface and encoder/decoder operation. */
1023 outb( sc->addr + FE_MBH0, FE_MBH0_MAGIC | FE_MBH0_INTR_DISABLE );
1024#endif
1025
1026 /*
1027 * That's all. MBH10302 occupies 32 I/O addresses, by the way.
1028 */
1029 return 32;
1030}
1031
1032/* MBH specific initialization routine. */
1033static void
1034fe_init_mbh ( struct fe_softc * sc )
1035{
1036 /* Probably required after hot-insertion... */
1037
1038 /* Wait for a while. I'm not sure this is necessary. FIXME. */
1039 DELAY(200);
1040
1041 /* Minimul initialization of 86960. */
1042 outb( sc->addr + FE_DLCR6, sc->proto_dlcr6 | FE_D6_DLC_DISABLE );
1043 DELAY( 200 );
1044
1045 /* Disable all interrupts. */
1046 outb( sc->addr + FE_DLCR2, 0 );
1047 outb( sc->addr + FE_DLCR3, 0 );
1048
1049 /* Enable master interrupt flag. */
1050 outb( sc->addr + FE_MBH0, FE_MBH0_MAGIC | FE_MBH0_INTR_ENABLE );
1051}
1052
1053/*
1054 * Install interface into kernel networking data structures
1055 */
1056static int
1057fe_attach ( struct isa_device *isa_dev )
1058{
1059 struct fe_softc *sc = &fe_softc[isa_dev->id_unit];
1060
1061 /*
1062 * Initialize ifnet structure
1063 */
1064 sc->sc_if.if_unit = sc->sc_unit;
1065 sc->sc_if.if_name = "fe";
1066 sc->sc_if.if_output = ether_output;
1067 sc->sc_if.if_start = fe_start;
1068 sc->sc_if.if_ioctl = fe_ioctl;
1069 sc->sc_if.if_watchdog = fe_watchdog;
1070
1071 /*
1072 * Set default interface flags.
1073 */
1074 sc->sc_if.if_flags = IFF_BROADCAST | IFF_MULTICAST;
1075
1076 /*
1077 * Set maximum size of output queue, if it has not been set.
1078 * It is done here as this driver may be started after the
1079 * system intialization (i.e., the interface is PCMCIA.)
1080 *
1081 * I'm not sure this is really necessary, but, even if it is,
1082 * it should be done somewhere else, e.g., in if_attach(),
1083 * since it must be a common workaround for all network drivers.
1084 * FIXME.
1085 */
1086 if ( sc->sc_if.if_snd.ifq_maxlen == 0 ) {
1087 sc->sc_if.if_snd.ifq_maxlen = ifqmaxlen;
1088 }
1089
1090#if FE_DEBUG >= 3
1091 fe_dump( LOG_INFO, sc, "attach()" );
1092#endif
1093
1094#if FE_SINGLE_TRANSMISSION
1095 /* Override txb config to allocate minimum. */
1096 sc->proto_dlcr6 &= ~FE_D6_TXBSIZ
1097 sc->proto_dlcr6 |= FE_D6_TXBSIZ_2x2KB;
1098#endif
1099
1100 /* Modify hardware config if it is requested. */
1101 if ( isa_dev->id_flags & FE_FLAGS_OVERRIDE_DLCR6 ) {
1102 sc->proto_dlcr6 = isa_dev->id_flags & FE_FLAGS_DLCR6_VALUE;
1103 }
1104
1105 /* Find TX buffer size, based on the hardware dependent proto. */
1106 switch ( sc->proto_dlcr6 & FE_D6_TXBSIZ ) {
1107 case FE_D6_TXBSIZ_2x2KB: sc->txb_size = 2048; break;
1108 case FE_D6_TXBSIZ_2x4KB: sc->txb_size = 4096; break;
1109 case FE_D6_TXBSIZ_2x8KB: sc->txb_size = 8192; break;
1110 default:
1111 /* Oops, we can't work with single buffer configuration. */
1112#if FE_DEBUG >= 2
1113 log( LOG_WARNING, "fe%d: strange TXBSIZ config; fixing\n",
1114 sc->sc_unit );
1115#endif
1116 sc->proto_dlcr6 &= ~FE_D6_TXBSIZ;
1117 sc->proto_dlcr6 |= FE_D6_TXBSIZ_2x2KB;
1118 sc->txb_size = 2048;
1119 break;
1120 }
1121
1122 /* Attach and stop the interface. */
1123 if_attach( &sc->sc_if );
1124 fe_stop( sc->sc_unit ); /* This changes the state to IDLE. */
1125 fe_setlinkaddr( sc );
1126
1127 /* Print additional info when attached. */
1128 printf( "fe%d: address %6D, type %s\n", sc->sc_unit,
1129 sc->sc_enaddr, ":" , sc->typestr );
1130#if FE_DEBUG >= 3
1131 {
1132 int buf, txb, bbw, sbw, ram;
1133
1134 buf = txb = bbw = sbw = ram = -1;
1135 switch ( sc->proto_dlcr6 & FE_D6_BUFSIZ ) {
1136 case FE_D6_BUFSIZ_8KB: buf = 8; break;
1137 case FE_D6_BUFSIZ_16KB: buf = 16; break;
1138 case FE_D6_BUFSIZ_32KB: buf = 32; break;
1139 case FE_D6_BUFSIZ_64KB: buf = 64; break;
1140 }
1141 switch ( sc->proto_dlcr6 & FE_D6_TXBSIZ ) {
1142 case FE_D6_TXBSIZ_2x2KB: txb = 2; break;
1143 case FE_D6_TXBSIZ_2x4KB: txb = 4; break;
1144 case FE_D6_TXBSIZ_2x8KB: txb = 8; break;
1145 }
1146 switch ( sc->proto_dlcr6 & FE_D6_BBW ) {
1147 case FE_D6_BBW_BYTE: bbw = 8; break;
1148 case FE_D6_BBW_WORD: bbw = 16; break;
1149 }
1150 switch ( sc->proto_dlcr6 & FE_D6_SBW ) {
1151 case FE_D6_SBW_BYTE: sbw = 8; break;
1152 case FE_D6_SBW_WORD: sbw = 16; break;
1153 }
1154 switch ( sc->proto_dlcr6 & FE_D6_SRAM ) {
1155 case FE_D6_SRAM_100ns: ram = 100; break;
1156 case FE_D6_SRAM_150ns: ram = 150; break;
1157 }
1158 printf( "fe%d: SRAM %dKB %dbit %dns, TXB %dKBx2, %dbit I/O\n",
1159 sc->sc_unit, buf, bbw, ram, txb, sbw );
1160 }
1161#endif
1162
1163#if NBPFILTER > 0
1164 /* If BPF is in the kernel, call the attach for it. */
1165 bpfattach(&sc->bpf, &sc->sc_if, DLT_EN10MB,
1166 sizeof(struct ether_header));
1167#endif
1168 return 1;
1169}
1170
1171/*
1172 * Reset interface.
1173 */
1174static void
1175fe_reset ( int unit )
1176{
1177 /*
1178 * Stop interface and re-initialize.
1179 */
1180 fe_stop(unit);
1181 fe_init(unit);
1182}
1183
1184/*
1185 * Stop everything on the interface.
1186 *
1187 * All buffered packets, both transmitting and receiving,
1188 * if any, will be lost by stopping the interface.
1189 */
1190static void
1191fe_stop ( int unit )
1192{
1193 struct fe_softc *sc = &fe_softc[unit];
1194 int s;
1195
1196 s = splimp();
1197
1198#if FE_DEBUG >= 3
1199 fe_dump( LOG_INFO, sc, "stop()" );
1200#endif
1201
1202 /* Disable interrupts. */
1203 outb( sc->addr + FE_DLCR2, 0x00 );
1204 outb( sc->addr + FE_DLCR3, 0x00 );
1205
1206 /* Stop interface hardware. */
1207 DELAY( 200 );
1208 outb( sc->addr + FE_DLCR6, sc->proto_dlcr6 | FE_D6_DLC_DISABLE );
1209 DELAY( 200 );
1210
1211 /* Clear all interrupt status. */
1212 outb( sc->addr + FE_DLCR0, 0xFF );
1213 outb( sc->addr + FE_DLCR1, 0xFF );
1214
1215 /* Put the chip in stand-by mode. */
1216 DELAY( 200 );
1217 outb( sc->addr + FE_DLCR7, sc->proto_dlcr7 | FE_D7_POWER_DOWN );
1218 DELAY( 200 );
1219
1220 /* Reset transmitter variables and interface flags. */
1221 sc->sc_if.if_flags &= ~( IFF_OACTIVE | IFF_RUNNING );
1222 sc->sc_if.if_timer = 0;
1223 sc->txb_free = sc->txb_size;
1224 sc->txb_count = 0;
1225 sc->txb_sched = 0;
1226
1227 /* MAR loading can be delayed. */
1228 sc->filter_change = 0;
1229
1230 /* Update config status also. */
1231 sc->sc_dcstate = DC_IDLE;
1232
1233 /* Call a hook. */
1234 if ( sc->stop ) sc->stop( sc );
1235
1236#if FE_DEBUG >= 3
1237 fe_dump( LOG_INFO, sc, "end of stop()" );
1238#endif
1239
1240 (void) splx(s);
1241}
1242
1243/*
1244 * Device timeout/watchdog routine. Entered if the device neglects to
1245 * generate an interrupt after a transmit has been started on it.
1246 */
1247static void
1248fe_watchdog ( struct ifnet *ifp )
1249{
1250 struct fe_softc *sc = (struct fe_softc *)ifp;
1251
1252#if FE_DEBUG >= 1
1253 log( LOG_ERR, "fe%d: transmission timeout (%d+%d)%s\n",
1254 ifp->if_unit, sc->txb_sched, sc->txb_count,
1255 ( ifp->if_flags & IFF_UP ) ? "" : " when down" );
1256#endif
1257#if FE_DEBUG >= 3
1258 fe_dump( LOG_INFO, sc, NULL );
1259#endif
1260
1261 /* Record how many packets are lost by this accident. */
1262 ifp->if_oerrors += sc->txb_sched + sc->txb_count;
1263
1264 /* Put the interface into known initial state. */
1265 if ( ifp->if_flags & IFF_UP ) {
1266 fe_reset( ifp->if_unit );
1267 } else {
1268 fe_stop( ifp->if_unit );
1269 }
1270}
1271
1272/*
1273 * Initialize device.
1274 */
1275static void
1276fe_init ( int unit )
1277{
1278 struct fe_softc *sc = &fe_softc[unit];
1279 int i, s;
1280
1281#if FE_DEBUG >= 3
1282 fe_dump( LOG_INFO, sc, "init()" );
1283#endif
1284
1285 /* We need an address. */
1286 if (sc->sc_if.if_addrlist == 0) {
1287#if FE_DEBUG >= 1
1288 log( LOG_ERR, "fe%d: init() without any address\n",
1289 sc->sc_unit );
1290#endif
1291 return;
1292 }
1293
1294#if FE_DEBUG >= 1
1295 /*
1296 * Make sure we have a valid station address.
1297 * The following test is applicable for any Ethernet interfaces.
1298 * It can be done in somewhere common to all of them. FIXME.
1299 */
1300 if ( ( sc->sc_enaddr[ 0 ] & 0x01 ) != 0
1301 || ( sc->sc_enaddr[ 0 ] == 0x00
1302 && sc->sc_enaddr[ 1 ] == 0x00
1303 && sc->sc_enaddr[ 2 ] == 0x00 ) ) {
1304 log( LOG_ERR, "fe%d: invalid station address (%6D)\n",
1305 sc->sc_unit, sc->sc_enaddr, ":" );
1306 return;
1307 }
1308#endif
1309
1310 /* Start initializing 86960. */
1311 s = splimp();
1312
1313 /* Call a hook. */
1314 if ( sc->init ) sc->init( sc );
1315
1316#if FE_DEBUG >= 3
1317 fe_dump( LOG_INFO, sc, "after init hook" );
1318#endif
1319
1320 /*
1321 * Make sure to disable the chip, also.
1322 * This may also help re-programming the chip after
1323 * hot insertion of PCMCIAs.
1324 */
1325 outb( sc->addr + FE_DLCR6, sc->proto_dlcr6 | FE_D6_DLC_DISABLE );
1326
1327 /* Power up the chip and select register bank for DLCRs. */
1328 DELAY(200);
1329 outb( sc->addr + FE_DLCR7,
1330 sc->proto_dlcr7 | FE_D7_RBS_DLCR | FE_D7_POWER_UP );
1331 DELAY(200);
1332
1333 /* Feed the station address. */
1334 outblk( sc->addr + FE_DLCR8, sc->sc_enaddr, ETHER_ADDR_LEN );
1335
1336 /* Clear multicast address filter to receive nothing. */
1337 outb( sc->addr + FE_DLCR7,
1338 sc->proto_dlcr7 | FE_D7_RBS_MAR | FE_D7_POWER_UP );
1339 outblk( sc->addr + FE_MAR8, fe_filter_nothing.data, FE_FILTER_LEN );
1340
1341 /* Select the BMPR bank for runtime register access. */
1342 outb( sc->addr + FE_DLCR7,
1343 sc->proto_dlcr7 | FE_D7_RBS_BMPR | FE_D7_POWER_UP );
1344
1345 /* Initialize registers. */
1346 outb( sc->addr + FE_DLCR0, 0xFF ); /* Clear all bits. */
1347 outb( sc->addr + FE_DLCR1, 0xFF ); /* ditto. */
1348 outb( sc->addr + FE_DLCR2, 0x00 );
1349 outb( sc->addr + FE_DLCR3, 0x00 );
1350 outb( sc->addr + FE_DLCR4, sc->proto_dlcr4 );
1351 outb( sc->addr + FE_DLCR5, sc->proto_dlcr5 );
1352 outb( sc->addr + FE_BMPR10, 0x00 );
1353 outb( sc->addr + FE_BMPR11, FE_B11_CTRL_SKIP );
1354 outb( sc->addr + FE_BMPR12, 0x00 );
1355 outb( sc->addr + FE_BMPR13, FE_B13_TPTYPE_UTP | FE_B13_PORT_AUTO );
1356 outb( sc->addr + FE_BMPR14, 0x00 );
1357 outb( sc->addr + FE_BMPR15, 0x00 );
1358
1359#if FE_DEBUG >= 3
1360 fe_dump( LOG_INFO, sc, "just before enabling DLC" );
1361#endif
1362
1363 /* Enable interrupts. */
1364 outb( sc->addr + FE_DLCR2, FE_TMASK );
1365 outb( sc->addr + FE_DLCR3, FE_RMASK );
1366
1367 /* Enable transmitter and receiver. */
1368 DELAY(200);
1369 outb( sc->addr + FE_DLCR6, sc->proto_dlcr6 | FE_D6_DLC_ENABLE );
1370 DELAY(200);
1371
1372#if FE_DEBUG >= 3
1373 fe_dump( LOG_INFO, sc, "just after enabling DLC" );
1374#endif
1375 /*
1376 * Make sure to empty the receive buffer.
1377 *
1378 * This may be redundant, but *if* the receive buffer were full
1379 * at this point, the driver would hang. I have experienced
1380 * some strange hangups just after UP. I hope the following
1381 * code solve the problem.
1382 *
1383 * I have changed the order of hardware initialization.
1384 * I think the receive buffer cannot have any packets at this
1385 * point in this version. The following code *must* be
1386 * redundant now. FIXME.
1387 */
1388 for ( i = 0; i < FE_MAX_RECV_COUNT; i++ ) {
1389 if ( inb( sc->addr + FE_DLCR5 ) & FE_D5_BUFEMP ) break;
1390 outb( sc->addr + FE_BMPR14, FE_B14_SKIP );
1391 }
1392#if FE_DEBUG >= 1
1393 if ( i >= FE_MAX_RECV_COUNT ) {
1394 log( LOG_ERR, "fe%d: cannot empty receive buffer\n",
1395 sc->sc_unit );
1396 }
1397#endif
1398#if FE_DEBUG >= 3
1399 if ( i < FE_MAX_RECV_COUNT ) {
1400 log( LOG_INFO, "fe%d: receive buffer emptied (%d)\n",
1401 sc->sc_unit, i );
1402 }
1403#endif
1404
1405#if FE_DEBUG >= 3
1406 fe_dump( LOG_INFO, sc, "after ERB loop" );
1407#endif
1408
1409 /* Do we need this here? */
1410 outb( sc->addr + FE_DLCR0, 0xFF ); /* Clear all bits. */
1411 outb( sc->addr + FE_DLCR1, 0xFF ); /* ditto. */
1412
1413#if FE_DEBUG >= 3
1414 fe_dump( LOG_INFO, sc, "after FIXME" );
1415#endif
1416 /* Set 'running' flag, because we are now running. */
1417 sc->sc_if.if_flags |= IFF_RUNNING;
1418
1419 /* Update device config status. */
1420 sc->sc_dcstate = DC_BUSY;
1421
1422 /*
1423 * At this point, the interface is runnung properly,
1424 * except that it receives *no* packets. we then call
1425 * fe_setmode() to tell the chip what packets to be
1426 * received, based on the if_flags and multicast group
1427 * list. It completes the initialization process.
1428 */
1429 fe_setmode( sc );
1430
1431#if FE_DEBUG >= 3
1432 fe_dump( LOG_INFO, sc, "after setmode" );
1433#endif
1434
1435 /* ...and attempt to start output queued packets. */
1436 fe_start( &sc->sc_if );
1437
1438#if FE_DEBUG >= 3
1439 fe_dump( LOG_INFO, sc, "init() done" );
1440#endif
1441
1442 (void) splx(s);
1443}
1444
1445/*
1446 * This routine actually starts the transmission on the interface
1447 */
1448static INLINE void
1449fe_xmit ( struct fe_softc * sc )
1450{
1451 /*
1452 * Set a timer just in case we never hear from the board again.
1453 * We use longer timeout for multiple packet transmission.
1454 * I'm not sure this timer value is appropriate. FIXME.
1455 */
1456 sc->sc_if.if_timer = 1 + sc->txb_count;
1457
1458 /* Update txb variables. */
1459 sc->txb_sched = sc->txb_count;
1460 sc->txb_count = 0;
1461 sc->txb_free = sc->txb_size;
1462
1463#if FE_DELAYED_PADDING
1464 /* Omit the postponed padding process. */
1465 sc->txb_padding = 0;
1466#endif
1467
1468 /* Start transmitter, passing packets in TX buffer. */
1469 outb( sc->addr + FE_BMPR10, sc->txb_sched | FE_B10_START );
1470}
1471
1472/*
1473 * Start output on interface.
1474 * We make two assumptions here:
1475 * 1) that the current priority is set to splimp _before_ this code
1476 * is called *and* is returned to the appropriate priority after
1477 * return
1478 * 2) that the IFF_OACTIVE flag is checked before this code is called
1479 * (i.e. that the output part of the interface is idle)
1480 */
1481void
1482fe_start ( struct ifnet *ifp )
1483{
1484 struct fe_softc *sc = IFNET2SOFTC( ifp );
1485 struct mbuf *m;
1486
1487#if FE_DEBUG >= 1
1488 /* Just a sanity check. */
1489 if ( ( sc->txb_count == 0 ) != ( sc->txb_free == sc->txb_size ) ) {
1490 /*
1491 * Txb_count and txb_free co-works to manage the
1492 * transmission buffer. Txb_count keeps track of the
1493 * used potion of the buffer, while txb_free does unused
1494 * potion. So, as long as the driver runs properly,
1495 * txb_count is zero if and only if txb_free is same
1496 * as txb_size (which represents whole buffer.)
1497 */
1498 log( LOG_ERR, "fe%d: inconsistent txb variables (%d, %d)\n",
1499 sc->sc_unit, sc->txb_count, sc->txb_free );
1500 /*
1501 * So, what should I do, then?
1502 *
1503 * We now know txb_count and txb_free contradicts. We
1504 * cannot, however, tell which is wrong. More
1505 * over, we cannot peek 86960 transmission buffer or
1506 * reset the transmission buffer. (In fact, we can
1507 * reset the entire interface. I don't want to do it.)
1508 *
1509 * If txb_count is incorrect, leaving it as is will cause
1510 * sending of gabages after next interrupt. We have to
1511 * avoid it. Hence, we reset the txb_count here. If
1512 * txb_free was incorrect, resetting txb_count just loose
1513 * some packets. We can live with it.
1514 */
1515 sc->txb_count = 0;
1516 }
1517#endif
1518
1519#if FE_DEBUG >= 1
1520 /*
1521 * First, see if there are buffered packets and an idle
1522 * transmitter - should never happen at this point.
1523 */
1524 if ( ( sc->txb_count > 0 ) && ( sc->txb_sched == 0 ) ) {
1525 log( LOG_ERR,
1526 "fe%d: transmitter idle with %d buffered packets\n",
1527 sc->sc_unit, sc->txb_count );
1528 fe_xmit( sc );
1529 }
1530#endif
1531
1532 /*
1533 * Stop accepting more transmission packets temporarily, when
1534 * a filter change request is delayed. Updating the MARs on
1535 * 86960 flushes the transmisstion buffer, so it is delayed
1536 * until all buffered transmission packets have been sent
1537 * out.
1538 */
1539 if ( sc->filter_change ) {
1540 /*
1541 * Filter change requst is delayed only when the DLC is
1542 * working. DLC soon raise an interrupt after finishing
1543 * the work.
1544 */
1545 goto indicate_active;
1546 }
1547
1548 for (;;) {
1549
1550 /*
1551 * See if there is room to put another packet in the buffer.
1552 * We *could* do better job by peeking the send queue to
1553 * know the length of the next packet. Current version just
1554 * tests against the worst case (i.e., longest packet). FIXME.
1555 *
1556 * When adding the packet-peek feature, don't forget adding a
1557 * test on txb_count against QUEUEING_MAX.
1558 * There is a little chance the packet count exceeds
1559 * the limit. Assume transmission buffer is 8KB (2x8KB
1560 * configuration) and an application sends a bunch of small
1561 * (i.e., minimum packet sized) packets rapidly. An 8KB
1562 * buffer can hold 130 blocks of 62 bytes long...
1563 */
1564 if ( sc->txb_free < ETHER_MAX_LEN + FE_DATA_LEN_LEN ) {
1565 /* No room. */
1566 goto indicate_active;
1567 }
1568
1569#if FE_SINGLE_TRANSMISSION
1570 if ( sc->txb_count > 0 ) {
1571 /* Just one packet per a transmission buffer. */
1572 goto indicate_active;
1573 }
1574#endif
1575
1576 /*
1577 * Get the next mbuf chain for a packet to send.
1578 */
1579 IF_DEQUEUE( &sc->sc_if.if_snd, m );
1580 if ( m == NULL ) {
1581 /* No more packets to send. */
1582 goto indicate_inactive;
1583 }
1584
1585 /*
1586 * Copy the mbuf chain into the transmission buffer.
1587 * txb_* variables are updated as necessary.
1588 */
1589 fe_write_mbufs( sc, m );
1590
1591 /* Start transmitter if it's idle. */
1592 if ( sc->txb_sched == 0 ) fe_xmit( sc );
1593
1594#if 0 /* Turned of, since our interface is now duplex. */
1595 /*
1596 * Tap off here if there is a bpf listener.
1597 */
1598#if NBPFILTER > 0
1599 if ( sc->bpf ) bpf_mtap( sc->bpf, m );
1600#endif
1601#endif
1602
1603 m_freem( m );
1604 }
1605
1606 indicate_inactive:
1607 /*
1608 * We are using the !OACTIVE flag to indicate to
1609 * the outside world that we can accept an
1610 * additional packet rather than that the
1611 * transmitter is _actually_ active. Indeed, the
1612 * transmitter may be active, but if we haven't
1613 * filled all the buffers with data then we still
1614 * want to accept more.
1615 */
1616 sc->sc_if.if_flags &= ~IFF_OACTIVE;
1617 return;
1618
1619 indicate_active:
1620 /*
1621 * The transmitter is active, and there are no room for
1622 * more outgoing packets in the transmission buffer.
1623 */
1624 sc->sc_if.if_flags |= IFF_OACTIVE;
1625 return;
1626}
1627
1628/*
1629 * Drop (skip) a packet from receive buffer in 86960 memory.
1630 */
1631static INLINE void
1632fe_droppacket ( struct fe_softc * sc )
1633{
1634 outb( sc->addr + FE_BMPR14, FE_B14_SKIP );
1635}
1636
1637/*
1638 * Transmission interrupt handler
1639 * The control flow of this function looks silly. FIXME.
1640 */
1641static void
1642fe_tint ( struct fe_softc * sc, u_char tstat )
1643{
1644 int left;
1645 int col;
1646
1647 /*
1648 * Handle "excessive collision" interrupt.
1649 */
1650 if ( tstat & FE_D0_COLL16 ) {
1651
1652 /*
1653 * Find how many packets (including this collided one)
1654 * are left unsent in transmission buffer.
1655 */
1656 left = inb( sc->addr + FE_BMPR10 );
1657
1658#if FE_DEBUG >= 2
1659 log( LOG_WARNING, "fe%d: excessive collision (%d/%d)\n",
1660 sc->sc_unit, left, sc->txb_sched );
1661#endif
1662#if FE_DEBUG >= 3
1663 fe_dump( LOG_INFO, sc, NULL );
1664#endif
1665
1666 /*
1667 * Update statistics.
1668 */
1669 sc->sc_if.if_collisions += 16;
1670 sc->sc_if.if_oerrors++;
1671 sc->sc_if.if_opackets += sc->txb_sched - left;
1672
1673 /*
1674 * Collision statistics has been updated.
1675 * Clear the collision flag on 86960 now to avoid confusion.
1676 */
1677 outb( sc->addr + FE_DLCR0, FE_D0_COLLID );
1678
1679 /*
1680 * Restart transmitter, skipping the
1681 * collided packet.
1682 *
1683 * We *must* skip the packet to keep network running
1684 * properly. Excessive collision error is an
1685 * indication of the network overload. If we
1686 * tried sending the same packet after excessive
1687 * collision, the network would be filled with
1688 * out-of-time packets. Packets belonging
1689 * to reliable transport (such as TCP) are resent
1690 * by some upper layer.
1691 */
1692 outb( sc->addr + FE_BMPR11,
1693 FE_B11_CTRL_SKIP | FE_B11_MODE1 );
1694 sc->txb_sched = left - 1;
1695 }
1696
1697 /*
1698 * Handle "transmission complete" interrupt.
1699 */
1700 if ( tstat & FE_D0_TXDONE ) {
1701
1702 /*
1703 * Add in total number of collisions on last
1704 * transmission. We also clear "collision occurred" flag
1705 * here.
1706 *
1707 * 86960 has a design flaw on collision count on multiple
1708 * packet transmission. When we send two or more packets
1709 * with one start command (that's what we do when the
1710 * transmission queue is clauded), 86960 informs us number
1711 * of collisions occured on the last packet on the
1712 * transmission only. Number of collisions on previous
1713 * packets are lost. I have told that the fact is clearly
1714 * stated in the Fujitsu document.
1715 *
1716 * I considered not to mind it seriously. Collision
1717 * count is not so important, anyway. Any comments? FIXME.
1718 */
1719
1720 if ( inb( sc->addr + FE_DLCR0 ) & FE_D0_COLLID ) {
1721
1722 /* Clear collision flag. */
1723 outb( sc->addr + FE_DLCR0, FE_D0_COLLID );
1724
1725 /* Extract collision count from 86960. */
1726 col = inb( sc->addr + FE_DLCR4 );
1727 col = ( col & FE_D4_COL ) >> FE_D4_COL_SHIFT;
1728 if ( col == 0 ) {
1729 /*
1730 * Status register indicates collisions,
1731 * while the collision count is zero.
1732 * This can happen after multiple packet
1733 * transmission, indicating that one or more
1734 * previous packet(s) had been collided.
1735 *
1736 * Since the accurate number of collisions
1737 * has been lost, we just guess it as 1;
1738 * Am I too optimistic? FIXME.
1739 */
1740 col = 1;
1741 }
1742 sc->sc_if.if_collisions += col;
1743#if FE_DEBUG >= 3
1744 log( LOG_WARNING, "fe%d: %d collision(s) (%d)\n",
1745 sc->sc_unit, col, sc->txb_sched );
1746#endif
1747 }
1748
1749 /*
1750 * Update total number of successfully
1751 * transmitted packets.
1752 */
1753 sc->sc_if.if_opackets += sc->txb_sched;
1754 sc->txb_sched = 0;
1755
1756 /*
1757 * The transmitter is no more active.
1758 * Reset output active flag and watchdog timer.
1759 */
1760 sc->sc_if.if_flags &= ~IFF_OACTIVE;
1761 sc->sc_if.if_timer = 0;
1762
1763 /*
1764 * If more data is ready to transmit in the buffer, start
1765 * transmitting them. Otherwise keep transmitter idle,
1766 * even if more data is queued. This gives receive
1767 * process a slight priority.
1768 */
1769 if ( sc->txb_count > 0 ) fe_xmit( sc );
1770 }
1771}
1772
1773/*
1774 * Ethernet interface receiver interrupt.
1775 */
1776static void
1777fe_rint ( struct fe_softc * sc, u_char rstat )
1778{
1779 u_short len;
1780 u_char status;
1781 int i;
1782
1783 /*
1784 * Update statistics if this interrupt is caused by an error.
1785 */
1786 if ( rstat & ( FE_D1_OVRFLO | FE_D1_CRCERR
1787 | FE_D1_ALGERR | FE_D1_SRTPKT ) ) {
1788#if FE_DEBUG >= 3
1789 log( LOG_WARNING,
1790 "fe%d: receive error: %s%s%s%s(%02x)\n",
1791 sc->sc_unit,
1792 rstat & FE_D1_OVRFLO ? "OVR " : "",
1793 rstat & FE_D1_CRCERR ? "CRC " : "",
1794 rstat & FE_D1_ALGERR ? "ALG " : "",
1795 rstat & FE_D1_SRTPKT ? "LEN " : "",
1796 rstat );
1797#endif
1798 sc->sc_if.if_ierrors++;
1799 }
1800
1801 /*
1802 * MB86960 has a flag indicating "receive queue empty."
1803 * We just loop cheking the flag to pull out all received
1804 * packets.
1805 *
1806 * We limit the number of iterrations to avoid inifnit-loop.
1807 * It can be caused by a very slow CPU (some broken
1808 * peripheral may insert incredible number of wait cycles)
1809 * or, worse, by a broken MB86960 chip.
1810 */
1811 for ( i = 0; i < FE_MAX_RECV_COUNT; i++ ) {
1812
1813 /* Stop the iterration if 86960 indicates no packets. */
1814 if ( inb( sc->addr + FE_DLCR5 ) & FE_D5_BUFEMP ) break;
1815
1816 /*
1817 * Extract A receive status byte.
1818 * As our 86960 is in 16 bit bus access mode, we have to
1819 * use inw() to get the status byte. The significant
1820 * value is returned in lower 8 bits.
1821 */
1822 status = ( u_char )inw( sc->addr + FE_BMPR8 );
1823#if FE_DEBUG >= 4
1824 log( LOG_INFO, "fe%d: receive status = %04x\n",
1825 sc->sc_unit, status );
1826#endif
1827
1828 /*
1829 * If there was an error, update statistics and drop
1830 * the packet, unless the interface is in promiscuous
1831 * mode.
1832 */
1833 if ( ( status & 0xF0 ) != 0x20 ) {
1834 if ( !( sc->sc_if.if_flags & IFF_PROMISC ) ) {
1835 sc->sc_if.if_ierrors++;
1836 fe_droppacket(sc);
1837 continue;
1838 }
1839 }
1840
1841 /*
1842 * Extract the packet length.
1843 * It is a sum of a header (14 bytes) and a payload.
1844 * CRC has been stripped off by the 86960.
1845 */
1846 len = inw( sc->addr + FE_BMPR8 );
1847
1848 /*
1849 * MB86965 checks the packet length and drop big packet
1850 * before passing it to us. There are no chance we can
1851 * get [crufty] packets. Hence, if the length exceeds
1852 * the specified limit, it means some serious failure,
1853 * such as out-of-sync on receive buffer management.
1854 *
1855 * Is this statement true? FIXME.
1856 */
1857 if ( len > ETHER_MAX_LEN || len < ETHER_HDR_SIZE ) {
1858#if FE_DEBUG >= 2
1859 log( LOG_WARNING,
1860 "fe%d: received a %s packet? (%u bytes)\n",
1861 sc->sc_unit,
1862 len < ETHER_HDR_SIZE ? "partial" : "big",
1863 len );
1864#endif
1865 sc->sc_if.if_ierrors++;
1866 fe_droppacket( sc );
1867 continue;
1868 }
1869
1870 /*
1871 * Check for a short (RUNT) packet. We *do* check
1872 * but do nothing other than print a message.
1873 * Short packets are illegal, but does nothing bad
1874 * if it carries data for upper layer.
1875 */
1876#if FE_DEBUG >= 2
1877 if ( len < ETHER_MIN_LEN ) {
1878 log( LOG_WARNING,
1879 "fe%d: received a short packet? (%u bytes)\n",
1880 sc->sc_unit, len );
1881 }
1882#endif
1883
1884 /*
1885 * Go get a packet.
1886 */
1887 if ( fe_get_packet( sc, len ) < 0 ) {
1888 /* Skip a packet, updating statistics. */
1889#if FE_DEBUG >= 2
1890 log( LOG_WARNING, "%s%d: no enough mbuf;"
1891 " a packet (%u bytes) dropped\n",
1892 sc->sc_unit, len );
1893#endif
1894 sc->sc_if.if_ierrors++;
1895 fe_droppacket( sc );
1896
1897 /*
1898 * We stop receiving packets, even if there are
1899 * more in the buffer. We hope we can get more
1900 * mbuf next time.
1901 */
1902 return;
1903 }
1904
1905 /* Successfully received a packet. Update stat. */
1906 sc->sc_if.if_ipackets++;
1907 }
1908}
1909
1910/*
1911 * Ethernet interface interrupt processor
1912 */
1913void
1914feintr ( int unit )
1915{
1916 struct fe_softc *sc = &fe_softc[unit];
1917 u_char tstat, rstat;
1918
1919 /*
1920 * Loop until there are no more new interrupt conditions.
1921 */
1922 for (;;) {
1923
1924#if FE_DEBUG >= 4
1925 fe_dump( LOG_INFO, sc, "intr()" );
1926#endif
1927
1928 /*
1929 * Get interrupt conditions, masking unneeded flags.
1930 */
1931 tstat = inb( sc->addr + FE_DLCR0 ) & FE_TMASK;
1932 rstat = inb( sc->addr + FE_DLCR1 ) & FE_RMASK;
1933 if ( tstat == 0 && rstat == 0 ) break;
1934
1935 /*
1936 * Reset the conditions we are acknowledging.
1937 */
1938 outb( sc->addr + FE_DLCR0, tstat );
1939 outb( sc->addr + FE_DLCR1, rstat );
1940
1941 /*
1942 * Handle transmitter interrupts. Handle these first because
1943 * the receiver will reset the board under some conditions.
1944 */
1945 if ( tstat ) {
1946 fe_tint( sc, tstat );
1947 }
1948
1949 /*
1950 * Handle receiver interrupts
1951 */
1952 if ( rstat ) {
1953 fe_rint( sc, rstat );
1954 }
1955
1956 /*
1957 * Update the multicast address filter if it is
1958 * needed and possible. We do it now, because
1959 * we can make sure the transmission buffer is empty,
1960 * and there is a good chance that the receive queue
1961 * is empty. It will minimize the possibility of
1962 * packet lossage.
1963 */
1964 if ( sc->filter_change
1965 && sc->txb_count == 0 && sc->txb_sched == 0 ) {
1966 fe_loadmar(sc);
1967 sc->sc_if.if_flags &= ~IFF_OACTIVE;
1968 }
1969
1970 /*
1971 * If it looks like the transmitter can take more data,
1972 * attempt to start output on the interface. This is done
1973 * after handling the receiver interrupt to give the
1974 * receive operation priority.
1975 *
1976 * BTW, I'm not sure in what case the OACTIVE is on at
1977 * this point. Is the following test redundant?
1978 *
1979 * No. This routine polls for both transmitter and
1980 * receiver interrupts. 86960 can raise a receiver
1981 * interrupt when the transmission buffer is full.
1982 */
1983 if ( ( sc->sc_if.if_flags & IFF_OACTIVE ) == 0 ) {
1984 fe_start( &sc->sc_if );
1985 }
1986
1987 }
1988}
1989
1990/*
1991 * Process an ioctl request. This code needs some work - it looks
1992 * pretty ugly.
1993 */
1994int
1995fe_ioctl ( struct ifnet *ifp, int command, caddr_t data )
1996{
1997 struct fe_softc *sc = IFNET2SOFTC( ifp );
1998 int s, error = 0;
1999
2000#if FE_DEBUG >= 3
2001 log( LOG_INFO, "fe%d: ioctl(%x)\n", sc->sc_unit, command );
2002#endif
2003
2004 s = splimp();
2005
2006 switch (command) {
2007
2008 case SIOCSIFADDR:
2009 {
2010 struct ifaddr * ifa = ( struct ifaddr * )data;
2011
2012 sc->sc_if.if_flags |= IFF_UP;
2013
2014 switch (ifa->ifa_addr->sa_family) {
2015#ifdef INET
2016 case AF_INET:
2017 fe_init( sc->sc_unit ); /* before arpwhohas */
2018 arp_ifinit( &sc->arpcom, ifa );
2019 break;
2020#endif
2021#ifdef IPX
2022
2023 /*
2024 * XXX - This code is probably wrong
2025 */
2026 case AF_IPX:
2027 {
2028 register struct ipx_addr *ina
2029 = &(IA_SIPX(ifa)->sipx_addr);
2030
2031 if (ipx_nullhost(*ina))
2032 ina->x_host =
2033 *(union ipx_host *) (sc->sc_enaddr);
2034 else {
2035 bcopy((caddr_t) ina->x_host.c_host,
2036 (caddr_t) sc->sc_enaddr,
2037 sizeof(sc->sc_enaddr));
2038 }
2039
2040 /*
2041 * Set new address
2042 */
2043 fe_init(sc->sc_unit);
2044 break;
2045 }
2046#endif
2047#ifdef NS
2048
2049 /*
2050 * XXX - This code is probably wrong
2051 */
2052 case AF_NS:
2053 {
2054 register struct ns_addr *ina
2055 = &(IA_SNS(ifa)->sns_addr);
2056
2057 if (ns_nullhost(*ina))
2058 ina->x_host =
2059 *(union ns_host *) (sc->sc_enaddr);
2060 else {
2061 bcopy((caddr_t) ina->x_host.c_host,
2062 (caddr_t) sc->sc_enaddr,
2063 sizeof(sc->sc_enaddr));
2064 }
2065
2066 /*
2067 * Set new address
2068 */
2069 fe_init(sc->sc_unit);
2070 break;
2071 }
2072#endif
2073 default:
2074 fe_init( sc->sc_unit );
2075 break;
2076 }
2077 break;
2078 }
2079
2080#ifdef SIOCGIFADDR
2081 case SIOCGIFADDR:
2082 {
2083 struct ifreq * ifr = ( struct ifreq * )data;
2084 struct sockaddr * sa = ( struct sockaddr * )&ifr->ifr_data;
2085
2086 bcopy((caddr_t)sc->sc_enaddr,
2087 (caddr_t)sa->sa_data, ETHER_ADDR_LEN);
2088 break;
2089 }
2090#endif
2091
2092#ifdef SIOCGIFPHYSADDR
2093 case SIOCGIFPHYSADDR:
2094 {
2095 struct ifreq * ifr = ( struct ifreq * )data;
2096
2097 bcopy((caddr_t)sc->sc_enaddr,
2098 (caddr_t)&ifr->ifr_data, ETHER_ADDR_LEN);
2099 break;
2100 }
2101#endif
2102
2103#ifdef SIOCSIFPHYSADDR
2104 case SIOCSIFPHYSADDR:
2105 {
2106 /*
2107 * Set the physical (Ehternet) address of the interface.
2108 * When and by whom is this command used? FIXME.
2109 */
2110 struct ifreq * ifr = ( struct ifreq * )data;
2111
2112 bcopy((caddr_t)&ifr->ifr_data,
2113 (caddr_t)sc->sc_enaddr, ETHER_ADDR_LEN);
2114 fe_setlinkaddr( sc );
2115 break;
2116 }
2117#endif
2118
2119#ifdef SIOCSIFFLAGS
2120 case SIOCSIFFLAGS:
2121 {
2122 /*
2123 * Switch interface state between "running" and
2124 * "stopped", reflecting the UP flag.
2125 */
2126 if ( sc->sc_if.if_flags & IFF_UP ) {
2127 if ( ( sc->sc_if.if_flags & IFF_RUNNING ) == 0 ) {
2128 fe_init( sc->sc_unit );
2129 }
2130 } else {
2131 if ( ( sc->sc_if.if_flags & IFF_RUNNING ) != 0 ) {
2132 fe_stop( sc->sc_unit );
2133 }
2134 }
2135
2136 /*
2137 * Promiscuous and/or multicast flags may have changed,
2138 * so reprogram the multicast filter and/or receive mode.
2139 */
2140 fe_setmode( sc );
2141
2142#if FE_DEBUG >= 1
2143 /* "ifconfig fe0 debug" to print register dump. */
2144 if ( sc->sc_if.if_flags & IFF_DEBUG ) {
2145 fe_dump( LOG_DEBUG, sc, "SIOCSIFFLAGS(DEBUG)" );
2146 }
2147#endif
2148 break;
2149 }
2150#endif
2151
2152#ifdef SIOCADDMULTI
2153 case SIOCADDMULTI:
2154 case SIOCDELMULTI:
2155 {
2156 /*
2157 * Update out multicast list.
2158 */
2159 struct ifreq * ifr = ( struct ifreq * )data;
2160
2161 error = ( command == SIOCADDMULTI )
2162 ? ether_addmulti( ifr, &sc->arpcom )
2163 : ether_delmulti( ifr, &sc->arpcom );
2164
2165 if ( error == ENETRESET ) {
2166 /*
2167 * Multicast list has changed; set the hardware filter
2168 * accordingly.
2169 */
2170 fe_setmode( sc );
2171 error = 0;
2172 }
2173
2174 break;
2175 }
2176#endif
2177
2178#ifdef SIOCSIFMTU
2179 case SIOCSIFMTU:
2180 {
2181 /*
2182 * Set the interface MTU.
2183 */
2184 struct ifreq * ifr = ( struct ifreq * )data;
2185
2186 if ( ifr->ifr_mtu > ETHERMTU ) {
2187 error = EINVAL;
2188 } else {
2189 sc->sc_if.if_mtu = ifr->ifr_mtu;
2190 }
2191 break;
2192 }
2193#endif
2194
2195 default:
2196 error = EINVAL;
2197 }
2198
2199 (void) splx(s);
2200 return (error);
2201}
2202
2203/*
2204 * Retreive packet from receive buffer and send to the next level up via
2205 * ether_input(). If there is a BPF listener, give a copy to BPF, too.
2206 * Returns 0 if success, -1 if error (i.e., mbuf allocation failure).
2207 */
2208static int
2209fe_get_packet ( struct fe_softc * sc, u_short len )
2210{
2211 struct ether_header *eh;
2212 struct mbuf *m;
2213
2214 /*
2215 * NFS wants the data be aligned to the word (4 byte)
2216 * boundary. Ethernet header has 14 bytes. There is a
2217 * 2-byte gap.
2218 */
2219#define NFS_MAGIC_OFFSET 2
2220
2221 /*
2222 * This function assumes that an Ethernet packet fits in an
2223 * mbuf (with a cluster attached when necessary.) On FreeBSD
2224 * 2.0 for x86, which is the primary target of this driver, an
2225 * mbuf cluster has 4096 bytes, and we are happy. On ancient
2226 * BSDs, such as vanilla 4.3 for 386, a cluster size was 1024,
2227 * however. If the following #error message were printed upon
2228 * compile, you need to rewrite this function.
2229 */
2230#if ( MCLBYTES < ETHER_MAX_LEN + NFS_MAGIC_OFFSET )
2231#error "Too small MCLBYTES to use fe driver."
2232#endif
2233
2234 /*
2235 * Our strategy has one more problem. There is a policy on
2236 * mbuf cluster allocation. It says that we must have at
2237 * least MINCLSIZE (208 bytes on FreeBSD 2.0 for x86) to
2238 * allocate a cluster. For a packet of a size between
2239 * (MHLEN - 2) to (MINCLSIZE - 2), our code violates the rule...
2240 * On the other hand, the current code is short, simle,
2241 * and fast, however. It does no harmful thing, just waists
2242 * some memory. Any comments? FIXME.
2243 */
2244
2245 /* Allocate an mbuf with packet header info. */
2246 MGETHDR(m, M_DONTWAIT, MT_DATA);
2247 if ( m == NULL ) return -1;
2248
2249 /* Attach a cluster if this packet doesn't fit in a normal mbuf. */
2250 if ( len > MHLEN - NFS_MAGIC_OFFSET ) {
2251 MCLGET( m, M_DONTWAIT );
2252 if ( !( m->m_flags & M_EXT ) ) {
2253 m_freem( m );
2254 return -1;
2255 }
2256 }
2257
2258 /* Initialize packet header info. */
2259 m->m_pkthdr.rcvif = &sc->sc_if;
2260 m->m_pkthdr.len = len;
2261
2262 /* Set the length of this packet. */
2263 m->m_len = len;
2264
2265 /* The following sillines is to make NFS happy */
2266 m->m_data += NFS_MAGIC_OFFSET;
2267
2268 /* Get a packet. */
2269 insw( sc->addr + FE_BMPR8, m->m_data, ( len + 1 ) >> 1 );
2270
2271 /* Get (actually just point to) the header part. */
2272 eh = mtod( m, struct ether_header *);
2273
2274#define ETHER_ADDR_IS_MULTICAST(A) (*(char *)(A) & 1)
2275
2276#if NBPFILTER > 0
2277 /*
2278 * Check if there's a BPF listener on this interface.
2279 * If it is, hand off the raw packet to bpf.
2280 */
2281 if ( sc->bpf ) {
2282 bpf_mtap( sc->bpf, m );
2283 }
2284#endif
2285
2286 /*
2287 * Make sure this packet is (or may be) directed to us.
2288 * That is, the packet is either unicasted to our address,
2289 * or broad/multi-casted. If any other packets are
2290 * received, it is an indication of an error -- probably
2291 * 86960 is in a wrong operation mode.
2292 * Promiscuous mode is an exception. Under the mode, all
2293 * packets on the media must be received. (We must have
2294 * programmed the 86960 so.)
2295 */
2296
2297 if ( ( sc->sc_if.if_flags & IFF_PROMISC )
2298 && !ETHER_ADDR_IS_MULTICAST( eh->ether_dhost )
2299 && bcmp( eh->ether_dhost, sc->sc_enaddr, ETHER_ADDR_LEN ) != 0 ) {
2300 /*
2301 * The packet was not for us. This is normal since
2302 * we are now in promiscuous mode. Just drop the packet.
2303 */
2304 m_freem( m );
2305 return 0;
2306 }
2307
2308#if FE_DEBUG >= 3
2309 if ( !ETHER_ADDR_IS_MULTICAST( eh->ether_dhost )
2310 && bcmp( eh->ether_dhost, sc->sc_enaddr, ETHER_ADDR_LEN ) != 0 ) {
2311 /*
2312 * This packet was not for us. We can't be in promiscuous
2313 * mode since the case was handled by above test.
2314 * We found an error (of this driver.)
2315 */
2316 log( LOG_WARNING,
2317 "fe%d: got an unwanted packet, dst = %6D\n",
2318 sc->sc_unit,
2319 eh->ether_dhost , ":" );
2320 m_freem( m );
2321 return 0;
2322 }
2323#endif
2324
2325 /* Strip off the Ethernet header. */
2326 m->m_pkthdr.len -= sizeof ( struct ether_header );
2327 m->m_len -= sizeof ( struct ether_header );
2328 m->m_data += sizeof ( struct ether_header );
2329
2330 /* Feed the packet to upper layer. */
2331 ether_input( &sc->sc_if, eh, m );
2332 return 0;
2333}
2334
2335/*
2336 * Write an mbuf chain to the transmission buffer memory using 16 bit PIO.
2337 * Returns number of bytes actually written, including length word.
2338 *
2339 * If an mbuf chain is too long for an Ethernet frame, it is not sent.
2340 * Packets shorter than Ethernet minimum are legal, and we pad them
2341 * before sending out. An exception is "partial" packets which are
2342 * shorter than mandatory Ethernet header.
2343 *
2344 * I wrote a code for an experimental "delayed padding" technique.
2345 * When employed, it postpones the padding process for short packets.
2346 * If xmit() occured at the moment, the padding process is omitted, and
2347 * garbages are sent as pad data. If next packet is stored in the
2348 * transmission buffer before xmit(), write_mbuf() pads the previous
2349 * packet before transmitting new packet. This *may* gain the
2350 * system performance (slightly).
2351 */
2352static void
2353fe_write_mbufs ( struct fe_softc *sc, struct mbuf *m )
2354{
2355 u_short addr_bmpr8 = sc->addr + FE_BMPR8;
2356 u_short length, len;
2357 short pad;
2358 struct mbuf *mp;
2359 u_char *data;
2360 u_short savebyte; /* WARNING: Architecture dependent! */
2361#define NO_PENDING_BYTE 0xFFFF
2362
2363#if FE_DELAYED_PADDING
2364 /* Do the "delayed padding." */
2365 pad = sc->txb_padding >> 1;
2366 if ( pad > 0 ) {
2367 while ( --pad >= 0 ) {
2368 outw( addr_bmpr8, 0 );
2369 }
2370 sc->txb_padding = 0;
2371 }
2372#endif
2373
2374#if FE_DEBUG >= 2
2375 /* First, count up the total number of bytes to copy */
2376 length = 0;
2377 for ( mp = m; mp != NULL; mp = mp->m_next ) {
2378 length += mp->m_len;
2379 }
2380 /* Check if this matches the one in the packet header. */
2381 if ( length != m->m_pkthdr.len ) {
2382 log( LOG_WARNING, "fe%d: packet length mismatch? (%d/%d)\n",
2383 sc->sc_unit, length, m->m_pkthdr.len );
2384 }
2385#else
2386 /* Just use the length value in the packet header. */
2387 length = m->m_pkthdr.len;
2388#endif
2389
2390#if FE_DEBUG >= 1
2391 /*
2392 * Should never send big packets. If such a packet is passed,
2393 * it should be a bug of upper layer. We just ignore it.
2394 * ... Partial (too short) packets, neither.
2395 */
2396 if ( length > ETHER_MAX_LEN || length < ETHER_HDR_SIZE ) {
2397 log( LOG_ERR,
2398 "fe%d: got a %s packet (%u bytes) to send\n",
2399 sc->sc_unit,
2400 length < ETHER_HDR_SIZE ? "partial" : "big", length );
2401 sc->sc_if.if_oerrors++;
2402 return;
2403 }
2404#endif
2405
2406 /*
2407 * Put the length word for this frame.
2408 * Does 86960 accept odd length? -- Yes.
2409 * Do we need to pad the length to minimum size by ourselves?
2410 * -- Generally yes. But for (or will be) the last
2411 * packet in the transmission buffer, we can skip the
2412 * padding process. It may gain performance slightly. FIXME.
2413 */
2414 outw( addr_bmpr8, max( length, ETHER_MIN_LEN ) );
2415
2416 /*
2417 * Update buffer status now.
2418 * Truncate the length up to an even number, since we use outw().
2419 */
2420 length = ( length + 1 ) & ~1;
2421 sc->txb_free -= FE_DATA_LEN_LEN + max( length, ETHER_MIN_LEN );
2422 sc->txb_count++;
2423
2424#if FE_DELAYED_PADDING
2425 /* Postpone the packet padding if necessary. */
2426 if ( length < ETHER_MIN_LEN ) {
2427 sc->txb_padding = ETHER_MIN_LEN - length;
2428 }
2429#endif
2430
2431 /*
2432 * Transfer the data from mbuf chain to the transmission buffer.
2433 * MB86960 seems to require that data be transferred as words, and
2434 * only words. So that we require some extra code to patch
2435 * over odd-length mbufs.
2436 */
2437 savebyte = NO_PENDING_BYTE;
2438 for ( mp = m; mp != 0; mp = mp->m_next ) {
2439
2440 /* Ignore empty mbuf. */
2441 len = mp->m_len;
2442 if ( len == 0 ) continue;
2443
2444 /* Find the actual data to send. */
2445 data = mtod(mp, caddr_t);
2446
2447 /* Finish the last byte. */
2448 if ( savebyte != NO_PENDING_BYTE ) {
2449 outw( addr_bmpr8, savebyte | ( *data << 8 ) );
2450 data++;
2451 len--;
2452 savebyte = NO_PENDING_BYTE;
2453 }
2454
2455 /* output contiguous words */
2456 if (len > 1) {
2457 outsw( addr_bmpr8, data, len >> 1);
2458 data += len & ~1;
2459 len &= 1;
2460 }
2461
2462 /* Save a remaining byte, if there is one. */
2463 if ( len > 0 ) {
2464 savebyte = *data;
2465 }
2466 }
2467
2468 /* Spit the last byte, if the length is odd. */
2469 if ( savebyte != NO_PENDING_BYTE ) {
2470 outw( addr_bmpr8, savebyte );
2471 }
2472
2473#if ! FE_DELAYED_PADDING
2474 /*
2475 * Pad the packet to the minimum length if necessary.
2476 */
2477 pad = ( ETHER_MIN_LEN >> 1 ) - ( length >> 1 );
2478 while ( --pad >= 0 ) {
2479 outw( addr_bmpr8, 0 );
2480 }
2481#endif
2482}
2483
2484/*
2485 * Compute hash value for an Ethernet address
2486 */
2487static int
2488fe_hash ( u_char * ep )
2489{
2490#define FE_HASH_MAGIC_NUMBER 0xEDB88320L
2491
2492 u_long hash = 0xFFFFFFFFL;
2493 int i, j;
2494 u_char b;
2495 u_long m;
2496
2497 for ( i = ETHER_ADDR_LEN; --i >= 0; ) {
2498 b = *ep++;
2499 for ( j = 8; --j >= 0; ) {
2500 m = hash;
2501 hash >>= 1;
2502 if ( ( m ^ b ) & 1 ) hash ^= FE_HASH_MAGIC_NUMBER;
2503 b >>= 1;
2504 }
2505 }
2506 return ( ( int )( hash >> 26 ) );
2507}
2508
2509/*
2510 * Compute the multicast address filter from the
2511 * list of multicast addresses we need to listen to.
2512 */
2513static struct fe_filter
2514fe_mcaf ( struct fe_softc *sc )
2515{
2516 int index;
2517 struct fe_filter filter;
2518 struct ether_multi *enm;
2519 struct ether_multistep step;
2520
2521 filter = fe_filter_nothing;
2522 ETHER_FIRST_MULTI(step, &sc->arpcom, enm);
2523 while ( enm != NULL) {
2524 if ( bcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN) ) {
2525 return ( fe_filter_all );
2526 }
2527 index = fe_hash( enm->enm_addrlo );
2528#if FE_DEBUG >= 4
2529 log( LOG_INFO, "fe%d: hash(%6D) == %d\n",
2530 sc->sc_unit, enm->enm_addrlo , ":", index );
2531#endif
2532
2533 filter.data[index >> 3] |= 1 << (index & 7);
2534 ETHER_NEXT_MULTI(step, enm);
2535 }
2536 return ( filter );
2537}
2538
2539/*
2540 * Calculate a new "multicast packet filter" and put the 86960
2541 * receiver in appropriate mode.
2542 */
2543static void
2544fe_setmode ( struct fe_softc *sc )
2545{
2546 int flags = sc->sc_if.if_flags;
2547
2548 /*
2549 * If the interface is not running, we postpone the update
2550 * process for receive modes and multicast address filter
2551 * until the interface is restarted. It reduces some
2552 * complicated job on maintaining chip states. (Earlier versions
2553 * of this driver had a bug on that point...)
2554 *
2555 * To complete the trick, fe_init() calls fe_setmode() after
2556 * restarting the interface.
2557 */
2558 if ( !( flags & IFF_RUNNING ) ) return;
2559
2560 /*
2561 * Promiscuous mode is handled separately.
2562 */
2563 if ( flags & IFF_PROMISC ) {
2564 /*
2565 * Program 86960 to receive all packets on the segment
2566 * including those directed to other stations.
2567 * Multicast filter stored in MARs are ignored
2568 * under this setting, so we don't need to update it.
2569 *
2570 * Promiscuous mode in FreeBSD 2 is used solely by
2571 * BPF, and BPF only listens to valid (no error) packets.
2572 * So, we ignore errornous ones even in this mode.
2573 * (Older versions of fe driver mistook the point.)
2574 */
2575 outb( sc->addr + FE_DLCR5,
2576 sc->proto_dlcr5 | FE_D5_AFM0 | FE_D5_AFM1 );
2577 sc->filter_change = 0;
2578
2579#if FE_DEBUG >= 3
2580 log( LOG_INFO, "fe%d: promiscuous mode\n", sc->sc_unit );
2581#endif
2582 return;
2583 }
2584
2585 /*
2586 * Turn the chip to the normal (non-promiscuous) mode.
2587 */
2588 outb( sc->addr + FE_DLCR5, sc->proto_dlcr5 | FE_D5_AFM1 );
2589
2590 /*
2591 * Find the new multicast filter value.
2592 * I'm not sure we have to handle modes other than MULTICAST.
2593 * Who sets ALLMULTI? Who turns MULTICAST off? FIXME.
2594 */
2595 if ( flags & IFF_ALLMULTI ) {
2596 sc->filter = fe_filter_all;
2597 } else if ( flags & IFF_MULTICAST ) {
2598 sc->filter = fe_mcaf( sc );
2599 } else {
2600 sc->filter = fe_filter_nothing;
2601 }
2602 sc->filter_change = 1;
2603
2604#if FE_DEBUG >= 3
2605 log( LOG_INFO, "fe%d: address filter:"
2606 " [%02x %02x %02x %02x %02x %02x %02x %02x]\n",
2607 sc->sc_unit,
2608 sc->filter.data[0], sc->filter.data[1],
2609 sc->filter.data[2], sc->filter.data[3],
2610 sc->filter.data[4], sc->filter.data[5],
2611 sc->filter.data[6], sc->filter.data[7] );
2612#endif
2613
2614 /*
2615 * We have to update the multicast filter in the 86960, A.S.A.P.
2616 *
2617 * Note that the DLC (Data Linc Control unit, i.e. transmitter
2618 * and receiver) must be stopped when feeding the filter, and
2619 * DLC trushes all packets in both transmission and receive
2620 * buffers when stopped.
2621 *
2622 * ... Are the above sentenses correct? I have to check the
2623 * manual of the MB86960A. FIXME.
2624 *
2625 * To reduce the packet lossage, we delay the filter update
2626 * process until buffers are empty.
2627 */
2628 if ( sc->txb_sched == 0 && sc->txb_count == 0
2629 && !( inb( sc->addr + FE_DLCR1 ) & FE_D1_PKTRDY ) ) {
2630 /*
2631 * Buffers are (apparently) empty. Load
2632 * the new filter value into MARs now.
2633 */
2634 fe_loadmar(sc);
2635 } else {
2636 /*
2637 * Buffers are not empty. Mark that we have to update
2638 * the MARs. The new filter will be loaded by feintr()
2639 * later.
2640 */
2641#if FE_DEBUG >= 4
2642 log( LOG_INFO, "fe%d: filter change delayed\n", sc->sc_unit );
2643#endif
2644 }
2645}
2646
2647/*
2648 * Load a new multicast address filter into MARs.
2649 *
2650 * The caller must have splimp'ed befor fe_loadmar.
2651 * This function starts the DLC upon return. So it can be called only
2652 * when the chip is working, i.e., from the driver's point of view, when
2653 * a device is RUNNING. (I mistook the point in previous versions.)
2654 */
2655static void
2656fe_loadmar ( struct fe_softc * sc )
2657{
2658 /* Stop the DLC (transmitter and receiver). */
2659 outb( sc->addr + FE_DLCR6, sc->proto_dlcr6 | FE_D6_DLC_DISABLE );
2660
2661 /* Select register bank 1 for MARs. */
2662 outb( sc->addr + FE_DLCR7,
2663 sc->proto_dlcr7 | FE_D7_RBS_MAR | FE_D7_POWER_UP );
2664
2665 /* Copy filter value into the registers. */
2666 outblk( sc->addr + FE_MAR8, sc->filter.data, FE_FILTER_LEN );
2667
2668 /* Restore the bank selection for BMPRs (i.e., runtime registers). */
2669 outb( sc->addr + FE_DLCR7,
2670 sc->proto_dlcr7 | FE_D7_RBS_BMPR | FE_D7_POWER_UP );
2671
2672 /* Restart the DLC. */
2673 outb( sc->addr + FE_DLCR6, sc->proto_dlcr6 | FE_D6_DLC_ENABLE );
2674
2675 /* We have just updated the filter. */
2676 sc->filter_change = 0;
2677
2678#if FE_DEBUG >= 3
2679 log( LOG_INFO, "fe%d: address filter changed\n", sc->sc_unit );
2680#endif
2681}
2682
2683/*
2684 * Copy the physical (Ethernet) address into the "data link" address
2685 * entry of the address list for an interface.
2686 * This is (said to be) useful for netstat(1) to keep track of which
2687 * interface is which.
2688 *
2689 * What I'm not sure on this function is, why this is a driver's function.
2690 * Probably this should be moved to somewhere independent to a specific
2691 * hardware, such as if_ehtersubr.c. FIXME.
2692 */
2693static void
2694fe_setlinkaddr ( struct fe_softc * sc )
2695{
2696 struct ifaddr *ifa;
2697 struct sockaddr_dl * sdl;
2698
2699 /*
2700 * Search down the ifa address list looking for the AF_LINK type entry.
2701 */
2702 for ( ifa = sc->sc_if.if_addrlist; ifa != NULL; ifa = ifa->ifa_next ) {
2703 if ( ifa->ifa_addr != NULL
2704 && ifa->ifa_addr->sa_family == AF_LINK ) {
2705
2706 /*
2707 * We have found an AF_LINK type entry.
2708 * Fill in the link-level address for this interface
2709 */
2710 sdl = (struct sockaddr_dl *) ifa->ifa_addr;
2711 sdl->sdl_type = IFT_ETHER;
2712 sdl->sdl_alen = ETHER_ADDR_LEN;
2713 sdl->sdl_slen = 0;
2714 bcopy(sc->sc_enaddr, LLADDR(sdl), ETHER_ADDR_LEN);
2715#if FE_DEBUG >= 3
2716 log( LOG_INFO, "fe%d: link address set\n",
2717 sc->sc_unit );
2718#endif
2719 return;
2720 }
2721 }
2722}
2723
2724#if FE_DEBUG >= 1
2725static void
2726fe_dump ( int level, struct fe_softc * sc, char * message )
2727{
2728 log( level, "fe%d: %s,"
2729 " DLCR = %02x %02x %02x %02x %02x %02x %02x %02x,"
2730 " BMPR = xx xx %02x %02x %02x %02x %02x %02x,"
2731 " asic = %02x %02x %02x %02x %02x %02x %02x %02x"
2732 " + %02x %02x %02x %02x %02x %02x %02x %02x\n",
2733 sc->sc_unit, message ? message : "registers",
2734 inb( sc->addr + FE_DLCR0 ), inb( sc->addr + FE_DLCR1 ),
2735 inb( sc->addr + FE_DLCR2 ), inb( sc->addr + FE_DLCR3 ),
2736 inb( sc->addr + FE_DLCR4 ), inb( sc->addr + FE_DLCR5 ),
2737 inb( sc->addr + FE_DLCR6 ), inb( sc->addr + FE_DLCR7 ),
2738 inb( sc->addr + FE_BMPR10 ), inb( sc->addr + FE_BMPR11 ),
2739 inb( sc->addr + FE_BMPR12 ), inb( sc->addr + FE_BMPR13 ),
2740 inb( sc->addr + FE_BMPR14 ), inb( sc->addr + FE_BMPR15 ),
2741 inb( sc->addr + 0x10 ), inb( sc->addr + 0x11 ),
2742 inb( sc->addr + 0x12 ), inb( sc->addr + 0x13 ),
2743 inb( sc->addr + 0x14 ), inb( sc->addr + 0x15 ),
2744 inb( sc->addr + 0x16 ), inb( sc->addr + 0x17 ),
2745 inb( sc->addr + 0x18 ), inb( sc->addr + 0x19 ),
2746 inb( sc->addr + 0x1A ), inb( sc->addr + 0x1B ),
2747 inb( sc->addr + 0x1C ), inb( sc->addr + 0x1D ),
2748 inb( sc->addr + 0x1E ), inb( sc->addr + 0x1F ) );
2749}
2750#endif