Home | History | Annotate | Line # | Download | only in ic
siop_common.c revision 1.28.4.4
      1  1.28.4.4    tron /*	$NetBSD: siop_common.c,v 1.28.4.4 2005/03/17 17:49:39 tron Exp $	*/
      2       1.1  bouyer 
      3       1.1  bouyer /*
      4      1.22  bouyer  * Copyright (c) 2000, 2002 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.23  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.28.4.4    tron __KERNEL_RCSID(0, "$NetBSD: siop_common.c,v 1.28.4.4 2005/03/17 17:49:39 tron 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.22  bouyer #include <uvm/uvm_extern.h>
     47      1.22  bouyer 
     48       1.1  bouyer #include <machine/endian.h>
     49       1.1  bouyer #include <machine/bus.h>
     50       1.1  bouyer 
     51       1.1  bouyer #include <dev/scsipi/scsi_all.h>
     52       1.1  bouyer #include <dev/scsipi/scsi_message.h>
     53       1.1  bouyer #include <dev/scsipi/scsipi_all.h>
     54       1.1  bouyer 
     55       1.1  bouyer #include <dev/scsipi/scsiconf.h>
     56       1.1  bouyer 
     57       1.1  bouyer #include <dev/ic/siopreg.h>
     58       1.1  bouyer #include <dev/ic/siopvar_common.h>
     59       1.1  bouyer 
     60      1.16  bouyer #include "opt_siop.h"
     61      1.16  bouyer 
     62       1.2  bouyer #undef DEBUG
     63       1.2  bouyer #undef DEBUG_DR
     64      1.22  bouyer #undef DEBUG_NEG
     65      1.22  bouyer 
     66      1.22  bouyer int
     67      1.22  bouyer siop_common_attach(sc)
     68      1.22  bouyer 	struct siop_common_softc *sc;
     69      1.22  bouyer {
     70      1.22  bouyer 	int error, i;
     71      1.22  bouyer 	bus_dma_segment_t seg;
     72      1.22  bouyer 	int rseg;
     73      1.22  bouyer 
     74      1.22  bouyer 	/*
     75      1.22  bouyer 	 * Allocate DMA-safe memory for the script and map it.
     76      1.22  bouyer 	 */
     77      1.22  bouyer 	if ((sc->features & SF_CHIP_RAM) == 0) {
     78      1.22  bouyer 		error = bus_dmamem_alloc(sc->sc_dmat, PAGE_SIZE,
     79      1.22  bouyer 		    PAGE_SIZE, 0, &seg, 1, &rseg, BUS_DMA_NOWAIT);
     80      1.22  bouyer 		if (error) {
     81      1.22  bouyer 			printf("%s: unable to allocate script DMA memory, "
     82      1.22  bouyer 			    "error = %d\n", sc->sc_dev.dv_xname, error);
     83      1.22  bouyer 			return error;
     84      1.22  bouyer 		}
     85      1.22  bouyer 		error = bus_dmamem_map(sc->sc_dmat, &seg, rseg, PAGE_SIZE,
     86      1.22  bouyer 		    (caddr_t *)&sc->sc_script,
     87      1.22  bouyer 		    BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
     88      1.22  bouyer 		if (error) {
     89      1.22  bouyer 			printf("%s: unable to map script DMA memory, "
     90      1.22  bouyer 			    "error = %d\n", sc->sc_dev.dv_xname, error);
     91      1.22  bouyer 			return error;
     92      1.22  bouyer 		}
     93      1.22  bouyer 		error = bus_dmamap_create(sc->sc_dmat, PAGE_SIZE, 1,
     94      1.22  bouyer 		    PAGE_SIZE, 0, BUS_DMA_NOWAIT, &sc->sc_scriptdma);
     95      1.22  bouyer 		if (error) {
     96      1.22  bouyer 			printf("%s: unable to create script DMA map, "
     97      1.22  bouyer 			    "error = %d\n", sc->sc_dev.dv_xname, error);
     98      1.22  bouyer 			return error;
     99      1.22  bouyer 		}
    100      1.22  bouyer 		error = bus_dmamap_load(sc->sc_dmat, sc->sc_scriptdma,
    101      1.22  bouyer 		    sc->sc_script, PAGE_SIZE, NULL, BUS_DMA_NOWAIT);
    102      1.22  bouyer 		if (error) {
    103      1.22  bouyer 			printf("%s: unable to load script DMA map, "
    104      1.22  bouyer 			    "error = %d\n", sc->sc_dev.dv_xname, error);
    105      1.22  bouyer 			return error;
    106      1.22  bouyer 		}
    107      1.22  bouyer 		sc->sc_scriptaddr =
    108      1.22  bouyer 		    sc->sc_scriptdma->dm_segs[0].ds_addr;
    109      1.22  bouyer 		sc->ram_size = PAGE_SIZE;
    110      1.22  bouyer 	}
    111      1.22  bouyer 
    112      1.22  bouyer 	sc->sc_adapt.adapt_dev = &sc->sc_dev;
    113      1.22  bouyer 	sc->sc_adapt.adapt_nchannels = 1;
    114      1.22  bouyer 	sc->sc_adapt.adapt_openings = 0;
    115      1.22  bouyer 	sc->sc_adapt.adapt_ioctl = siop_ioctl;
    116      1.22  bouyer 	sc->sc_adapt.adapt_minphys = minphys;
    117      1.22  bouyer 
    118      1.22  bouyer 	memset(&sc->sc_chan, 0, sizeof(sc->sc_chan));
    119      1.22  bouyer 	sc->sc_chan.chan_adapter = &sc->sc_adapt;
    120      1.22  bouyer 	sc->sc_chan.chan_bustype = &scsi_bustype;
    121      1.22  bouyer 	sc->sc_chan.chan_channel = 0;
    122      1.22  bouyer 	sc->sc_chan.chan_flags = SCSIPI_CHAN_CANGROW;
    123      1.22  bouyer 	sc->sc_chan.chan_ntargets =
    124      1.22  bouyer 	    (sc->features & SF_BUS_WIDE) ? 16 : 8;
    125      1.22  bouyer 	sc->sc_chan.chan_nluns = 8;
    126      1.22  bouyer 	sc->sc_chan.chan_id =
    127      1.22  bouyer 	    bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_SCID);
    128      1.22  bouyer 	if (sc->sc_chan.chan_id == 0 ||
    129      1.22  bouyer 	    sc->sc_chan.chan_id >= sc->sc_chan.chan_ntargets)
    130      1.22  bouyer 		sc->sc_chan.chan_id = SIOP_DEFAULT_TARGET;
    131      1.22  bouyer 
    132      1.22  bouyer 	for (i = 0; i < 16; i++)
    133      1.22  bouyer 		sc->targets[i] = NULL;
    134      1.22  bouyer 
    135      1.22  bouyer 	/* find min/max sync period for this chip */
    136      1.22  bouyer 	sc->st_maxsync = 0;
    137      1.22  bouyer 	sc->dt_maxsync = 0;
    138      1.22  bouyer 	sc->st_minsync = 255;
    139      1.22  bouyer 	sc->dt_minsync = 255;
    140      1.22  bouyer 	for (i = 0; i < sizeof(scf_period) / sizeof(scf_period[0]); i++) {
    141      1.22  bouyer 		if (sc->clock_period != scf_period[i].clock)
    142      1.22  bouyer 			continue;
    143      1.22  bouyer 		if (sc->st_maxsync < scf_period[i].period)
    144      1.22  bouyer 			sc->st_maxsync = scf_period[i].period;
    145      1.22  bouyer 		if (sc->st_minsync > scf_period[i].period)
    146      1.22  bouyer 			sc->st_minsync = scf_period[i].period;
    147      1.22  bouyer 	}
    148      1.22  bouyer 	if (sc->st_maxsync == 255 || sc->st_minsync == 0)
    149      1.22  bouyer 		panic("siop: can't find my sync parameters\n");
    150      1.22  bouyer 	for (i = 0; i < sizeof(dt_scf_period) / sizeof(dt_scf_period[0]); i++) {
    151      1.22  bouyer 		if (sc->clock_period != dt_scf_period[i].clock)
    152      1.22  bouyer 			continue;
    153      1.22  bouyer 		if (sc->dt_maxsync < dt_scf_period[i].period)
    154      1.22  bouyer 			sc->dt_maxsync = dt_scf_period[i].period;
    155      1.22  bouyer 		if (sc->dt_minsync > dt_scf_period[i].period)
    156      1.22  bouyer 			sc->dt_minsync = dt_scf_period[i].period;
    157      1.22  bouyer 	}
    158      1.22  bouyer 	if (sc->dt_maxsync == 255 || sc->dt_minsync == 0)
    159      1.22  bouyer 		panic("siop: can't find my sync parameters\n");
    160      1.22  bouyer 	return 0;
    161      1.22  bouyer }
    162       1.1  bouyer 
    163       1.1  bouyer void
    164       1.1  bouyer siop_common_reset(sc)
    165      1.17  bouyer 	struct siop_common_softc *sc;
    166       1.1  bouyer {
    167       1.1  bouyer 	u_int32_t stest3;
    168       1.1  bouyer 
    169       1.1  bouyer 	/* reset the chip */
    170       1.1  bouyer 	bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_ISTAT, ISTAT_SRST);
    171       1.1  bouyer 	delay(1000);
    172       1.1  bouyer 	bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_ISTAT, 0);
    173       1.1  bouyer 
    174       1.1  bouyer 	/* init registers */
    175       1.1  bouyer 	bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_SCNTL0,
    176       1.1  bouyer 	    SCNTL0_ARB_MASK | SCNTL0_EPC | SCNTL0_AAP);
    177       1.1  bouyer 	bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_SCNTL1, 0);
    178       1.1  bouyer 	bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_SCNTL3, sc->clock_div);
    179       1.7  bouyer 	bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_SXFER, 0);
    180       1.1  bouyer 	bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_DIEN, 0xff);
    181       1.1  bouyer 	bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_SIEN0,
    182       1.1  bouyer 	    0xff & ~(SIEN0_CMP | SIEN0_SEL | SIEN0_RSL));
    183       1.1  bouyer 	bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_SIEN1,
    184       1.1  bouyer 	    0xff & ~(SIEN1_HTH | SIEN1_GEN));
    185       1.1  bouyer 	bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_STEST2, 0);
    186       1.1  bouyer 	bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_STEST3, STEST3_TE);
    187       1.1  bouyer 	bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_STIME0,
    188       1.1  bouyer 	    (0xb << STIME0_SEL_SHIFT));
    189       1.1  bouyer 	bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_SCID,
    190      1.14  bouyer 	    sc->sc_chan.chan_id | SCID_RRE);
    191       1.1  bouyer 	bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_RESPID0,
    192      1.14  bouyer 	    1 << sc->sc_chan.chan_id);
    193       1.1  bouyer 	bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_DCNTL,
    194       1.1  bouyer 	    (sc->features & SF_CHIP_PF) ? DCNTL_COM | DCNTL_PFEN : DCNTL_COM);
    195  1.28.4.2    tron 	if (sc->features & SF_CHIP_AAIP)
    196  1.28.4.2    tron 		bus_space_write_1(sc->sc_rt, sc->sc_rh,
    197  1.28.4.2    tron 		    SIOP_AIPCNTL1, AIPCNTL1_DIS);
    198       1.1  bouyer 
    199       1.1  bouyer 	/* enable clock doubler or quadruler if appropriate */
    200       1.1  bouyer 	if (sc->features & (SF_CHIP_DBLR | SF_CHIP_QUAD)) {
    201       1.1  bouyer 		stest3 = bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_STEST3);
    202       1.1  bouyer 		bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_STEST1,
    203       1.1  bouyer 		    STEST1_DBLEN);
    204       1.1  bouyer 		if (sc->features & SF_CHIP_QUAD) {
    205       1.1  bouyer 			/* wait for PPL to lock */
    206       1.1  bouyer 			while ((bus_space_read_1(sc->sc_rt, sc->sc_rh,
    207       1.1  bouyer 			    SIOP_STEST4) & STEST4_LOCK) == 0)
    208       1.1  bouyer 				delay(10);
    209       1.1  bouyer 		} else {
    210       1.1  bouyer 			/* data sheet says 20us - more won't hurt */
    211       1.1  bouyer 			delay(100);
    212       1.1  bouyer 		}
    213       1.1  bouyer 		/* halt scsi clock, select doubler/quad, restart clock */
    214       1.1  bouyer 		bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_STEST3,
    215       1.1  bouyer 		    stest3 | STEST3_HSC);
    216       1.1  bouyer 		bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_STEST1,
    217       1.1  bouyer 		    STEST1_DBLEN | STEST1_DBLSEL);
    218       1.1  bouyer 		bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_STEST3, stest3);
    219       1.1  bouyer 	} else {
    220       1.1  bouyer 		bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_STEST1, 0);
    221       1.1  bouyer 	}
    222       1.1  bouyer 	if (sc->features & SF_CHIP_FIFO)
    223       1.1  bouyer 		bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_CTEST5,
    224       1.1  bouyer 		    bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_CTEST5) |
    225       1.1  bouyer 		    CTEST5_DFS);
    226      1.21  bouyer 	if (sc->features & SF_CHIP_LED0) {
    227      1.21  bouyer 		/* Set GPIO0 as output if software LED control is required */
    228      1.21  bouyer 		bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_GPCNTL,
    229      1.21  bouyer 		    bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_GPCNTL) & 0xfe);
    230      1.21  bouyer 	}
    231      1.22  bouyer 	if (sc->features & SF_BUS_ULTRA3) {
    232      1.22  bouyer 		/* reset SCNTL4 */
    233      1.22  bouyer 		bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_SCNTL4, 0);
    234      1.22  bouyer 	}
    235      1.27  bouyer 	sc->mode = bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_STEST4) &
    236      1.27  bouyer 	    STEST4_MODE_MASK;
    237  1.28.4.1    tron 
    238  1.28.4.1    tron 	/*
    239  1.28.4.1    tron 	 * initialise the RAM. Without this we may get scsi gross errors on
    240  1.28.4.1    tron 	 * the 1010
    241  1.28.4.1    tron 	 */
    242  1.28.4.1    tron 	if (sc->features & SF_CHIP_RAM)
    243  1.28.4.1    tron 		bus_space_set_region_4(sc->sc_ramt, sc->sc_ramh,
    244  1.28.4.1    tron 			0, 0, sc->ram_size / 4);
    245       1.1  bouyer 	sc->sc_reset(sc);
    246       1.1  bouyer }
    247       1.1  bouyer 
    248      1.10  bouyer /* prepare tables before sending a cmd */
    249      1.10  bouyer void
    250      1.10  bouyer siop_setuptables(siop_cmd)
    251      1.17  bouyer 	struct siop_common_cmd *siop_cmd;
    252      1.10  bouyer {
    253      1.10  bouyer 	int i;
    254      1.17  bouyer 	struct siop_common_softc *sc = siop_cmd->siop_sc;
    255      1.10  bouyer 	struct scsipi_xfer *xs = siop_cmd->xs;
    256      1.14  bouyer 	int target = xs->xs_periph->periph_target;
    257      1.14  bouyer 	int lun = xs->xs_periph->periph_lun;
    258      1.14  bouyer 	int msgoffset = 1;
    259      1.10  bouyer 
    260      1.17  bouyer 	siop_cmd->siop_tables->id = htole32(sc->targets[target]->id);
    261      1.22  bouyer 	memset(siop_cmd->siop_tables->msg_out, 0,
    262      1.22  bouyer 	    sizeof(siop_cmd->siop_tables->msg_out));
    263      1.14  bouyer 	/* request sense doesn't disconnect */
    264      1.14  bouyer 	if (xs->xs_control & XS_CTL_REQSENSE)
    265      1.17  bouyer 		siop_cmd->siop_tables->msg_out[0] = MSG_IDENTIFY(lun, 0);
    266      1.26  bouyer 	else if ((sc->features & SF_CHIP_GEBUG) &&
    267      1.26  bouyer 	    (sc->targets[target]->flags & TARF_ISWIDE) == 0)
    268      1.26  bouyer 		/*
    269      1.26  bouyer 		 * 1010 bug: it seems that the 1010 has problems with reselect
    270      1.26  bouyer 		 * when not in wide mode (generate false SCSI gross error).
    271      1.26  bouyer 		 * The FreeBSD sym driver has comments about it but their
    272      1.26  bouyer 		 * workaround (disable SCSI gross error reporting) doesn't
    273      1.26  bouyer 		 * work with my adapter. So disable disconnect when not
    274      1.26  bouyer 		 * wide.
    275      1.26  bouyer 		 */
    276      1.26  bouyer 		siop_cmd->siop_tables->msg_out[0] = MSG_IDENTIFY(lun, 0);
    277      1.14  bouyer 	else
    278      1.17  bouyer 		siop_cmd->siop_tables->msg_out[0] = MSG_IDENTIFY(lun, 1);
    279      1.14  bouyer 	if (xs->xs_tag_type != 0) {
    280      1.14  bouyer 		if ((sc->targets[target]->flags & TARF_TAG) == 0) {
    281      1.14  bouyer 			scsipi_printaddr(xs->xs_periph);
    282      1.14  bouyer 			printf(": tagged command type %d id %d\n",
    283      1.14  bouyer 			    siop_cmd->xs->xs_tag_type, siop_cmd->xs->xs_tag_id);
    284      1.14  bouyer 			panic("tagged command for non-tagging device\n");
    285      1.14  bouyer 		}
    286      1.14  bouyer 		siop_cmd->flags |= CMDFL_TAG;
    287      1.17  bouyer 		siop_cmd->siop_tables->msg_out[1] = siop_cmd->xs->xs_tag_type;
    288      1.19  bouyer 		/*
    289      1.19  bouyer 		 * use siop_cmd->tag not xs->xs_tag_id, caller may want a
    290      1.19  bouyer 		 * different one
    291      1.19  bouyer 		 */
    292      1.19  bouyer 		siop_cmd->siop_tables->msg_out[2] = siop_cmd->tag;
    293      1.14  bouyer 		msgoffset = 3;
    294      1.20  bouyer 	}
    295      1.25  bouyer 	siop_cmd->siop_tables->t_msgout.count= htole32(msgoffset);
    296      1.10  bouyer 	if (sc->targets[target]->status == TARST_ASYNC) {
    297      1.27  bouyer 		if ((sc->targets[target]->flags & TARF_DT) &&
    298      1.27  bouyer 			(sc->mode == STEST4_MODE_LVD)) {
    299      1.22  bouyer 			sc->targets[target]->status = TARST_PPR_NEG;
    300      1.22  bouyer 			 siop_ppr_msg(siop_cmd, msgoffset, sc->dt_minsync,
    301      1.22  bouyer 			    sc->maxoff);
    302      1.22  bouyer 		} else if (sc->targets[target]->flags & TARF_WIDE) {
    303      1.10  bouyer 			sc->targets[target]->status = TARST_WIDE_NEG;
    304      1.14  bouyer 			siop_wdtr_msg(siop_cmd, msgoffset,
    305      1.14  bouyer 			    MSG_EXT_WDTR_BUS_16_BIT);
    306      1.10  bouyer 		} else if (sc->targets[target]->flags & TARF_SYNC) {
    307      1.10  bouyer 			sc->targets[target]->status = TARST_SYNC_NEG;
    308      1.22  bouyer 			siop_sdtr_msg(siop_cmd, msgoffset, sc->st_minsync,
    309      1.22  bouyer 			(sc->maxoff > 31) ? 31 :  sc->maxoff);
    310      1.10  bouyer 		} else {
    311      1.10  bouyer 			sc->targets[target]->status = TARST_OK;
    312      1.14  bouyer 			siop_update_xfer_mode(sc, target);
    313      1.10  bouyer 		}
    314      1.10  bouyer 	}
    315      1.17  bouyer 	siop_cmd->siop_tables->status =
    316      1.11  bouyer 	    htole32(SCSI_SIOP_NOSTATUS); /* set invalid status */
    317      1.10  bouyer 
    318      1.17  bouyer 	siop_cmd->siop_tables->cmd.count =
    319      1.10  bouyer 	    htole32(siop_cmd->dmamap_cmd->dm_segs[0].ds_len);
    320      1.17  bouyer 	siop_cmd->siop_tables->cmd.addr =
    321      1.10  bouyer 	    htole32(siop_cmd->dmamap_cmd->dm_segs[0].ds_addr);
    322      1.14  bouyer 	if (xs->xs_control & (XS_CTL_DATA_IN | XS_CTL_DATA_OUT)) {
    323      1.10  bouyer 		for (i = 0; i < siop_cmd->dmamap_data->dm_nsegs; i++) {
    324      1.17  bouyer 			siop_cmd->siop_tables->data[i].count =
    325      1.10  bouyer 			    htole32(siop_cmd->dmamap_data->dm_segs[i].ds_len);
    326      1.17  bouyer 			siop_cmd->siop_tables->data[i].addr =
    327      1.10  bouyer 			    htole32(siop_cmd->dmamap_data->dm_segs[i].ds_addr);
    328      1.10  bouyer 		}
    329      1.10  bouyer 	}
    330      1.10  bouyer }
    331      1.10  bouyer 
    332       1.1  bouyer int
    333       1.1  bouyer siop_wdtr_neg(siop_cmd)
    334      1.17  bouyer 	struct siop_common_cmd *siop_cmd;
    335       1.1  bouyer {
    336      1.17  bouyer 	struct siop_common_softc *sc = siop_cmd->siop_sc;
    337      1.17  bouyer 	struct siop_common_target *siop_target = siop_cmd->siop_target;
    338      1.14  bouyer 	int target = siop_cmd->xs->xs_periph->periph_target;
    339      1.17  bouyer 	struct siop_common_xfer *tables = siop_cmd->siop_tables;
    340       1.1  bouyer 
    341       1.1  bouyer 	if (siop_target->status == TARST_WIDE_NEG) {
    342       1.1  bouyer 		/* we initiated wide negotiation */
    343       1.9  bouyer 		switch (tables->msg_in[3]) {
    344       1.1  bouyer 		case MSG_EXT_WDTR_BUS_8_BIT:
    345       1.9  bouyer 			siop_target->flags &= ~TARF_ISWIDE;
    346       1.1  bouyer 			sc->targets[target]->id &= ~(SCNTL3_EWS << 24);
    347       1.1  bouyer 			break;
    348       1.1  bouyer 		case MSG_EXT_WDTR_BUS_16_BIT:
    349       1.9  bouyer 			if (siop_target->flags & TARF_WIDE) {
    350       1.9  bouyer 				siop_target->flags |= TARF_ISWIDE;
    351       1.1  bouyer 				sc->targets[target]->id |= (SCNTL3_EWS << 24);
    352       1.1  bouyer 				break;
    353       1.1  bouyer 			}
    354       1.1  bouyer 		/* FALLTHROUH */
    355       1.1  bouyer 		default:
    356       1.1  bouyer 			/*
    357       1.1  bouyer  			 * hum, we got more than what we can handle, shoudn't
    358       1.1  bouyer 			 * happen. Reject, and stay async
    359       1.1  bouyer 			 */
    360       1.9  bouyer 			siop_target->flags &= ~TARF_ISWIDE;
    361       1.1  bouyer 			siop_target->status = TARST_OK;
    362      1.14  bouyer 			siop_target->offset = siop_target->period = 0;
    363      1.14  bouyer 			siop_update_xfer_mode(sc, target);
    364       1.1  bouyer 			printf("%s: rejecting invalid wide negotiation from "
    365       1.1  bouyer 			    "target %d (%d)\n", sc->sc_dev.dv_xname, target,
    366       1.9  bouyer 			    tables->msg_in[3]);
    367       1.9  bouyer 			tables->t_msgout.count= htole32(1);
    368       1.9  bouyer 			tables->msg_out[0] = MSG_MESSAGE_REJECT;
    369       1.1  bouyer 			return SIOP_NEG_MSGOUT;
    370       1.1  bouyer 		}
    371       1.9  bouyer 		tables->id = htole32(sc->targets[target]->id);
    372       1.1  bouyer 		bus_space_write_1(sc->sc_rt, sc->sc_rh,
    373       1.1  bouyer 		    SIOP_SCNTL3,
    374       1.1  bouyer 		    (sc->targets[target]->id >> 24) & 0xff);
    375       1.1  bouyer 		/* we now need to do sync */
    376       1.9  bouyer 		if (siop_target->flags & TARF_SYNC) {
    377       1.6  bouyer 			siop_target->status = TARST_SYNC_NEG;
    378      1.22  bouyer 			siop_sdtr_msg(siop_cmd, 0, sc->st_minsync,
    379      1.22  bouyer 			    (sc->maxoff > 31) ? 31 : sc->maxoff);
    380       1.6  bouyer 			return SIOP_NEG_MSGOUT;
    381       1.6  bouyer 		} else {
    382       1.6  bouyer 			siop_target->status = TARST_OK;
    383      1.14  bouyer 			siop_update_xfer_mode(sc, target);
    384       1.6  bouyer 			return SIOP_NEG_ACK;
    385       1.6  bouyer 		}
    386       1.1  bouyer 	} else {
    387       1.1  bouyer 		/* target initiated wide negotiation */
    388       1.9  bouyer 		if (tables->msg_in[3] >= MSG_EXT_WDTR_BUS_16_BIT
    389       1.9  bouyer 		    && (siop_target->flags & TARF_WIDE)) {
    390       1.9  bouyer 			siop_target->flags |= TARF_ISWIDE;
    391       1.1  bouyer 			sc->targets[target]->id |= SCNTL3_EWS << 24;
    392       1.1  bouyer 		} else {
    393       1.9  bouyer 			siop_target->flags &= ~TARF_ISWIDE;
    394       1.1  bouyer 			sc->targets[target]->id &= ~(SCNTL3_EWS << 24);
    395       1.1  bouyer 		}
    396       1.9  bouyer 		tables->id = htole32(sc->targets[target]->id);
    397       1.1  bouyer 		bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_SCNTL3,
    398       1.1  bouyer 		    (sc->targets[target]->id >> 24) & 0xff);
    399       1.1  bouyer 		/*
    400       1.1  bouyer 		 * we did reset wide parameters, so fall back to async,
    401       1.8  bouyer 		 * but don't schedule a sync neg, target should initiate it
    402       1.1  bouyer 		 */
    403       1.1  bouyer 		siop_target->status = TARST_OK;
    404      1.14  bouyer 		siop_target->offset = siop_target->period = 0;
    405      1.14  bouyer 		siop_update_xfer_mode(sc, target);
    406      1.10  bouyer 		siop_wdtr_msg(siop_cmd, 0, (siop_target->flags & TARF_ISWIDE) ?
    407      1.10  bouyer 		    MSG_EXT_WDTR_BUS_16_BIT : MSG_EXT_WDTR_BUS_8_BIT);
    408       1.1  bouyer 		return SIOP_NEG_MSGOUT;
    409       1.1  bouyer 	}
    410       1.1  bouyer }
    411       1.1  bouyer 
    412       1.1  bouyer int
    413      1.22  bouyer siop_ppr_neg(siop_cmd)
    414      1.22  bouyer 	struct siop_common_cmd *siop_cmd;
    415      1.22  bouyer {
    416      1.22  bouyer 	struct siop_common_softc *sc = siop_cmd->siop_sc;
    417      1.22  bouyer 	struct siop_common_target *siop_target = siop_cmd->siop_target;
    418      1.22  bouyer 	int target = siop_cmd->xs->xs_periph->periph_target;
    419      1.22  bouyer 	struct siop_common_xfer *tables = siop_cmd->siop_tables;
    420      1.22  bouyer 	int sync, offset, options, scf = 0;
    421      1.22  bouyer 	int i;
    422      1.22  bouyer 
    423      1.22  bouyer #ifdef DEBUG_NEG
    424      1.22  bouyer 	printf("%s: anserw on ppr negotiation:", sc->sc_dev.dv_xname);
    425      1.22  bouyer 	for (i = 0; i < 8; i++)
    426      1.22  bouyer 		printf(" 0x%x", tables->msg_in[i]);
    427      1.22  bouyer 	printf("\n");
    428      1.22  bouyer #endif
    429      1.22  bouyer 
    430      1.22  bouyer 	if (siop_target->status == TARST_PPR_NEG) {
    431      1.22  bouyer 		/* we initiated PPR negotiation */
    432      1.22  bouyer 		sync = tables->msg_in[3];
    433      1.22  bouyer 		offset = tables->msg_in[5];
    434      1.22  bouyer 		options = tables->msg_in[7];
    435      1.22  bouyer 		if (options != MSG_EXT_PPR_DT) {
    436      1.22  bouyer 			/* should't happen */
    437      1.22  bouyer 			printf("%s: ppr negotiation for target %d: "
    438      1.22  bouyer 			    "no DT option\n", sc->sc_dev.dv_xname, target);
    439      1.22  bouyer 			siop_target->status = TARST_ASYNC;
    440      1.22  bouyer 			siop_target->flags &= ~(TARF_DT | TARF_ISDT);
    441      1.22  bouyer 			siop_target->offset = 0;
    442      1.22  bouyer 			siop_target->period = 0;
    443      1.22  bouyer 			goto reject;
    444      1.22  bouyer 		}
    445      1.22  bouyer 
    446      1.22  bouyer 		if (offset > sc->maxoff || sync < sc->dt_minsync ||
    447      1.22  bouyer 		    sync > sc->dt_maxsync) {
    448      1.22  bouyer 			printf("%s: ppr negotiation for target %d: "
    449      1.22  bouyer 			    "offset (%d) or sync (%d) out of range\n",
    450      1.22  bouyer 			    sc->sc_dev.dv_xname, target, offset, sync);
    451      1.22  bouyer 			/* should not happen */
    452      1.22  bouyer 			siop_target->offset = 0;
    453      1.22  bouyer 			siop_target->period = 0;
    454      1.22  bouyer 			goto reject;
    455      1.22  bouyer 		} else {
    456      1.22  bouyer 			for (i = 0; i <
    457      1.22  bouyer 			    sizeof(dt_scf_period) / sizeof(dt_scf_period[0]);
    458      1.22  bouyer 			    i++) {
    459      1.22  bouyer 				if (sc->clock_period != dt_scf_period[i].clock)
    460      1.22  bouyer 					continue;
    461      1.22  bouyer 				if (dt_scf_period[i].period == sync) {
    462      1.22  bouyer 					/* ok, found it. we now are sync. */
    463      1.22  bouyer 					siop_target->offset = offset;
    464      1.22  bouyer 					siop_target->period = sync;
    465      1.22  bouyer 					scf = dt_scf_period[i].scf;
    466      1.22  bouyer 					siop_target->flags |= TARF_ISDT;
    467      1.22  bouyer 				}
    468      1.22  bouyer 			}
    469      1.22  bouyer 			if ((siop_target->flags & TARF_ISDT) == 0) {
    470      1.22  bouyer 				printf("%s: ppr negotiation for target %d: "
    471      1.22  bouyer 				    "sync (%d) incompatible with adapter\n",
    472      1.22  bouyer 				    sc->sc_dev.dv_xname, target, sync);
    473      1.22  bouyer 				/*
    474      1.22  bouyer 				 * we didn't find it in our table, do async
    475      1.22  bouyer 				 * send reject msg, start SDTR/WDTR neg
    476      1.22  bouyer 				 */
    477      1.22  bouyer 				siop_target->status = TARST_ASYNC;
    478      1.22  bouyer 				siop_target->flags &= ~(TARF_DT | TARF_ISDT);
    479      1.22  bouyer 				siop_target->offset = 0;
    480      1.22  bouyer 				siop_target->period = 0;
    481      1.22  bouyer 				goto reject;
    482      1.22  bouyer 			}
    483      1.22  bouyer 		}
    484      1.22  bouyer 		if (tables->msg_in[6] != 1) {
    485      1.22  bouyer 			printf("%s: ppr negotiation for target %d: "
    486      1.22  bouyer 			    "transfer width (%d) incompatible with dt\n",
    487      1.22  bouyer 			    sc->sc_dev.dv_xname, target, tables->msg_in[6]);
    488      1.22  bouyer 			/* DT mode can only be done with wide transfers */
    489      1.22  bouyer 			siop_target->status = TARST_ASYNC;
    490      1.22  bouyer 			goto reject;
    491      1.22  bouyer 		}
    492      1.22  bouyer 		siop_target->flags |= TARF_ISWIDE;
    493      1.22  bouyer 		sc->targets[target]->id |= (SCNTL3_EWS << 24);
    494      1.22  bouyer 		sc->targets[target]->id &= ~(SCNTL3_SCF_MASK << 24);
    495      1.22  bouyer 		sc->targets[target]->id |= scf << (24 + SCNTL3_SCF_SHIFT);
    496      1.22  bouyer 		sc->targets[target]->id &= ~(SXFER_MO_MASK << 8);
    497      1.22  bouyer 		sc->targets[target]->id |=
    498      1.22  bouyer 		    (siop_target->offset & SXFER_MO_MASK) << 8;
    499      1.22  bouyer 		sc->targets[target]->id &= ~0xff;
    500      1.22  bouyer 		sc->targets[target]->id |= SCNTL4_U3EN;
    501      1.22  bouyer 		siop_target->status = TARST_OK;
    502      1.22  bouyer 		siop_update_xfer_mode(sc, target);
    503      1.22  bouyer 		bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_SCNTL3,
    504      1.22  bouyer 		    (sc->targets[target]->id >> 24) & 0xff);
    505      1.22  bouyer 		bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_SXFER,
    506      1.22  bouyer 		    (sc->targets[target]->id >> 8) & 0xff);
    507      1.22  bouyer 		bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_SCNTL4,
    508      1.22  bouyer 		    sc->targets[target]->id & 0xff);
    509      1.22  bouyer 		return SIOP_NEG_ACK;
    510      1.22  bouyer 	} else {
    511      1.22  bouyer 		/* target initiated PPR negotiation, shouldn't happen */
    512      1.22  bouyer 		printf("%s: rejecting invalid PPR negotiation from "
    513      1.22  bouyer 		    "target %d\n", sc->sc_dev.dv_xname, target);
    514      1.22  bouyer reject:
    515      1.22  bouyer 		tables->t_msgout.count= htole32(1);
    516      1.22  bouyer 		tables->msg_out[0] = MSG_MESSAGE_REJECT;
    517      1.22  bouyer 		return SIOP_NEG_MSGOUT;
    518      1.22  bouyer 	}
    519      1.22  bouyer }
    520      1.22  bouyer 
    521      1.22  bouyer int
    522       1.1  bouyer siop_sdtr_neg(siop_cmd)
    523      1.17  bouyer 	struct siop_common_cmd *siop_cmd;
    524       1.1  bouyer {
    525      1.17  bouyer 	struct siop_common_softc *sc = siop_cmd->siop_sc;
    526      1.17  bouyer 	struct siop_common_target *siop_target = siop_cmd->siop_target;
    527      1.14  bouyer 	int target = siop_cmd->xs->xs_periph->periph_target;
    528      1.22  bouyer 	int sync, maxoffset, offset, i;
    529       1.1  bouyer 	int send_msgout = 0;
    530      1.17  bouyer 	struct siop_common_xfer *tables = siop_cmd->siop_tables;
    531       1.1  bouyer 
    532      1.22  bouyer 	/* limit to Ultra/2 parameters, need PPR for Ultra/3 */
    533      1.22  bouyer 	maxoffset = (sc->maxoff > 31) ? 31 : sc->maxoff;
    534      1.22  bouyer 
    535       1.9  bouyer 	sync = tables->msg_in[3];
    536       1.9  bouyer 	offset = tables->msg_in[4];
    537       1.1  bouyer 
    538       1.1  bouyer 	if (siop_target->status == TARST_SYNC_NEG) {
    539       1.1  bouyer 		/* we initiated sync negotiation */
    540       1.1  bouyer 		siop_target->status = TARST_OK;
    541       1.1  bouyer #ifdef DEBUG
    542       1.1  bouyer 		printf("sdtr: sync %d offset %d\n", sync, offset);
    543       1.1  bouyer #endif
    544      1.22  bouyer 		if (offset > maxoffset || sync < sc->st_minsync ||
    545      1.22  bouyer 			sync > sc->st_maxsync)
    546       1.1  bouyer 			goto reject;
    547       1.1  bouyer 		for (i = 0; i < sizeof(scf_period) / sizeof(scf_period[0]);
    548       1.1  bouyer 		    i++) {
    549       1.1  bouyer 			if (sc->clock_period != scf_period[i].clock)
    550       1.1  bouyer 				continue;
    551       1.1  bouyer 			if (scf_period[i].period == sync) {
    552       1.1  bouyer 				/* ok, found it. we now are sync. */
    553      1.14  bouyer 				siop_target->offset = offset;
    554      1.14  bouyer 				siop_target->period = sync;
    555       1.1  bouyer 				sc->targets[target]->id &=
    556       1.1  bouyer 				    ~(SCNTL3_SCF_MASK << 24);
    557       1.1  bouyer 				sc->targets[target]->id |= scf_period[i].scf
    558       1.1  bouyer 				    << (24 + SCNTL3_SCF_SHIFT);
    559      1.22  bouyer 				if (sync < 25 && /* Ultra */
    560      1.22  bouyer 				    (sc->features & SF_BUS_ULTRA3) == 0)
    561       1.1  bouyer 					sc->targets[target]->id |=
    562       1.1  bouyer 					    SCNTL3_ULTRA << 24;
    563       1.1  bouyer 				else
    564       1.1  bouyer 					sc->targets[target]->id &=
    565       1.1  bouyer 					    ~(SCNTL3_ULTRA << 24);
    566       1.1  bouyer 				sc->targets[target]->id &=
    567       1.7  bouyer 				    ~(SXFER_MO_MASK << 8);
    568       1.1  bouyer 				sc->targets[target]->id |=
    569       1.7  bouyer 				    (offset & SXFER_MO_MASK) << 8;
    570      1.25  bouyer 				sc->targets[target]->id &= ~0xff; /* scntl4 */
    571       1.1  bouyer 				goto end;
    572       1.1  bouyer 			}
    573       1.1  bouyer 		}
    574       1.1  bouyer 		/*
    575       1.1  bouyer 		 * we didn't find it in our table, do async and send reject
    576       1.1  bouyer 		 * msg
    577       1.1  bouyer 		 */
    578       1.1  bouyer reject:
    579       1.1  bouyer 		send_msgout = 1;
    580       1.9  bouyer 		tables->t_msgout.count= htole32(1);
    581       1.9  bouyer 		tables->msg_out[0] = MSG_MESSAGE_REJECT;
    582       1.1  bouyer 		sc->targets[target]->id &= ~(SCNTL3_SCF_MASK << 24);
    583       1.1  bouyer 		sc->targets[target]->id &= ~(SCNTL3_ULTRA << 24);
    584       1.7  bouyer 		sc->targets[target]->id &= ~(SXFER_MO_MASK << 8);
    585      1.25  bouyer 		sc->targets[target]->id &= ~0xff; /* scntl4 */
    586      1.14  bouyer 		siop_target->offset = siop_target->period = 0;
    587       1.1  bouyer 	} else { /* target initiated sync neg */
    588       1.1  bouyer #ifdef DEBUG
    589       1.1  bouyer 		printf("sdtr (target): sync %d offset %d\n", sync, offset);
    590       1.1  bouyer #endif
    591      1.22  bouyer 		if (offset == 0 || sync > sc->st_maxsync) { /* async */
    592       1.1  bouyer 			goto async;
    593       1.1  bouyer 		}
    594      1.22  bouyer 		if (offset > maxoffset)
    595      1.22  bouyer 			offset = maxoffset;
    596      1.22  bouyer 		if (sync < sc->st_minsync)
    597      1.22  bouyer 			sync = sc->st_minsync;
    598       1.1  bouyer 		/* look for sync period */
    599       1.1  bouyer 		for (i = 0; i < sizeof(scf_period) / sizeof(scf_period[0]);
    600       1.1  bouyer 		    i++) {
    601       1.1  bouyer 			if (sc->clock_period != scf_period[i].clock)
    602       1.1  bouyer 				continue;
    603       1.1  bouyer 			if (scf_period[i].period == sync) {
    604       1.1  bouyer 				/* ok, found it. we now are sync. */
    605      1.14  bouyer 				siop_target->offset = offset;
    606      1.14  bouyer 				siop_target->period = sync;
    607       1.1  bouyer 				sc->targets[target]->id &=
    608       1.1  bouyer 				    ~(SCNTL3_SCF_MASK << 24);
    609       1.1  bouyer 				sc->targets[target]->id |= scf_period[i].scf
    610       1.1  bouyer 				    << (24 + SCNTL3_SCF_SHIFT);
    611      1.22  bouyer 				if (sync < 25 && /* Ultra */
    612      1.22  bouyer 				    (sc->features & SF_BUS_ULTRA3) == 0)
    613       1.1  bouyer 					sc->targets[target]->id |=
    614       1.1  bouyer 					    SCNTL3_ULTRA << 24;
    615       1.1  bouyer 				else
    616       1.1  bouyer 					sc->targets[target]->id &=
    617       1.1  bouyer 					    ~(SCNTL3_ULTRA << 24);
    618       1.1  bouyer 				sc->targets[target]->id &=
    619       1.7  bouyer 				    ~(SXFER_MO_MASK << 8);
    620       1.1  bouyer 				sc->targets[target]->id |=
    621       1.7  bouyer 				    (offset & SXFER_MO_MASK) << 8;
    622      1.25  bouyer 				sc->targets[target]->id &= ~0xff; /* scntl4 */
    623      1.10  bouyer 				siop_sdtr_msg(siop_cmd, 0, sync, offset);
    624       1.1  bouyer 				send_msgout = 1;
    625       1.1  bouyer 				goto end;
    626       1.1  bouyer 			}
    627       1.1  bouyer 		}
    628       1.1  bouyer async:
    629      1.14  bouyer 		siop_target->offset = siop_target->period = 0;
    630       1.1  bouyer 		sc->targets[target]->id &= ~(SCNTL3_SCF_MASK << 24);
    631       1.1  bouyer 		sc->targets[target]->id &= ~(SCNTL3_ULTRA << 24);
    632       1.7  bouyer 		sc->targets[target]->id &= ~(SXFER_MO_MASK << 8);
    633      1.25  bouyer 		sc->targets[target]->id &= ~0xff; /* scntl4 */
    634      1.10  bouyer 		siop_sdtr_msg(siop_cmd, 0, 0, 0);
    635       1.1  bouyer 		send_msgout = 1;
    636       1.1  bouyer 	}
    637       1.1  bouyer end:
    638      1.14  bouyer 	if (siop_target->status == TARST_OK)
    639      1.14  bouyer 		siop_update_xfer_mode(sc, target);
    640       1.1  bouyer #ifdef DEBUG
    641       1.1  bouyer 	printf("id now 0x%x\n", sc->targets[target]->id);
    642       1.1  bouyer #endif
    643       1.9  bouyer 	tables->id = htole32(sc->targets[target]->id);
    644       1.1  bouyer 	bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_SCNTL3,
    645       1.1  bouyer 	    (sc->targets[target]->id >> 24) & 0xff);
    646       1.7  bouyer 	bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_SXFER,
    647       1.1  bouyer 	    (sc->targets[target]->id >> 8) & 0xff);
    648       1.1  bouyer 	if (send_msgout) {
    649       1.1  bouyer 		return SIOP_NEG_MSGOUT;
    650       1.1  bouyer 	} else {
    651       1.1  bouyer 		return SIOP_NEG_ACK;
    652       1.1  bouyer 	}
    653       1.1  bouyer }
    654       1.1  bouyer 
    655       1.1  bouyer void
    656      1.10  bouyer siop_sdtr_msg(siop_cmd, offset, ssync, soff)
    657      1.17  bouyer 	struct siop_common_cmd *siop_cmd;
    658      1.10  bouyer 	int offset;
    659      1.10  bouyer 	int ssync, soff;
    660      1.10  bouyer {
    661      1.17  bouyer 	siop_cmd->siop_tables->msg_out[offset + 0] = MSG_EXTENDED;
    662      1.17  bouyer 	siop_cmd->siop_tables->msg_out[offset + 1] = MSG_EXT_SDTR_LEN;
    663      1.17  bouyer 	siop_cmd->siop_tables->msg_out[offset + 2] = MSG_EXT_SDTR;
    664      1.17  bouyer 	siop_cmd->siop_tables->msg_out[offset + 3] = ssync;
    665      1.17  bouyer 	siop_cmd->siop_tables->msg_out[offset + 4] = soff;
    666      1.17  bouyer 	siop_cmd->siop_tables->t_msgout.count =
    667      1.10  bouyer 	    htole32(offset + MSG_EXT_SDTR_LEN + 2);
    668      1.10  bouyer }
    669      1.10  bouyer 
    670      1.10  bouyer void
    671      1.10  bouyer siop_wdtr_msg(siop_cmd, offset, wide)
    672      1.17  bouyer 	struct siop_common_cmd *siop_cmd;
    673      1.10  bouyer 	int offset;
    674      1.10  bouyer {
    675      1.17  bouyer 	siop_cmd->siop_tables->msg_out[offset + 0] = MSG_EXTENDED;
    676      1.17  bouyer 	siop_cmd->siop_tables->msg_out[offset + 1] = MSG_EXT_WDTR_LEN;
    677      1.17  bouyer 	siop_cmd->siop_tables->msg_out[offset + 2] = MSG_EXT_WDTR;
    678      1.17  bouyer 	siop_cmd->siop_tables->msg_out[offset + 3] = wide;
    679      1.17  bouyer 	siop_cmd->siop_tables->t_msgout.count =
    680      1.10  bouyer 	    htole32(offset + MSG_EXT_WDTR_LEN + 2);
    681      1.22  bouyer }
    682      1.22  bouyer 
    683      1.22  bouyer void
    684      1.22  bouyer siop_ppr_msg(siop_cmd, offset, ssync, soff)
    685      1.22  bouyer 	struct siop_common_cmd *siop_cmd;
    686      1.22  bouyer 	int offset;
    687      1.22  bouyer 	int ssync, soff;
    688      1.22  bouyer {
    689      1.22  bouyer 	siop_cmd->siop_tables->msg_out[offset + 0] = MSG_EXTENDED;
    690      1.22  bouyer 	siop_cmd->siop_tables->msg_out[offset + 1] = MSG_EXT_PPR_LEN;
    691      1.22  bouyer 	siop_cmd->siop_tables->msg_out[offset + 2] = MSG_EXT_PPR;
    692      1.22  bouyer 	siop_cmd->siop_tables->msg_out[offset + 3] = ssync;
    693      1.22  bouyer 	siop_cmd->siop_tables->msg_out[offset + 4] = 0; /* reserved */
    694      1.22  bouyer 	siop_cmd->siop_tables->msg_out[offset + 5] = soff;
    695      1.22  bouyer 	siop_cmd->siop_tables->msg_out[offset + 6] = 1; /* wide */
    696      1.22  bouyer 	siop_cmd->siop_tables->msg_out[offset + 7] = MSG_EXT_PPR_DT;
    697      1.22  bouyer 	siop_cmd->siop_tables->t_msgout.count =
    698      1.22  bouyer 	    htole32(offset + MSG_EXT_PPR_LEN + 2);
    699      1.10  bouyer }
    700      1.10  bouyer 
    701      1.10  bouyer void
    702       1.1  bouyer siop_minphys(bp)
    703       1.1  bouyer 	struct buf *bp;
    704       1.1  bouyer {
    705       1.1  bouyer 	minphys(bp);
    706       1.1  bouyer }
    707       1.1  bouyer 
    708       1.1  bouyer int
    709      1.14  bouyer siop_ioctl(chan, cmd, arg, flag, p)
    710      1.14  bouyer 	struct scsipi_channel *chan;
    711       1.1  bouyer 	u_long cmd;
    712       1.1  bouyer 	caddr_t arg;
    713       1.1  bouyer 	int flag;
    714       1.1  bouyer 	struct proc *p;
    715       1.1  bouyer {
    716      1.17  bouyer 	struct siop_common_softc *sc = (void *)chan->chan_adapter->adapt_dev;
    717       1.1  bouyer 
    718       1.1  bouyer 	switch (cmd) {
    719       1.1  bouyer 	case SCBUSIORESET:
    720      1.24  bouyer 		/*
    721      1.24  bouyer 		 * abort the script. This will trigger an interrupt, which will
    722      1.24  bouyer 		 * trigger a bus reset.
    723      1.24  bouyer 		 * We can't safely trigger the reset here as we can't access
    724      1.24  bouyer 		 * the required register while the script is running.
    725      1.24  bouyer 		 */
    726      1.24  bouyer 		bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_ISTAT, ISTAT_ABRT);
    727       1.1  bouyer 		return (0);
    728       1.1  bouyer 	default:
    729       1.1  bouyer 		return (ENOTTY);
    730       1.1  bouyer 	}
    731       1.1  bouyer }
    732       1.1  bouyer 
    733       1.1  bouyer void
    734  1.28.4.4    tron siop_ma(siop_cmd)
    735      1.17  bouyer 	struct siop_common_cmd *siop_cmd;
    736       1.1  bouyer {
    737       1.1  bouyer 	int offset, dbc, sstat;
    738      1.17  bouyer 	struct siop_common_softc *sc = siop_cmd->siop_sc;
    739  1.28.4.4    tron 	scr_table_t *table; /* table with partial xfer */
    740       1.1  bouyer 
    741  1.28.4.4    tron 	/*
    742  1.28.4.4    tron 	 * compute how much of the current table didn't get handled when
    743  1.28.4.4    tron 	 * a phase mismatch occurs
    744  1.28.4.4    tron 	 */
    745       1.1  bouyer 	if ((siop_cmd->xs->xs_control & (XS_CTL_DATA_OUT | XS_CTL_DATA_IN))
    746       1.1  bouyer 	    == 0)
    747  1.28.4.4    tron 	    return; /* no valid data transfer */
    748  1.28.4.4    tron 
    749       1.1  bouyer 	offset = bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_SCRATCHA + 1);
    750       1.1  bouyer 	if (offset >= SIOP_NSG) {
    751       1.1  bouyer 		printf("%s: bad offset in siop_sdp (%d)\n",
    752       1.1  bouyer 		    sc->sc_dev.dv_xname, offset);
    753       1.1  bouyer 		return;
    754       1.1  bouyer 	}
    755      1.17  bouyer 	table = &siop_cmd->siop_tables->data[offset];
    756       1.1  bouyer #ifdef DEBUG_DR
    757  1.28.4.4    tron 	printf("siop_ma: offset %d count=%d addr=0x%x ", offset,
    758       1.1  bouyer 	    table->count, table->addr);
    759       1.1  bouyer #endif
    760       1.1  bouyer 	dbc = bus_space_read_4(sc->sc_rt, sc->sc_rh, SIOP_DBC) & 0x00ffffff;
    761       1.1  bouyer 	if (siop_cmd->xs->xs_control & XS_CTL_DATA_OUT) {
    762      1.13  bouyer 		if (sc->features & SF_CHIP_DFBC) {
    763      1.13  bouyer 			dbc +=
    764      1.13  bouyer 			    bus_space_read_2(sc->sc_rt, sc->sc_rh, SIOP_DFBC);
    765       1.1  bouyer 		} else {
    766      1.13  bouyer 			/* need to account stale data in FIFO */
    767      1.13  bouyer 			int dfifo =
    768      1.13  bouyer 			    bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_DFIFO);
    769      1.13  bouyer 			if (sc->features & SF_CHIP_FIFO) {
    770      1.13  bouyer 				dfifo |= (bus_space_read_1(sc->sc_rt, sc->sc_rh,
    771      1.13  bouyer 				    SIOP_CTEST5) & CTEST5_BOMASK) << 8;
    772      1.13  bouyer 				dbc += (dfifo - (dbc & 0x3ff)) & 0x3ff;
    773      1.13  bouyer 			} else {
    774      1.13  bouyer 				dbc += (dfifo - (dbc & 0x7f)) & 0x7f;
    775      1.13  bouyer 			}
    776       1.1  bouyer 		}
    777       1.1  bouyer 		sstat = bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_SSTAT0);
    778       1.1  bouyer 		if (sstat & SSTAT0_OLF)
    779       1.1  bouyer 			dbc++;
    780      1.13  bouyer 		if ((sstat & SSTAT0_ORF) && (sc->features & SF_CHIP_DFBC) == 0)
    781       1.1  bouyer 			dbc++;
    782       1.9  bouyer 		if (siop_cmd->siop_target->flags & TARF_ISWIDE) {
    783       1.1  bouyer 			sstat = bus_space_read_1(sc->sc_rt, sc->sc_rh,
    784       1.1  bouyer 			    SIOP_SSTAT2);
    785       1.1  bouyer 			if (sstat & SSTAT2_OLF1)
    786       1.1  bouyer 				dbc++;
    787      1.13  bouyer 			if ((sstat & SSTAT2_ORF1) &&
    788      1.13  bouyer 			    (sc->features & SF_CHIP_DFBC) == 0)
    789       1.1  bouyer 				dbc++;
    790       1.1  bouyer 		}
    791       1.1  bouyer 		/* clear the FIFO */
    792       1.1  bouyer 		bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_CTEST3,
    793       1.1  bouyer 		    bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_CTEST3) |
    794       1.1  bouyer 		    CTEST3_CLF);
    795       1.1  bouyer 	}
    796  1.28.4.4    tron 	siop_cmd->flags |= CMDFL_RESID;
    797  1.28.4.4    tron 	siop_cmd->resid = dbc;
    798  1.28.4.4    tron }
    799  1.28.4.4    tron 
    800  1.28.4.4    tron void
    801  1.28.4.4    tron siop_sdp(siop_cmd, offset)
    802  1.28.4.4    tron 	struct siop_common_cmd *siop_cmd;
    803  1.28.4.4    tron 	int offset;
    804  1.28.4.4    tron {
    805  1.28.4.4    tron 	scr_table_t *table;
    806  1.28.4.4    tron 
    807  1.28.4.4    tron 	if ((siop_cmd->xs->xs_control & (XS_CTL_DATA_OUT | XS_CTL_DATA_IN))
    808  1.28.4.4    tron 	    == 0)
    809  1.28.4.4    tron 	    return; /* no data pointers to save */
    810  1.28.4.4    tron 
    811  1.28.4.4    tron 	/*
    812  1.28.4.4    tron 	 * offset == SIOP_NSG may be a valid condition if we get a Save data
    813  1.28.4.4    tron 	 * pointer when the xfer is done. Just ignore the Save data pointer
    814  1.28.4.4    tron 	 * in this case
    815  1.28.4.4    tron 	 */
    816  1.28.4.4    tron 	if (offset == SIOP_NSG)
    817  1.28.4.4    tron 		return;
    818  1.28.4.4    tron #ifdef DIAGNOSTIC
    819  1.28.4.4    tron 	if (offset > SIOP_NSG) {
    820  1.28.4.4    tron 		scsipi_printaddr(siop_cmd->xs->xs_periph);
    821  1.28.4.4    tron 		printf(": offset %d > %d\n", offset, SIOP_NSG);
    822  1.28.4.4    tron 		panic("siop_sdp: offset");
    823  1.28.4.4    tron 	}
    824       1.1  bouyer #endif
    825  1.28.4.4    tron 	/*
    826  1.28.4.4    tron 	 * Save data pointer. We do this by adjusting the tables to point
    827  1.28.4.4    tron 	 * at the begginning of the data not yet transfered.
    828  1.28.4.4    tron 	 * offset points to the first table with untransfered data.
    829  1.28.4.4    tron 	 */
    830  1.28.4.4    tron 
    831  1.28.4.4    tron 	/*
    832  1.28.4.4    tron 	 * before doing that we decrease resid from the ammount of data which
    833  1.28.4.4    tron 	 * has been transfered.
    834  1.28.4.4    tron 	 */
    835  1.28.4.4    tron 	siop_update_resid(siop_cmd, offset);
    836  1.28.4.4    tron 
    837  1.28.4.4    tron 	/*
    838  1.28.4.4    tron 	 * First let see if we have a resid from a phase mismatch. If so,
    839  1.28.4.4    tron 	 * we have to adjst the table at offset to remove transfered data.
    840  1.28.4.4    tron 	 */
    841  1.28.4.4    tron 	if (siop_cmd->flags & CMDFL_RESID) {
    842  1.28.4.4    tron 		siop_cmd->flags &= ~CMDFL_RESID;
    843  1.28.4.4    tron 		table = &siop_cmd->siop_tables->data[offset];
    844  1.28.4.4    tron 		/* "cut" already transfered data from this table */
    845  1.28.4.4    tron 		table->addr =
    846  1.28.4.4    tron 		    htole32(le32toh(table->addr) +
    847  1.28.4.4    tron 		    le32toh(table->count) - siop_cmd->resid);
    848  1.28.4.4    tron 		table->count = htole32(siop_cmd->resid);
    849  1.28.4.4    tron 	}
    850  1.28.4.4    tron 
    851  1.28.4.4    tron 	/*
    852  1.28.4.4    tron 	 * now we can remove entries which have been transfered.
    853  1.28.4.4    tron 	 * We just move the entries with data left at the beggining of the
    854  1.28.4.4    tron 	 * tables
    855  1.28.4.4    tron 	 */
    856  1.28.4.4    tron 	memmove(&siop_cmd->siop_tables->data[0],
    857  1.28.4.4    tron 	    &siop_cmd->siop_tables->data[offset],
    858  1.28.4.4    tron 	    (SIOP_NSG - offset) * sizeof(scr_table_t));
    859  1.28.4.4    tron }
    860  1.28.4.4    tron 
    861  1.28.4.4    tron void
    862  1.28.4.4    tron siop_update_resid(siop_cmd, offset)
    863  1.28.4.4    tron 	struct siop_common_cmd *siop_cmd;
    864  1.28.4.4    tron 	int offset;
    865  1.28.4.4    tron {
    866  1.28.4.4    tron 	scr_table_t *table;
    867  1.28.4.4    tron 	int i;
    868  1.28.4.4    tron 
    869  1.28.4.4    tron 	if ((siop_cmd->xs->xs_control & (XS_CTL_DATA_OUT | XS_CTL_DATA_IN))
    870  1.28.4.4    tron 	    == 0)
    871  1.28.4.4    tron 	    return; /* no data to transfer */
    872  1.28.4.4    tron 
    873  1.28.4.4    tron 	/*
    874  1.28.4.4    tron 	 * update resid. First account for the table entries which have
    875  1.28.4.4    tron 	 * been fully completed.
    876  1.28.4.4    tron 	 */
    877  1.28.4.4    tron 	for (i = 0; i < offset; i++)
    878  1.28.4.4    tron 		siop_cmd->xs->resid -=
    879  1.28.4.4    tron 		    le32toh(siop_cmd->siop_tables->data[i].count);
    880  1.28.4.4    tron 	/*
    881  1.28.4.4    tron 	 * if CMDFL_RESID is set, the last table (pointed by offset) is a
    882  1.28.4.4    tron 	 * partial transfers. If not, offset points to the entry folloing
    883  1.28.4.4    tron 	 * the last full transfer.
    884  1.28.4.4    tron 	 */
    885  1.28.4.4    tron 	if (siop_cmd->flags & CMDFL_RESID) {
    886  1.28.4.4    tron 		table = &siop_cmd->siop_tables->data[offset];
    887  1.28.4.4    tron 		siop_cmd->xs->resid -= le32toh(table->count) - siop_cmd->resid;
    888  1.28.4.4    tron 	}
    889       1.1  bouyer }
    890       1.1  bouyer 
    891       1.1  bouyer void
    892       1.1  bouyer siop_clearfifo(sc)
    893      1.17  bouyer 	struct siop_common_softc *sc;
    894       1.1  bouyer {
    895       1.1  bouyer 	int timeout = 0;
    896       1.1  bouyer 	int ctest3 = bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_CTEST3);
    897       1.1  bouyer 
    898       1.1  bouyer #ifdef DEBUG_INTR
    899       1.1  bouyer 	printf("DMA fifo not empty !\n");
    900       1.1  bouyer #endif
    901       1.1  bouyer 	bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_CTEST3,
    902       1.1  bouyer 	    ctest3 | CTEST3_CLF);
    903       1.1  bouyer 	while ((bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_CTEST3) &
    904       1.1  bouyer 	    CTEST3_CLF) != 0) {
    905       1.1  bouyer 		delay(1);
    906       1.1  bouyer 		if (++timeout > 1000) {
    907       1.1  bouyer 			printf("clear fifo failed\n");
    908       1.1  bouyer 			bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_CTEST3,
    909       1.1  bouyer 			    bus_space_read_1(sc->sc_rt, sc->sc_rh,
    910       1.1  bouyer 			    SIOP_CTEST3) & ~CTEST3_CLF);
    911       1.1  bouyer 			return;
    912       1.1  bouyer 		}
    913       1.1  bouyer 	}
    914       1.3  bouyer }
    915       1.3  bouyer 
    916       1.3  bouyer int
    917       1.3  bouyer siop_modechange(sc)
    918      1.17  bouyer 	struct siop_common_softc *sc;
    919       1.3  bouyer {
    920       1.3  bouyer 	int retry;
    921      1.27  bouyer 	int sist0, sist1, stest2;
    922       1.3  bouyer 	for (retry = 0; retry < 5; retry++) {
    923       1.3  bouyer 		/*
    924       1.3  bouyer 		 * datasheet says to wait 100ms and re-read SIST1,
    925      1.14  bouyer 		 * to check that DIFFSENSE is stable.
    926       1.3  bouyer 		 * We may delay() 5 times for  100ms at interrupt time;
    927       1.3  bouyer 		 * hopefully this will not happen often.
    928       1.3  bouyer 		 */
    929       1.3  bouyer 		delay(100000);
    930       1.3  bouyer 		sist0 = bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_SIST0);
    931       1.3  bouyer 		sist1 = bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_SIST1);
    932       1.3  bouyer 		if (sist1 & SIEN1_SBMC)
    933       1.3  bouyer 			continue; /* we got an irq again */
    934      1.27  bouyer 		sc->mode = bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_STEST4) &
    935       1.3  bouyer 		    STEST4_MODE_MASK;
    936       1.3  bouyer 		stest2 = bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_STEST2);
    937      1.27  bouyer 		switch(sc->mode) {
    938       1.3  bouyer 		case STEST4_MODE_DIF:
    939       1.3  bouyer 			printf("%s: switching to differential mode\n",
    940       1.3  bouyer 			    sc->sc_dev.dv_xname);
    941       1.3  bouyer 			bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_STEST2,
    942       1.3  bouyer 			    stest2 | STEST2_DIF);
    943       1.3  bouyer 			break;
    944       1.3  bouyer 		case STEST4_MODE_SE:
    945       1.3  bouyer 			printf("%s: switching to single-ended mode\n",
    946       1.3  bouyer 			    sc->sc_dev.dv_xname);
    947       1.3  bouyer 			bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_STEST2,
    948       1.3  bouyer 			    stest2 & ~STEST2_DIF);
    949       1.3  bouyer 			break;
    950       1.3  bouyer 		case STEST4_MODE_LVD:
    951       1.3  bouyer 			printf("%s: switching to LVD mode\n",
    952       1.3  bouyer 			    sc->sc_dev.dv_xname);
    953       1.3  bouyer 			bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_STEST2,
    954       1.3  bouyer 			    stest2 & ~STEST2_DIF);
    955       1.3  bouyer 			break;
    956       1.3  bouyer 		default:
    957       1.3  bouyer 			printf("%s: invalid SCSI mode 0x%x\n",
    958      1.27  bouyer 			    sc->sc_dev.dv_xname, sc->mode);
    959       1.3  bouyer 			return 0;
    960       1.3  bouyer 		}
    961       1.3  bouyer 		return 1;
    962       1.3  bouyer 	}
    963       1.3  bouyer 	printf("%s: timeout waiting for DIFFSENSE to stabilise\n",
    964       1.3  bouyer 	    sc->sc_dev.dv_xname);
    965       1.3  bouyer 	return 0;
    966       1.6  bouyer }
    967       1.6  bouyer 
    968       1.6  bouyer void
    969       1.6  bouyer siop_resetbus(sc)
    970      1.17  bouyer 	struct siop_common_softc *sc;
    971       1.6  bouyer {
    972       1.6  bouyer 	int scntl1;
    973       1.6  bouyer 	scntl1 = bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_SCNTL1);
    974       1.6  bouyer 	bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_SCNTL1,
    975       1.6  bouyer 	    scntl1 | SCNTL1_RST);
    976       1.6  bouyer 	/* minimum 25 us, more time won't hurt */
    977       1.6  bouyer 	delay(100);
    978       1.6  bouyer 	bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_SCNTL1, scntl1);
    979      1.17  bouyer }
    980      1.17  bouyer 
    981      1.17  bouyer void
    982      1.17  bouyer siop_update_xfer_mode(sc, target)
    983      1.17  bouyer 	struct siop_common_softc *sc;
    984      1.17  bouyer 	int target;
    985      1.17  bouyer {
    986      1.17  bouyer 	struct siop_common_target *siop_target = sc->targets[target];
    987      1.17  bouyer 	struct scsipi_xfer_mode xm;
    988      1.17  bouyer 
    989      1.17  bouyer 	xm.xm_target = target;
    990      1.17  bouyer 	xm.xm_mode = 0;
    991      1.17  bouyer 	xm.xm_period = 0;
    992      1.17  bouyer 	xm.xm_offset = 0;
    993      1.26  bouyer 
    994      1.17  bouyer 
    995      1.17  bouyer 	if (siop_target->flags & TARF_ISWIDE)
    996      1.17  bouyer 		xm.xm_mode |= PERIPH_CAP_WIDE16;
    997      1.17  bouyer 	if (siop_target->period) {
    998      1.17  bouyer 		xm.xm_period = siop_target->period;
    999      1.17  bouyer 		xm.xm_offset = siop_target->offset;
   1000      1.17  bouyer 		xm.xm_mode |= PERIPH_CAP_SYNC;
   1001      1.17  bouyer 	}
   1002      1.28  bouyer 	if (siop_target->flags & TARF_TAG) {
   1003      1.28  bouyer 	/* 1010 workaround: can't do disconnect if not wide, so can't do tag */
   1004      1.28  bouyer 		if ((sc->features & SF_CHIP_GEBUG) == 0 ||
   1005      1.28  bouyer 		    (sc->targets[target]->flags & TARF_ISWIDE))
   1006      1.28  bouyer 			xm.xm_mode |= PERIPH_CAP_TQING;
   1007      1.28  bouyer 	}
   1008      1.28  bouyer 
   1009      1.17  bouyer 	scsipi_async_event(&sc->sc_chan, ASYNC_EVENT_XFER_MODE, &xm);
   1010       1.1  bouyer }
   1011