Home | History | Annotate | Line # | Download | only in podulebus
sfas.c revision 1.22.2.1
      1  1.22.2.1     rmind /*	$NetBSD: sfas.c,v 1.22.2.1 2014/05/18 17:44:53 rmind Exp $	*/
      2      1.12       agc 
      3      1.12       agc /*
      4      1.12       agc  * Copyright (c) 1990 The Regents of the University of California.
      5      1.12       agc  * All rights reserved.
      6      1.12       agc  *
      7      1.12       agc  * This code is derived from software contributed to Berkeley by
      8      1.12       agc  * Van Jacobson of Lawrence Berkeley Laboratory.
      9      1.12       agc  *
     10      1.12       agc  * Redistribution and use in source and binary forms, with or without
     11      1.12       agc  * modification, are permitted provided that the following conditions
     12      1.12       agc  * are met:
     13      1.12       agc  * 1. Redistributions of source code must retain the above copyright
     14      1.12       agc  *    notice, this list of conditions and the following disclaimer.
     15      1.12       agc  * 2. Redistributions in binary form must reproduce the above copyright
     16      1.12       agc  *    notice, this list of conditions and the following disclaimer in the
     17      1.12       agc  *    documentation and/or other materials provided with the distribution.
     18      1.12       agc  * 3. Neither the name of the University nor the names of its contributors
     19      1.12       agc  *    may be used to endorse or promote products derived from this software
     20      1.12       agc  *    without specific prior written permission.
     21      1.12       agc  *
     22      1.12       agc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23      1.12       agc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24      1.12       agc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25      1.12       agc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26      1.12       agc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27      1.12       agc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28      1.12       agc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29      1.12       agc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30      1.12       agc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31      1.12       agc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32      1.12       agc  * SUCH DAMAGE.
     33      1.12       agc  *
     34      1.12       agc  *	@(#)scsi.c	7.5 (Berkeley) 5/4/91
     35      1.12       agc  */
     36       1.1   reinoud 
     37       1.1   reinoud /*
     38       1.1   reinoud  * Copyright (c) 1995 Scott Stevens
     39       1.1   reinoud  * Copyright (c) 1995 Daniel Widenfalk
     40       1.1   reinoud  * Copyright (c) 1994 Christian E. Hopps
     41       1.1   reinoud  *
     42       1.1   reinoud  * This code is derived from software contributed to Berkeley by
     43       1.1   reinoud  * Van Jacobson of Lawrence Berkeley Laboratory.
     44       1.1   reinoud  *
     45       1.1   reinoud  * Redistribution and use in source and binary forms, with or without
     46       1.1   reinoud  * modification, are permitted provided that the following conditions
     47       1.1   reinoud  * are met:
     48       1.1   reinoud  * 1. Redistributions of source code must retain the above copyright
     49       1.1   reinoud  *    notice, this list of conditions and the following disclaimer.
     50       1.1   reinoud  * 2. Redistributions in binary form must reproduce the above copyright
     51       1.1   reinoud  *    notice, this list of conditions and the following disclaimer in the
     52       1.1   reinoud  *    documentation and/or other materials provided with the distribution.
     53       1.1   reinoud  * 3. All advertising materials mentioning features or use of this software
     54       1.1   reinoud  *    must display the following acknowledgement:
     55       1.1   reinoud  *	This product includes software developed by the University of
     56       1.1   reinoud  *	California, Berkeley and its contributors.
     57       1.1   reinoud  * 4. Neither the name of the University nor the names of its contributors
     58       1.1   reinoud  *    may be used to endorse or promote products derived from this software
     59       1.1   reinoud  *    without specific prior written permission.
     60       1.1   reinoud  *
     61       1.1   reinoud  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     62       1.1   reinoud  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     63       1.1   reinoud  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     64       1.1   reinoud  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     65       1.1   reinoud  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     66       1.1   reinoud  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     67       1.1   reinoud  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     68       1.1   reinoud  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     69       1.1   reinoud  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     70       1.1   reinoud  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     71       1.1   reinoud  * SUCH DAMAGE.
     72       1.1   reinoud  *
     73       1.1   reinoud  *	@(#)scsi.c	7.5 (Berkeley) 5/4/91
     74       1.1   reinoud  */
     75       1.1   reinoud 
     76       1.1   reinoud /*
     77       1.1   reinoud  * Emulex FAS216 scsi adaptor driver
     78       1.1   reinoud  */
     79       1.1   reinoud 
     80       1.1   reinoud /*
     81       1.1   reinoud  * Modified for NetBSD/arm32 by Scott Stevens
     82       1.1   reinoud  */
     83      1.11     lukem 
     84      1.11     lukem #include <sys/cdefs.h>
     85  1.22.2.1     rmind __KERNEL_RCSID(0, "$NetBSD: sfas.c,v 1.22.2.1 2014/05/18 17:44:53 rmind Exp $");
     86       1.1   reinoud 
     87       1.1   reinoud #include <sys/param.h>
     88       1.1   reinoud #include <sys/systm.h>
     89       1.1   reinoud #include <sys/device.h>
     90       1.1   reinoud #include <sys/buf.h>
     91       1.1   reinoud #include <sys/proc.h>
     92       1.1   reinoud 
     93       1.1   reinoud #include <dev/scsipi/scsi_all.h>
     94       1.1   reinoud #include <dev/scsipi/scsipi_all.h>
     95       1.1   reinoud #include <dev/scsipi/scsiconf.h>
     96       1.1   reinoud 
     97       1.1   reinoud #include <uvm/uvm_extern.h>
     98       1.1   reinoud 
     99       1.1   reinoud #include <machine/pmap.h>
    100       1.1   reinoud #include <machine/cpu.h>
    101       1.1   reinoud #include <machine/io.h>
    102       1.3   thorpej #include <machine/intr.h>
    103       1.2   thorpej #include <arm/arm32/katelib.h>
    104       1.1   reinoud #include <acorn32/podulebus/podulebus.h>
    105       1.1   reinoud #include <acorn32/podulebus/sfasreg.h>
    106       1.1   reinoud #include <acorn32/podulebus/sfasvar.h>
    107       1.1   reinoud 
    108       1.8       chs void sfas_minphys(struct buf *);
    109       1.8       chs void sfas_init_nexus(struct sfas_softc *, struct nexus *);
    110       1.8       chs void sfasinitialize(struct sfas_softc *);
    111       1.8       chs void sfas_scsi_request(struct scsipi_channel *, scsipi_adapter_req_t, void *);
    112       1.8       chs void sfas_donextcmd(struct sfas_softc *, struct sfas_pending *);
    113       1.8       chs void sfas_scsidone(struct sfas_softc *, struct scsipi_xfer *, int);
    114       1.8       chs void sfasintr(struct sfas_softc *);
    115       1.8       chs void sfasiwait(struct sfas_softc *);
    116       1.8       chs void sfas_ixfer(void *, int);
    117       1.8       chs void sfasreset(struct sfas_softc *, int);
    118       1.8       chs int  sfasselect(struct sfas_softc *, struct sfas_pending *, unsigned char *,
    119       1.8       chs 		int, unsigned char *, int, int);
    120       1.8       chs void sfasicmd(struct sfas_softc *, struct sfas_pending *);
    121       1.8       chs void sfasgo(struct sfas_softc *, struct sfas_pending *);
    122       1.8       chs void sfas_save_pointers(struct sfas_softc *);
    123       1.8       chs void sfas_restore_pointers(struct sfas_softc *);
    124       1.8       chs void sfas_build_sdtrm(struct sfas_softc *, int, int);
    125       1.8       chs int sfas_select_unit(struct sfas_softc *, short);
    126       1.8       chs struct nexus *sfas_arbitate_target(struct sfas_softc *, int);
    127       1.8       chs void sfas_setup_nexus(struct sfas_softc *, struct nexus *,
    128       1.8       chs 		      struct sfas_pending *, unsigned char *, int,
    129       1.8       chs 		      unsigned char *, int, int);
    130       1.8       chs int sfas_pretests(struct sfas_softc *, sfas_regmap_p);
    131       1.8       chs int sfas_midaction(struct sfas_softc *, sfas_regmap_p, struct nexus *);
    132       1.8       chs int sfas_postaction(struct sfas_softc *, sfas_regmap_p, struct nexus *);
    133       1.1   reinoud 
    134       1.1   reinoud /*
    135       1.1   reinoud  * Initialize these to make 'em patchable. Defaults to enable sync and discon.
    136       1.1   reinoud  */
    137       1.1   reinoud u_char	sfas_inhibit_sync[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
    138       1.1   reinoud u_char	sfas_inhibit_disc[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
    139       1.1   reinoud 
    140      1.21  uebayasi #undef DEBUG
    141       1.1   reinoud #define DEBUG
    142       1.1   reinoud #ifdef DEBUG
    143       1.1   reinoud #define QPRINTF(a) if (sfas_debug > 1) printf a
    144       1.1   reinoud int	sfas_debug = 2;
    145       1.1   reinoud #else
    146       1.1   reinoud #define QPRINTF
    147       1.1   reinoud #endif
    148       1.1   reinoud 
    149       1.1   reinoud /*
    150       1.1   reinoud  * default minphys routine for sfas based controllers
    151       1.1   reinoud  */
    152       1.1   reinoud void
    153      1.17       dsl sfas_minphys(struct buf *bp)
    154       1.1   reinoud {
    155       1.1   reinoud 
    156       1.1   reinoud 	/*
    157       1.1   reinoud 	 * No max transfer at this level.
    158       1.1   reinoud 	 */
    159       1.1   reinoud 	minphys(bp);
    160       1.1   reinoud }
    161       1.1   reinoud 
    162       1.1   reinoud /*
    163       1.1   reinoud  * Initialize the nexus structs.
    164       1.1   reinoud  */
    165       1.1   reinoud void
    166      1.17       dsl sfas_init_nexus(struct sfas_softc *dev, struct nexus *nexus)
    167       1.1   reinoud {
    168      1.18    cegger 	memset(nexus, 0, sizeof(struct nexus));
    169       1.1   reinoud 
    170       1.1   reinoud 	nexus->state	= SFAS_NS_IDLE;
    171       1.1   reinoud 	nexus->period	= 200;
    172       1.1   reinoud 	nexus->offset	= 0;
    173       1.1   reinoud 	nexus->syncper	= 5;
    174       1.1   reinoud 	nexus->syncoff	= 0;
    175       1.1   reinoud 	nexus->config3	= dev->sc_config3 & ~SFAS_CFG3_FASTSCSI;
    176       1.1   reinoud }
    177       1.1   reinoud 
    178       1.1   reinoud void
    179      1.17       dsl sfasinitialize(struct sfas_softc *dev)
    180       1.1   reinoud {
    181       1.1   reinoud 	int		 i;
    182       1.1   reinoud 
    183       1.1   reinoud 	dev->sc_led_status = 0;
    184       1.1   reinoud 
    185       1.1   reinoud 	TAILQ_INIT(&dev->sc_xs_pending);
    186       1.1   reinoud 	TAILQ_INIT(&dev->sc_xs_free);
    187       1.1   reinoud 
    188       1.1   reinoud /*
    189       1.1   reinoud  * Initialize the sfas_pending structs and link them into the free list. We
    190       1.1   reinoud  * have to set vm_link_data.pages to 0 or the vm FIX won't work.
    191       1.1   reinoud  */
    192       1.1   reinoud 	for(i=0; i<MAXPENDING; i++) {
    193       1.1   reinoud 		TAILQ_INSERT_TAIL(&dev->sc_xs_free, &dev->sc_xs_store[i],
    194       1.1   reinoud 				  link);
    195       1.1   reinoud 	}
    196       1.1   reinoud 
    197       1.1   reinoud /*
    198       1.1   reinoud  * Calculate the correct clock conversion factor 2 <= factor <= 8, i.e. set
    199       1.1   reinoud  * the factor to clock_freq / 5 (int).
    200       1.1   reinoud  */
    201       1.1   reinoud 	if (dev->sc_clock_freq <= 10)
    202       1.1   reinoud 		dev->sc_clock_conv_fact = 2;
    203       1.1   reinoud 	if (dev->sc_clock_freq <= 40)
    204       1.1   reinoud 		dev->sc_clock_conv_fact = 2+((dev->sc_clock_freq-10)/5);
    205       1.1   reinoud 	else
    206       1.1   reinoud 		panic("sfasinitialize: Clock frequence too high");
    207       1.1   reinoud 
    208       1.1   reinoud /* Setup and save the basic configuration registers */
    209       1.1   reinoud 	dev->sc_config1 = (dev->sc_host_id & SFAS_CFG1_BUS_ID_MASK);
    210       1.1   reinoud 	dev->sc_config2 = SFAS_CFG2_FEATURES_ENABLE;
    211       1.1   reinoud 	dev->sc_config3 = (dev->sc_clock_freq > 25 ? SFAS_CFG3_FASTCLK : 0);
    212       1.1   reinoud 
    213       1.1   reinoud /* Precalculate timeout value and clock period. */
    214       1.1   reinoud /* Ekkk ... floating point in the kernel !!!! */
    215       1.1   reinoud /*	dev->sc_timeout_val  = 1+dev->sc_timeout*dev->sc_clock_freq/
    216       1.1   reinoud 				 (7.682*dev->sc_clock_conv_fact);*/
    217       1.1   reinoud 	dev->sc_timeout_val  = 1+dev->sc_timeout*dev->sc_clock_freq/
    218       1.1   reinoud 				 ((7682*dev->sc_clock_conv_fact)/1000);
    219       1.1   reinoud 	dev->sc_clock_period = 1000/dev->sc_clock_freq;
    220       1.1   reinoud 
    221       1.1   reinoud 	sfasreset(dev, 1 | 2);	/* Reset Chip and Bus */
    222       1.1   reinoud 
    223       1.1   reinoud 	dev->sc_units_disconnected = 0;
    224       1.1   reinoud 	dev->sc_msg_in_len = 0;
    225       1.1   reinoud 	dev->sc_msg_out_len = 0;
    226       1.1   reinoud 
    227       1.1   reinoud 	dev->sc_flags = 0;
    228       1.1   reinoud 
    229       1.1   reinoud 	for(i=0; i<8; i++)
    230       1.1   reinoud 		sfas_init_nexus(dev, &dev->sc_nexus[i]);
    231       1.1   reinoud 
    232       1.1   reinoud 	if (dev->sc_ixfer == NULL)
    233       1.1   reinoud 		dev->sc_ixfer = sfas_ixfer;
    234       1.1   reinoud 
    235       1.1   reinoud /*
    236       1.1   reinoud  * Setup bump buffer.
    237       1.1   reinoud  */
    238      1.14      yamt 	dev->sc_bump_va = (u_char *)uvm_km_alloc(kernel_map, dev->sc_bump_sz, 0,
    239      1.14      yamt 	    UVM_KMF_WIRED | UVM_KMF_ZERO);
    240       1.1   reinoud 	(void) pmap_extract(pmap_kernel(), (vaddr_t)dev->sc_bump_va,
    241       1.1   reinoud 	    (paddr_t *)&dev->sc_bump_pa);
    242       1.1   reinoud 
    243       1.1   reinoud /*
    244       1.1   reinoud  * Setup pages to noncachable, that way we don't have to flush the cache
    245       1.1   reinoud  * every time we need "bumped" transfer.
    246       1.1   reinoud  */
    247  1.22.2.1     rmind 	pt_entry_t * const ptep = vtopte((vaddr_t) dev->sc_bump_va);
    248  1.22.2.1     rmind 	const pt_entry_t opte = *ptep;
    249  1.22.2.1     rmind 	const pt_entry_t npte = opte & ~(L2_C | L2_B);
    250  1.22.2.1     rmind 	l2pte_set(ptep, npte, opte);
    251  1.22.2.1     rmind 	PTE_SYNC(ptep);
    252       1.1   reinoud 	cpu_tlb_flushD();
    253       1.9   thorpej 	cpu_dcache_wbinv_range((vm_offset_t)dev->sc_bump_va, PAGE_SIZE);
    254       1.1   reinoud 
    255       1.1   reinoud 	printf(" dmabuf V0x%08x P0x%08x", (u_int)dev->sc_bump_va, (u_int)dev->sc_bump_pa);
    256       1.1   reinoud }
    257       1.1   reinoud 
    258       1.1   reinoud 
    259       1.1   reinoud /*
    260       1.1   reinoud  * used by specific sfas controller
    261       1.1   reinoud  */
    262       1.1   reinoud void
    263       1.1   reinoud sfas_scsi_request(struct scsipi_channel *chan, scsipi_adapter_req_t req,
    264       1.1   reinoud 								void *arg)
    265       1.1   reinoud {
    266       1.1   reinoud 	struct scsipi_xfer *xs;
    267      1.22       chs 	struct sfas_softc	*dev = device_private(chan->chan_adapter->adapt_dev);
    268       1.1   reinoud 	struct scsipi_periph	*periph;
    269       1.1   reinoud 	struct sfas_pending	*pendp;
    270       1.1   reinoud 	int			 flags, s, target;
    271       1.1   reinoud 
    272       1.1   reinoud 	switch (req) {
    273       1.1   reinoud 	case ADAPTER_REQ_RUN_XFER:
    274       1.1   reinoud 		xs = arg;
    275       1.1   reinoud 		periph = xs->xs_periph;
    276       1.1   reinoud 		flags = xs->xs_control;
    277       1.1   reinoud 		target = periph->periph_target;
    278       1.1   reinoud 
    279       1.1   reinoud 		if (flags & XS_CTL_DATA_UIO)
    280       1.1   reinoud 			panic("sfas: scsi data uio requested");
    281       1.1   reinoud 
    282       1.1   reinoud 		if ((flags & XS_CTL_POLL) && (dev->sc_flags & SFAS_ACTIVE))
    283       1.1   reinoud 			panic("sfas_scsicmd: busy");
    284       1.1   reinoud 
    285       1.1   reinoud /* Get hold of a sfas_pending block. */
    286       1.1   reinoud 		s = splbio();
    287       1.1   reinoud 		pendp = dev->sc_xs_free.tqh_first;
    288       1.1   reinoud 		if (pendp == NULL) {
    289       1.1   reinoud 			xs->error = XS_RESOURCE_SHORTAGE;
    290       1.1   reinoud 			scsipi_done(xs);
    291       1.1   reinoud 			splx(s);
    292       1.1   reinoud 			return;
    293       1.1   reinoud 		}
    294       1.1   reinoud 		TAILQ_REMOVE(&dev->sc_xs_free, pendp, link);
    295       1.1   reinoud 		pendp->xs = xs;
    296       1.1   reinoud 		splx(s);
    297       1.1   reinoud 
    298       1.1   reinoud 
    299       1.1   reinoud /* If the chip if busy OR the unit is busy, we have to wait for out turn. */
    300       1.1   reinoud 		if ((dev->sc_flags & SFAS_ACTIVE) ||
    301       1.1   reinoud 		    (dev->sc_nexus[target].flags & SFAS_NF_UNIT_BUSY)) {
    302       1.1   reinoud 			s = splbio();
    303       1.1   reinoud 			TAILQ_INSERT_TAIL(&dev->sc_xs_pending, pendp, link);
    304       1.1   reinoud 			splx(s);
    305       1.1   reinoud 		} else
    306       1.1   reinoud 			sfas_donextcmd(dev, pendp);
    307       1.1   reinoud 
    308       1.1   reinoud 		return;
    309       1.1   reinoud 
    310       1.1   reinoud 	case ADAPTER_REQ_GROW_RESOURCES:
    311       1.1   reinoud 	case ADAPTER_REQ_SET_XFER_MODE:
    312       1.1   reinoud 		/* XXX Not supported. */
    313       1.1   reinoud 		return;
    314       1.1   reinoud 	}
    315       1.1   reinoud }
    316       1.1   reinoud 
    317       1.1   reinoud /*
    318       1.1   reinoud  * Actually select the unit, whereby the whole scsi-process is started.
    319       1.1   reinoud  */
    320       1.1   reinoud void
    321      1.17       dsl sfas_donextcmd(struct sfas_softc *dev, struct sfas_pending *pendp)
    322       1.1   reinoud {
    323       1.1   reinoud 	int	s;
    324       1.1   reinoud 
    325       1.1   reinoud /*
    326       1.1   reinoud  * Special case for scsi unit reset. I think this is waterproof. We first
    327       1.1   reinoud  * select the unit during splbio. We then cycle through the generated
    328       1.1   reinoud  * interrupts until the interrupt routine signals that the unit has
    329       1.1   reinoud  * acknowledged the reset. After that we have to wait a reset to select
    330       1.1   reinoud  * delay before anything else can happend.
    331       1.1   reinoud  */
    332       1.1   reinoud 	if (pendp->xs->xs_control & XS_CTL_RESET) {
    333       1.1   reinoud 		struct nexus	*nexus;
    334       1.1   reinoud 
    335       1.1   reinoud 		s = splbio();
    336       1.1   reinoud 		while(!sfasselect(dev, pendp, 0, 0, 0, 0, SFAS_SELECT_K)) {
    337       1.1   reinoud 			splx(s);
    338       1.1   reinoud 			delay(10);
    339       1.1   reinoud 			s = splbio();
    340       1.1   reinoud 		}
    341       1.1   reinoud 
    342       1.1   reinoud 		nexus = dev->sc_cur_nexus;
    343       1.1   reinoud 		while(nexus->flags & SFAS_NF_UNIT_BUSY) {
    344       1.1   reinoud 			sfasiwait(dev);
    345       1.1   reinoud 			sfasintr(dev);
    346       1.1   reinoud 		}
    347       1.1   reinoud 
    348       1.1   reinoud 		nexus->flags |= SFAS_NF_UNIT_BUSY;
    349       1.1   reinoud 		splx(s);
    350       1.1   reinoud 
    351       1.1   reinoud 		sfasreset(dev, 0);
    352       1.1   reinoud 
    353       1.1   reinoud 		s = splbio();
    354       1.1   reinoud 		nexus->flags &= ~SFAS_NF_UNIT_BUSY;
    355       1.1   reinoud 		splx(s);
    356       1.1   reinoud 	}
    357       1.1   reinoud 
    358       1.1   reinoud /*
    359       1.1   reinoud  * If we are polling, go to splbio and perform the command, else we poke
    360       1.1   reinoud  * the scsi-bus via sfasgo to get the interrupt machine going.
    361       1.1   reinoud  */
    362       1.1   reinoud 	if (pendp->xs->xs_control & XS_CTL_POLL) {
    363       1.1   reinoud 		s = splbio();
    364       1.1   reinoud 		sfasicmd(dev, pendp);
    365       1.1   reinoud 		TAILQ_INSERT_TAIL(&dev->sc_xs_free, pendp, link);
    366       1.1   reinoud 		splx(s);
    367       1.1   reinoud 	} else {
    368       1.1   reinoud 		sfasgo(dev, pendp);
    369       1.1   reinoud 	}
    370       1.1   reinoud }
    371       1.1   reinoud 
    372       1.1   reinoud void
    373      1.17       dsl sfas_scsidone(struct sfas_softc *dev, struct scsipi_xfer *xs, int stat)
    374       1.1   reinoud {
    375       1.1   reinoud 	struct sfas_pending	*pendp;
    376       1.1   reinoud 	int			 s;
    377       1.1   reinoud 
    378       1.1   reinoud 	xs->status = stat;
    379       1.1   reinoud 
    380       1.1   reinoud 	if (stat == 0)
    381       1.1   reinoud 		xs->resid = 0;
    382       1.1   reinoud 	else {
    383       1.1   reinoud 		switch(stat) {
    384       1.1   reinoud 		case SCSI_CHECK:
    385       1.1   reinoud 		case SCSI_BUSY:
    386       1.1   reinoud 			xs->error = XS_BUSY;
    387       1.1   reinoud 			break;
    388       1.1   reinoud 		case -1:
    389       1.1   reinoud 			xs->error = XS_DRIVER_STUFFUP;
    390       1.1   reinoud 			QPRINTF(("sfas_scsicmd() bad %x\n", stat));
    391       1.1   reinoud 			break;
    392       1.1   reinoud 		default:
    393       1.1   reinoud 			xs->error = XS_TIMEOUT;
    394       1.1   reinoud 			break;
    395       1.1   reinoud 		}
    396       1.1   reinoud 	}
    397       1.1   reinoud 
    398       1.1   reinoud /* Steal the next command from the queue so that one unit can't hog the bus. */
    399       1.1   reinoud 	s = splbio();
    400       1.1   reinoud 	pendp = dev->sc_xs_pending.tqh_first;
    401       1.1   reinoud 	while(pendp) {
    402       1.1   reinoud 		if (!(dev->sc_nexus[pendp->xs->xs_periph->periph_target].flags &
    403       1.1   reinoud 		      SFAS_NF_UNIT_BUSY))
    404       1.1   reinoud 			break;
    405       1.1   reinoud 		pendp = pendp->link.tqe_next;
    406       1.1   reinoud 	}
    407       1.1   reinoud 
    408       1.1   reinoud 	if (pendp != NULL) {
    409       1.1   reinoud 		TAILQ_REMOVE(&dev->sc_xs_pending, pendp, link);
    410       1.1   reinoud 	}
    411       1.1   reinoud 
    412       1.1   reinoud 	splx(s);
    413       1.1   reinoud 	scsipi_done(xs);
    414       1.1   reinoud 
    415       1.1   reinoud 	if (pendp)
    416       1.1   reinoud 		sfas_donextcmd(dev, pendp);
    417       1.1   reinoud }
    418       1.1   reinoud 
    419       1.1   reinoud /*
    420       1.1   reinoud  * There are two kinds of reset:
    421       1.1   reinoud  *  1) CHIP-bus reset. This also implies a SCSI-bus reset.
    422       1.1   reinoud  *  2) SCSI-bus reset.
    423       1.1   reinoud  * After the appropriate resets have been performed we wait a reset to select
    424       1.1   reinoud  * delay time.
    425       1.1   reinoud  */
    426       1.1   reinoud void
    427      1.17       dsl sfasreset(struct sfas_softc *dev, int how)
    428       1.1   reinoud {
    429       1.1   reinoud 	sfas_regmap_p	rp;
    430       1.1   reinoud 	int		i, s;
    431       1.1   reinoud 
    432       1.1   reinoud 	rp = dev->sc_fas;
    433       1.1   reinoud 
    434       1.1   reinoud 	if (how & 1) {
    435       1.1   reinoud 		for(i=0; i<8; i++)
    436       1.1   reinoud 			sfas_init_nexus(dev, &dev->sc_nexus[i]);
    437       1.1   reinoud 
    438       1.1   reinoud 		*rp->sfas_command = SFAS_CMD_RESET_CHIP;
    439       1.1   reinoud 		delay(1);
    440       1.1   reinoud 		*rp->sfas_command = SFAS_CMD_NOP;
    441       1.1   reinoud 
    442       1.1   reinoud 		*rp->sfas_config1 = dev->sc_config1;
    443       1.1   reinoud 		*rp->sfas_config2 = dev->sc_config2;
    444       1.1   reinoud 		*rp->sfas_config3 = dev->sc_config3;
    445       1.1   reinoud 		*rp->sfas_timeout = dev->sc_timeout_val;
    446       1.1   reinoud 		*rp->sfas_clkconv = dev->sc_clock_conv_fact &
    447       1.1   reinoud 					SFAS_CLOCK_CONVERSION_MASK;
    448       1.1   reinoud 	}
    449       1.1   reinoud 
    450       1.1   reinoud 	if (how & 2) {
    451       1.1   reinoud 		for(i=0; i<8; i++)
    452       1.1   reinoud 			sfas_init_nexus(dev, &dev->sc_nexus[i]);
    453       1.1   reinoud 
    454       1.1   reinoud 		s = splbio();
    455       1.1   reinoud 
    456       1.1   reinoud 		*rp->sfas_command = SFAS_CMD_RESET_SCSI_BUS;
    457       1.1   reinoud 		delay(100);
    458       1.1   reinoud 
    459       1.1   reinoud /* Skip interrupt generated by RESET_SCSI_BUS */
    460       1.1   reinoud 		while(*rp->sfas_status & SFAS_STAT_INTERRUPT_PENDING) {
    461       1.1   reinoud 			dev->sc_status = *rp->sfas_status;
    462       1.1   reinoud 			dev->sc_interrupt = *rp->sfas_interrupt;
    463       1.1   reinoud 
    464       1.1   reinoud 			delay(100);
    465       1.1   reinoud 		}
    466       1.1   reinoud 
    467       1.1   reinoud 		dev->sc_status = *rp->sfas_status;
    468       1.1   reinoud 		dev->sc_interrupt = *rp->sfas_interrupt;
    469       1.1   reinoud 
    470       1.1   reinoud 		splx(s);
    471       1.1   reinoud 	}
    472       1.1   reinoud 
    473       1.1   reinoud 	if (dev->sc_config_flags & SFAS_SLOW_START)
    474       1.1   reinoud 		delay(4*250000); /* RESET to SELECT DELAY*4 for slow devices */
    475       1.1   reinoud 	else
    476       1.1   reinoud 		delay(250000);	 /* RESET to SELECT DELAY */
    477       1.1   reinoud }
    478       1.1   reinoud 
    479       1.1   reinoud /*
    480       1.1   reinoud  * Save active data pointers to the nexus block currently active.
    481       1.1   reinoud  */
    482       1.1   reinoud void
    483      1.17       dsl sfas_save_pointers(struct sfas_softc *dev)
    484       1.1   reinoud {
    485       1.1   reinoud 	struct nexus	*nx;
    486       1.1   reinoud 
    487       1.1   reinoud 	nx = dev->sc_cur_nexus;
    488       1.1   reinoud 	if (nx) {
    489       1.1   reinoud 		nx->cur_link	= dev->sc_cur_link;
    490       1.1   reinoud 		nx->max_link	= dev->sc_max_link;
    491       1.1   reinoud 		nx->buf		= dev->sc_buf;
    492       1.1   reinoud 		nx->len		= dev->sc_len;
    493       1.1   reinoud 		nx->dma_len	= dev->sc_dma_len;
    494       1.1   reinoud 		nx->dma_buf	= dev->sc_dma_buf;
    495       1.1   reinoud 		nx->dma_blk_flg	= dev->sc_dma_blk_flg;
    496       1.1   reinoud 		nx->dma_blk_len	= dev->sc_dma_blk_len;
    497       1.1   reinoud 		nx->dma_blk_ptr	= dev->sc_dma_blk_ptr;
    498       1.1   reinoud 	}
    499       1.1   reinoud }
    500       1.1   reinoud 
    501       1.1   reinoud /*
    502       1.1   reinoud  * Restore data pointers from the currently active nexus block.
    503       1.1   reinoud  */
    504       1.1   reinoud void
    505      1.17       dsl sfas_restore_pointers(struct sfas_softc *dev)
    506       1.1   reinoud {
    507       1.1   reinoud 	struct nexus	*nx;
    508       1.1   reinoud 
    509       1.1   reinoud 	nx = dev->sc_cur_nexus;
    510       1.1   reinoud 	if (nx) {
    511       1.1   reinoud 		dev->sc_cur_link    = nx->cur_link;
    512       1.1   reinoud 		dev->sc_max_link    = nx->max_link;
    513       1.1   reinoud 		dev->sc_buf	    = nx->buf;
    514       1.1   reinoud 		dev->sc_len	    = nx->len;
    515       1.1   reinoud 		dev->sc_dma_len	    = nx->dma_len;
    516       1.1   reinoud 		dev->sc_dma_buf	    = nx->dma_buf;
    517       1.1   reinoud 		dev->sc_dma_blk_flg = nx->dma_blk_flg;
    518       1.1   reinoud 		dev->sc_dma_blk_len = nx->dma_blk_len;
    519       1.1   reinoud 		dev->sc_dma_blk_ptr = nx->dma_blk_ptr;
    520       1.1   reinoud 		dev->sc_chain	    = nx->dma;
    521       1.1   reinoud 		dev->sc_unit	    = (nx->lun_unit & 0x0F);
    522       1.1   reinoud 		dev->sc_lun	    = (nx->lun_unit & 0xF0) >> 4;
    523       1.1   reinoud 	}
    524       1.1   reinoud }
    525       1.1   reinoud 
    526       1.1   reinoud /*
    527       1.1   reinoud  * sfasiwait is used during interrupt and polled IO to wait for an event from
    528       1.1   reinoud  * the FAS chip. This function MUST NOT BE CALLED without interrupt disabled.
    529       1.1   reinoud  */
    530       1.1   reinoud void
    531      1.17       dsl sfasiwait(struct sfas_softc *dev)
    532       1.1   reinoud {
    533       1.1   reinoud 	sfas_regmap_p	rp;
    534       1.1   reinoud 
    535       1.1   reinoud /*
    536       1.1   reinoud  * If SFAS_DONT_WAIT is set, we have already grabbed the interrupt info
    537       1.1   reinoud  * elsewhere. So we don't have to wait for it.
    538       1.1   reinoud  */
    539       1.1   reinoud 	if (dev->sc_flags & SFAS_DONT_WAIT) {
    540       1.1   reinoud 		dev->sc_flags &= ~SFAS_DONT_WAIT;
    541       1.1   reinoud 		return;
    542       1.1   reinoud 	}
    543       1.1   reinoud 
    544       1.1   reinoud 	rp = dev->sc_fas;
    545       1.1   reinoud 
    546       1.1   reinoud /* Wait for FAS chip to signal an interrupt. */
    547       1.1   reinoud 	while(!(*rp->sfas_status & SFAS_STAT_INTERRUPT_PENDING))
    548       1.1   reinoud 		delay(1);
    549       1.1   reinoud 
    550       1.1   reinoud /* Grab interrupt info from chip. */
    551       1.1   reinoud 	dev->sc_status = *rp->sfas_status;
    552       1.1   reinoud 	dev->sc_interrupt = *rp->sfas_interrupt;
    553       1.1   reinoud 	if (dev->sc_interrupt & SFAS_INT_RESELECTED) {
    554       1.1   reinoud 		dev->sc_resel[0] = *rp->sfas_fifo;
    555       1.1   reinoud 		dev->sc_resel[1] = *rp->sfas_fifo;
    556       1.1   reinoud 	}
    557       1.1   reinoud }
    558       1.1   reinoud 
    559       1.1   reinoud /*
    560       1.1   reinoud  * Transfer info to/from device. sfas_ixfer uses polled IO+sfasiwait so the
    561       1.1   reinoud  * rules that apply to sfasiwait also applies here.
    562       1.1   reinoud  */
    563       1.1   reinoud void
    564      1.17       dsl sfas_ixfer(void *v, int polling)
    565       1.1   reinoud {
    566       1.8       chs 	struct sfas_softc *dev = v;
    567       1.1   reinoud 	sfas_regmap_p	 rp;
    568       1.1   reinoud 	u_char		*buf;
    569       1.1   reinoud 	int		 len, mode, phase;
    570       1.1   reinoud 
    571       1.1   reinoud 	rp = dev->sc_fas;
    572       1.1   reinoud 	buf = dev->sc_buf;
    573       1.1   reinoud 	len = dev->sc_len;
    574       1.1   reinoud 
    575       1.1   reinoud /*
    576       1.1   reinoud  * Decode the scsi phase to determine whether we are reading or writing.
    577       1.1   reinoud  * mode == 1 => READ, mode == 0 => WRITE
    578       1.1   reinoud  */
    579       1.1   reinoud 	phase = dev->sc_status & SFAS_STAT_PHASE_MASK;
    580       1.1   reinoud 	mode = (phase == SFAS_PHASE_DATA_IN);
    581       1.1   reinoud 
    582       1.1   reinoud 	while(len && ((dev->sc_status & SFAS_STAT_PHASE_MASK) == phase))
    583       1.1   reinoud 		if (mode) {
    584       1.1   reinoud 			*rp->sfas_command = SFAS_CMD_TRANSFER_INFO;
    585       1.1   reinoud 
    586       1.1   reinoud 			sfasiwait(dev);
    587       1.1   reinoud 
    588       1.1   reinoud 			*buf++ = *rp->sfas_fifo;
    589       1.1   reinoud 			len--;
    590       1.1   reinoud 		} else {
    591       1.1   reinoud 			len--;
    592       1.1   reinoud 			*rp->sfas_fifo = *buf++;
    593       1.1   reinoud 			*rp->sfas_command = SFAS_CMD_TRANSFER_INFO;
    594       1.1   reinoud 
    595       1.1   reinoud 			sfasiwait(dev);
    596       1.1   reinoud 		}
    597       1.1   reinoud 
    598       1.1   reinoud /* Update buffer pointers to reflect the sent/received data. */
    599       1.1   reinoud 	dev->sc_buf = buf;
    600       1.1   reinoud 	dev->sc_len = len;
    601       1.1   reinoud 
    602       1.1   reinoud /*
    603       1.1   reinoud  * Since the last sfasiwait will be a phase-change, we can't wait for it
    604       1.1   reinoud  * again later, so we have to signal that.
    605       1.1   reinoud  * Since this may be called from an interrupt initiated routine then we
    606       1.1   reinoud  * must call sfasintr again to avoid losing an interrupt. Phew!
    607       1.1   reinoud  */
    608       1.1   reinoud 	if(polling)
    609       1.1   reinoud 		dev->sc_flags |= SFAS_DONT_WAIT;
    610       1.1   reinoud 	else
    611       1.1   reinoud 		sfasintr(dev);
    612       1.1   reinoud }
    613       1.1   reinoud 
    614       1.1   reinoud /*
    615       1.1   reinoud  * Build a Synchronous Data Transfer Request message
    616       1.1   reinoud  */
    617       1.1   reinoud void
    618      1.17       dsl sfas_build_sdtrm(struct sfas_softc *dev, int period, int offset)
    619       1.1   reinoud {
    620       1.1   reinoud 	dev->sc_msg_out[0] = 0x01;
    621       1.1   reinoud 	dev->sc_msg_out[1] = 0x03;
    622       1.1   reinoud 	dev->sc_msg_out[2] = 0x01;
    623       1.1   reinoud 	dev->sc_msg_out[3] = period/4;
    624       1.1   reinoud 	dev->sc_msg_out[4] = offset;
    625       1.1   reinoud 	dev->sc_msg_out_len= 5;
    626       1.1   reinoud }
    627       1.1   reinoud 
    628       1.1   reinoud /*
    629       1.1   reinoud  * Arbitate the scsi bus and select the unit
    630       1.1   reinoud  */
    631       1.1   reinoud int
    632      1.17       dsl sfas_select_unit(struct sfas_softc *dev, short target)
    633       1.1   reinoud {
    634       1.1   reinoud 	sfas_regmap_p	 rp;
    635       1.1   reinoud 	struct nexus	*nexus;
    636       1.1   reinoud 	int		 s, retcode, i;
    637       1.1   reinoud 	u_char		 cmd;
    638       1.1   reinoud 
    639       1.1   reinoud 	s = splbio();	/* Do this at splbio so that we won't be disturbed. */
    640       1.1   reinoud 
    641       1.1   reinoud 	retcode = 0;
    642       1.1   reinoud 
    643       1.1   reinoud 	nexus = &dev->sc_nexus[target];
    644       1.1   reinoud 
    645       1.1   reinoud /*
    646       1.1   reinoud  * Check if the chip is busy. If not the we mark it as so and hope that nobody
    647       1.1   reinoud  * reselects us until we have grabbed the bus.
    648       1.1   reinoud  */
    649       1.1   reinoud 	if (!(dev->sc_flags & SFAS_ACTIVE) && !dev->sc_sel_nexus) {
    650       1.1   reinoud 		dev->sc_flags |= SFAS_ACTIVE;
    651       1.1   reinoud 
    652       1.1   reinoud 		rp = dev->sc_fas;
    653       1.1   reinoud 
    654       1.1   reinoud 		*rp->sfas_syncper = nexus->syncper;
    655       1.1   reinoud 		*rp->sfas_syncoff = nexus->syncoff;
    656       1.1   reinoud 		*rp->sfas_config3 = nexus->config3;
    657       1.1   reinoud 
    658       1.1   reinoud 		*rp->sfas_config1 = dev->sc_config1;
    659       1.1   reinoud 		*rp->sfas_timeout = dev->sc_timeout_val;
    660       1.1   reinoud 		*rp->sfas_dest_id = target;
    661       1.1   reinoud 
    662       1.1   reinoud /* If nobody has stolen the bus, we can send a select command to the chip. */
    663       1.1   reinoud 		if (!(*rp->sfas_status & SFAS_STAT_INTERRUPT_PENDING)) {
    664       1.1   reinoud 			*rp->sfas_fifo = nexus->ID;
    665       1.1   reinoud 			if ((nexus->flags & (SFAS_NF_DO_SDTR | SFAS_NF_RESET))
    666       1.1   reinoud 			    || (dev->sc_msg_out_len != 0))
    667       1.1   reinoud 				cmd = SFAS_CMD_SEL_ATN_STOP;
    668       1.1   reinoud 			else {
    669       1.1   reinoud 				for(i=0; i<nexus->clen; i++)
    670       1.1   reinoud 					*rp->sfas_fifo = nexus->cbuf[i];
    671       1.1   reinoud 
    672       1.1   reinoud 				cmd = SFAS_CMD_SEL_ATN;
    673       1.1   reinoud 			}
    674       1.1   reinoud 
    675       1.1   reinoud 			dev->sc_sel_nexus = nexus;
    676       1.1   reinoud 
    677       1.1   reinoud 			*rp->sfas_command = cmd;
    678       1.1   reinoud 			retcode = 1;
    679       1.1   reinoud 			nexus->flags &= ~SFAS_NF_RETRY_SELECT;
    680       1.1   reinoud 		} else
    681       1.1   reinoud 			nexus->flags |= SFAS_NF_RETRY_SELECT;
    682       1.1   reinoud 	} else
    683       1.1   reinoud 		nexus->flags |= SFAS_NF_RETRY_SELECT;
    684       1.1   reinoud 
    685       1.1   reinoud 	splx(s);
    686       1.1   reinoud 	return(retcode);
    687       1.1   reinoud }
    688       1.1   reinoud 
    689       1.1   reinoud /*
    690       1.1   reinoud  * Grab the nexus if available else return 0.
    691       1.1   reinoud  */
    692       1.1   reinoud struct nexus *
    693      1.17       dsl sfas_arbitate_target(struct sfas_softc *dev, int target)
    694       1.1   reinoud {
    695       1.1   reinoud 	struct nexus	*nexus;
    696       1.1   reinoud 	int		 s;
    697       1.1   reinoud 
    698       1.1   reinoud /*
    699       1.1   reinoud  * This is realy simple. Raise interrupt level to splbio. Grab the nexus and
    700       1.1   reinoud  * leave.
    701       1.1   reinoud  */
    702       1.1   reinoud 	nexus = &dev->sc_nexus[target];
    703       1.1   reinoud 
    704       1.1   reinoud 	s = splbio();
    705       1.1   reinoud 
    706       1.1   reinoud 	if (nexus->flags & SFAS_NF_UNIT_BUSY)
    707       1.1   reinoud 		nexus = 0;
    708       1.1   reinoud 	else
    709       1.1   reinoud 		nexus->flags |= SFAS_NF_UNIT_BUSY;
    710       1.1   reinoud 
    711       1.1   reinoud 	splx(s);
    712       1.1   reinoud 	return(nexus);
    713       1.1   reinoud }
    714       1.1   reinoud 
    715       1.1   reinoud /*
    716      1.10       wiz  * Setup a nexus for use. Initializes command, buffer pointers and DMA chain.
    717       1.1   reinoud  */
    718       1.1   reinoud void
    719      1.17       dsl sfas_setup_nexus(struct sfas_softc *dev, struct nexus *nexus, struct sfas_pending *pendp, unsigned char *cbuf, int clen, unsigned char *buf, int len, int mode)
    720       1.1   reinoud {
    721       1.1   reinoud 	char	sync, target, lun;
    722       1.1   reinoud 
    723       1.1   reinoud 	target = pendp->xs->xs_periph->periph_target;
    724       1.1   reinoud 	lun    = pendp->xs->xs_periph->periph_lun;
    725       1.1   reinoud 
    726       1.1   reinoud /*
    727       1.1   reinoud  * Adopt mode to reflect the config flags.
    728       1.1   reinoud  * If we can't use DMA we can't use synch transfer. Also check the
    729       1.1   reinoud  * sfas_inhibit_xxx[target] flags.
    730       1.1   reinoud  */
    731       1.1   reinoud 	if ((dev->sc_config_flags & (SFAS_NO_SYNCH | SFAS_NO_DMA)) ||
    732       1.1   reinoud 	    sfas_inhibit_sync[(int)target])
    733       1.1   reinoud 		mode &= ~SFAS_SELECT_S;
    734       1.1   reinoud 
    735       1.1   reinoud 	if ((dev->sc_config_flags & SFAS_NO_RESELECT) ||
    736       1.1   reinoud 	    sfas_inhibit_disc[(int)target])
    737       1.1   reinoud 		mode &= ~SFAS_SELECT_R;
    738       1.1   reinoud 
    739       1.1   reinoud 	nexus->xs		= pendp->xs;
    740       1.1   reinoud 
    741       1.1   reinoud /* Setup the nexus struct. */
    742       1.1   reinoud 	nexus->ID	   = ((mode & SFAS_SELECT_R) ? 0xC0 : 0x80) | lun;
    743       1.1   reinoud 	nexus->clen	   = clen;
    744      1.20    cegger 	memcpy(nexus->cbuf, cbuf, nexus->clen);
    745       1.1   reinoud 	nexus->cbuf[1] |= lun << 5;		/* Fix the lun bits */
    746       1.1   reinoud 	nexus->cur_link	   = 0;
    747       1.1   reinoud 	nexus->dma_len	   = 0;
    748       1.1   reinoud 	nexus->dma_buf	   = 0;
    749       1.1   reinoud 	nexus->dma_blk_len = 0;
    750       1.1   reinoud 	nexus->dma_blk_ptr = 0;
    751       1.1   reinoud 	nexus->len	   = len;
    752       1.1   reinoud 	nexus->buf	   = buf;
    753       1.1   reinoud 	nexus->lun_unit	   = (lun << 4) | target;
    754       1.1   reinoud 	nexus->state	   = SFAS_NS_SELECTED;
    755       1.1   reinoud 
    756       1.1   reinoud /* We must keep these flags. All else must be zero. */
    757       1.1   reinoud 	nexus->flags	  &= SFAS_NF_UNIT_BUSY
    758       1.1   reinoud 			   | SFAS_NF_SYNC_TESTED | SFAS_NF_SELECT_ME;
    759       1.1   reinoud 
    760       1.1   reinoud 	if (mode & SFAS_SELECT_I)
    761       1.1   reinoud 		nexus->flags |= SFAS_NF_IMMEDIATE;
    762       1.1   reinoud 	if (mode & SFAS_SELECT_K)
    763       1.1   reinoud 		nexus->flags |= SFAS_NF_RESET;
    764       1.1   reinoud 
    765       1.1   reinoud 	sync  = ((mode & SFAS_SELECT_S) ? 1 : 0);
    766       1.1   reinoud 
    767       1.1   reinoud /* We can't use sync during polled IO. */
    768       1.1   reinoud 	if (sync && (mode & SFAS_SELECT_I))
    769       1.1   reinoud 		sync = 0;
    770       1.1   reinoud 
    771       1.1   reinoud 	if (!sync &&
    772       1.1   reinoud 	    ((nexus->flags & SFAS_NF_SYNC_TESTED) && (nexus->offset != 0))) {
    773       1.1   reinoud 		/*
    774       1.1   reinoud 		 * If the scsi unit is set to synch transfer and we don't want
    775       1.1   reinoud 		 * that, we have to renegotiate.
    776       1.1   reinoud 		 */
    777       1.1   reinoud 
    778       1.1   reinoud 		nexus->flags |= SFAS_NF_DO_SDTR;
    779       1.1   reinoud 		nexus->period = 200;
    780       1.1   reinoud 		nexus->offset = 0;
    781       1.1   reinoud 	} else if (sync && !(nexus->flags & SFAS_NF_SYNC_TESTED)) {
    782       1.1   reinoud 		/*
    783       1.1   reinoud 		 * If the scsi unit is not set to synch transfer and we want
    784       1.1   reinoud 		 * that, we have to negotiate. This should realy base the
    785       1.1   reinoud 		 * period on the clock frequence rather than just check if
    786      1.16     lukem 		 * >25 MHz
    787       1.1   reinoud 		 */
    788       1.1   reinoud 
    789       1.1   reinoud 		nexus->flags |= SFAS_NF_DO_SDTR;
    790       1.1   reinoud 		nexus->period = ((dev->sc_clock_freq>25) ? 100 : 200);
    791       1.1   reinoud 		nexus->offset = 8;
    792       1.1   reinoud 
    793       1.1   reinoud 		/* If the user has a long cable, we want to limit the period */
    794       1.1   reinoud 		if ((nexus->period == 100) &&
    795       1.1   reinoud 		    (dev->sc_config_flags & SFAS_SLOW_CABLE))
    796       1.1   reinoud 			nexus->period = 200;
    797       1.1   reinoud 	}
    798       1.1   reinoud 
    799       1.1   reinoud /*
    800      1.10       wiz  * Fake a DMA-block for polled IO. This way we can use the same code to handle
    801       1.1   reinoud  * reselection. Much nicer this way.
    802       1.1   reinoud  */
    803       1.1   reinoud 	if ((mode & SFAS_SELECT_I) || (dev->sc_config_flags & SFAS_NO_DMA)) {
    804       1.8       chs 		nexus->dma[0].ptr = buf;
    805       1.1   reinoud 		nexus->dma[0].len = len;
    806       1.1   reinoud 		nexus->dma[0].flg = SFAS_CHAIN_PRG;
    807       1.1   reinoud 		nexus->max_link   = 1;
    808       1.1   reinoud 	} else {
    809       1.1   reinoud 		nexus->max_link = dev->sc_build_dma_chain(dev, nexus->dma,
    810       1.1   reinoud 							  buf, len);
    811       1.1   reinoud 	}
    812       1.1   reinoud 
    813       1.1   reinoud /* Flush the caches. */
    814       1.1   reinoud 
    815       1.1   reinoud 	if (len && !(mode & SFAS_SELECT_I))
    816       1.4   thorpej 		cpu_dcache_wbinv_range((vm_offset_t)buf, len);
    817       1.1   reinoud }
    818       1.1   reinoud 
    819       1.1   reinoud int
    820      1.17       dsl sfasselect(struct sfas_softc *dev, struct sfas_pending *pendp, unsigned char *cbuf, int clen, unsigned char *buf, int len, int mode)
    821       1.1   reinoud {
    822       1.1   reinoud 	struct nexus	*nexus;
    823       1.1   reinoud 
    824       1.1   reinoud /* Get the nexus struct. */
    825       1.1   reinoud 	nexus = sfas_arbitate_target(dev, pendp->xs->xs_periph->periph_target);
    826       1.1   reinoud 	if (nexus == NULL)
    827       1.1   reinoud 		return(0);
    828       1.1   reinoud 
    829       1.1   reinoud /* Setup the nexus struct. */
    830       1.1   reinoud 	sfas_setup_nexus(dev, nexus, pendp, cbuf, clen, buf, len, mode);
    831       1.1   reinoud 
    832       1.1   reinoud /* Post it to the interrupt machine. */
    833       1.1   reinoud 	sfas_select_unit(dev, pendp->xs->xs_periph->periph_target);
    834       1.1   reinoud 
    835       1.1   reinoud 	return(1);
    836       1.1   reinoud }
    837       1.1   reinoud 
    838       1.1   reinoud void
    839      1.17       dsl sfasgo(struct sfas_softc *dev, struct sfas_pending *pendp)
    840       1.1   reinoud {
    841       1.1   reinoud 	int	 s;
    842       1.1   reinoud 	char	*buf;
    843       1.1   reinoud 
    844       1.1   reinoud 	buf    = pendp->xs->data;
    845       1.1   reinoud 
    846       1.1   reinoud 	if (sfasselect(dev, pendp, (char *)pendp->xs->cmd, pendp->xs->cmdlen,
    847       1.1   reinoud 		      buf, pendp->xs->datalen, SFAS_SELECT_RS)) {
    848       1.1   reinoud 		/*
    849       1.1   reinoud 		 * We got the command going so the sfas_pending struct is now
    850       1.1   reinoud 		 * free to reuse.
    851       1.1   reinoud 		 */
    852       1.1   reinoud 
    853       1.1   reinoud 		s = splbio();
    854       1.1   reinoud 		TAILQ_INSERT_TAIL(&dev->sc_xs_free, pendp, link);
    855       1.1   reinoud 		splx(s);
    856       1.1   reinoud 	} else {
    857       1.1   reinoud 		/*
    858       1.1   reinoud 		 * We couldn't make the command fly so we have to wait. The
    859       1.1   reinoud 		 * struct MUST be inserted at the head to keep the order of
    860       1.1   reinoud 		 * the commands.
    861       1.1   reinoud 		 */
    862       1.1   reinoud 
    863       1.1   reinoud 		s = splbio();
    864       1.1   reinoud 		TAILQ_INSERT_HEAD(&dev->sc_xs_pending, pendp, link);
    865       1.1   reinoud 		splx(s);
    866       1.1   reinoud 	}
    867       1.1   reinoud 
    868       1.1   reinoud 	return;
    869       1.1   reinoud }
    870       1.1   reinoud 
    871       1.1   reinoud /*
    872       1.1   reinoud  * Part one of the interrupt machine. Error checks and reselection test.
    873       1.1   reinoud  * We don't know if we have an active nexus here!
    874       1.1   reinoud  */
    875       1.1   reinoud int
    876      1.17       dsl sfas_pretests(struct sfas_softc *dev, sfas_regmap_p rp)
    877       1.1   reinoud {
    878       1.1   reinoud 	struct nexus	*nexus;
    879       1.1   reinoud 	int		 i, s;
    880       1.1   reinoud 
    881       1.1   reinoud 	if (dev->sc_interrupt & SFAS_INT_SCSI_RESET_DETECTED) {
    882       1.1   reinoud 		/*
    883       1.1   reinoud 		 * Cleanup and notify user. Lets hope that this is all we
    884       1.1   reinoud 		 * have to do
    885       1.1   reinoud 		 */
    886       1.1   reinoud 
    887       1.1   reinoud 		for(i=0; i<8; i++) {
    888       1.1   reinoud 			if (dev->sc_nexus[i].xs)
    889       1.1   reinoud 				sfas_scsidone(dev, dev->sc_nexus[i].xs, -2);
    890       1.1   reinoud 
    891       1.1   reinoud 			sfas_init_nexus(dev, &dev->sc_nexus[i]);
    892       1.1   reinoud 		}
    893       1.1   reinoud 		printf("sfasintr: SCSI-RESET detected!");
    894       1.1   reinoud 		return(-1);
    895       1.1   reinoud 	}
    896       1.1   reinoud 
    897       1.1   reinoud 	if (dev->sc_interrupt & SFAS_INT_ILLEGAL_COMMAND) {
    898       1.1   reinoud 		/* Something went terrible wrong! Dump some data and panic! */
    899       1.1   reinoud 
    900       1.1   reinoud 		printf("FIFO:");
    901       1.1   reinoud 		while(*rp->sfas_fifo_flags & SFAS_FIFO_COUNT_MASK)
    902       1.1   reinoud 			printf(" %x", *rp->sfas_fifo);
    903       1.1   reinoud 		printf("\n");
    904       1.1   reinoud 
    905       1.1   reinoud 		printf("CMD: %x\n", *rp->sfas_command);
    906       1.1   reinoud 		panic("sfasintr: ILLEGAL COMMAND!");
    907       1.1   reinoud 	}
    908       1.1   reinoud 
    909       1.1   reinoud 	if (dev->sc_interrupt & SFAS_INT_RESELECTED) {
    910       1.1   reinoud 		/* We were reselected. Set the chip as busy */
    911       1.1   reinoud 
    912       1.1   reinoud 		s = splbio();
    913       1.1   reinoud 		dev->sc_flags |= SFAS_ACTIVE;
    914       1.1   reinoud 		if (dev->sc_sel_nexus) {
    915       1.1   reinoud 			dev->sc_sel_nexus->flags |= SFAS_NF_SELECT_ME;
    916       1.1   reinoud 			dev->sc_sel_nexus = 0;
    917       1.1   reinoud 		}
    918       1.1   reinoud 		splx(s);
    919       1.1   reinoud 
    920       1.1   reinoud 		if (dev->sc_units_disconnected) {
    921       1.1   reinoud 			/* Find out who reselected us. */
    922       1.1   reinoud 
    923       1.1   reinoud 			dev->sc_resel[0] &= ~(1<<dev->sc_host_id);
    924       1.1   reinoud 
    925       1.1   reinoud 			for(i=0; i<8; i++)
    926       1.1   reinoud 				if (dev->sc_resel[0] & (1<<i))
    927       1.1   reinoud 					break;
    928       1.1   reinoud 
    929       1.1   reinoud 			if (i == 8)
    930       1.1   reinoud 				panic("Illegal reselection!");
    931       1.1   reinoud 
    932       1.1   reinoud 			if (dev->sc_nexus[i].state == SFAS_NS_DISCONNECTED) {
    933       1.1   reinoud 				/*
    934       1.1   reinoud 				 * This unit had disconnected, so we reconnect
    935       1.1   reinoud 				 * it.
    936       1.1   reinoud 				 */
    937       1.1   reinoud 
    938       1.1   reinoud 				dev->sc_cur_nexus = &dev->sc_nexus[i];
    939       1.1   reinoud 				nexus = dev->sc_cur_nexus;
    940       1.1   reinoud 
    941       1.1   reinoud 				*rp->sfas_syncper = nexus->syncper;
    942       1.1   reinoud 				*rp->sfas_syncoff = nexus->syncoff;
    943       1.1   reinoud 				*rp->sfas_config3 = nexus->config3;
    944       1.1   reinoud 
    945       1.1   reinoud 				*rp->sfas_dest_id = i & 7;
    946       1.1   reinoud 
    947       1.1   reinoud 				dev->sc_units_disconnected--;
    948       1.1   reinoud 				dev->sc_msg_in_len= 0;
    949       1.1   reinoud 
    950       1.1   reinoud 				/* Restore active pointers. */
    951       1.1   reinoud 				sfas_restore_pointers(dev);
    952       1.1   reinoud 
    953       1.1   reinoud 				nexus->state = SFAS_NS_RESELECTED;
    954       1.1   reinoud 
    955       1.1   reinoud 				*rp->sfas_command = SFAS_CMD_MESSAGE_ACCEPTED;
    956       1.1   reinoud 
    957       1.1   reinoud 				return(1);
    958       1.1   reinoud 			}
    959       1.1   reinoud 		}
    960       1.1   reinoud 
    961       1.1   reinoud 		/* Somehow we got an illegal reselection. Dump and panic. */
    962       1.1   reinoud 		printf("sfasintr: resel[0] %x resel[1] %x disconnected %d\n",
    963       1.1   reinoud 		       dev->sc_resel[0], dev->sc_resel[1],
    964       1.1   reinoud 		       dev->sc_units_disconnected);
    965       1.1   reinoud 		panic("sfasintr: Unexpected reselection!");
    966       1.1   reinoud 	}
    967       1.1   reinoud 
    968       1.1   reinoud 	return(0);
    969       1.1   reinoud }
    970       1.1   reinoud 
    971       1.1   reinoud /*
    972       1.1   reinoud  * Part two of the interrupt machine. Handle disconnection and post command
    973       1.1   reinoud  * processing. We know that we have an active nexus here.
    974       1.1   reinoud  */
    975       1.1   reinoud int
    976      1.17       dsl sfas_midaction(struct sfas_softc *dev, sfas_regmap_p rp, struct nexus *nexus)
    977       1.1   reinoud {
    978       1.1   reinoud 	int	i, left, len, s;
    979       1.1   reinoud 	u_char	status, msg;
    980       1.1   reinoud 
    981       1.1   reinoud 	if (dev->sc_interrupt & SFAS_INT_DISCONNECT) {
    982       1.1   reinoud 		s = splbio();
    983       1.1   reinoud 		dev->sc_cur_nexus = 0;
    984       1.1   reinoud 
    985       1.1   reinoud 		/* Mark chip as busy and clean up the chip FIFO. */
    986       1.1   reinoud 		dev->sc_flags &= ~SFAS_ACTIVE;
    987       1.1   reinoud 		*rp->sfas_command = SFAS_CMD_FLUSH_FIFO;
    988       1.1   reinoud 
    989       1.1   reinoud 		/* Let the nexus state reflect what we have to do. */
    990       1.1   reinoud 		switch(nexus->state) {
    991       1.1   reinoud 		case SFAS_NS_SELECTED:
    992       1.1   reinoud 			dev->sc_sel_nexus = 0;
    993       1.1   reinoud 			nexus->flags &= ~SFAS_NF_SELECT_ME;
    994       1.1   reinoud 
    995       1.1   reinoud 			/*
    996       1.1   reinoud 			 * We were trying to select the unit. Probably no unit
    997       1.1   reinoud 			 * at this ID.
    998       1.1   reinoud 			 */
    999       1.1   reinoud 			nexus->xs->resid = dev->sc_len;
   1000       1.1   reinoud 
   1001       1.1   reinoud 			nexus->status = -2;
   1002       1.1   reinoud 			nexus->flags &= ~SFAS_NF_UNIT_BUSY;
   1003       1.1   reinoud 			nexus->state = SFAS_NS_FINISHED;
   1004       1.1   reinoud 			break;
   1005       1.1   reinoud 
   1006       1.1   reinoud 		case SFAS_NS_DONE:
   1007       1.1   reinoud 			/* All done. */
   1008       1.1   reinoud 			nexus->xs->resid = dev->sc_len;
   1009       1.1   reinoud 
   1010       1.1   reinoud 			nexus->flags &= ~SFAS_NF_UNIT_BUSY;
   1011       1.1   reinoud 			nexus->state  = SFAS_NS_FINISHED;
   1012       1.1   reinoud 			dev->sc_led(dev, 0);
   1013       1.1   reinoud 			break;
   1014       1.1   reinoud 
   1015       1.1   reinoud 		case SFAS_NS_DISCONNECTING:
   1016       1.1   reinoud 			/*
   1017       1.1   reinoud 			 * We have received a DISCONNECT message, so we are
   1018       1.1   reinoud 			 * doing a normal disconnection.
   1019       1.1   reinoud 			 */
   1020       1.1   reinoud 			nexus->state = SFAS_NS_DISCONNECTED;
   1021       1.1   reinoud 
   1022       1.1   reinoud 			dev->sc_units_disconnected++;
   1023       1.1   reinoud 			break;
   1024       1.1   reinoud 
   1025       1.1   reinoud 		case SFAS_NS_RESET:
   1026       1.1   reinoud 			/*
   1027       1.1   reinoud 			 * We were reseting this SCSI-unit. Clean up the
   1028       1.1   reinoud 			 * nexus struct.
   1029       1.1   reinoud 			 */
   1030       1.1   reinoud 			dev->sc_led(dev, 0);
   1031       1.1   reinoud 			sfas_init_nexus(dev, nexus);
   1032       1.1   reinoud 			break;
   1033       1.1   reinoud 
   1034       1.1   reinoud 		default:
   1035       1.1   reinoud 			/*
   1036       1.1   reinoud 			 * Unexpected disconnection! Cleanup and exit. This
   1037       1.1   reinoud 			 * shouldn't cause any problems.
   1038       1.1   reinoud 			 */
   1039       1.1   reinoud 			printf("sfasintr: Unexpected disconnection\n");
   1040       1.1   reinoud 			printf("sfasintr: u %x s %d p %d f %x c %x\n",
   1041       1.1   reinoud 			       nexus->lun_unit, nexus->state,
   1042       1.1   reinoud 			       dev->sc_status & SFAS_STAT_PHASE_MASK,
   1043       1.1   reinoud 			       nexus->flags, nexus->cbuf[0]);
   1044       1.1   reinoud 
   1045       1.1   reinoud 			nexus->xs->resid = dev->sc_len;
   1046       1.1   reinoud 
   1047       1.1   reinoud 			nexus->flags &= ~SFAS_NF_UNIT_BUSY;
   1048       1.1   reinoud 			nexus->state = SFAS_NS_FINISHED;
   1049       1.1   reinoud 			nexus->status = -3;
   1050       1.1   reinoud 
   1051       1.1   reinoud 			dev->sc_led(dev, 0);
   1052       1.1   reinoud 			break;
   1053       1.1   reinoud 		}
   1054       1.1   reinoud 
   1055       1.1   reinoud 		/*
   1056       1.1   reinoud 		 * If we have disconnected units, we MUST enable reselection
   1057       1.1   reinoud 		 * within 250ms.
   1058       1.1   reinoud 		 */
   1059       1.1   reinoud 		if (dev->sc_units_disconnected &&
   1060       1.1   reinoud 		    !(dev->sc_flags & SFAS_ACTIVE))
   1061       1.1   reinoud 			*rp->sfas_command = SFAS_CMD_ENABLE_RESEL;
   1062       1.1   reinoud 
   1063       1.1   reinoud 		splx(s);
   1064       1.1   reinoud 
   1065       1.1   reinoud 		/* Select the first pre-initialized nexus we find. */
   1066       1.1   reinoud 		for(i=0; i<8; i++)
   1067       1.1   reinoud 			if (dev->sc_nexus[i].flags & (SFAS_NF_SELECT_ME | SFAS_NF_RETRY_SELECT))
   1068       1.1   reinoud 				if (sfas_select_unit(dev, i) == 2)
   1069       1.1   reinoud 					break;
   1070       1.1   reinoud 
   1071       1.1   reinoud 		/* We are done with this nexus! */
   1072       1.1   reinoud 		if (nexus->state == SFAS_NS_FINISHED)
   1073       1.1   reinoud 			sfas_scsidone(dev, nexus->xs, nexus->status);
   1074       1.1   reinoud 
   1075       1.1   reinoud 		return(1);
   1076       1.1   reinoud 	}
   1077       1.1   reinoud 
   1078       1.1   reinoud 	switch(nexus->state) {
   1079       1.1   reinoud 	case SFAS_NS_SELECTED:
   1080       1.1   reinoud 		dev->sc_cur_nexus = nexus;
   1081       1.1   reinoud 		dev->sc_sel_nexus = 0;
   1082       1.1   reinoud 
   1083       1.1   reinoud 		nexus->flags &= ~SFAS_NF_SELECT_ME;
   1084       1.1   reinoud 
   1085       1.1   reinoud 		/*
   1086       1.1   reinoud 		 * We have selected a unit. Setup chip, restore pointers and
   1087       1.1   reinoud 		 * light the led.
   1088       1.1   reinoud 		 */
   1089       1.1   reinoud 		*rp->sfas_syncper = nexus->syncper;
   1090       1.1   reinoud 		*rp->sfas_syncoff = nexus->syncoff;
   1091       1.1   reinoud 		*rp->sfas_config3 = nexus->config3;
   1092       1.1   reinoud 
   1093       1.1   reinoud 		sfas_restore_pointers(dev);
   1094       1.1   reinoud 
   1095       1.1   reinoud 		nexus->status	= 0xFF;
   1096       1.1   reinoud 		dev->sc_msg_in[0] = 0xFF;
   1097       1.1   reinoud 		dev->sc_msg_in_len= 0;
   1098       1.1   reinoud 
   1099       1.1   reinoud 		dev->sc_led(dev, 1);
   1100       1.1   reinoud 
   1101       1.1   reinoud 		break;
   1102       1.1   reinoud 
   1103       1.1   reinoud 	case SFAS_NS_DATA_IN:
   1104       1.1   reinoud 	case SFAS_NS_DATA_OUT:
   1105       1.1   reinoud 		/* We have transfered data. */
   1106       1.1   reinoud 		if (dev->sc_dma_len)
   1107       1.1   reinoud 			if (dev->sc_cur_link < dev->sc_max_link) {
   1108       1.1   reinoud 				/*
   1109      1.10       wiz 				 * Clean up DMA and at the same time get how
   1110       1.1   reinoud 				 * many bytes that were NOT transfered.
   1111       1.1   reinoud 				 */
   1112       1.1   reinoud 			  left = dev->sc_setup_dma(dev, 0, 0, SFAS_DMA_CLEAR);
   1113       1.1   reinoud 			  len  = dev->sc_dma_len;
   1114       1.1   reinoud 
   1115       1.1   reinoud 			  if (nexus->state == SFAS_NS_DATA_IN) {
   1116       1.1   reinoud 			    /*
   1117       1.1   reinoud 			     * If we were bumping we may have had an odd length
   1118       1.1   reinoud 			     * which means that there may be bytes left in the
   1119       1.1   reinoud 			     * fifo. We also need to move the data from the
   1120       1.1   reinoud 			     * bump buffer to the actual memory.
   1121       1.1   reinoud 			     */
   1122       1.1   reinoud 			    if (dev->sc_dma_buf == dev->sc_bump_pa)
   1123       1.1   reinoud 			    {
   1124       1.1   reinoud 			      while((*rp->sfas_fifo_flags&SFAS_FIFO_COUNT_MASK)
   1125       1.1   reinoud 				    && left)
   1126       1.1   reinoud 				dev->sc_bump_va[len-(left--)] = *rp->sfas_fifo;
   1127       1.1   reinoud 
   1128      1.20    cegger 			      memcpy(dev->sc_buf, dev->sc_bump_va, len-left);
   1129       1.1   reinoud 			    }
   1130       1.1   reinoud 			  } else {
   1131       1.1   reinoud 			    /* Count any unsent bytes and flush them. */
   1132       1.1   reinoud 			    left+= *rp->sfas_fifo_flags & SFAS_FIFO_COUNT_MASK;
   1133       1.1   reinoud 			    *rp->sfas_command = SFAS_CMD_FLUSH_FIFO;
   1134       1.1   reinoud 			  }
   1135       1.1   reinoud 
   1136       1.1   reinoud 			  /*
   1137       1.1   reinoud 			   * Update pointers/length to reflect the transfered
   1138       1.1   reinoud 			   * data.
   1139       1.1   reinoud 			   */
   1140       1.1   reinoud 			  dev->sc_len -= len-left;
   1141       1.1   reinoud 			  dev->sc_buf += len-left;
   1142       1.1   reinoud 
   1143       1.8       chs 			  dev->sc_dma_buf = (char *)dev->sc_dma_buf + len-left;
   1144       1.8       chs 			  dev->sc_dma_len = left;
   1145       1.1   reinoud 
   1146       1.8       chs 			  dev->sc_dma_blk_ptr = (char *)dev->sc_dma_blk_ptr +
   1147       1.8       chs 				  len-left;
   1148       1.1   reinoud 			  dev->sc_dma_blk_len -= len-left;
   1149       1.1   reinoud 
   1150       1.1   reinoud 			  /*
   1151      1.10       wiz 			   * If it was the end of a DMA block, we select the
   1152       1.1   reinoud 			   * next to begin with.
   1153       1.1   reinoud 			   */
   1154       1.1   reinoud 			  if (!dev->sc_dma_blk_len)
   1155       1.1   reinoud 			    dev->sc_cur_link++;
   1156       1.1   reinoud 			}
   1157       1.1   reinoud 		break;
   1158       1.1   reinoud 
   1159       1.1   reinoud 	case SFAS_NS_STATUS:
   1160       1.1   reinoud 		/*
   1161       1.1   reinoud 		 * If we were not sensing, grab the status byte. If we were
   1162       1.1   reinoud 		 * sensing and we got a bad status, let the user know.
   1163       1.1   reinoud 		 */
   1164       1.1   reinoud 
   1165       1.1   reinoud 		status = *rp->sfas_fifo;
   1166       1.1   reinoud 		msg = *rp->sfas_fifo;
   1167       1.1   reinoud 
   1168       1.1   reinoud 		nexus->status = status;
   1169       1.1   reinoud 		if (status != 0)
   1170       1.1   reinoud 			nexus->status = -1;
   1171       1.1   reinoud 
   1172       1.1   reinoud 		/*
   1173       1.1   reinoud 		 * Preload the command complete message. Handeled in
   1174       1.1   reinoud 		 * sfas_postaction.
   1175       1.1   reinoud 		 */
   1176       1.1   reinoud 		dev->sc_msg_in[0] = msg;
   1177       1.1   reinoud 		dev->sc_msg_in_len = 1;
   1178       1.1   reinoud 		nexus->flags |= SFAS_NF_HAS_MSG;
   1179       1.1   reinoud 		break;
   1180       1.1   reinoud 
   1181       1.1   reinoud 	default:
   1182       1.1   reinoud 		break;
   1183       1.1   reinoud 	}
   1184       1.1   reinoud 
   1185       1.1   reinoud 	return(0);
   1186       1.1   reinoud }
   1187       1.1   reinoud 
   1188       1.1   reinoud /*
   1189       1.1   reinoud  * Part three of the interrupt machine. Handle phase changes (and repeated
   1190       1.1   reinoud  * phase passes). We know that we have an active nexus here.
   1191       1.1   reinoud  */
   1192       1.1   reinoud int
   1193      1.17       dsl sfas_postaction(struct sfas_softc *dev, sfas_regmap_p rp, struct nexus *nexus)
   1194       1.1   reinoud {
   1195       1.1   reinoud 	int	i, len;
   1196       1.1   reinoud 	u_char	cmd;
   1197       1.1   reinoud 	short	offset, period;
   1198       1.1   reinoud 
   1199       1.1   reinoud 	cmd = 0;
   1200       1.1   reinoud 
   1201       1.1   reinoud 	switch(dev->sc_status & SFAS_STAT_PHASE_MASK) {
   1202       1.1   reinoud 	case SFAS_PHASE_DATA_OUT:
   1203       1.1   reinoud 	case SFAS_PHASE_DATA_IN:
   1204       1.1   reinoud 		if ((dev->sc_status & SFAS_STAT_PHASE_MASK) ==
   1205       1.1   reinoud 		    SFAS_PHASE_DATA_OUT)
   1206       1.1   reinoud 			nexus->state = SFAS_NS_DATA_OUT;
   1207       1.1   reinoud 		else
   1208       1.1   reinoud 			nexus->state = SFAS_NS_DATA_IN;
   1209       1.1   reinoud 
   1210       1.1   reinoud 		/* Make DMA ready to accept new data. Load active pointers
   1211       1.1   reinoud 		 * from the DMA block. */
   1212       1.1   reinoud 		dev->sc_setup_dma(dev, 0, 0, SFAS_DMA_CLEAR);
   1213       1.1   reinoud 		if (dev->sc_cur_link < dev->sc_max_link) {
   1214       1.1   reinoud 		  if (!dev->sc_dma_blk_len) {
   1215       1.1   reinoud 		    dev->sc_dma_blk_ptr = dev->sc_chain[dev->sc_cur_link].ptr;
   1216       1.1   reinoud 		    dev->sc_dma_blk_len = dev->sc_chain[dev->sc_cur_link].len;
   1217       1.1   reinoud 		    dev->sc_dma_blk_flg = dev->sc_chain[dev->sc_cur_link].flg;
   1218       1.1   reinoud 		  }
   1219       1.1   reinoud 
   1220       1.1   reinoud 		  /* We should use polled IO here. */
   1221       1.1   reinoud 		  if (dev->sc_dma_blk_flg == SFAS_CHAIN_PRG) {
   1222       1.1   reinoud 			dev->sc_ixfer(dev, nexus->xs->xs_control & XS_CTL_POLL);
   1223       1.1   reinoud 			dev->sc_cur_link++;
   1224       1.1   reinoud 			dev->sc_dma_len = 0;
   1225       1.1   reinoud 			break;
   1226       1.1   reinoud 		  }
   1227       1.1   reinoud 		  else if (dev->sc_dma_blk_flg == SFAS_CHAIN_BUMP)
   1228       1.1   reinoud 			len = dev->sc_dma_blk_len;
   1229       1.1   reinoud 		  else
   1230       1.1   reinoud 			len = dev->sc_need_bump(dev, dev->sc_dma_blk_ptr,
   1231       1.1   reinoud 						dev->sc_dma_blk_len);
   1232       1.1   reinoud 
   1233       1.1   reinoud 		  /*
   1234       1.1   reinoud 		   * If len != 0 we must bump the data, else we just DMA it
   1235       1.1   reinoud 		   * straight into memory.
   1236       1.1   reinoud 		   */
   1237       1.1   reinoud 		  if (len) {
   1238       1.1   reinoud 			dev->sc_dma_buf = dev->sc_bump_pa;
   1239       1.1   reinoud 			dev->sc_dma_len = len;
   1240       1.1   reinoud 
   1241       1.1   reinoud 			if (nexus->state == SFAS_NS_DATA_OUT)
   1242      1.20    cegger 			  memcpy(dev->sc_bump_va, dev->sc_buf, dev->sc_dma_len);
   1243       1.1   reinoud 		  } else {
   1244       1.1   reinoud 			dev->sc_dma_buf = dev->sc_dma_blk_ptr;
   1245       1.1   reinoud 			dev->sc_dma_len = dev->sc_dma_blk_len;
   1246       1.1   reinoud 		  }
   1247       1.1   reinoud 
   1248      1.13       wiz 		  /* Load DMA with address and length of transfer. */
   1249       1.1   reinoud 		  dev->sc_setup_dma(dev, dev->sc_dma_buf, dev->sc_dma_len,
   1250       1.1   reinoud 				    ((nexus->state == SFAS_NS_DATA_OUT) ?
   1251       1.1   reinoud 				     SFAS_DMA_WRITE : SFAS_DMA_READ));
   1252       1.1   reinoud 
   1253       1.1   reinoud /*		  printf("Using DMA !!!!\n");*/
   1254       1.1   reinoud 		  cmd = SFAS_CMD_TRANSFER_INFO | SFAS_CMD_DMA;
   1255       1.1   reinoud 		} else {
   1256       1.1   reinoud 			/*
   1257       1.1   reinoud 			 * Hmmm, the unit wants more info than we have or has
   1258       1.1   reinoud 			 * more than we want. Let the chip handle that.
   1259       1.1   reinoud 			 */
   1260       1.1   reinoud 
   1261       1.1   reinoud 			*rp->sfas_tc_low = 0; /* was 256 but this does not make sense */
   1262       1.1   reinoud 			*rp->sfas_tc_mid = 1;
   1263       1.1   reinoud 			*rp->sfas_tc_high = 0;
   1264       1.1   reinoud 			cmd = SFAS_CMD_TRANSFER_PAD;
   1265       1.1   reinoud 		}
   1266       1.1   reinoud 		break;
   1267       1.1   reinoud 
   1268       1.1   reinoud 	case SFAS_PHASE_COMMAND:
   1269       1.1   reinoud 		/* The scsi unit wants the command, send it. */
   1270       1.1   reinoud 		nexus->state = SFAS_NS_SVC;
   1271       1.1   reinoud 
   1272       1.1   reinoud 		*rp->sfas_command = SFAS_CMD_FLUSH_FIFO;
   1273       1.1   reinoud 		for(i=0; i<5; i++);
   1274       1.1   reinoud 
   1275       1.1   reinoud 		for(i=0; i<nexus->clen; i++)
   1276       1.1   reinoud 			*rp->sfas_fifo = nexus->cbuf[i];
   1277       1.1   reinoud 		cmd = SFAS_CMD_TRANSFER_INFO;
   1278       1.1   reinoud 		break;
   1279       1.1   reinoud 
   1280       1.1   reinoud 	case SFAS_PHASE_STATUS:
   1281       1.1   reinoud 		/*
   1282       1.1   reinoud 		 * We've got status phase. Request status and command
   1283       1.1   reinoud 		 * complete message.
   1284       1.1   reinoud 		 */
   1285       1.1   reinoud 		nexus->state = SFAS_NS_STATUS;
   1286       1.1   reinoud 		cmd = SFAS_CMD_COMMAND_COMPLETE;
   1287       1.1   reinoud 		break;
   1288       1.1   reinoud 
   1289       1.1   reinoud 	case SFAS_PHASE_MESSAGE_OUT:
   1290       1.1   reinoud 		/*
   1291       1.1   reinoud 		 * Either the scsi unit wants us to send a message or we have
   1292       1.1   reinoud 		 * asked for it by seting the ATN bit.
   1293       1.1   reinoud 		 */
   1294       1.1   reinoud 		nexus->state = SFAS_NS_MSG_OUT;
   1295       1.1   reinoud 
   1296       1.1   reinoud 		*rp->sfas_command = SFAS_CMD_FLUSH_FIFO;
   1297       1.1   reinoud 
   1298       1.1   reinoud 		if (nexus->flags & SFAS_NF_DO_SDTR) {
   1299       1.1   reinoud 			/* Send a Synchronous Data Transfer Request. */
   1300       1.1   reinoud 
   1301       1.1   reinoud 			sfas_build_sdtrm(dev, nexus->period, nexus->offset);
   1302       1.1   reinoud 			nexus->flags |= SFAS_NF_SDTR_SENT;
   1303       1.1   reinoud 			nexus->flags &= ~SFAS_NF_DO_SDTR;
   1304       1.1   reinoud 		} else if (nexus->flags & SFAS_NF_RESET) {
   1305       1.1   reinoud 			/* Send a reset scsi unit message. */
   1306       1.1   reinoud 
   1307       1.1   reinoud 			dev->sc_msg_out[0] = 0x0C;
   1308       1.1   reinoud 			dev->sc_msg_out_len = 1;
   1309       1.1   reinoud 			nexus->state = SFAS_NS_RESET;
   1310       1.1   reinoud 			nexus->flags &= ~SFAS_NF_RESET;
   1311       1.1   reinoud 		} else if (dev->sc_msg_out_len == 0) {
   1312       1.1   reinoud 			/* Don't know what to send so we send a NOP message. */
   1313       1.1   reinoud 
   1314       1.1   reinoud 			dev->sc_msg_out[0] = 0x08;
   1315       1.1   reinoud 			dev->sc_msg_out_len = 1;
   1316       1.1   reinoud 		}
   1317       1.1   reinoud 
   1318       1.1   reinoud 		cmd = SFAS_CMD_TRANSFER_INFO;
   1319       1.1   reinoud 
   1320       1.1   reinoud 		for(i=0; i<dev->sc_msg_out_len; i++)
   1321       1.1   reinoud 			*rp->sfas_fifo = dev->sc_msg_out[i];
   1322       1.1   reinoud 		dev->sc_msg_out_len = 0;
   1323       1.1   reinoud 
   1324       1.1   reinoud 		break;
   1325       1.1   reinoud 
   1326       1.1   reinoud 	case SFAS_PHASE_MESSAGE_IN:
   1327       1.1   reinoud 		/* Receive a message from the scsi unit. */
   1328       1.1   reinoud 		nexus->state = SFAS_NS_MSG_IN;
   1329       1.1   reinoud 
   1330       1.1   reinoud 		while(!(nexus->flags & SFAS_NF_HAS_MSG)) {
   1331       1.1   reinoud 			*rp->sfas_command = SFAS_CMD_TRANSFER_INFO;
   1332       1.1   reinoud 			sfasiwait(dev);
   1333       1.1   reinoud 
   1334       1.1   reinoud 			dev->sc_msg_in[dev->sc_msg_in_len++] = *rp->sfas_fifo;
   1335       1.1   reinoud 
   1336       1.1   reinoud 			/* Check if we got all the bytes in the message. */
   1337       1.1   reinoud 			if (dev->sc_msg_in[0] >= 0x80)       ;
   1338       1.1   reinoud 			else if (dev->sc_msg_in[0] >= 0x30)  ;
   1339       1.1   reinoud 			else if (((dev->sc_msg_in[0] >= 0x20) &&
   1340       1.1   reinoud 				  (dev->sc_msg_in_len == 2)) ||
   1341       1.1   reinoud 				 ((dev->sc_msg_in[0] != 0x01) &&
   1342       1.1   reinoud 				  (dev->sc_msg_in_len == 1))) {
   1343       1.1   reinoud 				nexus->flags |= SFAS_NF_HAS_MSG;
   1344       1.1   reinoud 				break;
   1345       1.1   reinoud 			} else {
   1346       1.1   reinoud 			  if (dev->sc_msg_in_len >= 2)
   1347       1.1   reinoud 			    if ((dev->sc_msg_in[1]+2) == dev->sc_msg_in_len) {
   1348       1.1   reinoud 				nexus->flags |= SFAS_NF_HAS_MSG;
   1349       1.1   reinoud 				break;
   1350       1.1   reinoud 			    }
   1351       1.1   reinoud 			}
   1352       1.1   reinoud 
   1353       1.1   reinoud 			*rp->sfas_command = SFAS_CMD_MESSAGE_ACCEPTED;
   1354       1.1   reinoud 			sfasiwait(dev);
   1355       1.1   reinoud 
   1356       1.1   reinoud 			if ((dev->sc_status & SFAS_STAT_PHASE_MASK) !=
   1357       1.1   reinoud 			    SFAS_PHASE_MESSAGE_IN)
   1358       1.1   reinoud 				break;
   1359       1.1   reinoud 		}
   1360       1.1   reinoud 
   1361       1.1   reinoud 		cmd = SFAS_CMD_MESSAGE_ACCEPTED;
   1362       1.1   reinoud 		if (nexus->flags & SFAS_NF_HAS_MSG) {
   1363       1.1   reinoud 			/* We have a message. Decode it. */
   1364       1.1   reinoud 
   1365       1.1   reinoud 			switch(dev->sc_msg_in[0]) {
   1366       1.1   reinoud 			case 0x00:	/* COMMAND COMPLETE */
   1367       1.1   reinoud 				nexus->state = SFAS_NS_DONE;
   1368       1.1   reinoud 				break;
   1369       1.1   reinoud 			case 0x04:	/* DISCONNECT */
   1370       1.1   reinoud 				nexus->state = SFAS_NS_DISCONNECTING;
   1371       1.1   reinoud 				break;
   1372       1.1   reinoud 			case 0x02:	/* SAVE DATA POINTER */
   1373       1.1   reinoud 				sfas_save_pointers(dev);
   1374       1.1   reinoud 				break;
   1375       1.1   reinoud 			case 0x03:	/* RESTORE DATA POINTERS */
   1376       1.1   reinoud 				sfas_restore_pointers(dev);
   1377       1.1   reinoud 				break;
   1378       1.1   reinoud 			case 0x07:	/* MESSAGE REJECT */
   1379       1.1   reinoud 				/*
   1380       1.1   reinoud 				 * If we had sent a SDTR and we got a message
   1381       1.1   reinoud 				 * reject, the scsi docs say that we must go
   1382       1.1   reinoud 				 * to async transfer.
   1383       1.1   reinoud 				 */
   1384       1.1   reinoud 				if (nexus->flags & SFAS_NF_SDTR_SENT) {
   1385       1.1   reinoud 					nexus->flags &= ~SFAS_NF_SDTR_SENT;
   1386       1.1   reinoud 
   1387       1.1   reinoud 					nexus->config3 &= ~SFAS_CFG3_FASTSCSI;
   1388       1.1   reinoud 					nexus->syncper = 5;
   1389       1.1   reinoud 					nexus->syncoff = 0;
   1390       1.1   reinoud 
   1391       1.1   reinoud 					*rp->sfas_syncper = nexus->syncper;
   1392       1.1   reinoud 					*rp->sfas_syncoff = nexus->syncoff;
   1393       1.1   reinoud 					*rp->sfas_config3 = nexus->config3;
   1394       1.1   reinoud 				} else
   1395       1.1   reinoud 				/*
   1396       1.1   reinoud 				 * Something was rejected but we don't know
   1397       1.1   reinoud 				 * what! PANIC!
   1398       1.1   reinoud 				 */
   1399       1.1   reinoud 				  panic("sfasintr: Unknown message rejected!");
   1400       1.1   reinoud 				break;
   1401       1.1   reinoud 			case 0x08:	/* MO OPERATION */
   1402       1.1   reinoud 				break;
   1403       1.1   reinoud 			case 0x01:	/* EXTENDED MESSAGE */
   1404       1.1   reinoud 				switch(dev->sc_msg_in[2]) {
   1405       1.1   reinoud 				case 0x01:/* SYNC. DATA TRANSFER REQUEST */
   1406       1.1   reinoud 					/* Decode the SDTR message. */
   1407       1.1   reinoud 					period = 4*dev->sc_msg_in[3];
   1408       1.1   reinoud 					offset = dev->sc_msg_in[4];
   1409       1.1   reinoud 
   1410       1.1   reinoud 					/*
   1411       1.1   reinoud 					 * Make sure that the specs are within
   1412       1.1   reinoud 					 * chip limits. Note that if we
   1413       1.1   reinoud 					 * initiated the negotiation the specs
   1414       1.1   reinoud 					 * WILL be withing chip limits. If it
   1415       1.1   reinoud 					 * was the scsi unit that initiated
   1416       1.1   reinoud 					 * the negotiation, the specs may be
   1417       1.1   reinoud 					 * to high.
   1418       1.1   reinoud 					 */
   1419       1.1   reinoud 					if (offset > 16)
   1420       1.1   reinoud 						offset = 16;
   1421       1.1   reinoud 					if ((period < 200) &&
   1422       1.1   reinoud 					    (dev->sc_clock_freq <= 25))
   1423       1.1   reinoud 						period = 200;
   1424       1.1   reinoud 
   1425       1.1   reinoud 					if (offset == 0)
   1426       1.1   reinoud 					       period = 5*dev->sc_clock_period;
   1427       1.1   reinoud 
   1428       1.1   reinoud 					nexus->syncper = period/
   1429       1.1   reinoud 							  dev->sc_clock_period;
   1430       1.1   reinoud 					nexus->syncoff = offset;
   1431       1.1   reinoud 
   1432       1.1   reinoud 					if (period < 200)
   1433       1.1   reinoud 					  nexus->config3 |= SFAS_CFG3_FASTSCSI;
   1434       1.1   reinoud 					else
   1435       1.1   reinoud 					  nexus->config3 &=~SFAS_CFG3_FASTSCSI;
   1436       1.1   reinoud 
   1437       1.1   reinoud 					nexus->flags |= SFAS_NF_SYNC_TESTED;
   1438       1.1   reinoud 
   1439       1.1   reinoud 					*rp->sfas_syncper = nexus->syncper;
   1440       1.1   reinoud 					*rp->sfas_syncoff = nexus->syncoff;
   1441       1.1   reinoud 					*rp->sfas_config3 = nexus->config3;
   1442       1.1   reinoud 
   1443       1.1   reinoud 					/*
   1444       1.1   reinoud 					 * Hmmm, it seems that the scsi unit
   1445       1.1   reinoud 					 * initiated sync negotiation, so lets
   1446       1.1   reinoud 					 * reply acording to scsi-2 standard.
   1447       1.1   reinoud 					 */
   1448       1.1   reinoud 					if (!(nexus->flags& SFAS_NF_SDTR_SENT))
   1449       1.1   reinoud 					{
   1450       1.1   reinoud 					  if ((dev->sc_config_flags &
   1451       1.1   reinoud 					       SFAS_NO_SYNCH) ||
   1452       1.1   reinoud 					      (dev->sc_config_flags &
   1453       1.1   reinoud 					       SFAS_NO_DMA) ||
   1454       1.1   reinoud 					      sfas_inhibit_sync[
   1455       1.1   reinoud 							nexus->lun_unit & 7]) {
   1456       1.1   reinoud 					          period = 200;
   1457       1.1   reinoud 					          offset = 0;
   1458       1.1   reinoud 					  }
   1459       1.1   reinoud 
   1460       1.1   reinoud 					  nexus->offset = offset;
   1461       1.1   reinoud 					  nexus->period = period;
   1462       1.1   reinoud 					  nexus->flags |= SFAS_NF_DO_SDTR;
   1463       1.1   reinoud 					  *rp->sfas_command = SFAS_CMD_SET_ATN;
   1464       1.1   reinoud 					}
   1465       1.1   reinoud 
   1466       1.1   reinoud 					nexus->flags &= ~SFAS_NF_SDTR_SENT;
   1467       1.1   reinoud 					break;
   1468       1.1   reinoud 
   1469       1.1   reinoud 				case 0x00: /* MODIFY DATA POINTERS */
   1470       1.1   reinoud 				case 0x02: /* EXTENDED IDENTIFY (SCSI-1) */
   1471       1.1   reinoud 				case 0x03: /* WIDE DATA TRANSFER REQUEST */
   1472       1.1   reinoud 			        default:
   1473       1.1   reinoud 					/* Reject any unhandeled messages. */
   1474       1.1   reinoud 
   1475       1.1   reinoud 					dev->sc_msg_out[0] = 0x07;
   1476       1.1   reinoud 					dev->sc_msg_out_len = 1;
   1477       1.1   reinoud 					*rp->sfas_command = SFAS_CMD_SET_ATN;
   1478       1.1   reinoud 					cmd = SFAS_CMD_MESSAGE_ACCEPTED;
   1479       1.1   reinoud 					break;
   1480       1.1   reinoud 				}
   1481       1.1   reinoud 				break;
   1482       1.1   reinoud 
   1483       1.1   reinoud 			default:
   1484       1.1   reinoud 				/* Reject any unhandeled messages. */
   1485       1.1   reinoud 
   1486       1.1   reinoud 				dev->sc_msg_out[0] = 0x07;
   1487       1.1   reinoud 				dev->sc_msg_out_len = 1;
   1488       1.1   reinoud 				*rp->sfas_command = SFAS_CMD_SET_ATN;
   1489       1.1   reinoud 				cmd = SFAS_CMD_MESSAGE_ACCEPTED;
   1490       1.1   reinoud 				break;
   1491       1.1   reinoud 			}
   1492       1.1   reinoud 			nexus->flags &= ~SFAS_NF_HAS_MSG;
   1493       1.1   reinoud 			dev->sc_msg_in_len = 0;
   1494       1.1   reinoud 		}
   1495       1.1   reinoud 		break;
   1496       1.1   reinoud 	default:
   1497       1.1   reinoud 		printf("SFASINTR: UNKNOWN PHASE! phase: %d\n",
   1498       1.1   reinoud 		       dev->sc_status & SFAS_STAT_PHASE_MASK);
   1499       1.1   reinoud 		dev->sc_led(dev, 0);
   1500       1.1   reinoud 		sfas_scsidone(dev, nexus->xs, -4);
   1501       1.1   reinoud 
   1502       1.1   reinoud 		return(-1);
   1503       1.1   reinoud 	}
   1504       1.1   reinoud 
   1505       1.1   reinoud 	if (cmd)
   1506       1.1   reinoud 		*rp->sfas_command = cmd;
   1507       1.1   reinoud 
   1508       1.1   reinoud 	return(0);
   1509       1.1   reinoud }
   1510       1.1   reinoud 
   1511       1.1   reinoud /*
   1512       1.1   reinoud  * Stub for interrupt machine.
   1513       1.1   reinoud  */
   1514       1.1   reinoud void
   1515      1.17       dsl sfasintr(struct sfas_softc *dev)
   1516       1.1   reinoud {
   1517       1.1   reinoud 	sfas_regmap_p	 rp;
   1518       1.1   reinoud 	struct nexus	*nexus;
   1519       1.1   reinoud 
   1520       1.1   reinoud 	rp = dev->sc_fas;
   1521       1.1   reinoud 
   1522       1.1   reinoud 	if (!sfas_pretests(dev, rp)) {
   1523       1.1   reinoud 
   1524       1.1   reinoud 		nexus = dev->sc_cur_nexus;
   1525       1.1   reinoud 		if (nexus == NULL)
   1526       1.1   reinoud 			nexus = dev->sc_sel_nexus;
   1527       1.1   reinoud 
   1528       1.1   reinoud 		if (nexus)
   1529       1.1   reinoud 			if (!sfas_midaction(dev, rp, nexus))
   1530       1.1   reinoud 				sfas_postaction(dev, rp, nexus);
   1531       1.1   reinoud 	}
   1532       1.1   reinoud }
   1533       1.1   reinoud 
   1534       1.1   reinoud /*
   1535       1.1   reinoud  * sfasicmd is used to perform IO when we can't use interrupts. sfasicmd
   1536       1.1   reinoud  * emulates the normal environment by waiting for the chip and calling
   1537       1.1   reinoud  * sfasintr.
   1538       1.1   reinoud  */
   1539       1.1   reinoud void
   1540      1.17       dsl sfasicmd(struct sfas_softc *dev, struct sfas_pending *pendp)
   1541       1.1   reinoud {
   1542       1.1   reinoud 	struct nexus	*nexus;
   1543       1.1   reinoud 
   1544       1.1   reinoud 	nexus = &dev->sc_nexus[pendp->xs->xs_periph->periph_target];
   1545       1.1   reinoud 
   1546       1.1   reinoud 	if (!sfasselect(dev, pendp, (char *)pendp->xs->cmd, pendp->xs->cmdlen,
   1547       1.1   reinoud 			(char *)pendp->xs->data, pendp->xs->datalen,
   1548       1.1   reinoud 			SFAS_SELECT_I))
   1549       1.1   reinoud 		panic("sfasicmd: Couldn't select unit");
   1550       1.1   reinoud 
   1551       1.1   reinoud 	while(nexus->state != SFAS_NS_FINISHED) {
   1552       1.1   reinoud 		sfasiwait(dev);
   1553       1.1   reinoud 		sfasintr(dev);
   1554       1.1   reinoud 	}
   1555       1.1   reinoud 
   1556       1.1   reinoud 	nexus->flags &= ~SFAS_NF_SYNC_TESTED;
   1557       1.1   reinoud }
   1558       1.1   reinoud 
   1559       1.1   reinoud 
   1560       1.1   reinoud #ifdef SFAS_DEBUG
   1561       1.1   reinoud 
   1562       1.1   reinoud void
   1563      1.17       dsl dump_nexus(struct nexus *nexus)
   1564       1.1   reinoud {
   1565       1.1   reinoud 	int loop;
   1566       1.1   reinoud 
   1567       1.1   reinoud 	printf("nexus=%08x\n", (u_int)nexus);
   1568       1.1   reinoud 	printf("scsi_fer=%08x\n", (u_int)nexus->xs);
   1569       1.1   reinoud 	printf("ID=%02x\n", nexus->ID);
   1570       1.1   reinoud 	printf("clen=%02x\n", nexus->clen);
   1571       1.1   reinoud 	printf("cbuf=");
   1572       1.1   reinoud 	for (loop = 0; loop< 14; ++loop)
   1573       1.1   reinoud 		printf(" %02x\n", nexus->cbuf[loop]);
   1574       1.1   reinoud 	printf("\n");
   1575      1.10       wiz 	printf("DMA:\n");
   1576       1.1   reinoud 	for (loop = 0; loop < MAXCHAIN; ++loop)
   1577       1.1   reinoud 		printf("dma_chain: %08x %04x %04x\n", nexus->dma[loop].ptr,
   1578       1.1   reinoud 		    nexus->dma[loop].len, nexus->dma[loop].flg);
   1579       1.1   reinoud 	printf("\n");
   1580       1.1   reinoud 
   1581       1.1   reinoud 	printf("max_link=%d\n", nexus->max_link);
   1582       1.1   reinoud 	printf("cur_link=%d\n", nexus->cur_link);
   1583       1.1   reinoud 
   1584       1.1   reinoud 	printf("buf=%08x\n", (u_int)nexus->buf);
   1585       1.1   reinoud 	printf("len=%08x\n", nexus->len);
   1586       1.1   reinoud 	printf("dma_buf=%08x\n", (u_int)nexus->dma_buf);
   1587       1.1   reinoud 	printf("dma_len=%08x\n", nexus->dma_len);
   1588       1.1   reinoud 	printf("dma_blk_ptr=%08x\n", (u_int)nexus->dma_blk_ptr);
   1589       1.1   reinoud 	printf("dma_blk_len=%08x\n", nexus->dma_blk_len);
   1590       1.1   reinoud 	printf("dma_blk_flag=%08x\n", nexus->dma_blk_flg);
   1591       1.1   reinoud 	printf("state=%02x\n", nexus->state);
   1592       1.1   reinoud 	printf("flags=%04x\n", nexus->flags);
   1593       1.1   reinoud 	printf("period=%d\n", nexus->period);
   1594       1.1   reinoud 	printf("offset=%d\n", nexus->offset);
   1595       1.1   reinoud 	printf("syncper=%d\n", nexus->syncper);
   1596       1.1   reinoud 	printf("syncoff=%d\n", nexus->syncoff);
   1597       1.1   reinoud 	printf("config3=%02x\n", nexus->config3);
   1598       1.1   reinoud 	printf("lun_unit=%d\n", nexus->lun_unit);
   1599       1.1   reinoud 	printf("status=%02x\n", nexus->status);
   1600       1.1   reinoud 	printf("\n");
   1601       1.1   reinoud }
   1602       1.1   reinoud 
   1603       1.1   reinoud void
   1604      1.17       dsl dump_nexii(struct sfas_softc *sc)
   1605       1.1   reinoud {
   1606       1.1   reinoud 	int loop;
   1607       1.1   reinoud 
   1608       1.1   reinoud 	for (loop = 0; loop < 8; ++loop) {
   1609       1.1   reinoud 		dump_nexus(&sc->sc_nexus[loop]);
   1610       1.1   reinoud 	}
   1611       1.1   reinoud }
   1612       1.1   reinoud 
   1613       1.1   reinoud void
   1614      1.17       dsl dump_sfassoftc(struct sfas_softc *sc)
   1615       1.1   reinoud {
   1616       1.1   reinoud 	printf("sfassoftc @ 0x%08x\n", (u_int)sc);
   1617       1.1   reinoud 	printf("clock_freq = %d\n", sc->sc_clock_freq);
   1618       1.1   reinoud 	printf("timeout = %d\n", sc->sc_timeout);
   1619       1.1   reinoud 	printf("host_id = %d\n", sc->sc_host_id);
   1620       1.1   reinoud 	printf("config_flags = 0x%08x\n", sc->sc_config_flags);
   1621       1.1   reinoud 	printf("led_status = %d\n", sc->sc_led_status);
   1622       1.1   reinoud 
   1623       1.1   reinoud 	dump_nexii(sc);
   1624       1.1   reinoud 	printf("cur_nexus = 0x%08x\n", (u_int)sc->sc_cur_nexus);
   1625       1.1   reinoud 	printf("sel_nexus = 0x%08x\n", (u_int)sc->sc_sel_nexus);
   1626       1.1   reinoud 	printf("\n");
   1627       1.1   reinoud }
   1628       1.1   reinoud 
   1629       1.1   reinoud #endif	/* SFAS_DEBUG */
   1630