sbic.c revision 1.24 1 /* $NetBSD: sbic.c,v 1.24 1996/05/01 16:58:41 mhitch 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 <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/device.h>
49 #include <sys/kernel.h> /* For hz */
50 #include <sys/disklabel.h>
51 #include <sys/dkstat.h>
52 #include <sys/buf.h>
53 #include <scsi/scsi_all.h>
54 #include <scsi/scsiconf.h>
55 #include <vm/vm.h>
56 #include <vm/vm_kern.h>
57 #include <vm/vm_page.h>
58 #include <machine/pmap.h>
59 #include <machine/cpu.h>
60 #include <amiga/amiga/device.h>
61 #include <amiga/amiga/custom.h>
62 #include <amiga/amiga/isr.h>
63 #include <amiga/dev/dmavar.h>
64 #include <amiga/dev/sbicreg.h>
65 #include <amiga/dev/sbicvar.h>
66
67 /* These are for bounce buffers */
68 #include <amiga/amiga/cc.h>
69 #include <amiga/dev/zbusvar.h>
70
71 #include <vm/pmap.h>
72
73 /* Since I can't find this in any other header files */
74 #define SCSI_PHASE(reg) (reg&0x07)
75
76 /*
77 * SCSI delays
78 * In u-seconds, primarily for state changes on the SPC.
79 */
80 #define SBIC_CMD_WAIT 50000 /* wait per step of 'immediate' cmds */
81 #define SBIC_DATA_WAIT 50000 /* wait per data in/out step */
82 #define SBIC_INIT_WAIT 50000 /* wait per step (both) during init */
83
84 #define b_cylin b_resid
85 #define SBIC_WAIT(regs, until, timeo) sbicwait(regs, until, timeo, __LINE__)
86
87 int sbicicmd __P((struct sbic_softc *, int, int, void *, int, void *, int));
88 int sbicgo __P((struct sbic_softc *, struct scsi_xfer *));
89 int sbicdmaok __P((struct sbic_softc *, struct scsi_xfer *));
90 int sbicwait __P((sbic_regmap_p, char, int , int));
91 int sbiccheckdmap __P((void *, u_long, u_long));
92 int sbicselectbus __P((struct sbic_softc *, sbic_regmap_p, u_char, u_char, u_char));
93 int sbicxfstart __P((sbic_regmap_p, int, u_char, int));
94 int sbicxfout __P((sbic_regmap_p regs, int, void *, int));
95 int sbicfromscsiperiod __P((struct sbic_softc *, sbic_regmap_p, int));
96 int sbictoscsiperiod __P((struct sbic_softc *, sbic_regmap_p, int));
97 int sbicpoll __P((struct sbic_softc *));
98 int sbicnextstate __P((struct sbic_softc *, u_char, u_char));
99 int sbicmsgin __P((struct sbic_softc *));
100 int sbicxfin __P((sbic_regmap_p regs, int, void *));
101 int sbicabort __P((struct sbic_softc *, sbic_regmap_p, char *));
102 void sbicxfdone __P((struct sbic_softc *, sbic_regmap_p, int));
103 void sbicerror __P((struct sbic_softc *, sbic_regmap_p, u_char));
104 void sbicstart __P((struct sbic_softc *));
105 void sbicreset __P((struct sbic_softc *));
106 void sbic_scsidone __P((struct sbic_acb *, int));
107 void sbic_sched __P((struct sbic_softc *));
108 void sbic_save_ptrs __P((struct sbic_softc *, sbic_regmap_p,int,int));
109 void sbic_load_ptrs __P((struct sbic_softc *, sbic_regmap_p,int,int));
110 #ifdef DEBUG
111 void sbicdumpstate __P((void));
112 void sbic_dump_acb __P((struct sbic_acb *));
113 #endif
114
115 /*
116 * Synch xfer parameters, and timing conversions
117 */
118 int sbic_min_period = SBIC_SYN_MIN_PERIOD; /* in cycles = f(ICLK,FSn) */
119 int sbic_max_offset = SBIC_SYN_MAX_OFFSET; /* pure number */
120
121 int sbic_cmd_wait = SBIC_CMD_WAIT;
122 int sbic_data_wait = SBIC_DATA_WAIT;
123 int sbic_init_wait = SBIC_INIT_WAIT;
124
125 /*
126 * was broken before.. now if you want this you get it for all drives
127 * on sbic controllers.
128 */
129 u_char sbic_inhibit_sync[8];
130 int sbic_enable_reselect = 1;
131 int sbic_clock_override = 0;
132 int sbic_no_dma = 0;
133 int sbic_parallel_operations = 1;
134
135 #ifdef DEBUG
136 sbic_regmap_p debug_sbic_regs;
137 int sbicdma_ops = 0; /* total DMA operations */
138 int sbicdma_bounces = 0; /* number operations using bounce buffer */
139 int sbicdma_hits = 0; /* number of DMA chains that were contiguous */
140 int sbicdma_misses = 0; /* number of DMA chains that were not contiguous */
141 int sbicdma_saves = 0;
142 #define QPRINTF(a) if (sbic_debug > 1) printf a
143 int sbic_debug = 0;
144 int sync_debug = 0;
145 int sbic_dma_debug = 0;
146 int reselect_debug = 0;
147 int report_sense = 0;
148 int data_pointer_debug = 0;
149 u_char debug_asr, debug_csr, routine;
150 void sbictimeout __P((struct sbic_softc *dev));
151
152 #define CSR_TRACE_SIZE 32
153 #if CSR_TRACE_SIZE
154 #define CSR_TRACE(w,c,a,x) do { \
155 int s = splbio(); \
156 csr_trace[csr_traceptr].whr = (w); csr_trace[csr_traceptr].csr = (c); \
157 csr_trace[csr_traceptr].asr = (a); csr_trace[csr_traceptr].xtn = (x); \
158 dma_cachectl((caddr_t)&csr_trace[csr_traceptr], sizeof(csr_trace[0])); \
159 csr_traceptr = (csr_traceptr + 1) & (CSR_TRACE_SIZE - 1); \
160 /* dma_cachectl((caddr_t)&csr_traceptr, sizeof(csr_traceptr));*/ \
161 splx(s); \
162 } while (0)
163 int csr_traceptr;
164 int csr_tracesize = CSR_TRACE_SIZE;
165 struct {
166 u_char whr;
167 u_char csr;
168 u_char asr;
169 u_char xtn;
170 } csr_trace[CSR_TRACE_SIZE];
171 #else
172 #define CSR_TRACE(w,c,a,x)
173 #endif
174
175 #define SBIC_TRACE_SIZE 0
176 #if SBIC_TRACE_SIZE
177 #define SBIC_TRACE(dev) do { \
178 int s = splbio(); \
179 sbic_trace[sbic_traceptr].sp = &s; \
180 sbic_trace[sbic_traceptr].line = __LINE__; \
181 sbic_trace[sbic_traceptr].sr = s; \
182 sbic_trace[sbic_traceptr].csr = csr_traceptr; \
183 dma_cachectl(&sbic_trace[sbic_traceptr], sizeof(sbic_trace[0])); \
184 sbic_traceptr = (sbic_traceptr + 1) & (SBIC_TRACE_SIZE - 1); \
185 dma_cachectl(&sbic_traceptr, sizeof(sbic_traceptr)); \
186 if (dev) dma_cachectl(dev, sizeof(*dev)); \
187 splx(s); \
188 } while (0)
189 int sbic_traceptr;
190 int sbic_tracesize = SBIC_TRACE_SIZE;
191 struct {
192 void *sp;
193 u_short line;
194 u_short sr;
195 int csr;
196 } sbic_trace[SBIC_TRACE_SIZE];
197 #else
198 #define SBIC_TRACE(dev)
199 #endif
200
201 #else /* DEBUG */
202 #define QPRINTF(a)
203 #define CSR_TRACE(w,c,a,x)
204 #define SBIC_TRACE(dev)
205 #endif /* DEBUG */
206
207 /*
208 * default minphys routine for sbic based controllers
209 */
210 void
211 sbic_minphys(bp)
212 struct buf *bp;
213 {
214
215 /*
216 * No max transfer at this level.
217 */
218 minphys(bp);
219 }
220
221 /*
222 * Save DMA pointers. Take into account partial transfer. Shut down DMA.
223 */
224 void
225 sbic_save_ptrs(dev, regs, target, lun)
226 struct sbic_softc *dev;
227 sbic_regmap_p regs;
228 int target, lun;
229 {
230 int count, asr, s;
231 struct sbic_acb* acb;
232
233 SBIC_TRACE(dev);
234 if( !dev->sc_cur ) return;
235 if( !(dev->sc_flags & SBICF_INDMA) ) return; /* DMA not active */
236
237 s = splbio();
238
239 acb = dev->sc_nexus;
240 count = -1;
241 do {
242 GET_SBIC_asr(regs, asr);
243 if( asr & SBIC_ASR_DBR ) {
244 printf("sbic_save_ptrs: asr %02x canceled!\n", asr);
245 splx(s);
246 SBIC_TRACE(dev);
247 return;
248 }
249 } while( asr & (SBIC_ASR_BSY|SBIC_ASR_CIP) );
250
251 /* Save important state */
252 /* must be done before dmastop */
253 acb->sc_dmacmd = dev->sc_dmacmd;
254 SBIC_TC_GET(regs, count);
255
256 /* Shut down DMA ====CAREFUL==== */
257 dev->sc_dmastop(dev);
258 dev->sc_flags &= ~SBICF_INDMA;
259 SBIC_TC_PUT(regs, 0);
260
261 #ifdef DEBUG
262 if(!count && sbic_debug) printf("%dcount0",target);
263 if(data_pointer_debug == -1)
264 printf("SBIC saving target %d data pointers from (%p,%x)%xASR:%02x",
265 target, dev->sc_cur->dc_addr, dev->sc_cur->dc_count,
266 acb->sc_dmacmd, asr);
267 #endif
268
269 /* Fixup partial xfers */
270 acb->sc_kv.dc_addr += (dev->sc_tcnt - count);
271 acb->sc_kv.dc_count -= (dev->sc_tcnt - count);
272 acb->sc_pa.dc_addr += (dev->sc_tcnt - count);
273 acb->sc_pa.dc_count -= ((dev->sc_tcnt - count)>>1);
274
275 acb->sc_tcnt = dev->sc_tcnt = count;
276 #ifdef DEBUG
277 if(data_pointer_debug)
278 printf(" at (%p,%x):%x\n",
279 dev->sc_cur->dc_addr, dev->sc_cur->dc_count,count);
280 sbicdma_saves++;
281 #endif
282 splx(s);
283 SBIC_TRACE(dev);
284 }
285
286
287 /*
288 * DOES NOT RESTART DMA!!!
289 */
290 void sbic_load_ptrs(dev, regs, target, lun)
291 struct sbic_softc *dev;
292 sbic_regmap_p regs;
293 int target, lun;
294 {
295 int s, count;
296 char* vaddr, * paddr;
297 struct sbic_acb *acb;
298
299 SBIC_TRACE(dev);
300 acb = dev->sc_nexus;
301 if( !acb->sc_kv.dc_count ) {
302 /* No data to xfer */
303 SBIC_TRACE(dev);
304 return;
305 }
306
307 s = splbio();
308
309 dev->sc_last = dev->sc_cur = &acb->sc_pa;
310 dev->sc_tcnt = acb->sc_tcnt;
311 dev->sc_dmacmd = acb->sc_dmacmd;
312
313 #ifdef DEBUG
314 sbicdma_ops++;
315 #endif
316 if( !dev->sc_tcnt ) {
317 /* sc_tcnt == 0 implies end of segment */
318
319 /* do kvm to pa mappings */
320 paddr = acb->sc_pa.dc_addr =
321 (char *) kvtop(acb->sc_kv.dc_addr);
322
323 vaddr = acb->sc_kv.dc_addr;
324 count = acb->sc_kv.dc_count;
325 for(count = (NBPG - ((int)vaddr & PGOFSET));
326 count < acb->sc_kv.dc_count
327 && (char*)kvtop(vaddr + count + 4) == paddr + count + 4;
328 count += NBPG);
329 /* If it's all contiguous... */
330 if(count > acb->sc_kv.dc_count ) {
331 count = acb->sc_kv.dc_count;
332 #ifdef DEBUG
333 sbicdma_hits++;
334 #endif
335 } else {
336 #ifdef DEBUG
337 sbicdma_misses++;
338 #endif
339 }
340 acb->sc_tcnt = count;
341 acb->sc_pa.dc_count = count >> 1;
342
343 #ifdef DEBUG
344 if(data_pointer_debug)
345 printf("DMA recalc:kv(%p,%x)pa(%p,%lx)\n",
346 acb->sc_kv.dc_addr,
347 acb->sc_kv.dc_count,
348 acb->sc_pa.dc_addr,
349 acb->sc_tcnt);
350 #endif
351 }
352 splx(s);
353 #ifdef DEBUG
354 if(data_pointer_debug)
355 printf("SBIC restoring target %d data pointers at (%p,%x)%x\n",
356 target, dev->sc_cur->dc_addr, dev->sc_cur->dc_count,
357 dev->sc_dmacmd);
358 #endif
359 SBIC_TRACE(dev);
360 }
361
362 /*
363 * used by specific sbic controller
364 *
365 * it appears that the higher level code does nothing with LUN's
366 * so I will too. I could plug it in, however so could they
367 * in scsi_scsi_cmd().
368 */
369 int
370 sbic_scsicmd(xs)
371 struct scsi_xfer *xs;
372 {
373 struct sbic_acb *acb;
374 struct sbic_softc *dev;
375 struct scsi_link *slp;
376 int flags, s, stat;
377
378 slp = xs->sc_link;
379 dev = slp->adapter_softc;
380 SBIC_TRACE(dev);
381 flags = xs->flags;
382
383 if (flags & SCSI_DATA_UIO)
384 panic("sbic: scsi data uio requested");
385
386 if (dev->sc_nexus && flags & SCSI_POLL)
387 panic("sbic_scsicmd: busy");
388
389 if (slp->target == slp->adapter_target)
390 return ESCAPE_NOT_SUPPORTED;
391
392 s = splbio();
393 acb = dev->free_list.tqh_first;
394 if (acb)
395 TAILQ_REMOVE(&dev->free_list, acb, chain);
396 splx(s);
397
398 if (acb == NULL) {
399 #ifdef DEBUG
400 printf("sbic_scsicmd: unable to queue request for target %d\n",
401 slp->target);
402 #ifdef DDB
403 Debugger();
404 #endif
405 #endif
406 xs->error = XS_DRIVER_STUFFUP;
407 SBIC_TRACE(dev);
408 return(TRY_AGAIN_LATER);
409 }
410
411 acb->flags = ACB_ACTIVE;
412 if (flags & SCSI_DATA_IN)
413 acb->flags |= ACB_DATAIN;
414 acb->xs = xs;
415 bcopy(xs->cmd, &acb->cmd, xs->cmdlen);
416 acb->clen = xs->cmdlen;
417 acb->sc_kv.dc_addr = xs->data;
418 acb->sc_kv.dc_count = xs->datalen;
419 acb->pa_addr = xs->data ? (char *)kvtop(xs->data) : 0; /* XXXX check */
420
421 if (flags & SCSI_POLL) {
422 s = splbio();
423 /*
424 * This has major side effects -- it locks up the machine
425 */
426
427 dev->sc_flags |= SBICF_ICMD;
428 do {
429 while(dev->sc_nexus)
430 sbicpoll(dev);
431 dev->sc_nexus = acb;
432 dev->sc_stat[0] = -1;
433 dev->sc_xs = xs;
434 dev->target = slp->target;
435 dev->lun = slp->lun;
436 stat = sbicicmd(dev, slp->target, slp->lun,
437 &acb->cmd, acb->clen,
438 acb->sc_kv.dc_addr, acb->sc_kv.dc_count);
439 } while (dev->sc_nexus != acb);
440 sbic_scsidone(acb, stat);
441
442 splx(s);
443 SBIC_TRACE(dev);
444 return(COMPLETE);
445 }
446
447 s = splbio();
448 TAILQ_INSERT_TAIL(&dev->ready_list, acb, chain);
449
450 if (dev->sc_nexus) {
451 splx(s);
452 SBIC_TRACE(dev);
453 return(SUCCESSFULLY_QUEUED);
454 }
455
456 /*
457 * nothing is active, try to start it now.
458 */
459 sbic_sched(dev);
460 splx(s);
461
462 SBIC_TRACE(dev);
463 /* TODO: add sbic_poll to do SCSI_POLL operations */
464 #if 0
465 if (flags & SCSI_POLL)
466 return(COMPLETE);
467 #endif
468 return(SUCCESSFULLY_QUEUED);
469 }
470
471 /*
472 * attempt to start the next available command
473 */
474 void
475 sbic_sched(dev)
476 struct sbic_softc *dev;
477 {
478 struct scsi_xfer *xs;
479 struct scsi_link *slp;
480 struct sbic_acb *acb;
481 int flags, /*phase,*/ stat, i;
482
483 SBIC_TRACE(dev);
484 if (dev->sc_nexus)
485 return; /* a command is current active */
486
487 SBIC_TRACE(dev);
488 for (acb = dev->ready_list.tqh_first; acb; acb = acb->chain.tqe_next) {
489 slp = acb->xs->sc_link;
490 i = slp->target;
491 if (!(dev->sc_tinfo[i].lubusy & (1 << slp->lun))) {
492 struct sbic_tinfo *ti = &dev->sc_tinfo[i];
493
494 TAILQ_REMOVE(&dev->ready_list, acb, chain);
495 dev->sc_nexus = acb;
496 slp = acb->xs->sc_link;
497 ti = &dev->sc_tinfo[slp->target];
498 ti->lubusy |= (1 << slp->lun);
499 acb->sc_pa.dc_addr = acb->pa_addr; /* XXXX check */
500 break;
501 }
502 }
503
504 SBIC_TRACE(dev);
505 if (acb == NULL)
506 return; /* did not find an available command */
507
508 dev->sc_xs = xs = acb->xs;
509 slp = xs->sc_link;
510 flags = xs->flags;
511
512 if (flags & SCSI_RESET)
513 sbicreset(dev);
514
515 #ifdef DEBUG
516 if( data_pointer_debug > 1 )
517 printf("sbic_sched(%d,%d)\n",slp->target,slp->lun);
518 #endif
519 dev->sc_stat[0] = -1;
520 dev->target = slp->target;
521 dev->lun = slp->lun;
522 if ( flags & SCSI_POLL || ( !sbic_parallel_operations
523 && (/*phase == STATUS_PHASE ||*/
524 sbicdmaok(dev, xs) == 0) ) )
525 stat = sbicicmd(dev, slp->target, slp->lun, &acb->cmd,
526 acb->clen, acb->sc_kv.dc_addr, acb->sc_kv.dc_count);
527 else if (sbicgo(dev, xs) == 0) {
528 SBIC_TRACE(dev);
529 return;
530 } else
531 stat = dev->sc_stat[0];
532
533 sbic_scsidone(acb, stat);
534 SBIC_TRACE(dev);
535 }
536
537 void
538 sbic_scsidone(acb, stat)
539 struct sbic_acb *acb;
540 int stat;
541 {
542 struct scsi_xfer *xs;
543 struct scsi_link *slp;
544 struct sbic_softc *dev;
545 int dosched = 0;
546
547 xs = acb->xs;
548 slp = xs->sc_link;
549 dev = slp->adapter_softc;
550 SBIC_TRACE(dev);
551 #ifdef DIAGNOSTIC
552 if (acb == NULL || xs == NULL) {
553 printf("sbic_scsidone -- (%d,%d) no scsi_xfer\n",
554 dev->target, dev->lun);
555 #ifdef DDB
556 Debugger();
557 #endif
558 return;
559 }
560 #endif
561 /*
562 * XXX Support old-style instrumentation for now.
563 * IS THIS REALLY THE RIGHT PLACE FOR THIS? --thorpej
564 */
565 if (slp->device_softc &&
566 ((struct device *)(slp->device_softc))->dv_unit < dk_ndrive)
567 ++dk_xfer[((struct device *)(slp->device_softc))->dv_unit];
568 /*
569 * is this right?
570 */
571 xs->status = stat;
572
573 #ifdef DEBUG
574 if( data_pointer_debug > 1 )
575 printf("scsidone: (%d,%d)->(%d,%d)%02x\n",
576 slp->target, slp->lun,
577 dev->target, dev->lun, stat);
578 if( xs->sc_link->target == dev->sc_link.adapter_target )
579 panic("target == hostid");
580 #endif
581
582 if (xs->error == XS_NOERROR && !(acb->flags & ACB_CHKSENSE)) {
583 if (stat == SCSI_CHECK) {
584 /* Schedule a REQUEST SENSE */
585 struct scsi_sense *ss = (void *)&acb->cmd;
586 #ifdef DEBUG
587 if (report_sense)
588 printf("sbic_scsidone: autosense %02x targ %d lun %d",
589 acb->cmd.opcode, slp->target, slp->lun);
590 #endif
591 bzero(ss, sizeof(*ss));
592 ss->opcode = REQUEST_SENSE;
593 ss->byte2 = slp->lun << 5;
594 ss->length = sizeof(struct scsi_sense_data);
595 acb->clen = sizeof(*ss);
596 acb->sc_kv.dc_addr = (char *)&xs->sense;
597 acb->sc_kv.dc_count = sizeof(struct scsi_sense_data);
598 acb->pa_addr = (char *)kvtop((u_char *)&xs->sense); /* XXX check */
599 acb->flags = ACB_ACTIVE | ACB_CHKSENSE | ACB_DATAIN;
600 TAILQ_INSERT_HEAD(&dev->ready_list, acb, chain);
601 dev->sc_tinfo[slp->target].lubusy &=
602 ~(1 << slp->lun);
603 dev->sc_tinfo[slp->target].senses++;
604 if (dev->sc_nexus == acb) {
605 dev->sc_nexus = NULL;
606 dev->sc_xs = NULL;
607 sbic_sched(dev);
608 }
609 SBIC_TRACE(dev);
610 return;
611 }
612 }
613 if (xs->error == XS_NOERROR && (acb->flags & ACB_CHKSENSE)) {
614 xs->error = XS_SENSE;
615 #ifdef DEBUG
616 if (report_sense)
617 printf(" => %02x %02x\n", xs->sense.flags,
618 xs->sense.extra_bytes[3]);
619 #endif
620 } else {
621 xs->resid = 0; /* XXXX */
622 }
623 #if whataboutthisone
624 case SCSI_BUSY:
625 xs->error = XS_BUSY;
626 break;
627 #endif
628 xs->flags |= ITSDONE;
629
630 /*
631 * Remove the ACB from whatever queue it's on. We have to do a bit of
632 * a hack to figure out which queue it's on. Note that it is *not*
633 * necessary to cdr down the ready queue, but we must cdr down the
634 * nexus queue and see if it's there, so we can mark the unit as no
635 * longer busy. This code is sickening, but it works.
636 */
637 if (acb == dev->sc_nexus) {
638 dev->sc_nexus = NULL;
639 dev->sc_xs = NULL;
640 dev->sc_tinfo[slp->target].lubusy &= ~(1<<slp->lun);
641 if (dev->ready_list.tqh_first)
642 dosched = 1; /* start next command */
643 } else if (dev->ready_list.tqh_last == &acb->chain.tqe_next) {
644 TAILQ_REMOVE(&dev->ready_list, acb, chain);
645 } else {
646 register struct sbic_acb *acb2;
647 for (acb2 = dev->nexus_list.tqh_first; acb2;
648 acb2 = acb2->chain.tqe_next) {
649 if (acb2 == acb) {
650 TAILQ_REMOVE(&dev->nexus_list, acb, chain);
651 dev->sc_tinfo[slp->target].lubusy
652 &= ~(1<<slp->lun);
653 break;
654 }
655 }
656 if (acb2)
657 ;
658 else if (acb->chain.tqe_next) {
659 TAILQ_REMOVE(&dev->ready_list, acb, chain);
660 } else {
661 printf("%s: can't find matching acb\n",
662 dev->sc_dev.dv_xname);
663 #ifdef DDB
664 Debugger();
665 #endif
666 }
667 }
668 /* Put it on the free list. */
669 acb->flags = ACB_FREE;
670 TAILQ_INSERT_HEAD(&dev->free_list, acb, chain);
671
672 dev->sc_tinfo[slp->target].cmds++;
673
674 scsi_done(xs);
675
676 if (dosched)
677 sbic_sched(dev);
678 SBIC_TRACE(dev);
679 }
680
681 int
682 sbicdmaok(dev, xs)
683 struct sbic_softc *dev;
684 struct scsi_xfer *xs;
685 {
686 if (sbic_no_dma || xs->datalen & 0x1 || (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->target].bounce)
702 return(1);
703 /*
704 * try to get one
705 */
706 else if ((dev->sc_tinfo[xs->sc_link->target].bounce
707 = (char *)alloc_z2mem(MAXPHYS))) {
708 if (isztwomem(dev->sc_tinfo[xs->sc_link->target].bounce))
709 printf("alloc ZII target %d bounce pa 0x%x\n",
710 xs->sc_link->target,
711 kvtop(dev->sc_tinfo[xs->sc_link->target].bounce));
712 else if (dev->sc_tinfo[xs->sc_link->target].bounce)
713 printf("alloc CHIP target %d bounce pa 0x%p\n",
714 xs->sc_link->target,
715 PREP_DMA_MEM(dev->sc_tinfo[xs->sc_link->target].bounce));
716 return(1);
717 }
718
719 return(0);
720 }
721
722
723 int
724 sbicwait(regs, until, timeo, line)
725 sbic_regmap_p 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_p 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_p 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_sbicp;
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_p 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_sbicp;
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.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_p regs;
982 u_char csr;
983 {
984 struct scsi_xfer *xs;
985
986 xs = dev->sc_xs;
987
988 #ifdef DIAGNOSTIC
989 if (xs == NULL)
990 panic("sbicerror");
991 #endif
992 if (xs->flags & SCSI_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_p 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->flags & SCSI_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_p 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_p 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_p 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_p 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_sbicp;
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_p 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 scsi_xfer *xs;
1652 {
1653 int i, dmaflags, count, usedma;
1654 u_char csr, asr, *addr;
1655 sbic_regmap_p regs;
1656 struct sbic_acb *acb;
1657
1658 SBIC_TRACE(dev);
1659 dev->target = xs->sc_link->target;
1660 dev->lun = xs->sc_link->lun;
1661 acb = dev->sc_nexus;
1662 regs = dev->sc_sbicp;
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->target].bounce
1735 = (char *)alloc_z2mem(MAXPHYS);
1736 if (isztwomem(dev->sc_tinfo[xs->sc_link->target].bounce))
1737 printf("alloc ZII target %d bounce pa 0x%x\n",
1738 xs->sc_link->target,
1739 kvtop(dev->sc_tinfo[xs->sc_link->target].bounce));
1740 else if (dev->sc_tinfo[xs->sc_link->target].bounce)
1741 printf("alloc CHIP target %d bounce pa 0x%p\n",
1742 xs->sc_link->target,
1743 PREP_DMA_MEM(dev->sc_tinfo[xs->sc_link->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)
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_p regs;
1867 u_char asr, csr;
1868 int i;
1869
1870 regs = dev->sc_sbicp;
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_p regs;
1915 u_char asr, csr;
1916 int i;
1917
1918 SBIC_TRACE(dev);
1919 regs = dev->sc_sbicp;
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_p regs;
1976 int recvlen;
1977 u_char asr, csr, *tmpaddr;
1978
1979 regs = dev->sc_sbicp;
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_p 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_sbicp;
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 #ifdef M68040
2289 if (dev->sc_flags & SBICF_DCFLUSH) {
2290 #if 0
2291 printf("sbic: 68040 DMA cache flush needs fixing? %x:%x\n",
2292 dev->sc_xs->data, dev->sc_xs->datalen);
2293 #endif
2294 }
2295 #endif
2296 #ifdef DEBUG
2297 if( data_pointer_debug > 1 )
2298 printf("next dmastop: %d(%p:%lx)\n",
2299 dev->target,dev->sc_cur->dc_addr,dev->sc_tcnt);
2300 dev->sc_dmatimo = 0;
2301 #endif
2302 dev->sc_dmastop(dev); /* was dmafree */
2303 if (acb->flags & ACB_BBUF) {
2304 if ((u_char *)kvtop(acb->sc_dmausrbuf) != acb->sc_usrbufpa)
2305 printf("%s: WARNING - buffer mapping changed %p->%x\n",
2306 dev->sc_dev.dv_xname, acb->sc_usrbufpa,
2307 kvtop(acb->sc_dmausrbuf));
2308 #ifdef DEBUG
2309 if(data_pointer_debug)
2310 printf("sbicgo:copying %lx bytes from target %d bounce %x\n",
2311 acb->sc_dmausrlen,
2312 dev->target,
2313 kvtop(dev->sc_tinfo[dev->target].bounce));
2314 #endif
2315 bcopy(dev->sc_tinfo[dev->target].bounce,
2316 acb->sc_dmausrbuf,
2317 acb->sc_dmausrlen);
2318 }
2319 dev->sc_flags &= ~(SBICF_INDMA | SBICF_DCFLUSH);
2320 sbic_scsidone(acb, dev->sc_stat[0]);
2321 SBIC_TRACE(dev);
2322 return SBIC_STATE_DONE;
2323
2324 case SBIC_CSR_XFERRED|DATA_OUT_PHASE:
2325 case SBIC_CSR_XFERRED|DATA_IN_PHASE:
2326 case SBIC_CSR_MIS|DATA_OUT_PHASE:
2327 case SBIC_CSR_MIS|DATA_IN_PHASE:
2328 case SBIC_CSR_MIS_1|DATA_OUT_PHASE:
2329 case SBIC_CSR_MIS_1|DATA_IN_PHASE:
2330 case SBIC_CSR_MIS_2|DATA_OUT_PHASE:
2331 case SBIC_CSR_MIS_2|DATA_IN_PHASE:
2332 if( dev->sc_xs->flags & SCSI_POLL || dev->sc_flags & SBICF_ICMD
2333 || acb->sc_dmacmd == 0 ) {
2334 /* Do PIO */
2335 SET_SBIC_control(regs, SBIC_CTL_EDI | SBIC_CTL_IDI);
2336 if (acb->sc_kv.dc_count <= 0) {
2337 printf("sbicnextstate:xfer count %d asr%x csr%x\n",
2338 acb->sc_kv.dc_count, asr, csr);
2339 goto abort;
2340 }
2341 wait = sbic_data_wait;
2342 if( sbicxfstart(regs,
2343 acb->sc_kv.dc_count,
2344 SBIC_PHASE(csr), wait))
2345 if( SBIC_PHASE(csr) == DATA_IN_PHASE )
2346 /* data in? */
2347 i=sbicxfin(regs,
2348 acb->sc_kv.dc_count,
2349 acb->sc_kv.dc_addr);
2350 else
2351 i=sbicxfout(regs,
2352 acb->sc_kv.dc_count,
2353 acb->sc_kv.dc_addr,
2354 SBIC_PHASE(csr));
2355 acb->sc_kv.dc_addr +=
2356 (acb->sc_kv.dc_count - i);
2357 acb->sc_kv.dc_count = i;
2358 } else {
2359 if (acb->sc_kv.dc_count <= 0) {
2360 printf("sbicnextstate:xfer count %d asr%x csr%x\n",
2361 acb->sc_kv.dc_count, asr, csr);
2362 goto abort;
2363 }
2364 /*
2365 * do scatter-gather dma
2366 * hacking the controller chip, ouch..
2367 */
2368 SET_SBIC_control(regs, SBIC_CTL_EDI | SBIC_CTL_IDI |
2369 SBIC_MACHINE_DMA_MODE);
2370 /*
2371 * set next dma addr and dec count
2372 */
2373 #if 0
2374 SBIC_TC_GET(regs, tcnt);
2375 dev->sc_cur->dc_count -= ((dev->sc_tcnt - tcnt) >> 1);
2376 dev->sc_cur->dc_addr += (dev->sc_tcnt - tcnt);
2377 dev->sc_tcnt = acb->sc_tcnt = tcnt;
2378 #else
2379 sbic_save_ptrs(dev, regs, dev->target, dev->lun);
2380 sbic_load_ptrs(dev, regs, dev->target, dev->lun);
2381 #endif
2382 #ifdef DEBUG
2383 if( data_pointer_debug > 1 )
2384 printf("next dmanext: %d(%p:%lx)\n",
2385 dev->target,dev->sc_cur->dc_addr,
2386 dev->sc_tcnt);
2387 dev->sc_dmatimo = 1;
2388 #endif
2389 dev->sc_tcnt = dev->sc_dmanext(dev);
2390 SBIC_TC_PUT(regs, (unsigned)dev->sc_tcnt);
2391 SET_SBIC_cmd(regs, SBIC_CMD_XFER_INFO);
2392 dev->sc_flags |= SBICF_INDMA;
2393 }
2394 break;
2395
2396 case SBIC_CSR_XFERRED|MESG_IN_PHASE:
2397 case SBIC_CSR_MIS|MESG_IN_PHASE:
2398 case SBIC_CSR_MIS_1|MESG_IN_PHASE:
2399 case SBIC_CSR_MIS_2|MESG_IN_PHASE:
2400 SBIC_TRACE(dev);
2401 return sbicmsgin(dev);
2402
2403 case SBIC_CSR_MSGIN_W_ACK:
2404 SET_SBIC_cmd(regs, SBIC_CMD_CLR_ACK); /* Dunno what I'm ACKing */
2405 printf("Acking unknown msgin CSR:%02x",csr);
2406 break;
2407
2408 case SBIC_CSR_XFERRED|MESG_OUT_PHASE:
2409 case SBIC_CSR_MIS|MESG_OUT_PHASE:
2410 case SBIC_CSR_MIS_1|MESG_OUT_PHASE:
2411 case SBIC_CSR_MIS_2|MESG_OUT_PHASE:
2412 #ifdef DEBUG
2413 if (sync_debug)
2414 printf ("sending REJECT msg to last msg.\n");
2415 #endif
2416
2417 sbic_save_ptrs(dev, regs, dev->target, dev->lun);
2418 /*
2419 * should only get here on reject,
2420 * since it's always US that
2421 * initiate a sync transfer
2422 */
2423 SEND_BYTE(regs, MSG_REJECT);
2424 WAIT_CIP(regs);
2425 if( asr & (SBIC_ASR_BSY|SBIC_ASR_LCI|SBIC_ASR_CIP) )
2426 printf("next: REJECT sent asr %02x\n", asr);
2427 SBIC_TRACE(dev);
2428 return SBIC_STATE_RUNNING;
2429
2430 case SBIC_CSR_DISC:
2431 case SBIC_CSR_DISC_1:
2432 dev->sc_flags &= ~(SBICF_INDMA|SBICF_SELECTED);
2433
2434 /* Try to schedule another target */
2435 #ifdef DEBUG
2436 if(reselect_debug>1)
2437 printf("sbicnext target %d disconnected\n", dev->target);
2438 #endif
2439 TAILQ_INSERT_HEAD(&dev->nexus_list, acb, chain);
2440 ++dev->sc_tinfo[dev->target].dconns;
2441 dev->sc_nexus = NULL;
2442 dev->sc_xs = NULL;
2443
2444 if( acb->xs->flags & SCSI_POLL
2445 || (dev->sc_flags & SBICF_ICMD)
2446 || !sbic_parallel_operations ) {
2447 SBIC_TRACE(dev);
2448 return SBIC_STATE_DISCONNECT;
2449 }
2450 sbic_sched(dev);
2451 SBIC_TRACE(dev);
2452 return SBIC_STATE_DISCONNECT;
2453
2454 case SBIC_CSR_RSLT_NI:
2455 case SBIC_CSR_RSLT_IFY:
2456 GET_SBIC_rselid(regs, newtarget);
2457 /* check SBIC_RID_SIV? */
2458 newtarget &= SBIC_RID_MASK;
2459 if (csr == SBIC_CSR_RSLT_IFY) {
2460 /* Read IFY msg to avoid lockup */
2461 GET_SBIC_data(regs, newlun);
2462 WAIT_CIP(regs);
2463 newlun &= SBIC_TLUN_MASK;
2464 CSR_TRACE('r',csr,asr,newtarget);
2465 } else {
2466 /* Need to get IFY message */
2467 for (newlun = 256; newlun; --newlun) {
2468 GET_SBIC_asr(regs, asr);
2469 if (asr & SBIC_ASR_INT)
2470 break;
2471 delay(1);
2472 }
2473 newlun = 0; /* XXXX */
2474 if ((asr & SBIC_ASR_INT) == 0) {
2475 #ifdef DEBUG
2476 if (reselect_debug)
2477 printf("RSLT_NI - no IFFY message? asr %x\n", asr);
2478 #endif
2479 } else {
2480 GET_SBIC_csr(regs,csr);
2481 CSR_TRACE('n',csr,asr,newtarget);
2482 if (csr == (SBIC_CSR_MIS | MESG_IN_PHASE) ||
2483 csr == (SBIC_CSR_MIS_1 | MESG_IN_PHASE) ||
2484 csr == (SBIC_CSR_MIS_2 | MESG_IN_PHASE)) {
2485 sbicmsgin(dev);
2486 newlun = dev->sc_msg[0] & 7;
2487 } else {
2488 printf("RSLT_NI - not MESG_IN_PHASE %x\n",
2489 csr);
2490 }
2491 }
2492 }
2493 #ifdef DEBUG
2494 if(reselect_debug>1 || (reselect_debug && csr==SBIC_CSR_RSLT_NI))
2495 printf("sbicnext: reselect %s from targ %d lun %d\n",
2496 csr == SBIC_CSR_RSLT_NI ? "NI" : "IFY",
2497 newtarget, newlun);
2498 #endif
2499 if (dev->sc_nexus) {
2500 #ifdef DEBUG
2501 if (reselect_debug > 1)
2502 printf("%s: reselect %s with active command\n",
2503 dev->sc_dev.dv_xname,
2504 csr == SBIC_CSR_RSLT_NI ? "NI" : "IFY");
2505 #ifdef DDB
2506 /* Debugger();*/
2507 #endif
2508 #endif
2509 TAILQ_INSERT_HEAD(&dev->ready_list, dev->sc_nexus, chain);
2510 dev->sc_tinfo[dev->target].lubusy &= ~(1 << dev->lun);
2511 dev->sc_nexus = NULL;
2512 dev->sc_xs = NULL;
2513 }
2514 /* Reload sync values for this target */
2515 if (dev->sc_sync[newtarget].state == SYNC_DONE)
2516 SET_SBIC_syn(regs, SBIC_SYN (dev->sc_sync[newtarget].offset,
2517 dev->sc_sync[newtarget].period));
2518 else
2519 SET_SBIC_syn(regs, SBIC_SYN (0, sbic_min_period));
2520 for (acb = dev->nexus_list.tqh_first; acb;
2521 acb = acb->chain.tqe_next) {
2522 if (acb->xs->sc_link->target != newtarget ||
2523 acb->xs->sc_link->lun != newlun)
2524 continue;
2525 TAILQ_REMOVE(&dev->nexus_list, acb, chain);
2526 dev->sc_nexus = acb;
2527 dev->sc_xs = acb->xs;
2528 dev->sc_flags |= SBICF_SELECTED;
2529 dev->target = newtarget;
2530 dev->lun = newlun;
2531 break;
2532 }
2533 if (acb == NULL) {
2534 printf("%s: reselect %s targ %d not in nexus_list %p\n",
2535 dev->sc_dev.dv_xname,
2536 csr == SBIC_CSR_RSLT_NI ? "NI" : "IFY", newtarget,
2537 &dev->nexus_list.tqh_first);
2538 panic("bad reselect in sbic");
2539 }
2540 if (csr == SBIC_CSR_RSLT_IFY)
2541 SET_SBIC_cmd(regs, SBIC_CMD_CLR_ACK);
2542 break;
2543
2544 default:
2545 abort:
2546 /*
2547 * Something unexpected happened -- deal with it.
2548 */
2549 printf("sbicnextstate: aborting csr %02x asr %02x\n", csr, asr);
2550 #ifdef DDB
2551 Debugger();
2552 #endif
2553 #ifdef DEBUG
2554 if( data_pointer_debug > 1 )
2555 printf("next dmastop: %d(%p:%lx)\n",
2556 dev->target,dev->sc_cur->dc_addr,dev->sc_tcnt);
2557 dev->sc_dmatimo = 0;
2558 #endif
2559 dev->sc_dmastop(dev);
2560 SET_SBIC_control(regs, SBIC_CTL_EDI | SBIC_CTL_IDI);
2561 sbicerror(dev, regs, csr);
2562 sbicabort(dev, regs, "next");
2563 if (dev->sc_flags & SBICF_INDMA) {
2564 /*
2565 * check for overlapping cache line, flush if so
2566 */
2567 #ifdef M68040
2568 if (dev->sc_flags & SBICF_DCFLUSH) {
2569 #if 0
2570 printf("sibc: 68040 DMA cache flush needs fixing? %x:%x\n",
2571 dev->sc_xs->data, dev->sc_xs->datalen);
2572 #endif
2573 }
2574 #endif
2575 dev->sc_flags &=
2576 ~(SBICF_INDMA | SBICF_DCFLUSH);
2577 #ifdef DEBUG
2578 if( data_pointer_debug > 1 )
2579 printf("next dmastop: %d(%p:%lx)\n",
2580 dev->target,dev->sc_cur->dc_addr,dev->sc_tcnt);
2581 dev->sc_dmatimo = 0;
2582 #endif
2583 dev->sc_dmastop(dev);
2584 sbic_scsidone(acb, -1);
2585 }
2586 SBIC_TRACE(dev);
2587 return SBIC_STATE_ERROR;
2588 }
2589
2590 SBIC_TRACE(dev);
2591 return(SBIC_STATE_RUNNING);
2592 }
2593
2594
2595 /*
2596 * Check if DMA can not be used with specified buffer
2597 */
2598
2599 int
2600 sbiccheckdmap(bp, len, mask)
2601 void *bp;
2602 u_long len, mask;
2603 {
2604 u_char *buffer;
2605 u_long phy_buf;
2606 u_long phy_len;
2607
2608 buffer = bp;
2609
2610 if (len == 0)
2611 return(0);
2612
2613 while (len) {
2614 phy_buf = kvtop(buffer);
2615 if (len < (phy_len = NBPG - ((int) buffer & PGOFSET)))
2616 phy_len = len;
2617 if (phy_buf & mask)
2618 return(1);
2619 buffer += phy_len;
2620 len -= phy_len;
2621 }
2622 return(0);
2623 }
2624
2625 int
2626 sbictoscsiperiod(dev, regs, a)
2627 struct sbic_softc *dev;
2628 sbic_regmap_p regs;
2629 int a;
2630 {
2631 unsigned int fs;
2632
2633 /*
2634 * cycle = DIV / (2*CLK)
2635 * DIV = FS+2
2636 * best we can do is 200ns at 20Mhz, 2 cycles
2637 */
2638
2639 GET_SBIC_myid(regs,fs);
2640 fs = (fs >>6) + 2; /* DIV */
2641 fs = (fs * 10000) / (dev->sc_clkfreq<<1); /* Cycle, in ns */
2642 if (a < 2) a = 8; /* map to Cycles */
2643 return ((fs*a)>>2); /* in 4 ns units */
2644 }
2645
2646 int
2647 sbicfromscsiperiod(dev, regs, p)
2648 struct sbic_softc *dev;
2649 sbic_regmap_p regs;
2650 int p;
2651 {
2652 register unsigned int fs, ret;
2653
2654 /* Just the inverse of the above */
2655
2656 GET_SBIC_myid(regs,fs);
2657 fs = (fs >>6) + 2; /* DIV */
2658 fs = (fs * 10000) / (dev->sc_clkfreq<<1); /* Cycle, in ns */
2659
2660 ret = p << 2; /* in ns units */
2661 ret = ret / fs; /* in Cycles */
2662 if (ret < sbic_min_period)
2663 return(sbic_min_period);
2664
2665 /* verify rounding */
2666 if (sbictoscsiperiod(dev, regs, ret) < p)
2667 ret++;
2668 return (ret >= 8) ? 0 : ret;
2669 }
2670
2671 #ifdef DEBUG
2672
2673 void
2674 sbicdumpstate()
2675 {
2676 u_char csr, asr;
2677
2678 GET_SBIC_asr(debug_sbic_regs,asr);
2679 GET_SBIC_csr(debug_sbic_regs,csr);
2680 printf("%s: asr:csr(%02x:%02x)->(%02x:%02x)\n",
2681 (routine==1)?"sbicgo":
2682 (routine==2)?"sbicintr":
2683 (routine==3)?"sbicicmd":
2684 (routine==4)?"sbicnext":"unknown",
2685 debug_asr, debug_csr, asr, csr);
2686
2687 }
2688
2689 void
2690 sbictimeout(dev)
2691 struct sbic_softc *dev;
2692 {
2693 int s, asr;
2694
2695 s = splbio();
2696 if (dev->sc_dmatimo) {
2697 if (dev->sc_dmatimo > 1) {
2698 printf("%s: dma timeout #%d\n",
2699 dev->sc_dev.dv_xname, dev->sc_dmatimo - 1);
2700 GET_SBIC_asr(dev->sc_sbicp, asr);
2701 if( asr & SBIC_ASR_INT ) {
2702 /* We need to service a missed IRQ */
2703 printf("Servicing a missed int:(%02x,%02x)->(%02x,??)\n",
2704 debug_asr, debug_csr, asr);
2705 sbicintr(dev);
2706 }
2707 sbicdumpstate();
2708 }
2709 dev->sc_dmatimo++;
2710 }
2711 splx(s);
2712 timeout((void *)sbictimeout, dev, 30 * hz);
2713 }
2714
2715 void
2716 sbic_dump_acb(acb)
2717 struct sbic_acb *acb;
2718 {
2719 u_char *b = (u_char *) &acb->cmd;
2720 int i;
2721
2722 printf("acb@%p ", acb);
2723 if (acb->xs == NULL) {
2724 printf("<unused>\n");
2725 return;
2726 }
2727 printf("(%d:%d) flags %2x clen %2d cmd ", acb->xs->sc_link->target,
2728 acb->xs->sc_link->lun, acb->flags, acb->clen);
2729 for (i = acb->clen; i; --i)
2730 printf(" %02x", *b++);
2731 printf("\n");
2732 printf(" xs: %8p data %8p:%04x ", acb->xs, acb->xs->data,
2733 acb->xs->datalen);
2734 printf("va %8p:%04x ", acb->sc_kv.dc_addr, acb->sc_kv.dc_count);
2735 printf("pa %8p:%04x tcnt %lx\n", acb->sc_pa.dc_addr, acb->sc_pa.dc_count,
2736 acb->sc_tcnt);
2737 }
2738
2739 void
2740 sbic_dump(dev)
2741 struct sbic_softc *dev;
2742 {
2743 sbic_regmap_p regs;
2744 u_char csr, asr;
2745 struct sbic_acb *acb;
2746 int s;
2747 int i;
2748
2749 s = splbio();
2750 regs = dev->sc_sbicp;
2751 #if CSR_TRACE_SIZE
2752 printf("csr trace: ");
2753 i = csr_traceptr;
2754 do {
2755 printf("%c%02x%02x%02x ", csr_trace[i].whr,
2756 csr_trace[i].csr, csr_trace[i].asr, csr_trace[i].xtn);
2757 switch(csr_trace[i].whr) {
2758 case 'g':
2759 printf("go "); break;
2760 case 's':
2761 printf("select "); break;
2762 case 'y':
2763 printf("select+ "); break;
2764 case 'i':
2765 printf("intr "); break;
2766 case 'f':
2767 printf("finish "); break;
2768 case '>':
2769 printf("out "); break;
2770 case '<':
2771 printf("in "); break;
2772 case 'm':
2773 printf("msgin "); break;
2774 case 'x':
2775 printf("msginx "); break;
2776 case 'X':
2777 printf("msginX "); break;
2778 case 'r':
2779 printf("reselect "); break;
2780 case 'I':
2781 printf("icmd "); break;
2782 case 'a':
2783 printf("abort "); break;
2784 default:
2785 printf("? ");
2786 }
2787 switch(csr_trace[i].csr) {
2788 case 0x11:
2789 printf("INITIATOR"); break;
2790 case 0x16:
2791 printf("S_XFERRED"); break;
2792 case 0x20:
2793 printf("MSGIN_ACK"); break;
2794 case 0x41:
2795 printf("DISC"); break;
2796 case 0x42:
2797 printf("SEL_TIMEO"); break;
2798 case 0x80:
2799 printf("RSLT_NI"); break;
2800 case 0x81:
2801 printf("RSLT_IFY"); break;
2802 case 0x85:
2803 printf("DISC_1"); break;
2804 case 0x18: case 0x19: case 0x1a:
2805 case 0x1b: case 0x1e: case 0x1f:
2806 case 0x28: case 0x29: case 0x2a:
2807 case 0x2b: case 0x2e: case 0x2f:
2808 case 0x48: case 0x49: case 0x4a:
2809 case 0x4b: case 0x4e: case 0x4f:
2810 case 0x88: case 0x89: case 0x8a:
2811 case 0x8b: case 0x8e: case 0x8f:
2812 switch(csr_trace[i].csr & 0xf0) {
2813 case 0x10:
2814 printf("DONE_"); break;
2815 case 0x20:
2816 printf("STOP_"); break;
2817 case 0x40:
2818 printf("ERR_"); break;
2819 case 0x80:
2820 printf("REQ_"); break;
2821 }
2822 switch(csr_trace[i].csr & 7) {
2823 case 0:
2824 printf("DATA_OUT"); break;
2825 case 1:
2826 printf("DATA_IN"); break;
2827 case 2:
2828 printf("CMD"); break;
2829 case 3:
2830 printf("STATUS"); break;
2831 case 6:
2832 printf("MSG_OUT"); break;
2833 case 7:
2834 printf("MSG_IN"); break;
2835 default:
2836 printf("invld phs");
2837 }
2838 break;
2839 default: printf("****"); break;
2840 }
2841 if (csr_trace[i].asr & SBIC_ASR_INT)
2842 printf(" ASR_INT");
2843 if (csr_trace[i].asr & SBIC_ASR_LCI)
2844 printf(" ASR_LCI");
2845 if (csr_trace[i].asr & SBIC_ASR_BSY)
2846 printf(" ASR_BSY");
2847 if (csr_trace[i].asr & SBIC_ASR_CIP)
2848 printf(" ASR_CIP");
2849 printf("\n");
2850 i = (i + 1) & (CSR_TRACE_SIZE - 1);
2851 } while (i != csr_traceptr);
2852 #endif
2853 GET_SBIC_asr(regs, asr);
2854 if ((asr & SBIC_ASR_INT) == 0)
2855 GET_SBIC_csr(regs, csr);
2856 else
2857 csr = 0;
2858 printf("%s@%p regs %p asr %x csr %x\n", dev->sc_dev.dv_xname,
2859 dev, regs, asr, csr);
2860 if ((acb = dev->free_list.tqh_first)) {
2861 printf("Free list:\n");
2862 while (acb) {
2863 sbic_dump_acb(acb);
2864 acb = acb->chain.tqe_next;
2865 }
2866 }
2867 if ((acb = dev->ready_list.tqh_first)) {
2868 printf("Ready list:\n");
2869 while (acb) {
2870 sbic_dump_acb(acb);
2871 acb = acb->chain.tqe_next;
2872 }
2873 }
2874 if ((acb = dev->nexus_list.tqh_first)) {
2875 printf("Nexus list:\n");
2876 while (acb) {
2877 sbic_dump_acb(acb);
2878 acb = acb->chain.tqe_next;
2879 }
2880 }
2881 if (dev->sc_nexus) {
2882 printf("nexus:\n");
2883 sbic_dump_acb(dev->sc_nexus);
2884 }
2885 printf("sc_xs %p targ %d lun %d flags %x tcnt %lx dmacmd %x mask %lx\n",
2886 dev->sc_xs, dev->target, dev->lun, dev->sc_flags, dev->sc_tcnt,
2887 dev->sc_dmacmd, dev->sc_dmamask);
2888 for (i = 0; i < 8; ++i) {
2889 if (dev->sc_tinfo[i].cmds > 2) {
2890 printf("tgt %d: cmds %d disc %d senses %d lubusy %x\n",
2891 i, dev->sc_tinfo[i].cmds,
2892 dev->sc_tinfo[i].dconns,
2893 dev->sc_tinfo[i].senses,
2894 dev->sc_tinfo[i].lubusy);
2895 }
2896 }
2897 splx(s);
2898 }
2899
2900 #endif
2901