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