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