Home | History | Annotate | Line # | Download | only in ic
iha.c revision 1.2
      1 /*	$NetBSD: iha.c,v 1.2 2001/06/05 12:42:44 tsutsui Exp $ */
      2 /*
      3  * Initio INI-9xxxU/UW SCSI Device Driver
      4  *
      5  * Copyright (c) 2000 Ken Westerback
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer,
     13  *    without modification, immediately at the beginning of the file.
     14  * 2. The name of the author may not be used to endorse or promote products
     15  *    derived from this software without specific prior written permission.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20  * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
     21  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     23  * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     25  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
     26  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     27  * THE POSSIBILITY OF SUCH DAMAGE.
     28  *
     29  *-------------------------------------------------------------------------
     30  *
     31  * Ported from i91u.c, provided by Initio Corporation, which credits:
     32  *
     33  * Device driver for the INI-9XXXU/UW or INIC-940/950  PCI SCSI Controller.
     34  *
     35  * FreeBSD
     36  *
     37  *  Written for 386bsd and FreeBSD by
     38  *	Winston Hung		<winstonh (at) initio.com>
     39  *
     40  * Copyright (c) 1997-99 Initio Corp.  All rights reserved.
     41  *
     42  *-------------------------------------------------------------------------
     43  */
     44 
     45 /*
     46  * Ported to NetBSD by Izumi Tsutsui <tsutsui (at) ceres.dti.ne.jp> from OpenBSD:
     47  * $OpenBSD: iha.c,v 1.3 2001/02/20 00:47:33 krw Exp $
     48  */
     49 
     50 #include <sys/param.h>
     51 #include <sys/systm.h>
     52 #include <sys/kernel.h>
     53 #include <sys/buf.h>
     54 #include <sys/device.h>
     55 #include <sys/malloc.h>
     56 
     57 #include <uvm/uvm_extern.h>
     58 
     59 #include <machine/bus.h>
     60 #include <machine/intr.h>
     61 
     62 #include <dev/scsipi/scsi_all.h>
     63 #include <dev/scsipi/scsipi_all.h>
     64 #include <dev/scsipi/scsiconf.h>
     65 #include <dev/scsipi/scsi_message.h>
     66 
     67 #include <dev/ic/ihareg.h>
     68 #include <dev/ic/ihavar.h>
     69 
     70 /*
     71  * SCSI Rate Table, indexed by FLAG_SCSI_RATE field of
     72  * tcs flags.
     73  */
     74 static u_int8_t tul_rate_tbl[8] = {
     75 	/* fast 20		  */
     76 	/* nanosecond divide by 4 */
     77 	12,	/* 50ns,  20M	  */
     78 	18,	/* 75ns,  13.3M	  */
     79 	25,	/* 100ns, 10M	  */
     80 	31,	/* 125ns, 8M	  */
     81 	37,	/* 150ns, 6.6M	  */
     82 	43,	/* 175ns, 5.7M	  */
     83 	50,	/* 200ns, 5M	  */
     84 	62	/* 250ns, 4M	  */
     85 };
     86 
     87 static u_int16_t eeprom_default[EEPROM_SIZE] = {
     88 	/* -- Header ------------------------------------ */
     89 	/* signature */
     90 	EEP_SIGNATURE,
     91 	/* size, revision */
     92 	EEP_WORD(EEPROM_SIZE * 2, 0x01),
     93 	/* -- Host Adapter Structure -------------------- */
     94 	/* model */
     95 	0x0095,
     96 	/* model info, number of channel */
     97 	EEP_WORD(0x00, 1),
     98 	/* BIOS config */
     99 	EEP_BIOSCFG_DEFAULT,
    100 	/* host adapter config */
    101 	0,
    102 
    103 	/* -- eeprom_adapter[0] ------------------------------- */
    104 	/* ID, adapter config 1 */
    105 	EEP_WORD(7, CFG_DEFAULT),
    106 	/* adapter config 2, number of targets */
    107 	EEP_WORD(0x00, 8),
    108 	/* target flags */
    109 	EEP_WORD(FLAG_DEFAULT, FLAG_DEFAULT),
    110 	EEP_WORD(FLAG_DEFAULT, FLAG_DEFAULT),
    111 	EEP_WORD(FLAG_DEFAULT, FLAG_DEFAULT),
    112 	EEP_WORD(FLAG_DEFAULT, FLAG_DEFAULT),
    113 	EEP_WORD(FLAG_DEFAULT, FLAG_DEFAULT),
    114 	EEP_WORD(FLAG_DEFAULT, FLAG_DEFAULT),
    115 	EEP_WORD(FLAG_DEFAULT, FLAG_DEFAULT),
    116 	EEP_WORD(FLAG_DEFAULT, FLAG_DEFAULT),
    117 
    118 	/* -- eeprom_adapter[1] ------------------------------- */
    119 	/* ID, adapter config 1 */
    120 	EEP_WORD(7, CFG_DEFAULT),
    121 	/* adapter config 2, number of targets */
    122 	EEP_WORD(0x00, 8),
    123 	/* target flags */
    124 	EEP_WORD(FLAG_DEFAULT, FLAG_DEFAULT),
    125 	EEP_WORD(FLAG_DEFAULT, FLAG_DEFAULT),
    126 	EEP_WORD(FLAG_DEFAULT, FLAG_DEFAULT),
    127 	EEP_WORD(FLAG_DEFAULT, FLAG_DEFAULT),
    128 	EEP_WORD(FLAG_DEFAULT, FLAG_DEFAULT),
    129 	EEP_WORD(FLAG_DEFAULT, FLAG_DEFAULT),
    130 	EEP_WORD(FLAG_DEFAULT, FLAG_DEFAULT),
    131 	EEP_WORD(FLAG_DEFAULT, FLAG_DEFAULT),
    132 	/* reserved[5] */
    133 	0, 0, 0, 0, 0,
    134 	/* checksum */
    135 	0
    136 };
    137 
    138 static u_int8_t tul_data_over_run(struct iha_scsi_req_q *);
    139 
    140 static int tul_push_sense_request(struct iha_softc *, struct iha_scsi_req_q *);
    141 static void tul_timeout(void *);
    142 static int tul_alloc_sglist(struct iha_softc *);
    143 
    144 static void tul_read_eeprom(struct iha_softc *, struct iha_eeprom *);
    145 static void tul_se2_update_all(struct iha_softc *);
    146 static int tul_se2_rd_all(struct iha_softc *, u_int16_t *);
    147 static void tul_se2_wr(struct iha_softc *, int, u_int16_t);
    148 static void tul_se2_instr(struct iha_softc *, int);
    149 static u_int16_t tul_se2_rd(struct iha_softc *, int);
    150 
    151 static void tul_reset_scsi_bus(struct iha_softc *);
    152 static void tul_reset_chip(struct iha_softc *);
    153 static void tul_reset_dma(struct iha_softc *);
    154 
    155 static void tul_reset_tcs(struct tcs *, u_int8_t);
    156 
    157 static void tul_done_scb(struct iha_softc *, struct iha_scsi_req_q *);
    158 static void tul_exec_scb(struct iha_softc *, struct iha_scsi_req_q *);
    159 
    160 static void tul_main(struct iha_softc *);
    161 static void tul_scsi(struct iha_softc *);
    162 
    163 static int  tul_wait(struct iha_softc *, u_int8_t);
    164 
    165 static __inline void tul_mark_busy_scb(struct iha_scsi_req_q *);
    166 
    167 static void tul_append_free_scb(struct iha_softc *, struct iha_scsi_req_q *);
    168 static void tul_append_done_scb(struct iha_softc *, struct iha_scsi_req_q *,
    169     u_int8_t);
    170 static __inline struct iha_scsi_req_q *tul_pop_done_scb(struct iha_softc *);
    171 
    172 static __inline void tul_append_pend_scb(struct iha_softc *,
    173     struct iha_scsi_req_q *);
    174 static __inline void tul_push_pend_scb(struct iha_softc *,
    175     struct iha_scsi_req_q *);
    176 static __inline void tul_del_pend_scb(struct iha_softc *,
    177     struct iha_scsi_req_q *);
    178 static struct iha_scsi_req_q *tul_find_pend_scb(struct iha_softc *);
    179 
    180 static void tul_sync_done(struct iha_softc *);
    181 static void tul_wdtr_done(struct iha_softc *);
    182 static void tul_bad_seq(struct iha_softc *);
    183 
    184 static int tul_next_state(struct iha_softc *);
    185 static int tul_state_1(struct iha_softc *);
    186 static int tul_state_2(struct iha_softc *);
    187 static int tul_state_3(struct iha_softc *);
    188 static int tul_state_4(struct iha_softc *);
    189 static int tul_state_5(struct iha_softc *);
    190 static int tul_state_6(struct iha_softc *);
    191 static int tul_state_8(struct iha_softc *);
    192 
    193 static void tul_set_ssig(struct iha_softc *, u_int8_t, u_int8_t);
    194 
    195 static int tul_xpad_in(struct iha_softc *);
    196 static int tul_xpad_out(struct iha_softc *);
    197 
    198 static int tul_xfer_data(struct iha_softc *, struct iha_scsi_req_q *,
    199     int direction);
    200 
    201 static int tul_status_msg(struct iha_softc *);
    202 
    203 static int tul_msgin(struct iha_softc *);
    204 static int tul_msgin_sync(struct iha_softc *);
    205 static int tul_msgin_extend(struct iha_softc *);
    206 static int tul_msgin_ignore_wid_resid(struct iha_softc *);
    207 
    208 static int  tul_msgout(struct iha_softc *, u_int8_t);
    209 static void tul_msgout_abort(struct iha_softc *, u_int8_t);
    210 static int  tul_msgout_reject(struct iha_softc *);
    211 static int  tul_msgout_sync(struct iha_softc *);
    212 static int  tul_msgout_wide(struct iha_softc *);
    213 
    214 static void tul_select(struct iha_softc *, struct iha_scsi_req_q *, u_int8_t);
    215 
    216 static void tul_busfree(struct iha_softc *);
    217 static int  tul_resel(struct iha_softc *);
    218 
    219 static void tul_abort_xs(struct iha_softc *, struct scsipi_xfer *, u_int8_t);
    220 
    221 static void iha_minphys(struct buf *);
    222 void iha_scsipi_request(struct scsipi_channel *, scsipi_adapter_req_t,
    223     void *arg);
    224 
    225 /*
    226  * iha_intr - the interrupt service routine for the iha driver
    227  */
    228 int
    229 iha_intr(arg)
    230 	void *arg;
    231 {
    232 	bus_space_tag_t iot;
    233 	bus_space_handle_t ioh;
    234 	struct iha_softc *sc;
    235 	int s;
    236 
    237 	sc  = (struct iha_softc *)arg;
    238 	iot = sc->sc_iot;
    239 	ioh = sc->sc_ioh;
    240 
    241 	if ((bus_space_read_1(iot, ioh, TUL_STAT0) & INTPD) == 0)
    242 		return (0);
    243 
    244 	s = splbio(); /* XXX - Or are interrupts off when ISR's are called? */
    245 
    246 	if (sc->sc_semaph != SEMAPH_IN_MAIN) {
    247 		/* XXX - need these inside a splbio()/splx()? */
    248 		bus_space_write_1(iot, ioh, TUL_IMSK, MASK_ALL);
    249 		sc->sc_semaph = SEMAPH_IN_MAIN;
    250 
    251 		tul_main(sc);
    252 
    253 		sc->sc_semaph = ~SEMAPH_IN_MAIN;
    254 		bus_space_write_1(iot, ioh, TUL_IMSK, (MASK_ALL & ~MSCMP));
    255 	}
    256 
    257 	splx(s);
    258 
    259 	return (1);
    260 }
    261 
    262 void
    263 iha_scsipi_request(chan, req, arg)
    264 	struct scsipi_channel *chan;
    265 	scsipi_adapter_req_t req;
    266 	void *arg;
    267 {
    268 	struct scsipi_xfer *xs;
    269 	struct scsipi_periph *periph;
    270 	struct iha_scsi_req_q *scb;
    271 	struct iha_softc *sc;
    272 	int error, flags, s;
    273 
    274 	sc = (struct iha_softc *)chan->chan_adapter->adapt_dev;
    275 
    276 	switch (req) {
    277 	case ADAPTER_REQ_RUN_XFER:
    278 		xs = arg;
    279 		periph = xs->xs_periph;
    280 		flags = xs->xs_control;
    281 
    282 		if (xs->cmdlen > sizeof(struct scsi_generic) ||
    283 		    periph->periph_target >= IHA_MAX_TARGETS) {
    284 			xs->error = XS_DRIVER_STUFFUP;
    285 			return;
    286 		}
    287 
    288 		s = splbio();
    289 		scb = TAILQ_FIRST(&sc->sc_freescb);
    290 		if (scb != NULL) {
    291 			scb->status = STATUS_RENT;
    292 			TAILQ_REMOVE(&sc->sc_freescb, scb, chain);
    293 		}
    294 #ifdef DIAGNOSTIC
    295 		else {
    296 			scsipi_printaddr(periph);
    297 			printf("unable to allocate scb\n");
    298 			panic("iha_scsipi_request");
    299 		}
    300 #endif
    301 		splx(s);
    302 
    303 		scb->target = periph->periph_target;
    304 		scb->lun = periph->periph_lun;
    305 		scb->tcs = &sc->sc_tcs[scb->target];
    306 		scb->flags = xs->xs_control; /* XXX */
    307 		scb->scb_id = MSG_IDENTIFY(periph->periph_lun,
    308 		    (xs->xs_control & XS_CTL_REQSENSE) == 0);
    309 
    310 		scb->xs = xs;
    311 		scb->timeout = xs->timeout;
    312 		scb->cmdlen = xs->cmdlen;
    313 		memcpy(&scb->cmd, xs->cmd, xs->cmdlen);
    314 
    315 		scb->buflen = xs->datalen;
    316 
    317 		if (scb->buflen > 0) {
    318 			error = bus_dmamap_load(sc->sc_dmat, scb->dmap,
    319 			    xs->data, scb->buflen, NULL,
    320 			    (xs->xs_control & XS_CTL_NOSLEEP) ?
    321 			    BUS_DMA_NOWAIT : BUS_DMA_WAITOK);
    322 
    323 			if (error) {
    324 				printf("%s: error %d loading dma map\n",
    325 				    sc->sc_dev.dv_xname, error);
    326 				tul_append_free_scb(sc, scb);
    327 				xs->error = XS_DRIVER_STUFFUP;
    328 				scsipi_done(xs);
    329 				return;
    330 			}
    331 			bus_dmamap_sync(sc->sc_dmat, scb->dmap,
    332 			    0, scb->dmap->dm_mapsize,
    333 			    (xs->xs_control & XS_CTL_DATA_IN) ?
    334 			    BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
    335 		}
    336 
    337 		tul_exec_scb(sc, scb);
    338 		return;
    339 
    340 	case ADAPTER_REQ_GROW_RESOURCES:
    341 		return; /* XXX */
    342 
    343 	case ADAPTER_REQ_SET_XFER_MODE:
    344 		return; /* XXX */
    345 	}
    346 }
    347 
    348 void
    349 iha_attach(sc)
    350 	struct iha_softc *sc;
    351 {
    352 	bus_space_tag_t iot = sc->sc_iot;
    353 	bus_space_handle_t ioh = sc->sc_ioh;
    354 	struct iha_scsi_req_q *scb;
    355 	struct iha_eeprom eeprom;
    356 	struct eeprom_adapter *conf;
    357 	int i, error, reg;
    358 
    359 	tul_read_eeprom(sc, &eeprom);
    360 
    361 	conf = &eeprom.adapter[0];
    362 
    363 	/*
    364 	 * fill in the rest of the IHA_SOFTC fields
    365 	 */
    366 	sc->sc_id = CFG_ID(conf->config1);
    367 	sc->sc_semaph = ~SEMAPH_IN_MAIN;
    368 	sc->sc_status0 = 0;
    369 	sc->sc_actscb = NULL;
    370 
    371 	TAILQ_INIT(&sc->sc_freescb);
    372 	TAILQ_INIT(&sc->sc_pendscb);
    373 	TAILQ_INIT(&sc->sc_donescb);
    374 	error = tul_alloc_sglist(sc);
    375 	if (error != 0) {
    376 		printf(": cannot allocate sglist\n");
    377 		return;
    378 	}
    379 
    380 	sc->sc_scb = malloc(sizeof(struct iha_scsi_req_q) * IHA_MAX_SCB,
    381 	    M_DEVBUF, M_NOWAIT);
    382 	if (sc->sc_scb == NULL) {
    383 		printf(": cannot allocate SCB\n");
    384 		return;
    385 	}
    386 	bzero(sc->sc_scb, sizeof(struct iha_scsi_req_q) * IHA_MAX_SCB);
    387 
    388 	for (i = 0, scb = sc->sc_scb; i < IHA_MAX_SCB; i++, scb++) {
    389 		scb->scb_tagid = i;
    390 		scb->sgoffset = IHA_SG_SIZE * i;
    391 		scb->sglist = &sc->sc_sglist[i].sg_element[0];
    392 		scb->sg_addr =
    393 		    sc->sc_dmamap->dm_segs[0].ds_addr + scb->sgoffset;
    394 
    395 		error = bus_dmamap_create(sc->sc_dmat,
    396 		    (IHA_MAX_SG_ENTRIES - 1) * PAGE_SIZE, IHA_MAX_SG_ENTRIES,
    397 		    (IHA_MAX_SG_ENTRIES - 1) * PAGE_SIZE, 0,
    398 		    BUS_DMA_NOWAIT, &scb->dmap);
    399 
    400 		if (error != 0) {
    401 			printf(": couldn't create SCB DMA map, error = %d\n",
    402 			    error);
    403 			return;
    404 		}
    405 		TAILQ_INSERT_TAIL(&sc->sc_freescb, scb, chain);
    406 	}
    407 
    408 	/* Mask all the interrupts */
    409 	bus_space_write_1(iot, ioh, TUL_IMSK, MASK_ALL);
    410 
    411 	/* Stop any I/O and reset the scsi module */
    412 	tul_reset_dma(sc);
    413 	bus_space_write_1(iot, ioh, TUL_SCTRL0, RSMOD);
    414 
    415 	/* Program HBA's SCSI ID */
    416 	bus_space_write_1(iot, ioh, TUL_SID, sc->sc_id << 4);
    417 
    418 	/*
    419 	 * Configure the channel as requested by the NVRAM settings read
    420 	 * by tul_read_eeprom() above.
    421 	 */
    422 
    423 	sc->sc_sconf1 = SCONFIG0DEFAULT;
    424 	if ((conf->config1 & CFG_EN_PAR) != 0)
    425 		sc->sc_sconf1 |= SPCHK;
    426 	bus_space_write_1(iot, ioh, TUL_SCONFIG0, sc->sc_sconf1);
    427 
    428 	/* set selection time out 250 ms */
    429 	bus_space_write_1(iot, ioh, TUL_STIMO, STIMO_250MS);
    430 
    431 	/* Enable desired SCSI termination configuration read from eeprom */
    432 	reg = 0;
    433 	if (conf->config1 & CFG_ACT_TERM1)
    434 		reg |= ENTMW;
    435 	if (conf->config1 & CFG_ACT_TERM2)
    436 		reg |= ENTM;
    437 	bus_space_write_1(iot, ioh, TUL_DCTRL0, reg);
    438 
    439 	reg = bus_space_read_1(iot, ioh, TUL_GCTRL1) & ~ATDEN;
    440 	if (conf->config1 & CFG_AUTO_TERM)
    441 		reg |= ATDEN;
    442 	bus_space_write_1(iot, ioh, TUL_GCTRL1, reg);
    443 
    444 	for (i = 0; i < IHA_MAX_TARGETS / 2; i++) {
    445 		sc->sc_tcs[i * 2    ].flags = EEP_LBYTE(conf->tflags[i]);
    446 		sc->sc_tcs[i * 2 + 1].flags = EEP_HBYTE(conf->tflags[i]);
    447 		tul_reset_tcs(&sc->sc_tcs[i * 2    ], sc->sc_sconf1);
    448 		tul_reset_tcs(&sc->sc_tcs[i * 2 + 1], sc->sc_sconf1);
    449 	}
    450 
    451 	tul_reset_chip(sc);
    452 	bus_space_write_1(iot, ioh, TUL_SIEN, ALL_INTERRUPTS);
    453 
    454 	/*
    455 	 * fill in the adapter.
    456 	 */
    457 	sc->sc_adapter.adapt_dev = &sc->sc_dev;
    458 	sc->sc_adapter.adapt_nchannels = 1;
    459 	sc->sc_adapter.adapt_openings = IHA_MAX_SCB;
    460 	sc->sc_adapter.adapt_max_periph = IHA_MAX_SCB;
    461 	sc->sc_adapter.adapt_ioctl = NULL;
    462 	sc->sc_adapter.adapt_minphys = iha_minphys;
    463 	sc->sc_adapter.adapt_request = iha_scsipi_request;
    464 
    465 	/*
    466 	 * fill in the channel.
    467 	 */
    468 	sc->sc_channel.chan_adapter = &sc->sc_adapter;
    469 	sc->sc_channel.chan_bustype = &scsi_bustype;
    470 	sc->sc_channel.chan_channel = 0;
    471 	sc->sc_channel.chan_ntargets = CFG_TARGET(conf->config2);
    472 	sc->sc_channel.chan_nluns = 8;
    473 	sc->sc_channel.chan_id = sc->sc_id;
    474 
    475 	/*
    476 	 * Now try to attach all the sub devices.
    477 	 */
    478 	config_found(&sc->sc_dev, &sc->sc_channel, scsiprint);
    479 }
    480 
    481 /*
    482  * iha_minphys - reduce bp->b_bcount to something less than
    483  *		 or equal to the largest I/O possible through
    484  *		 the adapter. Called from higher layers
    485  *		 via sc->sc_adapter.scsi_minphys.
    486  */
    487 static void
    488 iha_minphys(bp)
    489 	struct buf *bp;
    490 {
    491 	if (bp->b_bcount > ((IHA_MAX_SG_ENTRIES - 1) * PAGE_SIZE))
    492 		bp->b_bcount = ((IHA_MAX_SG_ENTRIES - 1) * PAGE_SIZE);
    493 
    494 	minphys(bp);
    495 }
    496 
    497 /*
    498  * tul_reset_dma - abort any active DMA xfer, reset tulip FIFO.
    499  */
    500 static void
    501 tul_reset_dma(sc)
    502 	struct iha_softc *sc;
    503 {
    504 	bus_space_tag_t iot = sc->sc_iot;
    505 	bus_space_handle_t ioh = sc->sc_ioh;
    506 
    507 	if ((bus_space_read_1(iot, ioh, TUL_ISTUS1) & XPEND) != 0) {
    508 		/* if DMA xfer is pending, abort DMA xfer */
    509 		bus_space_write_1(iot, ioh, TUL_DCMD, ABTXFR);
    510 		/* wait Abort DMA xfer done */
    511 		while ((bus_space_read_1(iot, ioh, TUL_ISTUS0) & DABT) == 0)
    512 			;
    513 	}
    514 
    515 	bus_space_write_1(iot, ioh, TUL_SCTRL0, RSFIFO);
    516 }
    517 
    518 /*
    519  * tul_append_free_scb - append the supplied SCB to the tail of the
    520  *			 sc_freescb queue after clearing and resetting
    521  *			 everything possible.
    522  */
    523 static void
    524 tul_append_free_scb(sc, scb)
    525 	struct iha_softc *sc;
    526 	struct iha_scsi_req_q *scb;
    527 {
    528 	int s;
    529 
    530 	s = splbio();
    531 
    532 	if (scb->xs != NULL)
    533 		callout_stop(&scb->xs->xs_callout);
    534 
    535 	if (scb == sc->sc_actscb)
    536 		sc->sc_actscb = NULL;
    537 
    538 	scb->status = STATUS_QUEUED;
    539 	scb->ha_stat = HOST_OK;
    540 	scb->ta_stat  = SCSI_OK;
    541 
    542 	scb->nextstat = 0;
    543 	scb->sg_index = 0;
    544 	scb->sg_max = 0;
    545 	scb->flags = 0;
    546 	scb->target = 0;
    547 	scb->lun = 0;
    548 	scb->buflen = 0;
    549 	scb->sg_size = 0;
    550 	scb->cmdlen = 0;
    551 	scb->scb_id = 0;
    552 	scb->scb_tagmsg = 0;
    553 	scb->timeout = 0;
    554 	scb->bufaddr = 0;
    555 
    556 	scb->xs = NULL;
    557 	scb->tcs = NULL;
    558 
    559 	bzero(scb->cmd, sizeof(scb->cmd));
    560 	bzero(scb->sglist, sizeof(scb->sglist));
    561 
    562 	/*
    563 	 * scb_tagid, sg_addr, sglist
    564 	 * SCB_SensePtr are set at initialization
    565 	 * and never change
    566 	 */
    567 
    568 	TAILQ_INSERT_TAIL(&sc->sc_freescb, scb, chain);
    569 
    570 	splx(s);
    571 }
    572 
    573 static __inline void
    574 tul_append_pend_scb(sc, scb)
    575 	struct iha_softc *sc;
    576 	struct iha_scsi_req_q *scb;
    577 {
    578 	/* ASSUMPTION: only called within a splbio()/splx() pair */
    579 
    580 	if (scb == sc->sc_actscb)
    581 		sc->sc_actscb = NULL;
    582 
    583 	scb->status = STATUS_QUEUED;
    584 
    585 	TAILQ_INSERT_TAIL(&sc->sc_pendscb, scb, chain);
    586 }
    587 
    588 static __inline void
    589 tul_push_pend_scb(sc, scb)
    590 	struct iha_softc *sc;
    591 	struct iha_scsi_req_q *scb;
    592 {
    593 	int s;
    594 
    595 	s = splbio();
    596 
    597 	if (scb == sc->sc_actscb)
    598 		sc->sc_actscb = NULL;
    599 
    600 	scb->status = STATUS_QUEUED;
    601 
    602 	TAILQ_INSERT_HEAD(&sc->sc_pendscb, scb, chain);
    603 
    604 	splx(s);
    605 }
    606 
    607 /*
    608  * tul_find_pend_scb - scan the pending queue for a SCB that can be
    609  *		       processed immediately. Return NULL if none found
    610  *		       and a pointer to the SCB if one is found. If there
    611  *		       is an active SCB, return NULL!
    612  */
    613 static struct iha_scsi_req_q *
    614 tul_find_pend_scb(sc)
    615 	struct iha_softc *sc;
    616 {
    617 	struct iha_scsi_req_q *scb;
    618 	struct tcs *tcs;
    619 	int s;
    620 
    621 	s = splbio();
    622 
    623 	if (sc->sc_actscb != NULL)
    624 		scb = NULL;
    625 
    626 	else
    627 		TAILQ_FOREACH(scb, &sc->sc_pendscb, chain) {
    628 			if ((scb->flags & XS_CTL_RESET) != 0)
    629 				/* ALWAYS willing to reset a device */
    630 				break;
    631 
    632 			tcs = scb->tcs;
    633 
    634 			if ((scb->scb_tagmsg) != 0) {
    635 				/*
    636 				 * A Tagged I/O. OK to start If no
    637 				 * non-tagged I/O is active on the same
    638 				 * target
    639 				 */
    640 				if (tcs->ntagscb == NULL)
    641 					break;
    642 
    643 			} else	if (scb->cmd[0] == REQUEST_SENSE) {
    644 				/*
    645 				 * OK to do a non-tagged request sense
    646 				 * even if a non-tagged I/O has been
    647 				 * started, 'cuz we don't allow any
    648 				 * disconnect during a request sense op
    649 				 */
    650 				break;
    651 
    652 			} else	if (tcs->tagcnt == 0) {
    653 				/*
    654 				 * No tagged I/O active on this target,
    655 				 * ok to start a non-tagged one if one
    656 				 * is not already active
    657 				 */
    658 				if (tcs->ntagscb == NULL)
    659 					break;
    660 			}
    661 		}
    662 
    663 	splx(s);
    664 
    665 	return (scb);
    666 }
    667 
    668 /*
    669  * tul_del_pend_scb - remove scb from sc_pendscb
    670  */
    671 static __inline void
    672 tul_del_pend_scb(sc, scb)
    673 	struct iha_softc *sc;
    674 	struct iha_scsi_req_q *scb;
    675 {
    676 	int s;
    677 
    678 	s = splbio();
    679 
    680 	TAILQ_REMOVE(&sc->sc_pendscb, scb, chain);
    681 
    682 	splx(s);
    683 }
    684 
    685 static __inline void
    686 tul_mark_busy_scb(scb)
    687 	struct iha_scsi_req_q *scb;
    688 {
    689 	int  s;
    690 
    691 	s = splbio();
    692 
    693 	scb->status = STATUS_BUSY;
    694 
    695 	if (scb->scb_tagmsg == 0)
    696 		scb->tcs->ntagscb = scb;
    697 	else
    698 		scb->tcs->tagcnt++;
    699 
    700 	splx(s);
    701 }
    702 
    703 static void
    704 tul_append_done_scb(sc, scb, hastat)
    705 	struct iha_softc *sc;
    706 	struct iha_scsi_req_q *scb;
    707 	u_int8_t hastat;
    708 {
    709 	struct tcs *tcs;
    710 	int s;
    711 
    712 	s = splbio();
    713 
    714 	if (scb->xs != NULL)
    715 		callout_stop(&scb->xs->xs_callout);
    716 
    717 	if (scb == sc->sc_actscb)
    718 		sc->sc_actscb = NULL;
    719 
    720 	tcs = scb->tcs;
    721 
    722 	if (scb->scb_tagmsg != 0) {
    723 		if (tcs->tagcnt)
    724 			tcs->tagcnt--;
    725 	} else if (tcs->ntagscb == scb)
    726 		tcs->ntagscb = NULL;
    727 
    728 	scb->status = STATUS_QUEUED;
    729 	scb->ha_stat = hastat;
    730 
    731 	TAILQ_INSERT_TAIL(&sc->sc_donescb, scb, chain);
    732 
    733 	splx(s);
    734 }
    735 
    736 static __inline struct iha_scsi_req_q *
    737 tul_pop_done_scb(sc)
    738 	struct iha_softc *sc;
    739 {
    740 	struct iha_scsi_req_q *scb;
    741 	int s;
    742 
    743 	s = splbio();
    744 
    745 	scb = TAILQ_FIRST(&sc->sc_donescb);
    746 
    747 	if (scb != NULL) {
    748 		scb->status = STATUS_RENT;
    749 		TAILQ_REMOVE(&sc->sc_donescb, scb, chain);
    750 	}
    751 
    752 	splx(s);
    753 
    754 	return (scb);
    755 }
    756 
    757 /*
    758  * tul_abort_xs - find the SCB associated with the supplied xs and
    759  *                stop all processing on it, moving it to the done
    760  *                queue with the supplied host status value.
    761  */
    762 static void
    763 tul_abort_xs(sc, xs, hastat)
    764 	struct iha_softc *sc;
    765 	struct scsipi_xfer *xs;
    766 	u_int8_t hastat;
    767 {
    768 	struct iha_scsi_req_q *scb;
    769 	int i, s;
    770 
    771 	s = splbio();
    772 
    773 	/* Check the pending queue for the SCB pointing to xs */
    774 
    775 	TAILQ_FOREACH(scb, &sc->sc_pendscb, chain)
    776 		if (scb->xs == xs) {
    777 			tul_del_pend_scb(sc, scb);
    778 			tul_append_done_scb(sc, scb, hastat);
    779 			splx(s);
    780 			return;
    781 		}
    782 
    783 	/*
    784 	 * If that didn't work, check all BUSY/SELECTING SCB's for one
    785 	 * pointing to xs
    786 	 */
    787 
    788 	for (i = 0, scb = sc->sc_scb; i < IHA_MAX_SCB; i++, scb++)
    789 		switch (scb->status) {
    790 		case STATUS_BUSY:
    791 		case STATUS_SELECT:
    792 			if (scb->xs == xs) {
    793 				tul_append_done_scb(sc, scb, hastat);
    794 				splx(s);
    795 				return;
    796 			}
    797 			break;
    798 		default:
    799 			break;
    800 		}
    801 
    802 	splx(s);
    803 }
    804 
    805 /*
    806  * tul_bad_seq - a SCSI bus phase was encountered out of the
    807  *               correct/expected sequence. Reset the SCSI bus.
    808  */
    809 static void
    810 tul_bad_seq(sc)
    811 	struct iha_softc *sc;
    812 {
    813 	struct iha_scsi_req_q *scb = sc->sc_actscb;
    814 
    815 	if (scb != NULL)
    816 		tul_append_done_scb(sc, scb, HOST_BAD_PHAS);
    817 
    818 	tul_reset_scsi_bus(sc);
    819 	tul_reset_chip(sc);
    820 }
    821 
    822 /*
    823  * tul_push_sense_request - obtain auto sense data by pushing the
    824  *			    SCB needing it back onto the pending
    825  *			    queue with a REQUEST_SENSE CDB.
    826  */
    827 static int
    828 tul_push_sense_request(sc, scb)
    829 	struct iha_softc *sc;
    830 	struct iha_scsi_req_q *scb;
    831 {
    832 	struct scsipi_xfer *xs = scb->xs;
    833 	struct scsipi_periph *periph = xs->xs_periph;
    834 	struct scsipi_sense *ss = (struct scsipi_sense *)scb->cmd;
    835 	int lun = periph->periph_lun;
    836 	int err;
    837 
    838 	ss->opcode = REQUEST_SENSE;
    839 	ss->byte2 = lun << SCSI_CMD_LUN_SHIFT;
    840 	ss->unused[0] = ss->unused[1] = 0;
    841 	ss->length = sizeof(struct scsipi_sense_data);
    842 	ss->control = 0;
    843 
    844 	scb->flags &= ~(FLAG_SG | XS_CTL_DATA_OUT);
    845 	scb->flags |= FLAG_RSENS | XS_CTL_DATA_IN;
    846 
    847 	scb->scb_id &= ~MSG_IDENTIFY_DISCFLAG;
    848 
    849 	scb->scb_tagmsg = 0;
    850 	scb->ta_stat = SCSI_OK;
    851 
    852 	scb->cmdlen = sizeof(struct scsipi_sense);
    853 	scb->buflen = ss->length;
    854 
    855 	err = bus_dmamap_load(sc->sc_dmat, scb->dmap,
    856 	    &xs->sense.scsi_sense, scb->buflen, NULL, BUS_DMA_NOWAIT);
    857 	if (err != 0) {
    858 		printf("iha_push_sense_request: cannot bus_dmamap_load()\n");
    859 		xs->error = XS_DRIVER_STUFFUP;
    860 		return 1;
    861 	}
    862 	bus_dmamap_sync(sc->sc_dmat, scb->dmap,
    863 	    0, scb->buflen, BUS_DMASYNC_PREREAD);
    864 
    865 	/* XXX What about queued command? */
    866 	tul_exec_scb(sc, scb);
    867 
    868 	return 0;
    869 }
    870 
    871 /*
    872  * tul_main - process the active SCB, taking one off pending and making it
    873  *	      active if necessary, and any done SCB's created as
    874  *	      a result until there are no interrupts pending and no pending
    875  *	      SCB's that can be started.
    876  */
    877 static void
    878 tul_main(sc)
    879 	struct iha_softc *sc;
    880 {
    881 	bus_space_tag_t iot = sc->sc_iot;
    882 	bus_space_handle_t ioh =sc->sc_ioh;
    883 	struct iha_scsi_req_q *scb;
    884 
    885 	for (;;) {
    886 		tul_scsi(sc);
    887 
    888 		while ((scb = tul_pop_done_scb(sc)) != NULL)
    889 			tul_done_scb(sc, scb);
    890 
    891 		/*
    892 		 * If there are no interrupts pending, or we can't start
    893 		 * a pending sc, break out of the for(;;). Otherwise
    894 		 * continue the good work with another call to
    895 		 * tul_scsi().
    896 		 */
    897 		if (((bus_space_read_1(iot, ioh, TUL_STAT0) & INTPD) == 0)
    898 		    && (tul_find_pend_scb(sc) == NULL))
    899 			break;
    900 	}
    901 }
    902 
    903 /*
    904  * tul_scsi - service any outstanding interrupts. If there are none, try to
    905  *            start another SCB currently in the pending queue.
    906  */
    907 static void
    908 tul_scsi(sc)
    909 	struct iha_softc *sc;
    910 {
    911 	bus_space_tag_t iot = sc->sc_iot;
    912 	bus_space_handle_t ioh = sc->sc_ioh;
    913 	struct iha_scsi_req_q *scb;
    914 	struct tcs *tcs;
    915 	u_int8_t stat;
    916 
    917 	/* service pending interrupts asap */
    918 
    919 	stat = bus_space_read_1(iot, ioh, TUL_STAT0);
    920 	if ((stat & INTPD) != 0) {
    921 		sc->sc_status0 = stat;
    922 		sc->sc_status1 = bus_space_read_1(iot, ioh, TUL_STAT1);
    923 		sc->sc_sistat = bus_space_read_1(iot, ioh, TUL_SISTAT);
    924 
    925 		sc->sc_phase = sc->sc_status0 & PH_MASK;
    926 
    927 		if ((sc->sc_sistat & SRSTD) != 0) {
    928 			tul_reset_scsi_bus(sc);
    929 			return;
    930 		}
    931 
    932 		if ((sc->sc_sistat & RSELED) != 0) {
    933 			tul_resel(sc);
    934 			return;
    935 		}
    936 
    937 		if ((sc->sc_sistat & (STIMEO | DISCD)) != 0) {
    938 			tul_busfree(sc);
    939 			return;
    940 		}
    941 
    942 		if ((sc->sc_sistat & (SCMDN | SBSRV)) != 0) {
    943 			tul_next_state(sc);
    944 			return;
    945 		}
    946 
    947 		if ((sc->sc_sistat & SELED) != 0)
    948 			tul_set_ssig(sc, 0, 0);
    949 	}
    950 
    951 	/*
    952 	 * There were no interrupts pending which required action elsewhere, so
    953 	 * see if it is possible to start the selection phase on a pending SCB
    954 	 */
    955 	if ((scb = tul_find_pend_scb(sc)) == NULL)
    956 		return;
    957 
    958 	tcs = scb->tcs;
    959 
    960 	/* program HBA's SCSI ID & target SCSI ID */
    961 	bus_space_write_1(iot, ioh, TUL_SID, (sc->sc_id << 4) | scb->target);
    962 
    963 	if ((scb->flags & XS_CTL_RESET) == 0) {
    964 		bus_space_write_1(iot, ioh, TUL_SYNCM, tcs->syncm);
    965 
    966 		if ((tcs->flags & FLAG_NO_NEG_SYNC) == 0 ||
    967 		    (tcs->flags & FLAG_NO_NEG_WIDE) == 0)
    968 			tul_select(sc, scb, SELATNSTOP);
    969 
    970 		else if (scb->scb_tagmsg != 0)
    971 			tul_select(sc, scb, SEL_ATN3);
    972 
    973 		else
    974 			tul_select(sc, scb, SEL_ATN);
    975 
    976 	} else {
    977 		tul_select(sc, scb, SELATNSTOP);
    978 		scb->nextstat = 8;
    979 	}
    980 
    981 	if ((scb->flags & XS_CTL_POLL) != 0) {
    982 		for (; scb->timeout > 0; scb->timeout--) {
    983 			if (tul_wait(sc, NO_OP) == -1)
    984 				break;
    985 			if (tul_next_state(sc) == -1)
    986 				break;
    987 			delay(1000); /* Only happens in boot, so it's ok */
    988 		}
    989 
    990 		/*
    991 		 * Since done queue processing not done until AFTER this
    992 		 * function returns, scb is on the done queue, not
    993 		 * the free queue at this point and still has valid data
    994 		 *
    995 		 * Conversely, xs->error has not been set yet
    996 		 */
    997 		if (scb->timeout == 0)
    998 			tul_timeout(scb);
    999 	}
   1000 }
   1001 
   1002 /*
   1003  * tul_data_over_run - return HOST_OK for all SCSI opcodes where BufLen
   1004  *		       is an 'Allocation Length'. All other SCSI opcodes
   1005  *		       get HOST_DO_DU as they SHOULD have xferred all the
   1006  *		       data requested.
   1007  *
   1008  *		       The list of opcodes using 'Allocation Length' was
   1009  *		       found by scanning all the SCSI-3 T10 drafts. See
   1010  *		       www.t10.org for the curious with a .pdf reader.
   1011  */
   1012 static u_int8_t
   1013 tul_data_over_run(scb)
   1014 	struct iha_scsi_req_q *scb;
   1015 {
   1016 	switch (scb->cmd[0]) {
   1017 	case 0x03: /* Request Sense                   SPC-2 */
   1018 	case 0x12: /* Inquiry                         SPC-2 */
   1019 	case 0x1a: /* Mode Sense (6 byte version)     SPC-2 */
   1020 	case 0x1c: /* Receive Diagnostic Results      SPC-2 */
   1021 	case 0x23: /* Read Format Capacities          MMC-2 */
   1022 	case 0x29: /* Read Generation                 SBC   */
   1023 	case 0x34: /* Read Position                   SSC-2 */
   1024 	case 0x37: /* Read Defect Data                SBC   */
   1025 	case 0x3c: /* Read Buffer                     SPC-2 */
   1026 	case 0x42: /* Read Sub Channel                MMC-2 */
   1027 	case 0x43: /* Read TOC/PMA/ATIP               MMC   */
   1028 
   1029 	/* XXX - 2 with same opcode of 0x44? */
   1030 	case 0x44: /* Read Header/Read Density Suprt  MMC/SSC*/
   1031 
   1032 	case 0x46: /* Get Configuration               MMC-2 */
   1033 	case 0x4a: /* Get Event/Status Notification   MMC-2 */
   1034 	case 0x4d: /* Log Sense                       SPC-2 */
   1035 	case 0x51: /* Read Disc Information           MMC   */
   1036 	case 0x52: /* Read Track Information          MMC   */
   1037 	case 0x59: /* Read Master CUE                 MMC   */
   1038 	case 0x5a: /* Mode Sense (10 byte version)    SPC-2 */
   1039 	case 0x5c: /* Read Buffer Capacity            MMC   */
   1040 	case 0x5e: /* Persistant Reserve In           SPC-2 */
   1041 	case 0x84: /* Receive Copy Results            SPC-2 */
   1042 	case 0xa0: /* Report LUNs                     SPC-2 */
   1043 	case 0xa3: /* Various Report requests         SBC-2/SCC-2*/
   1044 	case 0xa4: /* Report Key                      MMC-2 */
   1045 	case 0xad: /* Read DVD Structure              MMC-2 */
   1046 	case 0xb4: /* Read Element Status (Attached)  SMC   */
   1047 	case 0xb5: /* Request Volume Element Address  SMC   */
   1048 	case 0xb7: /* Read Defect Data (12 byte ver.) SBC   */
   1049 	case 0xb8: /* Read Element Status (Independ.) SMC   */
   1050 	case 0xba: /* Report Redundancy               SCC-2 */
   1051 	case 0xbd: /* Mechanism Status                MMC   */
   1052 	case 0xbe: /* Report Basic Redundancy         SCC-2 */
   1053 
   1054 		return (HOST_OK);
   1055 		break;
   1056 
   1057 	default:
   1058 		return (HOST_DO_DU);
   1059 		break;
   1060 	}
   1061 }
   1062 
   1063 /*
   1064  * tul_next_state - prcess the current SCB as requested in it's
   1065  *                  nextstat member.
   1066  */
   1067 static int
   1068 tul_next_state(sc)
   1069 	struct iha_softc *sc;
   1070 {
   1071 
   1072 	if (sc->sc_actscb == NULL)
   1073 		return (-1);
   1074 
   1075 	switch (sc->sc_actscb->nextstat) {
   1076 	case 1:
   1077 		if (tul_state_1(sc) == 3)
   1078 			goto state_3;
   1079 		break;
   1080 
   1081 	case 2:
   1082 		switch (tul_state_2(sc)) {
   1083 		case 3:
   1084 			goto state_3;
   1085 		case 4:
   1086 			goto state_4;
   1087 		default:
   1088 			break;
   1089 		}
   1090 		break;
   1091 
   1092 	case 3:
   1093 	state_3:
   1094 		if (tul_state_3(sc) == 4)
   1095 			goto state_4;
   1096 		break;
   1097 
   1098 	case 4:
   1099 	state_4:
   1100 		switch (tul_state_4(sc)) {
   1101 		case 0:
   1102 			return (0);
   1103 		case 6:
   1104 			goto state_6;
   1105 		default:
   1106 			break;
   1107 		}
   1108 		break;
   1109 
   1110 	case 5:
   1111 		switch (tul_state_5(sc)) {
   1112 		case 4:
   1113 			goto state_4;
   1114 		case 6:
   1115 			goto state_6;
   1116 		default:
   1117 			break;
   1118 		}
   1119 		break;
   1120 
   1121 	case 6:
   1122 	state_6:
   1123 		tul_state_6(sc);
   1124 		break;
   1125 
   1126 	case 8:
   1127 		tul_state_8(sc);
   1128 		break;
   1129 
   1130 	default:
   1131 #ifdef IHA_DEBUG_STATE
   1132 		printf("[debug] -unknown state: %i-\n",
   1133 		    sc->sc_actscb->nextstat);
   1134 #endif
   1135 		tul_bad_seq(sc);
   1136 		break;
   1137 	}
   1138 
   1139 	return (-1);
   1140 }
   1141 
   1142 /*
   1143  * tul_state_1 - selection is complete after a SELATNSTOP. If the target
   1144  *               has put the bus into MSG_OUT phase start wide/sync
   1145  *               negotiation. Otherwise clear the FIFO and go to state 3,
   1146  *	    	 which will send the SCSI CDB to the target.
   1147  */
   1148 static int
   1149 tul_state_1(sc)
   1150 	struct iha_softc *sc;
   1151 {
   1152 	bus_space_tag_t iot = sc->sc_iot;
   1153 	bus_space_handle_t ioh = sc->sc_ioh;
   1154 	struct iha_scsi_req_q *scb = sc->sc_actscb;
   1155 	struct tcs *tcs;
   1156 	int flags;
   1157 
   1158 	tul_mark_busy_scb(scb);
   1159 
   1160 	tcs = scb->tcs;
   1161 
   1162 	bus_space_write_1(iot, ioh, TUL_SCONFIG0, tcs->sconfig0);
   1163 
   1164 	/*
   1165 	 * If we are in PHASE_MSG_OUT, send
   1166 	 *     a) IDENT message (with tags if appropriate)
   1167 	 *     b) WDTR if the target is configured to negotiate wide xfers
   1168 	 *     ** OR **
   1169 	 *     c) SDTR if the target is configured to negotiate sync xfers
   1170 	 *	  but not wide ones
   1171 	 *
   1172 	 * If we are NOT, then the target is not asking for anything but
   1173 	 * the data/command, so go straight to state 3.
   1174 	 */
   1175 	if (sc->sc_phase == PHASE_MSG_OUT) {
   1176 		bus_space_write_1(iot, ioh, TUL_SCTRL1, (ESBUSIN | EHRSL));
   1177 		bus_space_write_1(iot, ioh, TUL_SFIFO, scb->scb_id);
   1178 
   1179 		if (scb->scb_tagmsg != 0) {
   1180 			bus_space_write_1(iot, ioh, TUL_SFIFO,
   1181 			    scb->scb_tagmsg);
   1182 			bus_space_write_1(iot, ioh, TUL_SFIFO,
   1183 			    scb->scb_tagid);
   1184 		}
   1185 
   1186 		flags = tcs->flags;
   1187 		if ((flags & FLAG_NO_NEG_WIDE) == 0) {
   1188 			if (tul_msgout_wide(sc) == -1)
   1189 				return (-1);
   1190 		} else if ((flags & FLAG_NO_NEG_SYNC) == 0) {
   1191 			if (tul_msgout_sync(sc) == -1)
   1192 				return (-1);
   1193 		}
   1194 
   1195 	} else {
   1196 		bus_space_write_1(iot, ioh, TUL_SCTRL0, RSFIFO);
   1197 		tul_set_ssig(sc, REQ | BSY | SEL | ATN, 0);
   1198 	}
   1199 
   1200 	return (3);
   1201 }
   1202 
   1203 /*
   1204  * tul_state_2 - selection is complete after a SEL_ATN or SEL_ATN3. If the SCSI
   1205  *		 CDB has already been send, go to state 4 to start the data
   1206  *		 xfer. Otherwise reset the FIFO and go to state 3, sending
   1207  *		 the SCSI CDB.
   1208  */
   1209 static int
   1210 tul_state_2(sc)
   1211 	struct iha_softc *sc;
   1212 {
   1213 	bus_space_tag_t iot = sc->sc_iot;
   1214 	bus_space_handle_t ioh = sc->sc_ioh;
   1215 	struct iha_scsi_req_q *scb = sc->sc_actscb;
   1216 
   1217 	tul_mark_busy_scb(scb);
   1218 
   1219 	bus_space_write_1(iot, ioh, TUL_SCONFIG0, scb->tcs->sconfig0);
   1220 
   1221 	if ((sc->sc_status1 & CPDNE) != 0)
   1222 		return (4);
   1223 
   1224 	bus_space_write_1(iot, ioh, TUL_SCTRL0, RSFIFO);
   1225 
   1226 	tul_set_ssig(sc, REQ | BSY | SEL | ATN, 0);
   1227 
   1228 	return (3);
   1229 }
   1230 
   1231 /*
   1232  * tul_state_3 - send the SCSI CDB to the target, processing any status
   1233  *		 or other messages received until that is done or
   1234  *		 abandoned.
   1235  */
   1236 static int
   1237 tul_state_3(sc)
   1238 	struct iha_softc *sc;
   1239 {
   1240 	bus_space_tag_t iot = sc->sc_iot;
   1241 	bus_space_handle_t ioh = sc->sc_ioh;
   1242 	struct iha_scsi_req_q *scb = sc->sc_actscb;
   1243 	int flags;
   1244 
   1245 	for (;;) {
   1246 		switch (sc->sc_phase) {
   1247 		case PHASE_CMD_OUT:
   1248 			bus_space_write_multi_1(iot, ioh, TUL_SFIFO,
   1249 			    scb->cmd, scb->cmdlen);
   1250 			if (tul_wait(sc, XF_FIFO_OUT) == -1)
   1251 				return (-1);
   1252 			else if (sc->sc_phase == PHASE_CMD_OUT) {
   1253 				tul_bad_seq(sc);
   1254 				return (-1);
   1255 			} else
   1256 				return (4);
   1257 
   1258 		case PHASE_MSG_IN:
   1259 			scb->nextstat = 3;
   1260 			if (tul_msgin(sc) == -1)
   1261 				return (-1);
   1262 			break;
   1263 
   1264 		case PHASE_STATUS_IN:
   1265 			if (tul_status_msg(sc) == -1)
   1266 				return (-1);
   1267 			break;
   1268 
   1269 		case PHASE_MSG_OUT:
   1270 			flags = scb->tcs->flags;
   1271 			if ((flags & FLAG_NO_NEG_SYNC) != 0) {
   1272 				if (tul_msgout(sc, MSG_NOOP) == -1)
   1273 					return (-1);
   1274 			} else if (tul_msgout_sync(sc) == -1)
   1275 				return (-1);
   1276 			break;
   1277 
   1278 		default:
   1279 			printf("[debug] -s3- bad phase = %d\n", sc->sc_phase);
   1280 			tul_bad_seq(sc);
   1281 			return (-1);
   1282 		}
   1283 	}
   1284 }
   1285 
   1286 /*
   1287  * tul_state_4 - start a data xfer. Handle any bus state
   1288  *               transitions until PHASE_DATA_IN/_OUT
   1289  *               or the attempt is abandoned. If there is
   1290  *               no data to xfer, go to state 6 and finish
   1291  *               processing the current SCB.
   1292  */
   1293 static int
   1294 tul_state_4(sc)
   1295 	struct iha_softc *sc;
   1296 {
   1297 	struct iha_scsi_req_q *scb = sc->sc_actscb;
   1298 
   1299 	if ((scb->flags & (XS_CTL_DATA_IN | XS_CTL_DATA_OUT)) ==
   1300 	    (XS_CTL_DATA_IN | XS_CTL_DATA_OUT))
   1301 		return (6); /* Both dir flags set => NO xfer was requested */
   1302 
   1303 	for (;;) {
   1304 		if (scb->buflen == 0)
   1305 			return (6);
   1306 
   1307 		switch (sc->sc_phase) {
   1308 		case PHASE_STATUS_IN:
   1309 			if ((scb->flags & (XS_CTL_DATA_IN | XS_CTL_DATA_OUT))
   1310 			    != 0)
   1311 				scb->ha_stat = tul_data_over_run(scb);
   1312 			if ((tul_status_msg(sc)) == -1)
   1313 				return (-1);
   1314 			break;
   1315 
   1316 		case PHASE_MSG_IN:
   1317 			scb->nextstat = 4;
   1318 			if (tul_msgin(sc) == -1)
   1319 				return (-1);
   1320 			break;
   1321 
   1322 		case PHASE_MSG_OUT:
   1323 			if ((sc->sc_status0 & SPERR) != 0) {
   1324 				scb->buflen = 0;
   1325 				scb->ha_stat = HOST_SPERR;
   1326 				if (tul_msgout(sc, MSG_INITIATOR_DET_ERR) == -1)
   1327 					return (-1);
   1328 				else
   1329 					return (6);
   1330 			} else {
   1331 				if (tul_msgout(sc, MSG_NOOP) == -1)
   1332 					return (-1);
   1333 			}
   1334 			break;
   1335 
   1336 		case PHASE_DATA_IN:
   1337 			return (tul_xfer_data(sc, scb, XS_CTL_DATA_IN));
   1338 
   1339 		case PHASE_DATA_OUT:
   1340 			return (tul_xfer_data(sc, scb, XS_CTL_DATA_OUT));
   1341 
   1342 		default:
   1343 			tul_bad_seq(sc);
   1344 			return (-1);
   1345 		}
   1346 	}
   1347 }
   1348 
   1349 /*
   1350  * tul_state_5 - handle the partial or final completion of the current
   1351  *		 data xfer. If DMA is still active stop it. If there is
   1352  *		 more data to xfer, go to state 4 and start the xfer.
   1353  *		 If not go to state 6 and finish the SCB.
   1354  */
   1355 static int
   1356 tul_state_5(sc)
   1357 	struct iha_softc *sc;
   1358 {
   1359 	bus_space_tag_t iot = sc->sc_iot;
   1360 	bus_space_handle_t ioh = sc->sc_ioh;
   1361 	struct iha_scsi_req_q *scb = sc->sc_actscb;
   1362 	struct iha_sg_element *sg;
   1363 	u_int32_t cnt;
   1364 	u_int8_t period, stat;
   1365 	long xcnt;  /* cannot use unsigned!! see code: if (xcnt < 0) */
   1366 	int i;
   1367 
   1368 	cnt = bus_space_read_4(iot, ioh, TUL_STCNT0) & TCNT;
   1369 
   1370 	/*
   1371 	 * Stop any pending DMA activity and check for parity error.
   1372 	 */
   1373 
   1374 	if ((bus_space_read_1(iot, ioh, TUL_DCMD) & XDIR) != 0) {
   1375 		/* Input Operation */
   1376 		if ((sc->sc_status0 & SPERR) != 0)
   1377 			scb->ha_stat = HOST_SPERR;
   1378 
   1379 		if ((bus_space_read_1(iot, ioh, TUL_ISTUS1) & XPEND) != 0) {
   1380 			bus_space_write_1(iot, ioh, TUL_DCTRL0,
   1381 			    bus_space_read_1(iot, ioh, TUL_DCTRL0) | SXSTP);
   1382 			while (bus_space_read_1(iot, ioh, TUL_ISTUS1) & XPEND)
   1383 				;
   1384 		}
   1385 
   1386 	} else {
   1387 		/* Output Operation */
   1388 		if ((sc->sc_status1 & SXCMP) == 0) {
   1389 			period = scb->tcs->syncm;
   1390 			if ((period & PERIOD_WIDE_SCSI) != 0)
   1391 				cnt += (bus_space_read_1(iot, ioh,
   1392 				    TUL_SFIFOCNT) & FIFOC) * 2;
   1393 			else
   1394 				cnt += bus_space_read_1(iot, ioh,
   1395 				    TUL_SFIFOCNT) & FIFOC;
   1396 		}
   1397 
   1398 		if ((bus_space_read_1(iot, ioh, TUL_ISTUS1) & XPEND) != 0) {
   1399 			bus_space_write_1(iot, ioh, TUL_DCMD, ABTXFR);
   1400 			do
   1401 				stat = bus_space_read_1(iot, ioh, TUL_ISTUS0);
   1402 			while ((stat & DABT) == 0);
   1403 		}
   1404 
   1405 		if ((cnt == 1) && (sc->sc_phase == PHASE_DATA_OUT)) {
   1406 			if (tul_wait(sc, XF_FIFO_OUT) == -1)
   1407 				return (-1);
   1408 			cnt = 0;
   1409 
   1410 		} else if ((sc->sc_status1 & SXCMP) == 0)
   1411 			bus_space_write_1(iot, ioh, TUL_SCTRL0, RSFIFO);
   1412 	}
   1413 
   1414 	if (cnt == 0) {
   1415 		scb->buflen = 0;
   1416 		return (6);
   1417 	}
   1418 
   1419 	/* Update active data pointer and restart the I/O at the new point */
   1420 
   1421 	xcnt = scb->buflen - cnt;	/* xcnt == bytes xferred */
   1422 	scb->buflen = cnt;	  	/* cnt  == bytes left    */
   1423 
   1424 	if ((scb->flags & FLAG_SG) != 0) {
   1425 		sg = &scb->sglist[scb->sg_index];
   1426 		for (i = scb->sg_index; i < scb->sg_max; sg++, i++) {
   1427 			xcnt -= le32toh(sg->sg_len);
   1428 			if (xcnt < 0) {
   1429 				xcnt += le32toh(sg->sg_len);
   1430 
   1431 				sg->sg_addr =
   1432 				    htole32(le32toh(sg->sg_addr) + xcnt);
   1433 				sg->sg_len =
   1434 				    htole32(le32toh(sg->sg_len) - xcnt);
   1435 				bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap,
   1436 				    scb->sgoffset, IHA_SG_SIZE,
   1437 				    BUS_DMASYNC_PREWRITE);
   1438 
   1439 				scb->bufaddr += (i - scb->sg_index) *
   1440 				    sizeof(struct iha_sg_element);
   1441 				scb->sg_size = scb->sg_max - i;
   1442 				scb->sg_index = i;
   1443 
   1444 				return (4);
   1445 			}
   1446 		}
   1447 		return (6);
   1448 
   1449 	} else
   1450 		scb->bufaddr += xcnt;
   1451 
   1452 	return (4);
   1453 }
   1454 
   1455 /*
   1456  * tul_state_6 - finish off the active scb (may require several
   1457  *		 iterations if PHASE_MSG_IN) and return -1 to indicate
   1458  *		 the bus is free.
   1459  */
   1460 static int
   1461 tul_state_6(sc)
   1462 	struct iha_softc *sc;
   1463 {
   1464 
   1465 	for (;;) {
   1466 		switch (sc->sc_phase) {
   1467 		case PHASE_STATUS_IN:
   1468 			if (tul_status_msg(sc) == -1)
   1469 				return (-1);
   1470 			break;
   1471 
   1472 		case PHASE_MSG_IN:
   1473 			sc->sc_actscb->nextstat = 6;
   1474 			if ((tul_msgin(sc)) == -1)
   1475 				return (-1);
   1476 			break;
   1477 
   1478 		case PHASE_MSG_OUT:
   1479 			if ((tul_msgout(sc, MSG_NOOP)) == -1)
   1480 				return (-1);
   1481 			break;
   1482 
   1483 		case PHASE_DATA_IN:
   1484 			if (tul_xpad_in(sc) == -1)
   1485 				return (-1);
   1486 			break;
   1487 
   1488 		case PHASE_DATA_OUT:
   1489 			if (tul_xpad_out(sc) == -1)
   1490 				return (-1);
   1491 			break;
   1492 
   1493 		default:
   1494 			tul_bad_seq(sc);
   1495 			return (-1);
   1496 		}
   1497 	}
   1498 }
   1499 
   1500 /*
   1501  * tul_state_8 - reset the active device and all busy SCBs using it
   1502  */
   1503 static int
   1504 tul_state_8(sc)
   1505 	struct iha_softc *sc;
   1506 {
   1507 	bus_space_tag_t iot = sc->sc_iot;
   1508 	bus_space_handle_t ioh = sc->sc_ioh;
   1509 	struct iha_scsi_req_q *scb;
   1510 	int i;
   1511 	u_int8_t tar;
   1512 
   1513 	if (sc->sc_phase == PHASE_MSG_OUT) {
   1514 		bus_space_write_1(iot, ioh, TUL_SFIFO, MSG_BUS_DEV_RESET);
   1515 
   1516 		scb = sc->sc_actscb;
   1517 
   1518 		/* This SCB finished correctly -- resetting the device */
   1519 		tul_append_done_scb(sc, scb, HOST_OK);
   1520 
   1521 		tul_reset_tcs(scb->tcs, sc->sc_sconf1);
   1522 
   1523 		tar = scb->target;
   1524 		for (i = 0, scb = sc->sc_scb; i < IHA_MAX_SCB; i++, scb++)
   1525 			if (scb->target == tar)
   1526 				switch (scb->status) {
   1527 				case STATUS_BUSY:
   1528 					tul_append_done_scb(sc,
   1529 					    scb, HOST_DEV_RST);
   1530 					break;
   1531 
   1532 				case STATUS_SELECT:
   1533 					tul_push_pend_scb(sc, scb);
   1534 					break;
   1535 
   1536 				default:
   1537 					break;
   1538 				}
   1539 
   1540 		sc->sc_flags |= FLAG_EXPECT_DISC;
   1541 
   1542 		if (tul_wait(sc, XF_FIFO_OUT) == -1)
   1543 			return (-1);
   1544 	}
   1545 
   1546 	tul_bad_seq(sc);
   1547 	return (-1);
   1548 }
   1549 
   1550 /*
   1551  * tul_xfer_data - initiate the DMA xfer of the data
   1552  */
   1553 static int
   1554 tul_xfer_data(sc, scb, direction)
   1555 	struct iha_softc *sc;
   1556 	struct iha_scsi_req_q *scb;
   1557 	int direction;
   1558 {
   1559 	bus_space_tag_t iot = sc->sc_iot;
   1560 	bus_space_handle_t ioh = sc->sc_ioh;
   1561 	u_int32_t xferlen;
   1562 	u_int8_t xfertype;
   1563 
   1564 	if ((scb->flags & (XS_CTL_DATA_IN | XS_CTL_DATA_OUT)) != direction)
   1565 		return (6); /* wrong direction, abandon I/O */
   1566 
   1567 	bus_space_write_4(iot, ioh, TUL_STCNT0, scb->buflen);
   1568 
   1569 	if ((scb->flags & FLAG_SG) == 0) {
   1570 		xferlen = scb->buflen;
   1571 		xfertype = (direction == XS_CTL_DATA_IN) ? ST_X_IN : ST_X_OUT;
   1572 
   1573 	} else {
   1574 		xferlen = scb->sg_size * sizeof(struct iha_sg_element);
   1575 		xfertype = (direction == XS_CTL_DATA_IN) ? ST_SG_IN : ST_SG_OUT;
   1576 	}
   1577 
   1578 	bus_space_write_4(iot, ioh, TUL_DXC,  xferlen);
   1579 	bus_space_write_4(iot, ioh, TUL_DXPA, scb->bufaddr);
   1580 	bus_space_write_1(iot, ioh, TUL_DCMD, xfertype);
   1581 
   1582 	bus_space_write_1(iot, ioh, TUL_SCMD,
   1583 	    (direction == XS_CTL_DATA_IN) ? XF_DMA_IN : XF_DMA_OUT);
   1584 
   1585 	scb->nextstat = 5;
   1586 
   1587 	return (0);
   1588 }
   1589 
   1590 static int
   1591 tul_xpad_in(sc)
   1592 	struct iha_softc *sc;
   1593 {
   1594 	bus_space_tag_t iot = sc->sc_iot;
   1595 	bus_space_handle_t ioh = sc->sc_ioh;
   1596 	struct iha_scsi_req_q *scb = sc->sc_actscb;
   1597 
   1598 	if ((scb->flags & (XS_CTL_DATA_IN | XS_CTL_DATA_OUT)) != 0)
   1599 		scb->ha_stat = HOST_DO_DU;
   1600 
   1601 	for (;;) {
   1602 		if ((scb->tcs->syncm & PERIOD_WIDE_SCSI) != 0)
   1603 			bus_space_write_4(iot, ioh, TUL_STCNT0, 2);
   1604 		else
   1605 			bus_space_write_4(iot, ioh, TUL_STCNT0, 1);
   1606 
   1607 		switch (tul_wait(sc, XF_FIFO_IN)) {
   1608 		case -1:
   1609 			return (-1);
   1610 
   1611 		case PHASE_DATA_IN:
   1612 			bus_space_read_1(iot, ioh, TUL_SFIFO);
   1613 			break;
   1614 
   1615 		default:
   1616 			bus_space_write_1(iot, ioh, TUL_SCTRL0, RSFIFO);
   1617 			return (6);
   1618 		}
   1619 	}
   1620 }
   1621 
   1622 static int
   1623 tul_xpad_out(sc)
   1624 	struct iha_softc *sc;
   1625 {
   1626 	bus_space_tag_t iot = sc->sc_iot;
   1627 	bus_space_handle_t ioh = sc->sc_ioh;
   1628 	struct iha_scsi_req_q *scb = sc->sc_actscb;
   1629 
   1630 	if ((scb->flags & (XS_CTL_DATA_IN | XS_CTL_DATA_OUT)) != 0)
   1631 		scb->ha_stat = HOST_DO_DU;
   1632 
   1633 	for (;;) {
   1634 		if ((scb->tcs->syncm & PERIOD_WIDE_SCSI) != 0)
   1635 			bus_space_write_4(iot, ioh, TUL_STCNT0, 2);
   1636 		else
   1637 			bus_space_write_4(iot, ioh, TUL_STCNT0, 1);
   1638 
   1639 		bus_space_write_1(iot, ioh, TUL_SFIFO, 0);
   1640 
   1641 		switch (tul_wait(sc, XF_FIFO_OUT)) {
   1642 		case -1:
   1643 			return (-1);
   1644 
   1645 		case PHASE_DATA_OUT:
   1646 			break;
   1647 
   1648 		default:
   1649 			/* Disable wide CPU to allow read 16 bits */
   1650 			bus_space_write_1(iot, ioh, TUL_SCTRL1, EHRSL);
   1651 			bus_space_write_1(iot, ioh, TUL_SCTRL0, RSFIFO);
   1652 			return (6);
   1653 		}
   1654 	}
   1655 }
   1656 
   1657 static int
   1658 tul_status_msg(sc)
   1659 	struct iha_softc *sc;
   1660 {
   1661 	bus_space_tag_t iot = sc->sc_iot;
   1662 	bus_space_handle_t ioh = sc->sc_ioh;
   1663 	struct iha_scsi_req_q *scb;
   1664 	u_int8_t msg;
   1665 	int phase;
   1666 
   1667 	if ((phase = tul_wait(sc, CMD_COMP)) == -1)
   1668 		return (-1);
   1669 
   1670 	scb = sc->sc_actscb;
   1671 
   1672 	scb->ta_stat = bus_space_read_1(iot, ioh, TUL_SFIFO);
   1673 
   1674 	if (phase == PHASE_MSG_OUT) {
   1675 		if ((sc->sc_status0 & SPERR) == 0)
   1676 			bus_space_write_1(iot, ioh, TUL_SFIFO, MSG_NOOP);
   1677 		else
   1678 			bus_space_write_1(iot, ioh, TUL_SFIFO,
   1679 			    MSG_PARITY_ERROR);
   1680 
   1681 		return (tul_wait(sc, XF_FIFO_OUT));
   1682 
   1683 	} else if (phase == PHASE_MSG_IN) {
   1684 		msg = bus_space_read_1(iot, ioh, TUL_SFIFO);
   1685 
   1686 		if ((sc->sc_status0 & SPERR) != 0)
   1687 			switch (tul_wait(sc, MSG_ACCEPT)) {
   1688 			case -1:
   1689 				return (-1);
   1690 			case PHASE_MSG_OUT:
   1691 				bus_space_write_1(iot, ioh, TUL_SFIFO,
   1692 				    MSG_PARITY_ERROR);
   1693 				return (tul_wait(sc, XF_FIFO_OUT));
   1694 			default:
   1695 				tul_bad_seq(sc);
   1696 				return (-1);
   1697 			}
   1698 
   1699 		if (msg == MSG_CMDCOMPLETE) {
   1700 			if ((scb->ta_stat &
   1701 			    (SCSI_INTERM | SCSI_BUSY)) == SCSI_INTERM) {
   1702 				tul_bad_seq(sc);
   1703 				return (-1);
   1704 			}
   1705 			sc->sc_flags |= FLAG_EXPECT_DONE_DISC;
   1706 			bus_space_write_1(iot, ioh, TUL_SCTRL0, RSFIFO);
   1707 			return (tul_wait(sc, MSG_ACCEPT));
   1708 		}
   1709 
   1710 		if ((msg == MSG_LINK_CMD_COMPLETE)
   1711 		    || (msg == MSG_LINK_CMD_COMPLETEF)) {
   1712 			if ((scb->ta_stat &
   1713 			    (SCSI_INTERM | SCSI_BUSY)) == SCSI_INTERM)
   1714 				return (tul_wait(sc, MSG_ACCEPT));
   1715 		}
   1716 	}
   1717 
   1718 	tul_bad_seq(sc);
   1719 	return (-1);
   1720 }
   1721 
   1722 /*
   1723  * tul_busfree - SCSI bus free detected as a result of a TIMEOUT or
   1724  *		 DISCONNECT interrupt. Reset the tulip FIFO and
   1725  *		 SCONFIG0 and enable hardware reselect. Move any active
   1726  *		 SCB to sc_donescb list. Return an appropriate host status
   1727  *		 if an I/O was active.
   1728  */
   1729 static void
   1730 tul_busfree(sc)
   1731 	struct iha_softc *sc;
   1732 {
   1733 	bus_space_tag_t iot = sc->sc_iot;
   1734 	bus_space_handle_t ioh = sc->sc_ioh;
   1735 	struct iha_scsi_req_q *scb;
   1736 
   1737 	bus_space_write_1(iot, ioh, TUL_SCTRL0, RSFIFO);
   1738 	bus_space_write_1(iot, ioh, TUL_SCONFIG0, SCONFIG0DEFAULT);
   1739 	bus_space_write_1(iot, ioh, TUL_SCTRL1, EHRSL);
   1740 
   1741 	scb = sc->sc_actscb;
   1742 
   1743 	if (scb != NULL) {
   1744 		if (scb->status == STATUS_SELECT)
   1745 			/* selection timeout   */
   1746 			tul_append_done_scb(sc, scb, HOST_SEL_TOUT);
   1747 		else
   1748 			/* Unexpected bus free */
   1749 			tul_append_done_scb(sc, scb, HOST_BAD_PHAS);
   1750 	}
   1751 }
   1752 
   1753 static void
   1754 tul_reset_scsi_bus(sc)
   1755 	struct iha_softc *sc;
   1756 {
   1757 	struct iha_scsi_req_q *scb;
   1758 	struct tcs *tcs;
   1759 	int i, s;
   1760 
   1761 	s = splbio();
   1762 
   1763 	tul_reset_dma(sc);
   1764 
   1765 	for (i = 0, scb = sc->sc_scb; i < IHA_MAX_SCB; i++, scb++)
   1766 		switch (scb->status) {
   1767 		case STATUS_BUSY:
   1768 			tul_append_done_scb(sc, scb, HOST_SCSI_RST);
   1769 			break;
   1770 
   1771 		case STATUS_SELECT:
   1772 			tul_push_pend_scb(sc, scb);
   1773 			break;
   1774 
   1775 		default:
   1776 			break;
   1777 		}
   1778 
   1779 	for (i = 0, tcs = sc->sc_tcs; i < IHA_MAX_TARGETS; i++, tcs++)
   1780 		tul_reset_tcs(tcs, sc->sc_sconf1);
   1781 
   1782 	splx(s);
   1783 }
   1784 
   1785 /*
   1786  * tul_resel - handle a detected SCSI bus reselection request.
   1787  */
   1788 static int
   1789 tul_resel(sc)
   1790 	struct iha_softc *sc;
   1791 {
   1792 	bus_space_tag_t iot = sc->sc_iot;
   1793 	bus_space_handle_t ioh = sc->sc_ioh;
   1794 	struct iha_scsi_req_q *scb;
   1795 	struct tcs *tcs;
   1796 	u_int8_t tag, target, lun, msg, abortmsg;
   1797 
   1798 	if (sc->sc_actscb != NULL) {
   1799 		if ((sc->sc_actscb->status == STATUS_SELECT))
   1800 			/* sets ActScb to NULL */
   1801 			tul_push_pend_scb(sc, sc->sc_actscb);
   1802 		else
   1803 			sc->sc_actscb = NULL;
   1804 	}
   1805 
   1806 	target = bus_space_read_1(iot, ioh, TUL_SBID);
   1807 	lun = bus_space_read_1(iot, ioh, TUL_SALVC) & MSG_IDENTIFY_LUNMASK;
   1808 
   1809 	tcs = &sc->sc_tcs[target];
   1810 
   1811 	bus_space_write_1(iot, ioh, TUL_SCONFIG0, tcs->sconfig0);
   1812 	bus_space_write_1(iot, ioh, TUL_SYNCM, tcs->syncm);
   1813 
   1814 	abortmsg = MSG_ABORT; /* until a valid tag has been obtained */
   1815 
   1816 	if (tcs->ntagscb != NULL)
   1817 		/* There is a non-tagged I/O active on the target */
   1818 		scb = tcs->ntagscb;
   1819 
   1820 	else {
   1821 		/*
   1822 		 * Since there is no active non-tagged operation
   1823 		 * read the tag type, the tag itself, and find
   1824 		 * the appropriate scb by indexing sc_scb with
   1825 		 * the tag.
   1826 		 */
   1827 
   1828 		switch (tul_wait(sc, MSG_ACCEPT)) {
   1829 		case -1:
   1830 			return (-1);
   1831 		case PHASE_MSG_IN:
   1832 			bus_space_write_4(iot, ioh, TUL_STCNT0, 1);
   1833 			if ((tul_wait(sc, XF_FIFO_IN)) == -1)
   1834 				return (-1);
   1835 			break;
   1836 		default:
   1837 			goto abort;
   1838 		}
   1839 
   1840 		msg = bus_space_read_1(iot, ioh, TUL_SFIFO); /* Read Tag Msg */
   1841 
   1842 		if ((msg < MSG_SIMPLE_Q_TAG) || (msg > MSG_ORDERED_Q_TAG))
   1843 			goto abort;
   1844 
   1845 		switch (tul_wait(sc, MSG_ACCEPT)) {
   1846 		case -1:
   1847 			return (-1);
   1848 		case PHASE_MSG_IN:
   1849 			bus_space_write_4(iot, ioh, TUL_STCNT0, 1);
   1850 			if ((tul_wait(sc, XF_FIFO_IN)) == -1)
   1851 				return (-1);
   1852 			break;
   1853 		default:
   1854 			goto abort;
   1855 		}
   1856 
   1857 		tag  = bus_space_read_1(iot, ioh, TUL_SFIFO); /* Read Tag ID */
   1858 		scb = &sc->sc_scb[tag];
   1859 
   1860 		abortmsg = MSG_ABORT_TAG; /* Now that we have valdid tag! */
   1861 	}
   1862 
   1863 	if ((scb->target != target)
   1864 	    || (scb->lun != lun)
   1865 	    || (scb->status != STATUS_BUSY)) {
   1866  abort:
   1867 		tul_msgout_abort(sc, abortmsg);
   1868 		return (-1);
   1869 	}
   1870 
   1871 	sc->sc_actscb = scb;
   1872 
   1873 	if (tul_wait(sc, MSG_ACCEPT) == -1)
   1874 		return (-1);
   1875 
   1876 	return (tul_next_state(sc));
   1877 }
   1878 
   1879 static int
   1880 tul_msgin(sc)
   1881 	struct iha_softc *sc;
   1882 {
   1883 	bus_space_tag_t iot = sc->sc_iot;
   1884 	bus_space_handle_t ioh = sc->sc_ioh;
   1885 	int flags;
   1886 	int phase;
   1887 	u_int8_t msg;
   1888 
   1889 	for (;;) {
   1890 		if ((bus_space_read_1(iot, ioh, TUL_SFIFOCNT) & FIFOC) > 0)
   1891 			bus_space_write_1(iot, ioh, TUL_SCTRL0, RSFIFO);
   1892 
   1893 		bus_space_write_4(iot, ioh, TUL_STCNT0, 1);
   1894 
   1895 		phase = tul_wait(sc, XF_FIFO_IN);
   1896 		msg = bus_space_read_1(iot, ioh, TUL_SFIFO);
   1897 
   1898 		switch (msg) {
   1899 		case MSG_DISCONNECT:
   1900 			sc->sc_flags |= FLAG_EXPECT_DISC;
   1901 			if (tul_wait(sc, MSG_ACCEPT) != -1)
   1902 				tul_bad_seq(sc);
   1903 			phase = -1;
   1904 			break;
   1905 		case MSG_SAVEDATAPOINTER:
   1906 		case MSG_RESTOREPOINTERS:
   1907 		case MSG_NOOP:
   1908 			phase = tul_wait(sc, MSG_ACCEPT);
   1909 			break;
   1910 		case MSG_MESSAGE_REJECT:
   1911 			/* XXX - need to clear FIFO like other 'Clear ATN'?*/
   1912 			tul_set_ssig(sc, REQ | BSY | SEL | ATN, 0);
   1913 			flags = sc->sc_actscb->tcs->flags;
   1914 			if ((flags & FLAG_NO_NEG_SYNC) == 0)
   1915 				tul_set_ssig(sc, REQ | BSY | SEL, ATN);
   1916 			phase = tul_wait(sc, MSG_ACCEPT);
   1917 			break;
   1918 		case MSG_EXTENDED:
   1919 			phase = tul_msgin_extend(sc);
   1920 			break;
   1921 		case MSG_IGN_WIDE_RESIDUE:
   1922 			phase = tul_msgin_ignore_wid_resid(sc);
   1923 			break;
   1924 		case MSG_CMDCOMPLETE:
   1925 			sc->sc_flags |= FLAG_EXPECT_DONE_DISC;
   1926 			bus_space_write_1(iot, ioh, TUL_SCTRL0, RSFIFO);
   1927 			phase = tul_wait(sc, MSG_ACCEPT);
   1928 			if (phase != -1) {
   1929 				tul_bad_seq(sc);
   1930 				return (-1);
   1931 			}
   1932 			break;
   1933 		default:
   1934 			printf("[debug] tul_msgin: bad msg type: %d\n", msg);
   1935 			phase = tul_msgout_reject(sc);
   1936 			break;
   1937 		}
   1938 
   1939 		if (phase != PHASE_MSG_IN)
   1940 			return (phase);
   1941 	}
   1942 	/* NOTREACHED */
   1943 }
   1944 
   1945 static int
   1946 tul_msgin_ignore_wid_resid(sc)
   1947 	struct iha_softc *sc;
   1948 {
   1949 	bus_space_tag_t iot = sc->sc_iot;
   1950 	bus_space_handle_t ioh = sc->sc_ioh;
   1951 	int phase;
   1952 
   1953 	phase = tul_wait(sc, MSG_ACCEPT);
   1954 
   1955 	if (phase == PHASE_MSG_IN) {
   1956 		if (tul_wait(sc, XF_FIFO_IN) == -1)
   1957 			return (-1);
   1958 
   1959 		bus_space_write_1(iot, ioh, TUL_SFIFO, 0); /* put pad	     */
   1960 		bus_space_read_1(iot, ioh, TUL_SFIFO);	   /* get IGNORE     */
   1961 		bus_space_read_1(iot, ioh, TUL_SFIFO);	   /* get pad	     */
   1962 
   1963 		return (tul_wait(sc, MSG_ACCEPT));
   1964 	}
   1965 	else
   1966 		return (phase);
   1967 }
   1968 
   1969 static int
   1970 tul_msgin_extend(sc)
   1971 	struct iha_softc *sc;
   1972 {
   1973 	bus_space_tag_t iot = sc->sc_iot;
   1974 	bus_space_handle_t ioh = sc->sc_ioh;
   1975 	int flags, i, phase, msglen, msgcode;
   1976 
   1977 	/*
   1978 	 * XXX - can we just stop reading and reject, or do we have to
   1979 	 *	 read all input, discarding the excess, and then reject
   1980 	 */
   1981 	for (i = 0; i < IHA_MAX_EXTENDED_MSG; i++) {
   1982 		phase = tul_wait(sc, MSG_ACCEPT);
   1983 
   1984 		if (phase != PHASE_MSG_IN)
   1985 			return (phase);
   1986 
   1987 		bus_space_write_4(iot, ioh, TUL_STCNT0, 1);
   1988 
   1989 		if (tul_wait(sc, XF_FIFO_IN) == -1)
   1990 			return (-1);
   1991 
   1992 		sc->sc_msg[i] = bus_space_read_1(iot, ioh, TUL_SFIFO);
   1993 
   1994 		if (sc->sc_msg[0] == i)
   1995 			break;
   1996 	}
   1997 
   1998 	msglen	= sc->sc_msg[0];
   1999 	msgcode = sc->sc_msg[1];
   2000 
   2001 	if ((msglen == MSG_EXT_SDTR_LEN) && (msgcode == MSG_EXT_SDTR)) {
   2002 		if (tul_msgin_sync(sc) == 0) {
   2003 			tul_sync_done(sc);
   2004 			return (tul_wait(sc, MSG_ACCEPT));
   2005 		}
   2006 
   2007 		tul_set_ssig(sc, REQ | BSY | SEL, ATN);
   2008 
   2009 		phase = tul_wait(sc, MSG_ACCEPT);
   2010 		if (phase != PHASE_MSG_OUT)
   2011 			return (phase);
   2012 
   2013 		/* Clear FIFO for important message - final SYNC offer */
   2014 		bus_space_write_1(iot, ioh, TUL_SCTRL0, RSFIFO);
   2015 
   2016 		tul_sync_done(sc); /* This is our final offer */
   2017 
   2018 		bus_space_write_1(iot, ioh, TUL_SFIFO, MSG_EXTENDED);
   2019 		bus_space_write_1(iot, ioh, TUL_SFIFO, MSG_EXT_SDTR_LEN);
   2020 		bus_space_write_1(iot, ioh, TUL_SFIFO, MSG_EXT_SDTR);
   2021 		bus_space_write_1(iot, ioh, TUL_SFIFO, sc->sc_msg[2]);
   2022 		bus_space_write_1(iot, ioh, TUL_SFIFO, sc->sc_msg[3]);
   2023 
   2024 	} else if ((msglen == MSG_EXT_WDTR_LEN) && (msgcode == MSG_EXT_WDTR)) {
   2025 
   2026 		flags = sc->sc_actscb->tcs->flags;
   2027 
   2028 		if ((flags & FLAG_NO_WIDE) != 0)
   2029 			sc->sc_msg[2] = 0;	/* Offer async xfers only    */
   2030 
   2031 		else if (sc->sc_msg[2] > 2)	/* BAD MSG: 2 is max  value  */
   2032 			return (tul_msgout_reject(sc));
   2033 
   2034 		else if (sc->sc_msg[2] == 2)	/* a request for 32 bit xfers*/
   2035 			sc->sc_msg[2] = 1;	/* Offer 16 instead	     */
   2036 
   2037 		else {
   2038 			tul_wdtr_done(sc);
   2039 			if ((flags & FLAG_NO_NEG_SYNC) == 0)
   2040 				tul_set_ssig(sc, REQ | BSY | SEL, ATN);
   2041 			return (tul_wait(sc, MSG_ACCEPT));
   2042 		}
   2043 
   2044 		tul_set_ssig(sc, REQ | BSY | SEL, ATN);
   2045 
   2046 		phase = tul_wait(sc, MSG_ACCEPT);
   2047 		if (phase != PHASE_MSG_OUT)
   2048 			return (phase);
   2049 
   2050 		/* WDTR msg out */
   2051 		bus_space_write_1(iot, ioh, TUL_SFIFO, MSG_EXTENDED);
   2052 		bus_space_write_1(iot, ioh, TUL_SFIFO, MSG_EXT_WDTR_LEN);
   2053 		bus_space_write_1(iot, ioh, TUL_SFIFO, MSG_EXT_WDTR);
   2054 		bus_space_write_1(iot, ioh, TUL_SFIFO, sc->sc_msg[2]);
   2055 
   2056 	} else
   2057 		return (tul_msgout_reject(sc));
   2058 
   2059 	return (tul_wait(sc, XF_FIFO_OUT));
   2060 }
   2061 
   2062 /*
   2063  * tul_msgin_sync - check SDTR msg in sc_msg. If the offer is
   2064  *		    acceptable leave sc_msg as is and return 0.
   2065  *		    If the negotiation must continue, modify sc_msg
   2066  *		    as needed and return 1. Else return 0.
   2067  */
   2068 static int
   2069 tul_msgin_sync(sc)
   2070 	struct iha_softc *sc;
   2071 {
   2072 	int flags;
   2073 	int newoffer;
   2074 	u_int8_t default_period;
   2075 
   2076 	flags = sc->sc_actscb->tcs->flags;
   2077 
   2078 	default_period = tul_rate_tbl[flags & FLAG_SCSI_RATE];
   2079 
   2080 	if (sc->sc_msg[3] == 0) /* target offered async only. Accept it. */
   2081 		return (0);
   2082 
   2083 	newoffer = 0;
   2084 
   2085 	if ((flags & FLAG_NO_SYNC) != 0) {
   2086 		sc->sc_msg[3] = 0;
   2087 		newoffer   = 1;
   2088 	}
   2089 
   2090 	if (sc->sc_msg[3] > IHA_MAX_OFFSET) {
   2091 		sc->sc_msg[3] = IHA_MAX_OFFSET;
   2092 		newoffer   = 1;
   2093 	}
   2094 
   2095 	if (sc->sc_msg[2] < default_period) {
   2096 		sc->sc_msg[2] = default_period;
   2097 		newoffer   = 1;
   2098 	}
   2099 
   2100 	if (sc->sc_msg[2] >= 59) { /* XXX magic */
   2101 		sc->sc_msg[3] = 0;
   2102 		newoffer   = 1;
   2103 	}
   2104 
   2105 	return (newoffer);
   2106 }
   2107 
   2108 static int
   2109 tul_msgout(sc, msg)
   2110 	struct iha_softc *sc;
   2111 	u_int8_t msg;
   2112 {
   2113 
   2114 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, TUL_SFIFO, msg);
   2115 
   2116 	return (tul_wait(sc, XF_FIFO_OUT));
   2117 }
   2118 
   2119 static void
   2120 tul_msgout_abort(sc, aborttype)
   2121 	struct iha_softc *sc;
   2122 	u_int8_t aborttype;
   2123 {
   2124 	bus_space_tag_t iot = sc->sc_iot;
   2125 	bus_space_handle_t ioh = sc->sc_ioh;
   2126 
   2127 	tul_set_ssig(sc, REQ | BSY | SEL, ATN);
   2128 
   2129 	switch (tul_wait(sc, MSG_ACCEPT)) {
   2130 	case -1:
   2131 		break;
   2132 
   2133 	case PHASE_MSG_OUT:
   2134 		bus_space_write_1(iot, ioh, TUL_SFIFO, aborttype);
   2135 
   2136 		sc->sc_flags |= FLAG_EXPECT_DISC;
   2137 
   2138 		if (tul_wait(sc, XF_FIFO_OUT) != -1)
   2139 			tul_bad_seq(sc);
   2140 		break;
   2141 
   2142 	default:
   2143 		tul_bad_seq(sc);
   2144 		break;
   2145 	}
   2146 }
   2147 
   2148 static int
   2149 tul_msgout_reject(sc)
   2150 	struct iha_softc *sc;
   2151 {
   2152 	bus_space_tag_t iot = sc->sc_iot;
   2153 	bus_space_handle_t ioh = sc->sc_ioh;
   2154 	int phase;
   2155 
   2156 	tul_set_ssig(sc, REQ | BSY | SEL, ATN);
   2157 
   2158 	if ((phase = tul_wait(sc, MSG_ACCEPT)) == -1)
   2159 		return (-1);
   2160 
   2161 	if (phase == PHASE_MSG_OUT) {
   2162 		bus_space_write_1(iot, ioh, TUL_SFIFO, MSG_MESSAGE_REJECT);
   2163 		return (tul_wait(sc, XF_FIFO_OUT));
   2164 	}
   2165 
   2166 	return (phase);
   2167 }
   2168 
   2169 static int
   2170 tul_msgout_wide(sc)
   2171 	struct iha_softc *sc;
   2172 {
   2173 	bus_space_tag_t iot = sc->sc_iot;
   2174 	bus_space_handle_t ioh = sc->sc_ioh;
   2175 	int phase;
   2176 
   2177 	sc->sc_actscb->tcs->flags |= FLAG_WIDE_DONE;
   2178 
   2179 	bus_space_write_1(iot, ioh, TUL_SFIFO, MSG_EXTENDED);
   2180 	bus_space_write_1(iot, ioh, TUL_SFIFO, MSG_EXT_WDTR_LEN);
   2181 	bus_space_write_1(iot, ioh, TUL_SFIFO, MSG_EXT_WDTR);
   2182 	bus_space_write_1(iot, ioh, TUL_SFIFO, MSG_EXT_WDTR_BUS_16_BIT);
   2183 
   2184 	phase = tul_wait(sc, XF_FIFO_OUT);
   2185 
   2186 	bus_space_write_1(iot, ioh, TUL_SCTRL0, RSFIFO);
   2187 	tul_set_ssig(sc, REQ | BSY | SEL | ATN, 0);
   2188 
   2189 	return (phase);
   2190 }
   2191 
   2192 static int
   2193 tul_msgout_sync(sc)
   2194 	struct iha_softc *sc;
   2195 {
   2196 	bus_space_tag_t iot = sc->sc_iot;
   2197 	bus_space_handle_t ioh = sc->sc_ioh;
   2198 	int rateindex;
   2199 	int phase;
   2200 	u_int8_t sync_rate;
   2201 
   2202 	rateindex = sc->sc_actscb->tcs->flags & FLAG_SCSI_RATE;
   2203 
   2204 	sync_rate = tul_rate_tbl[rateindex];
   2205 
   2206 	bus_space_write_1(iot, ioh, TUL_SFIFO, MSG_EXTENDED);
   2207 	bus_space_write_1(iot, ioh, TUL_SFIFO, MSG_EXT_SDTR_LEN);
   2208 	bus_space_write_1(iot, ioh, TUL_SFIFO, MSG_EXT_SDTR);
   2209 	bus_space_write_1(iot, ioh, TUL_SFIFO, sync_rate);
   2210 	bus_space_write_1(iot, ioh, TUL_SFIFO, IHA_MAX_OFFSET);/* REQ/ACK*/
   2211 
   2212 	phase = tul_wait(sc, XF_FIFO_OUT);
   2213 
   2214 	bus_space_write_1(iot, ioh, TUL_SCTRL0, RSFIFO);
   2215 	tul_set_ssig(sc, REQ | BSY | SEL | ATN, 0);
   2216 
   2217 	return (phase);
   2218 }
   2219 
   2220 static void
   2221 tul_wdtr_done(sc)
   2222 	struct iha_softc *sc;
   2223 {
   2224 	bus_space_tag_t iot = sc->sc_iot;
   2225 	bus_space_handle_t ioh = sc->sc_ioh;
   2226 	struct tcs *tcs = sc->sc_actscb->tcs;
   2227 
   2228 	tcs->syncm = 0;
   2229 	tcs->period = 0;
   2230 	tcs->offset = 0;
   2231 
   2232 	if (sc->sc_msg[2] != 0)
   2233 		tcs->syncm |= PERIOD_WIDE_SCSI;
   2234 
   2235 	tcs->sconfig0 &= ~ALTPD;
   2236 	tcs->flags &= ~FLAG_SYNC_DONE;
   2237 	tcs->flags |=  FLAG_WIDE_DONE;
   2238 
   2239 	bus_space_write_1(iot, ioh, TUL_SCONFIG0, tcs->sconfig0);
   2240 	bus_space_write_1(iot, ioh, TUL_SYNCM, tcs->syncm);
   2241 }
   2242 
   2243 static void
   2244 tul_sync_done(sc)
   2245 	struct iha_softc *sc;
   2246 {
   2247 	bus_space_tag_t iot = sc->sc_iot;
   2248 	bus_space_handle_t ioh = sc->sc_ioh;
   2249 	struct tcs *tcs = sc->sc_actscb->tcs;
   2250 	int i;
   2251 
   2252 	if ((tcs->flags & FLAG_SYNC_DONE) == 0) {
   2253 		tcs->period = sc->sc_msg[2];
   2254 		tcs->offset = sc->sc_msg[3];
   2255 		if (tcs->offset != 0) {
   2256 			tcs->syncm |= tcs->offset;
   2257 
   2258 			/* pick the highest possible rate */
   2259 			for (i = 0; i < 8; i++)
   2260 				if (tul_rate_tbl[i] >= tcs->period)
   2261 					break;
   2262 
   2263 			tcs->syncm |= (i << 4);
   2264 			tcs->sconfig0 |= ALTPD;
   2265 		}
   2266 
   2267 		tcs->flags |= FLAG_SYNC_DONE;
   2268 
   2269 		bus_space_write_1(iot, ioh, TUL_SCONFIG0, tcs->sconfig0);
   2270 		bus_space_write_1(iot, ioh, TUL_SYNCM, tcs->syncm);
   2271 	}
   2272 }
   2273 
   2274 void
   2275 tul_reset_chip(sc)
   2276 	struct iha_softc *sc;
   2277 {
   2278 	bus_space_tag_t iot = sc->sc_iot;
   2279 	bus_space_handle_t ioh = sc->sc_ioh;
   2280 
   2281 	/* reset tulip chip */
   2282 
   2283 	bus_space_write_1(iot, ioh, TUL_SCTRL0, RSCSI);
   2284 
   2285 	do {
   2286 		sc->sc_sistat = bus_space_read_1(iot, ioh, TUL_SISTAT);
   2287 	} while ((sc->sc_sistat & SRSTD) == 0);
   2288 
   2289 	tul_set_ssig(sc, 0, 0);
   2290 
   2291 	bus_space_read_1(iot, ioh, TUL_SISTAT); /* Clear any active interrupt*/
   2292 }
   2293 
   2294 static void
   2295 tul_select(sc, scb, select_type)
   2296 	struct iha_softc *sc;
   2297 	struct iha_scsi_req_q *scb;
   2298 	u_int8_t select_type;
   2299 {
   2300 	bus_space_tag_t iot = sc->sc_iot;
   2301 	bus_space_handle_t ioh = sc->sc_ioh;
   2302 
   2303 	switch (select_type) {
   2304 	case SEL_ATN:
   2305 		bus_space_write_1(iot, ioh, TUL_SFIFO, scb->scb_id);
   2306 		bus_space_write_multi_1(iot, ioh, TUL_SFIFO,
   2307 		    scb->cmd, scb->cmdlen);
   2308 
   2309 		scb->nextstat = 2;
   2310 		break;
   2311 
   2312 	case SELATNSTOP:
   2313 		scb->nextstat = 1;
   2314 		break;
   2315 
   2316 	case SEL_ATN3:
   2317 		bus_space_write_1(iot, ioh, TUL_SFIFO, scb->scb_id);
   2318 		bus_space_write_1(iot, ioh, TUL_SFIFO, scb->scb_tagmsg);
   2319 		bus_space_write_1(iot, ioh, TUL_SFIFO, scb->scb_tagid);
   2320 
   2321 		bus_space_write_multi_1(iot, ioh, TUL_SFIFO, scb->cmd,
   2322 		    scb->cmdlen);
   2323 
   2324 		scb->nextstat = 2;
   2325 		break;
   2326 
   2327 	default:
   2328 		printf("[debug] tul_select() - unknown select type = 0x%02x\n",
   2329 		    select_type);
   2330 		return;
   2331 	}
   2332 
   2333 	tul_del_pend_scb(sc, scb);
   2334 	scb->status = STATUS_SELECT;
   2335 
   2336 	sc->sc_actscb = scb;
   2337 
   2338 	bus_space_write_1(iot, ioh, TUL_SCMD, select_type);
   2339 }
   2340 
   2341 /*
   2342  * tul_wait - wait for an interrupt to service or a SCSI bus phase change
   2343  *            after writing the supplied command to the tulip chip. If
   2344  *            the command is NO_OP, skip the command writing.
   2345  */
   2346 static int
   2347 tul_wait(sc, cmd)
   2348 	struct iha_softc *sc;
   2349 	u_int8_t cmd;
   2350 {
   2351 	bus_space_tag_t iot = sc->sc_iot;
   2352 	bus_space_handle_t ioh = sc->sc_ioh;
   2353 
   2354 	if (cmd != NO_OP)
   2355 		bus_space_write_1(iot, ioh, TUL_SCMD, cmd);
   2356 
   2357 	/*
   2358 	 * Have to do this here, in addition to in iha_isr, because
   2359 	 * interrupts might be turned off when we get here.
   2360 	 */
   2361 	do {
   2362 		sc->sc_status0 = bus_space_read_1(iot, ioh, TUL_STAT0);
   2363 	} while ((sc->sc_status0 & INTPD) == 0);
   2364 
   2365 	sc->sc_status1 = bus_space_read_1(iot, ioh, TUL_STAT1);
   2366 	sc->sc_sistat = bus_space_read_1(iot, ioh, TUL_SISTAT);
   2367 
   2368 	sc->sc_phase = sc->sc_status0 & PH_MASK;
   2369 
   2370 	if ((sc->sc_sistat & SRSTD) != 0) {
   2371 		/* SCSI bus reset interrupt */
   2372 		tul_reset_scsi_bus(sc);
   2373 		return (-1);
   2374 	}
   2375 
   2376 	if ((sc->sc_sistat & RSELED) != 0)
   2377 		/* Reselection interrupt */
   2378 		return (tul_resel(sc));
   2379 
   2380 	if ((sc->sc_sistat & STIMEO) != 0) {
   2381 		/* selected/reselected timeout interrupt */
   2382 		tul_busfree(sc);
   2383 		return (-1);
   2384 	}
   2385 
   2386 	if ((sc->sc_sistat & DISCD) != 0) {
   2387 		/* BUS disconnection interrupt */
   2388 		if ((sc->sc_flags & FLAG_EXPECT_DONE_DISC) != 0) {
   2389 			bus_space_write_1(iot, ioh, TUL_SCTRL0, RSFIFO);
   2390 			bus_space_write_1(iot, ioh, TUL_SCONFIG0,
   2391 			    SCONFIG0DEFAULT);
   2392 			bus_space_write_1(iot, ioh, TUL_SCTRL1, EHRSL);
   2393 			tul_append_done_scb(sc, sc->sc_actscb, HOST_OK);
   2394 			sc->sc_flags &= ~FLAG_EXPECT_DONE_DISC;
   2395 
   2396 		} else if ((sc->sc_flags & FLAG_EXPECT_DISC) != 0) {
   2397 			bus_space_write_1(iot, ioh, TUL_SCTRL0, RSFIFO);
   2398 			bus_space_write_1(iot, ioh, TUL_SCONFIG0,
   2399 			    SCONFIG0DEFAULT);
   2400 			bus_space_write_1(iot, ioh, TUL_SCTRL1, EHRSL);
   2401 			sc->sc_actscb = NULL;
   2402 			sc->sc_flags &= ~FLAG_EXPECT_DISC;
   2403 
   2404 		} else
   2405 			tul_busfree(sc);
   2406 
   2407 		return (-1);
   2408 	}
   2409 
   2410 	return (sc->sc_phase);
   2411 }
   2412 
   2413 /*
   2414  * tul_done_scb - We have a scb which has been processed by the
   2415  *                adaptor, now we look to see how the operation went.
   2416  */
   2417 static void
   2418 tul_done_scb(sc, scb)
   2419 	struct iha_softc *sc;
   2420 	struct iha_scsi_req_q *scb;
   2421 {
   2422 	struct scsipi_xfer *xs = scb->xs;
   2423 
   2424 	if (xs != NULL) {
   2425 		if (xs->datalen > 0) {
   2426 			bus_dmamap_sync(sc->sc_dmat, scb->dmap,
   2427 			    0, scb->dmap->dm_mapsize,
   2428 			    (xs->xs_control & XS_CTL_DATA_IN) ?
   2429 			    BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
   2430 			bus_dmamap_unload(sc->sc_dmat, scb->dmap);
   2431 		}
   2432 
   2433 		xs->status = scb->ta_stat;
   2434 
   2435 		switch (scb->ha_stat) {
   2436 		case HOST_OK:
   2437 			switch (scb->ta_stat) {
   2438 			case SCSI_OK:
   2439 			case SCSI_CONDITION_MET:
   2440 			case SCSI_INTERM:
   2441 			case SCSI_INTERM_COND_MET:
   2442 				xs->resid = scb->buflen;
   2443 				xs->error = XS_NOERROR;
   2444 				if ((scb->flags & FLAG_RSENS) != 0)
   2445 					xs->error = XS_SENSE;
   2446 				break;
   2447 
   2448 			case SCSI_RESV_CONFLICT:
   2449 			case SCSI_BUSY:
   2450 			case SCSI_QUEUE_FULL:
   2451 				xs->error = XS_BUSY;
   2452 				break;
   2453 
   2454 			case SCSI_TERMINATED:
   2455 			case SCSI_ACA_ACTIVE:
   2456 			case SCSI_CHECK:
   2457 				scb->tcs->flags &=
   2458 				    ~(FLAG_SYNC_DONE | FLAG_WIDE_DONE);
   2459 
   2460 				if ((scb->flags & FLAG_RSENS) != 0 ||
   2461 				    tul_push_sense_request(sc, scb) != 0) {
   2462 					scb->flags &= FLAG_RSENS;
   2463 					printf("%s: request sense failed\n",
   2464 					    sc->sc_dev.dv_xname);
   2465 					xs->error = XS_DRIVER_STUFFUP;
   2466 					break;
   2467 				}
   2468 
   2469 				xs->error = XS_SENSE;
   2470 				return;
   2471 
   2472 			default:
   2473 				xs->error = XS_DRIVER_STUFFUP;
   2474 				break;
   2475 			}
   2476 			break;
   2477 
   2478 		case HOST_SEL_TOUT:
   2479 			xs->error = XS_SELTIMEOUT;
   2480 			break;
   2481 
   2482 		case HOST_SCSI_RST:
   2483 		case HOST_DEV_RST:
   2484 			xs->error = XS_RESET;
   2485 			break;
   2486 
   2487 		case HOST_SPERR:
   2488 			printf("%s: SCSI Parity error detected\n",
   2489 			    sc->sc_dev.dv_xname);
   2490 			xs->error = XS_DRIVER_STUFFUP;
   2491 			break;
   2492 
   2493 		case HOST_TIMED_OUT:
   2494 			xs->error = XS_TIMEOUT;
   2495 			break;
   2496 
   2497 		case HOST_DO_DU:
   2498 		case HOST_BAD_PHAS:
   2499 		default:
   2500 			xs->error = XS_DRIVER_STUFFUP;
   2501 			break;
   2502 		}
   2503 
   2504 		scsipi_done(xs);
   2505 	}
   2506 
   2507 	tul_append_free_scb(sc, scb);
   2508 }
   2509 
   2510 static void
   2511 tul_timeout(arg)
   2512 	void *arg;
   2513 {
   2514 	struct iha_scsi_req_q *scb = (struct iha_scsi_req_q *)arg;
   2515 	struct scsipi_xfer *xs = scb->xs;
   2516 	struct scsipi_periph *periph = xs->xs_periph;
   2517 	struct iha_softc *sc;
   2518 
   2519 	sc = (void *)periph->periph_channel->chan_adapter->adapt_dev;
   2520 
   2521 	if (xs == NULL)
   2522 		printf("[debug] tul_timeout called with xs == NULL\n");
   2523 
   2524 	else {
   2525 		scsipi_printaddr(periph);
   2526 		printf("SCSI OpCode 0x%02x timed out\n", xs->cmd->opcode);
   2527 
   2528 		tul_abort_xs(sc, xs, HOST_TIMED_OUT);
   2529 	}
   2530 }
   2531 
   2532 static void
   2533 tul_exec_scb(sc, scb)
   2534 	struct iha_softc *sc;
   2535 	struct iha_scsi_req_q *scb;
   2536 {
   2537 	bus_space_tag_t iot;
   2538 	bus_space_handle_t ioh;
   2539 	bus_dmamap_t dm;
   2540 	struct scsipi_xfer *xs = scb->xs;
   2541 	int nseg, s;
   2542 
   2543 	dm = scb->dmap;
   2544 	nseg = dm->dm_nsegs;
   2545 
   2546 	if (nseg > 1) {
   2547 		struct iha_sg_element *sg = scb->sglist;
   2548 		int i;
   2549 
   2550 		for (i = 0; i < nseg; i++) {
   2551 			sg[i].sg_len = htole32(dm->dm_segs[i].ds_len);
   2552 			sg[i].sg_addr = htole32(dm->dm_segs[i].ds_addr);
   2553 		}
   2554 		bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap,
   2555 		    scb->sgoffset, IHA_SG_SIZE,
   2556 		    BUS_DMASYNC_PREWRITE);
   2557 
   2558 		scb->flags |= FLAG_SG; /* XXX */
   2559 		scb->sg_size = scb->sg_max = nseg;
   2560 
   2561 		scb->bufaddr = scb->sg_addr;
   2562 	} else
   2563 		scb->bufaddr = dm->dm_segs[0].ds_addr;
   2564 
   2565 	if ((xs->xs_control & XS_CTL_POLL) == 0) {
   2566 		int timeout = xs->timeout;
   2567 		timeout = (timeout > 100000) ?
   2568 		    timeout / 1000 * hz : timeout * hz / 1000;
   2569 		if (timeout == 0)
   2570 			timeout = 1;
   2571 		callout_reset(&xs->xs_callout, timeout, tul_timeout, scb);
   2572 	}
   2573 
   2574 	s = splbio();
   2575 
   2576 	if (((scb->flags & XS_RESET) != 0) || (scb->cmd[0] == REQUEST_SENSE))
   2577 		tul_push_pend_scb(sc, scb);   /* Insert SCB at head of Pend */
   2578 	else
   2579 		tul_append_pend_scb(sc, scb); /* Append SCB to tail of Pend */
   2580 
   2581 	/*
   2582 	 * Run through tul_main() to ensure something is active, if
   2583 	 * only this new SCB.
   2584 	 */
   2585 	if (sc->sc_semaph != SEMAPH_IN_MAIN) {
   2586 		iot = sc->sc_iot;
   2587 		ioh = sc->sc_ioh;
   2588 
   2589 		bus_space_write_1(iot, ioh, TUL_IMSK, MASK_ALL);
   2590 		sc->sc_semaph = SEMAPH_IN_MAIN;;
   2591 
   2592 		splx(s);
   2593 		tul_main(sc);
   2594 		s = splbio();
   2595 
   2596 		sc->sc_semaph = ~SEMAPH_IN_MAIN;;
   2597 		bus_space_write_1(iot, ioh, TUL_IMSK, (MASK_ALL & ~MSCMP));
   2598 	}
   2599 
   2600 	splx(s);
   2601 }
   2602 
   2603 
   2604 /*
   2605  * tul_set_ssig - read the current scsi signal mask, then write a new
   2606  *		  one which turns off/on the specified signals.
   2607  */
   2608 static void
   2609 tul_set_ssig(sc, offsigs, onsigs)
   2610 	struct iha_softc *sc;
   2611 	u_int8_t offsigs, onsigs;
   2612 {
   2613 	bus_space_tag_t iot = sc->sc_iot;
   2614 	bus_space_handle_t ioh = sc->sc_ioh;
   2615 	u_int8_t currsigs;
   2616 
   2617 	currsigs = bus_space_read_1(iot, ioh, TUL_SSIGI);
   2618 	bus_space_write_1(iot, ioh, TUL_SSIGO, (currsigs & ~offsigs) | onsigs);
   2619 }
   2620 
   2621 /*
   2622  * tul_alloc_sglist - allocate and map sglist for SCB's
   2623  */
   2624 static int
   2625 tul_alloc_sglist(sc)
   2626 	struct iha_softc *sc;
   2627 {
   2628 	bus_dma_segment_t seg;
   2629 	int error, rseg;
   2630 
   2631 	/*
   2632 	 * Allocate dma-safe memory for the SCB's sglist
   2633 	 */
   2634 	if ((error = bus_dmamem_alloc(sc->sc_dmat,
   2635 	    IHA_SG_SIZE * IHA_MAX_SCB,
   2636 	    PAGE_SIZE, 0, &seg, 1, &rseg, BUS_DMA_NOWAIT)) != 0) {
   2637 		printf(": unable to allocate sglist, error = %d\n", error);
   2638 		return (error);
   2639 	}
   2640 	if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
   2641 	    IHA_SG_SIZE * IHA_MAX_SCB, (caddr_t *)&sc->sc_sglist,
   2642 	    BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) != 0) {
   2643 		printf(": unable to map sglist, error = %d\n", error);
   2644 		return (error);
   2645 	}
   2646 
   2647 	/*
   2648 	 * Create and load the DMA map used for the SCBs
   2649 	 */
   2650 	if ((error = bus_dmamap_create(sc->sc_dmat,
   2651 	    IHA_SG_SIZE * IHA_MAX_SCB, 1, IHA_SG_SIZE * IHA_MAX_SCB,
   2652 	    0, BUS_DMA_NOWAIT, &sc->sc_dmamap)) != 0) {
   2653 		printf(": unable to create control DMA map, error = %d\n",
   2654 		    error);
   2655 		return (error);
   2656 	}
   2657 	if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap,
   2658 	    sc->sc_sglist, IHA_SG_SIZE * IHA_MAX_SCB,
   2659 	    NULL, BUS_DMA_NOWAIT)) != 0) {
   2660 		printf(": unable to load control DMA map, error = %d\n", error);
   2661 		return (error);
   2662 	}
   2663 
   2664 	bzero(sc->sc_sglist, IHA_SG_SIZE * IHA_MAX_SCB);
   2665 
   2666 	return (0);
   2667 }
   2668 
   2669 /*
   2670  * tul_read_eeprom - read Serial EEPROM value & set to defaults
   2671  *		     if required. XXX - Writing does NOT work!
   2672  */
   2673 void
   2674 tul_read_eeprom(sc, eeprom)
   2675 	struct iha_softc *sc;
   2676 	struct iha_eeprom *eeprom;
   2677 {
   2678 	bus_space_tag_t iot = sc->sc_iot;
   2679 	bus_space_handle_t ioh = sc->sc_ioh;
   2680 	u_int16_t *buf = (u_int16_t *)eeprom;
   2681 	u_int8_t gctrl;
   2682 
   2683 	/*------Enable EEProm programming ---*/
   2684 	gctrl = bus_space_read_1(iot, ioh, TUL_GCTRL0) | EEPRG;
   2685 	bus_space_write_1(iot, ioh, TUL_GCTRL0, gctrl);
   2686 
   2687 	/*------ Program default pattern ----*/
   2688 	if (tul_se2_rd_all(sc, buf) == 0) {
   2689 		tul_se2_update_all(sc);
   2690 		if(tul_se2_rd_all(sc, buf)  == 0)
   2691 			panic("could not program iha Tulip EEPROM\n");
   2692 	}
   2693 
   2694 	/*------ Disable EEProm programming ---*/
   2695 	gctrl = bus_space_read_1(iot, ioh, TUL_GCTRL0) & ~EEPRG;
   2696 	bus_space_write_1(iot, ioh, TUL_GCTRL0, gctrl);
   2697 }
   2698 
   2699 /*
   2700  * tul_se2_update_all - Update SCSI H/A configuration parameters from
   2701  *			serial EEPROM Setup default pattern. Only
   2702  *			change those values different from the values
   2703  *			in tul_nvram.
   2704  */
   2705 void
   2706 tul_se2_update_all(sc)
   2707 	struct iha_softc *sc;
   2708 {
   2709 	bus_space_tag_t iot = sc->sc_iot;
   2710 	bus_space_handle_t ioh = sc->sc_ioh;
   2711 	u_int16_t *np;
   2712 	u_int32_t chksum;
   2713 	int i;
   2714 
   2715 	/* Enable erase/write state of EEPROM */
   2716 	tul_se2_instr(sc, ENABLE_ERASE);
   2717 	bus_space_write_1(iot, ioh, TUL_NVRAM, 0);
   2718 	EEP_WAIT();
   2719 
   2720 	np = (u_int16_t *)&eeprom_default;
   2721 
   2722 	for (i = 0, chksum = 0; i < EEPROM_SIZE - 1; i++) {
   2723 		tul_se2_wr(sc, i, *np);
   2724 		chksum += *np++;
   2725 	}
   2726 
   2727 	chksum &= 0x0000ffff;
   2728 	tul_se2_wr(sc, 31, chksum);
   2729 
   2730 	/* Disable erase/write state of EEPROM */
   2731 	tul_se2_instr(sc, 0);
   2732 	bus_space_write_1(iot, ioh, TUL_NVRAM, 0);
   2733 	EEP_WAIT();
   2734 }
   2735 
   2736 /*
   2737  * tul_se2_wr - write the given 16 bit value into the Serial EEPROM
   2738  *		at the specified offset
   2739  */
   2740 void
   2741 tul_se2_wr(sc, addr, writeword)
   2742 	struct iha_softc *sc;
   2743 	int addr;
   2744 	u_int16_t writeword;
   2745 {
   2746 	bus_space_tag_t iot = sc->sc_iot;
   2747 	bus_space_handle_t ioh = sc->sc_ioh;
   2748 	int i, bit;
   2749 
   2750 	/* send 'WRITE' Instruction == address | WRITE bit */
   2751 	tul_se2_instr(sc, addr | WRITE);
   2752 
   2753 	for (i = 16; i > 0; i--) {
   2754 		if (writeword & (1 << (i - 1)))
   2755 			bus_space_write_1(iot, ioh, TUL_NVRAM, NVRCS | NVRDO);
   2756 		else
   2757 			bus_space_write_1(iot, ioh, TUL_NVRAM, NVRCS);
   2758 		EEP_WAIT();
   2759 		bus_space_write_1(iot, ioh, TUL_NVRAM, NVRCS | NVRCK);
   2760 		EEP_WAIT();
   2761 	}
   2762 
   2763 	bus_space_write_1(iot, ioh, TUL_NVRAM, NVRCS);
   2764 	EEP_WAIT();
   2765 	bus_space_write_1(iot, ioh, TUL_NVRAM, 0);
   2766 	EEP_WAIT();
   2767 	bus_space_write_1(iot, ioh, TUL_NVRAM, NVRCS);
   2768 	EEP_WAIT();
   2769 
   2770 	for (;;) {
   2771 		bus_space_write_1(iot, ioh, TUL_NVRAM, NVRCS | NVRCK);
   2772 		EEP_WAIT();
   2773 		bus_space_write_1(iot, ioh, TUL_NVRAM, NVRCS);
   2774 		EEP_WAIT();
   2775 		bit = bus_space_read_1(iot, ioh, TUL_NVRAM) & NVRDI;
   2776 		EEP_WAIT();
   2777 		if (bit != 0)
   2778 			break; /* write complete */
   2779 	}
   2780 
   2781 	bus_space_write_1(iot, ioh, TUL_NVRAM, 0);
   2782 }
   2783 
   2784 /*
   2785  * tul_se2_rd - read & return the 16 bit value at the specified
   2786  *		offset in the Serial E2PROM
   2787  *
   2788  */
   2789 u_int16_t
   2790 tul_se2_rd(sc, addr)
   2791 	struct iha_softc *sc;
   2792 	int addr;
   2793 {
   2794 	bus_space_tag_t iot = sc->sc_iot;
   2795 	bus_space_handle_t ioh = sc->sc_ioh;
   2796 	int i, bit;
   2797 	u_int16_t readword;
   2798 
   2799 	/* Send 'READ' instruction == address | READ bit */
   2800 	tul_se2_instr(sc, addr | READ);
   2801 
   2802 	readword = 0;
   2803 	for (i = 16; i > 0; i--) {
   2804 		bus_space_write_1(iot, ioh, TUL_NVRAM, NVRCS | NVRCK);
   2805 		EEP_WAIT();
   2806 		bus_space_write_1(iot, ioh, TUL_NVRAM, NVRCS);
   2807 		EEP_WAIT();
   2808 		/* sample data after the following edge of clock     */
   2809 		bit = bus_space_read_1(iot, ioh, TUL_NVRAM) & NVRDI ? 1 : 0;
   2810 		EEP_WAIT();
   2811 
   2812 		readword |= bit << (i - 1);
   2813 	}
   2814 
   2815 	bus_space_write_1(iot, ioh, TUL_NVRAM, 0);
   2816 
   2817 	return (readword);
   2818 }
   2819 
   2820 /*
   2821  * tul_se2_rd_all - Read SCSI H/A config parameters from serial EEPROM
   2822  */
   2823 int
   2824 tul_se2_rd_all(sc, buf)
   2825 	struct iha_softc *sc;
   2826 	u_int16_t *buf;
   2827 {
   2828 	struct iha_eeprom *eeprom = (struct iha_eeprom *)buf;
   2829 	u_int32_t chksum;
   2830 	int i;
   2831 
   2832 	for (i = 0, chksum = 0; i < EEPROM_SIZE - 1; i++) {
   2833 		*buf = tul_se2_rd(sc, i);
   2834 		chksum += *buf++;
   2835 	}
   2836 	*buf = tul_se2_rd(sc, 31); /* just read checksum		    */
   2837 
   2838 	chksum &= 0x0000ffff;		/* checksum is lower 16 bits of sum */
   2839 
   2840 	return (eeprom->signature == EEP_SIGNATURE) &&
   2841 	    (eeprom->checksum == chksum);
   2842 }
   2843 
   2844 /*
   2845  * tul_se2_instr - write an octet to serial E2PROM one bit at a time
   2846  */
   2847 void
   2848 tul_se2_instr(sc, instr)
   2849 	struct iha_softc *sc;
   2850 	int instr;
   2851 {
   2852 	bus_space_tag_t iot = sc->sc_iot;
   2853 	bus_space_handle_t ioh = sc->sc_ioh;
   2854 	int b, i;
   2855 
   2856 	b = NVRCS | NVRDO; /* Write the start bit (== 1) */
   2857 
   2858 	bus_space_write_1(iot, ioh, TUL_NVRAM, b);
   2859 	EEP_WAIT();
   2860 	bus_space_write_1(iot, ioh, TUL_NVRAM, b | NVRCK);
   2861 	EEP_WAIT();
   2862 
   2863 	for (i = 8; i > 0; i--) {
   2864 		if (instr & (1 << (i - 1)))
   2865 			b = NVRCS | NVRDO; /* Write a 1 bit */
   2866 		else
   2867 			b = NVRCS;	   /* Write a 0 bit */
   2868 
   2869 		bus_space_write_1(iot, ioh, TUL_NVRAM, b);
   2870 		EEP_WAIT();
   2871 		bus_space_write_1(iot, ioh, TUL_NVRAM, b | NVRCK);
   2872 		EEP_WAIT();
   2873 	}
   2874 
   2875 	bus_space_write_1(iot, ioh, TUL_NVRAM, NVRCS);
   2876 }
   2877 
   2878 /*
   2879  * tul_reset_tcs - reset the target control structure pointed
   2880  *		   to by tcs to default values. tcs flags
   2881  *		   only has the negotiation done bits reset as
   2882  *		   the other bits are fixed at initialization.
   2883  */
   2884 void
   2885 tul_reset_tcs(tcs, config0)
   2886 	struct tcs *tcs;
   2887 	u_int8_t config0;
   2888 {
   2889 
   2890 	tcs->flags &= ~(FLAG_SYNC_DONE | FLAG_WIDE_DONE);
   2891 	tcs->period = 0;
   2892 	tcs->offset = 0;
   2893 	tcs->tagcnt = 0;
   2894 	tcs->ntagscb  = NULL;
   2895 	tcs->syncm = 0;
   2896 	tcs->sconfig0 = config0;
   2897 }
   2898