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