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