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