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