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