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