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