Home | History | Annotate | Line # | Download | only in ic
siop_common.c revision 1.14.2.1
      1  1.14.2.1  thorpej /*	$NetBSD: siop_common.c,v 1.14.2.1 2002/01/10 19:55:02 thorpej 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.14.2.1  thorpej 
     35  1.14.2.1  thorpej #include <sys/cdefs.h>
     36  1.14.2.1  thorpej __KERNEL_RCSID(0, "$NetBSD: siop_common.c,v 1.14.2.1 2002/01/10 19:55:02 thorpej 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.h>
     57       1.1   bouyer #include <dev/ic/siopvar_common.h>
     58       1.1   bouyer 
     59       1.2   bouyer #undef DEBUG
     60       1.2   bouyer #undef DEBUG_DR
     61       1.1   bouyer 
     62       1.1   bouyer void
     63       1.1   bouyer siop_common_reset(sc)
     64       1.1   bouyer 	struct siop_softc *sc;
     65       1.1   bouyer {
     66       1.1   bouyer 	u_int32_t stest3;
     67       1.1   bouyer 
     68       1.1   bouyer 	/* reset the chip */
     69       1.1   bouyer 	bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_ISTAT, ISTAT_SRST);
     70       1.1   bouyer 	delay(1000);
     71       1.1   bouyer 	bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_ISTAT, 0);
     72       1.1   bouyer 
     73       1.1   bouyer 	/* init registers */
     74       1.1   bouyer 	bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_SCNTL0,
     75       1.1   bouyer 	    SCNTL0_ARB_MASK | SCNTL0_EPC | SCNTL0_AAP);
     76       1.1   bouyer 	bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_SCNTL1, 0);
     77       1.1   bouyer 	bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_SCNTL3, sc->clock_div);
     78       1.7   bouyer 	bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_SXFER, 0);
     79       1.1   bouyer 	bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_DIEN, 0xff);
     80       1.1   bouyer 	bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_SIEN0,
     81       1.1   bouyer 	    0xff & ~(SIEN0_CMP | SIEN0_SEL | SIEN0_RSL));
     82       1.1   bouyer 	bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_SIEN1,
     83       1.1   bouyer 	    0xff & ~(SIEN1_HTH | SIEN1_GEN));
     84       1.1   bouyer 	bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_STEST2, 0);
     85       1.1   bouyer 	bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_STEST3, STEST3_TE);
     86       1.1   bouyer 	bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_STIME0,
     87       1.1   bouyer 	    (0xb << STIME0_SEL_SHIFT));
     88       1.1   bouyer 	bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_SCID,
     89      1.14   bouyer 	    sc->sc_chan.chan_id | SCID_RRE);
     90       1.1   bouyer 	bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_RESPID0,
     91      1.14   bouyer 	    1 << sc->sc_chan.chan_id);
     92       1.1   bouyer 	bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_DCNTL,
     93       1.1   bouyer 	    (sc->features & SF_CHIP_PF) ? DCNTL_COM | DCNTL_PFEN : DCNTL_COM);
     94       1.1   bouyer 
     95       1.1   bouyer 	/* enable clock doubler or quadruler if appropriate */
     96       1.1   bouyer 	if (sc->features & (SF_CHIP_DBLR | SF_CHIP_QUAD)) {
     97       1.1   bouyer 		stest3 = bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_STEST3);
     98       1.1   bouyer 		bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_STEST1,
     99       1.1   bouyer 		    STEST1_DBLEN);
    100       1.1   bouyer 		if (sc->features & SF_CHIP_QUAD) {
    101       1.1   bouyer 			/* wait for PPL to lock */
    102       1.1   bouyer 			while ((bus_space_read_1(sc->sc_rt, sc->sc_rh,
    103       1.1   bouyer 			    SIOP_STEST4) & STEST4_LOCK) == 0)
    104       1.1   bouyer 				delay(10);
    105       1.1   bouyer 		} else {
    106       1.1   bouyer 			/* data sheet says 20us - more won't hurt */
    107       1.1   bouyer 			delay(100);
    108       1.1   bouyer 		}
    109       1.1   bouyer 		/* halt scsi clock, select doubler/quad, restart clock */
    110       1.1   bouyer 		bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_STEST3,
    111       1.1   bouyer 		    stest3 | STEST3_HSC);
    112       1.1   bouyer 		bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_STEST1,
    113       1.1   bouyer 		    STEST1_DBLEN | STEST1_DBLSEL);
    114       1.1   bouyer 		bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_STEST3, stest3);
    115       1.1   bouyer 	} else {
    116       1.1   bouyer 		bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_STEST1, 0);
    117       1.1   bouyer 	}
    118       1.1   bouyer 	if (sc->features & SF_CHIP_FIFO)
    119       1.1   bouyer 		bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_CTEST5,
    120       1.1   bouyer 		    bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_CTEST5) |
    121       1.1   bouyer 		    CTEST5_DFS);
    122       1.1   bouyer 
    123       1.1   bouyer 	sc->sc_reset(sc);
    124       1.1   bouyer }
    125       1.1   bouyer 
    126      1.10   bouyer /* prepare tables before sending a cmd */
    127      1.10   bouyer void
    128      1.10   bouyer siop_setuptables(siop_cmd)
    129      1.10   bouyer 	struct siop_cmd *siop_cmd;
    130      1.10   bouyer {
    131      1.10   bouyer 	int i;
    132      1.10   bouyer 	struct siop_softc *sc = siop_cmd->siop_sc;
    133      1.10   bouyer 	struct scsipi_xfer *xs = siop_cmd->xs;
    134      1.14   bouyer 	int target = xs->xs_periph->periph_target;
    135      1.14   bouyer 	int lun = xs->xs_periph->periph_lun;
    136      1.14   bouyer 	int msgoffset = 1;
    137      1.10   bouyer 
    138      1.10   bouyer 	siop_cmd->siop_tables.id = htole32(sc->targets[target]->id);
    139      1.10   bouyer 	memset(siop_cmd->siop_tables.msg_out, 0, 8);
    140      1.14   bouyer 	/* request sense doesn't disconnect */
    141      1.14   bouyer 	if (xs->xs_control & XS_CTL_REQSENSE)
    142      1.14   bouyer 		siop_cmd->siop_tables.msg_out[0] = MSG_IDENTIFY(lun, 0);
    143      1.14   bouyer 	else
    144      1.12   bouyer 		siop_cmd->siop_tables.msg_out[0] = MSG_IDENTIFY(lun, 1);
    145      1.10   bouyer 	siop_cmd->siop_tables.t_msgout.count= htole32(1);
    146      1.14   bouyer 	if (xs->xs_tag_type != 0) {
    147      1.14   bouyer 		if ((sc->targets[target]->flags & TARF_TAG) == 0) {
    148      1.14   bouyer 			scsipi_printaddr(xs->xs_periph);
    149      1.14   bouyer 			printf(": tagged command type %d id %d\n",
    150      1.14   bouyer 			    siop_cmd->xs->xs_tag_type, siop_cmd->xs->xs_tag_id);
    151      1.14   bouyer 			panic("tagged command for non-tagging device\n");
    152      1.14   bouyer 		}
    153      1.14   bouyer 		siop_cmd->flags |= CMDFL_TAG;
    154      1.14   bouyer 		siop_cmd->siop_tables.msg_out[1] = siop_cmd->xs->xs_tag_type;
    155      1.14   bouyer 		siop_cmd->siop_tables.msg_out[2] = siop_cmd->xs->xs_tag_id + 1;
    156      1.14   bouyer 		siop_cmd->siop_tables.t_msgout.count = htole32(3);
    157      1.14   bouyer 		msgoffset = 3;
    158      1.14   bouyer 		siop_cmd->tag = siop_cmd->xs->xs_tag_id + 1;
    159      1.14   bouyer 	} else
    160      1.14   bouyer 		siop_cmd->tag = 0;
    161      1.10   bouyer 	if (sc->targets[target]->status == TARST_ASYNC) {
    162      1.10   bouyer 		if (sc->targets[target]->flags & TARF_WIDE) {
    163      1.10   bouyer 			sc->targets[target]->status = TARST_WIDE_NEG;
    164      1.14   bouyer 			siop_wdtr_msg(siop_cmd, msgoffset,
    165      1.14   bouyer 			    MSG_EXT_WDTR_BUS_16_BIT);
    166      1.10   bouyer 		} else if (sc->targets[target]->flags & TARF_SYNC) {
    167      1.10   bouyer 			sc->targets[target]->status = TARST_SYNC_NEG;
    168      1.14   bouyer 			siop_sdtr_msg(siop_cmd, msgoffset,
    169      1.14   bouyer 			    sc->minsync, sc->maxoff);
    170      1.10   bouyer 		} else {
    171      1.10   bouyer 			sc->targets[target]->status = TARST_OK;
    172      1.14   bouyer 			siop_update_xfer_mode(sc, target);
    173      1.10   bouyer 		}
    174      1.10   bouyer 	}
    175      1.14   bouyer 	if (xs->xs_tag_type != 0 && (siop_cmd->flags & CMDFL_TAG) == 0)
    176      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);
    177      1.11   bouyer 	siop_cmd->siop_tables.status =
    178      1.11   bouyer 	    htole32(SCSI_SIOP_NOSTATUS); /* set invalid status */
    179      1.10   bouyer 
    180      1.10   bouyer 	siop_cmd->siop_tables.cmd.count =
    181      1.10   bouyer 	    htole32(siop_cmd->dmamap_cmd->dm_segs[0].ds_len);
    182      1.10   bouyer 	siop_cmd->siop_tables.cmd.addr =
    183      1.10   bouyer 	    htole32(siop_cmd->dmamap_cmd->dm_segs[0].ds_addr);
    184      1.14   bouyer 	if (xs->xs_control & (XS_CTL_DATA_IN | XS_CTL_DATA_OUT)) {
    185      1.10   bouyer 		for (i = 0; i < siop_cmd->dmamap_data->dm_nsegs; i++) {
    186      1.10   bouyer 			siop_cmd->siop_tables.data[i].count =
    187      1.10   bouyer 			    htole32(siop_cmd->dmamap_data->dm_segs[i].ds_len);
    188      1.10   bouyer 			siop_cmd->siop_tables.data[i].addr =
    189      1.10   bouyer 			    htole32(siop_cmd->dmamap_data->dm_segs[i].ds_addr);
    190      1.10   bouyer 		}
    191      1.10   bouyer 	}
    192      1.10   bouyer 	siop_table_sync(siop_cmd, BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
    193      1.10   bouyer }
    194      1.10   bouyer 
    195       1.1   bouyer int
    196       1.1   bouyer siop_wdtr_neg(siop_cmd)
    197       1.1   bouyer 	struct siop_cmd *siop_cmd;
    198       1.1   bouyer {
    199       1.9   bouyer 	struct siop_softc *sc = siop_cmd->siop_sc;
    200       1.1   bouyer 	struct siop_target *siop_target = siop_cmd->siop_target;
    201      1.14   bouyer 	int target = siop_cmd->xs->xs_periph->periph_target;
    202       1.9   bouyer 	struct siop_xfer_common *tables = &siop_cmd->siop_xfer->tables;
    203       1.1   bouyer 
    204       1.1   bouyer 	if (siop_target->status == TARST_WIDE_NEG) {
    205       1.1   bouyer 		/* we initiated wide negotiation */
    206       1.9   bouyer 		switch (tables->msg_in[3]) {
    207       1.1   bouyer 		case MSG_EXT_WDTR_BUS_8_BIT:
    208       1.9   bouyer 			siop_target->flags &= ~TARF_ISWIDE;
    209       1.1   bouyer 			sc->targets[target]->id &= ~(SCNTL3_EWS << 24);
    210       1.1   bouyer 			break;
    211       1.1   bouyer 		case MSG_EXT_WDTR_BUS_16_BIT:
    212       1.9   bouyer 			if (siop_target->flags & TARF_WIDE) {
    213       1.9   bouyer 				siop_target->flags |= TARF_ISWIDE;
    214       1.1   bouyer 				sc->targets[target]->id |= (SCNTL3_EWS << 24);
    215       1.1   bouyer 				break;
    216       1.1   bouyer 			}
    217       1.1   bouyer 		/* FALLTHROUH */
    218       1.1   bouyer 		default:
    219       1.1   bouyer 			/*
    220       1.1   bouyer  			 * hum, we got more than what we can handle, shoudn't
    221       1.1   bouyer 			 * happen. Reject, and stay async
    222       1.1   bouyer 			 */
    223       1.9   bouyer 			siop_target->flags &= ~TARF_ISWIDE;
    224       1.1   bouyer 			siop_target->status = TARST_OK;
    225      1.14   bouyer 			siop_target->offset = siop_target->period = 0;
    226      1.14   bouyer 			siop_update_xfer_mode(sc, target);
    227       1.1   bouyer 			printf("%s: rejecting invalid wide negotiation from "
    228       1.1   bouyer 			    "target %d (%d)\n", sc->sc_dev.dv_xname, target,
    229       1.9   bouyer 			    tables->msg_in[3]);
    230       1.9   bouyer 			tables->t_msgout.count= htole32(1);
    231       1.9   bouyer 			tables->msg_out[0] = MSG_MESSAGE_REJECT;
    232       1.1   bouyer 			return SIOP_NEG_MSGOUT;
    233       1.1   bouyer 		}
    234       1.9   bouyer 		tables->id = htole32(sc->targets[target]->id);
    235       1.1   bouyer 		bus_space_write_1(sc->sc_rt, sc->sc_rh,
    236       1.1   bouyer 		    SIOP_SCNTL3,
    237       1.1   bouyer 		    (sc->targets[target]->id >> 24) & 0xff);
    238       1.1   bouyer 		/* we now need to do sync */
    239       1.9   bouyer 		if (siop_target->flags & TARF_SYNC) {
    240       1.6   bouyer 			siop_target->status = TARST_SYNC_NEG;
    241      1.10   bouyer 			siop_sdtr_msg(siop_cmd, 0, sc->minsync, sc->maxoff);
    242       1.6   bouyer 			return SIOP_NEG_MSGOUT;
    243       1.6   bouyer 		} else {
    244       1.6   bouyer 			siop_target->status = TARST_OK;
    245      1.14   bouyer 			siop_update_xfer_mode(sc, target);
    246       1.6   bouyer 			return SIOP_NEG_ACK;
    247       1.6   bouyer 		}
    248       1.1   bouyer 	} else {
    249       1.1   bouyer 		/* target initiated wide negotiation */
    250       1.9   bouyer 		if (tables->msg_in[3] >= MSG_EXT_WDTR_BUS_16_BIT
    251       1.9   bouyer 		    && (siop_target->flags & TARF_WIDE)) {
    252       1.9   bouyer 			siop_target->flags |= TARF_ISWIDE;
    253       1.1   bouyer 			sc->targets[target]->id |= SCNTL3_EWS << 24;
    254       1.1   bouyer 		} else {
    255       1.9   bouyer 			siop_target->flags &= ~TARF_ISWIDE;
    256       1.1   bouyer 			sc->targets[target]->id &= ~(SCNTL3_EWS << 24);
    257       1.1   bouyer 		}
    258       1.9   bouyer 		tables->id = htole32(sc->targets[target]->id);
    259       1.1   bouyer 		bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_SCNTL3,
    260       1.1   bouyer 		    (sc->targets[target]->id >> 24) & 0xff);
    261       1.1   bouyer 		/*
    262       1.1   bouyer 		 * we did reset wide parameters, so fall back to async,
    263       1.8   bouyer 		 * but don't schedule a sync neg, target should initiate it
    264       1.1   bouyer 		 */
    265       1.1   bouyer 		siop_target->status = TARST_OK;
    266      1.14   bouyer 		siop_target->offset = siop_target->period = 0;
    267      1.14   bouyer 		siop_update_xfer_mode(sc, target);
    268      1.10   bouyer 		siop_wdtr_msg(siop_cmd, 0, (siop_target->flags & TARF_ISWIDE) ?
    269      1.10   bouyer 		    MSG_EXT_WDTR_BUS_16_BIT : MSG_EXT_WDTR_BUS_8_BIT);
    270       1.1   bouyer 		return SIOP_NEG_MSGOUT;
    271       1.1   bouyer 	}
    272       1.1   bouyer }
    273       1.1   bouyer 
    274       1.1   bouyer int
    275       1.1   bouyer siop_sdtr_neg(siop_cmd)
    276       1.1   bouyer 	struct siop_cmd *siop_cmd;
    277       1.1   bouyer {
    278       1.9   bouyer 	struct siop_softc *sc = siop_cmd->siop_sc;
    279       1.1   bouyer 	struct siop_target *siop_target = siop_cmd->siop_target;
    280      1.14   bouyer 	int target = siop_cmd->xs->xs_periph->periph_target;
    281       1.1   bouyer 	int sync, offset, i;
    282       1.1   bouyer 	int send_msgout = 0;
    283       1.9   bouyer 	struct siop_xfer_common *tables = &siop_cmd->siop_xfer->tables;
    284       1.1   bouyer 
    285       1.9   bouyer 	sync = tables->msg_in[3];
    286       1.9   bouyer 	offset = tables->msg_in[4];
    287       1.1   bouyer 
    288       1.1   bouyer 	if (siop_target->status == TARST_SYNC_NEG) {
    289       1.1   bouyer 		/* we initiated sync negotiation */
    290       1.1   bouyer 		siop_target->status = TARST_OK;
    291       1.1   bouyer #ifdef DEBUG
    292       1.1   bouyer 		printf("sdtr: sync %d offset %d\n", sync, offset);
    293       1.1   bouyer #endif
    294       1.1   bouyer 		if (offset > sc->maxoff || sync < sc->minsync ||
    295       1.1   bouyer 			sync > sc->maxsync)
    296       1.1   bouyer 			goto reject;
    297       1.1   bouyer 		for (i = 0; i < sizeof(scf_period) / sizeof(scf_period[0]);
    298       1.1   bouyer 		    i++) {
    299       1.1   bouyer 			if (sc->clock_period != scf_period[i].clock)
    300       1.1   bouyer 				continue;
    301       1.1   bouyer 			if (scf_period[i].period == sync) {
    302       1.1   bouyer 				/* ok, found it. we now are sync. */
    303      1.14   bouyer 				siop_target->offset = offset;
    304      1.14   bouyer 				siop_target->period = sync;
    305       1.1   bouyer 				sc->targets[target]->id &=
    306       1.1   bouyer 				    ~(SCNTL3_SCF_MASK << 24);
    307       1.1   bouyer 				sc->targets[target]->id |= scf_period[i].scf
    308       1.1   bouyer 				    << (24 + SCNTL3_SCF_SHIFT);
    309       1.1   bouyer 				if (sync < 25) /* Ultra */
    310       1.1   bouyer 					sc->targets[target]->id |=
    311       1.1   bouyer 					    SCNTL3_ULTRA << 24;
    312       1.1   bouyer 				else
    313       1.1   bouyer 					sc->targets[target]->id &=
    314       1.1   bouyer 					    ~(SCNTL3_ULTRA << 24);
    315       1.1   bouyer 				sc->targets[target]->id &=
    316       1.7   bouyer 				    ~(SXFER_MO_MASK << 8);
    317       1.1   bouyer 				sc->targets[target]->id |=
    318       1.7   bouyer 				    (offset & SXFER_MO_MASK) << 8;
    319       1.1   bouyer 				goto end;
    320       1.1   bouyer 			}
    321       1.1   bouyer 		}
    322       1.1   bouyer 		/*
    323       1.1   bouyer 		 * we didn't find it in our table, do async and send reject
    324       1.1   bouyer 		 * msg
    325       1.1   bouyer 		 */
    326       1.1   bouyer reject:
    327       1.1   bouyer 		send_msgout = 1;
    328       1.9   bouyer 		tables->t_msgout.count= htole32(1);
    329       1.9   bouyer 		tables->msg_out[0] = MSG_MESSAGE_REJECT;
    330       1.1   bouyer 		sc->targets[target]->id &= ~(SCNTL3_SCF_MASK << 24);
    331       1.1   bouyer 		sc->targets[target]->id &= ~(SCNTL3_ULTRA << 24);
    332       1.7   bouyer 		sc->targets[target]->id &= ~(SXFER_MO_MASK << 8);
    333      1.14   bouyer 		siop_target->offset = siop_target->period = 0;
    334       1.1   bouyer 	} else { /* target initiated sync neg */
    335       1.1   bouyer #ifdef DEBUG
    336       1.1   bouyer 		printf("sdtr (target): sync %d offset %d\n", sync, offset);
    337       1.1   bouyer #endif
    338       1.1   bouyer 		if (offset == 0 || sync > sc->maxsync) { /* async */
    339       1.1   bouyer 			goto async;
    340       1.1   bouyer 		}
    341       1.1   bouyer 		if (offset > sc->maxoff)
    342       1.1   bouyer 			offset = sc->maxoff;
    343       1.1   bouyer 		if (sync < sc->minsync)
    344       1.1   bouyer 			sync = sc->minsync;
    345       1.1   bouyer 		/* look for sync period */
    346       1.1   bouyer 		for (i = 0; i < sizeof(scf_period) / sizeof(scf_period[0]);
    347       1.1   bouyer 		    i++) {
    348       1.1   bouyer 			if (sc->clock_period != scf_period[i].clock)
    349       1.1   bouyer 				continue;
    350       1.1   bouyer 			if (scf_period[i].period == sync) {
    351       1.1   bouyer 				/* ok, found it. we now are sync. */
    352      1.14   bouyer 				siop_target->offset = offset;
    353      1.14   bouyer 				siop_target->period = sync;
    354       1.1   bouyer 				sc->targets[target]->id &=
    355       1.1   bouyer 				    ~(SCNTL3_SCF_MASK << 24);
    356       1.1   bouyer 				sc->targets[target]->id |= scf_period[i].scf
    357       1.1   bouyer 				    << (24 + SCNTL3_SCF_SHIFT);
    358       1.1   bouyer 				if (sync < 25) /* Ultra */
    359       1.1   bouyer 					sc->targets[target]->id |=
    360       1.1   bouyer 					    SCNTL3_ULTRA << 24;
    361       1.1   bouyer 				else
    362       1.1   bouyer 					sc->targets[target]->id &=
    363       1.1   bouyer 					    ~(SCNTL3_ULTRA << 24);
    364       1.1   bouyer 				sc->targets[target]->id &=
    365       1.7   bouyer 				    ~(SXFER_MO_MASK << 8);
    366       1.1   bouyer 				sc->targets[target]->id |=
    367       1.7   bouyer 				    (offset & SXFER_MO_MASK) << 8;
    368      1.10   bouyer 				siop_sdtr_msg(siop_cmd, 0, sync, offset);
    369       1.1   bouyer 				send_msgout = 1;
    370       1.1   bouyer 				goto end;
    371       1.1   bouyer 			}
    372       1.1   bouyer 		}
    373       1.1   bouyer async:
    374      1.14   bouyer 		siop_target->offset = siop_target->period = 0;
    375       1.1   bouyer 		sc->targets[target]->id &= ~(SCNTL3_SCF_MASK << 24);
    376       1.1   bouyer 		sc->targets[target]->id &= ~(SCNTL3_ULTRA << 24);
    377       1.7   bouyer 		sc->targets[target]->id &= ~(SXFER_MO_MASK << 8);
    378      1.10   bouyer 		siop_sdtr_msg(siop_cmd, 0, 0, 0);
    379       1.1   bouyer 		send_msgout = 1;
    380       1.1   bouyer 	}
    381       1.1   bouyer end:
    382      1.14   bouyer 	if (siop_target->status == TARST_OK)
    383      1.14   bouyer 		siop_update_xfer_mode(sc, target);
    384       1.1   bouyer #ifdef DEBUG
    385       1.1   bouyer 	printf("id now 0x%x\n", sc->targets[target]->id);
    386       1.1   bouyer #endif
    387       1.9   bouyer 	tables->id = htole32(sc->targets[target]->id);
    388       1.1   bouyer 	bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_SCNTL3,
    389       1.1   bouyer 	    (sc->targets[target]->id >> 24) & 0xff);
    390       1.7   bouyer 	bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_SXFER,
    391       1.1   bouyer 	    (sc->targets[target]->id >> 8) & 0xff);
    392       1.1   bouyer 	if (send_msgout) {
    393       1.1   bouyer 		return SIOP_NEG_MSGOUT;
    394       1.1   bouyer 	} else {
    395       1.1   bouyer 		return SIOP_NEG_ACK;
    396       1.1   bouyer 	}
    397       1.1   bouyer }
    398       1.1   bouyer 
    399       1.1   bouyer void
    400      1.10   bouyer siop_sdtr_msg(siop_cmd, offset, ssync, soff)
    401      1.10   bouyer 	struct siop_cmd *siop_cmd;
    402      1.10   bouyer 	int offset;
    403      1.10   bouyer 	int ssync, soff;
    404      1.10   bouyer {
    405      1.10   bouyer 	siop_cmd->siop_tables.msg_out[offset + 0] = MSG_EXTENDED;
    406      1.10   bouyer 	siop_cmd->siop_tables.msg_out[offset + 1] = MSG_EXT_SDTR_LEN;
    407      1.10   bouyer 	siop_cmd->siop_tables.msg_out[offset + 2] = MSG_EXT_SDTR;
    408      1.10   bouyer 	siop_cmd->siop_tables.msg_out[offset + 3] = ssync;
    409      1.10   bouyer 	siop_cmd->siop_tables.msg_out[offset + 4] = soff;
    410      1.10   bouyer 	siop_cmd->siop_tables.t_msgout.count =
    411      1.10   bouyer 	    htole32(offset + MSG_EXT_SDTR_LEN + 2);
    412      1.10   bouyer }
    413      1.10   bouyer 
    414      1.10   bouyer void
    415      1.10   bouyer siop_wdtr_msg(siop_cmd, offset, wide)
    416      1.10   bouyer 	struct siop_cmd *siop_cmd;
    417      1.10   bouyer 	int offset;
    418      1.10   bouyer {
    419      1.10   bouyer 	siop_cmd->siop_tables.msg_out[offset + 0] = MSG_EXTENDED;
    420      1.10   bouyer 	siop_cmd->siop_tables.msg_out[offset + 1] = MSG_EXT_WDTR_LEN;
    421      1.10   bouyer 	siop_cmd->siop_tables.msg_out[offset + 2] = MSG_EXT_WDTR;
    422      1.10   bouyer 	siop_cmd->siop_tables.msg_out[offset + 3] = wide;
    423      1.10   bouyer 	siop_cmd->siop_tables.t_msgout.count =
    424      1.10   bouyer 	    htole32(offset + MSG_EXT_WDTR_LEN + 2);
    425      1.10   bouyer }
    426      1.10   bouyer 
    427      1.10   bouyer void
    428       1.1   bouyer siop_minphys(bp)
    429       1.1   bouyer 	struct buf *bp;
    430       1.1   bouyer {
    431       1.1   bouyer 	minphys(bp);
    432       1.1   bouyer }
    433       1.1   bouyer 
    434       1.1   bouyer int
    435      1.14   bouyer siop_ioctl(chan, cmd, arg, flag, p)
    436      1.14   bouyer 	struct scsipi_channel *chan;
    437       1.1   bouyer 	u_long cmd;
    438       1.1   bouyer 	caddr_t arg;
    439       1.1   bouyer 	int flag;
    440       1.1   bouyer 	struct proc *p;
    441       1.1   bouyer {
    442      1.14   bouyer 	struct siop_softc *sc = (void *)chan->chan_adapter->adapt_dev;
    443       1.1   bouyer 	u_int8_t scntl1;
    444       1.1   bouyer 	int s;
    445       1.1   bouyer 
    446       1.1   bouyer 	switch (cmd) {
    447       1.1   bouyer 	case SCBUSIORESET:
    448       1.1   bouyer 		s = splbio();
    449       1.1   bouyer 		scntl1 = bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_SCNTL1);
    450       1.1   bouyer 		bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_SCNTL1,
    451       1.1   bouyer 		    scntl1 | SCNTL1_RST);
    452       1.1   bouyer 		/* minimum 25 us, more time won't hurt */
    453       1.1   bouyer 		delay(100);
    454       1.1   bouyer 		bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_SCNTL1, scntl1);
    455       1.1   bouyer 		splx(s);
    456       1.1   bouyer 		return (0);
    457       1.1   bouyer 	default:
    458       1.1   bouyer 		return (ENOTTY);
    459       1.1   bouyer 	}
    460       1.1   bouyer }
    461       1.1   bouyer 
    462       1.1   bouyer void
    463       1.1   bouyer siop_sdp(siop_cmd)
    464       1.1   bouyer 	struct siop_cmd *siop_cmd;
    465       1.1   bouyer {
    466       1.1   bouyer 	/* save data pointer. Handle async only for now */
    467       1.1   bouyer 	int offset, dbc, sstat;
    468       1.9   bouyer 	struct siop_softc *sc = siop_cmd->siop_sc;
    469       1.1   bouyer 	scr_table_t *table; /* table to patch */
    470       1.1   bouyer 
    471       1.1   bouyer 	if ((siop_cmd->xs->xs_control & (XS_CTL_DATA_OUT | XS_CTL_DATA_IN))
    472       1.1   bouyer 	    == 0)
    473       1.1   bouyer 	    return; /* no data pointers to save */
    474       1.1   bouyer 	offset = bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_SCRATCHA + 1);
    475       1.1   bouyer 	if (offset >= SIOP_NSG) {
    476       1.1   bouyer 		printf("%s: bad offset in siop_sdp (%d)\n",
    477       1.1   bouyer 		    sc->sc_dev.dv_xname, offset);
    478       1.1   bouyer 		return;
    479       1.1   bouyer 	}
    480       1.9   bouyer 	table = &siop_cmd->siop_xfer->tables.data[offset];
    481       1.1   bouyer #ifdef DEBUG_DR
    482       1.1   bouyer 	printf("sdp: offset %d count=%d addr=0x%x ", offset,
    483       1.1   bouyer 	    table->count, table->addr);
    484       1.1   bouyer #endif
    485       1.1   bouyer 	dbc = bus_space_read_4(sc->sc_rt, sc->sc_rh, SIOP_DBC) & 0x00ffffff;
    486       1.1   bouyer 	if (siop_cmd->xs->xs_control & XS_CTL_DATA_OUT) {
    487      1.13   bouyer 		if (sc->features & SF_CHIP_DFBC) {
    488      1.13   bouyer 			dbc +=
    489      1.13   bouyer 			    bus_space_read_2(sc->sc_rt, sc->sc_rh, SIOP_DFBC);
    490       1.1   bouyer 		} else {
    491      1.13   bouyer 			/* need to account stale data in FIFO */
    492      1.13   bouyer 			int dfifo =
    493      1.13   bouyer 			    bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_DFIFO);
    494      1.13   bouyer 			if (sc->features & SF_CHIP_FIFO) {
    495      1.13   bouyer 				dfifo |= (bus_space_read_1(sc->sc_rt, sc->sc_rh,
    496      1.13   bouyer 				    SIOP_CTEST5) & CTEST5_BOMASK) << 8;
    497      1.13   bouyer 				dbc += (dfifo - (dbc & 0x3ff)) & 0x3ff;
    498      1.13   bouyer 			} else {
    499      1.13   bouyer 				dbc += (dfifo - (dbc & 0x7f)) & 0x7f;
    500      1.13   bouyer 			}
    501       1.1   bouyer 		}
    502       1.1   bouyer 		sstat = bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_SSTAT0);
    503       1.1   bouyer 		if (sstat & SSTAT0_OLF)
    504       1.1   bouyer 			dbc++;
    505      1.13   bouyer 		if ((sstat & SSTAT0_ORF) && (sc->features & SF_CHIP_DFBC) == 0)
    506       1.1   bouyer 			dbc++;
    507       1.9   bouyer 		if (siop_cmd->siop_target->flags & TARF_ISWIDE) {
    508       1.1   bouyer 			sstat = bus_space_read_1(sc->sc_rt, sc->sc_rh,
    509       1.1   bouyer 			    SIOP_SSTAT2);
    510       1.1   bouyer 			if (sstat & SSTAT2_OLF1)
    511       1.1   bouyer 				dbc++;
    512      1.13   bouyer 			if ((sstat & SSTAT2_ORF1) &&
    513      1.13   bouyer 			    (sc->features & SF_CHIP_DFBC) == 0)
    514       1.1   bouyer 				dbc++;
    515       1.1   bouyer 		}
    516       1.1   bouyer 		/* clear the FIFO */
    517       1.1   bouyer 		bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_CTEST3,
    518       1.1   bouyer 		    bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_CTEST3) |
    519       1.1   bouyer 		    CTEST3_CLF);
    520       1.1   bouyer 	}
    521       1.1   bouyer 	table->addr =
    522       1.1   bouyer 	    htole32(le32toh(table->addr) + le32toh(table->count) - dbc);
    523       1.1   bouyer 	table->count = htole32(dbc);
    524       1.1   bouyer #ifdef DEBUG_DR
    525       1.1   bouyer 	printf("now count=%d addr=0x%x\n", table->count, table->addr);
    526       1.1   bouyer #endif
    527       1.1   bouyer }
    528       1.1   bouyer 
    529       1.1   bouyer void
    530       1.1   bouyer siop_clearfifo(sc)
    531       1.1   bouyer 	struct siop_softc *sc;
    532       1.1   bouyer {
    533       1.1   bouyer 	int timeout = 0;
    534       1.1   bouyer 	int ctest3 = bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_CTEST3);
    535       1.1   bouyer 
    536       1.1   bouyer #ifdef DEBUG_INTR
    537       1.1   bouyer 	printf("DMA fifo not empty !\n");
    538       1.1   bouyer #endif
    539       1.1   bouyer 	bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_CTEST3,
    540       1.1   bouyer 	    ctest3 | CTEST3_CLF);
    541       1.1   bouyer 	while ((bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_CTEST3) &
    542       1.1   bouyer 	    CTEST3_CLF) != 0) {
    543       1.1   bouyer 		delay(1);
    544       1.1   bouyer 		if (++timeout > 1000) {
    545       1.1   bouyer 			printf("clear fifo failed\n");
    546       1.1   bouyer 			bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_CTEST3,
    547       1.1   bouyer 			    bus_space_read_1(sc->sc_rt, sc->sc_rh,
    548       1.1   bouyer 			    SIOP_CTEST3) & ~CTEST3_CLF);
    549       1.1   bouyer 			return;
    550       1.1   bouyer 		}
    551       1.1   bouyer 	}
    552       1.3   bouyer }
    553       1.3   bouyer 
    554       1.3   bouyer int
    555       1.3   bouyer siop_modechange(sc)
    556       1.3   bouyer 	struct siop_softc *sc;
    557       1.3   bouyer {
    558       1.3   bouyer 	int retry;
    559       1.3   bouyer 	int sist0, sist1, stest2, stest4;
    560       1.3   bouyer 	for (retry = 0; retry < 5; retry++) {
    561       1.3   bouyer 		/*
    562       1.3   bouyer 		 * datasheet says to wait 100ms and re-read SIST1,
    563      1.14   bouyer 		 * to check that DIFFSENSE is stable.
    564       1.3   bouyer 		 * We may delay() 5 times for  100ms at interrupt time;
    565       1.3   bouyer 		 * hopefully this will not happen often.
    566       1.3   bouyer 		 */
    567       1.3   bouyer 		delay(100000);
    568       1.3   bouyer 		sist0 = bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_SIST0);
    569       1.3   bouyer 		sist1 = bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_SIST1);
    570       1.3   bouyer 		if (sist1 & SIEN1_SBMC)
    571       1.3   bouyer 			continue; /* we got an irq again */
    572       1.3   bouyer 		stest4 = bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_STEST4) &
    573       1.3   bouyer 		    STEST4_MODE_MASK;
    574       1.3   bouyer 		stest2 = bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_STEST2);
    575       1.3   bouyer 		switch(stest4) {
    576       1.3   bouyer 		case STEST4_MODE_DIF:
    577       1.3   bouyer 			printf("%s: switching to differential mode\n",
    578       1.3   bouyer 			    sc->sc_dev.dv_xname);
    579       1.3   bouyer 			bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_STEST2,
    580       1.3   bouyer 			    stest2 | STEST2_DIF);
    581       1.3   bouyer 			break;
    582       1.3   bouyer 		case STEST4_MODE_SE:
    583       1.3   bouyer 			printf("%s: switching to single-ended mode\n",
    584       1.3   bouyer 			    sc->sc_dev.dv_xname);
    585       1.3   bouyer 			bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_STEST2,
    586       1.3   bouyer 			    stest2 & ~STEST2_DIF);
    587       1.3   bouyer 			break;
    588       1.3   bouyer 		case STEST4_MODE_LVD:
    589       1.3   bouyer 			printf("%s: switching to LVD mode\n",
    590       1.3   bouyer 			    sc->sc_dev.dv_xname);
    591       1.3   bouyer 			bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_STEST2,
    592       1.3   bouyer 			    stest2 & ~STEST2_DIF);
    593       1.3   bouyer 			break;
    594       1.3   bouyer 		default:
    595       1.3   bouyer 			printf("%s: invalid SCSI mode 0x%x\n",
    596       1.3   bouyer 			    sc->sc_dev.dv_xname, stest4);
    597       1.3   bouyer 			return 0;
    598       1.3   bouyer 		}
    599       1.3   bouyer 		bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_STEST0,
    600       1.3   bouyer 		    stest4 >> 2);
    601       1.3   bouyer 		return 1;
    602       1.3   bouyer 	}
    603       1.3   bouyer 	printf("%s: timeout waiting for DIFFSENSE to stabilise\n",
    604       1.3   bouyer 	    sc->sc_dev.dv_xname);
    605       1.3   bouyer 	return 0;
    606       1.6   bouyer }
    607       1.6   bouyer 
    608       1.6   bouyer void
    609       1.6   bouyer siop_resetbus(sc)
    610       1.6   bouyer 	struct siop_softc *sc;
    611       1.6   bouyer {
    612       1.6   bouyer 	int scntl1;
    613       1.6   bouyer 	scntl1 = bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_SCNTL1);
    614       1.6   bouyer 	bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_SCNTL1,
    615       1.6   bouyer 	    scntl1 | SCNTL1_RST);
    616       1.6   bouyer 	/* minimum 25 us, more time won't hurt */
    617       1.6   bouyer 	delay(100);
    618       1.6   bouyer 	bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_SCNTL1, scntl1);
    619       1.1   bouyer }
    620