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