]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/pc98/pc98/if_ed.c
Remove #if NFOO > 0 (it's not required in most cases) and also where it
[FreeBSD/FreeBSD.git] / sys / pc98 / pc98 / if_ed.c
1 /*
2  * Copyright (c) 1995, David Greenman
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice unmodified, this list of conditions, and the following
10  *    disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * $FreeBSD$
28  */
29
30 /*
31  * Device driver for National Semiconductor DS8390/WD83C690 based ethernet
32  *   adapters. By David Greenman, 29-April-1993
33  *
34  * Currently supports the Western Digital/SMC 8003 and 8013 series,
35  *   the SMC Elite Ultra (8216), the 3Com 3c503, the NE1000 and NE2000,
36  *   and a variety of similar clones.
37  *
38  */
39
40 /*
41  * FreeBSD(98) supports:
42  * [Novell]
43  *    Allied Telesis CenterCom LA-98-T, SIC-98
44  *    D-Link DE-298P, DE-298
45  *    ELECOM LANEED LD-BDN
46  *    ICM DT-ET-25, DT-ET-T5, IF-2766ET, IF_2711ET
47  *    IO-DATA PCLA/T, LA/T-98
48  *    MACNICA NE2098
49  *    NEC PC-9801-107,108
50  *    MELCO LPC-TJ, LPC-TS, LGY-98, LGH-98, IND-SP, IND-SS, EGY-98
51  *    PLANET SMART COM CREDITCARD/2000 PCMCIA, EN-2298
52  *    Contec C-NET(98), C-NET(98)E, C-NET(98)L, C-NET(98)E-A, C-NET(98)L-A
53  *    Networld EC/EP-98X
54  * [WD80x3]
55  *    SMC EtherEZ98
56  *
57  * Modified for FreeBSD(98) 2.2 by KATO T. of Nagoya University.
58  *
59  * LPC-T support routine was contributed by Chikun.
60  *
61  * SIC-98 support routine was derived from the code by A. Kojima of
62  * Kyoto University Microcomputer Club (KMC).
63  */
64
65 #include "ed.h"
66
67 #include <sys/param.h>
68 #include <sys/systm.h>
69 #include <sys/kernel.h>
70 #include <sys/sockio.h>
71 #include <sys/malloc.h>
72 #include <sys/mbuf.h>
73 #include <sys/socket.h>
74 #include <sys/syslog.h>
75
76 #include <sys/module.h>
77 #include <sys/bus.h>
78
79 #include <machine/bus.h>
80 #include <sys/rman.h>
81 #include <machine/resource.h>
82
83 #include <net/ethernet.h>
84 #include <net/if.h>
85 #include <net/if_arp.h>
86 #include <net/if_dl.h>
87 #include <net/if_mib.h>
88
89 #include <net/bpf.h>
90 #include "opt_bdg.h"
91 #ifdef BRIDGE
92 #include <net/bridge.h>
93 #endif
94
95 #include <machine/clock.h>
96 #include <machine/md_var.h>
97
98 #include <i386/isa/isa_device.h>
99 #include <i386/isa/icu.h>
100 #include <dev/ed/if_edreg.h>
101
102 #ifdef PC98
103 /* register offsets */
104 struct pc98_edregister {
105         u_short *port;
106         u_short asic_offset;
107         u_short data;
108         u_short reset;
109 };
110 #endif
111
112 /*
113  * ed_softc: per line info and status
114  */
115 struct ed_softc {
116         struct arpcom arpcom;   /* ethernet common */
117
118         char   *type_str;       /* pointer to type string */
119         u_char  vendor;         /* interface vendor */
120         u_char  type;           /* interface type code */
121         u_char  gone;           /* HW missing, presumed having a good time */
122
123         u_short asic_addr;      /* ASIC I/O bus address */
124         u_short nic_addr;       /* NIC (DS8390) I/O bus address */
125
126 /*
127  * The following 'proto' variable is part of a work-around for 8013EBT asics
128  *      being write-only. It's sort of a prototype/shadow of the real thing.
129  */
130         u_char  wd_laar_proto;
131         u_char  cr_proto;
132         u_char  isa16bit;       /* width of access to card 0=8 or 1=16 */
133         int     is790;          /* set by the probe code if the card is 790
134                                  * based */
135
136 /*
137  * HP PC LAN PLUS card support.
138  */
139
140         u_short hpp_options;    /* flags controlling behaviour of the HP card */
141         u_short hpp_id;         /* software revision and other fields */
142         caddr_t hpp_mem_start;  /* Memory-mapped IO register address */
143
144         caddr_t mem_start;      /* NIC memory start address */
145         caddr_t mem_end;        /* NIC memory end address */
146         u_long  mem_size;       /* total NIC memory size */
147         caddr_t mem_ring;       /* start of RX ring-buffer (in NIC mem) */
148
149         u_char  mem_shared;     /* NIC memory is shared with host */
150         u_char  xmit_busy;      /* transmitter is busy */
151         u_char  txb_cnt;        /* number of transmit buffers */
152         u_char  txb_inuse;      /* number of TX buffers currently in-use */
153
154         u_char  txb_new;        /* pointer to where new buffer will be added */
155         u_char  txb_next_tx;    /* pointer to next buffer ready to xmit */
156         u_short txb_len[8];     /* buffered xmit buffer lengths */
157         u_char  tx_page_start;  /* first page of TX buffer area */
158         u_char  rec_page_start; /* first page of RX ring-buffer */
159         u_char  rec_page_stop;  /* last page of RX ring-buffer */
160         u_char  next_packet;    /* pointer to next unread RX packet */
161         struct  ifmib_iso_8802_3 mibdata; /* stuff for network mgmt */
162 #ifdef PC98
163         struct pc98_edregister edreg;   /* I/O port register offset info */
164 #endif
165 };
166
167 static struct ed_softc ed_softc[NED];
168
169 #ifdef PC98
170 #include <pc98/pc98/if_ed98.h>  
171 #endif  
172
173 static int ed_attach            __P((struct ed_softc *, int, int));
174 static int ed_attach_isa        __P((struct isa_device *));
175
176 static void ed_init             __P((void *));
177 static ointhand2_t edintr;
178 static int ed_ioctl             __P((struct ifnet *, u_long, caddr_t));
179 static int ed_probe             __P((struct isa_device *));
180 static void ed_start            __P((struct ifnet *));
181 static void ed_reset            __P((struct ifnet *));
182 static void ed_watchdog         __P((struct ifnet *));
183
184 static void ed_stop             __P((struct ed_softc *));
185 static int ed_probe_generic8390 __P((struct ed_softc *));
186 static int ed_probe_WD80x3      __P((struct isa_device *));
187 static int ed_probe_3Com        __P((struct isa_device *));
188 static int ed_probe_Novell      __P((struct isa_device *));
189 static int ed_probe_Novell_generic __P((struct ed_softc *, int, int, int));
190 static int ed_probe_HP_pclanp   __P((struct isa_device *));
191 #ifdef PC98
192 static int ed_shm_testmem       __P((struct ed_softc *));
193 static int ed_probe_SIC98       __P((struct isa_device *));
194 static void ed_reset_CNET98     __P((int, int));
195 static void ed_winsel_CNET98    __P((struct ed_softc *, u_short));
196 static int ed_probe_CNET98      __P((struct isa_device *));
197 static int ed_probe_CNET98EL    __P((struct isa_device *));
198 #endif
199
200 #ifdef PC98
201 #include "pci.h"
202 #else
203 #define NPCI 0
204 #endif
205 #if NPCI > 0
206 void *ed_attach_NE2000_pci      __P((int, int));
207 #endif
208
209 #include "card.h"
210 #if NCARD > 0
211 static int ed_probe_pccard      __P((struct isa_device *, u_char *));
212 #endif
213
214 static void     ds_getmcaf      __P((struct ed_softc *, u_long *));
215
216 static void     ed_get_packet   __P((struct ed_softc *, char *, /* u_short */ int, int));
217
218 static __inline void    ed_rint __P((struct ed_softc *));
219 static __inline void    ed_xmit __P((struct ed_softc *));
220 static __inline char *  ed_ring_copy __P((struct ed_softc *, char *, char *,
221                                           /* u_short */ int));
222 static void     ed_hpp_set_physical_link __P((struct ed_softc *));
223 static void     ed_hpp_readmem  __P((struct ed_softc *, int, unsigned char *,
224                                     /* u_short */ int));
225 static u_short  ed_hpp_write_mbufs __P((struct ed_softc *, struct mbuf *,
226                                         int));
227
228 static void     ed_pio_readmem  __P((struct ed_softc *, int, unsigned char *,
229                                     /* u_short */ int));
230 static void     ed_pio_writemem __P((struct ed_softc *, char *,
231                                      /* u_short */ int, /* u_short */ int));
232 static u_short  ed_pio_write_mbufs __P((struct ed_softc *, struct mbuf *,
233                                         int));
234 void edintr_sc                  __P((struct ed_softc *));
235
236 static void     ed_setrcr       __P((struct ed_softc *));
237
238 static u_long   ds_crc          __P((u_char *ep));
239
240 #if NCARD > 0
241 #include <sys/select.h>
242 #include <sys/module.h>
243 #include <pccard/cardinfo.h>
244 #include <pccard/slot.h>
245
246 /*
247  *      PC-Card (PCMCIA) specific code.
248  */
249 static int      edinit          __P((struct pccard_devinfo *));
250 static void     edunload        __P((struct pccard_devinfo *));
251 static int      card_intr       __P((struct pccard_devinfo *));
252
253 PCCARD_MODULE(ed, edinit, edunload, card_intr, 0, net_imask);
254
255 /*
256  *      Initialize the device - called from Slot manager.
257  */
258 static int
259 edinit(struct pccard_devinfo *devi)
260 {
261         int i;
262         u_char  e;
263         struct ed_softc *sc = &ed_softc[devi->isahd.id_unit];
264
265         /* validate unit number. */
266         if (devi->isahd.id_unit >= NED)
267                 return(ENODEV);
268         /*
269          * Probe the device. If a value is returned, the
270          * device was found at the location.
271          */
272         sc->gone = 0;
273         if (ed_probe_pccard(&devi->isahd, devi->misc) == 0)
274                 return(ENXIO);
275         e = 0;
276         for (i = 0; i < ETHER_ADDR_LEN; ++i)
277                 e |= devi->misc[i];
278         if (e)
279                 for (i = 0; i < ETHER_ADDR_LEN; ++i)
280                         sc->arpcom.ac_enaddr[i] = devi->misc[i];
281         if (ed_attach_isa(&devi->isahd) == 0)
282                 return(ENXIO);
283
284         return(0);
285 }
286
287 /*
288  *      edunload - unload the driver and clear the table.
289  *      XXX TODO:
290  *      This is usually called when the card is ejected, but
291  *      can be caused by a modunload of a controller driver.
292  *      The idea is to reset the driver's view of the device
293  *      and ensure that any driver entry points such as
294  *      read and write do not hang.
295  */
296 static void
297 edunload(struct pccard_devinfo *devi)
298 {
299         struct ed_softc *sc = &ed_softc[devi->isahd.id_unit];
300         struct ifnet *ifp = &sc->arpcom.ac_if;
301
302         if (sc->gone) {
303                 printf("ed%d: already unloaded\n", devi->isahd.id_unit);
304                 return;
305         }
306         ifp->if_flags &= ~IFF_RUNNING;
307         if_down(ifp);
308         sc->gone = 1;
309         printf("ed%d: unload\n", devi->isahd.id_unit);
310 }
311
312 /*
313  *      card_intr - Shared interrupt called from
314  *      front end of PC-Card handler.
315  */
316 static int
317 card_intr(struct pccard_devinfo *devi)
318 {
319         edintr_sc(&ed_softc[devi->isahd.id_unit]);
320         return(1);
321 }
322 #endif /* NCARD > 0 */
323
324 struct isa_driver eddriver = {
325         ed_probe,
326         ed_attach_isa,
327         "ed",
328         1               /* We are ultra sensitive */
329 };
330
331 /*
332  * Interrupt conversion table for WD/SMC ASIC/83C584
333  * (IRQ* are defined in icu.h)
334  */
335 static unsigned short ed_intr_mask[] = {
336         IRQ9,
337         IRQ3,
338         IRQ5,
339         IRQ7,
340         IRQ10,
341         IRQ11,
342         IRQ15,
343         IRQ4
344 };
345
346 /*
347  * Interrupt conversion table for 83C790
348  */
349 static unsigned short ed_790_intr_mask[] = {
350 #ifdef PC98
351         0,
352         IRQ3,
353         IRQ5,
354         IRQ6,
355         0,
356         IRQ9,
357         IRQ12,
358         IRQ13
359 #else
360         0,
361         IRQ9,
362         IRQ3,
363         IRQ5,
364         IRQ7,
365         IRQ10,
366         IRQ11,
367         IRQ15
368 #endif
369 };
370
371 /*
372  * Interrupt conversion table for the HP PC LAN+
373  */
374
375 static unsigned short ed_hpp_intr_mask[] = {
376         0,              /* 0 */
377         0,              /* 1 */
378         0,              /* 2 */
379         IRQ3,           /* 3 */
380         IRQ4,           /* 4 */
381         IRQ5,           /* 5 */
382         IRQ6,           /* 6 */
383         IRQ7,           /* 7 */
384         0,              /* 8 */
385         IRQ9,           /* 9 */
386         IRQ10,          /* 10 */
387         IRQ11,          /* 11 */
388         IRQ12,          /* 12 */
389         0,              /* 13 */
390         0,              /* 14 */
391         IRQ15           /* 15 */
392 };
393
394 #ifdef PC98
395 /*
396  * Determine if the device is present
397  *
398  *   on entry:
399  *      a pointer to an isa_device struct
400  *   on exit:
401  *      NULL if device not found
402  *      or # of i/o addresses used (if found)
403  */
404 static int
405 ed_probe(isa_dev)
406         struct isa_device *isa_dev;
407 {
408         int nports, nports98;
409         int type98 = ED_TYPE98(isa_dev->id_flags);
410
411         nports98 = pc98_set_register(isa_dev, type98);
412         switch (type98) {
413         /*
414          * Generic probe routine
415          */
416         case ED_TYPE98_GENERIC:
417                 /*
418                  * SMC EtherEZ98
419                  */
420                 nports = ed_probe_WD80x3(isa_dev);
421                 if (nports)
422                         return (nports);
423
424 #if 0           /* XXX - probably not used */
425                 nports = ed_probe_3Com(isa_dev);
426                 if (nports)
427                         return (nports);
428 #endif
429
430                 /*
431                  * Allied Telesis CenterCom LA-98-T
432                  */
433                 nports = ed_probe_Novell(isa_dev);
434                 if (nports)
435                         return (nports);
436
437 #if 0           /* XXX - PC98 has no board of this architechure */
438                 nports = ed_probe_HP_pclanp(isa_dev);
439                 if (nports)
440                         return (nports);
441 #endif
442                 break;
443
444         /*
445          * NE2000-like board probe routine
446          */
447         case ED_TYPE98_BDN:
448                 /*
449                  * ELECOM LANEED LD-BDN
450                  * PLANET SMART COM 98 EN-2298
451                  */
452         case ED_TYPE98_LGY:
453                 /*
454                  * MELCO LGY-98, IND-SP, IND-SS
455                  * MACNICA NE2098
456                  */
457         case ED_TYPE98_ICM:
458                 /*
459                  * ICM DT-ET-25, DT-ET-T5, IF-2766ET, IF-2771ET
460                  * D-Link DE-298P, DE-298
461                  */
462         case ED_TYPE98_EGY:
463                 /*
464                  * MELCO EGY-98
465                  * Contec C-NET(98)E-A, C-NET(98)L-A
466                  */
467         case ED_TYPE98_LA98:
468                 /*
469                  * IO-DATA LA/T-98
470                  */
471         case ED_TYPE98_108:
472                 /*
473                  * NEC PC-9801-107,108
474                  */
475
476                 nports = ed_probe_Novell(isa_dev);
477                 if (nports)
478                         return (nports98);
479                 break;
480
481         /*
482          * other board with special probe routine
483          */
484         case ED_TYPE98_SIC:
485                 /*
486                  * Allied Telesis SIC-98
487                  */
488                 nports = ed_probe_SIC98(isa_dev);
489                 if (nports)
490                         return (nports);
491                 break;
492
493         case ED_TYPE98_CNET98EL:
494                 /*
495                  * Contec C-NET(98)E/L
496                  */
497                 nports = ed_probe_CNET98EL(isa_dev);
498                 if (nports)
499                         return (nports);
500                 break;
501
502         case ED_TYPE98_CNET98:
503                 /*
504                  * Contec C-NET(98)
505                  */
506                 nports = ed_probe_CNET98(isa_dev);
507                 if (nports)
508                         return (nports);
509                 break;
510
511         case ED_TYPE98_NW98X:
512                 /*
513                  * Networld EC/EP-98X
514                  */
515                 nports = ed_probe_NW98X(isa_dev);
516                 if (nports)
517                         return (nports);
518                 break;
519         }
520
521         return (0);
522 }
523
524 /*
525  * Generic probe routine for testing for the existance of a DS8390.
526  *      Must be called after the NIC has just been reset. This routine
527  *      works by looking at certain register values that are guaranteed
528  *      to be initialized a certain way after power-up or reset. Seems
529  *      not to currently work on the 83C690.
530  *
531  * Specifically:
532  *
533  *      Register                        reset bits      set bits
534  *      Command Register (CR)           TXP, STA        RD2, STP
535  *      Interrupt Status (ISR)                          RST
536  *      Interrupt Mask (IMR)            All bits
537  *      Data Control (DCR)                              LAS
538  *      Transmit Config. (TCR)          LB1, LB0
539  *
540  * XXX - We only check the CR register.
541  *
542  * Return 1 if 8390 was found, 0 if not.
543  */
544
545 static int
546 ed_probe_generic8390(sc)
547         struct ed_softc *sc;
548 {
549         if ((inb(sc->nic_addr + ED_P0_CR) &
550              (ED_CR_RD2 | ED_CR_TXP | ED_CR_STA | ED_CR_STP)) !=
551             (ED_CR_RD2 | ED_CR_STP))
552                 return (0);
553         (void)inb(sc->nic_addr + ED_P0_ISR);
554
555         return (1);
556 }
557
558 #else   /* !PC98 */
559 /*
560  * Determine if the device is present
561  *
562  *   on entry:
563  *      a pointer to an isa_device struct
564  *   on exit:
565  *      NULL if device not found
566  *      or # of i/o addresses used (if found)
567  */
568 static int
569 ed_probe(isa_dev)
570         struct isa_device *isa_dev;
571 {
572         int     nports;
573
574         nports = ed_probe_WD80x3(isa_dev);
575         if (nports)
576                 return (nports);
577
578         nports = ed_probe_3Com(isa_dev);
579         if (nports)
580                 return (nports);
581
582         nports = ed_probe_Novell(isa_dev);
583         if (nports)
584                 return (nports);
585
586         nports = ed_probe_HP_pclanp(isa_dev);
587         if (nports)
588                 return (nports);
589
590         return (0);
591 }
592
593 /*
594  * Generic probe routine for testing for the existance of a DS8390.
595  *      Must be called after the NIC has just been reset. This routine
596  *      works by looking at certain register values that are guaranteed
597  *      to be initialized a certain way after power-up or reset. Seems
598  *      not to currently work on the 83C690.
599  *
600  * Specifically:
601  *
602  *      Register                        reset bits      set bits
603  *      Command Register (CR)           TXP, STA        RD2, STP
604  *      Interrupt Status (ISR)                          RST
605  *      Interrupt Mask (IMR)            All bits
606  *      Data Control (DCR)                              LAS
607  *      Transmit Config. (TCR)          LB1, LB0
608  *
609  * We only look at the CR and ISR registers, however, because looking at
610  *      the others would require changing register pages (which would be
611  *      intrusive if this isn't an 8390).
612  *
613  * Return 1 if 8390 was found, 0 if not.
614  */
615
616 static int
617 ed_probe_generic8390(sc)
618         struct ed_softc *sc;
619 {
620         if ((inb(sc->nic_addr + ED_P0_CR) &
621              (ED_CR_RD2 | ED_CR_TXP | ED_CR_STA | ED_CR_STP)) !=
622             (ED_CR_RD2 | ED_CR_STP))
623                 return (0);
624         if ((inb(sc->nic_addr + ED_P0_ISR) & ED_ISR_RST) != ED_ISR_RST)
625                 return (0);
626
627         return (1);
628 }
629 #endif  /* PC98 */
630
631 /*
632  * Probe and vendor-specific initialization routine for SMC/WD80x3 boards
633  */
634 static int
635 ed_probe_WD80x3(isa_dev)
636         struct isa_device *isa_dev;
637 {
638         struct ed_softc *sc = &ed_softc[isa_dev->id_unit];
639         int     i;
640         u_int   memsize, maddr;
641         u_char  iptr, isa16bit, sum;
642
643         sc->asic_addr = isa_dev->id_iobase;
644         sc->nic_addr = sc->asic_addr + ED_WD_NIC_OFFSET;
645         sc->is790 = 0;
646
647 #ifdef TOSH_ETHER
648         outb(sc->asic_addr + ED_WD_MSR, ED_WD_MSR_POW);
649         DELAY(10000);
650 #endif
651
652         /*
653          * Attempt to do a checksum over the station address PROM. If it
654          * fails, it's probably not a SMC/WD board. There is a problem with
655          * this, though: some clone WD boards don't pass the checksum test.
656          * Danpex boards for one.
657          */
658         for (sum = 0, i = 0; i < 8; ++i)
659                 sum += inb(sc->asic_addr + ED_WD_PROM + i);
660
661         if (sum != ED_WD_ROM_CHECKSUM_TOTAL) {
662
663                 /*
664                  * Checksum is invalid. This often happens with cheap WD8003E
665                  * clones.  In this case, the checksum byte (the eighth byte)
666                  * seems to always be zero.
667                  */
668                 if (inb(sc->asic_addr + ED_WD_CARD_ID) != ED_TYPE_WD8003E ||
669                     inb(sc->asic_addr + ED_WD_PROM + 7) != 0)
670                         return (0);
671         }
672         /* reset card to force it into a known state. */
673 #ifdef TOSH_ETHER
674         outb(sc->asic_addr + ED_WD_MSR, ED_WD_MSR_RST | ED_WD_MSR_POW);
675 #else
676         outb(sc->asic_addr + ED_WD_MSR, ED_WD_MSR_RST);
677 #endif
678         DELAY(100);
679         outb(sc->asic_addr + ED_WD_MSR, inb(sc->asic_addr + ED_WD_MSR) & ~ED_WD_MSR_RST);
680         /* wait in the case this card is reading its EEROM */
681         DELAY(5000);
682
683         sc->vendor = ED_VENDOR_WD_SMC;
684         sc->type = inb(sc->asic_addr + ED_WD_CARD_ID);
685
686         /*
687          * Set initial values for width/size.
688          */
689         memsize = 8192;
690         isa16bit = 0;
691         switch (sc->type) {
692         case ED_TYPE_WD8003S:
693                 sc->type_str = "WD8003S";
694                 break;
695         case ED_TYPE_WD8003E:
696                 sc->type_str = "WD8003E";
697                 break;
698         case ED_TYPE_WD8003EB:
699                 sc->type_str = "WD8003EB";
700                 break;
701         case ED_TYPE_WD8003W:
702                 sc->type_str = "WD8003W";
703                 break;
704         case ED_TYPE_WD8013EBT:
705                 sc->type_str = "WD8013EBT";
706                 memsize = 16384;
707                 isa16bit = 1;
708                 break;
709         case ED_TYPE_WD8013W:
710                 sc->type_str = "WD8013W";
711                 memsize = 16384;
712                 isa16bit = 1;
713                 break;
714         case ED_TYPE_WD8013EP:  /* also WD8003EP */
715                 if (inb(sc->asic_addr + ED_WD_ICR)
716                     & ED_WD_ICR_16BIT) {
717                         isa16bit = 1;
718                         memsize = 16384;
719                         sc->type_str = "WD8013EP";
720                 } else {
721                         sc->type_str = "WD8003EP";
722                 }
723                 break;
724         case ED_TYPE_WD8013WC:
725                 sc->type_str = "WD8013WC";
726                 memsize = 16384;
727                 isa16bit = 1;
728                 break;
729         case ED_TYPE_WD8013EBP:
730                 sc->type_str = "WD8013EBP";
731                 memsize = 16384;
732                 isa16bit = 1;
733                 break;
734         case ED_TYPE_WD8013EPC:
735                 sc->type_str = "WD8013EPC";
736                 memsize = 16384;
737                 isa16bit = 1;
738                 break;
739         case ED_TYPE_SMC8216C: /* 8216 has 16K shared mem -- 8416 has 8K */
740         case ED_TYPE_SMC8216T:
741                 if (sc->type == ED_TYPE_SMC8216C) {
742                         sc->type_str = "SMC8216/SMC8216C";
743                 } else {
744                         sc->type_str = "SMC8216T";
745                 }
746
747                 outb(sc->asic_addr + ED_WD790_HWR,
748                     inb(sc->asic_addr + ED_WD790_HWR) | ED_WD790_HWR_SWH);
749                 switch (inb(sc->asic_addr + ED_WD790_RAR) & ED_WD790_RAR_SZ64) {
750                 case ED_WD790_RAR_SZ64:
751                         memsize = 65536;
752                         break;
753                 case ED_WD790_RAR_SZ32:
754                         memsize = 32768;
755                         break;
756                 case ED_WD790_RAR_SZ16:
757                         memsize = 16384;
758                         break;
759                 case ED_WD790_RAR_SZ8:
760                         /* 8216 has 16K shared mem -- 8416 has 8K */
761                         if (sc->type == ED_TYPE_SMC8216C) {
762                                 sc->type_str = "SMC8416C/SMC8416BT";
763                         } else {
764                                 sc->type_str = "SMC8416T";
765                         }
766                         memsize = 8192;
767                         break;
768                 }
769                 outb(sc->asic_addr + ED_WD790_HWR,
770                     inb(sc->asic_addr + ED_WD790_HWR) & ~ED_WD790_HWR_SWH);
771
772                 isa16bit = 1;
773                 sc->is790 = 1;
774                 break;
775 #ifdef TOSH_ETHER
776         case ED_TYPE_TOSHIBA1:
777                 sc->type_str = "Toshiba1";
778                 memsize = 32768;
779                 isa16bit = 1;
780                 break;
781         case ED_TYPE_TOSHIBA4:
782                 sc->type_str = "Toshiba4";
783                 memsize = 32768;
784                 isa16bit = 1;
785                 break;
786 #endif
787         default:
788                 sc->type_str = "";
789                 break;
790         }
791
792         /*
793          * Make some adjustments to initial values depending on what is found
794          * in the ICR.
795          */
796         if (isa16bit && (sc->type != ED_TYPE_WD8013EBT)
797 #ifdef TOSH_ETHER
798           && (sc->type != ED_TYPE_TOSHIBA1) && (sc->type != ED_TYPE_TOSHIBA4)
799 #endif
800             && ((inb(sc->asic_addr + ED_WD_ICR) & ED_WD_ICR_16BIT) == 0)) {
801                 isa16bit = 0;
802                 memsize = 8192;
803         }
804
805 #if ED_DEBUG
806         printf("type = %x type_str=%s isa16bit=%d memsize=%d id_msize=%d\n",
807                sc->type, sc->type_str, isa16bit, memsize, isa_dev->id_msize);
808         for (i = 0; i < 8; i++)
809                 printf("%x -> %x\n", i, inb(sc->asic_addr + i));
810 #endif
811
812         /*
813          * Allow the user to override the autoconfiguration
814          */
815         if (isa_dev->id_msize)
816                 memsize = isa_dev->id_msize;
817
818         maddr = (u_int) isa_dev->id_maddr & 0xffffff;
819         if (maddr < 0xa0000 || maddr + memsize > 0x1000000) {
820                 printf("ed%d: Invalid ISA memory address range configured: 0x%x - 0x%x\n",
821                     isa_dev->id_unit, maddr, maddr + memsize);
822                 return 0;
823         }
824
825         /*
826          * (note that if the user specifies both of the following flags that
827          * '8bit' mode intentionally has precedence)
828          */
829         if (isa_dev->id_flags & ED_FLAGS_FORCE_16BIT_MODE)
830                 isa16bit = 1;
831         if (isa_dev->id_flags & ED_FLAGS_FORCE_8BIT_MODE)
832                 isa16bit = 0;
833
834         /*
835          * If possible, get the assigned interrupt number from the card and
836          * use it.
837          */
838         if ((sc->type & ED_WD_SOFTCONFIG) && (!sc->is790)) {
839
840                 /*
841                  * Assemble together the encoded interrupt number.
842                  */
843                 iptr = (inb(isa_dev->id_iobase + ED_WD_ICR) & ED_WD_ICR_IR2) |
844                     ((inb(isa_dev->id_iobase + ED_WD_IRR) &
845                       (ED_WD_IRR_IR0 | ED_WD_IRR_IR1)) >> 5);
846
847                 /*
848                  * If no interrupt specified (or "?"), use what the board tells us.
849                  */
850                 if (isa_dev->id_irq <= 0)
851                         isa_dev->id_irq = ed_intr_mask[iptr];
852
853                 /*
854                  * Enable the interrupt.
855                  */
856                 outb(isa_dev->id_iobase + ED_WD_IRR,
857                      inb(isa_dev->id_iobase + ED_WD_IRR) | ED_WD_IRR_IEN);
858         }
859         if (sc->is790) {
860                 outb(isa_dev->id_iobase + ED_WD790_HWR,
861                   inb(isa_dev->id_iobase + ED_WD790_HWR) | ED_WD790_HWR_SWH);
862                 iptr = (((inb(isa_dev->id_iobase + ED_WD790_GCR) & ED_WD790_GCR_IR2) >> 4) |
863                         (inb(isa_dev->id_iobase + ED_WD790_GCR) &
864                          (ED_WD790_GCR_IR1 | ED_WD790_GCR_IR0)) >> 2);
865                 outb(isa_dev->id_iobase + ED_WD790_HWR,
866                  inb(isa_dev->id_iobase + ED_WD790_HWR) & ~ED_WD790_HWR_SWH);
867
868                 /*
869                  * If no interrupt specified (or "?"), use what the board tells us.
870                  */
871                 if (isa_dev->id_irq <= 0)
872                         isa_dev->id_irq = ed_790_intr_mask[iptr];
873
874                 /*
875                  * Enable interrupts.
876                  */
877                 outb(isa_dev->id_iobase + ED_WD790_ICR,
878                   inb(isa_dev->id_iobase + ED_WD790_ICR) | ED_WD790_ICR_EIL);
879         }
880         if (isa_dev->id_irq <= 0) {
881                 printf("ed%d: %s cards don't support auto-detected/assigned interrupts.\n",
882                     isa_dev->id_unit, sc->type_str);
883                 return (0);
884         }
885         sc->isa16bit = isa16bit;
886         sc->mem_shared = 1;
887         isa_dev->id_msize = memsize;
888         sc->mem_start = (caddr_t) isa_dev->id_maddr;
889
890         /*
891          * allocate one xmit buffer if < 16k, two buffers otherwise
892          */
893         if ((memsize < 16384) ||
894             (isa_dev->id_flags & ED_FLAGS_NO_MULTI_BUFFERING)) {
895                 sc->txb_cnt = 1;
896         } else {
897                 sc->txb_cnt = 2;
898         }
899         sc->tx_page_start = ED_WD_PAGE_OFFSET;
900         sc->rec_page_start = ED_WD_PAGE_OFFSET + ED_TXBUF_SIZE * sc->txb_cnt;
901         sc->rec_page_stop = ED_WD_PAGE_OFFSET + memsize / ED_PAGE_SIZE;
902         sc->mem_ring = sc->mem_start + (ED_PAGE_SIZE * sc->rec_page_start);
903         sc->mem_size = memsize;
904         sc->mem_end = sc->mem_start + memsize;
905
906         /*
907          * Get station address from on-board ROM
908          */
909         for (i = 0; i < ETHER_ADDR_LEN; ++i)
910                 sc->arpcom.ac_enaddr[i] = inb(sc->asic_addr + ED_WD_PROM + i);
911
912         /*
913          * Set upper address bits and 8/16 bit access to shared memory.
914          */
915         if (isa16bit) {
916                 if (sc->is790) {
917                         sc->wd_laar_proto = inb(sc->asic_addr + ED_WD_LAAR);
918                 } else {
919                         sc->wd_laar_proto = ED_WD_LAAR_L16EN |
920                             ((kvtop(sc->mem_start) >> 19) & ED_WD_LAAR_ADDRHI);
921                 }
922                 /*
923                  * Enable 16bit access
924                  */
925                 outb(sc->asic_addr + ED_WD_LAAR, sc->wd_laar_proto |
926                     ED_WD_LAAR_M16EN);
927         } else {
928                 if (((sc->type & ED_WD_SOFTCONFIG) ||
929 #ifdef TOSH_ETHER
930                     (sc->type == ED_TYPE_TOSHIBA1) || (sc->type == ED_TYPE_TOSHIBA4) ||
931 #endif
932                     (sc->type == ED_TYPE_WD8013EBT)) && (!sc->is790)) {
933                         sc->wd_laar_proto = (kvtop(sc->mem_start) >> 19) &
934                             ED_WD_LAAR_ADDRHI;
935                         outb(sc->asic_addr + ED_WD_LAAR, sc->wd_laar_proto);
936                 }
937         }
938
939         /*
940          * Set address and enable interface shared memory.
941          */
942         if (!sc->is790) {
943 #ifdef TOSH_ETHER
944                 outb(sc->asic_addr + ED_WD_MSR + 1, ((kvtop(sc->mem_start) >> 8) & 0xe0) | 4);
945                 outb(sc->asic_addr + ED_WD_MSR + 2, ((kvtop(sc->mem_start) >> 16) & 0x0f));
946                 outb(sc->asic_addr + ED_WD_MSR, ED_WD_MSR_MENB | ED_WD_MSR_POW);
947
948 #else
949                 outb(sc->asic_addr + ED_WD_MSR, ((kvtop(sc->mem_start) >> 13) &
950                     ED_WD_MSR_ADDR) | ED_WD_MSR_MENB);
951 #endif
952                 sc->cr_proto = ED_CR_RD2;
953         } else {
954                 outb(sc->asic_addr + ED_WD_MSR, ED_WD_MSR_MENB);
955                 outb(sc->asic_addr + ED_WD790_HWR, (inb(sc->asic_addr + ED_WD790_HWR) | ED_WD790_HWR_SWH));
956                 outb(sc->asic_addr + ED_WD790_RAR, ((kvtop(sc->mem_start) >> 13) & 0x0f) |
957                      ((kvtop(sc->mem_start) >> 11) & 0x40) |
958                      (inb(sc->asic_addr + ED_WD790_RAR) & 0xb0));
959                 outb(sc->asic_addr + ED_WD790_HWR, (inb(sc->asic_addr + ED_WD790_HWR) & ~ED_WD790_HWR_SWH));
960                 sc->cr_proto = 0;
961         }
962
963 #if 0
964         printf("starting memory performance test at 0x%x, size %d...\n",
965                 sc->mem_start, memsize*16384);
966         for (i = 0; i < 16384; i++)
967                 bzero(sc->mem_start, memsize);
968         printf("***DONE***\n");
969 #endif
970
971         /*
972          * Now zero memory and verify that it is clear
973          */
974         bzero(sc->mem_start, memsize);
975
976         for (i = 0; i < memsize; ++i) {
977                 if (sc->mem_start[i]) {
978                         printf("ed%d: failed to clear shared memory at %lx - check configuration\n",
979                             isa_dev->id_unit, kvtop(sc->mem_start + i));
980
981                         /*
982                          * Disable 16 bit access to shared memory
983                          */
984                         if (isa16bit) {
985                                 if (sc->is790) {
986                                         outb(sc->asic_addr + ED_WD_MSR, 0x00);
987                                 }
988                                 outb(sc->asic_addr + ED_WD_LAAR, sc->wd_laar_proto &
989                                     ~ED_WD_LAAR_M16EN);
990                         }
991                         return (0);
992                 }
993         }
994
995         /*
996          * Disable 16bit access to shared memory - we leave it
997          * disabled so that 1) machines reboot properly when the board
998          * is set 16 bit mode and there are conflicting 8bit
999          * devices/ROMS in the same 128k address space as this boards
1000          * shared memory. and 2) so that other 8 bit devices with
1001          * shared memory can be used in this 128k region, too.
1002          */
1003         if (isa16bit) {
1004                 if (sc->is790) {
1005                         outb(sc->asic_addr + ED_WD_MSR, 0x00);
1006                 }
1007                 outb(sc->asic_addr + ED_WD_LAAR, sc->wd_laar_proto &
1008                     ~ED_WD_LAAR_M16EN);
1009         }
1010         return (ED_WD_IO_PORTS);
1011 }
1012
1013 /*
1014  * Probe and vendor-specific initialization routine for 3Com 3c503 boards
1015  */
1016 static int
1017 ed_probe_3Com(isa_dev)
1018         struct isa_device *isa_dev;
1019 {
1020         struct ed_softc *sc = &ed_softc[isa_dev->id_unit];
1021         int     i;
1022         u_int   memsize;
1023         u_char  isa16bit;
1024
1025         sc->asic_addr = isa_dev->id_iobase + ED_3COM_ASIC_OFFSET;
1026         sc->nic_addr = isa_dev->id_iobase + ED_3COM_NIC_OFFSET;
1027
1028         /*
1029          * Verify that the kernel configured I/O address matches the board
1030          * configured address
1031          */
1032         switch (inb(sc->asic_addr + ED_3COM_BCFR)) {
1033         case ED_3COM_BCFR_300:
1034                 if (isa_dev->id_iobase != 0x300)
1035                         return (0);
1036                 break;
1037         case ED_3COM_BCFR_310:
1038                 if (isa_dev->id_iobase != 0x310)
1039                         return (0);
1040                 break;
1041         case ED_3COM_BCFR_330:
1042                 if (isa_dev->id_iobase != 0x330)
1043                         return (0);
1044                 break;
1045         case ED_3COM_BCFR_350:
1046                 if (isa_dev->id_iobase != 0x350)
1047                         return (0);
1048                 break;
1049         case ED_3COM_BCFR_250:
1050                 if (isa_dev->id_iobase != 0x250)
1051                         return (0);
1052                 break;
1053         case ED_3COM_BCFR_280:
1054                 if (isa_dev->id_iobase != 0x280)
1055                         return (0);
1056                 break;
1057         case ED_3COM_BCFR_2A0:
1058                 if (isa_dev->id_iobase != 0x2a0)
1059                         return (0);
1060                 break;
1061         case ED_3COM_BCFR_2E0:
1062                 if (isa_dev->id_iobase != 0x2e0)
1063                         return (0);
1064                 break;
1065         default:
1066                 return (0);
1067         }
1068
1069         /*
1070          * Verify that the kernel shared memory address matches the board
1071          * configured address.
1072          */
1073         switch (inb(sc->asic_addr + ED_3COM_PCFR)) {
1074         case ED_3COM_PCFR_DC000:
1075                 if (kvtop(isa_dev->id_maddr) != 0xdc000)
1076                         return (0);
1077                 break;
1078         case ED_3COM_PCFR_D8000:
1079                 if (kvtop(isa_dev->id_maddr) != 0xd8000)
1080                         return (0);
1081                 break;
1082         case ED_3COM_PCFR_CC000:
1083                 if (kvtop(isa_dev->id_maddr) != 0xcc000)
1084                         return (0);
1085                 break;
1086         case ED_3COM_PCFR_C8000:
1087                 if (kvtop(isa_dev->id_maddr) != 0xc8000)
1088                         return (0);
1089                 break;
1090         default:
1091                 return (0);
1092         }
1093
1094
1095         /*
1096          * Reset NIC and ASIC. Enable on-board transceiver throughout reset
1097          * sequence because it'll lock up if the cable isn't connected if we
1098          * don't.
1099          */
1100         outb(sc->asic_addr + ED_3COM_CR, ED_3COM_CR_RST | ED_3COM_CR_XSEL);
1101
1102         /*
1103          * Wait for a while, then un-reset it
1104          */
1105         DELAY(50);
1106
1107         /*
1108          * The 3Com ASIC defaults to rather strange settings for the CR after
1109          * a reset - it's important to set it again after the following outb
1110          * (this is done when we map the PROM below).
1111          */
1112         outb(sc->asic_addr + ED_3COM_CR, ED_3COM_CR_XSEL);
1113
1114         /*
1115          * Wait a bit for the NIC to recover from the reset
1116          */
1117         DELAY(5000);
1118
1119         sc->vendor = ED_VENDOR_3COM;
1120         sc->type_str = "3c503";
1121         sc->mem_shared = 1;
1122         sc->cr_proto = ED_CR_RD2;
1123
1124         /*
1125          * Hmmm...a 16bit 3Com board has 16k of memory, but only an 8k window
1126          * to it.
1127          */
1128         memsize = 8192;
1129
1130         /*
1131          * Get station address from on-board ROM
1132          */
1133
1134         /*
1135          * First, map ethernet address PROM over the top of where the NIC
1136          * registers normally appear.
1137          */
1138         outb(sc->asic_addr + ED_3COM_CR, ED_3COM_CR_EALO | ED_3COM_CR_XSEL);
1139
1140         for (i = 0; i < ETHER_ADDR_LEN; ++i)
1141                 sc->arpcom.ac_enaddr[i] = inb(sc->nic_addr + i);
1142
1143         /*
1144          * Unmap PROM - select NIC registers. The proper setting of the
1145          * tranceiver is set in ed_init so that the attach code is given a
1146          * chance to set the default based on a compile-time config option
1147          */
1148         outb(sc->asic_addr + ED_3COM_CR, ED_3COM_CR_XSEL);
1149
1150         /*
1151          * Determine if this is an 8bit or 16bit board
1152          */
1153
1154         /*
1155          * select page 0 registers
1156          */
1157         outb(sc->nic_addr + ED_P0_CR, ED_CR_RD2 | ED_CR_STP);
1158
1159         /*
1160          * Attempt to clear WTS bit. If it doesn't clear, then this is a 16bit
1161          * board.
1162          */
1163         outb(sc->nic_addr + ED_P0_DCR, 0);
1164
1165         /*
1166          * select page 2 registers
1167          */
1168         outb(sc->nic_addr + ED_P0_CR, ED_CR_PAGE_2 | ED_CR_RD2 | ED_CR_STP);
1169
1170         /*
1171          * The 3c503 forces the WTS bit to a one if this is a 16bit board
1172          */
1173         if (inb(sc->nic_addr + ED_P2_DCR) & ED_DCR_WTS)
1174                 isa16bit = 1;
1175         else
1176                 isa16bit = 0;
1177
1178         /*
1179          * select page 0 registers
1180          */
1181         outb(sc->nic_addr + ED_P2_CR, ED_CR_RD2 | ED_CR_STP);
1182
1183         sc->mem_start = (caddr_t) isa_dev->id_maddr;
1184         sc->mem_size = memsize;
1185         sc->mem_end = sc->mem_start + memsize;
1186
1187         /*
1188          * We have an entire 8k window to put the transmit buffers on the
1189          * 16bit boards. But since the 16bit 3c503's shared memory is only
1190          * fast enough to overlap the loading of one full-size packet, trying
1191          * to load more than 2 buffers can actually leave the transmitter idle
1192          * during the load. So 2 seems the best value. (Although a mix of
1193          * variable-sized packets might change this assumption. Nonetheless,
1194          * we optimize for linear transfers of same-size packets.)
1195          */
1196         if (isa16bit) {
1197                 if (isa_dev->id_flags & ED_FLAGS_NO_MULTI_BUFFERING)
1198                         sc->txb_cnt = 1;
1199                 else
1200                         sc->txb_cnt = 2;
1201
1202                 sc->tx_page_start = ED_3COM_TX_PAGE_OFFSET_16BIT;
1203                 sc->rec_page_start = ED_3COM_RX_PAGE_OFFSET_16BIT;
1204                 sc->rec_page_stop = memsize / ED_PAGE_SIZE +
1205                     ED_3COM_RX_PAGE_OFFSET_16BIT;
1206                 sc->mem_ring = sc->mem_start;
1207         } else {
1208                 sc->txb_cnt = 1;
1209                 sc->tx_page_start = ED_3COM_TX_PAGE_OFFSET_8BIT;
1210                 sc->rec_page_start = ED_TXBUF_SIZE + ED_3COM_TX_PAGE_OFFSET_8BIT;
1211                 sc->rec_page_stop = memsize / ED_PAGE_SIZE +
1212                     ED_3COM_TX_PAGE_OFFSET_8BIT;
1213                 sc->mem_ring = sc->mem_start + (ED_PAGE_SIZE * ED_TXBUF_SIZE);
1214         }
1215
1216         sc->isa16bit = isa16bit;
1217
1218         /*
1219          * Initialize GA page start/stop registers. Probably only needed if
1220          * doing DMA, but what the hell.
1221          */
1222         outb(sc->asic_addr + ED_3COM_PSTR, sc->rec_page_start);
1223         outb(sc->asic_addr + ED_3COM_PSPR, sc->rec_page_stop);
1224
1225         /*
1226          * Set IRQ. 3c503 only allows a choice of irq 2-5.
1227          */
1228         switch (isa_dev->id_irq) {
1229         case IRQ2:
1230                 outb(sc->asic_addr + ED_3COM_IDCFR, ED_3COM_IDCFR_IRQ2);
1231                 break;
1232         case IRQ3:
1233                 outb(sc->asic_addr + ED_3COM_IDCFR, ED_3COM_IDCFR_IRQ3);
1234                 break;
1235         case IRQ4:
1236                 outb(sc->asic_addr + ED_3COM_IDCFR, ED_3COM_IDCFR_IRQ4);
1237                 break;
1238         case IRQ5:
1239                 outb(sc->asic_addr + ED_3COM_IDCFR, ED_3COM_IDCFR_IRQ5);
1240                 break;
1241         default:
1242                 printf("ed%d: Invalid irq configuration (%d) must be 3-5,9 for 3c503\n",
1243                        isa_dev->id_unit, ffs(isa_dev->id_irq) - 1);
1244                 return (0);
1245         }
1246
1247         /*
1248          * Initialize GA configuration register. Set bank and enable shared
1249          * mem.
1250          */
1251         outb(sc->asic_addr + ED_3COM_GACFR, ED_3COM_GACFR_RSEL |
1252              ED_3COM_GACFR_MBS0);
1253
1254         /*
1255          * Initialize "Vector Pointer" registers. These gawd-awful things are
1256          * compared to 20 bits of the address on ISA, and if they match, the
1257          * shared memory is disabled. We set them to 0xffff0...allegedly the
1258          * reset vector.
1259          */
1260         outb(sc->asic_addr + ED_3COM_VPTR2, 0xff);
1261         outb(sc->asic_addr + ED_3COM_VPTR1, 0xff);
1262         outb(sc->asic_addr + ED_3COM_VPTR0, 0x00);
1263
1264         /*
1265          * Zero memory and verify that it is clear
1266          */
1267         bzero(sc->mem_start, memsize);
1268
1269         for (i = 0; i < memsize; ++i)
1270                 if (sc->mem_start[i]) {
1271                         printf("ed%d: failed to clear shared memory at %lx - check configuration\n",
1272                                isa_dev->id_unit, kvtop(sc->mem_start + i));
1273                         return (0);
1274                 }
1275         isa_dev->id_msize = memsize;
1276         return (ED_3COM_IO_PORTS);
1277 }
1278
1279 /*
1280  * Probe and vendor-specific initialization routine for NE1000/2000 boards
1281  */
1282 static int
1283 ed_probe_Novell_generic(sc, port, unit, flags)
1284         struct ed_softc *sc;
1285         int port;
1286         int unit;
1287         int flags;
1288 {
1289         u_int   memsize, n;
1290         u_char  romdata[16], tmp;
1291         static char test_pattern[32] = "THIS is A memory TEST pattern";
1292         char    test_buffer[32];
1293
1294         sc->asic_addr = port + ED_NOVELL_ASIC_OFFSET;
1295         sc->nic_addr = port + ED_NOVELL_NIC_OFFSET;
1296
1297         /* XXX - do Novell-specific probe here */
1298
1299         /* Reset the board */
1300 #ifdef GWETHER
1301         outb(sc->asic_addr + ED_NOVELL_RESET, 0);
1302         DELAY(200);
1303 #endif  /* GWETHER */
1304         tmp = inb(sc->asic_addr + ED_NOVELL_RESET);
1305
1306         /*
1307          * I don't know if this is necessary; probably cruft leftover from
1308          * Clarkson packet driver code. Doesn't do a thing on the boards I've
1309          * tested. -DG [note that a outb(0x84, 0) seems to work here, and is
1310          * non-invasive...but some boards don't seem to reset and I don't have
1311          * complete documentation on what the 'right' thing to do is...so we
1312          * do the invasive thing for now. Yuck.]
1313          */
1314         outb(sc->asic_addr + ED_NOVELL_RESET, tmp);
1315         DELAY(5000);
1316
1317         /*
1318          * This is needed because some NE clones apparently don't reset the
1319          * NIC properly (or the NIC chip doesn't reset fully on power-up) XXX
1320          * - this makes the probe invasive! ...Done against my better
1321          * judgement. -DLG
1322          */
1323         outb(sc->nic_addr + ED_P0_CR, ED_CR_RD2 | ED_CR_STP);
1324
1325         DELAY(5000);
1326
1327         /* Make sure that we really have an 8390 based board */
1328         if (!ed_probe_generic8390(sc))
1329                 return (0);
1330
1331         sc->vendor = ED_VENDOR_NOVELL;
1332         sc->mem_shared = 0;
1333         sc->cr_proto = ED_CR_RD2;
1334
1335         /*
1336          * Test the ability to read and write to the NIC memory. This has the
1337          * side affect of determining if this is an NE1000 or an NE2000.
1338          */
1339
1340         /*
1341          * This prevents packets from being stored in the NIC memory when the
1342          * readmem routine turns on the start bit in the CR.
1343          */
1344         outb(sc->nic_addr + ED_P0_RCR, ED_RCR_MON);
1345
1346         /* Temporarily initialize DCR for byte operations */
1347         outb(sc->nic_addr + ED_P0_DCR, ED_DCR_FT1 | ED_DCR_LS);
1348
1349         outb(sc->nic_addr + ED_P0_PSTART, 8192 / ED_PAGE_SIZE);
1350         outb(sc->nic_addr + ED_P0_PSTOP, 16384 / ED_PAGE_SIZE);
1351
1352         sc->isa16bit = 0;
1353
1354         /*
1355          * Write a test pattern in byte mode. If this fails, then there
1356          * probably isn't any memory at 8k - which likely means that the board
1357          * is an NE2000.
1358          */
1359         ed_pio_writemem(sc, test_pattern, 8192, sizeof(test_pattern));
1360         ed_pio_readmem(sc, 8192, test_buffer, sizeof(test_pattern));
1361
1362         if (bcmp(test_pattern, test_buffer, sizeof(test_pattern))) {
1363                 /* not an NE1000 - try NE2000 */
1364
1365                 outb(sc->nic_addr + ED_P0_DCR, ED_DCR_WTS | ED_DCR_FT1 | ED_DCR_LS);
1366                 outb(sc->nic_addr + ED_P0_PSTART, 16384 / ED_PAGE_SIZE);
1367                 outb(sc->nic_addr + ED_P0_PSTOP, 32768 / ED_PAGE_SIZE);
1368
1369                 sc->isa16bit = 1;
1370
1371                 /*
1372                  * Write a test pattern in word mode. If this also fails, then
1373                  * we don't know what this board is.
1374                  */
1375                 ed_pio_writemem(sc, test_pattern, 16384, sizeof(test_pattern));
1376                 ed_pio_readmem(sc, 16384, test_buffer, sizeof(test_pattern));
1377
1378                 if (bcmp(test_pattern, test_buffer, sizeof(test_pattern)))
1379                         return (0);     /* not an NE2000 either */
1380
1381 #ifndef PC98
1382                 sc->type = ED_TYPE_NE2000;
1383                 sc->type_str = "NE2000";
1384         } else {
1385                 sc->type = ED_TYPE_NE1000;
1386                 sc->type_str = "NE1000";
1387 #else
1388         }
1389         switch (sc->type) {
1390         case ED_TYPE98_GENERIC:
1391                 sc->type_str = "NE2000";
1392                 break;
1393         case ED_TYPE98_PCIC98:
1394                 sc->type_str = "UE2212-PCIC98";
1395                 break;
1396         case ED_TYPE98_BDN:
1397                 sc->type_str = "LD-BDN";
1398                 break;
1399         case ED_TYPE98_EGY:
1400                 sc->type_str = "EGY-98";
1401                 break;
1402         case ED_TYPE98_LGY:
1403                 sc->type_str = "LGY-98";
1404                 break;
1405         case ED_TYPE98_ICM:
1406                 sc->type_str = "ICM";
1407                 break;
1408         case ED_TYPE98_108:
1409                 sc->type_str = "PC-9801-108";
1410                 break;
1411         case ED_TYPE98_LA98:
1412                 sc->type_str = "LA-98";
1413                 break;
1414         case ED_TYPE98_NW98X:
1415                 sc->type_str = "NW98X";
1416                 break;
1417         default:
1418                 sc->type_str = "Unknown";
1419                 break;
1420 #endif  /* PC98 */
1421         }
1422
1423         /* 8k of memory plus an additional 8k if 16bit */
1424         memsize = 8192 + sc->isa16bit * 8192;
1425
1426 #if 0   /* probably not useful - NE boards only come two ways */
1427         /* allow kernel config file overrides */
1428         if (isa_dev->id_msize)
1429                 memsize = isa_dev->id_msize;
1430 #endif
1431
1432         sc->mem_size = memsize;
1433
1434         /* NIC memory doesn't start at zero on an NE board */
1435         /* The start address is tied to the bus width */
1436         sc->mem_start = (char *) 8192 + sc->isa16bit * 8192;
1437         sc->mem_end = sc->mem_start + memsize;
1438         sc->tx_page_start = memsize / ED_PAGE_SIZE;
1439
1440 #ifdef GWETHER
1441         {
1442                 int     x, i, mstart = 0, msize = 0;
1443                 char    pbuf0[ED_PAGE_SIZE], pbuf[ED_PAGE_SIZE], tbuf[ED_PAGE_SIZE];
1444
1445                 for (i = 0; i < ED_PAGE_SIZE; i++)
1446                         pbuf0[i] = 0;
1447
1448                 /* Clear all the memory. */
1449                 for (x = 1; x < 256; x++)
1450                         ed_pio_writemem(sc, pbuf0, x * 256, ED_PAGE_SIZE);
1451
1452                 /* Search for the start of RAM. */
1453                 for (x = 1; x < 256; x++) {
1454                         ed_pio_readmem(sc, x * 256, tbuf, ED_PAGE_SIZE);
1455                         if (bcmp(pbuf0, tbuf, ED_PAGE_SIZE) == 0) {
1456                                 for (i = 0; i < ED_PAGE_SIZE; i++)
1457                                         pbuf[i] = 255 - x;
1458                                 ed_pio_writemem(sc, pbuf, x * 256, ED_PAGE_SIZE);
1459                                 ed_pio_readmem(sc, x * 256, tbuf, ED_PAGE_SIZE);
1460                                 if (bcmp(pbuf, tbuf, ED_PAGE_SIZE) == 0) {
1461                                         mstart = x * ED_PAGE_SIZE;
1462                                         msize = ED_PAGE_SIZE;
1463                                         break;
1464                                 }
1465                         }
1466                 }
1467
1468                 if (mstart == 0) {
1469                         printf("ed%d: Cannot find start of RAM.\n", unit);
1470                         return 0;
1471                 }
1472                 /* Search for the start of RAM. */
1473                 for (x = (mstart / ED_PAGE_SIZE) + 1; x < 256; x++) {
1474                         ed_pio_readmem(sc, x * 256, tbuf, ED_PAGE_SIZE);
1475                         if (bcmp(pbuf0, tbuf, ED_PAGE_SIZE) == 0) {
1476                                 for (i = 0; i < ED_PAGE_SIZE; i++)
1477                                         pbuf[i] = 255 - x;
1478                                 ed_pio_writemem(sc, pbuf, x * 256, ED_PAGE_SIZE);
1479                                 ed_pio_readmem(sc, x * 256, tbuf, ED_PAGE_SIZE);
1480                                 if (bcmp(pbuf, tbuf, ED_PAGE_SIZE) == 0)
1481                                         msize += ED_PAGE_SIZE;
1482                                 else {
1483                                         break;
1484                                 }
1485                         } else {
1486                                 break;
1487                         }
1488                 }
1489
1490                 if (msize == 0) {
1491                         printf("ed%d: Cannot find any RAM, start : %d, x = %d.\n", unit, mstart, x);
1492                         return 0;
1493                 }
1494                 printf("ed%d: RAM start at %d, size : %d.\n", unit, mstart, msize);
1495
1496                 sc->mem_size = msize;
1497                 sc->mem_start = (char *) mstart;
1498                 sc->mem_end = (char *) (msize + mstart);
1499                 sc->tx_page_start = mstart / ED_PAGE_SIZE;
1500         }
1501 #endif  /* GWETHER */
1502
1503         /*
1504          * Use one xmit buffer if < 16k, two buffers otherwise (if not told
1505          * otherwise).
1506          */
1507         if ((memsize < 16384) || (flags & ED_FLAGS_NO_MULTI_BUFFERING))
1508                 sc->txb_cnt = 1;
1509         else
1510                 sc->txb_cnt = 2;
1511
1512         sc->rec_page_start = sc->tx_page_start + sc->txb_cnt * ED_TXBUF_SIZE;
1513         sc->rec_page_stop = sc->tx_page_start + memsize / ED_PAGE_SIZE;
1514
1515         sc->mem_ring = sc->mem_start + sc->txb_cnt * ED_PAGE_SIZE * ED_TXBUF_SIZE;
1516
1517         ed_pio_readmem(sc, 0, romdata, 16);
1518         for (n = 0; n < ETHER_ADDR_LEN; n++)
1519                 sc->arpcom.ac_enaddr[n] = romdata[n * (sc->isa16bit + 1)];
1520
1521 #ifdef GWETHER
1522         if (sc->arpcom.ac_enaddr[2] == 0x86) {
1523                 sc->type_str = "Gateway AT";
1524         }
1525 #endif  /* GWETHER */
1526
1527         /* clear any pending interrupts that might have occurred above */
1528         outb(sc->nic_addr + ED_P0_ISR, 0xff);
1529
1530         return (ED_NOVELL_IO_PORTS);
1531 }
1532
1533 static int
1534 ed_probe_Novell(isa_dev)
1535         struct isa_device *isa_dev;
1536 {
1537         struct ed_softc *sc = &ed_softc[isa_dev->id_unit];
1538         int     nports;
1539
1540         nports = ed_probe_Novell_generic(sc, isa_dev->id_iobase, 
1541                                        isa_dev->id_unit, isa_dev->id_flags);
1542         if (nports)
1543                 isa_dev->id_maddr = 0;
1544
1545         return (nports);
1546 }
1547
1548 #if NCARD > 0
1549 /*
1550  * Probe framework for pccards.  Replicates the standard framework, 
1551  * minus the pccard driver registration and ignores the ether address
1552  * supplied (from the CIS), relying on the probe to find it instead.
1553  */
1554 static int
1555 ed_probe_pccard(isa_dev, ether)
1556         struct isa_device *isa_dev;
1557         u_char *ether;
1558 {
1559         int     nports;
1560 #ifdef PC98
1561         int     nports98;
1562
1563         /* NE2000 PCMCIA on old 98Note */
1564         nports98 = pc98_set_register(isa_dev, ED_TYPE98_PCIC98);
1565         nports = ed_probe_Novell(isa_dev);
1566         if (nports)
1567                 return (nports98);
1568
1569         nports98 = pc98_set_register(isa_dev, ED_TYPE98_GENERIC);
1570 #endif
1571         nports = ed_probe_WD80x3(isa_dev);
1572         if (nports)
1573                 return (nports);
1574
1575         nports = ed_probe_Novell(isa_dev);
1576         if (nports)
1577                 return (nports);
1578
1579         return (0);
1580 }
1581
1582 #endif /* NCARD > 0 */
1583
1584 #define ED_HPP_TEST_SIZE        16
1585
1586 /*
1587  * Probe and vendor specific initialization for the HP PC Lan+ Cards.
1588  * (HP Part nos: 27247B and 27252A).
1589  *
1590  * The card has an asic wrapper around a DS8390 core.  The asic handles 
1591  * host accesses and offers both standard register IO and memory mapped 
1592  * IO.  Memory mapped I/O allows better performance at the expense of greater
1593  * chance of an incompatibility with existing ISA cards.
1594  *
1595  * The card has a few caveats: it isn't tolerant of byte wide accesses, only
1596  * short (16 bit) or word (32 bit) accesses are allowed.  Some card revisions
1597  * don't allow 32 bit accesses; these are indicated by a bit in the software
1598  * ID register (see if_edreg.h).
1599  * 
1600  * Other caveats are: we should read the MAC address only when the card
1601  * is inactive.
1602  *
1603  * For more information; please consult the CRYNWR packet driver.
1604  *
1605  * The AUI port is turned on using the "link2" option on the ifconfig 
1606  * command line.
1607  */
1608 static int
1609 ed_probe_HP_pclanp(isa_dev)
1610         struct isa_device *isa_dev;
1611 {
1612         struct ed_softc *sc = &ed_softc[isa_dev->id_unit];
1613         int n;                          /* temp var */
1614         int memsize;                    /* mem on board */
1615         u_char checksum;                /* checksum of board address */
1616         u_char irq;                     /* board configured IRQ */
1617         char test_pattern[ED_HPP_TEST_SIZE];    /* read/write areas for */
1618         char test_buffer[ED_HPP_TEST_SIZE];     /* probing card */
1619
1620
1621         /* Fill in basic information */
1622         sc->asic_addr = isa_dev->id_iobase + ED_HPP_ASIC_OFFSET;
1623         sc->nic_addr = isa_dev->id_iobase + ED_HPP_NIC_OFFSET;
1624         sc->is790 = 0;
1625         sc->isa16bit = 0;       /* the 8390 core needs to be in byte mode */
1626
1627         /* 
1628          * Look for the HP PCLAN+ signature: "0x50,0x48,0x00,0x53" 
1629          */
1630         
1631         if ((inb(sc->asic_addr + ED_HPP_ID) != 0x50) || 
1632             (inb(sc->asic_addr + ED_HPP_ID + 1) != 0x48) ||
1633             ((inb(sc->asic_addr + ED_HPP_ID + 2) & 0xF0) != 0) ||
1634             (inb(sc->asic_addr + ED_HPP_ID + 3) != 0x53))
1635                 return 0;
1636
1637         /* 
1638          * Read the MAC address and verify checksum on the address.
1639          */
1640
1641         outw(sc->asic_addr + ED_HPP_PAGING, ED_HPP_PAGE_MAC);
1642         for (n  = 0, checksum = 0; n < ETHER_ADDR_LEN; n++)
1643                 checksum += (sc->arpcom.ac_enaddr[n] = 
1644                         inb(sc->asic_addr + ED_HPP_MAC_ADDR + n));
1645         
1646         checksum += inb(sc->asic_addr + ED_HPP_MAC_ADDR + ETHER_ADDR_LEN);
1647
1648         if (checksum != 0xFF)
1649                 return 0;
1650
1651         /*
1652          * Verify that the software model number is 0.
1653          */
1654         
1655         outw(sc->asic_addr + ED_HPP_PAGING, ED_HPP_PAGE_ID);
1656         if (((sc->hpp_id = inw(sc->asic_addr + ED_HPP_PAGE_4)) & 
1657                 ED_HPP_ID_SOFT_MODEL_MASK) != 0x0000)
1658                 return 0;
1659
1660         /*
1661          * Read in and save the current options configured on card.
1662          */
1663
1664         sc->hpp_options = inw(sc->asic_addr + ED_HPP_OPTION);
1665
1666         sc->hpp_options |= (ED_HPP_OPTION_NIC_RESET | 
1667                                 ED_HPP_OPTION_CHIP_RESET |
1668                                 ED_HPP_OPTION_ENABLE_IRQ);
1669
1670         /* 
1671          * Reset the chip.  This requires writing to the option register
1672          * so take care to preserve the other bits.
1673          */
1674
1675         outw(sc->asic_addr + ED_HPP_OPTION, 
1676                 (sc->hpp_options & ~(ED_HPP_OPTION_NIC_RESET | 
1677                         ED_HPP_OPTION_CHIP_RESET)));
1678
1679         DELAY(5000);    /* wait for chip reset to complete */
1680
1681         outw(sc->asic_addr + ED_HPP_OPTION,
1682                 (sc->hpp_options | (ED_HPP_OPTION_NIC_RESET |
1683                         ED_HPP_OPTION_CHIP_RESET |
1684                         ED_HPP_OPTION_ENABLE_IRQ)));
1685
1686         DELAY(5000);
1687
1688         if (!(inb(sc->nic_addr + ED_P0_ISR) & ED_ISR_RST))
1689                 return 0;       /* reset did not complete */
1690
1691         /*
1692          * Read out configuration information.
1693          */
1694
1695         outw(sc->asic_addr + ED_HPP_PAGING, ED_HPP_PAGE_HW);
1696
1697         irq = inb(sc->asic_addr + ED_HPP_HW_IRQ);
1698
1699         /*
1700          * Check for impossible IRQ.
1701          */
1702
1703         if (irq >= (sizeof(ed_hpp_intr_mask) / sizeof(ed_hpp_intr_mask[0])))
1704                 return 0;
1705
1706         /* 
1707          * If the kernel IRQ was specified with a '?' use the cards idea
1708          * of the IRQ.  If the kernel IRQ was explicitly specified, it
1709          * should match that of the hardware.
1710          */
1711
1712         if (isa_dev->id_irq <= 0)
1713                 isa_dev->id_irq = ed_hpp_intr_mask[irq];
1714         else if (isa_dev->id_irq != ed_hpp_intr_mask[irq])
1715                 return 0;
1716
1717         /*
1718          * Fill in softconfig info.
1719          */
1720
1721         sc->vendor = ED_VENDOR_HP;
1722         sc->type = ED_TYPE_HP_PCLANPLUS;
1723         sc->type_str = "HP-PCLAN+";
1724
1725         sc->mem_shared = 0;     /* we DON'T have dual ported RAM */
1726         sc->mem_start = 0;      /* we use offsets inside the card RAM */
1727
1728         sc->hpp_mem_start = NULL;/* no memory mapped I/O by default */
1729
1730         /*
1731          * Check if memory mapping of the I/O registers possible.
1732          */
1733
1734         if (sc->hpp_options & ED_HPP_OPTION_MEM_ENABLE)
1735         {
1736                 u_long mem_addr;
1737
1738                 /*
1739                  * determine the memory address from the board.
1740                  */
1741                 
1742                 outw(sc->asic_addr + ED_HPP_PAGING, ED_HPP_PAGE_HW);
1743                 mem_addr = (inw(sc->asic_addr + ED_HPP_HW_MEM_MAP) << 8);
1744
1745                 /*
1746                  * Check that the kernel specified start of memory and
1747                  * hardware's idea of it match.
1748                  */
1749                 
1750                 if (mem_addr != kvtop(isa_dev->id_maddr))
1751                         return 0;
1752
1753                 sc->hpp_mem_start = isa_dev->id_maddr;
1754         }
1755
1756         /*
1757          * The board has 32KB of memory.  Is there a way to determine
1758          * this programmatically?
1759          */
1760         
1761         memsize = 32768;
1762
1763         /*
1764          * Fill in the rest of the soft config structure.
1765          */
1766
1767         /*
1768          * The transmit page index.
1769          */
1770
1771         sc->tx_page_start = ED_HPP_TX_PAGE_OFFSET;
1772
1773         if (isa_dev->id_flags & ED_FLAGS_NO_MULTI_BUFFERING)
1774                 sc->txb_cnt = 1;
1775         else
1776                 sc->txb_cnt = 2;
1777
1778         /*
1779          * Memory description
1780          */
1781
1782         sc->mem_size = memsize;
1783         sc->mem_ring = sc->mem_start + 
1784                 (sc->txb_cnt * ED_PAGE_SIZE * ED_TXBUF_SIZE);
1785         sc->mem_end = sc->mem_start + sc->mem_size;
1786
1787         /*
1788          * Receive area starts after the transmit area and 
1789          * continues till the end of memory.
1790          */
1791
1792         sc->rec_page_start = sc->tx_page_start + 
1793                                 (sc->txb_cnt * ED_TXBUF_SIZE);
1794         sc->rec_page_stop = (sc->mem_size / ED_PAGE_SIZE);
1795
1796
1797         sc->cr_proto = 0;       /* value works */
1798
1799         /*
1800          * Set the wrap registers for string I/O reads.
1801          */
1802
1803         outw(sc->asic_addr + ED_HPP_PAGING, ED_HPP_PAGE_HW);
1804         outw(sc->asic_addr + ED_HPP_HW_WRAP,
1805                 ((sc->rec_page_start / ED_PAGE_SIZE) |
1806                  (((sc->rec_page_stop / ED_PAGE_SIZE) - 1) << 8)));
1807
1808         /*
1809          * Reset the register page to normal operation.
1810          */
1811
1812         outw(sc->asic_addr + ED_HPP_PAGING, ED_HPP_PAGE_PERF);
1813
1814         /*
1815          * Verify that we can read/write from adapter memory.
1816          * Create test pattern.
1817          */
1818
1819         for (n = 0; n < ED_HPP_TEST_SIZE; n++)
1820         {
1821                 test_pattern[n] = (n*n) ^ ~n;
1822         }
1823
1824 #undef  ED_HPP_TEST_SIZE
1825
1826         /*
1827          * Check that the memory is accessible thru the I/O ports.
1828          * Write out the contents of "test_pattern", read back
1829          * into "test_buffer" and compare the two for any
1830          * mismatch.
1831          */
1832
1833         for (n = 0; n < (32768 / ED_PAGE_SIZE); n ++) {
1834
1835                 ed_pio_writemem(sc, test_pattern, (n * ED_PAGE_SIZE), 
1836                                 sizeof(test_pattern));
1837                 ed_pio_readmem(sc, (n * ED_PAGE_SIZE), 
1838                         test_buffer, sizeof(test_pattern));
1839
1840                 if (bcmp(test_pattern, test_buffer, 
1841                         sizeof(test_pattern)))
1842                         return 0;
1843         }
1844
1845         return (ED_HPP_IO_PORTS);
1846
1847 }
1848
1849 /*
1850  * HP PC Lan+ : Set the physical link to use AUI or TP/TL.
1851  */
1852
1853 void
1854 ed_hpp_set_physical_link(struct ed_softc *sc)
1855 {
1856         struct ifnet *ifp = &sc->arpcom.ac_if;
1857         int lan_page;
1858
1859         outw(sc->asic_addr + ED_HPP_PAGING, ED_HPP_PAGE_LAN);
1860         lan_page = inw(sc->asic_addr + ED_HPP_PAGE_0);
1861
1862         if (ifp->if_flags & IFF_ALTPHYS) {
1863
1864                 /*
1865                  * Use the AUI port.
1866                  */
1867
1868                 lan_page |= ED_HPP_LAN_AUI;
1869
1870                 outw(sc->asic_addr + ED_HPP_PAGING, ED_HPP_PAGE_LAN);
1871                 outw(sc->asic_addr + ED_HPP_PAGE_0, lan_page);
1872
1873
1874         } else {
1875
1876                 /*
1877                  * Use the ThinLan interface
1878                  */
1879
1880                 lan_page &= ~ED_HPP_LAN_AUI;
1881
1882                 outw(sc->asic_addr + ED_HPP_PAGING, ED_HPP_PAGE_LAN);
1883                 outw(sc->asic_addr + ED_HPP_PAGE_0, lan_page);
1884
1885         }
1886
1887         /*
1888          * Wait for the lan card to re-initialize itself
1889          */
1890
1891         DELAY(150000);  /* wait 150 ms */
1892
1893         /*
1894          * Restore normal pages.
1895          */
1896
1897         outw(sc->asic_addr + ED_HPP_PAGING, ED_HPP_PAGE_PERF);
1898
1899 }
1900
1901 #ifdef PC98
1902 /*
1903  * Probe and vendor-specific initialization routine for SIC-98 boards
1904  */
1905 static int
1906 ed_probe_SIC98(isa_dev)
1907         struct isa_device *isa_dev;
1908 {
1909         struct ed_softc *sc = &ed_softc[isa_dev->id_unit];
1910         int i;
1911         u_char sum;
1912
1913         if ((isa_dev->id_maddr == 0) || (isa_dev->id_msize == 0))
1914                 return (0);
1915
1916         /* Setup card RAM and I/O address
1917          * Kernel Virtual to segment C0000-DFFFF????
1918          */
1919         sc->asic_addr = isa_dev->id_iobase + ED_NOVELL_ASIC_OFFSET;
1920         sc->nic_addr  = isa_dev->id_iobase + ED_NOVELL_NIC_OFFSET;
1921         sc->mem_start = (caddr_t)isa_dev->id_maddr;
1922
1923         /* Reset card to force it into a known state. */
1924         outb(sc->asic_addr, 0x00);
1925         DELAY(100);
1926         if (ED_TYPE98SUB(isa_dev->id_flags) == 0) {
1927                 /* SIC-98/SIU-98 */
1928                 outb(sc->asic_addr, 0x94);
1929                 DELAY(100);
1930                 outb(sc->asic_addr, 0x94);
1931         } else {
1932                 /* SIU-98-D */
1933                 outb(sc->asic_addr, 0x80);
1934                 DELAY(100);
1935                 outb(sc->asic_addr, 0x94);
1936                 DELAY(100);
1937                 outb(sc->asic_addr, 0x9e);
1938         }
1939         DELAY(100);
1940
1941         /* Here we check the card ROM, if the checksum passes, and the
1942          * type code and ethernet address check out, then we know we have
1943          * an SIC card.
1944          */
1945         sum = sc->mem_start[6 * 2];
1946         for (i = 0; i < ETHER_ADDR_LEN; ++i)
1947                 sum ^= (sc->arpcom.ac_enaddr[i] = sc->mem_start[i * 2]);
1948         if (sum != 0)
1949                 return (0);
1950
1951         sc->vendor   = ED_VENDOR_MISC;
1952         sc->type_str = "SIC98";
1953         sc->isa16bit = 1;
1954         sc->cr_proto = 0;
1955         sc->mem_shared = 1;
1956         sc->mem_size = isa_dev->id_msize;
1957         sc->mem_end  = sc->mem_start + sc->mem_size;
1958         sc->mem_ring = sc->mem_start + (ED_TXBUF_SIZE * ED_PAGE_SIZE);
1959         sc->txb_cnt  = 1;
1960         sc->tx_page_start  = 0;
1961         sc->rec_page_start = ED_TXBUF_SIZE;
1962         sc->rec_page_stop  = sc->mem_size / ED_PAGE_SIZE;
1963
1964         /*
1965          * SIC RAM page 0x0000-0x3fff(or 0x7fff)
1966          */
1967         if (ED_TYPE98SUB(isa_dev->id_flags) == 0)
1968                 outb(sc->asic_addr, 0x90);
1969         else
1970                 outb(sc->asic_addr, 0x8e);
1971         DELAY(100);
1972
1973         /*
1974          * Test shared memory
1975          */
1976         if (!ed_shm_testmem(sc)) {
1977                 return (0);
1978         }
1979
1980         /*
1981          * Select page 0 register
1982          */
1983         outb(sc->nic_addr + ED_P2_CR, ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STP);
1984
1985         return (1);
1986 }
1987
1988 /*
1989  * Probe and vendor-specific initialization routine for C-NET(98) boards
1990  */
1991 static int
1992 ed_probe_CNET98(isa_dev)
1993         struct isa_device *isa_dev;
1994 {
1995         struct ed_softc *sc = &ed_softc[isa_dev->id_unit];
1996         u_char tmp, tmp_s;
1997
1998         if ((isa_dev->id_maddr == 0) || (isa_dev->id_msize == 0))
1999                 return (0);
2000
2001         sc->asic_addr = isa_dev->id_iobase;
2002         sc->nic_addr  = isa_dev->id_iobase;
2003         sc->vendor    = ED_VENDOR_MISC;         /* vendor name          */
2004         sc->type_str  = "CNET98";               /* board name           */
2005         sc->isa16bit  = 0;                      /* 16bit mode off       */
2006         sc->cr_proto  = ED_CR_RD2;
2007         sc->mem_shared = 1;
2008         sc->mem_start = (caddr_t)isa_dev->id_maddr;
2009         sc->mem_size  = isa_dev->id_msize;
2010         sc->mem_end   = sc->mem_start + sc->mem_size;
2011         sc->mem_ring  = sc->mem_start + (ED_PAGE_SIZE * ED_TXBUF_SIZE);
2012         sc->txb_cnt   = 1;                      /* tx buffer counter 1  */
2013         sc->tx_page_start  = 0;                 /* page offset 0        */
2014         sc->rec_page_start = ED_TXBUF_SIZE;     /* page offset 6        */
2015         sc->rec_page_stop  = sc->mem_size / ED_PAGE_SIZE;
2016                                                 /* page offset 40       */
2017         /*
2018          * Check i/o address.
2019          * 0x[a-f]3d0 are allowed.
2020          */
2021         if (((sc->asic_addr & (u_short)0x0fff) != 0x03d0)
2022         ||  ((sc->asic_addr & (u_short)0xf000) <  0xa000)) {
2023                 printf("ed%d: Invalid i/o port configuration (0x%x) must be "
2024                         "0x[a-f]3d0 for CNET98\n",
2025                         isa_dev->id_unit, sc->asic_addr);
2026                 return (0);
2027         }
2028
2029         /*
2030          * Check window area address.
2031          */
2032         tmp_s = kvtop(sc->mem_start) >> 12;
2033         if (tmp_s < 0x80) {
2034                 printf("ed%d: Please change window address(0x%x)\n",
2035                         isa_dev->id_unit, kvtop(sc->mem_start));
2036                 return (0);
2037         }
2038
2039         tmp_s &= 0x0f;
2040         tmp    = sc->asic_addr >> 12;
2041         if ((tmp_s <= tmp) && (tmp < (tmp_s + 4))) {
2042 printf("ed%d: Please change iobase address(0x%x) or window address(0x%x)\n",
2043                         isa_dev->id_unit, isa_dev->id_iobase,
2044                         kvtop(sc->mem_start));
2045                 return (0);
2046         }
2047
2048         /* Reset the board */
2049         ed_reset_CNET98(isa_dev->id_iobase, isa_dev->id_flags);
2050          
2051         /* Make sure that we really have an 8390 based board */
2052         if (!ed_probe_generic8390(sc))
2053                 return (0);
2054
2055         /*
2056          *  Set window ethernet address area
2057          *    board memory base 0x480000  data 256byte 
2058          */
2059         ed_winsel_CNET98(sc, 0x4800);
2060
2061         /*
2062          * Get station address from on-board ROM
2063          */
2064         bcopy(sc->mem_start, sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
2065
2066         /*
2067          * Set window buffer memory area
2068          *    board memory base 0x400000  data 16kbyte 
2069          */
2070         ed_winsel_CNET98(sc, 0x4000);
2071
2072         /*
2073          * Test shared memory
2074          */
2075         if (!ed_shm_testmem(sc))
2076                 return (0);
2077
2078         /*
2079          *   Set interrupt level
2080          */
2081         switch (isa_dev->id_irq) {
2082         case IRQ3:
2083                 tmp = ED_CNET98_INT_IRQ3;
2084                 break;
2085         default:
2086 printf("ed%d: Change Interrupt level default value from %d to %d.\n",
2087                         isa_dev->id_unit, ffs(isa_dev->id_irq) - 1, 5);
2088                 isa_dev->id_irq = IRQ5;
2089                 /* FALLTHROUGH */
2090         case IRQ5:
2091                 tmp = ED_CNET98_INT_IRQ5;
2092                 break;
2093         case IRQ6:
2094                 tmp = ED_CNET98_INT_IRQ6;
2095                 break;
2096         case IRQ9:
2097                 tmp = ED_CNET98_INT_IRQ9;
2098                 break;
2099         case IRQ12:
2100                 tmp = ED_CNET98_INT_IRQ12;
2101                 break;
2102         case IRQ13:
2103                 tmp = ED_CNET98_INT_IRQ13;
2104                 break;
2105         }
2106         outb(sc->asic_addr + ED_CNET98_INT_LEV, tmp);
2107         DELAY(1000);
2108         /*
2109          *   Set interrupt mask.
2110          *     bit7:1 all interrupt mask
2111          *     bit1:1 timer interrupt mask
2112          *     bit0:0 NS controler interrupt enable
2113          */
2114         outb(sc->asic_addr + ED_CNET98_INT_MASK, 0x7e);
2115         DELAY(1000);
2116
2117         return (ED_CNET98_IO_PORTS); 
2118 }
2119
2120 /*
2121  * Probe and vendor-specific initialization routine for C-NET(98)E/L boards
2122  */
2123 static int
2124 ed_probe_CNET98EL(isa_dev)
2125         struct isa_device *isa_dev;
2126 {
2127         struct ed_softc *sc = &ed_softc[isa_dev->id_unit];
2128         u_int memsize, n;
2129         u_char romdata[ETHER_ADDR_LEN * 2], tmp;
2130         static char test_pattern[32] = "THIS is A memory TEST pattern";
2131         char test_buffer[32];
2132
2133         sc->asic_addr = isa_dev->id_iobase + ED_NOVELL_ASIC_OFFSET;
2134         sc->nic_addr  = isa_dev->id_iobase + ED_NOVELL_NIC_OFFSET;
2135
2136         /* Check i/o address. CNET98E/L only allows ?3d0h */
2137         if ((sc->nic_addr & (u_short) 0x0fff) != 0x03d0) {
2138                 printf("ed%d: Invalid i/o port configuration (%x) must be "
2139                         "?3d0h for CNET98E/L\n",
2140                         isa_dev->id_unit, sc->nic_addr);
2141                 return (0);
2142         }
2143
2144         /* Reset the board */
2145         ed_reset_CNET98(isa_dev->id_iobase, isa_dev->id_flags);
2146          
2147         /* Make sure that we really have an 8390 based board */
2148         if (!ed_probe_generic8390(sc))
2149                 return (0);
2150
2151         sc->vendor = ED_VENDOR_NOVELL;
2152         sc->mem_shared = 0;
2153         sc->cr_proto = ED_CR_RD2;
2154
2155         /* Test the ability to read and write to the NIC memory. */
2156
2157         /*
2158          * This prevents packets from being stored in the NIC memory when the
2159          * readmem routine turns on the start bit in the CR.
2160          */
2161         outb(sc->nic_addr + ED_P0_RCR, ED_RCR_MON);
2162
2163         /* initialize DCR for word operations */
2164         outb(sc->nic_addr + ED_P0_DCR, ED_DCR_WTS | ED_DCR_FT1 | ED_DCR_LS);
2165
2166         sc->isa16bit = 1;
2167
2168         /* CNET98E/L board has 16k of memory */
2169         memsize = 16384;
2170
2171         /* NIC memory start at zero on a CNET98E/L board */
2172         sc->mem_start = (char *) ED_CNET98EL_PAGE_OFFSET;
2173         sc->mem_end = sc->mem_start + memsize;
2174         sc->tx_page_start = ED_CNET98EL_PAGE_OFFSET / ED_PAGE_SIZE;
2175
2176         /*
2177          * Write a test pattern in word mode. If failure page is not 16k, then
2178          * we don't know what this board is.
2179          */
2180         for (n = ED_CNET98EL_PAGE_OFFSET; n < 65536; n += 1024) {
2181                 ed_pio_writemem(sc, test_pattern, n, sizeof(test_pattern));
2182                 ed_pio_readmem(sc, n, test_buffer, sizeof(test_pattern));
2183
2184                 if (bcmp(test_pattern, test_buffer, sizeof(test_pattern)))
2185                         break;
2186         }
2187         if (n != (ED_CNET98EL_PAGE_OFFSET + memsize)) {
2188 #ifdef ED_DEBUG
2189                 printf("ed%d: CNET98E/L memory failure at %x\n",
2190                         isa_dev->id_unit, n);
2191 #endif
2192                 return (0);     /* not a CNET98E/L */
2193         }
2194
2195         /*
2196          * Set IRQ. CNET98E/L only allows a choice of irq 3,5,6.
2197          */
2198         switch (isa_dev->id_irq) {
2199         case IRQ3:
2200                 tmp = ED_CNET98EL_ICR_IRQ3;
2201                 break;
2202         case IRQ5:
2203                 tmp = ED_CNET98EL_ICR_IRQ5;
2204                 break;
2205         case IRQ6:
2206                 tmp = ED_CNET98EL_ICR_IRQ6;
2207                 break;
2208 #if 0
2209         case IRQ12:
2210                 tmp = ED_CNET98EL_ICR_IRQ12;
2211                 break;
2212 #endif
2213         default:
2214 printf("ed%d: Invalid irq configuration (%d) must be 3,5,6 for CNET98E/L\n",
2215                         isa_dev->id_unit, ffs(isa_dev->id_irq) - 1);
2216                 return (0);
2217         }
2218         outb(sc->asic_addr + ED_CNET98EL_ICR, tmp);
2219         outb(sc->asic_addr + ED_CNET98EL_IMR, 0x7e);
2220
2221         sc->type_str = "CNET98E/L";
2222
2223 #if 0 /* probably not useful - NE boards only come two ways */
2224         /* allow kernel config file overrides */
2225         if (isa_dev->id_msize)
2226                 memsize = isa_dev->id_msize;
2227 #endif
2228
2229         sc->mem_size = memsize;
2230
2231         /*
2232          * Use one xmit buffer if < 16k, two buffers otherwise (if not told
2233          * otherwise).
2234          */
2235         if ((memsize < 16384) || (isa_dev->id_flags & ED_FLAGS_NO_MULTI_BUFFERING))
2236                 sc->txb_cnt = 1;
2237         else
2238                 sc->txb_cnt = 2;
2239
2240         sc->rec_page_start = sc->tx_page_start + sc->txb_cnt * ED_TXBUF_SIZE;
2241         sc->rec_page_stop = sc->tx_page_start + memsize / ED_PAGE_SIZE;
2242
2243         sc->mem_ring = sc->mem_start + sc->txb_cnt * ED_PAGE_SIZE * ED_TXBUF_SIZE;
2244         isa_dev->id_maddr = 0;
2245
2246         /*
2247          * Get station address from on-board ROM
2248          */
2249         ed_pio_readmem(sc, 16384, romdata, sizeof(romdata));
2250         for (n = 0; n < ETHER_ADDR_LEN; n++)
2251                 sc->arpcom.ac_enaddr[n] = romdata[n * 2];
2252
2253         /* clear any pending interrupts that might have occurred above */
2254         outb(sc->nic_addr + ED_P0_ISR, 0xff);
2255
2256         return (ED_CNET98EL_IO_PORTS);
2257 }
2258
2259 static void
2260 ed_reset_CNET98(iobase, flags)
2261         int iobase;
2262         int flags;
2263 {
2264         u_short init_addr = ED_CNET98_INIT;
2265         u_char tmp;
2266
2267         /* Choose initial register address */
2268         if (ED_TYPE98SUB(flags) != 0) {
2269                 init_addr = ED_CNET98_INIT2;
2270         }
2271 #ifdef ED_DEBUG
2272         printf("ed?: initial register=%x\n", init_addr);
2273 #endif
2274         /*
2275          * Reset the board to force it into a known state.
2276          */
2277         outb(init_addr, 0x00);  /* request */
2278         DELAY(5000);
2279         outb(init_addr, 0x01);  /* cancel */
2280         DELAY(5000);
2281
2282         /*
2283          * Set i/o address(A15-12) and cpu type
2284          *
2285          *   AAAAIXXC(8bit)
2286          *   AAAA: A15-A12,  I: I/O enable, XX: reserved, C: CPU type
2287          *
2288          * CPU type is 1:80286 or higher, 0:not.
2289          * But FreeBSD runs under i386 or higher, thus it must be 1.
2290          */
2291         tmp = (iobase & (u_short) 0xf000) >> 8;
2292         tmp |= (0x08 | 0x01);
2293 #ifdef ED_DEBUG
2294         printf("ed?: outb(%x, %x)\n", init_addr + 2, tmp);
2295 #endif
2296         outb(init_addr + 2, tmp);
2297         DELAY(1000);
2298 }
2299
2300 static void
2301 ed_winsel_CNET98(sc, bank)
2302         struct ed_softc *sc;
2303         u_short bank;
2304 {
2305         u_char mem = (kvtop(sc->mem_start) >> 12) & 0xff;
2306
2307         /*
2308          * Disable window memory
2309          *    bit7 is 0:disable
2310          */
2311         outb(sc->asic_addr + ED_CNET98_WIN_REG, mem & 0x7f);
2312         DELAY(10);
2313
2314         /*
2315          * Select window address
2316          *    FreeBSD address 0xf00xxxxx
2317          */
2318         outb(sc->asic_addr + ED_CNET98_MAP_REG0L, bank & 0xff);
2319         DELAY(10);
2320         outb(sc->asic_addr + ED_CNET98_MAP_REG0H, (bank >> 8) & 0xff);
2321         DELAY(10);
2322         outb(sc->asic_addr + ED_CNET98_MAP_REG1L, 0x00);
2323         DELAY(10);
2324         outb(sc->asic_addr + ED_CNET98_MAP_REG1H, 0x41);
2325         DELAY(10);
2326         outb(sc->asic_addr + ED_CNET98_MAP_REG2L, 0x00);
2327         DELAY(10);
2328         outb(sc->asic_addr + ED_CNET98_MAP_REG2H, 0x42);
2329         DELAY(10);
2330         outb(sc->asic_addr + ED_CNET98_MAP_REG3L, 0x00);
2331         DELAY(10);
2332         outb(sc->asic_addr + ED_CNET98_MAP_REG3H, 0x43);
2333         DELAY(10);
2334
2335         /*
2336          * Enable window memory(16Kbyte)
2337          *    bit7 is 1:enable
2338          */
2339 #ifdef ED_DEBUG
2340         printf("ed?: window start address=%x\n", mem);
2341 #endif
2342         outb(sc->asic_addr + ED_CNET98_WIN_REG, mem);
2343         DELAY(10);
2344 }
2345
2346 /*
2347  * Probe and vendor-specific initialization routine for EC/EP-98X boards
2348  */
2349 static int
2350 ed_probe_NW98X(isa_dev)
2351         struct isa_device *isa_dev;
2352 {
2353         struct ed_softc *sc = &ed_softc[isa_dev->id_unit];
2354         int nports;
2355         u_char tmp;
2356
2357         nports = ed_probe_Novell(isa_dev);
2358         if (nports == 0)
2359                 return (0);
2360
2361         /*
2362          * Set IRQ. EC/EP-98X only allows a choice of irq 3,5,6,12,13.
2363          */
2364         switch (isa_dev->id_irq) {
2365         case IRQ3:
2366                 tmp = ED_NW98X_IRQ3;
2367                 break;
2368         case IRQ5:
2369                 tmp = ED_NW98X_IRQ5;
2370                 break;
2371         case IRQ6:
2372                 tmp = ED_NW98X_IRQ6;
2373                 break;
2374         case IRQ12:
2375                 tmp = ED_NW98X_IRQ12;
2376                 break;
2377         case IRQ13:
2378                 tmp = ED_NW98X_IRQ13;
2379                 break;
2380         default:
2381                 printf("ed%d: Invalid irq configuration (%d) must be "
2382                         "3,5,6,12,13 for EC/EP-98X\n",
2383                         isa_dev->id_unit, ffs(isa_dev->id_irq) - 1);
2384                 return (0);
2385         }
2386         outb(sc->asic_addr + ED_NW98X_IRQ, tmp);
2387
2388         return (1);
2389 }
2390
2391 /* TODO - should be used PC/AT also */
2392 static int
2393 ed_shm_testmem(sc)
2394         struct ed_softc *sc;
2395 {
2396         int i;
2397
2398         /*
2399          * clear interface memory, then sum to make sure its valid
2400          */
2401         bzero(sc->mem_start, sc->mem_size);
2402
2403         for (i = 0; i < sc->mem_size; ++i) {
2404                 if (sc->mem_start[i]) {
2405 printf("ed?: failed to clear shared memory at %lx - check configuration\n",
2406                                 kvtop(sc->mem_start + i));
2407                         return (0);
2408                 }
2409         }
2410
2411         return (1);
2412 }
2413 #endif  /* PC98 */
2414
2415 /*
2416  * Install interface into kernel networking data structures
2417  */
2418 static int
2419 ed_attach(sc, unit, flags)
2420         struct ed_softc *sc;
2421         int unit;
2422         int flags;
2423 {
2424         struct ifnet *ifp = &sc->arpcom.ac_if;
2425
2426         /*
2427          * Set interface to stopped condition (reset)
2428          */
2429         ed_stop(sc);
2430
2431         if (!ifp->if_name) {
2432                 /*
2433                  * Initialize ifnet structure
2434                  */
2435                 ifp->if_softc = sc;
2436                 ifp->if_unit = unit;
2437                 ifp->if_name = "ed";
2438                 ifp->if_output = ether_output;
2439                 ifp->if_start = ed_start;
2440                 ifp->if_ioctl = ed_ioctl;
2441                 ifp->if_watchdog = ed_watchdog;
2442                 ifp->if_init = ed_init;
2443                 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
2444                 ifp->if_linkmib = &sc->mibdata;
2445                 ifp->if_linkmiblen = sizeof sc->mibdata;
2446                 /*
2447                  * XXX - should do a better job.
2448                  */
2449                 if (sc->is790)
2450                         sc->mibdata.dot3StatsEtherChipSet =
2451                                 DOT3CHIPSET(dot3VendorWesternDigital,
2452                                             dot3ChipSetWesternDigital83C790);
2453                 else
2454                         sc->mibdata.dot3StatsEtherChipSet =
2455                                 DOT3CHIPSET(dot3VendorNational, 
2456                                             dot3ChipSetNational8390);
2457                 sc->mibdata.dot3Compliance = DOT3COMPLIANCE_COLLS;
2458
2459                 /*
2460                  * Set default state for ALTPHYS flag (used to disable the 
2461                  * tranceiver for AUI operation), based on compile-time 
2462                  * config option.
2463                  */
2464                 if (flags & ED_FLAGS_DISABLE_TRANCEIVER)
2465                         ifp->if_flags = (IFF_BROADCAST | IFF_SIMPLEX | 
2466                             IFF_MULTICAST | IFF_ALTPHYS);
2467                 else
2468                         ifp->if_flags = (IFF_BROADCAST | IFF_SIMPLEX |
2469                             IFF_MULTICAST);
2470
2471                 /*
2472                  * Attach the interface
2473                  */
2474                 if_attach(ifp);
2475                 ether_ifattach(ifp);
2476         }
2477         /* device attach does transition from UNCONFIGURED to IDLE state */
2478
2479         /*
2480          * Print additional info when attached
2481          */
2482         printf("%s%d: address %6D, ", ifp->if_name, ifp->if_unit, 
2483                 sc->arpcom.ac_enaddr, ":");
2484
2485         if (sc->type_str && (*sc->type_str != 0))
2486                 printf("type %s ", sc->type_str);
2487         else
2488                 printf("type unknown (0x%x) ", sc->type);
2489
2490         if (sc->vendor == ED_VENDOR_HP)
2491                 printf("(%s %s IO)", (sc->hpp_id & ED_HPP_ID_16_BIT_ACCESS) ?
2492                         "16-bit" : "32-bit",
2493                         sc->hpp_mem_start ? "memory mapped" : "regular");
2494         else
2495                 printf("%s ", sc->isa16bit ? "(16 bit)" : "(8 bit)");
2496
2497         printf("%s\n", (((sc->vendor == ED_VENDOR_3COM) ||
2498                          (sc->vendor == ED_VENDOR_HP)) &&
2499                 (ifp->if_flags & IFF_ALTPHYS)) ? " tranceiver disabled" : "");
2500
2501         /*
2502          * If BPF is in the kernel, call the attach for it
2503          */
2504         bpfattach(ifp, DLT_EN10MB, sizeof(struct ether_header));
2505         return 1;
2506 }
2507
2508 static int
2509 ed_attach_isa(isa_dev)
2510         struct isa_device *isa_dev;
2511 {
2512         int unit = isa_dev->id_unit;
2513         struct ed_softc *sc = &ed_softc[unit];
2514         int flags = isa_dev->id_flags;
2515
2516         isa_dev->id_ointr = edintr;
2517         return ed_attach(sc, unit, flags);
2518 }
2519
2520 #if NPCI > 0
2521 void *
2522 ed_attach_NE2000_pci(unit, port)
2523         int unit;
2524         int port;
2525 {
2526         struct ed_softc *sc = malloc(sizeof *sc, M_DEVBUF, M_NOWAIT);
2527         int isa_flags = 0;
2528
2529         if (!sc)
2530                 return sc;
2531
2532         bzero(sc, sizeof *sc);
2533 #ifdef PC98
2534         (void)pc98_set_register_unit(sc, ED_TYPE98_GENERIC, 0);
2535 #endif
2536         if (ed_probe_Novell_generic(sc, port, unit, isa_flags) == 0
2537             || ed_attach(sc, unit, isa_flags) == 0) {
2538                 free(sc, M_DEVBUF);
2539                 return NULL;
2540         }
2541         return sc;
2542 }
2543 #endif
2544
2545 /*
2546  * Reset interface.
2547  */
2548 static void
2549 ed_reset(ifp)
2550         struct ifnet *ifp;
2551 {
2552         struct ed_softc *sc = ifp->if_softc;
2553         int     s;
2554
2555         if (sc->gone)
2556                 return;
2557         s = splimp();
2558
2559         /*
2560          * Stop interface and re-initialize.
2561          */
2562         ed_stop(sc);
2563         ed_init(sc);
2564
2565         (void) splx(s);
2566 }
2567
2568 /*
2569  * Take interface offline.
2570  */
2571 static void
2572 ed_stop(sc)
2573         struct ed_softc *sc;
2574 {
2575         int     n = 5000;
2576
2577         if (sc->gone)
2578                 return;
2579         /*
2580          * Stop everything on the interface, and select page 0 registers.
2581          */
2582         outb(sc->nic_addr + ED_P0_CR, sc->cr_proto | ED_CR_STP);
2583
2584         /*
2585          * Wait for interface to enter stopped state, but limit # of checks to
2586          * 'n' (about 5ms). It shouldn't even take 5us on modern DS8390's, but
2587          * just in case it's an old one.
2588          */
2589         while (((inb(sc->nic_addr + ED_P0_ISR) & ED_ISR_RST) == 0) && --n);
2590 }
2591
2592 /*
2593  * Device timeout/watchdog routine. Entered if the device neglects to
2594  *      generate an interrupt after a transmit has been started on it.
2595  */
2596 static void
2597 ed_watchdog(ifp)
2598         struct ifnet *ifp;
2599 {
2600         struct ed_softc *sc = ifp->if_softc;
2601
2602         if (sc->gone)
2603                 return;
2604         log(LOG_ERR, "ed%d: device timeout\n", ifp->if_unit);
2605         ifp->if_oerrors++;
2606
2607         ed_reset(ifp);
2608 }
2609
2610 /*
2611  * Initialize device.
2612  */
2613 static void
2614 ed_init(xsc)
2615         void *xsc;
2616 {
2617         struct ed_softc *sc = xsc;
2618         struct ifnet *ifp = &sc->arpcom.ac_if;
2619         int     i, s;
2620
2621         if (sc->gone)
2622                 return;
2623
2624         /* address not known */
2625         if (TAILQ_EMPTY(&ifp->if_addrhead)) /* unlikely? XXX */
2626                 return;
2627
2628         /*
2629          * Initialize the NIC in the exact order outlined in the NS manual.
2630          * This init procedure is "mandatory"...don't change what or when
2631          * things happen.
2632          */
2633         s = splimp();
2634
2635         /* reset transmitter flags */
2636         sc->xmit_busy = 0;
2637         ifp->if_timer = 0;
2638
2639         sc->txb_inuse = 0;
2640         sc->txb_new = 0;
2641         sc->txb_next_tx = 0;
2642
2643         /* This variable is used below - don't move this assignment */
2644         sc->next_packet = sc->rec_page_start + 1;
2645
2646         /*
2647          * Set interface for page 0, Remote DMA complete, Stopped
2648          */
2649         outb(sc->nic_addr + ED_P0_CR, sc->cr_proto | ED_CR_STP);
2650
2651         if (sc->isa16bit) {
2652
2653                 /*
2654                  * Set FIFO threshold to 8, No auto-init Remote DMA, byte
2655                  * order=80x86, word-wide DMA xfers,
2656                  */
2657                 outb(sc->nic_addr + ED_P0_DCR, ED_DCR_FT1 | ED_DCR_WTS | ED_DCR_LS);
2658         } else {
2659
2660                 /*
2661                  * Same as above, but byte-wide DMA xfers
2662                  */
2663                 outb(sc->nic_addr + ED_P0_DCR, ED_DCR_FT1 | ED_DCR_LS);
2664         }
2665
2666         /*
2667          * Clear Remote Byte Count Registers
2668          */
2669         outb(sc->nic_addr + ED_P0_RBCR0, 0);
2670         outb(sc->nic_addr + ED_P0_RBCR1, 0);
2671
2672         /*
2673          * For the moment, don't store incoming packets in memory.
2674          */
2675         outb(sc->nic_addr + ED_P0_RCR, ED_RCR_MON);
2676
2677         /*
2678          * Place NIC in internal loopback mode
2679          */
2680         outb(sc->nic_addr + ED_P0_TCR, ED_TCR_LB0);
2681
2682         /*
2683          * Initialize transmit/receive (ring-buffer) Page Start
2684          */
2685         outb(sc->nic_addr + ED_P0_TPSR, sc->tx_page_start);
2686         outb(sc->nic_addr + ED_P0_PSTART, sc->rec_page_start);
2687         /* Set lower bits of byte addressable framing to 0 */
2688         if (sc->is790)
2689                 outb(sc->nic_addr + 0x09, 0);
2690
2691         /*
2692          * Initialize Receiver (ring-buffer) Page Stop and Boundry
2693          */
2694         outb(sc->nic_addr + ED_P0_PSTOP, sc->rec_page_stop);
2695         outb(sc->nic_addr + ED_P0_BNRY, sc->rec_page_start);
2696
2697         /*
2698          * Clear all interrupts. A '1' in each bit position clears the
2699          * corresponding flag.
2700          */
2701         outb(sc->nic_addr + ED_P0_ISR, 0xff);
2702
2703         /*
2704          * Enable the following interrupts: receive/transmit complete,
2705          * receive/transmit error, and Receiver OverWrite.
2706          *
2707          * Counter overflow and Remote DMA complete are *not* enabled.
2708          */
2709         outb(sc->nic_addr + ED_P0_IMR,
2710         ED_IMR_PRXE | ED_IMR_PTXE | ED_IMR_RXEE | ED_IMR_TXEE | ED_IMR_OVWE);
2711
2712         /*
2713          * Program Command Register for page 1
2714          */
2715         outb(sc->nic_addr + ED_P0_CR, sc->cr_proto | ED_CR_PAGE_1 | ED_CR_STP);
2716
2717         /*
2718          * Copy out our station address
2719          */
2720         for (i = 0; i < ETHER_ADDR_LEN; ++i)
2721                 outb(sc->nic_addr + ED_P1_PAR(i), sc->arpcom.ac_enaddr[i]);
2722
2723         /*
2724          * Set Current Page pointer to next_packet (initialized above)
2725          */
2726         outb(sc->nic_addr + ED_P1_CURR, sc->next_packet);
2727
2728         /*
2729          * Program Receiver Configuration Register and multicast filter. CR is
2730          * set to page 0 on return.
2731          */
2732         ed_setrcr(sc);
2733
2734         /*
2735          * Take interface out of loopback
2736          */
2737         outb(sc->nic_addr + ED_P0_TCR, 0);
2738
2739         /*
2740          * If this is a 3Com board, the tranceiver must be software enabled
2741          * (there is no settable hardware default).
2742          */
2743         if (sc->vendor == ED_VENDOR_3COM) {
2744                 if (ifp->if_flags & IFF_ALTPHYS) {
2745                         outb(sc->asic_addr + ED_3COM_CR, 0);
2746                 } else {
2747                         outb(sc->asic_addr + ED_3COM_CR, ED_3COM_CR_XSEL);
2748                 }
2749         }
2750
2751         /*
2752          * Set 'running' flag, and clear output active flag.
2753          */
2754         ifp->if_flags |= IFF_RUNNING;
2755         ifp->if_flags &= ~IFF_OACTIVE;
2756
2757         /*
2758          * ...and attempt to start output
2759          */
2760         ed_start(ifp);
2761
2762         (void) splx(s);
2763 }
2764
2765 /*
2766  * This routine actually starts the transmission on the interface
2767  */
2768 static __inline void
2769 ed_xmit(sc)
2770         struct ed_softc *sc;
2771 {
2772         struct ifnet *ifp = (struct ifnet *)sc;
2773         unsigned short len;
2774
2775         if (sc->gone)
2776                 return;
2777         len = sc->txb_len[sc->txb_next_tx];
2778
2779         /*
2780          * Set NIC for page 0 register access
2781          */
2782         outb(sc->nic_addr + ED_P0_CR, sc->cr_proto | ED_CR_STA);
2783
2784         /*
2785          * Set TX buffer start page
2786          */
2787         outb(sc->nic_addr + ED_P0_TPSR, sc->tx_page_start +
2788              sc->txb_next_tx * ED_TXBUF_SIZE);
2789
2790         /*
2791          * Set TX length
2792          */
2793         outb(sc->nic_addr + ED_P0_TBCR0, len);
2794         outb(sc->nic_addr + ED_P0_TBCR1, len >> 8);
2795
2796         /*
2797          * Set page 0, Remote DMA complete, Transmit Packet, and *Start*
2798          */
2799         outb(sc->nic_addr + ED_P0_CR, sc->cr_proto | ED_CR_TXP | ED_CR_STA);
2800         sc->xmit_busy = 1;
2801
2802         /*
2803          * Point to next transmit buffer slot and wrap if necessary.
2804          */
2805         sc->txb_next_tx++;
2806         if (sc->txb_next_tx == sc->txb_cnt)
2807                 sc->txb_next_tx = 0;
2808
2809         /*
2810          * Set a timer just in case we never hear from the board again
2811          */
2812         ifp->if_timer = 2;
2813 }
2814
2815 /*
2816  * Start output on interface.
2817  * We make two assumptions here:
2818  *  1) that the current priority is set to splimp _before_ this code
2819  *     is called *and* is returned to the appropriate priority after
2820  *     return
2821  *  2) that the IFF_OACTIVE flag is checked before this code is called
2822  *     (i.e. that the output part of the interface is idle)
2823  */
2824 static void
2825 ed_start(ifp)
2826         struct ifnet *ifp;
2827 {
2828         struct ed_softc *sc = ifp->if_softc;
2829         struct mbuf *m0, *m;
2830         caddr_t buffer;
2831         int     len;
2832
2833         if (sc->gone) {
2834                 printf("ed_start(%p) GONE\n",ifp);
2835                 return;
2836         }
2837 outloop:
2838
2839         /*
2840          * First, see if there are buffered packets and an idle transmitter -
2841          * should never happen at this point.
2842          */
2843         if (sc->txb_inuse && (sc->xmit_busy == 0)) {
2844                 printf("ed: packets buffered, but transmitter idle\n");
2845                 ed_xmit(sc);
2846         }
2847
2848         /*
2849          * See if there is room to put another packet in the buffer.
2850          */
2851         if (sc->txb_inuse == sc->txb_cnt) {
2852
2853                 /*
2854                  * No room. Indicate this to the outside world and exit.
2855                  */
2856                 ifp->if_flags |= IFF_OACTIVE;
2857                 return;
2858         }
2859         IF_DEQUEUE(&ifp->if_snd, m);
2860         if (m == 0) {
2861
2862                 /*
2863                  * We are using the !OACTIVE flag to indicate to the outside
2864                  * world that we can accept an additional packet rather than
2865                  * that the transmitter is _actually_ active. Indeed, the
2866                  * transmitter may be active, but if we haven't filled all the
2867                  * buffers with data then we still want to accept more.
2868                  */
2869                 ifp->if_flags &= ~IFF_OACTIVE;
2870                 return;
2871         }
2872
2873         /*
2874          * Copy the mbuf chain into the transmit buffer
2875          */
2876
2877         m0 = m;
2878
2879         /* txb_new points to next open buffer slot */
2880         buffer = sc->mem_start + (sc->txb_new * ED_TXBUF_SIZE * ED_PAGE_SIZE);
2881
2882         if (sc->mem_shared) {
2883
2884                 /*
2885                  * Special case setup for 16 bit boards...
2886                  */
2887                 if (sc->isa16bit) {
2888                         switch (sc->vendor) {
2889
2890                                 /*
2891                                  * For 16bit 3Com boards (which have 16k of
2892                                  * memory), we have the xmit buffers in a
2893                                  * different page of memory ('page 0') - so
2894                                  * change pages.
2895                                  */
2896                         case ED_VENDOR_3COM:
2897                                 outb(sc->asic_addr + ED_3COM_GACFR,
2898                                      ED_3COM_GACFR_RSEL);
2899                                 break;
2900
2901                                 /*
2902                                  * Enable 16bit access to shared memory on
2903                                  * WD/SMC boards.
2904                                  */
2905                         case ED_VENDOR_WD_SMC:{
2906                                         outb(sc->asic_addr + ED_WD_LAAR,
2907                                              sc->wd_laar_proto | ED_WD_LAAR_M16EN);
2908                                         if (sc->is790) {
2909                                                 outb(sc->asic_addr + ED_WD_MSR, ED_WD_MSR_MENB);
2910                                         }
2911                                         break;
2912                                 }
2913                         }
2914                 }
2915                 for (len = 0; m != 0; m = m->m_next) {
2916                         bcopy(mtod(m, caddr_t), buffer, m->m_len);
2917                         buffer += m->m_len;
2918                         len += m->m_len;
2919                 }
2920
2921                 /*
2922                  * Restore previous shared memory access
2923                  */
2924                 if (sc->isa16bit) {
2925                         switch (sc->vendor) {
2926                         case ED_VENDOR_3COM:
2927                                 outb(sc->asic_addr + ED_3COM_GACFR,
2928                                      ED_3COM_GACFR_RSEL | ED_3COM_GACFR_MBS0);
2929                                 break;
2930                         case ED_VENDOR_WD_SMC:{
2931                                         if (sc->is790) {
2932                                                 outb(sc->asic_addr + ED_WD_MSR, 0x00);
2933                                         }
2934                                         outb(sc->asic_addr + ED_WD_LAAR,
2935                                             sc->wd_laar_proto & ~ED_WD_LAAR_M16EN);
2936                                         break;
2937                                 }
2938                         }
2939                 }
2940         } else {
2941                 len = ed_pio_write_mbufs(sc, m, (int)buffer);
2942                 if (len == 0)
2943                         goto outloop;
2944         }
2945
2946         sc->txb_len[sc->txb_new] = max(len, (ETHER_MIN_LEN-ETHER_CRC_LEN));
2947
2948         sc->txb_inuse++;
2949
2950         /*
2951          * Point to next buffer slot and wrap if necessary.
2952          */
2953         sc->txb_new++;
2954         if (sc->txb_new == sc->txb_cnt)
2955                 sc->txb_new = 0;
2956
2957         if (sc->xmit_busy == 0)
2958                 ed_xmit(sc);
2959
2960         /*
2961          * Tap off here if there is a bpf listener.
2962          */
2963         if (ifp->if_bpf) {
2964                 bpf_mtap(ifp, m0);
2965         }
2966
2967         m_freem(m0);
2968
2969         /*
2970          * Loop back to the top to possibly buffer more packets
2971          */
2972         goto outloop;
2973 }
2974
2975 /*
2976  * Ethernet interface receiver interrupt.
2977  */
2978 static __inline void
2979 ed_rint(sc)
2980         struct ed_softc *sc;
2981 {
2982         struct ifnet *ifp = &sc->arpcom.ac_if;
2983         u_char  boundry;
2984         u_short len;
2985         struct ed_ring packet_hdr;
2986         char   *packet_ptr;
2987
2988         if (sc->gone)
2989                 return;
2990
2991         /*
2992          * Set NIC to page 1 registers to get 'current' pointer
2993          */
2994         outb(sc->nic_addr + ED_P0_CR, sc->cr_proto | ED_CR_PAGE_1 | ED_CR_STA);
2995
2996         /*
2997          * 'sc->next_packet' is the logical beginning of the ring-buffer -
2998          * i.e. it points to where new data has been buffered. The 'CURR'
2999          * (current) register points to the logical end of the ring-buffer -
3000          * i.e. it points to where additional new data will be added. We loop
3001          * here until the logical beginning equals the logical end (or in
3002          * other words, until the ring-buffer is empty).
3003          */
3004         while (sc->next_packet != inb(sc->nic_addr + ED_P1_CURR)) {
3005
3006                 /* get pointer to this buffer's header structure */
3007                 packet_ptr = sc->mem_ring +
3008                     (sc->next_packet - sc->rec_page_start) * ED_PAGE_SIZE;
3009
3010                 /*
3011                  * The byte count includes a 4 byte header that was added by
3012                  * the NIC.
3013                  */
3014                 if (sc->mem_shared)
3015                         packet_hdr = *(struct ed_ring *) packet_ptr;
3016                 else
3017                         ed_pio_readmem(sc, (int)packet_ptr, (char *) &packet_hdr,
3018                                        sizeof(packet_hdr));
3019                 len = packet_hdr.count;
3020                 if (len > (ETHER_MAX_LEN - ETHER_CRC_LEN + sizeof(struct ed_ring)) ||
3021                     len < (ETHER_MIN_LEN - ETHER_CRC_LEN + sizeof(struct ed_ring))) {
3022                         /*
3023                          * Length is a wild value. There's a good chance that
3024                          * this was caused by the NIC being old and buggy.
3025                          * The bug is that the length low byte is duplicated in
3026                          * the high byte. Try to recalculate the length based on
3027                          * the pointer to the next packet.
3028                          */
3029                         /*
3030                          * NOTE: sc->next_packet is pointing at the current packet.
3031                          */
3032                         len &= ED_PAGE_SIZE - 1;        /* preserve offset into page */
3033                         if (packet_hdr.next_packet >= sc->next_packet) {
3034                                 len += (packet_hdr.next_packet - sc->next_packet) * ED_PAGE_SIZE;
3035                         } else {
3036                                 len += ((packet_hdr.next_packet - sc->rec_page_start) +
3037                                         (sc->rec_page_stop - sc->next_packet)) * ED_PAGE_SIZE;
3038                         }
3039                         /*
3040                          * because buffers are aligned on 256-byte boundary,
3041                          * the length computed above is off by 256 in almost
3042                          * all cases. Fix it...
3043                          */
3044                         if (len & 0xff)
3045                                 len -= 256 ;
3046                         if (len > (ETHER_MAX_LEN - ETHER_CRC_LEN 
3047                                    + sizeof(struct ed_ring)))
3048                                 sc->mibdata.dot3StatsFrameTooLongs++;
3049                 }
3050                 /*
3051                  * Be fairly liberal about what we allow as a "reasonable" length
3052                  * so that a [crufty] packet will make it to BPF (and can thus
3053                  * be analyzed). Note that all that is really important is that
3054                  * we have a length that will fit into one mbuf cluster or less;
3055                  * the upper layer protocols can then figure out the length from
3056                  * their own length field(s).
3057                  */
3058                 if ((len > sizeof(struct ed_ring)) &&
3059                     (len <= MCLBYTES) &&
3060                     (packet_hdr.next_packet >= sc->rec_page_start) &&
3061                     (packet_hdr.next_packet < sc->rec_page_stop)) {
3062                         /*
3063                          * Go get packet.
3064                          */
3065                         ed_get_packet(sc, packet_ptr + sizeof(struct ed_ring),
3066                                       len - sizeof(struct ed_ring), packet_hdr.rsr & ED_RSR_PHY);
3067                         ifp->if_ipackets++;
3068                 } else {
3069                         /*
3070                          * Really BAD. The ring pointers are corrupted.
3071                          */
3072                         log(LOG_ERR,
3073                             "ed%d: NIC memory corrupt - invalid packet length %d\n",
3074                             ifp->if_unit, len);
3075                         ifp->if_ierrors++;
3076                         ed_reset(ifp);
3077                         return;
3078                 }
3079
3080                 /*
3081                  * Update next packet pointer
3082                  */
3083                 sc->next_packet = packet_hdr.next_packet;
3084
3085                 /*
3086                  * Update NIC boundry pointer - being careful to keep it one
3087                  * buffer behind. (as recommended by NS databook)
3088                  */
3089                 boundry = sc->next_packet - 1;
3090                 if (boundry < sc->rec_page_start)
3091                         boundry = sc->rec_page_stop - 1;
3092
3093                 /*
3094                  * Set NIC to page 0 registers to update boundry register
3095                  */
3096                 outb(sc->nic_addr + ED_P0_CR, sc->cr_proto | ED_CR_STA);
3097
3098                 outb(sc->nic_addr + ED_P0_BNRY, boundry);
3099
3100                 /*
3101                  * Set NIC to page 1 registers before looping to top (prepare
3102                  * to get 'CURR' current pointer)
3103                  */
3104                 outb(sc->nic_addr + ED_P0_CR, sc->cr_proto | ED_CR_PAGE_1 | ED_CR_STA);
3105         }
3106 }
3107
3108 /*
3109  * Ethernet interface interrupt processor
3110  */
3111 void
3112 edintr_sc(sc)
3113         struct ed_softc *sc;
3114 {
3115         struct ifnet *ifp = (struct ifnet *)sc;
3116         u_char  isr;
3117
3118         if (sc->gone)
3119                 return;
3120         /*
3121          * Set NIC to page 0 registers
3122          */
3123         outb(sc->nic_addr + ED_P0_CR, sc->cr_proto | ED_CR_STA);
3124
3125         /*
3126          * loop until there are no more new interrupts
3127          */
3128         while ((isr = inb(sc->nic_addr + ED_P0_ISR)) != 0) {
3129
3130                 /*
3131                  * reset all the bits that we are 'acknowledging' by writing a
3132                  * '1' to each bit position that was set (writing a '1'
3133                  * *clears* the bit)
3134                  */
3135                 outb(sc->nic_addr + ED_P0_ISR, isr);
3136
3137                 /*
3138                  * Handle transmitter interrupts. Handle these first because
3139                  * the receiver will reset the board under some conditions.
3140                  */
3141                 if (isr & (ED_ISR_PTX | ED_ISR_TXE)) {
3142                         u_char  collisions = inb(sc->nic_addr + ED_P0_NCR) & 0x0f;
3143
3144                         /*
3145                          * Check for transmit error. If a TX completed with an
3146                          * error, we end up throwing the packet away. Really
3147                          * the only error that is possible is excessive
3148                          * collisions, and in this case it is best to allow
3149                          * the automatic mechanisms of TCP to backoff the
3150                          * flow. Of course, with UDP we're screwed, but this
3151                          * is expected when a network is heavily loaded.
3152                          */
3153                         (void) inb(sc->nic_addr + ED_P0_TSR);
3154                         if (isr & ED_ISR_TXE) {
3155                                 u_char tsr;
3156
3157                                 /*
3158                                  * Excessive collisions (16)
3159                                  */
3160                                 tsr = inb(sc->nic_addr + ED_P0_TSR);
3161                                 if ((tsr & ED_TSR_ABT)  
3162                                     && (collisions == 0)) {
3163
3164                                         /*
3165                                          * When collisions total 16, the
3166                                          * P0_NCR will indicate 0, and the
3167                                          * TSR_ABT is set.
3168                                          */
3169                                         collisions = 16;
3170                                         sc->mibdata.dot3StatsExcessiveCollisions++;
3171                                         sc->mibdata.dot3StatsCollFrequencies[15]++;
3172                                 }
3173                                 if (tsr & ED_TSR_OWC)
3174                                         sc->mibdata.dot3StatsLateCollisions++;
3175                                 if (tsr & ED_TSR_CDH)
3176                                         sc->mibdata.dot3StatsSQETestErrors++;
3177                                 if (tsr & ED_TSR_CRS)
3178                                         sc->mibdata.dot3StatsCarrierSenseErrors++;
3179                                 if (tsr & ED_TSR_FU)
3180                                         sc->mibdata.dot3StatsInternalMacTransmitErrors++;
3181
3182                                 /*
3183                                  * update output errors counter
3184                                  */
3185                                 ifp->if_oerrors++;
3186                         } else {
3187
3188                                 /*
3189                                  * Update total number of successfully
3190                                  * transmitted packets.
3191                                  */
3192                                 ifp->if_opackets++;
3193                         }
3194
3195                         /*
3196                          * reset tx busy and output active flags
3197                          */
3198                         sc->xmit_busy = 0;
3199                         ifp->if_flags &= ~IFF_OACTIVE;
3200
3201                         /*
3202                          * clear watchdog timer
3203                          */
3204                         ifp->if_timer = 0;
3205
3206                         /*
3207                          * Add in total number of collisions on last
3208                          * transmission.
3209                          */
3210                         ifp->if_collisions += collisions;
3211                         switch(collisions) {
3212                         case 0:
3213                         case 16:
3214                                 break;
3215                         case 1:
3216                                 sc->mibdata.dot3StatsSingleCollisionFrames++;
3217                                 sc->mibdata.dot3StatsCollFrequencies[0]++;
3218                                 break;
3219                         default:
3220                                 sc->mibdata.dot3StatsMultipleCollisionFrames++;
3221                                 sc->mibdata.
3222                                         dot3StatsCollFrequencies[collisions-1]
3223                                                 ++;
3224                                 break;
3225                         }
3226
3227                         /*
3228                          * Decrement buffer in-use count if not zero (can only
3229                          * be zero if a transmitter interrupt occured while
3230                          * not actually transmitting). If data is ready to
3231                          * transmit, start it transmitting, otherwise defer
3232                          * until after handling receiver
3233                          */
3234                         if (sc->txb_inuse && --sc->txb_inuse)
3235                                 ed_xmit(sc);
3236                 }
3237
3238                 /*
3239                  * Handle receiver interrupts
3240                  */
3241                 if (isr & (ED_ISR_PRX | ED_ISR_RXE | ED_ISR_OVW)) {
3242
3243                         /*
3244                          * Overwrite warning. In order to make sure that a
3245                          * lockup of the local DMA hasn't occurred, we reset
3246                          * and re-init the NIC. The NSC manual suggests only a
3247                          * partial reset/re-init is necessary - but some chips
3248                          * seem to want more. The DMA lockup has been seen
3249                          * only with early rev chips - Methinks this bug was
3250                          * fixed in later revs. -DG
3251                          */
3252                         if (isr & ED_ISR_OVW) {
3253                                 ifp->if_ierrors++;
3254 #ifdef DIAGNOSTIC
3255                                 log(LOG_WARNING,
3256                                     "ed%d: warning - receiver ring buffer overrun\n",
3257                                     ifp->if_unit);
3258 #endif
3259
3260                                 /*
3261                                  * Stop/reset/re-init NIC
3262                                  */
3263                                 ed_reset(ifp);
3264                         } else {
3265
3266                                 /*
3267                                  * Receiver Error. One or more of: CRC error,
3268                                  * frame alignment error FIFO overrun, or
3269                                  * missed packet.
3270                                  */
3271                                 if (isr & ED_ISR_RXE) {
3272                                         u_char rsr;
3273                                         rsr = inb(sc->nic_addr + ED_P0_RSR);
3274                                         if (rsr & ED_RSR_CRC)
3275                                                 sc->mibdata.dot3StatsFCSErrors++;
3276                                         if (rsr & ED_RSR_FAE)
3277                                                 sc->mibdata.dot3StatsAlignmentErrors++;
3278                                         if (rsr & ED_RSR_FO)
3279                                                 sc->mibdata.dot3StatsInternalMacReceiveErrors++;
3280                                         ifp->if_ierrors++;
3281 #ifdef ED_DEBUG
3282                                         printf("ed%d: receive error %x\n", ifp->if_unit,
3283                                                inb(sc->nic_addr + ED_P0_RSR));
3284 #endif
3285                                 }
3286
3287                                 /*
3288                                  * Go get the packet(s) XXX - Doing this on an
3289                                  * error is dubious because there shouldn't be
3290                                  * any data to get (we've configured the
3291                                  * interface to not accept packets with
3292                                  * errors).
3293                                  */
3294
3295                                 /*
3296                                  * Enable 16bit access to shared memory first
3297                                  * on WD/SMC boards.
3298                                  */
3299                                 if (sc->isa16bit &&
3300                                     (sc->vendor == ED_VENDOR_WD_SMC)) {
3301
3302                                         outb(sc->asic_addr + ED_WD_LAAR,
3303                                              sc->wd_laar_proto | ED_WD_LAAR_M16EN);
3304                                         if (sc->is790) {
3305                                                 outb(sc->asic_addr + ED_WD_MSR,
3306                                                      ED_WD_MSR_MENB);
3307                                         }
3308                                 }
3309                                 ed_rint(sc);
3310
3311                                 /* disable 16bit access */
3312                                 if (sc->isa16bit &&
3313                                     (sc->vendor == ED_VENDOR_WD_SMC)) {
3314
3315                                         if (sc->is790) {
3316                                                 outb(sc->asic_addr + ED_WD_MSR, 0x00);
3317                                         }
3318                                         outb(sc->asic_addr + ED_WD_LAAR,
3319                                              sc->wd_laar_proto & ~ED_WD_LAAR_M16EN);
3320                                 }
3321                         }
3322                 }
3323
3324                 /*
3325                  * If it looks like the transmitter can take more data,
3326                  * attempt to start output on the interface. This is done
3327                  * after handling the receiver to give the receiver priority.
3328                  */
3329                 if ((ifp->if_flags & IFF_OACTIVE) == 0)
3330                         ed_start(ifp);
3331
3332                 /*
3333                  * return NIC CR to standard state: page 0, remote DMA
3334                  * complete, start (toggling the TXP bit off, even if was just
3335                  * set in the transmit routine, is *okay* - it is 'edge'
3336                  * triggered from low to high)
3337                  */
3338                 outb(sc->nic_addr + ED_P0_CR, sc->cr_proto | ED_CR_STA);
3339
3340                 /*
3341                  * If the Network Talley Counters overflow, read them to reset
3342                  * them. It appears that old 8390's won't clear the ISR flag
3343                  * otherwise - resulting in an infinite loop.
3344                  */
3345                 if (isr & ED_ISR_CNT) {
3346                         (void) inb(sc->nic_addr + ED_P0_CNTR0);
3347                         (void) inb(sc->nic_addr + ED_P0_CNTR1);
3348                         (void) inb(sc->nic_addr + ED_P0_CNTR2);
3349                 }
3350         }
3351 }
3352
3353 static void 
3354 edintr(unit)
3355         int unit;
3356 {
3357         edintr_sc (&ed_softc[unit]);
3358 }
3359
3360 /*
3361  * Process an ioctl request. This code needs some work - it looks
3362  *      pretty ugly.
3363  */
3364 static int
3365 ed_ioctl(ifp, command, data)
3366         register struct ifnet *ifp;
3367         u_long     command;
3368         caddr_t data;
3369 {
3370         struct ed_softc *sc = ifp->if_softc;
3371         int     s, error = 0;
3372
3373         if (sc->gone) {
3374                 ifp->if_flags &= ~IFF_RUNNING;
3375                 return ENXIO;
3376         }
3377         s = splimp();
3378
3379         switch (command) {
3380
3381         case SIOCSIFADDR:
3382         case SIOCGIFADDR:
3383         case SIOCSIFMTU:
3384                 error = ether_ioctl(ifp, command, data);
3385                 break;
3386
3387         case SIOCSIFFLAGS:
3388
3389                 /*
3390                  * If the interface is marked up and stopped, then start it.
3391                  * If it is marked down and running, then stop it.
3392                  */
3393                 if (ifp->if_flags & IFF_UP) {
3394                         if ((ifp->if_flags & IFF_RUNNING) == 0)
3395                                 ed_init(sc);
3396                 } else {
3397                         if (ifp->if_flags & IFF_RUNNING) {
3398                                 ed_stop(sc);
3399                                 ifp->if_flags &= ~IFF_RUNNING;
3400                         }
3401                 }
3402
3403                 /*
3404                  * Promiscuous flag may have changed, so reprogram the RCR.
3405                  */
3406                 ed_setrcr(sc);
3407
3408                 /*
3409                  * An unfortunate hack to provide the (required) software
3410                  * control of the tranceiver for 3Com boards. The ALTPHYS flag
3411                  * disables the tranceiver if set.
3412                  */
3413                 if (sc->vendor == ED_VENDOR_3COM) {
3414                         if (ifp->if_flags & IFF_ALTPHYS) {
3415                                 outb(sc->asic_addr + ED_3COM_CR, 0);
3416                         } else {
3417                                 outb(sc->asic_addr + ED_3COM_CR, ED_3COM_CR_XSEL);
3418                         }
3419                 } else if (sc->vendor == ED_VENDOR_HP) 
3420                         ed_hpp_set_physical_link(sc);
3421                 break;
3422
3423         case SIOCADDMULTI:
3424         case SIOCDELMULTI:
3425                 /*
3426                  * Multicast list has changed; set the hardware filter
3427                  * accordingly.
3428                  */
3429                 ed_setrcr(sc);
3430                 error = 0;
3431                 break;
3432
3433         default:
3434                 error = EINVAL;
3435         }
3436         (void) splx(s);
3437         return (error);
3438 }
3439
3440 /*
3441  * Given a source and destination address, copy 'amount' of a packet from
3442  *      the ring buffer into a linear destination buffer. Takes into account
3443  *      ring-wrap.
3444  */
3445 static __inline char *
3446 ed_ring_copy(sc, src, dst, amount)
3447         struct ed_softc *sc;
3448         char   *src;
3449         char   *dst;
3450         u_short amount;
3451 {
3452         u_short tmp_amount;
3453
3454         /* does copy wrap to lower addr in ring buffer? */
3455         if (src + amount > sc->mem_end) {
3456                 tmp_amount = sc->mem_end - src;
3457
3458                 /* copy amount up to end of NIC memory */
3459                 if (sc->mem_shared)
3460                         bcopy(src, dst, tmp_amount);
3461                 else
3462                         ed_pio_readmem(sc, (int)src, dst, tmp_amount);
3463
3464                 amount -= tmp_amount;
3465                 src = sc->mem_ring;
3466                 dst += tmp_amount;
3467         }
3468         if (sc->mem_shared)
3469                 bcopy(src, dst, amount);
3470         else
3471                 ed_pio_readmem(sc, (int)src, dst, amount);
3472
3473         return (src + amount);
3474 }
3475
3476 /*
3477  * Retreive packet from shared memory and send to the next level up via
3478  *      ether_input(). If there is a BPF listener, give a copy to BPF, too.
3479  */
3480 static void
3481 ed_get_packet(sc, buf, len, multicast)
3482         struct ed_softc *sc;
3483         char   *buf;
3484         u_short len;
3485         int     multicast;
3486 {
3487         struct ether_header *eh;
3488         struct mbuf *m;
3489
3490         /* Allocate a header mbuf */
3491         MGETHDR(m, M_DONTWAIT, MT_DATA);
3492         if (m == NULL)
3493                 return;
3494         m->m_pkthdr.rcvif = &sc->arpcom.ac_if;
3495         m->m_pkthdr.len = m->m_len = len;
3496
3497         /*
3498          * We always put the received packet in a single buffer -
3499          * either with just an mbuf header or in a cluster attached
3500          * to the header. The +2 is to compensate for the alignment
3501          * fixup below.
3502          */
3503         if ((len + 2) > MHLEN) {
3504                 /* Attach an mbuf cluster */
3505                 MCLGET(m, M_DONTWAIT);
3506
3507                 /* Insist on getting a cluster */
3508                 if ((m->m_flags & M_EXT) == 0) {
3509                         m_freem(m);
3510                         return;
3511                 }
3512         }
3513
3514         /*
3515          * The +2 is to longword align the start of the real packet.
3516          * This is important for NFS.
3517          */
3518         m->m_data += 2;
3519         eh = mtod(m, struct ether_header *);
3520
3521 #ifdef BRIDGE
3522         /*
3523          * Get link layer header, invoke brige_in, then
3524          * depending on the outcome of the test fetch the rest of the
3525          * packet and either pass up or call bdg_forward.
3526          */
3527         if (do_bridge) {
3528                 struct ifnet *ifp ;
3529                 int need_more = 1 ; /* in case not bpf */
3530
3531                 if (sc->arpcom.ac_if.if_bpf) {
3532                         need_more = 0 ;
3533                         ed_ring_copy(sc, buf, (char *)eh, len);
3534                         bpf_mtap(&sc->arpcom.ac_if, m);
3535                 } else
3536                         ed_ring_copy(sc, buf, (char *)eh, 14);
3537                 ifp = bridge_in(m);
3538                 if (ifp == BDG_DROP) {
3539                         m_freem(m);
3540                         return ;
3541                 }
3542                 /* else fetch rest of pkt and continue */
3543                 if (need_more && len > 14)
3544                         ed_ring_copy(sc, buf+14, (char *)(eh+1), len - 14);
3545                 if (ifp != BDG_LOCAL )
3546                         bdg_forward(&m, ifp); /* not local, need forwarding */
3547                 if (ifp == BDG_LOCAL || ifp == BDG_BCAST || ifp == BDG_MCAST)
3548                         goto getit ;
3549                 /* not local and not multicast, just drop it */
3550                 if (m)
3551                         m_freem(m);
3552                 return ;
3553         }
3554 #endif
3555         /*
3556          * Get packet, including link layer address, from interface.
3557          */
3558         ed_ring_copy(sc, buf, (char *)eh, len);
3559
3560         /*
3561          * Check if there's a BPF listener on this interface. If so, hand off
3562          * the raw packet to bpf.
3563          */
3564         if (sc->arpcom.ac_if.if_bpf)
3565                 bpf_mtap(&sc->arpcom.ac_if, m);
3566         /*
3567          * If we are in promiscuous mode, we have to check whether
3568          * this packet is really for us.
3569          */
3570         if ((sc->arpcom.ac_if.if_flags & IFF_PROMISC) &&
3571                 bcmp(eh->ether_dhost, sc->arpcom.ac_enaddr,
3572                       sizeof(eh->ether_dhost)) != 0 && multicast == 0) {
3573                 m_freem(m);
3574                 return;
3575         }
3576
3577 #ifdef BRIDGE
3578 getit:
3579 #endif
3580         /*
3581          * Remove link layer address.
3582          */
3583         m->m_pkthdr.len = m->m_len = len - sizeof(struct ether_header);
3584         m->m_data += sizeof(struct ether_header);
3585
3586         ether_input(&sc->arpcom.ac_if, eh, m);
3587         return;
3588 }
3589
3590 /*
3591  * Supporting routines
3592  */
3593
3594 /*
3595  * Given a NIC memory source address and a host memory destination
3596  *      address, copy 'amount' from NIC to host using Programmed I/O.
3597  *      The 'amount' is rounded up to a word - okay as long as mbufs
3598  *              are word sized.
3599  *      This routine is currently Novell-specific.
3600  */
3601 static void
3602 ed_pio_readmem(sc, src, dst, amount)
3603         struct ed_softc *sc;
3604         int src;
3605         unsigned char *dst;
3606         unsigned short amount;
3607 {
3608         /* HP cards need special handling */
3609         if (sc->vendor == ED_VENDOR_HP && sc->type == ED_TYPE_HP_PCLANPLUS) {
3610                 ed_hpp_readmem(sc, src, dst, amount);
3611                 return;
3612         }
3613                 
3614         /* Regular Novell cards */
3615         /* select page 0 registers */
3616         outb(sc->nic_addr + ED_P0_CR, ED_CR_RD2 | ED_CR_STA);
3617
3618         /* round up to a word */
3619         if (amount & 1)
3620                 ++amount;
3621
3622         /* set up DMA byte count */
3623         outb(sc->nic_addr + ED_P0_RBCR0, amount);
3624         outb(sc->nic_addr + ED_P0_RBCR1, amount >> 8);
3625
3626         /* set up source address in NIC mem */
3627         outb(sc->nic_addr + ED_P0_RSAR0, src);
3628         outb(sc->nic_addr + ED_P0_RSAR1, src >> 8);
3629
3630         outb(sc->nic_addr + ED_P0_CR, ED_CR_RD0 | ED_CR_STA);
3631
3632         if (sc->isa16bit) {
3633 #if defined(PC98) && (NCARD > 0)
3634                 if (sc->type == ED_TYPE98_PCIC98) ED_PCIC98_16BIT_ON();
3635 #endif
3636                 insw(sc->asic_addr + ED_NOVELL_DATA, dst, amount / 2);
3637 #if defined(PC98) && (NCARD > 0)
3638                 if (sc->type == ED_TYPE98_PCIC98) ED_PCIC98_16BIT_OFF();
3639 #endif
3640         } else
3641                 insb(sc->asic_addr + ED_NOVELL_DATA, dst, amount);
3642
3643 }
3644
3645 /*
3646  * Stripped down routine for writing a linear buffer to NIC memory.
3647  *      Only used in the probe routine to test the memory. 'len' must
3648  *      be even.
3649  */
3650 static void
3651 ed_pio_writemem(sc, src, dst, len)
3652         struct ed_softc *sc;
3653         char   *src;
3654         unsigned short dst;
3655         unsigned short len;
3656 {
3657         int     maxwait = 200;  /* about 240us */
3658
3659         if (sc->vendor == ED_VENDOR_NOVELL) {
3660
3661                 /* select page 0 registers */
3662                 outb(sc->nic_addr + ED_P0_CR, ED_CR_RD2 | ED_CR_STA);
3663
3664                 /* reset remote DMA complete flag */
3665                 outb(sc->nic_addr + ED_P0_ISR, ED_ISR_RDC);
3666
3667                 /* set up DMA byte count */
3668                 outb(sc->nic_addr + ED_P0_RBCR0, len);
3669                 outb(sc->nic_addr + ED_P0_RBCR1, len >> 8);
3670
3671                 /* set up destination address in NIC mem */
3672                 outb(sc->nic_addr + ED_P0_RSAR0, dst);
3673                 outb(sc->nic_addr + ED_P0_RSAR1, dst >> 8);
3674
3675                 /* set remote DMA write */
3676                 outb(sc->nic_addr + ED_P0_CR, ED_CR_RD1 | ED_CR_STA);
3677
3678                 if (sc->isa16bit)
3679 #if defined(PC98) && (NCARD > 0)
3680                 {
3681                         if (sc->type == ED_TYPE98_PCIC98) ED_PCIC98_16BIT_ON();
3682 #endif
3683                         outsw(sc->asic_addr + ED_NOVELL_DATA, src, len / 2);
3684 #if defined(PC98) && (NCARD > 0)
3685                         if (sc->type == ED_TYPE98_PCIC98) ED_PCIC98_16BIT_OFF();
3686                 }
3687 #endif  
3688                 else
3689                         outsb(sc->asic_addr + ED_NOVELL_DATA, src, len);
3690
3691                 /*
3692                  * Wait for remote DMA complete. This is necessary because on the
3693                  * transmit side, data is handled internally by the NIC in bursts and
3694                  * we can't start another remote DMA until this one completes. Not
3695                  * waiting causes really bad things to happen - like the NIC
3696                  * irrecoverably jamming the ISA bus.
3697                  */
3698                 while (((inb(sc->nic_addr + ED_P0_ISR) & ED_ISR_RDC) != ED_ISR_RDC) && --maxwait);
3699
3700         } else if ((sc->vendor == ED_VENDOR_HP) && 
3701                    (sc->type == ED_TYPE_HP_PCLANPLUS)) { 
3702
3703                 /* HP PCLAN+ */
3704
3705                 /* reset remote DMA complete flag */
3706                 outb(sc->nic_addr + ED_P0_ISR, ED_ISR_RDC);
3707
3708                 /* program the write address in RAM */
3709                 outw(sc->asic_addr + ED_HPP_PAGE_0, dst);
3710
3711                 if (sc->hpp_mem_start) {
3712                         u_short *s = (u_short *) src;
3713                         volatile u_short *d = (u_short *) sc->hpp_mem_start;
3714                         u_short *const fence = s + (len >> 1);
3715
3716                         /*
3717                          * Enable memory mapped access.
3718                          */
3719
3720                         outw(sc->asic_addr + ED_HPP_OPTION, 
3721                              sc->hpp_options & 
3722                                 ~(ED_HPP_OPTION_MEM_DISABLE | 
3723                                   ED_HPP_OPTION_BOOT_ROM_ENB));
3724
3725                         /*
3726                          * Copy to NIC memory.
3727                          */
3728
3729                         while (s < fence)
3730                                 *d = *s++;
3731
3732                         /*
3733                          * Restore Boot ROM access.
3734                          */
3735
3736                         outw(sc->asic_addr + ED_HPP_OPTION,
3737                              sc->hpp_options);
3738
3739                 } else {
3740                         /* write data using I/O writes */
3741                         outsw(sc->asic_addr + ED_HPP_PAGE_4, src, len / 2);
3742                 }
3743
3744         }
3745 }
3746
3747 /*
3748  * Write an mbuf chain to the destination NIC memory address using
3749  *      programmed I/O.
3750  */
3751 static u_short
3752 ed_pio_write_mbufs(sc, m, dst)
3753         struct ed_softc *sc;
3754         struct mbuf *m;
3755         int dst;
3756 {
3757         struct ifnet *ifp = (struct ifnet *)sc;
3758         unsigned short total_len, dma_len;
3759         struct mbuf *mp;
3760         int     maxwait = 200;  /* about 240us */
3761
3762         /*  HP PC Lan+ cards need special handling */
3763         if ((sc->vendor == ED_VENDOR_HP) && 
3764             (sc->type == ED_TYPE_HP_PCLANPLUS)) {
3765                 return ed_hpp_write_mbufs(sc, m, dst);
3766         }
3767
3768         /* First, count up the total number of bytes to copy */
3769         for (total_len = 0, mp = m; mp; mp = mp->m_next)
3770                 total_len += mp->m_len;
3771
3772         dma_len = total_len;
3773         if (sc->isa16bit && (dma_len & 1))
3774                 dma_len++;
3775
3776         /* select page 0 registers */
3777         outb(sc->nic_addr + ED_P0_CR, ED_CR_RD2 | ED_CR_STA);
3778
3779         /* reset remote DMA complete flag */
3780         outb(sc->nic_addr + ED_P0_ISR, ED_ISR_RDC);
3781
3782         /* set up DMA byte count */
3783         outb(sc->nic_addr + ED_P0_RBCR0, dma_len);
3784         outb(sc->nic_addr + ED_P0_RBCR1, dma_len >> 8);
3785
3786         /* set up destination address in NIC mem */
3787         outb(sc->nic_addr + ED_P0_RSAR0, dst);
3788         outb(sc->nic_addr + ED_P0_RSAR1, dst >> 8);
3789
3790         /* set remote DMA write */
3791         outb(sc->nic_addr + ED_P0_CR, ED_CR_RD1 | ED_CR_STA);
3792
3793   /*
3794    * Transfer the mbuf chain to the NIC memory.
3795    * 16-bit cards require that data be transferred as words, and only words.
3796    * So that case requires some extra code to patch over odd-length mbufs.
3797    */
3798
3799         if (!sc->isa16bit) {
3800                 /* NE1000s are easy */
3801                 while (m) {
3802                         if (m->m_len) {
3803                                 outsb(sc->asic_addr + ED_NOVELL_DATA,
3804                                       m->m_data, m->m_len);
3805                         }
3806                         m = m->m_next;
3807                 }
3808         } else {
3809                 /* NE2000s are a pain */
3810                 unsigned char *data;
3811                 int len, wantbyte;
3812                 unsigned char savebyte[2];
3813
3814                 wantbyte = 0;
3815
3816 #if defined(PC98) && (NCARD > 0)
3817                 if (sc->type == ED_TYPE98_PCIC98) ED_PCIC98_16BIT_ON();
3818 #endif
3819                 while (m) {
3820                         len = m->m_len;
3821                         if (len) {
3822                                 data = mtod(m, caddr_t);
3823                                 /* finish the last word */
3824                                 if (wantbyte) {
3825                                         savebyte[1] = *data;
3826                                         outw(sc->asic_addr + ED_NOVELL_DATA, *(u_short *)savebyte);
3827                                         data++;
3828                                         len--;
3829                                         wantbyte = 0;
3830                                 }
3831                                 /* output contiguous words */
3832                                 if (len > 1) {
3833                                         outsw(sc->asic_addr + ED_NOVELL_DATA,
3834                                               data, len >> 1);
3835                                         data += len & ~1;
3836                                         len &= 1;
3837                                 }
3838                                 /* save last byte, if necessary */
3839                                 if (len == 1) {
3840                                         savebyte[0] = *data;
3841                                         wantbyte = 1;
3842                                 }
3843                         }
3844                         m = m->m_next;
3845                 }
3846                 /* spit last byte */
3847                 if (wantbyte) {
3848                         outw(sc->asic_addr + ED_NOVELL_DATA, *(u_short *)savebyte);
3849                 }
3850 #if defined(PC98) && (NCARD > 0)
3851                 if (sc->type == ED_TYPE98_PCIC98) ED_PCIC98_16BIT_OFF();
3852 #endif
3853         }
3854
3855         /*
3856          * Wait for remote DMA complete. This is necessary because on the
3857          * transmit side, data is handled internally by the NIC in bursts and
3858          * we can't start another remote DMA until this one completes. Not
3859          * waiting causes really bad things to happen - like the NIC
3860          * irrecoverably jamming the ISA bus.
3861          */
3862         while (((inb(sc->nic_addr + ED_P0_ISR) & ED_ISR_RDC) != ED_ISR_RDC) && --maxwait);
3863
3864         if (!maxwait) {
3865                 log(LOG_WARNING, "ed%d: remote transmit DMA failed to complete\n",
3866                     ifp->if_unit);
3867                 ed_reset(ifp);
3868                 return(0);
3869         }
3870         return (total_len);
3871 }
3872
3873 /*
3874  * Support routines to handle the HP PC Lan+ card.
3875  */
3876
3877 /*
3878  * HP PC Lan+: Read from NIC memory, using either PIO or memory mapped
3879  * IO.
3880  */
3881
3882 static void
3883 ed_hpp_readmem(sc, src, dst, amount)
3884         struct ed_softc *sc; 
3885         unsigned short src;
3886         unsigned char *dst;
3887         unsigned short amount;
3888 {
3889
3890         int use_32bit_access = !(sc->hpp_id & ED_HPP_ID_16_BIT_ACCESS);
3891
3892
3893         /* Program the source address in RAM */
3894         outw(sc->asic_addr + ED_HPP_PAGE_2, src);
3895
3896         /*
3897          * The HP PC Lan+ card supports word reads as well as
3898          * a memory mapped i/o port that is aliased to every 
3899          * even address on the board.
3900          */
3901
3902         if (sc->hpp_mem_start) {
3903
3904                 /* Enable memory mapped access.  */
3905                 outw(sc->asic_addr + ED_HPP_OPTION, 
3906                      sc->hpp_options & 
3907                         ~(ED_HPP_OPTION_MEM_DISABLE | 
3908                           ED_HPP_OPTION_BOOT_ROM_ENB));
3909
3910                 if (use_32bit_access && (amount > 3)) {
3911                         u_long *dl = (u_long *) dst;    
3912                         volatile u_long *const sl = 
3913                                 (u_long *) sc->hpp_mem_start;
3914                         u_long *const fence = dl + (amount >> 2);
3915                         
3916                         /* Copy out NIC data.  We could probably write this
3917                            as a `movsl'. The currently generated code is lousy.
3918                            */
3919
3920                         while (dl < fence)
3921                                 *dl++ = *sl;
3922                 
3923                         dst += (amount & ~3);
3924                         amount &= 3;
3925
3926                 } 
3927
3928                 /* Finish off any words left, as a series of short reads */
3929                 if (amount > 1) {
3930                         u_short *d = (u_short *) dst;   
3931                         volatile u_short *const s = 
3932                                 (u_short *) sc->hpp_mem_start;
3933                         u_short *const fence = d + (amount >> 1);
3934                         
3935                         /* Copy out NIC data.  */
3936
3937                         while (d < fence)
3938                                 *d++ = *s;
3939         
3940                         dst += (amount & ~1);
3941                         amount &= 1;
3942                 }
3943
3944                 /*
3945                  * read in a byte; however we need to always read 16 bits
3946                  * at a time or the hardware gets into a funny state
3947                  */
3948
3949                 if (amount == 1) {
3950                         /* need to read in a short and copy LSB */
3951                         volatile u_short *const s = 
3952                                 (volatile u_short *) sc->hpp_mem_start;
3953                         
3954                         *dst = (*s) & 0xFF;     
3955                 }
3956
3957                 /* Restore Boot ROM access.  */
3958
3959                 outw(sc->asic_addr + ED_HPP_OPTION,
3960                      sc->hpp_options);
3961
3962
3963         } else { 
3964                 /* Read in data using the I/O port */
3965                 if (use_32bit_access && (amount > 3)) {
3966                         insl(sc->asic_addr + ED_HPP_PAGE_4, dst, amount >> 2);
3967                         dst += (amount & ~3);
3968                         amount &= 3;
3969                 }
3970                 if (amount > 1) {
3971                         insw(sc->asic_addr + ED_HPP_PAGE_4, dst, amount >> 1);
3972                         dst += (amount & ~1);
3973                         amount &= 1;
3974                 }
3975                 if (amount == 1) { /* read in a short and keep the LSB */
3976                         *dst = inw(sc->asic_addr + ED_HPP_PAGE_4) & 0xFF;
3977                 }
3978         }
3979 }
3980
3981 /*
3982  * Write to HP PC Lan+ NIC memory.  Access to the NIC can be by using 
3983  * outsw() or via the memory mapped interface to the same register.
3984  * Writes have to be in word units; byte accesses won't work and may cause
3985  * the NIC to behave wierdly. Long word accesses are permitted if the ASIC
3986  * allows it.
3987  */
3988
3989 static u_short
3990 ed_hpp_write_mbufs(struct ed_softc *sc, struct mbuf *m, int dst)
3991 {
3992         int len, wantbyte;
3993         unsigned short total_len;
3994         unsigned char savebyte[2];
3995         volatile u_short * const d = 
3996                 (volatile u_short *) sc->hpp_mem_start;
3997         int use_32bit_accesses = !(sc->hpp_id & ED_HPP_ID_16_BIT_ACCESS);
3998
3999         /* select page 0 registers */
4000         outb(sc->nic_addr + ED_P0_CR, sc->cr_proto | ED_CR_STA);
4001
4002         /* reset remote DMA complete flag */
4003         outb(sc->nic_addr + ED_P0_ISR, ED_ISR_RDC);
4004
4005         /* program the write address in RAM */
4006         outw(sc->asic_addr + ED_HPP_PAGE_0, dst);
4007
4008         if (sc->hpp_mem_start)  /* enable memory mapped I/O */
4009                 outw(sc->asic_addr + ED_HPP_OPTION, sc->hpp_options & 
4010                         ~(ED_HPP_OPTION_MEM_DISABLE |
4011                         ED_HPP_OPTION_BOOT_ROM_ENB));
4012
4013         wantbyte = 0;
4014         total_len = 0;
4015
4016         if (sc->hpp_mem_start) {        /* Memory mapped I/O port */
4017                 while (m) {
4018                         total_len += (len = m->m_len);
4019                         if (len) {
4020                                 caddr_t data = mtod(m, caddr_t);
4021                                 /* finish the last word of the previous mbuf */
4022                                 if (wantbyte) {
4023                                         savebyte[1] = *data;
4024                                         *d = *((ushort *) savebyte);
4025                                         data++; len--; wantbyte = 0;
4026                                 }
4027                                 /* output contiguous words */
4028                                 if ((len > 3) && (use_32bit_accesses)) {
4029                                         volatile u_long *const dl = 
4030                                                 (volatile u_long *) d;
4031                                         u_long *sl = (u_long *) data;
4032                                         u_long *fence = sl + (len >> 2);
4033
4034                                         while (sl < fence)
4035                                                 *dl = *sl++;
4036
4037                                         data += (len & ~3);
4038                                         len &= 3;
4039                                 }
4040                                 /* finish off remain 16 bit writes */
4041                                 if (len > 1) {
4042                                         u_short *s = (u_short *) data;
4043                                         u_short *fence = s + (len >> 1);
4044
4045                                         while (s < fence)
4046                                                 *d = *s++;
4047
4048                                         data += (len & ~1); 
4049                                         len &= 1;
4050                                 }
4051                                 /* save last byte if needed */
4052                                 if ((wantbyte = (len == 1)) != 0)
4053                                         savebyte[0] = *data;
4054                         }
4055                         m = m->m_next;  /* to next mbuf */
4056                 }
4057                 if (wantbyte) /* write last byte */
4058                         *d = *((u_short *) savebyte);
4059         } else {
4060                 /* use programmed I/O */
4061                 while (m) {
4062                         total_len += (len = m->m_len);
4063                         if (len) {
4064                                 caddr_t data = mtod(m, caddr_t);
4065                                 /* finish the last word of the previous mbuf */
4066                                 if (wantbyte) {
4067                                         savebyte[1] = *data;
4068                                         outw(sc->asic_addr + ED_HPP_PAGE_4, 
4069                                              *((u_short *)savebyte));
4070                                         data++; 
4071                                         len--; 
4072                                         wantbyte = 0;
4073                                 }
4074                                 /* output contiguous words */
4075                                 if ((len > 3) && use_32bit_accesses) {
4076                                         outsl(sc->asic_addr + ED_HPP_PAGE_4,
4077                                                 data, len >> 2);
4078                                         data += (len & ~3);
4079                                         len &= 3;
4080                                 }
4081                                 /* finish off remaining 16 bit accesses */
4082                                 if (len > 1) {
4083                                         outsw(sc->asic_addr + ED_HPP_PAGE_4,
4084                                               data, len >> 1);
4085                                         data += (len & ~1);
4086                                         len &= 1;
4087                                 }
4088                                 if ((wantbyte = (len == 1)) != 0)
4089                                         savebyte[0] = *data;
4090
4091                         } /* if len != 0 */
4092                         m = m->m_next;
4093                 }
4094                 if (wantbyte) /* spit last byte */
4095                         outw(sc->asic_addr + ED_HPP_PAGE_4, 
4096                                 *(u_short *)savebyte);
4097
4098         }
4099
4100         if (sc->hpp_mem_start)  /* turn off memory mapped i/o */
4101                 outw(sc->asic_addr + ED_HPP_OPTION,
4102                      sc->hpp_options);
4103
4104         return (total_len);
4105 }
4106
4107 static void
4108 ed_setrcr(sc)
4109         struct ed_softc *sc;
4110 {
4111         struct ifnet *ifp = (struct ifnet *)sc;
4112         int     i;
4113
4114         /* set page 1 registers */
4115         outb(sc->nic_addr + ED_P0_CR, sc->cr_proto | ED_CR_PAGE_1 | ED_CR_STP);
4116
4117         if (ifp->if_flags & IFF_PROMISC) {
4118
4119                 /*
4120                  * Reconfigure the multicast filter.
4121                  */
4122                 for (i = 0; i < 8; i++)
4123                         outb(sc->nic_addr + ED_P1_MAR(i), 0xff);
4124
4125                 /*
4126                  * And turn on promiscuous mode. Also enable reception of
4127                  * runts and packets with CRC & alignment errors.
4128                  */
4129                 /* Set page 0 registers */
4130                 outb(sc->nic_addr + ED_P0_CR, sc->cr_proto | ED_CR_STP);
4131
4132                 outb(sc->nic_addr + ED_P0_RCR, ED_RCR_PRO | ED_RCR_AM |
4133                      ED_RCR_AB | ED_RCR_AR | ED_RCR_SEP);
4134         } else {
4135                 /* set up multicast addresses and filter modes */
4136                 if (ifp->if_flags & IFF_MULTICAST) {
4137                         u_long  mcaf[2];
4138
4139                         if (ifp->if_flags & IFF_ALLMULTI) {
4140                                 mcaf[0] = 0xffffffff;
4141                                 mcaf[1] = 0xffffffff;
4142                         } else
4143                                 ds_getmcaf(sc, mcaf);
4144
4145                         /*
4146                          * Set multicast filter on chip.
4147                          */
4148                         for (i = 0; i < 8; i++)
4149                                 outb(sc->nic_addr + ED_P1_MAR(i), ((u_char *) mcaf)[i]);
4150
4151                         /* Set page 0 registers */
4152                         outb(sc->nic_addr + ED_P0_CR, sc->cr_proto | ED_CR_STP);
4153
4154                         outb(sc->nic_addr + ED_P0_RCR, ED_RCR_AM | ED_RCR_AB);
4155                 } else {
4156
4157                         /*
4158                          * Initialize multicast address hashing registers to
4159                          * not accept multicasts.
4160                          */
4161                         for (i = 0; i < 8; ++i)
4162                                 outb(sc->nic_addr + ED_P1_MAR(i), 0x00);
4163
4164                         /* Set page 0 registers */
4165                         outb(sc->nic_addr + ED_P0_CR, sc->cr_proto | ED_CR_STP);
4166
4167                         outb(sc->nic_addr + ED_P0_RCR, ED_RCR_AB);
4168                 }
4169         }
4170
4171         /*
4172          * Start interface.
4173          */
4174         outb(sc->nic_addr + ED_P0_CR, sc->cr_proto | ED_CR_STA);
4175 }
4176
4177 /*
4178  * Compute crc for ethernet address
4179  */
4180 static u_long
4181 ds_crc(ep)
4182         u_char *ep;
4183 {
4184 #define POLYNOMIAL 0x04c11db6
4185         register u_long crc = 0xffffffffL;
4186         register int carry, i, j;
4187         register u_char b;
4188
4189         for (i = 6; --i >= 0;) {
4190                 b = *ep++;
4191                 for (j = 8; --j >= 0;) {
4192                         carry = ((crc & 0x80000000L) ? 1 : 0) ^ (b & 0x01);
4193                         crc <<= 1;
4194                         b >>= 1;
4195                         if (carry)
4196                                 crc = ((crc ^ POLYNOMIAL) | carry);
4197                 }
4198         }
4199         return crc;
4200 #undef POLYNOMIAL
4201 }
4202
4203 /*
4204  * Compute the multicast address filter from the
4205  * list of multicast addresses we need to listen to.
4206  */
4207 static void
4208 ds_getmcaf(sc, mcaf)
4209         struct ed_softc *sc;
4210         u_long *mcaf;
4211 {
4212         register u_int index;
4213         register u_char *af = (u_char *) mcaf;
4214         struct ifmultiaddr *ifma;
4215
4216         mcaf[0] = 0;
4217         mcaf[1] = 0;
4218
4219         for (ifma = sc->arpcom.ac_if.if_multiaddrs.lh_first; ifma;
4220              ifma = ifma->ifma_link.le_next) {
4221                 if (ifma->ifma_addr->sa_family != AF_LINK)
4222                         continue;
4223                 index = ds_crc(LLADDR((struct sockaddr_dl *)ifma->ifma_addr))
4224                         >> 26;
4225                 af[index >> 3] |= 1 << (index & 7);
4226         }
4227 }
4228
4229 /*
4230  * support PnP cards if we are using 'em
4231  */
4232
4233 #if NPNP > 0
4234
4235 static pnpid_t edpnp_ids[] = {
4236         { 0xd680d041, "NE2000"},
4237         { 0 }
4238 };
4239
4240 static char *edpnp_probe(u_long csn, u_long vend_id);
4241 static void edpnp_attach(u_long csn, u_long vend_id, char *name,
4242         struct isa_device *dev);
4243 static u_long nedpnp = NED;
4244
4245 static struct pnp_device edpnp = {
4246         "edpnp",
4247         edpnp_probe,
4248         edpnp_attach,
4249         &nedpnp,
4250         &net_imask
4251 };
4252 DATA_SET (pnpdevice_set, edpnp);
4253
4254 static char *
4255 edpnp_probe(u_long csn, u_long vend_id)
4256 {
4257         pnpid_t *id;
4258         char *s = NULL;
4259
4260         for(id = edpnp_ids; id->vend_id != 0; id++) {
4261                 if (vend_id == id->vend_id) {
4262                         s = id->id_str;
4263                         break;
4264                 }
4265         }
4266
4267         if (s) {
4268                 struct pnp_cinfo d;
4269                 read_pnp_parms(&d, 0);
4270                 if (d.enable == 0 || d.flags & 1) {
4271                         printf("CSN %lu is disabled.\n", csn);
4272                         return (NULL);
4273                 }
4274
4275         }
4276
4277         return (s);
4278 }
4279
4280 static void
4281 edpnp_attach(u_long csn, u_long vend_id, char *name, struct isa_device *dev)
4282 {
4283         struct pnp_cinfo d;
4284
4285         if (dev->id_unit >= NED)
4286                 return;
4287
4288         if (read_pnp_parms(&d, 0) == 0) {
4289                 printf("failed to read pnp parms\n");
4290                 return;
4291         }
4292
4293         write_pnp_parms(&d, 0);
4294
4295         enable_pnp_card();
4296
4297         dev->id_iobase = d.port[0];
4298         dev->id_irq = (1 << d.irq[0]);
4299         dev->id_ointr = edintr;
4300         dev->id_drq = -1;
4301
4302         if (dev->id_driver == NULL) {
4303                 dev->id_driver = &eddriver;
4304                 dev->id_id = isa_compat_nextid();
4305         }
4306
4307         if (ed_probe(dev) != 0)
4308                 ed_attach_isa(dev);
4309         else
4310                 printf("ed%d: probe failed\n", dev->id_unit);
4311 }
4312 #endif