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