Home | History | Annotate | Line # | Download | only in ic
we.c revision 1.13.16.1
      1  1.13.16.1       mjf /*	$NetBSD: we.c,v 1.13.16.1 2008/04/03 12:42:43 mjf Exp $	*/
      2        1.1  jdolecek 
      3        1.1  jdolecek /*-
      4        1.1  jdolecek  * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
      5        1.1  jdolecek  * All rights reserved.
      6        1.1  jdolecek  *
      7        1.1  jdolecek  * This code is derived from software contributed to The NetBSD Foundation
      8        1.1  jdolecek  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9        1.1  jdolecek  * NASA Ames Research Center.
     10        1.1  jdolecek  *
     11        1.1  jdolecek  * Redistribution and use in source and binary forms, with or without
     12        1.1  jdolecek  * modification, are permitted provided that the following conditions
     13        1.1  jdolecek  * are met:
     14        1.1  jdolecek  * 1. Redistributions of source code must retain the above copyright
     15        1.1  jdolecek  *    notice, this list of conditions and the following disclaimer.
     16        1.1  jdolecek  * 2. Redistributions in binary form must reproduce the above copyright
     17        1.1  jdolecek  *    notice, this list of conditions and the following disclaimer in the
     18        1.1  jdolecek  *    documentation and/or other materials provided with the distribution.
     19        1.1  jdolecek  * 3. All advertising materials mentioning features or use of this software
     20        1.1  jdolecek  *    must display the following acknowledgement:
     21        1.1  jdolecek  *	This product includes software developed by the NetBSD
     22        1.1  jdolecek  *	Foundation, Inc. and its contributors.
     23        1.1  jdolecek  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24        1.1  jdolecek  *    contributors may be used to endorse or promote products derived
     25        1.1  jdolecek  *    from this software without specific prior written permission.
     26        1.1  jdolecek  *
     27        1.1  jdolecek  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28        1.1  jdolecek  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29        1.1  jdolecek  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30        1.1  jdolecek  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31        1.1  jdolecek  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32        1.1  jdolecek  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33        1.1  jdolecek  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34        1.1  jdolecek  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35        1.1  jdolecek  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36        1.1  jdolecek  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37        1.1  jdolecek  * POSSIBILITY OF SUCH DAMAGE.
     38        1.1  jdolecek  */
     39        1.1  jdolecek 
     40        1.1  jdolecek /*
     41        1.1  jdolecek  * Device driver for National Semiconductor DS8390/WD83C690 based ethernet
     42        1.1  jdolecek  * adapters.
     43        1.1  jdolecek  *
     44        1.1  jdolecek  * Copyright (c) 1994, 1995 Charles M. Hannum.  All rights reserved.
     45        1.1  jdolecek  *
     46        1.1  jdolecek  * Copyright (C) 1993, David Greenman.  This software may be used, modified,
     47        1.1  jdolecek  * copied, distributed, and sold, in both source and binary form provided that
     48        1.1  jdolecek  * the above copyright and these terms are retained.  Under no circumstances is
     49        1.1  jdolecek  * the author responsible for the proper functioning of this software, nor does
     50        1.1  jdolecek  * the author assume any responsibility for damages incurred with its use.
     51        1.1  jdolecek  */
     52        1.1  jdolecek 
     53        1.1  jdolecek /*
     54        1.1  jdolecek  * Device driver for the Western Digital/SMC 8003 and 8013 series,
     55        1.1  jdolecek  * and the SMC Elite Ultra (8216).
     56        1.1  jdolecek  */
     57        1.4     lukem 
     58        1.4     lukem #include <sys/cdefs.h>
     59  1.13.16.1       mjf __KERNEL_RCSID(0, "$NetBSD: we.c,v 1.13.16.1 2008/04/03 12:42:43 mjf Exp $");
     60        1.1  jdolecek 
     61        1.1  jdolecek #include <sys/param.h>
     62        1.1  jdolecek #include <sys/systm.h>
     63        1.1  jdolecek #include <sys/device.h>
     64        1.1  jdolecek #include <sys/socket.h>
     65        1.1  jdolecek #include <sys/mbuf.h>
     66        1.1  jdolecek #include <sys/syslog.h>
     67        1.1  jdolecek 
     68        1.1  jdolecek #include <net/if.h>
     69        1.1  jdolecek #include <net/if_dl.h>
     70        1.1  jdolecek #include <net/if_types.h>
     71        1.1  jdolecek #include <net/if_media.h>
     72        1.1  jdolecek 
     73        1.1  jdolecek #include <net/if_ether.h>
     74        1.1  jdolecek 
     75       1.13        ad #include <sys/bus.h>
     76       1.10       dsl #include <sys/bswap.h>
     77       1.13        ad #include <sys/intr.h>
     78        1.1  jdolecek 
     79        1.1  jdolecek #include <dev/isa/isareg.h>
     80        1.1  jdolecek #include <dev/isa/isavar.h>
     81        1.1  jdolecek 
     82        1.1  jdolecek #include <dev/ic/dp8390reg.h>
     83        1.1  jdolecek #include <dev/ic/dp8390var.h>
     84        1.1  jdolecek #include <dev/ic/wereg.h>
     85        1.1  jdolecek #include <dev/ic/wevar.h>
     86        1.1  jdolecek 
     87        1.1  jdolecek #ifndef __BUS_SPACE_HAS_STREAM_METHODS
     88        1.1  jdolecek #define	bus_space_read_region_stream_2	bus_space_read_region_2
     89        1.1  jdolecek #define	bus_space_write_stream_2	bus_space_write_2
     90        1.1  jdolecek #define	bus_space_write_region_stream_2	bus_space_write_region_2
     91        1.1  jdolecek #endif
     92        1.1  jdolecek 
     93        1.6     perry static void	we_set_media(struct we_softc *, int);
     94        1.1  jdolecek 
     95        1.6     perry static void	we_media_init(struct dp8390_softc *);
     96        1.1  jdolecek 
     97        1.6     perry static int	we_mediachange(struct dp8390_softc *);
     98        1.6     perry static void	we_mediastatus(struct dp8390_softc *, struct ifmediareq *);
     99        1.1  jdolecek 
    100        1.6     perry static void	we_recv_int(struct dp8390_softc *);
    101        1.6     perry static void	we_init_card(struct dp8390_softc *);
    102        1.6     perry static int	we_write_mbuf(struct dp8390_softc *, struct mbuf *, int);
    103       1.12  christos static int	we_ring_copy(struct dp8390_softc *, int, void *, u_short);
    104        1.6     perry static void	we_read_hdr(struct dp8390_softc *, int, struct dp8390_ring *);
    105        1.6     perry static int	we_test_mem(struct dp8390_softc *);
    106        1.1  jdolecek 
    107        1.9     perry static inline void we_readmem(struct we_softc *, int, u_int8_t *, int);
    108        1.1  jdolecek 
    109        1.1  jdolecek /*
    110        1.1  jdolecek  * Delay needed when switching 16-bit access to shared memory.
    111        1.1  jdolecek  */
    112        1.1  jdolecek #define	WE_DELAY(wsc) delay(3)
    113        1.1  jdolecek 
    114        1.1  jdolecek /*
    115        1.1  jdolecek  * Enable card RAM, and 16-bit access.
    116        1.1  jdolecek  */
    117        1.1  jdolecek #define	WE_MEM_ENABLE(wsc) \
    118        1.7  jdolecek if (((wsc)->sc_flags & WE_16BIT_NOTOGGLE) == 0) {			\
    119        1.7  jdolecek 	if ((wsc)->sc_flags & WE_16BIT_ENABLE)				\
    120        1.7  jdolecek 		bus_space_write_1((wsc)->sc_asict, (wsc)->sc_asich,	\
    121        1.7  jdolecek 		    WE_LAAR, (wsc)->sc_laar_proto | WE_LAAR_M16EN);	\
    122        1.7  jdolecek 	bus_space_write_1((wsc)->sc_asict, (wsc)->sc_asich,		\
    123        1.7  jdolecek 	    WE_MSR, wsc->sc_msr_proto | WE_MSR_MENB);			\
    124        1.7  jdolecek 	WE_DELAY((wsc));						\
    125        1.7  jdolecek }
    126        1.1  jdolecek 
    127        1.1  jdolecek /*
    128        1.1  jdolecek  * Disable card RAM, and 16-bit access.
    129        1.1  jdolecek  */
    130        1.1  jdolecek #define	WE_MEM_DISABLE(wsc) \
    131        1.7  jdolecek if (((wsc)->sc_flags & WE_16BIT_NOTOGGLE) == 0) {			\
    132        1.7  jdolecek 	bus_space_write_1((wsc)->sc_asict, (wsc)->sc_asich,		\
    133        1.7  jdolecek 	    WE_MSR, (wsc)->sc_msr_proto);				\
    134        1.7  jdolecek 	if ((wsc)->sc_flags & WE_16BIT_ENABLE)				\
    135        1.7  jdolecek 		bus_space_write_1((wsc)->sc_asict, (wsc)->sc_asich,	\
    136        1.7  jdolecek 		    WE_LAAR, (wsc)->sc_laar_proto);			\
    137        1.7  jdolecek 	WE_DELAY((wsc));						\
    138        1.7  jdolecek }
    139        1.1  jdolecek 
    140        1.1  jdolecek int
    141  1.13.16.1       mjf we_config(device_t self, struct we_softc *wsc, const char *typestr)
    142        1.1  jdolecek {
    143        1.1  jdolecek 	struct dp8390_softc *sc = &wsc->sc_dp8390;
    144        1.1  jdolecek 	u_int8_t x;
    145        1.1  jdolecek 	int i, forced_16bit = 0;
    146        1.1  jdolecek 
    147        1.1  jdolecek 	/*
    148        1.1  jdolecek 	 * Allow user to override 16-bit mode.  8-bit takes precedence.
    149        1.1  jdolecek 	 */
    150       1.11   thorpej 	if (device_cfdata(self)->cf_flags & DP8390_FORCE_16BIT_MODE) {
    151        1.7  jdolecek 		wsc->sc_flags |= WE_16BIT_ENABLE;
    152        1.1  jdolecek 		forced_16bit = 1;
    153        1.1  jdolecek 	}
    154       1.11   thorpej 	if (device_cfdata(self)->cf_flags & DP8390_FORCE_8BIT_MODE)
    155        1.7  jdolecek 		wsc->sc_flags &= ~WE_16BIT_ENABLE;
    156        1.1  jdolecek 
    157        1.1  jdolecek 	/* Registers are linear. */
    158        1.1  jdolecek 	for (i = 0; i < 16; i++)
    159        1.1  jdolecek 		sc->sc_reg_map[i] = i;
    160        1.1  jdolecek 
    161        1.1  jdolecek 	/* Now we can use the NIC_{GET,PUT}() macros. */
    162        1.1  jdolecek 
    163  1.13.16.1       mjf 	aprint_normal_dev(self, "%s Ethernet (%s-bit)\n",
    164        1.7  jdolecek 	    typestr, wsc->sc_flags & WE_16BIT_ENABLE ? "16" : "8");
    165        1.1  jdolecek 
    166        1.1  jdolecek 	/* Get station address from EEPROM. */
    167        1.1  jdolecek 	for (i = 0; i < ETHER_ADDR_LEN; i++)
    168        1.1  jdolecek 		sc->sc_enaddr[i] = bus_space_read_1(wsc->sc_asict,
    169        1.1  jdolecek 					wsc->sc_asich, WE_PROM + i);
    170        1.1  jdolecek 
    171        1.1  jdolecek 	/*
    172        1.1  jdolecek 	 * Set upper address bits and 8/16 bit access to shared memory.
    173        1.1  jdolecek 	 */
    174        1.1  jdolecek 	if (sc->is790) {
    175        1.1  jdolecek 		wsc->sc_laar_proto =
    176        1.1  jdolecek 		    bus_space_read_1(wsc->sc_asict, wsc->sc_asich, WE_LAAR) &
    177        1.1  jdolecek 		    ~WE_LAAR_M16EN;
    178        1.1  jdolecek 		bus_space_write_1(wsc->sc_asict, wsc->sc_asich, WE_LAAR,
    179        1.7  jdolecek 		    wsc->sc_laar_proto | (wsc->sc_flags & WE_16BIT_ENABLE ? WE_LAAR_M16EN : 0));
    180        1.1  jdolecek 	} else if ((wsc->sc_type & WE_SOFTCONFIG) ||
    181        1.1  jdolecek #ifdef TOSH_ETHER
    182        1.1  jdolecek 	    (wsc->sc_type == WE_TYPE_TOSHIBA1) ||
    183        1.1  jdolecek 	    (wsc->sc_type == WE_TYPE_TOSHIBA4) ||
    184        1.1  jdolecek #endif
    185        1.1  jdolecek 	    (forced_16bit) ||
    186        1.1  jdolecek 	    (wsc->sc_type == WE_TYPE_WD8013EBT)) {
    187        1.1  jdolecek 		wsc->sc_laar_proto = (wsc->sc_maddr >> 19) & WE_LAAR_ADDRHI;
    188        1.7  jdolecek 		if (wsc->sc_flags & WE_16BIT_ENABLE)
    189        1.1  jdolecek 			wsc->sc_laar_proto |= WE_LAAR_L16EN;
    190        1.1  jdolecek 		bus_space_write_1(wsc->sc_asict, wsc->sc_asich, WE_LAAR,
    191        1.7  jdolecek 		    wsc->sc_laar_proto | (wsc->sc_flags & WE_16BIT_ENABLE ? WE_LAAR_M16EN : 0));
    192        1.1  jdolecek 	}
    193        1.1  jdolecek 
    194        1.1  jdolecek 	/*
    195        1.1  jdolecek 	 * Set address and enable interface shared memory.
    196        1.1  jdolecek 	 */
    197        1.1  jdolecek 	if (sc->is790) {
    198        1.1  jdolecek 		/* XXX MAGIC CONSTANTS XXX */
    199        1.1  jdolecek 		x = bus_space_read_1(wsc->sc_asict, wsc->sc_asich, 0x04);
    200        1.1  jdolecek 		bus_space_write_1(wsc->sc_asict, wsc->sc_asich, 0x04, x | 0x80);
    201        1.1  jdolecek 		bus_space_write_1(wsc->sc_asict, wsc->sc_asich, 0x0b,
    202        1.1  jdolecek 		    ((wsc->sc_maddr >> 13) & 0x0f) |
    203        1.1  jdolecek 		    ((wsc->sc_maddr >> 11) & 0x40) |
    204        1.1  jdolecek 		    (bus_space_read_1(wsc->sc_asict, wsc->sc_asich, 0x0b) & 0xb0));
    205        1.1  jdolecek 		bus_space_write_1(wsc->sc_asict, wsc->sc_asich, 0x04, x);
    206        1.1  jdolecek 		wsc->sc_msr_proto = 0x00;
    207        1.1  jdolecek 		sc->cr_proto = 0x00;
    208        1.1  jdolecek 	} else {
    209        1.1  jdolecek #ifdef TOSH_ETHER
    210        1.1  jdolecek 		if (wsc->sc_type == WE_TYPE_TOSHIBA1 ||
    211        1.1  jdolecek 		    wsc->sc_type == WE_TYPE_TOSHIBA4) {
    212        1.1  jdolecek 			bus_space_write_1(wsc->sc_asict, wsc->sc_asich,
    213        1.1  jdolecek 			    WE_MSR + 1,
    214        1.1  jdolecek 			    ((wsc->sc_maddr >> 8) & 0xe0) | 0x04);
    215        1.1  jdolecek 			bus_space_write_1(wsc->sc_asict, wsc->sc_asich,
    216        1.1  jdolecek 			    WE_MSR + 2,
    217        1.1  jdolecek 			    ((wsc->sc_maddr >> 16) & 0x0f));
    218        1.1  jdolecek 			wsc->sc_msr_proto = WE_MSR_POW;
    219        1.1  jdolecek 		} else
    220        1.1  jdolecek #endif
    221        1.1  jdolecek 			wsc->sc_msr_proto = (wsc->sc_maddr >> 13) &
    222        1.1  jdolecek 			    WE_MSR_ADDR;
    223        1.1  jdolecek 
    224        1.1  jdolecek 		sc->cr_proto = ED_CR_RD2;
    225        1.1  jdolecek 	}
    226        1.1  jdolecek 
    227        1.1  jdolecek 	bus_space_write_1(wsc->sc_asict, wsc->sc_asich, WE_MSR,
    228        1.1  jdolecek 	    wsc->sc_msr_proto | WE_MSR_MENB);
    229        1.1  jdolecek 	WE_DELAY(wsc);
    230        1.1  jdolecek 
    231        1.1  jdolecek 	/*
    232        1.1  jdolecek 	 * DCR gets:
    233        1.1  jdolecek 	 *
    234        1.1  jdolecek 	 *	FIFO threshold to 8, No auto-init Remote DMA,
    235        1.1  jdolecek 	 *	byte order=80x86.
    236        1.1  jdolecek 	 *
    237        1.1  jdolecek 	 * 16-bit cards also get word-wide DMA transfers.
    238        1.1  jdolecek 	 */
    239        1.1  jdolecek 	sc->dcr_reg = ED_DCR_FT1 | ED_DCR_LS |
    240        1.7  jdolecek 	    (wsc->sc_flags & WE_16BIT_ENABLE ? ED_DCR_WTS : 0);
    241        1.1  jdolecek 
    242        1.1  jdolecek 	sc->test_mem = we_test_mem;
    243        1.1  jdolecek 	sc->ring_copy = we_ring_copy;
    244        1.1  jdolecek 	sc->write_mbuf = we_write_mbuf;
    245        1.1  jdolecek 	sc->read_hdr = we_read_hdr;
    246        1.1  jdolecek 	sc->recv_int = we_recv_int;
    247        1.1  jdolecek 	sc->init_card = we_init_card;
    248        1.1  jdolecek 
    249        1.1  jdolecek 	sc->sc_mediachange = we_mediachange;
    250        1.1  jdolecek 	sc->sc_mediastatus = we_mediastatus;
    251        1.1  jdolecek 
    252        1.1  jdolecek 	sc->mem_start = 0;
    253        1.1  jdolecek 	/* sc->mem_size has to be set by frontend */
    254        1.1  jdolecek 
    255       1.11   thorpej 	sc->sc_flags = device_cfdata(self)->cf_flags;
    256        1.1  jdolecek 
    257        1.1  jdolecek 	/* Do generic parts of attach. */
    258        1.1  jdolecek 	if (wsc->sc_type & WE_SOFTCONFIG)
    259        1.1  jdolecek 		sc->sc_media_init = we_media_init;
    260        1.1  jdolecek 	else
    261        1.1  jdolecek 		sc->sc_media_init = dp8390_media_init;
    262        1.1  jdolecek 	if (dp8390_config(sc)) {
    263  1.13.16.1       mjf 		aprint_error_dev(self, "configuration failed\n");
    264        1.1  jdolecek 		return (1);
    265        1.1  jdolecek 	}
    266        1.1  jdolecek 
    267        1.1  jdolecek 	/*
    268        1.1  jdolecek 	 * Disable 16-bit access to shared memory - we leave it disabled
    269        1.1  jdolecek 	 * so that:
    270        1.1  jdolecek 	 *
    271        1.1  jdolecek 	 *	(1) machines reboot properly when the board is set to
    272        1.1  jdolecek 	 *	    16-bit mode and there are conflicting 8-bit devices
    273        1.1  jdolecek 	 *	    within the same 128k address space as this board's
    274        1.1  jdolecek 	 *	    shared memory, and
    275        1.1  jdolecek 	 *
    276        1.1  jdolecek 	 *	(2) so that other 8-bit devices with shared memory
    277        1.1  jdolecek 	 *	    in this same 128k address space will work.
    278        1.1  jdolecek 	 */
    279        1.1  jdolecek 	WE_MEM_DISABLE(wsc);
    280        1.1  jdolecek 
    281        1.1  jdolecek 	return (0);
    282        1.1  jdolecek }
    283        1.1  jdolecek 
    284        1.1  jdolecek static int
    285  1.13.16.1       mjf we_test_mem(struct dp8390_softc *sc)
    286        1.1  jdolecek {
    287        1.1  jdolecek 	struct we_softc *wsc = (struct we_softc *)sc;
    288        1.1  jdolecek 	bus_space_tag_t memt = sc->sc_buft;
    289        1.1  jdolecek 	bus_space_handle_t memh = sc->sc_bufh;
    290        1.1  jdolecek 	bus_size_t memsize = sc->mem_size;
    291        1.1  jdolecek 	int i;
    292        1.1  jdolecek 
    293        1.7  jdolecek 	if (wsc->sc_flags & WE_16BIT_ENABLE)
    294        1.1  jdolecek 		bus_space_set_region_2(memt, memh, 0, 0, memsize >> 1);
    295        1.1  jdolecek 	else
    296        1.1  jdolecek 		bus_space_set_region_1(memt, memh, 0, 0, memsize);
    297        1.1  jdolecek 
    298        1.7  jdolecek 	if (wsc->sc_flags & WE_16BIT_ENABLE) {
    299        1.1  jdolecek 		for (i = 0; i < memsize; i += 2) {
    300        1.1  jdolecek 			if (bus_space_read_2(memt, memh, i) != 0)
    301        1.1  jdolecek 				goto fail;
    302        1.1  jdolecek 		}
    303        1.1  jdolecek 	} else {
    304        1.1  jdolecek 		for (i = 0; i < memsize; i++) {
    305        1.1  jdolecek 			if (bus_space_read_1(memt, memh, i) != 0)
    306        1.1  jdolecek 				goto fail;
    307        1.1  jdolecek 		}
    308        1.1  jdolecek 	}
    309        1.1  jdolecek 
    310        1.1  jdolecek 	return (0);
    311        1.1  jdolecek 
    312        1.1  jdolecek  fail:
    313  1.13.16.1       mjf 	aprint_error_dev(sc->sc_dev,
    314  1.13.16.1       mjf 	    "failed to clear shared memory at offset 0x%x\n", i);
    315        1.1  jdolecek 	WE_MEM_DISABLE(wsc);
    316        1.1  jdolecek 	return (1);
    317        1.1  jdolecek }
    318        1.1  jdolecek 
    319        1.1  jdolecek /*
    320        1.1  jdolecek  * Given a NIC memory source address and a host memory destination address,
    321        1.1  jdolecek  * copy 'len' from NIC to host using shared memory.  The 'len' is rounded
    322        1.1  jdolecek  * up to a word - ok as long as mbufs are word-sized.
    323        1.1  jdolecek  */
    324        1.9     perry static inline void
    325  1.13.16.1       mjf we_readmem(struct we_softc *wsc, int from, u_int8_t *to, int len)
    326        1.1  jdolecek {
    327        1.1  jdolecek 	bus_space_tag_t memt = wsc->sc_dp8390.sc_buft;
    328        1.1  jdolecek 	bus_space_handle_t memh = wsc->sc_dp8390.sc_bufh;
    329        1.1  jdolecek 
    330        1.1  jdolecek 	if (len & 1)
    331        1.1  jdolecek 		++len;
    332        1.1  jdolecek 
    333        1.7  jdolecek 	if (wsc->sc_flags & WE_16BIT_ENABLE)
    334        1.1  jdolecek 		bus_space_read_region_stream_2(memt, memh, from,
    335        1.1  jdolecek 		    (u_int16_t *)to, len >> 1);
    336        1.1  jdolecek 	else
    337        1.1  jdolecek 		bus_space_read_region_1(memt, memh, from,
    338        1.1  jdolecek 		    to, len);
    339        1.1  jdolecek }
    340        1.1  jdolecek 
    341        1.1  jdolecek static int
    342  1.13.16.1       mjf we_write_mbuf(struct dp8390_softc *sc, struct mbuf *m, int buf)
    343        1.1  jdolecek {
    344        1.1  jdolecek 	struct we_softc *wsc = (struct we_softc *)sc;
    345        1.1  jdolecek 	bus_space_tag_t memt = wsc->sc_dp8390.sc_buft;
    346        1.1  jdolecek 	bus_space_handle_t memh = wsc->sc_dp8390.sc_bufh;
    347        1.1  jdolecek 	u_int8_t *data, savebyte[2];
    348        1.1  jdolecek 	int savelen, len, leftover;
    349        1.1  jdolecek #ifdef DIAGNOSTIC
    350        1.1  jdolecek 	u_int8_t *lim;
    351        1.1  jdolecek #endif
    352        1.1  jdolecek 
    353        1.1  jdolecek 	savelen = m->m_pkthdr.len;
    354        1.1  jdolecek 
    355        1.1  jdolecek 	WE_MEM_ENABLE(wsc);
    356        1.1  jdolecek 
    357        1.1  jdolecek 	/*
    358        1.1  jdolecek 	 * 8-bit boards are simple; no alignment tricks are necessary.
    359        1.1  jdolecek 	 */
    360        1.7  jdolecek 	if ((wsc->sc_flags & WE_16BIT_ENABLE) == 0) {
    361        1.1  jdolecek 		for (; m != NULL; buf += m->m_len, m = m->m_next)
    362        1.1  jdolecek 			bus_space_write_region_1(memt, memh,
    363        1.1  jdolecek 			    buf, mtod(m, u_int8_t *), m->m_len);
    364        1.5    bouyer 		if (savelen < ETHER_MIN_LEN - ETHER_CRC_LEN) {
    365        1.5    bouyer 			bus_space_set_region_1(memt, memh,
    366        1.5    bouyer 			    buf, 0, ETHER_MIN_LEN - ETHER_CRC_LEN - savelen);
    367        1.5    bouyer 			savelen = ETHER_MIN_LEN - ETHER_CRC_LEN;
    368        1.5    bouyer 		}
    369        1.1  jdolecek 		goto out;
    370        1.1  jdolecek 	}
    371        1.1  jdolecek 
    372        1.1  jdolecek 	/* Start out with no leftover data. */
    373        1.1  jdolecek 	leftover = 0;
    374        1.1  jdolecek 	savebyte[0] = savebyte[1] = 0;
    375        1.1  jdolecek 
    376        1.1  jdolecek 	for (; m != NULL; m = m->m_next) {
    377        1.1  jdolecek 		len = m->m_len;
    378        1.1  jdolecek 		if (len == 0)
    379        1.1  jdolecek 			continue;
    380        1.1  jdolecek 		data = mtod(m, u_int8_t *);
    381        1.1  jdolecek #ifdef DIAGNOSTIC
    382        1.1  jdolecek 		lim = data + len;
    383        1.1  jdolecek #endif
    384        1.1  jdolecek 		while (len > 0) {
    385        1.1  jdolecek 			if (leftover) {
    386        1.1  jdolecek 				/*
    387        1.1  jdolecek 				 * Data left over (from mbuf or realignment).
    388        1.1  jdolecek 				 * Buffer the next byte, and write it and
    389        1.1  jdolecek 				 * the leftover data out.
    390        1.1  jdolecek 				 */
    391        1.1  jdolecek 				savebyte[1] = *data++;
    392        1.1  jdolecek 				len--;
    393        1.1  jdolecek 				bus_space_write_stream_2(memt, memh, buf,
    394        1.1  jdolecek 				    *(u_int16_t *)savebyte);
    395        1.1  jdolecek 				buf += 2;
    396        1.1  jdolecek 				leftover = 0;
    397        1.1  jdolecek 			} else if (BUS_SPACE_ALIGNED_POINTER(data, u_int16_t)
    398        1.1  jdolecek 				   == 0) {
    399        1.1  jdolecek 				/*
    400        1.1  jdolecek 				 * Unaligned dta; buffer the next byte.
    401        1.1  jdolecek 				 */
    402        1.1  jdolecek 				savebyte[0] = *data++;
    403        1.1  jdolecek 				len--;
    404        1.1  jdolecek 				leftover = 1;
    405        1.1  jdolecek 			} else {
    406        1.1  jdolecek 				/*
    407        1.1  jdolecek 				 * Aligned data; output contiguous words as
    408        1.1  jdolecek 				 * much as we can, then buffer the remaining
    409        1.1  jdolecek 				 * byte, if any.
    410        1.1  jdolecek 				 */
    411        1.1  jdolecek 				leftover = len & 1;
    412        1.1  jdolecek 				len &= ~1;
    413        1.1  jdolecek 				bus_space_write_region_stream_2(memt, memh,
    414        1.1  jdolecek 				    buf, (u_int16_t *)data, len >> 1);
    415        1.1  jdolecek 				data += len;
    416        1.1  jdolecek 				buf += len;
    417        1.1  jdolecek 				if (leftover)
    418        1.1  jdolecek 					savebyte[0] = *data++;
    419        1.1  jdolecek 				len = 0;
    420        1.1  jdolecek 			}
    421        1.1  jdolecek 		}
    422        1.1  jdolecek 		if (len < 0)
    423        1.1  jdolecek 			panic("we_write_mbuf: negative len");
    424        1.1  jdolecek #ifdef DIAGNOSTIC
    425        1.1  jdolecek 		if (data != lim)
    426        1.1  jdolecek 			panic("we_write_mbuf: data != lim");
    427        1.1  jdolecek #endif
    428        1.1  jdolecek 	}
    429        1.1  jdolecek 	if (leftover) {
    430        1.1  jdolecek 		savebyte[1] = 0;
    431        1.1  jdolecek 		bus_space_write_stream_2(memt, memh, buf,
    432        1.1  jdolecek 		    *(u_int16_t *)savebyte);
    433        1.5    bouyer 		buf += 2;
    434        1.5    bouyer 	}
    435        1.5    bouyer 	if (savelen < ETHER_MIN_LEN - ETHER_CRC_LEN) {
    436        1.5    bouyer 		bus_space_set_region_2(memt, memh,
    437        1.5    bouyer 		    buf, 0, (ETHER_MIN_LEN - ETHER_CRC_LEN - savelen) >> 1);
    438        1.5    bouyer 		savelen = ETHER_MIN_LEN - ETHER_CRC_LEN;
    439        1.1  jdolecek 	}
    440        1.1  jdolecek 
    441        1.1  jdolecek  out:
    442        1.1  jdolecek 	WE_MEM_DISABLE(wsc);
    443        1.1  jdolecek 
    444        1.1  jdolecek 	return (savelen);
    445        1.1  jdolecek }
    446        1.1  jdolecek 
    447        1.1  jdolecek static int
    448  1.13.16.1       mjf we_ring_copy(struct dp8390_softc *sc, int src, void *dstv, u_short amount)
    449        1.1  jdolecek {
    450       1.12  christos 	char *dst = dstv;
    451        1.1  jdolecek 	struct we_softc *wsc = (struct we_softc *)sc;
    452        1.1  jdolecek 	u_short tmp_amount;
    453        1.1  jdolecek 
    454        1.1  jdolecek 	/* Does copy wrap to lower addr in ring buffer? */
    455        1.1  jdolecek 	if (src + amount > sc->mem_end) {
    456        1.1  jdolecek 		tmp_amount = sc->mem_end - src;
    457        1.1  jdolecek 
    458        1.1  jdolecek 		/* Copy amount up to end of NIC memory. */
    459        1.1  jdolecek 		we_readmem(wsc, src, dst, tmp_amount);
    460        1.1  jdolecek 
    461        1.1  jdolecek 		amount -= tmp_amount;
    462        1.1  jdolecek 		src = sc->mem_ring;
    463        1.1  jdolecek 		dst += tmp_amount;
    464        1.1  jdolecek 	}
    465        1.1  jdolecek 
    466        1.1  jdolecek 	we_readmem(wsc, src, dst, amount);
    467        1.1  jdolecek 
    468        1.1  jdolecek 	return (src + amount);
    469        1.1  jdolecek }
    470        1.1  jdolecek 
    471        1.1  jdolecek static void
    472  1.13.16.1       mjf we_read_hdr(struct dp8390_softc *sc, int packet_ptr,
    473  1.13.16.1       mjf     struct dp8390_ring *packet_hdrp)
    474        1.1  jdolecek {
    475        1.1  jdolecek 	struct we_softc *wsc = (struct we_softc *)sc;
    476        1.1  jdolecek 
    477        1.1  jdolecek 	we_readmem(wsc, packet_ptr, (u_int8_t *)packet_hdrp,
    478        1.1  jdolecek 	    sizeof(struct dp8390_ring));
    479        1.1  jdolecek #if BYTE_ORDER == BIG_ENDIAN
    480        1.1  jdolecek 	packet_hdrp->count = bswap16(packet_hdrp->count);
    481        1.1  jdolecek #endif
    482        1.1  jdolecek }
    483        1.1  jdolecek 
    484        1.1  jdolecek static void
    485  1.13.16.1       mjf we_recv_int(struct dp8390_softc *sc)
    486        1.1  jdolecek {
    487        1.1  jdolecek 	struct we_softc *wsc = (struct we_softc *)sc;
    488        1.1  jdolecek 
    489        1.1  jdolecek 	WE_MEM_ENABLE(wsc);
    490        1.1  jdolecek 	dp8390_rint(sc);
    491        1.1  jdolecek 	WE_MEM_DISABLE(wsc);
    492        1.1  jdolecek }
    493        1.1  jdolecek 
    494        1.1  jdolecek static void
    495        1.1  jdolecek we_media_init(struct dp8390_softc *sc)
    496        1.1  jdolecek {
    497        1.1  jdolecek 	struct we_softc *wsc = (void *) sc;
    498        1.1  jdolecek 	int defmedia = IFM_ETHER;
    499        1.1  jdolecek 	u_int8_t x;
    500        1.1  jdolecek 
    501        1.1  jdolecek 	if (sc->is790) {
    502        1.1  jdolecek 		x = bus_space_read_1(wsc->sc_asict, wsc->sc_asich, WE790_HWR);
    503        1.1  jdolecek 		bus_space_write_1(wsc->sc_asict, wsc->sc_asich, WE790_HWR,
    504        1.1  jdolecek 		    x | WE790_HWR_SWH);
    505        1.1  jdolecek 		if (bus_space_read_1(wsc->sc_asict, wsc->sc_asich, WE790_GCR) &
    506        1.1  jdolecek 		    WE790_GCR_GPOUT)
    507        1.1  jdolecek 			defmedia |= IFM_10_2;
    508        1.1  jdolecek 		else
    509        1.1  jdolecek 			defmedia |= IFM_10_5;
    510        1.1  jdolecek 		bus_space_write_1(wsc->sc_asict, wsc->sc_asich, WE790_HWR,
    511        1.1  jdolecek 		    x & ~WE790_HWR_SWH);
    512        1.1  jdolecek 	} else {
    513        1.1  jdolecek 		x = bus_space_read_1(wsc->sc_asict, wsc->sc_asich, WE_IRR);
    514        1.1  jdolecek 		if (x & WE_IRR_OUT2)
    515        1.1  jdolecek 			defmedia |= IFM_10_2;
    516        1.1  jdolecek 		else
    517        1.1  jdolecek 			defmedia |= IFM_10_5;
    518        1.1  jdolecek 	}
    519        1.1  jdolecek 
    520        1.1  jdolecek 	ifmedia_init(&sc->sc_media, 0, dp8390_mediachange, dp8390_mediastatus);
    521        1.1  jdolecek 	ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_10_2, 0, NULL);
    522        1.1  jdolecek 	ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_10_5, 0, NULL);
    523        1.1  jdolecek 	ifmedia_set(&sc->sc_media, defmedia);
    524        1.1  jdolecek }
    525        1.1  jdolecek 
    526        1.1  jdolecek static int
    527  1.13.16.1       mjf we_mediachange(struct dp8390_softc *sc)
    528        1.1  jdolecek {
    529        1.1  jdolecek 
    530        1.1  jdolecek 	/*
    531        1.1  jdolecek 	 * Current media is already set up.  Just reset the interface
    532        1.1  jdolecek 	 * to let the new value take hold.  The new media will be
    533        1.1  jdolecek 	 * set up in we_init_card() called via dp8390_init().
    534        1.1  jdolecek 	 */
    535        1.1  jdolecek 	dp8390_reset(sc);
    536        1.1  jdolecek 	return (0);
    537        1.1  jdolecek }
    538        1.1  jdolecek 
    539        1.1  jdolecek static void
    540  1.13.16.1       mjf we_mediastatus(struct dp8390_softc *sc, struct ifmediareq *ifmr)
    541        1.1  jdolecek {
    542        1.1  jdolecek 	struct ifmedia *ifm = &sc->sc_media;
    543        1.1  jdolecek 
    544        1.1  jdolecek 	/*
    545        1.1  jdolecek 	 * The currently selected media is always the active media.
    546        1.1  jdolecek 	 */
    547        1.1  jdolecek 	ifmr->ifm_active = ifm->ifm_cur->ifm_media;
    548        1.1  jdolecek }
    549        1.1  jdolecek 
    550        1.1  jdolecek static void
    551  1.13.16.1       mjf we_init_card(struct dp8390_softc *sc)
    552        1.1  jdolecek {
    553        1.1  jdolecek 	struct we_softc *wsc = (struct we_softc *)sc;
    554        1.1  jdolecek 	struct ifmedia *ifm = &sc->sc_media;
    555        1.1  jdolecek 
    556        1.1  jdolecek 	if (wsc->sc_init_hook)
    557        1.1  jdolecek 		(*wsc->sc_init_hook)(wsc);
    558        1.1  jdolecek 
    559        1.1  jdolecek 	we_set_media(wsc, ifm->ifm_cur->ifm_media);
    560        1.1  jdolecek }
    561        1.1  jdolecek 
    562        1.1  jdolecek static void
    563  1.13.16.1       mjf we_set_media(struct we_softc *wsc, int media)
    564        1.1  jdolecek {
    565        1.1  jdolecek 	struct dp8390_softc *sc = &wsc->sc_dp8390;
    566        1.1  jdolecek 	bus_space_tag_t asict = wsc->sc_asict;
    567        1.1  jdolecek 	bus_space_handle_t asich = wsc->sc_asich;
    568        1.1  jdolecek 	u_int8_t hwr, gcr, irr;
    569        1.1  jdolecek 
    570        1.1  jdolecek 	if (sc->is790) {
    571        1.1  jdolecek 		hwr = bus_space_read_1(asict, asich, WE790_HWR);
    572        1.1  jdolecek 		bus_space_write_1(asict, asich, WE790_HWR,
    573        1.1  jdolecek 		    hwr | WE790_HWR_SWH);
    574        1.1  jdolecek 		gcr = bus_space_read_1(asict, asich, WE790_GCR);
    575        1.1  jdolecek 		if (IFM_SUBTYPE(media) == IFM_10_2)
    576        1.1  jdolecek 			gcr |= WE790_GCR_GPOUT;
    577        1.1  jdolecek 		else
    578        1.1  jdolecek 			gcr &= ~WE790_GCR_GPOUT;
    579        1.1  jdolecek 		bus_space_write_1(asict, asich, WE790_GCR,
    580        1.1  jdolecek 		    gcr | WE790_GCR_LIT);
    581        1.1  jdolecek 		bus_space_write_1(asict, asich, WE790_HWR,
    582        1.1  jdolecek 		    hwr & ~WE790_HWR_SWH);
    583        1.1  jdolecek 		return;
    584        1.1  jdolecek 	}
    585        1.1  jdolecek 
    586        1.1  jdolecek 	irr = bus_space_read_1(wsc->sc_asict, wsc->sc_asich, WE_IRR);
    587        1.1  jdolecek 	if (IFM_SUBTYPE(media) == IFM_10_2)
    588        1.1  jdolecek 		irr |= WE_IRR_OUT2;
    589        1.1  jdolecek 	else
    590        1.1  jdolecek 		irr &= ~WE_IRR_OUT2;
    591        1.1  jdolecek 	bus_space_write_1(wsc->sc_asict, wsc->sc_asich, WE_IRR, irr);
    592        1.1  jdolecek }
    593