xref: /openbsd-src/sys/dev/isa/if_we.c (revision 8500990981f885cbe5e6a4958549cacc238b5ae6)
1 /*	$OpenBSD: if_we.c,v 1.11 2003/04/17 03:49:52 drahn 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(struct device *, void *, void *);
134 void	we_attach(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(bus_space_tag_t, bus_space_handle_t, u_int8_t *,
155 	    bus_size_t *, int *, int *);
156 void	we_set_media(struct we_softc *, int);
157 
158 void	we_media_init(struct dp8390_softc *);
159 
160 int	we_mediachange(struct dp8390_softc *);
161 void	we_mediastatus(struct dp8390_softc *, struct ifmediareq *);
162 
163 void	we_recv_int(struct dp8390_softc *);
164 void	we_init_card(struct dp8390_softc *);
165 int	we_write_mbuf(struct dp8390_softc *, struct mbuf *, int);
166 int	we_ring_copy(struct dp8390_softc *, int, caddr_t, u_short);
167 void	we_read_hdr(struct dp8390_softc *, int, struct dp8390_ring *);
168 int	we_test_mem(struct dp8390_softc *);
169 
170 __inline void we_readmem(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 alpha
690 #define ALIGNED_POINTER(p,t)	((((u_long)(p)) & (sizeof(t)-1)) == 0)
691 #endif
692 			} else if (ALIGNED_POINTER(data, u_int16_t) == 0) {
693 				/*
694 				 * Unaligned dta; buffer the next byte.
695 				 */
696 				savebyte[0] = *data++;
697 				len--;
698 				leftover = 1;
699 			} else {
700 				/*
701 				 * Aligned data; output contiguous words as
702 				 * much as we can, then buffer the remaining
703 				 * byte, if any.
704 				 */
705 				leftover = len & 1;
706 				len &= ~1;
707 				bus_space_write_region_stream_2(memt, memh,
708 				    buf, (u_int16_t *)data, len >> 1);
709 				data += len;
710 				buf += len;
711 				if (leftover)
712 					savebyte[0] = *data++;
713 				len = 0;
714 			}
715 		}
716 		if (len < 0)
717 			panic("we_write_mbuf: negative len");
718 #ifdef DIAGNOSTIC
719 		if (data != lim)
720 			panic("we_write_mbuf: data != lim");
721 #endif
722 	}
723 	if (leftover) {
724 		savebyte[1] = 0;
725 		bus_space_write_stream_2(memt, memh, buf,
726 		    *(u_int16_t *)savebyte);
727 	}
728 
729  out:
730 	WE_MEM_DISABLE(wsc);
731 
732 	return (savelen);
733 }
734 
735 int
736 we_ring_copy(sc, src, dst, amount)
737 	struct dp8390_softc *sc;
738 	int src;
739 	caddr_t dst;
740 	u_short amount;
741 {
742 	struct we_softc *wsc = (struct we_softc *)sc;
743 	u_short tmp_amount;
744 
745 	/* Does copy wrap to lower addr in ring buffer? */
746 	if (src + amount > sc->mem_end) {
747 		tmp_amount = sc->mem_end - src;
748 
749 		/* Copy amount up to end of NIC memory. */
750 		we_readmem(wsc, src, dst, tmp_amount);
751 
752 		amount -= tmp_amount;
753 		src = sc->mem_ring;
754 		dst += tmp_amount;
755 	}
756 
757 	we_readmem(wsc, src, dst, amount);
758 
759 	return (src + amount);
760 }
761 
762 void
763 we_read_hdr(sc, packet_ptr, packet_hdrp)
764 	struct dp8390_softc *sc;
765 	int packet_ptr;
766 	struct dp8390_ring *packet_hdrp;
767 {
768 	struct we_softc *wsc = (struct we_softc *)sc;
769 
770 	we_readmem(wsc, packet_ptr, (u_int8_t *)packet_hdrp,
771 	    sizeof(struct dp8390_ring));
772 #if BYTE_ORDER == BIG_ENDIAN
773 	packet_hdrp->count = swap16(packet_hdrp->count);
774 #endif
775 }
776 
777 void
778 we_recv_int(sc)
779 	struct dp8390_softc *sc;
780 {
781 	struct we_softc *wsc = (struct we_softc *)sc;
782 
783 	WE_MEM_ENABLE(wsc);
784 	dp8390_rint(sc);
785 	WE_MEM_DISABLE(wsc);
786 }
787 
788 void
789 we_media_init(struct dp8390_softc *sc)
790 {
791 	struct we_softc *wsc = (void *)sc;
792 	int defmedia = IFM_ETHER;
793 	u_int8_t x;
794 
795 	if (sc->is790) {
796 		x = bus_space_read_1(wsc->sc_asict, wsc->sc_asich, WE790_HWR);
797 		bus_space_write_1(wsc->sc_asict, wsc->sc_asich, WE790_HWR,
798 		    x | WE790_HWR_SWH);
799 		if (bus_space_read_1(wsc->sc_asict, wsc->sc_asich, WE790_GCR) &
800 		    WE790_GCR_GPOUT)
801 			defmedia |= IFM_10_2;
802 		else
803 			defmedia |= IFM_10_5;
804 		bus_space_write_1(wsc->sc_asict, wsc->sc_asich, WE790_HWR,
805 		    x &~ WE790_HWR_SWH);
806 	} else {
807 		x = bus_space_read_1(wsc->sc_asict, wsc->sc_asich, WE_IRR);
808 		if (x & WE_IRR_OUT2)
809 			defmedia |= IFM_10_2;
810 		else
811 			defmedia |= IFM_10_5;
812 	}
813 
814 	ifmedia_init(&sc->sc_media, 0, dp8390_mediachange, dp8390_mediastatus);
815 	ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_10_2, 0, NULL);
816 	ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_10_5, 0, NULL);
817 	ifmedia_set(&sc->sc_media, defmedia);
818 }
819 
820 int
821 we_mediachange(sc)
822 	struct dp8390_softc *sc;
823 {
824 
825 	/*
826 	 * Current media is already set up.  Just reset the interface
827 	 * to let the new value take hold.  The new media will be
828 	 * set up in we_init_card() called via dp8390_init().
829 	 */
830 	dp8390_reset(sc);
831 	return (0);
832 }
833 
834 void
835 we_mediastatus(sc, ifmr)
836 	struct dp8390_softc *sc;
837 	struct ifmediareq *ifmr;
838 {
839 	struct ifmedia *ifm = &sc->sc_media;
840 
841 	/*
842 	 * The currently selected media is always the active media.
843 	 */
844 	ifmr->ifm_active = ifm->ifm_cur->ifm_media;
845 }
846 
847 void
848 we_init_card(sc)
849 	struct dp8390_softc *sc;
850 {
851 	struct we_softc *wsc = (struct we_softc *)sc;
852 	struct ifmedia *ifm = &sc->sc_media;
853 
854 	we_set_media(wsc, ifm->ifm_cur->ifm_media);
855 }
856 
857 void
858 we_set_media(wsc, media)
859 	struct we_softc *wsc;
860 	int media;
861 {
862 	struct dp8390_softc *sc = &wsc->sc_dp8390;
863 	bus_space_tag_t asict = wsc->sc_asict;
864 	bus_space_handle_t asich = wsc->sc_asich;
865 	u_int8_t hwr, gcr, irr;
866 
867 	if (sc->is790) {
868 		hwr = bus_space_read_1(asict, asich, WE790_HWR);
869 		bus_space_write_1(asict, asich, WE790_HWR,
870 		    hwr | WE790_HWR_SWH);
871 		gcr = bus_space_read_1(asict, asich, WE790_GCR);
872 		if (IFM_SUBTYPE(media) == IFM_10_2)
873 			gcr |= WE790_GCR_GPOUT;
874 		else
875 			gcr &= ~WE790_GCR_GPOUT;
876 		bus_space_write_1(asict, asich, WE790_GCR,
877 		    gcr | WE790_GCR_LIT);
878 		bus_space_write_1(asict, asich, WE790_HWR,
879 		    hwr & ~WE790_HWR_SWH);
880 		return;
881 	}
882 
883 	irr = bus_space_read_1(wsc->sc_asict, wsc->sc_asich, WE_IRR);
884 	if (IFM_SUBTYPE(media) == IFM_10_2)
885 		irr |= WE_IRR_OUT2;
886 	else
887 		irr &= ~WE_IRR_OUT2;
888 	bus_space_write_1(wsc->sc_asict, wsc->sc_asich, WE_IRR, irr);
889 }
890 
891 const char *
892 we_params(asict, asich, typep, memsizep, is16bitp, is790p)
893 	bus_space_tag_t asict;
894 	bus_space_handle_t asich;
895 	u_int8_t *typep;
896 	bus_size_t *memsizep;
897 	int *is16bitp, *is790p;
898 {
899 	const char *typestr;
900 	bus_size_t memsize;
901 	int is16bit, is790;
902 	u_int8_t type;
903 
904 	memsize = 8192;
905 	is16bit = is790 = 0;
906 
907 	type = bus_space_read_1(asict, asich, WE_CARD_ID);
908 	switch (type) {
909 	case WE_TYPE_WD8003S:
910 		typestr = "WD8003S";
911 		break;
912 	case WE_TYPE_WD8003E:
913 		typestr = "WD8003E";
914 		break;
915 	case WE_TYPE_WD8003EB:
916 		typestr = "WD8003EB";
917 		break;
918 	case WE_TYPE_WD8003W:
919 		typestr = "WD8003W";
920 		break;
921 	case WE_TYPE_WD8013EBT:
922 		typestr = "WD8013EBT";
923 		memsize = 16384;
924 		is16bit = 1;
925 		break;
926 	case WE_TYPE_WD8013W:
927 		typestr = "WD8013W";
928 		memsize = 16384;
929 		is16bit = 1;
930 		break;
931 	case WE_TYPE_WD8013EP:		/* also WD8003EP */
932 		if (bus_space_read_1(asict, asich, WE_ICR) & WE_ICR_16BIT) {
933 			is16bit = 1;
934 			memsize = 16384;
935 			typestr = "WD8013EP";
936 		} else
937 			typestr = "WD8003EP";
938 		break;
939 	case WE_TYPE_WD8013WC:
940 		typestr = "WD8013WC";
941 		memsize = 16384;
942 		is16bit = 1;
943 		break;
944 	case WE_TYPE_WD8013EBP:
945 		typestr = "WD8013EBP";
946 		memsize = 16384;
947 		is16bit = 1;
948 		break;
949 	case WE_TYPE_WD8013EPC:
950 		typestr = "WD8013EPC";
951 		memsize = 16384;
952 		is16bit = 1;
953 		break;
954 	case WE_TYPE_SMC8216C:
955 	case WE_TYPE_SMC8216T:
956 	    {
957 		u_int8_t hwr;
958 
959 		typestr = (type == WE_TYPE_SMC8216C) ?
960 		    "SMC8216/SMC8216C" : "SMC8216T";
961 
962 		hwr = bus_space_read_1(asict, asich, WE790_HWR);
963 		bus_space_write_1(asict, asich, WE790_HWR,
964 		    hwr | WE790_HWR_SWH);
965 		switch (bus_space_read_1(asict, asich, WE790_RAR) &
966 		    WE790_RAR_SZ64) {
967 		case WE790_RAR_SZ64:
968 			memsize = 65536;
969 			break;
970 		case WE790_RAR_SZ32:
971 			memsize = 32768;
972 			break;
973 		case WE790_RAR_SZ16:
974 			memsize = 16384;
975 			break;
976 		case WE790_RAR_SZ8:
977 			/* 8216 has 16K shared mem -- 8416 has 8K */
978 			typestr = (type == WE_TYPE_SMC8216C) ?
979 			    "SMC8416C/SMC8416BT" : "SMC8416T";
980 			memsize = 8192;
981 			break;
982 		}
983 		bus_space_write_1(asict, asich, WE790_HWR, hwr);
984 
985 		is16bit = 1;
986 		is790 = 1;
987 		break;
988 	    }
989 #ifdef TOSH_ETHER
990 	case WE_TYPE_TOSHIBA1:
991 		typestr = "Toshiba1";
992 		memsize = 32768;
993 		is16bit = 1;
994 		break;
995 	case WE_TYPE_TOSHIBA4:
996 		typestr = "Toshiba4";
997 		memsize = 32768;
998 		is16bit = 1;
999 		break;
1000 #endif
1001 	default:
1002 		/* Not one we recognize. */
1003 		return (NULL);
1004 	}
1005 
1006 	/*
1007 	 * Make some adjustments to initial values depending on what is
1008 	 * found in the ICR.
1009 	 */
1010 	if (is16bit && (type != WE_TYPE_WD8013EBT) &&
1011 #ifdef TOSH_ETHER
1012 	    (type != WE_TYPE_TOSHIBA1 && type != WE_TYPE_TOSHIBA4) &&
1013 #endif
1014 	    (bus_space_read_1(asict, asich, WE_ICR) & WE_ICR_16BIT) == 0) {
1015 		is16bit = 0;
1016 		memsize = 8192;
1017 	}
1018 
1019 #ifdef WE_DEBUG
1020 	{
1021 		int i;
1022 
1023 		printf("we_params: type = 0x%x, typestr = %s, is16bit = %d, "
1024 		    "memsize = %d\n", type, typestr, is16bit, memsize);
1025 		for (i = 0; i < 8; i++)
1026 			printf("     %d -> 0x%x\n", i,
1027 			    bus_space_read_1(asict, asich, i));
1028 	}
1029 #endif
1030 
1031 	if (typep != NULL)
1032 		*typep = type;
1033 	if (memsizep != NULL)
1034 		*memsizep = memsize;
1035 	if (is16bitp != NULL)
1036 		*is16bitp = is16bit;
1037 	if (is790p != NULL)
1038 		*is790p = is790;
1039 	return (typestr);
1040 }
1041