Home | History | Annotate | Line # | Download | only in dev
siop.c revision 1.10
      1   1.1      mw /*
      2   1.8  chopps  * Copyright (c) 1994 Michael L. Hitch
      3   1.1      mw  * Copyright (c) 1990 The Regents of the University of California.
      4   1.1      mw  * All rights reserved.
      5   1.1      mw  *
      6   1.1      mw  * This code is derived from software contributed to Berkeley by
      7   1.1      mw  * Van Jacobson of Lawrence Berkeley Laboratory.
      8   1.1      mw  *
      9   1.1      mw  * Redistribution and use in source and binary forms, with or without
     10   1.1      mw  * modification, are permitted provided that the following conditions
     11   1.1      mw  * are met:
     12   1.1      mw  * 1. Redistributions of source code must retain the above copyright
     13   1.1      mw  *    notice, this list of conditions and the following disclaimer.
     14   1.1      mw  * 2. Redistributions in binary form must reproduce the above copyright
     15   1.1      mw  *    notice, this list of conditions and the following disclaimer in the
     16   1.1      mw  *    documentation and/or other materials provided with the distribution.
     17   1.1      mw  * 3. All advertising materials mentioning features or use of this software
     18   1.1      mw  *    must display the following acknowledgement:
     19   1.1      mw  *	This product includes software developed by the University of
     20   1.1      mw  *	California, Berkeley and its contributors.
     21   1.1      mw  * 4. Neither the name of the University nor the names of its contributors
     22   1.1      mw  *    may be used to endorse or promote products derived from this software
     23   1.1      mw  *    without specific prior written permission.
     24   1.1      mw  *
     25   1.1      mw  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     26   1.1      mw  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     27   1.1      mw  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     28   1.1      mw  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     29   1.1      mw  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     30   1.1      mw  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     31   1.1      mw  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     32   1.1      mw  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     33   1.1      mw  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     34   1.1      mw  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     35   1.1      mw  * SUCH DAMAGE.
     36   1.1      mw  *
     37   1.1      mw  *	@(#)siop.c	7.5 (Berkeley) 5/4/91
     38  1.10  chopps  *	$Id: siop.c,v 1.10 1994/05/22 07:22:33 chopps Exp $
     39   1.1      mw  */
     40   1.1      mw 
     41   1.1      mw /*
     42   1.1      mw  * AMIGA 53C710 scsi adaptor driver
     43   1.1      mw  */
     44   1.2  chopps 
     45   1.2  chopps /* need to know if any tapes have been configured */
     46   1.2  chopps #include "st.h"
     47   1.2  chopps 
     48   1.4  chopps #include <sys/param.h>
     49   1.4  chopps #include <sys/systm.h>
     50   1.8  chopps #include <sys/device.h>
     51   1.4  chopps #include <sys/buf.h>
     52   1.8  chopps #include <scsi/scsi_all.h>
     53   1.8  chopps #include <scsi/scsiconf.h>
     54   1.8  chopps #include <machine/cpu.h>
     55   1.8  chopps #include <amiga/amiga/custom.h>
     56   1.8  chopps #include <amiga/dev/siopreg.h>
     57   1.4  chopps #include <amiga/dev/siopvar.h>
     58   1.1      mw 
     59   1.1      mw extern u_int	kvtop();
     60   1.1      mw 
     61   1.1      mw /*
     62   1.1      mw  * SCSI delays
     63   1.1      mw  * In u-seconds, primarily for state changes on the SPC.
     64   1.1      mw  */
     65   1.1      mw #define	SCSI_CMD_WAIT	50000	/* wait per step of 'immediate' cmds */
     66   1.1      mw #define	SCSI_DATA_WAIT	50000	/* wait per data in/out step */
     67   1.1      mw #define	SCSI_INIT_WAIT	50000	/* wait per step (both) during init */
     68   1.1      mw 
     69   1.8  chopps int  siopicmd __P((struct siop_softc *, int, void *, int, void *, int));
     70   1.8  chopps int  siopgo __P((struct siop_softc *, struct scsi_xfer *));
     71   1.8  chopps int  siopgetsense __P((struct siop_softc *, struct scsi_xfer *));
     72   1.8  chopps void siopabort __P((struct siop_softc *, siop_regmap_p, char *));
     73   1.8  chopps void sioperror __P((struct siop_softc *, siop_regmap_p, u_char));
     74   1.8  chopps void siopstart __P((struct siop_softc *));
     75   1.8  chopps void siopreset __P((struct siop_softc *));
     76   1.8  chopps void siopsetdelay __P((int));
     77   1.8  chopps void siop_scsidone __P((struct siop_softc *, int));
     78   1.8  chopps void siop_donextcmd __P((struct siop_softc *));
     79   1.8  chopps int  siopintr __P((struct siop_softc *));
     80   1.1      mw 
     81   1.1      mw /* 53C710 script */
     82   1.1      mw unsigned long scripts[] = {
     83   1.1      mw 	0x47000000, 0x00000298,	/* 000 -   0 */
     84   1.1      mw 	0x838b0000, 0x000000d0,	/* 008 -   8 */
     85   1.1      mw 	0x7a1b1000, 0x00000000,	/* 010 -  16 */
     86   1.1      mw 	0x828a0000, 0x00000088,	/* 018 -  24 */
     87   1.1      mw 	0x9e020000, 0x0000ff01,	/* 020 -  32 */
     88   1.1      mw 	0x72350000, 0x00000000,	/* 028 -  40 */
     89   1.1      mw 	0x808c0000, 0x00000048,	/* 030 -  48 */
     90   1.1      mw 	0x58000008, 0x00000000,	/* 038 -  56 */
     91   1.1      mw 	0x1e000024, 0x00000024,	/* 040 -  64 */
     92   1.1      mw 	0x838b0000, 0x00000090,	/* 048 -  72 */
     93   1.1      mw 	0x1f00002c, 0x0000002c,	/* 050 -  80 */
     94   1.1      mw 	0x838b0000, 0x00000080,	/* 058 -  88 */
     95   1.1      mw 	0x868a0000, 0xffffffd0,	/* 060 -  96 */
     96   1.1      mw 	0x838a0000, 0x00000070,	/* 068 - 104 */
     97   1.1      mw 	0x878a0000, 0x00000120,	/* 070 - 112 */
     98   1.1      mw 	0x80880000, 0x00000028,	/* 078 - 120 */
     99   1.1      mw 	0x1e000004, 0x00000004,	/* 080 - 128 */
    100   1.1      mw 	0x838b0000, 0x00000050,	/* 088 - 136 */
    101   1.1      mw 	0x868a0000, 0xffffffe8,	/* 090 - 144 */
    102   1.1      mw 	0x838a0000, 0x00000040,	/* 098 - 152 */
    103   1.1      mw 	0x878a0000, 0x000000f0,	/* 0a0 - 160 */
    104   1.1      mw 	0x9a020000, 0x0000ff02,	/* 0a8 - 168 */
    105   1.1      mw 	0x1a00000c, 0x0000000c,	/* 0b0 - 176 */
    106   1.1      mw 	0x878b0000, 0x00000130,	/* 0b8 - 184 */
    107   1.1      mw 	0x838a0000, 0x00000018,	/* 0c0 - 192 */
    108   1.1      mw 	0x818a0000, 0x000000b0,	/* 0c8 - 200 */
    109   1.1      mw 	0x808a0000, 0x00000080,	/* 0d0 - 208 */
    110   1.1      mw 	0x98080000, 0x0000ff03,	/* 0d8 - 216 */
    111   1.1      mw 	0x1b000014, 0x00000014,	/* 0e0 - 224 */
    112   1.1      mw 	0x72090000, 0x00000000,	/* 0e8 - 232 */
    113   1.1      mw 	0x6a340000, 0x00000000,	/* 0f0 - 240 */
    114   1.1      mw 	0x9f030000, 0x0000ff04,	/* 0f8 - 248 */
    115   1.1      mw 	0x1f00001c, 0x0000001c,	/* 100 - 256 */
    116   1.1      mw 	0x98040000, 0x0000ff26,	/* 108 - 264 */
    117   1.1      mw 	0x60000040, 0x00000000,	/* 110 - 272 */
    118   1.1      mw 	0x48000000, 0x00000000,	/* 118 - 280 */
    119   1.1      mw 	0x7c1bef00, 0x00000000,	/* 120 - 288 */
    120   1.1      mw 	0x72340000, 0x00000000,	/* 128 - 296 */
    121   1.1      mw 	0x980c0002, 0x0000fffc,	/* 130 - 304 */
    122   1.1      mw 	0x980c0008, 0x0000fffb,	/* 138 - 312 */
    123   1.1      mw 	0x980c0018, 0x0000fffd,	/* 140 - 320 */
    124   1.1      mw 	0x98040000, 0x0000fffe,	/* 148 - 328 */
    125   1.1      mw 	0x98080000, 0x0000ff00,	/* 150 - 336 */
    126   1.1      mw 	0x18000034, 0x00000034,	/* 158 - 344 */
    127   1.1      mw 	0x808b0000, 0x000001c0,	/* 160 - 352 */
    128   1.1      mw 	0x838b0000, 0xffffff70,	/* 168 - 360 */
    129   1.1      mw 	0x878a0000, 0x000000d0,	/* 170 - 368 */
    130   1.1      mw 	0x98080000, 0x0000ff05,	/* 178 - 376 */
    131   1.1      mw 	0x19000034, 0x00000034,	/* 180 - 384 */
    132   1.1      mw 	0x818b0000, 0x00000160,	/* 188 - 392 */
    133   1.1      mw 	0x80880000, 0xffffffd0,	/* 190 - 400 */
    134   1.1      mw 	0x1f00001c, 0x0000001c,	/* 198 - 408 */
    135   1.1      mw 	0x808c0001, 0x00000018,	/* 1a0 - 416 */
    136   1.1      mw 	0x980c0002, 0x0000ff08,	/* 1a8 - 424 */
    137   1.1      mw 	0x808c0004, 0x00000020,	/* 1b0 - 432 */
    138   1.1      mw 	0x98080000, 0x0000ff06,	/* 1b8 - 440 */
    139   1.1      mw 	0x60000040, 0x00000000,	/* 1c0 - 448 */
    140   1.1      mw 	0x1f00002c, 0x0000002c,	/* 1c8 - 456 */
    141   1.1      mw 	0x98080000, 0x0000ff07,	/* 1d0 - 464 */
    142   1.1      mw 	0x60000040, 0x00000000,	/* 1d8 - 472 */
    143   1.1      mw 	0x48000000, 0x00000000,	/* 1e0 - 480 */
    144   1.1      mw 	0x98080000, 0x0000ff09,	/* 1e8 - 488 */
    145   1.1      mw 	0x1f00001c, 0x0000001c,	/* 1f0 - 496 */
    146   1.1      mw 	0x808c0001, 0x00000018,	/* 1f8 - 504 */
    147   1.1      mw 	0x980c0002, 0x0000ff10,	/* 200 - 512 */
    148   1.1      mw 	0x808c0004, 0x00000020,	/* 208 - 520 */
    149   1.1      mw 	0x98080000, 0x0000ff11,	/* 210 - 528 */
    150   1.1      mw 	0x60000040, 0x00000000,	/* 218 - 536 */
    151   1.1      mw 	0x1f00002c, 0x0000002c,	/* 220 - 544 */
    152   1.1      mw 	0x98080000, 0x0000ff12,	/* 228 - 552 */
    153   1.1      mw 	0x60000040, 0x00000000,	/* 230 - 560 */
    154   1.1      mw 	0x48000000, 0x00000000,	/* 238 - 568 */
    155   1.1      mw 	0x98080000, 0x0000ff13,	/* 240 - 576 */
    156   1.1      mw 	0x1f00001c, 0x0000001c,	/* 248 - 584 */
    157   1.1      mw 	0x808c0001, 0x00000018,	/* 250 - 592 */
    158   1.1      mw 	0x980c0002, 0x0000ff14,	/* 258 - 600 */
    159   1.1      mw 	0x808c0004, 0x00000020,	/* 260 - 608 */
    160   1.1      mw 	0x98080000, 0x0000ff15,	/* 268 - 616 */
    161   1.1      mw 	0x60000040, 0x00000000,	/* 270 - 624 */
    162   1.1      mw 	0x1f00002c, 0x0000002c,	/* 278 - 632 */
    163   1.1      mw 	0x98080000, 0x0000ff16,	/* 280 - 640 */
    164   1.1      mw 	0x60000040, 0x00000000,	/* 288 - 648 */
    165   1.1      mw 	0x48000000, 0x00000000,	/* 290 - 656 */
    166   1.1      mw 	0x98080000, 0x0000ff17,	/* 298 - 664 */
    167   1.1      mw 	0x54000000, 0x00000040,	/* 2a0 - 672 */
    168   1.1      mw 	0x9f030000, 0x0000ff18,	/* 2a8 - 680 */
    169   1.1      mw 	0x1f00001c, 0x0000001c,	/* 2b0 - 688 */
    170   1.1      mw 	0x990b0000, 0x0000ff19,	/* 2b8 - 696 */
    171   1.1      mw 	0x980a0000, 0x0000ff20,	/* 2c0 - 704 */
    172   1.1      mw 	0x9f0a0000, 0x0000ff21,	/* 2c8 - 712 */
    173   1.1      mw 	0x9b0a0000, 0x0000ff22,	/* 2d0 - 720 */
    174   1.1      mw 	0x9e0a0000, 0x0000ff23,	/* 2d8 - 728 */
    175   1.1      mw 	0x98080000, 0x0000ff24,	/* 2e0 - 736 */
    176   1.1      mw 	0x98080000, 0x0000ff25,	/* 2e8 - 744 */
    177   1.1      mw 	0x76100800, 0x00000000,	/* 2f0 - 752 */
    178   1.1      mw 	0x80840700, 0x00000008,	/* 2f8 - 760 */
    179   1.1      mw 	0x7e110100, 0x00000000,	/* 300 - 768 */
    180   1.1      mw 	0x6a100000, 0x00000000,	/* 308 - 776 */
    181   1.1      mw 	0x19000034, 0x00000034,	/* 310 - 784 */
    182   1.1      mw 	0x818b0000, 0xffffffd0,	/* 318 - 792 */
    183   1.1      mw 	0x98080000, 0x0000ff27,	/* 320 - 800 */
    184   1.1      mw 	0x76100800, 0x00000000,	/* 328 - 808 */
    185   1.1      mw 	0x80840700, 0x00000008,	/* 330 - 816 */
    186   1.1      mw 	0x7e110100, 0x00000000,	/* 338 - 824 */
    187   1.1      mw 	0x6a100000, 0x00000000,	/* 340 - 832 */
    188   1.1      mw 	0x18000034, 0x00000034,	/* 348 - 840 */
    189   1.1      mw 	0x808b0000, 0xffffffd0,	/* 350 - 848 */
    190   1.1      mw 	0x98080000, 0x0000ff27	/* 358 - 856 */
    191   1.1      mw };
    192   1.1      mw 
    193   1.1      mw #define	Ent_msgout	0x00000018
    194   1.1      mw #define	Ent_cmd		0x000000a8
    195   1.1      mw #define	Ent_status	0x000000e0
    196   1.1      mw #define	Ent_msgin	0x000000f8
    197   1.1      mw #define	Ent_dataout	0x00000158
    198   1.1      mw #define	Ent_datain	0x00000180
    199   1.1      mw 
    200   1.8  chopps /* default to not inhibit sync negotiation on any drive */
    201   1.8  chopps /* XXXX - unit 2 inhibits sync for my WangTek tape drive - mlh */
    202   1.8  chopps u_char siop_inhibit_sync[8] = { 0, 0, 1, 0, 0, 0, 0 }; /* initialize, so patchable */
    203   1.8  chopps int siop_no_dma = 0;
    204   1.8  chopps 
    205   1.8  chopps int siop_reset_delay = 2000;	/* delay after reset, in milleseconds */
    206   1.8  chopps int siop_sync_period = 50;	/* synchronous transfer period, in nanoseconds */
    207   1.1      mw 
    208   1.1      mw int siop_cmd_wait = SCSI_CMD_WAIT;
    209   1.1      mw int siop_data_wait = SCSI_DATA_WAIT;
    210   1.1      mw int siop_init_wait = SCSI_INIT_WAIT;
    211   1.1      mw 
    212   1.1      mw static struct {
    213   1.1      mw 	unsigned char x;	/* period from sync request message */
    214   1.1      mw 	unsigned char y;	/* siop_period << 4 | sbcl */
    215   1.1      mw } xxx[] = {
    216   1.1      mw 	{0x0f, 0x01},
    217   1.1      mw 	{0x13, 0x11},
    218   1.1      mw 	{0x17, 0x21},
    219   1.1      mw /*	{0x17, 0x02},	*/
    220   1.1      mw 	{0x1b, 0x31},
    221   1.1      mw 	{0x1d, 0x12},
    222   1.1      mw 	{0x1e, 0x41},
    223   1.1      mw /*	{0x1e, 0x03},	*/
    224   1.1      mw 	{0x22, 0x51},
    225   1.1      mw 	{0x23, 0x22},
    226   1.1      mw 	{0x26, 0x61},
    227   1.1      mw /*	{0x26, 0x13},	*/
    228   1.1      mw 	{0x29, 0x32},
    229   1.1      mw 	{0x2a, 0x71},
    230   1.1      mw 	{0x2d, 0x23},
    231   1.1      mw 	{0x2e, 0x42},
    232   1.1      mw 	{0x34, 0x52},
    233   1.1      mw 	{0x35, 0x33},
    234   1.1      mw 	{0x3a, 0x62},
    235   1.1      mw 	{0x3c, 0x43},
    236   1.1      mw 	{0x40, 0x72},
    237   1.1      mw 	{0x44, 0x53},
    238   1.1      mw 	{0x4b, 0x63},
    239   1.1      mw 	{0x53, 0x73}
    240   1.1      mw };
    241   1.1      mw 
    242   1.1      mw #ifdef DEBUG
    243   1.8  chopps #define QPRINTF(a) if (siop_debug > 1) printf a
    244   1.8  chopps /*
    245   1.8  chopps  *	0x01 - full debug
    246   1.8  chopps  *	0x02 - DMA chaining
    247   1.8  chopps  *	0x04 - siopintr
    248   1.8  chopps  *	0x08 - phase mismatch
    249   1.8  chopps  *	0x10 - panic on phase mismatch
    250   1.8  chopps  */
    251   1.8  chopps int	siop_debug = 0;
    252   1.8  chopps int	siopsync_debug = 0;
    253   1.8  chopps int	siopdma_hits = 0;
    254   1.8  chopps int	siopdma_misses = 0;
    255   1.1      mw #endif
    256   1.8  chopps 
    257   1.8  chopps 
    258   1.8  chopps /*
    259   1.8  chopps  * default minphys routine for siop based controllers
    260   1.8  chopps  */
    261   1.8  chopps void
    262   1.8  chopps siop_minphys(bp)
    263   1.8  chopps 	struct buf *bp;
    264   1.8  chopps {
    265   1.8  chopps 	/*
    266   1.8  chopps 	 * no max transfer at this level
    267   1.8  chopps 	 */
    268   1.1      mw }
    269   1.1      mw 
    270   1.8  chopps /*
    271   1.8  chopps  * must be used
    272   1.8  chopps  */
    273   1.8  chopps u_int
    274   1.8  chopps siop_adinfo()
    275   1.8  chopps {
    276   1.8  chopps 	/*
    277   1.8  chopps 	 * one request at a time please
    278   1.8  chopps 	 */
    279   1.8  chopps 	return(1);
    280   1.8  chopps }
    281   1.1      mw 
    282   1.1      mw /*
    283   1.8  chopps  * used by specific siop controller
    284   1.1      mw  *
    285   1.8  chopps  * it appears that the higher level code does nothing with LUN's
    286   1.8  chopps  * so I will too.  I could plug it in, however so could they
    287   1.8  chopps  * in scsi_scsi_cmd().
    288   1.8  chopps  */
    289   1.8  chopps int
    290   1.8  chopps siop_scsicmd(xs)
    291   1.8  chopps 	struct scsi_xfer *xs;
    292   1.8  chopps {
    293   1.8  chopps 	struct siop_pending *pendp;
    294   1.8  chopps 	struct siop_softc *dev;
    295   1.8  chopps 	struct scsi_link *slp;
    296   1.8  chopps 	int flags, s;
    297   1.8  chopps 
    298   1.8  chopps 	slp = xs->sc_link;
    299   1.8  chopps 	dev = slp->adapter_softc;
    300   1.8  chopps 	flags = xs->flags;
    301   1.8  chopps 
    302   1.8  chopps 	if (flags & SCSI_DATA_UIO)
    303   1.8  chopps 		panic("siop: scsi data uio requested");
    304   1.8  chopps 
    305   1.8  chopps 	if (dev->sc_xs && flags & SCSI_NOMASK)
    306   1.8  chopps 		panic("siop_scsicmd: busy");
    307   1.8  chopps 
    308   1.8  chopps 	s = splbio();
    309   1.8  chopps 	pendp = &dev->sc_xsstore[slp->target][slp->lun];
    310   1.8  chopps 	if (pendp->xs) {
    311   1.8  chopps 		splx(s);
    312   1.8  chopps 		return(TRY_AGAIN_LATER);
    313   1.8  chopps 	}
    314   1.8  chopps 
    315   1.8  chopps 	if (dev->sc_xs) {
    316   1.8  chopps 		pendp->xs = xs;
    317   1.8  chopps 		TAILQ_INSERT_TAIL(&dev->sc_xslist, pendp, link);
    318   1.8  chopps 		splx(s);
    319   1.8  chopps 		return(SUCCESSFULLY_QUEUED);
    320   1.8  chopps 	}
    321   1.8  chopps 	pendp->xs = NULL;
    322   1.8  chopps 	dev->sc_xs = xs;
    323   1.8  chopps 	splx(s);
    324   1.8  chopps 
    325   1.8  chopps 	/*
    326   1.8  chopps 	 * nothing is pending do it now.
    327   1.8  chopps 	 */
    328   1.8  chopps 	siop_donextcmd(dev);
    329   1.8  chopps 
    330   1.8  chopps 	if (flags & SCSI_NOMASK)
    331   1.8  chopps 		return(COMPLETE);
    332   1.8  chopps 	return(SUCCESSFULLY_QUEUED);
    333   1.8  chopps }
    334   1.8  chopps 
    335   1.8  chopps /*
    336   1.8  chopps  * entered with dev->sc_xs pointing to the next xfer to perform
    337   1.1      mw  */
    338   1.1      mw void
    339   1.8  chopps siop_donextcmd(dev)
    340   1.8  chopps 	struct siop_softc *dev;
    341   1.8  chopps {
    342   1.8  chopps 	struct scsi_xfer *xs;
    343   1.8  chopps 	struct scsi_link *slp;
    344   1.8  chopps 	int flags, phase, stat;
    345   1.8  chopps 
    346   1.8  chopps 	xs = dev->sc_xs;
    347   1.8  chopps 	slp = xs->sc_link;
    348   1.8  chopps 	flags = xs->flags;
    349   1.8  chopps 
    350   1.8  chopps #if 0
    351   1.8  chopps 	if (flags & SCSI_DATA_IN)
    352   1.8  chopps 		phase = DATA_IN_PHASE;
    353   1.8  chopps 	else if (flags & SCSI_DATA_OUT)
    354   1.8  chopps 		phase = DATA_OUT_PHASE;
    355   1.8  chopps 	else
    356   1.8  chopps 		phase = STATUS_PHASE;
    357   1.8  chopps #endif
    358   1.8  chopps 
    359   1.8  chopps 	if (flags & SCSI_RESET)
    360   1.8  chopps 		siopreset(dev);
    361   1.8  chopps 
    362   1.8  chopps 	dev->sc_stat[0] = -1;
    363   1.8  chopps #if 0
    364   1.8  chopps 	if (phase == STATUS_PHASE || flags & SCSI_NOMASK)
    365   1.8  chopps #else
    366  1.10  chopps 	if (flags & SCSI_NOMASK || siop_no_dma)
    367   1.8  chopps #endif
    368   1.8  chopps 		stat = siopicmd(dev, slp->target, xs->cmd, xs->cmdlen,
    369   1.8  chopps 		    xs->data, xs->datalen/*, phase*/);
    370   1.8  chopps 	else if (siopgo(dev, xs) == 0)
    371   1.8  chopps 		return;
    372   1.8  chopps 	else
    373   1.8  chopps 		stat = dev->sc_stat[0];
    374   1.8  chopps 
    375   1.8  chopps 	siop_scsidone(dev, stat);
    376   1.8  chopps }
    377   1.8  chopps 
    378   1.8  chopps void
    379   1.8  chopps siop_scsidone(dev, stat)
    380   1.8  chopps 	struct siop_softc *dev;
    381   1.8  chopps 	int stat;
    382   1.1      mw {
    383   1.8  chopps 	struct siop_pending *pendp;
    384   1.8  chopps 	struct scsi_xfer *xs;
    385   1.8  chopps 	int s, donext;
    386   1.8  chopps 
    387   1.8  chopps 	xs = dev->sc_xs;
    388   1.8  chopps #ifdef DIAGNOSTIC
    389   1.8  chopps 	if (xs == NULL)
    390   1.8  chopps 		panic("siop_scsidone");
    391   1.8  chopps #endif
    392   1.8  chopps 	/*
    393   1.8  chopps 	 * is this right?
    394   1.8  chopps 	 */
    395   1.8  chopps 	xs->status = stat;
    396   1.8  chopps 
    397   1.8  chopps 	if (stat == 0 || xs->flags & SCSI_ERR_OK)
    398   1.8  chopps 		xs->resid = 0;
    399   1.8  chopps 	else {
    400   1.8  chopps 		switch(stat) {
    401   1.8  chopps 		case SCSI_CHECK:
    402   1.8  chopps 			if (stat = siopgetsense(dev, xs))
    403   1.8  chopps 				goto bad_sense;
    404   1.8  chopps 			xs->error = XS_SENSE;
    405   1.8  chopps 			break;
    406   1.8  chopps 		case SCSI_BUSY:
    407   1.8  chopps 			xs->error = XS_BUSY;
    408   1.8  chopps 			break;
    409   1.8  chopps 		bad_sense:
    410   1.8  chopps 		default:
    411   1.8  chopps 			xs->error = XS_DRIVER_STUFFUP;
    412   1.8  chopps 			QPRINTF(("siop_scsicmd() bad %x\n", stat));
    413   1.8  chopps 			break;
    414   1.8  chopps 		}
    415   1.8  chopps 	}
    416   1.8  chopps 	xs->flags |= ITSDONE;
    417   1.1      mw 
    418   1.8  chopps 	/*
    419   1.8  chopps 	 * grab next command before scsi_done()
    420   1.8  chopps 	 * this way no single device can hog scsi resources.
    421   1.8  chopps 	 */
    422   1.8  chopps 	s = splbio();
    423   1.8  chopps 	pendp = dev->sc_xslist.tqh_first;
    424   1.8  chopps 	if (pendp == NULL) {
    425   1.8  chopps 		donext = 0;
    426   1.8  chopps 		dev->sc_xs = NULL;
    427   1.1      mw 	} else {
    428   1.8  chopps 		donext = 1;
    429   1.8  chopps 		TAILQ_REMOVE(&dev->sc_xslist, pendp, link);
    430   1.8  chopps 		dev->sc_xs = pendp->xs;
    431   1.8  chopps 		pendp->xs = NULL;
    432   1.1      mw 	}
    433   1.8  chopps 	splx(s);
    434   1.8  chopps 	scsi_done(xs);
    435   1.8  chopps 
    436   1.8  chopps 	if (donext)
    437   1.8  chopps 		siop_donextcmd(dev);
    438   1.1      mw }
    439   1.1      mw 
    440   1.8  chopps int
    441   1.8  chopps siopgetsense(dev, xs)
    442   1.8  chopps 	struct siop_softc *dev;
    443   1.8  chopps 	struct scsi_xfer *xs;
    444   1.8  chopps {
    445   1.8  chopps 	struct scsi_sense rqs;
    446   1.8  chopps 	struct scsi_link *slp;
    447   1.8  chopps 	int stat;
    448   1.1      mw 
    449   1.8  chopps 	slp = xs->sc_link;
    450   1.8  chopps 
    451   1.8  chopps 	rqs.op_code = REQUEST_SENSE;
    452   1.8  chopps 	rqs.byte2 = slp->lun << 5;
    453   1.9  chopps #ifdef not_yet
    454   1.8  chopps 	rqs.length = xs->req_sense_length ? xs->req_sense_length :
    455   1.8  chopps 	    sizeof(xs->sense);
    456   1.9  chopps #else
    457   1.9  chopps 	rqs.length = sizeof(xs->sense);
    458   1.9  chopps #endif
    459   1.8  chopps 	if (rqs.length > sizeof (xs->sense))
    460   1.8  chopps 		rqs.length = sizeof (xs->sense);
    461   1.8  chopps 	rqs.unused[0] = rqs.unused[1] = rqs.control = 0;
    462   1.8  chopps 
    463   1.8  chopps 	return(siopicmd(dev, slp->target, &rqs, sizeof(rqs), &xs->sense,
    464   1.8  chopps 	    rqs.length));
    465   1.8  chopps }
    466   1.1      mw 
    467   1.8  chopps void
    468   1.1      mw siopabort(dev, regs, where)
    469   1.1      mw 	register struct siop_softc *dev;
    470   1.8  chopps 	siop_regmap_p regs;
    471   1.1      mw 	char *where;
    472   1.1      mw {
    473   1.1      mw 
    474   1.8  chopps 	printf ("%s: abort %s: dstat %02x, sstat0 %02x sbcl %02x\n",
    475   1.8  chopps 	    dev->sc_dev.dv_xname,
    476   1.1      mw 	    where, regs->siop_dstat, regs->siop_sstat0, regs->siop_sbcl);
    477   1.1      mw 
    478   1.1      mw 	if (dev->sc_flags & SIOP_SELECTED) {
    479   1.1      mw #ifdef TODO
    480   1.1      mw       SET_SBIC_cmd (regs, SBIC_CMD_ABORT);
    481   1.1      mw       WAIT_CIP (regs);
    482   1.1      mw 
    483   1.1      mw       GET_SBIC_asr (regs, asr);
    484   1.1      mw       if (asr & (SBIC_ASR_BSY|SBIC_ASR_LCI))
    485   1.1      mw         {
    486   1.1      mw           /* ok, get more drastic.. */
    487   1.1      mw 
    488   1.1      mw 	  SET_SBIC_cmd (regs, SBIC_CMD_RESET);
    489   1.1      mw 	  DELAY(25);
    490   1.1      mw 	  SBIC_WAIT(regs, SBIC_ASR_INT, 0);
    491   1.1      mw 	  GET_SBIC_csr (regs, csr);       /* clears interrupt also */
    492   1.1      mw 
    493   1.1      mw           dev->sc_flags &= ~SIOP_SELECTED;
    494   1.1      mw           return;
    495   1.1      mw         }
    496   1.1      mw 
    497   1.1      mw       do
    498   1.1      mw         {
    499   1.1      mw           SBIC_WAIT (regs, SBIC_ASR_INT, 0);
    500   1.1      mw           GET_SBIC_csr (regs, csr);
    501   1.1      mw         }
    502   1.1      mw       while ((csr != SBIC_CSR_DISC) && (csr != SBIC_CSR_DISC_1)
    503   1.1      mw 	      && (csr != SBIC_CSR_CMD_INVALID));
    504   1.1      mw #endif
    505   1.1      mw 
    506   1.1      mw 		/* lets just hope it worked.. */
    507   1.1      mw 		dev->sc_flags &= ~SIOP_SELECTED;
    508   1.1      mw 	}
    509   1.1      mw }
    510   1.1      mw 
    511   1.8  chopps /*
    512   1.8  chopps  * XXX Set/reset long delays.
    513   1.8  chopps  *
    514   1.8  chopps  * if delay == 0, reset default delays
    515   1.8  chopps  * if delay < 0,  set both delays to default long initialization values
    516   1.8  chopps  * if delay > 0,  set both delays to this value
    517   1.8  chopps  *
    518   1.8  chopps  * Used when a devices is expected to respond slowly (e.g. during
    519   1.8  chopps  * initialization).
    520   1.8  chopps  */
    521   1.8  chopps void
    522   1.8  chopps siop_delay(delay)
    523   1.8  chopps 	int delay;
    524   1.1      mw {
    525   1.8  chopps 	static int saved_cmd_wait, saved_data_wait;
    526   1.1      mw 
    527   1.8  chopps 	if (delay) {
    528   1.8  chopps 		saved_cmd_wait = siop_cmd_wait;
    529   1.8  chopps 		saved_data_wait = siop_data_wait;
    530   1.8  chopps 		if (delay > 0)
    531   1.8  chopps 			siop_cmd_wait = siop_data_wait = delay;
    532   1.8  chopps 		else
    533   1.8  chopps 			siop_cmd_wait = siop_data_wait = siop_init_wait;
    534   1.8  chopps 	} else {
    535   1.8  chopps 		siop_cmd_wait = saved_cmd_wait;
    536   1.8  chopps 		siop_data_wait = saved_data_wait;
    537   1.8  chopps 	}
    538   1.1      mw }
    539   1.1      mw 
    540   1.1      mw void
    541   1.8  chopps siopreset(dev)
    542   1.8  chopps 	struct siop_softc *dev;
    543   1.1      mw {
    544   1.8  chopps 	siop_regmap_p regs;
    545   1.1      mw 	u_int i, s;
    546   1.1      mw 	u_char  my_id, csr;
    547   1.1      mw 
    548   1.8  chopps 	regs = dev->sc_siopp;
    549   1.8  chopps 
    550   1.1      mw 	if (dev->sc_flags & SIOP_ALIVE)
    551   1.1      mw 		siopabort(dev, regs, "reset");
    552   1.1      mw 
    553   1.8  chopps 	printf("\n%s: ", dev->sc_dev.dv_xname);		/* XXXX */
    554   1.1      mw 
    555   1.1      mw 	s = splbio();
    556   1.1      mw 	my_id = 7;
    557   1.1      mw 
    558   1.1      mw 	/*
    559   1.1      mw 	 * Reset the chip
    560   1.1      mw 	 * XXX - is this really needed?
    561   1.1      mw 	 */
    562   1.1      mw 	regs->siop_sien &= ~SIOP_SIEN_RST;
    563   1.1      mw 	regs->siop_scntl1 |= SIOP_SCNTL1_RST;
    564   1.1      mw 	for (i = 0; i < 1000; ++i)
    565   1.1      mw 		;
    566   1.1      mw 	regs->siop_scntl1 &= ~SIOP_SCNTL1_RST;
    567   1.1      mw 	regs->siop_sien |= SIOP_SIEN_RST;
    568   1.1      mw 
    569   1.1      mw 	/*
    570   1.1      mw 	 * Set up various chip parameters
    571   1.1      mw 	 */
    572   1.1      mw 	regs->siop_istat = 0x40;
    573   1.1      mw 	for (i = 0; i < 1000; ++i)
    574   1.1      mw 		;
    575   1.1      mw 	regs->siop_istat = 0x00;
    576   1.1      mw 	regs->siop_scntl0 = SIOP_ARB_FULL | SIOP_SCNTL0_EPC | SIOP_SCNTL0_EPG;
    577   1.8  chopps 	regs->siop_dcntl = dev->sc_clock_freq & 0xff;
    578   1.8  chopps 	regs->siop_dmode = 0x80;	/* burst length = 4 */
    579   1.1      mw 	regs->siop_sien = 0x00;	/* don't enable interrupts yet */
    580   1.1      mw 	regs->siop_dien = 0x00;	/* don't enable interrupts yet */
    581   1.1      mw 	regs->siop_scid = 1 << my_id;
    582   1.1      mw 	regs->siop_dwt = 0x00;
    583   1.1      mw 	regs->siop_ctest0 |= 0x20;	/* Enable Active Negation ?? */
    584   1.8  chopps 	regs->siop_ctest7 |= (dev->sc_clock_freq >> 8) & 0xff;
    585   1.1      mw 
    586   1.1      mw 	/* will need to re-negotiate sync xfers */
    587   1.1      mw 	bzero(&dev->sc_sync, sizeof (dev->sc_sync));
    588   1.1      mw 
    589   1.1      mw 	splx (s);
    590   1.1      mw 
    591   1.8  chopps  	DELAY (siop_reset_delay * 10000);
    592   1.8  chopps 	printf("siop id %d reset\n", my_id);
    593   1.1      mw 	dev->sc_flags |= SIOP_ALIVE;
    594   1.1      mw 	dev->sc_flags &= ~(SIOP_SELECTED | SIOP_DMA);
    595   1.1      mw }
    596   1.1      mw 
    597   1.1      mw /*
    598   1.1      mw  * Setup Data Storage for 53C710 and start SCRIPTS processing
    599   1.1      mw  */
    600   1.1      mw 
    601   1.8  chopps void
    602   1.1      mw siop_setup (dev, target, cbuf, clen, buf, len)
    603   1.1      mw 	struct siop_softc *dev;
    604   1.1      mw 	int target;
    605   1.1      mw 	u_char *cbuf;
    606   1.1      mw 	int clen;
    607   1.1      mw 	u_char *buf;
    608   1.1      mw 	int len;
    609   1.1      mw {
    610   1.8  chopps 	siop_regmap_p regs = dev->sc_siopp;
    611   1.1      mw 	int i;
    612   1.1      mw 	int nchain;
    613   1.1      mw 	int count, tcount;
    614   1.1      mw 	char *addr, *dmaend;
    615   1.1      mw 
    616   1.1      mw 	dev->sc_istat = 0;
    617   1.1      mw 	dev->sc_lun = 0x80;			/* XXX */
    618   1.1      mw 	dev->sc_stat[0] = -1;
    619   1.1      mw 	dev->sc_msg[0] = -1;
    620   1.1      mw 	dev->sc_ds.scsi_addr = (0x10000 << target) | (dev->sc_sync[target].period << 8);
    621   1.1      mw 	dev->sc_ds.idlen = 1;
    622   1.1      mw 	dev->sc_ds.idbuf = (char *) kvtop(&dev->sc_lun);
    623   1.1      mw 	dev->sc_ds.cmdlen = clen;
    624   1.1      mw 	dev->sc_ds.cmdbuf = (char *) kvtop(cbuf);
    625   1.1      mw 	dev->sc_ds.stslen = 1;
    626   1.1      mw 	dev->sc_ds.stsbuf = (char *) kvtop(&dev->sc_stat[0]);
    627   1.1      mw 	dev->sc_ds.msglen = 1;
    628   1.1      mw 	dev->sc_ds.msgbuf = (char *) kvtop(&dev->sc_msg[0]);
    629   1.1      mw 	dev->sc_ds.sdtrolen = 0;
    630   1.1      mw 	dev->sc_ds.sdtrilen = 0;
    631   1.1      mw 	dev->sc_ds.chain[0].datalen = len;
    632   1.1      mw 	dev->sc_ds.chain[0].databuf = (char *) kvtop(buf);
    633   1.1      mw 
    634   1.1      mw 	if (dev->sc_sync[target].state == SYNC_START) {
    635   1.8  chopps 		if (siop_inhibit_sync[target]) {
    636   1.1      mw 			dev->sc_sync[target].state = SYNC_DONE;
    637   1.1      mw 			dev->sc_sync[target].offset = 0;
    638   1.1      mw 			dev->sc_sync[target].period = 0;
    639   1.1      mw #ifdef DEBUG
    640   1.1      mw 			if (siopsync_debug)
    641   1.1      mw 				printf ("Forcing target %d asynchronous\n", target);
    642   1.1      mw #endif
    643   1.1      mw 		}
    644   1.1      mw 		else {
    645   1.1      mw 			dev->sc_msg[1] = MSG_IDENTIFY;
    646   1.1      mw 			dev->sc_msg[2] = MSG_EXT_MESSAGE;
    647   1.1      mw 			dev->sc_msg[3] = 3;
    648   1.1      mw 			dev->sc_msg[4] = MSG_SYNC_REQ;
    649   1.8  chopps 			dev->sc_msg[5] = siop_sync_period / 4;
    650   1.1      mw 			dev->sc_msg[6] = SIOP_MAX_OFFSET;
    651   1.1      mw 			dev->sc_ds.sdtrolen = 6;
    652   1.8  chopps 			dev->sc_ds.sdtrilen = 6;
    653   1.1      mw 			dev->sc_ds.sdtrobuf = dev->sc_ds.sdtribuf = (char *) kvtop(dev->sc_msg + 1);
    654   1.1      mw 			dev->sc_sync[target].state = SYNC_SENT;
    655   1.1      mw #ifdef DEBUG
    656   1.1      mw 			if (siopsync_debug)
    657   1.1      mw 				printf ("Sending sync request to target %d\n", target);
    658   1.1      mw #endif
    659   1.1      mw 		}
    660   1.1      mw 	}
    661   1.1      mw 
    662   1.1      mw /*
    663   1.1      mw  * If length is > 1 page, check for consecutive physical pages
    664   1.1      mw  * Need to set up chaining if not
    665   1.1      mw  */
    666   1.1      mw 	nchain = 0;
    667   1.1      mw 	count = len;
    668   1.1      mw 	addr = buf;
    669   1.1      mw 	dmaend = NULL;
    670   1.1      mw 	while (count > 0) {
    671   1.1      mw 		dev->sc_ds.chain[nchain].databuf = (char *) kvtop (addr);
    672   1.1      mw 		if (count < (tcount = NBPG - ((int) addr & PGOFSET)))
    673   1.1      mw 			tcount = count;
    674   1.1      mw 		dev->sc_ds.chain[nchain].datalen = tcount;
    675   1.1      mw 		addr += tcount;
    676   1.1      mw 		count -= tcount;
    677   1.1      mw 		if (dev->sc_ds.chain[nchain].databuf == dmaend) {
    678   1.1      mw 			dmaend += dev->sc_ds.chain[nchain].datalen;
    679   1.1      mw 			dev->sc_ds.chain[--nchain].datalen += tcount;
    680   1.1      mw #ifdef DEBUG
    681   1.1      mw 			++siopdma_hits;
    682   1.1      mw #endif
    683   1.1      mw 		}
    684   1.1      mw 		else {
    685   1.1      mw 			dmaend = dev->sc_ds.chain[nchain].databuf +
    686   1.1      mw 			    dev->sc_ds.chain[nchain].datalen;
    687   1.1      mw 			dev->sc_ds.chain[nchain].datalen = tcount;
    688   1.1      mw #ifdef DEBUG
    689   1.1      mw 			++siopdma_misses;
    690   1.1      mw #endif
    691   1.1      mw 		}
    692   1.1      mw 		++nchain;
    693   1.1      mw 	}
    694   1.1      mw #ifdef DEBUG
    695   1.1      mw 	if (nchain != 1 && len != 0 && siop_debug & 3) {
    696   1.1      mw 		printf ("DMA chaining set: %d\n", nchain);
    697   1.1      mw 		for (i = 0; i < nchain; ++i) {
    698   1.1      mw 			printf ("  [%d] %8x %4x\n", i, dev->sc_ds.chain[i].databuf,
    699   1.1      mw 			    dev->sc_ds.chain[i].datalen);
    700   1.1      mw 		}
    701   1.1      mw 	}
    702   1.1      mw #endif
    703   1.1      mw 
    704   1.1      mw 	regs->siop_sbcl = dev->sc_sync[target].offset;
    705   1.1      mw 	if (dev->sc_ds.sdtrolen)
    706   1.1      mw 		regs->siop_scratch = regs->siop_scratch | 0x100;
    707   1.1      mw 	else
    708   1.1      mw 		regs->siop_scratch = regs->siop_scratch & ~0xff00;
    709   1.1      mw 	regs->siop_dsa = (long) kvtop(&dev->sc_ds);
    710   1.1      mw 	DCIS();				/* push data cache */
    711   1.1      mw 	regs->siop_dsp = (long) kvtop(scripts);
    712   1.1      mw }
    713   1.1      mw 
    714   1.1      mw /*
    715   1.1      mw  * Process a DMA or SCSI interrupt from the 53C710 SIOP
    716   1.1      mw  */
    717   1.1      mw 
    718   1.8  chopps int
    719   1.1      mw siop_checkintr(dev, istat, dstat, sstat0, status)
    720   1.1      mw 	struct	siop_softc *dev;
    721   1.1      mw 	u_char	istat;
    722   1.1      mw 	u_char	dstat;
    723   1.1      mw 	u_char	sstat0;
    724   1.1      mw 	int	*status;
    725   1.1      mw {
    726   1.8  chopps 	siop_regmap_p regs = dev->sc_siopp;
    727   1.1      mw 	int	target;
    728   1.1      mw 
    729   1.1      mw 	regs->siop_ctest8 |= 0x04;
    730   1.1      mw 	while ((regs->siop_ctest1 & SIOP_CTEST1_FMT) == 0)
    731   1.1      mw 		;
    732   1.1      mw 	regs->siop_ctest8 &= ~0x04;
    733   1.1      mw #ifdef DEBUG
    734   1.1      mw 	if (siop_debug & 1) {
    735   1.1      mw 		DCIAS(kvtop(&dev->sc_stat));	/* XXX */
    736   1.1      mw 		printf ("siopchkintr: istat %x dstat %x sstat0 %x dsps %x sbcl %x sts %x msg %x\n",
    737   1.1      mw 		    istat, dstat, sstat0, regs->siop_dsps, regs->siop_sbcl, dev->sc_stat[0], dev->sc_msg[0]);
    738   1.1      mw 	}
    739   1.1      mw #endif
    740   1.1      mw 	if (dstat & SIOP_DSTAT_SIR && (regs->siop_dsps == 0xff00 ||
    741   1.1      mw 	    regs->siop_dsps == 0xfffc)) {
    742   1.1      mw 		/* Normal completion status, or check condition */
    743   1.1      mw 		if (regs->siop_dsa != (long) kvtop(&dev->sc_ds)) {
    744   1.1      mw 			printf ("siop: invalid dsa: %x %x\n", regs->siop_dsa,
    745   1.1      mw 			    kvtop(&dev->sc_ds));
    746   1.1      mw 			panic("*** siop DSA invalid ***");
    747   1.1      mw 		}
    748   1.1      mw 		target = dev->sc_slave;
    749   1.1      mw 		if (dev->sc_sync[target].state == SYNC_SENT) {
    750   1.1      mw #ifdef DEBUG
    751   1.1      mw 			if (siopsync_debug)
    752   1.1      mw 				printf ("sync msg in: %02x %02x %02x %02x %02x %02x\n",
    753   1.1      mw 				    dev->sc_msg[1], dev->sc_msg[2], dev->sc_msg[3],
    754   1.1      mw 				    dev->sc_msg[4], dev->sc_msg[5], dev->sc_msg[6]);
    755   1.1      mw #endif
    756   1.1      mw 			dev->sc_sync[target].state = SYNC_DONE;
    757   1.1      mw 			dev->sc_sync[target].period = 0;
    758   1.1      mw 			dev->sc_sync[target].offset = 0;
    759   1.1      mw 			if (dev->sc_msg[1] == MSG_EXT_MESSAGE &&
    760   1.1      mw 			    dev->sc_msg[2] == 3 &&
    761   1.8  chopps 			    dev->sc_msg[3] == MSG_SYNC_REQ &&
    762   1.8  chopps 			    dev->sc_msg[5] != 0) {
    763   1.8  chopps 				if (dev->sc_msg[4] && dev->sc_msg[4] < 100 / 4) {
    764   1.8  chopps #ifdef DEBUG
    765   1.8  chopps 					printf ("%d: target %d wanted %dns period\n",
    766   1.8  chopps 					    dev->sc_dev.dv_xname, target,
    767   1.8  chopps 					    dev->sc_msg[4] * 4);
    768   1.8  chopps #endif
    769   1.8  chopps 					/*
    770   1.8  chopps 					 * Kludge for Maxtor XT8580S
    771   1.8  chopps 					 * It accepts whatever we request, even
    772   1.8  chopps 					 * though it won't work.  So we ask for
    773   1.8  chopps 					 * a short period than we can handle.  If
    774   1.8  chopps 					 * the device says it can do it, use 208ns.
    775   1.8  chopps 					 * If the device says it can do less than
    776   1.8  chopps 					 * 100ns, then we limit it to 100ns.
    777   1.8  chopps 					 */
    778   1.8  chopps 					if (dev->sc_msg[4] == siop_sync_period / 4)
    779   1.8  chopps 						dev->sc_msg[4] = 208 / 4;
    780   1.8  chopps 					else
    781   1.8  chopps 						dev->sc_msg[4] = 100 / 4;
    782   1.8  chopps 				}
    783   1.8  chopps 				printf ("%s: target %d now synchronous, period=%dns, offset=%d\n",
    784   1.8  chopps 				    dev->sc_dev.dv_xname, target,
    785   1.1      mw 				    dev->sc_msg[4] * 4, dev->sc_msg[5]);
    786   1.1      mw 				scsi_period_to_siop (dev, target);
    787   1.1      mw 			}
    788   1.1      mw 		}
    789   1.1      mw 		DCIAS(kvtop(&dev->sc_stat));	/* XXX */
    790   1.1      mw 		*status = dev->sc_stat[0];
    791   1.1      mw 		return 1;
    792   1.1      mw 	}
    793   1.1      mw 	if (sstat0 & SIOP_SSTAT0_M_A) {		/* Phase mismatch */
    794   1.1      mw #ifdef DEBUG
    795   1.1      mw 		if (siop_debug & 9)
    796   1.1      mw 			printf ("Phase mismatch: %x dsp +%x\n", regs->siop_sbcl,
    797   1.1      mw 			    regs->siop_dsp - kvtop(scripts));
    798   1.8  chopps 		if (siop_debug & 0x10)
    799   1.8  chopps 			panic ("53c710 phase mismatch");
    800   1.1      mw #endif
    801   1.1      mw 		if ((regs->siop_sbcl & SIOP_REQ) == 0)
    802   1.1      mw 			printf ("Phase mismatch: REQ not asserted! %02x\n",
    803   1.1      mw 			    regs->siop_sbcl);
    804   1.1      mw 		switch (regs->siop_sbcl & 7) {
    805   1.1      mw /*
    806   1.1      mw  * For data out and data in phase, check for DMA chaining
    807   1.1      mw  */
    808   1.1      mw 
    809   1.1      mw /*
    810   1.1      mw  * for message in, check for possible reject for sync request
    811   1.1      mw  */
    812   1.1      mw 		case 0:
    813   1.1      mw 			regs->siop_dsp = kvtop(scripts) + Ent_dataout;
    814   1.1      mw 			break;
    815   1.1      mw 		case 1:
    816   1.1      mw 			regs->siop_dsp = kvtop(scripts) + Ent_datain;
    817   1.1      mw 			break;
    818   1.1      mw 		case 2:
    819   1.1      mw 			regs->siop_dsp = kvtop(scripts) + Ent_cmd;
    820   1.1      mw 			break;
    821   1.1      mw 		case 3:
    822   1.1      mw 			regs->siop_dsp = kvtop(scripts) + Ent_status;
    823   1.1      mw 			break;
    824   1.1      mw 		case 6:
    825   1.1      mw 			regs->siop_dsp = kvtop(scripts) + Ent_msgout;
    826   1.1      mw 			break;
    827   1.1      mw 		case 7:
    828   1.1      mw 			regs->siop_dsp = kvtop(scripts) + Ent_msgin;
    829   1.1      mw 			break;
    830   1.1      mw 		default:
    831   1.1      mw 			goto bad_phase;
    832   1.1      mw 		}
    833   1.1      mw 		return 0;
    834   1.1      mw 	}
    835   1.1      mw 	if (sstat0 & SIOP_SSTAT0_STO) {		/* Select timed out */
    836   1.1      mw 		*status = -1;
    837   1.1      mw 		return 1;
    838   1.1      mw 	}
    839   1.1      mw 	if (dstat & SIOP_DSTAT_SIR && regs->siop_dsps == 0xff05 &&
    840   1.1      mw 	    (regs->siop_sbcl & (SIOP_MSG | SIOP_CD)) == 0) {
    841   1.1      mw 		printf ("DMA chaining failed\n");
    842   1.8  chopps 		siopreset (dev);
    843   1.1      mw 		*status = -1;
    844   1.1      mw 		return 1;
    845   1.1      mw 	}
    846   1.1      mw 	if (dstat & SIOP_DSTAT_SIR && regs->siop_dsps == 0xff27) {
    847   1.1      mw #ifdef DEBUG
    848   1.1      mw 		if (siop_debug & 3)
    849   1.1      mw 			printf ("DMA chaining completed: dsa %x dnad %x addr %x\n",
    850   1.1      mw 				regs->siop_dsa,	regs->siop_dnad, regs->siop_addr);
    851   1.1      mw #endif
    852   1.1      mw 		regs->siop_dsa = kvtop (&dev->sc_ds);
    853   1.1      mw 		regs->siop_dsp = kvtop (scripts) + Ent_status;
    854   1.1      mw 		return 0;
    855   1.1      mw 	}
    856   1.1      mw 	target = dev->sc_slave;
    857   1.1      mw 	if (dstat & SIOP_DSTAT_SIR && regs->siop_dsps == 0xff26 &&
    858   1.1      mw 	    dev->sc_msg[0] == MSG_REJECT && dev->sc_sync[target].state == SYNC_SENT) {
    859   1.1      mw 		dev->sc_sync[target].state = SYNC_DONE;
    860   1.1      mw 		dev->sc_sync[target].period = 0;
    861   1.1      mw 		dev->sc_sync[target].offset = 0;
    862   1.1      mw 		dev->sc_ds.sdtrolen = 0;
    863   1.1      mw 		dev->sc_ds.sdtrilen = 0;
    864   1.1      mw #ifdef DEBUG
    865   1.1      mw 		if (siopsync_debug || 1)
    866   1.1      mw 			printf ("target %d rejected sync, going asynchronous\n", target);
    867   1.1      mw #endif
    868   1.8  chopps 		siop_inhibit_sync[target] = -1;
    869   1.1      mw 		if ((regs->siop_sbcl & 7) == 6) {
    870   1.1      mw 			regs->siop_dsp = kvtop(scripts) + Ent_msgout;
    871   1.1      mw 			return (0);
    872   1.1      mw 		}
    873   1.1      mw 	}
    874   1.8  chopps 	if ((dstat & SIOP_DSTAT_SIR && regs->siop_dsps == 0xff13) ||
    875   1.8  chopps 	    sstat0 & SIOP_SSTAT0_UDC) {
    876   1.8  chopps #ifdef DEBUG
    877   1.8  chopps 		printf ("%s: target %d disconnected unexpectedly\n",
    878   1.8  chopps 		   dev->sc_dev.dv_xname, target);
    879   1.8  chopps #endif
    880   1.8  chopps #if 0
    881   1.8  chopps 		siopabort (dev, regs, "siopchkintr");
    882   1.8  chopps #endif
    883   1.8  chopps 		*status = STS_BUSY;
    884   1.8  chopps 		return 1;
    885   1.8  chopps 	}
    886   1.8  chopps 	if (dstat & SIOP_DSTAT_SIR &&regs->siop_dsps == 0xfffb) {
    887   1.8  chopps #if 0
    888   1.8  chopps 		printf ("%s: target %d busy\n", dev->sc_dev.dv_xname, target);
    889   1.8  chopps #endif
    890   1.8  chopps #if 0
    891   1.8  chopps 		siopabort (dev, regs, "siopchkintr");
    892   1.8  chopps #endif
    893   1.8  chopps 		*status = STS_BUSY;
    894   1.8  chopps 		return 1;
    895   1.8  chopps 	}
    896   1.1      mw 	if (sstat0 == 0 && dstat & SIOP_DSTAT_SIR) {
    897   1.1      mw 		DCIAS(kvtop(&dev->sc_stat));
    898   1.1      mw 		printf ("SIOP interrupt: %x sts %x msg %x sbcl %x\n",
    899   1.1      mw 		    regs->siop_dsps, dev->sc_stat[0], dev->sc_msg[0],
    900   1.1      mw 		    regs->siop_sbcl);
    901   1.8  chopps 		siopreset (dev);
    902   1.1      mw 		*status = -1;
    903   1.1      mw 		return 1;
    904   1.1      mw 	}
    905   1.1      mw bad_phase:
    906   1.1      mw /*
    907   1.1      mw  * temporary panic for unhandled conditions
    908   1.1      mw  * displays various things about the 53C710 status and registers
    909   1.1      mw  * then panics
    910   1.1      mw  */
    911   1.1      mw printf ("siopchkintr: target %x ds %x\n", target, &dev->sc_ds);
    912   1.1      mw printf ("scripts %x ds %x regs %x dsp %x dcmd %x\n", kvtop(scripts),
    913   1.1      mw     kvtop(&dev->sc_ds), kvtop(regs), regs->siop_dsp, *((long *)&regs->siop_dcmd));
    914   1.1      mw printf ("siopchkintr: istat %x dstat %x sstat0 %x dsps %x dsa %x sbcl %x sts %x msg %x\n",
    915   1.1      mw     istat, dstat, sstat0, regs->siop_dsps, regs->siop_dsa, regs->siop_sbcl,
    916   1.1      mw     dev->sc_stat[0], dev->sc_msg[0]);
    917   1.1      mw panic("siopchkintr: **** temp ****");
    918   1.1      mw }
    919   1.1      mw 
    920   1.1      mw /*
    921   1.1      mw  * SCSI 'immediate' command:  issue a command to some SCSI device
    922   1.1      mw  * and get back an 'immediate' response (i.e., do programmed xfer
    923   1.1      mw  * to get the response data).  'cbuf' is a buffer containing a scsi
    924   1.1      mw  * command of length clen bytes.  'buf' is a buffer of length 'len'
    925   1.1      mw  * bytes for data.  The transfer direction is determined by the device
    926   1.1      mw  * (i.e., by the scsi bus data xfer phase).  If 'len' is zero, the
    927   1.1      mw  * command must supply no data.  'xferphase' is the bus phase the
    928   1.1      mw  * caller expects to happen after the command is issued.  It should
    929   1.1      mw  * be one of DATA_IN_PHASE, DATA_OUT_PHASE or STATUS_PHASE.
    930   1.1      mw  *
    931   1.1      mw  * XXX - 53C710 will use DMA, but no interrupts (it's a heck of a
    932   1.1      mw  * lot easier to do than to use programmed I/O).
    933   1.1      mw  *
    934   1.1      mw  */
    935   1.8  chopps int
    936   1.1      mw siopicmd(dev, target, cbuf, clen, buf, len)
    937   1.1      mw 	struct siop_softc *dev;
    938   1.1      mw 	int target;
    939   1.8  chopps 	void *cbuf;
    940   1.1      mw 	int clen;
    941   1.8  chopps 	void *buf;
    942   1.1      mw 	int len;
    943   1.1      mw {
    944   1.8  chopps 	siop_regmap_p regs = dev->sc_siopp;
    945   1.1      mw 	int i;
    946   1.1      mw 	int status;
    947   1.1      mw 	u_char istat;
    948   1.1      mw 	u_char dstat;
    949   1.1      mw 	u_char sstat0;
    950   1.1      mw 
    951   1.1      mw 	if (dev->sc_flags & SIOP_SELECTED) {
    952   1.1      mw 		printf ("siopicmd%d: bus busy\n", target);
    953   1.1      mw 		return -1;
    954   1.1      mw 	}
    955   1.1      mw 	regs->siop_sien = 0x00;		/* disable SCSI and DMA interrupts */
    956   1.1      mw 	regs->siop_dien = 0x00;
    957   1.1      mw 	dev->sc_flags |= SIOP_SELECTED;
    958   1.1      mw 	dev->sc_slave = target;
    959   1.1      mw #ifdef DEBUG
    960   1.1      mw 	if (siop_debug & 1)
    961   1.1      mw 		printf ("siopicmd: target %x cmd %02x ds %x\n", target,
    962   1.8  chopps 		    *((char *)cbuf), &dev->sc_ds);
    963   1.1      mw #endif
    964   1.1      mw 	siop_setup (dev, target, cbuf, clen, buf, len);
    965   1.1      mw 
    966   1.1      mw 	for (;;) {
    967   1.1      mw 		/* use cmd_wait values? */
    968   1.1      mw 		i = siop_cmd_wait << 1;
    969   1.1      mw 		while (((istat = regs->siop_istat) &
    970   1.1      mw 		    (SIOP_ISTAT_SIP | SIOP_ISTAT_DIP)) == 0) {
    971   1.1      mw 			if (--i <= 0) {
    972   1.1      mw 				printf ("waiting: tgt %d cmd %02x sbcl %02x dsp %x (+%x) dcmd %x ds %x\n",
    973   1.8  chopps 				    target, *((char *)cbuf),
    974   1.1      mw 				    regs->siop_sbcl, regs->siop_dsp,
    975   1.1      mw 				    regs->siop_dsp - kvtop(scripts),
    976   1.1      mw 				    *((long *)&regs->siop_dcmd), &dev->sc_ds);
    977   1.1      mw 				i = siop_cmd_wait << 2;
    978   1.8  chopps 				/* XXXX need an upper limit and reset */
    979   1.1      mw 			}
    980   1.1      mw 			DELAY(1);
    981   1.1      mw 		}
    982   1.1      mw 		dstat = regs->siop_dstat;
    983   1.1      mw 		sstat0 = regs->siop_sstat0;
    984   1.1      mw #ifdef DEBUG
    985   1.1      mw 		if (siop_debug & 1) {
    986   1.1      mw 			DCIAS(kvtop(&dev->sc_stat));	/* XXX should just invalidate dev->sc_stat */
    987   1.1      mw 			printf ("siopicmd: istat %x dstat %x sstat0 %x dsps %x sbcl %x sts %x msg %x\n",
    988   1.1      mw 			    istat, dstat, sstat0, regs->siop_dsps, regs->siop_sbcl,
    989   1.1      mw 			    dev->sc_stat[0], dev->sc_msg[0]);
    990   1.1      mw 		}
    991   1.1      mw #endif
    992   1.1      mw 		if (siop_checkintr(dev, istat, dstat, sstat0, &status)) {
    993   1.1      mw 			dev->sc_flags &= ~SIOP_SELECTED;
    994   1.1      mw 			return (status);
    995   1.1      mw 		}
    996   1.1      mw 	}
    997   1.1      mw }
    998   1.1      mw 
    999   1.1      mw int
   1000   1.8  chopps siopgo(dev, xs)
   1001   1.8  chopps 	struct siop_softc *dev;
   1002   1.8  chopps 	struct scsi_xfer *xs;
   1003   1.1      mw {
   1004   1.8  chopps 	siop_regmap_p regs;
   1005   1.1      mw 	int i;
   1006   1.1      mw 	int nchain;
   1007   1.1      mw 	int count, tcount;
   1008   1.1      mw 	char *addr, *dmaend;
   1009   1.1      mw 
   1010   1.1      mw #ifdef DEBUG
   1011   1.1      mw 	if (siop_debug & 1)
   1012   1.8  chopps 		printf ("%s: go ", dev->sc_dev.dv_xname);
   1013   1.8  chopps #if 0
   1014   1.1      mw 	if ((cdb->cdb[1] & 1) == 0 &&
   1015   1.1      mw 	    ((cdb->cdb[0] == CMD_WRITE && cdb->cdb[2] == 0 && cdb->cdb[3] == 0) ||
   1016   1.1      mw 	    (cdb->cdb[0] == CMD_WRITE_EXT && cdb->cdb[2] == 0 && cdb->cdb[3] == 0
   1017   1.1      mw 	    && cdb->cdb[4] == 0)))
   1018   1.1      mw 		panic ("siopgo: attempted write to block < 0x100");
   1019   1.1      mw #endif
   1020   1.8  chopps #endif
   1021   1.8  chopps #if 0
   1022   1.1      mw 	cdb->cdb[1] |= unit << 5;
   1023   1.8  chopps #endif
   1024   1.1      mw 
   1025   1.1      mw 	if (dev->sc_flags & SIOP_SELECTED) {
   1026   1.8  chopps 		printf ("%s: bus busy\n", dev->sc_dev.dv_xname);
   1027   1.1      mw 		return 1;
   1028   1.1      mw 	}
   1029   1.1      mw 
   1030   1.1      mw 	dev->sc_flags |= SIOP_SELECTED | SIOP_DMA;
   1031   1.8  chopps 	dev->sc_slave = xs->sc_link->target;
   1032   1.8  chopps 	regs = dev->sc_siopp;
   1033   1.1      mw 	/* enable SCSI and DMA interrupts */
   1034   1.1      mw 	regs->siop_sien = SIOP_SIEN_M_A | SIOP_SIEN_STO | SIOP_SIEN_SEL | SIOP_SIEN_SGE |
   1035   1.1      mw 	    SIOP_SIEN_UDC | SIOP_SIEN_RST | SIOP_SIEN_PAR;
   1036   1.1      mw 	regs->siop_dien = 0x20 | SIOP_DIEN_ABRT | SIOP_DIEN_SIR | SIOP_DIEN_WTD |
   1037   1.1      mw 	    SIOP_DIEN_OPC;
   1038   1.1      mw #ifdef DEBUG
   1039   1.1      mw 	if (siop_debug & 1)
   1040  1.10  chopps 		printf ("siopgo: target %x cmd %02x ds %x\n", dev->sc_slave, xs->cmd->opcode, &dev->sc_ds);
   1041   1.1      mw #endif
   1042   1.1      mw 
   1043   1.8  chopps 	siop_setup(dev, dev->sc_slave, xs->cmd, xs->cmdlen, xs->data, xs->datalen);
   1044   1.1      mw 
   1045   1.1      mw 	return (0);
   1046   1.1      mw }
   1047   1.1      mw 
   1048   1.1      mw /*
   1049   1.8  chopps  * Check for 53C710 interrupts
   1050   1.1      mw  */
   1051   1.1      mw 
   1052   1.1      mw int
   1053   1.8  chopps siopintr (dev)
   1054   1.1      mw 	register struct siop_softc *dev;
   1055   1.1      mw {
   1056   1.8  chopps 	siop_regmap_p regs;
   1057   1.1      mw 	register u_char istat, dstat, sstat0;
   1058   1.1      mw 	int unit;
   1059   1.1      mw 	int status;
   1060   1.1      mw 	int found = 0;
   1061   1.1      mw 
   1062   1.8  chopps 	regs = dev->sc_siopp;
   1063   1.8  chopps 	istat = dev->sc_istat;
   1064   1.8  chopps 	if ((istat & (SIOP_ISTAT_SIP | SIOP_ISTAT_DIP)) == 0)
   1065   1.8  chopps 		return;
   1066   1.8  chopps 	if ((dev->sc_flags & (SIOP_DMA | SIOP_SELECTED)) == SIOP_SELECTED)
   1067   1.8  chopps 		return;	/* doing non-interrupt I/O */
   1068   1.1      mw 		/* Got a valid interrupt on this device */
   1069   1.8  chopps 	dstat = dev->sc_dstat;
   1070   1.8  chopps 	sstat0 = dev->sc_sstat0;
   1071   1.8  chopps 	dev->sc_istat = 0;
   1072   1.1      mw #ifdef DEBUG
   1073   1.8  chopps 	if (siop_debug & 1)
   1074   1.8  chopps 		printf ("%s: intr istat %x dstat %x sstat0 %x\n",
   1075   1.8  chopps 		    dev->sc_dev.dv_xname, istat, dstat, sstat0);
   1076   1.8  chopps 	if ((dev->sc_flags & SIOP_DMA) == 0) {
   1077   1.8  chopps 		printf ("%s: spurious interrupt? istat %x dstat %x sstat0 %x\n",
   1078   1.8  chopps 		    dev->sc_dev.dv_xname, istat, dstat, sstat0);
   1079   1.8  chopps 	}
   1080   1.1      mw #endif
   1081   1.1      mw 
   1082   1.1      mw #ifdef DEBUG
   1083   1.8  chopps 	if (siop_debug & 5) {
   1084   1.8  chopps 		DCIAS(kvtop(&dev->sc_stat));
   1085   1.8  chopps 		printf ("siopintr%d: istat %x dstat %x sstat0 %x dsps %x sbcl %x sts %x msg %x\n",
   1086   1.8  chopps 		    unit, istat, dstat, sstat0, regs->siop_dsps,
   1087   1.8  chopps 		    regs->siop_sbcl, dev->sc_stat[0], dev->sc_msg[0]);
   1088   1.8  chopps 	}
   1089   1.1      mw #endif
   1090   1.8  chopps 	if (siop_checkintr (dev, istat, dstat, sstat0, &status)) {
   1091   1.1      mw #if 1
   1092   1.8  chopps 		regs->siop_sien = 0;
   1093   1.8  chopps 		regs->siop_dien = 0;
   1094   1.8  chopps 		if (status == 0xff)
   1095   1.8  chopps 			printf ("siopintr: status == 0xff\n");
   1096   1.8  chopps #endif
   1097   1.8  chopps 		dev->sc_flags &= ~(SIOP_DMA | SIOP_SELECTED);
   1098   1.8  chopps 		siop_scsidone(dev, dev->sc_stat[0]);
   1099   1.1      mw 	}
   1100   1.1      mw }
   1101   1.1      mw 
   1102   1.8  chopps scsi_period_to_siop (dev, target)
   1103   1.8  chopps 	struct siop_softc *dev;
   1104   1.1      mw {
   1105   1.8  chopps 	int period, offset, i, sxfer;
   1106   1.1      mw 
   1107   1.8  chopps 	period = dev->sc_msg[4];
   1108   1.8  chopps 	offset = dev->sc_msg[5];
   1109   1.8  chopps 	sxfer = 0;
   1110   1.8  chopps 	if (offset <= SIOP_MAX_OFFSET)
   1111   1.8  chopps 		sxfer = offset;
   1112   1.8  chopps 	for (i = 0; i < sizeof (xxx) / 2; ++i) {
   1113   1.8  chopps 		if (period <= xxx[i].x) {
   1114   1.8  chopps 			sxfer |= xxx[i].y & 0x70;
   1115   1.8  chopps 			offset = xxx[i].y & 0x03;
   1116   1.8  chopps 			break;
   1117   1.8  chopps 		}
   1118   1.8  chopps 	}
   1119   1.8  chopps 	dev->sc_sync[target].period = sxfer;
   1120   1.8  chopps 	dev->sc_sync[target].offset = offset;
   1121   1.1      mw #ifdef DEBUG
   1122   1.8  chopps 	printf ("sync: siop_sxfr %02x, siop_sbcl %02x\n", sxfer, offset);
   1123   1.1      mw #endif
   1124   1.1      mw }
   1125