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