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