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