1 /*	$OpenBSD: if_ed.c,v 1.52 2004/05/12 06:35:10 tedu Exp $	*/
2 /*	$NetBSD: if_ed.c,v 1.105 1996/10/21 22:40:45 thorpej Exp $	*/
3 
4 /*
5  * Device driver for National Semiconductor DS8390/WD83C690 based ethernet
6  * adapters.
7  *
8  * Copyright (c) 1994, 1995 Charles M. Hannum.  All rights reserved.
9  *
10  * Copyright (C) 1993, David Greenman.  This software may be used, modified,
11  * copied, distributed, and sold, in both source and binary form provided that
12  * the above copyright and these terms are retained.  Under no circumstances is
13  * the author responsible for the proper functioning of this software, nor does
14  * the author assume any responsibility for damages incurred with its use.
15  *
16  * Currently supports the Western Digital/SMC 8003 and 8013 series, the SMC
17  * Elite Ultra (8216), the 3Com 3c503, the NE1000 and NE2000, and a variety of
18  * similar clones.
19  */
20 
21 #include "bpfilter.h"
22 #include "ed.h"
23 
24 #include <sys/param.h>
25 #include <sys/systm.h>
26 #include <sys/errno.h>
27 #include <sys/ioctl.h>
28 #include <sys/mbuf.h>
29 #include <sys/socket.h>
30 #include <sys/syslog.h>
31 #include <sys/device.h>
32 
33 #include <net/if.h>
34 #include <net/if_dl.h>
35 #include <net/if_types.h>
36 #include <net/netisr.h>
37 
38 #ifdef INET
39 #include <netinet/in.h>
40 #include <netinet/in_systm.h>
41 #include <netinet/in_var.h>
42 #include <netinet/ip.h>
43 #include <netinet/if_ether.h>
44 #endif
45 
46 #if NBPFILTER > 0
47 #include <net/bpf.h>
48 #endif
49 
50 #include <machine/cpu.h>
51 #include <machine/bus.h>
52 #include <machine/intr.h>
53 
54 #include <dev/isa/isareg.h>
55 #include <dev/isa/isavar.h>
56 #include <dev/ic/dp8390reg.h>
57 #include <dev/isa/if_edreg.h>
58 
59 /*
60  * ed_softc: per line info and status
61  */
62 struct ed_softc {
63 	struct	device sc_dev;
64 	void	*sc_ih;
65 
66 	struct	arpcom sc_arpcom;	/* ethernet common */
67 	void	*sc_sh;			/* shutdown hook */
68 
69 	char	*type_str;	/* pointer to type string */
70 	u_char	vendor;		/* interface vendor */
71 	u_char	type;		/* interface type code */
72 	u_int16_t	spec_flags;
73 #define ED_REATTACH	0x0001	/* Reattach */
74 #define ED_NOTPRESENT 	0x0002	/* card not present; do not allow
75 				   reconfiguration */
76 
77 	bus_space_tag_t sc_iot; /* bus identifier */
78 	bus_space_tag_t sc_memt;
79 	bus_space_handle_t sc_ioh;   /* io handle */
80 	bus_space_handle_t sc_delaybah; /* io handle for `delay port' */
81 	bus_space_handle_t sc_memh; /* bus memory handle */
82 	isa_chipset_tag_t sc_ic;
83 
84 	bus_size_t	asic_base;	/* offset of ASIC I/O port */
85 	bus_size_t	nic_base;	/* offset of NIC (DS8390) I/O port */
86 
87 /*
88  * The following 'proto' variable is part of a work-around for 8013EBT asics
89  * being write-only.  It's sort of a prototype/shadow of the real thing.
90  */
91 	u_char	wd_laar_proto;
92 /*
93  * This `proto' variable is so we can turn MENB on and off without reading
94  * the value back from the card all the time.
95  */
96 	u_char	wd_msr_proto;
97 	u_char	cr_proto;	/* values always set in CR */
98 	u_char	isa16bit;	/* width of access to card 0=8 or 1=16 */
99 	u_char	is790;		/* set by probe if NIC is a 790 */
100 
101 	int	mem_start;	/* offset of NIC memory */
102 	int	mem_end;	/* offset of NIC memory end */
103 	int	mem_size;	/* total NIC memory size */
104 	int	mem_ring;	/* offset of RX ring-buffer (in NIC mem) */
105 
106 	u_char	mem_shared;	/* NIC memory is shared with host */
107 	u_char	txb_cnt;	/* number of transmit buffers */
108 	u_char	txb_inuse;	/* number of transmit buffers active */
109 
110 	u_char 	txb_new;	/* pointer to where new buffer will be added */
111 	u_char	txb_next_tx;	/* pointer to next buffer ready to xmit */
112 	u_int16_t	txb_len[8];	/* buffered xmit buffer lengths */
113 	u_char	tx_page_start;	/* first page of TX buffer area */
114 	u_char	rec_page_start;	/* first page of RX ring-buffer */
115 	u_char	rec_page_stop;	/* last page of RX ring-buffer */
116 	u_char	next_packet;	/* pointer to next unread RX packet */
117 };
118 
119 int edprobe(struct device *, void *, void *);
120 void edattach(struct device *, struct device *, void *);
121 int ed_find(struct ed_softc *, struct cfdata *,
122     struct isa_attach_args *ia);
123 int ed_probe_generic8390(bus_space_tag_t, bus_space_handle_t, int);
124 int ed_find_WD80x3(struct ed_softc *, struct cfdata *,
125     struct isa_attach_args *ia);
126 int ed_find_3Com(struct ed_softc *, struct cfdata *,
127     struct isa_attach_args *ia);
128 int ed_find_Novell(struct ed_softc *, struct cfdata *,
129     struct isa_attach_args *ia);
130 int edintr(void *);
131 int edioctl(struct ifnet *, u_long, caddr_t);
132 void edstart(struct ifnet *);
133 void edwatchdog(struct ifnet *);
134 void edreset(struct ed_softc *);
135 void edinit(struct ed_softc *);
136 void edstop(struct ed_softc *);
137 
138 void ed_shared_writemem(struct ed_softc *, caddr_t, int, int);
139 void ed_shared_readmem(struct ed_softc *, int, caddr_t, int);
140 
141 #define inline	/* XXX for debugging porpoises */
142 
143 void ed_getmcaf(struct arpcom *, u_int32_t *);
144 void edread(struct ed_softc *, int, int);
145 struct mbuf *edget(struct ed_softc *, int, int);
146 static __inline void ed_rint(struct ed_softc *);
147 static __inline void ed_xmit(struct ed_softc *);
148 static __inline int ed_ring_copy(struct ed_softc *, int, caddr_t,
149 					u_int16_t);
150 
151 void ed_pio_readmem(struct ed_softc *, u_int16_t, caddr_t, u_int16_t);
152 void ed_pio_writemem(struct ed_softc *, caddr_t, u_int16_t, u_int16_t);
153 u_int16_t ed_pio_write_mbufs(struct ed_softc *, struct mbuf *, u_int16_t);
154 
155 #if NED_ISA > 0
156 struct cfattach ed_isa_ca = {
157 	sizeof(struct ed_softc), edprobe, edattach
158 };
159 #endif
160 
161 struct cfdriver ed_cd = {
162 	NULL, "ed", DV_IFNET
163 };
164 
165 #define	NIC_PUT(t, bah, nic, reg, val)	\
166 	bus_space_write_1((t), (bah), ((nic) + (reg)), (val))
167 #define	NIC_GET(t, bah, nic, reg)	\
168 	bus_space_read_1((t), (bah), ((nic) + (reg)))
169 
170 #if NED_PCMCIA > 0
171 #include <dev/pcmcia/pcmciavar.h>
172 
173 int ed_pcmcia_match(struct device *, void *, void *);
174 void ed_pcmcia_attach(struct device *, struct device *, void *);
175 int ed_pcmcia_detach(struct device *);
176 
177 struct cfattach ed_pcmcia_ca = {
178 	sizeof(struct ed_softc), ed_pcmcia_match, edattach, ed_pcmcia_detach
179 };
180 
181 int ed_pcmcia_isa_attach(struct device *, void *, void *,
182     struct pcmcia_link *);
183 int edmod(struct pcmcia_link *, struct device *, struct pcmcia_conf *,
184     struct cfdata *cf);
185 int ed_remove(struct pcmcia_link *, struct device *);
186 
187 /* additional setup needed for pcmcia devices */
188 int
ed_pcmcia_isa_attach(parent,match,aux,pc_link)189 ed_pcmcia_isa_attach(parent, match, aux, pc_link)
190 	struct device *parent;
191 	void *match;
192 	void *aux;
193 	struct pcmcia_link *pc_link;
194 {
195 	struct ed_softc *sc = match;
196 	struct cfdata *cf = sc->sc_dev.dv_cfdata;
197 	struct isa_attach_args *ia = aux;
198 	struct pcmciadevs *dev=pc_link->device;
199 	int err;
200 	u_char enaddr[ETHER_ADDR_LEN];
201 
202 	if ((int)dev->param != -1)
203 		err = pcmcia_read_cis(pc_link, enaddr,
204 				      (int) dev->param, ETHER_ADDR_LEN);
205 	else
206 		err = 0;
207 	if (err)
208 		printf("%s: attaching ed: cannot read cis info %d\n",
209 		       parent->dv_xname, err);
210 
211 	if (ed_find_Novell(sc, cf, ia)) {
212 		delay(100);
213 		if ((int)dev->param != -1) {
214 		    err = pcmcia_read_cis(pc_link, sc->sc_arpcom.ac_enaddr,
215 				      (int) dev->param, ETHER_ADDR_LEN);
216 		    if (err) {
217 			    printf("Cannot read cis info %d\n", err);
218 			    return 0;
219 		    }
220 		    if (bcmp(enaddr, sc->sc_arpcom.ac_enaddr, ETHER_ADDR_LEN)) {
221 			    printf("ENADDR MISMATCH %s ",
222 				   ether_sprintf(sc->sc_arpcom.ac_enaddr));
223 			    printf("- %s\n", ether_sprintf(enaddr));
224 			    bcopy(enaddr,sc->sc_arpcom.ac_enaddr,
225 					    ETHER_ADDR_LEN);
226 		    }
227 		}
228 		/* clear ED_NOTPRESENT, set ED_REATTACH if needed */
229 		sc->spec_flags=pc_link->flags&PCMCIA_REATTACH?ED_REATTACH:0;
230 		sc->type_str = dev->model;
231 		sc->sc_ic = ia->ia_ic;
232 		return 1;
233 	} else
234 	    return 0;
235 }
236 
237 /* modify config entry */
238 int
edmod(pc_link,self,pc_cf,cf)239 edmod(pc_link, self, pc_cf, cf)
240 	struct pcmcia_link *pc_link;
241 	struct device *self;
242 	struct pcmcia_conf *pc_cf;
243 	struct cfdata *cf;
244 {
245 	int err;
246 /*	struct pcmciadevs *dev=pc_link->device;*/
247 /*	struct ed_softc *sc = (void *)self;*/
248 	int svec_card =  pc_cf->memwin  == 5;
249 	int de650_0 = (pc_cf->memwin != 0) && !svec_card;
250 	err = PCMCIA_BUS_CONFIG(pc_link->adapter, pc_link, self, pc_cf, cf);
251 	if (err)
252 		return err;
253 
254 	if (svec_card) {
255 		pc_cf->memwin = 0;
256 #if 0
257 		pc_cf->cfgid = 32;  /* Try this if it still doesn't work */
258 		pc_cf->cfgid |= 32;  /* or Try this if it still doesn't work */
259 #endif
260 	}
261 	if (de650_0) {
262 		pc_cf->io[0].flags =
263 		    (pc_cf->io[0].flags&~PCMCIA_MAP_16)|PCMCIA_MAP_8;
264 		pc_cf->memwin = 0;
265 		pc_cf->cfgtype = DOSRESET|1;
266 	}
267 	else {
268 		/* still wrong in CIS; fix it here */
269 		pc_cf->io[0].flags = PCMCIA_MAP_8|PCMCIA_MAP_16;
270 		pc_cf->cfgtype = 1;
271 	}
272 
273 	return err;
274 }
275 
276 int
ed_remove(pc_link,self)277 ed_remove(pc_link,self)
278 	struct pcmcia_link *pc_link;
279 	struct device *self;
280 {
281 	struct ed_softc *sc = (void *)self;
282 	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
283 	if_down(ifp);
284 	edstop(sc);
285 	shutdownhook_disestablish(sc->sc_sh);
286 	ifp->if_flags &= ~(IFF_RUNNING|IFF_UP);
287 	sc->spec_flags |= ED_NOTPRESENT;
288 	isa_intr_disestablish(sc->sc_ic, sc->sc_ih);
289 	return PCMCIA_BUS_UNCONFIG(pc_link->adapter, pc_link);
290 }
291 
292 static struct pcmcia_dlink {
293 	struct pcmcia_device pcd;
294 } pcmcia_dlink = {
295 	{"PCMCIA Novell compatible", edmod, ed_pcmcia_isa_attach,
296 	 NULL, ed_remove}
297 };
298 
299 struct pcmciadevs pcmcia_ed_devs[]={
300       { "ed", 0, "D-Link", "DE-650", "Ver 01.00", NULL, (void *) -1,
301 	(void *)&pcmcia_dlink },
302       { "ed", 0, "D-Link", "DE-650", "", NULL, (void *) 0x40,
303 	(void *)&pcmcia_dlink },
304       { "ed", 0, "LINKSYS", "E-CARD", "Ver 01.00", NULL, (void *)-1,
305         (void *)&pcmcia_dlink },
306       { "ed", 0, "IBM Corp.", "Ethernet", "0933495", NULL, (void *) 0xff0,
307 	(void *)&pcmcia_dlink },
308       { "ed", 0, "Socket Communications Inc",
309 	"Socket EA PCMCIA LAN Adapter Revision D", "Ethernet ID 000000000000",
310 	NULL, (void *) -1,
311 	(void *)&pcmcia_dlink },
312       /* something screwed up in ports requested */
313       { "ed", 0, "SVEC", "FD605 PCMCIA EtherNet Card", "V1-1", NULL,
314 	(void *)-1, (void *)&pcmcia_dlink },
315       { "ed", 0, "Ethernet", "Adapter", "2.0", NULL, (void *) -1,
316 	(void *)&pcmcia_dlink },
317 #if 0
318       /* not quite right for ethernet address */
319       { "ed", 0, "PMX   ", "PE-200", "ETHERNET", "R01", (void *)-1,
320         (void *)&pcmcia_dlink },
321 #endif
322       { NULL }
323 };
324 
325 #define ned_pcmcia_devs sizeof(pcmcia_ed_devs)/sizeof(pcmcia_ed_devs[0])
326 
327 int
ed_pcmcia_match(parent,match,aux)328 ed_pcmcia_match(parent, match, aux)
329 	struct device *parent;
330 	void *match, *aux;
331 {
332 	return pcmcia_slave_match(parent, match, aux, pcmcia_ed_devs,
333 				  ned_pcmcia_devs);
334 }
335 
336 void
ed_pcmcia_attach(parent,self,aux)337 ed_pcmcia_attach(parent, self, aux)
338 	struct device *parent, *self;
339 	void *aux;
340 {
341 	struct pcmcia_attach_args *paa = aux;
342 
343 	printf("ed_pcmcia_attach %p %p %p\n", parent, self, aux);
344 	delay(2000000);
345 	if (!pcmcia_configure(parent, self, paa->paa_link)) {
346 		struct ed_softc *sc = (void *)self;
347 		sc->spec_flags |= ED_NOTPRESENT;
348 		printf(": not attached\n");
349 	}
350 }
351 
352 /*
353  * No detach; network devices are too well linked into the rest of the
354  * kernel.
355  */
356 int
ed_pcmcia_detach(self)357 ed_pcmcia_detach(self)
358 	struct device *self;
359 {
360 	return EBUSY;
361 }
362 
363 #endif
364 
365 #if NED_PCI > 0
366 
367 #include <dev/pci/pcireg.h>
368 #include <dev/pci/pcivar.h>
369 #include <dev/pci/pcidevs.h>
370 
371 #define PCI_CBIO		0x10	/* Configuration Base IO Address */
372 
373 int	ed_pci_match(struct device *, void *, void *);
374 void	ed_pci_attach(struct device *, struct device *, void *);
375 
376 struct cfattach ed_pci_ca = {
377 	sizeof(struct ed_softc), ed_pci_match, ed_pci_attach
378 };
379 
380 static struct ed_pci_devs {
381 	pci_vendor_id_t vendor;
382 	pci_product_id_t product;
383 } ed_pci_devs[] = {
384 	{ PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8029 },
385 	{ PCI_VENDOR_WINBOND, PCI_PRODUCT_WINBOND_W89C940F },
386 	{ PCI_VENDOR_WINBOND2, PCI_PRODUCT_WINBOND2_W89C940 },
387 	{ PCI_VENDOR_NETVIN, PCI_PRODUCT_NETVIN_NV5000 },
388 	{ PCI_VENDOR_COMPEX, PCI_PRODUCT_COMPEX_COMPEXE },
389 	{ PCI_VENDOR_KTI, PCI_PRODUCT_KTI_KTIE },
390 	{ PCI_VENDOR_SURECOM, PCI_PRODUCT_SURECOM_NE34 },
391 	{ PCI_VENDOR_VIATECH, PCI_PRODUCT_VIATECH_VT86C926 },
392 };
393 
394 int
ed_pci_match(parent,match,aux)395 ed_pci_match(parent, match, aux)
396 	struct device *parent;
397 	void *match, *aux;
398 {
399 	struct pci_attach_args *pa = aux;
400 	int i;
401 
402 	for (i = 0; i < sizeof(ed_pci_devs)/sizeof(ed_pci_devs[0]); i++)
403 		if (ed_pci_devs[i].vendor == PCI_VENDOR(pa->pa_id) &&
404 		    ed_pci_devs[i].product == PCI_PRODUCT(pa->pa_id))
405 			return (1);
406 	return (0);
407 }
408 
409 /*
410  * XXX - Note that we pretend this is a 16bit card until the rest
411  * of the driver can deal with a 32bit bus (isa16bit -> bus_width)
412  */
413 void
ed_pci_attach(parent,self,aux)414 ed_pci_attach(parent, self, aux)
415 	struct device *parent, *self;
416 	void *aux;
417 {
418 	struct ed_softc *sc = (void *)self;
419 	struct pci_attach_args *pa = aux;
420 	pci_chipset_tag_t pc = pa->pa_pc;
421 	pci_intr_handle_t ih;
422 	bus_space_tag_t iot;
423 	bus_space_handle_t ioh;
424 	bus_addr_t iobase;
425 	bus_size_t iosize, asicbase, nicbase;
426 	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
427 	u_char romdata[32], tmp;
428 	const char *intrstr;
429 	int i;
430 
431 	iot = pa->pa_iot;
432 
433 	if (pci_io_find(pc, pa->pa_tag, PCI_CBIO, &iobase, &iosize)) {
434 		printf("%s: can't find I/O base\n", sc->sc_dev.dv_xname);
435 		return;
436 	}
437 
438 	if (bus_space_map(iot, iobase, iosize, 0, &ioh)) {
439 		printf("%s: can't map I/O space\n", sc->sc_dev.dv_xname);
440 		return;
441 	}
442 
443 	sc->asic_base = asicbase = ED_NOVELL_ASIC_OFFSET;
444 	sc->nic_base = nicbase = ED_NOVELL_NIC_OFFSET;
445 	sc->vendor = ED_VENDOR_NOVELL;
446 	sc->mem_shared = 0;
447 	sc->cr_proto = ED_CR_RD2;
448 	sc->type = ED_TYPE_NE2000;
449 	sc->type_str = "NE2000";
450 
451 	/* Reset the board. */
452 	tmp = bus_space_read_1(iot, ioh, asicbase + ED_NOVELL_RESET);
453 
454 	/* Put the board into 16-bit mode (XXX - someday do 32-bit) */
455 	sc->isa16bit = 1;
456 	NIC_PUT(iot, ioh, nicbase, ED_P0_DCR,
457 	    ED_DCR_WTS | ED_DCR_FT1 | ED_DCR_LS);
458 	NIC_PUT(iot, ioh, nicbase, ED_P0_PSTART, 16384 >> ED_PAGE_SHIFT);
459 	NIC_PUT(iot, ioh, nicbase, ED_P0_PSTOP, 32768 >> ED_PAGE_SHIFT);
460 
461 	/*
462 	 * NIC memory doesn't start at zero on an NE board.
463 	 * The start address (and size) is tied to the bus width.
464 	 * XXX - these should be 32K but the driver doesn't grok > 16bit
465 	 */
466 	sc->mem_size = 16384;		/* XXX - should be 8K x bus width */
467 	sc->mem_start = 16384;		/*     - and this as well */
468 	sc->mem_end = sc->mem_start + sc->mem_size;
469 	sc->tx_page_start = sc->mem_size >> ED_PAGE_SHIFT;
470 	sc->txb_cnt = sc->mem_size / 8192;
471 	sc->rec_page_start = sc->tx_page_start + sc->txb_cnt * ED_TXBUF_SIZE;
472 	sc->rec_page_stop = sc->tx_page_start + (sc->mem_size >> ED_PAGE_SHIFT);
473 	sc->mem_ring =
474 	    sc->mem_start + ((sc->txb_cnt * ED_TXBUF_SIZE) << ED_PAGE_SHIFT);
475 	sc->sc_delaybah = 0;			/* unused */
476 	sc->sc_iot = iot;
477 	sc->sc_ioh = ioh;
478 
479 	/* Get ethernet address (XXX - size field should be "8 * buswidth") */
480 	ed_pio_readmem(sc, 0, romdata, sizeof(romdata));
481 	/* XXX - change to (i * buswidth) when driver does 32bit */
482 	for (i = 0; i < ETHER_ADDR_LEN; i++)
483 		sc->sc_arpcom.ac_enaddr[i] = romdata[i * 2];
484 
485 	/* Clear any pending interrupts that might have occurred above. */
486 	NIC_PUT(iot, ioh, nicbase, ED_P0_ISR, 0xff);
487 
488 	/* Set interface to stopped condition (reset). */
489 	edstop(sc);
490 
491 	/* Initialize ifnet structure. */
492 	bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
493 	ifp->if_softc = sc;
494 	ifp->if_start = edstart;
495 	ifp->if_ioctl = edioctl;
496 	ifp->if_watchdog = edwatchdog;
497 	ifp->if_flags =
498 	    IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
499 	IFQ_SET_READY(&ifp->if_snd);
500 
501 	/* Attach the interface. */
502 	if ((sc->spec_flags & ED_REATTACH) == 0)
503 		if_attach(ifp);
504 	ether_ifattach(ifp);
505 
506 	/* Print additional info when attached. */
507 	printf(": address %s, ", ether_sprintf(sc->sc_arpcom.ac_enaddr));
508 
509 	if (sc->type_str)
510 		printf("type %s ", sc->type_str);
511 	else
512 		printf("type unknown (0x%x) ", sc->type);
513 	printf("%s", sc->isa16bit ? "(16-bit)" : "(8-bit)");	/* XXX */
514 
515 	/* Map and establish the interrupt. */
516 	if (pci_intr_map(pa, &ih)) {
517 		printf("\n%s: couldn't map interrupt\n", sc->sc_dev.dv_xname);
518 		return;
519 	}
520 	intrstr = pci_intr_string(pc, ih);
521 	sc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, edintr,
522 	    sc, sc->sc_dev.dv_xname);
523 	if (sc->sc_ih == NULL) {
524 		printf("\n%s: couldn't establish interrupt",
525 		    sc->sc_dev.dv_xname);
526 		if (intrstr != NULL)
527 			printf(" at %s", intrstr);
528 		printf("\n");
529 		return;
530 	}
531 	printf(", %s\n", intrstr);
532 }
533 
534 #endif
535 
536 /*
537  * Determine if the device is present.
538  */
539 int
edprobe(parent,match,aux)540 edprobe(parent, match, aux)
541 	struct device *parent;
542 	void *match, *aux;
543 {
544 	struct ed_softc *sc = match;
545 
546 	return (ed_find(match, sc->sc_dev.dv_cfdata, aux));
547 }
548 
549 /*
550  * Fill in softc (if given), based on device type, cfdata and attach args.
551  * Return 1 if successful, 0 otherwise.
552  */
553 int
ed_find(sc,cf,ia)554 ed_find(sc, cf, ia)
555 	struct ed_softc *sc;
556 	struct cfdata *cf;
557 	struct isa_attach_args *ia;
558 {
559 
560 	if (ed_find_WD80x3(sc, cf, ia))
561 		return (1);
562 	if (ed_find_3Com(sc, cf, ia))
563 		return (1);
564 	if (ed_find_Novell(sc, cf, ia))
565 		return (1);
566 	return (0);
567 }
568 
569 /*
570  * Generic probe routine for testing for the existance of a DS8390.  Must be
571  * called after the NIC has just been reset.  This routine works by looking at
572  * certain register values that are guaranteed to be initialized a certain way
573  * after power-up or reset.  Seems not to currently work on the 83C690.
574  *
575  * Specifically:
576  *
577  *	Register			reset bits	set bits
578  *	Command Register (CR)		TXP, STA	RD2, STP
579  *	Interrupt Status (ISR)				RST
580  *	Interrupt Mask (IMR)		All bits
581  *	Data Control (DCR)				LAS
582  *	Transmit Config. (TCR)		LB1, LB0
583  *
584  * We only look at the CR and ISR registers, however, because looking at the
585  * others would require changing register pages (which would be intrusive if
586  * this isn't an 8390).
587  *
588  * Return 1 if 8390 was found, 0 if not.
589  */
590 int
ed_probe_generic8390(t,bah,nicbase)591 ed_probe_generic8390(t, bah, nicbase)
592 	bus_space_tag_t t;
593 	bus_space_handle_t bah;
594 	int nicbase;
595 {
596 
597 	if ((NIC_GET(t, bah, nicbase, ED_P0_CR) &
598 	     (ED_CR_RD2 | ED_CR_TXP | ED_CR_STA | ED_CR_STP)) !=
599 	    (ED_CR_RD2 | ED_CR_STP))
600 		return (0);
601 	if ((NIC_GET(t, bah, nicbase, ED_P0_ISR) & ED_ISR_RST) != ED_ISR_RST)
602 		return (0);
603 
604 	return (1);
605 }
606 
607 int ed_wd584_irq[] = { 9, 3, 5, 7, 10, 11, 15, 4 };
608 int ed_wd790_irq[] = { IRQUNK, 9, 3, 5, 7, 10, 11, 15 };
609 
610 /*
611  * Probe and vendor-specific initialization routine for SMC/WD80x3 boards.
612  */
613 int
ed_find_WD80x3(sc,cf,ia)614 ed_find_WD80x3(sc, cf, ia)
615 	struct ed_softc *sc;
616 	struct cfdata *cf;
617 	struct isa_attach_args *ia;
618 {
619 	bus_space_tag_t iot;
620 	bus_space_tag_t memt;
621 	bus_space_handle_t ioh;
622 	bus_space_handle_t delaybah = ia->ia_delaybah;
623 	bus_space_handle_t memh;
624 	u_int memsize;
625 	u_char iptr, isa16bit, sum, wd790rev;
626 	int i, rv, memfail, mapped_mem = 0;
627 	int asicbase, nicbase;
628 
629 	iot = ia->ia_iot;
630 	memt = ia->ia_memt;
631 	rv = 0;
632 
633 	/* Set initial values for width/size. */
634 	memsize = 8192;
635 	isa16bit = 0;
636 
637 	if (bus_space_map(iot, ia->ia_iobase, ED_WD_IO_PORTS, 0, &ioh))
638 		return (0);
639 
640 	sc->asic_base = asicbase = 0;
641 	sc->nic_base = nicbase = asicbase + ED_WD_NIC_OFFSET;
642 	sc->is790 = 0;
643 
644 #ifdef TOSH_ETHER
645 	bus_space_write_1(iot, ioh, asicbase + ED_WD_MSR, ED_WD_MSR_POW);
646 	delay(10000);
647 #endif
648 
649 	/*
650 	 * Attempt to do a checksum over the station address PROM.  If it
651 	 * fails, it's probably not a SMC/WD board.  There is a problem with
652 	 * this, though: some clone WD boards don't pass the checksum test.
653 	 * Danpex boards for one.
654 	 */
655 	for (sum = 0, i = 0; i < 8; ++i)
656 		sum += bus_space_read_1(iot, ioh, asicbase + ED_WD_PROM + i);
657 
658 	if (sum != ED_WD_ROM_CHECKSUM_TOTAL) {
659 		/*
660 		 * Checksum is invalid.  This often happens with cheap WD8003E
661 		 * clones.  In this case, the checksum byte (the eighth byte)
662 		 * seems to always be zero.
663 		 */
664 		if (bus_space_read_1(iot, ioh, asicbase + ED_WD_CARD_ID) !=
665 		    ED_TYPE_WD8003E ||
666 		    bus_space_read_1(iot, ioh, asicbase + ED_WD_PROM + 7) != 0)
667 			goto out;
668 	}
669 
670 	/* Reset card to force it into a known state. */
671 #ifdef TOSH_ETHER
672 	bus_space_write_1(iot, ioh, asicbase + ED_WD_MSR,
673 	    ED_WD_MSR_RST | ED_WD_MSR_POW);
674 #else
675 	bus_space_write_1(iot, ioh, asicbase + ED_WD_MSR, ED_WD_MSR_RST);
676 #endif
677 	delay(100);
678 	bus_space_write_1(iot, ioh, asicbase + ED_WD_MSR,
679 	    bus_space_read_1(iot, ioh, asicbase + ED_WD_MSR) & ~ED_WD_MSR_RST);
680 	/* Wait in the case this card is reading it's EEROM. */
681 	delay(5000);
682 
683 	sc->vendor = ED_VENDOR_WD_SMC;
684 	sc->type = bus_space_read_1(iot, ioh, asicbase + ED_WD_CARD_ID);
685 
686 	switch (sc->type) {
687 	case ED_TYPE_WD8003S:
688 		sc->type_str = "WD8003S";
689 		break;
690 	case ED_TYPE_WD8003E:
691 		sc->type_str = "WD8003E";
692 		break;
693 	case ED_TYPE_WD8003EB:
694 		sc->type_str = "WD8003EB";
695 		break;
696 	case ED_TYPE_WD8003W:
697 		sc->type_str = "WD8003W";
698 		break;
699 	case ED_TYPE_WD8013EBT:
700 		sc->type_str = "WD8013EBT";
701 		memsize = 16384;
702 		isa16bit = 1;
703 		break;
704 	case ED_TYPE_WD8013W:
705 		sc->type_str = "WD8013W";
706 		memsize = 16384;
707 		isa16bit = 1;
708 		break;
709 	case ED_TYPE_WD8013EP:		/* also WD8003EP */
710 		if (bus_space_read_1(iot, ioh, asicbase + ED_WD_ICR)
711 		    & ED_WD_ICR_16BIT) {
712 			isa16bit = 1;
713 			memsize = 16384;
714 			sc->type_str = "WD8013EP";
715 		} else
716 			sc->type_str = "WD8003EP";
717 		break;
718 	case ED_TYPE_WD8013WC:
719 		sc->type_str = "WD8013WC";
720 		memsize = 16384;
721 		isa16bit = 1;
722 		break;
723 	case ED_TYPE_WD8013EBP:
724 		sc->type_str = "WD8013EBP";
725 		memsize = 16384;
726 		isa16bit = 1;
727 		break;
728 	case ED_TYPE_WD8013EPC:
729 		sc->type_str = "WD8013EPC";
730 		memsize = 16384;
731 		isa16bit = 1;
732 		break;
733 	case ED_TYPE_SMC8216C:
734 	case ED_TYPE_SMC8216T:
735 		wd790rev = bus_space_read_1(iot, ioh, asicbase + ED_WD790_REV);
736 		if (wd790rev < ED_WD795)
737 			sc->type_str = (sc->type == ED_TYPE_SMC8216C) ?
738 			    "SMC8216/SMC8216C" : "SMC8216T";
739 		else {
740 			sc->type_str = "SMC8416C/SMC8416BT";
741 			if (bus_space_read_1(iot, ioh,
742 					     asicbase + ED_WD795_PIO)) {
743 				printf ("%s: detected SMC8416 in PIO mode, unsupported hardware configuration.\n", sc->sc_dev.dv_xname);
744 				goto out;
745 			}
746 		}
747 
748 		bus_space_write_1(iot, ioh, asicbase + ED_WD790_HWR,
749 		    bus_space_read_1(iot, ioh, asicbase + ED_WD790_HWR)
750 		    | ED_WD790_HWR_SWH);
751 		switch (bus_space_read_1(iot, ioh, asicbase + ED_WD790_RAR) &
752 		    ED_WD790_RAR_SZ64) {
753 		case ED_WD790_RAR_SZ64:
754 			memsize = 65536;
755 			break;
756 		case ED_WD790_RAR_SZ32:
757 			memsize = 32768;
758 			break;
759 		case ED_WD790_RAR_SZ16:
760 			memsize = 16384;
761 			break;
762 		case ED_WD790_RAR_SZ8:
763 			memsize = 8192;
764 			break;
765 		}
766 		bus_space_write_1(iot, ioh, asicbase + ED_WD790_HWR,
767 		    bus_space_read_1(iot, ioh, asicbase + ED_WD790_HWR) &
768 		    ~ED_WD790_HWR_SWH);
769 
770 		isa16bit = 1;
771 		sc->is790 = 1;
772 		break;
773 #ifdef TOSH_ETHER
774 	case ED_TYPE_TOSHIBA1:
775 		sc->type_str = "Toshiba1";
776 		memsize = 32768;
777 		isa16bit = 1;
778 		break;
779 	case ED_TYPE_TOSHIBA4:
780 		sc->type_str = "Toshiba4";
781 		memsize = 32768;
782 		isa16bit = 1;
783 		break;
784 #endif
785 	default:
786 		sc->type_str = NULL;
787 		break;
788 	}
789 	/*
790 	 * Make some adjustments to initial values depending on what is found
791 	 * in the ICR.
792 	 */
793 	if (isa16bit && (sc->type != ED_TYPE_WD8013EBT) &&
794 #ifdef TOSH_ETHER
795 	    (sc->type != ED_TYPE_TOSHIBA1) && (sc->type != ED_TYPE_TOSHIBA4) &&
796 #endif
797 	    ((bus_space_read_1(iot, ioh, asicbase + ED_WD_ICR) &
798 	    ED_WD_ICR_16BIT) == 0)) {
799 		isa16bit = 0;
800 		memsize = 8192;
801 	}
802 
803 #ifdef ED_DEBUG
804 	printf("type=%x type_str=%s isa16bit=%d memsize=%d id_msize=%d\n",
805 	    sc->type, sc->type_str ?: "unknown", isa16bit, memsize,
806 	    ia->ia_msize);
807 	for (i = 0; i < 8; i++)
808 		printf("%x -> %x\n", i, bus_space_read_1(iot, ioh,
809 		    asicbase + i));
810 #endif
811 	/* Allow the user to override the autoconfiguration. */
812 	if (ia->ia_msize)
813 		memsize = ia->ia_msize;
814 	/*
815 	 * (Note that if the user specifies both of the following flags that
816 	 * '8-bit' mode intentionally has precedence.)
817 	 */
818 	if (cf->cf_flags & ED_FLAGS_FORCE_16BIT_MODE)
819 		isa16bit = 1;
820 	if (cf->cf_flags & ED_FLAGS_FORCE_8BIT_MODE)
821 		isa16bit = 0;
822 
823 	/*
824 	 * If possible, get the assigned interrupt number from the card and
825 	 * use it.
826 	 */
827 	if (sc->is790) {
828 		u_char x;
829 
830 		/* Assemble together the encoded interrupt number. */
831 		bus_space_write_1(iot, ioh, ED_WD790_HWR,
832 		    bus_space_read_1(iot, ioh, ED_WD790_HWR) |
833 		    ED_WD790_HWR_SWH);
834 		x = bus_space_read_1(iot, ioh, ED_WD790_GCR);
835 		iptr = ((x & ED_WD790_GCR_IR2) >> 4) |
836 		    ((x & (ED_WD790_GCR_IR1|ED_WD790_GCR_IR0)) >> 2);
837 		bus_space_write_1(iot, ioh, ED_WD790_HWR,
838 		    bus_space_read_1(iot, ioh, ED_WD790_HWR) &
839 		    ~ED_WD790_HWR_SWH);
840 		/*
841 		 * Translate it using translation table, and check for
842 		 * correctness.
843 		 */
844 		if (ia->ia_irq != IRQUNK) {
845 			if (ia->ia_irq != ed_wd790_irq[iptr]) {
846 				printf("%s: irq mismatch; kernel configured %d != board configured %d\n",
847 				    sc->sc_dev.dv_xname, ia->ia_irq,
848 				    ed_wd790_irq[iptr]);
849 				goto out;
850 			}
851 		} else
852 			ia->ia_irq = ed_wd790_irq[iptr];
853 		/* Enable the interrupt. */
854 		bus_space_write_1(iot, ioh, ED_WD790_ICR,
855 		    bus_space_read_1(iot, ioh, ED_WD790_ICR) |
856 		    ED_WD790_ICR_EIL);
857 	} else if (sc->type & ED_WD_SOFTCONFIG) {
858 		/* Assemble together the encoded interrupt number. */
859 		iptr = (bus_space_read_1(iot, ioh, ED_WD_ICR) &
860 		    ED_WD_ICR_IR2) |
861 		    ((bus_space_read_1(iot, ioh, ED_WD_IRR) &
862 		      (ED_WD_IRR_IR0 | ED_WD_IRR_IR1)) >> 5);
863 		/*
864 		 * Translate it using translation table, and check for
865 		 * correctness.
866 		 */
867 		if (ia->ia_irq != IRQUNK) {
868 			if (ia->ia_irq != ed_wd584_irq[iptr]) {
869 				printf("%s: irq mismatch; kernel configured %d != board configured %d\n",
870 				    sc->sc_dev.dv_xname, ia->ia_irq,
871 				    ed_wd584_irq[iptr]);
872 				goto out;
873 			}
874 		} else
875 			ia->ia_irq = ed_wd584_irq[iptr];
876 		/* Enable the interrupt. */
877 		bus_space_write_1(iot, ioh, ED_WD_IRR,
878 		    bus_space_read_1(iot, ioh, ED_WD_IRR) | ED_WD_IRR_IEN);
879 	} else {
880 		if (ia->ia_irq == IRQUNK) {
881 			printf("%s: %s does not have soft configuration\n",
882 			    sc->sc_dev.dv_xname, sc->type_str);
883 			goto out;
884 		}
885 	}
886 
887 	/* XXX Figure out the shared memory address. */
888 
889 	if (ia->ia_maddr == MADDRUNK)
890 		goto out;
891 	sc->isa16bit = isa16bit;
892 	sc->mem_shared = 1;
893 	ia->ia_msize = memsize;
894 	if (bus_space_map(memt, ia->ia_maddr, memsize, 0, &memh))
895 		goto out;
896 	mapped_mem = 1;
897 	sc->mem_start = 0;	/* offset */
898 
899 	/* Allocate one xmit buffer if < 16k, two buffers otherwise. */
900 	if ((memsize < 16384) || (cf->cf_flags & ED_FLAGS_NO_MULTI_BUFFERING))
901 		sc->txb_cnt = 1;
902 	else
903 		sc->txb_cnt = 2;
904 
905 	sc->tx_page_start = ED_WD_PAGE_OFFSET;
906 	sc->rec_page_start = sc->tx_page_start + sc->txb_cnt * ED_TXBUF_SIZE;
907 	sc->rec_page_stop = sc->tx_page_start + (memsize >> ED_PAGE_SHIFT);
908 	sc->mem_ring = sc->mem_start + (sc->rec_page_start << ED_PAGE_SHIFT);
909 	sc->mem_size = memsize;
910 	sc->mem_end = sc->mem_start + memsize;
911 
912 	/* Get station address from on-board ROM. */
913 	for (i = 0; i < ETHER_ADDR_LEN; ++i)
914 		sc->sc_arpcom.ac_enaddr[i] =
915 		    bus_space_read_1(iot, ioh, asicbase + ED_WD_PROM + i);
916 
917 	/*
918 	 * Set upper address bits and 8/16 bit access to shared memory.
919 	 */
920 	if (isa16bit) {
921 		if (sc->is790) {
922 			sc->wd_laar_proto =
923 			    bus_space_read_1(iot, ioh, asicbase + ED_WD_LAAR) &
924 			    ~ED_WD_LAAR_M16EN;
925 		} else {
926 			sc->wd_laar_proto = ED_WD_LAAR_L16EN |
927 			    ((ia->ia_maddr >> 19) & ED_WD_LAAR_ADDRHI);
928 		}
929 		bus_space_write_1(iot, ioh, asicbase + ED_WD_LAAR,
930 		    sc->wd_laar_proto | ED_WD_LAAR_M16EN);
931 	} else  {
932 		if ((sc->type & ED_WD_SOFTCONFIG) ||
933 #ifdef TOSH_ETHER
934 		    (sc->type == ED_TYPE_TOSHIBA1) ||
935 		    (sc->type == ED_TYPE_TOSHIBA4) ||
936 #endif
937 		    ((sc->type == ED_TYPE_WD8013EBT) && !sc->is790)) {
938 			sc->wd_laar_proto =
939 			    ((ia->ia_maddr >> 19) &
940 			    ED_WD_LAAR_ADDRHI);
941 			bus_space_write_1(iot, ioh, asicbase + ED_WD_LAAR,
942 			    sc->wd_laar_proto);
943 		}
944 	}
945 
946 	/*
947 	 * Set address and enable interface shared memory.
948 	 */
949 	if (!sc->is790) {
950 #ifdef TOSH_ETHER
951 		bus_space_write_1(iot, ioh, asicbase + ED_WD_MSR + 1,
952 		    ((ia->ia_maddr >> 8) & 0xe0) | 4);
953 		bus_space_write_1(iot, ioh, asicbase + ED_WD_MSR + 2,
954 		    ((ia->ia_maddr >> 16) & 0x0f));
955 		sc->wd_msr_proto = ED_WD_MSR_POW;
956 #else
957 		sc->wd_msr_proto =
958 		    (ia->ia_maddr >> 13) & ED_WD_MSR_ADDR;
959 #endif
960 		sc->cr_proto = ED_CR_RD2;
961 	} else {
962 		bus_space_write_1(iot, ioh, asicbase + 0x04,
963 		    bus_space_read_1(iot, ioh, asicbase + 0x04) | 0x80);
964 		bus_space_write_1(iot, ioh, asicbase + 0x0b,
965 		    ((ia->ia_maddr >> 13) & 0x0f) |
966 		    ((ia->ia_maddr >> 11) & 0x40) |
967 		    (bus_space_read_1(iot, ioh, asicbase + 0x0b) & 0xb0));
968 		bus_space_write_1(iot, ioh, asicbase + 0x04,
969 		    bus_space_read_1(iot, ioh, asicbase + 0x04) & ~0x80);
970 		sc->wd_msr_proto = 0x00;
971 		sc->cr_proto = 0;
972 	}
973 	bus_space_write_1(iot, ioh, asicbase + ED_WD_MSR,
974 	    sc->wd_msr_proto | ED_WD_MSR_MENB);
975 
976 	(void) bus_space_read_1(iot, delaybah, 0);
977 	(void) bus_space_read_1(iot, delaybah, 0);
978 
979 	/* Now zero memory and verify that it is clear. */
980 	if (isa16bit) {
981 		for (i = 0; i < memsize; i += 2)
982 			bus_space_write_2(memt, memh, sc->mem_start + i, 0);
983 	} else {
984 		for (i = 0; i < memsize; ++i)
985 			bus_space_write_1(memt, memh, sc->mem_start + i, 0);
986 	}
987 
988 	memfail = 0;
989 	if (isa16bit) {
990 		for (i = 0; i < memsize; i += 2) {
991 			if (bus_space_read_2(memt, memh, sc->mem_start + i)) {
992 				memfail = 1;
993 				break;
994 			}
995 		}
996 	} else {
997 		for (i = 0; i < memsize; ++i) {
998 			if (bus_space_read_1(memt, memh, sc->mem_start + i)) {
999 				memfail = 1;
1000 				break;
1001 			}
1002 		}
1003 	}
1004 
1005 	if (memfail) {
1006 		printf("%s: failed to clear shared memory at %x - "
1007 		    "check configuration\n",
1008 		    sc->sc_dev.dv_xname,
1009 		    (ia->ia_maddr + sc->mem_start + i));
1010 
1011 		/* Disable 16 bit access to shared memory. */
1012 		bus_space_write_1(iot, ioh, asicbase + ED_WD_MSR,
1013 		    sc->wd_msr_proto);
1014 		if (isa16bit)
1015 			bus_space_write_1(iot, ioh, asicbase + ED_WD_LAAR,
1016 			    sc->wd_laar_proto);
1017 		(void) bus_space_read_1(iot, delaybah, 0);
1018 		(void) bus_space_read_1(iot, delaybah, 0);
1019 		goto out;
1020 	}
1021 
1022 	/*
1023 	 * Disable 16bit access to shared memory - we leave it disabled
1024 	 * so that 1) machines reboot properly when the board is set 16
1025 	 * 16 bit mode and there are conflicting 8bit devices/ROMS in
1026 	 * the same 128k address space as this boards shared memory,
1027 	 * and 2) so that other 8 bit devices with shared memory can be
1028 	 * used in this 128k region, too.
1029 	 */
1030 	bus_space_write_1(iot, ioh, asicbase + ED_WD_MSR, sc->wd_msr_proto);
1031 	if (isa16bit)
1032 		bus_space_write_1(iot, ioh, asicbase + ED_WD_LAAR,
1033 		    sc->wd_laar_proto);
1034 	(void) bus_space_read_1(iot, delaybah, 0);
1035 	(void) bus_space_read_1(iot, delaybah, 0);
1036 
1037 	ia->ia_iosize = ED_WD_IO_PORTS;
1038 	rv = 1;
1039 
1040  out:
1041 	/*
1042 	 * XXX Should always unmap, but we can't yet.
1043 	 * XXX Need to squish "indirect" first.
1044 	 */
1045 	if (rv == 0) {
1046 		bus_space_unmap(iot, ioh, ED_WD_IO_PORTS);
1047 		if (mapped_mem)
1048 			bus_space_unmap(memt, memh, memsize);
1049 	} else {
1050 		/* XXX this is all "indirect" brokenness */
1051 		sc->sc_iot = iot;
1052 		sc->sc_memt = memt;
1053 		sc->sc_ioh = ioh;
1054 		sc->sc_memh = memh;
1055 	}
1056 	return (rv);
1057 }
1058 
1059 int ed_3com_iobase[] =
1060     {0x2e0, 0x2a0, 0x280, 0x250, 0x350, 0x330, 0x310, 0x300};
1061 int ed_3com_maddr[] = {
1062     MADDRUNK, MADDRUNK, MADDRUNK, MADDRUNK, 0xc8000, 0xcc000, 0xd8000, 0xdc000
1063 };
1064 #if 0
1065 int ed_3com_irq[] = {IRQUNK, IRQUNK, IRQUNK, IRQUNK, 9, 3, 4, 5};
1066 #endif
1067 
1068 /*
1069  * Probe and vendor-specific initialization routine for 3Com 3c503 boards.
1070  */
1071 int
ed_find_3Com(sc,cf,ia)1072 ed_find_3Com(sc, cf, ia)
1073 	struct ed_softc *sc;
1074 	struct cfdata *cf;
1075 	struct isa_attach_args *ia;
1076 {
1077 	bus_space_tag_t iot;
1078 	bus_space_tag_t memt;
1079 	bus_space_handle_t ioh;
1080 	bus_space_handle_t memh;
1081 	int i;
1082 	u_int memsize, memfail;
1083 	u_char isa16bit, x;
1084 	int ptr, asicbase, nicbase;
1085 
1086 	/*
1087 	 * Hmmm...a 16bit 3Com board has 16k of memory, but only an 8k window
1088 	 * to it.
1089 	 */
1090 	memsize = 8192;
1091 
1092 	iot = ia->ia_iot;
1093 	memt = ia->ia_memt;
1094 
1095 	if (bus_space_map(iot, ia->ia_iobase, ED_3COM_IO_PORTS, 0, &ioh))
1096 		return (0);
1097 
1098 	sc->asic_base = asicbase = ED_3COM_ASIC_OFFSET;
1099 	sc->nic_base = nicbase = ED_3COM_NIC_OFFSET;
1100 
1101 	/*
1102 	 * Verify that the kernel configured I/O address matches the board
1103 	 * configured address.
1104 	 *
1105 	 * This is really only useful to see if something that looks like the
1106 	 * board is there; after all, we are already talking it at that
1107 	 * address.
1108 	 */
1109 	x = bus_space_read_1(iot, ioh, asicbase + ED_3COM_BCFR);
1110 	if (x == 0 || (x & (x - 1)) != 0)
1111 		goto err;
1112 	ptr = ffs(x) - 1;
1113 	if (ia->ia_iobase != IOBASEUNK) {
1114 		if (ia->ia_iobase != ed_3com_iobase[ptr]) {
1115 			printf("%s: %s mismatch; kernel configured %x != board configured %x\n",
1116 			    "iobase", sc->sc_dev.dv_xname, ia->ia_iobase,
1117 			    ed_3com_iobase[ptr]);
1118 			goto err;
1119 		}
1120 	} else
1121 		ia->ia_iobase = ed_3com_iobase[ptr];	/* XXX --thorpej */
1122 
1123 	x = bus_space_read_1(iot, ioh, asicbase + ED_3COM_PCFR);
1124 	if (x == 0 || (x & (x - 1)) != 0) {
1125 		printf("%s: The 3c503 is not currently supported with memory "
1126 		       "mapping disabled.\n%s: Reconfigure the card to "
1127 		       "enable memory mapping.\n",
1128 		       sc->sc_dev.dv_xname, sc->sc_dev.dv_xname);
1129 		goto err;
1130 	}
1131 	ptr = ffs(x) - 1;
1132 	if (ia->ia_maddr != MADDRUNK) {
1133 		if (ia->ia_maddr != ed_3com_maddr[ptr]) {
1134 			printf("%s: %s mismatch; kernel configured %x != board configured %x\n",
1135 			    "maddr", sc->sc_dev.dv_xname, ia->ia_maddr,
1136 			    ed_3com_maddr[ptr]);
1137 			goto err;
1138 		}
1139 	} else
1140 		ia->ia_maddr = ed_3com_maddr[ptr];
1141 
1142 #if 0
1143 	x = bus_space_read_1(iot, ioh, asicbase + ED_3COM_IDCFR) &
1144 	    ED_3COM_IDCFR_IRQ;
1145 	if (x == 0 || (x & (x - 1)) != 0)
1146 		goto out;
1147 	ptr = ffs(x) - 1;
1148 	if (ia->ia_irq != IRQUNK) {
1149 		if (ia->ia_irq != ed_3com_irq[ptr]) {
1150 			printf("%s: irq mismatch; kernel configured %d != board configured %d\n",
1151 			    sc->sc_dev.dv_xname, ia->ia_irq,
1152 			    ed_3com_irq[ptr]);
1153 			goto err;
1154 		}
1155 	} else
1156 		ia->ia_irq = ed_3com_irq[ptr];
1157 #endif
1158 
1159 	/*
1160 	 * Reset NIC and ASIC.  Enable on-board transceiver throughout reset
1161 	 * sequence because it'll lock up if the cable isn't connected if we
1162 	 * don't.
1163 	 */
1164 	bus_space_write_1(iot, ioh, asicbase + ED_3COM_CR,
1165 	    ED_3COM_CR_RST | ED_3COM_CR_XSEL);
1166 
1167 	/* Wait for a while, then un-reset it. */
1168 	delay(50);
1169 
1170 	/*
1171 	 * The 3Com ASIC defaults to rather strange settings for the CR after a
1172 	 * reset - it's important to set it again after the following outb
1173 	 * (this is done when we map the PROM below).
1174 	 */
1175 	bus_space_write_1(iot, ioh, asicbase + ED_3COM_CR, ED_3COM_CR_XSEL);
1176 
1177 	/* Wait a bit for the NIC to recover from the reset. */
1178 	delay(5000);
1179 
1180 	sc->vendor = ED_VENDOR_3COM;
1181 	sc->type_str = "3c503";
1182 	sc->mem_shared = 1;
1183 	sc->cr_proto = ED_CR_RD2;
1184 
1185 	/*
1186 	 * Get station address from on-board ROM.
1187 	 *
1188 	 * First, map ethernet address PROM over the top of where the NIC
1189 	 * registers normally appear.
1190 	 */
1191 	bus_space_write_1(iot, ioh, asicbase + ED_3COM_CR,
1192 	    ED_3COM_CR_EALO | ED_3COM_CR_XSEL);
1193 
1194 	for (i = 0; i < ETHER_ADDR_LEN; ++i)
1195 		sc->sc_arpcom.ac_enaddr[i] = NIC_GET(iot, ioh, nicbase, i);
1196 
1197 	/*
1198 	 * Unmap PROM - select NIC registers.  The proper setting of the
1199 	 * transceiver is set in edinit so that the attach code is given a
1200 	 * chance to set the default based on a compile-time config option.
1201 	 */
1202 	bus_space_write_1(iot, ioh, asicbase + ED_3COM_CR, ED_3COM_CR_XSEL);
1203 
1204 	/* Determine if this is an 8bit or 16bit board. */
1205 
1206 	/* Select page 0 registers. */
1207 	NIC_PUT(iot, ioh, nicbase, ED_P0_CR,
1208 	    ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STP);
1209 
1210 	/*
1211 	 * Attempt to clear WTS bit.  If it doesn't clear, then this is a
1212 	 * 16-bit board.
1213 	 */
1214 	NIC_PUT(iot, ioh, nicbase, ED_P0_DCR, 0);
1215 
1216 	/* Select page 2 registers. */
1217 	NIC_PUT(iot, ioh, nicbase,
1218 	    ED_P0_CR, ED_CR_RD2 | ED_CR_PAGE_2 | ED_CR_STP);
1219 
1220 	/* The 3c503 forces the WTS bit to a one if this is a 16bit board. */
1221 	if (NIC_GET(iot, ioh, nicbase, ED_P2_DCR) & ED_DCR_WTS)
1222 		isa16bit = 1;
1223 	else
1224 		isa16bit = 0;
1225 
1226 	/* Select page 0 registers. */
1227 	NIC_PUT(iot, ioh, nicbase, ED_P2_CR,
1228 	    ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STP);
1229 
1230 	if (bus_space_map(memt, ia->ia_maddr, memsize, 0, &memh))
1231 		goto err;
1232 	sc->mem_start = 0;		/* offset */
1233 	sc->mem_size = memsize;
1234 	sc->mem_end = sc->mem_start + memsize;
1235 
1236 	/*
1237 	 * We have an entire 8k window to put the transmit buffers on the
1238 	 * 16-bit boards.  But since the 16bit 3c503's shared memory is only
1239 	 * fast enough to overlap the loading of one full-size packet, trying
1240 	 * to load more than 2 buffers can actually leave the transmitter idle
1241 	 * during the load.  So 2 seems the best value.  (Although a mix of
1242 	 * variable-sized packets might change this assumption.  Nonetheless,
1243 	 * we optimize for linear transfers of same-size packets.)
1244 	 */
1245 	if (isa16bit) {
1246  		if (cf->cf_flags & ED_FLAGS_NO_MULTI_BUFFERING)
1247 			sc->txb_cnt = 1;
1248 		else
1249 			sc->txb_cnt = 2;
1250 
1251 		sc->tx_page_start = ED_3COM_TX_PAGE_OFFSET_16BIT;
1252 		sc->rec_page_start = ED_3COM_RX_PAGE_OFFSET_16BIT;
1253 		sc->rec_page_stop =
1254 		    (memsize >> ED_PAGE_SHIFT) + ED_3COM_RX_PAGE_OFFSET_16BIT;
1255 		sc->mem_ring = sc->mem_start;
1256 	} else {
1257 		sc->txb_cnt = 1;
1258 		sc->tx_page_start = ED_3COM_TX_PAGE_OFFSET_8BIT;
1259 		sc->rec_page_start =
1260 		    ED_TXBUF_SIZE + ED_3COM_TX_PAGE_OFFSET_8BIT;
1261 		sc->rec_page_stop =
1262 		    (memsize >> ED_PAGE_SHIFT) + ED_3COM_TX_PAGE_OFFSET_8BIT;
1263 		sc->mem_ring =
1264 		    sc->mem_start + (ED_TXBUF_SIZE << ED_PAGE_SHIFT);
1265 	}
1266 
1267 	sc->isa16bit = isa16bit;
1268 
1269 	/*
1270 	 * Initialize GA page start/stop registers.  Probably only needed if
1271 	 * doing DMA, but what the Hell.
1272 	 */
1273 	bus_space_write_1(iot, ioh, asicbase + ED_3COM_PSTR, sc->rec_page_start);
1274 	bus_space_write_1(iot, ioh, asicbase + ED_3COM_PSPR, sc->rec_page_stop);
1275 
1276 	/* Set IRQ.  3c503 only allows a choice of irq 3-5 or 9. */
1277 	switch (ia->ia_irq) {
1278 	case 9:
1279 		bus_space_write_1(iot, ioh, asicbase + ED_3COM_IDCFR,
1280 		    ED_3COM_IDCFR_IRQ2);
1281 		break;
1282 	case 3:
1283 		bus_space_write_1(iot, ioh, asicbase + ED_3COM_IDCFR,
1284 		    ED_3COM_IDCFR_IRQ3);
1285 		break;
1286 	case 4:
1287 		bus_space_write_1(iot, ioh, asicbase + ED_3COM_IDCFR,
1288 		    ED_3COM_IDCFR_IRQ4);
1289 		break;
1290 	case 5:
1291 		bus_space_write_1(iot, ioh, asicbase + ED_3COM_IDCFR,
1292 		    ED_3COM_IDCFR_IRQ5);
1293 		break;
1294 	default:
1295 		printf("%s: invalid irq configuration (%d) must be 3-5 or 9 for 3c503\n",
1296 		    sc->sc_dev.dv_xname, ia->ia_irq);
1297 		goto out;
1298 	}
1299 
1300 	/*
1301 	 * Initialize GA configuration register.  Set bank and enable shared
1302 	 * mem.
1303 	 */
1304 	bus_space_write_1(iot, ioh, asicbase + ED_3COM_GACFR,
1305 	    ED_3COM_GACFR_RSEL | ED_3COM_GACFR_MBS0);
1306 
1307 	/*
1308 	 * Initialize "Vector Pointer" registers. These gawd-awful things are
1309 	 * compared to 20 bits of the address on ISA, and if they match, the
1310 	 * shared memory is disabled. We set them to 0xffff0...allegedly the
1311 	 * reset vector.
1312 	 */
1313 	bus_space_write_1(iot, ioh, asicbase + ED_3COM_VPTR2, 0xff);
1314 	bus_space_write_1(iot, ioh, asicbase + ED_3COM_VPTR1, 0xff);
1315 	bus_space_write_1(iot, ioh, asicbase + ED_3COM_VPTR0, 0x00);
1316 
1317 	/* Now zero memory and verify that it is clear. */
1318 	if (isa16bit) {
1319 		for (i = 0; i < memsize; i += 2)
1320 			bus_space_write_2(memt, memh, sc->mem_start + i, 0);
1321 	} else {
1322 		for (i = 0; i < memsize; ++i)
1323 			bus_space_write_1(memt, memh, sc->mem_start + i, 0);
1324 	}
1325 
1326 	memfail = 0;
1327 	if (isa16bit) {
1328 		for (i = 0; i < memsize; i += 2) {
1329 			if (bus_space_read_2(memt, memh, sc->mem_start + i)) {
1330 				memfail = 1;
1331 				break;
1332 			}
1333 		}
1334 	} else {
1335 		for (i = 0; i < memsize; ++i) {
1336 			if (bus_space_read_1(memt, memh, sc->mem_start + i)) {
1337 				memfail = 1;
1338 				break;
1339 			}
1340 		}
1341 	}
1342 
1343 	if (memfail) {
1344 		printf("%s: failed to clear shared memory at %x - "
1345 		    "check configuration\n",
1346 		    sc->sc_dev.dv_xname,
1347 		    (ia->ia_maddr + sc->mem_start + i));
1348 		goto out;
1349 	}
1350 
1351 	ia->ia_msize = memsize;
1352 	ia->ia_iosize = ED_3COM_IO_PORTS;
1353 
1354 	/*
1355 	 * XXX Sould always unmap, but we can't yet.
1356 	 * XXX Need to squish "indirect" first.
1357 	 */
1358 	sc->sc_iot = iot;
1359 	sc->sc_memt = memt;
1360 	sc->sc_ioh = ioh;
1361 	sc->sc_memh = memh;
1362 	return 1;
1363 
1364  out:
1365 	bus_space_unmap(memt, memh, memsize);
1366  err:
1367 	bus_space_unmap(iot, ioh, ED_3COM_IO_PORTS);
1368 	return 0;
1369 }
1370 
1371 /*
1372  * Probe and vendor-specific initialization routine for NE1000/2000 boards.
1373  */
1374 int
ed_find_Novell(sc,cf,ia)1375 ed_find_Novell(sc, cf, ia)
1376 	struct ed_softc *sc;
1377 	struct cfdata *cf;
1378 	struct isa_attach_args *ia;
1379 {
1380 	bus_space_tag_t iot;
1381 	bus_space_handle_t ioh;
1382 	u_int memsize, n;
1383 	u_char romdata[16], tmp;
1384 	static u_char test_pattern[32] = "THIS is A memory TEST pattern";
1385 	u_char test_buffer[32];
1386 	int asicbase, nicbase;
1387 
1388 	iot = ia->ia_iot;
1389 
1390 	if (bus_space_map(iot, ia->ia_iobase, ED_NOVELL_IO_PORTS, 0, &ioh))
1391 		return (0);
1392 
1393 	sc->asic_base = asicbase = ED_NOVELL_ASIC_OFFSET;
1394 	sc->nic_base = nicbase = ED_NOVELL_NIC_OFFSET;
1395 
1396 	/* XXX - do Novell-specific probe here */
1397 
1398 	/* Reset the board. */
1399 #ifdef GWETHER
1400 	bus_space_write_1(iot, ioh, asicbase + ED_NOVELL_RESET, 0);
1401 	delay(200);
1402 #endif /* GWETHER */
1403 	tmp = bus_space_read_1(iot, ioh, asicbase + ED_NOVELL_RESET);
1404 
1405 	/*
1406 	 * I don't know if this is necessary; probably cruft leftover from
1407 	 * Clarkson packet driver code. Doesn't do a thing on the boards I've
1408 	 * tested. -DG [note that a outb(0x84, 0) seems to work here, and is
1409 	 * non-invasive...but some boards don't seem to reset and I don't have
1410 	 * complete documentation on what the 'right' thing to do is...so we do
1411 	 * the invasive thing for now.  Yuck.]
1412 	 */
1413 	bus_space_write_1(iot, ioh, asicbase + ED_NOVELL_RESET, tmp);
1414 	delay(5000);
1415 
1416 	/*
1417 	 * This is needed because some NE clones apparently don't reset the NIC
1418 	 * properly (or the NIC chip doesn't reset fully on power-up)
1419 	 * XXX - this makes the probe invasive! ...Done against my better
1420 	 * judgement.  -DLG
1421 	 */
1422 	NIC_PUT(iot, ioh, nicbase, ED_P0_CR,
1423 	    ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STP);
1424 
1425 	delay(5000);
1426 
1427 	/* Make sure that we really have an 8390 based board. */
1428 	if (!ed_probe_generic8390(iot, ioh, nicbase))
1429 		goto out;
1430 
1431 	sc->vendor = ED_VENDOR_NOVELL;
1432 	sc->mem_shared = 0;
1433 	sc->cr_proto = ED_CR_RD2;
1434 	ia->ia_msize = 0;
1435 
1436 	/*
1437 	 * Test the ability to read and write to the NIC memory.  This has the
1438 	 * side affect of determining if this is an NE1000 or an NE2000.
1439 	 */
1440 
1441 	/*
1442 	 * This prevents packets from being stored in the NIC memory when the
1443 	 * readmem routine turns on the start bit in the CR.
1444 	 */
1445 	NIC_PUT(iot, ioh, nicbase, ED_P0_RCR, ED_RCR_MON);
1446 
1447 	/* Temporarily initialize DCR for byte operations. */
1448 	NIC_PUT(iot, ioh, nicbase, ED_P0_DCR, ED_DCR_FT1 | ED_DCR_LS);
1449 
1450 	NIC_PUT(iot, ioh, nicbase, ED_P0_PSTART, 8192 >> ED_PAGE_SHIFT);
1451 	NIC_PUT(iot, ioh, nicbase, ED_P0_PSTOP, 16384 >> ED_PAGE_SHIFT);
1452 
1453 	sc->isa16bit = 0;
1454 
1455 	/*
1456 	 * XXX indirect brokenness, used by ed_pio{read,write}mem()
1457 	 */
1458 	sc->sc_iot = iot;
1459 	sc->sc_ioh = ioh;
1460 
1461 	/*
1462 	 * Write a test pattern in byte mode.  If this fails, then there
1463 	 * probably isn't any memory at 8k - which likely means that the board
1464 	 * is an NE2000.
1465 	 */
1466 	ed_pio_writemem(sc, test_pattern, 8192, sizeof(test_pattern));
1467 	ed_pio_readmem(sc, 8192, test_buffer, sizeof(test_pattern));
1468 
1469 	if (bcmp(test_pattern, test_buffer, sizeof(test_pattern))) {
1470 		/* not an NE1000 - try NE2000 */
1471 
1472 		NIC_PUT(iot, ioh, nicbase, ED_P0_DCR,
1473 		    ED_DCR_WTS | ED_DCR_FT1 | ED_DCR_LS);
1474 		NIC_PUT(iot, ioh, nicbase, ED_P0_PSTART, 16384 >> ED_PAGE_SHIFT);
1475 		NIC_PUT(iot, ioh, nicbase, ED_P0_PSTOP, 32768 >> ED_PAGE_SHIFT);
1476 
1477 		sc->isa16bit = 1;
1478 
1479 		/*
1480 		 * Write a test pattern in word mode.  If this also fails, then
1481 		 * we don't know what this board is.
1482 		 */
1483 		ed_pio_writemem(sc, test_pattern, 16384, sizeof(test_pattern));
1484 		ed_pio_readmem(sc, 16384, test_buffer, sizeof(test_pattern));
1485 
1486 		if (bcmp(test_pattern, test_buffer, sizeof(test_pattern)))
1487 			goto out; /* not an NE2000 either */
1488 
1489 		sc->type = ED_TYPE_NE2000;
1490 		sc->type_str = "NE2000";
1491 	} else {
1492 		sc->type = ED_TYPE_NE1000;
1493 		sc->type_str = "NE1000";
1494 	}
1495 
1496 	if (ia->ia_irq == IRQUNK) {
1497 		printf("%s: %s does not have soft configuration\n",
1498 		    sc->sc_dev.dv_xname, sc->type_str);
1499 		goto out;
1500 	}
1501 
1502 	/* 8k of memory plus an additional 8k if 16-bit. */
1503 	memsize = 8192 + sc->isa16bit * 8192;
1504 
1505 #if 0 /* probably not useful - NE boards only come two ways */
1506 	/* Allow kernel config file overrides. */
1507 	if (ia->ia_msize)
1508 		memsize = ia->ia_msize;
1509 #endif
1510 
1511 	/* NIC memory doesn't start at zero on an NE board. */
1512 	/* The start address is tied to the bus width. */
1513 	sc->mem_start = (8192 + sc->isa16bit * 8192);
1514 	sc->tx_page_start = memsize >> ED_PAGE_SHIFT;
1515 
1516 #ifdef GWETHER
1517 	{
1518 		int x, i, mstart = 0;
1519 		char pbuf0[ED_PAGE_SIZE], pbuf[ED_PAGE_SIZE], tbuf[ED_PAGE_SIZE];
1520 
1521 		for (i = 0; i < ED_PAGE_SIZE; i++)
1522 			pbuf0[i] = 0;
1523 
1524 		/* Search for the start of RAM. */
1525 		for (x = 1; x < 256; x++) {
1526 			ed_pio_writemem(sc, pbuf0, x << ED_PAGE_SHIFT, ED_PAGE_SIZE);
1527 			ed_pio_readmem(sc, x << ED_PAGE_SHIFT, tbuf, ED_PAGE_SIZE);
1528 			if (!bcmp(pbuf0, tbuf, ED_PAGE_SIZE)) {
1529 				for (i = 0; i < ED_PAGE_SIZE; i++)
1530 					pbuf[i] = 255 - x;
1531 				ed_pio_writemem(sc, pbuf, x << ED_PAGE_SHIFT, ED_PAGE_SIZE);
1532 				ed_pio_readmem(sc, x << ED_PAGE_SHIFT, tbuf, ED_PAGE_SIZE);
1533 				if (!bcmp(pbuf, tbuf, ED_PAGE_SIZE)) {
1534 					mstart = x << ED_PAGE_SHIFT;
1535 					memsize = ED_PAGE_SIZE;
1536 					break;
1537 				}
1538 			}
1539 		}
1540 
1541 		if (mstart == 0) {
1542 			printf("%s: cannot find start of RAM\n",
1543 			    sc->sc_dev.dv_xname);
1544 			goto err;
1545 		}
1546 
1547 		/* Search for the end of RAM. */
1548 		for (++x; x < 256; x++) {
1549 			ed_pio_writemem(sc, pbuf0, x << ED_PAGE_SHIFT, ED_PAGE_SIZE);
1550 			ed_pio_readmem(sc, x << ED_PAGE_SHIFT, tbuf, ED_PAGE_SIZE);
1551 			if (!bcmp(pbuf0, tbuf, ED_PAGE_SIZE)) {
1552 				for (i = 0; i < ED_PAGE_SIZE; i++)
1553 					pbuf[i] = 255 - x;
1554 				ed_pio_writemem(sc, pbuf, x << ED_PAGE_SHIFT, ED_PAGE_SIZE);
1555 				ed_pio_readmem(sc, x << ED_PAGE_SHIFT, tbuf, ED_PAGE_SIZE);
1556 				if (!bcmp(pbuf, tbuf, ED_PAGE_SIZE))
1557 					memsize += ED_PAGE_SIZE;
1558 				else
1559 					break;
1560 			} else
1561 				break;
1562 		}
1563 
1564 		printf("%s: RAM start %x, size %d\n",
1565 		    sc->sc_dev.dv_xname, mstart, memsize);
1566 
1567 		sc->mem_start = (caddr_t)mstart;
1568 		sc->tx_page_start = mstart >> ED_PAGE_SHIFT;
1569 	}
1570 #endif /* GWETHER */
1571 
1572 	sc->mem_size = memsize;
1573 	sc->mem_end = sc->mem_start + memsize;
1574 
1575 	/*
1576 	 * Use one xmit buffer if < 16k, two buffers otherwise (if not told
1577 	 * otherwise).
1578 	 */
1579 	if ((memsize < 16384) || (cf->cf_flags & ED_FLAGS_NO_MULTI_BUFFERING))
1580 		sc->txb_cnt = 1;
1581 	else
1582 		sc->txb_cnt = 2;
1583 
1584 	sc->rec_page_start = sc->tx_page_start + sc->txb_cnt * ED_TXBUF_SIZE;
1585 	sc->rec_page_stop = sc->tx_page_start + (memsize >> ED_PAGE_SHIFT);
1586 
1587 	sc->mem_ring =
1588 	    sc->mem_start + ((sc->txb_cnt * ED_TXBUF_SIZE) << ED_PAGE_SHIFT);
1589 
1590 	ed_pio_readmem(sc, 0, romdata, 16);
1591 	for (n = 0; n < ETHER_ADDR_LEN; n++)
1592 		sc->sc_arpcom.ac_enaddr[n] = romdata[n*(sc->isa16bit+1)];
1593 
1594 #ifdef GWETHER
1595 	if (sc->arpcom.ac_enaddr[2] == 0x86)
1596 		sc->type_str = "Gateway AT";
1597 #endif /* GWETHER */
1598 
1599 	/* Clear any pending interrupts that might have occurred above. */
1600 	NIC_PUT(iot, ioh, nicbase, ED_P0_ISR, 0xff);
1601 
1602 	ia->ia_iosize = ED_NOVELL_IO_PORTS;
1603 
1604 	/*
1605 	 * XXX Sould always unmap, but we can't yet.
1606 	 * XXX Need to squish "indirect" first.
1607 	 */
1608 	sc->sc_iot = iot;
1609 	sc->sc_ioh = ioh;
1610 	/* sc_memh is not used by this driver */
1611 	return 1;
1612  out:
1613 	bus_space_unmap(iot, ioh, ED_NOVELL_IO_PORTS);
1614 
1615 	return 0;
1616 }
1617 
1618 /*
1619  * Install interface into kernel networking data structures.
1620  */
1621 void
edattach(parent,self,aux)1622 edattach(parent, self, aux)
1623 	struct device *parent, *self;
1624 	void *aux;
1625 {
1626 	bus_space_tag_t iot;
1627 	bus_space_handle_t ioh;
1628 	struct ed_softc *sc = (void *)self;
1629 	struct isa_attach_args *ia = aux;
1630 	struct cfdata *cf = sc->sc_dev.dv_cfdata;
1631 	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
1632 	int asicbase;
1633 
1634 	/*
1635 	 * XXX Should re-map io and mem, but can't
1636 	 * XXX until we squish "indirect" brokenness.
1637 	 */
1638 	iot = sc->sc_iot;		/* XXX */
1639 	ioh = sc->sc_ioh;		/* XXX */
1640 
1641 	asicbase = sc->asic_base;
1642 	sc->sc_delaybah = ia->ia_delaybah;
1643 
1644 	/* Set interface to stopped condition (reset). */
1645 	edstop(sc);
1646 
1647 	/* Initialize ifnet structure. */
1648 	bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
1649 	ifp->if_softc = sc;
1650 	ifp->if_start = edstart;
1651 	ifp->if_ioctl = edioctl;
1652 	ifp->if_watchdog = edwatchdog;
1653 	ifp->if_flags =
1654 	    IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
1655 	IFQ_SET_READY(&ifp->if_snd);
1656 
1657 	/*
1658 	 * Set default state for LINK0 flag (used to disable the transceiver
1659 	 * for AUI operation), based on compile-time config option.
1660 	 */
1661 	switch (sc->vendor) {
1662 	case ED_VENDOR_3COM:
1663 		if (cf->cf_flags & ED_FLAGS_DISABLE_TRANSCEIVER)
1664 			ifp->if_flags |= IFF_LINK0;
1665 		break;
1666 	case ED_VENDOR_WD_SMC:
1667 		if ((sc->type & ED_WD_SOFTCONFIG) == 0)
1668 			break;
1669 		if ((bus_space_read_1(iot, ioh, asicbase + ED_WD_IRR) &
1670 		    ED_WD_IRR_OUT2) == 0)
1671 			ifp->if_flags |= IFF_LINK0;
1672 		break;
1673 	}
1674 
1675 	/* Attach the interface. */
1676 	if ((sc->spec_flags & ED_REATTACH) == 0) {
1677 		if_attach(ifp);
1678 		ether_ifattach(ifp);
1679 	}
1680 	ether_ifattach(ifp);
1681 
1682 	/* Print additional info when attached. */
1683 	printf(": address %s, ", ether_sprintf(sc->sc_arpcom.ac_enaddr));
1684 
1685 	if (sc->type_str)
1686 		printf("type %s ", sc->type_str);
1687 	else
1688 		printf("type unknown (0x%x) ", sc->type);
1689 
1690 	printf("%s", sc->isa16bit ? "(16-bit)" : "(8-bit)");
1691 
1692 	switch (sc->vendor) {
1693 	case ED_VENDOR_WD_SMC:
1694 		if ((sc->type & ED_WD_SOFTCONFIG) == 0)
1695 			break;
1696 	case ED_VENDOR_3COM:
1697 		if (ifp->if_flags & IFF_LINK0)
1698 			printf(" aui");
1699 		else
1700 			printf(" bnc");
1701 		break;
1702 	}
1703 
1704 	printf("\n");
1705 
1706 	sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_EDGE,
1707 	    IPL_NET, edintr, sc, sc->sc_dev.dv_xname);
1708 	sc->sc_sh = shutdownhook_establish((void (*)(void *))edstop, sc);
1709 }
1710 
1711 /*
1712  * Reset interface.
1713  */
1714 void
edreset(sc)1715 edreset(sc)
1716 	struct ed_softc *sc;
1717 {
1718 	int s;
1719 
1720 	s = splnet();
1721 	edstop(sc);
1722 	edinit(sc);
1723 	splx(s);
1724 }
1725 
1726 /*
1727  * Take interface offline.
1728  */
1729 void
edstop(sc)1730 edstop(sc)
1731 	struct ed_softc *sc;
1732 {
1733 	bus_space_tag_t iot = sc->sc_iot;
1734 	bus_space_handle_t ioh = sc->sc_ioh;
1735 	int nicbase = sc->nic_base;
1736 	int n = 5000;
1737 
1738 	/* Stop everything on the interface, and select page 0 registers. */
1739 	NIC_PUT(iot, ioh, nicbase, ED_P0_CR,
1740 	    sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STP);
1741 
1742 	/*
1743 	 * Wait for interface to enter stopped state, but limit # of checks to
1744 	 * 'n' (about 5ms).  It shouldn't even take 5us on modern DS8390's, but
1745 	 * just in case it's an old one.
1746 	 */
1747 	while (((NIC_GET(iot, ioh, nicbase,
1748 	    ED_P0_ISR) & ED_ISR_RST) == 0) && --n);
1749 }
1750 
1751 /*
1752  * Device timeout/watchdog routine.  Entered if the device neglects to generate
1753  * an interrupt after a transmit has been started on it.
1754  */
1755 void
edwatchdog(ifp)1756 edwatchdog(ifp)
1757 	struct ifnet *ifp;
1758 {
1759 	struct ed_softc *sc = ifp->if_softc;
1760 
1761 	log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
1762 	++sc->sc_arpcom.ac_if.if_oerrors;
1763 
1764 	edreset(sc);
1765 }
1766 
1767 /*
1768  * Initialize device.
1769  */
1770 void
edinit(sc)1771 edinit(sc)
1772 	struct ed_softc *sc;
1773 {
1774 	bus_space_tag_t iot = sc->sc_iot;
1775 	bus_space_handle_t ioh = sc->sc_ioh;
1776 	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
1777 	int nicbase = sc->nic_base, asicbase = sc->asic_base;
1778 	int i;
1779 	u_int32_t mcaf[2];
1780 
1781 	/*
1782 	 * Initialize the NIC in the exact order outlined in the NS manual.
1783 	 * This init procedure is "mandatory"...don't change what or when
1784 	 * things happen.
1785 	 */
1786 
1787 	/* Reset transmitter flags. */
1788 	ifp->if_timer = 0;
1789 
1790 	sc->txb_inuse = 0;
1791 	sc->txb_new = 0;
1792 	sc->txb_next_tx = 0;
1793 
1794 	/* Set interface for page 0, remote DMA complete, stopped. */
1795 	NIC_PUT(iot, ioh, nicbase, ED_P0_CR,
1796 	    sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STP);
1797 
1798 	if (sc->isa16bit) {
1799 		/*
1800 		 * Set FIFO threshold to 8, No auto-init Remote DMA, byte
1801 		 * order=80x86, word-wide DMA xfers,
1802 		 */
1803 		NIC_PUT(iot, ioh, nicbase, ED_P0_DCR,
1804 		    ED_DCR_FT1 | ED_DCR_WTS | ED_DCR_LS);
1805 	} else {
1806 		/* Same as above, but byte-wide DMA xfers. */
1807 		NIC_PUT(iot, ioh, nicbase, ED_P0_DCR, ED_DCR_FT1 | ED_DCR_LS);
1808 	}
1809 
1810 	/* Clear remote byte count registers. */
1811 	NIC_PUT(iot, ioh, nicbase, ED_P0_RBCR0, 0);
1812 	NIC_PUT(iot, ioh, nicbase, ED_P0_RBCR1, 0);
1813 
1814 	/* Tell RCR to do nothing for now. */
1815 	NIC_PUT(iot, ioh, nicbase, ED_P0_RCR, ED_RCR_MON);
1816 
1817 	/* Place NIC in internal loopback mode. */
1818 	NIC_PUT(iot, ioh, nicbase, ED_P0_TCR, ED_TCR_LB0);
1819 
1820 	/* Set lower bits of byte addressable framing to 0. */
1821 	if (sc->is790)
1822 		NIC_PUT(iot, ioh, nicbase, 0x09, 0);
1823 
1824 	/* Initialize receive buffer ring. */
1825 	NIC_PUT(iot, ioh, nicbase, ED_P0_BNRY, sc->rec_page_start);
1826 	NIC_PUT(iot, ioh, nicbase, ED_P0_PSTART, sc->rec_page_start);
1827 	NIC_PUT(iot, ioh, nicbase, ED_P0_PSTOP, sc->rec_page_stop);
1828 
1829 	/*
1830 	 * Clear all interrupts.  A '1' in each bit position clears the
1831 	 * corresponding flag.
1832 	 */
1833 	NIC_PUT(iot, ioh, nicbase, ED_P0_ISR, 0xff);
1834 
1835 	/*
1836 	 * Enable the following interrupts: receive/transmit complete,
1837 	 * receive/transmit error, and Receiver OverWrite.
1838 	 *
1839 	 * Counter overflow and Remote DMA complete are *not* enabled.
1840 	 */
1841 	NIC_PUT(iot, ioh, nicbase, ED_P0_IMR,
1842 	    ED_IMR_PRXE | ED_IMR_PTXE | ED_IMR_RXEE | ED_IMR_TXEE |
1843 	    ED_IMR_OVWE);
1844 
1845 	/* Program command register for page 1. */
1846 	NIC_PUT(iot, ioh, nicbase, ED_P0_CR,
1847 	    sc->cr_proto | ED_CR_PAGE_1 | ED_CR_STP);
1848 
1849 	/* Copy out our station address. */
1850 	for (i = 0; i < ETHER_ADDR_LEN; ++i)
1851 		NIC_PUT(iot, ioh, nicbase, ED_P1_PAR0 + i,
1852 		    sc->sc_arpcom.ac_enaddr[i]);
1853 
1854 	/* Set multicast filter on chip. */
1855 	ed_getmcaf(&sc->sc_arpcom, mcaf);
1856 	for (i = 0; i < 8; i++)
1857 		NIC_PUT(iot, ioh, nicbase, ED_P1_MAR0 + i,
1858 		    ((u_char *)mcaf)[i]);
1859 
1860 	/*
1861 	 * Set current page pointer to one page after the boundary pointer, as
1862 	 * recommended in the National manual.
1863 	 */
1864 	sc->next_packet = sc->rec_page_start + 1;
1865 	NIC_PUT(iot, ioh, nicbase, ED_P1_CURR, sc->next_packet);
1866 
1867 	/* Program command register for page 0. */
1868 	NIC_PUT(iot, ioh, nicbase, ED_P1_CR,
1869 	    sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STP);
1870 
1871 	i = ED_RCR_AB | ED_RCR_AM;
1872 	if (ifp->if_flags & IFF_PROMISC) {
1873 		/*
1874 		 * Set promiscuous mode.  Multicast filter was set earlier so
1875 		 * that we should receive all multicast packets.
1876 		 */
1877 		i |= ED_RCR_PRO | ED_RCR_AR | ED_RCR_SEP;
1878 	}
1879 	NIC_PUT(iot, ioh, nicbase, ED_P0_RCR, i);
1880 
1881 	/* Take interface out of loopback. */
1882 	NIC_PUT(iot, ioh, nicbase, ED_P0_TCR, 0);
1883 
1884 	/*
1885 	 * If this is a 3Com board, the transceiver must be software enabled
1886 	 * (there is no settable hardware default).
1887 	 */
1888 	switch (sc->vendor) {
1889 		u_char x;
1890 	case ED_VENDOR_3COM:
1891 		if (ifp->if_flags & IFF_LINK0)
1892 			bus_space_write_1(iot, ioh, asicbase + ED_3COM_CR, 0);
1893 		else
1894 			bus_space_write_1(iot, ioh, asicbase + ED_3COM_CR,
1895 			    ED_3COM_CR_XSEL);
1896 		break;
1897 	case ED_VENDOR_WD_SMC:
1898 		if ((sc->type & ED_WD_SOFTCONFIG) == 0)
1899 			break;
1900 		x = bus_space_read_1(iot, ioh, asicbase + ED_WD_IRR);
1901 		if (ifp->if_flags & IFF_LINK0)
1902 			x &= ~ED_WD_IRR_OUT2;
1903 		else
1904 			x |= ED_WD_IRR_OUT2;
1905 		bus_space_write_1(iot, ioh, asicbase + ED_WD_IRR, x);
1906 		break;
1907 	}
1908 
1909 	/* Fire up the interface. */
1910 	NIC_PUT(iot, ioh, nicbase, ED_P0_CR,
1911 	    sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STA);
1912 
1913 	/* Set 'running' flag, and clear output active flag. */
1914 	ifp->if_flags |= IFF_RUNNING;
1915 	ifp->if_flags &= ~IFF_OACTIVE;
1916 
1917 	/* ...and attempt to start output. */
1918 	edstart(ifp);
1919 }
1920 
1921 /*
1922  * This routine actually starts the transmission on the interface.
1923  */
1924 static __inline void
ed_xmit(sc)1925 ed_xmit(sc)
1926 	struct ed_softc *sc;
1927 {
1928 	bus_space_tag_t iot = sc->sc_iot;
1929 	bus_space_handle_t ioh = sc->sc_ioh;
1930 	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
1931 	int nicbase = sc->nic_base;
1932 	u_int16_t len;
1933 
1934 	len = sc->txb_len[sc->txb_next_tx];
1935 
1936 	/* Set NIC for page 0 register access. */
1937 	NIC_PUT(iot, ioh, nicbase, ED_P0_CR,
1938 	    sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STA);
1939 
1940 	/* Set TX buffer start page. */
1941 	NIC_PUT(iot, ioh, nicbase, ED_P0_TPSR, sc->tx_page_start +
1942 	    sc->txb_next_tx * ED_TXBUF_SIZE);
1943 
1944 	/* Set TX length. */
1945 	NIC_PUT(iot, ioh, nicbase, ED_P0_TBCR0, len);
1946 	NIC_PUT(iot, ioh, nicbase, ED_P0_TBCR1, len >> 8);
1947 
1948 	/* Set page 0, remote DMA complete, transmit packet, and *start*. */
1949 	NIC_PUT(iot, ioh, nicbase, ED_P0_CR,
1950 	    sc->cr_proto | ED_CR_PAGE_0 | ED_CR_TXP | ED_CR_STA);
1951 
1952 	/* Point to next transmit buffer slot and wrap if necessary. */
1953 	sc->txb_next_tx++;
1954 	if (sc->txb_next_tx == sc->txb_cnt)
1955 		sc->txb_next_tx = 0;
1956 
1957 	/* Set a timer just in case we never hear from the board again. */
1958 	ifp->if_timer = 2;
1959 }
1960 
1961 /*
1962  * Start output on interface.
1963  * We make two assumptions here:
1964  *  1) that the current priority is set to splnet _before_ this code
1965  *     is called *and* is returned to the appropriate priority after
1966  *     return
1967  *  2) that the IFF_OACTIVE flag is checked before this code is called
1968  *     (i.e. that the output part of the interface is idle)
1969  */
1970 void
edstart(ifp)1971 edstart(ifp)
1972 	struct ifnet *ifp;
1973 {
1974 	struct ed_softc *sc = ifp->if_softc;
1975 	bus_space_tag_t iot = sc->sc_iot;
1976 	bus_space_handle_t ioh = sc->sc_ioh;
1977 	struct mbuf *m0, *m;
1978 	int buffer;
1979 	int asicbase = sc->asic_base;
1980 	int len;
1981 
1982 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
1983 		return;
1984 
1985 outloop:
1986 	/* See if there is room to put another packet in the buffer. */
1987 	if (sc->txb_inuse == sc->txb_cnt) {
1988 		/* No room.  Indicate this to the outside world and exit. */
1989 		ifp->if_flags |= IFF_OACTIVE;
1990 		return;
1991 	}
1992 
1993 	IFQ_DEQUEUE(&ifp->if_snd, m0);
1994 	if (m0 == 0)
1995 		return;
1996 
1997 	/* We need to use m->m_pkthdr.len, so require the header */
1998 	if ((m0->m_flags & M_PKTHDR) == 0)
1999 		panic("edstart: no header mbuf");
2000 
2001 #if NBPFILTER > 0
2002 	/* Tap off here if there is a BPF listener. */
2003 	if (ifp->if_bpf)
2004 		bpf_mtap(ifp->if_bpf, m0);
2005 #endif
2006 
2007 	/* txb_new points to next open buffer slot. */
2008 	buffer = sc->mem_start +
2009 	    ((sc->txb_new * ED_TXBUF_SIZE) << ED_PAGE_SHIFT);
2010 
2011 	if (sc->mem_shared) {
2012 		/* Special case setup for 16 bit boards... */
2013 		switch (sc->vendor) {
2014 		/*
2015 		 * For 16bit 3Com boards (which have 16k of memory), we
2016 		 * have the xmit buffers in a different page of memory
2017 		 * ('page 0') - so change pages.
2018 		 */
2019 		case ED_VENDOR_3COM:
2020 			if (sc->isa16bit)
2021 				bus_space_write_1(iot, ioh,
2022 				    asicbase + ED_3COM_GACFR,
2023 				    ED_3COM_GACFR_RSEL);
2024 			break;
2025 		/*
2026 		 * Enable 16bit access to shared memory on WD/SMC
2027 		 * boards.
2028 		 */
2029 		case ED_VENDOR_WD_SMC:
2030 			if (sc->isa16bit)
2031 				bus_space_write_1(iot, ioh, asicbase + ED_WD_LAAR,
2032 				    sc->wd_laar_proto | ED_WD_LAAR_M16EN);
2033 			bus_space_write_1(iot, ioh, asicbase + ED_WD_MSR,
2034 			    sc->wd_msr_proto | ED_WD_MSR_MENB);
2035 			(void) bus_space_read_1(iot, sc->sc_delaybah, 0);
2036 			(void) bus_space_read_1(iot, sc->sc_delaybah, 0);
2037 			break;
2038 		}
2039 
2040 		for (m = m0; m != 0; m = m->m_next) {
2041 			ed_shared_writemem(sc, mtod(m, caddr_t), buffer,
2042 			    m->m_len);
2043 			buffer += m->m_len;
2044 		}
2045 		len = m0->m_pkthdr.len;
2046 
2047 		/* Restore previous shared memory access. */
2048 		switch (sc->vendor) {
2049 		case ED_VENDOR_3COM:
2050 			if (sc->isa16bit)
2051 				bus_space_write_1(iot, ioh,
2052 				    asicbase + ED_3COM_GACFR,
2053 				    ED_3COM_GACFR_RSEL | ED_3COM_GACFR_MBS0);
2054 			break;
2055 		case ED_VENDOR_WD_SMC:
2056 			bus_space_write_1(iot, ioh, asicbase + ED_WD_MSR,
2057 			    sc->wd_msr_proto);
2058 			if (sc->isa16bit)
2059 				bus_space_write_1(iot, ioh, asicbase + ED_WD_LAAR,
2060 				    sc->wd_laar_proto);
2061 			(void) bus_space_read_1(iot, sc->sc_delaybah, 0);
2062 			(void) bus_space_read_1(iot, sc->sc_delaybah, 0);
2063 			break;
2064 		}
2065 	} else
2066 		len = ed_pio_write_mbufs(sc, m0, (u_int16_t)buffer);
2067 
2068 	m_freem(m0);
2069 	sc->txb_len[sc->txb_new] = max(len, ETHER_MIN_LEN);
2070 
2071 	/* Start the first packet transmitting. */
2072 	if (sc->txb_inuse == 0)
2073 		ed_xmit(sc);
2074 
2075 	/* Point to next buffer slot and wrap if necessary. */
2076 	if (++sc->txb_new == sc->txb_cnt)
2077 		sc->txb_new = 0;
2078 	sc->txb_inuse++;
2079 
2080 	/* Loop back to the top to possibly buffer more packets. */
2081 	goto outloop;
2082 }
2083 
2084 /*
2085  * Ethernet interface receiver interrupt.
2086  */
2087 static __inline void
ed_rint(sc)2088 ed_rint(sc)
2089 	struct ed_softc *sc;
2090 {
2091 	bus_space_tag_t iot = sc->sc_iot;
2092 	bus_space_handle_t ioh = sc->sc_ioh;
2093 	int nicbase = sc->nic_base;
2094 	u_int8_t boundary, current;
2095 	u_int16_t len;
2096 	u_int8_t nlen;
2097 	u_int8_t next_packet;		/* pointer to next packet */
2098 	u_int16_t count;		/* bytes in packet (length + 4) */
2099 	u_int8_t packet_hdr[ED_RING_HDRSZ];
2100 	int packet_ptr;
2101 
2102 loop:
2103 	/* Set NIC to page 1 registers to get 'current' pointer. */
2104 	NIC_PUT(iot, ioh, nicbase, ED_P0_CR,
2105 	    sc->cr_proto | ED_CR_PAGE_1 | ED_CR_STA);
2106 
2107 	/*
2108 	 * 'sc->next_packet' is the logical beginning of the ring-buffer - i.e.
2109 	 * it points to where new data has been buffered.  The 'CURR' (current)
2110 	 * register points to the logical end of the ring-buffer - i.e. it
2111 	 * points to where additional new data will be added.  We loop here
2112 	 * until the logical beginning equals the logical end (or in other
2113 	 * words, until the ring-buffer is empty).
2114 	 */
2115 	current = NIC_GET(iot, ioh, nicbase, ED_P1_CURR);
2116 	if (sc->next_packet == current)
2117 		return;
2118 
2119 	/* Set NIC to page 0 registers to update boundary register. */
2120 	NIC_PUT(iot, ioh, nicbase, ED_P1_CR,
2121 	    sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STA);
2122 
2123 	do {
2124 		/* Get pointer to this buffer's header structure. */
2125 		packet_ptr = sc->mem_ring +
2126 		    ((sc->next_packet - sc->rec_page_start) << ED_PAGE_SHIFT);
2127 
2128 		/*
2129 		 * The byte count includes a 4 byte header that was added by
2130 		 * the NIC.
2131 		 */
2132 		if (sc->mem_shared)
2133 			ed_shared_readmem(sc, packet_ptr, packet_hdr,
2134 			    sizeof(packet_hdr));
2135 		else
2136 			ed_pio_readmem(sc, (u_int16_t)packet_ptr, packet_hdr,
2137 			    sizeof(packet_hdr));
2138 		next_packet = packet_hdr[ED_RING_NEXT_PACKET];
2139 		len = count = packet_hdr[ED_RING_COUNT] +
2140 		    256 * packet_hdr[ED_RING_COUNT + 1];
2141 
2142 		/*
2143 		 * Try do deal with old, buggy chips that sometimes duplicate
2144 		 * the low byte of the length into the high byte.  We do this
2145 		 * by simply ignoring the high byte of the length and always
2146 		 * recalculating it.
2147 		 *
2148 		 * NOTE: sc->next_packet is pointing at the current packet.
2149 		 */
2150 		if (next_packet >= sc->next_packet)
2151 			nlen = (next_packet - sc->next_packet);
2152 		else
2153 			nlen = ((next_packet - sc->rec_page_start) +
2154 				(sc->rec_page_stop - sc->next_packet));
2155 		--nlen;
2156 		if ((len & ED_PAGE_MASK) + sizeof(packet_hdr) > ED_PAGE_SIZE)
2157 			--nlen;
2158 		len = (len & ED_PAGE_MASK) | (nlen << ED_PAGE_SHIFT);
2159 #ifdef DIAGNOSTIC
2160 		if (len != count) {
2161 			printf("%s: length does not match next packet pointer\n",
2162 			    sc->sc_dev.dv_xname);
2163 			printf("%s: len %04x nlen %04x start %02x first %02x curr %02x next %02x stop %02x\n",
2164 			    sc->sc_dev.dv_xname, count, len,
2165 			    sc->rec_page_start, sc->next_packet, current,
2166 			    next_packet, sc->rec_page_stop);
2167 		}
2168 #endif
2169 
2170 		/*
2171 		 * Be fairly liberal about what we allow as a "reasonable"
2172 		 * length so that a [crufty] packet will make it to BPF (and
2173 		 * can thus be analyzed).  Note that all that is really
2174 		 * important is that we have a length that will fit into one
2175 		 * mbuf cluster or less; the upper layer protocols can then
2176 		 * figure out the length from their own length field(s).
2177 		 */
2178 		if (len <= MCLBYTES &&
2179 		    next_packet >= sc->rec_page_start &&
2180 		    next_packet < sc->rec_page_stop) {
2181 			/* Go get packet. */
2182 			edread(sc, packet_ptr + ED_RING_HDRSZ,
2183 			    len - ED_RING_HDRSZ);
2184 		} else {
2185 			/* Really BAD.  The ring pointers are corrupted. */
2186 			log(LOG_ERR,
2187 			    "%s: NIC memory corrupt - invalid packet length %d\n",
2188 			    sc->sc_dev.dv_xname, len);
2189 			++sc->sc_arpcom.ac_if.if_ierrors;
2190 			edreset(sc);
2191 			return;
2192 		}
2193 
2194 		/* Update next packet pointer. */
2195 		sc->next_packet = next_packet;
2196 
2197 		/*
2198 		 * Update NIC boundary pointer - being careful to keep it one
2199 		 * buffer behind (as recommended by NS databook).
2200 		 */
2201 		boundary = sc->next_packet - 1;
2202 		if (boundary < sc->rec_page_start)
2203 			boundary = sc->rec_page_stop - 1;
2204 		NIC_PUT(iot, ioh, nicbase, ED_P0_BNRY, boundary);
2205 	} while (sc->next_packet != current);
2206 
2207 	goto loop;
2208 }
2209 
2210 /* Ethernet interface interrupt processor. */
2211 int
edintr(arg)2212 edintr(arg)
2213 	void *arg;
2214 {
2215 	struct ed_softc *sc = arg;
2216 	bus_space_tag_t iot = sc->sc_iot;
2217 	bus_space_handle_t ioh = sc->sc_ioh;
2218 	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
2219 	int nicbase = sc->nic_base, asicbase = sc->asic_base;
2220 	u_char isr;
2221 
2222 	/* Set NIC to page 0 registers. */
2223 	NIC_PUT(iot, ioh, nicbase, ED_P0_CR,
2224 	    sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STA);
2225 
2226 	isr = NIC_GET(iot, ioh, nicbase, ED_P0_ISR);
2227 	if (!isr)
2228 		return (0);
2229 
2230 	/* Loop until there are no more new interrupts. */
2231 	for (;;) {
2232 		/*
2233 		 * Reset all the bits that we are 'acknowledging' by writing a
2234 		 * '1' to each bit position that was set.
2235 		 * (Writing a '1' *clears* the bit.)
2236 		 */
2237 		NIC_PUT(iot, ioh, nicbase, ED_P0_ISR, isr);
2238 
2239 		/*
2240 		 * Handle transmitter interrupts.  Handle these first because
2241 		 * the receiver will reset the board under some conditions.
2242 		 */
2243 		if (isr & (ED_ISR_PTX | ED_ISR_TXE)) {
2244 			u_char collisions = NIC_GET(iot, ioh, nicbase,
2245 			    ED_P0_NCR) & 0x0f;
2246 
2247 			/*
2248 			 * Check for transmit error.  If a TX completed with an
2249 			 * error, we end up throwing the packet away.  Really
2250 			 * the only error that is possible is excessive
2251 			 * collisions, and in this case it is best to allow the
2252 			 * automatic mechanisms of TCP to backoff the flow.  Of
2253 			 * course, with UDP we're screwed, but this is expected
2254 			 * when a network is heavily loaded.
2255 			 */
2256 			(void) NIC_GET(iot, ioh, nicbase, ED_P0_TSR);
2257 			if (isr & ED_ISR_TXE) {
2258 				/*
2259 				 * Excessive collisions (16).
2260 				 */
2261 				if ((NIC_GET(iot, ioh, nicbase, ED_P0_TSR) &
2262 				    ED_TSR_ABT) && (collisions == 0)) {
2263 					/*
2264 					 * When collisions total 16, the P0_NCR
2265 					 * will indicate 0, and the TSR_ABT is
2266 					 * set.
2267 					 */
2268 					collisions = 16;
2269 				}
2270 
2271 				/* Update output errors counter. */
2272 				++ifp->if_oerrors;
2273 			} else {
2274 				/*
2275 				 * Update total number of successfully
2276 				 * transmitted packets.
2277 				 */
2278 				++ifp->if_opackets;
2279 			}
2280 
2281 			/* Done with the buffer. */
2282 			sc->txb_inuse--;
2283 
2284 			/* Clear watchdog timer. */
2285 			ifp->if_timer = 0;
2286 			ifp->if_flags &= ~IFF_OACTIVE;
2287 
2288 			/*
2289 			 * Add in total number of collisions on last
2290 			 * transmission.
2291 			 */
2292 			ifp->if_collisions += collisions;
2293 
2294 			/*
2295 			 * Decrement buffer in-use count if not zero (can only
2296 			 * be zero if a transmitter interrupt occurred while not
2297 			 * actually transmitting).
2298 			 * If data is ready to transmit, start it transmitting,
2299 			 * otherwise defer until after handling receiver.
2300 			 */
2301 			if (sc->txb_inuse > 0)
2302 				ed_xmit(sc);
2303 		}
2304 
2305 		/* Handle receiver interrupts. */
2306 		if (isr & (ED_ISR_PRX | ED_ISR_RXE | ED_ISR_OVW)) {
2307 			/*
2308 			 * Overwrite warning.  In order to make sure that a
2309 			 * lockup of the local DMA hasn't occurred, we reset
2310 			 * and re-init the NIC.  The NSC manual suggests only a
2311 			 * partial reset/re-init is necessary - but some chips
2312 			 * seem to want more.  The DMA lockup has been seen
2313 			 * only with early rev chips - Methinks this bug was
2314 			 * fixed in later revs.  -DG
2315 			 */
2316 			if (isr & ED_ISR_OVW) {
2317 				++ifp->if_ierrors;
2318 #ifdef DIAGNOSTIC
2319 				log(LOG_WARNING,
2320 				    "%s: warning - receiver ring buffer overrun\n",
2321 				    sc->sc_dev.dv_xname);
2322 #endif
2323 				/* Stop/reset/re-init NIC. */
2324 				edreset(sc);
2325 			} else {
2326 				/*
2327 				 * Receiver Error.  One or more of: CRC error,
2328 				 * frame alignment error FIFO overrun, or
2329 				 * missed packet.
2330 				 */
2331 				if (isr & ED_ISR_RXE) {
2332 					++ifp->if_ierrors;
2333 #ifdef ED_DEBUG
2334 					printf("%s: receive error %x\n",
2335 					    sc->sc_dev.dv_xname,
2336 					    NIC_GET(iot,ioh,nicbase,ED_P0_RSR));
2337 #endif
2338 				}
2339 
2340 				/*
2341 				 * Go get the packet(s).
2342 				 * XXX - Doing this on an error is dubious
2343 				 * because there shouldn't be any data to get
2344 				 * (we've configured the interface to not
2345 				 * accept packets with errors).
2346 				 */
2347 
2348 				/*
2349 				 * Enable 16bit access to shared memory first
2350 				 * on WD/SMC boards.
2351 				 */
2352 				if (sc->vendor == ED_VENDOR_WD_SMC) {
2353 					if (sc->isa16bit)
2354 						bus_space_write_1(iot, ioh,
2355 						    asicbase + ED_WD_LAAR,
2356 						    sc->wd_laar_proto |
2357 						    ED_WD_LAAR_M16EN);
2358 					bus_space_write_1(iot, ioh,
2359 					    asicbase + ED_WD_MSR,
2360 					    sc->wd_msr_proto | ED_WD_MSR_MENB);
2361 					(void) bus_space_read_1(iot,
2362 					    sc->sc_delaybah, 0);
2363 					(void) bus_space_read_1(iot,
2364 					    sc->sc_delaybah, 0);
2365 				}
2366 
2367 				ed_rint(sc);
2368 
2369 				/* Disable 16-bit access. */
2370 				if (sc->vendor == ED_VENDOR_WD_SMC) {
2371 					bus_space_write_1(iot, ioh,
2372 					    asicbase + ED_WD_MSR,
2373 					    sc->wd_msr_proto);
2374 					if (sc->isa16bit)
2375 						bus_space_write_1(iot, ioh,
2376 						    asicbase + ED_WD_LAAR,
2377 						    sc->wd_laar_proto);
2378 					(void) bus_space_read_1(iot,
2379 					    sc->sc_delaybah, 0);
2380 					(void) bus_space_read_1(iot,
2381 					    sc->sc_delaybah, 0);
2382 				}
2383 			}
2384 		}
2385 
2386 		/*
2387 		 * If it looks like the transmitter can take more data,	attempt
2388 		 * to start output on the interface.  This is done after
2389 		 * handling the receiver to give the receiver priority.
2390 		 */
2391 		edstart(ifp);
2392 
2393 		/*
2394 		 * Return NIC CR to standard state: page 0, remote DMA
2395 		 * complete, start (toggling the TXP bit off, even if was just
2396 		 * set in the transmit routine, is *okay* - it is 'edge'
2397 		 * triggered from low to high).
2398 		 */
2399 		NIC_PUT(iot, ioh, nicbase, ED_P0_CR,
2400 		    sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STA);
2401 
2402 		/*
2403 		 * If the Network Talley Counters overflow, read them to reset
2404 		 * them.  It appears that old 8390's won't clear the ISR flag
2405 		 * otherwise - resulting in an infinite loop.
2406 		 */
2407 		if (isr & ED_ISR_CNT) {
2408 			(void) NIC_GET(iot, ioh, nicbase, ED_P0_CNTR0);
2409 			(void) NIC_GET(iot, ioh, nicbase, ED_P0_CNTR1);
2410 			(void) NIC_GET(iot, ioh, nicbase, ED_P0_CNTR2);
2411 		}
2412 
2413 		isr = NIC_GET(iot, ioh, nicbase, ED_P0_ISR);
2414 		if (!isr)
2415 			return (1);
2416 	}
2417 }
2418 
2419 /*
2420  * Process an ioctl request.  This code needs some work - it looks pretty ugly.
2421  */
2422 int
edioctl(ifp,cmd,data)2423 edioctl(ifp, cmd, data)
2424 	register struct ifnet *ifp;
2425 	u_long cmd;
2426 	caddr_t data;
2427 {
2428 	struct ed_softc *sc = ifp->if_softc;
2429 	register struct ifaddr *ifa = (struct ifaddr *)data;
2430 	struct ifreq *ifr = (struct ifreq *)data;
2431 	int s, error = 0;
2432 
2433 	s = splnet();
2434 	if ((sc->spec_flags & ED_NOTPRESENT) != 0) {
2435 		if_down(ifp);
2436 		printf("%s: device offline\n", sc->sc_dev.dv_xname);
2437 		splx(s);
2438 		return ENXIO;		/* may be ignored, oh well. */
2439 	}
2440 
2441 	if ((error = ether_ioctl(ifp, &sc->sc_arpcom, cmd, data)) > 0) {
2442 		splx(s);
2443 		return error;
2444 	}
2445 
2446 	switch (cmd) {
2447 
2448 	case SIOCSIFADDR:
2449 		ifp->if_flags |= IFF_UP;
2450 
2451 		switch (ifa->ifa_addr->sa_family) {
2452 #ifdef INET
2453 		case AF_INET:
2454 			edinit(sc);
2455 			arp_ifinit(&sc->sc_arpcom, ifa);
2456 			break;
2457 #endif
2458 		default:
2459 			edinit(sc);
2460 			break;
2461 		}
2462 		break;
2463 
2464 	case SIOCSIFFLAGS:
2465 		if ((ifp->if_flags & IFF_UP) == 0 &&
2466 		    (ifp->if_flags & IFF_RUNNING) != 0) {
2467 			/*
2468 			 * If interface is marked down and it is running, then
2469 			 * stop it.
2470 			 */
2471 			edstop(sc);
2472 			ifp->if_flags &= ~IFF_RUNNING;
2473 		} else if ((ifp->if_flags & IFF_UP) != 0 &&
2474 		    	   (ifp->if_flags & IFF_RUNNING) == 0) {
2475 			/*
2476 			 * If interface is marked up and it is stopped, then
2477 			 * start it.
2478 			 */
2479 			edinit(sc);
2480 		} else {
2481 			/*
2482 			 * Reset the interface to pick up changes in any other
2483 			 * flags that affect hardware registers.
2484 			 */
2485 			edstop(sc);
2486 			edinit(sc);
2487 		}
2488 		break;
2489 
2490 	case SIOCADDMULTI:
2491 	case SIOCDELMULTI:
2492 		/* Update our multicast list. */
2493 		error = (cmd == SIOCADDMULTI) ?
2494 		    ether_addmulti(ifr, &sc->sc_arpcom) :
2495 		    ether_delmulti(ifr, &sc->sc_arpcom);
2496 
2497 		if (error == ENETRESET) {
2498 			/*
2499 			 * Multicast list has changed; set the hardware filter
2500 			 * accordingly.
2501 			 */
2502 			edstop(sc); /* XXX for ds_setmcaf? */
2503 			edinit(sc);
2504 			error = 0;
2505 		}
2506 		break;
2507 
2508 	default:
2509 		error = EINVAL;
2510 		break;
2511 	}
2512 
2513 	splx(s);
2514 	return (error);
2515 }
2516 
2517 /*
2518  * Retreive packet from shared memory and send to the next level up via
2519  * ether_input().  If there is a BPF listener, give a copy to BPF, too.
2520  */
2521 void
edread(sc,buf,len)2522 edread(sc, buf, len)
2523 	struct ed_softc *sc;
2524 	int buf, len;
2525 {
2526 	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
2527 	struct mbuf *m;
2528 
2529 	/* Pull packet off interface. */
2530 	m = edget(sc, buf, len);
2531 	if (m == 0) {
2532 		ifp->if_ierrors++;
2533 		return;
2534 	}
2535 
2536 	ifp->if_ipackets++;
2537 
2538 #if NBPFILTER > 0
2539 	/*
2540 	 * Check if there's a BPF listener on this interface.
2541 	 * If so, hand off the raw packet to BPF.
2542 	 */
2543 	if (ifp->if_bpf)
2544 		bpf_mtap(ifp->if_bpf, m);
2545 #endif
2546 
2547 	ether_input_mbuf(ifp, m);
2548 }
2549 
2550 /*
2551  * Supporting routines.
2552  */
2553 
2554 /*
2555  * Given a NIC memory source address and a host memory destination address,
2556  * copy 'amount' from NIC to host using Programmed I/O.  The 'amount' is
2557  * rounded up to a word - okay as long as mbufs are word sized.
2558  * This routine is currently Novell-specific.
2559  */
2560 void
ed_pio_readmem(sc,src,dst,amount)2561 ed_pio_readmem(sc, src, dst, amount)
2562 	struct ed_softc *sc;
2563 	u_int16_t src;
2564 	caddr_t dst;
2565 	u_int16_t amount;
2566 {
2567 	bus_space_tag_t iot = sc->sc_iot;
2568 	bus_space_handle_t ioh = sc->sc_ioh;
2569 	int nicbase = sc->nic_base;
2570 
2571 	/* Select page 0 registers. */
2572 	NIC_PUT(iot, ioh, nicbase, ED_P0_CR,
2573 	    ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STA);
2574 
2575 	/* Round up to a word. */
2576 	if (amount & 1)
2577 		++amount;
2578 
2579 	/* Set up DMA byte count. */
2580 	NIC_PUT(iot, ioh, nicbase, ED_P0_RBCR0, amount);
2581 	NIC_PUT(iot, ioh, nicbase, ED_P0_RBCR1, amount >> 8);
2582 
2583 	/* Set up source address in NIC mem. */
2584 	NIC_PUT(iot, ioh, nicbase, ED_P0_RSAR0, src);
2585 	NIC_PUT(iot, ioh, nicbase, ED_P0_RSAR1, src >> 8);
2586 
2587 	NIC_PUT(iot, ioh, nicbase, ED_P0_CR,
2588 	    ED_CR_RD0 | ED_CR_PAGE_0 | ED_CR_STA);
2589 
2590 	if (sc->isa16bit)
2591 		bus_space_read_raw_multi_2(iot, ioh,
2592 		    sc->asic_base + ED_NOVELL_DATA, dst, amount);
2593 	else
2594 		bus_space_read_multi_1(iot, ioh,
2595 		    sc->asic_base + ED_NOVELL_DATA, dst, amount);
2596 }
2597 
2598 /*
2599  * Stripped down routine for writing a linear buffer to NIC memory.  Only used
2600  * in the probe routine to test the memory.  'len' must be even.
2601  */
2602 void
ed_pio_writemem(sc,src,dst,len)2603 ed_pio_writemem(sc, src, dst, len)
2604 	struct ed_softc *sc;
2605 	caddr_t src;
2606 	u_int16_t dst;
2607 	u_int16_t len;
2608 {
2609 	bus_space_tag_t iot = sc->sc_iot;
2610 	bus_space_handle_t ioh = sc->sc_ioh;
2611 	int nicbase = sc->nic_base;
2612 	int maxwait = 100; /* about 120us */
2613 
2614 	/* Select page 0 registers. */
2615 	NIC_PUT(iot, ioh, nicbase, ED_P0_CR,
2616 	    ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STA);
2617 
2618 	/* Reset remote DMA complete flag. */
2619 	NIC_PUT(iot, ioh, nicbase, ED_P0_ISR, ED_ISR_RDC);
2620 
2621 	/* Set up DMA byte count. */
2622 	NIC_PUT(iot, ioh, nicbase, ED_P0_RBCR0, len);
2623 	NIC_PUT(iot, ioh, nicbase, ED_P0_RBCR1, len >> 8);
2624 
2625 	/* Set up destination address in NIC mem. */
2626 	NIC_PUT(iot, ioh, nicbase, ED_P0_RSAR0, dst);
2627 	NIC_PUT(iot, ioh, nicbase, ED_P0_RSAR1, dst >> 8);
2628 
2629 	/* Set remote DMA write. */
2630 	NIC_PUT(iot, ioh, nicbase, ED_P0_CR,
2631 	    ED_CR_RD1 | ED_CR_PAGE_0 | ED_CR_STA);
2632 
2633 	if (sc->isa16bit)
2634 		bus_space_write_raw_multi_2(iot, ioh,
2635 		    sc->asic_base + ED_NOVELL_DATA, src, len);
2636 	else
2637 		bus_space_write_multi_1(iot, ioh,
2638 		    sc->asic_base + ED_NOVELL_DATA, src, len);
2639 
2640 	/*
2641 	 * Wait for remote DMA complete.  This is necessary because on the
2642 	 * transmit side, data is handled internally by the NIC in bursts and
2643 	 * we can't start another remote DMA until this one completes.  Not
2644 	 * waiting causes really bad things to happen - like the NIC
2645 	 * irrecoverably jamming the ISA bus.
2646 	 */
2647 	while (((NIC_GET(iot, ioh, nicbase, ED_P0_ISR) & ED_ISR_RDC) !=
2648 	    ED_ISR_RDC) && --maxwait);
2649 }
2650 
2651 /*
2652  * Write an mbuf chain to the destination NIC memory address using programmed
2653  * I/O.
2654  */
2655 u_int16_t
ed_pio_write_mbufs(sc,m,dst)2656 ed_pio_write_mbufs(sc, m, dst)
2657 	struct ed_softc *sc;
2658 	struct mbuf *m;
2659 	u_int16_t dst;
2660 {
2661 	bus_space_tag_t iot = sc->sc_iot;
2662 	bus_space_handle_t ioh = sc->sc_ioh;
2663 	int nicbase = sc->nic_base, asicbase = sc->asic_base;
2664 	u_int16_t len;
2665 	int maxwait = 100; /* about 120us */
2666 
2667 	len = m->m_pkthdr.len;
2668 
2669 	/* Select page 0 registers. */
2670 	NIC_PUT(iot, ioh, nicbase, ED_P0_CR,
2671 	    ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STA);
2672 
2673 	/* Reset remote DMA complete flag. */
2674 	NIC_PUT(iot, ioh, nicbase, ED_P0_ISR, ED_ISR_RDC);
2675 
2676 	/* Set up DMA byte count. */
2677 	NIC_PUT(iot, ioh, nicbase, ED_P0_RBCR0, len);
2678 	NIC_PUT(iot, ioh, nicbase, ED_P0_RBCR1, len >> 8);
2679 
2680 	/* Set up destination address in NIC mem. */
2681 	NIC_PUT(iot, ioh, nicbase, ED_P0_RSAR0, dst);
2682 	NIC_PUT(iot, ioh, nicbase, ED_P0_RSAR1, dst >> 8);
2683 
2684 	/* Set remote DMA write. */
2685 	NIC_PUT(iot, ioh, nicbase, ED_P0_CR,
2686 	    ED_CR_RD1 | ED_CR_PAGE_0 | ED_CR_STA);
2687 
2688 	/*
2689 	 * Transfer the mbuf chain to the NIC memory.
2690 	 * 16-bit cards require that data be transferred as words, and only
2691 	 * words, so that case requires some extra code to patch over
2692 	 * odd-length mbufs.
2693 	 */
2694 	if (!sc->isa16bit) {
2695 		/* NE1000s are easy. */
2696 		for (; m != 0; m = m->m_next) {
2697 			if (m->m_len) {
2698 				bus_space_write_multi_1(iot, ioh,
2699 				    asicbase + ED_NOVELL_DATA,
2700 				    mtod(m, u_char *), m->m_len);
2701 			}
2702 		}
2703 	} else {
2704 		/* NE2000s are a bit trickier. */
2705 		u_int8_t *data, savebyte[2];
2706 		int len, wantbyte;
2707 
2708 		wantbyte = 0;
2709 		for (; m != 0; m = m->m_next) {
2710 			len = m->m_len;
2711 			if (len == 0)
2712 				continue;
2713 			data = mtod(m, u_int8_t *);
2714 			/* Finish the last word. */
2715 			if (wantbyte) {
2716 				savebyte[1] = *data;
2717 				bus_space_write_raw_multi_2(iot, ioh,
2718 				    asicbase + ED_NOVELL_DATA, savebyte, 2);
2719 				data++;
2720 				len--;
2721 				wantbyte = 0;
2722 			}
2723 			/* Output contiguous words. */
2724 			if (len > 1) {
2725 				bus_space_write_raw_multi_2(iot, ioh,
2726 				    asicbase + ED_NOVELL_DATA, data, len & ~1);
2727 			}
2728 			/* Save last byte, if necessary. */
2729 			if (len & 1) {
2730 				data += len & ~1;
2731 				savebyte[0] = *data;
2732 				wantbyte = 1;
2733 			}
2734 		}
2735 
2736 		if (wantbyte) {
2737 			savebyte[1] = 0;
2738 			bus_space_write_raw_multi_2(iot, ioh,
2739 			    asicbase + ED_NOVELL_DATA, savebyte, 2);
2740 		}
2741 	}
2742 
2743 	/*
2744 	 * Wait for remote DMA complete.  This is necessary because on the
2745 	 * transmit side, data is handled internally by the NIC in bursts and
2746 	 * we can't start another remote DMA until this one completes. 	Not
2747 	 * waiting causes really bad things to happen - like the NIC
2748 	 * irrecoverably jamming the ISA bus.
2749 	 */
2750 	while (((NIC_GET(iot, ioh, nicbase, ED_P0_ISR) & ED_ISR_RDC) !=
2751 	    ED_ISR_RDC) && --maxwait);
2752 
2753 	if (!maxwait) {
2754 		log(LOG_WARNING,
2755 		    "%s: remote transmit DMA failed to complete\n",
2756 		    sc->sc_dev.dv_xname);
2757 		edreset(sc);
2758 	}
2759 
2760 	return (len);
2761 }
2762 
2763 /*
2764  * Given a source and destination address, copy 'amount' of a packet from the
2765  * ring buffer into a linear destination buffer.  Takes into account ring-wrap.
2766  */
2767 static __inline int
ed_ring_copy(sc,src,dst,amount)2768 ed_ring_copy(sc, src, dst, amount)
2769 	struct ed_softc *sc;
2770 	int src;
2771 	caddr_t dst;
2772 	u_int16_t amount;
2773 {
2774 	u_int16_t tmp_amount;
2775 
2776 	/* Does copy wrap to lower addr in ring buffer? */
2777 	if (src + amount > sc->mem_end) {
2778 		tmp_amount = sc->mem_end - src;
2779 
2780 		/* Copy amount up to end of NIC memory. */
2781 		if (sc->mem_shared)
2782 			ed_shared_readmem(sc, src, dst, tmp_amount);
2783 		else
2784 			ed_pio_readmem(sc, (u_int16_t)src, dst, tmp_amount);
2785 
2786 		amount -= tmp_amount;
2787 		src = sc->mem_ring;
2788 		dst += tmp_amount;
2789 	}
2790 
2791 	if (sc->mem_shared)
2792 		ed_shared_readmem(sc, src, dst, amount);
2793 	else
2794 		ed_pio_readmem(sc, (u_int16_t)src, dst, amount);
2795 
2796 	return (src + amount);
2797 }
2798 
2799 /*
2800  * Copy data from receive buffer to end of mbuf chain allocate additional mbufs
2801  * as needed.  Return pointer to last mbuf in chain.
2802  * sc = ed info (softc)
2803  * src = pointer in ed ring buffer
2804  * totlen = maximum packet size
2805  */
2806 struct mbuf *
edget(sc,src,totlen)2807 edget(sc, src, totlen)
2808 	struct ed_softc *sc;
2809 	int src;
2810 	int totlen;
2811 {
2812 	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
2813 	struct mbuf *top, **mp, *m;
2814 	int len, pad;
2815 
2816 	MGETHDR(m, M_DONTWAIT, MT_DATA);
2817 	if (m == 0)
2818 		return 0;
2819 
2820 	m->m_pkthdr.rcvif = ifp;
2821 	m->m_pkthdr.len = totlen;
2822 	pad = ALIGN(sizeof(struct ether_header)) - sizeof(struct ether_header);
2823 	m->m_data += pad;
2824 	len = MHLEN - pad;
2825 	top = 0;
2826 	mp = &top;
2827 
2828 	while (totlen > 0) {
2829 		if (top) {
2830 			MGET(m, M_DONTWAIT, MT_DATA);
2831 			if (m == 0) {
2832 				m_freem(top);
2833 				return 0;
2834 			}
2835 			len = MLEN;
2836 		}
2837 		if (top && totlen >= MINCLSIZE) {
2838 			MCLGET(m, M_DONTWAIT);
2839 			if (m->m_flags & M_EXT)
2840 				len = MCLBYTES;
2841 		}
2842 		m->m_len = len = min(totlen, len);
2843 		src = ed_ring_copy(sc, src, mtod(m, caddr_t), len);
2844 		totlen -= len;
2845 		*mp = m;
2846 		mp = &m->m_next;
2847 	}
2848 
2849 	return top;
2850 }
2851 
2852 /*
2853  * Compute the multicast address filter from the list of multicast addresses we
2854  * need to listen to.
2855  */
2856 void
ed_getmcaf(ac,af)2857 ed_getmcaf(ac, af)
2858 	struct arpcom *ac;
2859 	u_int32_t *af;
2860 {
2861 	struct ifnet *ifp = &ac->ac_if;
2862 	struct ether_multi *enm;
2863 	register u_char *cp, c;
2864 	register u_int32_t crc;
2865 	register int i, len;
2866 	struct ether_multistep step;
2867 
2868 	/*
2869 	 * Set up multicast address filter by passing all multicast addresses
2870 	 * through a crc generator, and then using the high order 6 bits as an
2871 	 * index into the 64 bit logical address filter.  The high order bit
2872 	 * selects the word, while the rest of the bits select the bit within
2873 	 * the word.
2874 	 */
2875 
2876 	if (ifp->if_flags & IFF_PROMISC) {
2877 		ifp->if_flags |= IFF_ALLMULTI;
2878 		af[0] = af[1] = 0xffffffff;
2879 		return;
2880 	}
2881 
2882 	af[0] = af[1] = 0;
2883 	ETHER_FIRST_MULTI(step, ac, enm);
2884 	while (enm != NULL) {
2885 		if (bcmp(enm->enm_addrlo, enm->enm_addrhi,
2886 		    sizeof(enm->enm_addrlo)) != 0) {
2887 			/*
2888 			 * We must listen to a range of multicast addresses.
2889 			 * For now, just accept all multicasts, rather than
2890 			 * trying to set only those filter bits needed to match
2891 			 * the range.  (At this time, the only use of address
2892 			 * ranges is for IP multicast routing, for which the
2893 			 * range is big enough to require all bits set.)
2894 			 */
2895 			ifp->if_flags |= IFF_ALLMULTI;
2896 			af[0] = af[1] = 0xffffffff;
2897 			return;
2898 		}
2899 
2900 		cp = enm->enm_addrlo;
2901 		crc = 0xffffffff;
2902 		for (len = sizeof(enm->enm_addrlo); --len >= 0;) {
2903 			c = *cp++;
2904 			for (i = 8; --i >= 0;) {
2905 				if (((crc & 0x80000000) ? 1 : 0)
2906 				    ^ (c & 0x01)) {
2907 					crc <<= 1;
2908 					crc ^= 0x04c11db6 | 1;
2909 				} else
2910 					crc <<= 1;
2911 				c >>= 1;
2912 			}
2913 		}
2914 		/* Just want the 6 most significant bits. */
2915 		crc >>= 26;
2916 
2917 		/* Turn on the corresponding bit in the filter. */
2918 		af[crc >> 5] |= 1 << ((crc & 0x1f) ^ 0);
2919 
2920 		ETHER_NEXT_MULTI(step, enm);
2921 	}
2922 	ifp->if_flags &= ~IFF_ALLMULTI;
2923 }
2924 
2925 void
ed_shared_writemem(sc,from,card,len)2926 ed_shared_writemem(sc, from, card, len)
2927 	struct ed_softc *sc;
2928 	caddr_t from;
2929 	int card, len;
2930 {
2931 	bus_space_tag_t memt = sc->sc_memt;
2932 	bus_space_handle_t memh = sc->sc_memh;
2933 	u_int16_t word;
2934 
2935 	/*
2936 	 * For 16-bit cards, 16-bit memory access has already
2937 	 * been set up.  Note that some cards are really picky
2938 	 * about enforcing 16-bit access to memory, so we
2939 	 * have to be careful.
2940 	 */
2941 	if (sc->isa16bit) {
2942 		/*
2943 		 * If writing to an odd location, we need to align first.
2944 		 * This requires a read-modify-write cycle as we should
2945 		 * keep accesses 16-bit wide.
2946 		 */
2947 		if (len > 0 && (card & 1)) {
2948 			word = bus_space_read_2(memt, memh, card & ~1);
2949 			word = (word & 0xff) | (*from << 8);
2950 			bus_space_write_2(memt, memh, card & ~1, word);
2951 			from++;
2952 			card++;
2953 			len--;
2954 		}
2955 		/* XXX I think maybe a bus_space_write_raw_region is needed. */
2956 		while (len > 1) {
2957 			word = (u_int8_t)from[0] | (u_int8_t)from[1] << 8;
2958 			bus_space_write_2(memt, memh, card, word);
2959 			from += 2;
2960 			card += 2;
2961 			len -= 2;
2962 		}
2963 		if (len == 1) {
2964 			word = *from;
2965 			bus_space_write_2(memt, memh, card, word);
2966 		}
2967 	} else {
2968 		while (len--)
2969 			bus_space_write_1(memt, memh, card++, *from++);
2970 	}
2971 }
2972 
2973 void
ed_shared_readmem(sc,card,to,len)2974 ed_shared_readmem(sc, card, to, len)
2975 	struct ed_softc *sc;
2976 	caddr_t to;
2977 	int card, len;
2978 {
2979 	bus_space_tag_t memt = sc->sc_memt;
2980 	bus_space_handle_t memh = sc->sc_memh;
2981 	u_int16_t word;
2982 
2983 	/*
2984 	 * See comment above re. 16-bit cards.
2985 	 */
2986 	if (sc->isa16bit) {
2987 		/* XXX I think maybe a bus_space_read_raw_region is needed.  */
2988 		while (len > 1) {
2989 			word = bus_space_read_2(memt, memh, card);
2990 			*to++ = word & 0xff;
2991 			*to++ = word >> 8 & 0xff;
2992 			card += 2;
2993 			len -= 2;
2994 		}
2995 		if (len == 1)
2996 			*to = bus_space_read_2(memt, memh, card) & 0xff;
2997 	} else {
2998 		while (len--)
2999 			*to++ = bus_space_read_1(memt, memh, card++);
3000 	}
3001 }
3002