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