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