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