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