xref: /netbsd-src/sys/arch/amiga/dev/sbic.c (revision 89c5a767f8fc7a4633b2d409966e2becbb98ff92)
1 /*	$NetBSD: sbic.c,v 1.38 2000/01/18 19:33:32 thorpej Exp $	*/
2 
3 /*
4  * Copyright (c) 1994 Christian E. Hopps
5  * Copyright (c) 1990 The Regents of the University of California.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * Van Jacobson of Lawrence Berkeley Laboratory.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *	This product includes software developed by the University of
22  *	California, Berkeley and its contributors.
23  * 4. Neither the name of the University nor the names of its contributors
24  *    may be used to endorse or promote products derived from this software
25  *    without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37  * SUCH DAMAGE.
38  *
39  *	@(#)scsi.c	7.5 (Berkeley) 5/4/91
40  */
41 
42 /*
43  * AMIGA AMD 33C93 scsi adaptor driver
44  */
45 
46 #include "opt_ddb.h"
47 
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/device.h>
51 #include <sys/kernel.h> /* For hz */
52 #include <sys/disklabel.h>
53 #include <sys/dkstat.h>
54 #include <sys/buf.h>
55 #include <dev/scsipi/scsi_all.h>
56 #include <dev/scsipi/scsipi_all.h>
57 #include <dev/scsipi/scsiconf.h>
58 #include <vm/vm.h>
59 #include <vm/vm_kern.h>
60 #include <vm/vm_page.h>
61 #include <machine/pmap.h>
62 #include <machine/cpu.h>
63 #include <amiga/amiga/device.h>
64 #include <amiga/amiga/custom.h>
65 #include <amiga/amiga/isr.h>
66 #include <amiga/dev/dmavar.h>
67 #include <amiga/dev/sbicreg.h>
68 #include <amiga/dev/sbicvar.h>
69 
70 /* These are for bounce buffers */
71 #include <amiga/amiga/cc.h>
72 #include <amiga/dev/zbusvar.h>
73 
74 #include <vm/pmap.h>
75 
76 /* Since I can't find this in any other header files */
77 #define SCSI_PHASE(reg)	(reg&0x07)
78 
79 /*
80  * SCSI delays
81  * In u-seconds, primarily for state changes on the SPC.
82  */
83 #define	SBIC_CMD_WAIT	50000	/* wait per step of 'immediate' cmds */
84 #define	SBIC_DATA_WAIT	50000	/* wait per data in/out step */
85 #define	SBIC_INIT_WAIT	50000	/* wait per step (both) during init */
86 
87 #define SBIC_WAIT(regs, until, timeo) sbicwait(regs, until, timeo, __LINE__)
88 
89 int  sbicicmd __P((struct sbic_softc *, int, int, void *, int, void *, int));
90 int  sbicgo __P((struct sbic_softc *, struct scsipi_xfer *));
91 int  sbicdmaok __P((struct sbic_softc *, struct scsipi_xfer *));
92 int  sbicwait __P((sbic_regmap_t, char, int , int));
93 int  sbiccheckdmap __P((void *, u_long, u_long));
94 int  sbicselectbus __P((struct sbic_softc *, sbic_regmap_t, u_char, u_char, u_char));
95 int  sbicxfstart __P((sbic_regmap_t, int, u_char, int));
96 int  sbicxfout __P((sbic_regmap_t regs, int, void *, int));
97 int  sbicfromscsiperiod __P((struct sbic_softc *, sbic_regmap_t, int));
98 int  sbictoscsiperiod __P((struct sbic_softc *, sbic_regmap_t, int));
99 int  sbicpoll __P((struct sbic_softc *));
100 int  sbicnextstate __P((struct sbic_softc *, u_char, u_char));
101 int  sbicmsgin __P((struct sbic_softc *));
102 int  sbicxfin __P((sbic_regmap_t regs, int, void *));
103 int  sbicabort __P((struct sbic_softc *, sbic_regmap_t, char *));
104 void sbicxfdone __P((struct sbic_softc *, sbic_regmap_t, int));
105 void sbicerror __P((struct sbic_softc *, sbic_regmap_t, u_char));
106 void sbicstart __P((struct sbic_softc *));
107 void sbicreset __P((struct sbic_softc *));
108 void sbic_scsidone __P((struct sbic_acb *, int));
109 void sbic_sched __P((struct sbic_softc *));
110 void sbic_save_ptrs __P((struct sbic_softc *, sbic_regmap_t,int,int));
111 void sbic_load_ptrs __P((struct sbic_softc *, sbic_regmap_t,int,int));
112 #ifdef DEBUG
113 void sbicdumpstate __P((void));
114 void sbic_dump_acb __P((struct sbic_acb *));
115 #endif
116 
117 /*
118  * Synch xfer parameters, and timing conversions
119  */
120 int sbic_min_period = SBIC_SYN_MIN_PERIOD;  /* in cycles = f(ICLK,FSn) */
121 int sbic_max_offset = SBIC_SYN_MAX_OFFSET;  /* pure number */
122 
123 int sbic_cmd_wait = SBIC_CMD_WAIT;
124 int sbic_data_wait = SBIC_DATA_WAIT;
125 int sbic_init_wait = SBIC_INIT_WAIT;
126 
127 /*
128  * was broken before.. now if you want this you get it for all drives
129  * on sbic controllers.
130  */
131 u_char sbic_inhibit_sync[8];
132 int sbic_enable_reselect = 1;
133 int sbic_clock_override = 0;
134 int sbic_no_dma = 0;
135 int sbic_parallel_operations = 1;
136 
137 #ifdef DEBUG
138 sbic_regmap_t debug_sbic_regs;
139 int	sbicdma_ops = 0;	/* total DMA operations */
140 int	sbicdma_bounces = 0;	/* number operations using bounce buffer */
141 int	sbicdma_hits = 0;	/* number of DMA chains that were contiguous */
142 int	sbicdma_misses = 0;	/* number of DMA chains that were not contiguous */
143 int     sbicdma_saves = 0;
144 #define QPRINTF(a) if (sbic_debug > 1) printf a
145 int	sbic_debug = 0;
146 int	sync_debug = 0;
147 int	sbic_dma_debug = 0;
148 int	reselect_debug = 0;
149 int	report_sense = 0;
150 int	data_pointer_debug = 0;
151 u_char	debug_asr, debug_csr, routine;
152 void sbictimeout __P((struct sbic_softc *dev));
153 
154 #define CSR_TRACE_SIZE 32
155 #if CSR_TRACE_SIZE
156 #define CSR_TRACE(w,c,a,x) do { \
157 	int s = splbio(); \
158 	csr_trace[csr_traceptr].whr = (w); csr_trace[csr_traceptr].csr = (c); \
159 	csr_trace[csr_traceptr].asr = (a); csr_trace[csr_traceptr].xtn = (x); \
160 	dma_cachectl((caddr_t)&csr_trace[csr_traceptr], sizeof(csr_trace[0])); \
161 	csr_traceptr = (csr_traceptr + 1) & (CSR_TRACE_SIZE - 1); \
162 /*	dma_cachectl((caddr_t)&csr_traceptr, sizeof(csr_traceptr));*/ \
163 	splx(s); \
164 } while (0)
165 int csr_traceptr;
166 int csr_tracesize = CSR_TRACE_SIZE;
167 struct {
168 	u_char whr;
169 	u_char csr;
170 	u_char asr;
171 	u_char xtn;
172 } csr_trace[CSR_TRACE_SIZE];
173 #else
174 #define CSR_TRACE(w,c,a,x)
175 #endif
176 
177 #define SBIC_TRACE_SIZE 0
178 #if SBIC_TRACE_SIZE
179 #define SBIC_TRACE(dev) do { \
180 	int s = splbio(); \
181 	sbic_trace[sbic_traceptr].sp = &s; \
182 	sbic_trace[sbic_traceptr].line = __LINE__; \
183 	sbic_trace[sbic_traceptr].sr = s; \
184 	sbic_trace[sbic_traceptr].csr = csr_traceptr; \
185 	dma_cachectl(&sbic_trace[sbic_traceptr], sizeof(sbic_trace[0])); \
186 	sbic_traceptr = (sbic_traceptr + 1) & (SBIC_TRACE_SIZE - 1); \
187 	dma_cachectl(&sbic_traceptr, sizeof(sbic_traceptr)); \
188 	if (dev) dma_cachectl(dev, sizeof(*dev)); \
189 	splx(s); \
190 } while (0)
191 int sbic_traceptr;
192 int sbic_tracesize = SBIC_TRACE_SIZE;
193 struct {
194 	void *sp;
195 	u_short line;
196 	u_short sr;
197 	int csr;
198 } sbic_trace[SBIC_TRACE_SIZE];
199 #else
200 #define SBIC_TRACE(dev)
201 #endif
202 
203 #else	/* DEBUG */
204 #define QPRINTF(a)
205 #define CSR_TRACE(w,c,a,x)
206 #define SBIC_TRACE(dev)
207 #endif	/* DEBUG */
208 
209 /*
210  * default minphys routine for sbic based controllers
211  */
212 void
213 sbic_minphys(bp)
214 	struct buf *bp;
215 {
216 
217 	/*
218 	 * No max transfer at this level.
219 	 */
220 	minphys(bp);
221 }
222 
223 /*
224  * Save DMA pointers.  Take into account partial transfer. Shut down DMA.
225  */
226 void
227 sbic_save_ptrs(dev, regs, target, lun)
228 	struct sbic_softc *dev;
229 	sbic_regmap_t regs;
230 	int target, lun;
231 {
232 	int count, asr, s;
233 	struct sbic_acb* acb;
234 
235 	SBIC_TRACE(dev);
236 	if( !dev->sc_cur ) return;
237 	if( !(dev->sc_flags & SBICF_INDMA) ) return; /* DMA not active */
238 
239 	s = splbio();
240 
241 	acb = dev->sc_nexus;
242 	count = -1;
243 	do {
244 		GET_SBIC_asr(regs, asr);
245 		if( asr & SBIC_ASR_DBR ) {
246 			printf("sbic_save_ptrs: asr %02x canceled!\n", asr);
247 			splx(s);
248 			SBIC_TRACE(dev);
249 			return;
250 		}
251 	} while( asr & (SBIC_ASR_BSY|SBIC_ASR_CIP) );
252 
253 	/* Save important state */
254 	/* must be done before dmastop */
255 	acb->sc_dmacmd = dev->sc_dmacmd;
256 	SBIC_TC_GET(regs, count);
257 
258 	/* Shut down DMA ====CAREFUL==== */
259 	dev->sc_dmastop(dev);
260 	dev->sc_flags &= ~SBICF_INDMA;
261 	SBIC_TC_PUT(regs, 0);
262 
263 #ifdef DEBUG
264 	if(!count && sbic_debug) printf("%dcount0",target);
265 	if(data_pointer_debug == -1)
266 		printf("SBIC saving target %d data pointers from (%p,%x)%xASR:%02x",
267 		       target, dev->sc_cur->dc_addr, dev->sc_cur->dc_count,
268 		       acb->sc_dmacmd, asr);
269 #endif
270 
271 	/* Fixup partial xfers */
272 	acb->sc_kv.dc_addr += (dev->sc_tcnt - count);
273 	acb->sc_kv.dc_count -= (dev->sc_tcnt - count);
274 	acb->sc_pa.dc_addr += (dev->sc_tcnt - count);
275 	acb->sc_pa.dc_count -= ((dev->sc_tcnt - count)>>1);
276 
277 	acb->sc_tcnt = dev->sc_tcnt = count;
278 #ifdef DEBUG
279 	if(data_pointer_debug)
280 		printf(" at (%p,%x):%x\n",
281 		       dev->sc_cur->dc_addr, dev->sc_cur->dc_count,count);
282 	sbicdma_saves++;
283 #endif
284 	splx(s);
285 	SBIC_TRACE(dev);
286 }
287 
288 
289 /*
290  * DOES NOT RESTART DMA!!!
291  */
292 void sbic_load_ptrs(dev, regs, target, lun)
293 	struct sbic_softc *dev;
294 	sbic_regmap_t regs;
295 	int target, lun;
296 {
297 	int s, count;
298 	char* vaddr, * paddr;
299 	struct sbic_acb *acb;
300 
301 	SBIC_TRACE(dev);
302 	acb = dev->sc_nexus;
303 	if( !acb->sc_kv.dc_count ) {
304 		/* No data to xfer */
305 		SBIC_TRACE(dev);
306 		return;
307 	}
308 
309 	s = splbio();
310 
311 	dev->sc_last = dev->sc_cur = &acb->sc_pa;
312 	dev->sc_tcnt = acb->sc_tcnt;
313 	dev->sc_dmacmd = acb->sc_dmacmd;
314 
315 #ifdef DEBUG
316 	sbicdma_ops++;
317 #endif
318 	if( !dev->sc_tcnt ) {
319 		/* sc_tcnt == 0 implies end of segment */
320 
321 		/* do kvm to pa mappings */
322 		paddr = acb->sc_pa.dc_addr =
323 			(char *) kvtop(acb->sc_kv.dc_addr);
324 
325 		vaddr = acb->sc_kv.dc_addr;
326 		count = acb->sc_kv.dc_count;
327 		for(count = (NBPG - ((int)vaddr & PGOFSET));
328 		    count < acb->sc_kv.dc_count
329 		    && (char*)kvtop(vaddr + count + 4) == paddr + count + 4;
330 		    count += NBPG);
331 		/* If it's all contiguous... */
332 		if(count > acb->sc_kv.dc_count ) {
333 			count = acb->sc_kv.dc_count;
334 #ifdef DEBUG
335 			sbicdma_hits++;
336 #endif
337 		} else {
338 #ifdef DEBUG
339 			sbicdma_misses++;
340 #endif
341 		}
342 		acb->sc_tcnt = count;
343 		acb->sc_pa.dc_count = count >> 1;
344 
345 #ifdef DEBUG
346 		if(data_pointer_debug)
347 			printf("DMA recalc:kv(%p,%x)pa(%p,%lx)\n",
348 			       acb->sc_kv.dc_addr,
349 			       acb->sc_kv.dc_count,
350 			       acb->sc_pa.dc_addr,
351 			       acb->sc_tcnt);
352 #endif
353 	}
354 	splx(s);
355 #ifdef DEBUG
356 	if(data_pointer_debug)
357 		printf("SBIC restoring target %d data pointers at (%p,%x)%x\n",
358 		       target, dev->sc_cur->dc_addr, dev->sc_cur->dc_count,
359 		       dev->sc_dmacmd);
360 #endif
361 	SBIC_TRACE(dev);
362 }
363 
364 /*
365  * used by specific sbic controller
366  *
367  * it appears that the higher level code does nothing with LUN's
368  * so I will too.  I could plug it in, however so could they
369  * in scsi_scsipi_cmd().
370  */
371 int
372 sbic_scsicmd(xs)
373 	struct scsipi_xfer *xs;
374 {
375 	struct sbic_acb *acb;
376 	struct sbic_softc *dev;
377 	struct scsipi_link *slp;
378 	int flags, s, stat;
379 
380 	slp = xs->sc_link;
381 	dev = slp->adapter_softc;
382 	SBIC_TRACE(dev);
383 	flags = xs->xs_control;
384 
385 	if (flags & XS_CTL_DATA_UIO)
386 		panic("sbic: scsi data uio requested");
387 
388 	if (dev->sc_nexus && flags & XS_CTL_POLL)
389 		panic("sbic_scsicmd: busy");
390 
391 	if (slp->scsipi_scsi.target == slp->scsipi_scsi.adapter_target)
392 		return ESCAPE_NOT_SUPPORTED;
393 
394 	s = splbio();
395 	acb = dev->free_list.tqh_first;
396 	if (acb)
397 		TAILQ_REMOVE(&dev->free_list, acb, chain);
398 	splx(s);
399 
400 	if (acb == NULL) {
401 #ifdef DEBUG
402 		printf("sbic_scsicmd: unable to queue request for target %d\n",
403 		    slp->scsipi_scsi.target);
404 #ifdef DDB
405 		Debugger();
406 #endif
407 #endif
408 		xs->error = XS_DRIVER_STUFFUP;
409 		SBIC_TRACE(dev);
410 		return(TRY_AGAIN_LATER);
411 	}
412 
413 	acb->flags = ACB_ACTIVE;
414 	if (flags & XS_CTL_DATA_IN)
415 		acb->flags |= ACB_DATAIN;
416 	acb->xs = xs;
417 	bcopy(xs->cmd, &acb->cmd, xs->cmdlen);
418 	acb->clen = xs->cmdlen;
419 	acb->sc_kv.dc_addr = xs->data;
420 	acb->sc_kv.dc_count = xs->datalen;
421 	acb->pa_addr = xs->data ? (char *)kvtop(xs->data) : 0;	/* XXXX check */
422 
423 	if (flags & XS_CTL_POLL) {
424 		s = splbio();
425 		/*
426 		 * This has major side effects -- it locks up the machine
427 		 */
428 
429 		dev->sc_flags |= SBICF_ICMD;
430 		do {
431 			while(dev->sc_nexus)
432 				sbicpoll(dev);
433 			dev->sc_nexus = acb;
434 			dev->sc_stat[0] = -1;
435 			dev->sc_xs = xs;
436 			dev->target = slp->scsipi_scsi.target;
437 			dev->lun = slp->scsipi_scsi.lun;
438 			stat = sbicicmd(dev, slp->scsipi_scsi.target, slp->scsipi_scsi.lun,
439 					&acb->cmd, acb->clen,
440 					acb->sc_kv.dc_addr, acb->sc_kv.dc_count);
441 		} while (dev->sc_nexus != acb);
442 		sbic_scsidone(acb, stat);
443 
444 		splx(s);
445 		SBIC_TRACE(dev);
446 		return(COMPLETE);
447 	}
448 
449 	s = splbio();
450 	TAILQ_INSERT_TAIL(&dev->ready_list, acb, chain);
451 
452 	if (dev->sc_nexus) {
453 		splx(s);
454 		SBIC_TRACE(dev);
455 		return(SUCCESSFULLY_QUEUED);
456 	}
457 
458 	/*
459 	 * nothing is active, try to start it now.
460 	 */
461 	sbic_sched(dev);
462 	splx(s);
463 
464 	SBIC_TRACE(dev);
465 /* TODO:  add sbic_poll to do XS_CTL_POLL operations */
466 #if 0
467 	if (flags & XS_CTL_POLL)
468 		return(COMPLETE);
469 #endif
470 	return(SUCCESSFULLY_QUEUED);
471 }
472 
473 /*
474  * attempt to start the next available command
475  */
476 void
477 sbic_sched(dev)
478 	struct sbic_softc *dev;
479 {
480 	struct scsipi_xfer *xs;
481 	struct scsipi_link *slp;
482 	struct sbic_acb *acb;
483 	int flags, /*phase,*/ stat, i;
484 
485 	SBIC_TRACE(dev);
486 	if (dev->sc_nexus)
487 		return;			/* a command is current active */
488 
489 	SBIC_TRACE(dev);
490 	for (acb = dev->ready_list.tqh_first; acb; acb = acb->chain.tqe_next) {
491 		slp = acb->xs->sc_link;
492 		i = slp->scsipi_scsi.target;
493 		if (!(dev->sc_tinfo[i].lubusy & (1 << slp->scsipi_scsi.lun))) {
494 			struct sbic_tinfo *ti = &dev->sc_tinfo[i];
495 
496 			TAILQ_REMOVE(&dev->ready_list, acb, chain);
497 			dev->sc_nexus = acb;
498 			slp = acb->xs->sc_link;
499 			ti = &dev->sc_tinfo[slp->scsipi_scsi.target];
500 			ti->lubusy |= (1 << slp->scsipi_scsi.lun);
501 			acb->sc_pa.dc_addr = acb->pa_addr;	/* XXXX check */
502 			break;
503 		}
504 	}
505 
506 	SBIC_TRACE(dev);
507 	if (acb == NULL)
508 		return;			/* did not find an available command */
509 
510 	dev->sc_xs = xs = acb->xs;
511 	slp = xs->sc_link;
512 	flags = xs->xs_control;
513 
514 	if (flags & XS_CTL_RESET)
515 		sbicreset(dev);
516 
517 #ifdef DEBUG
518 	if( data_pointer_debug > 1 )
519 		printf("sbic_sched(%d,%d)\n",slp->scsipi_scsi.target,
520 			slp->scsipi_scsi.lun);
521 #endif
522 	dev->sc_stat[0] = -1;
523 	dev->target = slp->scsipi_scsi.target;
524 	dev->lun = slp->scsipi_scsi.lun;
525 	if ( flags & XS_CTL_POLL || ( !sbic_parallel_operations
526 				   && (sbicdmaok(dev, xs) == 0)))
527 		stat = sbicicmd(dev, slp->scsipi_scsi.target,
528 			slp->scsipi_scsi.lun, &acb->cmd,
529 		    acb->clen, acb->sc_kv.dc_addr, acb->sc_kv.dc_count);
530 	else if (sbicgo(dev, xs) == 0 && xs->error != XS_SELTIMEOUT) {
531 		SBIC_TRACE(dev);
532 		return;
533 	} else
534 		stat = dev->sc_stat[0];
535 
536 	sbic_scsidone(acb, stat);
537 	SBIC_TRACE(dev);
538 }
539 
540 void
541 sbic_scsidone(acb, stat)
542 	struct sbic_acb *acb;
543 	int stat;
544 {
545 	struct scsipi_xfer *xs;
546 	struct scsipi_link *slp;
547 	struct sbic_softc *dev;
548 	int dosched = 0;
549 
550 	xs = acb->xs;
551 	slp = xs->sc_link;
552 	dev = slp->adapter_softc;
553 	SBIC_TRACE(dev);
554 #ifdef DIAGNOSTIC
555 	if (acb == NULL || xs == NULL) {
556 		printf("sbic_scsidone -- (%d,%d) no scsi_xfer\n",
557 		       dev->target, dev->lun);
558 #ifdef DDB
559 		Debugger();
560 #endif
561 		return;
562 	}
563 #endif
564 	/*
565 	 * is this right?
566 	 */
567 	xs->status = stat;
568 
569 #ifdef DEBUG
570 	if( data_pointer_debug > 1 )
571 		printf("scsidone: (%d,%d)->(%d,%d)%02x\n",
572 		       slp->scsipi_scsi.target, slp->scsipi_scsi.lun,
573 		       dev->target,  dev->lun,  stat);
574 	if( xs->sc_link->scsipi_scsi.target ==
575 		dev->sc_link.scsipi_scsi.adapter_target )
576 		panic("target == hostid");
577 #endif
578 
579 	if (xs->error == XS_NOERROR && !(acb->flags & ACB_CHKSENSE)) {
580 		if (stat == SCSI_CHECK) {
581 			/* Schedule a REQUEST SENSE */
582 			struct scsipi_sense *ss = (void *)&acb->cmd;
583 #ifdef DEBUG
584 			if (report_sense)
585 				printf("sbic_scsidone: autosense %02x targ %d lun %d",
586 				    acb->cmd.opcode, slp->scsipi_scsi.target,
587 					slp->scsipi_scsi.lun);
588 #endif
589 			bzero(ss, sizeof(*ss));
590 			ss->opcode = REQUEST_SENSE;
591 			ss->byte2 = slp->scsipi_scsi.lun << 5;
592 			ss->length = sizeof(struct scsipi_sense_data);
593 			acb->clen = sizeof(*ss);
594 			acb->sc_kv.dc_addr = (char *)&xs->sense.scsi_sense;
595 			acb->sc_kv.dc_count = sizeof(struct scsipi_sense_data);
596 			acb->pa_addr = (char *)kvtop((u_char *)&xs->sense.scsi_sense); /* XXX check */
597 			acb->flags = ACB_ACTIVE | ACB_CHKSENSE | ACB_DATAIN;
598 			TAILQ_INSERT_HEAD(&dev->ready_list, acb, chain);
599 			dev->sc_tinfo[slp->scsipi_scsi.target].lubusy &=
600 			    ~(1 << slp->scsipi_scsi.lun);
601 			dev->sc_tinfo[slp->scsipi_scsi.target].senses++;
602 			if (dev->sc_nexus == acb) {
603 				dev->sc_nexus = NULL;
604 				dev->sc_xs = NULL;
605 				sbic_sched(dev);
606 			}
607 			SBIC_TRACE(dev);
608 			return;
609 		}
610 	}
611 	if (xs->error == XS_NOERROR && (acb->flags & ACB_CHKSENSE)) {
612 		xs->error = XS_SENSE;
613 #ifdef DEBUG
614 		if (report_sense)
615 			printf(" => %02x %02x\n", xs->sense.scsi_sense.flags,
616 			    xs->sense.scsi_sense.extra_bytes[3]);
617 #endif
618 	} else {
619 		xs->resid = 0;		/* XXXX */
620 	}
621 #if whataboutthisone
622 		case SCSI_BUSY:
623 			xs->error = XS_BUSY;
624 			break;
625 #endif
626 	xs->xs_status |= XS_STS_DONE;
627 
628 	/*
629 	 * Remove the ACB from whatever queue it's on.  We have to do a bit of
630 	 * a hack to figure out which queue it's on.  Note that it is *not*
631 	 * necessary to cdr down the ready queue, but we must cdr down the
632 	 * nexus queue and see if it's there, so we can mark the unit as no
633 	 * longer busy.  This code is sickening, but it works.
634 	 */
635 	if (acb == dev->sc_nexus) {
636 		dev->sc_nexus = NULL;
637 		dev->sc_xs = NULL;
638 		dev->sc_tinfo[slp->scsipi_scsi.target].lubusy &=
639 			~(1<<slp->scsipi_scsi.lun);
640 		if (dev->ready_list.tqh_first)
641 			dosched = 1;	/* start next command */
642 	} else if (dev->ready_list.tqh_last == &acb->chain.tqe_next) {
643 		TAILQ_REMOVE(&dev->ready_list, acb, chain);
644 	} else {
645 		register struct sbic_acb *acb2;
646 		for (acb2 = dev->nexus_list.tqh_first; acb2;
647 		    acb2 = acb2->chain.tqe_next) {
648 			if (acb2 == acb) {
649 				TAILQ_REMOVE(&dev->nexus_list, acb, chain);
650 				dev->sc_tinfo[slp->scsipi_scsi.target].lubusy
651 					&= ~(1<<slp->scsipi_scsi.lun);
652 				break;
653 			}
654 		}
655 		if (acb2)
656 			;
657 		else if (acb->chain.tqe_next) {
658 			TAILQ_REMOVE(&dev->ready_list, acb, chain);
659 		} else {
660 			printf("%s: can't find matching acb\n",
661 			    dev->sc_dev.dv_xname);
662 #ifdef DDB
663 			Debugger();
664 #endif
665 		}
666 	}
667 	/* Put it on the free list. */
668 	acb->flags = ACB_FREE;
669 	TAILQ_INSERT_HEAD(&dev->free_list, acb, chain);
670 
671 	dev->sc_tinfo[slp->scsipi_scsi.target].cmds++;
672 
673 	scsipi_done(xs);
674 
675 	if (dosched)
676 		sbic_sched(dev);
677 	SBIC_TRACE(dev);
678 }
679 
680 int
681 sbicdmaok(dev, xs)
682 	struct sbic_softc *dev;
683 	struct scsipi_xfer *xs;
684 {
685 	if (sbic_no_dma || !xs->datalen || xs->datalen & 0x1 ||
686 	    (u_int)xs->data & 0x3)
687 		return(0);
688 	/*
689 	 * controller supports dma to any addresses?
690 	 */
691 	else if ((dev->sc_flags & SBICF_BADDMA) == 0)
692 		return(1);
693 	/*
694 	 * this address is ok for dma?
695 	 */
696 	else if (sbiccheckdmap(xs->data, xs->datalen, dev->sc_dmamask) == 0)
697 		return(1);
698 	/*
699 	 * we have a bounce buffer?
700 	 */
701 	else if (dev->sc_tinfo[xs->sc_link->scsipi_scsi.target].bounce)
702 		return(1);
703 	/*
704 	 * try to get one
705 	 */
706 	else if ((dev->sc_tinfo[xs->sc_link->scsipi_scsi.target].bounce
707 		 = (char *)alloc_z2mem(MAXPHYS))) {
708 		if (isztwomem(dev->sc_tinfo[xs->sc_link->scsipi_scsi.target].bounce))
709 			printf("alloc ZII target %d bounce pa 0x%x\n",
710 			       xs->sc_link->scsipi_scsi.target,
711 			       kvtop(dev->sc_tinfo[xs->sc_link->scsipi_scsi.target].bounce));
712 		else if (dev->sc_tinfo[xs->sc_link->scsipi_scsi.target].bounce)
713 			printf("alloc CHIP target %d bounce pa 0x%p\n",
714 			       xs->sc_link->scsipi_scsi.target,
715 			       PREP_DMA_MEM(dev->sc_tinfo[xs->sc_link->scsipi_scsi.target].bounce));
716 		return(1);
717 	}
718 
719 	return(0);
720 }
721 
722 
723 int
724 sbicwait(regs, until, timeo, line)
725 	sbic_regmap_t regs;
726 	char until;
727 	int timeo;
728 	int line;
729 {
730 	u_char val;
731 	int csr;
732 
733 	SBIC_TRACE((struct sbic_softc *)0);
734 	if (timeo == 0)
735 		timeo = 1000000;	/* some large value.. */
736 
737 	GET_SBIC_asr(regs,val);
738 	while ((val & until) == 0) {
739 		if (timeo-- == 0) {
740 			GET_SBIC_csr(regs, csr);
741 			printf("sbicwait TIMEO @%d with asr=x%x csr=x%x\n",
742 			    line, val, csr);
743 #if defined(DDB) && defined(DEBUG)
744 			Debugger();
745 #endif
746 			return(val); /* Maybe I should abort */
747 			break;
748 		}
749 		DELAY(1);
750 		GET_SBIC_asr(regs,val);
751 	}
752 	SBIC_TRACE((struct sbic_softc *)0);
753 	return(val);
754 }
755 
756 int
757 sbicabort(dev, regs, where)
758 	struct sbic_softc *dev;
759 	sbic_regmap_t regs;
760 	char *where;
761 {
762 	u_char csr, asr;
763 
764 	GET_SBIC_asr(regs, asr);
765 	GET_SBIC_csr(regs, csr);
766 
767 	printf ("%s: abort %s: csr = 0x%02x, asr = 0x%02x\n",
768 	    dev->sc_dev.dv_xname, where, csr, asr);
769 
770 
771 #if 0
772 	/* Clean up running command */
773 	if (dev->sc_nexus != NULL) {
774 		dev->sc_nexus->xs->error = XS_DRIVER_STUFFUP;
775 		sbic_scsidone(dev->sc_nexus, dev->sc_stat[0]);
776 	}
777 	while (acb = dev->nexus_list.tqh_first) {
778 		acb->xs->error = XS_DRIVER_STUFFUP;
779 		sbic_scsidone(acb, -1 /*acb->stat[0]*/);
780 	}
781 #endif
782 
783 	/* Clean up chip itself */
784 	if (dev->sc_flags & SBICF_SELECTED) {
785 		while( asr & SBIC_ASR_DBR ) {
786 			/* sbic is jammed w/data. need to clear it */
787 			/* But we don't know what direction it needs to go */
788 			GET_SBIC_data(regs, asr);
789 			printf("%s: abort %s: clearing data buffer 0x%02x\n",
790 			       dev->sc_dev.dv_xname, where, asr);
791 			GET_SBIC_asr(regs, asr);
792 			if( asr & SBIC_ASR_DBR ) /* Not the read direction, then */
793 				SET_SBIC_data(regs, asr);
794 			GET_SBIC_asr(regs, asr);
795 		}
796 		WAIT_CIP(regs);
797 printf("%s: sbicabort - sending ABORT command\n", dev->sc_dev.dv_xname);
798 		SET_SBIC_cmd(regs, SBIC_CMD_ABORT);
799 		WAIT_CIP(regs);
800 
801 		GET_SBIC_asr(regs, asr);
802 		if (asr & (SBIC_ASR_BSY|SBIC_ASR_LCI)) {
803 			/* ok, get more drastic.. */
804 
805 printf("%s: sbicabort - asr %x, trying to reset\n", dev->sc_dev.dv_xname, asr);
806 			sbicreset(dev);
807 			dev->sc_flags &= ~SBICF_SELECTED;
808 			return -1;
809 		}
810 printf("%s: sbicabort - sending DISC command\n", dev->sc_dev.dv_xname);
811 		SET_SBIC_cmd(regs, SBIC_CMD_DISC);
812 
813 		do {
814 			asr = SBIC_WAIT (regs, SBIC_ASR_INT, 0);
815 			GET_SBIC_csr (regs, csr);
816 			CSR_TRACE('a',csr,asr,0);
817 		} while ((csr != SBIC_CSR_DISC) && (csr != SBIC_CSR_DISC_1)
818 		    && (csr != SBIC_CSR_CMD_INVALID));
819 
820 		/* lets just hope it worked.. */
821 		dev->sc_flags &= ~SBICF_SELECTED;
822 	}
823 	return -1;
824 }
825 
826 
827 /*
828  * Initialize driver-private structures
829  */
830 
831 void
832 sbicinit(dev)
833 	struct sbic_softc *dev;
834 {
835 	sbic_regmap_t regs;
836 	u_int i;
837 	struct sbic_acb *acb;
838 	u_int inhibit_sync;
839 
840 	extern u_long scsi_nosync;
841 	extern int shift_nosync;
842 
843 	regs = dev->sc_sbic;
844 
845 	if ((dev->sc_flags & SBICF_ALIVE) == 0) {
846 		TAILQ_INIT(&dev->ready_list);
847 		TAILQ_INIT(&dev->nexus_list);
848 		TAILQ_INIT(&dev->free_list);
849 		dev->sc_nexus = NULL;
850 		dev->sc_xs = NULL;
851 		acb = dev->sc_acb;
852 		bzero(acb, sizeof(dev->sc_acb));
853 		for (i = 0; i < sizeof(dev->sc_acb) / sizeof(*acb); i++) {
854 			TAILQ_INSERT_TAIL(&dev->free_list, acb, chain);
855 			acb++;
856 		}
857 		bzero(dev->sc_tinfo, sizeof(dev->sc_tinfo));
858 #ifdef DEBUG
859 		/* make sure timeout is really not needed */
860 		timeout((void *)sbictimeout, dev, 30 * hz);
861 #endif
862 
863 	} else panic("sbic: reinitializing driver!");
864 
865 	dev->sc_flags |= SBICF_ALIVE;
866 	dev->sc_flags &= ~SBICF_SELECTED;
867 
868 	/* initialize inhibit array */
869 	if (scsi_nosync) {
870 		inhibit_sync = (scsi_nosync >> shift_nosync) & 0xff;
871 		shift_nosync += 8;
872 #ifdef DEBUG
873 		if (inhibit_sync)
874 			printf("%s: Inhibiting synchronous transfer %02x\n",
875 				dev->sc_dev.dv_xname, inhibit_sync);
876 #endif
877 		for (i = 0; i < 8; ++i)
878 			if (inhibit_sync & (1 << i))
879 				sbic_inhibit_sync[i] = 1;
880 	}
881 
882 	sbicreset(dev);
883 }
884 
885 void
886 sbicreset(dev)
887 	struct sbic_softc *dev;
888 {
889 	sbic_regmap_t regs;
890 	u_int my_id, s;
891 	u_char csr;
892 #if 0
893 	u_int i;
894 	struct sbic_acb *acb;
895 #endif
896 
897 	regs = dev->sc_sbic;
898 #if 0
899 	if (dev->sc_flags & SBICF_ALIVE) {
900 		SET_SBIC_cmd(regs, SBIC_CMD_ABORT);
901 		WAIT_CIP(regs);
902 	}
903 #else
904 		SET_SBIC_cmd(regs, SBIC_CMD_ABORT);
905 		WAIT_CIP(regs);
906 #endif
907 	s = splbio();
908 	my_id = dev->sc_link.scsipi_scsi.adapter_target & SBIC_ID_MASK;
909 
910 	/* Enable advanced mode */
911 	my_id |= SBIC_ID_EAF /*| SBIC_ID_EHP*/ ;
912 	SET_SBIC_myid(regs, my_id);
913 
914 	/*
915 	 * Disable interrupts (in dmainit) then reset the chip
916 	 */
917 	SET_SBIC_cmd(regs, SBIC_CMD_RESET);
918 	DELAY(25);
919 	SBIC_WAIT(regs, SBIC_ASR_INT, 0);
920 	GET_SBIC_csr(regs, csr);       /* clears interrupt also */
921 
922 	if (dev->sc_clkfreq < 110)
923 		my_id |= SBIC_ID_FS_8_10;
924 	else if (dev->sc_clkfreq < 160)
925 		my_id |= SBIC_ID_FS_12_15;
926 	else if (dev->sc_clkfreq < 210)
927 		my_id |= SBIC_ID_FS_16_20;
928 
929 	SET_SBIC_myid(regs, my_id);
930 
931 	/*
932 	 * Set up various chip parameters
933 	 */
934 	SET_SBIC_control(regs, SBIC_CTL_EDI | SBIC_CTL_IDI /* | SBIC_CTL_HSP */
935 	    | SBIC_MACHINE_DMA_MODE);
936 	/*
937 	 * don't allow (re)selection (SBIC_RID_ES)
938 	 * until we can handle target mode!!
939 	 */
940 	SET_SBIC_rselid(regs, SBIC_RID_ER);
941 	SET_SBIC_syn(regs, 0);     /* asynch for now */
942 
943 	/*
944 	 * anything else was zeroed by reset
945 	 */
946 	splx(s);
947 
948 #if 0
949 	if ((dev->sc_flags & SBICF_ALIVE) == 0) {
950 		TAILQ_INIT(&dev->ready_list);
951 		TAILQ_INIT(&dev->nexus_list);
952 		TAILQ_INIT(&dev->free_list);
953 		dev->sc_nexus = NULL;
954 		dev->sc_xs = NULL;
955 		acb = dev->sc_acb;
956 		bzero(acb, sizeof(dev->sc_acb));
957 		for (i = 0; i < sizeof(dev->sc_acb) / sizeof(*acb); i++) {
958 			TAILQ_INSERT_TAIL(&dev->free_list, acb, chain);
959 			acb++;
960 		}
961 		bzero(dev->sc_tinfo, sizeof(dev->sc_tinfo));
962 	} else {
963 		if (dev->sc_nexus != NULL) {
964 			dev->sc_nexus->xs->error = XS_DRIVER_STUFFUP;
965 			sbic_scsidone(dev->sc_nexus, dev->sc_stat[0]);
966 		}
967 		while (acb = dev->nexus_list.tqh_first) {
968 			acb->xs->error = XS_DRIVER_STUFFUP;
969 			sbic_scsidone(acb, -1 /*acb->stat[0]*/);
970 		}
971 	}
972 
973 	dev->sc_flags |= SBICF_ALIVE;
974 #endif
975 	dev->sc_flags &= ~SBICF_SELECTED;
976 }
977 
978 void
979 sbicerror(dev, regs, csr)
980 	struct sbic_softc *dev;
981 	sbic_regmap_t regs;
982 	u_char csr;
983 {
984 	struct scsipi_xfer *xs;
985 
986 	xs = dev->sc_xs;
987 
988 #ifdef DIAGNOSTIC
989 	if (xs == NULL)
990 		panic("sbicerror");
991 #endif
992 	if (xs->xs_control & XS_CTL_SILENT)
993 		return;
994 
995 	printf("%s: ", dev->sc_dev.dv_xname);
996 	printf("csr == 0x%02x\n", csr);	/* XXX */
997 }
998 
999 /*
1000  * select the bus, return when selected or error.
1001  */
1002 int
1003 sbicselectbus(dev, regs, target, lun, our_addr)
1004         struct sbic_softc *dev;
1005 	sbic_regmap_t regs;
1006 	u_char target, lun, our_addr;
1007 {
1008 	u_char asr, csr, id;
1009 
1010 	SBIC_TRACE(dev);
1011 	QPRINTF(("sbicselectbus %d\n", target));
1012 
1013 	/*
1014 	 * if we're already selected, return (XXXX panic maybe?)
1015 	 */
1016 	if (dev->sc_flags & SBICF_SELECTED) {
1017 		SBIC_TRACE(dev);
1018 		return(1);
1019 	}
1020 
1021 	/*
1022 	 * issue select
1023 	 */
1024 	SBIC_TC_PUT(regs, 0);
1025 	SET_SBIC_selid(regs, target);
1026 	SET_SBIC_timeo(regs, SBIC_TIMEOUT(250,dev->sc_clkfreq));
1027 
1028 	/*
1029 	 * set sync or async
1030 	 */
1031 	if (dev->sc_sync[target].state == SYNC_DONE)
1032 		SET_SBIC_syn(regs, SBIC_SYN (dev->sc_sync[target].offset,
1033 		    dev->sc_sync[target].period));
1034 	else
1035 		SET_SBIC_syn(regs, SBIC_SYN (0, sbic_min_period));
1036 
1037 	GET_SBIC_asr(regs, asr);
1038 	if( asr & (SBIC_ASR_INT|SBIC_ASR_BSY) ) {
1039 		/* This means we got ourselves reselected upon */
1040 /*		printf("sbicselectbus: INT/BSY asr %02x\n", asr);*/
1041 #ifdef DDB
1042 /*		Debugger();*/
1043 #endif
1044 		SBIC_TRACE(dev);
1045 		return 1;
1046 	}
1047 
1048 	SET_SBIC_cmd(regs, SBIC_CMD_SEL_ATN);
1049 
1050 	/*
1051 	 * wait for select (merged from seperate function may need
1052 	 * cleanup)
1053 	 */
1054 	WAIT_CIP(regs);
1055 	do {
1056 		asr = SBIC_WAIT(regs, SBIC_ASR_INT | SBIC_ASR_LCI, 0);
1057 		if (asr & SBIC_ASR_LCI) {
1058 #ifdef DEBUG
1059 			if (reselect_debug)
1060 				printf("sbicselectbus: late LCI asr %02x\n", asr);
1061 #endif
1062 			SBIC_TRACE(dev);
1063 			return 1;
1064 		}
1065 		GET_SBIC_csr (regs, csr);
1066 		CSR_TRACE('s',csr,asr,target);
1067 		QPRINTF(("%02x ", csr));
1068 		if( csr == SBIC_CSR_RSLT_NI || csr == SBIC_CSR_RSLT_IFY) {
1069 #ifdef DEBUG
1070 			if(reselect_debug)
1071 				printf("sbicselectbus: reselected asr %02x\n", asr);
1072 #endif
1073 			/* We need to handle this now so we don't lock up later */
1074 			sbicnextstate(dev, csr, asr);
1075 			SBIC_TRACE(dev);
1076 			return 1;
1077 		}
1078 		if( csr == SBIC_CSR_SLT || csr == SBIC_CSR_SLT_ATN) {
1079 			panic("sbicselectbus: target issued select!");
1080 			return 1;
1081 		}
1082 	} while (csr != (SBIC_CSR_MIS_2|MESG_OUT_PHASE)
1083 	    && csr != (SBIC_CSR_MIS_2|CMD_PHASE) && csr != SBIC_CSR_SEL_TIMEO);
1084 
1085 	/* Enable (or not) reselection */
1086 	if(!sbic_enable_reselect && dev->nexus_list.tqh_first == NULL)
1087 		SET_SBIC_rselid (regs, 0);
1088 	else
1089 		SET_SBIC_rselid (regs, SBIC_RID_ER);
1090 
1091 	if (csr == (SBIC_CSR_MIS_2|CMD_PHASE)) {
1092 		dev->sc_flags |= SBICF_SELECTED;	/* device ignored ATN */
1093 		GET_SBIC_selid(regs, id);
1094 		dev->target = id;
1095 		GET_SBIC_tlun(regs,dev->lun);
1096 		if( dev->lun & SBIC_TLUN_VALID )
1097 			dev->lun &= SBIC_TLUN_MASK;
1098 		else
1099 			dev->lun = lun;
1100 	} else if (csr == (SBIC_CSR_MIS_2|MESG_OUT_PHASE)) {
1101 		/*
1102 		 * Send identify message
1103 		 * (SCSI-2 requires an identify msg (?))
1104 		 */
1105 		GET_SBIC_selid(regs, id);
1106 		dev->target = id;
1107 		GET_SBIC_tlun(regs,dev->lun);
1108 		if( dev->lun & SBIC_TLUN_VALID )
1109 			dev->lun &= SBIC_TLUN_MASK;
1110 		else
1111 			dev->lun = lun;
1112 		/*
1113 		 * handle drives that don't want to be asked
1114 		 * whether to go sync at all.
1115 		 */
1116 		if (sbic_inhibit_sync[id]
1117 		    && dev->sc_sync[id].state == SYNC_START) {
1118 #ifdef DEBUG
1119 			if (sync_debug)
1120 				printf("Forcing target %d asynchronous.\n", id);
1121 #endif
1122 			dev->sc_sync[id].offset = 0;
1123 			dev->sc_sync[id].period = sbic_min_period;
1124 			dev->sc_sync[id].state = SYNC_DONE;
1125 		}
1126 
1127 
1128 		if (dev->sc_sync[id].state != SYNC_START){
1129 			if( dev->sc_xs->xs_control & XS_CTL_POLL
1130 			   || (dev->sc_flags & SBICF_ICMD)
1131 			   || !sbic_enable_reselect )
1132 				SEND_BYTE (regs, MSG_IDENTIFY | lun);
1133 			else
1134 				SEND_BYTE (regs, MSG_IDENTIFY_DR | lun);
1135 		} else {
1136 			/*
1137 			 * try to initiate a sync transfer.
1138 			 * So compose the sync message we're going
1139 			 * to send to the target
1140 			 */
1141 
1142 #ifdef DEBUG
1143 			if (sync_debug)
1144 				printf("Sending sync request to target %d ... ",
1145 				    id);
1146 #endif
1147 			/*
1148 			 * setup scsi message sync message request
1149 			 */
1150 			dev->sc_msg[0] = MSG_IDENTIFY | lun;
1151 			dev->sc_msg[1] = MSG_EXT_MESSAGE;
1152 			dev->sc_msg[2] = 3;
1153 			dev->sc_msg[3] = MSG_SYNC_REQ;
1154 			dev->sc_msg[4] = sbictoscsiperiod(dev, regs,
1155 			    sbic_min_period);
1156 			dev->sc_msg[5] = sbic_max_offset;
1157 
1158 			if (sbicxfstart(regs, 6, MESG_OUT_PHASE, sbic_cmd_wait))
1159 				sbicxfout(regs, 6, dev->sc_msg, MESG_OUT_PHASE);
1160 
1161 			dev->sc_sync[id].state = SYNC_SENT;
1162 #ifdef DEBUG
1163 			if (sync_debug)
1164 				printf ("sent\n");
1165 #endif
1166 		}
1167 
1168 		asr = SBIC_WAIT (regs, SBIC_ASR_INT, 0);
1169 		GET_SBIC_csr (regs, csr);
1170 		CSR_TRACE('y',csr,asr,target);
1171 		QPRINTF(("[%02x]", csr));
1172 #ifdef DEBUG
1173 		if (sync_debug && dev->sc_sync[id].state == SYNC_SENT)
1174 			printf("csr-result of last msgout: 0x%x\n", csr);
1175 #endif
1176 
1177 		if (csr != SBIC_CSR_SEL_TIMEO)
1178 			dev->sc_flags |= SBICF_SELECTED;
1179 	}
1180 	if (csr == SBIC_CSR_SEL_TIMEO)
1181 		dev->sc_xs->error = XS_SELTIMEOUT;
1182 
1183 	QPRINTF(("\n"));
1184 
1185 	SBIC_TRACE(dev);
1186 	return(csr == SBIC_CSR_SEL_TIMEO);
1187 }
1188 
1189 int
1190 sbicxfstart(regs, len, phase, wait)
1191 	sbic_regmap_t regs;
1192 	int len, wait;
1193 	u_char phase;
1194 {
1195 	u_char id;
1196 
1197 	switch (phase) {
1198 	case DATA_IN_PHASE:
1199 	case MESG_IN_PHASE:
1200 		GET_SBIC_selid (regs, id);
1201 		id |= SBIC_SID_FROM_SCSI;
1202 		SET_SBIC_selid (regs, id);
1203 		SBIC_TC_PUT (regs, (unsigned)len);
1204 		break;
1205 	case DATA_OUT_PHASE:
1206 	case MESG_OUT_PHASE:
1207 	case CMD_PHASE:
1208 		GET_SBIC_selid (regs, id);
1209 		id &= ~SBIC_SID_FROM_SCSI;
1210 		SET_SBIC_selid (regs, id);
1211 		SBIC_TC_PUT (regs, (unsigned)len);
1212 		break;
1213 	default:
1214 		SBIC_TC_PUT (regs, 0);
1215 	}
1216 	QPRINTF(("sbicxfstart %d, %d, %d\n", len, phase, wait));
1217 
1218 	return(1);
1219 }
1220 
1221 int
1222 sbicxfout(regs, len, bp, phase)
1223 	sbic_regmap_t regs;
1224 	int len;
1225 	void *bp;
1226 	int phase;
1227 {
1228 	u_char orig_csr, asr, *buf;
1229 	int wait;
1230 
1231 	buf = bp;
1232 	wait = sbic_data_wait;
1233 
1234 	QPRINTF(("sbicxfout {%d} %02x %02x %02x %02x %02x "
1235 	    "%02x %02x %02x %02x %02x\n", len, buf[0], buf[1], buf[2],
1236 	    buf[3], buf[4], buf[5], buf[6], buf[7], buf[8], buf[9]));
1237 
1238 	GET_SBIC_csr (regs, orig_csr);
1239 	CSR_TRACE('>',orig_csr,0,0);
1240 
1241 	/*
1242 	 * sigh.. WD-PROTO strikes again.. sending the command in one go
1243 	 * causes the chip to lock up if talking to certain (misbehaving?)
1244 	 * targets. Anyway, this procedure should work for all targets, but
1245 	 * it's slightly slower due to the overhead
1246 	 */
1247 	WAIT_CIP (regs);
1248 	SET_SBIC_cmd (regs, SBIC_CMD_XFER_INFO);
1249 	for (;len > 0; len--) {
1250 		GET_SBIC_asr (regs, asr);
1251 		while ((asr & SBIC_ASR_DBR) == 0) {
1252 			if ((asr & SBIC_ASR_INT) || --wait < 0) {
1253 #ifdef DEBUG
1254 				if (sbic_debug)
1255 					printf("sbicxfout fail: l%d i%x w%d\n",
1256 					    len, asr, wait);
1257 #endif
1258 				return (len);
1259 			}
1260 /*			DELAY(1);*/
1261 			GET_SBIC_asr (regs, asr);
1262 		}
1263 
1264 		SET_SBIC_data (regs, *buf);
1265 		buf++;
1266 	}
1267 	SBIC_TC_GET(regs, len);
1268 	QPRINTF(("sbicxfout done %d bytes\n", len));
1269 	/*
1270 	 * this leaves with one csr to be read
1271 	 */
1272 	return(0);
1273 }
1274 
1275 /* returns # bytes left to read */
1276 int
1277 sbicxfin(regs, len, bp)
1278 	sbic_regmap_t regs;
1279 	int len;
1280 	void *bp;
1281 {
1282 	int wait;
1283 	u_char *obp, *buf;
1284 	u_char orig_csr, csr, asr;
1285 
1286 	wait = sbic_data_wait;
1287 	obp = bp;
1288 	buf = bp;
1289 
1290 	GET_SBIC_csr (regs, orig_csr);
1291 	CSR_TRACE('<',orig_csr,0,0);
1292 
1293 	QPRINTF(("sbicxfin %d, csr=%02x\n", len, orig_csr));
1294 
1295 	WAIT_CIP (regs);
1296 	SET_SBIC_cmd (regs, SBIC_CMD_XFER_INFO);
1297 	for (;len > 0; len--) {
1298 		GET_SBIC_asr (regs, asr);
1299 		if((asr & SBIC_ASR_PE)) {
1300 #ifdef DEBUG
1301 			printf("sbicxfin parity error: l%d i%x w%d\n",
1302 			       len, asr, wait);
1303 /*			return ((unsigned long)buf - (unsigned long)bp); */
1304 #ifdef DDB
1305 			Debugger();
1306 #endif
1307 #endif
1308 		}
1309 		while ((asr & SBIC_ASR_DBR) == 0) {
1310 			if ((asr & SBIC_ASR_INT) || --wait < 0) {
1311 #ifdef DEBUG
1312 				if (sbic_debug) {
1313 	QPRINTF(("sbicxfin fail:{%d} %02x %02x %02x %02x %02x %02x "
1314 	    "%02x %02x %02x %02x\n", len, obp[0], obp[1], obp[2],
1315 	    obp[3], obp[4], obp[5], obp[6], obp[7], obp[8], obp[9]));
1316 					printf("sbicxfin fail: l%d i%x w%d\n",
1317 					    len, asr, wait);
1318 }
1319 #endif
1320 				return len;
1321 			}
1322 
1323 			if( ! asr & SBIC_ASR_BSY ) {
1324 				GET_SBIC_csr(regs, csr);
1325 				CSR_TRACE('<',csr,asr,len);
1326 				QPRINTF(("[CSR%02xASR%02x]", csr, asr));
1327 			}
1328 
1329 /*			DELAY(1);*/
1330 			GET_SBIC_asr (regs, asr);
1331 		}
1332 
1333 		GET_SBIC_data (regs, *buf);
1334 /*		QPRINTF(("asr=%02x, csr=%02x, data=%02x\n", asr, csr, *buf));*/
1335 		buf++;
1336 	}
1337 
1338 	QPRINTF(("sbicxfin {%d} %02x %02x %02x %02x %02x %02x "
1339 	    "%02x %02x %02x %02x\n", len, obp[0], obp[1], obp[2],
1340 	    obp[3], obp[4], obp[5], obp[6], obp[7], obp[8], obp[9]));
1341 
1342 	/* this leaves with one csr to be read */
1343 	return len;
1344 }
1345 
1346 /*
1347  * SCSI 'immediate' command:  issue a command to some SCSI device
1348  * and get back an 'immediate' response (i.e., do programmed xfer
1349  * to get the response data).  'cbuf' is a buffer containing a scsi
1350  * command of length clen bytes.  'buf' is a buffer of length 'len'
1351  * bytes for data.  The transfer direction is determined by the device
1352  * (i.e., by the scsi bus data xfer phase).  If 'len' is zero, the
1353  * command must supply no data.
1354  */
1355 int
1356 sbicicmd(dev, target, lun, cbuf, clen, buf, len)
1357 	struct sbic_softc *dev;
1358 	void *cbuf, *buf;
1359 	int clen, len;
1360 {
1361 	sbic_regmap_t regs;
1362 	u_char phase, csr, asr;
1363 	int wait, i;
1364 	struct sbic_acb *acb;
1365 
1366 #define CSR_LOG_BUF_SIZE 0
1367 #if CSR_LOG_BUF_SIZE
1368 	int bufptr;
1369 	int csrbuf[CSR_LOG_BUF_SIZE];
1370 	bufptr=0;
1371 #endif
1372 
1373 	SBIC_TRACE(dev);
1374 	regs = dev->sc_sbic;
1375 	acb = dev->sc_nexus;
1376 
1377 	/* Make sure pointers are OK */
1378 	dev->sc_last = dev->sc_cur = &acb->sc_pa;
1379 	dev->sc_tcnt = acb->sc_tcnt = 0;
1380 	acb->sc_pa.dc_count = 0; /* No DMA */
1381 	acb->sc_kv.dc_addr = buf;
1382 	acb->sc_kv.dc_count = len;
1383 
1384 #ifdef DEBUG
1385 	routine = 3;
1386 	debug_sbic_regs = regs; /* store this to allow debug calls */
1387 	if( data_pointer_debug > 1 )
1388 		printf("sbicicmd(%d,%d):%d\n", target, lun,
1389 		       acb->sc_kv.dc_count);
1390 #endif
1391 
1392 	/*
1393 	 * set the sbic into non-DMA mode
1394 	 */
1395 	SET_SBIC_control(regs, SBIC_CTL_EDI | SBIC_CTL_IDI /*| SBIC_CTL_HSP*/);
1396 
1397 	dev->sc_stat[0] = 0xff;
1398 	dev->sc_msg[0] = 0xff;
1399 	i = 1; /* pre-load */
1400 
1401 	/* We're stealing the SCSI bus */
1402 	dev->sc_flags |= SBICF_ICMD;
1403 
1404 	do {
1405 		/*
1406 		 * select the SCSI bus (it's an error if bus isn't free)
1407 		 */
1408 		if (!( dev->sc_flags & SBICF_SELECTED )
1409 		    && sbicselectbus(dev, regs, target, lun, dev->sc_scsiaddr)) {
1410 			/* printf("sbicicmd: trying to select busy bus!\n"); */
1411 			dev->sc_flags &= ~SBICF_ICMD;
1412 			return(-1);
1413 		}
1414 
1415 		/*
1416 		 * Wait for a phase change (or error) then let the device sequence
1417 		 * us through the various SCSI phases.
1418 		 */
1419 
1420 		wait = sbic_cmd_wait;
1421 
1422 		asr = GET_SBIC_asr (regs, asr);
1423 		GET_SBIC_csr (regs, csr);
1424 		CSR_TRACE('I',csr,asr,target);
1425 		QPRINTF((">ASR:%02xCSR:%02x<", asr, csr));
1426 
1427 #if CSR_LOG_BUF_SIZE
1428 		csrbuf[bufptr++] = csr;
1429 #endif
1430 
1431 
1432 		switch (csr) {
1433 		case SBIC_CSR_S_XFERRED:
1434 		case SBIC_CSR_DISC:
1435 		case SBIC_CSR_DISC_1:
1436 			dev->sc_flags &= ~SBICF_SELECTED;
1437 			GET_SBIC_cmd_phase (regs, phase);
1438 			if (phase == 0x60) {
1439 				GET_SBIC_tlun (regs, dev->sc_stat[0]);
1440 				i = 0; /* done */
1441 /*				break; */ /* Bypass all the state gobldygook */
1442 			} else {
1443 #ifdef DEBUG
1444 				if(reselect_debug>1)
1445 					printf("sbicicmd: handling disconnect\n");
1446 #endif
1447 				i = SBIC_STATE_DISCONNECT;
1448 			}
1449 			break;
1450 
1451 		case SBIC_CSR_XFERRED|CMD_PHASE:
1452 		case SBIC_CSR_MIS|CMD_PHASE:
1453 		case SBIC_CSR_MIS_1|CMD_PHASE:
1454 		case SBIC_CSR_MIS_2|CMD_PHASE:
1455 			if (sbicxfstart(regs, clen, CMD_PHASE, sbic_cmd_wait))
1456 				if (sbicxfout(regs, clen,
1457 					      cbuf, CMD_PHASE))
1458 					i = sbicabort(dev, regs,"icmd sending cmd");
1459 #if 0
1460 			GET_SBIC_csr(regs, csr); /* Lets us reload tcount */
1461 			WAIT_CIP(regs);
1462 			GET_SBIC_asr(regs, asr);
1463 			CSR_TRACE('I',csr,asr,target);
1464 			if( asr & (SBIC_ASR_BSY|SBIC_ASR_LCI|SBIC_ASR_CIP) )
1465 				printf("next: cmd sent asr %02x, csr %02x\n",
1466 				       asr, csr);
1467 #endif
1468 			break;
1469 
1470 #if 0
1471 		case SBIC_CSR_XFERRED|DATA_OUT_PHASE:
1472 		case SBIC_CSR_XFERRED|DATA_IN_PHASE:
1473 		case SBIC_CSR_MIS|DATA_OUT_PHASE:
1474 		case SBIC_CSR_MIS|DATA_IN_PHASE:
1475 		case SBIC_CSR_MIS_1|DATA_OUT_PHASE:
1476 		case SBIC_CSR_MIS_1|DATA_IN_PHASE:
1477 		case SBIC_CSR_MIS_2|DATA_OUT_PHASE:
1478 		case SBIC_CSR_MIS_2|DATA_IN_PHASE:
1479 			if (acb->sc_kv.dc_count <= 0)
1480 				i = sbicabort(dev, regs, "icmd out of data");
1481 			else {
1482 			  wait = sbic_data_wait;
1483 			  if (sbicxfstart(regs,
1484 					  acb->sc_kv.dc_count,
1485 					  SBIC_PHASE(csr), wait))
1486 			    if (csr & 0x01)
1487 			      /* data in? */
1488 			      i=sbicxfin(regs,
1489 					 acb->sc_kv.dc_count,
1490 					 acb->sc_kv.dc_addr);
1491 			    else
1492 			      i=sbicxfout(regs,
1493 					  acb->sc_kv.dc_count,
1494 					  acb->sc_kv.dc_addr,
1495 					     SBIC_PHASE(csr));
1496 			  acb->sc_kv.dc_addr +=
1497 				  (acb->sc_kv.dc_count - i);
1498 			  acb->sc_kv.dc_count = i;
1499 			  i = 1;
1500 			}
1501 			break;
1502 
1503 #endif
1504 		case SBIC_CSR_XFERRED|STATUS_PHASE:
1505 		case SBIC_CSR_MIS|STATUS_PHASE:
1506 		case SBIC_CSR_MIS_1|STATUS_PHASE:
1507 		case SBIC_CSR_MIS_2|STATUS_PHASE:
1508 			/*
1509 			 * the sbic does the status/cmd-complete reading ok,
1510 			 * so do this with its hi-level commands.
1511 			 */
1512 #ifdef DEBUG
1513 			if(sbic_debug)
1514 				printf("SBICICMD status phase\n");
1515 #endif
1516 			SBIC_TC_PUT(regs, 0);
1517 			SET_SBIC_cmd_phase(regs, 0x46);
1518 			SET_SBIC_cmd(regs, SBIC_CMD_SEL_ATN_XFER);
1519 			break;
1520 
1521 #if THIS_IS_A_RESERVED_STATE
1522 		case BUS_FREE_PHASE:		/* This is not legal */
1523 			if( dev->sc_stat[0] != 0xff )
1524 				goto out;
1525 			break;
1526 #endif
1527 
1528 		default:
1529 			i = sbicnextstate(dev, csr, asr);
1530 		}
1531 
1532 		/*
1533 		 * make sure the last command was taken,
1534 		 * ie. we're not hunting after an ignored command..
1535 		 */
1536 		GET_SBIC_asr(regs, asr);
1537 
1538 		/* tapes may take a loooong time.. */
1539 		while (asr & SBIC_ASR_BSY){
1540 			if(asr & SBIC_ASR_DBR) {
1541 				printf("sbicicmd: Waiting while sbic is jammed, CSR:%02x,ASR:%02x\n",
1542 				       csr,asr);
1543 #ifdef DDB
1544 				Debugger();
1545 #endif
1546 				/* SBIC is jammed */
1547 				/* DUNNO which direction */
1548 				/* Try old direction */
1549 				GET_SBIC_data(regs,i);
1550 				GET_SBIC_asr(regs, asr);
1551 				if( asr & SBIC_ASR_DBR) /* Wants us to write */
1552 					SET_SBIC_data(regs,i);
1553 			}
1554 			GET_SBIC_asr(regs, asr);
1555 		}
1556 
1557 		/*
1558 		 * wait for last command to complete
1559 		 */
1560 		if (asr & SBIC_ASR_LCI) {
1561 			printf("sbicicmd: last command ignored\n");
1562 		}
1563 		else if( i == 1 ) /* Bsy */
1564 			SBIC_WAIT (regs, SBIC_ASR_INT, wait);
1565 
1566 		/*
1567 		 * do it again
1568 		 */
1569 	} while ( i > 0 && dev->sc_stat[0] == 0xff);
1570 
1571 	/* Sometimes we need to do an extra read of the CSR */
1572 	GET_SBIC_csr(regs, csr);
1573 	CSR_TRACE('I',csr,asr,0xff);
1574 
1575 #if CSR_LOG_BUF_SIZE
1576 	if(reselect_debug>1)
1577 		for(i=0; i<bufptr; i++)
1578 			printf("CSR:%02x", csrbuf[i]);
1579 #endif
1580 
1581 #ifdef DEBUG
1582 	if(data_pointer_debug > 1)
1583 		printf("sbicicmd done(%d,%d):%d =%d=\n",
1584 		       dev->target, lun,
1585 		       acb->sc_kv.dc_count,
1586 		       dev->sc_stat[0]);
1587 #endif
1588 
1589 	QPRINTF(("=STS:%02x=", dev->sc_stat[0]));
1590 	dev->sc_flags &= ~SBICF_ICMD;
1591 
1592 	SBIC_TRACE(dev);
1593 	return(dev->sc_stat[0]);
1594 }
1595 
1596 /*
1597  * Finish SCSI xfer command:  After the completion interrupt from
1598  * a read/write operation, sequence through the final phases in
1599  * programmed i/o.  This routine is a lot like sbicicmd except we
1600  * skip (and don't allow) the select, cmd out and data in/out phases.
1601  */
1602 void
1603 sbicxfdone(dev, regs, target)
1604 	struct sbic_softc *dev;
1605 	sbic_regmap_t regs;
1606 	int target;
1607 {
1608 	u_char phase, asr, csr;
1609 	int s;
1610 
1611 	SBIC_TRACE(dev);
1612 	QPRINTF(("{"));
1613 	s = splbio();
1614 
1615 	/*
1616 	 * have the sbic complete on its own
1617 	 */
1618 	SBIC_TC_PUT(regs, 0);
1619 	SET_SBIC_cmd_phase(regs, 0x46);
1620 	SET_SBIC_cmd(regs, SBIC_CMD_SEL_ATN_XFER);
1621 
1622 	do {
1623 		asr = SBIC_WAIT (regs, SBIC_ASR_INT, 0);
1624 		GET_SBIC_csr (regs, csr);
1625 		CSR_TRACE('f',csr,asr,target);
1626 		QPRINTF(("%02x:", csr));
1627 	} while ((csr != SBIC_CSR_DISC) && (csr != SBIC_CSR_DISC_1)
1628 	    && (csr != SBIC_CSR_S_XFERRED));
1629 
1630 	dev->sc_flags &= ~SBICF_SELECTED;
1631 
1632 	GET_SBIC_cmd_phase (regs, phase);
1633 	QPRINTF(("}%02x", phase));
1634 	if (phase == 0x60)
1635 		GET_SBIC_tlun(regs, dev->sc_stat[0]);
1636 	else
1637 		sbicerror(dev, regs, csr);
1638 
1639 	QPRINTF(("=STS:%02x=\n", dev->sc_stat[0]));
1640 	splx(s);
1641 	SBIC_TRACE(dev);
1642 }
1643 
1644 	/*
1645 	 * No DMA chains
1646 	 */
1647 
1648 int
1649 sbicgo(dev, xs)
1650 	struct sbic_softc *dev;
1651 	struct scsipi_xfer *xs;
1652 {
1653 	int i, dmaflags, count, usedma;
1654 	u_char csr, asr, *addr;
1655 	sbic_regmap_t regs;
1656 	struct sbic_acb *acb;
1657 
1658 	SBIC_TRACE(dev);
1659 	dev->target = xs->sc_link->scsipi_scsi.target;
1660 	dev->lun = xs->sc_link->scsipi_scsi.lun;
1661 	acb = dev->sc_nexus;
1662 	regs = dev->sc_sbic;
1663 
1664 	usedma = sbicdmaok(dev, xs);
1665 #ifdef DEBUG
1666 	routine = 1;
1667 	debug_sbic_regs = regs; /* store this to allow debug calls */
1668 	if( data_pointer_debug > 1 )
1669 		printf("sbicgo(%d,%d)\n", dev->target, dev->lun);
1670 #endif
1671 
1672 	/*
1673 	 * set the sbic into DMA mode
1674 	 */
1675 	if( usedma )
1676 		SET_SBIC_control(regs, SBIC_CTL_EDI | SBIC_CTL_IDI |
1677 				 SBIC_MACHINE_DMA_MODE);
1678 	else
1679 		SET_SBIC_control(regs, SBIC_CTL_EDI | SBIC_CTL_IDI);
1680 
1681 
1682 	/*
1683 	 * select the SCSI bus (it's an error if bus isn't free)
1684 	 */
1685 	if (sbicselectbus(dev, regs, dev->target, dev->lun,
1686 	    dev->sc_scsiaddr)) {
1687 		/* printf("sbicgo: Trying to select busy bus!\n"); */
1688 		SBIC_TRACE(dev);
1689 		return(0); /* Not done: needs to be rescheduled */
1690 	}
1691 	dev->sc_stat[0] = 0xff;
1692 
1693 	/*
1694 	 * Calculate DMA chains now
1695 	 */
1696 
1697 	dmaflags = 0;
1698 	if (acb->flags & ACB_DATAIN)
1699 		dmaflags |= DMAGO_READ;
1700 
1701 
1702 	/*
1703 	 * Deal w/bounce buffers.
1704 	 */
1705 
1706 	addr = acb->sc_kv.dc_addr;
1707 	count = acb->sc_kv.dc_count;
1708 	if (count && (char *)kvtop(addr) != acb->sc_pa.dc_addr)	{ /* XXXX check */
1709 		printf("sbic: DMA buffer mapping changed %p->%x\n",
1710 		    acb->sc_pa.dc_addr, kvtop(addr));
1711 #ifdef DDB
1712 		Debugger();
1713 #endif
1714 	}
1715 
1716 #ifdef DEBUG
1717 	++sbicdma_ops;			/* count total DMA operations */
1718 #endif
1719 	if (count && usedma && dev->sc_flags & SBICF_BADDMA &&
1720 	    sbiccheckdmap(addr, count, dev->sc_dmamask)) {
1721 		/*
1722 		 * need to bounce the dma.
1723 		 */
1724 		if (dmaflags & DMAGO_READ) {
1725 			acb->flags |= ACB_BBUF;
1726 			acb->sc_dmausrbuf = addr;
1727 			acb->sc_dmausrlen = count;
1728 			acb->sc_usrbufpa = (u_char *)kvtop(addr);
1729 			if(!dev->sc_tinfo[dev->target].bounce) {
1730 				printf("sbicgo: HELP! no bounce allocated for %d\n",
1731 				       dev->target);
1732 				printf("xfer: (%p->%p,%lx)\n", acb->sc_dmausrbuf,
1733 				       acb->sc_usrbufpa, acb->sc_dmausrlen);
1734 				dev->sc_tinfo[xs->sc_link->scsipi_scsi.target].bounce
1735 					= (char *)alloc_z2mem(MAXPHYS);
1736 				if (isztwomem(dev->sc_tinfo[xs->sc_link->scsipi_scsi.target].bounce))
1737 					printf("alloc ZII target %d bounce pa 0x%x\n",
1738 					       xs->sc_link->scsipi_scsi.target,
1739 					       kvtop(dev->sc_tinfo[xs->sc_link->scsipi_scsi.target].bounce));
1740 				else if (dev->sc_tinfo[xs->sc_link->scsipi_scsi.target].bounce)
1741 					printf("alloc CHIP target %d bounce pa 0x%p\n",
1742 					       xs->sc_link->scsipi_scsi.target,
1743 					       PREP_DMA_MEM(dev->sc_tinfo[xs->sc_link->scsipi_scsi.target].bounce));
1744 
1745 				printf("Allocating %d bounce at %x\n",
1746 				       dev->target,
1747 				       kvtop(dev->sc_tinfo[dev->target].bounce));
1748 			}
1749 		} else {	/* write: copy to dma buffer */
1750 #ifdef DEBUG
1751 			if(data_pointer_debug)
1752 			printf("sbicgo: copying %x bytes to target %d bounce %x\n",
1753 			       count, dev->target,
1754 			       kvtop(dev->sc_tinfo[dev->target].bounce));
1755 #endif
1756 			bcopy (addr, dev->sc_tinfo[dev->target].bounce, count);
1757 		}
1758 		addr = dev->sc_tinfo[dev->target].bounce;/* and use dma buffer */
1759 		acb->sc_kv.dc_addr = addr;
1760 #ifdef DEBUG
1761 		++sbicdma_bounces;		/* count number of bounced */
1762 #endif
1763 	}
1764 
1765 	/*
1766 	 * Allocate the DMA chain
1767 	 */
1768 
1769 	/* Set start KVM addresses */
1770 #if 0
1771 	acb->sc_kv.dc_addr = addr;
1772 	acb->sc_kv.dc_count = count;
1773 #endif
1774 
1775 	/* Mark end of segment */
1776 	acb->sc_tcnt = dev->sc_tcnt = 0;
1777 	acb->sc_pa.dc_count = 0;
1778 
1779 	sbic_load_ptrs(dev, regs, dev->target, dev->lun);
1780 	SBIC_TRACE(dev);
1781 	/* Enable interrupts but don't do any DMA */
1782 	dev->sc_enintr(dev);
1783 	if (usedma) {
1784 		dev->sc_tcnt = dev->sc_dmago(dev, acb->sc_pa.dc_addr,
1785 		    acb->sc_pa.dc_count,
1786 		    dmaflags);
1787 #ifdef DEBUG
1788 		dev->sc_dmatimo = dev->sc_tcnt ? 1 : 0;
1789 #endif
1790         } else
1791 		dev->sc_dmacmd = 0; /* Don't use DMA */
1792 	dev->sc_flags |= SBICF_INDMA;
1793 /*	SBIC_TC_PUT(regs, dev->sc_tcnt); */ /* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX */
1794 	SBIC_TRACE(dev);
1795 	sbic_save_ptrs(dev, regs, dev->target, dev->lun);
1796 
1797 	/*
1798 	 * push the data cache ( I think this won't work (EH))
1799 	 */
1800 #if defined(M68040) || defined(M68060)
1801 	if (mmutype == MMU_68040 && usedma && count) {
1802 		dma_cachectl(addr, count);
1803 		if (((u_int)addr & 0xF) || (((u_int)addr + count) & 0xF))
1804 			dev->sc_flags |= SBICF_DCFLUSH;
1805 	}
1806 #endif
1807 
1808 	/*
1809 	 * enintr() also enables interrupts for the sbic
1810 	 */
1811 #ifdef DEBUG
1812 	if( data_pointer_debug > 1 )
1813 		printf("sbicgo dmago:%d(%p:%lx)\n",
1814 		       dev->target,dev->sc_cur->dc_addr,dev->sc_tcnt);
1815 #if 0
1816 	/*
1817 	 * Hmm - this isn't right:  asr and csr haven't been set yet.
1818 	 */
1819 	debug_asr = asr;
1820 	debug_csr = csr;
1821 #endif
1822 #endif
1823 
1824 	/*
1825 	 * Lets cycle a while then let the interrupt handler take over
1826 	 */
1827 
1828 	asr = GET_SBIC_asr(regs, asr);
1829 	do {
1830 		GET_SBIC_csr(regs, csr);
1831 		CSR_TRACE('g',csr,asr,dev->target);
1832 #ifdef DEBUG
1833 		debug_csr = csr;
1834 		routine = 1;
1835 #endif
1836 		QPRINTF(("go[0x%x]", csr));
1837 
1838 		i = sbicnextstate(dev, csr, asr);
1839 
1840 		WAIT_CIP(regs);
1841 		GET_SBIC_asr(regs, asr);
1842 #ifdef DEBUG
1843 		debug_asr = asr;
1844 #endif
1845 		if(asr & SBIC_ASR_LCI) printf("sbicgo: LCI asr:%02x csr:%02x\n",
1846 					      asr,csr);
1847 	} while( i == SBIC_STATE_RUNNING
1848 		&& asr & (SBIC_ASR_INT|SBIC_ASR_LCI) );
1849 
1850 	CSR_TRACE('g',csr,asr,i<<4);
1851 	SBIC_TRACE(dev);
1852 if (i == SBIC_STATE_DONE && dev->sc_stat[0] == 0xff) printf("sbicgo: done & stat = 0xff\n");
1853 	if (i == SBIC_STATE_DONE && dev->sc_stat[0] != 0xff) {
1854 /*	if( i == SBIC_STATE_DONE && dev->sc_stat[0] ) { */
1855 		/* Did we really finish that fast? */
1856 		return 1;
1857 	}
1858 	return 0;
1859 }
1860 
1861 
1862 int
1863 sbicintr(dev)
1864 	struct sbic_softc *dev;
1865 {
1866 	sbic_regmap_t regs;
1867 	u_char asr, csr;
1868 	int i;
1869 
1870 	regs = dev->sc_sbic;
1871 
1872 	/*
1873 	 * pending interrupt?
1874 	 */
1875 	GET_SBIC_asr (regs, asr);
1876 	if ((asr & SBIC_ASR_INT) == 0)
1877 		return(0);
1878 
1879 	SBIC_TRACE(dev);
1880 	do {
1881 		GET_SBIC_csr(regs, csr);
1882 		CSR_TRACE('i',csr,asr,dev->target);
1883 #ifdef DEBUG
1884 		debug_csr = csr;
1885 		routine = 2;
1886 #endif
1887 		QPRINTF(("intr[0x%x]", csr));
1888 
1889 		i = sbicnextstate(dev, csr, asr);
1890 
1891 		WAIT_CIP(regs);
1892 		GET_SBIC_asr(regs, asr);
1893 #ifdef DEBUG
1894 		debug_asr = asr;
1895 #endif
1896 #if 0
1897 		if(asr & SBIC_ASR_LCI) printf("sbicintr: LCI asr:%02x csr:%02x\n",
1898 					      asr,csr);
1899 #endif
1900 	} while(i == SBIC_STATE_RUNNING &&
1901 		asr & (SBIC_ASR_INT|SBIC_ASR_LCI));
1902 	CSR_TRACE('i',csr,asr,i<<4);
1903 	SBIC_TRACE(dev);
1904 	return(1);
1905 }
1906 
1907 /*
1908  * Run commands and wait for disconnect
1909  */
1910 int
1911 sbicpoll(dev)
1912 	struct sbic_softc *dev;
1913 {
1914 	sbic_regmap_t regs;
1915 	u_char asr, csr;
1916 	int i;
1917 
1918 	SBIC_TRACE(dev);
1919 	regs = dev->sc_sbic;
1920 
1921 	do {
1922 		GET_SBIC_asr (regs, asr);
1923 #ifdef DEBUG
1924 		debug_asr = asr;
1925 #endif
1926 		GET_SBIC_csr(regs, csr);
1927 		CSR_TRACE('p',csr,asr,dev->target);
1928 #ifdef DEBUG
1929 		debug_csr = csr;
1930 		routine = 2;
1931 #endif
1932 		QPRINTF(("poll[0x%x]", csr));
1933 
1934 		i = sbicnextstate(dev, csr, asr);
1935 
1936 		WAIT_CIP(regs);
1937 		GET_SBIC_asr(regs, asr);
1938 		/* tapes may take a loooong time.. */
1939 		while (asr & SBIC_ASR_BSY){
1940 			if(asr & SBIC_ASR_DBR) {
1941 				printf("sbipoll: Waiting while sbic is jammed, CSR:%02x,ASR:%02x\n",
1942 				       csr,asr);
1943 #ifdef DDB
1944 				Debugger();
1945 #endif
1946 				/* SBIC is jammed */
1947 				/* DUNNO which direction */
1948 				/* Try old direction */
1949 				GET_SBIC_data(regs,i);
1950 				GET_SBIC_asr(regs, asr);
1951 				if( asr & SBIC_ASR_DBR) /* Wants us to write */
1952 					SET_SBIC_data(regs,i);
1953 			}
1954 			GET_SBIC_asr(regs, asr);
1955 		}
1956 
1957 		if(asr & SBIC_ASR_LCI) printf("sbicpoll: LCI asr:%02x csr:%02x\n",
1958 					      asr,csr);
1959 		else if( i == 1 ) /* BSY */
1960 			SBIC_WAIT(regs, SBIC_ASR_INT, sbic_cmd_wait);
1961 	} while(i == SBIC_STATE_RUNNING);
1962 	CSR_TRACE('p',csr,asr,i<<4);
1963 	SBIC_TRACE(dev);
1964 	return(1);
1965 }
1966 
1967 /*
1968  * Handle a single msgin
1969  */
1970 
1971 int
1972 sbicmsgin(dev)
1973 	struct sbic_softc *dev;
1974 {
1975 	sbic_regmap_t regs;
1976 	int recvlen;
1977 	u_char asr, csr, *tmpaddr;
1978 
1979 	regs = dev->sc_sbic;
1980 
1981 	dev->sc_msg[0] = 0xff;
1982 	dev->sc_msg[1] = 0xff;
1983 
1984 	GET_SBIC_asr(regs, asr);
1985 #ifdef DEBUG
1986 	if(reselect_debug>1)
1987 		printf("sbicmsgin asr=%02x\n", asr);
1988 #endif
1989 
1990 	sbic_save_ptrs(dev, regs, dev->target, dev->lun);
1991 
1992 	GET_SBIC_selid (regs, csr);
1993 	SET_SBIC_selid (regs, csr | SBIC_SID_FROM_SCSI);
1994 
1995 	SBIC_TC_PUT(regs, 0);
1996 	tmpaddr = dev->sc_msg;
1997 	recvlen = 1;
1998 	do {
1999 		while( recvlen-- ) {
2000 			asr = GET_SBIC_asr(regs, asr);
2001 			GET_SBIC_csr(regs, csr);
2002 			QPRINTF(("sbicmsgin ready to go (csr,asr)=(%02x,%02x)\n",
2003 				 csr, asr));
2004 
2005 			RECV_BYTE(regs, *tmpaddr);
2006 			CSR_TRACE('m',csr,asr,*tmpaddr);
2007 #if 1
2008 			/*
2009 			 * get the command completion interrupt, or we
2010 			 * can't send a new command (LCI)
2011 			 */
2012 			SBIC_WAIT(regs, SBIC_ASR_INT, 0);
2013 			GET_SBIC_csr(regs, csr);
2014 			CSR_TRACE('X',csr,asr,dev->target);
2015 #else
2016 			WAIT_CIP(regs);
2017 			do {
2018 				GET_SBIC_asr(regs, asr);
2019 				csr = 0xff;
2020 				GET_SBIC_csr(regs, csr);
2021 				CSR_TRACE('X',csr,asr,dev->target);
2022 				if( csr == 0xff )
2023 					printf("sbicmsgin waiting: csr %02x asr %02x\n", csr, asr);
2024 			} while( csr == 0xff );
2025 #endif
2026 #ifdef DEBUG
2027 			if(reselect_debug>1)
2028 				printf("sbicmsgin: got %02x csr %02x asr %02x\n",
2029 				       *tmpaddr, csr, asr);
2030 #endif
2031 #if do_parity_check
2032 			if( asr & SBIC_ASR_PE ) {
2033 				printf ("Parity error");
2034 				/* This code simply does not work. */
2035 				WAIT_CIP(regs);
2036 				SET_SBIC_cmd(regs, SBIC_CMD_SET_ATN);
2037 				WAIT_CIP(regs);
2038 				GET_SBIC_asr(regs, asr);
2039 				WAIT_CIP(regs);
2040 				SET_SBIC_cmd(regs, SBIC_CMD_CLR_ACK);
2041 				WAIT_CIP(regs);
2042 				if( !(asr & SBIC_ASR_LCI) )
2043 					/* Target wants to send garbled msg*/
2044 					continue;
2045 				printf("--fixing\n");
2046 				/* loop until a msgout phase occurs on target */
2047 				while(csr & 0x07 != MESG_OUT_PHASE) {
2048 					while( asr & SBIC_ASR_BSY &&
2049 					      !(asr & SBIC_ASR_DBR|SBIC_ASR_INT) )
2050 						GET_SBIC_asr(regs, asr);
2051 					if( asr & SBIC_ASR_DBR )
2052 						panic("msgin: jammed again!\n");
2053 					GET_SBIC_csr(regs, csr);
2054 					CSR_TRACE('e',csr,asr,dev->target);
2055 					if( csr & 0x07 != MESG_OUT_PHASE ) {
2056 						sbicnextstate(dev, csr, asr);
2057 						sbic_save_ptrs(dev, regs,
2058 							       dev->target,
2059 							       dev->lun);
2060 					}
2061 				}
2062 				/* Should be msg out by now */
2063 				SEND_BYTE(regs, MSG_PARITY_ERROR);
2064 			}
2065 			else
2066 #endif
2067 				tmpaddr++;
2068 
2069 			if(recvlen) {
2070 				/* Clear ACK */
2071 				WAIT_CIP(regs);
2072 				GET_SBIC_asr(regs, asr);
2073 				GET_SBIC_csr(regs, csr);
2074 				CSR_TRACE('X',csr,asr,dev->target);
2075 				QPRINTF(("sbicmsgin pre byte CLR_ACK (csr,asr)=(%02x,%02x)\n",
2076 					 csr, asr));
2077 				SET_SBIC_cmd(regs, SBIC_CMD_CLR_ACK);
2078 				SBIC_WAIT(regs, SBIC_ASR_INT, 0);
2079 			}
2080 
2081 		};
2082 
2083 		if(dev->sc_msg[0] == 0xff) {
2084 			printf("sbicmsgin: sbic swallowed our message\n");
2085 			break;
2086 		}
2087 #ifdef DEBUG
2088 		if (sync_debug)
2089 			printf("msgin done csr 0x%x asr 0x%x msg 0x%x\n",
2090 			       csr, asr, dev->sc_msg[0]);
2091 #endif
2092 		/*
2093 		 * test whether this is a reply to our sync
2094 		 * request
2095 		 */
2096 		if (MSG_ISIDENTIFY(dev->sc_msg[0])) {
2097 			QPRINTF(("IFFY"));
2098 #if 0
2099 			/* There is an implied load-ptrs here */
2100 			sbic_load_ptrs(dev, regs, dev->target, dev->lun);
2101 #endif
2102 			/* Got IFFY msg -- ack it */
2103 		} else if (dev->sc_msg[0] == MSG_REJECT
2104 			   && dev->sc_sync[dev->target].state == SYNC_SENT) {
2105 			QPRINTF(("REJECT of SYN"));
2106 #ifdef DEBUG
2107 			if (sync_debug)
2108 				printf("target %d rejected sync, going async\n",
2109 				       dev->target);
2110 #endif
2111 			dev->sc_sync[dev->target].period = sbic_min_period;
2112 			dev->sc_sync[dev->target].offset = 0;
2113 			dev->sc_sync[dev->target].state = SYNC_DONE;
2114 			SET_SBIC_syn(regs,
2115 				     SBIC_SYN(dev->sc_sync[dev->target].offset,
2116 					      dev->sc_sync[dev->target].period));
2117 		} else if ((dev->sc_msg[0] == MSG_REJECT)) {
2118 			QPRINTF(("REJECT"));
2119 			/*
2120 			 * we'll never REJECt a REJECT message..
2121 			 */
2122 		} else if ((dev->sc_msg[0] == MSG_SAVE_DATA_PTR)) {
2123 			QPRINTF(("MSG_SAVE_DATA_PTR"));
2124 			/*
2125 			 * don't reject this either.
2126 			 */
2127 		} else if ((dev->sc_msg[0] == MSG_DISCONNECT)) {
2128 			QPRINTF(("DISCONNECT"));
2129 #ifdef DEBUG
2130 			if( reselect_debug>1 && dev->sc_msg[0] == MSG_DISCONNECT )
2131 				printf("sbicmsgin: got disconnect msg %s\n",
2132 				       (dev->sc_flags & SBICF_ICMD)?"rejecting":"");
2133 #endif
2134 			if( dev->sc_flags & SBICF_ICMD ) {
2135 				/* We're in immediate mode. Prevent disconnects. */
2136 				/* prepare to reject the message, NACK */
2137 				SET_SBIC_cmd(regs, SBIC_CMD_SET_ATN);
2138 				WAIT_CIP(regs);
2139 			}
2140 		} else if (dev->sc_msg[0] == MSG_CMD_COMPLETE ) {
2141 			QPRINTF(("CMD_COMPLETE"));
2142 			/* !! KLUDGE ALERT !! quite a few drives don't seem to
2143 			 * really like the current way of sending the
2144 			 * sync-handshake together with the ident-message, and
2145 			 * they react by sending command-complete and
2146 			 * disconnecting right after returning the valid sync
2147 			 * handshake. So, all I can do is reselect the drive,
2148 			 * and hope it won't disconnect again. I don't think
2149 			 * this is valid behavior, but I can't help fixing a
2150 			 * problem that apparently exists.
2151 			 *
2152 			 * Note: we should not get here on `normal' command
2153 			 * completion, as that condition is handled by the
2154 			 * high-level sel&xfer resume command used to walk
2155 			 * thru status/cc-phase.
2156 			 */
2157 
2158 #ifdef DEBUG
2159 			if (sync_debug)
2160 				printf ("GOT MSG %d! target %d acting weird.."
2161 					" waiting for disconnect...\n",
2162 					dev->sc_msg[0], dev->target);
2163 #endif
2164 			/* Check to see if sbic is handling this */
2165 			GET_SBIC_asr(regs, asr);
2166 			if(asr & SBIC_ASR_BSY)
2167 				return SBIC_STATE_RUNNING;
2168 
2169 			/* Let's try this: Assume it works and set status to 00 */
2170 			dev->sc_stat[0] = 0;
2171 		} else if (dev->sc_msg[0] == MSG_EXT_MESSAGE
2172 			   && tmpaddr == &dev->sc_msg[1]) {
2173 			QPRINTF(("ExtMSG\n"));
2174 			/* Read in whole extended message */
2175 			SET_SBIC_cmd(regs, SBIC_CMD_CLR_ACK);
2176 			SBIC_WAIT(regs, SBIC_ASR_INT, 0);
2177 			GET_SBIC_asr(regs, asr);
2178 			GET_SBIC_csr(regs, csr);
2179 			QPRINTF(("CLR ACK asr %02x, csr %02x\n", asr, csr));
2180 			RECV_BYTE(regs, *tmpaddr);
2181 			CSR_TRACE('x',csr,asr,*tmpaddr);
2182 			/* Wait for command completion IRQ */
2183 			SBIC_WAIT(regs, SBIC_ASR_INT, 0);
2184 			recvlen = *tmpaddr++;
2185 			QPRINTF(("Recving ext msg, asr %02x csr %02x len %02x\n",
2186 			       asr, csr, recvlen));
2187 		} else if (dev->sc_msg[0] == MSG_EXT_MESSAGE && dev->sc_msg[1] == 3
2188 			   && dev->sc_msg[2] == MSG_SYNC_REQ) {
2189 			QPRINTF(("SYN"));
2190 			dev->sc_sync[dev->target].period =
2191 				sbicfromscsiperiod(dev,
2192 						   regs, dev->sc_msg[3]);
2193 			dev->sc_sync[dev->target].offset = dev->sc_msg[4];
2194 			dev->sc_sync[dev->target].state = SYNC_DONE;
2195 			SET_SBIC_syn(regs,
2196 				     SBIC_SYN(dev->sc_sync[dev->target].offset,
2197 					      dev->sc_sync[dev->target].period));
2198 			printf("%s: target %d now synchronous,"
2199 			       " period=%dns, offset=%d.\n",
2200 			       dev->sc_dev.dv_xname, dev->target,
2201 			       dev->sc_msg[3] * 4, dev->sc_msg[4]);
2202 		} else {
2203 #ifdef DEBUG
2204 			if (sbic_debug || sync_debug)
2205 				printf ("sbicmsgin: Rejecting message 0x%02x\n",
2206 					dev->sc_msg[0]);
2207 #endif
2208 			/* prepare to reject the message, NACK */
2209 			SET_SBIC_cmd(regs, SBIC_CMD_SET_ATN);
2210 			WAIT_CIP(regs);
2211 		}
2212 		/* Clear ACK */
2213 		WAIT_CIP(regs);
2214 		GET_SBIC_asr(regs, asr);
2215 		GET_SBIC_csr(regs, csr);
2216 		CSR_TRACE('X',csr,asr,dev->target);
2217 		QPRINTF(("sbicmsgin pre CLR_ACK (csr,asr)=(%02x,%02x)%d\n",
2218 			 csr, asr, recvlen));
2219 		SET_SBIC_cmd(regs, SBIC_CMD_CLR_ACK);
2220 		SBIC_WAIT(regs, SBIC_ASR_INT, 0);
2221 	}
2222 #if 0
2223 	while((csr == SBIC_CSR_MSGIN_W_ACK)
2224 	      || (SBIC_PHASE(csr) == MESG_IN_PHASE));
2225 #else
2226 	while (recvlen>0);
2227 #endif
2228 
2229 	QPRINTF(("sbicmsgin finished: csr %02x, asr %02x\n",csr, asr));
2230 
2231 	/* Should still have one CSR to read */
2232 	return SBIC_STATE_RUNNING;
2233 }
2234 
2235 
2236 /*
2237  * sbicnextstate()
2238  * return:
2239  *		0  == done
2240  *		1  == working
2241  *		2  == disconnected
2242  *		-1 == error
2243  */
2244 int
2245 sbicnextstate(dev, csr, asr)
2246 	struct sbic_softc *dev;
2247 	u_char csr, asr;
2248 {
2249 	sbic_regmap_t regs;
2250 	struct sbic_acb *acb;
2251 	int i, newtarget, newlun, wait;
2252 #if 0
2253 	unsigned tcnt;
2254 #endif
2255 
2256 	i = 0;
2257 	SBIC_TRACE(dev);
2258 	regs = dev->sc_sbic;
2259 	acb = dev->sc_nexus;
2260 
2261 	QPRINTF(("next[%02x,%02x]",asr,csr));
2262 
2263 	switch (csr) {
2264 	case SBIC_CSR_XFERRED|CMD_PHASE:
2265 	case SBIC_CSR_MIS|CMD_PHASE:
2266 	case SBIC_CSR_MIS_1|CMD_PHASE:
2267 	case SBIC_CSR_MIS_2|CMD_PHASE:
2268 		sbic_save_ptrs(dev, regs, dev->target, dev->lun);
2269 		if (sbicxfstart(regs, acb->clen, CMD_PHASE, sbic_cmd_wait))
2270 			if (sbicxfout(regs, acb->clen,
2271 				      &acb->cmd, CMD_PHASE))
2272 				goto abort;
2273 		break;
2274 
2275 	case SBIC_CSR_XFERRED|STATUS_PHASE:
2276 	case SBIC_CSR_MIS|STATUS_PHASE:
2277 	case SBIC_CSR_MIS_1|STATUS_PHASE:
2278 	case SBIC_CSR_MIS_2|STATUS_PHASE:
2279 		/*
2280 		 * this should be the normal i/o completion case.
2281 		 * get the status & cmd complete msg then let the
2282 		 * device driver look at what happened.
2283 		 */
2284 		sbicxfdone(dev,regs,dev->target);
2285 		/*
2286 		 * check for overlapping cache line, flush if so
2287 		 */
2288 #if defined(M68040) || defined(M68060)
2289 		if (dev->sc_flags & SBICF_DCFLUSH) {
2290 #if 0
2291 			printf("sbic: 68040/68060 DMA cache flush needs"
2292 			    "fixing? %x:%x\n",
2293 			    dev->sc_xs->data, dev->sc_xs->datalen);
2294 #endif
2295 		}
2296 #endif
2297 #ifdef DEBUG
2298 		if( data_pointer_debug > 1 )
2299 			printf("next dmastop: %d(%p:%lx)\n",
2300 			       dev->target,dev->sc_cur->dc_addr,dev->sc_tcnt);
2301 		dev->sc_dmatimo = 0;
2302 #endif
2303 		dev->sc_dmastop(dev); /* was dmafree */
2304 		if (acb->flags & ACB_BBUF) {
2305 			if ((u_char *)kvtop(acb->sc_dmausrbuf) != acb->sc_usrbufpa)
2306 				printf("%s: WARNING - buffer mapping changed %p->%x\n",
2307 				    dev->sc_dev.dv_xname, acb->sc_usrbufpa,
2308 				    kvtop(acb->sc_dmausrbuf));
2309 #ifdef DEBUG
2310 			if(data_pointer_debug)
2311 			printf("sbicgo:copying %lx bytes from target %d bounce %x\n",
2312 			       acb->sc_dmausrlen,
2313 			       dev->target,
2314 			       kvtop(dev->sc_tinfo[dev->target].bounce));
2315 #endif
2316 			bcopy(dev->sc_tinfo[dev->target].bounce,
2317 			      acb->sc_dmausrbuf,
2318 			      acb->sc_dmausrlen);
2319 		}
2320 		dev->sc_flags &= ~(SBICF_INDMA | SBICF_DCFLUSH);
2321 		sbic_scsidone(acb, dev->sc_stat[0]);
2322 		SBIC_TRACE(dev);
2323 		return SBIC_STATE_DONE;
2324 
2325 	case SBIC_CSR_XFERRED|DATA_OUT_PHASE:
2326 	case SBIC_CSR_XFERRED|DATA_IN_PHASE:
2327 	case SBIC_CSR_MIS|DATA_OUT_PHASE:
2328 	case SBIC_CSR_MIS|DATA_IN_PHASE:
2329 	case SBIC_CSR_MIS_1|DATA_OUT_PHASE:
2330 	case SBIC_CSR_MIS_1|DATA_IN_PHASE:
2331 	case SBIC_CSR_MIS_2|DATA_OUT_PHASE:
2332 	case SBIC_CSR_MIS_2|DATA_IN_PHASE:
2333 		if( dev->sc_xs->xs_control & XS_CTL_POLL || dev->sc_flags & SBICF_ICMD
2334 		   || acb->sc_dmacmd == 0 ) {
2335 			/* Do PIO */
2336 			SET_SBIC_control(regs, SBIC_CTL_EDI | SBIC_CTL_IDI);
2337 			if (acb->sc_kv.dc_count <= 0) {
2338 				printf("sbicnextstate:xfer count %d asr%x csr%x\n",
2339 				       acb->sc_kv.dc_count, asr, csr);
2340 				goto abort;
2341 			}
2342 			wait = sbic_data_wait;
2343 			if( sbicxfstart(regs,
2344 					acb->sc_kv.dc_count,
2345 					SBIC_PHASE(csr), wait)) {
2346 				if( SBIC_PHASE(csr) == DATA_IN_PHASE )
2347 					/* data in? */
2348 					i=sbicxfin(regs,
2349 						   acb->sc_kv.dc_count,
2350 						   acb->sc_kv.dc_addr);
2351 				else
2352 					i=sbicxfout(regs,
2353 						    acb->sc_kv.dc_count,
2354 						    acb->sc_kv.dc_addr,
2355 						    SBIC_PHASE(csr));
2356 			}
2357 			acb->sc_kv.dc_addr +=
2358 				(acb->sc_kv.dc_count - i);
2359 			acb->sc_kv.dc_count = i;
2360 		} else {
2361 			if (acb->sc_kv.dc_count <= 0) {
2362 				printf("sbicnextstate:xfer count %d asr%x csr%x\n",
2363 				       acb->sc_kv.dc_count, asr, csr);
2364 				goto abort;
2365 			}
2366 			/*
2367 			 * do scatter-gather dma
2368 			 * hacking the controller chip, ouch..
2369 			 */
2370 			SET_SBIC_control(regs, SBIC_CTL_EDI | SBIC_CTL_IDI |
2371 					 SBIC_MACHINE_DMA_MODE);
2372 			/*
2373 			 * set next dma addr and dec count
2374 			 */
2375 #if 0
2376 			SBIC_TC_GET(regs, tcnt);
2377 			dev->sc_cur->dc_count -= ((dev->sc_tcnt - tcnt) >> 1);
2378 			dev->sc_cur->dc_addr += (dev->sc_tcnt - tcnt);
2379 			dev->sc_tcnt = acb->sc_tcnt = tcnt;
2380 #else
2381 			sbic_save_ptrs(dev, regs, dev->target, dev->lun);
2382 			sbic_load_ptrs(dev, regs, dev->target, dev->lun);
2383 #endif
2384 #ifdef DEBUG
2385 			if( data_pointer_debug > 1 )
2386 				printf("next dmanext: %d(%p:%lx)\n",
2387 				       dev->target,dev->sc_cur->dc_addr,
2388 				       dev->sc_tcnt);
2389 			dev->sc_dmatimo = 1;
2390 #endif
2391 			dev->sc_tcnt = dev->sc_dmanext(dev);
2392 			SBIC_TC_PUT(regs, (unsigned)dev->sc_tcnt);
2393 			SET_SBIC_cmd(regs, SBIC_CMD_XFER_INFO);
2394 			dev->sc_flags |= SBICF_INDMA;
2395 		}
2396 		break;
2397 
2398 	case SBIC_CSR_XFERRED|MESG_IN_PHASE:
2399 	case SBIC_CSR_MIS|MESG_IN_PHASE:
2400 	case SBIC_CSR_MIS_1|MESG_IN_PHASE:
2401 	case SBIC_CSR_MIS_2|MESG_IN_PHASE:
2402 		SBIC_TRACE(dev);
2403 		return sbicmsgin(dev);
2404 
2405 	case SBIC_CSR_MSGIN_W_ACK:
2406 		SET_SBIC_cmd(regs, SBIC_CMD_CLR_ACK); /* Dunno what I'm ACKing */
2407 		printf("Acking unknown msgin CSR:%02x",csr);
2408 		break;
2409 
2410 	case SBIC_CSR_XFERRED|MESG_OUT_PHASE:
2411 	case SBIC_CSR_MIS|MESG_OUT_PHASE:
2412 	case SBIC_CSR_MIS_1|MESG_OUT_PHASE:
2413 	case SBIC_CSR_MIS_2|MESG_OUT_PHASE:
2414 #ifdef DEBUG
2415 		if (sync_debug)
2416 			printf ("sending REJECT msg to last msg.\n");
2417 #endif
2418 
2419 		sbic_save_ptrs(dev, regs, dev->target, dev->lun);
2420 		/*
2421 		 * should only get here on reject,
2422 		 * since it's always US that
2423 		 * initiate a sync transfer
2424 		 */
2425 		SEND_BYTE(regs, MSG_REJECT);
2426 		WAIT_CIP(regs);
2427 		if( asr & (SBIC_ASR_BSY|SBIC_ASR_LCI|SBIC_ASR_CIP) )
2428 			printf("next: REJECT sent asr %02x\n", asr);
2429 		SBIC_TRACE(dev);
2430 		return SBIC_STATE_RUNNING;
2431 
2432 	case SBIC_CSR_DISC:
2433 	case SBIC_CSR_DISC_1:
2434 		dev->sc_flags &= ~(SBICF_INDMA|SBICF_SELECTED);
2435 
2436 		/* Try to schedule another target */
2437 #ifdef DEBUG
2438 		if(reselect_debug>1)
2439 			printf("sbicnext target %d disconnected\n", dev->target);
2440 #endif
2441 		TAILQ_INSERT_HEAD(&dev->nexus_list, acb, chain);
2442 		++dev->sc_tinfo[dev->target].dconns;
2443 		dev->sc_nexus = NULL;
2444 		dev->sc_xs = NULL;
2445 
2446 		if( acb->xs->xs_control & XS_CTL_POLL
2447 		   || (dev->sc_flags & SBICF_ICMD)
2448 		   || !sbic_parallel_operations ) {
2449 			SBIC_TRACE(dev);
2450 			return SBIC_STATE_DISCONNECT;
2451 		}
2452 		sbic_sched(dev);
2453 		SBIC_TRACE(dev);
2454 		return SBIC_STATE_DISCONNECT;
2455 
2456 	case SBIC_CSR_RSLT_NI:
2457 	case SBIC_CSR_RSLT_IFY:
2458 		GET_SBIC_rselid(regs, newtarget);
2459 		/* check SBIC_RID_SIV? */
2460 		newtarget &= SBIC_RID_MASK;
2461 		if (csr == SBIC_CSR_RSLT_IFY) {
2462 			/* Read IFY msg to avoid lockup */
2463 			GET_SBIC_data(regs, newlun);
2464 			WAIT_CIP(regs);
2465 			newlun &= SBIC_TLUN_MASK;
2466 			CSR_TRACE('r',csr,asr,newtarget);
2467 		} else {
2468 			/* Need to get IFY message */
2469 			for (newlun = 256; newlun; --newlun) {
2470 				GET_SBIC_asr(regs, asr);
2471 				if (asr & SBIC_ASR_INT)
2472 					break;
2473 				delay(1);
2474 			}
2475 			newlun = 0;	/* XXXX */
2476 			if ((asr & SBIC_ASR_INT) == 0) {
2477 #ifdef DEBUG
2478 				if (reselect_debug)
2479 					printf("RSLT_NI - no IFFY message? asr %x\n", asr);
2480 #endif
2481 			} else {
2482 				GET_SBIC_csr(regs,csr);
2483 				CSR_TRACE('n',csr,asr,newtarget);
2484 				if (csr == (SBIC_CSR_MIS | MESG_IN_PHASE) ||
2485 				    csr == (SBIC_CSR_MIS_1 | MESG_IN_PHASE) ||
2486 				    csr == (SBIC_CSR_MIS_2 | MESG_IN_PHASE)) {
2487 					sbicmsgin(dev);
2488 					newlun = dev->sc_msg[0] & 7;
2489 				} else {
2490 					printf("RSLT_NI - not MESG_IN_PHASE %x\n",
2491 					    csr);
2492 				}
2493 			}
2494 		}
2495 #ifdef DEBUG
2496 		if(reselect_debug>1 || (reselect_debug && csr==SBIC_CSR_RSLT_NI))
2497 			printf("sbicnext: reselect %s from targ %d lun %d\n",
2498 			    csr == SBIC_CSR_RSLT_NI ? "NI" : "IFY",
2499 			    newtarget, newlun);
2500 #endif
2501 		if (dev->sc_nexus) {
2502 #ifdef DEBUG
2503 			if (reselect_debug > 1)
2504 				printf("%s: reselect %s with active command\n",
2505 				    dev->sc_dev.dv_xname,
2506 				    csr == SBIC_CSR_RSLT_NI ? "NI" : "IFY");
2507 #ifdef DDB
2508 /*			Debugger();*/
2509 #endif
2510 #endif
2511 			TAILQ_INSERT_HEAD(&dev->ready_list, dev->sc_nexus, chain);
2512 			dev->sc_tinfo[dev->target].lubusy &= ~(1 << dev->lun);
2513 			dev->sc_nexus = NULL;
2514 			dev->sc_xs = NULL;
2515 		}
2516 		/* Reload sync values for this target */
2517 		if (dev->sc_sync[newtarget].state == SYNC_DONE)
2518 			SET_SBIC_syn(regs, SBIC_SYN (dev->sc_sync[newtarget].offset,
2519 			    dev->sc_sync[newtarget].period));
2520 		else
2521 			SET_SBIC_syn(regs, SBIC_SYN (0, sbic_min_period));
2522 		for (acb = dev->nexus_list.tqh_first; acb;
2523 		    acb = acb->chain.tqe_next) {
2524 			if (acb->xs->sc_link->scsipi_scsi.target != newtarget ||
2525 			    acb->xs->sc_link->scsipi_scsi.lun != newlun)
2526 				continue;
2527 			TAILQ_REMOVE(&dev->nexus_list, acb, chain);
2528 			dev->sc_nexus = acb;
2529 			dev->sc_xs = acb->xs;
2530 			dev->sc_flags |= SBICF_SELECTED;
2531 			dev->target = newtarget;
2532 			dev->lun = newlun;
2533 			break;
2534 		}
2535 		if (acb == NULL) {
2536 			printf("%s: reselect %s targ %d not in nexus_list %p\n",
2537 			    dev->sc_dev.dv_xname,
2538 			    csr == SBIC_CSR_RSLT_NI ? "NI" : "IFY", newtarget,
2539 			    &dev->nexus_list.tqh_first);
2540 			panic("bad reselect in sbic");
2541 		}
2542 		if (csr == SBIC_CSR_RSLT_IFY)
2543 			SET_SBIC_cmd(regs, SBIC_CMD_CLR_ACK);
2544 		break;
2545 
2546 	default:
2547         abort:
2548 		/*
2549 		 * Something unexpected happened -- deal with it.
2550 		 */
2551 		printf("sbicnextstate: aborting csr %02x asr %02x\n", csr, asr);
2552 #ifdef DDB
2553 		Debugger();
2554 #endif
2555 #ifdef DEBUG
2556 		if( data_pointer_debug > 1 )
2557 			printf("next dmastop: %d(%p:%lx)\n",
2558 			       dev->target,dev->sc_cur->dc_addr,dev->sc_tcnt);
2559 		dev->sc_dmatimo = 0;
2560 #endif
2561 		dev->sc_dmastop(dev);
2562 		SET_SBIC_control(regs, SBIC_CTL_EDI | SBIC_CTL_IDI);
2563 		sbicerror(dev, regs, csr);
2564 		sbicabort(dev, regs, "next");
2565 		if (dev->sc_flags & SBICF_INDMA) {
2566 			/*
2567 			 * check for overlapping cache line, flush if so
2568 			 */
2569 #if defined(M68040) || defined(M68060)
2570 			if (dev->sc_flags & SBICF_DCFLUSH) {
2571 #if 0
2572 				printf("sbic: 68040/060 DMA cache flush needs"
2573 				    "fixing? %x:%x\n",
2574 				    dev->sc_xs->data, dev->sc_xs->datalen);
2575 #endif
2576 			}
2577 #endif
2578 			dev->sc_flags &=
2579 				~(SBICF_INDMA | SBICF_DCFLUSH);
2580 #ifdef DEBUG
2581 			if( data_pointer_debug > 1 )
2582 				printf("next dmastop: %d(%p:%lx)\n",
2583 				    dev->target,dev->sc_cur->dc_addr,dev->sc_tcnt);
2584 			dev->sc_dmatimo = 0;
2585 #endif
2586 			dev->sc_dmastop(dev);
2587 			sbic_scsidone(acb, -1);
2588 		}
2589 		SBIC_TRACE(dev);
2590                 return SBIC_STATE_ERROR;
2591 	}
2592 
2593 	SBIC_TRACE(dev);
2594 	return(SBIC_STATE_RUNNING);
2595 }
2596 
2597 
2598 /*
2599  * Check if DMA can not be used with specified buffer
2600  */
2601 
2602 int
2603 sbiccheckdmap(bp, len, mask)
2604 	void *bp;
2605 	u_long len, mask;
2606 {
2607 	u_char *buffer;
2608 	u_long phy_buf;
2609 	u_long phy_len;
2610 
2611 	buffer = bp;
2612 
2613 	if (len == 0)
2614 		return(0);
2615 
2616 	while (len) {
2617 		phy_buf = kvtop(buffer);
2618 		if (len < (phy_len = NBPG - ((int) buffer & PGOFSET)))
2619 			phy_len = len;
2620 		if (phy_buf & mask)
2621 			return(1);
2622 		buffer += phy_len;
2623 		len -= phy_len;
2624 	}
2625 	return(0);
2626 }
2627 
2628 int
2629 sbictoscsiperiod(dev, regs, a)
2630 	struct sbic_softc *dev;
2631 	sbic_regmap_t regs;
2632 	int a;
2633 {
2634 	unsigned int fs;
2635 
2636 	/*
2637 	 * cycle = DIV / (2*CLK)
2638 	 * DIV = FS+2
2639 	 * best we can do is 200ns at 20Mhz, 2 cycles
2640 	 */
2641 
2642 	GET_SBIC_myid(regs,fs);
2643 	fs = (fs >>6) + 2;		/* DIV */
2644 	fs = (fs * 10000) / (dev->sc_clkfreq<<1);	/* Cycle, in ns */
2645 	if (a < 2) a = 8;		/* map to Cycles */
2646 	return ((fs*a)>>2);		/* in 4 ns units */
2647 }
2648 
2649 int
2650 sbicfromscsiperiod(dev, regs, p)
2651 	struct sbic_softc *dev;
2652 	sbic_regmap_t regs;
2653 	int p;
2654 {
2655 	register unsigned int fs, ret;
2656 
2657 	/* Just the inverse of the above */
2658 
2659 	GET_SBIC_myid(regs,fs);
2660 	fs = (fs >>6) + 2;		/* DIV */
2661 	fs = (fs * 10000) / (dev->sc_clkfreq<<1);   /* Cycle, in ns */
2662 
2663 	ret = p << 2;			/* in ns units */
2664 	ret = ret / fs;			/* in Cycles */
2665 	if (ret < sbic_min_period)
2666 		return(sbic_min_period);
2667 
2668 	/* verify rounding */
2669 	if (sbictoscsiperiod(dev, regs, ret) < p)
2670 		ret++;
2671 	return (ret >= 8) ? 0 : ret;
2672 }
2673 
2674 #ifdef DEBUG
2675 
2676 void
2677 sbicdumpstate()
2678 {
2679 	u_char csr, asr;
2680 
2681 	GET_SBIC_asr(debug_sbic_regs,asr);
2682 	GET_SBIC_csr(debug_sbic_regs,csr);
2683 	printf("%s: asr:csr(%02x:%02x)->(%02x:%02x)\n",
2684 	       (routine==1)?"sbicgo":
2685 	       (routine==2)?"sbicintr":
2686 	       (routine==3)?"sbicicmd":
2687 	       (routine==4)?"sbicnext":"unknown",
2688 	       debug_asr, debug_csr, asr, csr);
2689 
2690 }
2691 
2692 void
2693 sbictimeout(dev)
2694 	struct sbic_softc *dev;
2695 {
2696 	int s, asr;
2697 
2698 	s = splbio();
2699 	if (dev->sc_dmatimo) {
2700 		if (dev->sc_dmatimo > 1) {
2701 			printf("%s: dma timeout #%d\n",
2702 			    dev->sc_dev.dv_xname, dev->sc_dmatimo - 1);
2703 			GET_SBIC_asr(dev->sc_sbic, asr);
2704 			if( asr & SBIC_ASR_INT ) {
2705 				/* We need to service a missed IRQ */
2706 				printf("Servicing a missed int:(%02x,%02x)->(%02x,??)\n",
2707 				    debug_asr, debug_csr, asr);
2708 				sbicintr(dev);
2709 			}
2710 			sbicdumpstate();
2711 		}
2712 		dev->sc_dmatimo++;
2713 	}
2714 	splx(s);
2715 	timeout((void *)sbictimeout, dev, 30 * hz);
2716 }
2717 
2718 void
2719 sbic_dump_acb(acb)
2720 	struct sbic_acb *acb;
2721 {
2722 	u_char *b = (u_char *) &acb->cmd;
2723 	int i;
2724 
2725 	printf("acb@%p ", acb);
2726 	if (acb->xs == NULL) {
2727 		printf("<unused>\n");
2728 		return;
2729 	}
2730 	printf("(%d:%d) flags %2x clen %2d cmd ",
2731 		acb->xs->sc_link->scsipi_scsi.target,
2732 	    acb->xs->sc_link->scsipi_scsi.lun, acb->flags, acb->clen);
2733 	for (i = acb->clen; i; --i)
2734 		printf(" %02x", *b++);
2735 	printf("\n");
2736 	printf("  xs: %8p data %8p:%04x ", acb->xs, acb->xs->data,
2737 	    acb->xs->datalen);
2738 	printf("va %8p:%04x ", acb->sc_kv.dc_addr, acb->sc_kv.dc_count);
2739 	printf("pa %8p:%04x tcnt %lx\n", acb->sc_pa.dc_addr, acb->sc_pa.dc_count,
2740 	    acb->sc_tcnt);
2741 }
2742 
2743 void
2744 sbic_dump(dev)
2745 	struct sbic_softc *dev;
2746 {
2747 	sbic_regmap_t regs;
2748 	u_char csr, asr;
2749 	struct sbic_acb *acb;
2750 	int s;
2751 	int i;
2752 
2753 	s = splbio();
2754 	regs = dev->sc_sbic;
2755 #if CSR_TRACE_SIZE
2756 	printf("csr trace: ");
2757 	i = csr_traceptr;
2758 	do {
2759 		printf("%c%02x%02x%02x ", csr_trace[i].whr,
2760 		    csr_trace[i].csr, csr_trace[i].asr, csr_trace[i].xtn);
2761 		switch(csr_trace[i].whr) {
2762 		case 'g':
2763 			printf("go "); break;
2764 		case 's':
2765 			printf("select "); break;
2766 		case 'y':
2767 			printf("select+ "); break;
2768 		case 'i':
2769 			printf("intr "); break;
2770 		case 'f':
2771 			printf("finish "); break;
2772 		case '>':
2773 			printf("out "); break;
2774 		case '<':
2775 			printf("in "); break;
2776 		case 'm':
2777 			printf("msgin "); break;
2778 		case 'x':
2779 			printf("msginx "); break;
2780 		case 'X':
2781 			printf("msginX "); break;
2782 		case 'r':
2783 			printf("reselect "); break;
2784 		case 'I':
2785 			printf("icmd "); break;
2786 		case 'a':
2787 			printf("abort "); break;
2788 		default:
2789 			printf("? ");
2790 		}
2791 		switch(csr_trace[i].csr) {
2792 		case 0x11:
2793 			printf("INITIATOR"); break;
2794 		case 0x16:
2795 			printf("S_XFERRED"); break;
2796 		case 0x20:
2797 			printf("MSGIN_ACK"); break;
2798 		case 0x41:
2799 			printf("DISC"); break;
2800 		case 0x42:
2801 			printf("SEL_TIMEO"); break;
2802 		case 0x80:
2803 			printf("RSLT_NI"); break;
2804 		case 0x81:
2805 			printf("RSLT_IFY"); break;
2806 		case 0x85:
2807 			printf("DISC_1"); break;
2808 		case 0x18: case 0x19: case 0x1a:
2809 		case 0x1b: case 0x1e: case 0x1f:
2810 		case 0x28: case 0x29: case 0x2a:
2811 		case 0x2b: case 0x2e: case 0x2f:
2812 		case 0x48: case 0x49: case 0x4a:
2813 		case 0x4b: case 0x4e: case 0x4f:
2814 		case 0x88: case 0x89: case 0x8a:
2815 		case 0x8b: case 0x8e: case 0x8f:
2816 			switch(csr_trace[i].csr & 0xf0) {
2817 			case 0x10:
2818 				printf("DONE_"); break;
2819 			case 0x20:
2820 				printf("STOP_"); break;
2821 			case 0x40:
2822 				printf("ERR_"); break;
2823 			case 0x80:
2824 				printf("REQ_"); break;
2825 			}
2826 			switch(csr_trace[i].csr & 7) {
2827 			case 0:
2828 				printf("DATA_OUT"); break;
2829 			case 1:
2830 				printf("DATA_IN"); break;
2831 			case 2:
2832 				printf("CMD"); break;
2833 			case 3:
2834 				printf("STATUS"); break;
2835 			case 6:
2836 				printf("MSG_OUT"); break;
2837 			case 7:
2838 				printf("MSG_IN"); break;
2839 			default:
2840 				printf("invld phs");
2841 			}
2842 			break;
2843 		default:    printf("****"); break;
2844 		}
2845 		if (csr_trace[i].asr & SBIC_ASR_INT)
2846 			printf(" ASR_INT");
2847 		if (csr_trace[i].asr & SBIC_ASR_LCI)
2848 			printf(" ASR_LCI");
2849 		if (csr_trace[i].asr & SBIC_ASR_BSY)
2850 			printf(" ASR_BSY");
2851 		if (csr_trace[i].asr & SBIC_ASR_CIP)
2852 			printf(" ASR_CIP");
2853 		printf("\n");
2854 		i = (i + 1) & (CSR_TRACE_SIZE - 1);
2855 	} while (i != csr_traceptr);
2856 #endif
2857 	GET_SBIC_asr(regs, asr);
2858 	if ((asr & SBIC_ASR_INT) == 0)
2859 		GET_SBIC_csr(regs, csr);
2860 	else
2861 		csr = 0;
2862 	printf("%s@%p regs %p/%p asr %x csr %x\n", dev->sc_dev.dv_xname,
2863 	    dev, regs.sbic_asr_p, regs.sbic_value_p, asr, csr);
2864 	if ((acb = dev->free_list.tqh_first)) {
2865 		printf("Free list:\n");
2866 		while (acb) {
2867 			sbic_dump_acb(acb);
2868 			acb = acb->chain.tqe_next;
2869 		}
2870 	}
2871 	if ((acb = dev->ready_list.tqh_first)) {
2872 		printf("Ready list:\n");
2873 		while (acb) {
2874 			sbic_dump_acb(acb);
2875 			acb = acb->chain.tqe_next;
2876 		}
2877 	}
2878 	if ((acb = dev->nexus_list.tqh_first)) {
2879 		printf("Nexus list:\n");
2880 		while (acb) {
2881 			sbic_dump_acb(acb);
2882 			acb = acb->chain.tqe_next;
2883 		}
2884 	}
2885 	if (dev->sc_nexus) {
2886 		printf("nexus:\n");
2887 		sbic_dump_acb(dev->sc_nexus);
2888 	}
2889 	printf("sc_xs %p targ %d lun %d flags %x tcnt %lx dmacmd %x mask %lx\n",
2890 	    dev->sc_xs, dev->target, dev->lun, dev->sc_flags, dev->sc_tcnt,
2891 	    dev->sc_dmacmd, dev->sc_dmamask);
2892 	for (i = 0; i < 8; ++i) {
2893 		if (dev->sc_tinfo[i].cmds > 2) {
2894 			printf("tgt %d: cmds %d disc %d senses %d lubusy %x\n",
2895 			    i, dev->sc_tinfo[i].cmds,
2896 			    dev->sc_tinfo[i].dconns,
2897 			    dev->sc_tinfo[i].senses,
2898 			    dev->sc_tinfo[i].lubusy);
2899 		}
2900 	}
2901 	splx(s);
2902 }
2903 
2904 #endif
2905