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