xref: /netbsd-src/sys/dev/ic/osiop.c (revision d20841bb642898112fe68f0ad3f7b26dddf56f07)
1 /*	$NetBSD: osiop.c,v 1.17 2003/11/02 11:07:45 wiz Exp $	*/
2 
3 /*
4  * Copyright (c) 2001 Izumi Tsutsui.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 /*
30  * Copyright (c) 1990 The Regents of the University of California.
31  * All rights reserved.
32  *
33  * This code is derived from software contributed to Berkeley by
34  * Van Jacobson of Lawrence Berkeley Laboratory.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  * 1. Redistributions of source code must retain the above copyright
40  *    notice, this list of conditions and the following disclaimer.
41  * 2. Redistributions in binary form must reproduce the above copyright
42  *    notice, this list of conditions and the following disclaimer in the
43  *    documentation and/or other materials provided with the distribution.
44  * 3. Neither the name of the University nor the names of its contributors
45  *    may be used to endorse or promote products derived from this software
46  *    without specific prior written permission.
47  *
48  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58  * SUCH DAMAGE.
59  *
60  *	@(#)siop.c	7.5 (Berkeley) 5/4/91
61  */
62 
63 /*
64  * Copyright (c) 1994 Michael L. Hitch
65  *
66  * This code is derived from software contributed to Berkeley by
67  * Van Jacobson of Lawrence Berkeley Laboratory.
68  *
69  * Redistribution and use in source and binary forms, with or without
70  * modification, are permitted provided that the following conditions
71  * are met:
72  * 1. Redistributions of source code must retain the above copyright
73  *    notice, this list of conditions and the following disclaimer.
74  * 2. Redistributions in binary form must reproduce the above copyright
75  *    notice, this list of conditions and the following disclaimer in the
76  *    documentation and/or other materials provided with the distribution.
77  * 3. All advertising materials mentioning features or use of this software
78  *    must display the following acknowledgement:
79  *	This product includes software developed by the University of
80  *	California, Berkeley and its contributors.
81  * 4. Neither the name of the University nor the names of its contributors
82  *    may be used to endorse or promote products derived from this software
83  *    without specific prior written permission.
84  *
85  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
86  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
87  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
88  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
89  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
90  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
91  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
92  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
93  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
94  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
95  * SUCH DAMAGE.
96  *
97  *	@(#)siop.c	7.5 (Berkeley) 5/4/91
98  */
99 
100 /*
101  * MI NCR53C710 scsi adaptor driver; based on arch/amiga/dev/siop.c:
102  *	NetBSD: siop.c,v 1.43 1999/09/30 22:59:53 thorpej Exp
103  *
104  * bus_space/bus_dma'fied by Izumi Tsutsui <tsutsui@ceres.dti.ne.jp>
105  *
106  * The 53c710 datasheet is available at:
107  * http://www.lsilogic.com/techlib/techdocs/storage_stand_prod/index.html
108  */
109 
110 #include <sys/cdefs.h>
111 __KERNEL_RCSID(0, "$NetBSD: osiop.c,v 1.17 2003/11/02 11:07:45 wiz Exp $");
112 
113 /* #define OSIOP_DEBUG */
114 
115 #include "opt_ddb.h"
116 
117 #include <sys/param.h>
118 #include <sys/systm.h>
119 #include <sys/device.h>
120 #include <sys/malloc.h>
121 #include <sys/buf.h>
122 #include <sys/kernel.h>
123 
124 #include <uvm/uvm_extern.h>
125 
126 #include <dev/scsipi/scsi_all.h>
127 #include <dev/scsipi/scsipi_all.h>
128 #include <dev/scsipi/scsiconf.h>
129 #include <dev/scsipi/scsi_message.h>
130 
131 #include <machine/cpu.h>
132 #include <machine/bus.h>
133 
134 #include <dev/ic/osiopreg.h>
135 #include <dev/ic/osiopvar.h>
136 
137 /* 53C710 script */
138 #include <dev/microcode/siop/osiop.out>
139 
140 void osiop_attach(struct osiop_softc *);
141 void osiop_minphys(struct buf *);
142 void osiop_scsipi_request(struct scsipi_channel *, scsipi_adapter_req_t,
143     void *);
144 void osiop_poll(struct osiop_softc *, struct osiop_acb *);
145 void osiop_sched(struct osiop_softc *);
146 void osiop_scsidone(struct osiop_acb *, int);
147 void osiop_abort(struct osiop_softc *, const char *);
148 void osiop_init(struct osiop_softc *);
149 void osiop_reset(struct osiop_softc *);
150 void osiop_resetbus(struct osiop_softc *);
151 void osiop_start(struct osiop_softc *);
152 int osiop_checkintr(struct osiop_softc *, u_int8_t, u_int8_t, u_int8_t, int *);
153 void osiop_select(struct osiop_softc *);
154 void osiop_update_xfer_mode(struct osiop_softc *, int);
155 void scsi_period_to_osiop(struct osiop_softc *, int);
156 void osiop_timeout(void *);
157 
158 int osiop_reset_delay = 250;	/* delay after reset, in milliseconds */
159 
160 #ifdef OSIOP_DEBUG
161 #define DEBUG_DMA	0x01
162 #define DEBUG_INT	0x02
163 #define DEBUG_PHASE	0x04
164 #define DEBUG_UNEXCEPT	0x08
165 #define DEBUG_DISC	0x10
166 #define DEBUG_CMD	0x20
167 #define DEBUG_ALL	0xff
168 int osiop_debug = 0; /*DEBUG_ALL;*/
169 int osiopsync_debug = 0;
170 int osiopdma_hits = 1;
171 int osiopstarts = 0;
172 int osiopints = 0;
173 int osiopphmm = 0;
174 int osiop_trix = 0;
175 #define OSIOP_TRACE_SIZE	128
176 #define OSIOP_TRACE(a,b,c,d)	do {				\
177 	osiop_trbuf[osiop_trix + 0] = (a);			\
178 	osiop_trbuf[osiop_trix + 1] = (b);			\
179 	osiop_trbuf[osiop_trix + 2] = (c);			\
180 	osiop_trbuf[osiop_trix + 3] = (d);			\
181 	osiop_trix = (osiop_trix + 4) & (OSIOP_TRACE_SIZE - 1);	\
182 } while (0)
183 u_int8_t osiop_trbuf[OSIOP_TRACE_SIZE];
184 void osiop_dump_trace(void);
185 void osiop_dump_acb(struct osiop_acb *);
186 void osiop_dump(struct osiop_softc *);
187 #else
188 #define OSIOP_TRACE(a,b,c,d)
189 #endif
190 
191 void
192 osiop_attach(sc)
193 	struct osiop_softc *sc;
194 {
195 	struct osiop_acb *acb;
196 	bus_dma_segment_t seg;
197 	int nseg;
198 	int i, err;
199 
200 	/*
201 	 * Allocate and map DMA-safe memory for the script.
202 	 */
203 	err = bus_dmamem_alloc(sc->sc_dmat, PAGE_SIZE, PAGE_SIZE, 0,
204 	    &seg, 1, &nseg, BUS_DMA_NOWAIT);
205 	if (err) {
206 		printf(": failed to allocate script memory, err=%d\n", err);
207 		return;
208 	}
209 	err = bus_dmamem_map(sc->sc_dmat, &seg, nseg, PAGE_SIZE,
210 	    (caddr_t *)&sc->sc_script, BUS_DMA_NOWAIT | BUS_DMA_COHERENT);
211 	if (err) {
212 		printf(": failed to map script memory, err=%d\n", err);
213 		return;
214 	}
215 	err = bus_dmamap_create(sc->sc_dmat, PAGE_SIZE, 1, PAGE_SIZE, 0,
216 	    BUS_DMA_NOWAIT, &sc->sc_scrdma);
217 	if (err) {
218 		printf(": failed to create script map, err=%d\n", err);
219 		return;
220 	}
221 	err = bus_dmamap_load(sc->sc_dmat, sc->sc_scrdma,
222 	    sc->sc_script, PAGE_SIZE, NULL, BUS_DMA_NOWAIT);
223 	if (err) {
224 		printf(": failed to load script map, err=%d\n", err);
225 		return;
226 	}
227 
228 	/*
229 	 * Copy and sync script
230 	 */
231 	memcpy(sc->sc_script, osiop_script, sizeof(osiop_script));
232 	bus_dmamap_sync(sc->sc_dmat, sc->sc_scrdma, 0, sizeof(osiop_script),
233 	    BUS_DMASYNC_PREWRITE);
234 
235 	/*
236 	 * Allocate and map DMA-safe memory for the script data structure.
237 	 */
238 	err = bus_dmamem_alloc(sc->sc_dmat,
239 	    sizeof(struct osiop_ds) * OSIOP_NACB, PAGE_SIZE, 0,
240 	    &seg, 1, &nseg, BUS_DMA_NOWAIT);
241 	if (err) {
242 		printf(": failed to allocate ds memory, err=%d\n", err);
243 		return;
244 	}
245 	err = bus_dmamem_map(sc->sc_dmat, &seg, nseg,
246 	    sizeof(struct osiop_ds) * OSIOP_NACB, (caddr_t *)&sc->sc_ds,
247 	    BUS_DMA_NOWAIT | BUS_DMA_COHERENT);
248 	if (err) {
249 		printf(": failed to map ds memory, err=%d\n", err);
250 		return;
251 	}
252 	err = bus_dmamap_create(sc->sc_dmat,
253 	    sizeof(struct osiop_ds) * OSIOP_NACB, 1,
254 	    sizeof(struct osiop_ds) * OSIOP_NACB, 0,
255 	    BUS_DMA_NOWAIT, &sc->sc_dsdma);
256 	if (err) {
257 		printf(": failed to create ds map, err=%d\n", err);
258 		return;
259 	}
260 	err = bus_dmamap_load(sc->sc_dmat, sc->sc_dsdma, sc->sc_ds,
261 	    sizeof(struct osiop_ds) * OSIOP_NACB, NULL, BUS_DMA_NOWAIT);
262 	if (err) {
263 		printf(": failed to load ds map, err=%d\n", err);
264 		return;
265 	}
266 
267 	acb = malloc(sizeof(struct osiop_acb) * OSIOP_NACB,
268 	    M_DEVBUF, M_NOWAIT|M_ZERO);
269 	if (acb == NULL) {
270 		printf(": can't allocate memory for acb\n");
271 		return;
272 	}
273 	sc->sc_acb = acb;
274 	sc->sc_cfflags = sc->sc_dev.dv_cfdata->cf_flags;
275 	sc->sc_nexus = NULL;
276 	sc->sc_active = 0;
277 	memset(sc->sc_tinfo, 0, sizeof(sc->sc_tinfo));
278 
279 	/* Initialize command block queue */
280 	TAILQ_INIT(&sc->ready_list);
281 	TAILQ_INIT(&sc->nexus_list);
282 	TAILQ_INIT(&sc->free_list);
283 
284 	/* Initialize each command block */
285 	for (i = 0; i < OSIOP_NACB; i++) {
286 		bus_addr_t dsa;
287 
288 		/* XXX How much size is required for each command block? */
289 		err = bus_dmamap_create(sc->sc_dmat, PAGE_SIZE, 1, PAGE_SIZE,
290 		    0, BUS_DMA_NOWAIT, &acb->cmddma);
291 		if (err) {
292 			printf(": failed to create cmddma map, err=%d\n", err);
293 			return;
294 		}
295 		err = bus_dmamap_create(sc->sc_dmat, OSIOP_MAX_XFER, OSIOP_NSG,
296 		    OSIOP_MAX_XFER, 0, BUS_DMA_NOWAIT, &acb->datadma);
297 		if (err) {
298 			printf(": failed to create datadma map, err=%d\n",
299 			    err);
300 			return;
301 		}
302 
303 		acb->sc = sc;
304 		acb->ds = &sc->sc_ds[i];
305 		acb->dsoffset = sizeof(struct osiop_ds) * i;
306 
307 		dsa = sc->sc_dsdma->dm_segs[0].ds_addr + acb->dsoffset;
308 		acb->ds->id.addr = dsa + OSIOP_DSIDOFF;
309 		acb->ds->status.count = 1;
310 		acb->ds->status.addr = dsa + OSIOP_DSSTATOFF;
311 		acb->ds->msg.count = 1;
312 		acb->ds->msg.addr = dsa + OSIOP_DSMSGOFF;
313 		acb->ds->msgin.count = 1;
314 		acb->ds->msgin.addr = dsa + OSIOP_DSMSGINOFF;
315 		acb->ds->extmsg.count = 1;
316 		acb->ds->extmsg.addr = dsa + OSIOP_DSEXTMSGOFF;
317 		acb->ds->synmsg.count = 3;
318 		acb->ds->synmsg.addr = dsa + OSIOP_DSSYNMSGOFF;
319 		TAILQ_INSERT_TAIL(&sc->free_list, acb, chain);
320 
321 		acb++;
322 	}
323 
324 	printf(": NCR53C710 rev %d, %dMHz, SCSI ID %d\n",
325 	    osiop_read_1(sc, OSIOP_CTEST8) >> 4, sc->sc_clock_freq, sc->sc_id);
326 
327 	/*
328 	 * Initialize all
329 	 */
330 	osiop_init(sc);
331 
332 	/*
333 	 * Fill in the adapter.
334 	 */
335 	sc->sc_adapter.adapt_dev = &sc->sc_dev;
336 	sc->sc_adapter.adapt_nchannels = 1;
337 	sc->sc_adapter.adapt_openings = OSIOP_NACB;
338 	sc->sc_adapter.adapt_max_periph = 1;
339 	sc->sc_adapter.adapt_ioctl = NULL;
340 	sc->sc_adapter.adapt_minphys = osiop_minphys;
341 	sc->sc_adapter.adapt_request = osiop_scsipi_request;
342 
343 	/*
344 	 * Fill in the channel.
345 	 */
346 	sc->sc_channel.chan_adapter = &sc->sc_adapter;
347 	sc->sc_channel.chan_bustype = &scsi_bustype;
348 	sc->sc_channel.chan_channel = 0;
349 	sc->sc_channel.chan_ntargets = OSIOP_NTGT;
350 	sc->sc_channel.chan_nluns = 8;
351 	sc->sc_channel.chan_id = sc->sc_id;
352 
353 	/*
354 	 * Now try to attach all the sub devices.
355 	 */
356 	config_found(&sc->sc_dev, &sc->sc_channel, scsiprint);
357 }
358 
359 /*
360  * default minphys routine for osiop based controllers
361  */
362 void
363 osiop_minphys(bp)
364 	struct buf *bp;
365 {
366 
367 	if (bp->b_bcount > OSIOP_MAX_XFER)
368 		bp->b_bcount = OSIOP_MAX_XFER;
369 	minphys(bp);
370 }
371 
372 /*
373  * used by specific osiop controller
374  *
375  */
376 void
377 osiop_scsipi_request(chan, req, arg)
378 	struct scsipi_channel *chan;
379 	scsipi_adapter_req_t req;
380 	void *arg;
381 {
382 	struct scsipi_xfer *xs;
383 	struct scsipi_periph *periph;
384 	struct osiop_acb *acb;
385 	struct osiop_softc *sc;
386 	int err, flags, s;
387 
388 	sc = (struct osiop_softc *)chan->chan_adapter->adapt_dev;
389 
390 	switch (req) {
391 	case ADAPTER_REQ_RUN_XFER:
392 		xs = arg;
393 		periph = xs->xs_periph;
394 		flags = xs->xs_control;
395 
396 		/* XXXX ?? */
397 		if (flags & XS_CTL_DATA_UIO)
398 			panic("osiop: scsi data uio requested");
399 
400 		/* XXXX ?? */
401 		if (sc->sc_nexus && flags & XS_CTL_POLL)
402 #if 0
403 			panic("osiop_scsicmd: busy");
404 #else
405 			printf("osiop_scsicmd: busy\n");
406 #endif
407 
408 		s = splbio();
409 		acb = TAILQ_FIRST(&sc->free_list);
410 		if (acb != NULL) {
411 			TAILQ_REMOVE(&sc->free_list, acb, chain);
412 		}
413 #ifdef DIAGNOSTIC
414 		else {
415 			scsipi_printaddr(periph);
416 			printf("unable to allocate acb\n");
417 			panic("osiop_scsipi_request");
418 		}
419 #endif
420 
421 		acb->status = ACB_S_READY;
422 		acb->xs = xs;
423 
424 		/* Setup DMA map for SCSI command buffer */
425 		err = bus_dmamap_load(sc->sc_dmat, acb->cmddma,
426 		    xs->cmd, xs->cmdlen, NULL, BUS_DMA_NOWAIT);
427 		if (err) {
428 			printf("%s: unable to load cmd DMA map: %d",
429 			    sc->sc_dev.dv_xname, err);
430 			xs->error = XS_DRIVER_STUFFUP;
431 			TAILQ_INSERT_TAIL(&sc->free_list, acb, chain);
432 			scsipi_done(xs);
433 			splx(s);
434 			return;
435 		}
436 
437 		/* Setup DMA map for data buffer */
438 		if (xs->xs_control & (XS_CTL_DATA_IN | XS_CTL_DATA_OUT)) {
439 			err = bus_dmamap_load(sc->sc_dmat, acb->datadma,
440 			    xs->data, xs->datalen, NULL,
441 			    BUS_DMA_NOWAIT | BUS_DMA_STREAMING |
442 			    ((xs->xs_control & XS_CTL_DATA_IN) ?
443 			     BUS_DMA_READ : BUS_DMA_WRITE));
444 			if (err) {
445 				printf("%s: unable to load data DMA map: %d",
446 				    sc->sc_dev.dv_xname, err);
447 				xs->error = XS_DRIVER_STUFFUP;
448 				scsipi_done(xs);
449 				bus_dmamap_unload(sc->sc_dmat, acb->cmddma);
450 				TAILQ_INSERT_TAIL(&sc->free_list, acb, chain);
451 				splx(s);
452 				return;
453 			}
454 			bus_dmamap_sync(sc->sc_dmat, acb->datadma,
455 			    0, xs->datalen, (xs->xs_control & XS_CTL_DATA_IN) ?
456 			    BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
457 		}
458 		bus_dmamap_sync(sc->sc_dmat, acb->cmddma, 0, xs->cmdlen,
459 		    BUS_DMASYNC_PREWRITE);
460 
461 		acb->cmdlen = xs->cmdlen;
462 		acb->datalen = xs->datalen;
463 #ifdef OSIOP_DEBUG
464 		acb->data = xs->data;
465 #endif
466 
467 		TAILQ_INSERT_TAIL(&sc->ready_list, acb, chain);
468 
469 		if (sc->sc_nexus == NULL)
470 			osiop_sched(sc);
471 
472 		splx(s);
473 
474 		if (flags & XS_CTL_POLL || sc->sc_flags & OSIOP_NODMA)
475 			osiop_poll(sc, acb);
476 		return;
477 
478 	case ADAPTER_REQ_GROW_RESOURCES:
479 		return;
480 
481 	case ADAPTER_REQ_SET_XFER_MODE:
482 		{
483 			struct osiop_tinfo *ti;
484 			struct scsipi_xfer_mode *xm = arg;
485 
486 			ti = &sc->sc_tinfo[xm->xm_target];
487 
488 			if ((xm->xm_mode & PERIPH_CAP_SYNC) != 0 &&
489 			    (ti->flags & TI_NOSYNC) == 0)
490 				ti->state = NEG_INIT;
491 
492 			/*
493 			 * If we're not going to negotiate, send the
494 			 * notification now, since it won't happen later.
495 			 */
496 			if (ti->state == NEG_DONE)
497 				osiop_update_xfer_mode(sc, xm->xm_target);
498 
499 			return;
500 		}
501 	}
502 }
503 
504 void
505 osiop_poll(sc, acb)
506 	struct osiop_softc *sc;
507 	struct osiop_acb *acb;
508 {
509 	struct scsipi_xfer *xs = acb->xs;
510 	int status, i, s, to;
511 	u_int8_t istat, dstat, sstat0;
512 
513 	s = splbio();
514 	to = xs->timeout / 1000;
515 	if (!TAILQ_EMPTY(&sc->nexus_list))
516 		printf("%s: osiop_poll called with disconnected device\n",
517 		    sc->sc_dev.dv_xname);
518 	for (;;) {
519 		i = 1000;
520 		while (((istat = osiop_read_1(sc, OSIOP_ISTAT)) &
521 		    (OSIOP_ISTAT_SIP | OSIOP_ISTAT_DIP)) == 0) {
522 			if (i <= 0) {
523 #ifdef OSIOP_DEBUG
524 				printf("waiting: tgt %d cmd %02x sbcl %02x"
525 				    " dsp %x (+%lx) dcmd %x"
526 				    " ds %p timeout %d\n",
527 				    xs->xs_periph->periph_target,
528 				    xs->cmd->opcode,
529 				    osiop_read_1(sc, OSIOP_SBCL),
530 				    osiop_read_4(sc, OSIOP_DSP),
531 				    osiop_read_4(sc, OSIOP_DSP) -
532 				        sc->sc_scrdma->dm_segs[0].ds_addr,
533 				    osiop_read_1(sc, OSIOP_DCMD),
534 				    acb->ds, acb->xs->timeout);
535 #endif
536 				i = 1000;
537 				to--;
538 				if (to <= 0) {
539 					osiop_reset(sc);
540 					splx(s);
541 					return;
542 				}
543 			}
544 			delay(1000);
545 			i--;
546 		}
547 		sstat0 = osiop_read_1(sc, OSIOP_SSTAT0);
548 		delay(25);
549 		dstat = osiop_read_1(sc, OSIOP_DSTAT);
550 		if (osiop_checkintr(sc, istat, dstat, sstat0, &status)) {
551 			if (acb != sc->sc_nexus)
552 				printf("%s: osiop_poll disconnected device"
553 				    " completed\n", sc->sc_dev.dv_xname);
554 			else if ((sc->sc_flags & OSIOP_INTDEFER) == 0) {
555 				sc->sc_flags &= ~OSIOP_INTSOFF;
556 				osiop_write_1(sc, OSIOP_SIEN, sc->sc_sien);
557 				osiop_write_1(sc, OSIOP_DIEN, sc->sc_dien);
558 			}
559 			osiop_scsidone(sc->sc_nexus, status);
560 		}
561 
562 		if (xs->xs_status & XS_STS_DONE)
563 			break;
564 	}
565 
566 	splx(s);
567 	return;
568 }
569 
570 /*
571  * start next command that's ready
572  */
573 void
574 osiop_sched(sc)
575 	struct osiop_softc *sc;
576 {
577 	struct scsipi_periph *periph;
578 	struct osiop_acb *acb;
579 	int i;
580 
581 #ifdef OSIOP_DEBUG
582 	if (sc->sc_nexus != NULL) {
583 		printf("%s: osiop_sched- nexus %p/%d ready %p/%d\n",
584 		    sc->sc_dev.dv_xname, sc->sc_nexus,
585 		    sc->sc_nexus->xs->xs_periph->periph_target,
586 		    sc->ready_list.tqh_first,
587 		    sc->ready_list.tqh_first->xs->xs_periph->periph_target);
588 		return;
589 	}
590 #endif
591 	TAILQ_FOREACH(acb, &sc->ready_list, chain) {
592 		periph = acb->xs->xs_periph;
593 		i = periph->periph_target;
594 		if ((sc->sc_tinfo[i].lubusy & (1 << periph->periph_lun)) == 0) {
595 			struct osiop_tinfo *ti;
596 
597 			TAILQ_REMOVE(&sc->ready_list, acb, chain);
598 			sc->sc_nexus = acb;
599 			ti = &sc->sc_tinfo[i];
600 			ti->lubusy |= (1 << periph->periph_lun);
601 			break;
602 		}
603 	}
604 
605 	if (acb == NULL) {
606 #ifdef OSIOP_DEBUG
607 		printf("%s: osiop_sched didn't find ready command\n",
608 		    sc->sc_dev.dv_xname);
609 #endif
610 		return;
611 	}
612 
613 	if (acb->xs->xs_control & XS_CTL_RESET)
614 		osiop_reset(sc);
615 
616 	sc->sc_active++;
617 	osiop_select(sc);
618 }
619 
620 void
621 osiop_scsidone(acb, status)
622 	struct osiop_acb *acb;
623 	int status;
624 {
625 	struct scsipi_xfer *xs;
626 	struct scsipi_periph *periph;
627 	struct osiop_softc *sc;
628 	int dosched = 0;
629 
630 #ifdef DIAGNOSTIC
631 	if (acb == NULL || acb->xs == NULL) {
632 		printf("osiop_scsidone: NULL acb or scsipi_xfer\n");
633 #if defined(OSIOP_DEBUG) && defined(DDB)
634 		Debugger();
635 #endif
636 		return;
637 	}
638 #endif
639 	xs = acb->xs;
640 	sc = acb->sc;
641 	periph = xs->xs_periph;
642 
643 #ifdef OSIOP_DEBUG
644 	if (acb->status != ACB_S_DONE)
645 		printf("%s: acb not done (status %d)\n",
646 		    sc->sc_dev.dv_xname, acb->status);
647 #endif
648 
649 	xs->status = status;
650 
651 	switch (status) {
652 	case SCSI_OK:
653 		xs->error = XS_NOERROR;
654 		break;
655 	case SCSI_BUSY:
656 		xs->error = XS_BUSY;
657 		break;
658 	case SCSI_CHECK:
659 		xs->error = XS_BUSY;
660 		break;
661 	case SCSI_OSIOP_NOCHECK:
662 		/*
663 		 * don't check status, xs->error is already valid
664 		 */
665 		break;
666 	case SCSI_OSIOP_NOSTATUS:
667 		/*
668 		 * the status byte was not updated, cmd was
669 		 * aborted
670 		 */
671 		xs->error = XS_SELTIMEOUT;
672 		break;
673 	default:
674 #ifdef OSIOP_DEBUG
675 		printf("%s: osiop_scsidone: unknown status code (0x%02x)\n",
676 		    sc->sc_dev.dv_xname, status);
677 #endif
678 		xs->error = XS_DRIVER_STUFFUP;
679 		break;
680 	}
681 
682 	if (xs->xs_control & (XS_CTL_DATA_IN | XS_CTL_DATA_OUT)) {
683 		bus_dmamap_sync(sc->sc_dmat, acb->datadma, 0, acb->datalen,
684 		    (xs->xs_control & XS_CTL_DATA_IN) ?
685 		    BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
686 		bus_dmamap_unload(sc->sc_dmat, acb->datadma);
687 	}
688 
689 	bus_dmamap_sync(sc->sc_dmat, acb->cmddma, 0, acb->cmdlen,
690 	    BUS_DMASYNC_POSTWRITE);
691 	bus_dmamap_unload(sc->sc_dmat, acb->cmddma);
692 
693 	/*
694 	 * Remove the ACB from whatever queue it's on.  We have to do a bit of
695 	 * a hack to figure out which queue it's on.  Note that it is *not*
696 	 * necessary to cdr down the ready queue, but we must cdr down the
697 	 * nexus queue and see if it's there, so we can mark the unit as no
698 	 * longer busy.  This code is sickening, but it works.
699 	 */
700 	if (acb == sc->sc_nexus) {
701 		sc->sc_nexus = NULL;
702 		sc->sc_tinfo[periph->periph_target].lubusy &=
703 		    ~(1 << periph->periph_lun);
704 		if (!TAILQ_EMPTY(&sc->ready_list))
705 			dosched = 1;	/* start next command */
706 		sc->sc_active--;
707 		OSIOP_TRACE('d', 'a', status, 0);
708 	} else if (sc->ready_list.tqh_last == &acb->chain.tqe_next) {
709 		TAILQ_REMOVE(&sc->ready_list, acb, chain);
710 		OSIOP_TRACE('d', 'r', status, 0);
711 	} else {
712 		struct osiop_acb *acb2;
713 		TAILQ_FOREACH(acb2, &sc->nexus_list, chain) {
714 			if (acb2 == acb) {
715 				TAILQ_REMOVE(&sc->nexus_list, acb, chain);
716 				sc->sc_tinfo[periph->periph_target].lubusy &=
717 				    ~(1 << periph->periph_lun);
718 				sc->sc_active--;
719 				break;
720 			}
721 		}
722 		if (acb2 == NULL) {
723 			if (acb->chain.tqe_next != NULL) {
724 				TAILQ_REMOVE(&sc->ready_list, acb, chain);
725 				sc->sc_active--;
726 			} else {
727 				printf("%s: can't find matching acb\n",
728 				    sc->sc_dev.dv_xname);
729 #ifdef DDB
730 #if 0
731 				Debugger();
732 #endif
733 #endif
734 			}
735 		}
736 		OSIOP_TRACE('d', 'n', status, 0);
737 	}
738 	/* Put it on the free list. */
739 	acb->status = ACB_S_FREE;
740 	TAILQ_INSERT_TAIL(&sc->free_list, acb, chain);
741 	sc->sc_tinfo[periph->periph_target].cmds++;
742 
743 	callout_stop(&xs->xs_callout);
744 	xs->resid = 0;
745 	scsipi_done(xs);
746 
747 	if (dosched && sc->sc_nexus == NULL)
748 		osiop_sched(sc);
749 }
750 
751 void
752 osiop_abort(sc, where)
753 	struct osiop_softc *sc;
754 	const char *where;
755 {
756 
757 	printf("%s: abort %s: dstat %02x, sstat0 %02x sbcl %02x\n",
758 	    sc->sc_dev.dv_xname, where,
759 	    osiop_read_1(sc, OSIOP_DSTAT),
760 	    osiop_read_1(sc, OSIOP_SSTAT0),
761 	    osiop_read_1(sc, OSIOP_SBCL));
762 
763 	/* XXX XXX XXX */
764 	if (sc->sc_active > 0) {
765 		sc->sc_active = 0;
766 	}
767 }
768 
769 void
770 osiop_init(sc)
771 	struct osiop_softc *sc;
772 {
773 	int i, inhibit_sync, inhibit_disc;
774 
775 	sc->sc_tcp[1] = 1000 / sc->sc_clock_freq;
776 	sc->sc_tcp[2] = 1500 / sc->sc_clock_freq;
777 	sc->sc_tcp[3] = 2000 / sc->sc_clock_freq;
778 	sc->sc_minsync = sc->sc_tcp[1];		/* in 4ns units */
779 
780 	if (sc->sc_minsync < 25)
781 		sc->sc_minsync = 25;
782 
783 	if (sc->sc_clock_freq <= 25) {
784 		sc->sc_dcntl |= OSIOP_DCNTL_CF_1;	/* SCLK/1 */
785 		sc->sc_tcp[0] = sc->sc_tcp[1];
786 	} else if (sc->sc_clock_freq <= 37) {
787 		sc->sc_dcntl |= OSIOP_DCNTL_CF_1_5;	/* SCLK/1.5 */
788 		sc->sc_tcp[0] = sc->sc_tcp[2];
789 	} else if (sc->sc_clock_freq <= 50) {
790 		sc->sc_dcntl |= OSIOP_DCNTL_CF_2;	/* SCLK/2 */
791 		sc->sc_tcp[0] = sc->sc_tcp[3];
792 	} else {
793 		sc->sc_dcntl |= OSIOP_DCNTL_CF_3;	/* SCLK/3 */
794 		sc->sc_tcp[0] = 3000 / sc->sc_clock_freq;
795 	}
796 
797 	if ((sc->sc_cfflags & 0x10000) != 0) {
798 		sc->sc_flags |= OSIOP_NODMA;
799 #ifdef OSIOP_DEBUG
800 		printf("%s: DMA disabled; use polling\n",
801 		    sc->sc_dev.dv_xname);
802 #endif
803 	}
804 
805 	inhibit_sync = (sc->sc_cfflags & 0xff00) >> 8;	/* XXX */
806 	inhibit_disc =  sc->sc_cfflags & 0x00ff;	/* XXX */
807 #ifdef OSIOP_DEBUG
808 	if (inhibit_sync != 0)
809 		printf("%s: Inhibiting synchronous transfer: 0x%02x\n",
810 		    sc->sc_dev.dv_xname, inhibit_sync);
811 	if (inhibit_disc != 0)
812 		printf("%s: Inhibiting disconnect: 0x%02x\n",
813 		    sc->sc_dev.dv_xname, inhibit_disc);
814 #endif
815 	for (i = 0; i < OSIOP_NTGT; i++) {
816 		if (inhibit_sync & (1 << i))
817 			sc->sc_tinfo[i].flags |= TI_NOSYNC;
818 		if (inhibit_disc & (1 << i))
819 			sc->sc_tinfo[i].flags |= TI_NODISC;
820 	}
821 
822 	osiop_resetbus(sc);
823 	osiop_reset(sc);
824 }
825 
826 void
827 osiop_reset(sc)
828 	struct osiop_softc *sc;
829 {
830 	struct osiop_acb *acb;
831 	int i, s;
832 	u_int8_t stat;
833 
834 #ifdef OSIOP_DEBUG
835 	printf("%s: resetting chip\n", sc->sc_dev.dv_xname);
836 #endif
837 	if (sc->sc_flags & OSIOP_ALIVE)
838 		osiop_abort(sc, "reset");
839 
840 	s = splbio();
841 
842 	/*
843 	 * Reset the chip
844 	 * XXX - is this really needed?
845 	 */
846 
847 	/* abort current script */
848 	osiop_write_1(sc, OSIOP_ISTAT,
849 	    osiop_read_1(sc, OSIOP_ISTAT) | OSIOP_ISTAT_ABRT);
850 	/* reset chip */
851 	osiop_write_1(sc, OSIOP_ISTAT,
852 	    osiop_read_1(sc, OSIOP_ISTAT) | OSIOP_ISTAT_RST);
853 	delay(100);
854 	osiop_write_1(sc, OSIOP_ISTAT,
855 	    osiop_read_1(sc, OSIOP_ISTAT) & ~OSIOP_ISTAT_RST);
856 	delay(100);
857 
858 	/*
859 	 * Set up various chip parameters
860 	 */
861 	osiop_write_1(sc, OSIOP_SCNTL0,
862 	    OSIOP_ARB_FULL | OSIOP_SCNTL0_EPC | OSIOP_SCNTL0_EPG);
863 	osiop_write_1(sc, OSIOP_SCNTL1, OSIOP_SCNTL1_ESR);
864 	osiop_write_1(sc, OSIOP_DCNTL, sc->sc_dcntl);
865 	osiop_write_1(sc, OSIOP_DMODE, OSIOP_DMODE_BL4);
866 	/* don't enable interrupts yet */
867 	osiop_write_1(sc, OSIOP_SIEN, 0x00);
868 	osiop_write_1(sc, OSIOP_DIEN, 0x00);
869 	osiop_write_1(sc, OSIOP_SCID, OSIOP_SCID_VALUE(sc->sc_id));
870 	osiop_write_1(sc, OSIOP_DWT, 0x00);
871 	osiop_write_1(sc, OSIOP_CTEST0, osiop_read_1(sc, OSIOP_CTEST0)
872 	    | OSIOP_CTEST0_BTD | OSIOP_CTEST0_EAN);
873 	osiop_write_1(sc, OSIOP_CTEST7,
874 	    osiop_read_1(sc, OSIOP_CTEST7) | sc->sc_ctest7);
875 
876 	/* will need to re-negotiate sync xfers */
877 	for (i = 0; i < OSIOP_NTGT; i++) {
878 		sc->sc_tinfo[i].state = NEG_INIT;
879 		sc->sc_tinfo[i].period = 0;
880 		sc->sc_tinfo[i].offset = 0;
881 	}
882 
883 	stat = osiop_read_1(sc, OSIOP_ISTAT);
884 	if (stat & OSIOP_ISTAT_SIP)
885 		osiop_read_1(sc, OSIOP_SSTAT0);
886 	delay(25);
887 	if (stat & OSIOP_ISTAT_DIP)
888 		osiop_read_1(sc, OSIOP_DSTAT);
889 
890 	splx(s);
891 
892 	delay(osiop_reset_delay * 1000);
893 
894 	if (sc->sc_nexus != NULL) {
895 		sc->sc_nexus->xs->error =
896 		    (sc->sc_nexus->flags & ACB_F_TIMEOUT) ?
897 		    XS_TIMEOUT : XS_RESET;
898 		sc->sc_nexus->status = ACB_S_DONE;
899 		sc->sc_nexus->flags = 0;
900 		osiop_scsidone(sc->sc_nexus, SCSI_OSIOP_NOCHECK);
901 	}
902 	while ((acb = TAILQ_FIRST(&sc->nexus_list)) != NULL) {
903 		acb->xs->error = (acb->flags & ACB_F_TIMEOUT) ?
904 		    XS_TIMEOUT : XS_RESET;
905 		acb->status = ACB_S_DONE;
906 		acb->flags = 0;
907 		osiop_scsidone(acb, SCSI_OSIOP_NOCHECK);
908 	}
909 
910 	sc->sc_flags &= ~(OSIOP_INTDEFER | OSIOP_INTSOFF);
911 	/* enable SCSI and DMA interrupts */
912 	sc->sc_sien = OSIOP_SIEN_M_A | OSIOP_SIEN_STO | /*OSIOP_SIEN_SEL |*/
913 	    OSIOP_SIEN_SGE | OSIOP_SIEN_UDC | OSIOP_SIEN_RST | OSIOP_SIEN_PAR;
914 	sc->sc_dien = OSIOP_DIEN_BF | OSIOP_DIEN_ABRT | OSIOP_DIEN_SIR |
915 	    /*OSIOP_DIEN_WTD |*/ OSIOP_DIEN_IID;
916 	osiop_write_1(sc, OSIOP_SIEN, sc->sc_sien);
917 	osiop_write_1(sc, OSIOP_DIEN, sc->sc_dien);
918 }
919 
920 void
921 osiop_resetbus(sc)
922 	struct osiop_softc *sc;
923 {
924 
925 	osiop_write_1(sc, OSIOP_SIEN, 0);
926 	osiop_write_1(sc, OSIOP_SCNTL1,
927 	    osiop_read_1(sc, OSIOP_SCNTL1) | OSIOP_SCNTL1_RST);
928 	delay(25);
929 	osiop_write_1(sc, OSIOP_SCNTL1,
930 	    osiop_read_1(sc, OSIOP_SCNTL1) & ~OSIOP_SCNTL1_RST);
931 }
932 
933 /*
934  * Setup Data Storage for 53C710 and start SCRIPTS processing
935  */
936 
937 void
938 osiop_start(sc)
939 	struct osiop_softc *sc;
940 {
941 	struct osiop_acb *acb = sc->sc_nexus;
942 	struct osiop_ds *ds = acb->ds;
943 	struct scsipi_xfer *xs = acb->xs;
944 	bus_dmamap_t dsdma = sc->sc_dsdma, datadma = acb->datadma;
945 	struct osiop_tinfo *ti;
946 	int target = xs->xs_periph->periph_target;
947 	int lun = xs->xs_periph->periph_lun;
948 	int disconnect, i;
949 
950 #ifdef OSIOP_DEBUG
951 	if (osiop_debug & DEBUG_DISC &&
952 	    osiop_read_1(sc, OSIOP_SBCL) & OSIOP_BSY) {
953 		printf("ACK! osiop was busy: script %p dsa %p active %d\n",
954 		    sc->sc_script, acb->ds, sc->sc_active);
955 		printf("istat %02x sfbr %02x lcrc %02x sien %02x dien %02x\n",
956 		    osiop_read_1(sc, OSIOP_ISTAT),
957 		    osiop_read_1(sc, OSIOP_SFBR),
958 		    osiop_read_1(sc, OSIOP_LCRC),
959 		    osiop_read_1(sc, OSIOP_SIEN),
960 		    osiop_read_1(sc, OSIOP_DIEN));
961 #ifdef DDB
962 #if 0
963 		Debugger();
964 #endif
965 #endif
966 	}
967 #endif
968 
969 #ifdef OSIOP_DEBUG
970 	if (acb->status != ACB_S_READY)
971 		panic("osiop_start: non-ready cmd in acb");
972 #endif
973 
974 	acb->intstat = 0;
975 
976 	ds->cmd.count = acb->cmdlen;
977 	ds->cmd.addr = acb->cmddma->dm_segs[0].ds_addr;
978 
979 	ti = &sc->sc_tinfo[target];
980 	ds->scsi_addr = ((1 << 16) << target) | (ti->sxfer << 8);
981 
982 	disconnect = (xs->xs_control & XS_CTL_REQSENSE) == 0 &&
983 	    (ti->flags & TI_NODISC) == 0;
984 
985 	ds->msgout[0] = MSG_IDENTIFY(lun, disconnect);
986 	ds->id.count = 1;
987 	ds->stat[0] = SCSI_OSIOP_NOSTATUS;	/* set invalid status */
988 	ds->msgbuf[0] = ds->msgbuf[1] = MSG_INVALID;
989 	memset(&ds->data, 0, sizeof(ds->data));
990 
991 	/*
992 	 * Negotiate wide is the initial negotiation state;  since the 53c710
993 	 * doesn't do wide transfers, just begin the synchronous transfer
994 	 * negotiation here.
995 	 */
996 	if (ti->state == NEG_INIT) {
997 		if ((ti->flags & TI_NOSYNC) != 0) {
998 			ti->state = NEG_DONE;
999 			ti->sbcl = 0;
1000 			ti->sxfer = 0;
1001 			ti->period = 0;
1002 			ti->offset = 0;
1003 			osiop_update_xfer_mode(sc, target);
1004 #ifdef OSIOP_DEBUG
1005 			if (osiopsync_debug)
1006 				printf("Forcing target %d asynchronous\n",
1007 				    target);
1008 #endif
1009 		} else {
1010 			ds->msgbuf[2] = MSG_INVALID;
1011 			ds->msgout[1] = MSG_EXTENDED;
1012 			ds->msgout[2] = MSG_EXT_SDTR_LEN;
1013 			ds->msgout[3] = MSG_EXT_SDTR;
1014 			ds->msgout[4] = sc->sc_minsync;
1015 			ds->msgout[5] = OSIOP_MAX_OFFSET;
1016 			ds->id.count = MSG_EXT_SDTR_LEN + 3;
1017 			ti->state = NEG_WAITS;
1018 #ifdef OSIOP_DEBUG
1019 			if (osiopsync_debug)
1020 				printf("Sending sync request to target %d\n",
1021 				    target);
1022 #endif
1023 		}
1024 	}
1025 
1026 	acb->curaddr = 0;
1027 	acb->curlen = 0;
1028 
1029 	/*
1030 	 * Build physical DMA addresses for scatter/gather I/O
1031 	 */
1032 	if (xs->xs_control & (XS_CTL_DATA_IN | XS_CTL_DATA_OUT)) {
1033 		for (i = 0; i < datadma->dm_nsegs; i++) {
1034 			ds->data[i].count = datadma->dm_segs[i].ds_len;
1035 			ds->data[i].addr  = datadma->dm_segs[i].ds_addr;
1036 		}
1037 	}
1038 
1039 	/* sync script data structure */
1040 	bus_dmamap_sync(sc->sc_dmat, dsdma,
1041 	    acb->dsoffset, sizeof(struct osiop_ds),
1042 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1043 
1044 	acb->status = ACB_S_ACTIVE;
1045 
1046 	/* handle timeout */
1047 	if ((xs->xs_control & XS_CTL_POLL) == 0) {
1048 		int timeout = mstohz(acb->xs->timeout);
1049 		/* start expire timer */
1050 		if (timeout == 0)
1051 			timeout = 1;
1052 		callout_reset(&xs->xs_callout, timeout,
1053 		    osiop_timeout, acb);
1054 	}
1055 #ifdef OSIOP_DEBUG
1056 	if (osiop_debug & DEBUG_DISC &&
1057 	    osiop_read_1(sc, OSIOP_SBCL) & OSIOP_BSY) {
1058 		printf("ACK! osiop was busy at start: "
1059 		    "script %p dsa %p active %d\n",
1060 		    sc->sc_script, acb->ds, sc->sc_active);
1061 #ifdef DDB
1062 #if 0
1063 		Debugger();
1064 #endif
1065 #endif
1066 	}
1067 #endif
1068 	if (TAILQ_EMPTY(&sc->nexus_list)) {
1069 		if (osiop_read_1(sc, OSIOP_ISTAT) & OSIOP_ISTAT_CON)
1070 			printf("%s: osiop_select while connected?\n",
1071 			    sc->sc_dev.dv_xname);
1072 		osiop_write_4(sc, OSIOP_TEMP, 0);
1073 		osiop_write_1(sc, OSIOP_SBCL, ti->sbcl);
1074 		osiop_write_4(sc, OSIOP_DSA,
1075 		    dsdma->dm_segs[0].ds_addr + acb->dsoffset);
1076 		osiop_write_4(sc, OSIOP_DSP,
1077 		    sc->sc_scrdma->dm_segs[0].ds_addr + Ent_scripts);
1078 		OSIOP_TRACE('s', 1, 0, 0);
1079 	} else {
1080 		if ((osiop_read_1(sc, OSIOP_ISTAT) & OSIOP_ISTAT_CON) == 0) {
1081 			osiop_write_1(sc, OSIOP_ISTAT, OSIOP_ISTAT_SIGP);
1082 			OSIOP_TRACE('s', 2, 0, 0);
1083 		} else {
1084 			OSIOP_TRACE('s', 3,
1085 			    osiop_read_1(sc, OSIOP_ISTAT), 0);
1086 		}
1087 	}
1088 #ifdef OSIOP_DEBUG
1089 	osiopstarts++;
1090 #endif
1091 }
1092 
1093 /*
1094  * Process a DMA or SCSI interrupt from the 53C710 SIOP
1095  */
1096 
1097 int
1098 osiop_checkintr(sc, istat, dstat, sstat0, status)
1099 	struct	osiop_softc *sc;
1100 	u_int8_t istat;
1101 	u_int8_t dstat;
1102 	u_int8_t sstat0;
1103 	int *status;
1104 {
1105 	struct osiop_acb *acb = sc->sc_nexus;
1106 	struct osiop_ds *ds = NULL;	/* XXX */
1107 	bus_dmamap_t dsdma = sc->sc_dsdma;
1108 	bus_addr_t scraddr = sc->sc_scrdma->dm_segs[0].ds_addr;
1109 	int target = 0;
1110 	int dfifo, dbc, intcode, sstat1;
1111 
1112 	dfifo = osiop_read_1(sc, OSIOP_DFIFO);
1113 	dbc = osiop_read_4(sc, OSIOP_DBC) & 0x00ffffff;
1114 	sstat1 = osiop_read_1(sc, OSIOP_SSTAT1);
1115 	osiop_write_1(sc, OSIOP_CTEST8,
1116 	    osiop_read_1(sc, OSIOP_CTEST8) | OSIOP_CTEST8_CLF);
1117 	while ((osiop_read_1(sc, OSIOP_CTEST1) & OSIOP_CTEST1_FMT) !=
1118 	    OSIOP_CTEST1_FMT)
1119 		;
1120 	osiop_write_1(sc, OSIOP_CTEST8,
1121 	    osiop_read_1(sc, OSIOP_CTEST8) & ~OSIOP_CTEST8_CLF);
1122 	intcode = osiop_read_4(sc, OSIOP_DSPS);
1123 #ifdef OSIOP_DEBUG
1124 	osiopints++;
1125 	if (osiop_read_4(sc, OSIOP_DSP) != 0 &&
1126 	    (osiop_read_4(sc, OSIOP_DSP) < scraddr ||
1127 	    osiop_read_4(sc, OSIOP_DSP) >= scraddr + sizeof(osiop_script))) {
1128 		printf("%s: dsp not within script dsp %x scripts %lx:%lx",
1129 		    sc->sc_dev.dv_xname,
1130 		    osiop_read_4(sc, OSIOP_DSP),
1131 		    scraddr, scraddr + sizeof(osiop_script));
1132 		printf(" istat %x dstat %x sstat0 %x\n", istat, dstat, sstat0);
1133 #ifdef DDB
1134 		Debugger();
1135 #endif
1136 	}
1137 #endif
1138 	OSIOP_TRACE('i', dstat, istat, (istat & OSIOP_ISTAT_DIP) ?
1139 	    intcode & 0xff : sstat0);
1140 
1141 	if (acb != NULL) { /* XXX */
1142 		ds = acb->ds;
1143 		bus_dmamap_sync(sc->sc_dmat, dsdma,
1144 		    acb->dsoffset, sizeof(struct osiop_ds),
1145 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1146 #ifdef OSIOP_DEBUG
1147 		if (acb->status != ACB_S_ACTIVE)
1148 			printf("osiop_checkintr: acb not active (status %d)\n",
1149 			    acb->status);
1150 #endif
1151 	}
1152 
1153 
1154 	if (dstat & OSIOP_DSTAT_SIR && intcode == A_ok) {
1155 		/* Normal completion status, or check condition */
1156 		struct osiop_tinfo *ti;
1157 #ifdef OSIOP_DEBUG
1158 		if (osiop_read_4(sc, OSIOP_DSA) !=
1159 		    dsdma->dm_segs[0].ds_addr + acb->dsoffset) {
1160 			printf("osiop: invalid dsa: %x %lx\n",
1161 			    osiop_read_4(sc, OSIOP_DSA),
1162 			    dsdma->dm_segs[0].ds_addr + acb->dsoffset);
1163 			panic("*** osiop DSA invalid ***");
1164 		}
1165 #endif
1166 		target = acb->xs->xs_periph->periph_target;
1167 		ti = &sc->sc_tinfo[target];
1168 		if (ti->state == NEG_WAITS) {
1169 			if (ds->msgbuf[1] == MSG_INVALID)
1170 				printf("%s: target %d ignored sync request\n",
1171 				    sc->sc_dev.dv_xname, target);
1172 			else if (ds->msgbuf[1] == MSG_MESSAGE_REJECT)
1173 				printf("%s: target %d rejected sync request\n",
1174 				    sc->sc_dev.dv_xname, target);
1175 			ti->period = 0;
1176 			ti->offset = 0;
1177 			osiop_update_xfer_mode(sc, target);
1178 			ti->state = NEG_DONE;
1179 		}
1180 #ifdef OSIOP_DEBUG
1181 		if (osiop_read_1(sc, OSIOP_SBCL) & OSIOP_BSY) {
1182 #if 0
1183 			printf("ACK! osiop was busy at end: "
1184 			    "script %p dsa %p\n", &osiop_script, ds);
1185 #ifdef DDB
1186 			Debugger();
1187 #endif
1188 #endif
1189 		}
1190 		if (ds->msgbuf[0] != MSG_CMDCOMPLETE)
1191 			printf("%s: message was not COMMAND COMPLETE: %02x\n",
1192 			    sc->sc_dev.dv_xname, ds->msgbuf[0]);
1193 #endif
1194 		if (!TAILQ_EMPTY(&sc->nexus_list))
1195 			osiop_write_1(sc, OSIOP_DCNTL,
1196 			    osiop_read_1(sc, OSIOP_DCNTL) | OSIOP_DCNTL_STD);
1197 		*status = ds->stat[0];
1198 		acb->status = ACB_S_DONE;
1199 		return (1);
1200 	}
1201 	if (dstat & OSIOP_DSTAT_SIR && intcode == A_int_syncmsg) {
1202 		target = acb->xs->xs_periph->periph_target;
1203 		if (ds->msgbuf[1] == MSG_EXTENDED &&
1204 		    ds->msgbuf[2] == MSG_EXT_SDTR_LEN &&
1205 		    ds->msgbuf[3] == MSG_EXT_SDTR) {
1206 			struct osiop_tinfo *ti = &sc->sc_tinfo[target];
1207 #ifdef OSIOP_DEBUG
1208 			if (osiopsync_debug)
1209 				printf("sync msg in: "
1210 				    "%02x %02x %02x %02x %02x %02x\n",
1211 				    ds->msgbuf[0], ds->msgbuf[1],
1212 				    ds->msgbuf[2], ds->msgbuf[3],
1213 				    ds->msgbuf[4], ds->msgbuf[5]);
1214 #endif
1215 			ti->period = ds->msgbuf[4];
1216 			ti->offset = ds->msgbuf[5];
1217 			ti->sxfer = 0;
1218 			ti->sbcl = 0;
1219 			if (ds->msgbuf[5] != 0)
1220 				scsi_period_to_osiop(sc, target);
1221 			osiop_update_xfer_mode(sc, target);
1222 
1223 			bus_dmamap_sync(sc->sc_dmat, dsdma,
1224 			    acb->dsoffset, sizeof(struct osiop_ds),
1225 			    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1226 			osiop_write_1(sc, OSIOP_SXFER, ti->sxfer);
1227 			osiop_write_1(sc, OSIOP_SBCL, ti->sbcl);
1228 			if (ti->state == NEG_WAITS) {
1229 				ti->state = NEG_DONE;
1230 				osiop_write_4(sc, OSIOP_DSP,
1231 				    scraddr + Ent_clear_ack);
1232 				return (0);
1233 			}
1234 			osiop_write_1(sc, OSIOP_DCNTL,
1235 			    osiop_read_1(sc, OSIOP_DCNTL) | OSIOP_DCNTL_STD);
1236 			ti->state = NEG_DONE;
1237 			return (0);
1238 		}
1239 		/* XXX - not SDTR message */
1240 	}
1241 	if (sstat0 & OSIOP_SSTAT0_M_A) {
1242 		/* Phase mismatch */
1243 #ifdef OSIOP_DEBUG
1244 		osiopphmm++;
1245 		if (acb == NULL)
1246 			printf("%s: Phase mismatch with no active command?\n",
1247 			    sc->sc_dev.dv_xname);
1248 #endif
1249 		if (acb->datalen > 0) {
1250 			int adjust = (dfifo - (dbc & 0x7f)) & 0x7f;
1251 			if (sstat1 & OSIOP_SSTAT1_ORF)
1252 				adjust++;
1253 			if (sstat1 & OSIOP_SSTAT1_OLF)
1254 				adjust++;
1255 			acb->curaddr = osiop_read_4(sc, OSIOP_DNAD) - adjust;
1256 			acb->curlen = dbc + adjust;
1257 #ifdef OSIOP_DEBUG
1258 			if (osiop_debug & DEBUG_DISC) {
1259 				printf("Phase mismatch: curaddr %lx "
1260 				    "curlen %lx dfifo %x dbc %x sstat1 %x "
1261 				    "adjust %x sbcl %x starts %d acb %p\n",
1262 				    acb->curaddr, acb->curlen, dfifo,
1263 				    dbc, sstat1, adjust,
1264 				    osiop_read_1(sc, OSIOP_SBCL),
1265 				    osiopstarts, acb);
1266 				if (ds->data[1].count != 0) {
1267 					int i;
1268 					for (i = 0; ds->data[i].count != 0; i++)
1269 						printf("chain[%d] "
1270 						    "addr %x len %x\n", i,
1271 						    ds->data[i].addr,
1272 						    ds->data[i].count);
1273 				}
1274 				bus_dmamap_sync(sc->sc_dmat, dsdma,
1275 				    acb->dsoffset, sizeof(struct osiop_ds),
1276 				    BUS_DMASYNC_PREREAD |
1277 				    BUS_DMASYNC_PREWRITE);
1278 			}
1279 #endif
1280 		}
1281 #ifdef OSIOP_DEBUG
1282 		OSIOP_TRACE('m', osiop_read_1(sc, OSIOP_SBCL),
1283 		    osiop_read_4(sc, OSIOP_DSP) >> 8,
1284 		    osiop_read_4(sc, OSIOP_DSP));
1285 		if (osiop_debug & DEBUG_PHASE)
1286 			printf("Phase mismatch: %x dsp +%lx dcmd %x\n",
1287 			    osiop_read_1(sc, OSIOP_SBCL),
1288 			    osiop_read_4(sc, OSIOP_DSP) - scraddr,
1289 			    osiop_read_4(sc, OSIOP_DBC));
1290 #endif
1291 		if ((osiop_read_1(sc, OSIOP_SBCL) & OSIOP_REQ) == 0) {
1292 			printf("Phase mismatch: "
1293 			    "REQ not asserted! %02x dsp %x\n",
1294 			    osiop_read_1(sc, OSIOP_SBCL),
1295 			    osiop_read_4(sc, OSIOP_DSP));
1296 #if defined(OSIOP_DEBUG) && defined(DDB)
1297 			/*Debugger(); XXX is*/
1298 #endif
1299 		}
1300 		switch (OSIOP_PHASE(osiop_read_1(sc, OSIOP_SBCL))) {
1301 		case DATA_OUT_PHASE:
1302 		case DATA_IN_PHASE:
1303 		case STATUS_PHASE:
1304 		case COMMAND_PHASE:
1305 		case MSG_IN_PHASE:
1306 		case MSG_OUT_PHASE:
1307 			osiop_write_4(sc, OSIOP_DSP, scraddr + Ent_switch);
1308 			break;
1309 		default:
1310 			printf("%s: invalid phase\n", sc->sc_dev.dv_xname);
1311 			goto bad_phase;
1312 		}
1313 		return (0);
1314 	}
1315 	if (sstat0 & OSIOP_SSTAT0_STO) {
1316 		/* Select timed out */
1317 #ifdef OSIOP_DEBUG
1318 		if (acb == NULL)
1319 			printf("%s: Select timeout with no active command?\n",
1320 			    sc->sc_dev.dv_xname);
1321 		if (osiop_read_1(sc, OSIOP_SBCL) & OSIOP_BSY) {
1322 			printf("ACK! osiop was busy at timeout: "
1323 			    "script %p dsa %lx\n", sc->sc_script,
1324 			    dsdma->dm_segs[0].ds_addr + acb->dsoffset);
1325 			printf(" sbcl %x sdid %x "
1326 			    "istat %x dstat %x sstat0 %x\n",
1327 			    osiop_read_1(sc, OSIOP_SBCL),
1328 			    osiop_read_1(sc, OSIOP_SDID),
1329 			    istat, dstat, sstat0);
1330 			if ((osiop_read_1(sc, OSIOP_SBCL) & OSIOP_BSY) == 0) {
1331 				printf("Yikes, it's not busy now!\n");
1332 #if 0
1333 				*status = SCSI_OSIOP_NOSTATUS;
1334 				if (!TAILQ_EMPTY(&sc->nexus_list))
1335 					osiop_write_4(sc, OSIOP_DSP,
1336 					    scraddr + Ent_wait_reselect);
1337 				return (1);
1338 #endif
1339 			}
1340 #if 0
1341 			osiop_write_1(sc, OSIOP_DCNTL,
1342 			    osiop_read_1(sc, OSIOP_DCNTL) | OSIOP_DCNTL_STD);
1343 #endif
1344 #ifdef DDB
1345 			Debugger();
1346 #endif
1347 			return (0);
1348 		}
1349 #endif
1350 		acb->status = ACB_S_DONE;
1351 		*status = SCSI_OSIOP_NOSTATUS;
1352 		acb->xs->error = XS_SELTIMEOUT;
1353 		if (!TAILQ_EMPTY(&sc->nexus_list))
1354 			osiop_write_4(sc, OSIOP_DSP,
1355 			    scraddr + Ent_wait_reselect);
1356 		return (1);
1357 	}
1358 	if (acb != NULL)
1359 		target = acb->xs->xs_periph->periph_target;
1360 	else
1361 		target = sc->sc_id;
1362 	if (sstat0 & OSIOP_SSTAT0_UDC) {
1363 #ifdef OSIOP_DEBUG
1364 		if (acb == NULL)
1365 			printf("%s: Unexpected disconnect "
1366 			    "with no active command?\n", sc->sc_dev.dv_xname);
1367 		printf("%s: target %d disconnected unexpectedly\n",
1368 		    sc->sc_dev.dv_xname, target);
1369 #endif
1370 #if 0
1371 		osiop_abort(sc, "osiop_chkintr");
1372 #endif
1373 		*status = SCSI_CHECK;
1374 		if (!TAILQ_EMPTY(&sc->nexus_list))
1375 			osiop_write_4(sc, OSIOP_DSP,
1376 			    scraddr + Ent_wait_reselect);
1377 		return (acb != NULL);
1378 	}
1379 	if (dstat & OSIOP_DSTAT_SIR &&
1380 	    (intcode == A_int_disc || intcode == A_int_disc_wodp)) {
1381 		/* Disconnect */
1382 		if (acb == NULL) {
1383 			printf("%s: Disconnect with no active command?\n",
1384 			    sc->sc_dev.dv_xname);
1385 			return (0);
1386 		}
1387 #ifdef OSIOP_DEBUG
1388 		if (osiop_debug & DEBUG_DISC) {
1389 			printf("%s: ID %02x disconnected TEMP %x (+%lx) "
1390 			    "curaddr %lx curlen %lx buf %x len %x dfifo %x "
1391 			    "dbc %x sstat1 %x starts %d acb %p\n",
1392 			    sc->sc_dev.dv_xname, 1 << target,
1393 			    osiop_read_4(sc, OSIOP_TEMP),
1394 			    (osiop_read_4(sc, OSIOP_TEMP) != 0) ?
1395 			        osiop_read_4(sc, OSIOP_TEMP) - scraddr : 0,
1396 			    acb->curaddr, acb->curlen,
1397 			    ds->data[0].addr, ds->data[0].count,
1398 			    dfifo, dbc, sstat1, osiopstarts, acb);
1399 			bus_dmamap_sync(sc->sc_dmat, dsdma,
1400 			    acb->dsoffset, sizeof(struct osiop_ds),
1401 			    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1402 		}
1403 #endif
1404 		/*
1405 		 * XXXX need to update curaddr/curlen to reflect
1406 		 * current data transferred.  If device disconnected in
1407 		 * the middle of a DMA block, they should already be set
1408 		 * by the phase change interrupt.  If the disconnect
1409 		 * occurs on a DMA block boundary, we have to figure out
1410 		 * which DMA block it was.
1411 		 */
1412 		if (acb->datalen > 0 &&
1413 		    osiop_read_4(sc, OSIOP_TEMP) != 0) {
1414 			long n = osiop_read_4(sc, OSIOP_TEMP) - scraddr;
1415 
1416 			if (acb->curlen != 0 &&
1417 			    acb->curlen != ds->data[0].count)
1418 				printf("%s: curaddr/curlen already set? "
1419 				    "n %lx iob %lx/%lx chain[0] %x/%x\n",
1420 				    sc->sc_dev.dv_xname, n,
1421 				    acb->curaddr, acb->curlen,
1422 				    ds->data[0].addr, ds->data[0].count);
1423 			if (n < Ent_datain)
1424 				n = (n - Ent_dataout) / 16;
1425 			else
1426 				n = (n - Ent_datain) / 16;
1427 			if (n < 0 || n >= OSIOP_NSG)
1428 				printf("TEMP invalid %ld\n", n);
1429 			else {
1430 				acb->curaddr = ds->data[n].addr;
1431 				acb->curlen = ds->data[n].count;
1432 			}
1433 #ifdef OSIOP_DEBUG
1434 			if (osiop_debug & DEBUG_DISC) {
1435 				printf("%s: TEMP offset %ld",
1436 				    sc->sc_dev.dv_xname, n);
1437 				printf(" curaddr %lx curlen %lx\n",
1438 				    acb->curaddr, acb->curlen);
1439 			}
1440 #endif
1441 		}
1442 		/*
1443 		 * If data transfer was interrupted by disconnect, curaddr
1444 		 * and curlen should reflect the point of interruption.
1445 		 * Adjust the DMA chain so that the data transfer begins
1446 		 * at the appropriate place upon reselection.
1447 		 * XXX This should only be done on save data pointer message?
1448 		 */
1449 		if (acb->curlen > 0) {
1450 			int i, j;
1451 
1452 #ifdef OSIOP_DEBUG
1453 			if (osiop_debug & DEBUG_DISC)
1454 				printf("%s: adjusting DMA chain\n",
1455 				    sc->sc_dev.dv_xname);
1456 			if (intcode == A_int_disc_wodp)
1457 				printf("%s: ID %02x disconnected "
1458 				    "without Save Data Pointers\n",
1459 				    sc->sc_dev.dv_xname, 1 << target);
1460 #endif
1461 			for (i = 0; i < OSIOP_NSG; i++) {
1462 				if (ds->data[i].count == 0)
1463 					break;
1464 				if (acb->curaddr >= ds->data[i].addr &&
1465 				    acb->curaddr <
1466 				    (ds->data[i].addr + ds->data[i].count))
1467 					break;
1468 			}
1469 			if (i >= OSIOP_NSG || ds->data[i].count == 0) {
1470 				printf("couldn't find saved data pointer: "
1471 				    "curaddr %lx curlen %lx i %d\n",
1472 				    acb->curaddr, acb->curlen, i);
1473 #ifdef DDB
1474 				Debugger();
1475 #endif
1476 			}
1477 #ifdef OSIOP_DEBUG
1478 			if (osiop_debug & DEBUG_DISC)
1479 				printf(" chain[0]: %x/%x -> %lx/%lx\n",
1480 				    ds->data[0].addr, ds->data[0].count,
1481 				    acb->curaddr, acb->curlen);
1482 #endif
1483 			ds->data[0].addr = acb->curaddr;
1484 			ds->data[0].count = acb->curlen;
1485 			for (j = 1, i = i + 1;
1486 			    i < OSIOP_NSG && ds->data[i].count > 0;
1487 			    i++, j++) {
1488 #ifdef OSIOP_DEBUG
1489 				if (osiop_debug & DEBUG_DISC)
1490 					printf("  chain[%d]: %x/%x -> %x/%x\n",
1491 					    j, ds->data[j].addr,
1492 					    ds->data[j].count,
1493 					    ds->data[i].addr,
1494 					    ds->data[i].count);
1495 #endif
1496 				ds->data[j].addr  = ds->data[i].addr;
1497 				ds->data[j].count = ds->data[i].count;
1498 			}
1499 			if (j < OSIOP_NSG) {
1500 				ds->data[j].addr  = 0;
1501 				ds->data[j].count = 0;
1502 			}
1503 			bus_dmamap_sync(sc->sc_dmat, dsdma,
1504 			    acb->dsoffset, sizeof(struct osiop_ds),
1505 			    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1506 		}
1507 		sc->sc_tinfo[target].dconns++;
1508 		/*
1509 		 * add nexus to waiting list
1510 		 * clear nexus
1511 		 * try to start another command for another target/lun
1512 		 */
1513 		acb->intstat = sc->sc_flags & OSIOP_INTSOFF;
1514 		TAILQ_INSERT_TAIL(&sc->nexus_list, acb, chain);
1515 		sc->sc_nexus = NULL;		/* no current device */
1516 		osiop_write_4(sc, OSIOP_DSP, scraddr + Ent_wait_reselect);
1517 		/* XXXX start another command ? */
1518 		if (!TAILQ_EMPTY(&sc->ready_list))
1519 			osiop_sched(sc);
1520 		return (0);
1521 	}
1522 	if (dstat & OSIOP_DSTAT_SIR && intcode == A_int_reconnect) {
1523 		int reselid = ffs(osiop_read_4(sc, OSIOP_SCRATCH) & 0xff) - 1;
1524 		int reselun = osiop_read_1(sc, OSIOP_SFBR) & 0x07;
1525 #ifdef OSIOP_DEBUG
1526 		u_int8_t resmsg;
1527 #endif
1528 
1529 		/* Reconnect */
1530 		/* XXXX save current SBCL */
1531 		sc->sc_sstat1 = osiop_read_1(sc, OSIOP_SBCL);
1532 #ifdef OSIOP_DEBUG
1533 		if (osiop_debug & DEBUG_DISC)
1534 			printf("%s: target ID %02x reselected dsps %x\n",
1535 			    sc->sc_dev.dv_xname, reselid, intcode);
1536 		resmsg = osiop_read_1(sc, OSIOP_SFBR);
1537 		if (!MSG_ISIDENTIFY(resmsg))
1538 			printf("%s: Reselect message in was not identify: "
1539 			    "%02x\n", sc->sc_dev.dv_xname, resmsg);
1540 #endif
1541 		if (sc->sc_nexus != NULL) {
1542 			struct scsipi_periph *periph =
1543 			    sc->sc_nexus->xs->xs_periph;
1544 #ifdef OSIOP_DEBUG
1545 			if (osiop_debug & DEBUG_DISC)
1546 				printf("%s: reselect ID %02x w/active\n",
1547 				    sc->sc_dev.dv_xname, reselid);
1548 #endif
1549 			TAILQ_INSERT_HEAD(&sc->ready_list,
1550 			    sc->sc_nexus, chain);
1551 			sc->sc_tinfo[periph->periph_target].lubusy
1552 			    &= ~(1 << periph->periph_lun);
1553 			sc->sc_active--;
1554 		}
1555 		/*
1556 		 * locate acb of reselecting device
1557 		 * set sc->sc_nexus to acb
1558 		 */
1559 		TAILQ_FOREACH(acb, &sc->nexus_list, chain) {
1560 			struct scsipi_periph *periph = acb->xs->xs_periph;
1561 			if (reselid != periph->periph_target ||
1562 			    reselun != periph->periph_lun) {
1563 				continue;
1564 			}
1565 			TAILQ_REMOVE(&sc->nexus_list, acb, chain);
1566 			sc->sc_nexus = acb;
1567 			sc->sc_flags |= acb->intstat;
1568 			acb->intstat = 0;
1569 			osiop_write_4(sc, OSIOP_DSA,
1570 			    dsdma->dm_segs[0].ds_addr + acb->dsoffset);
1571 			osiop_write_1(sc, OSIOP_SXFER,
1572 			    sc->sc_tinfo[reselid].sxfer);
1573 			osiop_write_1(sc, OSIOP_SBCL,
1574 			    sc->sc_tinfo[reselid].sbcl);
1575 			break;
1576 		}
1577 		if (acb == NULL) {
1578 			printf("%s: target ID %02x reselect nexus_list %p\n",
1579 			    sc->sc_dev.dv_xname, reselid,
1580 			    TAILQ_FIRST(&sc->nexus_list));
1581 			panic("unable to find reselecting device");
1582 		}
1583 
1584 		osiop_write_4(sc, OSIOP_TEMP, 0);
1585 		osiop_write_1(sc, OSIOP_DCNTL,
1586 		    osiop_read_1(sc, OSIOP_DCNTL) | OSIOP_DCNTL_STD);
1587 		return (0);
1588 	}
1589 	if (dstat & OSIOP_DSTAT_SIR && intcode == A_int_connect) {
1590 #ifdef OSIOP_DEBUG
1591 		u_int8_t ctest2 = osiop_read_1(sc, OSIOP_CTEST2);
1592 
1593 		/* reselect was interrupted (by Sig_P or select) */
1594 		if (osiop_debug & DEBUG_DISC ||
1595 		    (ctest2 & OSIOP_CTEST2_SIGP) == 0)
1596 			printf("%s: reselect interrupted (Sig_P?) "
1597 			    "scntl1 %x ctest2 %x sfbr %x istat %x/%x\n",
1598 			    sc->sc_dev.dv_xname,
1599 			    osiop_read_1(sc, OSIOP_SCNTL1), ctest2,
1600 			    osiop_read_1(sc, OSIOP_SFBR), istat,
1601 			    osiop_read_1(sc, OSIOP_ISTAT));
1602 #endif
1603 		/* XXX assumes it was not select */
1604 		if (sc->sc_nexus == NULL) {
1605 #ifdef OSIOP_DEBUG
1606 			printf("%s: reselect interrupted, sc_nexus == NULL\n",
1607 			    sc->sc_dev.dv_xname);
1608 #if 0
1609 			osiop_dump(sc);
1610 #ifdef DDB
1611 			Debugger();
1612 #endif
1613 #endif
1614 #endif
1615 			osiop_write_1(sc, OSIOP_DCNTL,
1616 			    osiop_read_1(sc, OSIOP_DCNTL) | OSIOP_DCNTL_STD);
1617 			return (0);
1618 		}
1619 		target = sc->sc_nexus->xs->xs_periph->periph_target;
1620 		osiop_write_4(sc, OSIOP_TEMP, 0);
1621 		osiop_write_4(sc, OSIOP_DSA,
1622 		    dsdma->dm_segs[0].ds_addr + sc->sc_nexus->dsoffset);
1623 		osiop_write_1(sc, OSIOP_SXFER, sc->sc_tinfo[target].sxfer);
1624 		osiop_write_1(sc, OSIOP_SBCL, sc->sc_tinfo[target].sbcl);
1625 		osiop_write_4(sc, OSIOP_DSP, scraddr + Ent_scripts);
1626 		return (0);
1627 	}
1628 	if (dstat & OSIOP_DSTAT_SIR && intcode == A_int_msgin) {
1629 		/* Unrecognized message in byte */
1630 		if (acb == NULL)
1631 			printf("%s: Bad message-in with no active command?\n",
1632 			    sc->sc_dev.dv_xname);
1633 		printf("%s: Unrecognized message in data "
1634 		    "sfbr %x msg %x sbcl %x\n", sc->sc_dev.dv_xname,
1635 		    osiop_read_1(sc, OSIOP_SFBR), ds->msgbuf[1],
1636 		    osiop_read_1(sc, OSIOP_SBCL));
1637 		/* what should be done here? */
1638 		osiop_write_4(sc, OSIOP_DSP, scraddr + Ent_switch);
1639 		bus_dmamap_sync(sc->sc_dmat, dsdma,
1640 		    acb->dsoffset, sizeof(struct osiop_ds),
1641 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1642 		return (0);
1643 	}
1644 	if (dstat & OSIOP_DSTAT_SIR && intcode == A_int_status) {
1645 		/* Status phase wasn't followed by message in phase? */
1646 		printf("%s: Status phase not followed by message in phase? "
1647 		    "sbcl %x sbdl %x\n", sc->sc_dev.dv_xname,
1648 		    osiop_read_1(sc, OSIOP_SBCL),
1649 		    osiop_read_1(sc, OSIOP_SBDL));
1650 		if (osiop_read_1(sc, OSIOP_SBCL) == 0xa7) {
1651 			/* It is now, just continue the script? */
1652 			osiop_write_1(sc, OSIOP_DCNTL,
1653 			    osiop_read_1(sc, OSIOP_DCNTL) | OSIOP_DCNTL_STD);
1654 			return (0);
1655 		}
1656 	}
1657 	if (dstat & OSIOP_DSTAT_SIR && sstat0 == 0) {
1658 		printf("OSIOP interrupt: %x sts %x msg %x %x sbcl %x\n",
1659 		    intcode, ds->stat[0], ds->msgbuf[0], ds->msgbuf[1],
1660 		    osiop_read_1(sc, OSIOP_SBCL));
1661 		osiop_reset(sc);
1662 		*status = SCSI_OSIOP_NOSTATUS;
1663 		return (0);	/* osiop_reset has cleaned up */
1664 	}
1665 	if (sstat0 & OSIOP_SSTAT0_SGE)
1666 		printf("%s: SCSI Gross Error\n", sc->sc_dev.dv_xname);
1667 	if (sstat0 & OSIOP_SSTAT0_PAR)
1668 		printf("%s: Parity Error\n", sc->sc_dev.dv_xname);
1669 	if (dstat & OSIOP_DSTAT_IID)
1670 		printf("%s: Invalid instruction detected\n",
1671 		    sc->sc_dev.dv_xname);
1672  bad_phase:
1673 	/*
1674 	 * temporary panic for unhandled conditions
1675 	 * displays various things about the 53C710 status and registers
1676 	 * then panics.
1677 	 * XXXX need to clean this up to print out the info, reset, and continue
1678 	 */
1679 	printf("osiop_chkintr: target %x ds %p\n", target, ds);
1680 	printf("scripts %lx ds %lx dsp %x dcmd %x\n", scraddr,
1681 	    sc->sc_dsdma->dm_segs[0].ds_addr + acb->dsoffset,
1682 	    osiop_read_4(sc, OSIOP_DSP),
1683 	    osiop_read_4(sc, OSIOP_DBC));
1684 	printf("osiop_chkintr: istat %x dstat %x sstat0 %x "
1685 	    "dsps %x dsa %x sbcl %x sts %x msg %x %x sfbr %x\n",
1686 	    istat, dstat, sstat0, intcode,
1687 	    osiop_read_4(sc, OSIOP_DSA),
1688 	    osiop_read_1(sc, OSIOP_SBCL),
1689 	    ds->stat[0], ds->msgbuf[0], ds->msgbuf[1],
1690 	    osiop_read_1(sc, OSIOP_SFBR));
1691 #ifdef OSIOP_DEBUG
1692 	if (osiop_debug & DEBUG_DMA)
1693 		panic("osiop_chkintr: **** temp ****");
1694 #endif
1695 #ifdef DDB
1696 	Debugger();
1697 #endif
1698 	osiop_reset(sc);	/* hard reset */
1699 	*status = SCSI_OSIOP_NOSTATUS;
1700 	acb->status = ACB_S_DONE;
1701 	return (0);		/* osiop_reset cleaned up */
1702 }
1703 
1704 void
1705 osiop_select(sc)
1706 	struct osiop_softc *sc;
1707 {
1708 	struct osiop_acb *acb = sc->sc_nexus;
1709 
1710 #ifdef OSIOP_DEBUG
1711 	if (osiop_debug & DEBUG_CMD)
1712 		printf("%s: select ", sc->sc_dev.dv_xname);
1713 #endif
1714 
1715 	if (acb->xs->xs_control & XS_CTL_POLL || sc->sc_flags & OSIOP_NODMA) {
1716 		sc->sc_flags |= OSIOP_INTSOFF;
1717 		sc->sc_flags &= ~OSIOP_INTDEFER;
1718 		if ((osiop_read_1(sc, OSIOP_ISTAT) & OSIOP_ISTAT_CON) == 0) {
1719 			osiop_write_1(sc, OSIOP_SIEN, 0);
1720 			osiop_write_1(sc, OSIOP_DIEN, 0);
1721 		}
1722 #if 0
1723 	} else if ((sc->sc_flags & OSIOP_INTDEFER) == 0) {
1724 		sc->sc_flags &= ~OSIOP_INTSOFF;
1725 		if ((osiop_read_1(sc, OSIOP_ISTAT) & OSIOP_ISTAT_CON) == 0) {
1726 			osiop_write_1(sc, OSIOP_SIEN, sc->sc_sien);
1727 			osiop_write_1(sc, OSIOP_DIEN, sc->sc_dien);
1728 		}
1729 #endif
1730 	}
1731 #ifdef OSIOP_DEBUG
1732 	if (osiop_debug & DEBUG_CMD)
1733 		printf("osiop_select: target %x cmd %02x ds %p\n",
1734 		    acb->xs->xs_periph->periph_target,
1735 		    acb->xs->cmd->opcode, sc->sc_nexus->ds);
1736 #endif
1737 
1738 	osiop_start(sc);
1739 
1740 	return;
1741 }
1742 
1743 /*
1744  * 53C710 interrupt handler
1745  */
1746 
1747 void
1748 osiop_intr(sc)
1749 	struct osiop_softc *sc;
1750 {
1751 	int status, s;
1752 	u_int8_t istat, dstat, sstat0;
1753 
1754 	s = splbio();
1755 
1756 	istat = sc->sc_istat;
1757 	if ((istat & (OSIOP_ISTAT_SIP | OSIOP_ISTAT_DIP)) == 0) {
1758 		splx(s);
1759 		return;
1760 	}
1761 
1762 	/* Got a valid interrupt on this device; set by MD handler */
1763 	dstat = sc->sc_dstat;
1764 	sstat0 = sc->sc_sstat0;
1765 	sc->sc_istat = 0;
1766 #ifdef OSIOP_DEBUG
1767 	if (!sc->sc_active) {
1768 		/* XXX needs sync */
1769 		printf("%s: spurious interrupt? "
1770 		    "istat %x dstat %x sstat0 %x nexus %p status %x\n",
1771 		    sc->sc_dev.dv_xname, istat, dstat, sstat0, sc->sc_nexus,
1772 		    (sc->sc_nexus != NULL) ? sc->sc_nexus->ds->stat[0] : 0);
1773 	}
1774 #endif
1775 
1776 #ifdef OSIOP_DEBUG
1777 	if (osiop_debug & (DEBUG_INT|DEBUG_CMD)) {
1778 		/* XXX needs sync */
1779 		printf("%s: intr istat %x dstat %x sstat0 %x dsps %x "
1780 		    "sbcl %x dsp %x dcmd %x sts %x msg %x\n",
1781 		    sc->sc_dev.dv_xname,
1782 		    istat, dstat, sstat0,
1783 		    osiop_read_4(sc, OSIOP_DSPS),
1784 		    osiop_read_1(sc, OSIOP_SBCL),
1785 		    osiop_read_4(sc, OSIOP_DSP),
1786 		    osiop_read_4(sc, OSIOP_DBC),
1787 		    (sc->sc_nexus != NULL) ? sc->sc_nexus->ds->stat[0] : 0,
1788 		    (sc->sc_nexus != NULL) ? sc->sc_nexus->ds->msgbuf[0] : 0);
1789 	}
1790 #endif
1791 	if (sc->sc_flags & OSIOP_INTDEFER) {
1792 		sc->sc_flags &= ~(OSIOP_INTDEFER | OSIOP_INTSOFF);
1793 		osiop_write_1(sc, OSIOP_SIEN, sc->sc_sien);
1794 		osiop_write_1(sc, OSIOP_DIEN, sc->sc_dien);
1795 	}
1796 	if (osiop_checkintr(sc, istat, dstat, sstat0, &status)) {
1797 #if 0
1798 		if (status == SCSI_OSIOP_NOSTATUS)
1799 			printf("osiop_intr: no valid status \n");
1800 #endif
1801 		if ((sc->sc_flags & (OSIOP_INTSOFF | OSIOP_INTDEFER)) !=
1802 		    OSIOP_INTSOFF) {
1803 #if 0
1804 			if (osiop_read_1(sc, OSIOP_SBCL) & OSIOP_BSY) {
1805 				struct scsipi_periph *periph;
1806 
1807 				periph = sc->sc_nexus->xs->xs_periph;
1808 				printf("%s: SCSI bus busy at completion"
1809 				    " targ %d sbcl %02x sfbr %x lcrc "
1810 				    "%02x dsp +%x\n", sc->sc_dev.dv_xname,
1811 				    periph->periphtarget,
1812 				    osiop_read_1(sc, OSIOP_SBCL),
1813 				    osiop_read_1(sc, OSIOP_SFBR),
1814 				    osiop_read_1(sc, OSIOP_LCRC),
1815 				    osiop_read_4(sc, OSIOP_DSP) -
1816 				        sc->sc_scrdma->dm_segs[0].ds_addr);
1817 			}
1818 #endif
1819 			osiop_scsidone(sc->sc_nexus, status);
1820 		}
1821 	}
1822 	splx(s);
1823 }
1824 
1825 void
1826 osiop_update_xfer_mode(sc, target)
1827 	struct osiop_softc *sc;
1828 	int target;
1829 {
1830 	struct osiop_tinfo *tinfo = &sc->sc_tinfo[target];
1831 	struct scsipi_xfer_mode xm;
1832 
1833 	xm.xm_target = target;
1834 	xm.xm_mode = 0;
1835 	xm.xm_period = 0;
1836 	xm.xm_offset = 0;
1837 
1838 	if (tinfo->period) {
1839 		xm.xm_mode |= PERIPH_CAP_SYNC;
1840 		xm.xm_period = tinfo->period;
1841 		xm.xm_offset = tinfo->offset;
1842 	}
1843 
1844 	scsipi_async_event(&sc->sc_channel, ASYNC_EVENT_XFER_MODE, &xm);
1845 }
1846 
1847 void
1848 scsi_period_to_osiop(sc, target)
1849 	struct osiop_softc *sc;
1850 	int target;
1851 {
1852 	int period, offset, sxfer, sbcl;
1853 
1854 	period = sc->sc_tinfo[target].period;
1855 	offset = sc->sc_tinfo[target].offset;
1856 	for (sbcl = 1; sbcl < 4; sbcl++) {
1857 		sxfer = (period * 4 - 1) / sc->sc_tcp[sbcl] - 3;
1858 		if (sxfer >= 0 && sxfer <= 7)
1859 			break;
1860 	}
1861 	if (sbcl > 3) {
1862 		printf("osiop sync: unable to compute sync params "
1863 		    "for period %d ns\n", period * 4);
1864 		/*
1865 		 * XXX need to pick a value we can do and renegotiate
1866 		 */
1867 		sxfer = sbcl = 0;
1868 	} else {
1869 		sxfer = (sxfer << 4) | ((offset <= OSIOP_MAX_OFFSET) ?
1870 		    offset : OSIOP_MAX_OFFSET);
1871 #ifdef DEBUG_SYNC
1872 		printf("osiop sync: params for period %dns: sxfer %x sbcl %x",
1873 		    period * 4, sxfer, sbcl);
1874 		printf(" actual period %dns\n",
1875 		    sc->sc_tcp[sbcl] * ((sxfer >> 4) + 4));
1876 #endif
1877 	}
1878 	sc->sc_tinfo[target].sxfer = sxfer;
1879 	sc->sc_tinfo[target].sbcl = sbcl;
1880 #ifdef DEBUG_SYNC
1881 	printf("osiop sync: osiop_sxfr %02x, osiop_sbcl %02x\n", sxfer, sbcl);
1882 #endif
1883 }
1884 
1885 void
1886 osiop_timeout(arg)
1887 	void *arg;
1888 {
1889 	struct osiop_acb *acb = arg;
1890 	struct scsipi_xfer *xs = acb->xs;
1891 	struct osiop_softc *sc = acb->sc;
1892 	int s;
1893 
1894 	scsipi_printaddr(xs->xs_periph);
1895 	printf("command timeout\n");
1896 
1897 	s = splbio();
1898 	/* reset the scsi bus */
1899 	osiop_resetbus(sc);
1900 
1901 	/* deactivate callout */
1902 	callout_stop(&xs->xs_callout);
1903 	acb->flags |= ACB_F_TIMEOUT;
1904 	osiop_reset(sc);
1905 	splx(s);
1906 	return;
1907 }
1908 
1909 #ifdef OSIOP_DEBUG
1910 
1911 #if OSIOP_TRACE_SIZE
1912 void
1913 osiop_dump_trace()
1914 {
1915 	int i;
1916 
1917 	printf("osiop trace: next index %d\n", osiop_trix);
1918 	i = osiop_trix;
1919 	do {
1920 		printf("%3d: '%c' %02x %02x %02x\n", i,
1921 		    osiop_trbuf[i], osiop_trbuf[i + 1],
1922 		    osiop_trbuf[i + 2], osiop_trbuf[i + 3]);
1923 		i = (i + 4) & (OSIOP_TRACE_SIZE - 1);
1924 	} while (i != osiop_trix);
1925 }
1926 #endif
1927 
1928 void
1929 osiop_dump_acb(acb)
1930 	struct osiop_acb *acb;
1931 {
1932 	u_int8_t *b;
1933 	int i;
1934 
1935 	printf("acb@%p ", acb);
1936 	if (acb->xs == NULL) {
1937 		printf("<unused>\n");
1938 		return;
1939 	}
1940 
1941 	b = (u_int8_t *)&acb->xs->cmd;
1942 	printf("(%d:%d) status %2x cmdlen %2ld cmd ",
1943 	    acb->xs->xs_periph->periph_target,
1944 	    acb->xs->xs_periph->periph_lun, acb->status, acb->cmdlen);
1945 	for (i = acb->cmdlen; i > 0; i--)
1946 		printf(" %02x", *b++);
1947 	printf("\n");
1948 	printf("  xs: %p data %p:%04x ", acb->xs, acb->xs->data,
1949 	    acb->xs->datalen);
1950 	printf("va %p:%lx ", acb->data, acb->datalen);
1951 	printf("cur %lx:%lx\n", acb->curaddr, acb->curlen);
1952 }
1953 
1954 void
1955 osiop_dump(sc)
1956 	struct osiop_softc *sc;
1957 {
1958 	struct osiop_acb *acb;
1959 	int i, s;
1960 
1961 	s = splbio();
1962 #if OSIOP_TRACE_SIZE
1963 	osiop_dump_trace();
1964 #endif
1965 	printf("%s@%p istat %02x\n",
1966 	    sc->sc_dev.dv_xname, sc, osiop_read_1(sc, OSIOP_ISTAT));
1967 	if ((acb = TAILQ_FIRST(&sc->free_list)) != NULL) {
1968 		printf("Free list:\n");
1969 		while (acb) {
1970 			osiop_dump_acb(acb);
1971 			acb = TAILQ_NEXT(acb, chain);
1972 		}
1973 	}
1974 	if ((acb = TAILQ_FIRST(&sc->ready_list)) != NULL) {
1975 		printf("Ready list:\n");
1976 		while (acb) {
1977 			osiop_dump_acb(acb);
1978 			acb = TAILQ_NEXT(acb, chain);
1979 		}
1980 	}
1981 	if ((acb = TAILQ_FIRST(&sc->nexus_list)) != NULL) {
1982 		printf("Nexus list:\n");
1983 		while (acb) {
1984 			osiop_dump_acb(acb);
1985 			acb = TAILQ_NEXT(acb, chain);
1986 		}
1987 	}
1988 	if (sc->sc_nexus) {
1989 		printf("Nexus:\n");
1990 		osiop_dump_acb(sc->sc_nexus);
1991 	}
1992 	for (i = 0; i < OSIOP_NTGT; i++) {
1993 		if (sc->sc_tinfo[i].cmds > 2) {
1994 			printf("tgt %d: cmds %d disc %d lubusy %x\n",
1995 			    i, sc->sc_tinfo[i].cmds,
1996 			    sc->sc_tinfo[i].dconns,
1997 			    sc->sc_tinfo[i].lubusy);
1998 		}
1999 	}
2000 	splx(s);
2001 }
2002 #endif
2003