Home | History | Annotate | Line # | Download | only in ic
uha.c revision 1.24
      1  1.24   thorpej /*	$NetBSD: uha.c,v 1.24 2000/11/14 18:21:02 thorpej Exp $	*/
      2   1.1   mycroft 
      3   1.1   mycroft #undef UHADEBUG
      4   1.1   mycroft #ifdef DDB
      5   1.1   mycroft #define	integrate
      6   1.1   mycroft #else
      7   1.1   mycroft #define	integrate	static inline
      8   1.1   mycroft #endif
      9   1.1   mycroft 
     10   1.8   thorpej /*-
     11  1.15   thorpej  * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
     12   1.8   thorpej  * All rights reserved.
     13   1.8   thorpej  *
     14   1.8   thorpej  * This code is derived from software contributed to The NetBSD Foundation
     15  1.17   mycroft  * by Charles M. Hannum and by Jason R. Thorpe of the Numerical Aerospace
     16  1.17   mycroft  * Simulation Facility, NASA Ames Research Center.
     17   1.8   thorpej  *
     18   1.8   thorpej  * Redistribution and use in source and binary forms, with or without
     19   1.8   thorpej  * modification, are permitted provided that the following conditions
     20   1.8   thorpej  * are met:
     21   1.8   thorpej  * 1. Redistributions of source code must retain the above copyright
     22   1.8   thorpej  *    notice, this list of conditions and the following disclaimer.
     23   1.8   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     24   1.8   thorpej  *    notice, this list of conditions and the following disclaimer in the
     25   1.8   thorpej  *    documentation and/or other materials provided with the distribution.
     26   1.8   thorpej  * 3. All advertising materials mentioning features or use of this software
     27   1.8   thorpej  *    must display the following acknowledgement:
     28   1.8   thorpej  *	This product includes software developed by the NetBSD
     29   1.8   thorpej  *	Foundation, Inc. and its contributors.
     30   1.8   thorpej  * 4. Neither the name of The NetBSD Foundation nor the names of its
     31   1.8   thorpej  *    contributors may be used to endorse or promote products derived
     32   1.8   thorpej  *    from this software without specific prior written permission.
     33   1.8   thorpej  *
     34   1.8   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     35   1.8   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     36   1.8   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     37   1.8   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     38   1.8   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     39   1.8   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     40   1.8   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     41   1.8   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     42   1.8   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     43   1.8   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     44   1.8   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     45   1.1   mycroft  */
     46   1.1   mycroft 
     47   1.1   mycroft /*
     48   1.1   mycroft  * Ported for use with the UltraStor 14f by Gary Close (gclose (at) wvnvms.wvnet.edu)
     49   1.1   mycroft  * Slight fixes to timeouts to run with the 34F
     50   1.1   mycroft  * Thanks to Julian Elischer for advice and help with this port.
     51   1.1   mycroft  *
     52   1.1   mycroft  * Originally written by Julian Elischer (julian (at) tfs.com)
     53   1.1   mycroft  * for TRW Financial Systems for use under the MACH(2.5) operating system.
     54   1.1   mycroft  *
     55   1.1   mycroft  * TRW Financial Systems, in accordance with their agreement with Carnegie
     56   1.1   mycroft  * Mellon University, makes this software available to CMU to distribute
     57   1.1   mycroft  * or use in any manner that they see fit as long as this message is kept with
     58   1.1   mycroft  * the software. For this reason TFS also grants any other persons or
     59   1.1   mycroft  * organisations permission to use or modify this software.
     60   1.1   mycroft  *
     61   1.1   mycroft  * TFS supplies this software to be publicly redistributed
     62   1.1   mycroft  * on the understanding that TFS is not responsible for the correct
     63   1.1   mycroft  * functioning of this software in any circumstances.
     64   1.1   mycroft  *
     65   1.1   mycroft  * commenced: Sun Sep 27 18:14:01 PDT 1992
     66   1.1   mycroft  * slight mod to make work with 34F as well: Wed Jun  2 18:05:48 WST 1993
     67   1.1   mycroft  */
     68   1.1   mycroft 
     69   1.1   mycroft #include <sys/types.h>
     70   1.1   mycroft #include <sys/param.h>
     71   1.1   mycroft #include <sys/systm.h>
     72   1.1   mycroft #include <sys/kernel.h>
     73   1.1   mycroft #include <sys/errno.h>
     74   1.1   mycroft #include <sys/ioctl.h>
     75   1.1   mycroft #include <sys/device.h>
     76   1.1   mycroft #include <sys/malloc.h>
     77   1.1   mycroft #include <sys/buf.h>
     78   1.1   mycroft #include <sys/proc.h>
     79   1.1   mycroft #include <sys/user.h>
     80   1.1   mycroft 
     81  1.24   thorpej #include <uvm/uvm_extern.h>
     82  1.24   thorpej 
     83   1.1   mycroft #include <machine/bus.h>
     84   1.1   mycroft #include <machine/intr.h>
     85   1.1   mycroft 
     86   1.9    bouyer #include <dev/scsipi/scsi_all.h>
     87   1.9    bouyer #include <dev/scsipi/scsipi_all.h>
     88   1.9    bouyer #include <dev/scsipi/scsiconf.h>
     89   1.1   mycroft 
     90   1.1   mycroft #include <dev/ic/uhareg.h>
     91   1.1   mycroft #include <dev/ic/uhavar.h>
     92   1.1   mycroft 
     93   1.1   mycroft #ifndef	DDB
     94   1.5  jonathan #define Debugger() panic("should call debugger here (uha.c)")
     95   1.1   mycroft #endif /* ! DDB */
     96   1.1   mycroft 
     97   1.8   thorpej #define	UHA_MAXXFER	((UHA_NSEG - 1) << PGSHIFT)
     98   1.1   mycroft 
     99   1.4   mycroft integrate void uha_reset_mscp __P((struct uha_softc *, struct uha_mscp *));
    100   1.1   mycroft void uha_free_mscp __P((struct uha_softc *, struct uha_mscp *));
    101  1.10   thorpej integrate int uha_init_mscp __P((struct uha_softc *, struct uha_mscp *));
    102   1.1   mycroft struct uha_mscp *uha_get_mscp __P((struct uha_softc *, int));
    103   1.1   mycroft void uhaminphys __P((struct buf *));
    104   1.9    bouyer int uha_scsi_cmd __P((struct scsipi_xfer *));
    105  1.16   thorpej int uha_create_mscps __P((struct uha_softc *, struct uha_mscp *, int));
    106   1.1   mycroft 
    107   1.1   mycroft /* the below structure is so we have a default dev struct for out link struct */
    108   1.9    bouyer struct scsipi_device uha_dev = {
    109   1.1   mycroft 	NULL,			/* Use default error handler */
    110   1.1   mycroft 	NULL,			/* have a queue, served by this */
    111   1.1   mycroft 	NULL,			/* have no async handler */
    112   1.1   mycroft 	NULL,			/* Use default 'done' routine */
    113   1.1   mycroft };
    114   1.1   mycroft 
    115   1.1   mycroft #define	UHA_ABORT_TIMEOUT	2000	/* time to wait for abort (mSec) */
    116   1.8   thorpej 
    117   1.1   mycroft /*
    118   1.1   mycroft  * Attach all the sub-devices we can find
    119   1.1   mycroft  */
    120   1.1   mycroft void
    121   1.7   mycroft uha_attach(sc, upd)
    122   1.1   mycroft 	struct uha_softc *sc;
    123   1.7   mycroft 	struct uha_probe_data *upd;
    124   1.1   mycroft {
    125  1.16   thorpej 	bus_dma_segment_t seg;
    126  1.16   thorpej 	int i, error, rseg;
    127   1.1   mycroft 
    128   1.8   thorpej 	TAILQ_INIT(&sc->sc_free_mscp);
    129  1.21   thorpej 	TAILQ_INIT(&sc->sc_queue);
    130   1.8   thorpej 
    131   1.1   mycroft 	(sc->init)(sc);
    132   1.1   mycroft 
    133   1.1   mycroft 	/*
    134  1.19   thorpej 	 * Fill in the adapter.
    135  1.19   thorpej 	 */
    136  1.19   thorpej 	sc->sc_adapter.scsipi_cmd = uha_scsi_cmd;
    137  1.19   thorpej 	sc->sc_adapter.scsipi_minphys = uhaminphys;
    138  1.19   thorpej 
    139  1.19   thorpej 	/*
    140   1.9    bouyer 	 * fill in the prototype scsipi_link.
    141   1.1   mycroft 	 */
    142   1.9    bouyer 	sc->sc_link.scsipi_scsi.channel = SCSI_CHANNEL_ONLY_ONE;
    143   1.1   mycroft 	sc->sc_link.adapter_softc = sc;
    144   1.9    bouyer 	sc->sc_link.scsipi_scsi.adapter_target = upd->sc_scsi_dev;
    145  1.19   thorpej 	sc->sc_link.adapter = &sc->sc_adapter;
    146   1.1   mycroft 	sc->sc_link.device = &uha_dev;
    147   1.1   mycroft 	sc->sc_link.openings = 2;
    148   1.9    bouyer 	sc->sc_link.scsipi_scsi.max_target = 7;
    149  1.20    mjacob 	sc->sc_link.scsipi_scsi.max_lun = 7;
    150   1.9    bouyer 	sc->sc_link.type = BUS_SCSI;
    151   1.1   mycroft 
    152  1.16   thorpej #define	MSCPSIZE	(UHA_MSCP_MAX * sizeof(struct uha_mscp))
    153  1.16   thorpej 
    154  1.16   thorpej 	/*
    155  1.16   thorpej 	 * Allocate the MSCPs.
    156  1.16   thorpej 	 */
    157  1.16   thorpej 	if ((error = bus_dmamem_alloc(sc->sc_dmat, MSCPSIZE,
    158  1.24   thorpej 	    PAGE_SIZE, 0, &seg, 1, &rseg, BUS_DMA_NOWAIT)) != 0) {
    159  1.16   thorpej 		printf("%s: unable to allocate mscps, error = %d\n",
    160  1.16   thorpej 		    sc->sc_dev.dv_xname, error);
    161  1.16   thorpej 		return;
    162  1.16   thorpej 	}
    163  1.16   thorpej 	if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
    164  1.16   thorpej 	    MSCPSIZE, (caddr_t *)&sc->sc_mscps,
    165  1.16   thorpej 	    BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
    166  1.16   thorpej 		printf("%s: unable to map mscps, error = %d\n",
    167  1.16   thorpej 		    sc->sc_dev.dv_xname, error);
    168  1.16   thorpej 		return;
    169  1.16   thorpej 	}
    170  1.16   thorpej 
    171  1.16   thorpej 	/*
    172  1.16   thorpej 	 * Create and load the DMA map used for the mscps.
    173  1.16   thorpej 	 */
    174  1.16   thorpej 	if ((error = bus_dmamap_create(sc->sc_dmat, MSCPSIZE,
    175  1.16   thorpej 	    1, MSCPSIZE, 0, BUS_DMA_NOWAIT | sc->sc_dmaflags,
    176  1.16   thorpej 	    &sc->sc_dmamap_mscp)) != 0) {
    177  1.16   thorpej 		printf("%s: unable to create mscp DMA map, error = %d\n",
    178  1.16   thorpej 		    sc->sc_dev.dv_xname, error);
    179  1.16   thorpej 		return;
    180  1.16   thorpej 	}
    181  1.16   thorpej 	if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap_mscp,
    182  1.16   thorpej 	    sc->sc_mscps, MSCPSIZE, NULL, BUS_DMA_NOWAIT)) != 0) {
    183  1.16   thorpej 		printf("%s: unable to load mscp DMA map, error = %d\n",
    184  1.16   thorpej 		    sc->sc_dev.dv_xname, error);
    185  1.16   thorpej 		return;
    186  1.16   thorpej 	}
    187  1.16   thorpej 
    188  1.16   thorpej #undef MSCPSIZE
    189  1.16   thorpej 
    190  1.16   thorpej 	/*
    191  1.16   thorpej 	 * Initialize the mscps.
    192  1.16   thorpej 	 */
    193  1.16   thorpej 	i = uha_create_mscps(sc, sc->sc_mscps, UHA_MSCP_MAX);
    194  1.16   thorpej 	if (i == 0) {
    195  1.16   thorpej 		printf("%s: unable to create mscps\n",
    196  1.16   thorpej 		    sc->sc_dev.dv_xname);
    197  1.16   thorpej 		return;
    198  1.16   thorpej 	} else if (i != UHA_MSCP_MAX) {
    199  1.16   thorpej 		printf("%s: WARNING: only %d of %d mscps created\n",
    200  1.16   thorpej 		    sc->sc_dev.dv_xname, i, UHA_MSCP_MAX);
    201  1.16   thorpej 	}
    202  1.16   thorpej 
    203   1.1   mycroft 	/*
    204   1.1   mycroft 	 * ask the adapter what subunits are present
    205   1.1   mycroft 	 */
    206   1.1   mycroft 	config_found(&sc->sc_dev, &sc->sc_link, scsiprint);
    207   1.1   mycroft }
    208   1.1   mycroft 
    209   1.1   mycroft integrate void
    210   1.1   mycroft uha_reset_mscp(sc, mscp)
    211   1.1   mycroft 	struct uha_softc *sc;
    212   1.1   mycroft 	struct uha_mscp *mscp;
    213   1.1   mycroft {
    214   1.1   mycroft 
    215   1.1   mycroft 	mscp->flags = 0;
    216   1.1   mycroft }
    217   1.1   mycroft 
    218   1.1   mycroft /*
    219   1.1   mycroft  * A mscp (and hence a mbx-out) is put onto the free list.
    220   1.1   mycroft  */
    221   1.1   mycroft void
    222   1.1   mycroft uha_free_mscp(sc, mscp)
    223   1.1   mycroft 	struct uha_softc *sc;
    224   1.1   mycroft 	struct uha_mscp *mscp;
    225   1.1   mycroft {
    226   1.1   mycroft 	int s;
    227   1.1   mycroft 
    228   1.1   mycroft 	s = splbio();
    229   1.1   mycroft 
    230   1.1   mycroft 	uha_reset_mscp(sc, mscp);
    231   1.1   mycroft 	TAILQ_INSERT_HEAD(&sc->sc_free_mscp, mscp, chain);
    232   1.1   mycroft 
    233   1.1   mycroft 	/*
    234   1.1   mycroft 	 * If there were none, wake anybody waiting for one to come free,
    235   1.1   mycroft 	 * starting with queued entries.
    236   1.1   mycroft 	 */
    237   1.1   mycroft 	if (mscp->chain.tqe_next == 0)
    238   1.1   mycroft 		wakeup(&sc->sc_free_mscp);
    239   1.1   mycroft 
    240   1.1   mycroft 	splx(s);
    241   1.1   mycroft }
    242   1.1   mycroft 
    243  1.10   thorpej integrate int
    244   1.1   mycroft uha_init_mscp(sc, mscp)
    245   1.1   mycroft 	struct uha_softc *sc;
    246   1.1   mycroft 	struct uha_mscp *mscp;
    247   1.1   mycroft {
    248   1.8   thorpej 	bus_dma_tag_t dmat = sc->sc_dmat;
    249  1.10   thorpej 	int hashnum, error;
    250   1.1   mycroft 
    251   1.8   thorpej 	/*
    252  1.16   thorpej 	 * Create the DMA map for this MSCP.
    253   1.8   thorpej 	 */
    254  1.10   thorpej 	error = bus_dmamap_create(dmat, UHA_MAXXFER, UHA_NSEG, UHA_MAXXFER,
    255   1.8   thorpej 	    0, BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW | sc->sc_dmaflags,
    256  1.10   thorpej 	    &mscp->dmamap_xfer);
    257  1.10   thorpej 	if (error) {
    258  1.16   thorpej 		printf("%s: can't create mscp DMA map, error = %d\n",
    259  1.16   thorpej 		    sc->sc_dev.dv_xname, error);
    260  1.10   thorpej 		return (error);
    261  1.10   thorpej 	}
    262   1.8   thorpej 
    263   1.1   mycroft 	/*
    264   1.1   mycroft 	 * put in the phystokv hash table
    265   1.1   mycroft 	 * Never gets taken out.
    266   1.1   mycroft 	 */
    267  1.16   thorpej 	mscp->hashkey = sc->sc_dmamap_mscp->dm_segs[0].ds_addr +
    268  1.16   thorpej 	    UHA_MSCP_OFF(mscp);
    269   1.1   mycroft 	hashnum = MSCP_HASH(mscp->hashkey);
    270   1.1   mycroft 	mscp->nexthash = sc->sc_mscphash[hashnum];
    271   1.1   mycroft 	sc->sc_mscphash[hashnum] = mscp;
    272   1.1   mycroft 	uha_reset_mscp(sc, mscp);
    273  1.10   thorpej 	return (0);
    274   1.1   mycroft }
    275   1.1   mycroft 
    276   1.1   mycroft /*
    277   1.8   thorpej  * Create a set of MSCPs and add them to the free list.
    278   1.8   thorpej  */
    279   1.8   thorpej int
    280  1.16   thorpej uha_create_mscps(sc, mscpstore, count)
    281   1.8   thorpej 	struct uha_softc *sc;
    282  1.16   thorpej 	struct uha_mscp *mscpstore;
    283  1.16   thorpej 	int count;
    284   1.8   thorpej {
    285   1.8   thorpej 	struct uha_mscp *mscp;
    286  1.16   thorpej 	int i, error;
    287   1.8   thorpej 
    288  1.16   thorpej 	bzero(mscpstore, sizeof(struct uha_mscp) * count);
    289  1.16   thorpej 	for (i = 0; i < count; i++) {
    290  1.16   thorpej 		mscp = &mscpstore[i];
    291  1.16   thorpej 		if ((error = uha_init_mscp(sc, mscp)) != 0) {
    292  1.16   thorpej 			printf("%s: unable to initialize mscp, error = %d\n",
    293  1.16   thorpej 			    sc->sc_dev.dv_xname, error);
    294  1.16   thorpej 			goto out;
    295  1.10   thorpej 		}
    296   1.8   thorpej 		TAILQ_INSERT_TAIL(&sc->sc_free_mscp, mscp, chain);
    297   1.8   thorpej 	}
    298  1.16   thorpej  out:
    299  1.16   thorpej 	return (i);
    300   1.8   thorpej }
    301   1.8   thorpej 
    302   1.8   thorpej /*
    303   1.1   mycroft  * Get a free mscp
    304   1.1   mycroft  *
    305   1.1   mycroft  * If there are none, see if we can allocate a new one.  If so, put it in the
    306   1.1   mycroft  * hash table too otherwise either return an error or sleep.
    307   1.1   mycroft  */
    308   1.1   mycroft struct uha_mscp *
    309   1.1   mycroft uha_get_mscp(sc, flags)
    310   1.1   mycroft 	struct uha_softc *sc;
    311   1.1   mycroft 	int flags;
    312   1.1   mycroft {
    313   1.1   mycroft 	struct uha_mscp *mscp;
    314   1.1   mycroft 	int s;
    315   1.1   mycroft 
    316   1.1   mycroft 	s = splbio();
    317   1.1   mycroft 
    318   1.1   mycroft 	/*
    319   1.1   mycroft 	 * If we can and have to, sleep waiting for one to come free
    320   1.1   mycroft 	 * but only if we can't allocate a new one
    321   1.1   mycroft 	 */
    322   1.1   mycroft 	for (;;) {
    323   1.1   mycroft 		mscp = sc->sc_free_mscp.tqh_first;
    324   1.1   mycroft 		if (mscp) {
    325   1.1   mycroft 			TAILQ_REMOVE(&sc->sc_free_mscp, mscp, chain);
    326   1.1   mycroft 			break;
    327   1.1   mycroft 		}
    328  1.22   thorpej 		if ((flags & XS_CTL_NOSLEEP) != 0)
    329   1.1   mycroft 			goto out;
    330   1.1   mycroft 		tsleep(&sc->sc_free_mscp, PRIBIO, "uhamsc", 0);
    331   1.1   mycroft 	}
    332   1.1   mycroft 
    333   1.1   mycroft 	mscp->flags |= MSCP_ALLOC;
    334   1.1   mycroft 
    335   1.1   mycroft out:
    336   1.1   mycroft 	splx(s);
    337   1.1   mycroft 	return (mscp);
    338   1.1   mycroft }
    339   1.1   mycroft 
    340   1.1   mycroft /*
    341   1.1   mycroft  * given a physical address, find the mscp that it corresponds to.
    342   1.1   mycroft  */
    343   1.1   mycroft struct uha_mscp *
    344   1.1   mycroft uha_mscp_phys_kv(sc, mscp_phys)
    345   1.1   mycroft 	struct uha_softc *sc;
    346   1.1   mycroft 	u_long mscp_phys;
    347   1.1   mycroft {
    348   1.1   mycroft 	int hashnum = MSCP_HASH(mscp_phys);
    349   1.1   mycroft 	struct uha_mscp *mscp = sc->sc_mscphash[hashnum];
    350   1.1   mycroft 
    351   1.1   mycroft 	while (mscp) {
    352   1.1   mycroft 		if (mscp->hashkey == mscp_phys)
    353   1.1   mycroft 			break;
    354   1.1   mycroft 		mscp = mscp->nexthash;
    355   1.1   mycroft 	}
    356   1.1   mycroft 	return (mscp);
    357   1.1   mycroft }
    358   1.1   mycroft 
    359   1.1   mycroft /*
    360   1.1   mycroft  * We have a mscp which has been processed by the adaptor, now we look to see
    361   1.1   mycroft  * how the operation went.
    362   1.1   mycroft  */
    363   1.1   mycroft void
    364   1.1   mycroft uha_done(sc, mscp)
    365   1.1   mycroft 	struct uha_softc *sc;
    366   1.1   mycroft 	struct uha_mscp *mscp;
    367   1.1   mycroft {
    368   1.8   thorpej 	bus_dma_tag_t dmat = sc->sc_dmat;
    369   1.9    bouyer 	struct scsipi_sense_data *s1, *s2;
    370   1.9    bouyer 	struct scsipi_xfer *xs = mscp->xs;
    371   1.1   mycroft 
    372   1.1   mycroft 	SC_DEBUG(xs->sc_link, SDEV_DB2, ("uha_done\n"));
    373   1.8   thorpej 
    374  1.16   thorpej 	bus_dmamap_sync(dmat, sc->sc_dmamap_mscp,
    375  1.16   thorpej 	    UHA_MSCP_OFF(mscp), sizeof(struct uha_mscp),
    376  1.16   thorpej 	    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
    377  1.16   thorpej 
    378   1.8   thorpej 	/*
    379   1.8   thorpej 	 * If we were a data transfer, unload the map that described
    380   1.8   thorpej 	 * the data buffer.
    381   1.8   thorpej 	 */
    382   1.8   thorpej 	if (xs->datalen) {
    383  1.15   thorpej 		bus_dmamap_sync(dmat, mscp->dmamap_xfer, 0,
    384  1.15   thorpej 		    mscp->dmamap_xfer->dm_mapsize,
    385  1.22   thorpej 		    (xs->xs_control & XS_CTL_DATA_IN) ? BUS_DMASYNC_POSTREAD :
    386   1.8   thorpej 		    BUS_DMASYNC_POSTWRITE);
    387   1.8   thorpej 		bus_dmamap_unload(dmat, mscp->dmamap_xfer);
    388   1.8   thorpej 	}
    389   1.8   thorpej 
    390   1.1   mycroft 	/*
    391   1.1   mycroft 	 * Otherwise, put the results of the operation
    392   1.1   mycroft 	 * into the xfer and call whoever started it
    393   1.1   mycroft 	 */
    394   1.1   mycroft 	if ((mscp->flags & MSCP_ALLOC) == 0) {
    395   1.3  christos 		printf("%s: exiting ccb not allocated!\n", sc->sc_dev.dv_xname);
    396   1.1   mycroft 		Debugger();
    397   1.1   mycroft 		return;
    398   1.1   mycroft 	}
    399   1.1   mycroft 	if (xs->error == XS_NOERROR) {
    400   1.1   mycroft 		if (mscp->host_stat != UHA_NO_ERR) {
    401   1.1   mycroft 			switch (mscp->host_stat) {
    402   1.1   mycroft 			case UHA_SBUS_TIMEOUT:		/* No response */
    403   1.1   mycroft 				xs->error = XS_SELTIMEOUT;
    404   1.1   mycroft 				break;
    405   1.1   mycroft 			default:	/* Other scsi protocol messes */
    406   1.3  christos 				printf("%s: host_stat %x\n",
    407   1.1   mycroft 				    sc->sc_dev.dv_xname, mscp->host_stat);
    408   1.1   mycroft 				xs->error = XS_DRIVER_STUFFUP;
    409   1.1   mycroft 			}
    410   1.1   mycroft 		} else if (mscp->target_stat != SCSI_OK) {
    411   1.1   mycroft 			switch (mscp->target_stat) {
    412   1.1   mycroft 			case SCSI_CHECK:
    413   1.1   mycroft 				s1 = &mscp->mscp_sense;
    414   1.9    bouyer 				s2 = &xs->sense.scsi_sense;
    415   1.1   mycroft 				*s2 = *s1;
    416   1.1   mycroft 				xs->error = XS_SENSE;
    417   1.1   mycroft 				break;
    418   1.1   mycroft 			case SCSI_BUSY:
    419   1.1   mycroft 				xs->error = XS_BUSY;
    420   1.1   mycroft 				break;
    421   1.1   mycroft 			default:
    422   1.3  christos 				printf("%s: target_stat %x\n",
    423   1.1   mycroft 				    sc->sc_dev.dv_xname, mscp->target_stat);
    424   1.1   mycroft 				xs->error = XS_DRIVER_STUFFUP;
    425   1.1   mycroft 			}
    426   1.1   mycroft 		} else
    427   1.1   mycroft 			xs->resid = 0;
    428   1.1   mycroft 	}
    429   1.1   mycroft 	uha_free_mscp(sc, mscp);
    430  1.22   thorpej 	xs->xs_status |= XS_STS_DONE;
    431   1.9    bouyer 	scsipi_done(xs);
    432  1.11   thorpej 
    433  1.11   thorpej 	/*
    434  1.11   thorpej 	 * If there are queue entries in the software queue, try to
    435  1.11   thorpej 	 * run the first one.  We should be more or less guaranteed
    436  1.11   thorpej 	 * to succeed, since we just freed an MSCP.
    437  1.11   thorpej 	 *
    438  1.11   thorpej 	 * NOTE: uha_scsi_cmd() relies on our calling it with
    439  1.11   thorpej 	 * the first entry in the queue.
    440  1.11   thorpej 	 */
    441  1.21   thorpej 	if ((xs = TAILQ_FIRST(&sc->sc_queue)) != NULL)
    442  1.11   thorpej 		(void) uha_scsi_cmd(xs);
    443   1.1   mycroft }
    444   1.1   mycroft 
    445   1.1   mycroft void
    446   1.1   mycroft uhaminphys(bp)
    447   1.1   mycroft 	struct buf *bp;
    448   1.1   mycroft {
    449   1.1   mycroft 
    450   1.8   thorpej 	if (bp->b_bcount > UHA_MAXXFER)
    451   1.8   thorpej 		bp->b_bcount = UHA_MAXXFER;
    452   1.1   mycroft 	minphys(bp);
    453   1.1   mycroft }
    454   1.1   mycroft 
    455   1.1   mycroft /*
    456   1.1   mycroft  * start a scsi operation given the command and the data address.  Also
    457   1.1   mycroft  * needs the unit, target and lu.
    458   1.1   mycroft  */
    459   1.1   mycroft int
    460   1.1   mycroft uha_scsi_cmd(xs)
    461   1.9    bouyer 	struct scsipi_xfer *xs;
    462   1.1   mycroft {
    463   1.9    bouyer 	struct scsipi_link *sc_link = xs->sc_link;
    464   1.1   mycroft 	struct uha_softc *sc = sc_link->adapter_softc;
    465   1.8   thorpej 	bus_dma_tag_t dmat = sc->sc_dmat;
    466   1.1   mycroft 	struct uha_mscp *mscp;
    467   1.1   mycroft 	struct uha_dma_seg *sg;
    468   1.8   thorpej 	int error, seg, flags, s;
    469  1.23   thorpej 	int fromqueue = 0, dontqueue = 0, nowait = 0;
    470   1.1   mycroft 
    471   1.1   mycroft 	SC_DEBUG(sc_link, SDEV_DB2, ("uha_scsi_cmd\n"));
    472  1.11   thorpej 
    473  1.11   thorpej 	s = splbio();		/* protect the queue */
    474  1.11   thorpej 
    475  1.11   thorpej 	/*
    476  1.11   thorpej 	 * If we're running the queue from bha_done(), we've been
    477  1.11   thorpej 	 * called with the first queue entry as our argument.
    478  1.11   thorpej 	 */
    479  1.21   thorpej 	if (xs == TAILQ_FIRST(&sc->sc_queue)) {
    480  1.21   thorpej 		TAILQ_REMOVE(&sc->sc_queue, xs, adapter_q);
    481  1.11   thorpej 		fromqueue = 1;
    482  1.23   thorpej 		nowait = 1;
    483  1.11   thorpej 		goto get_mscp;
    484  1.11   thorpej 	}
    485  1.11   thorpej 
    486  1.11   thorpej 	/* Polled requests can't be queued for later. */
    487  1.22   thorpej 	dontqueue = xs->xs_control & XS_CTL_POLL;
    488  1.11   thorpej 
    489  1.11   thorpej 	/*
    490  1.11   thorpej 	 * If there are jobs in the queue, run them first.
    491  1.11   thorpej 	 */
    492  1.21   thorpej 	if (TAILQ_FIRST(&sc->sc_queue) != NULL) {
    493  1.11   thorpej 		/*
    494  1.11   thorpej 		 * If we can't queue, we have to abort, since
    495  1.11   thorpej 		 * we have to preserve order.
    496  1.11   thorpej 		 */
    497  1.11   thorpej 		if (dontqueue) {
    498  1.11   thorpej 			splx(s);
    499  1.11   thorpej 			xs->error = XS_DRIVER_STUFFUP;
    500  1.11   thorpej 			return (TRY_AGAIN_LATER);
    501  1.11   thorpej 		}
    502  1.11   thorpej 
    503  1.11   thorpej 		/*
    504  1.11   thorpej 		 * Swap with the first queue entry.
    505  1.11   thorpej 		 */
    506  1.21   thorpej 		TAILQ_INSERT_TAIL(&sc->sc_queue, xs, adapter_q);
    507  1.21   thorpej 		xs = TAILQ_FIRST(&sc->sc_queue);
    508  1.21   thorpej 		TAILQ_REMOVE(&sc->sc_queue, xs, adapter_q);
    509  1.11   thorpej 		fromqueue = 1;
    510  1.11   thorpej 	}
    511  1.11   thorpej 
    512  1.11   thorpej  get_mscp:
    513   1.1   mycroft 	/*
    514   1.1   mycroft 	 * get a mscp (mbox-out) to use. If the transfer
    515   1.1   mycroft 	 * is from a buf (possibly from interrupt time)
    516   1.1   mycroft 	 * then we can't allow it to sleep
    517   1.1   mycroft 	 */
    518  1.22   thorpej 	flags = xs->xs_control;
    519  1.23   thorpej 	if (nowait)
    520  1.23   thorpej 		flags |= XS_CTL_NOSLEEP;
    521   1.1   mycroft 	if ((mscp = uha_get_mscp(sc, flags)) == NULL) {
    522  1.11   thorpej 		/*
    523  1.11   thorpej 		 * If we can't queue, we lose.
    524  1.11   thorpej 		 */
    525  1.11   thorpej 		if (dontqueue) {
    526  1.11   thorpej 			splx(s);
    527  1.11   thorpej 			xs->error = XS_DRIVER_STUFFUP;
    528  1.11   thorpej 			return (TRY_AGAIN_LATER);
    529  1.11   thorpej 		}
    530  1.11   thorpej 
    531  1.11   thorpej 		/*
    532  1.11   thorpej 		 * Stuff ourselves into the queue, in front
    533  1.11   thorpej 		 * if we came off in the first place.
    534  1.11   thorpej 		 */
    535  1.21   thorpej 		if (fromqueue)
    536  1.21   thorpej 			TAILQ_INSERT_HEAD(&sc->sc_queue, xs, adapter_q);
    537  1.21   thorpej 		else
    538  1.21   thorpej 			TAILQ_INSERT_TAIL(&sc->sc_queue, xs, adapter_q);
    539  1.11   thorpej 		splx(s);
    540  1.11   thorpej 		return (SUCCESSFULLY_QUEUED);
    541   1.1   mycroft 	}
    542  1.11   thorpej 
    543  1.11   thorpej 	splx(s);		/* done playing with the queue */
    544  1.11   thorpej 
    545   1.1   mycroft 	mscp->xs = xs;
    546   1.1   mycroft 	mscp->timeout = xs->timeout;
    547   1.1   mycroft 
    548   1.1   mycroft 	/*
    549   1.1   mycroft 	 * Put all the arguments for the xfer in the mscp
    550   1.1   mycroft 	 */
    551  1.22   thorpej 	if (flags & XS_CTL_RESET) {
    552   1.1   mycroft 		mscp->opcode = UHA_SDR;
    553   1.1   mycroft 		mscp->ca = 0x01;
    554   1.1   mycroft 	} else {
    555   1.1   mycroft 		mscp->opcode = UHA_TSP;
    556   1.1   mycroft 		/* XXX Not for tapes. */
    557   1.1   mycroft 		mscp->ca = 0x01;
    558   1.1   mycroft 		bcopy(xs->cmd, &mscp->scsi_cmd, mscp->scsi_cmd_length);
    559   1.1   mycroft 	}
    560   1.1   mycroft 	mscp->xdir = UHA_SDET;
    561   1.1   mycroft 	mscp->dcn = 0x00;
    562   1.1   mycroft 	mscp->chan = 0x00;
    563   1.9    bouyer 	mscp->target = sc_link->scsipi_scsi.target;
    564   1.9    bouyer 	mscp->lun = sc_link->scsipi_scsi.lun;
    565   1.1   mycroft 	mscp->scsi_cmd_length = xs->cmdlen;
    566  1.16   thorpej 	mscp->sense_ptr = sc->sc_dmamap_mscp->dm_segs[0].ds_addr +
    567  1.16   thorpej 	    UHA_MSCP_OFF(mscp) + offsetof(struct uha_mscp, mscp_sense);
    568   1.1   mycroft 	mscp->req_sense_length = sizeof(mscp->mscp_sense);
    569   1.1   mycroft 	mscp->host_stat = 0x00;
    570   1.1   mycroft 	mscp->target_stat = 0x00;
    571   1.1   mycroft 
    572   1.1   mycroft 	if (xs->datalen) {
    573   1.1   mycroft 		sg = mscp->uha_dma;
    574   1.1   mycroft 		seg = 0;
    575   1.1   mycroft #ifdef	TFS
    576   1.1   mycroft 		if (flags & SCSI_DATA_UIO) {
    577   1.8   thorpej 			error = bus_dmamap_load_uio(dmat,
    578   1.8   thorpej 			    mscp->dmamap_xfer, (struct uio *)xs->data,
    579  1.22   thorpej 			    (flags & XS_CTL_NOSLEEP) ? BUS_DMA_NOWAIT :
    580   1.8   thorpej 			    BUS_DMA_WAITOK);
    581   1.1   mycroft 		} else
    582   1.1   mycroft #endif /*TFS */
    583   1.1   mycroft 		{
    584   1.8   thorpej 			error = bus_dmamap_load(dmat,
    585   1.8   thorpej 			    mscp->dmamap_xfer, xs->data, xs->datalen, NULL,
    586  1.22   thorpej 			    (flags & XS_CTL_NOSLEEP) ? BUS_DMA_NOWAIT :
    587   1.8   thorpej 			    BUS_DMA_WAITOK);
    588   1.8   thorpej 		}
    589   1.8   thorpej 
    590   1.8   thorpej 		if (error) {
    591   1.8   thorpej 			if (error == EFBIG) {
    592   1.8   thorpej 				printf("%s: uha_scsi_cmd, more than %d"
    593   1.8   thorpej 				    " dma segments\n",
    594   1.8   thorpej 				    sc->sc_dev.dv_xname, UHA_NSEG);
    595   1.8   thorpej 			} else {
    596   1.8   thorpej 				printf("%s: uha_scsi_cmd, error %d loading"
    597   1.8   thorpej 				    " dma map\n",
    598   1.8   thorpej 				    sc->sc_dev.dv_xname, error);
    599   1.1   mycroft 			}
    600   1.8   thorpej 			goto bad;
    601   1.1   mycroft 		}
    602   1.8   thorpej 
    603  1.15   thorpej 		bus_dmamap_sync(dmat, mscp->dmamap_xfer, 0,
    604  1.15   thorpej 		    mscp->dmamap_xfer->dm_mapsize,
    605  1.22   thorpej 		    (flags & XS_CTL_DATA_IN) ? BUS_DMASYNC_PREREAD :
    606   1.8   thorpej 		    BUS_DMASYNC_PREWRITE);
    607   1.8   thorpej 
    608   1.8   thorpej 		/*
    609   1.8   thorpej 		 * Load the hardware scatter/gather map with the
    610   1.8   thorpej 		 * contents of the DMA map.
    611   1.8   thorpej 		 */
    612   1.8   thorpej 		for (seg = 0; seg < mscp->dmamap_xfer->dm_nsegs; seg++) {
    613   1.8   thorpej 			mscp->uha_dma[seg].seg_addr =
    614   1.8   thorpej 			    mscp->dmamap_xfer->dm_segs[seg].ds_addr;
    615   1.8   thorpej 			mscp->uha_dma[seg].seg_len =
    616   1.8   thorpej 			    mscp->dmamap_xfer->dm_segs[seg].ds_len;
    617   1.1   mycroft 		}
    618   1.8   thorpej 
    619  1.16   thorpej 		mscp->data_addr = sc->sc_dmamap_mscp->dm_segs[0].ds_addr +
    620  1.16   thorpej 		    UHA_MSCP_OFF(mscp) + offsetof(struct uha_mscp, uha_dma);
    621   1.1   mycroft 		mscp->data_length = xs->datalen;
    622   1.1   mycroft 		mscp->sgth = 0x01;
    623   1.1   mycroft 		mscp->sg_num = seg;
    624   1.1   mycroft 	} else {		/* No data xfer, use non S/G values */
    625   1.1   mycroft 		mscp->data_addr = (physaddr)0;
    626   1.1   mycroft 		mscp->data_length = 0;
    627   1.1   mycroft 		mscp->sgth = 0x00;
    628   1.1   mycroft 		mscp->sg_num = 0;
    629   1.1   mycroft 	}
    630   1.1   mycroft 	mscp->link_id = 0;
    631   1.1   mycroft 	mscp->link_addr = (physaddr)0;
    632  1.16   thorpej 
    633  1.16   thorpej 	bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_mscp,
    634  1.16   thorpej 	    UHA_MSCP_OFF(mscp), sizeof(struct uha_mscp),
    635  1.16   thorpej 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    636   1.1   mycroft 
    637   1.1   mycroft 	s = splbio();
    638   1.1   mycroft 	(sc->start_mbox)(sc, mscp);
    639   1.1   mycroft 	splx(s);
    640   1.1   mycroft 
    641   1.1   mycroft 	/*
    642   1.1   mycroft 	 * Usually return SUCCESSFULLY QUEUED
    643   1.1   mycroft 	 */
    644  1.22   thorpej 	if ((flags & XS_CTL_POLL) == 0)
    645   1.1   mycroft 		return (SUCCESSFULLY_QUEUED);
    646   1.1   mycroft 
    647   1.1   mycroft 	/*
    648   1.1   mycroft 	 * If we can't use interrupts, poll on completion
    649   1.1   mycroft 	 */
    650   1.1   mycroft 	if ((sc->poll)(sc, xs, mscp->timeout)) {
    651   1.1   mycroft 		uha_timeout(mscp);
    652   1.1   mycroft 		if ((sc->poll)(sc, xs, mscp->timeout))
    653   1.1   mycroft 			uha_timeout(mscp);
    654   1.1   mycroft 	}
    655   1.1   mycroft 	return (COMPLETE);
    656   1.1   mycroft 
    657   1.1   mycroft bad:
    658   1.1   mycroft 	xs->error = XS_DRIVER_STUFFUP;
    659   1.1   mycroft 	uha_free_mscp(sc, mscp);
    660   1.1   mycroft 	return (COMPLETE);
    661   1.1   mycroft }
    662   1.1   mycroft 
    663   1.1   mycroft void
    664   1.1   mycroft uha_timeout(arg)
    665   1.1   mycroft 	void *arg;
    666   1.1   mycroft {
    667   1.1   mycroft 	struct uha_mscp *mscp = arg;
    668   1.9    bouyer 	struct scsipi_xfer *xs = mscp->xs;
    669   1.9    bouyer 	struct scsipi_link *sc_link = xs->sc_link;
    670   1.1   mycroft 	struct uha_softc *sc = sc_link->adapter_softc;
    671   1.1   mycroft 	int s;
    672   1.1   mycroft 
    673   1.9    bouyer 	scsi_print_addr(sc_link);
    674   1.3  christos 	printf("timed out");
    675   1.1   mycroft 
    676   1.1   mycroft 	s = splbio();
    677   1.1   mycroft 
    678   1.1   mycroft 	if (mscp->flags & MSCP_ABORT) {
    679   1.1   mycroft 		/* abort timed out */
    680   1.3  christos 		printf(" AGAIN\n");
    681   1.1   mycroft 		/* XXX Must reset! */
    682   1.1   mycroft 	} else {
    683   1.1   mycroft 		/* abort the operation that has timed out */
    684   1.3  christos 		printf("\n");
    685   1.1   mycroft 		mscp->xs->error = XS_TIMEOUT;
    686   1.1   mycroft 		mscp->timeout = UHA_ABORT_TIMEOUT;
    687   1.1   mycroft 		mscp->flags |= MSCP_ABORT;
    688   1.1   mycroft 		(sc->start_mbox)(sc, mscp);
    689   1.1   mycroft 	}
    690   1.1   mycroft 
    691   1.1   mycroft 	splx(s);
    692   1.1   mycroft }
    693