9833d3f0ccb5c8f55e02babdc9391883f3db6f52
[dragonfly.git] / sys / dev / netif / ed / 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: src/sys/dev/ed/if_ed.c,v 1.224 2003/12/08 07:54:12 obrien Exp $
28  * $DragonFly: src/sys/dev/netif/ed/if_ed.c,v 1.23 2005/06/14 11:08:40 joerg Exp $
29  */
30
31 /*
32  * Device driver for National Semiconductor DS8390/WD83C690 based ethernet
33  *   adapters. By David Greenman, 29-April-1993
34  *
35  * Currently supports the Western Digital/SMC 8003 and 8013 series,
36  *   the SMC Elite Ultra (8216), the 3Com 3c503, the NE1000 and NE2000,
37  *   and a variety of similar clones.
38  *
39  */
40
41 #include "opt_ed.h"
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/sockio.h>
46 #include <sys/mbuf.h>
47 #include <sys/kernel.h>
48 #include <sys/socket.h>
49 #include <sys/syslog.h>
50 #include <sys/thread2.h>
51
52 #include <sys/module.h>
53 #include <sys/bus.h>
54
55 #include <machine/bus.h>
56 #include <sys/rman.h>
57 #include <machine/resource.h>
58
59 #include <net/ethernet.h>
60 #include <net/if.h>
61 #include <net/ifq_var.h>
62 #include <net/if_arp.h>
63 #include <net/if_dl.h>
64 #include <net/if_mib.h>
65 #include <net/if_media.h>
66
67 #ifndef ED_NO_MIIBUS
68 #include <dev/netif/mii_layer/mii.h>
69 #include <dev/netif/mii_layer/miivar.h>
70 #endif
71
72 #include <net/bpf.h>
73 #include "opt_bdg.h"
74 #include <net/bridge/bridge.h>
75
76 #include <machine/md_var.h>
77
78 #include "if_edreg.h"
79 #include "if_edvar.h"
80
81 devclass_t ed_devclass;
82
83 static void     ed_init         (void *);
84 static int      ed_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
85 static void     ed_start        (struct ifnet *);
86 static void     ed_reset        (struct ifnet *);
87 static void     ed_watchdog     (struct ifnet *);
88 #ifndef ED_NO_MIIBUS
89 static void     ed_tick         (void *);
90 #endif
91
92 static void     ds_getmcaf      (struct ed_softc *, u_int32_t *);
93
94 static void     ed_get_packet   (struct ed_softc *, char *, /* u_short */ int);
95
96 static __inline void    ed_rint (struct ed_softc *);
97 static __inline void    ed_xmit (struct ed_softc *);
98 static __inline char *  ed_ring_copy (struct ed_softc *, char *, char *,
99                                           /* u_short */ int);
100 static void     ed_hpp_set_physical_link (struct ed_softc *);
101 static void     ed_hpp_readmem  (struct ed_softc *, int, unsigned char *,
102                                     /* u_short */ int);
103 static void     ed_hpp_writemem (struct ed_softc *, unsigned char *,
104                                     /* u_short */ int, /* u_short */ int);
105 static u_short  ed_hpp_write_mbufs (struct ed_softc *, struct mbuf *,
106                                         int);
107
108 static u_short  ed_pio_write_mbufs (struct ed_softc *, struct mbuf *,
109                                         int);
110
111 static void     ed_setrcr       (struct ed_softc *);
112
113 static uint32_t ds_mchash       (const uint8_t *);
114
115 DECLARE_DUMMY_MODULE(if_ed);
116
117 /*
118  * Interrupt conversion table for WD/SMC ASIC/83C584
119  */
120 static unsigned short ed_intr_val[] = {
121         9,
122         3,
123         5,
124         7,
125         10,
126         11,
127         15,
128         4
129 };
130
131 /*
132  * Interrupt conversion table for 83C790
133  */
134 static unsigned short ed_790_intr_val[] = {
135         0,
136         9,
137         3,
138         5,
139         7,
140         10,
141         11,
142         15
143 };
144
145 /*
146  * Interrupt conversion table for the HP PC LAN+
147  */
148
149 static unsigned short ed_hpp_intr_val[] = {
150         0,              /* 0 */
151         0,              /* 1 */
152         0,              /* 2 */
153         3,              /* 3 */
154         4,              /* 4 */
155         5,              /* 5 */
156         6,              /* 6 */
157         7,              /* 7 */
158         0,              /* 8 */
159         9,              /* 9 */
160         10,             /* 10 */
161         11,             /* 11 */
162         12,             /* 12 */
163         0,              /* 13 */
164         0,              /* 14 */
165         15              /* 15 */
166 };
167
168 /*
169  * Generic probe routine for testing for the existance of a DS8390.
170  *      Must be called after the NIC has just been reset. This routine
171  *      works by looking at certain register values that are guaranteed
172  *      to be initialized a certain way after power-up or reset. Seems
173  *      not to currently work on the 83C690.
174  *
175  * Specifically:
176  *
177  *      Register                        reset bits      set bits
178  *      Command Register (CR)           TXP, STA        RD2, STP
179  *      Interrupt Status (ISR)                          RST
180  *      Interrupt Mask (IMR)            All bits
181  *      Data Control (DCR)                              LAS
182  *      Transmit Config. (TCR)          LB1, LB0
183  *
184  * We only look at the CR and ISR registers, however, because looking at
185  *      the others would require changing register pages (which would be
186  *      intrusive if this isn't an 8390).
187  *
188  * Return 1 if 8390 was found, 0 if not.
189  */
190
191 int
192 ed_probe_generic8390(sc)
193         struct ed_softc *sc;
194 {
195         if ((ed_nic_inb(sc, ED_P0_CR) &
196              (ED_CR_RD2 | ED_CR_TXP | ED_CR_STA | ED_CR_STP)) !=
197             (ED_CR_RD2 | ED_CR_STP))
198                 return (0);
199         if ((ed_nic_inb(sc, ED_P0_ISR) & ED_ISR_RST) != ED_ISR_RST)
200                 return (0);
201
202         return (1);
203 }
204
205 /*
206  * Probe and vendor-specific initialization routine for SMC/WD80x3 boards
207  */
208 int
209 ed_probe_WD80x3_generic(dev, flags, intr_vals)
210         device_t dev;
211         int flags;
212         unsigned short *intr_vals[];
213 {
214         struct ed_softc *sc = device_get_softc(dev);
215         int     error;
216         int     i;
217         u_int   memsize, maddr;
218         u_char  iptr, isa16bit, sum, totalsum;
219         u_long  conf_maddr, conf_msize, irq, junk;
220
221         sc->chip_type = ED_CHIP_TYPE_DP8390;
222
223         if (ED_FLAGS_GETTYPE(flags) == ED_FLAGS_TOSH_ETHER) {
224                 totalsum = ED_WD_ROM_CHECKSUM_TOTAL_TOSH_ETHER;
225                 ed_asic_outb(sc, ED_WD_MSR, ED_WD_MSR_POW);
226                 DELAY(10000);
227         }
228         else
229                 totalsum = ED_WD_ROM_CHECKSUM_TOTAL;
230
231         /*
232          * Attempt to do a checksum over the station address PROM. If it
233          * fails, it's probably not a SMC/WD board. There is a problem with
234          * this, though: some clone WD boards don't pass the checksum test.
235          * Danpex boards for one.
236          */
237         for (sum = 0, i = 0; i < 8; ++i)
238                 sum += ed_asic_inb(sc, ED_WD_PROM + i);
239
240         if (sum != totalsum) {
241
242                 /*
243                  * Checksum is invalid. This often happens with cheap WD8003E
244                  * clones.  In this case, the checksum byte (the eighth byte)
245                  * seems to always be zero.
246                  */
247                 if (ed_asic_inb(sc, ED_WD_CARD_ID) != ED_TYPE_WD8003E ||
248                     ed_asic_inb(sc, ED_WD_PROM + 7) != 0)
249                         return (ENXIO);
250         }
251         /* reset card to force it into a known state. */
252         if (ED_FLAGS_GETTYPE(flags) == ED_FLAGS_TOSH_ETHER)
253                 ed_asic_outb(sc, ED_WD_MSR, ED_WD_MSR_RST | ED_WD_MSR_POW);
254         else
255                 ed_asic_outb(sc, ED_WD_MSR, ED_WD_MSR_RST);
256
257         DELAY(100);
258         ed_asic_outb(sc, ED_WD_MSR, ed_asic_inb(sc, ED_WD_MSR) & ~ED_WD_MSR_RST);
259         /* wait in the case this card is reading its EEROM */
260         DELAY(5000);
261
262         sc->vendor = ED_VENDOR_WD_SMC;
263         sc->type = ed_asic_inb(sc, ED_WD_CARD_ID);
264
265         /*
266          * Set initial values for width/size.
267          */
268         memsize = 8192;
269         isa16bit = 0;
270         switch (sc->type) {
271         case ED_TYPE_WD8003S:
272                 sc->type_str = "WD8003S";
273                 break;
274         case ED_TYPE_WD8003E:
275                 sc->type_str = "WD8003E";
276                 break;
277         case ED_TYPE_WD8003EB:
278                 sc->type_str = "WD8003EB";
279                 break;
280         case ED_TYPE_WD8003W:
281                 sc->type_str = "WD8003W";
282                 break;
283         case ED_TYPE_WD8013EBT:
284                 sc->type_str = "WD8013EBT";
285                 memsize = 16384;
286                 isa16bit = 1;
287                 break;
288         case ED_TYPE_WD8013W:
289                 sc->type_str = "WD8013W";
290                 memsize = 16384;
291                 isa16bit = 1;
292                 break;
293         case ED_TYPE_WD8013EP:  /* also WD8003EP */
294                 if (ed_asic_inb(sc, ED_WD_ICR) & ED_WD_ICR_16BIT) {
295                         isa16bit = 1;
296                         memsize = 16384;
297                         sc->type_str = "WD8013EP";
298                 } else {
299                         sc->type_str = "WD8003EP";
300                 }
301                 break;
302         case ED_TYPE_WD8013WC:
303                 sc->type_str = "WD8013WC";
304                 memsize = 16384;
305                 isa16bit = 1;
306                 break;
307         case ED_TYPE_WD8013EBP:
308                 sc->type_str = "WD8013EBP";
309                 memsize = 16384;
310                 isa16bit = 1;
311                 break;
312         case ED_TYPE_WD8013EPC:
313                 sc->type_str = "WD8013EPC";
314                 memsize = 16384;
315                 isa16bit = 1;
316                 break;
317         case ED_TYPE_SMC8216C: /* 8216 has 16K shared mem -- 8416 has 8K */
318         case ED_TYPE_SMC8216T:
319                 if (sc->type == ED_TYPE_SMC8216C) {
320                         sc->type_str = "SMC8216/SMC8216C";
321                 } else {
322                         sc->type_str = "SMC8216T";
323                 }
324
325                 ed_asic_outb(sc, ED_WD790_HWR,
326                     ed_asic_inb(sc, ED_WD790_HWR) | ED_WD790_HWR_SWH);
327                 switch (ed_asic_inb(sc, ED_WD790_RAR) & ED_WD790_RAR_SZ64) {
328                 case ED_WD790_RAR_SZ64:
329                         memsize = 65536;
330                         break;
331                 case ED_WD790_RAR_SZ32:
332                         memsize = 32768;
333                         break;
334                 case ED_WD790_RAR_SZ16:
335                         memsize = 16384;
336                         break;
337                 case ED_WD790_RAR_SZ8:
338                         /* 8216 has 16K shared mem -- 8416 has 8K */
339                         if (sc->type == ED_TYPE_SMC8216C) {
340                                 sc->type_str = "SMC8416C/SMC8416BT";
341                         } else {
342                                 sc->type_str = "SMC8416T";
343                         }
344                         memsize = 8192;
345                         break;
346                 }
347                 ed_asic_outb(sc, ED_WD790_HWR,
348                     ed_asic_inb(sc, ED_WD790_HWR) & ~ED_WD790_HWR_SWH);
349
350                 isa16bit = 1;
351                 sc->chip_type = ED_CHIP_TYPE_WD790;
352                 break;
353         case ED_TYPE_TOSHIBA1:
354                 sc->type_str = "Toshiba1";
355                 memsize = 32768;
356                 isa16bit = 1;
357                 break;
358         case ED_TYPE_TOSHIBA4:
359                 sc->type_str = "Toshiba4";
360                 memsize = 32768;
361                 isa16bit = 1;
362                 break;
363         default:
364                 sc->type_str = "";
365                 break;
366         }
367
368         /*
369          * Make some adjustments to initial values depending on what is found
370          * in the ICR.
371          */
372         if (isa16bit && (sc->type != ED_TYPE_WD8013EBT)
373           && (sc->type != ED_TYPE_TOSHIBA1) && (sc->type != ED_TYPE_TOSHIBA4)
374             && ((ed_asic_inb(sc, ED_WD_ICR) & ED_WD_ICR_16BIT) == 0)) {
375                 isa16bit = 0;
376                 memsize = 8192;
377         }
378
379         error = bus_get_resource(dev, SYS_RES_MEMORY, 0,
380                                  &conf_maddr, &conf_msize);
381         if (error)
382                 return (error);
383
384 #if ED_DEBUG
385         printf("type = %x type_str=%s isa16bit=%d memsize=%d id_msize=%d\n",
386                sc->type, sc->type_str, isa16bit, memsize, conf_msize);
387         for (i = 0; i < 8; i++)
388                 printf("%x -> %x\n", i, ed_asic_inb(sc, i));
389 #endif
390
391         /*
392          * Allow the user to override the autoconfiguration
393          */
394         if (conf_msize > 1)
395                 memsize = conf_msize;
396
397         maddr = conf_maddr;
398         if (maddr < 0xa0000 || maddr + memsize > 0x1000000) {
399                 device_printf(dev, "Invalid ISA memory address range configured: 0x%x - 0x%x\n",
400                               maddr, maddr + memsize);
401                 return (ENXIO);
402         }
403
404         /*
405          * (note that if the user specifies both of the following flags that
406          * '8bit' mode intentionally has precedence)
407          */
408         if (flags & ED_FLAGS_FORCE_16BIT_MODE)
409                 isa16bit = 1;
410         if (flags & ED_FLAGS_FORCE_8BIT_MODE)
411                 isa16bit = 0;
412
413         /*
414          * If possible, get the assigned interrupt number from the card and
415          * use it.
416          */
417         if ((sc->type & ED_WD_SOFTCONFIG) &&
418             (sc->chip_type != ED_CHIP_TYPE_WD790)) {
419
420                 /*
421                  * Assemble together the encoded interrupt number.
422                  */
423                 iptr = (ed_asic_inb(sc, ED_WD_ICR) & ED_WD_ICR_IR2) |
424                     ((ed_asic_inb(sc, ED_WD_IRR) &
425                       (ED_WD_IRR_IR0 | ED_WD_IRR_IR1)) >> 5);
426
427                 /*
428                  * If no interrupt specified (or "?"), use what the board tells us.
429                  */
430                 error = bus_get_resource(dev, SYS_RES_IRQ, 0,
431                                          &irq, &junk);
432                 if (error && intr_vals[0] != NULL) {
433                         error = bus_set_resource(dev, SYS_RES_IRQ, 0,
434                                                  intr_vals[0][iptr], 1);
435                 }
436                 if (error)
437                         return (error);
438
439                 /*
440                  * Enable the interrupt.
441                  */
442                 ed_asic_outb(sc, ED_WD_IRR,
443                      ed_asic_inb(sc, ED_WD_IRR) | ED_WD_IRR_IEN);
444         }
445         if (sc->chip_type == ED_CHIP_TYPE_WD790) {
446                 ed_asic_outb(sc, ED_WD790_HWR,
447                   ed_asic_inb(sc, ED_WD790_HWR) | ED_WD790_HWR_SWH);
448                 iptr = (((ed_asic_inb(sc, ED_WD790_GCR) & ED_WD790_GCR_IR2) >> 4) |
449                         (ed_asic_inb(sc, ED_WD790_GCR) &
450                          (ED_WD790_GCR_IR1 | ED_WD790_GCR_IR0)) >> 2);
451                 ed_asic_outb(sc, ED_WD790_HWR,
452                  ed_asic_inb(sc, ED_WD790_HWR) & ~ED_WD790_HWR_SWH);
453
454                 /*
455                  * If no interrupt specified (or "?"), use what the board tells us.
456                  */
457                 error = bus_get_resource(dev, SYS_RES_IRQ, 0,
458                                          &irq, &junk);
459                 if (error && intr_vals[1] != NULL) {
460                         error = bus_set_resource(dev, SYS_RES_IRQ, 0,
461                                                  intr_vals[1][iptr], 1);
462                 }
463                 if (error)
464                         return (error);
465
466                 /*
467                  * Enable interrupts.
468                  */
469                 ed_asic_outb(sc, ED_WD790_ICR,
470                   ed_asic_inb(sc, ED_WD790_ICR) | ED_WD790_ICR_EIL);
471         }
472         error = bus_get_resource(dev, SYS_RES_IRQ, 0,
473                                  &irq, &junk);
474         if (error) {
475                 device_printf(dev, "%s cards don't support auto-detected/assigned interrupts.\n",
476                               sc->type_str);
477                 return (ENXIO);
478         }
479         sc->isa16bit = isa16bit;
480         sc->mem_shared = 1;
481
482         error = ed_alloc_memory(dev, 0, memsize);
483         if (error) {
484                 printf("*** ed_alloc_memory() failed! (%d)\n", error);
485                 return (error);
486         }
487         sc->mem_start = (caddr_t) rman_get_virtual(sc->mem_res);
488
489         /*
490          * allocate one xmit buffer if < 16k, two buffers otherwise
491          */
492         if ((memsize < 16384) ||
493             (flags & ED_FLAGS_NO_MULTI_BUFFERING)) {
494                 sc->txb_cnt = 1;
495         } else {
496                 sc->txb_cnt = 2;
497         }
498         sc->tx_page_start = ED_WD_PAGE_OFFSET;
499         sc->rec_page_start = ED_WD_PAGE_OFFSET + ED_TXBUF_SIZE * sc->txb_cnt;
500         sc->rec_page_stop = ED_WD_PAGE_OFFSET + memsize / ED_PAGE_SIZE;
501         sc->mem_ring = sc->mem_start + (ED_PAGE_SIZE * sc->rec_page_start);
502         sc->mem_size = memsize;
503         sc->mem_end = sc->mem_start + memsize;
504
505         /*
506          * Get station address from on-board ROM
507          */
508         for (i = 0; i < ETHER_ADDR_LEN; ++i)
509                 sc->arpcom.ac_enaddr[i] = ed_asic_inb(sc, ED_WD_PROM + i);
510
511         /*
512          * Set upper address bits and 8/16 bit access to shared memory.
513          */
514         if (isa16bit) {
515                 if (sc->chip_type == ED_CHIP_TYPE_WD790) {
516                         sc->wd_laar_proto = ed_asic_inb(sc, ED_WD_LAAR);
517                 } else {
518                         sc->wd_laar_proto = ED_WD_LAAR_L16EN |
519                             ((kvtop(sc->mem_start) >> 19) & ED_WD_LAAR_ADDRHI);
520                 }
521                 /*
522                  * Enable 16bit access
523                  */
524                 ed_asic_outb(sc, ED_WD_LAAR, sc->wd_laar_proto |
525                     ED_WD_LAAR_M16EN);
526         } else {
527                 if (((sc->type & ED_WD_SOFTCONFIG) ||
528                      (sc->type == ED_TYPE_TOSHIBA1) ||
529                      (sc->type == ED_TYPE_TOSHIBA4) ||
530                      (sc->type == ED_TYPE_WD8013EBT)) &&
531                     (sc->chip_type != ED_CHIP_TYPE_WD790)) {
532                         sc->wd_laar_proto = (kvtop(sc->mem_start) >> 19) &
533                             ED_WD_LAAR_ADDRHI;
534                         ed_asic_outb(sc, ED_WD_LAAR, sc->wd_laar_proto);
535                 }
536         }
537
538         /*
539          * Set address and enable interface shared memory.
540          */
541         if (sc->chip_type != ED_CHIP_TYPE_WD790) {
542                 if (ED_FLAGS_GETTYPE(flags) == ED_FLAGS_TOSH_ETHER) {
543                         ed_asic_outb(sc, ED_WD_MSR + 1,
544                                      ((kvtop(sc->mem_start) >> 8) & 0xe0) | 4);
545                         ed_asic_outb(sc, ED_WD_MSR + 2,
546                                      ((kvtop(sc->mem_start) >> 16) & 0x0f));
547                         ed_asic_outb(sc, ED_WD_MSR,
548                                      ED_WD_MSR_MENB | ED_WD_MSR_POW);
549                 } else {
550                         ed_asic_outb(sc, ED_WD_MSR,
551                                      ((kvtop(sc->mem_start) >> 13) &
552                                       ED_WD_MSR_ADDR) | ED_WD_MSR_MENB);
553                 }
554                 sc->cr_proto = ED_CR_RD2;
555         } else {
556                 ed_asic_outb(sc, ED_WD_MSR, ED_WD_MSR_MENB);
557                 ed_asic_outb(sc, ED_WD790_HWR, (ed_asic_inb(sc, ED_WD790_HWR) | ED_WD790_HWR_SWH));
558                 ed_asic_outb(sc, ED_WD790_RAR, ((kvtop(sc->mem_start) >> 13) & 0x0f) |
559                      ((kvtop(sc->mem_start) >> 11) & 0x40) |
560                      (ed_asic_inb(sc, ED_WD790_RAR) & 0xb0));
561                 ed_asic_outb(sc, ED_WD790_HWR, (ed_asic_inb(sc, ED_WD790_HWR) & ~ED_WD790_HWR_SWH));
562                 sc->cr_proto = 0;
563         }
564
565 #if 0
566         printf("starting memory performance test at 0x%x, size %d...\n",
567                 sc->mem_start, memsize*16384);
568         for (i = 0; i < 16384; i++)
569                 bzero(sc->mem_start, memsize);
570         printf("***DONE***\n");
571 #endif
572
573         /*
574          * Now zero memory and verify that it is clear
575          */
576         bzero(sc->mem_start, memsize);
577
578         for (i = 0; i < memsize; ++i) {
579                 if (sc->mem_start[i]) {
580                         device_printf(dev, "failed to clear shared memory at %llx - check configuration\n",
581                                       (long long)kvtop(sc->mem_start + i));
582
583                         /*
584                          * Disable 16 bit access to shared memory
585                          */
586                         if (isa16bit) {
587                                 if (sc->chip_type == ED_CHIP_TYPE_WD790) {
588                                         ed_asic_outb(sc, ED_WD_MSR, 0x00);
589                                 }
590                                 ed_asic_outb(sc, ED_WD_LAAR, sc->wd_laar_proto &
591                                     ~ED_WD_LAAR_M16EN);
592                         }
593                         return (ENXIO);
594                 }
595         }
596
597         /*
598          * Disable 16bit access to shared memory - we leave it
599          * disabled so that 1) machines reboot properly when the board
600          * is set 16 bit mode and there are conflicting 8bit
601          * devices/ROMS in the same 128k address space as this boards
602          * shared memory. and 2) so that other 8 bit devices with
603          * shared memory can be used in this 128k region, too.
604          */
605         if (isa16bit) {
606                 if (sc->chip_type == ED_CHIP_TYPE_WD790) {
607                         ed_asic_outb(sc, ED_WD_MSR, 0x00);
608                 }
609                 ed_asic_outb(sc, ED_WD_LAAR, sc->wd_laar_proto &
610                     ~ED_WD_LAAR_M16EN);
611         }
612         return (0);
613 }
614
615 int
616 ed_probe_WD80x3(dev, port_rid, flags)
617         device_t dev;
618         int port_rid;
619         int flags;
620 {
621         struct ed_softc *sc = device_get_softc(dev);
622         int     error;
623         static unsigned short *intr_vals[] = {ed_intr_val, ed_790_intr_val};
624
625         error = ed_alloc_port(dev, port_rid, ED_WD_IO_PORTS);
626         if (error)
627                 return (error);
628
629         sc->asic_offset = ED_WD_ASIC_OFFSET;
630         sc->nic_offset  = ED_WD_NIC_OFFSET;
631
632         return ed_probe_WD80x3_generic(dev, flags, intr_vals);
633 }
634
635 /*
636  * Probe and vendor-specific initialization routine for 3Com 3c503 boards
637  */
638 int
639 ed_probe_3Com(dev, port_rid, flags)
640         device_t dev;
641         int port_rid;
642         int flags;
643 {
644         struct ed_softc *sc = device_get_softc(dev);
645         int     error;
646         int     i;
647         u_int   memsize;
648         u_char  isa16bit;
649         u_long  conf_maddr, conf_msize, irq, junk;
650
651         error = ed_alloc_port(dev, 0, ED_3COM_IO_PORTS);
652         if (error)
653                 return (error);
654
655         sc->asic_offset = ED_3COM_ASIC_OFFSET;
656         sc->nic_offset  = ED_3COM_NIC_OFFSET;
657
658         /*
659          * Verify that the kernel configured I/O address matches the board
660          * configured address
661          */
662         switch (ed_asic_inb(sc, ED_3COM_BCFR)) {
663         case ED_3COM_BCFR_300:
664                 if (rman_get_start(sc->port_res) != 0x300)
665                         return (ENXIO);
666                 break;
667         case ED_3COM_BCFR_310:
668                 if (rman_get_start(sc->port_res) != 0x310)
669                         return (ENXIO);
670                 break;
671         case ED_3COM_BCFR_330:
672                 if (rman_get_start(sc->port_res) != 0x330)
673                         return (ENXIO);
674                 break;
675         case ED_3COM_BCFR_350:
676                 if (rman_get_start(sc->port_res) != 0x350)
677                         return (ENXIO);
678                 break;
679         case ED_3COM_BCFR_250:
680                 if (rman_get_start(sc->port_res) != 0x250)
681                         return (ENXIO);
682                 break;
683         case ED_3COM_BCFR_280:
684                 if (rman_get_start(sc->port_res) != 0x280)
685                         return (ENXIO);
686                 break;
687         case ED_3COM_BCFR_2A0:
688                 if (rman_get_start(sc->port_res) != 0x2a0)
689                         return (ENXIO);
690                 break;
691         case ED_3COM_BCFR_2E0:
692                 if (rman_get_start(sc->port_res) != 0x2e0)
693                         return (ENXIO);
694                 break;
695         default:
696                 return (ENXIO);
697         }
698
699         error = bus_get_resource(dev, SYS_RES_MEMORY, 0,
700                                  &conf_maddr, &conf_msize);
701         if (error)
702                 return (error);
703
704         /*
705          * Verify that the kernel shared memory address matches the board
706          * configured address.
707          */
708         switch (ed_asic_inb(sc, ED_3COM_PCFR)) {
709         case ED_3COM_PCFR_DC000:
710                 if (conf_maddr != 0xdc000)
711                         return (ENXIO);
712                 break;
713         case ED_3COM_PCFR_D8000:
714                 if (conf_maddr != 0xd8000)
715                         return (ENXIO);
716                 break;
717         case ED_3COM_PCFR_CC000:
718                 if (conf_maddr != 0xcc000)
719                         return (ENXIO);
720                 break;
721         case ED_3COM_PCFR_C8000:
722                 if (conf_maddr != 0xc8000)
723                         return (ENXIO);
724                 break;
725         default:
726                 return (ENXIO);
727         }
728
729
730         /*
731          * Reset NIC and ASIC. Enable on-board transceiver throughout reset
732          * sequence because it'll lock up if the cable isn't connected if we
733          * don't.
734          */
735         ed_asic_outb(sc, ED_3COM_CR, ED_3COM_CR_RST | ED_3COM_CR_XSEL);
736
737         /*
738          * Wait for a while, then un-reset it
739          */
740         DELAY(50);
741
742         /*
743          * The 3Com ASIC defaults to rather strange settings for the CR after
744          * a reset - it's important to set it again after the following outb
745          * (this is done when we map the PROM below).
746          */
747         ed_asic_outb(sc, ED_3COM_CR, ED_3COM_CR_XSEL);
748
749         /*
750          * Wait a bit for the NIC to recover from the reset
751          */
752         DELAY(5000);
753
754         sc->vendor = ED_VENDOR_3COM;
755         sc->type_str = "3c503";
756         sc->mem_shared = 1;
757         sc->cr_proto = ED_CR_RD2;
758
759         /*
760          * Hmmm...a 16bit 3Com board has 16k of memory, but only an 8k window
761          * to it.
762          */
763         memsize = 8192;
764
765         /*
766          * Get station address from on-board ROM
767          */
768
769         /*
770          * First, map ethernet address PROM over the top of where the NIC
771          * registers normally appear.
772          */
773         ed_asic_outb(sc, ED_3COM_CR, ED_3COM_CR_EALO | ED_3COM_CR_XSEL);
774
775         for (i = 0; i < ETHER_ADDR_LEN; ++i)
776                 sc->arpcom.ac_enaddr[i] = ed_nic_inb(sc, i);
777
778         /*
779          * Unmap PROM - select NIC registers. The proper setting of the
780          * tranceiver is set in ed_init so that the attach code is given a
781          * chance to set the default based on a compile-time config option
782          */
783         ed_asic_outb(sc, ED_3COM_CR, ED_3COM_CR_XSEL);
784
785         /*
786          * Determine if this is an 8bit or 16bit board
787          */
788
789         /*
790          * select page 0 registers
791          */
792         ed_nic_outb(sc, ED_P0_CR, ED_CR_RD2 | ED_CR_STP);
793
794         /*
795          * Attempt to clear WTS bit. If it doesn't clear, then this is a 16bit
796          * board.
797          */
798         ed_nic_outb(sc, ED_P0_DCR, 0);
799
800         /*
801          * select page 2 registers
802          */
803         ed_nic_outb(sc, ED_P0_CR, ED_CR_PAGE_2 | ED_CR_RD2 | ED_CR_STP);
804
805         /*
806          * The 3c503 forces the WTS bit to a one if this is a 16bit board
807          */
808         if (ed_nic_inb(sc, ED_P2_DCR) & ED_DCR_WTS)
809                 isa16bit = 1;
810         else
811                 isa16bit = 0;
812
813         /*
814          * select page 0 registers
815          */
816         ed_nic_outb(sc, ED_P2_CR, ED_CR_RD2 | ED_CR_STP);
817
818         error = ed_alloc_memory(dev, 0, memsize);
819         if (error)
820                 return (error);
821
822         sc->mem_start = (caddr_t) rman_get_virtual(sc->mem_res);
823         sc->mem_size = memsize;
824         sc->mem_end = sc->mem_start + memsize;
825
826         /*
827          * We have an entire 8k window to put the transmit buffers on the
828          * 16bit boards. But since the 16bit 3c503's shared memory is only
829          * fast enough to overlap the loading of one full-size packet, trying
830          * to load more than 2 buffers can actually leave the transmitter idle
831          * during the load. So 2 seems the best value. (Although a mix of
832          * variable-sized packets might change this assumption. Nonetheless,
833          * we optimize for linear transfers of same-size packets.)
834          */
835         if (isa16bit) {
836                 if (flags & ED_FLAGS_NO_MULTI_BUFFERING)
837                         sc->txb_cnt = 1;
838                 else
839                         sc->txb_cnt = 2;
840
841                 sc->tx_page_start = ED_3COM_TX_PAGE_OFFSET_16BIT;
842                 sc->rec_page_start = ED_3COM_RX_PAGE_OFFSET_16BIT;
843                 sc->rec_page_stop = memsize / ED_PAGE_SIZE +
844                     ED_3COM_RX_PAGE_OFFSET_16BIT;
845                 sc->mem_ring = sc->mem_start;
846         } else {
847                 sc->txb_cnt = 1;
848                 sc->tx_page_start = ED_3COM_TX_PAGE_OFFSET_8BIT;
849                 sc->rec_page_start = ED_TXBUF_SIZE + ED_3COM_TX_PAGE_OFFSET_8BIT;
850                 sc->rec_page_stop = memsize / ED_PAGE_SIZE +
851                     ED_3COM_TX_PAGE_OFFSET_8BIT;
852                 sc->mem_ring = sc->mem_start + (ED_PAGE_SIZE * ED_TXBUF_SIZE);
853         }
854
855         sc->isa16bit = isa16bit;
856
857         /*
858          * Initialize GA page start/stop registers. Probably only needed if
859          * doing DMA, but what the hell.
860          */
861         ed_asic_outb(sc, ED_3COM_PSTR, sc->rec_page_start);
862         ed_asic_outb(sc, ED_3COM_PSPR, sc->rec_page_stop);
863
864         /*
865          * Set IRQ. 3c503 only allows a choice of irq 2-5.
866          */
867         error = bus_get_resource(dev, SYS_RES_IRQ, 0, &irq, &junk);
868         if (error)
869                 return (error);
870
871         switch (irq) {
872         case 2:
873         case 9:
874                 ed_asic_outb(sc, ED_3COM_IDCFR, ED_3COM_IDCFR_IRQ2);
875                 break;
876         case 3:
877                 ed_asic_outb(sc, ED_3COM_IDCFR, ED_3COM_IDCFR_IRQ3);
878                 break;
879         case 4:
880                 ed_asic_outb(sc, ED_3COM_IDCFR, ED_3COM_IDCFR_IRQ4);
881                 break;
882         case 5:
883                 ed_asic_outb(sc, ED_3COM_IDCFR, ED_3COM_IDCFR_IRQ5);
884                 break;
885         default:
886                 device_printf(dev, "Invalid irq configuration (%ld) must be 3-5,9 for 3c503\n",
887                               irq);
888                 return (ENXIO);
889         }
890
891         /*
892          * Initialize GA configuration register. Set bank and enable shared
893          * mem.
894          */
895         ed_asic_outb(sc, ED_3COM_GACFR, ED_3COM_GACFR_RSEL |
896              ED_3COM_GACFR_MBS0);
897
898         /*
899          * Initialize "Vector Pointer" registers. These gawd-awful things are
900          * compared to 20 bits of the address on ISA, and if they match, the
901          * shared memory is disabled. We set them to 0xffff0...allegedly the
902          * reset vector.
903          */
904         ed_asic_outb(sc, ED_3COM_VPTR2, 0xff);
905         ed_asic_outb(sc, ED_3COM_VPTR1, 0xff);
906         ed_asic_outb(sc, ED_3COM_VPTR0, 0x00);
907
908         /*
909          * Zero memory and verify that it is clear
910          */
911         bzero(sc->mem_start, memsize);
912
913         for (i = 0; i < memsize; ++i)
914                 if (sc->mem_start[i]) {
915                         device_printf(dev, "failed to clear shared memory at %llx - check configuration\n",
916                                       (unsigned long long)kvtop(sc->mem_start + i));
917                         return (ENXIO);
918                 }
919         return (0);
920 }
921
922 /*
923  * Probe and vendor-specific initialization routine for SIC boards
924  */
925 int
926 ed_probe_SIC(dev, port_rid, flags)
927         device_t dev;
928         int port_rid;
929         int flags;
930 {
931         struct ed_softc *sc = device_get_softc(dev);
932         int     error;
933         int     i;
934         u_int   memsize;
935         u_long  conf_maddr, conf_msize;
936         u_char  sum;
937
938         error = ed_alloc_port(dev, 0, ED_SIC_IO_PORTS);
939         if (error)
940                 return (error);
941
942         sc->asic_offset = ED_SIC_ASIC_OFFSET;
943         sc->nic_offset  = ED_SIC_NIC_OFFSET;
944
945         error = bus_get_resource(dev, SYS_RES_MEMORY, 0,
946                                  &conf_maddr, &conf_msize);
947         if (error)
948                 return (error);
949
950         memsize = 16384;
951         if (conf_msize > 1)
952                 memsize = conf_msize;
953
954         error = ed_alloc_memory(dev, 0, memsize);
955         if (error)
956                 return (error);
957
958         sc->mem_start = (caddr_t) rman_get_virtual(sc->mem_res);
959         sc->mem_size  = memsize;
960
961         /* Reset card to force it into a known state. */
962         ed_asic_outb(sc, 0, 0x00);
963         DELAY(100);
964
965         /*
966          * Here we check the card ROM, if the checksum passes, and the
967          * type code and ethernet address check out, then we know we have
968          * an SIC card.
969          */
970         ed_asic_outb(sc, 0, 0x81);
971         DELAY(100);
972
973         sum = sc->mem_start[6];
974         for (i = 0; i < ETHER_ADDR_LEN; i++) {
975                 sum ^= (sc->arpcom.ac_enaddr[i] = sc->mem_start[i]);
976         }
977 #ifdef ED_DEBUG
978         device_printf(dev, "ed_probe_sic: got address %6D\n",
979                       sc->arpcom.ac_enaddr, ":");
980 #endif
981         if (sum != 0) {
982                 return (ENXIO);
983         }
984         if ((sc->arpcom.ac_enaddr[0] | sc->arpcom.ac_enaddr[1] |
985              sc->arpcom.ac_enaddr[2]) == 0) {
986                 return (ENXIO);
987         }
988
989         sc->vendor   = ED_VENDOR_SIC;
990         sc->type_str = "SIC";
991         sc->isa16bit = 0;
992         sc->cr_proto = 0;
993
994         /*
995          * SIC RAM page 0x0000-0x3fff(or 0x7fff)
996          */
997         ed_asic_outb(sc, 0, 0x80);
998         DELAY(100);
999
1000         /*
1001          * Now zero memory and verify that it is clear
1002          */
1003         bzero(sc->mem_start, sc->mem_size);
1004
1005         for (i = 0; i < sc->mem_size; i++) {
1006                 if (sc->mem_start[i]) {
1007                         device_printf(dev, "failed to clear shared memory "
1008                                 "at %llx - check configuration\n",
1009                                 (long long)kvtop(sc->mem_start + i));
1010
1011                         return (ENXIO);
1012                 }
1013         }
1014
1015         sc->mem_shared = 1;
1016         sc->mem_end = sc->mem_start + sc->mem_size;
1017
1018         /*
1019          * allocate one xmit buffer if < 16k, two buffers otherwise
1020          */
1021         if ((sc->mem_size < 16384) || (flags & ED_FLAGS_NO_MULTI_BUFFERING)) {
1022                 sc->txb_cnt = 1;
1023         } else {
1024                 sc->txb_cnt = 2;
1025         }
1026         sc->tx_page_start = 0;
1027
1028         sc->rec_page_start = sc->tx_page_start + ED_TXBUF_SIZE * sc->txb_cnt;
1029         sc->rec_page_stop = sc->tx_page_start + sc->mem_size / ED_PAGE_SIZE;
1030
1031         sc->mem_ring = sc->mem_start + sc->txb_cnt * ED_PAGE_SIZE * ED_TXBUF_SIZE;
1032
1033         return (0);
1034 }
1035
1036 /*
1037  * Probe and vendor-specific initialization routine for NE1000/2000 boards
1038  */
1039 int
1040 ed_probe_Novell_generic(dev, flags)
1041         device_t dev;
1042         int flags;
1043 {
1044         struct ed_softc *sc = device_get_softc(dev);
1045         u_int   memsize, n;
1046         u_char  romdata[16], tmp;
1047         static char test_pattern[32] = "THIS is A memory TEST pattern";
1048         char    test_buffer[32];
1049
1050         /* XXX - do Novell-specific probe here */
1051
1052         /* Reset the board */
1053         if (ED_FLAGS_GETTYPE(flags) == ED_FLAGS_GWETHER) {
1054                 ed_asic_outb(sc, ED_NOVELL_RESET, 0);
1055                 DELAY(200);
1056         }
1057         tmp = ed_asic_inb(sc, ED_NOVELL_RESET);
1058
1059         /*
1060          * I don't know if this is necessary; probably cruft leftover from
1061          * Clarkson packet driver code. Doesn't do a thing on the boards I've
1062          * tested. -DG [note that an outb(0x84, 0) seems to work here, and is
1063          * non-invasive...but some boards don't seem to reset and I don't have
1064          * complete documentation on what the 'right' thing to do is...so we
1065          * do the invasive thing for now. Yuck.]
1066          */
1067         ed_asic_outb(sc, ED_NOVELL_RESET, tmp);
1068         DELAY(5000);
1069
1070         /*
1071          * This is needed because some NE clones apparently don't reset the
1072          * NIC properly (or the NIC chip doesn't reset fully on power-up) XXX
1073          * - this makes the probe invasive! ...Done against my better
1074          * judgement. -DLG
1075          */
1076         ed_nic_outb(sc, ED_P0_CR, ED_CR_RD2 | ED_CR_STP);
1077
1078         DELAY(5000);
1079
1080         /* Make sure that we really have an 8390 based board */
1081         if (!ed_probe_generic8390(sc))
1082                 return (ENXIO);
1083
1084         sc->vendor = ED_VENDOR_NOVELL;
1085         sc->mem_shared = 0;
1086         sc->cr_proto = ED_CR_RD2;
1087
1088         /*
1089          * Test the ability to read and write to the NIC memory. This has the
1090          * side affect of determining if this is an NE1000 or an NE2000.
1091          */
1092
1093         /*
1094          * This prevents packets from being stored in the NIC memory when the
1095          * readmem routine turns on the start bit in the CR.
1096          */
1097         ed_nic_outb(sc, ED_P0_RCR, ED_RCR_MON);
1098
1099         /* Temporarily initialize DCR for byte operations */
1100         ed_nic_outb(sc, ED_P0_DCR, ED_DCR_FT1 | ED_DCR_LS);
1101
1102         ed_nic_outb(sc, ED_P0_PSTART, 8192 / ED_PAGE_SIZE);
1103         ed_nic_outb(sc, ED_P0_PSTOP, 16384 / ED_PAGE_SIZE);
1104
1105         sc->isa16bit = 0;
1106
1107         /*
1108          * Write a test pattern in byte mode. If this fails, then there
1109          * probably isn't any memory at 8k - which likely means that the board
1110          * is an NE2000.
1111          */
1112         ed_pio_writemem(sc, test_pattern, 8192, sizeof(test_pattern));
1113         ed_pio_readmem(sc, 8192, test_buffer, sizeof(test_pattern));
1114
1115         if (bcmp(test_pattern, test_buffer, sizeof(test_pattern)) == 0) {
1116                 sc->type = ED_TYPE_NE1000;
1117                 sc->type_str = "NE1000";
1118         } else {
1119
1120                 /* neither an NE1000 nor a Linksys - try NE2000 */
1121                 ed_nic_outb(sc, ED_P0_DCR, ED_DCR_WTS | ED_DCR_FT1 | ED_DCR_LS);
1122                 ed_nic_outb(sc, ED_P0_PSTART, 16384 / ED_PAGE_SIZE);
1123                 ed_nic_outb(sc, ED_P0_PSTOP, 32768 / ED_PAGE_SIZE);
1124
1125                 sc->isa16bit = 1;
1126
1127                 /*
1128                  * Write a test pattern in word mode. If this also fails, then
1129                  * we don't know what this board is.
1130                  */
1131                 ed_pio_writemem(sc, test_pattern, 16384, sizeof(test_pattern));
1132                 ed_pio_readmem(sc, 16384, test_buffer, sizeof(test_pattern));
1133                 if (bcmp(test_pattern, test_buffer, sizeof(test_pattern)) == 0) {
1134                         sc->type = ED_TYPE_NE2000;
1135                         sc->type_str = "NE2000";
1136                 } else {
1137                         return (ENXIO);
1138                 }
1139         }
1140
1141
1142         /* 8k of memory plus an additional 8k if 16bit */
1143         memsize = 8192 + sc->isa16bit * 8192;
1144
1145 #if 0   /* probably not useful - NE boards only come two ways */
1146         /* allow kernel config file overrides */
1147         if (isa_dev->id_msize)
1148                 memsize = isa_dev->id_msize;
1149 #endif
1150
1151         sc->mem_size = memsize;
1152
1153         /* NIC memory doesn't start at zero on an NE board */
1154         /* The start address is tied to the bus width */
1155         sc->mem_start = (char *) 8192 + sc->isa16bit * 8192;
1156         sc->mem_end = sc->mem_start + memsize;
1157         sc->tx_page_start = memsize / ED_PAGE_SIZE;
1158
1159         if (ED_FLAGS_GETTYPE(flags) == ED_FLAGS_GWETHER) {
1160                 int     x, i, mstart = 0, msize = 0;
1161                 char    pbuf0[ED_PAGE_SIZE], pbuf[ED_PAGE_SIZE], tbuf[ED_PAGE_SIZE];
1162
1163                 for (i = 0; i < ED_PAGE_SIZE; i++)
1164                         pbuf0[i] = 0;
1165
1166                 /* Clear all the memory. */
1167                 for (x = 1; x < 256; x++)
1168                         ed_pio_writemem(sc, pbuf0, x * 256, ED_PAGE_SIZE);
1169
1170                 /* Search for the start of RAM. */
1171                 for (x = 1; x < 256; x++) {
1172                         ed_pio_readmem(sc, x * 256, tbuf, ED_PAGE_SIZE);
1173                         if (bcmp(pbuf0, tbuf, ED_PAGE_SIZE) == 0) {
1174                                 for (i = 0; i < ED_PAGE_SIZE; i++)
1175                                         pbuf[i] = 255 - x;
1176                                 ed_pio_writemem(sc, pbuf, x * 256, ED_PAGE_SIZE);
1177                                 ed_pio_readmem(sc, x * 256, tbuf, ED_PAGE_SIZE);
1178                                 if (bcmp(pbuf, tbuf, ED_PAGE_SIZE) == 0) {
1179                                         mstart = x * ED_PAGE_SIZE;
1180                                         msize = ED_PAGE_SIZE;
1181                                         break;
1182                                 }
1183                         }
1184                 }
1185
1186                 if (mstart == 0) {
1187                         device_printf(dev, "Cannot find start of RAM.\n");
1188                         return (ENXIO);
1189                 }
1190                 /* Search for the start of RAM. */
1191                 for (x = (mstart / ED_PAGE_SIZE) + 1; x < 256; x++) {
1192                         ed_pio_readmem(sc, x * 256, tbuf, ED_PAGE_SIZE);
1193                         if (bcmp(pbuf0, tbuf, ED_PAGE_SIZE) == 0) {
1194                                 for (i = 0; i < ED_PAGE_SIZE; i++)
1195                                         pbuf[i] = 255 - x;
1196                                 ed_pio_writemem(sc, pbuf, x * 256, ED_PAGE_SIZE);
1197                                 ed_pio_readmem(sc, x * 256, tbuf, ED_PAGE_SIZE);
1198                                 if (bcmp(pbuf, tbuf, ED_PAGE_SIZE) == 0)
1199                                         msize += ED_PAGE_SIZE;
1200                                 else {
1201                                         break;
1202                                 }
1203                         } else {
1204                                 break;
1205                         }
1206                 }
1207
1208                 if (msize == 0) {
1209                         device_printf(dev, "Cannot find any RAM, start : %d, x = %d.\n", mstart, x);
1210                         return (ENXIO);
1211                 }
1212                 device_printf(dev, "RAM start at %d, size : %d.\n", mstart, msize);
1213
1214                 sc->mem_size = msize;
1215                 sc->mem_start = (caddr_t) mstart;
1216                 sc->mem_end = (caddr_t) (msize + mstart);
1217                 sc->tx_page_start = mstart / ED_PAGE_SIZE;
1218         }
1219
1220         /*
1221          * Use one xmit buffer if < 16k, two buffers otherwise (if not told
1222          * otherwise).
1223          */
1224         if ((memsize < 16384) || (flags & ED_FLAGS_NO_MULTI_BUFFERING))
1225                 sc->txb_cnt = 1;
1226         else
1227                 sc->txb_cnt = 2;
1228
1229         sc->rec_page_start = sc->tx_page_start + sc->txb_cnt * ED_TXBUF_SIZE;
1230         sc->rec_page_stop = sc->tx_page_start + memsize / ED_PAGE_SIZE;
1231
1232         sc->mem_ring = sc->mem_start + sc->txb_cnt * ED_PAGE_SIZE * ED_TXBUF_SIZE;
1233
1234         ed_pio_readmem(sc, 0, romdata, 16);
1235         for (n = 0; n < ETHER_ADDR_LEN; n++)
1236                 sc->arpcom.ac_enaddr[n] = romdata[n * (sc->isa16bit + 1)];
1237
1238         if ((ED_FLAGS_GETTYPE(flags) == ED_FLAGS_GWETHER) &&
1239             (sc->arpcom.ac_enaddr[2] == 0x86)) {
1240                 sc->type_str = "Gateway AT";
1241         }
1242
1243         /* clear any pending interrupts that might have occurred above */
1244         ed_nic_outb(sc, ED_P0_ISR, 0xff);
1245
1246         return (0);
1247 }
1248
1249 int
1250 ed_probe_Novell(dev, port_rid, flags)
1251         device_t dev;
1252         int port_rid;
1253         int flags;
1254 {
1255         struct ed_softc *sc = device_get_softc(dev);
1256         int     error;
1257
1258         error = ed_alloc_port(dev, port_rid, ED_NOVELL_IO_PORTS);
1259         if (error)
1260                 return (error);
1261
1262         sc->asic_offset = ED_NOVELL_ASIC_OFFSET;
1263         sc->nic_offset  = ED_NOVELL_NIC_OFFSET;
1264
1265         return ed_probe_Novell_generic(dev, flags);
1266 }
1267
1268 #define ED_HPP_TEST_SIZE        16
1269
1270 /*
1271  * Probe and vendor specific initialization for the HP PC Lan+ Cards.
1272  * (HP Part nos: 27247B and 27252A).
1273  *
1274  * The card has an asic wrapper around a DS8390 core.  The asic handles 
1275  * host accesses and offers both standard register IO and memory mapped 
1276  * IO.  Memory mapped I/O allows better performance at the expense of greater
1277  * chance of an incompatibility with existing ISA cards.
1278  *
1279  * The card has a few caveats: it isn't tolerant of byte wide accesses, only
1280  * short (16 bit) or word (32 bit) accesses are allowed.  Some card revisions
1281  * don't allow 32 bit accesses; these are indicated by a bit in the software
1282  * ID register (see if_edreg.h).
1283  * 
1284  * Other caveats are: we should read the MAC address only when the card
1285  * is inactive.
1286  *
1287  * For more information; please consult the CRYNWR packet driver.
1288  *
1289  * The AUI port is turned on using the "link2" option on the ifconfig 
1290  * command line.
1291  */
1292 int
1293 ed_probe_HP_pclanp(dev, port_rid, flags)
1294         device_t dev;
1295         int port_rid;
1296         int flags;
1297 {
1298         struct ed_softc *sc = device_get_softc(dev);
1299         int error;
1300         int n;                          /* temp var */
1301         int memsize;                    /* mem on board */
1302         u_char checksum;                /* checksum of board address */
1303         u_char irq;                     /* board configured IRQ */
1304         char test_pattern[ED_HPP_TEST_SIZE];    /* read/write areas for */
1305         char test_buffer[ED_HPP_TEST_SIZE];     /* probing card */
1306         u_long conf_maddr, conf_msize, conf_irq, junk;
1307
1308         error = ed_alloc_port(dev, 0, ED_HPP_IO_PORTS);
1309         if (error)
1310                 return (error);
1311
1312         /* Fill in basic information */
1313         sc->asic_offset = ED_HPP_ASIC_OFFSET;
1314         sc->nic_offset  = ED_HPP_NIC_OFFSET;
1315
1316         sc->chip_type = ED_CHIP_TYPE_DP8390;
1317         sc->isa16bit = 0;       /* the 8390 core needs to be in byte mode */
1318
1319         /* 
1320          * Look for the HP PCLAN+ signature: "0x50,0x48,0x00,0x53" 
1321          */
1322         
1323         if ((ed_asic_inb(sc, ED_HPP_ID) != 0x50) || 
1324             (ed_asic_inb(sc, ED_HPP_ID + 1) != 0x48) ||
1325             ((ed_asic_inb(sc, ED_HPP_ID + 2) & 0xF0) != 0) ||
1326             (ed_asic_inb(sc, ED_HPP_ID + 3) != 0x53))
1327                 return ENXIO;
1328
1329         /* 
1330          * Read the MAC address and verify checksum on the address.
1331          */
1332
1333         ed_asic_outw(sc, ED_HPP_PAGING, ED_HPP_PAGE_MAC);
1334         for (n  = 0, checksum = 0; n < ETHER_ADDR_LEN; n++)
1335                 checksum += (sc->arpcom.ac_enaddr[n] = 
1336                         ed_asic_inb(sc, ED_HPP_MAC_ADDR + n));
1337         
1338         checksum += ed_asic_inb(sc, ED_HPP_MAC_ADDR + ETHER_ADDR_LEN);
1339
1340         if (checksum != 0xFF)
1341                 return ENXIO;
1342
1343         /*
1344          * Verify that the software model number is 0.
1345          */
1346         
1347         ed_asic_outw(sc, ED_HPP_PAGING, ED_HPP_PAGE_ID);
1348         if (((sc->hpp_id = ed_asic_inw(sc, ED_HPP_PAGE_4)) & 
1349                 ED_HPP_ID_SOFT_MODEL_MASK) != 0x0000)
1350                 return ENXIO;
1351
1352         /*
1353          * Read in and save the current options configured on card.
1354          */
1355
1356         sc->hpp_options = ed_asic_inw(sc, ED_HPP_OPTION);
1357
1358         sc->hpp_options |= (ED_HPP_OPTION_NIC_RESET | 
1359                                 ED_HPP_OPTION_CHIP_RESET |
1360                                 ED_HPP_OPTION_ENABLE_IRQ);
1361
1362         /* 
1363          * Reset the chip.  This requires writing to the option register
1364          * so take care to preserve the other bits.
1365          */
1366
1367         ed_asic_outw(sc, ED_HPP_OPTION, 
1368                 (sc->hpp_options & ~(ED_HPP_OPTION_NIC_RESET | 
1369                         ED_HPP_OPTION_CHIP_RESET)));
1370
1371         DELAY(5000);    /* wait for chip reset to complete */
1372
1373         ed_asic_outw(sc, ED_HPP_OPTION,
1374                 (sc->hpp_options | (ED_HPP_OPTION_NIC_RESET |
1375                         ED_HPP_OPTION_CHIP_RESET |
1376                         ED_HPP_OPTION_ENABLE_IRQ)));
1377
1378         DELAY(5000);
1379
1380         if (!(ed_nic_inb(sc, ED_P0_ISR) & ED_ISR_RST))
1381                 return ENXIO;   /* reset did not complete */
1382
1383         /*
1384          * Read out configuration information.
1385          */
1386
1387         ed_asic_outw(sc, ED_HPP_PAGING, ED_HPP_PAGE_HW);
1388
1389         irq = ed_asic_inb(sc, ED_HPP_HW_IRQ);
1390
1391         /*
1392          * Check for impossible IRQ.
1393          */
1394
1395         if (irq >= (sizeof(ed_hpp_intr_val) / sizeof(ed_hpp_intr_val[0])))
1396                 return ENXIO;
1397
1398         /* 
1399          * If the kernel IRQ was specified with a '?' use the cards idea
1400          * of the IRQ.  If the kernel IRQ was explicitly specified, it
1401          * should match that of the hardware.
1402          */
1403         error = bus_get_resource(dev, SYS_RES_IRQ, 0,
1404                                  &conf_irq, &junk);
1405         if (error) {
1406                 bus_set_resource(dev, SYS_RES_IRQ, 0,
1407                                  ed_hpp_intr_val[irq], 1);
1408         } else {
1409                 if (conf_irq != ed_hpp_intr_val[irq])
1410                         return (ENXIO);
1411         }
1412
1413         /*
1414          * Fill in softconfig info.
1415          */
1416
1417         sc->vendor = ED_VENDOR_HP;
1418         sc->type = ED_TYPE_HP_PCLANPLUS;
1419         sc->type_str = "HP-PCLAN+";
1420
1421         sc->mem_shared = 0;     /* we DON'T have dual ported RAM */
1422         sc->mem_start = 0;      /* we use offsets inside the card RAM */
1423
1424         sc->hpp_mem_start = NULL;/* no memory mapped I/O by default */
1425
1426         /*
1427          * The board has 32KB of memory.  Is there a way to determine
1428          * this programmatically?
1429          */
1430         
1431         memsize = 32768;
1432
1433         /*
1434          * Check if memory mapping of the I/O registers possible.
1435          */
1436
1437         if (sc->hpp_options & ED_HPP_OPTION_MEM_ENABLE)
1438         {
1439                 u_long mem_addr;
1440
1441                 /*
1442                  * determine the memory address from the board.
1443                  */
1444                 
1445                 ed_asic_outw(sc, ED_HPP_PAGING, ED_HPP_PAGE_HW);
1446                 mem_addr = (ed_asic_inw(sc, ED_HPP_HW_MEM_MAP) << 8);
1447
1448                 /*
1449                  * Check that the kernel specified start of memory and
1450                  * hardware's idea of it match.
1451                  */
1452                 error = bus_get_resource(dev, SYS_RES_MEMORY, 0,
1453                                          &conf_maddr, &conf_msize);
1454                 if (error)
1455                         return (error);
1456                 
1457                 if (mem_addr != conf_maddr)
1458                         return ENXIO;
1459
1460                 error = ed_alloc_memory(dev, 0, memsize);
1461                 if (error)
1462                         return (error);
1463
1464                 sc->hpp_mem_start = rman_get_virtual(sc->mem_res);
1465         }
1466
1467         /*
1468          * Fill in the rest of the soft config structure.
1469          */
1470
1471         /*
1472          * The transmit page index.
1473          */
1474
1475         sc->tx_page_start = ED_HPP_TX_PAGE_OFFSET;
1476
1477         if (device_get_flags(dev) & ED_FLAGS_NO_MULTI_BUFFERING)
1478                 sc->txb_cnt = 1;
1479         else
1480                 sc->txb_cnt = 2;
1481
1482         /*
1483          * Memory description
1484          */
1485
1486         sc->mem_size = memsize;
1487         sc->mem_ring = sc->mem_start + 
1488                 (sc->txb_cnt * ED_PAGE_SIZE * ED_TXBUF_SIZE);
1489         sc->mem_end = sc->mem_start + sc->mem_size;
1490
1491         /*
1492          * Receive area starts after the transmit area and 
1493          * continues till the end of memory.
1494          */
1495
1496         sc->rec_page_start = sc->tx_page_start + 
1497                                 (sc->txb_cnt * ED_TXBUF_SIZE);
1498         sc->rec_page_stop = (sc->mem_size / ED_PAGE_SIZE);
1499
1500
1501         sc->cr_proto = 0;       /* value works */
1502
1503         /*
1504          * Set the wrap registers for string I/O reads.
1505          */
1506
1507         ed_asic_outw(sc, ED_HPP_PAGING, ED_HPP_PAGE_HW);
1508         ed_asic_outw(sc, ED_HPP_HW_WRAP,
1509                 ((sc->rec_page_start / ED_PAGE_SIZE) |
1510                  (((sc->rec_page_stop / ED_PAGE_SIZE) - 1) << 8)));
1511
1512         /*
1513          * Reset the register page to normal operation.
1514          */
1515
1516         ed_asic_outw(sc, ED_HPP_PAGING, ED_HPP_PAGE_PERF);
1517
1518         /*
1519          * Verify that we can read/write from adapter memory.
1520          * Create test pattern.
1521          */
1522
1523         for (n = 0; n < ED_HPP_TEST_SIZE; n++)
1524         {
1525                 test_pattern[n] = (n*n) ^ ~n;
1526         }
1527
1528 #undef  ED_HPP_TEST_SIZE
1529
1530         /*
1531          * Check that the memory is accessible thru the I/O ports.
1532          * Write out the contents of "test_pattern", read back
1533          * into "test_buffer" and compare the two for any
1534          * mismatch.
1535          */
1536
1537         for (n = 0; n < (32768 / ED_PAGE_SIZE); n ++) {
1538
1539                 ed_hpp_writemem(sc, test_pattern, (n * ED_PAGE_SIZE), 
1540                                 sizeof(test_pattern));
1541                 ed_hpp_readmem(sc, (n * ED_PAGE_SIZE), 
1542                         test_buffer, sizeof(test_pattern));
1543
1544                 if (bcmp(test_pattern, test_buffer, 
1545                         sizeof(test_pattern)))
1546                         return ENXIO;
1547         }
1548
1549         return (0);
1550
1551 }
1552
1553 /*
1554  * HP PC Lan+ : Set the physical link to use AUI or TP/TL.
1555  */
1556
1557 static void
1558 ed_hpp_set_physical_link(struct ed_softc *sc)
1559 {
1560         struct ifnet *ifp = &sc->arpcom.ac_if;
1561         int lan_page;
1562
1563         ed_asic_outw(sc, ED_HPP_PAGING, ED_HPP_PAGE_LAN);
1564         lan_page = ed_asic_inw(sc, ED_HPP_PAGE_0);
1565
1566         if (ifp->if_flags & IFF_ALTPHYS) {
1567
1568                 /*
1569                  * Use the AUI port.
1570                  */
1571
1572                 lan_page |= ED_HPP_LAN_AUI;
1573
1574                 ed_asic_outw(sc, ED_HPP_PAGING, ED_HPP_PAGE_LAN);
1575                 ed_asic_outw(sc, ED_HPP_PAGE_0, lan_page);
1576
1577
1578         } else {
1579
1580                 /*
1581                  * Use the ThinLan interface
1582                  */
1583
1584                 lan_page &= ~ED_HPP_LAN_AUI;
1585
1586                 ed_asic_outw(sc, ED_HPP_PAGING, ED_HPP_PAGE_LAN);
1587                 ed_asic_outw(sc, ED_HPP_PAGE_0, lan_page);
1588
1589         }
1590
1591         /*
1592          * Wait for the lan card to re-initialize itself
1593          */
1594
1595         DELAY(150000);  /* wait 150 ms */
1596
1597         /*
1598          * Restore normal pages.
1599          */
1600
1601         ed_asic_outw(sc, ED_HPP_PAGING, ED_HPP_PAGE_PERF);
1602
1603 }
1604
1605 /*
1606  * Allocate a port resource with the given resource id.
1607  */
1608 int
1609 ed_alloc_port(dev, rid, size)
1610         device_t dev;
1611         int rid;
1612         int size;
1613 {
1614         struct ed_softc *sc = device_get_softc(dev);
1615         struct resource *res;
1616
1617         res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
1618                                  0ul, ~0ul, size, RF_ACTIVE);
1619         if (res) {
1620                 sc->port_rid = rid;
1621                 sc->port_res = res;
1622                 sc->port_used = size;
1623                 return (0);
1624         } else {
1625                 return (ENOENT);
1626         }
1627 }
1628
1629 /*
1630  * Allocate a memory resource with the given resource id.
1631  */
1632 int
1633 ed_alloc_memory(dev, rid, size)
1634         device_t dev;
1635         int rid;
1636         int size;
1637 {
1638         struct ed_softc *sc = device_get_softc(dev);
1639         struct resource *res;
1640
1641         res = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid,
1642                                  0ul, ~0ul, size, RF_ACTIVE);
1643         if (res) {
1644                 sc->mem_rid = rid;
1645                 sc->mem_res = res;
1646                 sc->mem_used = size;
1647                 return (0);
1648         } else {
1649                 return (ENOENT);
1650         }
1651 }
1652
1653 /*
1654  * Allocate an irq resource with the given resource id.
1655  */
1656 int
1657 ed_alloc_irq(dev, rid, flags)
1658         device_t dev;
1659         int rid;
1660         int flags;
1661 {
1662         struct ed_softc *sc = device_get_softc(dev);
1663         struct resource *res;
1664
1665         res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
1666             (RF_ACTIVE | flags));
1667         if (res) {
1668                 sc->irq_rid = rid;
1669                 sc->irq_res = res;
1670                 return (0);
1671         } else {
1672                 return (ENOENT);
1673         }
1674 }
1675
1676 /*
1677  * Release all resources
1678  */
1679 void
1680 ed_release_resources(dev)
1681         device_t dev;
1682 {
1683         struct ed_softc *sc = device_get_softc(dev);
1684
1685         if (sc->port_res) {
1686                 bus_deactivate_resource(dev, SYS_RES_IOPORT,
1687                                         sc->port_rid, sc->port_res);
1688                 bus_release_resource(dev, SYS_RES_IOPORT,
1689                                      sc->port_rid, sc->port_res);
1690                 sc->port_res = 0;
1691         }
1692         if (sc->mem_res) {
1693                 bus_deactivate_resource(dev, SYS_RES_MEMORY,
1694                                         sc->mem_rid, sc->mem_res);
1695                 bus_release_resource(dev, SYS_RES_MEMORY,
1696                                      sc->mem_rid, sc->mem_res);
1697                 sc->mem_res = 0;
1698         }
1699         if (sc->irq_res) {
1700                 bus_deactivate_resource(dev, SYS_RES_IRQ,
1701                                         sc->irq_rid, sc->irq_res);
1702                 bus_release_resource(dev, SYS_RES_IRQ,
1703                                      sc->irq_rid, sc->irq_res);
1704                 sc->irq_res = 0;
1705         }
1706 }
1707
1708 /*
1709  * Install interface into kernel networking data structures
1710  */
1711 int
1712 ed_attach(device_t dev)
1713 {
1714         struct ed_softc *sc = device_get_softc(dev);
1715         struct ifnet *ifp = &sc->arpcom.ac_if;
1716
1717         callout_init(&sc->ed_timer);
1718         /*
1719          * Set interface to stopped condition (reset)
1720          */
1721         ed_stop(sc);
1722
1723         /*
1724          * Initialize ifnet structure
1725          */
1726         ifp->if_softc = sc;
1727         if_initname(ifp, device_get_name(dev), device_get_unit(dev));
1728         ifp->if_mtu = ETHERMTU;
1729         ifp->if_start = ed_start;
1730         ifp->if_ioctl = ed_ioctl;
1731         ifp->if_watchdog = ed_watchdog;
1732         ifp->if_init = ed_init;
1733         ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
1734         ifq_set_ready(&ifp->if_snd);
1735         ifp->if_linkmib = &sc->mibdata;
1736         ifp->if_linkmiblen = sizeof sc->mibdata;
1737         ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1738         /*
1739          * XXX - should do a better job.
1740          */
1741         if (sc->chip_type == ED_CHIP_TYPE_WD790)
1742                 sc->mibdata.dot3StatsEtherChipSet =
1743                         DOT3CHIPSET(dot3VendorWesternDigital,
1744                                     dot3ChipSetWesternDigital83C790);
1745         else
1746                 sc->mibdata.dot3StatsEtherChipSet =
1747                         DOT3CHIPSET(dot3VendorNational, 
1748                                     dot3ChipSetNational8390);
1749         sc->mibdata.dot3Compliance = DOT3COMPLIANCE_COLLS;
1750
1751         /*
1752          * Set default state for ALTPHYS flag (used to disable the 
1753          * tranceiver for AUI operation), based on compile-time 
1754          * config option.
1755          */
1756         if (device_get_flags(dev) & ED_FLAGS_DISABLE_TRANCEIVER)
1757                 ifp->if_flags |= IFF_ALTPHYS;
1758
1759         /*
1760          * Attach the interface
1761          */
1762         ether_ifattach(ifp, sc->arpcom.ac_enaddr);
1763
1764         /* device attach does transition from UNCONFIGURED to IDLE state */
1765
1766         if (sc->type_str && (*sc->type_str != 0))
1767                 printf("type %s ", sc->type_str);
1768         else
1769                 printf("type unknown (0x%x) ", sc->type);
1770
1771         if (sc->vendor == ED_VENDOR_HP)
1772                 printf("(%s %s IO)", (sc->hpp_id & ED_HPP_ID_16_BIT_ACCESS) ?
1773                         "16-bit" : "32-bit",
1774                         sc->hpp_mem_start ? "memory mapped" : "regular");
1775         else
1776                 printf("%s ", sc->isa16bit ? "(16 bit)" : "(8 bit)");
1777
1778         printf("%s\n", (((sc->vendor == ED_VENDOR_3COM) ||
1779                          (sc->vendor == ED_VENDOR_HP)) &&
1780                 (ifp->if_flags & IFF_ALTPHYS)) ? " tranceiver disabled" : "");
1781
1782         return (0);
1783 }
1784
1785 /*
1786  * Reset interface.
1787  */
1788 static void
1789 ed_reset(ifp)
1790         struct ifnet *ifp;
1791 {
1792         struct ed_softc *sc = ifp->if_softc;
1793
1794         crit_enter();
1795
1796         if (sc->gone) {
1797                 crit_exit();
1798                 return;
1799         }
1800
1801         /*
1802          * Stop interface and re-initialize.
1803          */
1804         ed_stop(sc);
1805         ed_init(sc);
1806
1807         crit_exit();
1808 }
1809
1810 /*
1811  * Take interface offline.
1812  */
1813 void
1814 ed_stop(sc)
1815         struct ed_softc *sc;
1816 {
1817         int     n = 5000;
1818
1819 #ifndef ED_NO_MIIBUS
1820         callout_stop(&sc->ed_timer);
1821 #endif
1822         if (sc->gone)
1823                 return;
1824         /*
1825          * Stop everything on the interface, and select page 0 registers.
1826          */
1827         ed_nic_outb(sc, ED_P0_CR, sc->cr_proto | ED_CR_STP);
1828
1829         /*
1830          * Wait for interface to enter stopped state, but limit # of checks to
1831          * 'n' (about 5ms). It shouldn't even take 5us on modern DS8390's, but
1832          * just in case it's an old one.
1833          */
1834         if (sc->chip_type != ED_CHIP_TYPE_AX88190)
1835                 while (((ed_nic_inb(sc, ED_P0_ISR) & ED_ISR_RST) == 0) && --n);
1836 }
1837
1838 /*
1839  * Device timeout/watchdog routine. Entered if the device neglects to
1840  *      generate an interrupt after a transmit has been started on it.
1841  */
1842 static void
1843 ed_watchdog(ifp)
1844         struct ifnet *ifp;
1845 {
1846         struct ed_softc *sc = ifp->if_softc;
1847
1848         if (sc->gone)
1849                 return;
1850         log(LOG_ERR, "%s: device timeout\n", ifp->if_xname);
1851         ifp->if_oerrors++;
1852
1853         ed_reset(ifp);
1854 }
1855
1856 #ifndef ED_NO_MIIBUS
1857 static void
1858 ed_tick(arg)
1859         void *arg;
1860 {
1861         struct ed_softc *sc = arg;
1862         struct mii_data *mii;
1863
1864         crit_enter();
1865
1866         if (sc->gone) {
1867                 crit_exit();
1868                 return;
1869         }
1870
1871         if (sc->miibus != NULL) {
1872                 mii = device_get_softc(sc->miibus);
1873                 mii_tick(mii);
1874         }
1875
1876         callout_reset(&sc->ed_timer, hz, ed_tick, sc);
1877
1878         crit_exit();
1879 }
1880 #endif
1881
1882 /*
1883  * Initialize device.
1884  */
1885 static void
1886 ed_init(xsc)
1887         void *xsc;
1888 {
1889         struct ed_softc *sc = xsc;
1890         struct ifnet *ifp = &sc->arpcom.ac_if;
1891         int i;
1892
1893         crit_enter();
1894
1895         if (sc->gone) {
1896                 crit_exit();
1897                 return;
1898         }
1899
1900         /* address not known */
1901         if (TAILQ_EMPTY(&ifp->if_addrhead)) { /* unlikely? XXX */
1902                 crit_exit();
1903                 return;
1904         }
1905
1906         /*
1907          * Initialize the NIC in the exact order outlined in the NS manual.
1908          * This init procedure is "mandatory"...don't change what or when
1909          * things happen.
1910          */
1911
1912         /* reset transmitter flags */
1913         sc->xmit_busy = 0;
1914         ifp->if_timer = 0;
1915
1916         sc->txb_inuse = 0;
1917         sc->txb_new = 0;
1918         sc->txb_next_tx = 0;
1919
1920         /* This variable is used below - don't move this assignment */
1921         sc->next_packet = sc->rec_page_start + 1;
1922
1923         /*
1924          * Set interface for page 0, Remote DMA complete, Stopped
1925          */
1926         ed_nic_outb(sc, ED_P0_CR, sc->cr_proto | ED_CR_STP);
1927
1928         if (sc->isa16bit) {
1929
1930                 /*
1931                  * Set FIFO threshold to 8, No auto-init Remote DMA, byte
1932                  * order=80x86, word-wide DMA xfers,
1933                  */
1934                 ed_nic_outb(sc, ED_P0_DCR, ED_DCR_FT1 | ED_DCR_WTS | ED_DCR_LS);
1935         } else {
1936
1937                 /*
1938                  * Same as above, but byte-wide DMA xfers
1939                  */
1940                 ed_nic_outb(sc, ED_P0_DCR, ED_DCR_FT1 | ED_DCR_LS);
1941         }
1942
1943         /*
1944          * Clear Remote Byte Count Registers
1945          */
1946         ed_nic_outb(sc, ED_P0_RBCR0, 0);
1947         ed_nic_outb(sc, ED_P0_RBCR1, 0);
1948
1949         /*
1950          * For the moment, don't store incoming packets in memory.
1951          */
1952         ed_nic_outb(sc, ED_P0_RCR, ED_RCR_MON);
1953
1954         /*
1955          * Place NIC in internal loopback mode
1956          */
1957         ed_nic_outb(sc, ED_P0_TCR, ED_TCR_LB0);
1958
1959         /*
1960          * Initialize transmit/receive (ring-buffer) Page Start
1961          */
1962         ed_nic_outb(sc, ED_P0_TPSR, sc->tx_page_start);
1963         ed_nic_outb(sc, ED_P0_PSTART, sc->rec_page_start);
1964         /* Set lower bits of byte addressable framing to 0 */
1965         if (sc->chip_type == ED_CHIP_TYPE_WD790)
1966                 ed_nic_outb(sc, 0x09, 0);
1967
1968         /*
1969          * Initialize Receiver (ring-buffer) Page Stop and Boundry
1970          */
1971         ed_nic_outb(sc, ED_P0_PSTOP, sc->rec_page_stop);
1972         ed_nic_outb(sc, ED_P0_BNRY, sc->rec_page_start);
1973
1974         /*
1975          * Clear all interrupts. A '1' in each bit position clears the
1976          * corresponding flag.
1977          */
1978         ed_nic_outb(sc, ED_P0_ISR, 0xff);
1979
1980         /*
1981          * Enable the following interrupts: receive/transmit complete,
1982          * receive/transmit error, and Receiver OverWrite.
1983          *
1984          * Counter overflow and Remote DMA complete are *not* enabled.
1985          */
1986         ed_nic_outb(sc, ED_P0_IMR,
1987         ED_IMR_PRXE | ED_IMR_PTXE | ED_IMR_RXEE | ED_IMR_TXEE | ED_IMR_OVWE);
1988
1989         /*
1990          * Program Command Register for page 1
1991          */
1992         ed_nic_outb(sc, ED_P0_CR, sc->cr_proto | ED_CR_PAGE_1 | ED_CR_STP);
1993
1994         /*
1995          * Copy out our station address
1996          */
1997         for (i = 0; i < ETHER_ADDR_LEN; ++i)
1998                 ed_nic_outb(sc, ED_P1_PAR(i), sc->arpcom.ac_enaddr[i]);
1999
2000         /*
2001          * Set Current Page pointer to next_packet (initialized above)
2002          */
2003         ed_nic_outb(sc, ED_P1_CURR, sc->next_packet);
2004
2005         /*
2006          * Program Receiver Configuration Register and multicast filter. CR is
2007          * set to page 0 on return.
2008          */
2009         ed_setrcr(sc);
2010
2011         /*
2012          * Take interface out of loopback
2013          */
2014         ed_nic_outb(sc, ED_P0_TCR, 0);
2015
2016         /*
2017          * If this is a 3Com board, the tranceiver must be software enabled
2018          * (there is no settable hardware default).
2019          */
2020         if (sc->vendor == ED_VENDOR_3COM) {
2021                 if (ifp->if_flags & IFF_ALTPHYS) {
2022                         ed_asic_outb(sc, ED_3COM_CR, 0);
2023                 } else {
2024                         ed_asic_outb(sc, ED_3COM_CR, ED_3COM_CR_XSEL);
2025                 }
2026         }
2027
2028 #ifndef ED_NO_MIIBUS
2029         if (sc->miibus != NULL) {
2030                 struct mii_data *mii;
2031                 mii = device_get_softc(sc->miibus);
2032                 mii_mediachg(mii);
2033         }
2034 #endif
2035         /*
2036          * Set 'running' flag, and clear output active flag.
2037          */
2038         ifp->if_flags |= IFF_RUNNING;
2039         ifp->if_flags &= ~IFF_OACTIVE;
2040
2041         /*
2042          * ...and attempt to start output
2043          */
2044         ed_start(ifp);
2045
2046 #ifndef ED_NO_MIIBUS
2047         callout_reset(&sc->ed_timer, hz, ed_tick, sc);
2048 #endif
2049
2050         crit_exit();
2051 }
2052
2053 /*
2054  * This routine actually starts the transmission on the interface
2055  */
2056 static __inline void
2057 ed_xmit(sc)
2058         struct ed_softc *sc;
2059 {
2060         struct ifnet *ifp = (struct ifnet *)sc;
2061         unsigned short len;
2062
2063         if (sc->gone)
2064                 return;
2065         len = sc->txb_len[sc->txb_next_tx];
2066
2067         /*
2068          * Set NIC for page 0 register access
2069          */
2070         ed_nic_outb(sc, ED_P0_CR, sc->cr_proto | ED_CR_STA);
2071
2072         /*
2073          * Set TX buffer start page
2074          */
2075         ed_nic_outb(sc, ED_P0_TPSR, sc->tx_page_start +
2076                     sc->txb_next_tx * ED_TXBUF_SIZE);
2077
2078         /*
2079          * Set TX length
2080          */
2081         ed_nic_outb(sc, ED_P0_TBCR0, len);
2082         ed_nic_outb(sc, ED_P0_TBCR1, len >> 8);
2083
2084         /*
2085          * Set page 0, Remote DMA complete, Transmit Packet, and *Start*
2086          */
2087         ed_nic_outb(sc, ED_P0_CR, sc->cr_proto | ED_CR_TXP | ED_CR_STA);
2088         sc->xmit_busy = 1;
2089
2090         /*
2091          * Point to next transmit buffer slot and wrap if necessary.
2092          */
2093         sc->txb_next_tx++;
2094         if (sc->txb_next_tx == sc->txb_cnt)
2095                 sc->txb_next_tx = 0;
2096
2097         /*
2098          * Set a timer just in case we never hear from the board again
2099          */
2100         ifp->if_timer = 2;
2101 }
2102
2103 /*
2104  * Start output on interface.
2105  * We make two assumptions here:
2106  *  1) that the current priority is set to splimp _before_ this code
2107  *     is called *and* is returned to the appropriate priority after
2108  *     return
2109  *  2) that the IFF_OACTIVE flag is checked before this code is called
2110  *     (i.e. that the output part of the interface is idle)
2111  */
2112 static void
2113 ed_start(ifp)
2114         struct ifnet *ifp;
2115 {
2116         struct ed_softc *sc = ifp->if_softc;
2117         struct mbuf *m0, *m;
2118         caddr_t buffer;
2119         int     len;
2120
2121         if (sc->gone) {
2122                 printf("ed_start(%p) GONE\n",ifp);
2123                 return;
2124         }
2125 outloop:
2126
2127         /*
2128          * First, see if there are buffered packets and an idle transmitter -
2129          * should never happen at this point.
2130          */
2131         if (sc->txb_inuse && (sc->xmit_busy == 0)) {
2132                 printf("ed: packets buffered, but transmitter idle\n");
2133                 ed_xmit(sc);
2134         }
2135
2136         /*
2137          * See if there is room to put another packet in the buffer.
2138          */
2139         if (sc->txb_inuse == sc->txb_cnt) {
2140
2141                 /*
2142                  * No room. Indicate this to the outside world and exit.
2143                  */
2144                 ifp->if_flags |= IFF_OACTIVE;
2145                 return;
2146         }
2147         m = ifq_dequeue(&ifp->if_snd);
2148         if (m == 0) {
2149
2150                 /*
2151                  * We are using the !OACTIVE flag to indicate to the outside
2152                  * world that we can accept an additional packet rather than
2153                  * that the transmitter is _actually_ active. Indeed, the
2154                  * transmitter may be active, but if we haven't filled all the
2155                  * buffers with data then we still want to accept more.
2156                  */
2157                 ifp->if_flags &= ~IFF_OACTIVE;
2158                 return;
2159         }
2160
2161         /*
2162          * Copy the mbuf chain into the transmit buffer
2163          */
2164
2165         m0 = m;
2166
2167         /* txb_new points to next open buffer slot */
2168         buffer = sc->mem_start + (sc->txb_new * ED_TXBUF_SIZE * ED_PAGE_SIZE);
2169
2170         if (sc->mem_shared) {
2171
2172                 /*
2173                  * Special case setup for 16 bit boards...
2174                  */
2175                 if (sc->isa16bit) {
2176                         switch (sc->vendor) {
2177
2178                                 /*
2179                                  * For 16bit 3Com boards (which have 16k of
2180                                  * memory), we have the xmit buffers in a
2181                                  * different page of memory ('page 0') - so
2182                                  * change pages.
2183                                  */
2184                         case ED_VENDOR_3COM:
2185                                 ed_asic_outb(sc, ED_3COM_GACFR,
2186                                              ED_3COM_GACFR_RSEL);
2187                                 break;
2188
2189                                 /*
2190                                  * Enable 16bit access to shared memory on
2191                                  * WD/SMC boards.
2192                                  */
2193                         case ED_VENDOR_WD_SMC:
2194                                 ed_asic_outb(sc, ED_WD_LAAR,
2195                                              sc->wd_laar_proto | ED_WD_LAAR_M16EN);
2196                                 if (sc->chip_type == ED_CHIP_TYPE_WD790) {
2197                                         ed_asic_outb(sc, ED_WD_MSR, ED_WD_MSR_MENB);
2198                                 }
2199                                 break;
2200                         }
2201                 }
2202                 for (len = 0; m != 0; m = m->m_next) {
2203                         bcopy(mtod(m, caddr_t), buffer, m->m_len);
2204                         buffer += m->m_len;
2205                         len += m->m_len;
2206                 }
2207
2208                 /*
2209                  * Restore previous shared memory access
2210                  */
2211                 if (sc->isa16bit) {
2212                         switch (sc->vendor) {
2213                         case ED_VENDOR_3COM:
2214                                 ed_asic_outb(sc, ED_3COM_GACFR,
2215                                              ED_3COM_GACFR_RSEL | ED_3COM_GACFR_MBS0);
2216                                 break;
2217                         case ED_VENDOR_WD_SMC:
2218                                 if (sc->chip_type == ED_CHIP_TYPE_WD790) {
2219                                         ed_asic_outb(sc, ED_WD_MSR, 0x00);
2220                                 }
2221                                 ed_asic_outb(sc, ED_WD_LAAR,
2222                                              sc->wd_laar_proto & ~ED_WD_LAAR_M16EN);
2223                                 break;
2224                         }
2225                 }
2226         } else {
2227                 len = ed_pio_write_mbufs(sc, m, (int)buffer);
2228                 if (len == 0) {
2229                         m_freem(m0);
2230                         goto outloop;
2231                 }
2232         }
2233
2234         sc->txb_len[sc->txb_new] = max(len, (ETHER_MIN_LEN-ETHER_CRC_LEN));
2235
2236         sc->txb_inuse++;
2237
2238         /*
2239          * Point to next buffer slot and wrap if necessary.
2240          */
2241         sc->txb_new++;
2242         if (sc->txb_new == sc->txb_cnt)
2243                 sc->txb_new = 0;
2244
2245         if (sc->xmit_busy == 0)
2246                 ed_xmit(sc);
2247
2248         BPF_MTAP(ifp, m0);
2249
2250         m_freem(m0);
2251
2252         /*
2253          * Loop back to the top to possibly buffer more packets
2254          */
2255         goto outloop;
2256 }
2257
2258 /*
2259  * Ethernet interface receiver interrupt.
2260  */
2261 static __inline void
2262 ed_rint(sc)
2263         struct ed_softc *sc;
2264 {
2265         struct ifnet *ifp = &sc->arpcom.ac_if;
2266         u_char  boundry;
2267         u_short len;
2268         struct ed_ring packet_hdr;
2269         char   *packet_ptr;
2270
2271         if (sc->gone)
2272                 return;
2273
2274         /*
2275          * Set NIC to page 1 registers to get 'current' pointer
2276          */
2277         ed_nic_outb(sc, ED_P0_CR, sc->cr_proto | ED_CR_PAGE_1 | ED_CR_STA);
2278
2279         /*
2280          * 'sc->next_packet' is the logical beginning of the ring-buffer -
2281          * i.e. it points to where new data has been buffered. The 'CURR'
2282          * (current) register points to the logical end of the ring-buffer -
2283          * i.e. it points to where additional new data will be added. We loop
2284          * here until the logical beginning equals the logical end (or in
2285          * other words, until the ring-buffer is empty).
2286          */
2287         while (sc->next_packet != ed_nic_inb(sc, ED_P1_CURR)) {
2288
2289                 /* get pointer to this buffer's header structure */
2290                 packet_ptr = sc->mem_ring +
2291                     (sc->next_packet - sc->rec_page_start) * ED_PAGE_SIZE;
2292
2293                 /*
2294                  * The byte count includes a 4 byte header that was added by
2295                  * the NIC.
2296                  */
2297                 if (sc->mem_shared)
2298                         packet_hdr = *(struct ed_ring *) packet_ptr;
2299                 else
2300                         ed_pio_readmem(sc, (int)packet_ptr, (char *) &packet_hdr,
2301                                        sizeof(packet_hdr));
2302                 len = packet_hdr.count;
2303                 if (len > (ETHER_MAX_LEN - ETHER_CRC_LEN + sizeof(struct ed_ring)) ||
2304                     len < (ETHER_MIN_LEN - ETHER_CRC_LEN + sizeof(struct ed_ring))) {
2305                         /*
2306                          * Length is a wild value. There's a good chance that
2307                          * this was caused by the NIC being old and buggy.
2308                          * The bug is that the length low byte is duplicated in
2309                          * the high byte. Try to recalculate the length based on
2310                          * the pointer to the next packet.
2311                          */
2312                         /*
2313                          * NOTE: sc->next_packet is pointing at the current packet.
2314                          */
2315                         len &= ED_PAGE_SIZE - 1;        /* preserve offset into page */
2316                         if (packet_hdr.next_packet >= sc->next_packet) {
2317                                 len += (packet_hdr.next_packet - sc->next_packet) * ED_PAGE_SIZE;
2318                         } else {
2319                                 len += ((packet_hdr.next_packet - sc->rec_page_start) +
2320                                         (sc->rec_page_stop - sc->next_packet)) * ED_PAGE_SIZE;
2321                         }
2322                         /*
2323                          * because buffers are aligned on 256-byte boundary,
2324                          * the length computed above is off by 256 in almost
2325                          * all cases. Fix it...
2326                          */
2327                         if (len & 0xff)
2328                                 len -= 256 ;
2329                         if (len > (ETHER_MAX_LEN - ETHER_CRC_LEN 
2330                                    + sizeof(struct ed_ring)))
2331                                 sc->mibdata.dot3StatsFrameTooLongs++;
2332                 }
2333                 /*
2334                  * Be fairly liberal about what we allow as a "reasonable" length
2335                  * so that a [crufty] packet will make it to BPF (and can thus
2336                  * be analyzed). Note that all that is really important is that
2337                  * we have a length that will fit into one mbuf cluster or less;
2338                  * the upper layer protocols can then figure out the length from
2339                  * their own length field(s).
2340                  * But make sure that we have at least a full ethernet header
2341                  * or we would be unable to call ether_input() later.
2342                  */
2343                 if ((len >= sizeof(struct ed_ring) + ETHER_HDR_LEN) &&
2344                     (len <= MCLBYTES) &&
2345                     (packet_hdr.next_packet >= sc->rec_page_start) &&
2346                     (packet_hdr.next_packet < sc->rec_page_stop)) {
2347                         /*
2348                          * Go get packet.
2349                          */
2350                         ed_get_packet(sc, packet_ptr + sizeof(struct ed_ring),
2351                                       len - sizeof(struct ed_ring));
2352                         ifp->if_ipackets++;
2353                 } else {
2354                         /*
2355                          * Really BAD. The ring pointers are corrupted.
2356                          */
2357                         log(LOG_ERR,
2358                             "%s: NIC memory corrupt - invalid packet length %d\n",
2359                             ifp->if_xname, len);
2360                         ifp->if_ierrors++;
2361                         ed_reset(ifp);
2362                         return;
2363                 }
2364
2365                 /*
2366                  * Update next packet pointer
2367                  */
2368                 sc->next_packet = packet_hdr.next_packet;
2369
2370                 /*
2371                  * Update NIC boundry pointer - being careful to keep it one
2372                  * buffer behind. (as recommended by NS databook)
2373                  */
2374                 boundry = sc->next_packet - 1;
2375                 if (boundry < sc->rec_page_start)
2376                         boundry = sc->rec_page_stop - 1;
2377
2378                 /*
2379                  * Set NIC to page 0 registers to update boundry register
2380                  */
2381                 ed_nic_outb(sc, ED_P0_CR, sc->cr_proto | ED_CR_STA);
2382
2383                 ed_nic_outb(sc, ED_P0_BNRY, boundry);
2384
2385                 /*
2386                  * Set NIC to page 1 registers before looping to top (prepare
2387                  * to get 'CURR' current pointer)
2388                  */
2389                 ed_nic_outb(sc, ED_P0_CR, sc->cr_proto | ED_CR_PAGE_1 | ED_CR_STA);
2390         }
2391 }
2392
2393 /*
2394  * Ethernet interface interrupt processor
2395  */
2396 void
2397 edintr(arg)
2398         void *arg;
2399 {
2400         struct ed_softc *sc = (struct ed_softc*) arg;
2401         struct ifnet *ifp = (struct ifnet *)sc;
2402         u_char  isr;
2403         int     count;
2404
2405         if (sc->gone)
2406                 return;
2407         /*
2408          * Set NIC to page 0 registers
2409          */
2410         ed_nic_outb(sc, ED_P0_CR, sc->cr_proto | ED_CR_STA);
2411
2412         /*
2413          * loop until there are no more new interrupts.  When the card
2414          * goes away, the hardware will read back 0xff.  Looking at
2415          * the interrupts, it would appear that 0xff is impossible,
2416          * or at least extremely unlikely.
2417          */
2418         while ((isr = ed_nic_inb(sc, ED_P0_ISR)) != 0 && isr != 0xff) {
2419
2420                 /*
2421                  * reset all the bits that we are 'acknowledging' by writing a
2422                  * '1' to each bit position that was set (writing a '1'
2423                  * *clears* the bit)
2424                  */
2425                 ed_nic_outb(sc, ED_P0_ISR, isr);
2426
2427                 /* 
2428                  * XXX workaround for AX88190
2429                  * We limit this to 5000 iterations.  At 1us per inb/outb,
2430                  * this translates to about 15ms, which should be plenty
2431                  * of time, and also gives protection in the card eject
2432                  * case.
2433                  */
2434                 if (sc->chip_type == ED_CHIP_TYPE_AX88190) {
2435                         count = 5000;           /* 15ms */
2436                         while (count-- && (ed_nic_inb(sc, ED_P0_ISR) & isr)) {
2437                                 ed_nic_outb(sc, ED_P0_ISR,0);
2438                                 ed_nic_outb(sc, ED_P0_ISR,isr);
2439                         }
2440                         if (count == 0)
2441                                 break;
2442                 }
2443
2444                 /*
2445                  * Handle transmitter interrupts. Handle these first because
2446                  * the receiver will reset the board under some conditions.
2447                  */
2448                 if (isr & (ED_ISR_PTX | ED_ISR_TXE)) {
2449                         u_char  collisions = ed_nic_inb(sc, ED_P0_NCR) & 0x0f;
2450
2451                         /*
2452                          * Check for transmit error. If a TX completed with an
2453                          * error, we end up throwing the packet away. Really
2454                          * the only error that is possible is excessive
2455                          * collisions, and in this case it is best to allow
2456                          * the automatic mechanisms of TCP to backoff the
2457                          * flow. Of course, with UDP we're screwed, but this
2458                          * is expected when a network is heavily loaded.
2459                          */
2460                         (void) ed_nic_inb(sc, ED_P0_TSR);
2461                         if (isr & ED_ISR_TXE) {
2462                                 u_char tsr;
2463
2464                                 /*
2465                                  * Excessive collisions (16)
2466                                  */
2467                                 tsr = ed_nic_inb(sc, ED_P0_TSR);
2468                                 if ((tsr & ED_TSR_ABT)  
2469                                     && (collisions == 0)) {
2470
2471                                         /*
2472                                          * When collisions total 16, the
2473                                          * P0_NCR will indicate 0, and the
2474                                          * TSR_ABT is set.
2475                                          */
2476                                         collisions = 16;
2477                                         sc->mibdata.dot3StatsExcessiveCollisions++;
2478                                         sc->mibdata.dot3StatsCollFrequencies[15]++;
2479                                 }
2480                                 if (tsr & ED_TSR_OWC)
2481                                         sc->mibdata.dot3StatsLateCollisions++;
2482                                 if (tsr & ED_TSR_CDH)
2483                                         sc->mibdata.dot3StatsSQETestErrors++;
2484                                 if (tsr & ED_TSR_CRS)
2485                                         sc->mibdata.dot3StatsCarrierSenseErrors++;
2486                                 if (tsr & ED_TSR_FU)
2487                                         sc->mibdata.dot3StatsInternalMacTransmitErrors++;
2488
2489                                 /*
2490                                  * update output errors counter
2491                                  */
2492                                 ifp->if_oerrors++;
2493                         } else {
2494
2495                                 /*
2496                                  * Update total number of successfully
2497                                  * transmitted packets.
2498                                  */
2499                                 ifp->if_opackets++;
2500                         }
2501
2502                         /*
2503                          * reset tx busy and output active flags
2504                          */
2505                         sc->xmit_busy = 0;
2506                         ifp->if_flags &= ~IFF_OACTIVE;
2507
2508                         /*
2509                          * clear watchdog timer
2510                          */
2511                         ifp->if_timer = 0;
2512
2513                         /*
2514                          * Add in total number of collisions on last
2515                          * transmission.
2516                          */
2517                         ifp->if_collisions += collisions;
2518                         switch(collisions) {
2519                         case 0:
2520                         case 16:
2521                                 break;
2522                         case 1:
2523                                 sc->mibdata.dot3StatsSingleCollisionFrames++;
2524                                 sc->mibdata.dot3StatsCollFrequencies[0]++;
2525                                 break;
2526                         default:
2527                                 sc->mibdata.dot3StatsMultipleCollisionFrames++;
2528                                 sc->mibdata.
2529                                         dot3StatsCollFrequencies[collisions-1]
2530                                                 ++;
2531                                 break;
2532                         }
2533
2534                         /*
2535                          * Decrement buffer in-use count if not zero (can only
2536                          * be zero if a transmitter interrupt occured while
2537                          * not actually transmitting). If data is ready to
2538                          * transmit, start it transmitting, otherwise defer
2539                          * until after handling receiver
2540                          */
2541                         if (sc->txb_inuse && --sc->txb_inuse)
2542                                 ed_xmit(sc);
2543                 }
2544
2545                 /*
2546                  * Handle receiver interrupts
2547                  */
2548                 if (isr & (ED_ISR_PRX | ED_ISR_RXE | ED_ISR_OVW)) {
2549
2550                         /*
2551                          * Overwrite warning. In order to make sure that a
2552                          * lockup of the local DMA hasn't occurred, we reset
2553                          * and re-init the NIC. The NSC manual suggests only a
2554                          * partial reset/re-init is necessary - but some chips
2555                          * seem to want more. The DMA lockup has been seen
2556                          * only with early rev chips - Methinks this bug was
2557                          * fixed in later revs. -DG
2558                          */
2559                         if (isr & ED_ISR_OVW) {
2560                                 ifp->if_ierrors++;
2561 #ifdef DIAGNOSTIC
2562                                 log(LOG_WARNING,
2563                                     "%s: warning - receiver ring buffer overrun\n",
2564                                     ifp->if_xname);
2565 #endif
2566
2567                                 /*
2568                                  * Stop/reset/re-init NIC
2569                                  */
2570                                 ed_reset(ifp);
2571                         } else {
2572
2573                                 /*
2574                                  * Receiver Error. One or more of: CRC error,
2575                                  * frame alignment error FIFO overrun, or
2576                                  * missed packet.
2577                                  */
2578                                 if (isr & ED_ISR_RXE) {
2579                                         u_char rsr;
2580                                         rsr = ed_nic_inb(sc, ED_P0_RSR);
2581                                         if (rsr & ED_RSR_CRC)
2582                                                 sc->mibdata.dot3StatsFCSErrors++;
2583                                         if (rsr & ED_RSR_FAE)
2584                                                 sc->mibdata.dot3StatsAlignmentErrors++;
2585                                         if (rsr & ED_RSR_FO)
2586                                                 sc->mibdata.dot3StatsInternalMacReceiveErrors++;
2587                                         ifp->if_ierrors++;
2588 #ifdef ED_DEBUG
2589                                         if_printf("receive error %x\n",
2590                                                ed_nic_inb(sc, ED_P0_RSR));
2591 #endif
2592                                 }
2593
2594                                 /*
2595                                  * Go get the packet(s) XXX - Doing this on an
2596                                  * error is dubious because there shouldn't be
2597                                  * any data to get (we've configured the
2598                                  * interface to not accept packets with
2599                                  * errors).
2600                                  */
2601
2602                                 /*
2603                                  * Enable 16bit access to shared memory first
2604                                  * on WD/SMC boards.
2605                                  */
2606                                 if (sc->isa16bit &&
2607                                     (sc->vendor == ED_VENDOR_WD_SMC)) {
2608
2609                                         ed_asic_outb(sc, ED_WD_LAAR,
2610                                                      sc->wd_laar_proto | ED_WD_LAAR_M16EN);
2611                                         if (sc->chip_type == ED_CHIP_TYPE_WD790) {
2612                                                 ed_asic_outb(sc, ED_WD_MSR,
2613                                                              ED_WD_MSR_MENB);
2614                                         }
2615                                 }
2616                                 ed_rint(sc);
2617
2618                                 /* disable 16bit access */
2619                                 if (sc->isa16bit &&
2620                                     (sc->vendor == ED_VENDOR_WD_SMC)) {
2621
2622                                         if (sc->chip_type == ED_CHIP_TYPE_WD790) {
2623                                                 ed_asic_outb(sc, ED_WD_MSR, 0x00);
2624                                         }
2625                                         ed_asic_outb(sc, ED_WD_LAAR,
2626                                                      sc->wd_laar_proto & ~ED_WD_LAAR_M16EN);
2627                                 }
2628                         }
2629                 }
2630
2631                 /*
2632                  * If it looks like the transmitter can take more data,
2633                  * attempt to start output on the interface. This is done
2634                  * after handling the receiver to give the receiver priority.
2635                  */
2636                 if ((ifp->if_flags & IFF_OACTIVE) == 0)
2637                         ed_start(ifp);
2638
2639                 /*
2640                  * return NIC CR to standard state: page 0, remote DMA
2641                  * complete, start (toggling the TXP bit off, even if was just
2642                  * set in the transmit routine, is *okay* - it is 'edge'
2643                  * triggered from low to high)
2644                  */
2645                 ed_nic_outb(sc, ED_P0_CR, sc->cr_proto | ED_CR_STA);
2646
2647                 /*
2648                  * If the Network Talley Counters overflow, read them to reset
2649                  * them. It appears that old 8390's won't clear the ISR flag
2650                  * otherwise - resulting in an infinite loop.
2651                  */
2652                 if (isr & ED_ISR_CNT) {
2653                         (void) ed_nic_inb(sc, ED_P0_CNTR0);
2654                         (void) ed_nic_inb(sc, ED_P0_CNTR1);
2655                         (void) ed_nic_inb(sc, ED_P0_CNTR2);
2656                 }
2657         }
2658 }
2659
2660 /*
2661  * Process an ioctl request. This code needs some work - it looks
2662  *      pretty ugly.
2663  */
2664 static int
2665 ed_ioctl(ifp, command, data, cr)
2666         struct ifnet *ifp;
2667         u_long     command;
2668         caddr_t data;
2669         struct ucred *cr;
2670 {
2671         struct ed_softc *sc = ifp->if_softc;
2672 #ifndef ED_NO_MIIBUS
2673         struct ifreq *ifr = (struct ifreq *)data;
2674         struct mii_data *mii;
2675 #endif
2676         int error = 0;
2677
2678         crit_enter();
2679
2680         if (sc == NULL || sc->gone) {
2681                 ifp->if_flags &= ~IFF_RUNNING;
2682                 crit_exit();
2683                 return ENXIO;
2684         }
2685
2686         switch (command) {
2687         case SIOCSIFFLAGS:
2688
2689                 /*
2690                  * If the interface is marked up and stopped, then start it.
2691                  * If it is marked down and running, then stop it.
2692                  */
2693                 if (ifp->if_flags & IFF_UP) {
2694                         if ((ifp->if_flags & IFF_RUNNING) == 0)
2695                                 ed_init(sc);
2696                 } else {
2697                         if (ifp->if_flags & IFF_RUNNING) {
2698                                 ed_stop(sc);
2699                                 ifp->if_flags &= ~IFF_RUNNING;
2700                         }
2701                 }
2702
2703                 /*
2704                  * Promiscuous flag may have changed, so reprogram the RCR.
2705                  */
2706                 ed_setrcr(sc);
2707
2708                 /*
2709                  * An unfortunate hack to provide the (required) software
2710                  * control of the tranceiver for 3Com boards. The ALTPHYS flag
2711                  * disables the tranceiver if set.
2712                  */
2713                 if (sc->vendor == ED_VENDOR_3COM) {
2714                         if (ifp->if_flags & IFF_ALTPHYS) {
2715                                 ed_asic_outb(sc, ED_3COM_CR, 0);
2716                         } else {
2717                                 ed_asic_outb(sc, ED_3COM_CR, ED_3COM_CR_XSEL);
2718                         }
2719                 } else if (sc->vendor == ED_VENDOR_HP) 
2720                         ed_hpp_set_physical_link(sc);
2721                 break;
2722
2723         case SIOCADDMULTI:
2724         case SIOCDELMULTI:
2725                 /*
2726                  * Multicast list has changed; set the hardware filter
2727                  * accordingly.
2728                  */
2729                 ed_setrcr(sc);
2730                 error = 0;
2731                 break;
2732
2733 #ifndef ED_NO_MIIBUS
2734         case SIOCGIFMEDIA:
2735         case SIOCSIFMEDIA:
2736                 if (sc->miibus == NULL) {
2737                         error = EINVAL;
2738                         break;
2739                 }
2740                 mii = device_get_softc(sc->miibus);
2741                 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
2742                 break;
2743 #endif
2744
2745         default:
2746                 error = ether_ioctl(ifp, command, data);
2747                 break;
2748         }
2749
2750         crit_exit();
2751
2752         return (error);
2753 }
2754
2755 /*
2756  * Given a source and destination address, copy 'amount' of a packet from
2757  *      the ring buffer into a linear destination buffer. Takes into account
2758  *      ring-wrap.
2759  */
2760 static __inline char *
2761 ed_ring_copy(sc, src, dst, amount)
2762         struct ed_softc *sc;
2763         char   *src;
2764         char   *dst;
2765         u_short amount;
2766 {
2767         u_short tmp_amount;
2768
2769         /* does copy wrap to lower addr in ring buffer? */
2770         if (src + amount > sc->mem_end) {
2771                 tmp_amount = sc->mem_end - src;
2772
2773                 /* copy amount up to end of NIC memory */
2774                 if (sc->mem_shared)
2775                         bcopy(src, dst, tmp_amount);
2776                 else
2777                         ed_pio_readmem(sc, (int)src, dst, tmp_amount);
2778
2779                 amount -= tmp_amount;
2780                 src = sc->mem_ring;
2781                 dst += tmp_amount;
2782         }
2783         if (sc->mem_shared)
2784                 bcopy(src, dst, amount);
2785         else
2786                 ed_pio_readmem(sc, (int)src, dst, amount);
2787
2788         return (src + amount);
2789 }
2790
2791 /*
2792  * Retreive packet from shared memory and send to the next level up via
2793  * ether_input().
2794  */
2795 static void
2796 ed_get_packet(sc, buf, len)
2797         struct ed_softc *sc;
2798         char   *buf;
2799         u_short len;
2800 {
2801         struct ifnet *ifp = &sc->arpcom.ac_if;
2802         struct ether_header *eh;
2803         struct mbuf *m;
2804
2805         /*
2806          * Allocate a header mbuf.
2807          * We always put the received packet in a single buffer -
2808          * either with just an mbuf header or in a cluster attached
2809          * to the header. The +2 is to compensate for the alignment
2810          * fixup below.
2811          */
2812         m = m_getl(len + 2, MB_DONTWAIT, MT_DATA, M_PKTHDR, NULL);
2813         if (m == NULL)
2814                 return;
2815         m->m_pkthdr.rcvif = ifp;
2816         m->m_pkthdr.len = m->m_len = len;
2817
2818         /*
2819          * The +2 is to longword align the start of the real packet.
2820          * This is important for NFS.
2821          */
2822         m->m_data += 2;
2823         eh = mtod(m, struct ether_header *);
2824
2825         /*
2826          * Don't read in the entire packet if we know we're going to drop it
2827          * and no bpf is active.
2828          */
2829         if (!ifp->if_bpf && BDG_ACTIVE( (ifp) ) ) {
2830                 struct ifnet *bif;
2831
2832                 ed_ring_copy(sc, buf, (char *)eh, ETHER_HDR_LEN);
2833                 bif = bridge_in_ptr(ifp, eh) ;
2834                 if (bif == BDG_DROP) {
2835                         m_freem(m);
2836                         return;
2837                 }
2838                 if (len > ETHER_HDR_LEN)
2839                         ed_ring_copy(sc, buf + ETHER_HDR_LEN,
2840                                 (char *)(eh + 1), len - ETHER_HDR_LEN);
2841         } else
2842                 /*
2843                  * Get packet, including link layer address, from interface.
2844                  */
2845                 ed_ring_copy(sc, buf, (char *)eh, len);
2846
2847         m->m_pkthdr.len = m->m_len = len;
2848
2849         (*ifp->if_input)(ifp, m);
2850 }
2851
2852 /*
2853  * Supporting routines
2854  */
2855
2856 /*
2857  * Given a NIC memory source address and a host memory destination
2858  *      address, copy 'amount' from NIC to host using Programmed I/O.
2859  *      The 'amount' is rounded up to a word - okay as long as mbufs
2860  *              are word sized.
2861  *      This routine is currently Novell-specific.
2862  */
2863 void
2864 ed_pio_readmem(sc, src, dst, amount)
2865         struct ed_softc *sc;
2866         int src;
2867         unsigned char *dst;
2868         unsigned short amount;
2869 {
2870         /* HP PC Lan+ cards need special handling */
2871         if (sc->vendor == ED_VENDOR_HP && sc->type == ED_TYPE_HP_PCLANPLUS) {
2872                 ed_hpp_readmem(sc, src, dst, amount);
2873                 return;
2874         }
2875
2876         /* Regular Novell cards */
2877         /* select page 0 registers */
2878         ed_nic_outb(sc, ED_P0_CR, ED_CR_RD2 | ED_CR_STA);
2879
2880         /* round up to a word */
2881         if (amount & 1)
2882                 ++amount;
2883
2884         /* set up DMA byte count */
2885         ed_nic_outb(sc, ED_P0_RBCR0, amount);
2886         ed_nic_outb(sc, ED_P0_RBCR1, amount >> 8);
2887
2888         /* set up source address in NIC mem */
2889         ed_nic_outb(sc, ED_P0_RSAR0, src);
2890         ed_nic_outb(sc, ED_P0_RSAR1, src >> 8);
2891
2892         ed_nic_outb(sc, ED_P0_CR, ED_CR_RD0 | ED_CR_STA);
2893
2894         if (sc->isa16bit) {
2895                 ed_asic_insw(sc, ED_NOVELL_DATA, dst, amount / 2);
2896         } else {
2897                 ed_asic_insb(sc, ED_NOVELL_DATA, dst, amount);
2898         }
2899 }
2900
2901 /*
2902  * Stripped down routine for writing a linear buffer to NIC memory.
2903  *      Only used in the probe routine to test the memory. 'len' must
2904  *      be even.
2905  */
2906 void
2907 ed_pio_writemem(sc, src, dst, len)
2908         struct ed_softc *sc;
2909         char   *src;
2910         unsigned short dst;
2911         unsigned short len;
2912 {
2913         int     maxwait = 200;  /* about 240us */
2914
2915         /* select page 0 registers */
2916         ed_nic_outb(sc, ED_P0_CR, ED_CR_RD2 | ED_CR_STA);
2917
2918         /* reset remote DMA complete flag */
2919         ed_nic_outb(sc, ED_P0_ISR, ED_ISR_RDC);
2920
2921         /* set up DMA byte count */
2922         ed_nic_outb(sc, ED_P0_RBCR0, len);
2923         ed_nic_outb(sc, ED_P0_RBCR1, len >> 8);
2924
2925         /* set up destination address in NIC mem */
2926         ed_nic_outb(sc, ED_P0_RSAR0, dst);
2927         ed_nic_outb(sc, ED_P0_RSAR1, dst >> 8);
2928
2929         /* set remote DMA write */
2930         ed_nic_outb(sc, ED_P0_CR, ED_CR_RD1 | ED_CR_STA);
2931
2932         if (sc->isa16bit) {
2933                 ed_asic_outsw(sc, ED_NOVELL_DATA, src, len / 2);
2934         } else {
2935                 ed_asic_outsb(sc, ED_NOVELL_DATA, src, len);
2936         }
2937
2938         /*
2939          * Wait for remote DMA complete. This is necessary because on the
2940          * transmit side, data is handled internally by the NIC in bursts and
2941          * we can't start another remote DMA until this one completes. Not
2942          * waiting causes really bad things to happen - like the NIC
2943          * irrecoverably jamming the ISA bus.
2944          */
2945         while (((ed_nic_inb(sc, ED_P0_ISR) & ED_ISR_RDC) != ED_ISR_RDC) && --maxwait);
2946 }
2947
2948 /*
2949  * Write an mbuf chain to the destination NIC memory address using
2950  *      programmed I/O.
2951  */
2952 static u_short
2953 ed_pio_write_mbufs(sc, m, dst)
2954         struct ed_softc *sc;
2955         struct mbuf *m;
2956         int dst;
2957 {
2958         struct ifnet *ifp = (struct ifnet *)sc;
2959         unsigned short total_len, dma_len;
2960         struct mbuf *mp;
2961         int     maxwait = 200;  /* about 240us */
2962
2963         /* HP PC Lan+ cards need special handling */
2964         if (sc->vendor == ED_VENDOR_HP && sc->type == ED_TYPE_HP_PCLANPLUS) {
2965                 return ed_hpp_write_mbufs(sc, m, dst);
2966         }
2967
2968         /* Regular Novell cards */
2969         /* First, count up the total number of bytes to copy */
2970         for (total_len = 0, mp = m; mp; mp = mp->m_next)
2971                 total_len += mp->m_len;
2972
2973         dma_len = total_len;
2974         if (sc->isa16bit && (dma_len & 1))
2975                 dma_len++;
2976
2977         /* select page 0 registers */
2978         ed_nic_outb(sc, ED_P0_CR, ED_CR_RD2 | ED_CR_STA);
2979
2980         /* reset remote DMA complete flag */
2981         ed_nic_outb(sc, ED_P0_ISR, ED_ISR_RDC);
2982
2983         /* set up DMA byte count */
2984         ed_nic_outb(sc, ED_P0_RBCR0, dma_len);
2985         ed_nic_outb(sc, ED_P0_RBCR1, dma_len >> 8);
2986
2987         /* set up destination address in NIC mem */
2988         ed_nic_outb(sc, ED_P0_RSAR0, dst);
2989         ed_nic_outb(sc, ED_P0_RSAR1, dst >> 8);
2990
2991         /* set remote DMA write */
2992         ed_nic_outb(sc, ED_P0_CR, ED_CR_RD1 | ED_CR_STA);
2993
2994   /*
2995    * Transfer the mbuf chain to the NIC memory.
2996    * 16-bit cards require that data be transferred as words, and only words.
2997    * So that case requires some extra code to patch over odd-length mbufs.
2998    */
2999
3000         if (!sc->isa16bit) {
3001                 /* NE1000s are easy */
3002                 while (m) {
3003                         if (m->m_len) {
3004                                 ed_asic_outsb(sc, ED_NOVELL_DATA,
3005                                               m->m_data, m->m_len);
3006                         }
3007                         m = m->m_next;
3008                 }
3009         } else {
3010                 /* NE2000s are a pain */
3011                 unsigned char *data;
3012                 int len, wantbyte;
3013                 unsigned char savebyte[2];
3014
3015                 wantbyte = 0;
3016
3017                 while (m) {
3018                         len = m->m_len;
3019                         if (len) {
3020                                 data = mtod(m, caddr_t);
3021                                 /* finish the last word */
3022                                 if (wantbyte) {
3023                                         savebyte[1] = *data;
3024                                         ed_asic_outw(sc, ED_NOVELL_DATA,
3025                                                      *(u_short *)savebyte);
3026                                         data++;
3027                                         len--;
3028                                         wantbyte = 0;
3029                                 }
3030                                 /* output contiguous words */
3031                                 if (len > 1) {
3032                                         ed_asic_outsw(sc, ED_NOVELL_DATA,
3033                                                       data, len >> 1);
3034                                         data += len & ~1;
3035                                         len &= 1;
3036                                 }
3037                                 /* save last byte, if necessary */
3038                                 if (len == 1) {
3039                                         savebyte[0] = *data;
3040                                         wantbyte = 1;
3041                                 }
3042                         }
3043                         m = m->m_next;
3044                 }
3045                 /* spit last byte */
3046                 if (wantbyte) {
3047                         ed_asic_outw(sc, ED_NOVELL_DATA, *(u_short *)savebyte);
3048                 }
3049         }
3050
3051         /*
3052          * Wait for remote DMA complete. This is necessary because on the
3053          * transmit side, data is handled internally by the NIC in bursts and
3054          * we can't start another remote DMA until this one completes. Not
3055          * waiting causes really bad things to happen - like the NIC
3056          * irrecoverably jamming the ISA bus.
3057          */
3058         while (((ed_nic_inb(sc, ED_P0_ISR) & ED_ISR_RDC) != ED_ISR_RDC) && --maxwait);
3059
3060         if (!maxwait) {
3061                 log(LOG_WARNING, "%s: remote transmit DMA failed to complete\n",
3062                     ifp->if_xname);
3063                 ed_reset(ifp);
3064                 return(0);
3065         }
3066         return (total_len);
3067 }
3068
3069 /*
3070  * Support routines to handle the HP PC Lan+ card.
3071  */
3072
3073 /*
3074  * HP PC Lan+: Read from NIC memory, using either PIO or memory mapped
3075  * IO.
3076  */
3077
3078 static void
3079 ed_hpp_readmem(sc, src, dst, amount)
3080         struct ed_softc *sc; 
3081         unsigned short src;
3082         unsigned char *dst;
3083         unsigned short amount;
3084 {
3085
3086         int use_32bit_access = !(sc->hpp_id & ED_HPP_ID_16_BIT_ACCESS);
3087
3088
3089         /* Program the source address in RAM */
3090         ed_asic_outw(sc, ED_HPP_PAGE_2, src);
3091
3092         /*
3093          * The HP PC Lan+ card supports word reads as well as
3094          * a memory mapped i/o port that is aliased to every 
3095          * even address on the board.
3096          */
3097
3098         if (sc->hpp_mem_start) {
3099
3100                 /* Enable memory mapped access.  */
3101                 ed_asic_outw(sc, ED_HPP_OPTION, sc->hpp_options & 
3102                         ~(ED_HPP_OPTION_MEM_DISABLE | 
3103                           ED_HPP_OPTION_BOOT_ROM_ENB));
3104
3105                 if (use_32bit_access && (amount > 3)) {
3106                         u_int32_t *dl = (u_int32_t *) dst;      
3107                         volatile u_int32_t *const sl = 
3108                                 (u_int32_t *) sc->hpp_mem_start;
3109                         u_int32_t *const fence = dl + (amount >> 2);
3110                         
3111                         /* Copy out NIC data.  We could probably write this
3112                            as a `movsl'. The currently generated code is lousy.
3113                            */
3114
3115                         while (dl < fence)
3116                                 *dl++ = *sl;
3117                 
3118                         dst += (amount & ~3);
3119                         amount &= 3;
3120
3121                 } 
3122
3123                 /* Finish off any words left, as a series of short reads */
3124                 if (amount > 1) {
3125                         u_short *d = (u_short *) dst;   
3126                         volatile u_short *const s = 
3127                                 (u_short *) sc->hpp_mem_start;
3128                         u_short *const fence = d + (amount >> 1);
3129                         
3130                         /* Copy out NIC data.  */
3131
3132                         while (d < fence)
3133                                 *d++ = *s;
3134         
3135                         dst += (amount & ~1);
3136                         amount &= 1;
3137                 }
3138
3139                 /*
3140                  * read in a byte; however we need to always read 16 bits
3141                  * at a time or the hardware gets into a funny state
3142                  */
3143
3144                 if (amount == 1) {
3145                         /* need to read in a short and copy LSB */
3146                         volatile u_short *const s = 
3147                                 (volatile u_short *) sc->hpp_mem_start;
3148                         
3149                         *dst = (*s) & 0xFF;     
3150                 }
3151
3152                 /* Restore Boot ROM access.  */
3153
3154                 ed_asic_outw(sc, ED_HPP_OPTION, sc->hpp_options);
3155
3156
3157         } else { 
3158                 /* Read in data using the I/O port */
3159                 if (use_32bit_access && (amount > 3)) {
3160                         ed_asic_insl(sc, ED_HPP_PAGE_4, dst, amount >> 2);
3161                         dst += (amount & ~3);
3162                         amount &= 3;
3163                 }
3164                 if (amount > 1) {
3165                         ed_asic_insw(sc, ED_HPP_PAGE_4, dst, amount >> 1);
3166                         dst += (amount & ~1);
3167                         amount &= 1;
3168                 }
3169                 if (amount == 1) { /* read in a short and keep the LSB */
3170                         *dst = ed_asic_inw(sc, ED_HPP_PAGE_4) & 0xFF;
3171                 }
3172         }
3173 }
3174
3175 /*
3176  * HP PC Lan+: Write to NIC memory, using either PIO or memory mapped
3177  * IO.
3178  *      Only used in the probe routine to test the memory. 'len' must
3179  *      be even.
3180  */
3181 static void
3182 ed_hpp_writemem(sc, src, dst, len)
3183         struct ed_softc *sc;
3184         unsigned char *src;
3185         unsigned short dst;
3186         unsigned short len;
3187 {
3188         /* reset remote DMA complete flag */
3189         ed_nic_outb(sc, ED_P0_ISR, ED_ISR_RDC);
3190
3191         /* program the write address in RAM */
3192         ed_asic_outw(sc, ED_HPP_PAGE_0, dst);
3193
3194         if (sc->hpp_mem_start) {
3195                 u_short *s = (u_short *) src;
3196                 volatile u_short *d = (u_short *) sc->hpp_mem_start;
3197                 u_short *const fence = s + (len >> 1);
3198
3199                 /*
3200                  * Enable memory mapped access.
3201                  */
3202
3203                 ed_asic_outw(sc, ED_HPP_OPTION, sc->hpp_options & 
3204                         ~(ED_HPP_OPTION_MEM_DISABLE | 
3205                           ED_HPP_OPTION_BOOT_ROM_ENB));
3206
3207                 /*
3208                  * Copy to NIC memory.
3209                  */
3210
3211                 while (s < fence)
3212                         *d = *s++;
3213
3214                 /*
3215                  * Restore Boot ROM access.
3216                  */
3217
3218                 ed_asic_outw(sc, ED_HPP_OPTION, sc->hpp_options);
3219
3220         } else {
3221                 /* write data using I/O writes */
3222                 ed_asic_outsw(sc, ED_HPP_PAGE_4, src, len / 2);
3223         }
3224 }
3225
3226 /*
3227  * Write to HP PC Lan+ NIC memory.  Access to the NIC can be by using 
3228  * outsw() or via the memory mapped interface to the same register.
3229  * Writes have to be in word units; byte accesses won't work and may cause
3230  * the NIC to behave weirdly. Long word accesses are permitted if the ASIC
3231  * allows it.
3232  */
3233
3234 static u_short
3235 ed_hpp_write_mbufs(struct ed_softc *sc, struct mbuf *m, int dst)
3236 {
3237         int len, wantbyte;
3238         unsigned short total_len;
3239         unsigned char savebyte[2];
3240         volatile u_short * const d = 
3241                 (volatile u_short *) sc->hpp_mem_start;
3242         int use_32bit_accesses = !(sc->hpp_id & ED_HPP_ID_16_BIT_ACCESS);
3243
3244         /* select page 0 registers */
3245         ed_nic_outb(sc, ED_P0_CR, sc->cr_proto | ED_CR_STA);
3246
3247         /* reset remote DMA complete flag */
3248         ed_nic_outb(sc, ED_P0_ISR, ED_ISR_RDC);
3249
3250         /* program the write address in RAM */
3251         ed_asic_outw(sc, ED_HPP_PAGE_0, dst);
3252
3253         if (sc->hpp_mem_start)  /* enable memory mapped I/O */
3254                 ed_asic_outw(sc, ED_HPP_OPTION, sc->hpp_options & 
3255                         ~(ED_HPP_OPTION_MEM_DISABLE |
3256                         ED_HPP_OPTION_BOOT_ROM_ENB));
3257
3258         wantbyte = 0;
3259         total_len = 0;
3260
3261         if (sc->hpp_mem_start) {        /* Memory mapped I/O port */
3262                 while (m) {
3263                         total_len += (len = m->m_len);
3264                         if (len) {
3265                                 caddr_t data = mtod(m, caddr_t);
3266                                 /* finish the last word of the previous mbuf */
3267                                 if (wantbyte) {
3268                                         savebyte[1] = *data;
3269                                         *d = *((u_short *) savebyte);
3270                                         data++; len--; wantbyte = 0;
3271                                 }
3272                                 /* output contiguous words */
3273                                 if ((len > 3) && (use_32bit_accesses)) {
3274                                         volatile u_int32_t *const dl = 
3275                                                 (volatile u_int32_t *) d;
3276                                         u_int32_t *sl = (u_int32_t *) data;
3277                                         u_int32_t *fence = sl + (len >> 2);
3278
3279                                         while (sl < fence)
3280                                                 *dl = *sl++;
3281
3282                                         data += (len & ~3);
3283                                         len &= 3;
3284                                 }
3285                                 /* finish off remain 16 bit writes */
3286                                 if (len > 1) {
3287                                         u_short *s = (u_short *) data;
3288                                         u_short *fence = s + (len >> 1);
3289
3290                                         while (s < fence)
3291                                                 *d = *s++;
3292
3293                                         data += (len & ~1); 
3294                                         len &= 1;
3295                                 }
3296                                 /* save last byte if needed */
3297                                 if ((wantbyte = (len == 1)) != 0)
3298                                         savebyte[0] = *data;
3299                         }
3300                         m = m->m_next;  /* to next mbuf */
3301                 }
3302                 if (wantbyte) /* write last byte */
3303                         *d = *((u_short *) savebyte);
3304         } else {
3305                 /* use programmed I/O */
3306                 while (m) {
3307                         total_len += (len = m->m_len);
3308                         if (len) {
3309                                 caddr_t data = mtod(m, caddr_t);
3310                                 /* finish the last word of the previous mbuf */
3311                                 if (wantbyte) {
3312                                         savebyte[1] = *data;
3313                                         ed_asic_outw(sc, ED_HPP_PAGE_4,
3314                                                      *((u_short *)savebyte));
3315                                         data++; 
3316                                         len--; 
3317                                         wantbyte = 0;
3318                                 }
3319                                 /* output contiguous words */
3320                                 if ((len > 3) && use_32bit_accesses) {
3321                                         ed_asic_outsl(sc, ED_HPP_PAGE_4,
3322                                                       data, len >> 2);
3323                                         data += (len & ~3);
3324                                         len &= 3;
3325                                 }
3326                                 /* finish off remaining 16 bit accesses */
3327                                 if (len > 1) {
3328                                         ed_asic_outsw(sc, ED_HPP_PAGE_4,
3329                                                       data, len >> 1);
3330                                         data += (len & ~1);
3331                                         len &= 1;
3332                                 }
3333                                 if ((wantbyte = (len == 1)) != 0)
3334                                         savebyte[0] = *data;
3335
3336                         } /* if len != 0 */
3337                         m = m->m_next;
3338                 }
3339                 if (wantbyte) /* spit last byte */
3340                         ed_asic_outw(sc, ED_HPP_PAGE_4, *(u_short *)savebyte);
3341
3342         }
3343
3344         if (sc->hpp_mem_start)  /* turn off memory mapped i/o */
3345                 ed_asic_outw(sc, ED_HPP_OPTION, sc->hpp_options);
3346
3347         return (total_len);
3348 }
3349
3350 #ifndef ED_NO_MIIBUS
3351 /*
3352  * MII bus support routines.
3353  */
3354 int
3355 ed_miibus_readreg(dev, phy, reg)
3356         device_t dev;
3357         int phy, reg;
3358 {
3359         struct ed_softc *sc = device_get_softc(dev);
3360         int failed, val;
3361
3362         crit_enter();
3363
3364         if (sc->gone) {
3365                 crit_exit();
3366                 return (0);
3367         }
3368
3369         (*sc->mii_writebits)(sc, 0xffffffff, 32);
3370         (*sc->mii_writebits)(sc, ED_MII_STARTDELIM, ED_MII_STARTDELIM_BITS);
3371         (*sc->mii_writebits)(sc, ED_MII_READOP, ED_MII_OP_BITS);
3372         (*sc->mii_writebits)(sc, phy, ED_MII_PHY_BITS);
3373         (*sc->mii_writebits)(sc, reg, ED_MII_REG_BITS);
3374
3375         failed = (*sc->mii_readbits)(sc, ED_MII_ACK_BITS);
3376         val = (*sc->mii_readbits)(sc, ED_MII_DATA_BITS);
3377         (*sc->mii_writebits)(sc, ED_MII_IDLE, ED_MII_IDLE_BITS);
3378
3379         crit_exit();
3380
3381         return (failed ? 0 : val);
3382 }
3383
3384 void
3385 ed_miibus_writereg(dev, phy, reg, data)
3386         device_t dev;
3387         int phy, reg, data;
3388 {
3389         struct ed_softc *sc = device_get_softc(dev);
3390
3391         crit_enter();
3392
3393         if (sc->gone) {
3394                 crit_exit();
3395                 return;
3396         }
3397
3398         (*sc->mii_writebits)(sc, 0xffffffff, 32);
3399         (*sc->mii_writebits)(sc, ED_MII_STARTDELIM, ED_MII_STARTDELIM_BITS);
3400         (*sc->mii_writebits)(sc, ED_MII_WRITEOP, ED_MII_OP_BITS);
3401         (*sc->mii_writebits)(sc, phy, ED_MII_PHY_BITS);
3402         (*sc->mii_writebits)(sc, reg, ED_MII_REG_BITS);
3403         (*sc->mii_writebits)(sc, ED_MII_TURNAROUND, ED_MII_TURNAROUND_BITS);
3404         (*sc->mii_writebits)(sc, data, ED_MII_DATA_BITS);
3405         (*sc->mii_writebits)(sc, ED_MII_IDLE, ED_MII_IDLE_BITS);
3406
3407         crit_exit();
3408 }
3409
3410 int
3411 ed_ifmedia_upd(ifp)
3412         struct ifnet *ifp;
3413 {
3414         struct ed_softc *sc;
3415         struct mii_data *mii;
3416
3417         sc = ifp->if_softc;
3418         if (sc->gone || sc->miibus == NULL)
3419                 return (ENXIO);
3420         
3421         mii = device_get_softc(sc->miibus);
3422         return mii_mediachg(mii);
3423 }
3424
3425 void
3426 ed_ifmedia_sts(ifp, ifmr)
3427         struct ifnet *ifp;
3428         struct ifmediareq *ifmr;
3429 {
3430         struct ed_softc *sc;
3431         struct mii_data *mii;
3432
3433         sc = ifp->if_softc;
3434         if (sc->gone || sc->miibus == NULL)
3435                 return;
3436
3437         mii = device_get_softc(sc->miibus);
3438         mii_pollstat(mii);
3439         ifmr->ifm_active = mii->mii_media_active;
3440         ifmr->ifm_status = mii->mii_media_status;
3441 }
3442
3443 void
3444 ed_child_detached(dev, child)
3445         device_t dev;
3446         device_t child;
3447 {
3448         struct ed_softc *sc;
3449
3450         sc = device_get_softc(dev);
3451         if (child == sc->miibus)
3452                 sc->miibus = NULL;
3453 }
3454 #endif
3455
3456 static void
3457 ed_setrcr(sc)
3458         struct ed_softc *sc;
3459 {
3460         struct ifnet *ifp = (struct ifnet *)sc;
3461         int     i;
3462         u_char  reg1;
3463
3464         /* Bit 6 in AX88190 RCR register must be set. */
3465         if (sc->chip_type == ED_CHIP_TYPE_AX88190)
3466                 reg1 = ED_RCR_INTT;
3467         else
3468                 reg1 = 0x00;
3469
3470         /* set page 1 registers */
3471         ed_nic_outb(sc, ED_P0_CR, sc->cr_proto | ED_CR_PAGE_1 | ED_CR_STP);
3472
3473         if (ifp->if_flags & IFF_PROMISC) {
3474
3475                 /*
3476                  * Reconfigure the multicast filter.
3477                  */
3478                 for (i = 0; i < 8; i++)
3479                         ed_nic_outb(sc, ED_P1_MAR(i), 0xff);
3480
3481                 /*
3482                  * And turn on promiscuous mode. Also enable reception of
3483                  * runts and packets with CRC & alignment errors.
3484                  */
3485                 /* Set page 0 registers */
3486                 ed_nic_outb(sc, ED_P0_CR, sc->cr_proto | ED_CR_STP);
3487
3488                 ed_nic_outb(sc, ED_P0_RCR, ED_RCR_PRO | ED_RCR_AM |
3489                             ED_RCR_AB | ED_RCR_AR | ED_RCR_SEP | reg1);
3490         } else {
3491                 /* set up multicast addresses and filter modes */
3492                 if (ifp->if_flags & IFF_MULTICAST) {
3493                         u_int32_t  mcaf[2];
3494
3495                         if (ifp->if_flags & IFF_ALLMULTI) {
3496                                 mcaf[0] = 0xffffffff;
3497                                 mcaf[1] = 0xffffffff;
3498                         } else
3499                                 ds_getmcaf(sc, mcaf);
3500
3501                         /*
3502                          * Set multicast filter on chip.
3503                          */
3504                         for (i = 0; i < 8; i++)
3505                                 ed_nic_outb(sc, ED_P1_MAR(i), ((u_char *) mcaf)[i]);
3506
3507                         /* Set page 0 registers */
3508                         ed_nic_outb(sc, ED_P0_CR, sc->cr_proto | ED_CR_STP);
3509
3510                         ed_nic_outb(sc, ED_P0_RCR, ED_RCR_AM | ED_RCR_AB | reg1);
3511                 } else {
3512
3513                         /*
3514                          * Initialize multicast address hashing registers to
3515                          * not accept multicasts.
3516                          */
3517                         for (i = 0; i < 8; ++i)
3518                                 ed_nic_outb(sc, ED_P1_MAR(i), 0x00);
3519
3520                         /* Set page 0 registers */
3521                         ed_nic_outb(sc, ED_P0_CR, sc->cr_proto | ED_CR_STP);
3522
3523                         ed_nic_outb(sc, ED_P0_RCR, ED_RCR_AB | reg1);
3524                 }
3525         }
3526
3527         /*
3528          * Start interface.
3529          */
3530         ed_nic_outb(sc, ED_P0_CR, sc->cr_proto | ED_CR_STA);
3531 }
3532
3533 /*
3534  * Compute crc for ethernet address
3535  */
3536 static uint32_t
3537 ds_mchash(addr)
3538         const uint8_t *addr;
3539 {
3540 #define ED_POLYNOMIAL 0x04c11db6
3541         uint32_t crc = 0xffffffff;
3542         int carry, idx, bit;
3543         uint8_t data;
3544
3545         for (idx = 6; --idx >= 0;) {
3546                 for (data = *addr++, bit = 8; --bit >= 0; data >>=1 ) {
3547                         carry = ((crc & 0x80000000) ? 1 : 0) ^ (data & 0x01);
3548                         crc <<= 1;
3549                         if (carry)
3550                                 crc = (crc ^ ED_POLYNOMIAL) | carry;
3551                 }
3552         }
3553         return crc;
3554 #undef POLYNOMIAL
3555 }
3556
3557 /*
3558  * Compute the multicast address filter from the
3559  * list of multicast addresses we need to listen to.
3560  */
3561 static void
3562 ds_getmcaf(sc, mcaf)
3563         struct ed_softc *sc;
3564         u_int32_t *mcaf;
3565 {
3566         u_int32_t index;
3567         u_char *af = (u_char *) mcaf;
3568         struct ifmultiaddr *ifma;
3569
3570         mcaf[0] = 0;
3571         mcaf[1] = 0;
3572
3573         for (ifma = sc->arpcom.ac_if.if_multiaddrs.lh_first; ifma;
3574              ifma = ifma->ifma_link.le_next) {
3575                 if (ifma->ifma_addr->sa_family != AF_LINK)
3576                         continue;
3577                 index = ds_mchash(LLADDR((struct sockaddr_dl *)ifma->ifma_addr))
3578                         >> 26;
3579                 af[index >> 3] |= 1 << (index & 7);
3580         }
3581 }