Home | History | Annotate | Line # | Download | only in isa
if_ec.c revision 1.11
      1 /*	$NetBSD: if_ec.c,v 1.11 1999/03/23 21:41:08 drochner 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 	sc->init_card = ec_init_card;
    412 
    413 	sc->sc_mediachange = ec_mediachange;
    414 	sc->sc_mediastatus = ec_mediastatus;
    415 
    416 	sc->mem_start = 0;
    417 	sc->mem_size = memsize;
    418 
    419 	/* Do generic parts of attach. */
    420 	if (dp8390_config(sc, ec_media, NEC_MEDIA, EC_DEFMEDIA)) {
    421 		printf("%s: configuration failed\n", sc->sc_dev.dv_xname);
    422 		return;
    423 	}
    424 
    425 	/*
    426 	 * We need to override the way dp8390_config() set up our
    427 	 * shared memory.
    428 	 *
    429 	 * We have an entire 8k window to put the transmit buffers on the
    430 	 * 16-bit boards.  But since the 16bit 3c503's shared memory is only
    431 	 * fast enough to overlap the loading of one full-size packet, trying
    432 	 * to load more than 2 buffers can actually leave the transmitter idle
    433 	 * during the load.  So 2 seems the best value.  (Although a mix of
    434 	 * variable-sized packets might change this assumption.  Nonetheless,
    435 	 * we optimize for linear transfers of same-size packets.)
    436 	 */
    437 	if (esc->sc_16bitp) {
    438 		if (sc->sc_dev.dv_cfdata->cf_flags & DP8390_NO_MULTI_BUFFERING)
    439 			sc->txb_cnt = 1;
    440 		else
    441 			sc->txb_cnt = 2;
    442 
    443 		sc->tx_page_start = ELINK2_TX_PAGE_OFFSET_16BIT;
    444 		sc->rec_page_start = ELINK2_RX_PAGE_OFFSET_16BIT;
    445 		sc->rec_page_stop = (memsize >> ED_PAGE_SHIFT) +
    446 		    sc->rec_page_start;
    447 		sc->mem_ring = sc->mem_start;
    448 	} else {
    449 		sc->txb_cnt = 1;
    450 		sc->tx_page_start = ELINK2_TX_PAGE_OFFSET_8BIT;
    451 		sc->rec_page_start = sc->tx_page_start + ED_TXBUF_SIZE;
    452 		sc->rec_page_stop = (memsize >> ED_PAGE_SHIFT) +
    453 		    sc->tx_page_start;
    454 		sc->mem_ring = sc->mem_start +
    455 		    (ED_TXBUF_SIZE << ED_PAGE_SHIFT);
    456 	}
    457 
    458 	/*
    459 	 * Initialize CA page start/stop registers.  Probably only needed
    460 	 * if doing DMA, but what the Hell.
    461 	 */
    462 	bus_space_write_1(asict, asich, ELINK2_PSTR, sc->rec_page_start);
    463 	bus_space_write_1(asict, asich, ELINK2_PSPR, sc->rec_page_stop);
    464 
    465 	/*
    466 	 * Program the IRQ.
    467 	 */
    468 	switch (ia->ia_irq) {
    469 	case 9:	tmp = ELINK2_IDCFR_IRQ2; break;
    470 	case 3:	tmp = ELINK2_IDCFR_IRQ3; break;
    471 	case 4:	tmp = ELINK2_IDCFR_IRQ4; break;
    472 	case 5:	tmp = ELINK2_IDCFR_IRQ5; break;
    473 		break;
    474 
    475 	case IRQUNK:
    476 		printf("%s: wildcarded IRQ is not allowed\n",
    477 		    sc->sc_dev.dv_xname);
    478 		return;
    479 
    480 	default:
    481 		printf("%s: invalid IRQ %d, must be 3, 4, 5, or 9\n",
    482 		    sc->sc_dev.dv_xname, ia->ia_irq);
    483 		return;
    484 	}
    485 
    486 	bus_space_write_1(asict, asich, ELINK2_IDCFR, tmp);
    487 
    488 	/*
    489 	 * Initialize the GA configuration register.  Set bank and enable
    490 	 * shared memory.
    491 	 */
    492 	bus_space_write_1(asict, asich, ELINK2_GACFR,
    493 	    ELINK2_GACFR_RSEL | ELINK2_GACFR_MBS0);
    494 
    495 	/*
    496 	 * Intialize "Vector Pointer" registers.  These gawd-awful things
    497 	 * are compared to 20 bits of the address on the ISA, and if they
    498 	 * match, the shared memory is disabled.  We se them to 0xffff0...
    499 	 * allegedly the reset vector.
    500 	 */
    501 	bus_space_write_1(asict, asich, ELINK2_VPTR2, 0xff);
    502 	bus_space_write_1(asict, asich, ELINK2_VPTR1, 0xff);
    503 	bus_space_write_1(asict, asich, ELINK2_VPTR0, 0x00);
    504 
    505 	/*
    506 	 * Now run the real memory test.
    507 	 */
    508 	if (ec_test_mem(sc)) {
    509 		printf("%s: memory test failed\n", sc->sc_dev.dv_xname);
    510 		return;
    511 	}
    512 
    513 	/* Establish interrupt handler. */
    514 	esc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_EDGE,
    515 	    IPL_NET, dp8390_intr, sc);
    516 	if (esc->sc_ih == NULL)
    517 		printf("%s: can't establish interrupt\n", sc->sc_dev.dv_xname);
    518 }
    519 
    520 int
    521 ec_fake_test_mem(sc)
    522 	struct dp8390_softc *sc;
    523 {
    524 
    525 	/*
    526 	 * We have to do this after we initialize the GA, but we
    527 	 * have to do that after calling dp8390_config(), which
    528 	 * wants to test memory.  Put this noop here, and then
    529 	 * actually test memory later.
    530 	 */
    531 	return (0);
    532 }
    533 
    534 int
    535 ec_test_mem(sc)
    536 	struct dp8390_softc *sc;
    537 {
    538 	struct ec_softc *esc = (struct ec_softc *)sc;
    539 	bus_space_tag_t memt = sc->sc_buft;
    540 	bus_space_handle_t memh = sc->sc_bufh;
    541 	bus_size_t memsize = sc->mem_size;
    542 	int i;
    543 
    544 	if (esc->sc_16bitp)
    545 		bus_space_set_region_2(memt, memh, 0, 0, memsize >> 1);
    546 	else
    547 		bus_space_set_region_1(memt, memh, 0, 0, memsize);
    548 
    549 	if (esc->sc_16bitp) {
    550 		for (i = 0; i < memsize; i += 2) {
    551 			if (bus_space_read_2(memt, memh, i) != 0)
    552 				goto fail;
    553 		}
    554 	} else {
    555 		for (i = 0; i < memsize; i++) {
    556 			if (bus_space_read_1(memt, memh, i) != 0)
    557 				goto fail;
    558 		}
    559 	}
    560 
    561 	return (0);
    562 
    563  fail:
    564 	printf("%s: failed to clear shared memory at offset 0x%x\n",
    565 	    sc->sc_dev.dv_xname, i);
    566 	return (1);
    567 }
    568 
    569 /*
    570  * Given a NIC memory source address and a host memory destination address,
    571  * copy 'len' from NIC to host using shared memory.  The 'len' is rounded
    572  * up to a word - ok as long as mbufs are word-sized.
    573  */
    574 __inline void
    575 ec_readmem(esc, from, to, len)
    576 	struct ec_softc *esc;
    577 	int from;
    578 	u_int8_t *to;
    579 	int 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 (len & 1)
    585 		++len;
    586 
    587 	if (esc->sc_16bitp)
    588 		bus_space_read_region_2(memt, memh, from, (u_int16_t *)to,
    589 		    len >> 1);
    590 	else
    591 		bus_space_read_region_1(memt, memh, from, to, len);
    592 }
    593 
    594 int
    595 ec_write_mbuf(sc, m, buf)
    596 	struct dp8390_softc *sc;
    597 	struct mbuf *m;
    598 	int buf;
    599 {
    600 	struct ec_softc *esc = (struct ec_softc *)sc;
    601 	bus_space_tag_t asict = esc->sc_asict;
    602 	bus_space_handle_t asich = esc->sc_asich;
    603 	bus_space_tag_t memt = esc->sc_dp8390.sc_buft;
    604 	bus_space_handle_t memh = esc->sc_dp8390.sc_bufh;
    605 	u_int8_t *data, savebyte[2];
    606 	int savelen, len, leftover;
    607 #ifdef DIAGNOSTIC
    608 	u_int8_t *lim;
    609 #endif
    610 
    611 	savelen = m->m_pkthdr.len;
    612 
    613 	/*
    614 	 * 8-bit boards are simple: we're already in the correct
    615 	 * page, and no alignment tricks are necessary.
    616 	 */
    617 	if (esc->sc_16bitp == 0) {
    618 		for (; m != NULL; buf += m->m_len, m = m->m_next)
    619 			bus_space_write_region_1(memt, memh, buf,
    620 			    mtod(m, u_int8_t *), m->m_len);
    621 		return (savelen);
    622 	}
    623 
    624 	/*
    625 	 * If it's a 16-bit board, we have transmit buffers
    626 	 * in a different page; switch to it.
    627 	 */
    628 	if (esc->sc_16bitp)
    629 		bus_space_write_1(asict, asich, ELINK2_GACFR,
    630 		    ELINK2_GACFR_RSEL);
    631 
    632 	/* Start out with no leftover data. */
    633 	leftover = 0;
    634 	savebyte[0] = savebyte[1] = 0;
    635 
    636 	for (; m != NULL; m = m->m_next) {
    637 		len = m->m_len;
    638 		if (len == 0)
    639 			continue;
    640 		data = mtod(m, u_int8_t *);
    641 #ifdef DIAGNOSTIC
    642 		lim = data + len;
    643 #endif
    644 		while (len > 0) {
    645 			if (leftover) {
    646 				/*
    647 				 * Data left over (from mbuf or realignment).
    648 				 * Buffer the next byte, and write it and
    649 				 * the leftover data out.
    650 				 */
    651 				savebyte[1] = *data++;
    652 				len--;
    653 				bus_space_write_2(memt, memh, buf,
    654 				    *(u_int16_t *)savebyte);
    655 				buf += 2;
    656 				leftover = 0;
    657 			} else if (BUS_SPACE_ALIGNED_POINTER(data, u_int16_t)
    658 				   == 0) {
    659 				/*
    660 				 * Unaligned data; buffer the next byte.
    661 				 */
    662 				savebyte[0] = *data++;
    663 				len--;
    664 				leftover = 1;
    665 			} else {
    666 				/*
    667 				 * Aligned data; output contiguous words as
    668 				 * much as we can, then buffer the remaining
    669 				 * byte, if any.
    670 				 */
    671 				leftover = len & 1;
    672 				len &= ~1;
    673 				bus_space_write_region_2(memt, memh, buf,
    674 				    (u_int16_t *)data, len >> 1);
    675 				data += len;
    676 				buf += len;
    677 				if (leftover)
    678 					savebyte[0] = *data++;
    679 				len = 0;
    680 			}
    681 		}
    682 		if (len < 0)
    683 			panic("ec_write_mbuf: negative len");
    684 #ifdef DIAGNOSTIC
    685 		if (data != lim)
    686 			panic("ec_write_mbuf: data != lim");
    687 #endif
    688 	}
    689 	if (leftover) {
    690 		savebyte[1] = 0;
    691 		bus_space_write_2(memt, memh, buf, *(u_int16_t *)savebyte);
    692 	}
    693 
    694 	/*
    695 	 * Switch back to receive page.
    696 	 */
    697 	if (esc->sc_16bitp)
    698 		bus_space_write_1(asict, asich, ELINK2_GACFR,
    699 		    ELINK2_GACFR_RSEL | ELINK2_GACFR_MBS0);
    700 
    701 	return (savelen);
    702 }
    703 
    704 int
    705 ec_ring_copy(sc, src, dst, amount)
    706 	struct dp8390_softc *sc;
    707 	int src;
    708 	caddr_t dst;
    709 	u_short amount;
    710 {
    711 	struct ec_softc *esc = (struct ec_softc *)sc;
    712 	u_short tmp_amount;
    713 
    714 	/* Does copy wrap to lower addr in ring buffer? */
    715 	if (src + amount > sc->mem_end) {
    716 		tmp_amount = sc->mem_end - src;
    717 
    718 		/* Copy amount up to end of NIC memory. */
    719 		ec_readmem(esc, src, dst, tmp_amount);
    720 
    721 		amount -= tmp_amount;
    722 		src = sc->mem_ring;
    723 		dst += tmp_amount;
    724 	}
    725 
    726 	ec_readmem(esc, src, dst, amount);
    727 
    728 	return (src + amount);
    729 }
    730 
    731 void
    732 ec_read_hdr(sc, packet_ptr, packet_hdrp)
    733 	struct dp8390_softc *sc;
    734 	int packet_ptr;
    735 	struct dp8390_ring *packet_hdrp;
    736 {
    737 	struct ec_softc *esc = (struct ec_softc *)sc;
    738 
    739 	ec_readmem(esc, packet_ptr, (u_int8_t *)packet_hdrp,
    740 	    sizeof(struct dp8390_ring));
    741 #if BYTE_ORDER == BIG_ENDIAN
    742 	packet_hdrp->count = bswap16(packet_hdrp->count);
    743 #endif
    744 }
    745 
    746 int
    747 ec_mediachange(sc)
    748 	struct dp8390_softc *sc;
    749 {
    750 	struct ec_softc *esc = (struct ec_softc *)sc;
    751 	struct ifmedia *ifm = &sc->sc_media;
    752 
    753 	return (ec_set_media(esc, ifm->ifm_media));
    754 }
    755 
    756 void
    757 ec_mediastatus(sc, ifmr)
    758 	struct dp8390_softc *sc;
    759 	struct ifmediareq *ifmr;
    760 {
    761 	struct ifmedia *ifm = &sc->sc_media;
    762 
    763 	/*
    764 	 * The currently selected media is always the active media.
    765 	 */
    766 	ifmr->ifm_active = ifm->ifm_cur->ifm_media;
    767 }
    768 
    769 void
    770 ec_init_card(sc)
    771 	struct dp8390_softc *sc;
    772 {
    773 	struct ec_softc *esc = (struct ec_softc *)sc;
    774 	struct ifmedia *ifm = &sc->sc_media;
    775 
    776 	(void) ec_set_media(esc, ifm->ifm_cur->ifm_media);
    777 }
    778 
    779 int
    780 ec_set_media(esc, media)
    781 	struct ec_softc *esc;
    782 	int media;
    783 {
    784 	u_int8_t new;
    785 
    786 	if (IFM_TYPE(media) != IFM_ETHER)
    787 		return (EINVAL);
    788 
    789 	switch (IFM_SUBTYPE(media)) {
    790 	case IFM_10_2:
    791 		new = ELINK2_CR_XSEL;
    792 		break;
    793 
    794 	case IFM_10_5:
    795 		new = 0;
    796 		break;
    797 
    798 	default:
    799 		return (EINVAL);
    800 	}
    801 
    802 	bus_space_write_1(esc->sc_asict, esc->sc_asich, ELINK2_CR, new);
    803 	return (0);
    804 }
    805