Home | History | Annotate | Line # | Download | only in ic
siop.c revision 1.37
      1 /*	$NetBSD: siop.c,v 1.37 2000/11/14 18:21:02 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2000 Manuel Bouyer.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. All advertising materials mentioning features or use of this software
     15  *    must display the following acknowledgement:
     16  *	This product includes software developed by Manuel Bouyer
     17  * 4. The name of the author may not be used to endorse or promote products
     18  *    derived from this software without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  *
     31  */
     32 
     33 /* SYM53c7/8xx PCI-SCSI I/O Processors driver */
     34 
     35 #include <sys/param.h>
     36 #include <sys/systm.h>
     37 #include <sys/device.h>
     38 #include <sys/malloc.h>
     39 #include <sys/buf.h>
     40 #include <sys/kernel.h>
     41 
     42 #include <uvm/uvm_extern.h>
     43 
     44 #include <machine/endian.h>
     45 #include <machine/bus.h>
     46 
     47 #include <dev/microcode/siop/siop.out>
     48 
     49 #include <dev/scsipi/scsi_all.h>
     50 #include <dev/scsipi/scsi_message.h>
     51 #include <dev/scsipi/scsipi_all.h>
     52 
     53 #include <dev/scsipi/scsiconf.h>
     54 
     55 #include <dev/ic/siopreg.h>
     56 #include <dev/ic/siopvar.h>
     57 #include <dev/ic/siopvar_common.h>
     58 
     59 #ifndef DEBUG
     60 #undef DEBUG
     61 #endif
     62 #undef SIOP_DEBUG
     63 #undef SIOP_DEBUG_DR
     64 #undef SIOP_DEBUG_INTR
     65 #undef SIOP_DEBUG_SCHED
     66 #undef DUMP_SCRIPT
     67 
     68 #define SIOP_STATS
     69 
     70 #ifndef SIOP_DEFAULT_TARGET
     71 #define SIOP_DEFAULT_TARGET 7
     72 #endif
     73 
     74 /* number of cmd descriptors per block */
     75 #define SIOP_NCMDPB (PAGE_SIZE / sizeof(struct siop_xfer))
     76 
     77 /* Number of scheduler slot (needs to match script) */
     78 #define SIOP_NSLOTS 40
     79 
     80 void	siop_reset __P((struct siop_softc *));
     81 void	siop_handle_reset __P((struct siop_softc *));
     82 int	siop_handle_qtag_reject __P((struct siop_cmd *));
     83 void	siop_scsicmd_end __P((struct siop_cmd *));
     84 void	siop_start __P((struct siop_softc *));
     85 void 	siop_timeout __P((void *));
     86 int	siop_scsicmd __P((struct scsipi_xfer *));
     87 void	siop_dump_script __P((struct siop_softc *));
     88 int	siop_morecbd __P((struct siop_softc *));
     89 struct siop_lunsw *siop_get_lunsw __P((struct siop_softc *));
     90 void	siop_add_reselsw __P((struct siop_softc *, int));
     91 void	siop_update_scntl3 __P((struct siop_softc *, struct siop_target *));
     92 
     93 struct scsipi_adapter siop_adapter = {
     94 	0,
     95 	siop_scsicmd,
     96 	siop_minphys,
     97 	siop_ioctl,
     98 	NULL,
     99 	NULL,
    100 };
    101 
    102 struct scsipi_device siop_dev = {
    103 	NULL,
    104 	NULL,
    105 	NULL,
    106 	NULL,
    107 };
    108 
    109 #ifdef SIOP_STATS
    110 static int siop_stat_intr = 0;
    111 static int siop_stat_intr_shortxfer = 0;
    112 static int siop_stat_intr_sdp = 0;
    113 static int siop_stat_intr_done = 0;
    114 static int siop_stat_intr_xferdisc = 0;
    115 static int siop_stat_intr_lunresel = 0;
    116 static int siop_stat_intr_qfull = 0;
    117 void siop_printstats __P((void));
    118 #define INCSTAT(x) x++
    119 #else
    120 #define INCSTAT(x)
    121 #endif
    122 
    123 static __inline__ void siop_script_sync __P((struct siop_softc *, int));
    124 static __inline__ void
    125 siop_script_sync(sc, ops)
    126 	struct siop_softc *sc;
    127 	int ops;
    128 {
    129 	if ((sc->features & SF_CHIP_RAM) == 0)
    130 		bus_dmamap_sync(sc->sc_dmat, sc->sc_scriptdma, 0,
    131 		    PAGE_SIZE, ops);
    132 }
    133 
    134 static __inline__ u_int32_t siop_script_read __P((struct siop_softc *, u_int));
    135 static __inline__ u_int32_t
    136 siop_script_read(sc, offset)
    137 	struct siop_softc *sc;
    138 	u_int offset;
    139 {
    140 	if (sc->features & SF_CHIP_RAM) {
    141 		return bus_space_read_4(sc->sc_ramt, sc->sc_ramh, offset * 4);
    142 	} else {
    143 		return le32toh(sc->sc_script[offset]);
    144 	}
    145 }
    146 
    147 static __inline__ void siop_script_write __P((struct siop_softc *, u_int,
    148 	u_int32_t));
    149 static __inline__ void
    150 siop_script_write(sc, offset, val)
    151 	struct siop_softc *sc;
    152 	u_int offset;
    153 	u_int32_t val;
    154 {
    155 	if (sc->features & SF_CHIP_RAM) {
    156 		bus_space_write_4(sc->sc_ramt, sc->sc_ramh, offset * 4, val);
    157 	} else {
    158 		sc->sc_script[offset] = htole32(val);
    159 	}
    160 }
    161 
    162 void
    163 siop_attach(sc)
    164 	struct siop_softc *sc;
    165 {
    166 	int error, i;
    167 	bus_dma_segment_t seg;
    168 	int rseg;
    169 
    170 	/*
    171 	 * Allocate DMA-safe memory for the script and map it.
    172 	 */
    173 	if ((sc->features & SF_CHIP_RAM) == 0) {
    174 		error = bus_dmamem_alloc(sc->sc_dmat, PAGE_SIZE,
    175 		    PAGE_SIZE, 0, &seg, 1, &rseg, BUS_DMA_NOWAIT);
    176 		if (error) {
    177 			printf("%s: unable to allocate script DMA memory, "
    178 			    "error = %d\n", sc->sc_dev.dv_xname, error);
    179 			return;
    180 		}
    181 		error = bus_dmamem_map(sc->sc_dmat, &seg, rseg, PAGE_SIZE,
    182 		    (caddr_t *)&sc->sc_script, BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
    183 		if (error) {
    184 			printf("%s: unable to map script DMA memory, "
    185 			    "error = %d\n", sc->sc_dev.dv_xname, error);
    186 			return;
    187 		}
    188 		error = bus_dmamap_create(sc->sc_dmat, PAGE_SIZE, 1,
    189 		    PAGE_SIZE, 0, BUS_DMA_NOWAIT, &sc->sc_scriptdma);
    190 		if (error) {
    191 			printf("%s: unable to create script DMA map, "
    192 			    "error = %d\n", sc->sc_dev.dv_xname, error);
    193 			return;
    194 		}
    195 		error = bus_dmamap_load(sc->sc_dmat, sc->sc_scriptdma,
    196 		    sc->sc_script, PAGE_SIZE, NULL, BUS_DMA_NOWAIT);
    197 		if (error) {
    198 			printf("%s: unable to load script DMA map, "
    199 			    "error = %d\n", sc->sc_dev.dv_xname, error);
    200 			return;
    201 		}
    202 		sc->sc_scriptaddr = sc->sc_scriptdma->dm_segs[0].ds_addr;
    203 		sc->ram_size = PAGE_SIZE;
    204 	}
    205 	TAILQ_INIT(&sc->free_list);
    206 	TAILQ_INIT(&sc->ready_list);
    207 	TAILQ_INIT(&sc->urgent_list);
    208 	TAILQ_INIT(&sc->cmds);
    209 	TAILQ_INIT(&sc->lunsw_list);
    210 	sc->sc_currschedslot = 0;
    211 #ifdef SIOP_DEBUG
    212 	printf("%s: script size = %d, PHY addr=0x%x, VIRT=%p\n",
    213 	    sc->sc_dev.dv_xname, (int)sizeof(siop_script),
    214 	    (u_int32_t)sc->sc_scriptaddr, sc->sc_script);
    215 #endif
    216 
    217 	sc->sc_link.adapter_softc = sc;
    218 	sc->sc_link.openings = 2;
    219 	sc->sc_link.scsipi_scsi.channel = SCSI_CHANNEL_ONLY_ONE;
    220 	sc->sc_link.scsipi_scsi.max_target  =
    221 	    (sc->features & SF_BUS_WIDE) ? 15 : 7;
    222 	sc->sc_link.scsipi_scsi.max_lun = 7;
    223 	sc->sc_link.scsipi_scsi.adapter_target = bus_space_read_1(sc->sc_rt,
    224 	    sc->sc_rh, SIOP_SCID);
    225 	if (sc->sc_link.scsipi_scsi.adapter_target == 0 ||
    226 	    sc->sc_link.scsipi_scsi.adapter_target >
    227 	    sc->sc_link.scsipi_scsi.max_target)
    228 		sc->sc_link.scsipi_scsi.adapter_target = SIOP_DEFAULT_TARGET;
    229 	sc->sc_link.type = BUS_SCSI;
    230 	sc->sc_link.adapter = &siop_adapter;
    231 	sc->sc_link.device = &siop_dev;
    232 	sc->sc_link.flags  = 0;
    233 
    234 	for (i = 0; i < 16; i++)
    235 		sc->targets[i] = NULL;
    236 
    237 	/* find min/max sync period for this chip */
    238 	sc->maxsync = 0;
    239 	sc->minsync = 255;
    240 	for (i = 0; i < sizeof(scf_period) / sizeof(scf_period[0]); i++) {
    241 		if (sc->clock_period != scf_period[i].clock)
    242 			continue;
    243 		if (sc->maxsync < scf_period[i].period)
    244 			sc->maxsync = scf_period[i].period;
    245 		if (sc->minsync > scf_period[i].period)
    246 			sc->minsync = scf_period[i].period;
    247 	}
    248 	if (sc->maxsync == 255 || sc->minsync == 0)
    249 		panic("siop: can't find my sync parameters\n");
    250 	/* Do a bus reset, so that devices fall back to narrow/async */
    251 	siop_resetbus(sc);
    252 	/*
    253 	 * siop_reset() will reset the chip, thus clearing pending interrupts
    254 	 */
    255 	siop_reset(sc);
    256 #ifdef DUMP_SCRIPT
    257 	siop_dump_script(sc);
    258 #endif
    259 
    260 	config_found((struct device*)sc, &sc->sc_link, scsiprint);
    261 }
    262 
    263 void
    264 siop_reset(sc)
    265 	struct siop_softc *sc;
    266 {
    267 	int i, j;
    268 	struct siop_lunsw *lunsw;
    269 
    270 	siop_common_reset(sc);
    271 
    272 	/* copy and patch the script */
    273 	if (sc->features & SF_CHIP_RAM) {
    274 		bus_space_write_region_4(sc->sc_ramt, sc->sc_ramh, 0,
    275 		    siop_script, sizeof(siop_script) / sizeof(siop_script[0]));
    276 		for (j = 0; j <
    277 		    (sizeof(E_abs_msgin_Used) / sizeof(E_abs_msgin_Used[0]));
    278 		    j++) {
    279 			bus_space_write_4(sc->sc_ramt, sc->sc_ramh,
    280 			    E_abs_msgin_Used[j] * 4,
    281 			    sc->sc_scriptaddr + Ent_msgin_space);
    282 		}
    283 	} else {
    284 		for (j = 0;
    285 		    j < (sizeof(siop_script) / sizeof(siop_script[0])); j++) {
    286 			sc->sc_script[j] = htole32(siop_script[j]);
    287 		}
    288 		for (j = 0; j <
    289 		    (sizeof(E_abs_msgin_Used) / sizeof(E_abs_msgin_Used[0]));
    290 		    j++) {
    291 			sc->sc_script[E_abs_msgin_Used[j]] =
    292 			    htole32(sc->sc_scriptaddr + Ent_msgin_space);
    293 		}
    294 	}
    295 	sc->script_free_lo = sizeof(siop_script) / sizeof(siop_script[0]);
    296 	sc->script_free_hi = sc->ram_size / 4;
    297 
    298 	/* free used and unused lun switches */
    299 	while((lunsw = TAILQ_FIRST(&sc->lunsw_list)) != NULL) {
    300 #ifdef SIOP_DEBUG
    301 		printf("%s: free lunsw at offset %d\n",
    302 				sc->sc_dev.dv_xname, lunsw->lunsw_off);
    303 #endif
    304 		TAILQ_REMOVE(&sc->lunsw_list, lunsw, next);
    305 		free(lunsw, M_DEVBUF);
    306 	}
    307 	TAILQ_INIT(&sc->lunsw_list);
    308 	/* restore reselect switch */
    309 	for (i = 0; i <= sc->sc_link.scsipi_scsi.max_target; i++) {
    310 		if (sc->targets[i] == NULL)
    311 			continue;
    312 #ifdef SIOP_DEBUG
    313 		printf("%s: restore sw for target %d\n",
    314 				sc->sc_dev.dv_xname, i);
    315 #endif
    316 		free(sc->targets[i]->lunsw, M_DEVBUF);
    317 		sc->targets[i]->lunsw = siop_get_lunsw(sc);
    318 		if (sc->targets[i]->lunsw == NULL) {
    319 			printf("%s: can't alloc lunsw for target %d\n",
    320 			    sc->sc_dev.dv_xname, i);
    321 			break;
    322 		}
    323 		siop_add_reselsw(sc, i);
    324 	}
    325 
    326 	/* start script */
    327 	if ((sc->features & SF_CHIP_RAM) == 0) {
    328 		bus_dmamap_sync(sc->sc_dmat, sc->sc_scriptdma, 0, PAGE_SIZE,
    329 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
    330 	}
    331 	bus_space_write_4(sc->sc_rt, sc->sc_rh, SIOP_DSP,
    332 	    sc->sc_scriptaddr + Ent_reselect);
    333 }
    334 
    335 #if 0
    336 #define CALL_SCRIPT(ent) do {\
    337 	printf ("start script DSA 0x%lx DSP 0x%lx\n", \
    338 	    siop_cmd->dsa, \
    339 	    sc->sc_scriptaddr + ent); \
    340 bus_space_write_4(sc->sc_rt, sc->sc_rh, SIOP_DSP, sc->sc_scriptaddr + ent); \
    341 } while (0)
    342 #else
    343 #define CALL_SCRIPT(ent) do {\
    344 bus_space_write_4(sc->sc_rt, sc->sc_rh, SIOP_DSP, sc->sc_scriptaddr + ent); \
    345 } while (0)
    346 #endif
    347 
    348 int
    349 siop_intr(v)
    350 	void *v;
    351 {
    352 	struct siop_softc *sc = v;
    353 	struct siop_target *siop_target;
    354 	struct siop_cmd *siop_cmd;
    355 	struct siop_lun *siop_lun;
    356 	struct scsipi_xfer *xs;
    357 	int istat, sist, sstat1, dstat;
    358 	u_int32_t irqcode;
    359 	int need_reset = 0;
    360 	int offset, target, lun, tag;
    361 	bus_addr_t dsa;
    362 	struct siop_cbd *cbdp;
    363 	int freetarget = 0;
    364 
    365 	istat = bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_ISTAT);
    366 	if ((istat & (ISTAT_INTF | ISTAT_DIP | ISTAT_SIP)) == 0)
    367 		return 0;
    368 	INCSTAT(siop_stat_intr);
    369 	if (istat & ISTAT_INTF) {
    370 		printf("INTRF\n");
    371 		bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_ISTAT, ISTAT_INTF);
    372 	}
    373 	/* use DSA to find the current siop_cmd */
    374 	dsa = bus_space_read_4(sc->sc_rt, sc->sc_rh, SIOP_DSA);
    375 	for (cbdp = TAILQ_FIRST(&sc->cmds); cbdp != NULL;
    376 	    cbdp = TAILQ_NEXT(cbdp, next)) {
    377 		if (dsa >= cbdp->xferdma->dm_segs[0].ds_addr &&
    378 	    	    dsa < cbdp->xferdma->dm_segs[0].ds_addr + PAGE_SIZE) {
    379 			dsa -= cbdp->xferdma->dm_segs[0].ds_addr;
    380 			siop_cmd = &cbdp->cmds[dsa / sizeof(struct siop_xfer)];
    381 			siop_table_sync(siop_cmd,
    382 			    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
    383 			break;
    384 		}
    385 	}
    386 	if (cbdp == NULL) {
    387 		siop_cmd = NULL;
    388 	}
    389 	if (siop_cmd) {
    390 		xs = siop_cmd->xs;
    391 		siop_target = siop_cmd->siop_target;
    392 		target = siop_cmd->xs->sc_link->scsipi_scsi.target;
    393 		lun = siop_cmd->xs->sc_link->scsipi_scsi.lun;
    394 		tag = siop_cmd->tag;
    395 		siop_lun = siop_target->siop_lun[lun];
    396 #ifdef DIAGNOSTIC
    397 		if (siop_cmd->status != CMDST_ACTIVE &&
    398 		    siop_cmd->status != CMDST_SENSE_ACTIVE) {
    399 			printf("siop_cmd (lun %d) not active (%d)\n",
    400 				lun, siop_cmd->status);
    401 			xs = NULL;
    402 			siop_target = NULL;
    403 			target = -1;
    404 			lun = -1;
    405 			tag = -1;
    406 			siop_lun = NULL;
    407 			siop_cmd = NULL;
    408 		} else if (siop_lun->siop_tag[tag].active != siop_cmd) {
    409 			printf("siop_cmd (lun %d tag %d) not in siop_lun "
    410 			    "active (%p != %p)\n", lun, tag, siop_cmd,
    411 			    siop_lun->siop_tag[tag].active);
    412 		}
    413 #endif
    414 	} else {
    415 		xs = NULL;
    416 		siop_target = NULL;
    417 		target = -1;
    418 		lun = -1;
    419 		tag = -1;
    420 		siop_lun = NULL;
    421 	}
    422 	if (istat & ISTAT_DIP) {
    423 		dstat = bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_DSTAT);
    424 		if (dstat & DSTAT_SSI) {
    425 			printf("single step dsp 0x%08x dsa 0x08%x\n",
    426 			    (int)(bus_space_read_4(sc->sc_rt, sc->sc_rh, SIOP_DSP) -
    427 			    sc->sc_scriptaddr),
    428 			    bus_space_read_4(sc->sc_rt, sc->sc_rh, SIOP_DSA));
    429 			if ((dstat & ~(DSTAT_DFE | DSTAT_SSI)) == 0 &&
    430 			    (istat & ISTAT_SIP) == 0) {
    431 				bus_space_write_1(sc->sc_rt, sc->sc_rh,
    432 				    SIOP_DCNTL, bus_space_read_1(sc->sc_rt,
    433 				    sc->sc_rh, SIOP_DCNTL) | DCNTL_STD);
    434 			}
    435 			return 1;
    436 		}
    437 		if (dstat & ~(DSTAT_SIR | DSTAT_DFE | DSTAT_SSI)) {
    438 		printf("DMA IRQ:");
    439 		if (dstat & DSTAT_IID)
    440 			printf(" Illegal instruction");
    441 		if (dstat & DSTAT_ABRT)
    442 			printf(" abort");
    443 		if (dstat & DSTAT_BF)
    444 			printf(" bus fault");
    445 		if (dstat & DSTAT_MDPE)
    446 			printf(" parity");
    447 		if (dstat & DSTAT_DFE)
    448 			printf(" dma fifo empty");
    449 		printf(", DSP=0x%x DSA=0x%x: ",
    450 		    (int)(bus_space_read_4(sc->sc_rt, sc->sc_rh, SIOP_DSP) -
    451 		    sc->sc_scriptaddr),
    452 		    bus_space_read_4(sc->sc_rt, sc->sc_rh, SIOP_DSA));
    453 		if (siop_cmd)
    454 			printf("last msg_in=0x%x status=0x%x\n",
    455 			    siop_cmd->siop_tables.msg_in[0],
    456 			    le32toh(siop_cmd->siop_tables.status));
    457 		else
    458 			printf("%s: current DSA invalid\n",
    459 			    sc->sc_dev.dv_xname);
    460 		need_reset = 1;
    461 		}
    462 	}
    463 	if (istat & ISTAT_SIP) {
    464 		if (istat & ISTAT_DIP)
    465 			delay(10);
    466 		/*
    467 		 * Can't read sist0 & sist1 independantly, or we have to
    468 		 * insert delay
    469 		 */
    470 		sist = bus_space_read_2(sc->sc_rt, sc->sc_rh, SIOP_SIST0);
    471 		sstat1 = bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_SSTAT1);
    472 #ifdef SIOP_DEBUG_INTR
    473 		printf("scsi interrupt, sist=0x%x sstat1=0x%x "
    474 		    "DSA=0x%x DSP=0x%lx\n", sist,
    475 		    bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_SSTAT1),
    476 		    bus_space_read_4(sc->sc_rt, sc->sc_rh, SIOP_DSA),
    477 		    (u_long)(bus_space_read_4(sc->sc_rt, sc->sc_rh, SIOP_DSP) -
    478 		    sc->sc_scriptaddr));
    479 #endif
    480 		if (sist & SIST0_RST) {
    481 			siop_handle_reset(sc);
    482 			siop_start(sc);
    483 			/* no table to flush here */
    484 			return 1;
    485 		}
    486 		if (sist & SIST0_SGE) {
    487 			if (siop_cmd)
    488 				scsi_print_addr(xs->sc_link);
    489 			else
    490 				printf("%s:", sc->sc_dev.dv_xname);
    491 			printf("scsi gross error\n");
    492 			goto reset;
    493 		}
    494 		if ((sist & SIST0_MA) && need_reset == 0) {
    495 			if (siop_cmd) {
    496 				int scratcha0;
    497 				dstat = bus_space_read_1(sc->sc_rt, sc->sc_rh,
    498 				    SIOP_DSTAT);
    499 				/*
    500 				 * first restore DSA, in case we were in a S/G
    501 				 * operation.
    502 				 */
    503 				bus_space_write_4(sc->sc_rt, sc->sc_rh,
    504 				    SIOP_DSA, siop_cmd->dsa);
    505 				scratcha0 = bus_space_read_1(sc->sc_rt,
    506 				    sc->sc_rh, SIOP_SCRATCHA);
    507 				switch (sstat1 & SSTAT1_PHASE_MASK) {
    508 				case SSTAT1_PHASE_STATUS:
    509 				/*
    510 				 * previous phase may be aborted for any reason
    511 				 * ( for example, the target has less data to
    512 				 * transfer than requested). Just go to status
    513 				 * and the command should terminate.
    514 				 */
    515 					INCSTAT(siop_stat_intr_shortxfer);
    516 					if ((dstat & DSTAT_DFE) == 0)
    517 						siop_clearfifo(sc);
    518 					/* no table to flush here */
    519 					CALL_SCRIPT(Ent_status);
    520 					return 1;
    521 				case SSTAT1_PHASE_MSGIN:
    522 					/*
    523 					 * target may be ready to disconnect
    524 					 * Save data pointers just in case.
    525 					 */
    526 					INCSTAT(siop_stat_intr_xferdisc);
    527 					if (scratcha0 & A_flag_data)
    528 						siop_sdp(siop_cmd);
    529 					else if ((dstat & DSTAT_DFE) == 0)
    530 						siop_clearfifo(sc);
    531 					bus_space_write_1(sc->sc_rt, sc->sc_rh,
    532 					    SIOP_SCRATCHA,
    533 					    scratcha0 & ~A_flag_data);
    534 					siop_table_sync(siop_cmd,
    535 					    BUS_DMASYNC_PREREAD |
    536 					    BUS_DMASYNC_PREWRITE);
    537 					CALL_SCRIPT(Ent_msgin);
    538 					return 1;
    539 				}
    540 				printf("%s: unexpected phase mismatch %d\n",
    541 				    sc->sc_dev.dv_xname,
    542 				    sstat1 & SSTAT1_PHASE_MASK);
    543 			} else {
    544 				printf("%s: phase mismatch without command\n",
    545 				    sc->sc_dev.dv_xname);
    546 			}
    547 			need_reset = 1;
    548 		}
    549 		if (sist & SIST0_PAR) {
    550 			/* parity error, reset */
    551 			if (siop_cmd)
    552 				scsi_print_addr(xs->sc_link);
    553 			else
    554 				printf("%s:", sc->sc_dev.dv_xname);
    555 			printf("parity error\n");
    556 			goto reset;
    557 		}
    558 		if ((sist & (SIST1_STO << 8)) && need_reset == 0) {
    559 			/* selection time out, assume there's no device here */
    560 			if (siop_cmd) {
    561 				siop_cmd->status = CMDST_DONE;
    562 				xs->error = XS_SELTIMEOUT;
    563 				freetarget = 1;
    564 				goto end;
    565 			} else {
    566 				printf("%s: selection timeout without "
    567 				    "command\n", sc->sc_dev.dv_xname);
    568 				need_reset = 1;
    569 			}
    570 		}
    571 		if (sist & SIST0_UDC) {
    572 			/*
    573 			 * unexpected disconnect. Usually the target signals
    574 			 * a fatal condition this way. Attempt to get sense.
    575 			 */
    576 			 if (siop_cmd) {
    577 				siop_cmd->siop_tables.status =
    578 				    htole32(SCSI_CHECK);
    579 				goto end;
    580 			}
    581 			printf("%s: unexpected disconnect without "
    582 			    "command\n", sc->sc_dev.dv_xname);
    583 			goto reset;
    584 		}
    585 		if (sist & (SIST1_SBMC << 8)) {
    586 			/* SCSI bus mode change */
    587 			if (siop_modechange(sc) == 0 || need_reset == 1)
    588 				goto reset;
    589 			if ((istat & ISTAT_DIP) && (dstat & DSTAT_SIR)) {
    590 				/*
    591 				 * we have a script interrupt, it will
    592 				 * restart the script.
    593 				 */
    594 				goto scintr;
    595 			}
    596 			/*
    597 			 * else we have to restart it ourselve, at the
    598 			 * interrupted instruction.
    599 			 */
    600 			bus_space_write_4(sc->sc_rt, sc->sc_rh, SIOP_DSP,
    601 			    bus_space_read_4(sc->sc_rt, sc->sc_rh,
    602 			    SIOP_DSP) - 8);
    603 			return 1;
    604 		}
    605 		/* Else it's an unhandled exeption (for now). */
    606 		printf("%s: unhandled scsi interrupt, sist=0x%x sstat1=0x%x "
    607 		    "DSA=0x%x DSP=0x%x\n", sc->sc_dev.dv_xname, sist,
    608 		    bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_SSTAT1),
    609 		    bus_space_read_4(sc->sc_rt, sc->sc_rh, SIOP_DSA),
    610 		    (int)(bus_space_read_4(sc->sc_rt, sc->sc_rh, SIOP_DSP) -
    611 		    sc->sc_scriptaddr));
    612 		if (siop_cmd) {
    613 			siop_cmd->status = CMDST_DONE;
    614 			xs->error = XS_SELTIMEOUT;
    615 			goto end;
    616 		}
    617 		need_reset = 1;
    618 	}
    619 	if (need_reset) {
    620 reset:
    621 		/* fatal error, reset the bus */
    622 		siop_resetbus(sc);
    623 		/* no table to flush here */
    624 		return 1;
    625 	}
    626 
    627 scintr:
    628 	if ((istat & ISTAT_DIP) && (dstat & DSTAT_SIR)) { /* script interrupt */
    629 		irqcode = bus_space_read_4(sc->sc_rt, sc->sc_rh,
    630 		    SIOP_DSPS);
    631 #ifdef SIOP_DEBUG_INTR
    632 		printf("script interrupt 0x%x\n", irqcode);
    633 #endif
    634 		/*
    635 		 * no command, or an inactive command is only valid for a
    636 		 * reselect interrupt
    637 		 */
    638 		if ((irqcode & 0x80) == 0) {
    639 			if (siop_cmd == NULL) {
    640 				printf("%s: script interrupt (0x%x) with
    641 				    invalid DSA !!!\n", sc->sc_dev.dv_xname,
    642 				    irqcode);
    643 				goto reset;
    644 			}
    645 			if (siop_cmd->status != CMDST_ACTIVE &&
    646 			    siop_cmd->status != CMDST_SENSE_ACTIVE) {
    647 				printf("%s: command with invalid status "
    648 				    "(IRQ code 0x%x current status %d) !\n",
    649 				    sc->sc_dev.dv_xname,
    650 				    irqcode, siop_cmd->status);
    651 				xs = NULL;
    652 			}
    653 		}
    654 		switch(irqcode) {
    655 		case A_int_err:
    656 			printf("error, DSP=0x%x\n",
    657 			    (int)(bus_space_read_4(sc->sc_rt, sc->sc_rh,
    658 			    SIOP_DSP) - sc->sc_scriptaddr));
    659 			if (xs) {
    660 				xs->error = XS_SELTIMEOUT;
    661 				goto end;
    662 			} else {
    663 				goto reset;
    664 			}
    665 		case A_int_reseltarg:
    666 			printf("%s: reselect with invalid target\n",
    667 				    sc->sc_dev.dv_xname);
    668 			goto reset;
    669 		case A_int_resellun:
    670 			INCSTAT(siop_stat_intr_lunresel);
    671 			target = bus_space_read_1(sc->sc_rt, sc->sc_rh,
    672 			    SIOP_SCRATCHA) & 0xf;
    673 			lun = bus_space_read_1(sc->sc_rt, sc->sc_rh,
    674 			    SIOP_SCRATCHA + 1);
    675 			tag = bus_space_read_1(sc->sc_rt, sc->sc_rh,
    676 			    SIOP_SCRATCHA + 2);
    677 			siop_target = sc->targets[target];
    678 			if (siop_target == NULL) {
    679 				printf("%s: reselect with invalid "
    680 				    "target %d\n", sc->sc_dev.dv_xname, target);
    681 				goto reset;
    682 			}
    683 			siop_lun = siop_target->siop_lun[lun];
    684 			if (siop_lun == NULL) {
    685 				printf("%s: target %d reselect with invalid "
    686 				    "lun %d\n", sc->sc_dev.dv_xname,
    687 				    target, lun);
    688 				goto reset;
    689 			}
    690 			if (siop_lun->siop_tag[tag].active == NULL) {
    691 				printf("%s: target %d lun %d tag %d reselect "
    692 				    "without command\n", sc->sc_dev.dv_xname,
    693 				    target, lun, tag);
    694 				goto reset;
    695 			}
    696 			siop_cmd = siop_lun->siop_tag[tag].active;
    697 			bus_space_write_4(sc->sc_rt, sc->sc_rh, SIOP_DSP,
    698 			    siop_cmd->dsa + sizeof(struct siop_xfer_common) +
    699 			    Ent_ldsa_reload_dsa);
    700 			return 1;
    701 		case A_int_reseltag:
    702 			printf("%s: reselect with invalid tag\n",
    703 				    sc->sc_dev.dv_xname);
    704 			goto reset;
    705 		case A_int_msgin:
    706 		{
    707 			int msgin = bus_space_read_1(sc->sc_rt, sc->sc_rh,
    708 			    SIOP_SFBR);
    709 			if (msgin == MSG_MESSAGE_REJECT) {
    710 				int msg, extmsg;
    711 				if (siop_cmd->siop_tables.msg_out[0] & 0x80) {
    712 					/*
    713 					 * message was part of a identify +
    714 					 * something else. Identify shoudl't
    715 					 * have been rejected.
    716 					 */
    717 					msg = siop_cmd->siop_tables.msg_out[1];
    718 					extmsg =
    719 					    siop_cmd->siop_tables.msg_out[3];
    720 				} else {
    721 					msg = siop_cmd->siop_tables.msg_out[0];
    722 					extmsg =
    723 					    siop_cmd->siop_tables.msg_out[2];
    724 				}
    725 				if (msg == MSG_MESSAGE_REJECT) {
    726 					/* MSG_REJECT  for a MSG_REJECT  !*/
    727 					if (xs)
    728 						scsi_print_addr(xs->sc_link);
    729 					else
    730 						printf("%s: ",
    731 						   sc->sc_dev.dv_xname);
    732 					printf("our reject message was "
    733 					    "rejected\n");
    734 					goto reset;
    735 				}
    736 				if (msg == MSG_EXTENDED &&
    737 				    extmsg == MSG_EXT_WDTR) {
    738 					/* WDTR rejected, initiate sync */
    739 					printf("%s: target %d using 8bit "
    740 					    "transfers\n", sc->sc_dev.dv_xname,
    741 					    target);
    742 					if ((siop_target->flags & TARF_SYNC)
    743 					    == 0) {
    744 						siop_target->status = TARST_OK;
    745 						/* no table to flush here */
    746 						CALL_SCRIPT(Ent_msgin_ack);
    747 						return 1;
    748 					}
    749 					siop_target->status = TARST_SYNC_NEG;
    750 					siop_sdtr_msg(siop_cmd, 0,
    751 					    sc->minsync, sc->maxoff);
    752 					siop_table_sync(siop_cmd,
    753 					    BUS_DMASYNC_PREREAD |
    754 					    BUS_DMASYNC_PREWRITE);
    755 					CALL_SCRIPT(Ent_send_msgout);
    756 					return 1;
    757 				} else if (msg == MSG_EXTENDED &&
    758 				    extmsg == MSG_EXT_SDTR) {
    759 					/* sync rejected */
    760 					printf("%s: target %d asynchronous\n",
    761 					    sc->sc_dev.dv_xname,
    762 					    target);
    763 					siop_target->status = TARST_OK;
    764 					/* no table to flush here */
    765 					CALL_SCRIPT(Ent_msgin_ack);
    766 					return 1;
    767 				} else if (msg == MSG_SIMPLE_Q_TAG ||
    768 				    msg == MSG_HEAD_OF_Q_TAG ||
    769 				    msg == MSG_ORDERED_Q_TAG) {
    770 					if (siop_handle_qtag_reject(
    771 					    siop_cmd) == -1)
    772 						goto reset;
    773 					CALL_SCRIPT(Ent_msgin_ack);
    774 					return 1;
    775 				}
    776 				if (xs)
    777 					scsi_print_addr(xs->sc_link);
    778 				else
    779 					printf("%s: ", sc->sc_dev.dv_xname);
    780 				if (msg == MSG_EXTENDED) {
    781 					printf("scsi message reject, extended "
    782 					    "message sent was 0x%x\n", extmsg);
    783 				} else {
    784 					printf("scsi message reject, message "
    785 					    "sent was 0x%x\n", msg);
    786 				}
    787 				/* no table to flush here */
    788 				CALL_SCRIPT(Ent_msgin_ack);
    789 				return 1;
    790 			}
    791 			if (xs)
    792 				scsi_print_addr(xs->sc_link);
    793 			else
    794 				printf("%s: ", sc->sc_dev.dv_xname);
    795 			printf("unhandled message 0x%x\n",
    796 			    siop_cmd->siop_tables.msg_in[0]);
    797 			siop_cmd->siop_tables.msg_out[0] = MSG_MESSAGE_REJECT;
    798 			siop_cmd->siop_tables.t_msgout.count= htole32(1);
    799 			siop_table_sync(siop_cmd,
    800 			    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
    801 			CALL_SCRIPT(Ent_send_msgout);
    802 			return 1;
    803 		}
    804 		case A_int_extmsgin:
    805 #ifdef SIOP_DEBUG_INTR
    806 			printf("extended message: msg 0x%x len %d\n",
    807 			    siop_cmd->siop_tables.msg_in[2],
    808 			    siop_cmd->siop_tables.msg_in[1]);
    809 #endif
    810 			if (siop_cmd->siop_tables.msg_in[1] > 6)
    811 				printf("%s: extended message too big (%d)\n",
    812 				    sc->sc_dev.dv_xname,
    813 				    siop_cmd->siop_tables.msg_in[1]);
    814 			siop_cmd->siop_tables.t_extmsgdata.count =
    815 			    htole32(siop_cmd->siop_tables.msg_in[1] - 1);
    816 			siop_table_sync(siop_cmd,
    817 			    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
    818 			CALL_SCRIPT(Ent_get_extmsgdata);
    819 			return 1;
    820 		case A_int_extmsgdata:
    821 #ifdef SIOP_DEBUG_INTR
    822 			{
    823 			int i;
    824 			printf("extended message: 0x%x, data:",
    825 			    siop_cmd->siop_tables.msg_in[2]);
    826 			for (i = 3; i < 2 + siop_cmd->siop_tables.msg_in[1];
    827 			    i++)
    828 				printf(" 0x%x",
    829 				    siop_cmd->siop_tables.msg_in[i]);
    830 			printf("\n");
    831 			}
    832 #endif
    833 			if (siop_cmd->siop_tables.msg_in[2] == MSG_EXT_WDTR) {
    834 				switch (siop_wdtr_neg(siop_cmd)) {
    835 				case SIOP_NEG_MSGOUT:
    836 					siop_update_scntl3(sc,
    837 					    siop_cmd->siop_target);
    838 					siop_table_sync(siop_cmd,
    839 					    BUS_DMASYNC_PREREAD |
    840 					    BUS_DMASYNC_PREWRITE);
    841 					CALL_SCRIPT(Ent_send_msgout);
    842 					return(1);
    843 				case SIOP_NEG_ACK:
    844 					siop_update_scntl3(sc,
    845 					    siop_cmd->siop_target);
    846 					CALL_SCRIPT(Ent_msgin_ack);
    847 					return(1);
    848 				default:
    849 					panic("invalid retval from "
    850 					    "siop_wdtr_neg()");
    851 				}
    852 				return(1);
    853 			}
    854 			if (siop_cmd->siop_tables.msg_in[2] == MSG_EXT_SDTR) {
    855 				switch (siop_sdtr_neg(siop_cmd)) {
    856 				case SIOP_NEG_MSGOUT:
    857 					siop_update_scntl3(sc,
    858 					    siop_cmd->siop_target);
    859 					siop_table_sync(siop_cmd,
    860 					    BUS_DMASYNC_PREREAD |
    861 					    BUS_DMASYNC_PREWRITE);
    862 					CALL_SCRIPT(Ent_send_msgout);
    863 					return(1);
    864 				case SIOP_NEG_ACK:
    865 					siop_update_scntl3(sc,
    866 					    siop_cmd->siop_target);
    867 					CALL_SCRIPT(Ent_msgin_ack);
    868 					return(1);
    869 				default:
    870 					panic("invalid retval from "
    871 					    "siop_wdtr_neg()");
    872 				}
    873 				return(1);
    874 			}
    875 			/* send a message reject */
    876 			siop_cmd->siop_tables.msg_out[0] = MSG_MESSAGE_REJECT;
    877 			siop_cmd->siop_tables.t_msgout.count = htole32(1);
    878 			siop_table_sync(siop_cmd,
    879 			    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
    880 			CALL_SCRIPT(Ent_send_msgout);
    881 			return 1;
    882 		case A_int_disc:
    883 			INCSTAT(siop_stat_intr_sdp);
    884 			offset = bus_space_read_1(sc->sc_rt, sc->sc_rh,
    885 			    SIOP_SCRATCHA + 1);
    886 #ifdef SIOP_DEBUG_DR
    887 			printf("disconnect offset %d\n", offset);
    888 #endif
    889 			if (offset > SIOP_NSG) {
    890 				printf("%s: bad offset for disconnect (%d)\n",
    891 				    sc->sc_dev.dv_xname, offset);
    892 				goto reset;
    893 			}
    894 			/*
    895 			 * offset == SIOP_NSG may be a valid condition if
    896 			 * we get a sdp when the xfer is done.
    897 			 * Don't call memmove in this case.
    898 			 */
    899 			if (offset < SIOP_NSG) {
    900 				memmove(&siop_cmd->siop_tables.data[0],
    901 				    &siop_cmd->siop_tables.data[offset],
    902 				    (SIOP_NSG - offset) * sizeof(scr_table_t));
    903 				siop_table_sync(siop_cmd,
    904 				    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
    905 			}
    906 			CALL_SCRIPT(Ent_script_sched);
    907 			/* check if we can put some command in scheduler */
    908 			siop_start(sc);
    909 			return 1;
    910 		case A_int_resfail:
    911 			printf("reselect failed\n");
    912 			CALL_SCRIPT(Ent_script_sched);
    913 			return  1;
    914 		case A_int_done:
    915 			if (xs == NULL) {
    916 				printf("%s: done without command, DSA=0x%lx\n",
    917 				    sc->sc_dev.dv_xname, (u_long)siop_cmd->dsa);
    918 				siop_cmd->status = CMDST_FREE;
    919 				siop_start(sc);
    920 				CALL_SCRIPT(Ent_script_sched);
    921 				return 1;
    922 			}
    923 #ifdef SIOP_DEBUG_INTR
    924 			printf("done, DSA=0x%lx target id 0x%x last msg "
    925 			    "in=0x%x status=0x%x\n", (u_long)siop_cmd->dsa,
    926 			    le32toh(siop_cmd->siop_tables.id),
    927 			    siop_cmd->siop_tables.msg_in[0],
    928 			    le32toh(siop_cmd->siop_tables.status));
    929 #endif
    930 			INCSTAT(siop_stat_intr_done);
    931 			if (siop_cmd->status == CMDST_SENSE_ACTIVE)
    932 				siop_cmd->status = CMDST_SENSE_DONE;
    933 			else
    934 				siop_cmd->status = CMDST_DONE;
    935 			goto end;
    936 		default:
    937 			printf("unknown irqcode %x\n", irqcode);
    938 			if (xs) {
    939 				xs->error = XS_SELTIMEOUT;
    940 				goto end;
    941 			}
    942 			goto reset;
    943 		}
    944 		return 1;
    945 	}
    946 	/* We just should't get there */
    947 	panic("siop_intr: I shouldn't be there !");
    948 	return 1;
    949 end:
    950 	CALL_SCRIPT(Ent_script_sched);
    951 	siop_scsicmd_end(siop_cmd);
    952 	siop_lun->siop_tag[tag].active = NULL;
    953 	if (siop_cmd->status == CMDST_FREE) {
    954 		TAILQ_INSERT_TAIL(&sc->free_list, siop_cmd, next);
    955 		siop_lun->lun_flags &= ~SIOP_LUNF_FULL;
    956 		if (freetarget && siop_target->status == TARST_PROBING)
    957 			siop_del_dev(sc, target, lun);
    958 	}
    959 	siop_start(sc);
    960 	return 1;
    961 }
    962 
    963 void
    964 siop_scsicmd_end(siop_cmd)
    965 	struct siop_cmd *siop_cmd;
    966 {
    967 	struct scsipi_xfer *xs = siop_cmd->xs;
    968 	struct siop_softc *sc = siop_cmd->siop_sc;
    969 
    970 	switch(le32toh(siop_cmd->siop_tables.status)) {
    971 	case SCSI_OK:
    972 		xs->error = (siop_cmd->status == CMDST_DONE) ?
    973 		    XS_NOERROR : XS_SENSE;
    974 		break;
    975 	case SCSI_BUSY:
    976 		xs->error = XS_BUSY;
    977 		break;
    978 	case SCSI_CHECK:
    979 		if (siop_cmd->status == CMDST_SENSE_DONE) {
    980 			/* request sense on a request sense ? */
    981 			printf("request sense failed\n");
    982 			xs->error = XS_DRIVER_STUFFUP;
    983 		} else {
    984 			siop_cmd->status = CMDST_SENSE;
    985 		}
    986 		break;
    987 	case SCSI_QUEUE_FULL:
    988 		{
    989 		struct siop_lun *siop_lun = siop_cmd->siop_target->siop_lun[
    990 		    xs->sc_link->scsipi_scsi.lun];
    991 		/*
    992 		 * device didn't queue the command. We have to
    993 		 * retry it.
    994 		 * We insert it in the urgent list, hoping to preserve order.
    995 		 * But unfortunably, commands already in the scheduler may
    996 		 * be accepted before this one.
    997 		 * Also remember the condition, to avoid starting new commands
    998 		 * for this device before one is done.
    999 		 */
   1000 		INCSTAT(siop_stat_intr_qfull);
   1001 #ifdef SIOP_DEBUG
   1002 		printf("%s:%d:%d: queue full (tag %d)\n", sc->sc_dev.dv_xname,
   1003 		    xs->sc_link->scsipi_scsi.target,
   1004 		    xs->sc_link->scsipi_scsi.lun, siop_cmd->tag);
   1005 #endif
   1006 		callout_stop(&xs->xs_callout);
   1007 		siop_lun->lun_flags |= SIOP_LUNF_FULL;
   1008 		siop_cmd->status = CMDST_READY;
   1009 		siop_setuptables(siop_cmd);
   1010 		TAILQ_INSERT_TAIL(&sc->urgent_list, siop_cmd, next);
   1011 		return;
   1012 		}
   1013 	case SCSI_SIOP_NOCHECK:
   1014 		/*
   1015 		 * don't check status, xs->error is already valid
   1016 		 */
   1017 		break;
   1018 	case SCSI_SIOP_NOSTATUS:
   1019 		/*
   1020 		 * the status byte was not updated, cmd was
   1021 		 * aborted
   1022 		 */
   1023 		xs->error = XS_SELTIMEOUT;
   1024 		break;
   1025 	default:
   1026 		xs->error = XS_DRIVER_STUFFUP;
   1027 	}
   1028 	if (siop_cmd->status != CMDST_SENSE_DONE &&
   1029 	    xs->xs_control & (XS_CTL_DATA_IN | XS_CTL_DATA_OUT)) {
   1030 		bus_dmamap_sync(sc->sc_dmat, siop_cmd->dmamap_data, 0,
   1031 		    siop_cmd->dmamap_data->dm_mapsize,
   1032 		    (xs->xs_control & XS_CTL_DATA_IN) ?
   1033 		    BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
   1034 		bus_dmamap_unload(sc->sc_dmat, siop_cmd->dmamap_data);
   1035 	}
   1036 	bus_dmamap_unload(sc->sc_dmat, siop_cmd->dmamap_cmd);
   1037 	if (siop_cmd->status == CMDST_SENSE) {
   1038 		/* issue a request sense for this target */
   1039 		int error;
   1040 		siop_cmd->rs_cmd.opcode = REQUEST_SENSE;
   1041 		siop_cmd->rs_cmd.byte2 = xs->sc_link->scsipi_scsi.lun << 5;
   1042 		siop_cmd->rs_cmd.unused[0] = siop_cmd->rs_cmd.unused[1] = 0;
   1043 		siop_cmd->rs_cmd.length = sizeof(struct scsipi_sense_data);
   1044 		siop_cmd->rs_cmd.control = 0;
   1045 		siop_cmd->flags &= ~CMDFL_TAG;
   1046 		error = bus_dmamap_load(sc->sc_dmat, siop_cmd->dmamap_cmd,
   1047 		    &siop_cmd->rs_cmd, sizeof(struct scsipi_sense),
   1048 		    NULL, BUS_DMA_NOWAIT);
   1049 		if (error) {
   1050 			printf("%s: unable to load cmd DMA map: %d",
   1051 			    sc->sc_dev.dv_xname, error);
   1052 			xs->error = XS_DRIVER_STUFFUP;
   1053 			goto out;
   1054 		}
   1055 		error = bus_dmamap_load(sc->sc_dmat, siop_cmd->dmamap_data,
   1056 		    &xs->sense.scsi_sense, sizeof(struct  scsipi_sense_data),
   1057 		    NULL, BUS_DMA_NOWAIT);
   1058 		if (error) {
   1059 			printf("%s: unable to load sense DMA map: %d",
   1060 			    sc->sc_dev.dv_xname, error);
   1061 			xs->error = XS_DRIVER_STUFFUP;
   1062 			bus_dmamap_unload(sc->sc_dmat, siop_cmd->dmamap_cmd);
   1063 			goto out;
   1064 		}
   1065 		bus_dmamap_sync(sc->sc_dmat, siop_cmd->dmamap_data, 0,
   1066 		    siop_cmd->dmamap_data->dm_mapsize, BUS_DMASYNC_PREREAD);
   1067 		bus_dmamap_sync(sc->sc_dmat, siop_cmd->dmamap_cmd, 0,
   1068 		    siop_cmd->dmamap_cmd->dm_mapsize, BUS_DMASYNC_PREWRITE);
   1069 
   1070 		siop_setuptables(siop_cmd);
   1071 		/* arrange for the cmd to be handled now */
   1072 		TAILQ_INSERT_HEAD(&sc->urgent_list, siop_cmd, next);
   1073 		return;
   1074 	} else if (siop_cmd->status == CMDST_SENSE_DONE) {
   1075 		bus_dmamap_sync(sc->sc_dmat, siop_cmd->dmamap_data, 0,
   1076 		    siop_cmd->dmamap_data->dm_mapsize, BUS_DMASYNC_POSTREAD);
   1077 		bus_dmamap_unload(sc->sc_dmat, siop_cmd->dmamap_data);
   1078 	}
   1079 out:
   1080 	callout_stop(&siop_cmd->xs->xs_callout);
   1081 	siop_cmd->status = CMDST_FREE;
   1082 	xs->xs_status |= XS_STS_DONE;
   1083 	xs->resid = 0;
   1084 	if ((xs->xs_control & XS_CTL_POLL) == 0)
   1085 		scsipi_done (xs);
   1086 }
   1087 
   1088 /*
   1089  * handle a rejected queue tag message: the command will run untagged,
   1090  * has to adjust the reselect script.
   1091  */
   1092 int
   1093 siop_handle_qtag_reject(siop_cmd)
   1094 	struct siop_cmd *siop_cmd;
   1095 {
   1096 	struct siop_softc *sc = siop_cmd->siop_sc;
   1097 	int target = siop_cmd->xs->sc_link->scsipi_scsi.target;
   1098 	int lun = siop_cmd->xs->sc_link->scsipi_scsi.lun;
   1099 	int tag = siop_cmd->siop_tables.msg_out[2];
   1100 	struct siop_lun *siop_lun = sc->targets[target]->siop_lun[lun];
   1101 
   1102 #ifdef SIOP_DEBUG
   1103 	printf("%s:%d:%d: tag message %d (%d) rejected (status %d)\n",
   1104 	    sc->sc_dev.dv_xname, target, lun, tag, siop_cmd->tag,
   1105 	    siop_cmd->status);
   1106 #endif
   1107 
   1108 	if (siop_lun->siop_tag[0].active != NULL) {
   1109 		printf("%s: untagged command already running for target %d "
   1110 		    "lun %d (status %d)\n", sc->sc_dev.dv_xname, target, lun,
   1111 		    siop_lun->siop_tag[0].active->status);
   1112 		return -1;
   1113 	}
   1114 	/* clear tag slot */
   1115 	siop_lun->siop_tag[tag].active = NULL;
   1116 	/* add command to non-tagged slot */
   1117 	siop_lun->siop_tag[0].active = siop_cmd;
   1118 	siop_cmd->tag = 0;
   1119 	/* adjust reselect script if there is one */
   1120 	if (siop_lun->siop_tag[0].reseloff > 0) {
   1121 		siop_script_write(sc,
   1122 		    siop_lun->siop_tag[0].reseloff + 1,
   1123 		    siop_cmd->dsa + sizeof(struct siop_xfer_common) +
   1124 		    Ent_ldsa_reload_dsa);
   1125 	}
   1126 	return 0;
   1127 }
   1128 
   1129 /*
   1130  * handle a bus reset: reset chip, unqueue all active commands, free all
   1131  * target struct and report loosage to upper layer.
   1132  * As the upper layer may requeue immediatly we have to first store
   1133  * all active commands in a temporary queue.
   1134  */
   1135 void
   1136 siop_handle_reset(sc)
   1137 	struct siop_softc *sc;
   1138 {
   1139 	struct cmd_list reset_list;
   1140 	struct siop_cmd *siop_cmd, *next_siop_cmd;
   1141 	struct siop_lun *siop_lun;
   1142 	int target, lun, tag;
   1143 	/*
   1144 	 * scsi bus reset. reset the chip and restart
   1145 	 * the queue. Need to clean up all active commands
   1146 	 */
   1147 	printf("%s: scsi bus reset\n", sc->sc_dev.dv_xname);
   1148 	/* stop, reset and restart the chip */
   1149 	siop_reset(sc);
   1150 	TAILQ_INIT(&reset_list);
   1151 	/*
   1152 	 * Process all commands: first commmands being executed
   1153 	 */
   1154 	for (target = 0; target <= sc->sc_link.scsipi_scsi.max_target;
   1155 	    target++) {
   1156 		if (sc->targets[target] == NULL)
   1157 			continue;
   1158 		for (lun = 0; lun < 8; lun++) {
   1159 			siop_lun = sc->targets[target]->siop_lun[lun];
   1160 			if (siop_lun == NULL)
   1161 				continue;
   1162 			siop_lun->lun_flags &= ~SIOP_LUNF_FULL;
   1163 			for (tag = 0; tag <
   1164 			    ((sc->targets[target]->flags & TARF_TAG) ?
   1165 			    SIOP_NTAG : 1);
   1166 			    tag++) {
   1167 				siop_cmd = siop_lun->siop_tag[tag].active;
   1168 				if (siop_cmd == NULL)
   1169 					continue;
   1170 				printf("cmd %p (target %d:%d) in reset list\n",
   1171 				    siop_cmd, target, lun);
   1172 				TAILQ_INSERT_TAIL(&reset_list, siop_cmd, next);
   1173 				siop_lun->siop_tag[tag].active = NULL;
   1174 			}
   1175 		}
   1176 		sc->targets[target]->status = TARST_ASYNC;
   1177 		sc->targets[target]->flags &= ~TARF_ISWIDE;
   1178 	}
   1179 	/* Next commands from the urgent list */
   1180 	for (siop_cmd = TAILQ_FIRST(&sc->urgent_list); siop_cmd != NULL;
   1181 	    siop_cmd = next_siop_cmd) {
   1182 		next_siop_cmd = TAILQ_NEXT(siop_cmd, next);
   1183 		siop_cmd->flags &= ~CMDFL_TAG;
   1184 		printf("cmd %p (target %d:%d) in reset list (wait)\n",
   1185 		    siop_cmd, siop_cmd->xs->sc_link->scsipi_scsi.target,
   1186 		    siop_cmd->xs->sc_link->scsipi_scsi.lun);
   1187 		TAILQ_REMOVE(&sc->urgent_list, siop_cmd, next);
   1188 		TAILQ_INSERT_TAIL(&reset_list, siop_cmd, next);
   1189 	}
   1190 	/* Then command waiting in the input list */
   1191 	for (siop_cmd = TAILQ_FIRST(&sc->ready_list); siop_cmd != NULL;
   1192 	    siop_cmd = next_siop_cmd) {
   1193 		next_siop_cmd = TAILQ_NEXT(siop_cmd, next);
   1194 		siop_cmd->flags &= ~CMDFL_TAG;
   1195 		printf("cmd %p (target %d:%d) in reset list (wait)\n",
   1196 		    siop_cmd, siop_cmd->xs->sc_link->scsipi_scsi.target,
   1197 		    siop_cmd->xs->sc_link->scsipi_scsi.lun);
   1198 		TAILQ_REMOVE(&sc->ready_list, siop_cmd, next);
   1199 		TAILQ_INSERT_TAIL(&reset_list, siop_cmd, next);
   1200 	}
   1201 
   1202 	for (siop_cmd = TAILQ_FIRST(&reset_list); siop_cmd != NULL;
   1203 	    siop_cmd = next_siop_cmd) {
   1204 		next_siop_cmd = TAILQ_NEXT(siop_cmd, next);
   1205 		siop_cmd->xs->error = (siop_cmd->flags & CMDFL_TIMEOUT) ?
   1206 		    XS_TIMEOUT : XS_RESET;
   1207 		siop_cmd->siop_tables.status = htole32(SCSI_SIOP_NOCHECK);
   1208 		printf("cmd %p (status %d) about to be processed\n", siop_cmd,
   1209 		    siop_cmd->status);
   1210 		if (siop_cmd->status == CMDST_SENSE ||
   1211 		    siop_cmd->status == CMDST_SENSE_ACTIVE)
   1212 			siop_cmd->status = CMDST_SENSE_DONE;
   1213 		else
   1214 			siop_cmd->status = CMDST_DONE;
   1215 		TAILQ_REMOVE(&reset_list, siop_cmd, next);
   1216 		siop_scsicmd_end(siop_cmd);
   1217 		TAILQ_INSERT_TAIL(&sc->free_list, siop_cmd, next);
   1218 	}
   1219 }
   1220 
   1221 int
   1222 siop_scsicmd(xs)
   1223 	struct scsipi_xfer *xs;
   1224 {
   1225 	struct siop_softc *sc = (struct siop_softc *)xs->sc_link->adapter_softc;
   1226 	struct siop_cmd *siop_cmd;
   1227 	int s, error, i;
   1228 	const int target = xs->sc_link->scsipi_scsi.target;
   1229 	const int lun = xs->sc_link->scsipi_scsi.lun;
   1230 
   1231 	s = splbio();
   1232 #ifdef SIOP_DEBUG_SCHED
   1233 	printf("starting cmd for %d:%d\n", target, lun);
   1234 #endif
   1235 	siop_cmd = TAILQ_FIRST(&sc->free_list);
   1236 	if (siop_cmd) {
   1237 		TAILQ_REMOVE(&sc->free_list, siop_cmd, next);
   1238 	} else {
   1239 		if (siop_morecbd(sc) == 0) {
   1240 			siop_cmd = TAILQ_FIRST(&sc->free_list);
   1241 #ifdef DIAGNOSTIC
   1242 			if (siop_cmd == NULL)
   1243 				panic("siop_morecbd succeed and does nothing");
   1244 #endif
   1245 			TAILQ_REMOVE(&sc->free_list, siop_cmd, next);
   1246 		}
   1247 	}
   1248 	if (siop_cmd == NULL) {
   1249 		xs->error = XS_DRIVER_STUFFUP;
   1250 		splx(s);
   1251 		return(TRY_AGAIN_LATER);
   1252 	}
   1253 #ifdef DIAGNOSTIC
   1254 	if (siop_cmd->status != CMDST_FREE)
   1255 		panic("siop_scsicmd: new cmd not free");
   1256 #endif
   1257 	if (sc->targets[target] == NULL) {
   1258 #ifdef SIOP_DEBUG
   1259 		printf("%s: alloc siop_target for target %d\n",
   1260 			sc->sc_dev.dv_xname, target);
   1261 #endif
   1262 		sc->targets[target] =
   1263 		    malloc(sizeof(struct siop_target), M_DEVBUF, M_NOWAIT);
   1264 		if (sc->targets[target] == NULL) {
   1265 			printf("%s: can't malloc memory for target %d\n",
   1266 			    sc->sc_dev.dv_xname, target);
   1267 			xs->error = XS_DRIVER_STUFFUP;
   1268 			splx(s);
   1269 			return(TRY_AGAIN_LATER);
   1270 		}
   1271 		sc->targets[target]->status = TARST_PROBING;
   1272 		sc->targets[target]->flags = 0;
   1273 		sc->targets[target]->id = sc->clock_div << 24; /* scntl3 */
   1274 		sc->targets[target]->id |=  target << 16; /* id */
   1275 		/* sc->targets[target]->id |= 0x0 << 8; scxfer is 0 */
   1276 
   1277 		/* get a lun switch script */
   1278 		sc->targets[target]->lunsw = siop_get_lunsw(sc);
   1279 		if (sc->targets[target]->lunsw == NULL) {
   1280 			printf("%s: can't alloc lunsw for target %d\n",
   1281 			    sc->sc_dev.dv_xname, target);
   1282 			xs->error = XS_DRIVER_STUFFUP;
   1283 			splx(s);
   1284 			return(TRY_AGAIN_LATER);
   1285 		}
   1286 		for (i=0; i < 8; i++)
   1287 			sc->targets[target]->siop_lun[i] = NULL;
   1288 		siop_add_reselsw(sc, target);
   1289 	}
   1290 	if (sc->targets[target]->siop_lun[lun] == NULL) {
   1291 		sc->targets[target]->siop_lun[lun] =
   1292 		    malloc(sizeof(struct siop_lun), M_DEVBUF, M_NOWAIT);
   1293 		if (sc->targets[target]->siop_lun[lun] == NULL) {
   1294 			printf("%s: can't alloc siop_lun for target %d "
   1295 			    "lun %d\n", sc->sc_dev.dv_xname, target, lun);
   1296 			xs->error = XS_DRIVER_STUFFUP;
   1297 			splx(s);
   1298 			return(TRY_AGAIN_LATER);
   1299 		}
   1300 		memset(sc->targets[target]->siop_lun[lun], 0,
   1301 		    sizeof(struct siop_lun));
   1302 	}
   1303 	siop_cmd->siop_target = sc->targets[target];
   1304 	siop_cmd->xs = xs;
   1305 	siop_cmd->flags = 0;
   1306 	siop_cmd->status = CMDST_READY;
   1307 
   1308 	/* load the DMA maps */
   1309 	error = bus_dmamap_load(sc->sc_dmat, siop_cmd->dmamap_cmd,
   1310 	    xs->cmd, xs->cmdlen, NULL, BUS_DMA_NOWAIT);
   1311 	if (error) {
   1312 		printf("%s: unable to load cmd DMA map: %d",
   1313 		    sc->sc_dev.dv_xname, error);
   1314 		xs->error = XS_DRIVER_STUFFUP;
   1315 		splx(s);
   1316 		return(TRY_AGAIN_LATER);
   1317 	}
   1318 	if (xs->xs_control & (XS_CTL_DATA_IN | XS_CTL_DATA_OUT)) {
   1319 		error = bus_dmamap_load(sc->sc_dmat, siop_cmd->dmamap_data,
   1320 		    xs->data, xs->datalen, NULL, BUS_DMA_NOWAIT);
   1321 		if (error) {
   1322 			printf("%s: unable to load cmd DMA map: %d",
   1323 			    sc->sc_dev.dv_xname, error);
   1324 			xs->error = XS_DRIVER_STUFFUP;
   1325 			bus_dmamap_unload(sc->sc_dmat, siop_cmd->dmamap_cmd);
   1326 			splx(s);
   1327 			return(TRY_AGAIN_LATER);
   1328 		}
   1329 		bus_dmamap_sync(sc->sc_dmat, siop_cmd->dmamap_data, 0,
   1330 		    siop_cmd->dmamap_data->dm_mapsize,
   1331 		    (xs->xs_control & XS_CTL_DATA_IN) ?
   1332 		    BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
   1333 	}
   1334 	bus_dmamap_sync(sc->sc_dmat, siop_cmd->dmamap_cmd, 0,
   1335 	    siop_cmd->dmamap_cmd->dm_mapsize, BUS_DMASYNC_PREWRITE);
   1336 
   1337 	siop_setuptables(siop_cmd);
   1338 
   1339 	TAILQ_INSERT_TAIL(&sc->ready_list, siop_cmd, next);
   1340 	siop_start(sc);
   1341 	if (xs->xs_control & XS_CTL_POLL) {
   1342 		/* poll for command completion */
   1343 		while ((xs->xs_status & XS_STS_DONE) == 0) {
   1344 			delay(1000);
   1345 			siop_intr(sc);
   1346 		}
   1347 		splx(s);
   1348 		return (COMPLETE);
   1349 	}
   1350 	splx(s);
   1351 	return (SUCCESSFULLY_QUEUED);
   1352 }
   1353 
   1354 void
   1355 siop_start(sc)
   1356 	struct siop_softc *sc;
   1357 {
   1358 	struct siop_cmd *siop_cmd, *next_siop_cmd;
   1359 	struct siop_lun *siop_lun;
   1360 	u_int32_t dsa;
   1361 	int timeout;
   1362 	int target, lun, tag, slot;
   1363 	int newcmd = 0;
   1364 	int doingready = 0;
   1365 
   1366 	/*
   1367 	 * first make sure to read valid data
   1368 	 */
   1369 	siop_script_sync(sc, BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
   1370 
   1371 	/*
   1372 	 * The queue management here is a bit tricky: the script always looks
   1373 	 * at the slot from first to last, so if we always use the first
   1374 	 * free slot commands can stay at the tail of the queue ~forever.
   1375 	 * The algorithm used here is to restart from the head when we know
   1376 	 * that the queue is empty, and only add commands after the last one.
   1377 	 * When we're at the end of the queue wait for the script to clear it.
   1378 	 * The best thing to do here would be to implement a circular queue,
   1379 	 * but using only 53c720 features this can be "interesting".
   1380 	 * A mid-way solution could be to implement 2 queues and swap orders.
   1381 	 */
   1382 	slot = sc->sc_currschedslot;
   1383 	/*
   1384 	 * If the instruction is 0x80000000 (JUMP foo, IF FALSE) the slot is
   1385 	 * free. As this is the last used slot, all previous slots are free,
   1386 	 * we can restart from 0.
   1387 	 */
   1388 	if (siop_script_read(sc, (Ent_script_sched_slot0 / 4) + slot * 2) ==
   1389 	    0x80000000) {
   1390 		slot = sc->sc_currschedslot = 0;
   1391 	} else {
   1392 		slot++;
   1393 	}
   1394 	/* first handle commands from the urgent list */
   1395 	siop_cmd = TAILQ_FIRST(&sc->urgent_list);
   1396 again:
   1397 	for (; siop_cmd != NULL; siop_cmd = next_siop_cmd) {
   1398 		next_siop_cmd = TAILQ_NEXT(siop_cmd, next);
   1399 #ifdef DIAGNOSTIC
   1400 		if (siop_cmd->status != CMDST_READY &&
   1401 		    siop_cmd->status != CMDST_SENSE)
   1402 			panic("siop: non-ready cmd in ready list");
   1403 #endif
   1404 		target = siop_cmd->xs->sc_link->scsipi_scsi.target;
   1405 		lun = siop_cmd->xs->sc_link->scsipi_scsi.lun;
   1406 		siop_lun = sc->targets[target]->siop_lun[lun];
   1407 		/* if non-tagged command active, wait */
   1408 		if (siop_lun->siop_tag[0].active != NULL)
   1409 			continue;
   1410 		/*
   1411 		 * if we're in a queue full condition don't start a new
   1412 		 * command, unless it's a request sense
   1413 		 */
   1414 		if ((siop_lun->lun_flags & SIOP_LUNF_FULL) &&
   1415 		    siop_cmd->status == CMDST_READY)
   1416 			continue;
   1417 		/* find a free tag if needed */
   1418 		if (siop_cmd->flags & CMDFL_TAG) {
   1419 			for (tag = 1; tag < SIOP_NTAG; tag++) {
   1420 				if (siop_lun->siop_tag[tag].active == NULL)
   1421 					break;
   1422 			}
   1423 			if (tag == SIOP_NTAG) /* no free tag */
   1424 				continue;
   1425 		} else {
   1426 			tag = 0;
   1427 		}
   1428 		siop_cmd->tag = tag;
   1429 		/* find a free scheduler slot and load it */
   1430 		for (; slot < SIOP_NSLOTS; slot++) {
   1431 			/*
   1432 			 * If cmd if 0x80000000 the slot is free
   1433 			 */
   1434 			if (siop_script_read(sc,
   1435 			    (Ent_script_sched_slot0 / 4) + slot * 2) ==
   1436 			    0x80000000)
   1437 				break;
   1438 		}
   1439 		/* no more free slot, no need to continue */
   1440 		if (slot == SIOP_NSLOTS) {
   1441 			goto end;
   1442 		}
   1443 #ifdef SIOP_DEBUG_SCHED
   1444 		printf("using slot %d for DSA 0x%lx\n", slot,
   1445 		    (u_long)siop_cmd->dsa);
   1446 #endif
   1447 		/* Ok, we can add the tag message */
   1448 		if (tag > 0) {
   1449 #ifdef DIAGNOSTIC
   1450 			int msgcount =
   1451 			    le32toh(siop_cmd->siop_tables.t_msgout.count);
   1452 			if (msgcount != 1)
   1453 				printf("%s:%d:%d: tag %d with msgcount %d\n",
   1454 				    sc->sc_dev.dv_xname, target, lun, tag,
   1455 				    msgcount);
   1456 #endif
   1457 			if (siop_cmd->xs->bp != NULL &&
   1458 			    (siop_cmd->xs->bp->b_flags & B_ASYNC))
   1459 				siop_cmd->siop_tables.msg_out[1] =
   1460 				    MSG_SIMPLE_Q_TAG;
   1461 			else
   1462 				siop_cmd->siop_tables.msg_out[1] =
   1463 				    MSG_ORDERED_Q_TAG;
   1464 			siop_cmd->siop_tables.msg_out[2] = tag;
   1465 			siop_cmd->siop_tables.t_msgout.count = htole32(3);
   1466 		}
   1467 		/* note that we started a new command */
   1468 		newcmd = 1;
   1469 		/* mark command as active */
   1470 		if (siop_cmd->status == CMDST_READY) {
   1471 			siop_cmd->status = CMDST_ACTIVE;
   1472 		} else if (siop_cmd->status == CMDST_SENSE) {
   1473 			siop_cmd->status = CMDST_SENSE_ACTIVE;
   1474 		} else
   1475 			panic("siop_start: bad status");
   1476 		if (doingready)
   1477 			TAILQ_REMOVE(&sc->ready_list, siop_cmd, next);
   1478 		else
   1479 			TAILQ_REMOVE(&sc->urgent_list, siop_cmd, next);
   1480 		siop_lun->siop_tag[tag].active = siop_cmd;
   1481 		/* patch scripts with DSA addr */
   1482 		dsa = siop_cmd->dsa;
   1483 		/* first reselect switch, if we have an entry */
   1484 		if (siop_lun->siop_tag[tag].reseloff > 0)
   1485 			siop_script_write(sc,
   1486 			    siop_lun->siop_tag[tag].reseloff + 1,
   1487 			    dsa + sizeof(struct siop_xfer_common) +
   1488 			    Ent_ldsa_reload_dsa);
   1489 		/* CMD script: MOVE MEMORY addr */
   1490 		siop_cmd->siop_xfer->resel[E_ldsa_abs_slot_Used[0]] =
   1491 		   htole32(sc->sc_scriptaddr + Ent_script_sched_slot0 +
   1492 		   slot * 8);
   1493 		/* scheduler slot: JUMP ldsa_select */
   1494 		siop_script_write(sc,
   1495 		    (Ent_script_sched_slot0 / 4) + slot * 2 + 1,
   1496 		    dsa + sizeof(struct siop_xfer_common) + Ent_ldsa_select);
   1497 		/* handle timeout */
   1498 		if (siop_cmd->status == CMDST_ACTIVE) {
   1499 			if ((siop_cmd->xs->xs_control &
   1500 			    XS_CTL_POLL) == 0) {
   1501 				/* start exire timer */
   1502 				timeout = (u_int64_t) siop_cmd->xs->timeout *
   1503 				    (u_int64_t)hz / 1000;
   1504 				if (timeout == 0)
   1505 					timeout = 1;
   1506 				callout_reset( &siop_cmd->xs->xs_callout,
   1507 				    timeout, siop_timeout, siop_cmd);
   1508 			}
   1509 		}
   1510 		/*
   1511 		 * Change JUMP cmd so that this slot will be handled
   1512 		 */
   1513 		siop_script_write(sc, (Ent_script_sched_slot0 / 4) + slot * 2,
   1514 		    0x80080000);
   1515 		sc->sc_currschedslot = slot;
   1516 		slot++;
   1517 	}
   1518 	if (doingready == 0) {
   1519 		/* now process ready list */
   1520 		doingready = 1;
   1521 		siop_cmd = TAILQ_FIRST(&sc->ready_list);
   1522 		goto again;
   1523 	}
   1524 
   1525 end:
   1526 	/* if nothing changed no need to flush cache and wakeup script */
   1527 	if (newcmd == 0)
   1528 		return;
   1529 	/* make sure SCRIPT processor will read valid data */
   1530 	siop_script_sync(sc,BUS_DMASYNC_PREREAD |  BUS_DMASYNC_PREWRITE);
   1531 	/* Signal script it has some work to do */
   1532 	bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_ISTAT, ISTAT_SIGP);
   1533 	/* and wait for IRQ */
   1534 	return;
   1535 }
   1536 
   1537 void
   1538 siop_timeout(v)
   1539 	void *v;
   1540 {
   1541 	struct siop_cmd *siop_cmd = v;
   1542 	struct siop_softc *sc = siop_cmd->siop_sc;
   1543 	int s;
   1544 
   1545 	scsi_print_addr(siop_cmd->xs->sc_link);
   1546 	printf("command timeout\n");
   1547 
   1548 	s = splbio();
   1549 	/* reset the scsi bus */
   1550 	siop_resetbus(sc);
   1551 
   1552 	/* deactivate callout */
   1553 	callout_stop(&siop_cmd->xs->xs_callout);
   1554 	/* mark command as being timed out; siop_intr will handle it */
   1555 	/*
   1556 	 * mark command has being timed out and just return;
   1557 	 * the bus reset will generate an interrupt,
   1558 	 * it will be handled in siop_intr()
   1559 	 */
   1560 	siop_cmd->flags |= CMDFL_TIMEOUT;
   1561 	splx(s);
   1562 	return;
   1563 
   1564 }
   1565 
   1566 void
   1567 siop_dump_script(sc)
   1568 	struct siop_softc *sc;
   1569 {
   1570 	int i;
   1571 	for (i = 0; i < PAGE_SIZE / 4; i += 2) {
   1572 		printf("0x%04x: 0x%08x 0x%08x", i * 4,
   1573 		    le32toh(sc->sc_script[i]), le32toh(sc->sc_script[i+1]));
   1574 		if ((le32toh(sc->sc_script[i]) & 0xe0000000) == 0xc0000000) {
   1575 			i++;
   1576 			printf(" 0x%08x", le32toh(sc->sc_script[i+1]));
   1577 		}
   1578 		printf("\n");
   1579 	}
   1580 }
   1581 
   1582 int
   1583 siop_morecbd(sc)
   1584 	struct siop_softc *sc;
   1585 {
   1586 	int error, i, j;
   1587 	bus_dma_segment_t seg;
   1588 	int rseg;
   1589 	struct siop_cbd *newcbd;
   1590 	bus_addr_t dsa;
   1591 	u_int32_t *scr;
   1592 
   1593 	/* allocate a new list head */
   1594 	newcbd = malloc(sizeof(struct siop_cbd), M_DEVBUF, M_NOWAIT);
   1595 	if (newcbd == NULL) {
   1596 		printf("%s: can't allocate memory for command descriptors "
   1597 		    "head\n", sc->sc_dev.dv_xname);
   1598 		return ENOMEM;
   1599 	}
   1600 	memset(newcbd, 0, sizeof(struct siop_cbd));
   1601 
   1602 	/* allocate cmd list */
   1603 	newcbd->cmds =
   1604 	    malloc(sizeof(struct siop_cmd) * SIOP_NCMDPB, M_DEVBUF, M_NOWAIT);
   1605 	if (newcbd->cmds == NULL) {
   1606 		printf("%s: can't allocate memory for command descriptors\n",
   1607 		    sc->sc_dev.dv_xname);
   1608 		error = ENOMEM;
   1609 		goto bad3;
   1610 	}
   1611 	memset(newcbd->cmds, 0, sizeof(struct siop_cmd) * SIOP_NCMDPB);
   1612 	error = bus_dmamem_alloc(sc->sc_dmat, PAGE_SIZE, PAGE_SIZE, 0, &seg,
   1613 	    1, &rseg, BUS_DMA_NOWAIT);
   1614 	if (error) {
   1615 		printf("%s: unable to allocate cbd DMA memory, error = %d\n",
   1616 		    sc->sc_dev.dv_xname, error);
   1617 		goto bad2;
   1618 	}
   1619 	error = bus_dmamem_map(sc->sc_dmat, &seg, rseg, PAGE_SIZE,
   1620 	    (caddr_t *)&newcbd->xfers, BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
   1621 	if (error) {
   1622 		printf("%s: unable to map cbd DMA memory, error = %d\n",
   1623 		    sc->sc_dev.dv_xname, error);
   1624 		goto bad2;
   1625 	}
   1626 	error = bus_dmamap_create(sc->sc_dmat, PAGE_SIZE, 1, PAGE_SIZE, 0,
   1627 	    BUS_DMA_NOWAIT, &newcbd->xferdma);
   1628 	if (error) {
   1629 		printf("%s: unable to create cbd DMA map, error = %d\n",
   1630 		    sc->sc_dev.dv_xname, error);
   1631 		goto bad1;
   1632 	}
   1633 	error = bus_dmamap_load(sc->sc_dmat, newcbd->xferdma, newcbd->xfers,
   1634 	    PAGE_SIZE, NULL, BUS_DMA_NOWAIT);
   1635 	if (error) {
   1636 		printf("%s: unable to load cbd DMA map, error = %d\n",
   1637 		    sc->sc_dev.dv_xname, error);
   1638 		goto bad0;
   1639 	}
   1640 #ifdef DEBUG
   1641 	printf("%s: alloc newcdb at PHY addr 0x%lx\n", sc->sc_dev.dv_xname,
   1642 	    (unsigned long)newcbd->xferdma->dm_segs[0].ds_addr);
   1643 #endif
   1644 
   1645 	for (i = 0; i < SIOP_NCMDPB; i++) {
   1646 		error = bus_dmamap_create(sc->sc_dmat, MAXPHYS, SIOP_NSG,
   1647 		    MAXPHYS, 0, BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,
   1648 		    &newcbd->cmds[i].dmamap_data);
   1649 		if (error) {
   1650 			printf("%s: unable to create data DMA map for cbd: "
   1651 			    "error %d\n",
   1652 			    sc->sc_dev.dv_xname, error);
   1653 			goto bad0;
   1654 		}
   1655 		error = bus_dmamap_create(sc->sc_dmat,
   1656 		    sizeof(struct scsipi_generic), 1,
   1657 		    sizeof(struct scsipi_generic), 0,
   1658 		    BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,
   1659 		    &newcbd->cmds[i].dmamap_cmd);
   1660 		if (error) {
   1661 			printf("%s: unable to create cmd DMA map for cbd %d\n",
   1662 			    sc->sc_dev.dv_xname, error);
   1663 			goto bad0;
   1664 		}
   1665 		newcbd->cmds[i].siop_sc = sc;
   1666 		newcbd->cmds[i].siop_cbdp = newcbd;
   1667 		newcbd->cmds[i].siop_xfer = &newcbd->xfers[i];
   1668 		memset(newcbd->cmds[i].siop_xfer, 0,
   1669 		    sizeof(struct siop_xfer));
   1670 		newcbd->cmds[i].dsa = newcbd->xferdma->dm_segs[0].ds_addr +
   1671 		    i * sizeof(struct siop_xfer);
   1672 		dsa = newcbd->cmds[i].dsa;
   1673 		newcbd->cmds[i].status = CMDST_FREE;
   1674 		newcbd->cmds[i].siop_tables.t_msgout.count= htole32(1);
   1675 		newcbd->cmds[i].siop_tables.t_msgout.addr = htole32(dsa);
   1676 		newcbd->cmds[i].siop_tables.t_msgin.count= htole32(1);
   1677 		newcbd->cmds[i].siop_tables.t_msgin.addr = htole32(dsa + 8);
   1678 		newcbd->cmds[i].siop_tables.t_extmsgin.count= htole32(2);
   1679 		newcbd->cmds[i].siop_tables.t_extmsgin.addr = htole32(dsa + 9);
   1680 		newcbd->cmds[i].siop_tables.t_extmsgdata.addr =
   1681 		    htole32(dsa + 11);
   1682 		newcbd->cmds[i].siop_tables.t_status.count= htole32(1);
   1683 		newcbd->cmds[i].siop_tables.t_status.addr = htole32(dsa + 16);
   1684 
   1685 		/* The select/reselect script */
   1686 		scr = &newcbd->cmds[i].siop_xfer->resel[0];
   1687 		for (j = 0; j < sizeof(load_dsa) / sizeof(load_dsa[0]); j++)
   1688 			scr[j] = htole32(load_dsa[j]);
   1689 		/*
   1690 		 * 0x78000000 is a 'move data8 to reg'. data8 is the second
   1691 		 * octet, reg offset is the third.
   1692 		 */
   1693 		scr[Ent_rdsa0 / 4] =
   1694 		    htole32(0x78100000 | ((dsa & 0x000000ff) <<  8));
   1695 		scr[Ent_rdsa1 / 4] =
   1696 		    htole32(0x78110000 | ( dsa & 0x0000ff00       ));
   1697 		scr[Ent_rdsa2 / 4] =
   1698 		    htole32(0x78120000 | ((dsa & 0x00ff0000) >>  8));
   1699 		scr[Ent_rdsa3 / 4] =
   1700 		    htole32(0x78130000 | ((dsa & 0xff000000) >> 16));
   1701 		scr[E_ldsa_abs_reselected_Used[0]] =
   1702 		    htole32(sc->sc_scriptaddr + Ent_reselected);
   1703 		scr[E_ldsa_abs_reselect_Used[0]] =
   1704 		    htole32(sc->sc_scriptaddr + Ent_reselect);
   1705 		scr[E_ldsa_abs_selected_Used[0]] =
   1706 		    htole32(sc->sc_scriptaddr + Ent_selected);
   1707 		scr[E_ldsa_abs_data_Used[0]] =
   1708 		    htole32(dsa + sizeof(struct siop_xfer_common) +
   1709 		    Ent_ldsa_data);
   1710 		/* JUMP foo, IF FALSE - used by MOVE MEMORY to clear the slot */
   1711 		scr[Ent_ldsa_data / 4] = htole32(0x80000000);
   1712 		TAILQ_INSERT_TAIL(&sc->free_list, &newcbd->cmds[i], next);
   1713 #ifdef SIOP_DEBUG
   1714 		printf("tables[%d]: in=0x%x out=0x%x status=0x%x\n", i,
   1715 		    le32toh(newcbd->cmds[i].siop_tables.t_msgin.addr),
   1716 		    le32toh(newcbd->cmds[i].siop_tables.t_msgout.addr),
   1717 		    le32toh(newcbd->cmds[i].siop_tables.t_status.addr));
   1718 #endif
   1719 	}
   1720 	TAILQ_INSERT_TAIL(&sc->cmds, newcbd, next);
   1721 	return 0;
   1722 bad0:
   1723 	bus_dmamap_destroy(sc->sc_dmat, newcbd->xferdma);
   1724 bad1:
   1725 	bus_dmamem_free(sc->sc_dmat, &seg, rseg);
   1726 bad2:
   1727 	free(newcbd->cmds, M_DEVBUF);
   1728 bad3:
   1729 	free(newcbd, M_DEVBUF);
   1730 	return error;
   1731 }
   1732 
   1733 struct siop_lunsw *
   1734 siop_get_lunsw(sc)
   1735 	struct siop_softc *sc;
   1736 {
   1737 	struct siop_lunsw *lunsw;
   1738 	int i;
   1739 
   1740 	if (sc->script_free_lo + (sizeof(lun_switch) / sizeof(lun_switch[0])) >=
   1741 	    sc->script_free_hi)
   1742 		return NULL;
   1743 	lunsw = TAILQ_FIRST(&sc->lunsw_list);
   1744 	if (lunsw != NULL) {
   1745 #ifdef SIOP_DEBUG
   1746 		printf("siop_get_lunsw got lunsw at offset %d\n",
   1747 		    lunsw->lunsw_off);
   1748 #endif
   1749 		TAILQ_REMOVE(&sc->lunsw_list, lunsw, next);
   1750 		return lunsw;
   1751 	}
   1752 	lunsw = malloc(sizeof(struct siop_lunsw), M_DEVBUF, M_NOWAIT);
   1753 	if (lunsw == NULL)
   1754 		return NULL;
   1755 	memset(lunsw, 0, sizeof(struct siop_lunsw));
   1756 #ifdef SIOP_DEBUG
   1757 	printf("allocating lunsw at offset %d\n", sc->script_free_lo);
   1758 #endif
   1759 	if (sc->features & SF_CHIP_RAM) {
   1760 		bus_space_write_region_4(sc->sc_ramt, sc->sc_ramh,
   1761 		    sc->script_free_lo * 4, lun_switch,
   1762 		    sizeof(lun_switch) / sizeof(lun_switch[0]));
   1763 		bus_space_write_4(sc->sc_ramt, sc->sc_ramh,
   1764 		    (sc->script_free_lo + E_abs_lunsw_return_Used[0]) * 4,
   1765 		    sc->sc_scriptaddr + Ent_lunsw_return);
   1766 	} else {
   1767 		for (i = 0; i < sizeof(lun_switch) / sizeof(lun_switch[0]);
   1768 		    i++)
   1769 			sc->sc_script[sc->script_free_lo + i] =
   1770 			    htole32(lun_switch[i]);
   1771 		sc->sc_script[sc->script_free_lo + E_abs_lunsw_return_Used[0]] =
   1772 		    htole32(sc->sc_scriptaddr + Ent_lunsw_return);
   1773 	}
   1774 	lunsw->lunsw_off = sc->script_free_lo;
   1775 	lunsw->lunsw_size = sizeof(lun_switch) / sizeof(lun_switch[0]);
   1776 	sc->script_free_lo += lunsw->lunsw_size;
   1777 	if (sc->script_free_lo > 1024)
   1778 		printf("%s: script_free_lo (%d) > 1024\n", sc->sc_dev.dv_xname,
   1779 		    sc->script_free_lo);
   1780 	siop_script_sync(sc, BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   1781 	return lunsw;
   1782 }
   1783 
   1784 void
   1785 siop_add_reselsw(sc, target)
   1786 	struct siop_softc *sc;
   1787 	int target;
   1788 {
   1789 	int i;
   1790 	struct siop_lun *siop_lun;
   1791 	/*
   1792 	 * add an entry to resel switch
   1793 	 */
   1794 	siop_script_sync(sc, BUS_DMASYNC_POSTWRITE);
   1795 	for (i = 0; i < 15; i++) {
   1796 		sc->targets[target]->reseloff = Ent_resel_targ0 / 4 + i * 2;
   1797 		if ((siop_script_read(sc, sc->targets[target]->reseloff) & 0xff)
   1798 		    == 0xff) { /* it's free */
   1799 #ifdef SIOP_DEBUG
   1800 			printf("siop: target %d slot %d offset %d\n",
   1801 			    target, i, sc->targets[target]->reseloff);
   1802 #endif
   1803 			/* JUMP abs_foo, IF target | 0x80; */
   1804 			siop_script_write(sc, sc->targets[target]->reseloff,
   1805 			    0x800c0080 | target);
   1806 			siop_script_write(sc, sc->targets[target]->reseloff + 1,
   1807 			    sc->sc_scriptaddr +
   1808 			    sc->targets[target]->lunsw->lunsw_off * 4 +
   1809 			    Ent_lun_switch_entry);
   1810 			break;
   1811 		}
   1812 	}
   1813 	if (i == 15) /* no free slot, shouldn't happen */
   1814 		panic("siop: resel switch full");
   1815 
   1816 	sc->sc_ntargets++;
   1817 	for (i = 0; i < 8; i++) {
   1818 		siop_lun = sc->targets[target]->siop_lun[i];
   1819 		if (siop_lun == NULL)
   1820 			continue;
   1821 		if (siop_lun->reseloff > 0) {
   1822 			siop_lun->reseloff = 0;
   1823 			siop_add_dev(sc, target, i);
   1824 		}
   1825 	}
   1826 	siop_update_scntl3(sc, sc->targets[target]);
   1827 	siop_script_sync(sc, BUS_DMASYNC_PREWRITE);
   1828 }
   1829 
   1830 void
   1831 siop_update_scntl3(sc, siop_target)
   1832 	struct siop_softc *sc;
   1833 	struct siop_target *siop_target;
   1834 {
   1835 	/* MOVE target->id >> 24 TO SCNTL3 */
   1836 	siop_script_write(sc,
   1837 	    siop_target->lunsw->lunsw_off + (Ent_restore_scntl3 / 4),
   1838 	    0x78030000 | ((siop_target->id >> 16) & 0x0000ff00));
   1839 	/* MOVE target->id >> 8 TO SXFER */
   1840 	siop_script_write(sc,
   1841 	    siop_target->lunsw->lunsw_off + (Ent_restore_scntl3 / 4) + 2,
   1842 	    0x78050000 | (siop_target->id & 0x0000ff00));
   1843 	siop_script_sync(sc, BUS_DMASYNC_PREWRITE);
   1844 }
   1845 
   1846 void
   1847 siop_add_dev(sc, target, lun)
   1848 	struct siop_softc *sc;
   1849 	int target;
   1850 	int lun;
   1851 {
   1852 	struct siop_lunsw *lunsw;
   1853 	struct siop_lun *siop_lun = sc->targets[target]->siop_lun[lun];
   1854 	int i, ntargets;
   1855 
   1856 	if (siop_lun->reseloff > 0)
   1857 		return;
   1858 	lunsw = sc->targets[target]->lunsw;
   1859 	if ((lunsw->lunsw_off + lunsw->lunsw_size) < sc->script_free_lo) {
   1860 		/*
   1861 		 * can't extend this slot. Probably not worth trying to deal
   1862 		 * with this case
   1863 		 */
   1864 #ifdef DEBUG
   1865 		printf("%s:%d:%d: can't allocate a lun sw slot\n",
   1866 		    sc->sc_dev.dv_xname, target, lun);
   1867 #endif
   1868 		return;
   1869 	}
   1870 	/* count how many free targets we still have to probe */
   1871 	ntargets =  sc->sc_link.scsipi_scsi.max_target - 1 - sc->sc_ntargets;
   1872 
   1873 	/*
   1874 	 * we need 8 bytes for the lun sw additionnal entry, and
   1875 	 * eventually sizeof(tag_switch) for the tag switch entry.
   1876 	 * Keep enouth free space for the free targets that could be
   1877 	 * probed later.
   1878 	 */
   1879 	if (sc->script_free_lo + 2 +
   1880 	    (ntargets * sizeof(lun_switch) / sizeof(lun_switch[0])) >=
   1881 	    ((sc->targets[target]->flags & TARF_TAG) ?
   1882 	    sc->script_free_hi - (sizeof(tag_switch) / sizeof(tag_switch[0])) :
   1883 	    sc->script_free_hi)) {
   1884 		/*
   1885 		 * not enouth space, probably not worth dealing with it.
   1886 		 * We can hold 13 tagged-queuing capable devices in the 4k RAM.
   1887 		 */
   1888 #ifdef DEBUG
   1889 		printf("%s:%d:%d: not enouth memory for a lun sw slot\n",
   1890 		    sc->sc_dev.dv_xname, target, lun);
   1891 #endif
   1892 		return;
   1893 	}
   1894 #ifdef SIOP_DEBUG
   1895 	printf("%s:%d:%d: allocate lun sw entry\n",
   1896 	    sc->sc_dev.dv_xname, target, lun);
   1897 #endif
   1898 	/* INT int_resellun */
   1899 	siop_script_write(sc, sc->script_free_lo, 0x98080000);
   1900 	siop_script_write(sc, sc->script_free_lo + 1, A_int_resellun);
   1901 	/* Now the slot entry: JUMP abs_foo, IF lun */
   1902 	siop_script_write(sc, sc->script_free_lo - 2,
   1903 	    0x800c0000 | lun);
   1904 	siop_script_write(sc, sc->script_free_lo - 1, 0);
   1905 	siop_lun->reseloff = sc->script_free_lo - 2;
   1906 	lunsw->lunsw_size += 2;
   1907 	sc->script_free_lo += 2;
   1908 	if (sc->targets[target]->flags & TARF_TAG) {
   1909 		/* we need a tag switch */
   1910 		sc->script_free_hi -=
   1911 		    sizeof(tag_switch) / sizeof(tag_switch[0]);
   1912 		if (sc->features & SF_CHIP_RAM) {
   1913 			bus_space_write_region_4(sc->sc_ramt, sc->sc_ramh,
   1914 			    sc->script_free_hi * 4, tag_switch,
   1915 			    sizeof(tag_switch) / sizeof(tag_switch[0]));
   1916 		} else {
   1917 			for(i = 0;
   1918 			    i < sizeof(tag_switch) / sizeof(tag_switch[0]);
   1919 			    i++) {
   1920 				sc->sc_script[sc->script_free_hi + i] =
   1921 				    htole32(tag_switch[i]);
   1922 			}
   1923 		}
   1924 		siop_script_write(sc,
   1925 		    siop_lun->reseloff + 1,
   1926 		    sc->sc_scriptaddr + sc->script_free_hi * 4 +
   1927 		    Ent_tag_switch_entry);
   1928 
   1929 		for (i = 0; i < SIOP_NTAG; i++) {
   1930 			siop_lun->siop_tag[i].reseloff =
   1931 			    sc->script_free_hi + (Ent_resel_tag0 / 4) + i * 2;
   1932 		}
   1933 	} else {
   1934 		/* non-tag case; just work with the lun switch */
   1935 		siop_lun->siop_tag[0].reseloff =
   1936 		    sc->targets[target]->siop_lun[lun]->reseloff;
   1937 	}
   1938 	siop_script_sync(sc, BUS_DMASYNC_PREWRITE);
   1939 }
   1940 
   1941 void
   1942 siop_del_dev(sc, target, lun)
   1943 	struct siop_softc *sc;
   1944 	int target;
   1945 	int lun;
   1946 {
   1947 	int i;
   1948 #ifdef SIOP_DEBUG
   1949 		printf("%s:%d:%d: free lun sw entry\n",
   1950 		    sc->sc_dev.dv_xname, target, lun);
   1951 #endif
   1952 	if (sc->targets[target] == NULL)
   1953 		return;
   1954 	free(sc->targets[target]->siop_lun[lun], M_DEVBUF);
   1955 	sc->targets[target]->siop_lun[lun] = NULL;
   1956 	/* XXX compact sw entry too ? */
   1957 	/* check if we can free the whole target */
   1958 	for (i = 0; i < 8; i++) {
   1959 		if (sc->targets[target]->siop_lun[i] != NULL)
   1960 			return;
   1961 	}
   1962 #ifdef SIOP_DEBUG
   1963 	printf("%s: free siop_target for target %d lun %d lunsw offset %d\n",
   1964 	    sc->sc_dev.dv_xname, target, lun,
   1965 	    sc->targets[target]->lunsw->lunsw_off);
   1966 #endif
   1967 	/*
   1968 	 * nothing here, free the target struct and resel
   1969 	 * switch entry
   1970 	 */
   1971 	siop_script_write(sc, sc->targets[target]->reseloff, 0x800c00ff);
   1972 	siop_script_sync(sc, BUS_DMASYNC_PREWRITE);
   1973 	TAILQ_INSERT_TAIL(&sc->lunsw_list, sc->targets[target]->lunsw, next);
   1974 	free(sc->targets[target], M_DEVBUF);
   1975 	sc->targets[target] = NULL;
   1976 	sc->sc_ntargets--;
   1977 }
   1978 
   1979 #ifdef SIOP_STATS
   1980 void
   1981 siop_printstats()
   1982 {
   1983 	printf("siop_stat_intr %d\n", siop_stat_intr);
   1984 	printf("siop_stat_intr_shortxfer %d\n", siop_stat_intr_shortxfer);
   1985 	printf("siop_stat_intr_xferdisc %d\n", siop_stat_intr_xferdisc);
   1986 	printf("siop_stat_intr_sdp %d\n", siop_stat_intr_sdp);
   1987 	printf("siop_stat_intr_done %d\n", siop_stat_intr_done);
   1988 	printf("siop_stat_intr_lunresel %d\n", siop_stat_intr_lunresel);
   1989 	printf("siop_stat_intr_qfull %d\n", siop_stat_intr_qfull);
   1990 }
   1991 #endif
   1992