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