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