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