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