No empty .Rs/.Re
[netbsd-mini2440.git] / sys / arch / mvme68k / dev / sbic.c
blob3fb4380b005bf2b546ab321e20f7d11c82ea15b6
1 /* $NetBSD: sbic.c,v 1.31 2007/10/17 19:55:47 garbled Exp $ */
3 /*
4 * Copyright (c) 1990 The Regents of the University of California.
5 * All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Van Jacobson of Lawrence Berkeley Laboratory.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
34 * @(#)scsi.c 7.5 (Berkeley) 5/4/91
38 * Changes Copyright (c) 1996 Steve Woodford
39 * Original Copyright (c) 1994 Christian E. Hopps
41 * This code is derived from software contributed to Berkeley by
42 * Van Jacobson of Lawrence Berkeley Laboratory.
44 * Redistribution and use in source and binary forms, with or without
45 * modification, are permitted provided that the following conditions
46 * are met:
47 * 1. Redistributions of source code must retain the above copyright
48 * notice, this list of conditions and the following disclaimer.
49 * 2. Redistributions in binary form must reproduce the above copyright
50 * notice, this list of conditions and the following disclaimer in the
51 * documentation and/or other materials provided with the distribution.
52 * 3. All advertising materials mentioning features or use of this software
53 * must display the following acknowledgement:
54 * This product includes software developed by the University of
55 * California, Berkeley and its contributors.
56 * 4. Neither the name of the University nor the names of its contributors
57 * may be used to endorse or promote products derived from this software
58 * without specific prior written permission.
60 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
61 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
62 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
63 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
64 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
65 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
66 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
67 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
68 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
69 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
70 * SUCH DAMAGE.
72 * @(#)scsi.c 7.5 (Berkeley) 5/4/91
76 * Steve Woodford (SCW), Apr, 1996
77 * MVME147S WD33C93 Scsi Bus Interface Controller driver,
79 * Basically a de-loused and tidied up version of the Amiga AMD 33C93 driver.
81 * The original driver used features which required at least a WD33C93A
82 * chip. The '147 has the original WD33C93 chip (no 'A' suffix).
84 * This version of the driver is pretty well generic, so should work with
85 * any flavour of WD33C93 chip.
88 #include <sys/cdefs.h>
89 __KERNEL_RCSID(0, "$NetBSD: sbic.c,v 1.31 2007/10/17 19:55:47 garbled Exp $");
91 #include "opt_ddb.h"
93 #include <sys/param.h>
94 #include <sys/systm.h>
95 #include <sys/device.h>
96 #include <sys/kernel.h> /* For hz */
97 #include <sys/disklabel.h>
98 #include <sys/buf.h>
100 #include <dev/scsipi/scsi_all.h>
101 #include <dev/scsipi/scsipi_all.h>
102 #include <dev/scsipi/scsiconf.h>
104 #include <uvm/uvm_extern.h>
106 #include <mvme68k/mvme68k/isr.h>
107 #include <mvme68k/dev/dmavar.h>
108 #include <mvme68k/dev/sbicreg.h>
109 #include <mvme68k/dev/sbicvar.h>
113 * Since I can't find this in any other header files
115 #define SCSI_PHASE(reg) (reg&0x07)
118 * SCSI delays
119 * In u-seconds, primarily for state changes on the SPC.
121 #define SBIC_CMD_WAIT 50000 /* wait per step of 'immediate' cmds */
122 #define SBIC_DATA_WAIT 50000 /* wait per data in/out step */
123 #define SBIC_INIT_WAIT 50000 /* wait per step (both) during init */
126 * Convenience macro for waiting for a particular sbic event
128 #define SBIC_WAIT(regs, until, timeo) sbicwait(regs, until, timeo, __LINE__)
130 int sbicicmd (struct sbic_softc *, void *, int, void *, int);
131 int sbicgo (struct sbic_softc *, struct scsipi_xfer *);
132 int sbicdmaok (struct sbic_softc *, struct scsipi_xfer *);
133 int sbicwait (sbic_regmap_p, u_char, int , int);
134 int sbiccheckdmap (void *, u_long, u_long);
135 u_char sbicselectbus (struct sbic_softc *);
136 int sbicxfout (sbic_regmap_p, int, void *);
137 int sbicxfin (sbic_regmap_p, int, void *);
138 int sbicfromscsiperiod (struct sbic_softc *, int);
139 int sbictoscsiperiod (struct sbic_softc *, int);
140 int sbicpoll (struct sbic_softc *);
141 int sbicnextstate (struct sbic_softc *, u_char, u_char);
142 int sbicmsgin (struct sbic_softc *);
143 int sbicabort (struct sbic_softc *, const char *);
144 void sbicxfdone (struct sbic_softc *);
145 void sbicerror (struct sbic_softc *, u_char);
146 void sbicreset (struct sbic_softc *);
147 void sbic_scsidone (struct sbic_acb *, int);
148 void sbic_sched (struct sbic_softc *);
149 void sbic_save_ptrs (struct sbic_softc *);
150 void sbic_load_ptrs (struct sbic_softc *);
153 * Synch xfer parameters, and timing conversions
155 int sbic_min_period = SBIC_SYN_MIN_PERIOD; /* in cycles = f(ICLK,FSn) */
156 int sbic_max_offset = SBIC_SYN_MAX_OFFSET; /* pure number */
157 int sbic_cmd_wait = SBIC_CMD_WAIT;
158 int sbic_data_wait = SBIC_DATA_WAIT;
159 int sbic_init_wait = SBIC_INIT_WAIT;
162 * was broken before.. now if you want this you get it for all drives
163 * on sbic controllers.
165 u_char sbic_inhibit_sync[8];
166 int sbic_enable_reselect = 1; /* Allow Disconnect / Reselect */
167 int sbic_no_dma = 0; /* Use PIO transfers instead of DMA */
168 int sbic_parallel_operations = 1; /* Allow command queues */
171 * Some useful stuff for debugging purposes
173 #ifdef DEBUG
174 int sbicdma_ops = 0; /* total DMA operations */
175 int sbicdma_hits = 0; /* number of DMA chains that were contiguous */
176 int sbicdma_misses = 0; /* number of DMA chains that were not contiguous */
177 int sbicdma_saves = 0;
179 #define QPRINTF(a) if (sbic_debug > 1) printf a
181 int sbic_debug = 0; /* Debug all chip related things */
182 int sync_debug = 0; /* Debug all Synchronous Scsi related things */
183 int reselect_debug = 0; /* Debug all reselection related things */
184 int data_pointer_debug = 0; /* Debug Data Pointer related things */
186 void sbictimeout(struct sbic_softc *dev);
188 #else
189 #define QPRINTF(a) /* */
190 #endif
194 * default minphys routine for sbic based controllers
196 void
197 sbic_minphys(struct buf *bp)
200 * No max transfer at this level.
202 minphys(bp);
207 * Save DMA pointers. Take into account partial transfer. Shut down DMA.
209 void
210 sbic_save_ptrs(struct sbic_softc *dev)
212 sbic_regmap_p regs;
213 struct sbic_acb *acb;
214 int count, asr, s;
217 * Only need to save pointers if DMA was active...
219 if (dev->sc_cur == NULL || (dev->sc_flags & SBICF_INDMA) == 0)
220 return;
222 regs = dev->sc_sbicp;
224 s = splbio();
227 * Wait until WD chip is idle
229 do {
230 GET_SBIC_asr(regs, asr);
231 if (asr & SBIC_ASR_DBR) {
232 printf("%s: asr %02x canceled!\n", __func__, asr);
233 splx(s);
234 return;
236 } while(asr & (SBIC_ASR_BSY|SBIC_ASR_CIP));
240 * Save important state.
241 * must be done before dmastop
243 acb = dev->sc_nexus;
244 acb->sc_dmacmd = dev->sc_dmacmd;
247 * Fetch the residual count
249 SBIC_TC_GET(regs, count);
252 * Shut down DMA
254 dev->sc_dmastop(dev);
257 * No longer in DMA
259 dev->sc_flags &= ~SBICF_INDMA;
262 * Ensure the WD chip is back in polled I/O mode, with nothing to
263 * transfer.
265 SBIC_TC_PUT(regs, 0);
266 SET_SBIC_control(regs, SBIC_CTL_EDI | SBIC_CTL_IDI);
269 * Update current count...
271 acb->sc_tcnt = count;
274 * Work out how many bytes were actually transferred
276 count = dev->sc_tcnt - count;
277 dev->sc_tcnt = acb->sc_tcnt;
280 * Fixup partial xfers
282 acb->sc_kv.dc_addr += count;
283 acb->sc_kv.dc_count -= count;
284 acb->sc_pa.dc_addr += count;
285 acb->sc_pa.dc_count -= count >> 1;
287 #ifdef DEBUG
288 if (data_pointer_debug)
289 printf("save at (%p,%x):%x\n",
290 dev->sc_cur->dc_addr, dev->sc_cur->dc_count,count);
291 sbicdma_saves++;
292 #endif
294 splx(s);
299 * DOES NOT RESTART DMA!!!
301 void
302 sbic_load_ptrs(struct sbic_softc *dev)
304 struct sbic_acb *acb = dev->sc_nexus;
305 int s;
307 if (acb->sc_kv.dc_count == 0) {
309 * No data to xfer
311 return;
314 s = splbio();
317 * Reset the Scatter-Gather chain
319 dev->sc_last = dev->sc_cur = &acb->sc_pa;
322 * Restore the Transfer Count and DMA specific data
324 dev->sc_tcnt = acb->sc_tcnt;
325 dev->sc_dmacmd = acb->sc_dmacmd;
327 #ifdef DEBUG
328 sbicdma_ops++;
329 #endif
332 * Need to fixup new segment?
334 if (dev->sc_tcnt == 0) {
336 * sc_tcnt == 0 implies end of segment
338 char *vaddr, *paddr;
339 int count;
342 * do kvm to pa mappings
344 vaddr = acb->sc_kv.dc_addr;
345 paddr = acb->sc_pa.dc_addr = (char *)kvtop((void *)vaddr);
347 for (count = (PAGE_SIZE - ((int)vaddr & PGOFSET));
348 count < acb->sc_kv.dc_count &&
349 (char *)kvtop((void *)(vaddr + count + 4)) ==
350 paddr + count + 4;
351 count += PAGE_SIZE)
352 ; /* Do nothing */
355 * If it's all contiguous...
357 if (count > acb->sc_kv.dc_count) {
358 count = acb->sc_kv.dc_count;
359 #ifdef DEBUG
360 sbicdma_hits++;
361 #endif
363 #ifdef DEBUG
364 else
365 sbicdma_misses++;
366 #endif
368 acb->sc_tcnt = count;
369 acb->sc_pa.dc_count = count >> 1;
371 #ifdef DEBUG
372 if (data_pointer_debug)
373 printf("DMA recalc:kv(%p,%x)pa(%p,%lx)\n",
374 acb->sc_kv.dc_addr,
375 acb->sc_kv.dc_count,
376 acb->sc_pa.dc_addr,
377 acb->sc_tcnt);
378 #endif
382 splx(s);
386 * used by specific sbic controller
388 * it appears that the higher level code does nothing with LUN's
389 * so I will too. I could plug it in, however so could they
390 * in scsi_scsipi_cmd().
392 void
393 sbic_scsi_request(struct scsipi_channel *chan, scsipi_adapter_req_t req,
394 void *arg)
396 struct scsipi_xfer *xs;
397 struct scsipi_periph *periph;
398 struct sbic_softc *dev = (void *)chan->chan_adapter->adapt_dev;
399 struct sbic_acb *acb;
400 int flags, s;
402 switch (req) {
403 case ADAPTER_REQ_RUN_XFER:
404 xs = arg;
405 periph = xs->xs_periph;
406 flags = xs->xs_control;
408 if (flags & XS_CTL_DATA_UIO)
409 panic("sbic: scsi data uio requested");
411 if (dev->sc_nexus && (flags & XS_CTL_POLL))
412 panic("sbic_scsicmd: busy");
414 s = splbio();
416 if ((acb = dev->free_list.tqh_first) != NULL)
417 TAILQ_REMOVE(&dev->free_list, acb, chain);
419 splx(s);
421 if (acb == NULL) {
422 #ifdef DEBUG
423 printf("%s: unable to queue request for target %d\n",
424 __func__, periph->periph_target);
425 #ifdef DDB
426 Debugger();
427 #endif
428 #endif
429 xs->error = XS_RESOURCE_SHORTAGE;
430 scsipi_done(xs);
431 return;
434 if (flags & XS_CTL_DATA_IN)
435 acb->flags = ACB_ACTIVE | ACB_DATAIN;
436 else
437 acb->flags = ACB_ACTIVE;
439 acb->xs = xs;
440 acb->clen = xs->cmdlen;
441 acb->sc_kv.dc_addr = xs->data;
442 acb->sc_kv.dc_count = xs->datalen;
443 acb->pa_addr = xs->data ?
444 (char *)kvtop((void *)xs->data) : 0;
445 memcpy(&acb->cmd, xs->cmd, xs->cmdlen);
447 if (flags & XS_CTL_POLL) {
449 * This has major side effects
450 * -- it locks up the machine
452 int stat;
454 s = splbio();
456 dev->sc_flags |= SBICF_ICMD;
458 do {
460 * If we already had a nexus, while away
461 * the time until idle...
462 * This is likely only to happen if
463 * a reselection occurs between
464 * here and our earlier check for
465 * ICMD && sc_nexus(which would
466 * have resulted in a panic() had it been true).
468 while (dev->sc_nexus)
469 sbicpoll(dev);
472 * Fix up the new nexus
474 dev->sc_nexus = acb;
475 dev->sc_xs = xs;
476 dev->target = periph->periph_target;
477 dev->lun = periph->periph_lun;
479 stat = sbicicmd(dev, &acb->cmd, acb->clen,
480 acb->sc_kv.dc_addr, acb->sc_kv.dc_count);
482 } while (dev->sc_nexus != acb);
484 sbic_scsidone(acb, stat);
486 splx(s);
488 return;
491 s = splbio();
492 TAILQ_INSERT_TAIL(&dev->ready_list, acb, chain);
495 * If nothing is active, try to start it now.
497 if (dev->sc_nexus == NULL)
498 sbic_sched(dev);
500 splx(s);
502 return;
504 case ADAPTER_REQ_GROW_RESOURCES:
505 /* XXX Not supported. */
506 return;
508 case ADAPTER_REQ_SET_XFER_MODE:
509 /* XXX Not supported. */
510 return;
515 * attempt to start the next available command
517 void
518 sbic_sched(struct sbic_softc *dev)
520 struct scsipi_xfer *xs;
521 struct scsipi_periph *periph = NULL; /* Gag the compiler */
522 struct sbic_acb *acb;
523 int flags, stat;
526 * XXXSCW
527 * I'll keep this test here, even though I can't see any obvious way
528 * in which sbic_sched() could be called with sc_nexus non NULL
530 if (dev->sc_nexus)
531 return; /* a command is current active */
534 * Loop through the ready list looking for work to do...
536 for (acb = dev->ready_list.tqh_first; acb; acb = acb->chain.tqe_next) {
537 int i, j;
539 periph = acb->xs->xs_periph;
540 i = periph->periph_target;
541 j = 1 << periph->periph_lun;
544 * We've found a potential command, but is the target/lun busy?
546 if ((dev->sc_tinfo[i].lubusy & j) == 0) {
548 * Nope, it's not busy, so we can use it.
550 dev->sc_tinfo[i].lubusy |= j;
551 TAILQ_REMOVE(&dev->ready_list, acb, chain);
552 dev->sc_nexus = acb;
553 acb->sc_pa.dc_addr = acb->pa_addr; /* XXXX check */
554 break;
558 if (acb == NULL) {
559 QPRINTF(("sbicsched: no work\n"));
560 return; /* did not find an available command */
563 #ifdef DEBUG
564 if (data_pointer_debug > 1)
565 printf("sbic_sched(%d,%d)\n", periph->periph_target,
566 periph->periph_lun);
567 #endif
569 dev->sc_xs = xs = acb->xs;
570 flags = xs->xs_control;
572 if (flags & XS_CTL_RESET)
573 sbicreset(dev);
575 dev->sc_stat[0] = -1;
576 dev->target = periph->periph_target;
577 dev->lun = periph->periph_lun;
579 if (flags & XS_CTL_POLL || (!sbic_parallel_operations &&
580 (sbicdmaok(dev, xs) == 0)) )
581 stat = sbicicmd(dev, &acb->cmd, acb->clen,
582 acb->sc_kv.dc_addr, acb->sc_kv.dc_count);
583 else if (sbicgo(dev, xs) == 0 && xs->error != XS_SELTIMEOUT)
584 return;
585 else
586 stat = dev->sc_stat[0];
588 sbic_scsidone(acb, stat);
591 void
592 sbic_scsidone(struct sbic_acb *acb, int stat)
594 struct scsipi_xfer *xs = acb->xs;
595 struct scsipi_periph *periph = xs->xs_periph;
596 struct sbic_softc *dev =
597 (void *)periph->periph_channel->chan_adapter->adapt_dev;
598 int dosched = 0;
600 #ifdef DIAGNOSTIC
601 if (acb == NULL || xs == NULL) {
602 printf("sbic_scsidone -- (%d,%d) no scsipi_xfer\n",
603 dev->target, dev->lun);
604 #ifdef DDB
605 Debugger();
606 #endif
607 return;
609 #endif
612 #ifdef DEBUG
613 if (data_pointer_debug > 1)
614 printf("scsidone: (%d,%d)->(%d,%d)%02x\n",
615 periph->periph_target, periph->periph_lun,
616 dev->target, dev->lun, stat);
618 if (xs->xs_periph->periph_target == dev->sc_channel.chan_id)
619 panic("target == hostid");
620 #endif
622 xs->status = stat;
623 xs->resid = 0; /* XXXX */
624 if (xs->error == XS_NOERROR) {
625 if (stat == SCSI_CHECK || stat == SCSI_BUSY)
626 xs->error = XS_BUSY;
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.
637 if (acb == dev->sc_nexus ) {
639 dev->sc_nexus = NULL;
640 dev->sc_xs = NULL;
642 dev->sc_tinfo[periph->periph_target].lubusy &=
643 ~(1 << periph->periph_lun);
645 if (dev->ready_list.tqh_first)
646 dosched = 1; /* start next command */
648 } else if (dev->ready_list.tqh_last == &acb->chain.tqe_next) {
650 TAILQ_REMOVE(&dev->ready_list, acb, chain);
652 } else {
654 struct sbic_acb *a;
656 for (a = dev->nexus_list.tqh_first; a != NULL;
657 a = a->chain.tqe_next) {
658 if (a == acb) {
659 TAILQ_REMOVE(&dev->nexus_list, acb, chain);
660 dev->sc_tinfo[periph->periph_target].lubusy &=
661 ~(1 << periph->periph_lun);
662 break;
666 if (a != NULL)
668 else if ( acb->chain.tqe_next ) {
669 TAILQ_REMOVE(&dev->ready_list, acb, chain);
670 } else {
671 printf("%s: can't find matching acb\n",
672 dev->sc_dev.dv_xname);
673 #ifdef DDB
674 Debugger();
675 #endif
680 * Put it on the free list.
682 acb->flags = ACB_FREE;
683 TAILQ_INSERT_HEAD(&dev->free_list, acb, chain);
685 dev->sc_tinfo[periph->periph_target].cmds++;
687 scsipi_done(xs);
689 if (dosched)
690 sbic_sched(dev);
694 sbicdmaok(struct sbic_softc *dev, struct scsipi_xfer *xs)
697 if (sbic_no_dma || xs->datalen == 0 ||
698 xs->datalen & 0x03 || (int)xs->data & 0x03)
699 return 0;
702 * controller supports DMA to any addresses?
704 if ((dev->sc_flags & SBICF_BADDMA) == 0)
705 return 1;
708 * this address is ok for DMA?
710 if (sbiccheckdmap(xs->data, xs->datalen, dev->sc_dmamask) == 0)
711 return 1;
713 return 0;
717 sbicwait(sbic_regmap_p regs, u_char until, int timeo, int line)
719 u_char val;
721 if (timeo == 0)
722 timeo = 1000000; /* some large value.. */
724 GET_SBIC_asr(regs, val);
726 while ((val & until) == 0) {
728 if (timeo-- == 0) {
729 int csr;
730 GET_SBIC_csr(regs, csr);
731 printf("sbicwait TIMEO @%d with asr=x%x csr=x%x\n",
732 line, val, csr);
733 #if defined(DDB) && defined(DEBUG)
734 Debugger();
735 #endif
736 return val; /* Maybe I should abort */
737 break;
740 DELAY(1);
741 GET_SBIC_asr(regs, val);
744 return val;
748 sbicabort(struct sbic_softc *dev, const char *where)
750 sbic_regmap_p regs = dev->sc_sbicp;
751 u_char csr, asr;
753 GET_SBIC_asr(regs, asr);
754 GET_SBIC_csr(regs, csr);
756 printf ("%s: abort %s: csr = 0x%02x, asr = 0x%02x\n",
757 dev->sc_dev.dv_xname, where, csr, asr);
760 * Clean up chip itself
762 if (dev->sc_flags & SBICF_SELECTED) {
764 while (asr & SBIC_ASR_DBR) {
766 * sbic is jammed w/data. need to clear it
767 * But we don't know what direction it needs to go
769 GET_SBIC_data(regs, asr);
770 printf("%s: abort %s: clearing data buffer 0x%02x\n",
771 dev->sc_dev.dv_xname, where, asr);
772 GET_SBIC_asr(regs, asr);
773 if (asr & SBIC_ASR_DBR)
774 /* Not the read direction, then */
775 SET_SBIC_data(regs, asr);
776 GET_SBIC_asr(regs, asr);
779 WAIT_CIP(regs);
781 printf("%s: sbicabort - sending ABORT command\n",
782 dev->sc_dev.dv_xname);
783 SET_SBIC_cmd(regs, SBIC_CMD_ABORT);
784 WAIT_CIP(regs);
786 GET_SBIC_asr(regs, asr);
788 if (asr & (SBIC_ASR_BSY|SBIC_ASR_LCI)) {
790 * ok, get more drastic..
792 printf("%s: sbicabort - asr %x, trying to reset\n",
793 dev->sc_dev.dv_xname, asr);
794 sbicreset(dev);
795 dev->sc_flags &= ~SBICF_SELECTED;
796 return SBIC_STATE_ERROR;
799 printf("%s: sbicabort - sending DISC command\n",
800 dev->sc_dev.dv_xname);
801 SET_SBIC_cmd(regs, SBIC_CMD_DISC);
803 do {
804 SBIC_WAIT (regs, SBIC_ASR_INT, 0);
805 GET_SBIC_asr(regs, asr);
806 GET_SBIC_csr (regs, csr);
807 QPRINTF(("csr: 0x%02x, asr: 0x%02x\n", csr, asr));
808 } while ((csr != SBIC_CSR_DISC) && (csr != SBIC_CSR_DISC_1) &&
809 (csr != SBIC_CSR_CMD_INVALID));
812 * lets just hope it worked..
814 dev->sc_flags &= ~SBICF_SELECTED;
817 return SBIC_STATE_ERROR;
822 * Initialize driver-private structures
824 void
825 sbicinit(struct sbic_softc *dev)
827 u_int i;
829 if ((dev->sc_flags & SBICF_ALIVE) == 0) {
831 struct sbic_acb *acb;
833 TAILQ_INIT(&dev->ready_list);
834 TAILQ_INIT(&dev->nexus_list);
835 TAILQ_INIT(&dev->free_list);
836 callout_init(&dev->sc_timo_ch, 0);
838 dev->sc_nexus = NULL;
839 dev->sc_xs = NULL;
841 acb = dev->sc_acb;
842 memset(acb, 0, sizeof(dev->sc_acb));
844 for (i = 0; i < sizeof(dev->sc_acb) / sizeof(*acb); i++) {
845 TAILQ_INSERT_TAIL(&dev->free_list, acb, chain);
846 acb++;
849 memset(dev->sc_tinfo, 0, sizeof(dev->sc_tinfo));
851 #ifdef DEBUG
853 * make sure timeout is really not needed
855 callout_reset(&dev->sc_timo_ch, 30 * hz,
856 (void *)sbictimeout, dev);
857 #endif
859 } else
860 panic("sbic: reinitializing driver!");
862 dev->sc_flags |= SBICF_ALIVE;
863 dev->sc_flags &= ~SBICF_SELECTED;
866 * initialize inhibit array
867 * Never enable Sync, since it just doesn't work on mvme147 :(
869 for (i = 0; i < 8; ++i)
870 sbic_inhibit_sync[i] = 1;
872 sbicreset(dev);
875 void
876 sbicreset(struct sbic_softc *dev)
878 sbic_regmap_p regs = dev->sc_sbicp;
879 u_int my_id, s;
880 u_char csr;
882 s = splbio();
884 my_id = dev->sc_channel.chan_id & SBIC_ID_MASK;
886 if (dev->sc_clkfreq < 110)
887 my_id |= SBIC_ID_FS_8_10;
888 else if (dev->sc_clkfreq < 160)
889 my_id |= SBIC_ID_FS_12_15;
890 else if (dev->sc_clkfreq < 210)
891 my_id |= SBIC_ID_FS_16_20;
893 SET_SBIC_myid(regs, my_id);
896 * Reset the chip
898 SET_SBIC_cmd(regs, SBIC_CMD_RESET);
899 DELAY(25);
901 SBIC_WAIT(regs, SBIC_ASR_INT, 0);
902 GET_SBIC_csr(regs, csr); /* clears interrupt also */
905 * Set up various chip parameters
907 SET_SBIC_control(regs, SBIC_CTL_EDI | SBIC_CTL_IDI);
910 * don't allow Selection (SBIC_RID_ES)
911 * until we can handle target mode!!
913 SET_SBIC_rselid(regs, SBIC_RID_ER);
916 * Asynchronous for now
918 SET_SBIC_syn(regs, 0);
921 * Anything else was zeroed by reset
923 splx(s);
925 dev->sc_flags &= ~SBICF_SELECTED;
928 void
929 sbicerror(struct sbic_softc *dev, u_char csr)
931 struct scsipi_xfer *xs = dev->sc_xs;
933 #ifdef DIAGNOSTIC
934 if (xs == NULL)
935 panic("sbicerror: dev->sc_xs == NULL");
936 #endif
938 if ( xs->xs_control & XS_CTL_SILENT )
939 return;
941 printf("%s: csr == 0x%02x\n", dev->sc_dev.dv_xname, csr);
945 * select the bus, return when selected or error.
947 * Returns the current CSR following selection and optionally MSG out phase.
948 * i.e. the returned CSR *should* indicate CMD phase...
949 * If the return value is 0, some error happened.
951 u_char
952 sbicselectbus(struct sbic_softc *dev)
954 sbic_regmap_p regs = dev->sc_sbicp;
955 u_char target = dev->target, lun = dev->lun, asr, csr, id;
958 * if we're already selected, return (XXXX panic maybe?)
960 if (dev->sc_flags & SBICF_SELECTED)
961 return 0;
963 QPRINTF(("sbicselectbus %d: ", target));
966 * issue select
968 SET_SBIC_selid(regs, target);
969 SET_SBIC_timeo(regs, SBIC_TIMEOUT(250, dev->sc_clkfreq));
971 GET_SBIC_asr(regs, asr);
973 if (asr & (SBIC_ASR_INT|SBIC_ASR_BSY)) {
975 * This means we got ourselves reselected upon
977 QPRINTF(("WD busy (reselect?)\n"));
978 return 0;
981 SET_SBIC_cmd(regs, SBIC_CMD_SEL_ATN);
984 * wait for select (merged from separate function may need
985 * cleanup)
987 WAIT_CIP(regs);
989 do {
991 asr = SBIC_WAIT(regs, SBIC_ASR_INT | SBIC_ASR_LCI, 0);
993 if (asr & SBIC_ASR_LCI) {
994 QPRINTF(("late LCI: asr %02x\n", asr));
995 return 0;
999 * Clear interrupt
1001 GET_SBIC_csr (regs, csr);
1003 QPRINTF(("%02x ", csr));
1006 * Reselected from under our feet?
1008 if (csr == SBIC_CSR_RSLT_NI || csr == SBIC_CSR_RSLT_IFY) {
1009 QPRINTF(("got reselected, asr %02x\n", asr));
1011 * We need to handle this now so we don't lock up later
1013 sbicnextstate(dev, csr, asr);
1015 return 0;
1019 * Whoops!
1021 if (csr == SBIC_CSR_SLT || csr == SBIC_CSR_SLT_ATN) {
1022 panic("sbicselectbus: target issued select!");
1023 return 0;
1026 } while (csr != (SBIC_CSR_MIS_2 | MESG_OUT_PHASE) &&
1027 csr != (SBIC_CSR_MIS_2 | CMD_PHASE) &&
1028 csr != SBIC_CSR_SEL_TIMEO);
1031 * Anyone at home?
1033 if (csr == SBIC_CSR_SEL_TIMEO) {
1034 dev->sc_xs->error = XS_SELTIMEOUT;
1035 QPRINTF(("Selection Timeout\n"));
1036 return 0;
1039 QPRINTF(("Selection Complete\n"));
1042 * Assume we're now selected
1044 GET_SBIC_selid(regs, id);
1045 dev->target = id;
1046 dev->lun = lun;
1047 dev->sc_flags |= SBICF_SELECTED;
1050 * Enable (or not) reselection
1051 * XXXSCW This is probably not necessary since we don't use use the
1052 * Select-and-Xfer-with-ATN command to initiate a selection...
1054 if (!sbic_enable_reselect && dev->nexus_list.tqh_first == NULL)
1055 SET_SBIC_rselid (regs, 0);
1056 else
1057 SET_SBIC_rselid (regs, SBIC_RID_ER);
1060 * We only really need to do anything when the target goes to MSG out
1061 * If the device ignored ATN, it's probably old and brain-dead,
1062 * but we'll try to support it anyhow.
1063 * If it doesn't support message out, it definately doesn't
1064 * support synchronous transfers, so no point in even asking...
1066 if (csr == (SBIC_CSR_MIS_2 | MESG_OUT_PHASE)) {
1068 * Send identify message (SCSI-2 requires an identify msg)
1070 if (sbic_inhibit_sync[id] &&
1071 dev->sc_sync[id].state == SYNC_START) {
1073 * Handle drives that don't want to be asked
1074 * whether to go sync at all.
1076 dev->sc_sync[id].offset = 0;
1077 dev->sc_sync[id].period = sbic_min_period;
1078 dev->sc_sync[id].state = SYNC_DONE;
1082 * Do we need to negotiate Synchronous Xfers for this target?
1084 if (dev->sc_sync[id].state != SYNC_START) {
1086 * Nope, we've already negotiated.
1087 * Now see if we should allow the target to
1088 * disconnect/reselect...
1090 if (dev->sc_xs->xs_control & XS_CTL_POLL ||
1091 dev->sc_flags & SBICF_ICMD ||
1092 !sbic_enable_reselect)
1093 SEND_BYTE (regs, MSG_IDENTIFY | lun);
1094 else
1095 SEND_BYTE (regs, MSG_IDENTIFY_DR | lun);
1097 } else {
1099 * try to initiate a sync transfer.
1100 * So compose the sync message we're going
1101 * to send to the target
1103 #ifdef DEBUG
1104 if (sync_debug)
1105 printf("\nSending sync request "
1106 "to target %d ... ", id);
1107 #endif
1109 * setup scsi message sync message request
1111 dev->sc_msg[0] = MSG_IDENTIFY | lun;
1112 dev->sc_msg[1] = MSG_EXT_MESSAGE;
1113 dev->sc_msg[2] = 3;
1114 dev->sc_msg[3] = MSG_SYNC_REQ;
1115 dev->sc_msg[4] = sbictoscsiperiod(dev, sbic_min_period);
1116 dev->sc_msg[5] = sbic_max_offset;
1118 sbicxfout(regs, 6, dev->sc_msg);
1120 dev->sc_sync[id].state = SYNC_SENT;
1121 #ifdef DEBUG
1122 if (sync_debug)
1123 printf ("sent\n");
1124 #endif
1128 * There's one interrupt still to come: the change to
1129 * CMD phase...
1131 SBIC_WAIT(regs, SBIC_ASR_INT , 0);
1132 GET_SBIC_csr(regs, csr);
1136 * set sync or async
1138 if (dev->sc_sync[target].state == SYNC_DONE) {
1139 #ifdef DEBUG
1140 if (sync_debug)
1141 printf("select(%d): sync reg = 0x%02x\n", target,
1142 SBIC_SYN(dev->sc_sync[target].offset,
1143 dev->sc_sync[target].period));
1144 #endif
1145 SET_SBIC_syn(regs, SBIC_SYN(dev->sc_sync[target].offset,
1146 dev->sc_sync[target].period));
1147 } else {
1148 #ifdef DEBUG
1149 if (sync_debug)
1150 printf("select(%d): sync reg = 0x%02x\n", target,
1151 SBIC_SYN(0,sbic_min_period));
1152 #endif
1153 SET_SBIC_syn(regs, SBIC_SYN(0, sbic_min_period));
1156 return csr;
1160 * Information Transfer *to* a Scsi Target.
1162 * Note: Don't expect there to be an interrupt immediately after all
1163 * the data is transferred out. The WD spec sheet says that the Transfer-
1164 * Info command for non-MSG_IN phases only completes when the target
1165 * next asserts 'REQ'. That is, when the SCSI bus changes to a new state.
1167 * This can have a nasty effect on commands which take a relatively long
1168 * time to complete, for example a START/STOP unit command may remain in
1169 * CMD phase until the disk has spun up. Only then will the target change
1170 * to STATUS phase. This is really only a problem for immediate commands
1171 * since we don't allow disconnection for them (yet).
1174 sbicxfout(sbic_regmap_p regs, int len, void *bp)
1176 int wait = sbic_data_wait;
1177 u_char asr, *buf = bp;
1179 QPRINTF(("sbicxfout {%d} %02x %02x %02x %02x %02x "
1180 "%02x %02x %02x %02x %02x\n", len, buf[0], buf[1], buf[2],
1181 buf[3], buf[4], buf[5], buf[6], buf[7], buf[8], buf[9]));
1184 * sigh.. WD-PROTO strikes again.. sending the command in one go
1185 * causes the chip to lock up if talking to certain (misbehaving?)
1186 * targets. Anyway, this procedure should work for all targets, but
1187 * it's slightly slower due to the overhead
1189 WAIT_CIP (regs);
1191 SBIC_TC_PUT (regs, 0);
1192 SET_SBIC_control(regs, SBIC_CTL_EDI | SBIC_CTL_IDI);
1193 SBIC_TC_PUT (regs, (unsigned)len);
1194 SET_SBIC_cmd (regs, SBIC_CMD_XFER_INFO);
1197 * Loop for each byte transferred
1199 do {
1201 GET_SBIC_asr(regs, asr);
1203 if (asr & SBIC_ASR_DBR) {
1204 if (len) {
1205 SET_SBIC_data (regs, *buf);
1206 buf++;
1207 len--;
1208 } else {
1209 SET_SBIC_data (regs, 0);
1211 wait = sbic_data_wait;
1214 } while (len && (asr & SBIC_ASR_INT) == 0 && wait-- > 0);
1216 #ifdef DEBUG
1217 QPRINTF(("sbicxfout done: %d bytes remaining (wait:%d)\n", len, wait));
1218 #endif
1221 * Normally, an interrupt will be pending when this routing returns.
1223 return len;
1227 * Information Transfer *from* a Scsi Target
1228 * returns # bytes left to read
1231 sbicxfin(sbic_regmap_p regs, int len, void *bp)
1233 int wait = sbic_data_wait;
1234 u_char *buf = bp;
1235 u_char asr;
1236 #ifdef DEBUG
1237 u_char *obp = bp;
1238 #endif
1240 WAIT_CIP(regs);
1242 SET_SBIC_control(regs, SBIC_CTL_EDI | SBIC_CTL_IDI);
1243 SBIC_TC_PUT(regs, (unsigned int)len);
1244 SET_SBIC_cmd (regs, SBIC_CMD_XFER_INFO);
1247 * Loop for each byte transferred
1249 do {
1251 GET_SBIC_asr(regs, asr);
1253 if (asr & SBIC_ASR_DBR) {
1254 if (len) {
1255 GET_SBIC_data (regs, *buf);
1256 buf++;
1257 len--;
1258 } else {
1259 u_char foo;
1260 GET_SBIC_data (regs, foo);
1262 wait = sbic_data_wait;
1265 } while ((asr & SBIC_ASR_INT) == 0 && wait-- > 0);
1267 QPRINTF(("sbicxfin {%d} %02x %02x %02x %02x %02x %02x "
1268 "%02x %02x %02x %02x\n", len, obp[0], obp[1], obp[2],
1269 obp[3], obp[4], obp[5], obp[6], obp[7], obp[8], obp[9]));
1271 SBIC_TC_PUT (regs, 0);
1274 * this leaves with one csr to be read
1276 return len;
1280 * SCSI 'immediate' command: issue a command to some SCSI device
1281 * and get back an 'immediate' response (i.e., do programmed xfer
1282 * to get the response data). 'cbuf' is a buffer containing a scsi
1283 * command of length clen bytes. 'buf' is a buffer of length 'len'
1284 * bytes for data. The transfer direction is determined by the device
1285 * (i.e., by the scsi bus data xfer phase). If 'len' is zero, the
1286 * command must supply no data.
1288 * Note that although this routine looks like it can handle disconnect/
1289 * reselect, the fact is that it can't. There is still some work to be
1290 * done to clean this lot up.
1293 sbicicmd(struct sbic_softc *dev, void *cbuf, int clen, void *buf, int len)
1295 sbic_regmap_p regs = dev->sc_sbicp;
1296 struct sbic_acb *acb = dev->sc_nexus;
1297 u_char csr, asr;
1298 int still_busy = SBIC_STATE_RUNNING;
1301 * Make sure pointers are OK
1303 dev->sc_last = dev->sc_cur = &acb->sc_pa;
1304 dev->sc_tcnt = acb->sc_tcnt = 0;
1306 acb->sc_dmacmd = 0;
1307 acb->sc_pa.dc_count = 0; /* No DMA */
1308 acb->sc_kv.dc_addr = buf;
1309 acb->sc_kv.dc_count = len;
1311 #ifdef DEBUG
1312 if (data_pointer_debug > 1)
1313 printf("sbicicmd(%d,%d):%d\n",
1314 dev->target, dev->lun, acb->sc_kv.dc_count);
1315 #endif
1318 * set the sbic into non-DMA mode
1320 SET_SBIC_control(regs, SBIC_CTL_EDI | SBIC_CTL_IDI);
1322 dev->sc_stat[0] = 0xff;
1323 dev->sc_msg[0] = 0xff;
1326 * We're stealing the SCSI bus
1328 dev->sc_flags |= SBICF_ICMD;
1330 do {
1331 GET_SBIC_asr(regs, asr);
1334 * select the SCSI bus (it's an error if bus isn't free)
1336 if ((dev->sc_flags & SBICF_SELECTED) == 0 &&
1337 still_busy != SBIC_STATE_DISCONNECT) {
1338 if ((csr = sbicselectbus(dev)) == 0) {
1339 dev->sc_flags &= ~SBICF_ICMD;
1340 return -1;
1342 } else if ((asr & (SBIC_ASR_BSY | SBIC_ASR_INT)) ==
1343 SBIC_ASR_INT)
1344 GET_SBIC_csr(regs, csr);
1345 else
1346 csr = 0;
1348 if (csr) {
1350 QPRINTF((">ASR:0x%02x CSR:0x%02x< ", asr, csr));
1352 switch (csr) {
1354 case SBIC_CSR_S_XFERRED:
1355 case SBIC_CSR_DISC:
1356 case SBIC_CSR_DISC_1:
1358 u_char phase;
1360 dev->sc_flags &= ~SBICF_SELECTED;
1361 GET_SBIC_cmd_phase(regs, phase);
1363 if (phase == 0x60) {
1364 GET_SBIC_tlun(regs, dev->sc_stat[0]);
1365 still_busy = SBIC_STATE_DONE; /* done */
1366 } else {
1367 #ifdef DEBUG
1368 if (reselect_debug > 1)
1369 printf("sbicicmd: "
1370 "handling disconnect\n");
1371 #endif
1372 still_busy = SBIC_STATE_DISCONNECT;
1374 break;
1377 case SBIC_CSR_XFERRED | CMD_PHASE:
1378 case SBIC_CSR_MIS | CMD_PHASE:
1379 case SBIC_CSR_MIS_1 | CMD_PHASE:
1380 case SBIC_CSR_MIS_2 | CMD_PHASE:
1382 if (sbicxfout(regs, clen, cbuf))
1383 still_busy = sbicabort(dev,
1384 "icmd sending cmd");
1385 break;
1387 case SBIC_CSR_XFERRED | STATUS_PHASE:
1388 case SBIC_CSR_MIS | STATUS_PHASE:
1389 case SBIC_CSR_MIS_1 | STATUS_PHASE:
1390 case SBIC_CSR_MIS_2 | STATUS_PHASE:
1393 * The sbic does the status/cmd-complete
1394 * reading ok, so do this with its
1395 * hi-level commands.
1397 #ifdef DEBUG
1398 if (sbic_debug)
1399 printf("SBICICMD status phase "
1400 "(bsy=%d)\n", still_busy);
1401 #endif
1402 SET_SBIC_cmd_phase(regs, 0x46);
1403 SET_SBIC_cmd(regs, SBIC_CMD_SEL_ATN_XFER);
1404 break;
1406 default:
1407 still_busy = sbicnextstate(dev, csr, asr);
1408 break;
1412 * make sure the last command was taken,
1413 * ie. we're not hunting after an ignored command..
1415 GET_SBIC_asr(regs, asr);
1418 * tapes may take a loooong time..
1420 while (asr & SBIC_ASR_BSY) {
1422 if (asr & SBIC_ASR_DBR) {
1423 int i;
1425 printf("sbicicmd: "
1426 "Waiting while sbic is jammed, "
1427 "CSR:%02x,ASR:%02x\n", csr, asr);
1428 #ifdef DDB
1429 Debugger();
1430 #endif
1432 * SBIC is jammed
1433 * DUNNO which direction
1434 * Try old direction
1436 GET_SBIC_data(regs, i);
1437 GET_SBIC_asr(regs, asr);
1439 if (asr & SBIC_ASR_DBR)
1440 /* Wants us to write */
1441 SET_SBIC_data(regs, i);
1444 GET_SBIC_asr(regs, asr);
1449 * wait for last command to complete
1451 if (asr & SBIC_ASR_LCI) {
1452 printf("sbicicmd: last command ignored\n");
1453 } else if (still_busy >= SBIC_STATE_RUNNING) /* Bsy */
1454 SBIC_WAIT(regs, SBIC_ASR_INT, sbic_cmd_wait);
1457 * do it again
1459 } while (still_busy >= SBIC_STATE_RUNNING && dev->sc_stat[0] == 0xff);
1462 * Sometimes we need to do an extra read of the CSR
1464 GET_SBIC_csr(regs, csr);
1466 #ifdef DEBUG
1467 if (data_pointer_debug > 1)
1468 printf("sbicicmd done(%d,%d):%d =%d=\n", dev->target, dev->lun,
1469 acb->sc_kv.dc_count, dev->sc_stat[0]);
1470 #endif
1472 dev->sc_flags &= ~SBICF_ICMD;
1474 return dev->sc_stat[0];
1478 * Finish SCSI xfer command: After the completion interrupt from
1479 * a read/write operation, sequence through the final phases in
1480 * programmed i/o. This routine is a lot like sbicicmd except we
1481 * skip (and don't allow) the select, cmd out and data in/out phases.
1483 void
1484 sbicxfdone(struct sbic_softc *dev)
1486 sbic_regmap_p regs = dev->sc_sbicp;
1487 u_char phase, csr;
1488 int s;
1490 QPRINTF(("{"));
1491 s = splbio();
1494 * have the sbic complete on its own
1496 SBIC_TC_PUT(regs, 0);
1497 SET_SBIC_cmd_phase(regs, 0x46);
1498 SET_SBIC_cmd(regs, SBIC_CMD_SEL_ATN_XFER);
1500 do {
1502 SBIC_WAIT(regs, SBIC_ASR_INT, 0);
1503 GET_SBIC_csr(regs, csr);
1504 QPRINTF(("%02x:", csr));
1506 } while ((csr != SBIC_CSR_DISC) && (csr != SBIC_CSR_DISC_1) &&
1507 (csr != SBIC_CSR_S_XFERRED));
1509 dev->sc_flags &= ~SBICF_SELECTED;
1511 GET_SBIC_cmd_phase (regs, phase);
1512 QPRINTF(("}%02x", phase));
1514 if (phase == 0x60)
1515 GET_SBIC_tlun(regs, dev->sc_stat[0]);
1516 else
1517 sbicerror(dev, csr);
1519 QPRINTF(("=STS:%02x=\n", dev->sc_stat[0]));
1521 splx(s);
1525 * No DMA chains
1528 sbicgo(struct sbic_softc *dev, struct scsipi_xfer *xs)
1530 struct sbic_acb *acb = dev->sc_nexus;
1531 sbic_regmap_p regs = dev->sc_sbicp;
1532 int i, dmaflags, count, usedma;
1533 u_char csr, asr, *addr;
1535 dev->target = xs->xs_periph->periph_target;
1536 dev->lun = xs->xs_periph->periph_lun;
1538 usedma = sbicdmaok(dev, xs);
1540 #ifdef DEBUG
1541 if (data_pointer_debug > 1)
1542 printf("sbicgo(%d,%d): usedma=%d\n",
1543 dev->target, dev->lun, usedma);
1544 #endif
1547 * select the SCSI bus (it's an error if bus isn't free)
1549 if ((csr = sbicselectbus(dev)) == 0)
1550 return 0; /* Not done: needs to be rescheduled */
1552 dev->sc_stat[0] = 0xff;
1555 * Calculate DMA chains now
1557 if (acb->flags & ACB_DATAIN)
1558 dmaflags = DMAGO_READ;
1559 else
1560 dmaflags = 0;
1562 addr = acb->sc_kv.dc_addr;
1563 count = acb->sc_kv.dc_count;
1565 if (count && ((char *)kvtop((void *)addr) != acb->sc_pa.dc_addr)) {
1566 printf("sbic: DMA buffer mapping changed %p->%x\n",
1567 acb->sc_pa.dc_addr, kvtop((void *)addr));
1568 #ifdef DDB
1569 Debugger();
1570 #endif
1573 #ifdef DEBUG
1574 ++sbicdma_ops; /* count total DMA operations */
1575 #endif
1578 * Allocate the DMA chain
1579 * Mark end of segment...
1581 acb->sc_tcnt = dev->sc_tcnt = 0;
1582 acb->sc_pa.dc_count = 0;
1584 sbic_load_ptrs(dev);
1587 * Enable interrupts but don't do any DMA
1588 * enintr() also enables interrupts for the sbic
1590 dev->sc_enintr(dev);
1592 if (usedma) {
1593 dev->sc_tcnt = dev->sc_dmago(dev, acb->sc_pa.dc_addr,
1594 acb->sc_pa.dc_count, dmaflags);
1595 #ifdef DEBUG
1596 dev->sc_dmatimo = dev->sc_tcnt ? 1 : 0;
1597 #endif
1598 } else
1599 dev->sc_dmacmd = 0; /* Don't use DMA */
1601 acb->sc_dmacmd = dev->sc_dmacmd;
1603 #ifdef DEBUG
1604 if (data_pointer_debug > 1) {
1605 printf("sbicgo dmago:%d(%p:%lx) dmacmd=0x%02x\n", dev->target,
1606 dev->sc_cur->dc_addr,
1607 dev->sc_tcnt,
1608 dev->sc_dmacmd);
1610 #endif
1613 * Lets cycle a while then let the interrupt handler take over.
1615 GET_SBIC_asr(regs, asr);
1617 do {
1619 QPRINTF(("go "));
1622 * Handle the new phase
1624 i = sbicnextstate(dev, csr, asr);
1625 #if 0
1626 WAIT_CIP(regs);
1627 #endif
1628 if (i == SBIC_STATE_RUNNING) {
1629 GET_SBIC_asr(regs, asr);
1631 if (asr & SBIC_ASR_LCI)
1632 printf("sbicgo: LCI asr:%02x csr:%02x\n",
1633 asr, csr);
1635 if (asr & SBIC_ASR_INT)
1636 GET_SBIC_csr(regs, csr);
1639 } while (i == SBIC_STATE_RUNNING && asr & (SBIC_ASR_INT|SBIC_ASR_LCI));
1641 if (i == SBIC_STATE_DONE) {
1642 if (dev->sc_stat[0] == 0xff)
1643 #if 0
1644 printf("sbicgo: done & stat = 0xff\n");
1645 #else
1647 #endif
1648 else
1649 return 1; /* Did we really finish that fast? */
1652 return 0;
1657 sbicintr(struct sbic_softc *dev)
1659 sbic_regmap_p regs = dev->sc_sbicp;
1660 u_char asr, csr;
1661 int i;
1664 * pending interrupt?
1666 GET_SBIC_asr (regs, asr);
1667 if ((asr & SBIC_ASR_INT) == 0)
1668 return 0;
1670 GET_SBIC_csr(regs, csr);
1672 do {
1674 QPRINTF(("intr[0x%x]", csr));
1676 i = sbicnextstate(dev, csr, asr);
1677 #if 0
1678 WAIT_CIP(regs);
1679 #endif
1680 if (i == SBIC_STATE_RUNNING) {
1681 GET_SBIC_asr(regs, asr);
1683 if (asr & SBIC_ASR_LCI)
1684 printf("sbicgo: LCI asr:%02x csr:%02x\n", asr, csr);
1686 if (asr & SBIC_ASR_INT)
1687 GET_SBIC_csr(regs, csr);
1690 } while (i == SBIC_STATE_RUNNING && asr & (SBIC_ASR_INT|SBIC_ASR_LCI));
1692 QPRINTF(("intr done. state=%d, asr=0x%02x\n", i, asr));
1694 return 1;
1698 * Run commands and wait for disconnect.
1699 * This is only ever called when a command is in progress, when we
1700 * want to busy wait for it to finish.
1703 sbicpoll(struct sbic_softc *dev)
1705 sbic_regmap_p regs = dev->sc_sbicp;
1706 u_char asr, csr = SBIC_CSR_RESET; /* XXX: Quell un-init warning */
1707 int i;
1710 * Wait for the next interrupt
1712 SBIC_WAIT(regs, SBIC_ASR_INT, sbic_cmd_wait);
1714 do {
1715 GET_SBIC_asr (regs, asr);
1717 if (asr & SBIC_ASR_INT)
1718 GET_SBIC_csr(regs, csr);
1720 QPRINTF(("poll[0x%x]", csr));
1723 * Handle it
1725 i = sbicnextstate(dev, csr, asr);
1727 WAIT_CIP(regs);
1728 GET_SBIC_asr(regs, asr);
1731 * tapes may take a loooong time..
1733 while (asr & SBIC_ASR_BSY) {
1734 u_char z = 0;
1736 if (asr & SBIC_ASR_DBR) {
1737 printf("sbipoll: Waiting while sbic is jammed,"
1738 " CSR:%02x,ASR:%02x\n", csr, asr);
1739 #ifdef DDB
1740 Debugger();
1741 #endif
1743 * SBIC is jammed
1744 * DUNNO which direction
1745 * Try old direction
1747 GET_SBIC_data(regs, z);
1748 GET_SBIC_asr(regs, asr);
1750 if (asr & SBIC_ASR_DBR) /* Wants us to write */
1751 SET_SBIC_data(regs, z);
1754 GET_SBIC_asr(regs, asr);
1757 if (asr & SBIC_ASR_LCI)
1758 printf("sbicpoll: LCI asr:%02x csr:%02x\n", asr,csr);
1759 else if (i == SBIC_STATE_RUNNING) /* BSY */
1760 SBIC_WAIT(regs, SBIC_ASR_INT, sbic_cmd_wait);
1762 } while (i == SBIC_STATE_RUNNING);
1764 return 1;
1768 * Handle a single msgin
1771 sbicmsgin(struct sbic_softc *dev)
1773 sbic_regmap_p regs = dev->sc_sbicp;
1774 int recvlen = 1;
1775 u_char asr, csr, *tmpaddr, *msgaddr;
1777 tmpaddr = msgaddr = dev->sc_msg;
1779 tmpaddr[0] = 0xff;
1780 tmpaddr[1] = 0xff;
1782 GET_SBIC_asr(regs, asr);
1784 #ifdef DEBUG
1785 if (reselect_debug > 1)
1786 printf("sbicmsgin asr=%02x\n", asr);
1787 #endif
1789 GET_SBIC_selid (regs, csr);
1790 SET_SBIC_selid (regs, csr | SBIC_SID_FROM_SCSI);
1792 SBIC_TC_PUT(regs, 0);
1793 SET_SBIC_control(regs, SBIC_CTL_EDI | SBIC_CTL_IDI);
1795 do {
1796 while (recvlen--) {
1799 * Fetch the next byte of the message
1801 RECV_BYTE(regs, *tmpaddr);
1804 * get the command completion interrupt, or we
1805 * can't send a new command (LCI)
1807 SBIC_WAIT(regs, SBIC_ASR_INT, 0);
1808 GET_SBIC_csr(regs, csr);
1810 #ifdef DEBUG
1811 if (reselect_debug > 1)
1812 printf("sbicmsgin: got %02x csr %02x\n",
1813 *tmpaddr, csr);
1814 #endif
1816 tmpaddr++;
1818 if (recvlen) {
1820 * Clear ACK, and wait for the interrupt
1821 * for the next byte
1823 SET_SBIC_cmd(regs, SBIC_CMD_CLR_ACK);
1824 SBIC_WAIT(regs, SBIC_ASR_INT, 0);
1825 GET_SBIC_csr(regs, csr);
1829 if (msgaddr[0] == 0xff) {
1830 printf("sbicmsgin: sbic swallowed our message\n");
1831 break;
1834 #ifdef DEBUG
1835 if (sync_debug) {
1836 GET_SBIC_asr(regs, asr);
1837 printf("msgin done csr 0x%x asr 0x%x msg 0x%x\n",
1838 csr, asr, msgaddr[0]);
1840 #endif
1842 * test whether this is a reply to our sync
1843 * request
1845 if (MSG_ISIDENTIFY(msgaddr[0])) {
1848 * Got IFFY msg -- ack it
1850 QPRINTF(("IFFY"));
1852 } else if (msgaddr[0] == MSG_REJECT &&
1853 dev->sc_sync[dev->target].state == SYNC_SENT) {
1856 * Target probably rejected our Sync negotiation.
1858 QPRINTF(("REJECT of SYN"));
1860 #ifdef DEBUG
1861 if (sync_debug)
1862 printf("target %d rejected sync, going async\n",
1863 dev->target);
1864 #endif
1866 dev->sc_sync[dev->target].period = sbic_min_period;
1867 dev->sc_sync[dev->target].offset = 0;
1868 dev->sc_sync[dev->target].state = SYNC_DONE;
1869 SET_SBIC_syn(regs,
1870 SBIC_SYN(dev->sc_sync[dev->target].offset,
1871 dev->sc_sync[dev->target].period));
1873 } else if (msgaddr[0] == MSG_REJECT) {
1876 * we'll never REJECt a REJECT message..
1878 QPRINTF(("REJECT"));
1880 } else if (msgaddr[0] == MSG_SAVE_DATA_PTR) {
1883 * don't reject this either.
1885 QPRINTF(("MSG_SAVE_DATA_PTR"));
1887 } else if (msgaddr[0] == MSG_RESTORE_PTR) {
1890 * don't reject this either.
1892 QPRINTF(("MSG_RESTORE_PTR"));
1894 } else if (msgaddr[0] == MSG_DISCONNECT) {
1897 * Target is disconnecting...
1899 QPRINTF(("DISCONNECT"));
1901 #ifdef DEBUG
1902 if (reselect_debug > 1 && msgaddr[0] == MSG_DISCONNECT)
1903 printf("sbicmsgin: got disconnect msg %s\n",
1904 (dev->sc_flags & SBICF_ICMD) ?
1905 "rejecting" : "");
1906 #endif
1908 if (dev->sc_flags & SBICF_ICMD) {
1910 * We're in immediate mode. Prevent disconnects.
1911 * prepare to reject the message, NACK
1913 SET_SBIC_cmd(regs, SBIC_CMD_SET_ATN);
1914 WAIT_CIP(regs);
1917 } else if (msgaddr[0] == MSG_CMD_COMPLETE) {
1920 * !! KLUDGE ALERT !! quite a few drives don't seem to
1921 * really like the current way of sending the
1922 * sync-handshake together with the ident-message, and
1923 * they react by sending command-complete and
1924 * disconnecting right after returning the valid sync
1925 * handshake. So, all I can do is reselect the drive,
1926 * and hope it won't disconnect again. I don't think
1927 * this is valid behavior, but I can't help fixing a
1928 * problem that apparently exists.
1930 * Note: we should not get here on `normal' command
1931 * completion, as that condition is handled by the
1932 * high-level sel&xfer resume command used to walk
1933 * thru status/cc-phase.
1935 QPRINTF(("CMD_COMPLETE"));
1937 #ifdef DEBUG
1938 if (sync_debug)
1939 printf("GOT MSG %d! target %d acting weird.."
1940 " waiting for disconnect...\n",
1941 msgaddr[0], dev->target);
1942 #endif
1945 * Check to see if sbic is handling this
1947 GET_SBIC_asr(regs, asr);
1950 * XXXSCW: I'm not convinced of this,
1951 * we haven't negated ACK yet...
1953 if (asr & SBIC_ASR_BSY)
1954 return SBIC_STATE_RUNNING;
1957 * Let's try this: Assume it works and set status to 00
1959 dev->sc_stat[0] = 0;
1961 } else if (msgaddr[0] == MSG_EXT_MESSAGE &&
1962 tmpaddr == &(msgaddr[1])) {
1965 * Target is sending us an extended message.
1966 * We'll assume it's the response to our Sync.
1967 * negotiation.
1969 QPRINTF(("ExtMSG\n"));
1972 * Read in whole extended message.
1973 * First, negate ACK to accept
1974 * the MSG_EXT_MESSAGE byte...
1976 SET_SBIC_cmd(regs, SBIC_CMD_CLR_ACK);
1979 * Wait for the interrupt for the next byte (length)
1981 SBIC_WAIT(regs, SBIC_ASR_INT, 0);
1982 GET_SBIC_csr(regs, csr);
1984 #ifdef DEBUG
1985 QPRINTF(("CLR ACK csr %02x\n", csr));
1986 #endif
1989 * Read the length byte
1991 RECV_BYTE(regs, *tmpaddr);
1994 * Wait for command completion IRQ
1996 SBIC_WAIT(regs, SBIC_ASR_INT, 0);
1997 GET_SBIC_csr(regs, csr);
2000 * Reload the loop counter
2002 recvlen = *tmpaddr++;
2004 QPRINTF(("Recving ext msg, csr %02x len %02x\n",
2005 csr, recvlen));
2007 } else if (msgaddr[0] == MSG_EXT_MESSAGE &&
2008 msgaddr[1] == 3 &&
2009 msgaddr[2] == MSG_SYNC_REQ ) {
2012 * We've received the complete Extended Message Sync.
2013 * Request...
2015 QPRINTF(("SYN"));
2018 * Compute the required Transfer Period for
2019 * the WD chip...
2021 dev->sc_sync[dev->target].period =
2022 sbicfromscsiperiod(dev, msgaddr[3]);
2023 dev->sc_sync[dev->target].offset = msgaddr[4];
2024 dev->sc_sync[dev->target].state = SYNC_DONE;
2027 * Put the WD chip in synchronous mode
2029 SET_SBIC_syn(regs,
2030 SBIC_SYN(dev->sc_sync[dev->target].offset,
2031 dev->sc_sync[dev->target].period));
2032 #ifdef DEBUG
2033 if (sync_debug)
2034 printf("msgin(%d): sync reg = 0x%02x\n",
2035 dev->target,
2036 SBIC_SYN(dev->sc_sync[dev->target].offset,
2037 dev->sc_sync[dev->target].period));
2038 #endif
2040 printf("%s: target %d now synchronous, "
2041 "period=%dns, offset=%d.\n",
2042 dev->sc_dev.dv_xname, dev->target,
2043 msgaddr[3] * 4, msgaddr[4]);
2045 } else {
2048 * We don't support whatever this message is...
2050 #ifdef DEBUG
2051 if (sbic_debug || sync_debug)
2052 printf("sbicmsgin: Rejecting message 0x%02x\n",
2053 msgaddr[0]);
2054 #endif
2057 * prepare to reject the message, NACK
2059 SET_SBIC_cmd(regs, SBIC_CMD_SET_ATN);
2060 WAIT_CIP(regs);
2064 * Negate ACK to complete the transfer
2066 SET_SBIC_cmd(regs, SBIC_CMD_CLR_ACK);
2069 * Wait for the interrupt for the next byte, or phase change.
2070 * Only read the CSR if we have more data to transfer.
2071 * XXXSCW: We should really verify that we're still in
2072 * MSG IN phase before blindly going back around this loop,
2073 * but that would mean we read the CSR... <sigh>
2075 SBIC_WAIT(regs, SBIC_ASR_INT, 0);
2076 if (recvlen > 0)
2077 GET_SBIC_csr(regs, csr);
2079 } while (recvlen > 0);
2082 * Should still have one CSR to read
2084 return SBIC_STATE_RUNNING;
2089 * sbicnextstate()
2090 * return:
2091 * SBIC_STATE_DONE == done
2092 * SBIC_STATE_RUNNING == working
2093 * SBIC_STATE_DISCONNECT == disconnected
2094 * SBIC_STATE_ERROR == error
2097 sbicnextstate(struct sbic_softc *dev, u_char csr, u_char asr)
2099 sbic_regmap_p regs = dev->sc_sbicp;
2100 struct sbic_acb *acb = dev->sc_nexus;
2102 QPRINTF(("next[%02x,%02x]: ",asr,csr));
2104 switch (csr) {
2106 case SBIC_CSR_XFERRED | CMD_PHASE:
2107 case SBIC_CSR_MIS | CMD_PHASE:
2108 case SBIC_CSR_MIS_1 | CMD_PHASE:
2109 case SBIC_CSR_MIS_2 | CMD_PHASE:
2110 if ( sbicxfout(regs, acb->clen, &acb->cmd) )
2111 goto abort;
2112 break;
2114 case SBIC_CSR_XFERRED | STATUS_PHASE:
2115 case SBIC_CSR_MIS | STATUS_PHASE:
2116 case SBIC_CSR_MIS_1 | STATUS_PHASE:
2117 case SBIC_CSR_MIS_2 | STATUS_PHASE:
2118 SET_SBIC_control(regs, SBIC_CTL_EDI | SBIC_CTL_IDI);
2121 * this should be the normal i/o completion case.
2122 * get the status & cmd complete msg then let the
2123 * device driver look at what happened.
2125 sbicxfdone(dev);
2127 #ifdef DEBUG
2128 dev->sc_dmatimo = 0;
2129 if (data_pointer_debug > 1)
2130 printf("next dmastop: %d(%p:%lx)\n",
2131 dev->target,
2132 dev->sc_cur->dc_addr,
2133 dev->sc_tcnt);
2134 #endif
2136 * Stop the DMA chip
2138 dev->sc_dmastop(dev);
2140 dev->sc_flags &= ~(SBICF_INDMA | SBICF_DCFLUSH);
2143 * Indicate to the upper layers that the command is done
2145 sbic_scsidone(acb, dev->sc_stat[0]);
2147 return SBIC_STATE_DONE;
2149 case SBIC_CSR_XFERRED | DATA_OUT_PHASE:
2150 case SBIC_CSR_XFERRED | DATA_IN_PHASE:
2151 case SBIC_CSR_MIS | DATA_OUT_PHASE:
2152 case SBIC_CSR_MIS | DATA_IN_PHASE:
2153 case SBIC_CSR_MIS_1 | DATA_OUT_PHASE:
2154 case SBIC_CSR_MIS_1 | DATA_IN_PHASE:
2155 case SBIC_CSR_MIS_2 | DATA_OUT_PHASE:
2156 case SBIC_CSR_MIS_2 | DATA_IN_PHASE:
2158 * Verify that we expected to transfer data...
2160 if (acb->sc_kv.dc_count <= 0) {
2161 printf("next: DATA phase with xfer count == %d, "
2162 "asr:0x%02x csr:0x%02x\n",
2163 acb->sc_kv.dc_count, asr, csr);
2164 goto abort;
2168 * Should we transfer using PIO or DMA ?
2170 if (dev->sc_xs->xs_control & XS_CTL_POLL ||
2171 dev->sc_flags & SBICF_ICMD ||
2172 acb->sc_dmacmd == 0 ) {
2175 * Do PIO transfer
2177 int i;
2179 #ifdef DEBUG
2180 if (data_pointer_debug > 1)
2181 printf("next PIO: %d(%p:%x)\n", dev->target,
2182 acb->sc_kv.dc_addr,
2183 acb->sc_kv.dc_count);
2184 #endif
2186 if (SBIC_PHASE(csr) == DATA_IN_PHASE)
2188 * data in
2190 i = sbicxfin(regs, acb->sc_kv.dc_count,
2191 acb->sc_kv.dc_addr);
2192 else
2194 * data out
2196 i = sbicxfout(regs, acb->sc_kv.dc_count,
2197 acb->sc_kv.dc_addr);
2199 acb->sc_kv.dc_addr += (acb->sc_kv.dc_count - i);
2200 acb->sc_kv.dc_count = i;
2203 * Update current count...
2205 acb->sc_tcnt = dev->sc_tcnt = i;
2207 dev->sc_flags &= ~SBICF_INDMA;
2209 } else {
2212 * Do DMA transfer
2213 * set next DMA addr and dec count
2215 sbic_save_ptrs(dev);
2216 sbic_load_ptrs(dev);
2218 SET_SBIC_control(regs, SBIC_CTL_EDI | SBIC_CTL_IDI |
2219 SBIC_MACHINE_DMA_MODE);
2221 #ifdef DEBUG
2222 dev->sc_dmatimo = 1;
2223 if (data_pointer_debug > 1)
2224 printf("next DMA: %d(%p:%lx)\n", dev->target,
2225 dev->sc_cur->dc_addr,
2226 dev->sc_tcnt);
2227 #endif
2229 * Start the DMA chip going
2231 dev->sc_tcnt = dev->sc_dmanext(dev);
2234 * Tell the WD chip how much to transfer
2235 * this time around
2237 SBIC_TC_PUT(regs, (unsigned)dev->sc_tcnt);
2240 * Start the transfer
2242 SET_SBIC_cmd(regs, SBIC_CMD_XFER_INFO);
2245 * Indicate that we're in DMA mode
2247 dev->sc_flags |= SBICF_INDMA;
2249 break;
2251 case SBIC_CSR_XFERRED | MESG_IN_PHASE:
2252 case SBIC_CSR_MIS | MESG_IN_PHASE:
2253 case SBIC_CSR_MIS_1 | MESG_IN_PHASE:
2254 case SBIC_CSR_MIS_2 | MESG_IN_PHASE:
2255 sbic_save_ptrs(dev);
2258 * Handle a single message in...
2260 return sbicmsgin(dev);
2262 case SBIC_CSR_MSGIN_W_ACK:
2264 * We should never see this since it's handled in 'sbicmsgin()'
2265 * but just for the sake of paranoia...
2267 /* Dunno what I'm ACKing */
2268 SET_SBIC_cmd(regs, SBIC_CMD_CLR_ACK);
2269 printf("Acking unknown msgin CSR:%02x",csr);
2270 break;
2272 case SBIC_CSR_XFERRED | MESG_OUT_PHASE:
2273 case SBIC_CSR_MIS | MESG_OUT_PHASE:
2274 case SBIC_CSR_MIS_1 | MESG_OUT_PHASE:
2275 case SBIC_CSR_MIS_2 | MESG_OUT_PHASE:
2277 * We only ever handle a message out phase here for sending a
2278 * REJECT message.
2280 sbic_save_ptrs(dev);
2282 #ifdef DEBUG
2283 if (sync_debug)
2284 printf("sending REJECT msg to last msg.\n");
2285 #endif
2287 SEND_BYTE(regs, MSG_REJECT);
2288 WAIT_CIP(regs);
2289 break;
2291 case SBIC_CSR_DISC:
2292 case SBIC_CSR_DISC_1:
2294 * Try to schedule another target
2296 sbic_save_ptrs(dev);
2298 dev->sc_flags &= ~SBICF_SELECTED;
2300 #ifdef DEBUG
2301 if (reselect_debug > 1)
2302 printf("sbicnext target %d disconnected\n",
2303 dev->target);
2304 #endif
2306 TAILQ_INSERT_HEAD(&dev->nexus_list, acb, chain);
2308 ++dev->sc_tinfo[dev->target].dconns;
2310 dev->sc_nexus = NULL;
2311 dev->sc_xs = NULL;
2313 if (acb->xs->xs_control & XS_CTL_POLL ||
2314 dev->sc_flags & SBICF_ICMD ||
2315 !sbic_parallel_operations )
2316 return SBIC_STATE_DISCONNECT;
2318 QPRINTF(("sbicnext: calling sbic_sched\n"));
2320 sbic_sched(dev);
2322 QPRINTF(("sbicnext: sbic_sched returned\n"));
2324 return SBIC_STATE_DISCONNECT;
2326 case SBIC_CSR_RSLT_NI:
2327 case SBIC_CSR_RSLT_IFY:
2330 * A reselection.
2331 * Note that since we don't enable Advanced Features (assuming
2332 * the WD chip is at least the 'A' revision), we're only ever
2333 * likely to see the 'SBIC_CSR_RSLT_NI' status. But for the
2334 * hell of it, we'll handle it anyway, for all the extra code
2335 * it needs...
2337 u_char newtarget, newlun;
2339 GET_SBIC_rselid(regs, newtarget);
2342 * check SBIC_RID_SIV?
2344 newtarget &= SBIC_RID_MASK;
2346 if (csr == SBIC_CSR_RSLT_IFY) {
2349 * Read Identify msg to avoid lockup
2351 GET_SBIC_data(regs, newlun);
2352 WAIT_CIP(regs);
2353 newlun &= SBIC_TLUN_MASK;
2355 } else {
2358 * Need to read Identify message the hard way, assuming
2359 * the target even sends us one...
2361 for (newlun = 255; newlun; --newlun) {
2362 GET_SBIC_asr(regs, asr);
2363 if (asr & SBIC_ASR_INT)
2364 break;
2365 delay(10);
2369 * If we didn't get an interrupt, somethink's up
2371 if ((asr & SBIC_ASR_INT) == 0) {
2372 printf("%s: Reselect without identify?"
2373 " asr %x\n", dev->sc_dev.dv_xname, asr);
2374 newlun = 0; /* XXXX */
2375 } else {
2377 * We got an interrupt, verify that
2378 * it's a change to message in phase,
2379 * and if so read the message.
2381 GET_SBIC_csr(regs,csr);
2383 if (csr == (SBIC_CSR_MIS | MESG_IN_PHASE) ||
2384 csr == (SBIC_CSR_MIS_1 | MESG_IN_PHASE) ||
2385 csr == (SBIC_CSR_MIS_2 | MESG_IN_PHASE)) {
2387 * Yup, gone to message in.
2388 * Fetch the target LUN
2390 sbicmsgin(dev);
2391 newlun = dev->sc_msg[0] & 0x07;
2393 } else {
2395 * Whoops! Target didn't go to
2396 * message in phase!!
2398 printf("RSLT_NI - "
2399 "not MESG_IN_PHASE %x\n", csr);
2400 newlun = 0; /* XXXSCW */
2406 * Ok, we have the identity of the reselecting target.
2408 #ifdef DEBUG
2409 if (reselect_debug > 1 ||
2410 (reselect_debug && csr == SBIC_CSR_RSLT_NI)) {
2411 printf("sbicnext: reselect %s from targ %d lun %d\n",
2412 csr == SBIC_CSR_RSLT_NI ? "NI" : "IFY",
2413 newtarget, newlun);
2415 #endif
2417 if (dev->sc_nexus) {
2419 * Whoops! We've been reselected with
2420 * an command in progress!
2421 * The best we can do is to put the current command
2422 * back on the ready list and hope for the best.
2424 #ifdef DEBUG
2425 if (reselect_debug > 1) {
2426 printf("%s: reselect %s with active command\n",
2427 dev->sc_dev.dv_xname,
2428 csr == SBIC_CSR_RSLT_NI ? "NI" : "IFY");
2430 #endif
2432 TAILQ_INSERT_HEAD(&dev->ready_list, dev->sc_nexus,
2433 chain);
2435 dev->sc_tinfo[dev->target].lubusy &= ~(1 << dev->lun);
2437 dev->sc_nexus = NULL;
2438 dev->sc_xs = NULL;
2442 * Reload sync values for this target
2444 if (dev->sc_sync[newtarget].state == SYNC_DONE)
2445 SET_SBIC_syn(regs,
2446 SBIC_SYN(dev->sc_sync[newtarget].offset,
2447 dev->sc_sync[newtarget].period));
2448 else
2449 SET_SBIC_syn(regs, SBIC_SYN (0, sbic_min_period));
2452 * Loop through the nexus list until we find the saved entry
2453 * for the reselecting target...
2455 for (acb = dev->nexus_list.tqh_first; acb;
2456 acb = acb->chain.tqe_next) {
2458 if ( acb->xs->xs_periph->periph_target == newtarget &&
2459 acb->xs->xs_periph->periph_lun == newlun) {
2461 * We've found the saved entry. Dequeue it, and
2462 * make it current again.
2464 TAILQ_REMOVE(&dev->nexus_list, acb, chain);
2466 dev->sc_nexus = acb;
2467 dev->sc_xs = acb->xs;
2468 dev->sc_flags |= SBICF_SELECTED;
2469 dev->target = newtarget;
2470 dev->lun = newlun;
2471 break;
2475 if (acb == NULL) {
2476 printf("%s: reselect %s targ %d not in nexus_list %p\n",
2477 dev->sc_dev.dv_xname,
2478 csr == SBIC_CSR_RSLT_NI ? "NI" : "IFY", newtarget,
2479 &dev->nexus_list.tqh_first);
2480 panic("bad reselect in sbic");
2483 if (csr == SBIC_CSR_RSLT_IFY)
2484 SET_SBIC_cmd(regs, SBIC_CMD_CLR_ACK);
2485 break;
2487 default:
2488 abort:
2490 * Something unexpected happened -- deal with it.
2492 printf("next: aborting asr 0x%02x csr 0x%02x\n", asr, csr);
2494 #ifdef DDB
2495 Debugger();
2496 #endif
2498 #ifdef DEBUG
2499 dev->sc_dmatimo = 0;
2500 if (data_pointer_debug > 1)
2501 printf("next dmastop: %d(%p:%lx)\n", dev->target,
2502 dev->sc_cur->dc_addr,
2503 dev->sc_tcnt);
2504 #endif
2506 dev->sc_dmastop(dev);
2507 SET_SBIC_control(regs, SBIC_CTL_EDI | SBIC_CTL_IDI);
2508 if (dev->sc_xs)
2509 sbicerror(dev, csr);
2510 sbicabort(dev, "next");
2512 if (dev->sc_flags & SBICF_INDMA) {
2513 dev->sc_flags &= ~(SBICF_INDMA | SBICF_DCFLUSH);
2515 #ifdef DEBUG
2516 dev->sc_dmatimo = 0;
2517 if (data_pointer_debug > 1)
2518 printf("next dmastop: %d(%p:%lx)\n",
2519 dev->target,
2520 dev->sc_cur->dc_addr,
2521 dev->sc_tcnt);
2522 #endif
2523 sbic_scsidone(acb, -1);
2526 return SBIC_STATE_ERROR;
2529 return SBIC_STATE_RUNNING;
2534 * Check if DMA can not be used with specified buffer
2537 sbiccheckdmap(void *bp, u_long len, u_long mask)
2539 u_char *buffer;
2540 u_long phy_buf;
2541 u_long phy_len;
2543 buffer = bp;
2545 if (len == 0)
2546 return 1;
2548 while (len) {
2550 phy_buf = kvtop((void *)buffer);
2551 phy_len = PAGE_SIZE - ((int)buffer & PGOFSET);
2553 if (len < phy_len)
2554 phy_len = len;
2556 if (phy_buf & mask)
2557 return 1;
2559 buffer += phy_len;
2560 len -= phy_len;
2563 return 0;
2567 sbictoscsiperiod(struct sbic_softc *dev, int a)
2569 unsigned int fs;
2572 * cycle = DIV / (2 * CLK)
2573 * DIV = FS + 2
2574 * best we can do is 200ns at 20 MHz, 2 cycles
2577 GET_SBIC_myid(dev->sc_sbicp, fs);
2579 fs = (fs >> 6) + 2; /* DIV */
2581 fs = (fs * 10000) / (dev->sc_clkfreq << 1); /* Cycle, in ns */
2583 if (a < 2)
2584 a = 8; /* map to Cycles */
2586 return (fs * a) >> 2; /* in 4 ns units */
2590 sbicfromscsiperiod(struct sbic_softc *dev, int p)
2592 unsigned int fs, ret;
2595 * Just the inverse of the above
2597 GET_SBIC_myid(dev->sc_sbicp, fs);
2599 fs = (fs >> 6) + 2; /* DIV */
2601 fs = (fs * 10000) / (dev->sc_clkfreq << 1); /* Cycle, in ns */
2603 ret = p << 2; /* in ns units */
2604 ret = ret / fs; /* in Cycles */
2606 if (ret < sbic_min_period)
2607 return(sbic_min_period);
2610 * verify rounding
2612 if (sbictoscsiperiod(dev, ret) < p)
2613 ret++;
2615 return (ret >= 8) ? 0 : ret;
2618 #ifdef DEBUG
2619 void
2620 sbictimeout(struct sbic_softc *dev)
2622 int s, asr;
2624 s = splbio();
2626 if (dev->sc_dmatimo) {
2628 if (dev->sc_dmatimo > 1) {
2630 printf("%s: DMA timeout #%d\n", dev->sc_dev.dv_xname,
2631 dev->sc_dmatimo - 1);
2633 GET_SBIC_asr(dev->sc_sbicp, asr);
2635 if (asr & SBIC_ASR_INT) {
2637 * We need to service a missed IRQ
2639 sbicintr(dev);
2640 } else {
2641 (void)sbicabort(dev, "timeout");
2642 splx(s);
2643 return;
2647 dev->sc_dmatimo++;
2650 splx(s);
2652 callout_reset(&dev->sc_timo_ch, 30 * hz, (void *)sbictimeout, dev);
2654 #endif