Home | History | Annotate | Line # | Download | only in ic
siop.c revision 1.37.2.6
      1 /*	$NetBSD: siop.c,v 1.37.2.6 2001/01/15 09:26:26 bouyer Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2000 Manuel Bouyer.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. All advertising materials mentioning features or use of this software
     15  *    must display the following acknowledgement:
     16  *	This product includes software developed by Manuel Bouyer
     17  * 4. The name of the author may not be used to endorse or promote products
     18  *    derived from this software without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  *
     31  */
     32 
     33 /* SYM53c7/8xx PCI-SCSI I/O Processors driver */
     34 
     35 #include <sys/param.h>
     36 #include <sys/systm.h>
     37 #include <sys/device.h>
     38 #include <sys/malloc.h>
     39 #include <sys/buf.h>
     40 #include <sys/kernel.h>
     41 
     42 #include <uvm/uvm_extern.h>
     43 
     44 #include <machine/endian.h>
     45 #include <machine/bus.h>
     46 
     47 #include <dev/microcode/siop/siop.out>
     48 
     49 #include <dev/scsipi/scsi_all.h>
     50 #include <dev/scsipi/scsi_message.h>
     51 #include <dev/scsipi/scsipi_all.h>
     52 
     53 #include <dev/scsipi/scsiconf.h>
     54 
     55 #include <dev/ic/siopreg.h>
     56 #include <dev/ic/siopvar.h>
     57 #include <dev/ic/siopvar_common.h>
     58 
     59 #ifndef DEBUG
     60 #undef DEBUG
     61 #endif
     62 #undef SIOP_DEBUG
     63 #undef SIOP_DEBUG_DR
     64 #undef SIOP_DEBUG_INTR
     65 #undef SIOP_DEBUG_SCHED
     66 #undef DUMP_SCRIPT
     67 
     68 #define SIOP_STATS
     69 
     70 #ifndef SIOP_DEFAULT_TARGET
     71 #define SIOP_DEFAULT_TARGET 7
     72 #endif
     73 
     74 /* number of cmd descriptors per block */
     75 #define SIOP_NCMDPB (PAGE_SIZE / sizeof(struct siop_xfer))
     76 
     77 /* Number of scheduler slot (needs to match script) */
     78 #define SIOP_NSLOTS 40
     79 
     80 void	siop_reset __P((struct siop_softc *));
     81 void	siop_handle_reset __P((struct siop_softc *));
     82 int	siop_handle_qtag_reject __P((struct siop_cmd *));
     83 void	siop_scsicmd_end __P((struct siop_cmd *));
     84 void	siop_start __P((struct siop_softc *));
     85 void 	siop_timeout __P((void *));
     86 int	siop_scsicmd __P((struct scsipi_xfer *));
     87 void	siop_scsipi_request __P(( struct scsipi_channel *,
     88 			scsipi_adapter_req_t, void *));
     89 void	siop_dump_script __P((struct siop_softc *));
     90 int	siop_morecbd __P((struct siop_softc *));
     91 struct siop_lunsw *siop_get_lunsw __P((struct siop_softc *));
     92 void	siop_add_reselsw __P((struct siop_softc *, int));
     93 void	siop_update_scntl3 __P((struct siop_softc *, struct siop_target *));
     94 
     95 #ifdef SIOP_STATS
     96 static int siop_stat_intr = 0;
     97 static int siop_stat_intr_shortxfer = 0;
     98 static int siop_stat_intr_sdp = 0;
     99 static int siop_stat_intr_done = 0;
    100 static int siop_stat_intr_xferdisc = 0;
    101 static int siop_stat_intr_lunresel = 0;
    102 static int siop_stat_intr_qfull = 0;
    103 void siop_printstats __P((void));
    104 #define INCSTAT(x) x++
    105 #else
    106 #define INCSTAT(x)
    107 #endif
    108 
    109 static __inline__ void siop_script_sync __P((struct siop_softc *, int));
    110 static __inline__ void
    111 siop_script_sync(sc, ops)
    112 	struct siop_softc *sc;
    113 	int ops;
    114 {
    115 	if ((sc->features & SF_CHIP_RAM) == 0)
    116 		bus_dmamap_sync(sc->sc_dmat, sc->sc_scriptdma, 0,
    117 		    PAGE_SIZE, ops);
    118 }
    119 
    120 static __inline__ u_int32_t siop_script_read __P((struct siop_softc *, u_int));
    121 static __inline__ u_int32_t
    122 siop_script_read(sc, offset)
    123 	struct siop_softc *sc;
    124 	u_int offset;
    125 {
    126 	if (sc->features & SF_CHIP_RAM) {
    127 		return bus_space_read_4(sc->sc_ramt, sc->sc_ramh, offset * 4);
    128 	} else {
    129 		return le32toh(sc->sc_script[offset]);
    130 	}
    131 }
    132 
    133 static __inline__ void siop_script_write __P((struct siop_softc *, u_int,
    134 	u_int32_t));
    135 static __inline__ void
    136 siop_script_write(sc, offset, val)
    137 	struct siop_softc *sc;
    138 	u_int offset;
    139 	u_int32_t val;
    140 {
    141 	if (sc->features & SF_CHIP_RAM) {
    142 		bus_space_write_4(sc->sc_ramt, sc->sc_ramh, offset * 4, val);
    143 	} else {
    144 		sc->sc_script[offset] = htole32(val);
    145 	}
    146 }
    147 
    148 void
    149 siop_attach(sc)
    150 	struct siop_softc *sc;
    151 {
    152 	int error, i;
    153 	bus_dma_segment_t seg;
    154 	int rseg;
    155 
    156 	/*
    157 	 * Allocate DMA-safe memory for the script and map it.
    158 	 */
    159 	if ((sc->features & SF_CHIP_RAM) == 0) {
    160 		error = bus_dmamem_alloc(sc->sc_dmat, PAGE_SIZE,
    161 		    PAGE_SIZE, 0, &seg, 1, &rseg, BUS_DMA_NOWAIT);
    162 		if (error) {
    163 			printf("%s: unable to allocate script DMA memory, "
    164 			    "error = %d\n", sc->sc_dev.dv_xname, error);
    165 			return;
    166 		}
    167 		error = bus_dmamem_map(sc->sc_dmat, &seg, rseg, PAGE_SIZE,
    168 		    (caddr_t *)&sc->sc_script, BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
    169 		if (error) {
    170 			printf("%s: unable to map script DMA memory, "
    171 			    "error = %d\n", sc->sc_dev.dv_xname, error);
    172 			return;
    173 		}
    174 		error = bus_dmamap_create(sc->sc_dmat, PAGE_SIZE, 1,
    175 		    PAGE_SIZE, 0, BUS_DMA_NOWAIT, &sc->sc_scriptdma);
    176 		if (error) {
    177 			printf("%s: unable to create script DMA map, "
    178 			    "error = %d\n", sc->sc_dev.dv_xname, error);
    179 			return;
    180 		}
    181 		error = bus_dmamap_load(sc->sc_dmat, sc->sc_scriptdma,
    182 		    sc->sc_script, PAGE_SIZE, NULL, BUS_DMA_NOWAIT);
    183 		if (error) {
    184 			printf("%s: unable to load script DMA map, "
    185 			    "error = %d\n", sc->sc_dev.dv_xname, error);
    186 			return;
    187 		}
    188 		sc->sc_scriptaddr = sc->sc_scriptdma->dm_segs[0].ds_addr;
    189 		sc->ram_size = PAGE_SIZE;
    190 	}
    191 	TAILQ_INIT(&sc->free_list);
    192 	TAILQ_INIT(&sc->ready_list);
    193 	TAILQ_INIT(&sc->urgent_list);
    194 	TAILQ_INIT(&sc->cmds);
    195 	TAILQ_INIT(&sc->lunsw_list);
    196 	sc->sc_currschedslot = 0;
    197 #ifdef SIOP_DEBUG
    198 	printf("%s: script size = %d, PHY addr=0x%x, VIRT=%p\n",
    199 	    sc->sc_dev.dv_xname, (int)sizeof(siop_script),
    200 	    (u_int32_t)sc->sc_scriptaddr, sc->sc_script);
    201 #endif
    202 
    203 	sc->sc_adapt.adapt_dev = &sc->sc_dev;
    204 	sc->sc_adapt.adapt_nchannels = 1;
    205 	sc->sc_adapt.adapt_openings = 225;
    206 	sc->sc_adapt.adapt_max_periph = SIOP_NTAG - 1;
    207 	sc->sc_adapt.adapt_ioctl = siop_ioctl;
    208 	sc->sc_adapt.adapt_minphys = minphys;
    209 	sc->sc_adapt.adapt_request = siop_scsipi_request;
    210 
    211 	memset(&sc->sc_chan, 0, sizeof(sc->sc_chan));
    212 	sc->sc_chan.chan_adapter = &sc->sc_adapt;
    213 	sc->sc_chan.chan_bustype = &scsi_bustype;
    214 	sc->sc_chan.chan_channel = 0;
    215 	sc->sc_chan.chan_ntargets = (sc->features & SF_BUS_WIDE) ? 16 : 8;
    216 	sc->sc_chan.chan_nluns = 8;
    217 	sc->sc_chan.chan_id = bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_SCID);
    218 	if (sc->sc_chan.chan_id == 0 ||
    219 	    sc->sc_chan.chan_id >= sc->sc_chan.chan_ntargets)
    220 		sc->sc_chan.chan_id = SIOP_DEFAULT_TARGET;
    221 
    222 	for (i = 0; i < 16; i++)
    223 		sc->targets[i] = NULL;
    224 
    225 	/* find min/max sync period for this chip */
    226 	sc->maxsync = 0;
    227 	sc->minsync = 255;
    228 	for (i = 0; i < sizeof(scf_period) / sizeof(scf_period[0]); i++) {
    229 		if (sc->clock_period != scf_period[i].clock)
    230 			continue;
    231 		if (sc->maxsync < scf_period[i].period)
    232 			sc->maxsync = scf_period[i].period;
    233 		if (sc->minsync > scf_period[i].period)
    234 			sc->minsync = scf_period[i].period;
    235 	}
    236 	if (sc->maxsync == 255 || sc->minsync == 0)
    237 		panic("siop: can't find my sync parameters\n");
    238 	/* Do a bus reset, so that devices fall back to narrow/async */
    239 	siop_resetbus(sc);
    240 	/*
    241 	 * siop_reset() will reset the chip, thus clearing pending interrupts
    242 	 */
    243 	siop_reset(sc);
    244 #ifdef DUMP_SCRIPT
    245 	siop_dump_script(sc);
    246 #endif
    247 
    248 	config_found((struct device*)sc, &sc->sc_chan, scsiprint);
    249 }
    250 
    251 void
    252 siop_reset(sc)
    253 	struct siop_softc *sc;
    254 {
    255 	int i, j;
    256 	struct siop_lunsw *lunsw;
    257 
    258 	siop_common_reset(sc);
    259 
    260 	/* copy and patch the script */
    261 	if (sc->features & SF_CHIP_RAM) {
    262 		bus_space_write_region_4(sc->sc_ramt, sc->sc_ramh, 0,
    263 		    siop_script, sizeof(siop_script) / sizeof(siop_script[0]));
    264 		for (j = 0; j <
    265 		    (sizeof(E_abs_msgin_Used) / sizeof(E_abs_msgin_Used[0]));
    266 		    j++) {
    267 			bus_space_write_4(sc->sc_ramt, sc->sc_ramh,
    268 			    E_abs_msgin_Used[j] * 4,
    269 			    sc->sc_scriptaddr + Ent_msgin_space);
    270 		}
    271 	} else {
    272 		for (j = 0;
    273 		    j < (sizeof(siop_script) / sizeof(siop_script[0])); j++) {
    274 			sc->sc_script[j] = htole32(siop_script[j]);
    275 		}
    276 		for (j = 0; j <
    277 		    (sizeof(E_abs_msgin_Used) / sizeof(E_abs_msgin_Used[0]));
    278 		    j++) {
    279 			sc->sc_script[E_abs_msgin_Used[j]] =
    280 			    htole32(sc->sc_scriptaddr + Ent_msgin_space);
    281 		}
    282 	}
    283 	sc->script_free_lo = sizeof(siop_script) / sizeof(siop_script[0]);
    284 	sc->script_free_hi = sc->ram_size / 4;
    285 
    286 	/* free used and unused lun switches */
    287 	while((lunsw = TAILQ_FIRST(&sc->lunsw_list)) != NULL) {
    288 #ifdef SIOP_DEBUG
    289 		printf("%s: free lunsw at offset %d\n",
    290 				sc->sc_dev.dv_xname, lunsw->lunsw_off);
    291 #endif
    292 		TAILQ_REMOVE(&sc->lunsw_list, lunsw, next);
    293 		free(lunsw, M_DEVBUF);
    294 	}
    295 	TAILQ_INIT(&sc->lunsw_list);
    296 	/* restore reselect switch */
    297 	for (i = 0; i < sc->sc_chan.chan_ntargets; i++) {
    298 		if (sc->targets[i] == NULL)
    299 			continue;
    300 #ifdef SIOP_DEBUG
    301 		printf("%s: restore sw for target %d\n",
    302 				sc->sc_dev.dv_xname, i);
    303 #endif
    304 		free(sc->targets[i]->lunsw, M_DEVBUF);
    305 		sc->targets[i]->lunsw = siop_get_lunsw(sc);
    306 		if (sc->targets[i]->lunsw == NULL) {
    307 			printf("%s: can't alloc lunsw for target %d\n",
    308 			    sc->sc_dev.dv_xname, i);
    309 			break;
    310 		}
    311 		siop_add_reselsw(sc, i);
    312 	}
    313 
    314 	/* start script */
    315 	if ((sc->features & SF_CHIP_RAM) == 0) {
    316 		bus_dmamap_sync(sc->sc_dmat, sc->sc_scriptdma, 0, PAGE_SIZE,
    317 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
    318 	}
    319 	bus_space_write_4(sc->sc_rt, sc->sc_rh, SIOP_DSP,
    320 	    sc->sc_scriptaddr + Ent_reselect);
    321 }
    322 
    323 #if 0
    324 #define CALL_SCRIPT(ent) do {\
    325 	printf ("start script DSA 0x%lx DSP 0x%lx\n", \
    326 	    siop_cmd->dsa, \
    327 	    sc->sc_scriptaddr + ent); \
    328 bus_space_write_4(sc->sc_rt, sc->sc_rh, SIOP_DSP, sc->sc_scriptaddr + ent); \
    329 } while (0)
    330 #else
    331 #define CALL_SCRIPT(ent) do {\
    332 bus_space_write_4(sc->sc_rt, sc->sc_rh, SIOP_DSP, sc->sc_scriptaddr + ent); \
    333 } while (0)
    334 #endif
    335 
    336 int
    337 siop_intr(v)
    338 	void *v;
    339 {
    340 	struct siop_softc *sc = v;
    341 	struct siop_target *siop_target;
    342 	struct siop_cmd *siop_cmd;
    343 	struct siop_lun *siop_lun;
    344 	struct scsipi_xfer *xs;
    345 	int istat, sist, sstat1, dstat;
    346 	u_int32_t irqcode;
    347 	int need_reset = 0;
    348 	int offset, target, lun, tag;
    349 	bus_addr_t dsa;
    350 	struct siop_cbd *cbdp;
    351 	int freetarget = 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) not active (%d)\n",
    387 				lun, siop_cmd->status);
    388 			xs = NULL;
    389 			siop_target = NULL;
    390 			target = -1;
    391 			lun = -1;
    392 			tag = -1;
    393 			siop_lun = NULL;
    394 			siop_cmd = NULL;
    395 		} else if (siop_lun->siop_tag[tag].active != siop_cmd) {
    396 			printf("siop_cmd (lun %d tag %d) not in siop_lun "
    397 			    "active (%p != %p)\n", lun, tag, siop_cmd,
    398 			    siop_lun->siop_tag[tag].active);
    399 		}
    400 #endif
    401 	} else {
    402 		xs = NULL;
    403 		siop_target = NULL;
    404 		target = -1;
    405 		lun = -1;
    406 		tag = -1;
    407 		siop_lun = NULL;
    408 	}
    409 	if (istat & ISTAT_DIP) {
    410 		dstat = bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_DSTAT);
    411 		if (dstat & DSTAT_SSI) {
    412 			printf("single step dsp 0x%08x dsa 0x08%x\n",
    413 			    (int)(bus_space_read_4(sc->sc_rt, sc->sc_rh, SIOP_DSP) -
    414 			    sc->sc_scriptaddr),
    415 			    bus_space_read_4(sc->sc_rt, sc->sc_rh, SIOP_DSA));
    416 			if ((dstat & ~(DSTAT_DFE | DSTAT_SSI)) == 0 &&
    417 			    (istat & ISTAT_SIP) == 0) {
    418 				bus_space_write_1(sc->sc_rt, sc->sc_rh,
    419 				    SIOP_DCNTL, bus_space_read_1(sc->sc_rt,
    420 				    sc->sc_rh, SIOP_DCNTL) | DCNTL_STD);
    421 			}
    422 			return 1;
    423 		}
    424 		if (dstat & ~(DSTAT_SIR | DSTAT_DFE | DSTAT_SSI)) {
    425 		printf("DMA IRQ:");
    426 		if (dstat & DSTAT_IID)
    427 			printf(" Illegal instruction");
    428 		if (dstat & DSTAT_ABRT)
    429 			printf(" abort");
    430 		if (dstat & DSTAT_BF)
    431 			printf(" bus fault");
    432 		if (dstat & DSTAT_MDPE)
    433 			printf(" parity");
    434 		if (dstat & DSTAT_DFE)
    435 			printf(" dma fifo empty");
    436 		printf(", DSP=0x%x DSA=0x%x: ",
    437 		    (int)(bus_space_read_4(sc->sc_rt, sc->sc_rh, SIOP_DSP) -
    438 		    sc->sc_scriptaddr),
    439 		    bus_space_read_4(sc->sc_rt, sc->sc_rh, SIOP_DSA));
    440 		if (siop_cmd)
    441 			printf("last msg_in=0x%x status=0x%x\n",
    442 			    siop_cmd->siop_tables.msg_in[0],
    443 			    le32toh(siop_cmd->siop_tables.status));
    444 		else
    445 			printf("%s: current DSA invalid\n",
    446 			    sc->sc_dev.dv_xname);
    447 		need_reset = 1;
    448 		}
    449 	}
    450 	if (istat & ISTAT_SIP) {
    451 		if (istat & ISTAT_DIP)
    452 			delay(10);
    453 		/*
    454 		 * Can't read sist0 & sist1 independantly, or we have to
    455 		 * insert delay
    456 		 */
    457 		sist = bus_space_read_2(sc->sc_rt, sc->sc_rh, SIOP_SIST0);
    458 		sstat1 = bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_SSTAT1);
    459 #ifdef SIOP_DEBUG_INTR
    460 		printf("scsi interrupt, sist=0x%x sstat1=0x%x "
    461 		    "DSA=0x%x DSP=0x%lx\n", sist,
    462 		    bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_SSTAT1),
    463 		    bus_space_read_4(sc->sc_rt, sc->sc_rh, SIOP_DSA),
    464 		    (u_long)(bus_space_read_4(sc->sc_rt, sc->sc_rh, SIOP_DSP) -
    465 		    sc->sc_scriptaddr));
    466 #endif
    467 		if (sist & SIST0_RST) {
    468 			siop_handle_reset(sc);
    469 			siop_start(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("%s: script interrupt (0x%x) with
    628 				    invalid DSA !!!\n", sc->sc_dev.dv_xname,
    629 				    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 			return 1;
    687 		case A_int_reseltag:
    688 			printf("%s: reselect with invalid tag\n",
    689 				    sc->sc_dev.dv_xname);
    690 			goto reset;
    691 		case A_int_msgin:
    692 		{
    693 			int msgin = bus_space_read_1(sc->sc_rt, sc->sc_rh,
    694 			    SIOP_SFBR);
    695 			if (msgin == MSG_MESSAGE_REJECT) {
    696 				int msg, extmsg;
    697 				if (siop_cmd->siop_tables.msg_out[0] & 0x80) {
    698 					/*
    699 					 * message was part of a identify +
    700 					 * something else. Identify shoudl't
    701 					 * have been rejected.
    702 					 */
    703 					msg = siop_cmd->siop_tables.msg_out[1];
    704 					extmsg =
    705 					    siop_cmd->siop_tables.msg_out[3];
    706 				} else {
    707 					msg = siop_cmd->siop_tables.msg_out[0];
    708 					extmsg =
    709 					    siop_cmd->siop_tables.msg_out[2];
    710 				}
    711 				if (msg == MSG_MESSAGE_REJECT) {
    712 					/* MSG_REJECT  for a MSG_REJECT  !*/
    713 					if (xs)
    714 						scsipi_printaddr(xs->xs_periph);
    715 					else
    716 						printf("%s: ",
    717 						   sc->sc_dev.dv_xname);
    718 					printf("our reject message was "
    719 					    "rejected\n");
    720 					goto reset;
    721 				}
    722 				if (msg == MSG_EXTENDED &&
    723 				    extmsg == MSG_EXT_WDTR) {
    724 					/* WDTR rejected, initiate sync */
    725 					if ((siop_target->flags & TARF_SYNC)
    726 					    == 0) {
    727 						siop_target->status = TARST_OK;
    728 						siop_update_xfer_mode(sc,
    729 						    target);
    730 						/* no table to flush here */
    731 						CALL_SCRIPT(Ent_msgin_ack);
    732 						return 1;
    733 					}
    734 					siop_target->status = TARST_SYNC_NEG;
    735 					siop_sdtr_msg(siop_cmd, 0,
    736 					    sc->minsync, sc->maxoff);
    737 					siop_table_sync(siop_cmd,
    738 					    BUS_DMASYNC_PREREAD |
    739 					    BUS_DMASYNC_PREWRITE);
    740 					CALL_SCRIPT(Ent_send_msgout);
    741 					return 1;
    742 				} else if (msg == MSG_EXTENDED &&
    743 				    extmsg == MSG_EXT_SDTR) {
    744 					/* sync rejected */
    745 					siop_target->offset = 0;
    746 					siop_target->period = 0;
    747 					siop_target->status = TARST_OK;
    748 					siop_update_xfer_mode(sc, target);
    749 					/* no table to flush here */
    750 					CALL_SCRIPT(Ent_msgin_ack);
    751 					return 1;
    752 				} else if (msg == MSG_SIMPLE_Q_TAG ||
    753 				    msg == MSG_HEAD_OF_Q_TAG ||
    754 				    msg == MSG_ORDERED_Q_TAG) {
    755 					if (siop_handle_qtag_reject(
    756 					    siop_cmd) == -1)
    757 						goto reset;
    758 					CALL_SCRIPT(Ent_msgin_ack);
    759 					return 1;
    760 				}
    761 				if (xs)
    762 					scsipi_printaddr(xs->xs_periph);
    763 				else
    764 					printf("%s: ", sc->sc_dev.dv_xname);
    765 				if (msg == MSG_EXTENDED) {
    766 					printf("scsi message reject, extended "
    767 					    "message sent was 0x%x\n", extmsg);
    768 				} else {
    769 					printf("scsi message reject, message "
    770 					    "sent was 0x%x\n", msg);
    771 				}
    772 				/* no table to flush here */
    773 				CALL_SCRIPT(Ent_msgin_ack);
    774 				return 1;
    775 			}
    776 			if (xs)
    777 				scsipi_printaddr(xs->xs_periph);
    778 			else
    779 				printf("%s: ", sc->sc_dev.dv_xname);
    780 			printf("unhandled message 0x%x\n",
    781 			    siop_cmd->siop_tables.msg_in[0]);
    782 			siop_cmd->siop_tables.msg_out[0] = MSG_MESSAGE_REJECT;
    783 			siop_cmd->siop_tables.t_msgout.count= htole32(1);
    784 			siop_table_sync(siop_cmd,
    785 			    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
    786 			CALL_SCRIPT(Ent_send_msgout);
    787 			return 1;
    788 		}
    789 		case A_int_extmsgin:
    790 #ifdef SIOP_DEBUG_INTR
    791 			printf("extended message: msg 0x%x len %d\n",
    792 			    siop_cmd->siop_tables.msg_in[2],
    793 			    siop_cmd->siop_tables.msg_in[1]);
    794 #endif
    795 			if (siop_cmd->siop_tables.msg_in[1] > 6)
    796 				printf("%s: extended message too big (%d)\n",
    797 				    sc->sc_dev.dv_xname,
    798 				    siop_cmd->siop_tables.msg_in[1]);
    799 			siop_cmd->siop_tables.t_extmsgdata.count =
    800 			    htole32(siop_cmd->siop_tables.msg_in[1] - 1);
    801 			siop_table_sync(siop_cmd,
    802 			    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
    803 			CALL_SCRIPT(Ent_get_extmsgdata);
    804 			return 1;
    805 		case A_int_extmsgdata:
    806 #ifdef SIOP_DEBUG_INTR
    807 			{
    808 			int i;
    809 			printf("extended message: 0x%x, data:",
    810 			    siop_cmd->siop_tables.msg_in[2]);
    811 			for (i = 3; i < 2 + siop_cmd->siop_tables.msg_in[1];
    812 			    i++)
    813 				printf(" 0x%x",
    814 				    siop_cmd->siop_tables.msg_in[i]);
    815 			printf("\n");
    816 			}
    817 #endif
    818 			if (siop_cmd->siop_tables.msg_in[2] == MSG_EXT_WDTR) {
    819 				switch (siop_wdtr_neg(siop_cmd)) {
    820 				case SIOP_NEG_MSGOUT:
    821 					siop_update_scntl3(sc,
    822 					    siop_cmd->siop_target);
    823 					siop_table_sync(siop_cmd,
    824 					    BUS_DMASYNC_PREREAD |
    825 					    BUS_DMASYNC_PREWRITE);
    826 					CALL_SCRIPT(Ent_send_msgout);
    827 					return(1);
    828 				case SIOP_NEG_ACK:
    829 					siop_update_scntl3(sc,
    830 					    siop_cmd->siop_target);
    831 					CALL_SCRIPT(Ent_msgin_ack);
    832 					return(1);
    833 				default:
    834 					panic("invalid retval from "
    835 					    "siop_wdtr_neg()");
    836 				}
    837 				return(1);
    838 			}
    839 			if (siop_cmd->siop_tables.msg_in[2] == MSG_EXT_SDTR) {
    840 				switch (siop_sdtr_neg(siop_cmd)) {
    841 				case SIOP_NEG_MSGOUT:
    842 					siop_update_scntl3(sc,
    843 					    siop_cmd->siop_target);
    844 					siop_table_sync(siop_cmd,
    845 					    BUS_DMASYNC_PREREAD |
    846 					    BUS_DMASYNC_PREWRITE);
    847 					CALL_SCRIPT(Ent_send_msgout);
    848 					return(1);
    849 				case SIOP_NEG_ACK:
    850 					siop_update_scntl3(sc,
    851 					    siop_cmd->siop_target);
    852 					CALL_SCRIPT(Ent_msgin_ack);
    853 					return(1);
    854 				default:
    855 					panic("invalid retval from "
    856 					    "siop_wdtr_neg()");
    857 				}
    858 				return(1);
    859 			}
    860 			/* send a message reject */
    861 			siop_cmd->siop_tables.msg_out[0] = MSG_MESSAGE_REJECT;
    862 			siop_cmd->siop_tables.t_msgout.count = htole32(1);
    863 			siop_table_sync(siop_cmd,
    864 			    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
    865 			CALL_SCRIPT(Ent_send_msgout);
    866 			return 1;
    867 		case A_int_disc:
    868 			INCSTAT(siop_stat_intr_sdp);
    869 			offset = bus_space_read_1(sc->sc_rt, sc->sc_rh,
    870 			    SIOP_SCRATCHA + 1);
    871 #ifdef SIOP_DEBUG_DR
    872 			printf("disconnect offset %d\n", offset);
    873 #endif
    874 			if (offset > SIOP_NSG) {
    875 				printf("%s: bad offset for disconnect (%d)\n",
    876 				    sc->sc_dev.dv_xname, offset);
    877 				goto reset;
    878 			}
    879 			/*
    880 			 * offset == SIOP_NSG may be a valid condition if
    881 			 * we get a sdp when the xfer is done.
    882 			 * Don't call memmove in this case.
    883 			 */
    884 			if (offset < SIOP_NSG) {
    885 				memmove(&siop_cmd->siop_tables.data[0],
    886 				    &siop_cmd->siop_tables.data[offset],
    887 				    (SIOP_NSG - offset) * sizeof(scr_table_t));
    888 				siop_table_sync(siop_cmd,
    889 				    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
    890 			}
    891 			CALL_SCRIPT(Ent_script_sched);
    892 			/* check if we can put some command in scheduler */
    893 			siop_start(sc);
    894 			return 1;
    895 		case A_int_resfail:
    896 			printf("reselect failed\n");
    897 			CALL_SCRIPT(Ent_script_sched);
    898 			return  1;
    899 		case A_int_done:
    900 			if (xs == NULL) {
    901 				printf("%s: done without command, DSA=0x%lx\n",
    902 				    sc->sc_dev.dv_xname, (u_long)siop_cmd->dsa);
    903 				siop_cmd->status = CMDST_FREE;
    904 				siop_start(sc);
    905 				CALL_SCRIPT(Ent_script_sched);
    906 				return 1;
    907 			}
    908 #ifdef SIOP_DEBUG_INTR
    909 			printf("done, DSA=0x%lx target id 0x%x last msg "
    910 			    "in=0x%x status=0x%x\n", (u_long)siop_cmd->dsa,
    911 			    le32toh(siop_cmd->siop_tables.id),
    912 			    siop_cmd->siop_tables.msg_in[0],
    913 			    le32toh(siop_cmd->siop_tables.status));
    914 #endif
    915 			INCSTAT(siop_stat_intr_done);
    916 			siop_cmd->status = CMDST_DONE;
    917 			goto end;
    918 		default:
    919 			printf("unknown irqcode %x\n", irqcode);
    920 			if (xs) {
    921 				xs->error = XS_SELTIMEOUT;
    922 				goto end;
    923 			}
    924 			goto reset;
    925 		}
    926 		return 1;
    927 	}
    928 	/* We just should't get there */
    929 	panic("siop_intr: I shouldn't be there !");
    930 	return 1;
    931 end:
    932 	CALL_SCRIPT(Ent_script_sched);
    933 	siop_lun->siop_tag[tag].active = NULL;
    934 	siop_scsicmd_end(siop_cmd);
    935 	if (siop_cmd->status == CMDST_FREE) {
    936 		TAILQ_INSERT_TAIL(&sc->free_list, siop_cmd, next);
    937 		if (freetarget && siop_target->status == TARST_PROBING)
    938 			siop_del_dev(sc, target, lun);
    939 	}
    940 	siop_start(sc);
    941 	return 1;
    942 }
    943 
    944 void
    945 siop_scsicmd_end(siop_cmd)
    946 	struct siop_cmd *siop_cmd;
    947 {
    948 	struct scsipi_xfer *xs = siop_cmd->xs;
    949 	struct siop_softc *sc = siop_cmd->siop_sc;
    950 
    951 	xs->status = le32toh(siop_cmd->siop_tables.status);
    952 	switch(xs->status) {
    953 	case SCSI_OK:
    954 		xs->error = XS_NOERROR;
    955 		break;
    956 	case SCSI_BUSY:
    957 		xs->error = XS_BUSY;
    958 		break;
    959 	case SCSI_CHECK:
    960 		xs->error = XS_BUSY;
    961 		break;
    962 	case SCSI_QUEUE_FULL:
    963 		INCSTAT(siop_stat_intr_qfull);
    964 #ifdef SIOP_DEBUG
    965 		printf("%s:%d:%d: queue full (tag %d)\n", sc->sc_dev.dv_xname,
    966 		    xs->xs_periph->periph_target,
    967 		    xs->xs_periph->periph_lun, siop_cmd->tag);
    968 #endif
    969 		xs->error = XS_BUSY;
    970 		break;
    971 	case SCSI_SIOP_NOCHECK:
    972 		/*
    973 		 * don't check status, xs->error is already valid
    974 		 */
    975 		break;
    976 	case SCSI_SIOP_NOSTATUS:
    977 		/*
    978 		 * the status byte was not updated, cmd was
    979 		 * aborted
    980 		 */
    981 		xs->error = XS_SELTIMEOUT;
    982 		break;
    983 	default:
    984 		xs->error = XS_DRIVER_STUFFUP;
    985 	}
    986 	if (xs->xs_control & (XS_CTL_DATA_IN | XS_CTL_DATA_OUT)) {
    987 		bus_dmamap_sync(sc->sc_dmat, siop_cmd->dmamap_data, 0,
    988 		    siop_cmd->dmamap_data->dm_mapsize,
    989 		    (xs->xs_control & XS_CTL_DATA_IN) ?
    990 		    BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
    991 		bus_dmamap_unload(sc->sc_dmat, siop_cmd->dmamap_data);
    992 	}
    993 	bus_dmamap_unload(sc->sc_dmat, siop_cmd->dmamap_cmd);
    994 	callout_stop(&siop_cmd->xs->xs_callout);
    995 	siop_cmd->status = CMDST_FREE;
    996 	xs->resid = 0;
    997 	scsipi_done (xs);
    998 }
    999 
   1000 /*
   1001  * handle a rejected queue tag message: the command will run untagged,
   1002  * has to adjust the reselect script.
   1003  */
   1004 int
   1005 siop_handle_qtag_reject(siop_cmd)
   1006 	struct siop_cmd *siop_cmd;
   1007 {
   1008 	struct siop_softc *sc = siop_cmd->siop_sc;
   1009 	int target = siop_cmd->xs->xs_periph->periph_target;
   1010 	int lun = siop_cmd->xs->xs_periph->periph_lun;
   1011 	int tag = siop_cmd->siop_tables.msg_out[2];
   1012 	struct siop_lun *siop_lun = sc->targets[target]->siop_lun[lun];
   1013 
   1014 #ifdef SIOP_DEBUG
   1015 	printf("%s:%d:%d: tag message %d (%d) rejected (status %d)\n",
   1016 	    sc->sc_dev.dv_xname, target, lun, tag, siop_cmd->tag,
   1017 	    siop_cmd->status);
   1018 #endif
   1019 
   1020 	if (siop_lun->siop_tag[0].active != NULL) {
   1021 		printf("%s: untagged command already running for target %d "
   1022 		    "lun %d (status %d)\n", sc->sc_dev.dv_xname, target, lun,
   1023 		    siop_lun->siop_tag[0].active->status);
   1024 		return -1;
   1025 	}
   1026 	/* clear tag slot */
   1027 	siop_lun->siop_tag[tag].active = NULL;
   1028 	/* add command to non-tagged slot */
   1029 	siop_lun->siop_tag[0].active = siop_cmd;
   1030 	siop_cmd->tag = 0;
   1031 	/* adjust reselect script if there is one */
   1032 	if (siop_lun->siop_tag[0].reseloff > 0) {
   1033 		siop_script_write(sc,
   1034 		    siop_lun->siop_tag[0].reseloff + 1,
   1035 		    siop_cmd->dsa + sizeof(struct siop_xfer_common) +
   1036 		    Ent_ldsa_reload_dsa);
   1037 	}
   1038 	return 0;
   1039 }
   1040 
   1041 /*
   1042  * handle a bus reset: reset chip, unqueue all active commands, free all
   1043  * target struct and report loosage to upper layer.
   1044  * As the upper layer may requeue immediatly we have to first store
   1045  * all active commands in a temporary queue.
   1046  */
   1047 void
   1048 siop_handle_reset(sc)
   1049 	struct siop_softc *sc;
   1050 {
   1051 	struct cmd_list reset_list;
   1052 	struct siop_cmd *siop_cmd, *next_siop_cmd;
   1053 	struct siop_lun *siop_lun;
   1054 	int target, lun, tag;
   1055 	/*
   1056 	 * scsi bus reset. reset the chip and restart
   1057 	 * the queue. Need to clean up all active commands
   1058 	 */
   1059 	printf("%s: scsi bus reset\n", sc->sc_dev.dv_xname);
   1060 	/* stop, reset and restart the chip */
   1061 	siop_reset(sc);
   1062 	TAILQ_INIT(&reset_list);
   1063 	/*
   1064 	 * Process all commands: first commmands being executed
   1065 	 */
   1066 	for (target = 0; target < sc->sc_chan.chan_ntargets;
   1067 	    target++) {
   1068 		if (sc->targets[target] == NULL)
   1069 			continue;
   1070 		for (lun = 0; lun < 8; lun++) {
   1071 			siop_lun = sc->targets[target]->siop_lun[lun];
   1072 			if (siop_lun == NULL)
   1073 				continue;
   1074 			for (tag = 0; tag <
   1075 			    ((sc->targets[target]->flags & TARF_TAG) ?
   1076 			    SIOP_NTAG : 1);
   1077 			    tag++) {
   1078 				siop_cmd = siop_lun->siop_tag[tag].active;
   1079 				if (siop_cmd == NULL)
   1080 					continue;
   1081 				printf("cmd %p (target %d:%d) in reset list\n",
   1082 				    siop_cmd, target, lun);
   1083 				TAILQ_INSERT_TAIL(&reset_list, siop_cmd, next);
   1084 				siop_lun->siop_tag[tag].active = NULL;
   1085 			}
   1086 		}
   1087 		sc->targets[target]->status = TARST_ASYNC;
   1088 		sc->targets[target]->flags &= ~TARF_ISWIDE;
   1089 		sc->targets[target]->period = sc->targets[target]->offset = 0;
   1090 		siop_update_xfer_mode(sc, target);
   1091 	}
   1092 	/* Next commands from the urgent list */
   1093 	for (siop_cmd = TAILQ_FIRST(&sc->urgent_list); siop_cmd != NULL;
   1094 	    siop_cmd = next_siop_cmd) {
   1095 		next_siop_cmd = TAILQ_NEXT(siop_cmd, next);
   1096 		siop_cmd->flags &= ~CMDFL_TAG;
   1097 		printf("cmd %p (target %d:%d) in reset list (wait)\n",
   1098 		    siop_cmd, siop_cmd->xs->xs_periph->periph_target,
   1099 		    siop_cmd->xs->xs_periph->periph_lun);
   1100 		TAILQ_REMOVE(&sc->urgent_list, siop_cmd, next);
   1101 		TAILQ_INSERT_TAIL(&reset_list, siop_cmd, next);
   1102 	}
   1103 	/* Then command waiting in the input list */
   1104 	for (siop_cmd = TAILQ_FIRST(&sc->ready_list); siop_cmd != NULL;
   1105 	    siop_cmd = next_siop_cmd) {
   1106 		next_siop_cmd = TAILQ_NEXT(siop_cmd, next);
   1107 		siop_cmd->flags &= ~CMDFL_TAG;
   1108 		printf("cmd %p (target %d:%d) in reset list (wait)\n",
   1109 		    siop_cmd, siop_cmd->xs->xs_periph->periph_target,
   1110 		    siop_cmd->xs->xs_periph->periph_lun);
   1111 		TAILQ_REMOVE(&sc->ready_list, siop_cmd, next);
   1112 		TAILQ_INSERT_TAIL(&reset_list, siop_cmd, next);
   1113 	}
   1114 
   1115 	for (siop_cmd = TAILQ_FIRST(&reset_list); siop_cmd != NULL;
   1116 	    siop_cmd = next_siop_cmd) {
   1117 		next_siop_cmd = TAILQ_NEXT(siop_cmd, next);
   1118 		siop_cmd->xs->error = (siop_cmd->flags & CMDFL_TIMEOUT) ?
   1119 		    XS_TIMEOUT : XS_RESET;
   1120 		siop_cmd->siop_tables.status = htole32(SCSI_SIOP_NOCHECK);
   1121 		printf("cmd %p (status %d) about to be processed\n", siop_cmd,
   1122 		    siop_cmd->status);
   1123 		siop_cmd->status = CMDST_DONE;
   1124 		TAILQ_REMOVE(&reset_list, siop_cmd, next);
   1125 		siop_scsicmd_end(siop_cmd);
   1126 		TAILQ_INSERT_TAIL(&sc->free_list, siop_cmd, next);
   1127 	}
   1128 	scsipi_async_event(&sc->sc_chan, ASYNC_EVENT_RESET, NULL);
   1129 }
   1130 
   1131 void
   1132 siop_scsipi_request(chan, req, arg)
   1133 	struct scsipi_channel *chan;
   1134 	scsipi_adapter_req_t req;
   1135 	void *arg;
   1136 {
   1137 	struct scsipi_xfer *xs;
   1138 	struct scsipi_periph *periph;
   1139 	struct siop_softc *sc = (void *)chan->chan_adapter->adapt_dev;
   1140 	struct siop_cmd *siop_cmd;
   1141 	int s, error, i;
   1142 	int target;
   1143 	int lun;
   1144 
   1145 	switch (req) {
   1146 	case ADAPTER_REQ_RUN_XFER:
   1147 		xs = arg;
   1148 		periph = xs->xs_periph;
   1149 		target = periph->periph_target;
   1150 		lun = periph->periph_lun;
   1151 
   1152 		s = splbio();
   1153 #ifdef SIOP_DEBUG_SCHED
   1154 		printf("starting cmd for %d:%d\n", target, lun);
   1155 #endif
   1156 		siop_cmd = TAILQ_FIRST(&sc->free_list);
   1157 		if (siop_cmd) {
   1158 			TAILQ_REMOVE(&sc->free_list, siop_cmd, next);
   1159 		} else {
   1160 			if (siop_morecbd(sc) == 0) {
   1161 				siop_cmd = TAILQ_FIRST(&sc->free_list);
   1162 #ifdef DIAGNOSTIC
   1163 				if (siop_cmd == NULL)
   1164 					panic("siop_morecbd succeed and does nothing");
   1165 #endif
   1166 				TAILQ_REMOVE(&sc->free_list, siop_cmd, next);
   1167 			}
   1168 		}
   1169 		if (siop_cmd == NULL) {
   1170 			xs->error = XS_RESOURCE_SHORTAGE;
   1171 			splx(s);
   1172 			return;
   1173 		}
   1174 #ifdef DIAGNOSTIC
   1175 		if (siop_cmd->status != CMDST_FREE)
   1176 			panic("siop_scsicmd: new cmd not free");
   1177 #endif
   1178 		if (sc->targets[target] == NULL) {
   1179 #ifdef SIOP_DEBUG
   1180 			printf("%s: alloc siop_target for target %d\n",
   1181 				sc->sc_dev.dv_xname, target);
   1182 #endif
   1183 			sc->targets[target] =
   1184 			    malloc(sizeof(struct siop_target),
   1185 				M_DEVBUF, M_NOWAIT);
   1186 			if (sc->targets[target] == NULL) {
   1187 				printf("%s: can't malloc memory for "
   1188 				    "target %d\n", sc->sc_dev.dv_xname, target);
   1189 				xs->error = XS_RESOURCE_SHORTAGE;
   1190 				splx(s);
   1191 				return;
   1192 			}
   1193 			sc->targets[target]->status = TARST_PROBING;
   1194 			sc->targets[target]->flags = 0;
   1195 			sc->targets[target]->id =
   1196 			    sc->clock_div << 24; /* scntl3 */
   1197 			sc->targets[target]->id |=  target << 16; /* id */
   1198 			/* sc->targets[target]->id |= 0x0 << 8; scxfer is 0 */
   1199 
   1200 			/* get a lun switch script */
   1201 			sc->targets[target]->lunsw = siop_get_lunsw(sc);
   1202 			if (sc->targets[target]->lunsw == NULL) {
   1203 				printf("%s: can't alloc lunsw for target %d\n",
   1204 				    sc->sc_dev.dv_xname, target);
   1205 				xs->error = XS_RESOURCE_SHORTAGE;
   1206 				splx(s);
   1207 				return;
   1208 			}
   1209 			for (i=0; i < 8; i++)
   1210 				sc->targets[target]->siop_lun[i] = NULL;
   1211 			siop_add_reselsw(sc, target);
   1212 		}
   1213 		if (sc->targets[target]->siop_lun[lun] == NULL) {
   1214 			sc->targets[target]->siop_lun[lun] =
   1215 			    malloc(sizeof(struct siop_lun), M_DEVBUF, M_NOWAIT);
   1216 			if (sc->targets[target]->siop_lun[lun] == NULL) {
   1217 				printf("%s: can't alloc siop_lun for "
   1218 				    "target %d lun %d\n",
   1219 				    sc->sc_dev.dv_xname, target, lun);
   1220 				xs->error = XS_RESOURCE_SHORTAGE;
   1221 				splx(s);
   1222 				return;
   1223 			}
   1224 			memset(sc->targets[target]->siop_lun[lun], 0,
   1225 			    sizeof(struct siop_lun));
   1226 		}
   1227 		siop_cmd->siop_target = sc->targets[target];
   1228 		siop_cmd->xs = xs;
   1229 		siop_cmd->flags = 0;
   1230 		siop_cmd->status = CMDST_READY;
   1231 
   1232 		/* load the DMA maps */
   1233 		error = bus_dmamap_load(sc->sc_dmat, siop_cmd->dmamap_cmd,
   1234 		    xs->cmd, xs->cmdlen, NULL, BUS_DMA_NOWAIT);
   1235 		if (error) {
   1236 			printf("%s: unable to load cmd DMA map: %d",
   1237 			    sc->sc_dev.dv_xname, error);
   1238 			xs->error = XS_DRIVER_STUFFUP;
   1239 			splx(s);
   1240 			return;
   1241 		}
   1242 		if (xs->xs_control & (XS_CTL_DATA_IN | XS_CTL_DATA_OUT)) {
   1243 			error = bus_dmamap_load(sc->sc_dmat,
   1244 			    siop_cmd->dmamap_data, xs->data, xs->datalen,
   1245 			    NULL, BUS_DMA_NOWAIT);
   1246 			if (error) {
   1247 				printf("%s: unable to load cmd DMA map: %d",
   1248 				    sc->sc_dev.dv_xname, error);
   1249 				xs->error = XS_DRIVER_STUFFUP;
   1250 				bus_dmamap_unload(sc->sc_dmat, siop_cmd->dmamap_cmd);
   1251 				splx(s);
   1252 				return;
   1253 			}
   1254 			bus_dmamap_sync(sc->sc_dmat, siop_cmd->dmamap_data, 0,
   1255 			    siop_cmd->dmamap_data->dm_mapsize,
   1256 			    (xs->xs_control & XS_CTL_DATA_IN) ?
   1257 			    BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
   1258 		}
   1259 		bus_dmamap_sync(sc->sc_dmat, siop_cmd->dmamap_cmd, 0,
   1260 		    siop_cmd->dmamap_cmd->dm_mapsize, BUS_DMASYNC_PREWRITE);
   1261 
   1262 		siop_setuptables(siop_cmd);
   1263 
   1264 		TAILQ_INSERT_TAIL(&sc->ready_list, siop_cmd, next);
   1265 		siop_start(sc);
   1266 		if (xs->xs_control & XS_CTL_POLL) {
   1267 			/* poll for command completion */
   1268 			while ((xs->xs_status & XS_STS_DONE) == 0) {
   1269 				delay(1000);
   1270 				siop_intr(sc);
   1271 			}
   1272 		}
   1273 		splx(s);
   1274 		return;
   1275 
   1276 	case ADAPTER_REQ_GROW_RESOURCES:
   1277 		/* XXX Not supported. */
   1278 		return;
   1279 
   1280 	case ADAPTER_REQ_SET_XFER_MODE:
   1281 	{
   1282 		struct scsipi_xfer_mode *xm = arg;
   1283 		if (sc->targets[xm->xm_target] == NULL)
   1284 			return;
   1285 		s = splbio();
   1286 		if (xm->xm_mode & PERIPH_CAP_TQING)
   1287 			sc->targets[xm->xm_target]->flags |= TARF_TAG;
   1288 		if ((xm->xm_mode & PERIPH_CAP_WIDE16) &&
   1289 		    (sc->features & SF_BUS_WIDE))
   1290 			sc->targets[xm->xm_target]->flags |= TARF_WIDE;
   1291 		if (xm->xm_mode & PERIPH_CAP_SYNC)
   1292 			sc->targets[xm->xm_target]->flags |= TARF_SYNC;
   1293 		if ((xm->xm_mode & (PERIPH_CAP_SYNC | PERIPH_CAP_WIDE16)) ||
   1294 		    sc->targets[xm->xm_target]->status == TARST_PROBING)
   1295 			sc->targets[xm->xm_target]->status =
   1296 			    TARST_ASYNC;
   1297 
   1298 		for (lun = 0; lun < sc->sc_chan.chan_nluns; lun++) {
   1299 			if (sc->sc_chan.chan_periphs[xm->xm_target][lun])
   1300 				/* allocate a lun sw entry for this device */
   1301 				siop_add_dev(sc, xm->xm_target, lun);
   1302 		}
   1303 
   1304 		splx(s);
   1305 	}
   1306 	}
   1307 }
   1308 
   1309 void
   1310 siop_start(sc)
   1311 	struct siop_softc *sc;
   1312 {
   1313 	struct siop_cmd *siop_cmd, *next_siop_cmd;
   1314 	struct siop_lun *siop_lun;
   1315 	u_int32_t dsa;
   1316 	int timeout;
   1317 	int target, lun, tag, slot;
   1318 	int newcmd = 0;
   1319 	int doingready = 0;
   1320 
   1321 	/*
   1322 	 * first make sure to read valid data
   1323 	 */
   1324 	siop_script_sync(sc, BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
   1325 
   1326 	/*
   1327 	 * The queue management here is a bit tricky: the script always looks
   1328 	 * at the slot from first to last, so if we always use the first
   1329 	 * free slot commands can stay at the tail of the queue ~forever.
   1330 	 * The algorithm used here is to restart from the head when we know
   1331 	 * that the queue is empty, and only add commands after the last one.
   1332 	 * When we're at the end of the queue wait for the script to clear it.
   1333 	 * The best thing to do here would be to implement a circular queue,
   1334 	 * but using only 53c720 features this can be "interesting".
   1335 	 * A mid-way solution could be to implement 2 queues and swap orders.
   1336 	 */
   1337 	slot = sc->sc_currschedslot;
   1338 	/*
   1339 	 * If the instruction is 0x80000000 (JUMP foo, IF FALSE) the slot is
   1340 	 * free. As this is the last used slot, all previous slots are free,
   1341 	 * we can restart from 0.
   1342 	 */
   1343 	if (siop_script_read(sc, (Ent_script_sched_slot0 / 4) + slot * 2) ==
   1344 	    0x80000000) {
   1345 		slot = sc->sc_currschedslot = 0;
   1346 	} else {
   1347 		slot++;
   1348 	}
   1349 	/* first handle commands from the urgent list */
   1350 	siop_cmd = TAILQ_FIRST(&sc->urgent_list);
   1351 again:
   1352 	for (; siop_cmd != NULL; siop_cmd = next_siop_cmd) {
   1353 		next_siop_cmd = TAILQ_NEXT(siop_cmd, next);
   1354 #ifdef DIAGNOSTIC
   1355 		if (siop_cmd->status != CMDST_READY)
   1356 			panic("siop: non-ready cmd in ready list");
   1357 #endif
   1358 		target = siop_cmd->xs->xs_periph->periph_target;
   1359 		lun = siop_cmd->xs->xs_periph->periph_lun;
   1360 		siop_lun = sc->targets[target]->siop_lun[lun];
   1361 		/* if non-tagged command active, wait */
   1362 		if (siop_lun->siop_tag[0].active != NULL)
   1363 			continue;
   1364 		/* find a free tag if needed */
   1365 		if (siop_cmd->flags & CMDFL_TAG) {
   1366 			tag = siop_cmd->xs->xs_tag_id + 1;
   1367 #ifdef DIAGNOSTIC
   1368 			if (siop_lun->siop_tag[tag].active != NULL)
   1369 				panic("siop_start: tag not free");
   1370 			if (tag >= SIOP_NTAG) {
   1371 				scsipi_printaddr(siop_cmd->xs->xs_periph);
   1372 				printf(": tag id %d\n", tag);
   1373 				panic("siop_start: invalid tag id");
   1374 			}
   1375 #endif
   1376 		} else {
   1377 			tag = 0;
   1378 		}
   1379 		siop_cmd->tag = tag;
   1380 		/* find a free scheduler slot and load it */
   1381 		for (; slot < SIOP_NSLOTS; slot++) {
   1382 			/*
   1383 			 * If cmd if 0x80000000 the slot is free
   1384 			 */
   1385 			if (siop_script_read(sc,
   1386 			    (Ent_script_sched_slot0 / 4) + slot * 2) ==
   1387 			    0x80000000)
   1388 				break;
   1389 		}
   1390 		/* no more free slot, no need to continue */
   1391 		if (slot == SIOP_NSLOTS) {
   1392 			goto end;
   1393 		}
   1394 #ifdef SIOP_DEBUG_SCHED
   1395 		printf("using slot %d for DSA 0x%lx\n", slot,
   1396 		    (u_long)siop_cmd->dsa);
   1397 #endif
   1398 		/* Ok, we can add the tag message */
   1399 		if (tag > 0) {
   1400 #ifdef DIAGNOSTIC
   1401 			int msgcount =
   1402 			    le32toh(siop_cmd->siop_tables.t_msgout.count);
   1403 			if (msgcount != 1)
   1404 				printf("%s:%d:%d: tag %d with msgcount %d\n",
   1405 				    sc->sc_dev.dv_xname, target, lun, tag,
   1406 				    msgcount);
   1407 #endif
   1408 			siop_cmd->siop_tables.msg_out[1] =
   1409 			    siop_cmd->xs->xs_tag_type;
   1410 			siop_cmd->siop_tables.msg_out[2] = tag;
   1411 			siop_cmd->siop_tables.t_msgout.count = htole32(3);
   1412 		}
   1413 		/* note that we started a new command */
   1414 		newcmd = 1;
   1415 		/* mark command as active */
   1416 		if (siop_cmd->status == CMDST_READY) {
   1417 			siop_cmd->status = CMDST_ACTIVE;
   1418 		} else
   1419 			panic("siop_start: bad status");
   1420 		if (doingready)
   1421 			TAILQ_REMOVE(&sc->ready_list, siop_cmd, next);
   1422 		else
   1423 			TAILQ_REMOVE(&sc->urgent_list, siop_cmd, next);
   1424 		siop_lun->siop_tag[tag].active = siop_cmd;
   1425 		/* patch scripts with DSA addr */
   1426 		dsa = siop_cmd->dsa;
   1427 		/* first reselect switch, if we have an entry */
   1428 		if (siop_lun->siop_tag[tag].reseloff > 0)
   1429 			siop_script_write(sc,
   1430 			    siop_lun->siop_tag[tag].reseloff + 1,
   1431 			    dsa + sizeof(struct siop_xfer_common) +
   1432 			    Ent_ldsa_reload_dsa);
   1433 		/* CMD script: MOVE MEMORY addr */
   1434 		siop_cmd->siop_xfer->resel[E_ldsa_abs_slot_Used[0]] =
   1435 		   htole32(sc->sc_scriptaddr + Ent_script_sched_slot0 +
   1436 		   slot * 8);
   1437 		/* scheduler slot: JUMP ldsa_select */
   1438 		siop_script_write(sc,
   1439 		    (Ent_script_sched_slot0 / 4) + slot * 2 + 1,
   1440 		    dsa + sizeof(struct siop_xfer_common) + Ent_ldsa_select);
   1441 		/* handle timeout */
   1442 		if (siop_cmd->status == CMDST_ACTIVE) {
   1443 			if ((siop_cmd->xs->xs_control &
   1444 			    XS_CTL_POLL) == 0) {
   1445 				/* start exire timer */
   1446 				timeout = (u_int64_t) siop_cmd->xs->timeout *
   1447 				    (u_int64_t)hz / 1000;
   1448 				if (timeout == 0)
   1449 					timeout = 1;
   1450 				callout_reset( &siop_cmd->xs->xs_callout,
   1451 				    timeout, siop_timeout, siop_cmd);
   1452 			}
   1453 		}
   1454 		/*
   1455 		 * Change JUMP cmd so that this slot will be handled
   1456 		 */
   1457 		siop_script_write(sc, (Ent_script_sched_slot0 / 4) + slot * 2,
   1458 		    0x80080000);
   1459 		sc->sc_currschedslot = slot;
   1460 		slot++;
   1461 	}
   1462 	if (doingready == 0) {
   1463 		/* now process ready list */
   1464 		doingready = 1;
   1465 		siop_cmd = TAILQ_FIRST(&sc->ready_list);
   1466 		goto again;
   1467 	}
   1468 
   1469 end:
   1470 	/* if nothing changed no need to flush cache and wakeup script */
   1471 	if (newcmd == 0)
   1472 		return;
   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