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