Home | History | Annotate | Line # | Download | only in ic
siop_common.c revision 1.19
      1  1.19  bouyer /*	$NetBSD: siop_common.c,v 1.19 2002/04/22 15:48:56 bouyer Exp $	*/
      2   1.1  bouyer 
      3   1.1  bouyer /*
      4   1.1  bouyer  * Copyright (c) 2000 Manuel Bouyer.
      5   1.1  bouyer  *
      6   1.1  bouyer  * Redistribution and use in source and binary forms, with or without
      7   1.1  bouyer  * modification, are permitted provided that the following conditions
      8   1.1  bouyer  * are met:
      9   1.1  bouyer  * 1. Redistributions of source code must retain the above copyright
     10   1.1  bouyer  *    notice, this list of conditions and the following disclaimer.
     11   1.1  bouyer  * 2. Redistributions in binary form must reproduce the above copyright
     12   1.1  bouyer  *    notice, this list of conditions and the following disclaimer in the
     13   1.1  bouyer  *    documentation and/or other materials provided with the distribution.
     14   1.1  bouyer  * 3. All advertising materials mentioning features or use of this software
     15   1.1  bouyer  *    must display the following acknowledgement:
     16   1.1  bouyer  *	This product includes software developed by Manuel Bouyer
     17   1.1  bouyer  * 4. The name of the author may not be used to endorse or promote products
     18   1.1  bouyer  *    derived from this software without specific prior written permission.
     19   1.1  bouyer  *
     20   1.1  bouyer  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21   1.1  bouyer  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22   1.1  bouyer  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23   1.1  bouyer  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24   1.1  bouyer  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25   1.1  bouyer  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26   1.1  bouyer  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27   1.1  bouyer  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28   1.1  bouyer  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29   1.1  bouyer  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30   1.1  bouyer  *
     31   1.1  bouyer  */
     32   1.1  bouyer 
     33   1.1  bouyer /* SYM53c7/8xx PCI-SCSI I/O Processors driver */
     34  1.15   lukem 
     35  1.15   lukem #include <sys/cdefs.h>
     36  1.19  bouyer __KERNEL_RCSID(0, "$NetBSD: siop_common.c,v 1.19 2002/04/22 15:48:56 bouyer Exp $");
     37   1.1  bouyer 
     38   1.1  bouyer #include <sys/param.h>
     39   1.1  bouyer #include <sys/systm.h>
     40   1.1  bouyer #include <sys/device.h>
     41   1.1  bouyer #include <sys/malloc.h>
     42   1.1  bouyer #include <sys/buf.h>
     43   1.1  bouyer #include <sys/kernel.h>
     44   1.1  bouyer #include <sys/scsiio.h>
     45   1.1  bouyer 
     46   1.1  bouyer #include <machine/endian.h>
     47   1.1  bouyer #include <machine/bus.h>
     48   1.1  bouyer 
     49   1.1  bouyer #include <dev/scsipi/scsi_all.h>
     50   1.1  bouyer #include <dev/scsipi/scsi_message.h>
     51   1.1  bouyer #include <dev/scsipi/scsipi_all.h>
     52   1.1  bouyer 
     53   1.1  bouyer #include <dev/scsipi/scsiconf.h>
     54   1.1  bouyer 
     55   1.1  bouyer #include <dev/ic/siopreg.h>
     56   1.1  bouyer #include <dev/ic/siopvar_common.h>
     57   1.1  bouyer 
     58  1.16  bouyer #include "opt_siop.h"
     59  1.16  bouyer 
     60   1.2  bouyer #undef DEBUG
     61   1.2  bouyer #undef DEBUG_DR
     62   1.1  bouyer 
     63   1.1  bouyer void
     64   1.1  bouyer siop_common_reset(sc)
     65  1.17  bouyer 	struct siop_common_softc *sc;
     66   1.1  bouyer {
     67   1.1  bouyer 	u_int32_t stest3;
     68   1.1  bouyer 
     69   1.1  bouyer 	/* reset the chip */
     70   1.1  bouyer 	bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_ISTAT, ISTAT_SRST);
     71   1.1  bouyer 	delay(1000);
     72   1.1  bouyer 	bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_ISTAT, 0);
     73   1.1  bouyer 
     74   1.1  bouyer 	/* init registers */
     75   1.1  bouyer 	bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_SCNTL0,
     76   1.1  bouyer 	    SCNTL0_ARB_MASK | SCNTL0_EPC | SCNTL0_AAP);
     77   1.1  bouyer 	bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_SCNTL1, 0);
     78   1.1  bouyer 	bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_SCNTL3, sc->clock_div);
     79   1.7  bouyer 	bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_SXFER, 0);
     80   1.1  bouyer 	bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_DIEN, 0xff);
     81   1.1  bouyer 	bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_SIEN0,
     82   1.1  bouyer 	    0xff & ~(SIEN0_CMP | SIEN0_SEL | SIEN0_RSL));
     83   1.1  bouyer 	bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_SIEN1,
     84   1.1  bouyer 	    0xff & ~(SIEN1_HTH | SIEN1_GEN));
     85   1.1  bouyer 	bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_STEST2, 0);
     86   1.1  bouyer 	bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_STEST3, STEST3_TE);
     87   1.1  bouyer 	bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_STIME0,
     88   1.1  bouyer 	    (0xb << STIME0_SEL_SHIFT));
     89   1.1  bouyer 	bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_SCID,
     90  1.14  bouyer 	    sc->sc_chan.chan_id | SCID_RRE);
     91   1.1  bouyer 	bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_RESPID0,
     92  1.14  bouyer 	    1 << sc->sc_chan.chan_id);
     93   1.1  bouyer 	bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_DCNTL,
     94   1.1  bouyer 	    (sc->features & SF_CHIP_PF) ? DCNTL_COM | DCNTL_PFEN : DCNTL_COM);
     95   1.1  bouyer 
     96   1.1  bouyer 	/* enable clock doubler or quadruler if appropriate */
     97   1.1  bouyer 	if (sc->features & (SF_CHIP_DBLR | SF_CHIP_QUAD)) {
     98   1.1  bouyer 		stest3 = bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_STEST3);
     99   1.1  bouyer 		bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_STEST1,
    100   1.1  bouyer 		    STEST1_DBLEN);
    101   1.1  bouyer 		if (sc->features & SF_CHIP_QUAD) {
    102   1.1  bouyer 			/* wait for PPL to lock */
    103   1.1  bouyer 			while ((bus_space_read_1(sc->sc_rt, sc->sc_rh,
    104   1.1  bouyer 			    SIOP_STEST4) & STEST4_LOCK) == 0)
    105   1.1  bouyer 				delay(10);
    106   1.1  bouyer 		} else {
    107   1.1  bouyer 			/* data sheet says 20us - more won't hurt */
    108   1.1  bouyer 			delay(100);
    109   1.1  bouyer 		}
    110   1.1  bouyer 		/* halt scsi clock, select doubler/quad, restart clock */
    111   1.1  bouyer 		bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_STEST3,
    112   1.1  bouyer 		    stest3 | STEST3_HSC);
    113   1.1  bouyer 		bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_STEST1,
    114   1.1  bouyer 		    STEST1_DBLEN | STEST1_DBLSEL);
    115   1.1  bouyer 		bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_STEST3, stest3);
    116   1.1  bouyer 	} else {
    117   1.1  bouyer 		bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_STEST1, 0);
    118   1.1  bouyer 	}
    119   1.1  bouyer 	if (sc->features & SF_CHIP_FIFO)
    120   1.1  bouyer 		bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_CTEST5,
    121   1.1  bouyer 		    bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_CTEST5) |
    122   1.1  bouyer 		    CTEST5_DFS);
    123  1.16  bouyer #ifdef SIOP_SYMLED
    124  1.16  bouyer 	/* Set GPIO0 as output if software LED control is required */
    125  1.16  bouyer 	bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_GPCNTL,
    126  1.16  bouyer 	    bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_GPCNTL) & 0xfe);
    127  1.16  bouyer #endif
    128   1.1  bouyer 
    129   1.1  bouyer 	sc->sc_reset(sc);
    130   1.1  bouyer }
    131   1.1  bouyer 
    132  1.10  bouyer /* prepare tables before sending a cmd */
    133  1.10  bouyer void
    134  1.10  bouyer siop_setuptables(siop_cmd)
    135  1.17  bouyer 	struct siop_common_cmd *siop_cmd;
    136  1.10  bouyer {
    137  1.10  bouyer 	int i;
    138  1.17  bouyer 	struct siop_common_softc *sc = siop_cmd->siop_sc;
    139  1.10  bouyer 	struct scsipi_xfer *xs = siop_cmd->xs;
    140  1.14  bouyer 	int target = xs->xs_periph->periph_target;
    141  1.14  bouyer 	int lun = xs->xs_periph->periph_lun;
    142  1.14  bouyer 	int msgoffset = 1;
    143  1.10  bouyer 
    144  1.17  bouyer 	siop_cmd->siop_tables->id = htole32(sc->targets[target]->id);
    145  1.17  bouyer 	memset(siop_cmd->siop_tables->msg_out, 0, 8);
    146  1.14  bouyer 	/* request sense doesn't disconnect */
    147  1.14  bouyer 	if (xs->xs_control & XS_CTL_REQSENSE)
    148  1.17  bouyer 		siop_cmd->siop_tables->msg_out[0] = MSG_IDENTIFY(lun, 0);
    149  1.14  bouyer 	else
    150  1.17  bouyer 		siop_cmd->siop_tables->msg_out[0] = MSG_IDENTIFY(lun, 1);
    151  1.17  bouyer 	siop_cmd->siop_tables->t_msgout.count= htole32(1);
    152  1.14  bouyer 	if (xs->xs_tag_type != 0) {
    153  1.14  bouyer 		if ((sc->targets[target]->flags & TARF_TAG) == 0) {
    154  1.14  bouyer 			scsipi_printaddr(xs->xs_periph);
    155  1.14  bouyer 			printf(": tagged command type %d id %d\n",
    156  1.14  bouyer 			    siop_cmd->xs->xs_tag_type, siop_cmd->xs->xs_tag_id);
    157  1.14  bouyer 			panic("tagged command for non-tagging device\n");
    158  1.14  bouyer 		}
    159  1.14  bouyer 		siop_cmd->flags |= CMDFL_TAG;
    160  1.17  bouyer 		siop_cmd->siop_tables->msg_out[1] = siop_cmd->xs->xs_tag_type;
    161  1.19  bouyer 		/*
    162  1.19  bouyer 		 * use siop_cmd->tag not xs->xs_tag_id, caller may want a
    163  1.19  bouyer 		 * different one
    164  1.19  bouyer 		 */
    165  1.19  bouyer 		siop_cmd->siop_tables->msg_out[2] = siop_cmd->tag;
    166  1.17  bouyer 		siop_cmd->siop_tables->t_msgout.count = htole32(3);
    167  1.14  bouyer 		msgoffset = 3;
    168  1.14  bouyer 	} else
    169  1.14  bouyer 		siop_cmd->tag = 0;
    170  1.10  bouyer 	if (sc->targets[target]->status == TARST_ASYNC) {
    171  1.10  bouyer 		if (sc->targets[target]->flags & TARF_WIDE) {
    172  1.10  bouyer 			sc->targets[target]->status = TARST_WIDE_NEG;
    173  1.14  bouyer 			siop_wdtr_msg(siop_cmd, msgoffset,
    174  1.14  bouyer 			    MSG_EXT_WDTR_BUS_16_BIT);
    175  1.10  bouyer 		} else if (sc->targets[target]->flags & TARF_SYNC) {
    176  1.10  bouyer 			sc->targets[target]->status = TARST_SYNC_NEG;
    177  1.14  bouyer 			siop_sdtr_msg(siop_cmd, msgoffset,
    178  1.14  bouyer 			    sc->minsync, sc->maxoff);
    179  1.10  bouyer 		} else {
    180  1.10  bouyer 			sc->targets[target]->status = TARST_OK;
    181  1.14  bouyer 			siop_update_xfer_mode(sc, target);
    182  1.10  bouyer 		}
    183  1.10  bouyer 	}
    184  1.14  bouyer 	if (xs->xs_tag_type != 0 && (siop_cmd->flags & CMDFL_TAG) == 0)
    185  1.14  bouyer 		printf("siop_setuptables: tagged CMD type 0x%x for target %d lun %d runs untagged, status 0x%x flags 0x%x\n", xs->xs_tag_type, target, lun, sc->targets[target]->status, sc->targets[target]->flags);
    186  1.17  bouyer 	siop_cmd->siop_tables->status =
    187  1.11  bouyer 	    htole32(SCSI_SIOP_NOSTATUS); /* set invalid status */
    188  1.10  bouyer 
    189  1.17  bouyer 	siop_cmd->siop_tables->cmd.count =
    190  1.10  bouyer 	    htole32(siop_cmd->dmamap_cmd->dm_segs[0].ds_len);
    191  1.17  bouyer 	siop_cmd->siop_tables->cmd.addr =
    192  1.10  bouyer 	    htole32(siop_cmd->dmamap_cmd->dm_segs[0].ds_addr);
    193  1.14  bouyer 	if (xs->xs_control & (XS_CTL_DATA_IN | XS_CTL_DATA_OUT)) {
    194  1.10  bouyer 		for (i = 0; i < siop_cmd->dmamap_data->dm_nsegs; i++) {
    195  1.17  bouyer 			siop_cmd->siop_tables->data[i].count =
    196  1.10  bouyer 			    htole32(siop_cmd->dmamap_data->dm_segs[i].ds_len);
    197  1.17  bouyer 			siop_cmd->siop_tables->data[i].addr =
    198  1.10  bouyer 			    htole32(siop_cmd->dmamap_data->dm_segs[i].ds_addr);
    199  1.10  bouyer 		}
    200  1.10  bouyer 	}
    201  1.10  bouyer }
    202  1.10  bouyer 
    203   1.1  bouyer int
    204   1.1  bouyer siop_wdtr_neg(siop_cmd)
    205  1.17  bouyer 	struct siop_common_cmd *siop_cmd;
    206   1.1  bouyer {
    207  1.17  bouyer 	struct siop_common_softc *sc = siop_cmd->siop_sc;
    208  1.17  bouyer 	struct siop_common_target *siop_target = siop_cmd->siop_target;
    209  1.14  bouyer 	int target = siop_cmd->xs->xs_periph->periph_target;
    210  1.17  bouyer 	struct siop_common_xfer *tables = siop_cmd->siop_tables;
    211   1.1  bouyer 
    212   1.1  bouyer 	if (siop_target->status == TARST_WIDE_NEG) {
    213   1.1  bouyer 		/* we initiated wide negotiation */
    214   1.9  bouyer 		switch (tables->msg_in[3]) {
    215   1.1  bouyer 		case MSG_EXT_WDTR_BUS_8_BIT:
    216   1.9  bouyer 			siop_target->flags &= ~TARF_ISWIDE;
    217   1.1  bouyer 			sc->targets[target]->id &= ~(SCNTL3_EWS << 24);
    218   1.1  bouyer 			break;
    219   1.1  bouyer 		case MSG_EXT_WDTR_BUS_16_BIT:
    220   1.9  bouyer 			if (siop_target->flags & TARF_WIDE) {
    221   1.9  bouyer 				siop_target->flags |= TARF_ISWIDE;
    222   1.1  bouyer 				sc->targets[target]->id |= (SCNTL3_EWS << 24);
    223   1.1  bouyer 				break;
    224   1.1  bouyer 			}
    225   1.1  bouyer 		/* FALLTHROUH */
    226   1.1  bouyer 		default:
    227   1.1  bouyer 			/*
    228   1.1  bouyer  			 * hum, we got more than what we can handle, shoudn't
    229   1.1  bouyer 			 * happen. Reject, and stay async
    230   1.1  bouyer 			 */
    231   1.9  bouyer 			siop_target->flags &= ~TARF_ISWIDE;
    232   1.1  bouyer 			siop_target->status = TARST_OK;
    233  1.14  bouyer 			siop_target->offset = siop_target->period = 0;
    234  1.14  bouyer 			siop_update_xfer_mode(sc, target);
    235   1.1  bouyer 			printf("%s: rejecting invalid wide negotiation from "
    236   1.1  bouyer 			    "target %d (%d)\n", sc->sc_dev.dv_xname, target,
    237   1.9  bouyer 			    tables->msg_in[3]);
    238   1.9  bouyer 			tables->t_msgout.count= htole32(1);
    239   1.9  bouyer 			tables->msg_out[0] = MSG_MESSAGE_REJECT;
    240   1.1  bouyer 			return SIOP_NEG_MSGOUT;
    241   1.1  bouyer 		}
    242   1.9  bouyer 		tables->id = htole32(sc->targets[target]->id);
    243   1.1  bouyer 		bus_space_write_1(sc->sc_rt, sc->sc_rh,
    244   1.1  bouyer 		    SIOP_SCNTL3,
    245   1.1  bouyer 		    (sc->targets[target]->id >> 24) & 0xff);
    246   1.1  bouyer 		/* we now need to do sync */
    247   1.9  bouyer 		if (siop_target->flags & TARF_SYNC) {
    248   1.6  bouyer 			siop_target->status = TARST_SYNC_NEG;
    249  1.10  bouyer 			siop_sdtr_msg(siop_cmd, 0, sc->minsync, sc->maxoff);
    250   1.6  bouyer 			return SIOP_NEG_MSGOUT;
    251   1.6  bouyer 		} else {
    252   1.6  bouyer 			siop_target->status = TARST_OK;
    253  1.14  bouyer 			siop_update_xfer_mode(sc, target);
    254   1.6  bouyer 			return SIOP_NEG_ACK;
    255   1.6  bouyer 		}
    256   1.1  bouyer 	} else {
    257   1.1  bouyer 		/* target initiated wide negotiation */
    258   1.9  bouyer 		if (tables->msg_in[3] >= MSG_EXT_WDTR_BUS_16_BIT
    259   1.9  bouyer 		    && (siop_target->flags & TARF_WIDE)) {
    260   1.9  bouyer 			siop_target->flags |= TARF_ISWIDE;
    261   1.1  bouyer 			sc->targets[target]->id |= SCNTL3_EWS << 24;
    262   1.1  bouyer 		} else {
    263   1.9  bouyer 			siop_target->flags &= ~TARF_ISWIDE;
    264   1.1  bouyer 			sc->targets[target]->id &= ~(SCNTL3_EWS << 24);
    265   1.1  bouyer 		}
    266   1.9  bouyer 		tables->id = htole32(sc->targets[target]->id);
    267   1.1  bouyer 		bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_SCNTL3,
    268   1.1  bouyer 		    (sc->targets[target]->id >> 24) & 0xff);
    269   1.1  bouyer 		/*
    270   1.1  bouyer 		 * we did reset wide parameters, so fall back to async,
    271   1.8  bouyer 		 * but don't schedule a sync neg, target should initiate it
    272   1.1  bouyer 		 */
    273   1.1  bouyer 		siop_target->status = TARST_OK;
    274  1.14  bouyer 		siop_target->offset = siop_target->period = 0;
    275  1.14  bouyer 		siop_update_xfer_mode(sc, target);
    276  1.10  bouyer 		siop_wdtr_msg(siop_cmd, 0, (siop_target->flags & TARF_ISWIDE) ?
    277  1.10  bouyer 		    MSG_EXT_WDTR_BUS_16_BIT : MSG_EXT_WDTR_BUS_8_BIT);
    278   1.1  bouyer 		return SIOP_NEG_MSGOUT;
    279   1.1  bouyer 	}
    280   1.1  bouyer }
    281   1.1  bouyer 
    282   1.1  bouyer int
    283   1.1  bouyer siop_sdtr_neg(siop_cmd)
    284  1.17  bouyer 	struct siop_common_cmd *siop_cmd;
    285   1.1  bouyer {
    286  1.17  bouyer 	struct siop_common_softc *sc = siop_cmd->siop_sc;
    287  1.17  bouyer 	struct siop_common_target *siop_target = siop_cmd->siop_target;
    288  1.14  bouyer 	int target = siop_cmd->xs->xs_periph->periph_target;
    289   1.1  bouyer 	int sync, offset, i;
    290   1.1  bouyer 	int send_msgout = 0;
    291  1.17  bouyer 	struct siop_common_xfer *tables = siop_cmd->siop_tables;
    292   1.1  bouyer 
    293   1.9  bouyer 	sync = tables->msg_in[3];
    294   1.9  bouyer 	offset = tables->msg_in[4];
    295   1.1  bouyer 
    296   1.1  bouyer 	if (siop_target->status == TARST_SYNC_NEG) {
    297   1.1  bouyer 		/* we initiated sync negotiation */
    298   1.1  bouyer 		siop_target->status = TARST_OK;
    299   1.1  bouyer #ifdef DEBUG
    300   1.1  bouyer 		printf("sdtr: sync %d offset %d\n", sync, offset);
    301   1.1  bouyer #endif
    302   1.1  bouyer 		if (offset > sc->maxoff || sync < sc->minsync ||
    303   1.1  bouyer 			sync > sc->maxsync)
    304   1.1  bouyer 			goto reject;
    305   1.1  bouyer 		for (i = 0; i < sizeof(scf_period) / sizeof(scf_period[0]);
    306   1.1  bouyer 		    i++) {
    307   1.1  bouyer 			if (sc->clock_period != scf_period[i].clock)
    308   1.1  bouyer 				continue;
    309   1.1  bouyer 			if (scf_period[i].period == sync) {
    310   1.1  bouyer 				/* ok, found it. we now are sync. */
    311  1.14  bouyer 				siop_target->offset = offset;
    312  1.14  bouyer 				siop_target->period = sync;
    313   1.1  bouyer 				sc->targets[target]->id &=
    314   1.1  bouyer 				    ~(SCNTL3_SCF_MASK << 24);
    315   1.1  bouyer 				sc->targets[target]->id |= scf_period[i].scf
    316   1.1  bouyer 				    << (24 + SCNTL3_SCF_SHIFT);
    317   1.1  bouyer 				if (sync < 25) /* Ultra */
    318   1.1  bouyer 					sc->targets[target]->id |=
    319   1.1  bouyer 					    SCNTL3_ULTRA << 24;
    320   1.1  bouyer 				else
    321   1.1  bouyer 					sc->targets[target]->id &=
    322   1.1  bouyer 					    ~(SCNTL3_ULTRA << 24);
    323   1.1  bouyer 				sc->targets[target]->id &=
    324   1.7  bouyer 				    ~(SXFER_MO_MASK << 8);
    325   1.1  bouyer 				sc->targets[target]->id |=
    326   1.7  bouyer 				    (offset & SXFER_MO_MASK) << 8;
    327   1.1  bouyer 				goto end;
    328   1.1  bouyer 			}
    329   1.1  bouyer 		}
    330   1.1  bouyer 		/*
    331   1.1  bouyer 		 * we didn't find it in our table, do async and send reject
    332   1.1  bouyer 		 * msg
    333   1.1  bouyer 		 */
    334   1.1  bouyer reject:
    335   1.1  bouyer 		send_msgout = 1;
    336   1.9  bouyer 		tables->t_msgout.count= htole32(1);
    337   1.9  bouyer 		tables->msg_out[0] = MSG_MESSAGE_REJECT;
    338   1.1  bouyer 		sc->targets[target]->id &= ~(SCNTL3_SCF_MASK << 24);
    339   1.1  bouyer 		sc->targets[target]->id &= ~(SCNTL3_ULTRA << 24);
    340   1.7  bouyer 		sc->targets[target]->id &= ~(SXFER_MO_MASK << 8);
    341  1.14  bouyer 		siop_target->offset = siop_target->period = 0;
    342   1.1  bouyer 	} else { /* target initiated sync neg */
    343   1.1  bouyer #ifdef DEBUG
    344   1.1  bouyer 		printf("sdtr (target): sync %d offset %d\n", sync, offset);
    345   1.1  bouyer #endif
    346   1.1  bouyer 		if (offset == 0 || sync > sc->maxsync) { /* async */
    347   1.1  bouyer 			goto async;
    348   1.1  bouyer 		}
    349   1.1  bouyer 		if (offset > sc->maxoff)
    350   1.1  bouyer 			offset = sc->maxoff;
    351   1.1  bouyer 		if (sync < sc->minsync)
    352   1.1  bouyer 			sync = sc->minsync;
    353   1.1  bouyer 		/* look for sync period */
    354   1.1  bouyer 		for (i = 0; i < sizeof(scf_period) / sizeof(scf_period[0]);
    355   1.1  bouyer 		    i++) {
    356   1.1  bouyer 			if (sc->clock_period != scf_period[i].clock)
    357   1.1  bouyer 				continue;
    358   1.1  bouyer 			if (scf_period[i].period == sync) {
    359   1.1  bouyer 				/* ok, found it. we now are sync. */
    360  1.14  bouyer 				siop_target->offset = offset;
    361  1.14  bouyer 				siop_target->period = sync;
    362   1.1  bouyer 				sc->targets[target]->id &=
    363   1.1  bouyer 				    ~(SCNTL3_SCF_MASK << 24);
    364   1.1  bouyer 				sc->targets[target]->id |= scf_period[i].scf
    365   1.1  bouyer 				    << (24 + SCNTL3_SCF_SHIFT);
    366   1.1  bouyer 				if (sync < 25) /* Ultra */
    367   1.1  bouyer 					sc->targets[target]->id |=
    368   1.1  bouyer 					    SCNTL3_ULTRA << 24;
    369   1.1  bouyer 				else
    370   1.1  bouyer 					sc->targets[target]->id &=
    371   1.1  bouyer 					    ~(SCNTL3_ULTRA << 24);
    372   1.1  bouyer 				sc->targets[target]->id &=
    373   1.7  bouyer 				    ~(SXFER_MO_MASK << 8);
    374   1.1  bouyer 				sc->targets[target]->id |=
    375   1.7  bouyer 				    (offset & SXFER_MO_MASK) << 8;
    376  1.10  bouyer 				siop_sdtr_msg(siop_cmd, 0, sync, offset);
    377   1.1  bouyer 				send_msgout = 1;
    378   1.1  bouyer 				goto end;
    379   1.1  bouyer 			}
    380   1.1  bouyer 		}
    381   1.1  bouyer async:
    382  1.14  bouyer 		siop_target->offset = siop_target->period = 0;
    383   1.1  bouyer 		sc->targets[target]->id &= ~(SCNTL3_SCF_MASK << 24);
    384   1.1  bouyer 		sc->targets[target]->id &= ~(SCNTL3_ULTRA << 24);
    385   1.7  bouyer 		sc->targets[target]->id &= ~(SXFER_MO_MASK << 8);
    386  1.10  bouyer 		siop_sdtr_msg(siop_cmd, 0, 0, 0);
    387   1.1  bouyer 		send_msgout = 1;
    388   1.1  bouyer 	}
    389   1.1  bouyer end:
    390  1.14  bouyer 	if (siop_target->status == TARST_OK)
    391  1.14  bouyer 		siop_update_xfer_mode(sc, target);
    392   1.1  bouyer #ifdef DEBUG
    393   1.1  bouyer 	printf("id now 0x%x\n", sc->targets[target]->id);
    394   1.1  bouyer #endif
    395   1.9  bouyer 	tables->id = htole32(sc->targets[target]->id);
    396   1.1  bouyer 	bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_SCNTL3,
    397   1.1  bouyer 	    (sc->targets[target]->id >> 24) & 0xff);
    398   1.7  bouyer 	bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_SXFER,
    399   1.1  bouyer 	    (sc->targets[target]->id >> 8) & 0xff);
    400   1.1  bouyer 	if (send_msgout) {
    401   1.1  bouyer 		return SIOP_NEG_MSGOUT;
    402   1.1  bouyer 	} else {
    403   1.1  bouyer 		return SIOP_NEG_ACK;
    404   1.1  bouyer 	}
    405   1.1  bouyer }
    406   1.1  bouyer 
    407   1.1  bouyer void
    408  1.10  bouyer siop_sdtr_msg(siop_cmd, offset, ssync, soff)
    409  1.17  bouyer 	struct siop_common_cmd *siop_cmd;
    410  1.10  bouyer 	int offset;
    411  1.10  bouyer 	int ssync, soff;
    412  1.10  bouyer {
    413  1.17  bouyer 	siop_cmd->siop_tables->msg_out[offset + 0] = MSG_EXTENDED;
    414  1.17  bouyer 	siop_cmd->siop_tables->msg_out[offset + 1] = MSG_EXT_SDTR_LEN;
    415  1.17  bouyer 	siop_cmd->siop_tables->msg_out[offset + 2] = MSG_EXT_SDTR;
    416  1.17  bouyer 	siop_cmd->siop_tables->msg_out[offset + 3] = ssync;
    417  1.17  bouyer 	siop_cmd->siop_tables->msg_out[offset + 4] = soff;
    418  1.17  bouyer 	siop_cmd->siop_tables->t_msgout.count =
    419  1.10  bouyer 	    htole32(offset + MSG_EXT_SDTR_LEN + 2);
    420  1.10  bouyer }
    421  1.10  bouyer 
    422  1.10  bouyer void
    423  1.10  bouyer siop_wdtr_msg(siop_cmd, offset, wide)
    424  1.17  bouyer 	struct siop_common_cmd *siop_cmd;
    425  1.10  bouyer 	int offset;
    426  1.10  bouyer {
    427  1.17  bouyer 	siop_cmd->siop_tables->msg_out[offset + 0] = MSG_EXTENDED;
    428  1.17  bouyer 	siop_cmd->siop_tables->msg_out[offset + 1] = MSG_EXT_WDTR_LEN;
    429  1.17  bouyer 	siop_cmd->siop_tables->msg_out[offset + 2] = MSG_EXT_WDTR;
    430  1.17  bouyer 	siop_cmd->siop_tables->msg_out[offset + 3] = wide;
    431  1.17  bouyer 	siop_cmd->siop_tables->t_msgout.count =
    432  1.10  bouyer 	    htole32(offset + MSG_EXT_WDTR_LEN + 2);
    433  1.10  bouyer }
    434  1.10  bouyer 
    435  1.10  bouyer void
    436   1.1  bouyer siop_minphys(bp)
    437   1.1  bouyer 	struct buf *bp;
    438   1.1  bouyer {
    439   1.1  bouyer 	minphys(bp);
    440   1.1  bouyer }
    441   1.1  bouyer 
    442   1.1  bouyer int
    443  1.14  bouyer siop_ioctl(chan, cmd, arg, flag, p)
    444  1.14  bouyer 	struct scsipi_channel *chan;
    445   1.1  bouyer 	u_long cmd;
    446   1.1  bouyer 	caddr_t arg;
    447   1.1  bouyer 	int flag;
    448   1.1  bouyer 	struct proc *p;
    449   1.1  bouyer {
    450  1.17  bouyer 	struct siop_common_softc *sc = (void *)chan->chan_adapter->adapt_dev;
    451   1.1  bouyer 	u_int8_t scntl1;
    452   1.1  bouyer 	int s;
    453   1.1  bouyer 
    454   1.1  bouyer 	switch (cmd) {
    455   1.1  bouyer 	case SCBUSIORESET:
    456   1.1  bouyer 		s = splbio();
    457   1.1  bouyer 		scntl1 = bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_SCNTL1);
    458   1.1  bouyer 		bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_SCNTL1,
    459   1.1  bouyer 		    scntl1 | SCNTL1_RST);
    460   1.1  bouyer 		/* minimum 25 us, more time won't hurt */
    461   1.1  bouyer 		delay(100);
    462   1.1  bouyer 		bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_SCNTL1, scntl1);
    463   1.1  bouyer 		splx(s);
    464   1.1  bouyer 		return (0);
    465   1.1  bouyer 	default:
    466   1.1  bouyer 		return (ENOTTY);
    467   1.1  bouyer 	}
    468   1.1  bouyer }
    469   1.1  bouyer 
    470   1.1  bouyer void
    471   1.1  bouyer siop_sdp(siop_cmd)
    472  1.17  bouyer 	struct siop_common_cmd *siop_cmd;
    473   1.1  bouyer {
    474   1.1  bouyer 	/* save data pointer. Handle async only for now */
    475   1.1  bouyer 	int offset, dbc, sstat;
    476  1.17  bouyer 	struct siop_common_softc *sc = siop_cmd->siop_sc;
    477   1.1  bouyer 	scr_table_t *table; /* table to patch */
    478   1.1  bouyer 
    479   1.1  bouyer 	if ((siop_cmd->xs->xs_control & (XS_CTL_DATA_OUT | XS_CTL_DATA_IN))
    480   1.1  bouyer 	    == 0)
    481   1.1  bouyer 	    return; /* no data pointers to save */
    482   1.1  bouyer 	offset = bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_SCRATCHA + 1);
    483   1.1  bouyer 	if (offset >= SIOP_NSG) {
    484   1.1  bouyer 		printf("%s: bad offset in siop_sdp (%d)\n",
    485   1.1  bouyer 		    sc->sc_dev.dv_xname, offset);
    486   1.1  bouyer 		return;
    487   1.1  bouyer 	}
    488  1.17  bouyer 	table = &siop_cmd->siop_tables->data[offset];
    489   1.1  bouyer #ifdef DEBUG_DR
    490   1.1  bouyer 	printf("sdp: offset %d count=%d addr=0x%x ", offset,
    491   1.1  bouyer 	    table->count, table->addr);
    492   1.1  bouyer #endif
    493   1.1  bouyer 	dbc = bus_space_read_4(sc->sc_rt, sc->sc_rh, SIOP_DBC) & 0x00ffffff;
    494   1.1  bouyer 	if (siop_cmd->xs->xs_control & XS_CTL_DATA_OUT) {
    495  1.13  bouyer 		if (sc->features & SF_CHIP_DFBC) {
    496  1.13  bouyer 			dbc +=
    497  1.13  bouyer 			    bus_space_read_2(sc->sc_rt, sc->sc_rh, SIOP_DFBC);
    498   1.1  bouyer 		} else {
    499  1.13  bouyer 			/* need to account stale data in FIFO */
    500  1.13  bouyer 			int dfifo =
    501  1.13  bouyer 			    bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_DFIFO);
    502  1.13  bouyer 			if (sc->features & SF_CHIP_FIFO) {
    503  1.13  bouyer 				dfifo |= (bus_space_read_1(sc->sc_rt, sc->sc_rh,
    504  1.13  bouyer 				    SIOP_CTEST5) & CTEST5_BOMASK) << 8;
    505  1.13  bouyer 				dbc += (dfifo - (dbc & 0x3ff)) & 0x3ff;
    506  1.13  bouyer 			} else {
    507  1.13  bouyer 				dbc += (dfifo - (dbc & 0x7f)) & 0x7f;
    508  1.13  bouyer 			}
    509   1.1  bouyer 		}
    510   1.1  bouyer 		sstat = bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_SSTAT0);
    511   1.1  bouyer 		if (sstat & SSTAT0_OLF)
    512   1.1  bouyer 			dbc++;
    513  1.13  bouyer 		if ((sstat & SSTAT0_ORF) && (sc->features & SF_CHIP_DFBC) == 0)
    514   1.1  bouyer 			dbc++;
    515   1.9  bouyer 		if (siop_cmd->siop_target->flags & TARF_ISWIDE) {
    516   1.1  bouyer 			sstat = bus_space_read_1(sc->sc_rt, sc->sc_rh,
    517   1.1  bouyer 			    SIOP_SSTAT2);
    518   1.1  bouyer 			if (sstat & SSTAT2_OLF1)
    519   1.1  bouyer 				dbc++;
    520  1.13  bouyer 			if ((sstat & SSTAT2_ORF1) &&
    521  1.13  bouyer 			    (sc->features & SF_CHIP_DFBC) == 0)
    522   1.1  bouyer 				dbc++;
    523   1.1  bouyer 		}
    524   1.1  bouyer 		/* clear the FIFO */
    525   1.1  bouyer 		bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_CTEST3,
    526   1.1  bouyer 		    bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_CTEST3) |
    527   1.1  bouyer 		    CTEST3_CLF);
    528   1.1  bouyer 	}
    529   1.1  bouyer 	table->addr =
    530   1.1  bouyer 	    htole32(le32toh(table->addr) + le32toh(table->count) - dbc);
    531   1.1  bouyer 	table->count = htole32(dbc);
    532   1.1  bouyer #ifdef DEBUG_DR
    533   1.1  bouyer 	printf("now count=%d addr=0x%x\n", table->count, table->addr);
    534   1.1  bouyer #endif
    535   1.1  bouyer }
    536   1.1  bouyer 
    537   1.1  bouyer void
    538   1.1  bouyer siop_clearfifo(sc)
    539  1.17  bouyer 	struct siop_common_softc *sc;
    540   1.1  bouyer {
    541   1.1  bouyer 	int timeout = 0;
    542   1.1  bouyer 	int ctest3 = bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_CTEST3);
    543   1.1  bouyer 
    544   1.1  bouyer #ifdef DEBUG_INTR
    545   1.1  bouyer 	printf("DMA fifo not empty !\n");
    546   1.1  bouyer #endif
    547   1.1  bouyer 	bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_CTEST3,
    548   1.1  bouyer 	    ctest3 | CTEST3_CLF);
    549   1.1  bouyer 	while ((bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_CTEST3) &
    550   1.1  bouyer 	    CTEST3_CLF) != 0) {
    551   1.1  bouyer 		delay(1);
    552   1.1  bouyer 		if (++timeout > 1000) {
    553   1.1  bouyer 			printf("clear fifo failed\n");
    554   1.1  bouyer 			bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_CTEST3,
    555   1.1  bouyer 			    bus_space_read_1(sc->sc_rt, sc->sc_rh,
    556   1.1  bouyer 			    SIOP_CTEST3) & ~CTEST3_CLF);
    557   1.1  bouyer 			return;
    558   1.1  bouyer 		}
    559   1.1  bouyer 	}
    560   1.3  bouyer }
    561   1.3  bouyer 
    562   1.3  bouyer int
    563   1.3  bouyer siop_modechange(sc)
    564  1.17  bouyer 	struct siop_common_softc *sc;
    565   1.3  bouyer {
    566   1.3  bouyer 	int retry;
    567   1.3  bouyer 	int sist0, sist1, stest2, stest4;
    568   1.3  bouyer 	for (retry = 0; retry < 5; retry++) {
    569   1.3  bouyer 		/*
    570   1.3  bouyer 		 * datasheet says to wait 100ms and re-read SIST1,
    571  1.14  bouyer 		 * to check that DIFFSENSE is stable.
    572   1.3  bouyer 		 * We may delay() 5 times for  100ms at interrupt time;
    573   1.3  bouyer 		 * hopefully this will not happen often.
    574   1.3  bouyer 		 */
    575   1.3  bouyer 		delay(100000);
    576   1.3  bouyer 		sist0 = bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_SIST0);
    577   1.3  bouyer 		sist1 = bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_SIST1);
    578   1.3  bouyer 		if (sist1 & SIEN1_SBMC)
    579   1.3  bouyer 			continue; /* we got an irq again */
    580   1.3  bouyer 		stest4 = bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_STEST4) &
    581   1.3  bouyer 		    STEST4_MODE_MASK;
    582   1.3  bouyer 		stest2 = bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_STEST2);
    583   1.3  bouyer 		switch(stest4) {
    584   1.3  bouyer 		case STEST4_MODE_DIF:
    585   1.3  bouyer 			printf("%s: switching to differential mode\n",
    586   1.3  bouyer 			    sc->sc_dev.dv_xname);
    587   1.3  bouyer 			bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_STEST2,
    588   1.3  bouyer 			    stest2 | STEST2_DIF);
    589   1.3  bouyer 			break;
    590   1.3  bouyer 		case STEST4_MODE_SE:
    591   1.3  bouyer 			printf("%s: switching to single-ended mode\n",
    592   1.3  bouyer 			    sc->sc_dev.dv_xname);
    593   1.3  bouyer 			bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_STEST2,
    594   1.3  bouyer 			    stest2 & ~STEST2_DIF);
    595   1.3  bouyer 			break;
    596   1.3  bouyer 		case STEST4_MODE_LVD:
    597   1.3  bouyer 			printf("%s: switching to LVD mode\n",
    598   1.3  bouyer 			    sc->sc_dev.dv_xname);
    599   1.3  bouyer 			bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_STEST2,
    600   1.3  bouyer 			    stest2 & ~STEST2_DIF);
    601   1.3  bouyer 			break;
    602   1.3  bouyer 		default:
    603   1.3  bouyer 			printf("%s: invalid SCSI mode 0x%x\n",
    604   1.3  bouyer 			    sc->sc_dev.dv_xname, stest4);
    605   1.3  bouyer 			return 0;
    606   1.3  bouyer 		}
    607   1.3  bouyer 		bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_STEST0,
    608   1.3  bouyer 		    stest4 >> 2);
    609   1.3  bouyer 		return 1;
    610   1.3  bouyer 	}
    611   1.3  bouyer 	printf("%s: timeout waiting for DIFFSENSE to stabilise\n",
    612   1.3  bouyer 	    sc->sc_dev.dv_xname);
    613   1.3  bouyer 	return 0;
    614   1.6  bouyer }
    615   1.6  bouyer 
    616   1.6  bouyer void
    617   1.6  bouyer siop_resetbus(sc)
    618  1.17  bouyer 	struct siop_common_softc *sc;
    619   1.6  bouyer {
    620   1.6  bouyer 	int scntl1;
    621   1.6  bouyer 	scntl1 = bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_SCNTL1);
    622   1.6  bouyer 	bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_SCNTL1,
    623   1.6  bouyer 	    scntl1 | SCNTL1_RST);
    624   1.6  bouyer 	/* minimum 25 us, more time won't hurt */
    625   1.6  bouyer 	delay(100);
    626   1.6  bouyer 	bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_SCNTL1, scntl1);
    627  1.17  bouyer }
    628  1.17  bouyer 
    629  1.17  bouyer void
    630  1.17  bouyer siop_update_xfer_mode(sc, target)
    631  1.17  bouyer 	struct siop_common_softc *sc;
    632  1.17  bouyer 	int target;
    633  1.17  bouyer {
    634  1.17  bouyer 	struct siop_common_target *siop_target = sc->targets[target];
    635  1.17  bouyer 	struct scsipi_xfer_mode xm;
    636  1.17  bouyer 
    637  1.17  bouyer 	xm.xm_target = target;
    638  1.17  bouyer 	xm.xm_mode = 0;
    639  1.17  bouyer 	xm.xm_period = 0;
    640  1.17  bouyer 	xm.xm_offset = 0;
    641  1.17  bouyer 
    642  1.17  bouyer 	if (siop_target->flags & TARF_ISWIDE)
    643  1.17  bouyer 		xm.xm_mode |= PERIPH_CAP_WIDE16;
    644  1.17  bouyer 	if (siop_target->period) {
    645  1.17  bouyer 		xm.xm_period = siop_target->period;
    646  1.17  bouyer 		xm.xm_offset = siop_target->offset;
    647  1.17  bouyer 		xm.xm_mode |= PERIPH_CAP_SYNC;
    648  1.17  bouyer 	}
    649  1.17  bouyer 	if (siop_target->flags & TARF_TAG)
    650  1.17  bouyer 		xm.xm_mode |= PERIPH_CAP_TQING;
    651  1.17  bouyer 	scsipi_async_event(&sc->sc_chan, ASYNC_EVENT_XFER_MODE, &xm);
    652   1.1  bouyer }
    653