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