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