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