if_xname support Part 1/2: Convert most of the netif devices to use
[dragonfly.git] / sys / dev / netif / fe / if_fe.c
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  * $FreeBSD: src/sys/dev/fe/if_fe.c,v 1.65.2.1 2000/09/22 10:01:47 nyan Exp $
25  * $DragonFly: src/sys/dev/netif/fe/if_fe.c,v 1.6 2004/01/06 01:40:47 dillon Exp $
26  *
27  * Device driver for Fujitsu MB86960A/MB86965A based Ethernet cards.
28  * Contributed by M. Sekiguchi. <seki@sysrap.cs.fujitsu.co.jp>
29  *
30  * This version is intended to be a generic template for various
31  * MB86960A/MB86965A based Ethernet cards.  It currently supports
32  * Fujitsu FMV-180 series for ISA and Allied-Telesis AT1700/RE2000
33  * series for ISA, as well as Fujitsu MBH10302 PC card.
34  * There are some currently-
35  * unused hooks embedded, which are primarily intended to support
36  * other types of Ethernet cards, but the author is not sure whether
37  * they are useful.
38  *
39  * This version also includes some alignments to support RE1000,
40  * C-NET(98)P2 and so on. These cards are not for AT-compatibles,
41  * but for NEC PC-98 bus -- a proprietary bus architecture available
42  * only in Japan. Confusingly, it is different from the Microsoft's
43  * PC98 architecture. :-{
44  * Further work for PC-98 version will be available as a part of
45  * FreeBSD(98) project.
46  *
47  * This software is a derivative work of if_ed.c version 1.56 by David
48  * Greenman available as a part of FreeBSD 2.0 RELEASE source distribution.
49  *
50  * The following lines are retained from the original if_ed.c:
51  *
52  * Copyright (C) 1993, David Greenman. This software may be used, modified,
53  *   copied, distributed, and sold, in both source and binary form provided
54  *   that the above copyright and these terms are retained. Under no
55  *   circumstances is the author responsible for the proper functioning
56  *   of this software, nor does the author assume any responsibility
57  *   for damages incurred with its use.
58  */
59
60 /*
61  * TODO:
62  *  o   To support ISA PnP auto configuration for FMV-183/184.
63  *  o   To support REX-9886/87(PC-98 only).
64  *  o   To reconsider mbuf usage.
65  *  o   To reconsider transmission buffer usage, including
66  *      transmission buffer size (currently 4KB x 2) and pros-and-
67  *      cons of multiple frame transmission.
68  *  o   To test IPX codes.
69  *  o   To test new-bus frontend.
70  */
71
72 #include "opt_fe.h"
73 #include "opt_inet.h"
74 #include "opt_ipx.h"
75
76 #include <sys/param.h>
77 #include <sys/systm.h>
78 #include <sys/socket.h>
79 #include <sys/sockio.h>
80 #include <sys/mbuf.h>
81 #include <sys/interrupt.h>
82 #include <sys/linker_set.h>
83 #include <sys/module.h>
84 #include <machine/clock.h>
85
86 #include <sys/bus.h>
87 #include <machine/bus.h>
88 #include <sys/rman.h>
89 #include <machine/resource.h>
90
91 #include <net/ethernet.h>
92 #include <net/if.h>
93 #include <net/if_dl.h>
94 #include <net/if_mib.h>
95 #include <net/if_media.h>
96
97 #include <netinet/in.h>
98 #include <netinet/if_ether.h>
99
100 #include <net/bpf.h>
101
102 #include <i386/isa/ic/mb86960.h>
103 #include "if_fereg.h"
104 #include "if_fevar.h"
105
106 /*
107  * Transmit just one packet per a "send" command to 86960.
108  * This option is intended for performance test.  An EXPERIMENTAL option.
109  */
110 #ifndef FE_SINGLE_TRANSMISSION
111 #define FE_SINGLE_TRANSMISSION 0
112 #endif
113
114 /*
115  * Maximum loops when interrupt.
116  * This option prevents an infinite loop due to hardware failure.
117  * (Some laptops make an infinite loop after PC-Card is ejected.)
118  */
119 #ifndef FE_MAX_LOOP
120 #define FE_MAX_LOOP 0x800
121 #endif
122
123 /*
124  * If you define this option, 8-bit cards are also supported.
125  */
126 /*#define FE_8BIT_SUPPORT*/
127
128 /*
129  * Device configuration flags.
130  */
131
132 /* DLCR6 settings.  */
133 #define FE_FLAGS_DLCR6_VALUE    0x007F
134
135 /* Force DLCR6 override.  */
136 #define FE_FLAGS_OVERRIDE_DLCR6 0x0080
137
138
139 devclass_t fe_devclass;
140
141 /*
142  * Special filter values.
143  */
144 static struct fe_filter const fe_filter_nothing = { FE_FILTER_NOTHING };
145 static struct fe_filter const fe_filter_all     = { FE_FILTER_ALL };
146
147 /* Standard driver entry points.  These can be static.  */
148 static void             fe_init         (void *);
149 static inthand2_t       fe_intr;
150 static int              fe_ioctl        (struct ifnet *, u_long, caddr_t);
151 static void             fe_start        (struct ifnet *);
152 static void             fe_watchdog     (struct ifnet *);
153 static int              fe_medchange    (struct ifnet *);
154 static void             fe_medstat      (struct ifnet *, struct ifmediareq *);
155
156 /* Local functions.  Order of declaration is confused.  FIXME.  */
157 static int      fe_get_packet   ( struct fe_softc *, u_short );
158 static void     fe_tint         ( struct fe_softc *, u_char );
159 static void     fe_rint         ( struct fe_softc *, u_char );
160 static void     fe_xmit         ( struct fe_softc * );
161 static void     fe_write_mbufs  ( struct fe_softc *, struct mbuf * );
162 static void     fe_setmode      ( struct fe_softc * );
163 static void     fe_loadmar      ( struct fe_softc * );
164
165 #ifdef DIAGNOSTIC
166 static void     fe_emptybuffer  ( struct fe_softc * );
167 #endif
168
169 DECLARE_DUMMY_MODULE(if_fe);
170
171 /*
172  * Fe driver specific constants which relate to 86960/86965.
173  */
174
175 /* Interrupt masks  */
176 #define FE_TMASK ( FE_D2_COLL16 | FE_D2_TXDONE )
177 #define FE_RMASK ( FE_D3_OVRFLO | FE_D3_CRCERR \
178                  | FE_D3_ALGERR | FE_D3_SRTPKT | FE_D3_PKTRDY )
179
180 /* Maximum number of iterations for a receive interrupt.  */
181 #define FE_MAX_RECV_COUNT ( ( 65536 - 2048 * 2 ) / 64 )
182         /*
183          * Maximum size of SRAM is 65536,
184          * minimum size of transmission buffer in fe is 2x2KB,
185          * and minimum amount of received packet including headers
186          * added by the chip is 64 bytes.
187          * Hence FE_MAX_RECV_COUNT is the upper limit for number
188          * of packets in the receive buffer.
189          */
190
191 /*
192  * Miscellaneous definitions not directly related to hardware.
193  */
194
195 /* The following line must be delete when "net/if_media.h" support it.  */
196 #ifndef IFM_10_FL
197 #define IFM_10_FL       /* 13 */ IFM_10_5
198 #endif
199
200 #if 0
201 /* Mapping between media bitmap (in fe_softc.mbitmap) and ifm_media.  */
202 static int const bit2media [] = {
203                         IFM_HDX | IFM_ETHER | IFM_AUTO,
204                         IFM_HDX | IFM_ETHER | IFM_MANUAL,
205                         IFM_HDX | IFM_ETHER | IFM_10_T,
206                         IFM_HDX | IFM_ETHER | IFM_10_2,
207                         IFM_HDX | IFM_ETHER | IFM_10_5,
208                         IFM_HDX | IFM_ETHER | IFM_10_FL,
209                         IFM_FDX | IFM_ETHER | IFM_10_T,
210         /* More can be come here... */
211                         0
212 };
213 #else
214 /* Mapping between media bitmap (in fe_softc.mbitmap) and ifm_media.  */
215 static int const bit2media [] = {
216                         IFM_ETHER | IFM_AUTO,
217                         IFM_ETHER | IFM_MANUAL,
218                         IFM_ETHER | IFM_10_T,
219                         IFM_ETHER | IFM_10_2,
220                         IFM_ETHER | IFM_10_5,
221                         IFM_ETHER | IFM_10_FL,
222                         IFM_ETHER | IFM_10_T,
223         /* More can be come here... */
224                         0
225 };
226 #endif
227
228 /*
229  * Check for specific bits in specific registers have specific values.
230  * A common utility function called from various sub-probe routines.
231  */
232 int
233 fe_simple_probe (struct fe_softc const * sc,
234                  struct fe_simple_probe_struct const * sp)
235 {
236         struct fe_simple_probe_struct const *p;
237
238         for (p  = sp; p->mask != 0; p++) {
239                 if ((fe_inb(sc, p->port) & p->mask) != p->bits)
240                         return 0;
241         }
242         return 1;
243 }
244
245 /* Test if a given 6 byte value is a valid Ethernet station (MAC)
246    address.  "Vendor" is an expected vendor code (first three bytes,)
247    or a zero when nothing expected.  */
248 int
249 valid_Ether_p (u_char const * addr, unsigned vendor)
250 {
251 #ifdef FE_DEBUG
252         printf("fe?: validating %6D against %06x\n", addr, ":", vendor);
253 #endif
254
255         /* All zero is not allowed as a vendor code.  */
256         if (addr[0] == 0 && addr[1] == 0 && addr[2] == 0) return 0;
257
258         switch (vendor) {
259             case 0x000000:
260                 /* Legal Ethernet address (stored in ROM) must have
261                    its Group and Local bits cleared.  */
262                 if ((addr[0] & 0x03) != 0) return 0;
263                 break;
264             case 0x020000:
265                 /* Same as above, but a local address is allowed in
266                    this context.  */
267                 if ((addr[0] & 0x01) != 0) return 0;
268                 break;
269             default:
270                 /* Make sure the vendor part matches if one is given.  */
271                 if (   addr[0] != ((vendor >> 16) & 0xFF)
272                     || addr[1] != ((vendor >>  8) & 0xFF)
273                     || addr[2] != ((vendor      ) & 0xFF)) return 0;
274                 break;
275         }
276
277         /* Host part must not be all-zeros nor all-ones.  */
278         if (addr[3] == 0xFF && addr[4] == 0xFF && addr[5] == 0xFF) return 0;
279         if (addr[3] == 0x00 && addr[4] == 0x00 && addr[5] == 0x00) return 0;
280
281         /* Given addr looks like an Ethernet address.  */
282         return 1;
283 }
284
285 /* Fill our softc struct with default value.  */
286 void
287 fe_softc_defaults (struct fe_softc *sc)
288 {
289         /* Prepare for typical register prototypes.  We assume a
290            "typical" board has <32KB> of <fast> SRAM connected with a
291            <byte-wide> data lines.  */
292         sc->proto_dlcr4 = FE_D4_LBC_DISABLE | FE_D4_CNTRL;
293         sc->proto_dlcr5 = 0;
294         sc->proto_dlcr6 = FE_D6_BUFSIZ_32KB | FE_D6_TXBSIZ_2x4KB
295                 | FE_D6_BBW_BYTE | FE_D6_SBW_WORD | FE_D6_SRAM_100ns;
296         sc->proto_dlcr7 = FE_D7_BYTSWP_LH;
297         sc->proto_bmpr13 = 0;
298
299         /* Assume the probe process (to be done later) is stable.  */
300         sc->stability = 0;
301
302         /* A typical board needs no hooks.  */
303         sc->init = NULL;
304         sc->stop = NULL;
305
306         /* Assume the board has no software-controllable media selection.  */
307         sc->mbitmap = MB_HM;
308         sc->defmedia = MB_HM;
309         sc->msel = NULL;
310 }
311
312 /* Common error reporting routine used in probe routines for
313    "soft configured IRQ"-type boards.  */
314 void
315 fe_irq_failure (char const *name, int unit, int irq, char const *list)
316 {
317         printf("fe%d: %s board is detected, but %s IRQ was given\n",
318                unit, name, (irq == NO_IRQ ? "no" : "invalid"));
319         if (list != NULL) {
320                 printf("fe%d: specify an IRQ from %s in kernel config\n",
321                        unit, list);
322         }
323 }
324
325 /*
326  * Hardware (vendor) specific hooks.
327  */
328
329 /*
330  * Generic media selection scheme for MB86965 based boards.
331  */
332 void
333 fe_msel_965 (struct fe_softc *sc)
334 {
335         u_char b13;
336
337         /* Find the appropriate bits for BMPR13 tranceiver control.  */
338         switch (IFM_SUBTYPE(sc->media.ifm_media)) {
339             case IFM_AUTO: b13 = FE_B13_PORT_AUTO | FE_B13_TPTYPE_UTP; break;
340             case IFM_10_T: b13 = FE_B13_PORT_TP   | FE_B13_TPTYPE_UTP; break;
341             default:       b13 = FE_B13_PORT_AUI;  break;
342         }
343
344         /* Write it into the register.  It takes effect immediately.  */
345         fe_outb(sc, FE_BMPR13, sc->proto_bmpr13 | b13);
346 }
347
348
349 /*
350  * Fujitsu MB86965 JLI mode support routines.
351  */
352
353 /*
354  * Routines to read all bytes from the config EEPROM through MB86965A.
355  * It is a MicroWire (3-wire) serial EEPROM with 6-bit address.
356  * (93C06 or 93C46.)
357  */
358 static void
359 fe_strobe_eeprom_jli (struct fe_softc *sc, u_short bmpr16)
360 {
361         /*
362          * We must guarantee 1us (or more) interval to access slow
363          * EEPROMs.  The following redundant code provides enough
364          * delay with ISA timing.  (Even if the bus clock is "tuned.")
365          * Some modification will be needed on faster busses.
366          */
367         fe_outb(sc, bmpr16, FE_B16_SELECT);
368         fe_outb(sc, bmpr16, FE_B16_SELECT | FE_B16_CLOCK);
369         fe_outb(sc, bmpr16, FE_B16_SELECT | FE_B16_CLOCK);
370         fe_outb(sc, bmpr16, FE_B16_SELECT);
371 }
372
373 void
374 fe_read_eeprom_jli (struct fe_softc * sc, u_char * data)
375 {
376         u_char n, val, bit;
377         u_char save16, save17;
378
379         /* Save the current value of the EEPROM interface registers.  */
380         save16 = fe_inb(sc, FE_BMPR16);
381         save17 = fe_inb(sc, FE_BMPR17);
382
383         /* Read bytes from EEPROM; two bytes per an iteration.  */
384         for (n = 0; n < JLI_EEPROM_SIZE / 2; n++) {
385
386                 /* Reset the EEPROM interface.  */
387                 fe_outb(sc, FE_BMPR16, 0x00);
388                 fe_outb(sc, FE_BMPR17, 0x00);
389
390                 /* Start EEPROM access.  */
391                 fe_outb(sc, FE_BMPR16, FE_B16_SELECT);
392                 fe_outb(sc, FE_BMPR17, FE_B17_DATA);
393                 fe_strobe_eeprom_jli(sc, FE_BMPR16);
394
395                 /* Pass the iteration count as well as a READ command.  */
396                 val = 0x80 | n;
397                 for (bit = 0x80; bit != 0x00; bit >>= 1) {
398                         fe_outb(sc, FE_BMPR17, (val & bit) ? FE_B17_DATA : 0);
399                         fe_strobe_eeprom_jli(sc, FE_BMPR16);
400                 }
401                 fe_outb(sc, FE_BMPR17, 0x00);
402
403                 /* Read a byte.  */
404                 val = 0;
405                 for (bit = 0x80; bit != 0x00; bit >>= 1) {
406                         fe_strobe_eeprom_jli(sc, FE_BMPR16);
407                         if (fe_inb(sc, FE_BMPR17) & FE_B17_DATA)
408                                 val |= bit;
409                 }
410                 *data++ = val;
411
412                 /* Read one more byte.  */
413                 val = 0;
414                 for (bit = 0x80; bit != 0x00; bit >>= 1) {
415                         fe_strobe_eeprom_jli(sc, FE_BMPR16);
416                         if (fe_inb(sc, FE_BMPR17) & FE_B17_DATA)
417                                 val |= bit;
418                 }
419                 *data++ = val;
420         }
421
422 #if 0
423         /* Reset the EEPROM interface, again.  */
424         fe_outb(sc, FE_BMPR16, 0x00);
425         fe_outb(sc, FE_BMPR17, 0x00);
426 #else
427         /* Make sure to restore the original value of EEPROM interface
428            registers, since we are not yet sure we have MB86965A on
429            the address.  */
430         fe_outb(sc, FE_BMPR17, save17);
431         fe_outb(sc, FE_BMPR16, save16);
432 #endif
433
434 #if 1
435         /* Report what we got.  */
436         if (bootverbose) {
437                 int i;
438                 data -= JLI_EEPROM_SIZE;
439                 for (i = 0; i < JLI_EEPROM_SIZE; i += 16) {
440                         printf("fe%d: EEPROM(JLI):%3x: %16D\n",
441                                sc->sc_unit, i, data + i, " ");
442                 }
443         }
444 #endif
445 }
446
447 void
448 fe_init_jli (struct fe_softc * sc)
449 {
450         /* "Reset" by writing into a magic location.  */
451         DELAY(200);
452         fe_outb(sc, 0x1E, fe_inb(sc, 0x1E));
453         DELAY(300);
454 }
455
456
457 /*
458  * SSi 78Q8377A support routines.
459  */
460
461 /*
462  * Routines to read all bytes from the config EEPROM through 78Q8377A.
463  * It is a MicroWire (3-wire) serial EEPROM with 8-bit address.  (I.e.,
464  * 93C56 or 93C66.)
465  *
466  * As I don't have SSi manuals, (hmm, an old song again!) I'm not exactly
467  * sure the following code is correct...  It is just stolen from the
468  * C-NET(98)P2 support routine in FreeBSD(98).
469  */
470
471 void
472 fe_read_eeprom_ssi (struct fe_softc *sc, u_char *data)
473 {
474         u_char val, bit;
475         int n;
476         u_char save6, save7, save12;
477
478         /* Save the current value for the DLCR registers we are about
479            to destroy.  */
480         save6 = fe_inb(sc, FE_DLCR6);
481         save7 = fe_inb(sc, FE_DLCR7);
482
483         /* Put the 78Q8377A into a state that we can access the EEPROM.  */
484         fe_outb(sc, FE_DLCR6,
485             FE_D6_BBW_WORD | FE_D6_SBW_WORD | FE_D6_DLC_DISABLE);
486         fe_outb(sc, FE_DLCR7,
487             FE_D7_BYTSWP_LH | FE_D7_RBS_BMPR | FE_D7_RDYPNS | FE_D7_POWER_UP);
488
489         /* Save the current value for the BMPR12 register, too.  */
490         save12 = fe_inb(sc, FE_DLCR12);
491
492         /* Read bytes from EEPROM; two bytes per an iteration.  */
493         for (n = 0; n < SSI_EEPROM_SIZE / 2; n++) {
494
495                 /* Start EEPROM access  */
496                 fe_outb(sc, FE_DLCR12, SSI_EEP);
497                 fe_outb(sc, FE_DLCR12, SSI_EEP | SSI_CSL);
498
499                 /* Send the following four bits to the EEPROM in the
500                    specified order: a dummy bit, a start bit, and
501                    command bits (10) for READ.  */
502                 fe_outb(sc, FE_DLCR12, SSI_EEP | SSI_CSL                    );
503                 fe_outb(sc, FE_DLCR12, SSI_EEP | SSI_CSL | SSI_CLK          );  /* 0 */
504                 fe_outb(sc, FE_DLCR12, SSI_EEP | SSI_CSL           | SSI_DAT);
505                 fe_outb(sc, FE_DLCR12, SSI_EEP | SSI_CSL | SSI_CLK | SSI_DAT);  /* 1 */
506                 fe_outb(sc, FE_DLCR12, SSI_EEP | SSI_CSL           | SSI_DAT);
507                 fe_outb(sc, FE_DLCR12, SSI_EEP | SSI_CSL | SSI_CLK | SSI_DAT);  /* 1 */
508                 fe_outb(sc, FE_DLCR12, SSI_EEP | SSI_CSL                    );
509                 fe_outb(sc, FE_DLCR12, SSI_EEP | SSI_CSL | SSI_CLK          );  /* 0 */
510
511                 /* Pass the iteration count to the chip.  */
512                 for (bit = 0x80; bit != 0x00; bit >>= 1) {
513                     val = ( n & bit ) ? SSI_DAT : 0;
514                     fe_outb(sc, FE_DLCR12, SSI_EEP | SSI_CSL           | val);
515                     fe_outb(sc, FE_DLCR12, SSI_EEP | SSI_CSL | SSI_CLK | val);
516                 }
517
518                 /* Read a byte.  */
519                 val = 0;
520                 for (bit = 0x80; bit != 0x00; bit >>= 1) {
521                     fe_outb(sc, FE_DLCR12, SSI_EEP | SSI_CSL);
522                     fe_outb(sc, FE_DLCR12, SSI_EEP | SSI_CSL | SSI_CLK);
523                     if (fe_inb(sc, FE_DLCR12) & SSI_DIN)
524                         val |= bit;
525                 }
526                 *data++ = val;
527
528                 /* Read one more byte.  */
529                 val = 0;
530                 for (bit = 0x80; bit != 0x00; bit >>= 1) {
531                     fe_outb(sc, FE_DLCR12, SSI_EEP | SSI_CSL);
532                     fe_outb(sc, FE_DLCR12, SSI_EEP | SSI_CSL | SSI_CLK);
533                     if (fe_inb(sc, FE_DLCR12) & SSI_DIN)
534                         val |= bit;
535                 }
536                 *data++ = val;
537
538                 fe_outb(sc, FE_DLCR12, SSI_EEP);
539         }
540
541         /* Reset the EEPROM interface.  (For now.)  */
542         fe_outb(sc, FE_DLCR12, 0x00);
543
544         /* Restore the saved register values, for the case that we
545            didn't have 78Q8377A at the given address.  */
546         fe_outb(sc, FE_DLCR12, save12);
547         fe_outb(sc, FE_DLCR7, save7);
548         fe_outb(sc, FE_DLCR6, save6);
549
550 #if 1
551         /* Report what we got.  */
552         if (bootverbose) {
553                 int i;
554                 data -= SSI_EEPROM_SIZE;
555                 for (i = 0; i < SSI_EEPROM_SIZE; i += 16) {
556                         printf("fe%d: EEPROM(SSI):%3x: %16D\n",
557                                sc->sc_unit, i, data + i, " ");
558                 }
559         }
560 #endif
561 }
562
563 /*
564  * TDK/LANX boards support routines.
565  */
566
567 /* It is assumed that the CLK line is low and SDA is high (float) upon entry.  */
568 #define LNX_PH(D,K,N) \
569         ((LNX_SDA_##D | LNX_CLK_##K) << N)
570 #define LNX_CYCLE(D1,D2,D3,D4,K1,K2,K3,K4) \
571         (LNX_PH(D1,K1,0)|LNX_PH(D2,K2,8)|LNX_PH(D3,K3,16)|LNX_PH(D4,K4,24))
572
573 #define LNX_CYCLE_START LNX_CYCLE(HI,LO,LO,HI, HI,HI,LO,LO)
574 #define LNX_CYCLE_STOP  LNX_CYCLE(LO,LO,HI,HI, LO,HI,HI,LO)
575 #define LNX_CYCLE_HI    LNX_CYCLE(HI,HI,HI,HI, LO,HI,LO,LO)
576 #define LNX_CYCLE_LO    LNX_CYCLE(LO,LO,LO,HI, LO,HI,LO,LO)
577 #define LNX_CYCLE_INIT  LNX_CYCLE(LO,HI,HI,HI, LO,LO,LO,LO)
578
579 static void
580 fe_eeprom_cycle_lnx (struct fe_softc *sc, u_short reg20, u_long cycle)
581 {
582         fe_outb(sc, reg20, (cycle      ) & 0xFF);
583         DELAY(15);
584         fe_outb(sc, reg20, (cycle >>  8) & 0xFF);
585         DELAY(15);
586         fe_outb(sc, reg20, (cycle >> 16) & 0xFF);
587         DELAY(15);
588         fe_outb(sc, reg20, (cycle >> 24) & 0xFF);
589         DELAY(15);
590 }
591
592 static u_char
593 fe_eeprom_receive_lnx (struct fe_softc *sc, u_short reg20)
594 {
595         u_char dat;
596
597         fe_outb(sc, reg20, LNX_CLK_HI | LNX_SDA_FL);
598         DELAY(15);
599         dat = fe_inb(sc, reg20);
600         fe_outb(sc, reg20, LNX_CLK_LO | LNX_SDA_FL);
601         DELAY(15);
602         return (dat & LNX_SDA_IN);
603 }
604
605 void
606 fe_read_eeprom_lnx (struct fe_softc *sc, u_char *data)
607 {
608         int i;
609         u_char n, bit, val;
610         u_char save20;
611         u_short reg20 = 0x14;
612
613         save20 = fe_inb(sc, reg20);
614
615         /* NOTE: DELAY() timing constants are approximately three
616            times longer (slower) than the required minimum.  This is
617            to guarantee a reliable operation under some tough
618            conditions...  Fortunately, this routine is only called
619            during the boot phase, so the speed is less important than
620            stability.  */
621
622 #if 1
623         /* Reset the X24C01's internal state machine and put it into
624            the IDLE state.  We usually don't need this, but *if*
625            someone (e.g., probe routine of other driver) write some
626            garbage into the register at 0x14, synchronization will be
627            lost, and the normal EEPROM access protocol won't work.
628            Moreover, as there are no easy way to reset, we need a
629            _manoeuvre_ here.  (It even lacks a reset pin, so pushing
630            the RESET button on the PC doesn't help!)  */
631         fe_eeprom_cycle_lnx(sc, reg20, LNX_CYCLE_INIT);
632         for (i = 0; i < 10; i++)
633                 fe_eeprom_cycle_lnx(sc, reg20, LNX_CYCLE_START);
634         fe_eeprom_cycle_lnx(sc, reg20, LNX_CYCLE_STOP);
635         DELAY(10000);
636 #endif
637
638         /* Issue a start condition.  */
639         fe_eeprom_cycle_lnx(sc, reg20, LNX_CYCLE_START);
640
641         /* Send seven bits of the starting address (zero, in this
642            case) and a command bit for READ.  */
643         val = 0x01;
644         for (bit = 0x80; bit != 0x00; bit >>= 1) {
645                 if (val & bit) {
646                         fe_eeprom_cycle_lnx(sc, reg20, LNX_CYCLE_HI);
647                 } else {
648                         fe_eeprom_cycle_lnx(sc, reg20, LNX_CYCLE_LO);
649                 }
650         }
651
652         /* Receive an ACK bit.  */
653         if (fe_eeprom_receive_lnx(sc, reg20)) {
654                 /* ACK was not received.  EEPROM is not present (i.e.,
655                    this board was not a TDK/LANX) or not working
656                    properly.  */
657                 if (bootverbose) {
658                         printf("fe%d: no ACK received from EEPROM(LNX)\n",
659                                sc->sc_unit);
660                 }
661                 /* Clear the given buffer to indicate we could not get
662                    any info. and return.  */
663                 bzero(data, LNX_EEPROM_SIZE);
664                 goto RET;
665         }
666
667         /* Read bytes from EEPROM.  */
668         for (n = 0; n < LNX_EEPROM_SIZE; n++) {
669
670                 /* Read a byte and store it into the buffer.  */
671                 val = 0x00;
672                 for (bit = 0x80; bit != 0x00; bit >>= 1) {
673                         if (fe_eeprom_receive_lnx(sc, reg20))
674                                 val |= bit;
675                 }
676                 *data++ = val;
677
678                 /* Acknowledge if we have to read more.  */
679                 if (n < LNX_EEPROM_SIZE - 1) {
680                         fe_eeprom_cycle_lnx(sc, reg20, LNX_CYCLE_LO);
681                 }
682         }
683
684         /* Issue a STOP condition, de-activating the clock line.
685            It will be safer to keep the clock line low than to leave
686            it high.  */
687         fe_eeprom_cycle_lnx(sc, reg20, LNX_CYCLE_STOP);
688
689     RET:
690         fe_outb(sc, reg20, save20);
691         
692 #if 1
693         /* Report what we got.  */
694         if (bootverbose) {
695                 data -= LNX_EEPROM_SIZE;
696                 for (i = 0; i < LNX_EEPROM_SIZE; i += 16) {
697                         printf("fe%d: EEPROM(LNX):%3x: %16D\n",
698                                sc->sc_unit, i, data + i, " ");
699                 }
700         }
701 #endif
702 }
703
704 void
705 fe_init_lnx (struct fe_softc * sc)
706 {
707         /* Reset the 86960.  Do we need this?  FIXME.  */
708         fe_outb(sc, 0x12, 0x06);
709         DELAY(100);
710         fe_outb(sc, 0x12, 0x07);
711         DELAY(100);
712
713         /* Setup IRQ control register on the ASIC.  */
714         fe_outb(sc, 0x14, sc->priv_info);
715 }
716
717
718 /*
719  * Ungermann-Bass boards support routine.
720  */
721 void
722 fe_init_ubn (struct fe_softc * sc)
723 {
724         /* Do we need this?  FIXME.  */
725         fe_outb(sc, FE_DLCR7,
726                 sc->proto_dlcr7 | FE_D7_RBS_BMPR | FE_D7_POWER_UP);
727         fe_outb(sc, 0x18, 0x00);
728         DELAY(200);
729
730         /* Setup IRQ control register on the ASIC.  */
731         fe_outb(sc, 0x14, sc->priv_info);
732 }
733
734
735 /*
736  * Install interface into kernel networking data structures
737  */
738 int
739 fe_attach (device_t dev)
740 {
741         struct fe_softc *sc = device_get_softc(dev);
742         int flags = device_get_flags(dev);
743         int b, error;
744
745         error = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_NET,
746                                fe_intr, sc, &sc->irq_handle);
747         if (error) {
748                 fe_release_resource(dev);
749                 return ENXIO;
750         }
751
752         /*
753          * Initialize ifnet structure
754          */
755         sc->sc_if.if_softc    = sc;
756         if_initname(&(sc->sc_if), "fe", sc->sc_unit);
757         sc->sc_if.if_output   = ether_output;
758         sc->sc_if.if_start    = fe_start;
759         sc->sc_if.if_ioctl    = fe_ioctl;
760         sc->sc_if.if_watchdog = fe_watchdog;
761         sc->sc_if.if_init     = fe_init;
762         sc->sc_if.if_linkmib  = &sc->mibdata;
763         sc->sc_if.if_linkmiblen = sizeof (sc->mibdata);
764
765 #if 0 /* I'm not sure... */
766         sc->mibdata.dot3Compliance = DOT3COMPLIANCE_COLLS;
767 #endif
768
769         /*
770          * Set fixed interface flags.
771          */
772         sc->sc_if.if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
773
774 #if 1
775         /*
776          * Set maximum size of output queue, if it has not been set.
777          * It is done here as this driver may be started after the
778          * system initialization (i.e., the interface is PCMCIA.)
779          *
780          * I'm not sure this is really necessary, but, even if it is,
781          * it should be done somewhere else, e.g., in if_attach(),
782          * since it must be a common workaround for all network drivers.
783          * FIXME.
784          */
785         if (sc->sc_if.if_snd.ifq_maxlen == 0)
786                 sc->sc_if.if_snd.ifq_maxlen = ifqmaxlen;
787 #endif
788
789 #if FE_SINGLE_TRANSMISSION
790         /* Override txb config to allocate minimum.  */
791         sc->proto_dlcr6 &= ~FE_D6_TXBSIZ
792         sc->proto_dlcr6 |=  FE_D6_TXBSIZ_2x2KB;
793 #endif
794
795         /* Modify hardware config if it is requested.  */
796         if (flags & FE_FLAGS_OVERRIDE_DLCR6)
797                 sc->proto_dlcr6 = flags & FE_FLAGS_DLCR6_VALUE;
798
799         /* Find TX buffer size, based on the hardware dependent proto.  */
800         switch (sc->proto_dlcr6 & FE_D6_TXBSIZ) {
801           case FE_D6_TXBSIZ_2x2KB: sc->txb_size = 2048; break;
802           case FE_D6_TXBSIZ_2x4KB: sc->txb_size = 4096; break;
803           case FE_D6_TXBSIZ_2x8KB: sc->txb_size = 8192; break;
804           default:
805                 /* Oops, we can't work with single buffer configuration.  */
806                 if (bootverbose) {
807                         printf("fe%d: strange TXBSIZ config; fixing\n",
808                                sc->sc_unit);
809                 }
810                 sc->proto_dlcr6 &= ~FE_D6_TXBSIZ;
811                 sc->proto_dlcr6 |=  FE_D6_TXBSIZ_2x2KB;
812                 sc->txb_size = 2048;
813                 break;
814         }
815
816         /* Initialize the if_media interface.  */
817         ifmedia_init(&sc->media, 0, fe_medchange, fe_medstat);
818         for (b = 0; bit2media[b] != 0; b++) {
819                 if (sc->mbitmap & (1 << b)) {
820                         ifmedia_add(&sc->media, bit2media[b], 0, NULL);
821                 }
822         }
823         for (b = 0; bit2media[b] != 0; b++) {
824                 if (sc->defmedia & (1 << b)) {
825                         ifmedia_set(&sc->media, bit2media[b]);
826                         break;
827                 }
828         }
829 #if 0   /* Turned off; this is called later, when the interface UPs.  */
830         fe_medchange(sc);
831 #endif
832
833         /* Attach and stop the interface. */
834         ether_ifattach(&sc->sc_if, ETHER_BPF_SUPPORTED);
835         fe_stop(sc);
836   
837         /* Print additional info when attached.  */
838         device_printf(dev, "address %6D, type %s%s\n",
839                       sc->sc_enaddr, ":" , sc->typestr,
840                       (sc->proto_dlcr4 & FE_D4_DSC) ? ", full duplex" : "");
841         if (bootverbose) {
842                 int buf, txb, bbw, sbw, ram;
843
844                 buf = txb = bbw = sbw = ram = -1;
845                 switch ( sc->proto_dlcr6 & FE_D6_BUFSIZ ) {
846                   case FE_D6_BUFSIZ_8KB:  buf =  8; break;
847                   case FE_D6_BUFSIZ_16KB: buf = 16; break;
848                   case FE_D6_BUFSIZ_32KB: buf = 32; break;
849                   case FE_D6_BUFSIZ_64KB: buf = 64; break;
850                 }
851                 switch ( sc->proto_dlcr6 & FE_D6_TXBSIZ ) {
852                   case FE_D6_TXBSIZ_2x2KB: txb = 2; break;
853                   case FE_D6_TXBSIZ_2x4KB: txb = 4; break;
854                   case FE_D6_TXBSIZ_2x8KB: txb = 8; break;
855                 }
856                 switch ( sc->proto_dlcr6 & FE_D6_BBW ) {
857                   case FE_D6_BBW_BYTE: bbw =  8; break;
858                   case FE_D6_BBW_WORD: bbw = 16; break;
859                 }
860                 switch ( sc->proto_dlcr6 & FE_D6_SBW ) {
861                   case FE_D6_SBW_BYTE: sbw =  8; break;
862                   case FE_D6_SBW_WORD: sbw = 16; break;
863                 }
864                 switch ( sc->proto_dlcr6 & FE_D6_SRAM ) {
865                   case FE_D6_SRAM_100ns: ram = 100; break;
866                   case FE_D6_SRAM_150ns: ram = 150; break;
867                 }
868                 device_printf(dev, "SRAM %dKB %dbit %dns, TXB %dKBx2, %dbit I/O\n",
869                               buf, bbw, ram, txb, sbw);
870         }
871         if (sc->stability & UNSTABLE_IRQ)
872                 device_printf(dev, "warning: IRQ number may be incorrect\n");
873         if (sc->stability & UNSTABLE_MAC)
874                 device_printf(dev, "warning: above MAC address may be incorrect\n");
875         if (sc->stability & UNSTABLE_TYPE)
876                 device_printf(dev, "warning: hardware type was not validated\n");
877
878         return 0;
879 }
880
881 int
882 fe_alloc_port(device_t dev, int size)
883 {
884         struct fe_softc *sc = device_get_softc(dev);
885         struct resource *res;
886         int rid;
887
888         rid = 0;
889         res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
890                                  0ul, ~0ul, size, RF_ACTIVE);
891         if (res) {
892                 sc->port_used = size;
893                 sc->port_res = res;
894                 sc->iot = rman_get_bustag(res);
895                 sc->ioh = rman_get_bushandle(res);
896                 return (0);
897         }
898
899         return (ENOENT);
900 }
901
902 int
903 fe_alloc_irq(device_t dev, int flags)
904 {
905         struct fe_softc *sc = device_get_softc(dev);
906         struct resource *res;
907         int rid;
908
909         rid = 0;
910         res = bus_alloc_resource(dev, SYS_RES_IRQ, &rid,
911                                  0ul, ~0ul, 1, RF_ACTIVE | flags);
912         if (res) {
913                 sc->irq_res = res;
914                 return (0);
915         }
916
917         return (ENOENT);
918 }
919
920 void
921 fe_release_resource(device_t dev)
922 {
923         struct fe_softc *sc = device_get_softc(dev);
924
925         if (sc->port_res) {
926                 bus_release_resource(dev, SYS_RES_IOPORT, 0, sc->port_res);
927                 sc->port_res = NULL;
928         }
929         if (sc->irq_res) {
930                 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->irq_res);
931                 sc->irq_res = NULL;
932         }
933 }
934
935 /*
936  * Reset interface, after some (hardware) trouble is deteced.
937  */
938 static void
939 fe_reset (struct fe_softc *sc)
940 {
941         /* Record how many packets are lost by this accident.  */
942         sc->sc_if.if_oerrors += sc->txb_sched + sc->txb_count;
943         sc->mibdata.dot3StatsInternalMacTransmitErrors++;
944
945         /* Put the interface into known initial state.  */
946         fe_stop(sc);
947         if (sc->sc_if.if_flags & IFF_UP)
948                 fe_init(sc);
949 }
950
951 /*
952  * Stop everything on the interface.
953  *
954  * All buffered packets, both transmitting and receiving,
955  * if any, will be lost by stopping the interface.
956  */
957 void
958 fe_stop (struct fe_softc *sc)
959 {
960         int s;
961
962         s = splimp();
963
964         /* Disable interrupts.  */
965         fe_outb(sc, FE_DLCR2, 0x00);
966         fe_outb(sc, FE_DLCR3, 0x00);
967
968         /* Stop interface hardware.  */
969         DELAY(200);
970         fe_outb(sc, FE_DLCR6, sc->proto_dlcr6 | FE_D6_DLC_DISABLE);
971         DELAY(200);
972
973         /* Clear all interrupt status.  */
974         fe_outb(sc, FE_DLCR0, 0xFF);
975         fe_outb(sc, FE_DLCR1, 0xFF);
976
977         /* Put the chip in stand-by mode.  */
978         DELAY(200);
979         fe_outb(sc, FE_DLCR7, sc->proto_dlcr7 | FE_D7_POWER_DOWN);
980         DELAY(200);
981
982         /* Reset transmitter variables and interface flags.  */
983         sc->sc_if.if_flags &= ~(IFF_OACTIVE | IFF_RUNNING);
984         sc->sc_if.if_timer = 0;
985         sc->txb_free = sc->txb_size;
986         sc->txb_count = 0;
987         sc->txb_sched = 0;
988
989         /* MAR loading can be delayed.  */
990         sc->filter_change = 0;
991
992         /* Call a device-specific hook.  */
993         if (sc->stop)
994                 sc->stop(sc);
995
996         (void) splx(s);
997 }
998
999 /*
1000  * Device timeout/watchdog routine. Entered if the device neglects to
1001  * generate an interrupt after a transmit has been started on it.
1002  */
1003 static void
1004 fe_watchdog ( struct ifnet *ifp )
1005 {
1006         struct fe_softc *sc = (struct fe_softc *)ifp;
1007
1008         /* A "debug" message.  */
1009         printf("%s: transmission timeout (%d+%d)%s\n",
1010                ifp->if_xname, sc->txb_sched, sc->txb_count,
1011                (ifp->if_flags & IFF_UP) ? "" : " when down");
1012         if (sc->sc_if.if_opackets == 0 && sc->sc_if.if_ipackets == 0)
1013                 printf("%s: wrong IRQ setting in config?\n", ifp->if_xname);
1014         fe_reset(sc);
1015 }
1016
1017 /*
1018  * Initialize device.
1019  */
1020 static void
1021 fe_init (void * xsc)
1022 {
1023         struct fe_softc *sc = xsc;
1024         int s;
1025
1026         /* We need an address. */
1027         if (TAILQ_EMPTY(&sc->sc_if.if_addrhead)) { /* XXX unlikely */
1028 #ifdef DIAGNOSTIC
1029                 printf("fe%d: init() without any address\n", sc->sc_unit);
1030 #endif
1031                 return;
1032         }
1033
1034         /* Start initializing 86960.  */
1035         s = splimp();
1036
1037         /* Call a hook before we start initializing the chip.  */
1038         if (sc->init)
1039                 sc->init(sc);
1040
1041         /*
1042          * Make sure to disable the chip, also.
1043          * This may also help re-programming the chip after
1044          * hot insertion of PCMCIAs.
1045          */
1046         DELAY(200);
1047         fe_outb(sc, FE_DLCR6, sc->proto_dlcr6 | FE_D6_DLC_DISABLE);
1048         DELAY(200);
1049
1050         /* Power up the chip and select register bank for DLCRs.  */
1051         DELAY(200);
1052         fe_outb(sc, FE_DLCR7,
1053                 sc->proto_dlcr7 | FE_D7_RBS_DLCR | FE_D7_POWER_UP);
1054         DELAY(200);
1055
1056         /* Feed the station address.  */
1057         fe_outblk(sc, FE_DLCR8, sc->sc_enaddr, ETHER_ADDR_LEN);
1058
1059         /* Clear multicast address filter to receive nothing.  */
1060         fe_outb(sc, FE_DLCR7,
1061                 sc->proto_dlcr7 | FE_D7_RBS_MAR | FE_D7_POWER_UP);
1062         fe_outblk(sc, FE_MAR8, fe_filter_nothing.data, FE_FILTER_LEN);
1063
1064         /* Select the BMPR bank for runtime register access.  */
1065         fe_outb(sc, FE_DLCR7,
1066                 sc->proto_dlcr7 | FE_D7_RBS_BMPR | FE_D7_POWER_UP);
1067
1068         /* Initialize registers.  */
1069         fe_outb(sc, FE_DLCR0, 0xFF);    /* Clear all bits.  */
1070         fe_outb(sc, FE_DLCR1, 0xFF);    /* ditto.  */
1071         fe_outb(sc, FE_DLCR2, 0x00);
1072         fe_outb(sc, FE_DLCR3, 0x00);
1073         fe_outb(sc, FE_DLCR4, sc->proto_dlcr4);
1074         fe_outb(sc, FE_DLCR5, sc->proto_dlcr5);
1075         fe_outb(sc, FE_BMPR10, 0x00);
1076         fe_outb(sc, FE_BMPR11, FE_B11_CTRL_SKIP | FE_B11_MODE1);
1077         fe_outb(sc, FE_BMPR12, 0x00);
1078         fe_outb(sc, FE_BMPR13, sc->proto_bmpr13);
1079         fe_outb(sc, FE_BMPR14, 0x00);
1080         fe_outb(sc, FE_BMPR15, 0x00);
1081
1082         /* Enable interrupts.  */
1083         fe_outb(sc, FE_DLCR2, FE_TMASK);
1084         fe_outb(sc, FE_DLCR3, FE_RMASK);
1085
1086         /* Select requested media, just before enabling DLC.  */
1087         if (sc->msel)
1088                 sc->msel(sc);
1089
1090         /* Enable transmitter and receiver.  */
1091         DELAY(200);
1092         fe_outb(sc, FE_DLCR6, sc->proto_dlcr6 | FE_D6_DLC_ENABLE);
1093         DELAY(200);
1094
1095 #ifdef DIAGNOSTIC
1096         /*
1097          * Make sure to empty the receive buffer.
1098          *
1099          * This may be redundant, but *if* the receive buffer were full
1100          * at this point, then the driver would hang.  I have experienced
1101          * some strange hang-up just after UP.  I hope the following
1102          * code solve the problem.
1103          *
1104          * I have changed the order of hardware initialization.
1105          * I think the receive buffer cannot have any packets at this
1106          * point in this version.  The following code *must* be
1107          * redundant now.  FIXME.
1108          *
1109          * I've heard a rumore that on some PC card implementation of
1110          * 8696x, the receive buffer can have some data at this point.
1111          * The following message helps discovering the fact.  FIXME.
1112          */
1113         if (!(fe_inb(sc, FE_DLCR5) & FE_D5_BUFEMP)) {
1114                 printf("fe%d: receive buffer has some data after reset\n",
1115                        sc->sc_unit);
1116                 fe_emptybuffer(sc);
1117         }
1118
1119         /* Do we need this here?  Actually, no.  I must be paranoia.  */
1120         fe_outb(sc, FE_DLCR0, 0xFF);    /* Clear all bits.  */
1121         fe_outb(sc, FE_DLCR1, 0xFF);    /* ditto.  */
1122 #endif
1123
1124         /* Set 'running' flag, because we are now running.   */
1125         sc->sc_if.if_flags |= IFF_RUNNING;
1126
1127         /*
1128          * At this point, the interface is running properly,
1129          * except that it receives *no* packets.  we then call
1130          * fe_setmode() to tell the chip what packets to be
1131          * received, based on the if_flags and multicast group
1132          * list.  It completes the initialization process.
1133          */
1134         fe_setmode(sc);
1135
1136 #if 0
1137         /* ...and attempt to start output queued packets.  */
1138         /* TURNED OFF, because the semi-auto media prober wants to UP
1139            the interface keeping it idle.  The upper layer will soon
1140            start the interface anyway, and there are no significant
1141            delay.  */
1142         fe_start(&sc->sc_if);
1143 #endif
1144
1145         (void) splx(s);
1146 }
1147
1148 /*
1149  * This routine actually starts the transmission on the interface
1150  */
1151 static void
1152 fe_xmit (struct fe_softc *sc)
1153 {
1154         /*
1155          * Set a timer just in case we never hear from the board again.
1156          * We use longer timeout for multiple packet transmission.
1157          * I'm not sure this timer value is appropriate.  FIXME.
1158          */
1159         sc->sc_if.if_timer = 1 + sc->txb_count;
1160
1161         /* Update txb variables.  */
1162         sc->txb_sched = sc->txb_count;
1163         sc->txb_count = 0;
1164         sc->txb_free = sc->txb_size;
1165         sc->tx_excolls = 0;
1166
1167         /* Start transmitter, passing packets in TX buffer.  */
1168         fe_outb(sc, FE_BMPR10, sc->txb_sched | FE_B10_START);
1169 }
1170
1171 /*
1172  * Start output on interface.
1173  * We make two assumptions here:
1174  *  1) that the current priority is set to splimp _before_ this code
1175  *     is called *and* is returned to the appropriate priority after
1176  *     return
1177  *  2) that the IFF_OACTIVE flag is checked before this code is called
1178  *     (i.e. that the output part of the interface is idle)
1179  */
1180 void
1181 fe_start (struct ifnet *ifp)
1182 {
1183         struct fe_softc *sc = ifp->if_softc;
1184         struct mbuf *m;
1185
1186 #ifdef DIAGNOSTIC
1187         /* Just a sanity check.  */
1188         if ((sc->txb_count == 0) != (sc->txb_free == sc->txb_size)) {
1189                 /*
1190                  * Txb_count and txb_free co-works to manage the
1191                  * transmission buffer.  Txb_count keeps track of the
1192                  * used potion of the buffer, while txb_free does unused
1193                  * potion.  So, as long as the driver runs properly,
1194                  * txb_count is zero if and only if txb_free is same
1195                  * as txb_size (which represents whole buffer.)
1196                  */
1197                 printf("fe%d: inconsistent txb variables (%d, %d)\n",
1198                         sc->sc_unit, sc->txb_count, sc->txb_free);
1199                 /*
1200                  * So, what should I do, then?
1201                  *
1202                  * We now know txb_count and txb_free contradicts.  We
1203                  * cannot, however, tell which is wrong.  More
1204                  * over, we cannot peek 86960 transmission buffer or
1205                  * reset the transmission buffer.  (In fact, we can
1206                  * reset the entire interface.  I don't want to do it.)
1207                  *
1208                  * If txb_count is incorrect, leaving it as-is will cause
1209                  * sending of garbage after next interrupt.  We have to
1210                  * avoid it.  Hence, we reset the txb_count here.  If
1211                  * txb_free was incorrect, resetting txb_count just loose
1212                  * some packets.  We can live with it.
1213                  */
1214                 sc->txb_count = 0;
1215         }
1216 #endif
1217
1218         /*
1219          * First, see if there are buffered packets and an idle
1220          * transmitter - should never happen at this point.
1221          */
1222         if ((sc->txb_count > 0) && (sc->txb_sched == 0)) {
1223                 printf("fe%d: transmitter idle with %d buffered packets\n",
1224                        sc->sc_unit, sc->txb_count);
1225                 fe_xmit(sc);
1226         }
1227
1228         /*
1229          * Stop accepting more transmission packets temporarily, when
1230          * a filter change request is delayed.  Updating the MARs on
1231          * 86960 flushes the transmission buffer, so it is delayed
1232          * until all buffered transmission packets have been sent
1233          * out.
1234          */
1235         if (sc->filter_change) {
1236                 /*
1237                  * Filter change request is delayed only when the DLC is
1238                  * working.  DLC soon raise an interrupt after finishing
1239                  * the work.
1240                  */
1241                 goto indicate_active;
1242         }
1243
1244         for (;;) {
1245
1246                 /*
1247                  * See if there is room to put another packet in the buffer.
1248                  * We *could* do better job by peeking the send queue to
1249                  * know the length of the next packet.  Current version just
1250                  * tests against the worst case (i.e., longest packet).  FIXME.
1251                  *
1252                  * When adding the packet-peek feature, don't forget adding a
1253                  * test on txb_count against QUEUEING_MAX.
1254                  * There is a little chance the packet count exceeds
1255                  * the limit.  Assume transmission buffer is 8KB (2x8KB
1256                  * configuration) and an application sends a bunch of small
1257                  * (i.e., minimum packet sized) packets rapidly.  An 8KB
1258                  * buffer can hold 130 blocks of 62 bytes long...
1259                  */
1260                 if (sc->txb_free
1261                     < ETHER_MAX_LEN - ETHER_CRC_LEN + FE_DATA_LEN_LEN) {
1262                         /* No room.  */
1263                         goto indicate_active;
1264                 }
1265
1266 #if FE_SINGLE_TRANSMISSION
1267                 if (sc->txb_count > 0) {
1268                         /* Just one packet per a transmission buffer.  */
1269                         goto indicate_active;
1270                 }
1271 #endif
1272
1273                 /*
1274                  * Get the next mbuf chain for a packet to send.
1275                  */
1276                 IF_DEQUEUE(&sc->sc_if.if_snd, m);
1277                 if (m == NULL) {
1278                         /* No more packets to send.  */
1279                         goto indicate_inactive;
1280                 }
1281
1282                 /*
1283                  * Copy the mbuf chain into the transmission buffer.
1284                  * txb_* variables are updated as necessary.
1285                  */
1286                 fe_write_mbufs(sc, m);
1287
1288                 /* Start transmitter if it's idle.  */
1289                 if ((sc->txb_count > 0) && (sc->txb_sched == 0))
1290                         fe_xmit(sc);
1291
1292                 /*
1293                  * Tap off here if there is a bpf listener,
1294                  * and the device is *not* in promiscuous mode.
1295                  * (86960 receives self-generated packets if 
1296                  * and only if it is in "receive everything"
1297                  * mode.)
1298                  */
1299                 if (sc->sc_if.if_bpf &&
1300                     !(sc->sc_if.if_flags & IFF_PROMISC))
1301                         bpf_mtap(&sc->sc_if, m);
1302
1303                 m_freem(m);
1304         }
1305
1306   indicate_inactive:
1307         /*
1308          * We are using the !OACTIVE flag to indicate to
1309          * the outside world that we can accept an
1310          * additional packet rather than that the
1311          * transmitter is _actually_ active.  Indeed, the
1312          * transmitter may be active, but if we haven't
1313          * filled all the buffers with data then we still
1314          * want to accept more.
1315          */
1316         sc->sc_if.if_flags &= ~IFF_OACTIVE;
1317         return;
1318
1319   indicate_active:
1320         /*
1321          * The transmitter is active, and there are no room for
1322          * more outgoing packets in the transmission buffer.
1323          */
1324         sc->sc_if.if_flags |= IFF_OACTIVE;
1325         return;
1326 }
1327
1328 /*
1329  * Drop (skip) a packet from receive buffer in 86960 memory.
1330  */
1331 static void
1332 fe_droppacket (struct fe_softc * sc, int len)
1333 {
1334         int i;
1335
1336         /*
1337          * 86960 manual says that we have to read 8 bytes from the buffer
1338          * before skip the packets and that there must be more than 8 bytes
1339          * remaining in the buffer when issue a skip command.
1340          * Remember, we have already read 4 bytes before come here.
1341          */
1342         if (len > 12) {
1343                 /* Read 4 more bytes, and skip the rest of the packet.  */
1344 #ifdef FE_8BIT_SUPPORT
1345                 if ((sc->proto_dlcr6 & FE_D6_SBW) == FE_D6_SBW_BYTE)
1346                 {
1347                         (void) fe_inb(sc, FE_BMPR8);
1348                         (void) fe_inb(sc, FE_BMPR8);
1349                         (void) fe_inb(sc, FE_BMPR8);
1350                         (void) fe_inb(sc, FE_BMPR8);
1351                 }
1352                 else
1353 #endif
1354                 {
1355                         (void) fe_inw(sc, FE_BMPR8);
1356                         (void) fe_inw(sc, FE_BMPR8);
1357                 }
1358                 fe_outb(sc, FE_BMPR14, FE_B14_SKIP);
1359         } else {
1360                 /* We should not come here unless receiving RUNTs.  */
1361 #ifdef FE_8BIT_SUPPORT
1362                 if ((sc->proto_dlcr6 & FE_D6_SBW) == FE_D6_SBW_BYTE)
1363                 {
1364                         for (i = 0; i < len; i++)
1365                                 (void) fe_inb(sc, FE_BMPR8);
1366                 }
1367                 else
1368 #endif
1369                 {
1370                         for (i = 0; i < len; i += 2)
1371                                 (void) fe_inw(sc, FE_BMPR8);
1372                 }
1373         }
1374 }
1375
1376 #ifdef DIAGNOSTIC
1377 /*
1378  * Empty receiving buffer.
1379  */
1380 static void
1381 fe_emptybuffer (struct fe_softc * sc)
1382 {
1383         int i;
1384         u_char saved_dlcr5;
1385
1386 #ifdef FE_DEBUG
1387         printf("fe%d: emptying receive buffer\n", sc->sc_unit);
1388 #endif
1389
1390         /*
1391          * Stop receiving packets, temporarily.
1392          */
1393         saved_dlcr5 = fe_inb(sc, FE_DLCR5);
1394         fe_outb(sc, FE_DLCR5, sc->proto_dlcr5);
1395         DELAY(1300);
1396
1397         /*
1398          * When we come here, the receive buffer management may
1399          * have been broken.  So, we cannot use skip operation.
1400          * Just discard everything in the buffer.
1401          */
1402 #ifdef FE_8BIT_SUPPORT
1403         if ((sc->proto_dlcr6 & FE_D6_SBW) == FE_D6_SBW_BYTE)
1404         {
1405                 for (i = 0; i < 65536; i++) {
1406                         if (fe_inb(sc, FE_DLCR5) & FE_D5_BUFEMP)
1407                                 break;
1408                         (void) fe_inb(sc, FE_BMPR8);
1409                 }
1410         }
1411         else
1412 #endif
1413         {
1414                 for (i = 0; i < 65536; i += 2) {
1415                         if (fe_inb(sc, FE_DLCR5) & FE_D5_BUFEMP)
1416                                 break;
1417                         (void) fe_inw(sc, FE_BMPR8);
1418                 }
1419         }
1420
1421         /*
1422          * Double check.
1423          */
1424         if (fe_inb(sc, FE_DLCR5) & FE_D5_BUFEMP) {
1425                 printf("fe%d: could not empty receive buffer\n", sc->sc_unit);
1426                 /* Hmm.  What should I do if this happens?  FIXME.  */
1427         }
1428
1429         /*
1430          * Restart receiving packets.
1431          */
1432         fe_outb(sc, FE_DLCR5, saved_dlcr5);
1433 }
1434 #endif
1435
1436 /*
1437  * Transmission interrupt handler
1438  * The control flow of this function looks silly.  FIXME.
1439  */
1440 static void
1441 fe_tint (struct fe_softc * sc, u_char tstat)
1442 {
1443         int left;
1444         int col;
1445
1446         /*
1447          * Handle "excessive collision" interrupt.
1448          */
1449         if (tstat & FE_D0_COLL16) {
1450
1451                 /*
1452                  * Find how many packets (including this collided one)
1453                  * are left unsent in transmission buffer.
1454                  */
1455                 left = fe_inb(sc, FE_BMPR10);
1456                 printf("fe%d: excessive collision (%d/%d)\n",
1457                        sc->sc_unit, left, sc->txb_sched);
1458
1459                 /*
1460                  * Clear the collision flag (in 86960) here
1461                  * to avoid confusing statistics.
1462                  */
1463                 fe_outb(sc, FE_DLCR0, FE_D0_COLLID);
1464
1465                 /*
1466                  * Restart transmitter, skipping the
1467                  * collided packet.
1468                  *
1469                  * We *must* skip the packet to keep network running
1470                  * properly.  Excessive collision error is an
1471                  * indication of the network overload.  If we
1472                  * tried sending the same packet after excessive
1473                  * collision, the network would be filled with
1474                  * out-of-time packets.  Packets belonging
1475                  * to reliable transport (such as TCP) are resent
1476                  * by some upper layer.
1477                  */
1478                 fe_outb(sc, FE_BMPR11, FE_B11_CTRL_SKIP | FE_B11_MODE1);
1479
1480                 /* Update statistics.  */
1481                 sc->tx_excolls++;
1482         }
1483
1484         /*
1485          * Handle "transmission complete" interrupt.
1486          */
1487         if (tstat & FE_D0_TXDONE) {
1488
1489                 /*
1490                  * Add in total number of collisions on last
1491                  * transmission.  We also clear "collision occurred" flag
1492                  * here.
1493                  *
1494                  * 86960 has a design flaw on collision count on multiple
1495                  * packet transmission.  When we send two or more packets
1496                  * with one start command (that's what we do when the
1497                  * transmission queue is crowded), 86960 informs us number
1498                  * of collisions occurred on the last packet on the
1499                  * transmission only.  Number of collisions on previous
1500                  * packets are lost.  I have told that the fact is clearly
1501                  * stated in the Fujitsu document.
1502                  *
1503                  * I considered not to mind it seriously.  Collision
1504                  * count is not so important, anyway.  Any comments?  FIXME.
1505                  */
1506
1507                 if (fe_inb(sc, FE_DLCR0) & FE_D0_COLLID) {
1508
1509                         /* Clear collision flag.  */
1510                         fe_outb(sc, FE_DLCR0, FE_D0_COLLID);
1511
1512                         /* Extract collision count from 86960.  */
1513                         col = fe_inb(sc, FE_DLCR4);
1514                         col = (col & FE_D4_COL) >> FE_D4_COL_SHIFT;
1515                         if (col == 0) {
1516                                 /*
1517                                  * Status register indicates collisions,
1518                                  * while the collision count is zero.
1519                                  * This can happen after multiple packet
1520                                  * transmission, indicating that one or more
1521                                  * previous packet(s) had been collided.
1522                                  *
1523                                  * Since the accurate number of collisions
1524                                  * has been lost, we just guess it as 1;
1525                                  * Am I too optimistic?  FIXME.
1526                                  */
1527                                 col = 1;
1528                         }
1529                         sc->sc_if.if_collisions += col;
1530                         if (col == 1)
1531                                 sc->mibdata.dot3StatsSingleCollisionFrames++;
1532                         else
1533                                 sc->mibdata.dot3StatsMultipleCollisionFrames++;
1534                         sc->mibdata.dot3StatsCollFrequencies[col-1]++;
1535                 }
1536
1537                 /*
1538                  * Update transmission statistics.
1539                  * Be sure to reflect number of excessive collisions.
1540                  */
1541                 col = sc->tx_excolls;
1542                 sc->sc_if.if_opackets += sc->txb_sched - col;
1543                 sc->sc_if.if_oerrors += col;
1544                 sc->sc_if.if_collisions += col * 16;
1545                 sc->mibdata.dot3StatsExcessiveCollisions += col;
1546                 sc->mibdata.dot3StatsCollFrequencies[15] += col;
1547                 sc->txb_sched = 0;
1548
1549                 /*
1550                  * The transmitter is no more active.
1551                  * Reset output active flag and watchdog timer.
1552                  */
1553                 sc->sc_if.if_flags &= ~IFF_OACTIVE;
1554                 sc->sc_if.if_timer = 0;
1555
1556                 /*
1557                  * If more data is ready to transmit in the buffer, start
1558                  * transmitting them.  Otherwise keep transmitter idle,
1559                  * even if more data is queued.  This gives receive
1560                  * process a slight priority.
1561                  */
1562                 if (sc->txb_count > 0)
1563                         fe_xmit(sc);
1564         }
1565 }
1566
1567 /*
1568  * Ethernet interface receiver interrupt.
1569  */
1570 static void
1571 fe_rint (struct fe_softc * sc, u_char rstat)
1572 {
1573         u_short len;
1574         u_char status;
1575         int i;
1576
1577         /*
1578          * Update statistics if this interrupt is caused by an error.
1579          * Note that, when the system was not sufficiently fast, the
1580          * receive interrupt might not be acknowledged immediately.  If
1581          * one or more errornous frames were received before this routine
1582          * was scheduled, they are ignored, and the following error stats
1583          * give less than real values.
1584          */
1585         if (rstat & (FE_D1_OVRFLO | FE_D1_CRCERR | FE_D1_ALGERR | FE_D1_SRTPKT)) {
1586                 if (rstat & FE_D1_OVRFLO)
1587                         sc->mibdata.dot3StatsInternalMacReceiveErrors++;
1588                 if (rstat & FE_D1_CRCERR)
1589                         sc->mibdata.dot3StatsFCSErrors++;
1590                 if (rstat & FE_D1_ALGERR)
1591                         sc->mibdata.dot3StatsAlignmentErrors++;
1592 #if 0
1593                 /* The reference MAC receiver defined in 802.3
1594                    silently ignores short frames (RUNTs) without
1595                    notifying upper layer.  RFC 1650 (dot3 MIB) is
1596                    based on the 802.3, and it has no stats entry for
1597                    RUNTs...  */
1598                 if (rstat & FE_D1_SRTPKT)
1599                         sc->mibdata.dot3StatsFrameTooShorts++; /* :-) */
1600 #endif
1601                 sc->sc_if.if_ierrors++;
1602         }
1603
1604         /*
1605          * MB86960 has a flag indicating "receive queue empty."
1606          * We just loop, checking the flag, to pull out all received
1607          * packets.
1608          *
1609          * We limit the number of iterations to avoid infinite-loop.
1610          * The upper bound is set to unrealistic high value.
1611          */
1612         for (i = 0; i < FE_MAX_RECV_COUNT * 2; i++) {
1613
1614                 /* Stop the iteration if 86960 indicates no packets.  */
1615                 if (fe_inb(sc, FE_DLCR5) & FE_D5_BUFEMP)
1616                         return;
1617
1618                 /*
1619                  * Extract a receive status byte.
1620                  * As our 86960 is in 16 bit bus access mode, we have to
1621                  * use inw() to get the status byte.  The significant
1622                  * value is returned in lower 8 bits.
1623                  */
1624 #ifdef FE_8BIT_SUPPORT
1625                 if ((sc->proto_dlcr6 & FE_D6_SBW) == FE_D6_SBW_BYTE)
1626                 {
1627                         status = fe_inb(sc, FE_BMPR8);
1628                         (void) fe_inb(sc, FE_BMPR8);
1629                 }
1630                 else
1631 #endif
1632                 {
1633                         status = (u_char) fe_inw(sc, FE_BMPR8);
1634                 }       
1635
1636                 /*
1637                  * Extract the packet length.
1638                  * It is a sum of a header (14 bytes) and a payload.
1639                  * CRC has been stripped off by the 86960.
1640                  */
1641 #ifdef FE_8BIT_SUPPORT
1642                 if ((sc->proto_dlcr6 & FE_D6_SBW) == FE_D6_SBW_BYTE)
1643                 {
1644                         len  =  fe_inb(sc, FE_BMPR8);
1645                         len |= (fe_inb(sc, FE_BMPR8) << 8);
1646                 }
1647                 else
1648 #endif
1649                 {
1650                         len = fe_inw(sc, FE_BMPR8);
1651                 }
1652
1653                 /*
1654                  * AS our 86960 is programed to ignore errored frame,
1655                  * we must not see any error indication in the
1656                  * receive buffer.  So, any error condition is a
1657                  * serious error, e.g., out-of-sync of the receive
1658                  * buffer pointers.
1659                  */
1660                 if ((status & 0xF0) != 0x20 ||
1661                     len > ETHER_MAX_LEN - ETHER_CRC_LEN ||
1662                     len < ETHER_MIN_LEN - ETHER_CRC_LEN) {
1663                         printf("fe%d: RX buffer out-of-sync\n", sc->sc_unit);
1664                         sc->sc_if.if_ierrors++;
1665                         sc->mibdata.dot3StatsInternalMacReceiveErrors++;
1666                         fe_reset(sc);
1667                         return;
1668                 }
1669
1670                 /*
1671                  * Go get a packet.
1672                  */
1673                 if (fe_get_packet(sc, len) < 0) {
1674                         /*
1675                          * Negative return from fe_get_packet()
1676                          * indicates no available mbuf.  We stop
1677                          * receiving packets, even if there are more
1678                          * in the buffer.  We hope we can get more
1679                          * mbuf next time.
1680                          */
1681                         sc->sc_if.if_ierrors++;
1682                         sc->mibdata.dot3StatsMissedFrames++;
1683                         fe_droppacket(sc, len);
1684                         return;
1685                 }
1686
1687                 /* Successfully received a packet.  Update stat.  */
1688                 sc->sc_if.if_ipackets++;
1689         }
1690
1691         /* Maximum number of frames has been received.  Something
1692            strange is happening here... */
1693         printf("fe%d: unusual receive flood\n", sc->sc_unit);
1694         sc->mibdata.dot3StatsInternalMacReceiveErrors++;
1695         fe_reset(sc);
1696 }
1697
1698 /*
1699  * Ethernet interface interrupt processor
1700  */
1701 static void
1702 fe_intr (void *arg)
1703 {
1704         struct fe_softc *sc = arg;
1705         u_char tstat, rstat;
1706         int loop_count = FE_MAX_LOOP;
1707
1708         /* Loop until there are no more new interrupt conditions.  */
1709         while (loop_count-- > 0) {
1710                 /*
1711                  * Get interrupt conditions, masking unneeded flags.
1712                  */
1713                 tstat = fe_inb(sc, FE_DLCR0) & FE_TMASK;
1714                 rstat = fe_inb(sc, FE_DLCR1) & FE_RMASK;
1715                 if (tstat == 0 && rstat == 0)
1716                         return;
1717
1718                 /*
1719                  * Reset the conditions we are acknowledging.
1720                  */
1721                 fe_outb(sc, FE_DLCR0, tstat);
1722                 fe_outb(sc, FE_DLCR1, rstat);
1723
1724                 /*
1725                  * Handle transmitter interrupts.
1726                  */
1727                 if (tstat)
1728                         fe_tint(sc, tstat);
1729
1730                 /*
1731                  * Handle receiver interrupts
1732                  */
1733                 if (rstat)
1734                         fe_rint(sc, rstat);
1735
1736                 /*
1737                  * Update the multicast address filter if it is
1738                  * needed and possible.  We do it now, because
1739                  * we can make sure the transmission buffer is empty,
1740                  * and there is a good chance that the receive queue
1741                  * is empty.  It will minimize the possibility of
1742                  * packet loss.
1743                  */
1744                 if (sc->filter_change &&
1745                     sc->txb_count == 0 && sc->txb_sched == 0) {
1746                         fe_loadmar(sc);
1747                         sc->sc_if.if_flags &= ~IFF_OACTIVE;
1748                 }
1749
1750                 /*
1751                  * If it looks like the transmitter can take more data,
1752                  * attempt to start output on the interface. This is done
1753                  * after handling the receiver interrupt to give the
1754                  * receive operation priority.
1755                  *
1756                  * BTW, I'm not sure in what case the OACTIVE is on at
1757                  * this point.  Is the following test redundant?
1758                  *
1759                  * No.  This routine polls for both transmitter and
1760                  * receiver interrupts.  86960 can raise a receiver
1761                  * interrupt when the transmission buffer is full.
1762                  */
1763                 if ((sc->sc_if.if_flags & IFF_OACTIVE) == 0)
1764                         fe_start(&sc->sc_if);
1765         }
1766
1767         printf("fe%d: too many loops\n", sc->sc_unit);
1768 }
1769
1770 /*
1771  * Process an ioctl request. This code needs some work - it looks
1772  * pretty ugly.
1773  */
1774 static int
1775 fe_ioctl (struct ifnet * ifp, u_long command, caddr_t data)
1776 {
1777         struct fe_softc *sc = ifp->if_softc;
1778         struct ifreq *ifr = (struct ifreq *)data;
1779         int s, error = 0;
1780
1781         s = splimp();
1782
1783         switch (command) {
1784
1785           case SIOCSIFADDR:
1786           case SIOCGIFADDR:
1787           case SIOCSIFMTU:
1788                 /* Just an ordinary action.  */
1789                 error = ether_ioctl(ifp, command, data);
1790                 break;
1791
1792           case SIOCSIFFLAGS:
1793                 /*
1794                  * Switch interface state between "running" and
1795                  * "stopped", reflecting the UP flag.
1796                  */
1797                 if (sc->sc_if.if_flags & IFF_UP) {
1798                         if ((sc->sc_if.if_flags & IFF_RUNNING) == 0)
1799                                 fe_init(sc);
1800                 } else {
1801                         if ((sc->sc_if.if_flags & IFF_RUNNING) != 0)
1802                                 fe_stop(sc);
1803                 }
1804
1805                 /*
1806                  * Promiscuous and/or multicast flags may have changed,
1807                  * so reprogram the multicast filter and/or receive mode.
1808                  */
1809                 fe_setmode(sc);
1810
1811                 /* Done.  */
1812                 break;
1813
1814           case SIOCADDMULTI:
1815           case SIOCDELMULTI:
1816                 /*
1817                  * Multicast list has changed; set the hardware filter
1818                  * accordingly.
1819                  */
1820                 fe_setmode(sc);
1821                 break;
1822
1823           case SIOCSIFMEDIA:
1824           case SIOCGIFMEDIA:
1825                 /* Let if_media to handle these commands and to call
1826                    us back.  */
1827                 error = ifmedia_ioctl(ifp, ifr, &sc->media, command);
1828                 break;
1829
1830           default:
1831                 error = EINVAL;
1832                 break;
1833         }
1834
1835         (void) splx(s);
1836         return (error);
1837 }
1838
1839 /*
1840  * Retrieve packet from receive buffer and send to the next level up via
1841  * ether_input().
1842  * Returns 0 if success, -1 if error (i.e., mbuf allocation failure).
1843  */
1844 static int
1845 fe_get_packet (struct fe_softc * sc, u_short len)
1846 {
1847         struct ether_header *eh;
1848         struct mbuf *m;
1849
1850         /*
1851          * NFS wants the data be aligned to the word (4 byte)
1852          * boundary.  Ethernet header has 14 bytes.  There is a
1853          * 2-byte gap.
1854          */
1855 #define NFS_MAGIC_OFFSET 2
1856
1857         /*
1858          * This function assumes that an Ethernet packet fits in an
1859          * mbuf (with a cluster attached when necessary.)  On FreeBSD
1860          * 2.0 for x86, which is the primary target of this driver, an
1861          * mbuf cluster has 4096 bytes, and we are happy.  On ancient
1862          * BSDs, such as vanilla 4.3 for 386, a cluster size was 1024,
1863          * however.  If the following #error message were printed upon
1864          * compile, you need to rewrite this function.
1865          */
1866 #if ( MCLBYTES < ETHER_MAX_LEN - ETHER_CRC_LEN + NFS_MAGIC_OFFSET )
1867 #error "Too small MCLBYTES to use fe driver."
1868 #endif
1869
1870         /*
1871          * Our strategy has one more problem.  There is a policy on
1872          * mbuf cluster allocation.  It says that we must have at
1873          * least MINCLSIZE (208 bytes on FreeBSD 2.0 for x86) to
1874          * allocate a cluster.  For a packet of a size between
1875          * (MHLEN - 2) to (MINCLSIZE - 2), our code violates the rule...
1876          * On the other hand, the current code is short, simple,
1877          * and fast, however.  It does no harmful thing, just waists
1878          * some memory.  Any comments?  FIXME.
1879          */
1880
1881         /* Allocate an mbuf with packet header info.  */
1882         MGETHDR(m, M_DONTWAIT, MT_DATA);
1883         if (m == NULL)
1884                 return -1;
1885
1886         /* Attach a cluster if this packet doesn't fit in a normal mbuf.  */
1887         if (len > MHLEN - NFS_MAGIC_OFFSET) {
1888                 MCLGET(m, M_DONTWAIT);
1889                 if (!(m->m_flags & M_EXT)) {
1890                         m_freem(m);
1891                         return -1;
1892                 }
1893         }
1894
1895         /* Initialize packet header info.  */
1896         m->m_pkthdr.rcvif = &sc->sc_if;
1897         m->m_pkthdr.len = len;
1898
1899         /* Set the length of this packet.  */
1900         m->m_len = len;
1901
1902         /* The following silliness is to make NFS happy */
1903         m->m_data += NFS_MAGIC_OFFSET;
1904
1905         /* Get (actually just point to) the header part.  */
1906         eh = mtod(m, struct ether_header *);
1907
1908         /* Get a packet.  */
1909 #ifdef FE_8BIT_SUPPORT
1910         if ((sc->proto_dlcr6 & FE_D6_SBW) == FE_D6_SBW_BYTE)
1911         {
1912                 fe_insb(sc, FE_BMPR8, (u_int8_t *)eh, len);
1913         }
1914         else
1915 #endif
1916         {
1917                 fe_insw(sc, FE_BMPR8, (u_int16_t *)eh, (len + 1) >> 1);
1918         }
1919
1920         /* Strip off the Ethernet header.  */
1921         m->m_pkthdr.len -= sizeof (struct ether_header);
1922         m->m_len -= sizeof (struct ether_header);
1923         m->m_data += sizeof (struct ether_header);
1924
1925         /* Feed the packet to upper layer.  */
1926         ether_input(&sc->sc_if, eh, m);
1927         return 0;
1928 }
1929
1930 /*
1931  * Write an mbuf chain to the transmission buffer memory using 16 bit PIO.
1932  * Returns number of bytes actually written, including length word.
1933  *
1934  * If an mbuf chain is too long for an Ethernet frame, it is not sent.
1935  * Packets shorter than Ethernet minimum are legal, and we pad them
1936  * before sending out.  An exception is "partial" packets which are
1937  * shorter than mandatory Ethernet header.
1938  */
1939 static void
1940 fe_write_mbufs (struct fe_softc *sc, struct mbuf *m)
1941 {
1942         u_short length, len;
1943         struct mbuf *mp;
1944         u_char *data;
1945         u_short savebyte;       /* WARNING: Architecture dependent!  */
1946 #define NO_PENDING_BYTE 0xFFFF
1947
1948         static u_char padding [ETHER_MIN_LEN - ETHER_CRC_LEN - ETHER_HDR_LEN];
1949
1950 #ifdef DIAGNOSTIC
1951         /* First, count up the total number of bytes to copy */
1952         length = 0;
1953         for (mp = m; mp != NULL; mp = mp->m_next)
1954                 length += mp->m_len;
1955
1956         /* Check if this matches the one in the packet header.  */
1957         if (length != m->m_pkthdr.len) {
1958                 printf("fe%d: packet length mismatch? (%d/%d)\n", sc->sc_unit,
1959                        length, m->m_pkthdr.len);
1960         }
1961 #else
1962         /* Just use the length value in the packet header.  */
1963         length = m->m_pkthdr.len;
1964 #endif
1965
1966 #ifdef DIAGNOSTIC
1967         /*
1968          * Should never send big packets.  If such a packet is passed,
1969          * it should be a bug of upper layer.  We just ignore it.
1970          * ... Partial (too short) packets, neither.
1971          */
1972         if (length < ETHER_HDR_LEN ||
1973             length > ETHER_MAX_LEN - ETHER_CRC_LEN) {
1974                 printf("fe%d: got an out-of-spec packet (%u bytes) to send\n",
1975                         sc->sc_unit, length);
1976                 sc->sc_if.if_oerrors++;
1977                 sc->mibdata.dot3StatsInternalMacTransmitErrors++;
1978                 return;
1979         }
1980 #endif
1981
1982         /*
1983          * Put the length word for this frame.
1984          * Does 86960 accept odd length?  -- Yes.
1985          * Do we need to pad the length to minimum size by ourselves?
1986          * -- Generally yes.  But for (or will be) the last
1987          * packet in the transmission buffer, we can skip the
1988          * padding process.  It may gain performance slightly.  FIXME.
1989          */
1990 #ifdef FE_8BIT_SUPPORT
1991         if ((sc->proto_dlcr6 & FE_D6_SBW) == FE_D6_SBW_BYTE)
1992         {
1993                 len = max(length, ETHER_MIN_LEN - ETHER_CRC_LEN);
1994                 fe_outb(sc, FE_BMPR8,  len & 0x00ff);
1995                 fe_outb(sc, FE_BMPR8, (len & 0xff00) >> 8);
1996         }
1997         else
1998 #endif
1999         {
2000                 fe_outw(sc, FE_BMPR8,
2001                         max(length, ETHER_MIN_LEN - ETHER_CRC_LEN));
2002         }
2003
2004         /*
2005          * Update buffer status now.
2006          * Truncate the length up to an even number, since we use outw().
2007          */
2008 #ifdef FE_8BIT_SUPPORT
2009         if ((sc->proto_dlcr6 & FE_D6_SBW) != FE_D6_SBW_BYTE)
2010 #endif
2011         {
2012                 length = (length + 1) & ~1;
2013         }
2014         sc->txb_free -= FE_DATA_LEN_LEN +
2015             max(length, ETHER_MIN_LEN - ETHER_CRC_LEN);
2016         sc->txb_count++;
2017
2018         /*
2019          * Transfer the data from mbuf chain to the transmission buffer.
2020          * MB86960 seems to require that data be transferred as words, and
2021          * only words.  So that we require some extra code to patch
2022          * over odd-length mbufs.
2023          */
2024 #ifdef FE_8BIT_SUPPORT
2025         if ((sc->proto_dlcr6 & FE_D6_SBW) == FE_D6_SBW_BYTE)
2026         {
2027                 /* 8-bit cards are easy.  */
2028                 for (mp = m; mp != 0; mp = mp->m_next) {
2029                         if (mp->m_len)
2030                                 fe_outsb(sc, FE_BMPR8, mtod(mp, caddr_t),
2031                                          mp->m_len);
2032                 }
2033         }
2034         else
2035 #endif
2036         {
2037                 /* 16-bit cards are a pain.  */
2038                 savebyte = NO_PENDING_BYTE;
2039                 for (mp = m; mp != 0; mp = mp->m_next) {
2040
2041                         /* Ignore empty mbuf.  */
2042                         len = mp->m_len;
2043                         if (len == 0)
2044                                 continue;
2045
2046                         /* Find the actual data to send.  */
2047                         data = mtod(mp, caddr_t);
2048
2049                         /* Finish the last byte.  */
2050                         if (savebyte != NO_PENDING_BYTE) {
2051                                 fe_outw(sc, FE_BMPR8, savebyte | (*data << 8));
2052                                 data++;
2053                                 len--;
2054                                 savebyte = NO_PENDING_BYTE;
2055                         }
2056
2057                         /* output contiguous words */
2058                         if (len > 1) {
2059                                 fe_outsw(sc, FE_BMPR8, (u_int16_t *)data,
2060                                          len >> 1);
2061                                 data += len & ~1;
2062                                 len &= 1;
2063                         }
2064
2065                         /* Save a remaining byte, if there is one.  */
2066                         if (len > 0)
2067                                 savebyte = *data;
2068                 }
2069
2070                 /* Spit the last byte, if the length is odd.  */
2071                 if (savebyte != NO_PENDING_BYTE)
2072                         fe_outw(sc, FE_BMPR8, savebyte);
2073         }
2074
2075         /* Pad to the Ethernet minimum length, if the packet is too short.  */
2076         if (length < ETHER_MIN_LEN - ETHER_CRC_LEN) {
2077 #ifdef FE_8BIT_SUPPORT
2078                 if ((sc->proto_dlcr6 & FE_D6_SBW) == FE_D6_SBW_BYTE)
2079                 {
2080                         fe_outsb(sc, FE_BMPR8, padding,
2081                                  ETHER_MIN_LEN - ETHER_CRC_LEN - length);
2082                 }
2083                 else
2084 #endif
2085                 {
2086                         fe_outsw(sc, FE_BMPR8, (u_int16_t *)padding,
2087                                  (ETHER_MIN_LEN - ETHER_CRC_LEN - length) >> 1);
2088                 }
2089         }
2090 }
2091
2092 /*
2093  * Compute hash value for an Ethernet address
2094  */
2095 static int
2096 fe_hash ( u_char * ep )
2097 {
2098 #define FE_HASH_MAGIC_NUMBER 0xEDB88320L
2099
2100         u_long hash = 0xFFFFFFFFL;
2101         int i, j;
2102         u_char b;
2103         u_long m;
2104
2105         for ( i = ETHER_ADDR_LEN; --i >= 0; ) {
2106                 b = *ep++;
2107                 for ( j = 8; --j >= 0; ) {
2108                         m = hash;
2109                         hash >>= 1;
2110                         if ( ( m ^ b ) & 1 ) hash ^= FE_HASH_MAGIC_NUMBER;
2111                         b >>= 1;
2112                 }
2113         }
2114         return ( ( int )( hash >> 26 ) );
2115 }
2116
2117 /*
2118  * Compute the multicast address filter from the
2119  * list of multicast addresses we need to listen to.
2120  */
2121 static struct fe_filter
2122 fe_mcaf ( struct fe_softc *sc )
2123 {
2124         int index;
2125         struct fe_filter filter;
2126         struct ifmultiaddr *ifma;
2127
2128         filter = fe_filter_nothing;
2129         for (ifma = sc->arpcom.ac_if.if_multiaddrs.lh_first; ifma;
2130              ifma = ifma->ifma_link.le_next) {
2131                 if (ifma->ifma_addr->sa_family != AF_LINK)
2132                         continue;
2133                 index = fe_hash(LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
2134 #ifdef FE_DEBUG
2135                 printf("fe%d: hash(%6D) == %d\n",
2136                         sc->sc_unit, enm->enm_addrlo , ":", index);
2137 #endif
2138
2139                 filter.data[index >> 3] |= 1 << (index & 7);
2140         }
2141         return ( filter );
2142 }
2143
2144 /*
2145  * Calculate a new "multicast packet filter" and put the 86960
2146  * receiver in appropriate mode.
2147  */
2148 static void
2149 fe_setmode (struct fe_softc *sc)
2150 {
2151         int flags = sc->sc_if.if_flags;
2152
2153         /*
2154          * If the interface is not running, we postpone the update
2155          * process for receive modes and multicast address filter
2156          * until the interface is restarted.  It reduces some
2157          * complicated job on maintaining chip states.  (Earlier versions
2158          * of this driver had a bug on that point...)
2159          *
2160          * To complete the trick, fe_init() calls fe_setmode() after
2161          * restarting the interface.
2162          */
2163         if (!(flags & IFF_RUNNING))
2164                 return;
2165
2166         /*
2167          * Promiscuous mode is handled separately.
2168          */
2169         if (flags & IFF_PROMISC) {
2170                 /*
2171                  * Program 86960 to receive all packets on the segment
2172                  * including those directed to other stations.
2173                  * Multicast filter stored in MARs are ignored
2174                  * under this setting, so we don't need to update it.
2175                  *
2176                  * Promiscuous mode in FreeBSD 2 is used solely by
2177                  * BPF, and BPF only listens to valid (no error) packets.
2178                  * So, we ignore erroneous ones even in this mode.
2179                  * (Older versions of fe driver mistook the point.)
2180                  */
2181                 fe_outb(sc, FE_DLCR5,
2182                         sc->proto_dlcr5 | FE_D5_AFM0 | FE_D5_AFM1);
2183                 sc->filter_change = 0;
2184                 return;
2185         }
2186
2187         /*
2188          * Turn the chip to the normal (non-promiscuous) mode.
2189          */
2190         fe_outb(sc, FE_DLCR5, sc->proto_dlcr5 | FE_D5_AFM1);
2191
2192         /*
2193          * Find the new multicast filter value.
2194          */
2195         if (flags & IFF_ALLMULTI)
2196                 sc->filter = fe_filter_all;
2197         else
2198                 sc->filter = fe_mcaf(sc);
2199         sc->filter_change = 1;
2200
2201         /*
2202          * We have to update the multicast filter in the 86960, A.S.A.P.
2203          *
2204          * Note that the DLC (Data Link Control unit, i.e. transmitter
2205          * and receiver) must be stopped when feeding the filter, and
2206          * DLC trashes all packets in both transmission and receive
2207          * buffers when stopped.
2208          *
2209          * To reduce the packet loss, we delay the filter update
2210          * process until buffers are empty.
2211          */
2212         if (sc->txb_sched == 0 && sc->txb_count == 0 &&
2213             !(fe_inb(sc, FE_DLCR1) & FE_D1_PKTRDY)) {
2214                 /*
2215                  * Buffers are (apparently) empty.  Load
2216                  * the new filter value into MARs now.
2217                  */
2218                 fe_loadmar(sc);
2219         } else {
2220                 /*
2221                  * Buffers are not empty.  Mark that we have to update
2222                  * the MARs.  The new filter will be loaded by feintr()
2223                  * later.
2224                  */
2225         }
2226 }
2227
2228 /*
2229  * Load a new multicast address filter into MARs.
2230  *
2231  * The caller must have splimp'ed before fe_loadmar.
2232  * This function starts the DLC upon return.  So it can be called only
2233  * when the chip is working, i.e., from the driver's point of view, when
2234  * a device is RUNNING.  (I mistook the point in previous versions.)
2235  */
2236 static void
2237 fe_loadmar (struct fe_softc * sc)
2238 {
2239         /* Stop the DLC (transmitter and receiver).  */
2240         DELAY(200);
2241         fe_outb(sc, FE_DLCR6, sc->proto_dlcr6 | FE_D6_DLC_DISABLE);
2242         DELAY(200);
2243
2244         /* Select register bank 1 for MARs.  */
2245         fe_outb(sc, FE_DLCR7, sc->proto_dlcr7 | FE_D7_RBS_MAR | FE_D7_POWER_UP);
2246
2247         /* Copy filter value into the registers.  */
2248         fe_outblk(sc, FE_MAR8, sc->filter.data, FE_FILTER_LEN);
2249
2250         /* Restore the bank selection for BMPRs (i.e., runtime registers).  */
2251         fe_outb(sc, FE_DLCR7,
2252                 sc->proto_dlcr7 | FE_D7_RBS_BMPR | FE_D7_POWER_UP);
2253
2254         /* Restart the DLC.  */
2255         DELAY(200);
2256         fe_outb(sc, FE_DLCR6, sc->proto_dlcr6 | FE_D6_DLC_ENABLE);
2257         DELAY(200);
2258
2259         /* We have just updated the filter.  */
2260         sc->filter_change = 0;
2261 }
2262
2263 /* Change the media selection.  */
2264 static int
2265 fe_medchange (struct ifnet *ifp)
2266 {
2267         struct fe_softc *sc = (struct fe_softc *)ifp->if_softc;
2268
2269 #ifdef DIAGNOSTIC
2270         /* If_media should not pass any request for a media which this
2271            interface doesn't support.  */
2272         int b;
2273
2274         for (b = 0; bit2media[b] != 0; b++) {
2275                 if (bit2media[b] == sc->media.ifm_media) break;
2276         }
2277         if (((1 << b) & sc->mbitmap) == 0) {
2278                 printf("fe%d: got an unsupported media request (0x%x)\n",
2279                        sc->sc_unit, sc->media.ifm_media);
2280                 return EINVAL;
2281         }
2282 #endif
2283
2284         /* We don't actually change media when the interface is down.
2285            fe_init() will do the job, instead.  Should we also wait
2286            until the transmission buffer being empty?  Changing the
2287            media when we are sending a frame will cause two garbages
2288            on wires, one on old media and another on new.  FIXME */
2289         if (sc->sc_if.if_flags & IFF_UP) {
2290                 if (sc->msel) sc->msel(sc);
2291         }
2292
2293         return 0;
2294 }
2295
2296 /* I don't know how I can support media status callback... FIXME.  */
2297 static void
2298 fe_medstat (struct ifnet *ifp, struct ifmediareq *ifmr)
2299 {
2300         (void)ifp;
2301         (void)ifmr;
2302 }