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