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