xref: /netbsd-src/sys/arch/amiga/dev/siop2.c (revision 0dd5877adce57db949b16ae963e5a6831cccdfb6)
1 /*	$NetBSD: siop2.c,v 1.18 2002/01/28 09:57:03 aymeric Exp $ */
2 
3 /*
4  * Copyright (c) 1994,1998 Michael L. Hitch
5  * Copyright (c) 1990 The Regents of the University of California.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * Van Jacobson of Lawrence Berkeley Laboratory.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *	This product includes software developed by the University of
22  *	California, Berkeley and its contributors.
23  * 4. Neither the name of the University nor the names of its contributors
24  *    may be used to endorse or promote products derived from this software
25  *    without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37  * SUCH DAMAGE.
38  *
39  *	@(#)siop.c	7.5 (Berkeley) 5/4/91
40  */
41 
42 /*
43  * AMIGA 53C720/770 scsi adaptor driver
44  */
45 
46 #include "opt_ddb.h"
47 
48 #include <sys/cdefs.h>
49 __KERNEL_RCSID(0, "$NetBSD: siop2.c,v 1.18 2002/01/28 09:57:03 aymeric Exp $");
50 
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/device.h>
54 #include <sys/disklabel.h>
55 #include <sys/dkstat.h>
56 #include <sys/buf.h>
57 #include <sys/malloc.h>
58 #include <dev/scsipi/scsi_all.h>
59 #include <dev/scsipi/scsipi_all.h>
60 #include <dev/scsipi/scsiconf.h>
61 #include <machine/cpu.h>
62 #ifdef __m68k__
63 #include <m68k/include/cacheops.h>
64 #endif
65 #include <amiga/amiga/custom.h>
66 #include <amiga/amiga/isr.h>
67 
68 #define ARCH_720
69 
70 #include <amiga/dev/siopreg.h>
71 #include <amiga/dev/siopvar.h>
72 
73 /*
74  * SCSI delays
75  * In u-seconds, primarily for state changes on the SPC.
76  */
77 #define	SCSI_CMD_WAIT	500000	/* wait per step of 'immediate' cmds */
78 #define	SCSI_DATA_WAIT	500000	/* wait per data in/out step */
79 #define	SCSI_INIT_WAIT	500000	/* wait per step (both) during init */
80 
81 void siopng_select(struct siop_softc *);
82 void siopngabort(struct siop_softc *, siop_regmap_p, char *);
83 void siopngerror(struct siop_softc *, siop_regmap_p, u_char);
84 void siopngstart(struct siop_softc *);
85 int  siopng_checkintr(struct siop_softc *, u_char, u_char, u_short, int *);
86 void siopngreset(struct siop_softc *);
87 void siopngsetdelay(int);
88 void siopng_scsidone(struct siop_acb *, int);
89 void siopng_sched(struct siop_softc *);
90 void siopng_poll(struct siop_softc *, struct siop_acb *);
91 void siopngintr(struct siop_softc *);
92 void scsi_period_to_siopng(struct siop_softc *, int);
93 void siopng_start(struct siop_softc *, int, int, u_char *, int, u_char *, int);
94 void siopng_dump_acb(struct siop_acb *);
95 
96 /* 53C720/770 script */
97 
98 #include <amiga/dev/siop2_script.out>
99 
100 /* default to not inhibit sync negotiation on any drive */
101 u_char siopng_inhibit_sync[16] = {
102 	0, 0, 0, 0,	0, 0, 0, 0,	0, 0, 0, 0,	0, 0, 0, 0
103 }; /* initialize, so patchable */
104 
105 u_char siopng_inhibit_wide[16] = {
106 	0, 0, 0, 0,	0, 0, 0, 0,	0, 0, 0, 0,	0, 0, 0, 0
107 }; /* initialize, so patchable */
108 
109 u_char siopng_allow_disc[16] = {
110 	3, 3, 3, 3,	3, 3, 3, 3,	3, 3, 3, 3,	3, 3, 3, 3
111 };
112 
113 int siopng_no_dma = 0;
114 
115 int siopng_reset_delay = 250;	/* delay after reset, in milleseconds */
116 
117 int siopng_cmd_wait = SCSI_CMD_WAIT;
118 int siopng_data_wait = SCSI_DATA_WAIT;
119 int siopng_init_wait = SCSI_INIT_WAIT;
120 
121 #define DEBUG_SYNC
122 
123 #ifdef DEBUG
124 /*
125  *	0x01 - full debug
126  *	0x02 - DMA chaining
127  *	0x04 - siopngintr
128  *	0x08 - phase mismatch
129  *	0x10 - <not used>
130  *	0x20 - panic on unhandled exceptions
131  *	0x100 - disconnect/reselect
132  */
133 int	siopng_debug = 0;
134 int	siopngsync_debug = 0;
135 int	siopngdma_hits = 0;
136 int	siopngdma_misses = 0;
137 int	siopngchain_ints = 0;
138 int	siopngstarts = 0;
139 int	siopngints = 0;
140 int	siopngphmm = 0;
141 #define SIOP_TRACE_SIZE	128
142 #define SIOP_TRACE(a,b,c,d) \
143 	siopng_trbuf[siopng_trix] = (a); \
144 	siopng_trbuf[siopng_trix+1] = (b); \
145 	siopng_trbuf[siopng_trix+2] = (c); \
146 	siopng_trbuf[siopng_trix+3] = (d); \
147 	siopng_trix = (siopng_trix + 4) & (SIOP_TRACE_SIZE - 1);
148 u_char	siopng_trbuf[SIOP_TRACE_SIZE];
149 int	siopng_trix;
150 void siopng_dump(struct siop_softc *);
151 void siopng_dump_trace(void);
152 #else
153 #define SIOP_TRACE(a,b,c,d)
154 #endif
155 
156 
157 static char *siopng_chips[] = {
158 	"720", "720SE", "770", "0x3",
159 	"810A", "0x5", "0x6", "0x7",
160 	"0x8", "0x9", "0xA", "0xB",
161 	"0xC", "0xD", "0xE", "0xF",
162 };
163 
164 /*
165  * default minphys routine for siopng based controllers
166  */
167 void
168 siopng_minphys(struct buf *bp)
169 {
170 
171 	/*
172 	 * No max transfer at this level.
173 	 */
174 	minphys(bp);
175 }
176 
177 /*
178  * used by specific siopng controller
179  *
180  */
181 void
182 siopng_scsipi_request(struct scsipi_channel *chan, scsipi_adapter_req_t req,
183                       void *arg)
184 {
185 	struct scsipi_xfer *xs;
186 	struct scsipi_periph *periph;
187 	struct siop_acb *acb;
188 	struct siop_softc *sc = (void *)chan->chan_adapter->adapt_dev;
189 	int flags, s;
190 
191 	switch (req) {
192 	case ADAPTER_REQ_RUN_XFER:
193 		xs = arg;
194 		periph = xs->xs_periph;
195 		flags = xs->xs_control;
196 
197 		/* XXXX ?? */
198 		if (flags & XS_CTL_DATA_UIO)
199 			panic("siopng: scsi data uio requested");
200 
201 		/* XXXX ?? */
202 		if (sc->sc_nexus && flags & XS_CTL_POLL)
203 /*			panic("siopng_scsicmd: busy");*/
204 			printf("siopng_scsicmd: busy\n");
205 
206 		s = splbio();
207 		acb = sc->free_list.tqh_first;
208 		if (acb) {
209 			TAILQ_REMOVE(&sc->free_list, acb, chain);
210 		}
211 		splx(s);
212 
213 #ifdef DIAGNOSTIC
214 		/*
215 		 * This should never happen as we track the resources
216 		 * in the mid-layer.
217 		 */
218 		if (acb == NULL) {
219 			scsipi_printaddr(periph);
220 			printf("unable to allocate acb\n");
221 			panic("siopng_scsipi_request");
222 		}
223 #endif
224 		acb->flags = ACB_ACTIVE;
225 		acb->xs = xs;
226 		bcopy(xs->cmd, &acb->cmd, xs->cmdlen);
227 		acb->clen = xs->cmdlen;
228 		acb->daddr = xs->data;
229 		acb->dleft = xs->datalen;
230 
231 		s = splbio();
232 		TAILQ_INSERT_TAIL(&sc->ready_list, acb, chain);
233 
234 		if (sc->sc_nexus == NULL)
235 			siopng_sched(sc);
236 
237 		splx(s);
238 
239 		if (flags & XS_CTL_POLL || siopng_no_dma)
240 			siopng_poll(sc, acb);
241 		return;
242 
243 	case ADAPTER_REQ_GROW_RESOURCES:
244 		return;
245 
246 	case ADAPTER_REQ_SET_XFER_MODE:
247 		return;
248 	}
249 }
250 
251 void
252 siopng_poll(struct siop_softc *sc, struct siop_acb *acb)
253 {
254 	siop_regmap_p rp = sc->sc_siopp;
255 	struct scsipi_xfer *xs = acb->xs;
256 	int i;
257 	int status;
258 	u_char istat;
259 	u_char dstat;
260 	u_short sist;
261 	int s;
262 	int to;
263 
264 	s = splbio();
265 	to = xs->timeout / 1000;
266 	if (sc->nexus_list.tqh_first)
267 		printf("%s: siopng_poll called with disconnected device\n",
268 		    sc->sc_dev.dv_xname);
269 	for (;;) {
270 		/* use cmd_wait values? */
271 		i = 50000;
272 		/* XXX spl0(); */
273 		while (((istat = rp->siop_istat) &
274 		    (SIOP_ISTAT_SIP | SIOP_ISTAT_DIP)) == 0) {
275 			if (--i <= 0) {
276 #ifdef DEBUG
277 				printf ("waiting: tgt %d cmd %02x sbcl %02x istat %02x sbdl %04x\n         dsp %lx (+%lx) dcmd %lx ds %p timeout %d\n",
278 				    xs->xs_periph->periph_target, acb->cmd.opcode,
279 				    rp->siop_sbcl, istat, rp->siop_sbdl, rp->siop_dsp,
280 				    rp->siop_dsp - sc->sc_scriptspa,
281 				    *((long *)&rp->siop_dcmd), &acb->ds, acb->xs->timeout);
282 #endif
283 				i = 50000;
284 				--to;
285 				if (to <= 0) {
286 					siopngreset(sc);
287 					return;
288 				}
289 			}
290 			delay(20);
291 		}
292 		sist = rp->siop_sist;
293 		dstat = rp->siop_dstat;
294 		if (siopng_checkintr(sc, istat, dstat, sist, &status)) {
295 			if (acb != sc->sc_nexus)
296 				printf("%s: siopng_poll disconnected device completed\n",
297 				    sc->sc_dev.dv_xname);
298 			else if ((sc->sc_flags & SIOP_INTDEFER) == 0) {
299 				sc->sc_flags &= ~SIOP_INTSOFF;
300 				rp->siop_sien = sc->sc_sien;
301 				rp->siop_dien = sc->sc_dien;
302 			}
303 			siopng_scsidone(sc->sc_nexus, status);
304 		}
305 		if (xs->xs_status & XS_STS_DONE)
306 			break;
307 	}
308 	splx(s);
309 }
310 
311 /*
312  * start next command that's ready
313  */
314 void
315 siopng_sched(struct siop_softc *sc)
316 {
317 	struct scsipi_periph *periph;
318 	struct siop_acb *acb;
319 	int i;
320 
321 #ifdef DEBUG
322 	if (sc->sc_nexus) {
323 		printf("%s: siopng_sched- nexus %p/%d ready %p/%d\n",
324 		    sc->sc_dev.dv_xname, sc->sc_nexus,
325 		    sc->sc_nexus->xs->xs_periph->periph_target,
326 		    sc->ready_list.tqh_first,
327 		    sc->ready_list.tqh_first->xs->xs_periph->periph_target);
328 		return;
329 	}
330 #endif
331 	for (acb = sc->ready_list.tqh_first; acb; acb = acb->chain.tqe_next) {
332 		periph = acb->xs->xs_periph;
333 		i = periph->periph_target;
334 		if(!(sc->sc_tinfo[i].lubusy & (1 << periph->periph_lun))) {
335 			struct siop_tinfo *ti = &sc->sc_tinfo[i];
336 
337 			TAILQ_REMOVE(&sc->ready_list, acb, chain);
338 			sc->sc_nexus = acb;
339 			periph = acb->xs->xs_periph;
340 			ti = &sc->sc_tinfo[periph->periph_target];
341 			ti->lubusy |= (1 << periph->periph_lun);
342 			break;
343 		}
344 	}
345 
346 	if (acb == NULL) {
347 #ifdef DEBUGXXX
348 		printf("%s: siopng_sched didn't find ready command\n",
349 		    sc->sc_dev.dv_xname);
350 #endif
351 		return;
352 	}
353 
354 	if (acb->xs->xs_control & XS_CTL_RESET)
355 		siopngreset(sc);
356 
357 #if 0
358 	acb->cmd.bytes[0] |= periph->periph_lun << 5;	/* XXXX */
359 #endif
360 	++sc->sc_active;
361 	siopng_select(sc);
362 }
363 
364 void
365 siopng_scsidone(struct siop_acb *acb, int stat)
366 {
367 	struct scsipi_xfer *xs;
368 	struct scsipi_periph *periph;
369 	struct siop_softc *sc;
370 	int dosched = 0;
371 
372 	if (acb == NULL || (xs = acb->xs) == NULL) {
373 #ifdef DIAGNOSTIC
374 		printf("siopng_scsidone: NULL acb or scsipi_xfer\n");
375 #if defined(DEBUG) && defined(DDB)
376 		Debugger();
377 #endif
378 #endif
379 		return;
380 	}
381 	periph = xs->xs_periph;
382 	sc = (void *)periph->periph_channel->chan_adapter->adapt_dev;
383 
384 	xs->status = stat;
385 	xs->resid = 0;		/* XXXX */
386 
387 	if (xs->error == XS_NOERROR) {
388 		if (stat == SCSI_CHECK || stat == SCSI_BUSY)
389 			xs->error = XS_BUSY;
390 	}
391 
392 	/*
393 	 * Remove the ACB from whatever queue it's on.  We have to do a bit of
394 	 * a hack to figure out which queue it's on.  Note that it is *not*
395 	 * necessary to cdr down the ready queue, but we must cdr down the
396 	 * nexus queue and see if it's there, so we can mark the unit as no
397 	 * longer busy.  This code is sickening, but it works.
398 	 */
399 	if (acb == sc->sc_nexus) {
400 		sc->sc_nexus = NULL;
401 		sc->sc_tinfo[periph->periph_target].lubusy &=
402 			~(1<<periph->periph_lun);
403 		if (sc->ready_list.tqh_first)
404 			dosched = 1;	/* start next command */
405 		--sc->sc_active;
406 		SIOP_TRACE('d','a',stat,0)
407 	} else if (sc->ready_list.tqh_last == &acb->chain.tqe_next) {
408 		TAILQ_REMOVE(&sc->ready_list, acb, chain);
409 		SIOP_TRACE('d','r',stat,0)
410 	} else {
411 		register struct siop_acb *acb2;
412 		for (acb2 = sc->nexus_list.tqh_first; acb2;
413 		    acb2 = acb2->chain.tqe_next)
414 			if (acb2 == acb) {
415 				TAILQ_REMOVE(&sc->nexus_list, acb, chain);
416 				sc->sc_tinfo[periph->periph_target].lubusy
417 					&= ~(1<<periph->periph_lun);
418 				--sc->sc_active;
419 				break;
420 			}
421 		if (acb2)
422 			;
423 		else if (acb->chain.tqe_next) {
424 			TAILQ_REMOVE(&sc->ready_list, acb, chain);
425 			--sc->sc_active;
426 		} else {
427 			printf("%s: can't find matching acb\n",
428 			    sc->sc_dev.dv_xname);
429 #ifdef DDB
430 /*			Debugger(); */
431 #endif
432 		}
433 		SIOP_TRACE('d','n',stat,0);
434 	}
435 	/* Put it on the free list. */
436 	acb->flags = ACB_FREE;
437 	TAILQ_INSERT_HEAD(&sc->free_list, acb, chain);
438 
439 	sc->sc_tinfo[periph->periph_target].cmds++;
440 
441 	scsipi_done(xs);
442 
443 	if (dosched && sc->sc_nexus == NULL)
444 		siopng_sched(sc);
445 }
446 
447 void
448 siopngabort(register struct siop_softc *sc, siop_regmap_p rp, char *where)
449 {
450 #ifdef fix_this
451 	int i;
452 #endif
453 
454 	printf ("%s: abort %s: dstat %02x, istat %02x sist %04x sien %04x sbcl %02x\n",
455 	    sc->sc_dev.dv_xname,
456 	    where, rp->siop_dstat, rp->siop_istat, rp->siop_sist,
457 	    rp->siop_sien, rp->siop_sbcl);
458 	siopng_dump_registers(sc);
459 
460 	if (sc->sc_active > 0) {
461 #ifdef TODO
462       SET_SBIC_cmd (rp, SBIC_CMD_ABORT);
463       WAIT_CIP (rp);
464 
465       GET_SBIC_asr (rp, asr);
466       if (asr & (SBIC_ASR_BSY|SBIC_ASR_LCI))
467         {
468           /* ok, get more drastic.. */
469 
470 	  SET_SBIC_cmd (rp, SBIC_CMD_RESET);
471 	  delay(25);
472 	  SBIC_WAIT(rp, SBIC_ASR_INT, 0);
473 	  GET_SBIC_csr (rp, csr);       /* clears interrupt also */
474 
475           return;
476         }
477 
478       do
479         {
480           SBIC_WAIT (rp, SBIC_ASR_INT, 0);
481           GET_SBIC_csr (rp, csr);
482         }
483       while ((csr != SBIC_CSR_DISC) && (csr != SBIC_CSR_DISC_1)
484 	      && (csr != SBIC_CSR_CMD_INVALID));
485 #endif
486 
487 		/* lets just hope it worked.. */
488 #ifdef fix_this
489 		for (i = 0; i < 2; ++i) {
490 			if (sc->sc_iob[i].sc_xs && &sc->sc_iob[i] !=
491 			    sc->sc_cur) {
492 				printf ("siopngabort: cleanup!\n");
493 				sc->sc_iob[i].sc_xs = NULL;
494 			}
495 		}
496 #endif	/* fix_this */
497 /*		sc->sc_active = 0; */
498 	}
499 }
500 
501 void
502 siopnginitialize(struct siop_softc *sc)
503 {
504 	int i;
505 	u_int inhibit_sync;
506 	extern u_long scsi_nosync;
507 	extern int shift_nosync;
508 
509 	/*
510 	 * Need to check that scripts is on a long word boundary
511 	 * Also should verify that dev doesn't span non-contiguous
512 	 * physical pages.
513 	 */
514 	sc->sc_scriptspa = kvtop((caddr_t)siopng_scripts);
515 
516 	/*
517 	 * malloc sc_acb to ensure that DS is on a long word boundary.
518 	 */
519 
520 	MALLOC(sc->sc_acb, struct siop_acb *,
521 		sizeof(struct siop_acb) * SIOP_NACB, M_DEVBUF, M_NOWAIT);
522 	if (sc->sc_acb == NULL)
523 		panic("siopnginitialize: ACB malloc failed!");
524 
525 	sc->sc_tcp[1] = 1000 / sc->sc_clock_freq;
526 	sc->sc_tcp[2] = 1500 / sc->sc_clock_freq;
527 	sc->sc_tcp[3] = 2000 / sc->sc_clock_freq;
528 	sc->sc_minsync = sc->sc_tcp[1];		/* in 4ns units */
529 	if (sc->sc_minsync < 25)
530 		sc->sc_minsync = 25;
531 	sc->sc_minsync >>= 1;			/* Using clock doubler, allow Ultra */
532 	if (sc->sc_clock_freq <= 25) {
533 		sc->sc_dcntl |= 0x80;		/* SCLK/1 */
534 		sc->sc_tcp[0] = sc->sc_tcp[1];
535 	} else if (sc->sc_clock_freq <= 37) {
536 		sc->sc_dcntl |= 0x40;		/* SCLK/1.5 */
537 		sc->sc_tcp[0] = sc->sc_tcp[2];
538 	} else if (sc->sc_clock_freq <= 50) {
539 		sc->sc_dcntl |= 0x00;		/* SCLK/2 */
540 		sc->sc_tcp[0] = sc->sc_tcp[3];
541 	} else {
542 		sc->sc_dcntl |= 0xc0;		/* SCLK/3 */
543 		sc->sc_tcp[0] = 3000 / sc->sc_clock_freq;
544 	}
545 
546 	if (scsi_nosync) {
547 		inhibit_sync = (scsi_nosync >> shift_nosync) & 0xffff;
548 		shift_nosync += 16;		/* XXX maxtarget */
549 #ifdef DEBUG
550 		if (inhibit_sync)
551 			printf("%s: Inhibiting synchronous transfer %02x\n",
552 				sc->sc_dev.dv_xname, inhibit_sync);
553 #endif
554 		for (i = 0; i < 16; ++i)		/* XXX maxtarget */
555 			if (inhibit_sync & (1 << i))
556 				siopng_inhibit_sync[i] = 1;
557 	}
558 
559 	siopngreset (sc);
560 }
561 
562 void
563 siopngreset(struct siop_softc *sc)
564 {
565 	siop_regmap_p rp;
566 	u_int i, s;
567 	u_short  dummy;
568 	struct siop_acb *acb;
569 
570 	rp = sc->sc_siopp;
571 
572 	if (sc->sc_flags & SIOP_ALIVE)
573 		siopngabort(sc, rp, "reset");
574 
575 	printf("%s: ", sc->sc_dev.dv_xname);		/* XXXX */
576 
577 	s = splbio();
578 
579 	/*
580 	 * Reset the chip
581 	 * XXX - is this really needed?
582 	 */
583 	rp->siop_istat |= SIOP_ISTAT_ABRT;	/* abort current script */
584 	rp->siop_istat |= SIOP_ISTAT_RST;		/* reset chip */
585 	rp->siop_istat &= ~SIOP_ISTAT_RST;
586 	/*
587 	 * Reset SCSI bus (do we really want this?)
588 	 */
589 	rp->siop_sien = 0;
590 	rp->siop_scntl1 |= SIOP_SCNTL1_RST;
591 	delay(1);
592 	rp->siop_scntl1 &= ~SIOP_SCNTL1_RST;
593 
594 	/*
595 	 * Set up various chip parameters
596 	 */
597 	rp->siop_stest1 |= SIOP_STEST1_DBLEN;	/* SCLK doubler enable */
598 	delay(20);
599 	rp->siop_stest3 |= SIOP_STEST3_HSC;	/* Halt SCSI clock */
600 	rp->siop_scntl3 = 0x15;			/* SCF/CCF*/
601 	rp->siop_stest1 |= SIOP_STEST1_DBLSEL;	/* SCLK doubler select */
602 	rp->siop_stest3 &= ~SIOP_STEST3_HSC;	/* Clear Halt SCSI clock */
603 	rp->siop_scntl0 = SIOP_ARB_FULL | /*SIOP_SCNTL0_EPC |*/ SIOP_SCNTL0_EPG;
604 	rp->siop_dcntl = sc->sc_dcntl;
605 	rp->siop_dmode = 0xc0;		/* XXX burst length */
606 	rp->siop_sien = 0x00;	/* don't enable interrupts yet */
607 	rp->siop_dien = 0x00;	/* don't enable interrupts yet */
608 	rp->siop_scid = sc->sc_channel.chan_id |
609 	    SIOP_SCID_RRE | SIOP_SCID_SRE;
610 	rp->siop_respid = 1 << sc->sc_channel.chan_id;
611 	rp->siop_dwt = 0x00;
612 	rp->siop_stime0 = 0x0c;		/* XXXXX check */
613 
614 	/* will need to re-negotiate sync xfers */
615 	bzero(&sc->sc_sync, sizeof (sc->sc_sync));
616 
617 	i = rp->siop_istat;
618 	if (i & SIOP_ISTAT_SIP)
619 		dummy = rp->siop_sist;
620 	if (i & SIOP_ISTAT_DIP)
621 		dummy = rp->siop_dstat;
622 
623 	splx (s);
624 
625 	delay (siopng_reset_delay * 1000);
626 
627 	/*
628 	 * is lower half unterminated?
629 	 */
630 	if ((rp->siop_sbdl & 0x00ff) == 0x00ff) {
631 		printf(" no SCSI termination, host adapter deactivated.\n");
632 		sc->sc_channel.chan_ntargets = 0;	/* XXX */
633 		sc->sc_flags &= ~(SIOP_ALIVE|SIOP_INTDEFER|SIOP_INTSOFF);
634 		/* disable SCSI and DMA interrupts */
635 		sc->sc_sien = 0;
636 		sc->sc_dien = 0;
637 		rp->siop_sien = sc->sc_sien;
638 		rp->siop_dien = sc->sc_dien;
639 
640 		return;
641 	}
642 
643 	/*
644 	 * Check if upper half of SCSI bus is unterminated, and disallow
645 	 * disconnections if it appears to be unterminated.
646 	 */
647 	if ((rp->siop_sbdl & 0xff00) == 0xff00) {
648 		printf(" NO WIDE TERM");
649 		/* XXX need to restrict maximum target ID as well? */
650 		sc->sc_channel.chan_ntargets = 8;
651 		for (i = 0; i < 16; ++i) {
652 			siopng_allow_disc[i] = 0;
653 			siopng_inhibit_wide[i] |= 0x80;
654 		}
655 	}
656 
657 	printf("siopng type %s id %d reset V%d\n",
658 	    siopng_chips[rp->siop_macntl>>4],
659 	    sc->sc_channel.chan_ntargets,
660 	    rp->siop_ctest3 >> 4);
661 
662 	if ((sc->sc_flags & SIOP_ALIVE) == 0) {
663 		TAILQ_INIT(&sc->ready_list);
664 		TAILQ_INIT(&sc->nexus_list);
665 		TAILQ_INIT(&sc->free_list);
666 		sc->sc_nexus = NULL;
667 		acb = sc->sc_acb;
668 		bzero(acb, sizeof(struct siop_acb) * SIOP_NACB);
669 		for (i = 0; i < SIOP_NACB; i++) {
670 			TAILQ_INSERT_TAIL(&sc->free_list, acb, chain);
671 			acb++;
672 		}
673 		bzero(sc->sc_tinfo, sizeof(sc->sc_tinfo));
674 	} else {
675 		if (sc->sc_nexus != NULL) {
676 			sc->sc_nexus->xs->error = XS_RESET;
677 			siopng_scsidone(sc->sc_nexus, sc->sc_nexus->stat[0]);
678 		}
679 		while ((acb = sc->nexus_list.tqh_first) > 0) {
680 			acb->xs->error = XS_RESET;
681 			siopng_scsidone(acb, acb->stat[0]);
682 		}
683 	}
684 
685 	sc->sc_flags |= SIOP_ALIVE;
686 	sc->sc_flags &= ~(SIOP_INTDEFER|SIOP_INTSOFF);
687 	/* enable SCSI and DMA interrupts */
688 	sc->sc_sien = SIOP_SIEN_MA | SIOP_SIEN_STO | /*SIOP_SIEN_GEN |*/
689 	    /*SIOP_SIEN_SEL |*/ SIOP_SIEN_SGE | SIOP_SIEN_UDC |
690 	     SIOP_SIEN_RST | SIOP_SIEN_PAR;
691 	sc->sc_dien = SIOP_DIEN_BF | SIOP_DIEN_ABRT | SIOP_DIEN_SIR |
692 	    /*SIOP_DIEN_WTD |*/ SIOP_DIEN_IID;
693 	rp->siop_sien = sc->sc_sien;
694 	rp->siop_dien = sc->sc_dien;
695 	/*siopng_dump_registers(sc);*/
696 }
697 
698 /*
699  * Setup Data Storage for 53C720/770 and start SCRIPTS processing
700  */
701 
702 void
703 siopng_start(struct siop_softc *sc, int target, int lun, u_char *cbuf,
704              int clen, u_char *buf, int len)
705 {
706 	siop_regmap_p rp = sc->sc_siopp;
707 	int nchain;
708 	int count, tcount;
709 	char *addr, *dmaend;
710 	struct siop_acb *acb = sc->sc_nexus;
711 #ifdef DEBUG
712 	int i;
713 #endif
714 
715 #ifdef DEBUG
716 	if (siopng_debug & 0x100 && rp->siop_sbcl & SIOP_BSY) {
717 		printf ("ACK! siopng was busy: rp %p script %p dsa %p active %ld\n",
718 		    rp, &siopng_scripts, &acb->ds, sc->sc_active);
719 		printf ("istat %02x sfbr %02x respid %02x sien %04x dien %02x\n",
720 		    rp->siop_istat, rp->siop_sfbr, rp->siop_respid,
721 		    rp->siop_sien, rp->siop_dien);
722 #ifdef DDB
723 		/*Debugger();*/
724 #endif
725 	}
726 #endif
727 	acb->msgout[0] = MSG_IDENTIFY | lun;
728 	if (siopng_allow_disc[target] & 2 ||
729 	    (siopng_allow_disc[target] && len == 0))
730 		acb->msgout[0] = MSG_IDENTIFY_DR | lun;
731 	acb->status = 0;
732 	acb->stat[0] = -1;
733 	acb->msg[0] = -1;
734 	acb->ds.scsi_addr = (target << 16) | (sc->sc_sync[target].sxfer << 8) |
735 	    (sc->sc_sync[target].scntl3 << 24);
736 	acb->ds.idlen = 1;
737 	acb->ds.idbuf = (char *) kvtop(&acb->msgout[0]);
738 	acb->ds.cmdlen = clen;
739 	acb->ds.cmdbuf = (char *) kvtop(cbuf);
740 	acb->ds.stslen = 1;
741 	acb->ds.stsbuf = (char *) kvtop(&acb->stat[0]);
742 	acb->ds.msglen = 1;
743 	acb->ds.msgbuf = (char *) kvtop(&acb->msg[0]);
744 	acb->msg[1] = -1;
745 	acb->ds.msginlen = 1;
746 	acb->ds.extmsglen = 1;
747 	acb->ds.synmsglen = 3;
748 	acb->ds.msginbuf = acb->ds.msgbuf + 1;
749 	acb->ds.extmsgbuf = acb->ds.msginbuf + 1;
750 	acb->ds.synmsgbuf = acb->ds.extmsgbuf + 1;
751 	bzero(&acb->ds.chain, sizeof (acb->ds.chain));
752 
753 	if (sc->sc_sync[target].state == NEG_WIDE) {
754 		if (siopng_inhibit_wide[target]) {
755 			sc->sc_sync[target].state = NEG_SYNC;
756 			sc->sc_sync[target].scntl3 &= ~SIOP_SCNTL3_EWS;
757 #ifdef DEBUG
758 			if (siopngsync_debug)
759 				printf ("Forcing target %d narrow\n", target);
760 #endif
761 		}
762 		else {
763 			sc->sc_sync[target].scntl3 = 0x15 |	/* XXX */
764 			    (sc->sc_sync[target].scntl3 & 0x88);	/* XXX */
765 			acb->msg[2] = -1;
766 			acb->msgout[1] = MSG_EXT_MESSAGE;
767 			acb->msgout[2] = 2;
768 			acb->msgout[3] = MSG_WIDE_REQ;
769 			acb->msgout[4] = 1;
770 			acb->ds.idlen = 5;
771 			acb->ds.synmsglen = 2;
772 			sc->sc_sync[target].state = NEG_WAITW;
773 #ifdef DEBUG
774 			if (siopngsync_debug)
775 				printf("Sending wide request to target %d\n", target);
776 #endif
777 		}
778 	}
779 	if (sc->sc_sync[target].state == NEG_SYNC) {
780 		if (siopng_inhibit_sync[target]) {
781 			sc->sc_sync[target].state = NEG_DONE;
782 			sc->sc_sync[target].scntl3 = 5 |		/* XXX */
783 			    (sc->sc_sync[target].scntl3 & 0x88);	/* XXX */
784 			sc->sc_sync[target].sxfer = 0;
785 #ifdef DEBUG
786 			if (siopngsync_debug)
787 				printf ("Forcing target %d asynchronous\n", target);
788 #endif
789 		}
790 		else {
791 			sc->sc_sync[target].scntl3 = 0x15 |	/* XXX */
792 			    (sc->sc_sync[target].scntl3 & 0x88);	/* XXX */
793 			acb->msg[2] = -1;
794 			acb->msgout[1] = MSG_EXT_MESSAGE;
795 			acb->msgout[2] = 3;
796 			acb->msgout[3] = MSG_SYNC_REQ;
797 #ifdef MAXTOR_SYNC_KLUDGE
798 			acb->msgout[4] = 50 / 4;	/* ask for ridiculous period */
799 #else
800 			acb->msgout[4] = sc->sc_minsync;
801 #endif
802 			acb->msgout[5] = SIOP_MAX_OFFSET;
803 			acb->ds.idlen = 6;
804 			sc->sc_sync[target].state = NEG_WAITS;
805 #ifdef DEBUG
806 			if (siopngsync_debug)
807 				printf ("Sending sync request to target %d\n", target);
808 #endif
809 		}
810 	}
811 
812 /*
813  * Build physical DMA addresses for scatter/gather I/O
814  */
815 	acb->iob_buf = buf;
816 	acb->iob_len = len;
817 	acb->iob_curbuf = acb->iob_curlen = 0;
818 	nchain = 0;
819 	count = len;
820 	addr = buf;
821 	dmaend = NULL;
822 	while (count > 0) {
823 		acb->ds.chain[nchain].databuf = (char *) kvtop (addr);
824 		if (count < (tcount = NBPG - ((int) addr & PGOFSET)))
825 			tcount = count;
826 
827 #if DEBUG_ONLY_IF_DESPERATE
828 		printf("chain[%d]: count %d tcount %d vaddr %p paddr %p\n",
829 			nchain, count, tcount, addr,
830 			acb->ds.chain[nchain].databuf);
831 #endif
832 		acb->ds.chain[nchain].datalen = tcount;
833 		addr += tcount;
834 		count -= tcount;
835 		if (acb->ds.chain[nchain].databuf == dmaend) {
836 			dmaend += acb->ds.chain[nchain].datalen;
837 			acb->ds.chain[nchain].datalen = 0;
838 			acb->ds.chain[--nchain].datalen += tcount;
839 #ifdef DEBUG
840 			++siopngdma_hits;
841 #endif
842 		}
843 		else {
844 			dmaend = acb->ds.chain[nchain].databuf +
845 			    acb->ds.chain[nchain].datalen;
846 			acb->ds.chain[nchain].datalen = tcount;
847 #ifdef DEBUG
848 			if (nchain)	/* Don't count miss on first one */
849 				++siopngdma_misses;
850 #endif
851 		}
852 		++nchain;
853 	}
854 #ifdef DEBUG
855 	if (nchain != 1 && len != 0 && siopng_debug & 3) {
856 		printf ("DMA chaining set: %d\n", nchain);
857 		for (i = 0; i < nchain; ++i) {
858 			printf ("  [%d] %8p %lx\n", i, acb->ds.chain[i].databuf,
859 			    acb->ds.chain[i].datalen);
860 		}
861 	}
862 #endif
863 
864 	/* push data cache for all data the 53c720/770 needs to access */
865 	dma_cachectl ((caddr_t)acb, sizeof (struct siop_acb));
866 	dma_cachectl (cbuf, clen);
867 	if (buf != NULL && len != 0)
868 		dma_cachectl (buf, len);
869 #ifdef DEBUG
870 	if (siopng_debug & 0x100 && rp->siop_sbcl & SIOP_BSY) {
871 		printf ("ACK! siopng was busy at start: rp %p script %p dsa %p active %ld\n",
872 		    rp, &siopng_scripts, &acb->ds, sc->sc_active);
873 #ifdef DDB
874 		/*Debugger();*/
875 #endif
876 	}
877 #endif
878 	if (sc->nexus_list.tqh_first == NULL) {
879 		if (rp->siop_istat & SIOP_ISTAT_CON)
880 			printf("%s: siopng_select while connected?\n",
881 			    sc->sc_dev.dv_xname);
882 		rp->siop_temp = 0;
883 #ifndef FIXME
884 		rp->siop_scntl3 = sc->sc_sync[target].scntl3;
885 #endif
886 		rp->siop_dsa = kvtop((caddr_t)&acb->ds);
887 		rp->siop_dsp = sc->sc_scriptspa;
888 		SIOP_TRACE('s',1,0,0)
889 	} else {
890 		if ((rp->siop_istat & SIOP_ISTAT_CON) == 0) {
891 			rp->siop_istat = SIOP_ISTAT_SIGP;
892 			SIOP_TRACE('s',2,0,0);
893 		}
894 		else {
895 			SIOP_TRACE('s',3,rp->siop_istat,0);
896 		}
897 	}
898 #ifdef DEBUG
899 	++siopngstarts;
900 #endif
901 }
902 
903 /*
904  * Process a DMA or SCSI interrupt from the 53C720/770 SIOP
905  */
906 
907 int
908 siopng_checkintr(struct siop_softc *sc, u_char istat, u_char dstat,
909                  u_short sist, int *status)
910 {
911 	siop_regmap_p rp = sc->sc_siopp;
912 	struct siop_acb *acb = sc->sc_nexus;
913 	int	target = 0;
914 	int	dfifo, dbc, sstat0, sstat1, sstat2;
915 
916 	dfifo = rp->siop_dfifo;
917 	dbc = rp->siop_dbc0;
918 	sstat0 = rp->siop_sstat0;
919 	sstat1 = rp->siop_sstat1;
920 	sstat2 = rp->siop_sstat2;
921 	rp->siop_ctest3 |= SIOP_CTEST8_CLF;
922 	while ((rp->siop_ctest1 & SIOP_CTEST1_FMT) != SIOP_CTEST1_FMT)
923 		;
924 	rp->siop_ctest3 &= ~SIOP_CTEST8_CLF;
925 #ifdef DEBUG
926 	++siopngints;
927 #if 0
928 	if (siopng_debug & 0x100) {
929 		DCIAS(&acb->stat[0]);	/* XXX */
930 		printf ("siopngchkintr: istat %x dstat %x sist %x dsps %x sbcl %x sts %x msg %x\n",
931 		    istat, dstat, sist, rp->siop_dsps, rp->siop_sbcl, acb->stat[0], acb->msg[0]);
932 		printf ("sync msg in: %02x %02x %02x %02x %02x %02x\n",
933 		    acb->msg[0], acb->msg[1], acb->msg[2],
934 		    acb->msg[3], acb->msg[4], acb->msg[5]);
935 	}
936 #endif
937 	if (rp->siop_dsp && (rp->siop_dsp < sc->sc_scriptspa ||
938 	    rp->siop_dsp >= sc->sc_scriptspa + sizeof(siopng_scripts))) {
939 		printf ("%s: dsp not within script dsp %lx scripts %lx:%lx",
940 		    sc->sc_dev.dv_xname, rp->siop_dsp, sc->sc_scriptspa,
941 		    sc->sc_scriptspa + sizeof(siopng_scripts));
942 		printf(" istat %x dstat %x sist %x\n",
943 		    istat, dstat, sist);
944 #ifdef DDB
945 		Debugger();
946 #endif
947 	}
948 #endif
949 	SIOP_TRACE('i',dstat,istat,(istat&SIOP_ISTAT_DIP)?rp->siop_dsps&0xff:sist);
950 	if (dstat & SIOP_DSTAT_SIR && rp->siop_dsps == 0xff00) {
951 		/* Normal completion status, or check condition */
952 #ifdef DEBUG
953 		if (rp->siop_dsa != kvtop((caddr_t)&acb->ds)) {
954 			printf ("siopng: invalid dsa: %lx %x\n", rp->siop_dsa,
955 			    kvtop((caddr_t)&acb->ds));
956 			panic("*** siopng DSA invalid ***");
957 		}
958 #endif
959 		target = acb->xs->xs_periph->periph_target;
960 		if (sc->sc_sync[target].state == NEG_WAITW) {
961 			if (acb->msg[1] == 0xff)
962 				printf ("%s: target %d ignored wide request\n",
963 				    sc->sc_dev.dv_xname, target);
964 			else if (acb->msg[1] == MSG_REJECT)
965 				printf ("%s: target %d rejected wide request\n",
966 				    sc->sc_dev.dv_xname, target);
967 			else {
968 				printf("%s: target %d (wide) %02x %02x %02x %02x\n",
969 				    sc->sc_dev.dv_xname, target, acb->msg[1],
970 				    acb->msg[2], acb->msg[3], acb->msg[4]);
971 				if (acb->msg[1] == MSG_EXT_MESSAGE &&
972 				    acb->msg[2] == 2 &&
973 				    acb->msg[3] == MSG_WIDE_REQ)
974 					sc->sc_sync[target].scntl3 = acb->msg[4] ?
975 					    sc->sc_sync[target].scntl3 | SIOP_SCNTL3_EWS :
976 					    sc->sc_sync[target].scntl3 & ~SIOP_SCNTL3_EWS;
977 			}
978 			sc->sc_sync[target].state = NEG_SYNC;
979 		}
980 		if (sc->sc_sync[target].state == NEG_WAITS) {
981 			if (acb->msg[1] == 0xff)
982 				printf ("%s: target %d ignored sync request\n",
983 				    sc->sc_dev.dv_xname, target);
984 			else if (acb->msg[1] == MSG_REJECT)
985 				printf ("%s: target %d rejected sync request\n",
986 				    sc->sc_dev.dv_xname, target);
987 			else
988 /* XXX - need to set sync transfer parameters */
989 				printf("%s: target %d (sync) %02x %02x %02x\n",
990 				    sc->sc_dev.dv_xname, target, acb->msg[1],
991 				    acb->msg[2], acb->msg[3]);
992 			sc->sc_sync[target].state = NEG_DONE;
993 		}
994 		dma_cachectl(&acb->stat[0], 1);
995 		*status = acb->stat[0];
996 #ifdef DEBUG
997 		if (rp->siop_sbcl & SIOP_BSY) {
998 			/*printf ("ACK! siop was busy at end: rp %x script %x dsa %x\n",
999 			    rp, &siopng_scripts, &acb->ds);*/
1000 #ifdef DDB
1001 			/*Debugger();*/
1002 #endif
1003 		}
1004 		if (acb->msg[0] != 0x00)
1005 			printf("%s: message was not COMMAND COMPLETE: %x\n",
1006 			    sc->sc_dev.dv_xname, acb->msg[0]);
1007 #endif
1008 		if (sc->nexus_list.tqh_first)
1009 			rp->siop_dcntl |= SIOP_DCNTL_STD;
1010 		return 1;
1011 	}
1012 	if (dstat & SIOP_DSTAT_SIR && rp->siop_dsps == 0xff0b) {
1013 		target = acb->xs->xs_periph->periph_target;
1014 		if (acb->msg[1] == MSG_EXT_MESSAGE && acb->msg[2] == 2 &&
1015 		    acb->msg[3] == MSG_WIDE_REQ) {
1016 #ifdef DEBUG
1017 			if (siopngsync_debug)
1018 				printf ("wide msg in: %02x %02x %02x %02x %02x %02x\n",
1019 				    acb->msg[0], acb->msg[1], acb->msg[2],
1020 				    acb->msg[3], acb->msg[4], acb->msg[5]);
1021 #endif
1022 			sc->sc_sync[target].scntl3 &= ~(SIOP_SCNTL3_EWS);
1023 			if (acb->msg[2] == 2 &&
1024 			    acb->msg[3] == MSG_WIDE_REQ &&
1025 			    acb->msg[4] != 0) {
1026 				sc->sc_sync[target].scntl3 |= SIOP_SCNTL3_EWS;
1027 				printf ("%s: target %d now wide %d\n",
1028 				    sc->sc_dev.dv_xname, target,
1029 				    acb->msg[4]);
1030 			}
1031 			rp->siop_scntl3 = sc->sc_sync[target].scntl3;
1032 			if (sc->sc_sync[target].state == NEG_WAITW) {
1033 				sc->sc_sync[target].state = NEG_SYNC;
1034 				rp->siop_dsp = sc->sc_scriptspa + Ent_clear_ack;
1035 				return(0);
1036 			}
1037 			rp->siop_dcntl |= SIOP_DCNTL_STD;
1038 			sc->sc_sync[target].state = NEG_SYNC;
1039 			return (0);
1040 		}
1041 		if (acb->msg[1] == MSG_EXT_MESSAGE && acb->msg[2] == 3 &&
1042 		    acb->msg[3] == MSG_SYNC_REQ) {
1043 #ifdef DEBUG
1044 			if (siopngsync_debug)
1045 				printf ("sync msg in: %02x %02x %02x %02x %02x %02x\n",
1046 				    acb->msg[0], acb->msg[1], acb->msg[2],
1047 				    acb->msg[3], acb->msg[4], acb->msg[5]);
1048 #endif
1049 			sc->sc_sync[target].sxfer = 0;
1050 			sc->sc_sync[target].scntl3 = 5 |		/* XXX */
1051 			    (sc->sc_sync[target].scntl3 & 0x88);	/* XXX */
1052 			if (acb->msg[2] == 3 &&
1053 			    acb->msg[3] == MSG_SYNC_REQ &&
1054 			    acb->msg[5] != 0) {
1055 #ifdef MAXTOR_KLUDGE
1056 				/*
1057 				 * Kludge for my Maxtor XT8580S
1058 				 * It accepts whatever we request, even
1059 				 * though it won't work.  So we ask for
1060 				 * a short period than we can handle.  If
1061 				 * the device says it can do it, use 208ns.
1062 				 * If the device says it can do less than
1063 				 * 100ns, then we limit it to 100ns.
1064 				 */
1065 				if (acb->msg[4] && acb->msg[4] < 100 / 4) {
1066 #ifdef DEBUG
1067 					printf ("%d: target %d wanted %dns period\n",
1068 					    sc->sc_dev.dv_xname, target,
1069 					    acb->msg[4] * 4);
1070 #endif
1071 					if (acb->msg[4] == 50 / 4)
1072 						acb->msg[4] = 208 / 4;
1073 					else
1074 						acb->msg[4] = 100 / 4;
1075 				}
1076 #endif /* MAXTOR_KLUDGE */
1077 				printf ("%s: target %d now synchronous, period=%dns, offset=%d\n",
1078 				    sc->sc_dev.dv_xname, target,
1079 				    (acb->msg[4] == 12) ? 50 : acb->msg[4] * 4,
1080 				    acb->msg[5]);
1081 				scsi_period_to_siopng (sc, target);
1082 			}
1083 			rp->siop_sxfer = sc->sc_sync[target].sxfer;
1084 			rp->siop_scntl3 = sc->sc_sync[target].scntl3;
1085 			if (sc->sc_sync[target].state == NEG_WAITS) {
1086 				sc->sc_sync[target].state = NEG_DONE;
1087 				rp->siop_dsp = sc->sc_scriptspa + Ent_clear_ack;
1088 				return(0);
1089 			}
1090 			rp->siop_dcntl |= SIOP_DCNTL_STD;
1091 			sc->sc_sync[target].state = NEG_DONE;
1092 			return (0);
1093 		}
1094 		/* XXX - not SDTR message */
1095 	}
1096 	if (sist & SIOP_SIST_MA) {		/* Phase mismatch */
1097 #ifdef DEBUG
1098 		++siopngphmm;
1099 		if (acb == NULL)
1100 			printf("%s: Phase mismatch with no active command?\n",
1101 			    sc->sc_dev.dv_xname);
1102 #endif
1103 		if (acb->iob_len) {
1104 			int adjust;
1105 			adjust = ((dfifo - (dbc & 0x7f)) & 0x7f);
1106 			if (sstat0 & SIOP_SSTAT0_OLF)	/* sstat0 SODL lsb */
1107 				++adjust;
1108 			if (sstat0 & SIOP_SSTAT0_ORF)	/* sstat0 SODR lsb */
1109 				++adjust;
1110 			if (sstat2 & SIOP_SSTAT2_OLF1)	/* sstat2 SODL msb */
1111 				++adjust;
1112 			if (sstat2 & SIOP_SSTAT2_ORF1)	/* sstat2 SODR msb */
1113 				++adjust;
1114 			acb->iob_curlen = *((long *)&rp->siop_dcmd) & 0xffffff;
1115 			acb->iob_curlen += adjust;
1116 			acb->iob_curbuf = *((long *)&rp->siop_dnad) - adjust;
1117 #ifdef DEBUG
1118 			if (siopng_debug & 0x100) {
1119 				int i;
1120 				printf ("Phase mismatch: curbuf %lx curlen %lx dfifo %x dbc %x sstat1 %x adjust %x sbcl %x starts %d acb %p\n",
1121 				    acb->iob_curbuf, acb->iob_curlen, dfifo,
1122 				    dbc, sstat1, adjust, rp->siop_sbcl, siopngstarts, acb);
1123 				if (acb->ds.chain[1].datalen) {
1124 					for (i = 0; acb->ds.chain[i].datalen; ++i)
1125 						printf("chain[%d] addr %p len %lx\n",
1126 						    i, acb->ds.chain[i].databuf,
1127 						    acb->ds.chain[i].datalen);
1128 				}
1129 			}
1130 #endif
1131 			dma_cachectl ((caddr_t)acb, sizeof(*acb));
1132 		}
1133 #ifdef DEBUG
1134 		SIOP_TRACE('m',rp->siop_sbcl,(rp->siop_dsp>>8),rp->siop_dsp);
1135 		if (siopng_debug & 9)
1136 			printf ("Phase mismatch: %x dsp +%lx dcmd %lx\n",
1137 			    rp->siop_sbcl,
1138 			    rp->siop_dsp - sc->sc_scriptspa,
1139 			    *((long *)&rp->siop_dcmd));
1140 #endif
1141 		if ((rp->siop_sbcl & SIOP_REQ) == 0) {
1142 			printf ("Phase mismatch: REQ not asserted! %02x dsp %lx\n",
1143 			    rp->siop_sbcl, rp->siop_dsp);
1144 #if defined(DEBUG) && defined(DDB)
1145 			Debugger();
1146 #endif
1147 		}
1148 		switch (rp->siop_sbcl & 7) {
1149 		case 0:		/* data out */
1150 		case 1:		/* data in */
1151 		case 2:		/* status */
1152 		case 3:		/* command */
1153 		case 6:		/* message in */
1154 		case 7:		/* message out */
1155 			rp->siop_dsp = sc->sc_scriptspa + Ent_switch;
1156 			break;
1157 		default:
1158 			goto bad_phase;
1159 		}
1160 		return 0;
1161 	}
1162 	if (sist & SIOP_SIST_STO) {		/* Select timed out */
1163 #ifdef DEBUG
1164 		if (acb == NULL)
1165 			printf("%s: Select timeout with no active command?\n",
1166 			    sc->sc_dev.dv_xname);
1167 		if (rp->siop_sbcl & SIOP_BSY) {
1168 			printf ("ACK! siop was busy at timeout: rp %p script %p dsa %p\n",
1169 			    rp, &siopng_scripts, &acb->ds);
1170 			printf(" sbcl %x sdid %x istat %x dstat %x sist %x\n",
1171 			    rp->siop_sbcl, rp->siop_sdid, istat, dstat, sist);
1172 			if (!(rp->siop_sbcl & SIOP_BSY)) {
1173 				printf ("Yikes, it's not busy now!\n");
1174 #if 0
1175 				*status = -1;
1176 				if (sc->nexus_list.tqh_first)
1177 					rp->siop_dsp = sc->sc_scriptspa + Ent_wait_reselect;
1178 				return 1;
1179 #endif
1180 			}
1181 /*			rp->siop_dcntl |= SIOP_DCNTL_STD;*/
1182 			return (0);
1183 #ifdef DDB
1184 			Debugger();
1185 #endif
1186 		}
1187 #endif
1188 		*status = -1;
1189 		acb->xs->error = XS_SELTIMEOUT;
1190 		if (sc->nexus_list.tqh_first)
1191 			rp->siop_dsp = sc->sc_scriptspa + Ent_wait_reselect;
1192 		return 1;
1193 	}
1194 	if (acb)
1195 		target = acb->xs->xs_periph->periph_target;
1196 	else
1197 		target = 7;
1198 	if (sist & SIOP_SIST_UDC) {
1199 #ifdef DEBUG
1200 		if (acb == NULL)
1201 			printf("%s: Unexpected disconnect with no active command?\n",
1202 			    sc->sc_dev.dv_xname);
1203 		printf ("%s: target %d disconnected unexpectedly\n",
1204 		   sc->sc_dev.dv_xname, target);
1205 siopng_dump_registers(sc);
1206 siopng_dump(sc);
1207 #endif
1208 #if 0
1209 		siopngabort (sc, rp, "siopngchkintr");
1210 #endif
1211 		*status = STS_BUSY;
1212 		if (sc->nexus_list.tqh_first)
1213 			rp->siop_dsp = sc->sc_scriptspa + Ent_wait_reselect;
1214 		return (acb != NULL);
1215 	}
1216 	if (dstat & SIOP_DSTAT_SIR && (rp->siop_dsps == 0xff01 ||
1217 	    rp->siop_dsps == 0xff02)) {
1218 #ifdef DEBUG
1219 		if (siopng_debug & 0x100)
1220 			printf ("%s: ID %02x disconnected TEMP %lx (+%lx) curbuf %lx curlen %lx buf %p len %lx dfifo %x dbc %x sstat1 %x starts %d acb %p\n",
1221 			    sc->sc_dev.dv_xname, 1 << target, rp->siop_temp,
1222 			    rp->siop_temp ? rp->siop_temp - sc->sc_scriptspa : 0,
1223 			    acb->iob_curbuf, acb->iob_curlen,
1224 			    acb->ds.chain[0].databuf, acb->ds.chain[0].datalen, dfifo, dbc, sstat1, siopngstarts, acb);
1225 #endif
1226 		if (acb == NULL) {
1227 			printf("%s: Disconnect with no active command?\n",
1228 			    sc->sc_dev.dv_xname);
1229 			return (0);
1230 		}
1231 		/*
1232 		 * XXXX need to update iob_curbuf/iob_curlen to reflect
1233 		 * current data transferred.  If device disconnected in
1234 		 * the middle of a DMA block, they should already be set
1235 		 * by the phase change interrupt.  If the disconnect
1236 		 * occurs on a DMA block boundary, we have to figure out
1237 		 * which DMA block it was.
1238 		 */
1239 		if (acb->iob_len && rp->siop_temp) {
1240 			int n = rp->siop_temp - sc->sc_scriptspa;
1241 
1242 			if (acb->iob_curlen && acb->iob_curlen != acb->ds.chain[0].datalen)
1243 				printf("%s: iob_curbuf/len already set? n %x iob %lx/%lx chain[0] %p/%lx\n",
1244 				    sc->sc_dev.dv_xname, n, acb->iob_curbuf, acb->iob_curlen,
1245 				    acb->ds.chain[0].databuf, acb->ds.chain[0].datalen);
1246 			if (n < Ent_datain)
1247 				n = (n - Ent_dataout) / 16;
1248 			else
1249 				n = (n - Ent_datain) / 16;
1250 			if (n <= 0 && n > DMAMAXIO)
1251 				printf("TEMP invalid %d\n", n);
1252 			else {
1253 				acb->iob_curbuf = (u_long)acb->ds.chain[n].databuf;
1254 				acb->iob_curlen = acb->ds.chain[n].datalen;
1255 			}
1256 #ifdef DEBUG
1257 			if (siopng_debug & 0x100) {
1258 				printf("%s: TEMP offset %d", sc->sc_dev.dv_xname, n);
1259 				printf(" curbuf %lx curlen %lx\n", acb->iob_curbuf,
1260 				    acb->iob_curlen);
1261 			}
1262 #endif
1263 		}
1264 		/*
1265 		 * If data transfer was interrupted by disconnect, iob_curbuf
1266 		 * and iob_curlen should reflect the point of interruption.
1267 		 * Adjust the DMA chain so that the data transfer begins
1268 		 * at the appropriate place upon reselection.
1269 		 * XXX This should only be done on save data pointer message?
1270 		 */
1271 		if (acb->iob_curlen) {
1272 			int i, j;
1273 
1274 #ifdef DEBUG
1275 			if (siopng_debug & 0x100)
1276 				printf ("%s: adjusting DMA chain\n",
1277 				    sc->sc_dev.dv_xname);
1278 			if (rp->siop_dsps == 0xff02)
1279 				printf ("%s: ID %02x disconnected without Save Data Pointers\n",
1280 				    sc->sc_dev.dv_xname, 1 << target);
1281 #endif
1282 			for (i = 0; i < DMAMAXIO; ++i) {
1283 				if (acb->ds.chain[i].datalen == 0)
1284 					break;
1285 				if (acb->iob_curbuf >= (long)acb->ds.chain[i].databuf &&
1286 				    acb->iob_curbuf < (long)(acb->ds.chain[i].databuf +
1287 				    acb->ds.chain[i].datalen))
1288 					break;
1289 			}
1290 			if (i >= DMAMAXIO || acb->ds.chain[i].datalen == 0) {
1291 				printf("couldn't find saved data pointer: ");
1292 				printf("curbuf %lx curlen %lx i %d\n",
1293 				    acb->iob_curbuf, acb->iob_curlen, i);
1294 #ifdef DDB
1295 				Debugger();
1296 #endif
1297 			}
1298 #ifdef DEBUG
1299 			if (siopng_debug & 0x100)
1300 				printf("  chain[0]: %p/%lx -> %lx/%lx\n",
1301 				    acb->ds.chain[0].databuf,
1302 				    acb->ds.chain[0].datalen,
1303 				    acb->iob_curbuf,
1304 				    acb->iob_curlen);
1305 #endif
1306 			acb->ds.chain[0].databuf = (char *)acb->iob_curbuf;
1307 			acb->ds.chain[0].datalen = acb->iob_curlen;
1308 			for (j = 1, ++i; i < DMAMAXIO && acb->ds.chain[i].datalen; ++i, ++j) {
1309 #ifdef DEBUG
1310 			if (siopng_debug & 0x100)
1311 				printf("  chain[%d]: %p/%lx -> %p/%lx\n", j,
1312 				    acb->ds.chain[j].databuf,
1313 				    acb->ds.chain[j].datalen,
1314 				    acb->ds.chain[i].databuf,
1315 				    acb->ds.chain[i].datalen);
1316 #endif
1317 				acb->ds.chain[j].databuf = acb->ds.chain[i].databuf;
1318 				acb->ds.chain[j].datalen = acb->ds.chain[i].datalen;
1319 			}
1320 			if (j < DMAMAXIO)
1321 				acb->ds.chain[j].datalen = 0;
1322 			DCIAS(kvtop((caddr_t)&acb->ds.chain));
1323 		}
1324 		++sc->sc_tinfo[target].dconns;
1325 		/*
1326 		 * add nexus to waiting list
1327 		 * clear nexus
1328 		 * try to start another command for another target/lun
1329 		 */
1330 		acb->status = sc->sc_flags & SIOP_INTSOFF;
1331 		TAILQ_INSERT_HEAD(&sc->nexus_list, acb, chain);
1332 		sc->sc_nexus = NULL;		/* no current device */
1333 		/* start script to wait for reselect */
1334 		if (sc->sc_nexus == NULL)
1335 			rp->siop_dsp = sc->sc_scriptspa + Ent_wait_reselect;
1336 /* XXXX start another command ? */
1337 		if (sc->ready_list.tqh_first)
1338 			siopng_sched(sc);
1339 #if 0
1340 		else
1341 			rp->siop_dcntl |= SIOP_DCNTL_STD;
1342 #endif
1343 		return (0);
1344 	}
1345 	if (dstat & SIOP_DSTAT_SIR && rp->siop_dsps == 0xff03) {
1346 		int reselid = rp->siop_scratcha & 0x7f;
1347 		int reselun = rp->siop_sfbr & 0x07;
1348 
1349 		sc->sc_sstat1 = rp->siop_sbcl;	/* XXXX save current SBCL */
1350 #ifdef DEBUG
1351 		if (siopng_debug & 0x100)
1352 			printf ("%s: target ID %02x reselected dsps %lx\n",
1353 			     sc->sc_dev.dv_xname, reselid,
1354 			     rp->siop_dsps);
1355 		if ((rp->siop_sfbr & 0x80) == 0)
1356 			printf("%s: Reselect message in was not identify: %x\n",
1357 			    sc->sc_dev.dv_xname, rp->siop_sfbr);
1358 #endif
1359 		if (sc->sc_nexus) {
1360 #ifdef DEBUG
1361 			if (siopng_debug & 0x100)
1362 				printf ("%s: reselect ID %02x w/active\n",
1363 				    sc->sc_dev.dv_xname, reselid);
1364 #endif
1365 			TAILQ_INSERT_HEAD(&sc->ready_list, sc->sc_nexus, chain);
1366 			sc->sc_tinfo[sc->sc_nexus->xs->xs_periph->periph_target].lubusy
1367 			    &= ~(1 << sc->sc_nexus->xs->xs_periph->periph_lun);
1368 			--sc->sc_active;
1369 		}
1370 		/*
1371 		 * locate acb of reselecting device
1372 		 * set sc->sc_nexus to acb
1373 		 */
1374 		for (acb = sc->nexus_list.tqh_first; acb;
1375 		    acb = acb->chain.tqe_next) {
1376 			if (reselid != ((acb->ds.scsi_addr >> 16) & 0xff) ||
1377 			    reselun != (acb->msgout[0] & 0x07))
1378 				continue;
1379 			TAILQ_REMOVE(&sc->nexus_list, acb, chain);
1380 			sc->sc_nexus = acb;
1381 			sc->sc_flags |= acb->status;
1382 			acb->status = 0;
1383 			DCIAS(kvtop(&acb->stat[0]));
1384 			rp->siop_dsa = kvtop((caddr_t)&acb->ds);
1385 			rp->siop_sxfer =
1386 				sc->sc_sync[acb->xs->xs_periph->periph_target].sxfer;
1387 #ifndef FIXME
1388 			rp->siop_scntl3 =
1389 				sc->sc_sync[acb->xs->xs_periph->periph_target].scntl3;
1390 #endif
1391 			break;
1392 		}
1393 		if (acb == NULL) {
1394 			printf("%s: target ID %02x reselect nexus_list %p\n",
1395 			    sc->sc_dev.dv_xname, reselid,
1396 			    sc->nexus_list.tqh_first);
1397 			panic("unable to find reselecting device");
1398 		}
1399 		dma_cachectl ((caddr_t)acb, sizeof(*acb));
1400 		rp->siop_temp = 0;
1401 		rp->siop_dcntl |= SIOP_DCNTL_STD;
1402 		return (0);
1403 	}
1404 	if (dstat & SIOP_DSTAT_SIR && rp->siop_dsps == 0xff04) {
1405 #ifdef DEBUG
1406 		u_short ctest2 = rp->siop_ctest2;
1407 
1408 		/* reselect was interrupted (by Sig_P or select) */
1409 		if (siopng_debug & 0x100 ||
1410 		    (ctest2 & SIOP_CTEST2_SIGP) == 0)
1411 			printf ("%s: reselect interrupted (Sig_P?) scntl1 %x ctest2 %x sfbr %x istat %x/%x\n",
1412 			    sc->sc_dev.dv_xname, rp->siop_scntl1,
1413 			    ctest2, rp->siop_sfbr, istat, rp->siop_istat);
1414 #endif
1415 		/* XXX assumes it was not select */
1416 		if (sc->sc_nexus == NULL) {
1417 #ifdef DEBUG
1418 			printf("%s: reselect interrupted, sc_nexus == NULL\n",
1419 			    sc->sc_dev.dv_xname);
1420 #if 0
1421 			siopng_dump(sc);
1422 #ifdef DDB
1423 			Debugger();
1424 #endif
1425 #endif
1426 #endif
1427 			rp->siop_dcntl |= SIOP_DCNTL_STD;
1428 			return(0);
1429 		}
1430 		target = sc->sc_nexus->xs->xs_periph->periph_target;
1431 		rp->siop_temp = 0;
1432 		rp->siop_dsa = kvtop((caddr_t)&sc->sc_nexus->ds);
1433 		rp->siop_sxfer = sc->sc_sync[target].sxfer;
1434 #ifndef FIXME
1435 		rp->siop_scntl3 = sc->sc_sync[target].scntl3;
1436 #endif
1437 		rp->siop_dsp = sc->sc_scriptspa;
1438 		return (0);
1439 	}
1440 	if (dstat & SIOP_DSTAT_SIR && rp->siop_dsps == 0xff06) {
1441 		if (acb == NULL)
1442 			printf("%s: Bad message-in with no active command?\n",
1443 			    sc->sc_dev.dv_xname);
1444 		/* Unrecognized message in byte */
1445 		dma_cachectl (&acb->msg[1],1);
1446 		printf ("%s: Unrecognized message in data sfbr %x msg %x sbcl %x\n",
1447 			sc->sc_dev.dv_xname, rp->siop_sfbr, acb->msg[1], rp->siop_sbcl);
1448 		/* what should be done here? */
1449 		DCIAS(kvtop(&acb->msg[1]));
1450 		rp->siop_dsp = sc->sc_scriptspa + Ent_switch;
1451 		return (0);
1452 	}
1453 	if (dstat & SIOP_DSTAT_SIR && rp->siop_dsps == 0xff0a) {
1454 		/* Status phase wasn't followed by message in phase? */
1455 		printf ("%s: Status phase not followed by message in phase? sbcl %x sbdl %x\n",
1456 			sc->sc_dev.dv_xname, rp->siop_sbcl, rp->siop_sbdl);
1457 		if (rp->siop_sbcl == 0xa7) {
1458 			/* It is now, just continue the script? */
1459 			rp->siop_dcntl |= SIOP_DCNTL_STD;
1460 			return (0);
1461 		}
1462 	}
1463 	if (sist == 0 && dstat & SIOP_DSTAT_SIR) {
1464 		dma_cachectl (&acb->stat[0], 1);
1465 		dma_cachectl (&acb->msg[0], 1);
1466 		printf ("SIOP interrupt: %lx sts %x msg %x %x sbcl %x\n",
1467 		    rp->siop_dsps, acb->stat[0], acb->msg[0], acb->msg[1],
1468 		    rp->siop_sbcl);
1469 		siopngreset (sc);
1470 		*status = -1;
1471 		return 0;	/* siopngreset has cleaned up */
1472 	}
1473 	if (sist & SIOP_SIST_SGE)
1474 		printf ("SIOP: SCSI Gross Error\n");
1475 	if (sist & SIOP_SIST_PAR)
1476 		printf ("SIOP: Parity Error\n");
1477 	if (dstat & SIOP_DSTAT_IID)
1478 		printf ("SIOP: Invalid instruction detected\n");
1479 bad_phase:
1480 	/*
1481 	 * temporary panic for unhandled conditions
1482 	 * displays various things about the 53C720/770 status and registers
1483 	 * then panics.
1484 	 * XXXX need to clean this up to print out the info, reset, and continue
1485 	 */
1486 	printf ("siopngchkintr: target %x ds %p\n", target, &acb->ds);
1487 	printf ("scripts %lx ds %x rp %x dsp %lx dcmd %lx\n", sc->sc_scriptspa,
1488 	    kvtop((caddr_t)&acb->ds), kvtop((caddr_t)rp), rp->siop_dsp,
1489 	    *((long *)&rp->siop_dcmd));
1490 	printf ("siopngchkintr: istat %x dstat %x sist %x dsps %lx dsa %lx sbcl %x sts %x msg %x %x sfbr %x\n",
1491 	    istat, dstat, sist, rp->siop_dsps, rp->siop_dsa,
1492 	     rp->siop_sbcl, acb->stat[0], acb->msg[0], acb->msg[1], rp->siop_sfbr);
1493 #ifdef DEBUG
1494 	if (siopng_debug & 0x20)
1495 		panic("siopngchkintr: **** temp ****");
1496 #endif
1497 #ifdef DDB
1498 	Debugger ();
1499 #endif
1500 	siopngreset (sc);		/* hard reset */
1501 	*status = -1;
1502 	return 0;		/* siopngreset cleaned up */
1503 }
1504 
1505 void
1506 siopng_select(struct siop_softc *sc)
1507 {
1508 	siop_regmap_p rp;
1509 	struct siop_acb *acb = sc->sc_nexus;
1510 
1511 #ifdef DEBUG
1512 	if (siopng_debug & 1)
1513 		printf ("%s: select ", sc->sc_dev.dv_xname);
1514 #endif
1515 
1516 	rp = sc->sc_siopp;
1517 	if (acb->xs->xs_control & XS_CTL_POLL || siopng_no_dma) {
1518 		sc->sc_flags |= SIOP_INTSOFF;
1519 		sc->sc_flags &= ~SIOP_INTDEFER;
1520 		if ((rp->siop_istat & 0x08) == 0) {
1521 			rp->siop_sien = 0;
1522 			rp->siop_dien = 0;
1523 		}
1524 #if 0
1525 	} else if ((sc->sc_flags & SIOP_INTDEFER) == 0) {
1526 		sc->sc_flags &= ~SIOP_INTSOFF;
1527 		if ((rp->siop_istat & 0x08) == 0) {
1528 			rp->siop_sien = sc->sc_sien;
1529 			rp->siop_dien = sc->sc_dien;
1530 		}
1531 #endif
1532 	}
1533 #ifdef DEBUG
1534 	if (siopng_debug & 1)
1535 		printf ("siopng_select: target %x cmd %02x ds %p\n",
1536 		    acb->xs->xs_periph->periph_target, acb->cmd.opcode,
1537 		    &sc->sc_nexus->ds);
1538 #endif
1539 
1540 	siopng_start(sc, acb->xs->xs_periph->periph_target,
1541 		acb->xs->xs_periph->periph_lun,
1542 	    (u_char *)&acb->cmd, acb->clen, acb->daddr, acb->dleft);
1543 
1544 	return;
1545 }
1546 
1547 /*
1548  * 53C720/770 interrupt handler
1549  */
1550 
1551 void
1552 siopngintr(register struct siop_softc *sc)
1553 {
1554 	siop_regmap_p rp;
1555 	u_char istat, dstat;
1556 	u_short sist;
1557 	int status;
1558 	int s = splbio();
1559 
1560 	istat = sc->sc_istat;
1561 	if ((istat & (SIOP_ISTAT_SIP | SIOP_ISTAT_DIP)) == 0) {
1562 		splx(s);
1563 		return;
1564 	}
1565 
1566 	/* Got a valid interrupt on this device */
1567 	rp = sc->sc_siopp;
1568 	dstat = sc->sc_dstat;
1569 	sist = sc->sc_sist;
1570 	if (dstat & SIOP_DSTAT_SIR)
1571 		sc->sc_intcode = rp->siop_dsps;
1572 	sc->sc_istat = 0;
1573 #ifdef DEBUG
1574 	if (siopng_debug & 1)
1575 		printf ("%s: intr istat %x dstat %x sist %x\n",
1576 		    sc->sc_dev.dv_xname, istat, dstat, sist);
1577 	if (!sc->sc_active) {
1578 		printf ("%s: spurious interrupt? istat %x dstat %x sist %x nexus %p status %x\n",
1579 		    sc->sc_dev.dv_xname, istat, dstat, sist,
1580 		    sc->sc_nexus, sc->sc_nexus ? sc->sc_nexus->stat[0] : 0);
1581 	}
1582 #endif
1583 
1584 #ifdef DEBUG
1585 	if (siopng_debug & 5) {
1586 		DCIAS(kvtop(&sc->sc_nexus->stat[0]));
1587 		printf ("%s: intr istat %x dstat %x sist %x dsps %lx sbcl %x sts %x msg %x\n",
1588 		    sc->sc_dev.dv_xname, istat, dstat, sist,
1589 		    rp->siop_dsps,  rp->siop_sbcl,
1590 		    sc->sc_nexus->stat[0], sc->sc_nexus->msg[0]);
1591 	}
1592 #endif
1593 	if (sc->sc_flags & SIOP_INTDEFER) {
1594 		sc->sc_flags &= ~(SIOP_INTDEFER | SIOP_INTSOFF);
1595 		rp->siop_sien = sc->sc_sien;
1596 		rp->siop_dien = sc->sc_dien;
1597 	}
1598 	if (siopng_checkintr (sc, istat, dstat, sist, &status)) {
1599 #if 1
1600 		if (status == 0xff)
1601 			printf ("siopngintr: status == 0xff\n");
1602 #endif
1603 		if ((sc->sc_flags & (SIOP_INTSOFF | SIOP_INTDEFER)) != SIOP_INTSOFF) {
1604 #if 0
1605 			if (rp->siop_sbcl & SIOP_BSY) {
1606 				printf ("%s: SCSI bus busy at completion",
1607 					sc->sc_dev.dv_xname);
1608 				printf(" targ %d sbcl %02x sfbr %x respid %02x dsp +%x\n",
1609 				    sc->sc_nexus->xs->xs_periph->periph_target,
1610 				    rp->siop_sbcl, rp->siop_sfbr, rp->siop_respid,
1611 				    rp->siop_dsp - sc->sc_scriptspa);
1612 			}
1613 #endif
1614 			siopng_scsidone(sc->sc_nexus, sc->sc_nexus ?
1615 			    sc->sc_nexus->stat[0] : -1);
1616 		}
1617 	}
1618 	splx(s);
1619 }
1620 
1621 /*
1622  * This is based on the Progressive Peripherals 33Mhz Zeus driver and will
1623  * not be correct for other 53c710 boards.
1624  *
1625  */
1626 void
1627 scsi_period_to_siopng(struct siop_softc *sc, int target)
1628 {
1629 	int period, offset, sxfer, scntl3 = 0;
1630 
1631 	period = sc->sc_nexus->msg[4];
1632 	offset = sc->sc_nexus->msg[5];
1633 #ifdef FIXME
1634 	for (scntl3 = 1; scntl3 < 4; ++scntl3) {
1635 		sxfer = (period * 4 - 1) / sc->sc_tcp[scntl3] - 3;
1636 		if (sxfer >= 0 && sxfer <= 7)
1637 			break;
1638 	}
1639 	if (scntl3 > 3) {
1640 		printf("siopng sync: unable to compute sync params for period %dns\n",
1641 		    period * 4);
1642 		/*
1643 		 * XXX need to pick a value we can do and renegotiate
1644 		 */
1645 		sxfer = scntl3 = 0;
1646 	} else {
1647 		sxfer = (sxfer << 4) | ((offset <= SIOP_MAX_OFFSET) ?
1648 		    offset : SIOP_MAX_OFFSET);
1649 #ifdef DEBUG_SYNC
1650 		printf("siopng sync: params for period %dns: sxfer %x scntl3 %x",
1651 		    period * 4, sxfer, scntl3);
1652 		printf(" actual period %dns\n",
1653 		    sc->sc_tcp[scntl3] * ((sxfer >> 4) + 4));
1654 #endif /* DEBUG_SYNC */
1655 	}
1656 #else /* FIXME */
1657 	sxfer = offset <= SIOP_MAX_OFFSET ? offset : SIOP_MAX_OFFSET;
1658 	sxfer |= 0x20;		/* XXX XFERP: 5 */
1659 #ifndef FIXME
1660 	if (period <= (50 / 4))	/* XXX */
1661 		scntl3 = 0x95;	/* Ultra, SCF: /1, CCF: /4 */
1662 	else if (period <= (100 / 4))
1663 		scntl3 = 0x35;	/* SCF: /2, CCF: /4 */
1664 	else if (period <= (200 / 4))
1665 		scntl3 = 0x55;	/* SCF: /4, CCF: /4 */
1666 	else
1667 		scntl3 = 0xff;	/* XXX ??? */
1668 #else
1669 	scntl3 = 5;
1670 #endif
1671 #endif
1672 	sc->sc_sync[target].sxfer = sxfer;
1673 	sc->sc_sync[target].scntl3 = scntl3 |
1674 	    (sc->sc_sync[target].scntl3 & SIOP_SCNTL3_EWS);
1675 #ifdef DEBUG_SYNC
1676 	printf ("siopng sync: siop_sxfr %02x, siop_scntl3 %02x\n", sxfer, scntl3);
1677 #endif
1678 }
1679 
1680 void
1681 siopng_dump_registers(struct siop_softc *sc)
1682 {
1683 	siop_regmap_p rp = sc->sc_siopp;
1684 
1685 	printf("  scntl0   %02x scntl1 %02x scntl2 %02x scntl3 %02x\n",
1686 	    rp->siop_scntl0, rp->siop_scntl1, rp->siop_scntl2, rp->siop_scntl3);
1687 	printf("  scid     %02x sxfer  %02x sdid   %02x gpreg  %02x\n",
1688 	    rp->siop_scid, rp->siop_sxfer, rp->siop_sdid, rp->siop_gpreg);
1689 	printf("  sfbr     %02x socl   %02x ssid   %02x sbcl   %02x\n",
1690 	    rp->siop_sfbr, rp->siop_socl, rp->siop_ssid, rp->siop_sbcl);
1691 	printf("  dstat    %02x sstat0 %02x sstat1 %02x sstat2 %02x\n",
1692 	    rp->siop_dstat, rp->siop_sstat0, rp->siop_sstat1, rp->siop_sstat2);
1693 	printf("  ctest0   %02x ctest1 %02x ctest2 %02x ctest3 %02x\n",
1694 	    rp->siop_ctest0, rp->siop_ctest1, rp->siop_ctest2, rp->siop_ctest3);
1695 	printf("  dfifo    %02x ctest4 %02x ctest5 %02x ctest6 %02x\n",
1696 	    0, rp->siop_ctest4, rp->siop_ctest5, rp->siop_ctest6);
1697 	printf("  dcmd     %02x dbc2   %02x dbc1   %02x dbc0   %02x\n",
1698 	    rp->siop_dcmd, rp->siop_dbc2, rp->siop_dbc1, rp->siop_dbc0);
1699 	printf("  dmode    %02x dien   %02x dwt    %02x dcntl  %02x\n",
1700 	    rp->siop_dmode, rp->siop_dien, rp->siop_dwt, rp->siop_dcntl);
1701 	printf("  stest0   %02x stest1 %02x stest2 %02x stest3 %02x\n",
1702 	    rp->siop_stest0, rp->siop_stest1, rp->siop_stest2, rp->siop_stest3);
1703 	printf("  istat    %02x sien %04x sist %04x respid %04x\n",
1704 	    rp->siop_istat, rp->siop_sien, rp->siop_sist, rp->siop_respid);
1705 	printf("  sidl   %04x sodl %04x sbdl %04x\n",
1706 	    rp->siop_sidl, rp->siop_sodl, rp->siop_sbdl);
1707 	printf("  dsps     %08lx dsp      %08lx (+%lx)\n",
1708 	    rp->siop_dsps, rp->siop_dsp, rp->siop_dsp > sc->sc_scriptspa ?
1709 	    rp->siop_dsp - sc->sc_scriptspa : 0);
1710 	printf("  dsa      %08lx temp     %08lx dnad     %08lx\n",
1711 	    rp->siop_dsa, rp->siop_temp, rp->siop_dnad);
1712 	printf("  scratcha %08lx scratchb %08lx adder     %08lx\n",
1713 	    rp->siop_scratcha, rp->siop_scratchb, rp->siop_adder);
1714 }
1715 
1716 #ifdef DEBUG
1717 
1718 #if SIOP_TRACE_SIZE
1719 void
1720 siopng_dump_trace(void)
1721 {
1722 	int i;
1723 
1724 	printf("siopng trace: next index %d\n", siopng_trix);
1725 	i = siopng_trix;
1726 	do {
1727 		printf("%3d: '%c' %02x %02x %02x\n", i, siopng_trbuf[i],
1728 		    siopng_trbuf[i + 1], siopng_trbuf[i + 2], siopng_trbuf[i + 3]);
1729 		i = (i + 4) & (SIOP_TRACE_SIZE - 1);
1730 	} while (i != siopng_trix);
1731 }
1732 #endif
1733 
1734 void
1735 siopng_dump_acb(struct siop_acb *acb)
1736 {
1737 	u_char *b = (u_char *) &acb->cmd;
1738 	int i;
1739 
1740 	printf("acb@%p ", acb);
1741 	if (acb->xs == NULL) {
1742 		printf("<unused>\n");
1743 		return;
1744 	}
1745 	printf("(%d:%d) flags %2x clen %2d cmd ",
1746 		acb->xs->xs_periph->periph_target,
1747 	    acb->xs->xs_periph->periph_lun, acb->flags, acb->clen);
1748 	for (i = acb->clen; i; --i)
1749 		printf(" %02x", *b++);
1750 	printf("\n");
1751 	printf("  xs: %p data %p:%04x ", acb->xs, acb->xs->data,
1752 	    acb->xs->datalen);
1753 	printf("va %p:%lx ", acb->iob_buf, acb->iob_len);
1754 	printf("cur %lx:%lx\n", acb->iob_curbuf, acb->iob_curlen);
1755 }
1756 
1757 void
1758 siopng_dump(struct siop_softc *sc)
1759 {
1760 	struct siop_acb *acb;
1761 	siop_regmap_p rp = sc->sc_siopp;
1762 	int s;
1763 	int i;
1764 
1765 	s = splbio();
1766 #if SIOP_TRACE_SIZE
1767 	siopng_dump_trace();
1768 #endif
1769 	printf("%s@%p regs %p istat %x\n",
1770 	    sc->sc_dev.dv_xname, sc, rp, rp->siop_istat);
1771 	if ((acb = sc->free_list.tqh_first) > 0) {
1772 		printf("Free list:\n");
1773 		while (acb) {
1774 			siopng_dump_acb(acb);
1775 			acb = acb->chain.tqe_next;
1776 		}
1777 	}
1778 	if ((acb = sc->ready_list.tqh_first) > 0) {
1779 		printf("Ready list:\n");
1780 		while (acb) {
1781 			siopng_dump_acb(acb);
1782 			acb = acb->chain.tqe_next;
1783 		}
1784 	}
1785 	if ((acb = sc->nexus_list.tqh_first) > 0) {
1786 		printf("Nexus list:\n");
1787 		while (acb) {
1788 			siopng_dump_acb(acb);
1789 			acb = acb->chain.tqe_next;
1790 		}
1791 	}
1792 	if (sc->sc_nexus) {
1793 		printf("Nexus:\n");
1794 		siopng_dump_acb(sc->sc_nexus);
1795 	}
1796 	for (i = 0; i < 8; ++i) {
1797 		if (sc->sc_tinfo[i].cmds > 2) {
1798 			printf("tgt %d: cmds %d disc %d lubusy %x\n",
1799 			    i, sc->sc_tinfo[i].cmds,
1800 			    sc->sc_tinfo[i].dconns,
1801 			    sc->sc_tinfo[i].lubusy);
1802 		}
1803 	}
1804 	splx(s);
1805 }
1806 #endif
1807