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