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