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