xref: /openbsd-src/sys/dev/isa/if_ec.c (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1 /*	$OpenBSD: if_ec.c,v 1.3 2001/03/12 05:36:59 aaron Exp $	*/
2 /*	$NetBSD: if_ec.c,v 1.9 1998/07/05 06:49:12 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 3Com Etherlink II (3c503).
56  */
57 
58 #include "bpfilter.h"
59 
60 #include <sys/param.h>
61 #include <sys/systm.h>
62 #include <sys/device.h>
63 #include <sys/socket.h>
64 #include <sys/mbuf.h>
65 #include <sys/syslog.h>
66 
67 #include <net/if.h>
68 #include <net/if_dl.h>
69 #include <net/if_types.h>
70 #include <net/if_media.h>
71 
72 #ifdef __NetBSD__
73 #include <net/if_ether.h>
74 #endif
75 
76 #ifdef INET
77 #include <netinet/in.h>
78 #include <netinet/in_systm.h>
79 #include <netinet/in_var.h>
80 #include <netinet/ip.h>
81 #ifdef __NetBSD__
82 #include <netinet/if_inarp.h>
83 #else
84 #include <netinet/if_ether.h>
85 #endif
86 #endif
87 
88 #ifdef NS
89 #include <netns/ns.h>
90 #include <netns/ns_if.h>
91 #endif
92 
93 #if NBPFILTER > 0
94 #include <net/bpf.h>
95 #include <net/bpfdesc.h>
96 #endif
97 
98 #include <machine/bus.h>
99 #include <machine/intr.h>
100 
101 #include <dev/isa/isareg.h>
102 #include <dev/isa/isavar.h>
103 
104 #include <dev/ic/dp8390reg.h>
105 #include <dev/ic/dp8390var.h>
106 
107 #include <dev/isa/if_ecreg.h>
108 
109 struct ec_softc {
110 	struct dp8390_softc sc_dp8390;
111 
112 	bus_space_tag_t sc_asict;	/* space tag for ASIC */
113 	bus_space_handle_t sc_asich;	/* space handle for ASIC */
114 
115 	int sc_16bitp;			/* are we 16 bit? */
116 
117 	void *sc_ih;			/* interrupt handle */
118 };
119 
120 int	ec_probe __P((struct device *, void *, void *));
121 void	ec_attach __P((struct device *, struct device *, void *));
122 
123 struct cfattach ec_ca = {
124 	sizeof(struct ec_softc), ec_probe, ec_attach
125 };
126 
127 int	ec_set_media __P((struct ec_softc *, int));
128 
129 void	ec_media_init __P((struct dp8390_softc *));
130 
131 int	ec_mediachange __P((struct dp8390_softc *));
132 void	ec_mediastatus __P((struct dp8390_softc *, struct ifmediareq *));
133 
134 void	ec_init_card __P((struct dp8390_softc *));
135 int	ec_write_mbuf __P((struct dp8390_softc *, struct mbuf *, int));
136 int	ec_ring_copy __P((struct dp8390_softc *, int, caddr_t, u_short));
137 void	ec_read_hdr __P((struct dp8390_softc *, int, struct dp8390_ring *));
138 int	ec_fake_test_mem __P((struct dp8390_softc *));
139 int	ec_test_mem __P((struct dp8390_softc *));
140 
141 __inline void ec_readmem __P((struct ec_softc *, int, u_int8_t *, int));
142 
143 static const int ec_iobase[] = {
144 	0x2e0, 0x2a0, 0x280, 0x250, 0x350, 0x330, 0x310, 0x300,
145 };
146 #define	NEC_IOBASE	(sizeof(ec_iobase) / sizeof(ec_iobase[0]))
147 
148 static const int ec_membase[] = {
149 	MADDRUNK, MADDRUNK, MADDRUNK, MADDRUNK, 0xc8000, 0xcc000,
150 	0xd8000, 0xdc000,
151 };
152 #define	NEC_MEMBASE	(sizeof(ec_membase) / sizeof(ec_membase[0]))
153 
154 struct cfdriver ec_cd = {
155 	NULL, "ec", DV_IFNET
156 };
157 
158 int
159 ec_probe(parent, match, aux)
160 	struct device *parent;
161 	void *match, *aux;
162 {
163 	struct isa_attach_args *ia = aux;
164 	bus_space_tag_t nict, asict, memt;
165 	bus_space_handle_t nich, asich, memh;
166 	bus_size_t memsize;
167 	int nich_valid, asich_valid, memh_valid;
168 	int i, rv = 0;
169 	u_int8_t x;
170 
171 	nict = asict = ia->ia_iot;
172 	memt = ia->ia_memt;
173 
174 	nich_valid = asich_valid = memh_valid = 0;
175 
176 	/*
177 	 * Hmm, a 16-bit card has 16k of memory, but only an 8k window
178 	 * to it.
179 	 */
180 	memsize = 8192;
181 
182 	/* Disallow wildcarded i/o addresses. */
183 	if (ia->ia_iobase == -1 /* ISACF_PORT_DEFAULT */)
184 		return (0);
185 
186 	/* Disallow wildcarded mem address. */
187 	if (ia->ia_maddr == -1 /* ISACF_IOMEM_DEFAULT */)
188 		return (0);
189 
190 	/* Validate the i/o base. */
191 	for (i = 0; i < NEC_IOBASE; i++)
192 		if (ia->ia_iobase == ec_iobase[i])
193 			break;
194 	if (i == NEC_IOBASE)
195 		return (0);
196 
197 	/* Validate the mem base. */
198 	for (i = 0; i < NEC_MEMBASE; i++) {
199 		if (ec_membase[i] == MADDRUNK)
200 			continue;
201 		if (ia->ia_maddr == ec_membase[i])
202 			break;
203 	}
204 	if (i == NEC_MEMBASE)
205 		return (0);
206 
207 	/* Attempt to map the NIC space. */
208 	if (bus_space_map(nict, ia->ia_iobase + ELINK2_NIC_OFFSET,
209 	    ELINK2_NIC_PORTS, 0, &nich))
210 		goto out;
211 	nich_valid = 1;
212 
213 	/* Attempt to map the ASIC space. */
214 	if (bus_space_map(asict, ia->ia_iobase + ELINK2_ASIC_OFFSET,
215 	    ELINK2_ASIC_PORTS, 0, &asich))
216 		goto out;
217 	asich_valid = 1;
218 
219 	/* Attempt to map the memory space. */
220 	if (bus_space_map(memt, ia->ia_maddr, memsize, 0, &memh))
221 		goto out;
222 	memh_valid = 1;
223 
224 	/*
225 	 * Verify that the kernel configured I/O address matches the
226 	 * board configured I/O address.
227 	 *
228 	 * This is really only useful to see if something that looks like
229 	 * the board is there; after all, we're already talking to it at
230 	 * this point.
231 	 */
232 	x = bus_space_read_1(asict, asich, ELINK2_BCFR);
233 	if (x == 0 || (x & (x - 1)) != 0)
234 		goto out;
235 	i = ffs(x) - 1;
236 	if (ia->ia_iobase != ec_iobase[i])
237 		goto out;
238 
239 	/*
240 	 * ...and for the memory address.  Note we do not support
241 	 * cards configured with shared memory disabled.
242 	 */
243 	x = bus_space_read_1(asict, asich, ELINK2_PCFR);
244 	if (x == 0 || (x & (x - 1)) != 0)
245 		goto out;
246 	i = ffs(x) - 1;
247 	if (ia->ia_maddr != ec_membase[i])
248 		goto out;
249 
250 	/* So, we say we've found it! */
251 	ia->ia_iosize = ELINK2_NIC_PORTS;
252 	ia->ia_msize = memsize;
253 	rv = 1;
254 
255  out:
256 	if (nich_valid)
257 		bus_space_unmap(nict, nich, ELINK2_NIC_PORTS);
258 	if (asich_valid)
259 		bus_space_unmap(asict, asich, ELINK2_ASIC_PORTS);
260 	if (memh_valid)
261 		bus_space_unmap(memt, memh, memsize);
262 	return (rv);
263 }
264 
265 void
266 ec_attach(parent, self, aux)
267 	struct device *parent, *self;
268 	void *aux;
269 {
270 	struct ec_softc *esc = (struct ec_softc *)self;
271 	struct dp8390_softc *sc = &esc->sc_dp8390;
272 	struct isa_attach_args *ia = aux;
273 	bus_space_tag_t nict, asict, memt;
274 	bus_space_handle_t nich, asich, memh;
275 	bus_size_t memsize;
276 	u_int8_t tmp;
277 	int i;
278 
279 	printf("\n");
280 
281 	nict = asict = ia->ia_iot;
282 	memt = ia->ia_memt;
283 
284 	/*
285 	 * Hmm, a 16-bit card has 16k of memory, but only an 8k window
286 	 * to it.
287 	 */
288 	memsize = 8192;
289 
290 	/* Map the NIC space. */
291 	if (bus_space_map(nict, ia->ia_iobase + ELINK2_NIC_OFFSET,
292 	    ELINK2_NIC_PORTS, 0, &nich)) {
293 		printf("%s: can't map nic i/o space\n",
294 		    sc->sc_dev.dv_xname);
295 		return;
296 	}
297 
298 	/* Map the ASIC space. */
299 	if (bus_space_map(asict, ia->ia_iobase + ELINK2_ASIC_OFFSET,
300 	    ELINK2_ASIC_PORTS, 0, &asich)) {
301 		printf("%s: can't map asic i/o space\n",
302 		    sc->sc_dev.dv_xname);
303 		return;
304 	}
305 
306 	/* Map the memory space. */
307 	if (bus_space_map(memt, ia->ia_maddr, memsize, 0, &memh)) {
308 		printf("%s: can't map shared memory\n",
309 		    sc->sc_dev.dv_xname);
310 		return;
311 	}
312 
313 	esc->sc_asict = asict;
314 	esc->sc_asich = asich;
315 
316 	sc->sc_regt = nict;
317 	sc->sc_regh = nich;
318 
319 	sc->sc_buft = memt;
320 	sc->sc_bufh = memh;
321 
322 	/* Interface is always enabled. */
323 	sc->sc_enabled = 1;
324 
325 	/* Registers are linear. */
326 	for (i = 0; i < 16; i++)
327 		sc->sc_reg_map[i] = i;
328 
329 	/* Now we can use the NIC_{GET,PUT}() macros. */
330 
331 	/*
332 	 * Reset NIC and ASIC.  Enable on-board transeiver throughout
333 	 * reset sequence since it will lock up if the cable isn't
334 	 * connected if we don't.
335 	 */
336 	bus_space_write_1(asict, asich, ELINK2_CR,
337 	    ELINK2_CR_RST | ELINK2_CR_XSEL);
338 
339 	/* Wait for a while, then un-reset it. */
340 	delay(50);
341 
342 	/*
343 	 * The 3Com ASIC defaults to rather strange settings for the CR
344 	 * after a reset.  It's important to set it again after the
345 	 * following write (this is done when we map the PROM below).
346 	 */
347 	bus_space_write_1(asict, asich, ELINK2_CR, ELINK2_CR_XSEL);
348 
349 	/* Wait a bit for the NIC to recover from the reset. */
350 	delay(5000);
351 
352 	/*
353 	 * Get the station address from on-board ROM.
354 	 *
355 	 * First, map Ethernet address PROM over the top of where the NIC
356 	 * registers normally appear.
357 	 */
358 	bus_space_write_1(asict, asich, ELINK2_CR,
359 	    ELINK2_CR_XSEL | ELINK2_CR_EALO);
360 
361 	for (i = 0; i < ETHER_ADDR_LEN; i++)
362 #ifdef __NetBSD__
363 		sc->sc_enaddr[i] = NIC_GET(nict, nich, i);
364 #else
365 		sc->sc_arpcom.ac_enaddr[i] = NIC_GET(nict, nich, i);
366 #endif
367 
368 	/*
369 	 * Unmap PROM - select NIC registers.  The proper setting of the
370 	 * transciever is set in later in ec_init_card() via dp8390_init().
371 	 */
372 	bus_space_write_1(asict, asich, ELINK2_CR, ELINK2_CR_XSEL);
373 
374 	/* Determine if this is an 8-bit or 16-bit board. */
375 
376 	/* Select page 0 registers. */
377 	NIC_PUT(nict, nich, ED_P0_CR, ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STP);
378 
379 	/*
380 	 * Attempt to clear WTS.  If it doesn't clear, then this is a
381 	 * 16-bit board.
382 	 */
383 	NIC_PUT(nict, nich, ED_P0_DCR, 0);
384 
385 	/* Select page 2 registers. */
386 	NIC_PUT(nict, nich, ED_P0_CR, ED_CR_RD2 | ED_CR_PAGE_2 | ED_CR_STP);
387 
388 	/* The 3c503 forces the WTS bit to a one if this is a 16-bit board. */
389 	if (NIC_GET(nict, nich, ED_P2_DCR) & ED_DCR_WTS)
390 		esc->sc_16bitp = 1;
391 	else
392 		esc->sc_16bitp = 0;
393 
394 	printf("%s: 3Com 3c503 Ethernet (%s-bit)\n",
395 	    sc->sc_dev.dv_xname, esc->sc_16bitp ? "16" : "8");
396 
397 	/* Select page 0 registers. */
398 	NIC_PUT(nict, nich, ED_P2_CR, ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STP);
399 
400 	sc->cr_proto = ED_CR_RD2;
401 
402 	/*
403 	 * DCR gets:
404 	 *
405 	 *	FIFO threshold to 8, No auto-init Remote DMA,
406 	 *	byte order=80x86.
407 	 *
408 	 * 16-bit cards also get word-wide DMA transfers.
409 	 */
410 	sc->dcr_reg = ED_DCR_FT1 | ED_DCR_LS |
411 	    (esc->sc_16bitp ? ED_DCR_WTS : 0);
412 
413 	sc->test_mem = ec_fake_test_mem;
414 	sc->ring_copy = ec_ring_copy;
415 	sc->write_mbuf = ec_write_mbuf;
416 	sc->read_hdr = ec_read_hdr;
417 
418 	sc->sc_media_init = ec_media_init;
419 
420 	sc->sc_mediachange = ec_mediachange;
421 	sc->sc_mediastatus = ec_mediastatus;
422 
423 	sc->mem_start = 0;
424 	sc->mem_size = memsize;
425 
426 	/* Do generic parts of attach. */
427 	if (dp8390_config(sc)) {
428 		printf("%s: configuration failed\n", sc->sc_dev.dv_xname);
429 		return;
430 	}
431 
432 	/*
433 	 * We need to override the way dp8390_config() set up our
434 	 * shared memory.
435 	 *
436 	 * We have an entire 8k window to put the transmit buffers on the
437 	 * 16-bit boards.  But since the 16bit 3c503's shared memory is only
438 	 * fast enough to overlap the loading of one full-size packet, trying
439 	 * to load more than 2 buffers can actually leave the transmitter idle
440 	 * during the load.  So 2 seems the best value.  (Although a mix of
441 	 * variable-sized packets might change this assumption.  Nonetheless,
442 	 * we optimize for linear transfers of same-size packets.)
443 	 */
444 	if (esc->sc_16bitp) {
445 		if (sc->sc_dev.dv_cfdata->cf_flags & DP8390_NO_MULTI_BUFFERING)
446 			sc->txb_cnt = 1;
447 		else
448 			sc->txb_cnt = 2;
449 
450 		sc->tx_page_start = ELINK2_TX_PAGE_OFFSET_16BIT;
451 		sc->rec_page_start = ELINK2_RX_PAGE_OFFSET_16BIT;
452 		sc->rec_page_stop = (memsize >> ED_PAGE_SHIFT) +
453 		    sc->rec_page_start;
454 		sc->mem_ring = sc->mem_start;
455 	} else {
456 		sc->txb_cnt = 1;
457 		sc->tx_page_start = ELINK2_TX_PAGE_OFFSET_8BIT;
458 		sc->rec_page_start = sc->tx_page_start + ED_TXBUF_SIZE;
459 		sc->rec_page_stop = (memsize >> ED_PAGE_SHIFT) +
460 		    sc->tx_page_start;
461 		sc->mem_ring = sc->mem_start +
462 		    (ED_TXBUF_SIZE << ED_PAGE_SHIFT);
463 	}
464 
465 	/*
466 	 * Initialize CA page start/stop registers.  Probably only needed
467 	 * if doing DMA, but what the Hell.
468 	 */
469 	bus_space_write_1(asict, asich, ELINK2_PSTR, sc->rec_page_start);
470 	bus_space_write_1(asict, asich, ELINK2_PSPR, sc->rec_page_stop);
471 
472 	/*
473 	 * Program the IRQ.
474 	 */
475 	switch (ia->ia_irq) {
476 	case 9:	tmp = ELINK2_IDCFR_IRQ2; break;
477 	case 3:	tmp = ELINK2_IDCFR_IRQ3; break;
478 	case 4:	tmp = ELINK2_IDCFR_IRQ4; break;
479 	case 5:	tmp = ELINK2_IDCFR_IRQ5; break;
480 		break;
481 
482 	case IRQUNK:
483 		printf("%s: wildcarded IRQ is not allowed\n",
484 		    sc->sc_dev.dv_xname);
485 		return;
486 
487 	default:
488 		printf("%s: invalid IRQ %d, must be 3, 4, 5, or 9\n",
489 		    sc->sc_dev.dv_xname, ia->ia_irq);
490 		return;
491 	}
492 
493 	bus_space_write_1(asict, asich, ELINK2_IDCFR, tmp);
494 
495 	/*
496 	 * Initialize the GA configuration register.  Set bank and enable
497 	 * shared memory.
498 	 */
499 	bus_space_write_1(asict, asich, ELINK2_GACFR,
500 	    ELINK2_GACFR_RSEL | ELINK2_GACFR_MBS0);
501 
502 	/*
503 	 * Intialize "Vector Pointer" registers.  These gawd-awful things
504 	 * are compared to 20 bits of the address on the ISA, and if they
505 	 * match, the shared memory is disabled.  We se them to 0xffff0...
506 	 * allegedly the reset vector.
507 	 */
508 	bus_space_write_1(asict, asich, ELINK2_VPTR2, 0xff);
509 	bus_space_write_1(asict, asich, ELINK2_VPTR1, 0xff);
510 	bus_space_write_1(asict, asich, ELINK2_VPTR0, 0x00);
511 
512 	/*
513 	 * Now run the real memory test.
514 	 */
515 	if (ec_test_mem(sc)) {
516 		printf("%s: memory test failed\n", sc->sc_dev.dv_xname);
517 		return;
518 	}
519 
520 	/* Establish interrupt handler. */
521 	esc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_EDGE,
522 	    IPL_NET, dp8390_intr, sc, sc->sc_dev.dv_xname);
523 	if (esc->sc_ih == NULL)
524 		printf("%s: can't establish interrupt\n", sc->sc_dev.dv_xname);
525 }
526 
527 int
528 ec_fake_test_mem(sc)
529 	struct dp8390_softc *sc;
530 {
531 
532 	/*
533 	 * We have to do this after we initialize the GA, but we
534 	 * have to do that after calling dp8390_config(), which
535 	 * wants to test memory.  Put this noop here, and then
536 	 * actually test memory later.
537 	 */
538 	return (0);
539 }
540 
541 int
542 ec_test_mem(sc)
543 	struct dp8390_softc *sc;
544 {
545 	struct ec_softc *esc = (struct ec_softc *)sc;
546 	bus_space_tag_t memt = sc->sc_buft;
547 	bus_space_handle_t memh = sc->sc_bufh;
548 	bus_size_t memsize = sc->mem_size;
549 	int i;
550 
551 	if (esc->sc_16bitp)
552 		bus_space_set_region_2(memt, memh, 0, 0, memsize >> 1);
553 	else
554 		bus_space_set_region_1(memt, memh, 0, 0, memsize);
555 
556 	if (esc->sc_16bitp) {
557 		for (i = 0; i < memsize; i += 2) {
558 			if (bus_space_read_2(memt, memh, i) != 0)
559 				goto fail;
560 		}
561 	} else {
562 		for (i = 0; i < memsize; i++) {
563 			if (bus_space_read_1(memt, memh, i) != 0)
564 				goto fail;
565 		}
566 	}
567 
568 	return (0);
569 
570  fail:
571 	printf("%s: failed to clear shared memory at offset 0x%x\n",
572 	    sc->sc_dev.dv_xname, i);
573 	return (1);
574 }
575 
576 /*
577  * Given a NIC memory source address and a host memory destination address,
578  * copy 'len' from NIC to host using shared memory.  The 'len' is rounded
579  * up to a word - ok as long as mbufs are word-sized.
580  */
581 __inline void
582 ec_readmem(esc, from, to, len)
583 	struct ec_softc *esc;
584 	int from;
585 	u_int8_t *to;
586 	int len;
587 {
588 	bus_space_tag_t memt = esc->sc_dp8390.sc_buft;
589 	bus_space_handle_t memh = esc->sc_dp8390.sc_bufh;
590 
591 	if (len & 1)
592 		++len;
593 
594 	if (esc->sc_16bitp)
595 		bus_space_read_region_2(memt, memh, from, (u_int16_t *)to,
596 		    len >> 1);
597 	else
598 		bus_space_read_region_1(memt, memh, from, to, len);
599 }
600 
601 int
602 ec_write_mbuf(sc, m, buf)
603 	struct dp8390_softc *sc;
604 	struct mbuf *m;
605 	int buf;
606 {
607 	struct ec_softc *esc = (struct ec_softc *)sc;
608 	bus_space_tag_t asict = esc->sc_asict;
609 	bus_space_handle_t asich = esc->sc_asich;
610 	bus_space_tag_t memt = esc->sc_dp8390.sc_buft;
611 	bus_space_handle_t memh = esc->sc_dp8390.sc_bufh;
612 	u_int8_t *data, savebyte[2];
613 	int savelen, len, leftover;
614 #ifdef DIAGNOSTIC
615 	u_int8_t *lim;
616 #endif
617 
618 	savelen = m->m_pkthdr.len;
619 
620 	/*
621 	 * 8-bit boards are simple: we're already in the correct
622 	 * page, and no alignment tricks are necessary.
623 	 */
624 	if (esc->sc_16bitp == 0) {
625 		for (; m != NULL; buf += m->m_len, m = m->m_next)
626 			bus_space_write_region_1(memt, memh, buf,
627 			    mtod(m, u_int8_t *), m->m_len);
628 		return (savelen);
629 	}
630 
631 	/*
632 	 * If it's a 16-bit board, we have transmit buffers
633 	 * in a different page; switch to it.
634 	 */
635 	if (esc->sc_16bitp)
636 		bus_space_write_1(asict, asich, ELINK2_GACFR,
637 		    ELINK2_GACFR_RSEL);
638 
639 	/* Start out with no leftover data. */
640 	leftover = 0;
641 	savebyte[0] = savebyte[1] = 0;
642 
643 	for (; m != NULL; m = m->m_next) {
644 		len = m->m_len;
645 		if (len == 0)
646 			continue;
647 		data = mtod(m, u_int8_t *);
648 #ifdef DIAGNOSTIC
649 		lim = data + len;
650 #endif
651 		while (len > 0) {
652 			if (leftover) {
653 				/*
654 				 * Data left over (from mbuf or realignment).
655 				 * Buffer the next byte, and write it and
656 				 * the leftover data out.
657 				 */
658 				savebyte[1] = *data++;
659 				len--;
660 				bus_space_write_2(memt, memh, buf,
661 				    *(u_int16_t *)savebyte);
662 				buf += 2;
663 				leftover = 0;
664 #ifdef i386
665 #define ALIGNED_POINTER(p,t)	1
666 #endif
667 #ifdef alpha
668 #define ALIGNED_POINTER(p,t)	((((u_long)(p)) & (sizeof(t)-1)) == 0)
669 #endif
670 			} else if (ALIGNED_POINTER(data, u_int16_t) == 0) {
671 				/*
672 				 * Unaligned data; buffer the next byte.
673 				 */
674 				savebyte[0] = *data++;
675 				len--;
676 				leftover = 1;
677 			} else {
678 				/*
679 				 * Aligned data; output contiguous words as
680 				 * much as we can, then buffer the remaining
681 				 * byte, if any.
682 				 */
683 				leftover = len & 1;
684 				len &= ~1;
685 				bus_space_write_region_2(memt, memh, buf,
686 				    (u_int16_t *)data, len >> 1);
687 				data += len;
688 				buf += len;
689 				if (leftover)
690 					savebyte[0] = *data++;
691 				len = 0;
692 			}
693 		}
694 		if (len < 0)
695 			panic("ec_write_mbuf: negative len");
696 #ifdef DIAGNOSTIC
697 		if (data != lim)
698 			panic("ec_write_mbuf: data != lim");
699 #endif
700 	}
701 	if (leftover) {
702 		savebyte[1] = 0;
703 		bus_space_write_2(memt, memh, buf, *(u_int16_t *)savebyte);
704 	}
705 
706 	/*
707 	 * Switch back to receive page.
708 	 */
709 	if (esc->sc_16bitp)
710 		bus_space_write_1(asict, asich, ELINK2_GACFR,
711 		    ELINK2_GACFR_RSEL | ELINK2_GACFR_MBS0);
712 
713 	return (savelen);
714 }
715 
716 int
717 ec_ring_copy(sc, src, dst, amount)
718 	struct dp8390_softc *sc;
719 	int src;
720 	caddr_t dst;
721 	u_short amount;
722 {
723 	struct ec_softc *esc = (struct ec_softc *)sc;
724 	u_short tmp_amount;
725 
726 	/* Does copy wrap to lower addr in ring buffer? */
727 	if (src + amount > sc->mem_end) {
728 		tmp_amount = sc->mem_end - src;
729 
730 		/* Copy amount up to end of NIC memory. */
731 		ec_readmem(esc, src, dst, tmp_amount);
732 
733 		amount -= tmp_amount;
734 		src = sc->mem_ring;
735 		dst += tmp_amount;
736 	}
737 
738 	ec_readmem(esc, src, dst, amount);
739 
740 	return (src + amount);
741 }
742 
743 void
744 ec_read_hdr(sc, packet_ptr, packet_hdrp)
745 	struct dp8390_softc *sc;
746 	int packet_ptr;
747 	struct dp8390_ring *packet_hdrp;
748 {
749 	struct ec_softc *esc = (struct ec_softc *)sc;
750 
751 	ec_readmem(esc, packet_ptr, (u_int8_t *)packet_hdrp,
752 	    sizeof(struct dp8390_ring));
753 #if BYTE_ORDER == BIG_ENDIAN
754 	packet_hdrp->count = swap16(packet_hdrp->count);
755 #endif
756 }
757 
758 void
759 ec_media_init(struct dp8390_softc *sc)
760 {
761 	ifmedia_init(&sc->sc_media, 0, dp8390_mediachange, dp8390_mediastatus);
762 	ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_10_2, 0, NULL);
763 	ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_10_5, 0, NULL);
764 	ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_10_2);
765 }
766 
767 int
768 ec_mediachange(sc)
769 	struct dp8390_softc *sc;
770 {
771 	struct ec_softc *esc = (struct ec_softc *)sc;
772 	struct ifmedia *ifm = &sc->sc_media;
773 
774 	return (ec_set_media(esc, ifm->ifm_media));
775 }
776 
777 void
778 ec_mediastatus(sc, ifmr)
779 	struct dp8390_softc *sc;
780 	struct ifmediareq *ifmr;
781 {
782 	struct ifmedia *ifm = &sc->sc_media;
783 
784 	/*
785 	 * The currently selected media is always the active media.
786 	 */
787 	ifmr->ifm_active = ifm->ifm_cur->ifm_media;
788 }
789 
790 void
791 ec_init_card(sc)
792 	struct dp8390_softc *sc;
793 {
794 	struct ec_softc *esc = (struct ec_softc *)sc;
795 	struct ifmedia *ifm = &sc->sc_media;
796 
797 	(void) ec_set_media(esc, ifm->ifm_cur->ifm_media);
798 }
799 
800 int
801 ec_set_media(esc, media)
802 	struct ec_softc *esc;
803 	int media;
804 {
805 	u_int8_t new;
806 
807 	if (IFM_TYPE(media) != IFM_ETHER)
808 		return (EINVAL);
809 
810 	switch (IFM_SUBTYPE(media)) {
811 	case IFM_10_2:
812 		new = ELINK2_CR_XSEL;
813 		break;
814 
815 	case IFM_10_5:
816 		new = 0;
817 		break;
818 
819 	default:
820 		return (EINVAL);
821 	}
822 
823 	bus_space_write_1(esc->sc_asict, esc->sc_asich, ELINK2_CR, new);
824 	return (0);
825 }
826