Home | History | Annotate | Line # | Download | only in isa
if_iy.c revision 1.114
      1  1.114   thorpej /*	$NetBSD: if_iy.c,v 1.114 2022/09/17 17:21:52 thorpej Exp $	*/
      2    1.1        is /* #define IYDEBUG */
      3    1.1        is /* #define IYMEMDEBUG */
      4   1.30        is 
      5    1.1        is /*-
      6   1.52        is  * Copyright (c) 1996,2001 The NetBSD Foundation, Inc.
      7    1.1        is  * All rights reserved.
      8    1.1        is  *
      9   1.30        is  * This code is derived from software contributed to The NetBSD Foundation
     10   1.30        is  * by Ignatios Souvatzis.
     11   1.30        is  *
     12    1.1        is  * Redistribution and use in source and binary forms, with or without
     13    1.1        is  * modification, are permitted provided that the following conditions
     14    1.1        is  * are met:
     15    1.1        is  * 1. Redistributions of source code must retain the above copyright
     16    1.1        is  *    notice, this list of conditions and the following disclaimer.
     17    1.1        is  * 2. Redistributions in binary form must reproduce the above copyright
     18    1.1        is  *    notice, this list of conditions and the following disclaimer in the
     19    1.1        is  *    documentation and/or other materials provided with the distribution.
     20    1.1        is  *
     21   1.30        is  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     22   1.30        is  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     23   1.30        is  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     24   1.30        is  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     25   1.30        is  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     26   1.30        is  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     27   1.30        is  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     28   1.30        is  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     29   1.30        is  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     30   1.30        is  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     31   1.30        is  * POSSIBILITY OF SUCH DAMAGE.
     32   1.27        is  */
     33   1.27        is 
     34   1.27        is /*
     35   1.27        is  * Supported hardware:
     36   1.27        is  *
     37   1.27        is  * - Intel EtherExpress Pro/10.
     38   1.27        is  * - possibly other boards using the i82595 chip and no special tweaks.
     39    1.1        is  */
     40   1.56     lukem 
     41   1.56     lukem #include <sys/cdefs.h>
     42  1.114   thorpej __KERNEL_RCSID(0, "$NetBSD: if_iy.c,v 1.114 2022/09/17 17:21:52 thorpej Exp $");
     43    1.1        is 
     44   1.22  jonathan #include "opt_inet.h"
     45    1.1        is 
     46    1.1        is #include <sys/param.h>
     47    1.1        is #include <sys/systm.h>
     48    1.1        is #include <sys/mbuf.h>
     49    1.1        is #include <sys/buf.h>
     50    1.1        is #include <sys/protosw.h>
     51    1.1        is #include <sys/socket.h>
     52    1.1        is #include <sys/ioctl.h>
     53    1.1        is #include <sys/errno.h>
     54    1.1        is #include <sys/syslog.h>
     55    1.1        is #include <sys/device.h>
     56   1.41        is #include <sys/endian.h>
     57   1.94  riastrad #include <sys/rndsource.h>
     58    1.1        is 
     59    1.1        is #include <net/if.h>
     60    1.1        is #include <net/if_types.h>
     61    1.1        is #include <net/if_dl.h>
     62  1.101   msaitoh #include <net/bpf.h>
     63   1.10        is #include <net/if_ether.h>
     64  1.105   msaitoh #include <net/if_media.h>
     65    1.1        is 
     66    1.1        is #ifdef INET
     67    1.1        is #include <netinet/in.h>
     68    1.1        is #include <netinet/in_systm.h>
     69    1.1        is #include <netinet/in_var.h>
     70    1.1        is #include <netinet/ip.h>
     71   1.10        is #include <netinet/if_inarp.h>
     72    1.1        is #endif
     73    1.1        is 
     74    1.1        is 
     75   1.78        ad #include <sys/cpu.h>
     76   1.78        ad #include <sys/bus.h>
     77   1.78        ad #include <sys/intr.h>
     78    1.1        is 
     79    1.1        is #include <dev/isa/isareg.h>
     80    1.1        is #include <dev/isa/isavar.h>
     81    1.1        is #include <dev/ic/i82595reg.h>
     82    1.1        is 
     83   1.41        is /* XXX why isn't this centralized? */
     84   1.41        is #ifndef __BUS_SPACE_HAS_STREAM_METHODS
     85   1.41        is #define bus_space_write_stream_2	bus_space_write_2
     86   1.41        is #define bus_space_write_multi_stream_2	bus_space_write_multi_2
     87   1.41        is #define bus_space_read_stream_2		bus_space_read_2
     88   1.41        is #define bus_space_read_multi_stream_2	bus_space_read_multi_2
     89   1.41        is #endif /* __BUS_SPACE_HAS_STREAM_METHODS */
     90   1.41        is 
     91    1.1        is /*
     92    1.1        is  * Ethernet status, per interface.
     93    1.1        is  */
     94    1.1        is struct iy_softc {
     95   1.91       chs 	device_t sc_dev;
     96    1.1        is 	void *sc_ih;
     97    1.1        is 
     98    1.9   thorpej 	bus_space_tag_t sc_iot;
     99    1.9   thorpej 	bus_space_handle_t sc_ioh;
    100    1.6        is 
    101   1.10        is 	struct ethercom sc_ethercom;
    102    1.1        is 
    103   1.18    bouyer 	struct ifmedia iy_ifmedia;
    104   1.18    bouyer 	int iy_media;
    105   1.18    bouyer 
    106    1.1        is 	int mappedirq;
    107    1.1        is 
    108    1.1        is 	int hard_vers;
    109    1.1        is 
    110  1.110   msaitoh 	u_short promisc;
    111    1.1        is 
    112    1.1        is 	int sram, tx_size, rx_size;
    113    1.1        is 
    114    1.1        is 	int tx_start, tx_end, tx_last;
    115  1.114   thorpej 	bool tx_busy;
    116  1.114   thorpej 
    117    1.1        is 	int rx_start;
    118    1.1        is 
    119   1.26        is 	int doing_mc_setup;
    120    1.1        is #ifdef IYDEBUG
    121    1.1        is 	int sc_debug;
    122    1.1        is #endif
    123   1.14  explorer 
    124   1.89       tls 	krndsource_t rnd_source;
    125    1.1        is };
    126    1.1        is 
    127   1.66     perry void iywatchdog(struct ifnet *);
    128   1.75  christos int iyioctl(struct ifnet *, u_long, void *);
    129   1.66     perry int iyintr(void *);
    130   1.66     perry void iyinit(struct iy_softc *);
    131   1.66     perry void iystop(struct iy_softc *);
    132   1.66     perry void iystart(struct ifnet *);
    133   1.66     perry 
    134   1.66     perry void iy_intr_rx(struct iy_softc *);
    135   1.66     perry void iy_intr_tx(struct iy_softc *);
    136   1.66     perry 
    137   1.66     perry void iyreset(struct iy_softc *);
    138   1.66     perry void iy_readframe(struct iy_softc *, int);
    139   1.66     perry void iy_drop_packet_buffer(struct iy_softc *);
    140   1.66     perry void iy_find_mem_size(struct iy_softc *);
    141   1.66     perry void iyrint(struct iy_softc *);
    142   1.66     perry void iytint(struct iy_softc *);
    143   1.66     perry void iyxmit(struct iy_softc *);
    144   1.66     perry static void iy_mc_setup(struct iy_softc *);
    145   1.66     perry static void iy_mc_reset(struct iy_softc *);
    146   1.66     perry void iyget(struct iy_softc *, bus_space_tag_t, bus_space_handle_t, int);
    147   1.66     perry void iyprobemem(struct iy_softc *);
    148   1.70     perry static inline void eepromwritebit(bus_space_tag_t, bus_space_handle_t, int);
    149   1.70     perry static inline int eepromreadbit(bus_space_tag_t, bus_space_handle_t);
    150   1.26        is 
    151    1.1        is #ifdef IYDEBUGX
    152   1.66     perry void print_rbd(volatile struct iy_recv_buf_desc *);
    153    1.1        is 
    154    1.1        is int in_ifrint = 0;
    155    1.1        is int in_iftint = 0;
    156    1.1        is #endif
    157    1.1        is 
    158   1.66     perry int iy_mediachange(struct ifnet *);
    159   1.66     perry void iy_mediastatus(struct ifnet *, struct ifmediareq *);
    160   1.18    bouyer 
    161   1.86    cegger int iyprobe(device_t, cfdata_t, void *);
    162   1.86    cegger void iyattach(device_t, device_t, void *);
    163    1.1        is 
    164  1.106   msaitoh static uint16_t eepromread(bus_space_tag_t, bus_space_handle_t, int);
    165   1.10        is 
    166  1.106   msaitoh static int eepromreadall(bus_space_tag_t, bus_space_handle_t, uint16_t *,
    167   1.66     perry     int);
    168    1.1        is 
    169   1.91       chs CFATTACH_DECL_NEW(iy, sizeof(struct iy_softc),
    170   1.61   thorpej     iyprobe, iyattach, NULL, NULL);
    171    1.1        is 
    172  1.106   msaitoh static uint8_t eepro_irqmap[] = EEPP_INTMAP;
    173  1.106   msaitoh static uint8_t eepro_revirqmap[] = EEPP_RINTMAP;
    174    1.1        is 
    175    1.1        is int
    176   1.86    cegger iyprobe(device_t parent, cfdata_t match, void *aux)
    177    1.1        is {
    178    1.1        is 	struct isa_attach_args *ia = aux;
    179  1.106   msaitoh 	uint16_t eaddr[8];
    180    1.9   thorpej 	bus_space_tag_t iot;
    181    1.9   thorpej 	bus_space_handle_t ioh;
    182  1.106   msaitoh 	uint8_t c, d;
    183   1.57   thorpej 	int irq;
    184   1.57   thorpej 
    185   1.57   thorpej 	if (ia->ia_nio < 1)
    186  1.106   msaitoh 		return 0;
    187   1.57   thorpej 	if (ia->ia_nirq < 1)
    188  1.106   msaitoh 		return 0;
    189    1.6        is 
    190   1.57   thorpej 	if (ISA_DIRECT_CONFIG(ia))
    191  1.106   msaitoh 		return 0;
    192    1.1        is 
    193    1.9   thorpej 	iot = ia->ia_iot;
    194   1.15  drochner 
    195   1.64  drochner 	if (ia->ia_io[0].ir_addr == ISA_UNKNOWN_PORT)
    196   1.15  drochner 		return 0;
    197   1.15  drochner 
    198   1.57   thorpej 	if (bus_space_map(iot, ia->ia_io[0].ir_addr, 16, 0, &ioh))
    199   1.10        is 		return 0;
    200    1.1        is 
    201    1.1        is 	/* try to find the round robin sig: */
    202    1.1        is 
    203    1.9   thorpej 	c = bus_space_read_1(iot, ioh, ID_REG);
    204   1.10        is 	if ((c & ID_REG_MASK) != ID_REG_SIG)
    205    1.6        is 		goto out;
    206    1.1        is 
    207    1.9   thorpej 	d = bus_space_read_1(iot, ioh, ID_REG);
    208   1.10        is 	if ((d & ID_REG_MASK) != ID_REG_SIG)
    209    1.6        is 		goto out;
    210    1.1        is 
    211    1.1        is 	if (((d-c) & R_ROBIN_BITS) != 0x40)
    212    1.6        is 		goto out;
    213   1.67     perry 
    214    1.9   thorpej 	d = bus_space_read_1(iot, ioh, ID_REG);
    215   1.10        is 	if ((d & ID_REG_MASK) != ID_REG_SIG)
    216    1.6        is 		goto out;
    217    1.1        is 
    218    1.1        is 	if (((d-c) & R_ROBIN_BITS) != 0x80)
    219    1.6        is 		goto out;
    220   1.67     perry 
    221    1.9   thorpej 	d = bus_space_read_1(iot, ioh, ID_REG);
    222   1.10        is 	if ((d & ID_REG_MASK) != ID_REG_SIG)
    223    1.6        is 		goto out;
    224    1.1        is 
    225    1.1        is 	if (((d-c) & R_ROBIN_BITS) != 0xC0)
    226    1.6        is 		goto out;
    227   1.67     perry 
    228    1.9   thorpej 	d = bus_space_read_1(iot, ioh, ID_REG);
    229   1.10        is 	if ((d & ID_REG_MASK) != ID_REG_SIG)
    230    1.6        is 		goto out;
    231    1.1        is 
    232    1.1        is 	if (((d-c) & R_ROBIN_BITS) != 0x00)
    233    1.6        is 		goto out;
    234   1.67     perry 
    235    1.1        is #ifdef IYDEBUG
    236   1.10        is 		printf("iyprobe verified working ID reg.\n");
    237    1.1        is #endif
    238   1.67     perry 
    239   1.10        is 	if (eepromreadall(iot, ioh, eaddr, 8))
    240   1.10        is 		goto out;
    241   1.67     perry 
    242   1.64  drochner 	if (ia->ia_irq[0].ir_irq == ISA_UNKNOWN_IRQ)
    243   1.57   thorpej 		irq = eepro_irqmap[eaddr[EEPPW1] & EEPP_Int];
    244   1.57   thorpej 	else
    245   1.57   thorpej 		irq = ia->ia_irq[0].ir_irq;
    246    1.1        is 
    247   1.57   thorpej 	if (irq >= sizeof(eepro_revirqmap))
    248    1.6        is 		goto out;
    249    1.1        is 
    250   1.57   thorpej 	if (eepro_revirqmap[irq] == 0xff)
    251    1.6        is 		goto out;
    252    1.1        is 
    253    1.1        is 	/* now lets reset the chip */
    254   1.67     perry 
    255    1.9   thorpej 	bus_space_write_1(iot, ioh, COMMAND_REG, RESET_CMD);
    256    1.1        is 	delay(200);
    257   1.67     perry 
    258   1.57   thorpej 	ia->ia_nio = 1;
    259   1.57   thorpej 	ia->ia_io[0].ir_size = 16;
    260   1.57   thorpej 
    261   1.57   thorpej 	ia->ia_nirq = 1;
    262   1.57   thorpej 	ia->ia_irq[0].ir_irq = irq;
    263   1.57   thorpej 
    264   1.57   thorpej 	ia->ia_niomem = 0;
    265   1.57   thorpej 	ia->ia_ndrq = 0;
    266    1.6        is 
    267   1.10        is 	bus_space_unmap(iot, ioh, 16);
    268    1.1        is 	return 1;		/* found */
    269    1.6        is out:
    270    1.9   thorpej 	bus_space_unmap(iot, ioh, 16);
    271    1.6        is 	return 0;
    272    1.1        is }
    273    1.1        is 
    274    1.1        is void
    275   1.86    cegger iyattach(device_t parent, device_t self, void *aux)
    276    1.1        is {
    277   1.91       chs 	struct iy_softc *sc = device_private(self);
    278    1.1        is 	struct isa_attach_args *ia = aux;
    279   1.10        is 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    280    1.9   thorpej 	bus_space_tag_t iot;
    281    1.9   thorpej 	bus_space_handle_t ioh;
    282   1.10        is 	unsigned temp;
    283  1.106   msaitoh 	uint16_t eaddr[8];
    284  1.106   msaitoh 	uint8_t myaddr[ETHER_ADDR_LEN];
    285   1.10        is 	int eirq;
    286    1.6        is 
    287  1.111   msaitoh 	sc->sc_dev = self;
    288   1.10        is 	iot = ia->ia_iot;
    289   1.67     perry 
    290   1.57   thorpej 	if (bus_space_map(iot, ia->ia_io[0].ir_addr, 16, 0, &ioh)) {
    291   1.97   msaitoh 		aprint_error(": can't map i/o space\n");
    292   1.16   thorpej 		return;
    293   1.16   thorpej 	}
    294   1.10        is 
    295   1.10        is 	sc->sc_iot = iot;
    296   1.10        is 	sc->sc_ioh = ioh;
    297   1.10        is 
    298   1.57   thorpej 	sc->mappedirq = eepro_revirqmap[ia->ia_irq[0].ir_irq];
    299   1.10        is 
    300   1.10        is 	/* now let's reset the chip */
    301   1.67     perry 
    302   1.10        is 	bus_space_write_1(iot, ioh, COMMAND_REG, RESET_CMD);
    303   1.10        is 	delay(200);
    304   1.67     perry 
    305   1.10        is 	iyprobemem(sc);
    306    1.1        is 
    307   1.91       chs 	strlcpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ);
    308    1.2   thorpej 	ifp->if_softc = sc;
    309    1.1        is 	ifp->if_start = iystart;
    310  1.104   msaitoh 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    311   1.26        is 
    312   1.26        is 	sc->doing_mc_setup = 0;
    313    1.1        is 
    314    1.1        is 	ifp->if_ioctl = iyioctl;
    315    1.1        is 	ifp->if_watchdog = iywatchdog;
    316    1.1        is 
    317   1.45   thorpej 	IFQ_SET_READY(&ifp->if_snd);
    318   1.45   thorpej 
    319   1.10        is 	(void)eepromreadall(iot, ioh, eaddr, 8);
    320   1.10        is 	sc->hard_vers = eaddr[EEPW6] & EEPP_BoardRev;
    321   1.10        is 
    322   1.10        is #ifdef DIAGNOSTICS
    323   1.67     perry 	if ((eaddr[EEPPEther0] !=
    324   1.10        is 	     eepromread(iot, ioh, EEPPEther0a)) &&
    325   1.67     perry 	    (eaddr[EEPPEther1] !=
    326   1.10        is 	     eepromread(iot, ioh, EEPPEther1a)) &&
    327   1.67     perry 	    (eaddr[EEPPEther2] !=
    328   1.10        is 	     eepromread(iot, ioh, EEPPEther2a)))
    329   1.10        is 
    330   1.97   msaitoh 		aprint_error("EEPROM Ethernet address differs from copy\n");
    331   1.10        is #endif
    332   1.10        is 
    333  1.107   msaitoh 	myaddr[1] = eaddr[EEPPEther0] & 0xFF;
    334  1.107   msaitoh 	myaddr[0] = eaddr[EEPPEther0] >> 8;
    335  1.107   msaitoh 	myaddr[3] = eaddr[EEPPEther1] & 0xFF;
    336  1.107   msaitoh 	myaddr[2] = eaddr[EEPPEther1] >> 8;
    337  1.107   msaitoh 	myaddr[5] = eaddr[EEPPEther2] & 0xFF;
    338  1.107   msaitoh 	myaddr[4] = eaddr[EEPPEther2] >> 8;
    339   1.67     perry 
    340  1.109   msaitoh 	/* Initialize ifmedia structures. */
    341  1.109   msaitoh 	sc->sc_ethercom.ec_ifmedia = &sc->iy_ifmedia;
    342   1.18    bouyer 	ifmedia_init(&sc->iy_ifmedia, 0, iy_mediachange, iy_mediastatus);
    343   1.18    bouyer 	ifmedia_add(&sc->iy_ifmedia, IFM_ETHER | IFM_10_2, 0, NULL);
    344   1.18    bouyer 	ifmedia_add(&sc->iy_ifmedia, IFM_ETHER | IFM_10_5, 0, NULL);
    345   1.18    bouyer 	ifmedia_add(&sc->iy_ifmedia, IFM_ETHER | IFM_10_T, 0, NULL);
    346   1.18    bouyer 	ifmedia_add(&sc->iy_ifmedia, IFM_ETHER | IFM_AUTO, 0, NULL);
    347   1.18    bouyer 	ifmedia_set(&sc->iy_ifmedia, IFM_ETHER | IFM_AUTO);
    348    1.1        is 	/* Attach the interface. */
    349    1.1        is 	if_attach(ifp);
    350   1.10        is 	ether_ifattach(ifp, myaddr);
    351   1.97   msaitoh 	aprint_normal(": address %s, rev. %d, %d kB\n",
    352   1.10        is 	    ether_sprintf(myaddr),
    353    1.1        is 	    sc->hard_vers, sc->sram/1024);
    354   1.10        is 
    355   1.10        is 	eirq = eepro_irqmap[eaddr[EEPPW1] & EEPP_Int];
    356   1.57   thorpej 	if (eirq != ia->ia_irq[0].ir_irq)
    357   1.97   msaitoh 		aprint_error("%s: EEPROM irq setting %d ignored\n",
    358   1.91       chs 		    device_xname(sc->sc_dev), eirq);
    359    1.1        is 
    360   1.57   thorpej 	sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq[0].ir_irq,
    361   1.57   thorpej 	    IST_EDGE, IPL_NET, iyintr, sc);
    362   1.10        is 
    363   1.91       chs 	rnd_attach_source(&sc->rnd_source, device_xname(sc->sc_dev),
    364   1.93       tls 			  RND_TYPE_NET, RND_FLAG_DEFAULT);
    365   1.14  explorer 
    366   1.10        is 	temp = bus_space_read_1(iot, ioh, INT_NO_REG);
    367   1.10        is 	bus_space_write_1(iot, ioh, INT_NO_REG, (temp & 0xf8) | sc->mappedirq);
    368    1.1        is }
    369    1.1        is 
    370    1.1        is void
    371   1.84       dsl iystop(struct iy_softc *sc)
    372    1.1        is {
    373    1.9   thorpej 	bus_space_tag_t iot;
    374    1.9   thorpej 	bus_space_handle_t ioh;
    375    1.1        is #ifdef IYDEBUG
    376    1.1        is 	u_int p, v;
    377    1.1        is #endif
    378    1.1        is 
    379    1.9   thorpej 	iot = sc->sc_iot;
    380    1.6        is 	ioh = sc->sc_ioh;
    381   1.67     perry 
    382    1.9   thorpej 	bus_space_write_1(iot, ioh, COMMAND_REG, RCV_DISABLE_CMD);
    383    1.1        is 
    384    1.9   thorpej 	bus_space_write_1(iot, ioh, INT_MASK_REG, ALL_INTS);
    385    1.9   thorpej 	bus_space_write_1(iot, ioh, STATUS_REG, ALL_INTS);
    386    1.1        is 
    387    1.9   thorpej 	bus_space_write_1(iot, ioh, COMMAND_REG, RESET_CMD);
    388    1.1        is 	delay(200);
    389   1.67     perry #ifdef IYDEBUG
    390   1.67     perry 	printf("%s: dumping tx chain (st 0x%x end 0x%x last 0x%x)\n",
    391   1.97   msaitoh 	    device_xname(sc->sc_dev), sc->tx_start, sc->tx_end, sc->tx_last);
    392    1.1        is 	p = sc->tx_last;
    393    1.1        is 	if (!p)
    394    1.1        is 		p = sc->tx_start;
    395    1.1        is 	do {
    396   1.42        tv 		char sbuf[128];
    397   1.42        tv 
    398    1.9   thorpej 		bus_space_write_2(iot, ioh, HOST_ADDR_REG, p);
    399   1.42        tv 
    400   1.41        is 		v = le16toh(bus_space_read_stream_2(iot, ioh, MEM_PORT_REG));
    401   1.82  christos 		snprintb(sbuf, sizeof(sbuf), "\020\006Ab\010Dn", v);
    402   1.42        tv 		printf("0x%04x: %s ", p, sbuf);
    403   1.42        tv 
    404   1.41        is 		v = le16toh(bus_space_read_stream_2(iot, ioh, MEM_PORT_REG));
    405   1.97   msaitoh 		snprintb(sbuf, sizeof(sbuf),
    406   1.82  christos 		    "\020\6MAX_COL\7HRT_BEAT\010TX_DEF\011UND_RUN"
    407   1.82  christos 		    "\012JERR\013LST_CRS\014LTCOL\016TX_OK\020COLL", v);
    408  1.100   msaitoh 		printf("%s", sbuf);
    409   1.42        tv 
    410   1.41        is 		p = le16toh(bus_space_read_stream_2(iot, ioh, MEM_PORT_REG));
    411    1.8  christos 		printf(" 0x%04x", p);
    412   1.42        tv 
    413   1.41        is 		v = le16toh(bus_space_read_stream_2(iot, ioh, MEM_PORT_REG));
    414   1.82  christos 		snprintb(sbuf, sizeof(sbuf), "\020\020Ch", v);
    415  1.100   msaitoh 		printf(" %s\n", sbuf);
    416   1.42        tv 
    417    1.1        is 	} while (v & 0x8000);
    418    1.1        is #endif
    419    1.1        is 	sc->tx_start = sc->tx_end = sc->rx_size;
    420    1.1        is 	sc->tx_last = 0;
    421  1.114   thorpej 	sc->sc_ethercom.ec_if.if_flags &= ~IFF_RUNNING;
    422  1.114   thorpej 	sc->tx_busy = false;
    423    1.1        is }
    424    1.1        is 
    425    1.1        is void
    426   1.84       dsl iyreset(struct iy_softc *sc)
    427    1.1        is {
    428    1.1        is 	int s;
    429   1.29   mycroft 	s = splnet();
    430    1.1        is 	iystop(sc);
    431    1.1        is 	iyinit(sc);
    432    1.1        is 	splx(s);
    433    1.1        is }
    434    1.1        is 
    435    1.1        is void
    436   1.84       dsl iyinit(struct iy_softc *sc)
    437    1.1        is {
    438    1.1        is 	int i;
    439    1.1        is 	unsigned temp;
    440    1.1        is 	struct ifnet *ifp;
    441    1.9   thorpej 	bus_space_tag_t iot;
    442    1.9   thorpej 	bus_space_handle_t ioh;
    443    1.6        is 
    444    1.9   thorpej 	iot = sc->sc_iot;
    445    1.6        is 	ioh = sc->sc_ioh;
    446    1.1        is 
    447   1.10        is 	ifp = &sc->sc_ethercom.ec_if;
    448    1.1        is #ifdef IYDEBUG
    449    1.8  christos 	printf("ifp is %p\n", ifp);
    450    1.1        is #endif
    451    1.1        is 
    452    1.9   thorpej 	bus_space_write_1(iot, ioh, 0, BANK_SEL(2));
    453    1.1        is 
    454    1.9   thorpej 	temp = bus_space_read_1(iot, ioh, EEPROM_REG);
    455    1.1        is 	if (temp & 0x10)
    456    1.9   thorpej 		bus_space_write_1(iot, ioh, EEPROM_REG, temp & ~0x10);
    457   1.67     perry 
    458    1.1        is 	for (i=0; i<6; ++i) {
    459   1.76    dyoung 		bus_space_write_1(iot, ioh, I_ADD(i), CLLADDR(ifp->if_sadl)[i]);
    460    1.1        is 	}
    461    1.1        is 
    462    1.9   thorpej 	temp = bus_space_read_1(iot, ioh, REG1);
    463   1.10        is 	bus_space_write_1(iot, ioh, REG1,
    464   1.49        is 	    temp | /* XMT_CHAIN_INT | XMT_CHAIN_ERRSTOP | */ RCV_DISCARD_BAD);
    465   1.67     perry 
    466  1.106   msaitoh 	if (ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI))
    467   1.25        is 		temp = MATCH_ALL;
    468  1.106   msaitoh 	else
    469   1.51        is 		temp = MATCH_BRDCST;
    470   1.25        is 
    471   1.25        is 	bus_space_write_1(iot, ioh, RECV_MODES_REG, temp);
    472   1.26        is 
    473    1.1        is #ifdef IYDEBUG
    474   1.42        tv 	{
    475   1.42        tv 		char sbuf[128];
    476   1.42        tv 
    477   1.97   msaitoh 		snprintb(sbuf, sizeof(sbuf),
    478   1.82  christos 		    "\020\1PRMSC\2NOBRDST\3SEECRC\4LENGTH\5NOSaIns\6MultiIA",
    479   1.82  christos 		    temp);
    480   1.97   msaitoh 
    481   1.97   msaitoh 		printf("%s: RECV_MODES set to %s\n", device_xname(sc->sc_dev),
    482   1.97   msaitoh 		    sbuf);
    483   1.42        tv 	}
    484    1.1        is #endif
    485   1.26        is 	/* XXX VOODOO */
    486   1.26        is 	temp = bus_space_read_1(iot, ioh, MEDIA_SELECT);
    487   1.26        is 	bus_space_write_1(iot, ioh, MEDIA_SELECT, temp);
    488   1.26        is 	/* XXX END OF VOODOO */
    489    1.1        is 
    490    1.1        is 
    491   1.10        is 	delay(500000); /* for the hardware to test for the connector */
    492    1.1        is 
    493    1.9   thorpej 	temp = bus_space_read_1(iot, ioh, MEDIA_SELECT);
    494    1.1        is #ifdef IYDEBUG
    495   1.42        tv 	{
    496   1.42        tv 		char sbuf[128];
    497   1.42        tv 
    498   1.82  christos 		snprintb(sbuf, sizeof(sbuf),
    499   1.83    cegger 		    "\020\1LnkInDis\2PolCor\3TPE\4JabberDis\5NoAport\6BNC",
    500   1.83    cegger 		    temp);
    501  1.100   msaitoh 		printf("%s: media select was %s ", device_xname(sc->sc_dev),
    502   1.97   msaitoh 		    sbuf);
    503   1.42        tv 	}
    504    1.1        is #endif
    505    1.5        is 	temp = (temp & TEST_MODE_MASK);
    506   1.67     perry 
    507  1.106   msaitoh 	switch (IFM_SUBTYPE(sc->iy_ifmedia.ifm_media)) {
    508   1.18    bouyer 	case IFM_10_5:
    509    1.6        is 		temp &= ~ (BNC_BIT | TPE_BIT);
    510    1.6        is 		break;
    511    1.5        is 
    512   1.18    bouyer 	case IFM_10_2:
    513   1.10        is 		temp = (temp & ~TPE_BIT) | BNC_BIT;
    514    1.6        is 		break;
    515   1.67     perry 
    516   1.18    bouyer 	case IFM_10_T:
    517   1.10        is 		temp = (temp & ~BNC_BIT) | TPE_BIT;
    518    1.6        is 		break;
    519    1.5        is 	default:
    520   1.53     lukem 		;
    521    1.6        is 		/* nothing; leave as it is */
    522    1.5        is 	}
    523   1.18    bouyer 	switch (temp & (BNC_BIT | TPE_BIT)) {
    524   1.18    bouyer 	case BNC_BIT:
    525   1.18    bouyer 		sc->iy_media = IFM_ETHER | IFM_10_2;
    526   1.18    bouyer 		break;
    527   1.18    bouyer 	case TPE_BIT:
    528   1.18    bouyer 		sc->iy_media = IFM_ETHER | IFM_10_T;
    529   1.18    bouyer 		break;
    530   1.18    bouyer 	default:
    531   1.18    bouyer 		sc->iy_media = IFM_ETHER | IFM_10_5;
    532   1.18    bouyer 	}
    533    1.6        is 
    534    1.9   thorpej 	bus_space_write_1(iot, ioh, MEDIA_SELECT, temp);
    535    1.1        is #ifdef IYDEBUG
    536   1.42        tv 	{
    537   1.42        tv 		char sbuf[128];
    538   1.42        tv 
    539   1.82  christos 		snprintb(sbuf, sizeof(sbuf),
    540   1.82  christos 		    "\020\1LnkInDis\2PolCor\3TPE\4JabberDis\5NoAport\6BNC",
    541   1.82  christos 		    temp);
    542  1.100   msaitoh 		printf("changed to %s\n", sbuf);
    543   1.42        tv 	}
    544    1.1        is #endif
    545    1.1        is 
    546   1.10        is 	bus_space_write_1(iot, ioh, 0, BANK_SEL(0));
    547   1.10        is 	bus_space_write_1(iot, ioh, INT_MASK_REG, ALL_INTS);
    548    1.9   thorpej 	bus_space_write_1(iot, ioh, 0, BANK_SEL(1));
    549    1.1        is 
    550    1.9   thorpej 	temp = bus_space_read_1(iot, ioh, INT_NO_REG);
    551    1.9   thorpej 	bus_space_write_1(iot, ioh, INT_NO_REG, (temp & 0xf8) | sc->mappedirq);
    552    1.1        is 
    553    1.1        is #ifdef IYDEBUG
    554   1.42        tv 	{
    555   1.42        tv 		char sbuf[128];
    556   1.42        tv 
    557   1.82  christos 		snprintb(sbuf, sizeof(sbuf),
    558   1.82  christos 		    "\020\4bad_irq\010flash/boot present", temp);
    559   1.97   msaitoh 
    560   1.91       chs 		printf("%s: int no was %s\n", device_xname(sc->sc_dev), sbuf);
    561   1.42        tv 
    562   1.42        tv 		temp = bus_space_read_1(iot, ioh, INT_NO_REG);
    563   1.82  christos 		snprintb(sbuf, sizeof(sbuf),
    564   1.82  christos 		    "\020\4bad_irq\010flash/boot present", temp);
    565   1.91       chs 		printf("%s: int no now %s\n", device_xname(sc->sc_dev), sbuf);
    566   1.42        tv 	}
    567    1.1        is #endif
    568    1.1        is 
    569    1.9   thorpej 	bus_space_write_1(iot, ioh, RCV_LOWER_LIMIT_REG, 0);
    570   1.48        is 	bus_space_write_1(iot, ioh, RCV_UPPER_LIMIT_REG, (sc->rx_size -2) >>8);
    571   1.48        is 	bus_space_write_1(iot, ioh, XMT_LOWER_LIMIT_REG, sc->rx_size >>8);
    572   1.48        is 	bus_space_write_1(iot, ioh, XMT_UPPER_LIMIT_REG, (sc->sram - 2) >>8);
    573    1.1        is 
    574    1.9   thorpej 	temp = bus_space_read_1(iot, ioh, REG1);
    575    1.1        is #ifdef IYDEBUG
    576   1.42        tv 	{
    577   1.42        tv 		char sbuf[128];
    578   1.42        tv 
    579   1.82  christos 		snprintb(sbuf, sizeof(sbuf), "\020\2WORD_WIDTH\010INT_ENABLE",
    580   1.82  christos 		    temp);
    581   1.97   msaitoh 
    582   1.91       chs 		printf("%s: HW access is %s\n", device_xname(sc->sc_dev), sbuf);
    583   1.42        tv 	}
    584    1.1        is #endif
    585    1.9   thorpej 	bus_space_write_1(iot, ioh, REG1, temp | INT_ENABLE); /* XXX what about WORD_WIDTH? */
    586    1.1        is 
    587    1.1        is #ifdef IYDEBUG
    588   1.42        tv 	{
    589   1.42        tv 		char sbuf[128];
    590   1.42        tv 
    591   1.42        tv 		temp = bus_space_read_1(iot, ioh, REG1);
    592   1.82  christos 		snprintb(sbuf, sizeof(sbuf), "\020\2WORD_WIDTH\010INT_ENABLE",
    593   1.82  christos 		    temp);
    594   1.91       chs 		printf("%s: HW access is %s\n", device_xname(sc->sc_dev), sbuf);
    595   1.42        tv 	}
    596    1.1        is #endif
    597    1.1        is 
    598    1.9   thorpej 	bus_space_write_1(iot, ioh, 0, BANK_SEL(0));
    599    1.1        is 
    600  1.106   msaitoh 	bus_space_write_1(iot, ioh, INT_MASK_REG,
    601  1.106   msaitoh 	    ALL_INTS & ~(RX_BIT | TX_BIT));
    602    1.9   thorpej 	bus_space_write_1(iot, ioh, STATUS_REG, ALL_INTS); /* clear ints */
    603    1.1        is 
    604   1.50        is 	bus_space_write_1(iot, ioh, RCV_COPY_THRESHOLD, 0);
    605   1.50        is 
    606    1.9   thorpej 	bus_space_write_2(iot, ioh, RCV_START_LOW, 0);
    607    1.9   thorpej 	bus_space_write_2(iot, ioh, RCV_STOP_LOW,  sc->rx_size - 2);
    608    1.1        is 	sc->rx_start = 0;
    609    1.1        is 
    610    1.9   thorpej 	bus_space_write_1(iot, ioh, 0, SEL_RESET_CMD);
    611   1.10        is 	delay(200);
    612    1.1        is 
    613    1.9   thorpej 	bus_space_write_2(iot, ioh, XMT_ADDR_REG, sc->rx_size);
    614    1.1        is 
    615    1.1        is 	sc->tx_start = sc->tx_end = sc->rx_size;
    616    1.1        is 	sc->tx_last = 0;
    617    1.1        is 
    618    1.9   thorpej 	bus_space_write_1(iot, ioh, 0, RCV_ENABLE_CMD);
    619    1.1        is 
    620    1.1        is 	ifp->if_flags |= IFF_RUNNING;
    621  1.114   thorpej 	sc->tx_busy = false;
    622    1.1        is }
    623    1.1        is 
    624    1.1        is void
    625   1.84       dsl iystart(struct ifnet *ifp)
    626    1.1        is {
    627    1.1        is 	struct iy_softc *sc;
    628    1.6        is 
    629    1.1        is 
    630    1.1        is 	struct mbuf *m0, *m;
    631    1.1        is 	u_int len, pad, last, end;
    632    1.1        is 	u_int llen, residual;
    633    1.1        is 	int avail;
    634   1.75  christos 	char *data;
    635   1.52        is 	unsigned temp;
    636  1.106   msaitoh 	uint16_t resval, stat;
    637    1.9   thorpej 	bus_space_tag_t iot;
    638    1.9   thorpej 	bus_space_handle_t ioh;
    639    1.1        is 
    640    1.1        is #ifdef IYDEBUG
    641    1.8  christos 	printf("iystart called\n");
    642    1.1        is #endif
    643   1.26        is 	sc = ifp->if_softc;
    644   1.26        is 
    645  1.114   thorpej 	if ((ifp->if_flags & IFF_RUNNING) == 0)
    646  1.114   thorpej 		return;
    647  1.114   thorpej 
    648  1.114   thorpej 	if (sc->tx_busy)
    649  1.107   msaitoh 		return;
    650    1.1        is 
    651   1.52        is 	iy_intr_tx(sc);
    652   1.52        is 
    653    1.9   thorpej 	iot = sc->sc_iot;
    654    1.6        is 	ioh = sc->sc_ioh;
    655    1.1        is 
    656   1.45   thorpej 	for (;;) {
    657   1.45   thorpej 		IFQ_POLL(&ifp->if_snd, m0);
    658   1.45   thorpej 		if (m0 == NULL)
    659   1.45   thorpej 			break;
    660    1.1        is #ifdef IYDEBUG
    661    1.8  christos 		printf("%s: trying to write another packet to the hardware\n",
    662   1.91       chs 		    device_xname(sc->sc_dev));
    663    1.1        is #endif
    664    1.1        is 
    665    1.1        is 		/* We need to use m->m_pkthdr.len, so require the header */
    666    1.1        is 		if ((m0->m_flags & M_PKTHDR) == 0)
    667    1.1        is 			panic("iystart: no header mbuf");
    668    1.1        is 
    669    1.1        is 		len = m0->m_pkthdr.len;
    670    1.1        is 		pad = len & 1;
    671    1.1        is 
    672    1.1        is #ifdef IYDEBUG
    673   1.91       chs 		printf("%s: length is %d.\n", device_xname(sc->sc_dev), len);
    674    1.1        is #endif
    675   1.35   thorpej 		if (len < (ETHER_MIN_LEN - ETHER_CRC_LEN)) {
    676   1.35   thorpej 			pad = ETHER_MIN_LEN - ETHER_CRC_LEN - len;
    677    1.1        is 		}
    678    1.1        is 
    679  1.107   msaitoh 		if (len + pad > ETHER_MAX_LEN) {
    680  1.107   msaitoh 			/* packet is obviously too large: toss it */
    681  1.113   thorpej 			if_statinc(ifp, if_oerrors);
    682  1.107   msaitoh 			IFQ_DEQUEUE(&ifp->if_snd, m0);
    683  1.107   msaitoh 			m_freem(m0);
    684    1.1        is 			continue;
    685  1.107   msaitoh 		}
    686    1.1        is 
    687  1.102   msaitoh 		bpf_mtap(ifp, m0, BPF_D_OUT);
    688    1.1        is 
    689    1.1        is 		avail = sc->tx_start - sc->tx_end;
    690    1.1        is 		if (avail <= 0)
    691    1.1        is 			avail += sc->tx_size;
    692    1.1        is 
    693    1.1        is #ifdef IYDEBUG
    694   1.91       chs 		printf("%s: avail is %d.\n", device_xname(sc->sc_dev), avail);
    695    1.1        is #endif
    696   1.67     perry 		/*
    697   1.67     perry 		 * we MUST RUN at splnet here  ---
    698   1.67     perry 		 * XXX todo: or even turn off the boards ints ??? hm...
    699    1.1        is 		 */
    700   1.67     perry 
    701  1.107   msaitoh 		/* See if there is room to put another packet in the buffer. */
    702   1.67     perry 
    703    1.1        is 		if ((len+pad+2*I595_XMT_HDRLEN) > avail) {
    704   1.20        is #ifdef IYDEBUG
    705  1.114   thorpej 			printf("%s: len = %d, avail = %d, setting tx_busy\n",
    706   1.91       chs 			    device_xname(sc->sc_dev), len, avail);
    707   1.20        is #endif
    708   1.52        is 			/* mark interface as full ... */
    709  1.114   thorpej 			sc->tx_busy = true;
    710   1.52        is 
    711   1.52        is 			/* and wait for any transmission result */
    712   1.52        is 			bus_space_write_1(iot, ioh, 0, BANK_SEL(2));
    713   1.52        is 
    714   1.52        is 			temp = bus_space_read_1(iot, ioh, REG1);
    715   1.52        is 			bus_space_write_1(iot, ioh, REG1,
    716  1.107   msaitoh 				temp & ~XMT_CHAIN_INT);
    717   1.52        is 
    718   1.52        is 			bus_space_write_1(iot, ioh, 0, BANK_SEL(0));
    719   1.52        is 
    720    1.1        is 			return;
    721    1.1        is 		}
    722   1.67     perry 
    723    1.1        is 		/* we know it fits in the hardware now, so dequeue it */
    724   1.45   thorpej 		IFQ_DEQUEUE(&ifp->if_snd, m0);
    725   1.67     perry 
    726    1.1        is 		last = sc->tx_end;
    727   1.67     perry 		end = last + pad + len + I595_XMT_HDRLEN;
    728   1.67     perry 
    729    1.1        is 		if (end >= sc->sram) {
    730    1.1        is 			if ((sc->sram - last) <= I595_XMT_HDRLEN) {
    731    1.1        is 				/* keep header in one piece */
    732    1.1        is 				last = sc->rx_size;
    733    1.1        is 				end = last + pad + len + I595_XMT_HDRLEN;
    734    1.1        is 			} else
    735    1.1        is 				end -= sc->tx_size;
    736    1.1        is 		}
    737    1.1        is 
    738    1.9   thorpej 		bus_space_write_2(iot, ioh, HOST_ADDR_REG, last);
    739   1.41        is 		bus_space_write_stream_2(iot, ioh, MEM_PORT_REG,
    740   1.41        is 			htole16(XMT_CMD));
    741   1.41        is 
    742    1.9   thorpej 		bus_space_write_2(iot, ioh, MEM_PORT_REG, 0);
    743    1.9   thorpej 		bus_space_write_2(iot, ioh, MEM_PORT_REG, 0);
    744   1.41        is 
    745   1.67     perry 		bus_space_write_stream_2(iot, ioh, MEM_PORT_REG,
    746   1.41        is 			htole16(len + pad));
    747    1.1        is 
    748    1.1        is 		residual = resval = 0;
    749    1.1        is 
    750    1.1        is 		while ((m = m0)!=0) {
    751   1.75  christos 			data = mtod(m, void *);
    752    1.1        is 			llen = m->m_len;
    753    1.1        is 			if (residual) {
    754    1.1        is #ifdef IYDEBUG
    755    1.8  christos 				printf("%s: merging residual with next mbuf.\n",
    756   1.91       chs 				    device_xname(sc->sc_dev));
    757    1.1        is #endif
    758    1.1        is 				resval |= *data << 8;
    759   1.41        is 				bus_space_write_stream_2(iot, ioh,
    760   1.41        is 					MEM_PORT_REG, resval);
    761    1.1        is 				--llen;
    762    1.1        is 				++data;
    763    1.1        is 			}
    764   1.65   thorpej 			/*
    765   1.65   thorpej 			 * XXX ALIGNMENT LOSSAGE HERE.
    766   1.65   thorpej 			 */
    767    1.1        is 			if (llen > 1)
    768   1.41        is 				bus_space_write_multi_stream_2(iot, ioh,
    769  1.106   msaitoh 					MEM_PORT_REG, (uint16_t *) data,
    770   1.65   thorpej 					llen>>1);
    771    1.1        is 			residual = llen & 1;
    772    1.1        is 			if (residual) {
    773    1.1        is 				resval = *(data + llen - 1);
    774    1.1        is #ifdef IYDEBUG
    775    1.8  christos 				printf("%s: got odd mbuf to send.\n",
    776   1.91       chs 				    device_xname(sc->sc_dev));
    777    1.1        is #endif
    778    1.1        is 			}
    779    1.1        is 
    780   1.98  christos 			m0 = m_free(m);
    781    1.1        is 		}
    782    1.1        is 
    783    1.1        is 		if (residual)
    784   1.41        is 			bus_space_write_stream_2(iot, ioh, MEM_PORT_REG,
    785   1.41        is 				resval);
    786    1.1        is 
    787    1.1        is 		pad >>= 1;
    788    1.1        is 		while (pad-- > 0)
    789   1.41        is 			bus_space_write_stream_2(iot, ioh, MEM_PORT_REG, 0);
    790   1.67     perry 
    791    1.1        is #ifdef IYDEBUG
    792    1.8  christos 		printf("%s: new last = 0x%x, end = 0x%x.\n",
    793   1.91       chs 		    device_xname(sc->sc_dev), last, end);
    794    1.8  christos 		printf("%s: old start = 0x%x, end = 0x%x, last = 0x%x\n",
    795   1.97   msaitoh 		    device_xname(sc->sc_dev), sc->tx_start, sc->tx_end,
    796   1.97   msaitoh 		    sc->tx_last);
    797    1.1        is #endif
    798    1.1        is 
    799    1.1        is 		if (sc->tx_start != sc->tx_end) {
    800   1.41        is 			bus_space_write_2(iot, ioh, HOST_ADDR_REG,
    801   1.41        is 				sc->tx_last + XMT_COUNT);
    802    1.1        is 
    803   1.41        is 			/*
    804   1.41        is 			 * XXX We keep stat in le order, to potentially save
    805   1.41        is 			 * a byte swap.
    806   1.41        is 			 */
    807   1.41        is 			stat = bus_space_read_stream_2(iot, ioh, MEM_PORT_REG);
    808   1.41        is 
    809   1.41        is 			bus_space_write_2(iot, ioh, HOST_ADDR_REG,
    810   1.41        is 				sc->tx_last + XMT_CHAIN);
    811   1.41        is 
    812   1.41        is 			bus_space_write_stream_2(iot, ioh, MEM_PORT_REG,
    813   1.41        is 				htole16(last));
    814   1.41        is 
    815   1.41        is 			bus_space_write_stream_2(iot, ioh, MEM_PORT_REG,
    816   1.41        is 				stat | htole16(CHAIN));
    817    1.1        is #ifdef IYDEBUG
    818    1.8  christos 			printf("%s: setting 0x%x to 0x%x\n",
    819   1.91       chs 			    device_xname(sc->sc_dev), sc->tx_last + XMT_COUNT,
    820   1.41        is 			    le16toh(stat) | CHAIN);
    821    1.1        is #endif
    822    1.1        is 		}
    823  1.106   msaitoh 		/* dummy read */
    824  1.106   msaitoh 		stat = bus_space_read_2(iot, ioh, MEM_PORT_REG);
    825    1.1        is 
    826    1.1        is 		/* XXX todo: enable ints here if disabled */
    827   1.67     perry 
    828  1.113   thorpej 		if_statinc(ifp, if_opackets);
    829    1.1        is 
    830    1.1        is 		if (sc->tx_start == sc->tx_end) {
    831    1.9   thorpej 			bus_space_write_2(iot, ioh, XMT_ADDR_REG, last);
    832    1.9   thorpej 			bus_space_write_1(iot, ioh, 0, XMT_CMD);
    833    1.1        is 			sc->tx_start = last;
    834    1.1        is #ifdef IYDEBUG
    835    1.8  christos 			printf("%s: writing 0x%x to XAR and giving XCMD\n",
    836   1.91       chs 			    device_xname(sc->sc_dev), last);
    837    1.1        is #endif
    838    1.1        is 		} else {
    839    1.9   thorpej 			bus_space_write_1(iot, ioh, 0, RESUME_XMT_CMD);
    840    1.1        is #ifdef IYDEBUG
    841    1.8  christos 			printf("%s: giving RESUME_XCMD\n",
    842   1.91       chs 			    device_xname(sc->sc_dev));
    843    1.1        is #endif
    844    1.1        is 		}
    845    1.1        is 		sc->tx_last = last;
    846    1.1        is 		sc->tx_end = end;
    847    1.1        is 	}
    848   1.52        is 	/* and wait only for end of transmission chain */
    849   1.52        is 	bus_space_write_1(iot, ioh, 0, BANK_SEL(2));
    850   1.52        is 
    851   1.52        is 	temp = bus_space_read_1(iot, ioh, REG1);
    852   1.52        is 	bus_space_write_1(iot, ioh, REG1, temp | XMT_CHAIN_INT);
    853   1.52        is 
    854   1.52        is 	bus_space_write_1(iot, ioh, 0, BANK_SEL(0));
    855    1.1        is }
    856    1.1        is 
    857    1.1        is 
    858   1.70     perry static inline void
    859   1.84       dsl eepromwritebit(bus_space_tag_t iot, bus_space_handle_t ioh, int what)
    860    1.1        is {
    861   1.10        is 	bus_space_write_1(iot, ioh, EEPROM_REG, what);
    862    1.1        is 	delay(1);
    863  1.106   msaitoh 	bus_space_write_1(iot, ioh, EEPROM_REG, what | EESK);
    864    1.1        is 	delay(1);
    865   1.10        is 	bus_space_write_1(iot, ioh, EEPROM_REG, what);
    866    1.1        is 	delay(1);
    867    1.1        is }
    868    1.1        is 
    869   1.70     perry static inline int
    870   1.84       dsl eepromreadbit(bus_space_tag_t iot, bus_space_handle_t ioh)
    871    1.1        is {
    872   1.67     perry 	int b;
    873    1.1        is 
    874  1.106   msaitoh 	bus_space_write_1(iot, ioh, EEPROM_REG, EECS | EESK);
    875    1.1        is 	delay(1);
    876   1.10        is 	b = bus_space_read_1(iot, ioh, EEPROM_REG);
    877   1.10        is 	bus_space_write_1(iot, ioh, EEPROM_REG, EECS);
    878    1.1        is 	delay(1);
    879    1.1        is 
    880    1.1        is 	return ((b & EEDO) != 0);
    881    1.1        is }
    882    1.1        is 
    883  1.106   msaitoh static uint16_t
    884   1.84       dsl eepromread(bus_space_tag_t iot, bus_space_handle_t ioh, int offset)
    885    1.1        is {
    886    1.1        is 	volatile int i;
    887    1.1        is 	volatile int j;
    888  1.106   msaitoh 	volatile uint16_t readval;
    889    1.1        is 
    890    1.9   thorpej 	bus_space_write_1(iot, ioh, 0, BANK_SEL(2));
    891    1.1        is 	delay(1);
    892   1.10        is 	bus_space_write_1(iot, ioh, EEPROM_REG, EECS); /* XXXX??? */
    893    1.1        is 	delay(1);
    894   1.67     perry 
    895  1.106   msaitoh 	eepromwritebit(iot, ioh, EECS | EEDI);
    896  1.106   msaitoh 	eepromwritebit(iot, ioh, EECS | EEDI);
    897   1.10        is 	eepromwritebit(iot, ioh, EECS);
    898   1.67     perry 
    899    1.1        is 	for (j=5; j>=0; --j) {
    900   1.67     perry 		if ((offset>>j) & 1)
    901  1.106   msaitoh 			eepromwritebit(iot, ioh, EECS | EEDI);
    902    1.1        is 		else
    903   1.10        is 			eepromwritebit(iot, ioh, EECS);
    904    1.1        is 	}
    905    1.1        is 
    906    1.1        is 	for (readval=0, i=0; i<16; ++i) {
    907    1.1        is 		readval<<=1;
    908   1.10        is 		readval |= eepromreadbit(iot, ioh);
    909    1.1        is 	}
    910    1.1        is 
    911  1.106   msaitoh 	bus_space_write_1(iot, ioh, EEPROM_REG, 0 | EESK);
    912    1.1        is 	delay(1);
    913   1.10        is 	bus_space_write_1(iot, ioh, EEPROM_REG, 0);
    914    1.1        is 
    915   1.10        is 	bus_space_write_1(iot, ioh, COMMAND_REG, BANK_SEL(0));
    916    1.1        is 
    917    1.1        is 	return readval;
    918    1.1        is }
    919    1.1        is 
    920    1.1        is /*
    921    1.1        is  * Device timeout/watchdog routine.  Entered if the device neglects to generate
    922    1.1        is  * an interrupt after a transmit has been started on it.
    923    1.1        is  */
    924    1.1        is void
    925   1.84       dsl iywatchdog(struct ifnet *ifp)
    926    1.1        is {
    927    1.2   thorpej 	struct iy_softc *sc = ifp->if_softc;
    928    1.1        is 
    929   1.91       chs 	log(LOG_ERR, "%s: device timeout\n", device_xname(sc->sc_dev));
    930  1.113   thorpej 	if_statinc(ifp, if_oerrors);
    931    1.1        is 	iyreset(sc);
    932    1.1        is }
    933    1.1        is 
    934    1.1        is /*
    935    1.1        is  * What to do upon receipt of an interrupt.
    936    1.1        is  */
    937    1.1        is int
    938   1.84       dsl iyintr(void *arg)
    939    1.1        is {
    940   1.52        is 	struct iy_softc *sc;
    941   1.52        is 	struct ifnet *ifp;
    942    1.9   thorpej 	bus_space_tag_t iot;
    943    1.9   thorpej 	bus_space_handle_t ioh;
    944    1.6        is 
    945   1.36  augustss 	u_short status;
    946    1.1        is 
    947   1.52        is 	sc = arg;
    948    1.9   thorpej 	iot = sc->sc_iot;
    949    1.6        is 	ioh = sc->sc_ioh;
    950    1.6        is 
    951   1.52        is 	ifp = &sc->sc_ethercom.ec_if;
    952   1.52        is 
    953    1.9   thorpej 	status = bus_space_read_1(iot, ioh, STATUS_REG);
    954    1.1        is #ifdef IYDEBUG
    955    1.1        is 	if (status & ALL_INTS) {
    956   1.42        tv 		char sbuf[128];
    957   1.42        tv 
    958   1.82  christos 		snprintb(sbuf, sizeof(sbuf), "\020\1RX_STP\2RX\3TX\4EXEC",
    959   1.82  christos 		    status);
    960   1.91       chs 		printf("%s: got interrupt %s", device_xname(sc->sc_dev), sbuf);
    961   1.42        tv 
    962   1.42        tv 		if (status & EXEC_INT) {
    963   1.97   msaitoh 			snprintb(sbuf, sizeof(sbuf),
    964   1.82  christos 			     "\020\6ABORT", bus_space_read_1(iot, ioh, 0));
    965   1.42        tv 			printf(" event %s\n", sbuf);
    966   1.42        tv 		} else
    967    1.8  christos 			printf("\n");
    968    1.1        is 	}
    969    1.1        is #endif
    970   1.26        is 	if ((status & (RX_INT | TX_INT)) == 0)
    971    1.1        is 		return 0;
    972    1.1        is 
    973    1.1        is 	if (status & RX_INT) {
    974    1.1        is 		iy_intr_rx(sc);
    975    1.9   thorpej 		bus_space_write_1(iot, ioh, STATUS_REG, RX_INT);
    976   1.26        is 	}
    977   1.26        is 	if (status & TX_INT) {
    978   1.52        is 		/* Tell feeders we may be able to accept more data... */
    979  1.114   thorpej 		sc->tx_busy = false;
    980   1.52        is 		/* and get more data. */
    981   1.52        is 		iystart(ifp);
    982    1.9   thorpej 		bus_space_write_1(iot, ioh, STATUS_REG, TX_INT);
    983    1.1        is 	}
    984   1.14  explorer 
    985   1.14  explorer 	rnd_add_uint32(&sc->rnd_source, status);
    986   1.14  explorer 
    987    1.1        is 	return 1;
    988    1.1        is }
    989    1.1        is 
    990    1.1        is void
    991   1.97   msaitoh iyget(struct iy_softc *sc, bus_space_tag_t iot, bus_space_handle_t ioh,
    992   1.97   msaitoh     int rxlen)
    993    1.1        is {
    994    1.1        is 	struct mbuf *m, *top, **mp;
    995    1.1        is 	struct ifnet *ifp;
    996    1.1        is 	int len;
    997    1.1        is 
    998   1.10        is 	ifp = &sc->sc_ethercom.ec_if;
    999    1.1        is 
   1000   1.13   mycroft 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   1001   1.13   mycroft 	if (m == 0)
   1002   1.13   mycroft 		goto dropped;
   1003   1.96     ozaki 	m_set_rcvif(m, ifp);
   1004    1.1        is 	m->m_pkthdr.len = rxlen;
   1005    1.1        is 	len = MHLEN;
   1006    1.1        is 	top = 0;
   1007    1.1        is 	mp = &top;
   1008    1.1        is 
   1009    1.1        is 	while (rxlen > 0) {
   1010    1.1        is 		if (top) {
   1011   1.13   mycroft 			MGET(m, M_DONTWAIT, MT_DATA);
   1012    1.1        is 			if (m == 0) {
   1013   1.13   mycroft 				m_freem(top);
   1014   1.13   mycroft 				goto dropped;
   1015    1.1        is 			}
   1016    1.1        is 			len = MLEN;
   1017    1.1        is 		}
   1018    1.1        is 		if (rxlen >= MINCLSIZE) {
   1019    1.1        is 			MCLGET(m, M_DONTWAIT);
   1020   1.12   mycroft 			if ((m->m_flags & M_EXT) == 0) {
   1021   1.13   mycroft 				m_free(m);
   1022   1.11   mycroft 				m_freem(top);
   1023   1.11   mycroft 				goto dropped;
   1024   1.11   mycroft 			}
   1025   1.11   mycroft 			len = MCLBYTES;
   1026    1.1        is 		}
   1027  1.103  riastrad 		len = uimin(rxlen, len);
   1028   1.65   thorpej 		/*
   1029   1.65   thorpej 		 * XXX ALIGNMENT LOSSAGE HERE.
   1030   1.65   thorpej 		 */
   1031    1.1        is 		if (len > 1) {
   1032    1.1        is 			len &= ~1;
   1033    1.6        is 
   1034   1.67     perry 			bus_space_read_multi_stream_2(iot, ioh, MEM_PORT_REG,
   1035  1.106   msaitoh 			    mtod(m, uint16_t *), len / 2);
   1036    1.1        is 		} else {
   1037    1.1        is #ifdef IYDEBUG
   1038   1.97   msaitoh 			printf("%s: received odd mbuf\n",
   1039   1.97   msaitoh 			    device_xname(sc->sc_dev));
   1040    1.1        is #endif
   1041   1.75  christos 			*(mtod(m, char *)) = bus_space_read_stream_2(iot, ioh,
   1042    1.6        is 			    MEM_PORT_REG);
   1043    1.1        is 		}
   1044    1.1        is 		m->m_len = len;
   1045    1.1        is 		rxlen -= len;
   1046    1.1        is 		*mp = m;
   1047    1.1        is 		mp = &m->m_next;
   1048    1.1        is 	}
   1049   1.71  christos 
   1050   1.71  christos 	if (top == NULL)
   1051   1.71  christos 		return;
   1052   1.71  christos 
   1053   1.95     ozaki 	if_percpuq_enqueue(ifp->if_percpuq, top);
   1054    1.1        is 	return;
   1055    1.1        is 
   1056    1.1        is dropped:
   1057  1.113   thorpej 	if_statinc(ifp, if_ierrors);
   1058    1.1        is 	return;
   1059    1.1        is }
   1060   1.26        is 
   1061    1.1        is void
   1062   1.84       dsl iy_intr_rx(struct iy_softc *sc)
   1063    1.1        is {
   1064    1.9   thorpej 	bus_space_tag_t iot;
   1065    1.9   thorpej 	bus_space_handle_t ioh;
   1066    1.6        is 
   1067    1.1        is 	u_int rxadrs, rxevnt, rxstatus, rxnext, rxlen;
   1068    1.1        is 
   1069    1.9   thorpej 	iot = sc->sc_iot;
   1070    1.6        is 	ioh = sc->sc_ioh;
   1071    1.1        is 
   1072    1.1        is 	rxadrs = sc->rx_start;
   1073    1.9   thorpej 	bus_space_write_2(iot, ioh, HOST_ADDR_REG, rxadrs);
   1074   1.41        is 	rxevnt = le16toh(bus_space_read_stream_2(iot, ioh, MEM_PORT_REG));
   1075    1.1        is 	rxnext = 0;
   1076   1.67     perry 
   1077    1.1        is 	while (rxevnt == RCV_DONE) {
   1078   1.41        is 		rxstatus = le16toh(bus_space_read_stream_2(iot, ioh,
   1079   1.41        is 				MEM_PORT_REG));
   1080   1.41        is 		rxnext = le16toh(bus_space_read_stream_2(iot, ioh,
   1081   1.41        is 				MEM_PORT_REG));
   1082   1.41        is 		rxlen = le16toh(bus_space_read_stream_2(iot, ioh,
   1083   1.41        is 				MEM_PORT_REG));
   1084    1.1        is #ifdef IYDEBUG
   1085   1.42        tv 		{
   1086   1.42        tv 			char sbuf[128];
   1087   1.42        tv 
   1088   1.82  christos 			snprintb(sbuf, sizeof(sbuf),
   1089   1.82  christos 			    "\020\1RCLD\2IA_MCH\010SHORT\011OVRN\013ALGERR"
   1090   1.82  christos 			    "\014CRCERR\015LENERR\016RCVOK\020TYP", rxstatus);
   1091   1.82  christos 
   1092   1.42        tv 			printf("%s: pck at 0x%04x stat %s next 0x%x len 0x%x\n",
   1093   1.97   msaitoh 			    device_xname(sc->sc_dev), rxadrs, sbuf, rxnext,
   1094   1.97   msaitoh 			    rxlen);
   1095   1.42        tv 		}
   1096   1.92  christos #else
   1097   1.92  christos 		__USE(rxstatus);
   1098    1.1        is #endif
   1099    1.9   thorpej 		iyget(sc, iot, ioh, rxlen);
   1100    1.1        is 
   1101    1.1        is 		/* move stop address */
   1102    1.9   thorpej 		bus_space_write_2(iot, ioh, RCV_STOP_LOW,
   1103    1.1        is 			    rxnext == 0 ? sc->rx_size - 2 : rxnext - 2);
   1104    1.1        is 
   1105    1.9   thorpej 		bus_space_write_2(iot, ioh, HOST_ADDR_REG, rxnext);
   1106    1.1        is 		rxadrs = rxnext;
   1107   1.41        is 		rxevnt = le16toh(bus_space_read_stream_2(iot, ioh,
   1108   1.41        is 				MEM_PORT_REG));
   1109    1.1        is 	}
   1110    1.1        is 	sc->rx_start = rxnext;
   1111    1.1        is }
   1112    1.1        is 
   1113    1.1        is void
   1114   1.84       dsl iy_intr_tx(struct iy_softc *sc)
   1115    1.1        is {
   1116    1.9   thorpej 	bus_space_tag_t iot;
   1117    1.9   thorpej 	bus_space_handle_t ioh;
   1118    1.1        is 	struct ifnet *ifp;
   1119    1.1        is 	u_int txstatus, txstat2, txlen, txnext;
   1120    1.1        is 
   1121   1.10        is 	ifp = &sc->sc_ethercom.ec_if;
   1122    1.9   thorpej 	iot = sc->sc_iot;
   1123    1.6        is 	ioh = sc->sc_ioh;
   1124    1.6        is 
   1125    1.1        is 	while (sc->tx_start != sc->tx_end) {
   1126    1.9   thorpej 		bus_space_write_2(iot, ioh, HOST_ADDR_REG, sc->tx_start);
   1127   1.41        is 		txstatus = le16toh(bus_space_read_stream_2(iot, ioh,
   1128   1.41        is 			MEM_PORT_REG));
   1129   1.41        is 
   1130  1.106   msaitoh 		if ((txstatus & (TX_DONE | CMD_MASK)) != (TX_DONE | XMT_CMD))
   1131    1.1        is 			break;
   1132    1.1        is 
   1133   1.41        is 		txstat2 = le16toh(bus_space_read_stream_2(iot, ioh,
   1134   1.41        is 				MEM_PORT_REG));
   1135   1.41        is 		txnext = le16toh(bus_space_read_stream_2(iot, ioh,
   1136   1.41        is 				MEM_PORT_REG));
   1137   1.41        is 		txlen = le16toh(bus_space_read_stream_2(iot, ioh,
   1138   1.41        is 				MEM_PORT_REG));
   1139    1.1        is #ifdef IYDEBUG
   1140   1.42        tv 		{
   1141   1.42        tv 			char sbuf[128];
   1142   1.42        tv 
   1143   1.82  christos 			snprintb(sbuf, sizeof(sbuf),
   1144   1.82  christos 			    "\020\6MAX_COL\7HRT_BEAT\010TX_DEF"
   1145   1.82  christos 			    "\011UND_RUN\012JERR\013LST_CRS"
   1146   1.83    cegger 			    "\014LTCOL\016TX_OK\020COLL", txstat2);
   1147   1.82  christos 
   1148  1.100   msaitoh 			printf("txstat 0x%x stat2 %s next 0x%x len 0x%x\n",
   1149   1.42        tv 			       txstatus, sbuf, txnext, txlen);
   1150   1.42        tv 		}
   1151    1.1        is #endif
   1152    1.1        is 		if (txlen & CHAIN)
   1153    1.1        is 			sc->tx_start = txnext;
   1154    1.1        is 		else
   1155    1.1        is 			sc->tx_start = sc->tx_end;
   1156  1.114   thorpej 		sc->tx_busy = false;
   1157   1.67     perry 
   1158   1.47        is 		if (txstat2 & 0x0020)
   1159  1.113   thorpej 			if_statadd(ifp, if_collisions, 16);
   1160   1.47        is 		else
   1161  1.113   thorpej 			if_statadd(ifp, if_collisions, txstat2 & 0x000f);
   1162   1.47        is 
   1163   1.49        is 		if ((txstat2 & 0x2000) == 0)
   1164  1.113   thorpej 			if_statinc(ifp, if_oerrors);
   1165    1.1        is 	}
   1166    1.1        is }
   1167    1.1        is 
   1168    1.1        is int
   1169   1.81    dyoung iyioctl(struct ifnet *ifp, u_long cmd, void *data)
   1170    1.1        is {
   1171    1.1        is 	struct iy_softc *sc;
   1172    1.1        is 	struct ifaddr *ifa;
   1173    1.1        is 	int s, error = 0;
   1174    1.1        is 
   1175    1.2   thorpej 	sc = ifp->if_softc;
   1176    1.1        is 	ifa = (struct ifaddr *)data;
   1177    1.1        is 
   1178    1.1        is #ifdef IYDEBUG
   1179   1.68  christos 	printf("iyioctl called with ifp %p (%s) cmd 0x%lx data %p\n",
   1180    1.2   thorpej 	    ifp, ifp->if_xname, cmd, data);
   1181    1.1        is #endif
   1182    1.1        is 
   1183   1.29   mycroft 	s = splnet();
   1184    1.1        is 
   1185    1.1        is 	switch (cmd) {
   1186    1.1        is 
   1187   1.81    dyoung 	case SIOCINITIFADDR:
   1188    1.1        is 		ifp->if_flags |= IFF_UP;
   1189    1.1        is 
   1190   1.81    dyoung 		iyinit(sc);
   1191    1.1        is 		switch (ifa->ifa_addr->sa_family) {
   1192    1.1        is #ifdef INET
   1193    1.1        is 		case AF_INET:
   1194   1.10        is 			arp_ifinit(ifp, ifa);
   1195    1.1        is 			break;
   1196    1.1        is #endif
   1197    1.1        is 		default:
   1198    1.1        is 			break;
   1199    1.1        is 		}
   1200    1.1        is 		break;
   1201    1.1        is 
   1202    1.1        is 	case SIOCSIFFLAGS:
   1203   1.81    dyoung 		if ((error = ifioctl_common(ifp, cmd, data)) != 0)
   1204   1.81    dyoung 			break;
   1205    1.1        is 		sc->promisc = ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI);
   1206   1.81    dyoung 		/* XXX re-use ether_ioctl() */
   1207  1.106   msaitoh 		switch (ifp->if_flags & (IFF_UP | IFF_RUNNING)) {
   1208   1.81    dyoung 		case IFF_RUNNING:
   1209    1.1        is 			/*
   1210    1.1        is 			 * If interface is marked down and it is running, then
   1211    1.1        is 			 * stop it.
   1212    1.1        is 			 */
   1213    1.1        is 			iystop(sc);
   1214    1.1        is 			ifp->if_flags &= ~IFF_RUNNING;
   1215   1.81    dyoung 			break;
   1216   1.81    dyoung 		case IFF_UP:
   1217    1.1        is 			/*
   1218    1.1        is 			 * If interface is marked up and it is stopped, then
   1219    1.1        is 			 * start it.
   1220    1.1        is 			 */
   1221    1.1        is 			iyinit(sc);
   1222   1.81    dyoung 			break;
   1223   1.81    dyoung 		default:
   1224    1.1        is 			/*
   1225    1.1        is 			 * Reset the interface to pick up changes in any other
   1226    1.1        is 			 * flags that affect hardware registers.
   1227    1.1        is 			 */
   1228    1.1        is 			iystop(sc);
   1229    1.1        is 			iyinit(sc);
   1230   1.81    dyoung 			break;
   1231    1.1        is 		}
   1232    1.1        is #ifdef IYDEBUGX
   1233    1.1        is 		if (ifp->if_flags & IFF_DEBUG)
   1234    1.1        is 			sc->sc_debug = IFY_ALL;
   1235    1.1        is 		else
   1236    1.1        is 			sc->sc_debug = 0;
   1237    1.1        is #endif
   1238    1.1        is 		break;
   1239    1.1        is 
   1240    1.1        is 	case SIOCADDMULTI:
   1241    1.1        is 	case SIOCDELMULTI:
   1242   1.77    dyoung 		if ((error = ether_ioctl(ifp, cmd, data)) == ENETRESET) {
   1243    1.1        is 			/*
   1244    1.1        is 			 * Multicast list has changed; set the hardware filter
   1245    1.1        is 			 * accordingly.
   1246    1.1        is 			 */
   1247   1.65   thorpej 			if (ifp->if_flags & IFF_RUNNING) {
   1248   1.65   thorpej 				/* XXX can't make it work otherwise */
   1249   1.65   thorpej 				iyreset(sc);
   1250   1.65   thorpej 				iy_mc_reset(sc);
   1251   1.65   thorpej 			}
   1252    1.1        is 			error = 0;
   1253    1.1        is 		}
   1254    1.1        is 		break;
   1255   1.28       rvb 
   1256    1.1        is 	default:
   1257   1.81    dyoung 		error = ether_ioctl(ifp, cmd, data);
   1258    1.1        is 	}
   1259    1.1        is 	splx(s);
   1260    1.1        is 	return error;
   1261   1.18    bouyer }
   1262   1.18    bouyer 
   1263   1.18    bouyer int
   1264   1.84       dsl iy_mediachange(struct ifnet *ifp)
   1265   1.18    bouyer {
   1266   1.18    bouyer 	struct iy_softc *sc = ifp->if_softc;
   1267   1.18    bouyer 
   1268   1.18    bouyer 	if (IFM_TYPE(sc->iy_ifmedia.ifm_media) != IFM_ETHER)
   1269   1.18    bouyer 	    return EINVAL;
   1270  1.106   msaitoh 	switch (IFM_SUBTYPE(sc->iy_ifmedia.ifm_media)) {
   1271   1.18    bouyer 	case IFM_10_5:
   1272   1.18    bouyer 	case IFM_10_2:
   1273   1.18    bouyer 	case IFM_10_T:
   1274   1.18    bouyer 	case IFM_AUTO:
   1275   1.18    bouyer 	    iystop(sc);
   1276   1.18    bouyer 	    iyinit(sc);
   1277   1.18    bouyer 	    return 0;
   1278   1.18    bouyer 	default:
   1279   1.18    bouyer 	    return EINVAL;
   1280   1.18    bouyer 	}
   1281   1.18    bouyer }
   1282   1.18    bouyer 
   1283   1.18    bouyer void
   1284   1.84       dsl iy_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
   1285   1.18    bouyer {
   1286   1.18    bouyer 	struct iy_softc *sc = ifp->if_softc;
   1287   1.18    bouyer 
   1288   1.18    bouyer 	ifmr->ifm_active = sc->iy_media;
   1289   1.18    bouyer 	ifmr->ifm_status = IFM_AVALID | IFM_ACTIVE;
   1290    1.1        is }
   1291    1.1        is 
   1292   1.26        is 
   1293   1.26        is static void
   1294   1.84       dsl iy_mc_setup(struct iy_softc *sc)
   1295   1.26        is {
   1296   1.26        is 	struct ether_multi *enm;
   1297   1.26        is 	struct ether_multistep step;
   1298   1.26        is 	struct ethercom *ecp;
   1299   1.26        is 	struct ifnet *ifp;
   1300   1.26        is 	bus_space_tag_t iot;
   1301   1.26        is 	bus_space_handle_t ioh;
   1302   1.26        is 	int avail, last /*, end*/ , len;
   1303   1.26        is 	int timeout;
   1304  1.106   msaitoh 	volatile uint16_t dum;
   1305  1.106   msaitoh 	uint8_t temp;
   1306   1.67     perry 
   1307   1.26        is 
   1308   1.26        is 	ecp = &sc->sc_ethercom;
   1309   1.26        is 	ifp = &ecp->ec_if;
   1310   1.26        is 
   1311   1.26        is 	iot = sc->sc_iot;
   1312   1.26        is 	ioh = sc->sc_ioh;
   1313   1.26        is 
   1314  1.112   msaitoh 	ETHER_LOCK(ecp);
   1315   1.50        is 	len = 6 * ecp->ec_multicnt;
   1316   1.67     perry 
   1317   1.26        is 	avail = sc->tx_start - sc->tx_end;
   1318   1.26        is 	if (avail <= 0)
   1319   1.26        is 		avail += sc->tx_size;
   1320   1.37        is 	if (ifp->if_flags & IFF_DEBUG)
   1321   1.37        is 		printf("%s: iy_mc_setup called, %d addresses, "
   1322   1.67     perry 		    "%d/%d bytes needed/avail\n", ifp->if_xname,
   1323   1.37        is 		    ecp->ec_multicnt, len + I595_XMT_HDRLEN, avail);
   1324   1.26        is 
   1325   1.26        is 	last = sc->rx_size;
   1326   1.26        is 
   1327   1.26        is 	bus_space_write_1(iot, ioh, 0, BANK_SEL(2));
   1328   1.51        is 	bus_space_write_1(iot, ioh, RECV_MODES_REG, MATCH_BRDCST);
   1329   1.26        is 	/* XXX VOODOO */
   1330   1.26        is 	temp = bus_space_read_1(iot, ioh, MEDIA_SELECT);
   1331   1.26        is 	bus_space_write_1(iot, ioh, MEDIA_SELECT, temp);
   1332   1.26        is 	/* XXX END OF VOODOO */
   1333   1.26        is 	bus_space_write_1(iot, ioh, 0, BANK_SEL(0));
   1334   1.26        is 	bus_space_write_2(iot, ioh, HOST_ADDR_REG, last);
   1335   1.41        is 	bus_space_write_stream_2(iot, ioh, MEM_PORT_REG, htole16(MC_SETUP_CMD));
   1336   1.26        is 	bus_space_write_2(iot, ioh, MEM_PORT_REG, 0);
   1337   1.26        is 	bus_space_write_2(iot, ioh, MEM_PORT_REG, 0);
   1338   1.41        is 	bus_space_write_stream_2(iot, ioh, MEM_PORT_REG, htole16(len));
   1339   1.67     perry 
   1340   1.26        is 	ETHER_FIRST_MULTI(step, ecp, enm);
   1341  1.106   msaitoh 	while (enm) {
   1342   1.65   thorpej 		/*
   1343   1.65   thorpej 		 * XXX ALIGNMENT LOSSAGE HERE?
   1344   1.65   thorpej 		 */
   1345   1.41        is 		bus_space_write_multi_stream_2(iot, ioh, MEM_PORT_REG,
   1346  1.106   msaitoh 		    (uint16_t *) enm->enm_addrlo, 3);
   1347   1.26        is 
   1348   1.26        is 		ETHER_NEXT_MULTI(step, enm);
   1349   1.26        is 	}
   1350  1.108   msaitoh 	ETHER_UNLOCK(ecp);
   1351   1.40        is 	dum = bus_space_read_2(iot, ioh, MEM_PORT_REG); /* dummy read */
   1352   1.92  christos 	__USE(dum);
   1353   1.26        is 	bus_space_write_2(iot, ioh, XMT_ADDR_REG, last);
   1354   1.26        is 	bus_space_write_1(iot, ioh, 0, MC_SETUP_CMD);
   1355   1.67     perry 
   1356   1.26        is 
   1357  1.107   msaitoh 	sc->tx_start =	sc->rx_size;
   1358   1.26        is 	sc->tx_end = sc->rx_size + I595_XMT_HDRLEN + len;
   1359   1.26        is 
   1360   1.26        is 	for (timeout=0; timeout<100; timeout++) {
   1361   1.26        is 		DELAY(2);
   1362   1.26        is 		if ((bus_space_read_1(iot, ioh, STATUS_REG) & EXEC_INT) == 0)
   1363   1.26        is 			continue;
   1364   1.26        is 
   1365   1.26        is 		temp = bus_space_read_1(iot, ioh, 0);
   1366   1.26        is 		bus_space_write_1(iot, ioh, STATUS_REG, EXEC_INT);
   1367   1.26        is #ifdef DIAGNOSTIC
   1368   1.26        is 		if (temp & 0x20) {
   1369   1.97   msaitoh 			aprint_error_dev(sc->sc_dev,
   1370   1.97   msaitoh 			    "mc setup failed, %d usec\n", timeout * 2);
   1371   1.37        is 		} else if (((temp & 0x0f) == 0x03) &&
   1372   1.37        is 			    (ifp->if_flags & IFF_DEBUG)) {
   1373   1.26        is 				printf("%s: mc setup done, %d usec\n",
   1374   1.91       chs 			    device_xname(sc->sc_dev), timeout * 2);
   1375   1.26        is 		}
   1376   1.26        is #endif
   1377   1.26        is 		break;
   1378   1.26        is 	}
   1379   1.26        is 	sc->tx_start = sc->tx_end;
   1380  1.114   thorpej 	sc->tx_busy = false;
   1381   1.26        is }
   1382   1.26        is 
   1383    1.1        is static void
   1384   1.84       dsl iy_mc_reset(struct iy_softc *sc)
   1385    1.1        is {
   1386    1.1        is 	struct ether_multi *enm;
   1387    1.1        is 	struct ether_multistep step;
   1388   1.25        is 	struct ethercom *ecp;
   1389   1.25        is 	struct ifnet *ifp;
   1390   1.26        is 	bus_space_tag_t iot;
   1391   1.26        is 	bus_space_handle_t ioh;
   1392  1.106   msaitoh 	uint16_t temp;
   1393   1.25        is 
   1394   1.25        is 	ecp = &sc->sc_ethercom;
   1395   1.25        is 	ifp = &ecp->ec_if;
   1396    1.1        is 
   1397   1.26        is 	iot = sc->sc_iot;
   1398   1.26        is 	ioh = sc->sc_ioh;
   1399   1.26        is 
   1400   1.25        is 	if (ecp->ec_multicnt > 63) {
   1401   1.26        is 		ifp->if_flags |= IFF_ALLMULTI;
   1402   1.25        is 
   1403   1.26        is 	} else if (ecp->ec_multicnt > 0) {
   1404   1.25        is 		/*
   1405   1.25        is 		 * Step through the list of addresses.
   1406   1.25        is 		 */
   1407  1.108   msaitoh 		ETHER_LOCK(ecp);
   1408   1.25        is 		ETHER_FIRST_MULTI(step, ecp, enm);
   1409  1.106   msaitoh 		while (enm) {
   1410   1.54   thorpej 			if (memcmp(enm->enm_addrlo, enm->enm_addrhi, 6) != 0) {
   1411   1.26        is 				ifp->if_flags |= IFF_ALLMULTI;
   1412  1.108   msaitoh 				ETHER_UNLOCK(ecp);
   1413   1.26        is 				goto setupmulti;
   1414   1.25        is 			}
   1415   1.25        is 			ETHER_NEXT_MULTI(step, enm);
   1416   1.67     perry 		}
   1417  1.108   msaitoh 		ETHER_UNLOCK(ecp);
   1418   1.25        is 		/* OK, we really need to do it now: */
   1419   1.26        is #if 0
   1420  1.114   thorpej 		if ((ifp->if_flags & IFF_RUNNING) == 0 || sc->tx_busy) {
   1421  1.114   thorpej 			/* XXX This logic is b0rk3n. */
   1422  1.114   thorpej 			sc->tx_busy = true;
   1423   1.26        is 			sc->want_mc_setup = 1;
   1424  1.107   msaitoh 			return;
   1425   1.25        is 		}
   1426   1.26        is #endif
   1427   1.26        is 		iy_mc_setup(sc);
   1428   1.25        is 	} else {
   1429   1.26        is 		ifp->if_flags &= ~IFF_ALLMULTI;
   1430    1.1        is 	}
   1431   1.25        is 
   1432   1.26        is setupmulti:
   1433   1.26        is 	bus_space_write_1(iot, ioh, 0, BANK_SEL(2));
   1434  1.106   msaitoh 	if (ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI))
   1435   1.26        is 		temp = MATCH_ALL;
   1436  1.106   msaitoh 	else
   1437   1.51        is 		temp = MATCH_BRDCST;
   1438   1.26        is 
   1439   1.26        is 	bus_space_write_1(iot, ioh, RECV_MODES_REG, temp);
   1440   1.26        is 	/* XXX VOODOO */
   1441   1.26        is 	temp = bus_space_read_1(iot, ioh, MEDIA_SELECT);
   1442   1.26        is 	bus_space_write_1(iot, ioh, MEDIA_SELECT, temp);
   1443   1.26        is 	/* XXX END OF VOODOO */
   1444   1.26        is 
   1445   1.26        is 	/* XXX TBD: setup hardware for all multicasts */
   1446   1.26        is 	bus_space_write_1(iot, ioh, 0, BANK_SEL(0));
   1447   1.25        is 	return;
   1448    1.1        is }
   1449    1.1        is 
   1450   1.42        tv #ifdef IYDEBUGX
   1451    1.1        is void
   1452   1.84       dsl print_rbd(volatile struct ie_recv_buf_desc *rbd)
   1453    1.1        is {
   1454    1.8  christos 	printf("RBD at %08lx:\nactual %04x, next %04x, buffer %08x\n"
   1455    1.1        is 	    "length %04x, mbz %04x\n", (u_long)rbd, rbd->ie_rbd_actual,
   1456    1.1        is 	    rbd->ie_rbd_next, rbd->ie_rbd_buffer, rbd->ie_rbd_length,
   1457    1.1        is 	    rbd->mbz);
   1458    1.1        is }
   1459    1.1        is #endif
   1460    1.1        is 
   1461    1.1        is void
   1462   1.84       dsl iyprobemem(struct iy_softc *sc)
   1463    1.1        is {
   1464    1.9   thorpej 	bus_space_tag_t iot;
   1465    1.9   thorpej 	bus_space_handle_t ioh;
   1466    1.1        is 	int testing;
   1467    1.1        is 
   1468    1.9   thorpej 	iot = sc->sc_iot;
   1469    1.6        is 	ioh = sc->sc_ioh;
   1470    1.1        is 
   1471   1.10        is 	bus_space_write_1(iot, ioh, COMMAND_REG, BANK_SEL(0));
   1472   1.10        is 	delay(1);
   1473    1.9   thorpej 	bus_space_write_2(iot, ioh, HOST_ADDR_REG, 4096-2);
   1474    1.9   thorpej 	bus_space_write_2(iot, ioh, MEM_PORT_REG, 0);
   1475    1.1        is 
   1476    1.1        is 	for (testing=65536; testing >= 4096; testing >>= 1) {
   1477    1.9   thorpej 		bus_space_write_2(iot, ioh, HOST_ADDR_REG, testing-2);
   1478    1.9   thorpej 		bus_space_write_2(iot, ioh, MEM_PORT_REG, 0xdead);
   1479    1.9   thorpej 		bus_space_write_2(iot, ioh, HOST_ADDR_REG, testing-2);
   1480    1.9   thorpej 		if (bus_space_read_2(iot, ioh, MEM_PORT_REG) != 0xdead) {
   1481    1.1        is #ifdef IYMEMDEBUG
   1482    1.8  christos 			printf("%s: Didn't keep 0xdead at 0x%x\n",
   1483   1.91       chs 			    device_xname(sc->sc_dev), testing-2);
   1484    1.1        is #endif
   1485    1.1        is 			continue;
   1486    1.1        is 		}
   1487    1.1        is 
   1488    1.9   thorpej 		bus_space_write_2(iot, ioh, HOST_ADDR_REG, testing-2);
   1489    1.9   thorpej 		bus_space_write_2(iot, ioh, MEM_PORT_REG, 0xbeef);
   1490    1.9   thorpej 		bus_space_write_2(iot, ioh, HOST_ADDR_REG, testing-2);
   1491    1.9   thorpej 		if (bus_space_read_2(iot, ioh, MEM_PORT_REG) != 0xbeef) {
   1492    1.1        is #ifdef IYMEMDEBUG
   1493    1.8  christos 			printf("%s: Didn't keep 0xbeef at 0x%x\n",
   1494   1.91       chs 			    device_xname(sc->sc_dev), testing-2);
   1495    1.1        is #endif
   1496    1.1        is 			continue;
   1497    1.1        is 		}
   1498    1.1        is 
   1499    1.9   thorpej 		bus_space_write_2(iot, ioh, HOST_ADDR_REG, 0);
   1500    1.9   thorpej 		bus_space_write_2(iot, ioh, MEM_PORT_REG, 0);
   1501    1.9   thorpej 		bus_space_write_2(iot, ioh, HOST_ADDR_REG, testing >> 1);
   1502    1.9   thorpej 		bus_space_write_2(iot, ioh, MEM_PORT_REG, testing >> 1);
   1503    1.9   thorpej 		bus_space_write_2(iot, ioh, HOST_ADDR_REG, 0);
   1504    1.9   thorpej 		if (bus_space_read_2(iot, ioh, MEM_PORT_REG) == (testing >> 1)) {
   1505    1.1        is #ifdef IYMEMDEBUG
   1506    1.8  christos 			printf("%s: 0x%x alias of 0x0\n",
   1507   1.91       chs 			    device_xname(sc->sc_dev), testing >> 1);
   1508    1.1        is #endif
   1509    1.1        is 			continue;
   1510    1.1        is 		}
   1511    1.1        is 
   1512    1.1        is 		break;
   1513    1.1        is 	}
   1514    1.1        is 
   1515    1.1        is 	sc->sram = testing;
   1516    1.1        is 
   1517  1.106   msaitoh 	switch (testing) {
   1518    1.1        is 		case 65536:
   1519    1.1        is 			/* 4 NFS packets + overhead RX, 2 NFS + overhead TX  */
   1520    1.1        is 			sc->rx_size = 44*1024;
   1521    1.1        is 			break;
   1522    1.1        is 
   1523    1.1        is 		case 32768:
   1524    1.1        is 			/* 2 NFS packets + overhead RX, 1 NFS + overhead TX  */
   1525    1.1        is 			sc->rx_size = 22*1024;
   1526    1.1        is 			break;
   1527    1.1        is 
   1528    1.1        is 		case 16384:
   1529    1.1        is 			/* 1 NFS packet + overhead RX, 4 big packets TX */
   1530    1.1        is 			sc->rx_size = 10*1024;
   1531    1.1        is 			break;
   1532   1.67     perry 		default:
   1533    1.1        is 			sc->rx_size = testing/2;
   1534    1.1        is 			break;
   1535    1.1        is 	}
   1536    1.1        is 	sc->tx_size = testing - sc->rx_size;
   1537   1.10        is }
   1538   1.10        is 
   1539   1.10        is static int
   1540  1.106   msaitoh eepromreadall(bus_space_tag_t iot, bus_space_handle_t ioh, uint16_t *wordp,
   1541   1.97   msaitoh     int maxi)
   1542   1.10        is {
   1543   1.10        is 	int i;
   1544  1.106   msaitoh 	uint16_t checksum, tmp;
   1545   1.10        is 
   1546   1.10        is 	checksum = 0;
   1547   1.10        is 
   1548   1.10        is 	for (i=0; i<EEPP_LENGTH; ++i) {
   1549   1.10        is 		tmp = eepromread(iot, ioh, i);
   1550   1.10        is 		checksum += tmp;
   1551   1.10        is 		if (i<maxi)
   1552   1.10        is 			wordp[i] = tmp;
   1553   1.10        is 	}
   1554   1.10        is 
   1555   1.10        is 	if (checksum != EEPP_CHKSUM) {
   1556   1.10        is #ifdef IYDEBUG
   1557   1.10        is 		printf("wrong EEPROM checksum 0x%x should be 0x%x\n",
   1558   1.10        is 		    checksum, EEPP_CHKSUM);
   1559   1.10        is #endif
   1560   1.10        is 		return 1;
   1561   1.10        is 	}
   1562   1.10        is 	return 0;
   1563    1.1        is }
   1564