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