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