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