xref: /openbsd-src/sys/dev/isa/if_we.c (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1 /*	$OpenBSD: if_we.c,v 1.9 2001/03/12 05:37:00 aaron Exp $	*/
2 /*	$NetBSD: if_we.c,v 1.11 1998/07/05 06:49:14 jonathan Exp $	*/
3 
4 /*-
5  * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
10  * NASA Ames Research Center.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. All advertising materials mentioning features or use of this software
21  *    must display the following acknowledgement:
22  *	This product includes software developed by the NetBSD
23  *	Foundation, Inc. and its contributors.
24  * 4. Neither the name of The NetBSD Foundation nor the names of its
25  *    contributors may be used to endorse or promote products derived
26  *    from this software without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
32  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38  * POSSIBILITY OF SUCH DAMAGE.
39  */
40 
41 /*
42  * Device driver for National Semiconductor DS8390/WD83C690 based ethernet
43  * adapters.
44  *
45  * Copyright (c) 1994, 1995 Charles M. Hannum.  All rights reserved.
46  *
47  * Copyright (C) 1993, David Greenman.  This software may be used, modified,
48  * copied, distributed, and sold, in both source and binary form provided that
49  * the above copyright and these terms are retained.  Under no circumstances is
50  * the author responsible for the proper functioning of this software, nor does
51  * the author assume any responsibility for damages incurred with its use.
52  */
53 
54 /*
55  * Device driver for the Western Digital/SMC 8003 and 8013 series,
56  * and the SMC Elite Ultra (8216).
57  */
58 
59 #include "bpfilter.h"
60 #include "we.h"
61 
62 #include <sys/param.h>
63 #include <sys/systm.h>
64 #include <sys/device.h>
65 #include <sys/socket.h>
66 #include <sys/mbuf.h>
67 #include <sys/syslog.h>
68 
69 #include <net/if.h>
70 #include <net/if_dl.h>
71 #include <net/if_types.h>
72 #include <net/if_media.h>
73 
74 #ifdef __NetBSD__
75 #include <net/if_ether.h>
76 #endif
77 
78 #ifdef INET
79 #include <netinet/in.h>
80 #include <netinet/in_systm.h>
81 #include <netinet/in_var.h>
82 #include <netinet/ip.h>
83 #ifdef __NetBSD__
84 #include <netinet/if_inarp.h>
85 #else
86 #include <netinet/if_ether.h>
87 #endif
88 #endif
89 
90 #ifdef NS
91 #include <netns/ns.h>
92 #include <netns/ns_if.h>
93 #endif
94 
95 #if NBPFILTER > 0
96 #include <net/bpf.h>
97 #include <net/bpfdesc.h>
98 #endif
99 
100 #include <machine/bus.h>
101 #include <machine/intr.h>
102 
103 #include <dev/isa/isareg.h>
104 #include <dev/isa/isavar.h>
105 
106 #include <dev/ic/dp8390reg.h>
107 #include <dev/ic/dp8390var.h>
108 
109 #include <dev/isa/if_wereg.h>
110 
111 #ifndef __BUS_SPACE_HAS_STREAM_METHODS
112 #define	bus_space_read_region_stream_2	bus_space_read_region_2
113 #define	bus_space_write_stream_2	bus_space_write_2
114 #define	bus_space_write_region_stream_2	bus_space_write_region_2
115 #endif
116 
117 struct we_softc {
118 	struct dp8390_softc sc_dp8390;
119 
120 	bus_space_tag_t sc_asict;	/* space tag for ASIC */
121 	bus_space_handle_t sc_asich;	/* space handle for ASIC */
122 
123 	u_int8_t sc_laar_proto;
124 	u_int8_t sc_msr_proto;
125 
126 	u_int8_t sc_type;		/* our type */
127 
128 	int sc_16bitp;			/* are we 16 bit? */
129 
130 	void *sc_ih;			/* interrupt handle */
131 };
132 
133 int	we_probe __P((struct device *, void *, void *));
134 void	we_attach __P((struct device *, struct device *, void *));
135 
136 struct cfattach we_isa_ca = {
137 	sizeof(struct we_softc), we_probe, we_attach
138 };
139 
140 #if NWE_ISAPNP
141 struct cfattach we_isapnp_ca = {
142 	sizeof(struct we_softc), we_probe, we_attach
143 };
144 #endif /* NWE_ISAPNP */
145 
146 #ifdef __NetBSD__
147 extern struct cfdriver we_cd;
148 #else
149 struct cfdriver we_cd = {
150 	NULL, "we", DV_IFNET
151 };
152 #endif
153 
154 const char *we_params __P((bus_space_tag_t, bus_space_handle_t, u_int8_t *,
155 	    bus_size_t *, int *, int *));
156 void	we_set_media __P((struct we_softc *, int));
157 
158 void	we_media_init __P((struct dp8390_softc *));
159 
160 int	we_mediachange __P((struct dp8390_softc *));
161 void	we_mediastatus __P((struct dp8390_softc *, struct ifmediareq *));
162 
163 void	we_recv_int __P((struct dp8390_softc *));
164 void	we_init_card __P((struct dp8390_softc *));
165 int	we_write_mbuf __P((struct dp8390_softc *, struct mbuf *, int));
166 int	we_ring_copy __P((struct dp8390_softc *, int, caddr_t, u_short));
167 void	we_read_hdr __P((struct dp8390_softc *, int, struct dp8390_ring *));
168 int	we_test_mem __P((struct dp8390_softc *));
169 
170 __inline void we_readmem __P((struct we_softc *, int, u_int8_t *, int));
171 
172 static const int we_584_irq[] = {
173 	9, 3, 5, 7, 10, 11, 15, 4,
174 };
175 #define	NWE_584_IRQ	(sizeof(we_584_irq) / sizeof(we_584_irq[0]))
176 
177 static const int we_790_irq[] = {
178 	IRQUNK, 9, 3, 5, 7, 10, 11, 15,
179 };
180 #define	NWE_790_IRQ	(sizeof(we_790_irq) / sizeof(we_790_irq[0]))
181 
182 /*
183  * Delay needed when switching 16-bit access to shared memory.
184  */
185 #define	WE_DELAY(wsc) delay(3)
186 
187 /*
188  * Enable card RAM, and 16-bit access.
189  */
190 #define	WE_MEM_ENABLE(wsc) \
191 do { \
192 	if ((wsc)->sc_16bitp) \
193 		bus_space_write_1((wsc)->sc_asict, (wsc)->sc_asich, \
194 		    WE_LAAR, (wsc)->sc_laar_proto | WE_LAAR_M16EN); \
195 	bus_space_write_1((wsc)->sc_asict, (wsc)->sc_asich, \
196 	    WE_MSR, wsc->sc_msr_proto | WE_MSR_MENB); \
197 	WE_DELAY((wsc)); \
198 } while (0)
199 
200 /*
201  * Disable card RAM, and 16-bit access.
202  */
203 #define	WE_MEM_DISABLE(wsc) \
204 do { \
205 	bus_space_write_1((wsc)->sc_asict, (wsc)->sc_asich, \
206 	    WE_MSR, (wsc)->sc_msr_proto); \
207 	if ((wsc)->sc_16bitp) \
208 		bus_space_write_1((wsc)->sc_asict, (wsc)->sc_asich, \
209 		    WE_LAAR, (wsc)->sc_laar_proto); \
210 	WE_DELAY((wsc)); \
211 } while (0)
212 
213 int
214 we_probe(parent, match, aux)
215 	struct device *parent;
216 	void *match, *aux;
217 {
218 	struct isa_attach_args *ia = aux;
219 	struct cfdata *cf = match;
220 	bus_space_tag_t asict, memt;
221 	bus_space_handle_t asich, memh;
222 	bus_size_t memsize;
223 	int asich_valid, memh_valid;
224 	int i, is790, rv = 0;
225 	u_int8_t x, type;
226 
227 	asict = ia->ia_iot;
228 	memt = ia->ia_memt;
229 
230 	asich_valid = memh_valid = 0;
231 
232 	/* Disallow wildcarded i/o addresses. */
233 	if (ia->ia_iobase == -1 /* ISACF_PORT_DEFAULT */)
234 		return (0);
235 
236 	/* Disallow wildcarded mem address. */
237 	if (ia->ia_maddr == -1 /* ISACF_IOMEM_DEFAULT */)
238 		return (0);
239 
240 	/* Attempt to map the device. */
241 	if (!strcmp(parent->dv_cfdata->cf_driver->cd_name, "isapnp") && ia->ia_ioh)
242 		asich = ia->ia_ioh;
243 	else {
244 		if (bus_space_map(asict, ia->ia_iobase, WE_NPORTS, 0, &asich))
245 			goto out;
246 		asich_valid = 1;
247 	}
248 
249 #ifdef TOSH_ETHER
250 	bus_space_write_1(asict, asich, WE_MSR, WE_MSR_POW);
251 #endif
252 
253 	/*
254 	 * Attempt to do a checksum over the station address PROM.
255 	 * If it fails, it's probably not a WD/SMC board.  There is
256 	 * a problem with this, though.  Some clone WD8003E boards
257 	 * (e.g. Danpex) won't pass the checksum.  In this case,
258 	 * the checksum byte always seems to be 0.
259 	 */
260 	for (x = 0, i = 0; i < 8; i++)
261 		x += bus_space_read_1(asict, asich, WE_PROM + i);
262 
263 	if (x != WE_ROM_CHECKSUM_TOTAL) {
264 		/* Make sure it's an 8003E clone... */
265 		if (bus_space_read_1(asict, asich, WE_CARD_ID) !=
266 		    WE_TYPE_WD8003E)
267 			goto out;
268 
269 		/* Check the checksum byte. */
270 		if (bus_space_read_1(asict, asich, WE_PROM + 7) != 0)
271 			goto out;
272 	}
273 
274 	/*
275 	 * Reset the card to force it into a known state.
276 	 */
277 #ifdef TOSH_ETHER
278 	bus_space_write_1(asict, asich, WE_MSR, WE_MSR_RST | WE_MSR_POW);
279 #else
280 	bus_space_write_1(asict, asich, WE_MSR, WE_MSR_RST);
281 #endif
282 	delay(100);
283 
284 	bus_space_write_1(asict, asich, WE_MSR,
285 	    bus_space_read_1(asict, asich, WE_MSR) & ~WE_MSR_RST);
286 
287 	/* Wait in case the card is reading it's EEPROM. */
288 	delay(5000);
289 
290 	/*
291 	 * Get parameters.
292 	 */
293 	if (we_params(asict, asich, &type, &memsize, NULL, &is790) == NULL)
294 		goto out;
295 
296 	/* Allow user to override probed value. */
297 	if (ia->ia_msize)
298 		memsize = ia->ia_msize;
299 
300 	/* Attempt to map the memory space. */
301 	if (!strcmp(parent->dv_cfdata->cf_driver->cd_name, "isapnp") && ia->ia_memh)
302 		memh = ia->ia_memh;
303 	else {
304 		if (bus_space_map(memt, ia->ia_maddr, memsize, 0, &memh))
305 			goto out;
306 		memh_valid = 1;
307 	}
308 
309 	/*
310 	 * If possible, get the assigned interrupt number from the card
311 	 * and use it.
312 	 */
313 	if (is790) {
314 		u_int8_t hwr;
315 
316 		/* Assemble together the encoded interrupt number. */
317 		hwr = bus_space_read_1(asict, asich, WE790_HWR);
318 		bus_space_write_1(asict, asich, WE790_HWR,
319 		    hwr | WE790_HWR_SWH);
320 
321 		x = bus_space_read_1(asict, asich, WE790_GCR);
322 		i = ((x & WE790_GCR_IR2) >> 4) |
323 		    ((x & (WE790_GCR_IR1|WE790_GCR_IR0)) >> 2);
324 		bus_space_write_1(asict, asich, WE790_HWR,
325 		    hwr & ~WE790_HWR_SWH);
326 
327 		if (ia->ia_irq != IRQUNK && ia->ia_irq != we_790_irq[i])
328 			printf("%s%d: changing IRQ %d to %d\n",
329 			    we_cd.cd_name, cf->cf_unit, ia->ia_irq,
330 			    we_790_irq[i]);
331 		ia->ia_irq = we_790_irq[i];
332 	} else if (type & WE_SOFTCONFIG) {
333 		/* Assemble together the encoded interrupt number. */
334 		i = (bus_space_read_1(asict, asich, WE_ICR) & WE_ICR_IR2) |
335 		    ((bus_space_read_1(asict, asich, WE_IRR) &
336 		      (WE_IRR_IR0 | WE_IRR_IR1)) >> 5);
337 
338 		if (ia->ia_irq != IRQUNK && ia->ia_irq != we_584_irq[i])
339 			printf("%s%d: changing IRQ %d to %d\n",
340 			    we_cd.cd_name, cf->cf_unit, ia->ia_irq,
341 			    we_584_irq[i]);
342 		ia->ia_irq = we_584_irq[i];
343 	}
344 
345 	/* So, we say we've found it! */
346 	ia->ia_iosize = WE_NPORTS;
347 	ia->ia_msize = memsize;
348 	rv = 1;
349 
350  out:
351 	if (asich_valid)
352 		bus_space_unmap(asict, asich, WE_NPORTS);
353 	if (memh_valid)
354 		bus_space_unmap(memt, memh, memsize);
355 	return (rv);
356 }
357 
358 void
359 we_attach(parent, self, aux)
360 	struct device *parent, *self;
361 	void *aux;
362 {
363 	struct we_softc *wsc = (struct we_softc *)self;
364 	struct dp8390_softc *sc = &wsc->sc_dp8390;
365 	struct isa_attach_args *ia = aux;
366 	bus_space_tag_t nict, asict, memt;
367 	bus_space_handle_t nich, asich, memh;
368 	const char *typestr;
369 	u_int8_t x;
370 	int i;
371 
372 	nict = asict = ia->ia_iot;
373 	memt = ia->ia_memt;
374 
375 	/* Map the device. */
376 	if (!strcmp(parent->dv_cfdata->cf_driver->cd_name, "isapnp") && ia->ia_ioh)
377 		asich = ia->ia_ioh;
378 	else if (bus_space_map(asict, ia->ia_iobase, WE_NPORTS, 0, &asich)) {
379 		printf(": can't map nic i/o space\n");
380 		return;
381 	}
382 
383 	if (bus_space_subregion(asict, asich, WE_NIC_OFFSET, WE_NIC_NPORTS,
384 	    &nich)) {
385 		printf(": can't subregion i/o space\n");
386 		return;
387 	}
388 
389 	typestr = we_params(asict, asich, &wsc->sc_type, NULL,
390 	    &wsc->sc_16bitp, &sc->is790);
391 	if (typestr == NULL) {
392 		printf(": where did the card go?\n");
393 		return;
394 	}
395 
396 	/*
397 	 * Map memory space.  Note we use the size that might have
398 	 * been overridden by the user.
399 	 */
400 	if (!strcmp(parent->dv_cfdata->cf_driver->cd_name, "isapnp") && ia->ia_memh)
401 		memh = ia->ia_memh;
402 	else if (bus_space_map(memt, ia->ia_maddr, ia->ia_msize, 0, &memh)) {
403 		printf(": can't map shared memory\n");
404 		return;
405 	}
406 
407 	/*
408 	 * Allow user to override 16-bit mode.  8-bit takes precedence.
409 	 */
410 	if (self->dv_cfdata->cf_flags & WE_FLAGS_FORCE_16BIT_MODE)
411 		wsc->sc_16bitp = 1;
412 	if (self->dv_cfdata->cf_flags & WE_FLAGS_FORCE_8BIT_MODE)
413 		wsc->sc_16bitp = 0;
414 
415 	wsc->sc_asict = asict;
416 	wsc->sc_asich = asich;
417 
418 	sc->sc_regt = nict;
419 	sc->sc_regh = nich;
420 
421 	sc->sc_buft = memt;
422 	sc->sc_bufh = memh;
423 
424 	/* Interface is always enabled. */
425 	sc->sc_enabled = 1;
426 
427 	/* Registers are linear. */
428 	for (i = 0; i < 16; i++)
429 		sc->sc_reg_map[i] = i;
430 
431 	/* Now we can use the NIC_{GET,PUT}() macros. */
432 
433 	printf(": %s (%s-bit)\n", typestr, wsc->sc_16bitp ? "16" : "8");
434 
435 	/* Get station address from EEPROM. */
436 	for (i = 0; i < ETHER_ADDR_LEN; i++)
437 #ifdef __NetBSD__
438 		sc->sc_enaddr[i] = bus_space_read_1(asict, asich, WE_PROM + i);
439 #else
440 		sc->sc_arpcom.ac_enaddr[i] =
441 		    bus_space_read_1(asict, asich, WE_PROM + i);
442 #endif
443 
444 	/*
445 	 * Set upper address bits and 8/16 bit access to shared memory.
446 	 */
447 	if (sc->is790) {
448 		wsc->sc_laar_proto =
449 		    bus_space_read_1(asict, asich, WE_LAAR) &
450 		    ~WE_LAAR_M16EN;
451 		bus_space_write_1(asict, asich, WE_LAAR,
452 		    wsc->sc_laar_proto | (wsc->sc_16bitp ? WE_LAAR_M16EN : 0));
453 	} else if ((wsc->sc_type & WE_SOFTCONFIG) ||
454 #ifdef TOSH_ETHER
455 	    (wsc->sc_type == WE_TYPE_TOSHIBA1) ||
456 	    (wsc->sc_type == WE_TYPE_TOSHIBA4) ||
457 #endif
458 	    (wsc->sc_type == WE_TYPE_WD8013EBT)) {
459 		wsc->sc_laar_proto = (ia->ia_maddr >> 19) & WE_LAAR_ADDRHI;
460 		if (wsc->sc_16bitp)
461 			wsc->sc_laar_proto |= WE_LAAR_L16EN;
462 		bus_space_write_1(asict, asich, WE_LAAR,
463 		    wsc->sc_laar_proto | (wsc->sc_16bitp ? WE_LAAR_M16EN : 0));
464 	}
465 
466 	/*
467 	 * Set address and enable interface shared memory.
468 	 */
469 	if (sc->is790) {
470 		/* XXX MAGIC CONSTANTS XXX */
471 		x = bus_space_read_1(asict, asich, 0x04);
472 		bus_space_write_1(asict, asich, 0x04, x | 0x80);
473 		bus_space_write_1(asict, asich, 0x0b,
474 		    ((ia->ia_maddr >> 13) & 0x0f) |
475 		    ((ia->ia_maddr >> 11) & 0x40) |
476 		    (bus_space_read_1(asict, asich, 0x0b) & 0xb0));
477 		bus_space_write_1(asict, asich, 0x04, x);
478 		wsc->sc_msr_proto = 0x00;
479 		sc->cr_proto = 0x00;
480 	} else {
481 #ifdef TOSH_ETHER
482 		if (wsc->sc_type == WE_TYPE_TOSHIBA1 ||
483 		    wsc->sc_type == WE_TYPE_TOSHIBA4) {
484 			bus_space_write_1(asict, asich, WE_MSR + 1,
485 			    ((ia->ia_maddr >> 8) & 0xe0) | 0x04);
486 			bus_space_write_1(asict, asich, WE_MSR + 2,
487 			    ((ia->ia_maddr >> 16) & 0x0f));
488 			wsc->sc_msr_proto = WE_MSR_POW;
489 		} else
490 #endif
491 			wsc->sc_msr_proto = (ia->ia_maddr >> 13) &
492 			    WE_MSR_ADDR;
493 
494 		sc->cr_proto = ED_CR_RD2;
495 	}
496 
497 	bus_space_write_1(asict, asich, WE_MSR,
498 	    wsc->sc_msr_proto | WE_MSR_MENB);
499 	WE_DELAY(wsc);
500 
501 	/*
502 	 * DCR gets:
503 	 *
504 	 *	FIFO threshold to 8, No auto-init Remote DMA,
505 	 *	byte order=80x86.
506 	 *
507 	 * 16-bit cards also get word-wide DMA transfers.
508 	 */
509 	sc->dcr_reg = ED_DCR_FT1 | ED_DCR_LS |
510 	    (wsc->sc_16bitp ? ED_DCR_WTS : 0);
511 
512 	sc->test_mem = we_test_mem;
513 	sc->ring_copy = we_ring_copy;
514 	sc->write_mbuf = we_write_mbuf;
515 	sc->read_hdr = we_read_hdr;
516 	sc->recv_int = we_recv_int;
517 
518 	sc->sc_mediachange = we_mediachange;
519 	sc->sc_mediastatus = we_mediastatus;
520 
521 	sc->mem_start = 0;
522 	sc->mem_size = ia->ia_msize;
523 
524 	sc->sc_flags = self->dv_cfdata->cf_flags;
525 
526 	/* Do generic parts of attach. */
527 	if (wsc->sc_type & WE_SOFTCONFIG)
528 		sc->sc_media_init = we_media_init;
529 	else
530 		sc->sc_media_init = dp8390_media_init;
531 	if (dp8390_config(sc)) {
532 		printf("%s: configuration failed\n", sc->sc_dev.dv_xname);
533 		return;
534 	}
535 
536 	/*
537 	 * Disable 16-bit access to shared memory - we leave it disabled
538 	 * so that:
539 	 *
540 	 *	(1) machines reboot properly when the board is set to
541 	 *	    16-bit mode and there are conflicting 8-bit devices
542 	 *	    within the same 128k address space as this board's
543 	 *	    shared memory, and
544 	 *
545 	 *	(2) so that other 8-bit devices with shared memory
546 	 *	    in this same 128k address space will work.
547 	 */
548 	WE_MEM_DISABLE(wsc);
549 
550 	/*
551 	 * Enable the configured interrupt.
552 	 */
553 	if (sc->is790)
554 		bus_space_write_1(asict, asich, WE790_ICR,
555 		    bus_space_read_1(asict, asich, WE790_ICR) |
556 		    WE790_ICR_EIL);
557 	else if (wsc->sc_type & WE_SOFTCONFIG)
558 		bus_space_write_1(asict, asich, WE_IRR,
559 		    bus_space_read_1(asict, asich, WE_IRR) | WE_IRR_IEN);
560 	else if (ia->ia_irq == IRQUNK) {
561 		printf("%s: can't wildcard IRQ on a %s\n",
562 		    sc->sc_dev.dv_xname, typestr);
563 		return;
564 	}
565 
566 	/* Establish interrupt handler. */
567 	wsc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_EDGE,
568 	    IPL_NET, dp8390_intr, sc, sc->sc_dev.dv_xname);
569 	if (wsc->sc_ih == NULL)
570 		printf("%s: can't establish interrupt\n", sc->sc_dev.dv_xname);
571 }
572 
573 int
574 we_test_mem(sc)
575 	struct dp8390_softc *sc;
576 {
577 	struct we_softc *wsc = (struct we_softc *)sc;
578 	bus_space_tag_t memt = sc->sc_buft;
579 	bus_space_handle_t memh = sc->sc_bufh;
580 	bus_size_t memsize = sc->mem_size;
581 	int i;
582 
583 	if (wsc->sc_16bitp)
584 		bus_space_set_region_2(memt, memh, 0, 0, memsize >> 1);
585 	else
586 		bus_space_set_region_1(memt, memh, 0, 0, memsize);
587 
588 	if (wsc->sc_16bitp) {
589 		for (i = 0; i < memsize; i += 2) {
590 			if (bus_space_read_2(memt, memh, i) != 0)
591 				goto fail;
592 		}
593 	} else {
594 		for (i = 0; i < memsize; i++) {
595 			if (bus_space_read_1(memt, memh, i) != 0)
596 				goto fail;
597 		}
598 	}
599 
600 	return (0);
601 
602  fail:
603 	printf("%s: failed to clear shared memory at offset 0x%x\n",
604 	    sc->sc_dev.dv_xname, i);
605 	WE_MEM_DISABLE(wsc);
606 	return (1);
607 }
608 
609 /*
610  * Given a NIC memory source address and a host memory destination address,
611  * copy 'len' from NIC to host using shared memory.  The 'len' is rounded
612  * up to a word - ok as long as mbufs are word-sized.
613  */
614 __inline void
615 we_readmem(wsc, from, to, len)
616 	struct we_softc *wsc;
617 	int from;
618 	u_int8_t *to;
619 	int len;
620 {
621 	bus_space_tag_t memt = wsc->sc_dp8390.sc_buft;
622 	bus_space_handle_t memh = wsc->sc_dp8390.sc_bufh;
623 
624 	if (len & 1)
625 		++len;
626 
627 	if (wsc->sc_16bitp)
628 		bus_space_read_region_stream_2(memt, memh, from,
629 		    (u_int16_t *)to, len >> 1);
630 	else
631 		bus_space_read_region_1(memt, memh, from,
632 		    to, len);
633 }
634 
635 int
636 we_write_mbuf(sc, m, buf)
637 	struct dp8390_softc *sc;
638 	struct mbuf *m;
639 	int buf;
640 {
641 	struct we_softc *wsc = (struct we_softc *)sc;
642 	bus_space_tag_t memt = wsc->sc_dp8390.sc_buft;
643 	bus_space_handle_t memh = wsc->sc_dp8390.sc_bufh;
644 	u_int8_t *data, savebyte[2];
645 	int savelen, len, leftover;
646 #ifdef DIAGNOSTIC
647 	u_int8_t *lim;
648 #endif
649 
650 	savelen = m->m_pkthdr.len;
651 
652 	WE_MEM_ENABLE(wsc);
653 
654 	/*
655 	 * 8-bit boards are simple; no alignment tricks are necessary.
656 	 */
657 	if (wsc->sc_16bitp == 0) {
658 		for (; m != NULL; buf += m->m_len, m = m->m_next)
659 			bus_space_write_region_1(memt, memh,
660 			    buf, mtod(m, u_int8_t *), m->m_len);
661 		goto out;
662 	}
663 
664 	/* Start out with no leftover data. */
665 	leftover = 0;
666 	savebyte[0] = savebyte[1] = 0;
667 
668 	for (; m != NULL; m = m->m_next) {
669 		len = m->m_len;
670 		if (len == 0)
671 			continue;
672 		data = mtod(m, u_int8_t *);
673 #ifdef DIAGNOSTIC
674 		lim = data + len;
675 #endif
676 		while (len > 0) {
677 			if (leftover) {
678 				/*
679 				 * Data left over (from mbuf or realignment).
680 				 * Buffer the next byte, and write it and
681 				 * the leftover data out.
682 				 */
683 				savebyte[1] = *data++;
684 				len--;
685 				bus_space_write_stream_2(memt, memh, buf,
686 				    *(u_int16_t *)savebyte);
687 				buf += 2;
688 				leftover = 0;
689 #ifdef i386
690 #define ALIGNED_POINTER(p,t)	1
691 #endif
692 #ifdef alpha
693 #define ALIGNED_POINTER(p,t)	((((u_long)(p)) & (sizeof(t)-1)) == 0)
694 #endif
695 			} else if (ALIGNED_POINTER(data, u_int16_t) == 0) {
696 				/*
697 				 * Unaligned dta; buffer the next byte.
698 				 */
699 				savebyte[0] = *data++;
700 				len--;
701 				leftover = 1;
702 			} else {
703 				/*
704 				 * Aligned data; output contiguous words as
705 				 * much as we can, then buffer the remaining
706 				 * byte, if any.
707 				 */
708 				leftover = len & 1;
709 				len &= ~1;
710 				bus_space_write_region_stream_2(memt, memh,
711 				    buf, (u_int16_t *)data, len >> 1);
712 				data += len;
713 				buf += len;
714 				if (leftover)
715 					savebyte[0] = *data++;
716 				len = 0;
717 			}
718 		}
719 		if (len < 0)
720 			panic("we_write_mbuf: negative len");
721 #ifdef DIAGNOSTIC
722 		if (data != lim)
723 			panic("we_write_mbuf: data != lim");
724 #endif
725 	}
726 	if (leftover) {
727 		savebyte[1] = 0;
728 		bus_space_write_stream_2(memt, memh, buf,
729 		    *(u_int16_t *)savebyte);
730 	}
731 
732  out:
733 	WE_MEM_DISABLE(wsc);
734 
735 	return (savelen);
736 }
737 
738 int
739 we_ring_copy(sc, src, dst, amount)
740 	struct dp8390_softc *sc;
741 	int src;
742 	caddr_t dst;
743 	u_short amount;
744 {
745 	struct we_softc *wsc = (struct we_softc *)sc;
746 	u_short tmp_amount;
747 
748 	/* Does copy wrap to lower addr in ring buffer? */
749 	if (src + amount > sc->mem_end) {
750 		tmp_amount = sc->mem_end - src;
751 
752 		/* Copy amount up to end of NIC memory. */
753 		we_readmem(wsc, src, dst, tmp_amount);
754 
755 		amount -= tmp_amount;
756 		src = sc->mem_ring;
757 		dst += tmp_amount;
758 	}
759 
760 	we_readmem(wsc, src, dst, amount);
761 
762 	return (src + amount);
763 }
764 
765 void
766 we_read_hdr(sc, packet_ptr, packet_hdrp)
767 	struct dp8390_softc *sc;
768 	int packet_ptr;
769 	struct dp8390_ring *packet_hdrp;
770 {
771 	struct we_softc *wsc = (struct we_softc *)sc;
772 
773 	we_readmem(wsc, packet_ptr, (u_int8_t *)packet_hdrp,
774 	    sizeof(struct dp8390_ring));
775 #if BYTE_ORDER == BIG_ENDIAN
776 	packet_hdrp->count = swap16(packet_hdrp->count);
777 #endif
778 }
779 
780 void
781 we_recv_int(sc)
782 	struct dp8390_softc *sc;
783 {
784 	struct we_softc *wsc = (struct we_softc *)sc;
785 
786 	WE_MEM_ENABLE(wsc);
787 	dp8390_rint(sc);
788 	WE_MEM_DISABLE(wsc);
789 }
790 
791 void
792 we_media_init(struct dp8390_softc *sc)
793 {
794 	struct we_softc *wsc = (void *)sc;
795 	int defmedia = IFM_ETHER;
796 	u_int8_t x;
797 
798 	if (sc->is790) {
799 		x = bus_space_read_1(wsc->sc_asict, wsc->sc_asich, WE790_HWR);
800 		bus_space_write_1(wsc->sc_asict, wsc->sc_asich, WE790_HWR,
801 		    x | WE790_HWR_SWH);
802 		if (bus_space_read_1(wsc->sc_asict, wsc->sc_asich, WE790_GCR) &
803 		    WE790_GCR_GPOUT)
804 			defmedia |= IFM_10_2;
805 		else
806 			defmedia |= IFM_10_5;
807 		bus_space_write_1(wsc->sc_asict, wsc->sc_asich, WE790_HWR,
808 		    x &~ WE790_HWR_SWH);
809 	} else {
810 		x = bus_space_read_1(wsc->sc_asict, wsc->sc_asich, WE_IRR);
811 		if (x & WE_IRR_OUT2)
812 			defmedia |= IFM_10_2;
813 		else
814 			defmedia |= IFM_10_5;
815 	}
816 
817 	ifmedia_init(&sc->sc_media, 0, dp8390_mediachange, dp8390_mediastatus);
818 	ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_10_2, 0, NULL);
819 	ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_10_5, 0, NULL);
820 	ifmedia_set(&sc->sc_media, defmedia);
821 }
822 
823 int
824 we_mediachange(sc)
825 	struct dp8390_softc *sc;
826 {
827 
828 	/*
829 	 * Current media is already set up.  Just reset the interface
830 	 * to let the new value take hold.  The new media will be
831 	 * set up in we_init_card() called via dp8390_init().
832 	 */
833 	dp8390_reset(sc);
834 	return (0);
835 }
836 
837 void
838 we_mediastatus(sc, ifmr)
839 	struct dp8390_softc *sc;
840 	struct ifmediareq *ifmr;
841 {
842 	struct ifmedia *ifm = &sc->sc_media;
843 
844 	/*
845 	 * The currently selected media is always the active media.
846 	 */
847 	ifmr->ifm_active = ifm->ifm_cur->ifm_media;
848 }
849 
850 void
851 we_init_card(sc)
852 	struct dp8390_softc *sc;
853 {
854 	struct we_softc *wsc = (struct we_softc *)sc;
855 	struct ifmedia *ifm = &sc->sc_media;
856 
857 	we_set_media(wsc, ifm->ifm_cur->ifm_media);
858 }
859 
860 void
861 we_set_media(wsc, media)
862 	struct we_softc *wsc;
863 	int media;
864 {
865 	struct dp8390_softc *sc = &wsc->sc_dp8390;
866 	bus_space_tag_t asict = wsc->sc_asict;
867 	bus_space_handle_t asich = wsc->sc_asich;
868 	u_int8_t hwr, gcr, irr;
869 
870 	if (sc->is790) {
871 		hwr = bus_space_read_1(asict, asich, WE790_HWR);
872 		bus_space_write_1(asict, asich, WE790_HWR,
873 		    hwr | WE790_HWR_SWH);
874 		gcr = bus_space_read_1(asict, asich, WE790_GCR);
875 		if (IFM_SUBTYPE(media) == IFM_10_2)
876 			gcr |= WE790_GCR_GPOUT;
877 		else
878 			gcr &= ~WE790_GCR_GPOUT;
879 		bus_space_write_1(asict, asich, WE790_GCR,
880 		    gcr | WE790_GCR_LIT);
881 		bus_space_write_1(asict, asich, WE790_HWR,
882 		    hwr & ~WE790_HWR_SWH);
883 		return;
884 	}
885 
886 	irr = bus_space_read_1(wsc->sc_asict, wsc->sc_asich, WE_IRR);
887 	if (IFM_SUBTYPE(media) == IFM_10_2)
888 		irr |= WE_IRR_OUT2;
889 	else
890 		irr &= ~WE_IRR_OUT2;
891 	bus_space_write_1(wsc->sc_asict, wsc->sc_asich, WE_IRR, irr);
892 }
893 
894 const char *
895 we_params(asict, asich, typep, memsizep, is16bitp, is790p)
896 	bus_space_tag_t asict;
897 	bus_space_handle_t asich;
898 	u_int8_t *typep;
899 	bus_size_t *memsizep;
900 	int *is16bitp, *is790p;
901 {
902 	const char *typestr;
903 	bus_size_t memsize;
904 	int is16bit, is790;
905 	u_int8_t type;
906 
907 	memsize = 8192;
908 	is16bit = is790 = 0;
909 
910 	type = bus_space_read_1(asict, asich, WE_CARD_ID);
911 	switch (type) {
912 	case WE_TYPE_WD8003S:
913 		typestr = "WD8003S";
914 		break;
915 	case WE_TYPE_WD8003E:
916 		typestr = "WD8003E";
917 		break;
918 	case WE_TYPE_WD8003EB:
919 		typestr = "WD8003EB";
920 		break;
921 	case WE_TYPE_WD8003W:
922 		typestr = "WD8003W";
923 		break;
924 	case WE_TYPE_WD8013EBT:
925 		typestr = "WD8013EBT";
926 		memsize = 16384;
927 		is16bit = 1;
928 		break;
929 	case WE_TYPE_WD8013W:
930 		typestr = "WD8013W";
931 		memsize = 16384;
932 		is16bit = 1;
933 		break;
934 	case WE_TYPE_WD8013EP:		/* also WD8003EP */
935 		if (bus_space_read_1(asict, asich, WE_ICR) & WE_ICR_16BIT) {
936 			is16bit = 1;
937 			memsize = 16384;
938 			typestr = "WD8013EP";
939 		} else
940 			typestr = "WD8003EP";
941 		break;
942 	case WE_TYPE_WD8013WC:
943 		typestr = "WD8013WC";
944 		memsize = 16384;
945 		is16bit = 1;
946 		break;
947 	case WE_TYPE_WD8013EBP:
948 		typestr = "WD8013EBP";
949 		memsize = 16384;
950 		is16bit = 1;
951 		break;
952 	case WE_TYPE_WD8013EPC:
953 		typestr = "WD8013EPC";
954 		memsize = 16384;
955 		is16bit = 1;
956 		break;
957 	case WE_TYPE_SMC8216C:
958 	case WE_TYPE_SMC8216T:
959 	    {
960 		u_int8_t hwr;
961 
962 		typestr = (type == WE_TYPE_SMC8216C) ?
963 		    "SMC8216/SMC8216C" : "SMC8216T";
964 
965 		hwr = bus_space_read_1(asict, asich, WE790_HWR);
966 		bus_space_write_1(asict, asich, WE790_HWR,
967 		    hwr | WE790_HWR_SWH);
968 		switch (bus_space_read_1(asict, asich, WE790_RAR) &
969 		    WE790_RAR_SZ64) {
970 		case WE790_RAR_SZ64:
971 			memsize = 65536;
972 			break;
973 		case WE790_RAR_SZ32:
974 			memsize = 32768;
975 			break;
976 		case WE790_RAR_SZ16:
977 			memsize = 16384;
978 			break;
979 		case WE790_RAR_SZ8:
980 			/* 8216 has 16K shared mem -- 8416 has 8K */
981 			typestr = (type == WE_TYPE_SMC8216C) ?
982 			    "SMC8416C/SMC8416BT" : "SMC8416T";
983 			memsize = 8192;
984 			break;
985 		}
986 		bus_space_write_1(asict, asich, WE790_HWR, hwr);
987 
988 		is16bit = 1;
989 		is790 = 1;
990 		break;
991 	    }
992 #ifdef TOSH_ETHER
993 	case WE_TYPE_TOSHIBA1:
994 		typestr = "Toshiba1";
995 		memsize = 32768;
996 		is16bit = 1;
997 		break;
998 	case WE_TYPE_TOSHIBA4:
999 		typestr = "Toshiba4";
1000 		memsize = 32768;
1001 		is16bit = 1;
1002 		break;
1003 #endif
1004 	default:
1005 		/* Not one we recognize. */
1006 		return (NULL);
1007 	}
1008 
1009 	/*
1010 	 * Make some adjustments to initial values depending on what is
1011 	 * found in the ICR.
1012 	 */
1013 	if (is16bit && (type != WE_TYPE_WD8013EBT) &&
1014 #ifdef TOSH_ETHER
1015 	    (type != WE_TYPE_TOSHIBA1 && type != WE_TYPE_TOSHIBA4) &&
1016 #endif
1017 	    (bus_space_read_1(asict, asich, WE_ICR) & WE_ICR_16BIT) == 0) {
1018 		is16bit = 0;
1019 		memsize = 8192;
1020 	}
1021 
1022 #ifdef WE_DEBUG
1023 	{
1024 		int i;
1025 
1026 		printf("we_params: type = 0x%x, typestr = %s, is16bit = %d, "
1027 		    "memsize = %d\n", type, typestr, is16bit, memsize);
1028 		for (i = 0; i < 8; i++)
1029 			printf("     %d -> 0x%x\n", i,
1030 			    bus_space_read_1(asict, asich, i));
1031 	}
1032 #endif
1033 
1034 	if (typep != NULL)
1035 		*typep = type;
1036 	if (memsizep != NULL)
1037 		*memsizep = memsize;
1038 	if (is16bitp != NULL)
1039 		*is16bitp = is16bit;
1040 	if (is790p != NULL)
1041 		*is790p = is790;
1042 	return (typestr);
1043 }
1044