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